win-pvdrivers

view xennet/xennet.c @ 900:938de6b8623d

fix memory leak on suspend/resume
author James Harper <james.harper@bendigoit.com.au>
date Thu Mar 31 20:20:36 2011 +1100 (2011-03-31)
parents 730447ed7524
children 2e4f05dd4dfe
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 /* Not really necessary but keeps PREfast happy */
26 DRIVER_INITIALIZE DriverEntry;
27 static IO_WORKITEM_ROUTINE XenNet_ResumeWorkItem;
28 #if (NTDDI_VERSION >= NTDDI_WINXP)
29 static KDEFERRED_ROUTINE XenNet_SuspendResume;
30 #endif
32 #pragma NDIS_INIT_FUNCTION(DriverEntry)
34 /* ----- BEGIN Other people's code --------- */
35 /* from linux/include/linux/ctype.h, used under GPLv2 */
36 #define _U 0x01 /* upper */
37 #define _L 0x02 /* lower */
38 #define _D 0x04 /* digit */
39 #define _C 0x08 /* cntrl */
40 #define _P 0x10 /* punct */
41 #define _S 0x20 /* white space (space/lf/tab) */
42 #define _X 0x40 /* hex digit */
43 #define _SP 0x80 /* hard space (0x20) */
45 /* from linux/include/lib/ctype.c, used under GPLv2 */
46 unsigned char _ctype[] = {
47 _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
48 _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
49 _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
50 _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
51 _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
52 _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
53 _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
54 _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
55 _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
56 _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
57 _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
58 _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
59 _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
60 _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
61 _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
62 _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
63 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
64 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
65 _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
66 _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
67 _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
68 _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
69 _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
70 _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
72 /* from linux/include/linux/ctype.h, used under GPLv2 */
73 #define __ismask(x) (_ctype[(int)(unsigned char)(x)])
75 #define isalnum(c) ((__ismask(c)&(_U|_L|_D)) != 0)
76 #define isalpha(c) ((__ismask(c)&(_U|_L)) != 0)
77 #define iscntrl(c) ((__ismask(c)&(_C)) != 0)
78 #define isdigit(c) ((__ismask(c)&(_D)) != 0)
79 #define isgraph(c) ((__ismask(c)&(_P|_U|_L|_D)) != 0)
80 #define islower(c) ((__ismask(c)&(_L)) != 0)
81 #define isprint(c) ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
82 #define ispunct(c) ((__ismask(c)&(_P)) != 0)
83 #define isspace(c) ((__ismask(c)&(_S)) != 0)
84 #define isupper(c) ((__ismask(c)&(_U)) != 0)
85 #define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
87 #define TOLOWER(x) ((x) | 0x20)
89 /* from linux/lib/vsprintf.c, used under GPLv2 */
90 /* Copyright (C) 1991, 1992 Linus Torvalds
91 * Wirzenius wrote this portably, Torvalds fucked it up :-)
92 */
93 /**
94 * simple_strtoul - convert a string to an unsigned long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
98 */
99 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
100 {
101 unsigned long result = 0,value;
103 if (!base) {
104 base = 10;
105 if (*cp == '0') {
106 base = 8;
107 cp++;
108 if ((TOLOWER(*cp) == 'x') && isxdigit(cp[1])) {
109 cp++;
110 base = 16;
111 }
112 }
113 } else if (base == 16) {
114 if (cp[0] == '0' && TOLOWER(cp[1]) == 'x')
115 cp += 2;
116 }
117 while (isxdigit(*cp) &&
118 (value = isdigit(*cp) ? *cp-'0' : TOLOWER(*cp)-'a'+10) < base) {
119 result = result*base + value;
120 cp++;
121 }
122 if (endp)
123 *endp = (char *)cp;
124 return result;
125 }
126 /* end vsprintf.c code */
127 /* ----- END Other people's code --------- */
129 static NDIS_STATUS
130 XenNet_ConnectBackend(struct xennet_info *xi)
131 {
132 PUCHAR ptr;
133 UCHAR type;
134 PCHAR setting, value, value2;
135 UINT i;
136 ULONG backend_sg = 0;
137 ULONG backend_gso = 0;
139 FUNCTION_ENTER();
141 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
143 ptr = xi->config_page;
144 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
145 {
146 switch(type)
147 {
148 case XEN_INIT_TYPE_RING: /* frontend ring */
149 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, value));
150 if (strcmp(setting, "tx-ring-ref") == 0)
151 {
152 FRONT_RING_INIT(&xi->tx, (netif_tx_sring_t *)value, PAGE_SIZE);
153 } else if (strcmp(setting, "rx-ring-ref") == 0)
154 {
155 FRONT_RING_INIT(&xi->rx, (netif_rx_sring_t *)value, PAGE_SIZE);
156 }
157 break;
158 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
159 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, PtrToUlong(value)));
160 if (strcmp(setting, "event-channel") == 0)
161 {
162 xi->event_channel = PtrToUlong(value);
163 }
164 break;
165 case XEN_INIT_TYPE_READ_STRING_FRONT:
166 break;
167 case XEN_INIT_TYPE_READ_STRING_BACK:
168 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
169 if (strcmp(setting, "mac") == 0)
170 {
171 char *s, *e;
172 s = value;
173 for (i = 0; i < ETH_ALEN; i++) {
174 xi->perm_mac_addr[i] = (uint8_t)simple_strtoul(s, &e, 16);
175 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
176 KdPrint((__DRIVER_NAME "Error parsing MAC address\n"));
177 }
178 s = e + 1;
179 }
180 if ((xi->curr_mac_addr[0] & 0x03) != 0x02)
181 {
182 /* only copy if curr_mac_addr is not a LUA */
183 memcpy(xi->curr_mac_addr, xi->perm_mac_addr, ETH_ALEN);
184 }
185 }
186 else if (strcmp(setting, "feature-sg") == 0)
187 {
188 if (atoi(value))
189 {
190 backend_sg = 1;
191 }
192 }
193 else if (strcmp(setting, "feature-gso-tcpv4") == 0)
194 {
195 if (atoi(value))
196 {
197 backend_gso = 1;
198 }
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_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 if (xi->config_sg && !backend_sg)
224 {
225 KdPrint((__DRIVER_NAME " SG not supported by backend - disabling\n"));
226 xi->config_sg = 0;
227 }
228 if (xi->config_gso && !backend_gso)
229 {
230 KdPrint((__DRIVER_NAME " GSO not supported by backend - disabling\n"));
231 xi->config_gso = 0;
232 }
233 FUNCTION_EXIT();
235 return NDIS_STATUS_SUCCESS;
236 }
238 static VOID
239 XenNet_ResumeWorkItem(PDEVICE_OBJECT device_object, PVOID context)
240 {
241 struct xennet_info *xi = context;
242 KIRQL old_irql;
244 UNREFERENCED_PARAMETER(device_object);
246 FUNCTION_ENTER();
248 ASSERT(xi->resume_work_item);
250 IoFreeWorkItem(xi->resume_work_item);
252 XenNet_TxResumeStart(xi);
253 XenNet_RxResumeStart(xi);
254 XenNet_ConnectBackend(xi);
255 XenNet_RxResumeEnd(xi);
256 XenNet_TxResumeEnd(xi);
258 KeAcquireSpinLock(&xi->resume_lock, &old_irql);
259 xi->resume_work_item = NULL;
260 KdPrint((__DRIVER_NAME " *Setting suspend_resume_state_fdo = %d\n", xi->device_state->suspend_resume_state_pdo));
261 xi->device_state->suspend_resume_state_fdo = xi->device_state->suspend_resume_state_pdo;
262 KdPrint((__DRIVER_NAME " *Notifying event channel %d\n", xi->device_state->pdo_event_channel));
263 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
264 KeReleaseSpinLock(&xi->resume_lock, old_irql);
266 FUNCTION_EXIT();
268 }
270 static VOID
271 XenNet_SuspendResume(PKDPC dpc, PVOID context, PVOID arg1, PVOID arg2)
272 {
273 struct xennet_info *xi = context;
274 KIRQL old_irql;
275 PIO_WORKITEM resume_work_item;
277 UNREFERENCED_PARAMETER(dpc);
278 UNREFERENCED_PARAMETER(arg1);
279 UNREFERENCED_PARAMETER(arg2);
281 FUNCTION_ENTER();
283 switch (xi->device_state->suspend_resume_state_pdo)
284 {
285 case SR_STATE_SUSPENDING:
286 KdPrint((__DRIVER_NAME " New state SUSPENDING\n"));
287 KeAcquireSpinLock(&xi->rx_lock, &old_irql);
288 if (xi->rx_id_free == NET_RX_RING_SIZE)
289 {
290 xi->device_state->suspend_resume_state_fdo = SR_STATE_SUSPENDING;
291 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xi->device_state->pdo_event_channel));
292 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
293 }
294 KeReleaseSpinLock(&xi->rx_lock, old_irql);
295 break;
296 case SR_STATE_RESUMING:
297 KdPrint((__DRIVER_NAME " New state SR_STATE_RESUMING\n"));
298 /* do it like this so we don't race and double-free the work item */
299 resume_work_item = IoAllocateWorkItem(xi->fdo);
300 KeAcquireSpinLock(&xi->resume_lock, &old_irql);
301 if (xi->resume_work_item || xi->device_state->suspend_resume_state_fdo == SR_STATE_RESUMING)
302 {
303 KeReleaseSpinLock(&xi->resume_lock, old_irql);
304 IoFreeWorkItem(resume_work_item);
305 return;
306 }
307 xi->resume_work_item = resume_work_item;
308 KeReleaseSpinLock(&xi->resume_lock, old_irql);
309 IoQueueWorkItem(xi->resume_work_item, XenNet_ResumeWorkItem, DelayedWorkQueue, xi);
310 break;
311 default:
312 KdPrint((__DRIVER_NAME " New state %d\n", xi->device_state->suspend_resume_state_fdo));
313 xi->device_state->suspend_resume_state_fdo = xi->device_state->suspend_resume_state_pdo;
314 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xi->device_state->pdo_event_channel));
315 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
316 break;
317 }
318 KeMemoryBarrier();
320 FUNCTION_EXIT();
321 }
323 static DDKAPI BOOLEAN
324 XenNet_HandleEvent(PVOID context)
325 {
326 struct xennet_info *xi = context;
327 ULONG suspend_resume_state_pdo;
329 //FUNCTION_ENTER();
330 suspend_resume_state_pdo = xi->device_state->suspend_resume_state_pdo;
331 KeMemoryBarrier();
332 // KdPrint((__DRIVER_NAME " connected = %d, inactive = %d, suspend_resume_state_pdo = %d\n",
333 // xi->connected, xi->inactive, suspend_resume_state_pdo));
334 if (!xi->shutting_down && suspend_resume_state_pdo != xi->device_state->suspend_resume_state_fdo)
335 {
336 KeInsertQueueDpc(&xi->suspend_dpc, NULL, NULL);
337 }
338 if (xi->connected && !xi->inactive && suspend_resume_state_pdo != SR_STATE_RESUMING)
339 {
340 KeInsertQueueDpc(&xi->tx_dpc, NULL, NULL);
341 KeInsertQueueDpc(&xi->rx_dpc, NULL, NULL);
342 }
343 //FUNCTION_EXIT();
344 return TRUE;
345 }
347 VOID
348 XenNet_SetPower(PDEVICE_OBJECT device_object, PVOID context)
349 {
350 NTSTATUS status = STATUS_SUCCESS;
351 KIRQL old_irql;
352 struct xennet_info *xi = context;
354 FUNCTION_ENTER();
355 UNREFERENCED_PARAMETER(device_object);
357 switch (xi->new_power_state)
358 {
359 case NdisDeviceStateD0:
360 KdPrint((" NdisDeviceStateD0\n"));
361 status = XenNet_D0Entry(xi);
362 break;
363 case NdisDeviceStateD1:
364 KdPrint((" NdisDeviceStateD1\n"));
365 if (xi->power_state == NdisDeviceStateD0)
366 status = XenNet_D0Exit(xi);
367 break;
368 case NdisDeviceStateD2:
369 KdPrint((" NdisDeviceStateD2\n"));
370 if (xi->power_state == NdisDeviceStateD0)
371 status = XenNet_D0Exit(xi);
372 break;
373 case NdisDeviceStateD3:
374 KdPrint((" NdisDeviceStateD3\n"));
375 if (xi->power_state == NdisDeviceStateD0)
376 status = XenNet_D0Exit(xi);
377 break;
378 default:
379 KdPrint((" NdisDeviceState??\n"));
380 status = NDIS_STATUS_NOT_SUPPORTED;
381 break;
382 }
383 xi->power_state = xi->new_power_state;
385 old_irql = KeRaiseIrqlToDpcLevel();
386 NdisMSetInformationComplete(xi->adapter_handle, status);
387 KeLowerIrql(old_irql);
389 FUNCTION_EXIT();
390 }
392 NDIS_STATUS
393 XenNet_D0Entry(struct xennet_info *xi)
394 {
395 NDIS_STATUS status;
396 PUCHAR ptr;
397 CHAR buf[128];
399 FUNCTION_ENTER();
401 xi->shutting_down = FALSE;
403 ptr = xi->config_page;
404 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "tx-ring-ref", NULL, NULL);
405 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "rx-ring-ref", NULL, NULL);
406 #pragma warning(suppress:4054)
407 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL, "event-channel", (PVOID)XenNet_HandleEvent, xi);
408 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mac", NULL, NULL);
409 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "feature-sg", NULL, NULL);
410 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "feature-gso-tcpv4", NULL, NULL);
411 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "request-rx-copy", "1", NULL);
412 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-rx-notify", "1", NULL);
413 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !xi->config_csum);
414 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-no-csum-offload", buf, NULL);
415 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", (int)xi->config_sg);
416 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-sg", buf, NULL);
417 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !!xi->config_gso);
418 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-gso-tcpv4", buf, NULL);
419 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
420 __ADD_XEN_INIT_UCHAR(&ptr, 0); /* no pre-connect required */
421 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
422 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
423 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
424 __ADD_XEN_INIT_UCHAR(&ptr, 20);
425 __ADD_XEN_INIT_UCHAR(&ptr, 0);
426 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
427 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
428 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
429 __ADD_XEN_INIT_UCHAR(&ptr, 50);
430 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
431 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
432 __ADD_XEN_INIT_UCHAR(&ptr, 50);
433 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
434 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
435 __ADD_XEN_INIT_UCHAR(&ptr, 50);
436 __ADD_XEN_INIT_UCHAR(&ptr, 0);
437 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
439 status = xi->vectors.XenPci_XenConfigDevice(xi->vectors.context);
440 if (!NT_SUCCESS(status))
441 {
442 KdPrint(("Failed to complete device configuration (%08x)\n", status));
443 return status;
444 }
446 status = XenNet_ConnectBackend(xi);
448 if (!NT_SUCCESS(status))
449 {
450 KdPrint(("Failed to complete device configuration (%08x)\n", status));
451 return status;
452 }
454 if (!xi->config_sg)
455 {
456 /* without SG, GSO can be a maximum of PAGE_SIZE */
457 xi->config_gso = min(xi->config_gso, PAGE_SIZE);
458 }
460 XenNet_TxInit(xi);
461 XenNet_RxInit(xi);
463 xi->connected = TRUE;
465 KeMemoryBarrier(); // packets could be received anytime after we set Frontent to Connected
467 FUNCTION_EXIT();
469 return status;
470 }
472 // Called at <= DISPATCH_LEVEL
473 static NDIS_STATUS
474 XenNet_Init(
475 OUT PNDIS_STATUS OpenErrorStatus,
476 OUT PUINT SelectedMediumIndex,
477 IN PNDIS_MEDIUM MediumArray,
478 IN UINT MediumArraySize,
479 IN NDIS_HANDLE MiniportAdapterHandle,
480 IN NDIS_HANDLE WrapperConfigurationContext
481 )
482 {
483 NDIS_STATUS status;
484 BOOLEAN medium_found = FALSE;
485 struct xennet_info *xi = NULL;
486 UINT nrl_length;
487 PNDIS_RESOURCE_LIST nrl;
488 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
489 KIRQL irq_level = 0;
490 ULONG irq_vector = 0;
491 ULONG irq_mode = 0;
492 NDIS_HANDLE config_handle;
493 NDIS_STRING config_param_name;
494 PNDIS_CONFIGURATION_PARAMETER config_param;
495 ULONG i;
496 PUCHAR ptr;
497 UCHAR type;
498 PCHAR setting, value;
499 ULONG length;
500 //CHAR buf[128];
501 PVOID network_address;
502 UINT network_address_length;
503 BOOLEAN qemu_hide_filter = FALSE;
504 ULONG qemu_hide_flags_value = 0;
506 UNREFERENCED_PARAMETER(OpenErrorStatus);
508 FUNCTION_ENTER();
510 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
512 /* deal with medium stuff */
513 for (i = 0; i < MediumArraySize; i++)
514 {
515 if (MediumArray[i] == NdisMedium802_3)
516 {
517 medium_found = TRUE;
518 break;
519 }
520 }
521 if (!medium_found)
522 {
523 KdPrint(("NIC_MEDIA_TYPE not in MediumArray\n"));
524 return NDIS_STATUS_UNSUPPORTED_MEDIA;
525 }
526 *SelectedMediumIndex = i;
528 /* Alloc memory for adapter private info */
529 status = NdisAllocateMemoryWithTag((PVOID)&xi, sizeof(*xi), XENNET_POOL_TAG);
530 if (!NT_SUCCESS(status))
531 {
532 KdPrint(("NdisAllocateMemoryWithTag failed with 0x%x\n", status));
533 status = NDIS_STATUS_RESOURCES;
534 goto err;
535 }
536 RtlZeroMemory(xi, sizeof(*xi));
537 xi->adapter_handle = MiniportAdapterHandle;
538 xi->rx_target = RX_DFL_MIN_TARGET;
539 xi->rx_min_target = RX_DFL_MIN_TARGET;
540 xi->rx_max_target = RX_MAX_TARGET;
541 xi->inactive = TRUE;
542 NdisMSetAttributesEx(xi->adapter_handle, (NDIS_HANDLE) xi, 0, 0 /* the last zero is to give the next | something to | with */
543 #ifdef NDIS51_MINIPORT
544 |NDIS_ATTRIBUTE_USES_SAFE_BUFFER_APIS
545 #endif
546 |NDIS_ATTRIBUTE_DESERIALIZE
547 |NDIS_ATTRIBUTE_SURPRISE_REMOVE_OK
548 |NDIS_ATTRIBUTE_BUS_MASTER,
549 NdisInterfaceInternal);
550 xi->multicast_list_size = 0;
551 xi->current_lookahead = MIN_LOOKAHEAD_LENGTH;
553 nrl_length = 0;
554 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
555 NULL, (PUINT)&nrl_length);
556 KdPrint((__DRIVER_NAME " nrl_length = %d\n", nrl_length));
557 status = NdisAllocateMemoryWithTag((PVOID)&nrl, nrl_length, XENNET_POOL_TAG);
558 if (status != NDIS_STATUS_SUCCESS)
559 {
560 KdPrint((__DRIVER_NAME " Could not get allocate memory for Adapter Resources 0x%x\n", status));
561 return NDIS_STATUS_RESOURCES;
562 }
563 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
564 nrl, (PUINT)&nrl_length);
565 if (status != NDIS_STATUS_SUCCESS)
566 {
567 KdPrint((__DRIVER_NAME " Could not get Adapter Resources 0x%x\n", status));
568 return NDIS_STATUS_RESOURCES;
569 }
570 xi->event_channel = 0;
571 xi->config_csum = 1;
572 xi->config_csum_rx_check = 1;
573 xi->config_sg = 1;
574 xi->config_gso = 61440;
575 xi->config_page = NULL;
576 xi->config_rx_interrupt_moderation = 0;
578 for (i = 0; i < nrl->Count; i++)
579 {
580 prd = &nrl->PartialDescriptors[i];
582 switch(prd->Type)
583 {
584 case CmResourceTypeInterrupt:
585 irq_vector = prd->u.Interrupt.Vector;
586 irq_level = (KIRQL)prd->u.Interrupt.Level;
587 irq_mode = (prd->Flags & CM_RESOURCE_INTERRUPT_LATCHED)?NdisInterruptLatched:NdisInterruptLevelSensitive;
588 KdPrint((__DRIVER_NAME " irq_vector = %03x, irq_level = %03x, irq_mode = %s\n", irq_vector, irq_level,
589 (irq_mode == NdisInterruptLatched)?"NdisInterruptLatched":"NdisInterruptLevelSensitive"));
590 break;
591 case CmResourceTypeMemory:
592 if (xi->config_page)
593 {
594 KdPrint(("More than one memory range\n"));
595 return NDIS_STATUS_RESOURCES;
596 }
597 else
598 {
599 status = NdisMMapIoSpace(&xi->config_page, MiniportAdapterHandle, prd->u.Memory.Start, prd->u.Memory.Length);
600 if (!NT_SUCCESS(status))
601 {
602 KdPrint(("NdisMMapIoSpace failed with 0x%x\n", status));
603 NdisFreeMemory(nrl, nrl_length, 0);
604 return NDIS_STATUS_RESOURCES;
605 }
606 }
607 break;
608 }
609 }
610 NdisFreeMemory(nrl, nrl_length, 0);
611 if (!xi->config_page)
612 {
613 KdPrint(("No config page given\n"));
614 return NDIS_STATUS_RESOURCES;
615 }
617 KeInitializeDpc(&xi->suspend_dpc, XenNet_SuspendResume, xi);
618 KeInitializeSpinLock(&xi->resume_lock);
620 NdisMGetDeviceProperty(MiniportAdapterHandle, &xi->pdo, &xi->fdo,
621 &xi->lower_do, NULL, NULL);
622 xi->packet_filter = 0;
624 status = IoGetDeviceProperty(xi->pdo, DevicePropertyDeviceDescription,
625 NAME_SIZE, xi->dev_desc, &length);
626 if (!NT_SUCCESS(status))
627 {
628 KdPrint(("IoGetDeviceProperty failed with 0x%x\n", status));
629 status = NDIS_STATUS_FAILURE;
630 goto err;
631 }
633 ptr = xi->config_page;
634 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value)) != XEN_INIT_TYPE_END)
635 {
636 switch(type)
637 {
638 case XEN_INIT_TYPE_VECTORS:
639 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
640 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
641 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
642 {
643 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
644 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
645 FUNCTION_EXIT();
646 return NDIS_STATUS_FAILURE;
647 }
648 else
649 memcpy(&xi->vectors, value, sizeof(XENPCI_VECTORS));
650 break;
651 case XEN_INIT_TYPE_STATE_PTR:
652 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
653 xi->device_state = (PXENPCI_DEVICE_STATE)value;
654 break;
655 case XEN_INIT_TYPE_QEMU_HIDE_FLAGS:
656 qemu_hide_flags_value = PtrToUlong(value);
657 break;
658 case XEN_INIT_TYPE_QEMU_HIDE_FILTER:
659 qemu_hide_filter = TRUE;
660 break;
661 default:
662 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
663 break;
664 }
665 }
667 if ((qemu_hide_flags_value & QEMU_UNPLUG_ALL_IDE_DISKS) || qemu_hide_filter)
668 xi->inactive = FALSE;
670 xi->power_state = NdisDeviceStateD0;
671 xi->power_workitem = IoAllocateWorkItem(xi->fdo);
673 // now build config page
675 NdisOpenConfiguration(&status, &config_handle, WrapperConfigurationContext);
676 if (!NT_SUCCESS(status))
677 {
678 KdPrint(("Could not open config in registry (%08x)\n", status));
679 status = NDIS_STATUS_RESOURCES;
680 goto err;
681 }
683 NdisInitUnicodeString(&config_param_name, L"ScatterGather");
684 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
685 if (!NT_SUCCESS(status))
686 {
687 KdPrint(("Could not read ScatterGather value (%08x)\n", status));
688 xi->config_sg = 1;
689 }
690 else
691 {
692 KdPrint(("ScatterGather = %d\n", config_param->ParameterData.IntegerData));
693 xi->config_sg = config_param->ParameterData.IntegerData;
694 }
696 NdisInitUnicodeString(&config_param_name, L"LargeSendOffload");
697 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
698 if (!NT_SUCCESS(status))
699 {
700 KdPrint(("Could not read LargeSendOffload value (%08x)\n", status));
701 xi->config_gso = 0;
702 }
703 else
704 {
705 KdPrint(("LargeSendOffload = %d\n", config_param->ParameterData.IntegerData));
706 xi->config_gso = config_param->ParameterData.IntegerData;
707 if (xi->config_gso > 61440)
708 {
709 xi->config_gso = 61440;
710 KdPrint(("(clipped to %d)\n", xi->config_gso));
711 }
712 }
714 NdisInitUnicodeString(&config_param_name, L"ChecksumOffload");
715 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
716 if (!NT_SUCCESS(status))
717 {
718 KdPrint(("Could not read ChecksumOffload value (%08x)\n", status));
719 xi->config_csum = 1;
720 }
721 else
722 {
723 KdPrint(("ChecksumOffload = %d\n", config_param->ParameterData.IntegerData));
724 xi->config_csum = !!config_param->ParameterData.IntegerData;
725 }
727 NdisInitUnicodeString(&config_param_name, L"ChecksumOffloadRxCheck");
728 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
729 if (!NT_SUCCESS(status))
730 {
731 KdPrint(("Could not read ChecksumOffloadRxCheck value (%08x)\n", status));
732 xi->config_csum_rx_check = 1;
733 }
734 else
735 {
736 KdPrint(("ChecksumOffloadRxCheck = %d\n", config_param->ParameterData.IntegerData));
737 xi->config_csum_rx_check = !!config_param->ParameterData.IntegerData;
738 }
740 NdisInitUnicodeString(&config_param_name, L"MTU");
741 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
742 if (!NT_SUCCESS(status))
743 {
744 KdPrint(("Could not read MTU value (%08x)\n", status));
745 xi->config_mtu = 1500;
746 }
747 else
748 {
749 KdPrint(("MTU = %d\n", config_param->ParameterData.IntegerData));
750 xi->config_mtu = config_param->ParameterData.IntegerData;
751 }
753 NdisInitUnicodeString(&config_param_name, L"RxInterruptModeration");
754 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
755 if (!NT_SUCCESS(status))
756 {
757 KdPrint(("Could not read RxInterruptModeration value (%08x)\n", status));
758 xi->config_rx_interrupt_moderation = 1500;
759 }
760 else
761 {
762 KdPrint(("RxInterruptModeration = %d\n", config_param->ParameterData.IntegerData));
763 xi->config_rx_interrupt_moderation = config_param->ParameterData.IntegerData;
764 }
767 NdisReadNetworkAddress(&status, &network_address, &network_address_length, config_handle);
768 if (!NT_SUCCESS(status) || network_address_length != ETH_ALEN || ((((PUCHAR)network_address)[0] & 0x03) != 0x02))
769 {
770 KdPrint(("Could not read NetworkAddress value (%08x) or value is invalid\n", status));
771 memset(xi->curr_mac_addr, 0, ETH_ALEN);
772 }
773 else
774 {
775 memcpy(xi->curr_mac_addr, network_address, ETH_ALEN);
776 KdPrint((" Set MAC address from registry to %02X:%02X:%02X:%02X:%02X:%02X\n",
777 xi->curr_mac_addr[0], xi->curr_mac_addr[1], xi->curr_mac_addr[2],
778 xi->curr_mac_addr[3], xi->curr_mac_addr[4], xi->curr_mac_addr[5]));
779 }
781 xi->config_max_pkt_size = max(xi->config_mtu + XN_HDR_SIZE, xi->config_gso + XN_HDR_SIZE);
783 NdisCloseConfiguration(config_handle);
785 status = XenNet_D0Entry(xi);
786 if (!NT_SUCCESS(status))
787 {
788 KdPrint(("Failed to go to D0 (%08x)\n", status));
789 goto err;
790 }
791 return NDIS_STATUS_SUCCESS;
793 err:
794 NdisFreeMemory(xi, 0, 0);
795 *OpenErrorStatus = status;
796 FUNCTION_EXIT_STATUS(status);
797 return status;
798 }
800 VOID
801 XenNet_PnPEventNotify(
802 IN NDIS_HANDLE MiniportAdapterContext,
803 IN NDIS_DEVICE_PNP_EVENT PnPEvent,
804 IN PVOID InformationBuffer,
805 IN ULONG InformationBufferLength
806 )
807 {
808 UNREFERENCED_PARAMETER(MiniportAdapterContext);
809 UNREFERENCED_PARAMETER(PnPEvent);
810 UNREFERENCED_PARAMETER(InformationBuffer);
811 UNREFERENCED_PARAMETER(InformationBufferLength);
813 FUNCTION_ENTER();
814 switch (PnPEvent)
815 {
816 case NdisDevicePnPEventSurpriseRemoved:
817 KdPrint((__DRIVER_NAME " NdisDevicePnPEventSurpriseRemoved\n"));
818 break;
819 case NdisDevicePnPEventPowerProfileChanged :
820 KdPrint((__DRIVER_NAME " NdisDevicePnPEventPowerProfileChanged\n"));
821 break;
822 default:
823 KdPrint((__DRIVER_NAME " %d\n", PnPEvent));
824 break;
825 }
826 FUNCTION_EXIT();
827 }
829 /* Called when machine is shutting down, so just quiesce the HW and be done fast. */
830 VOID DDKAPI
831 XenNet_Shutdown(
832 IN NDIS_HANDLE MiniportAdapterContext
833 )
834 {
835 UNREFERENCED_PARAMETER(MiniportAdapterContext);
837 /* remember we are called at >= DIRQL here */
838 FUNCTION_ENTER();
839 FUNCTION_EXIT();
840 }
842 /* Opposite of XenNet_Init */
843 VOID DDKAPI
844 XenNet_Halt(
845 IN NDIS_HANDLE MiniportAdapterContext
846 )
847 {
848 struct xennet_info *xi = MiniportAdapterContext;
850 FUNCTION_ENTER();
851 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
853 XenNet_D0Exit(xi);
855 NdisFreeMemory(xi, 0, 0);
857 FUNCTION_EXIT();
858 }
860 NDIS_STATUS
861 XenNet_D0Exit(struct xennet_info *xi)
862 {
863 FUNCTION_ENTER();
864 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
866 xi->shutting_down = TRUE;
867 KeMemoryBarrier(); /* make sure everyone sees that we are now shutting down */
869 XenNet_TxShutdown(xi);
870 XenNet_RxShutdown(xi);
872 xi->connected = FALSE;
873 KeMemoryBarrier(); /* make sure everyone sees that we are now disconnected */
875 xi->vectors.XenPci_XenShutdownDevice(xi->vectors.context);
877 FUNCTION_EXIT();
879 return STATUS_SUCCESS;
880 }
882 NDIS_STATUS
883 XenNet_Reset(
884 PBOOLEAN AddressingReset,
885 NDIS_HANDLE MiniportAdapterContext
886 )
887 {
888 UNREFERENCED_PARAMETER(MiniportAdapterContext);
890 *AddressingReset = FALSE;
891 return NDIS_STATUS_SUCCESS;
892 }
894 NTSTATUS
895 DriverEntry(
896 PDRIVER_OBJECT DriverObject,
897 PUNICODE_STRING RegistryPath
898 )
899 {
900 NTSTATUS status;
901 NDIS_HANDLE ndis_wrapper_handle = NULL;
902 NDIS_MINIPORT_CHARACTERISTICS mini_chars;
904 FUNCTION_ENTER();
906 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
908 NdisZeroMemory(&mini_chars, sizeof(mini_chars));
910 KdPrint((__DRIVER_NAME " NdisGetVersion = %x\n", NdisGetVersion()));
912 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
913 NdisMInitializeWrapper(&ndis_wrapper_handle, DriverObject, RegistryPath, NULL);
914 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
915 if (!ndis_wrapper_handle)
916 {
917 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper failed\n"));
918 return NDIS_STATUS_FAILURE;
919 }
920 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper succeeded\n"));
922 /* NDIS 5.1 driver */
923 mini_chars.MajorNdisVersion = NDIS_MINIPORT_MAJOR_VERSION;
924 mini_chars.MinorNdisVersion = NDIS_MINIPORT_MINOR_VERSION;
926 KdPrint((__DRIVER_NAME " MajorNdisVersion = %d, MinorNdisVersion = %d\n", NDIS_MINIPORT_MAJOR_VERSION, NDIS_MINIPORT_MINOR_VERSION));
928 mini_chars.HaltHandler = XenNet_Halt;
929 mini_chars.InitializeHandler = XenNet_Init;
930 //mini_chars.ISRHandler = XenNet_InterruptIsr;
931 //mini_chars.HandleInterruptHandler = XenNet_InterruptDpc;
932 mini_chars.QueryInformationHandler = XenNet_QueryInformation;
933 mini_chars.ResetHandler = XenNet_Reset;
934 mini_chars.SetInformationHandler = XenNet_SetInformation;
935 /* added in v.4 -- use multiple pkts interface */
936 mini_chars.ReturnPacketHandler = XenNet_ReturnPacket;
937 mini_chars.SendPacketsHandler = XenNet_SendPackets;
938 /* don't support cancel - no point as packets are never queued for long */
939 //mini_chars.CancelSendPacketsHandler = XenNet_CancelSendPackets;
941 #ifdef NDIS51_MINIPORT
942 /* added in v.5.1 */
943 mini_chars.PnPEventNotifyHandler = XenNet_PnPEventNotify;
944 mini_chars.AdapterShutdownHandler = XenNet_Shutdown;
945 #else
946 // something else here
947 #endif
949 /* set up upper-edge interface */
950 KdPrint((__DRIVER_NAME " about to call NdisMRegisterMiniport\n"));
951 status = NdisMRegisterMiniport(ndis_wrapper_handle, &mini_chars, sizeof(mini_chars));
952 KdPrint((__DRIVER_NAME " called NdisMRegisterMiniport\n"));
953 if (!NT_SUCCESS(status))
954 {
955 KdPrint((__DRIVER_NAME " NdisMRegisterMiniport failed, status = 0x%x\n", status));
956 NdisTerminateWrapper(ndis_wrapper_handle, NULL);
957 return status;
958 }
960 FUNCTION_EXIT();
962 return status;