add FreeBSD support
[dpdk.git] / lib / librte_pmd_virtio / virtio_pci.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _VIRTIO_PCI_H_
35 #define _VIRTIO_PCI_H_
36
37 #include <stdint.h>
38
39 #ifdef __FreeBSD__
40 #include <sys/types.h>
41 #include <machine/cpufunc.h>
42 #else
43 #include <sys/io.h>
44 #endif
45
46 #include <rte_ethdev.h>
47
48 struct virtqueue;
49
50 /* VirtIO PCI vendor/device ID. */
51 #define VIRTIO_PCI_VENDORID     0x1AF4
52 #define VIRTIO_PCI_DEVICEID_MIN 0x1000
53 #define VIRTIO_PCI_DEVICEID_MAX 0x103F
54
55 /* VirtIO ABI version, this must match exactly. */
56 #define VIRTIO_PCI_ABI_VERSION 0
57
58 /*
59  * VirtIO Header, located in BAR 0.
60  */
61 #define VIRTIO_PCI_HOST_FEATURES  0  /* host's supported features (32bit, RO)*/
62 #define VIRTIO_PCI_GUEST_FEATURES 4  /* guest's supported features (32, RW) */
63 #define VIRTIO_PCI_QUEUE_PFN      8  /* physical address of VQ (32, RW) */
64 #define VIRTIO_PCI_QUEUE_NUM      12 /* number of ring entries (16, RO) */
65 #define VIRTIO_PCI_QUEUE_SEL      14 /* current VQ selection (16, RW) */
66 #define VIRTIO_PCI_QUEUE_NOTIFY   16 /* notify host regarding VQ (16, RW) */
67 #define VIRTIO_PCI_STATUS         18 /* device status register (8, RW) */
68 #define VIRTIO_PCI_ISR            19 /* interrupt status register, reading
69                                       * also clears the register (8, RO) */
70 /* Only if MSIX is enabled: */
71 #define VIRTIO_MSI_CONFIG_VECTOR  20 /* configuration change vector (16, RW) */
72 #define VIRTIO_MSI_QUEUE_VECTOR   22 /* vector for selected VQ notifications
73                                       (16, RW) */
74
75 /* The bit of the ISR which indicates a device has an interrupt. */
76 #define VIRTIO_PCI_ISR_INTR   0x1
77 /* The bit of the ISR which indicates a device configuration change. */
78 #define VIRTIO_PCI_ISR_CONFIG 0x2
79 /* Vector value used to disable MSI for queue. */
80 #define VIRTIO_MSI_NO_VECTOR 0xFFFF
81
82 /* VirtIO device IDs. */
83 #define VIRTIO_ID_NETWORK  0x01
84 #define VIRTIO_ID_BLOCK    0x02
85 #define VIRTIO_ID_CONSOLE  0x03
86 #define VIRTIO_ID_ENTROPY  0x04
87 #define VIRTIO_ID_BALLOON  0x05
88 #define VIRTIO_ID_IOMEMORY 0x06
89 #define VIRTIO_ID_9P       0x09
90
91 /* Status byte for guest to report progress. */
92 #define VIRTIO_CONFIG_STATUS_RESET     0x00
93 #define VIRTIO_CONFIG_STATUS_ACK       0x01
94 #define VIRTIO_CONFIG_STATUS_DRIVER    0x02
95 #define VIRTIO_CONFIG_STATUS_DRIVER_OK 0x04
96 #define VIRTIO_CONFIG_STATUS_FAILED    0x80
97
98 /*
99  * Generate interrupt when the virtqueue ring is
100  * completely used, even if we've suppressed them.
101  */
102 #define VIRTIO_F_NOTIFY_ON_EMPTY (1 << 24)
103
104 /*
105  * The guest should never negotiate this feature; it
106  * is used to detect faulty drivers.
107  */
108 #define VIRTIO_F_BAD_FEATURE (1 << 30)
109
110 /*
111  * Some VirtIO feature bits (currently bits 28 through 31) are
112  * reserved for the transport being used (eg. virtio_ring), the
113  * rest are per-device feature bits.
114  */
115 #define VIRTIO_TRANSPORT_F_START 28
116 #define VIRTIO_TRANSPORT_F_END   32
117
118 /*
119  * Each virtqueue indirect descriptor list must be physically contiguous.
120  * To allow us to malloc(9) each list individually, limit the number
121  * supported to what will fit in one page. With 4KB pages, this is a limit
122  * of 256 descriptors. If there is ever a need for more, we can switch to
123  * contigmalloc(9) for the larger allocations, similar to what
124  * bus_dmamem_alloc(9) does.
125  *
126  * Note the sizeof(struct vring_desc) is 16 bytes.
127  */
128 #define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16))
129
130 /* The feature bitmap for virtio net */
131 #define VIRTIO_NET_F_CSUM       0x00001 /* Host handles pkts w/ partial csum */
132 #define VIRTIO_NET_F_GUEST_CSUM 0x00002 /* Guest handles pkts w/ partial csum*/
133 #define VIRTIO_NET_F_MAC        0x00020 /* Host has given MAC address. */
134 #define VIRTIO_NET_F_GSO        0x00040 /* Host handles pkts w/ any GSO type */
135 #define VIRTIO_NET_F_GUEST_TSO4 0x00080 /* Guest can handle TSOv4 in. */
136 #define VIRTIO_NET_F_GUEST_TSO6 0x00100 /* Guest can handle TSOv6 in. */
137 #define VIRTIO_NET_F_GUEST_ECN  0x00200 /* Guest can handle TSO[6] w/ ECN in.*/
138 #define VIRTIO_NET_F_GUEST_UFO  0x00400 /* Guest can handle UFO in. */
139 #define VIRTIO_NET_F_HOST_TSO4  0x00800 /* Host can handle TSOv4 in. */
140 #define VIRTIO_NET_F_HOST_TSO6  0x01000 /* Host can handle TSOv6 in. */
141 #define VIRTIO_NET_F_HOST_ECN   0x02000 /* Host can handle TSO[6] w/ ECN in. */
142 #define VIRTIO_NET_F_HOST_UFO   0x04000 /* Host can handle UFO in. */
143 #define VIRTIO_NET_F_MRG_RXBUF  0x08000 /* Host can merge receive buffers. */
144 #define VIRTIO_NET_F_STATUS     0x10000 /* virtio_net_config.status available*/
145 #define VIRTIO_NET_F_CTRL_VQ    0x20000 /* Control channel available */
146 #define VIRTIO_NET_F_CTRL_RX    0x40000 /* Control channel RX mode support */
147 #define VIRTIO_NET_F_CTRL_VLAN  0x80000 /* Control channel VLAN filtering */
148 #define VIRTIO_NET_F_CTRL_RX_EXTRA  0x100000 /* Extra RX mode control support */
149 #define VIRTIO_RING_F_INDIRECT_DESC 0x10000000 /* Support for indirect buffer descriptors. */
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  */
155 #define VIRTIO_RING_F_EVENT_IDX 0x20000000
156
157 #define VIRTIO_NET_S_LINK_UP 1 /* Link is up */
158
159 /*
160  * Maximum number of virtqueues per device.
161  */
162 #define VIRTIO_MAX_VIRTQUEUES 8
163
164 struct virtio_hw {
165         uint32_t    io_base;
166         uint32_t    host_features;
167         uint32_t    guest_features;
168         
169         struct virtqueue *cvq;
170
171         uint16_t    vtnet_hdr_size;
172
173         uint32_t    max_tx_queues;
174         uint32_t    max_rx_queues;
175         uint16_t    device_id;
176         uint16_t    vendor_id;
177         uint16_t    subsystem_device_id;
178         uint16_t    subsystem_vendor_id;
179         uint8_t     revision_id;
180         uint8_t     mac_addr[ETHER_ADDR_LEN];
181         int         adapter_stopped;
182         struct      rte_eth_stats eth_stats;
183 };
184
185 /*
186  * This structure is just a reference to read
187  * net device specific config space; it just a chodu structure
188  *
189  */
190 struct virtio_net_config {
191         /* The config defining mac address (if VIRTIO_NET_F_MAC) */
192         uint8_t    mac[ETHER_ADDR_LEN];
193         /* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
194         uint16_t   status;
195 };
196 /* Value indicated in device config */
197 #define VIRTIO_PCI_FLAG_MSIX  0x0020
198 /*
199  * The remaining space is defined by each driver as the per-driver
200  * configuration space.
201  */
202 #define VIRTIO_PCI_CONFIG(hw) (((hw)->guest_features & VIRTIO_PCI_FLAG_MSIX) ? 24 : 20)
203
204 /*
205  * How many bits to shift physical queue address written to QUEUE_PFN.
206  * 12 is historical, and due to x86 page size.
207  */
208 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
209
210 /* The alignment to use between consumer and producer parts of vring. */
211 #define VIRTIO_PCI_VRING_ALIGN 4096
212
213 #ifdef __FreeBSD__
214
215 static inline void
216 outb_p(unsigned char data, unsigned int port)
217 {
218
219         outb(port, (u_char)data);
220 }
221
222 static inline void
223 outw_p(unsigned short data, unsigned int port)
224 {
225         outw(port, (u_short)data);
226 }
227
228 static inline void
229 outl_p(unsigned int data, unsigned int port)
230 {
231         outl(port, (u_int)data);
232 }
233 #endif
234
235 #define VIRTIO_PCI_REG_ADDR(hw, reg) \
236         (unsigned short)((hw)->io_base + (reg))
237
238 #define VIRTIO_READ_REG_1(hw, reg) \
239         inb((VIRTIO_PCI_REG_ADDR((hw), (reg))))
240 #define VIRTIO_WRITE_REG_1(hw, reg, value) \
241         outb_p((unsigned char)(value), (VIRTIO_PCI_REG_ADDR((hw), (reg))))
242
243 #define VIRTIO_READ_REG_2(hw, reg) \
244         inw((VIRTIO_PCI_REG_ADDR((hw), (reg))))
245 #define VIRTIO_WRITE_REG_2(hw, reg, value) \
246         outw_p((unsigned short)(value), (VIRTIO_PCI_REG_ADDR((hw), (reg))))
247
248 #define VIRTIO_READ_REG_4(hw, reg) \
249         inl((VIRTIO_PCI_REG_ADDR((hw), (reg))))
250 #define VIRTIO_WRITE_REG_4(hw, reg, value) \
251         outl_p((unsigned int)(value), (VIRTIO_PCI_REG_ADDR((hw), (reg))))
252
253 static inline int
254 vtpci_with_feature(struct virtio_hw *hw, uint32_t feature)
255 {
256         return ((hw->guest_features & feature) != 0);
257 }
258
259 /*
260  * Function declaration from virtio_pci.c
261  */
262 void vtpci_reset(struct virtio_hw *);
263
264 void vtpci_reinit_complete(struct virtio_hw *);
265
266 uint8_t vtpci_get_status(struct virtio_hw *);
267
268 void vtpci_set_status(struct virtio_hw *, uint8_t);
269
270 uint32_t vtpci_negotiate_features(struct virtio_hw *, uint32_t);
271
272 void vtpci_write_dev_config(struct virtio_hw *, uint64_t, void *, int);
273
274 void vtpci_read_dev_config(struct virtio_hw *, uint64_t, void *, int);
275
276 #endif /* _VIRTIO_PCI_H_ */