Files
Jungfraujoch/fpga/host_library/JungfraujochDevice.cpp

225 lines
7.4 KiB
C++

// Copyright (2019-2023) Paul Scherrer Institute
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "../include/jfjoch_fpga.h"
#include "JungfraujochDevice.h"
#include "../../common/JFJochException.h"
#include "../pcie_driver/jfjoch_ioctl.h"
JungfraujochDevice::JungfraujochDevice(const std::string &device_name, bool write_access) {
fd = open(device_name.c_str(), write_access ? O_RDWR : O_RDONLY);
if (fd == -1) {
if (errno == EBUSY)
throw JFJochException(JFJochExceptionCategory::PCIeError, "Device already opened");
else if (errno == EACCES)
throw JFJochException(JFJochExceptionCategory::PCIeError, "Permission denied");
else
throw JFJochException(JFJochExceptionCategory::PCIeError, "Cannot open device");
}
auto action_type = GetDataCollectionStatus().action_type;
if (action_type != ACTION_TYPE)
throw JFJochException(JFJochExceptionCategory::PCIeError, "Wrong device type");
auto action_release = GetDataCollectionStatus().release_level;
if (action_release != RELEASE_LEVEL)
throw JFJochException(JFJochExceptionCategory::PCIeError, "Mismatch of FPGA release between driver and library");
}
JungfraujochDevice::~JungfraujochDevice() {
close(fd);
}
void JungfraujochDevice::Start() {
if (ioctl(fd, IOCTL_JFJOCH_START) != 0)
throw PCIeDeviceException("Failed starting action");
}
void JungfraujochDevice::Cancel() {
if (ioctl(fd, IOCTL_JFJOCH_CANCEL) != 0)
throw PCIeDeviceException("Failed setting cancel bit");
}
void JungfraujochDevice::End() {
if (ioctl(fd, IOCTL_JFJOCH_END) != 0)
throw PCIeDeviceException("Failed ending action");
}
bool JungfraujochDevice::IsIdle() const {
uint32_t tmp;
if (ioctl(fd, IOCTL_JFJOCH_ISIDLE, &tmp) != 0)
throw PCIeDeviceException("Failed checking if idle");
return tmp;
}
DataCollectionStatus JungfraujochDevice::GetDataCollectionStatus() const {
DataCollectionStatus ret{};
if (ioctl(fd, IOCTL_JFJOCH_STATUS, &ret) != 0)
throw PCIeDeviceException("Failed reading status");
return ret;
}
DataCollectionConfig JungfraujochDevice::GetConfig() const {
DataCollectionConfig ret{};
if (ioctl(fd, IOCTL_JFJOCH_READ_CONFIG, &ret) != 0)
throw PCIeDeviceException("Failed reading config");
return ret;
}
DeviceStatus JungfraujochDevice::GetDeviceStatus() const {
DeviceStatus ret{};
if (ioctl(fd, IOCTL_JFJOCH_GET_DEV_STATUS, &ret) != 0)
throw PCIeDeviceException("Failed reading env. data");
return ret;
}
void JungfraujochDevice::ClearNetworkCounters() {
if (ioctl(fd, IOCTL_JFJOCH_CLR_CNTRS) != 0)
throw PCIeDeviceException("Failed clearing network counters");
}
void JungfraujochDevice::Reset() {
}
uint32_t JungfraujochDevice::GetNumaNode() const {
int32_t tmp;
if (ioctl(fd, IOCTL_JFJOCH_NUMA, &tmp) != 0)
throw PCIeDeviceException("Failed reading NUMA node");
return tmp;
}
void JungfraujochDevice::SetConfig(const DataCollectionConfig &config) {
if (config.nframes > MAX_FRAMES)
throw PCIeDeviceException("Cannot handle this many frames");
if (ioctl(fd, IOCTL_JFJOCH_SET_CONFIG, &config) != 0)
throw PCIeDeviceException("Failed writing config");
}
bool JungfraujochDevice::ReadWorkCompletion(uint32_t *output) {
int tmp = ioctl(fd, IOCTL_JFJOCH_READ_WC_MBOX, output);
if (tmp != 0) {
if ((errno == EAGAIN) || (errno == EINTR))
return false;
throw PCIeDeviceException("Failed receiving work completion");
}
return true;
}
bool JungfraujochDevice::SendWorkRequest(uint32_t id) {
int tmp = ioctl(fd, IOCTL_JFJOCH_SEND_WR, &id);
if (tmp != 0) {
if (errno == EAGAIN)
return false;
throw PCIeDeviceException("Failed sending work request");
}
return true;
}
uint32_t JungfraujochDevice::GetBufferCount() const {
uint32_t tmp;
if (ioctl(fd, IOCTL_JFJOCH_BUF_COUNT, &tmp) != 0)
throw PCIeDeviceException("Failed getting buffer count");
return tmp;
}
void JungfraujochDevice::SetMACAddress(uint64_t addr) {
if (ioctl(fd, IOCTL_JFJOCH_SET_MAC, &addr) != 0)
throw PCIeDeviceException("Failed setting MAC address");
}
uint64_t JungfraujochDevice::GetMACAddress() const {
uint64_t tmp;
if (ioctl(fd, IOCTL_JFJOCH_GET_MAC, &tmp) != 0)
throw PCIeDeviceException("Failed getting MAC address");
return tmp;
}
void JungfraujochDevice::SetDefaultMACAddress() {
if (ioctl(fd, IOCTL_JFJOCH_DEFAULT_MAC) != 0)
throw PCIeDeviceException("Failed setting default MAC");
}
uint32_t JungfraujochDevice::GetIPv4Address() const {
uint32_t tmp;
if (ioctl(fd, IOCTL_JFJOCH_GET_IPV4, &tmp) != 0)
throw PCIeDeviceException("Failed getting IPv4 address");
return tmp;
}
void JungfraujochDevice::SetIPv4Address(uint32_t input) {
if (ioctl(fd, IOCTL_JFJOCH_SET_IPV4, &input) != 0)
throw PCIeDeviceException("Failed setting IPv4 address");
}
void JungfraujochDevice::RunFrameGenerator(const FrameGeneratorConfig &config) {
if (ioctl(fd, IOCTL_JFJOCH_RUN_FRAME_GEN, &config) != 0)
throw PCIeDeviceException("Failed starting frame generator");
}
uint32_t JungfraujochDevice::ReadRegister(uint32_t addr) const {
RegisterConfig config;
config.addr = addr;
if (ioctl(fd, IOCTL_JFJOCH_READ_REGISTER, &config) != 0)
throw PCIeDeviceException("Failed reading register");
return config.val;
}
void JungfraujochDevice::LoadCalibration(const LoadCalibrationConfig &config) {
if (ioctl(fd, IOCTL_JFJOCH_LOAD_CALIB, &config) != 0)
throw PCIeDeviceException("Failed uploading calibration");
}
DeviceOutput *JungfraujochDevice::MapKernelBuffer(uint32_t id) {
auto tmp = (DeviceOutput *) mmap(nullptr, FPGA_BUFFER_LOCATION_SIZE,
PROT_READ | PROT_WRITE, MAP_SHARED,
fd, FPGA_BUFFER_LOCATION_SIZE * id);
if (tmp == MAP_FAILED)
throw PCIeDeviceException("Mmap of kernel buffer error");
return tmp;
}
void JungfraujochDevice::UnmapKernelBuffer(DeviceOutput *val) {
munmap(val, FPGA_BUFFER_LOCATION_SIZE);
}
void JungfraujochDevice::SetSpotFinderParameters(const SpotFinderParameters& params) {
if (ioctl(fd, IOCTL_JFJOCH_SET_SPOTFIN_PAR, &params) != 0)
throw PCIeDeviceException("Failed settings spot finder parameters");
}
SpotFinderParameters JungfraujochDevice::GetSpotFinderParameters() {
SpotFinderParameters ret{};
if (ioctl(fd, IOCTL_JFJOCH_GET_SPOTFIN_PAR, &ret) != 0)
throw PCIeDeviceException("Failed fetching spot finder parameters");
return ret;
}
uint32_t JungfraujochDevice::GetDataSource() {
uint32_t tmp;
if (ioctl(fd, IOCTL_JFJOCH_GET_DATA_SOURCE, &tmp) != 0)
throw JFJochException(JFJochExceptionCategory::PCIeError, "Failed getting data source");
return tmp;
}
void JungfraujochDevice::SetDataSource(uint32_t id) {
if (id >= 4)
throw JFJochException(JFJochExceptionCategory::PCIeError, "Data source is 2-bit variable");
if (ioctl(fd, IOCTL_JFJOCH_SET_DATA_SOURCE, &id) != 0)
throw JFJochException(JFJochExceptionCategory::PCIeError, "Failed setting data source");
}
bool JungfraujochDevice::ReadWorkCompletion(JungfraujochDeviceCompletion &completion) {
uint32_t tmp = 0;
bool ret = ReadWorkCompletion(&tmp);
completion.buffer_id = tmp & 0xFFFF;
completion.data_collection_id = (tmp >> 16) & 0xFFFF;
return false;
}