Skip to content

Commit 38fe397

Browse files
brettcreeleyawilliam
authored andcommitted
vfio/pds: Initial support for pds VFIO driver
This is the initial framework for the new pds-vfio-pci device driver. This does the very basics of registering the PDS PCI device and configuring it as a VFIO PCI device. With this change, the VF device can be bound to the pds-vfio-pci driver on the host and presented to the VM as an ethernet VF. Signed-off-by: Brett Creeley <[email protected]> Signed-off-by: Shannon Nelson <[email protected]> Reviewed-by: Simon Horman <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Shameer Kolothum <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 9a4087f commit 38fe397

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

drivers/vfio/pci/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
1111
obj-$(CONFIG_MLX5_VFIO_PCI) += mlx5/
1212

1313
obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/
14+
15+
obj-$(CONFIG_PDS_VFIO_PCI) += pds/

drivers/vfio/pci/pds/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# Copyright (c) 2023 Advanced Micro Devices, Inc.
3+
4+
obj-$(CONFIG_PDS_VFIO_PCI) += pds-vfio-pci.o
5+
6+
pds-vfio-pci-y := \
7+
pci_drv.o \
8+
vfio_dev.o

drivers/vfio/pci/pds/pci_drv.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright(c) 2023 Advanced Micro Devices, Inc. */
3+
4+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5+
6+
#include <linux/module.h>
7+
#include <linux/pci.h>
8+
#include <linux/types.h>
9+
#include <linux/vfio.h>
10+
11+
#include <linux/pds/pds_core_if.h>
12+
13+
#include "vfio_dev.h"
14+
15+
#define PDS_VFIO_DRV_DESCRIPTION "AMD/Pensando VFIO Device Driver"
16+
#define PCI_VENDOR_ID_PENSANDO 0x1dd8
17+
18+
static int pds_vfio_pci_probe(struct pci_dev *pdev,
19+
const struct pci_device_id *id)
20+
{
21+
struct pds_vfio_pci_device *pds_vfio;
22+
int err;
23+
24+
pds_vfio = vfio_alloc_device(pds_vfio_pci_device, vfio_coredev.vdev,
25+
&pdev->dev, pds_vfio_ops_info());
26+
if (IS_ERR(pds_vfio))
27+
return PTR_ERR(pds_vfio);
28+
29+
dev_set_drvdata(&pdev->dev, &pds_vfio->vfio_coredev);
30+
31+
err = vfio_pci_core_register_device(&pds_vfio->vfio_coredev);
32+
if (err)
33+
goto out_put_vdev;
34+
35+
return 0;
36+
37+
out_put_vdev:
38+
vfio_put_device(&pds_vfio->vfio_coredev.vdev);
39+
return err;
40+
}
41+
42+
static void pds_vfio_pci_remove(struct pci_dev *pdev)
43+
{
44+
struct pds_vfio_pci_device *pds_vfio = pds_vfio_pci_drvdata(pdev);
45+
46+
vfio_pci_core_unregister_device(&pds_vfio->vfio_coredev);
47+
vfio_put_device(&pds_vfio->vfio_coredev.vdev);
48+
}
49+
50+
static const struct pci_device_id pds_vfio_pci_table[] = {
51+
{ PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_PENSANDO, 0x1003) }, /* Ethernet VF */
52+
{ 0, }
53+
};
54+
MODULE_DEVICE_TABLE(pci, pds_vfio_pci_table);
55+
56+
static struct pci_driver pds_vfio_pci_driver = {
57+
.name = KBUILD_MODNAME,
58+
.id_table = pds_vfio_pci_table,
59+
.probe = pds_vfio_pci_probe,
60+
.remove = pds_vfio_pci_remove,
61+
.driver_managed_dma = true,
62+
};
63+
64+
module_pci_driver(pds_vfio_pci_driver);
65+
66+
MODULE_DESCRIPTION(PDS_VFIO_DRV_DESCRIPTION);
67+
MODULE_AUTHOR("Brett Creeley <[email protected]>");
68+
MODULE_LICENSE("GPL");

drivers/vfio/pci/pds/vfio_dev.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright(c) 2023 Advanced Micro Devices, Inc. */
3+
4+
#include <linux/vfio.h>
5+
#include <linux/vfio_pci_core.h>
6+
7+
#include "vfio_dev.h"
8+
9+
struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev)
10+
{
11+
struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
12+
13+
return container_of(core_device, struct pds_vfio_pci_device,
14+
vfio_coredev);
15+
}
16+
17+
static int pds_vfio_init_device(struct vfio_device *vdev)
18+
{
19+
struct pds_vfio_pci_device *pds_vfio =
20+
container_of(vdev, struct pds_vfio_pci_device,
21+
vfio_coredev.vdev);
22+
struct pci_dev *pdev = to_pci_dev(vdev->dev);
23+
int err, vf_id;
24+
25+
vf_id = pci_iov_vf_id(pdev);
26+
if (vf_id < 0)
27+
return vf_id;
28+
29+
err = vfio_pci_core_init_dev(vdev);
30+
if (err)
31+
return err;
32+
33+
pds_vfio->vf_id = vf_id;
34+
35+
return 0;
36+
}
37+
38+
static int pds_vfio_open_device(struct vfio_device *vdev)
39+
{
40+
struct pds_vfio_pci_device *pds_vfio =
41+
container_of(vdev, struct pds_vfio_pci_device,
42+
vfio_coredev.vdev);
43+
int err;
44+
45+
err = vfio_pci_core_enable(&pds_vfio->vfio_coredev);
46+
if (err)
47+
return err;
48+
49+
vfio_pci_core_finish_enable(&pds_vfio->vfio_coredev);
50+
51+
return 0;
52+
}
53+
54+
static const struct vfio_device_ops pds_vfio_ops = {
55+
.name = "pds-vfio",
56+
.init = pds_vfio_init_device,
57+
.release = vfio_pci_core_release_dev,
58+
.open_device = pds_vfio_open_device,
59+
.close_device = vfio_pci_core_close_device,
60+
.ioctl = vfio_pci_core_ioctl,
61+
.device_feature = vfio_pci_core_ioctl_feature,
62+
.read = vfio_pci_core_read,
63+
.write = vfio_pci_core_write,
64+
.mmap = vfio_pci_core_mmap,
65+
.request = vfio_pci_core_request,
66+
.match = vfio_pci_core_match,
67+
.bind_iommufd = vfio_iommufd_physical_bind,
68+
.unbind_iommufd = vfio_iommufd_physical_unbind,
69+
.attach_ioas = vfio_iommufd_physical_attach_ioas,
70+
};
71+
72+
const struct vfio_device_ops *pds_vfio_ops_info(void)
73+
{
74+
return &pds_vfio_ops;
75+
}

drivers/vfio/pci/pds/vfio_dev.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright(c) 2023 Advanced Micro Devices, Inc. */
3+
4+
#ifndef _VFIO_DEV_H_
5+
#define _VFIO_DEV_H_
6+
7+
#include <linux/pci.h>
8+
#include <linux/vfio_pci_core.h>
9+
10+
struct pds_vfio_pci_device {
11+
struct vfio_pci_core_device vfio_coredev;
12+
13+
int vf_id;
14+
};
15+
16+
const struct vfio_device_ops *pds_vfio_ops_info(void);
17+
struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev);
18+
19+
#endif /* _VFIO_DEV_H_ */

0 commit comments

Comments
 (0)