aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/via-velocity.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/via-velocity.c')
-rw-r--r--drivers/net/via-velocity.c3431
1 files changed, 1630 insertions, 1801 deletions
diff --git a/drivers/net/via-velocity.c b/drivers/net/via-velocity.c
index 3ba35956327a..47be41a39d35 100644
--- a/drivers/net/via-velocity.c
+++ b/drivers/net/via-velocity.c
@@ -61,9 +61,9 @@
61#include <linux/interrupt.h> 61#include <linux/interrupt.h>
62#include <linux/string.h> 62#include <linux/string.h>
63#include <linux/wait.h> 63#include <linux/wait.h>
64#include <asm/io.h> 64#include <linux/io.h>
65#include <linux/if.h> 65#include <linux/if.h>
66#include <asm/uaccess.h> 66#include <linux/uaccess.h>
67#include <linux/proc_fs.h> 67#include <linux/proc_fs.h>
68#include <linux/inetdevice.h> 68#include <linux/inetdevice.h>
69#include <linux/reboot.h> 69#include <linux/reboot.h>
@@ -81,7 +81,7 @@
81#include "via-velocity.h" 81#include "via-velocity.h"
82 82
83 83
84static int velocity_nics = 0; 84static int velocity_nics;
85static int msglevel = MSG_LEVEL_INFO; 85static int msglevel = MSG_LEVEL_INFO;
86 86
87/** 87/**
@@ -92,8 +92,7 @@ static int msglevel = MSG_LEVEL_INFO;
92 * Fetch the mask bits of the selected CAM and store them into the 92 * Fetch the mask bits of the selected CAM and store them into the
93 * provided mask buffer. 93 * provided mask buffer.
94 */ 94 */
95 95static void mac_get_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
96static void mac_get_cam_mask(struct mac_regs __iomem * regs, u8 * mask)
97{ 96{
98 int i; 97 int i;
99 98
@@ -111,7 +110,6 @@ static void mac_get_cam_mask(struct mac_regs __iomem * regs, u8 * mask)
111 110
112 /* Select mar */ 111 /* Select mar */
113 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR); 112 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
114
115} 113}
116 114
117 115
@@ -122,8 +120,7 @@ static void mac_get_cam_mask(struct mac_regs __iomem * regs, u8 * mask)
122 * 120 *
123 * Store a new mask into a CAM 121 * Store a new mask into a CAM
124 */ 122 */
125 123static void mac_set_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
126static void mac_set_cam_mask(struct mac_regs __iomem * regs, u8 * mask)
127{ 124{
128 int i; 125 int i;
129 /* Select CAM mask */ 126 /* Select CAM mask */
@@ -131,9 +128,9 @@ static void mac_set_cam_mask(struct mac_regs __iomem * regs, u8 * mask)
131 128
132 writeb(CAMADDR_CAMEN, &regs->CAMADDR); 129 writeb(CAMADDR_CAMEN, &regs->CAMADDR);
133 130
134 for (i = 0; i < 8; i++) { 131 for (i = 0; i < 8; i++)
135 writeb(*mask++, &(regs->MARCAM[i])); 132 writeb(*mask++, &(regs->MARCAM[i]));
136 } 133
137 /* disable CAMEN */ 134 /* disable CAMEN */
138 writeb(0, &regs->CAMADDR); 135 writeb(0, &regs->CAMADDR);
139 136
@@ -141,7 +138,7 @@ static void mac_set_cam_mask(struct mac_regs __iomem * regs, u8 * mask)
141 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR); 138 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
142} 139}
143 140
144static void mac_set_vlan_cam_mask(struct mac_regs __iomem * regs, u8 * mask) 141static void mac_set_vlan_cam_mask(struct mac_regs __iomem *regs, u8 *mask)
145{ 142{
146 int i; 143 int i;
147 /* Select CAM mask */ 144 /* Select CAM mask */
@@ -149,9 +146,9 @@ static void mac_set_vlan_cam_mask(struct mac_regs __iomem * regs, u8 * mask)
149 146
150 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL, &regs->CAMADDR); 147 writeb(CAMADDR_CAMEN | CAMADDR_VCAMSL, &regs->CAMADDR);
151 148
152 for (i = 0; i < 8; i++) { 149 for (i = 0; i < 8; i++)
153 writeb(*mask++, &(regs->MARCAM[i])); 150 writeb(*mask++, &(regs->MARCAM[i]));
154 } 151
155 /* disable CAMEN */ 152 /* disable CAMEN */
156 writeb(0, &regs->CAMADDR); 153 writeb(0, &regs->CAMADDR);
157 154
@@ -167,8 +164,7 @@ static void mac_set_vlan_cam_mask(struct mac_regs __iomem * regs, u8 * mask)
167 * 164 *
168 * Load an address or vlan tag into a CAM 165 * Load an address or vlan tag into a CAM
169 */ 166 */
170 167static void mac_set_cam(struct mac_regs __iomem *regs, int idx, const u8 *addr)
171static void mac_set_cam(struct mac_regs __iomem * regs, int idx, const u8 *addr)
172{ 168{
173 int i; 169 int i;
174 170
@@ -179,9 +175,9 @@ static void mac_set_cam(struct mac_regs __iomem * regs, int idx, const u8 *addr)
179 175
180 writeb(CAMADDR_CAMEN | idx, &regs->CAMADDR); 176 writeb(CAMADDR_CAMEN | idx, &regs->CAMADDR);
181 177
182 for (i = 0; i < 6; i++) { 178 for (i = 0; i < 6; i++)
183 writeb(*addr++, &(regs->MARCAM[i])); 179 writeb(*addr++, &(regs->MARCAM[i]));
184 } 180
185 BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR); 181 BYTE_REG_BITS_ON(CAMCR_CAMWR, &regs->CAMCR);
186 182
187 udelay(10); 183 udelay(10);
@@ -192,7 +188,7 @@ static void mac_set_cam(struct mac_regs __iomem * regs, int idx, const u8 *addr)
192 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR); 188 BYTE_REG_BITS_SET(CAMCR_PS_MAR, CAMCR_PS1 | CAMCR_PS0, &regs->CAMCR);
193} 189}
194 190
195static void mac_set_vlan_cam(struct mac_regs __iomem * regs, int idx, 191static void mac_set_vlan_cam(struct mac_regs __iomem *regs, int idx,
196 const u8 *addr) 192 const u8 *addr)
197{ 193{
198 194
@@ -223,8 +219,7 @@ static void mac_set_vlan_cam(struct mac_regs __iomem * regs, int idx,
223 * reset the Wake on lan features. This function doesn't restore 219 * reset the Wake on lan features. This function doesn't restore
224 * the rest of the logic from the result of sleep/wakeup 220 * the rest of the logic from the result of sleep/wakeup
225 */ 221 */
226 222static void mac_wol_reset(struct mac_regs __iomem *regs)
227static void mac_wol_reset(struct mac_regs __iomem * regs)
228{ 223{
229 224
230 /* Turn off SWPTAG right after leaving power mode */ 225 /* Turn off SWPTAG right after leaving power mode */
@@ -242,7 +237,6 @@ static void mac_wol_reset(struct mac_regs __iomem * regs)
242 writew(0xFFFF, &regs->WOLSRClr); 237 writew(0xFFFF, &regs->WOLSRClr);
243} 238}
244 239
245static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
246static const struct ethtool_ops velocity_ethtool_ops; 240static const struct ethtool_ops velocity_ethtool_ops;
247 241
248/* 242/*
@@ -253,10 +247,10 @@ MODULE_AUTHOR("VIA Networking Technologies, Inc.");
253MODULE_LICENSE("GPL"); 247MODULE_LICENSE("GPL");
254MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver"); 248MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
255 249
256#define VELOCITY_PARAM(N,D) \ 250#define VELOCITY_PARAM(N, D) \
257 static int N[MAX_UNITS]=OPTION_DEFAULT;\ 251 static int N[MAX_UNITS] = OPTION_DEFAULT;\
258 module_param_array(N, int, NULL, 0); \ 252 module_param_array(N, int, NULL, 0); \
259 MODULE_PARM_DESC(N, D); 253 MODULE_PARM_DESC(N, D);
260 254
261#define RX_DESC_MIN 64 255#define RX_DESC_MIN 64
262#define RX_DESC_MAX 255 256#define RX_DESC_MAX 255
@@ -336,8 +330,8 @@ VELOCITY_PARAM(flow_control, "Enable flow control ability");
336 4: indicate 10Mbps full duplex mode 330 4: indicate 10Mbps full duplex mode
337 331
338 Note: 332 Note:
339 if EEPROM have been set to the force mode, this option is ignored 333 if EEPROM have been set to the force mode, this option is ignored
340 by driver. 334 by driver.
341*/ 335*/
342VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode"); 336VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
343 337
@@ -370,76 +364,14 @@ static int rx_copybreak = 200;
370module_param(rx_copybreak, int, 0644); 364module_param(rx_copybreak, int, 0644);
371MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames"); 365MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
372 366
373static void velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr,
374 const struct velocity_info_tbl *info);
375static int velocity_get_pci_info(struct velocity_info *, struct pci_dev *pdev);
376static void velocity_print_info(struct velocity_info *vptr);
377static int velocity_open(struct net_device *dev);
378static int velocity_change_mtu(struct net_device *dev, int mtu);
379static int velocity_xmit(struct sk_buff *skb, struct net_device *dev);
380static irqreturn_t velocity_intr(int irq, void *dev_instance);
381static void velocity_set_multi(struct net_device *dev);
382static struct net_device_stats *velocity_get_stats(struct net_device *dev);
383static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
384static int velocity_close(struct net_device *dev);
385static int velocity_receive_frame(struct velocity_info *, int idx);
386static int velocity_alloc_rx_buf(struct velocity_info *, int idx);
387static void velocity_free_rd_ring(struct velocity_info *vptr);
388static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *);
389static int velocity_soft_reset(struct velocity_info *vptr);
390static void mii_init(struct velocity_info *vptr, u32 mii_status);
391static u32 velocity_get_link(struct net_device *dev);
392static u32 velocity_get_opt_media_mode(struct velocity_info *vptr);
393static void velocity_print_link_status(struct velocity_info *vptr);
394static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs);
395static void velocity_shutdown(struct velocity_info *vptr);
396static void enable_flow_control_ability(struct velocity_info *vptr);
397static void enable_mii_autopoll(struct mac_regs __iomem * regs);
398static int velocity_mii_read(struct mac_regs __iomem *, u8 byIdx, u16 * pdata);
399static int velocity_mii_write(struct mac_regs __iomem *, u8 byMiiAddr, u16 data);
400static u32 mii_check_media_mode(struct mac_regs __iomem * regs);
401static u32 check_connection_type(struct mac_regs __iomem * regs);
402static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status);
403
404#ifdef CONFIG_PM 367#ifdef CONFIG_PM
405
406static int velocity_suspend(struct pci_dev *pdev, pm_message_t state);
407static int velocity_resume(struct pci_dev *pdev);
408
409static DEFINE_SPINLOCK(velocity_dev_list_lock); 368static DEFINE_SPINLOCK(velocity_dev_list_lock);
410static LIST_HEAD(velocity_dev_list); 369static LIST_HEAD(velocity_dev_list);
411
412#endif
413
414#if defined(CONFIG_PM) && defined(CONFIG_INET)
415
416static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr);
417
418static struct notifier_block velocity_inetaddr_notifier = {
419 .notifier_call = velocity_netdev_event,
420};
421
422static void velocity_register_notifier(void)
423{
424 register_inetaddr_notifier(&velocity_inetaddr_notifier);
425}
426
427static void velocity_unregister_notifier(void)
428{
429 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
430}
431
432#else
433
434#define velocity_register_notifier() do {} while (0)
435#define velocity_unregister_notifier() do {} while (0)
436
437#endif 370#endif
438 371
439/* 372/*
440 * Internal board variants. At the moment we have only one 373 * Internal board variants. At the moment we have only one
441 */ 374 */
442
443static struct velocity_info_tbl chip_info_table[] = { 375static struct velocity_info_tbl chip_info_table[] = {
444 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL}, 376 {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 1, 0x00FFFFFFUL},
445 { } 377 { }
@@ -449,7 +381,6 @@ static struct velocity_info_tbl chip_info_table[] = {
449 * Describe the PCI device identifiers that we support in this 381 * Describe the PCI device identifiers that we support in this
450 * device driver. Used for hotplug autoloading. 382 * device driver. Used for hotplug autoloading.
451 */ 383 */
452
453static const struct pci_device_id velocity_id_table[] __devinitdata = { 384static const struct pci_device_id velocity_id_table[] __devinitdata = {
454 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) }, 385 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X) },
455 { } 386 { }
@@ -464,7 +395,6 @@ MODULE_DEVICE_TABLE(pci, velocity_id_table);
464 * Given a chip identifier return a suitable description. Returns 395 * Given a chip identifier return a suitable description. Returns
465 * a pointer a static string valid while the driver is loaded. 396 * a pointer a static string valid while the driver is loaded.
466 */ 397 */
467
468static const char __devinit *get_chip_name(enum chip_type chip_id) 398static const char __devinit *get_chip_name(enum chip_type chip_id)
469{ 399{
470 int i; 400 int i;
@@ -482,7 +412,6 @@ static const char __devinit *get_chip_name(enum chip_type chip_id)
482 * unload for each active device that is present. Disconnects 412 * unload for each active device that is present. Disconnects
483 * the device from the network layer and frees all the resources 413 * the device from the network layer and frees all the resources
484 */ 414 */
485
486static void __devexit velocity_remove1(struct pci_dev *pdev) 415static void __devexit velocity_remove1(struct pci_dev *pdev)
487{ 416{
488 struct net_device *dev = pci_get_drvdata(pdev); 417 struct net_device *dev = pci_get_drvdata(pdev);
@@ -520,7 +449,6 @@ static void __devexit velocity_remove1(struct pci_dev *pdev)
520 * all the verification and checking as well as reporting so that 449 * all the verification and checking as well as reporting so that
521 * we don't duplicate code for each option. 450 * we don't duplicate code for each option.
522 */ 451 */
523
524static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max, int def, char *name, const char *devname) 452static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max, int def, char *name, const char *devname)
525{ 453{
526 if (val == -1) 454 if (val == -1)
@@ -549,8 +477,7 @@ static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max,
549 * all the verification and checking as well as reporting so that 477 * all the verification and checking as well as reporting so that
550 * we don't duplicate code for each option. 478 * we don't duplicate code for each option.
551 */ 479 */
552 480static void __devinit velocity_set_bool_opt(u32 *opt, int val, int def, u32 flag, char *name, const char *devname)
553static void __devinit velocity_set_bool_opt(u32 * opt, int val, int def, u32 flag, char *name, const char *devname)
554{ 481{
555 (*opt) &= (~flag); 482 (*opt) &= (~flag);
556 if (val == -1) 483 if (val == -1)
@@ -575,7 +502,6 @@ static void __devinit velocity_set_bool_opt(u32 * opt, int val, int def, u32 fla
575 * Turn the module and command options into a single structure 502 * Turn the module and command options into a single structure
576 * for the current device 503 * for the current device
577 */ 504 */
578
579static void __devinit velocity_get_options(struct velocity_opt *opts, int index, const char *devname) 505static void __devinit velocity_get_options(struct velocity_opt *opts, int index, const char *devname)
580{ 506{
581 507
@@ -601,10 +527,9 @@ static void __devinit velocity_get_options(struct velocity_opt *opts, int index,
601 * Initialize the content addressable memory used for filters. Load 527 * Initialize the content addressable memory used for filters. Load
602 * appropriately according to the presence of VLAN 528 * appropriately according to the presence of VLAN
603 */ 529 */
604
605static void velocity_init_cam_filter(struct velocity_info *vptr) 530static void velocity_init_cam_filter(struct velocity_info *vptr)
606{ 531{
607 struct mac_regs __iomem * regs = vptr->mac_regs; 532 struct mac_regs __iomem *regs = vptr->mac_regs;
608 533
609 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */ 534 /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
610 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG); 535 WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
@@ -647,19 +572,19 @@ static void velocity_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
647{ 572{
648 struct velocity_info *vptr = netdev_priv(dev); 573 struct velocity_info *vptr = netdev_priv(dev);
649 574
650 spin_lock_irq(&vptr->lock); 575 spin_lock_irq(&vptr->lock);
651 velocity_init_cam_filter(vptr); 576 velocity_init_cam_filter(vptr);
652 spin_unlock_irq(&vptr->lock); 577 spin_unlock_irq(&vptr->lock);
653} 578}
654 579
655static void velocity_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid) 580static void velocity_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
656{ 581{
657 struct velocity_info *vptr = netdev_priv(dev); 582 struct velocity_info *vptr = netdev_priv(dev);
658 583
659 spin_lock_irq(&vptr->lock); 584 spin_lock_irq(&vptr->lock);
660 vlan_group_set_device(vptr->vlgrp, vid, NULL); 585 vlan_group_set_device(vptr->vlgrp, vid, NULL);
661 velocity_init_cam_filter(vptr); 586 velocity_init_cam_filter(vptr);
662 spin_unlock_irq(&vptr->lock); 587 spin_unlock_irq(&vptr->lock);
663} 588}
664 589
665static void velocity_init_rx_ring_indexes(struct velocity_info *vptr) 590static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
@@ -674,11 +599,10 @@ static void velocity_init_rx_ring_indexes(struct velocity_info *vptr)
674 * Reset the ownership and status for the receive ring side. 599 * Reset the ownership and status for the receive ring side.
675 * Hand all the receive queue to the NIC. 600 * Hand all the receive queue to the NIC.
676 */ 601 */
677
678static void velocity_rx_reset(struct velocity_info *vptr) 602static void velocity_rx_reset(struct velocity_info *vptr)
679{ 603{
680 604
681 struct mac_regs __iomem * regs = vptr->mac_regs; 605 struct mac_regs __iomem *regs = vptr->mac_regs;
682 int i; 606 int i;
683 607
684 velocity_init_rx_ring_indexes(vptr); 608 velocity_init_rx_ring_indexes(vptr);
@@ -696,6 +620,647 @@ static void velocity_rx_reset(struct velocity_info *vptr)
696} 620}
697 621
698/** 622/**
623 * velocity_get_opt_media_mode - get media selection
624 * @vptr: velocity adapter
625 *
626 * Get the media mode stored in EEPROM or module options and load
627 * mii_status accordingly. The requested link state information
628 * is also returned.
629 */
630static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
631{
632 u32 status = 0;
633
634 switch (vptr->options.spd_dpx) {
635 case SPD_DPX_AUTO:
636 status = VELOCITY_AUTONEG_ENABLE;
637 break;
638 case SPD_DPX_100_FULL:
639 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
640 break;
641 case SPD_DPX_10_FULL:
642 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
643 break;
644 case SPD_DPX_100_HALF:
645 status = VELOCITY_SPEED_100;
646 break;
647 case SPD_DPX_10_HALF:
648 status = VELOCITY_SPEED_10;
649 break;
650 }
651 vptr->mii_status = status;
652 return status;
653}
654
655/**
656 * safe_disable_mii_autopoll - autopoll off
657 * @regs: velocity registers
658 *
659 * Turn off the autopoll and wait for it to disable on the chip
660 */
661static void safe_disable_mii_autopoll(struct mac_regs __iomem *regs)
662{
663 u16 ww;
664
665 /* turn off MAUTO */
666 writeb(0, &regs->MIICR);
667 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
668 udelay(1);
669 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
670 break;
671 }
672}
673
674/**
675 * enable_mii_autopoll - turn on autopolling
676 * @regs: velocity registers
677 *
678 * Enable the MII link status autopoll feature on the Velocity
679 * hardware. Wait for it to enable.
680 */
681static void enable_mii_autopoll(struct mac_regs __iomem *regs)
682{
683 int ii;
684
685 writeb(0, &(regs->MIICR));
686 writeb(MIIADR_SWMPL, &regs->MIIADR);
687
688 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
689 udelay(1);
690 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
691 break;
692 }
693
694 writeb(MIICR_MAUTO, &regs->MIICR);
695
696 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
697 udelay(1);
698 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
699 break;
700 }
701
702}
703
704/**
705 * velocity_mii_read - read MII data
706 * @regs: velocity registers
707 * @index: MII register index
708 * @data: buffer for received data
709 *
710 * Perform a single read of an MII 16bit register. Returns zero
711 * on success or -ETIMEDOUT if the PHY did not respond.
712 */
713static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
714{
715 u16 ww;
716
717 /*
718 * Disable MIICR_MAUTO, so that mii addr can be set normally
719 */
720 safe_disable_mii_autopoll(regs);
721
722 writeb(index, &regs->MIIADR);
723
724 BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
725
726 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
727 if (!(readb(&regs->MIICR) & MIICR_RCMD))
728 break;
729 }
730
731 *data = readw(&regs->MIIDATA);
732
733 enable_mii_autopoll(regs);
734 if (ww == W_MAX_TIMEOUT)
735 return -ETIMEDOUT;
736 return 0;
737}
738
739
740/**
741 * mii_check_media_mode - check media state
742 * @regs: velocity registers
743 *
744 * Check the current MII status and determine the link status
745 * accordingly
746 */
747static u32 mii_check_media_mode(struct mac_regs __iomem *regs)
748{
749 u32 status = 0;
750 u16 ANAR;
751
752 if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
753 status |= VELOCITY_LINK_FAIL;
754
755 if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
756 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
757 else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
758 status |= (VELOCITY_SPEED_1000);
759 else {
760 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
761 if (ANAR & ANAR_TXFD)
762 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
763 else if (ANAR & ANAR_TX)
764 status |= VELOCITY_SPEED_100;
765 else if (ANAR & ANAR_10FD)
766 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
767 else
768 status |= (VELOCITY_SPEED_10);
769 }
770
771 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
772 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
773 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
774 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
775 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
776 status |= VELOCITY_AUTONEG_ENABLE;
777 }
778 }
779
780 return status;
781}
782
783/**
784 * velocity_mii_write - write MII data
785 * @regs: velocity registers
786 * @index: MII register index
787 * @data: 16bit data for the MII register
788 *
789 * Perform a single write to an MII 16bit register. Returns zero
790 * on success or -ETIMEDOUT if the PHY did not respond.
791 */
792static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
793{
794 u16 ww;
795
796 /*
797 * Disable MIICR_MAUTO, so that mii addr can be set normally
798 */
799 safe_disable_mii_autopoll(regs);
800
801 /* MII reg offset */
802 writeb(mii_addr, &regs->MIIADR);
803 /* set MII data */
804 writew(data, &regs->MIIDATA);
805
806 /* turn on MIICR_WCMD */
807 BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
808
809 /* W_MAX_TIMEOUT is the timeout period */
810 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
811 udelay(5);
812 if (!(readb(&regs->MIICR) & MIICR_WCMD))
813 break;
814 }
815 enable_mii_autopoll(regs);
816
817 if (ww == W_MAX_TIMEOUT)
818 return -ETIMEDOUT;
819 return 0;
820}
821
822/**
823 * set_mii_flow_control - flow control setup
824 * @vptr: velocity interface
825 *
826 * Set up the flow control on this interface according to
827 * the supplied user/eeprom options.
828 */
829static void set_mii_flow_control(struct velocity_info *vptr)
830{
831 /*Enable or Disable PAUSE in ANAR */
832 switch (vptr->options.flow_cntl) {
833 case FLOW_CNTL_TX:
834 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
835 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
836 break;
837
838 case FLOW_CNTL_RX:
839 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
840 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
841 break;
842
843 case FLOW_CNTL_TX_RX:
844 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
845 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
846 break;
847
848 case FLOW_CNTL_DISABLE:
849 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
850 MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
851 break;
852 default:
853 break;
854 }
855}
856
857/**
858 * mii_set_auto_on - autonegotiate on
859 * @vptr: velocity
860 *
861 * Enable autonegotation on this interface
862 */
863static void mii_set_auto_on(struct velocity_info *vptr)
864{
865 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
866 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
867 else
868 MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
869}
870
871static u32 check_connection_type(struct mac_regs __iomem *regs)
872{
873 u32 status = 0;
874 u8 PHYSR0;
875 u16 ANAR;
876 PHYSR0 = readb(&regs->PHYSR0);
877
878 /*
879 if (!(PHYSR0 & PHYSR0_LINKGD))
880 status|=VELOCITY_LINK_FAIL;
881 */
882
883 if (PHYSR0 & PHYSR0_FDPX)
884 status |= VELOCITY_DUPLEX_FULL;
885
886 if (PHYSR0 & PHYSR0_SPDG)
887 status |= VELOCITY_SPEED_1000;
888 else if (PHYSR0 & PHYSR0_SPD10)
889 status |= VELOCITY_SPEED_10;
890 else
891 status |= VELOCITY_SPEED_100;
892
893 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
894 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
895 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
896 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
897 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
898 status |= VELOCITY_AUTONEG_ENABLE;
899 }
900 }
901
902 return status;
903}
904
905
906
907/**
908 * velocity_set_media_mode - set media mode
909 * @mii_status: old MII link state
910 *
911 * Check the media link state and configure the flow control
912 * PHY and also velocity hardware setup accordingly. In particular
913 * we need to set up CD polling and frame bursting.
914 */
915static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
916{
917 u32 curr_status;
918 struct mac_regs __iomem *regs = vptr->mac_regs;
919
920 vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
921 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
922
923 /* Set mii link status */
924 set_mii_flow_control(vptr);
925
926 /*
927 Check if new status is consisent with current status
928 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
929 || (mii_status==curr_status)) {
930 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
931 vptr->mii_status=check_connection_type(vptr->mac_regs);
932 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
933 return 0;
934 }
935 */
936
937 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
938 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
939
940 /*
941 * If connection type is AUTO
942 */
943 if (mii_status & VELOCITY_AUTONEG_ENABLE) {
944 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
945 /* clear force MAC mode bit */
946 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
947 /* set duplex mode of MAC according to duplex mode of MII */
948 MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
949 MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
950 MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
951
952 /* enable AUTO-NEGO mode */
953 mii_set_auto_on(vptr);
954 } else {
955 u16 ANAR;
956 u8 CHIPGCR;
957
958 /*
959 * 1. if it's 3119, disable frame bursting in halfduplex mode
960 * and enable it in fullduplex mode
961 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
962 * 3. only enable CD heart beat counter in 10HD mode
963 */
964
965 /* set force MAC mode bit */
966 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
967
968 CHIPGCR = readb(&regs->CHIPGCR);
969 CHIPGCR &= ~CHIPGCR_FCGMII;
970
971 if (mii_status & VELOCITY_DUPLEX_FULL) {
972 CHIPGCR |= CHIPGCR_FCFDX;
973 writeb(CHIPGCR, &regs->CHIPGCR);
974 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
975 if (vptr->rev_id < REV_ID_VT3216_A0)
976 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
977 } else {
978 CHIPGCR &= ~CHIPGCR_FCFDX;
979 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
980 writeb(CHIPGCR, &regs->CHIPGCR);
981 if (vptr->rev_id < REV_ID_VT3216_A0)
982 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
983 }
984
985 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
986
987 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10))
988 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
989 else
990 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
991
992 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
993 velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
994 ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
995 if (mii_status & VELOCITY_SPEED_100) {
996 if (mii_status & VELOCITY_DUPLEX_FULL)
997 ANAR |= ANAR_TXFD;
998 else
999 ANAR |= ANAR_TX;
1000 } else {
1001 if (mii_status & VELOCITY_DUPLEX_FULL)
1002 ANAR |= ANAR_10FD;
1003 else
1004 ANAR |= ANAR_10;
1005 }
1006 velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
1007 /* enable AUTO-NEGO mode */
1008 mii_set_auto_on(vptr);
1009 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
1010 }
1011 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
1012 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
1013 return VELOCITY_LINK_CHANGE;
1014}
1015
1016/**
1017 * velocity_print_link_status - link status reporting
1018 * @vptr: velocity to report on
1019 *
1020 * Turn the link status of the velocity card into a kernel log
1021 * description of the new link state, detailing speed and duplex
1022 * status
1023 */
1024static void velocity_print_link_status(struct velocity_info *vptr)
1025{
1026
1027 if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1028 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
1029 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1030 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->dev->name);
1031
1032 if (vptr->mii_status & VELOCITY_SPEED_1000)
1033 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1034 else if (vptr->mii_status & VELOCITY_SPEED_100)
1035 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1036 else
1037 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1038
1039 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1040 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1041 else
1042 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1043 } else {
1044 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
1045 switch (vptr->options.spd_dpx) {
1046 case SPD_DPX_100_HALF:
1047 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1048 break;
1049 case SPD_DPX_100_FULL:
1050 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1051 break;
1052 case SPD_DPX_10_HALF:
1053 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1054 break;
1055 case SPD_DPX_10_FULL:
1056 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1057 break;
1058 default:
1059 break;
1060 }
1061 }
1062}
1063
1064/**
1065 * enable_flow_control_ability - flow control
1066 * @vptr: veloity to configure
1067 *
1068 * Set up flow control according to the flow control options
1069 * determined by the eeprom/configuration.
1070 */
1071static void enable_flow_control_ability(struct velocity_info *vptr)
1072{
1073
1074 struct mac_regs __iomem *regs = vptr->mac_regs;
1075
1076 switch (vptr->options.flow_cntl) {
1077
1078 case FLOW_CNTL_DEFAULT:
1079 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
1080 writel(CR0_FDXRFCEN, &regs->CR0Set);
1081 else
1082 writel(CR0_FDXRFCEN, &regs->CR0Clr);
1083
1084 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
1085 writel(CR0_FDXTFCEN, &regs->CR0Set);
1086 else
1087 writel(CR0_FDXTFCEN, &regs->CR0Clr);
1088 break;
1089
1090 case FLOW_CNTL_TX:
1091 writel(CR0_FDXTFCEN, &regs->CR0Set);
1092 writel(CR0_FDXRFCEN, &regs->CR0Clr);
1093 break;
1094
1095 case FLOW_CNTL_RX:
1096 writel(CR0_FDXRFCEN, &regs->CR0Set);
1097 writel(CR0_FDXTFCEN, &regs->CR0Clr);
1098 break;
1099
1100 case FLOW_CNTL_TX_RX:
1101 writel(CR0_FDXTFCEN, &regs->CR0Set);
1102 writel(CR0_FDXRFCEN, &regs->CR0Set);
1103 break;
1104
1105 case FLOW_CNTL_DISABLE:
1106 writel(CR0_FDXRFCEN, &regs->CR0Clr);
1107 writel(CR0_FDXTFCEN, &regs->CR0Clr);
1108 break;
1109
1110 default:
1111 break;
1112 }
1113
1114}
1115
1116/**
1117 * velocity_soft_reset - soft reset
1118 * @vptr: velocity to reset
1119 *
1120 * Kick off a soft reset of the velocity adapter and then poll
1121 * until the reset sequence has completed before returning.
1122 */
1123static int velocity_soft_reset(struct velocity_info *vptr)
1124{
1125 struct mac_regs __iomem *regs = vptr->mac_regs;
1126 int i = 0;
1127
1128 writel(CR0_SFRST, &regs->CR0Set);
1129
1130 for (i = 0; i < W_MAX_TIMEOUT; i++) {
1131 udelay(5);
1132 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
1133 break;
1134 }
1135
1136 if (i == W_MAX_TIMEOUT) {
1137 writel(CR0_FORSRST, &regs->CR0Set);
1138 /* FIXME: PCI POSTING */
1139 /* delay 2ms */
1140 mdelay(2);
1141 }
1142 return 0;
1143}
1144
1145/**
1146 * velocity_set_multi - filter list change callback
1147 * @dev: network device
1148 *
1149 * Called by the network layer when the filter lists need to change
1150 * for a velocity adapter. Reload the CAMs with the new address
1151 * filter ruleset.
1152 */
1153static void velocity_set_multi(struct net_device *dev)
1154{
1155 struct velocity_info *vptr = netdev_priv(dev);
1156 struct mac_regs __iomem *regs = vptr->mac_regs;
1157 u8 rx_mode;
1158 int i;
1159 struct dev_mc_list *mclist;
1160
1161 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1162 writel(0xffffffff, &regs->MARCAM[0]);
1163 writel(0xffffffff, &regs->MARCAM[4]);
1164 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
1165 } else if ((dev->mc_count > vptr->multicast_limit)
1166 || (dev->flags & IFF_ALLMULTI)) {
1167 writel(0xffffffff, &regs->MARCAM[0]);
1168 writel(0xffffffff, &regs->MARCAM[4]);
1169 rx_mode = (RCR_AM | RCR_AB);
1170 } else {
1171 int offset = MCAM_SIZE - vptr->multicast_limit;
1172 mac_get_cam_mask(regs, vptr->mCAMmask);
1173
1174 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) {
1175 mac_set_cam(regs, i + offset, mclist->dmi_addr);
1176 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
1177 }
1178
1179 mac_set_cam_mask(regs, vptr->mCAMmask);
1180 rx_mode = RCR_AM | RCR_AB | RCR_AP;
1181 }
1182 if (dev->mtu > 1500)
1183 rx_mode |= RCR_AL;
1184
1185 BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
1186
1187}
1188
1189/*
1190 * MII access , media link mode setting functions
1191 */
1192
1193/**
1194 * mii_init - set up MII
1195 * @vptr: velocity adapter
1196 * @mii_status: links tatus
1197 *
1198 * Set up the PHY for the current link state.
1199 */
1200static void mii_init(struct velocity_info *vptr, u32 mii_status)
1201{
1202 u16 BMCR;
1203
1204 switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
1205 case PHYID_CICADA_CS8201:
1206 /*
1207 * Reset to hardware default
1208 */
1209 MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
1210 /*
1211 * Turn on ECHODIS bit in NWay-forced full mode and turn it
1212 * off it in NWay-forced half mode for NWay-forced v.s.
1213 * legacy-forced issue.
1214 */
1215 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1216 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
1217 else
1218 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
1219 /*
1220 * Turn on Link/Activity LED enable bit for CIS8201
1221 */
1222 MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
1223 break;
1224 case PHYID_VT3216_32BIT:
1225 case PHYID_VT3216_64BIT:
1226 /*
1227 * Reset to hardware default
1228 */
1229 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
1230 /*
1231 * Turn on ECHODIS bit in NWay-forced full mode and turn it
1232 * off it in NWay-forced half mode for NWay-forced v.s.
1233 * legacy-forced issue
1234 */
1235 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1236 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
1237 else
1238 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
1239 break;
1240
1241 case PHYID_MARVELL_1000:
1242 case PHYID_MARVELL_1000S:
1243 /*
1244 * Assert CRS on Transmit
1245 */
1246 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
1247 /*
1248 * Reset to hardware default
1249 */
1250 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
1251 break;
1252 default:
1253 ;
1254 }
1255 velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
1256 if (BMCR & BMCR_ISO) {
1257 BMCR &= ~BMCR_ISO;
1258 velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
1259 }
1260}
1261
1262
1263/**
699 * velocity_init_registers - initialise MAC registers 1264 * velocity_init_registers - initialise MAC registers
700 * @vptr: velocity to init 1265 * @vptr: velocity to init
701 * @type: type of initialisation (hot or cold) 1266 * @type: type of initialisation (hot or cold)
@@ -703,11 +1268,10 @@ static void velocity_rx_reset(struct velocity_info *vptr)
703 * Initialise the MAC on a reset or on first set up on the 1268 * Initialise the MAC on a reset or on first set up on the
704 * hardware. 1269 * hardware.
705 */ 1270 */
706
707static void velocity_init_registers(struct velocity_info *vptr, 1271static void velocity_init_registers(struct velocity_info *vptr,
708 enum velocity_init_type type) 1272 enum velocity_init_type type)
709{ 1273{
710 struct mac_regs __iomem * regs = vptr->mac_regs; 1274 struct mac_regs __iomem *regs = vptr->mac_regs;
711 int i, mii_status; 1275 int i, mii_status;
712 1276
713 mac_wol_reset(regs); 1277 mac_wol_reset(regs);
@@ -750,9 +1314,9 @@ static void velocity_init_registers(struct velocity_info *vptr,
750 mdelay(5); 1314 mdelay(5);
751 1315
752 mac_eeprom_reload(regs); 1316 mac_eeprom_reload(regs);
753 for (i = 0; i < 6; i++) { 1317 for (i = 0; i < 6; i++)
754 writeb(vptr->dev->dev_addr[i], &(regs->PAR[i])); 1318 writeb(vptr->dev->dev_addr[i], &(regs->PAR[i]));
755 } 1319
756 /* 1320 /*
757 * clear Pre_ACPI bit. 1321 * clear Pre_ACPI bit.
758 */ 1322 */
@@ -819,291 +1383,29 @@ static void velocity_init_registers(struct velocity_info *vptr,
819 } 1383 }
820} 1384}
821 1385
822/** 1386static void velocity_give_many_rx_descs(struct velocity_info *vptr)
823 * velocity_soft_reset - soft reset
824 * @vptr: velocity to reset
825 *
826 * Kick off a soft reset of the velocity adapter and then poll
827 * until the reset sequence has completed before returning.
828 */
829
830static int velocity_soft_reset(struct velocity_info *vptr)
831{
832 struct mac_regs __iomem * regs = vptr->mac_regs;
833 int i = 0;
834
835 writel(CR0_SFRST, &regs->CR0Set);
836
837 for (i = 0; i < W_MAX_TIMEOUT; i++) {
838 udelay(5);
839 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
840 break;
841 }
842
843 if (i == W_MAX_TIMEOUT) {
844 writel(CR0_FORSRST, &regs->CR0Set);
845 /* FIXME: PCI POSTING */
846 /* delay 2ms */
847 mdelay(2);
848 }
849 return 0;
850}
851
852static const struct net_device_ops velocity_netdev_ops = {
853 .ndo_open = velocity_open,
854 .ndo_stop = velocity_close,
855 .ndo_start_xmit = velocity_xmit,
856 .ndo_get_stats = velocity_get_stats,
857 .ndo_validate_addr = eth_validate_addr,
858 .ndo_set_mac_address = eth_mac_addr,
859 .ndo_set_multicast_list = velocity_set_multi,
860 .ndo_change_mtu = velocity_change_mtu,
861 .ndo_do_ioctl = velocity_ioctl,
862 .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
863 .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
864 .ndo_vlan_rx_register = velocity_vlan_rx_register,
865};
866
867/**
868 * velocity_found1 - set up discovered velocity card
869 * @pdev: PCI device
870 * @ent: PCI device table entry that matched
871 *
872 * Configure a discovered adapter from scratch. Return a negative
873 * errno error code on failure paths.
874 */
875
876static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
877{ 1387{
878 static int first = 1; 1388 struct mac_regs __iomem *regs = vptr->mac_regs;
879 struct net_device *dev; 1389 int avail, dirty, unusable;
880 int i;
881 const char *drv_string;
882 const struct velocity_info_tbl *info = &chip_info_table[ent->driver_data];
883 struct velocity_info *vptr;
884 struct mac_regs __iomem * regs;
885 int ret = -ENOMEM;
886
887 /* FIXME: this driver, like almost all other ethernet drivers,
888 * can support more than MAX_UNITS.
889 */
890 if (velocity_nics >= MAX_UNITS) {
891 dev_notice(&pdev->dev, "already found %d NICs.\n",
892 velocity_nics);
893 return -ENODEV;
894 }
895
896 dev = alloc_etherdev(sizeof(struct velocity_info));
897 if (!dev) {
898 dev_err(&pdev->dev, "allocate net device failed.\n");
899 goto out;
900 }
901
902 /* Chain it all together */
903
904 SET_NETDEV_DEV(dev, &pdev->dev);
905 vptr = netdev_priv(dev);
906
907
908 if (first) {
909 printk(KERN_INFO "%s Ver. %s\n",
910 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
911 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
912 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
913 first = 0;
914 }
915
916 velocity_init_info(pdev, vptr, info);
917
918 vptr->dev = dev;
919
920 dev->irq = pdev->irq;
921
922 ret = pci_enable_device(pdev);
923 if (ret < 0)
924 goto err_free_dev;
925
926 ret = velocity_get_pci_info(vptr, pdev);
927 if (ret < 0) {
928 /* error message already printed */
929 goto err_disable;
930 }
931
932 ret = pci_request_regions(pdev, VELOCITY_NAME);
933 if (ret < 0) {
934 dev_err(&pdev->dev, "No PCI resources.\n");
935 goto err_disable;
936 }
937
938 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
939 if (regs == NULL) {
940 ret = -EIO;
941 goto err_release_res;
942 }
943
944 vptr->mac_regs = regs;
945
946 mac_wol_reset(regs);
947
948 dev->base_addr = vptr->ioaddr;
949
950 for (i = 0; i < 6; i++)
951 dev->dev_addr[i] = readb(&regs->PAR[i]);
952
953
954 drv_string = dev_driver_string(&pdev->dev);
955
956 velocity_get_options(&vptr->options, velocity_nics, drv_string);
957
958 /*
959 * Mask out the options cannot be set to the chip
960 */
961
962 vptr->options.flags &= info->flags;
963 1390
964 /* 1391 /*
965 * Enable the chip specified capbilities 1392 * RD number must be equal to 4X per hardware spec
1393 * (programming guide rev 1.20, p.13)
966 */ 1394 */
1395 if (vptr->rx.filled < 4)
1396 return;
967 1397
968 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL); 1398 wmb();
969
970 vptr->wol_opts = vptr->options.wol_opts;
971 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
972
973 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
974
975 dev->irq = pdev->irq;
976 dev->netdev_ops = &velocity_netdev_ops;
977 dev->ethtool_ops = &velocity_ethtool_ops;
978
979#ifdef VELOCITY_ZERO_COPY_SUPPORT
980 dev->features |= NETIF_F_SG;
981#endif
982 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER |
983 NETIF_F_HW_VLAN_RX;
984
985 if (vptr->flags & VELOCITY_FLAGS_TX_CSUM)
986 dev->features |= NETIF_F_IP_CSUM;
987
988 ret = register_netdev(dev);
989 if (ret < 0)
990 goto err_iounmap;
991
992 if (!velocity_get_link(dev)) {
993 netif_carrier_off(dev);
994 vptr->mii_status |= VELOCITY_LINK_FAIL;
995 }
996
997 velocity_print_info(vptr);
998 pci_set_drvdata(pdev, dev);
999
1000 /* and leave the chip powered down */
1001
1002 pci_set_power_state(pdev, PCI_D3hot);
1003#ifdef CONFIG_PM
1004 {
1005 unsigned long flags;
1006
1007 spin_lock_irqsave(&velocity_dev_list_lock, flags);
1008 list_add(&vptr->list, &velocity_dev_list);
1009 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
1010 }
1011#endif
1012 velocity_nics++;
1013out:
1014 return ret;
1015
1016err_iounmap:
1017 iounmap(regs);
1018err_release_res:
1019 pci_release_regions(pdev);
1020err_disable:
1021 pci_disable_device(pdev);
1022err_free_dev:
1023 free_netdev(dev);
1024 goto out;
1025}
1026
1027/**
1028 * velocity_print_info - per driver data
1029 * @vptr: velocity
1030 *
1031 * Print per driver data as the kernel driver finds Velocity
1032 * hardware
1033 */
1034
1035static void __devinit velocity_print_info(struct velocity_info *vptr)
1036{
1037 struct net_device *dev = vptr->dev;
1038
1039 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
1040 printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
1041 dev->name,
1042 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1043 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1044}
1045
1046/**
1047 * velocity_init_info - init private data
1048 * @pdev: PCI device
1049 * @vptr: Velocity info
1050 * @info: Board type
1051 *
1052 * Set up the initial velocity_info struct for the device that has been
1053 * discovered.
1054 */
1055
1056static void __devinit velocity_init_info(struct pci_dev *pdev,
1057 struct velocity_info *vptr,
1058 const struct velocity_info_tbl *info)
1059{
1060 memset(vptr, 0, sizeof(struct velocity_info));
1061
1062 vptr->pdev = pdev;
1063 vptr->chip_id = info->chip_id;
1064 vptr->tx.numq = info->txqueue;
1065 vptr->multicast_limit = MCAM_SIZE;
1066 spin_lock_init(&vptr->lock);
1067 INIT_LIST_HEAD(&vptr->list);
1068}
1069
1070/**
1071 * velocity_get_pci_info - retrieve PCI info for device
1072 * @vptr: velocity device
1073 * @pdev: PCI device it matches
1074 *
1075 * Retrieve the PCI configuration space data that interests us from
1076 * the kernel PCI layer
1077 */
1078
1079static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
1080{
1081 vptr->rev_id = pdev->revision;
1082
1083 pci_set_master(pdev);
1084
1085 vptr->ioaddr = pci_resource_start(pdev, 0);
1086 vptr->memaddr = pci_resource_start(pdev, 1);
1087
1088 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
1089 dev_err(&pdev->dev,
1090 "region #0 is not an I/O resource, aborting.\n");
1091 return -EINVAL;
1092 }
1093
1094 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
1095 dev_err(&pdev->dev,
1096 "region #1 is an I/O resource, aborting.\n");
1097 return -EINVAL;
1098 }
1099 1399
1100 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) { 1400 unusable = vptr->rx.filled & 0x0003;
1101 dev_err(&pdev->dev, "region #1 is too small.\n"); 1401 dirty = vptr->rx.dirty - unusable;
1102 return -EINVAL; 1402 for (avail = vptr->rx.filled & 0xfffc; avail; avail--) {
1403 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1404 vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
1103 } 1405 }
1104 vptr->pdev = pdev;
1105 1406
1106 return 0; 1407 writew(vptr->rx.filled & 0xfffc, &regs->RBRDU);
1408 vptr->rx.filled = unusable;
1107} 1409}
1108 1410
1109/** 1411/**
@@ -1113,7 +1415,6 @@ static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pc
1113 * Allocate PCI mapped DMA rings for the receive and transmit layer 1415 * Allocate PCI mapped DMA rings for the receive and transmit layer
1114 * to use. 1416 * to use.
1115 */ 1417 */
1116
1117static int velocity_init_dma_rings(struct velocity_info *vptr) 1418static int velocity_init_dma_rings(struct velocity_info *vptr)
1118{ 1419{
1119 struct velocity_opt *opt = &vptr->options; 1420 struct velocity_opt *opt = &vptr->options;
@@ -1154,46 +1455,50 @@ static int velocity_init_dma_rings(struct velocity_info *vptr)
1154 return 0; 1455 return 0;
1155} 1456}
1156 1457
1458static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
1459{
1460 vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
1461}
1462
1157/** 1463/**
1158 * velocity_free_dma_rings - free PCI ring pointers 1464 * velocity_alloc_rx_buf - allocate aligned receive buffer
1159 * @vptr: Velocity to free from 1465 * @vptr: velocity
1466 * @idx: ring index
1160 * 1467 *
1161 * Clean up the PCI ring buffers allocated to this velocity. 1468 * Allocate a new full sized buffer for the reception of a frame and
1469 * map it into PCI space for the hardware to use. The hardware
1470 * requires *64* byte alignment of the buffer which makes life
1471 * less fun than would be ideal.
1162 */ 1472 */
1163 1473static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1164static void velocity_free_dma_rings(struct velocity_info *vptr)
1165{ 1474{
1166 const int size = vptr->options.numrx * sizeof(struct rx_desc) + 1475 struct rx_desc *rd = &(vptr->rx.ring[idx]);
1167 vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq; 1476 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
1168
1169 pci_free_consistent(vptr->pdev, size, vptr->rx.ring, vptr->rx.pool_dma);
1170}
1171 1477
1172static void velocity_give_many_rx_descs(struct velocity_info *vptr) 1478 rd_info->skb = dev_alloc_skb(vptr->rx.buf_sz + 64);
1173{ 1479 if (rd_info->skb == NULL)
1174 struct mac_regs __iomem *regs = vptr->mac_regs; 1480 return -ENOMEM;
1175 int avail, dirty, unusable;
1176 1481
1177 /* 1482 /*
1178 * RD number must be equal to 4X per hardware spec 1483 * Do the gymnastics to get the buffer head for data at
1179 * (programming guide rev 1.20, p.13) 1484 * 64byte alignment.
1180 */ 1485 */
1181 if (vptr->rx.filled < 4) 1486 skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
1182 return; 1487 rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data,
1183 1488 vptr->rx.buf_sz, PCI_DMA_FROMDEVICE);
1184 wmb();
1185 1489
1186 unusable = vptr->rx.filled & 0x0003; 1490 /*
1187 dirty = vptr->rx.dirty - unusable; 1491 * Fill in the descriptor to match
1188 for (avail = vptr->rx.filled & 0xfffc; avail; avail--) { 1492 */
1189 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1190 vptr->rx.ring[dirty].rdesc0.len |= OWNED_BY_NIC;
1191 }
1192 1493
1193 writew(vptr->rx.filled & 0xfffc, &regs->RBRDU); 1494 *((u32 *) & (rd->rdesc0)) = 0;
1194 vptr->rx.filled = unusable; 1495 rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
1496 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1497 rd->pa_high = 0;
1498 return 0;
1195} 1499}
1196 1500
1501
1197static int velocity_rx_refill(struct velocity_info *vptr) 1502static int velocity_rx_refill(struct velocity_info *vptr)
1198{ 1503{
1199 int dirty = vptr->rx.dirty, done = 0; 1504 int dirty = vptr->rx.dirty, done = 0;
@@ -1221,42 +1526,6 @@ static int velocity_rx_refill(struct velocity_info *vptr)
1221 return done; 1526 return done;
1222} 1527}
1223 1528
1224static void velocity_set_rxbufsize(struct velocity_info *vptr, int mtu)
1225{
1226 vptr->rx.buf_sz = (mtu <= ETH_DATA_LEN) ? PKT_BUF_SZ : mtu + 32;
1227}
1228
1229/**
1230 * velocity_init_rd_ring - set up receive ring
1231 * @vptr: velocity to configure
1232 *
1233 * Allocate and set up the receive buffers for each ring slot and
1234 * assign them to the network adapter.
1235 */
1236
1237static int velocity_init_rd_ring(struct velocity_info *vptr)
1238{
1239 int ret = -ENOMEM;
1240
1241 vptr->rx.info = kcalloc(vptr->options.numrx,
1242 sizeof(struct velocity_rd_info), GFP_KERNEL);
1243 if (!vptr->rx.info)
1244 goto out;
1245
1246 velocity_init_rx_ring_indexes(vptr);
1247
1248 if (velocity_rx_refill(vptr) != vptr->options.numrx) {
1249 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1250 "%s: failed to allocate RX buffer.\n", vptr->dev->name);
1251 velocity_free_rd_ring(vptr);
1252 goto out;
1253 }
1254
1255 ret = 0;
1256out:
1257 return ret;
1258}
1259
1260/** 1529/**
1261 * velocity_free_rd_ring - free receive ring 1530 * velocity_free_rd_ring - free receive ring
1262 * @vptr: velocity to clean up 1531 * @vptr: velocity to clean up
@@ -1264,7 +1533,6 @@ out:
1264 * Free the receive buffers for each ring slot and any 1533 * Free the receive buffers for each ring slot and any
1265 * attached socket buffers that need to go away. 1534 * attached socket buffers that need to go away.
1266 */ 1535 */
1267
1268static void velocity_free_rd_ring(struct velocity_info *vptr) 1536static void velocity_free_rd_ring(struct velocity_info *vptr)
1269{ 1537{
1270 int i; 1538 int i;
@@ -1292,6 +1560,38 @@ static void velocity_free_rd_ring(struct velocity_info *vptr)
1292 vptr->rx.info = NULL; 1560 vptr->rx.info = NULL;
1293} 1561}
1294 1562
1563
1564
1565/**
1566 * velocity_init_rd_ring - set up receive ring
1567 * @vptr: velocity to configure
1568 *
1569 * Allocate and set up the receive buffers for each ring slot and
1570 * assign them to the network adapter.
1571 */
1572static int velocity_init_rd_ring(struct velocity_info *vptr)
1573{
1574 int ret = -ENOMEM;
1575
1576 vptr->rx.info = kcalloc(vptr->options.numrx,
1577 sizeof(struct velocity_rd_info), GFP_KERNEL);
1578 if (!vptr->rx.info)
1579 goto out;
1580
1581 velocity_init_rx_ring_indexes(vptr);
1582
1583 if (velocity_rx_refill(vptr) != vptr->options.numrx) {
1584 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1585 "%s: failed to allocate RX buffer.\n", vptr->dev->name);
1586 velocity_free_rd_ring(vptr);
1587 goto out;
1588 }
1589
1590 ret = 0;
1591out:
1592 return ret;
1593}
1594
1295/** 1595/**
1296 * velocity_init_td_ring - set up transmit ring 1596 * velocity_init_td_ring - set up transmit ring
1297 * @vptr: velocity 1597 * @vptr: velocity
@@ -1300,7 +1600,6 @@ static void velocity_free_rd_ring(struct velocity_info *vptr)
1300 * Returns zero on success or a negative posix errno code for 1600 * Returns zero on success or a negative posix errno code for
1301 * failure. 1601 * failure.
1302 */ 1602 */
1303
1304static int velocity_init_td_ring(struct velocity_info *vptr) 1603static int velocity_init_td_ring(struct velocity_info *vptr)
1305{ 1604{
1306 dma_addr_t curr; 1605 dma_addr_t curr;
@@ -1314,7 +1613,7 @@ static int velocity_init_td_ring(struct velocity_info *vptr)
1314 sizeof(struct velocity_td_info), 1613 sizeof(struct velocity_td_info),
1315 GFP_KERNEL); 1614 GFP_KERNEL);
1316 if (!vptr->tx.infos[j]) { 1615 if (!vptr->tx.infos[j]) {
1317 while(--j >= 0) 1616 while (--j >= 0)
1318 kfree(vptr->tx.infos[j]); 1617 kfree(vptr->tx.infos[j]);
1319 return -ENOMEM; 1618 return -ENOMEM;
1320 } 1619 }
@@ -1324,22 +1623,92 @@ static int velocity_init_td_ring(struct velocity_info *vptr)
1324 return 0; 1623 return 0;
1325} 1624}
1326 1625
1626/**
1627 * velocity_free_dma_rings - free PCI ring pointers
1628 * @vptr: Velocity to free from
1629 *
1630 * Clean up the PCI ring buffers allocated to this velocity.
1631 */
1632static void velocity_free_dma_rings(struct velocity_info *vptr)
1633{
1634 const int size = vptr->options.numrx * sizeof(struct rx_desc) +
1635 vptr->options.numtx * sizeof(struct tx_desc) * vptr->tx.numq;
1636
1637 pci_free_consistent(vptr->pdev, size, vptr->rx.ring, vptr->rx.pool_dma);
1638}
1639
1640
1641static int velocity_init_rings(struct velocity_info *vptr, int mtu)
1642{
1643 int ret;
1644
1645 velocity_set_rxbufsize(vptr, mtu);
1646
1647 ret = velocity_init_dma_rings(vptr);
1648 if (ret < 0)
1649 goto out;
1650
1651 ret = velocity_init_rd_ring(vptr);
1652 if (ret < 0)
1653 goto err_free_dma_rings_0;
1654
1655 ret = velocity_init_td_ring(vptr);
1656 if (ret < 0)
1657 goto err_free_rd_ring_1;
1658out:
1659 return ret;
1660
1661err_free_rd_ring_1:
1662 velocity_free_rd_ring(vptr);
1663err_free_dma_rings_0:
1664 velocity_free_dma_rings(vptr);
1665 goto out;
1666}
1667
1668/**
1669 * velocity_free_tx_buf - free transmit buffer
1670 * @vptr: velocity
1671 * @tdinfo: buffer
1672 *
1673 * Release an transmit buffer. If the buffer was preallocated then
1674 * recycle it, if not then unmap the buffer.
1675 */
1676static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
1677{
1678 struct sk_buff *skb = tdinfo->skb;
1679 int i;
1680 int pktlen;
1681
1682 /*
1683 * Don't unmap the pre-allocated tx_bufs
1684 */
1685 if (tdinfo->skb_dma) {
1686
1687 pktlen = max_t(unsigned int, skb->len, ETH_ZLEN);
1688 for (i = 0; i < tdinfo->nskb_dma; i++) {
1689 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], pktlen, PCI_DMA_TODEVICE);
1690 tdinfo->skb_dma[i] = 0;
1691 }
1692 }
1693 dev_kfree_skb_irq(skb);
1694 tdinfo->skb = NULL;
1695}
1696
1697
1327/* 1698/*
1328 * FIXME: could we merge this with velocity_free_tx_buf ? 1699 * FIXME: could we merge this with velocity_free_tx_buf ?
1329 */ 1700 */
1330
1331static void velocity_free_td_ring_entry(struct velocity_info *vptr, 1701static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1332 int q, int n) 1702 int q, int n)
1333{ 1703{
1334 struct velocity_td_info * td_info = &(vptr->tx.infos[q][n]); 1704 struct velocity_td_info *td_info = &(vptr->tx.infos[q][n]);
1335 int i; 1705 int i;
1336 1706
1337 if (td_info == NULL) 1707 if (td_info == NULL)
1338 return; 1708 return;
1339 1709
1340 if (td_info->skb) { 1710 if (td_info->skb) {
1341 for (i = 0; i < td_info->nskb_dma; i++) 1711 for (i = 0; i < td_info->nskb_dma; i++) {
1342 {
1343 if (td_info->skb_dma[i]) { 1712 if (td_info->skb_dma[i]) {
1344 pci_unmap_single(vptr->pdev, td_info->skb_dma[i], 1713 pci_unmap_single(vptr->pdev, td_info->skb_dma[i],
1345 td_info->skb->len, PCI_DMA_TODEVICE); 1714 td_info->skb->len, PCI_DMA_TODEVICE);
@@ -1358,7 +1727,6 @@ static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1358 * Free up the transmit ring for this particular velocity adapter. 1727 * Free up the transmit ring for this particular velocity adapter.
1359 * We free the ring contents but not the ring itself. 1728 * We free the ring contents but not the ring itself.
1360 */ 1729 */
1361
1362static void velocity_free_td_ring(struct velocity_info *vptr) 1730static void velocity_free_td_ring(struct velocity_info *vptr)
1363{ 1731{
1364 int i, j; 1732 int i, j;
@@ -1366,70 +1734,175 @@ static void velocity_free_td_ring(struct velocity_info *vptr)
1366 for (j = 0; j < vptr->tx.numq; j++) { 1734 for (j = 0; j < vptr->tx.numq; j++) {
1367 if (vptr->tx.infos[j] == NULL) 1735 if (vptr->tx.infos[j] == NULL)
1368 continue; 1736 continue;
1369 for (i = 0; i < vptr->options.numtx; i++) { 1737 for (i = 0; i < vptr->options.numtx; i++)
1370 velocity_free_td_ring_entry(vptr, j, i); 1738 velocity_free_td_ring_entry(vptr, j, i);
1371 1739
1372 }
1373 kfree(vptr->tx.infos[j]); 1740 kfree(vptr->tx.infos[j]);
1374 vptr->tx.infos[j] = NULL; 1741 vptr->tx.infos[j] = NULL;
1375 } 1742 }
1376} 1743}
1377 1744
1745
1746static void velocity_free_rings(struct velocity_info *vptr)
1747{
1748 velocity_free_td_ring(vptr);
1749 velocity_free_rd_ring(vptr);
1750 velocity_free_dma_rings(vptr);
1751}
1752
1378/** 1753/**
1379 * velocity_rx_srv - service RX interrupt 1754 * velocity_error - handle error from controller
1380 * @vptr: velocity 1755 * @vptr: velocity
1381 * @status: adapter status (unused) 1756 * @status: card status
1757 *
1758 * Process an error report from the hardware and attempt to recover
1759 * the card itself. At the moment we cannot recover from some
1760 * theoretically impossible errors but this could be fixed using
1761 * the pci_device_failed logic to bounce the hardware
1382 * 1762 *
1383 * Walk the receive ring of the velocity adapter and remove
1384 * any received packets from the receive queue. Hand the ring
1385 * slots back to the adapter for reuse.
1386 */ 1763 */
1387 1764static void velocity_error(struct velocity_info *vptr, int status)
1388static int velocity_rx_srv(struct velocity_info *vptr, int status)
1389{ 1765{
1390 struct net_device_stats *stats = &vptr->dev->stats;
1391 int rd_curr = vptr->rx.curr;
1392 int works = 0;
1393 1766
1394 do { 1767 if (status & ISR_TXSTLI) {
1395 struct rx_desc *rd = vptr->rx.ring + rd_curr; 1768 struct mac_regs __iomem *regs = vptr->mac_regs;
1396 1769
1397 if (!vptr->rx.info[rd_curr].skb) 1770 printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(&regs->TDIdx[0]));
1398 break; 1771 BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
1772 writew(TRDCSR_RUN, &regs->TDCSRClr);
1773 netif_stop_queue(vptr->dev);
1399 1774
1400 if (rd->rdesc0.len & OWNED_BY_NIC) 1775 /* FIXME: port over the pci_device_failed code and use it
1401 break; 1776 here */
1777 }
1402 1778
1403 rmb(); 1779 if (status & ISR_SRCI) {
1780 struct mac_regs __iomem *regs = vptr->mac_regs;
1781 int linked;
1782
1783 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1784 vptr->mii_status = check_connection_type(regs);
1404 1785
1786 /*
1787 * If it is a 3119, disable frame bursting in
1788 * halfduplex mode and enable it in fullduplex
1789 * mode
1790 */
1791 if (vptr->rev_id < REV_ID_VT3216_A0) {
1792 if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
1793 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
1794 else
1795 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
1796 }
1797 /*
1798 * Only enable CD heart beat counter in 10HD mode
1799 */
1800 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10))
1801 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
1802 else
1803 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
1804 }
1405 /* 1805 /*
1406 * Don't drop CE or RL error frame although RXOK is off 1806 * Get link status from PHYSR0
1407 */ 1807 */
1408 if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) { 1808 linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
1409 if (velocity_receive_frame(vptr, rd_curr) < 0)
1410 stats->rx_dropped++;
1411 } else {
1412 if (rd->rdesc0.RSR & RSR_CRC)
1413 stats->rx_crc_errors++;
1414 if (rd->rdesc0.RSR & RSR_FAE)
1415 stats->rx_frame_errors++;
1416 1809
1417 stats->rx_dropped++; 1810 if (linked) {
1811 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1812 netif_carrier_on(vptr->dev);
1813 } else {
1814 vptr->mii_status |= VELOCITY_LINK_FAIL;
1815 netif_carrier_off(vptr->dev);
1418 } 1816 }
1419 1817
1420 rd->size |= RX_INTEN; 1818 velocity_print_link_status(vptr);
1819 enable_flow_control_ability(vptr);
1421 1820
1422 rd_curr++; 1821 /*
1423 if (rd_curr >= vptr->options.numrx) 1822 * Re-enable auto-polling because SRCI will disable
1424 rd_curr = 0; 1823 * auto-polling
1425 } while (++works <= 15); 1824 */
1426 1825
1427 vptr->rx.curr = rd_curr; 1826 enable_mii_autopoll(regs);
1428 1827
1429 if ((works > 0) && (velocity_rx_refill(vptr) > 0)) 1828 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1430 velocity_give_many_rx_descs(vptr); 1829 netif_stop_queue(vptr->dev);
1830 else
1831 netif_wake_queue(vptr->dev);
1431 1832
1432 VAR_USED(stats); 1833 };
1834 if (status & ISR_MIBFI)
1835 velocity_update_hw_mibs(vptr);
1836 if (status & ISR_LSTEI)
1837 mac_rx_queue_wake(vptr->mac_regs);
1838}
1839
1840/**
1841 * tx_srv - transmit interrupt service
1842 * @vptr; Velocity
1843 * @status:
1844 *
1845 * Scan the queues looking for transmitted packets that
1846 * we can complete and clean up. Update any statistics as
1847 * necessary/
1848 */
1849static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
1850{
1851 struct tx_desc *td;
1852 int qnum;
1853 int full = 0;
1854 int idx;
1855 int works = 0;
1856 struct velocity_td_info *tdinfo;
1857 struct net_device_stats *stats = &vptr->dev->stats;
1858
1859 for (qnum = 0; qnum < vptr->tx.numq; qnum++) {
1860 for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0;
1861 idx = (idx + 1) % vptr->options.numtx) {
1862
1863 /*
1864 * Get Tx Descriptor
1865 */
1866 td = &(vptr->tx.rings[qnum][idx]);
1867 tdinfo = &(vptr->tx.infos[qnum][idx]);
1868
1869 if (td->tdesc0.len & OWNED_BY_NIC)
1870 break;
1871
1872 if ((works++ > 15))
1873 break;
1874
1875 if (td->tdesc0.TSR & TSR0_TERR) {
1876 stats->tx_errors++;
1877 stats->tx_dropped++;
1878 if (td->tdesc0.TSR & TSR0_CDH)
1879 stats->tx_heartbeat_errors++;
1880 if (td->tdesc0.TSR & TSR0_CRS)
1881 stats->tx_carrier_errors++;
1882 if (td->tdesc0.TSR & TSR0_ABT)
1883 stats->tx_aborted_errors++;
1884 if (td->tdesc0.TSR & TSR0_OWC)
1885 stats->tx_window_errors++;
1886 } else {
1887 stats->tx_packets++;
1888 stats->tx_bytes += tdinfo->skb->len;
1889 }
1890 velocity_free_tx_buf(vptr, tdinfo);
1891 vptr->tx.used[qnum]--;
1892 }
1893 vptr->tx.tail[qnum] = idx;
1894
1895 if (AVAIL_TD(vptr, qnum) < 1)
1896 full = 1;
1897 }
1898 /*
1899 * Look to see if we should kick the transmit network
1900 * layer for more work.
1901 */
1902 if (netif_queue_stopped(vptr->dev) && (full == 0)
1903 && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1904 netif_wake_queue(vptr->dev);
1905 }
1433 return works; 1906 return works;
1434} 1907}
1435 1908
@@ -1441,7 +1914,6 @@ static int velocity_rx_srv(struct velocity_info *vptr, int status)
1441 * Process the status bits for the received packet and determine 1914 * Process the status bits for the received packet and determine
1442 * if the checksum was computed and verified by the hardware 1915 * if the checksum was computed and verified by the hardware
1443 */ 1916 */
1444
1445static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb) 1917static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1446{ 1918{
1447 skb->ip_summed = CHECKSUM_NONE; 1919 skb->ip_summed = CHECKSUM_NONE;
@@ -1450,9 +1922,8 @@ static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1450 if (rd->rdesc1.CSM & CSM_IPOK) { 1922 if (rd->rdesc1.CSM & CSM_IPOK) {
1451 if ((rd->rdesc1.CSM & CSM_TCPKT) || 1923 if ((rd->rdesc1.CSM & CSM_TCPKT) ||
1452 (rd->rdesc1.CSM & CSM_UDPKT)) { 1924 (rd->rdesc1.CSM & CSM_UDPKT)) {
1453 if (!(rd->rdesc1.CSM & CSM_TUPOK)) { 1925 if (!(rd->rdesc1.CSM & CSM_TUPOK))
1454 return; 1926 return;
1455 }
1456 } 1927 }
1457 skb->ip_summed = CHECKSUM_UNNECESSARY; 1928 skb->ip_summed = CHECKSUM_UNNECESSARY;
1458 } 1929 }
@@ -1509,6 +1980,7 @@ static inline void velocity_iph_realign(struct velocity_info *vptr,
1509 } 1980 }
1510} 1981}
1511 1982
1983
1512/** 1984/**
1513 * velocity_receive_frame - received packet processor 1985 * velocity_receive_frame - received packet processor
1514 * @vptr: velocity we are handling 1986 * @vptr: velocity we are handling
@@ -1517,7 +1989,6 @@ static inline void velocity_iph_realign(struct velocity_info *vptr,
1517 * A packet has arrived. We process the packet and if appropriate 1989 * A packet has arrived. We process the packet and if appropriate
1518 * pass the frame up the network stack 1990 * pass the frame up the network stack
1519 */ 1991 */
1520
1521static int velocity_receive_frame(struct velocity_info *vptr, int idx) 1992static int velocity_receive_frame(struct velocity_info *vptr, int idx)
1522{ 1993{
1523 void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int); 1994 void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int);
@@ -1579,320 +2050,118 @@ static int velocity_receive_frame(struct velocity_info *vptr, int idx)
1579 return 0; 2050 return 0;
1580} 2051}
1581 2052
1582/**
1583 * velocity_alloc_rx_buf - allocate aligned receive buffer
1584 * @vptr: velocity
1585 * @idx: ring index
1586 *
1587 * Allocate a new full sized buffer for the reception of a frame and
1588 * map it into PCI space for the hardware to use. The hardware
1589 * requires *64* byte alignment of the buffer which makes life
1590 * less fun than would be ideal.
1591 */
1592
1593static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1594{
1595 struct rx_desc *rd = &(vptr->rx.ring[idx]);
1596 struct velocity_rd_info *rd_info = &(vptr->rx.info[idx]);
1597
1598 rd_info->skb = dev_alloc_skb(vptr->rx.buf_sz + 64);
1599 if (rd_info->skb == NULL)
1600 return -ENOMEM;
1601
1602 /*
1603 * Do the gymnastics to get the buffer head for data at
1604 * 64byte alignment.
1605 */
1606 skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
1607 rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data,
1608 vptr->rx.buf_sz, PCI_DMA_FROMDEVICE);
1609
1610 /*
1611 * Fill in the descriptor to match
1612 */
1613
1614 *((u32 *) & (rd->rdesc0)) = 0;
1615 rd->size = cpu_to_le16(vptr->rx.buf_sz) | RX_INTEN;
1616 rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1617 rd->pa_high = 0;
1618 return 0;
1619}
1620 2053
1621/** 2054/**
1622 * tx_srv - transmit interrupt service 2055 * velocity_rx_srv - service RX interrupt
1623 * @vptr; Velocity 2056 * @vptr: velocity
1624 * @status: 2057 * @status: adapter status (unused)
1625 * 2058 *
1626 * Scan the queues looking for transmitted packets that 2059 * Walk the receive ring of the velocity adapter and remove
1627 * we can complete and clean up. Update any statistics as 2060 * any received packets from the receive queue. Hand the ring
1628 * necessary/ 2061 * slots back to the adapter for reuse.
1629 */ 2062 */
1630 2063static int velocity_rx_srv(struct velocity_info *vptr, int status)
1631static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
1632{ 2064{
1633 struct tx_desc *td;
1634 int qnum;
1635 int full = 0;
1636 int idx;
1637 int works = 0;
1638 struct velocity_td_info *tdinfo;
1639 struct net_device_stats *stats = &vptr->dev->stats; 2065 struct net_device_stats *stats = &vptr->dev->stats;
2066 int rd_curr = vptr->rx.curr;
2067 int works = 0;
1640 2068
1641 for (qnum = 0; qnum < vptr->tx.numq; qnum++) { 2069 do {
1642 for (idx = vptr->tx.tail[qnum]; vptr->tx.used[qnum] > 0; 2070 struct rx_desc *rd = vptr->rx.ring + rd_curr;
1643 idx = (idx + 1) % vptr->options.numtx) {
1644 2071
1645 /* 2072 if (!vptr->rx.info[rd_curr].skb)
1646 * Get Tx Descriptor 2073 break;
1647 */
1648 td = &(vptr->tx.rings[qnum][idx]);
1649 tdinfo = &(vptr->tx.infos[qnum][idx]);
1650 2074
1651 if (td->tdesc0.len & OWNED_BY_NIC) 2075 if (rd->rdesc0.len & OWNED_BY_NIC)
1652 break; 2076 break;
1653 2077
1654 if ((works++ > 15)) 2078 rmb();
1655 break;
1656 2079
1657 if (td->tdesc0.TSR & TSR0_TERR) { 2080 /*
1658 stats->tx_errors++; 2081 * Don't drop CE or RL error frame although RXOK is off
1659 stats->tx_dropped++; 2082 */
1660 if (td->tdesc0.TSR & TSR0_CDH) 2083 if (rd->rdesc0.RSR & (RSR_RXOK | RSR_CE | RSR_RL)) {
1661 stats->tx_heartbeat_errors++; 2084 if (velocity_receive_frame(vptr, rd_curr) < 0)
1662 if (td->tdesc0.TSR & TSR0_CRS) 2085 stats->rx_dropped++;
1663 stats->tx_carrier_errors++; 2086 } else {
1664 if (td->tdesc0.TSR & TSR0_ABT) 2087 if (rd->rdesc0.RSR & RSR_CRC)
1665 stats->tx_aborted_errors++; 2088 stats->rx_crc_errors++;
1666 if (td->tdesc0.TSR & TSR0_OWC) 2089 if (rd->rdesc0.RSR & RSR_FAE)
1667 stats->tx_window_errors++; 2090 stats->rx_frame_errors++;
1668 } else {
1669 stats->tx_packets++;
1670 stats->tx_bytes += tdinfo->skb->len;
1671 }
1672 velocity_free_tx_buf(vptr, tdinfo);
1673 vptr->tx.used[qnum]--;
1674 }
1675 vptr->tx.tail[qnum] = idx;
1676 2091
1677 if (AVAIL_TD(vptr, qnum) < 1) { 2092 stats->rx_dropped++;
1678 full = 1;
1679 } 2093 }
1680 }
1681 /*
1682 * Look to see if we should kick the transmit network
1683 * layer for more work.
1684 */
1685 if (netif_queue_stopped(vptr->dev) && (full == 0)
1686 && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1687 netif_wake_queue(vptr->dev);
1688 }
1689 return works;
1690}
1691 2094
1692/** 2095 rd->size |= RX_INTEN;
1693 * velocity_print_link_status - link status reporting
1694 * @vptr: velocity to report on
1695 *
1696 * Turn the link status of the velocity card into a kernel log
1697 * description of the new link state, detailing speed and duplex
1698 * status
1699 */
1700 2096
1701static void velocity_print_link_status(struct velocity_info *vptr) 2097 rd_curr++;
1702{ 2098 if (rd_curr >= vptr->options.numrx)
2099 rd_curr = 0;
2100 } while (++works <= 15);
1703 2101
1704 if (vptr->mii_status & VELOCITY_LINK_FAIL) { 2102 vptr->rx.curr = rd_curr;
1705 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
1706 } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1707 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link auto-negotiation", vptr->dev->name);
1708 2103
1709 if (vptr->mii_status & VELOCITY_SPEED_1000) 2104 if ((works > 0) && (velocity_rx_refill(vptr) > 0))
1710 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps"); 2105 velocity_give_many_rx_descs(vptr);
1711 else if (vptr->mii_status & VELOCITY_SPEED_100)
1712 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1713 else
1714 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1715 2106
1716 if (vptr->mii_status & VELOCITY_DUPLEX_FULL) 2107 VAR_USED(stats);
1717 VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n"); 2108 return works;
1718 else
1719 VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1720 } else {
1721 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
1722 switch (vptr->options.spd_dpx) {
1723 case SPD_DPX_100_HALF:
1724 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1725 break;
1726 case SPD_DPX_100_FULL:
1727 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1728 break;
1729 case SPD_DPX_10_HALF:
1730 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1731 break;
1732 case SPD_DPX_10_FULL:
1733 VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1734 break;
1735 default:
1736 break;
1737 }
1738 }
1739} 2109}
1740 2110
2111
1741/** 2112/**
1742 * velocity_error - handle error from controller 2113 * velocity_intr - interrupt callback
1743 * @vptr: velocity 2114 * @irq: interrupt number
1744 * @status: card status 2115 * @dev_instance: interrupting device
1745 *
1746 * Process an error report from the hardware and attempt to recover
1747 * the card itself. At the moment we cannot recover from some
1748 * theoretically impossible errors but this could be fixed using
1749 * the pci_device_failed logic to bounce the hardware
1750 * 2116 *
2117 * Called whenever an interrupt is generated by the velocity
2118 * adapter IRQ line. We may not be the source of the interrupt
2119 * and need to identify initially if we are, and if not exit as
2120 * efficiently as possible.
1751 */ 2121 */
1752 2122static irqreturn_t velocity_intr(int irq, void *dev_instance)
1753static void velocity_error(struct velocity_info *vptr, int status)
1754{ 2123{
2124 struct net_device *dev = dev_instance;
2125 struct velocity_info *vptr = netdev_priv(dev);
2126 u32 isr_status;
2127 int max_count = 0;
1755 2128
1756 if (status & ISR_TXSTLI) {
1757 struct mac_regs __iomem * regs = vptr->mac_regs;
1758 2129
1759 printk(KERN_ERR "TD structure error TDindex=%hx\n", readw(&regs->TDIdx[0])); 2130 spin_lock(&vptr->lock);
1760 BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR); 2131 isr_status = mac_read_isr(vptr->mac_regs);
1761 writew(TRDCSR_RUN, &regs->TDCSRClr);
1762 netif_stop_queue(vptr->dev);
1763 2132
1764 /* FIXME: port over the pci_device_failed code and use it 2133 /* Not us ? */
1765 here */ 2134 if (isr_status == 0) {
2135 spin_unlock(&vptr->lock);
2136 return IRQ_NONE;
1766 } 2137 }
1767 2138
1768 if (status & ISR_SRCI) { 2139 mac_disable_int(vptr->mac_regs);
1769 struct mac_regs __iomem * regs = vptr->mac_regs;
1770 int linked;
1771
1772 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1773 vptr->mii_status = check_connection_type(regs);
1774
1775 /*
1776 * If it is a 3119, disable frame bursting in
1777 * halfduplex mode and enable it in fullduplex
1778 * mode
1779 */
1780 if (vptr->rev_id < REV_ID_VT3216_A0) {
1781 if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
1782 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
1783 else
1784 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
1785 }
1786 /*
1787 * Only enable CD heart beat counter in 10HD mode
1788 */
1789 if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10)) {
1790 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
1791 } else {
1792 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
1793 }
1794 }
1795 /*
1796 * Get link status from PHYSR0
1797 */
1798 linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
1799
1800 if (linked) {
1801 vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1802 netif_carrier_on(vptr->dev);
1803 } else {
1804 vptr->mii_status |= VELOCITY_LINK_FAIL;
1805 netif_carrier_off(vptr->dev);
1806 }
1807
1808 velocity_print_link_status(vptr);
1809 enable_flow_control_ability(vptr);
1810
1811 /*
1812 * Re-enable auto-polling because SRCI will disable
1813 * auto-polling
1814 */
1815
1816 enable_mii_autopoll(regs);
1817
1818 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1819 netif_stop_queue(vptr->dev);
1820 else
1821 netif_wake_queue(vptr->dev);
1822
1823 };
1824 if (status & ISR_MIBFI)
1825 velocity_update_hw_mibs(vptr);
1826 if (status & ISR_LSTEI)
1827 mac_rx_queue_wake(vptr->mac_regs);
1828}
1829
1830/**
1831 * velocity_free_tx_buf - free transmit buffer
1832 * @vptr: velocity
1833 * @tdinfo: buffer
1834 *
1835 * Release an transmit buffer. If the buffer was preallocated then
1836 * recycle it, if not then unmap the buffer.
1837 */
1838
1839static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
1840{
1841 struct sk_buff *skb = tdinfo->skb;
1842 int i;
1843 int pktlen;
1844 2140
1845 /* 2141 /*
1846 * Don't unmap the pre-allocated tx_bufs 2142 * Keep processing the ISR until we have completed
2143 * processing and the isr_status becomes zero
1847 */ 2144 */
1848 if (tdinfo->skb_dma) {
1849 2145
1850 pktlen = max_t(unsigned int, skb->len, ETH_ZLEN); 2146 while (isr_status != 0) {
1851 for (i = 0; i < tdinfo->nskb_dma; i++) { 2147 mac_write_isr(vptr->mac_regs, isr_status);
1852#ifdef VELOCITY_ZERO_COPY_SUPPORT 2148 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
1853 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], le16_to_cpu(td->tdesc1.len), PCI_DMA_TODEVICE); 2149 velocity_error(vptr, isr_status);
1854#else 2150 if (isr_status & (ISR_PRXI | ISR_PPRXI))
1855 pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], pktlen, PCI_DMA_TODEVICE); 2151 max_count += velocity_rx_srv(vptr, isr_status);
1856#endif 2152 if (isr_status & (ISR_PTXI | ISR_PPTXI))
1857 tdinfo->skb_dma[i] = 0; 2153 max_count += velocity_tx_srv(vptr, isr_status);
2154 isr_status = mac_read_isr(vptr->mac_regs);
2155 if (max_count > vptr->options.int_works) {
2156 printk(KERN_WARNING "%s: excessive work at interrupt.\n",
2157 dev->name);
2158 max_count = 0;
1858 } 2159 }
1859 } 2160 }
1860 dev_kfree_skb_irq(skb); 2161 spin_unlock(&vptr->lock);
1861 tdinfo->skb = NULL; 2162 mac_enable_int(vptr->mac_regs);
1862} 2163 return IRQ_HANDLED;
1863
1864static int velocity_init_rings(struct velocity_info *vptr, int mtu)
1865{
1866 int ret;
1867
1868 velocity_set_rxbufsize(vptr, mtu);
1869
1870 ret = velocity_init_dma_rings(vptr);
1871 if (ret < 0)
1872 goto out;
1873
1874 ret = velocity_init_rd_ring(vptr);
1875 if (ret < 0)
1876 goto err_free_dma_rings_0;
1877
1878 ret = velocity_init_td_ring(vptr);
1879 if (ret < 0)
1880 goto err_free_rd_ring_1;
1881out:
1882 return ret;
1883
1884err_free_rd_ring_1:
1885 velocity_free_rd_ring(vptr);
1886err_free_dma_rings_0:
1887 velocity_free_dma_rings(vptr);
1888 goto out;
1889}
1890 2164
1891static void velocity_free_rings(struct velocity_info *vptr)
1892{
1893 velocity_free_td_ring(vptr);
1894 velocity_free_rd_ring(vptr);
1895 velocity_free_dma_rings(vptr);
1896} 2165}
1897 2166
1898/** 2167/**
@@ -1905,7 +2174,6 @@ static void velocity_free_rings(struct velocity_info *vptr)
1905 * All the ring allocation and set up is done on open for this 2174 * All the ring allocation and set up is done on open for this
1906 * adapter to minimise memory usage when inactive 2175 * adapter to minimise memory usage when inactive
1907 */ 2176 */
1908
1909static int velocity_open(struct net_device *dev) 2177static int velocity_open(struct net_device *dev)
1910{ 2178{
1911 struct velocity_info *vptr = netdev_priv(dev); 2179 struct velocity_info *vptr = netdev_priv(dev);
@@ -1939,6 +2207,24 @@ out:
1939} 2207}
1940 2208
1941/** 2209/**
2210 * velocity_shutdown - shut down the chip
2211 * @vptr: velocity to deactivate
2212 *
2213 * Shuts down the internal operations of the velocity and
2214 * disables interrupts, autopolling, transmit and receive
2215 */
2216static void velocity_shutdown(struct velocity_info *vptr)
2217{
2218 struct mac_regs __iomem *regs = vptr->mac_regs;
2219 mac_disable_int(regs);
2220 writel(CR0_STOP, &regs->CR0Set);
2221 writew(0xFFFF, &regs->TDCSRClr);
2222 writeb(0xFF, &regs->RDCSRClr);
2223 safe_disable_mii_autopoll(regs);
2224 mac_clear_isr(regs);
2225}
2226
2227/**
1942 * velocity_change_mtu - MTU change callback 2228 * velocity_change_mtu - MTU change callback
1943 * @dev: network device 2229 * @dev: network device
1944 * @new_mtu: desired MTU 2230 * @new_mtu: desired MTU
@@ -1947,7 +2233,6 @@ out:
1947 * this interface. It gets called on a change by the network layer. 2233 * this interface. It gets called on a change by the network layer.
1948 * Return zero for success or negative posix error code. 2234 * Return zero for success or negative posix error code.
1949 */ 2235 */
1950
1951static int velocity_change_mtu(struct net_device *dev, int new_mtu) 2236static int velocity_change_mtu(struct net_device *dev, int new_mtu)
1952{ 2237{
1953 struct velocity_info *vptr = netdev_priv(dev); 2238 struct velocity_info *vptr = netdev_priv(dev);
@@ -2021,22 +2306,127 @@ out_0:
2021} 2306}
2022 2307
2023/** 2308/**
2024 * velocity_shutdown - shut down the chip 2309 * velocity_mii_ioctl - MII ioctl handler
2025 * @vptr: velocity to deactivate 2310 * @dev: network device
2311 * @ifr: the ifreq block for the ioctl
2312 * @cmd: the command
2026 * 2313 *
2027 * Shuts down the internal operations of the velocity and 2314 * Process MII requests made via ioctl from the network layer. These
2028 * disables interrupts, autopolling, transmit and receive 2315 * are used by tools like kudzu to interrogate the link state of the
2316 * hardware
2029 */ 2317 */
2318static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2319{
2320 struct velocity_info *vptr = netdev_priv(dev);
2321 struct mac_regs __iomem *regs = vptr->mac_regs;
2322 unsigned long flags;
2323 struct mii_ioctl_data *miidata = if_mii(ifr);
2324 int err;
2030 2325
2031static void velocity_shutdown(struct velocity_info *vptr) 2326 switch (cmd) {
2327 case SIOCGMIIPHY:
2328 miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
2329 break;
2330 case SIOCGMIIREG:
2331 if (!capable(CAP_NET_ADMIN))
2332 return -EPERM;
2333 if (velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
2334 return -ETIMEDOUT;
2335 break;
2336 case SIOCSMIIREG:
2337 if (!capable(CAP_NET_ADMIN))
2338 return -EPERM;
2339 spin_lock_irqsave(&vptr->lock, flags);
2340 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
2341 spin_unlock_irqrestore(&vptr->lock, flags);
2342 check_connection_type(vptr->mac_regs);
2343 if (err)
2344 return err;
2345 break;
2346 default:
2347 return -EOPNOTSUPP;
2348 }
2349 return 0;
2350}
2351
2352
2353/**
2354 * velocity_ioctl - ioctl entry point
2355 * @dev: network device
2356 * @rq: interface request ioctl
2357 * @cmd: command code
2358 *
2359 * Called when the user issues an ioctl request to the network
2360 * device in question. The velocity interface supports MII.
2361 */
2362static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2032{ 2363{
2033 struct mac_regs __iomem * regs = vptr->mac_regs; 2364 struct velocity_info *vptr = netdev_priv(dev);
2034 mac_disable_int(regs); 2365 int ret;
2035 writel(CR0_STOP, &regs->CR0Set); 2366
2036 writew(0xFFFF, &regs->TDCSRClr); 2367 /* If we are asked for information and the device is power
2037 writeb(0xFF, &regs->RDCSRClr); 2368 saving then we need to bring the device back up to talk to it */
2038 safe_disable_mii_autopoll(regs); 2369
2039 mac_clear_isr(regs); 2370 if (!netif_running(dev))
2371 pci_set_power_state(vptr->pdev, PCI_D0);
2372
2373 switch (cmd) {
2374 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2375 case SIOCGMIIREG: /* Read MII PHY register. */
2376 case SIOCSMIIREG: /* Write to MII PHY register. */
2377 ret = velocity_mii_ioctl(dev, rq, cmd);
2378 break;
2379
2380 default:
2381 ret = -EOPNOTSUPP;
2382 }
2383 if (!netif_running(dev))
2384 pci_set_power_state(vptr->pdev, PCI_D3hot);
2385
2386
2387 return ret;
2388}
2389
2390/**
2391 * velocity_get_status - statistics callback
2392 * @dev: network device
2393 *
2394 * Callback from the network layer to allow driver statistics
2395 * to be resynchronized with hardware collected state. In the
2396 * case of the velocity we need to pull the MIB counters from
2397 * the hardware into the counters before letting the network
2398 * layer display them.
2399 */
2400static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2401{
2402 struct velocity_info *vptr = netdev_priv(dev);
2403
2404 /* If the hardware is down, don't touch MII */
2405 if (!netif_running(dev))
2406 return &dev->stats;
2407
2408 spin_lock_irq(&vptr->lock);
2409 velocity_update_hw_mibs(vptr);
2410 spin_unlock_irq(&vptr->lock);
2411
2412 dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2413 dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2414 dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2415
2416// unsigned long rx_dropped; /* no space in linux buffers */
2417 dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2418 /* detailed rx_errors: */
2419// unsigned long rx_length_errors;
2420// unsigned long rx_over_errors; /* receiver ring buff overflow */
2421 dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2422// unsigned long rx_frame_errors; /* recv'd frame alignment error */
2423// unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2424// unsigned long rx_missed_errors; /* receiver missed packet */
2425
2426 /* detailed tx_errors */
2427// unsigned long tx_fifo_errors;
2428
2429 return &dev->stats;
2040} 2430}
2041 2431
2042/** 2432/**
@@ -2046,7 +2436,6 @@ static void velocity_shutdown(struct velocity_info *vptr)
2046 * Callback from the network layer when the velocity is being 2436 * Callback from the network layer when the velocity is being
2047 * deactivated by the network layer 2437 * deactivated by the network layer
2048 */ 2438 */
2049
2050static int velocity_close(struct net_device *dev) 2439static int velocity_close(struct net_device *dev)
2051{ 2440{
2052 struct velocity_info *vptr = netdev_priv(dev); 2441 struct velocity_info *vptr = netdev_priv(dev);
@@ -2076,7 +2465,6 @@ static int velocity_close(struct net_device *dev)
2076 * Called by the networ layer to request a packet is queued to 2465 * Called by the networ layer to request a packet is queued to
2077 * the velocity. Returns zero on success. 2466 * the velocity. Returns zero on success.
2078 */ 2467 */
2079
2080static int velocity_xmit(struct sk_buff *skb, struct net_device *dev) 2468static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
2081{ 2469{
2082 struct velocity_info *vptr = netdev_priv(dev); 2470 struct velocity_info *vptr = netdev_priv(dev);
@@ -2088,20 +2476,12 @@ static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
2088 __le16 len; 2476 __le16 len;
2089 int index; 2477 int index;
2090 2478
2091
2092 if (skb_padto(skb, ETH_ZLEN)) 2479 if (skb_padto(skb, ETH_ZLEN))
2093 goto out; 2480 goto out;
2094 pktlen = max_t(unsigned int, skb->len, ETH_ZLEN); 2481 pktlen = max_t(unsigned int, skb->len, ETH_ZLEN);
2095 2482
2096 len = cpu_to_le16(pktlen); 2483 len = cpu_to_le16(pktlen);
2097 2484
2098#ifdef VELOCITY_ZERO_COPY_SUPPORT
2099 if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
2100 kfree_skb(skb);
2101 return 0;
2102 }
2103#endif
2104
2105 spin_lock_irqsave(&vptr->lock, flags); 2485 spin_lock_irqsave(&vptr->lock, flags);
2106 2486
2107 index = vptr->tx.curr[qnum]; 2487 index = vptr->tx.curr[qnum];
@@ -2111,59 +2491,18 @@ static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
2111 td_ptr->tdesc1.TCR = TCR0_TIC; 2491 td_ptr->tdesc1.TCR = TCR0_TIC;
2112 td_ptr->td_buf[0].size &= ~TD_QUEUE; 2492 td_ptr->td_buf[0].size &= ~TD_QUEUE;
2113 2493
2114#ifdef VELOCITY_ZERO_COPY_SUPPORT 2494 /*
2115 if (skb_shinfo(skb)->nr_frags > 0) { 2495 * Map the linear network buffer into PCI space and
2116 int nfrags = skb_shinfo(skb)->nr_frags; 2496 * add it to the transmit ring.
2117 tdinfo->skb = skb; 2497 */
2118 if (nfrags > 6) { 2498 tdinfo->skb = skb;
2119 skb_copy_from_linear_data(skb, tdinfo->buf, skb->len); 2499 tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
2120 tdinfo->skb_dma[0] = tdinfo->buf_dma; 2500 td_ptr->tdesc0.len = len;
2121 td_ptr->tdesc0.len = len; 2501 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
2122 td_ptr->tx.buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]); 2502 td_ptr->td_buf[0].pa_high = 0;
2123 td_ptr->tx.buf[0].pa_high = 0; 2503 td_ptr->td_buf[0].size = len;
2124 td_ptr->tx.buf[0].size = len; /* queue is 0 anyway */ 2504 tdinfo->nskb_dma = 1;
2125 tdinfo->nskb_dma = 1;
2126 } else {
2127 int i = 0;
2128 tdinfo->nskb_dma = 0;
2129 tdinfo->skb_dma[i] = pci_map_single(vptr->pdev, skb->data,
2130 skb_headlen(skb), PCI_DMA_TODEVICE);
2131
2132 td_ptr->tdesc0.len = len;
2133
2134 /* FIXME: support 48bit DMA later */
2135 td_ptr->tx.buf[i].pa_low = cpu_to_le32(tdinfo->skb_dma);
2136 td_ptr->tx.buf[i].pa_high = 0;
2137 td_ptr->tx.buf[i].size = cpu_to_le16(skb_headlen(skb));
2138
2139 for (i = 0; i < nfrags; i++) {
2140 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2141 void *addr = (void *)page_address(frag->page) + frag->page_offset;
2142
2143 tdinfo->skb_dma[i + 1] = pci_map_single(vptr->pdev, addr, frag->size, PCI_DMA_TODEVICE);
2144
2145 td_ptr->tx.buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
2146 td_ptr->tx.buf[i + 1].pa_high = 0;
2147 td_ptr->tx.buf[i + 1].size = cpu_to_le16(frag->size);
2148 }
2149 tdinfo->nskb_dma = i - 1;
2150 }
2151 2505
2152 } else
2153#endif
2154 {
2155 /*
2156 * Map the linear network buffer into PCI space and
2157 * add it to the transmit ring.
2158 */
2159 tdinfo->skb = skb;
2160 tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
2161 td_ptr->tdesc0.len = len;
2162 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
2163 td_ptr->td_buf[0].pa_high = 0;
2164 td_ptr->td_buf[0].size = len;
2165 tdinfo->nskb_dma = 1;
2166 }
2167 td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16; 2506 td_ptr->tdesc1.cmd = TCPLS_NORMAL + (tdinfo->nskb_dma + 1) * 16;
2168 2507
2169 if (vptr->vlgrp && vlan_tx_tag_present(skb)) { 2508 if (vptr->vlgrp && vlan_tx_tag_present(skb)) {
@@ -2206,782 +2545,533 @@ out:
2206 return NETDEV_TX_OK; 2545 return NETDEV_TX_OK;
2207} 2546}
2208 2547
2548
2549static const struct net_device_ops velocity_netdev_ops = {
2550 .ndo_open = velocity_open,
2551 .ndo_stop = velocity_close,
2552 .ndo_start_xmit = velocity_xmit,
2553 .ndo_get_stats = velocity_get_stats,
2554 .ndo_validate_addr = eth_validate_addr,
2555 .ndo_set_mac_address = eth_mac_addr,
2556 .ndo_set_multicast_list = velocity_set_multi,
2557 .ndo_change_mtu = velocity_change_mtu,
2558 .ndo_do_ioctl = velocity_ioctl,
2559 .ndo_vlan_rx_add_vid = velocity_vlan_rx_add_vid,
2560 .ndo_vlan_rx_kill_vid = velocity_vlan_rx_kill_vid,
2561 .ndo_vlan_rx_register = velocity_vlan_rx_register,
2562};
2563
2209/** 2564/**
2210 * velocity_intr - interrupt callback 2565 * velocity_init_info - init private data
2211 * @irq: interrupt number 2566 * @pdev: PCI device
2212 * @dev_instance: interrupting device 2567 * @vptr: Velocity info
2568 * @info: Board type
2213 * 2569 *
2214 * Called whenever an interrupt is generated by the velocity 2570 * Set up the initial velocity_info struct for the device that has been
2215 * adapter IRQ line. We may not be the source of the interrupt 2571 * discovered.
2216 * and need to identify initially if we are, and if not exit as
2217 * efficiently as possible.
2218 */ 2572 */
2219 2573static void __devinit velocity_init_info(struct pci_dev *pdev,
2220static irqreturn_t velocity_intr(int irq, void *dev_instance) 2574 struct velocity_info *vptr,
2575 const struct velocity_info_tbl *info)
2221{ 2576{
2222 struct net_device *dev = dev_instance; 2577 memset(vptr, 0, sizeof(struct velocity_info));
2223 struct velocity_info *vptr = netdev_priv(dev);
2224 u32 isr_status;
2225 int max_count = 0;
2226
2227
2228 spin_lock(&vptr->lock);
2229 isr_status = mac_read_isr(vptr->mac_regs);
2230
2231 /* Not us ? */
2232 if (isr_status == 0) {
2233 spin_unlock(&vptr->lock);
2234 return IRQ_NONE;
2235 }
2236
2237 mac_disable_int(vptr->mac_regs);
2238
2239 /*
2240 * Keep processing the ISR until we have completed
2241 * processing and the isr_status becomes zero
2242 */
2243
2244 while (isr_status != 0) {
2245 mac_write_isr(vptr->mac_regs, isr_status);
2246 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2247 velocity_error(vptr, isr_status);
2248 if (isr_status & (ISR_PRXI | ISR_PPRXI))
2249 max_count += velocity_rx_srv(vptr, isr_status);
2250 if (isr_status & (ISR_PTXI | ISR_PPTXI))
2251 max_count += velocity_tx_srv(vptr, isr_status);
2252 isr_status = mac_read_isr(vptr->mac_regs);
2253 if (max_count > vptr->options.int_works)
2254 {
2255 printk(KERN_WARNING "%s: excessive work at interrupt.\n",
2256 dev->name);
2257 max_count = 0;
2258 }
2259 }
2260 spin_unlock(&vptr->lock);
2261 mac_enable_int(vptr->mac_regs);
2262 return IRQ_HANDLED;
2263 2578
2579 vptr->pdev = pdev;
2580 vptr->chip_id = info->chip_id;
2581 vptr->tx.numq = info->txqueue;
2582 vptr->multicast_limit = MCAM_SIZE;
2583 spin_lock_init(&vptr->lock);
2584 INIT_LIST_HEAD(&vptr->list);
2264} 2585}
2265 2586
2266
2267/** 2587/**
2268 * velocity_set_multi - filter list change callback 2588 * velocity_get_pci_info - retrieve PCI info for device
2269 * @dev: network device 2589 * @vptr: velocity device
2590 * @pdev: PCI device it matches
2270 * 2591 *
2271 * Called by the network layer when the filter lists need to change 2592 * Retrieve the PCI configuration space data that interests us from
2272 * for a velocity adapter. Reload the CAMs with the new address 2593 * the kernel PCI layer
2273 * filter ruleset.
2274 */ 2594 */
2275 2595static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
2276static void velocity_set_multi(struct net_device *dev)
2277{ 2596{
2278 struct velocity_info *vptr = netdev_priv(dev); 2597 vptr->rev_id = pdev->revision;
2279 struct mac_regs __iomem * regs = vptr->mac_regs;
2280 u8 rx_mode;
2281 int i;
2282 struct dev_mc_list *mclist;
2283 2598
2284 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */ 2599 pci_set_master(pdev);
2285 writel(0xffffffff, &regs->MARCAM[0]);
2286 writel(0xffffffff, &regs->MARCAM[4]);
2287 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
2288 } else if ((dev->mc_count > vptr->multicast_limit)
2289 || (dev->flags & IFF_ALLMULTI)) {
2290 writel(0xffffffff, &regs->MARCAM[0]);
2291 writel(0xffffffff, &regs->MARCAM[4]);
2292 rx_mode = (RCR_AM | RCR_AB);
2293 } else {
2294 int offset = MCAM_SIZE - vptr->multicast_limit;
2295 mac_get_cam_mask(regs, vptr->mCAMmask);
2296 2600
2297 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) { 2601 vptr->ioaddr = pci_resource_start(pdev, 0);
2298 mac_set_cam(regs, i + offset, mclist->dmi_addr); 2602 vptr->memaddr = pci_resource_start(pdev, 1);
2299 vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
2300 }
2301 2603
2302 mac_set_cam_mask(regs, vptr->mCAMmask); 2604 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
2303 rx_mode = RCR_AM | RCR_AB | RCR_AP; 2605 dev_err(&pdev->dev,
2606 "region #0 is not an I/O resource, aborting.\n");
2607 return -EINVAL;
2304 } 2608 }
2305 if (dev->mtu > 1500)
2306 rx_mode |= RCR_AL;
2307 2609
2308 BYTE_REG_BITS_ON(rx_mode, &regs->RCR); 2610 if ((pci_resource_flags(pdev, 1) & IORESOURCE_IO)) {
2611 dev_err(&pdev->dev,
2612 "region #1 is an I/O resource, aborting.\n");
2613 return -EINVAL;
2614 }
2309 2615
2616 if (pci_resource_len(pdev, 1) < VELOCITY_IO_SIZE) {
2617 dev_err(&pdev->dev, "region #1 is too small.\n");
2618 return -EINVAL;
2619 }
2620 vptr->pdev = pdev;
2621
2622 return 0;
2310} 2623}
2311 2624
2312/** 2625/**
2313 * velocity_get_status - statistics callback 2626 * velocity_print_info - per driver data
2314 * @dev: network device 2627 * @vptr: velocity
2315 * 2628 *
2316 * Callback from the network layer to allow driver statistics 2629 * Print per driver data as the kernel driver finds Velocity
2317 * to be resynchronized with hardware collected state. In the 2630 * hardware
2318 * case of the velocity we need to pull the MIB counters from
2319 * the hardware into the counters before letting the network
2320 * layer display them.
2321 */ 2631 */
2322 2632static void __devinit velocity_print_info(struct velocity_info *vptr)
2323static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2324{ 2633{
2325 struct velocity_info *vptr = netdev_priv(dev); 2634 struct net_device *dev = vptr->dev;
2326
2327 /* If the hardware is down, don't touch MII */
2328 if(!netif_running(dev))
2329 return &dev->stats;
2330
2331 spin_lock_irq(&vptr->lock);
2332 velocity_update_hw_mibs(vptr);
2333 spin_unlock_irq(&vptr->lock);
2334
2335 dev->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2336 dev->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2337 dev->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2338
2339// unsigned long rx_dropped; /* no space in linux buffers */
2340 dev->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2341 /* detailed rx_errors: */
2342// unsigned long rx_length_errors;
2343// unsigned long rx_over_errors; /* receiver ring buff overflow */
2344 dev->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2345// unsigned long rx_frame_errors; /* recv'd frame alignment error */
2346// unsigned long rx_fifo_errors; /* recv'r fifo overrun */
2347// unsigned long rx_missed_errors; /* receiver missed packet */
2348
2349 /* detailed tx_errors */
2350// unsigned long tx_fifo_errors;
2351 2635
2352 return &dev->stats; 2636 printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
2637 printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
2638 dev->name,
2639 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
2640 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
2353} 2641}
2354 2642
2355 2643static u32 velocity_get_link(struct net_device *dev)
2356/**
2357 * velocity_ioctl - ioctl entry point
2358 * @dev: network device
2359 * @rq: interface request ioctl
2360 * @cmd: command code
2361 *
2362 * Called when the user issues an ioctl request to the network
2363 * device in question. The velocity interface supports MII.
2364 */
2365
2366static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2367{ 2644{
2368 struct velocity_info *vptr = netdev_priv(dev); 2645 struct velocity_info *vptr = netdev_priv(dev);
2369 int ret; 2646 struct mac_regs __iomem *regs = vptr->mac_regs;
2370 2647 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 1 : 0;
2371 /* If we are asked for information and the device is power
2372 saving then we need to bring the device back up to talk to it */
2373
2374 if (!netif_running(dev))
2375 pci_set_power_state(vptr->pdev, PCI_D0);
2376
2377 switch (cmd) {
2378 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2379 case SIOCGMIIREG: /* Read MII PHY register. */
2380 case SIOCSMIIREG: /* Write to MII PHY register. */
2381 ret = velocity_mii_ioctl(dev, rq, cmd);
2382 break;
2383
2384 default:
2385 ret = -EOPNOTSUPP;
2386 }
2387 if (!netif_running(dev))
2388 pci_set_power_state(vptr->pdev, PCI_D3hot);
2389
2390
2391 return ret;
2392} 2648}
2393 2649
2394/*
2395 * Definition for our device driver. The PCI layer interface
2396 * uses this to handle all our card discover and plugging
2397 */
2398
2399static struct pci_driver velocity_driver = {
2400 .name = VELOCITY_NAME,
2401 .id_table = velocity_id_table,
2402 .probe = velocity_found1,
2403 .remove = __devexit_p(velocity_remove1),
2404#ifdef CONFIG_PM
2405 .suspend = velocity_suspend,
2406 .resume = velocity_resume,
2407#endif
2408};
2409 2650
2410/** 2651/**
2411 * velocity_init_module - load time function 2652 * velocity_found1 - set up discovered velocity card
2653 * @pdev: PCI device
2654 * @ent: PCI device table entry that matched
2412 * 2655 *
2413 * Called when the velocity module is loaded. The PCI driver 2656 * Configure a discovered adapter from scratch. Return a negative
2414 * is registered with the PCI layer, and in turn will call 2657 * errno error code on failure paths.
2415 * the probe functions for each velocity adapter installed
2416 * in the system.
2417 */ 2658 */
2418 2659static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
2419static int __init velocity_init_module(void)
2420{ 2660{
2421 int ret; 2661 static int first = 1;
2662 struct net_device *dev;
2663 int i;
2664 const char *drv_string;
2665 const struct velocity_info_tbl *info = &chip_info_table[ent->driver_data];
2666 struct velocity_info *vptr;
2667 struct mac_regs __iomem *regs;
2668 int ret = -ENOMEM;
2422 2669
2423 velocity_register_notifier(); 2670 /* FIXME: this driver, like almost all other ethernet drivers,
2424 ret = pci_register_driver(&velocity_driver); 2671 * can support more than MAX_UNITS.
2425 if (ret < 0) 2672 */
2426 velocity_unregister_notifier(); 2673 if (velocity_nics >= MAX_UNITS) {
2427 return ret; 2674 dev_notice(&pdev->dev, "already found %d NICs.\n",
2428} 2675 velocity_nics);
2676 return -ENODEV;
2677 }
2429 2678
2430/** 2679 dev = alloc_etherdev(sizeof(struct velocity_info));
2431 * velocity_cleanup - module unload 2680 if (!dev) {
2432 * 2681 dev_err(&pdev->dev, "allocate net device failed.\n");
2433 * When the velocity hardware is unloaded this function is called. 2682 goto out;
2434 * It will clean up the notifiers and the unregister the PCI 2683 }
2435 * driver interface for this hardware. This in turn cleans up
2436 * all discovered interfaces before returning from the function
2437 */
2438 2684
2439static void __exit velocity_cleanup_module(void) 2685 /* Chain it all together */
2440{
2441 velocity_unregister_notifier();
2442 pci_unregister_driver(&velocity_driver);
2443}
2444 2686
2445module_init(velocity_init_module); 2687 SET_NETDEV_DEV(dev, &pdev->dev);
2446module_exit(velocity_cleanup_module); 2688 vptr = netdev_priv(dev);
2447 2689
2448 2690
2449/* 2691 if (first) {
2450 * MII access , media link mode setting functions 2692 printk(KERN_INFO "%s Ver. %s\n",
2451 */ 2693 VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
2694 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
2695 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
2696 first = 0;
2697 }
2452 2698
2699 velocity_init_info(pdev, vptr, info);
2453 2700
2454/** 2701 vptr->dev = dev;
2455 * mii_init - set up MII
2456 * @vptr: velocity adapter
2457 * @mii_status: links tatus
2458 *
2459 * Set up the PHY for the current link state.
2460 */
2461 2702
2462static void mii_init(struct velocity_info *vptr, u32 mii_status) 2703 dev->irq = pdev->irq;
2463{
2464 u16 BMCR;
2465 2704
2466 switch (PHYID_GET_PHY_ID(vptr->phy_id)) { 2705 ret = pci_enable_device(pdev);
2467 case PHYID_CICADA_CS8201: 2706 if (ret < 0)
2468 /* 2707 goto err_free_dev;
2469 * Reset to hardware default
2470 */
2471 MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2472 /*
2473 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2474 * off it in NWay-forced half mode for NWay-forced v.s.
2475 * legacy-forced issue.
2476 */
2477 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2478 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2479 else
2480 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2481 /*
2482 * Turn on Link/Activity LED enable bit for CIS8201
2483 */
2484 MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
2485 break;
2486 case PHYID_VT3216_32BIT:
2487 case PHYID_VT3216_64BIT:
2488 /*
2489 * Reset to hardware default
2490 */
2491 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2492 /*
2493 * Turn on ECHODIS bit in NWay-forced full mode and turn it
2494 * off it in NWay-forced half mode for NWay-forced v.s.
2495 * legacy-forced issue
2496 */
2497 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2498 MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2499 else
2500 MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2501 break;
2502 2708
2503 case PHYID_MARVELL_1000: 2709 ret = velocity_get_pci_info(vptr, pdev);
2504 case PHYID_MARVELL_1000S: 2710 if (ret < 0) {
2505 /* 2711 /* error message already printed */
2506 * Assert CRS on Transmit 2712 goto err_disable;
2507 */
2508 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
2509 /*
2510 * Reset to hardware default
2511 */
2512 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2513 break;
2514 default:
2515 ;
2516 }
2517 velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
2518 if (BMCR & BMCR_ISO) {
2519 BMCR &= ~BMCR_ISO;
2520 velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
2521 } 2713 }
2522}
2523 2714
2524/** 2715 ret = pci_request_regions(pdev, VELOCITY_NAME);
2525 * safe_disable_mii_autopoll - autopoll off 2716 if (ret < 0) {
2526 * @regs: velocity registers 2717 dev_err(&pdev->dev, "No PCI resources.\n");
2527 * 2718 goto err_disable;
2528 * Turn off the autopoll and wait for it to disable on the chip
2529 */
2530
2531static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs)
2532{
2533 u16 ww;
2534
2535 /* turn off MAUTO */
2536 writeb(0, &regs->MIICR);
2537 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2538 udelay(1);
2539 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2540 break;
2541 } 2719 }
2542}
2543
2544/**
2545 * enable_mii_autopoll - turn on autopolling
2546 * @regs: velocity registers
2547 *
2548 * Enable the MII link status autopoll feature on the Velocity
2549 * hardware. Wait for it to enable.
2550 */
2551 2720
2552static void enable_mii_autopoll(struct mac_regs __iomem * regs) 2721 regs = ioremap(vptr->memaddr, VELOCITY_IO_SIZE);
2553{ 2722 if (regs == NULL) {
2554 int ii; 2723 ret = -EIO;
2724 goto err_release_res;
2725 }
2555 2726
2556 writeb(0, &(regs->MIICR)); 2727 vptr->mac_regs = regs;
2557 writeb(MIIADR_SWMPL, &regs->MIIADR);
2558 2728
2559 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) { 2729 mac_wol_reset(regs);
2560 udelay(1);
2561 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2562 break;
2563 }
2564 2730
2565 writeb(MIICR_MAUTO, &regs->MIICR); 2731 dev->base_addr = vptr->ioaddr;
2566 2732
2567 for (ii = 0; ii < W_MAX_TIMEOUT; ii++) { 2733 for (i = 0; i < 6; i++)
2568 udelay(1); 2734 dev->dev_addr[i] = readb(&regs->PAR[i]);
2569 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2570 break;
2571 }
2572 2735
2573}
2574 2736
2575/** 2737 drv_string = dev_driver_string(&pdev->dev);
2576 * velocity_mii_read - read MII data
2577 * @regs: velocity registers
2578 * @index: MII register index
2579 * @data: buffer for received data
2580 *
2581 * Perform a single read of an MII 16bit register. Returns zero
2582 * on success or -ETIMEDOUT if the PHY did not respond.
2583 */
2584 2738
2585static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data) 2739 velocity_get_options(&vptr->options, velocity_nics, drv_string);
2586{
2587 u16 ww;
2588 2740
2589 /* 2741 /*
2590 * Disable MIICR_MAUTO, so that mii addr can be set normally 2742 * Mask out the options cannot be set to the chip
2591 */ 2743 */
2592 safe_disable_mii_autopoll(regs);
2593
2594 writeb(index, &regs->MIIADR);
2595 2744
2596 BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR); 2745 vptr->options.flags &= info->flags;
2597 2746
2598 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) { 2747 /*
2599 if (!(readb(&regs->MIICR) & MIICR_RCMD)) 2748 * Enable the chip specified capbilities
2600 break; 2749 */
2601 }
2602 2750
2603 *data = readw(&regs->MIIDATA); 2751 vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
2604 2752
2605 enable_mii_autopoll(regs); 2753 vptr->wol_opts = vptr->options.wol_opts;
2606 if (ww == W_MAX_TIMEOUT) 2754 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2607 return -ETIMEDOUT;
2608 return 0;
2609}
2610 2755
2611/** 2756 vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
2612 * velocity_mii_write - write MII data
2613 * @regs: velocity registers
2614 * @index: MII register index
2615 * @data: 16bit data for the MII register
2616 *
2617 * Perform a single write to an MII 16bit register. Returns zero
2618 * on success or -ETIMEDOUT if the PHY did not respond.
2619 */
2620 2757
2621static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data) 2758 dev->irq = pdev->irq;
2622{ 2759 dev->netdev_ops = &velocity_netdev_ops;
2623 u16 ww; 2760 dev->ethtool_ops = &velocity_ethtool_ops;
2624 2761
2625 /* 2762 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_FILTER |
2626 * Disable MIICR_MAUTO, so that mii addr can be set normally 2763 NETIF_F_HW_VLAN_RX;
2627 */
2628 safe_disable_mii_autopoll(regs);
2629 2764
2630 /* MII reg offset */ 2765 if (vptr->flags & VELOCITY_FLAGS_TX_CSUM)
2631 writeb(mii_addr, &regs->MIIADR); 2766 dev->features |= NETIF_F_IP_CSUM;
2632 /* set MII data */
2633 writew(data, &regs->MIIDATA);
2634 2767
2635 /* turn on MIICR_WCMD */ 2768 ret = register_netdev(dev);
2636 BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR); 2769 if (ret < 0)
2770 goto err_iounmap;
2637 2771
2638 /* W_MAX_TIMEOUT is the timeout period */ 2772 if (!velocity_get_link(dev)) {
2639 for (ww = 0; ww < W_MAX_TIMEOUT; ww++) { 2773 netif_carrier_off(dev);
2640 udelay(5); 2774 vptr->mii_status |= VELOCITY_LINK_FAIL;
2641 if (!(readb(&regs->MIICR) & MIICR_WCMD))
2642 break;
2643 } 2775 }
2644 enable_mii_autopoll(regs);
2645 2776
2646 if (ww == W_MAX_TIMEOUT) 2777 velocity_print_info(vptr);
2647 return -ETIMEDOUT; 2778 pci_set_drvdata(pdev, dev);
2648 return 0;
2649}
2650 2779
2651/** 2780 /* and leave the chip powered down */
2652 * velocity_get_opt_media_mode - get media selection
2653 * @vptr: velocity adapter
2654 *
2655 * Get the media mode stored in EEPROM or module options and load
2656 * mii_status accordingly. The requested link state information
2657 * is also returned.
2658 */
2659 2781
2660static u32 velocity_get_opt_media_mode(struct velocity_info *vptr) 2782 pci_set_power_state(pdev, PCI_D3hot);
2661{ 2783#ifdef CONFIG_PM
2662 u32 status = 0; 2784 {
2785 unsigned long flags;
2663 2786
2664 switch (vptr->options.spd_dpx) { 2787 spin_lock_irqsave(&velocity_dev_list_lock, flags);
2665 case SPD_DPX_AUTO: 2788 list_add(&vptr->list, &velocity_dev_list);
2666 status = VELOCITY_AUTONEG_ENABLE; 2789 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
2667 break;
2668 case SPD_DPX_100_FULL:
2669 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
2670 break;
2671 case SPD_DPX_10_FULL:
2672 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
2673 break;
2674 case SPD_DPX_100_HALF:
2675 status = VELOCITY_SPEED_100;
2676 break;
2677 case SPD_DPX_10_HALF:
2678 status = VELOCITY_SPEED_10;
2679 break;
2680 } 2790 }
2681 vptr->mii_status = status; 2791#endif
2682 return status; 2792 velocity_nics++;
2683} 2793out:
2684 2794 return ret;
2685/**
2686 * mii_set_auto_on - autonegotiate on
2687 * @vptr: velocity
2688 *
2689 * Enable autonegotation on this interface
2690 */
2691 2795
2692static void mii_set_auto_on(struct velocity_info *vptr) 2796err_iounmap:
2693{ 2797 iounmap(regs);
2694 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs)) 2798err_release_res:
2695 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs); 2799 pci_release_regions(pdev);
2696 else 2800err_disable:
2697 MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); 2801 pci_disable_device(pdev);
2802err_free_dev:
2803 free_netdev(dev);
2804 goto out;
2698} 2805}
2699 2806
2700 2807
2701/* 2808#ifdef CONFIG_PM
2702static void mii_set_auto_off(struct velocity_info * vptr)
2703{
2704 MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2705}
2706*/
2707
2708/** 2809/**
2709 * set_mii_flow_control - flow control setup 2810 * wol_calc_crc - WOL CRC
2710 * @vptr: velocity interface 2811 * @pattern: data pattern
2812 * @mask_pattern: mask
2711 * 2813 *
2712 * Set up the flow control on this interface according to 2814 * Compute the wake on lan crc hashes for the packet header
2713 * the supplied user/eeprom options. 2815 * we are interested in.
2714 */ 2816 */
2715 2817static u16 wol_calc_crc(int size, u8 *pattern, u8 *mask_pattern)
2716static void set_mii_flow_control(struct velocity_info *vptr)
2717{ 2818{
2718 /*Enable or Disable PAUSE in ANAR */ 2819 u16 crc = 0xFFFF;
2719 switch (vptr->options.flow_cntl) { 2820 u8 mask;
2720 case FLOW_CNTL_TX: 2821 int i, j;
2721 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2722 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2723 break;
2724 2822
2725 case FLOW_CNTL_RX: 2823 for (i = 0; i < size; i++) {
2726 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs); 2824 mask = mask_pattern[i];
2727 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2728 break;
2729 2825
2730 case FLOW_CNTL_TX_RX: 2826 /* Skip this loop if the mask equals to zero */
2731 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs); 2827 if (mask == 0x00)
2732 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs); 2828 continue;
2733 break;
2734 2829
2735 case FLOW_CNTL_DISABLE: 2830 for (j = 0; j < 8; j++) {
2736 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs); 2831 if ((mask & 0x01) == 0) {
2737 MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs); 2832 mask >>= 1;
2738 break; 2833 continue;
2739 default: 2834 }
2740 break; 2835 mask >>= 1;
2836 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
2837 }
2741 } 2838 }
2839 /* Finally, invert the result once to get the correct data */
2840 crc = ~crc;
2841 return bitrev32(crc) >> 16;
2742} 2842}
2743 2843
2744/** 2844/**
2745 * velocity_set_media_mode - set media mode 2845 * velocity_set_wol - set up for wake on lan
2746 * @mii_status: old MII link state 2846 * @vptr: velocity to set WOL status on
2747 * 2847 *
2748 * Check the media link state and configure the flow control 2848 * Set a card up for wake on lan either by unicast or by
2749 * PHY and also velocity hardware setup accordingly. In particular 2849 * ARP packet.
2750 * we need to set up CD polling and frame bursting. 2850 *
2851 * FIXME: check static buffer is safe here
2751 */ 2852 */
2752 2853static int velocity_set_wol(struct velocity_info *vptr)
2753static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
2754{ 2854{
2755 u32 curr_status; 2855 struct mac_regs __iomem *regs = vptr->mac_regs;
2756 struct mac_regs __iomem * regs = vptr->mac_regs; 2856 static u8 buf[256];
2857 int i;
2757 2858
2758 vptr->mii_status = mii_check_media_mode(vptr->mac_regs); 2859 static u32 mask_pattern[2][4] = {
2759 curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL); 2860 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
2861 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
2862 };
2760 2863
2761 /* Set mii link status */ 2864 writew(0xFFFF, &regs->WOLCRClr);
2762 set_mii_flow_control(vptr); 2865 writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
2866 writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
2763 2867
2764 /* 2868 /*
2765 Check if new status is consisent with current status 2869 if (vptr->wol_opts & VELOCITY_WOL_PHY)
2766 if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE) 2870 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
2767 || (mii_status==curr_status)) {
2768 vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
2769 vptr->mii_status=check_connection_type(vptr->mac_regs);
2770 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
2771 return 0;
2772 }
2773 */ 2871 */
2774 2872
2775 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201) { 2873 if (vptr->wol_opts & VELOCITY_WOL_UCAST)
2776 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs); 2874 writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
2777 }
2778 2875
2779 /* 2876 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
2780 * If connection type is AUTO 2877 struct arp_packet *arp = (struct arp_packet *) buf;
2781 */ 2878 u16 crc;
2782 if (mii_status & VELOCITY_AUTONEG_ENABLE) { 2879 memset(buf, 0, sizeof(struct arp_packet) + 7);
2783 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
2784 /* clear force MAC mode bit */
2785 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
2786 /* set duplex mode of MAC according to duplex mode of MII */
2787 MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
2788 MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2789 MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
2790 2880
2791 /* enable AUTO-NEGO mode */ 2881 for (i = 0; i < 4; i++)
2792 mii_set_auto_on(vptr); 2882 writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
2793 } else {
2794 u16 ANAR;
2795 u8 CHIPGCR;
2796 2883
2797 /* 2884 arp->type = htons(ETH_P_ARP);
2798 * 1. if it's 3119, disable frame bursting in halfduplex mode 2885 arp->ar_op = htons(1);
2799 * and enable it in fullduplex mode
2800 * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
2801 * 3. only enable CD heart beat counter in 10HD mode
2802 */
2803 2886
2804 /* set force MAC mode bit */ 2887 memcpy(arp->ar_tip, vptr->ip_addr, 4);
2805 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
2806 2888
2807 CHIPGCR = readb(&regs->CHIPGCR); 2889 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
2808 CHIPGCR &= ~CHIPGCR_FCGMII; 2890 (u8 *) & mask_pattern[0][0]);
2809 2891
2810 if (mii_status & VELOCITY_DUPLEX_FULL) { 2892 writew(crc, &regs->PatternCRC[0]);
2811 CHIPGCR |= CHIPGCR_FCFDX; 2893 writew(WOLCR_ARP_EN, &regs->WOLCRSet);
2812 writeb(CHIPGCR, &regs->CHIPGCR); 2894 }
2813 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n"); 2895
2814 if (vptr->rev_id < REV_ID_VT3216_A0) 2896 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
2815 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR); 2897 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
2816 } else { 2898
2817 CHIPGCR &= ~CHIPGCR_FCFDX; 2899 writew(0x0FFF, &regs->WOLSRClr);
2818 VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n"); 2900
2819 writeb(CHIPGCR, &regs->CHIPGCR); 2901 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
2820 if (vptr->rev_id < REV_ID_VT3216_A0) 2902 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
2821 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR); 2903 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
2822 }
2823 2904
2824 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs); 2905 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2906 }
2825 2907
2826 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10)) { 2908 if (vptr->mii_status & VELOCITY_SPEED_1000)
2827 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG); 2909 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
2828 } else { 2910
2829 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG); 2911 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
2830 } 2912
2831 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */ 2913 {
2832 velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR); 2914 u8 GCR;
2833 ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)); 2915 GCR = readb(&regs->CHIPGCR);
2834 if (mii_status & VELOCITY_SPEED_100) { 2916 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
2835 if (mii_status & VELOCITY_DUPLEX_FULL) 2917 writeb(GCR, &regs->CHIPGCR);
2836 ANAR |= ANAR_TXFD;
2837 else
2838 ANAR |= ANAR_TX;
2839 } else {
2840 if (mii_status & VELOCITY_DUPLEX_FULL)
2841 ANAR |= ANAR_10FD;
2842 else
2843 ANAR |= ANAR_10;
2844 }
2845 velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
2846 /* enable AUTO-NEGO mode */
2847 mii_set_auto_on(vptr);
2848 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
2849 } 2918 }
2850 /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */ 2919
2851 /* vptr->mii_status=check_connection_type(vptr->mac_regs); */ 2920 BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
2852 return VELOCITY_LINK_CHANGE; 2921 /* Turn on SWPTAG just before entering power mode */
2922 BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
2923 /* Go to bed ..... */
2924 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
2925
2926 return 0;
2853} 2927}
2854 2928
2855/** 2929/**
2856 * mii_check_media_mode - check media state 2930 * velocity_save_context - save registers
2857 * @regs: velocity registers 2931 * @vptr: velocity
2932 * @context: buffer for stored context
2858 * 2933 *
2859 * Check the current MII status and determine the link status 2934 * Retrieve the current configuration from the velocity hardware
2860 * accordingly 2935 * and stash it in the context structure, for use by the context
2936 * restore functions. This allows us to save things we need across
2937 * power down states
2861 */ 2938 */
2862 2939static void velocity_save_context(struct velocity_info *vptr, struct velocity_context *context)
2863static u32 mii_check_media_mode(struct mac_regs __iomem * regs)
2864{ 2940{
2865 u32 status = 0; 2941 struct mac_regs __iomem *regs = vptr->mac_regs;
2866 u16 ANAR; 2942 u16 i;
2943 u8 __iomem *ptr = (u8 __iomem *)regs;
2867 2944
2868 if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs)) 2945 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
2869 status |= VELOCITY_LINK_FAIL; 2946 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
2870 2947
2871 if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs)) 2948 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
2872 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL; 2949 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
2873 else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
2874 status |= (VELOCITY_SPEED_1000);
2875 else {
2876 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2877 if (ANAR & ANAR_TXFD)
2878 status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
2879 else if (ANAR & ANAR_TX)
2880 status |= VELOCITY_SPEED_100;
2881 else if (ANAR & ANAR_10FD)
2882 status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
2883 else
2884 status |= (VELOCITY_SPEED_10);
2885 }
2886 2950
2887 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) { 2951 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
2888 velocity_mii_read(regs, MII_REG_ANAR, &ANAR); 2952 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
2889 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2890 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2891 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2892 status |= VELOCITY_AUTONEG_ENABLE;
2893 }
2894 }
2895 2953
2896 return status;
2897} 2954}
2898 2955
2899static u32 check_connection_type(struct mac_regs __iomem * regs) 2956static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
2900{ 2957{
2901 u32 status = 0; 2958 struct net_device *dev = pci_get_drvdata(pdev);
2902 u8 PHYSR0; 2959 struct velocity_info *vptr = netdev_priv(dev);
2903 u16 ANAR; 2960 unsigned long flags;
2904 PHYSR0 = readb(&regs->PHYSR0);
2905
2906 /*
2907 if (!(PHYSR0 & PHYSR0_LINKGD))
2908 status|=VELOCITY_LINK_FAIL;
2909 */
2910 2961
2911 if (PHYSR0 & PHYSR0_FDPX) 2962 if (!netif_running(vptr->dev))
2912 status |= VELOCITY_DUPLEX_FULL; 2963 return 0;
2913 2964
2914 if (PHYSR0 & PHYSR0_SPDG) 2965 netif_device_detach(vptr->dev);
2915 status |= VELOCITY_SPEED_1000;
2916 else if (PHYSR0 & PHYSR0_SPD10)
2917 status |= VELOCITY_SPEED_10;
2918 else
2919 status |= VELOCITY_SPEED_100;
2920 2966
2921 if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) { 2967 spin_lock_irqsave(&vptr->lock, flags);
2922 velocity_mii_read(regs, MII_REG_ANAR, &ANAR); 2968 pci_save_state(pdev);
2923 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) 2969#ifdef ETHTOOL_GWOL
2924 == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) { 2970 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
2925 if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs)) 2971 velocity_get_ip(vptr);
2926 status |= VELOCITY_AUTONEG_ENABLE; 2972 velocity_save_context(vptr, &vptr->context);
2927 } 2973 velocity_shutdown(vptr);
2974 velocity_set_wol(vptr);
2975 pci_enable_wake(pdev, PCI_D3hot, 1);
2976 pci_set_power_state(pdev, PCI_D3hot);
2977 } else {
2978 velocity_save_context(vptr, &vptr->context);
2979 velocity_shutdown(vptr);
2980 pci_disable_device(pdev);
2981 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2928 } 2982 }
2929 2983#else
2930 return status; 2984 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2985#endif
2986 spin_unlock_irqrestore(&vptr->lock, flags);
2987 return 0;
2931} 2988}
2932 2989
2933/** 2990/**
2934 * enable_flow_control_ability - flow control 2991 * velocity_restore_context - restore registers
2935 * @vptr: veloity to configure 2992 * @vptr: velocity
2993 * @context: buffer for stored context
2936 * 2994 *
2937 * Set up flow control according to the flow control options 2995 * Reload the register configuration from the velocity context
2938 * determined by the eeprom/configuration. 2996 * created by velocity_save_context.
2939 */ 2997 */
2940 2998static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
2941static void enable_flow_control_ability(struct velocity_info *vptr)
2942{ 2999{
3000 struct mac_regs __iomem *regs = vptr->mac_regs;
3001 int i;
3002 u8 __iomem *ptr = (u8 __iomem *)regs;
2943 3003
2944 struct mac_regs __iomem * regs = vptr->mac_regs; 3004 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4)
3005 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
2945 3006
2946 switch (vptr->options.flow_cntl) { 3007 /* Just skip cr0 */
3008 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3009 /* Clear */
3010 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3011 /* Set */
3012 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3013 }
2947 3014
2948 case FLOW_CNTL_DEFAULT: 3015 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4)
2949 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0)) 3016 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
2950 writel(CR0_FDXRFCEN, &regs->CR0Set);
2951 else
2952 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2953 3017
2954 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0)) 3018 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
2955 writel(CR0_FDXTFCEN, &regs->CR0Set); 3019 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
2956 else
2957 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2958 break;
2959 3020
2960 case FLOW_CNTL_TX: 3021 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++)
2961 writel(CR0_FDXTFCEN, &regs->CR0Set); 3022 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
2962 writel(CR0_FDXRFCEN, &regs->CR0Clr); 3023}
2963 break;
2964 3024
2965 case FLOW_CNTL_RX: 3025static int velocity_resume(struct pci_dev *pdev)
2966 writel(CR0_FDXRFCEN, &regs->CR0Set); 3026{
2967 writel(CR0_FDXTFCEN, &regs->CR0Clr); 3027 struct net_device *dev = pci_get_drvdata(pdev);
2968 break; 3028 struct velocity_info *vptr = netdev_priv(dev);
3029 unsigned long flags;
3030 int i;
2969 3031
2970 case FLOW_CNTL_TX_RX: 3032 if (!netif_running(vptr->dev))
2971 writel(CR0_FDXTFCEN, &regs->CR0Set); 3033 return 0;
2972 writel(CR0_FDXRFCEN, &regs->CR0Set);
2973 break;
2974 3034
2975 case FLOW_CNTL_DISABLE: 3035 pci_set_power_state(pdev, PCI_D0);
2976 writel(CR0_FDXRFCEN, &regs->CR0Clr); 3036 pci_enable_wake(pdev, 0, 0);
2977 writel(CR0_FDXTFCEN, &regs->CR0Clr); 3037 pci_restore_state(pdev);
2978 break;
2979 3038
2980 default: 3039 mac_wol_reset(vptr->mac_regs);
2981 break; 3040
3041 spin_lock_irqsave(&vptr->lock, flags);
3042 velocity_restore_context(vptr, &vptr->context);
3043 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3044 mac_disable_int(vptr->mac_regs);
3045
3046 velocity_tx_srv(vptr, 0);
3047
3048 for (i = 0; i < vptr->tx.numq; i++) {
3049 if (vptr->tx.used[i])
3050 mac_tx_queue_wake(vptr->mac_regs, i);
2982 } 3051 }
2983 3052
3053 mac_enable_int(vptr->mac_regs);
3054 spin_unlock_irqrestore(&vptr->lock, flags);
3055 netif_device_attach(vptr->dev);
3056
3057 return 0;
2984} 3058}
3059#endif
3060
3061/*
3062 * Definition for our device driver. The PCI layer interface
3063 * uses this to handle all our card discover and plugging
3064 */
3065static struct pci_driver velocity_driver = {
3066 .name = VELOCITY_NAME,
3067 .id_table = velocity_id_table,
3068 .probe = velocity_found1,
3069 .remove = __devexit_p(velocity_remove1),
3070#ifdef CONFIG_PM
3071 .suspend = velocity_suspend,
3072 .resume = velocity_resume,
3073#endif
3074};
2985 3075
2986 3076
2987/** 3077/**
@@ -2991,7 +3081,6 @@ static void enable_flow_control_ability(struct velocity_info *vptr)
2991 * Called before an ethtool operation. We need to make sure the 3081 * Called before an ethtool operation. We need to make sure the
2992 * chip is out of D3 state before we poke at it. 3082 * chip is out of D3 state before we poke at it.
2993 */ 3083 */
2994
2995static int velocity_ethtool_up(struct net_device *dev) 3084static int velocity_ethtool_up(struct net_device *dev)
2996{ 3085{
2997 struct velocity_info *vptr = netdev_priv(dev); 3086 struct velocity_info *vptr = netdev_priv(dev);
@@ -3007,7 +3096,6 @@ static int velocity_ethtool_up(struct net_device *dev)
3007 * Called after an ethtool operation. Restore the chip back to D3 3096 * Called after an ethtool operation. Restore the chip back to D3
3008 * state if it isn't running. 3097 * state if it isn't running.
3009 */ 3098 */
3010
3011static void velocity_ethtool_down(struct net_device *dev) 3099static void velocity_ethtool_down(struct net_device *dev)
3012{ 3100{
3013 struct velocity_info *vptr = netdev_priv(dev); 3101 struct velocity_info *vptr = netdev_priv(dev);
@@ -3018,7 +3106,7 @@ static void velocity_ethtool_down(struct net_device *dev)
3018static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 3106static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3019{ 3107{
3020 struct velocity_info *vptr = netdev_priv(dev); 3108 struct velocity_info *vptr = netdev_priv(dev);
3021 struct mac_regs __iomem * regs = vptr->mac_regs; 3109 struct mac_regs __iomem *regs = vptr->mac_regs;
3022 u32 status; 3110 u32 status;
3023 status = check_connection_type(vptr->mac_regs); 3111 status = check_connection_type(vptr->mac_regs);
3024 3112
@@ -3072,13 +3160,6 @@ static int velocity_set_settings(struct net_device *dev, struct ethtool_cmd *cmd
3072 return ret; 3160 return ret;
3073} 3161}
3074 3162
3075static u32 velocity_get_link(struct net_device *dev)
3076{
3077 struct velocity_info *vptr = netdev_priv(dev);
3078 struct mac_regs __iomem * regs = vptr->mac_regs;
3079 return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0) ? 1 : 0;
3080}
3081
3082static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 3163static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3083{ 3164{
3084 struct velocity_info *vptr = netdev_priv(dev); 3165 struct velocity_info *vptr = netdev_priv(dev);
@@ -3157,338 +3238,86 @@ static const struct ethtool_ops velocity_ethtool_ops = {
3157 .complete = velocity_ethtool_down 3238 .complete = velocity_ethtool_down
3158}; 3239};
3159 3240
3160/** 3241#ifdef CONFIG_PM
3161 * velocity_mii_ioctl - MII ioctl handler 3242#ifdef CONFIG_INET
3162 * @dev: network device 3243static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3163 * @ifr: the ifreq block for the ioctl
3164 * @cmd: the command
3165 *
3166 * Process MII requests made via ioctl from the network layer. These
3167 * are used by tools like kudzu to interrogate the link state of the
3168 * hardware
3169 */
3170
3171static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3172{ 3244{
3173 struct velocity_info *vptr = netdev_priv(dev); 3245 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3174 struct mac_regs __iomem * regs = vptr->mac_regs; 3246 struct net_device *dev = ifa->ifa_dev->dev;
3247 struct velocity_info *vptr;
3175 unsigned long flags; 3248 unsigned long flags;
3176 struct mii_ioctl_data *miidata = if_mii(ifr);
3177 int err;
3178 3249
3179 switch (cmd) { 3250 if (dev_net(dev) != &init_net)
3180 case SIOCGMIIPHY: 3251 return NOTIFY_DONE;
3181 miidata->phy_id = readb(&regs->MIIADR) & 0x1f; 3252
3182 break; 3253 spin_lock_irqsave(&velocity_dev_list_lock, flags);
3183 case SIOCGMIIREG: 3254 list_for_each_entry(vptr, &velocity_dev_list, list) {
3184 if (!capable(CAP_NET_ADMIN)) 3255 if (vptr->dev == dev) {
3185 return -EPERM; 3256 velocity_get_ip(vptr);
3186 if(velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0) 3257 break;
3187 return -ETIMEDOUT; 3258 }
3188 break;
3189 case SIOCSMIIREG:
3190 if (!capable(CAP_NET_ADMIN))
3191 return -EPERM;
3192 spin_lock_irqsave(&vptr->lock, flags);
3193 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
3194 spin_unlock_irqrestore(&vptr->lock, flags);
3195 check_connection_type(vptr->mac_regs);
3196 if(err)
3197 return err;
3198 break;
3199 default:
3200 return -EOPNOTSUPP;
3201 } 3259 }
3202 return 0; 3260 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
3203}
3204 3261
3205#ifdef CONFIG_PM 3262 return NOTIFY_DONE;
3263}
3264#endif /* CONFIG_INET */
3265#endif /* CONFIG_PM */
3206 3266
3207/** 3267#if defined(CONFIG_PM) && defined(CONFIG_INET)
3208 * velocity_save_context - save registers 3268static struct notifier_block velocity_inetaddr_notifier = {
3209 * @vptr: velocity 3269 .notifier_call = velocity_netdev_event,
3210 * @context: buffer for stored context 3270};
3211 *
3212 * Retrieve the current configuration from the velocity hardware
3213 * and stash it in the context structure, for use by the context
3214 * restore functions. This allows us to save things we need across
3215 * power down states
3216 */
3217 3271
3218static void velocity_save_context(struct velocity_info *vptr, struct velocity_context * context) 3272static void velocity_register_notifier(void)
3219{ 3273{
3220 struct mac_regs __iomem * regs = vptr->mac_regs; 3274 register_inetaddr_notifier(&velocity_inetaddr_notifier);
3221 u16 i;
3222 u8 __iomem *ptr = (u8 __iomem *)regs;
3223
3224 for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3225 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3226
3227 for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3228 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3229
3230 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3231 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3232
3233} 3275}
3234 3276
3235/** 3277static void velocity_unregister_notifier(void)
3236 * velocity_restore_context - restore registers
3237 * @vptr: velocity
3238 * @context: buffer for stored context
3239 *
3240 * Reload the register configuration from the velocity context
3241 * created by velocity_save_context.
3242 */
3243
3244static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3245{ 3278{
3246 struct mac_regs __iomem * regs = vptr->mac_regs; 3279 unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
3247 int i; 3280}
3248 u8 __iomem *ptr = (u8 __iomem *)regs;
3249
3250 for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4) {
3251 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3252 }
3253
3254 /* Just skip cr0 */
3255 for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3256 /* Clear */
3257 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3258 /* Set */
3259 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3260 }
3261
3262 for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4) {
3263 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3264 }
3265 3281
3266 for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4) { 3282#else
3267 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3268 }
3269 3283
3270 for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++) { 3284#define velocity_register_notifier() do {} while (0)
3271 writeb(*((u8 *) (context->mac_reg + i)), ptr + i); 3285#define velocity_unregister_notifier() do {} while (0)
3272 }
3273 3286
3274} 3287#endif /* defined(CONFIG_PM) && defined(CONFIG_INET) */
3275 3288
3276/** 3289/**
3277 * wol_calc_crc - WOL CRC 3290 * velocity_init_module - load time function
3278 * @pattern: data pattern
3279 * @mask_pattern: mask
3280 * 3291 *
3281 * Compute the wake on lan crc hashes for the packet header 3292 * Called when the velocity module is loaded. The PCI driver
3282 * we are interested in. 3293 * is registered with the PCI layer, and in turn will call
3294 * the probe functions for each velocity adapter installed
3295 * in the system.
3283 */ 3296 */
3284 3297static int __init velocity_init_module(void)
3285static u16 wol_calc_crc(int size, u8 * pattern, u8 *mask_pattern)
3286{ 3298{
3287 u16 crc = 0xFFFF; 3299 int ret;
3288 u8 mask;
3289 int i, j;
3290
3291 for (i = 0; i < size; i++) {
3292 mask = mask_pattern[i];
3293
3294 /* Skip this loop if the mask equals to zero */
3295 if (mask == 0x00)
3296 continue;
3297 3300
3298 for (j = 0; j < 8; j++) { 3301 velocity_register_notifier();
3299 if ((mask & 0x01) == 0) { 3302 ret = pci_register_driver(&velocity_driver);
3300 mask >>= 1; 3303 if (ret < 0)
3301 continue; 3304 velocity_unregister_notifier();
3302 } 3305 return ret;
3303 mask >>= 1;
3304 crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
3305 }
3306 }
3307 /* Finally, invert the result once to get the correct data */
3308 crc = ~crc;
3309 return bitrev32(crc) >> 16;
3310} 3306}
3311 3307
3312/** 3308/**
3313 * velocity_set_wol - set up for wake on lan 3309 * velocity_cleanup - module unload
3314 * @vptr: velocity to set WOL status on
3315 *
3316 * Set a card up for wake on lan either by unicast or by
3317 * ARP packet.
3318 * 3310 *
3319 * FIXME: check static buffer is safe here 3311 * When the velocity hardware is unloaded this function is called.
3312 * It will clean up the notifiers and the unregister the PCI
3313 * driver interface for this hardware. This in turn cleans up
3314 * all discovered interfaces before returning from the function
3320 */ 3315 */
3321 3316static void __exit velocity_cleanup_module(void)
3322static int velocity_set_wol(struct velocity_info *vptr)
3323{
3324 struct mac_regs __iomem * regs = vptr->mac_regs;
3325 static u8 buf[256];
3326 int i;
3327
3328 static u32 mask_pattern[2][4] = {
3329 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3330 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff} /* Magic Packet */
3331 };
3332
3333 writew(0xFFFF, &regs->WOLCRClr);
3334 writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
3335 writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
3336
3337 /*
3338 if (vptr->wol_opts & VELOCITY_WOL_PHY)
3339 writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
3340 */
3341
3342 if (vptr->wol_opts & VELOCITY_WOL_UCAST) {
3343 writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
3344 }
3345
3346 if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3347 struct arp_packet *arp = (struct arp_packet *) buf;
3348 u16 crc;
3349 memset(buf, 0, sizeof(struct arp_packet) + 7);
3350
3351 for (i = 0; i < 4; i++)
3352 writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
3353
3354 arp->type = htons(ETH_P_ARP);
3355 arp->ar_op = htons(1);
3356
3357 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3358
3359 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3360 (u8 *) & mask_pattern[0][0]);
3361
3362 writew(crc, &regs->PatternCRC[0]);
3363 writew(WOLCR_ARP_EN, &regs->WOLCRSet);
3364 }
3365
3366 BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
3367 BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
3368
3369 writew(0x0FFF, &regs->WOLSRClr);
3370
3371 if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3372 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3373 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
3374
3375 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
3376 }
3377
3378 if (vptr->mii_status & VELOCITY_SPEED_1000)
3379 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
3380
3381 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
3382
3383 {
3384 u8 GCR;
3385 GCR = readb(&regs->CHIPGCR);
3386 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3387 writeb(GCR, &regs->CHIPGCR);
3388 }
3389
3390 BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
3391 /* Turn on SWPTAG just before entering power mode */
3392 BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
3393 /* Go to bed ..... */
3394 BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
3395
3396 return 0;
3397}
3398
3399static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
3400{
3401 struct net_device *dev = pci_get_drvdata(pdev);
3402 struct velocity_info *vptr = netdev_priv(dev);
3403 unsigned long flags;
3404
3405 if(!netif_running(vptr->dev))
3406 return 0;
3407
3408 netif_device_detach(vptr->dev);
3409
3410 spin_lock_irqsave(&vptr->lock, flags);
3411 pci_save_state(pdev);
3412#ifdef ETHTOOL_GWOL
3413 if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3414 velocity_get_ip(vptr);
3415 velocity_save_context(vptr, &vptr->context);
3416 velocity_shutdown(vptr);
3417 velocity_set_wol(vptr);
3418 pci_enable_wake(pdev, PCI_D3hot, 1);
3419 pci_set_power_state(pdev, PCI_D3hot);
3420 } else {
3421 velocity_save_context(vptr, &vptr->context);
3422 velocity_shutdown(vptr);
3423 pci_disable_device(pdev);
3424 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3425 }
3426#else
3427 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3428#endif
3429 spin_unlock_irqrestore(&vptr->lock, flags);
3430 return 0;
3431}
3432
3433static int velocity_resume(struct pci_dev *pdev)
3434{
3435 struct net_device *dev = pci_get_drvdata(pdev);
3436 struct velocity_info *vptr = netdev_priv(dev);
3437 unsigned long flags;
3438 int i;
3439
3440 if(!netif_running(vptr->dev))
3441 return 0;
3442
3443 pci_set_power_state(pdev, PCI_D0);
3444 pci_enable_wake(pdev, 0, 0);
3445 pci_restore_state(pdev);
3446
3447 mac_wol_reset(vptr->mac_regs);
3448
3449 spin_lock_irqsave(&vptr->lock, flags);
3450 velocity_restore_context(vptr, &vptr->context);
3451 velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3452 mac_disable_int(vptr->mac_regs);
3453
3454 velocity_tx_srv(vptr, 0);
3455
3456 for (i = 0; i < vptr->tx.numq; i++) {
3457 if (vptr->tx.used[i]) {
3458 mac_tx_queue_wake(vptr->mac_regs, i);
3459 }
3460 }
3461
3462 mac_enable_int(vptr->mac_regs);
3463 spin_unlock_irqrestore(&vptr->lock, flags);
3464 netif_device_attach(vptr->dev);
3465
3466 return 0;
3467}
3468
3469#ifdef CONFIG_INET
3470
3471static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3472{ 3317{
3473 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr; 3318 velocity_unregister_notifier();
3474 struct net_device *dev = ifa->ifa_dev->dev; 3319 pci_unregister_driver(&velocity_driver);
3475 struct velocity_info *vptr;
3476 unsigned long flags;
3477
3478 if (dev_net(dev) != &init_net)
3479 return NOTIFY_DONE;
3480
3481 spin_lock_irqsave(&velocity_dev_list_lock, flags);
3482 list_for_each_entry(vptr, &velocity_dev_list, list) {
3483 if (vptr->dev == dev) {
3484 velocity_get_ip(vptr);
3485 break;
3486 }
3487 }
3488 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
3489
3490 return NOTIFY_DONE;
3491} 3320}
3492 3321
3493#endif 3322module_init(velocity_init_module);
3494#endif 3323module_exit(velocity_cleanup_module);