update Intel copyright years to 2014
[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 #include <sys/io.h>
39
40 #include <rte_ethdev.h>
41
42 struct virtqueue;
43
44 /* VirtIO PCI vendor/device ID. */
45 #define VIRTIO_PCI_VENDORID     0x1AF4
46 #define VIRTIO_PCI_DEVICEID_MIN 0x1000
47 #define VIRTIO_PCI_DEVICEID_MAX 0x103F
48
49 /* VirtIO ABI version, this must match exactly. */
50 #define VIRTIO_PCI_ABI_VERSION 0
51
52 /*
53  * VirtIO Header, located in BAR 0.
54  */
55 #define VIRTIO_PCI_HOST_FEATURES  0  /* host's supported features (32bit, RO)*/
56 #define VIRTIO_PCI_GUEST_FEATURES 4  /* guest's supported features (32, RW) */
57 #define VIRTIO_PCI_QUEUE_PFN      8  /* physical address of VQ (32, RW) */
58 #define VIRTIO_PCI_QUEUE_NUM      12 /* number of ring entries (16, RO) */
59 #define VIRTIO_PCI_QUEUE_SEL      14 /* current VQ selection (16, RW) */
60 #define VIRTIO_PCI_QUEUE_NOTIFY   16 /* notify host regarding VQ (16, RW) */
61 #define VIRTIO_PCI_STATUS         18 /* device status register (8, RW) */
62 #define VIRTIO_PCI_ISR            19 /* interrupt status register, reading
63                                       * also clears the register (8, RO) */
64 /* Only if MSIX is enabled: */
65 #define VIRTIO_MSI_CONFIG_VECTOR  20 /* configuration change vector (16, RW) */
66 #define VIRTIO_MSI_QUEUE_VECTOR   22 /* vector for selected VQ notifications
67                                       (16, RW) */
68
69 /* The bit of the ISR which indicates a device has an interrupt. */
70 #define VIRTIO_PCI_ISR_INTR   0x1
71 /* The bit of the ISR which indicates a device configuration change. */
72 #define VIRTIO_PCI_ISR_CONFIG 0x2
73 /* Vector value used to disable MSI for queue. */
74 #define VIRTIO_MSI_NO_VECTOR 0xFFFF
75
76 /* VirtIO device IDs. */
77 #define VIRTIO_ID_NETWORK  0x01
78 #define VIRTIO_ID_BLOCK    0x02
79 #define VIRTIO_ID_CONSOLE  0x03
80 #define VIRTIO_ID_ENTROPY  0x04
81 #define VIRTIO_ID_BALLOON  0x05
82 #define VIRTIO_ID_IOMEMORY 0x06
83 #define VIRTIO_ID_9P       0x09
84
85 /* Status byte for guest to report progress. */
86 #define VIRTIO_CONFIG_STATUS_RESET     0x00
87 #define VIRTIO_CONFIG_STATUS_ACK       0x01
88 #define VIRTIO_CONFIG_STATUS_DRIVER    0x02
89 #define VIRTIO_CONFIG_STATUS_DRIVER_OK 0x04
90 #define VIRTIO_CONFIG_STATUS_FAILED    0x80
91
92 /*
93  * Generate interrupt when the virtqueue ring is
94  * completely used, even if we've suppressed them.
95  */
96 #define VIRTIO_F_NOTIFY_ON_EMPTY (1 << 24)
97
98 /*
99  * The guest should never negotiate this feature; it
100  * is used to detect faulty drivers.
101  */
102 #define VIRTIO_F_BAD_FEATURE (1 << 30)
103
104 /*
105  * Some VirtIO feature bits (currently bits 28 through 31) are
106  * reserved for the transport being used (eg. virtio_ring), the
107  * rest are per-device feature bits.
108  */
109 #define VIRTIO_TRANSPORT_F_START 28
110 #define VIRTIO_TRANSPORT_F_END   32
111
112 /*
113  * Each virtqueue indirect descriptor list must be physically contiguous.
114  * To allow us to malloc(9) each list individually, limit the number
115  * supported to what will fit in one page. With 4KB pages, this is a limit
116  * of 256 descriptors. If there is ever a need for more, we can switch to
117  * contigmalloc(9) for the larger allocations, similar to what
118  * bus_dmamem_alloc(9) does.
119  *
120  * Note the sizeof(struct vring_desc) is 16 bytes.
121  */
122 #define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16))
123
124 /* The feature bitmap for virtio net */
125 #define VIRTIO_NET_F_CSUM       0x00001 /* Host handles pkts w/ partial csum */
126 #define VIRTIO_NET_F_GUEST_CSUM 0x00002 /* Guest handles pkts w/ partial csum*/
127 #define VIRTIO_NET_F_MAC        0x00020 /* Host has given MAC address. */
128 #define VIRTIO_NET_F_GSO        0x00040 /* Host handles pkts w/ any GSO type */
129 #define VIRTIO_NET_F_GUEST_TSO4 0x00080 /* Guest can handle TSOv4 in. */
130 #define VIRTIO_NET_F_GUEST_TSO6 0x00100 /* Guest can handle TSOv6 in. */
131 #define VIRTIO_NET_F_GUEST_ECN  0x00200 /* Guest can handle TSO[6] w/ ECN in.*/
132 #define VIRTIO_NET_F_GUEST_UFO  0x00400 /* Guest can handle UFO in. */
133 #define VIRTIO_NET_F_HOST_TSO4  0x00800 /* Host can handle TSOv4 in. */
134 #define VIRTIO_NET_F_HOST_TSO6  0x01000 /* Host can handle TSOv6 in. */
135 #define VIRTIO_NET_F_HOST_ECN   0x02000 /* Host can handle TSO[6] w/ ECN in. */
136 #define VIRTIO_NET_F_HOST_UFO   0x04000 /* Host can handle UFO in. */
137 #define VIRTIO_NET_F_MRG_RXBUF  0x08000 /* Host can merge receive buffers. */
138 #define VIRTIO_NET_F_STATUS     0x10000 /* virtio_net_config.status available*/
139 #define VIRTIO_NET_F_CTRL_VQ    0x20000 /* Control channel available */
140 #define VIRTIO_NET_F_CTRL_RX    0x40000 /* Control channel RX mode support */
141 #define VIRTIO_NET_F_CTRL_VLAN  0x80000 /* Control channel VLAN filtering */
142 #define VIRTIO_NET_F_CTRL_RX_EXTRA  0x100000 /* Extra RX mode control support */
143 #define VIRTIO_RING_F_INDIRECT_DESC 0x10000000 /* Support for indirect buffer descriptors. */
144 /* The guest publishes the used index for which it expects an interrupt
145  * at the end of the avail ring. Host should ignore the avail->flags field.
146  * The host publishes the avail index for which it expects a kick
147  * at the end of the used ring. Guest should ignore the used->flags field.
148  */
149 #define VIRTIO_RING_F_EVENT_IDX 0x20000000
150
151 #define VIRTIO_NET_S_LINK_UP 1 /* Link is up */
152
153 /*
154  * Maximum number of virtqueues per device.
155  */
156 #define VIRTIO_MAX_VIRTQUEUES 8
157
158 struct virtio_hw {
159         uint32_t    io_base;
160         uint32_t    host_features;
161         uint32_t    guest_features;
162         
163         struct virtqueue *cvq;
164
165         uint16_t    vtnet_hdr_size;
166
167         uint32_t    max_tx_queues;
168         uint32_t    max_rx_queues;
169         uint16_t    device_id;
170         uint16_t    vendor_id;
171         uint16_t    subsystem_device_id;
172         uint16_t    subsystem_vendor_id;
173         uint8_t     revision_id;
174         uint8_t     mac_addr[ETHER_ADDR_LEN];
175         int         adapter_stopped;
176         struct      rte_eth_stats eth_stats;
177 };
178
179 /*
180  * This structure is just a reference to read
181  * net device specific config space; it just a chodu structure
182  *
183  */
184 struct virtio_net_config {
185         /* The config defining mac address (if VIRTIO_NET_F_MAC) */
186         uint8_t    mac[ETHER_ADDR_LEN];
187         /* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
188         uint16_t   status;
189 };
190 /* Value indicated in device config */
191 #define VIRTIO_PCI_FLAG_MSIX  0x0020
192 /*
193  * The remaining space is defined by each driver as the per-driver
194  * configuration space.
195  */
196 #define VIRTIO_PCI_CONFIG(hw) (((hw)->guest_features & VIRTIO_PCI_FLAG_MSIX) ? 24 : 20)
197
198 /*
199  * How many bits to shift physical queue address written to QUEUE_PFN.
200  * 12 is historical, and due to x86 page size.
201  */
202 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
203
204 /* The alignment to use between consumer and producer parts of vring. */
205 #define VIRTIO_PCI_VRING_ALIGN 4096
206
207 #define VIRTIO_PCI_REG_ADDR(hw, reg) \
208         (unsigned short)((hw)->io_base + (reg))
209
210 #define VIRTIO_READ_REG_1(hw, reg) \
211         inb((VIRTIO_PCI_REG_ADDR((hw), (reg))))
212 #define VIRTIO_WRITE_REG_1(hw, reg, value) \
213         outb_p((unsigned char)(value), (VIRTIO_PCI_REG_ADDR((hw), (reg))))
214
215 #define VIRTIO_READ_REG_2(hw, reg) \
216         inw((VIRTIO_PCI_REG_ADDR((hw), (reg))))
217 #define VIRTIO_WRITE_REG_2(hw, reg, value) \
218         outw_p((unsigned short)(value), (VIRTIO_PCI_REG_ADDR((hw), (reg))))
219
220 #define VIRTIO_READ_REG_4(hw, reg) \
221         inl((VIRTIO_PCI_REG_ADDR((hw), (reg))))
222 #define VIRTIO_WRITE_REG_4(hw, reg, value) \
223         outl_p((unsigned int)(value), (VIRTIO_PCI_REG_ADDR((hw), (reg))))
224
225 static inline int
226 vtpci_with_feature(struct virtio_hw *hw, uint32_t feature)
227 {
228         return ((hw->guest_features & feature) != 0);
229 }
230
231 /*
232  * Function declaration from virtio_pci.c
233  */
234 void vtpci_reset(struct virtio_hw *);
235
236 void vtpci_reinit_complete(struct virtio_hw *);
237
238 uint8_t vtpci_get_status(struct virtio_hw *);
239
240 void vtpci_set_status(struct virtio_hw *, uint8_t);
241
242 uint32_t vtpci_negotiate_features(struct virtio_hw *, uint32_t);
243
244 void vtpci_write_dev_config(struct virtio_hw *, uint64_t, void *, int);
245
246 void vtpci_read_dev_config(struct virtio_hw *, uint64_t, void *, int);
247
248 #endif /* _VIRTIO_PCI_H_ */