win-pvdrivers

view xennet/xennet5.c @ 935:99023e044544

ndis5 driver now passes ndistest with only a call to DbgPrint marked as an error
author James Harper <james.harper@bendigoit.com.au>
date Mon Jun 20 23:16:14 2011 +1000 (2011-06-20)
parents f64a6d173e8d
children 8f483a2b2991
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 "xennet5.h"
25 /* Not really necessary but keeps PREfast happy */
26 DRIVER_INITIALIZE DriverEntry;
27 static IO_WORKITEM_ROUTINE XenNet_ResumeWorkItem;
28 #if (VER_PRODUCTBUILD >= 7600)
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 VOID
324 XenNet_RxTxDpc(PKDPC dpc, PVOID context, PVOID arg1, PVOID arg2)
325 {
326 struct xennet_info *xi = context;
327 BOOLEAN dont_set_event;
329 UNREFERENCED_PARAMETER(dpc);
330 UNREFERENCED_PARAMETER(arg1);
331 UNREFERENCED_PARAMETER(arg2);
333 /* if Rx goes over its per-dpc quota then make sure TxBufferGC doesn't set an event as we are already guaranteed to be called again */
334 dont_set_event = XenNet_RxBufferCheck(xi);
335 XenNet_TxBufferGC(xi, dont_set_event);
336 }
338 static BOOLEAN
339 XenNet_HandleEvent(PVOID context)
340 {
341 struct xennet_info *xi = context;
342 ULONG suspend_resume_state_pdo;
344 //FUNCTION_ENTER();
345 suspend_resume_state_pdo = xi->device_state->suspend_resume_state_pdo;
346 KeMemoryBarrier();
347 // KdPrint((__DRIVER_NAME " connected = %d, inactive = %d, suspend_resume_state_pdo = %d\n",
348 // xi->connected, xi->inactive, suspend_resume_state_pdo));
349 if (!xi->shutting_down && suspend_resume_state_pdo != xi->device_state->suspend_resume_state_fdo)
350 {
351 KeInsertQueueDpc(&xi->suspend_dpc, NULL, NULL);
352 }
353 if (xi->connected && !xi->inactive && suspend_resume_state_pdo != SR_STATE_RESUMING)
354 {
355 KeInsertQueueDpc(&xi->rxtx_dpc, NULL, NULL);
356 }
357 //FUNCTION_EXIT();
358 return TRUE;
359 }
361 VOID
362 XenNet_SetPower(PDEVICE_OBJECT device_object, PVOID context)
363 {
364 NTSTATUS status = STATUS_SUCCESS;
365 KIRQL old_irql;
366 struct xennet_info *xi = context;
368 FUNCTION_ENTER();
369 UNREFERENCED_PARAMETER(device_object);
371 switch (xi->new_power_state)
372 {
373 case NdisDeviceStateD0:
374 KdPrint((" NdisDeviceStateD0\n"));
375 status = XenNet_D0Entry(xi);
376 break;
377 case NdisDeviceStateD1:
378 KdPrint((" NdisDeviceStateD1\n"));
379 if (xi->power_state == NdisDeviceStateD0)
380 status = XenNet_D0Exit(xi);
381 break;
382 case NdisDeviceStateD2:
383 KdPrint((" NdisDeviceStateD2\n"));
384 if (xi->power_state == NdisDeviceStateD0)
385 status = XenNet_D0Exit(xi);
386 break;
387 case NdisDeviceStateD3:
388 KdPrint((" NdisDeviceStateD3\n"));
389 if (xi->power_state == NdisDeviceStateD0)
390 status = XenNet_D0Exit(xi);
391 break;
392 default:
393 KdPrint((" NdisDeviceState??\n"));
394 status = NDIS_STATUS_NOT_SUPPORTED;
395 break;
396 }
397 xi->power_state = xi->new_power_state;
399 old_irql = KeRaiseIrqlToDpcLevel();
400 NdisMSetInformationComplete(xi->adapter_handle, status);
401 KeLowerIrql(old_irql);
403 FUNCTION_EXIT();
404 }
406 NDIS_STATUS
407 XenNet_D0Entry(struct xennet_info *xi)
408 {
409 NDIS_STATUS status;
410 PUCHAR ptr;
411 CHAR buf[128];
413 FUNCTION_ENTER();
415 xi->shutting_down = FALSE;
417 ptr = xi->config_page;
418 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "tx-ring-ref", NULL, NULL);
419 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "rx-ring-ref", NULL, NULL);
420 #pragma warning(suppress:4054)
421 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL, "event-channel", (PVOID)XenNet_HandleEvent, xi);
422 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mac", NULL, NULL);
423 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "feature-sg", NULL, NULL);
424 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "feature-gso-tcpv4", NULL, NULL);
425 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "request-rx-copy", "1", NULL);
426 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-rx-notify", "1", NULL);
427 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !xi->config_csum);
428 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-no-csum-offload", buf, NULL);
429 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", (int)xi->config_sg);
430 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-sg", buf, NULL);
431 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !!xi->config_gso);
432 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-gso-tcpv4", buf, NULL);
433 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_PRE_CONNECT, NULL, NULL, NULL);
434 __ADD_XEN_INIT_UCHAR(&ptr, 0); /* no pre-connect required */
435 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_POST_CONNECT, NULL, NULL, NULL);
436 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
437 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateConnected);
438 __ADD_XEN_INIT_UCHAR(&ptr, 20);
439 __ADD_XEN_INIT_UCHAR(&ptr, 0);
440 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_XB_STATE_MAP_SHUTDOWN, NULL, NULL, NULL);
441 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
442 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosing);
443 __ADD_XEN_INIT_UCHAR(&ptr, 50);
444 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
445 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateClosed);
446 __ADD_XEN_INIT_UCHAR(&ptr, 50);
447 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitialising);
448 __ADD_XEN_INIT_UCHAR(&ptr, XenbusStateInitWait);
449 __ADD_XEN_INIT_UCHAR(&ptr, 50);
450 __ADD_XEN_INIT_UCHAR(&ptr, 0);
451 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
453 status = xi->vectors.XenPci_XenConfigDevice(xi->vectors.context);
454 if (!NT_SUCCESS(status))
455 {
456 KdPrint(("Failed to complete device configuration (%08x)\n", status));
457 return status;
458 }
460 status = XenNet_ConnectBackend(xi);
462 if (!NT_SUCCESS(status))
463 {
464 KdPrint(("Failed to complete device configuration (%08x)\n", status));
465 return status;
466 }
468 if (!xi->config_sg)
469 {
470 /* without SG, GSO can be a maximum of PAGE_SIZE */
471 xi->config_gso = min(xi->config_gso, PAGE_SIZE);
472 }
474 XenNet_TxInit(xi);
475 XenNet_RxInit(xi);
477 xi->connected = TRUE;
479 KeMemoryBarrier(); // packets could be received anytime after we set Frontent to Connected
481 FUNCTION_EXIT();
483 return status;
484 }
486 // Called at <= DISPATCH_LEVEL
487 static NDIS_STATUS
488 XenNet_Init(
489 OUT PNDIS_STATUS OpenErrorStatus,
490 OUT PUINT SelectedMediumIndex,
491 IN PNDIS_MEDIUM MediumArray,
492 IN UINT MediumArraySize,
493 IN NDIS_HANDLE MiniportAdapterHandle,
494 IN NDIS_HANDLE WrapperConfigurationContext
495 )
496 {
497 NDIS_STATUS status;
498 BOOLEAN medium_found = FALSE;
499 struct xennet_info *xi = NULL;
500 UINT nrl_length;
501 PNDIS_RESOURCE_LIST nrl;
502 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
503 KIRQL irq_level = 0;
504 ULONG irq_vector = 0;
505 ULONG irq_mode = 0;
506 NDIS_HANDLE config_handle;
507 NDIS_STRING config_param_name;
508 PNDIS_CONFIGURATION_PARAMETER config_param;
509 ULONG i;
510 PUCHAR ptr;
511 UCHAR type;
512 PCHAR setting, value;
513 ULONG length;
514 //CHAR buf[128];
515 PVOID network_address;
516 UINT network_address_length;
517 BOOLEAN qemu_hide_filter = FALSE;
518 ULONG qemu_hide_flags_value = 0;
520 UNREFERENCED_PARAMETER(OpenErrorStatus);
522 FUNCTION_ENTER();
524 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
526 /* deal with medium stuff */
527 for (i = 0; i < MediumArraySize; i++)
528 {
529 if (MediumArray[i] == NdisMedium802_3)
530 {
531 medium_found = TRUE;
532 break;
533 }
534 }
535 if (!medium_found)
536 {
537 KdPrint(("NIC_MEDIA_TYPE not in MediumArray\n"));
538 return NDIS_STATUS_UNSUPPORTED_MEDIA;
539 }
540 *SelectedMediumIndex = i;
542 /* Alloc memory for adapter private info */
543 status = NdisAllocateMemoryWithTag((PVOID)&xi, sizeof(*xi), XENNET_POOL_TAG);
544 if (!NT_SUCCESS(status))
545 {
546 KdPrint(("NdisAllocateMemoryWithTag failed with 0x%x\n", status));
547 status = NDIS_STATUS_RESOURCES;
548 goto err;
549 }
550 RtlZeroMemory(xi, sizeof(*xi));
551 xi->adapter_handle = MiniportAdapterHandle;
552 xi->rx_target = RX_DFL_MIN_TARGET;
553 xi->rx_min_target = RX_DFL_MIN_TARGET;
554 xi->rx_max_target = RX_MAX_TARGET;
555 xi->inactive = TRUE;
556 NdisMSetAttributesEx(xi->adapter_handle, (NDIS_HANDLE) xi, 0, 0 /* the last zero is to give the next | something to | with */
557 #ifdef NDIS51_MINIPORT
558 |NDIS_ATTRIBUTE_USES_SAFE_BUFFER_APIS
559 #endif
560 |NDIS_ATTRIBUTE_DESERIALIZE
561 |NDIS_ATTRIBUTE_SURPRISE_REMOVE_OK,
562 NdisInterfaceInternal); /* PnpBus option doesn't exist... */
563 xi->multicast_list_size = 0;
564 xi->current_lookahead = MIN_LOOKAHEAD_LENGTH;
566 nrl_length = 0;
567 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
568 NULL, (PUINT)&nrl_length);
569 KdPrint((__DRIVER_NAME " nrl_length = %d\n", nrl_length));
570 status = NdisAllocateMemoryWithTag((PVOID)&nrl, nrl_length, XENNET_POOL_TAG);
571 if (status != NDIS_STATUS_SUCCESS)
572 {
573 KdPrint((__DRIVER_NAME " Could not get allocate memory for Adapter Resources 0x%x\n", status));
574 return NDIS_STATUS_RESOURCES;
575 }
576 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
577 nrl, (PUINT)&nrl_length);
578 if (status != NDIS_STATUS_SUCCESS)
579 {
580 KdPrint((__DRIVER_NAME " Could not get Adapter Resources 0x%x\n", status));
581 return NDIS_STATUS_RESOURCES;
582 }
583 xi->event_channel = 0;
584 xi->config_csum = 1;
585 xi->config_csum_rx_check = 1;
586 xi->config_sg = 1;
587 xi->config_gso = 61440;
588 xi->config_page = NULL;
589 xi->config_rx_interrupt_moderation = 0;
591 for (i = 0; i < nrl->Count; i++)
592 {
593 prd = &nrl->PartialDescriptors[i];
595 switch(prd->Type)
596 {
597 case CmResourceTypeInterrupt:
598 irq_vector = prd->u.Interrupt.Vector;
599 irq_level = (KIRQL)prd->u.Interrupt.Level;
600 irq_mode = (prd->Flags & CM_RESOURCE_INTERRUPT_LATCHED)?NdisInterruptLatched:NdisInterruptLevelSensitive;
601 KdPrint((__DRIVER_NAME " irq_vector = %03x, irq_level = %03x, irq_mode = %s\n", irq_vector, irq_level,
602 (irq_mode == NdisInterruptLatched)?"NdisInterruptLatched":"NdisInterruptLevelSensitive"));
603 break;
604 case CmResourceTypeMemory:
605 if (xi->config_page)
606 {
607 KdPrint(("More than one memory range\n"));
608 return NDIS_STATUS_RESOURCES;
609 }
610 else
611 {
612 status = NdisMMapIoSpace(&xi->config_page, MiniportAdapterHandle, prd->u.Memory.Start, prd->u.Memory.Length);
613 if (!NT_SUCCESS(status))
614 {
615 KdPrint(("NdisMMapIoSpace failed with 0x%x\n", status));
616 NdisFreeMemory(nrl, nrl_length, 0);
617 return NDIS_STATUS_RESOURCES;
618 }
619 }
620 break;
621 }
622 }
623 NdisFreeMemory(nrl, nrl_length, 0);
624 if (!xi->config_page)
625 {
626 KdPrint(("No config page given\n"));
627 return NDIS_STATUS_RESOURCES;
628 }
630 KeInitializeDpc(&xi->suspend_dpc, XenNet_SuspendResume, xi);
631 KeInitializeSpinLock(&xi->resume_lock);
633 KeInitializeDpc(&xi->rxtx_dpc, XenNet_RxTxDpc, xi);
634 KeSetTargetProcessorDpc(&xi->rxtx_dpc, 0);
635 KeSetImportanceDpc(&xi->rxtx_dpc, HighImportance);
637 NdisMGetDeviceProperty(MiniportAdapterHandle, &xi->pdo, &xi->fdo,
638 &xi->lower_do, NULL, NULL);
639 xi->packet_filter = 0;
641 status = IoGetDeviceProperty(xi->pdo, DevicePropertyDeviceDescription,
642 NAME_SIZE, xi->dev_desc, &length);
643 if (!NT_SUCCESS(status))
644 {
645 KdPrint(("IoGetDeviceProperty failed with 0x%x\n", status));
646 status = NDIS_STATUS_FAILURE;
647 goto err;
648 }
650 ptr = xi->config_page;
651 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value)) != XEN_INIT_TYPE_END)
652 {
653 switch(type)
654 {
655 case XEN_INIT_TYPE_VECTORS:
656 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
657 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
658 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
659 {
660 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
661 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
662 FUNCTION_EXIT();
663 return NDIS_STATUS_FAILURE;
664 }
665 else
666 memcpy(&xi->vectors, value, sizeof(XENPCI_VECTORS));
667 break;
668 case XEN_INIT_TYPE_STATE_PTR:
669 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
670 xi->device_state = (PXENPCI_DEVICE_STATE)value;
671 break;
672 case XEN_INIT_TYPE_QEMU_HIDE_FLAGS:
673 qemu_hide_flags_value = PtrToUlong(value);
674 break;
675 case XEN_INIT_TYPE_QEMU_HIDE_FILTER:
676 qemu_hide_filter = TRUE;
677 break;
678 default:
679 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
680 break;
681 }
682 }
684 if ((qemu_hide_flags_value & QEMU_UNPLUG_ALL_IDE_DISKS) || qemu_hide_filter)
685 xi->inactive = FALSE;
687 xi->power_state = NdisDeviceStateD0;
688 xi->power_workitem = IoAllocateWorkItem(xi->fdo);
690 // now build config page
692 NdisOpenConfiguration(&status, &config_handle, WrapperConfigurationContext);
693 if (!NT_SUCCESS(status))
694 {
695 KdPrint(("Could not open config in registry (%08x)\n", status));
696 status = NDIS_STATUS_RESOURCES;
697 goto err;
698 }
700 NdisInitUnicodeString(&config_param_name, L"ScatterGather");
701 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
702 if (!NT_SUCCESS(status))
703 {
704 KdPrint(("Could not read ScatterGather value (%08x)\n", status));
705 xi->config_sg = 1;
706 }
707 else
708 {
709 KdPrint(("ScatterGather = %d\n", config_param->ParameterData.IntegerData));
710 xi->config_sg = config_param->ParameterData.IntegerData;
711 }
713 NdisInitUnicodeString(&config_param_name, L"LargeSendOffload");
714 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
715 if (!NT_SUCCESS(status))
716 {
717 KdPrint(("Could not read LargeSendOffload value (%08x)\n", status));
718 xi->config_gso = 0;
719 }
720 else
721 {
722 KdPrint(("LargeSendOffload = %d\n", config_param->ParameterData.IntegerData));
723 xi->config_gso = config_param->ParameterData.IntegerData;
724 if (xi->config_gso > 61440)
725 {
726 xi->config_gso = 61440;
727 KdPrint(("(clipped to %d)\n", xi->config_gso));
728 }
729 }
731 NdisInitUnicodeString(&config_param_name, L"ChecksumOffload");
732 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
733 if (!NT_SUCCESS(status))
734 {
735 KdPrint(("Could not read ChecksumOffload value (%08x)\n", status));
736 xi->config_csum = 1;
737 }
738 else
739 {
740 KdPrint(("ChecksumOffload = %d\n", config_param->ParameterData.IntegerData));
741 xi->config_csum = !!config_param->ParameterData.IntegerData;
742 }
744 NdisInitUnicodeString(&config_param_name, L"ChecksumOffloadRxCheck");
745 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
746 if (!NT_SUCCESS(status))
747 {
748 KdPrint(("Could not read ChecksumOffloadRxCheck value (%08x)\n", status));
749 xi->config_csum_rx_check = 1;
750 }
751 else
752 {
753 KdPrint(("ChecksumOffloadRxCheck = %d\n", config_param->ParameterData.IntegerData));
754 xi->config_csum_rx_check = !!config_param->ParameterData.IntegerData;
755 }
757 NdisInitUnicodeString(&config_param_name, L"ChecksumOffloadDontFix");
758 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
759 if (!NT_SUCCESS(status))
760 {
761 KdPrint(("Could not read ChecksumOffloadDontFix value (%08x)\n", status));
762 xi->config_csum_rx_dont_fix = 0;
763 }
764 else
765 {
766 KdPrint(("ChecksumOffloadDontFix = %d\n", config_param->ParameterData.IntegerData));
767 xi->config_csum_rx_dont_fix = !!config_param->ParameterData.IntegerData;
768 }
772 NdisInitUnicodeString(&config_param_name, L"MTU");
773 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
774 if (!NT_SUCCESS(status))
775 {
776 KdPrint(("Could not read MTU value (%08x)\n", status));
777 xi->config_mtu = 1500;
778 }
779 else
780 {
781 KdPrint(("MTU = %d\n", config_param->ParameterData.IntegerData));
782 xi->config_mtu = config_param->ParameterData.IntegerData;
783 }
785 NdisInitUnicodeString(&config_param_name, L"RxInterruptModeration");
786 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
787 if (!NT_SUCCESS(status))
788 {
789 KdPrint(("Could not read RxInterruptModeration value (%08x)\n", status));
790 xi->config_rx_interrupt_moderation = 1500;
791 }
792 else
793 {
794 KdPrint(("RxInterruptModeration = %d\n", config_param->ParameterData.IntegerData));
795 xi->config_rx_interrupt_moderation = config_param->ParameterData.IntegerData;
796 }
799 NdisReadNetworkAddress(&status, &network_address, &network_address_length, config_handle);
800 if (!NT_SUCCESS(status) || network_address_length != ETH_ALEN || ((((PUCHAR)network_address)[0] & 0x03) != 0x02))
801 {
802 KdPrint(("Could not read NetworkAddress value (%08x) or value is invalid\n", status));
803 memset(xi->curr_mac_addr, 0, ETH_ALEN);
804 }
805 else
806 {
807 memcpy(xi->curr_mac_addr, network_address, ETH_ALEN);
808 KdPrint((" Set MAC address from registry to %02X:%02X:%02X:%02X:%02X:%02X\n",
809 xi->curr_mac_addr[0], xi->curr_mac_addr[1], xi->curr_mac_addr[2],
810 xi->curr_mac_addr[3], xi->curr_mac_addr[4], xi->curr_mac_addr[5]));
811 }
813 xi->config_max_pkt_size = max(xi->config_mtu + XN_HDR_SIZE, xi->config_gso + XN_HDR_SIZE);
815 NdisCloseConfiguration(config_handle);
817 status = XenNet_D0Entry(xi);
818 if (!NT_SUCCESS(status))
819 {
820 KdPrint(("Failed to go to D0 (%08x)\n", status));
821 goto err;
822 }
823 return NDIS_STATUS_SUCCESS;
825 err:
826 NdisFreeMemory(xi, 0, 0);
827 *OpenErrorStatus = status;
828 FUNCTION_EXIT_STATUS(status);
829 return status;
830 }
832 VOID
833 XenNet_PnPEventNotify(
834 IN NDIS_HANDLE MiniportAdapterContext,
835 IN NDIS_DEVICE_PNP_EVENT PnPEvent,
836 IN PVOID InformationBuffer,
837 IN ULONG InformationBufferLength
838 )
839 {
840 UNREFERENCED_PARAMETER(MiniportAdapterContext);
841 UNREFERENCED_PARAMETER(PnPEvent);
842 UNREFERENCED_PARAMETER(InformationBuffer);
843 UNREFERENCED_PARAMETER(InformationBufferLength);
845 FUNCTION_ENTER();
846 switch (PnPEvent)
847 {
848 case NdisDevicePnPEventSurpriseRemoved:
849 KdPrint((__DRIVER_NAME " NdisDevicePnPEventSurpriseRemoved\n"));
850 break;
851 case NdisDevicePnPEventPowerProfileChanged :
852 KdPrint((__DRIVER_NAME " NdisDevicePnPEventPowerProfileChanged\n"));
853 break;
854 default:
855 KdPrint((__DRIVER_NAME " %d\n", PnPEvent));
856 break;
857 }
858 FUNCTION_EXIT();
859 }
861 /* Called when machine is shutting down, so just quiesce the HW and be done fast. */
862 VOID
863 XenNet_Shutdown(
864 IN NDIS_HANDLE MiniportAdapterContext
865 )
866 {
867 UNREFERENCED_PARAMETER(MiniportAdapterContext);
869 /* remember we are called at >= DIRQL here */
870 FUNCTION_ENTER();
871 FUNCTION_EXIT();
872 }
874 /* Opposite of XenNet_Init */
875 VOID
876 XenNet_Halt(
877 IN NDIS_HANDLE MiniportAdapterContext
878 )
879 {
880 struct xennet_info *xi = MiniportAdapterContext;
882 FUNCTION_ENTER();
883 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
885 XenNet_D0Exit(xi);
887 NdisFreeMemory(xi, 0, 0);
889 FUNCTION_EXIT();
890 }
892 NDIS_STATUS
893 XenNet_D0Exit(struct xennet_info *xi)
894 {
895 FUNCTION_ENTER();
896 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
898 xi->shutting_down = TRUE;
899 KeMemoryBarrier(); /* make sure everyone sees that we are now shutting down */
901 XenNet_TxShutdown(xi);
902 XenNet_RxShutdown(xi);
904 xi->connected = FALSE;
905 KeMemoryBarrier(); /* make sure everyone sees that we are now disconnected */
907 xi->vectors.XenPci_XenShutdownDevice(xi->vectors.context);
909 FUNCTION_EXIT();
911 return STATUS_SUCCESS;
912 }
914 NDIS_STATUS
915 XenNet_Reset(
916 PBOOLEAN AddressingReset,
917 NDIS_HANDLE MiniportAdapterContext
918 )
919 {
920 UNREFERENCED_PARAMETER(MiniportAdapterContext);
922 *AddressingReset = FALSE;
923 return NDIS_STATUS_SUCCESS;
924 }
926 NTSTATUS
927 DriverEntry(
928 PDRIVER_OBJECT DriverObject,
929 PUNICODE_STRING RegistryPath
930 )
931 {
932 NTSTATUS status;
933 NDIS_HANDLE ndis_wrapper_handle = NULL;
934 NDIS_MINIPORT_CHARACTERISTICS mini_chars;
936 FUNCTION_ENTER();
938 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
940 NdisZeroMemory(&mini_chars, sizeof(mini_chars));
942 KdPrint((__DRIVER_NAME " NdisGetVersion = %x\n", NdisGetVersion()));
944 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
945 NdisMInitializeWrapper(&ndis_wrapper_handle, DriverObject, RegistryPath, NULL);
946 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
947 if (!ndis_wrapper_handle)
948 {
949 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper failed\n"));
950 return NDIS_STATUS_FAILURE;
951 }
952 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper succeeded\n"));
954 /* NDIS 5.1 driver */
955 mini_chars.MajorNdisVersion = NDIS_MINIPORT_MAJOR_VERSION;
956 mini_chars.MinorNdisVersion = NDIS_MINIPORT_MINOR_VERSION;
958 KdPrint((__DRIVER_NAME " MajorNdisVersion = %d, MinorNdisVersion = %d\n", NDIS_MINIPORT_MAJOR_VERSION, NDIS_MINIPORT_MINOR_VERSION));
960 mini_chars.HaltHandler = XenNet_Halt;
961 mini_chars.InitializeHandler = XenNet_Init;
962 //mini_chars.ISRHandler = XenNet_InterruptIsr;
963 //mini_chars.HandleInterruptHandler = XenNet_InterruptDpc;
964 mini_chars.QueryInformationHandler = XenNet_QueryInformation;
965 mini_chars.ResetHandler = XenNet_Reset;
966 mini_chars.SetInformationHandler = XenNet_SetInformation;
967 /* added in v.4 -- use multiple pkts interface */
968 mini_chars.ReturnPacketHandler = XenNet_ReturnPacket;
969 mini_chars.SendPacketsHandler = XenNet_SendPackets;
970 /* don't support cancel - no point as packets are never queued for long */
971 //mini_chars.CancelSendPacketsHandler = XenNet_CancelSendPackets;
973 #ifdef NDIS51_MINIPORT
974 /* added in v.5.1 */
975 mini_chars.PnPEventNotifyHandler = XenNet_PnPEventNotify;
976 mini_chars.AdapterShutdownHandler = XenNet_Shutdown;
977 #else
978 // something else here
979 #endif
981 /* set up upper-edge interface */
982 KdPrint((__DRIVER_NAME " about to call NdisMRegisterMiniport\n"));
983 status = NdisMRegisterMiniport(ndis_wrapper_handle, &mini_chars, sizeof(mini_chars));
984 KdPrint((__DRIVER_NAME " called NdisMRegisterMiniport\n"));
985 if (!NT_SUCCESS(status))
986 {
987 KdPrint((__DRIVER_NAME " NdisMRegisterMiniport failed, status = 0x%x\n", status));
988 NdisTerminateWrapper(ndis_wrapper_handle, NULL);
989 return status;
990 }
992 FUNCTION_EXIT();
994 return status;