#include <linux/iopoll.h>
#include "snet_vdpa.h"
#define SNET_DEVICE_ID 0x1000
#define SNET_SIGNATURE 0xD0D06363
#define SNET_CFG_VERSION 0x2
#define SNET_QUEUE_ALIGNMENT PAGE_SIZE
#define SNET_KICK_VAL 0x1
#define SNET_CONFIG_OFF 0x0
#define SNET_DETECT_TIMEOUT 5000000
#define SNET_READ_CFG_TIMEOUT 3000000
#define SNET_GENERAL_CFG_LEN 36
#define SNET_GENERAL_CFG_VQ_LEN 40
static struct snet *vdpa_to_snet(struct vdpa_device *vdpa)
{
return container_of(vdpa, struct snet, vdpa);
}
static irqreturn_t snet_cfg_irq_hndlr(int irq, void *data)
{
struct snet *snet = data;
if (likely(snet->cb.callback))
return snet->cb.callback(snet->cb.private);
return IRQ_HANDLED;
}
static irqreturn_t snet_vq_irq_hndlr(int irq, void *data)
{
struct snet_vq *vq = data;
if (likely(vq->cb.callback))
return vq->cb.callback(vq->cb.private);
return IRQ_HANDLED;
}
static void snet_free_irqs(struct snet *snet)
{
struct psnet *psnet = snet->psnet;
struct pci_dev *pdev;
u32 i;
if (PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF))
pdev = snet->pdev->physfn;
else
pdev = snet->pdev;
if (snet->cfg_irq != -1) {
devm_free_irq(&pdev->dev, snet->cfg_irq, snet);
snet->cfg_irq = -1;
}
for (i = 0; i < snet->cfg->vq_num; i++) {
if (snet->vqs[i] && snet->vqs[i]->irq != -1) {
devm_free_irq(&pdev->dev, snet->vqs[i]->irq, snet->vqs[i]);
snet->vqs[i]->irq = -1;
}
}
}
static int snet_set_vq_address(struct vdpa_device *vdev, u16 idx, u64 desc_area,
u64 driver_area, u64 device_area)
{
struct snet *snet = vdpa_to_snet(vdev);
snet->vqs[idx]->desc_area = desc_area;
snet->vqs[idx]->driver_area = driver_area;
snet->vqs[idx]->device_area = device_area;
return 0;
}
static void snet_set_vq_num(struct vdpa_device *vdev, u16 idx, u32 num)
{
struct snet *snet = vdpa_to_snet(vdev);
snet->vqs[idx]->num = num;
}
static void snet_kick_vq(struct vdpa_device *vdev, u16 idx)
{
struct snet *snet = vdpa_to_snet(vdev);
if (unlikely(!snet->vqs[idx]->ready))
return;
iowrite32(SNET_KICK_VAL, snet->vqs[idx]->kick_ptr);
}
static void snet_kick_vq_with_data(struct vdpa_device *vdev, u32 data)
{
struct snet *snet = vdpa_to_snet(vdev);
u16 idx = data & 0xFFFF;
if (unlikely(!snet->vqs[idx]->ready))
return;
iowrite32((data & 0xFFFF0000) | SNET_KICK_VAL, snet->vqs[idx]->kick_ptr);
}
static void snet_set_vq_cb(struct vdpa_device *vdev, u16 idx, struct vdpa_callback *cb)
{
struct snet *snet = vdpa_to_snet(vdev);
snet->vqs[idx]->cb.callback = cb->callback;
snet->vqs[idx]->cb.private = cb->private;
}
static void snet_set_vq_ready(struct vdpa_device *vdev, u16 idx, bool ready)
{
struct snet *snet = vdpa_to_snet(vdev);
snet->vqs[idx]->ready = ready;
}
static bool snet_get_vq_ready(struct vdpa_device *vdev, u16 idx)
{
struct snet *snet = vdpa_to_snet(vdev);
return snet->vqs[idx]->ready;
}
static bool snet_vq_state_is_initial(struct snet *snet, const struct vdpa_vq_state *state)
{
if (SNET_HAS_FEATURE(snet, VIRTIO_F_RING_PACKED)) {
const struct vdpa_vq_state_packed *p = &state->packed;
if (p->last_avail_counter == 1 && p->last_used_counter == 1 &&
p->last_avail_idx == 0 && p->last_used_idx == 0)
return true;
} else {
const struct vdpa_vq_state_split *s = &state->split;
if (s->avail_index == 0)
return true;
}
return false;
}
static int snet_set_vq_state(struct vdpa_device *vdev, u16 idx, const struct vdpa_vq_state *state)
{
struct snet *snet = vdpa_to_snet(vdev);
if (SNET_CFG_VER(snet, 2)) {
memcpy(&snet->vqs[idx]->vq_state, state, sizeof(*state));
return 0;
}
if (snet_vq_state_is_initial(snet, state))
return 0;
return -EOPNOTSUPP;
}
static int snet_get_vq_state(struct vdpa_device *vdev, u16 idx, struct vdpa_vq_state *state)
{
struct snet *snet = vdpa_to_snet(vdev);
return snet_read_vq_state(snet, idx, state);
}
static int snet_get_vq_irq(struct vdpa_device *vdev, u16 idx)
{
struct snet *snet = vdpa_to_snet(vdev);
return snet->vqs[idx]->irq;
}
static u32 snet_get_vq_align(struct vdpa_device *vdev)
{
return (u32)SNET_QUEUE_ALIGNMENT;
}
static int snet_reset_dev(struct snet *snet)
{
struct pci_dev *pdev = snet->pdev;
int ret = 0;
u32 i;
if (!snet->status)
return 0;
if (snet->status & VIRTIO_CONFIG_S_DRIVER_OK)
ret = snet_destroy_dev(snet);
for (i = 0; i < snet->cfg->vq_num; i++) {
if (!snet->vqs[i])
continue;
snet->vqs[i]->cb.callback = NULL;
snet->vqs[i]->cb.private = NULL;
snet->vqs[i]->desc_area = 0;
snet->vqs[i]->device_area = 0;
snet->vqs[i]->driver_area = 0;
snet->vqs[i]->ready = false;
}
snet->cb.callback = NULL;
snet->cb.private = NULL;
snet_free_irqs(snet);
snet->status = 0;
snet->dpu_ready = false;
if (ret)
SNET_WARN(pdev, "Incomplete reset to SNET[%u] device, err: %d\n", snet->sid, ret);
else
SNET_DBG(pdev, "Reset SNET[%u] device\n", snet->sid);
return 0;
}
static int snet_reset(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
return snet_reset_dev(snet);
}
static size_t snet_get_config_size(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
return (size_t)snet->cfg->cfg_size;
}
static u64 snet_get_features(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
return snet->cfg->features;
}
static int snet_set_drv_features(struct vdpa_device *vdev, u64 features)
{
struct snet *snet = vdpa_to_snet(vdev);
snet->negotiated_features = snet->cfg->features & features;
return 0;
}
static u64 snet_get_drv_features(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
return snet->negotiated_features;
}
static u16 snet_get_vq_num_max(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
return (u16)snet->cfg->vq_size;
}
static void snet_set_config_cb(struct vdpa_device *vdev, struct vdpa_callback *cb)
{
struct snet *snet = vdpa_to_snet(vdev);
snet->cb.callback = cb->callback;
snet->cb.private = cb->private;
}
static u32 snet_get_device_id(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
return snet->cfg->virtio_id;
}
static u32 snet_get_vendor_id(struct vdpa_device *vdev)
{
return (u32)PCI_VENDOR_ID_SOLIDRUN;
}
static u8 snet_get_status(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
return snet->status;
}
static int snet_write_conf(struct snet *snet)
{
u32 off, i, tmp;
int ret;
if (snet->dpu_ready)
return true;
off = snet->psnet->cfg.host_cfg_off;
off += 4;
snet_write32(snet, off, snet->psnet->negotiated_cfg_ver);
off += 4;
snet_write32(snet, off, snet->sid);
off += 4;
snet_write32(snet, off, snet->cfg->vq_num);
off += 4;
snet_write32(snet, off, snet->cfg_irq_idx);
off += 4;
snet_write64(snet, off, snet->negotiated_features);
off += 8;
off += 8;
for (i = 0 ; i < snet->cfg->vq_num ; i++) {
tmp = (i << 16) | (snet->vqs[i]->num & 0xFFFF);
snet_write32(snet, off, tmp);
off += 4;
snet_write32(snet, off, snet->vqs[i]->irq_idx);
off += 4;
snet_write64(snet, off, snet->vqs[i]->desc_area);
off += 8;
snet_write64(snet, off, snet->vqs[i]->device_area);
off += 8;
snet_write64(snet, off, snet->vqs[i]->driver_area);
off += 8;
if (SNET_CFG_VER(snet, 2))
snet_write32(snet, off, *(u32 *)&snet->vqs[i]->vq_state);
off += 4;
off += 4;
}
snet_write32(snet, snet->psnet->cfg.host_cfg_off, SNET_SIGNATURE);
ret = readx_poll_timeout(ioread32, snet->bar + snet->psnet->cfg.host_cfg_off,
tmp, !tmp, 10, SNET_READ_CFG_TIMEOUT);
if (ret) {
SNET_ERR(snet->pdev, "Timeout waiting for the DPU to read the config\n");
return false;
}
snet->dpu_ready = true;
return true;
}
static int snet_request_irqs(struct pci_dev *pdev, struct snet *snet)
{
int ret, i, irq;
irq = pci_irq_vector(pdev, snet->cfg_irq_idx);
ret = devm_request_irq(&pdev->dev, irq, snet_cfg_irq_hndlr, 0,
snet->cfg_irq_name, snet);
if (ret) {
SNET_ERR(pdev, "Failed to request IRQ\n");
return ret;
}
snet->cfg_irq = irq;
for (i = 0; i < snet->cfg->vq_num; i++) {
irq = pci_irq_vector(pdev, snet->vqs[i]->irq_idx);
ret = devm_request_irq(&pdev->dev, irq, snet_vq_irq_hndlr, 0,
snet->vqs[i]->irq_name, snet->vqs[i]);
if (ret) {
SNET_ERR(pdev, "Failed to request IRQ\n");
return ret;
}
snet->vqs[i]->irq = irq;
}
return 0;
}
static void snet_set_status(struct vdpa_device *vdev, u8 status)
{
struct snet *snet = vdpa_to_snet(vdev);
struct psnet *psnet = snet->psnet;
struct pci_dev *pdev = snet->pdev;
int ret;
bool pf_irqs;
if (status == snet->status)
return;
if ((status & VIRTIO_CONFIG_S_DRIVER_OK) &&
!(snet->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
pf_irqs = PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF);
ret = snet_request_irqs(pf_irqs ? pdev->physfn : pdev, snet);
if (ret)
goto set_err;
if (snet_write_conf(snet)) {
SNET_INFO(pdev, "Create SNET[%u] device\n", snet->sid);
} else {
snet_free_irqs(snet);
goto set_err;
}
}
snet->status = status;
return;
set_err:
snet->status |= VIRTIO_CONFIG_S_FAILED;
}
static void snet_get_config(struct vdpa_device *vdev, unsigned int offset,
void *buf, unsigned int len)
{
struct snet *snet = vdpa_to_snet(vdev);
void __iomem *cfg_ptr = snet->cfg->virtio_cfg + offset;
u8 *buf_ptr = buf;
u32 i;
if (offset + len > snet->cfg->cfg_size)
return;
for (i = 0; i < len; i++)
*buf_ptr++ = ioread8(cfg_ptr + i);
}
static void snet_set_config(struct vdpa_device *vdev, unsigned int offset,
const void *buf, unsigned int len)
{
struct snet *snet = vdpa_to_snet(vdev);
void __iomem *cfg_ptr = snet->cfg->virtio_cfg + offset;
const u8 *buf_ptr = buf;
u32 i;
if (offset + len > snet->cfg->cfg_size)
return;
for (i = 0; i < len; i++)
iowrite8(*buf_ptr++, cfg_ptr + i);
}
static int snet_suspend(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
int ret;
ret = snet_suspend_dev(snet);
if (ret)
SNET_ERR(snet->pdev, "SNET[%u] suspend failed, err: %d\n", snet->sid, ret);
else
SNET_DBG(snet->pdev, "Suspend SNET[%u] device\n", snet->sid);
return ret;
}
static int snet_resume(struct vdpa_device *vdev)
{
struct snet *snet = vdpa_to_snet(vdev);
int ret;
ret = snet_resume_dev(snet);
if (ret)
SNET_ERR(snet->pdev, "SNET[%u] resume failed, err: %d\n", snet->sid, ret);
else
SNET_DBG(snet->pdev, "Resume SNET[%u] device\n", snet->sid);
return ret;
}
static const struct vdpa_config_ops snet_config_ops = {
.set_vq_address = snet_set_vq_address,
.set_vq_num = snet_set_vq_num,
.kick_vq = snet_kick_vq,
.kick_vq_with_data = snet_kick_vq_with_data,
.set_vq_cb = snet_set_vq_cb,
.set_vq_ready = snet_set_vq_ready,
.get_vq_ready = snet_get_vq_ready,
.set_vq_state = snet_set_vq_state,
.get_vq_state = snet_get_vq_state,
.get_vq_irq = snet_get_vq_irq,
.get_vq_align = snet_get_vq_align,
.reset = snet_reset,
.get_config_size = snet_get_config_size,
.get_device_features = snet_get_features,
.set_driver_features = snet_set_drv_features,
.get_driver_features = snet_get_drv_features,
.get_vq_num_min = snet_get_vq_num_max,
.get_vq_num_max = snet_get_vq_num_max,
.set_config_cb = snet_set_config_cb,
.get_device_id = snet_get_device_id,
.get_vendor_id = snet_get_vendor_id,
.get_status = snet_get_status,
.set_status = snet_set_status,
.get_config = snet_get_config,
.set_config = snet_set_config,
.suspend = snet_suspend,
.resume = snet_resume,
};
static int psnet_open_pf_bar(struct pci_dev *pdev, struct psnet *psnet)
{
char name[50];
int ret, i, mask = 0;
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (pci_resource_len(pdev, i))
mask |= (1 << i);
}
if (!mask) {
SNET_ERR(pdev, "Failed to find a PCI BAR\n");
return -ENODEV;
}
snprintf(name, sizeof(name), "psnet[%s]-bars", pci_name(pdev));
ret = pcim_iomap_regions(pdev, mask, name);
if (ret) {
SNET_ERR(pdev, "Failed to request and map PCI BARs\n");
return ret;
}
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (mask & (1 << i))
psnet->bars[i] = pcim_iomap_table(pdev)[i];
}
return 0;
}
static int snet_open_vf_bar(struct pci_dev *pdev, struct snet *snet)
{
char name[50];
int ret;
snprintf(name, sizeof(name), "snet[%s]-bar", pci_name(pdev));
ret = pcim_iomap_regions(pdev, BIT(snet->psnet->cfg.vf_bar), name);
if (ret) {
SNET_ERR(pdev, "Failed to request and map PCI BAR for a VF\n");
return ret;
}
snet->bar = pcim_iomap_table(pdev)[snet->psnet->cfg.vf_bar];
return 0;
}
static void snet_free_cfg(struct snet_cfg *cfg)
{
u32 i;
if (!cfg->devs)
return;
for (i = 0; i < cfg->devices_num; i++) {
if (!cfg->devs[i])
break;
kfree(cfg->devs[i]);
}
kfree(cfg->devs);
}
static int psnet_detect_bar(struct psnet *psnet, u32 off)
{
unsigned long exit_time;
int i;
exit_time = jiffies + usecs_to_jiffies(SNET_DETECT_TIMEOUT);
while (time_before(jiffies, exit_time)) {
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (!psnet->bars[i])
continue;
if (ioread32(psnet->bars[i] + off) == SNET_SIGNATURE)
return i;
}
usleep_range(1000, 10000);
}
return -ENODEV;
}
static void psnet_unmap_unused_bars(struct pci_dev *pdev, struct psnet *psnet)
{
int i, mask = 0;
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
if (psnet->bars[i] && i != psnet->barno)
mask |= (1 << i);
}
if (mask)
pcim_iounmap_regions(pdev, mask);
}
static int psnet_read_cfg(struct pci_dev *pdev, struct psnet *psnet)
{
struct snet_cfg *cfg = &psnet->cfg;
u32 i, off;
int barno;
off = SNET_CONFIG_OFF;
barno = psnet_detect_bar(psnet, off);
if (barno < 0) {
SNET_ERR(pdev, "SNET config is not ready.\n");
return barno;
}
psnet->barno = barno;
SNET_DBG(pdev, "Using BAR number %d\n", barno);
psnet_unmap_unused_bars(pdev, psnet);
cfg->key = psnet_read32(psnet, off);
off += 4;
cfg->cfg_size = psnet_read32(psnet, off);
off += 4;
cfg->cfg_ver = psnet_read32(psnet, off);
off += 4;
psnet->negotiated_cfg_ver = min_t(u32, cfg->cfg_ver, SNET_CFG_VERSION);
SNET_DBG(pdev, "SNET config version %u\n", psnet->negotiated_cfg_ver);
cfg->vf_num = psnet_read32(psnet, off);
off += 4;
cfg->vf_bar = psnet_read32(psnet, off);
off += 4;
cfg->host_cfg_off = psnet_read32(psnet, off);
off += 4;
cfg->max_size_host_cfg = psnet_read32(psnet, off);
off += 4;
cfg->virtio_cfg_off = psnet_read32(psnet, off);
off += 4;
cfg->kick_off = psnet_read32(psnet, off);
off += 4;
cfg->hwmon_off = psnet_read32(psnet, off);
off += 4;
cfg->ctrl_off = psnet_read32(psnet, off);
off += 4;
cfg->flags = psnet_read32(psnet, off);
off += 4;
off += sizeof(cfg->rsvd);
cfg->devices_num = psnet_read32(psnet, off);
off += 4;
cfg->devs = kcalloc(cfg->devices_num, sizeof(void *), GFP_KERNEL);
if (!cfg->devs)
return -ENOMEM;
for (i = 0; i < cfg->devices_num; i++) {
cfg->devs[i] = kzalloc(sizeof(*cfg->devs[i]), GFP_KERNEL);
if (!cfg->devs[i]) {
snet_free_cfg(cfg);
return -ENOMEM;
}
cfg->devs[i]->virtio_id = psnet_read32(psnet, off);
off += 4;
cfg->devs[i]->vq_num = psnet_read32(psnet, off);
off += 4;
cfg->devs[i]->vq_size = psnet_read32(psnet, off);
off += 4;
cfg->devs[i]->vfid = psnet_read32(psnet, off);
off += 4;
cfg->devs[i]->features = psnet_read64(psnet, off);
off += 8;
off += sizeof(cfg->devs[i]->rsvd);
cfg->devs[i]->cfg_size = psnet_read32(psnet, off);
off += 4;
if (SNET_GENERAL_CFG_LEN + SNET_GENERAL_CFG_VQ_LEN * cfg->devs[i]->vq_num >
cfg->max_size_host_cfg) {
SNET_ERR(pdev, "Failed to read SNET config, the config is too big..\n");
snet_free_cfg(cfg);
return -EINVAL;
}
}
return 0;
}
static int psnet_alloc_irq_vector(struct pci_dev *pdev, struct psnet *psnet)
{
int ret = 0;
u32 i, irq_num = 0;
for (i = 0; i < psnet->cfg.devices_num; i++)
irq_num += psnet->cfg.devs[i]->vq_num + 1;
ret = pci_alloc_irq_vectors(pdev, irq_num, irq_num, PCI_IRQ_MSIX);
if (ret != irq_num) {
SNET_ERR(pdev, "Failed to allocate IRQ vectors\n");
return ret;
}
SNET_DBG(pdev, "Allocated %u IRQ vectors from physical function\n", irq_num);
return 0;
}
static int snet_alloc_irq_vector(struct pci_dev *pdev, struct snet_dev_cfg *snet_cfg)
{
int ret = 0;
u32 irq_num;
irq_num = snet_cfg->vq_num + 1;
ret = pci_alloc_irq_vectors(pdev, irq_num, irq_num, PCI_IRQ_MSIX);
if (ret <= 0) {
SNET_ERR(pdev, "Failed to allocate IRQ vectors\n");
return ret;
}
return 0;
}
static void snet_free_vqs(struct snet *snet)
{
u32 i;
if (!snet->vqs)
return;
for (i = 0 ; i < snet->cfg->vq_num ; i++) {
if (!snet->vqs[i])
break;
kfree(snet->vqs[i]);
}
kfree(snet->vqs);
}
static int snet_build_vqs(struct snet *snet)
{
u32 i;
snet->vqs = kcalloc(snet->cfg->vq_num, sizeof(void *), GFP_KERNEL);
if (!snet->vqs)
return -ENOMEM;
for (i = 0; i < snet->cfg->vq_num; i++) {
snet->vqs[i] = kzalloc(sizeof(*snet->vqs[i]), GFP_KERNEL);
if (!snet->vqs[i]) {
snet_free_vqs(snet);
return -ENOMEM;
}
snet->vqs[i]->irq = -1;
snet->vqs[i]->sid = i;
snet->vqs[i]->kick_ptr = snet->bar + snet->psnet->cfg.kick_off +
snet->vqs[i]->sid * 4;
iowrite32(0, snet->vqs[i]->kick_ptr);
}
return 0;
}
static int psnet_get_next_irq_num(struct psnet *psnet)
{
int irq;
spin_lock(&psnet->lock);
irq = psnet->next_irq++;
spin_unlock(&psnet->lock);
return irq;
}
static void snet_reserve_irq_idx(struct pci_dev *pdev, struct snet *snet)
{
struct psnet *psnet = snet->psnet;
int i;
snet->cfg_irq_idx = psnet_get_next_irq_num(psnet);
snprintf(snet->cfg_irq_name, SNET_NAME_SIZE, "snet[%s]-cfg[%d]",
pci_name(pdev), snet->cfg_irq_idx);
for (i = 0; i < snet->cfg->vq_num; i++) {
snet->vqs[i]->irq_idx = psnet_get_next_irq_num(psnet);
snprintf(snet->vqs[i]->irq_name, SNET_NAME_SIZE, "snet[%s]-vq[%d]",
pci_name(pdev), snet->vqs[i]->irq_idx);
}
}
static struct snet_dev_cfg *snet_find_dev_cfg(struct snet_cfg *cfg, u32 vfid)
{
u32 i;
for (i = 0; i < cfg->devices_num; i++) {
if (cfg->devs[i]->vfid == vfid)
return cfg->devs[i];
}
return NULL;
}
static int snet_vdpa_probe_pf(struct pci_dev *pdev)
{
struct psnet *psnet;
int ret = 0;
bool pf_irqs = false;
ret = pcim_enable_device(pdev);
if (ret) {
SNET_ERR(pdev, "Failed to enable PCI device\n");
return ret;
}
psnet = kzalloc(sizeof(*psnet), GFP_KERNEL);
if (!psnet)
return -ENOMEM;
spin_lock_init(&psnet->lock);
pci_set_master(pdev);
pci_set_drvdata(pdev, psnet);
ret = psnet_open_pf_bar(pdev, psnet);
if (ret)
goto free_psnet;
ret = psnet_read_cfg(pdev, psnet);
if (ret)
goto free_psnet;
pf_irqs = PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF);
if (pf_irqs) {
ret = psnet_alloc_irq_vector(pdev, psnet);
if (ret)
goto free_cfg;
}
SNET_DBG(pdev, "Enable %u virtual functions\n", psnet->cfg.vf_num);
ret = pci_enable_sriov(pdev, psnet->cfg.vf_num);
if (ret) {
SNET_ERR(pdev, "Failed to enable SR-IOV\n");
goto free_irq;
}
if (PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_HWMON)) {
#if IS_ENABLED(CONFIG_HWMON)
psnet_create_hwmon(pdev);
#else
SNET_WARN(pdev, "Can't start HWMON, CONFIG_HWMON is not enabled\n");
#endif
}
return 0;
free_irq:
if (pf_irqs)
pci_free_irq_vectors(pdev);
free_cfg:
snet_free_cfg(&psnet->cfg);
free_psnet:
kfree(psnet);
return ret;
}
static int snet_vdpa_probe_vf(struct pci_dev *pdev)
{
struct pci_dev *pdev_pf = pdev->physfn;
struct psnet *psnet = pci_get_drvdata(pdev_pf);
struct snet_dev_cfg *dev_cfg;
struct snet *snet;
u32 vfid;
int ret;
bool pf_irqs = false;
ret = pci_iov_vf_id(pdev);
if (ret < 0) {
SNET_ERR(pdev, "Failed to find a VF id\n");
return ret;
}
vfid = ret + 1;
dev_cfg = snet_find_dev_cfg(&psnet->cfg, vfid);
if (!dev_cfg) {
SNET_WARN(pdev, "Failed to find a VF config..\n");
return -ENODEV;
}
pf_irqs = PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF);
ret = pcim_enable_device(pdev);
if (ret) {
SNET_ERR(pdev, "Failed to enable PCI VF device\n");
return ret;
}
if (!pf_irqs) {
ret = snet_alloc_irq_vector(pdev, dev_cfg);
if (ret)
return ret;
}
snet = vdpa_alloc_device(struct snet, vdpa, &pdev->dev, &snet_config_ops, 1, 1, NULL,
false);
if (!snet) {
SNET_ERR(pdev, "Failed to allocate a vdpa device\n");
ret = -ENOMEM;
goto free_irqs;
}
mutex_init(&snet->ctrl_lock);
spin_lock_init(&snet->ctrl_spinlock);
snet->pdev = pdev;
snet->psnet = psnet;
snet->cfg = dev_cfg;
snet->dpu_ready = false;
snet->sid = vfid;
snet->cfg_irq = -1;
ret = snet_open_vf_bar(pdev, snet);
if (ret)
goto put_device;
snet->cfg->virtio_cfg = snet->bar + snet->psnet->cfg.virtio_cfg_off;
snet_ctrl_clear(snet);
pci_set_master(pdev);
pci_set_drvdata(pdev, snet);
ret = snet_build_vqs(snet);
if (ret)
goto put_device;
snet_reserve_irq_idx(pf_irqs ? pdev_pf : pdev, snet);
snet->vdpa.dma_dev = &pdev->dev;
ret = vdpa_register_device(&snet->vdpa, snet->cfg->vq_num);
if (ret) {
SNET_ERR(pdev, "Failed to register vdpa device\n");
goto free_vqs;
}
return 0;
free_vqs:
snet_free_vqs(snet);
put_device:
put_device(&snet->vdpa.dev);
free_irqs:
if (!pf_irqs)
pci_free_irq_vectors(pdev);
return ret;
}
static int snet_vdpa_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
if (pdev->is_virtfn)
return snet_vdpa_probe_vf(pdev);
else
return snet_vdpa_probe_pf(pdev);
}
static void snet_vdpa_remove_pf(struct pci_dev *pdev)
{
struct psnet *psnet = pci_get_drvdata(pdev);
pci_disable_sriov(pdev);
if (PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF))
pci_free_irq_vectors(pdev);
snet_free_cfg(&psnet->cfg);
kfree(psnet);
}
static void snet_vdpa_remove_vf(struct pci_dev *pdev)
{
struct snet *snet = pci_get_drvdata(pdev);
struct psnet *psnet = snet->psnet;
vdpa_unregister_device(&snet->vdpa);
snet_free_vqs(snet);
if (!PSNET_FLAG_ON(psnet, SNET_CFG_FLAG_IRQ_PF))
pci_free_irq_vectors(pdev);
}
static void snet_vdpa_remove(struct pci_dev *pdev)
{
if (pdev->is_virtfn)
snet_vdpa_remove_vf(pdev);
else
snet_vdpa_remove_pf(pdev);
}
static struct pci_device_id snet_driver_pci_ids[] = {
{ PCI_DEVICE_SUB(PCI_VENDOR_ID_SOLIDRUN, SNET_DEVICE_ID,
PCI_VENDOR_ID_SOLIDRUN, SNET_DEVICE_ID) },
{ 0 },
};
MODULE_DEVICE_TABLE(pci, snet_driver_pci_ids);
static struct pci_driver snet_vdpa_driver = {
.name = "snet-vdpa-driver",
.id_table = snet_driver_pci_ids,
.probe = snet_vdpa_probe,
.remove = snet_vdpa_remove,
};
module_pci_driver(snet_vdpa_driver);
MODULE_AUTHOR("Alvaro Karsz <alvaro.karsz@solid-run.com>");
MODULE_DESCRIPTION("SolidRun vDPA driver");
MODULE_LICENSE("GPL v2"