summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/shared/lpc/network/lpc-ethernet.c
blob: 5692531d23b7a9f764088ab196164c78a9e53def (plain) (blame)
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
/**
 * @file
 *
 * @ingroup lpc_eth
 *
 * @brief Ethernet driver.
 */

/*
 * Copyright (c) 2009-2012 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Obere Lagerstr. 30
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#define __INSIDE_RTEMS_BSD_TCPIP_STACK__ 1
#define __BSD_VISIBLE 1

#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include <rtems.h>
#include <rtems/rtems_bsdnet.h>
#include <rtems/rtems_mii_ioctl.h>

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>

#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>

#include <bsp.h>
#include <bsp/irq.h>
#include <bsp/lpc-ethernet-config.h>
#include <bsp/utility.h>

#if MCLBYTES > (2 * 1024)
  #error "MCLBYTES to large"
#endif

#ifdef LPC_ETH_CONFIG_USE_TRANSMIT_DMA
  #define LPC_ETH_CONFIG_TX_BUF_SIZE sizeof(struct mbuf *)
#else
  #define LPC_ETH_CONFIG_TX_BUF_SIZE 1518U
#endif

#define DEFAULT_PHY 0
#define WATCHDOG_TIMEOUT 5

typedef struct {
  uint32_t start;
  uint32_t control;
} lpc_eth_transfer_descriptor;

typedef struct {
  uint32_t info;
  uint32_t hash_crc;
} lpc_eth_receive_status;

typedef struct {
  uint32_t mac1;
  uint32_t mac2;
  uint32_t ipgt;
  uint32_t ipgr;
  uint32_t clrt;
  uint32_t maxf;
  uint32_t supp;
  uint32_t test;
  uint32_t mcfg;
  uint32_t mcmd;
  uint32_t madr;
  uint32_t mwtd;
  uint32_t mrdd;
  uint32_t mind;
  uint32_t reserved_0 [2];
  uint32_t sa0;
  uint32_t sa1;
  uint32_t sa2;
  uint32_t reserved_1 [45];
  uint32_t command;
  uint32_t status;
  uint32_t rxdescriptor;
  uint32_t rxstatus;
  uint32_t rxdescriptornum;
  uint32_t rxproduceindex;
  uint32_t rxconsumeindex;
  uint32_t txdescriptor;
  uint32_t txstatus;
  uint32_t txdescriptornum;
  uint32_t txproduceindex;
  uint32_t txconsumeindex;
  uint32_t reserved_2 [10];
  uint32_t tsv0;
  uint32_t tsv1;
  uint32_t rsv;
  uint32_t reserved_3 [3];
  uint32_t flowcontrolcnt;
  uint32_t flowcontrolsts;
  uint32_t reserved_4 [34];
  uint32_t rxfilterctrl;
  uint32_t rxfilterwolsts;
  uint32_t rxfilterwolclr;
  uint32_t reserved_5 [1];
  uint32_t hashfilterl;
  uint32_t hashfilterh;
  uint32_t reserved_6 [882];
  uint32_t intstatus;
  uint32_t intenable;
  uint32_t intclear;
  uint32_t intset;
  uint32_t reserved_7 [1];
  uint32_t powerdown;
} lpc_eth_controller;

static volatile lpc_eth_controller *const lpc_eth = 
  (volatile lpc_eth_controller *) LPC_ETH_CONFIG_REG_BASE;

/* ETH_RX_CTRL */

#define ETH_RX_CTRL_SIZE_MASK 0x000007ffU
#define ETH_RX_CTRL_INTERRUPT 0x80000000U

/* ETH_RX_STAT */

#define ETH_RX_STAT_RXSIZE_MASK 0x000007ffU
#define ETH_RX_STAT_BYTES 0x00000100U
#define ETH_RX_STAT_CONTROL_FRAME 0x00040000U
#define ETH_RX_STAT_VLAN 0x00080000U
#define ETH_RX_STAT_FAIL_FILTER 0x00100000U
#define ETH_RX_STAT_MULTICAST 0x00200000U
#define ETH_RX_STAT_BROADCAST 0x00400000U
#define ETH_RX_STAT_CRC_ERROR 0x00800000U
#define ETH_RX_STAT_SYMBOL_ERROR 0x01000000U
#define ETH_RX_STAT_LENGTH_ERROR 0x02000000U
#define ETH_RX_STAT_RANGE_ERROR 0x04000000U
#define ETH_RX_STAT_ALIGNMENT_ERROR 0x08000000U
#define ETH_RX_STAT_OVERRUN 0x10000000U
#define ETH_RX_STAT_NO_DESCRIPTOR 0x20000000U
#define ETH_RX_STAT_LAST_FLAG 0x40000000U
#define ETH_RX_STAT_ERROR 0x80000000U

/* ETH_TX_CTRL */

#define ETH_TX_CTRL_SIZE_MASK 0x7ffU
#define ETH_TX_CTRL_SIZE_SHIFT 0
#define ETH_TX_CTRL_OVERRIDE 0x04000000U
#define ETH_TX_CTRL_HUGE 0x08000000U
#define ETH_TX_CTRL_PAD 0x10000000U
#define ETH_TX_CTRL_CRC 0x20000000U
#define ETH_TX_CTRL_LAST 0x40000000U
#define ETH_TX_CTRL_INTERRUPT 0x80000000U

/* ETH_TX_STAT */

#define ETH_TX_STAT_COLLISION_COUNT_MASK 0x01e00000U
#define ETH_TX_STAT_DEFER 0x02000000U
#define ETH_TX_STAT_EXCESSIVE_DEFER 0x04000000U
#define ETH_TX_STAT_EXCESSIVE_COLLISION 0x08000000U
#define ETH_TX_STAT_LATE_COLLISION 0x10000000U
#define ETH_TX_STAT_UNDERRUN 0x20000000U
#define ETH_TX_STAT_NO_DESCRIPTOR 0x40000000U
#define ETH_TX_STAT_ERROR 0x80000000U

/* ETH_INT */

#define ETH_INT_RX_OVERRUN 0x00000001U
#define ETH_INT_RX_ERROR 0x00000002U
#define ETH_INT_RX_FINISHED 0x00000004U
#define ETH_INT_RX_DONE 0x00000008U
#define ETH_INT_TX_UNDERRUN 0x00000010U
#define ETH_INT_TX_ERROR 0x00000020U
#define ETH_INT_TX_FINISHED 0x00000040U
#define ETH_INT_TX_DONE 0x00000080U
#define ETH_INT_SOFT 0x00001000U
#define ETH_INT_WAKEUP 0x00002000U

/* ETH_RX_FIL_CTRL */

#define ETH_RX_FIL_CTRL_ACCEPT_UNICAST 0x00000001U
#define ETH_RX_FIL_CTRL_ACCEPT_BROADCAST 0x00000002U
#define ETH_RX_FIL_CTRL_ACCEPT_MULTICAST 0x00000004U
#define ETH_RX_FIL_CTRL_ACCEPT_UNICAST_HASH 0x00000008U
#define ETH_RX_FIL_CTRL_ACCEPT_MULTICAST_HASH 0x00000010U
#define ETH_RX_FIL_CTRL_ACCEPT_PERFECT 0x00000020U
#define ETH_RX_FIL_CTRL_MAGIC_PACKET_WOL 0x00001000U
#define ETH_RX_FIL_CTRL_RX_FILTER_WOL 0x00002000U

/* ETH_CMD */

#define ETH_CMD_RX_ENABLE 0x00000001U
#define ETH_CMD_TX_ENABLE 0x00000002U
#define ETH_CMD_REG_RESET 0x00000008U
#define ETH_CMD_TX_RESET 0x00000010U
#define ETH_CMD_RX_RESET 0x00000020U
#define ETH_CMD_PASS_RUNT_FRAME 0x00000040U
#define ETH_CMD_PASS_RX_FILTER 0X00000080U
#define ETH_CMD_TX_FLOW_CONTROL 0x00000100U
#define ETH_CMD_RMII 0x00000200U
#define ETH_CMD_FULL_DUPLEX 0x00000400U

/* ETH_STAT */

#define ETH_STAT_RX_ACTIVE 0x00000001U
#define ETH_STAT_TX_ACTIVE 0x00000002U

/* ETH_MAC2 */

#define ETH_MAC2_FULL_DUPLEX BSP_BIT32(8)

/* ETH_SUPP */

#define ETH_SUPP_SPEED BSP_BIT32(8)

/* ETH_MCFG */

#define ETH_MCFG_CLOCK_SELECT(val) BSP_FLD32(val, 2, 4)

#define ETH_MCFG_RESETMIIMGMT BSP_BIT32(15)

/* ETH_MCMD */

#define ETH_MCMD_READ BSP_BIT32(0)
#define ETH_MCMD_SCAN BSP_BIT32(1)

/* ETH_MADR */

#define ETH_MADR_REG(val) BSP_FLD32(val, 0, 4)
#define ETH_MADR_PHY(val) BSP_FLD32(val, 8, 12)

/* ETH_MIND */

#define ETH_MIND_BUSY BSP_BIT32(0)
#define ETH_MIND_SCANNING BSP_BIT32(1)
#define ETH_MIND_NOT_VALID BSP_BIT32(2)
#define ETH_MIND_MII_LINK_FAIL BSP_BIT32(3)

/* Events */

#define LPC_ETH_EVENT_INITIALIZE RTEMS_EVENT_1

#define LPC_ETH_EVENT_TXSTART RTEMS_EVENT_2

#define LPC_ETH_EVENT_INTERRUPT RTEMS_EVENT_3

#define LPC_ETH_EVENT_STOP RTEMS_EVENT_4

/* Status */

#define LPC_ETH_INTERRUPT_RECEIVE \
  (ETH_INT_RX_ERROR | ETH_INT_RX_FINISHED | ETH_INT_RX_DONE)

#define LPC_ETH_INTERRUPT_TRANSMIT \
  (ETH_INT_TX_DONE | ETH_INT_TX_FINISHED | ETH_INT_TX_ERROR)

#define LPC_ETH_RX_STAT_ERRORS \
  (ETH_RX_STAT_CRC_ERROR \
    | ETH_RX_STAT_SYMBOL_ERROR \
    | ETH_RX_STAT_LENGTH_ERROR \
    | ETH_RX_STAT_ALIGNMENT_ERROR \
    | ETH_RX_STAT_OVERRUN \
    | ETH_RX_STAT_NO_DESCRIPTOR)

#define LPC_ETH_LAST_FRAGMENT_FLAGS \
  (ETH_TX_CTRL_OVERRIDE \
    | ETH_TX_CTRL_PAD \
    | ETH_TX_CTRL_CRC \
    | ETH_TX_CTRL_INTERRUPT \
    | ETH_TX_CTRL_LAST)

/* Debug */

#ifdef DEBUG
  #define LPC_ETH_PRINTF(...) printf(__VA_ARGS__)
  #define LPC_ETH_PRINTK(...) printk(__VA_ARGS__)
#else
  #define LPC_ETH_PRINTF(...)
  #define LPC_ETH_PRINTK(...)
#endif

typedef enum {
  LPC_ETH_STATE_NOT_INITIALIZED = 0,
  LPC_ETH_STATE_DOWN,
  LPC_ETH_STATE_UP
} lpc_eth_state;

typedef struct {
  struct arpcom arpcom;
  lpc_eth_state state;
  struct rtems_mdio_info mdio;
  uint32_t anlpar;
  rtems_id receive_task;
  rtems_id transmit_task;
  unsigned rx_unit_count;
  unsigned tx_unit_count;
  volatile lpc_eth_transfer_descriptor *rx_desc_table;
  volatile lpc_eth_receive_status *rx_status_table;
  struct mbuf **rx_mbuf_table;
  volatile lpc_eth_transfer_descriptor *tx_desc_table;
  volatile uint32_t *tx_status_table;
  void *tx_buf_table;
  unsigned received_frames;
  unsigned receive_interrupts;
  unsigned transmitted_frames;
  unsigned transmit_interrupts;
  unsigned receive_drop_errors;
  unsigned receive_overrun_errors;
  unsigned receive_fragment_errors;
  unsigned receive_crc_errors;
  unsigned receive_symbol_errors;
  unsigned receive_length_errors;
  unsigned receive_alignment_errors;
  unsigned receive_no_descriptor_errors;
  unsigned receive_fatal_errors;
  unsigned transmit_underrun_errors;
  unsigned transmit_late_collision_errors;
  unsigned transmit_excessive_collision_errors;
  unsigned transmit_excessive_defer_errors;
  unsigned transmit_no_descriptor_errors;
  unsigned transmit_overflow_errors;
  unsigned transmit_fatal_errors;
  uint32_t phy_id;
  int phy;
  rtems_vector_number interrupt_number;
  rtems_id control_task;
} lpc_eth_driver_entry;

static lpc_eth_driver_entry lpc_eth_driver_data;

static void lpc_eth_control_request_complete(const lpc_eth_driver_entry *e)
{
  rtems_status_code sc = rtems_event_transient_send(e->control_task);
  assert(sc == RTEMS_SUCCESSFUL);
}

static void lpc_eth_control_request(
  lpc_eth_driver_entry *e,
  rtems_id task,
  rtems_event_set event
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  uint32_t nest_count = 0;

  e->control_task = rtems_task_self();

  sc = rtems_bsdnet_event_send(task, event);
  assert(sc == RTEMS_SUCCESSFUL);

  nest_count = rtems_bsdnet_semaphore_release_recursive();
  sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  assert(sc == RTEMS_SUCCESSFUL);
  rtems_bsdnet_semaphore_obtain_recursive(nest_count);

  e->control_task = 0;
}

static inline uint32_t lpc_eth_increment(
  uint32_t value,
  uint32_t cycle
)
{
  if (value < cycle) {
    return ++value;
  } else {
    return 0;
  }
}

static void lpc_eth_enable_promiscous_mode(bool enable)
{
  if (enable) {
    lpc_eth->rxfilterctrl = ETH_RX_FIL_CTRL_ACCEPT_UNICAST
      | ETH_RX_FIL_CTRL_ACCEPT_MULTICAST
      | ETH_RX_FIL_CTRL_ACCEPT_BROADCAST;
  } else {
    lpc_eth->rxfilterctrl = ETH_RX_FIL_CTRL_ACCEPT_PERFECT
      | ETH_RX_FIL_CTRL_ACCEPT_MULTICAST_HASH
      | ETH_RX_FIL_CTRL_ACCEPT_BROADCAST;
  }
}

static void lpc_eth_interrupt_handler(void *arg)
{
  lpc_eth_driver_entry *e = (lpc_eth_driver_entry *) arg;
  rtems_event_set re = 0;
  rtems_event_set te = 0;
  uint32_t ie = 0;

  /* Get interrupt status */
  uint32_t im = lpc_eth->intenable;
  uint32_t is = lpc_eth->intstatus & im;

  /* Check receive interrupts */
  if ((is & ETH_INT_RX_OVERRUN) != 0) {
    re = LPC_ETH_EVENT_INITIALIZE;
    ++e->receive_fatal_errors;
  } else if ((is & LPC_ETH_INTERRUPT_RECEIVE) != 0) {
    re = LPC_ETH_EVENT_INTERRUPT;
    ie |= LPC_ETH_INTERRUPT_RECEIVE;
  }

  /* Send events to receive task */
  if (re != 0) {
    ++e->receive_interrupts;
    (void) rtems_bsdnet_event_send(e->receive_task, re);
  }

  /* Check transmit interrupts */
  if ((is & ETH_INT_TX_UNDERRUN) != 0) {
    te = LPC_ETH_EVENT_INITIALIZE;
    ++e->transmit_fatal_errors;
  } else if ((is & LPC_ETH_INTERRUPT_TRANSMIT) != 0) {
    te = LPC_ETH_EVENT_INTERRUPT;
    ie |= LPC_ETH_INTERRUPT_TRANSMIT;
  }

  /* Send events to transmit task */
  if (te != 0) {
    ++e->transmit_interrupts;
    (void) rtems_bsdnet_event_send(e->transmit_task, te);
  }

  LPC_ETH_PRINTK("interrupt: rx = 0x%08x, tx = 0x%08x\n", re, te);

  /* Update interrupt mask */
  lpc_eth->intenable = im & ~ie;

  /* Clear interrupts */
  lpc_eth->intclear = is;
}

static void lpc_eth_enable_receive_interrupts(void)
{
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);
  lpc_eth->intenable |= LPC_ETH_INTERRUPT_RECEIVE;
  rtems_interrupt_enable(level);
}

static void lpc_eth_disable_receive_interrupts(void)
{
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);
  lpc_eth->intenable &= ~LPC_ETH_INTERRUPT_RECEIVE;
  rtems_interrupt_enable(level);
}

static void lpc_eth_enable_transmit_interrupts(void)
{
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);
  lpc_eth->intenable |= LPC_ETH_INTERRUPT_TRANSMIT;
  rtems_interrupt_enable(level);
}

static void lpc_eth_disable_transmit_interrupts(void)
{
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);
  lpc_eth->intenable &= ~LPC_ETH_INTERRUPT_TRANSMIT;
  rtems_interrupt_enable(level);
}

#define LPC_ETH_RX_DATA_OFFSET 2

static struct mbuf *lpc_eth_new_mbuf(struct ifnet *ifp, bool wait)
{
  struct mbuf *m = NULL;
  int mw = wait ? M_WAIT : M_DONTWAIT;

  MGETHDR(m, mw, MT_DATA);
  if (m != NULL) {
    MCLGET(m, mw);
    if ((m->m_flags & M_EXT) != 0) {
      /* Set receive interface */
      m->m_pkthdr.rcvif = ifp;

      /* Adjust by two bytes for proper IP header alignment */
      m->m_data = mtod(m, char *) + LPC_ETH_RX_DATA_OFFSET;

      return m;
    } else {
      m_free(m);
    }
  }

  return NULL;
}

static bool lpc_eth_add_new_mbuf(
  struct ifnet *ifp,
  volatile lpc_eth_transfer_descriptor *desc,
  struct mbuf **mbufs,
  uint32_t i,
  bool wait
)
{
  /* New mbuf */
  struct mbuf *m = lpc_eth_new_mbuf(ifp, wait);

  /* Check mbuf */
  if (m != NULL) {
    /* Cache invalidate */
    rtems_cache_invalidate_multiple_data_lines(
      mtod(m, void *),
      MCLBYTES - LPC_ETH_RX_DATA_OFFSET
    );

    /* Add mbuf to queue */
    desc [i].start = mtod(m, uint32_t);
    desc [i].control = (MCLBYTES - LPC_ETH_RX_DATA_OFFSET - 1)
      | ETH_RX_CTRL_INTERRUPT;

    /* Cache flush of descriptor  */
    rtems_cache_flush_multiple_data_lines(
      (void *) &desc [i],
      sizeof(desc [0])
    );

    /* Add mbuf to table */
    mbufs [i] = m;

    return true;
  } else {
    return false;
  }
}

static void lpc_eth_receive_task(void *arg)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_event_set events = 0;
  lpc_eth_driver_entry *const e = (lpc_eth_driver_entry *) arg;
  struct ifnet *const ifp = &e->arpcom.ac_if;
  volatile lpc_eth_transfer_descriptor *const desc = e->rx_desc_table;
  volatile lpc_eth_receive_status *const status = e->rx_status_table;
  struct mbuf **const mbufs = e->rx_mbuf_table;
  uint32_t const index_max = e->rx_unit_count - 1;
  uint32_t produce_index = 0;
  uint32_t consume_index = 0;

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

  /* Main event loop */
  while (true) {
    /* Wait for events */
    sc = rtems_bsdnet_event_receive(
      LPC_ETH_EVENT_INITIALIZE
        | LPC_ETH_EVENT_STOP
        | LPC_ETH_EVENT_INTERRUPT,
      RTEMS_EVENT_ANY | RTEMS_WAIT,
      RTEMS_NO_TIMEOUT,
      &events
    );
    assert(sc == RTEMS_SUCCESSFUL);

    LPC_ETH_PRINTF("rx: wake up: 0x%08" PRIx32 "\n", events);

    /* Stop receiver? */
    if ((events & LPC_ETH_EVENT_STOP) != 0) {
      lpc_eth_control_request_complete(e);

      /* Wait for events */
      continue;
    }

    /* Initialize receiver? */
    if ((events & LPC_ETH_EVENT_INITIALIZE) != 0) {
      /* Disable receive interrupts */
      lpc_eth_disable_receive_interrupts();

      /* Disable receiver */
      lpc_eth->command &= ~ETH_CMD_RX_ENABLE;

      /* Wait for inactive status */
      while ((lpc_eth->status & ETH_STAT_RX_ACTIVE) != 0) {
        /* Wait */
      }

      /* Reset */
      lpc_eth->command |= ETH_CMD_RX_RESET;

      /* Clear receive interrupts */
      lpc_eth->intclear = LPC_ETH_INTERRUPT_RECEIVE;

      /* Move existing mbufs to the front */
      consume_index = 0;
      for (produce_index = 0; produce_index <= index_max; ++produce_index) {
        if (mbufs [produce_index] != NULL) {
          mbufs [consume_index] = mbufs [produce_index];
          ++consume_index;
        }
      }

      /* Fill receive queue */
      for (
        produce_index = consume_index;
        produce_index <= index_max;
        ++produce_index
      ) {
        lpc_eth_add_new_mbuf(ifp, desc, mbufs, produce_index, true);
      }

      /* Receive descriptor table */
      lpc_eth->rxdescriptornum = index_max;
      lpc_eth->rxdescriptor = (uint32_t) desc;
      lpc_eth->rxstatus = (uint32_t) status;

      /* Initialize indices */
      produce_index = lpc_eth->rxproduceindex;
      consume_index = lpc_eth->rxconsumeindex;

      /* Enable receiver */
      lpc_eth->command |= ETH_CMD_RX_ENABLE;

      /* Enable receive interrupts */
      lpc_eth_enable_receive_interrupts();

      lpc_eth_control_request_complete(e);

      /* Wait for events */
      continue;
    }

    while (true) {
      /* Clear receive interrupt status */
      lpc_eth->intclear = LPC_ETH_INTERRUPT_RECEIVE;

      /* Get current produce index */
      produce_index = lpc_eth->rxproduceindex;

      if (consume_index != produce_index) {
        uint32_t stat = 0;

        /* Fragment status */
        rtems_cache_invalidate_multiple_data_lines(
          (void *) &status [consume_index],
          sizeof(status [0])
        );
        stat = status [consume_index].info;

        if (
          (stat & ETH_RX_STAT_LAST_FLAG) != 0
            && (stat & LPC_ETH_RX_STAT_ERRORS) == 0
        ) {
          /* Received mbuf */
          struct mbuf *m = mbufs [consume_index];

          if (lpc_eth_add_new_mbuf(ifp, desc, mbufs, consume_index, false)) {
            /* Ethernet header */
            struct ether_header *eh = mtod(m, struct ether_header *);

            /* Discard Ethernet header and CRC */
            int sz = (int) (stat & ETH_RX_STAT_RXSIZE_MASK) + 1
              - ETHER_HDR_LEN - ETHER_CRC_LEN;

            /* Update mbuf */
            m->m_len = sz;
            m->m_pkthdr.len = sz;
            m->m_data = mtod(m, char *) + ETHER_HDR_LEN;

            LPC_ETH_PRINTF("rx: %02" PRIu32 ": %u\n", consume_index, sz);

            /* Hand over */
            ether_input(ifp, eh, m);

            /* Increment received frames counter */
            ++e->received_frames;
          } else {
            ++e->receive_drop_errors;
          }
        } else {
          /* Update error counters */
          if ((stat & ETH_RX_STAT_OVERRUN) != 0) {
            ++e->receive_overrun_errors;
          }
          if ((stat & ETH_RX_STAT_LAST_FLAG) == 0) {
            ++e->receive_fragment_errors;
          }
          if ((stat & ETH_RX_STAT_CRC_ERROR) != 0) {
            ++e->receive_crc_errors;
          }
          if ((stat & ETH_RX_STAT_SYMBOL_ERROR) != 0) {
            ++e->receive_symbol_errors;
          }
          if ((stat & ETH_RX_STAT_LENGTH_ERROR) != 0) {
            ++e->receive_length_errors;
          }
          if ((stat & ETH_RX_STAT_ALIGNMENT_ERROR) != 0) {
            ++e->receive_alignment_errors;
          }
          if ((stat & ETH_RX_STAT_NO_DESCRIPTOR) != 0) {
            ++e->receive_no_descriptor_errors;
          }
        }

        /* Increment and update consume index */
        consume_index = lpc_eth_increment(consume_index, index_max);
        lpc_eth->rxconsumeindex = consume_index;
      } else {
        /* Nothing to do, enable receive interrupts */
        lpc_eth_enable_receive_interrupts();
        break;
      }
    }
  }
}

static struct mbuf *lpc_eth_next_fragment(
  struct ifnet *ifp,
  struct mbuf *m,
  uint32_t *ctrl
)
{
  struct mbuf *n = NULL;
  int size = 0;

  while (true) {
    if (m == NULL) {
      /* Dequeue first fragment of the next frame */
      IF_DEQUEUE(&ifp->if_snd, m);

      /* Empty queue? */
      if (m == NULL) {
        return m;
      }
    }

    /* Get fragment size */
    size = m->m_len;

    if (size > 0) {
      /* Now we have a not empty fragment */
      break;
    } else {
      /* Discard empty fragments */
      m = m_free(m);
    }
  }

  /* Set fragment size */
  *ctrl = (uint32_t) (size - 1);

  /* Discard empty successive fragments */
  n = m->m_next;
  while (n != NULL && n->m_len <= 0) {
    n = m_free(n);
  }
  m->m_next = n;

  /* Is our fragment the last in the frame? */
  if (n == NULL) {
    *ctrl |= LPC_ETH_LAST_FRAGMENT_FLAGS;
  }

  return m;
}

static void lpc_eth_transmit_task(void *arg)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_event_set events = 0;
  lpc_eth_driver_entry *e = (lpc_eth_driver_entry *) arg;
  struct ifnet *ifp = &e->arpcom.ac_if;
  volatile lpc_eth_transfer_descriptor *const desc = e->tx_desc_table;
  volatile uint32_t *const status = e->tx_status_table;
  #ifdef LPC_ETH_CONFIG_USE_TRANSMIT_DMA
    struct mbuf **const mbufs = e->tx_buf_table;
  #else
    char *const buf = e->tx_buf_table;
  #endif
  struct mbuf *m = NULL;
  uint32_t const index_max = e->tx_unit_count - 1;
  uint32_t produce_index = 0;
  uint32_t consume_index = 0;
  uint32_t ctrl = 0;
  #ifndef LPC_ETH_CONFIG_USE_TRANSMIT_DMA
    uint32_t frame_length = 0;
    char *frame_buffer = NULL;
  #endif

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

  #ifndef LPC_ETH_CONFIG_USE_TRANSMIT_DMA
    /* Initialize descriptor table */
    for (produce_index = 0; produce_index <= index_max; ++produce_index) {
      desc [produce_index].start =
        (uint32_t) (buf + produce_index * LPC_ETH_CONFIG_TX_BUF_SIZE);
    }
  #endif

  /* Main event loop */
  while (true) {
    /* Wait for events */
    sc = rtems_bsdnet_event_receive(
      LPC_ETH_EVENT_INITIALIZE
        | LPC_ETH_EVENT_STOP
        | LPC_ETH_EVENT_TXSTART
        | LPC_ETH_EVENT_INTERRUPT,
      RTEMS_EVENT_ANY | RTEMS_WAIT,
      RTEMS_NO_TIMEOUT,
      &events
    );
    assert(sc == RTEMS_SUCCESSFUL);

    LPC_ETH_PRINTF("tx: wake up: 0x%08" PRIx32 "\n", events);

    /* Stop transmitter? */
    if ((events & LPC_ETH_EVENT_STOP) != 0) {
      lpc_eth_control_request_complete(e);

      /* Wait for events */
      continue;
    }

    /* Initialize transmitter? */
    if ((events & LPC_ETH_EVENT_INITIALIZE) != 0) {
      /* Disable transmit interrupts */
      lpc_eth_disable_transmit_interrupts();

      /* Disable transmitter */
      lpc_eth->command &= ~ETH_CMD_TX_ENABLE;

      /* Wait for inactive status */
      while ((lpc_eth->status & ETH_STAT_TX_ACTIVE) != 0) {
        /* Wait */
      }

      /* Reset */
      lpc_eth->command |= ETH_CMD_TX_RESET;

      /* Clear transmit interrupts */
      lpc_eth->intclear = LPC_ETH_INTERRUPT_TRANSMIT;

      /* Transmit descriptors */
      lpc_eth->txdescriptornum = index_max;
      lpc_eth->txdescriptor = (uint32_t) desc;
      lpc_eth->txstatus = (uint32_t) status;

      #ifdef LPC_ETH_CONFIG_USE_TRANSMIT_DMA
        /* Discard outstanding fragments (= data loss) */
        for (produce_index = 0; produce_index <= index_max; ++produce_index) {
          struct mbuf *victim = mbufs [produce_index];

          if (victim != NULL) {
            m_free(victim);
            mbufs [produce_index] = NULL;
          }
        }
      #endif

      /* Initialize indices */
      produce_index = lpc_eth->txproduceindex;
      consume_index = lpc_eth->txconsumeindex;

      #ifndef LPC_ETH_CONFIG_USE_TRANSMIT_DMA
        /* Fresh frame length and buffer start */
        frame_length = 0;
        frame_buffer = (char *) desc [produce_index].start;
      #endif

      /* Enable transmitter */
      lpc_eth->command |= ETH_CMD_TX_ENABLE;

      lpc_eth_control_request_complete(e);
    }

    /* Free consumed fragments */
    while (true) {
      /* Save last known consume index */
      uint32_t c = consume_index;

      /* Clear transmit interrupt status */
      lpc_eth->intclear = LPC_ETH_INTERRUPT_TRANSMIT;

      /* Get new consume index */
      consume_index = lpc_eth->txconsumeindex;

      /* Nothing consumed in the meantime? */
      if (c == consume_index) {
        break;
      }

      while (c != consume_index) {
        uint32_t s = status [c];

        /* Update error counters */
        if ((s & (ETH_TX_STAT_ERROR | ETH_TX_STAT_NO_DESCRIPTOR)) != 0) {
          if ((s & ETH_TX_STAT_UNDERRUN) != 0) {
            ++e->transmit_underrun_errors;
          }
          if ((s & ETH_TX_STAT_LATE_COLLISION) != 0) {
            ++e->transmit_late_collision_errors;
          }
          if ((s & ETH_TX_STAT_EXCESSIVE_COLLISION) != 0) {
            ++e->transmit_excessive_collision_errors;
          }
          if ((s & ETH_TX_STAT_EXCESSIVE_DEFER) != 0) {
            ++e->transmit_excessive_defer_errors;
          }
          if ((s & ETH_TX_STAT_NO_DESCRIPTOR) != 0) {
            ++e->transmit_no_descriptor_errors;
          }
        }

        #ifdef LPC_ETH_CONFIG_USE_TRANSMIT_DMA
          /* Release mbuf */
          m_free(mbufs [c]);
          mbufs [c] = NULL;
        #endif

        /* Next consume index */
        c = lpc_eth_increment(c, index_max);
      }
    }

    /* Transmit new fragments */
    while (true) {
      /* Compute next produce index */
      uint32_t p = lpc_eth_increment(produce_index, index_max);

      /* Get next fragment and control value */
      m = lpc_eth_next_fragment(ifp, m, &ctrl);

      /* Queue full? */
      if (p == consume_index) {
        LPC_ETH_PRINTF("tx: full queue: 0x%08x\n", m);

        /* The queue is full, wait for transmit interrupt */
        break;
      }

      /* New fragment? */
      if (m != NULL) {
        #ifdef LPC_ETH_CONFIG_USE_TRANSMIT_DMA
          /* Set the transfer data */
          rtems_cache_flush_multiple_data_lines(
            mtod(m, const void *),
            (size_t) m->m_len
          );
          desc [produce_index].start = mtod(m, uint32_t);
          desc [produce_index].control = ctrl;
          rtems_cache_flush_multiple_data_lines(
            (void *) &desc [produce_index],
            sizeof(desc [0])
           );
          mbufs [produce_index] = m;

          LPC_ETH_PRINTF(
            "tx: %02" PRIu32 ": %u %s\n",
            produce_index, m->m_len,
            (ctrl & ETH_TX_CTRL_LAST) != 0 ? "L" : ""
          );

          /* Next produce index */
          produce_index = p;

          /* Last fragment of a frame? */
          if ((ctrl & ETH_TX_CTRL_LAST) != 0) {
            /* Update the produce index */
            lpc_eth->txproduceindex = produce_index;

            /* Increment transmitted frames counter */
            ++e->transmitted_frames;
          }

          /* Next fragment of the frame */
          m = m->m_next;
        #else
          size_t fragment_length = (size_t) m->m_len;
          void *fragment_start = mtod(m, void *);
          uint32_t new_frame_length = frame_length + fragment_length;

          /* Check buffer size */
          if (new_frame_length > LPC_ETH_CONFIG_TX_BUF_SIZE) {
            LPC_ETH_PRINTF("tx: overflow\n");

            /* Discard overflow data */
            new_frame_length = LPC_ETH_CONFIG_TX_BUF_SIZE;
            fragment_length = new_frame_length - frame_length;

            /* Finalize frame */
            ctrl |= LPC_ETH_LAST_FRAGMENT_FLAGS;

            /* Update error counter */
            ++e->transmit_overflow_errors;
          }

          LPC_ETH_PRINTF(
            "tx: copy: %" PRIu32 "%s%s\n",
            fragment_length,
            (m->m_flags & M_EXT) != 0 ? ", E" : "",
            (m->m_flags & M_PKTHDR) != 0 ? ", H" : ""
          );

          /* Copy fragment to buffer in Ethernet RAM */
          memcpy(frame_buffer, fragment_start, fragment_length);

          if ((ctrl & ETH_TX_CTRL_LAST) != 0) {
            /* Finalize descriptor */
            desc [produce_index].control = (ctrl & ~ETH_TX_CTRL_SIZE_MASK)
              | (new_frame_length - 1);

            LPC_ETH_PRINTF(
              "tx: %02" PRIu32 ": %" PRIu32 "\n",
              produce_index,
              new_frame_length
            );

            /* Cache flush of data */
            rtems_cache_flush_multiple_data_lines(
              (const void *) desc [produce_index].start,
              new_frame_length
            );

            /* Cache flush of descriptor  */
            rtems_cache_flush_multiple_data_lines(
              (void *) &desc [produce_index],
              sizeof(desc [0])
            );

            /* Next produce index */
            produce_index = p;

            /* Update the produce index */
            lpc_eth->txproduceindex = produce_index;

            /* Fresh frame length and buffer start */
            frame_length = 0;
            frame_buffer = (char *) desc [produce_index].start;

            /* Increment transmitted frames counter */
            ++e->transmitted_frames;
          } else {
            /* New frame length */
            frame_length = new_frame_length;

            /* Update current frame buffer start */
            frame_buffer += fragment_length;
          }

          /* Free mbuf and get next */
          m = m_free(m);
        #endif
      } else {
        /* Nothing to transmit */
        break;
      }
    }

    /* No more fragments? */
    if (m == NULL) {
      /* Interface is now inactive */
      ifp->if_flags &= ~IFF_OACTIVE;
    } else {
      LPC_ETH_PRINTF("tx: enable interrupts\n");

      /* Enable transmit interrupts */
      lpc_eth_enable_transmit_interrupts();
    }
  }
}

static int lpc_eth_mdio_wait_for_not_busy(void)
{
  rtems_interval one_second = rtems_clock_get_ticks_per_second();
  rtems_interval i = 0;

  while ((lpc_eth->mind & ETH_MIND_BUSY) != 0 && i < one_second) {
    rtems_task_wake_after(1);
    ++i;
  }

  LPC_ETH_PRINTK("tx: lpc_eth_mdio_wait %s after %d\n",
                 i != one_second? "succeed": "timeout", i);

  return i != one_second ? 0 : ETIMEDOUT;
}

static uint32_t lpc_eth_mdio_read_anlpar(int phy)
{
  uint32_t madr = ETH_MADR_REG(MII_ANLPAR) | ETH_MADR_PHY(phy);
  uint32_t anlpar = 0;
  int eno = 0;

  if (lpc_eth->madr != madr) {
    lpc_eth->madr = madr;
  }

  if (lpc_eth->mcmd != ETH_MCMD_READ) {
    lpc_eth->mcmd = 0;
    lpc_eth->mcmd = ETH_MCMD_READ;
  }

  eno = lpc_eth_mdio_wait_for_not_busy();
  if (eno == 0) {
    anlpar = lpc_eth->mrdd;
  }

  /* Start next read */
  lpc_eth->mcmd = 0;
  lpc_eth->mcmd = ETH_MCMD_READ;

  return anlpar;
}

static int lpc_eth_mdio_read(
  int phy,
  void *arg __attribute__((unused)),
  unsigned reg,
  uint32_t *val
)
{
  int eno = 0;

  if (0 <= phy && phy <= 31) {
    lpc_eth->madr = ETH_MADR_REG(reg) | ETH_MADR_PHY(phy);
    lpc_eth->mcmd = 0;
    lpc_eth->mcmd = ETH_MCMD_READ;
    eno = lpc_eth_mdio_wait_for_not_busy();

    if (eno == 0) {
      *val = lpc_eth->mrdd;
    }
  } else {
    eno = EINVAL;
  }

  return eno;
}

static int lpc_eth_mdio_write(
  int phy,
  void *arg __attribute__((unused)),
  unsigned reg,
  uint32_t val
)
{
  int eno = 0;

  if (0 <= phy && phy <= 31) {
    lpc_eth->madr = ETH_MADR_REG(reg) | ETH_MADR_PHY(phy);
    lpc_eth->mwtd = val;
    eno = lpc_eth_mdio_wait_for_not_busy();
  } else {
    eno = EINVAL;
  }

  return eno;
}

static int lpc_eth_phy_get_id(int phy, uint32_t *id)
{
  uint32_t id1 = 0;
  int eno = lpc_eth_mdio_read(phy, NULL, MII_PHYIDR1, &id1);

  if (eno == 0) {
    uint32_t id2 = 0;

    eno = lpc_eth_mdio_read(phy, NULL, MII_PHYIDR2, &id2);
    if (eno == 0) {
      *id = (id1 << 16) | (id2 & 0xfff0);
    }
  }

  return eno;
}

#define PHY_KSZ80X1RNL 0x221550
#define PHY_DP83848    0x20005c90

typedef struct {
  unsigned reg;
  uint32_t set;
  uint32_t clear;
} lpc_eth_phy_action;

static int lpc_eth_phy_set_and_clear(
  lpc_eth_driver_entry *e,
  const lpc_eth_phy_action *actions,
  size_t n
)
{
  int eno = 0;
  size_t i;

  for (i = 0; eno == 0 && i < n; ++i) {
    const lpc_eth_phy_action *action = &actions [i];
    uint32_t val;

    eno = lpc_eth_mdio_read(e->phy, NULL, action->reg, &val);
    if (eno == 0) {
      val |= action->set;
      val &= ~action->clear;
      eno = lpc_eth_mdio_write(e->phy, NULL, action->reg, val);
    }
  }

  return eno;
}

static const lpc_eth_phy_action lpc_eth_phy_up_action_default [] = {
  { MII_BMCR, 0, BMCR_PDOWN },
  { MII_BMCR, BMCR_RESET, 0 },
  { MII_BMCR, BMCR_AUTOEN, 0 }
};

static const lpc_eth_phy_action lpc_eth_phy_up_pre_action_KSZ80X1RNL [] = {
  /* Disable slow oscillator mode */
  { 0x11, 0, 0x10 }
};

static const lpc_eth_phy_action lpc_eth_phy_up_post_action_KSZ80X1RNL [] = {
  /* Enable energy detect power down (EDPD) mode */
  { 0x18, 0x0800, 0 },
  /* Turn PLL of automatically in EDPD mode */
  { 0x10, 0x10, 0 }
};

static int lpc_eth_phy_up(lpc_eth_driver_entry *e)
{
  int eno;
  int retries = 64;
  uint32_t val;

  e->phy = DEFAULT_PHY - 1;
  while (true) {
    e->phy = (e->phy + 1) % 32;

    --retries;
    eno = lpc_eth_phy_get_id(e->phy, &e->phy_id);
    if (
      (eno == 0 && e->phy_id != 0xfffffff0 && e->phy_id != 0)
        || retries <= 0
    ) {
      break;
    }

    rtems_task_wake_after(1);
  }

  LPC_ETH_PRINTF("lpc_eth_phy_get_id: 0x%08" PRIx32 " from phy %d retries %d\n",
                 e->phy_id, e->phy, retries);

  if (eno == 0) {
    switch (e->phy_id) {
      case PHY_KSZ80X1RNL:
        eno = lpc_eth_phy_set_and_clear(
          e,
          &lpc_eth_phy_up_pre_action_KSZ80X1RNL [0],
          RTEMS_ARRAY_SIZE(lpc_eth_phy_up_pre_action_KSZ80X1RNL)
        );
        break;
      case PHY_DP83848:
        eno = lpc_eth_mdio_read(e->phy, NULL, 0x17, &val);
        LPC_ETH_PRINTF("phy PHY_DP83848 RBR 0x%08" PRIx32 "\n", val);
        /* val = 0x21; */
        val = 0x32 ;
        eno = lpc_eth_mdio_write(e->phy, NULL, 0x17, val);
        break;
      case 0:
      case 0xfffffff0:
        eno = EIO;
        e->phy = DEFAULT_PHY;
        break;
      default:
        break;
    }

    if (eno == 0) {
      eno = lpc_eth_phy_set_and_clear(
        e,
        &lpc_eth_phy_up_action_default [0],
        RTEMS_ARRAY_SIZE(lpc_eth_phy_up_action_default)
      );
    }

    if (eno == 0) {
      switch (e->phy_id) {
        case PHY_KSZ80X1RNL:
          eno = lpc_eth_phy_set_and_clear(
            e,
            &lpc_eth_phy_up_post_action_KSZ80X1RNL [0],
            RTEMS_ARRAY_SIZE(lpc_eth_phy_up_post_action_KSZ80X1RNL)
          );
          break;
        default:
          break;
      }
    }
  } else {
    e->phy_id = 0;
  }

  return eno;
}

static const lpc_eth_phy_action lpc_eth_phy_down_action_default [] = {
  { MII_BMCR, BMCR_PDOWN, 0 }
};

static const lpc_eth_phy_action lpc_eth_phy_down_post_action_KSZ80X1RNL [] = {
  /* Enable slow oscillator mode */
  { 0x11, 0x10, 0 }
};

static void lpc_eth_phy_down(lpc_eth_driver_entry *e)
{
  int eno = lpc_eth_phy_set_and_clear(
    e,
    &lpc_eth_phy_down_action_default [0],
    RTEMS_ARRAY_SIZE(lpc_eth_phy_down_action_default)
  );

  if (eno == 0) {
    switch (e->phy_id) {
      case PHY_KSZ80X1RNL:
        eno = lpc_eth_phy_set_and_clear(
          e,
          &lpc_eth_phy_down_post_action_KSZ80X1RNL [0],
          RTEMS_ARRAY_SIZE(lpc_eth_phy_down_post_action_KSZ80X1RNL)
        );
        break;
      default:
        break;
    }
  }
}

static void lpc_eth_soft_reset(void)
{
  lpc_eth->command = 0x38;
  lpc_eth->mac1 = 0xcf00;
  lpc_eth->mac1 = 0x0;
}

static int lpc_eth_up_or_down(lpc_eth_driver_entry *e, bool up)
{
  int eno = 0;
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  struct ifnet *ifp = &e->arpcom.ac_if;

  if (up && e->state == LPC_ETH_STATE_DOWN) {

    lpc_eth_config_module_enable();

    /* Enable RX/TX reset and disable soft reset */
    lpc_eth->mac1 = 0xf00;

    /* Initialize PHY */
    /* Clock value 10 (divide by 44 ) is safe on LPC178x up to 100 MHz AHB clock */
    lpc_eth->mcfg = ETH_MCFG_CLOCK_SELECT(10) | ETH_MCFG_RESETMIIMGMT;
    rtems_task_wake_after(1);
    lpc_eth->mcfg = ETH_MCFG_CLOCK_SELECT(10);
    rtems_task_wake_after(1);
    eno = lpc_eth_phy_up(e);

    if (eno == 0) {
      /*
       * We must have a valid external clock from the PHY at this point,
       * otherwise the system bus hangs and only a watchdog reset helps.
       */
      lpc_eth_soft_reset();

      /* Reinitialize registers */
      lpc_eth->mac2 = 0x31;
      lpc_eth->ipgt = 0x15;
      lpc_eth->ipgr = 0x12;
      lpc_eth->clrt = 0x370f;
      lpc_eth->maxf = 0x0600;
      lpc_eth->supp = ETH_SUPP_SPEED;
      lpc_eth->test = 0;
      #ifdef LPC_ETH_CONFIG_RMII
        lpc_eth->command = 0x0600;
      #else
        lpc_eth->command = 0x0400;
      #endif
      lpc_eth->intenable = ETH_INT_RX_OVERRUN | ETH_INT_TX_UNDERRUN;
      lpc_eth->intclear = 0x30ff;
      lpc_eth->powerdown = 0;

      /* MAC address */
      lpc_eth->sa0 = ((uint32_t) e->arpcom.ac_enaddr [5] << 8)
        | (uint32_t) e->arpcom.ac_enaddr [4];
      lpc_eth->sa1 = ((uint32_t) e->arpcom.ac_enaddr [3] << 8)
        | (uint32_t) e->arpcom.ac_enaddr [2];
      lpc_eth->sa2 = ((uint32_t) e->arpcom.ac_enaddr [1] << 8)
        | (uint32_t) e->arpcom.ac_enaddr [0];

      /* Enable receiver */
      lpc_eth->mac1 = 0x03;

      /* Initialize tasks */
      lpc_eth_control_request(e, e->receive_task, LPC_ETH_EVENT_INITIALIZE);
      lpc_eth_control_request(e, e->transmit_task, LPC_ETH_EVENT_INITIALIZE);

      /* Install interrupt handler */
      sc = rtems_interrupt_handler_install(
        e->interrupt_number,
        "Ethernet",
        RTEMS_INTERRUPT_UNIQUE,
        lpc_eth_interrupt_handler,
        e
      );
      assert(sc == RTEMS_SUCCESSFUL);

      /* Start watchdog timer */
      ifp->if_timer = 1;

      /* Change state */
      e->state = LPC_ETH_STATE_UP;
    }

    if (eno != 0) {
      ifp->if_flags &= ~IFF_UP;
    }
  } else if (!up && e->state == LPC_ETH_STATE_UP) {
    /* Remove interrupt handler */
    sc = rtems_interrupt_handler_remove(
      e->interrupt_number,
      lpc_eth_interrupt_handler,
      e
    );
    assert(sc == RTEMS_SUCCESSFUL);

    /* Stop tasks */
    lpc_eth_control_request(e, e->receive_task, LPC_ETH_EVENT_STOP);
    lpc_eth_control_request(e, e->transmit_task, LPC_ETH_EVENT_STOP);

    lpc_eth_soft_reset();
    lpc_eth_phy_down(e);
    lpc_eth_config_module_disable();

    /* Stop watchdog timer */
    ifp->if_timer = 0;

    /* Change state */
    e->state = LPC_ETH_STATE_DOWN;
  }

  return eno;
}

static void lpc_eth_interface_init(void *arg)
{
  /* Nothing to do */
}

static void lpc_eth_interface_stats(lpc_eth_driver_entry *e)
{
  int eno = EIO;
  int media = 0;

  if (e->state == LPC_ETH_STATE_UP) {
    media = IFM_MAKEWORD(0, 0, 0, 0);
    eno = rtems_mii_ioctl(&e->mdio, e, SIOCGIFMEDIA, &media);
  }

  rtems_bsdnet_semaphore_release();

  if (eno == 0) {
    rtems_ifmedia2str(media, NULL, 0);
    printf("\n");
  }

  printf("received frames:                     %u\n", e->received_frames);
  printf("receive interrupts:                  %u\n", e->receive_interrupts);
  printf("transmitted frames:                  %u\n", e->transmitted_frames);
  printf("transmit interrupts:                 %u\n", e->transmit_interrupts);
  printf("receive drop errors:                 %u\n", e->receive_drop_errors);
  printf("receive overrun errors:              %u\n", e->receive_overrun_errors);
  printf("receive fragment errors:             %u\n", e->receive_fragment_errors);
  printf("receive CRC errors:                  %u\n", e->receive_crc_errors);
  printf("receive symbol errors:               %u\n", e->receive_symbol_errors);
  printf("receive length errors:               %u\n", e->receive_length_errors);
  printf("receive alignment errors:            %u\n", e->receive_alignment_errors);
  printf("receive no descriptor errors:        %u\n", e->receive_no_descriptor_errors);
  printf("receive fatal errors:                %u\n", e->receive_fatal_errors);
  printf("transmit underrun errors:            %u\n", e->transmit_underrun_errors);
  printf("transmit late collision errors:      %u\n", e->transmit_late_collision_errors);
  printf("transmit excessive collision errors: %u\n", e->transmit_excessive_collision_errors);
  printf("transmit excessive defer errors:     %u\n", e->transmit_excessive_defer_errors);
  printf("transmit no descriptor errors:       %u\n", e->transmit_no_descriptor_errors);
  printf("transmit overflow errors:            %u\n", e->transmit_overflow_errors);
  printf("transmit fatal errors:               %u\n", e->transmit_fatal_errors);

  rtems_bsdnet_semaphore_obtain();
}

static int lpc_eth_multicast_control(
  bool add,
  struct ifreq *ifr,
  struct arpcom *ac
)
{
  int eno = 0;

  if (add) {
    eno = ether_addmulti(ifr, ac);
  } else {
    eno = ether_delmulti(ifr, ac);
  }

  if (eno == ENETRESET) {
    struct ether_multistep step;
    struct ether_multi *enm;

    eno = 0;

    lpc_eth->hashfilterl = 0;
    lpc_eth->hashfilterh = 0;

    ETHER_FIRST_MULTI(step, ac, enm);
    while (enm != NULL) {
      uint64_t addrlo = 0;
      uint64_t addrhi = 0;

      memcpy(&addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
      memcpy(&addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
      while (addrlo <= addrhi) {
        /* XXX: ether_crc32_le() does not work, why? */
        uint32_t crc = ether_crc32_be((uint8_t *) &addrlo, ETHER_ADDR_LEN);
        uint32_t index = (crc >> 23) & 0x3f;

        if (index < 32) {
          lpc_eth->hashfilterl |= 1U << index;
        } else {
          lpc_eth->hashfilterh |= 1U << (index - 32);
        }
        ++addrlo;
      }
      ETHER_NEXT_MULTI(step, enm);
    }
  }

  return eno;
}

static int lpc_eth_interface_ioctl(
  struct ifnet *ifp,
  ioctl_command_t cmd,
  caddr_t data
)
{
  lpc_eth_driver_entry *e = (lpc_eth_driver_entry *) ifp->if_softc;
  struct ifreq *ifr = (struct ifreq *) data;
  int eno = 0;

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

  switch (cmd)  {
    case SIOCGIFMEDIA:
    case SIOCSIFMEDIA:
      rtems_mii_ioctl(&e->mdio, e, cmd, &ifr->ifr_media);
      break;
    case SIOCGIFADDR:
    case SIOCSIFADDR:
      ether_ioctl(ifp, cmd, data);
      break;
    case SIOCSIFFLAGS:
      eno = lpc_eth_up_or_down(e, (ifp->if_flags & IFF_UP) != 0);
      if (eno == 0 && (ifp->if_flags & IFF_UP) != 0) {
        lpc_eth_enable_promiscous_mode((ifp->if_flags & IFF_PROMISC) != 0);
      }
      break;
    case SIOCADDMULTI:
    case SIOCDELMULTI:
      eno = lpc_eth_multicast_control(cmd == SIOCADDMULTI, ifr, &e->arpcom);
      break;
    case SIO_RTEMS_SHOW_STATS:
      lpc_eth_interface_stats(e);
      break;
    default:
      eno = EINVAL;
      break;
  }

  return eno;
}

static void lpc_eth_interface_start(struct ifnet *ifp)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  lpc_eth_driver_entry *e = (lpc_eth_driver_entry *) ifp->if_softc;

  ifp->if_flags |= IFF_OACTIVE;

  if (e->state == LPC_ETH_STATE_UP) {
    sc = rtems_bsdnet_event_send(e->transmit_task, LPC_ETH_EVENT_TXSTART);
    assert(sc == RTEMS_SUCCESSFUL);
  }
}

static void lpc_eth_interface_watchdog(struct ifnet *ifp)
{
  lpc_eth_driver_entry *e = (lpc_eth_driver_entry *) ifp->if_softc;

  if (e->state == LPC_ETH_STATE_UP) {
    uint32_t anlpar = lpc_eth_mdio_read_anlpar(e->phy);

    if (e->anlpar != anlpar) {
      bool full_duplex = false;
      bool speed = false;

      e->anlpar = anlpar;

      if ((anlpar & ANLPAR_TX_FD) != 0) {
        full_duplex = true;
        speed = true;
      } else if ((anlpar & ANLPAR_T4) != 0) {
        speed = true;
      } else if ((anlpar & ANLPAR_TX) != 0) {
        speed = true;
      } else if ((anlpar & ANLPAR_10_FD) != 0) {
        full_duplex = true;
      }

      if (full_duplex) {
        lpc_eth->mac2 |= ETH_MAC2_FULL_DUPLEX;
      } else {
        lpc_eth->mac2 &= ~ETH_MAC2_FULL_DUPLEX;
      }

      if (speed) {
        lpc_eth->supp |= ETH_SUPP_SPEED;
      } else {
        lpc_eth->supp &= ~ETH_SUPP_SPEED;
      }
    }

    ifp->if_timer = WATCHDOG_TIMEOUT;
  }
}

static unsigned lpc_eth_fixup_unit_count(int count, int default_value, int max)
{
  if (count <= 0) {
    count = default_value;
  } else if (count > max) {
    count = max;
  }

  return LPC_ETH_CONFIG_UNIT_MULTIPLE
    + (((unsigned) count - 1U) & ~(LPC_ETH_CONFIG_UNIT_MULTIPLE - 1U));
}

static int lpc_eth_attach(struct rtems_bsdnet_ifconfig *config)
{
  lpc_eth_driver_entry *e = &lpc_eth_driver_data;
  struct ifnet *ifp = &e->arpcom.ac_if;
  char *unit_name = NULL;
  int unit_index = rtems_bsdnet_parse_driver_name(config, &unit_name);
  size_t table_area_size = 0;
  char *table_area = NULL;
  char *table_location = NULL;

  /* Check parameter */
  if (unit_index < 0) {
    return 0;
  }
  if (unit_index != 0) {
    goto cleanup;
  }
  if (config->hardware_address == NULL) {
    goto cleanup;
  }
  if (e->state != LPC_ETH_STATE_NOT_INITIALIZED) {
    goto cleanup;
  }

  /* MDIO */
  e->mdio.mdio_r = lpc_eth_mdio_read;
  e->mdio.mdio_w = lpc_eth_mdio_write;
  e->mdio.has_gmii = 0;
  e->anlpar = 0;

  /* Interrupt number */
  config->irno = LPC_ETH_CONFIG_INTERRUPT;

  /* Device control */
  config->drv_ctrl = e;

  /* Receive unit count */
  e->rx_unit_count = lpc_eth_fixup_unit_count(
    config->rbuf_count,
    LPC_ETH_CONFIG_RX_UNIT_COUNT_DEFAULT,
    LPC_ETH_CONFIG_RX_UNIT_COUNT_MAX
  );
  config->rbuf_count = (int) e->rx_unit_count;

  /* Transmit unit count */
  e->tx_unit_count = lpc_eth_fixup_unit_count(
    config->xbuf_count,
    LPC_ETH_CONFIG_TX_UNIT_COUNT_DEFAULT,
    LPC_ETH_CONFIG_TX_UNIT_COUNT_MAX
  );
  config->xbuf_count = (int) e->tx_unit_count;

  /* Remember interrupt number */
  e->interrupt_number = config->irno;

  /* Copy MAC address */
  memcpy(e->arpcom.ac_enaddr, config->hardware_address, ETHER_ADDR_LEN);

  /* Allocate and clear table area */
  table_area_size =
    e->rx_unit_count
      * (sizeof(lpc_eth_transfer_descriptor)
        + sizeof(lpc_eth_receive_status)
        + sizeof(struct mbuf *))
    + e->tx_unit_count
      * (sizeof(lpc_eth_transfer_descriptor)
        + sizeof(uint32_t)
        + LPC_ETH_CONFIG_TX_BUF_SIZE);
  table_area = lpc_eth_config_alloc_table_area(table_area_size);
  if (table_area == NULL) {
    goto cleanup;
  }
  memset(table_area, 0, table_area_size);

  table_location = table_area;

  /*
   * The receive status table must be the first one since it has the strictest
   * alignment requirements.
   */
  e->rx_status_table = (volatile lpc_eth_receive_status *) table_location;
  table_location += e->rx_unit_count * sizeof(e->rx_status_table [0]);

  e->rx_desc_table = (volatile lpc_eth_transfer_descriptor *) table_location;
  table_location += e->rx_unit_count * sizeof(e->rx_desc_table [0]);

  e->rx_mbuf_table = (struct mbuf **) table_location;
  table_location += e->rx_unit_count * sizeof(e->rx_mbuf_table [0]);

  e->tx_desc_table = (volatile lpc_eth_transfer_descriptor *) table_location;
  table_location += e->tx_unit_count * sizeof(e->tx_desc_table [0]);

  e->tx_status_table = (volatile uint32_t *) table_location;
  table_location += e->tx_unit_count * sizeof(e->tx_status_table [0]);

  e->tx_buf_table = table_location;

  /* Set interface data */
  ifp->if_softc = e;
  ifp->if_unit = (short) unit_index;
  ifp->if_name = unit_name;
  ifp->if_mtu = (config->mtu > 0) ? (u_long) config->mtu : ETHERMTU;
  ifp->if_init = lpc_eth_interface_init;
  ifp->if_ioctl = lpc_eth_interface_ioctl;
  ifp->if_start = lpc_eth_interface_start;
  ifp->if_output = ether_output;
  ifp->if_watchdog = lpc_eth_interface_watchdog;
  ifp->if_flags = IFF_MULTICAST | IFF_BROADCAST | IFF_SIMPLEX;
  ifp->if_snd.ifq_maxlen = ifqmaxlen;
  ifp->if_timer = 0;

  /* Create tasks */
  e->receive_task = rtems_bsdnet_newproc(
    "ntrx",
    4096,
    lpc_eth_receive_task,
    e
  );
  e->transmit_task = rtems_bsdnet_newproc(
    "nttx",
    4096,
    lpc_eth_transmit_task,
    e
  );

  /* Change status */
  ifp->if_flags |= IFF_RUNNING;
  e->state = LPC_ETH_STATE_DOWN;

  /* Attach the interface */
  if_attach(ifp);
  ether_ifattach(ifp);

  return 1;

cleanup:

  lpc_eth_config_free_table_area(table_area);

  /* FIXME: Type */
  free(unit_name, (int) 0xdeadbeef);

  return 0;
}

static int lpc_eth_detach(
  struct rtems_bsdnet_ifconfig *config __attribute__((unused))
)
{
  /* FIXME: Detach the interface from the upper layers? */

  /* Module soft reset */
  lpc_eth->command = 0x38;
  lpc_eth->mac1 = 0xcf00;

  /* FIXME: More cleanup */

  return 0;
}

int lpc_eth_attach_detach(
  struct rtems_bsdnet_ifconfig *config,
  int attaching
)
{
  /* FIXME: Return value */

  if (attaching) {
    return lpc_eth_attach(config);
  } else {
    return lpc_eth_detach(config);
  }
}