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