12fc0202f78c68fbda7a035879faadfcc0cc4671
[dpdk.git] / drivers / net / virtio / virtio_pci.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _VIRTIO_PCI_H_
6 #define _VIRTIO_PCI_H_
7
8 #include <stdint.h>
9 #include <stdbool.h>
10
11 #include <rte_pci.h>
12 #include <rte_bus_pci.h>
13 #include <ethdev_driver.h>
14
15 #include "virtio.h"
16
17 struct virtqueue;
18 struct virtnet_ctl;
19
20 /* VirtIO PCI vendor/device ID. */
21 #define VIRTIO_PCI_VENDORID     0x1AF4
22 #define VIRTIO_PCI_LEGACY_DEVICEID_NET 0x1000
23 #define VIRTIO_PCI_MODERN_DEVICEID_NET 0x1041
24
25 /* VirtIO ABI version, this must match exactly. */
26 #define VIRTIO_PCI_ABI_VERSION 0
27
28 /*
29  * VirtIO Header, located in BAR 0.
30  */
31 #define VIRTIO_PCI_HOST_FEATURES  0  /* host's supported features (32bit, RO)*/
32 #define VIRTIO_PCI_GUEST_FEATURES 4  /* guest's supported features (32, RW) */
33 #define VIRTIO_PCI_QUEUE_PFN      8  /* physical address of VQ (32, RW) */
34 #define VIRTIO_PCI_QUEUE_NUM      12 /* number of ring entries (16, RO) */
35 #define VIRTIO_PCI_QUEUE_SEL      14 /* current VQ selection (16, RW) */
36 #define VIRTIO_PCI_QUEUE_NOTIFY   16 /* notify host regarding VQ (16, RW) */
37 #define VIRTIO_PCI_STATUS         18 /* device status register (8, RW) */
38 #define VIRTIO_PCI_ISR            19 /* interrupt status register, reading
39                                       * also clears the register (8, RO) */
40 /* Only if MSIX is enabled: */
41 #define VIRTIO_MSI_CONFIG_VECTOR  20 /* configuration change vector (16, RW) */
42 #define VIRTIO_MSI_QUEUE_VECTOR   22 /* vector for selected VQ notifications
43                                       (16, RW) */
44
45 /* The bit of the ISR which indicates a device has an interrupt. */
46 #define VIRTIO_PCI_ISR_INTR   0x1
47 /* The bit of the ISR which indicates a device configuration change. */
48 #define VIRTIO_PCI_ISR_CONFIG 0x2
49 /* Vector value used to disable MSI for queue. */
50 #define VIRTIO_MSI_NO_VECTOR 0xFFFF
51
52 /* VirtIO device IDs. */
53 #define VIRTIO_ID_NETWORK  0x01
54 #define VIRTIO_ID_BLOCK    0x02
55 #define VIRTIO_ID_CONSOLE  0x03
56 #define VIRTIO_ID_ENTROPY  0x04
57 #define VIRTIO_ID_BALLOON  0x05
58 #define VIRTIO_ID_IOMEMORY 0x06
59 #define VIRTIO_ID_9P       0x09
60
61 /* Status byte for guest to report progress. */
62 #define VIRTIO_CONFIG_STATUS_RESET              0x00
63 #define VIRTIO_CONFIG_STATUS_ACK                0x01
64 #define VIRTIO_CONFIG_STATUS_DRIVER             0x02
65 #define VIRTIO_CONFIG_STATUS_DRIVER_OK          0x04
66 #define VIRTIO_CONFIG_STATUS_FEATURES_OK        0x08
67 #define VIRTIO_CONFIG_STATUS_DEV_NEED_RESET     0x40
68 #define VIRTIO_CONFIG_STATUS_FAILED             0x80
69
70 /*
71  * Each virtqueue indirect descriptor list must be physically contiguous.
72  * To allow us to malloc(9) each list individually, limit the number
73  * supported to what will fit in one page. With 4KB pages, this is a limit
74  * of 256 descriptors. If there is ever a need for more, we can switch to
75  * contigmalloc(9) for the larger allocations, similar to what
76  * bus_dmamem_alloc(9) does.
77  *
78  * Note the sizeof(struct vring_desc) is 16 bytes.
79  */
80 #define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16))
81
82 /* The feature bitmap for virtio net */
83 #define VIRTIO_NET_F_CSUM       0       /* Host handles pkts w/ partial csum */
84 #define VIRTIO_NET_F_GUEST_CSUM 1       /* Guest handles pkts w/ partial csum */
85 #define VIRTIO_NET_F_MTU        3       /* Initial MTU advice. */
86 #define VIRTIO_NET_F_MAC        5       /* Host has given MAC address. */
87 #define VIRTIO_NET_F_GUEST_TSO4 7       /* Guest can handle TSOv4 in. */
88 #define VIRTIO_NET_F_GUEST_TSO6 8       /* Guest can handle TSOv6 in. */
89 #define VIRTIO_NET_F_GUEST_ECN  9       /* Guest can handle TSO[6] w/ ECN in. */
90 #define VIRTIO_NET_F_GUEST_UFO  10      /* Guest can handle UFO in. */
91 #define VIRTIO_NET_F_HOST_TSO4  11      /* Host can handle TSOv4 in. */
92 #define VIRTIO_NET_F_HOST_TSO6  12      /* Host can handle TSOv6 in. */
93 #define VIRTIO_NET_F_HOST_ECN   13      /* Host can handle TSO[6] w/ ECN in. */
94 #define VIRTIO_NET_F_HOST_UFO   14      /* Host can handle UFO in. */
95 #define VIRTIO_NET_F_MRG_RXBUF  15      /* Host can merge receive buffers. */
96 #define VIRTIO_NET_F_STATUS     16      /* virtio_net_config.status available */
97 #define VIRTIO_NET_F_CTRL_VQ    17      /* Control channel available */
98 #define VIRTIO_NET_F_CTRL_RX    18      /* Control channel RX mode support */
99 #define VIRTIO_NET_F_CTRL_VLAN  19      /* Control channel VLAN filtering */
100 #define VIRTIO_NET_F_CTRL_RX_EXTRA 20   /* Extra RX mode control support */
101 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21  /* Guest can announce device on the
102                                          * network */
103 #define VIRTIO_NET_F_MQ         22      /* Device supports Receive Flow
104                                          * Steering */
105 #define VIRTIO_NET_F_CTRL_MAC_ADDR 23   /* Set MAC address */
106
107 /* Do we get callbacks when the ring is completely used, even if we've
108  * suppressed them? */
109 #define VIRTIO_F_NOTIFY_ON_EMPTY        24
110
111 /* Can the device handle any descriptor layout? */
112 #define VIRTIO_F_ANY_LAYOUT             27
113
114 /* We support indirect buffer descriptors */
115 #define VIRTIO_RING_F_INDIRECT_DESC     28
116
117 #define VIRTIO_F_VERSION_1              32
118 #define VIRTIO_F_IOMMU_PLATFORM 33
119 #define VIRTIO_F_RING_PACKED            34
120
121 /*
122  * Some VirtIO feature bits (currently bits 28 through 31) are
123  * reserved for the transport being used (eg. virtio_ring), the
124  * rest are per-device feature bits.
125  */
126 #define VIRTIO_TRANSPORT_F_START 28
127 #define VIRTIO_TRANSPORT_F_END   34
128
129 /*
130  * Inorder feature indicates that all buffers are used by the device
131  * in the same order in which they have been made available.
132  */
133 #define VIRTIO_F_IN_ORDER 35
134
135 /*
136  * This feature indicates that memory accesses by the driver and the device
137  * are ordered in a way described by the platform.
138  */
139 #define VIRTIO_F_ORDER_PLATFORM 36
140
141 /*
142  * This feature indicates that the driver passes extra data (besides
143  * identifying the virtqueue) in its device notifications.
144  */
145 #define VIRTIO_F_NOTIFICATION_DATA 38
146
147 /* Device set linkspeed and duplex */
148 #define VIRTIO_NET_F_SPEED_DUPLEX 63
149
150 /* The Guest publishes the used index for which it expects an interrupt
151  * at the end of the avail ring. Host should ignore the avail->flags field. */
152 /* The Host publishes the avail index for which it expects a kick
153  * at the end of the used ring. Guest should ignore the used->flags field. */
154 #define VIRTIO_RING_F_EVENT_IDX         29
155
156 #define VIRTIO_NET_S_LINK_UP    1       /* Link is up */
157 #define VIRTIO_NET_S_ANNOUNCE   2       /* Announcement is needed */
158
159 /*
160  * Maximum number of virtqueues per device.
161  */
162 #define VIRTIO_MAX_VIRTQUEUE_PAIRS 8
163 #define VIRTIO_MAX_VIRTQUEUES (VIRTIO_MAX_VIRTQUEUE_PAIRS * 2 + 1)
164
165 /* Common configuration */
166 #define VIRTIO_PCI_CAP_COMMON_CFG       1
167 /* Notifications */
168 #define VIRTIO_PCI_CAP_NOTIFY_CFG       2
169 /* ISR Status */
170 #define VIRTIO_PCI_CAP_ISR_CFG          3
171 /* Device specific configuration */
172 #define VIRTIO_PCI_CAP_DEVICE_CFG       4
173 /* PCI configuration access */
174 #define VIRTIO_PCI_CAP_PCI_CFG          5
175
176 /* This is the PCI capability header: */
177 struct virtio_pci_cap {
178         uint8_t cap_vndr;               /* Generic PCI field: PCI_CAP_ID_VNDR */
179         uint8_t cap_next;               /* Generic PCI field: next ptr. */
180         uint8_t cap_len;                /* Generic PCI field: capability length */
181         uint8_t cfg_type;               /* Identifies the structure. */
182         uint8_t bar;                    /* Where to find it. */
183         uint8_t padding[3];             /* Pad to full dword. */
184         uint32_t offset;                /* Offset within bar. */
185         uint32_t length;                /* Length of the structure, in bytes. */
186 };
187
188 struct virtio_pci_notify_cap {
189         struct virtio_pci_cap cap;
190         uint32_t notify_off_multiplier; /* Multiplier for queue_notify_off. */
191 };
192
193 /* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
194 struct virtio_pci_common_cfg {
195         /* About the whole device. */
196         uint32_t device_feature_select; /* read-write */
197         uint32_t device_feature;        /* read-only */
198         uint32_t guest_feature_select;  /* read-write */
199         uint32_t guest_feature;         /* read-write */
200         uint16_t msix_config;           /* read-write */
201         uint16_t num_queues;            /* read-only */
202         uint8_t device_status;          /* read-write */
203         uint8_t config_generation;      /* read-only */
204
205         /* About a specific virtqueue. */
206         uint16_t queue_select;          /* read-write */
207         uint16_t queue_size;            /* read-write, power of 2. */
208         uint16_t queue_msix_vector;     /* read-write */
209         uint16_t queue_enable;          /* read-write */
210         uint16_t queue_notify_off;      /* read-only */
211         uint32_t queue_desc_lo;         /* read-write */
212         uint32_t queue_desc_hi;         /* read-write */
213         uint32_t queue_avail_lo;        /* read-write */
214         uint32_t queue_avail_hi;        /* read-write */
215         uint32_t queue_used_lo;         /* read-write */
216         uint32_t queue_used_hi;         /* read-write */
217 };
218
219 struct virtio_pci_dev {
220         struct virtio_hw hw;
221         struct rte_pci_device *pci_dev;
222         struct virtio_pci_common_cfg *common_cfg;
223         struct virtio_net_config *dev_cfg;
224         uint8_t *isr;
225         uint16_t *notify_base;
226         uint32_t notify_off_multiplier;
227         bool modern;
228 };
229
230 #define virtio_pci_get_dev(hwp) container_of(hwp, struct virtio_pci_dev, hw)
231
232 /*
233  * This structure is just a reference to read
234  * net device specific config space; it just a chodu structure
235  *
236  */
237 struct virtio_net_config {
238         /* The config defining mac address (if VIRTIO_NET_F_MAC) */
239         uint8_t    mac[RTE_ETHER_ADDR_LEN];
240         /* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
241         uint16_t   status;
242         uint16_t   max_virtqueue_pairs;
243         uint16_t   mtu;
244         /*
245          * speed, in units of 1Mb. All values 0 to INT_MAX are legal.
246          * Any other value stands for unknown.
247          */
248         uint32_t speed;
249         /*
250          * 0x00 - half duplex
251          * 0x01 - full duplex
252          * Any other value stands for unknown.
253          */
254         uint8_t duplex;
255
256 } __rte_packed;
257
258 /*
259  * How many bits to shift physical queue address written to QUEUE_PFN.
260  * 12 is historical, and due to x86 page size.
261  */
262 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
263
264 /* The alignment to use between consumer and producer parts of vring. */
265 #define VIRTIO_PCI_VRING_ALIGN 4096
266
267 enum virtio_msix_status {
268         VIRTIO_MSIX_NONE = 0,
269         VIRTIO_MSIX_DISABLED = 1,
270         VIRTIO_MSIX_ENABLED = 2
271 };
272
273 static inline int
274 vtpci_with_feature(struct virtio_hw *hw, uint64_t bit)
275 {
276         return (hw->guest_features & (1ULL << bit)) != 0;
277 }
278
279 static inline int
280 vtpci_packed_queue(struct virtio_hw *hw)
281 {
282         return vtpci_with_feature(hw, VIRTIO_F_RING_PACKED);
283 }
284
285 /*
286  * Function declaration from virtio_pci.c
287  */
288 int vtpci_init(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev);
289 void vtpci_reset(struct virtio_hw *);
290
291 void vtpci_reinit_complete(struct virtio_hw *);
292
293 uint8_t vtpci_get_status(struct virtio_hw *);
294 void vtpci_set_status(struct virtio_hw *, uint8_t);
295
296 uint64_t vtpci_negotiate_features(struct virtio_hw *, uint64_t);
297
298 void vtpci_write_dev_config(struct virtio_hw *, size_t, const void *, int);
299
300 void vtpci_read_dev_config(struct virtio_hw *, size_t, void *, int);
301
302 uint8_t vtpci_isr(struct virtio_hw *);
303
304 void vtpci_legacy_ioport_unmap(struct virtio_hw *hw);
305 int vtpci_legacy_ioport_map(struct virtio_hw *hw);
306
307 extern const struct virtio_ops legacy_ops;
308 extern const struct virtio_ops modern_ops;
309
310 #endif /* _VIRTIO_PCI_H_ */