<commit_before>/**
 * Copyright 2016 International Business Machines
 * Copyright 2016 Rackspace Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/time.h>

#include <libdonut.h>
#include <libcxl.h>
#include <donut_tools.h>
#include <donut_internal.h>
#include <donut_queue.h>

#define timediff_usec(t0, t1)						\
	((double)(((t0)->tv_sec * 1000000 + (t0)->tv_usec) -		\
		  ((t1)->tv_sec * 1000000 + (t1)->tv_usec)))

/* Trace hardware implementation */
static unsigned int dnut_trace = 0x0;
static unsigned int dnut_config = 0x0;
static struct dnut_action *actions = NULL;

#define dnut_trace_enabled()  (dnut_trace & 0x1)
#define reg_trace_enabled()   (dnut_trace & 0x2)
#define sim_trace_enabled()   (dnut_trace & 0x4)

int action_trace_enabled(void)
{
	return dnut_trace & 0x8;
}

#define simulation_enabled()  (dnut_config & 0x1)

#define dnut_trace(fmt, ...) do {					\
		if (dnut_trace_enabled())				\
			fprintf(stderr, "D " fmt, ## __VA_ARGS__);	\
	} while (0)

#define reg_trace(fmt, ...) do {					\
		if (reg_trace_enabled())				\
			fprintf(stderr, "R " fmt, ## __VA_ARGS__);	\
	} while (0)

#define sim_trace(fmt, ...) do {					\
		if (sim_trace_enabled())				\
			fprintf(stderr, "S " fmt, ## __VA_ARGS__);	\
	} while (0)

#define	FW_BASE_ADDR		0x00100
#define	FW_BASE_ADDR8		0x00108

/* FIXME Some of those addresses will be hidden in libdonut on future
   releases. */

/* TO BE REMOVED ACTION Specific register setup */
#define	ACTION_4		(ACTION_BASE + 0x04)
#define	ACTION_8		(ACTION_BASE + 0x08)

#define	ACTION_CONFIG		(ACTION_BASE + 0x10)
#define ACTION_CONFIG_COUNT	  0x00000001
#define	ACTION_CONFIG_COPY	  0x00000002

#define	ACTION_SRC_LOW		(ACTION_BASE + 0x14)
#define	ACTION_SRC_HIGH		(ACTION_BASE + 0x18)
#define	ACTION_DEST_LOW		(ACTION_BASE + 0x1c)
#define	ACTION_DEST_HIGH	(ACTION_BASE + 0x20)
#define	ACTION_CNT		(ACTION_BASE + 0x24)	/* Count Register */

struct wed {
	uint64_t res[16];
};

struct dnut_data {
	void *priv;
	struct cxl_afu_h *afu_h;
	uint16_t vendor_id;
	uint16_t device_id;
	uint16_t action_type;
	int afu_fd;

	struct wed *wed;
	struct dnut_action *action; /* software simulation mode */
	size_t errinfo_size;
	void *errinfo;		/* Err info Buffer */
};

/* To be used for software simulation, use funcs provided by action */
static int dnut_map_funcs(struct dnut_data *card, uint16_t action_type);

/**********************************************************************
 * DIRECT CARD ACCESS
 *********************************************************************/

static void *hw_dnut_card_alloc_dev(const char *path, uint16_t vendor_id,
				    uint16_t device_id)
{
	struct dnut_data *dn;
	struct cxl_afu_h *afu_h = NULL;
	struct wed *wed = NULL;
	int rc;
	long id = 0;

	dn = calloc(1, sizeof(*dn));
	if (NULL == dn)
		goto __dnut_alloc_err;

	dn->priv = NULL;

	afu_h = cxl_afu_open_dev((char*)path);
	if (NULL == afu_h)
		goto __dnut_alloc_err;

	dn->device_id = 0;
	/* Read and check Device id if it was given by caller */
	if (0 != device_id) {
		rc = cxl_get_cr_device(afu_h, 0, &id);
		if ((0 != rc) || ((uint16_t)id != device_id)) {
			dnut_trace("  %s: ERR 0x%ux Invalid Device ID\n", __func__, (int)id);
			goto __dnut_alloc_err;
		}
		dn->device_id = (uint16_t)id;
        }

	dn->vendor_id = 0;
	/* Read and check Vendor id if it was given by caller */
	if (0 != vendor_id) {
		rc = cxl_get_cr_vendor(afu_h, 0, &id);
		if ((0 != rc) || ((uint16_t)id != vendor_id)) {
			dnut_trace("  %s: ERR 0x%x Invalid Vendor ID\n", __func__, (int)id);
			goto __dnut_alloc_err;
		}
		dn->vendor_id = (uint16_t)id;<commit_msg>Minor fix for device and vendor id<commit_after>/**
 * Copyright 2016 International Business Machines
 * Copyright 2016 Rackspace Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/time.h>

#include <libdonut.h>
#include <libcxl.h>
#include <donut_tools.h>
#include <donut_internal.h>
#include <donut_queue.h>

#define timediff_usec(t0, t1)						\
	((double)(((t0)->tv_sec * 1000000 + (t0)->tv_usec) -		\
		  ((t1)->tv_sec * 1000000 + (t1)->tv_usec)))

/* Trace hardware implementation */
static unsigned int dnut_trace = 0x0;
static unsigned int dnut_config = 0x0;
static struct dnut_action *actions = NULL;

#define dnut_trace_enabled()  (dnut_trace & 0x1)
#define reg_trace_enabled()   (dnut_trace & 0x2)
#define sim_trace_enabled()   (dnut_trace & 0x4)

int action_trace_enabled(void)
{
	return dnut_trace & 0x8;
}

#define simulation_enabled()  (dnut_config & 0x1)

#define dnut_trace(fmt, ...) do {					\
		if (dnut_trace_enabled())				\
			fprintf(stderr, "D " fmt, ## __VA_ARGS__);	\
	} while (0)

#define reg_trace(fmt, ...) do {					\
		if (reg_trace_enabled())				\
			fprintf(stderr, "R " fmt, ## __VA_ARGS__);	\
	} while (0)

#define sim_trace(fmt, ...) do {					\
		if (sim_trace_enabled())				\
			fprintf(stderr, "S " fmt, ## __VA_ARGS__);	\
	} while (0)

#define	FW_BASE_ADDR		0x00100
#define	FW_BASE_ADDR8		0x00108

/* FIXME Some of those addresses will be hidden in libdonut on future
   releases. */

/* TO BE REMOVED ACTION Specific register setup */
#define	ACTION_4		(ACTION_BASE + 0x04)
#define	ACTION_8		(ACTION_BASE + 0x08)

#define	ACTION_CONFIG		(ACTION_BASE + 0x10)
#define ACTION_CONFIG_COUNT	  0x00000001
#define	ACTION_CONFIG_COPY	  0x00000002

#define	ACTION_SRC_LOW		(ACTION_BASE + 0x14)
#define	ACTION_SRC_HIGH		(ACTION_BASE + 0x18)
#define	ACTION_DEST_LOW		(ACTION_BASE + 0x1c)
#define	ACTION_DEST_HIGH	(ACTION_BASE + 0x20)
#define	ACTION_CNT		(ACTION_BASE + 0x24)	/* Count Register */

struct wed {
	uint64_t res[16];
};

struct dnut_data {
	void *priv;
	struct cxl_afu_h *afu_h;
	uint16_t vendor_id;
	uint16_t device_id;
	uint16_t action_type;
	int afu_fd;

	struct wed *wed;
	struct dnut_action *action; /* software simulation mode */
	size_t errinfo_size;
	void *errinfo;		/* Err info Buffer */
};

/* To be used for software simulation, use funcs provided by action */
static int dnut_map_funcs(struct dnut_data *card, uint16_t action_type);

/**********************************************************************
 * DIRECT CARD ACCESS
 *********************************************************************/

static void *hw_dnut_card_alloc_dev(const char *path, uint16_t vendor_id,
				    uint16_t device_id)
{
	struct dnut_data *dn;
	struct cxl_afu_h *afu_h = NULL;
	struct wed *wed = NULL;
	int rc;
	long id = 0;

	dn = calloc(1, sizeof(*dn));
	if (NULL == dn)
		goto __dnut_alloc_err;

	dn->priv = NULL;

	afu_h = cxl_afu_open_dev((char*)path);
	if (NULL == afu_h)
		goto __dnut_alloc_err;

	dn->device_id = 0;
	/* Read and check Device id if it was given by caller */
	if (0xffff != device_id) {
		rc = cxl_get_cr_device(afu_h, 0, &id);
		if ((0 != rc) || ((uint16_t)id != device_id)) {
			dnut_trace("  %s: ERR 0x%x Invalid Device ID 0x%x\n", __func__, (int)id, (int)device_id);
			goto __dnut_alloc_err;
		}
		dn->device_id = (uint16_t)id;
        }

	dn->vendor_id = 0;
	/* Read and check Vendor id if it was given by caller */
	if (0xffff != vendor_id) {
		rc = cxl_get_cr_vendor(afu_h, 0, &id);
		if ((0 != rc) || ((uint16_t)id != vendor_id)) {
			dnut_trace("  %s: ERR 0x%x Invalid Vendor ID\n", __func__, (int)id);
			goto __dnut_alloc_err;
		}
		dn->vendor_id = (uint16_t)id;
{'fraction_non_alphanumeric': 0.152779410036432, 'fraction_numerical': 0.0332589023387002, 'mean_word_length': 4.098861593768723, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1030436', 'n_tokens_mistral': 3482, 'n_tokens_neox': 3183, 'n_words': 873}