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
14 * The changes are (c) Copyright 2004, Red Hat Inc. <alan@lxorguk.ukuu.org.uk>
15 * Additional fixes and clean up: Francois Romieu
17 * This source has not been verified for use in safety critical systems.
19 * Please direct queries about the revamped driver to the linux-kernel
24 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
25 * All rights reserved.
27 * This software may be redistributed and/or modified under
28 * the terms of the GNU General Public License as published by the Free
29 * Software Foundation; either version 2 of the License, or
32 * This program is distributed in the hope that it will be useful, but
33 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
34 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
37 * Author: Chuang Liang-Shing, AJ Jiang
41 * MODULE_LICENSE("GPL");
45 #include <linux/module.h>
46 #include <linux/types.h>
47 #include <linux/bitops.h>
48 #include <linux/init.h>
49 #include <linux/dma-mapping.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/of_address.h>
69 #include <linux/of_device.h>
70 #include <linux/of_irq.h>
71 #include <linux/inetdevice.h>
72 #include <linux/platform_device.h>
73 #include <linux/reboot.h>
74 #include <linux/ethtool.h>
75 #include <linux/mii.h>
77 #include <linux/if_arp.h>
78 #include <linux/if_vlan.h>
80 #include <linux/tcp.h>
81 #include <linux/udp.h>
82 #include <linux/crc-ccitt.h>
83 #include <linux/crc32.h>
85 #include "via-velocity.h"
87 enum velocity_bus_type {
92 static int velocity_nics;
93 static int msglevel = MSG_LEVEL_INFO;
95 static void velocity_set_power_state(struct velocity_info *vptr, char state)
97 void *addr = vptr->mac_regs;
100 pci_set_power_state(vptr->pdev, state);
102 writeb(state, addr + 0x154);
106 * mac_get_cam_mask - Read a CAM mask
107 * @regs: register block for this velocity
108 * @mask: buffer to store mask
110 * Fetch the mask bits of the selected CAM and store them into the
111 * provided mask buffer.
113 static void mac_get_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
117 /* Select CAM mask */
118 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
120 writeb(0, ®s->CAMADDR);
123 for (i = 0; i < 8; i++)
124 *mask++ = readb(&(regs->MARCAM[i]));
127 writeb(0, ®s->CAMADDR);
130 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
134 * mac_set_cam_mask - Set a CAM mask
135 * @regs: register block for this velocity
136 * @mask: CAM mask to load
138 * Store a new mask into a CAM
140 static void mac_set_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
143 /* Select CAM mask */
144 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
146 writeb(CAMADDR_CAMEN, ®s->CAMADDR);
148 for (i = 0; i < 8; i++)
149 writeb(*mask++, &(regs->MARCAM[i]));
152 writeb(0, ®s->CAMADDR);
155 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
158 static void mac_set_vlan_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
161 /* Select CAM mask */
162 BYTE_REG_BITS_SET(CAMCR_PS_CAM_MASK, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
164 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL, ®s->CAMADDR);
166 for (i = 0; i < 8; i++)
167 writeb(*mask++, &(regs->MARCAM[i]));
170 writeb(0, ®s->CAMADDR);
173 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
177 * mac_set_cam - set CAM data
178 * @regs: register block of this velocity
180 * @addr: 2 or 6 bytes of CAM data
182 * Load an address or vlan tag into a CAM
184 static void mac_set_cam(struct mac_regs __iomem *regs, int idx, const u8 *addr)
188 /* Select CAM mask */
189 BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
193 writeb(CAMADDR_CAMEN | idx, ®s->CAMADDR);
195 for (i = 0; i < 6; i++)
196 writeb(*addr++, &(regs->MARCAM[i]));
198 BYTE_REG_BITS_ON(CAMCR_CAMWR, ®s->CAMCR);
202 writeb(0, ®s->CAMADDR);
205 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
208 static void mac_set_vlan_cam(struct mac_regs __iomem *regs, int idx,
212 /* Select CAM mask */
213 BYTE_REG_BITS_SET(CAMCR_PS_CAM_DATA, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
217 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL | idx, ®s->CAMADDR);
218 writew(*((u16 *) addr), ®s->MARCAM[0]);
220 BYTE_REG_BITS_ON(CAMCR_CAMWR, ®s->CAMCR);
224 writeb(0, ®s->CAMADDR);
227 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, ®s->CAMCR);
232 * mac_wol_reset - reset WOL after exiting low power
233 * @regs: register block of this velocity
235 * Called after we drop out of wake on lan mode in order to
236 * reset the Wake on lan features. This function doesn't restore
237 * the rest of the logic from the result of sleep/wakeup
239 static void mac_wol_reset(struct mac_regs __iomem *regs)
242 /* Turn off SWPTAG right after leaving power mode */
243 BYTE_REG_BITS_OFF(STICKHW_SWPTAG, ®s->STICKHW);
244 /* clear sticky bits */
245 BYTE_REG_BITS_OFF((STICKHW_DS1 | STICKHW_DS0), ®s->STICKHW);
247 BYTE_REG_BITS_OFF(CHIPGCR_FCGMII, ®s->CHIPGCR);
248 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, ®s->CHIPGCR);
249 /* disable force PME-enable */
250 writeb(WOLCFG_PMEOVR, ®s->WOLCFGClr);
251 /* disable power-event config bit */
252 writew(0xFFFF, ®s->WOLCRClr);
253 /* clear power status */
254 writew(0xFFFF, ®s->WOLSRClr);
257 static const struct ethtool_ops velocity_ethtool_ops;
260 Define module options
263 MODULE_AUTHOR("VIA Networking Technologies, Inc.");
264 MODULE_LICENSE("GPL");
265 MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
267 #define VELOCITY_PARAM(N, D) \
268 static int N[MAX_UNITS] = OPTION_DEFAULT;\
269 module_param_array(N, int, NULL, 0); \
270 MODULE_PARM_DESC(N, D);
272 #define RX_DESC_MIN 64
273 #define RX_DESC_MAX 255
274 #define RX_DESC_DEF 64
275 VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
277 #define TX_DESC_MIN 16
278 #define TX_DESC_MAX 256
279 #define TX_DESC_DEF 64
280 VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
282 #define RX_THRESH_MIN 0
283 #define RX_THRESH_MAX 3
284 #define RX_THRESH_DEF 0
285 /* rx_thresh[] is used for controlling the receive fifo threshold.
286 0: indicate the rxfifo threshold is 128 bytes.
287 1: indicate the rxfifo threshold is 512 bytes.
288 2: indicate the rxfifo threshold is 1024 bytes.
289 3: indicate the rxfifo threshold is store & forward.
291 VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
293 #define DMA_LENGTH_MIN 0
294 #define DMA_LENGTH_MAX 7
295 #define DMA_LENGTH_DEF 6
297 /* DMA_length[] is used for controlling the DMA length
304 6: SF(flush till emply)
305 7: SF(flush till emply)
307 VELOCITY_PARAM(DMA_length, "DMA length");
309 #define IP_ALIG_DEF 0
310 /* IP_byte_align[] is used for IP header DWORD byte aligned
311 0: indicate the IP header won't be DWORD byte aligned.(Default) .
312 1: indicate the IP header will be DWORD byte aligned.
313 In some environment, the IP header should be DWORD byte aligned,
314 or the packet will be droped when we receive it. (eg: IPVS)
316 VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
318 #define FLOW_CNTL_DEF 1
319 #define FLOW_CNTL_MIN 1
320 #define FLOW_CNTL_MAX 5
322 /* flow_control[] is used for setting the flow control ability of NIC.
323 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
324 2: enable TX flow control.
325 3: enable RX flow control.
326 4: enable RX/TX flow control.
329 VELOCITY_PARAM(flow_control, "Enable flow control ability");
331 #define MED_LNK_DEF 0
332 #define MED_LNK_MIN 0
333 #define MED_LNK_MAX 5
334 /* speed_duplex[] is used for setting the speed and duplex mode of NIC.
335 0: indicate autonegotiation for both speed and duplex mode
336 1: indicate 100Mbps half duplex mode
337 2: indicate 100Mbps full duplex mode
338 3: indicate 10Mbps half duplex mode
339 4: indicate 10Mbps full duplex mode
340 5: indicate 1000Mbps full duplex mode
343 if EEPROM have been set to the force mode, this option is ignored
346 VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
348 #define VAL_PKT_LEN_DEF 0
349 /* ValPktLen[] is used for setting the checksum offload ability of NIC.
350 0: Receive frame with invalid layer 2 length (Default)
351 1: Drop frame with invalid layer 2 length
353 VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
355 #define WOL_OPT_DEF 0
356 #define WOL_OPT_MIN 0
357 #define WOL_OPT_MAX 7
358 /* wol_opts[] is used for controlling wake on lan behavior.
359 0: Wake up if recevied a magic packet. (Default)
360 1: Wake up if link status is on/off.
361 2: Wake up if recevied an arp packet.
362 4: Wake up if recevied any unicast packet.
363 Those value can be sumed up to support more than one option.
365 VELOCITY_PARAM(wol_opts, "Wake On Lan options");
367 static int rx_copybreak = 200;
368 module_param(rx_copybreak, int, 0644);
369 MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
372 * Internal board variants. At the moment we have only one
374 static struct velocity_info_tbl chip_info_table[] = {
375 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
380 * Describe the PCI device identifiers that we support in this
381 * device driver. Used for hotplug autoloading.
384 static const struct pci_device_id velocity_pci_id_table[] = {
385 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
389 MODULE_DEVICE_TABLE(pci, velocity_pci_id_table);
392 * Describe the OF device identifiers that we support in this
393 * device driver. Used for devicetree nodes.
395 static struct of_device_id velocity_of_ids[] = {
396 { .compatible = "via,velocity-vt6110", .data = &chip_info_table[0] },
399 MODULE_DEVICE_TABLE(of, velocity_of_ids);
402 * get_chip_name - identifier to name
403 * @id: chip identifier
405 * Given a chip identifier return a suitable description. Returns
406 * a pointer a static string valid while the driver is loaded.
408 static const char *get_chip_name(enum chip_type chip_id)
411 for (i = 0; chip_info_table[i].name != NULL; i++)
412 if (chip_info_table[i].chip_id == chip_id)
414 return chip_info_table[i].name;
418 * velocity_set_int_opt - parser for integer options
419 * @opt: pointer to option value
420 * @val: value the user requested (or -1 for default)
421 * @min: lowest value allowed
422 * @max: highest value allowed
423 * @def: default value
424 * @name: property name
427 * Set an integer property in the module options. This function does
428 * all the verification and checking as well as reporting so that
429 * we don't duplicate code for each option.
431 static void velocity_set_int_opt(int *opt, int val, int min, int max, int def,
432 char *name, const char *devname)
436 else if (val < min || val > max) {
437 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
438 devname, name, min, max);
441 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
448 * velocity_set_bool_opt - parser for boolean options
449 * @opt: pointer to option value
450 * @val: value the user requested (or -1 for default)
451 * @def: default value (yes/no)
452 * @flag: numeric value to set for true.
453 * @name: property name
456 * Set a boolean property in the module options. This function does
457 * all the verification and checking as well as reporting so that
458 * we don't duplicate code for each option.
460 static void velocity_set_bool_opt(u32 *opt, int val, int def, u32 flag,
461 char *name, const char *devname)
465 *opt |= (def ? flag : 0);
466 else if (val < 0 || val > 1) {
467 printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
469 *opt |= (def ? flag : 0);
471 printk(KERN_INFO "%s: set parameter %s to %s\n",
472 devname, name, val ? "TRUE" : "FALSE");
473 *opt |= (val ? flag : 0);
478 * velocity_get_options - set options on device
479 * @opts: option structure for the device
480 * @index: index of option to use in module options array
481 * @devname: device name
483 * Turn the module and command options into a single structure
484 * for the current device
486 static void velocity_get_options(struct velocity_opt *opts, int index,
490 velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
491 velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
492 velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
493 velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
495 velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
496 velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
497 velocity_set_bool_opt(&opts->flags, ValPktLen[index], VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN, "ValPktLen", devname);
498 velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
499 velocity_set_int_opt(&opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
500 opts->numrx = (opts->numrx & ~3);
504 * velocity_init_cam_filter - initialise CAM
505 * @vptr: velocity to program
507 * Initialize the content addressable memory used for filters. Load
508 * appropriately according to the presence of VLAN
510 static void velocity_init_cam_filter(struct velocity_info *vptr)
512 struct mac_regs __iomem *regs = vptr->mac_regs;
513 unsigned int vid, i = 0;
515 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
516 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, ®s->MCFG);
517 WORD_REG_BITS_ON(MCFG_VIDFR, ®s->MCFG);
519 /* Disable all CAMs */
520 memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
521 memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
522 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
523 mac_set_cam_mask(regs, vptr->mCAMmask);
526 for_each_set_bit(vid, vptr->active_vlans, VLAN_N_VID) {
527 mac_set_vlan_cam(regs, i, (u8 *) &vid);
528 vptr->vCAMmask[i / 8] |= 0x1 << (i % 8);
529 if (++i >= VCAM_SIZE)
532 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
535 static int velocity_vlan_rx_add_vid(struct net_device *dev,
536 __be16 proto, u16 vid)
538 struct velocity_info *vptr = netdev_priv(dev);
540 spin_lock_irq(&vptr->lock);
541 set_bit(vid, vptr->active_vlans);
542 velocity_init_cam_filter(vptr);
543 spin_unlock_irq(&vptr->lock);
547 static int velocity_vlan_rx_kill_vid(struct net_device *dev,
548 __be16 proto, u16 vid)
550 struct velocity_info *vptr = netdev_priv(dev);
552 spin_lock_irq(&vptr->lock);
553 clear_bit(vid, vptr->active_vlans);
554 velocity_init_cam_filter(vptr);
555 spin_unlock_irq(&vptr->lock);
559 static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
561 vptr->rx.dirty = vptr->rx.filled = vptr->rx.curr = 0;
565 * velocity_rx_reset - handle a receive reset
566 * @vptr: velocity we are resetting
568 * Reset the ownership and status for the receive ring side.
569 * Hand all the receive queue to the NIC.
571 static void velocity_rx_reset(struct velocity_info *vptr)
574 struct mac_regs __iomem *regs = vptr->mac_regs;
577 velocity_init_rx_ring_indexes(vptr);
580 * Init state, all RD entries belong to the NIC
582 for (i = 0; i < vptr->options.numrx; ++i)
583 vptr->rx.ring[i].rdesc0.len |= OWNED_BY_NIC;
585 writew(vptr->options.numrx, ®s->RBRDU);
586 writel(vptr->rx.pool_dma, ®s->RDBaseLo);
587 writew(0, ®s->RDIdx);
588 writew(vptr->options.numrx - 1, ®s->RDCSize);
592 * velocity_get_opt_media_mode - get media selection
593 * @vptr: velocity adapter
595 * Get the media mode stored in EEPROM or module options and load
596 * mii_status accordingly. The requested link state information
599 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
603 switch (vptr->options.spd_dpx) {
605 status = VELOCITY_AUTONEG_ENABLE;
607 case SPD_DPX_100_FULL:
608 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
610 case SPD_DPX_10_FULL:
611 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
613 case SPD_DPX_100_HALF:
614 status = VELOCITY_SPEED_100;
616 case SPD_DPX_10_HALF:
617 status = VELOCITY_SPEED_10;
619 case SPD_DPX_1000_FULL:
620 status = VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
623 vptr->mii_status = status;
628 * safe_disable_mii_autopoll - autopoll off
629 * @regs: velocity registers
631 * Turn off the autopoll and wait for it to disable on the chip
633 static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs)
638 writeb(0, ®s->MIICR);
639 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
641 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
647 * enable_mii_autopoll - turn on autopolling
648 * @regs: velocity registers
650 * Enable the MII link status autopoll feature on the Velocity
651 * hardware. Wait for it to enable.
653 static void enable_mii_autopoll(struct mac_regs __iomem *regs)
657 writeb(0, &(regs->MIICR));
658 writeb(MIIADR_SWMPL, ®s->MIIADR);
660 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
662 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
666 writeb(MIICR_MAUTO, ®s->MIICR);
668 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
670 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
677 * velocity_mii_read - read MII data
678 * @regs: velocity registers
679 * @index: MII register index
680 * @data: buffer for received data
682 * Perform a single read of an MII 16bit register. Returns zero
683 * on success or -ETIMEDOUT if the PHY did not respond.
685 static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
690 * Disable MIICR_MAUTO, so that mii addr can be set normally
692 safe_disable_mii_autopoll(regs);
694 writeb(index, ®s->MIIADR);
696 BYTE_REG_BITS_ON(MIICR_RCMD, ®s->MIICR);
698 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
699 if (!(readb(®s->MIICR) & MIICR_RCMD))
703 *data = readw(®s->MIIDATA);
705 enable_mii_autopoll(regs);
706 if (ww == W_MAX_TIMEOUT)
712 * mii_check_media_mode - check media state
713 * @regs: velocity registers
715 * Check the current MII status and determine the link status
718 static u32 mii_check_media_mode(struct mac_regs __iomem *regs)
723 if (!MII_REG_BITS_IS_ON(BMSR_LSTATUS, MII_BMSR, regs))
724 status |= VELOCITY_LINK_FAIL;
726 if (MII_REG_BITS_IS_ON(ADVERTISE_1000FULL, MII_CTRL1000, regs))
727 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
728 else if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF, MII_CTRL1000, regs))
729 status |= (VELOCITY_SPEED_1000);
731 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
732 if (ANAR & ADVERTISE_100FULL)
733 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
734 else if (ANAR & ADVERTISE_100HALF)
735 status |= VELOCITY_SPEED_100;
736 else if (ANAR & ADVERTISE_10FULL)
737 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
739 status |= (VELOCITY_SPEED_10);
742 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
743 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
744 if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
745 == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
746 if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
747 status |= VELOCITY_AUTONEG_ENABLE;
755 * velocity_mii_write - write MII data
756 * @regs: velocity registers
757 * @index: MII register index
758 * @data: 16bit data for the MII register
760 * Perform a single write to an MII 16bit register. Returns zero
761 * on success or -ETIMEDOUT if the PHY did not respond.
763 static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
768 * Disable MIICR_MAUTO, so that mii addr can be set normally
770 safe_disable_mii_autopoll(regs);
773 writeb(mii_addr, ®s->MIIADR);
775 writew(data, ®s->MIIDATA);
777 /* turn on MIICR_WCMD */
778 BYTE_REG_BITS_ON(MIICR_WCMD, ®s->MIICR);
780 /* W_MAX_TIMEOUT is the timeout period */
781 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
783 if (!(readb(®s->MIICR) & MIICR_WCMD))
786 enable_mii_autopoll(regs);
788 if (ww == W_MAX_TIMEOUT)
794 * set_mii_flow_control - flow control setup
795 * @vptr: velocity interface
797 * Set up the flow control on this interface according to
798 * the supplied user/eeprom options.
800 static void set_mii_flow_control(struct velocity_info *vptr)
802 /*Enable or Disable PAUSE in ANAR */
803 switch (vptr->options.flow_cntl) {
805 MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
806 MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
810 MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
811 MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
814 case FLOW_CNTL_TX_RX:
815 MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
816 MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
819 case FLOW_CNTL_DISABLE:
820 MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
821 MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
829 * mii_set_auto_on - autonegotiate on
832 * Enable autonegotation on this interface
834 static void mii_set_auto_on(struct velocity_info *vptr)
836 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs))
837 MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
839 MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs);
842 static u32 check_connection_type(struct mac_regs __iomem *regs)
847 PHYSR0 = readb(®s->PHYSR0);
850 if (!(PHYSR0 & PHYSR0_LINKGD))
851 status|=VELOCITY_LINK_FAIL;
854 if (PHYSR0 & PHYSR0_FDPX)
855 status |= VELOCITY_DUPLEX_FULL;
857 if (PHYSR0 & PHYSR0_SPDG)
858 status |= VELOCITY_SPEED_1000;
859 else if (PHYSR0 & PHYSR0_SPD10)
860 status |= VELOCITY_SPEED_10;
862 status |= VELOCITY_SPEED_100;
864 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
865 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
866 if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
867 == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
868 if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
869 status |= VELOCITY_AUTONEG_ENABLE;
877 * velocity_set_media_mode - set media mode
878 * @mii_status: old MII link state
880 * Check the media link state and configure the flow control
881 * PHY and also velocity hardware setup accordingly. In particular
882 * we need to set up CD polling and frame bursting.
884 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
887 struct mac_regs __iomem *regs = vptr->mac_regs;
889 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
890 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
892 /* Set mii link status */
893 set_mii_flow_control(vptr);
896 Check if new status is consistent with current status
897 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE) ||
898 (mii_status==curr_status)) {
899 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
900 vptr->mii_status=check_connection_type(vptr->mac_regs);
901 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
906 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
907 MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
910 * If connection type is AUTO
912 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
913 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
914 /* clear force MAC mode bit */
915 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, ®s->CHIPGCR);
916 /* set duplex mode of MAC according to duplex mode of MII */
917 MII_REG_BITS_ON(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF, MII_ADVERTISE, vptr->mac_regs);
918 MII_REG_BITS_ON(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
919 MII_REG_BITS_ON(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs);
921 /* enable AUTO-NEGO mode */
922 mii_set_auto_on(vptr);
929 * 1. if it's 3119, disable frame bursting in halfduplex mode
930 * and enable it in fullduplex mode
931 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
932 * 3. only enable CD heart beat counter in 10HD mode
935 /* set force MAC mode bit */
936 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, ®s->CHIPGCR);
938 CHIPGCR = readb(®s->CHIPGCR);
940 if (mii_status & VELOCITY_SPEED_1000)
941 CHIPGCR |= CHIPGCR_FCGMII;
943 CHIPGCR &= ~CHIPGCR_FCGMII;
945 if (mii_status & VELOCITY_DUPLEX_FULL) {
946 CHIPGCR |= CHIPGCR_FCFDX;
947 writeb(CHIPGCR, ®s->CHIPGCR);
948 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
949 if (vptr->rev_id < REV_ID_VT3216_A0)
950 BYTE_REG_BITS_OFF(TCR_TB2BDIS, ®s->TCR);
952 CHIPGCR &= ~CHIPGCR_FCFDX;
953 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
954 writeb(CHIPGCR, ®s->CHIPGCR);
955 if (vptr->rev_id < REV_ID_VT3216_A0)
956 BYTE_REG_BITS_ON(TCR_TB2BDIS, ®s->TCR);
959 velocity_mii_read(vptr->mac_regs, MII_CTRL1000, &CTRL1000);
960 CTRL1000 &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
961 if ((mii_status & VELOCITY_SPEED_1000) &&
962 (mii_status & VELOCITY_DUPLEX_FULL)) {
963 CTRL1000 |= ADVERTISE_1000FULL;
965 velocity_mii_write(vptr->mac_regs, MII_CTRL1000, CTRL1000);
967 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10))
968 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, ®s->TESTCFG);
970 BYTE_REG_BITS_ON(TESTCFG_HBDIS, ®s->TESTCFG);
972 /* MII_REG_BITS_OFF(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs); */
973 velocity_mii_read(vptr->mac_regs, MII_ADVERTISE, &ANAR);
974 ANAR &= (~(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF));
975 if (mii_status & VELOCITY_SPEED_100) {
976 if (mii_status & VELOCITY_DUPLEX_FULL)
977 ANAR |= ADVERTISE_100FULL;
979 ANAR |= ADVERTISE_100HALF;
980 } else if (mii_status & VELOCITY_SPEED_10) {
981 if (mii_status & VELOCITY_DUPLEX_FULL)
982 ANAR |= ADVERTISE_10FULL;
984 ANAR |= ADVERTISE_10HALF;
986 velocity_mii_write(vptr->mac_regs, MII_ADVERTISE, ANAR);
987 /* enable AUTO-NEGO mode */
988 mii_set_auto_on(vptr);
989 /* MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs); */
991 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
992 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
993 return VELOCITY_LINK_CHANGE;
997 * velocity_print_link_status - link status reporting
998 * @vptr: velocity to report on
1000 * Turn the link status of the velocity card into a kernel log
1001 * description of the new link state, detailing speed and duplex
1004 static void velocity_print_link_status(struct velocity_info *vptr)
1007 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1008 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->netdev->name);
1009 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1010 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->netdev->name);
1012 if (vptr->mii_status & VELOCITY_SPEED_1000)
1013 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1014 else if (vptr->mii_status & VELOCITY_SPEED_100)
1015 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1017 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1019 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1020 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1022 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1024 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->netdev->name);
1025 switch (vptr->options.spd_dpx) {
1026 case SPD_DPX_1000_FULL:
1027 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps full duplex\n");
1029 case SPD_DPX_100_HALF:
1030 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1032 case SPD_DPX_100_FULL:
1033 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1035 case SPD_DPX_10_HALF:
1036 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1038 case SPD_DPX_10_FULL:
1039 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1048 * enable_flow_control_ability - flow control
1049 * @vptr: veloity to configure
1051 * Set up flow control according to the flow control options
1052 * determined by the eeprom/configuration.
1054 static void enable_flow_control_ability(struct velocity_info *vptr)
1057 struct mac_regs __iomem *regs = vptr->mac_regs;
1059 switch (vptr->options.flow_cntl) {
1061 case FLOW_CNTL_DEFAULT:
1062 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, ®s->PHYSR0))
1063 writel(CR0_FDXRFCEN, ®s->CR0Set);
1065 writel(CR0_FDXRFCEN, ®s->CR0Clr);
1067 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, ®s->PHYSR0))
1068 writel(CR0_FDXTFCEN, ®s->CR0Set);
1070 writel(CR0_FDXTFCEN, ®s->CR0Clr);
1074 writel(CR0_FDXTFCEN, ®s->CR0Set);
1075 writel(CR0_FDXRFCEN, ®s->CR0Clr);
1079 writel(CR0_FDXRFCEN, ®s->CR0Set);
1080 writel(CR0_FDXTFCEN, ®s->CR0Clr);
1083 case FLOW_CNTL_TX_RX:
1084 writel(CR0_FDXTFCEN, ®s->CR0Set);
1085 writel(CR0_FDXRFCEN, ®s->CR0Set);
1088 case FLOW_CNTL_DISABLE:
1089 writel(CR0_FDXRFCEN, ®s->CR0Clr);
1090 writel(CR0_FDXTFCEN, ®s->CR0Clr);
1100 * velocity_soft_reset - soft reset
1101 * @vptr: velocity to reset
1103 * Kick off a soft reset of the velocity adapter and then poll
1104 * until the reset sequence has completed before returning.
1106 static int velocity_soft_reset(struct velocity_info *vptr)
1108 struct mac_regs __iomem *regs = vptr->mac_regs;
1111 writel(CR0_SFRST, ®s->CR0Set);
1113 for (i = 0; i < W_MAX_TIMEOUT; i++) {
1115 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, ®s->CR0Set))
1119 if (i == W_MAX_TIMEOUT) {
1120 writel(CR0_FORSRST, ®s->CR0Set);
1121 /* FIXME: PCI POSTING */
1129 * velocity_set_multi - filter list change callback
1130 * @dev: network device
1132 * Called by the network layer when the filter lists need to change
1133 * for a velocity adapter. Reload the CAMs with the new address
1136 static void velocity_set_multi(struct net_device *dev)
1138 struct velocity_info *vptr = netdev_priv(dev);
1139 struct mac_regs __iomem *regs = vptr->mac_regs;
1142 struct netdev_hw_addr *ha;
1144 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1145 writel(0xffffffff, ®s->MARCAM[0]);
1146 writel(0xffffffff, ®s->MARCAM[4]);
1147 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
1148 } else if ((netdev_mc_count(dev) > vptr->multicast_limit) ||
1149 (dev->flags & IFF_ALLMULTI)) {
1150 writel(0xffffffff, ®s->MARCAM[0]);
1151 writel(0xffffffff, ®s->MARCAM[4]);
1152 rx_mode = (RCR_AM | RCR_AB);
1154 int offset = MCAM_SIZE - vptr->multicast_limit;
1155 mac_get_cam_mask(regs, vptr->mCAMmask);
1158 netdev_for_each_mc_addr(ha, dev) {
1159 mac_set_cam(regs, i + offset, ha->addr);
1160 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
1164 mac_set_cam_mask(regs, vptr->mCAMmask);
1165 rx_mode = RCR_AM | RCR_AB | RCR_AP;
1167 if (dev->mtu > 1500)
1170 BYTE_REG_BITS_ON(rx_mode, ®s->RCR);
1175 * MII access , media link mode setting functions
1179 * mii_init - set up MII
1180 * @vptr: velocity adapter
1181 * @mii_status: links tatus
1183 * Set up the PHY for the current link state.
1185 static void mii_init(struct velocity_info *vptr, u32 mii_status)
1189 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
1190 case PHYID_ICPLUS_IP101A:
1191 MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP),
1192 MII_ADVERTISE, vptr->mac_regs);
1193 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1194 MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION,
1197 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION,
1199 MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
1201 case PHYID_CICADA_CS8201:
1203 * Reset to hardware default
1205 MII_REG_BITS_OFF((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1207 * Turn on ECHODIS bit in NWay-forced full mode and turn it
1208 * off it in NWay-forced half mode for NWay-forced v.s.
1209 * legacy-forced issue.
1211 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1212 MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1214 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1216 * Turn on Link/Activity LED enable bit for CIS8201
1218 MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
1220 case PHYID_VT3216_32BIT:
1221 case PHYID_VT3216_64BIT:
1223 * Reset to hardware default
1225 MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1227 * Turn on ECHODIS bit in NWay-forced full mode and turn it
1228 * off it in NWay-forced half mode for NWay-forced v.s.
1229 * legacy-forced issue
1231 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1232 MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1234 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1237 case PHYID_MARVELL_1000:
1238 case PHYID_MARVELL_1000S:
1240 * Assert CRS on Transmit
1242 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
1244 * Reset to hardware default
1246 MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1251 velocity_mii_read(vptr->mac_regs, MII_BMCR, &BMCR);
1252 if (BMCR & BMCR_ISOLATE) {
1253 BMCR &= ~BMCR_ISOLATE;
1254 velocity_mii_write(vptr->mac_regs, MII_BMCR, BMCR);
1259 * setup_queue_timers - Setup interrupt timers
1261 * Setup interrupt frequency during suppression (timeout if the frame
1262 * count isn't filled).
1264 static void setup_queue_timers(struct velocity_info *vptr)
1266 /* Only for newer revisions */
1267 if (vptr->rev_id >= REV_ID_VT3216_A0) {
1268 u8 txqueue_timer = 0;
1269 u8 rxqueue_timer = 0;
1271 if (vptr->mii_status & (VELOCITY_SPEED_1000 |
1272 VELOCITY_SPEED_100)) {
1273 txqueue_timer = vptr->options.txqueue_timer;
1274 rxqueue_timer = vptr->options.rxqueue_timer;
1277 writeb(txqueue_timer, &vptr->mac_regs->TQETMR);
1278 writeb(rxqueue_timer, &vptr->mac_regs->RQETMR);
1283 * setup_adaptive_interrupts - Setup interrupt suppression
1285 * @vptr velocity adapter
1287 * The velocity is able to suppress interrupt during high interrupt load.
1288 * This function turns on that feature.
1290 static void setup_adaptive_interrupts(struct velocity_info *vptr)
1292 struct mac_regs __iomem *regs = vptr->mac_regs;
1293 u16 tx_intsup = vptr->options.tx_intsup;
1294 u16 rx_intsup = vptr->options.rx_intsup;
1296 /* Setup default interrupt mask (will be changed below) */
1297 vptr->int_mask = INT_MASK_DEF;
1299 /* Set Tx Interrupt Suppression Threshold */
1300 writeb(CAMCR_PS0, ®s->CAMCR);
1301 if (tx_intsup != 0) {
1302 vptr->int_mask &= ~(ISR_PTXI | ISR_PTX0I | ISR_PTX1I |
1303 ISR_PTX2I | ISR_PTX3I);
1304 writew(tx_intsup, ®s->ISRCTL);
1306 writew(ISRCTL_TSUPDIS, ®s->ISRCTL);
1308 /* Set Rx Interrupt Suppression Threshold */
1309 writeb(CAMCR_PS1, ®s->CAMCR);
1310 if (rx_intsup != 0) {
1311 vptr->int_mask &= ~ISR_PRXI;
1312 writew(rx_intsup, ®s->ISRCTL);
1314 writew(ISRCTL_RSUPDIS, ®s->ISRCTL);
1316 /* Select page to interrupt hold timer */
1317 writeb(0, ®s->CAMCR);
1321 * velocity_init_registers - initialise MAC registers
1322 * @vptr: velocity to init
1323 * @type: type of initialisation (hot or cold)
1325 * Initialise the MAC on a reset or on first set up on the
1328 static void velocity_init_registers(struct velocity_info *vptr,
1329 enum velocity_init_type type)
1331 struct mac_regs __iomem *regs = vptr->mac_regs;
1332 struct net_device *netdev = vptr->netdev;
1335 mac_wol_reset(regs);
1338 case VELOCITY_INIT_RESET:
1339 case VELOCITY_INIT_WOL:
1341 netif_stop_queue(netdev);
1344 * Reset RX to prevent RX pointer not on the 4X location
1346 velocity_rx_reset(vptr);
1347 mac_rx_queue_run(regs);
1348 mac_rx_queue_wake(regs);
1350 mii_status = velocity_get_opt_media_mode(vptr);
1351 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
1352 velocity_print_link_status(vptr);
1353 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
1354 netif_wake_queue(netdev);
1357 enable_flow_control_ability(vptr);
1359 mac_clear_isr(regs);
1360 writel(CR0_STOP, ®s->CR0Clr);
1361 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
1366 case VELOCITY_INIT_COLD:
1371 velocity_soft_reset(vptr);
1374 if (!vptr->no_eeprom) {
1375 mac_eeprom_reload(regs);
1376 for (i = 0; i < 6; i++)
1377 writeb(netdev->dev_addr[i], regs->PAR + i);
1381 * clear Pre_ACPI bit.
1383 BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
1384 mac_set_rx_thresh(regs, vptr->options.rx_thresh);
1385 mac_set_dma_length(regs, vptr->options.DMA_length);
1387 writeb(WOLCFG_SAM | WOLCFG_SAB, ®s->WOLCFGSet);
1389 * Back off algorithm use original IEEE standard
1391 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), ®s->CFGB);
1396 velocity_init_cam_filter(vptr);
1399 * Set packet filter: Receive directed and broadcast address
1401 velocity_set_multi(netdev);
1404 * Enable MII auto-polling
1406 enable_mii_autopoll(regs);
1408 setup_adaptive_interrupts(vptr);
1410 writel(vptr->rx.pool_dma, ®s->RDBaseLo);
1411 writew(vptr->options.numrx - 1, ®s->RDCSize);
1412 mac_rx_queue_run(regs);
1413 mac_rx_queue_wake(regs);
1415 writew(vptr->options.numtx - 1, ®s->TDCSize);
1417 for (i = 0; i < vptr->tx.numq; i++) {
1418 writel(vptr->tx.pool_dma[i], ®s->TDBaseLo[i]);
1419 mac_tx_queue_run(regs, i);
1422 init_flow_control_register(vptr);
1424 writel(CR0_STOP, ®s->CR0Clr);
1425 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), ®s->CR0Set);
1427 mii_status = velocity_get_opt_media_mode(vptr);
1428 netif_stop_queue(netdev);
1430 mii_init(vptr, mii_status);
1432 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
1433 velocity_print_link_status(vptr);
1434 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
1435 netif_wake_queue(netdev);
1438 enable_flow_control_ability(vptr);
1439 mac_hw_mibs_init(regs);
1440 mac_write_int_mask(vptr->int_mask, regs);
1441 mac_clear_isr(regs);
1446 static void velocity_give_many_rx_descs(struct velocity_info *vptr)
1448 struct mac_regs __iomem *regs = vptr->mac_regs;
1449 int avail, dirty, unusable;
1452 * RD number must be equal to 4X per hardware spec
1453 * (programming guide rev 1.20, p.13)
1455 if (vptr->rx.filled < 4)
1460 unusable = vptr->rx.filled & 0x0003;
1461 dirty = vptr->rx.dirty - unusable;
1462 for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
1463 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1464 vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
1467 writew(vptr->rx.filled & 0xfffc, ®s->RBRDU);
1468 vptr->rx.filled = unusable;
1472 * velocity_init_dma_rings - set up DMA rings
1473 * @vptr: Velocity to set up
1475 * Allocate PCI mapped DMA rings for the receive and transmit layer
1478 static int velocity_init_dma_rings(struct velocity_info *vptr)
1480 struct velocity_opt *opt = &vptr->options;
1481 const unsigned int rx_ring_size = opt->numrx * sizeof(struct rx_desc);
1482 const unsigned int tx_ring_size = opt->numtx * sizeof(struct tx_desc);
1483 dma_addr_t pool_dma;
1488 * Allocate all RD/TD rings a single pool.
1490 * dma_alloc_coherent() fulfills the requirement for 64 bytes
1493 pool = dma_alloc_coherent(vptr->dev, tx_ring_size * vptr->tx.numq +
1494 rx_ring_size, &pool_dma, GFP_ATOMIC);
1496 dev_err(vptr->dev, "%s : DMA memory allocation failed.\n",
1497 vptr->netdev->name);
1501 vptr->rx.ring = pool;
1502 vptr->rx.pool_dma = pool_dma;
1504 pool += rx_ring_size;
1505 pool_dma += rx_ring_size;
1507 for (i = 0; i < vptr->tx.numq; i++) {
1508 vptr->tx.rings[i] = pool;
1509 vptr->tx.pool_dma[i] = pool_dma;
1510 pool += tx_ring_size;
1511 pool_dma += tx_ring_size;
1517 static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
1519 vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
1523 * velocity_alloc_rx_buf - allocate aligned receive buffer
1527 * Allocate a new full sized buffer for the reception of a frame and
1528 * map it into PCI space for the hardware to use. The hardware
1529 * requires *64* byte alignment of the buffer which makes life
1530 * less fun than would be ideal.
1532 static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1534 struct rx_desc *rd = &(vptr->rx.ring[idx]);
1535 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
1537 rd_info->skb = netdev_alloc_skb(vptr->netdev, vptr->rx.buf_sz + 64);
1538 if (rd_info->skb == NULL)
1542 * Do the gymnastics to get the buffer head for data at
1545 skb_reserve(rd_info->skb,
1546 64 - ((unsigned long) rd_info->skb->data & 63));
1547 rd_info->skb_dma = dma_map_single(vptr->dev, rd_info->skb->data,
1548 vptr->rx.buf_sz, DMA_FROM_DEVICE);
1551 * Fill in the descriptor to match
1554 *((u32 *) & (rd->rdesc0)) = 0;
1555 rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
1556 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1562 static int velocity_rx_refill(struct velocity_info *vptr)
1564 int dirty = vptr->rx.dirty, done = 0;
1567 struct rx_desc *rd = vptr->rx.ring + dirty;
1569 /* Fine for an all zero Rx desc at init time as well */
1570 if (rd->rdesc0.len & OWNED_BY_NIC)
1573 if (!vptr->rx.info[dirty].skb) {
1574 if (velocity_alloc_rx_buf(vptr, dirty) < 0)
1578 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
1579 } while (dirty != vptr->rx.curr);
1582 vptr->rx.dirty = dirty;
1583 vptr->rx.filled += done;
1590 * velocity_free_rd_ring - free receive ring
1591 * @vptr: velocity to clean up
1593 * Free the receive buffers for each ring slot and any
1594 * attached socket buffers that need to go away.
1596 static void velocity_free_rd_ring(struct velocity_info *vptr)
1600 if (vptr->rx.info == NULL)
1603 for (i = 0; i < vptr->options.numrx; i++) {
1604 struct velocity_rd_info *rd_info = &(vptr->rx.info[i]);
1605 struct rx_desc *rd = vptr->rx.ring + i;
1607 memset(rd, 0, sizeof(*rd));
1611 dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
1613 rd_info->skb_dma = 0;
1615 dev_kfree_skb(rd_info->skb);
1616 rd_info->skb = NULL;
1619 kfree(vptr->rx.info);
1620 vptr->rx.info = NULL;
1624 * velocity_init_rd_ring - set up receive ring
1625 * @vptr: velocity to configure
1627 * Allocate and set up the receive buffers for each ring slot and
1628 * assign them to the network adapter.
1630 static int velocity_init_rd_ring(struct velocity_info *vptr)
1634 vptr->rx.info = kcalloc(vptr->options.numrx,
1635 sizeof(struct velocity_rd_info), GFP_KERNEL);
1639 velocity_init_rx_ring_indexes(vptr);
1641 if (velocity_rx_refill(vptr) != vptr->options.numrx) {
1642 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1643 "%s: failed to allocate RX buffer.\n", vptr->netdev->name);
1644 velocity_free_rd_ring(vptr);
1654 * velocity_init_td_ring - set up transmit ring
1657 * Set up the transmit ring and chain the ring pointers together.
1658 * Returns zero on success or a negative posix errno code for
1661 static int velocity_init_td_ring(struct velocity_info *vptr)
1665 /* Init the TD ring entries */
1666 for (j = 0; j < vptr->tx.numq; j++) {
1668 vptr->tx.infos[j] = kcalloc(vptr->options.numtx,
1669 sizeof(struct velocity_td_info),
1671 if (!vptr->tx.infos[j]) {
1673 kfree(vptr->tx.infos[j]);
1677 vptr->tx.tail[j] = vptr->tx.curr[j] = vptr->tx.used[j] = 0;
1683 * velocity_free_dma_rings - free PCI ring pointers
1684 * @vptr: Velocity to free from
1686 * Clean up the PCI ring buffers allocated to this velocity.
1688 static void velocity_free_dma_rings(struct velocity_info *vptr)
1690 const int size = vptr->options.numrx * sizeof(struct rx_desc) +
1691 vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;
1693 dma_free_coherent(vptr->dev, size, vptr->rx.ring, vptr->rx.pool_dma);
1696 static int velocity_init_rings(struct velocity_info *vptr, int mtu)
1700 velocity_set_rxbufsize(vptr, mtu);
1702 ret = velocity_init_dma_rings(vptr);
1706 ret = velocity_init_rd_ring(vptr);
1708 goto err_free_dma_rings_0;
1710 ret = velocity_init_td_ring(vptr);
1712 goto err_free_rd_ring_1;
1717 velocity_free_rd_ring(vptr);
1718 err_free_dma_rings_0:
1719 velocity_free_dma_rings(vptr);
1724 * velocity_free_tx_buf - free transmit buffer
1728 * Release an transmit buffer. If the buffer was preallocated then
1729 * recycle it, if not then unmap the buffer.
1731 static void velocity_free_tx_buf(struct velocity_info *vptr,
1732 struct velocity_td_info *tdinfo, struct tx_desc *td)
1734 struct sk_buff *skb = tdinfo->skb;
1737 * Don't unmap the pre-allocated tx_bufs
1739 if (tdinfo->skb_dma) {
1742 for (i = 0; i < tdinfo->nskb_dma; i++) {
1743 size_t pktlen = max_t(size_t, skb->len, ETH_ZLEN);
1745 /* For scatter-gather */
1746 if (skb_shinfo(skb)->nr_frags > 0)
1747 pktlen = max_t(size_t, pktlen,
1748 td->td_buf[i].size & ~TD_QUEUE);
1750 dma_unmap_single(vptr->dev, tdinfo->skb_dma[i],
1751 le16_to_cpu(pktlen), DMA_TO_DEVICE);
1754 dev_kfree_skb_irq(skb);
1759 * FIXME: could we merge this with velocity_free_tx_buf ?
1761 static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1764 struct velocity_td_info *td_info = &(vptr->tx.infos[q][n]);
1767 if (td_info == NULL)
1771 for (i = 0; i < td_info->nskb_dma; i++) {
1772 if (td_info->skb_dma[i]) {
1773 dma_unmap_single(vptr->dev, td_info->skb_dma[i],
1774 td_info->skb->len, DMA_TO_DEVICE);
1775 td_info->skb_dma[i] = 0;
1778 dev_kfree_skb(td_info->skb);
1779 td_info->skb = NULL;
1784 * velocity_free_td_ring - free td ring
1787 * Free up the transmit ring for this particular velocity adapter.
1788 * We free the ring contents but not the ring itself.
1790 static void velocity_free_td_ring(struct velocity_info *vptr)
1794 for (j = 0; j < vptr->tx.numq; j++) {
1795 if (vptr->tx.infos[j] == NULL)
1797 for (i = 0; i < vptr->options.numtx; i++)
1798 velocity_free_td_ring_entry(vptr, j, i);
1800 kfree(vptr->tx.infos[j]);
1801 vptr->tx.infos[j] = NULL;
1805 static void velocity_free_rings(struct velocity_info *vptr)
1807 velocity_free_td_ring(vptr);
1808 velocity_free_rd_ring(vptr);
1809 velocity_free_dma_rings(vptr);
1813 * velocity_error - handle error from controller
1815 * @status: card status
1817 * Process an error report from the hardware and attempt to recover
1818 * the card itself. At the moment we cannot recover from some
1819 * theoretically impossible errors but this could be fixed using
1820 * the pci_device_failed logic to bounce the hardware
1823 static void velocity_error(struct velocity_info *vptr, int status)
1826 if (status & ISR_TXSTLI) {
1827 struct mac_regs __iomem *regs = vptr->mac_regs;
1829 printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(®s->TDIdx[0]));
1830 BYTE_REG_BITS_ON(TXESR_TDSTR, ®s->TXESR);
1831 writew(TRDCSR_RUN, ®s->TDCSRClr);
1832 netif_stop_queue(vptr->netdev);
1834 /* FIXME: port over the pci_device_failed code and use it
1838 if (status & ISR_SRCI) {
1839 struct mac_regs __iomem *regs = vptr->mac_regs;
1842 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1843 vptr->mii_status = check_connection_type(regs);
1846 * If it is a 3119, disable frame bursting in
1847 * halfduplex mode and enable it in fullduplex
1850 if (vptr->rev_id < REV_ID_VT3216_A0) {
1851 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1852 BYTE_REG_BITS_ON(TCR_TB2BDIS, ®s->TCR);
1854 BYTE_REG_BITS_OFF(TCR_TB2BDIS, ®s->TCR);
1857 * Only enable CD heart beat counter in 10HD mode
1859 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10))
1860 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, ®s->TESTCFG);
1862 BYTE_REG_BITS_ON(TESTCFG_HBDIS, ®s->TESTCFG);
1864 setup_queue_timers(vptr);
1867 * Get link status from PHYSR0
1869 linked = readb(®s->PHYSR0) & PHYSR0_LINKGD;
1872 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1873 netif_carrier_on(vptr->netdev);
1875 vptr->mii_status |= VELOCITY_LINK_FAIL;
1876 netif_carrier_off(vptr->netdev);
1879 velocity_print_link_status(vptr);
1880 enable_flow_control_ability(vptr);
1883 * Re-enable auto-polling because SRCI will disable
1887 enable_mii_autopoll(regs);
1889 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1890 netif_stop_queue(vptr->netdev);
1892 netif_wake_queue(vptr->netdev);
1895 if (status & ISR_MIBFI)
1896 velocity_update_hw_mibs(vptr);
1897 if (status & ISR_LSTEI)
1898 mac_rx_queue_wake(vptr->mac_regs);
1902 * tx_srv - transmit interrupt service
1905 * Scan the queues looking for transmitted packets that
1906 * we can complete and clean up. Update any statistics as
1909 static int velocity_tx_srv(struct velocity_info *vptr)
1916 struct velocity_td_info *tdinfo;
1917 struct net_device_stats *stats = &vptr->netdev->stats;
1919 for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
1920 for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
1921 idx = (idx + 1) % vptr->options.numtx) {
1926 td = &(vptr->tx.rings[qnum][idx]);
1927 tdinfo = &(vptr->tx.infos[qnum][idx]);
1929 if (td->tdesc0.len & OWNED_BY_NIC)
1935 if (td->tdesc0.TSR & TSR0_TERR) {
1937 stats->tx_dropped++;
1938 if (td->tdesc0.TSR & TSR0_CDH)
1939 stats->tx_heartbeat_errors++;
1940 if (td->tdesc0.TSR & TSR0_CRS)
1941 stats->tx_carrier_errors++;
1942 if (td->tdesc0.TSR & TSR0_ABT)
1943 stats->tx_aborted_errors++;
1944 if (td->tdesc0.TSR & TSR0_OWC)
1945 stats->tx_window_errors++;
1947 stats->tx_packets++;
1948 stats->tx_bytes += tdinfo->skb->len;
1950 velocity_free_tx_buf(vptr, tdinfo, td);
1951 vptr->tx.used[qnum]--;
1953 vptr->tx.tail[qnum] = idx;
1955 if (AVAIL_TD(vptr, qnum) < 1)
1959 * Look to see if we should kick the transmit network
1960 * layer for more work.
1962 if (netif_queue_stopped(vptr->netdev) && (full == 0) &&
1963 (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1964 netif_wake_queue(vptr->netdev);
1970 * velocity_rx_csum - checksum process
1971 * @rd: receive packet descriptor
1972 * @skb: network layer packet buffer
1974 * Process the status bits for the received packet and determine
1975 * if the checksum was computed and verified by the hardware
1977 static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1979 skb_checksum_none_assert(skb);
1981 if (rd->rdesc1.CSM & CSM_IPKT) {
1982 if (rd->rdesc1.CSM & CSM_IPOK) {
1983 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
1984 (rd->rdesc1.CSM & CSM_UDPKT)) {
1985 if (!(rd->rdesc1.CSM & CSM_TUPOK))
1988 skb->ip_summed = CHECKSUM_UNNECESSARY;
1994 * velocity_rx_copy - in place Rx copy for small packets
1995 * @rx_skb: network layer packet buffer candidate
1996 * @pkt_size: received data size
1997 * @rd: receive packet descriptor
1998 * @dev: network device
2000 * Replace the current skb that is scheduled for Rx processing by a
2001 * shorter, immediately allocated skb, if the received packet is small
2002 * enough. This function returns a negative value if the received
2003 * packet is too big or if memory is exhausted.
2005 static int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
2006 struct velocity_info *vptr)
2009 if (pkt_size < rx_copybreak) {
2010 struct sk_buff *new_skb;
2012 new_skb = netdev_alloc_skb_ip_align(vptr->netdev, pkt_size);
2014 new_skb->ip_summed = rx_skb[0]->ip_summed;
2015 skb_copy_from_linear_data(*rx_skb, new_skb->data, pkt_size);
2025 * velocity_iph_realign - IP header alignment
2026 * @vptr: velocity we are handling
2027 * @skb: network layer packet buffer
2028 * @pkt_size: received data size
2030 * Align IP header on a 2 bytes boundary. This behavior can be
2031 * configured by the user.
2033 static inline void velocity_iph_realign(struct velocity_info *vptr,
2034 struct sk_buff *skb, int pkt_size)
2036 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
2037 memmove(skb->data + 2, skb->data, pkt_size);
2038 skb_reserve(skb, 2);
2043 * velocity_receive_frame - received packet processor
2044 * @vptr: velocity we are handling
2047 * A packet has arrived. We process the packet and if appropriate
2048 * pass the frame up the network stack
2050 static int velocity_receive_frame(struct velocity_info *vptr, int idx)
2052 struct net_device_stats *stats = &vptr->netdev->stats;
2053 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
2054 struct rx_desc *rd = &(vptr->rx.ring[idx]);
2055 int pkt_len = le16_to_cpu(rd->rdesc0.len) & 0x3fff;
2056 struct sk_buff *skb;
2058 if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
2059 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->netdev->name);
2060 stats->rx_length_errors++;
2064 if (rd->rdesc0.RSR & RSR_MAR)
2069 dma_sync_single_for_cpu(vptr->dev, rd_info->skb_dma,
2070 vptr->rx.buf_sz, DMA_FROM_DEVICE);
2073 * Drop frame not meeting IEEE 802.3
2076 if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
2077 if (rd->rdesc0.RSR & RSR_RL) {
2078 stats->rx_length_errors++;
2083 velocity_rx_csum(rd, skb);
2085 if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
2086 velocity_iph_realign(vptr, skb, pkt_len);
2087 rd_info->skb = NULL;
2088 dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
2091 dma_sync_single_for_device(vptr->dev, rd_info->skb_dma,
2092 vptr->rx.buf_sz, DMA_FROM_DEVICE);
2095 skb_put(skb, pkt_len - 4);
2096 skb->protocol = eth_type_trans(skb, vptr->netdev);
2098 if (rd->rdesc0.RSR & RSR_DETAG) {
2099 u16 vid = swab16(le16_to_cpu(rd->rdesc1.PQTAG));
2101 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
2103 netif_receive_skb(skb);
2105 stats->rx_bytes += pkt_len;
2106 stats->rx_packets++;
2112 * velocity_rx_srv - service RX interrupt
2115 * Walk the receive ring of the velocity adapter and remove
2116 * any received packets from the receive queue. Hand the ring
2117 * slots back to the adapter for reuse.
2119 static int velocity_rx_srv(struct velocity_info *vptr, int budget_left)
2121 struct net_device_stats *stats = &vptr->netdev->stats;
2122 int rd_curr = vptr->rx.curr;
2125 while (works < budget_left) {
2126 struct rx_desc *rd = vptr->rx.ring + rd_curr;
2128 if (!vptr->rx.info[rd_curr].skb)
2131 if (rd->rdesc0.len & OWNED_BY_NIC)
2137 * Don't drop CE or RL error frame although RXOK is off
2139 if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
2140 if (velocity_receive_frame(vptr, rd_curr) < 0)
2141 stats->rx_dropped++;
2143 if (rd->rdesc0.RSR & RSR_CRC)
2144 stats->rx_crc_errors++;
2145 if (rd->rdesc0.RSR & RSR_FAE)
2146 stats->rx_frame_errors++;
2148 stats->rx_dropped++;
2151 rd->size |= RX_INTEN;
2154 if (rd_curr >= vptr->options.numrx)
2159 vptr->rx.curr = rd_curr;
2161 if ((works > 0) && (velocity_rx_refill(vptr) > 0))
2162 velocity_give_many_rx_descs(vptr);
2168 static int velocity_poll(struct napi_struct *napi, int budget)
2170 struct velocity_info *vptr = container_of(napi,
2171 struct velocity_info, napi);
2172 unsigned int rx_done;
2173 unsigned long flags;
2176 * Do rx and tx twice for performance (taken from the VIA
2177 * out-of-tree driver).
2179 rx_done = velocity_rx_srv(vptr, budget);
2180 spin_lock_irqsave(&vptr->lock, flags);
2181 velocity_tx_srv(vptr);
2182 /* If budget not fully consumed, exit the polling mode */
2183 if (rx_done < budget) {
2184 napi_complete(napi);
2185 mac_enable_int(vptr->mac_regs);
2187 spin_unlock_irqrestore(&vptr->lock, flags);
2193 * velocity_intr - interrupt callback
2194 * @irq: interrupt number
2195 * @dev_instance: interrupting device
2197 * Called whenever an interrupt is generated by the velocity
2198 * adapter IRQ line. We may not be the source of the interrupt
2199 * and need to identify initially if we are, and if not exit as
2200 * efficiently as possible.
2202 static irqreturn_t velocity_intr(int irq, void *dev_instance)
2204 struct net_device *dev = dev_instance;
2205 struct velocity_info *vptr = netdev_priv(dev);
2208 spin_lock(&vptr->lock);
2209 isr_status = mac_read_isr(vptr->mac_regs);
2212 if (isr_status == 0) {
2213 spin_unlock(&vptr->lock);
2217 /* Ack the interrupt */
2218 mac_write_isr(vptr->mac_regs, isr_status);
2220 if (likely(napi_schedule_prep(&vptr->napi))) {
2221 mac_disable_int(vptr->mac_regs);
2222 __napi_schedule(&vptr->napi);
2225 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2226 velocity_error(vptr, isr_status);
2228 spin_unlock(&vptr->lock);
2234 * velocity_open - interface activation callback
2235 * @dev: network layer device to open
2237 * Called when the network layer brings the interface up. Returns
2238 * a negative posix error code on failure, or zero on success.
2240 * All the ring allocation and set up is done on open for this
2241 * adapter to minimise memory usage when inactive
2243 static int velocity_open(struct net_device *dev)
2245 struct velocity_info *vptr = netdev_priv(dev);
2248 ret = velocity_init_rings(vptr, dev->mtu);
2252 /* Ensure chip is running */
2253 velocity_set_power_state(vptr, PCI_D0);
2255 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
2257 ret = request_irq(dev->irq, velocity_intr, IRQF_SHARED,
2260 /* Power down the chip */
2261 velocity_set_power_state(vptr, PCI_D3hot);
2262 velocity_free_rings(vptr);
2266 velocity_give_many_rx_descs(vptr);
2268 mac_enable_int(vptr->mac_regs);
2269 netif_start_queue(dev);
2270 napi_enable(&vptr->napi);
2271 vptr->flags |= VELOCITY_FLAGS_OPENED;
2277 * velocity_shutdown - shut down the chip
2278 * @vptr: velocity to deactivate
2280 * Shuts down the internal operations of the velocity and
2281 * disables interrupts, autopolling, transmit and receive
2283 static void velocity_shutdown(struct velocity_info *vptr)
2285 struct mac_regs __iomem *regs = vptr->mac_regs;
2286 mac_disable_int(regs);
2287 writel(CR0_STOP, ®s->CR0Set);
2288 writew(0xFFFF, ®s->TDCSRClr);
2289 writeb(0xFF, ®s->RDCSRClr);
2290 safe_disable_mii_autopoll(regs);
2291 mac_clear_isr(regs);
2295 * velocity_change_mtu - MTU change callback
2296 * @dev: network device
2297 * @new_mtu: desired MTU
2299 * Handle requests from the networking layer for MTU change on
2300 * this interface. It gets called on a change by the network layer.
2301 * Return zero for success or negative posix error code.
2303 static int velocity_change_mtu(struct net_device *dev, int new_mtu)
2305 struct velocity_info *vptr = netdev_priv(dev);
2308 if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
2309 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
2310 vptr->netdev->name);
2315 if (!netif_running(dev)) {
2320 if (dev->mtu != new_mtu) {
2321 struct velocity_info *tmp_vptr;
2322 unsigned long flags;
2326 tmp_vptr = kzalloc(sizeof(*tmp_vptr), GFP_KERNEL);
2332 tmp_vptr->netdev = dev;
2333 tmp_vptr->pdev = vptr->pdev;
2334 tmp_vptr->dev = vptr->dev;
2335 tmp_vptr->options = vptr->options;
2336 tmp_vptr->tx.numq = vptr->tx.numq;
2338 ret = velocity_init_rings(tmp_vptr, new_mtu);
2340 goto out_free_tmp_vptr_1;
2342 napi_disable(&vptr->napi);
2344 spin_lock_irqsave(&vptr->lock, flags);
2346 netif_stop_queue(dev);
2347 velocity_shutdown(vptr);
2352 vptr->rx = tmp_vptr->rx;
2353 vptr->tx = tmp_vptr->tx;
2360 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
2362 velocity_give_many_rx_descs(vptr);
2364 napi_enable(&vptr->napi);
2366 mac_enable_int(vptr->mac_regs);
2367 netif_start_queue(dev);
2369 spin_unlock_irqrestore(&vptr->lock, flags);
2371 velocity_free_rings(tmp_vptr);
2373 out_free_tmp_vptr_1:
2380 #ifdef CONFIG_NET_POLL_CONTROLLER
2382 * velocity_poll_controller - Velocity Poll controller function
2383 * @dev: network device
2386 * Used by NETCONSOLE and other diagnostic tools to allow network I/P
2387 * with interrupts disabled.
2389 static void velocity_poll_controller(struct net_device *dev)
2391 disable_irq(dev->irq);
2392 velocity_intr(dev->irq, dev);
2393 enable_irq(dev->irq);
2398 * velocity_mii_ioctl - MII ioctl handler
2399 * @dev: network device
2400 * @ifr: the ifreq block for the ioctl
2403 * Process MII requests made via ioctl from the network layer. These
2404 * are used by tools like kudzu to interrogate the link state of the
2407 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2409 struct velocity_info *vptr = netdev_priv(dev);
2410 struct mac_regs __iomem *regs = vptr->mac_regs;
2411 unsigned long flags;
2412 struct mii_ioctl_data *miidata = if_mii(ifr);
2417 miidata->phy_id = readb(®s->MIIADR) & 0x1f;
2420 if (velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
2424 spin_lock_irqsave(&vptr->lock, flags);
2425 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
2426 spin_unlock_irqrestore(&vptr->lock, flags);
2427 check_connection_type(vptr->mac_regs);
2438 * velocity_ioctl - ioctl entry point
2439 * @dev: network device
2440 * @rq: interface request ioctl
2441 * @cmd: command code
2443 * Called when the user issues an ioctl request to the network
2444 * device in question. The velocity interface supports MII.
2446 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2448 struct velocity_info *vptr = netdev_priv(dev);
2451 /* If we are asked for information and the device is power
2452 saving then we need to bring the device back up to talk to it */
2454 if (!netif_running(dev))
2455 velocity_set_power_state(vptr, PCI_D0);
2458 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2459 case SIOCGMIIREG: /* Read MII PHY register. */
2460 case SIOCSMIIREG: /* Write to MII PHY register. */
2461 ret = velocity_mii_ioctl(dev, rq, cmd);
2467 if (!netif_running(dev))
2468 velocity_set_power_state(vptr, PCI_D3hot);
2475 * velocity_get_status - statistics callback
2476 * @dev: network device
2478 * Callback from the network layer to allow driver statistics
2479 * to be resynchronized with hardware collected state. In the
2480 * case of the velocity we need to pull the MIB counters from
2481 * the hardware into the counters before letting the network
2482 * layer display them.
2484 static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2486 struct velocity_info *vptr = netdev_priv(dev);
2488 /* If the hardware is down, don't touch MII */
2489 if (!netif_running(dev))
2492 spin_lock_irq(&vptr->lock);
2493 velocity_update_hw_mibs(vptr);
2494 spin_unlock_irq(&vptr->lock);
2496 dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2497 dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2498 dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2500 // unsigned long rx_dropped; /* no space in linux buffers */
2501 dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2502 /* detailed rx_errors: */
2503 // unsigned long rx_length_errors;
2504 // unsigned long rx_over_errors; /* receiver ring buff overflow */
2505 dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2506 // unsigned long rx_frame_errors; /* recv'd frame alignment error */
2507 // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2508 // unsigned long rx_missed_errors; /* receiver missed packet */
2510 /* detailed tx_errors */
2511 // unsigned long tx_fifo_errors;
2517 * velocity_close - close adapter callback
2518 * @dev: network device
2520 * Callback from the network layer when the velocity is being
2521 * deactivated by the network layer
2523 static int velocity_close(struct net_device *dev)
2525 struct velocity_info *vptr = netdev_priv(dev);
2527 napi_disable(&vptr->napi);
2528 netif_stop_queue(dev);
2529 velocity_shutdown(vptr);
2531 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
2532 velocity_get_ip(vptr);
2534 free_irq(dev->irq, dev);
2536 velocity_free_rings(vptr);
2538 vptr->flags &= (~VELOCITY_FLAGS_OPENED);
2543 * velocity_xmit - transmit packet callback
2544 * @skb: buffer to transmit
2545 * @dev: network device
2547 * Called by the networ layer to request a packet is queued to
2548 * the velocity. Returns zero on success.
2550 static netdev_tx_t velocity_xmit(struct sk_buff *skb,
2551 struct net_device *dev)
2553 struct velocity_info *vptr = netdev_priv(dev);
2555 struct tx_desc *td_ptr;
2556 struct velocity_td_info *tdinfo;
2557 unsigned long flags;
2562 if (skb_padto(skb, ETH_ZLEN))
2565 /* The hardware can handle at most 7 memory segments, so merge
2566 * the skb if there are more */
2567 if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
2568 dev_kfree_skb_any(skb);
2569 return NETDEV_TX_OK;
2572 pktlen = skb_shinfo(skb)->nr_frags == 0 ?
2573 max_t(unsigned int, skb->len, ETH_ZLEN) :
2576 spin_lock_irqsave(&vptr->lock, flags);
2578 index = vptr->tx.curr[qnum];
2579 td_ptr = &(vptr->tx.rings[qnum][index]);
2580 tdinfo = &(vptr->tx.infos[qnum][index]);
2582 td_ptr->tdesc1.TCR = TCR0_TIC;
2583 td_ptr->td_buf[0].size &= ~TD_QUEUE;
2586 * Map the linear network buffer into PCI space and
2587 * add it to the transmit ring.
2590 tdinfo->skb_dma[0] = dma_map_single(vptr->dev, skb->data, pktlen,
2592 td_ptr->tdesc0.len = cpu_to_le16(pktlen);
2593 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
2594 td_ptr->td_buf[0].pa_high = 0;
2595 td_ptr->td_buf[0].size = cpu_to_le16(pktlen);
2597 /* Handle fragments */
2598 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2599 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2601 tdinfo->skb_dma[i + 1] = skb_frag_dma_map(vptr->dev,
2603 skb_frag_size(frag),
2606 td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
2607 td_ptr->td_buf[i + 1].pa_high = 0;
2608 td_ptr->td_buf[i + 1].size = cpu_to_le16(skb_frag_size(frag));
2610 tdinfo->nskb_dma = i + 1;
2612 td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;
2614 if (vlan_tx_tag_present(skb)) {
2615 td_ptr->tdesc1.vlan = cpu_to_le16(vlan_tx_tag_get(skb));
2616 td_ptr->tdesc1.TCR |= TCR0_VETAG;
2620 * Handle hardware checksum
2622 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2623 const struct iphdr *ip = ip_hdr(skb);
2624 if (ip->protocol == IPPROTO_TCP)
2625 td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2626 else if (ip->protocol == IPPROTO_UDP)
2627 td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2628 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2633 prev = vptr->options.numtx - 1;
2634 td_ptr->tdesc0.len |= OWNED_BY_NIC;
2635 vptr->tx.used[qnum]++;
2636 vptr->tx.curr[qnum] = (index + 1) % vptr->options.numtx;
2638 if (AVAIL_TD(vptr, qnum) < 1)
2639 netif_stop_queue(dev);
2641 td_ptr = &(vptr->tx.rings[qnum][prev]);
2642 td_ptr->td_buf[0].size |= TD_QUEUE;
2643 mac_tx_queue_wake(vptr->mac_regs, qnum);
2645 spin_unlock_irqrestore(&vptr->lock, flags);
2647 return NETDEV_TX_OK;
2650 static const struct net_device_ops velocity_netdev_ops = {
2651 .ndo_open = velocity_open,
2652 .ndo_stop = velocity_close,
2653 .ndo_start_xmit = velocity_xmit,
2654 .ndo_get_stats = velocity_get_stats,
2655 .ndo_validate_addr = eth_validate_addr,
2656 .ndo_set_mac_address = eth_mac_addr,
2657 .ndo_set_rx_mode = velocity_set_multi,
2658 .ndo_change_mtu = velocity_change_mtu,
2659 .ndo_do_ioctl = velocity_ioctl,
2660 .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
2661 .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
2662 #ifdef CONFIG_NET_POLL_CONTROLLER
2663 .ndo_poll_controller = velocity_poll_controller,
2668 * velocity_init_info - init private data
2670 * @vptr: Velocity info
2673 * Set up the initial velocity_info struct for the device that has been
2676 static void velocity_init_info(struct velocity_info *vptr,
2677 const struct velocity_info_tbl *info)
2679 vptr->chip_id = info->chip_id;
2680 vptr->tx.numq = info->txqueue;
2681 vptr->multicast_limit = MCAM_SIZE;
2682 spin_lock_init(&vptr->lock);
2686 * velocity_get_pci_info - retrieve PCI info for device
2687 * @vptr: velocity device
2688 * @pdev: PCI device it matches
2690 * Retrieve the PCI configuration space data that interests us from
2691 * the kernel PCI layer
2693 static int velocity_get_pci_info(struct velocity_info *vptr)
2695 struct pci_dev *pdev = vptr->pdev;
2697 pci_set_master(pdev);
2699 vptr->ioaddr = pci_resource_start(pdev, 0);
2700 vptr->memaddr = pci_resource_start(pdev, 1);
2702 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
2704 "region #0 is not an I/O resource, aborting.\n");
2708 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
2710 "region #1 is an I/O resource, aborting.\n");
2714 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
2715 dev_err(&pdev->dev, "region #1 is too small.\n");
2723 * velocity_get_platform_info - retrieve platform info for device
2724 * @vptr: velocity device
2725 * @pdev: platform device it matches
2727 * Retrieve the Platform configuration data that interests us
2729 static int velocity_get_platform_info(struct velocity_info *vptr)
2731 struct resource res;
2734 if (of_get_property(vptr->dev->of_node, "no-eeprom", NULL))
2735 vptr->no_eeprom = 1;
2737 ret = of_address_to_resource(vptr->dev->of_node, 0, &res);
2739 dev_err(vptr->dev, "unable to find memory address\n");
2743 vptr->memaddr = res.start;
2745 if (resource_size(&res) < VELOCITY_IO_SIZE) {
2746 dev_err(vptr->dev, "memory region is too small.\n");
2754 * velocity_print_info - per driver data
2757 * Print per driver data as the kernel driver finds Velocity
2760 static void velocity_print_info(struct velocity_info *vptr)
2762 struct net_device *dev = vptr->netdev;
2764 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
2765 printk(KERN_INFO "%s: Ethernet Address: %pM\n",
2766 dev->name, dev->dev_addr);
2769 static u32 velocity_get_link(struct net_device *dev)
2771 struct velocity_info *vptr = netdev_priv(dev);
2772 struct mac_regs __iomem *regs = vptr->mac_regs;
2773 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, ®s->PHYSR0) ? 1 : 0;
2777 * velocity_probe - set up discovered velocity device
2779 * @ent: PCI device table entry that matched
2780 * @bustype: bus that device is connected to
2782 * Configure a discovered adapter from scratch. Return a negative
2783 * errno error code on failure paths.
2785 static int velocity_probe(struct device *dev, int irq,
2786 const struct velocity_info_tbl *info,
2787 enum velocity_bus_type bustype)
2789 static int first = 1;
2790 struct net_device *netdev;
2792 const char *drv_string;
2793 struct velocity_info *vptr;
2794 struct mac_regs __iomem *regs;
2797 /* FIXME: this driver, like almost all other ethernet drivers,
2798 * can support more than MAX_UNITS.
2800 if (velocity_nics >= MAX_UNITS) {
2801 dev_notice(dev, "already found %d NICs.\n", velocity_nics);
2805 netdev = alloc_etherdev(sizeof(struct velocity_info));
2809 /* Chain it all together */
2811 SET_NETDEV_DEV(netdev, dev);
2812 vptr = netdev_priv(netdev);
2815 printk(KERN_INFO "%s Ver. %s\n",
2816 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
2817 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
2818 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
2823 vptr->netdev = netdev;
2826 velocity_init_info(vptr, info);
2828 if (bustype == BUS_PCI) {
2829 vptr->pdev = to_pci_dev(dev);
2831 ret = velocity_get_pci_info(vptr);
2836 ret = velocity_get_platform_info(vptr);
2841 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
2847 vptr->mac_regs = regs;
2848 vptr->rev_id = readb(®s->rev_id);
2850 mac_wol_reset(regs);
2852 for (i = 0; i < 6; i++)
2853 netdev->dev_addr[i] = readb(®s->PAR[i]);
2856 drv_string = dev_driver_string(dev);
2858 velocity_get_options(&vptr->options, velocity_nics, drv_string);
2861 * Mask out the options cannot be set to the chip
2864 vptr->options.flags &= info->flags;
2867 * Enable the chip specified capbilities
2870 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
2872 vptr->wol_opts = vptr->options.wol_opts;
2873 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2875 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
2877 netdev->netdev_ops = &velocity_netdev_ops;
2878 netdev->ethtool_ops = &velocity_ethtool_ops;
2879 netif_napi_add(netdev, &vptr->napi, velocity_poll,
2880 VELOCITY_NAPI_WEIGHT);
2882 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
2883 NETIF_F_HW_VLAN_CTAG_TX;
2884 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2885 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX |
2888 ret = register_netdev(netdev);
2892 if (!velocity_get_link(netdev)) {
2893 netif_carrier_off(netdev);
2894 vptr->mii_status |= VELOCITY_LINK_FAIL;
2897 velocity_print_info(vptr);
2898 dev_set_drvdata(vptr->dev, netdev);
2900 /* and leave the chip powered down */
2902 velocity_set_power_state(vptr, PCI_D3hot);
2908 netif_napi_del(&vptr->napi);
2911 free_netdev(netdev);
2916 * velocity_remove - device unplug
2917 * @dev: device being removed
2919 * Device unload callback. Called on an unplug or on module
2920 * unload for each active device that is present. Disconnects
2921 * the device from the network layer and frees all the resources
2923 static int velocity_remove(struct device *dev)
2925 struct net_device *netdev = dev_get_drvdata(dev);
2926 struct velocity_info *vptr = netdev_priv(netdev);
2928 unregister_netdev(netdev);
2929 netif_napi_del(&vptr->napi);
2930 iounmap(vptr->mac_regs);
2931 free_netdev(netdev);
2937 static int velocity_pci_probe(struct pci_dev *pdev,
2938 const struct pci_device_id *ent)
2940 const struct velocity_info_tbl *info =
2941 &chip_info_table[ent->driver_data];
2944 ret = pci_enable_device(pdev);
2948 ret = pci_request_regions(pdev, VELOCITY_NAME);
2950 dev_err(&pdev->dev, "No PCI resources.\n");
2954 ret = velocity_probe(&pdev->dev, pdev->irq, info, BUS_PCI);
2958 pci_release_regions(pdev);
2960 pci_disable_device(pdev);
2964 static void velocity_pci_remove(struct pci_dev *pdev)
2966 velocity_remove(&pdev->dev);
2968 pci_release_regions(pdev);
2969 pci_disable_device(pdev);
2972 static int velocity_platform_probe(struct platform_device *pdev)
2974 const struct of_device_id *of_id;
2975 const struct velocity_info_tbl *info;
2978 of_id = of_match_device(velocity_of_ids, &pdev->dev);
2983 irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
2987 return velocity_probe(&pdev->dev, irq, info, BUS_PLATFORM);
2990 static int velocity_platform_remove(struct platform_device *pdev)
2992 velocity_remove(&pdev->dev);
2997 #ifdef CONFIG_PM_SLEEP
2999 * wol_calc_crc - WOL CRC
3000 * @pattern: data pattern
3001 * @mask_pattern: mask
3003 * Compute the wake on lan crc hashes for the packet header
3004 * we are interested in.
3006 static u16 wol_calc_crc(int size, u8 *pattern, u8 *mask_pattern)
3012 for (i = 0; i < size; i++) {
3013 mask = mask_pattern[i];
3015 /* Skip this loop if the mask equals to zero */
3019 for (j = 0; j < 8; j++) {
3020 if ((mask & 0x01) == 0) {
3025 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
3028 /* Finally, invert the result once to get the correct data */
3030 return bitrev32(crc) >> 16;
3034 * velocity_set_wol - set up for wake on lan
3035 * @vptr: velocity to set WOL status on
3037 * Set a card up for wake on lan either by unicast or by
3040 * FIXME: check static buffer is safe here
3042 static int velocity_set_wol(struct velocity_info *vptr)
3044 struct mac_regs __iomem *regs = vptr->mac_regs;
3045 enum speed_opt spd_dpx = vptr->options.spd_dpx;
3049 static u32 mask_pattern[2][4] = {
3050 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3051 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3054 writew(0xFFFF, ®s->WOLCRClr);
3055 writeb(WOLCFG_SAB | WOLCFG_SAM, ®s->WOLCFGSet);
3056 writew(WOLCR_MAGIC_EN, ®s->WOLCRSet);
3059 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3060 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), ®s->WOLCRSet);
3063 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
3064 writew(WOLCR_UNICAST_EN, ®s->WOLCRSet);
3066 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3067 struct arp_packet *arp = (struct arp_packet *) buf;
3069 memset(buf, 0, sizeof(struct arp_packet) + 7);
3071 for (i = 0; i < 4; i++)
3072 writel(mask_pattern[0][i], ®s->ByteMask[0][i]);
3074 arp->type = htons(ETH_P_ARP);
3075 arp->ar_op = htons(1);
3077 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3079 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3080 (u8 *) & mask_pattern[0][0]);
3082 writew(crc, ®s->PatternCRC[0]);
3083 writew(WOLCR_ARP_EN, ®s->WOLCRSet);
3086 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, ®s->PWCFGSet);
3087 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, ®s->PWCFGSet);
3089 writew(0x0FFF, ®s->WOLSRClr);
3091 if (spd_dpx == SPD_DPX_1000_FULL)
3094 if (spd_dpx != SPD_DPX_AUTO)
3095 goto advertise_done;
3097 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3098 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3099 MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
3101 MII_REG_BITS_OFF(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
3104 if (vptr->mii_status & VELOCITY_SPEED_1000)
3105 MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
3108 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, ®s->CHIPGCR);
3112 GCR = readb(®s->CHIPGCR);
3113 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3114 writeb(GCR, ®s->CHIPGCR);
3118 BYTE_REG_BITS_OFF(ISR_PWEI, ®s->ISR);
3119 /* Turn on SWPTAG just before entering power mode */
3120 BYTE_REG_BITS_ON(STICKHW_SWPTAG, ®s->STICKHW);
3121 /* Go to bed ..... */
3122 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), ®s->STICKHW);
3128 * velocity_save_context - save registers
3130 * @context: buffer for stored context
3132 * Retrieve the current configuration from the velocity hardware
3133 * and stash it in the context structure, for use by the context
3134 * restore functions. This allows us to save things we need across
3137 static void velocity_save_context(struct velocity_info *vptr, struct velocity_context *context)
3139 struct mac_regs __iomem *regs = vptr->mac_regs;
3141 u8 __iomem *ptr = (u8 __iomem *)regs;
3143 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3144 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3146 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3147 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3149 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3150 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3154 static int velocity_suspend(struct device *dev)
3156 struct net_device *netdev = dev_get_drvdata(dev);
3157 struct velocity_info *vptr = netdev_priv(netdev);
3158 unsigned long flags;
3160 if (!netif_running(vptr->netdev))
3163 netif_device_detach(vptr->netdev);
3165 spin_lock_irqsave(&vptr->lock, flags);
3167 pci_save_state(vptr->pdev);
3169 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3170 velocity_get_ip(vptr);
3171 velocity_save_context(vptr, &vptr->context);
3172 velocity_shutdown(vptr);
3173 velocity_set_wol(vptr);
3175 pci_enable_wake(vptr->pdev, PCI_D3hot, 1);
3176 velocity_set_power_state(vptr, PCI_D3hot);
3178 velocity_save_context(vptr, &vptr->context);
3179 velocity_shutdown(vptr);
3181 pci_disable_device(vptr->pdev);
3182 velocity_set_power_state(vptr, PCI_D3hot);
3185 spin_unlock_irqrestore(&vptr->lock, flags);
3190 * velocity_restore_context - restore registers
3192 * @context: buffer for stored context
3194 * Reload the register configuration from the velocity context
3195 * created by velocity_save_context.
3197 static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3199 struct mac_regs __iomem *regs = vptr->mac_regs;
3201 u8 __iomem *ptr = (u8 __iomem *)regs;
3203 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4)
3204 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3207 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3209 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3211 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3214 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4)
3215 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3217 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3218 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3220 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++)
3221 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3224 static int velocity_resume(struct device *dev)
3226 struct net_device *netdev = dev_get_drvdata(dev);
3227 struct velocity_info *vptr = netdev_priv(netdev);
3228 unsigned long flags;
3231 if (!netif_running(vptr->netdev))
3234 velocity_set_power_state(vptr, PCI_D0);
3237 pci_enable_wake(vptr->pdev, PCI_D0, 0);
3238 pci_restore_state(vptr->pdev);
3241 mac_wol_reset(vptr->mac_regs);
3243 spin_lock_irqsave(&vptr->lock, flags);
3244 velocity_restore_context(vptr, &vptr->context);
3245 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3246 mac_disable_int(vptr->mac_regs);
3248 velocity_tx_srv(vptr);
3250 for (i = 0; i < vptr->tx.numq; i++) {
3251 if (vptr->tx.used[i])
3252 mac_tx_queue_wake(vptr->mac_regs, i);
3255 mac_enable_int(vptr->mac_regs);
3256 spin_unlock_irqrestore(&vptr->lock, flags);
3257 netif_device_attach(vptr->netdev);
3261 #endif /* CONFIG_PM_SLEEP */
3263 static SIMPLE_DEV_PM_OPS(velocity_pm_ops, velocity_suspend, velocity_resume);
3266 * Definition for our device driver. The PCI layer interface
3267 * uses this to handle all our card discover and plugging
3269 static struct pci_driver velocity_pci_driver = {
3270 .name = VELOCITY_NAME,
3271 .id_table = velocity_pci_id_table,
3272 .probe = velocity_pci_probe,
3273 .remove = velocity_pci_remove,
3275 .pm = &velocity_pm_ops,
3279 static struct platform_driver velocity_platform_driver = {
3280 .probe = velocity_platform_probe,
3281 .remove = velocity_platform_remove,
3283 .name = "via-velocity",
3284 .owner = THIS_MODULE,
3285 .of_match_table = velocity_of_ids,
3286 .pm = &velocity_pm_ops,
3291 * velocity_ethtool_up - pre hook for ethtool
3292 * @dev: network device
3294 * Called before an ethtool operation. We need to make sure the
3295 * chip is out of D3 state before we poke at it.
3297 static int velocity_ethtool_up(struct net_device *dev)
3299 struct velocity_info *vptr = netdev_priv(dev);
3300 if (!netif_running(dev))
3301 velocity_set_power_state(vptr, PCI_D0);
3306 * velocity_ethtool_down - post hook for ethtool
3307 * @dev: network device
3309 * Called after an ethtool operation. Restore the chip back to D3
3310 * state if it isn't running.
3312 static void velocity_ethtool_down(struct net_device *dev)
3314 struct velocity_info *vptr = netdev_priv(dev);
3315 if (!netif_running(dev))
3316 velocity_set_power_state(vptr, PCI_D3hot);
3319 static int velocity_get_settings(struct net_device *dev,
3320 struct ethtool_cmd *cmd)
3322 struct velocity_info *vptr = netdev_priv(dev);
3323 struct mac_regs __iomem *regs = vptr->mac_regs;
3325 status = check_connection_type(vptr->mac_regs);
3327 cmd->supported = SUPPORTED_TP |
3329 SUPPORTED_10baseT_Half |
3330 SUPPORTED_10baseT_Full |
3331 SUPPORTED_100baseT_Half |
3332 SUPPORTED_100baseT_Full |
3333 SUPPORTED_1000baseT_Half |
3334 SUPPORTED_1000baseT_Full;
3336 cmd->advertising = ADVERTISED_TP | ADVERTISED_Autoneg;
3337 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
3339 ADVERTISED_10baseT_Half |
3340 ADVERTISED_10baseT_Full |
3341 ADVERTISED_100baseT_Half |
3342 ADVERTISED_100baseT_Full |
3343 ADVERTISED_1000baseT_Half |
3344 ADVERTISED_1000baseT_Full;
3346 switch (vptr->options.spd_dpx) {
3347 case SPD_DPX_1000_FULL:
3348 cmd->advertising |= ADVERTISED_1000baseT_Full;
3350 case SPD_DPX_100_HALF:
3351 cmd->advertising |= ADVERTISED_100baseT_Half;
3353 case SPD_DPX_100_FULL:
3354 cmd->advertising |= ADVERTISED_100baseT_Full;
3356 case SPD_DPX_10_HALF:
3357 cmd->advertising |= ADVERTISED_10baseT_Half;
3359 case SPD_DPX_10_FULL:
3360 cmd->advertising |= ADVERTISED_10baseT_Full;
3367 if (status & VELOCITY_SPEED_1000)
3368 ethtool_cmd_speed_set(cmd, SPEED_1000);
3369 else if (status & VELOCITY_SPEED_100)
3370 ethtool_cmd_speed_set(cmd, SPEED_100);
3372 ethtool_cmd_speed_set(cmd, SPEED_10);
3374 cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
3375 cmd->port = PORT_TP;
3376 cmd->transceiver = XCVR_INTERNAL;
3377 cmd->phy_address = readb(®s->MIIADR) & 0x1F;
3379 if (status & VELOCITY_DUPLEX_FULL)
3380 cmd->duplex = DUPLEX_FULL;
3382 cmd->duplex = DUPLEX_HALF;
3387 static int velocity_set_settings(struct net_device *dev,
3388 struct ethtool_cmd *cmd)
3390 struct velocity_info *vptr = netdev_priv(dev);
3391 u32 speed = ethtool_cmd_speed(cmd);
3396 curr_status = check_connection_type(vptr->mac_regs);
3397 curr_status &= (~VELOCITY_LINK_FAIL);
3399 new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
3400 new_status |= ((speed == SPEED_1000) ? VELOCITY_SPEED_1000 : 0);
3401 new_status |= ((speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
3402 new_status |= ((speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
3403 new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
3405 if ((new_status & VELOCITY_AUTONEG_ENABLE) &&
3406 (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE))) {
3409 enum speed_opt spd_dpx;
3411 if (new_status & VELOCITY_AUTONEG_ENABLE)
3412 spd_dpx = SPD_DPX_AUTO;
3413 else if ((new_status & VELOCITY_SPEED_1000) &&
3414 (new_status & VELOCITY_DUPLEX_FULL)) {
3415 spd_dpx = SPD_DPX_1000_FULL;
3416 } else if (new_status & VELOCITY_SPEED_100)
3417 spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
3418 SPD_DPX_100_FULL : SPD_DPX_100_HALF;
3419 else if (new_status & VELOCITY_SPEED_10)
3420 spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
3421 SPD_DPX_10_FULL : SPD_DPX_10_HALF;
3425 vptr->options.spd_dpx = spd_dpx;
3427 velocity_set_media_mode(vptr, new_status);
3433 static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3435 struct velocity_info *vptr = netdev_priv(dev);
3437 strlcpy(info->driver, VELOCITY_NAME, sizeof(info->driver));
3438 strlcpy(info->version, VELOCITY_VERSION, sizeof(info->version));
3440 strlcpy(info->bus_info, pci_name(vptr->pdev),
3441 sizeof(info->bus_info));
3443 strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
3446 static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3448 struct velocity_info *vptr = netdev_priv(dev);
3449 wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
3450 wol->wolopts |= WAKE_MAGIC;
3452 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3453 wol.wolopts|=WAKE_PHY;
3455 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
3456 wol->wolopts |= WAKE_UCAST;
3457 if (vptr->wol_opts & VELOCITY_WOL_ARP)
3458 wol->wolopts |= WAKE_ARP;
3459 memcpy(&wol->sopass, vptr->wol_passwd, 6);
3462 static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3464 struct velocity_info *vptr = netdev_priv(dev);
3466 if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
3468 vptr->wol_opts = VELOCITY_WOL_MAGIC;
3471 if (wol.wolopts & WAKE_PHY) {
3472 vptr->wol_opts|=VELOCITY_WOL_PHY;
3473 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
3477 if (wol->wolopts & WAKE_MAGIC) {
3478 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
3479 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3481 if (wol->wolopts & WAKE_UCAST) {
3482 vptr->wol_opts |= VELOCITY_WOL_UCAST;
3483 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3485 if (wol->wolopts & WAKE_ARP) {
3486 vptr->wol_opts |= VELOCITY_WOL_ARP;
3487 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3489 memcpy(vptr->wol_passwd, wol->sopass, 6);
3493 static u32 velocity_get_msglevel(struct net_device *dev)
3498 static void velocity_set_msglevel(struct net_device *dev, u32 value)
3503 static int get_pending_timer_val(int val)
3505 int mult_bits = val >> 6;
3521 return (val & 0x3f) * mult;
3524 static void set_pending_timer_val(int *val, u32 us)
3530 mult = 1; /* mult with 4 */
3533 if (us >= 0x3f * 4) {
3534 mult = 2; /* mult with 16 */
3537 if (us >= 0x3f * 16) {
3538 mult = 3; /* mult with 64 */
3542 *val = (mult << 6) | ((us >> shift) & 0x3f);
3546 static int velocity_get_coalesce(struct net_device *dev,
3547 struct ethtool_coalesce *ecmd)
3549 struct velocity_info *vptr = netdev_priv(dev);
3551 ecmd->tx_max_coalesced_frames = vptr->options.tx_intsup;
3552 ecmd->rx_max_coalesced_frames = vptr->options.rx_intsup;
3554 ecmd->rx_coalesce_usecs = get_pending_timer_val(vptr->options.rxqueue_timer);
3555 ecmd->tx_coalesce_usecs = get_pending_timer_val(vptr->options.txqueue_timer);
3560 static int velocity_set_coalesce(struct net_device *dev,
3561 struct ethtool_coalesce *ecmd)
3563 struct velocity_info *vptr = netdev_priv(dev);
3564 int max_us = 0x3f * 64;
3565 unsigned long flags;
3568 if (ecmd->tx_coalesce_usecs > max_us)
3570 if (ecmd->rx_coalesce_usecs > max_us)
3573 if (ecmd->tx_max_coalesced_frames > 0xff)
3575 if (ecmd->rx_max_coalesced_frames > 0xff)
3578 vptr->options.rx_intsup = ecmd->rx_max_coalesced_frames;
3579 vptr->options.tx_intsup = ecmd->tx_max_coalesced_frames;
3581 set_pending_timer_val(&vptr->options.rxqueue_timer,
3582 ecmd->rx_coalesce_usecs);
3583 set_pending_timer_val(&vptr->options.txqueue_timer,
3584 ecmd->tx_coalesce_usecs);
3586 /* Setup the interrupt suppression and queue timers */
3587 spin_lock_irqsave(&vptr->lock, flags);
3588 mac_disable_int(vptr->mac_regs);
3589 setup_adaptive_interrupts(vptr);
3590 setup_queue_timers(vptr);
3592 mac_write_int_mask(vptr->int_mask, vptr->mac_regs);
3593 mac_clear_isr(vptr->mac_regs);
3594 mac_enable_int(vptr->mac_regs);
3595 spin_unlock_irqrestore(&vptr->lock, flags);
3600 static const char velocity_gstrings[][ETH_GSTRING_LEN] = {
3619 "tx_ether_collisions",
3623 "rx_mac_control_frames",
3624 "tx_mac_control_frames",
3625 "rx_frame_alignement_errors",
3631 "in_range_length_errors",
3635 static void velocity_get_strings(struct net_device *dev, u32 sset, u8 *data)
3639 memcpy(data, *velocity_gstrings, sizeof(velocity_gstrings));
3644 static int velocity_get_sset_count(struct net_device *dev, int sset)
3648 return ARRAY_SIZE(velocity_gstrings);
3654 static void velocity_get_ethtool_stats(struct net_device *dev,
3655 struct ethtool_stats *stats, u64 *data)
3657 if (netif_running(dev)) {
3658 struct velocity_info *vptr = netdev_priv(dev);
3659 u32 *p = vptr->mib_counter;
3662 spin_lock_irq(&vptr->lock);
3663 velocity_update_hw_mibs(vptr);
3664 spin_unlock_irq(&vptr->lock);
3666 for (i = 0; i < ARRAY_SIZE(velocity_gstrings); i++)
3671 static const struct ethtool_ops velocity_ethtool_ops = {
3672 .get_settings = velocity_get_settings,
3673 .set_settings = velocity_set_settings,
3674 .get_drvinfo = velocity_get_drvinfo,
3675 .get_wol = velocity_ethtool_get_wol,
3676 .set_wol = velocity_ethtool_set_wol,
3677 .get_msglevel = velocity_get_msglevel,
3678 .set_msglevel = velocity_set_msglevel,
3679 .get_link = velocity_get_link,
3680 .get_strings = velocity_get_strings,
3681 .get_sset_count = velocity_get_sset_count,
3682 .get_ethtool_stats = velocity_get_ethtool_stats,
3683 .get_coalesce = velocity_get_coalesce,
3684 .set_coalesce = velocity_set_coalesce,
3685 .begin = velocity_ethtool_up,
3686 .complete = velocity_ethtool_down
3689 #if defined(CONFIG_PM) && defined(CONFIG_INET)
3690 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3692 struct in_ifaddr *ifa = ptr;
3693 struct net_device *dev = ifa->ifa_dev->dev;
3695 if (dev_net(dev) == &init_net &&
3696 dev->netdev_ops == &velocity_netdev_ops)
3697 velocity_get_ip(netdev_priv(dev));
3702 static struct notifier_block velocity_inetaddr_notifier = {
3703 .notifier_call = velocity_netdev_event,
3706 static void velocity_register_notifier(void)
3708 register_inetaddr_notifier(&velocity_inetaddr_notifier);
3711 static void velocity_unregister_notifier(void)
3713 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
3718 #define velocity_register_notifier() do {} while (0)
3719 #define velocity_unregister_notifier() do {} while (0)
3721 #endif /* defined(CONFIG_PM) && defined(CONFIG_INET) */
3724 * velocity_init_module - load time function
3726 * Called when the velocity module is loaded. The PCI driver
3727 * is registered with the PCI layer, and in turn will call
3728 * the probe functions for each velocity adapter installed
3731 static int __init velocity_init_module(void)
3733 int ret_pci, ret_platform;
3735 velocity_register_notifier();
3737 ret_pci = pci_register_driver(&velocity_pci_driver);
3738 ret_platform = platform_driver_register(&velocity_platform_driver);
3740 /* if both_registers failed, remove the notifier */
3741 if ((ret_pci < 0) && (ret_platform < 0)) {
3742 velocity_unregister_notifier();
3750 * velocity_cleanup - module unload
3752 * When the velocity hardware is unloaded this function is called.
3753 * It will clean up the notifiers and the unregister the PCI
3754 * driver interface for this hardware. This in turn cleans up
3755 * all discovered interfaces before returning from the function
3757 static void __exit velocity_cleanup_module(void)
3759 velocity_unregister_notifier();
3761 pci_unregister_driver(&velocity_pci_driver);
3762 platform_driver_unregister(&velocity_platform_driver);
3765 module_init(velocity_init_module);
3766 module_exit(velocity_cleanup_module);