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