xref: /qemu/hw/pci/pcie_aer.c (revision a05f686ff39c373384772b01f1b7fc71e7eb2500)
134e65944SIsaku Yamahata /*
234e65944SIsaku Yamahata  * pcie_aer.c
334e65944SIsaku Yamahata  *
434e65944SIsaku Yamahata  * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
534e65944SIsaku Yamahata  *                    VA Linux Systems Japan K.K.
634e65944SIsaku Yamahata  *
734e65944SIsaku Yamahata  * This program is free software; you can redistribute it and/or modify
834e65944SIsaku Yamahata  * it under the terms of the GNU General Public License as published by
934e65944SIsaku Yamahata  * the Free Software Foundation; either version 2 of the License, or
1034e65944SIsaku Yamahata  * (at your option) any later version.
1134e65944SIsaku Yamahata  *
1234e65944SIsaku Yamahata  * This program is distributed in the hope that it will be useful,
1334e65944SIsaku Yamahata  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1434e65944SIsaku Yamahata  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1534e65944SIsaku Yamahata  * GNU General Public License for more details.
1634e65944SIsaku Yamahata  *
1734e65944SIsaku Yamahata  * You should have received a copy of the GNU General Public License along
1834e65944SIsaku Yamahata  * with this program; if not, see <http://www.gnu.org/licenses/>.
1934e65944SIsaku Yamahata  */
2034e65944SIsaku Yamahata 
219c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
227b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h"
2383c9089eSPaolo Bonzini #include "monitor/monitor.h"
24c759b24fSMichael S. Tsirkin #include "hw/pci/pci_bridge.h"
25c759b24fSMichael S. Tsirkin #include "hw/pci/pcie.h"
26c759b24fSMichael S. Tsirkin #include "hw/pci/msix.h"
27c759b24fSMichael S. Tsirkin #include "hw/pci/msi.h"
2806aac7bdSMichael S. Tsirkin #include "hw/pci/pci_bus.h"
29c759b24fSMichael S. Tsirkin #include "hw/pci/pcie_regs.h"
3034e65944SIsaku Yamahata 
3134e65944SIsaku Yamahata //#define DEBUG_PCIE
3234e65944SIsaku Yamahata #ifdef DEBUG_PCIE
3334e65944SIsaku Yamahata # define PCIE_DPRINTF(fmt, ...)                                         \
3434e65944SIsaku Yamahata     fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
3534e65944SIsaku Yamahata #else
3634e65944SIsaku Yamahata # define PCIE_DPRINTF(fmt, ...) do {} while (0)
3734e65944SIsaku Yamahata #endif
3834e65944SIsaku Yamahata #define PCIE_DEV_PRINTF(dev, fmt, ...)                                  \
3934e65944SIsaku Yamahata     PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
4034e65944SIsaku Yamahata 
4181486b55SJan Kiszka #define PCI_ERR_SRC_COR_OFFS    0
4281486b55SJan Kiszka #define PCI_ERR_SRC_UNCOR_OFFS  2
4381486b55SJan Kiszka 
4434e65944SIsaku Yamahata /* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */
4534e65944SIsaku Yamahata static uint32_t pcie_aer_uncor_default_severity(uint32_t status)
4634e65944SIsaku Yamahata {
4734e65944SIsaku Yamahata     switch (status) {
4834e65944SIsaku Yamahata     case PCI_ERR_UNC_INTN:
4934e65944SIsaku Yamahata     case PCI_ERR_UNC_DLP:
5034e65944SIsaku Yamahata     case PCI_ERR_UNC_SDN:
5134e65944SIsaku Yamahata     case PCI_ERR_UNC_RX_OVER:
5234e65944SIsaku Yamahata     case PCI_ERR_UNC_FCP:
5334e65944SIsaku Yamahata     case PCI_ERR_UNC_MALF_TLP:
5434e65944SIsaku Yamahata         return PCI_ERR_ROOT_CMD_FATAL_EN;
5534e65944SIsaku Yamahata     case PCI_ERR_UNC_POISON_TLP:
5634e65944SIsaku Yamahata     case PCI_ERR_UNC_ECRC:
5734e65944SIsaku Yamahata     case PCI_ERR_UNC_UNSUP:
5834e65944SIsaku Yamahata     case PCI_ERR_UNC_COMP_TIME:
5934e65944SIsaku Yamahata     case PCI_ERR_UNC_COMP_ABORT:
6034e65944SIsaku Yamahata     case PCI_ERR_UNC_UNX_COMP:
6134e65944SIsaku Yamahata     case PCI_ERR_UNC_ACSV:
6234e65944SIsaku Yamahata     case PCI_ERR_UNC_MCBTLP:
6334e65944SIsaku Yamahata     case PCI_ERR_UNC_ATOP_EBLOCKED:
6434e65944SIsaku Yamahata     case PCI_ERR_UNC_TLP_PRF_BLOCKED:
6534e65944SIsaku Yamahata         return PCI_ERR_ROOT_CMD_NONFATAL_EN;
6634e65944SIsaku Yamahata     default:
6734e65944SIsaku Yamahata         abort();
6834e65944SIsaku Yamahata         break;
6934e65944SIsaku Yamahata     }
7034e65944SIsaku Yamahata     return PCI_ERR_ROOT_CMD_FATAL_EN;
7134e65944SIsaku Yamahata }
7234e65944SIsaku Yamahata 
7334e65944SIsaku Yamahata static int aer_log_add_err(PCIEAERLog *aer_log, const PCIEAERErr *err)
7434e65944SIsaku Yamahata {
7534e65944SIsaku Yamahata     if (aer_log->log_num == aer_log->log_max) {
7634e65944SIsaku Yamahata         return -1;
7734e65944SIsaku Yamahata     }
7834e65944SIsaku Yamahata     memcpy(&aer_log->log[aer_log->log_num], err, sizeof *err);
7934e65944SIsaku Yamahata     aer_log->log_num++;
8034e65944SIsaku Yamahata     return 0;
8134e65944SIsaku Yamahata }
8234e65944SIsaku Yamahata 
8334e65944SIsaku Yamahata static void aer_log_del_err(PCIEAERLog *aer_log, PCIEAERErr *err)
8434e65944SIsaku Yamahata {
8534e65944SIsaku Yamahata     assert(aer_log->log_num);
8634e65944SIsaku Yamahata     *err = aer_log->log[0];
8734e65944SIsaku Yamahata     aer_log->log_num--;
8834e65944SIsaku Yamahata     memmove(&aer_log->log[0], &aer_log->log[1],
8934e65944SIsaku Yamahata             aer_log->log_num * sizeof *err);
9034e65944SIsaku Yamahata }
9134e65944SIsaku Yamahata 
9234e65944SIsaku Yamahata static void aer_log_clear_all_err(PCIEAERLog *aer_log)
9334e65944SIsaku Yamahata {
9434e65944SIsaku Yamahata     aer_log->log_num = 0;
9534e65944SIsaku Yamahata }
9634e65944SIsaku Yamahata 
9734e65944SIsaku Yamahata int pcie_aer_init(PCIDevice *dev, uint16_t offset)
9834e65944SIsaku Yamahata {
9934e65944SIsaku Yamahata     PCIExpressDevice *exp;
10034e65944SIsaku Yamahata 
10134e65944SIsaku Yamahata     pcie_add_capability(dev, PCI_EXT_CAP_ID_ERR, PCI_ERR_VER,
10234e65944SIsaku Yamahata                         offset, PCI_ERR_SIZEOF);
10334e65944SIsaku Yamahata     exp = &dev->exp;
10434e65944SIsaku Yamahata     exp->aer_cap = offset;
10534e65944SIsaku Yamahata 
10634e65944SIsaku Yamahata     /* log_max is property */
10734e65944SIsaku Yamahata     if (dev->exp.aer_log.log_max == PCIE_AER_LOG_MAX_UNSET) {
10834e65944SIsaku Yamahata         dev->exp.aer_log.log_max = PCIE_AER_LOG_MAX_DEFAULT;
10934e65944SIsaku Yamahata     }
11034e65944SIsaku Yamahata     /* clip down the value to avoid unreasobale memory usage */
11134e65944SIsaku Yamahata     if (dev->exp.aer_log.log_max > PCIE_AER_LOG_MAX_LIMIT) {
11234e65944SIsaku Yamahata         return -EINVAL;
11334e65944SIsaku Yamahata     }
1147267c094SAnthony Liguori     dev->exp.aer_log.log = g_malloc0(sizeof dev->exp.aer_log.log[0] *
11534e65944SIsaku Yamahata                                         dev->exp.aer_log.log_max);
11634e65944SIsaku Yamahata 
11734e65944SIsaku Yamahata     pci_set_long(dev->w1cmask + offset + PCI_ERR_UNCOR_STATUS,
11834e65944SIsaku Yamahata                  PCI_ERR_UNC_SUPPORTED);
11934e65944SIsaku Yamahata 
12034e65944SIsaku Yamahata     pci_set_long(dev->config + offset + PCI_ERR_UNCOR_SEVER,
12134e65944SIsaku Yamahata                  PCI_ERR_UNC_SEVERITY_DEFAULT);
12234e65944SIsaku Yamahata     pci_set_long(dev->wmask + offset + PCI_ERR_UNCOR_SEVER,
12334e65944SIsaku Yamahata                  PCI_ERR_UNC_SUPPORTED);
12434e65944SIsaku Yamahata 
12534e65944SIsaku Yamahata     pci_long_test_and_set_mask(dev->w1cmask + offset + PCI_ERR_COR_STATUS,
126310e91f7SChen Fan                                PCI_ERR_COR_SUPPORTED);
12734e65944SIsaku Yamahata 
12834e65944SIsaku Yamahata     pci_set_long(dev->config + offset + PCI_ERR_COR_MASK,
12934e65944SIsaku Yamahata                  PCI_ERR_COR_MASK_DEFAULT);
13034e65944SIsaku Yamahata     pci_set_long(dev->wmask + offset + PCI_ERR_COR_MASK,
13134e65944SIsaku Yamahata                  PCI_ERR_COR_SUPPORTED);
13234e65944SIsaku Yamahata 
13334e65944SIsaku Yamahata     /* capabilities and control. multiple header logging is supported */
13434e65944SIsaku Yamahata     if (dev->exp.aer_log.log_max > 0) {
13534e65944SIsaku Yamahata         pci_set_long(dev->config + offset + PCI_ERR_CAP,
13634e65944SIsaku Yamahata                      PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC |
13734e65944SIsaku Yamahata                      PCI_ERR_CAP_MHRC);
13834e65944SIsaku Yamahata         pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
13934e65944SIsaku Yamahata                      PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE |
14034e65944SIsaku Yamahata                      PCI_ERR_CAP_MHRE);
14134e65944SIsaku Yamahata     } else {
14234e65944SIsaku Yamahata         pci_set_long(dev->config + offset + PCI_ERR_CAP,
14334e65944SIsaku Yamahata                      PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC);
14434e65944SIsaku Yamahata         pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
14534e65944SIsaku Yamahata                      PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
14634e65944SIsaku Yamahata     }
14734e65944SIsaku Yamahata 
14834e65944SIsaku Yamahata     switch (pcie_cap_get_type(dev)) {
14934e65944SIsaku Yamahata     case PCI_EXP_TYPE_ROOT_PORT:
15034e65944SIsaku Yamahata         /* this case will be set by pcie_aer_root_init() */
15134e65944SIsaku Yamahata         /* fallthrough */
15234e65944SIsaku Yamahata     case PCI_EXP_TYPE_DOWNSTREAM:
15334e65944SIsaku Yamahata     case PCI_EXP_TYPE_UPSTREAM:
15434e65944SIsaku Yamahata         pci_word_test_and_set_mask(dev->wmask + PCI_BRIDGE_CONTROL,
15534e65944SIsaku Yamahata                                    PCI_BRIDGE_CTL_SERR);
15634e65944SIsaku Yamahata         pci_long_test_and_set_mask(dev->w1cmask + PCI_STATUS,
15734e65944SIsaku Yamahata                                    PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
15834e65944SIsaku Yamahata         break;
15934e65944SIsaku Yamahata     default:
16034e65944SIsaku Yamahata         /* nothing */
16134e65944SIsaku Yamahata         break;
16234e65944SIsaku Yamahata     }
16334e65944SIsaku Yamahata     return 0;
16434e65944SIsaku Yamahata }
16534e65944SIsaku Yamahata 
16634e65944SIsaku Yamahata void pcie_aer_exit(PCIDevice *dev)
16734e65944SIsaku Yamahata {
1687267c094SAnthony Liguori     g_free(dev->exp.aer_log.log);
16934e65944SIsaku Yamahata }
17034e65944SIsaku Yamahata 
17134e65944SIsaku Yamahata static void pcie_aer_update_uncor_status(PCIDevice *dev)
17234e65944SIsaku Yamahata {
17334e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
17434e65944SIsaku Yamahata     PCIEAERLog *aer_log = &dev->exp.aer_log;
17534e65944SIsaku Yamahata 
17634e65944SIsaku Yamahata     uint16_t i;
17734e65944SIsaku Yamahata     for (i = 0; i < aer_log->log_num; i++) {
17834e65944SIsaku Yamahata         pci_long_test_and_set_mask(aer_cap + PCI_ERR_UNCOR_STATUS,
17934e65944SIsaku Yamahata                                    dev->exp.aer_log.log[i].status);
18034e65944SIsaku Yamahata     }
18134e65944SIsaku Yamahata }
18234e65944SIsaku Yamahata 
18334e65944SIsaku Yamahata /*
18434e65944SIsaku Yamahata  * return value:
185247c97f3SMichael S. Tsirkin  * true: error message needs to be sent up
18634e65944SIsaku Yamahata  * false: error message is masked
18734e65944SIsaku Yamahata  *
18834e65944SIsaku Yamahata  * 6.2.6 Error Message Control
18934e65944SIsaku Yamahata  * Figure 6-3
19034e65944SIsaku Yamahata  * all pci express devices part
19134e65944SIsaku Yamahata  */
19234e65944SIsaku Yamahata static bool
19334e65944SIsaku Yamahata pcie_aer_msg_alldev(PCIDevice *dev, const PCIEAERMsg *msg)
19434e65944SIsaku Yamahata {
19534e65944SIsaku Yamahata     if (!(pcie_aer_msg_is_uncor(msg) &&
19634e65944SIsaku Yamahata           (pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_SERR))) {
19734e65944SIsaku Yamahata         return false;
19834e65944SIsaku Yamahata     }
19934e65944SIsaku Yamahata 
20034e65944SIsaku Yamahata     /* Signaled System Error
20134e65944SIsaku Yamahata      *
20234e65944SIsaku Yamahata      * 7.5.1.1 Command register
20334e65944SIsaku Yamahata      * Bit 8 SERR# Enable
20434e65944SIsaku Yamahata      *
20534e65944SIsaku Yamahata      * When Set, this bit enables reporting of Non-fatal and Fatal
20634e65944SIsaku Yamahata      * errors detected by the Function to the Root Complex. Note that
20734e65944SIsaku Yamahata      * errors are reported if enabled either through this bit or through
20834e65944SIsaku Yamahata      * the PCI Express specific bits in the Device Control register (see
20934e65944SIsaku Yamahata      * Section 7.8.4).
21034e65944SIsaku Yamahata      */
21134e65944SIsaku Yamahata     pci_word_test_and_set_mask(dev->config + PCI_STATUS,
21234e65944SIsaku Yamahata                                PCI_STATUS_SIG_SYSTEM_ERROR);
21334e65944SIsaku Yamahata 
21434e65944SIsaku Yamahata     if (!(msg->severity &
21534e65944SIsaku Yamahata           pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL))) {
21634e65944SIsaku Yamahata         return false;
21734e65944SIsaku Yamahata     }
21834e65944SIsaku Yamahata 
21934e65944SIsaku Yamahata     /* send up error message */
220247c97f3SMichael S. Tsirkin     return true;
221247c97f3SMichael S. Tsirkin }
222247c97f3SMichael S. Tsirkin 
22334e65944SIsaku Yamahata /*
22434e65944SIsaku Yamahata  * return value:
22534e65944SIsaku Yamahata  * true: error message is sent up
22634e65944SIsaku Yamahata  * false: error message is masked
22734e65944SIsaku Yamahata  *
22834e65944SIsaku Yamahata  * 6.2.6 Error Message Control
22934e65944SIsaku Yamahata  * Figure 6-3
23034e65944SIsaku Yamahata  * virtual pci bridge part
23134e65944SIsaku Yamahata  */
23234e65944SIsaku Yamahata static bool pcie_aer_msg_vbridge(PCIDevice *dev, const PCIEAERMsg *msg)
23334e65944SIsaku Yamahata {
23434e65944SIsaku Yamahata     uint16_t bridge_control = pci_get_word(dev->config + PCI_BRIDGE_CONTROL);
23534e65944SIsaku Yamahata 
23634e65944SIsaku Yamahata     if (pcie_aer_msg_is_uncor(msg)) {
23734e65944SIsaku Yamahata         /* Received System Error */
23834e65944SIsaku Yamahata         pci_word_test_and_set_mask(dev->config + PCI_SEC_STATUS,
23934e65944SIsaku Yamahata                                    PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
24034e65944SIsaku Yamahata     }
24134e65944SIsaku Yamahata 
24234e65944SIsaku Yamahata     if (!(bridge_control & PCI_BRIDGE_CTL_SERR)) {
24334e65944SIsaku Yamahata         return false;
24434e65944SIsaku Yamahata     }
24534e65944SIsaku Yamahata     return true;
24634e65944SIsaku Yamahata }
24734e65944SIsaku Yamahata 
24834e65944SIsaku Yamahata void pcie_aer_root_set_vector(PCIDevice *dev, unsigned int vector)
24934e65944SIsaku Yamahata {
25034e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
25134e65944SIsaku Yamahata     assert(vector < PCI_ERR_ROOT_IRQ_MAX);
25234e65944SIsaku Yamahata     pci_long_test_and_clear_mask(aer_cap + PCI_ERR_ROOT_STATUS,
25334e65944SIsaku Yamahata                                  PCI_ERR_ROOT_IRQ);
25434e65944SIsaku Yamahata     pci_long_test_and_set_mask(aer_cap + PCI_ERR_ROOT_STATUS,
25534e65944SIsaku Yamahata                                vector << PCI_ERR_ROOT_IRQ_SHIFT);
25634e65944SIsaku Yamahata }
25734e65944SIsaku Yamahata 
25834e65944SIsaku Yamahata static unsigned int pcie_aer_root_get_vector(PCIDevice *dev)
25934e65944SIsaku Yamahata {
26034e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
26134e65944SIsaku Yamahata     uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
26234e65944SIsaku Yamahata     return (root_status & PCI_ERR_ROOT_IRQ) >> PCI_ERR_ROOT_IRQ_SHIFT;
26334e65944SIsaku Yamahata }
26434e65944SIsaku Yamahata 
265c3f33667SMichael S. Tsirkin /* Given a status register, get corresponding bits in the command register */
266c3f33667SMichael S. Tsirkin static uint32_t pcie_aer_status_to_cmd(uint32_t status)
267c3f33667SMichael S. Tsirkin {
268c3f33667SMichael S. Tsirkin     uint32_t cmd = 0;
269c3f33667SMichael S. Tsirkin     if (status & PCI_ERR_ROOT_COR_RCV) {
270c3f33667SMichael S. Tsirkin         cmd |= PCI_ERR_ROOT_CMD_COR_EN;
271c3f33667SMichael S. Tsirkin     }
272c3f33667SMichael S. Tsirkin     if (status & PCI_ERR_ROOT_NONFATAL_RCV) {
273c3f33667SMichael S. Tsirkin         cmd |= PCI_ERR_ROOT_CMD_NONFATAL_EN;
274c3f33667SMichael S. Tsirkin     }
275c3f33667SMichael S. Tsirkin     if (status & PCI_ERR_ROOT_FATAL_RCV) {
276c3f33667SMichael S. Tsirkin         cmd |= PCI_ERR_ROOT_CMD_FATAL_EN;
277c3f33667SMichael S. Tsirkin     }
278c3f33667SMichael S. Tsirkin     return cmd;
279c3f33667SMichael S. Tsirkin }
280c3f33667SMichael S. Tsirkin 
281513691b7SMichael S. Tsirkin static void pcie_aer_root_notify(PCIDevice *dev)
282513691b7SMichael S. Tsirkin {
283513691b7SMichael S. Tsirkin     if (msix_enabled(dev)) {
284513691b7SMichael S. Tsirkin         msix_notify(dev, pcie_aer_root_get_vector(dev));
285513691b7SMichael S. Tsirkin     } else if (msi_enabled(dev)) {
286513691b7SMichael S. Tsirkin         msi_notify(dev, pcie_aer_root_get_vector(dev));
287513691b7SMichael S. Tsirkin     } else {
2885a03e708SMarcel Apfelbaum         pci_irq_assert(dev);
289513691b7SMichael S. Tsirkin     }
290513691b7SMichael S. Tsirkin }
291513691b7SMichael S. Tsirkin 
29234e65944SIsaku Yamahata /*
29334e65944SIsaku Yamahata  * 6.2.6 Error Message Control
29434e65944SIsaku Yamahata  * Figure 6-3
29534e65944SIsaku Yamahata  * root port part
29634e65944SIsaku Yamahata  */
2975f47c187SMichael S. Tsirkin static void pcie_aer_msg_root_port(PCIDevice *dev, const PCIEAERMsg *msg)
29834e65944SIsaku Yamahata {
29934e65944SIsaku Yamahata     uint16_t cmd;
30034e65944SIsaku Yamahata     uint8_t *aer_cap;
30134e65944SIsaku Yamahata     uint32_t root_cmd;
302c3f33667SMichael S. Tsirkin     uint32_t root_status, prev_status;
30334e65944SIsaku Yamahata 
30434e65944SIsaku Yamahata     cmd = pci_get_word(dev->config + PCI_COMMAND);
30534e65944SIsaku Yamahata     aer_cap = dev->config + dev->exp.aer_cap;
30634e65944SIsaku Yamahata     root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
307c3f33667SMichael S. Tsirkin     prev_status = root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
30834e65944SIsaku Yamahata 
30934e65944SIsaku Yamahata     if (cmd & PCI_COMMAND_SERR) {
31034e65944SIsaku Yamahata         /* System Error.
31134e65944SIsaku Yamahata          *
31234e65944SIsaku Yamahata          * The way to report System Error is platform specific and
31334e65944SIsaku Yamahata          * it isn't implemented in qemu right now.
31434e65944SIsaku Yamahata          * So just discard the error for now.
31534e65944SIsaku Yamahata          * OS which cares of aer would receive errors via
31634e65944SIsaku Yamahata          * native aer mechanims, so this wouldn't matter.
31734e65944SIsaku Yamahata          */
31834e65944SIsaku Yamahata     }
31934e65944SIsaku Yamahata 
32034e65944SIsaku Yamahata     /* Errro Message Received: Root Error Status register */
32134e65944SIsaku Yamahata     switch (msg->severity) {
32234e65944SIsaku Yamahata     case PCI_ERR_ROOT_CMD_COR_EN:
32334e65944SIsaku Yamahata         if (root_status & PCI_ERR_ROOT_COR_RCV) {
32434e65944SIsaku Yamahata             root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
32534e65944SIsaku Yamahata         } else {
32681486b55SJan Kiszka             pci_set_word(aer_cap + PCI_ERR_ROOT_ERR_SRC + PCI_ERR_SRC_COR_OFFS,
32781486b55SJan Kiszka                          msg->source_id);
32834e65944SIsaku Yamahata         }
32934e65944SIsaku Yamahata         root_status |= PCI_ERR_ROOT_COR_RCV;
33034e65944SIsaku Yamahata         break;
33134e65944SIsaku Yamahata     case PCI_ERR_ROOT_CMD_NONFATAL_EN:
33234e65944SIsaku Yamahata         root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
33334e65944SIsaku Yamahata         break;
33434e65944SIsaku Yamahata     case PCI_ERR_ROOT_CMD_FATAL_EN:
33534e65944SIsaku Yamahata         if (!(root_status & PCI_ERR_ROOT_UNCOR_RCV)) {
33634e65944SIsaku Yamahata             root_status |= PCI_ERR_ROOT_FIRST_FATAL;
33734e65944SIsaku Yamahata         }
33834e65944SIsaku Yamahata         root_status |= PCI_ERR_ROOT_FATAL_RCV;
33934e65944SIsaku Yamahata         break;
34034e65944SIsaku Yamahata     default:
34134e65944SIsaku Yamahata         abort();
34234e65944SIsaku Yamahata         break;
34334e65944SIsaku Yamahata     }
34434e65944SIsaku Yamahata     if (pcie_aer_msg_is_uncor(msg)) {
34534e65944SIsaku Yamahata         if (root_status & PCI_ERR_ROOT_UNCOR_RCV) {
34634e65944SIsaku Yamahata             root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
34734e65944SIsaku Yamahata         } else {
34881486b55SJan Kiszka             pci_set_word(aer_cap + PCI_ERR_ROOT_ERR_SRC +
34981486b55SJan Kiszka                          PCI_ERR_SRC_UNCOR_OFFS, msg->source_id);
35034e65944SIsaku Yamahata         }
35134e65944SIsaku Yamahata         root_status |= PCI_ERR_ROOT_UNCOR_RCV;
35234e65944SIsaku Yamahata     }
35334e65944SIsaku Yamahata     pci_set_long(aer_cap + PCI_ERR_ROOT_STATUS, root_status);
35434e65944SIsaku Yamahata 
35534e65944SIsaku Yamahata     /* 6.2.4.1.2 Interrupt Generation */
356c3f33667SMichael S. Tsirkin     /* All the above did was set some bits in the status register.
357c3f33667SMichael S. Tsirkin      * Specifically these that match message severity.
358c3f33667SMichael S. Tsirkin      * The below code relies on this fact. */
359c3f33667SMichael S. Tsirkin     if (!(root_cmd & msg->severity) ||
360c3f33667SMichael S. Tsirkin         (pcie_aer_status_to_cmd(prev_status) & root_cmd)) {
361c3f33667SMichael S. Tsirkin         /* Condition is not being set or was already true so nothing to do. */
3625f47c187SMichael S. Tsirkin         return;
363c3f33667SMichael S. Tsirkin     }
364c3f33667SMichael S. Tsirkin 
365513691b7SMichael S. Tsirkin     pcie_aer_root_notify(dev);
36634e65944SIsaku Yamahata }
36734e65944SIsaku Yamahata 
36834e65944SIsaku Yamahata /*
36934e65944SIsaku Yamahata  * 6.2.6 Error Message Control Figure 6-3
370247c97f3SMichael S. Tsirkin  *
371d33d9156SMichael S. Tsirkin  * Walk up the bus tree from the device, propagate the error message.
37234e65944SIsaku Yamahata  */
373247c97f3SMichael S. Tsirkin static void pcie_aer_msg(PCIDevice *dev, const PCIEAERMsg *msg)
374247c97f3SMichael S. Tsirkin {
375d33d9156SMichael S. Tsirkin     uint8_t type;
376d33d9156SMichael S. Tsirkin 
377247c97f3SMichael S. Tsirkin     while (dev) {
378d33d9156SMichael S. Tsirkin         if (!pci_is_express(dev)) {
379d33d9156SMichael S. Tsirkin             /* just ignore it */
380d33d9156SMichael S. Tsirkin             /* TODO: Shouldn't we set PCI_STATUS_SIG_SYSTEM_ERROR?
381d33d9156SMichael S. Tsirkin              * Consider e.g. a PCI bridge above a PCI Express device. */
382247c97f3SMichael S. Tsirkin             return;
383247c97f3SMichael S. Tsirkin         }
384d33d9156SMichael S. Tsirkin 
385d33d9156SMichael S. Tsirkin         type = pcie_cap_get_type(dev);
386d33d9156SMichael S. Tsirkin         if ((type == PCI_EXP_TYPE_ROOT_PORT ||
387d33d9156SMichael S. Tsirkin             type == PCI_EXP_TYPE_UPSTREAM ||
388d33d9156SMichael S. Tsirkin             type == PCI_EXP_TYPE_DOWNSTREAM) &&
389d33d9156SMichael S. Tsirkin             !pcie_aer_msg_vbridge(dev, msg)) {
390d33d9156SMichael S. Tsirkin                 return;
391d33d9156SMichael S. Tsirkin         }
392d33d9156SMichael S. Tsirkin         if (!pcie_aer_msg_alldev(dev, msg)) {
393d33d9156SMichael S. Tsirkin             return;
394d33d9156SMichael S. Tsirkin         }
395d33d9156SMichael S. Tsirkin         if (type == PCI_EXP_TYPE_ROOT_PORT) {
396d33d9156SMichael S. Tsirkin             pcie_aer_msg_root_port(dev, msg);
397d33d9156SMichael S. Tsirkin             /* Root port can notify system itself,
398d33d9156SMichael S. Tsirkin                or send the error message to root complex event collector. */
399d33d9156SMichael S. Tsirkin             /*
400d33d9156SMichael S. Tsirkin              * if root port is associated with an event collector,
401d33d9156SMichael S. Tsirkin              * return the root complex event collector here.
402d33d9156SMichael S. Tsirkin              * For now root complex event collector isn't supported.
403d33d9156SMichael S. Tsirkin              */
404d33d9156SMichael S. Tsirkin             return;
405d33d9156SMichael S. Tsirkin         }
406d33d9156SMichael S. Tsirkin         dev = pci_bridge_get_device(dev->bus);
407247c97f3SMichael S. Tsirkin     }
40834e65944SIsaku Yamahata }
40934e65944SIsaku Yamahata 
41034e65944SIsaku Yamahata static void pcie_aer_update_log(PCIDevice *dev, const PCIEAERErr *err)
41134e65944SIsaku Yamahata {
41234e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
413786a4ea8SStefan Hajnoczi     uint8_t first_bit = ctz32(err->status);
41434e65944SIsaku Yamahata     uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
41534e65944SIsaku Yamahata     int i;
41634e65944SIsaku Yamahata 
41734e65944SIsaku Yamahata     assert(err->status);
41874d63b65SIsaku Yamahata     assert(!(err->status & (err->status - 1)));
41934e65944SIsaku Yamahata 
42034e65944SIsaku Yamahata     errcap &= ~(PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
42134e65944SIsaku Yamahata     errcap |= PCI_ERR_CAP_FEP(first_bit);
42234e65944SIsaku Yamahata 
42334e65944SIsaku Yamahata     if (err->flags & PCIE_AER_ERR_HEADER_VALID) {
42434e65944SIsaku Yamahata         for (i = 0; i < ARRAY_SIZE(err->header); ++i) {
42534e65944SIsaku Yamahata             /* 7.10.8 Header Log Register */
42634e65944SIsaku Yamahata             uint8_t *header_log =
42734e65944SIsaku Yamahata                 aer_cap + PCI_ERR_HEADER_LOG + i * sizeof err->header[0];
4286bd194abSPeter Maydell             stl_be_p(header_log, err->header[i]);
42934e65944SIsaku Yamahata         }
43034e65944SIsaku Yamahata     } else {
43134e65944SIsaku Yamahata         assert(!(err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT));
43234e65944SIsaku Yamahata         memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
43334e65944SIsaku Yamahata     }
43434e65944SIsaku Yamahata 
43534e65944SIsaku Yamahata     if ((err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT) &&
43677a3c1d7SChen Fan         (pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP2) &
43734e65944SIsaku Yamahata          PCI_EXP_DEVCAP2_EETLPP)) {
43834e65944SIsaku Yamahata         for (i = 0; i < ARRAY_SIZE(err->prefix); ++i) {
43934e65944SIsaku Yamahata             /* 7.10.12 tlp prefix log register */
44034e65944SIsaku Yamahata             uint8_t *prefix_log =
44134e65944SIsaku Yamahata                 aer_cap + PCI_ERR_TLP_PREFIX_LOG + i * sizeof err->prefix[0];
4426bd194abSPeter Maydell             stl_be_p(prefix_log, err->prefix[i]);
44334e65944SIsaku Yamahata         }
44434e65944SIsaku Yamahata         errcap |= PCI_ERR_CAP_TLP;
44534e65944SIsaku Yamahata     } else {
44634e65944SIsaku Yamahata         memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0,
44734e65944SIsaku Yamahata                PCI_ERR_TLP_PREFIX_LOG_SIZE);
44834e65944SIsaku Yamahata     }
44934e65944SIsaku Yamahata     pci_set_long(aer_cap + PCI_ERR_CAP, errcap);
45034e65944SIsaku Yamahata }
45134e65944SIsaku Yamahata 
45234e65944SIsaku Yamahata static void pcie_aer_clear_log(PCIDevice *dev)
45334e65944SIsaku Yamahata {
45434e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
45534e65944SIsaku Yamahata 
45634e65944SIsaku Yamahata     pci_long_test_and_clear_mask(aer_cap + PCI_ERR_CAP,
45734e65944SIsaku Yamahata                                  PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
45834e65944SIsaku Yamahata 
45934e65944SIsaku Yamahata     memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
46034e65944SIsaku Yamahata     memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0, PCI_ERR_TLP_PREFIX_LOG_SIZE);
46134e65944SIsaku Yamahata }
46234e65944SIsaku Yamahata 
46334e65944SIsaku Yamahata static void pcie_aer_clear_error(PCIDevice *dev)
46434e65944SIsaku Yamahata {
46534e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
46634e65944SIsaku Yamahata     uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
46734e65944SIsaku Yamahata     PCIEAERLog *aer_log = &dev->exp.aer_log;
46834e65944SIsaku Yamahata     PCIEAERErr err;
46934e65944SIsaku Yamahata 
47034e65944SIsaku Yamahata     if (!(errcap & PCI_ERR_CAP_MHRE) || !aer_log->log_num) {
47134e65944SIsaku Yamahata         pcie_aer_clear_log(dev);
47234e65944SIsaku Yamahata         return;
47334e65944SIsaku Yamahata     }
47434e65944SIsaku Yamahata 
47534e65944SIsaku Yamahata     /*
47634e65944SIsaku Yamahata      * If more errors are queued, set corresponding bits in uncorrectable
47734e65944SIsaku Yamahata      * error status.
47834e65944SIsaku Yamahata      * We emulate uncorrectable error status register as W1CS.
47934e65944SIsaku Yamahata      * So set bit in uncorrectable error status here again for multiple
48034e65944SIsaku Yamahata      * error recording support.
48134e65944SIsaku Yamahata      *
48234e65944SIsaku Yamahata      * 6.2.4.2 Multiple Error Handling(Advanced Error Reporting Capability)
48334e65944SIsaku Yamahata      */
48434e65944SIsaku Yamahata     pcie_aer_update_uncor_status(dev);
48534e65944SIsaku Yamahata 
48634e65944SIsaku Yamahata     aer_log_del_err(aer_log, &err);
48734e65944SIsaku Yamahata     pcie_aer_update_log(dev, &err);
48834e65944SIsaku Yamahata }
48934e65944SIsaku Yamahata 
49034e65944SIsaku Yamahata static int pcie_aer_record_error(PCIDevice *dev,
49134e65944SIsaku Yamahata                                  const PCIEAERErr *err)
49234e65944SIsaku Yamahata {
49334e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
49434e65944SIsaku Yamahata     uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
49534e65944SIsaku Yamahata     int fep = PCI_ERR_CAP_FEP(errcap);
49634e65944SIsaku Yamahata 
49734e65944SIsaku Yamahata     assert(err->status);
49874d63b65SIsaku Yamahata     assert(!(err->status & (err->status - 1)));
49934e65944SIsaku Yamahata 
50034e65944SIsaku Yamahata     if (errcap & PCI_ERR_CAP_MHRE &&
50134e65944SIsaku Yamahata         (pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS) & (1U << fep))) {
50234e65944SIsaku Yamahata         /*  Not first error. queue error */
50334e65944SIsaku Yamahata         if (aer_log_add_err(&dev->exp.aer_log, err) < 0) {
50434e65944SIsaku Yamahata             /* overflow */
50534e65944SIsaku Yamahata             return -1;
50634e65944SIsaku Yamahata         }
50734e65944SIsaku Yamahata         return 0;
50834e65944SIsaku Yamahata     }
50934e65944SIsaku Yamahata 
51034e65944SIsaku Yamahata     pcie_aer_update_log(dev, err);
51134e65944SIsaku Yamahata     return 0;
51234e65944SIsaku Yamahata }
51334e65944SIsaku Yamahata 
51434e65944SIsaku Yamahata typedef struct PCIEAERInject {
51534e65944SIsaku Yamahata     PCIDevice *dev;
51634e65944SIsaku Yamahata     uint8_t *aer_cap;
51734e65944SIsaku Yamahata     const PCIEAERErr *err;
51834e65944SIsaku Yamahata     uint16_t devctl;
51934e65944SIsaku Yamahata     uint16_t devsta;
52034e65944SIsaku Yamahata     uint32_t error_status;
52134e65944SIsaku Yamahata     bool unsupported_request;
52234e65944SIsaku Yamahata     bool log_overflow;
52334e65944SIsaku Yamahata     PCIEAERMsg msg;
52434e65944SIsaku Yamahata } PCIEAERInject;
52534e65944SIsaku Yamahata 
52634e65944SIsaku Yamahata static bool pcie_aer_inject_cor_error(PCIEAERInject *inj,
52734e65944SIsaku Yamahata                                       uint32_t uncor_status,
52834e65944SIsaku Yamahata                                       bool is_advisory_nonfatal)
52934e65944SIsaku Yamahata {
53034e65944SIsaku Yamahata     PCIDevice *dev = inj->dev;
53134e65944SIsaku Yamahata 
53234e65944SIsaku Yamahata     inj->devsta |= PCI_EXP_DEVSTA_CED;
53334e65944SIsaku Yamahata     if (inj->unsupported_request) {
53434e65944SIsaku Yamahata         inj->devsta |= PCI_EXP_DEVSTA_URD;
53534e65944SIsaku Yamahata     }
53634e65944SIsaku Yamahata     pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
53734e65944SIsaku Yamahata 
53834e65944SIsaku Yamahata     if (inj->aer_cap) {
53934e65944SIsaku Yamahata         uint32_t mask;
54034e65944SIsaku Yamahata         pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_COR_STATUS,
54134e65944SIsaku Yamahata                                    inj->error_status);
54234e65944SIsaku Yamahata         mask = pci_get_long(inj->aer_cap + PCI_ERR_COR_MASK);
54334e65944SIsaku Yamahata         if (mask & inj->error_status) {
54434e65944SIsaku Yamahata             return false;
54534e65944SIsaku Yamahata         }
54634e65944SIsaku Yamahata         if (is_advisory_nonfatal) {
54734e65944SIsaku Yamahata             uint32_t uncor_mask =
54834e65944SIsaku Yamahata                 pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
54934e65944SIsaku Yamahata             if (!(uncor_mask & uncor_status)) {
55034e65944SIsaku Yamahata                 inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
55134e65944SIsaku Yamahata             }
55234e65944SIsaku Yamahata             pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
55334e65944SIsaku Yamahata                                        uncor_status);
55434e65944SIsaku Yamahata         }
55534e65944SIsaku Yamahata     }
55634e65944SIsaku Yamahata 
55734e65944SIsaku Yamahata     if (inj->unsupported_request && !(inj->devctl & PCI_EXP_DEVCTL_URRE)) {
55834e65944SIsaku Yamahata         return false;
55934e65944SIsaku Yamahata     }
56034e65944SIsaku Yamahata     if (!(inj->devctl & PCI_EXP_DEVCTL_CERE)) {
56134e65944SIsaku Yamahata         return false;
56234e65944SIsaku Yamahata     }
56334e65944SIsaku Yamahata 
56434e65944SIsaku Yamahata     inj->msg.severity = PCI_ERR_ROOT_CMD_COR_EN;
56534e65944SIsaku Yamahata     return true;
56634e65944SIsaku Yamahata }
56734e65944SIsaku Yamahata 
56834e65944SIsaku Yamahata static bool pcie_aer_inject_uncor_error(PCIEAERInject *inj, bool is_fatal)
56934e65944SIsaku Yamahata {
57034e65944SIsaku Yamahata     PCIDevice *dev = inj->dev;
57134e65944SIsaku Yamahata     uint16_t cmd;
57234e65944SIsaku Yamahata 
57334e65944SIsaku Yamahata     if (is_fatal) {
57434e65944SIsaku Yamahata         inj->devsta |= PCI_EXP_DEVSTA_FED;
57534e65944SIsaku Yamahata     } else {
57634e65944SIsaku Yamahata         inj->devsta |= PCI_EXP_DEVSTA_NFED;
57734e65944SIsaku Yamahata     }
57834e65944SIsaku Yamahata     if (inj->unsupported_request) {
57934e65944SIsaku Yamahata         inj->devsta |= PCI_EXP_DEVSTA_URD;
58034e65944SIsaku Yamahata     }
58134e65944SIsaku Yamahata     pci_set_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
58234e65944SIsaku Yamahata 
58334e65944SIsaku Yamahata     if (inj->aer_cap) {
58434e65944SIsaku Yamahata         uint32_t mask = pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
58534e65944SIsaku Yamahata         if (mask & inj->error_status) {
58634e65944SIsaku Yamahata             pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
58734e65944SIsaku Yamahata                                        inj->error_status);
58834e65944SIsaku Yamahata             return false;
58934e65944SIsaku Yamahata         }
59034e65944SIsaku Yamahata 
59134e65944SIsaku Yamahata         inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
59234e65944SIsaku Yamahata         pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
59334e65944SIsaku Yamahata                                    inj->error_status);
59434e65944SIsaku Yamahata     }
59534e65944SIsaku Yamahata 
59634e65944SIsaku Yamahata     cmd = pci_get_word(dev->config + PCI_COMMAND);
59734e65944SIsaku Yamahata     if (inj->unsupported_request &&
59834e65944SIsaku Yamahata         !(inj->devctl & PCI_EXP_DEVCTL_URRE) && !(cmd & PCI_COMMAND_SERR)) {
59934e65944SIsaku Yamahata         return false;
60034e65944SIsaku Yamahata     }
60134e65944SIsaku Yamahata     if (is_fatal) {
60234e65944SIsaku Yamahata         if (!((cmd & PCI_COMMAND_SERR) ||
60334e65944SIsaku Yamahata               (inj->devctl & PCI_EXP_DEVCTL_FERE))) {
60434e65944SIsaku Yamahata             return false;
60534e65944SIsaku Yamahata         }
60634e65944SIsaku Yamahata         inj->msg.severity = PCI_ERR_ROOT_CMD_FATAL_EN;
60734e65944SIsaku Yamahata     } else {
60834e65944SIsaku Yamahata         if (!((cmd & PCI_COMMAND_SERR) ||
60934e65944SIsaku Yamahata               (inj->devctl & PCI_EXP_DEVCTL_NFERE))) {
61034e65944SIsaku Yamahata             return false;
61134e65944SIsaku Yamahata         }
61234e65944SIsaku Yamahata         inj->msg.severity = PCI_ERR_ROOT_CMD_NONFATAL_EN;
61334e65944SIsaku Yamahata     }
61434e65944SIsaku Yamahata     return true;
61534e65944SIsaku Yamahata }
61634e65944SIsaku Yamahata 
61734e65944SIsaku Yamahata /*
61834e65944SIsaku Yamahata  * non-Function specific error must be recorded in all functions.
61934e65944SIsaku Yamahata  * It is the responsibility of the caller of this function.
620e8e3bb2fSStefan Weil  * It is also caller's responsibility to determine which function should
621b01738c2SChen Fan  * report the error.
62234e65944SIsaku Yamahata  *
62334e65944SIsaku Yamahata  * 6.2.4 Error Logging
624b01738c2SChen Fan  * 6.2.5 Sequence of Device Error Signaling and Logging Operations
625ce394947SMichael S. Tsirkin  * Figure 6-2: Flowchart Showing Sequence of Device Error Signaling and Logging
62634e65944SIsaku Yamahata  *             Operations
62734e65944SIsaku Yamahata  */
62834e65944SIsaku Yamahata int pcie_aer_inject_error(PCIDevice *dev, const PCIEAERErr *err)
62934e65944SIsaku Yamahata {
63034e65944SIsaku Yamahata     uint8_t *aer_cap = NULL;
63134e65944SIsaku Yamahata     uint16_t devctl = 0;
63234e65944SIsaku Yamahata     uint16_t devsta = 0;
63334e65944SIsaku Yamahata     uint32_t error_status = err->status;
63434e65944SIsaku Yamahata     PCIEAERInject inj;
63534e65944SIsaku Yamahata 
63634e65944SIsaku Yamahata     if (!pci_is_express(dev)) {
63734e65944SIsaku Yamahata         return -ENOSYS;
63834e65944SIsaku Yamahata     }
63934e65944SIsaku Yamahata 
64034e65944SIsaku Yamahata     if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
64134e65944SIsaku Yamahata         error_status &= PCI_ERR_COR_SUPPORTED;
64234e65944SIsaku Yamahata     } else {
64334e65944SIsaku Yamahata         error_status &= PCI_ERR_UNC_SUPPORTED;
64434e65944SIsaku Yamahata     }
64534e65944SIsaku Yamahata 
64634e65944SIsaku Yamahata     /* invalid status bit. one and only one bit must be set */
64734e65944SIsaku Yamahata     if (!error_status || (error_status & (error_status - 1))) {
64834e65944SIsaku Yamahata         return -EINVAL;
64934e65944SIsaku Yamahata     }
65034e65944SIsaku Yamahata 
65134e65944SIsaku Yamahata     if (dev->exp.aer_cap) {
65234e65944SIsaku Yamahata         uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
65334e65944SIsaku Yamahata         aer_cap = dev->config + dev->exp.aer_cap;
65434e65944SIsaku Yamahata         devctl = pci_get_long(exp_cap + PCI_EXP_DEVCTL);
65534e65944SIsaku Yamahata         devsta = pci_get_long(exp_cap + PCI_EXP_DEVSTA);
65634e65944SIsaku Yamahata     }
65734e65944SIsaku Yamahata 
65834e65944SIsaku Yamahata     inj.dev = dev;
65934e65944SIsaku Yamahata     inj.aer_cap = aer_cap;
66034e65944SIsaku Yamahata     inj.err = err;
66134e65944SIsaku Yamahata     inj.devctl = devctl;
66234e65944SIsaku Yamahata     inj.devsta = devsta;
66334e65944SIsaku Yamahata     inj.error_status = error_status;
66434e65944SIsaku Yamahata     inj.unsupported_request = !(err->flags & PCIE_AER_ERR_IS_CORRECTABLE) &&
66534e65944SIsaku Yamahata         err->status == PCI_ERR_UNC_UNSUP;
66634e65944SIsaku Yamahata     inj.log_overflow = false;
66734e65944SIsaku Yamahata 
66834e65944SIsaku Yamahata     if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
66934e65944SIsaku Yamahata         if (!pcie_aer_inject_cor_error(&inj, 0, false)) {
67034e65944SIsaku Yamahata             return 0;
67134e65944SIsaku Yamahata         }
67234e65944SIsaku Yamahata     } else {
67334e65944SIsaku Yamahata         bool is_fatal =
67434e65944SIsaku Yamahata             pcie_aer_uncor_default_severity(error_status) ==
67534e65944SIsaku Yamahata             PCI_ERR_ROOT_CMD_FATAL_EN;
67634e65944SIsaku Yamahata         if (aer_cap) {
67734e65944SIsaku Yamahata             is_fatal =
67834e65944SIsaku Yamahata                 error_status & pci_get_long(aer_cap + PCI_ERR_UNCOR_SEVER);
67934e65944SIsaku Yamahata         }
68034e65944SIsaku Yamahata         if (!is_fatal && (err->flags & PCIE_AER_ERR_MAYBE_ADVISORY)) {
68134e65944SIsaku Yamahata             inj.error_status = PCI_ERR_COR_ADV_NONFATAL;
68234e65944SIsaku Yamahata             if (!pcie_aer_inject_cor_error(&inj, error_status, true)) {
68334e65944SIsaku Yamahata                 return 0;
68434e65944SIsaku Yamahata             }
68534e65944SIsaku Yamahata         } else {
68634e65944SIsaku Yamahata             if (!pcie_aer_inject_uncor_error(&inj, is_fatal)) {
68734e65944SIsaku Yamahata                 return 0;
68834e65944SIsaku Yamahata             }
68934e65944SIsaku Yamahata         }
69034e65944SIsaku Yamahata     }
69134e65944SIsaku Yamahata 
69234e65944SIsaku Yamahata     /* send up error message */
69334e65944SIsaku Yamahata     inj.msg.source_id = err->source_id;
69434e65944SIsaku Yamahata     pcie_aer_msg(dev, &inj.msg);
69534e65944SIsaku Yamahata 
69634e65944SIsaku Yamahata     if (inj.log_overflow) {
69734e65944SIsaku Yamahata         PCIEAERErr header_log_overflow = {
69834e65944SIsaku Yamahata             .status = PCI_ERR_COR_HL_OVERFLOW,
69934e65944SIsaku Yamahata             .flags = PCIE_AER_ERR_IS_CORRECTABLE,
70034e65944SIsaku Yamahata         };
70134e65944SIsaku Yamahata         int ret = pcie_aer_inject_error(dev, &header_log_overflow);
70234e65944SIsaku Yamahata         assert(!ret);
70334e65944SIsaku Yamahata     }
70434e65944SIsaku Yamahata     return 0;
70534e65944SIsaku Yamahata }
70634e65944SIsaku Yamahata 
70734e65944SIsaku Yamahata void pcie_aer_write_config(PCIDevice *dev,
70834e65944SIsaku Yamahata                            uint32_t addr, uint32_t val, int len)
70934e65944SIsaku Yamahata {
71034e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
71134e65944SIsaku Yamahata     uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
71234e65944SIsaku Yamahata     uint32_t first_error = 1U << PCI_ERR_CAP_FEP(errcap);
71334e65944SIsaku Yamahata     uint32_t uncorsta = pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS);
71434e65944SIsaku Yamahata 
71534e65944SIsaku Yamahata     /* uncorrectable error */
71634e65944SIsaku Yamahata     if (!(uncorsta & first_error)) {
71734e65944SIsaku Yamahata         /* the bit that corresponds to the first error is cleared */
71834e65944SIsaku Yamahata         pcie_aer_clear_error(dev);
71934e65944SIsaku Yamahata     } else if (errcap & PCI_ERR_CAP_MHRE) {
72034e65944SIsaku Yamahata         /* When PCI_ERR_CAP_MHRE is enabled and the first error isn't cleared
72134e65944SIsaku Yamahata          * nothing should happen. So we have to revert the modification to
72234e65944SIsaku Yamahata          * the register.
72334e65944SIsaku Yamahata          */
72434e65944SIsaku Yamahata         pcie_aer_update_uncor_status(dev);
72534e65944SIsaku Yamahata     } else {
72634e65944SIsaku Yamahata         /* capability & control
72734e65944SIsaku Yamahata          * PCI_ERR_CAP_MHRE might be cleared, so clear of header log.
72834e65944SIsaku Yamahata          */
72934e65944SIsaku Yamahata         aer_log_clear_all_err(&dev->exp.aer_log);
73034e65944SIsaku Yamahata     }
73134e65944SIsaku Yamahata }
73234e65944SIsaku Yamahata 
73334e65944SIsaku Yamahata void pcie_aer_root_init(PCIDevice *dev)
73434e65944SIsaku Yamahata {
73534e65944SIsaku Yamahata     uint16_t pos = dev->exp.aer_cap;
73634e65944SIsaku Yamahata 
73734e65944SIsaku Yamahata     pci_set_long(dev->wmask + pos + PCI_ERR_ROOT_COMMAND,
73834e65944SIsaku Yamahata                  PCI_ERR_ROOT_CMD_EN_MASK);
73934e65944SIsaku Yamahata     pci_set_long(dev->w1cmask + pos + PCI_ERR_ROOT_STATUS,
74034e65944SIsaku Yamahata                  PCI_ERR_ROOT_STATUS_REPORT_MASK);
7410e180d9cSJason Baron     /* PCI_ERR_ROOT_IRQ is RO but devices change it using a
7420e180d9cSJason Baron      * device-specific method.
7430e180d9cSJason Baron      */
7440e180d9cSJason Baron     pci_set_long(dev->cmask + pos + PCI_ERR_ROOT_STATUS,
7450e180d9cSJason Baron                  ~PCI_ERR_ROOT_IRQ);
74634e65944SIsaku Yamahata }
74734e65944SIsaku Yamahata 
74834e65944SIsaku Yamahata void pcie_aer_root_reset(PCIDevice *dev)
74934e65944SIsaku Yamahata {
75034e65944SIsaku Yamahata     uint8_t* aer_cap = dev->config + dev->exp.aer_cap;
75134e65944SIsaku Yamahata 
75234e65944SIsaku Yamahata     pci_set_long(aer_cap + PCI_ERR_ROOT_COMMAND, 0);
75334e65944SIsaku Yamahata 
75434e65944SIsaku Yamahata     /*
75534e65944SIsaku Yamahata      * Advanced Error Interrupt Message Number in Root Error Status Register
75634e65944SIsaku Yamahata      * must be updated by chip dependent code because it's chip dependent
75734e65944SIsaku Yamahata      * which number is used.
75834e65944SIsaku Yamahata      */
75934e65944SIsaku Yamahata }
76034e65944SIsaku Yamahata 
76134e65944SIsaku Yamahata void pcie_aer_root_write_config(PCIDevice *dev,
76234e65944SIsaku Yamahata                                 uint32_t addr, uint32_t val, int len,
76334e65944SIsaku Yamahata                                 uint32_t root_cmd_prev)
76434e65944SIsaku Yamahata {
76534e65944SIsaku Yamahata     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
76634e65944SIsaku Yamahata     uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
7672b3cb353SMichael S. Tsirkin     uint32_t enabled_cmd = pcie_aer_status_to_cmd(root_status);
7682b3cb353SMichael S. Tsirkin     uint32_t root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
7692b3cb353SMichael S. Tsirkin     /* 6.2.4.1.2 Interrupt Generation */
7702b3cb353SMichael S. Tsirkin     if (!msix_enabled(dev) && !msi_enabled(dev)) {
7715a03e708SMarcel Apfelbaum         pci_set_irq(dev, !!(root_cmd & enabled_cmd));
7722b3cb353SMichael S. Tsirkin         return;
7732b3cb353SMichael S. Tsirkin     }
7742b3cb353SMichael S. Tsirkin 
7752b3cb353SMichael S. Tsirkin     if ((root_cmd_prev & enabled_cmd) || !(root_cmd & enabled_cmd)) {
7762b3cb353SMichael S. Tsirkin         /* Send MSI on transition from false to true. */
7772b3cb353SMichael S. Tsirkin         return;
7782b3cb353SMichael S. Tsirkin     }
77934e65944SIsaku Yamahata 
780513691b7SMichael S. Tsirkin     pcie_aer_root_notify(dev);
78134e65944SIsaku Yamahata }
78234e65944SIsaku Yamahata 
78334e65944SIsaku Yamahata static const VMStateDescription vmstate_pcie_aer_err = {
78434e65944SIsaku Yamahata     .name = "PCIE_AER_ERROR",
78534e65944SIsaku Yamahata     .version_id = 1,
78634e65944SIsaku Yamahata     .minimum_version_id = 1,
78734e65944SIsaku Yamahata     .fields = (VMStateField[]) {
78834e65944SIsaku Yamahata         VMSTATE_UINT32(status, PCIEAERErr),
78934e65944SIsaku Yamahata         VMSTATE_UINT16(source_id, PCIEAERErr),
79034e65944SIsaku Yamahata         VMSTATE_UINT16(flags, PCIEAERErr),
79134e65944SIsaku Yamahata         VMSTATE_UINT32_ARRAY(header, PCIEAERErr, 4),
79234e65944SIsaku Yamahata         VMSTATE_UINT32_ARRAY(prefix, PCIEAERErr, 4),
79334e65944SIsaku Yamahata         VMSTATE_END_OF_LIST()
79434e65944SIsaku Yamahata     }
79534e65944SIsaku Yamahata };
79634e65944SIsaku Yamahata 
7975f691ff9SMichael S. Tsirkin static bool pcie_aer_state_log_num_valid(void *opaque, int version_id)
7985f691ff9SMichael S. Tsirkin {
7995f691ff9SMichael S. Tsirkin     PCIEAERLog *s = opaque;
8005f691ff9SMichael S. Tsirkin 
8015f691ff9SMichael S. Tsirkin     return s->log_num <= s->log_max;
8025f691ff9SMichael S. Tsirkin }
8035f691ff9SMichael S. Tsirkin 
80434e65944SIsaku Yamahata const VMStateDescription vmstate_pcie_aer_log = {
80534e65944SIsaku Yamahata     .name = "PCIE_AER_ERROR_LOG",
80634e65944SIsaku Yamahata     .version_id = 1,
80734e65944SIsaku Yamahata     .minimum_version_id = 1,
80834e65944SIsaku Yamahata     .fields = (VMStateField[]) {
80934e65944SIsaku Yamahata         VMSTATE_UINT16(log_num, PCIEAERLog),
8105f691ff9SMichael S. Tsirkin         VMSTATE_UINT16_EQUAL(log_max, PCIEAERLog),
8115f691ff9SMichael S. Tsirkin         VMSTATE_VALIDATE("log_num <= log_max", pcie_aer_state_log_num_valid),
81247188700SDmitry Eremin-Solenikov         VMSTATE_STRUCT_VARRAY_POINTER_UINT16(log, PCIEAERLog, log_num,
81334e65944SIsaku Yamahata                               vmstate_pcie_aer_err, PCIEAERErr),
81434e65944SIsaku Yamahata         VMSTATE_END_OF_LIST()
81534e65944SIsaku Yamahata     }
81634e65944SIsaku Yamahata };
8172ae63bdaSIsaku Yamahata 
8182ae63bdaSIsaku Yamahata typedef struct PCIEAERErrorName {
8192ae63bdaSIsaku Yamahata     const char *name;
8202ae63bdaSIsaku Yamahata     uint32_t val;
8212ae63bdaSIsaku Yamahata     bool correctable;
8222ae63bdaSIsaku Yamahata } PCIEAERErrorName;
8232ae63bdaSIsaku Yamahata 
8242ae63bdaSIsaku Yamahata /*
82566a0a2cbSDong Xu Wang  * AER error name -> value conversion table
8262ae63bdaSIsaku Yamahata  * This naming scheme is same to linux aer-injection tool.
8272ae63bdaSIsaku Yamahata  */
8282ae63bdaSIsaku Yamahata static const struct PCIEAERErrorName pcie_aer_error_list[] = {
8292ae63bdaSIsaku Yamahata     {
8302ae63bdaSIsaku Yamahata         .name = "DLP",
8312ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_DLP,
8322ae63bdaSIsaku Yamahata         .correctable = false,
8332ae63bdaSIsaku Yamahata     }, {
8342ae63bdaSIsaku Yamahata         .name = "SDN",
8352ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_SDN,
8362ae63bdaSIsaku Yamahata         .correctable = false,
8372ae63bdaSIsaku Yamahata     }, {
8382ae63bdaSIsaku Yamahata         .name = "POISON_TLP",
8392ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_POISON_TLP,
8402ae63bdaSIsaku Yamahata         .correctable = false,
8412ae63bdaSIsaku Yamahata     }, {
8422ae63bdaSIsaku Yamahata         .name = "FCP",
8432ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_FCP,
8442ae63bdaSIsaku Yamahata         .correctable = false,
8452ae63bdaSIsaku Yamahata     }, {
8462ae63bdaSIsaku Yamahata         .name = "COMP_TIME",
8472ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_COMP_TIME,
8482ae63bdaSIsaku Yamahata         .correctable = false,
8492ae63bdaSIsaku Yamahata     }, {
8502ae63bdaSIsaku Yamahata         .name = "COMP_ABORT",
8512ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_COMP_ABORT,
8522ae63bdaSIsaku Yamahata         .correctable = false,
8532ae63bdaSIsaku Yamahata     }, {
8542ae63bdaSIsaku Yamahata         .name = "UNX_COMP",
8552ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_UNX_COMP,
8562ae63bdaSIsaku Yamahata         .correctable = false,
8572ae63bdaSIsaku Yamahata     }, {
8582ae63bdaSIsaku Yamahata         .name = "RX_OVER",
8592ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_RX_OVER,
8602ae63bdaSIsaku Yamahata         .correctable = false,
8612ae63bdaSIsaku Yamahata     }, {
8622ae63bdaSIsaku Yamahata         .name = "MALF_TLP",
8632ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_MALF_TLP,
8642ae63bdaSIsaku Yamahata         .correctable = false,
8652ae63bdaSIsaku Yamahata     }, {
8662ae63bdaSIsaku Yamahata         .name = "ECRC",
8672ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_ECRC,
8682ae63bdaSIsaku Yamahata         .correctable = false,
8692ae63bdaSIsaku Yamahata     }, {
8702ae63bdaSIsaku Yamahata         .name = "UNSUP",
8712ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_UNSUP,
8722ae63bdaSIsaku Yamahata         .correctable = false,
8732ae63bdaSIsaku Yamahata     }, {
8742ae63bdaSIsaku Yamahata         .name = "ACSV",
8752ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_ACSV,
8762ae63bdaSIsaku Yamahata         .correctable = false,
8772ae63bdaSIsaku Yamahata     }, {
8782ae63bdaSIsaku Yamahata         .name = "INTN",
8792ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_INTN,
8802ae63bdaSIsaku Yamahata         .correctable = false,
8812ae63bdaSIsaku Yamahata     }, {
8822ae63bdaSIsaku Yamahata         .name = "MCBTLP",
8832ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_MCBTLP,
8842ae63bdaSIsaku Yamahata         .correctable = false,
8852ae63bdaSIsaku Yamahata     }, {
8862ae63bdaSIsaku Yamahata         .name = "ATOP_EBLOCKED",
8872ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_ATOP_EBLOCKED,
8882ae63bdaSIsaku Yamahata         .correctable = false,
8892ae63bdaSIsaku Yamahata     }, {
8902ae63bdaSIsaku Yamahata         .name = "TLP_PRF_BLOCKED",
8912ae63bdaSIsaku Yamahata         .val = PCI_ERR_UNC_TLP_PRF_BLOCKED,
8922ae63bdaSIsaku Yamahata         .correctable = false,
8932ae63bdaSIsaku Yamahata     }, {
8942ae63bdaSIsaku Yamahata         .name = "RCVR",
8952ae63bdaSIsaku Yamahata         .val = PCI_ERR_COR_RCVR,
8962ae63bdaSIsaku Yamahata         .correctable = true,
8972ae63bdaSIsaku Yamahata     }, {
8982ae63bdaSIsaku Yamahata         .name = "BAD_TLP",
8992ae63bdaSIsaku Yamahata         .val = PCI_ERR_COR_BAD_TLP,
9002ae63bdaSIsaku Yamahata         .correctable = true,
9012ae63bdaSIsaku Yamahata     }, {
9022ae63bdaSIsaku Yamahata         .name = "BAD_DLLP",
9032ae63bdaSIsaku Yamahata         .val = PCI_ERR_COR_BAD_DLLP,
9042ae63bdaSIsaku Yamahata         .correctable = true,
9052ae63bdaSIsaku Yamahata     }, {
9062ae63bdaSIsaku Yamahata         .name = "REP_ROLL",
9072ae63bdaSIsaku Yamahata         .val = PCI_ERR_COR_REP_ROLL,
9082ae63bdaSIsaku Yamahata         .correctable = true,
9092ae63bdaSIsaku Yamahata     }, {
9102ae63bdaSIsaku Yamahata         .name = "REP_TIMER",
9112ae63bdaSIsaku Yamahata         .val = PCI_ERR_COR_REP_TIMER,
9122ae63bdaSIsaku Yamahata         .correctable = true,
9132ae63bdaSIsaku Yamahata     }, {
9142ae63bdaSIsaku Yamahata         .name = "ADV_NONFATAL",
9152ae63bdaSIsaku Yamahata         .val = PCI_ERR_COR_ADV_NONFATAL,
9162ae63bdaSIsaku Yamahata         .correctable = true,
9172ae63bdaSIsaku Yamahata     }, {
9182ae63bdaSIsaku Yamahata         .name = "INTERNAL",
9192ae63bdaSIsaku Yamahata         .val = PCI_ERR_COR_INTERNAL,
9202ae63bdaSIsaku Yamahata         .correctable = true,
9212ae63bdaSIsaku Yamahata     }, {
9222ae63bdaSIsaku Yamahata         .name = "HL_OVERFLOW",
9232ae63bdaSIsaku Yamahata         .val = PCI_ERR_COR_HL_OVERFLOW,
9242ae63bdaSIsaku Yamahata         .correctable = true,
9252ae63bdaSIsaku Yamahata     },
9262ae63bdaSIsaku Yamahata };
9272ae63bdaSIsaku Yamahata 
9282ae63bdaSIsaku Yamahata static int pcie_aer_parse_error_string(const char *error_name,
9292ae63bdaSIsaku Yamahata                                        uint32_t *status, bool *correctable)
9302ae63bdaSIsaku Yamahata {
9312ae63bdaSIsaku Yamahata     int i;
9322ae63bdaSIsaku Yamahata 
9332ae63bdaSIsaku Yamahata     for (i = 0; i < ARRAY_SIZE(pcie_aer_error_list); i++) {
9342ae63bdaSIsaku Yamahata         const  PCIEAERErrorName *e = &pcie_aer_error_list[i];
9352ae63bdaSIsaku Yamahata         if (strcmp(error_name, e->name)) {
9362ae63bdaSIsaku Yamahata             continue;
9372ae63bdaSIsaku Yamahata         }
9382ae63bdaSIsaku Yamahata 
9392ae63bdaSIsaku Yamahata         *status = e->val;
9402ae63bdaSIsaku Yamahata         *correctable = e->correctable;
9412ae63bdaSIsaku Yamahata         return 0;
9422ae63bdaSIsaku Yamahata     }
9432ae63bdaSIsaku Yamahata     return -EINVAL;
9442ae63bdaSIsaku Yamahata }
9452ae63bdaSIsaku Yamahata 
94604e00c92SMarkus Armbruster static int do_pcie_aer_inject_error(Monitor *mon,
9472ae63bdaSIsaku Yamahata                                     const QDict *qdict, QObject **ret_data)
9482ae63bdaSIsaku Yamahata {
9492ae63bdaSIsaku Yamahata     const char *id = qdict_get_str(qdict, "id");
9502ae63bdaSIsaku Yamahata     const char *error_name;
9512ae63bdaSIsaku Yamahata     uint32_t error_status;
9522ae63bdaSIsaku Yamahata     bool correctable;
9532ae63bdaSIsaku Yamahata     PCIDevice *dev;
9542ae63bdaSIsaku Yamahata     PCIEAERErr err;
9552ae63bdaSIsaku Yamahata     int ret;
9562ae63bdaSIsaku Yamahata 
9572ae63bdaSIsaku Yamahata     ret = pci_qdev_find_device(id, &dev);
9582ae63bdaSIsaku Yamahata     if (ret < 0) {
9592ae63bdaSIsaku Yamahata         monitor_printf(mon,
9602ae63bdaSIsaku Yamahata                        "id or pci device path is invalid or device not "
9612ae63bdaSIsaku Yamahata                        "found. %s\n", id);
9622ae63bdaSIsaku Yamahata         return ret;
9632ae63bdaSIsaku Yamahata     }
9642ae63bdaSIsaku Yamahata     if (!pci_is_express(dev)) {
9652ae63bdaSIsaku Yamahata         monitor_printf(mon, "the device doesn't support pci express. %s\n",
9662ae63bdaSIsaku Yamahata                        id);
9672ae63bdaSIsaku Yamahata         return -ENOSYS;
9682ae63bdaSIsaku Yamahata     }
9692ae63bdaSIsaku Yamahata 
9702ae63bdaSIsaku Yamahata     error_name = qdict_get_str(qdict, "error_status");
9712ae63bdaSIsaku Yamahata     if (pcie_aer_parse_error_string(error_name, &error_status, &correctable)) {
9722ae63bdaSIsaku Yamahata         char *e = NULL;
9732ae63bdaSIsaku Yamahata         error_status = strtoul(error_name, &e, 0);
97434acbc95SEric Blake         correctable = qdict_get_try_bool(qdict, "correctable", false);
9752ae63bdaSIsaku Yamahata         if (!e || *e != '\0') {
9762ae63bdaSIsaku Yamahata             monitor_printf(mon, "invalid error status value. \"%s\"",
9772ae63bdaSIsaku Yamahata                            error_name);
9782ae63bdaSIsaku Yamahata             return -EINVAL;
9792ae63bdaSIsaku Yamahata         }
9802ae63bdaSIsaku Yamahata     }
98174d63b65SIsaku Yamahata     err.status = error_status;
982*a05f686fSPavel Fedin     err.source_id = pci_requester_id(dev);
9832ae63bdaSIsaku Yamahata 
9842ae63bdaSIsaku Yamahata     err.flags = 0;
9852ae63bdaSIsaku Yamahata     if (correctable) {
9862ae63bdaSIsaku Yamahata         err.flags |= PCIE_AER_ERR_IS_CORRECTABLE;
9872ae63bdaSIsaku Yamahata     }
98834acbc95SEric Blake     if (qdict_get_try_bool(qdict, "advisory_non_fatal", false)) {
9892ae63bdaSIsaku Yamahata         err.flags |= PCIE_AER_ERR_MAYBE_ADVISORY;
9902ae63bdaSIsaku Yamahata     }
9912ae63bdaSIsaku Yamahata     if (qdict_haskey(qdict, "header0")) {
9922ae63bdaSIsaku Yamahata         err.flags |= PCIE_AER_ERR_HEADER_VALID;
9932ae63bdaSIsaku Yamahata     }
9942ae63bdaSIsaku Yamahata     if (qdict_haskey(qdict, "prefix0")) {
9952ae63bdaSIsaku Yamahata         err.flags |= PCIE_AER_ERR_TLP_PREFIX_PRESENT;
9962ae63bdaSIsaku Yamahata     }
9972ae63bdaSIsaku Yamahata 
9982ae63bdaSIsaku Yamahata     err.header[0] = qdict_get_try_int(qdict, "header0", 0);
9992ae63bdaSIsaku Yamahata     err.header[1] = qdict_get_try_int(qdict, "header1", 0);
10002ae63bdaSIsaku Yamahata     err.header[2] = qdict_get_try_int(qdict, "header2", 0);
10012ae63bdaSIsaku Yamahata     err.header[3] = qdict_get_try_int(qdict, "header3", 0);
10022ae63bdaSIsaku Yamahata 
10032ae63bdaSIsaku Yamahata     err.prefix[0] = qdict_get_try_int(qdict, "prefix0", 0);
10042ae63bdaSIsaku Yamahata     err.prefix[1] = qdict_get_try_int(qdict, "prefix1", 0);
10052ae63bdaSIsaku Yamahata     err.prefix[2] = qdict_get_try_int(qdict, "prefix2", 0);
10062ae63bdaSIsaku Yamahata     err.prefix[3] = qdict_get_try_int(qdict, "prefix3", 0);
10072ae63bdaSIsaku Yamahata 
10082ae63bdaSIsaku Yamahata     ret = pcie_aer_inject_error(dev, &err);
10092ae63bdaSIsaku Yamahata     *ret_data = qobject_from_jsonf("{'id': %s, "
1010568f0690SDavid Gibson                                    "'root_bus': %s, 'bus': %d, 'devfn': %d, "
10112ae63bdaSIsaku Yamahata                                    "'ret': %d}",
1012568f0690SDavid Gibson                                    id, pci_root_bus_path(dev),
10132ae63bdaSIsaku Yamahata                                    pci_bus_num(dev->bus), dev->devfn,
10142ae63bdaSIsaku Yamahata                                    ret);
10152ae63bdaSIsaku Yamahata     assert(*ret_data);
10162ae63bdaSIsaku Yamahata 
10172ae63bdaSIsaku Yamahata     return 0;
10182ae63bdaSIsaku Yamahata }
101904e00c92SMarkus Armbruster 
102004e00c92SMarkus Armbruster void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
102104e00c92SMarkus Armbruster {
102204e00c92SMarkus Armbruster     QObject *data;
102304e00c92SMarkus Armbruster     int devfn;
102404e00c92SMarkus Armbruster 
102504e00c92SMarkus Armbruster     if (do_pcie_aer_inject_error(mon, qdict, &data) < 0) {
102604e00c92SMarkus Armbruster         return;
102704e00c92SMarkus Armbruster     }
102804e00c92SMarkus Armbruster 
102904e00c92SMarkus Armbruster     assert(qobject_type(data) == QTYPE_QDICT);
103004e00c92SMarkus Armbruster     qdict = qobject_to_qdict(data);
103104e00c92SMarkus Armbruster 
103204e00c92SMarkus Armbruster     devfn = (int)qdict_get_int(qdict, "devfn");
103304e00c92SMarkus Armbruster     monitor_printf(mon, "OK id: %s root bus: %s, bus: %x devfn: %x.%x\n",
103404e00c92SMarkus Armbruster                    qdict_get_str(qdict, "id"),
103504e00c92SMarkus Armbruster                    qdict_get_str(qdict, "root_bus"),
103604e00c92SMarkus Armbruster                    (int) qdict_get_int(qdict, "bus"),
103704e00c92SMarkus Armbruster                    PCI_SLOT(devfn), PCI_FUNC(devfn));
103804e00c92SMarkus Armbruster }
1039