Blame opae-libs/plugins/xfpga/token_list.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 <pthread.h>
Packit 534379
#include <stdio.h>
Packit 534379
#include <string.h>
Packit 534379
#include <stdlib.h>
Packit 534379
#include <dirent.h>
Packit 534379
#include <sys/stat.h>
Packit 534379
Packit 534379
#include "error_int.h"
Packit 534379
Packit 534379
#include "token_list_int.h"
Packit 534379
Packit 534379
/* global list of tokens we've seen */
Packit 534379
static struct token_map *token_root;
Packit 534379
/* mutex to protect global data structures */
Packit 534379
extern pthread_mutex_t global_lock;
Packit 534379
Packit 534379
/**
Packit 534379
 * @brief Add entry to linked list for tokens
Packit 534379
 *	Will allocate memory (which is freed by token_cleanup())
Packit 534379
 *
Packit 534379
 * @param sysfspath
Packit 534379
 * @param devpath
Packit 534379
 *
Packit 534379
 * @return
Packit 534379
 */
Packit 534379
struct _fpga_token *token_add(const char *sysfspath, const char *devpath)
Packit 534379
{
Packit 534379
	struct token_map *tmp;
Packit 534379
	int err = 0;
Packit 534379
	uint32_t device_instance;
Packit 534379
	uint32_t subdev_instance;
Packit 534379
	char *endptr = NULL;
Packit 534379
	const char *ptr;
Packit 534379
	size_t len;
Packit 534379
Packit 534379
	/* get the device instance id */
Packit 534379
	ptr = strchr(sysfspath, '.');
Packit 534379
	if (ptr == NULL) {
Packit 534379
		OPAE_MSG("sysfspath does not meet expected format");
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
	device_instance = strtoul(++ptr, &endptr, 10);
Packit 534379
	/* no digits in path */
Packit 534379
	if (endptr == ptr) {
Packit 534379
		OPAE_MSG("sysfspath does not meet expected format");
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
	/* get the sub-device (FME/Port) instance id */
Packit 534379
	ptr = strrchr(sysfspath, '.');
Packit 534379
	if (ptr == NULL) {
Packit 534379
		OPAE_MSG("sysfspath does not meet expected format");
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
	subdev_instance = strtoul(++ptr, &endptr, 10);
Packit 534379
	/* no digits in path */
Packit 534379
	if (endptr == ptr) {
Packit 534379
		OPAE_MSG("sysfspath does not meet expected format");
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
	if (pthread_mutex_lock(&global_lock)) {
Packit 534379
		OPAE_MSG("Failed to lock global mutex");
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
	/* Prevent duplicate entries. */
Packit 534379
	for (tmp = token_root ; NULL != tmp ; tmp = tmp->next) {
Packit 534379
		if ((0 == strncmp(sysfspath, tmp->_token.sysfspath,
Packit 534379
						SYSFS_PATH_MAX)) &&
Packit 534379
				(0 == strncmp(devpath, tmp->_token.devpath,
Packit 534379
					      DEV_PATH_MAX))) {
Packit 534379
			err = pthread_mutex_unlock(&global_lock);
Packit 534379
			if (err) {
Packit 534379
				OPAE_ERR("pthread_mutex_unlock() failed: %S", strerror(err));
Packit 534379
			}
Packit 534379
			return &tmp->_token;
Packit 534379
		}
Packit 534379
	}
Packit 534379
Packit 534379
	tmp = malloc(sizeof(struct token_map));
Packit 534379
	if (!tmp) {
Packit 534379
		err = pthread_mutex_unlock(&global_lock);
Packit 534379
		if (err) {
Packit 534379
			OPAE_ERR("pthread_mutex_unlock() failed: %S", strerror(err));
Packit 534379
		}
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
Packit 534379
	/* populate error list */
Packit 534379
	tmp->_token.errors = NULL;
Packit 534379
	char errpath[SYSFS_PATH_MAX] = { 0, };
Packit 534379
Packit 534379
	if (snprintf(errpath, sizeof(errpath),
Packit 534379
		     "%s/errors", sysfspath) < 0) {
Packit 534379
		OPAE_ERR("snprintf buffer overflow");
Packit 534379
		free(tmp);
Packit 534379
		if (pthread_mutex_unlock(&global_lock)) {
Packit 534379
			OPAE_ERR("pthread_mutex_unlock() failed: %S", strerror(err));
Packit 534379
		}
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
	build_error_list(errpath, &tmp->_token.errors);
Packit 534379
Packit 534379
	/* mark data structure as valid */
Packit 534379
	tmp->_token.magic = FPGA_TOKEN_MAGIC;
Packit 534379
Packit 534379
	/* assign the instances num from above */
Packit 534379
	tmp->_token.device_instance = device_instance;
Packit 534379
	tmp->_token.subdev_instance = subdev_instance;
Packit 534379
Packit 534379
	/* deep copy token data */
Packit 534379
	len = strnlen(sysfspath, SYSFS_PATH_MAX - 1);
Packit 534379
	memcpy(tmp->_token.sysfspath, sysfspath, len);
Packit 534379
	tmp->_token.sysfspath[len] = '\0';
Packit 534379
Packit 534379
	len = strnlen(devpath, DEV_PATH_MAX - 1);
Packit 534379
	memcpy(tmp->_token.devpath, devpath, len);
Packit 534379
	tmp->_token.devpath[len] = '\0';
Packit 534379
Packit 534379
	tmp->next = token_root;
Packit 534379
	token_root = tmp;
Packit 534379
Packit 534379
	err = pthread_mutex_unlock(&global_lock);
Packit 534379
	if (err) {
Packit 534379
		OPAE_ERR("pthread_mutex_unlock() failed: %S", strerror(err));
Packit 534379
	}
Packit 534379
Packit 534379
	return &tmp->_token;
Packit 534379
}
Packit 534379
Packit 534379
/**
Packit 534379
 * @ brief Find the token that is the parent of _t
Packit 534379
 *
Packit 534379
 * @param _t
Packit 534379
 *
Packit 534379
 * @return parent of _t, or NULL if not found.
Packit 534379
 */
Packit 534379
struct _fpga_token *token_get_parent(struct _fpga_token *_t)
Packit 534379
{
Packit 534379
	char *p;
Packit 534379
	char spath[SYSFS_PATH_MAX] = { 0, };
Packit 534379
	char rpath[PATH_MAX] = { 0, };
Packit 534379
	struct token_map *itr;
Packit 534379
	int err = 0;
Packit 534379
	char *rptr = NULL;
Packit 534379
	fpga_result res = FPGA_OK;
Packit 534379
Packit 534379
	p = strstr(_t->sysfspath, FPGA_SYSFS_AFU);
Packit 534379
	if (!p) // FME objects have no parent.
Packit 534379
		return NULL;
Packit 534379
Packit 534379
	res = sysfs_get_fme_path(_t->sysfspath, spath);
Packit 534379
	if (res) {
Packit 534379
		OPAE_ERR("Could not find fme path for token: %s",
Packit 534379
			 _t->sysfspath);
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
	if (pthread_mutex_lock(&global_lock)) {
Packit 534379
		OPAE_MSG("Failed to lock global mutex");
Packit 534379
		return NULL;
Packit 534379
	}
Packit 534379
Packit 534379
	for (itr = token_root ; NULL != itr ; itr = itr->next) {
Packit 534379
		rptr = realpath(itr->_token.sysfspath, rpath);
Packit 534379
		if (rptr && !strncmp(spath, rptr, SYSFS_PATH_MAX)) {
Packit 534379
			err = pthread_mutex_unlock(&global_lock);
Packit 534379
			if (err) {
Packit 534379
				OPAE_ERR("pthread_mutex_unlock() failed: %S", strerror(err));
Packit 534379
			}
Packit 534379
			return &itr->_token;
Packit 534379
		}
Packit 534379
	}
Packit 534379
Packit 534379
	err = pthread_mutex_unlock(&global_lock);
Packit 534379
	if (err) {
Packit 534379
		OPAE_ERR("pthread_mutex_unlock() failed: %S", strerror(err));
Packit 534379
	}
Packit 534379
Packit 534379
	return NULL;
Packit 534379
}
Packit 534379
Packit 534379
/*
Packit 534379
 * Clean up remaining entries in linked list
Packit 534379
 * Will delete all remaining entries
Packit 534379
 */
Packit 534379
void token_cleanup(void)
Packit 534379
{
Packit 534379
	int err = 0;
Packit 534379
	struct error_list *p;
Packit 534379
Packit 534379
	err = pthread_mutex_lock(&global_lock);
Packit 534379
	if (err) {
Packit 534379
		OPAE_ERR("pthread_mutex_lock() failed: %s", strerror(err));
Packit 534379
		return;
Packit 534379
	}
Packit 534379
Packit 534379
	if (!token_root)
Packit 534379
		goto out_unlock;
Packit 534379
Packit 534379
	while (token_root->next) {
Packit 534379
		struct token_map *tmp = token_root;
Packit 534379
		token_root = token_root->next;
Packit 534379
Packit 534379
		// free error list
Packit 534379
		p = tmp->_token.errors;
Packit 534379
		while (p) {
Packit 534379
			struct error_list *q = p->next;
Packit 534379
			free(p);
Packit 534379
			p = q;
Packit 534379
		}
Packit 534379
Packit 534379
		// invalidate magic (just in case)
Packit 534379
		tmp->_token.magic = FPGA_INVALID_MAGIC;
Packit 534379
		free(tmp);
Packit 534379
	}
Packit 534379
Packit 534379
	// free error list
Packit 534379
	p = token_root->_token.errors;
Packit 534379
	while (p) {
Packit 534379
		struct error_list *q = p->next;
Packit 534379
		free(p);
Packit 534379
		p = q;
Packit 534379
	}
Packit 534379
Packit 534379
	// invalidate magic (just in case)
Packit 534379
	token_root->_token.magic = FPGA_INVALID_MAGIC;
Packit 534379
	free(token_root);
Packit 534379
Packit 534379
	token_root = NULL;
Packit 534379
Packit 534379
out_unlock:
Packit 534379
	err = pthread_mutex_unlock(&global_lock);
Packit 534379
	if (err) {
Packit 534379
		OPAE_ERR("pthread_mutex_unlock() failed: %s", strerror(err));
Packit 534379
	}
Packit 534379
}