forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvia-velocity.c
3301 lines (2756 loc) · 86.1 KB
/
via-velocity.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This code is derived from the VIA reference driver (copyright message
* below) provided to Red Hat by VIA Networking Technologies, Inc. for
* addition to the Linux kernel.
*
* The code has been merged into one source file, cleaned up to follow
* Linux coding style, ported to the Linux 2.6 kernel tree and cleaned
* for 64bit hardware platforms.
*
* TODO
* Big-endian support
* rx_copybreak/alignment
* Scatter gather
* More testing
*
* The changes are (c) Copyright 2004, Red Hat Inc. <[email protected]>
* Additional fixes and clean up: Francois Romieu
*
* This source has not been verified for use in safety critical systems.
*
* Please direct queries about the revamped driver to the linux-kernel
* list not VIA.
*
* Original code:
*
* Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
* All rights reserved.
*
* This software may be redistributed and/or modified under
* the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* Author: Chuang Liang-Shing, AJ Jiang
*
* Date: Jan 24, 2003
*
* MODULE_LICENSE("GPL");
*
*/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/config.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/pci.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/version.h>
#include <linux/string.h>
#include <linux/wait.h>
#include <asm/io.h>
#include <linux/if.h>
#include <linux/config.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/inetdevice.h>
#include <linux/reboot.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/in.h>
#include <linux/if_arp.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/crc-ccitt.h>
#include <linux/crc32.h>
#include "via-velocity.h"
static int velocity_nics = 0;
static int msglevel = MSG_LEVEL_INFO;
static int velocity_mii_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
static struct ethtool_ops velocity_ethtool_ops;
/*
Define module options
*/
MODULE_AUTHOR("VIA Networking Technologies, Inc.");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("VIA Networking Velocity Family Gigabit Ethernet Adapter Driver");
#define VELOCITY_PARAM(N,D) \
static int N[MAX_UNITS]=OPTION_DEFAULT;\
module_param_array(N, int, NULL, 0); \
MODULE_PARM_DESC(N, D);
#define RX_DESC_MIN 64
#define RX_DESC_MAX 255
#define RX_DESC_DEF 64
VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
#define TX_DESC_MIN 16
#define TX_DESC_MAX 256
#define TX_DESC_DEF 64
VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
#define VLAN_ID_MIN 0
#define VLAN_ID_MAX 4095
#define VLAN_ID_DEF 0
/* VID_setting[] is used for setting the VID of NIC.
0: default VID.
1-4094: other VIDs.
*/
VELOCITY_PARAM(VID_setting, "802.1Q VLAN ID");
#define RX_THRESH_MIN 0
#define RX_THRESH_MAX 3
#define RX_THRESH_DEF 0
/* rx_thresh[] is used for controlling the receive fifo threshold.
0: indicate the rxfifo threshold is 128 bytes.
1: indicate the rxfifo threshold is 512 bytes.
2: indicate the rxfifo threshold is 1024 bytes.
3: indicate the rxfifo threshold is store & forward.
*/
VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
#define DMA_LENGTH_MIN 0
#define DMA_LENGTH_MAX 7
#define DMA_LENGTH_DEF 0
/* DMA_length[] is used for controlling the DMA length
0: 8 DWORDs
1: 16 DWORDs
2: 32 DWORDs
3: 64 DWORDs
4: 128 DWORDs
5: 256 DWORDs
6: SF(flush till emply)
7: SF(flush till emply)
*/
VELOCITY_PARAM(DMA_length, "DMA length");
#define TAGGING_DEF 0
/* enable_tagging[] is used for enabling 802.1Q VID tagging.
0: disable VID seeting(default).
1: enable VID setting.
*/
VELOCITY_PARAM(enable_tagging, "Enable 802.1Q tagging");
#define IP_ALIG_DEF 0
/* IP_byte_align[] is used for IP header DWORD byte aligned
0: indicate the IP header won't be DWORD byte aligned.(Default) .
1: indicate the IP header will be DWORD byte aligned.
In some enviroment, the IP header should be DWORD byte aligned,
or the packet will be droped when we receive it. (eg: IPVS)
*/
VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
#define TX_CSUM_DEF 1
/* txcsum_offload[] is used for setting the checksum offload ability of NIC.
(We only support RX checksum offload now)
0: disable csum_offload[checksum offload
1: enable checksum offload. (Default)
*/
VELOCITY_PARAM(txcsum_offload, "Enable transmit packet checksum offload");
#define FLOW_CNTL_DEF 1
#define FLOW_CNTL_MIN 1
#define FLOW_CNTL_MAX 5
/* flow_control[] is used for setting the flow control ability of NIC.
1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
2: enable TX flow control.
3: enable RX flow control.
4: enable RX/TX flow control.
5: disable
*/
VELOCITY_PARAM(flow_control, "Enable flow control ability");
#define MED_LNK_DEF 0
#define MED_LNK_MIN 0
#define MED_LNK_MAX 4
/* speed_duplex[] is used for setting the speed and duplex mode of NIC.
0: indicate autonegotiation for both speed and duplex mode
1: indicate 100Mbps half duplex mode
2: indicate 100Mbps full duplex mode
3: indicate 10Mbps half duplex mode
4: indicate 10Mbps full duplex mode
Note:
if EEPROM have been set to the force mode, this option is ignored
by driver.
*/
VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
#define VAL_PKT_LEN_DEF 0
/* ValPktLen[] is used for setting the checksum offload ability of NIC.
0: Receive frame with invalid layer 2 length (Default)
1: Drop frame with invalid layer 2 length
*/
VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
#define WOL_OPT_DEF 0
#define WOL_OPT_MIN 0
#define WOL_OPT_MAX 7
/* wol_opts[] is used for controlling wake on lan behavior.
0: Wake up if recevied a magic packet. (Default)
1: Wake up if link status is on/off.
2: Wake up if recevied an arp packet.
4: Wake up if recevied any unicast packet.
Those value can be sumed up to support more than one option.
*/
VELOCITY_PARAM(wol_opts, "Wake On Lan options");
#define INT_WORKS_DEF 20
#define INT_WORKS_MIN 10
#define INT_WORKS_MAX 64
VELOCITY_PARAM(int_works, "Number of packets per interrupt services");
static int rx_copybreak = 200;
module_param(rx_copybreak, int, 0644);
MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
static void velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr, struct velocity_info_tbl *info);
static int velocity_get_pci_info(struct velocity_info *, struct pci_dev *pdev);
static void velocity_print_info(struct velocity_info *vptr);
static int velocity_open(struct net_device *dev);
static int velocity_change_mtu(struct net_device *dev, int mtu);
static int velocity_xmit(struct sk_buff *skb, struct net_device *dev);
static int velocity_intr(int irq, void *dev_instance, struct pt_regs *regs);
static void velocity_set_multi(struct net_device *dev);
static struct net_device_stats *velocity_get_stats(struct net_device *dev);
static int velocity_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
static int velocity_close(struct net_device *dev);
static int velocity_receive_frame(struct velocity_info *, int idx);
static int velocity_alloc_rx_buf(struct velocity_info *, int idx);
static void velocity_free_rd_ring(struct velocity_info *vptr);
static void velocity_free_tx_buf(struct velocity_info *vptr, struct velocity_td_info *);
static int velocity_soft_reset(struct velocity_info *vptr);
static void mii_init(struct velocity_info *vptr, u32 mii_status);
static u32 velocity_get_opt_media_mode(struct velocity_info *vptr);
static void velocity_print_link_status(struct velocity_info *vptr);
static void safe_disable_mii_autopoll(struct mac_regs __iomem * regs);
static void velocity_shutdown(struct velocity_info *vptr);
static void enable_flow_control_ability(struct velocity_info *vptr);
static void enable_mii_autopoll(struct mac_regs __iomem * regs);
static int velocity_mii_read(struct mac_regs __iomem *, u8 byIdx, u16 * pdata);
static int velocity_mii_write(struct mac_regs __iomem *, u8 byMiiAddr, u16 data);
static u32 mii_check_media_mode(struct mac_regs __iomem * regs);
static u32 check_connection_type(struct mac_regs __iomem * regs);
static int velocity_set_media_mode(struct velocity_info *vptr, u32 mii_status);
#ifdef CONFIG_PM
static int velocity_suspend(struct pci_dev *pdev, pm_message_t state);
static int velocity_resume(struct pci_dev *pdev);
static int velocity_netdev_event(struct notifier_block *nb, unsigned long notification, void *ptr);
static struct notifier_block velocity_inetaddr_notifier = {
.notifier_call = velocity_netdev_event,
};
static DEFINE_SPINLOCK(velocity_dev_list_lock);
static LIST_HEAD(velocity_dev_list);
static void velocity_register_notifier(void)
{
register_inetaddr_notifier(&velocity_inetaddr_notifier);
}
static void velocity_unregister_notifier(void)
{
unregister_inetaddr_notifier(&velocity_inetaddr_notifier);
}
#else /* CONFIG_PM */
#define velocity_register_notifier() do {} while (0)
#define velocity_unregister_notifier() do {} while (0)
#endif /* !CONFIG_PM */
/*
* Internal board variants. At the moment we have only one
*/
static struct velocity_info_tbl chip_info_table[] = {
{CHIP_TYPE_VT6110, "VIA Networking Velocity Family Gigabit Ethernet Adapter", 256, 1, 0x00FFFFFFUL},
{0, NULL}
};
/*
* Describe the PCI device identifiers that we support in this
* device driver. Used for hotplug autoloading.
*/
static struct pci_device_id velocity_id_table[] __devinitdata = {
{PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_612X,
PCI_ANY_ID, PCI_ANY_ID, 0, 0, (unsigned long) chip_info_table},
{0, }
};
MODULE_DEVICE_TABLE(pci, velocity_id_table);
/**
* get_chip_name - identifier to name
* @id: chip identifier
*
* Given a chip identifier return a suitable description. Returns
* a pointer a static string valid while the driver is loaded.
*/
static char __devinit *get_chip_name(enum chip_type chip_id)
{
int i;
for (i = 0; chip_info_table[i].name != NULL; i++)
if (chip_info_table[i].chip_id == chip_id)
break;
return chip_info_table[i].name;
}
/**
* velocity_remove1 - device unplug
* @pdev: PCI device being removed
*
* Device unload callback. Called on an unplug or on module
* unload for each active device that is present. Disconnects
* the device from the network layer and frees all the resources
*/
static void __devexit velocity_remove1(struct pci_dev *pdev)
{
struct net_device *dev = pci_get_drvdata(pdev);
struct velocity_info *vptr = dev->priv;
#ifdef CONFIG_PM
unsigned long flags;
spin_lock_irqsave(&velocity_dev_list_lock, flags);
if (!list_empty(&velocity_dev_list))
list_del(&vptr->list);
spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
#endif
unregister_netdev(dev);
iounmap(vptr->mac_regs);
pci_release_regions(pdev);
pci_disable_device(pdev);
pci_set_drvdata(pdev, NULL);
free_netdev(dev);
velocity_nics--;
}
/**
* velocity_set_int_opt - parser for integer options
* @opt: pointer to option value
* @val: value the user requested (or -1 for default)
* @min: lowest value allowed
* @max: highest value allowed
* @def: default value
* @name: property name
* @dev: device name
*
* Set an integer property in the module options. This function does
* all the verification and checking as well as reporting so that
* we don't duplicate code for each option.
*/
static void __devinit velocity_set_int_opt(int *opt, int val, int min, int max, int def, char *name, char *devname)
{
if (val == -1)
*opt = def;
else if (val < min || val > max) {
VELOCITY_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
devname, name, min, max);
*opt = def;
} else {
VELOCITY_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
devname, name, val);
*opt = val;
}
}
/**
* velocity_set_bool_opt - parser for boolean options
* @opt: pointer to option value
* @val: value the user requested (or -1 for default)
* @def: default value (yes/no)
* @flag: numeric value to set for true.
* @name: property name
* @dev: device name
*
* Set a boolean property in the module options. This function does
* all the verification and checking as well as reporting so that
* we don't duplicate code for each option.
*/
static void __devinit velocity_set_bool_opt(u32 * opt, int val, int def, u32 flag, char *name, char *devname)
{
(*opt) &= (~flag);
if (val == -1)
*opt |= (def ? flag : 0);
else if (val < 0 || val > 1) {
printk(KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
devname, name);
*opt |= (def ? flag : 0);
} else {
printk(KERN_INFO "%s: set parameter %s to %s\n",
devname, name, val ? "TRUE" : "FALSE");
*opt |= (val ? flag : 0);
}
}
/**
* velocity_get_options - set options on device
* @opts: option structure for the device
* @index: index of option to use in module options array
* @devname: device name
*
* Turn the module and command options into a single structure
* for the current device
*/
static void __devinit velocity_get_options(struct velocity_opt *opts, int index, char *devname)
{
velocity_set_int_opt(&opts->rx_thresh, rx_thresh[index], RX_THRESH_MIN, RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh", devname);
velocity_set_int_opt(&opts->DMA_length, DMA_length[index], DMA_LENGTH_MIN, DMA_LENGTH_MAX, DMA_LENGTH_DEF, "DMA_length", devname);
velocity_set_int_opt(&opts->numrx, RxDescriptors[index], RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF, "RxDescriptors", devname);
velocity_set_int_opt(&opts->numtx, TxDescriptors[index], TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF, "TxDescriptors", devname);
velocity_set_int_opt(&opts->vid, VID_setting[index], VLAN_ID_MIN, VLAN_ID_MAX, VLAN_ID_DEF, "VID_setting", devname);
velocity_set_bool_opt(&opts->flags, enable_tagging[index], TAGGING_DEF, VELOCITY_FLAGS_TAGGING, "enable_tagging", devname);
velocity_set_bool_opt(&opts->flags, txcsum_offload[index], TX_CSUM_DEF, VELOCITY_FLAGS_TX_CSUM, "txcsum_offload", devname);
velocity_set_int_opt(&opts->flow_cntl, flow_control[index], FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF, "flow_control", devname);
velocity_set_bool_opt(&opts->flags, IP_byte_align[index], IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN, "IP_byte_align", devname);
velocity_set_bool_opt(&opts->flags, ValPktLen[index], VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN, "ValPktLen", devname);
velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index], MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF, "Media link mode", devname);
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);
velocity_set_int_opt((int *) &opts->int_works, int_works[index], INT_WORKS_MIN, INT_WORKS_MAX, INT_WORKS_DEF, "Interrupt service works", devname);
opts->numrx = (opts->numrx & ~3);
}
/**
* velocity_init_cam_filter - initialise CAM
* @vptr: velocity to program
*
* Initialize the content addressable memory used for filters. Load
* appropriately according to the presence of VLAN
*/
static void velocity_init_cam_filter(struct velocity_info *vptr)
{
struct mac_regs __iomem * regs = vptr->mac_regs;
/* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, ®s->MCFG);
WORD_REG_BITS_ON(MCFG_VIDFR, ®s->MCFG);
/* Disable all CAMs */
memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
/* Enable first VCAM */
if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
/* If Tagging option is enabled and VLAN ID is not zero, then
turn on MCFG_RTGOPT also */
if (vptr->options.vid != 0)
WORD_REG_BITS_ON(MCFG_RTGOPT, ®s->MCFG);
mac_set_cam(regs, 0, (u8 *) & (vptr->options.vid), VELOCITY_VLAN_ID_CAM);
vptr->vCAMmask[0] |= 1;
mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
} else {
u16 temp = 0;
mac_set_cam(regs, 0, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
temp = 1;
mac_set_cam_mask(regs, (u8 *) &temp, VELOCITY_VLAN_ID_CAM);
}
}
/**
* velocity_rx_reset - handle a receive reset
* @vptr: velocity we are resetting
*
* Reset the ownership and status for the receive ring side.
* Hand all the receive queue to the NIC.
*/
static void velocity_rx_reset(struct velocity_info *vptr)
{
struct mac_regs __iomem * regs = vptr->mac_regs;
int i;
vptr->rd_dirty = vptr->rd_filled = vptr->rd_curr = 0;
/*
* Init state, all RD entries belong to the NIC
*/
for (i = 0; i < vptr->options.numrx; ++i)
vptr->rd_ring[i].rdesc0.owner = OWNED_BY_NIC;
writew(vptr->options.numrx, ®s->RBRDU);
writel(vptr->rd_pool_dma, ®s->RDBaseLo);
writew(0, ®s->RDIdx);
writew(vptr->options.numrx - 1, ®s->RDCSize);
}
/**
* velocity_init_registers - initialise MAC registers
* @vptr: velocity to init
* @type: type of initialisation (hot or cold)
*
* Initialise the MAC on a reset or on first set up on the
* hardware.
*/
static void velocity_init_registers(struct velocity_info *vptr,
enum velocity_init_type type)
{
struct mac_regs __iomem * regs = vptr->mac_regs;
int i, mii_status;
mac_wol_reset(regs);
switch (type) {
case VELOCITY_INIT_RESET:
case VELOCITY_INIT_WOL:
netif_stop_queue(vptr->dev);
/*
* Reset RX to prevent RX pointer not on the 4X location
*/
velocity_rx_reset(vptr);
mac_rx_queue_run(regs);
mac_rx_queue_wake(regs);
mii_status = velocity_get_opt_media_mode(vptr);
if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
velocity_print_link_status(vptr);
if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
netif_wake_queue(vptr->dev);
}
enable_flow_control_ability(vptr);
mac_clear_isr(regs);
writel(CR0_STOP, ®s->CR0Clr);
writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
®s->CR0Set);
break;
case VELOCITY_INIT_COLD:
default:
/*
* Do reset
*/
velocity_soft_reset(vptr);
mdelay(5);
mac_eeprom_reload(regs);
for (i = 0; i < 6; i++) {
writeb(vptr->dev->dev_addr[i], &(regs->PAR[i]));
}
/*
* clear Pre_ACPI bit.
*/
BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
mac_set_rx_thresh(regs, vptr->options.rx_thresh);
mac_set_dma_length(regs, vptr->options.DMA_length);
writeb(WOLCFG_SAM | WOLCFG_SAB, ®s->WOLCFGSet);
/*
* Back off algorithm use original IEEE standard
*/
BYTE_REG_BITS_SET(CFGB_OFSET, (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA | CFGB_BAKOPT), ®s->CFGB);
/*
* Init CAM filter
*/
velocity_init_cam_filter(vptr);
/*
* Set packet filter: Receive directed and broadcast address
*/
velocity_set_multi(vptr->dev);
/*
* Enable MII auto-polling
*/
enable_mii_autopoll(regs);
vptr->int_mask = INT_MASK_DEF;
writel(cpu_to_le32(vptr->rd_pool_dma), ®s->RDBaseLo);
writew(vptr->options.numrx - 1, ®s->RDCSize);
mac_rx_queue_run(regs);
mac_rx_queue_wake(regs);
writew(vptr->options.numtx - 1, ®s->TDCSize);
for (i = 0; i < vptr->num_txq; i++) {
writel(cpu_to_le32(vptr->td_pool_dma[i]), &(regs->TDBaseLo[i]));
mac_tx_queue_run(regs, i);
}
init_flow_control_register(vptr);
writel(CR0_STOP, ®s->CR0Clr);
writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT), ®s->CR0Set);
mii_status = velocity_get_opt_media_mode(vptr);
netif_stop_queue(vptr->dev);
mii_init(vptr, mii_status);
if (velocity_set_media_mode(vptr, mii_status) != VELOCITY_LINK_CHANGE) {
velocity_print_link_status(vptr);
if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
netif_wake_queue(vptr->dev);
}
enable_flow_control_ability(vptr);
mac_hw_mibs_init(regs);
mac_write_int_mask(vptr->int_mask, regs);
mac_clear_isr(regs);
}
}
/**
* velocity_soft_reset - soft reset
* @vptr: velocity to reset
*
* Kick off a soft reset of the velocity adapter and then poll
* until the reset sequence has completed before returning.
*/
static int velocity_soft_reset(struct velocity_info *vptr)
{
struct mac_regs __iomem * regs = vptr->mac_regs;
int i = 0;
writel(CR0_SFRST, ®s->CR0Set);
for (i = 0; i < W_MAX_TIMEOUT; i++) {
udelay(5);
if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, ®s->CR0Set))
break;
}
if (i == W_MAX_TIMEOUT) {
writel(CR0_FORSRST, ®s->CR0Set);
/* FIXME: PCI POSTING */
/* delay 2ms */
mdelay(2);
}
return 0;
}
/**
* velocity_found1 - set up discovered velocity card
* @pdev: PCI device
* @ent: PCI device table entry that matched
*
* Configure a discovered adapter from scratch. Return a negative
* errno error code on failure paths.
*/
static int __devinit velocity_found1(struct pci_dev *pdev, const struct pci_device_id *ent)
{
static int first = 1;
struct net_device *dev;
int i;
struct velocity_info_tbl *info = (struct velocity_info_tbl *) ent->driver_data;
struct velocity_info *vptr;
struct mac_regs __iomem * regs;
int ret = -ENOMEM;
if (velocity_nics >= MAX_UNITS) {
printk(KERN_NOTICE VELOCITY_NAME ": already found %d NICs.\n",
velocity_nics);
return -ENODEV;
}
dev = alloc_etherdev(sizeof(struct velocity_info));
if (dev == NULL) {
printk(KERN_ERR VELOCITY_NAME ": allocate net device failed.\n");
goto out;
}
/* Chain it all together */
SET_MODULE_OWNER(dev);
SET_NETDEV_DEV(dev, &pdev->dev);
vptr = dev->priv;
if (first) {
printk(KERN_INFO "%s Ver. %s\n",
VELOCITY_FULL_DRV_NAM, VELOCITY_VERSION);
printk(KERN_INFO "Copyright (c) 2002, 2003 VIA Networking Technologies, Inc.\n");
printk(KERN_INFO "Copyright (c) 2004 Red Hat Inc.\n");
first = 0;
}
velocity_init_info(pdev, vptr, info);
vptr->dev = dev;
dev->irq = pdev->irq;
ret = pci_enable_device(pdev);
if (ret < 0)
goto err_free_dev;
ret = velocity_get_pci_info(vptr, pdev);
if (ret < 0) {
printk(KERN_ERR VELOCITY_NAME ": Failed to find PCI device.\n");
goto err_disable;
}
ret = pci_request_regions(pdev, VELOCITY_NAME);
if (ret < 0) {
printk(KERN_ERR VELOCITY_NAME ": Failed to find PCI device.\n");
goto err_disable;
}
regs = ioremap(vptr->memaddr, vptr->io_size);
if (regs == NULL) {
ret = -EIO;
goto err_release_res;
}
vptr->mac_regs = regs;
mac_wol_reset(regs);
dev->base_addr = vptr->ioaddr;
for (i = 0; i < 6; i++)
dev->dev_addr[i] = readb(®s->PAR[i]);
velocity_get_options(&vptr->options, velocity_nics, dev->name);
/*
* Mask out the options cannot be set to the chip
*/
vptr->options.flags &= info->flags;
/*
* Enable the chip specified capbilities
*/
vptr->flags = vptr->options.flags | (info->flags & 0xFF000000UL);
vptr->wol_opts = vptr->options.wol_opts;
vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
dev->irq = pdev->irq;
dev->open = velocity_open;
dev->hard_start_xmit = velocity_xmit;
dev->stop = velocity_close;
dev->get_stats = velocity_get_stats;
dev->set_multicast_list = velocity_set_multi;
dev->do_ioctl = velocity_ioctl;
dev->ethtool_ops = &velocity_ethtool_ops;
dev->change_mtu = velocity_change_mtu;
#ifdef VELOCITY_ZERO_COPY_SUPPORT
dev->features |= NETIF_F_SG;
#endif
if (vptr->flags & VELOCITY_FLAGS_TX_CSUM) {
dev->features |= NETIF_F_HW_CSUM;
}
ret = register_netdev(dev);
if (ret < 0)
goto err_iounmap;
velocity_print_info(vptr);
pci_set_drvdata(pdev, dev);
/* and leave the chip powered down */
pci_set_power_state(pdev, PCI_D3hot);
#ifdef CONFIG_PM
{
unsigned long flags;
spin_lock_irqsave(&velocity_dev_list_lock, flags);
list_add(&vptr->list, &velocity_dev_list);
spin_unlock_irqrestore(&velocity_dev_list_lock, flags);
}
#endif
velocity_nics++;
out:
return ret;
err_iounmap:
iounmap(regs);
err_release_res:
pci_release_regions(pdev);
err_disable:
pci_disable_device(pdev);
err_free_dev:
free_netdev(dev);
goto out;
}
/**
* velocity_print_info - per driver data
* @vptr: velocity
*
* Print per driver data as the kernel driver finds Velocity
* hardware
*/
static void __devinit velocity_print_info(struct velocity_info *vptr)
{
struct net_device *dev = vptr->dev;
printk(KERN_INFO "%s: %s\n", dev->name, get_chip_name(vptr->chip_id));
printk(KERN_INFO "%s: Ethernet Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
dev->name,
dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
}
/**
* velocity_init_info - init private data
* @pdev: PCI device
* @vptr: Velocity info
* @info: Board type
*
* Set up the initial velocity_info struct for the device that has been
* discovered.
*/
static void __devinit velocity_init_info(struct pci_dev *pdev, struct velocity_info *vptr, struct velocity_info_tbl *info)
{
memset(vptr, 0, sizeof(struct velocity_info));
vptr->pdev = pdev;
vptr->chip_id = info->chip_id;
vptr->io_size = info->io_size;
vptr->num_txq = info->txqueue;
vptr->multicast_limit = MCAM_SIZE;
spin_lock_init(&vptr->lock);
INIT_LIST_HEAD(&vptr->list);
}
/**
* velocity_get_pci_info - retrieve PCI info for device
* @vptr: velocity device
* @pdev: PCI device it matches
*
* Retrieve the PCI configuration space data that interests us from
* the kernel PCI layer
*/
static int __devinit velocity_get_pci_info(struct velocity_info *vptr, struct pci_dev *pdev)
{
if(pci_read_config_byte(pdev, PCI_REVISION_ID, &vptr->rev_id) < 0)
return -EIO;
pci_set_master(pdev);
vptr->ioaddr = pci_resource_start(pdev, 0);
vptr->memaddr = pci_resource_start(pdev, 1);
if(!(pci_resource_flags(pdev, 0) & IORESOURCE_IO))
{
printk(KERN_ERR "%s: region #0 is not an I/O resource, aborting.\n",
pci_name(pdev));
return -EINVAL;
}
if((pci_resource_flags(pdev, 1) & IORESOURCE_IO))
{
printk(KERN_ERR "%s: region #1 is an I/O resource, aborting.\n",
pci_name(pdev));
return -EINVAL;
}
if(pci_resource_len(pdev, 1) < 256)
{
printk(KERN_ERR "%s: region #1 is too small.\n",
pci_name(pdev));
return -EINVAL;
}
vptr->pdev = pdev;
return 0;
}
/**
* velocity_init_rings - set up DMA rings
* @vptr: Velocity to set up
*
* Allocate PCI mapped DMA rings for the receive and transmit layer
* to use.
*/
static int velocity_init_rings(struct velocity_info *vptr)
{
int i;
unsigned int psize;
unsigned int tsize;
dma_addr_t pool_dma;
u8 *pool;
/*
* Allocate all RD/TD rings a single pool
*/
psize = vptr->options.numrx * sizeof(struct rx_desc) +
vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;
/*
* pci_alloc_consistent() fulfills the requirement for 64 bytes
* alignment
*/
pool = pci_alloc_consistent(vptr->pdev, psize, &pool_dma);
if (pool == NULL) {
printk(KERN_ERR "%s : DMA memory allocation failed.\n",
vptr->dev->name);
return -ENOMEM;
}
memset(pool, 0, psize);
vptr->rd_ring = (struct rx_desc *) pool;
vptr->rd_pool_dma = pool_dma;
tsize = vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq;
vptr->tx_bufs = pci_alloc_consistent(vptr->pdev, tsize,
&vptr->tx_bufs_dma);
if (vptr->tx_bufs == NULL) {
printk(KERN_ERR "%s: DMA memory allocation failed.\n",
vptr->dev->name);
pci_free_consistent(vptr->pdev, psize, pool, pool_dma);
return -ENOMEM;
}
memset(vptr->tx_bufs, 0, vptr->options.numtx * PKT_BUF_SZ * vptr->num_txq);
i = vptr->options.numrx * sizeof(struct rx_desc);
pool += i;
pool_dma += i;
for (i = 0; i < vptr->num_txq; i++) {
int offset = vptr->options.numtx * sizeof(struct tx_desc);
vptr->td_pool_dma[i] = pool_dma;
vptr->td_rings[i] = (struct tx_desc *) pool;
pool += offset;
pool_dma += offset;
}
return 0;
}
/**
* velocity_free_rings - free PCI ring pointers
* @vptr: Velocity to free from
*
* Clean up the PCI ring buffers allocated to this velocity.
*/
static void velocity_free_rings(struct velocity_info *vptr)
{
int size;
size = vptr->options.numrx * sizeof(struct rx_desc) +
vptr->options.numtx * sizeof(struct tx_desc) * vptr->num_txq;