win-pvdrivers

view xennet/xennet.c @ 391:8e10579159a0

implement some macros for common entry/exit kdprints etc. msvc and gcc have different defines so this will make life with 2 compile environments easier.
I couldn't test on ddk before checking in, will try it tonight and fix compile if needed.
author Andy Grover <andy.grover@oracle.com>
date Tue Jul 15 18:27:06 2008 -0700 (2008-07-15)
parents d76831a77d19
children 7259dad4e96c fdb7f8853695
line source
1 /*
2 PV Net Driver 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 #include <stdlib.h>
22 #include <io/xenbus.h>
23 #include "xennet.h"
25 /* ----- BEGIN Other people's code --------- */
26 /* from linux/include/linux/ctype.h, used under GPLv2 */
27 #define _U 0x01 /* upper */
28 #define _L 0x02 /* lower */
29 #define _D 0x04 /* digit */
30 #define _C 0x08 /* cntrl */
31 #define _P 0x10 /* punct */
32 #define _S 0x20 /* white space (space/lf/tab) */
33 #define _X 0x40 /* hex digit */
34 #define _SP 0x80 /* hard space (0x20) */
36 /* from linux/include/lib/ctype.c, used under GPLv2 */
37 unsigned char _ctype[] = {
38 _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
39 _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
40 _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
41 _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
42 _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
43 _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
44 _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
45 _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
46 _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
47 _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
48 _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
49 _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
50 _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
51 _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
52 _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
53 _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
54 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
55 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
56 _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
57 _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
58 _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
59 _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
60 _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
61 _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
63 /* from linux/include/linux/ctype.h, used under GPLv2 */
64 #define __ismask(x) (_ctype[(int)(unsigned char)(x)])
66 #define isalnum(c) ((__ismask(c)&(_U|_L|_D)) != 0)
67 #define isalpha(c) ((__ismask(c)&(_U|_L)) != 0)
68 #define iscntrl(c) ((__ismask(c)&(_C)) != 0)
69 #define isdigit(c) ((__ismask(c)&(_D)) != 0)
70 #define isgraph(c) ((__ismask(c)&(_P|_U|_L|_D)) != 0)
71 #define islower(c) ((__ismask(c)&(_L)) != 0)
72 #define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
73 #define ispunct(c) ((__ismask(c)&(_P)) != 0)
74 #define isspace(c) ((__ismask(c)&(_S)) != 0)
75 #define isupper(c) ((__ismask(c)&(_U)) != 0)
76 #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
78 #define TOLOWER(x) ((x) | 0x20)
80 /* from linux/lib/vsprintf.c, used under GPLv2 */
81 /* Copyright (C) 1991, 1992 Linus Torvalds
82 * Wirzenius wrote this portably, Torvalds fucked it up :-)
83 */
84 /**
85 * simple_strtoul - convert a string to an unsigned long
86 * @cp: The start of the string
87 * @endp: A pointer to the end of the parsed string will be placed here
88 * @base: The number base to use
89 */
90 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
91 {
92 unsigned long result = 0,value;
94 if (!base) {
95 base = 10;
96 if (*cp == '0') {
97 base = 8;
98 cp++;
99 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
100 cp++;
101 base = 16;
102 }
103 }
104 } else if (base == 16) {
105 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
106 cp += 2;
107 }
108 while (isxdigit(*cp) &&
109 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
110 result = result*base + value;
111 cp++;
112 }
113 if (endp)
114 *endp = (char *)cp;
115 return result;
116 }
117 /* end vsprintf.c code */
118 /* ----- END Other people's code --------- */
120 // Called at DISPATCH_LEVEL
122 static DDKAPI VOID
123 XenNet_InterruptIsr(
124 PBOOLEAN InterruptRecognized,
125 PBOOLEAN QueueMiniportHandleInterrupt,
126 NDIS_HANDLE MiniportAdapterContext)
127 {
128 struct xennet_info *xi = MiniportAdapterContext;
130 //FUNCTION_ENTER();
132 *QueueMiniportHandleInterrupt = (BOOLEAN)!!xi->connected;
133 *InterruptRecognized = FALSE; /* we can't be sure here... */
135 //FUNCTION_EXIT();
136 }
138 static DDKAPI VOID
139 XenNet_InterruptDpc(NDIS_HANDLE MiniportAdapterContext)
140 {
141 struct xennet_info *xi = MiniportAdapterContext;
143 //FUNCTION_ENTER();
144 if (xi->connected)
145 {
146 XenNet_TxBufferGC(xi);
147 XenNet_RxBufferCheck(xi);
148 }
149 //FUNCTION_EXIT();
150 }
152 static NDIS_STATUS
153 XenNet_ConnectBackend(struct xennet_info *xi)
154 {
155 PUCHAR ptr;
156 UCHAR type;
157 PCHAR setting, value;
158 UINT i;
160 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
162 ptr = xi->config_page;
163 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value)) != XEN_INIT_TYPE_END)
164 {
165 switch(type)
166 {
167 case XEN_INIT_TYPE_RING: /* frontend ring */
168 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, value));
169 if (strcmp(setting, "tx-ring-ref") == 0)
170 {
171 FRONT_RING_INIT(&xi->tx, (netif_tx_sring_t *)value, PAGE_SIZE);
172 } else if (strcmp(setting, "rx-ring-ref") == 0)
173 {
174 FRONT_RING_INIT(&xi->rx, (netif_rx_sring_t *)value, PAGE_SIZE);
175 }
176 break;
177 case XEN_INIT_TYPE_EVENT_CHANNEL_IRQ: /* frontend event channel */
178 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, PtrToUlong(value)));
179 if (strcmp(setting, "event-channel") == 0)
180 {
181 xi->event_channel = PtrToUlong(value);
182 }
183 break;
184 case XEN_INIT_TYPE_READ_STRING_BACK:
185 case XEN_INIT_TYPE_READ_STRING_FRONT:
186 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
187 if (strcmp(setting, "mac") == 0)
188 {
189 char *s, *e;
190 s = value;
191 for (i = 0; i < ETH_ALEN; i++) {
192 xi->perm_mac_addr[i] = (uint8_t)simple_strtoul(s, &e, 16);
193 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
194 KdPrint((__DRIVER_NAME "Error parsing MAC address\n"));
195 }
196 s = e + 1;
197 }
198 memcpy(xi->curr_mac_addr, xi->perm_mac_addr, ETH_ALEN);
199 }
200 break;
201 case XEN_INIT_TYPE_VECTORS:
202 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
203 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
204 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
205 {
206 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
207 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
208 FUNCTION_ERROR_EXIT();
209 return NDIS_STATUS_ADAPTER_NOT_FOUND;
210 }
211 else
212 memcpy(&xi->vectors, value, sizeof(XENPCI_VECTORS));
213 break;
214 case XEN_INIT_TYPE_STATE_PTR:
215 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
216 xi->device_state = (PXENPCI_DEVICE_STATE)value;
217 break;
218 default:
219 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
220 break;
221 }
222 }
223 return NDIS_STATUS_SUCCESS;
224 }
226 static DDKAPI VOID
227 XenNet_Resume(PDEVICE_OBJECT device_object, PVOID context)
228 {
229 struct xennet_info *xi = context;
231 UNREFERENCED_PARAMETER(device_object);
233 XenNet_RxResumeStart(xi);
234 XenNet_TxResumeStart(xi);
235 XenNet_ConnectBackend(xi);
236 xi->device_state->resume_state = RESUME_STATE_RUNNING;
237 XenNet_RxResumeEnd(xi);
238 XenNet_TxResumeEnd(xi);
239 NdisMSetPeriodicTimer(&xi->resume_timer, 100);
240 }
242 static DDKAPI VOID
243 XenResumeCheck_Timer(
244 PVOID SystemSpecific1,
245 PVOID FunctionContext,
246 PVOID SystemSpecific2,
247 PVOID SystemSpecific3
248 )
249 {
250 struct xennet_info *xi = FunctionContext;
251 PIO_WORKITEM work_item;
252 BOOLEAN timer_cancelled;
254 UNREFERENCED_PARAMETER(SystemSpecific1);
255 UNREFERENCED_PARAMETER(SystemSpecific2);
256 UNREFERENCED_PARAMETER(SystemSpecific3);
258 if (xi->device_state->resume_state == RESUME_STATE_FRONTEND_RESUME)
259 {
260 NdisMCancelTimer(&xi->resume_timer, &timer_cancelled);
261 work_item = IoAllocateWorkItem(xi->fdo);
262 IoQueueWorkItem(work_item, XenNet_Resume, DelayedWorkQueue, xi);
263 }
264 }
266 // Called at <= DISPATCH_LEVEL
267 static DDKAPI NDIS_STATUS
268 XenNet_Init(
269 OUT PNDIS_STATUS OpenErrorStatus,
270 OUT PUINT SelectedMediumIndex,
271 IN PNDIS_MEDIUM MediumArray,
272 IN UINT MediumArraySize,
273 IN NDIS_HANDLE MiniportAdapterHandle,
274 IN NDIS_HANDLE WrapperConfigurationContext
275 )
276 {
277 NDIS_STATUS status;
278 BOOLEAN medium_found = FALSE;
279 struct xennet_info *xi = NULL;
280 ULONG nrl_length;
281 PNDIS_RESOURCE_LIST nrl;
282 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
283 KIRQL irq_level = 0;
284 ULONG irq_vector = 0;
285 NDIS_HANDLE config_handle;
286 NDIS_STRING config_param_name;
287 PNDIS_CONFIGURATION_PARAMETER config_param;
288 ULONG i;
289 PUCHAR ptr;
290 UCHAR type;
291 PCHAR setting, value;
292 ULONG length;
293 CHAR buf[128];
295 UNREFERENCED_PARAMETER(OpenErrorStatus);
297 FUNCTION_ENTER();
299 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
301 /* deal with medium stuff */
302 for (i = 0; i < MediumArraySize; i++)
303 {
304 if (MediumArray[i] == NdisMedium802_3)
305 {
306 medium_found = TRUE;
307 break;
308 }
309 }
310 if (!medium_found)
311 {
312 KdPrint(("NIC_MEDIA_TYPE not in MediumArray\n"));
313 return NDIS_STATUS_UNSUPPORTED_MEDIA;
314 }
315 *SelectedMediumIndex = i;
317 /* Alloc memory for adapter private info */
318 status = NdisAllocateMemoryWithTag((PVOID)&xi, sizeof(*xi), XENNET_POOL_TAG);
319 if (!NT_SUCCESS(status))
320 {
321 KdPrint(("NdisAllocateMemoryWithTag failed with 0x%x\n", status));
322 status = NDIS_STATUS_RESOURCES;
323 goto err;
324 }
325 RtlZeroMemory(xi, sizeof(*xi));
326 xi->adapter_handle = MiniportAdapterHandle;
327 xi->rx_target = RX_DFL_MIN_TARGET;
328 xi->rx_min_target = RX_DFL_MIN_TARGET;
329 xi->rx_max_target = RX_MAX_TARGET;
330 NdisMSetAttributesEx(xi->adapter_handle, (NDIS_HANDLE) xi,
331 0, NDIS_ATTRIBUTE_DESERIALIZE, // | NDIS_ATTRIBUTE_BUS_MASTER),
332 NdisInterfaceInternal);
334 nrl_length = 0;
335 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
336 NULL, (PUINT)&nrl_length);
337 KdPrint((__DRIVER_NAME " nrl_length = %d\n", nrl_length));
338 status = NdisAllocateMemoryWithTag((PVOID)&nrl, nrl_length, XENNET_POOL_TAG);
339 if (status != NDIS_STATUS_SUCCESS)
340 {
341 KdPrint((__DRIVER_NAME " Could not get allocate memory for Adapter Resources 0x%x\n", status));
342 return NDIS_STATUS_RESOURCES;
343 }
344 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
345 nrl, (PUINT)&nrl_length);
346 if (status != NDIS_STATUS_SUCCESS)
347 {
348 KdPrint((__DRIVER_NAME " Could not get Adapter Resources 0x%x\n", status));
349 return NDIS_STATUS_RESOURCES;
350 }
351 xi->event_channel = 0;
352 xi->config_csum = 1;
353 xi->config_sg = 1;
354 xi->config_gso = 61440;
355 xi->config_page = NULL;
357 for (i = 0; i < nrl->Count; i++)
358 {
359 prd = &nrl->PartialDescriptors[i];
361 switch(prd->Type)
362 {
363 case CmResourceTypeInterrupt:
364 irq_vector = prd->u.Interrupt.Vector;
365 irq_level = (KIRQL)prd->u.Interrupt.Level;
366 KdPrint((__DRIVER_NAME " irq_vector = %03x, irq_level = %03x\n", irq_vector, irq_level));
367 break;
369 case CmResourceTypeMemory:
370 status = NdisMMapIoSpace(&xi->config_page, MiniportAdapterHandle, prd->u.Memory.Start, prd->u.Memory.Length);
371 if (!NT_SUCCESS(status))
372 {
373 KdPrint(("NdisMMapIoSpace failed with 0x%x\n", status));
374 NdisFreeMemory(nrl, nrl_length, 0);
375 return NDIS_STATUS_RESOURCES;
376 }
377 break;
378 }
379 }
380 NdisFreeMemory(nrl, nrl_length, 0);
381 if (!xi->config_page)
382 {
383 KdPrint(("No config page given\n"));
384 return NDIS_STATUS_RESOURCES;
385 }
387 KeInitializeSpinLock(&xi->rx_lock);
389 InitializeListHead(&xi->tx_waiting_pkt_list);
391 NdisAllocatePacketPool(&status, &xi->packet_pool, XN_RX_QUEUE_LEN * 8,
392 PROTOCOL_RESERVED_SIZE_IN_PACKET);
393 if (status != NDIS_STATUS_SUCCESS)
394 {
395 KdPrint(("NdisAllocatePacketPool failed with 0x%x\n", status));
396 status = NDIS_STATUS_RESOURCES;
397 goto err;
398 }
399 NdisSetPacketPoolProtocolId(xi->packet_pool, NDIS_PROTOCOL_ID_TCP_IP);
401 NdisAllocateBufferPool(&status, &xi->buffer_pool, XN_RX_QUEUE_LEN);
402 if (status != NDIS_STATUS_SUCCESS)
403 {
404 KdPrint(("NdisAllocateBufferPool failed with 0x%x\n", status));
405 status = NDIS_STATUS_RESOURCES;
406 goto err;
407 }
409 NdisMGetDeviceProperty(MiniportAdapterHandle, &xi->pdo, &xi->fdo,
410 &xi->lower_do, NULL, NULL);
411 xi->packet_filter = NDIS_PACKET_TYPE_PROMISCUOUS;
413 status = IoGetDeviceProperty(xi->pdo, DevicePropertyDeviceDescription,
414 NAME_SIZE, xi->dev_desc, &length);
415 if (!NT_SUCCESS(status))
416 {
417 KdPrint(("IoGetDeviceProperty failed with 0x%x\n", status));
418 status = NDIS_STATUS_FAILURE;
419 goto err;
420 }
422 ptr = xi->config_page;
423 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value)) != XEN_INIT_TYPE_END)
424 {
425 switch(type)
426 {
427 case XEN_INIT_TYPE_VECTORS:
428 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
429 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
430 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
431 {
432 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
433 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
434 FUNCTION_ERROR_EXIT();
435 return NDIS_STATUS_FAILURE;
436 }
437 else
438 memcpy(&xi->vectors, value, sizeof(XENPCI_VECTORS));
439 break;
440 case XEN_INIT_TYPE_STATE_PTR:
441 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
442 xi->device_state = (PXENPCI_DEVICE_STATE)value;
443 break;
444 default:
445 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
446 break;
447 }
448 }
450 // now build config page
452 NdisOpenConfiguration(&status, &config_handle, WrapperConfigurationContext);
453 if (!NT_SUCCESS(status))
454 {
455 KdPrint(("Could not open config in registry (%08x)\n", status));
456 goto err;
457 }
459 NdisInitUnicodeString(&config_param_name, L"ScatterGather");
460 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
461 if (!NT_SUCCESS(status))
462 {
463 KdPrint(("Could not read ScatterGather value (%08x)\n", status));
464 xi->config_sg = 1;
465 }
466 else
467 {
468 KdPrint(("ScatterGather = %d\n", config_param->ParameterData.IntegerData));
469 xi->config_sg = config_param->ParameterData.IntegerData;
470 }
472 NdisInitUnicodeString(&config_param_name, L"LargeSendOffload");
473 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
474 if (!NT_SUCCESS(status))
475 {
476 KdPrint(("Could not read LargeSendOffload value (%08x)\n", status));
477 xi->config_gso = 0;
478 }
479 else
480 {
481 KdPrint(("LargeSendOffload = %d\n", config_param->ParameterData.IntegerData));
482 xi->config_gso = config_param->ParameterData.IntegerData;
483 if (xi->config_gso > 61440)
484 {
485 xi->config_gso = 61440;
486 KdPrint(("(clipped to %d)\n", xi->config_gso));
487 }
488 }
490 NdisInitUnicodeString(&config_param_name, L"ChecksumOffload");
491 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
492 if (!NT_SUCCESS(status))
493 {
494 KdPrint(("Could not read ChecksumOffload value (%08x)\n", status));
495 xi->config_csum = 1;
496 }
497 else
498 {
499 KdPrint(("ChecksumOffload = %d\n", config_param->ParameterData.IntegerData));
500 xi->config_csum = !!config_param->ParameterData.IntegerData;
501 }
503 NdisInitUnicodeString(&config_param_name, L"MTU");
504 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
505 if (!NT_SUCCESS(status))
506 {
507 KdPrint(("Could not read MTU value (%08x)\n", status));
508 xi->config_mtu = 1500;
509 }
510 else
511 {
512 KdPrint(("MTU = %d\n", config_param->ParameterData.IntegerData));
513 xi->config_mtu = config_param->ParameterData.IntegerData;
514 }
516 xi->config_max_pkt_size = max(xi->config_mtu + XN_HDR_SIZE, xi->config_gso + XN_HDR_SIZE);
518 NdisCloseConfiguration(config_handle);
520 ptr = xi->config_page;
521 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL);
522 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "tx-ring-ref", NULL);
523 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "rx-ring-ref", NULL);
524 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL);
525 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mac", NULL);
526 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !xi->config_csum);
527 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-no-csum-offload", buf);
528 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", (int)xi->config_sg);
529 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-sg", buf);
530 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !!xi->config_gso);
531 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-gso-tcpv4", buf);
532 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "request-rx-copy", "1");
533 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-rx-notify", "1");
534 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL);
536 status = xi->vectors.XenPci_XenConfigDevice(xi->vectors.context);
537 // check return value
539 status = XenNet_ConnectBackend(xi);
541 KeInitializeEvent(&xi->shutdown_event, SynchronizationEvent, FALSE);
543 XenNet_TxInit(xi);
544 XenNet_RxInit(xi);
546 xi->connected = TRUE;
548 KeMemoryBarrier(); // packets could be received anytime after we set Frontent to Connected
550 status = NdisMRegisterInterrupt(&xi->interrupt, MiniportAdapterHandle, irq_vector, irq_level,
551 TRUE, TRUE, NdisInterruptLatched);
552 /* send fake arp? */
553 if (!NT_SUCCESS(status))
554 {
555 KdPrint(("NdisMRegisterInterrupt failed with 0x%x\n", status));
556 //status = NDIS_STATUS_FAILURE;
557 //goto err;
558 }
560 NdisMInitializeTimer(&xi->resume_timer, xi->adapter_handle, XenResumeCheck_Timer, xi);
561 NdisMSetPeriodicTimer(&xi->resume_timer, 100);
563 FUNCTION_EXIT();
565 return NDIS_STATUS_SUCCESS;
567 err:
568 NdisFreeMemory(xi, 0, 0);
569 FUNCTION_ERROR_EXIT();
570 return status;
571 }
573 VOID
574 XenNet_PnPEventNotify(
575 IN NDIS_HANDLE MiniportAdapterContext,
576 IN NDIS_DEVICE_PNP_EVENT PnPEvent,
577 IN PVOID InformationBuffer,
578 IN ULONG InformationBufferLength
579 )
580 {
581 UNREFERENCED_PARAMETER(MiniportAdapterContext);
582 UNREFERENCED_PARAMETER(PnPEvent);
583 UNREFERENCED_PARAMETER(InformationBuffer);
584 UNREFERENCED_PARAMETER(InformationBufferLength);
586 FUNCTION_CALLED();
587 }
589 /* Called when machine is shutting down, so just quiesce the HW and be done fast. */
590 VOID DDKAPI
591 XenNet_Shutdown(
592 IN NDIS_HANDLE MiniportAdapterContext
593 )
594 {
595 struct xennet_info *xi = MiniportAdapterContext;
597 FUNCTION_CALLED();
599 NdisMDeregisterInterrupt(&xi->interrupt);
600 }
602 /* Opposite of XenNet_Init */
603 VOID DDKAPI
604 XenNet_Halt(
605 IN NDIS_HANDLE MiniportAdapterContext
606 )
607 {
608 struct xennet_info *xi = MiniportAdapterContext;
609 // CHAR TmpPath[MAX_XENBUS_STR_LEN];
610 // PVOID if_cxt = xi->XenInterface.InterfaceHeader.Context;
611 // LARGE_INTEGER timeout;
613 FUNCTION_ENTER();
614 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
616 // Disables the interrupt
617 XenNet_Shutdown(xi);
619 xi->vectors.XenPci_XenShutdownDevice(xi->vectors.context);
621 xi->connected = FALSE;
622 KeMemoryBarrier(); /* make sure everyone sees that we are now shut down */
624 // TODO: remove event channel xenbus entry (how?)
626 XenNet_TxShutdown(xi);
627 XenNet_RxShutdown(xi);
629 NdisFreeBufferPool(xi->buffer_pool);
630 NdisFreePacketPool(xi->packet_pool);
632 NdisFreeMemory(xi, 0, 0); // <= DISPATCH_LEVEL
634 FUNCTION_EXIT();
635 }
637 NTSTATUS DDKAPI
638 DriverEntry(
639 PDRIVER_OBJECT DriverObject,
640 PUNICODE_STRING RegistryPath
641 )
642 {
643 NTSTATUS status;
644 NDIS_HANDLE ndis_wrapper_handle;
645 NDIS_MINIPORT_CHARACTERISTICS mini_chars;
647 FUNCTION_ENTER();
649 RtlZeroMemory(&mini_chars, sizeof(mini_chars));
651 NdisMInitializeWrapper(&ndis_wrapper_handle, DriverObject, RegistryPath, NULL);
652 if (!ndis_wrapper_handle)
653 {
654 KdPrint(("NdisMInitializeWrapper failed\n"));
655 return NDIS_STATUS_FAILURE;
656 }
658 /* NDIS 5.1 driver */
659 mini_chars.MajorNdisVersion = NDIS_MINIPORT_MAJOR_VERSION;
660 mini_chars.MinorNdisVersion = NDIS_MINIPORT_MINOR_VERSION;
662 mini_chars.HaltHandler = XenNet_Halt;
663 mini_chars.InitializeHandler = XenNet_Init;
664 mini_chars.ISRHandler = XenNet_InterruptIsr;
665 mini_chars.HandleInterruptHandler = XenNet_InterruptDpc;
666 mini_chars.QueryInformationHandler = XenNet_QueryInformation;
667 mini_chars.ResetHandler = NULL; //TODO: fill in
668 mini_chars.SetInformationHandler = XenNet_SetInformation;
669 /* added in v.4 -- use multiple pkts interface */
670 mini_chars.ReturnPacketHandler = XenNet_ReturnPacket;
671 mini_chars.SendPacketsHandler = XenNet_SendPackets;
673 #if defined (NDIS51_MINIPORT)
674 /* added in v.5.1 */
675 mini_chars.PnPEventNotifyHandler = XenNet_PnPEventNotify;
676 mini_chars.AdapterShutdownHandler = XenNet_Shutdown;
677 #else
678 // something else here
679 #endif
681 /* set up upper-edge interface */
682 status = NdisMRegisterMiniport(ndis_wrapper_handle, &mini_chars, sizeof(mini_chars));
683 if (!NT_SUCCESS(status))
684 {
685 KdPrint(("NdisMRegisterMiniport failed, status = 0x%x\n", status));
686 NdisTerminateWrapper(ndis_wrapper_handle, NULL);
687 return status;
688 }
690 FUNCTION_EXIT();
692 return status;
693 }