net/virtio: move virtqueue defines in generic header
[dpdk.git] / drivers / net / virtio / virtio.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  * Copyright(c) 2021 Red Hat, Inc.
4  */
5
6 #ifndef _VIRTIO_H_
7 #define _VIRTIO_H_
8
9 #include <rte_ether.h>
10
11 /* The feature bitmap for virtio net */
12 #define VIRTIO_NET_F_CSUM       0       /* Host handles pkts w/ partial csum */
13 #define VIRTIO_NET_F_GUEST_CSUM 1       /* Guest handles pkts w/ partial csum */
14 #define VIRTIO_NET_F_MTU        3       /* Initial MTU advice. */
15 #define VIRTIO_NET_F_MAC        5       /* Host has given MAC address. */
16 #define VIRTIO_NET_F_GUEST_TSO4 7       /* Guest can handle TSOv4 in. */
17 #define VIRTIO_NET_F_GUEST_TSO6 8       /* Guest can handle TSOv6 in. */
18 #define VIRTIO_NET_F_GUEST_ECN  9       /* Guest can handle TSO[6] w/ ECN in. */
19 #define VIRTIO_NET_F_GUEST_UFO  10      /* Guest can handle UFO in. */
20 #define VIRTIO_NET_F_HOST_TSO4  11      /* Host can handle TSOv4 in. */
21 #define VIRTIO_NET_F_HOST_TSO6  12      /* Host can handle TSOv6 in. */
22 #define VIRTIO_NET_F_HOST_ECN   13      /* Host can handle TSO[6] w/ ECN in. */
23 #define VIRTIO_NET_F_HOST_UFO   14      /* Host can handle UFO in. */
24 #define VIRTIO_NET_F_MRG_RXBUF  15      /* Host can merge receive buffers. */
25 #define VIRTIO_NET_F_STATUS     16      /* virtio_net_config.status available */
26 #define VIRTIO_NET_F_CTRL_VQ    17      /* Control channel available */
27 #define VIRTIO_NET_F_CTRL_RX    18      /* Control channel RX mode support */
28 #define VIRTIO_NET_F_CTRL_VLAN  19      /* Control channel VLAN filtering */
29 #define VIRTIO_NET_F_CTRL_RX_EXTRA 20   /* Extra RX mode control support */
30 #define VIRTIO_NET_F_GUEST_ANNOUNCE 21  /* Guest can announce device on the network */
31 #define VIRTIO_NET_F_MQ         22      /* Device supports Receive Flow Steering */
32 #define VIRTIO_NET_F_CTRL_MAC_ADDR 23   /* Set MAC address */
33
34 /*
35  * Do we get callbacks when the ring is completely used,
36  * even if we've suppressed them?
37  */
38 #define VIRTIO_F_NOTIFY_ON_EMPTY        24
39
40 /* Can the device handle any descriptor layout? */
41 #define VIRTIO_F_ANY_LAYOUT             27
42
43 /* We support indirect buffer descriptors */
44 #define VIRTIO_RING_F_INDIRECT_DESC     28
45
46 #define VIRTIO_F_VERSION_1              32
47 #define VIRTIO_F_IOMMU_PLATFORM 33
48 #define VIRTIO_F_RING_PACKED            34
49
50 /*
51  * Some VirtIO feature bits (currently bits 28 through 31) are
52  * reserved for the transport being used (eg. virtio_ring), the
53  * rest are per-device feature bits.
54  */
55 #define VIRTIO_TRANSPORT_F_START 28
56 #define VIRTIO_TRANSPORT_F_END   34
57
58 /*
59  * Inorder feature indicates that all buffers are used by the device
60  * in the same order in which they have been made available.
61  */
62 #define VIRTIO_F_IN_ORDER 35
63
64 /*
65  * This feature indicates that memory accesses by the driver and the device
66  * are ordered in a way described by the platform.
67  */
68 #define VIRTIO_F_ORDER_PLATFORM 36
69
70 /*
71  * This feature indicates that the driver passes extra data (besides
72  * identifying the virtqueue) in its device notifications.
73  */
74 #define VIRTIO_F_NOTIFICATION_DATA 38
75
76 /* Device set linkspeed and duplex */
77 #define VIRTIO_NET_F_SPEED_DUPLEX 63
78
79 /*
80  * The Guest publishes the used index for which it expects an interrupt
81  * at the end of the avail ring. Host should ignore the avail->flags field
82  *
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.
85  */
86 #define VIRTIO_RING_F_EVENT_IDX         29
87
88 #define VIRTIO_NET_S_LINK_UP    1       /* Link is up */
89 #define VIRTIO_NET_S_ANNOUNCE   2       /* Announcement is needed */
90
91 /*
92  * Each virtqueue indirect descriptor list must be physically contiguous.
93  * To allow us to malloc(9) each list individually, limit the number
94  * supported to what will fit in one page. With 4KB pages, this is a limit
95  * of 256 descriptors. If there is ever a need for more, we can switch to
96  * contigmalloc(9) for the larger allocations, similar to what
97  * bus_dmamem_alloc(9) does.
98  *
99  * Note the sizeof(struct vring_desc) is 16 bytes.
100  */
101 #define VIRTIO_MAX_INDIRECT ((int)(PAGE_SIZE / 16))
102
103 /*
104  * Maximum number of virtqueues per device.
105  */
106 #define VIRTIO_MAX_VIRTQUEUE_PAIRS 8
107 #define VIRTIO_MAX_VIRTQUEUES (VIRTIO_MAX_VIRTQUEUE_PAIRS * 2 + 1)
108
109 struct virtio_hw {
110         struct virtqueue **vqs;
111         uint64_t guest_features;
112         uint16_t vtnet_hdr_size;
113         uint8_t started;
114         uint8_t weak_barriers;
115         uint8_t vlan_strip;
116         uint8_t has_tx_offload;
117         uint8_t has_rx_offload;
118         uint8_t use_vec_rx;
119         uint8_t use_vec_tx;
120         uint8_t use_inorder_rx;
121         uint8_t use_inorder_tx;
122         uint8_t opened;
123         uint16_t port_id;
124         uint8_t mac_addr[RTE_ETHER_ADDR_LEN];
125         uint32_t speed;  /* link speed in MB */
126         uint8_t duplex;
127         uint8_t use_msix;
128         uint16_t max_mtu;
129         /*
130          * App management thread and virtio interrupt handler thread
131          * both can change device state, this lock is meant to avoid
132          * such a contention.
133          */
134         rte_spinlock_t state_lock;
135         struct rte_mbuf **inject_pkts;
136         uint16_t max_queue_pairs;
137         uint64_t req_guest_features;
138         struct virtnet_ctl *cvq;
139 };
140
141 struct virtio_ops {
142         void (*read_dev_cfg)(struct virtio_hw *hw, size_t offset, void *dst, int len);
143         void (*write_dev_cfg)(struct virtio_hw *hw, size_t offset, const void *src, int len);
144         uint8_t (*get_status)(struct virtio_hw *hw);
145         void (*set_status)(struct virtio_hw *hw, uint8_t status);
146         uint64_t (*get_features)(struct virtio_hw *hw);
147         void (*set_features)(struct virtio_hw *hw, uint64_t features);
148         int (*features_ok)(struct virtio_hw *hw);
149         uint8_t (*get_isr)(struct virtio_hw *hw);
150         uint16_t (*set_config_irq)(struct virtio_hw *hw, uint16_t vec);
151         uint16_t (*set_queue_irq)(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec);
152         uint16_t (*get_queue_num)(struct virtio_hw *hw, uint16_t queue_id);
153         int (*setup_queue)(struct virtio_hw *hw, struct virtqueue *vq);
154         void (*del_queue)(struct virtio_hw *hw, struct virtqueue *vq);
155         void (*notify_queue)(struct virtio_hw *hw, struct virtqueue *vq);
156         void (*intr_detect)(struct virtio_hw *hw);
157         int (*dev_close)(struct virtio_hw *hw);
158 };
159
160 /*
161  * This structure stores per-process data. Only virtio_ops for now.
162  */
163 struct virtio_hw_internal {
164         const struct virtio_ops *virtio_ops;
165 };
166
167 #define VIRTIO_OPS(hw)  (virtio_hw_internal[(hw)->port_id].virtio_ops)
168
169 extern struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
170
171
172 static inline int
173 virtio_with_feature(struct virtio_hw *hw, uint64_t bit)
174 {
175         return (hw->guest_features & (1ULL << bit)) != 0;
176 }
177
178 static inline int
179 virtio_with_packed_queue(struct virtio_hw *hw)
180 {
181         return virtio_with_feature(hw, VIRTIO_F_RING_PACKED);
182 }
183
184 uint64_t virtio_negotiate_features(struct virtio_hw *hw, uint64_t host_features);
185
186 #endif /* _VIRTIO_H_ */