xref: /dpdk/drivers/net/virtio/virtio_pci.h (revision 1f37cb2bb46b1fd403faa7c3bf8884e6a4dfde66)
15566a3e3SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
25566a3e3SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
36c3169a3SBruce Richardson  */
46c3169a3SBruce Richardson 
56c3169a3SBruce Richardson #ifndef _VIRTIO_PCI_H_
66c3169a3SBruce Richardson #define _VIRTIO_PCI_H_
76c3169a3SBruce Richardson 
86c3169a3SBruce Richardson #include <stdint.h>
9db8d6790SMaxime Coquelin #include <stdbool.h>
106c3169a3SBruce Richardson 
11b8f04520SDavid Marchand #include <rte_pci.h>
12*1f37cb2bSDavid Marchand #include <bus_pci_driver.h>
13df96fd0dSBruce Richardson #include <ethdev_driver.h>
146c3169a3SBruce Richardson 
15f8b60756SMaxime Coquelin #include "virtio.h"
16f8b60756SMaxime Coquelin 
176c3169a3SBruce Richardson struct virtqueue;
1801ad44fdSHuawei Xie struct virtnet_ctl;
196c3169a3SBruce Richardson 
206c3169a3SBruce Richardson /* VirtIO PCI vendor/device ID. */
216c3169a3SBruce Richardson #define VIRTIO_PCI_VENDORID     0x1AF4
224c790365SJason Wang #define VIRTIO_PCI_LEGACY_DEVICEID_NET 0x1000
234c790365SJason Wang #define VIRTIO_PCI_MODERN_DEVICEID_NET 0x1041
246c3169a3SBruce Richardson 
256c3169a3SBruce Richardson /* VirtIO ABI version, this must match exactly. */
266c3169a3SBruce Richardson #define VIRTIO_PCI_ABI_VERSION 0
276c3169a3SBruce Richardson 
286c3169a3SBruce Richardson /*
296c3169a3SBruce Richardson  * VirtIO Header, located in BAR 0.
306c3169a3SBruce Richardson  */
316c3169a3SBruce Richardson #define VIRTIO_PCI_HOST_FEATURES  0  /* host's supported features (32bit, RO)*/
326c3169a3SBruce Richardson #define VIRTIO_PCI_GUEST_FEATURES 4  /* guest's supported features (32, RW) */
336c3169a3SBruce Richardson #define VIRTIO_PCI_QUEUE_PFN      8  /* physical address of VQ (32, RW) */
346c3169a3SBruce Richardson #define VIRTIO_PCI_QUEUE_NUM      12 /* number of ring entries (16, RO) */
356c3169a3SBruce Richardson #define VIRTIO_PCI_QUEUE_SEL      14 /* current VQ selection (16, RW) */
366c3169a3SBruce Richardson #define VIRTIO_PCI_QUEUE_NOTIFY   16 /* notify host regarding VQ (16, RW) */
376c3169a3SBruce Richardson #define VIRTIO_PCI_STATUS         18 /* device status register (8, RW) */
386c3169a3SBruce Richardson #define VIRTIO_PCI_ISR		  19 /* interrupt status register, reading
396c3169a3SBruce Richardson 				      * also clears the register (8, RO) */
406c3169a3SBruce Richardson /* Only if MSIX is enabled: */
416c3169a3SBruce Richardson #define VIRTIO_MSI_CONFIG_VECTOR  20 /* configuration change vector (16, RW) */
426c3169a3SBruce Richardson #define VIRTIO_MSI_QUEUE_VECTOR	  22 /* vector for selected VQ notifications
436c3169a3SBruce Richardson 				      (16, RW) */
446c3169a3SBruce Richardson 
456ba1f63bSYuanhan Liu /* Common configuration */
466ba1f63bSYuanhan Liu #define VIRTIO_PCI_CAP_COMMON_CFG	1
476ba1f63bSYuanhan Liu /* Notifications */
486ba1f63bSYuanhan Liu #define VIRTIO_PCI_CAP_NOTIFY_CFG	2
496ba1f63bSYuanhan Liu /* ISR Status */
506ba1f63bSYuanhan Liu #define VIRTIO_PCI_CAP_ISR_CFG		3
516ba1f63bSYuanhan Liu /* Device specific configuration */
526ba1f63bSYuanhan Liu #define VIRTIO_PCI_CAP_DEVICE_CFG	4
536ba1f63bSYuanhan Liu /* PCI configuration access */
546ba1f63bSYuanhan Liu #define VIRTIO_PCI_CAP_PCI_CFG		5
556ba1f63bSYuanhan Liu 
566ba1f63bSYuanhan Liu /* This is the PCI capability header: */
576ba1f63bSYuanhan Liu struct virtio_pci_cap {
586ba1f63bSYuanhan Liu 	uint8_t cap_vndr;		/* Generic PCI field: PCI_CAP_ID_VNDR */
596ba1f63bSYuanhan Liu 	uint8_t cap_next;		/* Generic PCI field: next ptr. */
606ba1f63bSYuanhan Liu 	uint8_t cap_len;		/* Generic PCI field: capability length */
616ba1f63bSYuanhan Liu 	uint8_t cfg_type;		/* Identifies the structure. */
626ba1f63bSYuanhan Liu 	uint8_t bar;			/* Where to find it. */
636ba1f63bSYuanhan Liu 	uint8_t padding[3];		/* Pad to full dword. */
646ba1f63bSYuanhan Liu 	uint32_t offset;		/* Offset within bar. */
656ba1f63bSYuanhan Liu 	uint32_t length;		/* Length of the structure, in bytes. */
666ba1f63bSYuanhan Liu };
676ba1f63bSYuanhan Liu 
686ba1f63bSYuanhan Liu struct virtio_pci_notify_cap {
696ba1f63bSYuanhan Liu 	struct virtio_pci_cap cap;
706ba1f63bSYuanhan Liu 	uint32_t notify_off_multiplier;	/* Multiplier for queue_notify_off. */
716ba1f63bSYuanhan Liu };
726ba1f63bSYuanhan Liu 
736ba1f63bSYuanhan Liu /* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
746ba1f63bSYuanhan Liu struct virtio_pci_common_cfg {
756ba1f63bSYuanhan Liu 	/* About the whole device. */
766ba1f63bSYuanhan Liu 	uint32_t device_feature_select;	/* read-write */
776ba1f63bSYuanhan Liu 	uint32_t device_feature;	/* read-only */
786ba1f63bSYuanhan Liu 	uint32_t guest_feature_select;	/* read-write */
796ba1f63bSYuanhan Liu 	uint32_t guest_feature;		/* read-write */
806ba1f63bSYuanhan Liu 	uint16_t msix_config;		/* read-write */
816ba1f63bSYuanhan Liu 	uint16_t num_queues;		/* read-only */
826ba1f63bSYuanhan Liu 	uint8_t device_status;		/* read-write */
836ba1f63bSYuanhan Liu 	uint8_t config_generation;	/* read-only */
846ba1f63bSYuanhan Liu 
856ba1f63bSYuanhan Liu 	/* About a specific virtqueue. */
866ba1f63bSYuanhan Liu 	uint16_t queue_select;		/* read-write */
876ba1f63bSYuanhan Liu 	uint16_t queue_size;		/* read-write, power of 2. */
886ba1f63bSYuanhan Liu 	uint16_t queue_msix_vector;	/* read-write */
896ba1f63bSYuanhan Liu 	uint16_t queue_enable;		/* read-write */
906ba1f63bSYuanhan Liu 	uint16_t queue_notify_off;	/* read-only */
916ba1f63bSYuanhan Liu 	uint32_t queue_desc_lo;		/* read-write */
926ba1f63bSYuanhan Liu 	uint32_t queue_desc_hi;		/* read-write */
936ba1f63bSYuanhan Liu 	uint32_t queue_avail_lo;	/* read-write */
946ba1f63bSYuanhan Liu 	uint32_t queue_avail_hi;	/* read-write */
956ba1f63bSYuanhan Liu 	uint32_t queue_used_lo;		/* read-write */
966ba1f63bSYuanhan Liu 	uint32_t queue_used_hi;		/* read-write */
976ba1f63bSYuanhan Liu };
986ba1f63bSYuanhan Liu 
996a504290SMaxime Coquelin enum virtio_msix_status {
1006a504290SMaxime Coquelin 	VIRTIO_MSIX_NONE = 0,
1016a504290SMaxime Coquelin 	VIRTIO_MSIX_DISABLED = 1,
1026a504290SMaxime Coquelin 	VIRTIO_MSIX_ENABLED = 2
1036a504290SMaxime Coquelin };
1046a504290SMaxime Coquelin 
105f305ecbbSMaxime Coquelin struct virtio_pci_dev {
106f305ecbbSMaxime Coquelin 	struct virtio_hw hw;
107266ece29SMaxime Coquelin 	struct virtio_pci_common_cfg *common_cfg;
108266ece29SMaxime Coquelin 	struct virtio_net_config *dev_cfg;
1096a504290SMaxime Coquelin 	enum virtio_msix_status msix_status;
110266ece29SMaxime Coquelin 	uint8_t *isr;
111266ece29SMaxime Coquelin 	uint16_t *notify_base;
112266ece29SMaxime Coquelin 	uint32_t notify_off_multiplier;
1131ac79346SMaxime Coquelin 	bool modern;
114f305ecbbSMaxime Coquelin };
115f305ecbbSMaxime Coquelin 
116f305ecbbSMaxime Coquelin #define virtio_pci_get_dev(hwp) container_of(hwp, struct virtio_pci_dev, hw)
117553f4593SYuanhan Liu 
118aa0d4b8aSMaxime Coquelin struct virtio_pci_internal {
119aa0d4b8aSMaxime Coquelin 	struct rte_pci_ioport io;
120aa0d4b8aSMaxime Coquelin 	struct rte_pci_device *dev;
121aa0d4b8aSMaxime Coquelin };
122aa0d4b8aSMaxime Coquelin 
123aa0d4b8aSMaxime Coquelin extern struct virtio_pci_internal virtio_pci_internal[RTE_MAX_ETHPORTS];
124aa0d4b8aSMaxime Coquelin 
125aa0d4b8aSMaxime Coquelin #define VTPCI_IO(hw) (&virtio_pci_internal[(hw)->port_id].io)
126aa0d4b8aSMaxime Coquelin #define VTPCI_DEV(hw) (virtio_pci_internal[(hw)->port_id].dev)
127aa0d4b8aSMaxime Coquelin 
128aa0d4b8aSMaxime Coquelin 
129553f4593SYuanhan Liu /*
1306c3169a3SBruce Richardson  * How many bits to shift physical queue address written to QUEUE_PFN.
1316c3169a3SBruce Richardson  * 12 is historical, and due to x86 page size.
1326c3169a3SBruce Richardson  */
1336c3169a3SBruce Richardson #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
1346c3169a3SBruce Richardson 
1356c3169a3SBruce Richardson /*
1366c3169a3SBruce Richardson  * Function declaration from virtio_pci.c
1376c3169a3SBruce Richardson  */
1381ac79346SMaxime Coquelin int vtpci_init(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev);
139c8d4b02fSMaxime Coquelin void vtpci_legacy_ioport_unmap(struct virtio_hw *hw);
140c8d4b02fSMaxime Coquelin int vtpci_legacy_ioport_map(struct virtio_hw *hw);
141c8d4b02fSMaxime Coquelin 
142f8b60756SMaxime Coquelin extern const struct virtio_ops legacy_ops;
143f8b60756SMaxime Coquelin extern const struct virtio_ops modern_ops;
1446d890f8aSYuanhan Liu 
1456c3169a3SBruce Richardson #endif /* _VIRTIO_PCI_H_ */
146