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