win-pvdrivers

view xennet/xennet.h @ 1025:aa2e51f67f7c

Fix hibernate under Win8. Change debugprints.
author James Harper <james.harper@bendigoit.com.au>
date Tue Feb 19 15:14:53 2013 +1100 (2013-02-19)
parents c21a4feb4a27
children fba0ce4d9e54
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
4 Copyright (C) 2007 Andrew Grover <andy.grover@oracle.com>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
21 #pragma warning(disable: 4201)
22 #pragma warning(disable: 4214)
24 #include <ntddk.h>
25 #include <wdm.h>
26 #define NDIS_MINIPORT_DRIVER 1
27 #if NTDDI_VERSION < NTDDI_WINXP
28 # define NDIS50_MINIPORT 1
29 #elif NTDDI_VERSION < NTDDI_VISTA
30 # define NDIS51_MINIPORT 1
31 #else
32 # define NDIS61_MINIPORT 1
33 #endif
34 #include <ndis.h>
35 #define NTSTRSAFE_LIB
36 #include <ntstrsafe.h>
37 #include <liblfds.h>
39 #define VENDOR_DRIVER_VERSION_MAJOR 0
40 #define VENDOR_DRIVER_VERSION_MINOR 11
42 #define MAX_LINK_SPEED 10000000000L /* there is not really any theoretical maximum... */
44 #define VENDOR_DRIVER_VERSION (((VENDOR_DRIVER_VERSION_MAJOR) << 16) | (VENDOR_DRIVER_VERSION_MINOR))
46 #define __DRIVER_NAME "XenNet"
48 #define PACKET_NEXT_PACKET_FIELD MiniportReservedEx[sizeof(PVOID)] // RX & TX
49 #define PACKET_FIRST_PB_FIELD MiniportReservedEx[0] // RX
50 #define PACKET_LIST_ENTRY_FIELD MiniportReservedEx[sizeof(PVOID)] // TX (2 entries)
51 #define PACKET_NEXT_PACKET(_packet) (*(PNDIS_PACKET *)&(_packet)->PACKET_NEXT_PACKET_FIELD)
52 #define PACKET_LIST_ENTRY(_packet) (*(PLIST_ENTRY)&(_packet)->PACKET_LIST_ENTRY_FIELD)
53 #define PACKET_FIRST_PB(_packet) (*(shared_buffer_t **)&(_packet)->PACKET_FIRST_PB_FIELD)
55 #define NB_LIST_ENTRY_FIELD MiniportReserved[0] // TX (2 entries)
56 #define NB_FIRST_PB_FIELD MiniportReserved[0] // RX
57 #define NB_NBL_FIELD MiniportReserved[2] // TX
58 #define NB_LIST_ENTRY(_nb) (*(PLIST_ENTRY)&(_nb)->NB_LIST_ENTRY_FIELD)
59 #define NB_NBL(_nb) (*(PNET_BUFFER_LIST *)&(_nb)->NB_NBL_FIELD)
60 #define NB_FIRST_PB(_nb) (*(shared_buffer_t **)&(_nb)->NB_FIRST_PB_FIELD)
62 #define NBL_REF_FIELD MiniportReserved[0] // TX
63 #define NBL_REF(_nbl) (*(ULONG_PTR *)&(_nbl)->NBL_REF_FIELD)
65 #define NDIS_STATUS_RESOURCES_MAX_LENGTH 64
67 #include <xen_windows.h>
68 #include <memory.h>
69 #include <grant_table.h>
70 #include <event_channel.h>
71 #include <hvm/params.h>
72 #include <hvm/hvm_op.h>
73 #include <xen_public.h>
74 #include <io/ring.h>
75 #include <io/netif.h>
76 #include <io/xenbus.h>
77 #include <stdlib.h>
78 #define XENNET_POOL_TAG (ULONG) 'XenN'
80 /* Xen macros use these, so they need to be redefined to Win equivs */
81 #define wmb() KeMemoryBarrier()
82 #define mb() KeMemoryBarrier()
84 #define GRANT_INVALID_REF 0
86 #define NAME_SIZE 64
88 #define ETH_ALEN 6
90 static FORCEINLINE USHORT
91 GET_NET_USHORT(USHORT data) {
92 return (data << 8) | (data >> 8);
93 }
95 static FORCEINLINE USHORT
96 GET_NET_PUSHORT(PVOID pdata) {
97 return (*((PUSHORT)pdata) << 8) | (*((PUSHORT)pdata) >> 8);
98 }
100 static FORCEINLINE VOID
101 SET_NET_USHORT(PVOID ptr, USHORT data) {
102 *((PUSHORT)ptr) = GET_NET_USHORT(data);
103 }
105 static FORCEINLINE ULONG
106 GET_NET_ULONG(ULONG data) {
107 ULONG tmp;
109 tmp = ((data & 0x00ff00ff) << 8) | ((data & 0xff00ff00) >> 8);
110 return (tmp << 16) | (tmp >> 16);
111 }
113 static FORCEINLINE ULONG
114 GET_NET_PULONG(PVOID pdata) {
115 ULONG tmp;
117 tmp = ((*((PULONG)pdata) & 0x00ff00ff) << 8) | ((*((PULONG)pdata) & 0xff00ff00) >> 8);
118 return (tmp << 16) | (tmp >> 16);
119 }
121 static FORCEINLINE VOID
122 SET_NET_ULONG(PVOID ptr, ULONG data) {
123 *((PULONG)ptr) = GET_NET_ULONG(data);
124 }
125 /*
126 #define GET_NET_ULONG(x) ((GET_NET_USHORT(x) << 16) | GET_NET_USHORT(((PUCHAR)&x)[2]))
127 #define SET_NET_ULONG(y, x) *((ULONG *)&(y)) = ((GET_NET_USHORT(x) << 16) | GET_NET_USHORT(((PUCHAR)&x)[2]))
128 */
130 #define SUPPORTED_PACKET_FILTERS (\
131 NDIS_PACKET_TYPE_DIRECTED | \
132 NDIS_PACKET_TYPE_MULTICAST | \
133 NDIS_PACKET_TYPE_BROADCAST | \
134 NDIS_PACKET_TYPE_PROMISCUOUS | \
135 NDIS_PACKET_TYPE_ALL_MULTICAST)
137 /* couldn't get regular xen ring macros to work...*/
138 #define __NET_RING_SIZE(type, _sz) \
139 (__RD32( \
140 (_sz - sizeof(struct type##_sring) + sizeof(union type##_sring_entry)) \
141 / sizeof(union type##_sring_entry)))
143 #define NET_TX_RING_SIZE __NET_RING_SIZE(netif_tx, PAGE_SIZE)
144 #define NET_RX_RING_SIZE __NET_RING_SIZE(netif_rx, PAGE_SIZE)
146 #pragma warning(disable: 4127) // conditional expression is constant
148 #define MIN_LARGE_SEND_SEGMENTS 4
150 /* TODO: crank this up if we support higher mtus? */
151 #define XN_HDR_SIZE 14
152 #define XN_MAX_DATA_SIZE 1500
153 #define XN_MIN_FRAME_SIZE 60
154 #define XN_MAX_FRAME_SIZE (XN_HDR_SIZE + XN_DATA_SIZE)
155 /*
156 #if !defined(OFFLOAD_LARGE_SEND)
157 #define XN_MAX_PKT_SIZE (XN_HDR_SIZE + XN_DATA_SIZE)
158 #else
159 #define XN_MAX_PKT_SIZE MAX_LARGE_SEND_OFFLOAD
160 #endif
161 */
163 #define XN_MAX_SEND_PKTS 16
165 #define XENSOURCE_MAC_HDR 0x00163E
166 #define XN_VENDOR_DESC "Xensource"
167 #define MAX_XENBUS_STR_LEN 128
169 #define RX_MIN_TARGET 8
170 #define RX_DFL_MIN_TARGET 256
171 #define RX_MAX_TARGET min(NET_RX_RING_SIZE, 256)
172 #define RX_MAX_PB_FREELIST (RX_MAX_TARGET * 4)
174 //#define MAX_BUFFERS_PER_PACKET NET_RX_RING_SIZE
176 #define MIN_ETH_HEADER_LENGTH 14
177 #define MAX_ETH_HEADER_LENGTH 14
178 #define MIN_IP4_HEADER_LENGTH 20
179 #define MAX_IP4_HEADER_LENGTH (15 * 4)
180 #define MIN_TCP_HEADER_LENGTH 20
181 #define MAX_TCP_HEADER_LENGTH (15 * 4)
182 #define MAX_PKT_HEADER_LENGTH (MAX_ETH_HEADER_LENGTH + MAX_IP4_HEADER_LENGTH + MAX_TCP_HEADER_LENGTH)
184 #define MIN_LOOKAHEAD_LENGTH (MAX_IP4_HEADER_LENGTH + MAX_TCP_HEADER_LENGTH)
185 #define MAX_LOOKAHEAD_LENGTH PAGE_SIZE
187 #define LINUX_MAX_SG_ELEMENTS 19
189 #define PAGE_LIST_SIZE (max(NET_RX_RING_SIZE, NET_TX_RING_SIZE) * 4)
190 #define MULTICAST_LIST_MAX_SIZE 32
192 #define TX_HEADER_BUFFER_SIZE 512
193 #define TX_COALESCE_BUFFERS (NET_TX_RING_SIZE)
195 /* split incoming large packets into MSS sized chunks */
196 #define RX_LSO_SPLIT_MSS 0
197 /* split incoming large packets in half, to not invoke the delayed ack timer */
198 #define RX_LSO_SPLIT_HALF 1
199 /* don't split incoming large packets. not really useful */
200 #define RX_LSO_SPLIT_NONE 2
202 #define DEVICE_STATE_DISCONNECTED 0 /* -> INITIALISING */
203 #define DEVICE_STATE_INITIALISING 1 /* -> ACTIVE or INACTIVE */
204 #define DEVICE_STATE_INACTIVE 2
205 #define DEVICE_STATE_ACTIVE 3 /* -> DISCONNECTING */
206 #define DEVICE_STATE_DISCONNECTING 4 /* -> DISCONNECTED */
208 struct _shared_buffer_t;
210 typedef struct _shared_buffer_t shared_buffer_t;
212 struct _shared_buffer_t {
213 struct netif_rx_response rsp;
214 shared_buffer_t *next;
215 grant_ref_t gref;
216 //USHORT offset;
217 PVOID virtual;
218 PMDL mdl;
219 //USHORT id;
220 volatile LONG ref_count;
221 };
223 typedef struct {
224 #if NTDDI_VERSION < NTDDI_VISTA
225 PNDIS_PACKET packet; /* only set on the last packet */
226 #else
227 PNET_BUFFER packet; /* only set on the last packet */
228 #endif
229 PVOID *cb;
230 grant_ref_t gref;
231 } tx_shadow_t;
233 typedef struct {
234 ULONG parse_result;
235 PMDL first_mdl;
236 MDL first_mdl_storage;
237 PPFN_NUMBER first_mdl_pfns[17]; /* maximum possible packet size */
238 PMDL curr_mdl;
239 shared_buffer_t *first_pb;
240 shared_buffer_t *curr_pb;
241 PUCHAR first_mdl_virtual;
242 //ULONG mdl_count;
243 ULONG first_mdl_offset;
244 ULONG first_mdl_length;
245 ULONG curr_mdl_offset;
246 USHORT mss;
247 //NDIS_TCP_IP_CHECKSUM_PACKET_INFO csum_info;
248 BOOLEAN csum_blank;
249 BOOLEAN data_validated;
250 BOOLEAN split_required;
251 UCHAR ip_version;
252 PUCHAR header;
253 ULONG header_length;
254 UCHAR ip_proto;
255 BOOLEAN ip_has_options;
256 ULONG total_length;
257 USHORT ip4_header_length;
258 USHORT ip4_length;
259 USHORT tcp_header_length;
260 BOOLEAN tcp_has_options;
261 USHORT tcp_length;
262 USHORT tcp_remaining;
263 ULONG tcp_seq;
264 BOOLEAN is_multicast;
265 BOOLEAN is_broadcast;
266 /* anything past here doesn't get cleared automatically by the ClearPacketInfo */
267 UCHAR header_data[MAX_LOOKAHEAD_LENGTH + MAX_ETH_HEADER_LENGTH];
268 } packet_info_t;
270 struct xennet_info
271 {
272 ULONG device_state;
274 /* Base device vars */
275 PDEVICE_OBJECT pdo;
276 PDEVICE_OBJECT fdo;
277 PDEVICE_OBJECT lower_do;
278 // WCHAR dev_desc[NAME_SIZE];
280 /* NDIS-related vars */
281 NDIS_HANDLE adapter_handle;
282 ULONG packet_filter;
283 uint8_t perm_mac_addr[ETH_ALEN];
284 uint8_t curr_mac_addr[ETH_ALEN];
285 ULONG current_lookahead;
287 /* Misc. Xen vars */
288 XN_HANDLE handle;
290 evtchn_port_t event_channel;
291 ULONG backend_state;
292 KEVENT backend_event;
293 UCHAR multicast_list[MULTICAST_LIST_MAX_SIZE][6];
294 ULONG multicast_list_size;
295 KDPC rxtx_dpc;
297 /* tx related - protected by tx_lock */
298 KSPIN_LOCK tx_lock; /* always acquire rx_lock before tx_lock */
299 LIST_ENTRY tx_waiting_pkt_list;
300 netif_tx_sring_t *tx_sring;
301 grant_ref_t tx_sring_gref;
302 struct netif_tx_front_ring tx_ring;
303 ULONG tx_ring_free;
304 tx_shadow_t tx_shadows[NET_TX_RING_SIZE];
305 ULONG tx_outstanding;
306 ULONG tx_id_free;
307 USHORT tx_id_list[NET_TX_RING_SIZE];
308 NPAGED_LOOKASIDE_LIST tx_lookaside_list;
309 KEVENT tx_idle_event;
311 /* rx_related - protected by rx_lock */
312 KSPIN_LOCK rx_lock; /* always acquire rx_lock before tx_lock */
313 netif_rx_sring_t *rx_sring;
314 grant_ref_t rx_sring_gref;
315 struct netif_rx_front_ring rx_ring;
316 ULONG rx_id_free;
317 packet_info_t *rxpi;
318 #if NTDDI_VERSION < NTDDI_VISTA
319 #else
320 NDIS_HANDLE rx_nbl_pool;
321 #endif
322 NDIS_HANDLE rx_packet_pool;
323 volatile LONG rx_pb_free;
324 struct stack_state *rx_pb_stack;
325 volatile LONG rx_hb_free;
326 struct stack_state *rx_hb_stack;
327 shared_buffer_t *rx_ring_pbs[NET_RX_RING_SIZE];
328 /* Receive-ring batched refills. */
329 ULONG rx_target;
330 ULONG rx_max_target;
331 ULONG rx_min_target;
332 shared_buffer_t *rx_partial_buf;
333 BOOLEAN rx_partial_extra_info_flag ;
334 BOOLEAN rx_partial_more_data_flag;
335 KEVENT rx_idle_event;
336 /* how many packets are in the net stack atm */
337 LONG rx_outstanding;
340 /* config vars from registry */
341 /* the frontend_* indicate our willingness to support */
342 BOOLEAN frontend_sg_supported;
343 BOOLEAN frontend_csum_supported;
344 ULONG frontend_gso_value;
345 ULONG frontend_mtu_value;
346 ULONG frontend_gso_rx_split_type; /* RX_LSO_SPLIT_* */
348 BOOLEAN backend_sg_supported;
349 BOOLEAN backend_csum_supported;
350 ULONG backend_gso_value;
352 BOOLEAN current_sg_supported;
353 BOOLEAN current_csum_supported;
354 ULONG current_gso_value;
355 ULONG current_mtu_value;
356 ULONG current_gso_rx_split_type;
358 BOOLEAN config_csum_rx_check;
359 BOOLEAN config_csum_rx_dont_fix;
361 #if NTDDI_VERSION < NTDDI_VISTA
362 NDIS_TASK_TCP_IP_CHECKSUM setting_csum;
363 #else
364 #endif
366 /* config stuff calculated from the above */
367 ULONG config_max_pkt_size;
369 /* stats */
370 #if NTDDI_VERSION < NTDDI_VISTA
371 ULONG64 stat_tx_ok;
372 ULONG64 stat_rx_ok;
373 ULONG64 stat_tx_error;
374 ULONG64 stat_rx_error;
375 ULONG64 stat_rx_no_buffer;
376 #else
377 NDIS_STATISTICS_INFO stats;
378 #endif
380 } typedef xennet_info_t;
382 extern USHORT ndis_os_major_version;
383 extern USHORT ndis_os_minor_version;
385 typedef NDIS_STATUS (*XEN_OID_REQUEST)(NDIS_HANDLE context, PVOID information_buffer, ULONG information_buffer_length, PULONG bytes_read, PULONG bytes_needed);
387 struct xennet_oids_t {
388 ULONG oid;
389 char *oid_name;
390 ULONG min_length;
391 XEN_OID_REQUEST query_routine;
392 XEN_OID_REQUEST set_routine;
393 };
395 extern struct xennet_oids_t xennet_oids[];
397 #if NTDDI_VERSION < NTDDI_VISTA
398 NDIS_STATUS
399 XenNet_QueryInformation(
400 IN NDIS_HANDLE MiniportAdapterContext,
401 IN NDIS_OID Oid,
402 IN PVOID InformationBuffer,
403 IN ULONG InformationBufferLength,
404 OUT PULONG BytesWritten,
405 OUT PULONG BytesNeeded);
407 NDIS_STATUS
408 XenNet_SetInformation(
409 IN NDIS_HANDLE MiniportAdapterContext,
410 IN NDIS_OID Oid,
411 IN PVOID InformationBuffer,
412 IN ULONG InformationBufferLength,
413 OUT PULONG BytesRead,
414 OUT PULONG BytesNeeded
415 );
417 VOID
418 XenNet_SendPackets(
419 IN NDIS_HANDLE MiniportAdapterContext,
420 IN PPNDIS_PACKET PacketArray,
421 IN UINT NumberOfPackets
422 );
424 VOID
425 XenNet_ReturnPacket(
426 IN NDIS_HANDLE MiniportAdapterContext,
427 IN PNDIS_PACKET Packet
428 );
429 #else
431 MINIPORT_OID_REQUEST XenNet_OidRequest;
432 MINIPORT_CANCEL_OID_REQUEST XenNet_CancelOidRequest;
434 MINIPORT_SEND_NET_BUFFER_LISTS XenNet_SendNetBufferLists;
435 MINIPORT_CANCEL_SEND XenNet_CancelSend;
437 MINIPORT_RETURN_NET_BUFFER_LISTS XenNet_ReturnNetBufferLists;
438 #endif
440 NTSTATUS XenNet_Connect(PVOID context, BOOLEAN suspend);
441 NTSTATUS XenNet_Disconnect(PVOID context, BOOLEAN suspend);
442 VOID XenNet_DeviceCallback(PVOID context, ULONG callback_type, PVOID value);
445 BOOLEAN XenNet_RxInit(xennet_info_t *xi);
446 VOID XenNet_RxShutdown(xennet_info_t *xi);
447 BOOLEAN XenNet_RxBufferCheck(struct xennet_info *xi);
449 BOOLEAN XenNet_TxInit(xennet_info_t *xi);
450 BOOLEAN XenNet_TxShutdown(xennet_info_t *xi);
451 VOID XenNet_TxBufferGC(struct xennet_info *xi, BOOLEAN dont_set_event);
454 /* return values */
455 #define PARSE_OK 0
456 #define PARSE_TOO_SMALL 1 /* first buffer is too small */
457 #define PARSE_UNKNOWN_TYPE 2
459 BOOLEAN XenNet_BuildHeader(packet_info_t *pi, PVOID header, ULONG new_header_size);
460 VOID XenNet_ParsePacketHeader(packet_info_t *pi, PUCHAR buffer, ULONG min_header_size);
461 BOOLEAN XenNet_FilterAcceptPacket(struct xennet_info *xi, packet_info_t *pi);
463 BOOLEAN XenNet_CheckIpHeaderSum(PUCHAR header, USHORT ip4_header_length);
464 VOID XenNet_SumIpHeader(PUCHAR header, USHORT ip4_header_length);
466 static __forceinline VOID
467 XenNet_ClearPacketInfo(packet_info_t *pi) {
468 RtlZeroMemory(pi, sizeof(packet_info_t) - FIELD_OFFSET(packet_info_t, header_data));
469 }
471 /* Get some data from the current packet, but don't cross a page boundry. */
472 static __forceinline ULONG
473 XenNet_QueryData(packet_info_t *pi, ULONG length) {
474 ULONG offset_in_page;
476 if (length > MmGetMdlByteCount(pi->curr_mdl) - pi->curr_mdl_offset)
477 length = MmGetMdlByteCount(pi->curr_mdl) - pi->curr_mdl_offset;
479 offset_in_page = (MmGetMdlByteOffset(pi->curr_mdl) + pi->curr_mdl_offset) & (PAGE_SIZE - 1);
480 if (offset_in_page + length > PAGE_SIZE)
481 length = PAGE_SIZE - offset_in_page;
483 return length;
484 }
486 /* Move the pointers forward by the given amount. No error checking is done. */
487 static __forceinline VOID
488 XenNet_EatData(packet_info_t *pi, ULONG length) {
489 pi->curr_mdl_offset += length;
490 if (pi->curr_mdl_offset >= MmGetMdlByteCount(pi->curr_mdl)) {
491 pi->curr_mdl_offset -= MmGetMdlByteCount(pi->curr_mdl);
492 #if NTDDI_VERSION < NTDDI_VISTA
493 NdisGetNextBuffer(pi->curr_mdl, &pi->curr_mdl);
494 #else
495 NdisGetNextMdl(pi->curr_mdl, &pi->curr_mdl);
496 #endif
497 }
498 }