Statistics
| Branch: | Revision:

root / hw / pcie_aer.c @ 47188700

History | View | Annotate | Download (31.3 kB)

1 34e65944 Isaku Yamahata
/*
2 34e65944 Isaku Yamahata
 * pcie_aer.c
3 34e65944 Isaku Yamahata
 *
4 34e65944 Isaku Yamahata
 * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
5 34e65944 Isaku Yamahata
 *                    VA Linux Systems Japan K.K.
6 34e65944 Isaku Yamahata
 *
7 34e65944 Isaku Yamahata
 * This program is free software; you can redistribute it and/or modify
8 34e65944 Isaku Yamahata
 * it under the terms of the GNU General Public License as published by
9 34e65944 Isaku Yamahata
 * the Free Software Foundation; either version 2 of the License, or
10 34e65944 Isaku Yamahata
 * (at your option) any later version.
11 34e65944 Isaku Yamahata
 *
12 34e65944 Isaku Yamahata
 * This program is distributed in the hope that it will be useful,
13 34e65944 Isaku Yamahata
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 34e65944 Isaku Yamahata
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 34e65944 Isaku Yamahata
 * GNU General Public License for more details.
16 34e65944 Isaku Yamahata
 *
17 34e65944 Isaku Yamahata
 * You should have received a copy of the GNU General Public License along
18 34e65944 Isaku Yamahata
 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 34e65944 Isaku Yamahata
 */
20 34e65944 Isaku Yamahata
21 34e65944 Isaku Yamahata
#include "sysemu.h"
22 2ae63bda Isaku Yamahata
#include "qemu-objects.h"
23 2ae63bda Isaku Yamahata
#include "monitor.h"
24 34e65944 Isaku Yamahata
#include "pci_bridge.h"
25 34e65944 Isaku Yamahata
#include "pcie.h"
26 34e65944 Isaku Yamahata
#include "msix.h"
27 34e65944 Isaku Yamahata
#include "msi.h"
28 34e65944 Isaku Yamahata
#include "pci_internals.h"
29 34e65944 Isaku Yamahata
#include "pcie_regs.h"
30 34e65944 Isaku Yamahata
31 34e65944 Isaku Yamahata
//#define DEBUG_PCIE
32 34e65944 Isaku Yamahata
#ifdef DEBUG_PCIE
33 34e65944 Isaku Yamahata
# define PCIE_DPRINTF(fmt, ...)                                         \
34 34e65944 Isaku Yamahata
    fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
35 34e65944 Isaku Yamahata
#else
36 34e65944 Isaku Yamahata
# define PCIE_DPRINTF(fmt, ...) do {} while (0)
37 34e65944 Isaku Yamahata
#endif
38 34e65944 Isaku Yamahata
#define PCIE_DEV_PRINTF(dev, fmt, ...)                                  \
39 34e65944 Isaku Yamahata
    PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
40 34e65944 Isaku Yamahata
41 34e65944 Isaku Yamahata
/* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */
42 34e65944 Isaku Yamahata
static uint32_t pcie_aer_uncor_default_severity(uint32_t status)
43 34e65944 Isaku Yamahata
{
44 34e65944 Isaku Yamahata
    switch (status) {
45 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_INTN:
46 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_DLP:
47 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_SDN:
48 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_RX_OVER:
49 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_FCP:
50 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_MALF_TLP:
51 34e65944 Isaku Yamahata
        return PCI_ERR_ROOT_CMD_FATAL_EN;
52 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_POISON_TLP:
53 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_ECRC:
54 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_UNSUP:
55 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_COMP_TIME:
56 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_COMP_ABORT:
57 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_UNX_COMP:
58 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_ACSV:
59 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_MCBTLP:
60 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_ATOP_EBLOCKED:
61 34e65944 Isaku Yamahata
    case PCI_ERR_UNC_TLP_PRF_BLOCKED:
62 34e65944 Isaku Yamahata
        return PCI_ERR_ROOT_CMD_NONFATAL_EN;
63 34e65944 Isaku Yamahata
    default:
64 34e65944 Isaku Yamahata
        abort();
65 34e65944 Isaku Yamahata
        break;
66 34e65944 Isaku Yamahata
    }
67 34e65944 Isaku Yamahata
    return PCI_ERR_ROOT_CMD_FATAL_EN;
68 34e65944 Isaku Yamahata
}
69 34e65944 Isaku Yamahata
70 34e65944 Isaku Yamahata
static int aer_log_add_err(PCIEAERLog *aer_log, const PCIEAERErr *err)
71 34e65944 Isaku Yamahata
{
72 34e65944 Isaku Yamahata
    if (aer_log->log_num == aer_log->log_max) {
73 34e65944 Isaku Yamahata
        return -1;
74 34e65944 Isaku Yamahata
    }
75 34e65944 Isaku Yamahata
    memcpy(&aer_log->log[aer_log->log_num], err, sizeof *err);
76 34e65944 Isaku Yamahata
    aer_log->log_num++;
77 34e65944 Isaku Yamahata
    return 0;
78 34e65944 Isaku Yamahata
}
79 34e65944 Isaku Yamahata
80 34e65944 Isaku Yamahata
static void aer_log_del_err(PCIEAERLog *aer_log, PCIEAERErr *err)
81 34e65944 Isaku Yamahata
{
82 34e65944 Isaku Yamahata
    assert(aer_log->log_num);
83 34e65944 Isaku Yamahata
    *err = aer_log->log[0];
84 34e65944 Isaku Yamahata
    aer_log->log_num--;
85 34e65944 Isaku Yamahata
    memmove(&aer_log->log[0], &aer_log->log[1],
86 34e65944 Isaku Yamahata
            aer_log->log_num * sizeof *err);
87 34e65944 Isaku Yamahata
}
88 34e65944 Isaku Yamahata
89 34e65944 Isaku Yamahata
static void aer_log_clear_all_err(PCIEAERLog *aer_log)
90 34e65944 Isaku Yamahata
{
91 34e65944 Isaku Yamahata
    aer_log->log_num = 0;
92 34e65944 Isaku Yamahata
}
93 34e65944 Isaku Yamahata
94 34e65944 Isaku Yamahata
int pcie_aer_init(PCIDevice *dev, uint16_t offset)
95 34e65944 Isaku Yamahata
{
96 34e65944 Isaku Yamahata
    PCIExpressDevice *exp;
97 34e65944 Isaku Yamahata
98 34e65944 Isaku Yamahata
    pcie_add_capability(dev, PCI_EXT_CAP_ID_ERR, PCI_ERR_VER,
99 34e65944 Isaku Yamahata
                        offset, PCI_ERR_SIZEOF);
100 34e65944 Isaku Yamahata
    exp = &dev->exp;
101 34e65944 Isaku Yamahata
    exp->aer_cap = offset;
102 34e65944 Isaku Yamahata
103 34e65944 Isaku Yamahata
    /* log_max is property */
104 34e65944 Isaku Yamahata
    if (dev->exp.aer_log.log_max == PCIE_AER_LOG_MAX_UNSET) {
105 34e65944 Isaku Yamahata
        dev->exp.aer_log.log_max = PCIE_AER_LOG_MAX_DEFAULT;
106 34e65944 Isaku Yamahata
    }
107 34e65944 Isaku Yamahata
    /* clip down the value to avoid unreasobale memory usage */
108 34e65944 Isaku Yamahata
    if (dev->exp.aer_log.log_max > PCIE_AER_LOG_MAX_LIMIT) {
109 34e65944 Isaku Yamahata
        return -EINVAL;
110 34e65944 Isaku Yamahata
    }
111 34e65944 Isaku Yamahata
    dev->exp.aer_log.log = qemu_mallocz(sizeof dev->exp.aer_log.log[0] *
112 34e65944 Isaku Yamahata
                                        dev->exp.aer_log.log_max);
113 34e65944 Isaku Yamahata
114 34e65944 Isaku Yamahata
    pci_set_long(dev->w1cmask + offset + PCI_ERR_UNCOR_STATUS,
115 34e65944 Isaku Yamahata
                 PCI_ERR_UNC_SUPPORTED);
116 34e65944 Isaku Yamahata
117 34e65944 Isaku Yamahata
    pci_set_long(dev->config + offset + PCI_ERR_UNCOR_SEVER,
118 34e65944 Isaku Yamahata
                 PCI_ERR_UNC_SEVERITY_DEFAULT);
119 34e65944 Isaku Yamahata
    pci_set_long(dev->wmask + offset + PCI_ERR_UNCOR_SEVER,
120 34e65944 Isaku Yamahata
                 PCI_ERR_UNC_SUPPORTED);
121 34e65944 Isaku Yamahata
122 34e65944 Isaku Yamahata
    pci_long_test_and_set_mask(dev->w1cmask + offset + PCI_ERR_COR_STATUS,
123 34e65944 Isaku Yamahata
                               PCI_ERR_COR_STATUS);
124 34e65944 Isaku Yamahata
125 34e65944 Isaku Yamahata
    pci_set_long(dev->config + offset + PCI_ERR_COR_MASK,
126 34e65944 Isaku Yamahata
                 PCI_ERR_COR_MASK_DEFAULT);
127 34e65944 Isaku Yamahata
    pci_set_long(dev->wmask + offset + PCI_ERR_COR_MASK,
128 34e65944 Isaku Yamahata
                 PCI_ERR_COR_SUPPORTED);
129 34e65944 Isaku Yamahata
130 34e65944 Isaku Yamahata
    /* capabilities and control. multiple header logging is supported */
131 34e65944 Isaku Yamahata
    if (dev->exp.aer_log.log_max > 0) {
132 34e65944 Isaku Yamahata
        pci_set_long(dev->config + offset + PCI_ERR_CAP,
133 34e65944 Isaku Yamahata
                     PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC |
134 34e65944 Isaku Yamahata
                     PCI_ERR_CAP_MHRC);
135 34e65944 Isaku Yamahata
        pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
136 34e65944 Isaku Yamahata
                     PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE |
137 34e65944 Isaku Yamahata
                     PCI_ERR_CAP_MHRE);
138 34e65944 Isaku Yamahata
    } else {
139 34e65944 Isaku Yamahata
        pci_set_long(dev->config + offset + PCI_ERR_CAP,
140 34e65944 Isaku Yamahata
                     PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC);
141 34e65944 Isaku Yamahata
        pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
142 34e65944 Isaku Yamahata
                     PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
143 34e65944 Isaku Yamahata
    }
144 34e65944 Isaku Yamahata
145 34e65944 Isaku Yamahata
    switch (pcie_cap_get_type(dev)) {
146 34e65944 Isaku Yamahata
    case PCI_EXP_TYPE_ROOT_PORT:
147 34e65944 Isaku Yamahata
        /* this case will be set by pcie_aer_root_init() */
148 34e65944 Isaku Yamahata
        /* fallthrough */
149 34e65944 Isaku Yamahata
    case PCI_EXP_TYPE_DOWNSTREAM:
150 34e65944 Isaku Yamahata
    case PCI_EXP_TYPE_UPSTREAM:
151 34e65944 Isaku Yamahata
        pci_word_test_and_set_mask(dev->wmask + PCI_BRIDGE_CONTROL,
152 34e65944 Isaku Yamahata
                                   PCI_BRIDGE_CTL_SERR);
153 34e65944 Isaku Yamahata
        pci_long_test_and_set_mask(dev->w1cmask + PCI_STATUS,
154 34e65944 Isaku Yamahata
                                   PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
155 34e65944 Isaku Yamahata
        break;
156 34e65944 Isaku Yamahata
    default:
157 34e65944 Isaku Yamahata
        /* nothing */
158 34e65944 Isaku Yamahata
        break;
159 34e65944 Isaku Yamahata
    }
160 34e65944 Isaku Yamahata
    return 0;
161 34e65944 Isaku Yamahata
}
162 34e65944 Isaku Yamahata
163 34e65944 Isaku Yamahata
void pcie_aer_exit(PCIDevice *dev)
164 34e65944 Isaku Yamahata
{
165 34e65944 Isaku Yamahata
    qemu_free(dev->exp.aer_log.log);
166 34e65944 Isaku Yamahata
}
167 34e65944 Isaku Yamahata
168 34e65944 Isaku Yamahata
static void pcie_aer_update_uncor_status(PCIDevice *dev)
169 34e65944 Isaku Yamahata
{
170 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
171 34e65944 Isaku Yamahata
    PCIEAERLog *aer_log = &dev->exp.aer_log;
172 34e65944 Isaku Yamahata
173 34e65944 Isaku Yamahata
    uint16_t i;
174 34e65944 Isaku Yamahata
    for (i = 0; i < aer_log->log_num; i++) {
175 34e65944 Isaku Yamahata
        pci_long_test_and_set_mask(aer_cap + PCI_ERR_UNCOR_STATUS,
176 34e65944 Isaku Yamahata
                                   dev->exp.aer_log.log[i].status);
177 34e65944 Isaku Yamahata
    }
178 34e65944 Isaku Yamahata
}
179 34e65944 Isaku Yamahata
180 34e65944 Isaku Yamahata
/*
181 34e65944 Isaku Yamahata
 * return value:
182 247c97f3 Michael S. Tsirkin
 * true: error message needs to be sent up
183 34e65944 Isaku Yamahata
 * false: error message is masked
184 34e65944 Isaku Yamahata
 *
185 34e65944 Isaku Yamahata
 * 6.2.6 Error Message Control
186 34e65944 Isaku Yamahata
 * Figure 6-3
187 34e65944 Isaku Yamahata
 * all pci express devices part
188 34e65944 Isaku Yamahata
 */
189 34e65944 Isaku Yamahata
static bool
190 34e65944 Isaku Yamahata
pcie_aer_msg_alldev(PCIDevice *dev, const PCIEAERMsg *msg)
191 34e65944 Isaku Yamahata
{
192 34e65944 Isaku Yamahata
    if (!(pcie_aer_msg_is_uncor(msg) &&
193 34e65944 Isaku Yamahata
          (pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_SERR))) {
194 34e65944 Isaku Yamahata
        return false;
195 34e65944 Isaku Yamahata
    }
196 34e65944 Isaku Yamahata
197 34e65944 Isaku Yamahata
    /* Signaled System Error
198 34e65944 Isaku Yamahata
     *
199 34e65944 Isaku Yamahata
     * 7.5.1.1 Command register
200 34e65944 Isaku Yamahata
     * Bit 8 SERR# Enable
201 34e65944 Isaku Yamahata
     *
202 34e65944 Isaku Yamahata
     * When Set, this bit enables reporting of Non-fatal and Fatal
203 34e65944 Isaku Yamahata
     * errors detected by the Function to the Root Complex. Note that
204 34e65944 Isaku Yamahata
     * errors are reported if enabled either through this bit or through
205 34e65944 Isaku Yamahata
     * the PCI Express specific bits in the Device Control register (see
206 34e65944 Isaku Yamahata
     * Section 7.8.4).
207 34e65944 Isaku Yamahata
     */
208 34e65944 Isaku Yamahata
    pci_word_test_and_set_mask(dev->config + PCI_STATUS,
209 34e65944 Isaku Yamahata
                               PCI_STATUS_SIG_SYSTEM_ERROR);
210 34e65944 Isaku Yamahata
211 34e65944 Isaku Yamahata
    if (!(msg->severity &
212 34e65944 Isaku Yamahata
          pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL))) {
213 34e65944 Isaku Yamahata
        return false;
214 34e65944 Isaku Yamahata
    }
215 34e65944 Isaku Yamahata
216 34e65944 Isaku Yamahata
    /* send up error message */
217 247c97f3 Michael S. Tsirkin
    return true;
218 247c97f3 Michael S. Tsirkin
}
219 247c97f3 Michael S. Tsirkin
220 34e65944 Isaku Yamahata
/*
221 34e65944 Isaku Yamahata
 * return value:
222 34e65944 Isaku Yamahata
 * true: error message is sent up
223 34e65944 Isaku Yamahata
 * false: error message is masked
224 34e65944 Isaku Yamahata
 *
225 34e65944 Isaku Yamahata
 * 6.2.6 Error Message Control
226 34e65944 Isaku Yamahata
 * Figure 6-3
227 34e65944 Isaku Yamahata
 * virtual pci bridge part
228 34e65944 Isaku Yamahata
 */
229 34e65944 Isaku Yamahata
static bool pcie_aer_msg_vbridge(PCIDevice *dev, const PCIEAERMsg *msg)
230 34e65944 Isaku Yamahata
{
231 34e65944 Isaku Yamahata
    uint16_t bridge_control = pci_get_word(dev->config + PCI_BRIDGE_CONTROL);
232 34e65944 Isaku Yamahata
233 34e65944 Isaku Yamahata
    if (pcie_aer_msg_is_uncor(msg)) {
234 34e65944 Isaku Yamahata
        /* Received System Error */
235 34e65944 Isaku Yamahata
        pci_word_test_and_set_mask(dev->config + PCI_SEC_STATUS,
236 34e65944 Isaku Yamahata
                                   PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
237 34e65944 Isaku Yamahata
    }
238 34e65944 Isaku Yamahata
239 34e65944 Isaku Yamahata
    if (!(bridge_control & PCI_BRIDGE_CTL_SERR)) {
240 34e65944 Isaku Yamahata
        return false;
241 34e65944 Isaku Yamahata
    }
242 34e65944 Isaku Yamahata
    return true;
243 34e65944 Isaku Yamahata
}
244 34e65944 Isaku Yamahata
245 34e65944 Isaku Yamahata
void pcie_aer_root_set_vector(PCIDevice *dev, unsigned int vector)
246 34e65944 Isaku Yamahata
{
247 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
248 34e65944 Isaku Yamahata
    assert(vector < PCI_ERR_ROOT_IRQ_MAX);
249 34e65944 Isaku Yamahata
    pci_long_test_and_clear_mask(aer_cap + PCI_ERR_ROOT_STATUS,
250 34e65944 Isaku Yamahata
                                 PCI_ERR_ROOT_IRQ);
251 34e65944 Isaku Yamahata
    pci_long_test_and_set_mask(aer_cap + PCI_ERR_ROOT_STATUS,
252 34e65944 Isaku Yamahata
                               vector << PCI_ERR_ROOT_IRQ_SHIFT);
253 34e65944 Isaku Yamahata
}
254 34e65944 Isaku Yamahata
255 34e65944 Isaku Yamahata
static unsigned int pcie_aer_root_get_vector(PCIDevice *dev)
256 34e65944 Isaku Yamahata
{
257 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
258 34e65944 Isaku Yamahata
    uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
259 34e65944 Isaku Yamahata
    return (root_status & PCI_ERR_ROOT_IRQ) >> PCI_ERR_ROOT_IRQ_SHIFT;
260 34e65944 Isaku Yamahata
}
261 34e65944 Isaku Yamahata
262 c3f33667 Michael S. Tsirkin
/* Given a status register, get corresponding bits in the command register */
263 c3f33667 Michael S. Tsirkin
static uint32_t pcie_aer_status_to_cmd(uint32_t status)
264 c3f33667 Michael S. Tsirkin
{
265 c3f33667 Michael S. Tsirkin
    uint32_t cmd = 0;
266 c3f33667 Michael S. Tsirkin
    if (status & PCI_ERR_ROOT_COR_RCV) {
267 c3f33667 Michael S. Tsirkin
        cmd |= PCI_ERR_ROOT_CMD_COR_EN;
268 c3f33667 Michael S. Tsirkin
    }
269 c3f33667 Michael S. Tsirkin
    if (status & PCI_ERR_ROOT_NONFATAL_RCV) {
270 c3f33667 Michael S. Tsirkin
        cmd |= PCI_ERR_ROOT_CMD_NONFATAL_EN;
271 c3f33667 Michael S. Tsirkin
    }
272 c3f33667 Michael S. Tsirkin
    if (status & PCI_ERR_ROOT_FATAL_RCV) {
273 c3f33667 Michael S. Tsirkin
        cmd |= PCI_ERR_ROOT_CMD_FATAL_EN;
274 c3f33667 Michael S. Tsirkin
    }
275 c3f33667 Michael S. Tsirkin
    return cmd;
276 c3f33667 Michael S. Tsirkin
}
277 c3f33667 Michael S. Tsirkin
278 513691b7 Michael S. Tsirkin
static void pcie_aer_root_notify(PCIDevice *dev)
279 513691b7 Michael S. Tsirkin
{
280 513691b7 Michael S. Tsirkin
    if (msix_enabled(dev)) {
281 513691b7 Michael S. Tsirkin
        msix_notify(dev, pcie_aer_root_get_vector(dev));
282 513691b7 Michael S. Tsirkin
    } else if (msi_enabled(dev)) {
283 513691b7 Michael S. Tsirkin
        msi_notify(dev, pcie_aer_root_get_vector(dev));
284 513691b7 Michael S. Tsirkin
    } else {
285 513691b7 Michael S. Tsirkin
        qemu_set_irq(dev->irq[dev->exp.aer_intx], 1);
286 513691b7 Michael S. Tsirkin
    }
287 513691b7 Michael S. Tsirkin
}
288 513691b7 Michael S. Tsirkin
289 34e65944 Isaku Yamahata
/*
290 34e65944 Isaku Yamahata
 * 6.2.6 Error Message Control
291 34e65944 Isaku Yamahata
 * Figure 6-3
292 34e65944 Isaku Yamahata
 * root port part
293 34e65944 Isaku Yamahata
 */
294 5f47c187 Michael S. Tsirkin
static void pcie_aer_msg_root_port(PCIDevice *dev, const PCIEAERMsg *msg)
295 34e65944 Isaku Yamahata
{
296 34e65944 Isaku Yamahata
    uint16_t cmd;
297 34e65944 Isaku Yamahata
    uint8_t *aer_cap;
298 34e65944 Isaku Yamahata
    uint32_t root_cmd;
299 c3f33667 Michael S. Tsirkin
    uint32_t root_status, prev_status;
300 34e65944 Isaku Yamahata
301 34e65944 Isaku Yamahata
    cmd = pci_get_word(dev->config + PCI_COMMAND);
302 34e65944 Isaku Yamahata
    aer_cap = dev->config + dev->exp.aer_cap;
303 34e65944 Isaku Yamahata
    root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
304 c3f33667 Michael S. Tsirkin
    prev_status = root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
305 34e65944 Isaku Yamahata
306 34e65944 Isaku Yamahata
    if (cmd & PCI_COMMAND_SERR) {
307 34e65944 Isaku Yamahata
        /* System Error.
308 34e65944 Isaku Yamahata
         *
309 34e65944 Isaku Yamahata
         * The way to report System Error is platform specific and
310 34e65944 Isaku Yamahata
         * it isn't implemented in qemu right now.
311 34e65944 Isaku Yamahata
         * So just discard the error for now.
312 34e65944 Isaku Yamahata
         * OS which cares of aer would receive errors via
313 34e65944 Isaku Yamahata
         * native aer mechanims, so this wouldn't matter.
314 34e65944 Isaku Yamahata
         */
315 34e65944 Isaku Yamahata
    }
316 34e65944 Isaku Yamahata
317 34e65944 Isaku Yamahata
    /* Errro Message Received: Root Error Status register */
318 34e65944 Isaku Yamahata
    switch (msg->severity) {
319 34e65944 Isaku Yamahata
    case PCI_ERR_ROOT_CMD_COR_EN:
320 34e65944 Isaku Yamahata
        if (root_status & PCI_ERR_ROOT_COR_RCV) {
321 34e65944 Isaku Yamahata
            root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
322 34e65944 Isaku Yamahata
        } else {
323 34e65944 Isaku Yamahata
            pci_set_word(aer_cap + PCI_ERR_ROOT_COR_SRC, msg->source_id);
324 34e65944 Isaku Yamahata
        }
325 34e65944 Isaku Yamahata
        root_status |= PCI_ERR_ROOT_COR_RCV;
326 34e65944 Isaku Yamahata
        break;
327 34e65944 Isaku Yamahata
    case PCI_ERR_ROOT_CMD_NONFATAL_EN:
328 34e65944 Isaku Yamahata
        root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
329 34e65944 Isaku Yamahata
        break;
330 34e65944 Isaku Yamahata
    case PCI_ERR_ROOT_CMD_FATAL_EN:
331 34e65944 Isaku Yamahata
        if (!(root_status & PCI_ERR_ROOT_UNCOR_RCV)) {
332 34e65944 Isaku Yamahata
            root_status |= PCI_ERR_ROOT_FIRST_FATAL;
333 34e65944 Isaku Yamahata
        }
334 34e65944 Isaku Yamahata
        root_status |= PCI_ERR_ROOT_FATAL_RCV;
335 34e65944 Isaku Yamahata
        break;
336 34e65944 Isaku Yamahata
    default:
337 34e65944 Isaku Yamahata
        abort();
338 34e65944 Isaku Yamahata
        break;
339 34e65944 Isaku Yamahata
    }
340 34e65944 Isaku Yamahata
    if (pcie_aer_msg_is_uncor(msg)) {
341 34e65944 Isaku Yamahata
        if (root_status & PCI_ERR_ROOT_UNCOR_RCV) {
342 34e65944 Isaku Yamahata
            root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
343 34e65944 Isaku Yamahata
        } else {
344 34e65944 Isaku Yamahata
            pci_set_word(aer_cap + PCI_ERR_ROOT_SRC, msg->source_id);
345 34e65944 Isaku Yamahata
        }
346 34e65944 Isaku Yamahata
        root_status |= PCI_ERR_ROOT_UNCOR_RCV;
347 34e65944 Isaku Yamahata
    }
348 34e65944 Isaku Yamahata
    pci_set_long(aer_cap + PCI_ERR_ROOT_STATUS, root_status);
349 34e65944 Isaku Yamahata
350 c3f33667 Michael S. Tsirkin
    /* 6.2.4.1.2 Interrupt Generation */
351 c3f33667 Michael S. Tsirkin
    /* All the above did was set some bits in the status register.
352 c3f33667 Michael S. Tsirkin
     * Specifically these that match message severity.
353 c3f33667 Michael S. Tsirkin
     * The below code relies on this fact. */
354 c3f33667 Michael S. Tsirkin
    if (!(root_cmd & msg->severity) ||
355 c3f33667 Michael S. Tsirkin
        (pcie_aer_status_to_cmd(prev_status) & root_cmd)) {
356 c3f33667 Michael S. Tsirkin
        /* Condition is not being set or was already true so nothing to do. */
357 5f47c187 Michael S. Tsirkin
        return;
358 c3f33667 Michael S. Tsirkin
    }
359 c3f33667 Michael S. Tsirkin
360 513691b7 Michael S. Tsirkin
    pcie_aer_root_notify(dev);
361 34e65944 Isaku Yamahata
}
362 34e65944 Isaku Yamahata
363 34e65944 Isaku Yamahata
/*
364 34e65944 Isaku Yamahata
 * 6.2.6 Error Message Control Figure 6-3
365 247c97f3 Michael S. Tsirkin
 *
366 d33d9156 Michael S. Tsirkin
 * Walk up the bus tree from the device, propagate the error message.
367 34e65944 Isaku Yamahata
 */
368 d33d9156 Michael S. Tsirkin
static void pcie_aer_msg(PCIDevice *dev, const PCIEAERMsg *msg)
369 34e65944 Isaku Yamahata
{
370 34e65944 Isaku Yamahata
    uint8_t type;
371 34e65944 Isaku Yamahata
372 d33d9156 Michael S. Tsirkin
    while (dev) {
373 d33d9156 Michael S. Tsirkin
        if (!pci_is_express(dev)) {
374 d33d9156 Michael S. Tsirkin
            /* just ignore it */
375 d33d9156 Michael S. Tsirkin
            /* TODO: Shouldn't we set PCI_STATUS_SIG_SYSTEM_ERROR?
376 d33d9156 Michael S. Tsirkin
             * Consider e.g. a PCI bridge above a PCI Express device. */
377 34e65944 Isaku Yamahata
            return;
378 34e65944 Isaku Yamahata
        }
379 247c97f3 Michael S. Tsirkin
380 d33d9156 Michael S. Tsirkin
        type = pcie_cap_get_type(dev);
381 d33d9156 Michael S. Tsirkin
        if ((type == PCI_EXP_TYPE_ROOT_PORT ||
382 d33d9156 Michael S. Tsirkin
            type == PCI_EXP_TYPE_UPSTREAM ||
383 d33d9156 Michael S. Tsirkin
            type == PCI_EXP_TYPE_DOWNSTREAM) &&
384 d33d9156 Michael S. Tsirkin
            !pcie_aer_msg_vbridge(dev, msg)) {
385 d33d9156 Michael S. Tsirkin
                return;
386 d33d9156 Michael S. Tsirkin
        }
387 d33d9156 Michael S. Tsirkin
        if (!pcie_aer_msg_alldev(dev, msg)) {
388 d33d9156 Michael S. Tsirkin
            return;
389 d33d9156 Michael S. Tsirkin
        }
390 d33d9156 Michael S. Tsirkin
        if (type == PCI_EXP_TYPE_ROOT_PORT) {
391 d33d9156 Michael S. Tsirkin
            pcie_aer_msg_root_port(dev, msg);
392 d33d9156 Michael S. Tsirkin
            /* Root port can notify system itself,
393 d33d9156 Michael S. Tsirkin
               or send the error message to root complex event collector. */
394 d33d9156 Michael S. Tsirkin
            /*
395 d33d9156 Michael S. Tsirkin
             * if root port is associated with an event collector,
396 d33d9156 Michael S. Tsirkin
             * return the root complex event collector here.
397 d33d9156 Michael S. Tsirkin
             * For now root complex event collector isn't supported.
398 d33d9156 Michael S. Tsirkin
             */
399 247c97f3 Michael S. Tsirkin
            return;
400 247c97f3 Michael S. Tsirkin
        }
401 d33d9156 Michael S. Tsirkin
        dev = pci_bridge_get_device(dev->bus);
402 247c97f3 Michael S. Tsirkin
    }
403 34e65944 Isaku Yamahata
}
404 34e65944 Isaku Yamahata
405 34e65944 Isaku Yamahata
static void pcie_aer_update_log(PCIDevice *dev, const PCIEAERErr *err)
406 34e65944 Isaku Yamahata
{
407 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
408 e6e055c9 Blue Swirl
    uint8_t first_bit = ffs(err->status) - 1;
409 34e65944 Isaku Yamahata
    uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
410 34e65944 Isaku Yamahata
    int i;
411 34e65944 Isaku Yamahata
412 34e65944 Isaku Yamahata
    assert(err->status);
413 34e65944 Isaku Yamahata
    assert(err->status & (err->status - 1));
414 34e65944 Isaku Yamahata
415 34e65944 Isaku Yamahata
    errcap &= ~(PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
416 34e65944 Isaku Yamahata
    errcap |= PCI_ERR_CAP_FEP(first_bit);
417 34e65944 Isaku Yamahata
418 34e65944 Isaku Yamahata
    if (err->flags & PCIE_AER_ERR_HEADER_VALID) {
419 34e65944 Isaku Yamahata
        for (i = 0; i < ARRAY_SIZE(err->header); ++i) {
420 34e65944 Isaku Yamahata
            /* 7.10.8 Header Log Register */
421 34e65944 Isaku Yamahata
            uint8_t *header_log =
422 34e65944 Isaku Yamahata
                aer_cap + PCI_ERR_HEADER_LOG + i * sizeof err->header[0];
423 34e65944 Isaku Yamahata
            cpu_to_be32wu((uint32_t*)header_log, err->header[i]);
424 34e65944 Isaku Yamahata
        }
425 34e65944 Isaku Yamahata
    } else {
426 34e65944 Isaku Yamahata
        assert(!(err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT));
427 34e65944 Isaku Yamahata
        memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
428 34e65944 Isaku Yamahata
    }
429 34e65944 Isaku Yamahata
430 34e65944 Isaku Yamahata
    if ((err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT) &&
431 34e65944 Isaku Yamahata
        (pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
432 34e65944 Isaku Yamahata
         PCI_EXP_DEVCAP2_EETLPP)) {
433 34e65944 Isaku Yamahata
        for (i = 0; i < ARRAY_SIZE(err->prefix); ++i) {
434 34e65944 Isaku Yamahata
            /* 7.10.12 tlp prefix log register */
435 34e65944 Isaku Yamahata
            uint8_t *prefix_log =
436 34e65944 Isaku Yamahata
                aer_cap + PCI_ERR_TLP_PREFIX_LOG + i * sizeof err->prefix[0];
437 34e65944 Isaku Yamahata
            cpu_to_be32wu((uint32_t*)prefix_log, err->prefix[i]);
438 34e65944 Isaku Yamahata
        }
439 34e65944 Isaku Yamahata
        errcap |= PCI_ERR_CAP_TLP;
440 34e65944 Isaku Yamahata
    } else {
441 34e65944 Isaku Yamahata
        memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0,
442 34e65944 Isaku Yamahata
               PCI_ERR_TLP_PREFIX_LOG_SIZE);
443 34e65944 Isaku Yamahata
    }
444 34e65944 Isaku Yamahata
    pci_set_long(aer_cap + PCI_ERR_CAP, errcap);
445 34e65944 Isaku Yamahata
}
446 34e65944 Isaku Yamahata
447 34e65944 Isaku Yamahata
static void pcie_aer_clear_log(PCIDevice *dev)
448 34e65944 Isaku Yamahata
{
449 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
450 34e65944 Isaku Yamahata
451 34e65944 Isaku Yamahata
    pci_long_test_and_clear_mask(aer_cap + PCI_ERR_CAP,
452 34e65944 Isaku Yamahata
                                 PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
453 34e65944 Isaku Yamahata
454 34e65944 Isaku Yamahata
    memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
455 34e65944 Isaku Yamahata
    memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0, PCI_ERR_TLP_PREFIX_LOG_SIZE);
456 34e65944 Isaku Yamahata
}
457 34e65944 Isaku Yamahata
458 34e65944 Isaku Yamahata
static void pcie_aer_clear_error(PCIDevice *dev)
459 34e65944 Isaku Yamahata
{
460 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
461 34e65944 Isaku Yamahata
    uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
462 34e65944 Isaku Yamahata
    PCIEAERLog *aer_log = &dev->exp.aer_log;
463 34e65944 Isaku Yamahata
    PCIEAERErr err;
464 34e65944 Isaku Yamahata
465 34e65944 Isaku Yamahata
    if (!(errcap & PCI_ERR_CAP_MHRE) || !aer_log->log_num) {
466 34e65944 Isaku Yamahata
        pcie_aer_clear_log(dev);
467 34e65944 Isaku Yamahata
        return;
468 34e65944 Isaku Yamahata
    }
469 34e65944 Isaku Yamahata
470 34e65944 Isaku Yamahata
    /*
471 34e65944 Isaku Yamahata
     * If more errors are queued, set corresponding bits in uncorrectable
472 34e65944 Isaku Yamahata
     * error status.
473 34e65944 Isaku Yamahata
     * We emulate uncorrectable error status register as W1CS.
474 34e65944 Isaku Yamahata
     * So set bit in uncorrectable error status here again for multiple
475 34e65944 Isaku Yamahata
     * error recording support.
476 34e65944 Isaku Yamahata
     *
477 34e65944 Isaku Yamahata
     * 6.2.4.2 Multiple Error Handling(Advanced Error Reporting Capability)
478 34e65944 Isaku Yamahata
     */
479 34e65944 Isaku Yamahata
    pcie_aer_update_uncor_status(dev);
480 34e65944 Isaku Yamahata
481 34e65944 Isaku Yamahata
    aer_log_del_err(aer_log, &err);
482 34e65944 Isaku Yamahata
    pcie_aer_update_log(dev, &err);
483 34e65944 Isaku Yamahata
}
484 34e65944 Isaku Yamahata
485 34e65944 Isaku Yamahata
static int pcie_aer_record_error(PCIDevice *dev,
486 34e65944 Isaku Yamahata
                                 const PCIEAERErr *err)
487 34e65944 Isaku Yamahata
{
488 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
489 34e65944 Isaku Yamahata
    uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
490 34e65944 Isaku Yamahata
    int fep = PCI_ERR_CAP_FEP(errcap);
491 34e65944 Isaku Yamahata
492 34e65944 Isaku Yamahata
    assert(err->status);
493 34e65944 Isaku Yamahata
    assert(err->status & (err->status - 1));
494 34e65944 Isaku Yamahata
495 34e65944 Isaku Yamahata
    if (errcap & PCI_ERR_CAP_MHRE &&
496 34e65944 Isaku Yamahata
        (pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS) & (1U << fep))) {
497 34e65944 Isaku Yamahata
        /*  Not first error. queue error */
498 34e65944 Isaku Yamahata
        if (aer_log_add_err(&dev->exp.aer_log, err) < 0) {
499 34e65944 Isaku Yamahata
            /* overflow */
500 34e65944 Isaku Yamahata
            return -1;
501 34e65944 Isaku Yamahata
        }
502 34e65944 Isaku Yamahata
        return 0;
503 34e65944 Isaku Yamahata
    }
504 34e65944 Isaku Yamahata
505 34e65944 Isaku Yamahata
    pcie_aer_update_log(dev, err);
506 34e65944 Isaku Yamahata
    return 0;
507 34e65944 Isaku Yamahata
}
508 34e65944 Isaku Yamahata
509 34e65944 Isaku Yamahata
typedef struct PCIEAERInject {
510 34e65944 Isaku Yamahata
    PCIDevice *dev;
511 34e65944 Isaku Yamahata
    uint8_t *aer_cap;
512 34e65944 Isaku Yamahata
    const PCIEAERErr *err;
513 34e65944 Isaku Yamahata
    uint16_t devctl;
514 34e65944 Isaku Yamahata
    uint16_t devsta;
515 34e65944 Isaku Yamahata
    uint32_t error_status;
516 34e65944 Isaku Yamahata
    bool unsupported_request;
517 34e65944 Isaku Yamahata
    bool log_overflow;
518 34e65944 Isaku Yamahata
    PCIEAERMsg msg;
519 34e65944 Isaku Yamahata
} PCIEAERInject;
520 34e65944 Isaku Yamahata
521 34e65944 Isaku Yamahata
static bool pcie_aer_inject_cor_error(PCIEAERInject *inj,
522 34e65944 Isaku Yamahata
                                      uint32_t uncor_status,
523 34e65944 Isaku Yamahata
                                      bool is_advisory_nonfatal)
524 34e65944 Isaku Yamahata
{
525 34e65944 Isaku Yamahata
    PCIDevice *dev = inj->dev;
526 34e65944 Isaku Yamahata
527 34e65944 Isaku Yamahata
    inj->devsta |= PCI_EXP_DEVSTA_CED;
528 34e65944 Isaku Yamahata
    if (inj->unsupported_request) {
529 34e65944 Isaku Yamahata
        inj->devsta |= PCI_EXP_DEVSTA_URD;
530 34e65944 Isaku Yamahata
    }
531 34e65944 Isaku Yamahata
    pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
532 34e65944 Isaku Yamahata
533 34e65944 Isaku Yamahata
    if (inj->aer_cap) {
534 34e65944 Isaku Yamahata
        uint32_t mask;
535 34e65944 Isaku Yamahata
        pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_COR_STATUS,
536 34e65944 Isaku Yamahata
                                   inj->error_status);
537 34e65944 Isaku Yamahata
        mask = pci_get_long(inj->aer_cap + PCI_ERR_COR_MASK);
538 34e65944 Isaku Yamahata
        if (mask & inj->error_status) {
539 34e65944 Isaku Yamahata
            return false;
540 34e65944 Isaku Yamahata
        }
541 34e65944 Isaku Yamahata
        if (is_advisory_nonfatal) {
542 34e65944 Isaku Yamahata
            uint32_t uncor_mask =
543 34e65944 Isaku Yamahata
                pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
544 34e65944 Isaku Yamahata
            if (!(uncor_mask & uncor_status)) {
545 34e65944 Isaku Yamahata
                inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
546 34e65944 Isaku Yamahata
            }
547 34e65944 Isaku Yamahata
            pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
548 34e65944 Isaku Yamahata
                                       uncor_status);
549 34e65944 Isaku Yamahata
        }
550 34e65944 Isaku Yamahata
    }
551 34e65944 Isaku Yamahata
552 34e65944 Isaku Yamahata
    if (inj->unsupported_request && !(inj->devctl & PCI_EXP_DEVCTL_URRE)) {
553 34e65944 Isaku Yamahata
        return false;
554 34e65944 Isaku Yamahata
    }
555 34e65944 Isaku Yamahata
    if (!(inj->devctl & PCI_EXP_DEVCTL_CERE)) {
556 34e65944 Isaku Yamahata
        return false;
557 34e65944 Isaku Yamahata
    }
558 34e65944 Isaku Yamahata
559 34e65944 Isaku Yamahata
    inj->msg.severity = PCI_ERR_ROOT_CMD_COR_EN;
560 34e65944 Isaku Yamahata
    return true;
561 34e65944 Isaku Yamahata
}
562 34e65944 Isaku Yamahata
563 34e65944 Isaku Yamahata
static bool pcie_aer_inject_uncor_error(PCIEAERInject *inj, bool is_fatal)
564 34e65944 Isaku Yamahata
{
565 34e65944 Isaku Yamahata
    PCIDevice *dev = inj->dev;
566 34e65944 Isaku Yamahata
    uint16_t cmd;
567 34e65944 Isaku Yamahata
568 34e65944 Isaku Yamahata
    if (is_fatal) {
569 34e65944 Isaku Yamahata
        inj->devsta |= PCI_EXP_DEVSTA_FED;
570 34e65944 Isaku Yamahata
    } else {
571 34e65944 Isaku Yamahata
        inj->devsta |= PCI_EXP_DEVSTA_NFED;
572 34e65944 Isaku Yamahata
    }
573 34e65944 Isaku Yamahata
    if (inj->unsupported_request) {
574 34e65944 Isaku Yamahata
        inj->devsta |= PCI_EXP_DEVSTA_URD;
575 34e65944 Isaku Yamahata
    }
576 34e65944 Isaku Yamahata
    pci_set_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
577 34e65944 Isaku Yamahata
578 34e65944 Isaku Yamahata
    if (inj->aer_cap) {
579 34e65944 Isaku Yamahata
        uint32_t mask = pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
580 34e65944 Isaku Yamahata
        if (mask & inj->error_status) {
581 34e65944 Isaku Yamahata
            pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
582 34e65944 Isaku Yamahata
                                       inj->error_status);
583 34e65944 Isaku Yamahata
            return false;
584 34e65944 Isaku Yamahata
        }
585 34e65944 Isaku Yamahata
586 34e65944 Isaku Yamahata
        inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
587 34e65944 Isaku Yamahata
        pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
588 34e65944 Isaku Yamahata
                                   inj->error_status);
589 34e65944 Isaku Yamahata
    }
590 34e65944 Isaku Yamahata
591 34e65944 Isaku Yamahata
    cmd = pci_get_word(dev->config + PCI_COMMAND);
592 34e65944 Isaku Yamahata
    if (inj->unsupported_request &&
593 34e65944 Isaku Yamahata
        !(inj->devctl & PCI_EXP_DEVCTL_URRE) && !(cmd & PCI_COMMAND_SERR)) {
594 34e65944 Isaku Yamahata
        return false;
595 34e65944 Isaku Yamahata
    }
596 34e65944 Isaku Yamahata
    if (is_fatal) {
597 34e65944 Isaku Yamahata
        if (!((cmd & PCI_COMMAND_SERR) ||
598 34e65944 Isaku Yamahata
              (inj->devctl & PCI_EXP_DEVCTL_FERE))) {
599 34e65944 Isaku Yamahata
            return false;
600 34e65944 Isaku Yamahata
        }
601 34e65944 Isaku Yamahata
        inj->msg.severity = PCI_ERR_ROOT_CMD_FATAL_EN;
602 34e65944 Isaku Yamahata
    } else {
603 34e65944 Isaku Yamahata
        if (!((cmd & PCI_COMMAND_SERR) ||
604 34e65944 Isaku Yamahata
              (inj->devctl & PCI_EXP_DEVCTL_NFERE))) {
605 34e65944 Isaku Yamahata
            return false;
606 34e65944 Isaku Yamahata
        }
607 34e65944 Isaku Yamahata
        inj->msg.severity = PCI_ERR_ROOT_CMD_NONFATAL_EN;
608 34e65944 Isaku Yamahata
    }
609 34e65944 Isaku Yamahata
    return true;
610 34e65944 Isaku Yamahata
}
611 34e65944 Isaku Yamahata
612 34e65944 Isaku Yamahata
/*
613 34e65944 Isaku Yamahata
 * non-Function specific error must be recorded in all functions.
614 34e65944 Isaku Yamahata
 * It is the responsibility of the caller of this function.
615 34e65944 Isaku Yamahata
 * It is also caller's responsiblity to determine which function should
616 34e65944 Isaku Yamahata
 * report the rerror.
617 34e65944 Isaku Yamahata
 *
618 34e65944 Isaku Yamahata
 * 6.2.4 Error Logging
619 34e65944 Isaku Yamahata
 * 6.2.5 Sqeunce of Device Error Signaling and Logging Operations
620 34e65944 Isaku Yamahata
 * table 6-2: Flowchard Showing Sequence of Device Error Signaling and Logging
621 34e65944 Isaku Yamahata
 *            Operations
622 34e65944 Isaku Yamahata
 */
623 34e65944 Isaku Yamahata
int pcie_aer_inject_error(PCIDevice *dev, const PCIEAERErr *err)
624 34e65944 Isaku Yamahata
{
625 34e65944 Isaku Yamahata
    uint8_t *aer_cap = NULL;
626 34e65944 Isaku Yamahata
    uint16_t devctl = 0;
627 34e65944 Isaku Yamahata
    uint16_t devsta = 0;
628 34e65944 Isaku Yamahata
    uint32_t error_status = err->status;
629 34e65944 Isaku Yamahata
    PCIEAERInject inj;
630 34e65944 Isaku Yamahata
631 34e65944 Isaku Yamahata
    if (!pci_is_express(dev)) {
632 34e65944 Isaku Yamahata
        return -ENOSYS;
633 34e65944 Isaku Yamahata
    }
634 34e65944 Isaku Yamahata
635 34e65944 Isaku Yamahata
    if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
636 34e65944 Isaku Yamahata
        error_status &= PCI_ERR_COR_SUPPORTED;
637 34e65944 Isaku Yamahata
    } else {
638 34e65944 Isaku Yamahata
        error_status &= PCI_ERR_UNC_SUPPORTED;
639 34e65944 Isaku Yamahata
    }
640 34e65944 Isaku Yamahata
641 34e65944 Isaku Yamahata
    /* invalid status bit. one and only one bit must be set */
642 34e65944 Isaku Yamahata
    if (!error_status || (error_status & (error_status - 1))) {
643 34e65944 Isaku Yamahata
        return -EINVAL;
644 34e65944 Isaku Yamahata
    }
645 34e65944 Isaku Yamahata
646 34e65944 Isaku Yamahata
    if (dev->exp.aer_cap) {
647 34e65944 Isaku Yamahata
        uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
648 34e65944 Isaku Yamahata
        aer_cap = dev->config + dev->exp.aer_cap;
649 34e65944 Isaku Yamahata
        devctl = pci_get_long(exp_cap + PCI_EXP_DEVCTL);
650 34e65944 Isaku Yamahata
        devsta = pci_get_long(exp_cap + PCI_EXP_DEVSTA);
651 34e65944 Isaku Yamahata
    }
652 34e65944 Isaku Yamahata
653 34e65944 Isaku Yamahata
    inj.dev = dev;
654 34e65944 Isaku Yamahata
    inj.aer_cap = aer_cap;
655 34e65944 Isaku Yamahata
    inj.err = err;
656 34e65944 Isaku Yamahata
    inj.devctl = devctl;
657 34e65944 Isaku Yamahata
    inj.devsta = devsta;
658 34e65944 Isaku Yamahata
    inj.error_status = error_status;
659 34e65944 Isaku Yamahata
    inj.unsupported_request = !(err->flags & PCIE_AER_ERR_IS_CORRECTABLE) &&
660 34e65944 Isaku Yamahata
        err->status == PCI_ERR_UNC_UNSUP;
661 34e65944 Isaku Yamahata
    inj.log_overflow = false;
662 34e65944 Isaku Yamahata
663 34e65944 Isaku Yamahata
    if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
664 34e65944 Isaku Yamahata
        if (!pcie_aer_inject_cor_error(&inj, 0, false)) {
665 34e65944 Isaku Yamahata
            return 0;
666 34e65944 Isaku Yamahata
        }
667 34e65944 Isaku Yamahata
    } else {
668 34e65944 Isaku Yamahata
        bool is_fatal =
669 34e65944 Isaku Yamahata
            pcie_aer_uncor_default_severity(error_status) ==
670 34e65944 Isaku Yamahata
            PCI_ERR_ROOT_CMD_FATAL_EN;
671 34e65944 Isaku Yamahata
        if (aer_cap) {
672 34e65944 Isaku Yamahata
            is_fatal =
673 34e65944 Isaku Yamahata
                error_status & pci_get_long(aer_cap + PCI_ERR_UNCOR_SEVER);
674 34e65944 Isaku Yamahata
        }
675 34e65944 Isaku Yamahata
        if (!is_fatal && (err->flags & PCIE_AER_ERR_MAYBE_ADVISORY)) {
676 34e65944 Isaku Yamahata
            inj.error_status = PCI_ERR_COR_ADV_NONFATAL;
677 34e65944 Isaku Yamahata
            if (!pcie_aer_inject_cor_error(&inj, error_status, true)) {
678 34e65944 Isaku Yamahata
                return 0;
679 34e65944 Isaku Yamahata
            }
680 34e65944 Isaku Yamahata
        } else {
681 34e65944 Isaku Yamahata
            if (!pcie_aer_inject_uncor_error(&inj, is_fatal)) {
682 34e65944 Isaku Yamahata
                return 0;
683 34e65944 Isaku Yamahata
            }
684 34e65944 Isaku Yamahata
        }
685 34e65944 Isaku Yamahata
    }
686 34e65944 Isaku Yamahata
687 34e65944 Isaku Yamahata
    /* send up error message */
688 34e65944 Isaku Yamahata
    inj.msg.source_id = err->source_id;
689 34e65944 Isaku Yamahata
    pcie_aer_msg(dev, &inj.msg);
690 34e65944 Isaku Yamahata
691 34e65944 Isaku Yamahata
    if (inj.log_overflow) {
692 34e65944 Isaku Yamahata
        PCIEAERErr header_log_overflow = {
693 34e65944 Isaku Yamahata
            .status = PCI_ERR_COR_HL_OVERFLOW,
694 34e65944 Isaku Yamahata
            .flags = PCIE_AER_ERR_IS_CORRECTABLE,
695 34e65944 Isaku Yamahata
        };
696 34e65944 Isaku Yamahata
        int ret = pcie_aer_inject_error(dev, &header_log_overflow);
697 34e65944 Isaku Yamahata
        assert(!ret);
698 34e65944 Isaku Yamahata
    }
699 34e65944 Isaku Yamahata
    return 0;
700 34e65944 Isaku Yamahata
}
701 34e65944 Isaku Yamahata
702 34e65944 Isaku Yamahata
void pcie_aer_write_config(PCIDevice *dev,
703 34e65944 Isaku Yamahata
                           uint32_t addr, uint32_t val, int len)
704 34e65944 Isaku Yamahata
{
705 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
706 34e65944 Isaku Yamahata
    uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
707 34e65944 Isaku Yamahata
    uint32_t first_error = 1U << PCI_ERR_CAP_FEP(errcap);
708 34e65944 Isaku Yamahata
    uint32_t uncorsta = pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS);
709 34e65944 Isaku Yamahata
710 34e65944 Isaku Yamahata
    /* uncorrectable error */
711 34e65944 Isaku Yamahata
    if (!(uncorsta & first_error)) {
712 34e65944 Isaku Yamahata
        /* the bit that corresponds to the first error is cleared */
713 34e65944 Isaku Yamahata
        pcie_aer_clear_error(dev);
714 34e65944 Isaku Yamahata
    } else if (errcap & PCI_ERR_CAP_MHRE) {
715 34e65944 Isaku Yamahata
        /* When PCI_ERR_CAP_MHRE is enabled and the first error isn't cleared
716 34e65944 Isaku Yamahata
         * nothing should happen. So we have to revert the modification to
717 34e65944 Isaku Yamahata
         * the register.
718 34e65944 Isaku Yamahata
         */
719 34e65944 Isaku Yamahata
        pcie_aer_update_uncor_status(dev);
720 34e65944 Isaku Yamahata
    } else {
721 34e65944 Isaku Yamahata
        /* capability & control
722 34e65944 Isaku Yamahata
         * PCI_ERR_CAP_MHRE might be cleared, so clear of header log.
723 34e65944 Isaku Yamahata
         */
724 34e65944 Isaku Yamahata
        aer_log_clear_all_err(&dev->exp.aer_log);
725 34e65944 Isaku Yamahata
    }
726 34e65944 Isaku Yamahata
}
727 34e65944 Isaku Yamahata
728 34e65944 Isaku Yamahata
void pcie_aer_root_init(PCIDevice *dev)
729 34e65944 Isaku Yamahata
{
730 34e65944 Isaku Yamahata
    uint16_t pos = dev->exp.aer_cap;
731 34e65944 Isaku Yamahata
732 34e65944 Isaku Yamahata
    pci_set_long(dev->wmask + pos + PCI_ERR_ROOT_COMMAND,
733 34e65944 Isaku Yamahata
                 PCI_ERR_ROOT_CMD_EN_MASK);
734 34e65944 Isaku Yamahata
    pci_set_long(dev->w1cmask + pos + PCI_ERR_ROOT_STATUS,
735 34e65944 Isaku Yamahata
                 PCI_ERR_ROOT_STATUS_REPORT_MASK);
736 34e65944 Isaku Yamahata
}
737 34e65944 Isaku Yamahata
738 34e65944 Isaku Yamahata
void pcie_aer_root_reset(PCIDevice *dev)
739 34e65944 Isaku Yamahata
{
740 34e65944 Isaku Yamahata
    uint8_t* aer_cap = dev->config + dev->exp.aer_cap;
741 34e65944 Isaku Yamahata
742 34e65944 Isaku Yamahata
    pci_set_long(aer_cap + PCI_ERR_ROOT_COMMAND, 0);
743 34e65944 Isaku Yamahata
744 34e65944 Isaku Yamahata
    /*
745 34e65944 Isaku Yamahata
     * Advanced Error Interrupt Message Number in Root Error Status Register
746 34e65944 Isaku Yamahata
     * must be updated by chip dependent code because it's chip dependent
747 34e65944 Isaku Yamahata
     * which number is used.
748 34e65944 Isaku Yamahata
     */
749 34e65944 Isaku Yamahata
}
750 34e65944 Isaku Yamahata
751 34e65944 Isaku Yamahata
void pcie_aer_root_write_config(PCIDevice *dev,
752 34e65944 Isaku Yamahata
                                uint32_t addr, uint32_t val, int len,
753 34e65944 Isaku Yamahata
                                uint32_t root_cmd_prev)
754 34e65944 Isaku Yamahata
{
755 34e65944 Isaku Yamahata
    uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
756 2b3cb353 Michael S. Tsirkin
    uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
757 2b3cb353 Michael S. Tsirkin
    uint32_t enabled_cmd = pcie_aer_status_to_cmd(root_status);
758 34e65944 Isaku Yamahata
    uint32_t root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
759 2b3cb353 Michael S. Tsirkin
    /* 6.2.4.1.2 Interrupt Generation */
760 2b3cb353 Michael S. Tsirkin
    if (!msix_enabled(dev) && !msi_enabled(dev)) {
761 2b3cb353 Michael S. Tsirkin
        qemu_set_irq(dev->irq[dev->exp.aer_intx], !!(root_cmd & enabled_cmd));
762 2b3cb353 Michael S. Tsirkin
        return;
763 2b3cb353 Michael S. Tsirkin
    }
764 34e65944 Isaku Yamahata
765 2b3cb353 Michael S. Tsirkin
    if ((root_cmd_prev & enabled_cmd) || !(root_cmd & enabled_cmd)) {
766 2b3cb353 Michael S. Tsirkin
        /* Send MSI on transition from false to true. */
767 2b3cb353 Michael S. Tsirkin
        return;
768 2b3cb353 Michael S. Tsirkin
    }
769 34e65944 Isaku Yamahata
770 513691b7 Michael S. Tsirkin
    pcie_aer_root_notify(dev);
771 34e65944 Isaku Yamahata
}
772 34e65944 Isaku Yamahata
773 34e65944 Isaku Yamahata
static const VMStateDescription vmstate_pcie_aer_err = {
774 34e65944 Isaku Yamahata
    .name = "PCIE_AER_ERROR",
775 34e65944 Isaku Yamahata
    .version_id = 1,
776 34e65944 Isaku Yamahata
    .minimum_version_id = 1,
777 34e65944 Isaku Yamahata
    .minimum_version_id_old = 1,
778 34e65944 Isaku Yamahata
    .fields     = (VMStateField[]) {
779 34e65944 Isaku Yamahata
        VMSTATE_UINT32(status, PCIEAERErr),
780 34e65944 Isaku Yamahata
        VMSTATE_UINT16(source_id, PCIEAERErr),
781 34e65944 Isaku Yamahata
        VMSTATE_UINT16(flags, PCIEAERErr),
782 34e65944 Isaku Yamahata
        VMSTATE_UINT32_ARRAY(header, PCIEAERErr, 4),
783 34e65944 Isaku Yamahata
        VMSTATE_UINT32_ARRAY(prefix, PCIEAERErr, 4),
784 34e65944 Isaku Yamahata
        VMSTATE_END_OF_LIST()
785 34e65944 Isaku Yamahata
    }
786 34e65944 Isaku Yamahata
};
787 34e65944 Isaku Yamahata
788 34e65944 Isaku Yamahata
const VMStateDescription vmstate_pcie_aer_log = {
789 34e65944 Isaku Yamahata
    .name = "PCIE_AER_ERROR_LOG",
790 34e65944 Isaku Yamahata
    .version_id = 1,
791 34e65944 Isaku Yamahata
    .minimum_version_id = 1,
792 34e65944 Isaku Yamahata
    .minimum_version_id_old = 1,
793 34e65944 Isaku Yamahata
    .fields     = (VMStateField[]) {
794 34e65944 Isaku Yamahata
        VMSTATE_UINT16(log_num, PCIEAERLog),
795 34e65944 Isaku Yamahata
        VMSTATE_UINT16(log_max, PCIEAERLog),
796 47188700 Dmitry Eremin-Solenikov
        VMSTATE_STRUCT_VARRAY_POINTER_UINT16(log, PCIEAERLog, log_num,
797 34e65944 Isaku Yamahata
                              vmstate_pcie_aer_err, PCIEAERErr),
798 34e65944 Isaku Yamahata
        VMSTATE_END_OF_LIST()
799 34e65944 Isaku Yamahata
    }
800 34e65944 Isaku Yamahata
};
801 2ae63bda Isaku Yamahata
802 2ae63bda Isaku Yamahata
void pcie_aer_inject_error_print(Monitor *mon, const QObject *data)
803 2ae63bda Isaku Yamahata
{
804 2ae63bda Isaku Yamahata
    QDict *qdict;
805 2ae63bda Isaku Yamahata
    int devfn;
806 2ae63bda Isaku Yamahata
    assert(qobject_type(data) == QTYPE_QDICT);
807 2ae63bda Isaku Yamahata
    qdict = qobject_to_qdict(data);
808 2ae63bda Isaku Yamahata
809 2ae63bda Isaku Yamahata
    devfn = (int)qdict_get_int(qdict, "devfn");
810 2ae63bda Isaku Yamahata
    monitor_printf(mon, "OK id: %s domain: %x, bus: %x devfn: %x.%x\n",
811 2ae63bda Isaku Yamahata
                   qdict_get_str(qdict, "id"),
812 2ae63bda Isaku Yamahata
                   (int) qdict_get_int(qdict, "domain"),
813 2ae63bda Isaku Yamahata
                   (int) qdict_get_int(qdict, "bus"),
814 2ae63bda Isaku Yamahata
                   PCI_SLOT(devfn), PCI_FUNC(devfn));
815 2ae63bda Isaku Yamahata
}
816 2ae63bda Isaku Yamahata
817 2ae63bda Isaku Yamahata
typedef struct PCIEAERErrorName {
818 2ae63bda Isaku Yamahata
    const char *name;
819 2ae63bda Isaku Yamahata
    uint32_t val;
820 2ae63bda Isaku Yamahata
    bool correctable;
821 2ae63bda Isaku Yamahata
} PCIEAERErrorName;
822 2ae63bda Isaku Yamahata
823 2ae63bda Isaku Yamahata
/*
824 2ae63bda Isaku Yamahata
 * AER error name -> value convertion table
825 2ae63bda Isaku Yamahata
 * This naming scheme is same to linux aer-injection tool.
826 2ae63bda Isaku Yamahata
 */
827 2ae63bda Isaku Yamahata
static const struct PCIEAERErrorName pcie_aer_error_list[] = {
828 2ae63bda Isaku Yamahata
    {
829 2ae63bda Isaku Yamahata
        .name = "TRAIN",
830 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_TRAIN,
831 2ae63bda Isaku Yamahata
        .correctable = false,
832 2ae63bda Isaku Yamahata
    }, {
833 2ae63bda Isaku Yamahata
        .name = "DLP",
834 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_DLP,
835 2ae63bda Isaku Yamahata
        .correctable = false,
836 2ae63bda Isaku Yamahata
    }, {
837 2ae63bda Isaku Yamahata
        .name = "SDN",
838 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_SDN,
839 2ae63bda Isaku Yamahata
        .correctable = false,
840 2ae63bda Isaku Yamahata
    }, {
841 2ae63bda Isaku Yamahata
        .name = "POISON_TLP",
842 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_POISON_TLP,
843 2ae63bda Isaku Yamahata
        .correctable = false,
844 2ae63bda Isaku Yamahata
    }, {
845 2ae63bda Isaku Yamahata
        .name = "FCP",
846 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_FCP,
847 2ae63bda Isaku Yamahata
        .correctable = false,
848 2ae63bda Isaku Yamahata
    }, {
849 2ae63bda Isaku Yamahata
        .name = "COMP_TIME",
850 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_COMP_TIME,
851 2ae63bda Isaku Yamahata
        .correctable = false,
852 2ae63bda Isaku Yamahata
    }, {
853 2ae63bda Isaku Yamahata
        .name = "COMP_ABORT",
854 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_COMP_ABORT,
855 2ae63bda Isaku Yamahata
        .correctable = false,
856 2ae63bda Isaku Yamahata
    }, {
857 2ae63bda Isaku Yamahata
        .name = "UNX_COMP",
858 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_UNX_COMP,
859 2ae63bda Isaku Yamahata
        .correctable = false,
860 2ae63bda Isaku Yamahata
    }, {
861 2ae63bda Isaku Yamahata
        .name = "RX_OVER",
862 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_RX_OVER,
863 2ae63bda Isaku Yamahata
        .correctable = false,
864 2ae63bda Isaku Yamahata
    }, {
865 2ae63bda Isaku Yamahata
        .name = "MALF_TLP",
866 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_MALF_TLP,
867 2ae63bda Isaku Yamahata
        .correctable = false,
868 2ae63bda Isaku Yamahata
    }, {
869 2ae63bda Isaku Yamahata
        .name = "ECRC",
870 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_ECRC,
871 2ae63bda Isaku Yamahata
        .correctable = false,
872 2ae63bda Isaku Yamahata
    }, {
873 2ae63bda Isaku Yamahata
        .name = "UNSUP",
874 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_UNSUP,
875 2ae63bda Isaku Yamahata
        .correctable = false,
876 2ae63bda Isaku Yamahata
    }, {
877 2ae63bda Isaku Yamahata
        .name = "ACSV",
878 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_ACSV,
879 2ae63bda Isaku Yamahata
        .correctable = false,
880 2ae63bda Isaku Yamahata
    }, {
881 2ae63bda Isaku Yamahata
        .name = "INTN",
882 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_INTN,
883 2ae63bda Isaku Yamahata
        .correctable = false,
884 2ae63bda Isaku Yamahata
    }, {
885 2ae63bda Isaku Yamahata
        .name = "MCBTLP",
886 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_MCBTLP,
887 2ae63bda Isaku Yamahata
        .correctable = false,
888 2ae63bda Isaku Yamahata
    }, {
889 2ae63bda Isaku Yamahata
        .name = "ATOP_EBLOCKED",
890 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_ATOP_EBLOCKED,
891 2ae63bda Isaku Yamahata
        .correctable = false,
892 2ae63bda Isaku Yamahata
    }, {
893 2ae63bda Isaku Yamahata
        .name = "TLP_PRF_BLOCKED",
894 2ae63bda Isaku Yamahata
        .val = PCI_ERR_UNC_TLP_PRF_BLOCKED,
895 2ae63bda Isaku Yamahata
        .correctable = false,
896 2ae63bda Isaku Yamahata
    }, {
897 2ae63bda Isaku Yamahata
        .name = "RCVR",
898 2ae63bda Isaku Yamahata
        .val = PCI_ERR_COR_RCVR,
899 2ae63bda Isaku Yamahata
        .correctable = true,
900 2ae63bda Isaku Yamahata
    }, {
901 2ae63bda Isaku Yamahata
        .name = "BAD_TLP",
902 2ae63bda Isaku Yamahata
        .val = PCI_ERR_COR_BAD_TLP,
903 2ae63bda Isaku Yamahata
        .correctable = true,
904 2ae63bda Isaku Yamahata
    }, {
905 2ae63bda Isaku Yamahata
        .name = "BAD_DLLP",
906 2ae63bda Isaku Yamahata
        .val = PCI_ERR_COR_BAD_DLLP,
907 2ae63bda Isaku Yamahata
        .correctable = true,
908 2ae63bda Isaku Yamahata
    }, {
909 2ae63bda Isaku Yamahata
        .name = "REP_ROLL",
910 2ae63bda Isaku Yamahata
        .val = PCI_ERR_COR_REP_ROLL,
911 2ae63bda Isaku Yamahata
        .correctable = true,
912 2ae63bda Isaku Yamahata
    }, {
913 2ae63bda Isaku Yamahata
        .name = "REP_TIMER",
914 2ae63bda Isaku Yamahata
        .val = PCI_ERR_COR_REP_TIMER,
915 2ae63bda Isaku Yamahata
        .correctable = true,
916 2ae63bda Isaku Yamahata
    }, {
917 2ae63bda Isaku Yamahata
        .name = "ADV_NONFATAL",
918 2ae63bda Isaku Yamahata
        .val = PCI_ERR_COR_ADV_NONFATAL,
919 2ae63bda Isaku Yamahata
        .correctable = true,
920 2ae63bda Isaku Yamahata
    }, {
921 2ae63bda Isaku Yamahata
        .name = "INTERNAL",
922 2ae63bda Isaku Yamahata
        .val = PCI_ERR_COR_INTERNAL,
923 2ae63bda Isaku Yamahata
        .correctable = true,
924 2ae63bda Isaku Yamahata
    }, {
925 2ae63bda Isaku Yamahata
        .name = "HL_OVERFLOW",
926 2ae63bda Isaku Yamahata
        .val = PCI_ERR_COR_HL_OVERFLOW,
927 2ae63bda Isaku Yamahata
        .correctable = true,
928 2ae63bda Isaku Yamahata
    },
929 2ae63bda Isaku Yamahata
};
930 2ae63bda Isaku Yamahata
931 2ae63bda Isaku Yamahata
static int pcie_aer_parse_error_string(const char *error_name,
932 2ae63bda Isaku Yamahata
                                       uint32_t *status, bool *correctable)
933 2ae63bda Isaku Yamahata
{
934 2ae63bda Isaku Yamahata
    int i;
935 2ae63bda Isaku Yamahata
936 2ae63bda Isaku Yamahata
    for (i = 0; i < ARRAY_SIZE(pcie_aer_error_list); i++) {
937 2ae63bda Isaku Yamahata
        const  PCIEAERErrorName *e = &pcie_aer_error_list[i];
938 2ae63bda Isaku Yamahata
        if (strcmp(error_name, e->name)) {
939 2ae63bda Isaku Yamahata
            continue;
940 2ae63bda Isaku Yamahata
        }
941 2ae63bda Isaku Yamahata
942 2ae63bda Isaku Yamahata
        *status = e->val;
943 2ae63bda Isaku Yamahata
        *correctable = e->correctable;
944 2ae63bda Isaku Yamahata
        return 0;
945 2ae63bda Isaku Yamahata
    }
946 2ae63bda Isaku Yamahata
    return -EINVAL;
947 2ae63bda Isaku Yamahata
}
948 2ae63bda Isaku Yamahata
949 2ae63bda Isaku Yamahata
int do_pcie_aer_inejct_error(Monitor *mon,
950 2ae63bda Isaku Yamahata
                             const QDict *qdict, QObject **ret_data)
951 2ae63bda Isaku Yamahata
{
952 2ae63bda Isaku Yamahata
    const char *id = qdict_get_str(qdict, "id");
953 2ae63bda Isaku Yamahata
    const char *error_name;
954 2ae63bda Isaku Yamahata
    uint32_t error_status;
955 2ae63bda Isaku Yamahata
    bool correctable;
956 2ae63bda Isaku Yamahata
    PCIDevice *dev;
957 2ae63bda Isaku Yamahata
    PCIEAERErr err;
958 2ae63bda Isaku Yamahata
    int ret;
959 2ae63bda Isaku Yamahata
960 2ae63bda Isaku Yamahata
    ret = pci_qdev_find_device(id, &dev);
961 2ae63bda Isaku Yamahata
    if (ret < 0) {
962 2ae63bda Isaku Yamahata
        monitor_printf(mon,
963 2ae63bda Isaku Yamahata
                       "id or pci device path is invalid or device not "
964 2ae63bda Isaku Yamahata
                       "found. %s\n", id);
965 2ae63bda Isaku Yamahata
        return ret;
966 2ae63bda Isaku Yamahata
    }
967 2ae63bda Isaku Yamahata
    if (!pci_is_express(dev)) {
968 2ae63bda Isaku Yamahata
        monitor_printf(mon, "the device doesn't support pci express. %s\n",
969 2ae63bda Isaku Yamahata
                       id);
970 2ae63bda Isaku Yamahata
        return -ENOSYS;
971 2ae63bda Isaku Yamahata
    }
972 2ae63bda Isaku Yamahata
973 2ae63bda Isaku Yamahata
    error_name = qdict_get_str(qdict, "error_status");
974 2ae63bda Isaku Yamahata
    if (pcie_aer_parse_error_string(error_name, &error_status, &correctable)) {
975 2ae63bda Isaku Yamahata
        char *e = NULL;
976 2ae63bda Isaku Yamahata
        error_status = strtoul(error_name, &e, 0);
977 2ae63bda Isaku Yamahata
        correctable = !!qdict_get_int(qdict, "correctable");
978 2ae63bda Isaku Yamahata
        if (!e || *e != '\0') {
979 2ae63bda Isaku Yamahata
            monitor_printf(mon, "invalid error status value. \"%s\"",
980 2ae63bda Isaku Yamahata
                           error_name);
981 2ae63bda Isaku Yamahata
            return -EINVAL;
982 2ae63bda Isaku Yamahata
        }
983 2ae63bda Isaku Yamahata
    }
984 2ae63bda Isaku Yamahata
    err.source_id = (pci_bus_num(dev->bus) << 8) | dev->devfn;
985 2ae63bda Isaku Yamahata
986 2ae63bda Isaku Yamahata
    err.flags = 0;
987 2ae63bda Isaku Yamahata
    if (correctable) {
988 2ae63bda Isaku Yamahata
        err.flags |= PCIE_AER_ERR_IS_CORRECTABLE;
989 2ae63bda Isaku Yamahata
    }
990 2ae63bda Isaku Yamahata
    if (qdict_get_int(qdict, "advisory_non_fatal")) {
991 2ae63bda Isaku Yamahata
        err.flags |= PCIE_AER_ERR_MAYBE_ADVISORY;
992 2ae63bda Isaku Yamahata
    }
993 2ae63bda Isaku Yamahata
    if (qdict_haskey(qdict, "header0")) {
994 2ae63bda Isaku Yamahata
        err.flags |= PCIE_AER_ERR_HEADER_VALID;
995 2ae63bda Isaku Yamahata
    }
996 2ae63bda Isaku Yamahata
    if (qdict_haskey(qdict, "prefix0")) {
997 2ae63bda Isaku Yamahata
        err.flags |= PCIE_AER_ERR_TLP_PREFIX_PRESENT;
998 2ae63bda Isaku Yamahata
    }
999 2ae63bda Isaku Yamahata
1000 2ae63bda Isaku Yamahata
    err.header[0] = qdict_get_try_int(qdict, "header0", 0);
1001 2ae63bda Isaku Yamahata
    err.header[1] = qdict_get_try_int(qdict, "header1", 0);
1002 2ae63bda Isaku Yamahata
    err.header[2] = qdict_get_try_int(qdict, "header2", 0);
1003 2ae63bda Isaku Yamahata
    err.header[3] = qdict_get_try_int(qdict, "header3", 0);
1004 2ae63bda Isaku Yamahata
1005 2ae63bda Isaku Yamahata
    err.prefix[0] = qdict_get_try_int(qdict, "prefix0", 0);
1006 2ae63bda Isaku Yamahata
    err.prefix[1] = qdict_get_try_int(qdict, "prefix1", 0);
1007 2ae63bda Isaku Yamahata
    err.prefix[2] = qdict_get_try_int(qdict, "prefix2", 0);
1008 2ae63bda Isaku Yamahata
    err.prefix[3] = qdict_get_try_int(qdict, "prefix3", 0);
1009 2ae63bda Isaku Yamahata
1010 2ae63bda Isaku Yamahata
    ret = pcie_aer_inject_error(dev, &err);
1011 2ae63bda Isaku Yamahata
    *ret_data = qobject_from_jsonf("{'id': %s, "
1012 2ae63bda Isaku Yamahata
                                   "'domain': %d, 'bus': %d, 'devfn': %d, "
1013 2ae63bda Isaku Yamahata
                                   "'ret': %d}",
1014 2ae63bda Isaku Yamahata
                                   id,
1015 2ae63bda Isaku Yamahata
                                   pci_find_domain(dev->bus),
1016 2ae63bda Isaku Yamahata
                                   pci_bus_num(dev->bus), dev->devfn,
1017 2ae63bda Isaku Yamahata
                                   ret);
1018 2ae63bda Isaku Yamahata
    assert(*ret_data);
1019 2ae63bda Isaku Yamahata
1020 2ae63bda Isaku Yamahata
    return 0;
1021 2ae63bda Isaku Yamahata
}