[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / drivers / net / via-velocity.c
blobc2d5907dc8e054edd6ecab18bdc954cfbfd1554b
1 /*
2 * This code is derived from the VIA reference driver (copyright message
3 * below) provided to Red Hat by VIA Networking Technologies, Inc. for
4 * addition to the Linux kernel.
6 * The code has been merged into one source file, cleaned up to follow
7 * Linux coding style, ported to the Linux 2.6 kernel tree and cleaned
8 * for 64bit hardware platforms.
10 * TODO
11 * Big-endian support
12 * rx_copybreak/alignment
13 * Scatter gather
14 * More testing
16 * The changes are (c) Copyright 2004, Red Hat Inc. <alan@redhat.com>
17 * Additional fixes and clean up: Francois Romieu
19 * This source has not been verified for use in safety critical systems.
21 * Please direct queries about the revamped driver to the linux-kernel
22 * list not VIA.
24 * Original code:
26 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
27 * All rights reserved.
29 * This software may be redistributed and/or modified under
30 * the terms of the GNU General Public License as published by the Free
31 * Software Foundation; either version 2 of the License, or
32 * any later version.
34 * This program is distributed in the hope that it will be useful, but
35 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
36 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
37 * for more details.
39 * Author: Chuang Liang-Shing, AJ Jiang
41 * Date: Jan 24, 2003
43 * MODULE_LICENSE("GPL");
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/config.h>
51 #include <linux/init.h>
52 #include <linux/mm.h>
53 #include <linux/errno.h>
54 #include <linux/ioport.h>
55 #include <linux/pci.h>
56 #include <linux/kernel.h>
57 #include <linux/netdevice.h>
58 #include <linux/etherdevice.h>
59 #include <linux/skbuff.h>
60 #include <linux/delay.h>
61 #include <linux/timer.h>
62 #include <linux/slab.h>
63 #include <linux/interrupt.h>
64 #include <linux/string.h>
65 #include <linux/wait.h>
66 #include <asm/io.h>
67 #include <linux/if.h>
68 #include <linux/config.h>
69 #include <asm/uaccess.h>
70 #include <linux/proc_fs.h>
71 #include <linux/inetdevice.h>
72 #include <linux/reboot.h>
73 #include <linux/ethtool.h>
74 #include <linux/mii.h>
75 #include <linux/in.h>
76 #include <linux/if_arp.h>
77 #include <linux/ip.h>
78 #include <linux/tcp.h>
79 #include <linux/udp.h>
80 #include <linux/crc-ccitt.h>
81 #include <linux/crc32.h>
83 #include "via-velocity.h"
86 static int velocity_nics = 0;
87 static int msglevel = MSG_LEVEL_INFO;
90 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
91 static struct ethtool_ops velocity_ethtool_ops;
94 Define module options
97 MODULE_AUTHOR("VIA Networking Technologies, Inc.");
98 MODULE_LICENSE("GPL");
99 MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
101 #define VELOCITY_PARAM(N,D) \
102 static int N[MAX_UNITS]=OPTION_DEFAULT;\
103 module_param_array(N, int, NULL, 0); \
104 MODULE_PARM_DESC(N, D);
106 #define RX_DESC_MIN 64
107 #define RX_DESC_MAX 255
108 #define RX_DESC_DEF 64
109 VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
111 #define TX_DESC_MIN 16
112 #define TX_DESC_MAX 256
113 #define TX_DESC_DEF 64
114 VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
116 #define VLAN_ID_MIN 0
117 #define VLAN_ID_MAX 4095
118 #define VLAN_ID_DEF 0
119 /* VID_setting[] is used for setting the VID of NIC.
120 0: default VID.
121 1-4094: other VIDs.
123 VELOCITY_PARAM(VID_setting, "802.1Q VLAN ID");
125 #define RX_THRESH_MIN 0
126 #define RX_THRESH_MAX 3
127 #define RX_THRESH_DEF 0
128 /* rx_thresh[] is used for controlling the receive fifo threshold.
129 0: indicate the rxfifo threshold is 128 bytes.
130 1: indicate the rxfifo threshold is 512 bytes.
131 2: indicate the rxfifo threshold is 1024 bytes.
132 3: indicate the rxfifo threshold is store & forward.
134 VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
136 #define DMA_LENGTH_MIN 0
137 #define DMA_LENGTH_MAX 7
138 #define DMA_LENGTH_DEF 0
140 /* DMA_length[] is used for controlling the DMA length
141 0: 8 DWORDs
142 1: 16 DWORDs
143 2: 32 DWORDs
144 3: 64 DWORDs
145 4: 128 DWORDs
146 5: 256 DWORDs
147 6: SF(flush till emply)
148 7: SF(flush till emply)
150 VELOCITY_PARAM(DMA_length, "DMA length");
152 #define TAGGING_DEF 0
153 /* enable_tagging[] is used for enabling 802.1Q VID tagging.
154 0: disable VID seeting(default).
155 1: enable VID setting.
157 VELOCITY_PARAM(enable_tagging, "Enable 802.1Q tagging");
159 #define IP_ALIG_DEF 0
160 /* IP_byte_align[] is used for IP header DWORD byte aligned
161 0: indicate the IP header won't be DWORD byte aligned.(Default) .
162 1: indicate the IP header will be DWORD byte aligned.
163 In some enviroment, the IP header should be DWORD byte aligned,
164 or the packet will be droped when we receive it. (eg: IPVS)
166 VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
168 #define TX_CSUM_DEF 1
169 /* txcsum_offload[] is used for setting the checksum offload ability of NIC.
170 (We only support RX checksum offload now)
171 0: disable csum_offload[checksum offload
172 1: enable checksum offload. (Default)
174 VELOCITY_PARAM(txcsum_offload, "Enable transmit packet checksum offload");
176 #define FLOW_CNTL_DEF 1
177 #define FLOW_CNTL_MIN 1
178 #define FLOW_CNTL_MAX 5
180 /* flow_control[] is used for setting the flow control ability of NIC.
181 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
182 2: enable TX flow control.
183 3: enable RX flow control.
184 4: enable RX/TX flow control.
185 5: disable
187 VELOCITY_PARAM(flow_control, "Enable flow control ability");
189 #define MED_LNK_DEF 0
190 #define MED_LNK_MIN 0
191 #define MED_LNK_MAX 4
192 /* speed_duplex[] is used for setting the speed and duplex mode of NIC.
193 0: indicate autonegotiation for both speed and duplex mode
194 1: indicate 100Mbps half duplex mode
195 2: indicate 100Mbps full duplex mode
196 3: indicate 10Mbps half duplex mode
197 4: indicate 10Mbps full duplex mode
199 Note:
200 if EEPROM have been set to the force mode, this option is ignored
201 by driver.
203 VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
205 #define VAL_PKT_LEN_DEF 0
206 /* ValPktLen[] is used for setting the checksum offload ability of NIC.
207 0: Receive frame with invalid layer 2 length (Default)
208 1: Drop frame with invalid layer 2 length
210 VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
212 #define WOL_OPT_DEF 0
213 #define WOL_OPT_MIN 0
214 #define WOL_OPT_MAX 7
215 /* wol_opts[] is used for controlling wake on lan behavior.
216 0: Wake up if recevied a magic packet. (Default)
217 1: Wake up if link status is on/off.
218 2: Wake up if recevied an arp packet.
219 4: Wake up if recevied any unicast packet.
220 Those value can be sumed up to support more than one option.
222 VELOCITY_PARAM(wol_opts, "Wake On Lan options");
224 #define INT_WORKS_DEF 20
225 #define INT_WORKS_MIN 10
226 #define INT_WORKS_MAX 64
228 VELOCITY_PARAM(int_works, "Number of packets per interrupt services");
230 static int rx_copybreak = 200;
231 module_param(rx_copybreak, int, 0644);
232 MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
234 static void velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr, struct velocity_info_tbl *info);
235 static int velocity_get_pci_info(struct velocity_info *, struct pci_dev *pdev);
236 static void velocity_print_info(struct velocity_info *vptr);
237 static int velocity_open(struct net_device *dev);
238 static int velocity_change_mtu(struct net_device *dev, int mtu);
239 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev);
240 static int velocity_intr(int irq, void *dev_instance, struct pt_regs *regs);
241 static void velocity_set_multi(struct net_device *dev);
242 static struct net_device_stats *velocity_get_stats(struct net_device *dev);
243 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
244 static int velocity_close(struct net_device *dev);
245 static int velocity_receive_frame(struct velocity_info *, int idx);
246 static int velocity_alloc_rx_buf(struct velocity_info *, int idx);
247 static void velocity_free_rd_ring(struct velocity_info *vptr);
248 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *);
249 static int velocity_soft_reset(struct velocity_info *vptr);
250 static void mii_init(struct velocity_info *vptr, u32 mii_status);
251 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr);
252 static void velocity_print_link_status(struct velocity_info *vptr);
253 static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs);
254 static void velocity_shutdown(struct velocity_info *vptr);
255 static void enable_flow_control_ability(struct velocity_info *vptr);
256 static void enable_mii_autopoll(struct mac_regs __iomem * regs);
257 static int velocity_mii_read(struct mac_regs __iomem *, u8 byIdx, u16 * pdata);
258 static int velocity_mii_write(struct mac_regs __iomem *, u8 byMiiAddr, u16 data);
259 static u32 mii_check_media_mode(struct mac_regs __iomem * regs);
260 static u32 check_connection_type(struct mac_regs __iomem * regs);
261 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status);
263 #ifdef CONFIG_PM
265 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state);
266 static int velocity_resume(struct pci_dev *pdev);
268 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr);
270 static struct notifier_block velocity_inetaddr_notifier = {
271 .notifier_call = velocity_netdev_event,
274 static DEFINE_SPINLOCK(velocity_dev_list_lock);
275 static LIST_HEAD(velocity_dev_list);
277 static void velocity_register_notifier(void)
279 register_inetaddr_notifier(&velocity_inetaddr_notifier);
282 static void velocity_unregister_notifier(void)
284 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
287 #else /* CONFIG_PM */
289 #define velocity_register_notifier() do {} while (0)
290 #define velocity_unregister_notifier() do {} while (0)
292 #endif /* !CONFIG_PM */
295 * Internal board variants. At the moment we have only one
298 static struct velocity_info_tbl chip_info_table[] = {
299 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 256, 1, 0x00FFFFFFUL},
300 {0, NULL}
304 * Describe the PCI device identifiers that we support in this
305 * device driver. Used for hotplug autoloading.
308 static struct pci_device_id velocity_id_table[] __devinitdata = {
309 {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X,
310 PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) chip_info_table},
311 {0, }
314 MODULE_DEVICE_TABLE(pci, velocity_id_table);
317 * get_chip_name - identifier to name
318 * @id: chip identifier
320 * Given a chip identifier return a suitable description. Returns
321 * a pointer a static string valid while the driver is loaded.
324 static char __devinit *get_chip_name(enum chip_type chip_id)
326 int i;
327 for (i = 0; chip_info_table[i].name != NULL; i++)
328 if (chip_info_table[i].chip_id == chip_id)
329 break;
330 return chip_info_table[i].name;
334 * velocity_remove1 - device unplug
335 * @pdev: PCI device being removed
337 * Device unload callback. Called on an unplug or on module
338 * unload for each active device that is present. Disconnects
339 * the device from the network layer and frees all the resources
342 static void __devexit velocity_remove1(struct pci_dev *pdev)
344 struct net_device *dev = pci_get_drvdata(pdev);
345 struct velocity_info *vptr = dev->priv;
347 #ifdef CONFIG_PM
348 unsigned long flags;
350 spin_lock_irqsave(&velocity_dev_list_lock, flags);
351 if (!list_empty(&velocity_dev_list))
352 list_del(&vptr->list);
353 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
354 #endif
355 unregister_netdev(dev);
356 iounmap(vptr->mac_regs);
357 pci_release_regions(pdev);
358 pci_disable_device(pdev);
359 pci_set_drvdata(pdev, NULL);
360 free_netdev(dev);
362 velocity_nics--;
366 * velocity_set_int_opt - parser for integer options
367 * @opt: pointer to option value
368 * @val: value the user requested (or -1 for default)
369 * @min: lowest value allowed
370 * @max: highest value allowed
371 * @def: default value
372 * @name: property name
373 * @dev: device name
375 * Set an integer property in the module options. This function does
376 * all the verification and checking as well as reporting so that
377 * we don't duplicate code for each option.
380 static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max, int def, char *name, char *devname)
382 if (val == -1)
383 *opt = def;
384 else if (val < min || val > max) {
385 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
386 devname, name, min, max);
387 *opt = def;
388 } else {
389 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
390 devname, name, val);
391 *opt = val;
396 * velocity_set_bool_opt - parser for boolean options
397 * @opt: pointer to option value
398 * @val: value the user requested (or -1 for default)
399 * @def: default value (yes/no)
400 * @flag: numeric value to set for true.
401 * @name: property name
402 * @dev: device name
404 * Set a boolean property in the module options. This function does
405 * all the verification and checking as well as reporting so that
406 * we don't duplicate code for each option.
409 static void __devinit velocity_set_bool_opt(u32 * opt, int val, int def, u32 flag, char *name, char *devname)
411 (*opt) &= (~flag);
412 if (val == -1)
413 *opt |= (def ? flag : 0);
414 else if (val < 0 || val > 1) {
415 printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
416 devname, name);
417 *opt |= (def ? flag : 0);
418 } else {
419 printk(KERN_INFO "%s: set parameter %s to %s\n",
420 devname, name, val ? "TRUE" : "FALSE");
421 *opt |= (val ? flag : 0);
426 * velocity_get_options - set options on device
427 * @opts: option structure for the device
428 * @index: index of option to use in module options array
429 * @devname: device name
431 * Turn the module and command options into a single structure
432 * for the current device
435 static void __devinit velocity_get_options(struct velocity_opt *opts, int index, char *devname)
438 velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
439 velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
440 velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
441 velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
442 velocity_set_int_opt(&opts->vid, VID_setting[index], VLAN_ID_MIN, VLAN_ID_MAX, VLAN_ID_DEF, "VID_setting", devname);
443 velocity_set_bool_opt(&opts->flags, enable_tagging[index], TAGGING_DEF, VELOCITY_FLAGS_TAGGING, "enable_tagging", devname);
444 velocity_set_bool_opt(&opts->flags, txcsum_offload[index], TX_CSUM_DEF, VELOCITY_FLAGS_TX_CSUM, "txcsum_offload", devname);
445 velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
446 velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
447 velocity_set_bool_opt(&opts->flags, ValPktLen[index], VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN, "ValPktLen", devname);
448 velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
449 velocity_set_int_opt((int *) &opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
450 velocity_set_int_opt((int *) &opts->int_works, int_works[index], INT_WORKS_MIN, INT_WORKS_MAX, INT_WORKS_DEF, "Interrupt service works", devname);
451 opts->numrx = (opts->numrx & ~3);
455 * velocity_init_cam_filter - initialise CAM
456 * @vptr: velocity to program
458 * Initialize the content addressable memory used for filters. Load
459 * appropriately according to the presence of VLAN
462 static void velocity_init_cam_filter(struct velocity_info *vptr)
464 struct mac_regs __iomem * regs = vptr->mac_regs;
466 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
467 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
468 WORD_REG_BITS_ON(MCFG_VIDFR, &regs->MCFG);
470 /* Disable all CAMs */
471 memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
472 memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
473 mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
474 mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
476 /* Enable first VCAM */
477 if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
478 /* If Tagging option is enabled and VLAN ID is not zero, then
479 turn on MCFG_RTGOPT also */
480 if (vptr->options.vid != 0)
481 WORD_REG_BITS_ON(MCFG_RTGOPT, &regs->MCFG);
483 mac_set_cam(regs, 0, (u8 *) & (vptr->options.vid), VELOCITY_VLAN_ID_CAM);
484 vptr->vCAMmask[0] |= 1;
485 mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
486 } else {
487 u16 temp = 0;
488 mac_set_cam(regs, 0, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
489 temp = 1;
490 mac_set_cam_mask(regs, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
495 * velocity_rx_reset - handle a receive reset
496 * @vptr: velocity we are resetting
498 * Reset the ownership and status for the receive ring side.
499 * Hand all the receive queue to the NIC.
502 static void velocity_rx_reset(struct velocity_info *vptr)
505 struct mac_regs __iomem * regs = vptr->mac_regs;
506 int i;
508 vptr->rd_dirty = vptr->rd_filled = vptr->rd_curr = 0;
511 * Init state, all RD entries belong to the NIC
513 for (i = 0; i < vptr->options.numrx; ++i)
514 vptr->rd_ring[i].rdesc0.owner = OWNED_BY_NIC;
516 writew(vptr->options.numrx, &regs->RBRDU);
517 writel(vptr->rd_pool_dma, &regs->RDBaseLo);
518 writew(0, &regs->RDIdx);
519 writew(vptr->options.numrx - 1, &regs->RDCSize);
523 * velocity_init_registers - initialise MAC registers
524 * @vptr: velocity to init
525 * @type: type of initialisation (hot or cold)
527 * Initialise the MAC on a reset or on first set up on the
528 * hardware.
531 static void velocity_init_registers(struct velocity_info *vptr,
532 enum velocity_init_type type)
534 struct mac_regs __iomem * regs = vptr->mac_regs;
535 int i, mii_status;
537 mac_wol_reset(regs);
539 switch (type) {
540 case VELOCITY_INIT_RESET:
541 case VELOCITY_INIT_WOL:
543 netif_stop_queue(vptr->dev);
546 * Reset RX to prevent RX pointer not on the 4X location
548 velocity_rx_reset(vptr);
549 mac_rx_queue_run(regs);
550 mac_rx_queue_wake(regs);
552 mii_status = velocity_get_opt_media_mode(vptr);
553 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
554 velocity_print_link_status(vptr);
555 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
556 netif_wake_queue(vptr->dev);
559 enable_flow_control_ability(vptr);
561 mac_clear_isr(regs);
562 writel(CR0_STOP, &regs->CR0Clr);
563 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
564 &regs->CR0Set);
566 break;
568 case VELOCITY_INIT_COLD:
569 default:
571 * Do reset
573 velocity_soft_reset(vptr);
574 mdelay(5);
576 mac_eeprom_reload(regs);
577 for (i = 0; i < 6; i++) {
578 writeb(vptr->dev->dev_addr[i], &(regs->PAR[i]));
581 * clear Pre_ACPI bit.
583 BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
584 mac_set_rx_thresh(regs, vptr->options.rx_thresh);
585 mac_set_dma_length(regs, vptr->options.DMA_length);
587 writeb(WOLCFG_SAM | WOLCFG_SAB, &regs->WOLCFGSet);
589 * Back off algorithm use original IEEE standard
591 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), &regs->CFGB);
594 * Init CAM filter
596 velocity_init_cam_filter(vptr);
599 * Set packet filter: Receive directed and broadcast address
601 velocity_set_multi(vptr->dev);
604 * Enable MII auto-polling
606 enable_mii_autopoll(regs);
608 vptr->int_mask = INT_MASK_DEF;
610 writel(cpu_to_le32(vptr->rd_pool_dma), &regs->RDBaseLo);
611 writew(vptr->options.numrx - 1, &regs->RDCSize);
612 mac_rx_queue_run(regs);
613 mac_rx_queue_wake(regs);
615 writew(vptr->options.numtx - 1, &regs->TDCSize);
617 for (i = 0; i < vptr->num_txq; i++) {
618 writel(cpu_to_le32(vptr->td_pool_dma[i]), &(regs->TDBaseLo[i]));
619 mac_tx_queue_run(regs, i);
622 init_flow_control_register(vptr);
624 writel(CR0_STOP, &regs->CR0Clr);
625 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), &regs->CR0Set);
627 mii_status = velocity_get_opt_media_mode(vptr);
628 netif_stop_queue(vptr->dev);
630 mii_init(vptr, mii_status);
632 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
633 velocity_print_link_status(vptr);
634 if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
635 netif_wake_queue(vptr->dev);
638 enable_flow_control_ability(vptr);
639 mac_hw_mibs_init(regs);
640 mac_write_int_mask(vptr->int_mask, regs);
641 mac_clear_isr(regs);
647 * velocity_soft_reset - soft reset
648 * @vptr: velocity to reset
650 * Kick off a soft reset of the velocity adapter and then poll
651 * until the reset sequence has completed before returning.
654 static int velocity_soft_reset(struct velocity_info *vptr)
656 struct mac_regs __iomem * regs = vptr->mac_regs;
657 int i = 0;
659 writel(CR0_SFRST, &regs->CR0Set);
661 for (i = 0; i < W_MAX_TIMEOUT; i++) {
662 udelay(5);
663 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
664 break;
667 if (i == W_MAX_TIMEOUT) {
668 writel(CR0_FORSRST, &regs->CR0Set);
669 /* FIXME: PCI POSTING */
670 /* delay 2ms */
671 mdelay(2);
673 return 0;
677 * velocity_found1 - set up discovered velocity card
678 * @pdev: PCI device
679 * @ent: PCI device table entry that matched
681 * Configure a discovered adapter from scratch. Return a negative
682 * errno error code on failure paths.
685 static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
687 static int first = 1;
688 struct net_device *dev;
689 int i;
690 struct velocity_info_tbl *info = (struct velocity_info_tbl *) ent->driver_data;
691 struct velocity_info *vptr;
692 struct mac_regs __iomem * regs;
693 int ret = -ENOMEM;
695 if (velocity_nics >= MAX_UNITS) {
696 printk(KERN_NOTICE VELOCITY_NAME ": already found %d NICs.\n",
697 velocity_nics);
698 return -ENODEV;
701 dev = alloc_etherdev(sizeof(struct velocity_info));
703 if (dev == NULL) {
704 printk(KERN_ERR VELOCITY_NAME ": allocate net device failed.\n");
705 goto out;
708 /* Chain it all together */
710 SET_MODULE_OWNER(dev);
711 SET_NETDEV_DEV(dev, &pdev->dev);
712 vptr = dev->priv;
715 if (first) {
716 printk(KERN_INFO "%s Ver. %s\n",
717 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
718 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
719 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
720 first = 0;
723 velocity_init_info(pdev, vptr, info);
725 vptr->dev = dev;
727 dev->irq = pdev->irq;
729 ret = pci_enable_device(pdev);
730 if (ret < 0)
731 goto err_free_dev;
733 ret = velocity_get_pci_info(vptr, pdev);
734 if (ret < 0) {
735 printk(KERN_ERR VELOCITY_NAME ": Failed to find PCI device.\n");
736 goto err_disable;
739 ret = pci_request_regions(pdev, VELOCITY_NAME);
740 if (ret < 0) {
741 printk(KERN_ERR VELOCITY_NAME ": Failed to find PCI device.\n");
742 goto err_disable;
745 regs = ioremap(vptr->memaddr, vptr->io_size);
746 if (regs == NULL) {
747 ret = -EIO;
748 goto err_release_res;
751 vptr->mac_regs = regs;
753 mac_wol_reset(regs);
755 dev->base_addr = vptr->ioaddr;
757 for (i = 0; i < 6; i++)
758 dev->dev_addr[i] = readb(&regs->PAR[i]);
761 velocity_get_options(&vptr->options, velocity_nics, dev->name);
764 * Mask out the options cannot be set to the chip
767 vptr->options.flags &= info->flags;
770 * Enable the chip specified capbilities
773 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
775 vptr->wol_opts = vptr->options.wol_opts;
776 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
778 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
780 dev->irq = pdev->irq;
781 dev->open = velocity_open;
782 dev->hard_start_xmit = velocity_xmit;
783 dev->stop = velocity_close;
784 dev->get_stats = velocity_get_stats;
785 dev->set_multicast_list = velocity_set_multi;
786 dev->do_ioctl = velocity_ioctl;
787 dev->ethtool_ops = &velocity_ethtool_ops;
788 dev->change_mtu = velocity_change_mtu;
789 #ifdef VELOCITY_ZERO_COPY_SUPPORT
790 dev->features |= NETIF_F_SG;
791 #endif
793 if (vptr->flags & VELOCITY_FLAGS_TX_CSUM) {
794 dev->features |= NETIF_F_IP_CSUM;
797 ret = register_netdev(dev);
798 if (ret < 0)
799 goto err_iounmap;
801 velocity_print_info(vptr);
802 pci_set_drvdata(pdev, dev);
804 /* and leave the chip powered down */
806 pci_set_power_state(pdev, PCI_D3hot);
807 #ifdef CONFIG_PM
809 unsigned long flags;
811 spin_lock_irqsave(&velocity_dev_list_lock, flags);
812 list_add(&vptr->list, &velocity_dev_list);
813 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
815 #endif
816 velocity_nics++;
817 out:
818 return ret;
820 err_iounmap:
821 iounmap(regs);
822 err_release_res:
823 pci_release_regions(pdev);
824 err_disable:
825 pci_disable_device(pdev);
826 err_free_dev:
827 free_netdev(dev);
828 goto out;
832 * velocity_print_info - per driver data
833 * @vptr: velocity
835 * Print per driver data as the kernel driver finds Velocity
836 * hardware
839 static void __devinit velocity_print_info(struct velocity_info *vptr)
841 struct net_device *dev = vptr->dev;
843 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
844 printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
845 dev->name,
846 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
847 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
851 * velocity_init_info - init private data
852 * @pdev: PCI device
853 * @vptr: Velocity info
854 * @info: Board type
856 * Set up the initial velocity_info struct for the device that has been
857 * discovered.
860 static void __devinit velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr, struct velocity_info_tbl *info)
862 memset(vptr, 0, sizeof(struct velocity_info));
864 vptr->pdev = pdev;
865 vptr->chip_id = info->chip_id;
866 vptr->io_size = info->io_size;
867 vptr->num_txq = info->txqueue;
868 vptr->multicast_limit = MCAM_SIZE;
869 spin_lock_init(&vptr->lock);
870 INIT_LIST_HEAD(&vptr->list);
874 * velocity_get_pci_info - retrieve PCI info for device
875 * @vptr: velocity device
876 * @pdev: PCI device it matches
878 * Retrieve the PCI configuration space data that interests us from
879 * the kernel PCI layer
882 static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
885 if(pci_read_config_byte(pdev, PCI_REVISION_ID, &vptr->rev_id) < 0)
886 return -EIO;
888 pci_set_master(pdev);
890 vptr->ioaddr = pci_resource_start(pdev, 0);
891 vptr->memaddr = pci_resource_start(pdev, 1);
893 if(!(pci_resource_flags(pdev, 0) & IORESOURCE_IO))
895 printk(KERN_ERR "%s: region #0 is not an I/O resource, aborting.\n",
896 pci_name(pdev));
897 return -EINVAL;
900 if((pci_resource_flags(pdev, 1) & IORESOURCE_IO))
902 printk(KERN_ERR "%s: region #1 is an I/O resource, aborting.\n",
903 pci_name(pdev));
904 return -EINVAL;
907 if(pci_resource_len(pdev, 1) < 256)
909 printk(KERN_ERR "%s: region #1 is too small.\n",
910 pci_name(pdev));
911 return -EINVAL;
913 vptr->pdev = pdev;
915 return 0;
919 * velocity_init_rings - set up DMA rings
920 * @vptr: Velocity to set up
922 * Allocate PCI mapped DMA rings for the receive and transmit layer
923 * to use.
926 static int velocity_init_rings(struct velocity_info *vptr)
928 int i;
929 unsigned int psize;
930 unsigned int tsize;
931 dma_addr_t pool_dma;
932 u8 *pool;
935 * Allocate all RD/TD rings a single pool
938 psize = vptr->options.numrx * sizeof(struct rx_desc) +
939 vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
942 * pci_alloc_consistent() fulfills the requirement for 64 bytes
943 * alignment
945 pool = pci_alloc_consistent(vptr->pdev, psize, &pool_dma);
947 if (pool == NULL) {
948 printk(KERN_ERR "%s : DMA memory allocation failed.\n",
949 vptr->dev->name);
950 return -ENOMEM;
953 memset(pool, 0, psize);
955 vptr->rd_ring = (struct rx_desc *) pool;
957 vptr->rd_pool_dma = pool_dma;
959 tsize = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
960 vptr->tx_bufs = pci_alloc_consistent(vptr->pdev, tsize,
961 &vptr->tx_bufs_dma);
963 if (vptr->tx_bufs == NULL) {
964 printk(KERN_ERR "%s: DMA memory allocation failed.\n",
965 vptr->dev->name);
966 pci_free_consistent(vptr->pdev, psize, pool, pool_dma);
967 return -ENOMEM;
970 memset(vptr->tx_bufs, 0, vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq);
972 i = vptr->options.numrx * sizeof(struct rx_desc);
973 pool += i;
974 pool_dma += i;
975 for (i = 0; i < vptr->num_txq; i++) {
976 int offset = vptr->options.numtx * sizeof(struct tx_desc);
978 vptr->td_pool_dma[i] = pool_dma;
979 vptr->td_rings[i] = (struct tx_desc *) pool;
980 pool += offset;
981 pool_dma += offset;
983 return 0;
987 * velocity_free_rings - free PCI ring pointers
988 * @vptr: Velocity to free from
990 * Clean up the PCI ring buffers allocated to this velocity.
993 static void velocity_free_rings(struct velocity_info *vptr)
995 int size;
997 size = vptr->options.numrx * sizeof(struct rx_desc) +
998 vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
1000 pci_free_consistent(vptr->pdev, size, vptr->rd_ring, vptr->rd_pool_dma);
1002 size = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
1004 pci_free_consistent(vptr->pdev, size, vptr->tx_bufs, vptr->tx_bufs_dma);
1007 static inline void velocity_give_many_rx_descs(struct velocity_info *vptr)
1009 struct mac_regs __iomem *regs = vptr->mac_regs;
1010 int avail, dirty, unusable;
1013 * RD number must be equal to 4X per hardware spec
1014 * (programming guide rev 1.20, p.13)
1016 if (vptr->rd_filled < 4)
1017 return;
1019 wmb();
1021 unusable = vptr->rd_filled & 0x0003;
1022 dirty = vptr->rd_dirty - unusable;
1023 for (avail = vptr->rd_filled & 0xfffc; avail; avail--) {
1024 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1025 vptr->rd_ring[dirty].rdesc0.owner = OWNED_BY_NIC;
1028 writew(vptr->rd_filled & 0xfffc, &regs->RBRDU);
1029 vptr->rd_filled = unusable;
1032 static int velocity_rx_refill(struct velocity_info *vptr)
1034 int dirty = vptr->rd_dirty, done = 0, ret = 0;
1036 do {
1037 struct rx_desc *rd = vptr->rd_ring + dirty;
1039 /* Fine for an all zero Rx desc at init time as well */
1040 if (rd->rdesc0.owner == OWNED_BY_NIC)
1041 break;
1043 if (!vptr->rd_info[dirty].skb) {
1044 ret = velocity_alloc_rx_buf(vptr, dirty);
1045 if (ret < 0)
1046 break;
1048 done++;
1049 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
1050 } while (dirty != vptr->rd_curr);
1052 if (done) {
1053 vptr->rd_dirty = dirty;
1054 vptr->rd_filled += done;
1055 velocity_give_many_rx_descs(vptr);
1058 return ret;
1062 * velocity_init_rd_ring - set up receive ring
1063 * @vptr: velocity to configure
1065 * Allocate and set up the receive buffers for each ring slot and
1066 * assign them to the network adapter.
1069 static int velocity_init_rd_ring(struct velocity_info *vptr)
1071 int ret = -ENOMEM;
1072 unsigned int rsize = sizeof(struct velocity_rd_info) *
1073 vptr->options.numrx;
1075 vptr->rd_info = kmalloc(rsize, GFP_KERNEL);
1076 if(vptr->rd_info == NULL)
1077 goto out;
1078 memset(vptr->rd_info, 0, rsize);
1080 vptr->rd_filled = vptr->rd_dirty = vptr->rd_curr = 0;
1082 ret = velocity_rx_refill(vptr);
1083 if (ret < 0) {
1084 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1085 "%s: failed to allocate RX buffer.\n", vptr->dev->name);
1086 velocity_free_rd_ring(vptr);
1088 out:
1089 return ret;
1093 * velocity_free_rd_ring - free receive ring
1094 * @vptr: velocity to clean up
1096 * Free the receive buffers for each ring slot and any
1097 * attached socket buffers that need to go away.
1100 static void velocity_free_rd_ring(struct velocity_info *vptr)
1102 int i;
1104 if (vptr->rd_info == NULL)
1105 return;
1107 for (i = 0; i < vptr->options.numrx; i++) {
1108 struct velocity_rd_info *rd_info = &(vptr->rd_info[i]);
1110 if (!rd_info->skb)
1111 continue;
1112 pci_unmap_single(vptr->pdev, rd_info->skb_dma, vptr->rx_buf_sz,
1113 PCI_DMA_FROMDEVICE);
1114 rd_info->skb_dma = (dma_addr_t) NULL;
1116 dev_kfree_skb(rd_info->skb);
1117 rd_info->skb = NULL;
1120 kfree(vptr->rd_info);
1121 vptr->rd_info = NULL;
1125 * velocity_init_td_ring - set up transmit ring
1126 * @vptr: velocity
1128 * Set up the transmit ring and chain the ring pointers together.
1129 * Returns zero on success or a negative posix errno code for
1130 * failure.
1133 static int velocity_init_td_ring(struct velocity_info *vptr)
1135 int i, j;
1136 dma_addr_t curr;
1137 struct tx_desc *td;
1138 struct velocity_td_info *td_info;
1139 unsigned int tsize = sizeof(struct velocity_td_info) *
1140 vptr->options.numtx;
1142 /* Init the TD ring entries */
1143 for (j = 0; j < vptr->num_txq; j++) {
1144 curr = vptr->td_pool_dma[j];
1146 vptr->td_infos[j] = kmalloc(tsize, GFP_KERNEL);
1147 if(vptr->td_infos[j] == NULL)
1149 while(--j >= 0)
1150 kfree(vptr->td_infos[j]);
1151 return -ENOMEM;
1153 memset(vptr->td_infos[j], 0, tsize);
1155 for (i = 0; i < vptr->options.numtx; i++, curr += sizeof(struct tx_desc)) {
1156 td = &(vptr->td_rings[j][i]);
1157 td_info = &(vptr->td_infos[j][i]);
1158 td_info->buf = vptr->tx_bufs +
1159 (j * vptr->options.numtx + i) * PKT_BUF_SZ;
1160 td_info->buf_dma = vptr->tx_bufs_dma +
1161 (j * vptr->options.numtx + i) * PKT_BUF_SZ;
1163 vptr->td_tail[j] = vptr->td_curr[j] = vptr->td_used[j] = 0;
1165 return 0;
1169 * FIXME: could we merge this with velocity_free_tx_buf ?
1172 static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1173 int q, int n)
1175 struct velocity_td_info * td_info = &(vptr->td_infos[q][n]);
1176 int i;
1178 if (td_info == NULL)
1179 return;
1181 if (td_info->skb) {
1182 for (i = 0; i < td_info->nskb_dma; i++)
1184 if (td_info->skb_dma[i]) {
1185 pci_unmap_single(vptr->pdev, td_info->skb_dma[i],
1186 td_info->skb->len, PCI_DMA_TODEVICE);
1187 td_info->skb_dma[i] = (dma_addr_t) NULL;
1190 dev_kfree_skb(td_info->skb);
1191 td_info->skb = NULL;
1196 * velocity_free_td_ring - free td ring
1197 * @vptr: velocity
1199 * Free up the transmit ring for this particular velocity adapter.
1200 * We free the ring contents but not the ring itself.
1203 static void velocity_free_td_ring(struct velocity_info *vptr)
1205 int i, j;
1207 for (j = 0; j < vptr->num_txq; j++) {
1208 if (vptr->td_infos[j] == NULL)
1209 continue;
1210 for (i = 0; i < vptr->options.numtx; i++) {
1211 velocity_free_td_ring_entry(vptr, j, i);
1214 kfree(vptr->td_infos[j]);
1215 vptr->td_infos[j] = NULL;
1220 * velocity_rx_srv - service RX interrupt
1221 * @vptr: velocity
1222 * @status: adapter status (unused)
1224 * Walk the receive ring of the velocity adapter and remove
1225 * any received packets from the receive queue. Hand the ring
1226 * slots back to the adapter for reuse.
1229 static int velocity_rx_srv(struct velocity_info *vptr, int status)
1231 struct net_device_stats *stats = &vptr->stats;
1232 int rd_curr = vptr->rd_curr;
1233 int works = 0;
1235 do {
1236 struct rx_desc *rd = vptr->rd_ring + rd_curr;
1238 if (!vptr->rd_info[rd_curr].skb)
1239 break;
1241 if (rd->rdesc0.owner == OWNED_BY_NIC)
1242 break;
1244 rmb();
1247 * Don't drop CE or RL error frame although RXOK is off
1249 if ((rd->rdesc0.RSR & RSR_RXOK) || (!(rd->rdesc0.RSR & RSR_RXOK) && (rd->rdesc0.RSR & (RSR_CE | RSR_RL)))) {
1250 if (velocity_receive_frame(vptr, rd_curr) < 0)
1251 stats->rx_dropped++;
1252 } else {
1253 if (rd->rdesc0.RSR & RSR_CRC)
1254 stats->rx_crc_errors++;
1255 if (rd->rdesc0.RSR & RSR_FAE)
1256 stats->rx_frame_errors++;
1258 stats->rx_dropped++;
1261 rd->inten = 1;
1263 vptr->dev->last_rx = jiffies;
1265 rd_curr++;
1266 if (rd_curr >= vptr->options.numrx)
1267 rd_curr = 0;
1268 } while (++works <= 15);
1270 vptr->rd_curr = rd_curr;
1272 if (works > 0 && velocity_rx_refill(vptr) < 0) {
1273 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1274 "%s: rx buf allocation failure\n", vptr->dev->name);
1277 VAR_USED(stats);
1278 return works;
1282 * velocity_rx_csum - checksum process
1283 * @rd: receive packet descriptor
1284 * @skb: network layer packet buffer
1286 * Process the status bits for the received packet and determine
1287 * if the checksum was computed and verified by the hardware
1290 static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1292 skb->ip_summed = CHECKSUM_NONE;
1294 if (rd->rdesc1.CSM & CSM_IPKT) {
1295 if (rd->rdesc1.CSM & CSM_IPOK) {
1296 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
1297 (rd->rdesc1.CSM & CSM_UDPKT)) {
1298 if (!(rd->rdesc1.CSM & CSM_TUPOK)) {
1299 return;
1302 skb->ip_summed = CHECKSUM_UNNECESSARY;
1308 * velocity_rx_copy - in place Rx copy for small packets
1309 * @rx_skb: network layer packet buffer candidate
1310 * @pkt_size: received data size
1311 * @rd: receive packet descriptor
1312 * @dev: network device
1314 * Replace the current skb that is scheduled for Rx processing by a
1315 * shorter, immediatly allocated skb, if the received packet is small
1316 * enough. This function returns a negative value if the received
1317 * packet is too big or if memory is exhausted.
1319 static inline int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
1320 struct velocity_info *vptr)
1322 int ret = -1;
1324 if (pkt_size < rx_copybreak) {
1325 struct sk_buff *new_skb;
1327 new_skb = dev_alloc_skb(pkt_size + 2);
1328 if (new_skb) {
1329 new_skb->dev = vptr->dev;
1330 new_skb->ip_summed = rx_skb[0]->ip_summed;
1332 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN)
1333 skb_reserve(new_skb, 2);
1335 memcpy(new_skb->data, rx_skb[0]->data, pkt_size);
1336 *rx_skb = new_skb;
1337 ret = 0;
1341 return ret;
1345 * velocity_iph_realign - IP header alignment
1346 * @vptr: velocity we are handling
1347 * @skb: network layer packet buffer
1348 * @pkt_size: received data size
1350 * Align IP header on a 2 bytes boundary. This behavior can be
1351 * configured by the user.
1353 static inline void velocity_iph_realign(struct velocity_info *vptr,
1354 struct sk_buff *skb, int pkt_size)
1356 /* FIXME - memmove ? */
1357 if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
1358 int i;
1360 for (i = pkt_size; i >= 0; i--)
1361 *(skb->data + i + 2) = *(skb->data + i);
1362 skb_reserve(skb, 2);
1367 * velocity_receive_frame - received packet processor
1368 * @vptr: velocity we are handling
1369 * @idx: ring index
1371 * A packet has arrived. We process the packet and if appropriate
1372 * pass the frame up the network stack
1375 static int velocity_receive_frame(struct velocity_info *vptr, int idx)
1377 void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int);
1378 struct net_device_stats *stats = &vptr->stats;
1379 struct velocity_rd_info *rd_info = &(vptr->rd_info[idx]);
1380 struct rx_desc *rd = &(vptr->rd_ring[idx]);
1381 int pkt_len = rd->rdesc0.len;
1382 struct sk_buff *skb;
1384 if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
1385 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->dev->name);
1386 stats->rx_length_errors++;
1387 return -EINVAL;
1390 if (rd->rdesc0.RSR & RSR_MAR)
1391 vptr->stats.multicast++;
1393 skb = rd_info->skb;
1394 skb->dev = vptr->dev;
1396 pci_dma_sync_single_for_cpu(vptr->pdev, rd_info->skb_dma,
1397 vptr->rx_buf_sz, PCI_DMA_FROMDEVICE);
1400 * Drop frame not meeting IEEE 802.3
1403 if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
1404 if (rd->rdesc0.RSR & RSR_RL) {
1405 stats->rx_length_errors++;
1406 return -EINVAL;
1410 pci_action = pci_dma_sync_single_for_device;
1412 velocity_rx_csum(rd, skb);
1414 if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
1415 velocity_iph_realign(vptr, skb, pkt_len);
1416 pci_action = pci_unmap_single;
1417 rd_info->skb = NULL;
1420 pci_action(vptr->pdev, rd_info->skb_dma, vptr->rx_buf_sz,
1421 PCI_DMA_FROMDEVICE);
1423 skb_put(skb, pkt_len - 4);
1424 skb->protocol = eth_type_trans(skb, skb->dev);
1426 stats->rx_bytes += pkt_len;
1427 netif_rx(skb);
1429 return 0;
1433 * velocity_alloc_rx_buf - allocate aligned receive buffer
1434 * @vptr: velocity
1435 * @idx: ring index
1437 * Allocate a new full sized buffer for the reception of a frame and
1438 * map it into PCI space for the hardware to use. The hardware
1439 * requires *64* byte alignment of the buffer which makes life
1440 * less fun than would be ideal.
1443 static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1445 struct rx_desc *rd = &(vptr->rd_ring[idx]);
1446 struct velocity_rd_info *rd_info = &(vptr->rd_info[idx]);
1448 rd_info->skb = dev_alloc_skb(vptr->rx_buf_sz + 64);
1449 if (rd_info->skb == NULL)
1450 return -ENOMEM;
1453 * Do the gymnastics to get the buffer head for data at
1454 * 64byte alignment.
1456 skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
1457 rd_info->skb->dev = vptr->dev;
1458 rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data, vptr->rx_buf_sz, PCI_DMA_FROMDEVICE);
1461 * Fill in the descriptor to match
1464 *((u32 *) & (rd->rdesc0)) = 0;
1465 rd->len = cpu_to_le32(vptr->rx_buf_sz);
1466 rd->inten = 1;
1467 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1468 rd->pa_high = 0;
1469 return 0;
1473 * tx_srv - transmit interrupt service
1474 * @vptr; Velocity
1475 * @status:
1477 * Scan the queues looking for transmitted packets that
1478 * we can complete and clean up. Update any statistics as
1479 * neccessary/
1482 static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
1484 struct tx_desc *td;
1485 int qnum;
1486 int full = 0;
1487 int idx;
1488 int works = 0;
1489 struct velocity_td_info *tdinfo;
1490 struct net_device_stats *stats = &vptr->stats;
1492 for (qnum = 0; qnum < vptr->num_txq; qnum++) {
1493 for (idx = vptr->td_tail[qnum]; vptr->td_used[qnum] > 0;
1494 idx = (idx + 1) % vptr->options.numtx) {
1497 * Get Tx Descriptor
1499 td = &(vptr->td_rings[qnum][idx]);
1500 tdinfo = &(vptr->td_infos[qnum][idx]);
1502 if (td->tdesc0.owner == OWNED_BY_NIC)
1503 break;
1505 if ((works++ > 15))
1506 break;
1508 if (td->tdesc0.TSR & TSR0_TERR) {
1509 stats->tx_errors++;
1510 stats->tx_dropped++;
1511 if (td->tdesc0.TSR & TSR0_CDH)
1512 stats->tx_heartbeat_errors++;
1513 if (td->tdesc0.TSR & TSR0_CRS)
1514 stats->tx_carrier_errors++;
1515 if (td->tdesc0.TSR & TSR0_ABT)
1516 stats->tx_aborted_errors++;
1517 if (td->tdesc0.TSR & TSR0_OWC)
1518 stats->tx_window_errors++;
1519 } else {
1520 stats->tx_packets++;
1521 stats->tx_bytes += tdinfo->skb->len;
1523 velocity_free_tx_buf(vptr, tdinfo);
1524 vptr->td_used[qnum]--;
1526 vptr->td_tail[qnum] = idx;
1528 if (AVAIL_TD(vptr, qnum) < 1) {
1529 full = 1;
1533 * Look to see if we should kick the transmit network
1534 * layer for more work.
1536 if (netif_queue_stopped(vptr->dev) && (full == 0)
1537 && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1538 netif_wake_queue(vptr->dev);
1540 return works;
1544 * velocity_print_link_status - link status reporting
1545 * @vptr: velocity to report on
1547 * Turn the link status of the velocity card into a kernel log
1548 * description of the new link state, detailing speed and duplex
1549 * status
1552 static void velocity_print_link_status(struct velocity_info *vptr)
1555 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1556 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
1557 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1558 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link autonegation", vptr->dev->name);
1560 if (vptr->mii_status & VELOCITY_SPEED_1000)
1561 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1562 else if (vptr->mii_status & VELOCITY_SPEED_100)
1563 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1564 else
1565 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1567 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1568 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1569 else
1570 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1571 } else {
1572 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
1573 switch (vptr->options.spd_dpx) {
1574 case SPD_DPX_100_HALF:
1575 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1576 break;
1577 case SPD_DPX_100_FULL:
1578 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1579 break;
1580 case SPD_DPX_10_HALF:
1581 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1582 break;
1583 case SPD_DPX_10_FULL:
1584 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1585 break;
1586 default:
1587 break;
1593 * velocity_error - handle error from controller
1594 * @vptr: velocity
1595 * @status: card status
1597 * Process an error report from the hardware and attempt to recover
1598 * the card itself. At the moment we cannot recover from some
1599 * theoretically impossible errors but this could be fixed using
1600 * the pci_device_failed logic to bounce the hardware
1604 static void velocity_error(struct velocity_info *vptr, int status)
1607 if (status & ISR_TXSTLI) {
1608 struct mac_regs __iomem * regs = vptr->mac_regs;
1610 printk(KERN_ERR "TD structure errror TDindex=%hx\n", readw(&regs->TDIdx[0]));
1611 BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
1612 writew(TRDCSR_RUN, &regs->TDCSRClr);
1613 netif_stop_queue(vptr->dev);
1615 /* FIXME: port over the pci_device_failed code and use it
1616 here */
1619 if (status & ISR_SRCI) {
1620 struct mac_regs __iomem * regs = vptr->mac_regs;
1621 int linked;
1623 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1624 vptr->mii_status = check_connection_type(regs);
1627 * If it is a 3119, disable frame bursting in
1628 * halfduplex mode and enable it in fullduplex
1629 * mode
1631 if (vptr->rev_id < REV_ID_VT3216_A0) {
1632 if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
1633 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
1634 else
1635 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
1638 * Only enable CD heart beat counter in 10HD mode
1640 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10)) {
1641 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
1642 } else {
1643 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
1647 * Get link status from PHYSR0
1649 linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
1651 if (linked) {
1652 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1653 } else {
1654 vptr->mii_status |= VELOCITY_LINK_FAIL;
1657 velocity_print_link_status(vptr);
1658 enable_flow_control_ability(vptr);
1661 * Re-enable auto-polling because SRCI will disable
1662 * auto-polling
1665 enable_mii_autopoll(regs);
1667 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1668 netif_stop_queue(vptr->dev);
1669 else
1670 netif_wake_queue(vptr->dev);
1673 if (status & ISR_MIBFI)
1674 velocity_update_hw_mibs(vptr);
1675 if (status & ISR_LSTEI)
1676 mac_rx_queue_wake(vptr->mac_regs);
1680 * velocity_free_tx_buf - free transmit buffer
1681 * @vptr: velocity
1682 * @tdinfo: buffer
1684 * Release an transmit buffer. If the buffer was preallocated then
1685 * recycle it, if not then unmap the buffer.
1688 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
1690 struct sk_buff *skb = tdinfo->skb;
1691 int i;
1694 * Don't unmap the pre-allocated tx_bufs
1696 if (tdinfo->skb_dma && (tdinfo->skb_dma[0] != tdinfo->buf_dma)) {
1698 for (i = 0; i < tdinfo->nskb_dma; i++) {
1699 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1700 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], td->tdesc1.len, PCI_DMA_TODEVICE);
1701 #else
1702 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], skb->len, PCI_DMA_TODEVICE);
1703 #endif
1704 tdinfo->skb_dma[i] = 0;
1707 dev_kfree_skb_irq(skb);
1708 tdinfo->skb = NULL;
1712 * velocity_open - interface activation callback
1713 * @dev: network layer device to open
1715 * Called when the network layer brings the interface up. Returns
1716 * a negative posix error code on failure, or zero on success.
1718 * All the ring allocation and set up is done on open for this
1719 * adapter to minimise memory usage when inactive
1722 static int velocity_open(struct net_device *dev)
1724 struct velocity_info *vptr = dev->priv;
1725 int ret;
1727 vptr->rx_buf_sz = (dev->mtu <= 1504 ? PKT_BUF_SZ : dev->mtu + 32);
1729 ret = velocity_init_rings(vptr);
1730 if (ret < 0)
1731 goto out;
1733 ret = velocity_init_rd_ring(vptr);
1734 if (ret < 0)
1735 goto err_free_desc_rings;
1737 ret = velocity_init_td_ring(vptr);
1738 if (ret < 0)
1739 goto err_free_rd_ring;
1741 /* Ensure chip is running */
1742 pci_set_power_state(vptr->pdev, PCI_D0);
1744 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1746 ret = request_irq(vptr->pdev->irq, &velocity_intr, SA_SHIRQ,
1747 dev->name, dev);
1748 if (ret < 0) {
1749 /* Power down the chip */
1750 pci_set_power_state(vptr->pdev, PCI_D3hot);
1751 goto err_free_td_ring;
1754 mac_enable_int(vptr->mac_regs);
1755 netif_start_queue(dev);
1756 vptr->flags |= VELOCITY_FLAGS_OPENED;
1757 out:
1758 return ret;
1760 err_free_td_ring:
1761 velocity_free_td_ring(vptr);
1762 err_free_rd_ring:
1763 velocity_free_rd_ring(vptr);
1764 err_free_desc_rings:
1765 velocity_free_rings(vptr);
1766 goto out;
1769 /**
1770 * velocity_change_mtu - MTU change callback
1771 * @dev: network device
1772 * @new_mtu: desired MTU
1774 * Handle requests from the networking layer for MTU change on
1775 * this interface. It gets called on a change by the network layer.
1776 * Return zero for success or negative posix error code.
1779 static int velocity_change_mtu(struct net_device *dev, int new_mtu)
1781 struct velocity_info *vptr = dev->priv;
1782 unsigned long flags;
1783 int oldmtu = dev->mtu;
1784 int ret = 0;
1786 if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
1787 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n",
1788 vptr->dev->name);
1789 return -EINVAL;
1792 if (new_mtu != oldmtu) {
1793 spin_lock_irqsave(&vptr->lock, flags);
1795 netif_stop_queue(dev);
1796 velocity_shutdown(vptr);
1798 velocity_free_td_ring(vptr);
1799 velocity_free_rd_ring(vptr);
1801 dev->mtu = new_mtu;
1802 if (new_mtu > 8192)
1803 vptr->rx_buf_sz = 9 * 1024;
1804 else if (new_mtu > 4096)
1805 vptr->rx_buf_sz = 8192;
1806 else
1807 vptr->rx_buf_sz = 4 * 1024;
1809 ret = velocity_init_rd_ring(vptr);
1810 if (ret < 0)
1811 goto out_unlock;
1813 ret = velocity_init_td_ring(vptr);
1814 if (ret < 0)
1815 goto out_unlock;
1817 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1819 mac_enable_int(vptr->mac_regs);
1820 netif_start_queue(dev);
1821 out_unlock:
1822 spin_unlock_irqrestore(&vptr->lock, flags);
1825 return ret;
1829 * velocity_shutdown - shut down the chip
1830 * @vptr: velocity to deactivate
1832 * Shuts down the internal operations of the velocity and
1833 * disables interrupts, autopolling, transmit and receive
1836 static void velocity_shutdown(struct velocity_info *vptr)
1838 struct mac_regs __iomem * regs = vptr->mac_regs;
1839 mac_disable_int(regs);
1840 writel(CR0_STOP, &regs->CR0Set);
1841 writew(0xFFFF, &regs->TDCSRClr);
1842 writeb(0xFF, &regs->RDCSRClr);
1843 safe_disable_mii_autopoll(regs);
1844 mac_clear_isr(regs);
1848 * velocity_close - close adapter callback
1849 * @dev: network device
1851 * Callback from the network layer when the velocity is being
1852 * deactivated by the network layer
1855 static int velocity_close(struct net_device *dev)
1857 struct velocity_info *vptr = dev->priv;
1859 netif_stop_queue(dev);
1860 velocity_shutdown(vptr);
1862 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
1863 velocity_get_ip(vptr);
1864 if (dev->irq != 0)
1865 free_irq(dev->irq, dev);
1867 /* Power down the chip */
1868 pci_set_power_state(vptr->pdev, PCI_D3hot);
1870 /* Free the resources */
1871 velocity_free_td_ring(vptr);
1872 velocity_free_rd_ring(vptr);
1873 velocity_free_rings(vptr);
1875 vptr->flags &= (~VELOCITY_FLAGS_OPENED);
1876 return 0;
1880 * velocity_xmit - transmit packet callback
1881 * @skb: buffer to transmit
1882 * @dev: network device
1884 * Called by the networ layer to request a packet is queued to
1885 * the velocity. Returns zero on success.
1888 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
1890 struct velocity_info *vptr = dev->priv;
1891 int qnum = 0;
1892 struct tx_desc *td_ptr;
1893 struct velocity_td_info *tdinfo;
1894 unsigned long flags;
1895 int index;
1897 int pktlen = skb->len;
1899 spin_lock_irqsave(&vptr->lock, flags);
1901 index = vptr->td_curr[qnum];
1902 td_ptr = &(vptr->td_rings[qnum][index]);
1903 tdinfo = &(vptr->td_infos[qnum][index]);
1905 td_ptr->tdesc1.TCPLS = TCPLS_NORMAL;
1906 td_ptr->tdesc1.TCR = TCR0_TIC;
1907 td_ptr->td_buf[0].queue = 0;
1910 * Pad short frames.
1912 if (pktlen < ETH_ZLEN) {
1913 /* Cannot occur until ZC support */
1914 if(skb_linearize(skb, GFP_ATOMIC))
1915 return 0;
1916 pktlen = ETH_ZLEN;
1917 memcpy(tdinfo->buf, skb->data, skb->len);
1918 memset(tdinfo->buf + skb->len, 0, ETH_ZLEN - skb->len);
1919 tdinfo->skb = skb;
1920 tdinfo->skb_dma[0] = tdinfo->buf_dma;
1921 td_ptr->tdesc0.pktsize = pktlen;
1922 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1923 td_ptr->td_buf[0].pa_high = 0;
1924 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1925 tdinfo->nskb_dma = 1;
1926 td_ptr->tdesc1.CMDZ = 2;
1927 } else
1928 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1929 if (skb_shinfo(skb)->nr_frags > 0) {
1930 int nfrags = skb_shinfo(skb)->nr_frags;
1931 tdinfo->skb = skb;
1932 if (nfrags > 6) {
1933 skb_linearize(skb, GFP_ATOMIC);
1934 memcpy(tdinfo->buf, skb->data, skb->len);
1935 tdinfo->skb_dma[0] = tdinfo->buf_dma;
1936 td_ptr->tdesc0.pktsize =
1937 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1938 td_ptr->td_buf[0].pa_high = 0;
1939 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1940 tdinfo->nskb_dma = 1;
1941 td_ptr->tdesc1.CMDZ = 2;
1942 } else {
1943 int i = 0;
1944 tdinfo->nskb_dma = 0;
1945 tdinfo->skb_dma[i] = pci_map_single(vptr->pdev, skb->data, skb->len - skb->data_len, PCI_DMA_TODEVICE);
1947 td_ptr->tdesc0.pktsize = pktlen;
1949 /* FIXME: support 48bit DMA later */
1950 td_ptr->td_buf[i].pa_low = cpu_to_le32(tdinfo->skb_dma);
1951 td_ptr->td_buf[i].pa_high = 0;
1952 td_ptr->td_buf[i].bufsize = skb->len->skb->data_len;
1954 for (i = 0; i < nfrags; i++) {
1955 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1956 void *addr = ((void *) page_address(frag->page + frag->page_offset));
1958 tdinfo->skb_dma[i + 1] = pci_map_single(vptr->pdev, addr, frag->size, PCI_DMA_TODEVICE);
1960 td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
1961 td_ptr->td_buf[i + 1].pa_high = 0;
1962 td_ptr->td_buf[i + 1].bufsize = frag->size;
1964 tdinfo->nskb_dma = i - 1;
1965 td_ptr->tdesc1.CMDZ = i;
1968 } else
1969 #endif
1972 * Map the linear network buffer into PCI space and
1973 * add it to the transmit ring.
1975 tdinfo->skb = skb;
1976 tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
1977 td_ptr->tdesc0.pktsize = pktlen;
1978 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1979 td_ptr->td_buf[0].pa_high = 0;
1980 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1981 tdinfo->nskb_dma = 1;
1982 td_ptr->tdesc1.CMDZ = 2;
1985 if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
1986 td_ptr->tdesc1.pqinf.VID = (vptr->options.vid & 0xfff);
1987 td_ptr->tdesc1.pqinf.priority = 0;
1988 td_ptr->tdesc1.pqinf.CFI = 0;
1989 td_ptr->tdesc1.TCR |= TCR0_VETAG;
1993 * Handle hardware checksum
1995 if ((vptr->flags & VELOCITY_FLAGS_TX_CSUM)
1996 && (skb->ip_summed == CHECKSUM_HW)) {
1997 struct iphdr *ip = skb->nh.iph;
1998 if (ip->protocol == IPPROTO_TCP)
1999 td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2000 else if (ip->protocol == IPPROTO_UDP)
2001 td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2002 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2006 int prev = index - 1;
2008 if (prev < 0)
2009 prev = vptr->options.numtx - 1;
2010 td_ptr->tdesc0.owner = OWNED_BY_NIC;
2011 vptr->td_used[qnum]++;
2012 vptr->td_curr[qnum] = (index + 1) % vptr->options.numtx;
2014 if (AVAIL_TD(vptr, qnum) < 1)
2015 netif_stop_queue(dev);
2017 td_ptr = &(vptr->td_rings[qnum][prev]);
2018 td_ptr->td_buf[0].queue = 1;
2019 mac_tx_queue_wake(vptr->mac_regs, qnum);
2021 dev->trans_start = jiffies;
2022 spin_unlock_irqrestore(&vptr->lock, flags);
2023 return 0;
2027 * velocity_intr - interrupt callback
2028 * @irq: interrupt number
2029 * @dev_instance: interrupting device
2030 * @pt_regs: CPU register state at interrupt
2032 * Called whenever an interrupt is generated by the velocity
2033 * adapter IRQ line. We may not be the source of the interrupt
2034 * and need to identify initially if we are, and if not exit as
2035 * efficiently as possible.
2038 static int velocity_intr(int irq, void *dev_instance, struct pt_regs *regs)
2040 struct net_device *dev = dev_instance;
2041 struct velocity_info *vptr = dev->priv;
2042 u32 isr_status;
2043 int max_count = 0;
2046 spin_lock(&vptr->lock);
2047 isr_status = mac_read_isr(vptr->mac_regs);
2049 /* Not us ? */
2050 if (isr_status == 0) {
2051 spin_unlock(&vptr->lock);
2052 return IRQ_NONE;
2055 mac_disable_int(vptr->mac_regs);
2058 * Keep processing the ISR until we have completed
2059 * processing and the isr_status becomes zero
2062 while (isr_status != 0) {
2063 mac_write_isr(vptr->mac_regs, isr_status);
2064 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2065 velocity_error(vptr, isr_status);
2066 if (isr_status & (ISR_PRXI | ISR_PPRXI))
2067 max_count += velocity_rx_srv(vptr, isr_status);
2068 if (isr_status & (ISR_PTXI | ISR_PPTXI))
2069 max_count += velocity_tx_srv(vptr, isr_status);
2070 isr_status = mac_read_isr(vptr->mac_regs);
2071 if (max_count > vptr->options.int_works)
2073 printk(KERN_WARNING "%s: excessive work at interrupt.\n",
2074 dev->name);
2075 max_count = 0;
2078 spin_unlock(&vptr->lock);
2079 mac_enable_int(vptr->mac_regs);
2080 return IRQ_HANDLED;
2086 * velocity_set_multi - filter list change callback
2087 * @dev: network device
2089 * Called by the network layer when the filter lists need to change
2090 * for a velocity adapter. Reload the CAMs with the new address
2091 * filter ruleset.
2094 static void velocity_set_multi(struct net_device *dev)
2096 struct velocity_info *vptr = dev->priv;
2097 struct mac_regs __iomem * regs = vptr->mac_regs;
2098 u8 rx_mode;
2099 int i;
2100 struct dev_mc_list *mclist;
2102 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2103 /* Unconditionally log net taps. */
2104 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
2105 writel(0xffffffff, &regs->MARCAM[0]);
2106 writel(0xffffffff, &regs->MARCAM[4]);
2107 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
2108 } else if ((dev->mc_count > vptr->multicast_limit)
2109 || (dev->flags & IFF_ALLMULTI)) {
2110 writel(0xffffffff, &regs->MARCAM[0]);
2111 writel(0xffffffff, &regs->MARCAM[4]);
2112 rx_mode = (RCR_AM | RCR_AB);
2113 } else {
2114 int offset = MCAM_SIZE - vptr->multicast_limit;
2115 mac_get_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
2117 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) {
2118 mac_set_cam(regs, i + offset, mclist->dmi_addr, VELOCITY_MULTICAST_CAM);
2119 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
2122 mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
2123 rx_mode = (RCR_AM | RCR_AB);
2125 if (dev->mtu > 1500)
2126 rx_mode |= RCR_AL;
2128 BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
2133 * velocity_get_status - statistics callback
2134 * @dev: network device
2136 * Callback from the network layer to allow driver statistics
2137 * to be resynchronized with hardware collected state. In the
2138 * case of the velocity we need to pull the MIB counters from
2139 * the hardware into the counters before letting the network
2140 * layer display them.
2143 static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2145 struct velocity_info *vptr = dev->priv;
2147 /* If the hardware is down, don't touch MII */
2148 if(!netif_running(dev))
2149 return &vptr->stats;
2151 spin_lock_irq(&vptr->lock);
2152 velocity_update_hw_mibs(vptr);
2153 spin_unlock_irq(&vptr->lock);
2155 vptr->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2156 vptr->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2157 vptr->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2159 // unsigned long rx_dropped; /* no space in linux buffers */
2160 vptr->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2161 /* detailed rx_errors: */
2162 // unsigned long rx_length_errors;
2163 // unsigned long rx_over_errors; /* receiver ring buff overflow */
2164 vptr->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2165 // unsigned long rx_frame_errors; /* recv'd frame alignment error */
2166 // unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2167 // unsigned long rx_missed_errors; /* receiver missed packet */
2169 /* detailed tx_errors */
2170 // unsigned long tx_fifo_errors;
2172 return &vptr->stats;
2177 * velocity_ioctl - ioctl entry point
2178 * @dev: network device
2179 * @rq: interface request ioctl
2180 * @cmd: command code
2182 * Called when the user issues an ioctl request to the network
2183 * device in question. The velocity interface supports MII.
2186 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2188 struct velocity_info *vptr = dev->priv;
2189 int ret;
2191 /* If we are asked for information and the device is power
2192 saving then we need to bring the device back up to talk to it */
2194 if (!netif_running(dev))
2195 pci_set_power_state(vptr->pdev, PCI_D0);
2197 switch (cmd) {
2198 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2199 case SIOCGMIIREG: /* Read MII PHY register. */
2200 case SIOCSMIIREG: /* Write to MII PHY register. */
2201 ret = velocity_mii_ioctl(dev, rq, cmd);
2202 break;
2204 default:
2205 ret = -EOPNOTSUPP;
2207 if (!netif_running(dev))
2208 pci_set_power_state(vptr->pdev, PCI_D3hot);
2211 return ret;
2215 * Definition for our device driver. The PCI layer interface
2216 * uses this to handle all our card discover and plugging
2219 static struct pci_driver velocity_driver = {
2220 .name = VELOCITY_NAME,
2221 .id_table = velocity_id_table,
2222 .probe = velocity_found1,
2223 .remove = __devexit_p(velocity_remove1),
2224 #ifdef CONFIG_PM
2225 .suspend = velocity_suspend,
2226 .resume = velocity_resume,
2227 #endif
2231 * velocity_init_module - load time function
2233 * Called when the velocity module is loaded. The PCI driver
2234 * is registered with the PCI layer, and in turn will call
2235 * the probe functions for each velocity adapter installed
2236 * in the system.
2239 static int __init velocity_init_module(void)
2241 int ret;
2243 velocity_register_notifier();
2244 ret = pci_module_init(&velocity_driver);
2245 if (ret < 0)
2246 velocity_unregister_notifier();
2247 return ret;
2251 * velocity_cleanup - module unload
2253 * When the velocity hardware is unloaded this function is called.
2254 * It will clean up the notifiers and the unregister the PCI
2255 * driver interface for this hardware. This in turn cleans up
2256 * all discovered interfaces before returning from the function
2259 static void __exit velocity_cleanup_module(void)
2261 velocity_unregister_notifier();
2262 pci_unregister_driver(&velocity_driver);
2265 module_init(velocity_init_module);
2266 module_exit(velocity_cleanup_module);
2270 * MII access , media link mode setting functions
2275 * mii_init - set up MII
2276 * @vptr: velocity adapter
2277 * @mii_status: links tatus
2279 * Set up the PHY for the current link state.
2282 static void mii_init(struct velocity_info *vptr, u32 mii_status)
2284 u16 BMCR;
2286 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
2287 case PHYID_CICADA_CS8201:
2289 * Reset to hardware default
2291 MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2293 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2294 * off it in NWay-forced half mode for NWay-forced v.s.
2295 * legacy-forced issue.
2297 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2298 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2299 else
2300 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2302 * Turn on Link/Activity LED enable bit for CIS8201
2304 MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
2305 break;
2306 case PHYID_VT3216_32BIT:
2307 case PHYID_VT3216_64BIT:
2309 * Reset to hardware default
2311 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2313 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2314 * off it in NWay-forced half mode for NWay-forced v.s.
2315 * legacy-forced issue
2317 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2318 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2319 else
2320 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2321 break;
2323 case PHYID_MARVELL_1000:
2324 case PHYID_MARVELL_1000S:
2326 * Assert CRS on Transmit
2328 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
2330 * Reset to hardware default
2332 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2333 break;
2334 default:
2337 velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
2338 if (BMCR & BMCR_ISO) {
2339 BMCR &= ~BMCR_ISO;
2340 velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
2345 * safe_disable_mii_autopoll - autopoll off
2346 * @regs: velocity registers
2348 * Turn off the autopoll and wait for it to disable on the chip
2351 static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs)
2353 u16 ww;
2355 /* turn off MAUTO */
2356 writeb(0, &regs->MIICR);
2357 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2358 udelay(1);
2359 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2360 break;
2365 * enable_mii_autopoll - turn on autopolling
2366 * @regs: velocity registers
2368 * Enable the MII link status autopoll feature on the Velocity
2369 * hardware. Wait for it to enable.
2372 static void enable_mii_autopoll(struct mac_regs __iomem * regs)
2374 int ii;
2376 writeb(0, &(regs->MIICR));
2377 writeb(MIIADR_SWMPL, &regs->MIIADR);
2379 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2380 udelay(1);
2381 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2382 break;
2385 writeb(MIICR_MAUTO, &regs->MIICR);
2387 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2388 udelay(1);
2389 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2390 break;
2396 * velocity_mii_read - read MII data
2397 * @regs: velocity registers
2398 * @index: MII register index
2399 * @data: buffer for received data
2401 * Perform a single read of an MII 16bit register. Returns zero
2402 * on success or -ETIMEDOUT if the PHY did not respond.
2405 static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
2407 u16 ww;
2410 * Disable MIICR_MAUTO, so that mii addr can be set normally
2412 safe_disable_mii_autopoll(regs);
2414 writeb(index, &regs->MIIADR);
2416 BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
2418 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2419 if (!(readb(&regs->MIICR) & MIICR_RCMD))
2420 break;
2423 *data = readw(&regs->MIIDATA);
2425 enable_mii_autopoll(regs);
2426 if (ww == W_MAX_TIMEOUT)
2427 return -ETIMEDOUT;
2428 return 0;
2432 * velocity_mii_write - write MII data
2433 * @regs: velocity registers
2434 * @index: MII register index
2435 * @data: 16bit data for the MII register
2437 * Perform a single write to an MII 16bit register. Returns zero
2438 * on success or -ETIMEDOUT if the PHY did not respond.
2441 static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
2443 u16 ww;
2446 * Disable MIICR_MAUTO, so that mii addr can be set normally
2448 safe_disable_mii_autopoll(regs);
2450 /* MII reg offset */
2451 writeb(mii_addr, &regs->MIIADR);
2452 /* set MII data */
2453 writew(data, &regs->MIIDATA);
2455 /* turn on MIICR_WCMD */
2456 BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
2458 /* W_MAX_TIMEOUT is the timeout period */
2459 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2460 udelay(5);
2461 if (!(readb(&regs->MIICR) & MIICR_WCMD))
2462 break;
2464 enable_mii_autopoll(regs);
2466 if (ww == W_MAX_TIMEOUT)
2467 return -ETIMEDOUT;
2468 return 0;
2472 * velocity_get_opt_media_mode - get media selection
2473 * @vptr: velocity adapter
2475 * Get the media mode stored in EEPROM or module options and load
2476 * mii_status accordingly. The requested link state information
2477 * is also returned.
2480 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
2482 u32 status = 0;
2484 switch (vptr->options.spd_dpx) {
2485 case SPD_DPX_AUTO:
2486 status = VELOCITY_AUTONEG_ENABLE;
2487 break;
2488 case SPD_DPX_100_FULL:
2489 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
2490 break;
2491 case SPD_DPX_10_FULL:
2492 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
2493 break;
2494 case SPD_DPX_100_HALF:
2495 status = VELOCITY_SPEED_100;
2496 break;
2497 case SPD_DPX_10_HALF:
2498 status = VELOCITY_SPEED_10;
2499 break;
2501 vptr->mii_status = status;
2502 return status;
2506 * mii_set_auto_on - autonegotiate on
2507 * @vptr: velocity
2509 * Enable autonegotation on this interface
2512 static void mii_set_auto_on(struct velocity_info *vptr)
2514 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
2515 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
2516 else
2517 MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2522 static void mii_set_auto_off(struct velocity_info * vptr)
2524 MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2529 * set_mii_flow_control - flow control setup
2530 * @vptr: velocity interface
2532 * Set up the flow control on this interface according to
2533 * the supplied user/eeprom options.
2536 static void set_mii_flow_control(struct velocity_info *vptr)
2538 /*Enable or Disable PAUSE in ANAR */
2539 switch (vptr->options.flow_cntl) {
2540 case FLOW_CNTL_TX:
2541 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2542 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2543 break;
2545 case FLOW_CNTL_RX:
2546 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2547 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2548 break;
2550 case FLOW_CNTL_TX_RX:
2551 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2552 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2553 break;
2555 case FLOW_CNTL_DISABLE:
2556 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2557 MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2558 break;
2559 default:
2560 break;
2565 * velocity_set_media_mode - set media mode
2566 * @mii_status: old MII link state
2568 * Check the media link state and configure the flow control
2569 * PHY and also velocity hardware setup accordingly. In particular
2570 * we need to set up CD polling and frame bursting.
2573 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
2575 u32 curr_status;
2576 struct mac_regs __iomem * regs = vptr->mac_regs;
2578 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
2579 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
2581 /* Set mii link status */
2582 set_mii_flow_control(vptr);
2585 Check if new status is consisent with current status
2586 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
2587 || (mii_status==curr_status)) {
2588 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
2589 vptr->mii_status=check_connection_type(vptr->mac_regs);
2590 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
2591 return 0;
2595 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201) {
2596 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
2600 * If connection type is AUTO
2602 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
2603 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
2604 /* clear force MAC mode bit */
2605 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
2606 /* set duplex mode of MAC according to duplex mode of MII */
2607 MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
2608 MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2609 MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
2611 /* enable AUTO-NEGO mode */
2612 mii_set_auto_on(vptr);
2613 } else {
2614 u16 ANAR;
2615 u8 CHIPGCR;
2618 * 1. if it's 3119, disable frame bursting in halfduplex mode
2619 * and enable it in fullduplex mode
2620 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
2621 * 3. only enable CD heart beat counter in 10HD mode
2624 /* set force MAC mode bit */
2625 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
2627 CHIPGCR = readb(&regs->CHIPGCR);
2628 CHIPGCR &= ~CHIPGCR_FCGMII;
2630 if (mii_status & VELOCITY_DUPLEX_FULL) {
2631 CHIPGCR |= CHIPGCR_FCFDX;
2632 writeb(CHIPGCR, &regs->CHIPGCR);
2633 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
2634 if (vptr->rev_id < REV_ID_VT3216_A0)
2635 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
2636 } else {
2637 CHIPGCR &= ~CHIPGCR_FCFDX;
2638 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
2639 writeb(CHIPGCR, &regs->CHIPGCR);
2640 if (vptr->rev_id < REV_ID_VT3216_A0)
2641 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
2644 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2646 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10)) {
2647 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
2648 } else {
2649 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
2651 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
2652 velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
2653 ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
2654 if (mii_status & VELOCITY_SPEED_100) {
2655 if (mii_status & VELOCITY_DUPLEX_FULL)
2656 ANAR |= ANAR_TXFD;
2657 else
2658 ANAR |= ANAR_TX;
2659 } else {
2660 if (mii_status & VELOCITY_DUPLEX_FULL)
2661 ANAR |= ANAR_10FD;
2662 else
2663 ANAR |= ANAR_10;
2665 velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
2666 /* enable AUTO-NEGO mode */
2667 mii_set_auto_on(vptr);
2668 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
2670 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
2671 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
2672 return VELOCITY_LINK_CHANGE;
2676 * mii_check_media_mode - check media state
2677 * @regs: velocity registers
2679 * Check the current MII status and determine the link status
2680 * accordingly
2683 static u32 mii_check_media_mode(struct mac_regs __iomem * regs)
2685 u32 status = 0;
2686 u16 ANAR;
2688 if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
2689 status |= VELOCITY_LINK_FAIL;
2691 if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
2692 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
2693 else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
2694 status |= (VELOCITY_SPEED_1000);
2695 else {
2696 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2697 if (ANAR & ANAR_TXFD)
2698 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
2699 else if (ANAR & ANAR_TX)
2700 status |= VELOCITY_SPEED_100;
2701 else if (ANAR & ANAR_10FD)
2702 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
2703 else
2704 status |= (VELOCITY_SPEED_10);
2707 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2708 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2709 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2710 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2711 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2712 status |= VELOCITY_AUTONEG_ENABLE;
2716 return status;
2719 static u32 check_connection_type(struct mac_regs __iomem * regs)
2721 u32 status = 0;
2722 u8 PHYSR0;
2723 u16 ANAR;
2724 PHYSR0 = readb(&regs->PHYSR0);
2727 if (!(PHYSR0 & PHYSR0_LINKGD))
2728 status|=VELOCITY_LINK_FAIL;
2731 if (PHYSR0 & PHYSR0_FDPX)
2732 status |= VELOCITY_DUPLEX_FULL;
2734 if (PHYSR0 & PHYSR0_SPDG)
2735 status |= VELOCITY_SPEED_1000;
2736 if (PHYSR0 & PHYSR0_SPD10)
2737 status |= VELOCITY_SPEED_10;
2738 else
2739 status |= VELOCITY_SPEED_100;
2741 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2742 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2743 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2744 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2745 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2746 status |= VELOCITY_AUTONEG_ENABLE;
2750 return status;
2754 * enable_flow_control_ability - flow control
2755 * @vptr: veloity to configure
2757 * Set up flow control according to the flow control options
2758 * determined by the eeprom/configuration.
2761 static void enable_flow_control_ability(struct velocity_info *vptr)
2764 struct mac_regs __iomem * regs = vptr->mac_regs;
2766 switch (vptr->options.flow_cntl) {
2768 case FLOW_CNTL_DEFAULT:
2769 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
2770 writel(CR0_FDXRFCEN, &regs->CR0Set);
2771 else
2772 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2774 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
2775 writel(CR0_FDXTFCEN, &regs->CR0Set);
2776 else
2777 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2778 break;
2780 case FLOW_CNTL_TX:
2781 writel(CR0_FDXTFCEN, &regs->CR0Set);
2782 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2783 break;
2785 case FLOW_CNTL_RX:
2786 writel(CR0_FDXRFCEN, &regs->CR0Set);
2787 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2788 break;
2790 case FLOW_CNTL_TX_RX:
2791 writel(CR0_FDXTFCEN, &regs->CR0Set);
2792 writel(CR0_FDXRFCEN, &regs->CR0Set);
2793 break;
2795 case FLOW_CNTL_DISABLE:
2796 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2797 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2798 break;
2800 default:
2801 break;
2808 * velocity_ethtool_up - pre hook for ethtool
2809 * @dev: network device
2811 * Called before an ethtool operation. We need to make sure the
2812 * chip is out of D3 state before we poke at it.
2815 static int velocity_ethtool_up(struct net_device *dev)
2817 struct velocity_info *vptr = dev->priv;
2818 if (!netif_running(dev))
2819 pci_set_power_state(vptr->pdev, PCI_D0);
2820 return 0;
2824 * velocity_ethtool_down - post hook for ethtool
2825 * @dev: network device
2827 * Called after an ethtool operation. Restore the chip back to D3
2828 * state if it isn't running.
2831 static void velocity_ethtool_down(struct net_device *dev)
2833 struct velocity_info *vptr = dev->priv;
2834 if (!netif_running(dev))
2835 pci_set_power_state(vptr->pdev, PCI_D3hot);
2838 static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2840 struct velocity_info *vptr = dev->priv;
2841 struct mac_regs __iomem * regs = vptr->mac_regs;
2842 u32 status;
2843 status = check_connection_type(vptr->mac_regs);
2845 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg | SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
2846 if (status & VELOCITY_SPEED_100)
2847 cmd->speed = SPEED_100;
2848 else
2849 cmd->speed = SPEED_10;
2850 cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2851 cmd->port = PORT_TP;
2852 cmd->transceiver = XCVR_INTERNAL;
2853 cmd->phy_address = readb(&regs->MIIADR) & 0x1F;
2855 if (status & VELOCITY_DUPLEX_FULL)
2856 cmd->duplex = DUPLEX_FULL;
2857 else
2858 cmd->duplex = DUPLEX_HALF;
2860 return 0;
2863 static int velocity_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2865 struct velocity_info *vptr = dev->priv;
2866 u32 curr_status;
2867 u32 new_status = 0;
2868 int ret = 0;
2870 curr_status = check_connection_type(vptr->mac_regs);
2871 curr_status &= (~VELOCITY_LINK_FAIL);
2873 new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
2874 new_status |= ((cmd->speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
2875 new_status |= ((cmd->speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
2876 new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
2878 if ((new_status & VELOCITY_AUTONEG_ENABLE) && (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE)))
2879 ret = -EINVAL;
2880 else
2881 velocity_set_media_mode(vptr, new_status);
2883 return ret;
2886 static u32 velocity_get_link(struct net_device *dev)
2888 struct velocity_info *vptr = dev->priv;
2889 struct mac_regs __iomem * regs = vptr->mac_regs;
2890 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 0 : 1;
2893 static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2895 struct velocity_info *vptr = dev->priv;
2896 strcpy(info->driver, VELOCITY_NAME);
2897 strcpy(info->version, VELOCITY_VERSION);
2898 strcpy(info->bus_info, pci_name(vptr->pdev));
2901 static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2903 struct velocity_info *vptr = dev->priv;
2904 wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
2905 wol->wolopts |= WAKE_MAGIC;
2907 if (vptr->wol_opts & VELOCITY_WOL_PHY)
2908 wol.wolopts|=WAKE_PHY;
2910 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
2911 wol->wolopts |= WAKE_UCAST;
2912 if (vptr->wol_opts & VELOCITY_WOL_ARP)
2913 wol->wolopts |= WAKE_ARP;
2914 memcpy(&wol->sopass, vptr->wol_passwd, 6);
2917 static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2919 struct velocity_info *vptr = dev->priv;
2921 if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
2922 return -EFAULT;
2923 vptr->wol_opts = VELOCITY_WOL_MAGIC;
2926 if (wol.wolopts & WAKE_PHY) {
2927 vptr->wol_opts|=VELOCITY_WOL_PHY;
2928 vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
2932 if (wol->wolopts & WAKE_MAGIC) {
2933 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
2934 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2936 if (wol->wolopts & WAKE_UCAST) {
2937 vptr->wol_opts |= VELOCITY_WOL_UCAST;
2938 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2940 if (wol->wolopts & WAKE_ARP) {
2941 vptr->wol_opts |= VELOCITY_WOL_ARP;
2942 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2944 memcpy(vptr->wol_passwd, wol->sopass, 6);
2945 return 0;
2948 static u32 velocity_get_msglevel(struct net_device *dev)
2950 return msglevel;
2953 static void velocity_set_msglevel(struct net_device *dev, u32 value)
2955 msglevel = value;
2958 static struct ethtool_ops velocity_ethtool_ops = {
2959 .get_settings = velocity_get_settings,
2960 .set_settings = velocity_set_settings,
2961 .get_drvinfo = velocity_get_drvinfo,
2962 .get_wol = velocity_ethtool_get_wol,
2963 .set_wol = velocity_ethtool_set_wol,
2964 .get_msglevel = velocity_get_msglevel,
2965 .set_msglevel = velocity_set_msglevel,
2966 .get_link = velocity_get_link,
2967 .begin = velocity_ethtool_up,
2968 .complete = velocity_ethtool_down
2972 * velocity_mii_ioctl - MII ioctl handler
2973 * @dev: network device
2974 * @ifr: the ifreq block for the ioctl
2975 * @cmd: the command
2977 * Process MII requests made via ioctl from the network layer. These
2978 * are used by tools like kudzu to interrogate the link state of the
2979 * hardware
2982 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2984 struct velocity_info *vptr = dev->priv;
2985 struct mac_regs __iomem * regs = vptr->mac_regs;
2986 unsigned long flags;
2987 struct mii_ioctl_data *miidata = if_mii(ifr);
2988 int err;
2990 switch (cmd) {
2991 case SIOCGMIIPHY:
2992 miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
2993 break;
2994 case SIOCGMIIREG:
2995 if (!capable(CAP_NET_ADMIN))
2996 return -EPERM;
2997 if(velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
2998 return -ETIMEDOUT;
2999 break;
3000 case SIOCSMIIREG:
3001 if (!capable(CAP_NET_ADMIN))
3002 return -EPERM;
3003 spin_lock_irqsave(&vptr->lock, flags);
3004 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
3005 spin_unlock_irqrestore(&vptr->lock, flags);
3006 check_connection_type(vptr->mac_regs);
3007 if(err)
3008 return err;
3009 break;
3010 default:
3011 return -EOPNOTSUPP;
3013 return 0;
3016 #ifdef CONFIG_PM
3019 * velocity_save_context - save registers
3020 * @vptr: velocity
3021 * @context: buffer for stored context
3023 * Retrieve the current configuration from the velocity hardware
3024 * and stash it in the context structure, for use by the context
3025 * restore functions. This allows us to save things we need across
3026 * power down states
3029 static void velocity_save_context(struct velocity_info *vptr, struct velocity_context * context)
3031 struct mac_regs __iomem * regs = vptr->mac_regs;
3032 u16 i;
3033 u8 __iomem *ptr = (u8 __iomem *)regs;
3035 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3036 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3038 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3039 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3041 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3042 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3047 * velocity_restore_context - restore registers
3048 * @vptr: velocity
3049 * @context: buffer for stored context
3051 * Reload the register configuration from the velocity context
3052 * created by velocity_save_context.
3055 static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3057 struct mac_regs __iomem * regs = vptr->mac_regs;
3058 int i;
3059 u8 __iomem *ptr = (u8 __iomem *)regs;
3061 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4) {
3062 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3065 /* Just skip cr0 */
3066 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3067 /* Clear */
3068 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3069 /* Set */
3070 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3073 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4) {
3074 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3077 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4) {
3078 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3081 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++) {
3082 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3088 * wol_calc_crc - WOL CRC
3089 * @pattern: data pattern
3090 * @mask_pattern: mask
3092 * Compute the wake on lan crc hashes for the packet header
3093 * we are interested in.
3096 static u16 wol_calc_crc(int size, u8 * pattern, u8 *mask_pattern)
3098 u16 crc = 0xFFFF;
3099 u8 mask;
3100 int i, j;
3102 for (i = 0; i < size; i++) {
3103 mask = mask_pattern[i];
3105 /* Skip this loop if the mask equals to zero */
3106 if (mask == 0x00)
3107 continue;
3109 for (j = 0; j < 8; j++) {
3110 if ((mask & 0x01) == 0) {
3111 mask >>= 1;
3112 continue;
3114 mask >>= 1;
3115 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
3118 /* Finally, invert the result once to get the correct data */
3119 crc = ~crc;
3120 return bitreverse(crc) >> 16;
3124 * velocity_set_wol - set up for wake on lan
3125 * @vptr: velocity to set WOL status on
3127 * Set a card up for wake on lan either by unicast or by
3128 * ARP packet.
3130 * FIXME: check static buffer is safe here
3133 static int velocity_set_wol(struct velocity_info *vptr)
3135 struct mac_regs __iomem * regs = vptr->mac_regs;
3136 static u8 buf[256];
3137 int i;
3139 static u32 mask_pattern[2][4] = {
3140 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3141 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3144 writew(0xFFFF, &regs->WOLCRClr);
3145 writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
3146 writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
3149 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3150 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
3153 if (vptr->wol_opts & VELOCITY_WOL_UCAST) {
3154 writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
3157 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3158 struct arp_packet *arp = (struct arp_packet *) buf;
3159 u16 crc;
3160 memset(buf, 0, sizeof(struct arp_packet) + 7);
3162 for (i = 0; i < 4; i++)
3163 writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
3165 arp->type = htons(ETH_P_ARP);
3166 arp->ar_op = htons(1);
3168 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3170 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3171 (u8 *) & mask_pattern[0][0]);
3173 writew(crc, &regs->PatternCRC[0]);
3174 writew(WOLCR_ARP_EN, &regs->WOLCRSet);
3177 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
3178 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
3180 writew(0x0FFF, &regs->WOLSRClr);
3182 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3183 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3184 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
3186 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
3189 if (vptr->mii_status & VELOCITY_SPEED_1000)
3190 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
3192 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
3195 u8 GCR;
3196 GCR = readb(&regs->CHIPGCR);
3197 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3198 writeb(GCR, &regs->CHIPGCR);
3201 BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
3202 /* Turn on SWPTAG just before entering power mode */
3203 BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
3204 /* Go to bed ..... */
3205 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
3207 return 0;
3210 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
3212 struct net_device *dev = pci_get_drvdata(pdev);
3213 struct velocity_info *vptr = netdev_priv(dev);
3214 unsigned long flags;
3216 if(!netif_running(vptr->dev))
3217 return 0;
3219 netif_device_detach(vptr->dev);
3221 spin_lock_irqsave(&vptr->lock, flags);
3222 pci_save_state(pdev);
3223 #ifdef ETHTOOL_GWOL
3224 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3225 velocity_get_ip(vptr);
3226 velocity_save_context(vptr, &vptr->context);
3227 velocity_shutdown(vptr);
3228 velocity_set_wol(vptr);
3229 pci_enable_wake(pdev, 3, 1);
3230 pci_set_power_state(pdev, PCI_D3hot);
3231 } else {
3232 velocity_save_context(vptr, &vptr->context);
3233 velocity_shutdown(vptr);
3234 pci_disable_device(pdev);
3235 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3237 #else
3238 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3239 #endif
3240 spin_unlock_irqrestore(&vptr->lock, flags);
3241 return 0;
3244 static int velocity_resume(struct pci_dev *pdev)
3246 struct net_device *dev = pci_get_drvdata(pdev);
3247 struct velocity_info *vptr = netdev_priv(dev);
3248 unsigned long flags;
3249 int i;
3251 if(!netif_running(vptr->dev))
3252 return 0;
3254 pci_set_power_state(pdev, PCI_D0);
3255 pci_enable_wake(pdev, 0, 0);
3256 pci_restore_state(pdev);
3258 mac_wol_reset(vptr->mac_regs);
3260 spin_lock_irqsave(&vptr->lock, flags);
3261 velocity_restore_context(vptr, &vptr->context);
3262 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3263 mac_disable_int(vptr->mac_regs);
3265 velocity_tx_srv(vptr, 0);
3267 for (i = 0; i < vptr->num_txq; i++) {
3268 if (vptr->td_used[i]) {
3269 mac_tx_queue_wake(vptr->mac_regs, i);
3273 mac_enable_int(vptr->mac_regs);
3274 spin_unlock_irqrestore(&vptr->lock, flags);
3275 netif_device_attach(vptr->dev);
3277 return 0;
3280 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3282 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3284 if (ifa) {
3285 struct net_device *dev = ifa->ifa_dev->dev;
3286 struct velocity_info *vptr;
3287 unsigned long flags;
3289 spin_lock_irqsave(&velocity_dev_list_lock, flags);
3290 list_for_each_entry(vptr, &velocity_dev_list, list) {
3291 if (vptr->dev == dev) {
3292 velocity_get_ip(vptr);
3293 break;
3296 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
3298 return NOTIFY_DONE;
3300 #endif