SURF  1.0
EthMacPkg.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 -- File : EthMacPkg.vhd
3 -- Company : SLAC National Accelerator Laboratory
4 -- Created : 2015-09-21
5 -- Last update: 2016-10-20
6 -------------------------------------------------------------------------------
7 -- Description: Ethernet MAC Package File
8 -------------------------------------------------------------------------------
9 -- This file is part of 'SLAC Firmware Standard Library'.
10 -- It is subject to the license terms in the LICENSE.txt file found in the
11 -- top-level directory of this distribution and at:
12 -- https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
13 -- No part of 'SLAC Firmware Standard Library', including this file,
14 -- may be copied, modified, propagated, or distributed except according to
15 -- the terms contained in the LICENSE.txt file.
16 -------------------------------------------------------------------------------
17 
18 library ieee;
19 use ieee.std_logic_1164.all;
20 use ieee.std_logic_unsigned.all;
21 use ieee.std_logic_arith.all;
22 
23 use work.StdRtlPkg.all;
24 use work.AxiStreamPkg.all;
25 
26 package EthMacPkg is
27 --! @file
28  --! @ingroup ethernet_EthMacCore
29 
30  -- Default MAC is 01:03:00:56:44:00
31  constant EMAC_ADDR_INIT_C : slv(47 downto 0) := x"020300564400";
32 
33  -- EtherTypes
34  constant ARP_TYPE_C : slv(15 downto 0) := x"0608"; -- EtherType = ARP = 0x0806
35  constant IPV4_TYPE_C : slv(15 downto 0) := x"0008"; -- EtherType = IPV4 = 0x0800
36  constant VLAN_TYPE_C : slv(15 downto 0) := x"0081"; -- EtherType = VLAN = 0x8100
37 
38  -- IPV4 Protocol Constants
39  constant UDP_C : slv(7 downto 0) := x"11"; -- Protocol = UDP = 0x11
40  constant TCP_C : slv(7 downto 0) := x"06"; -- Protocol = TCP = 0x06
41  constant ICMP_C : slv(7 downto 0) := x"01"; -- Protocol = ICMP = 0x01
42 
43  -- DHCP Constants
44  constant DHCP_CPORT : slv(15 downto 0) := x"4400"; -- Port = 68 = 0x0044
45  constant DHCP_SPORT : slv(15 downto 0) := x"4300"; -- Port = 67 = 0x0043
46 
47  -- First TUSER Bits
48  constant EMAC_FRAG_BIT_C : integer := 0;
49  constant EMAC_SOF_BIT_C : integer := 1;
50 
51  -- Last TUSER Bits
52  constant EMAC_EOFE_BIT_C : integer := 0;
53  constant EMAC_IPERR_BIT_C : integer := 1;
54  constant EMAC_TCPERR_BIT_C : integer := 2;
55  constant EMAC_UDPERR_BIT_C : integer := 3;
56 
57  -- Ethernet AXI Stream Configuration
59  TSTRB_EN_C => false,
60  TDATA_BYTES_C => 16,
61  TDEST_BITS_C => 8,
62  TID_BITS_C => 0,
63  TKEEP_MODE_C => TKEEP_COMP_C,
64  TUSER_BITS_C => 4,
65  TUSER_MODE_C => TUSER_FIRST_LAST_C);
66 
67  -- Generic XMAC Configuration
68  type EthMacConfigType is record
69  macAddress : slv(47 downto 0);
72  pauseTime : slv(15 downto 0);
77  end record EthMacConfigType;
80  filtEnable => '1',
81  pauseEnable => '1',
82  pauseTime => x"00FF",
83  ipCsumEn => '1',
84  tcpCsumEn => '1',
85  udpCsumEn => '1',
86  dropOnPause => '0');
87  type EthMacConfigArray is array (natural range<>) of EthMacConfigType;
88 
89  -- Generic XMAC Status
90  type EthMacStatusType is record
100  end record EthMacStatusType;
102  rxFifoDropCnt => '0',
103  rxPauseCnt => '0',
104  txPauseCnt => '0',
105  rxCountEn => '0',
106  rxOverFlow => '0',
107  rxCrcErrorCnt => '0',
108  txCountEn => '0',
109  txUnderRunCnt => '0',
110  txNotReadyCnt => '0');
111  type EthMacStatusArray is array (natural range<>) of EthMacStatusType;
112 
113  constant EMAC_CSUM_PIPELINE_C : natural := 3;
114  type EthMacCsumAccumType is record
116  sum1 : Slv32Array(1 downto 0);
117  sum3 : slv(31 downto 0);
118  sum5 : slv(15 downto 0);
119  end record EthMacCsumAccumType;
121  step => (others => '0'),
122  sum1 => (others => (others => '0')),
123  sum3 => (others => '0'),
124  sum5 => (others => '0'));
125  type EthMacCsumAccumArray is array (natural range<>) of EthMacCsumAccumType;
126 
127  function EthPortArrayBigEndian (portNum : PositiveArray; portSize : positive) return Slv16Array;
128 
130  -- Input
131  -- Summation/Accumulation Signals
132  -- Results procedure GetEthMacCsum (
133  udpDet : in sl;
134  last : in sl;
135  hdr : in Slv8Array(19 downto 0); -- IPv4 Header
136  tKeep : in slv(15 downto 0); -- TCP/Data tKeep
137  tData : in slv(127 downto 0); -- TCP/Data tKeep
138  len : in slv(15 downto 0);
139  ibcsum : in slv(15 downto 0); -- TCP/UDP checksum to compare
140  r : in EthMacCsumAccumArray(1 downto 0);
141  v : inout EthMacCsumAccumArray(1 downto 0);
142  ipValid : inout sl;
143  ipCsum : inout slv(15 downto 0);
144  valid : inout sl;
145  csum : inout slv(15 downto 0));
146 
147 end package EthMacPkg;
148 
149 package body EthMacPkg is
150 
151  function EthPortArrayBigEndian (portNum : PositiveArray; portSize : positive) return Slv16Array is
152  variable i : natural;
153  variable vec : slv(15 downto 0);
154  variable retVar : Slv16Array((portSize-1) downto 0);
155  begin
156  for i in (portSize-1) downto 0 loop
157  -- Convert the NaturalArray into 16-bit SLV
158  vec := toSlv(portNum(i), 16);
159  -- Convert to big Endian
160  retVar(i)(15 downto 8) := vec(7 downto 0);
161  retVar(i)(7 downto 0) := vec(15 downto 8);
162  end loop;
163  return retVar;
164  end function;
165 
167  -- Input
168  -- Summation/Accumulation Signals
169  -- Results procedure GetEthMacCsum (
170  udpDet : in sl;
171  last : in sl;
172  hdr : in Slv8Array(19 downto 0); -- IPv4 Header
173  tKeep : in slv(15 downto 0); -- TCP/Data tKeep
174  tData : in slv(127 downto 0); -- TCP/Data tData
175  len : in slv(15 downto 0); -- TCP/Data length
176  ibcsum : in slv(15 downto 0); -- TCP/UDP checksum to compare
177  r : in EthMacCsumAccumArray(1 downto 0);
178  v : inout EthMacCsumAccumArray(1 downto 0);
179  ipValid : inout sl;
180  ipCsum : inout slv(15 downto 0);
181  valid : inout sl;
182  csum : inout slv(15 downto 0)) is
183  variable i : natural;
184  variable header : Slv32Array(9 downto 0);
185  variable data : Slv32Array(7 downto 0);
186  variable hdrCsum : slv(15 downto 0);
187  variable lenProt : slv(31 downto 0);
188  variable sum0A : Slv32Array(3 downto 0);
189  variable sum0B : Slv32Array(3 downto 0);
190  variable sum2A : Slv32Array(1 downto 0);
191  variable sum2B : slv(31 downto 0);
192  variable sum3 : Slv32Array(1 downto 0);
193  variable sum3A : Slv32Array(1 downto 0);
194  variable sum3B : Slv32Array(1 downto 0);
195  variable sum4 : Slv32Array(1 downto 0);
196  begin
197  -- Convert to 32-bit (little Endian) words
198  lenProt := x"0000" & len;
199  for i in 9 downto 0 loop
200  header(i) := x"00000000";
201  -- Check for inbound checksum
202  if i = 5 then
203  -- Mask off and Save Header Checksum
204  hdrCsum(15 downto 8) := hdr(2*i+0);
205  hdrCsum(7 downto 0) := hdr(2*i+1);
206  else
207  header(i)(15 downto 8) := hdr(2*i+0);
208  header(i)(7 downto 0) := hdr(2*i+1);
209  end if;
210  end loop;
211  for i in 7 downto 0 loop
212  data(i) := x"00000000";
213  -- Check tKeep for big Endian upper byte of 16-bit word
214  if tKeep((2*i)+0) = '1' then
215  data(i)(15 downto 8) := tData((8*((2*i)+0))+7 downto (8*((2*i)+0))+0);
216  end if;
217  -- Check tKeep for big Endian lower byte of 16-bit word
218  if tKeep((2*i)+1) = '1' then
219  data(i)(7 downto 0) := tData((8*((2*i)+1))+7 downto (8*((2*i)+1))+0);
220  end if;
221  end loop;
222 
223  -- Summation: Level0
224  v(0).step(0) := last;
225  v(1).step(0) := last;
226  for i in 3 downto 0 loop
227  sum0A(i) := header(2*i+0) + header(2*i+1);
228  sum0B(i) := data(2*i+0) + data(2*i+1);
229  end loop;
230 
231  -- Summation: Level1
232  for i in 1 downto 0 loop
233  v(0).sum1(i) := sum0A(2*i+0) + sum0A(2*i+1);
234  v(1).sum1(i) := sum0B(2*i+0) + sum0B(2*i+1);
235  end loop;
236 
237  -- Summation: Level2
238  v(0).step(1) := r(0).step(0);
239  v(1).step(1) := r(1).step(0);
240  sum2A(0) := r(0).sum1(0) + r(0).sum1(1);
241  sum2A(1) := header(8) + header(9);
242  sum2B := r(1).sum1(0) + r(1).sum1(1);
243 
244  -- Summation: Level3
245  v(0).sum3 := sum2A(0) + sum2A(1);
246 
247  -- Check if we need to reset the accumulator
248  if r(0).step(1) = '1' then
249  v(1).sum3 := (others => '0');
250  else
251  -- Summation/Accumulation: Level3
252  v(1).sum3 := r(1).sum3 + sum2B;
253  end if;
254 
255  -- Summation: Level4
256  v(0).step(2) := r(0).step(1);
257  v(1).step(2) := r(1).step(1);
258  sum3(0) := r(0).sum3;
259  sum3(1) := r(1).sum3 + lenProt;
260  for i in 1 downto 0 loop
261  sum3A(i)(31 downto 16) := x"0000";
262  sum3A(i)(15 downto 0) := sum3(i)(31 downto 16);
263  sum3B(i)(31 downto 16) := x"0000";
264  sum3B(i)(15 downto 0) := sum3(i)(15 downto 0);
265  sum4(i) := sum3A(i) + sum3B(i);
266  end loop;
267 
268  -- Summation: Level5
269  for i in 1 downto 0 loop
270  v(i).sum5 := sum4(i)(31 downto 16) + sum4(i)(15 downto 0);
271  end loop;
272 
273  -- Perform 1's complement
274  v(0).step(3) := r(0).step(2);
275  v(1).step(3) := r(1).step(2);
276  ipCsum := not(r(0).sum5);
277  csum := not(r(1).sum5);
278 
279  -- UDP checksum is calculated using one's complement arithmetic (RFC 793).
280  -- UDP has a special case where 0x0000 is reserved for "no checksum computed".
281  -- Thus 0x0000 is illegal and when calculated following the standard
282  -- algorithm, replaced with 0xFFFF.
283  if (udpDet = '1') and (csum = x"0000") then
284  csum := x"FFFF";
285  end if;
286 
287  -- Check for valid inbound checksum
288  if (ipCsum = hdrCsum) then
289  ipValid := '1';
290  else
291  ipValid := '0';
292  end if;
293 
294  -- Check for UDP special case of 0x0000 checksum
295  if (udpDet = '1') and (ibcsum = x"0000") then
296  valid := '1';
297  -- check the standard checksum calculation
298  elsif (ibcsum = csum) then
299  valid := '1';
300  -- Else not a valid checksum
301  else
302  valid := '0';
303  end if;
304 
305  end procedure;
306 
307 end package body EthMacPkg;
EthMacCsumAccumType
Definition: EthMacPkg.vhd:114
sl rxCrcErrorCnt
Definition: EthMacPkg.vhd:96
natural range 0 to 8 TDEST_BITS_C
array(natural range <> ) of slv( 31 downto 0) Slv32Array
Definition: StdRtlPkg.vhd:379
Slv32Array( 1 downto 0) sum1
Definition: EthMacPkg.vhd:116
slv( 15 downto 0) := x"4300" DHCP_SPORT
Definition: EthMacPkg.vhd:45
EthMacStatusType
Definition: EthMacPkg.vhd:90
Slv16Array EthPortArrayBigEndianportNum,portSize,
Definition: EthMacPkg.vhd:127
sl dropOnPause
Definition: EthMacPkg.vhd:76
slv( 7 downto 0) := x"06" TCP_C
Definition: EthMacPkg.vhd:40
std_logic sl
Definition: StdRtlPkg.vhd:28
slv( 15 downto 0) := x"0008" IPV4_TYPE_C
Definition: EthMacPkg.vhd:35
slv( 15 downto 0) tKeep
sl pauseEnable
Definition: EthMacPkg.vhd:71
integer := 3 EMAC_UDPERR_BIT_C
Definition: EthMacPkg.vhd:55
integer := 2 EMAC_TCPERR_BIT_C
Definition: EthMacPkg.vhd:54
natural range 1 to 16 TDATA_BYTES_C
EthMacStatusType :=(rxFifoDropCnt => '0',rxPauseCnt => '0',txPauseCnt => '0',rxCountEn => '0',rxOverFlow => '0',rxCrcErrorCnt => '0',txCountEn => '0',txUnderRunCnt => '0',txNotReadyCnt => '0') ETH_MAC_STATUS_INIT_C
Definition: EthMacPkg.vhd:101
slv( 15 downto 0) := x"4400" DHCP_CPORT
Definition: EthMacPkg.vhd:44
slv( EMAC_CSUM_PIPELINE_C downto 0) step
Definition: EthMacPkg.vhd:115
TkeepModeType TKEEP_MODE_C
sl rxFifoDropCnt
Definition: EthMacPkg.vhd:91
slv( 15 downto 0) := x"0608" ARP_TYPE_C
Definition: EthMacPkg.vhd:34
natural range 0 to 8 TID_BITS_C
sl txNotReadyCnt
Definition: EthMacPkg.vhd:99
integer := 0 EMAC_FRAG_BIT_C
Definition: EthMacPkg.vhd:48
slv( 47 downto 0) macAddress
Definition: EthMacPkg.vhd:69
_library_ ieeeieee
slv( 127 downto 0) tData
slv( 31 downto 0) sum3
Definition: EthMacPkg.vhd:117
sl ipCsumEn
Definition: EthMacPkg.vhd:73
EthMacCsumAccumType :=(step =>( others => '0'),sum1 =>( others =>( others => '0')),sum3 =>( others => '0'),sum5 =>( others => '0')) ETH_MAC_CSUM_ACCUM_INIT_C
Definition: EthMacPkg.vhd:120
array(natural range <> ) of EthMacConfigType EthMacConfigArray
Definition: EthMacPkg.vhd:87
natural := 3 EMAC_CSUM_PIPELINE_C
Definition: EthMacPkg.vhd:113
array(natural range <> ) of positive PositiveArray
Definition: StdRtlPkg.vhd:35
sl txPauseCnt
Definition: EthMacPkg.vhd:93
sl udpCsumEn
Definition: EthMacPkg.vhd:75
EthMacConfigType
Definition: EthMacPkg.vhd:68
integer := 0 EMAC_EOFE_BIT_C
Definition: EthMacPkg.vhd:52
integer := 1 EMAC_SOF_BIT_C
Definition: EthMacPkg.vhd:49
boolean TSTRB_EN_C
sl txCountEn
Definition: EthMacPkg.vhd:97
slv( 15 downto 0) sum5
Definition: EthMacPkg.vhd:118
TUserModeType TUSER_MODE_C
GetEthMacCsumudpDet,last,hdr,tKeep,tData,len,ibcsum,r,v,ipValid,ipCsum,valid,csum,
Definition: EthMacPkg.vhd:129
sl filtEnable
Definition: EthMacPkg.vhd:70
natural range 0 to 8 TUSER_BITS_C
sl rxPauseCnt
Definition: EthMacPkg.vhd:92
array(natural range <> ) of slv( 15 downto 0) Slv16Array
Definition: StdRtlPkg.vhd:395
sl rxCountEn
Definition: EthMacPkg.vhd:94
slv( 15 downto 0) pauseTime
Definition: EthMacPkg.vhd:72
slv( 15 downto 0) := x"0081" VLAN_TYPE_C
Definition: EthMacPkg.vhd:36
slv( 7 downto 0) := x"11" UDP_C
Definition: EthMacPkg.vhd:39
sl rxOverFlow
Definition: EthMacPkg.vhd:95
slv( 47 downto 0) := x"020300564400" EMAC_ADDR_INIT_C
Definition: EthMacPkg.vhd:31
sl tcpCsumEn
Definition: EthMacPkg.vhd:74
EthMacConfigType :=(macAddress => EMAC_ADDR_INIT_C,filtEnable => '1',pauseEnable => '1',pauseTime => x"00FF",ipCsumEn => '1',tcpCsumEn => '1',udpCsumEn => '1',dropOnPause => '0') ETH_MAC_CONFIG_INIT_C
Definition: EthMacPkg.vhd:78
integer := 1 EMAC_IPERR_BIT_C
Definition: EthMacPkg.vhd:53
array(natural range <> ) of slv( 7 downto 0) Slv8Array
Definition: StdRtlPkg.vhd:403
array(natural range <> ) of EthMacStatusType EthMacStatusArray
Definition: EthMacPkg.vhd:111
AxiStreamConfigType :=(TSTRB_EN_C => false,TDATA_BYTES_C => 16,TDEST_BITS_C => 8,TID_BITS_C => 0,TKEEP_MODE_C => TKEEP_COMP_C,TUSER_BITS_C => 4,TUSER_MODE_C => TUSER_FIRST_LAST_C) EMAC_AXIS_CONFIG_C
Definition: EthMacPkg.vhd:58
sl txUnderRunCnt
Definition: EthMacPkg.vhd:98
array(natural range <> ) of EthMacCsumAccumType EthMacCsumAccumArray
Definition: EthMacPkg.vhd:125
std_logic_vector slv
Definition: StdRtlPkg.vhd:29
slv( 7 downto 0) := x"01" ICMP_C
Definition: EthMacPkg.vhd:41