]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/netvsc/hv_net_vsc.h
MFC 295748,295792,295793,295794
[FreeBSD/stable/10.git] / sys / dev / hyperv / netvsc / hv_net_vsc.h
1 /*-
2  * Copyright (c) 2009-2012 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/sx.h>
48
49 #include <machine/bus.h>
50 #include <sys/bus.h>
51 #include <sys/bus_dma.h>
52
53 #include <netinet/in.h>
54 #include <netinet/tcp_lro.h>
55
56 #include <net/if.h>
57 #include <net/if_media.h>
58
59 #include <dev/hyperv/include/hyperv.h>
60
61 #define HN_USE_TXDESC_BUFRING
62
63 MALLOC_DECLARE(M_NETVSC);
64
65 #define NVSP_INVALID_PROTOCOL_VERSION           (0xFFFFFFFF)
66
67 #define NVSP_PROTOCOL_VERSION_1                 2
68 #define NVSP_PROTOCOL_VERSION_2                 0x30002
69 #define NVSP_PROTOCOL_VERSION_4                 0x40000
70 #define NVSP_PROTOCOL_VERSION_5                 0x50000
71 #define NVSP_MIN_PROTOCOL_VERSION               (NVSP_PROTOCOL_VERSION_1)
72 #define NVSP_MAX_PROTOCOL_VERSION               (NVSP_PROTOCOL_VERSION_2)
73
74 #define NVSP_PROTOCOL_VERSION_CURRENT           NVSP_PROTOCOL_VERSION_2
75
76 #define VERSION_4_OFFLOAD_SIZE                  22
77
78 #define NVSP_OPERATIONAL_STATUS_OK              (0x00000000)
79 #define NVSP_OPERATIONAL_STATUS_DEGRADED        (0x00000001)
80 #define NVSP_OPERATIONAL_STATUS_NONRECOVERABLE  (0x00000002)
81 #define NVSP_OPERATIONAL_STATUS_NO_CONTACT      (0x00000003)
82 #define NVSP_OPERATIONAL_STATUS_LOST_COMMUNICATION (0x00000004)
83
84 /*
85  * Maximun number of transfer pages (packets) the VSP will use on a receive
86  */
87 #define NVSP_MAX_PACKETS_PER_RECEIVE            375
88
89
90 typedef enum nvsp_msg_type_ {
91         nvsp_msg_type_none                      = 0,
92
93         /*
94          * Init Messages
95          */
96         nvsp_msg_type_init                      = 1,
97         nvsp_msg_type_init_complete             = 2,
98
99         nvsp_version_msg_start                  = 100,
100
101         /*
102          * Version 1 Messages
103          */
104         nvsp_msg_1_type_send_ndis_vers          = nvsp_version_msg_start,
105
106         nvsp_msg_1_type_send_rx_buf,
107         nvsp_msg_1_type_send_rx_buf_complete,
108         nvsp_msg_1_type_revoke_rx_buf,
109
110         nvsp_msg_1_type_send_send_buf,
111         nvsp_msg_1_type_send_send_buf_complete,
112         nvsp_msg_1_type_revoke_send_buf,
113
114         nvsp_msg_1_type_send_rndis_pkt,
115         nvsp_msg_1_type_send_rndis_pkt_complete,
116     
117         /*
118          * Version 2 Messages
119          */
120         nvsp_msg_2_type_send_chimney_delegated_buf,
121         nvsp_msg_2_type_send_chimney_delegated_buf_complete,
122         nvsp_msg_2_type_revoke_chimney_delegated_buf,
123
124         nvsp_msg_2_type_resume_chimney_rx_indication,
125
126         nvsp_msg_2_type_terminate_chimney,
127         nvsp_msg_2_type_terminate_chimney_complete,
128
129         nvsp_msg_2_type_indicate_chimney_event,
130
131         nvsp_msg_2_type_send_chimney_packet,
132         nvsp_msg_2_type_send_chimney_packet_complete,
133
134         nvsp_msg_2_type_post_chimney_rx_request,
135         nvsp_msg_2_type_post_chimney_rx_request_complete,
136
137         nvsp_msg_2_type_alloc_rx_buf,
138         nvsp_msg_2_type_alloc_rx_buf_complete,
139
140         nvsp_msg_2_type_free_rx_buf,
141
142         nvsp_msg_2_send_vmq_rndis_pkt,
143         nvsp_msg_2_send_vmq_rndis_pkt_complete,
144
145         nvsp_msg_2_type_send_ndis_config,
146
147         nvsp_msg_2_type_alloc_chimney_handle,
148         nvsp_msg_2_type_alloc_chimney_handle_complete,
149 } nvsp_msg_type;
150
151 typedef enum nvsp_status_ {
152         nvsp_status_none = 0,
153         nvsp_status_success,
154         nvsp_status_failure,
155         /* Deprecated */
156         nvsp_status_prot_vers_range_too_new,
157         /* Deprecated */
158         nvsp_status_prot_vers_range_too_old,
159         nvsp_status_invalid_rndis_pkt,
160         nvsp_status_busy,
161         nvsp_status_max,
162 } nvsp_status;
163
164 typedef struct nvsp_msg_hdr_ {
165         uint32_t                                msg_type;
166 } __packed nvsp_msg_hdr;
167
168 /*
169  * Init Messages
170  */
171
172 /*
173  * This message is used by the VSC to initialize the channel
174  * after the channels has been opened. This message should 
175  * never include anything other then versioning (i.e. this
176  * message will be the same for ever).
177  *
178  * Forever is a long time.  The values have been redefined
179  * in Win7 to indicate major and minor protocol version
180  * number.
181  */
182 typedef struct nvsp_msg_init_ {
183         union {
184                 struct {
185                         uint16_t                minor_protocol_version;
186                         uint16_t                major_protocol_version;
187                 } s;
188                 /* Formerly min_protocol_version */
189                 uint32_t                        protocol_version;
190         } p1;
191         /* Formerly max_protocol_version */
192         uint32_t                                protocol_version_2;
193 } __packed nvsp_msg_init;
194
195 /*
196  * This message is used by the VSP to complete the initialization
197  * of the channel. This message should never include anything other 
198  * then versioning (i.e. this message will be the same forever).
199  */
200 typedef struct nvsp_msg_init_complete_ {
201         /* Deprecated */
202         uint32_t                                negotiated_prot_vers;
203         uint32_t                                max_mdl_chain_len;
204         uint32_t                                status;
205 } __packed nvsp_msg_init_complete;
206
207 typedef union nvsp_msg_init_uber_ {
208         nvsp_msg_init                           init;
209         nvsp_msg_init_complete                  init_compl;
210 } __packed nvsp_msg_init_uber;
211
212 /*
213  * Version 1 Messages
214  */
215
216 /*
217  * This message is used by the VSC to send the NDIS version
218  * to the VSP.  The VSP can use this information when handling
219  * OIDs sent by the VSC.
220  */
221 typedef struct nvsp_1_msg_send_ndis_version_ {
222         uint32_t                                ndis_major_vers;
223         /* Deprecated */
224         uint32_t                                ndis_minor_vers;
225 } __packed nvsp_1_msg_send_ndis_version;
226
227 /*
228  * This message is used by the VSC to send a receive buffer
229  * to the VSP. The VSP can then use the receive buffer to
230  * send data to the VSC.
231  */
232 typedef struct nvsp_1_msg_send_rx_buf_ {
233         uint32_t                                gpadl_handle;
234         uint16_t                                id;
235 } __packed nvsp_1_msg_send_rx_buf;
236
237 typedef struct nvsp_1_rx_buf_section_ {
238         uint32_t                                offset;
239         uint32_t                                sub_allocation_size;
240         uint32_t                                num_sub_allocations;
241         uint32_t                                end_offset;
242 } __packed nvsp_1_rx_buf_section;
243
244 /*
245  * This message is used by the VSP to acknowledge a receive 
246  * buffer send by the VSC.  This message must be sent by the 
247  * VSP before the VSP uses the receive buffer.
248  */
249 typedef struct nvsp_1_msg_send_rx_buf_complete_ {
250         uint32_t                                status;
251         uint32_t                                num_sections;
252
253         /*
254          * The receive buffer is split into two parts, a large
255          * suballocation section and a small suballocation
256          * section. These sections are then suballocated by a 
257          * certain size.
258          *
259          * For example, the following break up of the receive
260          * buffer has 6 large suballocations and 10 small
261          * suballocations.
262          *
263          * |            Large Section          |  |   Small Section   |
264          * ------------------------------------------------------------
265          * |     |     |     |     |     |     |  | | | | | | | | | | |
266          * |                                      |  
267          * LargeOffset                            SmallOffset
268          */
269         nvsp_1_rx_buf_section                   sections[1];
270
271 } __packed nvsp_1_msg_send_rx_buf_complete;
272
273 /*
274  * This message is sent by the VSC to revoke the receive buffer.
275  * After the VSP completes this transaction, the VSP should never
276  * use the receive buffer again.
277  */
278 typedef struct nvsp_1_msg_revoke_rx_buf_ {
279         uint16_t                                id;
280 } __packed nvsp_1_msg_revoke_rx_buf;
281
282 /*
283  * This message is used by the VSC to send a send buffer
284  * to the VSP. The VSC can then use the send buffer to
285  * send data to the VSP.
286  */
287 typedef struct nvsp_1_msg_send_send_buf_ {
288         uint32_t                                gpadl_handle;
289         uint16_t                                id;
290 } __packed nvsp_1_msg_send_send_buf;
291
292 /*
293  * This message is used by the VSP to acknowledge a send 
294  * buffer sent by the VSC. This message must be sent by the 
295  * VSP before the VSP uses the sent buffer.
296  */
297 typedef struct nvsp_1_msg_send_send_buf_complete_ {
298         uint32_t                                status;
299
300         /*
301          * The VSC gets to choose the size of the send buffer and
302          * the VSP gets to choose the sections size of the buffer.
303          * This was done to enable dynamic reconfigurations when
304          * the cost of GPA-direct buffers decreases.
305          */
306         uint32_t                                section_size;
307 } __packed nvsp_1_msg_send_send_buf_complete;
308
309 /*
310  * This message is sent by the VSC to revoke the send buffer.
311  * After the VSP completes this transaction, the vsp should never
312  * use the send buffer again.
313  */
314 typedef struct nvsp_1_msg_revoke_send_buf_ {
315         uint16_t                                id;
316 } __packed nvsp_1_msg_revoke_send_buf;
317
318 /*
319  * This message is used by both the VSP and the VSC to send
320  * an RNDIS message to the opposite channel endpoint.
321  */
322 typedef struct nvsp_1_msg_send_rndis_pkt_ {
323         /*
324          * This field is specified by RNIDS.  They assume there's
325          * two different channels of communication. However, 
326          * the Network VSP only has one.  Therefore, the channel
327          * travels with the RNDIS packet.
328          */
329         uint32_t                                chan_type;
330
331         /*
332          * This field is used to send part or all of the data
333          * through a send buffer. This values specifies an 
334          * index into the send buffer.  If the index is 
335          * 0xFFFFFFFF, then the send buffer is not being used
336          * and all of the data was sent through other VMBus
337          * mechanisms.
338          */
339         uint32_t                                send_buf_section_idx;
340         uint32_t                                send_buf_section_size;
341 } __packed nvsp_1_msg_send_rndis_pkt;
342
343 /*
344  * This message is used by both the VSP and the VSC to complete
345  * a RNDIS message to the opposite channel endpoint.  At this
346  * point, the initiator of this message cannot use any resources
347  * associated with the original RNDIS packet.
348  */
349 typedef struct nvsp_1_msg_send_rndis_pkt_complete_ {
350         uint32_t                                status;
351 } __packed nvsp_1_msg_send_rndis_pkt_complete;
352
353
354 /*
355  * Version 2 Messages
356  */
357
358 /*
359  * This message is used by the VSC to send the NDIS version
360  * to the VSP.  The VSP can use this information when handling
361  * OIDs sent by the VSC.
362  */
363 typedef struct nvsp_2_netvsc_capabilities_ {
364         union {
365                 uint64_t                        as_uint64;
366                 struct {
367                         uint64_t                vmq           : 1;
368                         uint64_t                chimney       : 1;
369                         uint64_t                sriov         : 1;
370                         uint64_t                ieee8021q     : 1;
371                         uint64_t                correlationid : 1;
372                         uint64_t                teaming       : 1;
373                 } u2;
374         } u1;
375 } __packed nvsp_2_netvsc_capabilities;
376
377 typedef struct nvsp_2_msg_send_ndis_config_ {
378         uint32_t                                mtu;
379         uint32_t                                reserved;
380         nvsp_2_netvsc_capabilities              capabilities;
381 } __packed nvsp_2_msg_send_ndis_config;
382
383 /*
384  * NvspMessage2TypeSendChimneyDelegatedBuffer
385  */
386 typedef struct nvsp_2_msg_send_chimney_buf_
387 {
388         /*
389          * On WIN7 beta, delegated_obj_max_size is defined as a uint32_t
390          * Since WIN7 RC, it was split into two uint16_t.  To have the same
391          * struct layout, delegated_obj_max_size shall be the first field.
392          */
393         uint16_t                                delegated_obj_max_size;
394
395         /*
396          * The revision # of chimney protocol used between NVSC and NVSP.
397          *
398          * This revision is NOT related to the chimney revision between
399          * NDIS protocol and miniport drivers.
400          */
401         uint16_t                                revision;
402
403         uint32_t                                gpadl_handle;
404 } __packed nvsp_2_msg_send_chimney_buf;
405
406
407 /* Unsupported chimney revision 0 (only present in WIN7 beta) */
408 #define NVSP_CHIMNEY_REVISION_0                 0
409
410 /* WIN7 Beta Chimney QFE */
411 #define NVSP_CHIMNEY_REVISION_1                 1
412
413 /* The chimney revision since WIN7 RC */
414 #define NVSP_CHIMNEY_REVISION_2                 2
415
416
417 /*
418  * NvspMessage2TypeSendChimneyDelegatedBufferComplete
419  */
420 typedef struct nvsp_2_msg_send_chimney_buf_complete_ {
421         uint32_t                                status;
422
423         /*
424          * Maximum number outstanding sends and pre-posted receives.
425          *
426          * NVSC should not post more than SendQuota/ReceiveQuota packets.
427          * Otherwise, it can block the non-chimney path for an indefinite
428          * amount of time.
429          * (since chimney sends/receives are affected by the remote peer).
430          *
431          * Note: NVSP enforces the quota restrictions on a per-VMBCHANNEL
432          * basis.  It doesn't enforce the restriction separately for chimney
433          * send/receive.  If NVSC doesn't voluntarily enforce "SendQuota",
434          * it may kill its own network connectivity.
435          */
436         uint32_t                                send_quota;
437         uint32_t                                rx_quota;
438 } __packed nvsp_2_msg_send_chimney_buf_complete;
439
440 /*
441  * NvspMessage2TypeRevokeChimneyDelegatedBuffer
442  */
443 typedef struct nvsp_2_msg_revoke_chimney_buf_ {
444         uint32_t                                gpadl_handle;
445 } __packed nvsp_2_msg_revoke_chimney_buf;
446
447
448 #define NVSP_CHIMNEY_OBJECT_TYPE_NEIGHBOR       0
449 #define NVSP_CHIMNEY_OBJECT_TYPE_PATH4          1
450 #define NVSP_CHIMNEY_OBJECT_TYPE_PATH6          2
451 #define NVSP_CHIMNEY_OBJECT_TYPE_TCP            3
452
453 /*
454  * NvspMessage2TypeAllocateChimneyHandle
455  */
456 typedef struct nvsp_2_msg_alloc_chimney_handle_ {
457         uint64_t                                vsc_context;
458         uint32_t                                object_type;
459 } __packed nvsp_2_msg_alloc_chimney_handle;
460
461 /*
462  * NvspMessage2TypeAllocateChimneyHandleComplete
463  */
464 typedef struct nvsp_2_msg_alloc_chimney_handle_complete_ {
465         uint32_t                                vsp_handle;
466 } __packed nvsp_2_msg_alloc_chimney_handle_complete;
467
468
469 /*
470  * NvspMessage2TypeResumeChimneyRXIndication
471  */
472 typedef struct nvsp_2_msg_resume_chimney_rx_indication {
473         /*
474          * Handle identifying the offloaded connection
475          */
476         uint32_t                                vsp_tcp_handle;
477 } __packed nvsp_2_msg_resume_chimney_rx_indication;
478
479
480 #define NVSP_2_MSG_TERMINATE_CHIMNEY_FLAGS_FIRST_STAGE      (0x01u)
481 #define NVSP_2_MSG_TERMINATE_CHIMNEY_FLAGS_RESERVED         (~(0x01u))
482
483 /*
484  * NvspMessage2TypeTerminateChimney
485  */
486 typedef struct nvsp_2_msg_terminate_chimney_ {
487         /*
488         * Handle identifying the offloaded object
489         */
490         uint32_t                                vsp_handle;
491
492         /*
493          * Terminate Offload Flags
494          *     Bit 0:
495          *         When set to 0, terminate the offload at the destination NIC
496          *     Bit 1-31:  Reserved, shall be zero
497          */
498         uint32_t                                flags;
499
500         union {
501                 /*
502                  * This field is valid only when bit 0 of flags is clear.
503                  * It specifies the index into the premapped delegated
504                  * object buffer.  The buffer was sent through the
505                  * NvspMessage2TypeSendChimneyDelegatedBuffer
506                  * message at initialization time.
507                  *
508                  * NVSP will write the delegated state into the delegated
509                  * buffer upon upload completion.
510                  */
511                 uint32_t                        index;
512
513                 /*
514                  * This field is valid only when bit 0 of flags is set.
515                  *
516                  * The seqence number of the most recently accepted RX
517                  * indication when VSC sets its TCP context into
518                  * "terminating" state.
519                  *
520                  * This allows NVSP to determines if there are any in-flight
521                  * RX indications for which the acceptance state is still
522                  * undefined.
523                  */
524                 uint64_t                        last_accepted_rx_seq_no;
525         } f0;
526 } __packed nvsp_2_msg_terminate_chimney;
527
528
529 #define NVSP_TERMINATE_CHIMNEY_COMPLETE_FLAG_DATA_CORRUPTED     0x0000001u
530
531 /*
532  * NvspMessage2TypeTerminateChimneyComplete
533  */
534 typedef struct nvsp_2_msg_terminate_chimney_complete_ {
535         uint64_t                                vsc_context;
536         uint32_t                                flags;
537 } __packed nvsp_2_msg_terminate_chimney_complete;
538
539 /*
540  * NvspMessage2TypeIndicateChimneyEvent
541  */
542 typedef struct nvsp_2_msg_indicate_chimney_event_ {
543         /*
544          * When VscTcpContext is 0, event_type is an NDIS_STATUS event code
545          * Otherwise, EventType is an TCP connection event (defined in
546          * NdisTcpOffloadEventHandler chimney DDK document).
547          */
548         uint32_t                                event_type;
549
550         /*
551          * When VscTcpContext is 0, EventType is an NDIS_STATUS event code
552          * Otherwise, EventType is an TCP connection event specific information
553          * (defined in NdisTcpOffloadEventHandler chimney DDK document).
554          */
555         uint32_t                                event_specific_info;
556
557         /*
558          * If not 0, the event is per-TCP connection event.  This field
559          * contains the VSC's TCP context.
560          * If 0, the event indication is global.
561          */
562         uint64_t                                vsc_tcp_context;
563 } __packed nvsp_2_msg_indicate_chimney_event;
564
565
566 #define NVSP_1_CHIMNEY_SEND_INVALID_OOB_INDEX       0xffffu
567 #define NVSP_1_CHIMNEY_SEND_INVALID_SECTION_INDEX   0xffffffff
568
569 /*
570  * NvspMessage2TypeSendChimneyPacket
571  */
572 typedef struct nvsp_2_msg_send_chimney_pkt_ {
573     /*
574      * Identify the TCP connection for which this chimney send is
575      */
576     uint32_t                                    vsp_tcp_handle;
577
578     /*
579      * This field is used to send part or all of the data
580      * through a send buffer. This values specifies an
581      * index into the send buffer. If the index is
582      * 0xFFFF, then the send buffer is not being used
583      * and all of the data was sent through other VMBus
584      * mechanisms.
585      */
586     uint16_t                                    send_buf_section_index;
587     uint16_t                                    send_buf_section_size;
588
589     /*
590      * OOB Data Index
591      * This an index to the OOB data buffer. If the index is 0xFFFFFFFF,
592      * then there is no OOB data.
593      *
594      * This field shall be always 0xFFFFFFFF for now. It is reserved for
595      * the future.
596      */
597     uint16_t                                    oob_data_index;
598
599     /*
600      * DisconnectFlags = 0
601      *      Normal chimney send. See MiniportTcpOffloadSend for details.
602      *
603      * DisconnectFlags = TCP_DISCONNECT_GRACEFUL_CLOSE (0x01)
604      *      Graceful disconnect. See MiniportTcpOffloadDisconnect for details.
605      *
606      * DisconnectFlags = TCP_DISCONNECT_ABORTIVE_CLOSE (0x02)
607      *      Abortive disconnect. See MiniportTcpOffloadDisconnect for details.
608      */
609     uint16_t                                    disconnect_flags;
610
611     uint32_t                                    seq_no;
612 } __packed nvsp_2_msg_send_chimney_pkt;
613
614 /*
615  * NvspMessage2TypeSendChimneyPacketComplete
616  */
617 typedef struct nvsp_2_msg_send_chimney_pkt_complete_ {
618     /*
619      * The NDIS_STATUS for the chimney send
620      */
621     uint32_t                                    status;
622
623     /*
624      * Number of bytes that have been sent to the peer (and ACKed by the peer).
625      */
626     uint32_t                                    bytes_transferred;
627 } __packed nvsp_2_msg_send_chimney_pkt_complete;
628
629
630 #define NVSP_1_CHIMNEY_RECV_FLAG_NO_PUSH        0x0001u
631 #define NVSP_1_CHIMNEY_RECV_INVALID_OOB_INDEX   0xffffu
632
633 /*
634  * NvspMessage2TypePostChimneyRecvRequest
635  */
636 typedef struct nvsp_2_msg_post_chimney_rx_request_ {
637         /*
638          * Identify the TCP connection which this chimney receive request
639          * is for.
640          */
641         uint32_t                                vsp_tcp_handle;
642
643         /*
644          * OOB Data Index
645          * This an index to the OOB data buffer. If the index is 0xFFFFFFFF,
646          * then there is no OOB data.
647          *
648          * This field shall be always 0xFFFFFFFF for now. It is reserved for
649          * the future.
650          */
651         uint32_t                                oob_data_index;
652
653         /*
654          * Bit 0
655          *      When it is set, this is a "no-push" receive.
656          *      When it is clear, this is a "push" receive.
657          *
658          * Bit 1-15:  Reserved and shall be zero
659          */
660         uint16_t                                flags;
661
662         /*
663          * For debugging and diagnoses purpose.
664          * The SeqNo is per TCP connection and starts from 0.
665          */
666         uint32_t                                seq_no;
667 } __packed nvsp_2_msg_post_chimney_rx_request;
668
669 /*
670  * NvspMessage2TypePostChimneyRecvRequestComplete
671  */
672 typedef struct nvsp_2_msg_post_chimney_rx_request_complete_ {
673         /*
674          * The NDIS_STATUS for the chimney send
675          */
676         uint32_t                                status;
677
678         /*
679          * Number of bytes that have been sent to the peer (and ACKed by
680          * the peer).
681          */
682         uint32_t                                bytes_xferred;
683 } __packed nvsp_2_msg_post_chimney_rx_request_complete;
684
685 /*
686  * NvspMessage2TypeAllocateReceiveBuffer
687  */
688 typedef struct nvsp_2_msg_alloc_rx_buf_ {
689         /*
690          * Allocation ID to match the allocation request and response
691          */
692         uint32_t                                allocation_id;
693
694         /*
695          * Length of the VM shared memory receive buffer that needs to
696          * be allocated
697          */
698         uint32_t                                length;
699 } __packed nvsp_2_msg_alloc_rx_buf;
700
701 /*
702  * NvspMessage2TypeAllocateReceiveBufferComplete
703  */
704 typedef struct nvsp_2_msg_alloc_rx_buf_complete_ {
705         /*
706          * The NDIS_STATUS code for buffer allocation
707          */
708         uint32_t                                status;
709
710         /*
711          * Allocation ID from NVSP_2_MESSAGE_ALLOCATE_RECEIVE_BUFFER
712          */
713         uint32_t                                allocation_id;
714
715         /*
716          * GPADL handle for the allocated receive buffer
717          */
718         uint32_t                                gpadl_handle;
719
720         /*
721          * Receive buffer ID that is further used in
722          * NvspMessage2SendVmqRndisPacket
723          */
724         uint64_t                                rx_buf_id;
725 } __packed nvsp_2_msg_alloc_rx_buf_complete;
726
727 /*
728  * NvspMessage2TypeFreeReceiveBuffer
729  */
730 typedef struct nvsp_2_msg_free_rx_buf_ {
731         /*
732          * Receive buffer ID previous returned in
733          * NvspMessage2TypeAllocateReceiveBufferComplete message
734          */
735         uint64_t                                rx_buf_id;
736 } __packed nvsp_2_msg_free_rx_buf;
737
738 /*
739  * This structure is used in defining the buffers in
740  * NVSP_2_MESSAGE_SEND_VMQ_RNDIS_PACKET structure
741  */
742 typedef struct nvsp_xfer_page_range_ {
743         /*
744          * Specifies the ID of the receive buffer that has the buffer. This
745          * ID can be the general receive buffer ID specified in
746          * NvspMessage1TypeSendReceiveBuffer or it can be the shared memory
747          * receive buffer ID allocated by the VSC and specified in
748          * NvspMessage2TypeAllocateReceiveBufferComplete message
749          */
750         uint64_t                                xfer_page_set_id;
751
752         /*
753          * Number of bytes
754          */
755         uint32_t                                byte_count;
756
757         /*
758          * Offset in bytes from the beginning of the buffer
759          */
760         uint32_t                                byte_offset;
761 } __packed nvsp_xfer_page_range;
762
763 /*
764  * NvspMessage2SendVmqRndisPacket
765  */
766 typedef struct nvsp_2_msg_send_vmq_rndis_pkt_ {
767         /*
768          * This field is specified by RNIDS. They assume there's
769          * two different channels of communication. However,
770          * the Network VSP only has one. Therefore, the channel
771          * travels with the RNDIS packet. It must be RMC_DATA
772          */
773         uint32_t                                channel_type;
774
775         /*
776          * Only the Range element corresponding to the RNDIS header of
777          * the first RNDIS message in the multiple RNDIS messages sent
778          * in one NVSP message.  Information about the data portions as well
779          * as the subsequent RNDIS messages in the same NVSP message are
780          * embedded in the RNDIS header itself
781          */
782         nvsp_xfer_page_range                    range;
783 } __packed nvsp_2_msg_send_vmq_rndis_pkt;
784
785 /*
786  * This message is used by the VSC to complete
787  * a RNDIS VMQ message to the VSP.  At this point,
788  * the initiator of this message can use any resources
789  * associated with the original RNDIS VMQ packet.
790  */
791 typedef struct nvsp_2_msg_send_vmq_rndis_pkt_complete_
792 {
793         uint32_t                                status;
794 } __packed nvsp_2_msg_send_vmq_rndis_pkt_complete;
795
796
797 typedef union nvsp_1_msg_uber_ {
798         nvsp_1_msg_send_ndis_version            send_ndis_vers;
799
800         nvsp_1_msg_send_rx_buf                  send_rx_buf;
801         nvsp_1_msg_send_rx_buf_complete         send_rx_buf_complete;
802         nvsp_1_msg_revoke_rx_buf                revoke_rx_buf;
803
804         nvsp_1_msg_send_send_buf                send_send_buf;
805         nvsp_1_msg_send_send_buf_complete       send_send_buf_complete;
806         nvsp_1_msg_revoke_send_buf              revoke_send_buf;
807
808         nvsp_1_msg_send_rndis_pkt               send_rndis_pkt;
809         nvsp_1_msg_send_rndis_pkt_complete      send_rndis_pkt_complete;
810 } __packed nvsp_1_msg_uber;
811
812
813 typedef union nvsp_2_msg_uber_ {
814         nvsp_2_msg_send_ndis_config             send_ndis_config;
815
816         nvsp_2_msg_send_chimney_buf             send_chimney_buf;
817         nvsp_2_msg_send_chimney_buf_complete    send_chimney_buf_complete;
818         nvsp_2_msg_revoke_chimney_buf           revoke_chimney_buf;
819
820         nvsp_2_msg_resume_chimney_rx_indication resume_chimney_rx_indication;
821         nvsp_2_msg_terminate_chimney            terminate_chimney;
822         nvsp_2_msg_terminate_chimney_complete   terminate_chimney_complete;
823         nvsp_2_msg_indicate_chimney_event       indicate_chimney_event;
824
825         nvsp_2_msg_send_chimney_pkt             send_chimney_packet;
826         nvsp_2_msg_send_chimney_pkt_complete    send_chimney_packet_complete;
827         nvsp_2_msg_post_chimney_rx_request      post_chimney_rx_request;
828         nvsp_2_msg_post_chimney_rx_request_complete
829                                                post_chimney_rx_request_complete;
830
831         nvsp_2_msg_alloc_rx_buf                 alloc_rx_buffer;
832         nvsp_2_msg_alloc_rx_buf_complete        alloc_rx_buffer_complete;
833         nvsp_2_msg_free_rx_buf                  free_rx_buffer;
834
835         nvsp_2_msg_send_vmq_rndis_pkt           send_vmq_rndis_pkt;
836         nvsp_2_msg_send_vmq_rndis_pkt_complete  send_vmq_rndis_pkt_complete;
837         nvsp_2_msg_alloc_chimney_handle         alloc_chimney_handle;
838         nvsp_2_msg_alloc_chimney_handle_complete alloc_chimney_handle_complete;
839 } __packed nvsp_2_msg_uber;
840
841
842 typedef union nvsp_all_msgs_ {
843         nvsp_msg_init_uber                      init_msgs;
844         nvsp_1_msg_uber                         vers_1_msgs;
845         nvsp_2_msg_uber                         vers_2_msgs;
846 } __packed nvsp_all_msgs;
847
848 /*
849  * ALL Messages
850  */
851 typedef struct nvsp_msg_ {
852         nvsp_msg_hdr                            hdr; 
853         nvsp_all_msgs                           msgs;
854 } __packed nvsp_msg;
855
856
857 /*
858  * The following arguably belongs in a separate header file
859  */
860
861 /*
862  * Defines
863  */
864
865 #define NETVSC_SEND_BUFFER_SIZE                 (1024*1024*15)   /* 15M */
866 #define NETVSC_SEND_BUFFER_ID                   0xface
867
868 #define NETVSC_RECEIVE_BUFFER_SIZE_LEGACY       (1024*1024*15) /* 15MB */
869 #define NETVSC_RECEIVE_BUFFER_SIZE              (1024*1024*16) /* 16MB */
870
871 #define NETVSC_RECEIVE_BUFFER_ID                0xcafe
872
873 #define NETVSC_RECEIVE_SG_COUNT                 1
874
875 /* Preallocated receive packets */
876 #define NETVSC_RECEIVE_PACKETLIST_COUNT         256
877
878 /*
879  * Maximum MTU we permit to be configured for a netvsc interface.
880  * When the code was developed, a max MTU of 12232 was tested and
881  * proven to work.  9K is a reasonable maximum for an Ethernet.
882  */
883 #define NETVSC_MAX_CONFIGURABLE_MTU             (9 * 1024)
884
885 #define NETVSC_PACKET_SIZE                      PAGE_SIZE
886
887 /*
888  * Data types
889  */
890
891 /*
892  * Per netvsc channel-specific
893  */
894 typedef struct netvsc_dev_ {
895         struct hv_device                        *dev;
896         int                                     num_outstanding_sends;
897
898         /* Send buffer allocated by us but manages by NetVSP */
899         void                                    *send_buf;
900         uint32_t                                send_buf_size;
901         uint32_t                                send_buf_gpadl_handle;
902         uint32_t                                send_section_size;
903         uint32_t                                send_section_count;
904         unsigned long                           bitsmap_words;
905         unsigned long                           *send_section_bitsmap;
906
907         /* Receive buffer allocated by us but managed by NetVSP */
908         void                                    *rx_buf;
909         uint32_t                                rx_buf_size;
910         uint32_t                                rx_buf_gpadl_handle;
911         uint32_t                                rx_section_count;
912         nvsp_1_rx_buf_section                   *rx_sections;
913
914         /* Used for NetVSP initialization protocol */
915         struct sema                             channel_init_sema;
916         nvsp_msg                                channel_init_packet;
917
918         nvsp_msg                                revoke_packet;
919         /*uint8_t                               hw_mac_addr[HW_MACADDR_LEN];*/
920
921         /* Holds rndis device info */
922         void                                    *extension;
923
924         hv_bool_uint8_t                         destroy;
925         /* Negotiated NVSP version */
926         uint32_t                                nvsp_version;
927         
928         uint8_t                                 callback_buf[NETVSC_PACKET_SIZE]; 
929 } netvsc_dev;
930
931
932 typedef void (*pfn_on_send_rx_completion)(void *);
933
934 #define NETVSC_DEVICE_RING_BUFFER_SIZE  (128 * PAGE_SIZE)
935 #define NETVSC_PACKET_MAXPAGE           32 
936
937
938 #define NETVSC_VLAN_PRIO_MASK           0xe000
939 #define NETVSC_VLAN_PRIO_SHIFT          13
940 #define NETVSC_VLAN_VID_MASK            0x0fff
941
942 #define TYPE_IPV4                       2
943 #define TYPE_IPV6                       4
944 #define TYPE_TCP                        2
945 #define TYPE_UDP                        4
946
947 #define TRANSPORT_TYPE_NOT_IP           0
948 #define TRANSPORT_TYPE_IPV4_TCP         ((TYPE_IPV4 << 16) | TYPE_TCP)
949 #define TRANSPORT_TYPE_IPV4_UDP         ((TYPE_IPV4 << 16) | TYPE_UDP)
950 #define TRANSPORT_TYPE_IPV6_TCP         ((TYPE_IPV6 << 16) | TYPE_TCP)
951 #define TRANSPORT_TYPE_IPV6_UDP         ((TYPE_IPV6 << 16) | TYPE_UDP)
952
953 #ifdef __LP64__
954 #define BITS_PER_LONG 64
955 #else
956 #define BITS_PER_LONG 32
957 #endif
958
959 typedef struct netvsc_packet_ {
960         struct hv_device           *device;
961         hv_bool_uint8_t            is_data_pkt;      /* One byte */
962         uint16_t                   vlan_tci;
963         uint32_t status;
964
965         /* Completion */
966         union {
967                 struct {
968                         uint64_t   rx_completion_tid;
969                         void       *rx_completion_context;
970                         /* This is no longer used */
971                         pfn_on_send_rx_completion   on_rx_completion;
972                 } rx;
973                 struct {
974                         uint64_t    send_completion_tid;
975                         void        *send_completion_context;
976                         /* Still used in netvsc and filter code */
977                         pfn_on_send_rx_completion   on_send_completion;
978                 } send;
979         } compl;
980         uint32_t        send_buf_section_idx;
981         uint32_t        send_buf_section_size;
982
983         void            *rndis_mesg;
984         uint32_t        tot_data_buf_len;
985         void            *data;
986         uint32_t        page_buf_count;
987         hv_vmbus_page_buffer    page_buffers[NETVSC_PACKET_MAXPAGE];
988 } netvsc_packet;
989
990 typedef struct {
991         uint8_t         mac_addr[6];  /* Assumption unsigned long */
992         hv_bool_uint8_t link_state;
993 } netvsc_device_info;
994
995 #ifndef HN_USE_TXDESC_BUFRING
996 struct hn_txdesc;
997 SLIST_HEAD(hn_txdesc_list, hn_txdesc);
998 #else
999 struct buf_ring;
1000 #endif
1001
1002 struct hn_rx_ring {
1003         struct lro_ctrl hn_lro;
1004
1005         /* Trust csum verification on host side */
1006         int             hn_trust_hcsum; /* HN_TRUST_HCSUM_ */
1007
1008         u_long          hn_csum_ip;
1009         u_long          hn_csum_tcp;
1010         u_long          hn_csum_udp;
1011         u_long          hn_csum_trusted;
1012         u_long          hn_lro_tried;
1013         u_long          hn_small_pkts;
1014 } __aligned(CACHE_LINE_SIZE);
1015
1016 #define HN_TRUST_HCSUM_IP       0x0001
1017 #define HN_TRUST_HCSUM_TCP      0x0002
1018 #define HN_TRUST_HCSUM_UDP      0x0004
1019
1020 struct hn_tx_ring {
1021 #ifndef HN_USE_TXDESC_BUFRING
1022         struct mtx      hn_txlist_spin;
1023         struct hn_txdesc_list hn_txlist;
1024 #else
1025         struct buf_ring *hn_txdesc_br;
1026 #endif
1027         int             hn_txdesc_cnt;
1028         int             hn_txdesc_avail;
1029         int             hn_txeof;
1030
1031         int             hn_sched_tx;
1032         struct taskqueue *hn_tx_taskq;
1033         struct task     hn_start_task;
1034         struct task     hn_txeof_task;
1035
1036         struct mtx      hn_tx_lock;
1037         struct hn_softc *hn_sc;
1038
1039         int             hn_direct_tx_size;
1040         int             hn_tx_chimney_size;
1041         bus_dma_tag_t   hn_tx_data_dtag;
1042         uint64_t        hn_csum_assist;
1043
1044         u_long          hn_no_txdescs;
1045         u_long          hn_send_failed;
1046         u_long          hn_txdma_failed;
1047         u_long          hn_tx_collapsed;
1048         u_long          hn_tx_chimney;
1049
1050         /* Rarely used stuffs */
1051         struct hn_txdesc *hn_txdesc;
1052         bus_dma_tag_t   hn_tx_rndis_dtag;
1053         struct sysctl_oid *hn_tx_sysctl_tree;
1054 } __aligned(CACHE_LINE_SIZE);
1055
1056 /*
1057  * Device-specific softc structure
1058  */
1059 typedef struct hn_softc {
1060         struct ifnet    *hn_ifp;
1061         struct arpcom   arpcom;
1062         struct ifmedia  hn_media;
1063         device_t        hn_dev;
1064         uint8_t         hn_unit;
1065         int             hn_carrier;
1066         int             hn_if_flags;
1067         struct mtx      hn_lock;
1068         int             hn_initdone;
1069         /* See hv_netvsc_drv_freebsd.c for rules on how to use */
1070         int             temp_unusable;
1071         struct hv_device  *hn_dev_obj;
1072         netvsc_dev      *net_dev;
1073
1074         int             hn_rx_ring_cnt;
1075         struct hn_rx_ring *hn_rx_ring;
1076
1077         int             hn_tx_ring_cnt;
1078         struct hn_tx_ring *hn_tx_ring;
1079         int             hn_tx_chimney_max;
1080         struct taskqueue *hn_tx_taskq;
1081         struct sysctl_oid *hn_tx_sysctl_tree;
1082 } hn_softc_t;
1083
1084 /*
1085  * Externs
1086  */
1087 extern int hv_promisc_mode;
1088
1089 void netvsc_linkstatus_callback(struct hv_device *device_obj, uint32_t status);
1090 void netvsc_xmit_completion(void *context);
1091 void hv_nv_on_receive_completion(struct hv_device *device,
1092     uint64_t tid, uint32_t status);
1093 netvsc_dev *hv_nv_on_device_add(struct hv_device *device,
1094     void *additional_info);
1095 int hv_nv_on_device_remove(struct hv_device *device,
1096     boolean_t destroy_channel);
1097 int hv_nv_on_send(struct hv_device *device, netvsc_packet *pkt);
1098 int hv_nv_get_next_send_section(netvsc_dev *net_dev);
1099
1100 #endif  /* __HV_NET_VSC_H__ */
1101