blob: f9a58280aae854a1c30ea76060fa6285898a2f1b [file] [log] [blame]
Mohsin Kazmid6c15af2018-10-23 18:00:47 +02001/*
2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef __included_virtio_pci_h__
17#define __included_virtio_pci_h__
18
19/* VirtIO ABI version, this must match exactly. */
20#define VIRTIO_PCI_ABI_VERSION 0
21
22/*
Mohsin Kazmib74fe322019-01-31 13:50:56 +000023 * Vector value used to disable MSI for queue.
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020024 * define in include/linux/virtio_pci.h
25 * #define VIRTIO_MSI_NO_VECTOR 0xFFFF
26 */
27
28/* The bit of the ISR which indicates a device has an interrupt. */
29#define VIRTIO_PCI_ISR_INTR 0x1
30/* The bit of the ISR which indicates a device configuration change. */
31#define VIRTIO_PCI_ISR_CONFIG 0x2
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020032
33/* VirtIO device IDs. */
34#define VIRTIO_ID_NETWORK 0x01
35
36/* Status byte for guest to report progress. */
37#define foreach_virtio_config_status_flags \
38 _ (VIRTIO_CONFIG_STATUS_RESET, 0x00) \
39 _ (VIRTIO_CONFIG_STATUS_ACK, 0x01) \
40 _ (VIRTIO_CONFIG_STATUS_DRIVER, 0x02) \
41 _ (VIRTIO_CONFIG_STATUS_DRIVER_OK, 0x04) \
42 _ (VIRTIO_CONFIG_STATUS_FEATURES_OK, 0x08) \
43 _ (VIRTIO_CONFIG_STATUS_DEVICE_NEEDS_RESET, 0x40) \
44 _ (VIRTIO_CONFIG_STATUS_FAILED, 0x80)
45
46typedef enum
47{
48#define _(a, b) a = b,
49 foreach_virtio_config_status_flags
50#undef _
51} virtio_config_status_flags_t;
52
53#define foreach_virtio_net_feature_flags \
54 _ (VIRTIO_NET_F_CSUM, 0) /* Host handles pkts w/ partial csum */ \
55 _ (VIRTIO_NET_F_GUEST_CSUM, 1) /* Guest handles pkts w/ partial csum */ \
56 _ (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, 2) /* Dynamic offload configuration. */ \
57 _ (VIRTIO_NET_F_MTU, 3) /* Initial MTU advice. */ \
58 _ (VIRTIO_NET_F_MAC, 5) /* Host has given MAC address. */ \
59 _ (VIRTIO_NET_F_GSO, 6) /* Host handles pkts w/ any GSO. */ \
60 _ (VIRTIO_NET_F_GUEST_TSO4, 7) /* Guest can handle TSOv4 in. */ \
61 _ (VIRTIO_NET_F_GUEST_TSO6, 8) /* Guest can handle TSOv6 in. */ \
62 _ (VIRTIO_NET_F_GUEST_ECN, 9) /* Guest can handle TSO[6] w/ ECN in. */ \
63 _ (VIRTIO_NET_F_GUEST_UFO, 10) /* Guest can handle UFO in. */ \
64 _ (VIRTIO_NET_F_HOST_TSO4, 11) /* Host can handle TSOv4 in. */ \
65 _ (VIRTIO_NET_F_HOST_TSO6, 12) /* Host can handle TSOv6 in. */ \
66 _ (VIRTIO_NET_F_HOST_ECN, 13) /* Host can handle TSO[6] w/ ECN in. */ \
67 _ (VIRTIO_NET_F_HOST_UFO, 14) /* Host can handle UFO in. */ \
68 _ (VIRTIO_NET_F_MRG_RXBUF, 15) /* Host can merge receive buffers. */ \
69 _ (VIRTIO_NET_F_STATUS, 16) /* virtio_net_config.status available */ \
70 _ (VIRTIO_NET_F_CTRL_VQ, 17) /* Control channel available */ \
71 _ (VIRTIO_NET_F_CTRL_RX, 18) /* Control channel RX mode support */ \
72 _ (VIRTIO_NET_F_CTRL_VLAN, 19) /* Control channel VLAN filtering */ \
73 _ (VIRTIO_NET_F_CTRL_RX_EXTRA, 20) /* Extra RX mode control support */ \
74 _ (VIRTIO_NET_F_GUEST_ANNOUNCE, 21) /* Guest can announce device on the network */ \
75 _ (VIRTIO_NET_F_MQ, 22) /* Device supports Receive Flow Steering */ \
76 _ (VIRTIO_NET_F_CTRL_MAC_ADDR, 23) /* Set MAC address */ \
77 _ (VIRTIO_F_NOTIFY_ON_EMPTY, 24) \
78 _ (VHOST_F_LOG_ALL, 26) /* Log all write descriptors */ \
79 _ (VIRTIO_F_ANY_LAYOUT, 27) /* Can the device handle any descripor layout */ \
80 _ (VIRTIO_RING_F_INDIRECT_DESC, 28) /* Support indirect buffer descriptors */ \
81 _ (VIRTIO_RING_F_EVENT_IDX, 29) /* The Guest publishes the used index for which it expects an interrupt \
82 * at the end of the avail ring. Host should ignore the avail->flags field. */ \
83/* The Host publishes the avail index for which it expects a kick \
84 * at the end of the used ring. Guest should ignore the used->flags field. */ \
Mohsin Kazmi379aac32020-08-20 10:25:12 +020085 _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \
86 _ (VIRTIO_F_VERSION_1, 32) \
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020087
Mohsin Kazmibbd6b742019-05-02 13:54:59 +020088#define VIRTIO_NET_F_CTRL_GUEST_OFFLOADS 2
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020089#define VIRTIO_NET_F_MTU 3
90#define VIRTIO_NET_S_LINK_UP 1 /* Link is up */
91#define VIRTIO_NET_S_ANNOUNCE 2 /* Announcement is needed */
92
Mohsin Kazmibbd6b742019-05-02 13:54:59 +020093/*
94 * Control network offloads
95 * Reconfigures the network offloads that Guest can handle.
96 * Available with the VIRTIO_NET_F_CTRL_GUEST_OFFLOADS feature bit.
97 * Command data format matches the feature bit mask exactly.
98 * See VIRTIO_NET_F_GUEST_* for the list of offloads
99 * that can be enabled/disabled.
100 */
101#define VIRTIO_NET_CTRL_GUEST_OFFLOADS 5
102#define VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET 0
103
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200104/* Common configuration */
105#define VIRTIO_PCI_CAP_COMMON_CFG 1
106/* Notifications */
107#define VIRTIO_PCI_CAP_NOTIFY_CFG 2
108/* ISR Status */
109#define VIRTIO_PCI_CAP_ISR_CFG 3
110/* Device specific configuration */
111#define VIRTIO_PCI_CAP_DEVICE_CFG 4
112/* PCI configuration access */
113#define VIRTIO_PCI_CAP_PCI_CFG 5
114
115#define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
116
117#define VIRTIO_PCI_VRING_ALIGN 4096
118
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200119typedef enum
120{
121 VIRTIO_MSIX_NONE = 0,
122 VIRTIO_MSIX_DISABLED = 1,
123 VIRTIO_MSIX_ENABLED = 2
124} virtio_msix_status_t;
125
126/* This is the PCI capability header: */
127typedef struct
128{
129 u8 cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */
130 u8 cap_next; /* Generic PCI field: next ptr. */
131 u8 cap_len; /* Generic PCI field: capability length */
132 u8 cfg_type; /* Identifies the structure. */
133 u8 bar; /* Where to find it. */
134 u8 padding[3]; /* Pad to full dword. */
135 u32 offset; /* Offset within bar. */
136 u32 length; /* Length of the structure, in bytes. */
137} virtio_pci_cap_t;
138
139typedef struct
140{
141 struct virtio_pci_cap cap;
142 u32 notify_off_multiplier; /* Multiplier for queue_notify_off. */
143} virtio_pci_notify_cap_t;
144
145/* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
146typedef struct
147{
148 /* About the whole device. */
149 u32 device_feature_select; /* read-write */
150 u32 device_feature; /* read-only */
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200151 u32 driver_feature_select; /* read-write */
152 u32 driver_feature; /* read-write */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200153 u16 msix_config; /* read-write */
154 u16 num_queues; /* read-only */
155 u8 device_status; /* read-write */
156 u8 config_generation; /* read-only */
157
158 /* About a specific virtqueue. */
159 u16 queue_select; /* read-write */
160 u16 queue_size; /* read-write, power of 2. */
161 u16 queue_msix_vector; /* read-write */
162 u16 queue_enable; /* read-write */
163 u16 queue_notify_off; /* read-only */
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200164 u64 queue_desc; /* read-write */
165 u64 queue_driver; /* read-write */
166 u64 queue_device; /* read-write */
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200167} virtio_pci_common_cfg_t;
168
169typedef struct
170{
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000171 u8 mac[6];
172 u16 status;
173 u16 max_virtqueue_pairs;
174 u16 mtu;
175} virtio_net_config_t;
176
177typedef struct
178{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200179 u64 addr;
180 u32 len;
181 u16 flags;
182 u16 next;
183} vring_desc_t;
184
185typedef struct
186{
187 u16 flags;
188 u16 idx;
189 u16 ring[0];
190 /* u16 used_event; */
191} vring_avail_t;
192
193typedef struct
194{
195 u32 id;
196 u32 len;
197} vring_used_elem_t;
198
199typedef struct
200{
201 u16 flags;
202 u16 idx;
203 vring_used_elem_t ring[0];
204 /* u16 avail_event; */
205} vring_used_t;
206
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000207typedef struct _virtio_pci_func
208{
209 void (*read_config) (vlib_main_t * vm, virtio_if_t * vif, void *dst,
210 int len, u32 addr);
211 void (*write_config) (vlib_main_t * vm, virtio_if_t * vif, void *src,
212 int len, u32 addr);
213
214 u64 (*get_device_features) (vlib_main_t * vm, virtio_if_t * vif);
215 u64 (*get_driver_features) (vlib_main_t * vm, virtio_if_t * vif);
216 void (*set_driver_features) (vlib_main_t * vm, virtio_if_t * vif,
217 u64 features);
218
219 u8 (*get_status) (vlib_main_t * vm, virtio_if_t * vif);
220 void (*set_status) (vlib_main_t * vm, virtio_if_t * vif, u8 status);
221 u8 (*device_reset) (vlib_main_t * vm, virtio_if_t * vif);
222
223 u8 (*get_isr) (vlib_main_t * vm, virtio_if_t * vif);
224
225 u16 (*get_queue_size) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id);
226 void (*set_queue_size) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id,
227 u16 queue_size);
228 u8 (*setup_queue) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id,
229 void *p);
230 void (*del_queue) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id);
231 void (*notify_queue) (vlib_main_t * vm, virtio_if_t * vif, u16 queue_id);
232
233 u16 (*set_config_irq) (vlib_main_t * vm, virtio_if_t * vif, u16 vec);
234 u16 (*set_queue_irq) (vlib_main_t * vm, virtio_if_t * vif, u16 vec,
235 u16 queue_id);
236
237 void (*get_mac) (vlib_main_t * vm, virtio_if_t * vif);
238 void (*set_mac) (vlib_main_t * vm, virtio_if_t * vif);
239 u16 (*get_device_status) (vlib_main_t * vm, virtio_if_t * vif);
240 u16 (*get_max_queue_pairs) (vlib_main_t * vm, virtio_if_t * vif);
241 u16 (*get_mtu) (vlib_main_t * vm, virtio_if_t * vif);
242 void (*device_debug_config_space) (vlib_main_t * vm, virtio_if_t * vif);
243} virtio_pci_func_t;
244
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200245typedef struct
246{
247 u32 addr;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200248 /* return */
249 i32 rv;
250 u32 sw_if_index;
251 u8 mac_addr_set;
252 u8 mac_addr[6];
253 u64 features;
Mohsin Kazmibbd6b742019-05-02 13:54:59 +0200254 u8 gso_enabled;
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000255 u8 checksum_offload_enabled;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200256 clib_error_t *error;
257} virtio_pci_create_if_args_t;
258
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000259extern const virtio_pci_func_t virtio_pci_legacy_func;
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200260extern const virtio_pci_func_t virtio_pci_modern_func;
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000261
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200262extern void device_status (vlib_main_t * vm, virtio_if_t * vif);
263void virtio_pci_create_if (vlib_main_t * vm,
264 virtio_pci_create_if_args_t * args);
265int virtio_pci_delete_if (vlib_main_t * vm, virtio_if_t * ad);
Mohsin Kazmi6d4af892020-01-03 15:11:53 +0000266int virtio_pci_enable_disable_offloads (vlib_main_t * vm, virtio_if_t * vif,
267 int gso_enabled,
268 int checksum_offload_enabled,
269 int offloads_disabled);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200270#endif /* __included_virtio_pci_h__ */
271/*
272 * fd.io coding-style-patch-verification: ON
273 *
274 * Local Variables:
275 * eval: (c-set-style "gnu")
276 * End:
277 */