]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/netvsc/hv_net_vsc.h
MFC 304204-304206,304252-304256
[FreeBSD/stable/10.git] / sys / dev / hyperv / netvsc / hv_net_vsc.h
1 /*-
2  * Copyright (c) 2009-2012,2016 Microsoft Corp.
3  * Copyright (c) 2010-2012 Citrix Inc.
4  * Copyright (c) 2012 NetApp Inc.
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  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 /*
32  * HyperV vmbus (virtual machine bus) network VSC (virtual services client)
33  * header file
34  *
35  * (Updated from unencumbered NvspProtocol.h)
36  */
37
38 #ifndef __HV_NET_VSC_H__
39 #define __HV_NET_VSC_H__
40
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/queue.h>
46 #include <sys/taskqueue.h>
47 #include <sys/sema.h>
48 #include <sys/sx.h>
49
50 #include <machine/bus.h>
51 #include <sys/bus.h>
52 #include <sys/bus_dma.h>
53
54 #include <netinet/in.h>
55 #include <netinet/tcp_lro.h>
56
57 #include <net/ethernet.h>
58 #include <net/if.h>
59 #include <net/if_media.h>
60
61 #include <dev/hyperv/include/hyperv.h>
62 #include <dev/hyperv/include/hyperv_busdma.h>
63 #include <dev/hyperv/include/vmbus.h>
64
65 #define HN_USE_TXDESC_BUFRING
66
67 MALLOC_DECLARE(M_NETVSC);
68
69 #define NVSP_INVALID_PROTOCOL_VERSION           (0xFFFFFFFF)
70
71 #define NVSP_PROTOCOL_VERSION_1                 2
72 #define NVSP_PROTOCOL_VERSION_2                 0x30002
73 #define NVSP_PROTOCOL_VERSION_4                 0x40000
74 #define NVSP_PROTOCOL_VERSION_5                 0x50000
75 #define NVSP_MIN_PROTOCOL_VERSION               (NVSP_PROTOCOL_VERSION_1)
76 #define NVSP_MAX_PROTOCOL_VERSION               (NVSP_PROTOCOL_VERSION_2)
77
78 #define NVSP_PROTOCOL_VERSION_CURRENT           NVSP_PROTOCOL_VERSION_2
79
80 #define VERSION_4_OFFLOAD_SIZE                  22
81
82 #define NVSP_OPERATIONAL_STATUS_OK              (0x00000000)
83 #define NVSP_OPERATIONAL_STATUS_DEGRADED        (0x00000001)
84 #define NVSP_OPERATIONAL_STATUS_NONRECOVERABLE  (0x00000002)
85 #define NVSP_OPERATIONAL_STATUS_NO_CONTACT      (0x00000003)
86 #define NVSP_OPERATIONAL_STATUS_LOST_COMMUNICATION (0x00000004)
87
88 /*
89  * Maximun number of transfer pages (packets) the VSP will use on a receive
90  */
91 #define NVSP_MAX_PACKETS_PER_RECEIVE            375
92
93 /* vRSS stuff */
94 #define RNDIS_OBJECT_TYPE_RSS_CAPABILITIES      0x88
95 #define RNDIS_OBJECT_TYPE_RSS_PARAMETERS        0x89
96
97 #define RNDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2     2
98 #define RNDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2       2
99
100 struct rndis_obj_header {
101         uint8_t type;
102         uint8_t rev;
103         uint16_t size;
104 } __packed;
105
106 /* rndis_recv_scale_cap/cap_flag */
107 #define RNDIS_RSS_CAPS_MESSAGE_SIGNALED_INTERRUPTS      0x01000000
108 #define RNDIS_RSS_CAPS_CLASSIFICATION_AT_ISR            0x02000000
109 #define RNDIS_RSS_CAPS_CLASSIFICATION_AT_DPC            0x04000000
110 #define RNDIS_RSS_CAPS_USING_MSI_X                      0x08000000
111 #define RNDIS_RSS_CAPS_RSS_AVAILABLE_ON_PORTS           0x10000000
112 #define RNDIS_RSS_CAPS_SUPPORTS_MSI_X                   0x20000000
113 #define RNDIS_RSS_CAPS_HASH_TYPE_TCP_IPV4               0x00000100
114 #define RNDIS_RSS_CAPS_HASH_TYPE_TCP_IPV6               0x00000200
115 #define RNDIS_RSS_CAPS_HASH_TYPE_TCP_IPV6_EX            0x00000400
116
117 /* RNDIS_RECEIVE_SCALE_CAPABILITIES */
118 struct rndis_recv_scale_cap {
119         struct rndis_obj_header hdr;
120         uint32_t cap_flag;
121         uint32_t num_int_msg;
122         uint32_t num_recv_que;
123         uint16_t num_indirect_tabent;
124 } __packed;
125
126 /* rndis_recv_scale_param flags */
127 #define RNDIS_RSS_PARAM_FLAG_BASE_CPU_UNCHANGED         0x0001
128 #define RNDIS_RSS_PARAM_FLAG_HASH_INFO_UNCHANGED        0x0002
129 #define RNDIS_RSS_PARAM_FLAG_ITABLE_UNCHANGED           0x0004
130 #define RNDIS_RSS_PARAM_FLAG_HASH_KEY_UNCHANGED         0x0008
131 #define RNDIS_RSS_PARAM_FLAG_DISABLE_RSS                0x0010
132
133 /* Hash info bits */
134 #define RNDIS_HASH_FUNC_TOEPLITZ                0x00000001
135 #define RNDIS_HASH_IPV4                         0x00000100
136 #define RNDIS_HASH_TCP_IPV4                     0x00000200
137 #define RNDIS_HASH_IPV6                         0x00000400
138 #define RNDIS_HASH_IPV6_EX                      0x00000800
139 #define RNDIS_HASH_TCP_IPV6                     0x00001000
140 #define RNDIS_HASH_TCP_IPV6_EX                  0x00002000
141
142 #define RNDIS_RSS_INDIRECTION_TABLE_MAX_SIZE_REVISION_2 (128 * 4)
143 #define RNDIS_RSS_HASH_SECRET_KEY_MAX_SIZE_REVISION_2   40
144
145 #define ITAB_NUM                                        128
146 #define HASH_KEYLEN RNDIS_RSS_HASH_SECRET_KEY_MAX_SIZE_REVISION_2
147
148 /* RNDIS_RECEIVE_SCALE_PARAMETERS */
149 typedef struct rndis_recv_scale_param_ {
150         struct rndis_obj_header hdr;
151
152         /* Qualifies the rest of the information */
153         uint16_t flag;
154
155         /* The base CPU number to do receive processing. not used */
156         uint16_t base_cpu_number;
157
158         /* This describes the hash function and type being enabled */
159         uint32_t hashinfo;
160
161         /* The size of indirection table array */
162         uint16_t indirect_tabsize;
163
164         /* The offset of the indirection table from the beginning of this
165          * structure
166          */
167         uint32_t indirect_taboffset;
168
169         /* The size of the hash secret key */
170         uint16_t hashkey_size;
171
172         /* The offset of the secret key from the beginning of this structure */
173         uint32_t hashkey_offset;
174
175         uint32_t processor_masks_offset;
176         uint32_t num_processor_masks;
177         uint32_t processor_masks_entry_size;
178 } rndis_recv_scale_param;
179
180 /*
181  * The following arguably belongs in a separate header file
182  */
183
184 /*
185  * Defines
186  */
187
188 #define NETVSC_SEND_BUFFER_SIZE                 (1024*1024*15)   /* 15M */
189 #define NETVSC_SEND_BUFFER_ID                   0xface
190
191 #define NETVSC_RECEIVE_BUFFER_SIZE_LEGACY       (1024*1024*15) /* 15MB */
192 #define NETVSC_RECEIVE_BUFFER_SIZE              (1024*1024*16) /* 16MB */
193
194 #define NETVSC_RECEIVE_BUFFER_ID                0xcafe
195
196 #define NETVSC_RECEIVE_SG_COUNT                 1
197
198 /* Preallocated receive packets */
199 #define NETVSC_RECEIVE_PACKETLIST_COUNT         256
200
201 /*
202  * Maximum MTU we permit to be configured for a netvsc interface.
203  * When the code was developed, a max MTU of 12232 was tested and
204  * proven to work.  9K is a reasonable maximum for an Ethernet.
205  */
206 #define NETVSC_MAX_CONFIGURABLE_MTU             (9 * 1024)
207
208 #define NETVSC_PACKET_SIZE                      PAGE_SIZE
209 #define VRSS_SEND_TABLE_SIZE                    16
210
211 /*
212  * Data types
213  */
214
215 /*
216  * Per netvsc channel-specific
217  */
218 typedef struct netvsc_dev_ {
219         struct hn_softc                         *sc;
220
221         /* Send buffer allocated by us but manages by NetVSP */
222         void                                    *send_buf;
223         uint32_t                                send_buf_size;
224         uint32_t                                send_buf_gpadl_handle;
225         uint32_t                                send_section_size;
226         uint32_t                                send_section_count;
227         unsigned long                           bitsmap_words;
228         unsigned long                           *send_section_bitsmap;
229
230         /* Receive buffer allocated by us but managed by NetVSP */
231         void                                    *rx_buf;
232         uint32_t                                rx_buf_size;
233         uint32_t                                rx_buf_gpadl_handle;
234         uint32_t                                rx_section_count;
235
236         /* Holds rndis device info */
237         void                                    *extension;
238
239         uint8_t                                 destroy;
240         /* Negotiated NVSP version */
241         uint32_t                                nvsp_version;
242
243         uint32_t                                num_channel;
244
245         struct hyperv_dma                       rxbuf_dma;
246         struct hyperv_dma                       txbuf_dma;
247 } netvsc_dev;
248
249 struct vmbus_channel;
250
251 typedef void (*pfn_on_send_rx_completion)(struct vmbus_channel *, void *);
252
253 #define NETVSC_DEVICE_RING_BUFFER_SIZE  (128 * PAGE_SIZE)
254 #define NETVSC_PACKET_MAXPAGE           32
255
256 #define NETVSC_VLAN_PRIO_MASK           0xe000
257 #define NETVSC_VLAN_PRIO_SHIFT          13
258 #define NETVSC_VLAN_VID_MASK            0x0fff
259
260 #define TYPE_IPV4                       2
261 #define TYPE_IPV6                       4
262 #define TYPE_TCP                        2
263 #define TYPE_UDP                        4
264
265 #define TRANSPORT_TYPE_NOT_IP           0
266 #define TRANSPORT_TYPE_IPV4_TCP         ((TYPE_IPV4 << 16) | TYPE_TCP)
267 #define TRANSPORT_TYPE_IPV4_UDP         ((TYPE_IPV4 << 16) | TYPE_UDP)
268 #define TRANSPORT_TYPE_IPV6_TCP         ((TYPE_IPV6 << 16) | TYPE_TCP)
269 #define TRANSPORT_TYPE_IPV6_UDP         ((TYPE_IPV6 << 16) | TYPE_UDP)
270
271 #ifdef __LP64__
272 #define BITS_PER_LONG 64
273 #else
274 #define BITS_PER_LONG 32
275 #endif
276
277 typedef struct netvsc_packet_ {
278         uint16_t        vlan_tci;
279         uint32_t        status;
280         uint32_t        tot_data_buf_len;
281         void            *data;
282 } netvsc_packet;
283
284 typedef struct {
285         uint8_t         mac_addr[6];  /* Assumption unsigned long */
286         uint8_t         link_state;
287 } netvsc_device_info;
288
289 #ifndef HN_USE_TXDESC_BUFRING
290 struct hn_txdesc;
291 SLIST_HEAD(hn_txdesc_list, hn_txdesc);
292 #else
293 struct buf_ring;
294 #endif
295
296 struct hn_tx_ring;
297
298 struct hn_rx_ring {
299         struct ifnet    *hn_ifp;
300         struct hn_tx_ring *hn_txr;
301         void            *hn_rdbuf;
302         int             hn_rx_idx;
303
304         /* Trust csum verification on host side */
305         int             hn_trust_hcsum; /* HN_TRUST_HCSUM_ */
306         struct lro_ctrl hn_lro;
307
308         u_long          hn_csum_ip;
309         u_long          hn_csum_tcp;
310         u_long          hn_csum_udp;
311         u_long          hn_csum_trusted;
312         u_long          hn_lro_tried;
313         u_long          hn_small_pkts;
314         u_long          hn_pkts;
315         u_long          hn_rss_pkts;
316
317         /* Rarely used stuffs */
318         struct sysctl_oid *hn_rx_sysctl_tree;
319         int             hn_rx_flags;
320 } __aligned(CACHE_LINE_SIZE);
321
322 #define HN_TRUST_HCSUM_IP       0x0001
323 #define HN_TRUST_HCSUM_TCP      0x0002
324 #define HN_TRUST_HCSUM_UDP      0x0004
325
326 #define HN_RX_FLAG_ATTACHED     0x1
327
328 struct hn_tx_ring {
329 #ifndef HN_USE_TXDESC_BUFRING
330         struct mtx      hn_txlist_spin;
331         struct hn_txdesc_list hn_txlist;
332 #else
333         struct buf_ring *hn_txdesc_br;
334 #endif
335         int             hn_txdesc_cnt;
336         int             hn_txdesc_avail;
337         u_short         hn_has_txeof;
338         u_short         hn_txdone_cnt;
339
340         int             hn_sched_tx;
341         void            (*hn_txeof)(struct hn_tx_ring *);
342         struct taskqueue *hn_tx_taskq;
343         struct task     hn_tx_task;
344         struct task     hn_txeof_task;
345
346         struct buf_ring *hn_mbuf_br;
347         int             hn_oactive;
348         int             hn_tx_idx;
349
350         struct mtx      hn_tx_lock;
351         struct hn_softc *hn_sc;
352         struct vmbus_channel *hn_chan;
353
354         int             hn_direct_tx_size;
355         int             hn_tx_chimney_size;
356         bus_dma_tag_t   hn_tx_data_dtag;
357         uint64_t        hn_csum_assist;
358
359         int             hn_gpa_cnt;
360         struct vmbus_gpa hn_gpa[NETVSC_PACKET_MAXPAGE];
361
362         u_long          hn_no_txdescs;
363         u_long          hn_send_failed;
364         u_long          hn_txdma_failed;
365         u_long          hn_tx_collapsed;
366         u_long          hn_tx_chimney_tried;
367         u_long          hn_tx_chimney;
368         u_long          hn_pkts;
369
370         /* Rarely used stuffs */
371         struct hn_txdesc *hn_txdesc;
372         bus_dma_tag_t   hn_tx_rndis_dtag;
373         struct sysctl_oid *hn_tx_sysctl_tree;
374         int             hn_tx_flags;
375 } __aligned(CACHE_LINE_SIZE);
376
377 #define HN_TX_FLAG_ATTACHED     0x1
378
379 /*
380  * Device-specific softc structure
381  */
382 typedef struct hn_softc {
383         struct ifnet    *hn_ifp;
384         struct arpcom   arpcom;
385         struct ifmedia  hn_media;
386         device_t        hn_dev;
387         uint8_t         hn_unit;
388         int             hn_carrier;
389         int             hn_if_flags;
390         struct mtx      hn_lock;
391         int             hn_initdone;
392         /* See hv_netvsc_drv_freebsd.c for rules on how to use */
393         int             temp_unusable;
394         netvsc_dev      *net_dev;
395         struct vmbus_channel *hn_prichan;
396
397         int             hn_rx_ring_cnt;
398         int             hn_rx_ring_inuse;
399         struct hn_rx_ring *hn_rx_ring;
400
401         int             hn_tx_ring_cnt;
402         int             hn_tx_ring_inuse;
403         struct hn_tx_ring *hn_tx_ring;
404
405         int             hn_cpu;
406         int             hn_tx_chimney_max;
407         struct taskqueue *hn_tx_taskq;
408         struct sysctl_oid *hn_tx_sysctl_tree;
409         struct sysctl_oid *hn_rx_sysctl_tree;
410         struct vmbus_xact_ctx *hn_xact;
411 } hn_softc_t;
412
413 /*
414  * Externs
415  */
416 extern int hv_promisc_mode;
417 struct hn_send_ctx;
418
419 void netvsc_linkstatus_callback(struct hn_softc *sc, uint32_t status);
420 netvsc_dev *hv_nv_on_device_add(struct hn_softc *sc,
421     void *additional_info, struct hn_rx_ring *rxr);
422 int hv_nv_on_device_remove(struct hn_softc *sc,
423     boolean_t destroy_channel);
424 int hv_nv_on_send(struct vmbus_channel *chan, uint32_t rndis_mtype,
425         struct hn_send_ctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt);
426 int hv_nv_get_next_send_section(netvsc_dev *net_dev);
427 void hv_nv_subchan_attach(struct vmbus_channel *chan,
428     struct hn_rx_ring *rxr);
429
430 #endif  /* __HV_NET_VSC_H__ */
431