125500d4bSJay Zhou /* SPDX-License-Identifier: BSD-3-Clause
225500d4bSJay Zhou * Copyright(c) 2018 HUAWEI TECHNOLOGIES CO., LTD.
325500d4bSJay Zhou */
425500d4bSJay Zhou
525500d4bSJay Zhou #ifndef _VIRTIO_PCI_H_
625500d4bSJay Zhou #define _VIRTIO_PCI_H_
725500d4bSJay Zhou
825500d4bSJay Zhou #include <stdint.h>
925500d4bSJay Zhou
10924e6b76SThomas Monjalon #include <rte_eal_paging.h>
1125500d4bSJay Zhou #include <rte_pci.h>
12*1f37cb2bSDavid Marchand #include <bus_pci_driver.h>
1325500d4bSJay Zhou #include <rte_cryptodev.h>
1425500d4bSJay Zhou
1525500d4bSJay Zhou #include "virtio_crypto.h"
1625500d4bSJay Zhou
1725500d4bSJay Zhou struct virtqueue;
1825500d4bSJay Zhou
1925500d4bSJay Zhou /* VirtIO PCI vendor/device ID. */
2025500d4bSJay Zhou #define VIRTIO_CRYPTO_PCI_VENDORID 0x1AF4
2125500d4bSJay Zhou #define VIRTIO_CRYPTO_PCI_DEVICEID 0x1054
2225500d4bSJay Zhou
2325500d4bSJay Zhou /* VirtIO ABI version, this must match exactly. */
2425500d4bSJay Zhou #define VIRTIO_PCI_ABI_VERSION 0
2525500d4bSJay Zhou
2625500d4bSJay Zhou /*
2725500d4bSJay Zhou * VirtIO Header, located in BAR 0.
2825500d4bSJay Zhou */
2925500d4bSJay Zhou #define VIRTIO_PCI_HOST_FEATURES 0 /* host's supported features (32bit, RO)*/
3025500d4bSJay Zhou #define VIRTIO_PCI_GUEST_FEATURES 4 /* guest's supported features (32, RW) */
3125500d4bSJay Zhou #define VIRTIO_PCI_QUEUE_PFN 8 /* physical address of VQ (32, RW) */
3225500d4bSJay Zhou #define VIRTIO_PCI_QUEUE_NUM 12 /* number of ring entries (16, RO) */
3325500d4bSJay Zhou #define VIRTIO_PCI_QUEUE_SEL 14 /* current VQ selection (16, RW) */
3425500d4bSJay Zhou #define VIRTIO_PCI_QUEUE_NOTIFY 16 /* notify host regarding VQ (16, RW) */
3525500d4bSJay Zhou #define VIRTIO_PCI_STATUS 18 /* device status register (8, RW) */
3625500d4bSJay Zhou #define VIRTIO_PCI_ISR 19 /* interrupt status register, reading
3725500d4bSJay Zhou * also clears the register (8, RO)
3825500d4bSJay Zhou */
3925500d4bSJay Zhou /* Only if MSIX is enabled: */
4025500d4bSJay Zhou
4125500d4bSJay Zhou /* configuration change vector (16, RW) */
4225500d4bSJay Zhou #define VIRTIO_MSI_CONFIG_VECTOR 20
4325500d4bSJay Zhou /* vector for selected VQ notifications */
4425500d4bSJay Zhou #define VIRTIO_MSI_QUEUE_VECTOR 22
4525500d4bSJay Zhou
4625500d4bSJay Zhou /* The bit of the ISR which indicates a device has an interrupt. */
4725500d4bSJay Zhou #define VIRTIO_PCI_ISR_INTR 0x1
4825500d4bSJay Zhou /* The bit of the ISR which indicates a device configuration change. */
4925500d4bSJay Zhou #define VIRTIO_PCI_ISR_CONFIG 0x2
5025500d4bSJay Zhou /* Vector value used to disable MSI for queue. */
5125500d4bSJay Zhou #define VIRTIO_MSI_NO_VECTOR 0xFFFF
5225500d4bSJay Zhou
5325500d4bSJay Zhou /* Status byte for guest to report progress. */
5425500d4bSJay Zhou #define VIRTIO_CONFIG_STATUS_RESET 0x00
5525500d4bSJay Zhou #define VIRTIO_CONFIG_STATUS_ACK 0x01
5625500d4bSJay Zhou #define VIRTIO_CONFIG_STATUS_DRIVER 0x02
5725500d4bSJay Zhou #define VIRTIO_CONFIG_STATUS_DRIVER_OK 0x04
5825500d4bSJay Zhou #define VIRTIO_CONFIG_STATUS_FEATURES_OK 0x08
5925500d4bSJay Zhou #define VIRTIO_CONFIG_STATUS_FAILED 0x80
6025500d4bSJay Zhou
6125500d4bSJay Zhou /*
6225500d4bSJay Zhou * Each virtqueue indirect descriptor list must be physically contiguous.
6325500d4bSJay Zhou * To allow us to malloc(9) each list individually, limit the number
6425500d4bSJay Zhou * supported to what will fit in one page. With 4KB pages, this is a limit
6525500d4bSJay Zhou * of 256 descriptors. If there is ever a need for more, we can switch to
6625500d4bSJay Zhou * contigmalloc(9) for the larger allocations, similar to what
6725500d4bSJay Zhou * bus_dmamem_alloc(9) does.
6825500d4bSJay Zhou *
6925500d4bSJay Zhou * Note the sizeof(struct vring_desc) is 16 bytes.
7025500d4bSJay Zhou */
71924e6b76SThomas Monjalon #define VIRTIO_MAX_INDIRECT ((int) (rte_mem_page_size() / 16))
7225500d4bSJay Zhou
7325500d4bSJay Zhou /* Do we get callbacks when the ring is completely used, even if we've
7425500d4bSJay Zhou * suppressed them?
7525500d4bSJay Zhou */
7625500d4bSJay Zhou #define VIRTIO_F_NOTIFY_ON_EMPTY 24
7725500d4bSJay Zhou
7825500d4bSJay Zhou /* Can the device handle any descriptor layout? */
7925500d4bSJay Zhou #define VIRTIO_F_ANY_LAYOUT 27
8025500d4bSJay Zhou
8125500d4bSJay Zhou /* We support indirect buffer descriptors */
8225500d4bSJay Zhou #define VIRTIO_RING_F_INDIRECT_DESC 28
8325500d4bSJay Zhou
8425500d4bSJay Zhou #define VIRTIO_F_VERSION_1 32
8525500d4bSJay Zhou #define VIRTIO_F_IOMMU_PLATFORM 33
8625500d4bSJay Zhou
8725500d4bSJay Zhou /* The Guest publishes the used index for which it expects an interrupt
8825500d4bSJay Zhou * at the end of the avail ring. Host should ignore the avail->flags field.
8925500d4bSJay Zhou */
9025500d4bSJay Zhou /* The Host publishes the avail index for which it expects a kick
9125500d4bSJay Zhou * at the end of the used ring. Guest should ignore the used->flags field.
9225500d4bSJay Zhou */
9325500d4bSJay Zhou #define VIRTIO_RING_F_EVENT_IDX 29
9425500d4bSJay Zhou
9525500d4bSJay Zhou /* Common configuration */
9625500d4bSJay Zhou #define VIRTIO_PCI_CAP_COMMON_CFG 1
9725500d4bSJay Zhou /* Notifications */
9825500d4bSJay Zhou #define VIRTIO_PCI_CAP_NOTIFY_CFG 2
9925500d4bSJay Zhou /* ISR Status */
10025500d4bSJay Zhou #define VIRTIO_PCI_CAP_ISR_CFG 3
10125500d4bSJay Zhou /* Device specific configuration */
10225500d4bSJay Zhou #define VIRTIO_PCI_CAP_DEVICE_CFG 4
10325500d4bSJay Zhou /* PCI configuration access */
10425500d4bSJay Zhou #define VIRTIO_PCI_CAP_PCI_CFG 5
10525500d4bSJay Zhou
10625500d4bSJay Zhou /* This is the PCI capability header: */
10725500d4bSJay Zhou struct virtio_pci_cap {
10825500d4bSJay Zhou uint8_t cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */
10925500d4bSJay Zhou uint8_t cap_next; /* Generic PCI field: next ptr. */
11025500d4bSJay Zhou uint8_t cap_len; /* Generic PCI field: capability length */
11125500d4bSJay Zhou uint8_t cfg_type; /* Identifies the structure. */
11225500d4bSJay Zhou uint8_t bar; /* Where to find it. */
11325500d4bSJay Zhou uint8_t padding[3]; /* Pad to full dword. */
11425500d4bSJay Zhou uint32_t offset; /* Offset within bar. */
11525500d4bSJay Zhou uint32_t length; /* Length of the structure, in bytes. */
11625500d4bSJay Zhou };
11725500d4bSJay Zhou
11825500d4bSJay Zhou struct virtio_pci_notify_cap {
11925500d4bSJay Zhou struct virtio_pci_cap cap;
12025500d4bSJay Zhou uint32_t notify_off_multiplier; /* Multiplier for queue_notify_off. */
12125500d4bSJay Zhou };
12225500d4bSJay Zhou
12325500d4bSJay Zhou /* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
12425500d4bSJay Zhou struct virtio_pci_common_cfg {
12525500d4bSJay Zhou /* About the whole device. */
12625500d4bSJay Zhou uint32_t device_feature_select; /* read-write */
12725500d4bSJay Zhou uint32_t device_feature; /* read-only */
12825500d4bSJay Zhou uint32_t guest_feature_select; /* read-write */
12925500d4bSJay Zhou uint32_t guest_feature; /* read-write */
13025500d4bSJay Zhou uint16_t msix_config; /* read-write */
13125500d4bSJay Zhou uint16_t num_queues; /* read-only */
13225500d4bSJay Zhou uint8_t device_status; /* read-write */
13325500d4bSJay Zhou uint8_t config_generation; /* read-only */
13425500d4bSJay Zhou
13525500d4bSJay Zhou /* About a specific virtqueue. */
13625500d4bSJay Zhou uint16_t queue_select; /* read-write */
13725500d4bSJay Zhou uint16_t queue_size; /* read-write, power of 2. */
13825500d4bSJay Zhou uint16_t queue_msix_vector; /* read-write */
13925500d4bSJay Zhou uint16_t queue_enable; /* read-write */
14025500d4bSJay Zhou uint16_t queue_notify_off; /* read-only */
14125500d4bSJay Zhou uint32_t queue_desc_lo; /* read-write */
14225500d4bSJay Zhou uint32_t queue_desc_hi; /* read-write */
14325500d4bSJay Zhou uint32_t queue_avail_lo; /* read-write */
14425500d4bSJay Zhou uint32_t queue_avail_hi; /* read-write */
14525500d4bSJay Zhou uint32_t queue_used_lo; /* read-write */
14625500d4bSJay Zhou uint32_t queue_used_hi; /* read-write */
14725500d4bSJay Zhou };
14825500d4bSJay Zhou
14925500d4bSJay Zhou struct virtio_crypto_hw;
15025500d4bSJay Zhou
15125500d4bSJay Zhou struct virtio_pci_ops {
15225500d4bSJay Zhou void (*read_dev_cfg)(struct virtio_crypto_hw *hw, size_t offset,
15325500d4bSJay Zhou void *dst, int len);
15425500d4bSJay Zhou void (*write_dev_cfg)(struct virtio_crypto_hw *hw, size_t offset,
15525500d4bSJay Zhou const void *src, int len);
15625500d4bSJay Zhou void (*reset)(struct virtio_crypto_hw *hw);
15725500d4bSJay Zhou
15825500d4bSJay Zhou uint8_t (*get_status)(struct virtio_crypto_hw *hw);
15925500d4bSJay Zhou void (*set_status)(struct virtio_crypto_hw *hw, uint8_t status);
16025500d4bSJay Zhou
16125500d4bSJay Zhou uint64_t (*get_features)(struct virtio_crypto_hw *hw);
16225500d4bSJay Zhou void (*set_features)(struct virtio_crypto_hw *hw, uint64_t features);
16325500d4bSJay Zhou
16425500d4bSJay Zhou uint8_t (*get_isr)(struct virtio_crypto_hw *hw);
16525500d4bSJay Zhou
16625500d4bSJay Zhou uint16_t (*set_config_irq)(struct virtio_crypto_hw *hw, uint16_t vec);
16725500d4bSJay Zhou
16825500d4bSJay Zhou uint16_t (*set_queue_irq)(struct virtio_crypto_hw *hw,
16925500d4bSJay Zhou struct virtqueue *vq, uint16_t vec);
17025500d4bSJay Zhou
17125500d4bSJay Zhou uint16_t (*get_queue_num)(struct virtio_crypto_hw *hw,
17225500d4bSJay Zhou uint16_t queue_id);
17325500d4bSJay Zhou int (*setup_queue)(struct virtio_crypto_hw *hw, struct virtqueue *vq);
17425500d4bSJay Zhou void (*del_queue)(struct virtio_crypto_hw *hw, struct virtqueue *vq);
17525500d4bSJay Zhou void (*notify_queue)(struct virtio_crypto_hw *hw, struct virtqueue *vq);
17625500d4bSJay Zhou };
17725500d4bSJay Zhou
17825500d4bSJay Zhou struct virtio_crypto_hw {
17925500d4bSJay Zhou /* control queue */
18025500d4bSJay Zhou struct virtqueue *cvq;
18125500d4bSJay Zhou uint16_t dev_id;
18225500d4bSJay Zhou uint16_t max_dataqueues;
18325500d4bSJay Zhou uint64_t req_guest_features;
18425500d4bSJay Zhou uint64_t guest_features;
18525500d4bSJay Zhou uint8_t use_msix;
18625500d4bSJay Zhou uint8_t modern;
18725500d4bSJay Zhou uint32_t notify_off_multiplier;
18825500d4bSJay Zhou uint8_t *isr;
18925500d4bSJay Zhou uint16_t *notify_base;
19025500d4bSJay Zhou struct virtio_pci_common_cfg *common_cfg;
19125500d4bSJay Zhou struct virtio_crypto_config *dev_cfg;
19225500d4bSJay Zhou const struct rte_cryptodev_capabilities *virtio_dev_capabilities;
19325500d4bSJay Zhou };
19425500d4bSJay Zhou
19525500d4bSJay Zhou /*
19625500d4bSJay Zhou * While virtio_crypto_hw is stored in shared memory, this structure stores
19725500d4bSJay Zhou * some infos that may vary in the multiple process model locally.
19825500d4bSJay Zhou * For example, the vtpci_ops pointer.
19925500d4bSJay Zhou */
20025500d4bSJay Zhou struct virtio_hw_internal {
20125500d4bSJay Zhou const struct virtio_pci_ops *vtpci_ops;
20225500d4bSJay Zhou struct rte_pci_ioport io;
20325500d4bSJay Zhou };
20425500d4bSJay Zhou
2052c449644SFerruh Yigit #define VTPCI_OPS(hw) (crypto_virtio_hw_internal[(hw)->dev_id].vtpci_ops)
2062c449644SFerruh Yigit #define VTPCI_IO(hw) (&crypto_virtio_hw_internal[(hw)->dev_id].io)
20725500d4bSJay Zhou
2082c449644SFerruh Yigit extern struct virtio_hw_internal crypto_virtio_hw_internal[RTE_MAX_VIRTIO_CRYPTO];
20925500d4bSJay Zhou
21025500d4bSJay Zhou /*
21125500d4bSJay Zhou * How many bits to shift physical queue address written to QUEUE_PFN.
21225500d4bSJay Zhou * 12 is historical, and due to x86 page size.
21325500d4bSJay Zhou */
21425500d4bSJay Zhou #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
21525500d4bSJay Zhou
21625500d4bSJay Zhou /* The alignment to use between consumer and producer parts of vring. */
21725500d4bSJay Zhou #define VIRTIO_PCI_VRING_ALIGN 4096
21825500d4bSJay Zhou
21925500d4bSJay Zhou enum virtio_msix_status {
22025500d4bSJay Zhou VIRTIO_MSIX_NONE = 0,
22125500d4bSJay Zhou VIRTIO_MSIX_DISABLED = 1,
22225500d4bSJay Zhou VIRTIO_MSIX_ENABLED = 2
22325500d4bSJay Zhou };
22425500d4bSJay Zhou
22525500d4bSJay Zhou static inline int
vtpci_with_feature(struct virtio_crypto_hw * hw,uint64_t bit)22625500d4bSJay Zhou vtpci_with_feature(struct virtio_crypto_hw *hw, uint64_t bit)
22725500d4bSJay Zhou {
22825500d4bSJay Zhou return (hw->guest_features & (1ULL << bit)) != 0;
22925500d4bSJay Zhou }
23025500d4bSJay Zhou
23125500d4bSJay Zhou /*
23225500d4bSJay Zhou * Function declaration from virtio_pci.c
23325500d4bSJay Zhou */
23425500d4bSJay Zhou int vtpci_cryptodev_init(struct rte_pci_device *dev,
23525500d4bSJay Zhou struct virtio_crypto_hw *hw);
23625500d4bSJay Zhou void vtpci_cryptodev_reset(struct virtio_crypto_hw *hw);
23725500d4bSJay Zhou
23825500d4bSJay Zhou void vtpci_cryptodev_reinit_complete(struct virtio_crypto_hw *hw);
23925500d4bSJay Zhou
24025500d4bSJay Zhou uint8_t vtpci_cryptodev_get_status(struct virtio_crypto_hw *hw);
24125500d4bSJay Zhou void vtpci_cryptodev_set_status(struct virtio_crypto_hw *hw, uint8_t status);
24225500d4bSJay Zhou
24325500d4bSJay Zhou uint64_t vtpci_cryptodev_negotiate_features(struct virtio_crypto_hw *hw,
24425500d4bSJay Zhou uint64_t host_features);
24525500d4bSJay Zhou
24625500d4bSJay Zhou void vtpci_write_cryptodev_config(struct virtio_crypto_hw *hw, size_t offset,
24725500d4bSJay Zhou const void *src, int length);
24825500d4bSJay Zhou
24925500d4bSJay Zhou void vtpci_read_cryptodev_config(struct virtio_crypto_hw *hw, size_t offset,
25025500d4bSJay Zhou void *dst, int length);
25125500d4bSJay Zhou
25225500d4bSJay Zhou uint8_t vtpci_cryptodev_isr(struct virtio_crypto_hw *hw);
25325500d4bSJay Zhou
25425500d4bSJay Zhou #endif /* _VIRTIO_PCI_H_ */
255