2021-04-13 11:02:46 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * 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.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "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 COPYRIGHT
|
|
|
|
* OWNER 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
|
|
|
|
#include "spdk/env.h"
|
|
|
|
#include "spdk/util.h"
|
|
|
|
#include "spdk/memory.h"
|
|
|
|
#include "spdk/likely.h"
|
|
|
|
|
|
|
|
#include "spdk/log.h"
|
|
|
|
#include "spdk_internal/idxd.h"
|
|
|
|
|
|
|
|
#include "idxd.h"
|
|
|
|
|
|
|
|
struct spdk_user_idxd_device {
|
|
|
|
struct spdk_idxd_device idxd;
|
2021-04-13 11:30:07 +00:00
|
|
|
struct spdk_pci_device *device;
|
2021-04-13 11:02:46 +00:00
|
|
|
int sock_id;
|
2022-02-02 20:09:08 +00:00
|
|
|
struct idxd_registers *registers;
|
2021-04-13 11:02:46 +00:00
|
|
|
};
|
|
|
|
|
2021-04-13 11:30:07 +00:00
|
|
|
typedef bool (*spdk_idxd_probe_cb)(void *cb_ctx, struct spdk_pci_device *pci_dev);
|
|
|
|
|
2021-04-13 11:02:46 +00:00
|
|
|
#define __user_idxd(idxd) (struct spdk_user_idxd_device *)idxd
|
|
|
|
|
|
|
|
pthread_mutex_t g_driver_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
static struct spdk_idxd_device *idxd_attach(struct spdk_pci_device *device);
|
|
|
|
|
|
|
|
/* Used for control commands, not for descriptor submission. */
|
|
|
|
static int
|
|
|
|
idxd_wait_cmd(struct spdk_idxd_device *idxd, int _timeout)
|
|
|
|
{
|
|
|
|
uint32_t timeout = _timeout;
|
2022-02-02 17:45:10 +00:00
|
|
|
union idxd_cmdsts_register cmd_status = {};
|
2022-02-02 20:09:08 +00:00
|
|
|
struct spdk_user_idxd_device *user_idxd = __user_idxd(idxd);
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
cmd_status.raw = spdk_mmio_read_4(&user_idxd->registers->cmdsts.raw);
|
2021-04-13 11:02:46 +00:00
|
|
|
while (cmd_status.active && --timeout) {
|
|
|
|
usleep(1);
|
2022-02-02 20:09:08 +00:00
|
|
|
cmd_status.raw = spdk_mmio_read_4(&user_idxd->registers->cmdsts.raw);
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for timeout */
|
|
|
|
if (timeout == 0 && cmd_status.active) {
|
|
|
|
SPDK_ERRLOG("Command timeout, waited %u\n", _timeout);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for error */
|
|
|
|
if (cmd_status.err) {
|
|
|
|
SPDK_ERRLOG("Command status reg reports error 0x%x\n", cmd_status.err);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
idxd_unmap_pci_bar(struct spdk_idxd_device *idxd, int bar)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
void *addr = NULL;
|
|
|
|
struct spdk_user_idxd_device *user_idxd = __user_idxd(idxd);
|
|
|
|
|
|
|
|
if (bar == IDXD_MMIO_BAR) {
|
2022-02-02 20:09:08 +00:00
|
|
|
addr = (void *)user_idxd->registers;
|
2021-04-13 11:02:46 +00:00
|
|
|
} else if (bar == IDXD_WQ_BAR) {
|
2022-01-21 19:50:31 +00:00
|
|
|
addr = (void *)idxd->portal;
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (addr) {
|
2021-04-13 11:30:07 +00:00
|
|
|
rc = spdk_pci_device_unmap_bar(user_idxd->device, 0, addr);
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
idxd_map_pci_bars(struct spdk_idxd_device *idxd)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
void *addr;
|
|
|
|
uint64_t phys_addr, size;
|
|
|
|
struct spdk_user_idxd_device *user_idxd = __user_idxd(idxd);
|
|
|
|
|
2021-04-13 11:30:07 +00:00
|
|
|
rc = spdk_pci_device_map_bar(user_idxd->device, IDXD_MMIO_BAR, &addr, &phys_addr, &size);
|
2021-04-13 11:02:46 +00:00
|
|
|
if (rc != 0 || addr == NULL) {
|
|
|
|
SPDK_ERRLOG("pci_device_map_range failed with error code %d\n", rc);
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-02 20:09:08 +00:00
|
|
|
user_idxd->registers = (struct idxd_registers *)addr;
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2021-04-13 11:30:07 +00:00
|
|
|
rc = spdk_pci_device_map_bar(user_idxd->device, IDXD_WQ_BAR, &addr, &phys_addr, &size);
|
2021-04-13 11:02:46 +00:00
|
|
|
if (rc != 0 || addr == NULL) {
|
|
|
|
SPDK_ERRLOG("pci_device_map_range failed with error code %d\n", rc);
|
|
|
|
rc = idxd_unmap_pci_bar(idxd, IDXD_MMIO_BAR);
|
|
|
|
if (rc) {
|
|
|
|
SPDK_ERRLOG("unable to unmap MMIO bar\n");
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2022-01-21 19:50:31 +00:00
|
|
|
idxd->portal = addr;
|
2021-04-13 11:02:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-01-05 21:38:52 +00:00
|
|
|
static void
|
|
|
|
idxd_disable_dev(struct spdk_idxd_device *idxd)
|
|
|
|
{
|
|
|
|
int rc;
|
2022-02-02 20:09:08 +00:00
|
|
|
struct spdk_user_idxd_device *user_idxd = __user_idxd(idxd);
|
|
|
|
union idxd_cmd_register cmd = {};
|
2022-01-05 21:38:52 +00:00
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
cmd.command_code = IDXD_DISABLE_DEV;
|
|
|
|
|
|
|
|
spdk_mmio_write_4(&user_idxd->registers->cmd.raw, cmd.raw);
|
2022-01-05 21:38:52 +00:00
|
|
|
rc = idxd_wait_cmd(idxd, IDXD_REGISTER_TIMEOUT_US);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Error disabling device %u\n", rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:02:46 +00:00
|
|
|
static int
|
|
|
|
idxd_reset_dev(struct spdk_idxd_device *idxd)
|
|
|
|
{
|
|
|
|
int rc;
|
2022-02-02 20:09:08 +00:00
|
|
|
struct spdk_user_idxd_device *user_idxd = __user_idxd(idxd);
|
|
|
|
union idxd_cmd_register cmd = {};
|
|
|
|
|
|
|
|
cmd.command_code = IDXD_RESET_DEVICE;
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
spdk_mmio_write_4(&user_idxd->registers->cmd.raw, cmd.raw);
|
2021-04-13 11:02:46 +00:00
|
|
|
rc = idxd_wait_cmd(idxd, IDXD_REGISTER_TIMEOUT_US);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Error resetting device %u\n", rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build group config based on getting info from the device combined
|
|
|
|
* with the defined configuration. Once built, it is written to the
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
idxd_group_config(struct spdk_idxd_device *idxd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct spdk_user_idxd_device *user_idxd = __user_idxd(idxd);
|
2022-02-02 20:09:08 +00:00
|
|
|
union idxd_groupcap_register groupcap;
|
|
|
|
union idxd_enginecap_register enginecap;
|
|
|
|
union idxd_wqcap_register wqcap;
|
|
|
|
union idxd_offsets_register table_offsets;
|
2022-02-03 21:18:31 +00:00
|
|
|
struct idxd_grptbl *grptbl;
|
2022-02-02 20:09:08 +00:00
|
|
|
|
|
|
|
groupcap.raw = spdk_mmio_read_8(&user_idxd->registers->groupcap.raw);
|
|
|
|
enginecap.raw = spdk_mmio_read_8(&user_idxd->registers->enginecap.raw);
|
|
|
|
wqcap.raw = spdk_mmio_read_8(&user_idxd->registers->wqcap.raw);
|
|
|
|
|
|
|
|
if (wqcap.num_wqs < 1) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
assert(groupcap.num_groups >= 1);
|
2022-03-02 19:49:03 +00:00
|
|
|
idxd->groups = calloc(1, sizeof(struct idxd_group));
|
2021-04-13 11:02:46 +00:00
|
|
|
if (idxd->groups == NULL) {
|
|
|
|
SPDK_ERRLOG("Failed to allocate group memory\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
for (i = 0; i < enginecap.num_engines; i++) {
|
2022-03-02 19:49:03 +00:00
|
|
|
idxd->groups->grpcfg.engines |= (1 << i);
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 19:49:03 +00:00
|
|
|
idxd->groups->grpcfg.wqs[0] = 0x1;
|
2022-02-02 20:09:08 +00:00
|
|
|
idxd->groups->grpcfg.flags.read_buffers_allowed = groupcap.read_bufs;
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2022-03-02 19:49:03 +00:00
|
|
|
idxd->groups->idxd = idxd;
|
|
|
|
idxd->groups->id = 0;
|
2022-02-23 23:48:56 +00:00
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
table_offsets.raw[0] = spdk_mmio_read_8(&user_idxd->registers->offsets.raw[0]);
|
|
|
|
table_offsets.raw[1] = spdk_mmio_read_8(&user_idxd->registers->offsets.raw[1]);
|
|
|
|
|
2022-02-03 21:18:31 +00:00
|
|
|
grptbl = (struct idxd_grptbl *)((uint8_t *)user_idxd->registers + (table_offsets.grpcfg *
|
2022-02-02 20:09:08 +00:00
|
|
|
IDXD_TABLE_OFFSET_MULT));
|
2022-03-02 19:49:03 +00:00
|
|
|
|
|
|
|
/* GRPWQCFG, work queues config */
|
2022-02-03 21:18:31 +00:00
|
|
|
spdk_mmio_write_8((uint64_t *)&grptbl->group[0].wqs[0], idxd->groups->grpcfg.wqs[0]);
|
2022-03-02 19:49:03 +00:00
|
|
|
|
|
|
|
/* GRPENGCFG, engine config */
|
2022-02-03 21:18:31 +00:00
|
|
|
spdk_mmio_write_8((uint64_t *)&grptbl->group[0].engines, idxd->groups->grpcfg.engines);
|
2022-03-02 19:49:03 +00:00
|
|
|
|
|
|
|
/* GRPFLAGS, flags config */
|
2022-02-03 21:18:31 +00:00
|
|
|
spdk_mmio_write_8((uint64_t *)&grptbl->group[0].flags, idxd->groups->grpcfg.flags.raw);
|
2021-04-13 11:02:46 +00:00
|
|
|
|
|
|
|
/*
|
2022-03-02 19:49:03 +00:00
|
|
|
* Now write the other groups to zero them out
|
2021-04-13 11:02:46 +00:00
|
|
|
*/
|
2022-02-02 20:09:08 +00:00
|
|
|
for (i = 1 ; i < groupcap.num_groups; i++) {
|
2021-04-13 11:02:46 +00:00
|
|
|
/* GRPWQCFG, work queues config */
|
2022-02-03 21:18:31 +00:00
|
|
|
spdk_mmio_write_8((uint64_t *)&grptbl->group[i].wqs[0], 0UL);
|
2021-04-13 11:02:46 +00:00
|
|
|
|
|
|
|
/* GRPENGCFG, engine config */
|
2022-02-03 21:18:31 +00:00
|
|
|
spdk_mmio_write_8((uint64_t *)&grptbl->group[i].engines, 0UL);
|
2021-04-13 11:02:46 +00:00
|
|
|
|
|
|
|
/* GRPFLAGS, flags config */
|
2022-02-03 21:18:31 +00:00
|
|
|
spdk_mmio_write_8((uint64_t *)&grptbl->group[i].flags, 0UL);
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build work queue (WQ) config based on getting info from the device combined
|
|
|
|
* with the defined configuration. Once built, it is written to the device.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
idxd_wq_config(struct spdk_user_idxd_device *user_idxd)
|
|
|
|
{
|
2022-03-02 19:49:03 +00:00
|
|
|
uint32_t j;
|
2021-04-13 11:02:46 +00:00
|
|
|
struct spdk_idxd_device *idxd = &user_idxd->idxd;
|
2022-02-02 20:09:08 +00:00
|
|
|
uint32_t wq_size;
|
|
|
|
union idxd_wqcap_register wqcap;
|
|
|
|
union idxd_offsets_register table_offsets;
|
2022-02-03 21:18:31 +00:00
|
|
|
struct idxd_wqtbl *wqtbl;
|
2022-02-03 21:29:40 +00:00
|
|
|
union idxd_wqcfg wqcfg;
|
2022-02-02 20:09:08 +00:00
|
|
|
|
|
|
|
wqcap.raw = spdk_mmio_read_8(&user_idxd->registers->wqcap.raw);
|
|
|
|
|
|
|
|
wq_size = wqcap.total_wq_size;
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2022-02-03 21:18:31 +00:00
|
|
|
assert(sizeof(wqtbl->wq[0]) == 1 << (WQCFG_SHIFT + wqcap.wqcfg_size));
|
|
|
|
|
2021-11-11 19:51:37 +00:00
|
|
|
SPDK_DEBUGLOG(idxd, "Total ring slots available space 0x%x, so per work queue is 0x%x\n",
|
2022-02-02 20:09:08 +00:00
|
|
|
wqcap.total_wq_size, wq_size);
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
idxd->total_wq_size = wqcap.total_wq_size;
|
2021-06-05 14:18:11 +00:00
|
|
|
/* Spread the channels we allow per device based on the total number of WQE to try
|
|
|
|
* and achieve optimal performance for common cases.
|
|
|
|
*/
|
|
|
|
idxd->chan_per_device = (idxd->total_wq_size >= 128) ? 8 : 4;
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
table_offsets.raw[0] = spdk_mmio_read_8(&user_idxd->registers->offsets.raw[0]);
|
|
|
|
table_offsets.raw[1] = spdk_mmio_read_8(&user_idxd->registers->offsets.raw[1]);
|
|
|
|
|
2022-02-03 21:18:31 +00:00
|
|
|
wqtbl = (struct idxd_wqtbl *)((uint8_t *)user_idxd->registers + (table_offsets.wqcfg *
|
|
|
|
IDXD_TABLE_OFFSET_MULT));
|
2022-02-02 20:09:08 +00:00
|
|
|
|
2022-03-02 19:49:03 +00:00
|
|
|
/* Per spec we need to read in existing values first so we don't zero out something we
|
|
|
|
* didn't touch when we write the cfg register out below.
|
|
|
|
*/
|
|
|
|
for (j = 0 ; j < (sizeof(union idxd_wqcfg) / sizeof(uint32_t)); j++) {
|
2022-02-03 21:29:40 +00:00
|
|
|
wqcfg.raw[j] = spdk_mmio_read_4(&wqtbl->wq[0].raw[j]);
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
2022-02-02 20:09:08 +00:00
|
|
|
|
2022-02-03 21:29:40 +00:00
|
|
|
wqcfg.wq_size = wq_size;
|
|
|
|
wqcfg.mode = WQ_MODE_DEDICATED;
|
|
|
|
wqcfg.max_batch_shift = LOG2_WQ_MAX_BATCH;
|
|
|
|
wqcfg.max_xfer_shift = LOG2_WQ_MAX_XFER;
|
|
|
|
wqcfg.wq_state = WQ_ENABLED;
|
|
|
|
wqcfg.priority = WQ_PRIORITY_1;
|
2022-03-02 19:49:03 +00:00
|
|
|
|
2021-04-13 11:02:46 +00:00
|
|
|
/*
|
2022-01-05 20:56:54 +00:00
|
|
|
* Now write the work queue config to the device for configured queues
|
2021-04-13 11:02:46 +00:00
|
|
|
*/
|
2022-03-02 19:49:03 +00:00
|
|
|
for (j = 0 ; j < (sizeof(union idxd_wqcfg) / sizeof(uint32_t)); j++) {
|
2022-02-03 21:29:40 +00:00
|
|
|
spdk_mmio_write_4(&wqtbl->wq[0].raw[j], wqcfg.raw[j]);
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
idxd_device_configure(struct spdk_user_idxd_device *user_idxd)
|
|
|
|
{
|
2022-02-02 20:09:08 +00:00
|
|
|
int rc = 0;
|
2022-02-02 17:55:01 +00:00
|
|
|
union idxd_gensts_register gensts_reg;
|
2021-04-13 11:02:46 +00:00
|
|
|
struct spdk_idxd_device *idxd = &user_idxd->idxd;
|
2022-02-02 20:09:08 +00:00
|
|
|
union idxd_cmd_register cmd = {};
|
2021-04-13 11:02:46 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Map BAR0 and BAR2
|
|
|
|
*/
|
|
|
|
rc = idxd_map_pci_bars(idxd);
|
|
|
|
if (rc) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset the device
|
|
|
|
*/
|
|
|
|
rc = idxd_reset_dev(idxd);
|
|
|
|
if (rc) {
|
|
|
|
goto err_reset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure groups and work queues.
|
|
|
|
*/
|
|
|
|
rc = idxd_group_config(idxd);
|
|
|
|
if (rc) {
|
|
|
|
goto err_group_cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = idxd_wq_config(user_idxd);
|
|
|
|
if (rc) {
|
|
|
|
goto err_wq_cfg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable the device
|
|
|
|
*/
|
2022-02-02 20:09:08 +00:00
|
|
|
gensts_reg.raw = spdk_mmio_read_4(&user_idxd->registers->gensts.raw);
|
2022-02-02 17:55:01 +00:00
|
|
|
assert(gensts_reg.state == IDXD_DEVICE_STATE_DISABLED);
|
2021-04-13 11:02:46 +00:00
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
cmd.command_code = IDXD_ENABLE_DEV;
|
|
|
|
|
|
|
|
spdk_mmio_write_4(&user_idxd->registers->cmd.raw, cmd.raw);
|
2021-04-13 11:02:46 +00:00
|
|
|
rc = idxd_wait_cmd(idxd, IDXD_REGISTER_TIMEOUT_US);
|
2022-02-02 20:09:08 +00:00
|
|
|
gensts_reg.raw = spdk_mmio_read_4(&user_idxd->registers->gensts.raw);
|
2022-02-02 17:55:01 +00:00
|
|
|
if ((rc < 0) || (gensts_reg.state != IDXD_DEVICE_STATE_ENABLED)) {
|
2021-04-13 11:02:46 +00:00
|
|
|
rc = -EINVAL;
|
|
|
|
SPDK_ERRLOG("Error enabling device %u\n", rc);
|
|
|
|
goto err_device_enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-03-02 19:49:03 +00:00
|
|
|
* Enable the work queue that we've configured
|
2021-04-13 11:02:46 +00:00
|
|
|
*/
|
2022-02-02 20:09:08 +00:00
|
|
|
cmd.command_code = IDXD_ENABLE_WQ;
|
|
|
|
cmd.operand = 0;
|
|
|
|
|
|
|
|
spdk_mmio_write_4(&user_idxd->registers->cmd.raw, cmd.raw);
|
2022-03-02 19:49:03 +00:00
|
|
|
rc = idxd_wait_cmd(idxd, IDXD_REGISTER_TIMEOUT_US);
|
|
|
|
if (rc < 0) {
|
|
|
|
SPDK_ERRLOG("Error enabling work queues 0x%x\n", rc);
|
|
|
|
goto err_wq_enable;
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
2022-02-02 17:55:01 +00:00
|
|
|
if ((rc == 0) && (gensts_reg.state == IDXD_DEVICE_STATE_ENABLED)) {
|
2022-02-02 20:09:08 +00:00
|
|
|
SPDK_DEBUGLOG(idxd, "Device enabled\n");
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
err_wq_enable:
|
|
|
|
err_device_enable:
|
|
|
|
err_wq_cfg:
|
|
|
|
free(idxd->groups);
|
|
|
|
err_group_cfg:
|
|
|
|
err_reset:
|
|
|
|
idxd_unmap_pci_bar(idxd, IDXD_MMIO_BAR);
|
|
|
|
idxd_unmap_pci_bar(idxd, IDXD_MMIO_BAR);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
user_idxd_device_destruct(struct spdk_idxd_device *idxd)
|
|
|
|
{
|
2021-04-13 11:30:07 +00:00
|
|
|
struct spdk_user_idxd_device *user_idxd = __user_idxd(idxd);
|
|
|
|
|
2022-01-05 21:38:52 +00:00
|
|
|
idxd_disable_dev(idxd);
|
|
|
|
|
2021-04-13 11:02:46 +00:00
|
|
|
idxd_unmap_pci_bar(idxd, IDXD_MMIO_BAR);
|
|
|
|
idxd_unmap_pci_bar(idxd, IDXD_WQ_BAR);
|
|
|
|
free(idxd->groups);
|
|
|
|
|
2021-04-13 11:30:07 +00:00
|
|
|
spdk_pci_device_detach(user_idxd->device);
|
|
|
|
free(user_idxd);
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct idxd_enum_ctx {
|
|
|
|
spdk_idxd_probe_cb probe_cb;
|
|
|
|
spdk_idxd_attach_cb attach_cb;
|
|
|
|
void *cb_ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This function must only be called while holding g_driver_lock */
|
|
|
|
static int
|
|
|
|
idxd_enum_cb(void *ctx, struct spdk_pci_device *pci_dev)
|
|
|
|
{
|
|
|
|
struct idxd_enum_ctx *enum_ctx = ctx;
|
|
|
|
struct spdk_idxd_device *idxd;
|
|
|
|
|
|
|
|
if (enum_ctx->probe_cb(enum_ctx->cb_ctx, pci_dev)) {
|
|
|
|
idxd = idxd_attach(pci_dev);
|
|
|
|
if (idxd == NULL) {
|
|
|
|
SPDK_ERRLOG("idxd_attach() failed\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:30:07 +00:00
|
|
|
enum_ctx->attach_cb(enum_ctx->cb_ctx, idxd);
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:30:07 +00:00
|
|
|
|
|
|
|
static bool
|
|
|
|
probe_cb(void *cb_ctx, struct spdk_pci_device *pci_dev)
|
|
|
|
{
|
2021-11-11 19:51:37 +00:00
|
|
|
struct spdk_pci_addr pci_addr __attribute__((unused));
|
|
|
|
|
|
|
|
pci_addr = spdk_pci_device_get_addr(pci_dev);
|
|
|
|
|
|
|
|
SPDK_DEBUGLOG(idxd,
|
|
|
|
" Found matching device at %04x:%02x:%02x.%x vendor:0x%04x device:0x%04x\n",
|
|
|
|
pci_addr.domain,
|
|
|
|
pci_addr.bus,
|
|
|
|
pci_addr.dev,
|
|
|
|
pci_addr.func,
|
|
|
|
spdk_pci_device_get_vendor_id(pci_dev),
|
|
|
|
spdk_pci_device_get_device_id(pci_dev));
|
2021-04-13 11:30:07 +00:00
|
|
|
|
|
|
|
/* Claim the device in case conflict with other process */
|
|
|
|
if (spdk_pci_device_claim(pci_dev) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-13 11:02:46 +00:00
|
|
|
static int
|
2021-04-13 11:30:07 +00:00
|
|
|
user_idxd_probe(void *cb_ctx, spdk_idxd_attach_cb attach_cb)
|
2021-04-13 11:02:46 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct idxd_enum_ctx enum_ctx;
|
|
|
|
|
|
|
|
enum_ctx.probe_cb = probe_cb;
|
|
|
|
enum_ctx.attach_cb = attach_cb;
|
|
|
|
enum_ctx.cb_ctx = cb_ctx;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&g_driver_lock);
|
|
|
|
rc = spdk_pci_enumerate(spdk_pci_idxd_get_driver(), idxd_enum_cb, &enum_ctx);
|
|
|
|
pthread_mutex_unlock(&g_driver_lock);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:29:27 +00:00
|
|
|
static void
|
|
|
|
user_idxd_dump_sw_err(struct spdk_idxd_device *idxd, void *portal)
|
|
|
|
{
|
2022-02-02 20:09:08 +00:00
|
|
|
struct spdk_user_idxd_device *user_idxd = __user_idxd(idxd);
|
|
|
|
union idxd_swerr_register sw_err;
|
2021-07-08 10:29:27 +00:00
|
|
|
uint16_t i;
|
|
|
|
|
2022-02-02 20:09:08 +00:00
|
|
|
SPDK_NOTICELOG("SW Error Raw:");
|
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
sw_err.raw[i] = spdk_mmio_read_8(&user_idxd->registers->sw_err.raw[i]);
|
|
|
|
SPDK_NOTICELOG(" 0x%lx\n", sw_err.raw[i]);
|
2021-07-08 10:29:27 +00:00
|
|
|
}
|
2022-02-02 20:09:08 +00:00
|
|
|
|
|
|
|
SPDK_NOTICELOG("SW Error error code: %#x\n", (uint8_t)(sw_err.error));
|
|
|
|
SPDK_NOTICELOG("SW Error WQ index: %u\n", (uint8_t)(sw_err.wq_idx));
|
|
|
|
SPDK_NOTICELOG("SW Error Operation: %u\n", (uint8_t)(sw_err.operation));
|
2021-07-08 10:29:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 11:02:46 +00:00
|
|
|
static char *
|
|
|
|
user_idxd_portal_get_addr(struct spdk_idxd_device *idxd)
|
|
|
|
{
|
2022-01-21 19:50:31 +00:00
|
|
|
return (char *)idxd->portal;
|
2021-04-13 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct spdk_idxd_impl g_user_idxd_impl = {
|
|
|
|
.name = "user",
|
|
|
|
.probe = user_idxd_probe,
|
|
|
|
.destruct = user_idxd_device_destruct,
|
2021-07-08 10:29:27 +00:00
|
|
|
.dump_sw_error = user_idxd_dump_sw_err,
|
2022-02-02 20:21:50 +00:00
|
|
|
.portal_get_addr = user_idxd_portal_get_addr
|
2021-04-13 11:02:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Caller must hold g_driver_lock */
|
|
|
|
static struct spdk_idxd_device *
|
|
|
|
idxd_attach(struct spdk_pci_device *device)
|
|
|
|
{
|
|
|
|
struct spdk_user_idxd_device *user_idxd;
|
|
|
|
struct spdk_idxd_device *idxd;
|
|
|
|
uint32_t cmd_reg;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
user_idxd = calloc(1, sizeof(struct spdk_user_idxd_device));
|
|
|
|
if (user_idxd == NULL) {
|
|
|
|
SPDK_ERRLOG("Failed to allocate memory for user_idxd device.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
idxd = &user_idxd->idxd;
|
2021-04-13 11:30:07 +00:00
|
|
|
user_idxd->device = device;
|
2021-04-13 11:02:46 +00:00
|
|
|
idxd->impl = &g_user_idxd_impl;
|
2021-09-01 20:17:46 +00:00
|
|
|
idxd->socket_id = device->socket_id;
|
2021-04-13 11:02:46 +00:00
|
|
|
pthread_mutex_init(&idxd->num_channels_lock, NULL);
|
|
|
|
|
|
|
|
/* Enable PCI busmaster. */
|
|
|
|
spdk_pci_device_cfg_read32(device, &cmd_reg, 4);
|
|
|
|
cmd_reg |= 0x4;
|
|
|
|
spdk_pci_device_cfg_write32(device, cmd_reg, 4);
|
|
|
|
|
|
|
|
rc = idxd_device_configure(user_idxd);
|
|
|
|
if (rc) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return idxd;
|
|
|
|
err:
|
|
|
|
user_idxd_device_destruct(idxd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPDK_IDXD_IMPL_REGISTER(user, &g_user_idxd_impl);
|