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.
11 * rx_copybreak/alignment
15 * The changes are (c) Copyright 2004, Red Hat Inc. <alan@lxorguk.ukuu.org.uk>
16 * Additional fixes and clean up: Francois Romieu
18 * This source has not been verified for use in safety critical systems.
20 * Please direct queries about the revamped driver to the linux-kernel
25 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
26 * All rights reserved.
28 * This software may be redistributed and/or modified under
29 * the terms of the GNU General Public License as published by the Free
30 * Software Foundation; either version 2 of the License, or
33 * This program is distributed in the hope that it will be useful, but
34 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
35 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
38 * Author: Chuang Liang-Shing, AJ Jiang
42 * MODULE_LICENSE("GPL");
47 #include <linux/module.h>
48 #include <linux/types.h>
49 #include <linux/init.h>
51 #include <linux/errno.h>
52 #include <linux/ioport.h>
53 #include <linux/pci.h>
54 #include <linux/kernel.h>
55 #include <linux/netdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/skbuff.h>
58 #include <linux/delay.h>
59 #include <linux/timer.h>
60 #include <linux/slab.h>
61 #include <linux/interrupt.h>
62 #include <linux/string.h>
63 #include <linux/wait.h>
66 #include <linux/uaccess.h>
67 #include <linux/proc_fs.h>
68 #include <linux/inetdevice.h>
69 #include <linux/reboot.h>
70 #include <linux/ethtool.h>
71 #include <linux/mii.h>
73 #include <linux/if_arp.h>
74 #include <linux/if_vlan.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;
85 static int msglevel = MSG_LEVEL_INFO;
88 * mac_get_cam_mask - Read a CAM mask
89 * @regs: register block for this velocity
90 * @mask: buffer to store mask
92 * Fetch the mask bits of the selected CAM and store them into the
93 * provided mask buffer.
96 static void mac_get_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
100 /* Select CAM mask */
101 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
103 writeb(0, ®s->CAMADDR);
106 for (i = 0; i < 8; i++)
107 *mask++ = readb(&(regs->MARCAM[i]));
110 writeb(0, ®s->CAMADDR);
113 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
118 * mac_set_cam_mask - Set a CAM mask
119 * @regs: register block for this velocity
120 * @mask: CAM mask to load
122 * Store a new mask into a CAM
125 static void mac_set_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
128 /* Select CAM mask */
129 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
131 writeb(CAMADDR_CAMEN, ®s->CAMADDR);
133 for (i = 0; i < 8; i++)
134 writeb(*mask++, &(regs->MARCAM[i]));
137 writeb(0, ®s->CAMADDR);
140 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
143 static void mac_set_vlan_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
146 /* Select CAM mask */
147 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
149 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL, ®s->CAMADDR);
151 for (i = 0; i < 8; i++)
152 writeb(*mask++, &(regs->MARCAM[i]));
155 writeb(0, ®s->CAMADDR);
158 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
162 * mac_set_cam - set CAM data
163 * @regs: register block of this velocity
165 * @addr: 2 or 6 bytes of CAM data
167 * Load an address or vlan tag into a CAM
170 static void mac_set_cam(struct mac_regs __iomem *regs, int idx, const u8 *addr)
174 /* Select CAM mask */
175 BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
179 writeb(CAMADDR_CAMEN | idx, ®s->CAMADDR);
181 for (i = 0; i < 6; i++)
182 writeb(*addr++, &(regs->MARCAM[i]));
184 BYTE_REG_BITS_ON(CAMCR_CAMWR, ®s->CAMCR);
188 writeb(0, ®s->CAMADDR);
191 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
194 static void mac_set_vlan_cam(struct mac_regs __iomem *regs, int idx,
198 /* Select CAM mask */
199 BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
203 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL | idx, ®s->CAMADDR);
204 writew(*((u16 *) addr), ®s->MARCAM[0]);
206 BYTE_REG_BITS_ON(CAMCR_CAMWR, ®s->CAMCR);
210 writeb(0, ®s->CAMADDR);
213 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
218 * mac_wol_reset - reset WOL after exiting low power
219 * @regs: register block of this velocity
221 * Called after we drop out of wake on lan mode in order to
222 * reset the Wake on lan features. This function doesn't restore
223 * the rest of the logic from the result of sleep/wakeup
226 static void mac_wol_reset(struct mac_regs __iomem *regs)
229 /* Turn off SWPTAG right after leaving power mode */
230 BYTE_REG_BITS_OFF(STICKHW_SWPTAG, ®s->STICKHW);
231 /* clear sticky bits */
232 BYTE_REG_BITS_OFF((STICKHW_DS1 | STICKHW_DS0), ®s->STICKHW);
234 BYTE_REG_BITS_OFF(CHIPGCR_FCGMII, ®s->CHIPGCR);
235 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, ®s->CHIPGCR);
236 /* disable force PME-enable */
237 writeb(WOLCFG_PMEOVR, ®s->WOLCFGClr);
238 /* disable power-event config bit */
239 writew(0xFFFF, ®s->WOLCRClr);
240 /* clear power status */
241 writew(0xFFFF, ®s->WOLSRClr);
244 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
245 static const struct ethtool_ops velocity_ethtool_ops;
248 Define module options
251 MODULE_AUTHOR("VIA Networking Technologies, Inc.");
252 MODULE_LICENSE("GPL");
253 MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
255 #define VELOCITY_PARAM(N, D) \
256 static int N[MAX_UNITS] = OPTION_DEFAULT;\
257 module_param_array(N, int, NULL, 0); \
258 MODULE_PARM_DESC(N, D);
260 #define RX_DESC_MIN 64
261 #define RX_DESC_MAX 255
262 #define RX_DESC_DEF 64
263 VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
265 #define TX_DESC_MIN 16
266 #define TX_DESC_MAX 256
267 #define TX_DESC_DEF 64
268 VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
270 #define RX_THRESH_MIN 0
271 #define RX_THRESH_MAX 3
272 #define RX_THRESH_DEF 0
273 /* rx_thresh[] is used for controlling the receive fifo threshold.
274 0: indicate the rxfifo threshold is 128 bytes.
275 1: indicate the rxfifo threshold is 512 bytes.
276 2: indicate the rxfifo threshold is 1024 bytes.
277 3: indicate the rxfifo threshold is store & forward.
279 VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
281 #define DMA_LENGTH_MIN 0
282 #define DMA_LENGTH_MAX 7
283 #define DMA_LENGTH_DEF 0
285 /* DMA_length[] is used for controlling the DMA length
292 6: SF(flush till emply)
293 7: SF(flush till emply)
295 VELOCITY_PARAM(DMA_length, "DMA length");
297 #define IP_ALIG_DEF 0
298 /* IP_byte_align[] is used for IP header DWORD byte aligned
299 0: indicate the IP header won't be DWORD byte aligned.(Default) .
300 1: indicate the IP header will be DWORD byte aligned.
301 In some enviroment, the IP header should be DWORD byte aligned,
302 or the packet will be droped when we receive it. (eg: IPVS)
304 VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
306 #define TX_CSUM_DEF 1
307 /* txcsum_offload[] is used for setting the checksum offload ability of NIC.
308 (We only support RX checksum offload now)
309 0: disable csum_offload[checksum offload
310 1: enable checksum offload. (Default)
312 VELOCITY_PARAM(txcsum_offload, "Enable transmit packet checksum offload");
314 #define FLOW_CNTL_DEF 1
315 #define FLOW_CNTL_MIN 1
316 #define FLOW_CNTL_MAX 5
318 /* flow_control[] is used for setting the flow control ability of NIC.
319 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
320 2: enable TX flow control.
321 3: enable RX flow control.
322 4: enable RX/TX flow control.
325 VELOCITY_PARAM(flow_control, "Enable flow control ability");
327 #define MED_LNK_DEF 0
328 #define MED_LNK_MIN 0
329 #define MED_LNK_MAX 4
330 /* speed_duplex[] is used for setting the speed and duplex mode of NIC.
331 0: indicate autonegotiation for both speed and duplex mode
332 1: indicate 100Mbps half duplex mode
333 2: indicate 100Mbps full duplex mode
334 3: indicate 10Mbps half duplex mode
335 4: indicate 10Mbps full duplex mode
338 if EEPROM have been set to the force mode, this option is ignored
341 VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
343 #define VAL_PKT_LEN_DEF 0
344 /* ValPktLen[] is used for setting the checksum offload ability of NIC.
345 0: Receive frame with invalid layer 2 length (Default)
346 1: Drop frame with invalid layer 2 length
348 VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
350 #define WOL_OPT_DEF 0
351 #define WOL_OPT_MIN 0
352 #define WOL_OPT_MAX 7
353 /* wol_opts[] is used for controlling wake on lan behavior.
354 0: Wake up if recevied a magic packet. (Default)
355 1: Wake up if link status is on/off.
356 2: Wake up if recevied an arp packet.
357 4: Wake up if recevied any unicast packet.
358 Those value can be sumed up to support more than one option.
360 VELOCITY_PARAM(wol_opts, "Wake On Lan options");
362 #define INT_WORKS_DEF 20
363 #define INT_WORKS_MIN 10
364 #define INT_WORKS_MAX 64
366 VELOCITY_PARAM(int_works, "Number of packets per interrupt services");
368 static int rx_copybreak = 200;
369 module_param(rx_copybreak, int, 0644);
370 MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
372 static void velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr,
373 const struct velocity_info_tbl *info);
374 static int velocity_get_pci_info(struct velocity_info *, struct pci_dev *pdev);
375 static void velocity_print_info(struct velocity_info *vptr);
376 static int velocity_open(struct net_device *dev);
377 static int velocity_change_mtu(struct net_device *dev, int mtu);
378 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev);
379 static irqreturn_t velocity_intr(int irq, void *dev_instance);
380 static void velocity_set_multi(struct net_device *dev);
381 static struct net_device_stats *velocity_get_stats(struct net_device *dev);
382 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
383 static int velocity_close(struct net_device *dev);
384 static int velocity_receive_frame(struct velocity_info *, int idx);
385 static int velocity_alloc_rx_buf(struct velocity_info *, int idx);
386 static void velocity_free_rd_ring(struct velocity_info *vptr);
387 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *);
388 static int velocity_soft_reset(struct velocity_info *vptr);
389 static void mii_init(struct velocity_info *vptr, u32 mii_status);
390 static u32 velocity_get_link(struct net_device *dev);
391 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr);
392 static void velocity_print_link_status(struct velocity_info *vptr);
393 static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs);
394 static void velocity_shutdown(struct velocity_info *vptr);
395 static void enable_flow_control_ability(struct velocity_info *vptr);
396 static void enable_mii_autopoll(struct mac_regs __iomem *regs);
397 static int velocity_mii_read(struct mac_regs __iomem *, u8 byIdx, u16 *pdata);
398 static int velocity_mii_write(struct mac_regs __iomem *, u8 byMiiAddr, u16 data);
399 static u32 mii_check_media_mode(struct mac_regs __iomem *regs);
400 static u32 check_connection_type(struct mac_regs __iomem *regs);
401 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status);
405 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state);
406 static int velocity_resume(struct pci_dev *pdev);
408 static DEFINE_SPINLOCK(velocity_dev_list_lock);
409 static LIST_HEAD(velocity_dev_list);
413 #if defined(CONFIG_PM) && defined(CONFIG_INET)
415 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr);
417 static struct notifier_block velocity_inetaddr_notifier = {
418 .notifier_call = velocity_netdev_event,
421 static void velocity_register_notifier(void)
423 register_inetaddr_notifier(&velocity_inetaddr_notifier);
426 static void velocity_unregister_notifier(void)
428 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
433 #define velocity_register_notifier() do {} while (0)
434 #define velocity_unregister_notifier() do {} while (0)
439 * Internal board variants. At the moment we have only one
442 static struct velocity_info_tbl chip_info_table[] = {
443 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
448 * Describe the PCI device identifiers that we support in this
449 * device driver. Used for hotplug autoloading.
452 static const struct pci_device_id velocity_id_table[] __devinitdata = {
453 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
457 MODULE_DEVICE_TABLE(pci, velocity_id_table);
460 * get_chip_name - identifier to name
461 * @id: chip identifier
463 * Given a chip identifier return a suitable description. Returns
464 * a pointer a static string valid while the driver is loaded.
467 static const char __devinit *get_chip_name(enum chip_type chip_id)
470 for (i = 0; chip_info_table[i].name != NULL; i++)
471 if (chip_info_table[i].chip_id == chip_id)
473 return chip_info_table[i].name;
477 * velocity_remove1 - device unplug
478 * @pdev: PCI device being removed
480 * Device unload callback. Called on an unplug or on module
481 * unload for each active device that is present. Disconnects
482 * the device from the network layer and frees all the resources
485 static void __devexit velocity_remove1(struct pci_dev *pdev)
487 struct net_device *dev = pci_get_drvdata(pdev);
488 struct velocity_info *vptr = netdev_priv(dev);
493 spin_lock_irqsave(&velocity_dev_list_lock, flags);
494 if (!list_empty(&velocity_dev_list))
495 list_del(&vptr->list);
496 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
498 unregister_netdev(dev);
499 iounmap(vptr->mac_regs);
500 pci_release_regions(pdev);
501 pci_disable_device(pdev);
502 pci_set_drvdata(pdev, NULL);
509 * velocity_set_int_opt - parser for integer options
510 * @opt: pointer to option value
511 * @val: value the user requested (or -1 for default)
512 * @min: lowest value allowed
513 * @max: highest value allowed
514 * @def: default value
515 * @name: property name
518 * Set an integer property in the module options. This function does
519 * all the verification and checking as well as reporting so that
520 * we don't duplicate code for each option.
523 static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max, int def, char *name, const char *devname)
527 else if (val < min || val > max) {
528 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
529 devname, name, min, max);
532 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
539 * velocity_set_bool_opt - parser for boolean options
540 * @opt: pointer to option value
541 * @val: value the user requested (or -1 for default)
542 * @def: default value (yes/no)
543 * @flag: numeric value to set for true.
544 * @name: property name
547 * Set a boolean property in the module options. This function does
548 * all the verification and checking as well as reporting so that
549 * we don't duplicate code for each option.
552 static void __devinit velocity_set_bool_opt(u32 *opt, int val, int def, u32 flag, char *name, const char *devname)
556 *opt |= (def ? flag : 0);
557 else if (val < 0 || val > 1) {
558 printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
560 *opt |= (def ? flag : 0);
562 printk(KERN_INFO "%s: set parameter %s to %s\n",
563 devname, name, val ? "TRUE" : "FALSE");
564 *opt |= (val ? flag : 0);
569 * velocity_get_options - set options on device
570 * @opts: option structure for the device
571 * @index: index of option to use in module options array
572 * @devname: device name
574 * Turn the module and command options into a single structure
575 * for the current device
578 static void __devinit velocity_get_options(struct velocity_opt *opts, int index, const char *devname)
581 velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
582 velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
583 velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
584 velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
586 velocity_set_bool_opt(&opts->flags, txcsum_offload[index], TX_CSUM_DEF, VELOCITY_FLAGS_TX_CSUM, "txcsum_offload", devname);
587 velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
588 velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
589 velocity_set_bool_opt(&opts->flags, ValPktLen[index], VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN, "ValPktLen", devname);
590 velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
591 velocity_set_int_opt((int *) &opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
592 velocity_set_int_opt((int *) &opts->int_works, int_works[index], INT_WORKS_MIN, INT_WORKS_MAX, INT_WORKS_DEF, "Interrupt service works", devname);
593 opts->numrx = (opts->numrx & ~3);
597 * velocity_init_cam_filter - initialise CAM
598 * @vptr: velocity to program
600 * Initialize the content addressable memory used for filters. Load
601 * appropriately according to the presence of VLAN
604 static void velocity_init_cam_filter(struct velocity_info *vptr)
606 struct mac_regs __iomem *regs = vptr->mac_regs;
608 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
609 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, ®s->MCFG);
610 WORD_REG_BITS_ON(MCFG_VIDFR, ®s->MCFG);
612 /* Disable all CAMs */
613 memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
614 memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
615 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
616 mac_set_cam_mask(regs, vptr->mCAMmask);
620 unsigned int vid, i = 0;
622 if (!vlan_group_get_device(vptr->vlgrp, 0))
623 WORD_REG_BITS_ON(MCFG_RTGOPT, ®s->MCFG);
625 for (vid = 1; (vid < VLAN_VID_MASK); vid++) {
626 if (vlan_group_get_device(vptr->vlgrp, vid)) {
627 mac_set_vlan_cam(regs, i, (u8 *) &vid);
628 vptr->vCAMmask[i / 8] |= 0x1 << (i % 8);
629 if (++i >= VCAM_SIZE)
633 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
637 static void velocity_vlan_rx_register(struct net_device *dev,
638 struct vlan_group *grp)
640 struct velocity_info *vptr = netdev_priv(dev);
645 static void velocity_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
647 struct velocity_info *vptr = netdev_priv(dev);
649 spin_lock_irq(&vptr->lock);
650 velocity_init_cam_filter(vptr);
651 spin_unlock_irq(&vptr->lock);
654 static void velocity_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
656 struct velocity_info *vptr = netdev_priv(dev);
658 spin_lock_irq(&vptr->lock);
659 vlan_group_set_device(vptr->vlgrp, vid, NULL);
660 velocity_init_cam_filter(vptr);
661 spin_unlock_irq(&vptr->lock);
664 static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
666 vptr->rx.dirty = vptr->rx.filled = vptr->rx.curr = 0;
670 * velocity_rx_reset - handle a receive reset
671 * @vptr: velocity we are resetting
673 * Reset the ownership and status for the receive ring side.
674 * Hand all the receive queue to the NIC.
677 static void velocity_rx_reset(struct velocity_info *vptr)
680 struct mac_regs __iomem *regs = vptr->mac_regs;
683 velocity_init_rx_ring_indexes(vptr);
686 * Init state, all RD entries belong to the NIC
688 for (i = 0; i < vptr->options.numrx; ++i)
689 vptr->rx.ring[i].rdesc0.len |= OWNED_BY_NIC;
691 writew(vptr->options.numrx, ®s->RBRDU);
692 writel(vptr->rx.pool_dma, ®s->RDBaseLo);
693 writew(0, ®s->RDIdx);
694 writew(vptr->options.numrx - 1, ®s->RDCSize);
698 * velocity_init_registers - initialise MAC registers
699 * @vptr: velocity to init
700 * @type: type of initialisation (hot or cold)
702 * Initialise the MAC on a reset or on first set up on the
706 static void velocity_init_registers(struct velocity_info *vptr,
707 enum velocity_init_type type)
709 struct mac_regs __iomem *regs = vptr->mac_regs;
715 case VELOCITY_INIT_RESET:
716 case VELOCITY_INIT_WOL:
718 netif_stop_queue(vptr->dev);
721 * Reset RX to prevent RX pointer not on the 4X location
723 velocity_rx_reset(vptr);
724 mac_rx_queue_run(regs);
725 mac_rx_queue_wake(regs);
727 mii_status = velocity_get_opt_media_mode(vptr);
728 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
729 velocity_print_link_status(vptr);
730 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
731 netif_wake_queue(vptr->dev);
734 enable_flow_control_ability(vptr);
737 writel(CR0_STOP, ®s->CR0Clr);
738 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
743 case VELOCITY_INIT_COLD:
748 velocity_soft_reset(vptr);
751 mac_eeprom_reload(regs);
752 for (i = 0; i < 6; i++)
753 writeb(vptr->dev->dev_addr[i], &(regs->PAR[i]));
756 * clear Pre_ACPI bit.
758 BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
759 mac_set_rx_thresh(regs, vptr->options.rx_thresh);
760 mac_set_dma_length(regs, vptr->options.DMA_length);
762 writeb(WOLCFG_SAM | WOLCFG_SAB, ®s->WOLCFGSet);
764 * Back off algorithm use original IEEE standard
766 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), ®s->CFGB);
771 velocity_init_cam_filter(vptr);
774 * Set packet filter: Receive directed and broadcast address
776 velocity_set_multi(vptr->dev);
779 * Enable MII auto-polling
781 enable_mii_autopoll(regs);
783 vptr->int_mask = INT_MASK_DEF;
785 writel(vptr->rx.pool_dma, ®s->RDBaseLo);
786 writew(vptr->options.numrx - 1, ®s->RDCSize);
787 mac_rx_queue_run(regs);
788 mac_rx_queue_wake(regs);
790 writew(vptr->options.numtx - 1, ®s->TDCSize);
792 for (i = 0; i < vptr->tx.numq; i++) {
793 writel(vptr->tx.pool_dma[i], ®s->TDBaseLo[i]);
794 mac_tx_queue_run(regs, i);
797 init_flow_control_register(vptr);
799 writel(CR0_STOP, ®s->CR0Clr);
800 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), ®s->CR0Set);
802 mii_status = velocity_get_opt_media_mode(vptr);
803 netif_stop_queue(vptr->dev);
805 mii_init(vptr, mii_status);
807 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
808 velocity_print_link_status(vptr);
809 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
810 netif_wake_queue(vptr->dev);
813 enable_flow_control_ability(vptr);
814 mac_hw_mibs_init(regs);
815 mac_write_int_mask(vptr->int_mask, regs);
822 * velocity_soft_reset - soft reset
823 * @vptr: velocity to reset
825 * Kick off a soft reset of the velocity adapter and then poll
826 * until the reset sequence has completed before returning.
829 static int velocity_soft_reset(struct velocity_info *vptr)
831 struct mac_regs __iomem *regs = vptr->mac_regs;
834 writel(CR0_SFRST, ®s->CR0Set);
836 for (i = 0; i < W_MAX_TIMEOUT; i++) {
838 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, ®s->CR0Set))
842 if (i == W_MAX_TIMEOUT) {
843 writel(CR0_FORSRST, ®s->CR0Set);
844 /* FIXME: PCI POSTING */
851 static const struct net_device_ops velocity_netdev_ops = {
852 .ndo_open = velocity_open,
853 .ndo_stop = velocity_close,
854 .ndo_start_xmit = velocity_xmit,
855 .ndo_get_stats = velocity_get_stats,
856 .ndo_validate_addr = eth_validate_addr,
857 .ndo_set_mac_address = eth_mac_addr,
858 .ndo_set_multicast_list = velocity_set_multi,
859 .ndo_change_mtu = velocity_change_mtu,
860 .ndo_do_ioctl = velocity_ioctl,
861 .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
862 .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
863 .ndo_vlan_rx_register = velocity_vlan_rx_register,
867 * velocity_found1 - set up discovered velocity card
869 * @ent: PCI device table entry that matched
871 * Configure a discovered adapter from scratch. Return a negative
872 * errno error code on failure paths.
875 static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
877 static int first = 1;
878 struct net_device *dev;
880 const char *drv_string;
881 const struct velocity_info_tbl *info = &chip_info_table[ent->driver_data];
882 struct velocity_info *vptr;
883 struct mac_regs __iomem *regs;
886 /* FIXME: this driver, like almost all other ethernet drivers,
887 * can support more than MAX_UNITS.
889 if (velocity_nics >= MAX_UNITS) {
890 dev_notice(&pdev->dev, "already found %d NICs.\n",
895 dev = alloc_etherdev(sizeof(struct velocity_info));
897 dev_err(&pdev->dev, "allocate net device failed.\n");
901 /* Chain it all together */
903 SET_NETDEV_DEV(dev, &pdev->dev);
904 vptr = netdev_priv(dev);
908 printk(KERN_INFO "%s Ver. %s\n",
909 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
910 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
911 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
915 velocity_init_info(pdev, vptr, info);
919 dev->irq = pdev->irq;
921 ret = pci_enable_device(pdev);
925 ret = velocity_get_pci_info(vptr, pdev);
927 /* error message already printed */
931 ret = pci_request_regions(pdev, VELOCITY_NAME);
933 dev_err(&pdev->dev, "No PCI resources.\n");
937 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
940 goto err_release_res;
943 vptr->mac_regs = regs;
947 dev->base_addr = vptr->ioaddr;
949 for (i = 0; i < 6; i++)
950 dev->dev_addr[i] = readb(®s->PAR[i]);
953 drv_string = dev_driver_string(&pdev->dev);
955 velocity_get_options(&vptr->options, velocity_nics, drv_string);
958 * Mask out the options cannot be set to the chip
961 vptr->options.flags &= info->flags;
964 * Enable the chip specified capbilities
967 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
969 vptr->wol_opts = vptr->options.wol_opts;
970 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
972 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
974 dev->irq = pdev->irq;
975 dev->netdev_ops = &velocity_netdev_ops;
976 dev->ethtool_ops = &velocity_ethtool_ops;
978 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER |
981 if (vptr->flags & VELOCITY_FLAGS_TX_CSUM)
982 dev->features |= NETIF_F_IP_CSUM;
984 ret = register_netdev(dev);
988 if (!velocity_get_link(dev)) {
989 netif_carrier_off(dev);
990 vptr->mii_status |= VELOCITY_LINK_FAIL;
993 velocity_print_info(vptr);
994 pci_set_drvdata(pdev, dev);
996 /* and leave the chip powered down */
998 pci_set_power_state(pdev, PCI_D3hot);
1001 unsigned long flags;
1003 spin_lock_irqsave(&velocity_dev_list_lock, flags);
1004 list_add(&vptr->list, &velocity_dev_list);
1005 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
1015 pci_release_regions(pdev);
1017 pci_disable_device(pdev);
1024 * velocity_print_info - per driver data
1027 * Print per driver data as the kernel driver finds Velocity
1031 static void __devinit velocity_print_info(struct velocity_info *vptr)
1033 struct net_device *dev = vptr->dev;
1035 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
1036 printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
1038 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1039 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1043 * velocity_init_info - init private data
1045 * @vptr: Velocity info
1048 * Set up the initial velocity_info struct for the device that has been
1052 static void __devinit velocity_init_info(struct pci_dev *pdev,
1053 struct velocity_info *vptr,
1054 const struct velocity_info_tbl *info)
1056 memset(vptr, 0, sizeof(struct velocity_info));
1059 vptr->chip_id = info->chip_id;
1060 vptr->tx.numq = info->txqueue;
1061 vptr->multicast_limit = MCAM_SIZE;
1062 spin_lock_init(&vptr->lock);
1063 INIT_LIST_HEAD(&vptr->list);
1067 * velocity_get_pci_info - retrieve PCI info for device
1068 * @vptr: velocity device
1069 * @pdev: PCI device it matches
1071 * Retrieve the PCI configuration space data that interests us from
1072 * the kernel PCI layer
1075 static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
1077 vptr->rev_id = pdev->revision;
1079 pci_set_master(pdev);
1081 vptr->ioaddr = pci_resource_start(pdev, 0);
1082 vptr->memaddr = pci_resource_start(pdev, 1);
1084 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
1086 "region #0 is not an I/O resource, aborting.\n");
1090 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
1092 "region #1 is an I/O resource, aborting.\n");
1096 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
1097 dev_err(&pdev->dev, "region #1 is too small.\n");
1106 * velocity_init_dma_rings - set up DMA rings
1107 * @vptr: Velocity to set up
1109 * Allocate PCI mapped DMA rings for the receive and transmit layer
1113 static int velocity_init_dma_rings(struct velocity_info *vptr)
1115 struct velocity_opt *opt = &vptr->options;
1116 const unsigned int rx_ring_size = opt->numrx * sizeof(struct rx_desc);
1117 const unsigned int tx_ring_size = opt->numtx * sizeof(struct tx_desc);
1118 struct pci_dev *pdev = vptr->pdev;
1119 dma_addr_t pool_dma;
1124 * Allocate all RD/TD rings a single pool.
1126 * pci_alloc_consistent() fulfills the requirement for 64 bytes
1129 pool = pci_alloc_consistent(pdev, tx_ring_size * vptr->tx.numq +
1130 rx_ring_size, &pool_dma);
1132 dev_err(&pdev->dev, "%s : DMA memory allocation failed.\n",
1137 vptr->rx.ring = pool;
1138 vptr->rx.pool_dma = pool_dma;
1140 pool += rx_ring_size;
1141 pool_dma += rx_ring_size;
1143 for (i = 0; i < vptr->tx.numq; i++) {
1144 vptr->tx.rings[i] = pool;
1145 vptr->tx.pool_dma[i] = pool_dma;
1146 pool += tx_ring_size;
1147 pool_dma += tx_ring_size;
1154 * velocity_free_dma_rings - free PCI ring pointers
1155 * @vptr: Velocity to free from
1157 * Clean up the PCI ring buffers allocated to this velocity.
1160 static void velocity_free_dma_rings(struct velocity_info *vptr)
1162 const int size = vptr->options.numrx * sizeof(struct rx_desc) +
1163 vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;
1165 pci_free_consistent(vptr->pdev, size, vptr->rx.ring, vptr->rx.pool_dma);
1168 static void velocity_give_many_rx_descs(struct velocity_info *vptr)
1170 struct mac_regs __iomem *regs = vptr->mac_regs;
1171 int avail, dirty, unusable;
1174 * RD number must be equal to 4X per hardware spec
1175 * (programming guide rev 1.20, p.13)
1177 if (vptr->rx.filled < 4)
1182 unusable = vptr->rx.filled & 0x0003;
1183 dirty = vptr->rx.dirty - unusable;
1184 for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
1185 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1186 vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
1189 writew(vptr->rx.filled & 0xfffc, ®s->RBRDU);
1190 vptr->rx.filled = unusable;
1193 static int velocity_rx_refill(struct velocity_info *vptr)
1195 int dirty = vptr->rx.dirty, done = 0;
1198 struct rx_desc *rd = vptr->rx.ring + dirty;
1200 /* Fine for an all zero Rx desc at init time as well */
1201 if (rd->rdesc0.len & OWNED_BY_NIC)
1204 if (!vptr->rx.info[dirty].skb) {
1205 if (velocity_alloc_rx_buf(vptr, dirty) < 0)
1209 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
1210 } while (dirty != vptr->rx.curr);
1213 vptr->rx.dirty = dirty;
1214 vptr->rx.filled += done;
1220 static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
1222 vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
1226 * velocity_init_rd_ring - set up receive ring
1227 * @vptr: velocity to configure
1229 * Allocate and set up the receive buffers for each ring slot and
1230 * assign them to the network adapter.
1233 static int velocity_init_rd_ring(struct velocity_info *vptr)
1237 vptr->rx.info = kcalloc(vptr->options.numrx,
1238 sizeof(struct velocity_rd_info), GFP_KERNEL);
1242 velocity_init_rx_ring_indexes(vptr);
1244 if (velocity_rx_refill(vptr) != vptr->options.numrx) {
1245 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1246 "%s: failed to allocate RX buffer.\n", vptr->dev->name);
1247 velocity_free_rd_ring(vptr);
1257 * velocity_free_rd_ring - free receive ring
1258 * @vptr: velocity to clean up
1260 * Free the receive buffers for each ring slot and any
1261 * attached socket buffers that need to go away.
1264 static void velocity_free_rd_ring(struct velocity_info *vptr)
1268 if (vptr->rx.info == NULL)
1271 for (i = 0; i < vptr->options.numrx; i++) {
1272 struct velocity_rd_info *rd_info = &(vptr->rx.info[i]);
1273 struct rx_desc *rd = vptr->rx.ring + i;
1275 memset(rd, 0, sizeof(*rd));
1279 pci_unmap_single(vptr->pdev, rd_info->skb_dma, vptr->rx.buf_sz,
1280 PCI_DMA_FROMDEVICE);
1281 rd_info->skb_dma = 0;
1283 dev_kfree_skb(rd_info->skb);
1284 rd_info->skb = NULL;
1287 kfree(vptr->rx.info);
1288 vptr->rx.info = NULL;
1292 * velocity_init_td_ring - set up transmit ring
1295 * Set up the transmit ring and chain the ring pointers together.
1296 * Returns zero on success or a negative posix errno code for
1300 static int velocity_init_td_ring(struct velocity_info *vptr)
1305 /* Init the TD ring entries */
1306 for (j = 0; j < vptr->tx.numq; j++) {
1307 curr = vptr->tx.pool_dma[j];
1309 vptr->tx.infos[j] = kcalloc(vptr->options.numtx,
1310 sizeof(struct velocity_td_info),
1312 if (!vptr->tx.infos[j]) {
1314 kfree(vptr->tx.infos[j]);
1318 vptr->tx.tail[j] = vptr->tx.curr[j] = vptr->tx.used[j] = 0;
1324 * FIXME: could we merge this with velocity_free_tx_buf ?
1327 static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1330 struct velocity_td_info *td_info = &(vptr->tx.infos[q][n]);
1333 if (td_info == NULL)
1337 for (i = 0; i < td_info->nskb_dma; i++) {
1338 if (td_info->skb_dma[i]) {
1339 pci_unmap_single(vptr->pdev, td_info->skb_dma[i],
1340 td_info->skb->len, PCI_DMA_TODEVICE);
1341 td_info->skb_dma[i] = 0;
1344 dev_kfree_skb(td_info->skb);
1345 td_info->skb = NULL;
1350 * velocity_free_td_ring - free td ring
1353 * Free up the transmit ring for this particular velocity adapter.
1354 * We free the ring contents but not the ring itself.
1357 static void velocity_free_td_ring(struct velocity_info *vptr)
1361 for (j = 0; j < vptr->tx.numq; j++) {
1362 if (vptr->tx.infos[j] == NULL)
1364 for (i = 0; i < vptr->options.numtx; i++)
1365 velocity_free_td_ring_entry(vptr, j, i);
1367 kfree(vptr->tx.infos[j]);
1368 vptr->tx.infos[j] = NULL;
1373 * velocity_rx_srv - service RX interrupt
1375 * @status: adapter status (unused)
1377 * Walk the receive ring of the velocity adapter and remove
1378 * any received packets from the receive queue. Hand the ring
1379 * slots back to the adapter for reuse.
1382 static int velocity_rx_srv(struct velocity_info *vptr, int status)
1384 struct net_device_stats *stats = &vptr->dev->stats;
1385 int rd_curr = vptr->rx.curr;
1389 struct rx_desc *rd = vptr->rx.ring + rd_curr;
1391 if (!vptr->rx.info[rd_curr].skb)
1394 if (rd->rdesc0.len & OWNED_BY_NIC)
1400 * Don't drop CE or RL error frame although RXOK is off
1402 if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
1403 if (velocity_receive_frame(vptr, rd_curr) < 0)
1404 stats->rx_dropped++;
1406 if (rd->rdesc0.RSR & RSR_CRC)
1407 stats->rx_crc_errors++;
1408 if (rd->rdesc0.RSR & RSR_FAE)
1409 stats->rx_frame_errors++;
1411 stats->rx_dropped++;
1414 rd->size |= RX_INTEN;
1417 if (rd_curr >= vptr->options.numrx)
1419 } while (++works <= 15);
1421 vptr->rx.curr = rd_curr;
1423 if ((works > 0) && (velocity_rx_refill(vptr) > 0))
1424 velocity_give_many_rx_descs(vptr);
1431 * velocity_rx_csum - checksum process
1432 * @rd: receive packet descriptor
1433 * @skb: network layer packet buffer
1435 * Process the status bits for the received packet and determine
1436 * if the checksum was computed and verified by the hardware
1439 static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1441 skb->ip_summed = CHECKSUM_NONE;
1443 if (rd->rdesc1.CSM & CSM_IPKT) {
1444 if (rd->rdesc1.CSM & CSM_IPOK) {
1445 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
1446 (rd->rdesc1.CSM & CSM_UDPKT)) {
1447 if (!(rd->rdesc1.CSM & CSM_TUPOK))
1450 skb->ip_summed = CHECKSUM_UNNECESSARY;
1456 * velocity_rx_copy - in place Rx copy for small packets
1457 * @rx_skb: network layer packet buffer candidate
1458 * @pkt_size: received data size
1459 * @rd: receive packet descriptor
1460 * @dev: network device
1462 * Replace the current skb that is scheduled for Rx processing by a
1463 * shorter, immediatly allocated skb, if the received packet is small
1464 * enough. This function returns a negative value if the received
1465 * packet is too big or if memory is exhausted.
1467 static int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
1468 struct velocity_info *vptr)
1471 if (pkt_size < rx_copybreak) {
1472 struct sk_buff *new_skb;
1474 new_skb = netdev_alloc_skb(vptr->dev, pkt_size + 2);
1476 new_skb->ip_summed = rx_skb[0]->ip_summed;
1477 skb_reserve(new_skb, 2);
1478 skb_copy_from_linear_data(*rx_skb, new_skb->data, pkt_size);
1488 * velocity_iph_realign - IP header alignment
1489 * @vptr: velocity we are handling
1490 * @skb: network layer packet buffer
1491 * @pkt_size: received data size
1493 * Align IP header on a 2 bytes boundary. This behavior can be
1494 * configured by the user.
1496 static inline void velocity_iph_realign(struct velocity_info *vptr,
1497 struct sk_buff *skb, int pkt_size)
1499 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
1500 memmove(skb->data + 2, skb->data, pkt_size);
1501 skb_reserve(skb, 2);
1506 * velocity_receive_frame - received packet processor
1507 * @vptr: velocity we are handling
1510 * A packet has arrived. We process the packet and if appropriate
1511 * pass the frame up the network stack
1514 static int velocity_receive_frame(struct velocity_info *vptr, int idx)
1516 void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int);
1517 struct net_device_stats *stats = &vptr->dev->stats;
1518 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
1519 struct rx_desc *rd = &(vptr->rx.ring[idx]);
1520 int pkt_len = le16_to_cpu(rd->rdesc0.len) & 0x3fff;
1521 struct sk_buff *skb;
1523 if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
1524 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->dev->name);
1525 stats->rx_length_errors++;
1529 if (rd->rdesc0.RSR & RSR_MAR)
1534 pci_dma_sync_single_for_cpu(vptr->pdev, rd_info->skb_dma,
1535 vptr->rx.buf_sz, PCI_DMA_FROMDEVICE);
1538 * Drop frame not meeting IEEE 802.3
1541 if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
1542 if (rd->rdesc0.RSR & RSR_RL) {
1543 stats->rx_length_errors++;
1548 pci_action = pci_dma_sync_single_for_device;
1550 velocity_rx_csum(rd, skb);
1552 if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
1553 velocity_iph_realign(vptr, skb, pkt_len);
1554 pci_action = pci_unmap_single;
1555 rd_info->skb = NULL;
1558 pci_action(vptr->pdev, rd_info->skb_dma, vptr->rx.buf_sz,
1559 PCI_DMA_FROMDEVICE);
1561 skb_put(skb, pkt_len - 4);
1562 skb->protocol = eth_type_trans(skb, vptr->dev);
1564 if (vptr->vlgrp && (rd->rdesc0.RSR & RSR_DETAG)) {
1565 vlan_hwaccel_rx(skb, vptr->vlgrp,
1566 swab16(le16_to_cpu(rd->rdesc1.PQTAG)));
1570 stats->rx_bytes += pkt_len;
1576 * velocity_alloc_rx_buf - allocate aligned receive buffer
1580 * Allocate a new full sized buffer for the reception of a frame and
1581 * map it into PCI space for the hardware to use. The hardware
1582 * requires *64* byte alignment of the buffer which makes life
1583 * less fun than would be ideal.
1586 static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1588 struct rx_desc *rd = &(vptr->rx.ring[idx]);
1589 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
1591 rd_info->skb = dev_alloc_skb(vptr->rx.buf_sz + 64);
1592 if (rd_info->skb == NULL)
1596 * Do the gymnastics to get the buffer head for data at
1599 skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
1600 rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data,
1601 vptr->rx.buf_sz, PCI_DMA_FROMDEVICE);
1604 * Fill in the descriptor to match
1607 *((u32 *) & (rd->rdesc0)) = 0;
1608 rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
1609 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1615 * tx_srv - transmit interrupt service
1619 * Scan the queues looking for transmitted packets that
1620 * we can complete and clean up. Update any statistics as
1624 static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
1631 struct velocity_td_info *tdinfo;
1632 struct net_device_stats *stats = &vptr->dev->stats;
1634 for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
1635 for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
1636 idx = (idx + 1) % vptr->options.numtx) {
1641 td = &(vptr->tx.rings[qnum][idx]);
1642 tdinfo = &(vptr->tx.infos[qnum][idx]);
1644 if (td->tdesc0.len & OWNED_BY_NIC)
1650 if (td->tdesc0.TSR & TSR0_TERR) {
1652 stats->tx_dropped++;
1653 if (td->tdesc0.TSR & TSR0_CDH)
1654 stats->tx_heartbeat_errors++;
1655 if (td->tdesc0.TSR & TSR0_CRS)
1656 stats->tx_carrier_errors++;
1657 if (td->tdesc0.TSR & TSR0_ABT)
1658 stats->tx_aborted_errors++;
1659 if (td->tdesc0.TSR & TSR0_OWC)
1660 stats->tx_window_errors++;
1662 stats->tx_packets++;
1663 stats->tx_bytes += tdinfo->skb->len;
1665 velocity_free_tx_buf(vptr, tdinfo);
1666 vptr->tx.used[qnum]--;
1668 vptr->tx.tail[qnum] = idx;
1670 if (AVAIL_TD(vptr, qnum) < 1)
1674 * Look to see if we should kick the transmit network
1675 * layer for more work.
1677 if (netif_queue_stopped(vptr->dev) && (full == 0)
1678 && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1679 netif_wake_queue(vptr->dev);
1685 * velocity_print_link_status - link status reporting
1686 * @vptr: velocity to report on
1688 * Turn the link status of the velocity card into a kernel log
1689 * description of the new link state, detailing speed and duplex
1693 static void velocity_print_link_status(struct velocity_info *vptr)
1696 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1697 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
1698 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1699 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->dev->name);
1701 if (vptr->mii_status & VELOCITY_SPEED_1000)
1702 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1703 else if (vptr->mii_status & VELOCITY_SPEED_100)
1704 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1706 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1708 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1709 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1711 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1713 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
1714 switch (vptr->options.spd_dpx) {
1715 case SPD_DPX_100_HALF:
1716 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1718 case SPD_DPX_100_FULL:
1719 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1721 case SPD_DPX_10_HALF:
1722 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1724 case SPD_DPX_10_FULL:
1725 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1734 * velocity_error - handle error from controller
1736 * @status: card status
1738 * Process an error report from the hardware and attempt to recover
1739 * the card itself. At the moment we cannot recover from some
1740 * theoretically impossible errors but this could be fixed using
1741 * the pci_device_failed logic to bounce the hardware
1745 static void velocity_error(struct velocity_info *vptr, int status)
1748 if (status & ISR_TXSTLI) {
1749 struct mac_regs __iomem *regs = vptr->mac_regs;
1751 printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(®s->TDIdx[0]));
1752 BYTE_REG_BITS_ON(TXESR_TDSTR, ®s->TXESR);
1753 writew(TRDCSR_RUN, ®s->TDCSRClr);
1754 netif_stop_queue(vptr->dev);
1756 /* FIXME: port over the pci_device_failed code and use it
1760 if (status & ISR_SRCI) {
1761 struct mac_regs __iomem *regs = vptr->mac_regs;
1764 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1765 vptr->mii_status = check_connection_type(regs);
1768 * If it is a 3119, disable frame bursting in
1769 * halfduplex mode and enable it in fullduplex
1772 if (vptr->rev_id < REV_ID_VT3216_A0) {
1773 if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
1774 BYTE_REG_BITS_ON(TCR_TB2BDIS, ®s->TCR);
1776 BYTE_REG_BITS_OFF(TCR_TB2BDIS, ®s->TCR);
1779 * Only enable CD heart beat counter in 10HD mode
1781 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10))
1782 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, ®s->TESTCFG);
1784 BYTE_REG_BITS_ON(TESTCFG_HBDIS, ®s->TESTCFG);
1787 * Get link status from PHYSR0
1789 linked = readb(®s->PHYSR0) & PHYSR0_LINKGD;
1792 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1793 netif_carrier_on(vptr->dev);
1795 vptr->mii_status |= VELOCITY_LINK_FAIL;
1796 netif_carrier_off(vptr->dev);
1799 velocity_print_link_status(vptr);
1800 enable_flow_control_ability(vptr);
1803 * Re-enable auto-polling because SRCI will disable
1807 enable_mii_autopoll(regs);
1809 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1810 netif_stop_queue(vptr->dev);
1812 netif_wake_queue(vptr->dev);
1815 if (status & ISR_MIBFI)
1816 velocity_update_hw_mibs(vptr);
1817 if (status & ISR_LSTEI)
1818 mac_rx_queue_wake(vptr->mac_regs);
1822 * velocity_free_tx_buf - free transmit buffer
1826 * Release an transmit buffer. If the buffer was preallocated then
1827 * recycle it, if not then unmap the buffer.
1830 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
1832 struct sk_buff *skb = tdinfo->skb;
1837 * Don't unmap the pre-allocated tx_bufs
1839 if (tdinfo->skb_dma) {
1841 pktlen = max_t(unsigned int, skb->len, ETH_ZLEN);
1842 for (i = 0; i < tdinfo->nskb_dma; i++) {
1843 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], pktlen, PCI_DMA_TODEVICE);
1844 tdinfo->skb_dma[i] = 0;
1847 dev_kfree_skb_irq(skb);
1851 static int velocity_init_rings(struct velocity_info *vptr, int mtu)
1855 velocity_set_rxbufsize(vptr, mtu);
1857 ret = velocity_init_dma_rings(vptr);
1861 ret = velocity_init_rd_ring(vptr);
1863 goto err_free_dma_rings_0;
1865 ret = velocity_init_td_ring(vptr);
1867 goto err_free_rd_ring_1;
1872 velocity_free_rd_ring(vptr);
1873 err_free_dma_rings_0:
1874 velocity_free_dma_rings(vptr);
1878 static void velocity_free_rings(struct velocity_info *vptr)
1880 velocity_free_td_ring(vptr);
1881 velocity_free_rd_ring(vptr);
1882 velocity_free_dma_rings(vptr);
1886 * velocity_open - interface activation callback
1887 * @dev: network layer device to open
1889 * Called when the network layer brings the interface up. Returns
1890 * a negative posix error code on failure, or zero on success.
1892 * All the ring allocation and set up is done on open for this
1893 * adapter to minimise memory usage when inactive
1896 static int velocity_open(struct net_device *dev)
1898 struct velocity_info *vptr = netdev_priv(dev);
1901 ret = velocity_init_rings(vptr, dev->mtu);
1905 /* Ensure chip is running */
1906 pci_set_power_state(vptr->pdev, PCI_D0);
1908 velocity_give_many_rx_descs(vptr);
1910 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1912 ret = request_irq(vptr->pdev->irq, &velocity_intr, IRQF_SHARED,
1915 /* Power down the chip */
1916 pci_set_power_state(vptr->pdev, PCI_D3hot);
1917 velocity_free_rings(vptr);
1921 mac_enable_int(vptr->mac_regs);
1922 netif_start_queue(dev);
1923 vptr->flags |= VELOCITY_FLAGS_OPENED;
1929 * velocity_change_mtu - MTU change callback
1930 * @dev: network device
1931 * @new_mtu: desired MTU
1933 * Handle requests from the networking layer for MTU change on
1934 * this interface. It gets called on a change by the network layer.
1935 * Return zero for success or negative posix error code.
1938 static int velocity_change_mtu(struct net_device *dev, int new_mtu)
1940 struct velocity_info *vptr = netdev_priv(dev);
1943 if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
1944 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
1950 if (!netif_running(dev)) {
1955 if (dev->mtu != new_mtu) {
1956 struct velocity_info *tmp_vptr;
1957 unsigned long flags;
1961 tmp_vptr = kzalloc(sizeof(*tmp_vptr), GFP_KERNEL);
1967 tmp_vptr->dev = dev;
1968 tmp_vptr->pdev = vptr->pdev;
1969 tmp_vptr->options = vptr->options;
1970 tmp_vptr->tx.numq = vptr->tx.numq;
1972 ret = velocity_init_rings(tmp_vptr, new_mtu);
1974 goto out_free_tmp_vptr_1;
1976 spin_lock_irqsave(&vptr->lock, flags);
1978 netif_stop_queue(dev);
1979 velocity_shutdown(vptr);
1984 vptr->rx = tmp_vptr->rx;
1985 vptr->tx = tmp_vptr->tx;
1992 velocity_give_many_rx_descs(vptr);
1994 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1996 mac_enable_int(vptr->mac_regs);
1997 netif_start_queue(dev);
1999 spin_unlock_irqrestore(&vptr->lock, flags);
2001 velocity_free_rings(tmp_vptr);
2003 out_free_tmp_vptr_1:
2011 * velocity_shutdown - shut down the chip
2012 * @vptr: velocity to deactivate
2014 * Shuts down the internal operations of the velocity and
2015 * disables interrupts, autopolling, transmit and receive
2018 static void velocity_shutdown(struct velocity_info *vptr)
2020 struct mac_regs __iomem *regs = vptr->mac_regs;
2021 mac_disable_int(regs);
2022 writel(CR0_STOP, ®s->CR0Set);
2023 writew(0xFFFF, ®s->TDCSRClr);
2024 writeb(0xFF, ®s->RDCSRClr);
2025 safe_disable_mii_autopoll(regs);
2026 mac_clear_isr(regs);
2030 * velocity_close - close adapter callback
2031 * @dev: network device
2033 * Callback from the network layer when the velocity is being
2034 * deactivated by the network layer
2037 static int velocity_close(struct net_device *dev)
2039 struct velocity_info *vptr = netdev_priv(dev);
2041 netif_stop_queue(dev);
2042 velocity_shutdown(vptr);
2044 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
2045 velocity_get_ip(vptr);
2047 free_irq(dev->irq, dev);
2049 /* Power down the chip */
2050 pci_set_power_state(vptr->pdev, PCI_D3hot);
2052 velocity_free_rings(vptr);
2054 vptr->flags &= (~VELOCITY_FLAGS_OPENED);
2059 * velocity_xmit - transmit packet callback
2060 * @skb: buffer to transmit
2061 * @dev: network device
2063 * Called by the networ layer to request a packet is queued to
2064 * the velocity. Returns zero on success.
2067 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
2069 struct velocity_info *vptr = netdev_priv(dev);
2071 struct tx_desc *td_ptr;
2072 struct velocity_td_info *tdinfo;
2073 unsigned long flags;
2079 if (skb_padto(skb, ETH_ZLEN))
2081 pktlen = max_t(unsigned int, skb->len, ETH_ZLEN);
2083 len = cpu_to_le16(pktlen);
2085 spin_lock_irqsave(&vptr->lock, flags);
2087 index = vptr->tx.curr[qnum];
2088 td_ptr = &(vptr->tx.rings[qnum][index]);
2089 tdinfo = &(vptr->tx.infos[qnum][index]);
2091 td_ptr->tdesc1.TCR = TCR0_TIC;
2092 td_ptr->td_buf[0].size &= ~TD_QUEUE;
2095 * Map the linear network buffer into PCI space and
2096 * add it to the transmit ring.
2099 tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
2100 td_ptr->tdesc0.len = len;
2101 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
2102 td_ptr->td_buf[0].pa_high = 0;
2103 td_ptr->td_buf[0].size = len;
2104 tdinfo->nskb_dma = 1;
2106 td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;
2108 if (vptr->vlgrp && vlan_tx_tag_present(skb)) {
2109 td_ptr->tdesc1.vlan = cpu_to_le16(vlan_tx_tag_get(skb));
2110 td_ptr->tdesc1.TCR |= TCR0_VETAG;
2114 * Handle hardware checksum
2116 if ((vptr->flags & VELOCITY_FLAGS_TX_CSUM)
2117 && (skb->ip_summed == CHECKSUM_PARTIAL)) {
2118 const struct iphdr *ip = ip_hdr(skb);
2119 if (ip->protocol == IPPROTO_TCP)
2120 td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2121 else if (ip->protocol == IPPROTO_UDP)
2122 td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2123 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2127 int prev = index - 1;
2130 prev = vptr->options.numtx - 1;
2131 td_ptr->tdesc0.len |= OWNED_BY_NIC;
2132 vptr->tx.used[qnum]++;
2133 vptr->tx.curr[qnum] = (index + 1) % vptr->options.numtx;
2135 if (AVAIL_TD(vptr, qnum) < 1)
2136 netif_stop_queue(dev);
2138 td_ptr = &(vptr->tx.rings[qnum][prev]);
2139 td_ptr->td_buf[0].size |= TD_QUEUE;
2140 mac_tx_queue_wake(vptr->mac_regs, qnum);
2142 dev->trans_start = jiffies;
2143 spin_unlock_irqrestore(&vptr->lock, flags);
2145 return NETDEV_TX_OK;
2149 * velocity_intr - interrupt callback
2150 * @irq: interrupt number
2151 * @dev_instance: interrupting device
2153 * Called whenever an interrupt is generated by the velocity
2154 * adapter IRQ line. We may not be the source of the interrupt
2155 * and need to identify initially if we are, and if not exit as
2156 * efficiently as possible.
2159 static irqreturn_t velocity_intr(int irq, void *dev_instance)
2161 struct net_device *dev = dev_instance;
2162 struct velocity_info *vptr = netdev_priv(dev);
2167 spin_lock(&vptr->lock);
2168 isr_status = mac_read_isr(vptr->mac_regs);
2171 if (isr_status == 0) {
2172 spin_unlock(&vptr->lock);
2176 mac_disable_int(vptr->mac_regs);
2179 * Keep processing the ISR until we have completed
2180 * processing and the isr_status becomes zero
2183 while (isr_status != 0) {
2184 mac_write_isr(vptr->mac_regs, isr_status);
2185 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2186 velocity_error(vptr, isr_status);
2187 if (isr_status & (ISR_PRXI | ISR_PPRXI))
2188 max_count += velocity_rx_srv(vptr, isr_status);
2189 if (isr_status & (ISR_PTXI | ISR_PPTXI))
2190 max_count += velocity_tx_srv(vptr, isr_status);
2191 isr_status = mac_read_isr(vptr->mac_regs);
2192 if (max_count > vptr->options.int_works) {
2193 printk(KERN_WARNING "%s: excessive work at interrupt.\n",
2198 spin_unlock(&vptr->lock);
2199 mac_enable_int(vptr->mac_regs);
2206 * velocity_set_multi - filter list change callback
2207 * @dev: network device
2209 * Called by the network layer when the filter lists need to change
2210 * for a velocity adapter. Reload the CAMs with the new address
2214 static void velocity_set_multi(struct net_device *dev)
2216 struct velocity_info *vptr = netdev_priv(dev);
2217 struct mac_regs __iomem *regs = vptr->mac_regs;
2220 struct dev_mc_list *mclist;
2222 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2223 writel(0xffffffff, ®s->MARCAM[0]);
2224 writel(0xffffffff, ®s->MARCAM[4]);
2225 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
2226 } else if ((dev->mc_count > vptr->multicast_limit)
2227 || (dev->flags & IFF_ALLMULTI)) {
2228 writel(0xffffffff, ®s->MARCAM[0]);
2229 writel(0xffffffff, ®s->MARCAM[4]);
2230 rx_mode = (RCR_AM | RCR_AB);
2232 int offset = MCAM_SIZE - vptr->multicast_limit;
2233 mac_get_cam_mask(regs, vptr->mCAMmask);
2235 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) {
2236 mac_set_cam(regs, i + offset, mclist->dmi_addr);
2237 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
2240 mac_set_cam_mask(regs, vptr->mCAMmask);
2241 rx_mode = RCR_AM | RCR_AB | RCR_AP;
2243 if (dev->mtu > 1500)
2246 BYTE_REG_BITS_ON(rx_mode, ®s->RCR);
2251 * velocity_get_status - statistics callback
2252 * @dev: network device
2254 * Callback from the network layer to allow driver statistics
2255 * to be resynchronized with hardware collected state. In the
2256 * case of the velocity we need to pull the MIB counters from
2257 * the hardware into the counters before letting the network
2258 * layer display them.
2261 static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2263 struct velocity_info *vptr = netdev_priv(dev);
2265 /* If the hardware is down, don't touch MII */
2266 if (!netif_running(dev))
2269 spin_lock_irq(&vptr->lock);
2270 velocity_update_hw_mibs(vptr);
2271 spin_unlock_irq(&vptr->lock);
2273 dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2274 dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2275 dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2277 // unsigned long rx_dropped; /* no space in linux buffers */
2278 dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2279 /* detailed rx_errors: */
2280 // unsigned long rx_length_errors;
2281 // unsigned long rx_over_errors; /* receiver ring buff overflow */
2282 dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2283 // unsigned long rx_frame_errors; /* recv'd frame alignment error */
2284 // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2285 // unsigned long rx_missed_errors; /* receiver missed packet */
2287 /* detailed tx_errors */
2288 // unsigned long tx_fifo_errors;
2295 * velocity_ioctl - ioctl entry point
2296 * @dev: network device
2297 * @rq: interface request ioctl
2298 * @cmd: command code
2300 * Called when the user issues an ioctl request to the network
2301 * device in question. The velocity interface supports MII.
2304 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2306 struct velocity_info *vptr = netdev_priv(dev);
2309 /* If we are asked for information and the device is power
2310 saving then we need to bring the device back up to talk to it */
2312 if (!netif_running(dev))
2313 pci_set_power_state(vptr->pdev, PCI_D0);
2316 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2317 case SIOCGMIIREG: /* Read MII PHY register. */
2318 case SIOCSMIIREG: /* Write to MII PHY register. */
2319 ret = velocity_mii_ioctl(dev, rq, cmd);
2325 if (!netif_running(dev))
2326 pci_set_power_state(vptr->pdev, PCI_D3hot);
2333 * Definition for our device driver. The PCI layer interface
2334 * uses this to handle all our card discover and plugging
2337 static struct pci_driver velocity_driver = {
2338 .name = VELOCITY_NAME,
2339 .id_table = velocity_id_table,
2340 .probe = velocity_found1,
2341 .remove = __devexit_p(velocity_remove1),
2343 .suspend = velocity_suspend,
2344 .resume = velocity_resume,
2349 * velocity_init_module - load time function
2351 * Called when the velocity module is loaded. The PCI driver
2352 * is registered with the PCI layer, and in turn will call
2353 * the probe functions for each velocity adapter installed
2357 static int __init velocity_init_module(void)
2361 velocity_register_notifier();
2362 ret = pci_register_driver(&velocity_driver);
2364 velocity_unregister_notifier();
2369 * velocity_cleanup - module unload
2371 * When the velocity hardware is unloaded this function is called.
2372 * It will clean up the notifiers and the unregister the PCI
2373 * driver interface for this hardware. This in turn cleans up
2374 * all discovered interfaces before returning from the function
2377 static void __exit velocity_cleanup_module(void)
2379 velocity_unregister_notifier();
2380 pci_unregister_driver(&velocity_driver);
2383 module_init(velocity_init_module);
2384 module_exit(velocity_cleanup_module);
2388 * MII access , media link mode setting functions
2393 * mii_init - set up MII
2394 * @vptr: velocity adapter
2395 * @mii_status: links tatus
2397 * Set up the PHY for the current link state.
2400 static void mii_init(struct velocity_info *vptr, u32 mii_status)
2404 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
2405 case PHYID_CICADA_CS8201:
2407 * Reset to hardware default
2409 MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2411 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2412 * off it in NWay-forced half mode for NWay-forced v.s.
2413 * legacy-forced issue.
2415 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2416 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2418 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2420 * Turn on Link/Activity LED enable bit for CIS8201
2422 MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
2424 case PHYID_VT3216_32BIT:
2425 case PHYID_VT3216_64BIT:
2427 * Reset to hardware default
2429 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2431 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2432 * off it in NWay-forced half mode for NWay-forced v.s.
2433 * legacy-forced issue
2435 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2436 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2438 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2441 case PHYID_MARVELL_1000:
2442 case PHYID_MARVELL_1000S:
2444 * Assert CRS on Transmit
2446 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
2448 * Reset to hardware default
2450 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2455 velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
2456 if (BMCR & BMCR_ISO) {
2458 velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
2463 * safe_disable_mii_autopoll - autopoll off
2464 * @regs: velocity registers
2466 * Turn off the autopoll and wait for it to disable on the chip
2469 static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs)
2473 /* turn off MAUTO */
2474 writeb(0, ®s->MIICR);
2475 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2477 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
2483 * enable_mii_autopoll - turn on autopolling
2484 * @regs: velocity registers
2486 * Enable the MII link status autopoll feature on the Velocity
2487 * hardware. Wait for it to enable.
2490 static void enable_mii_autopoll(struct mac_regs __iomem *regs)
2494 writeb(0, &(regs->MIICR));
2495 writeb(MIIADR_SWMPL, ®s->MIIADR);
2497 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2499 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
2503 writeb(MIICR_MAUTO, ®s->MIICR);
2505 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2507 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
2514 * velocity_mii_read - read MII data
2515 * @regs: velocity registers
2516 * @index: MII register index
2517 * @data: buffer for received data
2519 * Perform a single read of an MII 16bit register. Returns zero
2520 * on success or -ETIMEDOUT if the PHY did not respond.
2523 static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
2528 * Disable MIICR_MAUTO, so that mii addr can be set normally
2530 safe_disable_mii_autopoll(regs);
2532 writeb(index, ®s->MIIADR);
2534 BYTE_REG_BITS_ON(MIICR_RCMD, ®s->MIICR);
2536 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2537 if (!(readb(®s->MIICR) & MIICR_RCMD))
2541 *data = readw(®s->MIIDATA);
2543 enable_mii_autopoll(regs);
2544 if (ww == W_MAX_TIMEOUT)
2550 * velocity_mii_write - write MII data
2551 * @regs: velocity registers
2552 * @index: MII register index
2553 * @data: 16bit data for the MII register
2555 * Perform a single write to an MII 16bit register. Returns zero
2556 * on success or -ETIMEDOUT if the PHY did not respond.
2559 static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
2564 * Disable MIICR_MAUTO, so that mii addr can be set normally
2566 safe_disable_mii_autopoll(regs);
2568 /* MII reg offset */
2569 writeb(mii_addr, ®s->MIIADR);
2571 writew(data, ®s->MIIDATA);
2573 /* turn on MIICR_WCMD */
2574 BYTE_REG_BITS_ON(MIICR_WCMD, ®s->MIICR);
2576 /* W_MAX_TIMEOUT is the timeout period */
2577 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2579 if (!(readb(®s->MIICR) & MIICR_WCMD))
2582 enable_mii_autopoll(regs);
2584 if (ww == W_MAX_TIMEOUT)
2590 * velocity_get_opt_media_mode - get media selection
2591 * @vptr: velocity adapter
2593 * Get the media mode stored in EEPROM or module options and load
2594 * mii_status accordingly. The requested link state information
2598 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
2602 switch (vptr->options.spd_dpx) {
2604 status = VELOCITY_AUTONEG_ENABLE;
2606 case SPD_DPX_100_FULL:
2607 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
2609 case SPD_DPX_10_FULL:
2610 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
2612 case SPD_DPX_100_HALF:
2613 status = VELOCITY_SPEED_100;
2615 case SPD_DPX_10_HALF:
2616 status = VELOCITY_SPEED_10;
2619 vptr->mii_status = status;
2624 * mii_set_auto_on - autonegotiate on
2627 * Enable autonegotation on this interface
2630 static void mii_set_auto_on(struct velocity_info *vptr)
2632 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
2633 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
2635 MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2640 static void mii_set_auto_off(struct velocity_info *vptr)
2642 MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2647 * set_mii_flow_control - flow control setup
2648 * @vptr: velocity interface
2650 * Set up the flow control on this interface according to
2651 * the supplied user/eeprom options.
2654 static void set_mii_flow_control(struct velocity_info *vptr)
2656 /*Enable or Disable PAUSE in ANAR */
2657 switch (vptr->options.flow_cntl) {
2659 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2660 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2664 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2665 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2668 case FLOW_CNTL_TX_RX:
2669 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2670 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2673 case FLOW_CNTL_DISABLE:
2674 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2675 MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2683 * velocity_set_media_mode - set media mode
2684 * @mii_status: old MII link state
2686 * Check the media link state and configure the flow control
2687 * PHY and also velocity hardware setup accordingly. In particular
2688 * we need to set up CD polling and frame bursting.
2691 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
2694 struct mac_regs __iomem *regs = vptr->mac_regs;
2696 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
2697 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
2699 /* Set mii link status */
2700 set_mii_flow_control(vptr);
2703 Check if new status is consisent with current status
2704 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
2705 || (mii_status==curr_status)) {
2706 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
2707 vptr->mii_status=check_connection_type(vptr->mac_regs);
2708 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
2713 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
2714 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
2717 * If connection type is AUTO
2719 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
2720 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
2721 /* clear force MAC mode bit */
2722 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, ®s->CHIPGCR);
2723 /* set duplex mode of MAC according to duplex mode of MII */
2724 MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
2725 MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2726 MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
2728 /* enable AUTO-NEGO mode */
2729 mii_set_auto_on(vptr);
2735 * 1. if it's 3119, disable frame bursting in halfduplex mode
2736 * and enable it in fullduplex mode
2737 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
2738 * 3. only enable CD heart beat counter in 10HD mode
2741 /* set force MAC mode bit */
2742 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, ®s->CHIPGCR);
2744 CHIPGCR = readb(®s->CHIPGCR);
2745 CHIPGCR &= ~CHIPGCR_FCGMII;
2747 if (mii_status & VELOCITY_DUPLEX_FULL) {
2748 CHIPGCR |= CHIPGCR_FCFDX;
2749 writeb(CHIPGCR, ®s->CHIPGCR);
2750 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
2751 if (vptr->rev_id < REV_ID_VT3216_A0)
2752 BYTE_REG_BITS_OFF(TCR_TB2BDIS, ®s->TCR);
2754 CHIPGCR &= ~CHIPGCR_FCFDX;
2755 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
2756 writeb(CHIPGCR, ®s->CHIPGCR);
2757 if (vptr->rev_id < REV_ID_VT3216_A0)
2758 BYTE_REG_BITS_ON(TCR_TB2BDIS, ®s->TCR);
2761 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2763 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10))
2764 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, ®s->TESTCFG);
2766 BYTE_REG_BITS_ON(TESTCFG_HBDIS, ®s->TESTCFG);
2768 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
2769 velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
2770 ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
2771 if (mii_status & VELOCITY_SPEED_100) {
2772 if (mii_status & VELOCITY_DUPLEX_FULL)
2777 if (mii_status & VELOCITY_DUPLEX_FULL)
2782 velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
2783 /* enable AUTO-NEGO mode */
2784 mii_set_auto_on(vptr);
2785 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
2787 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
2788 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
2789 return VELOCITY_LINK_CHANGE;
2793 * mii_check_media_mode - check media state
2794 * @regs: velocity registers
2796 * Check the current MII status and determine the link status
2800 static u32 mii_check_media_mode(struct mac_regs __iomem *regs)
2805 if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
2806 status |= VELOCITY_LINK_FAIL;
2808 if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
2809 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
2810 else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
2811 status |= (VELOCITY_SPEED_1000);
2813 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2814 if (ANAR & ANAR_TXFD)
2815 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
2816 else if (ANAR & ANAR_TX)
2817 status |= VELOCITY_SPEED_100;
2818 else if (ANAR & ANAR_10FD)
2819 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
2821 status |= (VELOCITY_SPEED_10);
2824 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2825 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2826 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2827 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2828 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2829 status |= VELOCITY_AUTONEG_ENABLE;
2836 static u32 check_connection_type(struct mac_regs __iomem *regs)
2841 PHYSR0 = readb(®s->PHYSR0);
2844 if (!(PHYSR0 & PHYSR0_LINKGD))
2845 status|=VELOCITY_LINK_FAIL;
2848 if (PHYSR0 & PHYSR0_FDPX)
2849 status |= VELOCITY_DUPLEX_FULL;
2851 if (PHYSR0 & PHYSR0_SPDG)
2852 status |= VELOCITY_SPEED_1000;
2853 else if (PHYSR0 & PHYSR0_SPD10)
2854 status |= VELOCITY_SPEED_10;
2856 status |= VELOCITY_SPEED_100;
2858 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2859 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2860 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2861 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2862 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2863 status |= VELOCITY_AUTONEG_ENABLE;
2871 * enable_flow_control_ability - flow control
2872 * @vptr: veloity to configure
2874 * Set up flow control according to the flow control options
2875 * determined by the eeprom/configuration.
2878 static void enable_flow_control_ability(struct velocity_info *vptr)
2881 struct mac_regs __iomem *regs = vptr->mac_regs;
2883 switch (vptr->options.flow_cntl) {
2885 case FLOW_CNTL_DEFAULT:
2886 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, ®s->PHYSR0))
2887 writel(CR0_FDXRFCEN, ®s->CR0Set);
2889 writel(CR0_FDXRFCEN, ®s->CR0Clr);
2891 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, ®s->PHYSR0))
2892 writel(CR0_FDXTFCEN, ®s->CR0Set);
2894 writel(CR0_FDXTFCEN, ®s->CR0Clr);
2898 writel(CR0_FDXTFCEN, ®s->CR0Set);
2899 writel(CR0_FDXRFCEN, ®s->CR0Clr);
2903 writel(CR0_FDXRFCEN, ®s->CR0Set);
2904 writel(CR0_FDXTFCEN, ®s->CR0Clr);
2907 case FLOW_CNTL_TX_RX:
2908 writel(CR0_FDXTFCEN, ®s->CR0Set);
2909 writel(CR0_FDXRFCEN, ®s->CR0Set);
2912 case FLOW_CNTL_DISABLE:
2913 writel(CR0_FDXRFCEN, ®s->CR0Clr);
2914 writel(CR0_FDXTFCEN, ®s->CR0Clr);
2925 * velocity_ethtool_up - pre hook for ethtool
2926 * @dev: network device
2928 * Called before an ethtool operation. We need to make sure the
2929 * chip is out of D3 state before we poke at it.
2932 static int velocity_ethtool_up(struct net_device *dev)
2934 struct velocity_info *vptr = netdev_priv(dev);
2935 if (!netif_running(dev))
2936 pci_set_power_state(vptr->pdev, PCI_D0);
2941 * velocity_ethtool_down - post hook for ethtool
2942 * @dev: network device
2944 * Called after an ethtool operation. Restore the chip back to D3
2945 * state if it isn't running.
2948 static void velocity_ethtool_down(struct net_device *dev)
2950 struct velocity_info *vptr = netdev_priv(dev);
2951 if (!netif_running(dev))
2952 pci_set_power_state(vptr->pdev, PCI_D3hot);
2955 static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2957 struct velocity_info *vptr = netdev_priv(dev);
2958 struct mac_regs __iomem *regs = vptr->mac_regs;
2960 status = check_connection_type(vptr->mac_regs);
2962 cmd->supported = SUPPORTED_TP |
2964 SUPPORTED_10baseT_Half |
2965 SUPPORTED_10baseT_Full |
2966 SUPPORTED_100baseT_Half |
2967 SUPPORTED_100baseT_Full |
2968 SUPPORTED_1000baseT_Half |
2969 SUPPORTED_1000baseT_Full;
2970 if (status & VELOCITY_SPEED_1000)
2971 cmd->speed = SPEED_1000;
2972 else if (status & VELOCITY_SPEED_100)
2973 cmd->speed = SPEED_100;
2975 cmd->speed = SPEED_10;
2976 cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2977 cmd->port = PORT_TP;
2978 cmd->transceiver = XCVR_INTERNAL;
2979 cmd->phy_address = readb(®s->MIIADR) & 0x1F;
2981 if (status & VELOCITY_DUPLEX_FULL)
2982 cmd->duplex = DUPLEX_FULL;
2984 cmd->duplex = DUPLEX_HALF;
2989 static int velocity_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2991 struct velocity_info *vptr = netdev_priv(dev);
2996 curr_status = check_connection_type(vptr->mac_regs);
2997 curr_status &= (~VELOCITY_LINK_FAIL);
2999 new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
3000 new_status |= ((cmd->speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
3001 new_status |= ((cmd->speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
3002 new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
3004 if ((new_status & VELOCITY_AUTONEG_ENABLE) && (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE)))
3007 velocity_set_media_mode(vptr, new_status);
3012 static u32 velocity_get_link(struct net_device *dev)
3014 struct velocity_info *vptr = netdev_priv(dev);
3015 struct mac_regs __iomem *regs = vptr->mac_regs;
3016 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, ®s->PHYSR0) ? 1 : 0;
3019 static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3021 struct velocity_info *vptr = netdev_priv(dev);
3022 strcpy(info->driver, VELOCITY_NAME);
3023 strcpy(info->version, VELOCITY_VERSION);
3024 strcpy(info->bus_info, pci_name(vptr->pdev));
3027 static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3029 struct velocity_info *vptr = netdev_priv(dev);
3030 wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
3031 wol->wolopts |= WAKE_MAGIC;
3033 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3034 wol.wolopts|=WAKE_PHY;
3036 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
3037 wol->wolopts |= WAKE_UCAST;
3038 if (vptr->wol_opts & VELOCITY_WOL_ARP)
3039 wol->wolopts |= WAKE_ARP;
3040 memcpy(&wol->sopass, vptr->wol_passwd, 6);
3043 static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3045 struct velocity_info *vptr = netdev_priv(dev);
3047 if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
3049 vptr->wol_opts = VELOCITY_WOL_MAGIC;
3052 if (wol.wolopts & WAKE_PHY) {
3053 vptr->wol_opts|=VELOCITY_WOL_PHY;
3054 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
3058 if (wol->wolopts & WAKE_MAGIC) {
3059 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
3060 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3062 if (wol->wolopts & WAKE_UCAST) {
3063 vptr->wol_opts |= VELOCITY_WOL_UCAST;
3064 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3066 if (wol->wolopts & WAKE_ARP) {
3067 vptr->wol_opts |= VELOCITY_WOL_ARP;
3068 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3070 memcpy(vptr->wol_passwd, wol->sopass, 6);
3074 static u32 velocity_get_msglevel(struct net_device *dev)
3079 static void velocity_set_msglevel(struct net_device *dev, u32 value)
3084 static const struct ethtool_ops velocity_ethtool_ops = {
3085 .get_settings = velocity_get_settings,
3086 .set_settings = velocity_set_settings,
3087 .get_drvinfo = velocity_get_drvinfo,
3088 .get_wol = velocity_ethtool_get_wol,
3089 .set_wol = velocity_ethtool_set_wol,
3090 .get_msglevel = velocity_get_msglevel,
3091 .set_msglevel = velocity_set_msglevel,
3092 .get_link = velocity_get_link,
3093 .begin = velocity_ethtool_up,
3094 .complete = velocity_ethtool_down
3098 * velocity_mii_ioctl - MII ioctl handler
3099 * @dev: network device
3100 * @ifr: the ifreq block for the ioctl
3103 * Process MII requests made via ioctl from the network layer. These
3104 * are used by tools like kudzu to interrogate the link state of the
3108 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3110 struct velocity_info *vptr = netdev_priv(dev);
3111 struct mac_regs __iomem *regs = vptr->mac_regs;
3112 unsigned long flags;
3113 struct mii_ioctl_data *miidata = if_mii(ifr);
3118 miidata->phy_id = readb(®s->MIIADR) & 0x1f;
3121 if (!capable(CAP_NET_ADMIN))
3123 if (velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
3127 if (!capable(CAP_NET_ADMIN))
3129 spin_lock_irqsave(&vptr->lock, flags);
3130 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
3131 spin_unlock_irqrestore(&vptr->lock, flags);
3132 check_connection_type(vptr->mac_regs);
3145 * velocity_save_context - save registers
3147 * @context: buffer for stored context
3149 * Retrieve the current configuration from the velocity hardware
3150 * and stash it in the context structure, for use by the context
3151 * restore functions. This allows us to save things we need across
3155 static void velocity_save_context(struct velocity_info *vptr, struct velocity_context *context)
3157 struct mac_regs __iomem *regs = vptr->mac_regs;
3159 u8 __iomem *ptr = (u8 __iomem *)regs;
3161 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3162 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3164 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3165 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3167 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3168 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3173 * velocity_restore_context - restore registers
3175 * @context: buffer for stored context
3177 * Reload the register configuration from the velocity context
3178 * created by velocity_save_context.
3181 static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3183 struct mac_regs __iomem *regs = vptr->mac_regs;
3185 u8 __iomem *ptr = (u8 __iomem *)regs;
3187 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4)
3188 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3191 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3193 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3195 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3198 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4)
3199 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3201 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3202 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3204 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++)
3205 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3209 * wol_calc_crc - WOL CRC
3210 * @pattern: data pattern
3211 * @mask_pattern: mask
3213 * Compute the wake on lan crc hashes for the packet header
3214 * we are interested in.
3217 static u16 wol_calc_crc(int size, u8 *pattern, u8 *mask_pattern)
3223 for (i = 0; i < size; i++) {
3224 mask = mask_pattern[i];
3226 /* Skip this loop if the mask equals to zero */
3230 for (j = 0; j < 8; j++) {
3231 if ((mask & 0x01) == 0) {
3236 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
3239 /* Finally, invert the result once to get the correct data */
3241 return bitrev32(crc) >> 16;
3245 * velocity_set_wol - set up for wake on lan
3246 * @vptr: velocity to set WOL status on
3248 * Set a card up for wake on lan either by unicast or by
3251 * FIXME: check static buffer is safe here
3254 static int velocity_set_wol(struct velocity_info *vptr)
3256 struct mac_regs __iomem *regs = vptr->mac_regs;
3260 static u32 mask_pattern[2][4] = {
3261 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3262 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3265 writew(0xFFFF, ®s->WOLCRClr);
3266 writeb(WOLCFG_SAB | WOLCFG_SAM, ®s->WOLCFGSet);
3267 writew(WOLCR_MAGIC_EN, ®s->WOLCRSet);
3270 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3271 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), ®s->WOLCRSet);
3274 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
3275 writew(WOLCR_UNICAST_EN, ®s->WOLCRSet);
3277 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3278 struct arp_packet *arp = (struct arp_packet *) buf;
3280 memset(buf, 0, sizeof(struct arp_packet) + 7);
3282 for (i = 0; i < 4; i++)
3283 writel(mask_pattern[0][i], ®s->ByteMask[0][i]);
3285 arp->type = htons(ETH_P_ARP);
3286 arp->ar_op = htons(1);
3288 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3290 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3291 (u8 *) & mask_pattern[0][0]);
3293 writew(crc, ®s->PatternCRC[0]);
3294 writew(WOLCR_ARP_EN, ®s->WOLCRSet);
3297 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, ®s->PWCFGSet);
3298 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, ®s->PWCFGSet);
3300 writew(0x0FFF, ®s->WOLSRClr);
3302 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3303 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3304 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
3306 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
3309 if (vptr->mii_status & VELOCITY_SPEED_1000)
3310 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
3312 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, ®s->CHIPGCR);
3316 GCR = readb(®s->CHIPGCR);
3317 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3318 writeb(GCR, ®s->CHIPGCR);
3321 BYTE_REG_BITS_OFF(ISR_PWEI, ®s->ISR);
3322 /* Turn on SWPTAG just before entering power mode */
3323 BYTE_REG_BITS_ON(STICKHW_SWPTAG, ®s->STICKHW);
3324 /* Go to bed ..... */
3325 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), ®s->STICKHW);
3330 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
3332 struct net_device *dev = pci_get_drvdata(pdev);
3333 struct velocity_info *vptr = netdev_priv(dev);
3334 unsigned long flags;
3336 if (!netif_running(vptr->dev))
3339 netif_device_detach(vptr->dev);
3341 spin_lock_irqsave(&vptr->lock, flags);
3342 pci_save_state(pdev);
3344 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3345 velocity_get_ip(vptr);
3346 velocity_save_context(vptr, &vptr->context);
3347 velocity_shutdown(vptr);
3348 velocity_set_wol(vptr);
3349 pci_enable_wake(pdev, PCI_D3hot, 1);
3350 pci_set_power_state(pdev, PCI_D3hot);
3352 velocity_save_context(vptr, &vptr->context);
3353 velocity_shutdown(vptr);
3354 pci_disable_device(pdev);
3355 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3358 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3360 spin_unlock_irqrestore(&vptr->lock, flags);
3364 static int velocity_resume(struct pci_dev *pdev)
3366 struct net_device *dev = pci_get_drvdata(pdev);
3367 struct velocity_info *vptr = netdev_priv(dev);
3368 unsigned long flags;
3371 if (!netif_running(vptr->dev))
3374 pci_set_power_state(pdev, PCI_D0);
3375 pci_enable_wake(pdev, 0, 0);
3376 pci_restore_state(pdev);
3378 mac_wol_reset(vptr->mac_regs);
3380 spin_lock_irqsave(&vptr->lock, flags);
3381 velocity_restore_context(vptr, &vptr->context);
3382 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3383 mac_disable_int(vptr->mac_regs);
3385 velocity_tx_srv(vptr, 0);
3387 for (i = 0; i < vptr->tx.numq; i++) {
3388 if (vptr->tx.used[i])
3389 mac_tx_queue_wake(vptr->mac_regs, i);
3392 mac_enable_int(vptr->mac_regs);
3393 spin_unlock_irqrestore(&vptr->lock, flags);
3394 netif_device_attach(vptr->dev);
3401 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3403 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3404 struct net_device *dev = ifa->ifa_dev->dev;
3405 struct velocity_info *vptr;
3406 unsigned long flags;
3408 if (dev_net(dev) != &init_net)
3411 spin_lock_irqsave(&velocity_dev_list_lock, flags);
3412 list_for_each_entry(vptr, &velocity_dev_list, list) {
3413 if (vptr->dev == dev) {
3414 velocity_get_ip(vptr);
3418 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);