GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / net / hyperv / hyperv_net.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *
4  * Copyright (c) 2011, Microsoft Corporation.
5  *
6  * Authors:
7  *   Haiyang Zhang <haiyangz@microsoft.com>
8  *   Hank Janssen  <hjanssen@microsoft.com>
9  *   K. Y. Srinivasan <kys@microsoft.com>
10  */
11
12 #ifndef _HYPERV_NET_H
13 #define _HYPERV_NET_H
14
15 #include <linux/list.h>
16 #include <linux/hyperv.h>
17 #include <linux/rndis.h>
18 #include <linux/jhash.h>
19
20 /* RSS related */
21 #define OID_GEN_RECEIVE_SCALE_CAPABILITIES 0x00010203  /* query only */
22 #define OID_GEN_RECEIVE_SCALE_PARAMETERS 0x00010204  /* query and set */
23
24 #define NDIS_OBJECT_TYPE_RSS_CAPABILITIES 0x88
25 #define NDIS_OBJECT_TYPE_RSS_PARAMETERS 0x89
26 #define NDIS_OBJECT_TYPE_OFFLOAD        0xa7
27
28 #define NDIS_RECEIVE_SCALE_CAPABILITIES_REVISION_2 2
29 #define NDIS_RECEIVE_SCALE_PARAMETERS_REVISION_2 2
30
31 struct ndis_obj_header {
32         u8 type;
33         u8 rev;
34         u16 size;
35 } __packed;
36
37 /* ndis_recv_scale_cap/cap_flag */
38 #define NDIS_RSS_CAPS_MESSAGE_SIGNALED_INTERRUPTS 0x01000000
39 #define NDIS_RSS_CAPS_CLASSIFICATION_AT_ISR       0x02000000
40 #define NDIS_RSS_CAPS_CLASSIFICATION_AT_DPC       0x04000000
41 #define NDIS_RSS_CAPS_USING_MSI_X                 0x08000000
42 #define NDIS_RSS_CAPS_RSS_AVAILABLE_ON_PORTS      0x10000000
43 #define NDIS_RSS_CAPS_SUPPORTS_MSI_X              0x20000000
44 #define NDIS_RSS_CAPS_HASH_TYPE_TCP_IPV4          0x00000100
45 #define NDIS_RSS_CAPS_HASH_TYPE_TCP_IPV6          0x00000200
46 #define NDIS_RSS_CAPS_HASH_TYPE_TCP_IPV6_EX       0x00000400
47
48 struct ndis_recv_scale_cap { /* NDIS_RECEIVE_SCALE_CAPABILITIES */
49         struct ndis_obj_header hdr;
50         u32 cap_flag;
51         u32 num_int_msg;
52         u32 num_recv_que;
53         u16 num_indirect_tabent;
54 } __packed;
55
56
57 /* ndis_recv_scale_param flags */
58 #define NDIS_RSS_PARAM_FLAG_BASE_CPU_UNCHANGED     0x0001
59 #define NDIS_RSS_PARAM_FLAG_HASH_INFO_UNCHANGED    0x0002
60 #define NDIS_RSS_PARAM_FLAG_ITABLE_UNCHANGED       0x0004
61 #define NDIS_RSS_PARAM_FLAG_HASH_KEY_UNCHANGED     0x0008
62 #define NDIS_RSS_PARAM_FLAG_DISABLE_RSS            0x0010
63
64 /* Hash info bits */
65 #define NDIS_HASH_FUNC_TOEPLITZ 0x00000001
66 #define NDIS_HASH_IPV4          0x00000100
67 #define NDIS_HASH_TCP_IPV4      0x00000200
68 #define NDIS_HASH_IPV6          0x00000400
69 #define NDIS_HASH_IPV6_EX       0x00000800
70 #define NDIS_HASH_TCP_IPV6      0x00001000
71 #define NDIS_HASH_TCP_IPV6_EX   0x00002000
72
73 #define NDIS_RSS_INDIRECTION_TABLE_MAX_SIZE_REVISION_2 (128 * 4)
74 #define NDIS_RSS_HASH_SECRET_KEY_MAX_SIZE_REVISION_2   40
75
76 #define ITAB_NUM 128
77
78 struct ndis_recv_scale_param { /* NDIS_RECEIVE_SCALE_PARAMETERS */
79         struct ndis_obj_header hdr;
80
81         /* Qualifies the rest of the information */
82         u16 flag;
83
84         /* The base CPU number to do receive processing. not used */
85         u16 base_cpu_number;
86
87         /* This describes the hash function and type being enabled */
88         u32 hashinfo;
89
90         /* The size of indirection table array */
91         u16 indirect_tabsize;
92
93         /* The offset of the indirection table from the beginning of this
94          * structure
95          */
96         u32 indirect_taboffset;
97
98         /* The size of the hash secret key */
99         u16 hashkey_size;
100
101         /* The offset of the secret key from the beginning of this structure */
102         u32 hashkey_offset;
103
104         u32 processor_masks_offset;
105         u32 num_processor_masks;
106         u32 processor_masks_entry_size;
107 };
108
109 struct ndis_tcp_ip_checksum_info {
110         union {
111                 struct {
112                         u32 is_ipv4:1;
113                         u32 is_ipv6:1;
114                         u32 tcp_checksum:1;
115                         u32 udp_checksum:1;
116                         u32 ip_header_checksum:1;
117                         u32 reserved:11;
118                         u32 tcp_header_offset:10;
119                 } transmit;
120                 struct {
121                         u32 tcp_checksum_failed:1;
122                         u32 udp_checksum_failed:1;
123                         u32 ip_checksum_failed:1;
124                         u32 tcp_checksum_succeeded:1;
125                         u32 udp_checksum_succeeded:1;
126                         u32 ip_checksum_succeeded:1;
127                         u32 loopback:1;
128                         u32 tcp_checksum_value_invalid:1;
129                         u32 ip_checksum_value_invalid:1;
130                 } receive;
131                 u32  value;
132         };
133 };
134
135 struct ndis_pkt_8021q_info {
136         union {
137                 struct {
138                         u32 pri:3; /* User Priority */
139                         u32 cfi:1; /* Canonical Format ID */
140                         u32 vlanid:12; /* VLAN ID */
141                         u32 reserved:16;
142                 };
143                 u32 value;
144         };
145 };
146
147 /*
148  * Represent netvsc packet which contains 1 RNDIS and 1 ethernet frame
149  * within the RNDIS
150  *
151  * The size of this structure is less than 48 bytes and we can now
152  * place this structure in the skb->cb field.
153  */
154 struct hv_netvsc_packet {
155         /* Bookkeeping stuff */
156         u8 cp_partial; /* partial copy into send buffer */
157
158         u8 rmsg_size; /* RNDIS header and PPI size */
159         u8 rmsg_pgcnt; /* page count of RNDIS header and PPI */
160         u8 page_buf_cnt;
161
162         u16 q_idx;
163         u16 total_packets;
164
165         u32 total_bytes;
166         u32 send_buf_index;
167         u32 total_data_buflen;
168         struct hv_dma_range *dma_range;
169 };
170
171 #define NETVSC_HASH_KEYLEN 40
172
173 struct netvsc_device_info {
174         unsigned char mac_adr[ETH_ALEN];
175         u32  num_chn;
176         u32  send_sections;
177         u32  recv_sections;
178         u32  send_section_size;
179         u32  recv_section_size;
180
181         struct bpf_prog *bprog;
182
183         u8 rss_key[NETVSC_HASH_KEYLEN];
184 };
185
186 enum rndis_device_state {
187         RNDIS_DEV_UNINITIALIZED = 0,
188         RNDIS_DEV_INITIALIZING,
189         RNDIS_DEV_INITIALIZED,
190         RNDIS_DEV_DATAINITIALIZED,
191 };
192
193 struct rndis_device {
194         struct net_device *ndev;
195
196         enum rndis_device_state state;
197
198         atomic_t new_req_id;
199
200         spinlock_t request_lock;
201         struct list_head req_list;
202
203         struct work_struct mcast_work;
204         u32 filter;
205
206         bool link_state;        /* 0 - link up, 1 - link down */
207
208         u8 hw_mac_adr[ETH_ALEN];
209         u8 rss_key[NETVSC_HASH_KEYLEN];
210 };
211
212
213 /* Interface */
214 struct rndis_message;
215 struct ndis_offload_params;
216 struct netvsc_device;
217 struct netvsc_channel;
218 struct net_device_context;
219
220 extern u32 netvsc_ring_bytes;
221
222 struct netvsc_device *netvsc_device_add(struct hv_device *device,
223                                         const struct netvsc_device_info *info);
224 int netvsc_alloc_recv_comp_ring(struct netvsc_device *net_device, u32 q_idx);
225 void netvsc_device_remove(struct hv_device *device);
226 int netvsc_send(struct net_device *net,
227                 struct hv_netvsc_packet *packet,
228                 struct rndis_message *rndis_msg,
229                 struct hv_page_buffer *page_buffer,
230                 struct sk_buff *skb,
231                 bool xdp_tx);
232 void netvsc_linkstatus_callback(struct net_device *net,
233                                 struct rndis_message *resp,
234                                 void *data, u32 data_buflen);
235 int netvsc_recv_callback(struct net_device *net,
236                          struct netvsc_device *nvdev,
237                          struct netvsc_channel *nvchan);
238 void netvsc_channel_cb(void *context);
239 int netvsc_poll(struct napi_struct *napi, int budget);
240
241 void netvsc_xdp_xmit(struct sk_buff *skb, struct net_device *ndev);
242 u32 netvsc_run_xdp(struct net_device *ndev, struct netvsc_channel *nvchan,
243                    struct xdp_buff *xdp);
244 unsigned int netvsc_xdp_fraglen(unsigned int len);
245 struct bpf_prog *netvsc_xdp_get(struct netvsc_device *nvdev);
246 int netvsc_xdp_set(struct net_device *dev, struct bpf_prog *prog,
247                    struct netlink_ext_ack *extack,
248                    struct netvsc_device *nvdev);
249 int netvsc_vf_setxdp(struct net_device *vf_netdev, struct bpf_prog *prog);
250 int netvsc_bpf(struct net_device *dev, struct netdev_bpf *bpf);
251 int netvsc_ndoxdp_xmit(struct net_device *ndev, int n,
252                        struct xdp_frame **frames, u32 flags);
253
254 int rndis_set_subchannel(struct net_device *ndev,
255                          struct netvsc_device *nvdev,
256                          struct netvsc_device_info *dev_info);
257 int rndis_filter_open(struct netvsc_device *nvdev);
258 int rndis_filter_close(struct netvsc_device *nvdev);
259 struct netvsc_device *rndis_filter_device_add(struct hv_device *dev,
260                                               struct netvsc_device_info *info);
261 void rndis_filter_update(struct netvsc_device *nvdev);
262 void rndis_filter_device_remove(struct hv_device *dev,
263                                 struct netvsc_device *nvdev);
264 int rndis_filter_set_rss_param(struct rndis_device *rdev,
265                                const u8 *key);
266 int rndis_filter_set_offload_params(struct net_device *ndev,
267                                     struct netvsc_device *nvdev,
268                                     struct ndis_offload_params *req_offloads);
269 int rndis_filter_receive(struct net_device *ndev,
270                          struct netvsc_device *net_dev,
271                          struct netvsc_channel *nvchan,
272                          void *data, u32 buflen);
273
274 int rndis_filter_set_device_mac(struct netvsc_device *ndev,
275                                 const char *mac);
276
277 int netvsc_switch_datapath(struct net_device *nv_dev, bool vf);
278
279 #define NVSP_INVALID_PROTOCOL_VERSION   ((u32)0xFFFFFFFF)
280
281 #define NVSP_PROTOCOL_VERSION_1         2
282 #define NVSP_PROTOCOL_VERSION_2         0x30002
283 #define NVSP_PROTOCOL_VERSION_4         0x40000
284 #define NVSP_PROTOCOL_VERSION_5         0x50000
285 #define NVSP_PROTOCOL_VERSION_6         0x60000
286 #define NVSP_PROTOCOL_VERSION_61        0x60001
287
288 enum {
289         NVSP_MSG_TYPE_NONE = 0,
290
291         /* Init Messages */
292         NVSP_MSG_TYPE_INIT                      = 1,
293         NVSP_MSG_TYPE_INIT_COMPLETE             = 2,
294
295         NVSP_VERSION_MSG_START                  = 100,
296
297         /* Version 1 Messages */
298         NVSP_MSG1_TYPE_SEND_NDIS_VER            = NVSP_VERSION_MSG_START,
299
300         NVSP_MSG1_TYPE_SEND_RECV_BUF,
301         NVSP_MSG1_TYPE_SEND_RECV_BUF_COMPLETE,
302         NVSP_MSG1_TYPE_REVOKE_RECV_BUF,
303
304         NVSP_MSG1_TYPE_SEND_SEND_BUF,
305         NVSP_MSG1_TYPE_SEND_SEND_BUF_COMPLETE,
306         NVSP_MSG1_TYPE_REVOKE_SEND_BUF,
307
308         NVSP_MSG1_TYPE_SEND_RNDIS_PKT,
309         NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE,
310
311         /* Version 2 messages */
312         NVSP_MSG2_TYPE_SEND_CHIMNEY_DELEGATED_BUF,
313         NVSP_MSG2_TYPE_SEND_CHIMNEY_DELEGATED_BUF_COMP,
314         NVSP_MSG2_TYPE_REVOKE_CHIMNEY_DELEGATED_BUF,
315
316         NVSP_MSG2_TYPE_RESUME_CHIMNEY_RX_INDICATION,
317
318         NVSP_MSG2_TYPE_TERMINATE_CHIMNEY,
319         NVSP_MSG2_TYPE_TERMINATE_CHIMNEY_COMP,
320
321         NVSP_MSG2_TYPE_INDICATE_CHIMNEY_EVENT,
322
323         NVSP_MSG2_TYPE_SEND_CHIMNEY_PKT,
324         NVSP_MSG2_TYPE_SEND_CHIMNEY_PKT_COMP,
325
326         NVSP_MSG2_TYPE_POST_CHIMNEY_RECV_REQ,
327         NVSP_MSG2_TYPE_POST_CHIMNEY_RECV_REQ_COMP,
328
329         NVSP_MSG2_TYPE_ALLOC_RXBUF,
330         NVSP_MSG2_TYPE_ALLOC_RXBUF_COMP,
331
332         NVSP_MSG2_TYPE_FREE_RXBUF,
333
334         NVSP_MSG2_TYPE_SEND_VMQ_RNDIS_PKT,
335         NVSP_MSG2_TYPE_SEND_VMQ_RNDIS_PKT_COMP,
336
337         NVSP_MSG2_TYPE_SEND_NDIS_CONFIG,
338
339         NVSP_MSG2_TYPE_ALLOC_CHIMNEY_HANDLE,
340         NVSP_MSG2_TYPE_ALLOC_CHIMNEY_HANDLE_COMP,
341
342         NVSP_MSG2_MAX = NVSP_MSG2_TYPE_ALLOC_CHIMNEY_HANDLE_COMP,
343
344         /* Version 4 messages */
345         NVSP_MSG4_TYPE_SEND_VF_ASSOCIATION,
346         NVSP_MSG4_TYPE_SWITCH_DATA_PATH,
347         NVSP_MSG4_TYPE_UPLINK_CONNECT_STATE_DEPRECATED,
348
349         NVSP_MSG4_MAX = NVSP_MSG4_TYPE_UPLINK_CONNECT_STATE_DEPRECATED,
350
351         /* Version 5 messages */
352         NVSP_MSG5_TYPE_OID_QUERY_EX,
353         NVSP_MSG5_TYPE_OID_QUERY_EX_COMP,
354         NVSP_MSG5_TYPE_SUBCHANNEL,
355         NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE,
356
357         NVSP_MSG5_MAX = NVSP_MSG5_TYPE_SEND_INDIRECTION_TABLE,
358
359         /* Version 6 messages */
360         NVSP_MSG6_TYPE_PD_API,
361         NVSP_MSG6_TYPE_PD_POST_BATCH,
362
363         NVSP_MSG6_MAX = NVSP_MSG6_TYPE_PD_POST_BATCH
364 };
365
366 enum {
367         NVSP_STAT_NONE = 0,
368         NVSP_STAT_SUCCESS,
369         NVSP_STAT_FAIL,
370         NVSP_STAT_PROTOCOL_TOO_NEW,
371         NVSP_STAT_PROTOCOL_TOO_OLD,
372         NVSP_STAT_INVALID_RNDIS_PKT,
373         NVSP_STAT_BUSY,
374         NVSP_STAT_PROTOCOL_UNSUPPORTED,
375         NVSP_STAT_MAX,
376 };
377
378 struct nvsp_message_header {
379         u32 msg_type;
380 };
381
382 /* Init Messages */
383
384 /*
385  * This message is used by the VSC to initialize the channel after the channels
386  * has been opened. This message should never include anything other then
387  * versioning (i.e. this message will be the same for ever).
388  */
389 struct nvsp_message_init {
390         u32 min_protocol_ver;
391         u32 max_protocol_ver;
392 } __packed;
393
394 /*
395  * This message is used by the VSP to complete the initialization of the
396  * channel. This message should never include anything other then versioning
397  * (i.e. this message will be the same for ever).
398  */
399 struct nvsp_message_init_complete {
400         u32 negotiated_protocol_ver;
401         u32 max_mdl_chain_len;
402         u32 status;
403 } __packed;
404
405 union nvsp_message_init_uber {
406         struct nvsp_message_init init;
407         struct nvsp_message_init_complete init_complete;
408 } __packed;
409
410 /* Version 1 Messages */
411
412 /*
413  * This message is used by the VSC to send the NDIS version to the VSP. The VSP
414  * can use this information when handling OIDs sent by the VSC.
415  */
416 struct nvsp_1_message_send_ndis_version {
417         u32 ndis_major_ver;
418         u32 ndis_minor_ver;
419 } __packed;
420
421 /*
422  * This message is used by the VSC to send a receive buffer to the VSP. The VSP
423  * can then use the receive buffer to send data to the VSC.
424  */
425 struct nvsp_1_message_send_receive_buffer {
426         u32 gpadl_handle;
427         u16 id;
428 } __packed;
429
430 struct nvsp_1_receive_buffer_section {
431         u32 offset;
432         u32 sub_alloc_size;
433         u32 num_sub_allocs;
434         u32 end_offset;
435 } __packed;
436
437 /*
438  * This message is used by the VSP to acknowledge a receive buffer send by the
439  * VSC. This message must be sent by the VSP before the VSP uses the receive
440  * buffer.
441  */
442 struct nvsp_1_message_send_receive_buffer_complete {
443         u32 status;
444         u32 num_sections;
445
446         /*
447          * The receive buffer is split into two parts, a large suballocation
448          * section and a small suballocation section. These sections are then
449          * suballocated by a certain size.
450          */
451
452         /*
453          * For example, the following break up of the receive buffer has 6
454          * large suballocations and 10 small suballocations.
455          */
456
457         /*
458          * |            Large Section          |  |   Small Section   |
459          * ------------------------------------------------------------
460          * |     |     |     |     |     |     |  | | | | | | | | | | |
461          * |                                      |
462          *  LargeOffset                            SmallOffset
463          */
464
465         struct nvsp_1_receive_buffer_section sections[1];
466 } __packed;
467
468 /*
469  * This message is sent by the VSC to revoke the receive buffer.  After the VSP
470  * completes this transaction, the vsp should never use the receive buffer
471  * again.
472  */
473 struct nvsp_1_message_revoke_receive_buffer {
474         u16 id;
475 };
476
477 /*
478  * This message is used by the VSC to send a send buffer to the VSP. The VSC
479  * can then use the send buffer to send data to the VSP.
480  */
481 struct nvsp_1_message_send_send_buffer {
482         u32 gpadl_handle;
483         u16 id;
484 } __packed;
485
486 /*
487  * This message is used by the VSP to acknowledge a send buffer sent by the
488  * VSC. This message must be sent by the VSP before the VSP uses the sent
489  * buffer.
490  */
491 struct nvsp_1_message_send_send_buffer_complete {
492         u32 status;
493
494         /*
495          * The VSC gets to choose the size of the send buffer and the VSP gets
496          * to choose the sections size of the buffer.  This was done to enable
497          * dynamic reconfigurations when the cost of GPA-direct buffers
498          * decreases.
499          */
500         u32 section_size;
501 } __packed;
502
503 /*
504  * This message is sent by the VSC to revoke the send buffer.  After the VSP
505  * completes this transaction, the vsp should never use the send buffer again.
506  */
507 struct nvsp_1_message_revoke_send_buffer {
508         u16 id;
509 };
510
511 /*
512  * This message is used by both the VSP and the VSC to send a RNDIS message to
513  * the opposite channel endpoint.
514  */
515 struct nvsp_1_message_send_rndis_packet {
516         /*
517          * This field is specified by RNDIS. They assume there's two different
518          * channels of communication. However, the Network VSP only has one.
519          * Therefore, the channel travels with the RNDIS packet.
520          */
521         u32 channel_type;
522
523         /*
524          * This field is used to send part or all of the data through a send
525          * buffer. This values specifies an index into the send buffer. If the
526          * index is 0xFFFFFFFF, then the send buffer is not being used and all
527          * of the data was sent through other VMBus mechanisms.
528          */
529         u32 send_buf_section_index;
530         u32 send_buf_section_size;
531 } __packed;
532
533 /*
534  * This message is used by both the VSP and the VSC to complete a RNDIS message
535  * to the opposite channel endpoint. At this point, the initiator of this
536  * message cannot use any resources associated with the original RNDIS packet.
537  */
538 struct nvsp_1_message_send_rndis_packet_complete {
539         u32 status;
540 };
541
542 union nvsp_1_message_uber {
543         struct nvsp_1_message_send_ndis_version send_ndis_ver;
544
545         struct nvsp_1_message_send_receive_buffer send_recv_buf;
546         struct nvsp_1_message_send_receive_buffer_complete
547                                                 send_recv_buf_complete;
548         struct nvsp_1_message_revoke_receive_buffer revoke_recv_buf;
549
550         struct nvsp_1_message_send_send_buffer send_send_buf;
551         struct nvsp_1_message_send_send_buffer_complete send_send_buf_complete;
552         struct nvsp_1_message_revoke_send_buffer revoke_send_buf;
553
554         struct nvsp_1_message_send_rndis_packet send_rndis_pkt;
555         struct nvsp_1_message_send_rndis_packet_complete
556                                                 send_rndis_pkt_complete;
557 } __packed;
558
559
560 /*
561  * Network VSP protocol version 2 messages:
562  */
563 struct nvsp_2_vsc_capability {
564         union {
565                 u64 data;
566                 struct {
567                         u64 vmq:1;
568                         u64 chimney:1;
569                         u64 sriov:1;
570                         u64 ieee8021q:1;
571                         u64 correlation_id:1;
572                         u64 teaming:1;
573                         u64 vsubnetid:1;
574                         u64 rsc:1;
575                 };
576         };
577 } __packed;
578
579 struct nvsp_2_send_ndis_config {
580         u32 mtu;
581         u32 reserved;
582         struct nvsp_2_vsc_capability capability;
583 } __packed;
584
585 /* Allocate receive buffer */
586 struct nvsp_2_alloc_rxbuf {
587         /* Allocation ID to match the allocation request and response */
588         u32 alloc_id;
589
590         /* Length of the VM shared memory receive buffer that needs to
591          * be allocated
592          */
593         u32 len;
594 } __packed;
595
596 /* Allocate receive buffer complete */
597 struct nvsp_2_alloc_rxbuf_comp {
598         /* The NDIS_STATUS code for buffer allocation */
599         u32 status;
600
601         u32 alloc_id;
602
603         /* GPADL handle for the allocated receive buffer */
604         u32 gpadl_handle;
605
606         /* Receive buffer ID */
607         u64 recv_buf_id;
608 } __packed;
609
610 struct nvsp_2_free_rxbuf {
611         u64 recv_buf_id;
612 } __packed;
613
614 union nvsp_2_message_uber {
615         struct nvsp_2_send_ndis_config send_ndis_config;
616         struct nvsp_2_alloc_rxbuf alloc_rxbuf;
617         struct nvsp_2_alloc_rxbuf_comp alloc_rxbuf_comp;
618         struct nvsp_2_free_rxbuf free_rxbuf;
619 } __packed;
620
621 struct nvsp_4_send_vf_association {
622         /* 1: allocated, serial number is valid. 0: not allocated */
623         u32 allocated;
624
625         /* Serial number of the VF to team with */
626         u32 serial;
627 } __packed;
628
629 enum nvsp_vm_datapath {
630         NVSP_DATAPATH_SYNTHETIC = 0,
631         NVSP_DATAPATH_VF,
632         NVSP_DATAPATH_MAX
633 };
634
635 struct nvsp_4_sw_datapath {
636         u32 active_datapath; /* active data path in VM */
637 } __packed;
638
639 union nvsp_4_message_uber {
640         struct nvsp_4_send_vf_association vf_assoc;
641         struct nvsp_4_sw_datapath active_dp;
642 } __packed;
643
644 enum nvsp_subchannel_operation {
645         NVSP_SUBCHANNEL_NONE = 0,
646         NVSP_SUBCHANNEL_ALLOCATE,
647         NVSP_SUBCHANNEL_MAX
648 };
649
650 struct nvsp_5_subchannel_request {
651         u32 op;
652         u32 num_subchannels;
653 } __packed;
654
655 struct nvsp_5_subchannel_complete {
656         u32 status;
657         u32 num_subchannels; /* Actual number of subchannels allocated */
658 } __packed;
659
660 struct nvsp_5_send_indirect_table {
661         /* The number of entries in the send indirection table */
662         u32 count;
663
664         /* The offset of the send indirection table from the beginning of
665          * struct nvsp_message.
666          * The send indirection table tells which channel to put the send
667          * traffic on. Each entry is a channel number.
668          */
669         u32 offset;
670 } __packed;
671
672 union nvsp_5_message_uber {
673         struct nvsp_5_subchannel_request subchn_req;
674         struct nvsp_5_subchannel_complete subchn_comp;
675         struct nvsp_5_send_indirect_table send_table;
676 } __packed;
677
678 enum nvsp_6_pd_api_op {
679         PD_API_OP_CONFIG = 1,
680         PD_API_OP_SW_DATAPATH, /* Switch Datapath */
681         PD_API_OP_OPEN_PROVIDER,
682         PD_API_OP_CLOSE_PROVIDER,
683         PD_API_OP_CREATE_QUEUE,
684         PD_API_OP_FLUSH_QUEUE,
685         PD_API_OP_FREE_QUEUE,
686         PD_API_OP_ALLOC_COM_BUF, /* Allocate Common Buffer */
687         PD_API_OP_FREE_COM_BUF, /* Free Common Buffer */
688         PD_API_OP_MAX
689 };
690
691 struct grp_affinity {
692         u64 mask;
693         u16 grp;
694         u16 reserved[3];
695 } __packed;
696
697 struct nvsp_6_pd_api_req {
698         u32 op;
699
700         union {
701                 /* MMIO information is sent from the VM to VSP */
702                 struct __packed {
703                         u64 mmio_pa; /* MMIO Physical Address */
704                         u32 mmio_len;
705
706                         /* Number of PD queues a VM can support */
707                         u16 num_subchn;
708                 } config;
709
710                 /* Switch Datapath */
711                 struct __packed {
712                         /* Host Datapath Is PacketDirect */
713                         u8 host_dpath_is_pd;
714
715                         /* Guest PacketDirect Is Enabled */
716                         u8 guest_pd_enabled;
717                 } sw_dpath;
718
719                 /* Open Provider*/
720                 struct __packed {
721                         u32 prov_id; /* Provider id */
722                         u32 flag;
723                 } open_prov;
724
725                 /* Close Provider */
726                 struct __packed {
727                         u32 prov_id;
728                 } cls_prov;
729
730                 /* Create Queue*/
731                 struct __packed {
732                         u32 prov_id;
733                         u16 q_id;
734                         u16 q_size;
735                         u8 is_recv_q;
736                         u8 is_rss_q;
737                         u32 recv_data_len;
738                         struct grp_affinity affy;
739                 } cr_q;
740
741                 /* Delete Queue*/
742                 struct __packed {
743                         u32 prov_id;
744                         u16 q_id;
745                 } del_q;
746
747                 /* Flush Queue */
748                 struct __packed {
749                         u32 prov_id;
750                         u16 q_id;
751                 } flush_q;
752
753                 /* Allocate Common Buffer */
754                 struct __packed {
755                         u32 len;
756                         u32 pf_node; /* Preferred Node */
757                         u16 region_id;
758                 } alloc_com_buf;
759
760                 /* Free Common Buffer */
761                 struct __packed {
762                         u32 len;
763                         u64 pa; /* Physical Address */
764                         u32 pf_node; /* Preferred Node */
765                         u16 region_id;
766                         u8 cache_type;
767                 } free_com_buf;
768         } __packed;
769 } __packed;
770
771 struct nvsp_6_pd_api_comp {
772         u32 op;
773         u32 status;
774
775         union {
776                 struct __packed {
777                         /* actual number of PD queues allocated to the VM */
778                         u16 num_pd_q;
779
780                         /* Num Receive Rss PD Queues */
781                         u8 num_rss_q;
782
783                         u8 is_supported; /* Is supported by VSP */
784                         u8 is_enabled; /* Is enabled by VSP */
785                 } config;
786
787                 /* Open Provider */
788                 struct __packed {
789                         u32 prov_id;
790                 } open_prov;
791
792                 /* Create Queue */
793                 struct __packed {
794                         u32 prov_id;
795                         u16 q_id;
796                         u16 q_size;
797                         u32 recv_data_len;
798                         struct grp_affinity affy;
799                 } cr_q;
800
801                 /* Allocate Common Buffer */
802                 struct __packed {
803                         u64 pa; /* Physical Address */
804                         u32 len;
805                         u32 pf_node; /* Preferred Node */
806                         u16 region_id;
807                         u8 cache_type;
808                 } alloc_com_buf;
809         } __packed;
810 } __packed;
811
812 struct nvsp_6_pd_buf {
813         u32 region_offset;
814         u16 region_id;
815         u16 is_partial:1;
816         u16 reserved:15;
817 } __packed;
818
819 struct nvsp_6_pd_batch_msg {
820         struct nvsp_message_header hdr;
821         u16 count;
822         u16 guest2host:1;
823         u16 is_recv:1;
824         u16 reserved:14;
825         struct nvsp_6_pd_buf pd_buf[0];
826 } __packed;
827
828 union nvsp_6_message_uber {
829         struct nvsp_6_pd_api_req pd_req;
830         struct nvsp_6_pd_api_comp pd_comp;
831 } __packed;
832
833 union nvsp_all_messages {
834         union nvsp_message_init_uber init_msg;
835         union nvsp_1_message_uber v1_msg;
836         union nvsp_2_message_uber v2_msg;
837         union nvsp_4_message_uber v4_msg;
838         union nvsp_5_message_uber v5_msg;
839         union nvsp_6_message_uber v6_msg;
840 } __packed;
841
842 /* ALL Messages */
843 struct nvsp_message {
844         struct nvsp_message_header hdr;
845         union nvsp_all_messages msg;
846 } __packed;
847
848
849 #define NETVSC_MTU 65535
850 #define NETVSC_MTU_MIN ETH_MIN_MTU
851
852 /* Max buffer sizes allowed by a host */
853 #define NETVSC_RECEIVE_BUFFER_SIZE              (1024 * 1024 * 31) /* 31MB */
854 #define NETVSC_RECEIVE_BUFFER_SIZE_LEGACY       (1024 * 1024 * 15) /* 15MB */
855 #define NETVSC_RECEIVE_BUFFER_DEFAULT           (1024 * 1024 * 16)
856
857 #define NETVSC_SEND_BUFFER_SIZE                 (1024 * 1024 * 15)  /* 15MB */
858 #define NETVSC_SEND_BUFFER_DEFAULT              (1024 * 1024)
859
860 #define NETVSC_INVALID_INDEX                    -1
861
862 #define NETVSC_SEND_SECTION_SIZE                6144
863 #define NETVSC_RECV_SECTION_SIZE                1728
864
865 /* Default size of TX buf: 1MB, RX buf: 16MB */
866 #define NETVSC_MIN_TX_SECTIONS  10
867 #define NETVSC_DEFAULT_TX       (NETVSC_SEND_BUFFER_DEFAULT \
868                                  / NETVSC_SEND_SECTION_SIZE)
869 #define NETVSC_MIN_RX_SECTIONS  10
870 #define NETVSC_DEFAULT_RX       (NETVSC_RECEIVE_BUFFER_DEFAULT \
871                                  / NETVSC_RECV_SECTION_SIZE)
872
873 #define NETVSC_RECEIVE_BUFFER_ID                0xcafe
874 #define NETVSC_SEND_BUFFER_ID                   0
875
876 #define NETVSC_SUPPORTED_HW_FEATURES (NETIF_F_RXCSUM | NETIF_F_IP_CSUM | \
877                                       NETIF_F_TSO | NETIF_F_IPV6_CSUM | \
878                                       NETIF_F_TSO6 | NETIF_F_LRO | \
879                                       NETIF_F_SG | NETIF_F_RXHASH)
880
881 #define VRSS_SEND_TAB_SIZE 16  /* must be power of 2 */
882 #define VRSS_CHANNEL_MAX 64
883 #define VRSS_CHANNEL_DEFAULT 8
884
885 #define RNDIS_MAX_PKT_DEFAULT 8
886 #define RNDIS_PKT_ALIGN_DEFAULT 8
887
888 #define NETVSC_XDP_HDRM 256
889
890 #define NETVSC_MIN_OUT_MSG_SIZE (sizeof(struct vmpacket_descriptor) + \
891                                  sizeof(struct nvsp_message))
892 #define NETVSC_MIN_IN_MSG_SIZE sizeof(struct vmpacket_descriptor)
893
894 /* Estimated requestor size:
895  * out_ring_size/min_out_msg_size + in_ring_size/min_in_msg_size
896  */
897 static inline u32 netvsc_rqstor_size(unsigned long ringbytes)
898 {
899         return ringbytes / NETVSC_MIN_OUT_MSG_SIZE +
900                 ringbytes / NETVSC_MIN_IN_MSG_SIZE;
901 }
902
903 /* XFER PAGE packets can specify a maximum of 375 ranges for NDIS >= 6.0
904  * and a maximum of 64 ranges for NDIS < 6.0 with no RSC; with RSC, this
905  * limit is raised to 562 (= NVSP_RSC_MAX).
906  */
907 #define NETVSC_MAX_XFER_PAGE_RANGES NVSP_RSC_MAX
908 #define NETVSC_XFER_HEADER_SIZE(rng_cnt) \
909                 (offsetof(struct vmtransfer_page_packet_header, ranges) + \
910                 (rng_cnt) * sizeof(struct vmtransfer_page_range))
911 #define NETVSC_MAX_PKT_SIZE (NETVSC_XFER_HEADER_SIZE(NETVSC_MAX_XFER_PAGE_RANGES) + \
912                 sizeof(struct nvsp_message) + (sizeof(u32) * VRSS_SEND_TAB_SIZE))
913
914 struct multi_send_data {
915         struct sk_buff *skb; /* skb containing the pkt */
916         struct hv_netvsc_packet *pkt; /* netvsc pkt pending */
917         u32 count; /* counter of batched packets */
918 };
919
920 struct recv_comp_data {
921         u64 tid; /* transaction id */
922         u32 status;
923 };
924
925 struct multi_recv_comp {
926         struct recv_comp_data *slots;
927         u32 first;      /* first data entry */
928         u32 next;       /* next entry for writing */
929 };
930
931 #define NVSP_RSC_MAX 562 /* Max #RSC frags in a vmbus xfer page pkt */
932
933 struct nvsc_rsc {
934         struct ndis_pkt_8021q_info vlan;
935         struct ndis_tcp_ip_checksum_info csum_info;
936         u32 hash_info;
937         u8 ppi_flags; /* valid/present bits for the above PPIs */
938         u8 is_last; /* last RNDIS msg in a vmtransfer_page */
939         u32 cnt; /* #fragments in an RSC packet */
940         u32 pktlen; /* Full packet length */
941         void *data[NVSP_RSC_MAX];
942         u32 len[NVSP_RSC_MAX];
943 };
944
945 #define NVSC_RSC_VLAN           BIT(0)  /* valid/present bit for 'vlan' */
946 #define NVSC_RSC_CSUM_INFO      BIT(1)  /* valid/present bit for 'csum_info' */
947 #define NVSC_RSC_HASH_INFO      BIT(2)  /* valid/present bit for 'hash_info' */
948
949 struct netvsc_stats_tx {
950         u64 packets;
951         u64 bytes;
952         u64 xdp_xmit;
953         struct u64_stats_sync syncp;
954 };
955
956 struct netvsc_stats_rx {
957         u64 packets;
958         u64 bytes;
959         u64 broadcast;
960         u64 multicast;
961         u64 xdp_drop;
962         u64 xdp_redirect;
963         u64 xdp_tx;
964         struct u64_stats_sync syncp;
965 };
966
967 struct netvsc_ethtool_stats {
968         unsigned long tx_scattered;
969         unsigned long tx_no_memory;
970         unsigned long tx_no_space;
971         unsigned long tx_too_big;
972         unsigned long tx_busy;
973         unsigned long tx_send_full;
974         unsigned long rx_comp_busy;
975         unsigned long rx_no_memory;
976         unsigned long stop_queue;
977         unsigned long wake_queue;
978         unsigned long vlan_error;
979 };
980
981 struct netvsc_ethtool_pcpu_stats {
982         u64     rx_packets;
983         u64     rx_bytes;
984         u64     tx_packets;
985         u64     tx_bytes;
986         u64     vf_rx_packets;
987         u64     vf_rx_bytes;
988         u64     vf_tx_packets;
989         u64     vf_tx_bytes;
990 };
991
992 struct netvsc_vf_pcpu_stats {
993         u64     rx_packets;
994         u64     rx_bytes;
995         u64     tx_packets;
996         u64     tx_bytes;
997         struct u64_stats_sync   syncp;
998         u32     tx_dropped;
999 };
1000
1001 struct netvsc_reconfig {
1002         struct list_head list;
1003         u32 event;
1004 };
1005
1006 /* L4 hash bits for different protocols */
1007 #define HV_TCP4_L4HASH 1
1008 #define HV_TCP6_L4HASH 2
1009 #define HV_UDP4_L4HASH 4
1010 #define HV_UDP6_L4HASH 8
1011 #define HV_DEFAULT_L4HASH (HV_TCP4_L4HASH | HV_TCP6_L4HASH | HV_UDP4_L4HASH | \
1012                            HV_UDP6_L4HASH)
1013
1014 /* The context of the netvsc device  */
1015 struct net_device_context {
1016         /* point back to our device context */
1017         struct hv_device *device_ctx;
1018         /* netvsc_device */
1019         struct netvsc_device __rcu *nvdev;
1020         /* list of netvsc net_devices */
1021         struct list_head list;
1022         /* reconfigure work */
1023         struct delayed_work dwork;
1024         /* last reconfig time */
1025         unsigned long last_reconfig;
1026         /* reconfig events */
1027         struct list_head reconfig_events;
1028         /* list protection */
1029         spinlock_t lock;
1030
1031         u32 msg_enable; /* debug level */
1032
1033         u32 tx_checksum_mask;
1034
1035         u32 tx_table[VRSS_SEND_TAB_SIZE];
1036
1037         u16 rx_table[ITAB_NUM];
1038
1039         /* Ethtool settings */
1040         u8 duplex;
1041         u32 speed;
1042         u32 l4_hash; /* L4 hash settings */
1043         struct netvsc_ethtool_stats eth_stats;
1044
1045         /* State to manage the associated VF interface. */
1046         struct net_device __rcu *vf_netdev;
1047         struct netvsc_vf_pcpu_stats __percpu *vf_stats;
1048         struct delayed_work vf_takeover;
1049
1050         /* 1: allocated, serial number is valid. 0: not allocated */
1051         u32 vf_alloc;
1052         /* Serial number of the VF to team with */
1053         u32 vf_serial;
1054
1055         /* Is the current data path through the VF NIC? */
1056         bool  data_path_is_vf;
1057
1058         /* Used to temporarily save the config info across hibernation */
1059         struct netvsc_device_info *saved_netvsc_dev_info;
1060 };
1061
1062 /* Azure hosts don't support non-TCP port numbers in hashing for fragmented
1063  * packets. We can use ethtool to change UDP hash level when necessary.
1064  */
1065 static inline u32 netvsc_get_hash(struct sk_buff *skb,
1066                                   const struct net_device_context *ndc)
1067 {
1068         struct flow_keys flow;
1069         u32 hash, pkt_proto = 0;
1070         static u32 hashrnd __read_mostly;
1071
1072         net_get_random_once(&hashrnd, sizeof(hashrnd));
1073
1074         if (!skb_flow_dissect_flow_keys(skb, &flow, 0))
1075                 return 0;
1076
1077         switch (flow.basic.ip_proto) {
1078         case IPPROTO_TCP:
1079                 if (flow.basic.n_proto == htons(ETH_P_IP))
1080                         pkt_proto = HV_TCP4_L4HASH;
1081                 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
1082                         pkt_proto = HV_TCP6_L4HASH;
1083
1084                 break;
1085
1086         case IPPROTO_UDP:
1087                 if (flow.basic.n_proto == htons(ETH_P_IP))
1088                         pkt_proto = HV_UDP4_L4HASH;
1089                 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
1090                         pkt_proto = HV_UDP6_L4HASH;
1091
1092                 break;
1093         }
1094
1095         if (pkt_proto & ndc->l4_hash) {
1096                 return skb_get_hash(skb);
1097         } else {
1098                 if (flow.basic.n_proto == htons(ETH_P_IP))
1099                         hash = jhash2((u32 *)&flow.addrs.v4addrs, 2, hashrnd);
1100                 else if (flow.basic.n_proto == htons(ETH_P_IPV6))
1101                         hash = jhash2((u32 *)&flow.addrs.v6addrs, 8, hashrnd);
1102                 else
1103                         return 0;
1104
1105                 __skb_set_sw_hash(skb, hash, false);
1106         }
1107
1108         return hash;
1109 }
1110
1111 /* Per channel data */
1112 struct netvsc_channel {
1113         struct vmbus_channel *channel;
1114         struct netvsc_device *net_device;
1115         void *recv_buf; /* buffer to copy packets out from the receive buffer */
1116         const struct vmpacket_descriptor *desc;
1117         struct napi_struct napi;
1118         struct multi_send_data msd;
1119         struct multi_recv_comp mrc;
1120         atomic_t queue_sends;
1121         struct nvsc_rsc rsc;
1122
1123         struct bpf_prog __rcu *bpf_prog;
1124         struct xdp_rxq_info xdp_rxq;
1125         bool xdp_flush;
1126
1127         struct netvsc_stats_tx tx_stats;
1128         struct netvsc_stats_rx rx_stats;
1129 };
1130
1131 /* Per netvsc device */
1132 struct netvsc_device {
1133         u32 nvsp_version;
1134
1135         wait_queue_head_t wait_drain;
1136         bool destroy;
1137         bool tx_disable; /* if true, do not wake up queue again */
1138
1139         /* Receive buffer allocated by us but manages by NetVSP */
1140         void *recv_buf;
1141         void *recv_original_buf;
1142         u32 recv_buf_size; /* allocated bytes */
1143         struct vmbus_gpadl recv_buf_gpadl_handle;
1144         u32 recv_section_cnt;
1145         u32 recv_section_size;
1146         u32 recv_completion_cnt;
1147
1148         /* Send buffer allocated by us */
1149         void *send_buf;
1150         void *send_original_buf;
1151         u32 send_buf_size;
1152         struct vmbus_gpadl send_buf_gpadl_handle;
1153         u32 send_section_cnt;
1154         u32 send_section_size;
1155         unsigned long *send_section_map;
1156
1157         /* Used for NetVSP initialization protocol */
1158         struct completion channel_init_wait;
1159         struct nvsp_message channel_init_pkt;
1160
1161         struct nvsp_message revoke_packet;
1162
1163         u32 max_chn;
1164         u32 num_chn;
1165
1166         atomic_t open_chn;
1167         struct work_struct subchan_work;
1168         wait_queue_head_t subchan_open;
1169
1170         struct rndis_device *extension;
1171
1172         u32 max_pkt; /* max number of pkt in one send, e.g. 8 */
1173         u32 pkt_align; /* alignment bytes, e.g. 8 */
1174
1175         struct netvsc_channel chan_table[VRSS_CHANNEL_MAX];
1176
1177         struct rcu_head rcu;
1178 };
1179
1180 /* NdisInitialize message */
1181 struct rndis_initialize_request {
1182         u32 req_id;
1183         u32 major_ver;
1184         u32 minor_ver;
1185         u32 max_xfer_size;
1186 };
1187
1188 /* Response to NdisInitialize */
1189 struct rndis_initialize_complete {
1190         u32 req_id;
1191         u32 status;
1192         u32 major_ver;
1193         u32 minor_ver;
1194         u32 dev_flags;
1195         u32 medium;
1196         u32 max_pkt_per_msg;
1197         u32 max_xfer_size;
1198         u32 pkt_alignment_factor;
1199         u32 af_list_offset;
1200         u32 af_list_size;
1201 };
1202
1203 /* Call manager devices only: Information about an address family */
1204 /* supported by the device is appended to the response to NdisInitialize. */
1205 struct rndis_co_address_family {
1206         u32 address_family;
1207         u32 major_ver;
1208         u32 minor_ver;
1209 };
1210
1211 /* NdisHalt message */
1212 struct rndis_halt_request {
1213         u32 req_id;
1214 };
1215
1216 /* NdisQueryRequest message */
1217 struct rndis_query_request {
1218         u32 req_id;
1219         u32 oid;
1220         u32 info_buflen;
1221         u32 info_buf_offset;
1222         u32 dev_vc_handle;
1223 };
1224
1225 /* Response to NdisQueryRequest */
1226 struct rndis_query_complete {
1227         u32 req_id;
1228         u32 status;
1229         u32 info_buflen;
1230         u32 info_buf_offset;
1231 };
1232
1233 /* NdisSetRequest message */
1234 struct rndis_set_request {
1235         u32 req_id;
1236         u32 oid;
1237         u32 info_buflen;
1238         u32 info_buf_offset;
1239         u32 dev_vc_handle;
1240         u8  info_buf[];
1241 };
1242
1243 /* Response to NdisSetRequest */
1244 struct rndis_set_complete {
1245         u32 req_id;
1246         u32 status;
1247 };
1248
1249 /* NdisReset message */
1250 struct rndis_reset_request {
1251         u32 reserved;
1252 };
1253
1254 /* Response to NdisReset */
1255 struct rndis_reset_complete {
1256         u32 status;
1257         u32 addressing_reset;
1258 };
1259
1260 /* NdisMIndicateStatus message */
1261 struct rndis_indicate_status {
1262         u32 status;
1263         u32 status_buflen;
1264         u32 status_buf_offset;
1265 };
1266
1267 /* Diagnostic information passed as the status buffer in */
1268 /* struct rndis_indicate_status messages signifying error conditions. */
1269 struct rndis_diagnostic_info {
1270         u32 diag_status;
1271         u32 error_offset;
1272 };
1273
1274 /* NdisKeepAlive message */
1275 struct rndis_keepalive_request {
1276         u32 req_id;
1277 };
1278
1279 /* Response to NdisKeepAlive */
1280 struct rndis_keepalive_complete {
1281         u32 req_id;
1282         u32 status;
1283 };
1284
1285 /*
1286  * Data message. All Offset fields contain byte offsets from the beginning of
1287  * struct rndis_packet. All Length fields are in bytes.  VcHandle is set
1288  * to 0 for connectionless data, otherwise it contains the VC handle.
1289  */
1290 struct rndis_packet {
1291         u32 data_offset;
1292         u32 data_len;
1293         u32 oob_data_offset;
1294         u32 oob_data_len;
1295         u32 num_oob_data_elements;
1296         u32 per_pkt_info_offset;
1297         u32 per_pkt_info_len;
1298         u32 vc_handle;
1299         u32 reserved;
1300 };
1301
1302 /* Optional Out of Band data associated with a Data message. */
1303 struct rndis_oobd {
1304         u32 size;
1305         u32 type;
1306         u32 class_info_offset;
1307 };
1308
1309 /* Packet extension field contents associated with a Data message. */
1310 struct rndis_per_packet_info {
1311         u32 size;
1312         u32 type:31;
1313         u32 internal:1;
1314         u32 ppi_offset;
1315 };
1316
1317 enum ndis_per_pkt_info_type {
1318         TCPIP_CHKSUM_PKTINFO,
1319         IPSEC_PKTINFO,
1320         TCP_LARGESEND_PKTINFO,
1321         CLASSIFICATION_HANDLE_PKTINFO,
1322         NDIS_RESERVED,
1323         SG_LIST_PKTINFO,
1324         IEEE_8021Q_INFO,
1325         ORIGINAL_PKTINFO,
1326         PACKET_CANCEL_ID,
1327         NBL_HASH_VALUE = PACKET_CANCEL_ID,
1328         ORIGINAL_NET_BUFLIST,
1329         CACHED_NET_BUFLIST,
1330         SHORT_PKT_PADINFO,
1331         MAX_PER_PKT_INFO
1332 };
1333
1334 enum rndis_per_pkt_info_interal_type {
1335         RNDIS_PKTINFO_ID = 1,
1336         /* Add more members here */
1337
1338         RNDIS_PKTINFO_MAX
1339 };
1340
1341 #define RNDIS_PKTINFO_SUBALLOC BIT(0)
1342 #define RNDIS_PKTINFO_1ST_FRAG BIT(1)
1343 #define RNDIS_PKTINFO_LAST_FRAG BIT(2)
1344
1345 #define RNDIS_PKTINFO_ID_V1 1
1346
1347 struct rndis_pktinfo_id {
1348         u8 ver;
1349         u8 flag;
1350         u16 pkt_id;
1351 };
1352
1353 struct ndis_object_header {
1354         u8 type;
1355         u8 revision;
1356         u16 size;
1357 };
1358
1359 #define NDIS_OBJECT_TYPE_DEFAULT        0x80
1360 #define NDIS_OFFLOAD_PARAMETERS_REVISION_3 3
1361 #define NDIS_OFFLOAD_PARAMETERS_REVISION_2 2
1362 #define NDIS_OFFLOAD_PARAMETERS_REVISION_1 1
1363
1364 #define NDIS_OFFLOAD_PARAMETERS_NO_CHANGE 0
1365 #define NDIS_OFFLOAD_PARAMETERS_LSOV2_DISABLED 1
1366 #define NDIS_OFFLOAD_PARAMETERS_LSOV2_ENABLED  2
1367 #define NDIS_OFFLOAD_PARAMETERS_LSOV1_ENABLED  2
1368 #define NDIS_OFFLOAD_PARAMETERS_RSC_DISABLED 1
1369 #define NDIS_OFFLOAD_PARAMETERS_RSC_ENABLED 2
1370 #define NDIS_OFFLOAD_PARAMETERS_TX_RX_DISABLED 1
1371 #define NDIS_OFFLOAD_PARAMETERS_TX_ENABLED_RX_DISABLED 2
1372 #define NDIS_OFFLOAD_PARAMETERS_RX_ENABLED_TX_DISABLED 3
1373 #define NDIS_OFFLOAD_PARAMETERS_TX_RX_ENABLED 4
1374
1375 #define NDIS_TCP_LARGE_SEND_OFFLOAD_V2_TYPE     1
1376 #define NDIS_TCP_LARGE_SEND_OFFLOAD_IPV4        0
1377 #define NDIS_TCP_LARGE_SEND_OFFLOAD_IPV6        1
1378
1379 #define VERSION_4_OFFLOAD_SIZE                  22
1380 /*
1381  * New offload OIDs for NDIS 6
1382  */
1383 #define OID_TCP_OFFLOAD_CURRENT_CONFIG 0xFC01020B /* query only */
1384 #define OID_TCP_OFFLOAD_PARAMETERS 0xFC01020C           /* set only */
1385 #define OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES 0xFC01020D/* query only */
1386 #define OID_TCP_CONNECTION_OFFLOAD_CURRENT_CONFIG 0xFC01020E /* query only */
1387 #define OID_TCP_CONNECTION_OFFLOAD_HARDWARE_CAPABILITIES 0xFC01020F /* query */
1388 #define OID_OFFLOAD_ENCAPSULATION 0x0101010A /* set/query */
1389
1390 /*
1391  * OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES
1392  * ndis_type: NDIS_OBJTYPE_OFFLOAD
1393  */
1394
1395 #define NDIS_OFFLOAD_ENCAP_NONE         0x0000
1396 #define NDIS_OFFLOAD_ENCAP_NULL         0x0001
1397 #define NDIS_OFFLOAD_ENCAP_8023         0x0002
1398 #define NDIS_OFFLOAD_ENCAP_8023PQ       0x0004
1399 #define NDIS_OFFLOAD_ENCAP_8023PQ_OOB   0x0008
1400 #define NDIS_OFFLOAD_ENCAP_RFC1483      0x0010
1401
1402 struct ndis_csum_offload {
1403         u32     ip4_txenc;
1404         u32     ip4_txcsum;
1405 #define NDIS_TXCSUM_CAP_IP4OPT          0x001
1406 #define NDIS_TXCSUM_CAP_TCP4OPT         0x004
1407 #define NDIS_TXCSUM_CAP_TCP4            0x010
1408 #define NDIS_TXCSUM_CAP_UDP4            0x040
1409 #define NDIS_TXCSUM_CAP_IP4             0x100
1410
1411 #define NDIS_TXCSUM_ALL_TCP4    (NDIS_TXCSUM_CAP_TCP4 | NDIS_TXCSUM_CAP_TCP4OPT)
1412
1413         u32     ip4_rxenc;
1414         u32     ip4_rxcsum;
1415 #define NDIS_RXCSUM_CAP_IP4OPT          0x001
1416 #define NDIS_RXCSUM_CAP_TCP4OPT         0x004
1417 #define NDIS_RXCSUM_CAP_TCP4            0x010
1418 #define NDIS_RXCSUM_CAP_UDP4            0x040
1419 #define NDIS_RXCSUM_CAP_IP4             0x100
1420         u32     ip6_txenc;
1421         u32     ip6_txcsum;
1422 #define NDIS_TXCSUM_CAP_IP6EXT          0x001
1423 #define NDIS_TXCSUM_CAP_TCP6OPT         0x004
1424 #define NDIS_TXCSUM_CAP_TCP6            0x010
1425 #define NDIS_TXCSUM_CAP_UDP6            0x040
1426         u32     ip6_rxenc;
1427         u32     ip6_rxcsum;
1428 #define NDIS_RXCSUM_CAP_IP6EXT          0x001
1429 #define NDIS_RXCSUM_CAP_TCP6OPT         0x004
1430 #define NDIS_RXCSUM_CAP_TCP6            0x010
1431 #define NDIS_RXCSUM_CAP_UDP6            0x040
1432
1433 #define NDIS_TXCSUM_ALL_TCP6    (NDIS_TXCSUM_CAP_TCP6 |         \
1434                                  NDIS_TXCSUM_CAP_TCP6OPT |      \
1435                                  NDIS_TXCSUM_CAP_IP6EXT)
1436 };
1437
1438 struct ndis_lsov1_offload {
1439         u32     encap;
1440         u32     maxsize;
1441         u32     minsegs;
1442         u32     opts;
1443 };
1444
1445 struct ndis_ipsecv1_offload {
1446         u32     encap;
1447         u32     ah_esp;
1448         u32     xport_tun;
1449         u32     ip4_opts;
1450         u32     flags;
1451         u32     ip4_ah;
1452         u32     ip4_esp;
1453 };
1454
1455 struct ndis_lsov2_offload {
1456         u32     ip4_encap;
1457         u32     ip4_maxsz;
1458         u32     ip4_minsg;
1459         u32     ip6_encap;
1460         u32     ip6_maxsz;
1461         u32     ip6_minsg;
1462         u32     ip6_opts;
1463 #define NDIS_LSOV2_CAP_IP6EXT           0x001
1464 #define NDIS_LSOV2_CAP_TCP6OPT          0x004
1465
1466 #define NDIS_LSOV2_CAP_IP6              (NDIS_LSOV2_CAP_IP6EXT | \
1467                                          NDIS_LSOV2_CAP_TCP6OPT)
1468 };
1469
1470 struct ndis_ipsecv2_offload {
1471         u32     encap;
1472         u8      ip6;
1473         u8      ip4opt;
1474         u8      ip6ext;
1475         u8      ah;
1476         u8      esp;
1477         u8      ah_esp;
1478         u8      xport;
1479         u8      tun;
1480         u8      xport_tun;
1481         u8      lso;
1482         u8      extseq;
1483         u32     udp_esp;
1484         u32     auth;
1485         u32     crypto;
1486         u32     sa_caps;
1487 };
1488
1489 struct ndis_rsc_offload {
1490         u8      ip4;
1491         u8      ip6;
1492 };
1493
1494 struct ndis_encap_offload {
1495         u32     flags;
1496         u32     maxhdr;
1497 };
1498
1499 struct ndis_offload {
1500         struct ndis_object_header       header;
1501         struct ndis_csum_offload        csum;
1502         struct ndis_lsov1_offload       lsov1;
1503         struct ndis_ipsecv1_offload     ipsecv1;
1504         struct ndis_lsov2_offload       lsov2;
1505         u32                             flags;
1506         /* NDIS >= 6.1 */
1507         struct ndis_ipsecv2_offload     ipsecv2;
1508         /* NDIS >= 6.30 */
1509         struct ndis_rsc_offload         rsc;
1510         struct ndis_encap_offload       encap_gre;
1511 };
1512
1513 #define NDIS_OFFLOAD_SIZE               sizeof(struct ndis_offload)
1514 #define NDIS_OFFLOAD_SIZE_6_0           offsetof(struct ndis_offload, ipsecv2)
1515 #define NDIS_OFFLOAD_SIZE_6_1           offsetof(struct ndis_offload, rsc)
1516
1517 struct ndis_offload_params {
1518         struct ndis_object_header header;
1519         u8 ip_v4_csum;
1520         u8 tcp_ip_v4_csum;
1521         u8 udp_ip_v4_csum;
1522         u8 tcp_ip_v6_csum;
1523         u8 udp_ip_v6_csum;
1524         u8 lso_v1;
1525         u8 ip_sec_v1;
1526         u8 lso_v2_ipv4;
1527         u8 lso_v2_ipv6;
1528         u8 tcp_connection_ip_v4;
1529         u8 tcp_connection_ip_v6;
1530         u32 flags;
1531         u8 ip_sec_v2;
1532         u8 ip_sec_v2_ip_v4;
1533         struct {
1534                 u8 rsc_ip_v4;
1535                 u8 rsc_ip_v6;
1536         };
1537         struct {
1538                 u8 encapsulated_packet_task_offload;
1539                 u8 encapsulation_types;
1540         };
1541 };
1542
1543 struct ndis_tcp_lso_info {
1544         union {
1545                 struct {
1546                         u32 unused:30;
1547                         u32 type:1;
1548                         u32 reserved2:1;
1549                 } transmit;
1550                 struct {
1551                         u32 mss:20;
1552                         u32 tcp_header_offset:10;
1553                         u32 type:1;
1554                         u32 reserved2:1;
1555                 } lso_v1_transmit;
1556                 struct {
1557                         u32 tcp_payload:30;
1558                         u32 type:1;
1559                         u32 reserved2:1;
1560                 } lso_v1_transmit_complete;
1561                 struct {
1562                         u32 mss:20;
1563                         u32 tcp_header_offset:10;
1564                         u32 type:1;
1565                         u32 ip_version:1;
1566                 } lso_v2_transmit;
1567                 struct {
1568                         u32 reserved:30;
1569                         u32 type:1;
1570                         u32 reserved2:1;
1571                 } lso_v2_transmit_complete;
1572                 u32  value;
1573         };
1574 };
1575
1576 #define NDIS_VLAN_PPI_SIZE (sizeof(struct rndis_per_packet_info) + \
1577                 sizeof(struct ndis_pkt_8021q_info))
1578
1579 #define NDIS_CSUM_PPI_SIZE (sizeof(struct rndis_per_packet_info) + \
1580                 sizeof(struct ndis_tcp_ip_checksum_info))
1581
1582 #define NDIS_LSO_PPI_SIZE (sizeof(struct rndis_per_packet_info) + \
1583                 sizeof(struct ndis_tcp_lso_info))
1584
1585 #define NDIS_HASH_PPI_SIZE (sizeof(struct rndis_per_packet_info) + \
1586                 sizeof(u32))
1587
1588 /* Total size of all PPI data */
1589 #define NDIS_ALL_PPI_SIZE (NDIS_VLAN_PPI_SIZE + NDIS_CSUM_PPI_SIZE + \
1590                            NDIS_LSO_PPI_SIZE + NDIS_HASH_PPI_SIZE)
1591
1592 /* Format of Information buffer passed in a SetRequest for the OID */
1593 /* OID_GEN_RNDIS_CONFIG_PARAMETER. */
1594 struct rndis_config_parameter_info {
1595         u32 parameter_name_offset;
1596         u32 parameter_name_length;
1597         u32 parameter_type;
1598         u32 parameter_value_offset;
1599         u32 parameter_value_length;
1600 };
1601
1602 /* Values for ParameterType in struct rndis_config_parameter_info */
1603 #define RNDIS_CONFIG_PARAM_TYPE_INTEGER     0
1604 #define RNDIS_CONFIG_PARAM_TYPE_STRING      2
1605
1606 /* CONDIS Miniport messages for connection oriented devices */
1607 /* that do not implement a call manager. */
1608
1609 /* CoNdisMiniportCreateVc message */
1610 struct rcondis_mp_create_vc {
1611         u32 req_id;
1612         u32 ndis_vc_handle;
1613 };
1614
1615 /* Response to CoNdisMiniportCreateVc */
1616 struct rcondis_mp_create_vc_complete {
1617         u32 req_id;
1618         u32 dev_vc_handle;
1619         u32 status;
1620 };
1621
1622 /* CoNdisMiniportDeleteVc message */
1623 struct rcondis_mp_delete_vc {
1624         u32 req_id;
1625         u32 dev_vc_handle;
1626 };
1627
1628 /* Response to CoNdisMiniportDeleteVc */
1629 struct rcondis_mp_delete_vc_complete {
1630         u32 req_id;
1631         u32 status;
1632 };
1633
1634 /* CoNdisMiniportQueryRequest message */
1635 struct rcondis_mp_query_request {
1636         u32 req_id;
1637         u32 request_type;
1638         u32 oid;
1639         u32 dev_vc_handle;
1640         u32 info_buflen;
1641         u32 info_buf_offset;
1642 };
1643
1644 /* CoNdisMiniportSetRequest message */
1645 struct rcondis_mp_set_request {
1646         u32 req_id;
1647         u32 request_type;
1648         u32 oid;
1649         u32 dev_vc_handle;
1650         u32 info_buflen;
1651         u32 info_buf_offset;
1652 };
1653
1654 /* CoNdisIndicateStatus message */
1655 struct rcondis_indicate_status {
1656         u32 ndis_vc_handle;
1657         u32 status;
1658         u32 status_buflen;
1659         u32 status_buf_offset;
1660 };
1661
1662 /* CONDIS Call/VC parameters */
1663 struct rcondis_specific_parameters {
1664         u32 parameter_type;
1665         u32 parameter_length;
1666         u32 parameter_lffset;
1667 };
1668
1669 struct rcondis_media_parameters {
1670         u32 flags;
1671         u32 reserved1;
1672         u32 reserved2;
1673         struct rcondis_specific_parameters media_specific;
1674 };
1675
1676 struct rndis_flowspec {
1677         u32 token_rate;
1678         u32 token_bucket_size;
1679         u32 peak_bandwidth;
1680         u32 latency;
1681         u32 delay_variation;
1682         u32 service_type;
1683         u32 max_sdu_size;
1684         u32 minimum_policed_size;
1685 };
1686
1687 struct rcondis_call_manager_parameters {
1688         struct rndis_flowspec transmit;
1689         struct rndis_flowspec receive;
1690         struct rcondis_specific_parameters call_mgr_specific;
1691 };
1692
1693 /* CoNdisMiniportActivateVc message */
1694 struct rcondis_mp_activate_vc_request {
1695         u32 req_id;
1696         u32 flags;
1697         u32 dev_vc_handle;
1698         u32 media_params_offset;
1699         u32 media_params_length;
1700         u32 call_mgr_params_offset;
1701         u32 call_mgr_params_length;
1702 };
1703
1704 /* Response to CoNdisMiniportActivateVc */
1705 struct rcondis_mp_activate_vc_complete {
1706         u32 req_id;
1707         u32 status;
1708 };
1709
1710 /* CoNdisMiniportDeactivateVc message */
1711 struct rcondis_mp_deactivate_vc_request {
1712         u32 req_id;
1713         u32 flags;
1714         u32 dev_vc_handle;
1715 };
1716
1717 /* Response to CoNdisMiniportDeactivateVc */
1718 struct rcondis_mp_deactivate_vc_complete {
1719         u32 req_id;
1720         u32 status;
1721 };
1722
1723
1724 /* union with all of the RNDIS messages */
1725 union rndis_message_container {
1726         struct rndis_packet pkt;
1727         struct rndis_initialize_request init_req;
1728         struct rndis_halt_request halt_req;
1729         struct rndis_query_request query_req;
1730         struct rndis_set_request set_req;
1731         struct rndis_reset_request reset_req;
1732         struct rndis_keepalive_request keep_alive_req;
1733         struct rndis_indicate_status indicate_status;
1734         struct rndis_initialize_complete init_complete;
1735         struct rndis_query_complete query_complete;
1736         struct rndis_set_complete set_complete;
1737         struct rndis_reset_complete reset_complete;
1738         struct rndis_keepalive_complete keep_alive_complete;
1739         struct rcondis_mp_create_vc co_miniport_create_vc;
1740         struct rcondis_mp_delete_vc co_miniport_delete_vc;
1741         struct rcondis_indicate_status co_indicate_status;
1742         struct rcondis_mp_activate_vc_request co_miniport_activate_vc;
1743         struct rcondis_mp_deactivate_vc_request co_miniport_deactivate_vc;
1744         struct rcondis_mp_create_vc_complete co_miniport_create_vc_complete;
1745         struct rcondis_mp_delete_vc_complete co_miniport_delete_vc_complete;
1746         struct rcondis_mp_activate_vc_complete co_miniport_activate_vc_complete;
1747         struct rcondis_mp_deactivate_vc_complete
1748                 co_miniport_deactivate_vc_complete;
1749 };
1750
1751 /* Remote NDIS message format */
1752 struct rndis_message {
1753         u32 ndis_msg_type;
1754
1755         /* Total length of this message, from the beginning */
1756         /* of the struct rndis_message, in bytes. */
1757         u32 msg_len;
1758
1759         /* Actual message */
1760         union rndis_message_container msg;
1761 };
1762
1763
1764 /* Handy macros */
1765
1766 /* get the size of an RNDIS message. Pass in the message type, */
1767 /* struct rndis_set_request, struct rndis_packet for example */
1768 #define RNDIS_MESSAGE_SIZE(msg)                         \
1769         (sizeof(msg) + (sizeof(struct rndis_message) -  \
1770          sizeof(union rndis_message_container)))
1771
1772 #define RNDIS_HEADER_SIZE       (sizeof(struct rndis_message) - \
1773                                  sizeof(union rndis_message_container))
1774
1775 #define RNDIS_AND_PPI_SIZE (sizeof(struct rndis_message) + NDIS_ALL_PPI_SIZE)
1776
1777 #define NDIS_PACKET_TYPE_DIRECTED       0x00000001
1778 #define NDIS_PACKET_TYPE_MULTICAST      0x00000002
1779 #define NDIS_PACKET_TYPE_ALL_MULTICAST  0x00000004
1780 #define NDIS_PACKET_TYPE_BROADCAST      0x00000008
1781 #define NDIS_PACKET_TYPE_SOURCE_ROUTING 0x00000010
1782 #define NDIS_PACKET_TYPE_PROMISCUOUS    0x00000020
1783 #define NDIS_PACKET_TYPE_SMT            0x00000040
1784 #define NDIS_PACKET_TYPE_ALL_LOCAL      0x00000080
1785 #define NDIS_PACKET_TYPE_GROUP          0x00000100
1786 #define NDIS_PACKET_TYPE_ALL_FUNCTIONAL 0x00000200
1787 #define NDIS_PACKET_TYPE_FUNCTIONAL     0x00000400
1788 #define NDIS_PACKET_TYPE_MAC_FRAME      0x00000800
1789
1790 #define TRANSPORT_INFO_NOT_IP   0
1791 #define TRANSPORT_INFO_IPV4_TCP 0x01
1792 #define TRANSPORT_INFO_IPV4_UDP 0x02
1793 #define TRANSPORT_INFO_IPV6_TCP 0x10
1794 #define TRANSPORT_INFO_IPV6_UDP 0x20
1795
1796 #define RETRY_US_LO     5000
1797 #define RETRY_US_HI     10000
1798 #define RETRY_MAX       2000    /* >10 sec */
1799
1800 void netvsc_dma_unmap(struct hv_device *hv_dev,
1801                       struct hv_netvsc_packet *packet);
1802 #endif /* _HYPERV_NET_H */