Blame tools/fpgainfo/main.c

Packit 534379
// Copyright(c) 2018-2020, Intel Corporation
Packit 534379
//
Packit 534379
// Redistribution  and  use  in source  and  binary  forms,  with  or  without
Packit 534379
// modification, are permitted provided that the following conditions are met:
Packit 534379
//
Packit 534379
// * Redistributions of  source code  must retain the  above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer.
Packit 534379
// * Redistributions in binary form must reproduce the above copyright notice,
Packit 534379
//   this list of conditions and the following disclaimer in the documentation
Packit 534379
//   and/or other materials provided with the distribution.
Packit 534379
// * Neither the name  of Intel Corporation  nor the names of its contributors
Packit 534379
//   may be used to  endorse or promote  products derived  from this  software
Packit 534379
//   without specific prior written permission.
Packit 534379
//
Packit 534379
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 534379
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
Packit 534379
// IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 534379
// ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
Packit 534379
// LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
Packit 534379
// CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
Packit 534379
// SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
Packit 534379
// INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
Packit 534379
// CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
Packit 534379
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
Packit 534379
// POSSIBILITY OF SUCH DAMAGE.
Packit 534379
Packit 534379
#ifdef HAVE_CONFIG_H
Packit 534379
#include <config.h>
Packit 534379
#endif // HAVE_CONFIG_H
Packit 534379
#include <errno.h>
Packit 534379
#include <getopt.h>
Packit 534379
#include <stdlib.h>
Packit 534379
#include <string.h>
Packit 534379
#include <locale.h>
Packit 534379
#ifdef _WIN32
Packit 534379
#define EX_OK 0
Packit 534379
#define EX_USAGE (-1)
Packit 534379
#define EX_SOFTWARE (-2)
Packit 534379
#define EX_TEMPFAIL (-3)
Packit 534379
#else
Packit 534379
#include <sysexits.h>
Packit 534379
#endif
Packit 534379
Packit 534379
#include "argsfilter.h"
Packit 534379
#include "opae/fpga.h"
Packit 534379
Packit 534379
#include "fpgainfo.h"
Packit 534379
Packit 534379
#include "errors.h"
Packit 534379
#include "fmeinfo.h"
Packit 534379
#include "portinfo.h"
Packit 534379
#include "tempinfo.h"
Packit 534379
#include "powerinfo.h"
Packit 534379
#include "bmcinfo.h"
Packit 534379
#include "board.h"
Packit 534379
Packit 534379
void help(void);
Packit 534379
Packit 534379
typedef fpga_result (*filter_fn)(fpga_properties *, int, char **);
Packit 534379
typedef fpga_result (*command_fn)(fpga_token *, int, int, char **);
Packit 534379
typedef void (*help_fn)(void);
Packit 534379
Packit 534379
// define a list of command words and
Packit 534379
// function ptrs to the command handler
Packit 534379
static struct command_handler {
Packit 534379
	const char *command;
Packit 534379
	filter_fn filter;
Packit 534379
	command_fn run;
Packit 534379
	help_fn help;
Packit 534379
} cmd_array[] = {
Packit 534379
	{.command = "errors",
Packit 534379
	 .filter = errors_filter,
Packit 534379
	 .run = errors_command,
Packit 534379
	 .help = errors_help},
Packit 534379
	{.command = "power",
Packit 534379
	 .filter = power_filter,
Packit 534379
	 .run = power_command,
Packit 534379
	 .help = power_help},
Packit 534379
	{.command = "temp",
Packit 534379
	 .filter = temp_filter,
Packit 534379
	 .run = temp_command,
Packit 534379
	 .help = temp_help},
Packit 534379
	{.command = "fme",
Packit 534379
	 .filter = fme_filter,
Packit 534379
	 .run = fme_command,
Packit 534379
	 .help = fme_help},
Packit 534379
	{.command = "port",
Packit 534379
	 .filter = port_filter,
Packit 534379
	 .run = port_command,
Packit 534379
	 .help = port_help},
Packit 534379
	{.command = "perf",
Packit 534379
	 .filter = bmc_filter,
Packit 534379
	 .run = perf_command,
Packit 534379
	 .help = perf_help},
Packit 534379
	{.command = "bmc",
Packit 534379
	 .filter = bmc_filter,
Packit 534379
	 .run = bmc_command,
Packit 534379
	 .help = bmc_help},
Packit 534379
	{.command = "mac",
Packit 534379
	 .filter = mac_filter,
Packit 534379
	 .run = mac_command,
Packit 534379
	 .help = mac_help},
Packit 534379
	{.command = "phy",
Packit 534379
	 .filter = phy_filter,
Packit 534379
	 .run = phy_command,
Packit 534379
	 .help = phy_help},
Packit 534379
	{.command = "security",
Packit 534379
	 .filter = sec_filter,
Packit 534379
	 .run = sec_command,
Packit 534379
	 .help = sec_help},
Packit 534379
};
Packit 534379
Packit 534379
/*
Packit 534379
 * Parse command line arguments
Packit 534379
 */
Packit 534379
#define MAIN_GETOPT_STRING "+hv"
Packit 534379
int parse_args(int argc, char *argv[])
Packit 534379
{
Packit 534379
	struct option longopts[] = {
Packit 534379
		{"help", no_argument, NULL, 'h'},
Packit 534379
		{"version", no_argument, NULL, 'v'},
Packit 534379
		{0, 0, 0, 0},
Packit 534379
	};
Packit 534379
Packit 534379
	int getopt_ret = -1;
Packit 534379
	int option_index = 0;
Packit 534379
	if (argc < 2) {
Packit 534379
		help();
Packit 534379
		return EX_USAGE;
Packit 534379
	}
Packit 534379
Packit 534379
	while (-1
Packit 534379
	       != (getopt_ret = getopt_long(argc, argv, MAIN_GETOPT_STRING,
Packit 534379
					    longopts, &option_index))) {
Packit 534379
		const char *tmp_optarg = optarg;
Packit 534379
Packit 534379
		if ((optarg) && ('=' == *tmp_optarg))
Packit 534379
			++tmp_optarg;
Packit 534379
Packit 534379
		switch (getopt_ret) {
Packit 534379
		case 'h': /* help */
Packit 534379
			help();
Packit 534379
			return EX_TEMPFAIL;
Packit 534379
Packit 534379
		case 'v': /* version */
Packit 534379
			printf("fpgainfo %s %s%s\n",
Packit 534379
			       OPAE_VERSION,
Packit 534379
			       OPAE_GIT_COMMIT_HASH,
Packit 534379
			       OPAE_GIT_SRC_TREE_DIRTY ? "*":"");
Packit 534379
			return EX_TEMPFAIL;
Packit 534379
Packit 534379
		case ':': /* missing option argument */
Packit 534379
			OPAE_ERR("Missing option argument\n");
Packit 534379
			return EX_USAGE;
Packit 534379
Packit 534379
		case '?':
Packit 534379
		default: /* invalid option */
Packit 534379
			OPAE_ERR("Invalid cmdline options\n");
Packit 534379
			return EX_USAGE;
Packit 534379
		}
Packit 534379
	}
Packit 534379
Packit 534379
	optind = 0;
Packit 534379
	return EX_OK;
Packit 534379
}
Packit 534379
Packit 534379
struct command_handler *get_command(char *cmd)
Packit 534379
{
Packit 534379
	int cmd_size = sizeof(cmd_array) / sizeof(cmd_array[0]);
Packit 534379
	// find the command handler for the command
Packit 534379
	int i = 0;
Packit 534379
	for (i = 0; i < cmd_size; ++i) {
Packit 534379
		if (!strcmp(cmd, cmd_array[i].command)) {
Packit 534379
			return &cmd_array[i];
Packit 534379
		}
Packit 534379
	}
Packit 534379
	return NULL;
Packit 534379
}
Packit 534379
Packit 534379
/*
Packit 534379
 * Print help
Packit 534379
 */
Packit 534379
void help(void)
Packit 534379
{
Packit 534379
	unsigned int i;
Packit 534379
Packit 534379
	printf("\n"
Packit 534379
	       "fpgainfo\n"
Packit 534379
	       "FPGA information utility\n"
Packit 534379
	       "\n"
Packit 534379
	       "Usage:\n"
Packit 534379
	       "        fpgainfo [-h] [-B <bus>] [-D <device>] "
Packit 534379
	       "[-F <function>] [-S <socket-id>] {");
Packit 534379
	printf("%s", cmd_array[0].command);
Packit 534379
	for (i = 1; i < sizeof(cmd_array) / sizeof(cmd_array[0]); i++) {
Packit 534379
		printf(",%s", cmd_array[i].command);
Packit 534379
	}
Packit 534379
	printf("}\n\n"
Packit 534379
	       "                -h,--help           Print this help\n"
Packit 534379
	       "                -v,--version        Print version and exit\n"
Packit 534379
	       "                -B,--bus            Set target bus number\n"
Packit 534379
	       "                -D,--device         Set target device number\n"
Packit 534379
	       "                -F,--function       Set target function number\n"
Packit 534379
	       "                -S,--socket-id      Set target socket number\n"
Packit 534379
	       "                --segment           Set target segment\n"
Packit 534379
	       "\n");
Packit 534379
Packit 534379
	printf("Subcommands:\n");
Packit 534379
	for (i = 0; i < sizeof(cmd_array) / sizeof(cmd_array[0]); i++) {
Packit 534379
		cmd_array[i].help();
Packit 534379
	}
Packit 534379
}
Packit 534379
Packit 534379
int main(int argc, char *argv[])
Packit 534379
{
Packit 534379
	int ret_value = EX_OK;
Packit 534379
	fpga_result res = FPGA_OK;
Packit 534379
	uint32_t matches = 0;
Packit 534379
	uint32_t i = 0;
Packit 534379
	fpga_properties filter = NULL;
Packit 534379
	fpga_token *tokens = NULL;
Packit 534379
Packit 534379
	if (NULL == setlocale(LC_ALL, "")) {
Packit 534379
		OPAE_ERR("Could not set locale\n");
Packit 534379
		return EX_SOFTWARE;
Packit 534379
	}
Packit 534379
Packit 534379
	// start a filter using the first level command line arguments
Packit 534379
	res = fpgaGetProperties(NULL, &filter);
Packit 534379
	ON_FPGAINFO_ERR_GOTO(res, out_err, "creating properties object");
Packit 534379
Packit 534379
	ret_value = set_properties_from_args(filter, &res, &argc, argv);
Packit 534379
	if (ret_value != EX_OK) {
Packit 534379
		goto out_destroy;
Packit 534379
	}
Packit 534379
Packit 534379
	ret_value = parse_args(argc, argv);
Packit 534379
	if (ret_value != EX_OK) {
Packit 534379
		fpgaDestroyProperties(&filter);
Packit 534379
		return ret_value == EX_TEMPFAIL ? EX_OK : ret_value;
Packit 534379
	}
Packit 534379
Packit 534379
	uint32_t num_tokens = 0;
Packit 534379
	struct command_handler *handler = get_command(argv[1]);
Packit 534379
	if (handler == NULL) {
Packit 534379
		OPAE_ERR("Invalid command specified\n");
Packit 534379
		help();
Packit 534379
		goto out_destroy;
Packit 534379
	}
Packit 534379
	if (handler->filter) {
Packit 534379
		res = handler->filter(&filter, argc, argv);
Packit 534379
		ON_FPGAINFO_ERR_GOTO(res, out_destroy, 0);
Packit 534379
	}
Packit 534379
	res = fpgaEnumerate(&filter, 1, NULL, 0, &matches);
Packit 534379
	ON_FPGAINFO_ERR_GOTO(res, out_destroy, "enumerating resources");
Packit 534379
Packit 534379
	if (0 == matches) {
Packit 534379
		ret_value = EX_SOFTWARE;
Packit 534379
		OPAE_ERR("No FPGA resources found.\n");
Packit 534379
		goto out_destroy;
Packit 534379
	}
Packit 534379
Packit 534379
	num_tokens = matches;
Packit 534379
	tokens = (fpga_token *)malloc(num_tokens * sizeof(fpga_token));
Packit 534379
	res = fpgaEnumerate(&filter, 1, tokens, num_tokens, &matches);
Packit 534379
	ON_FPGAINFO_ERR_GOTO(res, out_destroy_tokens, "enumerating resources");
Packit 534379
	if (num_tokens != matches) {
Packit 534379
		ret_value = EX_SOFTWARE;
Packit 534379
		OPAE_ERR("token list changed in between enumeration calls\n");
Packit 534379
		goto out_destroy_tokens;
Packit 534379
	}
Packit 534379
Packit 534379
	res = handler->run(tokens, matches, argc, argv);
Packit 534379
Packit 534379
out_destroy_tokens:
Packit 534379
	for (i = 0; i < num_tokens; i++) {
Packit 534379
	    fpgaDestroyToken(&tokens[i]);
Packit 534379
	}
Packit 534379
	free(tokens);
Packit 534379
Packit 534379
out_destroy:
Packit 534379
	if (res != FPGA_OK)
Packit 534379
		ret_value = EX_SOFTWARE;
Packit 534379
	fpgaDestroyProperties(&filter); /* not needed anymore */
Packit 534379
out_err:
Packit 534379
	return ret_value;
Packit 534379
}