138fe3975SBrett Creeley // SPDX-License-Identifier: GPL-2.0 238fe3975SBrett Creeley /* Copyright(c) 2023 Advanced Micro Devices, Inc. */ 338fe3975SBrett Creeley 438fe3975SBrett Creeley #include <linux/vfio.h> 538fe3975SBrett Creeley #include <linux/vfio_pci_core.h> 638fe3975SBrett Creeley 7bb500dbeSBrett Creeley #include "lm.h" 8f232836aSBrett Creeley #include "dirty.h" 938fe3975SBrett Creeley #include "vfio_dev.h" 1038fe3975SBrett Creeley 1163f77a71SBrett Creeley struct pci_dev *pds_vfio_to_pci_dev(struct pds_vfio_pci_device *pds_vfio) 1263f77a71SBrett Creeley { 1363f77a71SBrett Creeley return pds_vfio->vfio_coredev.pdev; 1463f77a71SBrett Creeley } 1563f77a71SBrett Creeley 16bb500dbeSBrett Creeley struct device *pds_vfio_to_dev(struct pds_vfio_pci_device *pds_vfio) 17bb500dbeSBrett Creeley { 18bb500dbeSBrett Creeley return &pds_vfio_to_pci_dev(pds_vfio)->dev; 19bb500dbeSBrett Creeley } 20bb500dbeSBrett Creeley 2138fe3975SBrett Creeley struct pds_vfio_pci_device *pds_vfio_pci_drvdata(struct pci_dev *pdev) 2238fe3975SBrett Creeley { 2338fe3975SBrett Creeley struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev); 2438fe3975SBrett Creeley 2538fe3975SBrett Creeley return container_of(core_device, struct pds_vfio_pci_device, 2638fe3975SBrett Creeley vfio_coredev); 2738fe3975SBrett Creeley } 2838fe3975SBrett Creeley 29*6a7e448cSBrett Creeley void pds_vfio_reset(struct pds_vfio_pci_device *pds_vfio, 30*6a7e448cSBrett Creeley enum vfio_device_mig_state state) 31bb500dbeSBrett Creeley { 32bb500dbeSBrett Creeley pds_vfio_put_restore_file(pds_vfio); 33bb500dbeSBrett Creeley pds_vfio_put_save_file(pds_vfio); 34*6a7e448cSBrett Creeley if (state == VFIO_DEVICE_STATE_ERROR) 35f232836aSBrett Creeley pds_vfio_dirty_disable(pds_vfio, false); 36*6a7e448cSBrett Creeley pds_vfio->state = state; 37bb500dbeSBrett Creeley } 38bb500dbeSBrett Creeley 39bb500dbeSBrett Creeley static struct file * 40bb500dbeSBrett Creeley pds_vfio_set_device_state(struct vfio_device *vdev, 41bb500dbeSBrett Creeley enum vfio_device_mig_state new_state) 42bb500dbeSBrett Creeley { 43bb500dbeSBrett Creeley struct pds_vfio_pci_device *pds_vfio = 44bb500dbeSBrett Creeley container_of(vdev, struct pds_vfio_pci_device, 45bb500dbeSBrett Creeley vfio_coredev.vdev); 46bb500dbeSBrett Creeley struct file *res = NULL; 47bb500dbeSBrett Creeley 48bb500dbeSBrett Creeley mutex_lock(&pds_vfio->state_mutex); 497dabb1bcSBrett Creeley /* 507dabb1bcSBrett Creeley * only way to transition out of VFIO_DEVICE_STATE_ERROR is via 517dabb1bcSBrett Creeley * VFIO_DEVICE_RESET, so prevent the state machine from running since 527dabb1bcSBrett Creeley * vfio_mig_get_next_state() will throw a WARN_ON() when transitioning 537dabb1bcSBrett Creeley * from VFIO_DEVICE_STATE_ERROR to any other state 547dabb1bcSBrett Creeley */ 557dabb1bcSBrett Creeley while (pds_vfio->state != VFIO_DEVICE_STATE_ERROR && 567dabb1bcSBrett Creeley new_state != pds_vfio->state) { 57bb500dbeSBrett Creeley enum vfio_device_mig_state next_state; 58bb500dbeSBrett Creeley 59bb500dbeSBrett Creeley int err = vfio_mig_get_next_state(vdev, pds_vfio->state, 60bb500dbeSBrett Creeley new_state, &next_state); 61bb500dbeSBrett Creeley if (err) { 62bb500dbeSBrett Creeley res = ERR_PTR(err); 63bb500dbeSBrett Creeley break; 64bb500dbeSBrett Creeley } 65bb500dbeSBrett Creeley 66bb500dbeSBrett Creeley res = pds_vfio_step_device_state_locked(pds_vfio, next_state); 67bb500dbeSBrett Creeley if (IS_ERR(res)) 68bb500dbeSBrett Creeley break; 69bb500dbeSBrett Creeley 70bb500dbeSBrett Creeley pds_vfio->state = next_state; 71bb500dbeSBrett Creeley 72bb500dbeSBrett Creeley if (WARN_ON(res && new_state != pds_vfio->state)) { 73bb500dbeSBrett Creeley res = ERR_PTR(-EINVAL); 74bb500dbeSBrett Creeley break; 75bb500dbeSBrett Creeley } 76bb500dbeSBrett Creeley } 77*6a7e448cSBrett Creeley mutex_unlock(&pds_vfio->state_mutex); 787dabb1bcSBrett Creeley if (pds_vfio->state == VFIO_DEVICE_STATE_ERROR) 797dabb1bcSBrett Creeley res = ERR_PTR(-EIO); 80bb500dbeSBrett Creeley 81bb500dbeSBrett Creeley return res; 82bb500dbeSBrett Creeley } 83bb500dbeSBrett Creeley 84bb500dbeSBrett Creeley static int pds_vfio_get_device_state(struct vfio_device *vdev, 85bb500dbeSBrett Creeley enum vfio_device_mig_state *current_state) 86bb500dbeSBrett Creeley { 87bb500dbeSBrett Creeley struct pds_vfio_pci_device *pds_vfio = 88bb500dbeSBrett Creeley container_of(vdev, struct pds_vfio_pci_device, 89bb500dbeSBrett Creeley vfio_coredev.vdev); 90bb500dbeSBrett Creeley 91bb500dbeSBrett Creeley mutex_lock(&pds_vfio->state_mutex); 92bb500dbeSBrett Creeley *current_state = pds_vfio->state; 93*6a7e448cSBrett Creeley mutex_unlock(&pds_vfio->state_mutex); 94bb500dbeSBrett Creeley return 0; 95bb500dbeSBrett Creeley } 96bb500dbeSBrett Creeley 97bb500dbeSBrett Creeley static int pds_vfio_get_device_state_size(struct vfio_device *vdev, 98bb500dbeSBrett Creeley unsigned long *stop_copy_length) 99bb500dbeSBrett Creeley { 100bb500dbeSBrett Creeley *stop_copy_length = PDS_LM_DEVICE_STATE_LENGTH; 101bb500dbeSBrett Creeley return 0; 102bb500dbeSBrett Creeley } 103bb500dbeSBrett Creeley 104bb500dbeSBrett Creeley static const struct vfio_migration_ops pds_vfio_lm_ops = { 105bb500dbeSBrett Creeley .migration_set_state = pds_vfio_set_device_state, 106bb500dbeSBrett Creeley .migration_get_state = pds_vfio_get_device_state, 107bb500dbeSBrett Creeley .migration_get_data_size = pds_vfio_get_device_state_size 108bb500dbeSBrett Creeley }; 109bb500dbeSBrett Creeley 110f232836aSBrett Creeley static const struct vfio_log_ops pds_vfio_log_ops = { 111f232836aSBrett Creeley .log_start = pds_vfio_dma_logging_start, 112f232836aSBrett Creeley .log_stop = pds_vfio_dma_logging_stop, 113f232836aSBrett Creeley .log_read_and_clear = pds_vfio_dma_logging_report, 114f232836aSBrett Creeley }; 115f232836aSBrett Creeley 11638fe3975SBrett Creeley static int pds_vfio_init_device(struct vfio_device *vdev) 11738fe3975SBrett Creeley { 11838fe3975SBrett Creeley struct pds_vfio_pci_device *pds_vfio = 11938fe3975SBrett Creeley container_of(vdev, struct pds_vfio_pci_device, 12038fe3975SBrett Creeley vfio_coredev.vdev); 12138fe3975SBrett Creeley struct pci_dev *pdev = to_pci_dev(vdev->dev); 12263f77a71SBrett Creeley int err, vf_id, pci_id; 12338fe3975SBrett Creeley 12438fe3975SBrett Creeley vf_id = pci_iov_vf_id(pdev); 12538fe3975SBrett Creeley if (vf_id < 0) 12638fe3975SBrett Creeley return vf_id; 12738fe3975SBrett Creeley 12838fe3975SBrett Creeley err = vfio_pci_core_init_dev(vdev); 12938fe3975SBrett Creeley if (err) 13038fe3975SBrett Creeley return err; 13138fe3975SBrett Creeley 13238fe3975SBrett Creeley pds_vfio->vf_id = vf_id; 13338fe3975SBrett Creeley 13491aeb563SBrett Creeley mutex_init(&pds_vfio->state_mutex); 13591aeb563SBrett Creeley 136bb500dbeSBrett Creeley vdev->migration_flags = VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P; 137bb500dbeSBrett Creeley vdev->mig_ops = &pds_vfio_lm_ops; 138f232836aSBrett Creeley vdev->log_ops = &pds_vfio_log_ops; 139bb500dbeSBrett Creeley 14063f77a71SBrett Creeley pci_id = PCI_DEVID(pdev->bus->number, pdev->devfn); 14163f77a71SBrett Creeley dev_dbg(&pdev->dev, 14263f77a71SBrett Creeley "%s: PF %#04x VF %#04x vf_id %d domain %d pds_vfio %p\n", 14327004f89SShixiong Ou __func__, pci_dev_id(pci_physfn(pdev)), pci_id, vf_id, 14463f77a71SBrett Creeley pci_domain_nr(pdev->bus), pds_vfio); 14563f77a71SBrett Creeley 14638fe3975SBrett Creeley return 0; 14738fe3975SBrett Creeley } 14838fe3975SBrett Creeley 14991aeb563SBrett Creeley static void pds_vfio_release_device(struct vfio_device *vdev) 15091aeb563SBrett Creeley { 15191aeb563SBrett Creeley struct pds_vfio_pci_device *pds_vfio = 15291aeb563SBrett Creeley container_of(vdev, struct pds_vfio_pci_device, 15391aeb563SBrett Creeley vfio_coredev.vdev); 15491aeb563SBrett Creeley 15591aeb563SBrett Creeley mutex_destroy(&pds_vfio->state_mutex); 15691aeb563SBrett Creeley vfio_pci_core_release_dev(vdev); 15791aeb563SBrett Creeley } 15891aeb563SBrett Creeley 15938fe3975SBrett Creeley static int pds_vfio_open_device(struct vfio_device *vdev) 16038fe3975SBrett Creeley { 16138fe3975SBrett Creeley struct pds_vfio_pci_device *pds_vfio = 16238fe3975SBrett Creeley container_of(vdev, struct pds_vfio_pci_device, 16338fe3975SBrett Creeley vfio_coredev.vdev); 16438fe3975SBrett Creeley int err; 16538fe3975SBrett Creeley 16638fe3975SBrett Creeley err = vfio_pci_core_enable(&pds_vfio->vfio_coredev); 16738fe3975SBrett Creeley if (err) 16838fe3975SBrett Creeley return err; 16938fe3975SBrett Creeley 170bb500dbeSBrett Creeley pds_vfio->state = VFIO_DEVICE_STATE_RUNNING; 171bb500dbeSBrett Creeley 17238fe3975SBrett Creeley vfio_pci_core_finish_enable(&pds_vfio->vfio_coredev); 17338fe3975SBrett Creeley 17438fe3975SBrett Creeley return 0; 17538fe3975SBrett Creeley } 17638fe3975SBrett Creeley 177bb500dbeSBrett Creeley static void pds_vfio_close_device(struct vfio_device *vdev) 178bb500dbeSBrett Creeley { 179bb500dbeSBrett Creeley struct pds_vfio_pci_device *pds_vfio = 180bb500dbeSBrett Creeley container_of(vdev, struct pds_vfio_pci_device, 181bb500dbeSBrett Creeley vfio_coredev.vdev); 182bb500dbeSBrett Creeley 183bb500dbeSBrett Creeley mutex_lock(&pds_vfio->state_mutex); 184bb500dbeSBrett Creeley pds_vfio_put_restore_file(pds_vfio); 185bb500dbeSBrett Creeley pds_vfio_put_save_file(pds_vfio); 186f232836aSBrett Creeley pds_vfio_dirty_disable(pds_vfio, true); 187bb500dbeSBrett Creeley mutex_unlock(&pds_vfio->state_mutex); 188bb500dbeSBrett Creeley vfio_pci_core_close_device(vdev); 189bb500dbeSBrett Creeley } 190bb500dbeSBrett Creeley 19138fe3975SBrett Creeley static const struct vfio_device_ops pds_vfio_ops = { 19238fe3975SBrett Creeley .name = "pds-vfio", 19338fe3975SBrett Creeley .init = pds_vfio_init_device, 19491aeb563SBrett Creeley .release = pds_vfio_release_device, 19538fe3975SBrett Creeley .open_device = pds_vfio_open_device, 196bb500dbeSBrett Creeley .close_device = pds_vfio_close_device, 19738fe3975SBrett Creeley .ioctl = vfio_pci_core_ioctl, 19838fe3975SBrett Creeley .device_feature = vfio_pci_core_ioctl_feature, 19938fe3975SBrett Creeley .read = vfio_pci_core_read, 20038fe3975SBrett Creeley .write = vfio_pci_core_write, 20138fe3975SBrett Creeley .mmap = vfio_pci_core_mmap, 20238fe3975SBrett Creeley .request = vfio_pci_core_request, 20338fe3975SBrett Creeley .match = vfio_pci_core_match, 20438fe3975SBrett Creeley .bind_iommufd = vfio_iommufd_physical_bind, 20538fe3975SBrett Creeley .unbind_iommufd = vfio_iommufd_physical_unbind, 20638fe3975SBrett Creeley .attach_ioas = vfio_iommufd_physical_attach_ioas, 20738fe3975SBrett Creeley }; 20838fe3975SBrett Creeley 20938fe3975SBrett Creeley const struct vfio_device_ops *pds_vfio_ops_info(void) 21038fe3975SBrett Creeley { 21138fe3975SBrett Creeley return &pds_vfio_ops; 21238fe3975SBrett Creeley } 213