summaryrefslogtreecommitdiffstats
path: root/c/src/libchip/network/dwmac-desc-enh.c
blob: cb20ba43b88cd0f619f993d51b606248db3caccb (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
/**
 * @file
 *
 * @brief DWMAC 10/100/1000 Enhanced DMA Descriptor Handling.
 *
 * DWMAC 10/100/1000 on-chip Ethernet controllers.
 * Functions and data for the handling of enhanced DMA descriptors.
 */

/*
 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  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.org/license/LICENSE.
 */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include "dwmac-common.h"
#include "dwmac-desc-com.h"
#include "dwmac-core.h"
#include <sys/queue.h>

#undef DWMAC_DESC_ENH_DEBUG
#ifdef DWMAC_DESC_ENH_DEBUG
#define DWMAC_DESC_ENH_PRINT_DBG( fmt, args ... )  printk( fmt, ## args )
#else
#define DWMAC_DESC_ENH_PRINT_DBG( fmt, args ... )  do { } while ( 0 )
#endif

typedef enum {
  DWMAC_IP_PAYLOAD_TYPE_UNKNOWN,
  DWMAC_IP_PAYLOAD_TYPE_UDP,
  DWMAC_IP_PAYLOAD_TYPE_TCP,
  DWMAC_IP_PAYLOAD_TYPE_ICMP
} dwmac_ip_payload_type;

static void dwmac_desc_enh_rx_set_on_ring_chain(
  volatile dwmac_desc_ext *p, int end )
{
  /* For simplicity reasons we will not use the second buffer.
   * If we would use it we would have to set the size to MCLBYTES -1 */
  p->erx.des0_3.des1 = DWMAC_DESC_ERX_DES1_RECEIVE_BUFF_2_SIZE_SET(
    p->erx.des0_3.des1, 0
    );
  p->erx.des0_3.des3 = (uint32_t) NULL;

  if ( end )
    p->erx.des0_3.des1 |= DWMAC_DESC_ERX_DES1_RECEIVE_END_OF_RING;
}

static void dwmac_desc_enh_tx_set_on_ring_chain(
  volatile dwmac_desc_ext *p, const bool end )
{
  if ( end )
    p->etx.des0_3.des0 |= DWMAC_DESC_ETX_DES0_TRANSMIT_END_OF_RING;
}

static void dwmac_desc_enh_set_tx_desc_len(
  volatile dwmac_desc_ext *p_enh, size_t len )
{
  p_enh->etx.des0_3.des1 = DWMAC_DESC_ETX_DES1_TRANSMIT_BUFFER_1_SIZE_SET(
    p_enh->etx.des0_3.des1,
    len
    );
}

static bool dwmac_desc_enh_is_giant_frame( const uint32_t des0 )
{
  return (
    ( des0
      & DWMAC_DESC_ERX_DES0_TIMESTAMP_AVAIL_OR_CHECKSUM_ERROR_OR_GIANT_FRAME
    ) != 0
    );
}

static bool dwmac_desc_enh_is_udp_payload( const uint32_t des4 )
{
  return (
    DWMAC_DESC_EXT_ERX_DES4_IP_PAYLOAD_TYPE_GET( des4 )
    == DWMAC_IP_PAYLOAD_TYPE_UDP
    );
}

static bool dwmac_desc_enh_is_tcp_payload( const uint32_t des4 )
{
  return (
    DWMAC_DESC_EXT_ERX_DES4_IP_PAYLOAD_TYPE_GET( des4 )
    == DWMAC_IP_PAYLOAD_TYPE_TCP
    );
}

static bool dwmac_desc_enh_is_icmp_payload( const uint32_t des4 )
{
  return (
    DWMAC_DESC_EXT_ERX_DES4_IP_PAYLOAD_TYPE_GET( des4 )
    == DWMAC_IP_PAYLOAD_TYPE_ICMP
    );
}

static dwmac_common_rx_frame_status dwmac_desc_enh_coe_status(
  volatile dwmac_desc_ext *p_enh )
{
  dwmac_common_rx_frame_status ret  = DWMAC_COMMON_RX_FRAME_STATUS_GOOD;
  const uint32_t               DES0 = p_enh->erx.des0_3.des0;
  const uint32_t               DES4 = p_enh->erx.des4;


  if ( ( DES0 & DWMAC_DESC_ERX_DES0_EXT_STATUS_AVAIL_OR_RX_MAC_ADDR_STATUS )
       != 0 ) {
    if ( !dwmac_desc_enh_is_giant_frame( DES0 )
         && ( DES0 & DWMAC_DESC_ERX_DES0_FREAME_TYPE ) == 0
         && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV6_PACKET_RECEIVED ) == 0
         && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV4_PACKET_RECEIVED ) == 0
         && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IP_PAYLOAD_ERROR ) == 0
         && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IP_HEADER_ERROR ) == 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: IEEE 802.3 Type frame.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_LLC_SNAP;
    } else if ( ( ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV6_PACKET_RECEIVED ) != 0
                  || ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV4_PACKET_RECEIVED )
                  != 0 )
                && dwmac_desc_enh_is_giant_frame( DES0 ) ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: IPv4/6 No CSUM Error.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_GOOD;
    } else if ( ( ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV6_PACKET_RECEIVED ) != 0
                  || ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV4_PACKET_RECEIVED )
                  != 0 )
                && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IP_PAYLOAD_ERROR ) != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: IPv4/6 Payload Error.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_CSUM_NONE;
    } else if ( ( ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV6_PACKET_RECEIVED ) != 0
                  || ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV4_PACKET_RECEIVED )
                  != 0 )
                && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IP_HEADER_ERROR ) != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: IPv4/6 Header Error.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_CSUM_NONE;
    } else if ( ( ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV6_PACKET_RECEIVED ) != 0
                  || ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV4_PACKET_RECEIVED )
                  != 0 )
                && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IP_PAYLOAD_ERROR ) != 0
                && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IP_HEADER_ERROR ) != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG(
        "RX Des0 status: IPv4/6 Header and Payload Error.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_CSUM_NONE;
    } else if ( ( ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV6_PACKET_RECEIVED ) != 0
                  || ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV4_PACKET_RECEIVED )
                  != 0 )
                && ( !dwmac_desc_enh_is_udp_payload( DES4 ) )
                && ( !dwmac_desc_enh_is_tcp_payload( DES4 ) )
                && ( !dwmac_desc_enh_is_icmp_payload( DES4 ) ) ) {
      DWMAC_DESC_ENH_PRINT_DBG(
        "RX Des0 status: IPv4/6 unsupported IP PAYLOAD.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_DISCARD;
    } else if ( ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV6_PACKET_RECEIVED ) == 0
                && ( DES4 & DWMAC_DESC_EXT_ERX_DES4_IPV4_PACKET_RECEIVED )
                == 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: No IPv4, IPv6 frame.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_DISCARD;
    }
  } else {
    uint32_t status = (
      (uint32_t) ( dwmac_desc_enh_is_giant_frame( DES0 ) << 2U )
      | (uint32_t) ( ( ( DES0 & DWMAC_DESC_ERX_DES0_FREAME_TYPE )
                       != 0 ) << 1U ) )
                      & 0x7U;

    /* bits 5 7 0 | Frame status
     * ----------------------------------------------------------
     *      0 0 0 | IEEE 802.3 Type frame (length < 1536 octects)
     *      1 0 0 | IPv4/6 No CSUM errorS.
     *      1 0 1 | IPv4/6 CSUM PAYLOAD error
     *      1 1 0 | IPv4/6 CSUM IP HR error
     *      1 1 1 | IPv4/6 IP PAYLOAD AND HEADER errorS
     *      0 0 1 | IPv4/6 unsupported IP PAYLOAD
     *      0 1 1 | COE bypassed.. no IPv4/6 frame
     *      0 1 0 | Reserved.
     */
    if ( status == 0x0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: IEEE 802.3 Type frame.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_LLC_SNAP;
    } else if ( status == 0x4 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: IPv4/6 No CSUM errorS.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_GOOD;
    } else if ( status == 0x5 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: IPv4/6 Payload Error.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_CSUM_NONE;
    } else if ( status == 0x6 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: IPv4/6 Header Error.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_CSUM_NONE;
    } else if ( status == 0x7 ) {
      DWMAC_DESC_ENH_PRINT_DBG(
        "RX Des0 status: IPv4/6 Header and Payload Error.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_CSUM_NONE;
    } else if ( status == 0x1 ) {
      DWMAC_DESC_ENH_PRINT_DBG(
        "RX Des0 status: IPv4/6 unsupported IP PAYLOAD.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_DISCARD;
    } else if ( status == 0x3 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "RX Des0 status: No IPv4, IPv6 frame.\n" );
      ret = DWMAC_COMMON_RX_FRAME_STATUS_DISCARD;
    }
  }

  return ret;
}

static int dwmac_desc_enh_get_tx_status(
  dwmac_common_context *self,
  const unsigned int    idx_tx )
{
  int                                 ret    = 0;
  volatile dwmac_desc_ext            *dma_tx =
    (volatile dwmac_desc_ext *) self->dma_tx;
  volatile dwmac_desc_ext            *p_desc = &dma_tx[idx_tx];
  dwmac_common_desc_status_counts_tx *counts =
    &self->stats.desc_status_counts_tx;


  if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_ERROR_SUMMARY ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "DWMAC TX error... 0x%08x\n", p->des01.etx );

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_JABBER_TIMEOUT )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tjabber_timeout error\n" );
      ++counts->jabber;
    }

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_FRAME_FLUSHED )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tframe_flushed error\n" );
      ++counts->frame_flushed;
      dwmac_core_dma_flush_tx_fifo( self );
    }

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_LOSS_OF_CARRIER )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tloss_carrier error\n" );
      ++counts->losscarrier;
    }

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_NO_CARRIER )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tno_carrier error\n" );
      ++counts->no_carrier;
    }

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_EXCESSIVE_COLLISION )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\texcessive_collisions\n" );
      ++counts->excessive_collisions;
    }

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_EXCESSIVE_DEFERAL )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\texcessive tx_deferral\n" );
      ++counts->excessive_deferral;
    }

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_UNDERFLOW_ERROR )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tunderflow error\n" );
      dwmac_core_dma_flush_tx_fifo( self );
      ++counts->underflow;
    }

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_IP_HEADER_ERROR )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tTX IP header csum error\n" );
      ++counts->ip_header_error;
    }

    if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_IP_PAYLOAD_ERROR )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tAddr/Payload csum error\n" );
      ++counts->payload_error;
      dwmac_core_dma_flush_tx_fifo( self );
    }

    ret = -1;
  }

  if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_DEFERRED_BIT ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "GMAC TX status: tx deferred\n" );
    ++counts->deferred;
  }

  if ( ( p_desc->etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_VLAN_FRAME ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "GMAC TX status: VLAN frame\n" );
    ++counts->vlan;
  }

  return ret;
}

static dwmac_common_rx_frame_status dwmac_desc_enh_get_rx_status(
  dwmac_common_context *self,
  const unsigned int    desc_idx )
{
  dwmac_common_desc_status_counts_rx *counts =
    &self->stats.desc_status_counts_rx;
  dwmac_common_rx_frame_status        ret    =
    DWMAC_COMMON_RX_FRAME_STATUS_GOOD;
  volatile dwmac_desc_ext            *dma_rx =
    (volatile dwmac_desc_ext *) self->dma_rx;
  const uint32_t                      DES0   = dma_rx[desc_idx].erx.des0_3.des0;


  if ( ( DES0 & DWMAC_DESC_ERX_DES0_ERROR_SUMMARY ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "GMAC RX Error Summary 0x%08x\n",
                              DES0 );

    if ( ( DES0 & DWMAC_DESC_ERX_DES0_DESCRIPTOR_ERROR ) != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tdescriptor error\n" );
      ++counts->descriptor_error;
    }

    if ( ( DES0 & DWMAC_DESC_ERX_DES0_OVERFLOW_ERROR ) != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\toverflow error\n" );
      ++counts->overflow_error;
    }

    if ( dwmac_desc_enh_is_giant_frame( DES0 ) ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tIPC Csum Error/Giant frame\n" );
      ++counts->giant_frame;
    }

    if ( ( DES0 & DWMAC_DESC_ERX_DES0_LATE_COLLISION ) != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tlate_collision error\n" );
      ++counts->late_collision;
    }

    if ( ( DES0 & DWMAC_DESC_ERX_DES0_RECEIVE_WATCHDOG_TIMEOUT )
         != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\treceive_watchdog error\n" );
      ++counts->watchdog_timeout;
    }

    if ( ( DES0 & DWMAC_DESC_ERX_DES0_RECEIVE_ERROR ) != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tReceive Error\n" );
      ++counts->receive_error;
    }

    if ( ( DES0 & DWMAC_DESC_ERX_DES0_CRC_ERROR ) != 0 ) {
      DWMAC_DESC_ENH_PRINT_DBG( "\tCRC error\n" );
      ++counts->crc_error;
    }

    ret = DWMAC_COMMON_RX_FRAME_STATUS_DISCARD;
  }

  if ( ret == DWMAC_COMMON_RX_FRAME_STATUS_GOOD ) {
    /* After a payload csum error, the ES bit is set.
     * It doesn't match with the information reported into the databook.
     * At any rate, we need to understand if the CSUM hw computation is ok
     * and report this info to the upper layers. */
    ret = dwmac_desc_enh_coe_status( &dma_rx[desc_idx] );
  }

  if ( ( DES0 & DWMAC_DESC_ERX_DES0_DRIBBLE_BIT_ERROR ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "GMAC RX: dribbling error\n" );
    ++counts->dribble_bit_error;
  }

  if ( ( DES0 & DWMAC_DESC_ERX_DES0_SRC_ADDR_FILTER_FAIL ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "GMAC RX : Source Address filter fail\n" );
    ++counts->source_addr_fail;
    ret = DWMAC_COMMON_RX_FRAME_STATUS_DISCARD;
  }

  if ( ( DES0 & DWMAC_DESC_ERX_DES0_DEST_ADDR_FILTER_FAIL ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "GMAC RX : Dest Address filter fail\n" );
    ++counts->dest_addr_fail;
    ret = DWMAC_COMMON_RX_FRAME_STATUS_DISCARD;
  }

  if ( ( DES0 & DWMAC_DESC_ERX_DES0_LENGTH_ERROR ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "GMAC RX: length_error error\n" );
    ++counts->length_error;
    ret = DWMAC_COMMON_RX_FRAME_STATUS_DISCARD;
  }

  if ( ( DES0 & DWMAC_DESC_ERX_DES0_VLAN_TAG ) != 0 ) {
    DWMAC_DESC_ENH_PRINT_DBG( "GMAC RX: VLAN frame tagged\n" );
    ++counts->vlan_tag;
  }

  return ret;
}

static void dwmac_desc_enh_print_tx_desc(
  volatile dwmac_desc *p,
  const unsigned int   count )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) p;
  unsigned int             index;


  if ( p_enh != NULL ) {
    for ( index = 0; index < count; ++index ) {
      printf( "Transmit DMA Descriptor %d\n", index );
      printf( "des0\n" );
      printf(
        " %u own bit\n"
        " %u IRQ on Completion\n"
        " %u Last Segment\n"
        " %u First Segment\n"
        " %u Disable CRC\n"
        " %u Disable Pad\n"
        " %u Transmit Timestamp Enable\n"
        " %lu Checksum Insertion Control\n"
        " %u Transmit End of Ring\n"
        " %u Second Address Chained\n"
        " %u Transmit Timestamp Status\n"
        " %u IP Header Error\n"
        " %u VLAN Frame\n"
        " %lu Collision Count\n"
        " %u Deferred Bit\n",
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_OWN_BIT ) != 0,
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_IRQ_ON_COMPLETION ) != 0,
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_LAST_SEGMENT ) != 0,
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_FIRST_SEGMENT ) != 0,
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_DISABLE_CRC ) != 0,
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_DISABLE_PAD ) != 0,
        ( p_enh[index].etx.des0_3.des0
          & DWMAC_DESC_ETX_DES0_TRANSMIT_TIMESTAMP_ENABLE ) != 0,
        DWMAC_DESC_ETX_DES0_CHECKSUM_INSERTION_CONTROL_GET( p_enh[index].etx.
                                                            des0_3.des0 ),
        ( p_enh[index].etx.des0_3.des0
          & DWMAC_DESC_ETX_DES0_TRANSMIT_END_OF_RING ) != 0,
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_SECOND_ADDR_CHAINED ) != 0,
        ( p_enh[index].etx.des0_3.des0
          & DWMAC_DESC_ETX_DES0_TRANSMIT_TIMESTAMP_STATUS ) != 0,
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_IP_HEADER_ERROR ) != 0,
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_VLAN_FRAME ) != 0,
        DWMAC_DESC_ETX_DES0_COLLISION_COUNT_GET( p_enh[index].etx.des0_3.des0 ),
        ( p_enh[index].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_DEFERRED_BIT ) != 0
        );

      if ( ( p_enh[index].etx.des0_3.des0
             & DWMAC_DESC_ETX_DES0_ERROR_SUMMARY ) != 0 ) {
        printf( " Error Summary:\n" );

        if ( p_enh[index].etx.des0_3.des0
             & DWMAC_DESC_ETX_DES0_JABBER_TIMEOUT ) {
          printf( "  Jabber Timeout\n" );
        }

        if ( ( p_enh[index].etx.des0_3.des0
               & DWMAC_DESC_ETX_DES0_FRAME_FLUSHED ) != 0 ) {
          printf( "  Frame Flush\n" );
        }

        if ( ( p_enh[index].etx.des0_3.des0
               & DWMAC_DESC_ETX_DES0_IP_PAYLOAD_ERROR ) != 0 ) {
          printf( "  Payload Error\n" );
        }

        if ( ( p_enh[index].etx.des0_3.des0
               & DWMAC_DESC_ETX_DES0_LOSS_OF_CARRIER ) != 0 ) {
          printf( "  Loss of Carrier\n" );
        }

        if ( ( p_enh[index].etx.des0_3.des0
               & DWMAC_DESC_ETX_DES0_NO_CARRIER ) != 0 ) {
          printf( "  No Carrier\n" );
        }

        if ( ( p_enh[index].etx.des0_3.des0
               & DWMAC_DESC_ETX_DES0_EXCESSIVE_COLLISION ) != 0 ) {
          printf( "  Excessive Collision\n" );
        }

        if ( ( p_enh[index].etx.des0_3.des0
               & DWMAC_DESC_ETX_DES0_EXCESSIVE_COLLISION ) != 0 ) {
          printf( "  Ecessive Deferral\n" );
        }

        if ( ( p_enh[index].etx.des0_3.des0
               & DWMAC_DESC_ETX_DES0_UNDERFLOW_ERROR ) != 0 ) {
          printf( "  Undeflow Error\n" );
        }
      }

      printf( "des1\n" );
      printf(
        " %lu Transmit Buffer 2 Size\n"
        " %lu Transmit Buffer 1 Size\n",
        DWMAC_DESC_ETX_DES1_TRANSMIT_BUFFER_2_SIZE_GET( p_enh[index].etx.des0_3.
                                                        des1 ),
        DWMAC_DESC_ETX_DES1_TRANSMIT_BUFFER_1_SIZE_GET( p_enh[index].etx.des0_3.
                                                        des1 )
        );
      printf( "des2\n" );
      printf( " %p Buffer 1 Address\n", (void *) p_enh[index].etx.des0_3.des2 );
      printf( "des3\n" );
      printf( " %p Buffer 2 Address\n", (void *) p_enh[index].etx.des0_3.des3 );
    }
  }
}

static void dwmac_desc_enh_print_rx_desc(
  volatile dwmac_desc *p,
  const unsigned int   count )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) p;
  unsigned int             index;


  if ( p_enh != NULL ) {
    for ( index = 0; index < count; ++index ) {
      printf( "Receive DMA Descriptor %d\n", index );
      printf( "des0\n" );
      printf(
        " %u Own Bit\n"
        " %u Dest. Addr. Filter Fail\n"
        " %lu Frame Length\n"
        " %u Source Addr. Filter Fail\n"
        " %u Length Error\n"
        " %u VLAN Tag\n"
        " %u First Descriptor\n"
        " %u Last Descriptor\n"
        " %u Frame Type\n"
        " %u Dribble Bit Error\n"
        " %u Extended Status Available\n",
        ( p_enh[index].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_OWN_BIT ) != 0,
        ( p_enh[index].erx.des0_3.des0
          & DWMAC_DESC_ERX_DES0_DEST_ADDR_FILTER_FAIL ) != 0,
        DWMAC_DESC_ERX_DES0_FRAME_LENGTH_GET(
          p_enh[index].erx.des0_3.des0 ),
        ( p_enh[index].erx.des0_3.des0
          & DWMAC_DESC_ERX_DES0_SRC_ADDR_FILTER_FAIL ) != 0,
        ( p_enh[index].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_LENGTH_ERROR ) != 0,
        ( p_enh[index].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_VLAN_TAG ) != 0,
        ( p_enh[index].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_FIRST_DESCRIPTOR ) != 0,
        ( p_enh[index].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_LAST_DESCRIPTOR ) != 0,
        ( p_enh[index].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_FREAME_TYPE ) != 0,
        ( p_enh[index].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_DRIBBLE_BIT_ERROR ) != 0,
        ( p_enh[index].erx.des0_3.des0
          & DWMAC_DESC_ERX_DES0_EXT_STATUS_AVAIL_OR_RX_MAC_ADDR_STATUS ) != 0
        );

      if ( ( p_enh[index].erx.des0_3.des0
             & DWMAC_DESC_ERX_DES0_ERROR_SUMMARY ) != 0 ) {
        printf( " Error Summary:\n" );

        if ( ( p_enh[index].erx.des0_3.des0
               & DWMAC_DESC_ERX_DES0_DESCRIPTOR_ERROR ) != 0 ) {
          printf( "  Descriptor Error\n" );
        }

        if ( ( p_enh[index].erx.des0_3.des0
               & DWMAC_DESC_ERX_DES0_OVERFLOW_ERROR ) != 0 ) {
          printf( "  Overflow Error\n" );
        }

        if ( ( p_enh[index].erx.des0_3.des0
               &
               DWMAC_DESC_ERX_DES0_TIMESTAMP_AVAIL_OR_CHECKSUM_ERROR_OR_GIANT_FRAME )
             != 0 ) {
          printf( "  Giant Frame\n" );
        }

        if ( ( p_enh[index].erx.des0_3.des0
               & DWMAC_DESC_ERX_DES0_LATE_COLLISION ) != 0 ) {
          printf( "  Late Collision\n" );
        }

        if ( ( p_enh[index].erx.des0_3.des0
               & DWMAC_DESC_ERX_DES0_RECEIVE_WATCHDOG_TIMEOUT ) != 0
             || ( p_enh[index].erx.des0_3.des0
                  & DWMAC_DESC_ERX_DES0_RECEIVE_ERROR ) != 0 ) {
          printf( "  IP Header or IP Payload:\n" );

          if ( ( p_enh[index].erx.des0_3.des0
                 & DWMAC_DESC_ERX_DES0_RECEIVE_WATCHDOG_TIMEOUT ) != 0 ) {
            printf( "   Watchdog Timeout\n" );
          }

          if ( ( p_enh[index].erx.des0_3.des0
                 & DWMAC_DESC_ERX_DES0_RECEIVE_ERROR ) != 0 ) {
            printf( "   Receive Error\n" );
          }
        }

        if ( ( p_enh[index].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_CRC_ERROR )
             != 0 ) {
          printf( "  CRC Error\n" );
        }
      }

      printf( "des1\n" );
      printf(
        " %u Disable Interrupt on Completion\n"
        " %lu Receive Buffer 2 Size\n"
        " %u Receive End of Ring\n"
        " %u Second Addr. Chained\n"
        " %lu Receive Buffer 1 Size\n",
        ( p_enh[index].erx.des0_3.des1
          & DWMAC_DESC_ERX_DES1_DISABLE_IRQ_ON_COMPLETION ) != 0,
        DWMAC_DESC_ERX_DES1_RECEIVE_BUFF_2_SIZE_GET( p_enh[index].erx.des0_3.
                                                     des1 ),
        ( p_enh[index].erx.des0_3.des1 & DWMAC_DESC_ERX_DES1_RECEIVE_END_OF_RING ) != 0,
        ( p_enh[index].erx.des0_3.des1 & DWMAC_DESC_ERX_DES1_SECOND_ADDR_CHAINED ) != 0,
        DWMAC_DESC_ERX_DES1_RECEIVE_BUFF_1_SIZE_GET( p_enh[index].erx.des0_3.
                                                     des1 )
        );
      printf( "des2\n" );
      printf( " %p Buffer 1 Address\n", (void *) p_enh[index].erx.des0_3.des2 );
      printf( "des3\n" );
      printf( " %p Buffer 2 Address\n", (void *) p_enh[index].erx.des0_3.des3 );
    }
  }
}

static int dwmac_desc_enh_create_rx_desc( dwmac_common_context *self )
{
  int          eno        = 0;
  const size_t NUM_DESCS  = (size_t) self->bsd_config->rbuf_count;
  const size_t SIZE_DESCS = NUM_DESCS * sizeof( dwmac_desc_ext );
  void        *desc_mem   = NULL;


  assert( NULL == self->dma_rx );

  /* Allocate an array of mbuf pointers */
  self->mbuf_addr_rx = calloc( NUM_DESCS, sizeof( struct mbuf * ) );

  if ( self->mbuf_addr_rx == NULL ) {
    eno = ENOMEM;
  }

  /* Allocate an array of dma descriptors */
  if ( eno == 0 ) {
    eno = ( self->CFG->CALLBACK.mem_alloc_nocache )(
      self->arg,
      &desc_mem,
      SIZE_DESCS
      );
  }

  if ( eno == 0 ) {
    if ( desc_mem != NULL ) {
      memset( desc_mem, 0, SIZE_DESCS );
      DWMAC_COMMON_DSB();
    } else {
      eno = ENOMEM;
    }
  }

  if ( eno == 0 ) {
    self->dma_rx = (volatile dwmac_desc *) desc_mem;
    DWMAC_COMMON_DSB();
  }

  return eno;
}

static void dwmac_desc_enh_init_rx_desc(
  dwmac_common_context *self,
  const unsigned int    index )
{
  volatile dwmac_desc_ext *p_enh       =
    (volatile dwmac_desc_ext *) self->dma_rx;
  const size_t             NUM_DESCS   = (size_t) self->bsd_config->rbuf_count;
  char                    *clust_start =
    mtod( self->mbuf_addr_rx[index], char * );


  assert( NULL != p_enh );

  DWMAC_COMMON_DSB();

  rtems_cache_invalidate_multiple_data_lines(
    clust_start,
    DWMAC_DESC_COM_BUF_SIZE + ETHER_ALIGN
    );

  if ( self->mbuf_addr_rx[index] != NULL ) {
    p_enh[index].erx.des0_3.des1 = DWMAC_DESC_ERX_DES1_RECEIVE_BUFF_1_SIZE_SET(
      p_enh->erx.des0_3.des1,
      DWMAC_DESC_COM_BUF_SIZE );
  } else {
    p_enh[index].erx.des0_3.des1 = DWMAC_DESC_ERX_DES1_RECEIVE_BUFF_1_SIZE_SET(
      p_enh->erx.des0_3.des1,
      0 );
  }

  p_enh[index].erx.des0_3.des2 = (uint32_t) clust_start;

  /* The network controller supports adding a second data buffer to
   * p_enh->erx.des0_3.des3. For simplicity reasons we will not do this */
  dwmac_desc_enh_rx_set_on_ring_chain( &p_enh[index],
                                       ( index == NUM_DESCS - 1 ) );
  DWMAC_COMMON_DSB();
  p_enh[index].erx.des0_3.des0 = DWMAC_DESC_ERX_DES0_OWN_BIT;
}

static int dwmac_desc_enh_destroy_rx_desc( dwmac_common_context *self )
{
  int                  eno    = 0;
  volatile dwmac_desc *dma_rx = self->dma_rx;


  if ( self->mbuf_addr_rx != NULL ) {
    free( self->mbuf_addr_rx, 0 );
    self->mbuf_addr_rx = NULL;
  }

  if ( dma_rx != NULL ) {
    eno          = self->CFG->CALLBACK.mem_free_nocache( self->arg, dma_rx );
    self->dma_rx = NULL;
  }

  DWMAC_COMMON_DSB();

  return eno;
}

static void dwmac_desc_enh_release_rx_bufs( dwmac_common_context *self )
{
  volatile dwmac_desc_ext *p_enh     = (volatile dwmac_desc_ext *) self->dma_rx;
  const size_t             NUM_DESCS = (size_t) self->bsd_config->rbuf_count;
  unsigned int             i;


  assert( p_enh != NULL );

  for ( i = 0; i < NUM_DESCS; ++i ) {
    if ( p_enh[i].erx.des0_3.des2 != 0 ) {
      struct mbuf *dummy;

      assert( self->mbuf_addr_rx[i] != NULL );

      MFREE( self->mbuf_addr_rx[i], dummy );
      (void) dummy;
      memset(&p_enh[i].erx, 0, sizeof( dwmac_desc_ext ) );
    }
  }

  self->dma_rx = (volatile dwmac_desc *) p_enh;
  DWMAC_COMMON_DSB();
}

static int dwmac_desc_enh_create_tx_desc( dwmac_common_context *self )
{
  int          eno        = 0;
  void        *mem_desc   = NULL;
  const size_t NUM_DESCS  = (size_t) self->bsd_config->xbuf_count;
  const size_t SIZE_DESCS = NUM_DESCS * sizeof( dwmac_desc_ext );


  assert( self->dma_tx == NULL );

  /* Allocate an array of mbuf pointers */
  self->mbuf_addr_tx = calloc( NUM_DESCS, sizeof( struct mbuf * ) );

  if ( self->mbuf_addr_tx == NULL ) {
    eno = ENOMEM;
  }

  if ( eno == 0 ) {
    eno = ( self->CFG->CALLBACK.mem_alloc_nocache )(
      self->arg,
      &mem_desc,
      SIZE_DESCS
      );
  }

  if ( eno == 0 ) {
    if ( mem_desc != NULL ) {
      memset( mem_desc, 0, SIZE_DESCS );
      DWMAC_COMMON_DSB();
    } else {
      eno = ENOMEM;
    }
  }

  if ( eno == 0 ) {
    self->dma_tx = mem_desc;
    DWMAC_COMMON_DSB();
  }

  return eno;
}

static void dwmac_desc_enh_init_tx_desc( dwmac_common_context *self )
{
  volatile dwmac_desc_ext *p_enh     = (volatile dwmac_desc_ext *) self->dma_tx;
  const size_t             NUM_DESCS = (size_t) self->bsd_config->xbuf_count;
  unsigned int             i;


  assert( p_enh != NULL );

  for ( i = 0; i < NUM_DESCS; ++i ) {
    dwmac_desc_enh_tx_set_on_ring_chain( &p_enh[i], ( i == NUM_DESCS - 1 ) );
  }

  self->dma_tx = (volatile dwmac_desc *) &p_enh[0];
  DWMAC_COMMON_DSB();
}

static int dwmac_desc_enh_destroy_tx_desc( dwmac_common_context *self )
{
  int   eno      = 0;
  void *mem_desc = __DEVOLATILE( void *, self->dma_tx );


  if ( self->mbuf_addr_tx != NULL ) {
    free( self->mbuf_addr_tx, 0 );
    self->mbuf_addr_tx = NULL;
  }

  if ( mem_desc != NULL ) {
    eno          = self->CFG->CALLBACK.mem_free_nocache( self->arg, mem_desc );
    mem_desc     = NULL;
    self->dma_tx = (volatile dwmac_desc *) mem_desc;
  }

  DWMAC_COMMON_DSB();

  return eno;
}

static void dwmac_desc_enh_release_tx_bufs( dwmac_common_context *self )
{
  volatile dwmac_desc_ext *p_enh     = (volatile dwmac_desc_ext *) self->dma_tx;
  const size_t             NUM_DESCS = (size_t) self->bsd_config->xbuf_count;
  unsigned int             i;


  assert( p_enh != NULL );

  for ( i = 0; i < NUM_DESCS; ++i ) {
    if ( p_enh[i].etx.des0_3.des1 != 0 ) {
      struct mbuf *dummy;

      assert( self->mbuf_addr_tx[i] != NULL );

      MFREE( self->mbuf_addr_tx[i], dummy );
      (void) dummy;
      memset( __DEVOLATILE( void *,
                            &p_enh[i].etx ), 0, sizeof( dwmac_desc_ext ) );
    }
  }

  self->dma_tx = (volatile dwmac_desc *) p_enh;
  DWMAC_COMMON_DSB();
}

static inline size_t dwmac_desc_enh_get_rx_frame_len(
  dwmac_common_context *self,
  const unsigned int    desc_idx )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) self->dma_rx;


  /* The type-1 checksum offload engines append the checksum at
   * the end of frame and the two bytes of checksum are added in
   * the length.
   * Adjust for that in the framelen for type-1 checksum offload
   * engines. */
  if ( self->dmagrp->hw_feature & DMAGRP_HW_FEATURE_RXTYP1COE ) {
    return DWMAC_DESC_ERX_DES0_FRAME_LENGTH_GET( p_enh[desc_idx].erx.des0_3.des0 )
           - 2U;
  } else {
    return DWMAC_DESC_ERX_DES0_FRAME_LENGTH_GET( p_enh[desc_idx].erx.des0_3.des0 );
  }
}

static bool dwmac_desc_enh_am_i_rx_owner(
  dwmac_common_context *self,
  const unsigned int    desc_idx )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) self->dma_rx;
  bool                     am_i_owner;


  DWMAC_COMMON_DSB();
  am_i_owner =
    ( p_enh[desc_idx].erx.des0_3.des0 & DWMAC_DESC_ERX_DES0_OWN_BIT ) == 0;

  return am_i_owner;
}

static bool dwmac_desc_enh_am_i_tx_owner(
  dwmac_common_context *self,
  const unsigned int    idx_tx )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) self->dma_tx;
  bool                     am_i_owner;


  DWMAC_COMMON_DSB();
  am_i_owner =
    ( p_enh[idx_tx].etx.des0_3.des0 & DWMAC_DESC_ETX_DES0_OWN_BIT ) == 0;

  return am_i_owner;
}

static void dwmac_desc_enh_release_tx_ownership(
  dwmac_common_context *self,
  const unsigned int    idx_tx )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) self->dma_tx;


  DWMAC_COMMON_DSB();
  p_enh[idx_tx].erx.des0_3.des0 |= DWMAC_DESC_ETX_DES0_OWN_BIT;
}

static int dwmac_desc_enh_get_tx_ls(
  dwmac_common_context *self,
  const unsigned int    idx_tx )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) self->dma_tx;


  return ( ( p_enh[idx_tx].etx.des0_3.des0
             & DWMAC_DESC_ETX_DES0_LAST_SEGMENT ) != 0 );
}

static void dwmac_desc_enh_release_tx_desc(
  dwmac_common_context *self,
  const unsigned int    idx_tx )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) self->dma_tx;


  p_enh[idx_tx].etx.des0_3.des0 =
    p_enh[idx_tx].etx.des0_3.des0
    & ( DWMAC_DESC_ETX_DES0_TRANSMIT_END_OF_RING
        | DWMAC_DESC_ETX_DES0_SECOND_ADDR_CHAINED );

  p_enh[idx_tx].etx.des0_3.des1 = 0;
}

static void dwmac_desc_enh_prepare_tx_desc(
  dwmac_common_context *self,
  const unsigned int    idx,
  const bool            is_first,
  const size_t          len,
  const void           *pdata )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) self->dma_tx;


  if ( is_first ) {
    p_enh[idx].etx.des0_3.des0 |= DWMAC_DESC_ETX_DES0_FIRST_SEGMENT;
  }

  dwmac_desc_enh_set_tx_desc_len( &p_enh[idx], len );

  p_enh[idx].etx.des0_3.des2 = (uintptr_t) pdata;
}

static void dwmac_desc_enh_close_tx_desc(
  dwmac_common_context *self,
  const unsigned int    idx_tx )
{
  volatile dwmac_desc_ext *p_enh = (volatile dwmac_desc_ext *) self->dma_tx;


  p_enh[idx_tx].etx.des0_3.des0 |= DWMAC_DESC_ETX_DES0_LAST_SEGMENT;
  p_enh[idx_tx].etx.des0_3.des0 |= DWMAC_DESC_ETX_DES0_IRQ_ON_COMPLETION;
}

static bool dwmac_desc_enh_is_first_rx_segment(
  dwmac_common_context *self,
  const unsigned int    descriptor_index )
{
  volatile dwmac_desc_ext *p_descs = (volatile dwmac_desc_ext *) self->dma_rx;


  return ( ( p_descs[descriptor_index].erx.des0_3.des0
             & DWMAC_DESC_ERX_DES0_FIRST_DESCRIPTOR ) != 0 );
}

static bool dwmac_desc_enh_is_last_rx_segment(
  dwmac_common_context *self,
  const unsigned int    descriptor_index )
{
  volatile dwmac_desc_ext *p_descs = (volatile dwmac_desc_ext *) self->dma_rx;


  return ( ( p_descs[descriptor_index].erx.des0_3.des0
             & DWMAC_DESC_ERX_DES0_LAST_DESCRIPTOR ) != 0 );
}

static int dwmac_desc_enh_validate( dwmac_common_context *self )
{
  /* Does the hardware support enhanced descriptors? */
  if ( ( self->dmagrp->hw_feature & DMAGRP_HW_FEATURE_ENHDESSEL ) != 0 ) {
    return 0;
  } else {
    return EINVAL;
  }
}

static bool dwmac_desc_enh_use_enhanced_descs( dwmac_common_context *self )
{
  (void) self;

  /* Yes, we use enhanced descriptors */
  return true;
}

const dwmac_common_desc_ops dwmac_desc_ops_enhanced = {
  .validate             = dwmac_desc_enh_validate,
  .use_enhanced_descs   = dwmac_desc_enh_use_enhanced_descs,
  .tx_status            = dwmac_desc_enh_get_tx_status,
  .rx_status            = dwmac_desc_enh_get_rx_status,
  .create_rx_desc       = dwmac_desc_enh_create_rx_desc,
  .create_tx_desc       = dwmac_desc_enh_create_tx_desc,
  .destroy_rx_desc      = dwmac_desc_enh_destroy_rx_desc,
  .destroy_tx_desc      = dwmac_desc_enh_destroy_tx_desc,
  .init_rx_desc         = dwmac_desc_enh_init_rx_desc,
  .init_tx_desc         = dwmac_desc_enh_init_tx_desc,
  .release_rx_bufs      = dwmac_desc_enh_release_rx_bufs,
  .release_tx_bufs      = dwmac_desc_enh_release_tx_bufs,
  .alloc_data_buf       = dwmac_desc_com_new_mbuf,
  .am_i_tx_owner        = dwmac_desc_enh_am_i_tx_owner,
  .am_i_rx_owner        = dwmac_desc_enh_am_i_rx_owner,
  .release_tx_desc      = dwmac_desc_enh_release_tx_desc,
  .prepare_tx_desc      = dwmac_desc_enh_prepare_tx_desc,
  .close_tx_desc        = dwmac_desc_enh_close_tx_desc,
  .get_tx_ls            = dwmac_desc_enh_get_tx_ls,
  .release_tx_ownership = dwmac_desc_enh_release_tx_ownership,
  .get_rx_frame_len     = dwmac_desc_enh_get_rx_frame_len,
  .is_first_rx_segment  = dwmac_desc_enh_is_first_rx_segment,
  .is_last_rx_segment   = dwmac_desc_enh_is_last_rx_segment,
  .print_tx_desc        = dwmac_desc_enh_print_tx_desc,
  .print_rx_desc        = dwmac_desc_enh_print_rx_desc,
};

/* This wrapped function pointer struct can be passed into the
 * configuration initializer for the driver */
const dwmac_descriptor_ops DWMAC_DESCRIPTOR_OPS_ENHANCED =
  DWMAC_DESCRIPTOR_OPS_INITIALIZER(
    &dwmac_desc_ops_enhanced
    );