1
0
mirror of https://github.com/corundum/corundum.git synced 2025-01-16 08:12:53 +08:00
corundum/utils/mqnic.c

502 lines
15 KiB
C
Raw Normal View History

2019-07-21 21:53:37 -07:00
/*
Copyright 2019, The Regents of the University of California.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE REGENTS OF THE UNIVERSITY OF CALIFORNIA ''AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF CALIFORNIA OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of The Regents of the University of California.
*/
#include "mqnic.h"
#include <fcntl.h>
#include <limits.h>
2019-07-21 21:53:37 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2021-12-29 22:31:46 -08:00
#include <time.h>
2019-07-23 18:43:58 -07:00
#include <sys/ioctl.h>
2019-07-21 21:53:37 -07:00
#include <sys/mman.h>
#include <sys/stat.h>
2019-07-21 21:53:37 -07:00
struct mqnic *mqnic_open(const char *dev_name)
{
struct mqnic *dev = calloc(1, sizeof(struct mqnic));
struct stat st;
2019-07-21 21:53:37 -07:00
if (!dev)
{
perror("memory allocation failed");
goto fail_alloc;
}
dev->fd = open(dev_name, O_RDWR);
if (dev->fd < 0)
{
perror("open device failed");
goto fail_open;
}
if (fstat(dev->fd, &st) == -1)
2019-07-23 18:43:58 -07:00
{
perror("fstat failed");
goto fail_fstat;
}
dev->regs_size = st.st_size;
if (dev->regs_size == 0)
{
struct mqnic_ioctl_info info;
if (ioctl(dev->fd, MQNIC_IOCTL_INFO, &info) != 0)
{
perror("MQNIC_IOCTL_INFO ioctl failed");
goto fail_ioctl;
}
dev->regs_size = info.regs_size;
2019-07-23 18:43:58 -07:00
}
2019-07-21 21:53:37 -07:00
dev->regs = (volatile uint8_t *)mmap(NULL, dev->regs_size, PROT_READ | PROT_WRITE, MAP_SHARED, dev->fd, 0);
if (dev->regs == MAP_FAILED)
{
perror("mmap regs failed");
goto fail_mmap_regs;
}
2021-12-29 22:31:46 -08:00
if (mqnic_reg_read32(dev->regs, 4) == 0xffffffff)
{
// if we were given a PCIe resource, then we may need to enable the device
char path[PATH_MAX+32];
char *ptr;
strcpy(path, dev_name);
ptr = strrchr(path, '/');
if (ptr)
{
strcpy(++ptr, "enable");
}
if (access(path, F_OK) != -1)
{
FILE *fp = fopen(path, "w");
if (fp)
{
fputc('1', fp);
fclose(fp);
}
}
}
2021-12-29 22:31:46 -08:00
if (mqnic_reg_read32(dev->regs, 4) == 0xffffffff)
2019-12-04 13:37:18 -08:00
{
fprintf(stderr, "Error: device needs to be reset\n");
2020-02-03 17:01:00 -08:00
goto fail_reset;
2019-12-04 13:37:18 -08:00
}
2021-12-29 22:31:46 -08:00
dev->rb_list = enumerate_reg_block_list(dev->regs, 0, dev->regs_size);
2019-07-21 21:53:37 -07:00
2021-12-29 22:31:46 -08:00
if (!dev->rb_list)
{
fprintf(stderr, "Error: filed to enumerate blocks\n");
goto fail_enum;
}
// Read ID registers
dev->fw_id_rb = find_reg_block(dev->rb_list, MQNIC_RB_FW_ID_TYPE, MQNIC_RB_FW_ID_VER, 0);
if (!dev->fw_id_rb)
{
fprintf(stderr, "Error: FW ID block not found\n");
goto fail_enum;
}
dev->fpga_id = mqnic_reg_read32(dev->fw_id_rb->regs, MQNIC_RB_FW_ID_REG_FPGA_ID);
dev->fw_id = mqnic_reg_read32(dev->fw_id_rb->regs, MQNIC_RB_FW_ID_REG_FW_ID);
dev->fw_ver = mqnic_reg_read32(dev->fw_id_rb->regs, MQNIC_RB_FW_ID_REG_FW_VER);
dev->board_id = mqnic_reg_read32(dev->fw_id_rb->regs, MQNIC_RB_FW_ID_REG_BOARD_ID);
dev->board_ver = mqnic_reg_read32(dev->fw_id_rb->regs, MQNIC_RB_FW_ID_REG_BOARD_VER);
dev->build_date = mqnic_reg_read32(dev->fw_id_rb->regs, MQNIC_RB_FW_ID_REG_BUILD_DATE);
dev->git_hash = mqnic_reg_read32(dev->fw_id_rb->regs, MQNIC_RB_FW_ID_REG_GIT_HASH);
dev->rel_info = mqnic_reg_read32(dev->fw_id_rb->regs, MQNIC_RB_FW_ID_REG_REL_INFO);
time_t build_date = dev->build_date;
struct tm *tm_info = gmtime(&build_date);
strftime(dev->build_date_str, sizeof(dev->build_date_str), "%F %T", tm_info);
dev->phc_rb = find_reg_block(dev->rb_list, MQNIC_RB_PHC_TYPE, MQNIC_RB_PHC_VER, 0);
// Enumerate interfaces
dev->if_rb = find_reg_block(dev->rb_list, MQNIC_RB_IF_TYPE, MQNIC_RB_IF_VER, 0);
2019-07-21 21:53:37 -07:00
2021-12-29 22:31:46 -08:00
if (!dev->if_rb)
2019-07-21 21:53:37 -07:00
{
2021-12-29 22:31:46 -08:00
fprintf(stderr, "Interface block not found, skipping interface enumeration\n");
dev->if_count = 0;
goto skip_interface;
2019-07-21 21:53:37 -07:00
}
2021-12-29 22:31:46 -08:00
dev->if_offset = mqnic_reg_read32(dev->if_rb->regs, MQNIC_RB_IF_REG_OFFSET);
dev->if_count = mqnic_reg_read32(dev->if_rb->regs, MQNIC_RB_IF_REG_COUNT);
dev->if_stride = mqnic_reg_read32(dev->if_rb->regs, MQNIC_RB_IF_REG_STRIDE);
dev->if_csr_offset = mqnic_reg_read32(dev->if_rb->regs, MQNIC_RB_IF_REG_CSR_OFFSET);
2019-07-21 21:53:37 -07:00
if (dev->if_count > MQNIC_MAX_IF)
dev->if_count = MQNIC_MAX_IF;
for (int k = 0; k < dev->if_count; k++)
{
2021-12-29 22:31:46 -08:00
struct mqnic_if *interface = mqnic_if_open(dev, k, dev->regs + dev->if_offset + k*dev->if_stride);
2021-12-24 13:49:41 -08:00
if (!interface)
2021-12-29 22:31:46 -08:00
{
fprintf(stderr, "Failed to create interface %d, skipping\n", k);
continue;
}
2020-02-03 17:02:05 -08:00
2021-12-24 13:49:41 -08:00
dev->interfaces[k] = interface;
2019-07-21 21:53:37 -07:00
}
2021-12-29 22:31:46 -08:00
skip_interface:
2019-07-21 21:53:37 -07:00
return dev;
2021-12-24 13:49:41 -08:00
fail:
2021-12-29 22:31:46 -08:00
fail_enum:
2020-02-03 17:01:00 -08:00
fail_reset:
2021-12-24 13:49:41 -08:00
mqnic_close(dev);
return NULL;
2019-07-21 21:53:37 -07:00
fail_mmap_regs:
2019-07-23 18:43:58 -07:00
fail_ioctl:
fail_fstat:
2019-07-21 21:53:37 -07:00
close(dev->fd);
fail_open:
free(dev);
fail_alloc:
return NULL;
}
void mqnic_close(struct mqnic *dev)
{
2019-12-04 13:37:53 -08:00
if (!dev)
return;
2021-12-24 13:49:41 -08:00
for (int k = 0; k < dev->if_count; k++)
{
if (!dev->interfaces[k])
continue;
mqnic_if_close(dev->interfaces[k]);
dev->interfaces[k] = NULL;
}
2021-12-29 22:31:46 -08:00
if (dev->rb_list)
free_reg_block_list(dev->rb_list);
2019-07-21 21:53:37 -07:00
munmap((void *)dev->regs, dev->regs_size);
close(dev->fd);
free(dev);
}
2021-12-24 13:49:41 -08:00
struct mqnic_if *mqnic_if_open(struct mqnic *dev, int index, volatile uint8_t *regs)
{
struct mqnic_if *interface = calloc(1, sizeof(struct mqnic_if));
if (!interface)
return NULL;
interface->mqnic = dev;
interface->index = index;
interface->regs_size = dev->if_stride;
interface->regs = regs;
interface->csr_regs = interface->regs + dev->if_csr_offset;
if (interface->regs >= dev->regs+dev->regs_size || interface->csr_regs >= dev->regs+dev->regs_size)
{
fprintf(stderr, "Error: computed pointer out of range\n");
goto fail;
}
2021-12-29 22:31:46 -08:00
// Enumerate registers
interface->rb_list = enumerate_reg_block_list(interface->regs, dev->if_csr_offset, interface->regs_size);
if (!interface->rb_list)
{
fprintf(stderr, "Error: filed to enumerate blocks\n");
goto fail;
}
interface->if_ctrl_tx_rb = find_reg_block(interface->rb_list, MQNIC_RB_IF_CTRL_TX_TYPE, MQNIC_RB_IF_CTRL_TX_VER, 0);
2021-12-24 13:49:41 -08:00
2021-12-29 22:31:46 -08:00
if (!interface->if_ctrl_tx_rb)
{
fprintf(stderr, "Error: TX interface control block not found\n");
goto fail;
}
interface->if_tx_features = mqnic_reg_read32(interface->if_ctrl_tx_rb->regs, MQNIC_RB_IF_CTRL_TX_REG_FEATURES);
interface->max_tx_mtu = mqnic_reg_read32(interface->if_ctrl_tx_rb->regs, MQNIC_RB_IF_CTRL_TX_REG_MAX_MTU);
interface->if_ctrl_rx_rb = find_reg_block(interface->rb_list, MQNIC_RB_IF_CTRL_RX_TYPE, MQNIC_RB_IF_CTRL_RX_VER, 0);
if (!interface->if_ctrl_rx_rb)
{
fprintf(stderr, "Error: RX interface control block not found\n");
goto fail;
}
interface->if_rx_features = mqnic_reg_read32(interface->if_ctrl_rx_rb->regs, MQNIC_RB_IF_CTRL_RX_REG_FEATURES);
interface->max_rx_mtu = mqnic_reg_read32(interface->if_ctrl_rx_rb->regs, MQNIC_RB_IF_CTRL_TX_REG_MAX_MTU);
2021-12-24 13:49:41 -08:00
2021-12-29 22:31:46 -08:00
interface->event_queue_rb = find_reg_block(interface->rb_list, MQNIC_RB_EVENT_QM_TYPE, MQNIC_RB_EVENT_QM_VER, 0);
if (!interface->event_queue_rb)
{
fprintf(stderr, "Error: Event queue block not found\n");
goto fail;
}
interface->event_queue_offset = mqnic_reg_read32(interface->event_queue_rb->regs, MQNIC_RB_EVENT_QM_REG_OFFSET);
interface->event_queue_count = mqnic_reg_read32(interface->event_queue_rb->regs, MQNIC_RB_EVENT_QM_REG_COUNT);
interface->event_queue_stride = mqnic_reg_read32(interface->event_queue_rb->regs, MQNIC_RB_EVENT_QM_REG_STRIDE);
2021-12-24 13:49:41 -08:00
if (interface->event_queue_count > MQNIC_MAX_EVENT_RINGS)
interface->event_queue_count = MQNIC_MAX_EVENT_RINGS;
2021-12-29 22:31:46 -08:00
interface->tx_queue_rb = find_reg_block(interface->rb_list, MQNIC_RB_TX_QM_TYPE, MQNIC_RB_TX_QM_VER, 0);
if (!interface->tx_queue_rb)
{
fprintf(stderr, "Error: TX queue block not found\n");
goto fail;
}
interface->tx_queue_offset = mqnic_reg_read32(interface->tx_queue_rb->regs, MQNIC_RB_TX_QM_REG_OFFSET);
interface->tx_queue_count = mqnic_reg_read32(interface->tx_queue_rb->regs, MQNIC_RB_TX_QM_REG_COUNT);
interface->tx_queue_stride = mqnic_reg_read32(interface->tx_queue_rb->regs, MQNIC_RB_TX_QM_REG_STRIDE);
2021-12-24 13:49:41 -08:00
if (interface->tx_queue_count > MQNIC_MAX_TX_RINGS)
interface->tx_queue_count = MQNIC_MAX_TX_RINGS;
2021-12-29 22:31:46 -08:00
interface->tx_cpl_queue_rb = find_reg_block(interface->rb_list, MQNIC_RB_TX_CQM_TYPE, MQNIC_RB_TX_CQM_VER, 0);
if (!interface->tx_cpl_queue_rb)
{
fprintf(stderr, "Error: TX completion queue block not found\n");
goto fail;
}
interface->tx_cpl_queue_offset = mqnic_reg_read32(interface->tx_cpl_queue_rb->regs, MQNIC_RB_TX_CQM_REG_OFFSET);
interface->tx_cpl_queue_count = mqnic_reg_read32(interface->tx_cpl_queue_rb->regs, MQNIC_RB_TX_CQM_REG_COUNT);
interface->tx_cpl_queue_stride = mqnic_reg_read32(interface->tx_cpl_queue_rb->regs, MQNIC_RB_TX_CQM_REG_STRIDE);
2021-12-24 13:49:41 -08:00
if (interface->tx_cpl_queue_count > MQNIC_MAX_TX_CPL_RINGS)
interface->tx_cpl_queue_count = MQNIC_MAX_TX_CPL_RINGS;
2021-12-29 22:31:46 -08:00
interface->rx_queue_rb = find_reg_block(interface->rb_list, MQNIC_RB_RX_QM_TYPE, MQNIC_RB_RX_QM_VER, 0);
if (!interface->rx_queue_rb)
{
fprintf(stderr, "Error: RX queue block not found\n");
goto fail;
}
interface->rx_queue_offset = mqnic_reg_read32(interface->rx_queue_rb->regs, MQNIC_RB_RX_QM_REG_OFFSET);
interface->rx_queue_count = mqnic_reg_read32(interface->rx_queue_rb->regs, MQNIC_RB_RX_QM_REG_COUNT);
interface->rx_queue_stride = mqnic_reg_read32(interface->rx_queue_rb->regs, MQNIC_RB_RX_QM_REG_STRIDE);
2021-12-24 13:49:41 -08:00
if (interface->rx_queue_count > MQNIC_MAX_RX_RINGS)
interface->rx_queue_count = MQNIC_MAX_RX_RINGS;
2021-12-29 22:31:46 -08:00
interface->rx_cpl_queue_rb = find_reg_block(interface->rb_list, MQNIC_RB_RX_CQM_TYPE, MQNIC_RB_RX_CQM_VER, 0);
if (!interface->rx_cpl_queue_rb)
{
fprintf(stderr, "Error: RX completion queue block not found\n");
goto fail;
}
interface->rx_cpl_queue_offset = mqnic_reg_read32(interface->rx_cpl_queue_rb->regs, MQNIC_RB_RX_CQM_REG_OFFSET);
interface->rx_cpl_queue_count = mqnic_reg_read32(interface->rx_cpl_queue_rb->regs, MQNIC_RB_RX_CQM_REG_COUNT);
interface->rx_cpl_queue_stride = mqnic_reg_read32(interface->rx_cpl_queue_rb->regs, MQNIC_RB_RX_CQM_REG_STRIDE);
2021-12-24 13:49:41 -08:00
if (interface->rx_cpl_queue_count > MQNIC_MAX_RX_CPL_RINGS)
interface->rx_cpl_queue_count = MQNIC_MAX_RX_CPL_RINGS;
2021-12-29 22:31:46 -08:00
interface->port_count = 0;
while (interface->port_count < MQNIC_MAX_PORTS)
2021-12-24 13:49:41 -08:00
{
2021-12-29 22:31:46 -08:00
struct reg_block *sched_block_rb = find_reg_block(interface->rb_list, MQNIC_RB_SCHED_BLOCK_TYPE, MQNIC_RB_SCHED_BLOCK_VER, interface->port_count);
struct mqnic_port *port;
if (!sched_block_rb)
break;
port = mqnic_port_open(interface, interface->port_count, sched_block_rb);
2021-12-24 13:49:41 -08:00
if (!port)
goto fail;
2021-12-29 22:31:46 -08:00
interface->ports[interface->port_count++] = port;
2021-12-24 13:49:41 -08:00
}
return interface;
fail:
mqnic_if_close(interface);
return NULL;
}
void mqnic_if_close(struct mqnic_if *interface)
{
if (!interface)
return;
for (int k = 0; k < interface->port_count; k++)
{
if (!interface->ports[k])
continue;
mqnic_port_close(interface->ports[k]);
interface->ports[k] = NULL;
}
2021-12-29 22:31:46 -08:00
if (interface->rb_list)
free_reg_block_list(interface->rb_list);
2021-12-24 13:49:41 -08:00
free(interface);
}
2021-12-29 22:31:46 -08:00
struct mqnic_port *mqnic_port_open(struct mqnic_if *interface, int index, struct reg_block *block_rb)
2021-12-24 13:49:41 -08:00
{
struct mqnic_port *port = calloc(1, sizeof(struct mqnic_port));
if (!port)
return NULL;
2021-12-29 22:31:46 -08:00
int offset = mqnic_reg_read32(block_rb->regs, MQNIC_RB_SCHED_BLOCK_REG_OFFSET);
2021-12-24 13:49:41 -08:00
port->mqnic = interface->mqnic;
port->interface = interface;
port->index = index;
2021-12-29 22:31:46 -08:00
port->rb_list = enumerate_reg_block_list(interface->regs, offset, interface->regs_size);
2021-12-24 13:49:41 -08:00
2021-12-29 22:31:46 -08:00
if (!port->rb_list)
2021-12-24 13:49:41 -08:00
{
2021-12-29 22:31:46 -08:00
fprintf(stderr, "Error: filed to enumerate blocks\n");
2021-12-24 13:49:41 -08:00
goto fail;
}
2021-12-29 22:31:46 -08:00
port->sched_count = 0;
for (struct reg_block *rb = port->rb_list; rb->type && rb->version; rb++)
2021-12-24 13:49:41 -08:00
{
2021-12-29 22:31:46 -08:00
if (rb->type == MQNIC_RB_SCHED_RR_TYPE && rb->version == MQNIC_RB_SCHED_RR_VER)
{
struct mqnic_sched *sched = mqnic_sched_open(port, port->sched_count, rb);
2021-12-24 13:49:41 -08:00
2021-12-29 22:31:46 -08:00
if (!sched)
goto fail;
2021-12-24 13:49:41 -08:00
2021-12-29 22:31:46 -08:00
port->sched[port->sched_count++] = sched;
}
2021-12-24 13:49:41 -08:00
}
return port;
fail:
mqnic_port_close(port);
return NULL;
}
void mqnic_port_close(struct mqnic_port *port)
{
if (!port)
return;
for (int k = 0; k < port->sched_count; k++)
{
if (!port->sched[k])
continue;
mqnic_sched_close(port->sched[k]);
port->sched[k] = NULL;
}
2021-12-29 22:31:46 -08:00
if (port->rb_list)
free_reg_block_list(port->rb_list);
2021-12-24 13:49:41 -08:00
free(port);
}
2021-12-29 22:31:46 -08:00
struct mqnic_sched *mqnic_sched_open(struct mqnic_port *port, int index, struct reg_block *rb)
2021-12-24 13:49:41 -08:00
{
struct mqnic_sched *sched = calloc(1, sizeof(struct mqnic_sched));
if (!sched)
return NULL;
sched->mqnic = port->mqnic;
sched->interface = port->interface;
sched->port = port;
sched->index = index;
2021-12-29 22:31:46 -08:00
sched->rb = rb;
sched->regs = rb->base + mqnic_reg_read32(rb->regs, MQNIC_RB_SCHED_RR_REG_OFFSET);
2021-12-24 13:49:41 -08:00
if (sched->regs >= port->interface->regs+port->interface->regs_size)
{
fprintf(stderr, "Error: computed pointer out of range\n");
goto fail;
}
2021-12-29 22:31:46 -08:00
sched->type = rb->type;
sched->offset = mqnic_reg_read32(rb->regs, MQNIC_RB_SCHED_RR_REG_OFFSET);
sched->channel_count = mqnic_reg_read32(rb->regs, MQNIC_RB_SCHED_RR_REG_CH_COUNT);
sched->channel_stride = mqnic_reg_read32(rb->regs, MQNIC_RB_SCHED_RR_REG_CH_STRIDE);
2021-12-24 13:49:41 -08:00
return sched;
fail:
mqnic_sched_close(sched);
return NULL;
}
void mqnic_sched_close(struct mqnic_sched *sched)
{
if (!sched)
return;
free(sched);
}