3 # This script cleans up the "official" Broadcom hsi_struct_defs.h file as distributed
4 # to something somewhat more programmer friendly.
10 print "Input file not specified (should be path to hsi_struct_defs.h)\n";
14 if (!open(IN, "<", $ARGV[0])) {
15 print "Failure to open input file\n";
19 if (!open(OUT, ">", "hsi_struct_def.h")) {
20 print "Failure to open output file\n";
28 print OUT <<END_OF_NOTICE;
32 * Copyright (c) 2016 Broadcom, All Rights Reserved.
33 * The term Broadcom refers to Broadcom Limited and/or its subsidiaries
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * * Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * * Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in
42 * the documentation and/or other materials provided with the
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
49 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/cdefs.h>
59 __FBSDID("\$FreeBSD\$");
63 # Convert line endings
64 $header =~ s/\r\n/\n/gs;
66 # Convert arrays of two u32_t to a single uint64_t
67 $header =~ s/\bu32_t(\s+[a-zA-Z0-9_]+)\[2\]/uint64_t$1/gs;
69 # Convert uint32_t *_lo/uint32_t *_hi to a single uint64_t
70 $header =~ s/\bu32_t(\s+[a-zA-Z0-9_]+)_lo;\s*\r?\n\s*u32_t(\s+[a-zA-Z0-9_]+)_hi/uint64_t$1/gs;
73 $header =~ s/\bu([0-9]+)_t\b/uint$1_t/gs;
76 $header =~ s/\b((?:0x)?[0-9a-f]+)UL/UINT32_C($1)/gs;
79 #$header =~ s/^(\s*[^\/\s][^\/]+?)\s*\/\*.*?\*\/\s*?$/$1/gm;
80 #$header =~ s/[ \t]*\/\*.*?\*\/\s*?\n?//gs;
83 #$header =~ s/}(\s+)([^\s]+_t[,;])/} __attribute__((packed))$1$2/gs;
86 $header =~ s/( ) +(#define)/$1$2/gs;
87 $header =~ s/^(}[^\n]*;)\n([^\n])/$1\n\n$2/gsm;
88 $header =~ s/([^\n])\n(typedef)/$1\n\n$2/gs;
91 $header =~ s/([^\s]\t+) +/$1/g;
93 # Remove typedefs and pack structs
94 $header =~ s/^typedef struct (.*?)\n{\n(.*?)}[^\n]*;/struct $1 {\n$2} __attribute__((packed));/gsm;
100 if(!open(OUT, ">", "hsi_struct_decode.c")) {
101 print "Failure to open decoder output file\n";
105 print OUT <<END_OF_NOTICE;
109 * Copyright (c) 2016 Broadcom, All Rights Reserved.
110 * The term Broadcom refers to Broadcom Limited and/or its subsidiaries
112 * Redistribution and use in source and binary forms, with or without
113 * modification, are permitted provided that the following conditions
115 * * Redistributions of source code must retain the above copyright
116 * notice, this list of conditions and the following disclaimer.
117 * * Redistributions in binary form must reproduce the above copyright
118 * notice, this list of conditions and the following disclaimer in
119 * the documentation and/or other materials provided with the
122 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
123 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
124 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
125 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
126 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
127 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
128 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
129 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
130 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
131 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
132 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135 #include <sys/cdefs.h>
136 __FBSDID("\$FreeBSD\$");
140 if(!open(HDR, ">", "hsi_struct_decode.h")) {
141 print "Failure to open decoder output header file\n";
145 print HDR <<END_OF_NOTICE;
149 * Copyright(c) 2014-2015 Broadcom Corporation.
150 * All rights reserved.
152 * Redistribution and use in source and binary forms, with or without
153 * modification, are permitted provided that the following conditions
156 * * Redistributions of source code must retain the above copyright
157 * notice, this list of conditions and the following disclaimer.
158 * * Redistributions in binary form must reproduce the above copyright
159 * notice, this list of conditions and the following disclaimer in
160 * the documentation and/or other materials provided with the
162 * * Neither the name of Broadcom Corporation nor the names of its
163 * contributors may be used to endorse or promote products derived
164 * from this software without specific prior written permission.
166 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
167 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
168 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
169 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
170 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
171 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
172 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
173 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
174 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
175 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
176 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
181 print OUT "#ifdef HSI_DEBUG\n#include <inttypes.h>\n#include <rte_common.h>\n#include <rte_log.h>\n#include \"hsi_struct_def_dpdk.h\"\n#include \"hsi_struct_decode.h\"\n#include \"hsi_struct_decode.h\"\n\n";
182 print HDR "#ifdef HSI_DEBUG\n#include \"hsi_struct_def_dpdk.h\"\n\n";
190 my $max_field_len=shift;
192 my $macroshash = shift;
193 my %macros = %$macroshash;
195 my @macros = @$macrosref;
197 my @fields = @$macrosref;
199 if ($type eq 'uint32_t') {
200 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%08\"PRIX32\"\\n\", data->$field);\n",$max_field_len,$field;
202 elsif ($type eq 'uint16_t') {
203 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%04\"PRIX16\"\\n\", data->$field);\n",$max_field_len,$field;
205 elsif ($type eq 'uint8_t') {
206 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%02\"PRIX8\"\\n\", data->$field);\n",$max_field_len,$field;
208 elsif ($type eq 'uint64_t') {
209 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%016\"PRIX64\"\\n\", data->$field);\n",$max_field_len,$field;
211 elsif ($type eq 'char') {
212 if ($field =~ s/\[([0-9]+)\]//) {
213 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = \\\"%%.$1s\\\"\\n\", data->$field);\n",$max_field_len,$field;
216 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%02\"PRIX8\"\\n\", data->$field);\n",$max_field_len,$field;
220 print "Unhandled type: '$type'\n";
223 my $macro_prefix = uc($name).'_'.uc($field).'_';
224 # Special handling for the common flags_type field
225 $macro_prefix =~ s/FLAGS_TYPE_$/FLAGS_/ if ($field eq 'flags_type');
226 # Special handling for _hi types
227 $macro_prefix =~ s/_HI_/_/ if ($name =~ /_hi$/);
229 $macro_prefix =~ s/\[[0-9]+\]//;
231 my $vmacros_have_mask = 0;
234 my $all_single_bits=1;
236 foreach my $macro (@macros) {
237 if ($macro =~ /^$macro_prefix(.*)_MASK$/) {
239 my $maskdef = $macros{$macro};
241 my $subfield_value = "(data->$field & $macro)";
242 if (defined $macros{"$macro_prefix$subfield\_SFT"}) {
243 $subfield_value = "($subfield_value >> $macro_prefix$subfield\_SFT)";
245 $maskdef =~ s/[x0 ]//g;
246 if ($type eq 'uint64_t') {
247 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfield = %%0*\" PRIX64 \"\\n\", %u, $subfield_value);\n", $max_field_len, '', length($maskdef);
250 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfield = %%0*X\\n\", %u, $subfield_value);\n", $max_field_len, '', length($maskdef);
252 delete $$macroshash{$macro};
254 elsif ($macro =~ /^$macro_prefix(.*)_SFT$/) {
255 delete $$macroshash{$macro};
257 elsif ($macro =~ /^$macro_prefix\MASK$/) {
258 $vmacros_have_mask = 1;
259 delete $$macroshash{$macro};
261 elsif ($macro =~ /^$macro_prefix(.*)$/) {
265 # Check for longer fields with the same base... ie: link and link_speed
266 foreach my $extra_field (@fields) {
267 next if ($extra_field eq $field);
268 if ($extra_field =~ /^$field/) {
269 my $extra_prefix = uc($name).'_'.uc($extra_field).'_';
270 next MACRO if ($macro =~ /^$extra_prefix/);
274 push @vmacros, $macro;
275 my $macroeval = $macros{$macro};
276 $macroeval =~ s/UINT32_C\((.*?)\)/$1/g;
277 $vmacros{$macro} = eval("$macroeval");
278 $subfields{$macro} = $subfield;
280 $all_single_bits = 0 if ($vmacros{$macro} & ($vmacros{$macro}-1));
281 $all_single_bits = 0 if ($vmacros{$macro} == 0);
284 if ($all_single_bits) {
285 foreach my $macro (@vmacros) {
286 my $subfield_value = "(data->$field & $macro)";
287 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfields{$macro} : %%s\\n\", $subfield_value?\"ON\":\"OFF\");\n", $max_field_len, '';
288 delete $$macroshash{$macro};
292 printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s Value : %%s\\n\",\n", $max_field_len, '';
293 foreach my $macro (@vmacros) {
294 my $subfield_value = "data->$field";
295 $subfield_value = "($subfield_value & $macro_prefix\MASK)" if $vmacros_have_mask;
296 print OUT "\t\t$subfield_value == $macro ? \"$subfields{$macro}\" :\n";
297 delete $$macroshash{$macro};
299 print OUT "\t\t\"Unknown\");\n";
303 while ($header =~ /^typedef\s+struct\s+(.*?)\s+{(.*?)^}/msg) {
304 my ($name,$def) = ($1, $2);
311 # First, pull out all the fields in order...
312 while($def =~ /^\s*([^\s#\/]+?)\s+([^;\/\s]+?)\s*;/mg) {
313 my ($type, $name) = ($1, $2);
316 $max_field_len = length($name) if length($name) > $max_field_len;
318 # Now, pull out the macros...
319 while($def =~ /^\s*\#define\s+([^\s]+?)\s+(.*?)\s*$/mg) {
324 # Now, generate code to print the struct...
325 print OUT "void decode_$name(const char *string __rte_unused, struct $name *data) {\n\tRTE_LOG(DEBUG, PMD, \"$name\\n\");\n";
326 print HDR "void decode_$name(const char *string __rte_unused, struct $name *data);\n";
327 $hdr_defs .= "#define decode_$name(x, y) {}\n";
328 foreach my $field (@fields) {
329 if ($field =~ /\[([0-9]+)\]/) {
330 if ($type{$field} eq 'char') {
331 print_single_val($field, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields);
334 foreach my $idx (0..$1-1) {
336 $item =~ s/\[[0-9]+\]/[$idx]/;
337 print_single_val($item, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields);
342 print_single_val($field, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields);
345 # print "Unhandled macros:\n",join("\n", keys %macros),"\n" if (keys %macros > 0);
348 print OUT "#endif\n";
352 print HDR "#endif\n";