Blame tools/fpgainfo/fpgainfo.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
#define _GNU_SOURCE
Packit 534379
#include <string.h>
Packit 534379
Packit 534379
#include "fpgainfo.h"
Packit 534379
#include "opae/fpga.h"
Packit 534379
#include <inttypes.h>
Packit 534379
#include <uuid/uuid.h>
Packit 534379
#include <ctype.h>
Packit 534379
#include <limits.h>
Packit 534379
Packit 534379
/*
Packit 534379
 * Print readable error message for fpga_results
Packit 534379
 */
Packit 534379
void fpgainfo_print_err(const char *s, fpga_result res)
Packit 534379
{
Packit 534379
	if (s && res)
Packit 534379
		fprintf(stderr, "Error %s: %s\n", s, fpgaErrStr(res));
Packit 534379
}
Packit 534379
Packit 534379
void fpgainfo_print_common(const char *hdr, fpga_properties props)
Packit 534379
{
Packit 534379
	fpga_result res = FPGA_OK;
Packit 534379
	char guid_str[38] = {0};
Packit 534379
	uint64_t object_id = (uint64_t)-1;
Packit 534379
	uint8_t bus = (uint8_t)-1;
Packit 534379
	uint16_t segment = (uint16_t)-1;
Packit 534379
	uint8_t device = (uint8_t)-1;
Packit 534379
	uint8_t function = (uint8_t)-1;
Packit 534379
	uint16_t device_id = (uint16_t)-1;
Packit 534379
	uint8_t socket_id = (uint8_t)-1;
Packit 534379
	fpga_guid guid = {0};
Packit 534379
	fpga_guid port_guid = {0};
Packit 534379
	uint32_t num_slots = (uint32_t)-1;
Packit 534379
	uint64_t bbs_id = (uint64_t)-1;
Packit 534379
	fpga_version bbs_version = { 0, 0, 0 };
Packit 534379
	fpga_objtype objtype;
Packit 534379
	fpga_properties pprops = props;
Packit 534379
	fpga_token par = NULL;
Packit 534379
	int is_accelerator = 0;
Packit 534379
	bool has_parent = true;
Packit 534379
Packit 534379
	res = fpgaPropertiesGetObjectID(props, &object_id);
Packit 534379
	fpgainfo_print_err("reading object_id from properties", res);
Packit 534379
Packit 534379
	res = fpgaPropertiesGetBus(props, &bus;;
Packit 534379
	fpgainfo_print_err("reading bus from properties", res);
Packit 534379
Packit 534379
	res = fpgaPropertiesGetSegment(props, &segment);
Packit 534379
	fpgainfo_print_err("reading segment from properties", res);
Packit 534379
Packit 534379
	res = fpgaPropertiesGetDevice(props, &device);
Packit 534379
	fpgainfo_print_err("reading device from properties", res);
Packit 534379
Packit 534379
	res = fpgaPropertiesGetFunction(props, &function);
Packit 534379
	fpgainfo_print_err("reading function from properties", res);
Packit 534379
Packit 534379
	res = fpgaPropertiesGetSocketID(props, &socket_id);
Packit 534379
	fpgainfo_print_err("reading socket_id from properties", res);
Packit 534379
Packit 534379
	res = fpgaPropertiesGetDeviceID(props, &device_id);
Packit 534379
	fpgainfo_print_err("reading device_id from properties", res);
Packit 534379
Packit 534379
	res = fpgaPropertiesGetObjectType(props, &objtype);
Packit 534379
	fpgainfo_print_err("reading objtype from properties", res);
Packit 534379
Packit 534379
	if (objtype != FPGA_DEVICE) {
Packit 534379
		res = fpgaPropertiesGetGUID(props, &port_guid);
Packit 534379
		fpgainfo_print_err("reading guid from properties", res);
Packit 534379
		is_accelerator = 1;
Packit 534379
	}
Packit 534379
Packit 534379
	// Go up the tree until we find the device
Packit 534379
	while (objtype != FPGA_DEVICE) {
Packit 534379
		res = fpgaPropertiesGetParent(pprops, &par);
Packit 534379
		if (FPGA_NOT_FOUND == res) {
Packit 534379
			has_parent = false;
Packit 534379
			break;
Packit 534379
		}
Packit 534379
		fpgainfo_print_err("reading objtype from properties", res);
Packit 534379
Packit 534379
		if (pprops != props) {
Packit 534379
			res = fpgaDestroyProperties(pprops);
Packit 534379
			fpgainfo_print_err("destroying parent properties", res);
Packit 534379
			pprops = props;
Packit 534379
		}
Packit 534379
Packit 534379
		res = fpgaGetProperties(par, &pprops);
Packit 534379
		fpgainfo_print_err("reading parent properties", res);
Packit 534379
Packit 534379
		res = fpgaPropertiesGetObjectType(pprops, &objtype);
Packit 534379
		fpgainfo_print_err("reading objtype from properties", res);
Packit 534379
Packit 534379
		res = fpgaDestroyToken(&par);
Packit 534379
		fpgainfo_print_err("destroying parent token", res);
Packit 534379
	};
Packit 534379
Packit 534379
	res = fpgaPropertiesGetDeviceID(pprops, &device_id);
Packit 534379
	fpgainfo_print_err("reading device_id from properties", res);
Packit 534379
Packit 534379
	if (has_parent) {
Packit 534379
Packit 534379
		res = fpgaPropertiesGetGUID(pprops, &guid);
Packit 534379
		fpgainfo_print_err("reading guid from properties", res);
Packit 534379
Packit 534379
		res = fpgaPropertiesGetNumSlots(pprops, &num_slots);
Packit 534379
		fpgainfo_print_err("reading num_slots from properties", res);
Packit 534379
Packit 534379
		res = fpgaPropertiesGetBBSID(pprops, &bbs_id);
Packit 534379
		fpgainfo_print_err("reading bbs_id from properties", res);
Packit 534379
Packit 534379
		res = fpgaPropertiesGetBBSVersion(pprops, &bbs_version);
Packit 534379
		fpgainfo_print_err("reading bbs_version from properties", res);
Packit 534379
	}
Packit 534379
Packit 534379
	// TODO: Implement once model and capabilities accessors are
Packit 534379
	// implemented
Packit 534379
Packit 534379
	// res = fpgaPropertiesGetModel(props, &model);
Packit 534379
	// fpgainfo_print_err("reading model from properties", res);
Packit 534379
Packit 534379
	// res = fpgaPropertiesGetCapabilities(props, &capabilities);
Packit 534379
	// fpgainfo_print_err("reading capabilities from properties", res);
Packit 534379
Packit 534379
	if (pprops != props) {
Packit 534379
		res = fpgaDestroyProperties(&pprops);
Packit 534379
		fpgainfo_print_err("destroying parent properties after use",
Packit 534379
				   res);
Packit 534379
		pprops = props;
Packit 534379
	}
Packit 534379
Packit 534379
	printf("%s\n", hdr);
Packit 534379
	printf("%-32s : 0x%2" PRIX64 "\n", "Object Id", object_id);
Packit 534379
	printf("%-32s : %04X:%02X:%02X:%01X\n", "PCIe s:b:d:f", segment, bus,
Packit 534379
	       device, function);
Packit 534379
	printf("%-32s : 0x%04X\n", "Device Id", device_id);
Packit 534379
	printf("%-32s : 0x%02X\n", "Socket Id", socket_id);
Packit 534379
Packit 534379
	if (has_parent) {
Packit 534379
		printf("%-32s : %02d\n", "Ports Num", num_slots);
Packit 534379
		printf("%-32s : 0x%" PRIX64 "\n", "Bitstream Id", bbs_id);
Packit 534379
		printf("%-32s : %d.%d.%d\n", "Bitstream Version",
Packit 534379
			bbs_version.major, bbs_version.minor, bbs_version.patch);
Packit 534379
		uuid_unparse(guid, guid_str);
Packit 534379
		printf("%-32s : %s\n", "Pr Interface Id", guid_str);
Packit 534379
	}
Packit 534379
Packit 534379
	if (is_accelerator) {
Packit 534379
		uuid_unparse(port_guid, guid_str);
Packit 534379
		printf("%-32s : %s\n", "Accelerator Id", guid_str);
Packit 534379
	}
Packit 534379
Packit 534379
	if (objtype == FPGA_DEVICE) {
Packit 534379
		printf("%-32s : %s\n", "Boot Page",
Packit 534379
			bbs_id & FACTORY_BIT ? "factory" : "user");
Packit 534379
	}
Packit 534379
}
Packit 534379
Packit 534379
// Replace occurrences of character within string
Packit 534379
void replace_chars(char *str, char match, char rep)
Packit 534379
{
Packit 534379
	char *tmp = strchr(str, match);
Packit 534379
	while (tmp) {
Packit 534379
		*tmp = rep;
Packit 534379
		tmp = strchr(tmp + 1, match);
Packit 534379
	}
Packit 534379
}
Packit 534379
Packit 534379
// Turn all "pcie" into "PCIe"
Packit 534379
void upcase_pci(char *str, size_t len)
Packit 534379
{
Packit 534379
	char *tmp;
Packit 534379
Packit 534379
	tmp = strcasestr(str, "pci");
Packit 534379
	while (tmp) {
Packit 534379
		*tmp++ = 'P';
Packit 534379
		*tmp++ = 'C';
Packit 534379
		*tmp++ = 'I';
Packit 534379
		str = tmp + 3;
Packit 534379
		len -= 3;
Packit 534379
		tmp = strcasestr(str, "pci");
Packit 534379
	}
Packit 534379
}
Packit 534379
Packit 534379
// Upper-case the first letter of each word in str
Packit 534379
void upcase_first(char *str)
Packit 534379
{
Packit 534379
	*str = toupper(*str);
Packit 534379
	char *tmp = strchr(str + 1, ' ');
Packit 534379
	while (tmp) {
Packit 534379
		if (tmp[1] && isalpha(tmp[1])) {
Packit 534379
			tmp[1] = toupper(tmp[1]);
Packit 534379
		}
Packit 534379
		tmp = strchr(tmp + 1, ' ');
Packit 534379
	}
Packit 534379
}
Packit 534379
Packit 534379
// TODO: Move this to a common file for reuse in other fpgainfo files
Packit 534379
int str_in_list(const char *key, const char *list[], size_t size)
Packit 534379
{
Packit 534379
	size_t i = 0;
Packit 534379
	for (i = 0; i < size; ++i) {
Packit 534379
		if (!strcmp(key, list[i])) {
Packit 534379
			return (int)i;
Packit 534379
		}
Packit 534379
	}
Packit 534379
	return INT_MAX;
Packit 534379
}