VIA_VELOCITY: Dont oops on MTU change.
[linux-2.6.22.y-op.git] / drivers / net / via-velocity.c
blob63a2f81b07b6511f1ae4681d5a7d47dd9f5a72a0
1 /*
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.
10 * TODO
11 * Big-endian support
12 * rx_copybreak/alignment
13 * Scatter gather
14 * More testing
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
22 * list not VIA.
24 * Original code:
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
32 * any later version.
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
37 * for more details.
39 * Author: Chuang Liang-Shing, AJ Jiang
41 * Date: Jan 24, 2003
43 * MODULE_LICENSE("GPL");
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/init.h>
51 #include <linux/mm.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>
65 #include <asm/io.h>
66 #include <linux/if.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>
73 #include <linux/in.h>
74 #include <linux/if_arp.h>
75 #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>
81 #include "via-velocity.h"
84 static int velocity_nics = 0;
85 static int msglevel = MSG_LEVEL_INFO;
88 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
89 static const struct ethtool_ops velocity_ethtool_ops;
92 Define module options
95 MODULE_AUTHOR("VIA Networking Technologies, Inc.");
96 MODULE_LICENSE("GPL");
97 MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
99 #define VELOCITY_PARAM(N,D) \
100 static int N[MAX_UNITS]=OPTION_DEFAULT;\
101 module_param_array(N, int, NULL, 0); \
102 MODULE_PARM_DESC(N, D);
104 #define RX_DESC_MIN 64
105 #define RX_DESC_MAX 255
106 #define RX_DESC_DEF 64
107 VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
109 #define TX_DESC_MIN 16
110 #define TX_DESC_MAX 256
111 #define TX_DESC_DEF 64
112 VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
114 #define VLAN_ID_MIN 0
115 #define VLAN_ID_MAX 4095
116 #define VLAN_ID_DEF 0
117 /* VID_setting[] is used for setting the VID of NIC.
118 0: default VID.
119 1-4094: other VIDs.
121 VELOCITY_PARAM(VID_setting, "802.1Q VLAN ID");
123 #define RX_THRESH_MIN 0
124 #define RX_THRESH_MAX 3
125 #define RX_THRESH_DEF 0
126 /* rx_thresh[] is used for controlling the receive fifo threshold.
127 0: indicate the rxfifo threshold is 128 bytes.
128 1: indicate the rxfifo threshold is 512 bytes.
129 2: indicate the rxfifo threshold is 1024 bytes.
130 3: indicate the rxfifo threshold is store & forward.
132 VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
134 #define DMA_LENGTH_MIN 0
135 #define DMA_LENGTH_MAX 7
136 #define DMA_LENGTH_DEF 0
138 /* DMA_length[] is used for controlling the DMA length
139 0: 8 DWORDs
140 1: 16 DWORDs
141 2: 32 DWORDs
142 3: 64 DWORDs
143 4: 128 DWORDs
144 5: 256 DWORDs
145 6: SF(flush till emply)
146 7: SF(flush till emply)
148 VELOCITY_PARAM(DMA_length, "DMA length");
150 #define TAGGING_DEF 0
151 /* enable_tagging[] is used for enabling 802.1Q VID tagging.
152 0: disable VID seeting(default).
153 1: enable VID setting.
155 VELOCITY_PARAM(enable_tagging, "Enable 802.1Q tagging");
157 #define IP_ALIG_DEF 0
158 /* IP_byte_align[] is used for IP header DWORD byte aligned
159 0: indicate the IP header won't be DWORD byte aligned.(Default) .
160 1: indicate the IP header will be DWORD byte aligned.
161 In some enviroment, the IP header should be DWORD byte aligned,
162 or the packet will be droped when we receive it. (eg: IPVS)
164 VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
166 #define TX_CSUM_DEF 1
167 /* txcsum_offload[] is used for setting the checksum offload ability of NIC.
168 (We only support RX checksum offload now)
169 0: disable csum_offload[checksum offload
170 1: enable checksum offload. (Default)
172 VELOCITY_PARAM(txcsum_offload, "Enable transmit packet checksum offload");
174 #define FLOW_CNTL_DEF 1
175 #define FLOW_CNTL_MIN 1
176 #define FLOW_CNTL_MAX 5
178 /* flow_control[] is used for setting the flow control ability of NIC.
179 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
180 2: enable TX flow control.
181 3: enable RX flow control.
182 4: enable RX/TX flow control.
183 5: disable
185 VELOCITY_PARAM(flow_control, "Enable flow control ability");
187 #define MED_LNK_DEF 0
188 #define MED_LNK_MIN 0
189 #define MED_LNK_MAX 4
190 /* speed_duplex[] is used for setting the speed and duplex mode of NIC.
191 0: indicate autonegotiation for both speed and duplex mode
192 1: indicate 100Mbps half duplex mode
193 2: indicate 100Mbps full duplex mode
194 3: indicate 10Mbps half duplex mode
195 4: indicate 10Mbps full duplex mode
197 Note:
198 if EEPROM have been set to the force mode, this option is ignored
199 by driver.
201 VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
203 #define VAL_PKT_LEN_DEF 0
204 /* ValPktLen[] is used for setting the checksum offload ability of NIC.
205 0: Receive frame with invalid layer 2 length (Default)
206 1: Drop frame with invalid layer 2 length
208 VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
210 #define WOL_OPT_DEF 0
211 #define WOL_OPT_MIN 0
212 #define WOL_OPT_MAX 7
213 /* wol_opts[] is used for controlling wake on lan behavior.
214 0: Wake up if recevied a magic packet. (Default)
215 1: Wake up if link status is on/off.
216 2: Wake up if recevied an arp packet.
217 4: Wake up if recevied any unicast packet.
218 Those value can be sumed up to support more than one option.
220 VELOCITY_PARAM(wol_opts, "Wake On Lan options");
222 #define INT_WORKS_DEF 20
223 #define INT_WORKS_MIN 10
224 #define INT_WORKS_MAX 64
226 VELOCITY_PARAM(int_works, "Number of packets per interrupt services");
228 static int rx_copybreak = 200;
229 module_param(rx_copybreak, int, 0644);
230 MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
232 static void velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr,
233 const struct velocity_info_tbl *info);
234 static int velocity_get_pci_info(struct velocity_info *, struct pci_dev *pdev);
235 static void velocity_print_info(struct velocity_info *vptr);
236 static int velocity_open(struct net_device *dev);
237 static int velocity_change_mtu(struct net_device *dev, int mtu);
238 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev);
239 static int velocity_intr(int irq, void *dev_instance);
240 static void velocity_set_multi(struct net_device *dev);
241 static struct net_device_stats *velocity_get_stats(struct net_device *dev);
242 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
243 static int velocity_close(struct net_device *dev);
244 static int velocity_receive_frame(struct velocity_info *, int idx);
245 static int velocity_alloc_rx_buf(struct velocity_info *, int idx);
246 static void velocity_free_rd_ring(struct velocity_info *vptr);
247 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *);
248 static int velocity_soft_reset(struct velocity_info *vptr);
249 static void mii_init(struct velocity_info *vptr, u32 mii_status);
250 static u32 velocity_get_link(struct net_device *dev);
251 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr);
252 static void velocity_print_link_status(struct velocity_info *vptr);
253 static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs);
254 static void velocity_shutdown(struct velocity_info *vptr);
255 static void enable_flow_control_ability(struct velocity_info *vptr);
256 static void enable_mii_autopoll(struct mac_regs __iomem * regs);
257 static int velocity_mii_read(struct mac_regs __iomem *, u8 byIdx, u16 * pdata);
258 static int velocity_mii_write(struct mac_regs __iomem *, u8 byMiiAddr, u16 data);
259 static u32 mii_check_media_mode(struct mac_regs __iomem * regs);
260 static u32 check_connection_type(struct mac_regs __iomem * regs);
261 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status);
263 #ifdef CONFIG_PM
265 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state);
266 static int velocity_resume(struct pci_dev *pdev);
268 static DEFINE_SPINLOCK(velocity_dev_list_lock);
269 static LIST_HEAD(velocity_dev_list);
271 #endif
273 #if defined(CONFIG_PM) && defined(CONFIG_INET)
275 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr);
277 static struct notifier_block velocity_inetaddr_notifier = {
278 .notifier_call = velocity_netdev_event,
281 static void velocity_register_notifier(void)
283 register_inetaddr_notifier(&velocity_inetaddr_notifier);
286 static void velocity_unregister_notifier(void)
288 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
291 #else
293 #define velocity_register_notifier() do {} while (0)
294 #define velocity_unregister_notifier() do {} while (0)
296 #endif
299 * Internal board variants. At the moment we have only one
302 static const struct velocity_info_tbl chip_info_table[] __devinitdata = {
303 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
308 * Describe the PCI device identifiers that we support in this
309 * device driver. Used for hotplug autoloading.
312 static const struct pci_device_id velocity_id_table[] __devinitdata = {
313 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
317 MODULE_DEVICE_TABLE(pci, velocity_id_table);
320 * get_chip_name - identifier to name
321 * @id: chip identifier
323 * Given a chip identifier return a suitable description. Returns
324 * a pointer a static string valid while the driver is loaded.
327 static char __devinit *get_chip_name(enum chip_type chip_id)
329 int i;
330 for (i = 0; chip_info_table[i].name != NULL; i++)
331 if (chip_info_table[i].chip_id == chip_id)
332 break;
333 return chip_info_table[i].name;
337 * velocity_remove1 - device unplug
338 * @pdev: PCI device being removed
340 * Device unload callback. Called on an unplug or on module
341 * unload for each active device that is present. Disconnects
342 * the device from the network layer and frees all the resources
345 static void __devexit velocity_remove1(struct pci_dev *pdev)
347 struct net_device *dev = pci_get_drvdata(pdev);
348 struct velocity_info *vptr = netdev_priv(dev);
350 #ifdef CONFIG_PM
351 unsigned long flags;
353 spin_lock_irqsave(&velocity_dev_list_lock, flags);
354 if (!list_empty(&velocity_dev_list))
355 list_del(&vptr->list);
356 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
357 #endif
358 unregister_netdev(dev);
359 iounmap(vptr->mac_regs);
360 pci_release_regions(pdev);
361 pci_disable_device(pdev);
362 pci_set_drvdata(pdev, NULL);
363 free_netdev(dev);
365 velocity_nics--;
369 * velocity_set_int_opt - parser for integer options
370 * @opt: pointer to option value
371 * @val: value the user requested (or -1 for default)
372 * @min: lowest value allowed
373 * @max: highest value allowed
374 * @def: default value
375 * @name: property name
376 * @dev: device name
378 * Set an integer property in the module options. This function does
379 * all the verification and checking as well as reporting so that
380 * we don't duplicate code for each option.
383 static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max, int def, char *name, char *devname)
385 if (val == -1)
386 *opt = def;
387 else if (val < min || val > max) {
388 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
389 devname, name, min, max);
390 *opt = def;
391 } else {
392 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
393 devname, name, val);
394 *opt = val;
399 * velocity_set_bool_opt - parser for boolean options
400 * @opt: pointer to option value
401 * @val: value the user requested (or -1 for default)
402 * @def: default value (yes/no)
403 * @flag: numeric value to set for true.
404 * @name: property name
405 * @dev: device name
407 * Set a boolean property in the module options. This function does
408 * all the verification and checking as well as reporting so that
409 * we don't duplicate code for each option.
412 static void __devinit velocity_set_bool_opt(u32 * opt, int val, int def, u32 flag, char *name, char *devname)
414 (*opt) &= (~flag);
415 if (val == -1)
416 *opt |= (def ? flag : 0);
417 else if (val < 0 || val > 1) {
418 printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
419 devname, name);
420 *opt |= (def ? flag : 0);
421 } else {
422 printk(KERN_INFO "%s: set parameter %s to %s\n",
423 devname, name, val ? "TRUE" : "FALSE");
424 *opt |= (val ? flag : 0);
429 * velocity_get_options - set options on device
430 * @opts: option structure for the device
431 * @index: index of option to use in module options array
432 * @devname: device name
434 * Turn the module and command options into a single structure
435 * for the current device
438 static void __devinit velocity_get_options(struct velocity_opt *opts, int index, char *devname)
441 velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
442 velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
443 velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
444 velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
445 velocity_set_int_opt(&opts->vid, VID_setting[index], VLAN_ID_MIN, VLAN_ID_MAX, VLAN_ID_DEF, "VID_setting", devname);
446 velocity_set_bool_opt(&opts->flags, enable_tagging[index], TAGGING_DEF, VELOCITY_FLAGS_TAGGING, "enable_tagging", devname);
447 velocity_set_bool_opt(&opts->flags, txcsum_offload[index], TX_CSUM_DEF, VELOCITY_FLAGS_TX_CSUM, "txcsum_offload", devname);
448 velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
449 velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
450 velocity_set_bool_opt(&opts->flags, ValPktLen[index], VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN, "ValPktLen", devname);
451 velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
452 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);
453 velocity_set_int_opt((int *) &opts->int_works, int_works[index], INT_WORKS_MIN, INT_WORKS_MAX, INT_WORKS_DEF, "Interrupt service works", devname);
454 opts->numrx = (opts->numrx & ~3);
458 * velocity_init_cam_filter - initialise CAM
459 * @vptr: velocity to program
461 * Initialize the content addressable memory used for filters. Load
462 * appropriately according to the presence of VLAN
465 static void velocity_init_cam_filter(struct velocity_info *vptr)
467 struct mac_regs __iomem * regs = vptr->mac_regs;
469 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
470 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
471 WORD_REG_BITS_ON(MCFG_VIDFR, &regs->MCFG);
473 /* Disable all CAMs */
474 memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
475 memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
476 mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
477 mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
479 /* Enable first VCAM */
480 if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
481 /* If Tagging option is enabled and VLAN ID is not zero, then
482 turn on MCFG_RTGOPT also */
483 if (vptr->options.vid != 0)
484 WORD_REG_BITS_ON(MCFG_RTGOPT, &regs->MCFG);
486 mac_set_cam(regs, 0, (u8 *) & (vptr->options.vid), VELOCITY_VLAN_ID_CAM);
487 vptr->vCAMmask[0] |= 1;
488 mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
489 } else {
490 u16 temp = 0;
491 mac_set_cam(regs, 0, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
492 temp = 1;
493 mac_set_cam_mask(regs, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
498 * velocity_rx_reset - handle a receive reset
499 * @vptr: velocity we are resetting
501 * Reset the ownership and status for the receive ring side.
502 * Hand all the receive queue to the NIC.
505 static void velocity_rx_reset(struct velocity_info *vptr)
508 struct mac_regs __iomem * regs = vptr->mac_regs;
509 int i;
511 vptr->rd_dirty = vptr->rd_filled = vptr->rd_curr = 0;
514 * Init state, all RD entries belong to the NIC
516 for (i = 0; i < vptr->options.numrx; ++i)
517 vptr->rd_ring[i].rdesc0.owner = OWNED_BY_NIC;
519 writew(vptr->options.numrx, &regs->RBRDU);
520 writel(vptr->rd_pool_dma, &regs->RDBaseLo);
521 writew(0, &regs->RDIdx);
522 writew(vptr->options.numrx - 1, &regs->RDCSize);
526 * velocity_init_registers - initialise MAC registers
527 * @vptr: velocity to init
528 * @type: type of initialisation (hot or cold)
530 * Initialise the MAC on a reset or on first set up on the
531 * hardware.
534 static void velocity_init_registers(struct velocity_info *vptr,
535 enum velocity_init_type type)
537 struct mac_regs __iomem * regs = vptr->mac_regs;
538 int i, mii_status;
540 mac_wol_reset(regs);
542 switch (type) {
543 case VELOCITY_INIT_RESET:
544 case VELOCITY_INIT_WOL:
546 netif_stop_queue(vptr->dev);
549 * Reset RX to prevent RX pointer not on the 4X location
551 velocity_rx_reset(vptr);
552 mac_rx_queue_run(regs);
553 mac_rx_queue_wake(regs);
555 mii_status = velocity_get_opt_media_mode(vptr);
556 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
557 velocity_print_link_status(vptr);
558 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
559 netif_wake_queue(vptr->dev);
562 enable_flow_control_ability(vptr);
564 mac_clear_isr(regs);
565 writel(CR0_STOP, &regs->CR0Clr);
566 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
567 &regs->CR0Set);
569 break;
571 case VELOCITY_INIT_COLD:
572 default:
574 * Do reset
576 velocity_soft_reset(vptr);
577 mdelay(5);
579 mac_eeprom_reload(regs);
580 for (i = 0; i < 6; i++) {
581 writeb(vptr->dev->dev_addr[i], &(regs->PAR[i]));
584 * clear Pre_ACPI bit.
586 BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
587 mac_set_rx_thresh(regs, vptr->options.rx_thresh);
588 mac_set_dma_length(regs, vptr->options.DMA_length);
590 writeb(WOLCFG_SAM | WOLCFG_SAB, &regs->WOLCFGSet);
592 * Back off algorithm use original IEEE standard
594 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), &regs->CFGB);
597 * Init CAM filter
599 velocity_init_cam_filter(vptr);
602 * Set packet filter: Receive directed and broadcast address
604 velocity_set_multi(vptr->dev);
607 * Enable MII auto-polling
609 enable_mii_autopoll(regs);
611 vptr->int_mask = INT_MASK_DEF;
613 writel(cpu_to_le32(vptr->rd_pool_dma), &regs->RDBaseLo);
614 writew(vptr->options.numrx - 1, &regs->RDCSize);
615 mac_rx_queue_run(regs);
616 mac_rx_queue_wake(regs);
618 writew(vptr->options.numtx - 1, &regs->TDCSize);
620 for (i = 0; i < vptr->num_txq; i++) {
621 writel(cpu_to_le32(vptr->td_pool_dma[i]), &(regs->TDBaseLo[i]));
622 mac_tx_queue_run(regs, i);
625 init_flow_control_register(vptr);
627 writel(CR0_STOP, &regs->CR0Clr);
628 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), &regs->CR0Set);
630 mii_status = velocity_get_opt_media_mode(vptr);
631 netif_stop_queue(vptr->dev);
633 mii_init(vptr, mii_status);
635 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
636 velocity_print_link_status(vptr);
637 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
638 netif_wake_queue(vptr->dev);
641 enable_flow_control_ability(vptr);
642 mac_hw_mibs_init(regs);
643 mac_write_int_mask(vptr->int_mask, regs);
644 mac_clear_isr(regs);
650 * velocity_soft_reset - soft reset
651 * @vptr: velocity to reset
653 * Kick off a soft reset of the velocity adapter and then poll
654 * until the reset sequence has completed before returning.
657 static int velocity_soft_reset(struct velocity_info *vptr)
659 struct mac_regs __iomem * regs = vptr->mac_regs;
660 int i = 0;
662 writel(CR0_SFRST, &regs->CR0Set);
664 for (i = 0; i < W_MAX_TIMEOUT; i++) {
665 udelay(5);
666 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
667 break;
670 if (i == W_MAX_TIMEOUT) {
671 writel(CR0_FORSRST, &regs->CR0Set);
672 /* FIXME: PCI POSTING */
673 /* delay 2ms */
674 mdelay(2);
676 return 0;
680 * velocity_found1 - set up discovered velocity card
681 * @pdev: PCI device
682 * @ent: PCI device table entry that matched
684 * Configure a discovered adapter from scratch. Return a negative
685 * errno error code on failure paths.
688 static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
690 static int first = 1;
691 struct net_device *dev;
692 int i;
693 const struct velocity_info_tbl *info = &chip_info_table[ent->driver_data];
694 struct velocity_info *vptr;
695 struct mac_regs __iomem * regs;
696 int ret = -ENOMEM;
698 /* FIXME: this driver, like almost all other ethernet drivers,
699 * can support more than MAX_UNITS.
701 if (velocity_nics >= MAX_UNITS) {
702 dev_notice(&pdev->dev, "already found %d NICs.\n",
703 velocity_nics);
704 return -ENODEV;
707 dev = alloc_etherdev(sizeof(struct velocity_info));
708 if (!dev) {
709 dev_err(&pdev->dev, "allocate net device failed.\n");
710 goto out;
713 /* Chain it all together */
715 SET_MODULE_OWNER(dev);
716 SET_NETDEV_DEV(dev, &pdev->dev);
717 vptr = netdev_priv(dev);
720 if (first) {
721 printk(KERN_INFO "%s Ver. %s\n",
722 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
723 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
724 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
725 first = 0;
728 velocity_init_info(pdev, vptr, info);
730 vptr->dev = dev;
732 dev->irq = pdev->irq;
734 ret = pci_enable_device(pdev);
735 if (ret < 0)
736 goto err_free_dev;
738 ret = velocity_get_pci_info(vptr, pdev);
739 if (ret < 0) {
740 /* error message already printed */
741 goto err_disable;
744 ret = pci_request_regions(pdev, VELOCITY_NAME);
745 if (ret < 0) {
746 dev_err(&pdev->dev, "No PCI resources.\n");
747 goto err_disable;
750 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
751 if (regs == NULL) {
752 ret = -EIO;
753 goto err_release_res;
756 vptr->mac_regs = regs;
758 mac_wol_reset(regs);
760 dev->base_addr = vptr->ioaddr;
762 for (i = 0; i < 6; i++)
763 dev->dev_addr[i] = readb(&regs->PAR[i]);
766 velocity_get_options(&vptr->options, velocity_nics, dev->name);
769 * Mask out the options cannot be set to the chip
772 vptr->options.flags &= info->flags;
775 * Enable the chip specified capbilities
778 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
780 vptr->wol_opts = vptr->options.wol_opts;
781 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
783 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
785 dev->irq = pdev->irq;
786 dev->open = velocity_open;
787 dev->hard_start_xmit = velocity_xmit;
788 dev->stop = velocity_close;
789 dev->get_stats = velocity_get_stats;
790 dev->set_multicast_list = velocity_set_multi;
791 dev->do_ioctl = velocity_ioctl;
792 dev->ethtool_ops = &velocity_ethtool_ops;
793 dev->change_mtu = velocity_change_mtu;
794 #ifdef VELOCITY_ZERO_COPY_SUPPORT
795 dev->features |= NETIF_F_SG;
796 #endif
798 if (vptr->flags & VELOCITY_FLAGS_TX_CSUM) {
799 dev->features |= NETIF_F_IP_CSUM;
802 ret = register_netdev(dev);
803 if (ret < 0)
804 goto err_iounmap;
806 if (velocity_get_link(dev))
807 netif_carrier_off(dev);
809 velocity_print_info(vptr);
810 pci_set_drvdata(pdev, dev);
812 /* and leave the chip powered down */
814 pci_set_power_state(pdev, PCI_D3hot);
815 #ifdef CONFIG_PM
817 unsigned long flags;
819 spin_lock_irqsave(&velocity_dev_list_lock, flags);
820 list_add(&vptr->list, &velocity_dev_list);
821 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
823 #endif
824 velocity_nics++;
825 out:
826 return ret;
828 err_iounmap:
829 iounmap(regs);
830 err_release_res:
831 pci_release_regions(pdev);
832 err_disable:
833 pci_disable_device(pdev);
834 err_free_dev:
835 free_netdev(dev);
836 goto out;
840 * velocity_print_info - per driver data
841 * @vptr: velocity
843 * Print per driver data as the kernel driver finds Velocity
844 * hardware
847 static void __devinit velocity_print_info(struct velocity_info *vptr)
849 struct net_device *dev = vptr->dev;
851 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
852 printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
853 dev->name,
854 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
855 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
859 * velocity_init_info - init private data
860 * @pdev: PCI device
861 * @vptr: Velocity info
862 * @info: Board type
864 * Set up the initial velocity_info struct for the device that has been
865 * discovered.
868 static void __devinit velocity_init_info(struct pci_dev *pdev,
869 struct velocity_info *vptr,
870 const struct velocity_info_tbl *info)
872 memset(vptr, 0, sizeof(struct velocity_info));
874 vptr->pdev = pdev;
875 vptr->chip_id = info->chip_id;
876 vptr->num_txq = info->txqueue;
877 vptr->multicast_limit = MCAM_SIZE;
878 spin_lock_init(&vptr->lock);
879 INIT_LIST_HEAD(&vptr->list);
883 * velocity_get_pci_info - retrieve PCI info for device
884 * @vptr: velocity device
885 * @pdev: PCI device it matches
887 * Retrieve the PCI configuration space data that interests us from
888 * the kernel PCI layer
891 static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
893 if (pci_read_config_byte(pdev, PCI_REVISION_ID, &vptr->rev_id) < 0)
894 return -EIO;
896 pci_set_master(pdev);
898 vptr->ioaddr = pci_resource_start(pdev, 0);
899 vptr->memaddr = pci_resource_start(pdev, 1);
901 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
902 dev_err(&pdev->dev,
903 "region #0 is not an I/O resource, aborting.\n");
904 return -EINVAL;
907 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
908 dev_err(&pdev->dev,
909 "region #1 is an I/O resource, aborting.\n");
910 return -EINVAL;
913 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
914 dev_err(&pdev->dev, "region #1 is too small.\n");
915 return -EINVAL;
917 vptr->pdev = pdev;
919 return 0;
923 * velocity_init_rings - set up DMA rings
924 * @vptr: Velocity to set up
926 * Allocate PCI mapped DMA rings for the receive and transmit layer
927 * to use.
930 static int velocity_init_rings(struct velocity_info *vptr)
932 int i;
933 unsigned int psize;
934 unsigned int tsize;
935 dma_addr_t pool_dma;
936 u8 *pool;
939 * Allocate all RD/TD rings a single pool
942 psize = vptr->options.numrx * sizeof(struct rx_desc) +
943 vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
946 * pci_alloc_consistent() fulfills the requirement for 64 bytes
947 * alignment
949 pool = pci_alloc_consistent(vptr->pdev, psize, &pool_dma);
951 if (pool == NULL) {
952 printk(KERN_ERR "%s : DMA memory allocation failed.\n",
953 vptr->dev->name);
954 return -ENOMEM;
957 memset(pool, 0, psize);
959 vptr->rd_ring = (struct rx_desc *) pool;
961 vptr->rd_pool_dma = pool_dma;
963 tsize = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
964 vptr->tx_bufs = pci_alloc_consistent(vptr->pdev, tsize,
965 &vptr->tx_bufs_dma);
967 if (vptr->tx_bufs == NULL) {
968 printk(KERN_ERR "%s: DMA memory allocation failed.\n",
969 vptr->dev->name);
970 pci_free_consistent(vptr->pdev, psize, pool, pool_dma);
971 return -ENOMEM;
974 memset(vptr->tx_bufs, 0, vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq);
976 i = vptr->options.numrx * sizeof(struct rx_desc);
977 pool += i;
978 pool_dma += i;
979 for (i = 0; i < vptr->num_txq; i++) {
980 int offset = vptr->options.numtx * sizeof(struct tx_desc);
982 vptr->td_pool_dma[i] = pool_dma;
983 vptr->td_rings[i] = (struct tx_desc *) pool;
984 pool += offset;
985 pool_dma += offset;
987 return 0;
991 * velocity_free_rings - free PCI ring pointers
992 * @vptr: Velocity to free from
994 * Clean up the PCI ring buffers allocated to this velocity.
997 static void velocity_free_rings(struct velocity_info *vptr)
999 int size;
1001 size = vptr->options.numrx * sizeof(struct rx_desc) +
1002 vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
1004 pci_free_consistent(vptr->pdev, size, vptr->rd_ring, vptr->rd_pool_dma);
1006 size = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
1008 pci_free_consistent(vptr->pdev, size, vptr->tx_bufs, vptr->tx_bufs_dma);
1011 static inline void velocity_give_many_rx_descs(struct velocity_info *vptr)
1013 struct mac_regs __iomem *regs = vptr->mac_regs;
1014 int avail, dirty, unusable;
1017 * RD number must be equal to 4X per hardware spec
1018 * (programming guide rev 1.20, p.13)
1020 if (vptr->rd_filled < 4)
1021 return;
1023 wmb();
1025 unusable = vptr->rd_filled & 0x0003;
1026 dirty = vptr->rd_dirty - unusable;
1027 for (avail = vptr->rd_filled & 0xfffc; avail; avail--) {
1028 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1029 vptr->rd_ring[dirty].rdesc0.owner = OWNED_BY_NIC;
1032 writew(vptr->rd_filled & 0xfffc, &regs->RBRDU);
1033 vptr->rd_filled = unusable;
1036 static int velocity_rx_refill(struct velocity_info *vptr)
1038 int dirty = vptr->rd_dirty, done = 0, ret = 0;
1040 do {
1041 struct rx_desc *rd = vptr->rd_ring + dirty;
1043 /* Fine for an all zero Rx desc at init time as well */
1044 if (rd->rdesc0.owner == OWNED_BY_NIC)
1045 break;
1047 if (!vptr->rd_info[dirty].skb) {
1048 ret = velocity_alloc_rx_buf(vptr, dirty);
1049 if (ret < 0)
1050 break;
1052 done++;
1053 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
1054 } while (dirty != vptr->rd_curr);
1056 if (done) {
1057 vptr->rd_dirty = dirty;
1058 vptr->rd_filled += done;
1059 velocity_give_many_rx_descs(vptr);
1062 return ret;
1066 * velocity_init_rd_ring - set up receive ring
1067 * @vptr: velocity to configure
1069 * Allocate and set up the receive buffers for each ring slot and
1070 * assign them to the network adapter.
1073 static int velocity_init_rd_ring(struct velocity_info *vptr)
1075 int ret = -ENOMEM;
1076 unsigned int rsize = sizeof(struct velocity_rd_info) *
1077 vptr->options.numrx;
1079 vptr->rd_info = kmalloc(rsize, GFP_KERNEL);
1080 if(vptr->rd_info == NULL)
1081 goto out;
1082 memset(vptr->rd_info, 0, rsize);
1084 vptr->rd_filled = vptr->rd_dirty = vptr->rd_curr = 0;
1086 ret = velocity_rx_refill(vptr);
1087 if (ret < 0) {
1088 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1089 "%s: failed to allocate RX buffer.\n", vptr->dev->name);
1090 velocity_free_rd_ring(vptr);
1092 out:
1093 return ret;
1097 * velocity_free_rd_ring - free receive ring
1098 * @vptr: velocity to clean up
1100 * Free the receive buffers for each ring slot and any
1101 * attached socket buffers that need to go away.
1104 static void velocity_free_rd_ring(struct velocity_info *vptr)
1106 int i;
1108 if (vptr->rd_info == NULL)
1109 return;
1111 for (i = 0; i < vptr->options.numrx; i++) {
1112 struct velocity_rd_info *rd_info = &(vptr->rd_info[i]);
1113 struct rx_desc *rd = vptr->rd_ring + i;
1115 memset(rd, 0, sizeof(*rd));
1117 if (!rd_info->skb)
1118 continue;
1119 pci_unmap_single(vptr->pdev, rd_info->skb_dma, vptr->rx_buf_sz,
1120 PCI_DMA_FROMDEVICE);
1121 rd_info->skb_dma = (dma_addr_t) NULL;
1123 dev_kfree_skb(rd_info->skb);
1124 rd_info->skb = NULL;
1127 kfree(vptr->rd_info);
1128 vptr->rd_info = NULL;
1132 * velocity_init_td_ring - set up transmit ring
1133 * @vptr: velocity
1135 * Set up the transmit ring and chain the ring pointers together.
1136 * Returns zero on success or a negative posix errno code for
1137 * failure.
1140 static int velocity_init_td_ring(struct velocity_info *vptr)
1142 int i, j;
1143 dma_addr_t curr;
1144 struct tx_desc *td;
1145 struct velocity_td_info *td_info;
1146 unsigned int tsize = sizeof(struct velocity_td_info) *
1147 vptr->options.numtx;
1149 /* Init the TD ring entries */
1150 for (j = 0; j < vptr->num_txq; j++) {
1151 curr = vptr->td_pool_dma[j];
1153 vptr->td_infos[j] = kmalloc(tsize, GFP_KERNEL);
1154 if(vptr->td_infos[j] == NULL)
1156 while(--j >= 0)
1157 kfree(vptr->td_infos[j]);
1158 return -ENOMEM;
1160 memset(vptr->td_infos[j], 0, tsize);
1162 for (i = 0; i < vptr->options.numtx; i++, curr += sizeof(struct tx_desc)) {
1163 td = &(vptr->td_rings[j][i]);
1164 td_info = &(vptr->td_infos[j][i]);
1165 td_info->buf = vptr->tx_bufs +
1166 (j * vptr->options.numtx + i) * PKT_BUF_SZ;
1167 td_info->buf_dma = vptr->tx_bufs_dma +
1168 (j * vptr->options.numtx + i) * PKT_BUF_SZ;
1170 vptr->td_tail[j] = vptr->td_curr[j] = vptr->td_used[j] = 0;
1172 return 0;
1176 * FIXME: could we merge this with velocity_free_tx_buf ?
1179 static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1180 int q, int n)
1182 struct velocity_td_info * td_info = &(vptr->td_infos[q][n]);
1183 int i;
1185 if (td_info == NULL)
1186 return;
1188 if (td_info->skb) {
1189 for (i = 0; i < td_info->nskb_dma; i++)
1191 if (td_info->skb_dma[i]) {
1192 pci_unmap_single(vptr->pdev, td_info->skb_dma[i],
1193 td_info->skb->len, PCI_DMA_TODEVICE);
1194 td_info->skb_dma[i] = (dma_addr_t) NULL;
1197 dev_kfree_skb(td_info->skb);
1198 td_info->skb = NULL;
1203 * velocity_free_td_ring - free td ring
1204 * @vptr: velocity
1206 * Free up the transmit ring for this particular velocity adapter.
1207 * We free the ring contents but not the ring itself.
1210 static void velocity_free_td_ring(struct velocity_info *vptr)
1212 int i, j;
1214 for (j = 0; j < vptr->num_txq; j++) {
1215 if (vptr->td_infos[j] == NULL)
1216 continue;
1217 for (i = 0; i < vptr->options.numtx; i++) {
1218 velocity_free_td_ring_entry(vptr, j, i);
1221 kfree(vptr->td_infos[j]);
1222 vptr->td_infos[j] = NULL;
1227 * velocity_rx_srv - service RX interrupt
1228 * @vptr: velocity
1229 * @status: adapter status (unused)
1231 * Walk the receive ring of the velocity adapter and remove
1232 * any received packets from the receive queue. Hand the ring
1233 * slots back to the adapter for reuse.
1236 static int velocity_rx_srv(struct velocity_info *vptr, int status)
1238 struct net_device_stats *stats = &vptr->stats;
1239 int rd_curr = vptr->rd_curr;
1240 int works = 0;
1242 do {
1243 struct rx_desc *rd = vptr->rd_ring + rd_curr;
1245 if (!vptr->rd_info[rd_curr].skb)
1246 break;
1248 if (rd->rdesc0.owner == OWNED_BY_NIC)
1249 break;
1251 rmb();
1254 * Don't drop CE or RL error frame although RXOK is off
1256 if ((rd->rdesc0.RSR & RSR_RXOK) || (!(rd->rdesc0.RSR & RSR_RXOK) && (rd->rdesc0.RSR & (RSR_CE | RSR_RL)))) {
1257 if (velocity_receive_frame(vptr, rd_curr) < 0)
1258 stats->rx_dropped++;
1259 } else {
1260 if (rd->rdesc0.RSR & RSR_CRC)
1261 stats->rx_crc_errors++;
1262 if (rd->rdesc0.RSR & RSR_FAE)
1263 stats->rx_frame_errors++;
1265 stats->rx_dropped++;
1268 rd->inten = 1;
1270 vptr->dev->last_rx = jiffies;
1272 rd_curr++;
1273 if (rd_curr >= vptr->options.numrx)
1274 rd_curr = 0;
1275 } while (++works <= 15);
1277 vptr->rd_curr = rd_curr;
1279 if (works > 0 && velocity_rx_refill(vptr) < 0) {
1280 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1281 "%s: rx buf allocation failure\n", vptr->dev->name);
1284 VAR_USED(stats);
1285 return works;
1289 * velocity_rx_csum - checksum process
1290 * @rd: receive packet descriptor
1291 * @skb: network layer packet buffer
1293 * Process the status bits for the received packet and determine
1294 * if the checksum was computed and verified by the hardware
1297 static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1299 skb->ip_summed = CHECKSUM_NONE;
1301 if (rd->rdesc1.CSM & CSM_IPKT) {
1302 if (rd->rdesc1.CSM & CSM_IPOK) {
1303 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
1304 (rd->rdesc1.CSM & CSM_UDPKT)) {
1305 if (!(rd->rdesc1.CSM & CSM_TUPOK)) {
1306 return;
1309 skb->ip_summed = CHECKSUM_UNNECESSARY;
1315 * velocity_rx_copy - in place Rx copy for small packets
1316 * @rx_skb: network layer packet buffer candidate
1317 * @pkt_size: received data size
1318 * @rd: receive packet descriptor
1319 * @dev: network device
1321 * Replace the current skb that is scheduled for Rx processing by a
1322 * shorter, immediatly allocated skb, if the received packet is small
1323 * enough. This function returns a negative value if the received
1324 * packet is too big or if memory is exhausted.
1326 static inline int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
1327 struct velocity_info *vptr)
1329 int ret = -1;
1331 if (pkt_size < rx_copybreak) {
1332 struct sk_buff *new_skb;
1334 new_skb = dev_alloc_skb(pkt_size + 2);
1335 if (new_skb) {
1336 new_skb->dev = vptr->dev;
1337 new_skb->ip_summed = rx_skb[0]->ip_summed;
1339 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN)
1340 skb_reserve(new_skb, 2);
1342 skb_copy_from_linear_data(rx_skb[0], new_skb->data,
1343 pkt_size);
1344 *rx_skb = new_skb;
1345 ret = 0;
1349 return ret;
1353 * velocity_iph_realign - IP header alignment
1354 * @vptr: velocity we are handling
1355 * @skb: network layer packet buffer
1356 * @pkt_size: received data size
1358 * Align IP header on a 2 bytes boundary. This behavior can be
1359 * configured by the user.
1361 static inline void velocity_iph_realign(struct velocity_info *vptr,
1362 struct sk_buff *skb, int pkt_size)
1364 /* FIXME - memmove ? */
1365 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
1366 int i;
1368 for (i = pkt_size; i >= 0; i--)
1369 *(skb->data + i + 2) = *(skb->data + i);
1370 skb_reserve(skb, 2);
1375 * velocity_receive_frame - received packet processor
1376 * @vptr: velocity we are handling
1377 * @idx: ring index
1379 * A packet has arrived. We process the packet and if appropriate
1380 * pass the frame up the network stack
1383 static int velocity_receive_frame(struct velocity_info *vptr, int idx)
1385 void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int);
1386 struct net_device_stats *stats = &vptr->stats;
1387 struct velocity_rd_info *rd_info = &(vptr->rd_info[idx]);
1388 struct rx_desc *rd = &(vptr->rd_ring[idx]);
1389 int pkt_len = rd->rdesc0.len;
1390 struct sk_buff *skb;
1392 if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
1393 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->dev->name);
1394 stats->rx_length_errors++;
1395 return -EINVAL;
1398 if (rd->rdesc0.RSR & RSR_MAR)
1399 vptr->stats.multicast++;
1401 skb = rd_info->skb;
1403 pci_dma_sync_single_for_cpu(vptr->pdev, rd_info->skb_dma,
1404 vptr->rx_buf_sz, PCI_DMA_FROMDEVICE);
1407 * Drop frame not meeting IEEE 802.3
1410 if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
1411 if (rd->rdesc0.RSR & RSR_RL) {
1412 stats->rx_length_errors++;
1413 return -EINVAL;
1417 pci_action = pci_dma_sync_single_for_device;
1419 velocity_rx_csum(rd, skb);
1421 if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
1422 velocity_iph_realign(vptr, skb, pkt_len);
1423 pci_action = pci_unmap_single;
1424 rd_info->skb = NULL;
1427 pci_action(vptr->pdev, rd_info->skb_dma, vptr->rx_buf_sz,
1428 PCI_DMA_FROMDEVICE);
1430 skb_put(skb, pkt_len - 4);
1431 skb->protocol = eth_type_trans(skb, vptr->dev);
1433 stats->rx_bytes += pkt_len;
1434 netif_rx(skb);
1436 return 0;
1440 * velocity_alloc_rx_buf - allocate aligned receive buffer
1441 * @vptr: velocity
1442 * @idx: ring index
1444 * Allocate a new full sized buffer for the reception of a frame and
1445 * map it into PCI space for the hardware to use. The hardware
1446 * requires *64* byte alignment of the buffer which makes life
1447 * less fun than would be ideal.
1450 static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1452 struct rx_desc *rd = &(vptr->rd_ring[idx]);
1453 struct velocity_rd_info *rd_info = &(vptr->rd_info[idx]);
1455 rd_info->skb = dev_alloc_skb(vptr->rx_buf_sz + 64);
1456 if (rd_info->skb == NULL)
1457 return -ENOMEM;
1460 * Do the gymnastics to get the buffer head for data at
1461 * 64byte alignment.
1463 skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
1464 rd_info->skb->dev = vptr->dev;
1465 rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data, vptr->rx_buf_sz, PCI_DMA_FROMDEVICE);
1468 * Fill in the descriptor to match
1471 *((u32 *) & (rd->rdesc0)) = 0;
1472 rd->len = cpu_to_le32(vptr->rx_buf_sz);
1473 rd->inten = 1;
1474 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1475 rd->pa_high = 0;
1476 return 0;
1480 * tx_srv - transmit interrupt service
1481 * @vptr; Velocity
1482 * @status:
1484 * Scan the queues looking for transmitted packets that
1485 * we can complete and clean up. Update any statistics as
1486 * neccessary/
1489 static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
1491 struct tx_desc *td;
1492 int qnum;
1493 int full = 0;
1494 int idx;
1495 int works = 0;
1496 struct velocity_td_info *tdinfo;
1497 struct net_device_stats *stats = &vptr->stats;
1499 for (qnum = 0; qnum < vptr->num_txq; qnum++) {
1500 for (idx = vptr->td_tail[qnum]; vptr->td_used[qnum] > 0;
1501 idx = (idx + 1) % vptr->options.numtx) {
1504 * Get Tx Descriptor
1506 td = &(vptr->td_rings[qnum][idx]);
1507 tdinfo = &(vptr->td_infos[qnum][idx]);
1509 if (td->tdesc0.owner == OWNED_BY_NIC)
1510 break;
1512 if ((works++ > 15))
1513 break;
1515 if (td->tdesc0.TSR & TSR0_TERR) {
1516 stats->tx_errors++;
1517 stats->tx_dropped++;
1518 if (td->tdesc0.TSR & TSR0_CDH)
1519 stats->tx_heartbeat_errors++;
1520 if (td->tdesc0.TSR & TSR0_CRS)
1521 stats->tx_carrier_errors++;
1522 if (td->tdesc0.TSR & TSR0_ABT)
1523 stats->tx_aborted_errors++;
1524 if (td->tdesc0.TSR & TSR0_OWC)
1525 stats->tx_window_errors++;
1526 } else {
1527 stats->tx_packets++;
1528 stats->tx_bytes += tdinfo->skb->len;
1530 velocity_free_tx_buf(vptr, tdinfo);
1531 vptr->td_used[qnum]--;
1533 vptr->td_tail[qnum] = idx;
1535 if (AVAIL_TD(vptr, qnum) < 1) {
1536 full = 1;
1540 * Look to see if we should kick the transmit network
1541 * layer for more work.
1543 if (netif_queue_stopped(vptr->dev) && (full == 0)
1544 && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1545 netif_wake_queue(vptr->dev);
1547 return works;
1551 * velocity_print_link_status - link status reporting
1552 * @vptr: velocity to report on
1554 * Turn the link status of the velocity card into a kernel log
1555 * description of the new link state, detailing speed and duplex
1556 * status
1559 static void velocity_print_link_status(struct velocity_info *vptr)
1562 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1563 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
1564 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1565 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->dev->name);
1567 if (vptr->mii_status & VELOCITY_SPEED_1000)
1568 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1569 else if (vptr->mii_status & VELOCITY_SPEED_100)
1570 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1571 else
1572 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1574 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1575 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1576 else
1577 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1578 } else {
1579 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
1580 switch (vptr->options.spd_dpx) {
1581 case SPD_DPX_100_HALF:
1582 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1583 break;
1584 case SPD_DPX_100_FULL:
1585 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1586 break;
1587 case SPD_DPX_10_HALF:
1588 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1589 break;
1590 case SPD_DPX_10_FULL:
1591 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1592 break;
1593 default:
1594 break;
1600 * velocity_error - handle error from controller
1601 * @vptr: velocity
1602 * @status: card status
1604 * Process an error report from the hardware and attempt to recover
1605 * the card itself. At the moment we cannot recover from some
1606 * theoretically impossible errors but this could be fixed using
1607 * the pci_device_failed logic to bounce the hardware
1611 static void velocity_error(struct velocity_info *vptr, int status)
1614 if (status & ISR_TXSTLI) {
1615 struct mac_regs __iomem * regs = vptr->mac_regs;
1617 printk(KERN_ERR "TD structure errror TDindex=%hx\n", readw(&regs->TDIdx[0]));
1618 BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
1619 writew(TRDCSR_RUN, &regs->TDCSRClr);
1620 netif_stop_queue(vptr->dev);
1622 /* FIXME: port over the pci_device_failed code and use it
1623 here */
1626 if (status & ISR_SRCI) {
1627 struct mac_regs __iomem * regs = vptr->mac_regs;
1628 int linked;
1630 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1631 vptr->mii_status = check_connection_type(regs);
1634 * If it is a 3119, disable frame bursting in
1635 * halfduplex mode and enable it in fullduplex
1636 * mode
1638 if (vptr->rev_id < REV_ID_VT3216_A0) {
1639 if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
1640 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
1641 else
1642 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
1645 * Only enable CD heart beat counter in 10HD mode
1647 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10)) {
1648 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
1649 } else {
1650 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
1654 * Get link status from PHYSR0
1656 linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
1658 if (linked) {
1659 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1660 netif_carrier_on(vptr->dev);
1661 } else {
1662 vptr->mii_status |= VELOCITY_LINK_FAIL;
1663 netif_carrier_off(vptr->dev);
1666 velocity_print_link_status(vptr);
1667 enable_flow_control_ability(vptr);
1670 * Re-enable auto-polling because SRCI will disable
1671 * auto-polling
1674 enable_mii_autopoll(regs);
1676 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1677 netif_stop_queue(vptr->dev);
1678 else
1679 netif_wake_queue(vptr->dev);
1682 if (status & ISR_MIBFI)
1683 velocity_update_hw_mibs(vptr);
1684 if (status & ISR_LSTEI)
1685 mac_rx_queue_wake(vptr->mac_regs);
1689 * velocity_free_tx_buf - free transmit buffer
1690 * @vptr: velocity
1691 * @tdinfo: buffer
1693 * Release an transmit buffer. If the buffer was preallocated then
1694 * recycle it, if not then unmap the buffer.
1697 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
1699 struct sk_buff *skb = tdinfo->skb;
1700 int i;
1703 * Don't unmap the pre-allocated tx_bufs
1705 if (tdinfo->skb_dma && (tdinfo->skb_dma[0] != tdinfo->buf_dma)) {
1707 for (i = 0; i < tdinfo->nskb_dma; i++) {
1708 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1709 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], td->tdesc1.len, PCI_DMA_TODEVICE);
1710 #else
1711 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], skb->len, PCI_DMA_TODEVICE);
1712 #endif
1713 tdinfo->skb_dma[i] = 0;
1716 dev_kfree_skb_irq(skb);
1717 tdinfo->skb = NULL;
1721 * velocity_open - interface activation callback
1722 * @dev: network layer device to open
1724 * Called when the network layer brings the interface up. Returns
1725 * a negative posix error code on failure, or zero on success.
1727 * All the ring allocation and set up is done on open for this
1728 * adapter to minimise memory usage when inactive
1731 static int velocity_open(struct net_device *dev)
1733 struct velocity_info *vptr = netdev_priv(dev);
1734 int ret;
1736 vptr->rx_buf_sz = (dev->mtu <= 1504 ? PKT_BUF_SZ : dev->mtu + 32);
1738 ret = velocity_init_rings(vptr);
1739 if (ret < 0)
1740 goto out;
1742 ret = velocity_init_rd_ring(vptr);
1743 if (ret < 0)
1744 goto err_free_desc_rings;
1746 ret = velocity_init_td_ring(vptr);
1747 if (ret < 0)
1748 goto err_free_rd_ring;
1750 /* Ensure chip is running */
1751 pci_set_power_state(vptr->pdev, PCI_D0);
1753 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1755 ret = request_irq(vptr->pdev->irq, &velocity_intr, IRQF_SHARED,
1756 dev->name, dev);
1757 if (ret < 0) {
1758 /* Power down the chip */
1759 pci_set_power_state(vptr->pdev, PCI_D3hot);
1760 goto err_free_td_ring;
1763 mac_enable_int(vptr->mac_regs);
1764 netif_start_queue(dev);
1765 vptr->flags |= VELOCITY_FLAGS_OPENED;
1766 out:
1767 return ret;
1769 err_free_td_ring:
1770 velocity_free_td_ring(vptr);
1771 err_free_rd_ring:
1772 velocity_free_rd_ring(vptr);
1773 err_free_desc_rings:
1774 velocity_free_rings(vptr);
1775 goto out;
1779 * velocity_change_mtu - MTU change callback
1780 * @dev: network device
1781 * @new_mtu: desired MTU
1783 * Handle requests from the networking layer for MTU change on
1784 * this interface. It gets called on a change by the network layer.
1785 * Return zero for success or negative posix error code.
1788 static int velocity_change_mtu(struct net_device *dev, int new_mtu)
1790 struct velocity_info *vptr = netdev_priv(dev);
1791 unsigned long flags;
1792 int oldmtu = dev->mtu;
1793 int ret = 0;
1795 if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
1796 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
1797 vptr->dev->name);
1798 return -EINVAL;
1801 if (!netif_running(dev)) {
1802 dev->mtu = new_mtu;
1803 return 0;
1806 if (new_mtu != oldmtu) {
1807 spin_lock_irqsave(&vptr->lock, flags);
1809 netif_stop_queue(dev);
1810 velocity_shutdown(vptr);
1812 velocity_free_td_ring(vptr);
1813 velocity_free_rd_ring(vptr);
1815 dev->mtu = new_mtu;
1816 if (new_mtu > 8192)
1817 vptr->rx_buf_sz = 9 * 1024;
1818 else if (new_mtu > 4096)
1819 vptr->rx_buf_sz = 8192;
1820 else
1821 vptr->rx_buf_sz = 4 * 1024;
1823 ret = velocity_init_rd_ring(vptr);
1824 if (ret < 0)
1825 goto out_unlock;
1827 ret = velocity_init_td_ring(vptr);
1828 if (ret < 0)
1829 goto out_unlock;
1831 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1833 mac_enable_int(vptr->mac_regs);
1834 netif_start_queue(dev);
1835 out_unlock:
1836 spin_unlock_irqrestore(&vptr->lock, flags);
1839 return ret;
1843 * velocity_shutdown - shut down the chip
1844 * @vptr: velocity to deactivate
1846 * Shuts down the internal operations of the velocity and
1847 * disables interrupts, autopolling, transmit and receive
1850 static void velocity_shutdown(struct velocity_info *vptr)
1852 struct mac_regs __iomem * regs = vptr->mac_regs;
1853 mac_disable_int(regs);
1854 writel(CR0_STOP, &regs->CR0Set);
1855 writew(0xFFFF, &regs->TDCSRClr);
1856 writeb(0xFF, &regs->RDCSRClr);
1857 safe_disable_mii_autopoll(regs);
1858 mac_clear_isr(regs);
1862 * velocity_close - close adapter callback
1863 * @dev: network device
1865 * Callback from the network layer when the velocity is being
1866 * deactivated by the network layer
1869 static int velocity_close(struct net_device *dev)
1871 struct velocity_info *vptr = netdev_priv(dev);
1873 netif_stop_queue(dev);
1874 velocity_shutdown(vptr);
1876 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
1877 velocity_get_ip(vptr);
1878 if (dev->irq != 0)
1879 free_irq(dev->irq, dev);
1881 /* Power down the chip */
1882 pci_set_power_state(vptr->pdev, PCI_D3hot);
1884 /* Free the resources */
1885 velocity_free_td_ring(vptr);
1886 velocity_free_rd_ring(vptr);
1887 velocity_free_rings(vptr);
1889 vptr->flags &= (~VELOCITY_FLAGS_OPENED);
1890 return 0;
1894 * velocity_xmit - transmit packet callback
1895 * @skb: buffer to transmit
1896 * @dev: network device
1898 * Called by the networ layer to request a packet is queued to
1899 * the velocity. Returns zero on success.
1902 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
1904 struct velocity_info *vptr = netdev_priv(dev);
1905 int qnum = 0;
1906 struct tx_desc *td_ptr;
1907 struct velocity_td_info *tdinfo;
1908 unsigned long flags;
1909 int index;
1911 int pktlen = skb->len;
1913 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1914 if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
1915 kfree_skb(skb);
1916 return 0;
1918 #endif
1920 spin_lock_irqsave(&vptr->lock, flags);
1922 index = vptr->td_curr[qnum];
1923 td_ptr = &(vptr->td_rings[qnum][index]);
1924 tdinfo = &(vptr->td_infos[qnum][index]);
1926 td_ptr->tdesc1.TCPLS = TCPLS_NORMAL;
1927 td_ptr->tdesc1.TCR = TCR0_TIC;
1928 td_ptr->td_buf[0].queue = 0;
1931 * Pad short frames.
1933 if (pktlen < ETH_ZLEN) {
1934 /* Cannot occur until ZC support */
1935 pktlen = ETH_ZLEN;
1936 skb_copy_from_linear_data(skb, tdinfo->buf, skb->len);
1937 memset(tdinfo->buf + skb->len, 0, ETH_ZLEN - skb->len);
1938 tdinfo->skb = skb;
1939 tdinfo->skb_dma[0] = tdinfo->buf_dma;
1940 td_ptr->tdesc0.pktsize = pktlen;
1941 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1942 td_ptr->td_buf[0].pa_high = 0;
1943 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1944 tdinfo->nskb_dma = 1;
1945 td_ptr->tdesc1.CMDZ = 2;
1946 } else
1947 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1948 if (skb_shinfo(skb)->nr_frags > 0) {
1949 int nfrags = skb_shinfo(skb)->nr_frags;
1950 tdinfo->skb = skb;
1951 if (nfrags > 6) {
1952 skb_copy_from_linear_data(skb, tdinfo->buf, skb->len);
1953 tdinfo->skb_dma[0] = tdinfo->buf_dma;
1954 td_ptr->tdesc0.pktsize =
1955 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1956 td_ptr->td_buf[0].pa_high = 0;
1957 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1958 tdinfo->nskb_dma = 1;
1959 td_ptr->tdesc1.CMDZ = 2;
1960 } else {
1961 int i = 0;
1962 tdinfo->nskb_dma = 0;
1963 tdinfo->skb_dma[i] = pci_map_single(vptr->pdev, skb->data, skb->len - skb->data_len, PCI_DMA_TODEVICE);
1965 td_ptr->tdesc0.pktsize = pktlen;
1967 /* FIXME: support 48bit DMA later */
1968 td_ptr->td_buf[i].pa_low = cpu_to_le32(tdinfo->skb_dma);
1969 td_ptr->td_buf[i].pa_high = 0;
1970 td_ptr->td_buf[i].bufsize = skb->len->skb->data_len;
1972 for (i = 0; i < nfrags; i++) {
1973 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1974 void *addr = ((void *) page_address(frag->page + frag->page_offset));
1976 tdinfo->skb_dma[i + 1] = pci_map_single(vptr->pdev, addr, frag->size, PCI_DMA_TODEVICE);
1978 td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
1979 td_ptr->td_buf[i + 1].pa_high = 0;
1980 td_ptr->td_buf[i + 1].bufsize = frag->size;
1982 tdinfo->nskb_dma = i - 1;
1983 td_ptr->tdesc1.CMDZ = i;
1986 } else
1987 #endif
1990 * Map the linear network buffer into PCI space and
1991 * add it to the transmit ring.
1993 tdinfo->skb = skb;
1994 tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
1995 td_ptr->tdesc0.pktsize = pktlen;
1996 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1997 td_ptr->td_buf[0].pa_high = 0;
1998 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1999 tdinfo->nskb_dma = 1;
2000 td_ptr->tdesc1.CMDZ = 2;
2003 if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
2004 td_ptr->tdesc1.pqinf.VID = (vptr->options.vid & 0xfff);
2005 td_ptr->tdesc1.pqinf.priority = 0;
2006 td_ptr->tdesc1.pqinf.CFI = 0;
2007 td_ptr->tdesc1.TCR |= TCR0_VETAG;
2011 * Handle hardware checksum
2013 if ((vptr->flags & VELOCITY_FLAGS_TX_CSUM)
2014 && (skb->ip_summed == CHECKSUM_PARTIAL)) {
2015 const struct iphdr *ip = ip_hdr(skb);
2016 if (ip->protocol == IPPROTO_TCP)
2017 td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2018 else if (ip->protocol == IPPROTO_UDP)
2019 td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2020 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2024 int prev = index - 1;
2026 if (prev < 0)
2027 prev = vptr->options.numtx - 1;
2028 td_ptr->tdesc0.owner = OWNED_BY_NIC;
2029 vptr->td_used[qnum]++;
2030 vptr->td_curr[qnum] = (index + 1) % vptr->options.numtx;
2032 if (AVAIL_TD(vptr, qnum) < 1)
2033 netif_stop_queue(dev);
2035 td_ptr = &(vptr->td_rings[qnum][prev]);
2036 td_ptr->td_buf[0].queue = 1;
2037 mac_tx_queue_wake(vptr->mac_regs, qnum);
2039 dev->trans_start = jiffies;
2040 spin_unlock_irqrestore(&vptr->lock, flags);
2041 return 0;
2045 * velocity_intr - interrupt callback
2046 * @irq: interrupt number
2047 * @dev_instance: interrupting device
2049 * Called whenever an interrupt is generated by the velocity
2050 * adapter IRQ line. We may not be the source of the interrupt
2051 * and need to identify initially if we are, and if not exit as
2052 * efficiently as possible.
2055 static int velocity_intr(int irq, void *dev_instance)
2057 struct net_device *dev = dev_instance;
2058 struct velocity_info *vptr = netdev_priv(dev);
2059 u32 isr_status;
2060 int max_count = 0;
2063 spin_lock(&vptr->lock);
2064 isr_status = mac_read_isr(vptr->mac_regs);
2066 /* Not us ? */
2067 if (isr_status == 0) {
2068 spin_unlock(&vptr->lock);
2069 return IRQ_NONE;
2072 mac_disable_int(vptr->mac_regs);
2075 * Keep processing the ISR until we have completed
2076 * processing and the isr_status becomes zero
2079 while (isr_status != 0) {
2080 mac_write_isr(vptr->mac_regs, isr_status);
2081 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2082 velocity_error(vptr, isr_status);
2083 if (isr_status & (ISR_PRXI | ISR_PPRXI))
2084 max_count += velocity_rx_srv(vptr, isr_status);
2085 if (isr_status & (ISR_PTXI | ISR_PPTXI))
2086 max_count += velocity_tx_srv(vptr, isr_status);
2087 isr_status = mac_read_isr(vptr->mac_regs);
2088 if (max_count > vptr->options.int_works)
2090 printk(KERN_WARNING "%s: excessive work at interrupt.\n",
2091 dev->name);
2092 max_count = 0;
2095 spin_unlock(&vptr->lock);
2096 mac_enable_int(vptr->mac_regs);
2097 return IRQ_HANDLED;
2103 * velocity_set_multi - filter list change callback
2104 * @dev: network device
2106 * Called by the network layer when the filter lists need to change
2107 * for a velocity adapter. Reload the CAMs with the new address
2108 * filter ruleset.
2111 static void velocity_set_multi(struct net_device *dev)
2113 struct velocity_info *vptr = netdev_priv(dev);
2114 struct mac_regs __iomem * regs = vptr->mac_regs;
2115 u8 rx_mode;
2116 int i;
2117 struct dev_mc_list *mclist;
2119 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2120 writel(0xffffffff, &regs->MARCAM[0]);
2121 writel(0xffffffff, &regs->MARCAM[4]);
2122 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
2123 } else if ((dev->mc_count > vptr->multicast_limit)
2124 || (dev->flags & IFF_ALLMULTI)) {
2125 writel(0xffffffff, &regs->MARCAM[0]);
2126 writel(0xffffffff, &regs->MARCAM[4]);
2127 rx_mode = (RCR_AM | RCR_AB);
2128 } else {
2129 int offset = MCAM_SIZE - vptr->multicast_limit;
2130 mac_get_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
2132 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) {
2133 mac_set_cam(regs, i + offset, mclist->dmi_addr, VELOCITY_MULTICAST_CAM);
2134 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
2137 mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
2138 rx_mode = (RCR_AM | RCR_AB);
2140 if (dev->mtu > 1500)
2141 rx_mode |= RCR_AL;
2143 BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
2148 * velocity_get_status - statistics callback
2149 * @dev: network device
2151 * Callback from the network layer to allow driver statistics
2152 * to be resynchronized with hardware collected state. In the
2153 * case of the velocity we need to pull the MIB counters from
2154 * the hardware into the counters before letting the network
2155 * layer display them.
2158 static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2160 struct velocity_info *vptr = netdev_priv(dev);
2162 /* If the hardware is down, don't touch MII */
2163 if(!netif_running(dev))
2164 return &vptr->stats;
2166 spin_lock_irq(&vptr->lock);
2167 velocity_update_hw_mibs(vptr);
2168 spin_unlock_irq(&vptr->lock);
2170 vptr->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2171 vptr->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2172 vptr->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2174 // unsigned long rx_dropped; /* no space in linux buffers */
2175 vptr->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2176 /* detailed rx_errors: */
2177 // unsigned long rx_length_errors;
2178 // unsigned long rx_over_errors; /* receiver ring buff overflow */
2179 vptr->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2180 // unsigned long rx_frame_errors; /* recv'd frame alignment error */
2181 // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2182 // unsigned long rx_missed_errors; /* receiver missed packet */
2184 /* detailed tx_errors */
2185 // unsigned long tx_fifo_errors;
2187 return &vptr->stats;
2192 * velocity_ioctl - ioctl entry point
2193 * @dev: network device
2194 * @rq: interface request ioctl
2195 * @cmd: command code
2197 * Called when the user issues an ioctl request to the network
2198 * device in question. The velocity interface supports MII.
2201 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2203 struct velocity_info *vptr = netdev_priv(dev);
2204 int ret;
2206 /* If we are asked for information and the device is power
2207 saving then we need to bring the device back up to talk to it */
2209 if (!netif_running(dev))
2210 pci_set_power_state(vptr->pdev, PCI_D0);
2212 switch (cmd) {
2213 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2214 case SIOCGMIIREG: /* Read MII PHY register. */
2215 case SIOCSMIIREG: /* Write to MII PHY register. */
2216 ret = velocity_mii_ioctl(dev, rq, cmd);
2217 break;
2219 default:
2220 ret = -EOPNOTSUPP;
2222 if (!netif_running(dev))
2223 pci_set_power_state(vptr->pdev, PCI_D3hot);
2226 return ret;
2230 * Definition for our device driver. The PCI layer interface
2231 * uses this to handle all our card discover and plugging
2234 static struct pci_driver velocity_driver = {
2235 .name = VELOCITY_NAME,
2236 .id_table = velocity_id_table,
2237 .probe = velocity_found1,
2238 .remove = __devexit_p(velocity_remove1),
2239 #ifdef CONFIG_PM
2240 .suspend = velocity_suspend,
2241 .resume = velocity_resume,
2242 #endif
2246 * velocity_init_module - load time function
2248 * Called when the velocity module is loaded. The PCI driver
2249 * is registered with the PCI layer, and in turn will call
2250 * the probe functions for each velocity adapter installed
2251 * in the system.
2254 static int __init velocity_init_module(void)
2256 int ret;
2258 velocity_register_notifier();
2259 ret = pci_register_driver(&velocity_driver);
2260 if (ret < 0)
2261 velocity_unregister_notifier();
2262 return ret;
2266 * velocity_cleanup - module unload
2268 * When the velocity hardware is unloaded this function is called.
2269 * It will clean up the notifiers and the unregister the PCI
2270 * driver interface for this hardware. This in turn cleans up
2271 * all discovered interfaces before returning from the function
2274 static void __exit velocity_cleanup_module(void)
2276 velocity_unregister_notifier();
2277 pci_unregister_driver(&velocity_driver);
2280 module_init(velocity_init_module);
2281 module_exit(velocity_cleanup_module);
2285 * MII access , media link mode setting functions
2290 * mii_init - set up MII
2291 * @vptr: velocity adapter
2292 * @mii_status: links tatus
2294 * Set up the PHY for the current link state.
2297 static void mii_init(struct velocity_info *vptr, u32 mii_status)
2299 u16 BMCR;
2301 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
2302 case PHYID_CICADA_CS8201:
2304 * Reset to hardware default
2306 MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2308 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2309 * off it in NWay-forced half mode for NWay-forced v.s.
2310 * legacy-forced issue.
2312 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2313 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2314 else
2315 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2317 * Turn on Link/Activity LED enable bit for CIS8201
2319 MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
2320 break;
2321 case PHYID_VT3216_32BIT:
2322 case PHYID_VT3216_64BIT:
2324 * Reset to hardware default
2326 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2328 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2329 * off it in NWay-forced half mode for NWay-forced v.s.
2330 * legacy-forced issue
2332 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2333 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2334 else
2335 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2336 break;
2338 case PHYID_MARVELL_1000:
2339 case PHYID_MARVELL_1000S:
2341 * Assert CRS on Transmit
2343 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
2345 * Reset to hardware default
2347 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2348 break;
2349 default:
2352 velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
2353 if (BMCR & BMCR_ISO) {
2354 BMCR &= ~BMCR_ISO;
2355 velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
2360 * safe_disable_mii_autopoll - autopoll off
2361 * @regs: velocity registers
2363 * Turn off the autopoll and wait for it to disable on the chip
2366 static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs)
2368 u16 ww;
2370 /* turn off MAUTO */
2371 writeb(0, &regs->MIICR);
2372 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2373 udelay(1);
2374 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2375 break;
2380 * enable_mii_autopoll - turn on autopolling
2381 * @regs: velocity registers
2383 * Enable the MII link status autopoll feature on the Velocity
2384 * hardware. Wait for it to enable.
2387 static void enable_mii_autopoll(struct mac_regs __iomem * regs)
2389 int ii;
2391 writeb(0, &(regs->MIICR));
2392 writeb(MIIADR_SWMPL, &regs->MIIADR);
2394 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2395 udelay(1);
2396 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2397 break;
2400 writeb(MIICR_MAUTO, &regs->MIICR);
2402 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2403 udelay(1);
2404 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2405 break;
2411 * velocity_mii_read - read MII data
2412 * @regs: velocity registers
2413 * @index: MII register index
2414 * @data: buffer for received data
2416 * Perform a single read of an MII 16bit register. Returns zero
2417 * on success or -ETIMEDOUT if the PHY did not respond.
2420 static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
2422 u16 ww;
2425 * Disable MIICR_MAUTO, so that mii addr can be set normally
2427 safe_disable_mii_autopoll(regs);
2429 writeb(index, &regs->MIIADR);
2431 BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
2433 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2434 if (!(readb(&regs->MIICR) & MIICR_RCMD))
2435 break;
2438 *data = readw(&regs->MIIDATA);
2440 enable_mii_autopoll(regs);
2441 if (ww == W_MAX_TIMEOUT)
2442 return -ETIMEDOUT;
2443 return 0;
2447 * velocity_mii_write - write MII data
2448 * @regs: velocity registers
2449 * @index: MII register index
2450 * @data: 16bit data for the MII register
2452 * Perform a single write to an MII 16bit register. Returns zero
2453 * on success or -ETIMEDOUT if the PHY did not respond.
2456 static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
2458 u16 ww;
2461 * Disable MIICR_MAUTO, so that mii addr can be set normally
2463 safe_disable_mii_autopoll(regs);
2465 /* MII reg offset */
2466 writeb(mii_addr, &regs->MIIADR);
2467 /* set MII data */
2468 writew(data, &regs->MIIDATA);
2470 /* turn on MIICR_WCMD */
2471 BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
2473 /* W_MAX_TIMEOUT is the timeout period */
2474 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2475 udelay(5);
2476 if (!(readb(&regs->MIICR) & MIICR_WCMD))
2477 break;
2479 enable_mii_autopoll(regs);
2481 if (ww == W_MAX_TIMEOUT)
2482 return -ETIMEDOUT;
2483 return 0;
2487 * velocity_get_opt_media_mode - get media selection
2488 * @vptr: velocity adapter
2490 * Get the media mode stored in EEPROM or module options and load
2491 * mii_status accordingly. The requested link state information
2492 * is also returned.
2495 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
2497 u32 status = 0;
2499 switch (vptr->options.spd_dpx) {
2500 case SPD_DPX_AUTO:
2501 status = VELOCITY_AUTONEG_ENABLE;
2502 break;
2503 case SPD_DPX_100_FULL:
2504 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
2505 break;
2506 case SPD_DPX_10_FULL:
2507 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
2508 break;
2509 case SPD_DPX_100_HALF:
2510 status = VELOCITY_SPEED_100;
2511 break;
2512 case SPD_DPX_10_HALF:
2513 status = VELOCITY_SPEED_10;
2514 break;
2516 vptr->mii_status = status;
2517 return status;
2521 * mii_set_auto_on - autonegotiate on
2522 * @vptr: velocity
2524 * Enable autonegotation on this interface
2527 static void mii_set_auto_on(struct velocity_info *vptr)
2529 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
2530 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
2531 else
2532 MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2537 static void mii_set_auto_off(struct velocity_info * vptr)
2539 MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2544 * set_mii_flow_control - flow control setup
2545 * @vptr: velocity interface
2547 * Set up the flow control on this interface according to
2548 * the supplied user/eeprom options.
2551 static void set_mii_flow_control(struct velocity_info *vptr)
2553 /*Enable or Disable PAUSE in ANAR */
2554 switch (vptr->options.flow_cntl) {
2555 case FLOW_CNTL_TX:
2556 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2557 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2558 break;
2560 case FLOW_CNTL_RX:
2561 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2562 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2563 break;
2565 case FLOW_CNTL_TX_RX:
2566 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2567 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2568 break;
2570 case FLOW_CNTL_DISABLE:
2571 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2572 MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2573 break;
2574 default:
2575 break;
2580 * velocity_set_media_mode - set media mode
2581 * @mii_status: old MII link state
2583 * Check the media link state and configure the flow control
2584 * PHY and also velocity hardware setup accordingly. In particular
2585 * we need to set up CD polling and frame bursting.
2588 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
2590 u32 curr_status;
2591 struct mac_regs __iomem * regs = vptr->mac_regs;
2593 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
2594 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
2596 /* Set mii link status */
2597 set_mii_flow_control(vptr);
2600 Check if new status is consisent with current status
2601 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
2602 || (mii_status==curr_status)) {
2603 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
2604 vptr->mii_status=check_connection_type(vptr->mac_regs);
2605 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
2606 return 0;
2610 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201) {
2611 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
2615 * If connection type is AUTO
2617 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
2618 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
2619 /* clear force MAC mode bit */
2620 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
2621 /* set duplex mode of MAC according to duplex mode of MII */
2622 MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
2623 MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2624 MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
2626 /* enable AUTO-NEGO mode */
2627 mii_set_auto_on(vptr);
2628 } else {
2629 u16 ANAR;
2630 u8 CHIPGCR;
2633 * 1. if it's 3119, disable frame bursting in halfduplex mode
2634 * and enable it in fullduplex mode
2635 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
2636 * 3. only enable CD heart beat counter in 10HD mode
2639 /* set force MAC mode bit */
2640 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
2642 CHIPGCR = readb(&regs->CHIPGCR);
2643 CHIPGCR &= ~CHIPGCR_FCGMII;
2645 if (mii_status & VELOCITY_DUPLEX_FULL) {
2646 CHIPGCR |= CHIPGCR_FCFDX;
2647 writeb(CHIPGCR, &regs->CHIPGCR);
2648 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
2649 if (vptr->rev_id < REV_ID_VT3216_A0)
2650 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
2651 } else {
2652 CHIPGCR &= ~CHIPGCR_FCFDX;
2653 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
2654 writeb(CHIPGCR, &regs->CHIPGCR);
2655 if (vptr->rev_id < REV_ID_VT3216_A0)
2656 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
2659 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2661 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10)) {
2662 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
2663 } else {
2664 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
2666 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
2667 velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
2668 ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
2669 if (mii_status & VELOCITY_SPEED_100) {
2670 if (mii_status & VELOCITY_DUPLEX_FULL)
2671 ANAR |= ANAR_TXFD;
2672 else
2673 ANAR |= ANAR_TX;
2674 } else {
2675 if (mii_status & VELOCITY_DUPLEX_FULL)
2676 ANAR |= ANAR_10FD;
2677 else
2678 ANAR |= ANAR_10;
2680 velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
2681 /* enable AUTO-NEGO mode */
2682 mii_set_auto_on(vptr);
2683 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
2685 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
2686 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
2687 return VELOCITY_LINK_CHANGE;
2691 * mii_check_media_mode - check media state
2692 * @regs: velocity registers
2694 * Check the current MII status and determine the link status
2695 * accordingly
2698 static u32 mii_check_media_mode(struct mac_regs __iomem * regs)
2700 u32 status = 0;
2701 u16 ANAR;
2703 if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
2704 status |= VELOCITY_LINK_FAIL;
2706 if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
2707 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
2708 else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
2709 status |= (VELOCITY_SPEED_1000);
2710 else {
2711 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2712 if (ANAR & ANAR_TXFD)
2713 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
2714 else if (ANAR & ANAR_TX)
2715 status |= VELOCITY_SPEED_100;
2716 else if (ANAR & ANAR_10FD)
2717 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
2718 else
2719 status |= (VELOCITY_SPEED_10);
2722 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2723 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2724 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2725 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2726 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2727 status |= VELOCITY_AUTONEG_ENABLE;
2731 return status;
2734 static u32 check_connection_type(struct mac_regs __iomem * regs)
2736 u32 status = 0;
2737 u8 PHYSR0;
2738 u16 ANAR;
2739 PHYSR0 = readb(&regs->PHYSR0);
2742 if (!(PHYSR0 & PHYSR0_LINKGD))
2743 status|=VELOCITY_LINK_FAIL;
2746 if (PHYSR0 & PHYSR0_FDPX)
2747 status |= VELOCITY_DUPLEX_FULL;
2749 if (PHYSR0 & PHYSR0_SPDG)
2750 status |= VELOCITY_SPEED_1000;
2751 else if (PHYSR0 & PHYSR0_SPD10)
2752 status |= VELOCITY_SPEED_10;
2753 else
2754 status |= VELOCITY_SPEED_100;
2756 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2757 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2758 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2759 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2760 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2761 status |= VELOCITY_AUTONEG_ENABLE;
2765 return status;
2769 * enable_flow_control_ability - flow control
2770 * @vptr: veloity to configure
2772 * Set up flow control according to the flow control options
2773 * determined by the eeprom/configuration.
2776 static void enable_flow_control_ability(struct velocity_info *vptr)
2779 struct mac_regs __iomem * regs = vptr->mac_regs;
2781 switch (vptr->options.flow_cntl) {
2783 case FLOW_CNTL_DEFAULT:
2784 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
2785 writel(CR0_FDXRFCEN, &regs->CR0Set);
2786 else
2787 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2789 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
2790 writel(CR0_FDXTFCEN, &regs->CR0Set);
2791 else
2792 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2793 break;
2795 case FLOW_CNTL_TX:
2796 writel(CR0_FDXTFCEN, &regs->CR0Set);
2797 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2798 break;
2800 case FLOW_CNTL_RX:
2801 writel(CR0_FDXRFCEN, &regs->CR0Set);
2802 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2803 break;
2805 case FLOW_CNTL_TX_RX:
2806 writel(CR0_FDXTFCEN, &regs->CR0Set);
2807 writel(CR0_FDXRFCEN, &regs->CR0Set);
2808 break;
2810 case FLOW_CNTL_DISABLE:
2811 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2812 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2813 break;
2815 default:
2816 break;
2823 * velocity_ethtool_up - pre hook for ethtool
2824 * @dev: network device
2826 * Called before an ethtool operation. We need to make sure the
2827 * chip is out of D3 state before we poke at it.
2830 static int velocity_ethtool_up(struct net_device *dev)
2832 struct velocity_info *vptr = netdev_priv(dev);
2833 if (!netif_running(dev))
2834 pci_set_power_state(vptr->pdev, PCI_D0);
2835 return 0;
2839 * velocity_ethtool_down - post hook for ethtool
2840 * @dev: network device
2842 * Called after an ethtool operation. Restore the chip back to D3
2843 * state if it isn't running.
2846 static void velocity_ethtool_down(struct net_device *dev)
2848 struct velocity_info *vptr = netdev_priv(dev);
2849 if (!netif_running(dev))
2850 pci_set_power_state(vptr->pdev, PCI_D3hot);
2853 static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2855 struct velocity_info *vptr = netdev_priv(dev);
2856 struct mac_regs __iomem * regs = vptr->mac_regs;
2857 u32 status;
2858 status = check_connection_type(vptr->mac_regs);
2860 cmd->supported = SUPPORTED_TP |
2861 SUPPORTED_Autoneg |
2862 SUPPORTED_10baseT_Half |
2863 SUPPORTED_10baseT_Full |
2864 SUPPORTED_100baseT_Half |
2865 SUPPORTED_100baseT_Full |
2866 SUPPORTED_1000baseT_Half |
2867 SUPPORTED_1000baseT_Full;
2868 if (status & VELOCITY_SPEED_1000)
2869 cmd->speed = SPEED_1000;
2870 else if (status & VELOCITY_SPEED_100)
2871 cmd->speed = SPEED_100;
2872 else
2873 cmd->speed = SPEED_10;
2874 cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2875 cmd->port = PORT_TP;
2876 cmd->transceiver = XCVR_INTERNAL;
2877 cmd->phy_address = readb(&regs->MIIADR) & 0x1F;
2879 if (status & VELOCITY_DUPLEX_FULL)
2880 cmd->duplex = DUPLEX_FULL;
2881 else
2882 cmd->duplex = DUPLEX_HALF;
2884 return 0;
2887 static int velocity_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2889 struct velocity_info *vptr = netdev_priv(dev);
2890 u32 curr_status;
2891 u32 new_status = 0;
2892 int ret = 0;
2894 curr_status = check_connection_type(vptr->mac_regs);
2895 curr_status &= (~VELOCITY_LINK_FAIL);
2897 new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
2898 new_status |= ((cmd->speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
2899 new_status |= ((cmd->speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
2900 new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
2902 if ((new_status & VELOCITY_AUTONEG_ENABLE) && (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE)))
2903 ret = -EINVAL;
2904 else
2905 velocity_set_media_mode(vptr, new_status);
2907 return ret;
2910 static u32 velocity_get_link(struct net_device *dev)
2912 struct velocity_info *vptr = netdev_priv(dev);
2913 struct mac_regs __iomem * regs = vptr->mac_regs;
2914 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 1 : 0;
2917 static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2919 struct velocity_info *vptr = netdev_priv(dev);
2920 strcpy(info->driver, VELOCITY_NAME);
2921 strcpy(info->version, VELOCITY_VERSION);
2922 strcpy(info->bus_info, pci_name(vptr->pdev));
2925 static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2927 struct velocity_info *vptr = netdev_priv(dev);
2928 wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
2929 wol->wolopts |= WAKE_MAGIC;
2931 if (vptr->wol_opts & VELOCITY_WOL_PHY)
2932 wol.wolopts|=WAKE_PHY;
2934 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
2935 wol->wolopts |= WAKE_UCAST;
2936 if (vptr->wol_opts & VELOCITY_WOL_ARP)
2937 wol->wolopts |= WAKE_ARP;
2938 memcpy(&wol->sopass, vptr->wol_passwd, 6);
2941 static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2943 struct velocity_info *vptr = netdev_priv(dev);
2945 if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
2946 return -EFAULT;
2947 vptr->wol_opts = VELOCITY_WOL_MAGIC;
2950 if (wol.wolopts & WAKE_PHY) {
2951 vptr->wol_opts|=VELOCITY_WOL_PHY;
2952 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
2956 if (wol->wolopts & WAKE_MAGIC) {
2957 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
2958 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2960 if (wol->wolopts & WAKE_UCAST) {
2961 vptr->wol_opts |= VELOCITY_WOL_UCAST;
2962 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2964 if (wol->wolopts & WAKE_ARP) {
2965 vptr->wol_opts |= VELOCITY_WOL_ARP;
2966 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2968 memcpy(vptr->wol_passwd, wol->sopass, 6);
2969 return 0;
2972 static u32 velocity_get_msglevel(struct net_device *dev)
2974 return msglevel;
2977 static void velocity_set_msglevel(struct net_device *dev, u32 value)
2979 msglevel = value;
2982 static const struct ethtool_ops velocity_ethtool_ops = {
2983 .get_settings = velocity_get_settings,
2984 .set_settings = velocity_set_settings,
2985 .get_drvinfo = velocity_get_drvinfo,
2986 .get_wol = velocity_ethtool_get_wol,
2987 .set_wol = velocity_ethtool_set_wol,
2988 .get_msglevel = velocity_get_msglevel,
2989 .set_msglevel = velocity_set_msglevel,
2990 .get_link = velocity_get_link,
2991 .begin = velocity_ethtool_up,
2992 .complete = velocity_ethtool_down
2996 * velocity_mii_ioctl - MII ioctl handler
2997 * @dev: network device
2998 * @ifr: the ifreq block for the ioctl
2999 * @cmd: the command
3001 * Process MII requests made via ioctl from the network layer. These
3002 * are used by tools like kudzu to interrogate the link state of the
3003 * hardware
3006 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3008 struct velocity_info *vptr = netdev_priv(dev);
3009 struct mac_regs __iomem * regs = vptr->mac_regs;
3010 unsigned long flags;
3011 struct mii_ioctl_data *miidata = if_mii(ifr);
3012 int err;
3014 switch (cmd) {
3015 case SIOCGMIIPHY:
3016 miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
3017 break;
3018 case SIOCGMIIREG:
3019 if (!capable(CAP_NET_ADMIN))
3020 return -EPERM;
3021 if(velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
3022 return -ETIMEDOUT;
3023 break;
3024 case SIOCSMIIREG:
3025 if (!capable(CAP_NET_ADMIN))
3026 return -EPERM;
3027 spin_lock_irqsave(&vptr->lock, flags);
3028 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
3029 spin_unlock_irqrestore(&vptr->lock, flags);
3030 check_connection_type(vptr->mac_regs);
3031 if(err)
3032 return err;
3033 break;
3034 default:
3035 return -EOPNOTSUPP;
3037 return 0;
3040 #ifdef CONFIG_PM
3043 * velocity_save_context - save registers
3044 * @vptr: velocity
3045 * @context: buffer for stored context
3047 * Retrieve the current configuration from the velocity hardware
3048 * and stash it in the context structure, for use by the context
3049 * restore functions. This allows us to save things we need across
3050 * power down states
3053 static void velocity_save_context(struct velocity_info *vptr, struct velocity_context * context)
3055 struct mac_regs __iomem * regs = vptr->mac_regs;
3056 u16 i;
3057 u8 __iomem *ptr = (u8 __iomem *)regs;
3059 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3060 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3062 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3063 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3065 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3066 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3071 * velocity_restore_context - restore registers
3072 * @vptr: velocity
3073 * @context: buffer for stored context
3075 * Reload the register configuration from the velocity context
3076 * created by velocity_save_context.
3079 static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3081 struct mac_regs __iomem * regs = vptr->mac_regs;
3082 int i;
3083 u8 __iomem *ptr = (u8 __iomem *)regs;
3085 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4) {
3086 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3089 /* Just skip cr0 */
3090 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3091 /* Clear */
3092 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3093 /* Set */
3094 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3097 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4) {
3098 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3101 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4) {
3102 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3105 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++) {
3106 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3112 * wol_calc_crc - WOL CRC
3113 * @pattern: data pattern
3114 * @mask_pattern: mask
3116 * Compute the wake on lan crc hashes for the packet header
3117 * we are interested in.
3120 static u16 wol_calc_crc(int size, u8 * pattern, u8 *mask_pattern)
3122 u16 crc = 0xFFFF;
3123 u8 mask;
3124 int i, j;
3126 for (i = 0; i < size; i++) {
3127 mask = mask_pattern[i];
3129 /* Skip this loop if the mask equals to zero */
3130 if (mask == 0x00)
3131 continue;
3133 for (j = 0; j < 8; j++) {
3134 if ((mask & 0x01) == 0) {
3135 mask >>= 1;
3136 continue;
3138 mask >>= 1;
3139 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
3142 /* Finally, invert the result once to get the correct data */
3143 crc = ~crc;
3144 return bitrev32(crc) >> 16;
3148 * velocity_set_wol - set up for wake on lan
3149 * @vptr: velocity to set WOL status on
3151 * Set a card up for wake on lan either by unicast or by
3152 * ARP packet.
3154 * FIXME: check static buffer is safe here
3157 static int velocity_set_wol(struct velocity_info *vptr)
3159 struct mac_regs __iomem * regs = vptr->mac_regs;
3160 static u8 buf[256];
3161 int i;
3163 static u32 mask_pattern[2][4] = {
3164 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3165 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3168 writew(0xFFFF, &regs->WOLCRClr);
3169 writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
3170 writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
3173 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3174 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
3177 if (vptr->wol_opts & VELOCITY_WOL_UCAST) {
3178 writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
3181 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3182 struct arp_packet *arp = (struct arp_packet *) buf;
3183 u16 crc;
3184 memset(buf, 0, sizeof(struct arp_packet) + 7);
3186 for (i = 0; i < 4; i++)
3187 writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
3189 arp->type = htons(ETH_P_ARP);
3190 arp->ar_op = htons(1);
3192 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3194 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3195 (u8 *) & mask_pattern[0][0]);
3197 writew(crc, &regs->PatternCRC[0]);
3198 writew(WOLCR_ARP_EN, &regs->WOLCRSet);
3201 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
3202 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
3204 writew(0x0FFF, &regs->WOLSRClr);
3206 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3207 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3208 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
3210 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
3213 if (vptr->mii_status & VELOCITY_SPEED_1000)
3214 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
3216 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
3219 u8 GCR;
3220 GCR = readb(&regs->CHIPGCR);
3221 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3222 writeb(GCR, &regs->CHIPGCR);
3225 BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
3226 /* Turn on SWPTAG just before entering power mode */
3227 BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
3228 /* Go to bed ..... */
3229 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
3231 return 0;
3234 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
3236 struct net_device *dev = pci_get_drvdata(pdev);
3237 struct velocity_info *vptr = netdev_priv(dev);
3238 unsigned long flags;
3240 if(!netif_running(vptr->dev))
3241 return 0;
3243 netif_device_detach(vptr->dev);
3245 spin_lock_irqsave(&vptr->lock, flags);
3246 pci_save_state(pdev);
3247 #ifdef ETHTOOL_GWOL
3248 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3249 velocity_get_ip(vptr);
3250 velocity_save_context(vptr, &vptr->context);
3251 velocity_shutdown(vptr);
3252 velocity_set_wol(vptr);
3253 pci_enable_wake(pdev, 3, 1);
3254 pci_set_power_state(pdev, PCI_D3hot);
3255 } else {
3256 velocity_save_context(vptr, &vptr->context);
3257 velocity_shutdown(vptr);
3258 pci_disable_device(pdev);
3259 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3261 #else
3262 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3263 #endif
3264 spin_unlock_irqrestore(&vptr->lock, flags);
3265 return 0;
3268 static int velocity_resume(struct pci_dev *pdev)
3270 struct net_device *dev = pci_get_drvdata(pdev);
3271 struct velocity_info *vptr = netdev_priv(dev);
3272 unsigned long flags;
3273 int i;
3275 if(!netif_running(vptr->dev))
3276 return 0;
3278 pci_set_power_state(pdev, PCI_D0);
3279 pci_enable_wake(pdev, 0, 0);
3280 pci_restore_state(pdev);
3282 mac_wol_reset(vptr->mac_regs);
3284 spin_lock_irqsave(&vptr->lock, flags);
3285 velocity_restore_context(vptr, &vptr->context);
3286 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3287 mac_disable_int(vptr->mac_regs);
3289 velocity_tx_srv(vptr, 0);
3291 for (i = 0; i < vptr->num_txq; i++) {
3292 if (vptr->td_used[i]) {
3293 mac_tx_queue_wake(vptr->mac_regs, i);
3297 mac_enable_int(vptr->mac_regs);
3298 spin_unlock_irqrestore(&vptr->lock, flags);
3299 netif_device_attach(vptr->dev);
3301 return 0;
3304 #ifdef CONFIG_INET
3306 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3308 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3310 if (ifa) {
3311 struct net_device *dev = ifa->ifa_dev->dev;
3312 struct velocity_info *vptr;
3313 unsigned long flags;
3315 spin_lock_irqsave(&velocity_dev_list_lock, flags);
3316 list_for_each_entry(vptr, &velocity_dev_list, list) {
3317 if (vptr->dev == dev) {
3318 velocity_get_ip(vptr);
3319 break;
3322 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
3324 return NOTIFY_DONE;
3327 #endif
3328 #endif