Blame opae-libs/plugins/xfpga/properties.c

Packit 534379
// Copyright(c) 2017-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
Packit 534379
#include <string.h>
Packit 534379
Packit 534379
#include <opae/properties.h>
Packit 534379
Packit 534379
#include "xfpga.h"
Packit 534379
#include "common_int.h"
Packit 534379
#include "props.h"
Packit 534379
#include "error_int.h"
Packit 534379
Packit 534379
Packit 534379
fpga_result __XFPGA_API__
Packit 534379
xfpga_fpgaGetPropertiesFromHandle(fpga_handle handle, fpga_properties *prop)
Packit 534379
{
Packit 534379
	struct _fpga_handle *_handle = (struct _fpga_handle *)handle;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
	int err = 0;
Packit 534379
Packit 534379
	result = handle_check_and_lock(_handle);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	result = xfpga_fpgaGetProperties(_handle->token, prop);
Packit 534379
Packit 534379
	err = pthread_mutex_unlock(&_handle->lock);
Packit 534379
	if (err) {
Packit 534379
		OPAE_ERR("pthread_mutex_unlock() failed: %S", strerror(err));
Packit 534379
	}
Packit 534379
Packit 534379
	return result;
Packit 534379
}
Packit 534379
Packit 534379
fpga_result __XFPGA_API__ xfpga_fpgaGetProperties(fpga_token token,
Packit 534379
						 fpga_properties *prop)
Packit 534379
{
Packit 534379
	struct _fpga_properties *_prop = NULL;
Packit 534379
	fpga_result result = FPGA_OK;
Packit 534379
Packit 534379
	ASSERT_NOT_NULL(prop);
Packit 534379
Packit 534379
	result = fpgaGetProperties(NULL, (fpga_properties *)&_prop);
Packit 534379
Packit 534379
	ASSERT_RESULT(result);
Packit 534379
Packit 534379
	if (token) {
Packit 534379
		result = xfpga_fpgaUpdateProperties(token, _prop);
Packit 534379
		if (result != FPGA_OK)
Packit 534379
			goto out_free;
Packit 534379
	}
Packit 534379
Packit 534379
	*prop = (fpga_properties)_prop;
Packit 534379
	return result;
Packit 534379
Packit 534379
out_free:
Packit 534379
	free(_prop);
Packit 534379
	return result;
Packit 534379
}
Packit 534379
Packit 534379
fpga_result __XFPGA_API__ xfpga_fpgaUpdateProperties(fpga_token token,
Packit 534379
						    fpga_properties prop)
Packit 534379
{
Packit 534379
	struct _fpga_token *_token = (struct _fpga_token *)token;
Packit 534379
	struct _fpga_properties *_prop = (struct _fpga_properties *)prop;
Packit 534379
Packit 534379
	struct _fpga_properties _iprop;
Packit 534379
Packit 534379
	char spath[SYSFS_PATH_MAX] = { 0, };
Packit 534379
	char idpath[SYSFS_PATH_MAX] = { 0, };
Packit 534379
	char *p;
Packit 534379
	int s, b, d, f;
Packit 534379
	int res;
Packit 534379
	int err = 0;
Packit 534379
	int resval = 0;
Packit 534379
	uint64_t value = 0;
Packit 534379
	uint32_t x = 0;
Packit 534379
	size_t len;
Packit 534379
Packit 534379
	pthread_mutex_t lock;
Packit 534379
Packit 534379
	fpga_result result = FPGA_INVALID_PARAM;
Packit 534379
Packit 534379
	ASSERT_NOT_NULL(token);
Packit 534379
	if (_token->magic != FPGA_TOKEN_MAGIC) {
Packit 534379
		OPAE_MSG("Invalid token");
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	ASSERT_NOT_NULL(_prop);
Packit 534379
	if (_prop->magic != FPGA_PROPERTY_MAGIC) {
Packit 534379
		OPAE_MSG("Invalid properties object");
Packit 534379
		return FPGA_INVALID_PARAM;
Packit 534379
	}
Packit 534379
Packit 534379
	// clear fpga_properties buffer
Packit 534379
	memset(&_iprop, 0, sizeof(struct _fpga_properties));
Packit 534379
	_iprop.magic = FPGA_PROPERTY_MAGIC;
Packit 534379
Packit 534379
	// read the vendor and device ID from the 'device' path
Packit 534379
	if (snprintf(idpath, sizeof(idpath),
Packit 534379
		     "%s/../device/vendor", _token->sysfspath) < 0) {
Packit 534379
		OPAE_ERR("snprintf buffer overflow");
Packit 534379
		return FPGA_EXCEPTION;
Packit 534379
	}
Packit 534379
Packit 534379
	x = 0;
Packit 534379
	result = sysfs_read_u32(idpath, &x);
Packit 534379
	if (result != FPGA_OK)
Packit 534379
		return result;
Packit 534379
	_iprop.vendor_id = (uint16_t)x;
Packit 534379
	SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_VENDORID);
Packit 534379
Packit 534379
	if (snprintf(idpath, sizeof(idpath),
Packit 534379
		     "%s/../device/device", _token->sysfspath) < 0) {
Packit 534379
		OPAE_ERR("snprintf buffer overflow");
Packit 534379
		return FPGA_EXCEPTION;
Packit 534379
	}
Packit 534379
Packit 534379
	x = 0;
Packit 534379
	result = sysfs_read_u32(idpath, &x);
Packit 534379
	if (result != FPGA_OK)
Packit 534379
		return result;
Packit 534379
	_iprop.device_id = (uint16_t)x;
Packit 534379
	SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_DEVICEID);
Packit 534379
Packit 534379
	// The input token is either for an FME or an AFU.
Packit 534379
	// Go one level back to get to the dev.
Packit 534379
Packit 534379
	len = strnlen(_token->sysfspath, sizeof(spath) - 1);
Packit 534379
	memcpy(spath, _token->sysfspath, len);
Packit 534379
	spath[len] = '\0';
Packit 534379
Packit 534379
	p = strrchr(spath, '/');
Packit 534379
	ASSERT_NOT_NULL_MSG(p, "Invalid token sysfs path");
Packit 534379
Packit 534379
	*p = 0;
Packit 534379
Packit 534379
	p = strstr(_token->sysfspath, FPGA_SYSFS_AFU);
Packit 534379
	if (NULL != p) {
Packit 534379
		// AFU
Packit 534379
		result = sysfs_get_guid(_token, FPGA_SYSFS_AFU_GUID,
Packit 534379
			 _iprop.guid);
Packit 534379
		if (FPGA_OK != result)
Packit 534379
			return result;
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_GUID);
Packit 534379
Packit 534379
		_iprop.parent = (fpga_token)token_get_parent(_token);
Packit 534379
		if (NULL != _iprop.parent)
Packit 534379
			SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_PARENT);
Packit 534379
Packit 534379
		_iprop.objtype = FPGA_ACCELERATOR;
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_OBJTYPE);
Packit 534379
Packit 534379
		res = open(_token->devpath, O_RDWR);
Packit 534379
		if (-1 == res) {
Packit 534379
			_iprop.u.accelerator.state = FPGA_ACCELERATOR_ASSIGNED;
Packit 534379
		} else {
Packit 534379
			close(res);
Packit 534379
			_iprop.u.accelerator.state =
Packit 534379
				FPGA_ACCELERATOR_UNASSIGNED;
Packit 534379
		}
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_ACCELERATOR_STATE);
Packit 534379
Packit 534379
		_iprop.u.accelerator.num_mmio = 2;
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_NUM_MMIO);
Packit 534379
Packit 534379
		_iprop.u.accelerator.num_interrupts = 0;
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_NUM_INTERRUPTS);
Packit 534379
	}
Packit 534379
Packit 534379
	p = strstr(_token->sysfspath, FPGA_SYSFS_FME);
Packit 534379
	if (NULL != p) {
Packit 534379
		// FME
Packit 534379
		_iprop.objtype = FPGA_DEVICE;
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_OBJTYPE);
Packit 534379
		// get bitstream id
Packit 534379
		result = sysfs_get_interface_id(_token, _iprop.guid);
Packit 534379
		if (FPGA_OK != result)
Packit 534379
			return result;
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_GUID);
Packit 534379
Packit 534379
		resval = sysfs_parse_attribute64(_token->sysfspath,
Packit 534379
			FPGA_SYSFS_NUM_SLOTS, &value);
Packit 534379
		if (resval != 0) {
Packit 534379
			return FPGA_NOT_FOUND;
Packit 534379
		}
Packit 534379
		_iprop.u.fpga.num_slots = (uint32_t)value;
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_NUM_SLOTS);
Packit 534379
Packit 534379
		resval = sysfs_parse_attribute64(_token->sysfspath,
Packit 534379
			FPGA_SYSFS_BITSTREAM_ID, &_iprop.u.fpga.bbs_id);
Packit 534379
		if (resval != 0) {
Packit 534379
			return FPGA_NOT_FOUND;
Packit 534379
		}
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_BBSID);
Packit 534379
Packit 534379
		_iprop.u.fpga.bbs_version.major =
Packit 534379
				FPGA_BBS_VER_MAJOR(_iprop.u.fpga.bbs_id);
Packit 534379
		_iprop.u.fpga.bbs_version.minor =
Packit 534379
				FPGA_BBS_VER_MINOR(_iprop.u.fpga.bbs_id);
Packit 534379
		_iprop.u.fpga.bbs_version.patch =
Packit 534379
				FPGA_BBS_VER_PATCH(_iprop.u.fpga.bbs_id);
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_BBSVERSION);
Packit 534379
	}
Packit 534379
Packit 534379
	result = sysfs_sbdf_from_path(spath, &s, &b, &d, &f);
Packit 534379
	if (result)
Packit 534379
		return result;
Packit 534379
Packit 534379
	_iprop.segment = (uint16_t)s;
Packit 534379
	SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_SEGMENT);
Packit 534379
Packit 534379
	_iprop.bus = (uint8_t)b;
Packit 534379
	SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_BUS);
Packit 534379
Packit 534379
	_iprop.device = (uint8_t)d;
Packit 534379
	SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_DEVICE);
Packit 534379
Packit 534379
	_iprop.function = (uint8_t)f;
Packit 534379
	SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_FUNCTION);
Packit 534379
Packit 534379
	// only set socket id if we have it on sysfs
Packit 534379
	if (sysfs_get_fme_path(_token->sysfspath, spath) == FPGA_OK) {
Packit 534379
		resval = sysfs_parse_attribute64(spath,
Packit 534379
			FPGA_SYSFS_SOCKET_ID, &value);
Packit 534379
Packit 534379
		if (0 == resval) {
Packit 534379
			_iprop.socket_id = (uint8_t)value;
Packit 534379
			SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_SOCKETID);
Packit 534379
		}
Packit 534379
	}
Packit 534379
Packit 534379
	result = sysfs_objectid_from_path(_token->sysfspath, &_iprop.object_id);
Packit 534379
	if (0 == result)
Packit 534379
		SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_OBJECTID);
Packit 534379
Packit 534379
	char errpath[SYSFS_PATH_MAX] = { 0, };
Packit 534379
Packit 534379
	if (snprintf(errpath, sizeof(errpath),
Packit 534379
		     "%s/errors", _token->sysfspath) < 0) {
Packit 534379
		OPAE_ERR("snprintf buffer overflow");
Packit 534379
		return FPGA_EXCEPTION;
Packit 534379
	}
Packit 534379
Packit 534379
	_iprop.num_errors = count_error_files(errpath);
Packit 534379
	SET_FIELD_VALID(&_iprop, FPGA_PROPERTY_NUM_ERRORS);
Packit 534379
Packit 534379
	if (pthread_mutex_lock(&_prop->lock)) {
Packit 534379
		OPAE_MSG("Failed to lock properties mutex");
Packit 534379
		return FPGA_EXCEPTION;
Packit 534379
	}
Packit 534379
Packit 534379
	lock = _prop->lock;
Packit 534379
	*_prop = _iprop;
Packit 534379
	_prop->lock = lock;
Packit 534379
Packit 534379
	err = pthread_mutex_unlock(&_prop->lock);
Packit 534379
	if (err)
Packit 534379
		OPAE_ERR("pthread_mutex_unlock() failed: %s", strerror(err));
Packit 534379
Packit 534379
	return FPGA_OK;
Packit 534379
}