summaryrefslogtreecommitdiff
path: root/drivers/staging/fsl_ppfe/pfe_eth.c
blob: 85acc103064f383fdb85034b78b1ba70b90a76c2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
/*
 * Copyright 2015-2016 Freescale Semiconductor, Inc.
 * Copyright 2017 NXP
 *
 * 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/>.
 */

/* @pfe_eth.c.
 *  Ethernet driver for to handle exception path for PFE.
 *  - uses HIF functions to send/receive packets.
 *  - uses ctrl function to start/stop interfaces.
 *  - uses direct register accesses to control phy operation.
 */
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/phy.h>
#include <linux/timer.h>
#include <linux/hrtimer.h>
#include <linux/platform_device.h>

#include <net/ip.h>
#include <net/sock.h>

#include <linux/io.h>
#include <asm/irq.h>
#include <linux/delay.h>
#include <linux/regmap.h>
#include <linux/i2c.h>

#if defined(CONFIG_NF_CONNTRACK_MARK)
#include <net/netfilter/nf_conntrack.h>
#endif

#include "pfe_mod.h"
#include "pfe_eth.h"

static void *cbus_emac_base[3];
static void *cbus_gpi_base[3];

/* Forward Declaration */
static void pfe_eth_exit_one(struct pfe_eth_priv_s *priv);
static void pfe_eth_flush_tx(struct pfe_eth_priv_s *priv);
static void pfe_eth_flush_txQ(struct pfe_eth_priv_s *priv, int tx_q_num, int
				from_tx, int n_desc);

unsigned int gemac_regs[] = {
	0x0004, /* Interrupt event */
	0x0008, /* Interrupt mask */
	0x0024, /* Ethernet control */
	0x0064, /* MIB Control/Status */
	0x0084, /* Receive control/status */
	0x00C4, /* Transmit control */
	0x00E4, /* Physical address low */
	0x00E8, /* Physical address high */
	0x0144, /* Transmit FIFO Watermark and Store and Forward Control*/
	0x0190, /* Receive FIFO Section Full Threshold */
	0x01A0, /* Transmit FIFO Section Empty Threshold */
	0x01B0, /* Frame Truncation Length */
};

/********************************************************************/
/*                   SYSFS INTERFACE				    */
/********************************************************************/

#ifdef PFE_ETH_NAPI_STATS
/*
 * pfe_eth_show_napi_stats
 */
static ssize_t pfe_eth_show_napi_stats(struct device *dev,
				       struct device_attribute *attr,
				       char *buf)
{
	struct pfe_eth_priv_s *priv = netdev_priv(to_net_dev(dev));
	ssize_t len = 0;

	len += sprintf(buf + len, "sched:  %u\n",
			priv->napi_counters[NAPI_SCHED_COUNT]);
	len += sprintf(buf + len, "poll:   %u\n",
			priv->napi_counters[NAPI_POLL_COUNT]);
	len += sprintf(buf + len, "packet: %u\n",
			priv->napi_counters[NAPI_PACKET_COUNT]);
	len += sprintf(buf + len, "budget: %u\n",
			priv->napi_counters[NAPI_FULL_BUDGET_COUNT]);
	len += sprintf(buf + len, "desc:   %u\n",
			priv->napi_counters[NAPI_DESC_COUNT]);

	return len;
}

/*
 * pfe_eth_set_napi_stats
 */
static ssize_t pfe_eth_set_napi_stats(struct device *dev,
				      struct device_attribute *attr,
				      const char *buf, size_t count)
{
	struct pfe_eth_priv_s *priv = netdev_priv(to_net_dev(dev));

	memset(priv->napi_counters, 0, sizeof(priv->napi_counters));

	return count;
}
#endif
#ifdef PFE_ETH_TX_STATS
/* pfe_eth_show_tx_stats
 *
 */
static ssize_t pfe_eth_show_tx_stats(struct device *dev,
				     struct device_attribute *attr,
				     char *buf)
{
	struct pfe_eth_priv_s *priv = netdev_priv(to_net_dev(dev));
	ssize_t len = 0;
	int i;

	len += sprintf(buf + len, "TX queues stats:\n");

	for (i = 0; i < emac_txq_cnt; i++) {
		struct netdev_queue *tx_queue = netdev_get_tx_queue(priv->ndev,
									i);

		len += sprintf(buf + len, "\n");
		__netif_tx_lock_bh(tx_queue);

		hif_tx_lock(&pfe->hif);
		len += sprintf(buf + len,
				"Queue %2d :  credits               = %10d\n"
				, i, hif_lib_tx_credit_avail(pfe, priv->id, i));
		len += sprintf(buf + len,
				 "            tx packets            = %10d\n"
				,  pfe->tmu_credit.tx_packets[priv->id][i]);
		hif_tx_unlock(&pfe->hif);

		/* Don't output additionnal stats if queue never used */
		if (!pfe->tmu_credit.tx_packets[priv->id][i])
			goto skip;

		len += sprintf(buf + len,
				 "            clean_fail            = %10d\n"
				, priv->clean_fail[i]);
		len += sprintf(buf + len,
				 "            stop_queue            = %10d\n"
				, priv->stop_queue_total[i]);
		len += sprintf(buf + len,
				 "            stop_queue_hif        = %10d\n"
				, priv->stop_queue_hif[i]);
		len += sprintf(buf + len,
				"            stop_queue_hif_client = %10d\n"
				, priv->stop_queue_hif_client[i]);
		len += sprintf(buf + len,
				 "            stop_queue_credit     = %10d\n"
				, priv->stop_queue_credit[i]);
skip:
		__netif_tx_unlock_bh(tx_queue);
	}
	return len;
}

/* pfe_eth_set_tx_stats
 *
 */
static ssize_t pfe_eth_set_tx_stats(struct device *dev,
				    struct device_attribute *attr,
				    const char *buf, size_t count)
{
	struct pfe_eth_priv_s *priv = netdev_priv(to_net_dev(dev));
	int i;

	for (i = 0; i < emac_txq_cnt; i++) {
		struct netdev_queue *tx_queue = netdev_get_tx_queue(priv->ndev,
									i);

		__netif_tx_lock_bh(tx_queue);
		priv->clean_fail[i] = 0;
		priv->stop_queue_total[i] = 0;
		priv->stop_queue_hif[i] = 0;
		priv->stop_queue_hif_client[i] = 0;
		priv->stop_queue_credit[i] = 0;
		__netif_tx_unlock_bh(tx_queue);
	}

	return count;
}
#endif
/* pfe_eth_show_txavail
 *
 */
static ssize_t pfe_eth_show_txavail(struct device *dev,
				    struct device_attribute *attr,
				    char *buf)
{
	struct pfe_eth_priv_s *priv = netdev_priv(to_net_dev(dev));
	ssize_t len = 0;
	int i;

	for (i = 0; i < emac_txq_cnt; i++) {
		struct netdev_queue *tx_queue = netdev_get_tx_queue(priv->ndev,
									i);

		__netif_tx_lock_bh(tx_queue);

		len += sprintf(buf + len, "%d",
				hif_lib_tx_avail(&priv->client, i));

		__netif_tx_unlock_bh(tx_queue);

		if (i == (emac_txq_cnt - 1))
			len += sprintf(buf + len, "\n");
		else
			len += sprintf(buf + len, " ");
	}

	return len;
}

/* pfe_eth_show_default_priority
 *
 */
static ssize_t pfe_eth_show_default_priority(struct device *dev,
					     struct device_attribute *attr,
						char *buf)
{
	struct pfe_eth_priv_s *priv = netdev_priv(to_net_dev(dev));
	unsigned long flags;
	int rc;

	spin_lock_irqsave(&priv->lock, flags);
	rc = sprintf(buf, "%d\n", priv->default_priority);
	spin_unlock_irqrestore(&priv->lock, flags);

	return rc;
}

/* pfe_eth_set_default_priority
 *
 */

static ssize_t pfe_eth_set_default_priority(struct device *dev,
					    struct device_attribute *attr,
					    const char *buf, size_t count)
{
	struct pfe_eth_priv_s *priv = netdev_priv(to_net_dev(dev));
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
	priv->default_priority = kstrtoul(buf, 0, 0);
	spin_unlock_irqrestore(&priv->lock, flags);

	return count;
}

static DEVICE_ATTR(txavail, 0444, pfe_eth_show_txavail, NULL);
static DEVICE_ATTR(default_priority, 0644, pfe_eth_show_default_priority,
			pfe_eth_set_default_priority);

#ifdef PFE_ETH_NAPI_STATS
static DEVICE_ATTR(napi_stats, 0644, pfe_eth_show_napi_stats,
			pfe_eth_set_napi_stats);
#endif

#ifdef PFE_ETH_TX_STATS
static DEVICE_ATTR(tx_stats, 0644, pfe_eth_show_tx_stats,
			pfe_eth_set_tx_stats);
#endif

/*
 * pfe_eth_sysfs_init
 *
 */
static int pfe_eth_sysfs_init(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	int err;

	/* Initialize the default values */

	/*
	 * By default, packets without conntrack will use this default low
	 * priority queue
	 */
	priv->default_priority = 0;

	/* Create our sysfs files */
	err = device_create_file(&ndev->dev, &dev_attr_default_priority);
	if (err) {
		netdev_err(ndev,
			   "failed to create default_priority sysfs files\n");
		goto err_priority;
	}

	err = device_create_file(&ndev->dev, &dev_attr_txavail);
	if (err) {
		netdev_err(ndev,
			   "failed to create default_priority sysfs files\n");
		goto err_txavail;
	}

#ifdef PFE_ETH_NAPI_STATS
	err = device_create_file(&ndev->dev, &dev_attr_napi_stats);
	if (err) {
		netdev_err(ndev, "failed to create napi stats sysfs files\n");
		goto err_napi;
	}
#endif

#ifdef PFE_ETH_TX_STATS
	err = device_create_file(&ndev->dev, &dev_attr_tx_stats);
	if (err) {
		netdev_err(ndev, "failed to create tx stats sysfs files\n");
		goto err_tx;
	}
#endif

	return 0;

#ifdef PFE_ETH_TX_STATS
err_tx:
#endif
#ifdef PFE_ETH_NAPI_STATS
	device_remove_file(&ndev->dev, &dev_attr_napi_stats);

err_napi:
#endif
	device_remove_file(&ndev->dev, &dev_attr_txavail);

err_txavail:
	device_remove_file(&ndev->dev, &dev_attr_default_priority);

err_priority:
	return -1;
}

/* pfe_eth_sysfs_exit
 *
 */
void pfe_eth_sysfs_exit(struct net_device *ndev)
{
#ifdef PFE_ETH_TX_STATS
	device_remove_file(&ndev->dev, &dev_attr_tx_stats);
#endif

#ifdef PFE_ETH_NAPI_STATS
	device_remove_file(&ndev->dev, &dev_attr_napi_stats);
#endif
	device_remove_file(&ndev->dev, &dev_attr_txavail);
	device_remove_file(&ndev->dev, &dev_attr_default_priority);
}

/*************************************************************************/
/*		ETHTOOL INTERCAE					 */
/*************************************************************************/

/*MTIP GEMAC */
static const struct fec_stat {
	char name[ETH_GSTRING_LEN];
	u16 offset;
} fec_stats[] = {
	/* RMON TX */
	{ "tx_dropped", RMON_T_DROP },
	{ "tx_packets", RMON_T_PACKETS },
	{ "tx_broadcast", RMON_T_BC_PKT },
	{ "tx_multicast", RMON_T_MC_PKT },
	{ "tx_crc_errors", RMON_T_CRC_ALIGN },
	{ "tx_undersize", RMON_T_UNDERSIZE },
	{ "tx_oversize", RMON_T_OVERSIZE },
	{ "tx_fragment", RMON_T_FRAG },
	{ "tx_jabber", RMON_T_JAB },
	{ "tx_collision", RMON_T_COL },
	{ "tx_64byte", RMON_T_P64 },
	{ "tx_65to127byte", RMON_T_P65TO127 },
	{ "tx_128to255byte", RMON_T_P128TO255 },
	{ "tx_256to511byte", RMON_T_P256TO511 },
	{ "tx_512to1023byte", RMON_T_P512TO1023 },
	{ "tx_1024to2047byte", RMON_T_P1024TO2047 },
	{ "tx_GTE2048byte", RMON_T_P_GTE2048 },
	{ "tx_octets", RMON_T_OCTETS },

	/* IEEE TX */
	{ "IEEE_tx_drop", IEEE_T_DROP },
	{ "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
	{ "IEEE_tx_1col", IEEE_T_1COL },
	{ "IEEE_tx_mcol", IEEE_T_MCOL },
	{ "IEEE_tx_def", IEEE_T_DEF },
	{ "IEEE_tx_lcol", IEEE_T_LCOL },
	{ "IEEE_tx_excol", IEEE_T_EXCOL },
	{ "IEEE_tx_macerr", IEEE_T_MACERR },
	{ "IEEE_tx_cserr", IEEE_T_CSERR },
	{ "IEEE_tx_sqe", IEEE_T_SQE },
	{ "IEEE_tx_fdxfc", IEEE_T_FDXFC },
	{ "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },

	/* RMON RX */
	{ "rx_packets", RMON_R_PACKETS },
	{ "rx_broadcast", RMON_R_BC_PKT },
	{ "rx_multicast", RMON_R_MC_PKT },
	{ "rx_crc_errors", RMON_R_CRC_ALIGN },
	{ "rx_undersize", RMON_R_UNDERSIZE },
	{ "rx_oversize", RMON_R_OVERSIZE },
	{ "rx_fragment", RMON_R_FRAG },
	{ "rx_jabber", RMON_R_JAB },
	{ "rx_64byte", RMON_R_P64 },
	{ "rx_65to127byte", RMON_R_P65TO127 },
	{ "rx_128to255byte", RMON_R_P128TO255 },
	{ "rx_256to511byte", RMON_R_P256TO511 },
	{ "rx_512to1023byte", RMON_R_P512TO1023 },
	{ "rx_1024to2047byte", RMON_R_P1024TO2047 },
	{ "rx_GTE2048byte", RMON_R_P_GTE2048 },
	{ "rx_octets", RMON_R_OCTETS },

	/* IEEE RX */
	{ "IEEE_rx_drop", IEEE_R_DROP },
	{ "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
	{ "IEEE_rx_crc", IEEE_R_CRC },
	{ "IEEE_rx_align", IEEE_R_ALIGN },
	{ "IEEE_rx_macerr", IEEE_R_MACERR },
	{ "IEEE_rx_fdxfc", IEEE_R_FDXFC },
	{ "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
};

static void pfe_eth_fill_stats(struct net_device *ndev, struct ethtool_stats
				*stats, u64 *data)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	int i;

	for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
		data[i] = readl(priv->EMAC_baseaddr + fec_stats[i].offset);
}

static void pfe_eth_gstrings(struct net_device *netdev,
			     u32 stringset, u8 *data)
{
	int i;

	switch (stringset) {
	case ETH_SS_STATS:
		for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
			memcpy(data + i * ETH_GSTRING_LEN,
			       fec_stats[i].name, ETH_GSTRING_LEN);
		break;
	}
}

static int pfe_eth_stats_count(struct net_device *ndev, int sset)
{
	switch (sset) {
	case ETH_SS_STATS:
		return ARRAY_SIZE(fec_stats);
	default:
		return -EOPNOTSUPP;
	}
}

/*
 * pfe_eth_gemac_reglen - Return the length of the register structure.
 *
 */
static int pfe_eth_gemac_reglen(struct net_device *ndev)
{
	pr_info("%s()\n", __func__);
	return (sizeof(gemac_regs) / sizeof(u32));
}

/*
 * pfe_eth_gemac_get_regs - Return the gemac register structure.
 *
 */
static void  pfe_eth_gemac_get_regs(struct net_device *ndev, struct ethtool_regs
					*regs, void *regbuf)
{
	int i;

	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	u32 *buf = (u32 *)regbuf;

	pr_info("%s()\n", __func__);
	for (i = 0; i < sizeof(gemac_regs) / sizeof(u32); i++)
		buf[i] = readl(priv->EMAC_baseaddr + gemac_regs[i]);
}

/*
 * pfe_eth_set_wol - Set the magic packet option, in WoL register.
 *
 */
static int pfe_eth_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	if (wol->wolopts & ~WAKE_MAGIC)
		return -EOPNOTSUPP;

	/* for MTIP we store wol->wolopts */
	priv->wol = wol->wolopts;

	device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);

	return 0;
}

/*
 *
 * pfe_eth_get_wol - Get the WoL options.
 *
 */
static void pfe_eth_get_wol(struct net_device *ndev, struct ethtool_wolinfo
				*wol)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	wol->supported = WAKE_MAGIC;
	wol->wolopts = 0;

	if (priv->wol & WAKE_MAGIC)
		wol->wolopts = WAKE_MAGIC;

	memset(&wol->sopass, 0, sizeof(wol->sopass));
}

/*
 * pfe_eth_get_drvinfo -  Fills in the drvinfo structure with some basic info
 *
 */
static void pfe_eth_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo
				*drvinfo)
{
	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
	strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
	strlcpy(drvinfo->bus_info, "N/A", sizeof(drvinfo->bus_info));
}

/*
 * pfe_eth_set_settings - Used to send commands to PHY.
 *
 */
static int pfe_eth_set_settings(struct net_device *ndev,
				const struct ethtool_link_ksettings *cmd)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	struct phy_device *phydev = priv->phydev;

	if (!phydev)
		return -ENODEV;

	return phy_ethtool_ksettings_set(phydev, cmd);
}

/*
 * pfe_eth_getsettings - Return the current settings in the ethtool_cmd
 * structure.
 *
 */
static int pfe_eth_get_settings(struct net_device *ndev,
				struct ethtool_link_ksettings *cmd)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	struct phy_device *phydev = priv->phydev;

	if (!phydev)
		return -ENODEV;

	phy_ethtool_ksettings_get(phydev, cmd);

	return 0;
}

/*
 * pfe_eth_get_msglevel - Gets the debug message mask.
 *
 */
static uint32_t pfe_eth_get_msglevel(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	return priv->msg_enable;
}

/*
 * pfe_eth_set_msglevel - Sets the debug message mask.
 *
 */
static void pfe_eth_set_msglevel(struct net_device *ndev, uint32_t data)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	priv->msg_enable = data;
}

#define HIF_RX_COAL_MAX_CLKS		(~(1 << 31))
#define HIF_RX_COAL_CLKS_PER_USEC	(pfe->ctrl.sys_clk / 1000)
#define HIF_RX_COAL_MAX_USECS		(HIF_RX_COAL_MAX_CLKS	/ \
						HIF_RX_COAL_CLKS_PER_USEC)

/*
 * pfe_eth_set_coalesce - Sets rx interrupt coalescing timer.
 *
 */
static int pfe_eth_set_coalesce(struct net_device *ndev,
				struct ethtool_coalesce *ec)
{
	if (ec->rx_coalesce_usecs > HIF_RX_COAL_MAX_USECS)
		return -EINVAL;

	if (!ec->rx_coalesce_usecs) {
		writel(0, HIF_INT_COAL);
		return 0;
	}

	writel((ec->rx_coalesce_usecs * HIF_RX_COAL_CLKS_PER_USEC) |
			HIF_INT_COAL_ENABLE, HIF_INT_COAL);

	return 0;
}

/*
 * pfe_eth_get_coalesce - Gets rx interrupt coalescing timer value.
 *
 */
static int pfe_eth_get_coalesce(struct net_device *ndev,
				struct ethtool_coalesce *ec)
{
	int reg_val = readl(HIF_INT_COAL);

	if (reg_val & HIF_INT_COAL_ENABLE)
		ec->rx_coalesce_usecs = (reg_val & HIF_RX_COAL_MAX_CLKS) /
						HIF_RX_COAL_CLKS_PER_USEC;
	else
		ec->rx_coalesce_usecs = 0;

	return 0;
}

/*
 * pfe_eth_set_pauseparam - Sets pause parameters
 *
 */
static int pfe_eth_set_pauseparam(struct net_device *ndev,
				  struct ethtool_pauseparam *epause)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	if (epause->tx_pause != epause->rx_pause) {
		netdev_info(ndev,
			    "hardware only support enable/disable both tx and rx\n");
		return -EINVAL;
	}

	priv->pause_flag = 0;
	priv->pause_flag |= epause->rx_pause ? PFE_PAUSE_FLAG_ENABLE : 0;
	priv->pause_flag |= epause->autoneg ? PFE_PAUSE_FLAG_AUTONEG : 0;

	if (epause->rx_pause || epause->autoneg) {
		gemac_enable_pause_rx(priv->EMAC_baseaddr);
		writel((readl(priv->GPI_baseaddr + GPI_TX_PAUSE_TIME) |
					EGPI_PAUSE_ENABLE),
				priv->GPI_baseaddr + GPI_TX_PAUSE_TIME);
		if (priv->phydev) {
			priv->phydev->supported |= ADVERTISED_Pause |
							ADVERTISED_Asym_Pause;
			priv->phydev->advertising |= ADVERTISED_Pause |
							ADVERTISED_Asym_Pause;
		}
	} else {
		gemac_disable_pause_rx(priv->EMAC_baseaddr);
		writel((readl(priv->GPI_baseaddr + GPI_TX_PAUSE_TIME) &
					~EGPI_PAUSE_ENABLE),
				priv->GPI_baseaddr + GPI_TX_PAUSE_TIME);
		if (priv->phydev) {
			priv->phydev->supported &= ~(ADVERTISED_Pause |
							ADVERTISED_Asym_Pause);
			priv->phydev->advertising &= ~(ADVERTISED_Pause |
							ADVERTISED_Asym_Pause);
		}
	}

	return 0;
}

/*
 * pfe_eth_get_pauseparam - Gets pause parameters
 *
 */
static void pfe_eth_get_pauseparam(struct net_device *ndev,
				   struct ethtool_pauseparam *epause)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	epause->autoneg = (priv->pause_flag & PFE_PAUSE_FLAG_AUTONEG) != 0;
	epause->tx_pause = (priv->pause_flag & PFE_PAUSE_FLAG_ENABLE) != 0;
	epause->rx_pause = epause->tx_pause;
}

/*
 * pfe_eth_get_hash
 */
#define PFE_HASH_BITS	6		/* #bits in hash */
#define CRC32_POLY	0xEDB88320

static int pfe_eth_get_hash(u8 *addr)
{
	unsigned int i, bit, data, crc, hash;

	/* calculate crc32 value of mac address */
	crc = 0xffffffff;

	for (i = 0; i < 6; i++) {
		data = addr[i];
		for (bit = 0; bit < 8; bit++, data >>= 1) {
			crc = (crc >> 1) ^
				(((crc ^ data) & 1) ? CRC32_POLY : 0);
		}
	}

	/*
	 * only upper 6 bits (PFE_HASH_BITS) are used
	 * which point to specific bit in the hash registers
	 */
	hash = (crc >> (32 - PFE_HASH_BITS)) & 0x3f;

	return hash;
}

const struct ethtool_ops pfe_ethtool_ops = {
	.get_drvinfo = pfe_eth_get_drvinfo,
	.get_regs_len = pfe_eth_gemac_reglen,
	.get_regs = pfe_eth_gemac_get_regs,
	.get_link = ethtool_op_get_link,
	.get_wol  = pfe_eth_get_wol,
	.set_wol  = pfe_eth_set_wol,
	.set_pauseparam = pfe_eth_set_pauseparam,
	.get_pauseparam = pfe_eth_get_pauseparam,
	.get_strings = pfe_eth_gstrings,
	.get_sset_count = pfe_eth_stats_count,
	.get_ethtool_stats = pfe_eth_fill_stats,
	.get_msglevel = pfe_eth_get_msglevel,
	.set_msglevel = pfe_eth_set_msglevel,
	.set_coalesce = pfe_eth_set_coalesce,
	.get_coalesce = pfe_eth_get_coalesce,
	.get_link_ksettings = pfe_eth_get_settings,
	.set_link_ksettings = pfe_eth_set_settings,
};

/* pfe_eth_mdio_reset
 */
int pfe_eth_mdio_reset(struct mii_bus *bus)
{
	struct pfe_eth_priv_s *priv = (struct pfe_eth_priv_s *)bus->priv;
	u32 phy_speed;

	netif_info(priv, hw, priv->ndev, "%s\n", __func__);

	mutex_lock(&bus->mdio_lock);

	/*
	 * Set MII speed to 2.5 MHz (= clk_get_rate() / 2 * phy_speed)
	 *
	 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
	 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'.
	 */
	phy_speed = (DIV_ROUND_UP((pfe->ctrl.sys_clk * 1000), 4000000)
		     << EMAC_MII_SPEED_SHIFT);
	phy_speed |= EMAC_HOLDTIME(0x5);
	__raw_writel(phy_speed, priv->PHY_baseaddr + EMAC_MII_CTRL_REG);

	mutex_unlock(&bus->mdio_lock);

	return 0;
}

/* pfe_eth_gemac_phy_timeout
 *
 */
static int pfe_eth_gemac_phy_timeout(struct pfe_eth_priv_s *priv, int timeout)
{
	while (!(__raw_readl(priv->PHY_baseaddr + EMAC_IEVENT_REG) &
			EMAC_IEVENT_MII)) {
		if (timeout-- <= 0)
			return -1;
		usleep_range(10, 20);
	}
	__raw_writel(EMAC_IEVENT_MII, priv->PHY_baseaddr + EMAC_IEVENT_REG);
	return 0;
}

static int pfe_eth_mdio_mux(u8 muxval)
{
	struct i2c_adapter *a;
	struct i2c_msg msg;
	unsigned char buf[2];
	int ret;

	a = i2c_get_adapter(0);
	if (!a)
		return -ENODEV;

	/* set bit 1 (the second bit) of chip at 0x09, register 0x13 */
	buf[0] = 0x54; /* reg number */
	buf[1] = (muxval << 6) | 0x3; /* data */
	msg.addr = 0x66;
	msg.buf = buf;
	msg.len = 2;
	msg.flags = 0;
	ret = i2c_transfer(a, &msg, 1);
	i2c_put_adapter(a);
	if (ret != 1)
		return -ENODEV;
	return 0;
}

static int pfe_eth_mdio_write_addr(struct mii_bus *bus, int mii_id,
				   int dev_addr, int regnum)
{
	struct pfe_eth_priv_s *priv = (struct pfe_eth_priv_s *)bus->priv;

	__raw_writel(EMAC_MII_DATA_PA(mii_id) |
		     EMAC_MII_DATA_RA(dev_addr) |
		     EMAC_MII_DATA_TA | EMAC_MII_DATA(regnum),
		     priv->PHY_baseaddr + EMAC_MII_DATA_REG);

	if (pfe_eth_gemac_phy_timeout(priv, EMAC_MDIO_TIMEOUT)) {
		netdev_err(priv->ndev, "%s: phy MDIO address write timeout\n",
			   __func__);
		return -1;
	}

	return 0;
}

static int pfe_eth_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
			      u16 value)
{
	struct pfe_eth_priv_s *priv = (struct pfe_eth_priv_s *)bus->priv;

	/*To access external PHYs on QDS board mux needs to be configured*/
	if ((mii_id) && (pfe->mdio_muxval[mii_id]))
		pfe_eth_mdio_mux(pfe->mdio_muxval[mii_id]);

	if (regnum & MII_ADDR_C45) {
		pfe_eth_mdio_write_addr(bus, mii_id, (regnum >> 16) & 0x1f,
					regnum & 0xffff);
		__raw_writel(EMAC_MII_DATA_OP_CL45_WR |
			     EMAC_MII_DATA_PA(mii_id) |
			     EMAC_MII_DATA_RA((regnum >> 16) & 0x1f) |
			     EMAC_MII_DATA_TA | EMAC_MII_DATA(value),
			     priv->PHY_baseaddr + EMAC_MII_DATA_REG);
	} else {
		/* start a write op */
		__raw_writel(EMAC_MII_DATA_ST | EMAC_MII_DATA_OP_WR |
			     EMAC_MII_DATA_PA(mii_id) |
			     EMAC_MII_DATA_RA(regnum) |
			     EMAC_MII_DATA_TA | EMAC_MII_DATA(value),
			     priv->PHY_baseaddr + EMAC_MII_DATA_REG);
	}

	if (pfe_eth_gemac_phy_timeout(priv, EMAC_MDIO_TIMEOUT)) {
		netdev_err(priv->ndev, "%s: phy MDIO write timeout\n",
			   __func__);
		return -1;
	}
	netif_info(priv, hw, priv->ndev, "%s: phy %x reg %x val %x\n", __func__,
		   mii_id, regnum, value);

	return 0;
}

static int pfe_eth_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
{
	struct pfe_eth_priv_s *priv = (struct pfe_eth_priv_s *)bus->priv;
	u16 value = 0;

	/*To access external PHYs on QDS board mux needs to be configured*/
	if ((mii_id) && (pfe->mdio_muxval[mii_id]))
		pfe_eth_mdio_mux(pfe->mdio_muxval[mii_id]);

	if (regnum & MII_ADDR_C45) {
		pfe_eth_mdio_write_addr(bus, mii_id, (regnum >> 16) & 0x1f,
					regnum & 0xffff);
		__raw_writel(EMAC_MII_DATA_OP_CL45_RD |
			     EMAC_MII_DATA_PA(mii_id) |
			     EMAC_MII_DATA_RA((regnum >> 16) & 0x1f) |
			     EMAC_MII_DATA_TA,
			     priv->PHY_baseaddr + EMAC_MII_DATA_REG);
	} else {
		/* start a read op */
		__raw_writel(EMAC_MII_DATA_ST | EMAC_MII_DATA_OP_RD |
			     EMAC_MII_DATA_PA(mii_id) |
			     EMAC_MII_DATA_RA(regnum) |
			     EMAC_MII_DATA_TA, priv->PHY_baseaddr +
			     EMAC_MII_DATA_REG);
	}

	if (pfe_eth_gemac_phy_timeout(priv, EMAC_MDIO_TIMEOUT)) {
		netdev_err(priv->ndev, "%s: phy MDIO read timeout\n", __func__);
		return -1;
	}

	value = EMAC_MII_DATA(__raw_readl(priv->PHY_baseaddr +
						EMAC_MII_DATA_REG));
	netif_info(priv, hw, priv->ndev, "%s: phy %x reg %x val %x\n", __func__,
		   mii_id, regnum, value);
	return value;
}

static int pfe_eth_mdio_init(struct pfe_eth_priv_s *priv,
			     struct ls1012a_mdio_platform_data *minfo)
{
	struct mii_bus *bus;
	int rc, ii;
	struct phy_device *phydev;

	netif_info(priv, drv, priv->ndev, "%s\n", __func__);
	pr_info("%s\n", __func__);

	bus = mdiobus_alloc();
	if (!bus) {
		netdev_err(priv->ndev, "mdiobus_alloc() failed\n");
		rc = -ENOMEM;
		goto err0;
	}

	bus->name = "ls1012a MDIO Bus";
	bus->read = &pfe_eth_mdio_read;
	bus->write = &pfe_eth_mdio_write;
	bus->reset = &pfe_eth_mdio_reset;
	snprintf(bus->id, MII_BUS_ID_SIZE, "ls1012a-%x", priv->id);
	bus->priv = priv;

	bus->phy_mask = minfo->phy_mask;
	priv->mdc_div = minfo->mdc_div;

	if (!priv->mdc_div)
		priv->mdc_div = 64;

	bus->irq[0] = minfo->irq[0];

	bus->parent = priv->pfe->dev;

	netif_info(priv, drv, priv->ndev, "%s: mdc_div: %d, phy_mask: %x\n",
		   __func__, priv->mdc_div, bus->phy_mask);
	rc = mdiobus_register(bus);
	if (rc) {
		netdev_err(priv->ndev, "mdiobus_register(%s) failed\n",
			   bus->name);
		goto err1;
	}

	priv->mii_bus = bus;

	/* For clause 45 we need to call get_phy_device() with it's
	 * 3rd argument as true and then register the phy device
	 * via phy_device_register()
	 */

	if (priv->einfo->mii_config == PHY_INTERFACE_MODE_2500SGMII) {
		for (ii = 0; ii < NUM_GEMAC_SUPPORT; ii++) {
			phydev = get_phy_device(priv->mii_bus,
					priv->einfo->phy_id + ii, true);
			if (!phydev || IS_ERR(phydev)) {
				rc = -EIO;
				netdev_err(priv->ndev, "fail to get device\n");
				goto err1;
			}
			rc = phy_device_register(phydev);
			if (rc) {
				phy_device_free(phydev);
				netdev_err(priv->ndev,
					"phy_device_register() failed\n");
				goto err1;
			}
		}
	}

	pfe_eth_mdio_reset(bus);

	return 0;

err1:
	mdiobus_free(bus);
err0:
	return rc;
}

/* pfe_eth_mdio_exit
 */
static void pfe_eth_mdio_exit(struct mii_bus *bus)
{
	if (!bus)
		return;

	netif_info((struct pfe_eth_priv_s *)bus->priv, drv, ((struct
			pfe_eth_priv_s *)(bus->priv))->ndev, "%s\n", __func__);

	mdiobus_unregister(bus);
	mdiobus_free(bus);
}

/* pfe_get_phydev_speed
 */
static int pfe_get_phydev_speed(struct phy_device *phydev)
{
	switch (phydev->speed) {
	case 10:
			return SPEED_10M;
	case 100:
			return SPEED_100M;
	case 1000:
	default:
			return SPEED_1000M;
	}
}

/* pfe_set_rgmii_speed
 */
#define RGMIIPCR	0x434
/* RGMIIPCR bit definitions*/
#define SCFG_RGMIIPCR_EN_AUTO           (0x00000008)
#define SCFG_RGMIIPCR_SETSP_1000M       (0x00000004)
#define SCFG_RGMIIPCR_SETSP_100M        (0x00000000)
#define SCFG_RGMIIPCR_SETSP_10M         (0x00000002)
#define SCFG_RGMIIPCR_SETFD             (0x00000001)

static void pfe_set_rgmii_speed(struct phy_device *phydev)
{
	u32 rgmii_pcr;

	regmap_read(pfe->scfg, RGMIIPCR, &rgmii_pcr);
	rgmii_pcr  &= ~(SCFG_RGMIIPCR_SETSP_1000M | SCFG_RGMIIPCR_SETSP_10M);

	switch (phydev->speed) {
	case 10:
			rgmii_pcr |= SCFG_RGMIIPCR_SETSP_10M;
			break;
	case 1000:
			rgmii_pcr |= SCFG_RGMIIPCR_SETSP_1000M;
			break;
	case 100:
	default:
			/* Default is 100M */
			break;
	}
	regmap_write(pfe->scfg, RGMIIPCR, rgmii_pcr);
}

/* pfe_get_phydev_duplex
 */
static int pfe_get_phydev_duplex(struct phy_device *phydev)
{
	/*return (phydev->duplex == DUPLEX_HALF) ? DUP_HALF:DUP_FULL ; */
	return DUPLEX_FULL;
}

/* pfe_eth_adjust_link
 */
static void pfe_eth_adjust_link(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	unsigned long flags;
	struct phy_device *phydev = priv->phydev;
	int new_state = 0;

	netif_info(priv, drv, ndev, "%s\n", __func__);

	spin_lock_irqsave(&priv->lock, flags);

	if (phydev->link) {
		/*
		 * Now we make sure that we can be in full duplex mode.
		 * If not, we operate in half-duplex mode.
		 */
		if (phydev->duplex != priv->oldduplex) {
			new_state = 1;
			gemac_set_duplex(priv->EMAC_baseaddr,
					 pfe_get_phydev_duplex(phydev));
			priv->oldduplex = phydev->duplex;
		}

		if (phydev->speed != priv->oldspeed) {
			new_state = 1;
			gemac_set_speed(priv->EMAC_baseaddr,
					pfe_get_phydev_speed(phydev));
			if (priv->einfo->mii_config == PHY_INTERFACE_MODE_RGMII_TXID)
				pfe_set_rgmii_speed(phydev);
			priv->oldspeed = phydev->speed;
		}

		if (!priv->oldlink) {
			new_state = 1;
			priv->oldlink = 1;
		}

	} else if (priv->oldlink) {
		new_state = 1;
		priv->oldlink = 0;
		priv->oldspeed = 0;
		priv->oldduplex = -1;
	}

	if (new_state && netif_msg_link(priv))
		phy_print_status(phydev);

	spin_unlock_irqrestore(&priv->lock, flags);
}

/* pfe_phy_exit
 */
static void pfe_phy_exit(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	netif_info(priv, drv, ndev, "%s\n", __func__);

	phy_disconnect(priv->phydev);
	priv->phydev = NULL;
}

/* pfe_eth_stop
 */
static void pfe_eth_stop(struct net_device *ndev, int wake)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	netif_info(priv, drv, ndev, "%s\n", __func__);

	if (wake) {
		gemac_tx_disable(priv->EMAC_baseaddr);
	} else {
		gemac_disable(priv->EMAC_baseaddr);
		gpi_disable(priv->GPI_baseaddr);

		if (priv->phydev)
			phy_stop(priv->phydev);
	}
}

/* pfe_eth_start
 */
static int pfe_eth_start(struct pfe_eth_priv_s *priv)
{
	netif_info(priv, drv, priv->ndev, "%s\n", __func__);

	if (priv->phydev)
		phy_start(priv->phydev);

	gpi_enable(priv->GPI_baseaddr);
	gemac_enable(priv->EMAC_baseaddr);

	return 0;
}

/*
 * Configure on chip serdes through mdio
 */
static void ls1012a_configure_serdes(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = pfe->eth.eth_priv[0];
	int sgmii_2500 = 0;
	struct mii_bus *bus = priv->mii_bus;
	u16 value = 0;

	if (priv->einfo->mii_config == PHY_INTERFACE_MODE_2500SGMII)
		sgmii_2500 = 1;

	netif_info(priv, drv, ndev, "%s\n", __func__);
	/* PCS configuration done with corresponding GEMAC */

	pfe_eth_mdio_read(bus, 0, 0);
	pfe_eth_mdio_read(bus, 0, 1);

       /*These settings taken from validtion team */
	pfe_eth_mdio_write(bus, 0, 0x0, 0x8000);
	if (sgmii_2500) {
		pfe_eth_mdio_write(bus, 0, 0x14, 0x9);
		pfe_eth_mdio_write(bus, 0, 0x4, 0x4001);
		pfe_eth_mdio_write(bus, 0, 0x12, 0xa120);
		pfe_eth_mdio_write(bus, 0, 0x13, 0x7);
		/* Autonegotiation need to be disabled for 2.5G SGMII mode*/
		value = 0x0140;
		pfe_eth_mdio_write(bus, 0, 0x0, value);
	} else {
		pfe_eth_mdio_write(bus, 0, 0x14, 0xb);
		pfe_eth_mdio_write(bus, 0, 0x4, 0x4001); /* Autonegotiation is incorrect with value 0x1a1 */
		pfe_eth_mdio_write(bus, 0, 0x12, 0x400);
		pfe_eth_mdio_write(bus, 0, 0x13, 0x0);
		pfe_eth_mdio_write(bus, 0, 0x0, 0x1140);
	}
}

/*
 * pfe_phy_init
 *
 */
static int pfe_phy_init(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	struct phy_device *phydev;
	char phy_id[MII_BUS_ID_SIZE + 3];
	char bus_id[MII_BUS_ID_SIZE];
	phy_interface_t interface;
	
	struct mii_bus *bus = pfe->eth.eth_priv[0]->mii_bus;

	priv->oldlink = 0;
	priv->oldspeed = 0;
	priv->oldduplex = -1;

	snprintf(bus_id, MII_BUS_ID_SIZE, "ls1012a-%d", 0);
	snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
		 priv->einfo->phy_id);

	netif_info(priv, drv, ndev, "%s: %s\n", __func__, phy_id);
	interface = priv->einfo->mii_config;
	if ((interface == PHY_INTERFACE_MODE_SGMII) ||
	    (interface == PHY_INTERFACE_MODE_2500SGMII)) {
		/*Configure SGMII PCS */
		if (pfe->scfg) {
			/*Config MDIO from serdes */
			regmap_write(pfe->scfg, 0x484, 0x00000000);
		}
		ls1012a_configure_serdes(ndev);
	}

	if (pfe->scfg) {
		/*Config MDIO from PAD */
		regmap_write(pfe->scfg, 0x484, 0x80000000);
	}
	
	/* 
	 * Reset external PHY on MDIO bus with address 0 to undo
	 * changes from the unintended external writes during the 
	 * previous SERDES configuration. (Temporary fix)
	 */
	pfe_eth_mdio_write(bus, 0, 0x1F, 0x8000);
	msleep(10);
 	pfe_eth_mdio_write(bus, 0, 0x18, 0x61B6);

	priv->oldlink = 0;
	priv->oldspeed = 0;
	priv->oldduplex = -1;
	pr_info("%s interface %x\n", __func__, interface);
	phydev = phy_connect(ndev, phy_id, &pfe_eth_adjust_link, interface);

	if (IS_ERR(phydev)) {
		netdev_err(ndev, "phy_connect() failed\n");
		return PTR_ERR(phydev);
	}

	priv->phydev = phydev;
	phydev->irq = PHY_POLL;

	return 0;
}

/* pfe_gemac_init
 */
static int pfe_gemac_init(struct pfe_eth_priv_s *priv)
{
	struct gemac_cfg cfg;

	netif_info(priv, ifup, priv->ndev, "%s\n", __func__);

	cfg.speed = SPEED_1000M;
	cfg.duplex = DUPLEX_FULL;

	gemac_set_config(priv->EMAC_baseaddr, &cfg);
	gemac_allow_broadcast(priv->EMAC_baseaddr);
	gemac_enable_1536_rx(priv->EMAC_baseaddr);
	gemac_enable_rx_jmb(priv->EMAC_baseaddr);
	gemac_enable_stacked_vlan(priv->EMAC_baseaddr);
	gemac_enable_pause_rx(priv->EMAC_baseaddr);
	gemac_set_bus_width(priv->EMAC_baseaddr, 64);

	/*GEM will perform checksum verifications*/
	if (priv->ndev->features & NETIF_F_RXCSUM)
		gemac_enable_rx_checksum_offload(priv->EMAC_baseaddr);
	else
		gemac_disable_rx_checksum_offload(priv->EMAC_baseaddr);

	return 0;
}

/* pfe_eth_event_handler
 */
static int pfe_eth_event_handler(void *data, int event, int qno)
{
	struct pfe_eth_priv_s *priv = data;

	switch (event) {
	case EVENT_RX_PKT_IND:

		if (qno == 0) {
			if (napi_schedule_prep(&priv->high_napi)) {
				netif_info(priv, intr, priv->ndev,
					   "%s: schedule high prio poll\n"
					   , __func__);

#ifdef PFE_ETH_NAPI_STATS
				priv->napi_counters[NAPI_SCHED_COUNT]++;
#endif

				__napi_schedule(&priv->high_napi);
			}
		} else if (qno == 1) {
			if (napi_schedule_prep(&priv->low_napi)) {
				netif_info(priv, intr, priv->ndev,
					   "%s: schedule low prio poll\n"
					   , __func__);

#ifdef PFE_ETH_NAPI_STATS
				priv->napi_counters[NAPI_SCHED_COUNT]++;
#endif
				__napi_schedule(&priv->low_napi);
			}
		} else if (qno == 2) {
			if (napi_schedule_prep(&priv->lro_napi)) {
				netif_info(priv, intr, priv->ndev,
					   "%s: schedule lro prio poll\n"
					   , __func__);

#ifdef PFE_ETH_NAPI_STATS
				priv->napi_counters[NAPI_SCHED_COUNT]++;
#endif
				__napi_schedule(&priv->lro_napi);
			}
		}

		break;

	case EVENT_TXDONE_IND:
		pfe_eth_flush_tx(priv);
		hif_lib_event_handler_start(&priv->client, EVENT_TXDONE_IND, 0);
		break;
	case EVENT_HIGH_RX_WM:
	default:
		break;
	}

	return 0;
}

/* pfe_eth_open
 */
static int pfe_eth_open(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	struct hif_client_s *client;
	int rc;

	netif_info(priv, ifup, ndev, "%s\n", __func__);

	/* Register client driver with HIF */
	client = &priv->client;
	memset(client, 0, sizeof(*client));
	client->id = PFE_CL_GEM0 + priv->id;
	client->tx_qn = emac_txq_cnt;
	client->rx_qn = EMAC_RXQ_CNT;
	client->priv = priv;
	client->pfe = priv->pfe;
	client->event_handler = pfe_eth_event_handler;

	client->tx_qsize = EMAC_TXQ_DEPTH;
	client->rx_qsize = EMAC_RXQ_DEPTH;

	rc = hif_lib_client_register(client);
	if (rc) {
		netdev_err(ndev, "%s: hif_lib_client_register(%d) failed\n",
			   __func__, client->id);
		goto err0;
	}

	netif_info(priv, drv, ndev, "%s: registered client: %p\n", __func__,
		   client);

	pfe_gemac_init(priv);

	if (!is_valid_ether_addr(ndev->dev_addr)) {
		netdev_err(ndev, "%s: invalid MAC address\n", __func__);
		rc = -EADDRNOTAVAIL;
		goto err1;
	}

	gemac_set_laddrN(priv->EMAC_baseaddr,
			 (struct pfe_mac_addr *)ndev->dev_addr, 1);

	napi_enable(&priv->high_napi);
	napi_enable(&priv->low_napi);
	napi_enable(&priv->lro_napi);

	rc = pfe_eth_start(priv);

	netif_tx_wake_all_queues(ndev);

	return rc;

err1:
	hif_lib_client_unregister(&priv->client);

err0:
	return rc;
}

/*
 *  pfe_eth_shutdown
 */
int pfe_eth_shutdown(struct net_device *ndev, int wake)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	int i, qstatus;
	unsigned long next_poll = jiffies + 1, end = jiffies +
				(TX_POLL_TIMEOUT_MS * HZ) / 1000;
	int tx_pkts, prv_tx_pkts;

	netif_info(priv, ifdown, ndev, "%s\n", __func__);

	for (i = 0; i < emac_txq_cnt; i++)
		hrtimer_cancel(&priv->fast_tx_timeout[i].timer);

	netif_tx_stop_all_queues(ndev);

	do {
		tx_pkts = 0;
		pfe_eth_flush_tx(priv);

		for (i = 0; i < emac_txq_cnt; i++)
			tx_pkts += hif_lib_tx_pending(&priv->client, i);

		if (tx_pkts) {
			/*Don't wait forever, break if we cross max timeout */
			if (time_after(jiffies, end)) {
				pr_err(
					"(%s)Tx is not complete after %dmsec\n",
					ndev->name, TX_POLL_TIMEOUT_MS);
				break;
			}

			pr_info("%s : (%s) Waiting for tx packets to free. Pending tx pkts = %d.\n"
				, __func__, ndev->name, tx_pkts);
			if (need_resched())
				schedule();
		}

	} while (tx_pkts);

	end = jiffies + (TX_POLL_TIMEOUT_MS * HZ) / 1000;

	prv_tx_pkts = tmu_pkts_processed(priv->id);
	/*
	 * Wait till TMU transmits all pending packets
	 * poll tmu_qstatus and pkts processed by TMU for every 10ms
	 * Consider TMU is busy, If we see TMU qeueu pending or any packets
	 * processed by TMU
	 */
	while (1) {
		if (time_after(jiffies, next_poll)) {
			tx_pkts = tmu_pkts_processed(priv->id);
			qstatus = tmu_qstatus(priv->id) & 0x7ffff;

			if (!qstatus && (tx_pkts == prv_tx_pkts))
				break;
			/* Don't wait forever, break if we cross max
			 * timeout(TX_POLL_TIMEOUT_MS)
			 */
			if (time_after(jiffies, end)) {
				pr_err("TMU%d is busy after %dmsec\n",
				       priv->id, TX_POLL_TIMEOUT_MS);
				break;
			}
			prv_tx_pkts = tx_pkts;
			next_poll++;
		}
		if (need_resched())
			schedule();
	}
	/* Wait for some more time to complete transmitting packet if any */
	next_poll = jiffies + 1;
	while (1) {
		if (time_after(jiffies, next_poll))
			break;
		if (need_resched())
			schedule();
	}

	pfe_eth_stop(ndev, wake);

	napi_disable(&priv->lro_napi);
	napi_disable(&priv->low_napi);
	napi_disable(&priv->high_napi);

	hif_lib_client_unregister(&priv->client);

	return 0;
}

/* pfe_eth_close
 *
 */
static int pfe_eth_close(struct net_device *ndev)
{
	pfe_eth_shutdown(ndev, 0);

	return 0;
}

/* pfe_eth_suspend
 *
 * return value : 1 if netdevice is configured to wakeup system
 *                0 otherwise
 */
int pfe_eth_suspend(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	int retval = 0;

	if (priv->wol) {
		gemac_set_wol(priv->EMAC_baseaddr, priv->wol);
		retval = 1;
	}
	pfe_eth_shutdown(ndev, priv->wol);

	return retval;
}

/* pfe_eth_resume
 *
 */
int pfe_eth_resume(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	if (priv->wol)
		gemac_set_wol(priv->EMAC_baseaddr, 0);
	gemac_tx_enable(priv->EMAC_baseaddr);

	return pfe_eth_open(ndev);
}

/* pfe_eth_get_queuenum
 */
static int pfe_eth_get_queuenum(struct pfe_eth_priv_s *priv, struct sk_buff
					*skb)
{
	int queuenum = 0;
	unsigned long flags;

	/* Get the Fast Path queue number */
	/*
	 * Use conntrack mark (if conntrack exists), then packet mark (if any),
	 * then fallback to default
	 */
#if defined(CONFIG_IP_NF_CONNTRACK_MARK) || defined(CONFIG_NF_CONNTRACK_MARK)
	if (skb->nfct) {
		enum ip_conntrack_info cinfo;
		struct nf_conn *ct;

		ct = nf_ct_get(skb, &cinfo);

		if (ct) {
			u32 connmark;

			connmark = ct->mark;

			if ((connmark & 0x80000000) && priv->id != 0)
				connmark >>= 16;

			queuenum = connmark & EMAC_QUEUENUM_MASK;
		}
	} else  {/* continued after #endif ... */
#endif
		if (skb->mark) {
			queuenum = skb->mark & EMAC_QUEUENUM_MASK;
		} else {
			spin_lock_irqsave(&priv->lock, flags);
			queuenum = priv->default_priority & EMAC_QUEUENUM_MASK;
			spin_unlock_irqrestore(&priv->lock, flags);
		}
#if defined(CONFIG_IP_NF_CONNTRACK_MARK) || defined(CONFIG_NF_CONNTRACK_MARK)
	}
#endif
	return queuenum;
}

/* pfe_eth_might_stop_tx
 *
 */
static int pfe_eth_might_stop_tx(struct pfe_eth_priv_s *priv, int queuenum,
				 struct netdev_queue *tx_queue,
				 unsigned int n_desc,
				 unsigned int n_segs)
{
	ktime_t kt;
	int tried = 0;

try_again:
	if (unlikely((__hif_tx_avail(&pfe->hif) < n_desc) ||
	(hif_lib_tx_avail(&priv->client, queuenum) < n_desc) ||
	(hif_lib_tx_credit_avail(pfe, priv->id, queuenum) < n_segs))) {
		if (!tried) {
			__hif_lib_update_credit(&priv->client, queuenum);
			tried = 1;
			goto try_again;
		}
#ifdef PFE_ETH_TX_STATS
		if (__hif_tx_avail(&pfe->hif) < n_desc) {
			priv->stop_queue_hif[queuenum]++;
		} else if (hif_lib_tx_avail(&priv->client, queuenum) < n_desc) {
			priv->stop_queue_hif_client[queuenum]++;
		} else if (hif_lib_tx_credit_avail(pfe, priv->id, queuenum) <
			n_segs) {
			priv->stop_queue_credit[queuenum]++;
		}
		priv->stop_queue_total[queuenum]++;
#endif
		netif_tx_stop_queue(tx_queue);

		kt = ktime_set(0, LS1012A_TX_FAST_RECOVERY_TIMEOUT_MS *
				NSEC_PER_MSEC);
		hrtimer_start(&priv->fast_tx_timeout[queuenum].timer, kt,
			      HRTIMER_MODE_REL);
		return -1;
	} else {
		return 0;
	}
}

#define SA_MAX_OP 2
/* pfe_hif_send_packet
 *
 * At this level if TX fails we drop the packet
 */
static void pfe_hif_send_packet(struct sk_buff *skb, struct  pfe_eth_priv_s
					*priv, int queuenum)
{
	struct skb_shared_info *sh = skb_shinfo(skb);
	unsigned int nr_frags;
	u32 ctrl = 0;

	netif_info(priv, tx_queued, priv->ndev, "%s\n", __func__);

	if (skb_is_gso(skb)) {
		priv->stats.tx_dropped++;
		return;
	}

	if (skb->ip_summed == CHECKSUM_PARTIAL)
		ctrl = HIF_CTRL_TX_CHECKSUM;

	nr_frags = sh->nr_frags;

	if (nr_frags) {
		skb_frag_t *f;
		int i;

		__hif_lib_xmit_pkt(&priv->client, queuenum, skb->data,
				   skb_headlen(skb), ctrl, HIF_FIRST_BUFFER,
				   skb);

		for (i = 0; i < nr_frags - 1; i++) {
			f = &sh->frags[i];
			__hif_lib_xmit_pkt(&priv->client, queuenum,
					   skb_frag_address(f),
					   skb_frag_size(f),
					   0x0, 0x0, skb);
		}

		f = &sh->frags[i];

		__hif_lib_xmit_pkt(&priv->client, queuenum,
				   skb_frag_address(f), skb_frag_size(f),
				   0x0, HIF_LAST_BUFFER | HIF_DATA_VALID,
				   skb);

		netif_info(priv, tx_queued, priv->ndev,
			   "%s: pkt sent successfully skb:%p nr_frags:%d len:%d\n",
			   __func__, skb, nr_frags, skb->len);
	} else {
		__hif_lib_xmit_pkt(&priv->client, queuenum, skb->data,
				   skb->len, ctrl, HIF_FIRST_BUFFER |
				   HIF_LAST_BUFFER | HIF_DATA_VALID,
				   skb);
		netif_info(priv, tx_queued, priv->ndev,
			   "%s: pkt sent successfully skb:%p len:%d\n",
			   __func__, skb, skb->len);
	}
	hif_tx_dma_start();
	priv->stats.tx_packets++;
	priv->stats.tx_bytes += skb->len;
	hif_lib_tx_credit_use(pfe, priv->id, queuenum, 1);
}

/* pfe_eth_flush_txQ
 */
static void pfe_eth_flush_txQ(struct pfe_eth_priv_s *priv, int tx_q_num, int
				from_tx, int n_desc)
{
	struct sk_buff *skb;
	struct netdev_queue *tx_queue = netdev_get_tx_queue(priv->ndev,
								tx_q_num);
	unsigned int flags;

	netif_info(priv, tx_done, priv->ndev, "%s\n", __func__);

	if (!from_tx)
		__netif_tx_lock_bh(tx_queue);

	/* Clean HIF and client queue */
	while ((skb = hif_lib_tx_get_next_complete(&priv->client,
						   tx_q_num, &flags,
						   HIF_TX_DESC_NT))) {
		if (flags & HIF_DATA_VALID)
			dev_kfree_skb_any(skb);
	}
	if (!from_tx)
		__netif_tx_unlock_bh(tx_queue);
}

/* pfe_eth_flush_tx
 */
static void pfe_eth_flush_tx(struct pfe_eth_priv_s *priv)
{
	int ii;

	netif_info(priv, tx_done, priv->ndev, "%s\n", __func__);

	for (ii = 0; ii < emac_txq_cnt; ii++) {
		pfe_eth_flush_txQ(priv, ii, 0, 0);
		__hif_lib_update_credit(&priv->client, ii);
	}
}

void pfe_tx_get_req_desc(struct sk_buff *skb, unsigned int *n_desc, unsigned int
				*n_segs)
{
	struct skb_shared_info *sh = skb_shinfo(skb);

	/* Scattered data */
	if (sh->nr_frags) {
		*n_desc = sh->nr_frags + 1;
		*n_segs = 1;
	/* Regular case */
	} else {
		*n_desc = 1;
		*n_segs = 1;
	}
}

/* pfe_eth_send_packet
 */
static int pfe_eth_send_packet(struct sk_buff *skb, struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	int tx_q_num = skb_get_queue_mapping(skb);
	int n_desc, n_segs;
	struct netdev_queue *tx_queue = netdev_get_tx_queue(priv->ndev,
								tx_q_num);

	netif_info(priv, tx_queued, ndev, "%s\n", __func__);

	if ((!skb_is_gso(skb)) && (skb_headroom(skb) < (PFE_PKT_HEADER_SZ +
			sizeof(unsigned long)))) {
		netif_warn(priv, tx_err, priv->ndev, "%s: copying skb\n",
			   __func__);

		if (pskb_expand_head(skb, (PFE_PKT_HEADER_SZ + sizeof(unsigned
					long)), 0, GFP_ATOMIC)) {
			/* No need to re-transmit, no way to recover*/
			kfree_skb(skb);
			priv->stats.tx_dropped++;
			return NETDEV_TX_OK;
		}
	}

	pfe_tx_get_req_desc(skb, &n_desc, &n_segs);

	hif_tx_lock(&pfe->hif);
	if (unlikely(pfe_eth_might_stop_tx(priv, tx_q_num, tx_queue, n_desc,
					   n_segs))) {
#ifdef PFE_ETH_TX_STATS
		if (priv->was_stopped[tx_q_num]) {
			priv->clean_fail[tx_q_num]++;
			priv->was_stopped[tx_q_num] = 0;
		}
#endif
		hif_tx_unlock(&pfe->hif);
		return NETDEV_TX_BUSY;
	}

	pfe_hif_send_packet(skb, priv, tx_q_num);

	hif_tx_unlock(&pfe->hif);

	tx_queue->trans_start = jiffies;

#ifdef PFE_ETH_TX_STATS
	priv->was_stopped[tx_q_num] = 0;
#endif

	return NETDEV_TX_OK;
}

/* pfe_eth_select_queue
 *
 */
static u16 pfe_eth_select_queue(struct net_device *ndev, struct sk_buff *skb,
				void *accel_priv,
				select_queue_fallback_t fallback)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	return pfe_eth_get_queuenum(priv, skb);
}

/* pfe_eth_get_stats
 */
static struct net_device_stats *pfe_eth_get_stats(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);

	netif_info(priv, drv, ndev, "%s\n", __func__);

	return &priv->stats;
}

/* pfe_eth_set_mac_address
 */
static int pfe_eth_set_mac_address(struct net_device *ndev, void *addr)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	struct sockaddr *sa = addr;

	netif_info(priv, drv, ndev, "%s\n", __func__);

	if (!is_valid_ether_addr(sa->sa_data))
		return -EADDRNOTAVAIL;

	memcpy(ndev->dev_addr, sa->sa_data, ETH_ALEN);

	gemac_set_laddrN(priv->EMAC_baseaddr,
			 (struct pfe_mac_addr *)ndev->dev_addr, 1);

	return 0;
}

/* pfe_eth_enet_addr_byte_mac
 */
int pfe_eth_enet_addr_byte_mac(u8 *enet_byte_addr,
			       struct pfe_mac_addr *enet_addr)
{
	if (!enet_byte_addr || !enet_addr) {
		return -1;

	} else {
		enet_addr->bottom = enet_byte_addr[0] |
			(enet_byte_addr[1] << 8) |
			(enet_byte_addr[2] << 16) |
			(enet_byte_addr[3] << 24);
		enet_addr->top = enet_byte_addr[4] |
			(enet_byte_addr[5] << 8);
		return 0;
	}
}

/* pfe_eth_set_multi
 */
static void pfe_eth_set_multi(struct net_device *ndev)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	struct pfe_mac_addr    hash_addr; /* hash register structure */
	/* specific mac address	register structure */
	struct pfe_mac_addr    spec_addr;
	int		result; /* index into hash register to set.. */
	int		uc_count = 0;
	struct netdev_hw_addr *ha;

	if (ndev->flags & IFF_PROMISC) {
		netif_info(priv, drv, ndev, "entering promiscuous mode\n");

		priv->promisc = 1;
		gemac_enable_copy_all(priv->EMAC_baseaddr);
	} else {
		priv->promisc = 0;
		gemac_disable_copy_all(priv->EMAC_baseaddr);
	}

	/* Enable broadcast frame reception if required. */
	if (ndev->flags & IFF_BROADCAST) {
		gemac_allow_broadcast(priv->EMAC_baseaddr);
	} else {
		netif_info(priv, drv, ndev,
			   "disabling broadcast frame reception\n");

		gemac_no_broadcast(priv->EMAC_baseaddr);
	}

	if (ndev->flags & IFF_ALLMULTI) {
		/* Set the hash to rx all multicast frames */
		hash_addr.bottom = 0xFFFFFFFF;
		hash_addr.top = 0xFFFFFFFF;
		gemac_set_hash(priv->EMAC_baseaddr, &hash_addr);
		netdev_for_each_uc_addr(ha, ndev) {
			if (uc_count >= MAX_UC_SPEC_ADDR_REG)
				break;
			pfe_eth_enet_addr_byte_mac(ha->addr, &spec_addr);
			gemac_set_laddrN(priv->EMAC_baseaddr, &spec_addr,
					 uc_count + 2);
			uc_count++;
		}
	} else if ((netdev_mc_count(ndev) > 0)  || (netdev_uc_count(ndev))) {
		u8 *addr;

		hash_addr.bottom = 0;
		hash_addr.top = 0;

		netdev_for_each_mc_addr(ha, ndev) {
			addr = ha->addr;

			netif_info(priv, drv, ndev,
				   "adding multicast address %X:%X:%X:%X:%X:%X to gem filter\n",
				addr[0], addr[1], addr[2],
				addr[3], addr[4], addr[5]);

			result = pfe_eth_get_hash(addr);

			if (result < EMAC_HASH_REG_BITS) {
				if (result < 32)
					hash_addr.bottom |= (1 << result);
				else
					hash_addr.top |= (1 << (result - 32));
			} else {
				break;
			}
		}

		uc_count = -1;
		netdev_for_each_uc_addr(ha, ndev) {
			addr = ha->addr;

			if (++uc_count < MAX_UC_SPEC_ADDR_REG)   {
				netdev_info(ndev,
					    "adding unicast address %02x:%02x:%02x:%02x:%02x:%02x to gem filter\n",
					    addr[0], addr[1], addr[2],
					    addr[3], addr[4], addr[5]);
				pfe_eth_enet_addr_byte_mac(addr, &spec_addr);
				gemac_set_laddrN(priv->EMAC_baseaddr,
						 &spec_addr, uc_count + 2);
			} else {
				netif_info(priv, drv, ndev,
					   "adding unicast address %02x:%02x:%02x:%02x:%02x:%02x to gem hash\n",
					   addr[0], addr[1], addr[2],
					   addr[3], addr[4], addr[5]);

				result = pfe_eth_get_hash(addr);
				if (result >= EMAC_HASH_REG_BITS) {
					break;

				} else {
					if (result < 32)
						hash_addr.bottom |= (1 <<
								result);
					else
						hash_addr.top |= (1 <<
								(result - 32));
				}
			}
		}

		gemac_set_hash(priv->EMAC_baseaddr, &hash_addr);
	}

	if (!(netdev_uc_count(ndev) >= MAX_UC_SPEC_ADDR_REG)) {
		/*
		 *  Check if there are any specific address HW registers that
		 * need to be flushed
		 */
		for (uc_count = netdev_uc_count(ndev); uc_count <
			MAX_UC_SPEC_ADDR_REG; uc_count++)
			gemac_clear_laddrN(priv->EMAC_baseaddr, uc_count + 2);
	}

	if (ndev->flags & IFF_LOOPBACK)
		gemac_set_loop(priv->EMAC_baseaddr, LB_LOCAL);
}

/* pfe_eth_set_features
 */
static int pfe_eth_set_features(struct net_device *ndev, netdev_features_t
					features)
{
	struct pfe_eth_priv_s *priv = netdev_priv(ndev);
	int rc = 0;

	if (features & NETIF_F_RXCSUM)
		gemac_enable_rx_checksum_offload(priv->EMAC_baseaddr);
	else
		gemac_disable_rx_checksum_offload(priv->EMAC_baseaddr);
	return rc;
}

/* pfe_eth_fast_tx_timeout
 */
static enum hrtimer_restart pfe_eth_fast_tx_timeout(struct hrtimer *timer)
{
	struct pfe_eth_fast_timer *fast_tx_timeout = container_of(timer, struct
							pfe_eth_fast_timer,
							timer);
	struct pfe_eth_priv_s *priv =  container_of(fast_tx_timeout->base,
							struct pfe_eth_priv_s,
							fast_tx_timeout);
	struct netdev_queue *tx_queue = netdev_get_tx_queue(priv->ndev,
						fast_tx_timeout->queuenum);

	if (netif_tx_queue_stopped(tx_queue)) {
#ifdef PFE_ETH_TX_STATS
		priv->was_stopped[fast_tx_timeout->queuenum] = 1;
#endif
		netif_tx_wake_queue(tx_queue);
	}

	return HRTIMER_NORESTART;
}

/* pfe_eth_fast_tx_timeout_init
 */
static void pfe_eth_fast_tx_timeout_init(struct pfe_eth_priv_s *priv)
{
	int i;

	for (i = 0; i < emac_txq_cnt; i++) {
		priv->fast_tx_timeout[i].queuenum = i;
		hrtimer_init(&priv->fast_tx_timeout[i].timer, CLOCK_MONOTONIC,
			     HRTIMER_MODE_REL);
		priv->fast_tx_timeout[i].timer.function =
				pfe_eth_fast_tx_timeout;
		priv->fast_tx_timeout[i].base = priv->fast_tx_timeout;
	}
}

static struct sk_buff *pfe_eth_rx_skb(struct net_device *ndev,
				      struct	pfe_eth_priv_s *priv,
				      unsigned int qno)
{
	void *buf_addr;
	unsigned int rx_ctrl;
	unsigned int desc_ctrl = 0;
	struct hif_ipsec_hdr *ipsec_hdr = NULL;
	struct sk_buff *skb;
	struct sk_buff *skb_frag, *skb_frag_last = NULL;
	int length = 0, offset;

	skb = priv->skb_inflight[qno];

	if (skb) {
		skb_frag_last = skb_shinfo(skb)->frag_list;
		if (skb_frag_last) {
			while (skb_frag_last->next)
				skb_frag_last = skb_frag_last->next;
		}
	}

	while (!(desc_ctrl & CL_DESC_LAST)) {
		buf_addr = hif_lib_receive_pkt(&priv->client, qno, &length,
					       &offset, &rx_ctrl, &desc_ctrl,
					       (void **)&ipsec_hdr);
		if (!buf_addr)
			goto incomplete;

#ifdef PFE_ETH_NAPI_STATS
		priv->napi_counters[NAPI_DESC_COUNT]++;
#endif

		/* First frag */
		if (desc_ctrl & CL_DESC_FIRST) {
			skb = build_skb(buf_addr, 0);
			if (unlikely(!skb))
				goto pkt_drop;

			skb_reserve(skb, offset);
			skb_put(skb, length);
			skb->dev = ndev;

			if ((ndev->features & NETIF_F_RXCSUM) && (rx_ctrl &
					HIF_CTRL_RX_CHECKSUMMED))
				skb->ip_summed = CHECKSUM_UNNECESSARY;
			else
				skb_checksum_none_assert(skb);

		} else {
			/* Next frags */
			if (unlikely(!skb)) {
				pr_err("%s: NULL skb_inflight\n",
				       __func__);
				goto pkt_drop;
			}

			skb_frag = build_skb(buf_addr, 0);

			if (unlikely(!skb_frag)) {
				kfree(buf_addr);
				goto pkt_drop;
			}

			skb_reserve(skb_frag, offset);
			skb_put(skb_frag, length);

			skb_frag->dev = ndev;

			if (skb_shinfo(skb)->frag_list)
				skb_frag_last->next = skb_frag;
			else
				skb_shinfo(skb)->frag_list = skb_frag;

			skb->truesize += skb_frag->truesize;
			skb->data_len += length;
			skb->len += length;
			skb_frag_last = skb_frag;
		}
	}

	priv->skb_inflight[qno] = NULL;
	return skb;

incomplete:
	priv->skb_inflight[qno] = skb;
	return NULL;

pkt_drop:
	priv->skb_inflight[qno] = NULL;

	if (skb)
		kfree_skb(skb);
	else
		kfree(buf_addr);

	priv->stats.rx_errors++;

	return NULL;
}

/* pfe_eth_poll
 */
static int pfe_eth_poll(struct pfe_eth_priv_s *priv, struct napi_struct *napi,
			unsigned int qno, int budget)
{
	struct net_device *ndev = priv->ndev;
	struct sk_buff *skb;
	int work_done = 0;
	unsigned int len;

	netif_info(priv, intr, priv->ndev, "%s\n", __func__);

#ifdef PFE_ETH_NAPI_STATS
	priv->napi_counters[NAPI_POLL_COUNT]++;
#endif

	do {
		skb = pfe_eth_rx_skb(ndev, priv, qno);

		if (!skb)
			break;

		len = skb->len;

		/* Packet will be processed */
		skb->protocol = eth_type_trans(skb, ndev);

		netif_receive_skb(skb);

		priv->stats.rx_packets++;
		priv->stats.rx_bytes += len;

		work_done++;

#ifdef PFE_ETH_NAPI_STATS
		priv->napi_counters[NAPI_PACKET_COUNT]++;
#endif

	} while (work_done < budget);

	/*
	 * If no Rx receive nor cleanup work was done, exit polling mode.
	 * No more netif_running(dev) check is required here , as this is
	 * checked in net/core/dev.c (2.6.33.5 kernel specific).
	 */
	if (work_done < budget) {
		napi_complete(napi);

		hif_lib_event_handler_start(&priv->client, EVENT_RX_PKT_IND,
					    qno);
	}
#ifdef PFE_ETH_NAPI_STATS
	else
		priv->napi_counters[NAPI_FULL_BUDGET_COUNT]++;
#endif

	return work_done;
}

/*
 * pfe_eth_lro_poll
 */
static int pfe_eth_lro_poll(struct napi_struct *napi, int budget)
{
	struct pfe_eth_priv_s *priv = container_of(napi, struct pfe_eth_priv_s,
							lro_napi);

	netif_info(priv, intr, priv->ndev, "%s\n", __func__);

	return pfe_eth_poll(priv, napi, 2, budget);
}

/* pfe_eth_low_poll
 */
static int pfe_eth_low_poll(struct napi_struct *napi, int budget)
{
	struct pfe_eth_priv_s *priv = container_of(napi, struct pfe_eth_priv_s,
							low_napi);

	netif_info(priv, intr, priv->ndev, "%s\n", __func__);

	return pfe_eth_poll(priv, napi, 1, budget);
}

/* pfe_eth_high_poll
 */
static int pfe_eth_high_poll(struct napi_struct *napi, int budget)
{
	struct pfe_eth_priv_s *priv = container_of(napi, struct pfe_eth_priv_s,
							high_napi);

	netif_info(priv, intr, priv->ndev, "%s\n", __func__);

	return pfe_eth_poll(priv, napi, 0, budget);
}

static const struct net_device_ops pfe_netdev_ops = {
	.ndo_open = pfe_eth_open,
	.ndo_stop = pfe_eth_close,
	.ndo_start_xmit = pfe_eth_send_packet,
	.ndo_select_queue = pfe_eth_select_queue,
	.ndo_get_stats = pfe_eth_get_stats,
	.ndo_set_mac_address = pfe_eth_set_mac_address,
	.ndo_set_rx_mode = pfe_eth_set_multi,
	.ndo_set_features = pfe_eth_set_features,
	.ndo_validate_addr = eth_validate_addr,
};

/* pfe_eth_init_one
 */
static int pfe_eth_init_one(struct pfe *pfe, int id)
{
	struct net_device *ndev = NULL;
	struct pfe_eth_priv_s *priv = NULL;
	struct ls1012a_eth_platform_data *einfo;
	struct ls1012a_mdio_platform_data *minfo;
	struct ls1012a_pfe_platform_data *pfe_info;
	int err;

	/* Extract pltform data */
	pfe_info = (struct ls1012a_pfe_platform_data *)
					pfe->dev->platform_data;
	if (!pfe_info) {
		pr_err(
			"%s: pfe missing additional platform data\n"
			, __func__);
		err = -ENODEV;
		goto err0;
	}

	einfo = (struct ls1012a_eth_platform_data *)
				pfe_info->ls1012a_eth_pdata;

	/* einfo never be NULL, but no harm in having this check */
	if (!einfo) {
		pr_err(
			"%s: pfe missing additional gemacs platform data\n"
			, __func__);
		err = -ENODEV;
		goto err0;
	}

	minfo = (struct ls1012a_mdio_platform_data *)
				pfe_info->ls1012a_mdio_pdata;

	/* einfo never be NULL, but no harm in having this check */
	if (!minfo) {
		pr_err(
			"%s: pfe missing additional mdios platform data\n",
			 __func__);
		err = -ENODEV;
		goto err0;
	}

	/* Create an ethernet device instance */
	ndev = alloc_etherdev_mq(sizeof(*priv), emac_txq_cnt);

	if (!ndev) {
		pr_err("%s: gemac %d device allocation failed\n",
		       __func__, einfo[id].gem_id);
		err = -ENOMEM;
		goto err0;
	}

	priv = netdev_priv(ndev);
	priv->ndev = ndev;
	priv->id = einfo[id].gem_id;
	priv->pfe = pfe;

	SET_NETDEV_DEV(priv->ndev, priv->pfe->dev);

	pfe->eth.eth_priv[id] = priv;

	/* Set the info in the priv to the current info */
	priv->einfo = &einfo[id];
	priv->EMAC_baseaddr = cbus_emac_base[id];
	priv->PHY_baseaddr = cbus_emac_base[0];
	priv->GPI_baseaddr = cbus_gpi_base[id];

#define HIF_GEMAC_TMUQ_BASE	6
	priv->low_tmu_q	=  HIF_GEMAC_TMUQ_BASE + (id * 2);
	priv->high_tmu_q	=  priv->low_tmu_q + 1;

	spin_lock_init(&priv->lock);

	pfe_eth_fast_tx_timeout_init(priv);

	/* Copy the station address into the dev structure, */
	memcpy(ndev->dev_addr, einfo[id].mac_addr, ETH_ALEN);

	/* Initialize mdio */
	if (minfo[id].enabled) {
		err = pfe_eth_mdio_init(priv, &minfo[id]);
		if (err) {
			netdev_err(ndev, "%s: pfe_eth_mdio_init() failed\n",
				   __func__);
			goto err2;
		}
	}

	ndev->mtu = 1500;

	/* Set MTU limits */
	ndev->min_mtu = ETH_MIN_MTU;
	ndev->max_mtu = JUMBO_FRAME_SIZE;

	/* supported features */
	ndev->hw_features = NETIF_F_SG;

	/*Enable after checksum offload is validated */
	ndev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
		NETIF_F_IPV6_CSUM | NETIF_F_SG;

	/* enabled by default */
	ndev->features = ndev->hw_features;

	priv->usr_features = ndev->features;

	ndev->netdev_ops = &pfe_netdev_ops;

	ndev->ethtool_ops = &pfe_ethtool_ops;

	/* Enable basic messages by default */
	priv->msg_enable = NETIF_MSG_IFUP | NETIF_MSG_IFDOWN | NETIF_MSG_LINK |
				NETIF_MSG_PROBE;

	netif_napi_add(ndev, &priv->low_napi, pfe_eth_low_poll,
		       HIF_RX_POLL_WEIGHT - 16);
	netif_napi_add(ndev, &priv->high_napi, pfe_eth_high_poll,
		       HIF_RX_POLL_WEIGHT - 16);
	netif_napi_add(ndev, &priv->lro_napi, pfe_eth_lro_poll,
		       HIF_RX_POLL_WEIGHT - 16);

	err = register_netdev(ndev);

	if (err) {
		netdev_err(ndev, "register_netdev() failed\n");
		goto err3;
	}
	device_init_wakeup(&ndev->dev, WAKE_MAGIC);

	if (!(priv->einfo->phy_flags & GEMAC_NO_PHY)) {
		err = pfe_phy_init(ndev);
		if (err) {
			netdev_err(ndev, "%s: pfe_phy_init() failed\n",
				   __func__);
			goto err4;
		}
	}

	netif_carrier_on(ndev);

	/* Create all the sysfs files */
	if (pfe_eth_sysfs_init(ndev))
		goto err4;

	netif_info(priv, probe, ndev, "%s: created interface, baseaddr: %p\n",
		   __func__, priv->EMAC_baseaddr);

	return 0;
err4:
	unregister_netdev(ndev);
err3:
	pfe_eth_mdio_exit(priv->mii_bus);
err2:
	free_netdev(priv->ndev);
err0:
	return err;
}

/* pfe_eth_init
 */
int pfe_eth_init(struct pfe *pfe)
{
	int ii = 0;
	int err;

	pr_info("%s\n", __func__);

	cbus_emac_base[0] = EMAC1_BASE_ADDR;
	cbus_emac_base[1] = EMAC2_BASE_ADDR;

	cbus_gpi_base[0] = EGPI1_BASE_ADDR;
	cbus_gpi_base[1] = EGPI2_BASE_ADDR;

	for (ii = 0; ii < NUM_GEMAC_SUPPORT; ii++) {
		err = pfe_eth_init_one(pfe, ii);
		if (err)
			goto err0;
	}

	return 0;

err0:
	while (ii--)
		pfe_eth_exit_one(pfe->eth.eth_priv[ii]);

	/* Register three network devices in the kernel */
	return err;
}

/* pfe_eth_exit_one
 */
static void pfe_eth_exit_one(struct pfe_eth_priv_s *priv)
{
	netif_info(priv, probe, priv->ndev, "%s\n", __func__);

	pfe_eth_sysfs_exit(priv->ndev);

	unregister_netdev(priv->ndev);

	if (!(priv->einfo->phy_flags & GEMAC_NO_PHY))
		pfe_phy_exit(priv->ndev);

	if (priv->mii_bus)
		pfe_eth_mdio_exit(priv->mii_bus);

	free_netdev(priv->ndev);
}

/* pfe_eth_exit
 */
void pfe_eth_exit(struct pfe *pfe)
{
	int ii;

	pr_info("%s\n", __func__);

	for (ii = NUM_GEMAC_SUPPORT - 1; ii >= 0; ii--)
		pfe_eth_exit_one(pfe->eth.eth_priv[ii]);
}