Merge to Fedora kernel-2.6.17-1.2187_FC5 patched with stable patch-2.6.17.13-vs2...
[linux-2.6.git] / drivers / net / via-velocity.c
1 /*
2  * This code is derived from the VIA reference driver (copyright message
3  * below) provided to Red Hat by VIA Networking Technologies, Inc. for
4  * addition to the Linux kernel.
5  *
6  * The code has been merged into one source file, cleaned up to follow
7  * Linux coding style,  ported to the Linux 2.6 kernel tree and cleaned
8  * for 64bit hardware platforms.
9  *
10  * TODO
11  *      Big-endian support
12  *      rx_copybreak/alignment
13  *      Scatter gather
14  *      More testing
15  *
16  * The changes are (c) Copyright 2004, Red Hat Inc. <alan@redhat.com>
17  * Additional fixes and clean up: Francois Romieu
18  *
19  * This source has not been verified for use in safety critical systems.
20  *
21  * Please direct queries about the revamped driver to the linux-kernel
22  * list not VIA.
23  *
24  * Original code:
25  *
26  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
27  * All rights reserved.
28  *
29  * This software may be redistributed and/or modified under
30  * the terms of the GNU General Public License as published by the Free
31  * Software Foundation; either version 2 of the License, or
32  * any later version.
33  *
34  * This program is distributed in the hope that it will be useful, but
35  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
36  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
37  * for more details.
38  *
39  * Author: Chuang Liang-Shing, AJ Jiang
40  *
41  * Date: Jan 24, 2003
42  *
43  * MODULE_LICENSE("GPL");
44  *
45  */
46
47
48 #include <linux/module.h>
49 #include <linux/types.h>
50 #include <linux/config.h>
51 #include <linux/init.h>
52 #include <linux/mm.h>
53 #include <linux/errno.h>
54 #include <linux/ioport.h>
55 #include <linux/pci.h>
56 #include <linux/kernel.h>
57 #include <linux/netdevice.h>
58 #include <linux/etherdevice.h>
59 #include <linux/skbuff.h>
60 #include <linux/delay.h>
61 #include <linux/timer.h>
62 #include <linux/slab.h>
63 #include <linux/interrupt.h>
64 #include <linux/string.h>
65 #include <linux/wait.h>
66 #include <asm/io.h>
67 #include <linux/if.h>
68 #include <linux/config.h>
69 #include <asm/uaccess.h>
70 #include <linux/proc_fs.h>
71 #include <linux/inetdevice.h>
72 #include <linux/reboot.h>
73 #include <linux/ethtool.h>
74 #include <linux/mii.h>
75 #include <linux/in.h>
76 #include <linux/if_arp.h>
77 #include <linux/ip.h>
78 #include <linux/tcp.h>
79 #include <linux/udp.h>
80 #include <linux/crc-ccitt.h>
81 #include <linux/crc32.h>
82
83 #include "via-velocity.h"
84
85
86 static int velocity_nics = 0;
87 static int msglevel = MSG_LEVEL_INFO;
88
89
90 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
91 static struct ethtool_ops velocity_ethtool_ops;
92
93 /*
94     Define module options
95 */
96
97 MODULE_AUTHOR("VIA Networking Technologies, Inc.");
98 MODULE_LICENSE("GPL");
99 MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
100
101 #define VELOCITY_PARAM(N,D) \
102         static int N[MAX_UNITS]=OPTION_DEFAULT;\
103         module_param_array(N, int, NULL, 0); \
104         MODULE_PARM_DESC(N, D);
105
106 #define RX_DESC_MIN     64
107 #define RX_DESC_MAX     255
108 #define RX_DESC_DEF     64
109 VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
110
111 #define TX_DESC_MIN     16
112 #define TX_DESC_MAX     256
113 #define TX_DESC_DEF     64
114 VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
115
116 #define VLAN_ID_MIN     0
117 #define VLAN_ID_MAX     4095
118 #define VLAN_ID_DEF     0
119 /* VID_setting[] is used for setting the VID of NIC.
120    0: default VID.
121    1-4094: other VIDs.
122 */
123 VELOCITY_PARAM(VID_setting, "802.1Q VLAN ID");
124
125 #define RX_THRESH_MIN   0
126 #define RX_THRESH_MAX   3
127 #define RX_THRESH_DEF   0
128 /* rx_thresh[] is used for controlling the receive fifo threshold.
129    0: indicate the rxfifo threshold is 128 bytes.
130    1: indicate the rxfifo threshold is 512 bytes.
131    2: indicate the rxfifo threshold is 1024 bytes.
132    3: indicate the rxfifo threshold is store & forward.
133 */
134 VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
135
136 #define DMA_LENGTH_MIN  0
137 #define DMA_LENGTH_MAX  7
138 #define DMA_LENGTH_DEF  0
139
140 /* DMA_length[] is used for controlling the DMA length
141    0: 8 DWORDs
142    1: 16 DWORDs
143    2: 32 DWORDs
144    3: 64 DWORDs
145    4: 128 DWORDs
146    5: 256 DWORDs
147    6: SF(flush till emply)
148    7: SF(flush till emply)
149 */
150 VELOCITY_PARAM(DMA_length, "DMA length");
151
152 #define TAGGING_DEF     0
153 /* enable_tagging[] is used for enabling 802.1Q VID tagging.
154    0: disable VID seeting(default).
155    1: enable VID setting.
156 */
157 VELOCITY_PARAM(enable_tagging, "Enable 802.1Q tagging");
158
159 #define IP_ALIG_DEF     0
160 /* IP_byte_align[] is used for IP header DWORD byte aligned
161    0: indicate the IP header won't be DWORD byte aligned.(Default) .
162    1: indicate the IP header will be DWORD byte aligned.
163       In some enviroment, the IP header should be DWORD byte aligned,
164       or the packet will be droped when we receive it. (eg: IPVS)
165 */
166 VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
167
168 #define TX_CSUM_DEF     1
169 /* txcsum_offload[] is used for setting the checksum offload ability of NIC.
170    (We only support RX checksum offload now)
171    0: disable csum_offload[checksum offload
172    1: enable checksum offload. (Default)
173 */
174 VELOCITY_PARAM(txcsum_offload, "Enable transmit packet checksum offload");
175
176 #define FLOW_CNTL_DEF   1
177 #define FLOW_CNTL_MIN   1
178 #define FLOW_CNTL_MAX   5
179
180 /* flow_control[] is used for setting the flow control ability of NIC.
181    1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
182    2: enable TX flow control.
183    3: enable RX flow control.
184    4: enable RX/TX flow control.
185    5: disable
186 */
187 VELOCITY_PARAM(flow_control, "Enable flow control ability");
188
189 #define MED_LNK_DEF 0
190 #define MED_LNK_MIN 0
191 #define MED_LNK_MAX 4
192 /* speed_duplex[] is used for setting the speed and duplex mode of NIC.
193    0: indicate autonegotiation for both speed and duplex mode
194    1: indicate 100Mbps half duplex mode
195    2: indicate 100Mbps full duplex mode
196    3: indicate 10Mbps half duplex mode
197    4: indicate 10Mbps full duplex mode
198
199    Note:
200         if EEPROM have been set to the force mode, this option is ignored
201             by driver.
202 */
203 VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
204
205 #define VAL_PKT_LEN_DEF     0
206 /* ValPktLen[] is used for setting the checksum offload ability of NIC.
207    0: Receive frame with invalid layer 2 length (Default)
208    1: Drop frame with invalid layer 2 length
209 */
210 VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
211
212 #define WOL_OPT_DEF     0
213 #define WOL_OPT_MIN     0
214 #define WOL_OPT_MAX     7
215 /* wol_opts[] is used for controlling wake on lan behavior.
216    0: Wake up if recevied a magic packet. (Default)
217    1: Wake up if link status is on/off.
218    2: Wake up if recevied an arp packet.
219    4: Wake up if recevied any unicast packet.
220    Those value can be sumed up to support more than one option.
221 */
222 VELOCITY_PARAM(wol_opts, "Wake On Lan options");
223
224 #define INT_WORKS_DEF   20
225 #define INT_WORKS_MIN   10
226 #define INT_WORKS_MAX   64
227
228 VELOCITY_PARAM(int_works, "Number of packets per interrupt services");
229
230 static int rx_copybreak = 200;
231 module_param(rx_copybreak, int, 0644);
232 MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
233
234 static void velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr, struct velocity_info_tbl *info);
235 static int velocity_get_pci_info(struct velocity_info *, struct pci_dev *pdev);
236 static void velocity_print_info(struct velocity_info *vptr);
237 static int velocity_open(struct net_device *dev);
238 static int velocity_change_mtu(struct net_device *dev, int mtu);
239 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev);
240 static int velocity_intr(int irq, void *dev_instance, struct pt_regs *regs);
241 static void velocity_set_multi(struct net_device *dev);
242 static struct net_device_stats *velocity_get_stats(struct net_device *dev);
243 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
244 static int velocity_close(struct net_device *dev);
245 static int velocity_receive_frame(struct velocity_info *, int idx);
246 static int velocity_alloc_rx_buf(struct velocity_info *, int idx);
247 static void velocity_free_rd_ring(struct velocity_info *vptr);
248 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *);
249 static int velocity_soft_reset(struct velocity_info *vptr);
250 static void mii_init(struct velocity_info *vptr, u32 mii_status);
251 static u32 velocity_get_link(struct net_device *dev);
252 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr);
253 static void velocity_print_link_status(struct velocity_info *vptr);
254 static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs);
255 static void velocity_shutdown(struct velocity_info *vptr);
256 static void enable_flow_control_ability(struct velocity_info *vptr);
257 static void enable_mii_autopoll(struct mac_regs __iomem * regs);
258 static int velocity_mii_read(struct mac_regs __iomem *, u8 byIdx, u16 * pdata);
259 static int velocity_mii_write(struct mac_regs __iomem *, u8 byMiiAddr, u16 data);
260 static u32 mii_check_media_mode(struct mac_regs __iomem * regs);
261 static u32 check_connection_type(struct mac_regs __iomem * regs);
262 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status);
263
264 #ifdef CONFIG_PM
265
266 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state);
267 static int velocity_resume(struct pci_dev *pdev);
268
269 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr);
270
271 static struct notifier_block velocity_inetaddr_notifier = {
272       .notifier_call    = velocity_netdev_event,
273 };
274
275 static DEFINE_SPINLOCK(velocity_dev_list_lock);
276 static LIST_HEAD(velocity_dev_list);
277
278 static void velocity_register_notifier(void)
279 {
280         register_inetaddr_notifier(&velocity_inetaddr_notifier);
281 }
282
283 static void velocity_unregister_notifier(void)
284 {
285         unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
286 }
287
288 #else                           /* CONFIG_PM */
289
290 #define velocity_register_notifier()    do {} while (0)
291 #define velocity_unregister_notifier()  do {} while (0)
292
293 #endif                          /* !CONFIG_PM */
294
295 /*
296  *      Internal board variants. At the moment we have only one
297  */
298
299 static struct velocity_info_tbl chip_info_table[] = {
300         {CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 256, 1, 0x00FFFFFFUL},
301         {0, NULL}
302 };
303
304 /*
305  *      Describe the PCI device identifiers that we support in this
306  *      device driver. Used for hotplug autoloading.
307  */
308
309 static struct pci_device_id velocity_id_table[] __devinitdata = {
310         {PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X,
311          PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) chip_info_table},
312         {0, }
313 };
314
315 MODULE_DEVICE_TABLE(pci, velocity_id_table);
316
317 /**
318  *      get_chip_name   -       identifier to name
319  *      @id: chip identifier
320  *
321  *      Given a chip identifier return a suitable description. Returns
322  *      a pointer a static string valid while the driver is loaded.
323  */
324
325 static char __devinit *get_chip_name(enum chip_type chip_id)
326 {
327         int i;
328         for (i = 0; chip_info_table[i].name != NULL; i++)
329                 if (chip_info_table[i].chip_id == chip_id)
330                         break;
331         return chip_info_table[i].name;
332 }
333
334 /**
335  *      velocity_remove1        -       device unplug
336  *      @pdev: PCI device being removed
337  *
338  *      Device unload callback. Called on an unplug or on module
339  *      unload for each active device that is present. Disconnects
340  *      the device from the network layer and frees all the resources
341  */
342
343 static void __devexit velocity_remove1(struct pci_dev *pdev)
344 {
345         struct net_device *dev = pci_get_drvdata(pdev);
346         struct velocity_info *vptr = dev->priv;
347
348 #ifdef CONFIG_PM
349         unsigned long flags;
350
351         spin_lock_irqsave(&velocity_dev_list_lock, flags);
352         if (!list_empty(&velocity_dev_list))
353                 list_del(&vptr->list);
354         spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
355 #endif
356         unregister_netdev(dev);
357         iounmap(vptr->mac_regs);
358         pci_release_regions(pdev);
359         pci_disable_device(pdev);
360         pci_set_drvdata(pdev, NULL);
361         free_netdev(dev);
362
363         velocity_nics--;
364 }
365
366 /**
367  *      velocity_set_int_opt    -       parser for integer options
368  *      @opt: pointer to option value
369  *      @val: value the user requested (or -1 for default)
370  *      @min: lowest value allowed
371  *      @max: highest value allowed
372  *      @def: default value
373  *      @name: property name
374  *      @dev: device name
375  *
376  *      Set an integer property in the module options. This function does
377  *      all the verification and checking as well as reporting so that
378  *      we don't duplicate code for each option.
379  */
380
381 static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max, int def, char *name, char *devname)
382 {
383         if (val == -1)
384                 *opt = def;
385         else if (val < min || val > max) {
386                 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
387                                         devname, name, min, max);
388                 *opt = def;
389         } else {
390                 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
391                                         devname, name, val);
392                 *opt = val;
393         }
394 }
395
396 /**
397  *      velocity_set_bool_opt   -       parser for boolean options
398  *      @opt: pointer to option value
399  *      @val: value the user requested (or -1 for default)
400  *      @def: default value (yes/no)
401  *      @flag: numeric value to set for true.
402  *      @name: property name
403  *      @dev: device name
404  *
405  *      Set a boolean property in the module options. This function does
406  *      all the verification and checking as well as reporting so that
407  *      we don't duplicate code for each option.
408  */
409
410 static void __devinit velocity_set_bool_opt(u32 * opt, int val, int def, u32 flag, char *name, char *devname)
411 {
412         (*opt) &= (~flag);
413         if (val == -1)
414                 *opt |= (def ? flag : 0);
415         else if (val < 0 || val > 1) {
416                 printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n", 
417                         devname, name);
418                 *opt |= (def ? flag : 0);
419         } else {
420                 printk(KERN_INFO "%s: set parameter %s to %s\n", 
421                         devname, name, val ? "TRUE" : "FALSE");
422                 *opt |= (val ? flag : 0);
423         }
424 }
425
426 /**
427  *      velocity_get_options    -       set options on device
428  *      @opts: option structure for the device
429  *      @index: index of option to use in module options array
430  *      @devname: device name
431  *
432  *      Turn the module and command options into a single structure
433  *      for the current device
434  */
435
436 static void __devinit velocity_get_options(struct velocity_opt *opts, int index, char *devname)
437 {
438
439         velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
440         velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
441         velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
442         velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
443         velocity_set_int_opt(&opts->vid, VID_setting[index], VLAN_ID_MIN, VLAN_ID_MAX, VLAN_ID_DEF, "VID_setting", devname);
444         velocity_set_bool_opt(&opts->flags, enable_tagging[index], TAGGING_DEF, VELOCITY_FLAGS_TAGGING, "enable_tagging", devname);
445         velocity_set_bool_opt(&opts->flags, txcsum_offload[index], TX_CSUM_DEF, VELOCITY_FLAGS_TX_CSUM, "txcsum_offload", devname);
446         velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
447         velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
448         velocity_set_bool_opt(&opts->flags, ValPktLen[index], VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN, "ValPktLen", devname);
449         velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
450         velocity_set_int_opt((int *) &opts->wol_opts, wol_opts[index], WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF, "Wake On Lan options", devname);
451         velocity_set_int_opt((int *) &opts->int_works, int_works[index], INT_WORKS_MIN, INT_WORKS_MAX, INT_WORKS_DEF, "Interrupt service works", devname);
452         opts->numrx = (opts->numrx & ~3);
453 }
454
455 /**
456  *      velocity_init_cam_filter        -       initialise CAM
457  *      @vptr: velocity to program
458  *
459  *      Initialize the content addressable memory used for filters. Load
460  *      appropriately according to the presence of VLAN
461  */
462
463 static void velocity_init_cam_filter(struct velocity_info *vptr)
464 {
465         struct mac_regs __iomem * regs = vptr->mac_regs;
466
467         /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
468         WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
469         WORD_REG_BITS_ON(MCFG_VIDFR, &regs->MCFG);
470
471         /* Disable all CAMs */
472         memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
473         memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
474         mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
475         mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
476
477         /* Enable first VCAM */
478         if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
479                 /* If Tagging option is enabled and VLAN ID is not zero, then
480                    turn on MCFG_RTGOPT also */
481                 if (vptr->options.vid != 0)
482                         WORD_REG_BITS_ON(MCFG_RTGOPT, &regs->MCFG);
483
484                 mac_set_cam(regs, 0, (u8 *) & (vptr->options.vid), VELOCITY_VLAN_ID_CAM);
485                 vptr->vCAMmask[0] |= 1;
486                 mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
487         } else {
488                 u16 temp = 0;
489                 mac_set_cam(regs, 0, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
490                 temp = 1;
491                 mac_set_cam_mask(regs, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
492         }
493 }
494
495 /**
496  *      velocity_rx_reset       -       handle a receive reset
497  *      @vptr: velocity we are resetting
498  *
499  *      Reset the ownership and status for the receive ring side.
500  *      Hand all the receive queue to the NIC.
501  */
502
503 static void velocity_rx_reset(struct velocity_info *vptr)
504 {
505
506         struct mac_regs __iomem * regs = vptr->mac_regs;
507         int i;
508
509         vptr->rd_dirty = vptr->rd_filled = vptr->rd_curr = 0;
510
511         /*
512          *      Init state, all RD entries belong to the NIC
513          */
514         for (i = 0; i < vptr->options.numrx; ++i)
515                 vptr->rd_ring[i].rdesc0.owner = OWNED_BY_NIC;
516
517         writew(vptr->options.numrx, &regs->RBRDU);
518         writel(vptr->rd_pool_dma, &regs->RDBaseLo);
519         writew(0, &regs->RDIdx);
520         writew(vptr->options.numrx - 1, &regs->RDCSize);
521 }
522
523 /**
524  *      velocity_init_registers -       initialise MAC registers
525  *      @vptr: velocity to init
526  *      @type: type of initialisation (hot or cold)
527  *
528  *      Initialise the MAC on a reset or on first set up on the
529  *      hardware.
530  */
531
532 static void velocity_init_registers(struct velocity_info *vptr, 
533                                     enum velocity_init_type type)
534 {
535         struct mac_regs __iomem * regs = vptr->mac_regs;
536         int i, mii_status;
537
538         mac_wol_reset(regs);
539
540         switch (type) {
541         case VELOCITY_INIT_RESET:
542         case VELOCITY_INIT_WOL:
543
544                 netif_stop_queue(vptr->dev);
545
546                 /*
547                  *      Reset RX to prevent RX pointer not on the 4X location
548                  */
549                 velocity_rx_reset(vptr);
550                 mac_rx_queue_run(regs);
551                 mac_rx_queue_wake(regs);
552
553                 mii_status = velocity_get_opt_media_mode(vptr);
554                 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
555                         velocity_print_link_status(vptr);
556                         if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
557                                 netif_wake_queue(vptr->dev);
558                 }
559
560                 enable_flow_control_ability(vptr);
561
562                 mac_clear_isr(regs);
563                 writel(CR0_STOP, &regs->CR0Clr);
564                 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), 
565                                                         &regs->CR0Set);
566
567                 break;
568
569         case VELOCITY_INIT_COLD:
570         default:
571                 /*
572                  *      Do reset
573                  */
574                 velocity_soft_reset(vptr);
575                 mdelay(5);
576
577                 mac_eeprom_reload(regs);
578                 for (i = 0; i < 6; i++) {
579                         writeb(vptr->dev->dev_addr[i], &(regs->PAR[i]));
580                 }
581                 /*
582                  *      clear Pre_ACPI bit.
583                  */
584                 BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
585                 mac_set_rx_thresh(regs, vptr->options.rx_thresh);
586                 mac_set_dma_length(regs, vptr->options.DMA_length);
587
588                 writeb(WOLCFG_SAM | WOLCFG_SAB, &regs->WOLCFGSet);
589                 /*
590                  *      Back off algorithm use original IEEE standard
591                  */
592                 BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), &regs->CFGB);
593
594                 /*
595                  *      Init CAM filter
596                  */
597                 velocity_init_cam_filter(vptr);
598
599                 /*
600                  *      Set packet filter: Receive directed and broadcast address
601                  */
602                 velocity_set_multi(vptr->dev);
603
604                 /*
605                  *      Enable MII auto-polling
606                  */
607                 enable_mii_autopoll(regs);
608
609                 vptr->int_mask = INT_MASK_DEF;
610
611                 writel(cpu_to_le32(vptr->rd_pool_dma), &regs->RDBaseLo);
612                 writew(vptr->options.numrx - 1, &regs->RDCSize);
613                 mac_rx_queue_run(regs);
614                 mac_rx_queue_wake(regs);
615
616                 writew(vptr->options.numtx - 1, &regs->TDCSize);
617
618                 for (i = 0; i < vptr->num_txq; i++) {
619                         writel(cpu_to_le32(vptr->td_pool_dma[i]), &(regs->TDBaseLo[i]));
620                         mac_tx_queue_run(regs, i);
621                 }
622
623                 init_flow_control_register(vptr);
624
625                 writel(CR0_STOP, &regs->CR0Clr);
626                 writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), &regs->CR0Set);
627
628                 mii_status = velocity_get_opt_media_mode(vptr);
629                 netif_stop_queue(vptr->dev);
630
631                 mii_init(vptr, mii_status);
632
633                 if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
634                         velocity_print_link_status(vptr);
635                         if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
636                                 netif_wake_queue(vptr->dev);
637                 }
638
639                 enable_flow_control_ability(vptr);
640                 mac_hw_mibs_init(regs);
641                 mac_write_int_mask(vptr->int_mask, regs);
642                 mac_clear_isr(regs);
643
644         }
645 }
646
647 /**
648  *      velocity_soft_reset     -       soft reset
649  *      @vptr: velocity to reset
650  *
651  *      Kick off a soft reset of the velocity adapter and then poll
652  *      until the reset sequence has completed before returning.
653  */
654
655 static int velocity_soft_reset(struct velocity_info *vptr)
656 {
657         struct mac_regs __iomem * regs = vptr->mac_regs;
658         int i = 0;
659
660         writel(CR0_SFRST, &regs->CR0Set);
661
662         for (i = 0; i < W_MAX_TIMEOUT; i++) {
663                 udelay(5);
664                 if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
665                         break;
666         }
667
668         if (i == W_MAX_TIMEOUT) {
669                 writel(CR0_FORSRST, &regs->CR0Set);
670                 /* FIXME: PCI POSTING */
671                 /* delay 2ms */
672                 mdelay(2);
673         }
674         return 0;
675 }
676
677 /**
678  *      velocity_found1         -       set up discovered velocity card
679  *      @pdev: PCI device
680  *      @ent: PCI device table entry that matched
681  *
682  *      Configure a discovered adapter from scratch. Return a negative
683  *      errno error code on failure paths.
684  */
685
686 static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
687 {
688         static int first = 1;
689         struct net_device *dev;
690         int i;
691         struct velocity_info_tbl *info = (struct velocity_info_tbl *) ent->driver_data;
692         struct velocity_info *vptr;
693         struct mac_regs __iomem * regs;
694         int ret = -ENOMEM;
695
696         if (velocity_nics >= MAX_UNITS) {
697                 printk(KERN_NOTICE VELOCITY_NAME ": already found %d NICs.\n", 
698                                 velocity_nics);
699                 return -ENODEV;
700         }
701
702         dev = alloc_etherdev(sizeof(struct velocity_info));
703
704         if (dev == NULL) {
705                 printk(KERN_ERR VELOCITY_NAME ": allocate net device failed.\n");
706                 goto out;
707         }
708         
709         /* Chain it all together */
710         
711         SET_MODULE_OWNER(dev);
712         SET_NETDEV_DEV(dev, &pdev->dev);
713         vptr = dev->priv;
714
715
716         if (first) {
717                 printk(KERN_INFO "%s Ver. %s\n", 
718                         VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
719                 printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
720                 printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
721                 first = 0;
722         }
723
724         velocity_init_info(pdev, vptr, info);
725
726         vptr->dev = dev;
727
728         dev->irq = pdev->irq;
729
730         ret = pci_enable_device(pdev);
731         if (ret < 0) 
732                 goto err_free_dev;
733
734         ret = velocity_get_pci_info(vptr, pdev);
735         if (ret < 0) {
736                 printk(KERN_ERR VELOCITY_NAME ": Failed to find PCI device.\n");
737                 goto err_disable;
738         }
739
740         ret = pci_request_regions(pdev, VELOCITY_NAME);
741         if (ret < 0) {
742                 printk(KERN_ERR VELOCITY_NAME ": Failed to find PCI device.\n");
743                 goto err_disable;
744         }
745
746         regs = ioremap(vptr->memaddr, vptr->io_size);
747         if (regs == NULL) {
748                 ret = -EIO;
749                 goto err_release_res;
750         }
751
752         vptr->mac_regs = regs;
753
754         mac_wol_reset(regs);
755
756         dev->base_addr = vptr->ioaddr;
757
758         for (i = 0; i < 6; i++)
759                 dev->dev_addr[i] = readb(&regs->PAR[i]);
760
761
762         velocity_get_options(&vptr->options, velocity_nics, dev->name);
763
764         /* 
765          *      Mask out the options cannot be set to the chip
766          */
767          
768         vptr->options.flags &= info->flags;
769
770         /*
771          *      Enable the chip specified capbilities
772          */
773          
774         vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
775
776         vptr->wol_opts = vptr->options.wol_opts;
777         vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
778
779         vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
780
781         dev->irq = pdev->irq;
782         dev->open = velocity_open;
783         dev->hard_start_xmit = velocity_xmit;
784         dev->stop = velocity_close;
785         dev->get_stats = velocity_get_stats;
786         dev->set_multicast_list = velocity_set_multi;
787         dev->do_ioctl = velocity_ioctl;
788         dev->ethtool_ops = &velocity_ethtool_ops;
789         dev->change_mtu = velocity_change_mtu;
790 #ifdef  VELOCITY_ZERO_COPY_SUPPORT
791         dev->features |= NETIF_F_SG;
792 #endif
793
794         if (vptr->flags & VELOCITY_FLAGS_TX_CSUM) {
795                 dev->features |= NETIF_F_IP_CSUM;
796         }
797
798         ret = register_netdev(dev);
799         if (ret < 0)
800                 goto err_iounmap;
801
802         if (velocity_get_link(dev))
803                 netif_carrier_off(dev);
804
805         velocity_print_info(vptr);
806         pci_set_drvdata(pdev, dev);
807         
808         /* and leave the chip powered down */
809         
810         pci_set_power_state(pdev, PCI_D3hot);
811 #ifdef CONFIG_PM
812         {
813                 unsigned long flags;
814
815                 spin_lock_irqsave(&velocity_dev_list_lock, flags);
816                 list_add(&vptr->list, &velocity_dev_list);
817                 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
818         }
819 #endif
820         velocity_nics++;
821 out:
822         return ret;
823
824 err_iounmap:
825         iounmap(regs);
826 err_release_res:
827         pci_release_regions(pdev);
828 err_disable:
829         pci_disable_device(pdev);
830 err_free_dev:
831         free_netdev(dev);
832         goto out;
833 }
834
835 /**
836  *      velocity_print_info     -       per driver data
837  *      @vptr: velocity
838  *
839  *      Print per driver data as the kernel driver finds Velocity
840  *      hardware
841  */
842
843 static void __devinit velocity_print_info(struct velocity_info *vptr)
844 {
845         struct net_device *dev = vptr->dev;
846
847         printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
848         printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", 
849                 dev->name, 
850                 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2], 
851                 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
852 }
853
854 /**
855  *      velocity_init_info      -       init private data
856  *      @pdev: PCI device
857  *      @vptr: Velocity info
858  *      @info: Board type
859  *
860  *      Set up the initial velocity_info struct for the device that has been
861  *      discovered.
862  */
863
864 static void __devinit velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr, struct velocity_info_tbl *info)
865 {
866         memset(vptr, 0, sizeof(struct velocity_info));
867
868         vptr->pdev = pdev;
869         vptr->chip_id = info->chip_id;
870         vptr->io_size = info->io_size;
871         vptr->num_txq = info->txqueue;
872         vptr->multicast_limit = MCAM_SIZE;
873         spin_lock_init(&vptr->lock);
874         INIT_LIST_HEAD(&vptr->list);
875 }
876
877 /**
878  *      velocity_get_pci_info   -       retrieve PCI info for device
879  *      @vptr: velocity device
880  *      @pdev: PCI device it matches
881  *
882  *      Retrieve the PCI configuration space data that interests us from
883  *      the kernel PCI layer
884  */
885
886 static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
887 {
888
889         if(pci_read_config_byte(pdev, PCI_REVISION_ID, &vptr->rev_id) < 0)
890                 return -EIO;
891                 
892         pci_set_master(pdev);
893
894         vptr->ioaddr = pci_resource_start(pdev, 0);
895         vptr->memaddr = pci_resource_start(pdev, 1);
896         
897         if(!(pci_resource_flags(pdev, 0) & IORESOURCE_IO))
898         {
899                 printk(KERN_ERR "%s: region #0 is not an I/O resource, aborting.\n",
900                                 pci_name(pdev));
901                 return -EINVAL;
902         }
903
904         if((pci_resource_flags(pdev, 1) & IORESOURCE_IO))
905         {
906                 printk(KERN_ERR "%s: region #1 is an I/O resource, aborting.\n",
907                                 pci_name(pdev));
908                 return -EINVAL;
909         }
910
911         if(pci_resource_len(pdev, 1) < 256)
912         {
913                 printk(KERN_ERR "%s: region #1 is too small.\n", 
914                                 pci_name(pdev));
915                 return -EINVAL;
916         }
917         vptr->pdev = pdev;
918
919         return 0;
920 }
921
922 /**
923  *      velocity_init_rings     -       set up DMA rings
924  *      @vptr: Velocity to set up
925  *
926  *      Allocate PCI mapped DMA rings for the receive and transmit layer
927  *      to use.
928  */
929
930 static int velocity_init_rings(struct velocity_info *vptr)
931 {
932         int i;
933         unsigned int psize;
934         unsigned int tsize;
935         dma_addr_t pool_dma;
936         u8 *pool;
937
938         /*
939          *      Allocate all RD/TD rings a single pool 
940          */
941          
942         psize = vptr->options.numrx * sizeof(struct rx_desc) + 
943                 vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
944
945         /*
946          * pci_alloc_consistent() fulfills the requirement for 64 bytes
947          * alignment
948          */
949         pool = pci_alloc_consistent(vptr->pdev, psize, &pool_dma);
950
951         if (pool == NULL) {
952                 printk(KERN_ERR "%s : DMA memory allocation failed.\n", 
953                                         vptr->dev->name);
954                 return -ENOMEM;
955         }
956
957         memset(pool, 0, psize);
958
959         vptr->rd_ring = (struct rx_desc *) pool;
960
961         vptr->rd_pool_dma = pool_dma;
962
963         tsize = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
964         vptr->tx_bufs = pci_alloc_consistent(vptr->pdev, tsize, 
965                                                 &vptr->tx_bufs_dma);
966
967         if (vptr->tx_bufs == NULL) {
968                 printk(KERN_ERR "%s: DMA memory allocation failed.\n", 
969                                         vptr->dev->name);
970                 pci_free_consistent(vptr->pdev, psize, pool, pool_dma);
971                 return -ENOMEM;
972         }
973
974         memset(vptr->tx_bufs, 0, vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq);
975
976         i = vptr->options.numrx * sizeof(struct rx_desc);
977         pool += i;
978         pool_dma += i;
979         for (i = 0; i < vptr->num_txq; i++) {
980                 int offset = vptr->options.numtx * sizeof(struct tx_desc);
981
982                 vptr->td_pool_dma[i] = pool_dma;
983                 vptr->td_rings[i] = (struct tx_desc *) pool;
984                 pool += offset;
985                 pool_dma += offset;
986         }
987         return 0;
988 }
989
990 /**
991  *      velocity_free_rings     -       free PCI ring pointers
992  *      @vptr: Velocity to free from
993  *
994  *      Clean up the PCI ring buffers allocated to this velocity.
995  */
996
997 static void velocity_free_rings(struct velocity_info *vptr)
998 {
999         int size;
1000
1001         size = vptr->options.numrx * sizeof(struct rx_desc) + 
1002                vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
1003
1004         pci_free_consistent(vptr->pdev, size, vptr->rd_ring, vptr->rd_pool_dma);
1005
1006         size = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
1007
1008         pci_free_consistent(vptr->pdev, size, vptr->tx_bufs, vptr->tx_bufs_dma);
1009 }
1010
1011 static inline void velocity_give_many_rx_descs(struct velocity_info *vptr)
1012 {
1013         struct mac_regs __iomem *regs = vptr->mac_regs;
1014         int avail, dirty, unusable;
1015
1016         /*
1017          * RD number must be equal to 4X per hardware spec
1018          * (programming guide rev 1.20, p.13)
1019          */
1020         if (vptr->rd_filled < 4)
1021                 return;
1022
1023         wmb();
1024
1025         unusable = vptr->rd_filled & 0x0003;
1026         dirty = vptr->rd_dirty - unusable;
1027         for (avail = vptr->rd_filled & 0xfffc; avail; avail--) {
1028                 dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
1029                 vptr->rd_ring[dirty].rdesc0.owner = OWNED_BY_NIC;
1030         }
1031
1032         writew(vptr->rd_filled & 0xfffc, &regs->RBRDU);
1033         vptr->rd_filled = unusable;
1034 }
1035
1036 static int velocity_rx_refill(struct velocity_info *vptr)
1037 {
1038         int dirty = vptr->rd_dirty, done = 0, ret = 0;
1039
1040         do {
1041                 struct rx_desc *rd = vptr->rd_ring + dirty;
1042
1043                 /* Fine for an all zero Rx desc at init time as well */
1044                 if (rd->rdesc0.owner == OWNED_BY_NIC)
1045                         break;
1046
1047                 if (!vptr->rd_info[dirty].skb) {
1048                         ret = velocity_alloc_rx_buf(vptr, dirty);
1049                         if (ret < 0)
1050                                 break;
1051                 }
1052                 done++;
1053                 dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;      
1054         } while (dirty != vptr->rd_curr);
1055
1056         if (done) {
1057                 vptr->rd_dirty = dirty;
1058                 vptr->rd_filled += done;
1059                 velocity_give_many_rx_descs(vptr);
1060         }
1061
1062         return ret;
1063 }
1064
1065 /**
1066  *      velocity_init_rd_ring   -       set up receive ring
1067  *      @vptr: velocity to configure
1068  *
1069  *      Allocate and set up the receive buffers for each ring slot and
1070  *      assign them to the network adapter.
1071  */
1072
1073 static int velocity_init_rd_ring(struct velocity_info *vptr)
1074 {
1075         int ret = -ENOMEM;
1076         unsigned int rsize = sizeof(struct velocity_rd_info) * 
1077                                         vptr->options.numrx;
1078
1079         vptr->rd_info = kmalloc(rsize, GFP_KERNEL);
1080         if(vptr->rd_info == NULL)
1081                 goto out;
1082         memset(vptr->rd_info, 0, rsize);
1083
1084         vptr->rd_filled = vptr->rd_dirty = vptr->rd_curr = 0;
1085
1086         ret = velocity_rx_refill(vptr);
1087         if (ret < 0) {
1088                 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1089                         "%s: failed to allocate RX buffer.\n", vptr->dev->name);
1090                 velocity_free_rd_ring(vptr);
1091         }
1092 out:
1093         return ret;
1094 }
1095
1096 /**
1097  *      velocity_free_rd_ring   -       free receive ring
1098  *      @vptr: velocity to clean up
1099  *
1100  *      Free the receive buffers for each ring slot and any
1101  *      attached socket buffers that need to go away.
1102  */
1103
1104 static void velocity_free_rd_ring(struct velocity_info *vptr)
1105 {
1106         int i;
1107
1108         if (vptr->rd_info == NULL)
1109                 return;
1110
1111         for (i = 0; i < vptr->options.numrx; i++) {
1112                 struct velocity_rd_info *rd_info = &(vptr->rd_info[i]);
1113                 struct rx_desc *rd = vptr->rd_ring + i;
1114
1115                 memset(rd, 0, sizeof(*rd));
1116
1117                 if (!rd_info->skb)
1118                         continue;
1119                 pci_unmap_single(vptr->pdev, rd_info->skb_dma, vptr->rx_buf_sz,
1120                                  PCI_DMA_FROMDEVICE);
1121                 rd_info->skb_dma = (dma_addr_t) NULL;
1122
1123                 dev_kfree_skb(rd_info->skb);
1124                 rd_info->skb = NULL;
1125         }
1126
1127         kfree(vptr->rd_info);
1128         vptr->rd_info = NULL;
1129 }
1130
1131 /**
1132  *      velocity_init_td_ring   -       set up transmit ring
1133  *      @vptr:  velocity
1134  *
1135  *      Set up the transmit ring and chain the ring pointers together.
1136  *      Returns zero on success or a negative posix errno code for
1137  *      failure.
1138  */
1139  
1140 static int velocity_init_td_ring(struct velocity_info *vptr)
1141 {
1142         int i, j;
1143         dma_addr_t curr;
1144         struct tx_desc *td;
1145         struct velocity_td_info *td_info;
1146         unsigned int tsize = sizeof(struct velocity_td_info) * 
1147                                         vptr->options.numtx;
1148
1149         /* Init the TD ring entries */
1150         for (j = 0; j < vptr->num_txq; j++) {
1151                 curr = vptr->td_pool_dma[j];
1152
1153                 vptr->td_infos[j] = kmalloc(tsize, GFP_KERNEL);
1154                 if(vptr->td_infos[j] == NULL)
1155                 {
1156                         while(--j >= 0)
1157                                 kfree(vptr->td_infos[j]);
1158                         return -ENOMEM;
1159                 }
1160                 memset(vptr->td_infos[j], 0, tsize);
1161
1162                 for (i = 0; i < vptr->options.numtx; i++, curr += sizeof(struct tx_desc)) {
1163                         td = &(vptr->td_rings[j][i]);
1164                         td_info = &(vptr->td_infos[j][i]);
1165                         td_info->buf = vptr->tx_bufs +
1166                                 (j * vptr->options.numtx + i) * PKT_BUF_SZ;
1167                         td_info->buf_dma = vptr->tx_bufs_dma +
1168                                 (j * vptr->options.numtx + i) * PKT_BUF_SZ;
1169                 }
1170                 vptr->td_tail[j] = vptr->td_curr[j] = vptr->td_used[j] = 0;
1171         }
1172         return 0;
1173 }
1174
1175 /*
1176  *      FIXME: could we merge this with velocity_free_tx_buf ?
1177  */
1178
1179 static void velocity_free_td_ring_entry(struct velocity_info *vptr,
1180                                                          int q, int n)
1181 {
1182         struct velocity_td_info * td_info = &(vptr->td_infos[q][n]);
1183         int i;
1184         
1185         if (td_info == NULL)
1186                 return;
1187                 
1188         if (td_info->skb) {
1189                 for (i = 0; i < td_info->nskb_dma; i++)
1190                 {
1191                         if (td_info->skb_dma[i]) {
1192                                 pci_unmap_single(vptr->pdev, td_info->skb_dma[i], 
1193                                         td_info->skb->len, PCI_DMA_TODEVICE);
1194                                 td_info->skb_dma[i] = (dma_addr_t) NULL;
1195                         }
1196                 }
1197                 dev_kfree_skb(td_info->skb);
1198                 td_info->skb = NULL;
1199         }
1200 }
1201
1202 /**
1203  *      velocity_free_td_ring   -       free td ring
1204  *      @vptr: velocity
1205  *
1206  *      Free up the transmit ring for this particular velocity adapter.
1207  *      We free the ring contents but not the ring itself.
1208  */
1209  
1210 static void velocity_free_td_ring(struct velocity_info *vptr)
1211 {
1212         int i, j;
1213
1214         for (j = 0; j < vptr->num_txq; j++) {
1215                 if (vptr->td_infos[j] == NULL)
1216                         continue;
1217                 for (i = 0; i < vptr->options.numtx; i++) {
1218                         velocity_free_td_ring_entry(vptr, j, i);
1219
1220                 }
1221                 kfree(vptr->td_infos[j]);
1222                 vptr->td_infos[j] = NULL;
1223         }
1224 }
1225
1226 /**
1227  *      velocity_rx_srv         -       service RX interrupt
1228  *      @vptr: velocity
1229  *      @status: adapter status (unused)
1230  *
1231  *      Walk the receive ring of the velocity adapter and remove
1232  *      any received packets from the receive queue. Hand the ring
1233  *      slots back to the adapter for reuse.
1234  */
1235  
1236 static int velocity_rx_srv(struct velocity_info *vptr, int status)
1237 {
1238         struct net_device_stats *stats = &vptr->stats;
1239         int rd_curr = vptr->rd_curr;
1240         int works = 0;
1241
1242         do {
1243                 struct rx_desc *rd = vptr->rd_ring + rd_curr;
1244
1245                 if (!vptr->rd_info[rd_curr].skb)
1246                         break;
1247
1248                 if (rd->rdesc0.owner == OWNED_BY_NIC)
1249                         break;
1250
1251                 rmb();
1252
1253                 /*
1254                  *      Don't drop CE or RL error frame although RXOK is off
1255                  */
1256                 if ((rd->rdesc0.RSR & RSR_RXOK) || (!(rd->rdesc0.RSR & RSR_RXOK) && (rd->rdesc0.RSR & (RSR_CE | RSR_RL)))) {
1257                         if (velocity_receive_frame(vptr, rd_curr) < 0)
1258                                 stats->rx_dropped++;
1259                 } else {
1260                         if (rd->rdesc0.RSR & RSR_CRC)
1261                                 stats->rx_crc_errors++;
1262                         if (rd->rdesc0.RSR & RSR_FAE)
1263                                 stats->rx_frame_errors++;
1264
1265                         stats->rx_dropped++;
1266                 }
1267
1268                 rd->inten = 1;
1269
1270                 vptr->dev->last_rx = jiffies;
1271
1272                 rd_curr++;
1273                 if (rd_curr >= vptr->options.numrx)
1274                         rd_curr = 0;
1275         } while (++works <= 15);
1276
1277         vptr->rd_curr = rd_curr;
1278
1279         if (works > 0 && velocity_rx_refill(vptr) < 0) {
1280                 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_ERR
1281                         "%s: rx buf allocation failure\n", vptr->dev->name);
1282         }
1283
1284         VAR_USED(stats);
1285         return works;
1286 }
1287
1288 /**
1289  *      velocity_rx_csum        -       checksum process
1290  *      @rd: receive packet descriptor
1291  *      @skb: network layer packet buffer
1292  *
1293  *      Process the status bits for the received packet and determine
1294  *      if the checksum was computed and verified by the hardware
1295  */
1296  
1297 static inline void velocity_rx_csum(struct rx_desc *rd, struct sk_buff *skb)
1298 {
1299         skb->ip_summed = CHECKSUM_NONE;
1300
1301         if (rd->rdesc1.CSM & CSM_IPKT) {
1302                 if (rd->rdesc1.CSM & CSM_IPOK) {
1303                         if ((rd->rdesc1.CSM & CSM_TCPKT) || 
1304                                         (rd->rdesc1.CSM & CSM_UDPKT)) {
1305                                 if (!(rd->rdesc1.CSM & CSM_TUPOK)) {
1306                                         return;
1307                                 }
1308                         }
1309                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1310                 }
1311         }
1312 }
1313
1314 /**
1315  *      velocity_rx_copy        -       in place Rx copy for small packets
1316  *      @rx_skb: network layer packet buffer candidate
1317  *      @pkt_size: received data size
1318  *      @rd: receive packet descriptor
1319  *      @dev: network device
1320  *
1321  *      Replace the current skb that is scheduled for Rx processing by a
1322  *      shorter, immediatly allocated skb, if the received packet is small
1323  *      enough. This function returns a negative value if the received
1324  *      packet is too big or if memory is exhausted.
1325  */
1326 static inline int velocity_rx_copy(struct sk_buff **rx_skb, int pkt_size,
1327                                    struct velocity_info *vptr)
1328 {
1329         int ret = -1;
1330
1331         if (pkt_size < rx_copybreak) {
1332                 struct sk_buff *new_skb;
1333
1334                 new_skb = dev_alloc_skb(pkt_size + 2);
1335                 if (new_skb) {
1336                         new_skb->dev = vptr->dev;
1337                         new_skb->ip_summed = rx_skb[0]->ip_summed;
1338
1339                         if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN)
1340                                 skb_reserve(new_skb, 2);
1341
1342                         memcpy(new_skb->data, rx_skb[0]->data, pkt_size);
1343                         *rx_skb = new_skb;
1344                         ret = 0;
1345                 }
1346                 
1347         }
1348         return ret;
1349 }
1350
1351 /**
1352  *      velocity_iph_realign    -       IP header alignment
1353  *      @vptr: velocity we are handling
1354  *      @skb: network layer packet buffer
1355  *      @pkt_size: received data size
1356  *
1357  *      Align IP header on a 2 bytes boundary. This behavior can be
1358  *      configured by the user.
1359  */
1360 static inline void velocity_iph_realign(struct velocity_info *vptr,
1361                                         struct sk_buff *skb, int pkt_size)
1362 {
1363         /* FIXME - memmove ? */
1364         if (vptr->flags & VELOCITY_FLAGS_IP_ALIGN) {
1365                 int i;
1366
1367                 for (i = pkt_size; i >= 0; i--)
1368                         *(skb->data + i + 2) = *(skb->data + i);
1369                 skb_reserve(skb, 2);
1370         }
1371 }
1372
1373 /**
1374  *      velocity_receive_frame  -       received packet processor
1375  *      @vptr: velocity we are handling
1376  *      @idx: ring index
1377  *      
1378  *      A packet has arrived. We process the packet and if appropriate
1379  *      pass the frame up the network stack
1380  */
1381  
1382 static int velocity_receive_frame(struct velocity_info *vptr, int idx)
1383 {
1384         void (*pci_action)(struct pci_dev *, dma_addr_t, size_t, int);
1385         struct net_device_stats *stats = &vptr->stats;
1386         struct velocity_rd_info *rd_info = &(vptr->rd_info[idx]);
1387         struct rx_desc *rd = &(vptr->rd_ring[idx]);
1388         int pkt_len = rd->rdesc0.len;
1389         struct sk_buff *skb;
1390
1391         if (rd->rdesc0.RSR & (RSR_STP | RSR_EDP)) {
1392                 VELOCITY_PRT(MSG_LEVEL_VERBOSE, KERN_ERR " %s : the received frame span multple RDs.\n", vptr->dev->name);
1393                 stats->rx_length_errors++;
1394                 return -EINVAL;
1395         }
1396
1397         if (rd->rdesc0.RSR & RSR_MAR)
1398                 vptr->stats.multicast++;
1399
1400         skb = rd_info->skb;
1401         skb->dev = vptr->dev;
1402
1403         pci_dma_sync_single_for_cpu(vptr->pdev, rd_info->skb_dma,
1404                                     vptr->rx_buf_sz, PCI_DMA_FROMDEVICE);
1405
1406         /*
1407          *      Drop frame not meeting IEEE 802.3
1408          */
1409          
1410         if (vptr->flags & VELOCITY_FLAGS_VAL_PKT_LEN) {
1411                 if (rd->rdesc0.RSR & RSR_RL) {
1412                         stats->rx_length_errors++;
1413                         return -EINVAL;
1414                 }
1415         }
1416
1417         pci_action = pci_dma_sync_single_for_device;
1418
1419         velocity_rx_csum(rd, skb);
1420
1421         if (velocity_rx_copy(&skb, pkt_len, vptr) < 0) {
1422                 velocity_iph_realign(vptr, skb, pkt_len);
1423                 pci_action = pci_unmap_single;
1424                 rd_info->skb = NULL;
1425         }
1426
1427         pci_action(vptr->pdev, rd_info->skb_dma, vptr->rx_buf_sz,
1428                    PCI_DMA_FROMDEVICE);
1429
1430         skb_put(skb, pkt_len - 4);
1431         skb->protocol = eth_type_trans(skb, skb->dev);  
1432
1433         stats->rx_bytes += pkt_len;
1434         netif_rx(skb);
1435
1436         return 0;
1437 }
1438
1439 /**
1440  *      velocity_alloc_rx_buf   -       allocate aligned receive buffer
1441  *      @vptr: velocity
1442  *      @idx: ring index
1443  *
1444  *      Allocate a new full sized buffer for the reception of a frame and
1445  *      map it into PCI space for the hardware to use. The hardware
1446  *      requires *64* byte alignment of the buffer which makes life
1447  *      less fun than would be ideal.
1448  */
1449  
1450 static int velocity_alloc_rx_buf(struct velocity_info *vptr, int idx)
1451 {
1452         struct rx_desc *rd = &(vptr->rd_ring[idx]);
1453         struct velocity_rd_info *rd_info = &(vptr->rd_info[idx]);
1454
1455         rd_info->skb = dev_alloc_skb(vptr->rx_buf_sz + 64);
1456         if (rd_info->skb == NULL)
1457                 return -ENOMEM;
1458
1459         /*
1460          *      Do the gymnastics to get the buffer head for data at
1461          *      64byte alignment.
1462          */
1463         skb_reserve(rd_info->skb, (unsigned long) rd_info->skb->data & 63);
1464         rd_info->skb->dev = vptr->dev;
1465         rd_info->skb_dma = pci_map_single(vptr->pdev, rd_info->skb->data, vptr->rx_buf_sz, PCI_DMA_FROMDEVICE);
1466         
1467         /*
1468          *      Fill in the descriptor to match
1469          */     
1470          
1471         *((u32 *) & (rd->rdesc0)) = 0;
1472         rd->len = cpu_to_le32(vptr->rx_buf_sz);
1473         rd->inten = 1;
1474         rd->pa_low = cpu_to_le32(rd_info->skb_dma);
1475         rd->pa_high = 0;
1476         return 0;
1477 }
1478
1479 /**
1480  *      tx_srv          -       transmit interrupt service
1481  *      @vptr; Velocity
1482  *      @status:
1483  *
1484  *      Scan the queues looking for transmitted packets that
1485  *      we can complete and clean up. Update any statistics as
1486  *      neccessary/
1487  */
1488  
1489 static int velocity_tx_srv(struct velocity_info *vptr, u32 status)
1490 {
1491         struct tx_desc *td;
1492         int qnum;
1493         int full = 0;
1494         int idx;
1495         int works = 0;
1496         struct velocity_td_info *tdinfo;
1497         struct net_device_stats *stats = &vptr->stats;
1498
1499         for (qnum = 0; qnum < vptr->num_txq; qnum++) {
1500                 for (idx = vptr->td_tail[qnum]; vptr->td_used[qnum] > 0; 
1501                         idx = (idx + 1) % vptr->options.numtx) {
1502
1503                         /*
1504                          *      Get Tx Descriptor
1505                          */
1506                         td = &(vptr->td_rings[qnum][idx]);
1507                         tdinfo = &(vptr->td_infos[qnum][idx]);
1508
1509                         if (td->tdesc0.owner == OWNED_BY_NIC)
1510                                 break;
1511
1512                         if ((works++ > 15))
1513                                 break;
1514
1515                         if (td->tdesc0.TSR & TSR0_TERR) {
1516                                 stats->tx_errors++;
1517                                 stats->tx_dropped++;
1518                                 if (td->tdesc0.TSR & TSR0_CDH)
1519                                         stats->tx_heartbeat_errors++;
1520                                 if (td->tdesc0.TSR & TSR0_CRS)
1521                                         stats->tx_carrier_errors++;
1522                                 if (td->tdesc0.TSR & TSR0_ABT)
1523                                         stats->tx_aborted_errors++;
1524                                 if (td->tdesc0.TSR & TSR0_OWC)
1525                                         stats->tx_window_errors++;
1526                         } else {
1527                                 stats->tx_packets++;
1528                                 stats->tx_bytes += tdinfo->skb->len;
1529                         }
1530                         velocity_free_tx_buf(vptr, tdinfo);
1531                         vptr->td_used[qnum]--;
1532                 }
1533                 vptr->td_tail[qnum] = idx;
1534
1535                 if (AVAIL_TD(vptr, qnum) < 1) {
1536                         full = 1;
1537                 }
1538         }
1539         /*
1540          *      Look to see if we should kick the transmit network
1541          *      layer for more work.
1542          */
1543         if (netif_queue_stopped(vptr->dev) && (full == 0)
1544             && (!(vptr->mii_status & VELOCITY_LINK_FAIL))) {
1545                 netif_wake_queue(vptr->dev);
1546         }
1547         return works;
1548 }
1549
1550 /**
1551  *      velocity_print_link_status      -       link status reporting
1552  *      @vptr: velocity to report on
1553  *
1554  *      Turn the link status of the velocity card into a kernel log
1555  *      description of the new link state, detailing speed and duplex
1556  *      status
1557  */
1558
1559 static void velocity_print_link_status(struct velocity_info *vptr)
1560 {
1561
1562         if (vptr->mii_status & VELOCITY_LINK_FAIL) {
1563                 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: failed to detect cable link\n", vptr->dev->name);
1564         } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1565                 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link autonegation", vptr->dev->name);
1566
1567                 if (vptr->mii_status & VELOCITY_SPEED_1000)
1568                         VELOCITY_PRT(MSG_LEVEL_INFO, " speed 1000M bps");
1569                 else if (vptr->mii_status & VELOCITY_SPEED_100)
1570                         VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps");
1571                 else
1572                         VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps");
1573
1574                 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
1575                         VELOCITY_PRT(MSG_LEVEL_INFO, " full duplex\n");
1576                 else
1577                         VELOCITY_PRT(MSG_LEVEL_INFO, " half duplex\n");
1578         } else {
1579                 VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: Link forced", vptr->dev->name);
1580                 switch (vptr->options.spd_dpx) {
1581                 case SPD_DPX_100_HALF:
1582                         VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps half duplex\n");
1583                         break;
1584                 case SPD_DPX_100_FULL:
1585                         VELOCITY_PRT(MSG_LEVEL_INFO, " speed 100M bps full duplex\n");
1586                         break;
1587                 case SPD_DPX_10_HALF:
1588                         VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps half duplex\n");
1589                         break;
1590                 case SPD_DPX_10_FULL:
1591                         VELOCITY_PRT(MSG_LEVEL_INFO, " speed 10M bps full duplex\n");
1592                         break;
1593                 default:
1594                         break;
1595                 }
1596         }
1597 }
1598
1599 /**
1600  *      velocity_error  -       handle error from controller
1601  *      @vptr: velocity
1602  *      @status: card status
1603  *
1604  *      Process an error report from the hardware and attempt to recover
1605  *      the card itself. At the moment we cannot recover from some 
1606  *      theoretically impossible errors but this could be fixed using
1607  *      the pci_device_failed logic to bounce the hardware
1608  *
1609  */
1610  
1611 static void velocity_error(struct velocity_info *vptr, int status)
1612 {
1613
1614         if (status & ISR_TXSTLI) {
1615                 struct mac_regs __iomem * regs = vptr->mac_regs;
1616
1617                 printk(KERN_ERR "TD structure errror TDindex=%hx\n", readw(&regs->TDIdx[0]));
1618                 BYTE_REG_BITS_ON(TXESR_TDSTR, &regs->TXESR);
1619                 writew(TRDCSR_RUN, &regs->TDCSRClr);
1620                 netif_stop_queue(vptr->dev);
1621                 
1622                 /* FIXME: port over the pci_device_failed code and use it
1623                    here */
1624         }
1625
1626         if (status & ISR_SRCI) {
1627                 struct mac_regs __iomem * regs = vptr->mac_regs;
1628                 int linked;
1629
1630                 if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
1631                         vptr->mii_status = check_connection_type(regs);
1632
1633                         /*
1634                          *      If it is a 3119, disable frame bursting in 
1635                          *      halfduplex mode and enable it in fullduplex
1636                          *       mode
1637                          */
1638                         if (vptr->rev_id < REV_ID_VT3216_A0) {
1639                                 if (vptr->mii_status | VELOCITY_DUPLEX_FULL)
1640                                         BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
1641                                 else
1642                                         BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
1643                         }
1644                         /*
1645                          *      Only enable CD heart beat counter in 10HD mode
1646                          */
1647                         if (!(vptr->mii_status & VELOCITY_DUPLEX_FULL) && (vptr->mii_status & VELOCITY_SPEED_10)) {
1648                                 BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
1649                         } else {
1650                                 BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
1651                         }
1652                 }
1653                 /*
1654                  *      Get link status from PHYSR0
1655                  */
1656                 linked = readb(&regs->PHYSR0) & PHYSR0_LINKGD;
1657
1658                 if (linked) {
1659                         vptr->mii_status &= ~VELOCITY_LINK_FAIL;
1660                         netif_carrier_on(vptr->dev);
1661                 } else {
1662                         vptr->mii_status |= VELOCITY_LINK_FAIL;
1663                         netif_carrier_off(vptr->dev);
1664                 }
1665
1666                 velocity_print_link_status(vptr);
1667                 enable_flow_control_ability(vptr);
1668
1669                 /*
1670                  *      Re-enable auto-polling because SRCI will disable 
1671                  *      auto-polling
1672                  */
1673                  
1674                 enable_mii_autopoll(regs);
1675
1676                 if (vptr->mii_status & VELOCITY_LINK_FAIL)
1677                         netif_stop_queue(vptr->dev);
1678                 else
1679                         netif_wake_queue(vptr->dev);
1680
1681         };
1682         if (status & ISR_MIBFI)
1683                 velocity_update_hw_mibs(vptr);
1684         if (status & ISR_LSTEI)
1685                 mac_rx_queue_wake(vptr->mac_regs);
1686 }
1687
1688 /**
1689  *      velocity_free_tx_buf    -       free transmit buffer
1690  *      @vptr: velocity
1691  *      @tdinfo: buffer
1692  *
1693  *      Release an transmit buffer. If the buffer was preallocated then
1694  *      recycle it, if not then unmap the buffer.
1695  */
1696  
1697 static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *tdinfo)
1698 {
1699         struct sk_buff *skb = tdinfo->skb;
1700         int i;
1701
1702         /*
1703          *      Don't unmap the pre-allocated tx_bufs
1704          */
1705         if (tdinfo->skb_dma && (tdinfo->skb_dma[0] != tdinfo->buf_dma)) {
1706
1707                 for (i = 0; i < tdinfo->nskb_dma; i++) {
1708 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1709                         pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], td->tdesc1.len, PCI_DMA_TODEVICE);
1710 #else
1711                         pci_unmap_single(vptr->pdev, tdinfo->skb_dma[i], skb->len, PCI_DMA_TODEVICE);
1712 #endif
1713                         tdinfo->skb_dma[i] = 0;
1714                 }
1715         }
1716         dev_kfree_skb_irq(skb);
1717         tdinfo->skb = NULL;
1718 }
1719
1720 /**
1721  *      velocity_open           -       interface activation callback
1722  *      @dev: network layer device to open
1723  *
1724  *      Called when the network layer brings the interface up. Returns
1725  *      a negative posix error code on failure, or zero on success.
1726  *
1727  *      All the ring allocation and set up is done on open for this
1728  *      adapter to minimise memory usage when inactive
1729  */
1730  
1731 static int velocity_open(struct net_device *dev)
1732 {
1733         struct velocity_info *vptr = dev->priv;
1734         int ret;
1735
1736         vptr->rx_buf_sz = (dev->mtu <= 1504 ? PKT_BUF_SZ : dev->mtu + 32);
1737
1738         ret = velocity_init_rings(vptr);
1739         if (ret < 0)
1740                 goto out;
1741
1742         ret = velocity_init_rd_ring(vptr);
1743         if (ret < 0)
1744                 goto err_free_desc_rings;
1745
1746         ret = velocity_init_td_ring(vptr);
1747         if (ret < 0)
1748                 goto err_free_rd_ring;
1749         
1750         /* Ensure chip is running */    
1751         pci_set_power_state(vptr->pdev, PCI_D0);
1752         
1753         velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1754
1755         ret = request_irq(vptr->pdev->irq, &velocity_intr, SA_SHIRQ,
1756                           dev->name, dev);
1757         if (ret < 0) {
1758                 /* Power down the chip */
1759                 pci_set_power_state(vptr->pdev, PCI_D3hot);
1760                 goto err_free_td_ring;
1761         }
1762
1763         mac_enable_int(vptr->mac_regs);
1764         netif_start_queue(dev);
1765         vptr->flags |= VELOCITY_FLAGS_OPENED;
1766 out:
1767         return ret;
1768
1769 err_free_td_ring:
1770         velocity_free_td_ring(vptr);
1771 err_free_rd_ring:
1772         velocity_free_rd_ring(vptr);
1773 err_free_desc_rings:
1774         velocity_free_rings(vptr);
1775         goto out;
1776 }
1777
1778 /** 
1779  *      velocity_change_mtu     -       MTU change callback
1780  *      @dev: network device
1781  *      @new_mtu: desired MTU
1782  *
1783  *      Handle requests from the networking layer for MTU change on
1784  *      this interface. It gets called on a change by the network layer.
1785  *      Return zero for success or negative posix error code.
1786  */
1787  
1788 static int velocity_change_mtu(struct net_device *dev, int new_mtu)
1789 {
1790         struct velocity_info *vptr = dev->priv;
1791         unsigned long flags;
1792         int oldmtu = dev->mtu;
1793         int ret = 0;
1794
1795         if ((new_mtu < VELOCITY_MIN_MTU) || new_mtu > (VELOCITY_MAX_MTU)) {
1796                 VELOCITY_PRT(MSG_LEVEL_ERR, KERN_NOTICE "%s: Invalid MTU.\n", 
1797                                 vptr->dev->name);
1798                 return -EINVAL;
1799         }
1800
1801         if (new_mtu != oldmtu) {
1802                 spin_lock_irqsave(&vptr->lock, flags);
1803
1804                 netif_stop_queue(dev);
1805                 velocity_shutdown(vptr);
1806
1807                 velocity_free_td_ring(vptr);
1808                 velocity_free_rd_ring(vptr);
1809
1810                 dev->mtu = new_mtu;
1811                 if (new_mtu > 8192)
1812                         vptr->rx_buf_sz = 9 * 1024;
1813                 else if (new_mtu > 4096)
1814                         vptr->rx_buf_sz = 8192;
1815                 else
1816                         vptr->rx_buf_sz = 4 * 1024;
1817
1818                 ret = velocity_init_rd_ring(vptr);
1819                 if (ret < 0)
1820                         goto out_unlock;
1821
1822                 ret = velocity_init_td_ring(vptr);
1823                 if (ret < 0)
1824                         goto out_unlock;
1825
1826                 velocity_init_registers(vptr, VELOCITY_INIT_COLD);
1827
1828                 mac_enable_int(vptr->mac_regs);
1829                 netif_start_queue(dev);
1830 out_unlock:
1831                 spin_unlock_irqrestore(&vptr->lock, flags);
1832         }
1833
1834         return ret;
1835 }
1836
1837 /**
1838  *      velocity_shutdown       -       shut down the chip
1839  *      @vptr: velocity to deactivate
1840  *
1841  *      Shuts down the internal operations of the velocity and
1842  *      disables interrupts, autopolling, transmit and receive
1843  */
1844  
1845 static void velocity_shutdown(struct velocity_info *vptr)
1846 {
1847         struct mac_regs __iomem * regs = vptr->mac_regs;
1848         mac_disable_int(regs);
1849         writel(CR0_STOP, &regs->CR0Set);
1850         writew(0xFFFF, &regs->TDCSRClr);
1851         writeb(0xFF, &regs->RDCSRClr);
1852         safe_disable_mii_autopoll(regs);
1853         mac_clear_isr(regs);
1854 }
1855
1856 /**
1857  *      velocity_close          -       close adapter callback
1858  *      @dev: network device
1859  *
1860  *      Callback from the network layer when the velocity is being
1861  *      deactivated by the network layer
1862  */
1863
1864 static int velocity_close(struct net_device *dev)
1865 {
1866         struct velocity_info *vptr = dev->priv;
1867
1868         netif_stop_queue(dev);
1869         velocity_shutdown(vptr);
1870
1871         if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED)
1872                 velocity_get_ip(vptr);
1873         if (dev->irq != 0)
1874                 free_irq(dev->irq, dev);
1875                 
1876         /* Power down the chip */
1877         pci_set_power_state(vptr->pdev, PCI_D3hot);
1878         
1879         /* Free the resources */
1880         velocity_free_td_ring(vptr);
1881         velocity_free_rd_ring(vptr);
1882         velocity_free_rings(vptr);
1883
1884         vptr->flags &= (~VELOCITY_FLAGS_OPENED);
1885         return 0;
1886 }
1887
1888 /**
1889  *      velocity_xmit           -       transmit packet callback
1890  *      @skb: buffer to transmit
1891  *      @dev: network device
1892  *
1893  *      Called by the networ layer to request a packet is queued to
1894  *      the velocity. Returns zero on success.
1895  */
1896  
1897 static int velocity_xmit(struct sk_buff *skb, struct net_device *dev)
1898 {
1899         struct velocity_info *vptr = dev->priv;
1900         int qnum = 0;
1901         struct tx_desc *td_ptr;
1902         struct velocity_td_info *tdinfo;
1903         unsigned long flags;
1904         int index;
1905
1906         int pktlen = skb->len;
1907
1908 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1909         if (skb_shinfo(skb)->nr_frags > 6 && __skb_linearize(skb)) {
1910                 kfree_skb(skb);
1911                 return 0;
1912         }
1913 #endif
1914
1915         spin_lock_irqsave(&vptr->lock, flags);
1916
1917         index = vptr->td_curr[qnum];
1918         td_ptr = &(vptr->td_rings[qnum][index]);
1919         tdinfo = &(vptr->td_infos[qnum][index]);
1920
1921         td_ptr->tdesc1.TCPLS = TCPLS_NORMAL;
1922         td_ptr->tdesc1.TCR = TCR0_TIC;
1923         td_ptr->td_buf[0].queue = 0;
1924
1925         /*
1926          *      Pad short frames. 
1927          */
1928         if (pktlen < ETH_ZLEN) {
1929                 /* Cannot occur until ZC support */
1930                 pktlen = ETH_ZLEN;
1931                 memcpy(tdinfo->buf, skb->data, skb->len);
1932                 memset(tdinfo->buf + skb->len, 0, ETH_ZLEN - skb->len);
1933                 tdinfo->skb = skb;
1934                 tdinfo->skb_dma[0] = tdinfo->buf_dma;
1935                 td_ptr->tdesc0.pktsize = pktlen;
1936                 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1937                 td_ptr->td_buf[0].pa_high = 0;
1938                 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1939                 tdinfo->nskb_dma = 1;
1940                 td_ptr->tdesc1.CMDZ = 2;
1941         } else
1942 #ifdef VELOCITY_ZERO_COPY_SUPPORT
1943         if (skb_shinfo(skb)->nr_frags > 0) {
1944                 int nfrags = skb_shinfo(skb)->nr_frags;
1945                 tdinfo->skb = skb;
1946                 if (nfrags > 6) {
1947                         memcpy(tdinfo->buf, skb->data, skb->len);
1948                         tdinfo->skb_dma[0] = tdinfo->buf_dma;
1949                         td_ptr->tdesc0.pktsize = 
1950                         td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1951                         td_ptr->td_buf[0].pa_high = 0;
1952                         td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1953                         tdinfo->nskb_dma = 1;
1954                         td_ptr->tdesc1.CMDZ = 2;
1955                 } else {
1956                         int i = 0;
1957                         tdinfo->nskb_dma = 0;
1958                         tdinfo->skb_dma[i] = pci_map_single(vptr->pdev, skb->data, skb->len - skb->data_len, PCI_DMA_TODEVICE);
1959
1960                         td_ptr->tdesc0.pktsize = pktlen;
1961
1962                         /* FIXME: support 48bit DMA later */
1963                         td_ptr->td_buf[i].pa_low = cpu_to_le32(tdinfo->skb_dma);
1964                         td_ptr->td_buf[i].pa_high = 0;
1965                         td_ptr->td_buf[i].bufsize = skb->len->skb->data_len;
1966
1967                         for (i = 0; i < nfrags; i++) {
1968                                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1969                                 void *addr = ((void *) page_address(frag->page + frag->page_offset));
1970
1971                                 tdinfo->skb_dma[i + 1] = pci_map_single(vptr->pdev, addr, frag->size, PCI_DMA_TODEVICE);
1972
1973                                 td_ptr->td_buf[i + 1].pa_low = cpu_to_le32(tdinfo->skb_dma[i + 1]);
1974                                 td_ptr->td_buf[i + 1].pa_high = 0;
1975                                 td_ptr->td_buf[i + 1].bufsize = frag->size;
1976                         }
1977                         tdinfo->nskb_dma = i - 1;
1978                         td_ptr->tdesc1.CMDZ = i;
1979                 }
1980
1981         } else
1982 #endif
1983         {
1984                 /*
1985                  *      Map the linear network buffer into PCI space and
1986                  *      add it to the transmit ring.
1987                  */
1988                 tdinfo->skb = skb;
1989                 tdinfo->skb_dma[0] = pci_map_single(vptr->pdev, skb->data, pktlen, PCI_DMA_TODEVICE);
1990                 td_ptr->tdesc0.pktsize = pktlen;
1991                 td_ptr->td_buf[0].pa_low = cpu_to_le32(tdinfo->skb_dma[0]);
1992                 td_ptr->td_buf[0].pa_high = 0;
1993                 td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
1994                 tdinfo->nskb_dma = 1;
1995                 td_ptr->tdesc1.CMDZ = 2;
1996         }
1997
1998         if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
1999                 td_ptr->tdesc1.pqinf.VID = (vptr->options.vid & 0xfff);
2000                 td_ptr->tdesc1.pqinf.priority = 0;
2001                 td_ptr->tdesc1.pqinf.CFI = 0;
2002                 td_ptr->tdesc1.TCR |= TCR0_VETAG;
2003         }
2004
2005         /*
2006          *      Handle hardware checksum
2007          */
2008         if ((vptr->flags & VELOCITY_FLAGS_TX_CSUM)
2009                                  && (skb->ip_summed == CHECKSUM_HW)) {
2010                 struct iphdr *ip = skb->nh.iph;
2011                 if (ip->protocol == IPPROTO_TCP)
2012                         td_ptr->tdesc1.TCR |= TCR0_TCPCK;
2013                 else if (ip->protocol == IPPROTO_UDP)
2014                         td_ptr->tdesc1.TCR |= (TCR0_UDPCK);
2015                 td_ptr->tdesc1.TCR |= TCR0_IPCK;
2016         }
2017         {
2018
2019                 int prev = index - 1;
2020
2021                 if (prev < 0)
2022                         prev = vptr->options.numtx - 1;
2023                 td_ptr->tdesc0.owner = OWNED_BY_NIC;
2024                 vptr->td_used[qnum]++;
2025                 vptr->td_curr[qnum] = (index + 1) % vptr->options.numtx;
2026
2027                 if (AVAIL_TD(vptr, qnum) < 1)
2028                         netif_stop_queue(dev);
2029
2030                 td_ptr = &(vptr->td_rings[qnum][prev]);
2031                 td_ptr->td_buf[0].queue = 1;
2032                 mac_tx_queue_wake(vptr->mac_regs, qnum);
2033         }
2034         dev->trans_start = jiffies;
2035         spin_unlock_irqrestore(&vptr->lock, flags);
2036         return 0;
2037 }
2038
2039 /**
2040  *      velocity_intr           -       interrupt callback
2041  *      @irq: interrupt number
2042  *      @dev_instance: interrupting device
2043  *      @pt_regs: CPU register state at interrupt
2044  *
2045  *      Called whenever an interrupt is generated by the velocity
2046  *      adapter IRQ line. We may not be the source of the interrupt
2047  *      and need to identify initially if we are, and if not exit as
2048  *      efficiently as possible.
2049  */
2050  
2051 static int velocity_intr(int irq, void *dev_instance, struct pt_regs *regs)
2052 {
2053         struct net_device *dev = dev_instance;
2054         struct velocity_info *vptr = dev->priv;
2055         u32 isr_status;
2056         int max_count = 0;
2057
2058
2059         spin_lock(&vptr->lock);
2060         isr_status = mac_read_isr(vptr->mac_regs);
2061
2062         /* Not us ? */
2063         if (isr_status == 0) {
2064                 spin_unlock(&vptr->lock);
2065                 return IRQ_NONE;
2066         }
2067
2068         mac_disable_int(vptr->mac_regs);
2069
2070         /*
2071          *      Keep processing the ISR until we have completed
2072          *      processing and the isr_status becomes zero
2073          */
2074          
2075         while (isr_status != 0) {
2076                 mac_write_isr(vptr->mac_regs, isr_status);
2077                 if (isr_status & (~(ISR_PRXI | ISR_PPRXI | ISR_PTXI | ISR_PPTXI)))
2078                         velocity_error(vptr, isr_status);
2079                 if (isr_status & (ISR_PRXI | ISR_PPRXI))
2080                         max_count += velocity_rx_srv(vptr, isr_status);
2081                 if (isr_status & (ISR_PTXI | ISR_PPTXI))
2082                         max_count += velocity_tx_srv(vptr, isr_status);
2083                 isr_status = mac_read_isr(vptr->mac_regs);
2084                 if (max_count > vptr->options.int_works)
2085                 {
2086                         printk(KERN_WARNING "%s: excessive work at interrupt.\n", 
2087                                 dev->name);
2088                         max_count = 0;
2089                 }
2090         }
2091         spin_unlock(&vptr->lock);
2092         mac_enable_int(vptr->mac_regs);
2093         return IRQ_HANDLED;
2094
2095 }
2096
2097
2098 /**
2099  *      velocity_set_multi      -       filter list change callback
2100  *      @dev: network device
2101  *
2102  *      Called by the network layer when the filter lists need to change
2103  *      for a velocity adapter. Reload the CAMs with the new address
2104  *      filter ruleset.
2105  */
2106  
2107 static void velocity_set_multi(struct net_device *dev)
2108 {
2109         struct velocity_info *vptr = dev->priv;
2110         struct mac_regs __iomem * regs = vptr->mac_regs;
2111         u8 rx_mode;
2112         int i;
2113         struct dev_mc_list *mclist;
2114
2115         if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2116                 /* Unconditionally log net taps. */
2117                 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
2118                 writel(0xffffffff, &regs->MARCAM[0]);
2119                 writel(0xffffffff, &regs->MARCAM[4]);
2120                 rx_mode = (RCR_AM | RCR_AB | RCR_PROM);
2121         } else if ((dev->mc_count > vptr->multicast_limit)
2122                    || (dev->flags & IFF_ALLMULTI)) {
2123                 writel(0xffffffff, &regs->MARCAM[0]);
2124                 writel(0xffffffff, &regs->MARCAM[4]);
2125                 rx_mode = (RCR_AM | RCR_AB);
2126         } else {
2127                 int offset = MCAM_SIZE - vptr->multicast_limit;
2128                 mac_get_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
2129
2130                 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) {
2131                         mac_set_cam(regs, i + offset, mclist->dmi_addr, VELOCITY_MULTICAST_CAM);
2132                         vptr->mCAMmask[(offset + i) / 8] |= 1 << ((offset + i) & 7);
2133                 }
2134
2135                 mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
2136                 rx_mode = (RCR_AM | RCR_AB);
2137         }
2138         if (dev->mtu > 1500)
2139                 rx_mode |= RCR_AL;
2140
2141         BYTE_REG_BITS_ON(rx_mode, &regs->RCR);
2142
2143 }
2144
2145 /**
2146  *      velocity_get_status     -       statistics callback
2147  *      @dev: network device
2148  *
2149  *      Callback from the network layer to allow driver statistics
2150  *      to be resynchronized with hardware collected state. In the
2151  *      case of the velocity we need to pull the MIB counters from
2152  *      the hardware into the counters before letting the network
2153  *      layer display them.
2154  */
2155  
2156 static struct net_device_stats *velocity_get_stats(struct net_device *dev)
2157 {
2158         struct velocity_info *vptr = dev->priv;
2159         
2160         /* If the hardware is down, don't touch MII */
2161         if(!netif_running(dev))
2162                 return &vptr->stats;
2163
2164         spin_lock_irq(&vptr->lock);
2165         velocity_update_hw_mibs(vptr);
2166         spin_unlock_irq(&vptr->lock);
2167
2168         vptr->stats.rx_packets = vptr->mib_counter[HW_MIB_ifRxAllPkts];
2169         vptr->stats.rx_errors = vptr->mib_counter[HW_MIB_ifRxErrorPkts];
2170         vptr->stats.rx_length_errors = vptr->mib_counter[HW_MIB_ifInRangeLengthErrors];
2171
2172 //  unsigned long   rx_dropped;     /* no space in linux buffers    */
2173         vptr->stats.collisions = vptr->mib_counter[HW_MIB_ifTxEtherCollisions];
2174         /* detailed rx_errors: */
2175 //  unsigned long   rx_length_errors;
2176 //  unsigned long   rx_over_errors;     /* receiver ring buff overflow  */
2177         vptr->stats.rx_crc_errors = vptr->mib_counter[HW_MIB_ifRxPktCRCE];
2178 //  unsigned long   rx_frame_errors;    /* recv'd frame alignment error */
2179 //  unsigned long   rx_fifo_errors;     /* recv'r fifo overrun      */
2180 //  unsigned long   rx_missed_errors;   /* receiver missed packet   */
2181
2182         /* detailed tx_errors */
2183 //  unsigned long   tx_fifo_errors;
2184
2185         return &vptr->stats;
2186 }
2187
2188
2189 /**
2190  *      velocity_ioctl          -       ioctl entry point
2191  *      @dev: network device
2192  *      @rq: interface request ioctl
2193  *      @cmd: command code
2194  *
2195  *      Called when the user issues an ioctl request to the network
2196  *      device in question. The velocity interface supports MII.
2197  */
2198  
2199 static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2200 {
2201         struct velocity_info *vptr = dev->priv;
2202         int ret;
2203
2204         /* If we are asked for information and the device is power
2205            saving then we need to bring the device back up to talk to it */
2206                 
2207         if (!netif_running(dev))
2208                 pci_set_power_state(vptr->pdev, PCI_D0);
2209                 
2210         switch (cmd) {
2211         case SIOCGMIIPHY:       /* Get address of MII PHY in use. */
2212         case SIOCGMIIREG:       /* Read MII PHY register. */
2213         case SIOCSMIIREG:       /* Write to MII PHY register. */
2214                 ret = velocity_mii_ioctl(dev, rq, cmd);
2215                 break;
2216
2217         default:
2218                 ret = -EOPNOTSUPP;
2219         }
2220         if (!netif_running(dev))
2221                 pci_set_power_state(vptr->pdev, PCI_D3hot);
2222                 
2223                 
2224         return ret;
2225 }
2226
2227 /*
2228  *      Definition for our device driver. The PCI layer interface
2229  *      uses this to handle all our card discover and plugging
2230  */
2231  
2232 static struct pci_driver velocity_driver = {
2233       .name     = VELOCITY_NAME,
2234       .id_table = velocity_id_table,
2235       .probe    = velocity_found1,
2236       .remove   = __devexit_p(velocity_remove1),
2237 #ifdef CONFIG_PM
2238       .suspend  = velocity_suspend,
2239       .resume   = velocity_resume,
2240 #endif
2241 };
2242
2243 /**
2244  *      velocity_init_module    -       load time function
2245  *
2246  *      Called when the velocity module is loaded. The PCI driver
2247  *      is registered with the PCI layer, and in turn will call
2248  *      the probe functions for each velocity adapter installed
2249  *      in the system.
2250  */
2251  
2252 static int __init velocity_init_module(void)
2253 {
2254         int ret;
2255
2256         velocity_register_notifier();
2257         ret = pci_module_init(&velocity_driver);
2258         if (ret < 0)
2259                 velocity_unregister_notifier();
2260         return ret;
2261 }
2262
2263 /**
2264  *      velocity_cleanup        -       module unload
2265  *
2266  *      When the velocity hardware is unloaded this function is called.
2267  *      It will clean up the notifiers and the unregister the PCI 
2268  *      driver interface for this hardware. This in turn cleans up
2269  *      all discovered interfaces before returning from the function
2270  */
2271  
2272 static void __exit velocity_cleanup_module(void)
2273 {
2274         velocity_unregister_notifier();
2275         pci_unregister_driver(&velocity_driver);
2276 }
2277
2278 module_init(velocity_init_module);
2279 module_exit(velocity_cleanup_module);
2280
2281
2282 /*
2283  * MII access , media link mode setting functions
2284  */
2285  
2286  
2287 /**
2288  *      mii_init        -       set up MII
2289  *      @vptr: velocity adapter
2290  *      @mii_status:  links tatus
2291  *
2292  *      Set up the PHY for the current link state.
2293  */
2294  
2295 static void mii_init(struct velocity_info *vptr, u32 mii_status)
2296 {
2297         u16 BMCR;
2298
2299         switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
2300         case PHYID_CICADA_CS8201:
2301                 /*
2302                  *      Reset to hardware default
2303                  */
2304                 MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2305                 /*
2306                  *      Turn on ECHODIS bit in NWay-forced full mode and turn it
2307                  *      off it in NWay-forced half mode for NWay-forced v.s. 
2308                  *      legacy-forced issue.
2309                  */
2310                 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2311                         MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2312                 else
2313                         MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2314                 /*
2315                  *      Turn on Link/Activity LED enable bit for CIS8201
2316                  */
2317                 MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
2318                 break;
2319         case PHYID_VT3216_32BIT:
2320         case PHYID_VT3216_64BIT:
2321                 /*
2322                  *      Reset to hardware default
2323                  */
2324                 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2325                 /*
2326                  *      Turn on ECHODIS bit in NWay-forced full mode and turn it
2327                  *      off it in NWay-forced half mode for NWay-forced v.s. 
2328                  *      legacy-forced issue
2329                  */
2330                 if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
2331                         MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2332                 else
2333                         MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR, vptr->mac_regs);
2334                 break;
2335
2336         case PHYID_MARVELL_1000:
2337         case PHYID_MARVELL_1000S:
2338                 /*
2339                  *      Assert CRS on Transmit 
2340                  */
2341                 MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
2342                 /*
2343                  *      Reset to hardware default 
2344                  */
2345                 MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR, vptr->mac_regs);
2346                 break;
2347         default:
2348                 ;
2349         }
2350         velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
2351         if (BMCR & BMCR_ISO) {
2352                 BMCR &= ~BMCR_ISO;
2353                 velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
2354         }
2355 }
2356
2357 /**
2358  *      safe_disable_mii_autopoll       -       autopoll off
2359  *      @regs: velocity registers
2360  *
2361  *      Turn off the autopoll and wait for it to disable on the chip
2362  */
2363  
2364 static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs)
2365 {
2366         u16 ww;
2367
2368         /*  turn off MAUTO */
2369         writeb(0, &regs->MIICR);
2370         for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2371                 udelay(1);
2372                 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2373                         break;
2374         }
2375 }
2376
2377 /**
2378  *      enable_mii_autopoll     -       turn on autopolling
2379  *      @regs: velocity registers
2380  *
2381  *      Enable the MII link status autopoll feature on the Velocity
2382  *      hardware. Wait for it to enable.
2383  */
2384
2385 static void enable_mii_autopoll(struct mac_regs __iomem * regs)
2386 {
2387         int ii;
2388
2389         writeb(0, &(regs->MIICR));
2390         writeb(MIIADR_SWMPL, &regs->MIIADR);
2391
2392         for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2393                 udelay(1);
2394                 if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2395                         break;
2396         }
2397
2398         writeb(MIICR_MAUTO, &regs->MIICR);
2399
2400         for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
2401                 udelay(1);
2402                 if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
2403                         break;
2404         }
2405
2406 }
2407
2408 /**
2409  *      velocity_mii_read       -       read MII data
2410  *      @regs: velocity registers
2411  *      @index: MII register index
2412  *      @data: buffer for received data
2413  *
2414  *      Perform a single read of an MII 16bit register. Returns zero
2415  *      on success or -ETIMEDOUT if the PHY did not respond.
2416  */
2417  
2418 static int velocity_mii_read(struct mac_regs __iomem *regs, u8 index, u16 *data)
2419 {
2420         u16 ww;
2421
2422         /*
2423          *      Disable MIICR_MAUTO, so that mii addr can be set normally
2424          */
2425         safe_disable_mii_autopoll(regs);
2426
2427         writeb(index, &regs->MIIADR);
2428
2429         BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
2430
2431         for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2432                 if (!(readb(&regs->MIICR) & MIICR_RCMD))
2433                         break;
2434         }
2435
2436         *data = readw(&regs->MIIDATA);
2437
2438         enable_mii_autopoll(regs);
2439         if (ww == W_MAX_TIMEOUT)
2440                 return -ETIMEDOUT;
2441         return 0;
2442 }
2443
2444 /**
2445  *      velocity_mii_write      -       write MII data
2446  *      @regs: velocity registers
2447  *      @index: MII register index
2448  *      @data: 16bit data for the MII register
2449  *
2450  *      Perform a single write to an MII 16bit register. Returns zero
2451  *      on success or -ETIMEDOUT if the PHY did not respond.
2452  */
2453  
2454 static int velocity_mii_write(struct mac_regs __iomem *regs, u8 mii_addr, u16 data)
2455 {
2456         u16 ww;
2457
2458         /*
2459          *      Disable MIICR_MAUTO, so that mii addr can be set normally
2460          */
2461         safe_disable_mii_autopoll(regs);
2462
2463         /* MII reg offset */
2464         writeb(mii_addr, &regs->MIIADR);
2465         /* set MII data */
2466         writew(data, &regs->MIIDATA);
2467
2468         /* turn on MIICR_WCMD */
2469         BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
2470
2471         /* W_MAX_TIMEOUT is the timeout period */
2472         for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2473                 udelay(5);
2474                 if (!(readb(&regs->MIICR) & MIICR_WCMD))
2475                         break;
2476         }
2477         enable_mii_autopoll(regs);
2478
2479         if (ww == W_MAX_TIMEOUT)
2480                 return -ETIMEDOUT;
2481         return 0;
2482 }
2483
2484 /**
2485  *      velocity_get_opt_media_mode     -       get media selection
2486  *      @vptr: velocity adapter
2487  *
2488  *      Get the media mode stored in EEPROM or module options and load
2489  *      mii_status accordingly. The requested link state information
2490  *      is also returned.
2491  */
2492  
2493 static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
2494 {
2495         u32 status = 0;
2496
2497         switch (vptr->options.spd_dpx) {
2498         case SPD_DPX_AUTO:
2499                 status = VELOCITY_AUTONEG_ENABLE;
2500                 break;
2501         case SPD_DPX_100_FULL:
2502                 status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
2503                 break;
2504         case SPD_DPX_10_FULL:
2505                 status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
2506                 break;
2507         case SPD_DPX_100_HALF:
2508                 status = VELOCITY_SPEED_100;
2509                 break;
2510         case SPD_DPX_10_HALF:
2511                 status = VELOCITY_SPEED_10;
2512                 break;
2513         }
2514         vptr->mii_status = status;
2515         return status;
2516 }
2517
2518 /**
2519  *      mii_set_auto_on         -       autonegotiate on
2520  *      @vptr: velocity
2521  *
2522  *      Enable autonegotation on this interface
2523  */
2524  
2525 static void mii_set_auto_on(struct velocity_info *vptr)
2526 {
2527         if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
2528                 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
2529         else
2530                 MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2531 }
2532
2533
2534 /*
2535 static void mii_set_auto_off(struct velocity_info * vptr)
2536 {
2537     MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
2538 }
2539 */
2540
2541 /**
2542  *      set_mii_flow_control    -       flow control setup
2543  *      @vptr: velocity interface
2544  *
2545  *      Set up the flow control on this interface according to
2546  *      the supplied user/eeprom options.
2547  */
2548  
2549 static void set_mii_flow_control(struct velocity_info *vptr)
2550 {
2551         /*Enable or Disable PAUSE in ANAR */
2552         switch (vptr->options.flow_cntl) {
2553         case FLOW_CNTL_TX:
2554                 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2555                 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2556                 break;
2557
2558         case FLOW_CNTL_RX:
2559                 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2560                 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2561                 break;
2562
2563         case FLOW_CNTL_TX_RX:
2564                 MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2565                 MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2566                 break;
2567
2568         case FLOW_CNTL_DISABLE:
2569                 MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
2570                 MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
2571                 break;
2572         default:
2573                 break;
2574         }
2575 }
2576
2577 /**
2578  *      velocity_set_media_mode         -       set media mode
2579  *      @mii_status: old MII link state
2580  *
2581  *      Check the media link state and configure the flow control
2582  *      PHY and also velocity hardware setup accordingly. In particular
2583  *      we need to set up CD polling and frame bursting.
2584  */
2585  
2586 static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status)
2587 {
2588         u32 curr_status;
2589         struct mac_regs __iomem * regs = vptr->mac_regs;
2590
2591         vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
2592         curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
2593
2594         /* Set mii link status */
2595         set_mii_flow_control(vptr);
2596
2597         /*
2598            Check if new status is consisent with current status
2599            if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
2600            || (mii_status==curr_status)) {
2601            vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
2602            vptr->mii_status=check_connection_type(vptr->mac_regs);
2603            VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity link no change\n");
2604            return 0;
2605            }
2606          */
2607
2608         if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201) {
2609                 MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
2610         }
2611
2612         /*
2613          *      If connection type is AUTO
2614          */
2615         if (mii_status & VELOCITY_AUTONEG_ENABLE) {
2616                 VELOCITY_PRT(MSG_LEVEL_INFO, "Velocity is AUTO mode\n");
2617                 /* clear force MAC mode bit */
2618                 BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
2619                 /* set duplex mode of MAC according to duplex mode of MII */
2620                 MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10, MII_REG_ANAR, vptr->mac_regs);
2621                 MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2622                 MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs);
2623
2624                 /* enable AUTO-NEGO mode */
2625                 mii_set_auto_on(vptr);
2626         } else {
2627                 u16 ANAR;
2628                 u8 CHIPGCR;
2629
2630                 /*
2631                  * 1. if it's 3119, disable frame bursting in halfduplex mode
2632                  *    and enable it in fullduplex mode
2633                  * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
2634                  * 3. only enable CD heart beat counter in 10HD mode
2635                  */
2636
2637                 /* set force MAC mode bit */
2638                 BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
2639
2640                 CHIPGCR = readb(&regs->CHIPGCR);
2641                 CHIPGCR &= ~CHIPGCR_FCGMII;
2642
2643                 if (mii_status & VELOCITY_DUPLEX_FULL) {
2644                         CHIPGCR |= CHIPGCR_FCFDX;
2645                         writeb(CHIPGCR, &regs->CHIPGCR);
2646                         VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced full mode\n");
2647                         if (vptr->rev_id < REV_ID_VT3216_A0)
2648                                 BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
2649                 } else {
2650                         CHIPGCR &= ~CHIPGCR_FCFDX;
2651                         VELOCITY_PRT(MSG_LEVEL_INFO, "set Velocity to forced half mode\n");
2652                         writeb(CHIPGCR, &regs->CHIPGCR);
2653                         if (vptr->rev_id < REV_ID_VT3216_A0)
2654                                 BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
2655                 }
2656
2657                 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
2658
2659                 if (!(mii_status & VELOCITY_DUPLEX_FULL) && (mii_status & VELOCITY_SPEED_10)) {
2660                         BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
2661                 } else {
2662                         BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
2663                 }
2664                 /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
2665                 velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
2666                 ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
2667                 if (mii_status & VELOCITY_SPEED_100) {
2668                         if (mii_status & VELOCITY_DUPLEX_FULL)
2669                                 ANAR |= ANAR_TXFD;
2670                         else
2671                                 ANAR |= ANAR_TX;
2672                 } else {
2673                         if (mii_status & VELOCITY_DUPLEX_FULL)
2674                                 ANAR |= ANAR_10FD;
2675                         else
2676                                 ANAR |= ANAR_10;
2677                 }
2678                 velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
2679                 /* enable AUTO-NEGO mode */
2680                 mii_set_auto_on(vptr);
2681                 /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
2682         }
2683         /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
2684         /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
2685         return VELOCITY_LINK_CHANGE;
2686 }
2687
2688 /**
2689  *      mii_check_media_mode    -       check media state
2690  *      @regs: velocity registers
2691  *
2692  *      Check the current MII status and determine the link status
2693  *      accordingly
2694  */
2695  
2696 static u32 mii_check_media_mode(struct mac_regs __iomem * regs)
2697 {
2698         u32 status = 0;
2699         u16 ANAR;
2700
2701         if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
2702                 status |= VELOCITY_LINK_FAIL;
2703
2704         if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
2705                 status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
2706         else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
2707                 status |= (VELOCITY_SPEED_1000);
2708         else {
2709                 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2710                 if (ANAR & ANAR_TXFD)
2711                         status |= (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
2712                 else if (ANAR & ANAR_TX)
2713                         status |= VELOCITY_SPEED_100;
2714                 else if (ANAR & ANAR_10FD)
2715                         status |= (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
2716                 else
2717                         status |= (VELOCITY_SPEED_10);
2718         }
2719
2720         if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2721                 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2722                 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2723                     == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2724                         if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2725                                 status |= VELOCITY_AUTONEG_ENABLE;
2726                 }
2727         }
2728
2729         return status;
2730 }
2731
2732 static u32 check_connection_type(struct mac_regs __iomem * regs)
2733 {
2734         u32 status = 0;
2735         u8 PHYSR0;
2736         u16 ANAR;
2737         PHYSR0 = readb(&regs->PHYSR0);
2738
2739         /*
2740            if (!(PHYSR0 & PHYSR0_LINKGD))
2741            status|=VELOCITY_LINK_FAIL;
2742          */
2743
2744         if (PHYSR0 & PHYSR0_FDPX)
2745                 status |= VELOCITY_DUPLEX_FULL;
2746
2747         if (PHYSR0 & PHYSR0_SPDG)
2748                 status |= VELOCITY_SPEED_1000;
2749         if (PHYSR0 & PHYSR0_SPD10)
2750                 status |= VELOCITY_SPEED_10;
2751         else
2752                 status |= VELOCITY_SPEED_100;
2753
2754         if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
2755                 velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
2756                 if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
2757                     == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
2758                         if (MII_REG_BITS_IS_ON(G1000CR_1000 | G1000CR_1000FD, MII_REG_G1000CR, regs))
2759                                 status |= VELOCITY_AUTONEG_ENABLE;
2760                 }
2761         }
2762
2763         return status;
2764 }
2765
2766 /**
2767  *      enable_flow_control_ability     -       flow control
2768  *      @vptr: veloity to configure
2769  *
2770  *      Set up flow control according to the flow control options
2771  *      determined by the eeprom/configuration.
2772  */
2773
2774 static void enable_flow_control_ability(struct velocity_info *vptr)
2775 {
2776
2777         struct mac_regs __iomem * regs = vptr->mac_regs;
2778
2779         switch (vptr->options.flow_cntl) {
2780
2781         case FLOW_CNTL_DEFAULT:
2782                 if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
2783                         writel(CR0_FDXRFCEN, &regs->CR0Set);
2784                 else
2785                         writel(CR0_FDXRFCEN, &regs->CR0Clr);
2786
2787                 if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
2788                         writel(CR0_FDXTFCEN, &regs->CR0Set);
2789                 else
2790                         writel(CR0_FDXTFCEN, &regs->CR0Clr);
2791                 break;
2792
2793         case FLOW_CNTL_TX:
2794                 writel(CR0_FDXTFCEN, &regs->CR0Set);
2795                 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2796                 break;
2797
2798         case FLOW_CNTL_RX:
2799                 writel(CR0_FDXRFCEN, &regs->CR0Set);
2800                 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2801                 break;
2802
2803         case FLOW_CNTL_TX_RX:
2804                 writel(CR0_FDXTFCEN, &regs->CR0Set);
2805                 writel(CR0_FDXRFCEN, &regs->CR0Set);
2806                 break;
2807
2808         case FLOW_CNTL_DISABLE:
2809                 writel(CR0_FDXRFCEN, &regs->CR0Clr);
2810                 writel(CR0_FDXTFCEN, &regs->CR0Clr);
2811                 break;
2812
2813         default:
2814                 break;
2815         }
2816
2817 }
2818
2819
2820 /**
2821  *      velocity_ethtool_up     -       pre hook for ethtool
2822  *      @dev: network device
2823  *
2824  *      Called before an ethtool operation. We need to make sure the
2825  *      chip is out of D3 state before we poke at it.
2826  */
2827  
2828 static int velocity_ethtool_up(struct net_device *dev)
2829 {
2830         struct velocity_info *vptr = dev->priv;
2831         if (!netif_running(dev))
2832                 pci_set_power_state(vptr->pdev, PCI_D0);
2833         return 0;
2834 }       
2835
2836 /**
2837  *      velocity_ethtool_down   -       post hook for ethtool
2838  *      @dev: network device
2839  *
2840  *      Called after an ethtool operation. Restore the chip back to D3
2841  *      state if it isn't running.
2842  */
2843  
2844 static void velocity_ethtool_down(struct net_device *dev)
2845 {
2846         struct velocity_info *vptr = dev->priv;
2847         if (!netif_running(dev))
2848                 pci_set_power_state(vptr->pdev, PCI_D3hot);
2849 }
2850
2851 static int velocity_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2852 {
2853         struct velocity_info *vptr = dev->priv;
2854         struct mac_regs __iomem * regs = vptr->mac_regs;
2855         u32 status;
2856         status = check_connection_type(vptr->mac_regs);
2857
2858         cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg | SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
2859         if (status & VELOCITY_SPEED_100)
2860                 cmd->speed = SPEED_100;
2861         else
2862                 cmd->speed = SPEED_10;
2863         cmd->autoneg = (status & VELOCITY_AUTONEG_ENABLE) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2864         cmd->port = PORT_TP;
2865         cmd->transceiver = XCVR_INTERNAL;
2866         cmd->phy_address = readb(&regs->MIIADR) & 0x1F;
2867
2868         if (status & VELOCITY_DUPLEX_FULL)
2869                 cmd->duplex = DUPLEX_FULL;
2870         else
2871                 cmd->duplex = DUPLEX_HALF;
2872                 
2873         return 0;
2874 }
2875
2876 static int velocity_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2877 {
2878         struct velocity_info *vptr = dev->priv;
2879         u32 curr_status;
2880         u32 new_status = 0;
2881         int ret = 0;
2882         
2883         curr_status = check_connection_type(vptr->mac_regs);
2884         curr_status &= (~VELOCITY_LINK_FAIL);
2885
2886         new_status |= ((cmd->autoneg) ? VELOCITY_AUTONEG_ENABLE : 0);
2887         new_status |= ((cmd->speed == SPEED_100) ? VELOCITY_SPEED_100 : 0);
2888         new_status |= ((cmd->speed == SPEED_10) ? VELOCITY_SPEED_10 : 0);
2889         new_status |= ((cmd->duplex == DUPLEX_FULL) ? VELOCITY_DUPLEX_FULL : 0);
2890
2891         if ((new_status & VELOCITY_AUTONEG_ENABLE) && (new_status != (curr_status | VELOCITY_AUTONEG_ENABLE)))
2892                 ret = -EINVAL;
2893         else
2894                 velocity_set_media_mode(vptr, new_status);
2895
2896         return ret;
2897 }
2898
2899 static u32 velocity_get_link(struct net_device *dev)
2900 {
2901         struct velocity_info *vptr = dev->priv;
2902         struct mac_regs __iomem * regs = vptr->mac_regs;
2903         return BYTE_REG_BITS_IS_ON(PHYSR0_LINKGD, &regs->PHYSR0)  ? 0 : 1;
2904 }
2905
2906 static void velocity_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2907 {
2908         struct velocity_info *vptr = dev->priv;
2909         strcpy(info->driver, VELOCITY_NAME);
2910         strcpy(info->version, VELOCITY_VERSION);
2911         strcpy(info->bus_info, pci_name(vptr->pdev));
2912 }
2913
2914 static void velocity_ethtool_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2915 {
2916         struct velocity_info *vptr = dev->priv;
2917         wol->supported = WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP;
2918         wol->wolopts |= WAKE_MAGIC;
2919         /*
2920            if (vptr->wol_opts & VELOCITY_WOL_PHY)
2921                    wol.wolopts|=WAKE_PHY;
2922                          */
2923         if (vptr->wol_opts & VELOCITY_WOL_UCAST)
2924                 wol->wolopts |= WAKE_UCAST;
2925         if (vptr->wol_opts & VELOCITY_WOL_ARP)
2926                 wol->wolopts |= WAKE_ARP;
2927         memcpy(&wol->sopass, vptr->wol_passwd, 6);
2928 }
2929
2930 static int velocity_ethtool_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2931 {
2932         struct velocity_info *vptr = dev->priv;
2933
2934         if (!(wol->wolopts & (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_ARP)))
2935                 return -EFAULT;
2936         vptr->wol_opts = VELOCITY_WOL_MAGIC;
2937
2938         /*
2939            if (wol.wolopts & WAKE_PHY) {
2940            vptr->wol_opts|=VELOCITY_WOL_PHY;
2941            vptr->flags |=VELOCITY_FLAGS_WOL_ENABLED;
2942            }
2943          */
2944
2945         if (wol->wolopts & WAKE_MAGIC) {
2946                 vptr->wol_opts |= VELOCITY_WOL_MAGIC;
2947                 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2948         }
2949         if (wol->wolopts & WAKE_UCAST) {
2950                 vptr->wol_opts |= VELOCITY_WOL_UCAST;
2951                 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2952         }
2953         if (wol->wolopts & WAKE_ARP) {
2954                 vptr->wol_opts |= VELOCITY_WOL_ARP;
2955                 vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
2956         }
2957         memcpy(vptr->wol_passwd, wol->sopass, 6);
2958         return 0;
2959 }
2960
2961 static u32 velocity_get_msglevel(struct net_device *dev)
2962 {
2963         return msglevel;
2964 }
2965
2966 static void velocity_set_msglevel(struct net_device *dev, u32 value)
2967 {
2968          msglevel = value;
2969 }
2970
2971 static struct ethtool_ops velocity_ethtool_ops = {
2972         .get_settings   =       velocity_get_settings,
2973         .set_settings   =       velocity_set_settings,
2974         .get_drvinfo    =       velocity_get_drvinfo,
2975         .get_wol        =       velocity_ethtool_get_wol,
2976         .set_wol        =       velocity_ethtool_set_wol,
2977         .get_msglevel   =       velocity_get_msglevel,
2978         .set_msglevel   =       velocity_set_msglevel,
2979         .get_link       =       velocity_get_link,
2980         .begin          =       velocity_ethtool_up,
2981         .complete       =       velocity_ethtool_down
2982 };
2983
2984 /**
2985  *      velocity_mii_ioctl              -       MII ioctl handler
2986  *      @dev: network device
2987  *      @ifr: the ifreq block for the ioctl
2988  *      @cmd: the command
2989  *
2990  *      Process MII requests made via ioctl from the network layer. These
2991  *      are used by tools like kudzu to interrogate the link state of the
2992  *      hardware
2993  */
2994  
2995 static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2996 {
2997         struct velocity_info *vptr = dev->priv;
2998         struct mac_regs __iomem * regs = vptr->mac_regs;
2999         unsigned long flags;
3000         struct mii_ioctl_data *miidata = if_mii(ifr);
3001         int err;
3002         
3003         switch (cmd) {
3004         case SIOCGMIIPHY:
3005                 miidata->phy_id = readb(&regs->MIIADR) & 0x1f;
3006                 break;
3007         case SIOCGMIIREG:
3008                 if (!capable(CAP_NET_ADMIN))
3009                         return -EPERM;
3010                 if(velocity_mii_read(vptr->mac_regs, miidata->reg_num & 0x1f, &(miidata->val_out)) < 0)
3011                         return -ETIMEDOUT;
3012                 break;
3013         case SIOCSMIIREG:
3014                 if (!capable(CAP_NET_ADMIN))
3015                         return -EPERM;
3016                 spin_lock_irqsave(&vptr->lock, flags);
3017                 err = velocity_mii_write(vptr->mac_regs, miidata->reg_num & 0x1f, miidata->val_in);
3018                 spin_unlock_irqrestore(&vptr->lock, flags);
3019                 check_connection_type(vptr->mac_regs);
3020                 if(err)
3021                         return err;
3022                 break;
3023         default:
3024                 return -EOPNOTSUPP;
3025         }
3026         return 0;
3027 }
3028
3029 #ifdef CONFIG_PM
3030
3031 /**
3032  *      velocity_save_context   -       save registers
3033  *      @vptr: velocity 
3034  *      @context: buffer for stored context
3035  *
3036  *      Retrieve the current configuration from the velocity hardware
3037  *      and stash it in the context structure, for use by the context
3038  *      restore functions. This allows us to save things we need across
3039  *      power down states
3040  */
3041  
3042 static void velocity_save_context(struct velocity_info *vptr, struct velocity_context * context)
3043 {
3044         struct mac_regs __iomem * regs = vptr->mac_regs;
3045         u16 i;
3046         u8 __iomem *ptr = (u8 __iomem *)regs;
3047
3048         for (i = MAC_REG_PAR; i < MAC_REG_CR0_CLR; i += 4)
3049                 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3050
3051         for (i = MAC_REG_MAR; i < MAC_REG_TDCSR_CLR; i += 4)
3052                 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3053
3054         for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4)
3055                 *((u32 *) (context->mac_reg + i)) = readl(ptr + i);
3056
3057 }
3058
3059 /**
3060  *      velocity_restore_context        -       restore registers
3061  *      @vptr: velocity 
3062  *      @context: buffer for stored context
3063  *
3064  *      Reload the register configuration from the velocity context 
3065  *      created by velocity_save_context.
3066  */
3067  
3068 static void velocity_restore_context(struct velocity_info *vptr, struct velocity_context *context)
3069 {
3070         struct mac_regs __iomem * regs = vptr->mac_regs;
3071         int i;
3072         u8 __iomem *ptr = (u8 __iomem *)regs;
3073
3074         for (i = MAC_REG_PAR; i < MAC_REG_CR0_SET; i += 4) {
3075                 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3076         }
3077
3078         /* Just skip cr0 */
3079         for (i = MAC_REG_CR1_SET; i < MAC_REG_CR0_CLR; i++) {
3080                 /* Clear */
3081                 writeb(~(*((u8 *) (context->mac_reg + i))), ptr + i + 4);
3082                 /* Set */
3083                 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3084         }
3085
3086         for (i = MAC_REG_MAR; i < MAC_REG_IMR; i += 4) {
3087                 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3088         }
3089
3090         for (i = MAC_REG_RDBASE_LO; i < MAC_REG_FIFO_TEST0; i += 4) {
3091                 writel(*((u32 *) (context->mac_reg + i)), ptr + i);
3092         }
3093
3094         for (i = MAC_REG_TDCSR_SET; i <= MAC_REG_RDCSR_SET; i++) {
3095                 writeb(*((u8 *) (context->mac_reg + i)), ptr + i);
3096         }
3097
3098 }
3099
3100 /**
3101  *      wol_calc_crc            -       WOL CRC
3102  *      @pattern: data pattern
3103  *      @mask_pattern: mask
3104  *
3105  *      Compute the wake on lan crc hashes for the packet header
3106  *      we are interested in.
3107  */
3108
3109 static u16 wol_calc_crc(int size, u8 * pattern, u8 *mask_pattern)
3110 {
3111         u16 crc = 0xFFFF;
3112         u8 mask;
3113         int i, j;
3114
3115         for (i = 0; i < size; i++) {
3116                 mask = mask_pattern[i];
3117
3118                 /* Skip this loop if the mask equals to zero */
3119                 if (mask == 0x00)
3120                         continue;
3121
3122                 for (j = 0; j < 8; j++) {
3123                         if ((mask & 0x01) == 0) {
3124                                 mask >>= 1;
3125                                 continue;
3126                         }
3127                         mask >>= 1;
3128                         crc = crc_ccitt(crc, &(pattern[i * 8 + j]), 1);
3129                 }
3130         }
3131         /*      Finally, invert the result once to get the correct data */
3132         crc = ~crc;
3133         return bitreverse(crc) >> 16;
3134 }
3135
3136 /**
3137  *      velocity_set_wol        -       set up for wake on lan
3138  *      @vptr: velocity to set WOL status on
3139  *
3140  *      Set a card up for wake on lan either by unicast or by
3141  *      ARP packet.
3142  *
3143  *      FIXME: check static buffer is safe here
3144  */
3145
3146 static int velocity_set_wol(struct velocity_info *vptr)
3147 {
3148         struct mac_regs __iomem * regs = vptr->mac_regs;
3149         static u8 buf[256];
3150         int i;
3151
3152         static u32 mask_pattern[2][4] = {
3153                 {0x00203000, 0x000003C0, 0x00000000, 0x0000000}, /* ARP */
3154                 {0xfffff000, 0xffffffff, 0xffffffff, 0x000ffff}  /* Magic Packet */
3155         };
3156
3157         writew(0xFFFF, &regs->WOLCRClr);
3158         writeb(WOLCFG_SAB | WOLCFG_SAM, &regs->WOLCFGSet);
3159         writew(WOLCR_MAGIC_EN, &regs->WOLCRSet);
3160
3161         /*
3162            if (vptr->wol_opts & VELOCITY_WOL_PHY)
3163            writew((WOLCR_LINKON_EN|WOLCR_LINKOFF_EN), &regs->WOLCRSet);
3164          */
3165
3166         if (vptr->wol_opts & VELOCITY_WOL_UCAST) {
3167                 writew(WOLCR_UNICAST_EN, &regs->WOLCRSet);
3168         }
3169
3170         if (vptr->wol_opts & VELOCITY_WOL_ARP) {
3171                 struct arp_packet *arp = (struct arp_packet *) buf;
3172                 u16 crc;
3173                 memset(buf, 0, sizeof(struct arp_packet) + 7);
3174
3175                 for (i = 0; i < 4; i++)
3176                         writel(mask_pattern[0][i], &regs->ByteMask[0][i]);
3177
3178                 arp->type = htons(ETH_P_ARP);
3179                 arp->ar_op = htons(1);
3180
3181                 memcpy(arp->ar_tip, vptr->ip_addr, 4);
3182
3183                 crc = wol_calc_crc((sizeof(struct arp_packet) + 7) / 8, buf,
3184                                 (u8 *) & mask_pattern[0][0]);
3185
3186                 writew(crc, &regs->PatternCRC[0]);
3187                 writew(WOLCR_ARP_EN, &regs->WOLCRSet);
3188         }
3189
3190         BYTE_REG_BITS_ON(PWCFG_WOLTYPE, &regs->PWCFGSet);
3191         BYTE_REG_BITS_ON(PWCFG_LEGACY_WOLEN, &regs->PWCFGSet);
3192
3193         writew(0x0FFF, &regs->WOLSRClr);
3194
3195         if (vptr->mii_status & VELOCITY_AUTONEG_ENABLE) {
3196                 if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201)
3197                         MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR, vptr->mac_regs);
3198
3199                 MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000, MII_REG_G1000CR, vptr->mac_regs);
3200         }
3201
3202         if (vptr->mii_status & VELOCITY_SPEED_1000)
3203                 MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
3204
3205         BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
3206
3207         {
3208                 u8 GCR;
3209                 GCR = readb(&regs->CHIPGCR);
3210                 GCR = (GCR & ~CHIPGCR_FCGMII) | CHIPGCR_FCFDX;
3211                 writeb(GCR, &regs->CHIPGCR);
3212         }
3213
3214         BYTE_REG_BITS_OFF(ISR_PWEI, &regs->ISR);
3215         /* Turn on SWPTAG just before entering power mode */
3216         BYTE_REG_BITS_ON(STICKHW_SWPTAG, &regs->STICKHW);
3217         /* Go to bed ..... */
3218         BYTE_REG_BITS_ON((STICKHW_DS1 | STICKHW_DS0), &regs->STICKHW);
3219
3220         return 0;
3221 }
3222
3223 static int velocity_suspend(struct pci_dev *pdev, pm_message_t state)
3224 {
3225         struct net_device *dev = pci_get_drvdata(pdev);
3226         struct velocity_info *vptr = netdev_priv(dev);
3227         unsigned long flags;
3228
3229         if(!netif_running(vptr->dev))
3230                 return 0;
3231
3232         netif_device_detach(vptr->dev);
3233
3234         spin_lock_irqsave(&vptr->lock, flags);
3235         pci_save_state(pdev);
3236 #ifdef ETHTOOL_GWOL
3237         if (vptr->flags & VELOCITY_FLAGS_WOL_ENABLED) {
3238                 velocity_get_ip(vptr);
3239                 velocity_save_context(vptr, &vptr->context);
3240                 velocity_shutdown(vptr);
3241                 velocity_set_wol(vptr);
3242                 pci_enable_wake(pdev, 3, 1);
3243                 pci_set_power_state(pdev, PCI_D3hot);
3244         } else {
3245                 velocity_save_context(vptr, &vptr->context);
3246                 velocity_shutdown(vptr);
3247                 pci_disable_device(pdev);
3248                 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3249         }
3250 #else
3251         pci_set_power_state(pdev, pci_choose_state(pdev, state));
3252 #endif
3253         spin_unlock_irqrestore(&vptr->lock, flags);
3254         return 0;
3255 }
3256
3257 static int velocity_resume(struct pci_dev *pdev)
3258 {
3259         struct net_device *dev = pci_get_drvdata(pdev);
3260         struct velocity_info *vptr = netdev_priv(dev);
3261         unsigned long flags;
3262         int i;
3263
3264         if(!netif_running(vptr->dev))
3265                 return 0;
3266
3267         pci_set_power_state(pdev, PCI_D0);
3268         pci_enable_wake(pdev, 0, 0);
3269         pci_restore_state(pdev);
3270
3271         mac_wol_reset(vptr->mac_regs);
3272
3273         spin_lock_irqsave(&vptr->lock, flags);
3274         velocity_restore_context(vptr, &vptr->context);
3275         velocity_init_registers(vptr, VELOCITY_INIT_WOL);
3276         mac_disable_int(vptr->mac_regs);
3277
3278         velocity_tx_srv(vptr, 0);
3279
3280         for (i = 0; i < vptr->num_txq; i++) {
3281                 if (vptr->td_used[i]) {
3282                         mac_tx_queue_wake(vptr->mac_regs, i);
3283                 }
3284         }
3285
3286         mac_enable_int(vptr->mac_regs);
3287         spin_unlock_irqrestore(&vptr->lock, flags);
3288         netif_device_attach(vptr->dev);
3289
3290         return 0;
3291 }
3292
3293 static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr)
3294 {
3295         struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3296
3297         if (ifa) {
3298                 struct net_device *dev = ifa->ifa_dev->dev;
3299                 struct velocity_info *vptr;
3300                 unsigned long flags;
3301
3302                 spin_lock_irqsave(&velocity_dev_list_lock, flags);
3303                 list_for_each_entry(vptr, &velocity_dev_list, list) {
3304                         if (vptr->dev == dev) {
3305                                 velocity_get_ip(vptr);
3306                                 break;
3307                         }
3308                 }
3309                 spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
3310         }
3311         return NOTIFY_DONE;
3312 }
3313 #endif