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