2 * This code is derived from the VIA reference driver (copyright message
3 * below) provided to Red Hat by VIA Networking Technologies, Inc. for
4 * addition to the Linux kernel.
6 * The code has been merged into one source file, cleaned up to follow
7 * Linux coding style, ported to the Linux 2.6 kernel tree and cleaned
8 * for 64bit hardware platforms.
12 * rx_copybreak/alignment
16 * The changes are (c) Copyright 2004, Red Hat Inc. <alan@redhat.com>
17 * Additional fixes and clean up: Francois Romieu
19 * This source has not been verified for use in safety critical systems.
21 * Please direct queries about the revamped driver to the linux-kernel
26 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
27 * All rights reserved.
29 * This software may be redistributed and/or modified under
30 * the terms of the GNU General Public License as published by the Free
31 * Software Foundation; either version 2 of the License, or
34 * This program is distributed in the hope that it will be useful, but
35 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
36 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
39 * Author: Chuang Liang-Shing, AJ Jiang
43 * MODULE_LICENSE("GPL");
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/init.h>
52 #include <linux/errno.h>
53 #include <linux/ioport.h>
54 #include <linux/pci.h>
55 #include <linux/kernel.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58 #include <linux/skbuff.h>
59 #include <linux/delay.h>
60 #include <linux/timer.h>
61 #include <linux/slab.h>
62 #include <linux/interrupt.h>
63 #include <linux/string.h>
64 #include <linux/wait.h>
67 #include <asm/uaccess.h>
68 #include <linux/proc_fs.h>
69 #include <linux/inetdevice.h>
70 #include <linux/reboot.h>
71 #include <linux/ethtool.h>
72 #include <linux/mii.h>
74 #include <linux/if_arp.h>
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;
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.
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
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.
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
198 if EEPROM have been set to the force mode, this option is ignored
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);
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);
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);
293 #define velocity_register_notifier() do {} while (0)
294 #define velocity_unregister_notifier() do {} while (0)
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)
330 for (i = 0; chip_info_table[i].name != NULL; i++)
331 if (chip_info_table[i].chip_id == chip_id)
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);
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);
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);
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
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)
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);
392 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
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
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)
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",
420 *opt |= (def ? flag : 0);
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, ®s->MCFG);
471 WORD_REG_BITS_ON(MCFG_VIDFR, ®s->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, ®s->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);
491 mac_set_cam(regs, 0, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
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;
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, ®s->RBRDU);
520 writel(vptr->rd_pool_dma, ®s->RDBaseLo);
521 writew(0, ®s->RDIdx);
522 writew(vptr->options.numrx - 1, ®s->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
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;
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);
565 writel(CR0_STOP, ®s->CR0Clr);
566 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
571 case VELOCITY_INIT_COLD:
576 velocity_soft_reset(vptr);
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, ®s->WOLCFGSet);
592 * Back off algorithm use original IEEE standard
594 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), ®s->CFGB);
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), ®s->RDBaseLo);
614 writew(vptr->options.numrx - 1, ®s->RDCSize);
615 mac_rx_queue_run(regs);
616 mac_rx_queue_wake(regs);
618 writew(vptr->options.numtx - 1, ®s->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, ®s->CR0Clr);
628 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), ®s->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);
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;
662 writel(CR0_SFRST, ®s->CR0Set);
664 for (i = 0; i < W_MAX_TIMEOUT; i++) {
666 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, ®s->CR0Set))
670 if (i == W_MAX_TIMEOUT) {
671 writel(CR0_FORSRST, ®s->CR0Set);
672 /* FIXME: PCI POSTING */
680 * velocity_found1 - set up discovered velocity card
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;
693 const struct velocity_info_tbl *info = &chip_info_table[ent->driver_data];
694 struct velocity_info *vptr;
695 struct mac_regs __iomem * regs;
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",
707 dev = alloc_etherdev(sizeof(struct velocity_info));
709 dev_err(&pdev->dev, "allocate net device failed.\n");
713 /* Chain it all together */
715 SET_MODULE_OWNER(dev);
716 SET_NETDEV_DEV(dev, &pdev->dev);
717 vptr = netdev_priv(dev);
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");
728 velocity_init_info(pdev, vptr, info);
732 dev->irq = pdev->irq;
734 ret = pci_enable_device(pdev);
738 ret = velocity_get_pci_info(vptr, pdev);
740 /* error message already printed */
744 ret = pci_request_regions(pdev, VELOCITY_NAME);
746 dev_err(&pdev->dev, "No PCI resources.\n");
750 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
753 goto err_release_res;
756 vptr->mac_regs = regs;
760 dev->base_addr = vptr->ioaddr;
762 for (i = 0; i < 6; i++)
763 dev->dev_addr[i] = readb(®s->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;
798 if (vptr->flags & VELOCITY_FLAGS_TX_CSUM) {
799 dev->features |= NETIF_F_IP_CSUM;
802 ret = register_netdev(dev);
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);
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);
831 pci_release_regions(pdev);
833 pci_disable_device(pdev);
840 * velocity_print_info - per driver data
843 * Print per driver data as the kernel driver finds Velocity
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",
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
861 * @vptr: Velocity info
864 * Set up the initial velocity_info struct for the device that has been
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));
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 vptr->rev_id = pdev->revision;
895 pci_set_master(pdev);
897 vptr->ioaddr = pci_resource_start(pdev, 0);
898 vptr->memaddr = pci_resource_start(pdev, 1);
900 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
902 "region #0 is not an I/O resource, aborting.\n");
906 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
908 "region #1 is an I/O resource, aborting.\n");
912 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
913 dev_err(&pdev->dev, "region #1 is too small.\n");
922 * velocity_init_rings - set up DMA rings
923 * @vptr: Velocity to set up
925 * Allocate PCI mapped DMA rings for the receive and transmit layer
929 static int velocity_init_rings(struct velocity_info *vptr)
938 * Allocate all RD/TD rings a single pool
941 psize = vptr->options.numrx * sizeof(struct rx_desc) +
942 vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
945 * pci_alloc_consistent() fulfills the requirement for 64 bytes
948 pool = pci_alloc_consistent(vptr->pdev, psize, &pool_dma);
951 printk(KERN_ERR "%s : DMA memory allocation failed.\n",
956 memset(pool, 0, psize);
958 vptr->rd_ring = (struct rx_desc *) pool;
960 vptr->rd_pool_dma = pool_dma;
962 tsize = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
963 vptr->tx_bufs = pci_alloc_consistent(vptr->pdev, tsize,
966 if (vptr->tx_bufs == NULL) {
967 printk(KERN_ERR "%s: DMA memory allocation failed.\n",
969 pci_free_consistent(vptr->pdev, psize, pool, pool_dma);
973 memset(vptr->tx_bufs, 0, vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq);
975 i = vptr->options.numrx * sizeof(struct rx_desc);
978 for (i = 0; i < vptr->num_txq; i++) {
979 int offset = vptr->options.numtx * sizeof(struct tx_desc);
981 vptr->td_pool_dma[i] = pool_dma;
982 vptr->td_rings[i] = (struct tx_desc *) pool;
990 * velocity_free_rings - free PCI ring pointers
991 * @vptr: Velocity to free from
993 * Clean up the PCI ring buffers allocated to this velocity.
996 static void velocity_free_rings(struct velocity_info *vptr)
1000 size = vptr->options.numrx * sizeof(struct rx_desc) +
1001 vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
1003 pci_free_consistent(vptr->pdev, size, vptr->rd_ring, vptr->rd_pool_dma);
1005 size = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
1007 pci_free_consistent(vptr->pdev, size, vptr->tx_bufs, vptr->tx_bufs_dma);
1010 static inline void velocity_give_many_rx_descs(struct velocity_info *vptr)
1012 struct mac_regs __iomem *regs = vptr->mac_regs;
1013 int avail, dirty, unusable;
1016 * RD number must be equal to 4X per hardware spec
1017 * (programming guide rev 1.20, p.13)
1019 if (vptr->rd_filled < 4)
1024 unusable = vptr->rd_filled & 0x0003;
1025 dirty = vptr->rd_dirty - unusable;
1026 for (avail = vptr->rd_filled & 0xfffc; avail; avail--) {
1027 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1028 vptr->rd_ring[dirty].rdesc0.owner = OWNED_BY_NIC;
1031 writew(vptr->rd_filled & 0xfffc, ®s->RBRDU);
1032 vptr->rd_filled = unusable;
1035 static int velocity_rx_refill(struct velocity_info *vptr)
1037 int dirty = vptr->rd_dirty, done = 0, ret = 0;
1040 struct rx_desc *rd = vptr->rd_ring + dirty;
1042 /* Fine for an all zero Rx desc at init time as well */
1043 if (rd->rdesc0.owner == OWNED_BY_NIC)
1046 if (!vptr->rd_info[dirty].skb) {
1047 ret = velocity_alloc_rx_buf(vptr, dirty);
1052 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
1053 } while (dirty != vptr->rd_curr);
1056 vptr->rd_dirty = dirty;
1057 vptr->rd_filled += done;
1058 velocity_give_many_rx_descs(vptr);
1065 * velocity_init_rd_ring - set up receive ring
1066 * @vptr: velocity to configure
1068 * Allocate and set up the receive buffers for each ring slot and
1069 * assign them to the network adapter.
1072 static int velocity_init_rd_ring(struct velocity_info *vptr)
1076 vptr->rd_info = kcalloc(vptr->options.numrx,
1077 sizeof(struct velocity_rd_info), GFP_KERNEL);
1081 vptr->rd_filled = vptr->rd_dirty = vptr->rd_curr = 0;
1083 ret = velocity_rx_refill(vptr);
1085 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1086 "%s: failed to allocate RX buffer.\n", vptr->dev->name);
1087 velocity_free_rd_ring(vptr);
1094 * velocity_free_rd_ring - free receive ring
1095 * @vptr: velocity to clean up
1097 * Free the receive buffers for each ring slot and any
1098 * attached socket buffers that need to go away.
1101 static void velocity_free_rd_ring(struct velocity_info *vptr)
1105 if (vptr->rd_info == NULL)
1108 for (i = 0; i < vptr->options.numrx; i++) {
1109 struct velocity_rd_info *rd_info = &(vptr->rd_info[i]);
1110 struct rx_desc *rd = vptr->rd_ring + i;
1112 memset(rd, 0, sizeof(*rd));
1116 pci_unmap_single(vptr->pdev, rd_info->skb_dma, vptr->rx_buf_sz,
1117 PCI_DMA_FROMDEVICE);
1118 rd_info->skb_dma = (dma_addr_t) NULL;
1120 dev_kfree_skb(rd_info->skb);
1121 rd_info->skb = NULL;
1124 kfree(vptr->rd_info);
1125 vptr->rd_info = NULL;
1129 * velocity_init_td_ring - set up transmit ring
1132 * Set up the transmit ring and chain the ring pointers together.
1133 * Returns zero on success or a negative posix errno code for
1137 static int velocity_init_td_ring(struct velocity_info *vptr)
1142 struct velocity_td_info *td_info;
1144 /* Init the TD ring entries */
1145 for (j = 0; j < vptr->num_txq; j++) {
1146 curr = vptr->td_pool_dma[j];
1148 vptr->td_infos[j] = kcalloc(vptr->options.numtx,
1149 sizeof(struct velocity_td_info),
1151 if (!vptr->td_infos[j]) {
1153 kfree(vptr->td_infos[j]);
1157 for (i = 0; i < vptr->options.numtx; i++, curr += sizeof(struct tx_desc)) {
1158 td = &(vptr->td_rings[j][i]);
1159 td_info = &(vptr->td_infos[j][i]);
1160 td_info->buf = vptr->tx_bufs +
1161 (j * vptr->options.numtx + i) * PKT_BUF_SZ;
1162 td_info->buf_dma = vptr->tx_bufs_dma +
1163 (j * vptr->options.numtx + i) * PKT_BUF_SZ;
1165 vptr->td_tail[j] = vptr->td_curr[j] = vptr->td_used[j] = 0;
1171 * FIXME: could we merge this with velocity_free_tx_buf ?
1174 static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1177 struct velocity_td_info * td_info = &(vptr->td_infos[q][n]);
1180 if (td_info == NULL)
1184 for (i = 0; i < td_info->nskb_dma; i++)
1186 if (td_info->skb_dma[i]) {
1187 pci_unmap_single(vptr->pdev, td_info->skb_dma[i],
1188 td_info->skb->len, PCI_DMA_TODEVICE);
1189 td_info->skb_dma[i] = (dma_addr_t) NULL;
1192 dev_kfree_skb(td_info->skb);
1193 td_info->skb = NULL;
1198 * velocity_free_td_ring - free td ring
1201 * Free up the transmit ring for this particular velocity adapter.
1202 * We free the ring contents but not the ring itself.
1205 static void velocity_free_td_ring(struct velocity_info *vptr)
1209 for (j = 0; j < vptr->num_txq; j++) {
1210 if (vptr->td_infos[j] == NULL)
1212 for (i = 0; i < vptr->options.numtx; i++) {
1213 velocity_free_td_ring_entry(vptr, j, i);
1216 kfree(vptr->td_infos[j]);
1217 vptr->td_infos[j] = NULL;
1222 * velocity_rx_srv - service RX interrupt
1224 * @status: adapter status (unused)
1226 * Walk the receive ring of the velocity adapter and remove
1227 * any received packets from the receive queue. Hand the ring
1228 * slots back to the adapter for reuse.
1231 static int velocity_rx_srv(struct velocity_info *vptr, int status)
1233 struct net_device_stats *stats = &vptr->stats;
1234 int rd_curr = vptr->rd_curr;
1238 struct rx_desc *rd = vptr->rd_ring + rd_curr;
1240 if (!vptr->rd_info[rd_curr].skb)
1243 if (rd->rdesc0.owner == OWNED_BY_NIC)
1249 * Don't drop CE or RL error frame although RXOK is off
1251 if ((rd->rdesc0.RSR & RSR_RXOK) || (!(rd->rdesc0.RSR & RSR_RXOK) && (rd->rdesc0.RSR & (RSR_CE | RSR_RL)))) {
1252 if (velocity_receive_frame(vptr, rd_curr) < 0)
1253 stats->rx_dropped++;
1255 if (rd->rdesc0.RSR & RSR_CRC)
1256 stats->rx_crc_errors++;
1257 if (rd->rdesc0.RSR & RSR_FAE)
1258 stats->rx_frame_errors++;
1260 stats->rx_dropped++;
1265 vptr->dev->last_rx = jiffies;
1268 if (rd_curr >= vptr->options.numrx)
1270 } while (++works <= 15);
1272 vptr->rd_curr = rd_curr;
1274 if (works > 0 && velocity_rx_refill(vptr) < 0) {
1275 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1276 "%s: rx buf allocation failure\n", vptr->dev->name);
1284 * velocity_rx_csum - checksum process
1285 * @rd: receive packet descriptor
1286 * @skb: network layer packet buffer
1288 * Process the status bits for the received packet and determine
1289 * if the checksum was computed and verified by the hardware
1292 static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1294 skb->ip_summed = CHECKSUM_NONE;
1296 if (rd->rdesc1.CSM & CSM_IPKT) {
1297 if (rd->rdesc1.CSM & CSM_IPOK) {
1298 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
1299 (rd->rdesc1.CSM & CSM_UDPKT)) {
1300 if (!(rd->rdesc1.CSM & CSM_TUPOK)) {
1304 skb->ip_summed = CHECKSUM_UNNECESSARY;
1310 * velocity_rx_copy - in place Rx copy for small packets
1311 * @rx_skb: network layer packet buffer candidate
1312 * @pkt_size: received data size
1313 * @rd: receive packet descriptor
1314 * @dev: network device
1316 * Replace the current skb that is scheduled for Rx processing by a
1317 * shorter, immediatly allocated skb, if the received packet is small
1318 * enough. This function returns a negative value if the received
1319 * packet is too big or if memory is exhausted.
1321 static inline int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
1322 struct velocity_info *vptr)
1326 if (pkt_size < rx_copybreak) {
1327 struct sk_buff *new_skb;
1329 new_skb = dev_alloc_skb(pkt_size + 2);
1331 new_skb->dev = vptr->dev;
1332 new_skb->ip_summed = rx_skb[0]->ip_summed;
1334 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN)
1335 skb_reserve(new_skb, 2);
1337 skb_copy_from_linear_data(rx_skb[0], new_skb->data,
1348 * velocity_iph_realign - IP header alignment
1349 * @vptr: velocity we are handling
1350 * @skb: network layer packet buffer
1351 * @pkt_size: received data size
1353 * Align IP header on a 2 bytes boundary. This behavior can be
1354 * configured by the user.
1356 static inline void velocity_iph_realign(struct velocity_info *vptr,
1357 struct sk_buff *skb, int pkt_size)
1359 /* FIXME - memmove ? */
1360 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
1363 for (i = pkt_size; i >= 0; i--)
1364 *(skb->data + i + 2) = *(skb->data + i);
1365 skb_reserve(skb, 2);
1370 * velocity_receive_frame - received packet processor
1371 * @vptr: velocity we are handling
1374 * A packet has arrived. We process the packet and if appropriate
1375 * pass the frame up the network stack
1378 static int velocity_receive_frame(struct velocity_info *vptr, int idx)
1380 void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int);
1381 struct net_device_stats *stats = &vptr->stats;
1382 struct velocity_rd_info *rd_info = &(vptr->rd_info[idx]);
1383 struct rx_desc *rd = &(vptr->rd_ring[idx]);
1384 int pkt_len = rd->rdesc0.len;
1385 struct sk_buff *skb;
1387 if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
1388 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->dev->name);
1389 stats->rx_length_errors++;
1393 if (rd->rdesc0.RSR & RSR_MAR)
1394 vptr->stats.multicast++;
1398 pci_dma_sync_single_for_cpu(vptr->pdev, rd_info->skb_dma,
1399 vptr->rx_buf_sz, PCI_DMA_FROMDEVICE);
1402 * Drop frame not meeting IEEE 802.3
1405 if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
1406 if (rd->rdesc0.RSR & RSR_RL) {
1407 stats->rx_length_errors++;
1412 pci_action = pci_dma_sync_single_for_device;
1414 velocity_rx_csum(rd, skb);
1416 if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
1417 velocity_iph_realign(vptr, skb, pkt_len);
1418 pci_action = pci_unmap_single;
1419 rd_info->skb = NULL;
1422 pci_action(vptr->pdev, rd_info->skb_dma, vptr->rx_buf_sz,
1423 PCI_DMA_FROMDEVICE);
1425 skb_put(skb, pkt_len - 4);
1426 skb->protocol = eth_type_trans(skb, vptr->dev);
1428 stats->rx_bytes += pkt_len;
1435 * velocity_alloc_rx_buf - allocate aligned receive buffer
1439 * Allocate a new full sized buffer for the reception of a frame and
1440 * map it into PCI space for the hardware to use. The hardware
1441 * requires *64* byte alignment of the buffer which makes life
1442 * less fun than would be ideal.
1445 static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1447 struct rx_desc *rd = &(vptr->rd_ring[idx]);
1448 struct velocity_rd_info *rd_info = &(vptr->rd_info[idx]);
1450 rd_info->skb = dev_alloc_skb(vptr->rx_buf_sz + 64);
1451 if (rd_info->skb == NULL)
1455 * Do the gymnastics to get the buffer head for data at
1458 skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
1459 rd_info->skb->dev = vptr->dev;
1460 rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data, vptr->rx_buf_sz, PCI_DMA_FROMDEVICE);
1463 * Fill in the descriptor to match
1466 *((u32 *) & (rd->rdesc0)) = 0;
1467 rd->len = cpu_to_le32(vptr->rx_buf_sz);
1469 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1475 * tx_srv - transmit interrupt service
1479 * Scan the queues looking for transmitted packets that
1480 * we can complete and clean up. Update any statistics as
1484 static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
1491 struct velocity_td_info *tdinfo;
1492 struct net_device_stats *stats = &vptr->stats;
1494 for (qnum = 0; qnum < vptr->num_txq; qnum++) {
1495 for (idx = vptr->td_tail[qnum]; vptr->td_used[qnum] > 0;
1496 idx = (idx + 1) % vptr->options.numtx) {
1501 td = &(vptr->td_rings[qnum][idx]);
1502 tdinfo = &(vptr->td_infos[qnum][idx]);
1504 if (td->tdesc0.owner == OWNED_BY_NIC)
1510 if (td->tdesc0.TSR & TSR0_TERR) {
1512 stats->tx_dropped++;
1513 if (td->tdesc0.TSR & TSR0_CDH)
1514 stats->tx_heartbeat_errors++;
1515 if (td->tdesc0.TSR & TSR0_CRS)
1516 stats->tx_carrier_errors++;
1517 if (td->tdesc0.TSR & TSR0_ABT)
1518 stats->tx_aborted_errors++;
1519 if (td->tdesc0.TSR & TSR0_OWC)
1520 stats->tx_window_errors++;
1522 stats->tx_packets++;
1523 stats->tx_bytes += tdinfo->skb->len;
1525 velocity_free_tx_buf(vptr, tdinfo);
1526 vptr->td_used[qnum]--;
1528 vptr->td_tail[qnum] = idx;
1530 if (AVAIL_TD(vptr, qnum) < 1) {
1535 * Look to see if we should kick the transmit network
1536 * layer for more work.
1538 if (netif_queue_stopped(vptr->dev) && (full == 0)
1539 && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1540 netif_wake_queue(vptr->dev);
1546 * velocity_print_link_status - link status reporting
1547 * @vptr: velocity to report on
1549 * Turn the link status of the velocity card into a kernel log
1550 * description of the new link state, detailing speed and duplex
1554 static void velocity_print_link_status(struct velocity_info *vptr)
1557 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1558 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
1559 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1560 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->dev->name);
1562 if (vptr->mii_status & VELOCITY_SPEED_1000)
1563 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1564 else if (vptr->mii_status & VELOCITY_SPEED_100)
1565 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1567 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1569 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1570 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1572 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1574 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
1575 switch (vptr->options.spd_dpx) {
1576 case SPD_DPX_100_HALF:
1577 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1579 case SPD_DPX_100_FULL:
1580 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1582 case SPD_DPX_10_HALF:
1583 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1585 case SPD_DPX_10_FULL:
1586 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1595 * velocity_error - handle error from controller
1597 * @status: card status
1599 * Process an error report from the hardware and attempt to recover
1600 * the card itself. At the moment we cannot recover from some
1601 * theoretically impossible errors but this could be fixed using
1602 * the pci_device_failed logic to bounce the hardware
1606 static void velocity_error(struct velocity_info *vptr, int status)
1609 if (status & ISR_TXSTLI) {
1610 struct mac_regs __iomem * regs = vptr->mac_regs;
1612 printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(®s->TDIdx[0]));
1613 BYTE_REG_BITS_ON(TXESR_TDSTR, ®s->TXESR);
1614 writew(TRDCSR_RUN, ®s->TDCSRClr);
1615 netif_stop_queue(vptr->dev);
1617 /* FIXME: port over the pci_device_failed code and use it
1621 if (status & ISR_SRCI) {
1622 struct mac_regs __iomem * regs = vptr->mac_regs;
1625 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1626 vptr->mii_status = check_connection_type(regs);
1629 * If it is a 3119, disable frame bursting in
1630 * halfduplex mode and enable it in fullduplex
1633 if (vptr->rev_id < REV_ID_VT3216_A0) {
1634 if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
1635 BYTE_REG_BITS_ON(TCR_TB2BDIS, ®s->TCR);
1637 BYTE_REG_BITS_OFF(TCR_TB2BDIS, ®s->TCR);
1640 * Only enable CD heart beat counter in 10HD mode
1642 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10)) {
1643 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, ®s->TESTCFG);
1645 BYTE_REG_BITS_ON(TESTCFG_HBDIS, ®s->TESTCFG);
1649 * Get link status from PHYSR0
1651 linked = readb(®s->PHYSR0) & PHYSR0_LINKGD;
1654 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1655 netif_carrier_on(vptr->dev);
1657 vptr->mii_status |= VELOCITY_LINK_FAIL;
1658 netif_carrier_off(vptr->dev);
1661 velocity_print_link_status(vptr);
1662 enable_flow_control_ability(vptr);
1665 * Re-enable auto-polling because SRCI will disable
1669 enable_mii_autopoll(regs);
1671 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1672 netif_stop_queue(vptr->dev);
1674 netif_wake_queue(vptr->dev);
1677 if (status & ISR_MIBFI)
1678 velocity_update_hw_mibs(vptr);
1679 if (status & ISR_LSTEI)
1680 mac_rx_queue_wake(vptr->mac_regs);
1684 * velocity_free_tx_buf - free transmit buffer
1688 * Release an transmit buffer. If the buffer was preallocated then
1689 * recycle it, if not then unmap the buffer.
1692 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
1694 struct sk_buff *skb = tdinfo->skb;
1698 * Don't unmap the pre-allocated tx_bufs
1700 if (tdinfo->skb_dma && (tdinfo->skb_dma[0] != tdinfo->buf_dma)) {
1702 for (i = 0; i < tdinfo->nskb_dma; i++) {
1703 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1704 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], td->tdesc1.len, PCI_DMA_TODEVICE);
1706 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], skb->len, PCI_DMA_TODEVICE);
1708 tdinfo->skb_dma[i] = 0;
1711 dev_kfree_skb_irq(skb);
1716 * velocity_open - interface activation callback
1717 * @dev: network layer device to open
1719 * Called when the network layer brings the interface up. Returns
1720 * a negative posix error code on failure, or zero on success.
1722 * All the ring allocation and set up is done on open for this
1723 * adapter to minimise memory usage when inactive
1726 static int velocity_open(struct net_device *dev)
1728 struct velocity_info *vptr = netdev_priv(dev);
1731 vptr->rx_buf_sz = (dev->mtu <= 1504 ? PKT_BUF_SZ : dev->mtu + 32);
1733 ret = velocity_init_rings(vptr);
1737 ret = velocity_init_rd_ring(vptr);
1739 goto err_free_desc_rings;
1741 ret = velocity_init_td_ring(vptr);
1743 goto err_free_rd_ring;
1745 /* Ensure chip is running */
1746 pci_set_power_state(vptr->pdev, PCI_D0);
1748 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1750 ret = request_irq(vptr->pdev->irq, &velocity_intr, IRQF_SHARED,
1753 /* Power down the chip */
1754 pci_set_power_state(vptr->pdev, PCI_D3hot);
1755 goto err_free_td_ring;
1758 mac_enable_int(vptr->mac_regs);
1759 netif_start_queue(dev);
1760 vptr->flags |= VELOCITY_FLAGS_OPENED;
1765 velocity_free_td_ring(vptr);
1767 velocity_free_rd_ring(vptr);
1768 err_free_desc_rings:
1769 velocity_free_rings(vptr);
1774 * velocity_change_mtu - MTU change callback
1775 * @dev: network device
1776 * @new_mtu: desired MTU
1778 * Handle requests from the networking layer for MTU change on
1779 * this interface. It gets called on a change by the network layer.
1780 * Return zero for success or negative posix error code.
1783 static int velocity_change_mtu(struct net_device *dev, int new_mtu)
1785 struct velocity_info *vptr = netdev_priv(dev);
1786 unsigned long flags;
1787 int oldmtu = dev->mtu;
1790 if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
1791 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
1796 if (new_mtu != oldmtu) {
1797 spin_lock_irqsave(&vptr->lock, flags);
1799 netif_stop_queue(dev);
1800 velocity_shutdown(vptr);
1802 velocity_free_td_ring(vptr);
1803 velocity_free_rd_ring(vptr);
1807 vptr->rx_buf_sz = 9 * 1024;
1808 else if (new_mtu > 4096)
1809 vptr->rx_buf_sz = 8192;
1811 vptr->rx_buf_sz = 4 * 1024;
1813 ret = velocity_init_rd_ring(vptr);
1817 ret = velocity_init_td_ring(vptr);
1821 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1823 mac_enable_int(vptr->mac_regs);
1824 netif_start_queue(dev);
1826 spin_unlock_irqrestore(&vptr->lock, flags);
1833 * velocity_shutdown - shut down the chip
1834 * @vptr: velocity to deactivate
1836 * Shuts down the internal operations of the velocity and
1837 * disables interrupts, autopolling, transmit and receive
1840 static void velocity_shutdown(struct velocity_info *vptr)
1842 struct mac_regs __iomem * regs = vptr->mac_regs;
1843 mac_disable_int(regs);
1844 writel(CR0_STOP, ®s->CR0Set);
1845 writew(0xFFFF, ®s->TDCSRClr);
1846 writeb(0xFF, ®s->RDCSRClr);
1847 safe_disable_mii_autopoll(regs);
1848 mac_clear_isr(regs);
1852 * velocity_close - close adapter callback
1853 * @dev: network device
1855 * Callback from the network layer when the velocity is being
1856 * deactivated by the network layer
1859 static int velocity_close(struct net_device *dev)
1861 struct velocity_info *vptr = netdev_priv(dev);
1863 netif_stop_queue(dev);
1864 velocity_shutdown(vptr);
1866 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
1867 velocity_get_ip(vptr);
1869 free_irq(dev->irq, dev);
1871 /* Power down the chip */
1872 pci_set_power_state(vptr->pdev, PCI_D3hot);
1874 /* Free the resources */
1875 velocity_free_td_ring(vptr);
1876 velocity_free_rd_ring(vptr);
1877 velocity_free_rings(vptr);
1879 vptr->flags &= (~VELOCITY_FLAGS_OPENED);
1884 * velocity_xmit - transmit packet callback
1885 * @skb: buffer to transmit
1886 * @dev: network device
1888 * Called by the networ layer to request a packet is queued to
1889 * the velocity. Returns zero on success.
1892 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
1894 struct velocity_info *vptr = netdev_priv(dev);
1896 struct tx_desc *td_ptr;
1897 struct velocity_td_info *tdinfo;
1898 unsigned long flags;
1901 int pktlen = skb->len;
1903 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1904 if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
1910 spin_lock_irqsave(&vptr->lock, flags);
1912 index = vptr->td_curr[qnum];
1913 td_ptr = &(vptr->td_rings[qnum][index]);
1914 tdinfo = &(vptr->td_infos[qnum][index]);
1916 td_ptr->tdesc1.TCPLS = TCPLS_NORMAL;
1917 td_ptr->tdesc1.TCR = TCR0_TIC;
1918 td_ptr->td_buf[0].queue = 0;
1923 if (pktlen < ETH_ZLEN) {
1924 /* Cannot occur until ZC support */
1926 skb_copy_from_linear_data(skb, tdinfo->buf, skb->len);
1927 memset(tdinfo->buf + skb->len, 0, ETH_ZLEN - skb->len);
1929 tdinfo->skb_dma[0] = tdinfo->buf_dma;
1930 td_ptr->tdesc0.pktsize = pktlen;
1931 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1932 td_ptr->td_buf[0].pa_high = 0;
1933 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1934 tdinfo->nskb_dma = 1;
1935 td_ptr->tdesc1.CMDZ = 2;
1937 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1938 if (skb_shinfo(skb)->nr_frags > 0) {
1939 int nfrags = skb_shinfo(skb)->nr_frags;
1942 skb_copy_from_linear_data(skb, tdinfo->buf, skb->len);
1943 tdinfo->skb_dma[0] = tdinfo->buf_dma;
1944 td_ptr->tdesc0.pktsize =
1945 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1946 td_ptr->td_buf[0].pa_high = 0;
1947 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1948 tdinfo->nskb_dma = 1;
1949 td_ptr->tdesc1.CMDZ = 2;
1952 tdinfo->nskb_dma = 0;
1953 tdinfo->skb_dma[i] = pci_map_single(vptr->pdev, skb->data, skb->len - skb->data_len, PCI_DMA_TODEVICE);
1955 td_ptr->tdesc0.pktsize = pktlen;
1957 /* FIXME: support 48bit DMA later */
1958 td_ptr->td_buf[i].pa_low = cpu_to_le32(tdinfo->skb_dma);
1959 td_ptr->td_buf[i].pa_high = 0;
1960 td_ptr->td_buf[i].bufsize = skb->len->skb->data_len;
1962 for (i = 0; i < nfrags; i++) {
1963 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1964 void *addr = ((void *) page_address(frag->page + frag->page_offset));
1966 tdinfo->skb_dma[i + 1] = pci_map_single(vptr->pdev, addr, frag->size, PCI_DMA_TODEVICE);
1968 td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
1969 td_ptr->td_buf[i + 1].pa_high = 0;
1970 td_ptr->td_buf[i + 1].bufsize = frag->size;
1972 tdinfo->nskb_dma = i - 1;
1973 td_ptr->tdesc1.CMDZ = i;
1980 * Map the linear network buffer into PCI space and
1981 * add it to the transmit ring.
1984 tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
1985 td_ptr->tdesc0.pktsize = pktlen;
1986 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1987 td_ptr->td_buf[0].pa_high = 0;
1988 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1989 tdinfo->nskb_dma = 1;
1990 td_ptr->tdesc1.CMDZ = 2;
1993 if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
1994 td_ptr->tdesc1.pqinf.VID = (vptr->options.vid & 0xfff);
1995 td_ptr->tdesc1.pqinf.priority = 0;
1996 td_ptr->tdesc1.pqinf.CFI = 0;
1997 td_ptr->tdesc1.TCR |= TCR0_VETAG;
2001 * Handle hardware checksum
2003 if ((vptr->flags & VELOCITY_FLAGS_TX_CSUM)
2004 && (skb->ip_summed == CHECKSUM_PARTIAL)) {
2005 const struct iphdr *ip = ip_hdr(skb);
2006 if (ip->protocol == IPPROTO_TCP)
2007 td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2008 else if (ip->protocol == IPPROTO_UDP)
2009 td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2010 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2014 int prev = index - 1;
2017 prev = vptr->options.numtx - 1;
2018 td_ptr->tdesc0.owner = OWNED_BY_NIC;
2019 vptr->td_used[qnum]++;
2020 vptr->td_curr[qnum] = (index + 1) % vptr->options.numtx;
2022 if (AVAIL_TD(vptr, qnum) < 1)
2023 netif_stop_queue(dev);
2025 td_ptr = &(vptr->td_rings[qnum][prev]);
2026 td_ptr->td_buf[0].queue = 1;
2027 mac_tx_queue_wake(vptr->mac_regs, qnum);
2029 dev->trans_start = jiffies;
2030 spin_unlock_irqrestore(&vptr->lock, flags);
2035 * velocity_intr - interrupt callback
2036 * @irq: interrupt number
2037 * @dev_instance: interrupting device
2039 * Called whenever an interrupt is generated by the velocity
2040 * adapter IRQ line. We may not be the source of the interrupt
2041 * and need to identify initially if we are, and if not exit as
2042 * efficiently as possible.
2045 static int velocity_intr(int irq, void *dev_instance)
2047 struct net_device *dev = dev_instance;
2048 struct velocity_info *vptr = netdev_priv(dev);
2053 spin_lock(&vptr->lock);
2054 isr_status = mac_read_isr(vptr->mac_regs);
2057 if (isr_status == 0) {
2058 spin_unlock(&vptr->lock);
2062 mac_disable_int(vptr->mac_regs);
2065 * Keep processing the ISR until we have completed
2066 * processing and the isr_status becomes zero
2069 while (isr_status != 0) {
2070 mac_write_isr(vptr->mac_regs, isr_status);
2071 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2072 velocity_error(vptr, isr_status);
2073 if (isr_status & (ISR_PRXI | ISR_PPRXI))
2074 max_count += velocity_rx_srv(vptr, isr_status);
2075 if (isr_status & (ISR_PTXI | ISR_PPTXI))
2076 max_count += velocity_tx_srv(vptr, isr_status);
2077 isr_status = mac_read_isr(vptr->mac_regs);
2078 if (max_count > vptr->options.int_works)
2080 printk(KERN_WARNING "%s: excessive work at interrupt.\n",
2085 spin_unlock(&vptr->lock);
2086 mac_enable_int(vptr->mac_regs);
2093 * velocity_set_multi - filter list change callback
2094 * @dev: network device
2096 * Called by the network layer when the filter lists need to change
2097 * for a velocity adapter. Reload the CAMs with the new address
2101 static void velocity_set_multi(struct net_device *dev)
2103 struct velocity_info *vptr = netdev_priv(dev);
2104 struct mac_regs __iomem * regs = vptr->mac_regs;
2107 struct dev_mc_list *mclist;
2109 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2110 writel(0xffffffff, ®s->MARCAM[0]);
2111 writel(0xffffffff, ®s->MARCAM[4]);
2112 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
2113 } else if ((dev->mc_count > vptr->multicast_limit)
2114 || (dev->flags & IFF_ALLMULTI)) {
2115 writel(0xffffffff, ®s->MARCAM[0]);
2116 writel(0xffffffff, ®s->MARCAM[4]);
2117 rx_mode = (RCR_AM | RCR_AB);
2119 int offset = MCAM_SIZE - vptr->multicast_limit;
2120 mac_get_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
2122 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) {
2123 mac_set_cam(regs, i + offset, mclist->dmi_addr, VELOCITY_MULTICAST_CAM);
2124 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
2127 mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
2128 rx_mode = (RCR_AM | RCR_AB);
2130 if (dev->mtu > 1500)
2133 BYTE_REG_BITS_ON(rx_mode, ®s->RCR);
2138 * velocity_get_status - statistics callback
2139 * @dev: network device
2141 * Callback from the network layer to allow driver statistics
2142 * to be resynchronized with hardware collected state. In the
2143 * case of the velocity we need to pull the MIB counters from
2144 * the hardware into the counters before letting the network
2145 * layer display them.
2148 static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2150 struct velocity_info *vptr = netdev_priv(dev);
2152 /* If the hardware is down, don't touch MII */
2153 if(!netif_running(dev))
2154 return &vptr->stats;
2156 spin_lock_irq(&vptr->lock);
2157 velocity_update_hw_mibs(vptr);
2158 spin_unlock_irq(&vptr->lock);
2160 vptr->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2161 vptr->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2162 vptr->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2164 // unsigned long rx_dropped; /* no space in linux buffers */
2165 vptr->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2166 /* detailed rx_errors: */
2167 // unsigned long rx_length_errors;
2168 // unsigned long rx_over_errors; /* receiver ring buff overflow */
2169 vptr->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2170 // unsigned long rx_frame_errors; /* recv'd frame alignment error */
2171 // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2172 // unsigned long rx_missed_errors; /* receiver missed packet */
2174 /* detailed tx_errors */
2175 // unsigned long tx_fifo_errors;
2177 return &vptr->stats;
2182 * velocity_ioctl - ioctl entry point
2183 * @dev: network device
2184 * @rq: interface request ioctl
2185 * @cmd: command code
2187 * Called when the user issues an ioctl request to the network
2188 * device in question. The velocity interface supports MII.
2191 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2193 struct velocity_info *vptr = netdev_priv(dev);
2196 /* If we are asked for information and the device is power
2197 saving then we need to bring the device back up to talk to it */
2199 if (!netif_running(dev))
2200 pci_set_power_state(vptr->pdev, PCI_D0);
2203 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2204 case SIOCGMIIREG: /* Read MII PHY register. */
2205 case SIOCSMIIREG: /* Write to MII PHY register. */
2206 ret = velocity_mii_ioctl(dev, rq, cmd);
2212 if (!netif_running(dev))
2213 pci_set_power_state(vptr->pdev, PCI_D3hot);
2220 * Definition for our device driver. The PCI layer interface
2221 * uses this to handle all our card discover and plugging
2224 static struct pci_driver velocity_driver = {
2225 .name = VELOCITY_NAME,
2226 .id_table = velocity_id_table,
2227 .probe = velocity_found1,
2228 .remove = __devexit_p(velocity_remove1),
2230 .suspend = velocity_suspend,
2231 .resume = velocity_resume,
2236 * velocity_init_module - load time function
2238 * Called when the velocity module is loaded. The PCI driver
2239 * is registered with the PCI layer, and in turn will call
2240 * the probe functions for each velocity adapter installed
2244 static int __init velocity_init_module(void)
2248 velocity_register_notifier();
2249 ret = pci_register_driver(&velocity_driver);
2251 velocity_unregister_notifier();
2256 * velocity_cleanup - module unload
2258 * When the velocity hardware is unloaded this function is called.
2259 * It will clean up the notifiers and the unregister the PCI
2260 * driver interface for this hardware. This in turn cleans up
2261 * all discovered interfaces before returning from the function
2264 static void __exit velocity_cleanup_module(void)
2266 velocity_unregister_notifier();
2267 pci_unregister_driver(&velocity_driver);
2270 module_init(velocity_init_module);
2271 module_exit(velocity_cleanup_module);
2275 * MII access , media link mode setting functions
2280 * mii_init - set up MII
2281 * @vptr: velocity adapter
2282 * @mii_status: links tatus
2284 * Set up the PHY for the current link state.
2287 static void mii_init(struct velocity_info *vptr, u32 mii_status)
2291 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
2292 case PHYID_CICADA_CS8201:
2294 * Reset to hardware default
2296 MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2298 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2299 * off it in NWay-forced half mode for NWay-forced v.s.
2300 * legacy-forced issue.
2302 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2303 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2305 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2307 * Turn on Link/Activity LED enable bit for CIS8201
2309 MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
2311 case PHYID_VT3216_32BIT:
2312 case PHYID_VT3216_64BIT:
2314 * Reset to hardware default
2316 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2318 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2319 * off it in NWay-forced half mode for NWay-forced v.s.
2320 * legacy-forced issue
2322 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2323 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2325 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2328 case PHYID_MARVELL_1000:
2329 case PHYID_MARVELL_1000S:
2331 * Assert CRS on Transmit
2333 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
2335 * Reset to hardware default
2337 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2342 velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
2343 if (BMCR & BMCR_ISO) {
2345 velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
2350 * safe_disable_mii_autopoll - autopoll off
2351 * @regs: velocity registers
2353 * Turn off the autopoll and wait for it to disable on the chip
2356 static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs)
2360 /* turn off MAUTO */
2361 writeb(0, ®s->MIICR);
2362 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2364 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
2370 * enable_mii_autopoll - turn on autopolling
2371 * @regs: velocity registers
2373 * Enable the MII link status autopoll feature on the Velocity
2374 * hardware. Wait for it to enable.
2377 static void enable_mii_autopoll(struct mac_regs __iomem * regs)
2381 writeb(0, &(regs->MIICR));
2382 writeb(MIIADR_SWMPL, ®s->MIIADR);
2384 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2386 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
2390 writeb(MIICR_MAUTO, ®s->MIICR);
2392 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2394 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
2401 * velocity_mii_read - read MII data
2402 * @regs: velocity registers
2403 * @index: MII register index
2404 * @data: buffer for received data
2406 * Perform a single read of an MII 16bit register. Returns zero
2407 * on success or -ETIMEDOUT if the PHY did not respond.
2410 static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
2415 * Disable MIICR_MAUTO, so that mii addr can be set normally
2417 safe_disable_mii_autopoll(regs);
2419 writeb(index, ®s->MIIADR);
2421 BYTE_REG_BITS_ON(MIICR_RCMD, ®s->MIICR);
2423 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2424 if (!(readb(®s->MIICR) & MIICR_RCMD))
2428 *data = readw(®s->MIIDATA);
2430 enable_mii_autopoll(regs);
2431 if (ww == W_MAX_TIMEOUT)
2437 * velocity_mii_write - write MII data
2438 * @regs: velocity registers
2439 * @index: MII register index
2440 * @data: 16bit data for the MII register
2442 * Perform a single write to an MII 16bit register. Returns zero
2443 * on success or -ETIMEDOUT if the PHY did not respond.
2446 static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
2451 * Disable MIICR_MAUTO, so that mii addr can be set normally
2453 safe_disable_mii_autopoll(regs);
2455 /* MII reg offset */
2456 writeb(mii_addr, ®s->MIIADR);
2458 writew(data, ®s->MIIDATA);
2460 /* turn on MIICR_WCMD */
2461 BYTE_REG_BITS_ON(MIICR_WCMD, ®s->MIICR);
2463 /* W_MAX_TIMEOUT is the timeout period */
2464 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2466 if (!(readb(®s->MIICR) & MIICR_WCMD))
2469 enable_mii_autopoll(regs);
2471 if (ww == W_MAX_TIMEOUT)
2477 * velocity_get_opt_media_mode - get media selection
2478 * @vptr: velocity adapter
2480 * Get the media mode stored in EEPROM or module options and load
2481 * mii_status accordingly. The requested link state information
2485 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
2489 switch (vptr->options.spd_dpx) {
2491 status = VELOCITY_AUTONEG_ENABLE;
2493 case SPD_DPX_100_FULL:
2494 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
2496 case SPD_DPX_10_FULL:
2497 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
2499 case SPD_DPX_100_HALF:
2500 status = VELOCITY_SPEED_100;
2502 case SPD_DPX_10_HALF:
2503 status = VELOCITY_SPEED_10;
2506 vptr->mii_status = status;
2511 * mii_set_auto_on - autonegotiate on
2514 * Enable autonegotation on this interface
2517 static void mii_set_auto_on(struct velocity_info *vptr)
2519 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
2520 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
2522 MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2527 static void mii_set_auto_off(struct velocity_info * vptr)
2529 MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2534 * set_mii_flow_control - flow control setup
2535 * @vptr: velocity interface
2537 * Set up the flow control on this interface according to
2538 * the supplied user/eeprom options.
2541 static void set_mii_flow_control(struct velocity_info *vptr)
2543 /*Enable or Disable PAUSE in ANAR */
2544 switch (vptr->options.flow_cntl) {
2546 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2547 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2551 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2552 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2555 case FLOW_CNTL_TX_RX:
2556 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2557 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2560 case FLOW_CNTL_DISABLE:
2561 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2562 MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2570 * velocity_set_media_mode - set media mode
2571 * @mii_status: old MII link state
2573 * Check the media link state and configure the flow control
2574 * PHY and also velocity hardware setup accordingly. In particular
2575 * we need to set up CD polling and frame bursting.
2578 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
2581 struct mac_regs __iomem * regs = vptr->mac_regs;
2583 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
2584 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
2586 /* Set mii link status */
2587 set_mii_flow_control(vptr);
2590 Check if new status is consisent with current status
2591 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
2592 || (mii_status==curr_status)) {
2593 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
2594 vptr->mii_status=check_connection_type(vptr->mac_regs);
2595 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
2600 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201) {
2601 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
2605 * If connection type is AUTO
2607 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
2608 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
2609 /* clear force MAC mode bit */
2610 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, ®s->CHIPGCR);
2611 /* set duplex mode of MAC according to duplex mode of MII */
2612 MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
2613 MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2614 MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
2616 /* enable AUTO-NEGO mode */
2617 mii_set_auto_on(vptr);
2623 * 1. if it's 3119, disable frame bursting in halfduplex mode
2624 * and enable it in fullduplex mode
2625 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
2626 * 3. only enable CD heart beat counter in 10HD mode
2629 /* set force MAC mode bit */
2630 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, ®s->CHIPGCR);
2632 CHIPGCR = readb(®s->CHIPGCR);
2633 CHIPGCR &= ~CHIPGCR_FCGMII;
2635 if (mii_status & VELOCITY_DUPLEX_FULL) {
2636 CHIPGCR |= CHIPGCR_FCFDX;
2637 writeb(CHIPGCR, ®s->CHIPGCR);
2638 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
2639 if (vptr->rev_id < REV_ID_VT3216_A0)
2640 BYTE_REG_BITS_OFF(TCR_TB2BDIS, ®s->TCR);
2642 CHIPGCR &= ~CHIPGCR_FCFDX;
2643 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
2644 writeb(CHIPGCR, ®s->CHIPGCR);
2645 if (vptr->rev_id < REV_ID_VT3216_A0)
2646 BYTE_REG_BITS_ON(TCR_TB2BDIS, ®s->TCR);
2649 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2651 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10)) {
2652 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, ®s->TESTCFG);
2654 BYTE_REG_BITS_ON(TESTCFG_HBDIS, ®s->TESTCFG);
2656 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
2657 velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
2658 ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
2659 if (mii_status & VELOCITY_SPEED_100) {
2660 if (mii_status & VELOCITY_DUPLEX_FULL)
2665 if (mii_status & VELOCITY_DUPLEX_FULL)
2670 velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
2671 /* enable AUTO-NEGO mode */
2672 mii_set_auto_on(vptr);
2673 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
2675 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
2676 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
2677 return VELOCITY_LINK_CHANGE;
2681 * mii_check_media_mode - check media state
2682 * @regs: velocity registers
2684 * Check the current MII status and determine the link status
2688 static u32 mii_check_media_mode(struct mac_regs __iomem * regs)
2693 if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
2694 status |= VELOCITY_LINK_FAIL;
2696 if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
2697 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
2698 else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
2699 status |= (VELOCITY_SPEED_1000);
2701 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2702 if (ANAR & ANAR_TXFD)
2703 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
2704 else if (ANAR & ANAR_TX)
2705 status |= VELOCITY_SPEED_100;
2706 else if (ANAR & ANAR_10FD)
2707 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
2709 status |= (VELOCITY_SPEED_10);
2712 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2713 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2714 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2715 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2716 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2717 status |= VELOCITY_AUTONEG_ENABLE;
2724 static u32 check_connection_type(struct mac_regs __iomem * regs)
2729 PHYSR0 = readb(®s->PHYSR0);
2732 if (!(PHYSR0 & PHYSR0_LINKGD))
2733 status|=VELOCITY_LINK_FAIL;
2736 if (PHYSR0 & PHYSR0_FDPX)
2737 status |= VELOCITY_DUPLEX_FULL;
2739 if (PHYSR0 & PHYSR0_SPDG)
2740 status |= VELOCITY_SPEED_1000;
2741 else if (PHYSR0 & PHYSR0_SPD10)
2742 status |= VELOCITY_SPEED_10;
2744 status |= VELOCITY_SPEED_100;
2746 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2747 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2748 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2749 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2750 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2751 status |= VELOCITY_AUTONEG_ENABLE;
2759 * enable_flow_control_ability - flow control
2760 * @vptr: veloity to configure
2762 * Set up flow control according to the flow control options
2763 * determined by the eeprom/configuration.
2766 static void enable_flow_control_ability(struct velocity_info *vptr)
2769 struct mac_regs __iomem * regs = vptr->mac_regs;
2771 switch (vptr->options.flow_cntl) {
2773 case FLOW_CNTL_DEFAULT:
2774 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, ®s->PHYSR0))
2775 writel(CR0_FDXRFCEN, ®s->CR0Set);
2777 writel(CR0_FDXRFCEN, ®s->CR0Clr);
2779 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, ®s->PHYSR0))
2780 writel(CR0_FDXTFCEN, ®s->CR0Set);
2782 writel(CR0_FDXTFCEN, ®s->CR0Clr);
2786 writel(CR0_FDXTFCEN, ®s->CR0Set);
2787 writel(CR0_FDXRFCEN, ®s->CR0Clr);
2791 writel(CR0_FDXRFCEN, ®s->CR0Set);
2792 writel(CR0_FDXTFCEN, ®s->CR0Clr);
2795 case FLOW_CNTL_TX_RX:
2796 writel(CR0_FDXTFCEN, ®s->CR0Set);
2797 writel(CR0_FDXRFCEN, ®s->CR0Set);
2800 case FLOW_CNTL_DISABLE:
2801 writel(CR0_FDXRFCEN, ®s->CR0Clr);
2802 writel(CR0_FDXTFCEN, ®s->CR0Clr);
2813 * velocity_ethtool_up - pre hook for ethtool
2814 * @dev: network device
2816 * Called before an ethtool operation. We need to make sure the
2817 * chip is out of D3 state before we poke at it.
2820 static int velocity_ethtool_up(struct net_device *dev)
2822 struct velocity_info *vptr = netdev_priv(dev);
2823 if (!netif_running(dev))
2824 pci_set_power_state(vptr->pdev, PCI_D0);
2829 * velocity_ethtool_down - post hook for ethtool
2830 * @dev: network device
2832 * Called after an ethtool operation. Restore the chip back to D3
2833 * state if it isn't running.
2836 static void velocity_ethtool_down(struct net_device *dev)
2838 struct velocity_info *vptr = netdev_priv(dev);
2839 if (!netif_running(dev))
2840 pci_set_power_state(vptr->pdev, PCI_D3hot);
2843 static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2845 struct velocity_info *vptr = netdev_priv(dev);
2846 struct mac_regs __iomem * regs = vptr->mac_regs;
2848 status = check_connection_type(vptr->mac_regs);
2850 cmd->supported = SUPPORTED_TP |
2852 SUPPORTED_10baseT_Half |
2853 SUPPORTED_10baseT_Full |
2854 SUPPORTED_100baseT_Half |
2855 SUPPORTED_100baseT_Full |
2856 SUPPORTED_1000baseT_Half |
2857 SUPPORTED_1000baseT_Full;
2858 if (status & VELOCITY_SPEED_1000)
2859 cmd->speed = SPEED_1000;
2860 else if (status & VELOCITY_SPEED_100)
2861 cmd->speed = SPEED_100;
2863 cmd->speed = SPEED_10;
2864 cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2865 cmd->port = PORT_TP;
2866 cmd->transceiver = XCVR_INTERNAL;
2867 cmd->phy_address = readb(®s->MIIADR) & 0x1F;
2869 if (status & VELOCITY_DUPLEX_FULL)
2870 cmd->duplex = DUPLEX_FULL;
2872 cmd->duplex = DUPLEX_HALF;
2877 static int velocity_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2879 struct velocity_info *vptr = netdev_priv(dev);
2884 curr_status = check_connection_type(vptr->mac_regs);
2885 curr_status &= (~VELOCITY_LINK_FAIL);
2887 new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
2888 new_status |= ((cmd->speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
2889 new_status |= ((cmd->speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
2890 new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
2892 if ((new_status & VELOCITY_AUTONEG_ENABLE) && (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE)))
2895 velocity_set_media_mode(vptr, new_status);
2900 static u32 velocity_get_link(struct net_device *dev)
2902 struct velocity_info *vptr = netdev_priv(dev);
2903 struct mac_regs __iomem * regs = vptr->mac_regs;
2904 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, ®s->PHYSR0) ? 1 : 0;
2907 static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2909 struct velocity_info *vptr = netdev_priv(dev);
2910 strcpy(info->driver, VELOCITY_NAME);
2911 strcpy(info->version, VELOCITY_VERSION);
2912 strcpy(info->bus_info, pci_name(vptr->pdev));
2915 static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2917 struct velocity_info *vptr = netdev_priv(dev);
2918 wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
2919 wol->wolopts |= WAKE_MAGIC;
2921 if (vptr->wol_opts & VELOCITY_WOL_PHY)
2922 wol.wolopts|=WAKE_PHY;
2924 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
2925 wol->wolopts |= WAKE_UCAST;
2926 if (vptr->wol_opts & VELOCITY_WOL_ARP)
2927 wol->wolopts |= WAKE_ARP;
2928 memcpy(&wol->sopass, vptr->wol_passwd, 6);
2931 static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2933 struct velocity_info *vptr = netdev_priv(dev);
2935 if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
2937 vptr->wol_opts = VELOCITY_WOL_MAGIC;
2940 if (wol.wolopts & WAKE_PHY) {
2941 vptr->wol_opts|=VELOCITY_WOL_PHY;
2942 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
2946 if (wol->wolopts & WAKE_MAGIC) {
2947 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
2948 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2950 if (wol->wolopts & WAKE_UCAST) {
2951 vptr->wol_opts |= VELOCITY_WOL_UCAST;
2952 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2954 if (wol->wolopts & WAKE_ARP) {
2955 vptr->wol_opts |= VELOCITY_WOL_ARP;
2956 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2958 memcpy(vptr->wol_passwd, wol->sopass, 6);
2962 static u32 velocity_get_msglevel(struct net_device *dev)
2967 static void velocity_set_msglevel(struct net_device *dev, u32 value)
2972 static const struct ethtool_ops velocity_ethtool_ops = {
2973 .get_settings = velocity_get_settings,
2974 .set_settings = velocity_set_settings,
2975 .get_drvinfo = velocity_get_drvinfo,
2976 .get_wol = velocity_ethtool_get_wol,
2977 .set_wol = velocity_ethtool_set_wol,
2978 .get_msglevel = velocity_get_msglevel,
2979 .set_msglevel = velocity_set_msglevel,
2980 .get_link = velocity_get_link,
2981 .begin = velocity_ethtool_up,
2982 .complete = velocity_ethtool_down
2986 * velocity_mii_ioctl - MII ioctl handler
2987 * @dev: network device
2988 * @ifr: the ifreq block for the ioctl
2991 * Process MII requests made via ioctl from the network layer. These
2992 * are used by tools like kudzu to interrogate the link state of the
2996 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2998 struct velocity_info *vptr = netdev_priv(dev);
2999 struct mac_regs __iomem * regs = vptr->mac_regs;
3000 unsigned long flags;
3001 struct mii_ioctl_data *miidata = if_mii(ifr);
3006 miidata->phy_id = readb(®s->MIIADR) & 0x1f;
3009 if (!capable(CAP_NET_ADMIN))
3011 if(velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
3015 if (!capable(CAP_NET_ADMIN))
3017 spin_lock_irqsave(&vptr->lock, flags);
3018 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
3019 spin_unlock_irqrestore(&vptr->lock, flags);
3020 check_connection_type(vptr->mac_regs);
3033 * velocity_save_context - save registers
3035 * @context: buffer for stored context
3037 * Retrieve the current configuration from the velocity hardware
3038 * and stash it in the context structure, for use by the context
3039 * restore functions. This allows us to save things we need across
3043 static void velocity_save_context(struct velocity_info *vptr, struct velocity_context * context)
3045 struct mac_regs __iomem * regs = vptr->mac_regs;
3047 u8 __iomem *ptr = (u8 __iomem *)regs;
3049 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3050 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3052 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3053 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3055 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3056 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3061 * velocity_restore_context - restore registers
3063 * @context: buffer for stored context
3065 * Reload the register configuration from the velocity context
3066 * created by velocity_save_context.
3069 static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3071 struct mac_regs __iomem * regs = vptr->mac_regs;
3073 u8 __iomem *ptr = (u8 __iomem *)regs;
3075 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4) {
3076 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3080 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3082 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3084 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3087 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4) {
3088 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3091 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4) {
3092 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3095 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++) {
3096 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3102 * wol_calc_crc - WOL CRC
3103 * @pattern: data pattern
3104 * @mask_pattern: mask
3106 * Compute the wake on lan crc hashes for the packet header
3107 * we are interested in.
3110 static u16 wol_calc_crc(int size, u8 * pattern, u8 *mask_pattern)
3116 for (i = 0; i < size; i++) {
3117 mask = mask_pattern[i];
3119 /* Skip this loop if the mask equals to zero */
3123 for (j = 0; j < 8; j++) {
3124 if ((mask & 0x01) == 0) {
3129 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
3132 /* Finally, invert the result once to get the correct data */
3134 return bitrev32(crc) >> 16;
3138 * velocity_set_wol - set up for wake on lan
3139 * @vptr: velocity to set WOL status on
3141 * Set a card up for wake on lan either by unicast or by
3144 * FIXME: check static buffer is safe here
3147 static int velocity_set_wol(struct velocity_info *vptr)
3149 struct mac_regs __iomem * regs = vptr->mac_regs;
3153 static u32 mask_pattern[2][4] = {
3154 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3155 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3158 writew(0xFFFF, ®s->WOLCRClr);
3159 writeb(WOLCFG_SAB | WOLCFG_SAM, ®s->WOLCFGSet);
3160 writew(WOLCR_MAGIC_EN, ®s->WOLCRSet);
3163 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3164 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), ®s->WOLCRSet);
3167 if (vptr->wol_opts & VELOCITY_WOL_UCAST) {
3168 writew(WOLCR_UNICAST_EN, ®s->WOLCRSet);
3171 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3172 struct arp_packet *arp = (struct arp_packet *) buf;
3174 memset(buf, 0, sizeof(struct arp_packet) + 7);
3176 for (i = 0; i < 4; i++)
3177 writel(mask_pattern[0][i], ®s->ByteMask[0][i]);
3179 arp->type = htons(ETH_P_ARP);
3180 arp->ar_op = htons(1);
3182 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3184 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3185 (u8 *) & mask_pattern[0][0]);
3187 writew(crc, ®s->PatternCRC[0]);
3188 writew(WOLCR_ARP_EN, ®s->WOLCRSet);
3191 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, ®s->PWCFGSet);
3192 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, ®s->PWCFGSet);
3194 writew(0x0FFF, ®s->WOLSRClr);
3196 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3197 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3198 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
3200 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
3203 if (vptr->mii_status & VELOCITY_SPEED_1000)
3204 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
3206 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, ®s->CHIPGCR);
3210 GCR = readb(®s->CHIPGCR);
3211 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3212 writeb(GCR, ®s->CHIPGCR);
3215 BYTE_REG_BITS_OFF(ISR_PWEI, ®s->ISR);
3216 /* Turn on SWPTAG just before entering power mode */
3217 BYTE_REG_BITS_ON(STICKHW_SWPTAG, ®s->STICKHW);
3218 /* Go to bed ..... */
3219 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), ®s->STICKHW);
3224 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
3226 struct net_device *dev = pci_get_drvdata(pdev);
3227 struct velocity_info *vptr = netdev_priv(dev);
3228 unsigned long flags;
3230 if(!netif_running(vptr->dev))
3233 netif_device_detach(vptr->dev);
3235 spin_lock_irqsave(&vptr->lock, flags);
3236 pci_save_state(pdev);
3238 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3239 velocity_get_ip(vptr);
3240 velocity_save_context(vptr, &vptr->context);
3241 velocity_shutdown(vptr);
3242 velocity_set_wol(vptr);
3243 pci_enable_wake(pdev, 3, 1);
3244 pci_set_power_state(pdev, PCI_D3hot);
3246 velocity_save_context(vptr, &vptr->context);
3247 velocity_shutdown(vptr);
3248 pci_disable_device(pdev);
3249 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3252 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3254 spin_unlock_irqrestore(&vptr->lock, flags);
3258 static int velocity_resume(struct pci_dev *pdev)
3260 struct net_device *dev = pci_get_drvdata(pdev);
3261 struct velocity_info *vptr = netdev_priv(dev);
3262 unsigned long flags;
3265 if(!netif_running(vptr->dev))
3268 pci_set_power_state(pdev, PCI_D0);
3269 pci_enable_wake(pdev, 0, 0);
3270 pci_restore_state(pdev);
3272 mac_wol_reset(vptr->mac_regs);
3274 spin_lock_irqsave(&vptr->lock, flags);
3275 velocity_restore_context(vptr, &vptr->context);
3276 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3277 mac_disable_int(vptr->mac_regs);
3279 velocity_tx_srv(vptr, 0);
3281 for (i = 0; i < vptr->num_txq; i++) {
3282 if (vptr->td_used[i]) {
3283 mac_tx_queue_wake(vptr->mac_regs, i);
3287 mac_enable_int(vptr->mac_regs);
3288 spin_unlock_irqrestore(&vptr->lock, flags);
3289 netif_device_attach(vptr->dev);
3296 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3298 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3301 struct net_device *dev = ifa->ifa_dev->dev;
3302 struct velocity_info *vptr;
3303 unsigned long flags;
3305 spin_lock_irqsave(&velocity_dev_list_lock, flags);
3306 list_for_each_entry(vptr, &velocity_dev_list, list) {
3307 if (vptr->dev == dev) {
3308 velocity_get_ip(vptr);
3312 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);