-
Notifications
You must be signed in to change notification settings - Fork 79
/
fec_main.c
5339 lines (4505 loc) · 132 KB
/
fec_main.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+
/*
* Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
* Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
*
* Right now, I am very wasteful with the buffers. I allocate memory
* pages and then divide them into 2K frame buffers. This way I know I
* have buffers large enough to hold one frame within one buffer descriptor.
* Once I get this working, I will use 64 or 128 byte CPM buffers, which
* will be much more memory efficient and will easily handle lots of
* small packets.
*
* Much better multiple PHY support by Magnus Damm.
* Copyright (c) 2000 Ericsson Radio Systems AB.
*
* Support for FEC controller of ColdFire processors.
* Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
*
* Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
* Copyright (c) 2004-2006 Macq Electronique SA.
*
* Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/pm_runtime.h>
#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <net/ip.h>
#include <net/tso.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <linux/bitops.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/clk.h>
#include <linux/crc32.h>
#include <linux/platform_device.h>
#include <linux/mdio.h>
#include <linux/phy.h>
#include <linux/fec.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/of_mdio.h>
#include <linux/of_net.h>
#include <linux/regulator/consumer.h>
#include <linux/if_vlan.h>
#include <linux/pinctrl/consumer.h>
#include <linux/prefetch.h>
#include <linux/mfd/syscon.h>
#include <linux/regmap.h>
#include <soc/imx/cpuidle.h>
#include <asm/cacheflush.h>
#include "fec.h"
#if defined(CONFIG_KSZ_SWITCH_EMBEDDED)
#include <linux/of_irq.h>
#include <linux/spi/spi.h>
#include <linux/ip.h>
#include <net/ip.h>
#include <net/ipv6.h>
/* Need to predefine get_sysfs_data. */
#ifndef get_sysfs_data
struct ksz_port;
static void get_sysfs_data_(struct net_device *dev,
struct semaphore **proc_sem, struct ksz_port **port);
#define get_sysfs_data get_sysfs_data_
#endif
static void copy_old_skb(struct sk_buff *old, struct sk_buff *skb);
#define DO_NOT_USE_COPY_SKB
#if defined(CONFIG_IBA_KSZ9897)
#include "../micrel/iba-ksz9897.c"
#elif defined(CONFIG_HAVE_KSZ9897)
#include "../micrel/spi-ksz9897.c"
#elif defined(CONFIG_HAVE_KSZ8795)
#include "../micrel/spi-ksz8795.c"
#elif defined(CONFIG_SMI_KSZ8895)
#include "../micrel/smi-ksz8895.c"
#elif defined(CONFIG_HAVE_KSZ8895)
#include "../micrel/spi-ksz8895.c"
#elif defined(CONFIG_SMI_KSZ8863)
#include "../micrel/smi-ksz8863.c"
#elif defined(CONFIG_HAVE_KSZ8863)
#include "../micrel/spi-ksz8863.c"
#elif defined(CONFIG_IBA_LAN937X)
#include "../microchip/iba-lan937x.c"
#elif defined(CONFIG_SMI_LAN937X)
#include "../microchip/smi-lan937x.c"
#elif defined(CONFIG_HAVE_LAN937X)
#include "../microchip/spi-lan937x.c"
#endif
#endif
#if defined(CONFIG_KSZ_SWITCH) && !defined(CONFIG_KSZ_SWITCH_EMBEDDED)
#ifdef CONFIG_HAVE_LAN937X
#include "../microchip/lan937x_dev.h"
#else
#include "../micrel/ksz_spi_net.h"
#endif
#endif
#ifdef CONFIG_KSZ_SWITCH
#if !defined(get_sysfs_data) || defined(CONFIG_KSZ_SWITCH_EMBEDDED)
static void get_sysfs_data_(struct net_device *dev,
struct semaphore **proc_sem, struct ksz_port **port)
{
struct fec_enet_private *priv = netdev_priv(dev);
struct sw_priv *hw_priv;
hw_priv = priv->parent;
*port = &priv->port;
*proc_sem = &hw_priv->proc_sem;
} /* get_sysfs_data */
#endif
#ifndef get_sysfs_data
#define get_sysfs_data get_sysfs_data_
#endif
#if !defined(CONFIG_KSZ_SWITCH_EMBEDDED)
#define USE_SPEED_LINK
#define USE_MIB
#if defined(CONFIG_HAVE_KSZ9897)
#include "../micrel/ksz_sw_sysfs_9897.c"
#elif defined(CONFIG_HAVE_KSZ8795)
#include "../micrel/ksz_sw_sysfs_8795.c"
#elif defined(CONFIG_HAVE_KSZ8895)
#include "../micrel/ksz_sw_sysfs_8895.c"
#elif defined(CONFIG_HAVE_KSZ8863)
#include "../micrel/ksz_sw_sysfs.c"
#elif defined(CONFIG_HAVE_KSZ8463)
#include "../micrel/ksz_sw_sysfs.c"
#elif defined(CONFIG_HAVE_LAN937X)
#include "../microchip/lan937x_sw_sysfs.c"
#endif
#ifdef CONFIG_1588_PTP
#if defined(CONFIG_HAVE_KSZ9897)
#include "../micrel/ksz_ptp_sysfs.c"
#elif defined(CONFIG_HAVE_LAN937X)
#include "../microchip/lan937x_ptp_sysfs.c"
#endif
#endif
#ifdef CONFIG_KSZ_DLR
#include "../micrel/ksz_dlr_sysfs.c"
#endif
#endif
static inline int sw_is_switch(struct ksz_sw *sw)
{
return sw != NULL;
}
static void copy_old_skb(struct sk_buff *old, struct sk_buff *skb)
{
if (old->ip_summed) {
int offset = old->head - old->data;
skb->head = skb->data + offset;
}
skb->dev = old->dev;
skb->sk = old->sk;
skb->protocol = old->protocol;
skb->ip_summed = old->ip_summed;
skb->csum = old->csum;
skb_shinfo(skb)->tx_flags = skb_shinfo(old)->tx_flags;
skb_set_network_header(skb, ETH_HLEN);
dev_kfree_skb_any(old);
} /* copy_old_skb */
static int fec_enet_add_vid(struct net_device *dev, __be16 proto, u16 vid)
{
struct fec_enet_private *fep = netdev_priv(dev);
struct ksz_sw *sw = fep->port.sw;
if (sw_is_switch(sw))
sw->net_ops->add_vid(sw, vid);
return 0;
}
static int fec_enet_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
{
struct fec_enet_private *fep = netdev_priv(dev);
struct ksz_sw *sw = fep->port.sw;
if (sw_is_switch(sw))
sw->net_ops->kill_vid(sw, vid);
return 0;
}
static int sw_device_seen;
#if !defined(CONFIG_KSZ_IBA_ONLY)
static struct ksz_sw *check_avail_switch(struct net_device *netdev, int id)
{
int phy_mode;
char phy_id[MII_BUS_ID_SIZE];
char bus_id[MII_BUS_ID_SIZE];
struct ksz_sw *sw = NULL;
struct phy_device *phydev = NULL;
/* Check whether MII switch exists. */
phy_mode = PHY_INTERFACE_MODE_MII;
snprintf(bus_id, MII_BUS_ID_SIZE, "sw.%d", id);
snprintf(phy_id, MII_BUS_ID_SIZE, PHY_ID_FMT, bus_id, 0);
phydev = phy_attach(netdev, phy_id, phy_mode);
if (!IS_ERR(phydev)) {
struct phy_priv *phydata = phydev->priv;
sw = phydata->port->sw;
/*
* In case multiple devices mode is used and this phydev is not
* attached again.
*/
if (sw)
phydev->interface = sw->interface;
phy_detach(phydev);
}
return sw;
} /* check_avail_switch */
static int fec_enet_sw_chk(struct fec_enet_private *fep)
{
struct ksz_sw *sw;
sw = fep->port.sw;
if (!sw) {
sw = check_avail_switch(fep->netdev, sw_device_seen);
if (!sw_is_switch(sw))
return -ENXIO;
}
fep->port.sw = sw;
return 0;
}
#endif
#if defined(CONFIG_KSZ_IBA_ONLY) || defined(CONFIG_KSZ_SMI)
static int get_sw_irq(struct fec_enet_private *fep, struct device **ext_dev)
{
struct device *dev;
int spi_bus;
int spi_select;
char name[20];
spi_select = 0;
for (spi_bus = 0; spi_bus < 2; spi_bus++) {
sprintf(name, "spi%d.%d\n", spi_bus, spi_select);
dev = bus_find_device_by_name(&spi_bus_type, NULL, name);
if (dev && dev->of_node) {
int irq = of_irq_get(dev->of_node, 0);
if (ext_dev)
*ext_dev = dev;
return irq;
}
}
return -1;
} /* get_sw_irq */
#endif
static void stop_dev_queues(struct ksz_sw *sw, struct net_device *hw_dev,
struct fec_enet_private *fep, u16 qid)
{
if (sw_is_switch(sw)) {
int dev_count = sw->dev_count + sw->dev_offset;
struct netdev_queue *nq;
struct net_device *dev;
int p;
for (p = 0; p < dev_count; p++) {
dev = sw->netdev[p];
if (!dev || dev == hw_dev)
continue;
if (netif_running(dev) || dev == fep->netdev) {
nq = netdev_get_tx_queue(dev, qid);
netif_tx_stop_queue(nq);
}
}
}
} /* stop_dev_queues */
static void wake_dev_queues(struct ksz_sw *sw, struct net_device *hw_dev,
u16 qid)
{
if (sw_is_switch(sw)) {
int dev_count = sw->dev_count + sw->dev_offset;
struct netdev_queue *nq;
struct net_device *dev;
int p;
for (p = 0; p < dev_count; p++) {
dev = sw->netdev[p];
if (!dev || dev == hw_dev)
continue;
if (netif_running(dev)) {
nq = netdev_get_tx_queue(dev, qid);
if (netif_tx_queue_stopped(nq))
netif_tx_wake_queue(nq);
}
}
wake_up_interruptible(&sw->queue);
}
} /* wake_dev_queues */
#if defined(CONFIG_HAVE_KSZ9897) || defined(CONFIG_HAVE_LAN937X)
static int priv_multi(void *ptr)
{
struct fec_enet_private *priv = ptr;
return (priv->multi & 1);
} /* priv_multi */
#endif
static int priv_promisc(void *ptr)
{
struct fec_enet_private *priv = ptr;
return priv->promisc;
} /* priv_promisc */
#if !defined(CONFIG_HAVE_KSZ9897) && !defined(CONFIG_HAVE_LAN937X)
static int priv_match_multi(void *ptr, u8 *data)
{
struct netdev_hw_addr *ha;
struct fec_enet_private *fep = ptr;
int drop = true;
netdev_for_each_mc_addr(ha, fep->netdev) {
if (!memcmp(data, ha->addr, ETH_ALEN)) {
drop = false;
break;
}
}
return drop;
} /* priv_match_multi */
#endif
static struct net_device *sw_rx_proc(struct ksz_sw *sw, struct sk_buff *skb,
__u8 *data)
{
struct net_device *dev;
struct fec_enet_private *fep;
int len = skb->len;
int rx_port = 0;
#if defined(CONFIG_KSZ_SWITCH) || defined(CONFIG_1588_PTP)
int forward = 0;
int tag = 0;
void *ptr = NULL;
void (*rx_tstamp)(void *ptr, struct sk_buff *skb) = NULL;
#endif
#ifdef CONFIG_1588_PTP
struct ptp_info *ptp = &sw->ptp_hw;
int ptp_tag = 0;
#endif
struct net_device *parent_dev = NULL;
struct sk_buff *parent_skb = NULL;
dev = sw->net_ops->rx_dev(sw, data, &len, &tag, &rx_port);
if (!dev) {
dev_kfree_skb_any(skb);
return NULL;
}
/* vlan_get_tag requires network device in socket buffer. */
skb->dev = dev;
/* skb_put is already used. */
if (len != skb->len) {
int diff = skb->len - len;
skb->len -= diff;
skb->tail -= diff;
}
fep = netdev_priv(dev);
/* Internal packets handled by the switch. */
if (!sw->net_ops->drv_rx(sw, skb, rx_port)) {
dev->stats.rx_packets++;
dev->stats.rx_bytes += skb->len;
return NULL;
}
if (!sw->net_ops->match_pkt(sw, &dev, (void **) &fep, priv_promisc,
#if defined(CONFIG_HAVE_KSZ9897) || defined(CONFIG_HAVE_LAN937X)
priv_multi,
#else
priv_match_multi,
#endif
skb, fep->hw_priv->hw_promisc)) {
dev_kfree_skb_irq(skb);
return NULL;
}
#ifdef CONFIG_1588_PTP
ptr = ptp;
if (sw->features & PTP_HW) {
if (ptp->ops->drop_pkt(ptp, skb, sw->vlan_id, &tag, &ptp_tag,
&forward)) {
dev_kfree_skb_any(skb);
return NULL;
}
if (ptp_tag)
rx_tstamp = ptp->ops->get_rx_tstamp;
}
#endif
#if defined(CONFIG_KSZ_SWITCH)
/* Need to forward to VLAN devices for PAE messages. */
if (!forward) {
struct ethhdr *eth = (struct ethhdr *) data;
if (eth->h_proto == htons(0x888E))
forward = FWD_VLAN_DEV | FWD_STP_DEV;
}
/* No VLAN port forwarding; need to send to parent. */
if ((forward & FWD_VLAN_DEV) && !tag)
forward &= ~FWD_VLAN_DEV;
dev = sw->net_ops->parent_rx(sw, dev, skb, &forward, &parent_dev,
&parent_skb);
/* dev may change. */
if (dev != skb->dev) {
skb->dev = dev;
}
sw->net_ops->port_vlan_rx(sw, dev, parent_dev, skb,
forward, tag, ptr, rx_tstamp);
#endif
return dev;
} /* sw_rx_proc */
#endif
static void set_multicast_list(struct net_device *ndev);
static void fec_enet_itr_coal_init(struct net_device *ndev);
#define DRIVER_NAME "fec"
#define FEC_ENET_GET_QUQUE(_x) ((_x == 0) ? 1 : ((_x == 1) ? 2 : 0))
/* Pause frame feild and FIFO threshold */
#define FEC_ENET_FCE (1 << 5)
#define FEC_ENET_RSEM_V 0x84
#define FEC_ENET_RSFL_V 16
#define FEC_ENET_RAEM_V 0x8
#define FEC_ENET_RAFL_V 0x8
#define FEC_ENET_OPD_V 0xFFF0
#define FEC_MDIO_PM_TIMEOUT 100 /* ms */
struct fec_devinfo {
u32 quirks;
u8 stop_gpr_reg;
u8 stop_gpr_bit;
};
static const struct fec_devinfo fec_imx25_info = {
.quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
FEC_QUIRK_HAS_FRREG,
};
static const struct fec_devinfo fec_imx27_info = {
.quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG,
};
static const struct fec_devinfo fec_imx28_info = {
.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
FEC_QUIRK_HAS_FRREG,
};
static const struct fec_devinfo fec_imx6q_info = {
.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
FEC_QUIRK_HAS_RACC,
.stop_gpr_reg = 0x34,
.stop_gpr_bit = 27,
};
static const struct fec_devinfo fec_mvf600_info = {
.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC,
};
static const struct fec_devinfo fec_imx6x_info = {
.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE,
};
static const struct fec_devinfo fec_imx6ul_info = {
.quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
FEC_QUIRK_HAS_COALESCE,
};
static struct platform_device_id fec_devtype[] = {
{
/* keep it for coldfire */
.name = DRIVER_NAME,
.driver_data = 0,
}, {
.name = "imx25-fec",
.driver_data = (kernel_ulong_t)&fec_imx25_info,
}, {
.name = "imx27-fec",
.driver_data = (kernel_ulong_t)&fec_imx27_info,
}, {
.name = "imx28-fec",
.driver_data = (kernel_ulong_t)&fec_imx28_info,
}, {
.name = "imx6q-fec",
.driver_data = (kernel_ulong_t)&fec_imx6q_info,
}, {
.name = "mvf600-fec",
.driver_data = (kernel_ulong_t)&fec_mvf600_info,
}, {
.name = "imx6sx-fec",
.driver_data = (kernel_ulong_t)&fec_imx6x_info,
}, {
.name = "imx6ul-fec",
.driver_data = (kernel_ulong_t)&fec_imx6ul_info,
}, {
/* sentinel */
}
};
MODULE_DEVICE_TABLE(platform, fec_devtype);
enum imx_fec_type {
IMX25_FEC = 1, /* runs on i.mx25/50/53 */
IMX27_FEC, /* runs on i.mx27/35/51 */
IMX28_FEC,
IMX6Q_FEC,
MVF600_FEC,
IMX6SX_FEC,
IMX6UL_FEC,
};
static const struct of_device_id fec_dt_ids[] = {
{ .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], },
{ .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], },
{ .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], },
{ .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], },
{ .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], },
{ .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], },
{ .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, fec_dt_ids);
static unsigned char macaddr[ETH_ALEN];
module_param_array(macaddr, byte, NULL, 0);
MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
#if defined(CONFIG_M5272)
/*
* Some hardware gets it MAC address out of local flash memory.
* if this is non-zero then assume it is the address to get MAC from.
*/
#if defined(CONFIG_NETtel)
#define FEC_FLASHMAC 0xf0006006
#elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
#define FEC_FLASHMAC 0xf0006000
#elif defined(CONFIG_CANCam)
#define FEC_FLASHMAC 0xf0020000
#elif defined (CONFIG_M5272C3)
#define FEC_FLASHMAC (0xffe04000 + 4)
#elif defined(CONFIG_MOD5272)
#define FEC_FLASHMAC 0xffc0406b
#else
#define FEC_FLASHMAC 0
#endif
#endif /* CONFIG_M5272 */
/* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
*
* 2048 byte skbufs are allocated. However, alignment requirements
* varies between FEC variants. Worst case is 64, so round down by 64.
*/
#define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64))
#define PKT_MINBUF_SIZE 64
/* FEC receive acceleration */
#define FEC_RACC_IPDIS (1 << 1)
#define FEC_RACC_PRODIS (1 << 2)
#define FEC_RACC_SHIFT16 BIT(7)
#define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
/* MIB Control Register */
#define FEC_MIB_CTRLSTAT_DISABLE BIT(31)
/*
* The 5270/5271/5280/5282/532x RX control register also contains maximum frame
* size bits. Other FEC hardware does not, so we need to take that into
* account when setting it.
*/
#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \
defined(CONFIG_ARM64)
#define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
#else
#define OPT_FRAME_SIZE 0
#endif
/* FEC MII MMFR bits definition */
#define FEC_MMFR_ST (1 << 30)
#define FEC_MMFR_ST_C45 (0)
#define FEC_MMFR_OP_READ (2 << 28)
#define FEC_MMFR_OP_READ_C45 (3 << 28)
#define FEC_MMFR_OP_WRITE (1 << 28)
#define FEC_MMFR_OP_ADDR_WRITE (0)
#define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
#define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
#define FEC_MMFR_TA (2 << 16)
#define FEC_MMFR_DATA(v) (v & 0xffff)
/* FEC ECR bits definition */
#define FEC_ECR_MAGICEN (1 << 2)
#define FEC_ECR_SLEEP (1 << 3)
#define FEC_MII_TIMEOUT 30000 /* us */
/* Transmitter timeout */
#define TX_TIMEOUT (2 * HZ)
#define FEC_PAUSE_FLAG_AUTONEG 0x1
#define FEC_PAUSE_FLAG_ENABLE 0x2
#define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
#define FEC_WOL_FLAG_ENABLE (0x1 << 1)
#define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
#define COPYBREAK_DEFAULT 256
/* Max number of allowed TCP segments for software TSO */
#define FEC_MAX_TSO_SEGS 100
#define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
#define IS_TSO_HEADER(txq, addr) \
((addr >= txq->tso_hdrs_dma) && \
(addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
static int mii_cnt;
static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
struct bufdesc_prop *bd)
{
return (bdp >= bd->last) ? bd->base
: (struct bufdesc *)(((void *)bdp) + bd->dsize);
}
static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
struct bufdesc_prop *bd)
{
return (bdp <= bd->base) ? bd->last
: (struct bufdesc *)(((void *)bdp) - bd->dsize);
}
static int fec_enet_get_bd_index(struct bufdesc *bdp,
struct bufdesc_prop *bd)
{
return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
}
static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
{
int entries;
entries = (((const char *)txq->dirty_tx -
(const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
return entries >= 0 ? entries : entries + txq->bd.ring_size;
}
static void swap_buffer(void *bufaddr, int len)
{
int i;
unsigned int *buf = bufaddr;
for (i = 0; i < len; i += 4, buf++)
swab32s(buf);
}
static void swap_buffer2(void *dst_buf, void *src_buf, int len)
{
int i;
unsigned int *src = src_buf;
unsigned int *dst = dst_buf;
for (i = 0; i < len; i += 4, src++, dst++)
*dst = swab32p(src);
}
static void fec_dump(struct net_device *ndev)
{
struct fec_enet_private *fep = netdev_priv(ndev);
struct bufdesc *bdp;
struct fec_enet_priv_tx_q *txq;
int index = 0;
#ifdef CONFIG_KSZ_SWITCH
fep = fep->hw_priv;
#endif
netdev_info(ndev, "TX ring dump\n");
pr_info("Nr SC addr len SKB\n");
txq = fep->tx_queue[0];
bdp = txq->bd.base;
do {
pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
index,
bdp == txq->bd.cur ? 'S' : ' ',
bdp == txq->dirty_tx ? 'H' : ' ',
fec16_to_cpu(bdp->cbd_sc),
fec32_to_cpu(bdp->cbd_bufaddr),
fec16_to_cpu(bdp->cbd_datlen),
txq->tx_skbuff[index]);
bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
index++;
} while (bdp != txq->bd.base);
}
static inline bool is_ipv4_pkt(struct sk_buff *skb)
{
return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
}
static int
fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
{
/* Only run for packets requiring a checksum. */
if (skb->ip_summed != CHECKSUM_PARTIAL)
return 0;
if (unlikely(skb_cow_head(skb, 0)))
return -1;
if (is_ipv4_pkt(skb))
ip_hdr(skb)->check = 0;
*(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
return 0;
}
static struct bufdesc *
fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
struct sk_buff *skb,
struct net_device *ndev)
{
struct fec_enet_private *fep = netdev_priv(ndev);
struct bufdesc *bdp = txq->bd.cur;
struct bufdesc_ex *ebdp;
int nr_frags = skb_shinfo(skb)->nr_frags;
int frag, frag_len;
unsigned short status;
unsigned int estatus = 0;
skb_frag_t *this_frag;
unsigned int index;
void *bufaddr;
dma_addr_t addr;
int i;
#ifdef CONFIG_KSZ_SWITCH
fep = fep->hw_priv;
#endif
for (frag = 0; frag < nr_frags; frag++) {
this_frag = &skb_shinfo(skb)->frags[frag];
bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
ebdp = (struct bufdesc_ex *)bdp;
status = fec16_to_cpu(bdp->cbd_sc);
status &= ~BD_ENET_TX_STATS;
status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
/* Handle the last BD specially */
if (frag == nr_frags - 1) {
status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
#ifndef CONFIG_1588_PTP
if (fep->bufdesc_ex) {
estatus |= BD_ENET_TX_INT;
if (unlikely(skb_shinfo(skb)->tx_flags &
SKBTX_HW_TSTAMP && fep->hwts_tx_en))
estatus |= BD_ENET_TX_TS;
}
#endif
}
if (fep->bufdesc_ex) {
if (fep->quirks & FEC_QUIRK_HAS_AVB)
estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
if (skb->ip_summed == CHECKSUM_PARTIAL)
estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
ebdp->cbd_bdu = 0;
ebdp->cbd_esc = cpu_to_fec32(estatus);
}
bufaddr = skb_frag_address(this_frag);
index = fec_enet_get_bd_index(bdp, &txq->bd);
if (((unsigned long) bufaddr) & fep->tx_align ||
fep->quirks & FEC_QUIRK_SWAP_FRAME) {
memcpy(txq->tx_bounce[index], bufaddr, frag_len);
bufaddr = txq->tx_bounce[index];
if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
swap_buffer(bufaddr, frag_len);
}
addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
DMA_TO_DEVICE);
if (dma_mapping_error(&fep->pdev->dev, addr)) {
if (net_ratelimit())
netdev_err(ndev, "Tx DMA memory map failed\n");
goto dma_mapping_error;
}
bdp->cbd_bufaddr = cpu_to_fec32(addr);
bdp->cbd_datlen = cpu_to_fec16(frag_len);
/* Make sure the updates to rest of the descriptor are
* performed before transferring ownership.
*/
wmb();
bdp->cbd_sc = cpu_to_fec16(status);
}
return bdp;
dma_mapping_error:
bdp = txq->bd.cur;
for (i = 0; i < frag; i++) {
bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
}
return ERR_PTR(-ENOMEM);
}
static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
struct sk_buff *skb, struct net_device *ndev)
{
struct fec_enet_private *fep = netdev_priv(ndev);
int nr_frags = skb_shinfo(skb)->nr_frags;
struct bufdesc *bdp, *last_bdp;
void *bufaddr;
dma_addr_t addr;
unsigned short status;
unsigned short buflen;
unsigned int estatus = 0;
unsigned int index;
int entries_free;
#ifdef CONFIG_KSZ_SWITCH
struct ksz_sw *sw = fep->port.sw;
#endif
entries_free = fec_enet_get_free_txdesc_num(txq);
if (entries_free < MAX_SKB_FRAGS + 1) {
dev_kfree_skb_any(skb);
if (net_ratelimit())
netdev_err(ndev, "NOT enough BD for SG!\n");
return NETDEV_TX_OK;
}
#ifdef CONFIG_KSZ_SWITCH
if (sw_is_switch(sw)) {
struct ksz_port *port = &fep->port;
int len = skb->len;
int header = 0;
len = sw->net_ops->get_tx_len(sw, skb, port->first_port,
&header);
/* Hardware cannot generate checksum correctly for HSR frame. */
if (skb->ip_summed && header > VLAN_HLEN) {
struct sk_buff *nskb;
nskb = dev_alloc_skb(len);
if (nskb) {
skb_copy_and_csum_dev(skb, nskb->data);
skb->ip_summed = 0;
nskb->len = skb->len;
copy_old_skb(skb, nskb);
skb = nskb;
skb_set_tail_pointer(skb, skb->len);
}
}
}
#endif
/* Protocol checksum off-load for TCP and UDP. */
if (fec_enet_clear_csum(skb, ndev)) {
dev_kfree_skb_any(skb);
return NETDEV_TX_OK;
}
#ifdef CONFIG_KSZ_SWITCH
if (sw_is_switch(sw)) {
skb = sw->net_ops->final_skb(sw, skb, ndev, &fep->port);
if (!skb)
return NETDEV_TX_OK;
nr_frags = skb_shinfo(skb)->nr_frags;
fep = fep->hw_priv;
}
#endif
/* Fill in a Tx ring entry */
bdp = txq->bd.cur;
last_bdp = bdp;
status = fec16_to_cpu(bdp->cbd_sc);
status &= ~BD_ENET_TX_STATS;
/* Set buffer length and buffer pointer */
bufaddr = skb->data;
buflen = skb_headlen(skb);
index = fec_enet_get_bd_index(bdp, &txq->bd);
if (((unsigned long) bufaddr) & fep->tx_align ||
fep->quirks & FEC_QUIRK_SWAP_FRAME) {
memcpy(txq->tx_bounce[index], skb->data, buflen);
bufaddr = txq->tx_bounce[index];
if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
swap_buffer(bufaddr, buflen);
}
/* Push the data cache so the CPM does not get stale memory data. */
addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
if (dma_mapping_error(&fep->pdev->dev, addr)) {
dev_kfree_skb_any(skb);
if (net_ratelimit())
netdev_err(ndev, "Tx DMA memory map failed\n");
return NETDEV_TX_OK;
}
if (nr_frags) {
last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
if (IS_ERR(last_bdp)) {
dma_unmap_single(&fep->pdev->dev, addr,
buflen, DMA_TO_DEVICE);
dev_kfree_skb_any(skb);
return NETDEV_TX_OK;
}
} else {
status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
#ifndef CONFIG_1588_PTP
if (fep->bufdesc_ex) {
estatus = BD_ENET_TX_INT;
if (unlikely(skb_shinfo(skb)->tx_flags &
SKBTX_HW_TSTAMP && fep->hwts_tx_en))
estatus |= BD_ENET_TX_TS;
}
#endif
}
bdp->cbd_bufaddr = cpu_to_fec32(addr);
bdp->cbd_datlen = cpu_to_fec16(buflen);
if (fep->bufdesc_ex) {
struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
#ifndef CONFIG_1588_PTP
if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
fep->hwts_tx_en))
skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
#endif
if (fep->quirks & FEC_QUIRK_HAS_AVB)
estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);