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