Path: blob/main/sys/dev/bnxt/bnxt_en/convert_hsi.pl
39536 views
#!/usr/bin/env perl12# This script cleans up the "official" Broadcom hsi_struct_defs.h file as distributed3# to something somewhat more programmer friendly.4#56my $do_decode = 0;78if (! -f $ARGV[0]) {9print "Input file not specified (should be path to hsi_struct_defs.h)\n";10exit 1;11}1213if (!open(IN, "<", $ARGV[0])) {14print "Failure to open input file\n";15exit 1;16}1718if (!open(OUT, ">", "hsi_struct_def.h")) {19print "Failure to open output file\n";20exit 1;21}2223$/=undef;24my $header = <IN>;25close IN;2627print OUT <<END_OF_NOTICE;28/*-29* BSD LICENSE30*31* Copyright (c) 2016 Broadcom, All Rights Reserved.32* The term Broadcom refers to Broadcom Limited and/or its subsidiaries33*34* Redistribution and use in source and binary forms, with or without35* modification, are permitted provided that the following conditions36* are met:37* * Redistributions of source code must retain the above copyright38* notice, this list of conditions and the following disclaimer.39* * Redistributions in binary form must reproduce the above copyright40* notice, this list of conditions and the following disclaimer in41* the documentation and/or other materials provided with the42* distribution.43*44* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS45* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT46* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR47* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT48* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,49* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT50* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,51* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY52* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT53* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE54* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.55*/5657END_OF_NOTICE5859# Convert line endings60$header =~ s/\r\n/\n/gs;6162# Convert arrays of two u32_t to a single uint64_t63$header =~ s/\bu32_t(\s+[a-zA-Z0-9_]+)\[2\]/uint64_t$1/gs;6465# Convert uint32_t *_lo/uint32_t *_hi to a single uint64_t66$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;6768# Convert types69$header =~ s/\bu([0-9]+)_t\b/uint$1_t/gs;7071# Convert literals72$header =~ s/\b((?:0x)?[0-9a-f]+)UL/UINT32_C($1)/gs;7374# Strip comments75#$header =~ s/^(\s*[^\/\s][^\/]+?)\s*\/\*.*?\*\/\s*?$/$1/gm;76#$header =~ s/[ \t]*\/\*.*?\*\/\s*?\n?//gs;7778# Pack structs79#$header =~ s/}(\s+)([^\s]+_t[,;])/} __attribute__((packed))$1$2/gs;8081# Normalize indent82$header =~ s/( ) +(#define)/$1$2/gs;83$header =~ s/^(}[^\n]*;)\n([^\n])/$1\n\n$2/gsm;84$header =~ s/([^\n])\n(typedef)/$1\n\n$2/gs;85$header =~ s/ /\t/g;86$header =~ s/ /\t/g;87$header =~ s/([^\s]\t+) +/$1/g;8889# Remove typedefs and pack structs90$header =~ s/^typedef struct (.*?)\n{\n(.*?)}[^\n]*;/struct $1 {\n$2} __attribute__((packed));/gsm;9192print OUT $header;93close OUT;9495if ($do_decode) {96if(!open(OUT, ">", "hsi_struct_decode.c")) {97print "Failure to open decoder output file\n";98exit 1;99}100101print OUT <<END_OF_NOTICE;102/*-103* BSD LICENSE104*105* Copyright (c) 2016 Broadcom, All Rights Reserved.106* The term Broadcom refers to Broadcom Limited and/or its subsidiaries107*108* Redistribution and use in source and binary forms, with or without109* modification, are permitted provided that the following conditions110* are met:111* * Redistributions of source code must retain the above copyright112* notice, this list of conditions and the following disclaimer.113* * Redistributions in binary form must reproduce the above copyright114* notice, this list of conditions and the following disclaimer in115* the documentation and/or other materials provided with the116* distribution.117*118* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS119* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT120* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR121* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT122* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,123* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT124* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,125* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY126* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT127* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE128* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.129*/130131END_OF_NOTICE132133if(!open(HDR, ">", "hsi_struct_decode.h")) {134print "Failure to open decoder output header file\n";135exit 1;136}137138print HDR <<END_OF_NOTICE;139/*-140* BSD LICENSE141*142* Copyright(c) 2014-2015 Broadcom Corporation.143* All rights reserved.144*145* Redistribution and use in source and binary forms, with or without146* modification, are permitted provided that the following conditions147* are met:148*149* * Redistributions of source code must retain the above copyright150* notice, this list of conditions and the following disclaimer.151* * Redistributions in binary form must reproduce the above copyright152* notice, this list of conditions and the following disclaimer in153* the documentation and/or other materials provided with the154* distribution.155* * Neither the name of Broadcom Corporation nor the names of its156* contributors may be used to endorse or promote products derived157* from this software without specific prior written permission.158*159* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS160* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT161* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR162* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT163* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,164* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT165* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,166* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY167* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT168* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE169* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.170*/171172END_OF_NOTICE173174print 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";175print HDR "#ifdef HSI_DEBUG\n#include \"hsi_struct_def_dpdk.h\"\n\n";176177my $hdr_defs = '';178179sub print_single_val180{181my $field=shift;182my $type=shift;183my $max_field_len=shift;184my $name = shift;185my $macroshash = shift;186my %macros = %$macroshash;187$macrosref = shift;188my @macros = @$macrosref;189$macrosref = shift;190my @fields = @$macrosref;191192if ($type eq 'uint32_t') {193printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%08\"PRIX32\"\\n\", data->$field);\n",$max_field_len,$field;194}195elsif ($type eq 'uint16_t') {196printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%04\"PRIX16\"\\n\", data->$field);\n",$max_field_len,$field;197}198elsif ($type eq 'uint8_t') {199printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%02\"PRIX8\"\\n\", data->$field);\n",$max_field_len,$field;200}201elsif ($type eq 'uint64_t') {202printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%016\"PRIX64\"\\n\", data->$field);\n",$max_field_len,$field;203}204elsif ($type eq 'char') {205if ($field =~ s/\[([0-9]+)\]//) {206printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = \\\"%%.$1s\\\"\\n\", data->$field);\n",$max_field_len,$field;207}208else {209printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s = 0x%%02\"PRIX8\"\\n\", data->$field);\n",$max_field_len,$field;210}211}212else {213print "Unhandled type: '$type'\n";214}215216my $macro_prefix = uc($name).'_'.uc($field).'_';217# Special handling for the common flags_type field218$macro_prefix =~ s/FLAGS_TYPE_$/FLAGS_/ if ($field eq 'flags_type');219# Special handling for _hi types220$macro_prefix =~ s/_HI_/_/ if ($name =~ /_hi$/);221222$macro_prefix =~ s/\[[0-9]+\]//;223my %vmacros;224my $vmacros_have_mask = 0;225my @vmacros;226my %subfields;227my $all_single_bits=1;228MACRO:229foreach my $macro (@macros) {230if ($macro =~ /^$macro_prefix(.*)_MASK$/) {231my $macro = $&;232my $maskdef = $macros{$macro};233my $subfield = $1;234my $subfield_value = "(data->$field & $macro)";235if (defined $macros{"$macro_prefix$subfield\_SFT"}) {236$subfield_value = "($subfield_value >> $macro_prefix$subfield\_SFT)";237}238$maskdef =~ s/[x0 ]//g;239if ($type eq 'uint64_t') {240printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfield = %%0*\" PRIX64 \"\\n\", %u, $subfield_value);\n", $max_field_len, '', length($maskdef);241}242else {243printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfield = %%0*X\\n\", %u, $subfield_value);\n", $max_field_len, '', length($maskdef);244}245delete $$macroshash{$macro};246}247elsif ($macro =~ /^$macro_prefix(.*)_SFT$/) {248delete $$macroshash{$macro};249}250elsif ($macro =~ /^$macro_prefix\MASK$/) {251$vmacros_have_mask = 1;252delete $$macroshash{$macro};253}254elsif ($macro =~ /^$macro_prefix(.*)$/) {255my $macro = $&;256my $subfield = $1;257258# Check for longer fields with the same base... ie: link and link_speed259foreach my $extra_field (@fields) {260next if ($extra_field eq $field);261if ($extra_field =~ /^$field/) {262my $extra_prefix = uc($name).'_'.uc($extra_field).'_';263next MACRO if ($macro =~ /^$extra_prefix/);264}265}266267push @vmacros, $macro;268my $macroeval = $macros{$macro};269$macroeval =~ s/UINT32_C\((.*?)\)/$1/g;270$vmacros{$macro} = eval("$macroeval");271$subfields{$macro} = $subfield;272273$all_single_bits = 0 if ($vmacros{$macro} & ($vmacros{$macro}-1));274$all_single_bits = 0 if ($vmacros{$macro} == 0);275}276}277if ($all_single_bits) {278foreach my $macro (@vmacros) {279my $subfield_value = "(data->$field & $macro)";280printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s $subfields{$macro} : %%s\\n\", $subfield_value?\"ON\":\"OFF\");\n", $max_field_len, '';281delete $$macroshash{$macro};282}283}284else {285printf OUT "\tRTE_LOG(DEBUG, PMD, \" % *s Value : %%s\\n\",\n", $max_field_len, '';286foreach my $macro (@vmacros) {287my $subfield_value = "data->$field";288$subfield_value = "($subfield_value & $macro_prefix\MASK)" if $vmacros_have_mask;289print OUT "\t\t$subfield_value == $macro ? \"$subfields{$macro}\" :\n";290delete $$macroshash{$macro};291}292print OUT "\t\t\"Unknown\");\n";293}294}295296while ($header =~ /^typedef\s+struct\s+(.*?)\s+{(.*?)^}/msg) {297my ($name,$def) = ($1, $2);298my @fields=();299my %type=();300my @macros=();301my %macros=();302my $max_field_len=0;303304# First, pull out all the fields in order...305while($def =~ /^\s*([^\s#\/]+?)\s+([^;\/\s]+?)\s*;/mg) {306my ($type, $name) = ($1, $2);307push @fields, $name;308$type{$name}=$type;309$max_field_len = length($name) if length($name) > $max_field_len;310}311# Now, pull out the macros...312while($def =~ /^\s*\#define\s+([^\s]+?)\s+(.*?)\s*$/mg) {313push @macros, $1;314$macros{$1}=$2;315}316317# Now, generate code to print the struct...318print OUT "void decode_$name(const char *string __rte_unused, struct $name *data) {\n\tRTE_LOG(DEBUG, PMD, \"$name\\n\");\n";319print HDR "void decode_$name(const char *string __rte_unused, struct $name *data);\n";320$hdr_defs .= "#define decode_$name(x, y) {}\n";321foreach my $field (@fields) {322if ($field =~ /\[([0-9]+)\]/) {323if ($type{$field} eq 'char') {324print_single_val($field, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields);325}326else {327foreach my $idx (0..$1-1) {328my $item = $field;329$item =~ s/\[[0-9]+\]/[$idx]/;330print_single_val($item, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields);331}332}333}334else {335print_single_val($field, $type{$field}, $max_field_len, $name, \%macros, \@macros, \@fields);336}337}338# print "Unhandled macros:\n",join("\n", keys %macros),"\n" if (keys %macros > 0);339print OUT "}\n\n";340}341print OUT "#endif\n";342343print HDR "#else\n";344print HDR $hdr_defs;345print HDR "#endif\n";346close OUT;347close HDR;348}349350351