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