vhost-user-scsi: Fix memleaks in vus_proc_req()
[qemu.git] / hw / ppc / spapr_pci_vfio.c
blobecb34aaade173ff5629127b3493953f332dd7fd3
1 /*
2 * QEMU sPAPR PCI host for VFIO
4 * Copyright (c) 2011-2014 Alexey Kardashevskiy, IBM Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License,
9 * or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include <linux/vfio.h>
22 #include "cpu.h"
23 #include "hw/ppc/spapr.h"
24 #include "hw/pci-host/spapr.h"
25 #include "hw/pci/msix.h"
26 #include "hw/vfio/vfio.h"
27 #include "qemu/error-report.h"
28 #include "sysemu/qtest.h"
30 bool spapr_phb_eeh_available(SpaprPhbState *sphb)
32 return vfio_eeh_as_ok(&sphb->iommu_as);
35 static void spapr_phb_vfio_eeh_reenable(SpaprPhbState *sphb)
37 vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_ENABLE);
40 void spapr_phb_vfio_reset(DeviceState *qdev)
43 * The PE might be in frozen state. To reenable the EEH
44 * functionality on it will clean the frozen state, which
45 * ensures that the contained PCI devices will work properly
46 * after reboot.
48 spapr_phb_vfio_eeh_reenable(SPAPR_PCI_HOST_BRIDGE(qdev));
51 int spapr_phb_vfio_eeh_set_option(SpaprPhbState *sphb,
52 unsigned int addr, int option)
54 uint32_t op;
55 int ret;
57 switch (option) {
58 case RTAS_EEH_DISABLE:
59 op = VFIO_EEH_PE_DISABLE;
60 break;
61 case RTAS_EEH_ENABLE: {
62 PCIHostState *phb;
63 PCIDevice *pdev;
66 * The EEH functionality is enabled on basis of PCI device,
67 * instead of PE. We need check the validity of the PCI
68 * device address.
70 phb = PCI_HOST_BRIDGE(sphb);
71 pdev = pci_find_device(phb->bus,
72 (addr >> 16) & 0xFF, (addr >> 8) & 0xFF);
73 if (!pdev || !object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
74 return RTAS_OUT_PARAM_ERROR;
77 op = VFIO_EEH_PE_ENABLE;
78 break;
80 case RTAS_EEH_THAW_IO:
81 op = VFIO_EEH_PE_UNFREEZE_IO;
82 break;
83 case RTAS_EEH_THAW_DMA:
84 op = VFIO_EEH_PE_UNFREEZE_DMA;
85 break;
86 default:
87 return RTAS_OUT_PARAM_ERROR;
90 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
91 if (ret < 0) {
92 return RTAS_OUT_HW_ERROR;
95 return RTAS_OUT_SUCCESS;
98 int spapr_phb_vfio_eeh_get_state(SpaprPhbState *sphb, int *state)
100 int ret;
102 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_GET_STATE);
103 if (ret < 0) {
104 return RTAS_OUT_PARAM_ERROR;
107 *state = ret;
108 return RTAS_OUT_SUCCESS;
111 static void spapr_phb_vfio_eeh_clear_dev_msix(PCIBus *bus,
112 PCIDevice *pdev,
113 void *opaque)
115 /* Check if the device is VFIO PCI device */
116 if (!object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
117 return;
121 * The MSIx table will be cleaned out by reset. We need
122 * disable it so that it can be reenabled properly. Also,
123 * the cached MSIx table should be cleared as it's not
124 * reflecting the contents in hardware.
126 if (msix_enabled(pdev)) {
127 uint16_t flags;
129 flags = pci_host_config_read_common(pdev,
130 pdev->msix_cap + PCI_MSIX_FLAGS,
131 pci_config_size(pdev), 2);
132 flags &= ~PCI_MSIX_FLAGS_ENABLE;
133 pci_host_config_write_common(pdev,
134 pdev->msix_cap + PCI_MSIX_FLAGS,
135 pci_config_size(pdev), flags, 2);
138 msix_reset(pdev);
141 static void spapr_phb_vfio_eeh_clear_bus_msix(PCIBus *bus, void *opaque)
143 pci_for_each_device(bus, pci_bus_num(bus),
144 spapr_phb_vfio_eeh_clear_dev_msix, NULL);
147 static void spapr_phb_vfio_eeh_pre_reset(SpaprPhbState *sphb)
149 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
151 pci_for_each_bus(phb->bus, spapr_phb_vfio_eeh_clear_bus_msix, NULL);
154 int spapr_phb_vfio_eeh_reset(SpaprPhbState *sphb, int option)
156 uint32_t op;
157 int ret;
159 switch (option) {
160 case RTAS_SLOT_RESET_DEACTIVATE:
161 op = VFIO_EEH_PE_RESET_DEACTIVATE;
162 break;
163 case RTAS_SLOT_RESET_HOT:
164 spapr_phb_vfio_eeh_pre_reset(sphb);
165 op = VFIO_EEH_PE_RESET_HOT;
166 break;
167 case RTAS_SLOT_RESET_FUNDAMENTAL:
168 spapr_phb_vfio_eeh_pre_reset(sphb);
169 op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
170 break;
171 default:
172 return RTAS_OUT_PARAM_ERROR;
175 ret = vfio_eeh_as_op(&sphb->iommu_as, op);
176 if (ret < 0) {
177 return RTAS_OUT_HW_ERROR;
180 return RTAS_OUT_SUCCESS;
183 int spapr_phb_vfio_eeh_configure(SpaprPhbState *sphb)
185 int ret;
187 ret = vfio_eeh_as_op(&sphb->iommu_as, VFIO_EEH_PE_CONFIGURE);
188 if (ret < 0) {
189 return RTAS_OUT_PARAM_ERROR;
192 return RTAS_OUT_SUCCESS;