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 WOL_OPT_DEF 0
349 #define WOL_OPT_MIN 0
350 #define WOL_OPT_MAX 7
351 /* wol_opts[] is used for controlling wake on lan behavior.
352 0: Wake up if recevied a magic packet. (Default)
353 1: Wake up if link status is on/off.
354 2: Wake up if recevied an arp packet.
355 4: Wake up if recevied any unicast packet.
356 Those value can be sumed up to support more than one option.
358 VELOCITY_PARAM(wol_opts, "Wake On Lan options");
360 static int rx_copybreak = 200;
361 module_param(rx_copybreak, int, 0644);
362 MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
365 * Internal board variants. At the moment we have only one
367 static struct velocity_info_tbl chip_info_table[] = {
368 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
373 * Describe the PCI device identifiers that we support in this
374 * device driver. Used for hotplug autoloading.
377 static const struct pci_device_id velocity_pci_id_table[] = {
378 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
382 MODULE_DEVICE_TABLE(pci, velocity_pci_id_table);
385 * Describe the OF device identifiers that we support in this
386 * device driver. Used for devicetree nodes.
388 static const struct of_device_id velocity_of_ids[] = {
389 { .compatible = "via,velocity-vt6110", .data = &chip_info_table[0] },
392 MODULE_DEVICE_TABLE(of, velocity_of_ids);
395 * get_chip_name - identifier to name
396 * @id: chip identifier
398 * Given a chip identifier return a suitable description. Returns
399 * a pointer a static string valid while the driver is loaded.
401 static const char *get_chip_name(enum chip_type chip_id)
404 for (i = 0; chip_info_table[i].name != NULL; i++)
405 if (chip_info_table[i].chip_id == chip_id)
407 return chip_info_table[i].name;
411 * velocity_set_int_opt - parser for integer options
412 * @opt: pointer to option value
413 * @val: value the user requested (or -1 for default)
414 * @min: lowest value allowed
415 * @max: highest value allowed
416 * @def: default value
417 * @name: property name
420 * Set an integer property in the module options. This function does
421 * all the verification and checking as well as reporting so that
422 * we don't duplicate code for each option.
424 static void velocity_set_int_opt(int *opt, int val, int min, int max, int def,
425 char *name, const char *devname)
429 else if (val < min || val > max) {
430 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
431 devname, name, min, max);
434 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
441 * velocity_set_bool_opt - parser for boolean options
442 * @opt: pointer to option value
443 * @val: value the user requested (or -1 for default)
444 * @def: default value (yes/no)
445 * @flag: numeric value to set for true.
446 * @name: property name
449 * Set a boolean property in the module options. This function does
450 * all the verification and checking as well as reporting so that
451 * we don't duplicate code for each option.
453 static void velocity_set_bool_opt(u32 *opt, int val, int def, u32 flag,
454 char *name, const char *devname)
458 *opt |= (def ? flag : 0);
459 else if (val < 0 || val > 1) {
460 printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
462 *opt |= (def ? flag : 0);
464 printk(KERN_INFO "%s: set parameter %s to %s\n",
465 devname, name, val ? "TRUE" : "FALSE");
466 *opt |= (val ? flag : 0);
471 * velocity_get_options - set options on device
472 * @opts: option structure for the device
473 * @index: index of option to use in module options array
474 * @devname: device name
476 * Turn the module and command options into a single structure
477 * for the current device
479 static void velocity_get_options(struct velocity_opt *opts, int index,
483 velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
484 velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
485 velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
486 velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
488 velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
489 velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
490 velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
491 velocity_set_int_opt(&opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
492 opts->numrx = (opts->numrx & ~3);
496 * velocity_init_cam_filter - initialise CAM
497 * @vptr: velocity to program
499 * Initialize the content addressable memory used for filters. Load
500 * appropriately according to the presence of VLAN
502 static void velocity_init_cam_filter(struct velocity_info *vptr)
504 struct mac_regs __iomem *regs = vptr->mac_regs;
505 unsigned int vid, i = 0;
507 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
508 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, ®s->MCFG);
509 WORD_REG_BITS_ON(MCFG_VIDFR, ®s->MCFG);
511 /* Disable all CAMs */
512 memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
513 memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
514 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
515 mac_set_cam_mask(regs, vptr->mCAMmask);
518 for_each_set_bit(vid, vptr->active_vlans, VLAN_N_VID) {
519 mac_set_vlan_cam(regs, i, (u8 *) &vid);
520 vptr->vCAMmask[i / 8] |= 0x1 << (i % 8);
521 if (++i >= VCAM_SIZE)
524 mac_set_vlan_cam_mask(regs, vptr->vCAMmask);
527 static int velocity_vlan_rx_add_vid(struct net_device *dev,
528 __be16 proto, u16 vid)
530 struct velocity_info *vptr = netdev_priv(dev);
532 spin_lock_irq(&vptr->lock);
533 set_bit(vid, vptr->active_vlans);
534 velocity_init_cam_filter(vptr);
535 spin_unlock_irq(&vptr->lock);
539 static int velocity_vlan_rx_kill_vid(struct net_device *dev,
540 __be16 proto, u16 vid)
542 struct velocity_info *vptr = netdev_priv(dev);
544 spin_lock_irq(&vptr->lock);
545 clear_bit(vid, vptr->active_vlans);
546 velocity_init_cam_filter(vptr);
547 spin_unlock_irq(&vptr->lock);
551 static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
553 vptr->rx.dirty = vptr->rx.filled = vptr->rx.curr = 0;
557 * velocity_rx_reset - handle a receive reset
558 * @vptr: velocity we are resetting
560 * Reset the ownership and status for the receive ring side.
561 * Hand all the receive queue to the NIC.
563 static void velocity_rx_reset(struct velocity_info *vptr)
566 struct mac_regs __iomem *regs = vptr->mac_regs;
569 velocity_init_rx_ring_indexes(vptr);
572 * Init state, all RD entries belong to the NIC
574 for (i = 0; i < vptr->options.numrx; ++i)
575 vptr->rx.ring[i].rdesc0.len |= OWNED_BY_NIC;
577 writew(vptr->options.numrx, ®s->RBRDU);
578 writel(vptr->rx.pool_dma, ®s->RDBaseLo);
579 writew(0, ®s->RDIdx);
580 writew(vptr->options.numrx - 1, ®s->RDCSize);
584 * velocity_get_opt_media_mode - get media selection
585 * @vptr: velocity adapter
587 * Get the media mode stored in EEPROM or module options and load
588 * mii_status accordingly. The requested link state information
591 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
595 switch (vptr->options.spd_dpx) {
597 status = VELOCITY_AUTONEG_ENABLE;
599 case SPD_DPX_100_FULL:
600 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
602 case SPD_DPX_10_FULL:
603 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
605 case SPD_DPX_100_HALF:
606 status = VELOCITY_SPEED_100;
608 case SPD_DPX_10_HALF:
609 status = VELOCITY_SPEED_10;
611 case SPD_DPX_1000_FULL:
612 status = VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
615 vptr->mii_status = status;
620 * safe_disable_mii_autopoll - autopoll off
621 * @regs: velocity registers
623 * Turn off the autopoll and wait for it to disable on the chip
625 static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs)
630 writeb(0, ®s->MIICR);
631 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
633 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
639 * enable_mii_autopoll - turn on autopolling
640 * @regs: velocity registers
642 * Enable the MII link status autopoll feature on the Velocity
643 * hardware. Wait for it to enable.
645 static void enable_mii_autopoll(struct mac_regs __iomem *regs)
649 writeb(0, &(regs->MIICR));
650 writeb(MIIADR_SWMPL, ®s->MIIADR);
652 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
654 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
658 writeb(MIICR_MAUTO, ®s->MIICR);
660 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
662 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, ®s->MIISR))
669 * velocity_mii_read - read MII data
670 * @regs: velocity registers
671 * @index: MII register index
672 * @data: buffer for received data
674 * Perform a single read of an MII 16bit register. Returns zero
675 * on success or -ETIMEDOUT if the PHY did not respond.
677 static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
682 * Disable MIICR_MAUTO, so that mii addr can be set normally
684 safe_disable_mii_autopoll(regs);
686 writeb(index, ®s->MIIADR);
688 BYTE_REG_BITS_ON(MIICR_RCMD, ®s->MIICR);
690 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
691 if (!(readb(®s->MIICR) & MIICR_RCMD))
695 *data = readw(®s->MIIDATA);
697 enable_mii_autopoll(regs);
698 if (ww == W_MAX_TIMEOUT)
704 * mii_check_media_mode - check media state
705 * @regs: velocity registers
707 * Check the current MII status and determine the link status
710 static u32 mii_check_media_mode(struct mac_regs __iomem *regs)
715 if (!MII_REG_BITS_IS_ON(BMSR_LSTATUS, MII_BMSR, regs))
716 status |= VELOCITY_LINK_FAIL;
718 if (MII_REG_BITS_IS_ON(ADVERTISE_1000FULL, MII_CTRL1000, regs))
719 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
720 else if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF, MII_CTRL1000, regs))
721 status |= (VELOCITY_SPEED_1000);
723 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
724 if (ANAR & ADVERTISE_100FULL)
725 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
726 else if (ANAR & ADVERTISE_100HALF)
727 status |= VELOCITY_SPEED_100;
728 else if (ANAR & ADVERTISE_10FULL)
729 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
731 status |= (VELOCITY_SPEED_10);
734 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
735 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
736 if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
737 == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
738 if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
739 status |= VELOCITY_AUTONEG_ENABLE;
747 * velocity_mii_write - write MII data
748 * @regs: velocity registers
749 * @index: MII register index
750 * @data: 16bit data for the MII register
752 * Perform a single write to an MII 16bit register. Returns zero
753 * on success or -ETIMEDOUT if the PHY did not respond.
755 static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
760 * Disable MIICR_MAUTO, so that mii addr can be set normally
762 safe_disable_mii_autopoll(regs);
765 writeb(mii_addr, ®s->MIIADR);
767 writew(data, ®s->MIIDATA);
769 /* turn on MIICR_WCMD */
770 BYTE_REG_BITS_ON(MIICR_WCMD, ®s->MIICR);
772 /* W_MAX_TIMEOUT is the timeout period */
773 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
775 if (!(readb(®s->MIICR) & MIICR_WCMD))
778 enable_mii_autopoll(regs);
780 if (ww == W_MAX_TIMEOUT)
786 * set_mii_flow_control - flow control setup
787 * @vptr: velocity interface
789 * Set up the flow control on this interface according to
790 * the supplied user/eeprom options.
792 static void set_mii_flow_control(struct velocity_info *vptr)
794 /*Enable or Disable PAUSE in ANAR */
795 switch (vptr->options.flow_cntl) {
797 MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
798 MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
802 MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
803 MII_REG_BITS_ON(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
806 case FLOW_CNTL_TX_RX:
807 MII_REG_BITS_ON(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
808 MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
811 case FLOW_CNTL_DISABLE:
812 MII_REG_BITS_OFF(ADVERTISE_PAUSE_CAP, MII_ADVERTISE, vptr->mac_regs);
813 MII_REG_BITS_OFF(ADVERTISE_PAUSE_ASYM, MII_ADVERTISE, vptr->mac_regs);
821 * mii_set_auto_on - autonegotiate on
824 * Enable autonegotation on this interface
826 static void mii_set_auto_on(struct velocity_info *vptr)
828 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs))
829 MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
831 MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs);
834 static u32 check_connection_type(struct mac_regs __iomem *regs)
839 PHYSR0 = readb(®s->PHYSR0);
842 if (!(PHYSR0 & PHYSR0_LINKGD))
843 status|=VELOCITY_LINK_FAIL;
846 if (PHYSR0 & PHYSR0_FDPX)
847 status |= VELOCITY_DUPLEX_FULL;
849 if (PHYSR0 & PHYSR0_SPDG)
850 status |= VELOCITY_SPEED_1000;
851 else if (PHYSR0 & PHYSR0_SPD10)
852 status |= VELOCITY_SPEED_10;
854 status |= VELOCITY_SPEED_100;
856 if (MII_REG_BITS_IS_ON(BMCR_ANENABLE, MII_BMCR, regs)) {
857 velocity_mii_read(regs, MII_ADVERTISE, &ANAR);
858 if ((ANAR & (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF))
859 == (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF)) {
860 if (MII_REG_BITS_IS_ON(ADVERTISE_1000HALF | ADVERTISE_1000FULL, MII_CTRL1000, regs))
861 status |= VELOCITY_AUTONEG_ENABLE;
869 * velocity_set_media_mode - set media mode
870 * @mii_status: old MII link state
872 * Check the media link state and configure the flow control
873 * PHY and also velocity hardware setup accordingly. In particular
874 * we need to set up CD polling and frame bursting.
876 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
878 struct mac_regs __iomem *regs = vptr->mac_regs;
880 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
882 /* Set mii link status */
883 set_mii_flow_control(vptr);
885 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
886 MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
889 * If connection type is AUTO
891 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
892 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
893 /* clear force MAC mode bit */
894 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, ®s->CHIPGCR);
895 /* set duplex mode of MAC according to duplex mode of MII */
896 MII_REG_BITS_ON(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF, MII_ADVERTISE, vptr->mac_regs);
897 MII_REG_BITS_ON(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
898 MII_REG_BITS_ON(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs);
900 /* enable AUTO-NEGO mode */
901 mii_set_auto_on(vptr);
908 * 1. if it's 3119, disable frame bursting in halfduplex mode
909 * and enable it in fullduplex mode
910 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
911 * 3. only enable CD heart beat counter in 10HD mode
914 /* set force MAC mode bit */
915 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, ®s->CHIPGCR);
917 CHIPGCR = readb(®s->CHIPGCR);
919 if (mii_status & VELOCITY_SPEED_1000)
920 CHIPGCR |= CHIPGCR_FCGMII;
922 CHIPGCR &= ~CHIPGCR_FCGMII;
924 if (mii_status & VELOCITY_DUPLEX_FULL) {
925 CHIPGCR |= CHIPGCR_FCFDX;
926 writeb(CHIPGCR, ®s->CHIPGCR);
927 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
928 if (vptr->rev_id < REV_ID_VT3216_A0)
929 BYTE_REG_BITS_OFF(TCR_TB2BDIS, ®s->TCR);
931 CHIPGCR &= ~CHIPGCR_FCFDX;
932 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
933 writeb(CHIPGCR, ®s->CHIPGCR);
934 if (vptr->rev_id < REV_ID_VT3216_A0)
935 BYTE_REG_BITS_ON(TCR_TB2BDIS, ®s->TCR);
938 velocity_mii_read(vptr->mac_regs, MII_CTRL1000, &CTRL1000);
939 CTRL1000 &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
940 if ((mii_status & VELOCITY_SPEED_1000) &&
941 (mii_status & VELOCITY_DUPLEX_FULL)) {
942 CTRL1000 |= ADVERTISE_1000FULL;
944 velocity_mii_write(vptr->mac_regs, MII_CTRL1000, CTRL1000);
946 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10))
947 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, ®s->TESTCFG);
949 BYTE_REG_BITS_ON(TESTCFG_HBDIS, ®s->TESTCFG);
951 /* MII_REG_BITS_OFF(BMCR_SPEED1000, MII_BMCR, vptr->mac_regs); */
952 velocity_mii_read(vptr->mac_regs, MII_ADVERTISE, &ANAR);
953 ANAR &= (~(ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL | ADVERTISE_10HALF));
954 if (mii_status & VELOCITY_SPEED_100) {
955 if (mii_status & VELOCITY_DUPLEX_FULL)
956 ANAR |= ADVERTISE_100FULL;
958 ANAR |= ADVERTISE_100HALF;
959 } else if (mii_status & VELOCITY_SPEED_10) {
960 if (mii_status & VELOCITY_DUPLEX_FULL)
961 ANAR |= ADVERTISE_10FULL;
963 ANAR |= ADVERTISE_10HALF;
965 velocity_mii_write(vptr->mac_regs, MII_ADVERTISE, ANAR);
966 /* enable AUTO-NEGO mode */
967 mii_set_auto_on(vptr);
968 /* MII_REG_BITS_ON(BMCR_ANENABLE, MII_BMCR, vptr->mac_regs); */
970 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
971 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
972 return VELOCITY_LINK_CHANGE;
976 * velocity_print_link_status - link status reporting
977 * @vptr: velocity to report on
979 * Turn the link status of the velocity card into a kernel log
980 * description of the new link state, detailing speed and duplex
983 static void velocity_print_link_status(struct velocity_info *vptr)
986 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
987 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->netdev->name);
988 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
989 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->netdev->name);
991 if (vptr->mii_status & VELOCITY_SPEED_1000)
992 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
993 else if (vptr->mii_status & VELOCITY_SPEED_100)
994 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
996 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
998 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
999 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1001 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1003 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->netdev->name);
1004 switch (vptr->options.spd_dpx) {
1005 case SPD_DPX_1000_FULL:
1006 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps full duplex\n");
1008 case SPD_DPX_100_HALF:
1009 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1011 case SPD_DPX_100_FULL:
1012 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1014 case SPD_DPX_10_HALF:
1015 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1017 case SPD_DPX_10_FULL:
1018 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1027 * enable_flow_control_ability - flow control
1028 * @vptr: veloity to configure
1030 * Set up flow control according to the flow control options
1031 * determined by the eeprom/configuration.
1033 static void enable_flow_control_ability(struct velocity_info *vptr)
1036 struct mac_regs __iomem *regs = vptr->mac_regs;
1038 switch (vptr->options.flow_cntl) {
1040 case FLOW_CNTL_DEFAULT:
1041 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, ®s->PHYSR0))
1042 writel(CR0_FDXRFCEN, ®s->CR0Set);
1044 writel(CR0_FDXRFCEN, ®s->CR0Clr);
1046 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, ®s->PHYSR0))
1047 writel(CR0_FDXTFCEN, ®s->CR0Set);
1049 writel(CR0_FDXTFCEN, ®s->CR0Clr);
1053 writel(CR0_FDXTFCEN, ®s->CR0Set);
1054 writel(CR0_FDXRFCEN, ®s->CR0Clr);
1058 writel(CR0_FDXRFCEN, ®s->CR0Set);
1059 writel(CR0_FDXTFCEN, ®s->CR0Clr);
1062 case FLOW_CNTL_TX_RX:
1063 writel(CR0_FDXTFCEN, ®s->CR0Set);
1064 writel(CR0_FDXRFCEN, ®s->CR0Set);
1067 case FLOW_CNTL_DISABLE:
1068 writel(CR0_FDXRFCEN, ®s->CR0Clr);
1069 writel(CR0_FDXTFCEN, ®s->CR0Clr);
1079 * velocity_soft_reset - soft reset
1080 * @vptr: velocity to reset
1082 * Kick off a soft reset of the velocity adapter and then poll
1083 * until the reset sequence has completed before returning.
1085 static int velocity_soft_reset(struct velocity_info *vptr)
1087 struct mac_regs __iomem *regs = vptr->mac_regs;
1090 writel(CR0_SFRST, ®s->CR0Set);
1092 for (i = 0; i < W_MAX_TIMEOUT; i++) {
1094 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, ®s->CR0Set))
1098 if (i == W_MAX_TIMEOUT) {
1099 writel(CR0_FORSRST, ®s->CR0Set);
1100 /* FIXME: PCI POSTING */
1108 * velocity_set_multi - filter list change callback
1109 * @dev: network device
1111 * Called by the network layer when the filter lists need to change
1112 * for a velocity adapter. Reload the CAMs with the new address
1115 static void velocity_set_multi(struct net_device *dev)
1117 struct velocity_info *vptr = netdev_priv(dev);
1118 struct mac_regs __iomem *regs = vptr->mac_regs;
1121 struct netdev_hw_addr *ha;
1123 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1124 writel(0xffffffff, ®s->MARCAM[0]);
1125 writel(0xffffffff, ®s->MARCAM[4]);
1126 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
1127 } else if ((netdev_mc_count(dev) > vptr->multicast_limit) ||
1128 (dev->flags & IFF_ALLMULTI)) {
1129 writel(0xffffffff, ®s->MARCAM[0]);
1130 writel(0xffffffff, ®s->MARCAM[4]);
1131 rx_mode = (RCR_AM | RCR_AB);
1133 int offset = MCAM_SIZE - vptr->multicast_limit;
1134 mac_get_cam_mask(regs, vptr->mCAMmask);
1137 netdev_for_each_mc_addr(ha, dev) {
1138 mac_set_cam(regs, i + offset, ha->addr);
1139 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
1143 mac_set_cam_mask(regs, vptr->mCAMmask);
1144 rx_mode = RCR_AM | RCR_AB | RCR_AP;
1146 if (dev->mtu > 1500)
1149 BYTE_REG_BITS_ON(rx_mode, ®s->RCR);
1154 * MII access , media link mode setting functions
1158 * mii_init - set up MII
1159 * @vptr: velocity adapter
1160 * @mii_status: links tatus
1162 * Set up the PHY for the current link state.
1164 static void mii_init(struct velocity_info *vptr, u32 mii_status)
1168 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
1169 case PHYID_ICPLUS_IP101A:
1170 MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP),
1171 MII_ADVERTISE, vptr->mac_regs);
1172 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1173 MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION,
1176 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION,
1178 MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
1180 case PHYID_CICADA_CS8201:
1182 * Reset to hardware default
1184 MII_REG_BITS_OFF((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1186 * Turn on ECHODIS bit in NWay-forced full mode and turn it
1187 * off it in NWay-forced half mode for NWay-forced v.s.
1188 * legacy-forced issue.
1190 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1191 MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1193 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1195 * Turn on Link/Activity LED enable bit for CIS8201
1197 MII_REG_BITS_ON(PLED_LALBE, MII_TPISTATUS, vptr->mac_regs);
1199 case PHYID_VT3216_32BIT:
1200 case PHYID_VT3216_64BIT:
1202 * Reset to hardware default
1204 MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1206 * Turn on ECHODIS bit in NWay-forced full mode and turn it
1207 * off it in NWay-forced half mode for NWay-forced v.s.
1208 * legacy-forced issue
1210 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1211 MII_REG_BITS_ON(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1213 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_SREVISION, vptr->mac_regs);
1216 case PHYID_MARVELL_1000:
1217 case PHYID_MARVELL_1000S:
1219 * Assert CRS on Transmit
1221 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
1223 * Reset to hardware default
1225 MII_REG_BITS_ON((ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP), MII_ADVERTISE, vptr->mac_regs);
1230 velocity_mii_read(vptr->mac_regs, MII_BMCR, &BMCR);
1231 if (BMCR & BMCR_ISOLATE) {
1232 BMCR &= ~BMCR_ISOLATE;
1233 velocity_mii_write(vptr->mac_regs, MII_BMCR, BMCR);
1238 * setup_queue_timers - Setup interrupt timers
1240 * Setup interrupt frequency during suppression (timeout if the frame
1241 * count isn't filled).
1243 static void setup_queue_timers(struct velocity_info *vptr)
1245 /* Only for newer revisions */
1246 if (vptr->rev_id >= REV_ID_VT3216_A0) {
1247 u8 txqueue_timer = 0;
1248 u8 rxqueue_timer = 0;
1250 if (vptr->mii_status & (VELOCITY_SPEED_1000 |
1251 VELOCITY_SPEED_100)) {
1252 txqueue_timer = vptr->options.txqueue_timer;
1253 rxqueue_timer = vptr->options.rxqueue_timer;
1256 writeb(txqueue_timer, &vptr->mac_regs->TQETMR);
1257 writeb(rxqueue_timer, &vptr->mac_regs->RQETMR);
1262 * setup_adaptive_interrupts - Setup interrupt suppression
1264 * @vptr velocity adapter
1266 * The velocity is able to suppress interrupt during high interrupt load.
1267 * This function turns on that feature.
1269 static void setup_adaptive_interrupts(struct velocity_info *vptr)
1271 struct mac_regs __iomem *regs = vptr->mac_regs;
1272 u16 tx_intsup = vptr->options.tx_intsup;
1273 u16 rx_intsup = vptr->options.rx_intsup;
1275 /* Setup default interrupt mask (will be changed below) */
1276 vptr->int_mask = INT_MASK_DEF;
1278 /* Set Tx Interrupt Suppression Threshold */
1279 writeb(CAMCR_PS0, ®s->CAMCR);
1280 if (tx_intsup != 0) {
1281 vptr->int_mask &= ~(ISR_PTXI | ISR_PTX0I | ISR_PTX1I |
1282 ISR_PTX2I | ISR_PTX3I);
1283 writew(tx_intsup, ®s->ISRCTL);
1285 writew(ISRCTL_TSUPDIS, ®s->ISRCTL);
1287 /* Set Rx Interrupt Suppression Threshold */
1288 writeb(CAMCR_PS1, ®s->CAMCR);
1289 if (rx_intsup != 0) {
1290 vptr->int_mask &= ~ISR_PRXI;
1291 writew(rx_intsup, ®s->ISRCTL);
1293 writew(ISRCTL_RSUPDIS, ®s->ISRCTL);
1295 /* Select page to interrupt hold timer */
1296 writeb(0, ®s->CAMCR);
1300 * velocity_init_registers - initialise MAC registers
1301 * @vptr: velocity to init
1302 * @type: type of initialisation (hot or cold)
1304 * Initialise the MAC on a reset or on first set up on the
1307 static void velocity_init_registers(struct velocity_info *vptr,
1308 enum velocity_init_type type)
1310 struct mac_regs __iomem *regs = vptr->mac_regs;
1311 struct net_device *netdev = vptr->netdev;
1314 mac_wol_reset(regs);
1317 case VELOCITY_INIT_RESET:
1318 case VELOCITY_INIT_WOL:
1320 netif_stop_queue(netdev);
1323 * Reset RX to prevent RX pointer not on the 4X location
1325 velocity_rx_reset(vptr);
1326 mac_rx_queue_run(regs);
1327 mac_rx_queue_wake(regs);
1329 mii_status = velocity_get_opt_media_mode(vptr);
1330 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
1331 velocity_print_link_status(vptr);
1332 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
1333 netif_wake_queue(netdev);
1336 enable_flow_control_ability(vptr);
1338 mac_clear_isr(regs);
1339 writel(CR0_STOP, ®s->CR0Clr);
1340 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
1345 case VELOCITY_INIT_COLD:
1350 velocity_soft_reset(vptr);
1353 if (!vptr->no_eeprom) {
1354 mac_eeprom_reload(regs);
1355 for (i = 0; i < 6; i++)
1356 writeb(netdev->dev_addr[i], regs->PAR + i);
1360 * clear Pre_ACPI bit.
1362 BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
1363 mac_set_rx_thresh(regs, vptr->options.rx_thresh);
1364 mac_set_dma_length(regs, vptr->options.DMA_length);
1366 writeb(WOLCFG_SAM | WOLCFG_SAB, ®s->WOLCFGSet);
1368 * Back off algorithm use original IEEE standard
1370 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), ®s->CFGB);
1375 velocity_init_cam_filter(vptr);
1378 * Set packet filter: Receive directed and broadcast address
1380 velocity_set_multi(netdev);
1383 * Enable MII auto-polling
1385 enable_mii_autopoll(regs);
1387 setup_adaptive_interrupts(vptr);
1389 writel(vptr->rx.pool_dma, ®s->RDBaseLo);
1390 writew(vptr->options.numrx - 1, ®s->RDCSize);
1391 mac_rx_queue_run(regs);
1392 mac_rx_queue_wake(regs);
1394 writew(vptr->options.numtx - 1, ®s->TDCSize);
1396 for (i = 0; i < vptr->tx.numq; i++) {
1397 writel(vptr->tx.pool_dma[i], ®s->TDBaseLo[i]);
1398 mac_tx_queue_run(regs, i);
1401 init_flow_control_register(vptr);
1403 writel(CR0_STOP, ®s->CR0Clr);
1404 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), ®s->CR0Set);
1406 mii_status = velocity_get_opt_media_mode(vptr);
1407 netif_stop_queue(netdev);
1409 mii_init(vptr, mii_status);
1411 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
1412 velocity_print_link_status(vptr);
1413 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
1414 netif_wake_queue(netdev);
1417 enable_flow_control_ability(vptr);
1418 mac_hw_mibs_init(regs);
1419 mac_write_int_mask(vptr->int_mask, regs);
1420 mac_clear_isr(regs);
1425 static void velocity_give_many_rx_descs(struct velocity_info *vptr)
1427 struct mac_regs __iomem *regs = vptr->mac_regs;
1428 int avail, dirty, unusable;
1431 * RD number must be equal to 4X per hardware spec
1432 * (programming guide rev 1.20, p.13)
1434 if (vptr->rx.filled < 4)
1439 unusable = vptr->rx.filled & 0x0003;
1440 dirty = vptr->rx.dirty - unusable;
1441 for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
1442 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1443 vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
1446 writew(vptr->rx.filled & 0xfffc, ®s->RBRDU);
1447 vptr->rx.filled = unusable;
1451 * velocity_init_dma_rings - set up DMA rings
1452 * @vptr: Velocity to set up
1454 * Allocate PCI mapped DMA rings for the receive and transmit layer
1457 static int velocity_init_dma_rings(struct velocity_info *vptr)
1459 struct velocity_opt *opt = &vptr->options;
1460 const unsigned int rx_ring_size = opt->numrx * sizeof(struct rx_desc);
1461 const unsigned int tx_ring_size = opt->numtx * sizeof(struct tx_desc);
1462 dma_addr_t pool_dma;
1467 * Allocate all RD/TD rings a single pool.
1469 * dma_alloc_coherent() fulfills the requirement for 64 bytes
1472 pool = dma_alloc_coherent(vptr->dev, tx_ring_size * vptr->tx.numq +
1473 rx_ring_size, &pool_dma, GFP_ATOMIC);
1475 dev_err(vptr->dev, "%s : DMA memory allocation failed.\n",
1476 vptr->netdev->name);
1480 vptr->rx.ring = pool;
1481 vptr->rx.pool_dma = pool_dma;
1483 pool += rx_ring_size;
1484 pool_dma += rx_ring_size;
1486 for (i = 0; i < vptr->tx.numq; i++) {
1487 vptr->tx.rings[i] = pool;
1488 vptr->tx.pool_dma[i] = pool_dma;
1489 pool += tx_ring_size;
1490 pool_dma += tx_ring_size;
1496 static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
1498 vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
1502 * velocity_alloc_rx_buf - allocate aligned receive buffer
1506 * Allocate a new full sized buffer for the reception of a frame and
1507 * map it into PCI space for the hardware to use. The hardware
1508 * requires *64* byte alignment of the buffer which makes life
1509 * less fun than would be ideal.
1511 static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1513 struct rx_desc *rd = &(vptr->rx.ring[idx]);
1514 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
1516 rd_info->skb = netdev_alloc_skb(vptr->netdev, vptr->rx.buf_sz + 64);
1517 if (rd_info->skb == NULL)
1521 * Do the gymnastics to get the buffer head for data at
1524 skb_reserve(rd_info->skb,
1525 64 - ((unsigned long) rd_info->skb->data & 63));
1526 rd_info->skb_dma = dma_map_single(vptr->dev, rd_info->skb->data,
1527 vptr->rx.buf_sz, DMA_FROM_DEVICE);
1530 * Fill in the descriptor to match
1533 *((u32 *) & (rd->rdesc0)) = 0;
1534 rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
1535 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1541 static int velocity_rx_refill(struct velocity_info *vptr)
1543 int dirty = vptr->rx.dirty, done = 0;
1546 struct rx_desc *rd = vptr->rx.ring + dirty;
1548 /* Fine for an all zero Rx desc at init time as well */
1549 if (rd->rdesc0.len & OWNED_BY_NIC)
1552 if (!vptr->rx.info[dirty].skb) {
1553 if (velocity_alloc_rx_buf(vptr, dirty) < 0)
1557 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
1558 } while (dirty != vptr->rx.curr);
1561 vptr->rx.dirty = dirty;
1562 vptr->rx.filled += done;
1569 * velocity_free_rd_ring - free receive ring
1570 * @vptr: velocity to clean up
1572 * Free the receive buffers for each ring slot and any
1573 * attached socket buffers that need to go away.
1575 static void velocity_free_rd_ring(struct velocity_info *vptr)
1579 if (vptr->rx.info == NULL)
1582 for (i = 0; i < vptr->options.numrx; i++) {
1583 struct velocity_rd_info *rd_info = &(vptr->rx.info[i]);
1584 struct rx_desc *rd = vptr->rx.ring + i;
1586 memset(rd, 0, sizeof(*rd));
1590 dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
1592 rd_info->skb_dma = 0;
1594 dev_kfree_skb(rd_info->skb);
1595 rd_info->skb = NULL;
1598 kfree(vptr->rx.info);
1599 vptr->rx.info = NULL;
1603 * velocity_init_rd_ring - set up receive ring
1604 * @vptr: velocity to configure
1606 * Allocate and set up the receive buffers for each ring slot and
1607 * assign them to the network adapter.
1609 static int velocity_init_rd_ring(struct velocity_info *vptr)
1613 vptr->rx.info = kcalloc(vptr->options.numrx,
1614 sizeof(struct velocity_rd_info), GFP_KERNEL);
1618 velocity_init_rx_ring_indexes(vptr);
1620 if (velocity_rx_refill(vptr) != vptr->options.numrx) {
1621 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1622 "%s: failed to allocate RX buffer.\n", vptr->netdev->name);
1623 velocity_free_rd_ring(vptr);
1633 * velocity_init_td_ring - set up transmit ring
1636 * Set up the transmit ring and chain the ring pointers together.
1637 * Returns zero on success or a negative posix errno code for
1640 static int velocity_init_td_ring(struct velocity_info *vptr)
1644 /* Init the TD ring entries */
1645 for (j = 0; j < vptr->tx.numq; j++) {
1647 vptr->tx.infos[j] = kcalloc(vptr->options.numtx,
1648 sizeof(struct velocity_td_info),
1650 if (!vptr->tx.infos[j]) {
1652 kfree(vptr->tx.infos[j]);
1656 vptr->tx.tail[j] = vptr->tx.curr[j] = vptr->tx.used[j] = 0;
1662 * velocity_free_dma_rings - free PCI ring pointers
1663 * @vptr: Velocity to free from
1665 * Clean up the PCI ring buffers allocated to this velocity.
1667 static void velocity_free_dma_rings(struct velocity_info *vptr)
1669 const int size = vptr->options.numrx * sizeof(struct rx_desc) +
1670 vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;
1672 dma_free_coherent(vptr->dev, size, vptr->rx.ring, vptr->rx.pool_dma);
1675 static int velocity_init_rings(struct velocity_info *vptr, int mtu)
1679 velocity_set_rxbufsize(vptr, mtu);
1681 ret = velocity_init_dma_rings(vptr);
1685 ret = velocity_init_rd_ring(vptr);
1687 goto err_free_dma_rings_0;
1689 ret = velocity_init_td_ring(vptr);
1691 goto err_free_rd_ring_1;
1696 velocity_free_rd_ring(vptr);
1697 err_free_dma_rings_0:
1698 velocity_free_dma_rings(vptr);
1703 * velocity_free_tx_buf - free transmit buffer
1707 * Release an transmit buffer. If the buffer was preallocated then
1708 * recycle it, if not then unmap the buffer.
1710 static void velocity_free_tx_buf(struct velocity_info *vptr,
1711 struct velocity_td_info *tdinfo, struct tx_desc *td)
1713 struct sk_buff *skb = tdinfo->skb;
1717 * Don't unmap the pre-allocated tx_bufs
1719 for (i = 0; i < tdinfo->nskb_dma; i++) {
1720 size_t pktlen = max_t(size_t, skb->len, ETH_ZLEN);
1722 /* For scatter-gather */
1723 if (skb_shinfo(skb)->nr_frags > 0)
1724 pktlen = max_t(size_t, pktlen,
1725 td->td_buf[i].size & ~TD_QUEUE);
1727 dma_unmap_single(vptr->dev, tdinfo->skb_dma[i],
1728 le16_to_cpu(pktlen), DMA_TO_DEVICE);
1730 dev_kfree_skb_irq(skb);
1735 * FIXME: could we merge this with velocity_free_tx_buf ?
1737 static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1740 struct velocity_td_info *td_info = &(vptr->tx.infos[q][n]);
1743 if (td_info == NULL)
1747 for (i = 0; i < td_info->nskb_dma; i++) {
1748 if (td_info->skb_dma[i]) {
1749 dma_unmap_single(vptr->dev, td_info->skb_dma[i],
1750 td_info->skb->len, DMA_TO_DEVICE);
1751 td_info->skb_dma[i] = 0;
1754 dev_kfree_skb(td_info->skb);
1755 td_info->skb = NULL;
1760 * velocity_free_td_ring - free td ring
1763 * Free up the transmit ring for this particular velocity adapter.
1764 * We free the ring contents but not the ring itself.
1766 static void velocity_free_td_ring(struct velocity_info *vptr)
1770 for (j = 0; j < vptr->tx.numq; j++) {
1771 if (vptr->tx.infos[j] == NULL)
1773 for (i = 0; i < vptr->options.numtx; i++)
1774 velocity_free_td_ring_entry(vptr, j, i);
1776 kfree(vptr->tx.infos[j]);
1777 vptr->tx.infos[j] = NULL;
1781 static void velocity_free_rings(struct velocity_info *vptr)
1783 velocity_free_td_ring(vptr);
1784 velocity_free_rd_ring(vptr);
1785 velocity_free_dma_rings(vptr);
1789 * velocity_error - handle error from controller
1791 * @status: card status
1793 * Process an error report from the hardware and attempt to recover
1794 * the card itself. At the moment we cannot recover from some
1795 * theoretically impossible errors but this could be fixed using
1796 * the pci_device_failed logic to bounce the hardware
1799 static void velocity_error(struct velocity_info *vptr, int status)
1802 if (status & ISR_TXSTLI) {
1803 struct mac_regs __iomem *regs = vptr->mac_regs;
1805 printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(®s->TDIdx[0]));
1806 BYTE_REG_BITS_ON(TXESR_TDSTR, ®s->TXESR);
1807 writew(TRDCSR_RUN, ®s->TDCSRClr);
1808 netif_stop_queue(vptr->netdev);
1810 /* FIXME: port over the pci_device_failed code and use it
1814 if (status & ISR_SRCI) {
1815 struct mac_regs __iomem *regs = vptr->mac_regs;
1818 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1819 vptr->mii_status = check_connection_type(regs);
1822 * If it is a 3119, disable frame bursting in
1823 * halfduplex mode and enable it in fullduplex
1826 if (vptr->rev_id < REV_ID_VT3216_A0) {
1827 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1828 BYTE_REG_BITS_ON(TCR_TB2BDIS, ®s->TCR);
1830 BYTE_REG_BITS_OFF(TCR_TB2BDIS, ®s->TCR);
1833 * Only enable CD heart beat counter in 10HD mode
1835 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10))
1836 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, ®s->TESTCFG);
1838 BYTE_REG_BITS_ON(TESTCFG_HBDIS, ®s->TESTCFG);
1840 setup_queue_timers(vptr);
1843 * Get link status from PHYSR0
1845 linked = readb(®s->PHYSR0) & PHYSR0_LINKGD;
1848 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1849 netif_carrier_on(vptr->netdev);
1851 vptr->mii_status |= VELOCITY_LINK_FAIL;
1852 netif_carrier_off(vptr->netdev);
1855 velocity_print_link_status(vptr);
1856 enable_flow_control_ability(vptr);
1859 * Re-enable auto-polling because SRCI will disable
1863 enable_mii_autopoll(regs);
1865 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1866 netif_stop_queue(vptr->netdev);
1868 netif_wake_queue(vptr->netdev);
1871 if (status & ISR_MIBFI)
1872 velocity_update_hw_mibs(vptr);
1873 if (status & ISR_LSTEI)
1874 mac_rx_queue_wake(vptr->mac_regs);
1878 * tx_srv - transmit interrupt service
1881 * Scan the queues looking for transmitted packets that
1882 * we can complete and clean up. Update any statistics as
1885 static int velocity_tx_srv(struct velocity_info *vptr)
1892 struct velocity_td_info *tdinfo;
1893 struct net_device_stats *stats = &vptr->netdev->stats;
1895 for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
1896 for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
1897 idx = (idx + 1) % vptr->options.numtx) {
1902 td = &(vptr->tx.rings[qnum][idx]);
1903 tdinfo = &(vptr->tx.infos[qnum][idx]);
1905 if (td->tdesc0.len & OWNED_BY_NIC)
1911 if (td->tdesc0.TSR & TSR0_TERR) {
1913 stats->tx_dropped++;
1914 if (td->tdesc0.TSR & TSR0_CDH)
1915 stats->tx_heartbeat_errors++;
1916 if (td->tdesc0.TSR & TSR0_CRS)
1917 stats->tx_carrier_errors++;
1918 if (td->tdesc0.TSR & TSR0_ABT)
1919 stats->tx_aborted_errors++;
1920 if (td->tdesc0.TSR & TSR0_OWC)
1921 stats->tx_window_errors++;
1923 stats->tx_packets++;
1924 stats->tx_bytes += tdinfo->skb->len;
1926 velocity_free_tx_buf(vptr, tdinfo, td);
1927 vptr->tx.used[qnum]--;
1929 vptr->tx.tail[qnum] = idx;
1931 if (AVAIL_TD(vptr, qnum) < 1)
1935 * Look to see if we should kick the transmit network
1936 * layer for more work.
1938 if (netif_queue_stopped(vptr->netdev) && (full == 0) &&
1939 (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1940 netif_wake_queue(vptr->netdev);
1946 * velocity_rx_csum - checksum process
1947 * @rd: receive packet descriptor
1948 * @skb: network layer packet buffer
1950 * Process the status bits for the received packet and determine
1951 * if the checksum was computed and verified by the hardware
1953 static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1955 skb_checksum_none_assert(skb);
1957 if (rd->rdesc1.CSM & CSM_IPKT) {
1958 if (rd->rdesc1.CSM & CSM_IPOK) {
1959 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
1960 (rd->rdesc1.CSM & CSM_UDPKT)) {
1961 if (!(rd->rdesc1.CSM & CSM_TUPOK))
1964 skb->ip_summed = CHECKSUM_UNNECESSARY;
1970 * velocity_rx_copy - in place Rx copy for small packets
1971 * @rx_skb: network layer packet buffer candidate
1972 * @pkt_size: received data size
1973 * @rd: receive packet descriptor
1974 * @dev: network device
1976 * Replace the current skb that is scheduled for Rx processing by a
1977 * shorter, immediately allocated skb, if the received packet is small
1978 * enough. This function returns a negative value if the received
1979 * packet is too big or if memory is exhausted.
1981 static int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
1982 struct velocity_info *vptr)
1985 if (pkt_size < rx_copybreak) {
1986 struct sk_buff *new_skb;
1988 new_skb = netdev_alloc_skb_ip_align(vptr->netdev, pkt_size);
1990 new_skb->ip_summed = rx_skb[0]->ip_summed;
1991 skb_copy_from_linear_data(*rx_skb, new_skb->data, pkt_size);
2001 * velocity_iph_realign - IP header alignment
2002 * @vptr: velocity we are handling
2003 * @skb: network layer packet buffer
2004 * @pkt_size: received data size
2006 * Align IP header on a 2 bytes boundary. This behavior can be
2007 * configured by the user.
2009 static inline void velocity_iph_realign(struct velocity_info *vptr,
2010 struct sk_buff *skb, int pkt_size)
2012 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
2013 memmove(skb->data + 2, skb->data, pkt_size);
2014 skb_reserve(skb, 2);
2019 * velocity_receive_frame - received packet processor
2020 * @vptr: velocity we are handling
2023 * A packet has arrived. We process the packet and if appropriate
2024 * pass the frame up the network stack
2026 static int velocity_receive_frame(struct velocity_info *vptr, int idx)
2028 struct net_device_stats *stats = &vptr->netdev->stats;
2029 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
2030 struct rx_desc *rd = &(vptr->rx.ring[idx]);
2031 int pkt_len = le16_to_cpu(rd->rdesc0.len) & 0x3fff;
2032 struct sk_buff *skb;
2034 if (unlikely(rd->rdesc0.RSR & (RSR_STP | RSR_EDP | RSR_RL))) {
2035 if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP))
2036 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame spans multiple RDs.\n", vptr->netdev->name);
2037 stats->rx_length_errors++;
2041 if (rd->rdesc0.RSR & RSR_MAR)
2046 dma_sync_single_for_cpu(vptr->dev, rd_info->skb_dma,
2047 vptr->rx.buf_sz, DMA_FROM_DEVICE);
2049 velocity_rx_csum(rd, skb);
2051 if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
2052 velocity_iph_realign(vptr, skb, pkt_len);
2053 rd_info->skb = NULL;
2054 dma_unmap_single(vptr->dev, rd_info->skb_dma, vptr->rx.buf_sz,
2057 dma_sync_single_for_device(vptr->dev, rd_info->skb_dma,
2058 vptr->rx.buf_sz, DMA_FROM_DEVICE);
2061 skb_put(skb, pkt_len - 4);
2062 skb->protocol = eth_type_trans(skb, vptr->netdev);
2064 if (rd->rdesc0.RSR & RSR_DETAG) {
2065 u16 vid = swab16(le16_to_cpu(rd->rdesc1.PQTAG));
2067 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
2069 netif_receive_skb(skb);
2071 stats->rx_bytes += pkt_len;
2072 stats->rx_packets++;
2078 * velocity_rx_srv - service RX interrupt
2081 * Walk the receive ring of the velocity adapter and remove
2082 * any received packets from the receive queue. Hand the ring
2083 * slots back to the adapter for reuse.
2085 static int velocity_rx_srv(struct velocity_info *vptr, int budget_left)
2087 struct net_device_stats *stats = &vptr->netdev->stats;
2088 int rd_curr = vptr->rx.curr;
2091 while (works < budget_left) {
2092 struct rx_desc *rd = vptr->rx.ring + rd_curr;
2094 if (!vptr->rx.info[rd_curr].skb)
2097 if (rd->rdesc0.len & OWNED_BY_NIC)
2103 * Don't drop CE or RL error frame although RXOK is off
2105 if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
2106 if (velocity_receive_frame(vptr, rd_curr) < 0)
2107 stats->rx_dropped++;
2109 if (rd->rdesc0.RSR & RSR_CRC)
2110 stats->rx_crc_errors++;
2111 if (rd->rdesc0.RSR & RSR_FAE)
2112 stats->rx_frame_errors++;
2114 stats->rx_dropped++;
2117 rd->size |= RX_INTEN;
2120 if (rd_curr >= vptr->options.numrx)
2125 vptr->rx.curr = rd_curr;
2127 if ((works > 0) && (velocity_rx_refill(vptr) > 0))
2128 velocity_give_many_rx_descs(vptr);
2134 static int velocity_poll(struct napi_struct *napi, int budget)
2136 struct velocity_info *vptr = container_of(napi,
2137 struct velocity_info, napi);
2138 unsigned int rx_done;
2139 unsigned long flags;
2142 * Do rx and tx twice for performance (taken from the VIA
2143 * out-of-tree driver).
2145 rx_done = velocity_rx_srv(vptr, budget);
2146 spin_lock_irqsave(&vptr->lock, flags);
2147 velocity_tx_srv(vptr);
2148 /* If budget not fully consumed, exit the polling mode */
2149 if (rx_done < budget) {
2150 napi_complete_done(napi, rx_done);
2151 mac_enable_int(vptr->mac_regs);
2153 spin_unlock_irqrestore(&vptr->lock, flags);
2159 * velocity_intr - interrupt callback
2160 * @irq: interrupt number
2161 * @dev_instance: interrupting device
2163 * Called whenever an interrupt is generated by the velocity
2164 * adapter IRQ line. We may not be the source of the interrupt
2165 * and need to identify initially if we are, and if not exit as
2166 * efficiently as possible.
2168 static irqreturn_t velocity_intr(int irq, void *dev_instance)
2170 struct net_device *dev = dev_instance;
2171 struct velocity_info *vptr = netdev_priv(dev);
2174 spin_lock(&vptr->lock);
2175 isr_status = mac_read_isr(vptr->mac_regs);
2178 if (isr_status == 0) {
2179 spin_unlock(&vptr->lock);
2183 /* Ack the interrupt */
2184 mac_write_isr(vptr->mac_regs, isr_status);
2186 if (likely(napi_schedule_prep(&vptr->napi))) {
2187 mac_disable_int(vptr->mac_regs);
2188 __napi_schedule(&vptr->napi);
2191 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2192 velocity_error(vptr, isr_status);
2194 spin_unlock(&vptr->lock);
2200 * velocity_open - interface activation callback
2201 * @dev: network layer device to open
2203 * Called when the network layer brings the interface up. Returns
2204 * a negative posix error code on failure, or zero on success.
2206 * All the ring allocation and set up is done on open for this
2207 * adapter to minimise memory usage when inactive
2209 static int velocity_open(struct net_device *dev)
2211 struct velocity_info *vptr = netdev_priv(dev);
2214 ret = velocity_init_rings(vptr, dev->mtu);
2218 /* Ensure chip is running */
2219 velocity_set_power_state(vptr, PCI_D0);
2221 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
2223 ret = request_irq(dev->irq, velocity_intr, IRQF_SHARED,
2226 /* Power down the chip */
2227 velocity_set_power_state(vptr, PCI_D3hot);
2228 velocity_free_rings(vptr);
2232 velocity_give_many_rx_descs(vptr);
2234 mac_enable_int(vptr->mac_regs);
2235 netif_start_queue(dev);
2236 napi_enable(&vptr->napi);
2237 vptr->flags |= VELOCITY_FLAGS_OPENED;
2243 * velocity_shutdown - shut down the chip
2244 * @vptr: velocity to deactivate
2246 * Shuts down the internal operations of the velocity and
2247 * disables interrupts, autopolling, transmit and receive
2249 static void velocity_shutdown(struct velocity_info *vptr)
2251 struct mac_regs __iomem *regs = vptr->mac_regs;
2252 mac_disable_int(regs);
2253 writel(CR0_STOP, ®s->CR0Set);
2254 writew(0xFFFF, ®s->TDCSRClr);
2255 writeb(0xFF, ®s->RDCSRClr);
2256 safe_disable_mii_autopoll(regs);
2257 mac_clear_isr(regs);
2261 * velocity_change_mtu - MTU change callback
2262 * @dev: network device
2263 * @new_mtu: desired MTU
2265 * Handle requests from the networking layer for MTU change on
2266 * this interface. It gets called on a change by the network layer.
2267 * Return zero for success or negative posix error code.
2269 static int velocity_change_mtu(struct net_device *dev, int new_mtu)
2271 struct velocity_info *vptr = netdev_priv(dev);
2274 if (!netif_running(dev)) {
2279 if (dev->mtu != new_mtu) {
2280 struct velocity_info *tmp_vptr;
2281 unsigned long flags;
2285 tmp_vptr = kzalloc(sizeof(*tmp_vptr), GFP_KERNEL);
2291 tmp_vptr->netdev = dev;
2292 tmp_vptr->pdev = vptr->pdev;
2293 tmp_vptr->dev = vptr->dev;
2294 tmp_vptr->options = vptr->options;
2295 tmp_vptr->tx.numq = vptr->tx.numq;
2297 ret = velocity_init_rings(tmp_vptr, new_mtu);
2299 goto out_free_tmp_vptr_1;
2301 napi_disable(&vptr->napi);
2303 spin_lock_irqsave(&vptr->lock, flags);
2305 netif_stop_queue(dev);
2306 velocity_shutdown(vptr);
2311 vptr->rx = tmp_vptr->rx;
2312 vptr->tx = tmp_vptr->tx;
2319 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
2321 velocity_give_many_rx_descs(vptr);
2323 napi_enable(&vptr->napi);
2325 mac_enable_int(vptr->mac_regs);
2326 netif_start_queue(dev);
2328 spin_unlock_irqrestore(&vptr->lock, flags);
2330 velocity_free_rings(tmp_vptr);
2332 out_free_tmp_vptr_1:
2339 #ifdef CONFIG_NET_POLL_CONTROLLER
2341 * velocity_poll_controller - Velocity Poll controller function
2342 * @dev: network device
2345 * Used by NETCONSOLE and other diagnostic tools to allow network I/P
2346 * with interrupts disabled.
2348 static void velocity_poll_controller(struct net_device *dev)
2350 disable_irq(dev->irq);
2351 velocity_intr(dev->irq, dev);
2352 enable_irq(dev->irq);
2357 * velocity_mii_ioctl - MII ioctl handler
2358 * @dev: network device
2359 * @ifr: the ifreq block for the ioctl
2362 * Process MII requests made via ioctl from the network layer. These
2363 * are used by tools like kudzu to interrogate the link state of the
2366 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2368 struct velocity_info *vptr = netdev_priv(dev);
2369 struct mac_regs __iomem *regs = vptr->mac_regs;
2370 unsigned long flags;
2371 struct mii_ioctl_data *miidata = if_mii(ifr);
2376 miidata->phy_id = readb(®s->MIIADR) & 0x1f;
2379 if (velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
2383 spin_lock_irqsave(&vptr->lock, flags);
2384 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
2385 spin_unlock_irqrestore(&vptr->lock, flags);
2386 check_connection_type(vptr->mac_regs);
2397 * velocity_ioctl - ioctl entry point
2398 * @dev: network device
2399 * @rq: interface request ioctl
2400 * @cmd: command code
2402 * Called when the user issues an ioctl request to the network
2403 * device in question. The velocity interface supports MII.
2405 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2407 struct velocity_info *vptr = netdev_priv(dev);
2410 /* If we are asked for information and the device is power
2411 saving then we need to bring the device back up to talk to it */
2413 if (!netif_running(dev))
2414 velocity_set_power_state(vptr, PCI_D0);
2417 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2418 case SIOCGMIIREG: /* Read MII PHY register. */
2419 case SIOCSMIIREG: /* Write to MII PHY register. */
2420 ret = velocity_mii_ioctl(dev, rq, cmd);
2426 if (!netif_running(dev))
2427 velocity_set_power_state(vptr, PCI_D3hot);
2434 * velocity_get_status - statistics callback
2435 * @dev: network device
2437 * Callback from the network layer to allow driver statistics
2438 * to be resynchronized with hardware collected state. In the
2439 * case of the velocity we need to pull the MIB counters from
2440 * the hardware into the counters before letting the network
2441 * layer display them.
2443 static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2445 struct velocity_info *vptr = netdev_priv(dev);
2447 /* If the hardware is down, don't touch MII */
2448 if (!netif_running(dev))
2451 spin_lock_irq(&vptr->lock);
2452 velocity_update_hw_mibs(vptr);
2453 spin_unlock_irq(&vptr->lock);
2455 dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2456 dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2457 dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2459 // unsigned long rx_dropped; /* no space in linux buffers */
2460 dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2461 /* detailed rx_errors: */
2462 // unsigned long rx_length_errors;
2463 // unsigned long rx_over_errors; /* receiver ring buff overflow */
2464 dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2465 // unsigned long rx_frame_errors; /* recv'd frame alignment error */
2466 // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2467 // unsigned long rx_missed_errors; /* receiver missed packet */
2469 /* detailed tx_errors */
2470 // unsigned long tx_fifo_errors;
2476 * velocity_close - close adapter callback
2477 * @dev: network device
2479 * Callback from the network layer when the velocity is being
2480 * deactivated by the network layer
2482 static int velocity_close(struct net_device *dev)
2484 struct velocity_info *vptr = netdev_priv(dev);
2486 napi_disable(&vptr->napi);
2487 netif_stop_queue(dev);
2488 velocity_shutdown(vptr);
2490 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
2491 velocity_get_ip(vptr);
2493 free_irq(dev->irq, dev);
2495 velocity_free_rings(vptr);
2497 vptr->flags &= (~VELOCITY_FLAGS_OPENED);
2502 * velocity_xmit - transmit packet callback
2503 * @skb: buffer to transmit
2504 * @dev: network device
2506 * Called by the networ layer to request a packet is queued to
2507 * the velocity. Returns zero on success.
2509 static netdev_tx_t velocity_xmit(struct sk_buff *skb,
2510 struct net_device *dev)
2512 struct velocity_info *vptr = netdev_priv(dev);
2514 struct tx_desc *td_ptr;
2515 struct velocity_td_info *tdinfo;
2516 unsigned long flags;
2521 if (skb_padto(skb, ETH_ZLEN))
2524 /* The hardware can handle at most 7 memory segments, so merge
2525 * the skb if there are more */
2526 if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
2527 dev_kfree_skb_any(skb);
2528 return NETDEV_TX_OK;
2531 pktlen = skb_shinfo(skb)->nr_frags == 0 ?
2532 max_t(unsigned int, skb->len, ETH_ZLEN) :
2535 spin_lock_irqsave(&vptr->lock, flags);
2537 index = vptr->tx.curr[qnum];
2538 td_ptr = &(vptr->tx.rings[qnum][index]);
2539 tdinfo = &(vptr->tx.infos[qnum][index]);
2541 td_ptr->tdesc1.TCR = TCR0_TIC;
2542 td_ptr->td_buf[0].size &= ~TD_QUEUE;
2545 * Map the linear network buffer into PCI space and
2546 * add it to the transmit ring.
2549 tdinfo->skb_dma[0] = dma_map_single(vptr->dev, skb->data, pktlen,
2551 td_ptr->tdesc0.len = cpu_to_le16(pktlen);
2552 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
2553 td_ptr->td_buf[0].pa_high = 0;
2554 td_ptr->td_buf[0].size = cpu_to_le16(pktlen);
2556 /* Handle fragments */
2557 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2558 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2560 tdinfo->skb_dma[i + 1] = skb_frag_dma_map(vptr->dev,
2562 skb_frag_size(frag),
2565 td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
2566 td_ptr->td_buf[i + 1].pa_high = 0;
2567 td_ptr->td_buf[i + 1].size = cpu_to_le16(skb_frag_size(frag));
2569 tdinfo->nskb_dma = i + 1;
2571 td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;
2573 if (skb_vlan_tag_present(skb)) {
2574 td_ptr->tdesc1.vlan = cpu_to_le16(skb_vlan_tag_get(skb));
2575 td_ptr->tdesc1.TCR |= TCR0_VETAG;
2579 * Handle hardware checksum
2581 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2582 const struct iphdr *ip = ip_hdr(skb);
2583 if (ip->protocol == IPPROTO_TCP)
2584 td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2585 else if (ip->protocol == IPPROTO_UDP)
2586 td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2587 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2592 prev = vptr->options.numtx - 1;
2593 td_ptr->tdesc0.len |= OWNED_BY_NIC;
2594 vptr->tx.used[qnum]++;
2595 vptr->tx.curr[qnum] = (index + 1) % vptr->options.numtx;
2597 if (AVAIL_TD(vptr, qnum) < 1)
2598 netif_stop_queue(dev);
2600 td_ptr = &(vptr->tx.rings[qnum][prev]);
2601 td_ptr->td_buf[0].size |= TD_QUEUE;
2602 mac_tx_queue_wake(vptr->mac_regs, qnum);
2604 spin_unlock_irqrestore(&vptr->lock, flags);
2606 return NETDEV_TX_OK;
2609 static const struct net_device_ops velocity_netdev_ops = {
2610 .ndo_open = velocity_open,
2611 .ndo_stop = velocity_close,
2612 .ndo_start_xmit = velocity_xmit,
2613 .ndo_get_stats = velocity_get_stats,
2614 .ndo_validate_addr = eth_validate_addr,
2615 .ndo_set_mac_address = eth_mac_addr,
2616 .ndo_set_rx_mode = velocity_set_multi,
2617 .ndo_change_mtu = velocity_change_mtu,
2618 .ndo_do_ioctl = velocity_ioctl,
2619 .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
2620 .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
2621 #ifdef CONFIG_NET_POLL_CONTROLLER
2622 .ndo_poll_controller = velocity_poll_controller,
2627 * velocity_init_info - init private data
2629 * @vptr: Velocity info
2632 * Set up the initial velocity_info struct for the device that has been
2635 static void velocity_init_info(struct velocity_info *vptr,
2636 const struct velocity_info_tbl *info)
2638 vptr->chip_id = info->chip_id;
2639 vptr->tx.numq = info->txqueue;
2640 vptr->multicast_limit = MCAM_SIZE;
2641 spin_lock_init(&vptr->lock);
2645 * velocity_get_pci_info - retrieve PCI info for device
2646 * @vptr: velocity device
2647 * @pdev: PCI device it matches
2649 * Retrieve the PCI configuration space data that interests us from
2650 * the kernel PCI layer
2652 static int velocity_get_pci_info(struct velocity_info *vptr)
2654 struct pci_dev *pdev = vptr->pdev;
2656 pci_set_master(pdev);
2658 vptr->ioaddr = pci_resource_start(pdev, 0);
2659 vptr->memaddr = pci_resource_start(pdev, 1);
2661 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
2663 "region #0 is not an I/O resource, aborting.\n");
2667 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
2669 "region #1 is an I/O resource, aborting.\n");
2673 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
2674 dev_err(&pdev->dev, "region #1 is too small.\n");
2682 * velocity_get_platform_info - retrieve platform info for device
2683 * @vptr: velocity device
2684 * @pdev: platform device it matches
2686 * Retrieve the Platform configuration data that interests us
2688 static int velocity_get_platform_info(struct velocity_info *vptr)
2690 struct resource res;
2693 if (of_get_property(vptr->dev->of_node, "no-eeprom", NULL))
2694 vptr->no_eeprom = 1;
2696 ret = of_address_to_resource(vptr->dev->of_node, 0, &res);
2698 dev_err(vptr->dev, "unable to find memory address\n");
2702 vptr->memaddr = res.start;
2704 if (resource_size(&res) < VELOCITY_IO_SIZE) {
2705 dev_err(vptr->dev, "memory region is too small.\n");
2713 * velocity_print_info - per driver data
2716 * Print per driver data as the kernel driver finds Velocity
2719 static void velocity_print_info(struct velocity_info *vptr)
2721 struct net_device *dev = vptr->netdev;
2723 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
2724 printk(KERN_INFO "%s: Ethernet Address: %pM\n",
2725 dev->name, dev->dev_addr);
2728 static u32 velocity_get_link(struct net_device *dev)
2730 struct velocity_info *vptr = netdev_priv(dev);
2731 struct mac_regs __iomem *regs = vptr->mac_regs;
2732 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, ®s->PHYSR0) ? 1 : 0;
2736 * velocity_probe - set up discovered velocity device
2738 * @ent: PCI device table entry that matched
2739 * @bustype: bus that device is connected to
2741 * Configure a discovered adapter from scratch. Return a negative
2742 * errno error code on failure paths.
2744 static int velocity_probe(struct device *dev, int irq,
2745 const struct velocity_info_tbl *info,
2746 enum velocity_bus_type bustype)
2748 static int first = 1;
2749 struct net_device *netdev;
2751 const char *drv_string;
2752 struct velocity_info *vptr;
2753 struct mac_regs __iomem *regs;
2756 /* FIXME: this driver, like almost all other ethernet drivers,
2757 * can support more than MAX_UNITS.
2759 if (velocity_nics >= MAX_UNITS) {
2760 dev_notice(dev, "already found %d NICs.\n", velocity_nics);
2764 netdev = alloc_etherdev(sizeof(struct velocity_info));
2768 /* Chain it all together */
2770 SET_NETDEV_DEV(netdev, dev);
2771 vptr = netdev_priv(netdev);
2774 printk(KERN_INFO "%s Ver. %s\n",
2775 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
2776 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
2777 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
2782 vptr->netdev = netdev;
2785 velocity_init_info(vptr, info);
2787 if (bustype == BUS_PCI) {
2788 vptr->pdev = to_pci_dev(dev);
2790 ret = velocity_get_pci_info(vptr);
2795 ret = velocity_get_platform_info(vptr);
2800 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
2806 vptr->mac_regs = regs;
2807 vptr->rev_id = readb(®s->rev_id);
2809 mac_wol_reset(regs);
2811 for (i = 0; i < 6; i++)
2812 netdev->dev_addr[i] = readb(®s->PAR[i]);
2815 drv_string = dev_driver_string(dev);
2817 velocity_get_options(&vptr->options, velocity_nics, drv_string);
2820 * Mask out the options cannot be set to the chip
2823 vptr->options.flags &= info->flags;
2826 * Enable the chip specified capbilities
2829 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
2831 vptr->wol_opts = vptr->options.wol_opts;
2832 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2834 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
2836 netdev->netdev_ops = &velocity_netdev_ops;
2837 netdev->ethtool_ops = &velocity_ethtool_ops;
2838 netif_napi_add(netdev, &vptr->napi, velocity_poll,
2839 VELOCITY_NAPI_WEIGHT);
2841 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
2842 NETIF_F_HW_VLAN_CTAG_TX;
2843 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX |
2844 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX |
2847 /* MTU range: 64 - 9000 */
2848 netdev->min_mtu = VELOCITY_MIN_MTU;
2849 netdev->max_mtu = VELOCITY_MAX_MTU;
2851 ret = register_netdev(netdev);
2855 if (!velocity_get_link(netdev)) {
2856 netif_carrier_off(netdev);
2857 vptr->mii_status |= VELOCITY_LINK_FAIL;
2860 velocity_print_info(vptr);
2861 dev_set_drvdata(vptr->dev, netdev);
2863 /* and leave the chip powered down */
2865 velocity_set_power_state(vptr, PCI_D3hot);
2871 netif_napi_del(&vptr->napi);
2874 free_netdev(netdev);
2879 * velocity_remove - device unplug
2880 * @dev: device being removed
2882 * Device unload callback. Called on an unplug or on module
2883 * unload for each active device that is present. Disconnects
2884 * the device from the network layer and frees all the resources
2886 static int velocity_remove(struct device *dev)
2888 struct net_device *netdev = dev_get_drvdata(dev);
2889 struct velocity_info *vptr = netdev_priv(netdev);
2891 unregister_netdev(netdev);
2892 netif_napi_del(&vptr->napi);
2893 iounmap(vptr->mac_regs);
2894 free_netdev(netdev);
2900 static int velocity_pci_probe(struct pci_dev *pdev,
2901 const struct pci_device_id *ent)
2903 const struct velocity_info_tbl *info =
2904 &chip_info_table[ent->driver_data];
2907 ret = pci_enable_device(pdev);
2911 ret = pci_request_regions(pdev, VELOCITY_NAME);
2913 dev_err(&pdev->dev, "No PCI resources.\n");
2917 ret = velocity_probe(&pdev->dev, pdev->irq, info, BUS_PCI);
2921 pci_release_regions(pdev);
2923 pci_disable_device(pdev);
2927 static void velocity_pci_remove(struct pci_dev *pdev)
2929 velocity_remove(&pdev->dev);
2931 pci_release_regions(pdev);
2932 pci_disable_device(pdev);
2935 static int velocity_platform_probe(struct platform_device *pdev)
2937 const struct of_device_id *of_id;
2938 const struct velocity_info_tbl *info;
2941 of_id = of_match_device(velocity_of_ids, &pdev->dev);
2946 irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
2950 return velocity_probe(&pdev->dev, irq, info, BUS_PLATFORM);
2953 static int velocity_platform_remove(struct platform_device *pdev)
2955 velocity_remove(&pdev->dev);
2960 #ifdef CONFIG_PM_SLEEP
2962 * wol_calc_crc - WOL CRC
2963 * @pattern: data pattern
2964 * @mask_pattern: mask
2966 * Compute the wake on lan crc hashes for the packet header
2967 * we are interested in.
2969 static u16 wol_calc_crc(int size, u8 *pattern, u8 *mask_pattern)
2975 for (i = 0; i < size; i++) {
2976 mask = mask_pattern[i];
2978 /* Skip this loop if the mask equals to zero */
2982 for (j = 0; j < 8; j++) {
2983 if ((mask & 0x01) == 0) {
2988 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
2991 /* Finally, invert the result once to get the correct data */
2993 return bitrev32(crc) >> 16;
2997 * velocity_set_wol - set up for wake on lan
2998 * @vptr: velocity to set WOL status on
3000 * Set a card up for wake on lan either by unicast or by
3003 * FIXME: check static buffer is safe here
3005 static int velocity_set_wol(struct velocity_info *vptr)
3007 struct mac_regs __iomem *regs = vptr->mac_regs;
3008 enum speed_opt spd_dpx = vptr->options.spd_dpx;
3012 static u32 mask_pattern[2][4] = {
3013 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3014 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3017 writew(0xFFFF, ®s->WOLCRClr);
3018 writeb(WOLCFG_SAB | WOLCFG_SAM, ®s->WOLCFGSet);
3019 writew(WOLCR_MAGIC_EN, ®s->WOLCRSet);
3022 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3023 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), ®s->WOLCRSet);
3026 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
3027 writew(WOLCR_UNICAST_EN, ®s->WOLCRSet);
3029 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3030 struct arp_packet *arp = (struct arp_packet *) buf;
3032 memset(buf, 0, sizeof(struct arp_packet) + 7);
3034 for (i = 0; i < 4; i++)
3035 writel(mask_pattern[0][i], ®s->ByteMask[0][i]);
3037 arp->type = htons(ETH_P_ARP);
3038 arp->ar_op = htons(1);
3040 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3042 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3043 (u8 *) & mask_pattern[0][0]);
3045 writew(crc, ®s->PatternCRC[0]);
3046 writew(WOLCR_ARP_EN, ®s->WOLCRSet);
3049 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, ®s->PWCFGSet);
3050 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, ®s->PWCFGSet);
3052 writew(0x0FFF, ®s->WOLSRClr);
3054 if (spd_dpx == SPD_DPX_1000_FULL)
3057 if (spd_dpx != SPD_DPX_AUTO)
3058 goto advertise_done;
3060 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3061 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3062 MII_REG_BITS_ON(AUXCR_MDPPS, MII_NCONFIG, vptr->mac_regs);
3064 MII_REG_BITS_OFF(ADVERTISE_1000FULL | ADVERTISE_1000HALF, MII_CTRL1000, vptr->mac_regs);
3067 if (vptr->mii_status & VELOCITY_SPEED_1000)
3068 MII_REG_BITS_ON(BMCR_ANRESTART, MII_BMCR, vptr->mac_regs);
3071 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, ®s->CHIPGCR);
3075 GCR = readb(®s->CHIPGCR);
3076 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3077 writeb(GCR, ®s->CHIPGCR);
3081 BYTE_REG_BITS_OFF(ISR_PWEI, ®s->ISR);
3082 /* Turn on SWPTAG just before entering power mode */
3083 BYTE_REG_BITS_ON(STICKHW_SWPTAG, ®s->STICKHW);
3084 /* Go to bed ..... */
3085 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), ®s->STICKHW);
3091 * velocity_save_context - save registers
3093 * @context: buffer for stored context
3095 * Retrieve the current configuration from the velocity hardware
3096 * and stash it in the context structure, for use by the context
3097 * restore functions. This allows us to save things we need across
3100 static void velocity_save_context(struct velocity_info *vptr, struct velocity_context *context)
3102 struct mac_regs __iomem *regs = vptr->mac_regs;
3104 u8 __iomem *ptr = (u8 __iomem *)regs;
3106 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3107 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3109 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3110 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3112 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3113 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3117 static int velocity_suspend(struct device *dev)
3119 struct net_device *netdev = dev_get_drvdata(dev);
3120 struct velocity_info *vptr = netdev_priv(netdev);
3121 unsigned long flags;
3123 if (!netif_running(vptr->netdev))
3126 netif_device_detach(vptr->netdev);
3128 spin_lock_irqsave(&vptr->lock, flags);
3130 pci_save_state(vptr->pdev);
3132 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3133 velocity_get_ip(vptr);
3134 velocity_save_context(vptr, &vptr->context);
3135 velocity_shutdown(vptr);
3136 velocity_set_wol(vptr);
3138 pci_enable_wake(vptr->pdev, PCI_D3hot, 1);
3139 velocity_set_power_state(vptr, PCI_D3hot);
3141 velocity_save_context(vptr, &vptr->context);
3142 velocity_shutdown(vptr);
3144 pci_disable_device(vptr->pdev);
3145 velocity_set_power_state(vptr, PCI_D3hot);
3148 spin_unlock_irqrestore(&vptr->lock, flags);
3153 * velocity_restore_context - restore registers
3155 * @context: buffer for stored context
3157 * Reload the register configuration from the velocity context
3158 * created by velocity_save_context.
3160 static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3162 struct mac_regs __iomem *regs = vptr->mac_regs;
3164 u8 __iomem *ptr = (u8 __iomem *)regs;
3166 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4)
3167 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3170 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3172 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3174 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3177 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4)
3178 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3180 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3181 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3183 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++)
3184 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3187 static int velocity_resume(struct device *dev)
3189 struct net_device *netdev = dev_get_drvdata(dev);
3190 struct velocity_info *vptr = netdev_priv(netdev);
3191 unsigned long flags;
3194 if (!netif_running(vptr->netdev))
3197 velocity_set_power_state(vptr, PCI_D0);
3200 pci_enable_wake(vptr->pdev, PCI_D0, 0);
3201 pci_restore_state(vptr->pdev);
3204 mac_wol_reset(vptr->mac_regs);
3206 spin_lock_irqsave(&vptr->lock, flags);
3207 velocity_restore_context(vptr, &vptr->context);
3208 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3209 mac_disable_int(vptr->mac_regs);
3211 velocity_tx_srv(vptr);
3213 for (i = 0; i < vptr->tx.numq; i++) {
3214 if (vptr->tx.used[i])
3215 mac_tx_queue_wake(vptr->mac_regs, i);
3218 mac_enable_int(vptr->mac_regs);
3219 spin_unlock_irqrestore(&vptr->lock, flags);
3220 netif_device_attach(vptr->netdev);
3224 #endif /* CONFIG_PM_SLEEP */
3226 static SIMPLE_DEV_PM_OPS(velocity_pm_ops, velocity_suspend, velocity_resume);
3229 * Definition for our device driver. The PCI layer interface
3230 * uses this to handle all our card discover and plugging
3232 static struct pci_driver velocity_pci_driver = {
3233 .name = VELOCITY_NAME,
3234 .id_table = velocity_pci_id_table,
3235 .probe = velocity_pci_probe,
3236 .remove = velocity_pci_remove,
3238 .pm = &velocity_pm_ops,
3242 static struct platform_driver velocity_platform_driver = {
3243 .probe = velocity_platform_probe,
3244 .remove = velocity_platform_remove,
3246 .name = "via-velocity",
3247 .of_match_table = velocity_of_ids,
3248 .pm = &velocity_pm_ops,
3253 * velocity_ethtool_up - pre hook for ethtool
3254 * @dev: network device
3256 * Called before an ethtool operation. We need to make sure the
3257 * chip is out of D3 state before we poke at it.
3259 static int velocity_ethtool_up(struct net_device *dev)
3261 struct velocity_info *vptr = netdev_priv(dev);
3262 if (!netif_running(dev))
3263 velocity_set_power_state(vptr, PCI_D0);
3268 * velocity_ethtool_down - post hook for ethtool
3269 * @dev: network device
3271 * Called after an ethtool operation. Restore the chip back to D3
3272 * state if it isn't running.
3274 static void velocity_ethtool_down(struct net_device *dev)
3276 struct velocity_info *vptr = netdev_priv(dev);
3277 if (!netif_running(dev))
3278 velocity_set_power_state(vptr, PCI_D3hot);
3281 static int velocity_get_link_ksettings(struct net_device *dev,
3282 struct ethtool_link_ksettings *cmd)
3284 struct velocity_info *vptr = netdev_priv(dev);
3285 struct mac_regs __iomem *regs = vptr->mac_regs;
3287 u32 supported, advertising;
3289 status = check_connection_type(vptr->mac_regs);
3291 supported = SUPPORTED_TP |
3293 SUPPORTED_10baseT_Half |
3294 SUPPORTED_10baseT_Full |
3295 SUPPORTED_100baseT_Half |
3296 SUPPORTED_100baseT_Full |
3297 SUPPORTED_1000baseT_Half |
3298 SUPPORTED_1000baseT_Full;
3300 advertising = ADVERTISED_TP | ADVERTISED_Autoneg;
3301 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
3303 ADVERTISED_10baseT_Half |
3304 ADVERTISED_10baseT_Full |
3305 ADVERTISED_100baseT_Half |
3306 ADVERTISED_100baseT_Full |
3307 ADVERTISED_1000baseT_Half |
3308 ADVERTISED_1000baseT_Full;
3310 switch (vptr->options.spd_dpx) {
3311 case SPD_DPX_1000_FULL:
3312 advertising |= ADVERTISED_1000baseT_Full;
3314 case SPD_DPX_100_HALF:
3315 advertising |= ADVERTISED_100baseT_Half;
3317 case SPD_DPX_100_FULL:
3318 advertising |= ADVERTISED_100baseT_Full;
3320 case SPD_DPX_10_HALF:
3321 advertising |= ADVERTISED_10baseT_Half;
3323 case SPD_DPX_10_FULL:
3324 advertising |= ADVERTISED_10baseT_Full;
3331 if (status & VELOCITY_SPEED_1000)
3332 cmd->base.speed = SPEED_1000;
3333 else if (status & VELOCITY_SPEED_100)
3334 cmd->base.speed = SPEED_100;
3336 cmd->base.speed = SPEED_10;
3338 cmd->base.autoneg = (status & VELOCITY_AUTONEG_ENABLE) ?
3339 AUTONEG_ENABLE : AUTONEG_DISABLE;
3340 cmd->base.port = PORT_TP;
3341 cmd->base.phy_address = readb(®s->MIIADR) & 0x1F;
3343 if (status & VELOCITY_DUPLEX_FULL)
3344 cmd->base.duplex = DUPLEX_FULL;
3346 cmd->base.duplex = DUPLEX_HALF;
3348 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
3350 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
3356 static int velocity_set_link_ksettings(struct net_device *dev,
3357 const struct ethtool_link_ksettings *cmd)
3359 struct velocity_info *vptr = netdev_priv(dev);
3360 u32 speed = cmd->base.speed;
3365 curr_status = check_connection_type(vptr->mac_regs);
3366 curr_status &= (~VELOCITY_LINK_FAIL);
3368 new_status |= ((cmd->base.autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
3369 new_status |= ((speed == SPEED_1000) ? VELOCITY_SPEED_1000 : 0);
3370 new_status |= ((speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
3371 new_status |= ((speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
3372 new_status |= ((cmd->base.duplex == DUPLEX_FULL) ?
3373 VELOCITY_DUPLEX_FULL : 0);
3375 if ((new_status & VELOCITY_AUTONEG_ENABLE) &&
3376 (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE))) {
3379 enum speed_opt spd_dpx;
3381 if (new_status & VELOCITY_AUTONEG_ENABLE)
3382 spd_dpx = SPD_DPX_AUTO;
3383 else if ((new_status & VELOCITY_SPEED_1000) &&
3384 (new_status & VELOCITY_DUPLEX_FULL)) {
3385 spd_dpx = SPD_DPX_1000_FULL;
3386 } else if (new_status & VELOCITY_SPEED_100)
3387 spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
3388 SPD_DPX_100_FULL : SPD_DPX_100_HALF;
3389 else if (new_status & VELOCITY_SPEED_10)
3390 spd_dpx = (new_status & VELOCITY_DUPLEX_FULL) ?
3391 SPD_DPX_10_FULL : SPD_DPX_10_HALF;
3395 vptr->options.spd_dpx = spd_dpx;
3397 velocity_set_media_mode(vptr, new_status);
3403 static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3405 struct velocity_info *vptr = netdev_priv(dev);
3407 strlcpy(info->driver, VELOCITY_NAME, sizeof(info->driver));
3408 strlcpy(info->version, VELOCITY_VERSION, sizeof(info->version));
3410 strlcpy(info->bus_info, pci_name(vptr->pdev),
3411 sizeof(info->bus_info));
3413 strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
3416 static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3418 struct velocity_info *vptr = netdev_priv(dev);
3419 wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
3420 wol->wolopts |= WAKE_MAGIC;
3422 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3423 wol.wolopts|=WAKE_PHY;
3425 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
3426 wol->wolopts |= WAKE_UCAST;
3427 if (vptr->wol_opts & VELOCITY_WOL_ARP)
3428 wol->wolopts |= WAKE_ARP;
3429 memcpy(&wol->sopass, vptr->wol_passwd, 6);
3432 static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3434 struct velocity_info *vptr = netdev_priv(dev);
3436 if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
3438 vptr->wol_opts = VELOCITY_WOL_MAGIC;
3441 if (wol.wolopts & WAKE_PHY) {
3442 vptr->wol_opts|=VELOCITY_WOL_PHY;
3443 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
3447 if (wol->wolopts & WAKE_MAGIC) {
3448 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
3449 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3451 if (wol->wolopts & WAKE_UCAST) {
3452 vptr->wol_opts |= VELOCITY_WOL_UCAST;
3453 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3455 if (wol->wolopts & WAKE_ARP) {
3456 vptr->wol_opts |= VELOCITY_WOL_ARP;
3457 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
3459 memcpy(vptr->wol_passwd, wol->sopass, 6);
3463 static u32 velocity_get_msglevel(struct net_device *dev)
3468 static void velocity_set_msglevel(struct net_device *dev, u32 value)
3473 static int get_pending_timer_val(int val)
3475 int mult_bits = val >> 6;
3491 return (val & 0x3f) * mult;
3494 static void set_pending_timer_val(int *val, u32 us)
3500 mult = 1; /* mult with 4 */
3503 if (us >= 0x3f * 4) {
3504 mult = 2; /* mult with 16 */
3507 if (us >= 0x3f * 16) {
3508 mult = 3; /* mult with 64 */
3512 *val = (mult << 6) | ((us >> shift) & 0x3f);
3516 static int velocity_get_coalesce(struct net_device *dev,
3517 struct ethtool_coalesce *ecmd)
3519 struct velocity_info *vptr = netdev_priv(dev);
3521 ecmd->tx_max_coalesced_frames = vptr->options.tx_intsup;
3522 ecmd->rx_max_coalesced_frames = vptr->options.rx_intsup;
3524 ecmd->rx_coalesce_usecs = get_pending_timer_val(vptr->options.rxqueue_timer);
3525 ecmd->tx_coalesce_usecs = get_pending_timer_val(vptr->options.txqueue_timer);
3530 static int velocity_set_coalesce(struct net_device *dev,
3531 struct ethtool_coalesce *ecmd)
3533 struct velocity_info *vptr = netdev_priv(dev);
3534 int max_us = 0x3f * 64;
3535 unsigned long flags;
3538 if (ecmd->tx_coalesce_usecs > max_us)
3540 if (ecmd->rx_coalesce_usecs > max_us)
3543 if (ecmd->tx_max_coalesced_frames > 0xff)
3545 if (ecmd->rx_max_coalesced_frames > 0xff)
3548 vptr->options.rx_intsup = ecmd->rx_max_coalesced_frames;
3549 vptr->options.tx_intsup = ecmd->tx_max_coalesced_frames;
3551 set_pending_timer_val(&vptr->options.rxqueue_timer,
3552 ecmd->rx_coalesce_usecs);
3553 set_pending_timer_val(&vptr->options.txqueue_timer,
3554 ecmd->tx_coalesce_usecs);
3556 /* Setup the interrupt suppression and queue timers */
3557 spin_lock_irqsave(&vptr->lock, flags);
3558 mac_disable_int(vptr->mac_regs);
3559 setup_adaptive_interrupts(vptr);
3560 setup_queue_timers(vptr);
3562 mac_write_int_mask(vptr->int_mask, vptr->mac_regs);
3563 mac_clear_isr(vptr->mac_regs);
3564 mac_enable_int(vptr->mac_regs);
3565 spin_unlock_irqrestore(&vptr->lock, flags);
3570 static const char velocity_gstrings[][ETH_GSTRING_LEN] = {
3589 "tx_ether_collisions",
3593 "rx_mac_control_frames",
3594 "tx_mac_control_frames",
3595 "rx_frame_alignement_errors",
3601 "in_range_length_errors",
3605 static void velocity_get_strings(struct net_device *dev, u32 sset, u8 *data)
3609 memcpy(data, *velocity_gstrings, sizeof(velocity_gstrings));
3614 static int velocity_get_sset_count(struct net_device *dev, int sset)
3618 return ARRAY_SIZE(velocity_gstrings);
3624 static void velocity_get_ethtool_stats(struct net_device *dev,
3625 struct ethtool_stats *stats, u64 *data)
3627 if (netif_running(dev)) {
3628 struct velocity_info *vptr = netdev_priv(dev);
3629 u32 *p = vptr->mib_counter;
3632 spin_lock_irq(&vptr->lock);
3633 velocity_update_hw_mibs(vptr);
3634 spin_unlock_irq(&vptr->lock);
3636 for (i = 0; i < ARRAY_SIZE(velocity_gstrings); i++)
3641 static const struct ethtool_ops velocity_ethtool_ops = {
3642 .get_drvinfo = velocity_get_drvinfo,
3643 .get_wol = velocity_ethtool_get_wol,
3644 .set_wol = velocity_ethtool_set_wol,
3645 .get_msglevel = velocity_get_msglevel,
3646 .set_msglevel = velocity_set_msglevel,
3647 .get_link = velocity_get_link,
3648 .get_strings = velocity_get_strings,
3649 .get_sset_count = velocity_get_sset_count,
3650 .get_ethtool_stats = velocity_get_ethtool_stats,
3651 .get_coalesce = velocity_get_coalesce,
3652 .set_coalesce = velocity_set_coalesce,
3653 .begin = velocity_ethtool_up,
3654 .complete = velocity_ethtool_down,
3655 .get_link_ksettings = velocity_get_link_ksettings,
3656 .set_link_ksettings = velocity_set_link_ksettings,
3659 #if defined(CONFIG_PM) && defined(CONFIG_INET)
3660 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3662 struct in_ifaddr *ifa = ptr;
3663 struct net_device *dev = ifa->ifa_dev->dev;
3665 if (dev_net(dev) == &init_net &&
3666 dev->netdev_ops == &velocity_netdev_ops)
3667 velocity_get_ip(netdev_priv(dev));
3672 static struct notifier_block velocity_inetaddr_notifier = {
3673 .notifier_call = velocity_netdev_event,
3676 static void velocity_register_notifier(void)
3678 register_inetaddr_notifier(&velocity_inetaddr_notifier);
3681 static void velocity_unregister_notifier(void)
3683 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
3688 #define velocity_register_notifier() do {} while (0)
3689 #define velocity_unregister_notifier() do {} while (0)
3691 #endif /* defined(CONFIG_PM) && defined(CONFIG_INET) */
3694 * velocity_init_module - load time function
3696 * Called when the velocity module is loaded. The PCI driver
3697 * is registered with the PCI layer, and in turn will call
3698 * the probe functions for each velocity adapter installed
3701 static int __init velocity_init_module(void)
3703 int ret_pci, ret_platform;
3705 velocity_register_notifier();
3707 ret_pci = pci_register_driver(&velocity_pci_driver);
3708 ret_platform = platform_driver_register(&velocity_platform_driver);
3710 /* if both_registers failed, remove the notifier */
3711 if ((ret_pci < 0) && (ret_platform < 0)) {
3712 velocity_unregister_notifier();
3720 * velocity_cleanup - module unload
3722 * When the velocity hardware is unloaded this function is called.
3723 * It will clean up the notifiers and the unregister the PCI
3724 * driver interface for this hardware. This in turn cleans up
3725 * all discovered interfaces before returning from the function
3727 static void __exit velocity_cleanup_module(void)
3729 velocity_unregister_notifier();
3731 pci_unregister_driver(&velocity_pci_driver);
3732 platform_driver_unregister(&velocity_platform_driver);
3735 module_init(velocity_init_module);
3736 module_exit(velocity_cleanup_module);