]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/netvsc/hv_net_vsc.h
MFC 304327,304329,304330
[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 {
278         uint8_t         mac_addr[6];  /* Assumption unsigned long */
279         uint8_t         link_state;
280 } netvsc_device_info;
281
282 #ifndef HN_USE_TXDESC_BUFRING
283 struct hn_txdesc;
284 SLIST_HEAD(hn_txdesc_list, hn_txdesc);
285 #else
286 struct buf_ring;
287 #endif
288
289 struct hn_tx_ring;
290
291 struct hn_rx_ring {
292         struct ifnet    *hn_ifp;
293         struct hn_tx_ring *hn_txr;
294         void            *hn_rdbuf;
295         int             hn_rx_idx;
296
297         /* Trust csum verification on host side */
298         int             hn_trust_hcsum; /* HN_TRUST_HCSUM_ */
299         struct lro_ctrl hn_lro;
300
301         u_long          hn_csum_ip;
302         u_long          hn_csum_tcp;
303         u_long          hn_csum_udp;
304         u_long          hn_csum_trusted;
305         u_long          hn_lro_tried;
306         u_long          hn_small_pkts;
307         u_long          hn_pkts;
308         u_long          hn_rss_pkts;
309
310         /* Rarely used stuffs */
311         struct sysctl_oid *hn_rx_sysctl_tree;
312         int             hn_rx_flags;
313 } __aligned(CACHE_LINE_SIZE);
314
315 #define HN_TRUST_HCSUM_IP       0x0001
316 #define HN_TRUST_HCSUM_TCP      0x0002
317 #define HN_TRUST_HCSUM_UDP      0x0004
318
319 #define HN_RX_FLAG_ATTACHED     0x1
320
321 struct hn_tx_ring {
322 #ifndef HN_USE_TXDESC_BUFRING
323         struct mtx      hn_txlist_spin;
324         struct hn_txdesc_list hn_txlist;
325 #else
326         struct buf_ring *hn_txdesc_br;
327 #endif
328         int             hn_txdesc_cnt;
329         int             hn_txdesc_avail;
330         u_short         hn_has_txeof;
331         u_short         hn_txdone_cnt;
332
333         int             hn_sched_tx;
334         void            (*hn_txeof)(struct hn_tx_ring *);
335         struct taskqueue *hn_tx_taskq;
336         struct task     hn_tx_task;
337         struct task     hn_txeof_task;
338
339         struct buf_ring *hn_mbuf_br;
340         int             hn_oactive;
341         int             hn_tx_idx;
342
343         struct mtx      hn_tx_lock;
344         struct hn_softc *hn_sc;
345         struct vmbus_channel *hn_chan;
346
347         int             hn_direct_tx_size;
348         int             hn_tx_chimney_size;
349         bus_dma_tag_t   hn_tx_data_dtag;
350         uint64_t        hn_csum_assist;
351
352         int             hn_gpa_cnt;
353         struct vmbus_gpa hn_gpa[NETVSC_PACKET_MAXPAGE];
354
355         u_long          hn_no_txdescs;
356         u_long          hn_send_failed;
357         u_long          hn_txdma_failed;
358         u_long          hn_tx_collapsed;
359         u_long          hn_tx_chimney_tried;
360         u_long          hn_tx_chimney;
361         u_long          hn_pkts;
362
363         /* Rarely used stuffs */
364         struct hn_txdesc *hn_txdesc;
365         bus_dma_tag_t   hn_tx_rndis_dtag;
366         struct sysctl_oid *hn_tx_sysctl_tree;
367         int             hn_tx_flags;
368 } __aligned(CACHE_LINE_SIZE);
369
370 #define HN_TX_FLAG_ATTACHED     0x1
371
372 /*
373  * Device-specific softc structure
374  */
375 typedef struct hn_softc {
376         struct ifnet    *hn_ifp;
377         struct arpcom   arpcom;
378         struct ifmedia  hn_media;
379         device_t        hn_dev;
380         uint8_t         hn_unit;
381         int             hn_carrier;
382         int             hn_if_flags;
383         struct mtx      hn_lock;
384         int             hn_initdone;
385         /* See hv_netvsc_drv_freebsd.c for rules on how to use */
386         int             temp_unusable;
387         netvsc_dev      *net_dev;
388         struct vmbus_channel *hn_prichan;
389
390         int             hn_rx_ring_cnt;
391         int             hn_rx_ring_inuse;
392         struct hn_rx_ring *hn_rx_ring;
393
394         int             hn_tx_ring_cnt;
395         int             hn_tx_ring_inuse;
396         struct hn_tx_ring *hn_tx_ring;
397
398         int             hn_cpu;
399         int             hn_tx_chimney_max;
400         struct taskqueue *hn_tx_taskq;
401         struct sysctl_oid *hn_tx_sysctl_tree;
402         struct sysctl_oid *hn_rx_sysctl_tree;
403         struct vmbus_xact_ctx *hn_xact;
404 } hn_softc_t;
405
406 /*
407  * Externs
408  */
409 extern int hv_promisc_mode;
410 struct hn_send_ctx;
411
412 void netvsc_linkstatus_callback(struct hn_softc *sc, uint32_t status);
413 netvsc_dev *hv_nv_on_device_add(struct hn_softc *sc,
414     void *additional_info, struct hn_rx_ring *rxr);
415 int hv_nv_on_device_remove(struct hn_softc *sc,
416     boolean_t destroy_channel);
417 int hv_nv_on_send(struct vmbus_channel *chan, uint32_t rndis_mtype,
418         struct hn_send_ctx *sndc, struct vmbus_gpa *gpa, int gpa_cnt);
419 int hv_nv_get_next_send_section(netvsc_dev *net_dev);
420 void hv_nv_subchan_attach(struct vmbus_channel *chan,
421     struct hn_rx_ring *rxr);
422
423 #endif  /* __HV_NET_VSC_H__ */
424