xref: /openbmc/qemu/hw/nvme/nvme.h (revision 11871f53ef8ef8ff80ded133677230caf6261ac9)
188eea45cSKlaus Jensen /*
288eea45cSKlaus Jensen  * QEMU NVM Express
388eea45cSKlaus Jensen  *
488eea45cSKlaus Jensen  * Copyright (c) 2012 Intel Corporation
588eea45cSKlaus Jensen  * Copyright (c) 2021 Minwoo Im
688eea45cSKlaus Jensen  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
788eea45cSKlaus Jensen  *
888eea45cSKlaus Jensen  * Authors:
988eea45cSKlaus Jensen  *   Keith Busch            <kbusch@kernel.org>
1088eea45cSKlaus Jensen  *   Klaus Jensen           <k.jensen@samsung.com>
1188eea45cSKlaus Jensen  *   Gollu Appalanaidu      <anaidu.gollu@samsung.com>
1288eea45cSKlaus Jensen  *   Dmitry Fomichev        <dmitry.fomichev@wdc.com>
1388eea45cSKlaus Jensen  *   Minwoo Im              <minwoo.im.dev@gmail.com>
1488eea45cSKlaus Jensen  *
1588eea45cSKlaus Jensen  * This code is licensed under the GNU GPL v2 or later.
1688eea45cSKlaus Jensen  */
1788eea45cSKlaus Jensen 
1852581c71SMarkus Armbruster #ifndef HW_NVME_NVME_H
1952581c71SMarkus Armbruster #define HW_NVME_NVME_H
2088eea45cSKlaus Jensen 
2188eea45cSKlaus Jensen #include "qemu/uuid.h"
2288eea45cSKlaus Jensen #include "hw/pci/pci.h"
2388eea45cSKlaus Jensen #include "hw/block/block.h"
2488eea45cSKlaus Jensen 
2588eea45cSKlaus Jensen #include "block/nvme.h"
2688eea45cSKlaus Jensen 
2744c2c094SLukasz Maniak #define NVME_MAX_CONTROLLERS 256
2888eea45cSKlaus Jensen #define NVME_MAX_NAMESPACES  256
293276dde4SHeinrich Schuchardt #define NVME_EUI64_DEFAULT ((uint64_t)0x5254000000000000)
3088eea45cSKlaus Jensen 
3138f4ac65SKlaus Jensen QEMU_BUILD_BUG_ON(NVME_MAX_NAMESPACES > NVME_NSID_BROADCAST - 1);
3238f4ac65SKlaus Jensen 
3388eea45cSKlaus Jensen typedef struct NvmeCtrl NvmeCtrl;
3488eea45cSKlaus Jensen typedef struct NvmeNamespace NvmeNamespace;
3588eea45cSKlaus Jensen 
365ffbaeedSKlaus Jensen #define TYPE_NVME_BUS "nvme-bus"
375ffbaeedSKlaus Jensen OBJECT_DECLARE_SIMPLE_TYPE(NvmeBus, NVME_BUS)
385ffbaeedSKlaus Jensen 
395ffbaeedSKlaus Jensen typedef struct NvmeBus {
405ffbaeedSKlaus Jensen     BusState parent_bus;
415ffbaeedSKlaus Jensen } NvmeBus;
425ffbaeedSKlaus Jensen 
4388eea45cSKlaus Jensen #define TYPE_NVME_SUBSYS "nvme-subsys"
4488eea45cSKlaus Jensen #define NVME_SUBSYS(obj) \
4588eea45cSKlaus Jensen     OBJECT_CHECK(NvmeSubsystem, (obj), TYPE_NVME_SUBSYS)
4699f48ae7SLukasz Maniak #define SUBSYS_SLOT_RSVD (void *)0xFFFF
4788eea45cSKlaus Jensen 
4888eea45cSKlaus Jensen typedef struct NvmeSubsystem {
4988eea45cSKlaus Jensen     DeviceState parent_obj;
505ffbaeedSKlaus Jensen     NvmeBus     bus;
5188eea45cSKlaus Jensen     uint8_t     subnqn[256];
52a859eb9fSKlaus Jensen     char        *serial;
5388eea45cSKlaus Jensen 
5488eea45cSKlaus Jensen     NvmeCtrl      *ctrls[NVME_MAX_CONTROLLERS];
5588eea45cSKlaus Jensen     NvmeNamespace *namespaces[NVME_MAX_NAMESPACES + 1];
5688eea45cSKlaus Jensen 
5788eea45cSKlaus Jensen     struct {
5888eea45cSKlaus Jensen         char *nqn;
5988eea45cSKlaus Jensen     } params;
6088eea45cSKlaus Jensen } NvmeSubsystem;
6188eea45cSKlaus Jensen 
6288eea45cSKlaus Jensen int nvme_subsys_register_ctrl(NvmeCtrl *n, Error **errp);
63b0fde9e8SKlaus Jensen void nvme_subsys_unregister_ctrl(NvmeSubsystem *subsys, NvmeCtrl *n);
6488eea45cSKlaus Jensen 
6588eea45cSKlaus Jensen static inline NvmeCtrl *nvme_subsys_ctrl(NvmeSubsystem *subsys,
6688eea45cSKlaus Jensen                                          uint32_t cntlid)
6788eea45cSKlaus Jensen {
6888eea45cSKlaus Jensen     if (!subsys || cntlid >= NVME_MAX_CONTROLLERS) {
6988eea45cSKlaus Jensen         return NULL;
7088eea45cSKlaus Jensen     }
7188eea45cSKlaus Jensen 
7299f48ae7SLukasz Maniak     if (subsys->ctrls[cntlid] == SUBSYS_SLOT_RSVD) {
7399f48ae7SLukasz Maniak         return NULL;
7499f48ae7SLukasz Maniak     }
7599f48ae7SLukasz Maniak 
7688eea45cSKlaus Jensen     return subsys->ctrls[cntlid];
7788eea45cSKlaus Jensen }
7888eea45cSKlaus Jensen 
7988eea45cSKlaus Jensen static inline NvmeNamespace *nvme_subsys_ns(NvmeSubsystem *subsys,
8088eea45cSKlaus Jensen                                             uint32_t nsid)
8188eea45cSKlaus Jensen {
8288eea45cSKlaus Jensen     if (!subsys || !nsid || nsid > NVME_MAX_NAMESPACES) {
8388eea45cSKlaus Jensen         return NULL;
8488eea45cSKlaus Jensen     }
8588eea45cSKlaus Jensen 
8688eea45cSKlaus Jensen     return subsys->namespaces[nsid];
8788eea45cSKlaus Jensen }
8888eea45cSKlaus Jensen 
8988eea45cSKlaus Jensen #define TYPE_NVME_NS "nvme-ns"
9088eea45cSKlaus Jensen #define NVME_NS(obj) \
9188eea45cSKlaus Jensen     OBJECT_CHECK(NvmeNamespace, (obj), TYPE_NVME_NS)
9288eea45cSKlaus Jensen 
9388eea45cSKlaus Jensen typedef struct NvmeZone {
9488eea45cSKlaus Jensen     NvmeZoneDescr   d;
9588eea45cSKlaus Jensen     uint64_t        w_ptr;
9688eea45cSKlaus Jensen     QTAILQ_ENTRY(NvmeZone) entry;
9788eea45cSKlaus Jensen } NvmeZone;
9888eea45cSKlaus Jensen 
9988eea45cSKlaus Jensen typedef struct NvmeNamespaceParams {
10088eea45cSKlaus Jensen     bool     detached;
10188eea45cSKlaus Jensen     bool     shared;
10288eea45cSKlaus Jensen     uint32_t nsid;
10388eea45cSKlaus Jensen     QemuUUID uuid;
1046870cfb8SHeinrich Schuchardt     uint64_t eui64;
1053276dde4SHeinrich Schuchardt     bool     eui64_default;
10688eea45cSKlaus Jensen 
10788eea45cSKlaus Jensen     uint16_t ms;
10888eea45cSKlaus Jensen     uint8_t  mset;
10988eea45cSKlaus Jensen     uint8_t  pi;
11088eea45cSKlaus Jensen     uint8_t  pil;
11144219b60SNaveen Nagar     uint8_t  pif;
11288eea45cSKlaus Jensen 
11388eea45cSKlaus Jensen     uint16_t mssrl;
11488eea45cSKlaus Jensen     uint32_t mcl;
11588eea45cSKlaus Jensen     uint8_t  msrc;
11688eea45cSKlaus Jensen 
11788eea45cSKlaus Jensen     bool     zoned;
11888eea45cSKlaus Jensen     bool     cross_zone_read;
11988eea45cSKlaus Jensen     uint64_t zone_size_bs;
12088eea45cSKlaus Jensen     uint64_t zone_cap_bs;
12188eea45cSKlaus Jensen     uint32_t max_active_zones;
12288eea45cSKlaus Jensen     uint32_t max_open_zones;
12388eea45cSKlaus Jensen     uint32_t zd_extension_size;
124e321b4cdSKlaus Jensen 
125e321b4cdSKlaus Jensen     uint32_t numzrwa;
126e321b4cdSKlaus Jensen     uint64_t zrwas;
127e321b4cdSKlaus Jensen     uint64_t zrwafg;
12888eea45cSKlaus Jensen } NvmeNamespaceParams;
12988eea45cSKlaus Jensen 
13088eea45cSKlaus Jensen typedef struct NvmeNamespace {
13188eea45cSKlaus Jensen     DeviceState  parent_obj;
13288eea45cSKlaus Jensen     BlockConf    blkconf;
13388eea45cSKlaus Jensen     int32_t      bootindex;
13488eea45cSKlaus Jensen     int64_t      size;
13588eea45cSKlaus Jensen     int64_t      moff;
13688eea45cSKlaus Jensen     NvmeIdNs     id_ns;
13744219b60SNaveen Nagar     NvmeIdNsNvm  id_ns_nvm;
13888eea45cSKlaus Jensen     NvmeLBAF     lbaf;
139763c05dfSNaveen Nagar     unsigned int nlbaf;
14088eea45cSKlaus Jensen     size_t       lbasz;
14188eea45cSKlaus Jensen     const uint32_t *iocs;
14288eea45cSKlaus Jensen     uint8_t      csi;
14388eea45cSKlaus Jensen     uint16_t     status;
14488eea45cSKlaus Jensen     int          attached;
14544219b60SNaveen Nagar     uint8_t      pif;
14688eea45cSKlaus Jensen 
147e321b4cdSKlaus Jensen     struct {
148e321b4cdSKlaus Jensen         uint16_t zrwas;
149e321b4cdSKlaus Jensen         uint16_t zrwafg;
150e321b4cdSKlaus Jensen         uint32_t numzrwa;
151e321b4cdSKlaus Jensen     } zns;
152e321b4cdSKlaus Jensen 
15388eea45cSKlaus Jensen     QTAILQ_ENTRY(NvmeNamespace) entry;
15488eea45cSKlaus Jensen 
15588eea45cSKlaus Jensen     NvmeIdNsZoned   *id_ns_zoned;
15688eea45cSKlaus Jensen     NvmeZone        *zone_array;
15788eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeZone) exp_open_zones;
15888eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeZone) imp_open_zones;
15988eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeZone) closed_zones;
16088eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeZone) full_zones;
16188eea45cSKlaus Jensen     uint32_t        num_zones;
16288eea45cSKlaus Jensen     uint64_t        zone_size;
16388eea45cSKlaus Jensen     uint64_t        zone_capacity;
16488eea45cSKlaus Jensen     uint32_t        zone_size_log2;
16588eea45cSKlaus Jensen     uint8_t         *zd_extensions;
16688eea45cSKlaus Jensen     int32_t         nr_open_zones;
16788eea45cSKlaus Jensen     int32_t         nr_active_zones;
16888eea45cSKlaus Jensen 
16988eea45cSKlaus Jensen     NvmeNamespaceParams params;
17088eea45cSKlaus Jensen 
17188eea45cSKlaus Jensen     struct {
17288eea45cSKlaus Jensen         uint32_t err_rec;
17388eea45cSKlaus Jensen     } features;
17488eea45cSKlaus Jensen } NvmeNamespace;
17588eea45cSKlaus Jensen 
17688eea45cSKlaus Jensen static inline uint32_t nvme_nsid(NvmeNamespace *ns)
17788eea45cSKlaus Jensen {
17888eea45cSKlaus Jensen     if (ns) {
17988eea45cSKlaus Jensen         return ns->params.nsid;
18088eea45cSKlaus Jensen     }
18188eea45cSKlaus Jensen 
18288eea45cSKlaus Jensen     return 0;
18388eea45cSKlaus Jensen }
18488eea45cSKlaus Jensen 
18588eea45cSKlaus Jensen static inline size_t nvme_l2b(NvmeNamespace *ns, uint64_t lba)
18688eea45cSKlaus Jensen {
18788eea45cSKlaus Jensen     return lba << ns->lbaf.ds;
18888eea45cSKlaus Jensen }
18988eea45cSKlaus Jensen 
19088eea45cSKlaus Jensen static inline size_t nvme_m2b(NvmeNamespace *ns, uint64_t lba)
19188eea45cSKlaus Jensen {
19288eea45cSKlaus Jensen     return ns->lbaf.ms * lba;
19388eea45cSKlaus Jensen }
19488eea45cSKlaus Jensen 
19588eea45cSKlaus Jensen static inline int64_t nvme_moff(NvmeNamespace *ns, uint64_t lba)
19688eea45cSKlaus Jensen {
19788eea45cSKlaus Jensen     return ns->moff + nvme_m2b(ns, lba);
19888eea45cSKlaus Jensen }
19988eea45cSKlaus Jensen 
20088eea45cSKlaus Jensen static inline bool nvme_ns_ext(NvmeNamespace *ns)
20188eea45cSKlaus Jensen {
20288eea45cSKlaus Jensen     return !!NVME_ID_NS_FLBAS_EXTENDED(ns->id_ns.flbas);
20388eea45cSKlaus Jensen }
20488eea45cSKlaus Jensen 
20588eea45cSKlaus Jensen static inline NvmeZoneState nvme_get_zone_state(NvmeZone *zone)
20688eea45cSKlaus Jensen {
20788eea45cSKlaus Jensen     return zone->d.zs >> 4;
20888eea45cSKlaus Jensen }
20988eea45cSKlaus Jensen 
21088eea45cSKlaus Jensen static inline void nvme_set_zone_state(NvmeZone *zone, NvmeZoneState state)
21188eea45cSKlaus Jensen {
21288eea45cSKlaus Jensen     zone->d.zs = state << 4;
21388eea45cSKlaus Jensen }
21488eea45cSKlaus Jensen 
21588eea45cSKlaus Jensen static inline uint64_t nvme_zone_rd_boundary(NvmeNamespace *ns, NvmeZone *zone)
21688eea45cSKlaus Jensen {
21788eea45cSKlaus Jensen     return zone->d.zslba + ns->zone_size;
21888eea45cSKlaus Jensen }
21988eea45cSKlaus Jensen 
22088eea45cSKlaus Jensen static inline uint64_t nvme_zone_wr_boundary(NvmeZone *zone)
22188eea45cSKlaus Jensen {
22288eea45cSKlaus Jensen     return zone->d.zslba + zone->d.zcap;
22388eea45cSKlaus Jensen }
22488eea45cSKlaus Jensen 
22588eea45cSKlaus Jensen static inline bool nvme_wp_is_valid(NvmeZone *zone)
22688eea45cSKlaus Jensen {
22788eea45cSKlaus Jensen     uint8_t st = nvme_get_zone_state(zone);
22888eea45cSKlaus Jensen 
22988eea45cSKlaus Jensen     return st != NVME_ZONE_STATE_FULL &&
23088eea45cSKlaus Jensen            st != NVME_ZONE_STATE_READ_ONLY &&
23188eea45cSKlaus Jensen            st != NVME_ZONE_STATE_OFFLINE;
23288eea45cSKlaus Jensen }
23388eea45cSKlaus Jensen 
23488eea45cSKlaus Jensen static inline uint8_t *nvme_get_zd_extension(NvmeNamespace *ns,
23588eea45cSKlaus Jensen                                              uint32_t zone_idx)
23688eea45cSKlaus Jensen {
23788eea45cSKlaus Jensen     return &ns->zd_extensions[zone_idx * ns->params.zd_extension_size];
23888eea45cSKlaus Jensen }
23988eea45cSKlaus Jensen 
24088eea45cSKlaus Jensen static inline void nvme_aor_inc_open(NvmeNamespace *ns)
24188eea45cSKlaus Jensen {
24288eea45cSKlaus Jensen     assert(ns->nr_open_zones >= 0);
24388eea45cSKlaus Jensen     if (ns->params.max_open_zones) {
24488eea45cSKlaus Jensen         ns->nr_open_zones++;
24588eea45cSKlaus Jensen         assert(ns->nr_open_zones <= ns->params.max_open_zones);
24688eea45cSKlaus Jensen     }
24788eea45cSKlaus Jensen }
24888eea45cSKlaus Jensen 
24988eea45cSKlaus Jensen static inline void nvme_aor_dec_open(NvmeNamespace *ns)
25088eea45cSKlaus Jensen {
25188eea45cSKlaus Jensen     if (ns->params.max_open_zones) {
25288eea45cSKlaus Jensen         assert(ns->nr_open_zones > 0);
25388eea45cSKlaus Jensen         ns->nr_open_zones--;
25488eea45cSKlaus Jensen     }
25588eea45cSKlaus Jensen     assert(ns->nr_open_zones >= 0);
25688eea45cSKlaus Jensen }
25788eea45cSKlaus Jensen 
25888eea45cSKlaus Jensen static inline void nvme_aor_inc_active(NvmeNamespace *ns)
25988eea45cSKlaus Jensen {
26088eea45cSKlaus Jensen     assert(ns->nr_active_zones >= 0);
26188eea45cSKlaus Jensen     if (ns->params.max_active_zones) {
26288eea45cSKlaus Jensen         ns->nr_active_zones++;
26388eea45cSKlaus Jensen         assert(ns->nr_active_zones <= ns->params.max_active_zones);
26488eea45cSKlaus Jensen     }
26588eea45cSKlaus Jensen }
26688eea45cSKlaus Jensen 
26788eea45cSKlaus Jensen static inline void nvme_aor_dec_active(NvmeNamespace *ns)
26888eea45cSKlaus Jensen {
26988eea45cSKlaus Jensen     if (ns->params.max_active_zones) {
27088eea45cSKlaus Jensen         assert(ns->nr_active_zones > 0);
27188eea45cSKlaus Jensen         ns->nr_active_zones--;
27288eea45cSKlaus Jensen         assert(ns->nr_active_zones >= ns->nr_open_zones);
27388eea45cSKlaus Jensen     }
27488eea45cSKlaus Jensen     assert(ns->nr_active_zones >= 0);
27588eea45cSKlaus Jensen }
27688eea45cSKlaus Jensen 
27788eea45cSKlaus Jensen void nvme_ns_init_format(NvmeNamespace *ns);
2785e4f6bccSKlaus Jensen int nvme_ns_setup(NvmeNamespace *ns, Error **errp);
27988eea45cSKlaus Jensen void nvme_ns_drain(NvmeNamespace *ns);
28088eea45cSKlaus Jensen void nvme_ns_shutdown(NvmeNamespace *ns);
28188eea45cSKlaus Jensen void nvme_ns_cleanup(NvmeNamespace *ns);
28288eea45cSKlaus Jensen 
28388eea45cSKlaus Jensen typedef struct NvmeAsyncEvent {
28488eea45cSKlaus Jensen     QTAILQ_ENTRY(NvmeAsyncEvent) entry;
28588eea45cSKlaus Jensen     NvmeAerResult result;
28688eea45cSKlaus Jensen } NvmeAsyncEvent;
28788eea45cSKlaus Jensen 
28888eea45cSKlaus Jensen enum {
28988eea45cSKlaus Jensen     NVME_SG_ALLOC = 1 << 0,
29088eea45cSKlaus Jensen     NVME_SG_DMA   = 1 << 1,
29188eea45cSKlaus Jensen };
29288eea45cSKlaus Jensen 
29388eea45cSKlaus Jensen typedef struct NvmeSg {
29488eea45cSKlaus Jensen     int flags;
29588eea45cSKlaus Jensen 
29688eea45cSKlaus Jensen     union {
29788eea45cSKlaus Jensen         QEMUSGList   qsg;
29888eea45cSKlaus Jensen         QEMUIOVector iov;
29988eea45cSKlaus Jensen     };
30088eea45cSKlaus Jensen } NvmeSg;
30188eea45cSKlaus Jensen 
30288eea45cSKlaus Jensen typedef enum NvmeTxDirection {
30388eea45cSKlaus Jensen     NVME_TX_DIRECTION_TO_DEVICE   = 0,
30488eea45cSKlaus Jensen     NVME_TX_DIRECTION_FROM_DEVICE = 1,
30588eea45cSKlaus Jensen } NvmeTxDirection;
30688eea45cSKlaus Jensen 
30788eea45cSKlaus Jensen typedef struct NvmeRequest {
30888eea45cSKlaus Jensen     struct NvmeSQueue       *sq;
30988eea45cSKlaus Jensen     struct NvmeNamespace    *ns;
31088eea45cSKlaus Jensen     BlockAIOCB              *aiocb;
31188eea45cSKlaus Jensen     uint16_t                status;
31288eea45cSKlaus Jensen     void                    *opaque;
31388eea45cSKlaus Jensen     NvmeCqe                 cqe;
31488eea45cSKlaus Jensen     NvmeCmd                 cmd;
31588eea45cSKlaus Jensen     BlockAcctCookie         acct;
31688eea45cSKlaus Jensen     NvmeSg                  sg;
31788eea45cSKlaus Jensen     QTAILQ_ENTRY(NvmeRequest)entry;
31888eea45cSKlaus Jensen } NvmeRequest;
31988eea45cSKlaus Jensen 
32088eea45cSKlaus Jensen typedef struct NvmeBounceContext {
32188eea45cSKlaus Jensen     NvmeRequest *req;
32288eea45cSKlaus Jensen 
32388eea45cSKlaus Jensen     struct {
32488eea45cSKlaus Jensen         QEMUIOVector iov;
32588eea45cSKlaus Jensen         uint8_t *bounce;
32688eea45cSKlaus Jensen     } data, mdata;
32788eea45cSKlaus Jensen } NvmeBounceContext;
32888eea45cSKlaus Jensen 
32988eea45cSKlaus Jensen static inline const char *nvme_adm_opc_str(uint8_t opc)
33088eea45cSKlaus Jensen {
33188eea45cSKlaus Jensen     switch (opc) {
33288eea45cSKlaus Jensen     case NVME_ADM_CMD_DELETE_SQ:        return "NVME_ADM_CMD_DELETE_SQ";
33388eea45cSKlaus Jensen     case NVME_ADM_CMD_CREATE_SQ:        return "NVME_ADM_CMD_CREATE_SQ";
33488eea45cSKlaus Jensen     case NVME_ADM_CMD_GET_LOG_PAGE:     return "NVME_ADM_CMD_GET_LOG_PAGE";
33588eea45cSKlaus Jensen     case NVME_ADM_CMD_DELETE_CQ:        return "NVME_ADM_CMD_DELETE_CQ";
33688eea45cSKlaus Jensen     case NVME_ADM_CMD_CREATE_CQ:        return "NVME_ADM_CMD_CREATE_CQ";
33788eea45cSKlaus Jensen     case NVME_ADM_CMD_IDENTIFY:         return "NVME_ADM_CMD_IDENTIFY";
33888eea45cSKlaus Jensen     case NVME_ADM_CMD_ABORT:            return "NVME_ADM_CMD_ABORT";
33988eea45cSKlaus Jensen     case NVME_ADM_CMD_SET_FEATURES:     return "NVME_ADM_CMD_SET_FEATURES";
34088eea45cSKlaus Jensen     case NVME_ADM_CMD_GET_FEATURES:     return "NVME_ADM_CMD_GET_FEATURES";
34188eea45cSKlaus Jensen     case NVME_ADM_CMD_ASYNC_EV_REQ:     return "NVME_ADM_CMD_ASYNC_EV_REQ";
34288eea45cSKlaus Jensen     case NVME_ADM_CMD_NS_ATTACHMENT:    return "NVME_ADM_CMD_NS_ATTACHMENT";
343*11871f53SŁukasz Gieryk     case NVME_ADM_CMD_VIRT_MNGMT:       return "NVME_ADM_CMD_VIRT_MNGMT";
34488eea45cSKlaus Jensen     case NVME_ADM_CMD_FORMAT_NVM:       return "NVME_ADM_CMD_FORMAT_NVM";
34588eea45cSKlaus Jensen     default:                            return "NVME_ADM_CMD_UNKNOWN";
34688eea45cSKlaus Jensen     }
34788eea45cSKlaus Jensen }
34888eea45cSKlaus Jensen 
34988eea45cSKlaus Jensen static inline const char *nvme_io_opc_str(uint8_t opc)
35088eea45cSKlaus Jensen {
35188eea45cSKlaus Jensen     switch (opc) {
35288eea45cSKlaus Jensen     case NVME_CMD_FLUSH:            return "NVME_NVM_CMD_FLUSH";
35388eea45cSKlaus Jensen     case NVME_CMD_WRITE:            return "NVME_NVM_CMD_WRITE";
35488eea45cSKlaus Jensen     case NVME_CMD_READ:             return "NVME_NVM_CMD_READ";
35588eea45cSKlaus Jensen     case NVME_CMD_COMPARE:          return "NVME_NVM_CMD_COMPARE";
35688eea45cSKlaus Jensen     case NVME_CMD_WRITE_ZEROES:     return "NVME_NVM_CMD_WRITE_ZEROES";
35788eea45cSKlaus Jensen     case NVME_CMD_DSM:              return "NVME_NVM_CMD_DSM";
35888eea45cSKlaus Jensen     case NVME_CMD_VERIFY:           return "NVME_NVM_CMD_VERIFY";
35988eea45cSKlaus Jensen     case NVME_CMD_COPY:             return "NVME_NVM_CMD_COPY";
36088eea45cSKlaus Jensen     case NVME_CMD_ZONE_MGMT_SEND:   return "NVME_ZONED_CMD_MGMT_SEND";
36188eea45cSKlaus Jensen     case NVME_CMD_ZONE_MGMT_RECV:   return "NVME_ZONED_CMD_MGMT_RECV";
36288eea45cSKlaus Jensen     case NVME_CMD_ZONE_APPEND:      return "NVME_ZONED_CMD_ZONE_APPEND";
36388eea45cSKlaus Jensen     default:                        return "NVME_NVM_CMD_UNKNOWN";
36488eea45cSKlaus Jensen     }
36588eea45cSKlaus Jensen }
36688eea45cSKlaus Jensen 
36788eea45cSKlaus Jensen typedef struct NvmeSQueue {
36888eea45cSKlaus Jensen     struct NvmeCtrl *ctrl;
36988eea45cSKlaus Jensen     uint16_t    sqid;
37088eea45cSKlaus Jensen     uint16_t    cqid;
37188eea45cSKlaus Jensen     uint32_t    head;
37288eea45cSKlaus Jensen     uint32_t    tail;
37388eea45cSKlaus Jensen     uint32_t    size;
37488eea45cSKlaus Jensen     uint64_t    dma_addr;
37588eea45cSKlaus Jensen     QEMUTimer   *timer;
37688eea45cSKlaus Jensen     NvmeRequest *io_req;
37788eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeRequest) req_list;
37888eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeRequest) out_req_list;
37988eea45cSKlaus Jensen     QTAILQ_ENTRY(NvmeSQueue) entry;
38088eea45cSKlaus Jensen } NvmeSQueue;
38188eea45cSKlaus Jensen 
38288eea45cSKlaus Jensen typedef struct NvmeCQueue {
38388eea45cSKlaus Jensen     struct NvmeCtrl *ctrl;
38488eea45cSKlaus Jensen     uint8_t     phase;
38588eea45cSKlaus Jensen     uint16_t    cqid;
38688eea45cSKlaus Jensen     uint16_t    irq_enabled;
38788eea45cSKlaus Jensen     uint32_t    head;
38888eea45cSKlaus Jensen     uint32_t    tail;
38988eea45cSKlaus Jensen     uint32_t    vector;
39088eea45cSKlaus Jensen     uint32_t    size;
39188eea45cSKlaus Jensen     uint64_t    dma_addr;
39288eea45cSKlaus Jensen     QEMUTimer   *timer;
39388eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeSQueue) sq_list;
39488eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeRequest) req_list;
39588eea45cSKlaus Jensen } NvmeCQueue;
39688eea45cSKlaus Jensen 
39788eea45cSKlaus Jensen #define TYPE_NVME "nvme"
39888eea45cSKlaus Jensen #define NVME(obj) \
39988eea45cSKlaus Jensen         OBJECT_CHECK(NvmeCtrl, (obj), TYPE_NVME)
40088eea45cSKlaus Jensen 
40188eea45cSKlaus Jensen typedef struct NvmeParams {
40288eea45cSKlaus Jensen     char     *serial;
40388eea45cSKlaus Jensen     uint32_t num_queues; /* deprecated since 5.1 */
40488eea45cSKlaus Jensen     uint32_t max_ioqpairs;
40588eea45cSKlaus Jensen     uint16_t msix_qsize;
40688eea45cSKlaus Jensen     uint32_t cmb_size_mb;
40788eea45cSKlaus Jensen     uint8_t  aerl;
40888eea45cSKlaus Jensen     uint32_t aer_max_queued;
40988eea45cSKlaus Jensen     uint8_t  mdts;
41088eea45cSKlaus Jensen     uint8_t  vsl;
41188eea45cSKlaus Jensen     bool     use_intel_id;
41288eea45cSKlaus Jensen     uint8_t  zasl;
413cccc2651SNiklas Cassel     bool     auto_transition_zones;
41488eea45cSKlaus Jensen     bool     legacy_cmb;
41544c2c094SLukasz Maniak     uint8_t  sriov_max_vfs;
416746d42b1SŁukasz Gieryk     uint16_t sriov_vq_flexible;
417746d42b1SŁukasz Gieryk     uint16_t sriov_vi_flexible;
418746d42b1SŁukasz Gieryk     uint8_t  sriov_max_vq_per_vf;
419746d42b1SŁukasz Gieryk     uint8_t  sriov_max_vi_per_vf;
42088eea45cSKlaus Jensen } NvmeParams;
42188eea45cSKlaus Jensen 
42288eea45cSKlaus Jensen typedef struct NvmeCtrl {
42388eea45cSKlaus Jensen     PCIDevice    parent_obj;
42488eea45cSKlaus Jensen     MemoryRegion bar0;
42588eea45cSKlaus Jensen     MemoryRegion iomem;
42688eea45cSKlaus Jensen     NvmeBar      bar;
42788eea45cSKlaus Jensen     NvmeParams   params;
42888eea45cSKlaus Jensen     NvmeBus      bus;
42988eea45cSKlaus Jensen 
43088eea45cSKlaus Jensen     uint16_t    cntlid;
43188eea45cSKlaus Jensen     bool        qs_created;
43288eea45cSKlaus Jensen     uint32_t    page_size;
43388eea45cSKlaus Jensen     uint16_t    page_bits;
43488eea45cSKlaus Jensen     uint16_t    max_prp_ents;
43588eea45cSKlaus Jensen     uint16_t    cqe_size;
43688eea45cSKlaus Jensen     uint16_t    sqe_size;
43788eea45cSKlaus Jensen     uint32_t    max_q_ents;
43888eea45cSKlaus Jensen     uint8_t     outstanding_aers;
43988eea45cSKlaus Jensen     uint32_t    irq_status;
44083d7ed5cSKlaus Jensen     int         cq_pending;
44188eea45cSKlaus Jensen     uint64_t    host_timestamp;                 /* Timestamp sent by the host */
44288eea45cSKlaus Jensen     uint64_t    timestamp_set_qemu_clock_ms;    /* QEMU clock time */
44388eea45cSKlaus Jensen     uint64_t    starttime_ms;
44488eea45cSKlaus Jensen     uint16_t    temperature;
44588eea45cSKlaus Jensen     uint8_t     smart_critical_warning;
446decc0261SŁukasz Gieryk     uint32_t    conf_msix_qsize;
447decc0261SŁukasz Gieryk     uint32_t    conf_ioqpairs;
44888eea45cSKlaus Jensen 
44988eea45cSKlaus Jensen     struct {
45088eea45cSKlaus Jensen         MemoryRegion mem;
45188eea45cSKlaus Jensen         uint8_t      *buf;
45288eea45cSKlaus Jensen         bool         cmse;
45388eea45cSKlaus Jensen         hwaddr       cba;
45488eea45cSKlaus Jensen     } cmb;
45588eea45cSKlaus Jensen 
45688eea45cSKlaus Jensen     struct {
45788eea45cSKlaus Jensen         HostMemoryBackend *dev;
45888eea45cSKlaus Jensen         bool              cmse;
45988eea45cSKlaus Jensen         hwaddr            cba;
46088eea45cSKlaus Jensen     } pmr;
46188eea45cSKlaus Jensen 
46288eea45cSKlaus Jensen     uint8_t     aer_mask;
46388eea45cSKlaus Jensen     NvmeRequest **aer_reqs;
46488eea45cSKlaus Jensen     QTAILQ_HEAD(, NvmeAsyncEvent) aer_queue;
46588eea45cSKlaus Jensen     int         aer_queued;
46688eea45cSKlaus Jensen 
46788eea45cSKlaus Jensen     uint32_t    dmrsl;
46888eea45cSKlaus Jensen 
46988eea45cSKlaus Jensen     /* Namespace ID is started with 1 so bitmap should be 1-based */
47088eea45cSKlaus Jensen #define NVME_CHANGED_NSID_SIZE  (NVME_MAX_NAMESPACES + 1)
47188eea45cSKlaus Jensen     DECLARE_BITMAP(changed_nsids, NVME_CHANGED_NSID_SIZE);
47288eea45cSKlaus Jensen 
47388eea45cSKlaus Jensen     NvmeSubsystem   *subsys;
47488eea45cSKlaus Jensen 
47588eea45cSKlaus Jensen     NvmeNamespace   namespace;
47688eea45cSKlaus Jensen     NvmeNamespace   *namespaces[NVME_MAX_NAMESPACES + 1];
47788eea45cSKlaus Jensen     NvmeSQueue      **sq;
47888eea45cSKlaus Jensen     NvmeCQueue      **cq;
47988eea45cSKlaus Jensen     NvmeSQueue      admin_sq;
48088eea45cSKlaus Jensen     NvmeCQueue      admin_cq;
48188eea45cSKlaus Jensen     NvmeIdCtrl      id_ctrl;
48288eea45cSKlaus Jensen 
48388eea45cSKlaus Jensen     struct {
48488eea45cSKlaus Jensen         struct {
48588eea45cSKlaus Jensen             uint16_t temp_thresh_hi;
48688eea45cSKlaus Jensen             uint16_t temp_thresh_low;
48788eea45cSKlaus Jensen         };
488d0c0697bSNaveen Nagar 
48988eea45cSKlaus Jensen         uint32_t                async_config;
490d0c0697bSNaveen Nagar         NvmeHostBehaviorSupport hbs;
49188eea45cSKlaus Jensen     } features;
4925e6f963fSLukasz Maniak 
4935e6f963fSLukasz Maniak     NvmePriCtrlCap  pri_ctrl_cap;
49499f48ae7SLukasz Maniak     NvmeSecCtrlList sec_ctrl_list;
495*11871f53SŁukasz Gieryk     struct {
496*11871f53SŁukasz Gieryk         uint16_t    vqrfap;
497*11871f53SŁukasz Gieryk         uint16_t    virfap;
498*11871f53SŁukasz Gieryk     } next_pri_ctrl_cap;    /* These override pri_ctrl_cap after reset */
49988eea45cSKlaus Jensen } NvmeCtrl;
50088eea45cSKlaus Jensen 
5011e9c685eSŁukasz Gieryk typedef enum NvmeResetType {
5021e9c685eSŁukasz Gieryk     NVME_RESET_FUNCTION   = 0,
5031e9c685eSŁukasz Gieryk     NVME_RESET_CONTROLLER = 1,
5041e9c685eSŁukasz Gieryk } NvmeResetType;
5051e9c685eSŁukasz Gieryk 
50688eea45cSKlaus Jensen static inline NvmeNamespace *nvme_ns(NvmeCtrl *n, uint32_t nsid)
50788eea45cSKlaus Jensen {
50888eea45cSKlaus Jensen     if (!nsid || nsid > NVME_MAX_NAMESPACES) {
50988eea45cSKlaus Jensen         return NULL;
51088eea45cSKlaus Jensen     }
51188eea45cSKlaus Jensen 
51288eea45cSKlaus Jensen     return n->namespaces[nsid];
51388eea45cSKlaus Jensen }
51488eea45cSKlaus Jensen 
51588eea45cSKlaus Jensen static inline NvmeCQueue *nvme_cq(NvmeRequest *req)
51688eea45cSKlaus Jensen {
51788eea45cSKlaus Jensen     NvmeSQueue *sq = req->sq;
51888eea45cSKlaus Jensen     NvmeCtrl *n = sq->ctrl;
51988eea45cSKlaus Jensen 
52088eea45cSKlaus Jensen     return n->cq[sq->cqid];
52188eea45cSKlaus Jensen }
52288eea45cSKlaus Jensen 
52388eea45cSKlaus Jensen static inline NvmeCtrl *nvme_ctrl(NvmeRequest *req)
52488eea45cSKlaus Jensen {
52588eea45cSKlaus Jensen     NvmeSQueue *sq = req->sq;
52688eea45cSKlaus Jensen     return sq->ctrl;
52788eea45cSKlaus Jensen }
52888eea45cSKlaus Jensen 
52988eea45cSKlaus Jensen static inline uint16_t nvme_cid(NvmeRequest *req)
53088eea45cSKlaus Jensen {
53188eea45cSKlaus Jensen     if (!req) {
53288eea45cSKlaus Jensen         return 0xffff;
53388eea45cSKlaus Jensen     }
53488eea45cSKlaus Jensen 
53588eea45cSKlaus Jensen     return le16_to_cpu(req->cqe.cid);
53688eea45cSKlaus Jensen }
53788eea45cSKlaus Jensen 
53899f48ae7SLukasz Maniak static inline NvmeSecCtrlEntry *nvme_sctrl(NvmeCtrl *n)
53999f48ae7SLukasz Maniak {
54099f48ae7SLukasz Maniak     PCIDevice *pci_dev = &n->parent_obj;
54199f48ae7SLukasz Maniak     NvmeCtrl *pf = NVME(pcie_sriov_get_pf(pci_dev));
54299f48ae7SLukasz Maniak 
54399f48ae7SLukasz Maniak     if (pci_is_vf(pci_dev)) {
54499f48ae7SLukasz Maniak         return &pf->sec_ctrl_list.sec[pcie_sriov_vf_number(pci_dev)];
54599f48ae7SLukasz Maniak     }
54699f48ae7SLukasz Maniak 
54799f48ae7SLukasz Maniak     return NULL;
54899f48ae7SLukasz Maniak }
54999f48ae7SLukasz Maniak 
550*11871f53SŁukasz Gieryk static inline NvmeSecCtrlEntry *nvme_sctrl_for_cntlid(NvmeCtrl *n,
551*11871f53SŁukasz Gieryk                                                       uint16_t cntlid)
552*11871f53SŁukasz Gieryk {
553*11871f53SŁukasz Gieryk     NvmeSecCtrlList *list = &n->sec_ctrl_list;
554*11871f53SŁukasz Gieryk     uint8_t i;
555*11871f53SŁukasz Gieryk 
556*11871f53SŁukasz Gieryk     for (i = 0; i < list->numcntl; i++) {
557*11871f53SŁukasz Gieryk         if (le16_to_cpu(list->sec[i].scid) == cntlid) {
558*11871f53SŁukasz Gieryk             return &list->sec[i];
559*11871f53SŁukasz Gieryk         }
560*11871f53SŁukasz Gieryk     }
561*11871f53SŁukasz Gieryk 
562*11871f53SŁukasz Gieryk     return NULL;
563*11871f53SŁukasz Gieryk }
564*11871f53SŁukasz Gieryk 
56588eea45cSKlaus Jensen void nvme_attach_ns(NvmeCtrl *n, NvmeNamespace *ns);
5668d3a17beSPhilippe Mathieu-Daudé uint16_t nvme_bounce_data(NvmeCtrl *n, void *ptr, uint32_t len,
56788eea45cSKlaus Jensen                           NvmeTxDirection dir, NvmeRequest *req);
5688d3a17beSPhilippe Mathieu-Daudé uint16_t nvme_bounce_mdata(NvmeCtrl *n, void *ptr, uint32_t len,
56988eea45cSKlaus Jensen                            NvmeTxDirection dir, NvmeRequest *req);
57088eea45cSKlaus Jensen void nvme_rw_complete_cb(void *opaque, int ret);
57188eea45cSKlaus Jensen uint16_t nvme_map_dptr(NvmeCtrl *n, NvmeSg *sg, size_t len,
57288eea45cSKlaus Jensen                        NvmeCmd *cmd);
57388eea45cSKlaus Jensen 
57452581c71SMarkus Armbruster #endif /* HW_NVME_NVME_H */
575