-
Notifications
You must be signed in to change notification settings - Fork 2
/
r8126_n.c
18186 lines (15700 loc) · 708 KB
/
r8126_n.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
// SPDX-License-Identifier: GPL-2.0-only
/*
################################################################################
#
# r8126 is the Linux device driver released for Realtek 5 Gigabit Ethernet
# controllers with PCI-Express interface.
#
# Copyright(c) 2024 Realtek Semiconductor Corp. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# 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.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses/>.
#
# Author:
# Realtek NIC software team <nicfae@realtek.com>
# No. 2, Innovation Road II, Hsinchu Science Park, Hsinchu 300, Taiwan
#
################################################################################
*/
/************************************************************************************
* This product is covered by one or more of the following patents:
* US6,570,884, US6,115,776, and US6,327,625.
***********************************************************************************/
/*
* This driver is modified from r8169.c in Linux kernel 2.6.18
*/
#include <linux/module.h>
#include <linux/version.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/mii.h>
#include <linux/if_vlan.h>
#include <linux/crc32.h>
#include <linux/interrupt.h>
#include <linux/in.h>
#include <linux/ip.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
#include <linux/ipv6.h>
#include <net/ip6_checksum.h>
#endif
#include <linux/tcp.h>
#include <linux/init.h>
#include <linux/rtnetlink.h>
#include <linux/completion.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,4,0)
#include <linux/pci-aspm.h>
#endif
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,37)
#include <linux/prefetch.h>
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
#define dev_printk(A,B,fmt,args...) printk(A fmt,##args)
#else
#include <linux/dma-mapping.h>
#include <linux/moduleparam.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
#include <linux/mdio.h>
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,4,10)
#include <net/gso.h>
#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(6,4,10) */
#include <asm/io.h>
#include <asm/irq.h>
#include "r8126.h"
#include "rtl_eeprom.h"
#include "rtltool.h"
#include "r8126_firmware.h"
#ifdef ENABLE_R8126_PROCFS
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#endif
#define FIRMWARE_8126A_2 "rtl_nic/rtl8126a-2.fw"
#define FIRMWARE_8126A_3 "rtl_nic/rtl8126a-3.fw"
static const struct {
const char *name;
const char *fw_name;
} rtl_chip_fw_infos[] = {
/* PCI-E devices. */
[CFG_METHOD_1] = {"RTL8126A", },
[CFG_METHOD_2] = {"RTL8126A", FIRMWARE_8126A_2},
[CFG_METHOD_3] = {"RTL8126A", FIRMWARE_8126A_3},
[CFG_METHOD_DEFAULT] = {"Unknown", },
};
#define _R(NAME,MAC,RCR,MASK,JumFrameSz) \
{ .name = NAME, .mcfg = MAC, .RCR_Cfg = RCR, .RxConfigMask = MASK, .jumbo_frame_sz = JumFrameSz }
static const struct {
const char *name;
u8 mcfg;
u32 RCR_Cfg;
u32 RxConfigMask; /* Clears the bits supported by this chip */
u32 jumbo_frame_sz;
} rtl_chip_info[] = {
_R("RTL8126A",
CFG_METHOD_1,
Rx_Fetch_Number_8 | RxCfg_pause_slot_en | EnableInnerVlan | EnableOuterVlan | (RX_DMA_BURST_512 << RxCfgDMAShift),
0xff7e5880,
Jumbo_Frame_9k),
_R("RTL8126A",
CFG_METHOD_2,
Rx_Fetch_Number_8 | Rx_Close_Multiple | RxCfg_pause_slot_en | EnableInnerVlan | EnableOuterVlan | (RX_DMA_BURST_512 << RxCfgDMAShift),
0xff7e5880,
Jumbo_Frame_9k),
_R("RTL8126A",
CFG_METHOD_3,
Rx_Fetch_Number_8 | Rx_Close_Multiple | RxCfg_pause_slot_en | EnableInnerVlan | EnableOuterVlan | (RX_DMA_BURST_512 << RxCfgDMAShift),
0xff7e5880,
Jumbo_Frame_9k),
_R("Unknown",
CFG_METHOD_DEFAULT,
(RX_DMA_BURST_512 << RxCfgDMAShift),
0xff7e5880,
Jumbo_Frame_1k)
};
#undef _R
#ifndef PCI_VENDOR_ID_DLINK
#define PCI_VENDOR_ID_DLINK 0x1186
#endif
static struct pci_device_id rtl8126_pci_tbl[] = {
{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8126), },
{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x5000), },
{0,},
};
MODULE_DEVICE_TABLE(pci, rtl8126_pci_tbl);
static int use_dac = 1;
static int timer_count = 0x2600;
static int timer_count_v2 = (0x2600 / 0x100);
static struct {
u32 msg_enable;
} debug = { -1 };
static unsigned int speed_mode = SPEED_5000;
static unsigned int duplex_mode = DUPLEX_FULL;
static unsigned int autoneg_mode = AUTONEG_ENABLE;
#ifdef CONFIG_ASPM
static int aspm = 1;
#else
static int aspm = 0;
#endif
#ifdef ENABLE_S5WOL
static int s5wol = 1;
#else
static int s5wol = 0;
#endif
#ifdef ENABLE_S5_KEEP_CURR_MAC
static int s5_keep_curr_mac = 1;
#else
static int s5_keep_curr_mac = 0;
#endif
#ifdef ENABLE_EEE
static int eee_enable = 1;
#else
static int eee_enable = 0;
#endif
#ifdef CONFIG_SOC_LAN
static ulong hwoptimize = HW_PATCH_SOC_LAN;
#else
static ulong hwoptimize = 0;
#endif
#ifdef ENABLE_S0_MAGIC_PACKET
static int s0_magic_packet = 1;
#else
static int s0_magic_packet = 0;
#endif
#ifdef ENABLE_TX_NO_CLOSE
static int tx_no_close_enable = 1;
#else
static int tx_no_close_enable = 0;
#endif
#ifdef ENABLE_PTP_MASTER_MODE
static int enable_ptp_master_mode = 1;
#else
static int enable_ptp_master_mode = 0;
#endif
#ifdef DISABLE_WOL_SUPPORT
static int disable_wol_support = 1;
#else
static int disable_wol_support = 0;
#endif
#ifdef ENABLE_DOUBLE_VLAN
static int enable_double_vlan = 1;
#else
static int enable_double_vlan = 0;
#endif
MODULE_AUTHOR("Realtek and the Linux r8126 crew <netdev@vger.kernel.org>");
MODULE_DESCRIPTION("Realtek r8126 Ethernet controller driver");
module_param(speed_mode, uint, 0);
MODULE_PARM_DESC(speed_mode, "force phy operation. Deprecated by ethtool (8).");
module_param(duplex_mode, uint, 0);
MODULE_PARM_DESC(duplex_mode, "force phy operation. Deprecated by ethtool (8).");
module_param(autoneg_mode, uint, 0);
MODULE_PARM_DESC(autoneg_mode, "force phy operation. Deprecated by ethtool (8).");
module_param(aspm, int, 0);
MODULE_PARM_DESC(aspm, "Enable ASPM.");
module_param(s5wol, int, 0);
MODULE_PARM_DESC(s5wol, "Enable Shutdown Wake On Lan.");
module_param(s5_keep_curr_mac, int, 0);
MODULE_PARM_DESC(s5_keep_curr_mac, "Enable Shutdown Keep Current MAC Address.");
module_param(use_dac, int, 0);
MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
module_param(timer_count, int, 0);
MODULE_PARM_DESC(timer_count, "Timer Interrupt Interval.");
module_param(eee_enable, int, 0);
MODULE_PARM_DESC(eee_enable, "Enable Energy Efficient Ethernet.");
module_param(hwoptimize, ulong, 0);
MODULE_PARM_DESC(hwoptimize, "Enable HW optimization function.");
module_param(s0_magic_packet, int, 0);
MODULE_PARM_DESC(s0_magic_packet, "Enable S0 Magic Packet.");
module_param(tx_no_close_enable, int, 0);
MODULE_PARM_DESC(tx_no_close_enable, "Enable TX No Close.");
module_param(enable_ptp_master_mode, int, 0);
MODULE_PARM_DESC(enable_ptp_master_mode, "Enable PTP Master Mode.");
module_param(disable_wol_support, int, 0);
MODULE_PARM_DESC(disable_wol_support, "Disable PM support.");
module_param(enable_double_vlan, int, 0);
MODULE_PARM_DESC(enable_double_vlan, "Enable Double VLAN.");
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
module_param_named(debug, debug.msg_enable, int, 0);
MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
#endif//LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
MODULE_LICENSE("GPL");
#ifdef ENABLE_USE_FIRMWARE_FILE
MODULE_FIRMWARE(FIRMWARE_8126A_2);
MODULE_FIRMWARE(FIRMWARE_8126A_3);
#endif
MODULE_VERSION(RTL8126_VERSION);
/*
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
static void rtl8126_esd_timer(unsigned long __opaque);
#else
static void rtl8126_esd_timer(struct timer_list *t);
#endif
*/
/*
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
static void rtl8126_link_timer(unsigned long __opaque);
#else
static void rtl8126_link_timer(struct timer_list *t);
#endif
*/
static netdev_tx_t rtl8126_start_xmit(struct sk_buff *skb, struct net_device *dev);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
static irqreturn_t rtl8126_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
#else
static irqreturn_t rtl8126_interrupt(int irq, void *dev_instance);
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
static irqreturn_t rtl8126_interrupt_msix(int irq, void *dev_instance, struct pt_regs *regs);
#else
static irqreturn_t rtl8126_interrupt_msix(int irq, void *dev_instance);
#endif
static void rtl8126_set_rx_mode(struct net_device *dev);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)
static void rtl8126_tx_timeout(struct net_device *dev, unsigned int txqueue);
#else
static void rtl8126_tx_timeout(struct net_device *dev);
#endif
static int rtl8126_rx_interrupt(struct net_device *, struct rtl8126_private *, struct rtl8126_rx_ring *, napi_budget);
static int rtl8126_tx_interrupt(struct rtl8126_tx_ring *ring, int budget);
static int rtl8126_tx_interrupt_with_vector(struct rtl8126_private *tp, const int message_id, int budget);
static void rtl8126_wait_for_quiescence(struct net_device *dev);
static int rtl8126_change_mtu(struct net_device *dev, int new_mtu);
static void rtl8126_down(struct net_device *dev);
static int rtl8126_set_mac_address(struct net_device *dev, void *p);
static void rtl8126_rar_set(struct rtl8126_private *tp, const u8 *addr);
static void rtl8126_desc_addr_fill(struct rtl8126_private *);
static void rtl8126_tx_desc_init(struct rtl8126_private *tp);
static void rtl8126_rx_desc_init(struct rtl8126_private *tp);
static u16 rtl8126_get_hw_phy_mcu_code_ver(struct rtl8126_private *tp);
static void rtl8126_phy_power_up(struct net_device *dev);
static void rtl8126_phy_power_down(struct net_device *dev);
static int rtl8126_set_speed(struct net_device *dev, u8 autoneg, u32 speed, u8 duplex, u64 adv);
static bool rtl8126_set_phy_mcu_patch_request(struct rtl8126_private *tp);
static bool rtl8126_clear_phy_mcu_patch_request(struct rtl8126_private *tp);
#ifdef CONFIG_R8126_NAPI
static int rtl8126_poll(napi_ptr napi, napi_budget budget);
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
static void rtl8126_reset_task(void *_data);
static void rtl8126_esd_task(void *_data);
static void rtl8126_linkchg_task(void *_data);
#else
static void rtl8126_reset_task(struct work_struct *work);
static void rtl8126_esd_task(struct work_struct *work);
static void rtl8126_linkchg_task(struct work_struct *work);
#endif
static void rtl8126_schedule_reset_work(struct rtl8126_private *tp);
static void rtl8126_schedule_esd_work(struct rtl8126_private *tp);
static void rtl8126_schedule_linkchg_work(struct rtl8126_private *tp);
static void rtl8126_init_all_schedule_work(struct rtl8126_private *tp);
static void rtl8126_cancel_all_schedule_work(struct rtl8126_private *tp);
static inline struct device *tp_to_dev(struct rtl8126_private *tp)
{
return &tp->pci_dev->dev;
}
#if ((LINUX_VERSION_CODE < KERNEL_VERSION(4,7,0) && \
LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,00)))
void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
u32 legacy_u32)
{
bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
dst[0] = legacy_u32;
}
bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
const unsigned long *src)
{
bool retval = true;
/* TODO: following test will soon always be true */
if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
__ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
bitmap_fill(ext, 32);
bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
if (bitmap_intersects(ext, src,
__ETHTOOL_LINK_MODE_MASK_NBITS)) {
/* src mask goes beyond bit 31 */
retval = false;
}
}
*legacy_u32 = src[0];
return retval;
}
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
#ifndef LPA_1000FULL
#define LPA_1000FULL 0x0800
#endif
#ifndef LPA_1000HALF
#define LPA_1000HALF 0x0400
#endif
#endif //LINUX_VERSION_CODE < KERNEL_VERSION(3,3,0)
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
static inline void eth_hw_addr_random(struct net_device *dev)
{
random_ether_addr(dev->dev_addr);
}
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
#undef ethtool_ops
#define ethtool_ops _kc_ethtool_ops
struct _kc_ethtool_ops {
int (*get_settings)(struct net_device *, struct ethtool_cmd *);
int (*set_settings)(struct net_device *, struct ethtool_cmd *);
void (*get_drvinfo)(struct net_device *, struct ethtool_drvinfo *);
int (*get_regs_len)(struct net_device *);
void (*get_regs)(struct net_device *, struct ethtool_regs *, void *);
void (*get_wol)(struct net_device *, struct ethtool_wolinfo *);
int (*set_wol)(struct net_device *, struct ethtool_wolinfo *);
u32 (*get_msglevel)(struct net_device *);
void (*set_msglevel)(struct net_device *, u32);
int (*nway_reset)(struct net_device *);
u32 (*get_link)(struct net_device *);
int (*get_eeprom_len)(struct net_device *);
int (*get_eeprom)(struct net_device *, struct ethtool_eeprom *, u8 *);
int (*set_eeprom)(struct net_device *, struct ethtool_eeprom *, u8 *);
int (*get_coalesce)(struct net_device *, struct ethtool_coalesce *);
int (*set_coalesce)(struct net_device *, struct ethtool_coalesce *);
void (*get_ringparam)(struct net_device *, struct ethtool_ringparam *);
int (*set_ringparam)(struct net_device *, struct ethtool_ringparam *);
void (*get_pauseparam)(struct net_device *,
struct ethtool_pauseparam*);
int (*set_pauseparam)(struct net_device *,
struct ethtool_pauseparam*);
u32 (*get_rx_csum)(struct net_device *);
int (*set_rx_csum)(struct net_device *, u32);
u32 (*get_tx_csum)(struct net_device *);
int (*set_tx_csum)(struct net_device *, u32);
u32 (*get_sg)(struct net_device *);
int (*set_sg)(struct net_device *, u32);
u32 (*get_tso)(struct net_device *);
int (*set_tso)(struct net_device *, u32);
int (*self_test_count)(struct net_device *);
void (*self_test)(struct net_device *, struct ethtool_test *, u64 *);
void (*get_strings)(struct net_device *, u32 stringset, u8 *);
int (*phys_id)(struct net_device *, u32);
int (*get_stats_count)(struct net_device *);
void (*get_ethtool_stats)(struct net_device *, struct ethtool_stats *,
u64 *);
} *ethtool_ops = NULL;
#undef SET_ETHTOOL_OPS
#define SET_ETHTOOL_OPS(netdev, ops) (ethtool_ops = (ops))
#endif //LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,16,0)
#ifndef SET_ETHTOOL_OPS
#define SET_ETHTOOL_OPS(netdev,ops) \
((netdev)->ethtool_ops = (ops))
#endif //SET_ETHTOOL_OPS
#endif //LINUX_VERSION_CODE >= KERNEL_VERSION(3,16,0)
//#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
#ifndef netif_msg_init
#define netif_msg_init _kc_netif_msg_init
/* copied from linux kernel 2.6.20 include/linux/netdevice.h */
static inline u32 netif_msg_init(int debug_value, int default_msg_enable_bits)
{
/* use default */
if (debug_value < 0 || debug_value >= (sizeof(u32) * 8))
return default_msg_enable_bits;
if (debug_value == 0) /* no output */
return 0;
/* set low N bits */
return (1 << debug_value) - 1;
}
#endif //LINUX_VERSION_CODE < KERNEL_VERSION(2,6,5)
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22)
static inline void eth_copy_and_sum (struct sk_buff *dest,
const unsigned char *src,
int len, int base)
{
skb_copy_to_linear_data(dest, src, len);
}
#endif //LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22)
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7)
/* copied from linux kernel 2.6.20 /include/linux/time.h */
/* Parameters used to convert the timespec values: */
#define MSEC_PER_SEC 1000L
/* copied from linux kernel 2.6.20 /include/linux/jiffies.h */
/*
* Change timeval to jiffies, trying to avoid the
* most obvious overflows..
*
* And some not so obvious.
*
* Note that we don't want to return MAX_LONG, because
* for various timeout reasons we often end up having
* to wait "jiffies+1" in order to guarantee that we wait
* at _least_ "jiffies" - so "jiffies+1" had better still
* be positive.
*/
#define MAX_JIFFY_OFFSET ((~0UL >> 1)-1)
/*
* Convert jiffies to milliseconds and back.
*
* Avoid unnecessary multiplications/divisions in the
* two most common HZ cases:
*/
static inline unsigned int _kc_jiffies_to_msecs(const unsigned long j)
{
#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
return (MSEC_PER_SEC / HZ) * j;
#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
#else
return (j * MSEC_PER_SEC) / HZ;
#endif
}
static inline unsigned long _kc_msecs_to_jiffies(const unsigned int m)
{
if (m > _kc_jiffies_to_msecs(MAX_JIFFY_OFFSET))
return MAX_JIFFY_OFFSET;
#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
return (m + (MSEC_PER_SEC / HZ) - 1) / (MSEC_PER_SEC / HZ);
#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
return m * (HZ / MSEC_PER_SEC);
#else
return (m * HZ + MSEC_PER_SEC - 1) / MSEC_PER_SEC;
#endif
}
#endif //LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7)
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
/* copied from linux kernel 2.6.12.6 /include/linux/pm.h */
typedef int __bitwise pci_power_t;
/* copied from linux kernel 2.6.12.6 /include/linux/pci.h */
typedef u32 __bitwise pm_message_t;
#define PCI_D0 ((pci_power_t __force) 0)
#define PCI_D1 ((pci_power_t __force) 1)
#define PCI_D2 ((pci_power_t __force) 2)
#define PCI_D3hot ((pci_power_t __force) 3)
#define PCI_D3cold ((pci_power_t __force) 4)
#define PCI_POWER_ERROR ((pci_power_t __force) -1)
/* copied from linux kernel 2.6.12.6 /drivers/pci/pci.c */
/**
* pci_choose_state - Choose the power state of a PCI device
* @dev: PCI device to be suspended
* @state: target sleep state for the whole system. This is the value
* that is passed to suspend() function.
*
* Returns PCI power state suitable for given device and given system
* message.
*/
pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
{
if (!pci_find_capability(dev, PCI_CAP_ID_PM))
return PCI_D0;
switch (state) {
case 0:
return PCI_D0;
case 3:
return PCI_D3hot;
default:
printk("They asked me for state %d\n", state);
// BUG();
}
return PCI_D0;
}
#endif //LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9)
/**
* msleep_interruptible - sleep waiting for waitqueue interruptions
* @msecs: Time in milliseconds to sleep for
*/
#define msleep_interruptible _kc_msleep_interruptible
unsigned long _kc_msleep_interruptible(unsigned int msecs)
{
unsigned long timeout = _kc_msecs_to_jiffies(msecs);
while (timeout && !signal_pending(current)) {
set_current_state(TASK_INTERRUPTIBLE);
timeout = schedule_timeout(timeout);
}
return _kc_jiffies_to_msecs(timeout);
}
#endif //LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9)
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7)
/* copied from linux kernel 2.6.20 include/linux/sched.h */
#ifndef __sched
#define __sched __attribute__((__section__(".sched.text")))
#endif
/* copied from linux kernel 2.6.20 kernel/timer.c */
signed long __sched schedule_timeout_uninterruptible(signed long timeout)
{
__set_current_state(TASK_UNINTERRUPTIBLE);
return schedule_timeout(timeout);
}
/* copied from linux kernel 2.6.20 include/linux/mii.h */
#undef if_mii
#define if_mii _kc_if_mii
static inline struct mii_ioctl_data *if_mii(struct ifreq *rq)
{
return (struct mii_ioctl_data *) &rq->ifr_ifru;
}
#endif //LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7)
static u32 rtl8126_read_thermal_sensor(struct rtl8126_private *tp)
{
u16 ts_digout;
switch (tp->mcfg) {
default:
ts_digout = 0xffff;
break;
}
return ts_digout;
}
int rtl8126_dump_tally_counter(struct rtl8126_private *tp, dma_addr_t paddr)
{
u32 cmd;
u32 WaitCnt;
int retval = -1;
RTL_W32(tp, CounterAddrHigh, (u64)paddr >> 32);
cmd = (u64)paddr & DMA_BIT_MASK(32);
RTL_W32(tp, CounterAddrLow, cmd);
RTL_W32(tp, CounterAddrLow, cmd | CounterDump);
WaitCnt = 0;
while (RTL_R32(tp, CounterAddrLow) & CounterDump) {
udelay(10);
WaitCnt++;
if (WaitCnt > 20)
break;
}
if (WaitCnt <= 20)
retval = 0;
return retval;
}
static u32
rtl8126_get_hw_clo_ptr(struct rtl8126_tx_ring *ring)
{
struct rtl8126_private *tp = ring->priv;
switch (tp->HwSuppTxNoCloseVer) {
case 3:
return RTL_R16(tp, ring->hw_clo_ptr_reg);
case 4:
case 5:
case 6:
return RTL_R32(tp, ring->hw_clo_ptr_reg);
default:
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
WARN_ON(1);
#endif
return 0;
}
}
static u32
rtl8126_get_sw_tail_ptr(struct rtl8126_tx_ring *ring)
{
struct rtl8126_private *tp = ring->priv;
switch (tp->HwSuppTxNoCloseVer) {
case 3:
return RTL_R16(tp, ring->sw_tail_ptr_reg);
case 4:
case 5:
case 6:
return RTL_R32(tp, ring->sw_tail_ptr_reg);
default:
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
WARN_ON(1);
#endif
return 0;
}
}
static bool
rtl8126_sysfs_testmode_on(struct rtl8126_private *tp)
{
#ifdef ENABLE_R8126_SYSFS
return !!tp->testmode;
#else
return 1;
#endif
}
static u32 rtl8126_convert_link_speed(u16 status)
{
u32 speed = SPEED_UNKNOWN;
if (status & LinkStatus) {
if (status & _5000bpsF)
speed = SPEED_5000;
else if (status & _2500bpsF)
speed = SPEED_2500;
else if (status & _1000bpsF)
speed = SPEED_1000;
else if (status & _100bps)
speed = SPEED_100;
else if (status & _10bps)
speed = SPEED_10;
}
return speed;
}
static void rtl8126_mdi_swap(struct rtl8126_private *tp)
{
int i;
u16 reg, val, mdi_reverse;
u16 tps_p0, tps_p1, tps_p2, tps_p3, tps_p3_p0;
switch (tp->mcfg) {
default:
return;
};
tps_p3_p0 = rtl8126_mac_ocp_read(tp, 0xD440) & 0xF000;
tps_p3 = !!(tps_p3_p0 & BIT_15);
tps_p2 = !!(tps_p3_p0 & BIT_14);
tps_p1 = !!(tps_p3_p0 & BIT_13);
tps_p0 = !!(tps_p3_p0 & BIT_12);
mdi_reverse = rtl8126_mac_ocp_read(tp, 0xD442);
if ((mdi_reverse & BIT_5) && tps_p3_p0 == 0xA000)
return;
if (!(mdi_reverse & BIT_5))
val = tps_p0 << 8 |
tps_p1 << 9 |
tps_p2 << 10 |
tps_p3 << 11;
else
val = tps_p3 << 8 |
tps_p2 << 9 |
tps_p1 << 10 |
tps_p0 << 11;
for (i=8; i<12; i++) {
rtl8126_mdio_direct_write_phy_ocp(tp, 0xA436, reg);
rtl8126_clear_and_set_eth_phy_ocp_bit(tp,
0xA438,
BIT(i),
val & BIT(i));
}
}
static int rtl8126_vcd_test(struct rtl8126_private *tp)
{
u16 val;
u32 wait_cnt;
int ret = -1;
rtl8126_mdi_swap(tp);
rtl8126_clear_eth_phy_ocp_bit(tp, 0xA422, BIT(0));
rtl8126_set_eth_phy_ocp_bit(tp, 0xA422, 0x00F0);
rtl8126_set_eth_phy_ocp_bit(tp, 0xA422, BIT(0));
wait_cnt = 0;
do {
mdelay(1);
val = rtl8126_mdio_direct_read_phy_ocp(tp, 0xA422);
wait_cnt++;
} while (!(val & BIT_15) && (wait_cnt < 5000));
if (wait_cnt == 5000)
goto exit;
ret = 0;
exit:
return ret;
}
static void rtl8126_get_cp_len(struct rtl8126_private *tp,
int cp_len[RTL8126_CP_NUM])
{
int i;
u16 status;
int tmp_cp_len;
status = RTL_R16(tp, PHYstatus);
if (status & LinkStatus) {
if (status & _10bps) {
tmp_cp_len = -1;
} else if (status & (_100bps | _1000bpsF)) {
rtl8126_mdio_write(tp, 0x1f, 0x0a88);
tmp_cp_len = rtl8126_mdio_read(tp, 0x10);
} else if (status & _2500bpsF) {
switch (tp->mcfg) {
default:
rtl8126_mdio_write(tp, 0x1f, 0x0acb);
tmp_cp_len = rtl8126_mdio_read(tp, 0x15);
tmp_cp_len >>= 2;
break;
}
} else
tmp_cp_len = 0;
} else
tmp_cp_len = 0;
if (tmp_cp_len > 0)
tmp_cp_len &= 0xff;
for (i=0; i<RTL8126_CP_NUM; i++)
cp_len[i] = tmp_cp_len;
rtl8126_mdio_write(tp, 0x1f, 0x0000);
for (i=0; i<RTL8126_CP_NUM; i++)
if (cp_len[i] > RTL8126_MAX_SUPPORT_CP_LEN)
cp_len[i] = RTL8126_MAX_SUPPORT_CP_LEN;
return;
}
static int __rtl8126_get_cp_status(u16 val)
{
switch (val) {
case 0x0060:
return rtl8126_cp_normal;
case 0x0048:
return rtl8126_cp_open;
case 0x0050:
return rtl8126_cp_short;
case 0x0042:
case 0x0044:
return rtl8126_cp_mismatch;
default:
return rtl8126_cp_normal;
}
}
static int _rtl8126_get_cp_status(struct rtl8126_private *tp, u8 pair_num)
{
u16 val;
int cp_status = rtl8126_cp_unknown;
if (pair_num > 3)
goto exit;
rtl8126_mdio_direct_write_phy_ocp(tp, 0xA436, 0x8027 + 4 * pair_num);
val = rtl8126_mdio_direct_read_phy_ocp(tp, 0xA438);
cp_status = __rtl8126_get_cp_status(val);
exit:
return cp_status;
}
static const char * rtl8126_get_cp_status_string(int cp_status)
{
switch(cp_status) {
case rtl8126_cp_normal:
return "normal ";
case rtl8126_cp_short:
return "short ";
case rtl8126_cp_open:
return "open ";
case rtl8126_cp_mismatch:
return "mismatch";
default:
return "unknown ";
}
}
static u16 rtl8126_get_cp_pp(struct rtl8126_private *tp, u8 pair_num)
{
u16 pp = 0;
if (pair_num > 3)
goto exit;
rtl8126_mdio_direct_write_phy_ocp(tp, 0xA436, 0x8029 + 4 * pair_num);
pp = rtl8126_mdio_direct_read_phy_ocp(tp, 0xA438);
pp &= 0x3fff;
pp /= 80;
exit:
return pp;
}
static void rtl8126_get_cp_status(struct rtl8126_private *tp,
int cp_status[RTL8126_CP_NUM],
bool poe_mode)
{
u16 status;
int i;
status = RTL_R16(tp, PHYstatus);
if (status & LinkStatus && !(status & (_10bps | _100bps))) {
for (i=0; i<RTL8126_CP_NUM; i++)
cp_status[i] = rtl8126_cp_normal;
} else {
/* cannot do vcd when link is on */
rtl8126_vcd_test(tp);
for (i=0; i<RTL8126_CP_NUM; i++)
cp_status[i] = _rtl8126_get_cp_status(tp, i);
}
if (poe_mode) {
for (i=0; i<RTL8126_CP_NUM; i++) {
if (cp_status[i] == rtl8126_cp_mismatch)
cp_status[i] = rtl8126_cp_normal;
}
}
}
#ifdef ENABLE_R8126_PROCFS
/****************************************************************************
* -----------------------------PROCFS STUFF-------------------------
*****************************************************************************
*/
static struct proc_dir_entry *rtl8126_proc;
static int proc_init_num = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
static int proc_get_driver_variable(struct seq_file *m, void *v)
{
struct net_device *dev = m->private;
struct rtl8126_private *tp = netdev_priv(dev);
seq_puts(m, "\nDump Driver Variable\n");
rtnl_lock();
seq_puts(m, "Variable\tValue\n----------\t-----\n");
seq_printf(m, "MODULENAME\t%s\n", MODULENAME);
seq_printf(m, "driver version\t%s\n", RTL8126_VERSION);
seq_printf(m, "mcfg\t%d\n", tp->mcfg);
seq_printf(m, "chipset\t%d\n", tp->chipset);
seq_printf(m, "chipset_name\t%s\n", rtl_chip_info[tp->chipset].name);
seq_printf(m, "mtu\t%d\n", dev->mtu);
seq_printf(m, "NUM_RX_DESC\t0x%x\n", tp->rx_ring[0].num_rx_desc);
seq_printf(m, "cur_rx0\t0x%x\n", tp->rx_ring[0].cur_rx);
seq_printf(m, "dirty_rx0\t0x%x\n", tp->rx_ring[0].dirty_rx);
seq_printf(m, "cur_rx1\t0x%x\n", tp->rx_ring[1].cur_rx);
seq_printf(m, "dirty_rx1\t0x%x\n", tp->rx_ring[1].dirty_rx);
seq_printf(m, "cur_rx2\t0x%x\n", tp->rx_ring[2].cur_rx);
seq_printf(m, "dirty_rx2\t0x%x\n", tp->rx_ring[2].dirty_rx);
seq_printf(m, "cur_rx3\t0x%x\n", tp->rx_ring[3].cur_rx);
seq_printf(m, "dirty_rx3\t0x%x\n", tp->rx_ring[3].dirty_rx);
seq_printf(m, "NUM_TX_DESC\t0x%x\n", tp->tx_ring[0].num_tx_desc);
seq_printf(m, "cur_tx0\t0x%x\n", tp->tx_ring[0].cur_tx);
seq_printf(m, "dirty_tx0\t0x%x\n", tp->tx_ring[0].dirty_tx);
seq_printf(m, "cur_tx1\t0x%x\n", tp->tx_ring[1].cur_tx);
seq_printf(m, "dirty_tx1\t0x%x\n", tp->tx_ring[1].dirty_tx);
seq_printf(m, "rx_buf_sz\t0x%x\n", tp->rx_buf_sz);
#ifdef ENABLE_PAGE_REUSE
seq_printf(m, "rx_buf_page_order\t0x%x\n", tp->rx_buf_page_order);
seq_printf(m, "rx_buf_page_size\t0x%x\n", tp->rx_buf_page_size);
seq_printf(m, "page_reuse_fail_cnt\t0x%x\n", tp->page_reuse_fail_cnt);
#endif //ENABLE_PAGE_REUSE
seq_printf(m, "esd_flag\t0x%x\n", tp->esd_flag);
seq_printf(m, "pci_cfg_is_read\t0x%x\n", tp->pci_cfg_is_read);
seq_printf(m, "rtl8126_rx_config\t0x%x\n", tp->rtl8126_rx_config);
seq_printf(m, "cp_cmd\t0x%x\n", tp->cp_cmd);
seq_printf(m, "intr_mask\t0x%x\n", tp->intr_mask);
seq_printf(m, "timer_intr_mask\t0x%x\n", tp->timer_intr_mask);
seq_printf(m, "wol_enabled\t0x%x\n", tp->wol_enabled);
seq_printf(m, "wol_opts\t0x%x\n", tp->wol_opts);
seq_printf(m, "efuse_ver\t0x%x\n", tp->efuse_ver);
seq_printf(m, "eeprom_type\t0x%x\n", tp->eeprom_type);
seq_printf(m, "autoneg\t0x%x\n", tp->autoneg);
seq_printf(m, "duplex\t0x%x\n", tp->duplex);
seq_printf(m, "speed\t%d\n", tp->speed);