1//
2// Copyright 2018 Ettus Research, A National Instruments Company
3//
4// SPDX-License-Identifier: LGPL-3.0-or-later
5//
6
7// =============================================================
8//  Stream Status Bitfields
9// =============================================================
10
11// -----------------------
12//  Line 0
13// -----------------------
14// Bits     Name            Meaning
15// ----     ----            -------
16// 63:24    capacity_bytes  Downstream buffer capacity in bytes
17// 23:20    <reserved>
18// 19:16    status          Stream status code (enumeration)
19// 15:0     src_epid        Endpoint ID of the source of this msg
20
21// -----------------------
22//  Line 1
23// -----------------------
24// Bits     Name            Meaning
25// ----     ----            -------
26// 63:24    xfercnt_pkts    Transfer count in packets
27// 23:0     capacity_pkts   Downstream buffer capacity in packets
28
29// -----------------------
30//  Line 2
31// -----------------------
32// Bits     Name            Meaning
33// ----     ----            -------
34// 63:0     xfercnt_bytes   Transfer count in bytes
35
36// -----------------------
37//  Line 3
38// -----------------------
39// Bits     Name            Meaning
40// ----     ----            -------
41// 63:16    status_info     Extended information about status (diagnostic only)
42// 15:0     buff_info       Extended information about buffer state (diagnostic only)
43
44localparam [3:0] CHDR_STRS_STATUS_OKAY    = 4'd0; // No error
45localparam [3:0] CHDR_STRS_STATUS_CMDERR  = 4'd1; // Cmd execution failed
46localparam [3:0] CHDR_STRS_STATUS_SEQERR  = 4'd2; // Sequence number discontinuity
47localparam [3:0] CHDR_STRS_STATUS_DATAERR = 4'd3; // Data integrity check failed
48localparam [3:0] CHDR_STRS_STATUS_RTERR   = 4'd4; // Unexpected destination
49
50// 64-bit fields
51function [39:0] chdr64_strs_get_capacity_bytes(input [63:0] header);
52  chdr64_strs_get_capacity_bytes = header[63:24];
53endfunction
54
55function [3:0] chdr64_strs_get_status(input [63:0] header);
56  chdr64_strs_get_status = header[19:16];
57endfunction
58
59function [15:0] chdr64_strs_get_src_epid(input [63:0] header);
60  chdr64_strs_get_src_epid = header[15:0];
61endfunction
62
63function [39:0] chdr64_strs_get_xfercnt_pkts(input [63:0] header);
64  chdr64_strs_get_xfercnt_pkts = header[63:24];
65endfunction
66
67function [23:0] chdr64_strs_get_capacity_pkts(input [63:0] header);
68  chdr64_strs_get_capacity_pkts = header[23:0];
69endfunction
70
71function [63:0] chdr64_strs_get_xfercnt_bytes(input [63:0] header);
72  chdr64_strs_get_xfercnt_bytes = header[63:0];
73endfunction
74
75function [47:0] chdr64_strs_get_status_info(input [63:0] header);
76  chdr64_strs_get_status_info = header[63:16];
77endfunction
78
79function [15:0] chdr64_strs_get_buff_info(input [63:0] header);
80  chdr64_strs_get_buff_info = header[15:0];
81endfunction
82
83
84// 128-bit fields
85function [39:0] chdr128_strs_get_capacity_bytes(input [127:0] header);
86  chdr128_strs_get_capacity_bytes = chdr64_strs_get_capacity_bytes(header[63:0]);
87endfunction
88
89function [3:0] chdr128_strs_get_status(input [127:0] header);
90  chdr128_strs_get_status = chdr64_strs_get_status(header[63:0]);
91endfunction
92
93function [15:0] chdr128_strs_get_src_epid(input [127:0] header);
94  chdr128_strs_get_src_epid = chdr64_strs_get_src_epid(header[63:0]);
95endfunction
96
97function [23:0] chdr128_strs_get_capacity_pkts(input [127:0] header);
98  chdr128_strs_get_capacity_pkts = chdr64_strs_get_capacity_pkts(header[127:64]);
99endfunction
100
101function [39:0] chdr128_strs_get_xfercnt_pkts(input [127:0] header);
102  chdr128_strs_get_xfercnt_pkts = chdr64_strs_get_xfercnt_pkts(header[127:64]);
103endfunction
104
105function [63:0] chdr128_strs_get_xfercnt_bytes(input [127:0] header);
106  chdr128_strs_get_xfercnt_bytes = chdr64_strs_get_xfercnt_bytes(header[63:0]);
107endfunction
108
109function [47:0] chdr128_strs_get_status_info(input [127:0] header);
110  chdr128_strs_get_status_info = chdr64_strs_get_status_info(header[127:64]);
111endfunction
112
113function [15:0] chdr128_strs_get_buff_info(input [127:0] header);
114  chdr128_strs_get_buff_info = chdr64_strs_get_buff_info(header[127:64]);
115endfunction
116
117
118// 256-bit fields
119function [39:0] chdr256_strs_get_capacity_bytes(input [255:0] header);
120  chdr256_strs_get_capacity_bytes = chdr64_strs_get_capacity_bytes(header[63:0]);
121endfunction
122
123function [3:0] chdr256_strs_get_status(input [255:0] header);
124  chdr256_strs_get_status = chdr64_strs_get_status(header[63:0]);
125endfunction
126
127function [15:0] chdr256_strs_get_src_epid(input [255:0] header);
128  chdr256_strs_get_src_epid = chdr64_strs_get_src_epid(header[63:0]);
129endfunction
130
131function [23:0] chdr256_strs_get_capacity_pkts(input [255:0] header);
132  chdr256_strs_get_capacity_pkts = chdr64_strs_get_capacity_pkts(header[127:64]);
133endfunction
134
135function [39:0] chdr256_strs_get_xfercnt_pkts(input [255:0] header);
136  chdr256_strs_get_xfercnt_pkts = chdr64_strs_get_xfercnt_pkts(header[127:64]);
137endfunction
138
139function [63:0] chdr256_strs_get_xfercnt_bytes(input [255:0] header);
140  chdr256_strs_get_xfercnt_bytes = chdr64_strs_get_xfercnt_bytes(header[191:128]);
141endfunction
142
143function [47:0] chdr256_strs_get_status_info(input [255:0] header);
144  chdr256_strs_get_status_info = chdr64_strs_get_status_info(header[255:192]);
145endfunction
146
147function [15:0] chdr256_strs_get_buff_info(input [255:0] header);
148  chdr256_strs_get_buff_info = chdr64_strs_get_buff_info(header[255:192]);
149endfunction
150
151// Stream Status Setter Functions
152//
153
154// 64-bit fields
155function [63:0] chdr64_strs_build_w0(
156  input [39:0] capacity_bytes,
157  input [3:0]  status,
158  input [15:0] src_epid
159);
160  chdr64_strs_build_w0 = {capacity_bytes, 4'h0, status, src_epid};
161endfunction
162
163function [63:0] chdr64_strs_build_w1(
164  input [39:0] xfercnt_pkts,
165  input [23:0] capacity_pkts
166);
167  chdr64_strs_build_w1 = {xfercnt_pkts, capacity_pkts};
168endfunction
169
170function [63:0] chdr64_strs_build_w2(
171  input [63:0] xfercnt_bytes
172);
173  chdr64_strs_build_w2 = xfercnt_bytes;
174endfunction
175
176function [63:0] chdr64_strs_build_w3(
177  input [47:0] status_info,
178  input [15:0] buff_info
179);
180  chdr64_strs_build_w3 = {status_info, buff_info};
181endfunction
182
183// 128-bit fields
184function [127:0] chdr128_strs_build_w0(
185  input [39:0] xfercnt_pkts,
186  input [23:0] capacity_pkts,
187  input [39:0] capacity_bytes,
188  input [3:0]  status,
189  input [15:0] src_epid
190);
191  chdr128_strs_build_w0 = {
192    chdr64_strs_build_w1(xfercnt_pkts, capacity_pkts),
193    chdr64_strs_build_w0(capacity_bytes, status, src_epid)};
194endfunction
195
196function [127:0] chdr128_strs_build_w1(
197  input [47:0] status_info,
198  input [15:0] buff_info,
199  input [63:0] xfercnt_bytes
200);
201  chdr128_strs_build_w1 = {
202    chdr64_strs_build_w3(status_info, buff_info),
203    chdr64_strs_build_w2(xfercnt_bytes)};
204endfunction
205
206// 256-bit fields
207function [255:0] chdr256_strs_build(
208  input [47:0] status_info,
209  input [15:0] buff_info,
210  input [63:0] xfercnt_bytes,
211  input [39:0] xfercnt_pkts,
212  input [23:0] capacity_pkts,
213  input [39:0] capacity_bytes,
214  input [3:0]  status,
215  input [15:0] src_epid
216);
217  chdr256_strs_build = {
218    chdr64_strs_build_w3(status_info, buff_info),
219    chdr64_strs_build_w2(xfercnt_bytes),
220    chdr64_strs_build_w1(xfercnt_pkts, capacity_pkts),
221    chdr64_strs_build_w0(capacity_bytes, status, src_epid)};
222endfunction
223
224// =============================================================
225//  Stream Command Bitfields
226// =============================================================
227
228// -----------------------
229//  Line 0
230// -----------------------
231// Bits     Name            Meaning
232// ----     ----            -------
233// 63:24    num_pkts        Downstream buffer capacity in bytes
234// 23:20    op_data         Payload for command
235// 19:16    op_code         Command operation code (enumeration)
236// 15:0     src_epid        Endpoint ID of the source of this msg
237
238// -----------------------
239//  Line 1
240// -----------------------
241// Bits     Name            Meaning
242// ----     ----            -------
243// 63:0     num_bytes       Transfer count in packets
244
245localparam [3:0] CHDR_STRC_OPCODE_INIT    = 4'd0;
246localparam [3:0] CHDR_STRC_OPCODE_PING    = 4'd1;
247localparam [3:0] CHDR_STRC_OPCODE_RESYNC  = 4'd2;
248
249// 64-bit fields
250function [39:0] chdr64_strc_get_num_pkts(input [63:0] header);
251  chdr64_strc_get_num_pkts = header[63:24];
252endfunction
253
254function [3:0] chdr64_strc_get_op_data(input [63:0] header);
255  chdr64_strc_get_op_data = header[23:20];
256endfunction
257
258function [3:0] chdr64_strc_get_op_code(input [63:0] header);
259  chdr64_strc_get_op_code = header[19:16];
260endfunction
261
262function [15:0] chdr64_strc_get_src_epid(input [63:0] header);
263  chdr64_strc_get_src_epid = header[15:0];
264endfunction
265
266function [63:0] chdr64_strc_get_num_bytes(input [63:0] header);
267  chdr64_strc_get_num_bytes = header[63:0];
268endfunction
269
270// 128-bit fields
271function [39:0] chdr128_strc_get_num_pkts(input [127:0] header);
272  chdr128_strc_get_num_pkts = chdr64_strc_get_num_pkts(header[63:0]);
273endfunction
274
275function [3:0] chdr128_strc_get_op_data(input [127:0] header);
276  chdr128_strc_get_op_data = chdr64_strc_get_op_data(header[63:0]);
277endfunction
278
279function [3:0] chdr128_strc_get_op_code(input [127:0] header);
280  chdr128_strc_get_op_code = chdr64_strc_get_op_code(header[63:0]);
281endfunction
282
283function [15:0] chdr128_strc_get_src_epid(input [127:0] header);
284  chdr128_strc_get_src_epid = chdr64_strc_get_src_epid(header[63:0]);
285endfunction
286
287function [63:0] chdr128_strc_get_num_bytes(input [127:0] header);
288  chdr128_strc_get_num_bytes = chdr64_strc_get_num_bytes(header[127:64]);
289endfunction
290
291// Stream Command Setter Functions
292//
293
294// 64-bit fields
295
296function [63:0] chdr64_strc_build_w0(
297  input [39:0] num_pkts,
298  input [3:0]  op_data,
299  input [3:0]  op_code,
300  input [15:0] src_epid
301);
302  chdr64_strc_build_w0 = {num_pkts, op_data, op_code, src_epid};
303endfunction
304
305function [63:0] chdr64_strc_build_w1(
306  input [63:0] num_bytes
307);
308  chdr64_strc_build_w1 = num_bytes;
309endfunction
310
311// 128-bit fields
312function [127:0] chdr128_strc_build(
313  input [63:0] num_bytes,
314  input [39:0] num_pkts,
315  input [3:0]  op_data,
316  input [3:0]  op_code,
317  input [15:0] src_epid
318);
319  chdr128_strc_build = {
320    chdr64_strc_build_w1(num_bytes),
321    chdr64_strc_build_w0(num_pkts, op_data, op_code, src_epid)};
322endfunction
323
324// =============================================================
325//  Management Packet Bitfields
326// =============================================================
327
328// -----------------------
329//  HDR
330// -----------------------
331// Bits     Name          Meaning
332// ----     ----          -------
333// 63:48    proto_ver     Protocol Version
334// 47:45    chdr_w        Bitwidth of the CHDR interface
335// 44:26    <Reserved>
336// 25:16    num_hops      Number of hops that this message will take (TTL)
337// 15:0     src_epid      Endpoint ID of the source of this msg
338
339// -----------------------
340//  OP
341// -----------------------
342// Bits     Name          Meaning
343// ----     ----          -------
344// 63:16    op_payload    Operation Payload
345// 15:8     op_code       Operation code
346// 7:0      ops_pending   Number of operations pending in this hop
347
348localparam [2:0] CHDR_MGMT_WIDTH_64  = 3'd0;
349localparam [2:0] CHDR_MGMT_WIDTH_128 = 3'd1;
350localparam [2:0] CHDR_MGMT_WIDTH_256 = 3'd2;
351localparam [2:0] CHDR_MGMT_WIDTH_512 = 3'd3;
352
353function [2:0] chdr_w_to_enum(input integer bits);
354  if (bits == 512)
355    chdr_w_to_enum = CHDR_MGMT_WIDTH_512;
356  else if (bits == 256)
357    chdr_w_to_enum = CHDR_MGMT_WIDTH_256;
358  else if (bits == 128)
359    chdr_w_to_enum = CHDR_MGMT_WIDTH_128;
360  else
361    chdr_w_to_enum = CHDR_MGMT_WIDTH_64;
362endfunction
363
364localparam [7:0] CHDR_MGMT_OP_NOP         = 8'd0;
365localparam [7:0] CHDR_MGMT_OP_ADVERTISE   = 8'd1;
366localparam [7:0] CHDR_MGMT_OP_SEL_DEST    = 8'd2;
367localparam [7:0] CHDR_MGMT_OP_RETURN      = 8'd3;
368localparam [7:0] CHDR_MGMT_OP_INFO_REQ    = 8'd4;
369localparam [7:0] CHDR_MGMT_OP_INFO_RESP   = 8'd5;
370localparam [7:0] CHDR_MGMT_OP_CFG_WR_REQ  = 8'd6;
371localparam [7:0] CHDR_MGMT_OP_CFG_RD_REQ  = 8'd7;
372localparam [7:0] CHDR_MGMT_OP_CFG_RD_RESP = 8'd8;
373
374function [15:0] chdr_mgmt_get_proto_ver(input [63:0] header);
375  chdr_mgmt_get_proto_ver = header[63:48];
376endfunction
377
378function [2:0] chdr_mgmt_get_chdr_w(input [63:0] header);
379  chdr_mgmt_get_chdr_w = header[47:45];
380endfunction
381
382function [9:0] chdr_mgmt_get_num_hops(input [63:0] header);
383  chdr_mgmt_get_num_hops = header[25:16];
384endfunction
385
386function [15:0] chdr_mgmt_get_src_epid(input [63:0] header);
387  chdr_mgmt_get_src_epid = header[15:0];
388endfunction
389
390function [47:0] chdr_mgmt_get_op_payload(input [63:0] header);
391  chdr_mgmt_get_op_payload = header[63:16];
392endfunction
393
394function [7:0] chdr_mgmt_get_op_code(input [63:0] header);
395  chdr_mgmt_get_op_code = header[15:8];
396endfunction
397
398function [7:0] chdr_mgmt_get_ops_pending(input [63:0] header);
399  chdr_mgmt_get_ops_pending = header[7:0];
400endfunction
401
402function [63:0] chdr_mgmt_build_hdr(
403  input [15:0] proto_ver,
404  input [2:0]  chdr_w,
405  input [9:0]  num_hops,
406  input [15:0] src_epid
407);
408  chdr_mgmt_build_hdr = {proto_ver, chdr_w, 19'h0, num_hops, src_epid};
409endfunction
410
411function [63:0] chdr_mgmt_build_op(
412  input [47:0] op_payload,
413  input [7:0]  op_code,
414  input [7:0]  ops_pending
415);
416  chdr_mgmt_build_op = {op_payload, op_code, ops_pending};
417endfunction
418
419// Definition for the TID field for the output of chdr_mgmt_pkt_handler
420localparam [1:0] CHDR_MGMT_ROUTE_EPID    = 2'd0;  // Route based on EPID
421localparam [1:0] CHDR_MGMT_ROUTE_TDEST   = 2'd1;  // Route based on tdest field
422localparam [1:0] CHDR_MGMT_RETURN_TO_SRC = 2'd2;  // Return packet to sender
423
424// -----------------------
425//  OP specific fields
426// -----------------------
427
428localparam [3:0] NODE_TYPE_INVALID   = 4'd0;
429localparam [3:0] NODE_TYPE_XBAR      = 4'd1;
430localparam [3:0] NODE_TYPE_STREAM_EP = 4'd2;
431localparam [3:0] NODE_TYPE_TRANSPORT = 4'd3;
432
433function [47:0] chdr_mgmt_build_node_info(
434  input [17:0] ext_info,
435  input [9:0]  node_inst,
436  input [3:0]  node_type,
437  input [15:0] device_id
438);
439  chdr_mgmt_build_node_info = {ext_info, node_inst, node_type, device_id};
440endfunction
441
442function [9:0] chdr_mgmt_sel_dest_get_tdest(input [47:0] payload);
443  chdr_mgmt_sel_dest_get_tdest = payload[9:0];
444endfunction
445
446function [15:0] chdr_mgmt_cfg_reg_get_addr(input [47:0] payload);
447  chdr_mgmt_cfg_reg_get_addr = payload[15:0];
448endfunction
449
450function [31:0] chdr_mgmt_cfg_reg_get_data(input [47:0] payload);
451  chdr_mgmt_cfg_reg_get_data = payload[47:16];
452endfunction
453