win-pvdrivers

view xennet/xennet.c @ 586:bdb2d6b163f3

More protection against the Dpcs running after shutdown
author James Harper <james.harper@bendigoit.com.au>
date Sun Jun 07 20:31:20 2009 +1000 (2009-06-07)
parents dc0a293c870c
children 682273b8ce5b
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 static NDIS_STATUS
121 XenNet_ConnectBackend(struct xennet_info *xi)
122 {
123 PUCHAR ptr;
124 UCHAR type;
125 PCHAR setting, value, value2;
126 UINT i;
128 FUNCTION_ENTER();
130 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
132 ptr = xi->config_page;
133 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value2)) != XEN_INIT_TYPE_END)
134 {
135 switch(type)
136 {
137 case XEN_INIT_TYPE_RING: /* frontend ring */
138 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_RING - %s = %p\n", setting, value));
139 if (strcmp(setting, "tx-ring-ref") == 0)
140 {
141 FRONT_RING_INIT(&xi->tx, (netif_tx_sring_t *)value, PAGE_SIZE);
142 } else if (strcmp(setting, "rx-ring-ref") == 0)
143 {
144 FRONT_RING_INIT(&xi->rx, (netif_rx_sring_t *)value, PAGE_SIZE);
145 }
146 break;
147 case XEN_INIT_TYPE_EVENT_CHANNEL: /* frontend event channel */
148 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_EVENT_CHANNEL - %s = %d\n", setting, PtrToUlong(value)));
149 if (strcmp(setting, "event-channel") == 0)
150 {
151 xi->event_channel = PtrToUlong(value);
152 }
153 break;
154 case XEN_INIT_TYPE_READ_STRING_BACK:
155 case XEN_INIT_TYPE_READ_STRING_FRONT:
156 //KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_READ_STRING - %s = %s\n", setting, value));
157 if (strcmp(setting, "mac") == 0)
158 {
159 char *s, *e;
160 s = value;
161 for (i = 0; i < ETH_ALEN; i++) {
162 xi->perm_mac_addr[i] = (uint8_t)simple_strtoul(s, &e, 16);
163 if ((s == e) || (*e != ((i == ETH_ALEN-1) ? '\0' : ':'))) {
164 KdPrint((__DRIVER_NAME "Error parsing MAC address\n"));
165 }
166 s = e + 1;
167 }
168 if (!(xi->curr_mac_addr[0] & 0x02))
169 {
170 /* only copy if curr_mac_addr is not a LUA */
171 memcpy(xi->curr_mac_addr, xi->perm_mac_addr, ETH_ALEN);
172 }
173 }
174 break;
175 case XEN_INIT_TYPE_VECTORS:
176 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
177 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
178 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
179 {
180 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
181 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
182 FUNCTION_ERROR_EXIT();
183 return NDIS_STATUS_ADAPTER_NOT_FOUND;
184 }
185 else
186 memcpy(&xi->vectors, value, sizeof(XENPCI_VECTORS));
187 break;
188 case XEN_INIT_TYPE_STATE_PTR:
189 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
190 xi->device_state = (PXENPCI_DEVICE_STATE)value;
191 break;
192 default:
193 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
194 break;
195 }
196 }
197 FUNCTION_EXIT();
199 return NDIS_STATUS_SUCCESS;
200 }
202 static DDKAPI VOID
203 XenNet_Resume(PDEVICE_OBJECT device_object, PVOID context)
204 {
205 struct xennet_info *xi = context;
207 UNREFERENCED_PARAMETER(device_object);
209 FUNCTION_ENTER();
211 ASSERT(xi->resume_work_item);
212 IoFreeWorkItem(xi->resume_work_item);
213 xi->resume_work_item = NULL;
215 XenNet_TxResumeStart(xi);
216 XenNet_RxResumeStart(xi);
217 XenNet_ConnectBackend(xi);
218 XenNet_RxResumeEnd(xi);
219 XenNet_TxResumeEnd(xi);
220 KdPrint((__DRIVER_NAME " *Setting suspend_resume_state_fdo = %d\n", xi->device_state->suspend_resume_state_pdo));
221 xi->device_state->suspend_resume_state_fdo = xi->device_state->suspend_resume_state_pdo;
222 KdPrint((__DRIVER_NAME " *Notifying event channel %d\n", xi->device_state->pdo_event_channel));
223 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
225 FUNCTION_EXIT();
227 }
229 static VOID
230 XenNet_SuspendResume(PKDPC dpc, PVOID context, PVOID arg1, PVOID arg2)
231 {
232 struct xennet_info *xi = context;
233 KIRQL old_irql;
235 UNREFERENCED_PARAMETER(dpc);
236 UNREFERENCED_PARAMETER(arg1);
237 UNREFERENCED_PARAMETER(arg2);
239 FUNCTION_ENTER();
241 switch (xi->device_state->suspend_resume_state_pdo)
242 {
243 case SR_STATE_SUSPENDING:
244 KdPrint((__DRIVER_NAME " New state SUSPENDING\n"));
245 KeAcquireSpinLock(&xi->rx_lock, &old_irql);
246 if (xi->tx_id_free == NET_TX_RING_SIZE)
247 {
248 xi->device_state->suspend_resume_state_fdo = SR_STATE_SUSPENDING;
249 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xi->device_state->pdo_event_channel));
250 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
251 }
252 KeReleaseSpinLock(&xi->rx_lock, old_irql);
253 break;
254 case SR_STATE_RESUMING:
255 KdPrint((__DRIVER_NAME " New state SR_STATE_RESUMING\n"));
256 ASSERT(!xi->resume_work_item);
257 xi->resume_work_item = IoAllocateWorkItem(xi->fdo);
258 IoQueueWorkItem(xi->resume_work_item, XenNet_Resume, DelayedWorkQueue, xi);
259 break;
260 default:
261 KdPrint((__DRIVER_NAME " New state %d\n", xi->device_state->suspend_resume_state_fdo));
262 xi->device_state->suspend_resume_state_fdo = xi->device_state->suspend_resume_state_pdo;
263 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xi->device_state->pdo_event_channel));
264 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
265 break;
266 }
267 KeMemoryBarrier();
269 FUNCTION_EXIT();
270 }
272 static DDKAPI BOOLEAN
273 XenNet_HandleEvent(PVOID context)
274 {
275 struct xennet_info *xi = context;
276 ULONG suspend_resume_state_pdo;
278 //FUNCTION_ENTER();
279 suspend_resume_state_pdo = xi->device_state->suspend_resume_state_pdo;
280 KeMemoryBarrier();
281 // KdPrint((__DRIVER_NAME " connected = %d, inactive = %d, suspend_resume_state_pdo = %d\n",
282 // xi->connected, xi->inactive, suspend_resume_state_pdo));
283 if (!xi->shutting_down)
284 {
285 if (suspend_resume_state_pdo != xi->device_state->suspend_resume_state_fdo)
286 {
287 KeInsertQueueDpc(&xi->suspend_dpc, NULL, NULL);
288 }
289 else if (xi->connected && !xi->inactive && suspend_resume_state_pdo != SR_STATE_RESUMING)
290 {
291 KeInsertQueueDpc(&xi->tx_dpc, NULL, NULL);
292 KeInsertQueueDpc(&xi->rx_dpc, NULL, NULL);
293 }
294 }
295 //FUNCTION_EXIT();
296 return TRUE;
297 }
299 // Called at <= DISPATCH_LEVEL
300 static DDKAPI NDIS_STATUS
301 XenNet_Init(
302 OUT PNDIS_STATUS OpenErrorStatus,
303 OUT PUINT SelectedMediumIndex,
304 IN PNDIS_MEDIUM MediumArray,
305 IN UINT MediumArraySize,
306 IN NDIS_HANDLE MiniportAdapterHandle,
307 IN NDIS_HANDLE WrapperConfigurationContext
308 )
309 {
310 NDIS_STATUS status;
311 BOOLEAN medium_found = FALSE;
312 struct xennet_info *xi = NULL;
313 UINT nrl_length;
314 PNDIS_RESOURCE_LIST nrl;
315 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
316 KIRQL irq_level = 0;
317 ULONG irq_vector = 0;
318 ULONG irq_mode = 0;
319 NDIS_HANDLE config_handle;
320 NDIS_STRING config_param_name;
321 PNDIS_CONFIGURATION_PARAMETER config_param;
322 ULONG i;
323 PUCHAR ptr;
324 UCHAR type;
325 PCHAR setting, value;
326 ULONG length;
327 CHAR buf[128];
328 PVOID network_address;
329 UINT network_address_length;
331 UNREFERENCED_PARAMETER(OpenErrorStatus);
333 FUNCTION_ENTER();
335 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
337 length = NdisReadPciSlotInformation(MiniportAdapterHandle, 0, 0, &buf, 128);
338 KdPrint((__DRIVER_NAME " NdisReadPciSlotInformation = %d\n", length));
339 /* deal with medium stuff */
340 for (i = 0; i < MediumArraySize; i++)
341 {
342 if (MediumArray[i] == NdisMedium802_3)
343 {
344 medium_found = TRUE;
345 break;
346 }
347 }
348 if (!medium_found)
349 {
350 KdPrint(("NIC_MEDIA_TYPE not in MediumArray\n"));
351 return NDIS_STATUS_UNSUPPORTED_MEDIA;
352 }
353 *SelectedMediumIndex = i;
355 /* Alloc memory for adapter private info */
356 status = NdisAllocateMemoryWithTag((PVOID)&xi, sizeof(*xi), XENNET_POOL_TAG);
357 if (!NT_SUCCESS(status))
358 {
359 KdPrint(("NdisAllocateMemoryWithTag failed with 0x%x\n", status));
360 status = NDIS_STATUS_RESOURCES;
361 goto err;
362 }
363 RtlZeroMemory(xi, sizeof(*xi));
364 xi->adapter_handle = MiniportAdapterHandle;
365 xi->rx_target = RX_DFL_MIN_TARGET;
366 xi->rx_min_target = RX_DFL_MIN_TARGET;
367 xi->rx_max_target = RX_MAX_TARGET;
368 xi->inactive = TRUE;
369 NdisMSetAttributesEx(xi->adapter_handle, (NDIS_HANDLE) xi, 0,
370 #ifdef NDIS51_MINIPORT
371 NDIS_ATTRIBUTE_USES_SAFE_BUFFER_APIS
372 #endif
373 |NDIS_ATTRIBUTE_DESERIALIZE
374 |NDIS_ATTRIBUTE_SURPRISE_REMOVE_OK
375 |NDIS_ATTRIBUTE_BUS_MASTER,
376 NdisInterfaceInternal);
377 xi->multicast_list_size = 0;
379 nrl_length = 0;
380 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
381 NULL, (PUINT)&nrl_length);
382 KdPrint((__DRIVER_NAME " nrl_length = %d\n", nrl_length));
383 status = NdisAllocateMemoryWithTag((PVOID)&nrl, nrl_length, XENNET_POOL_TAG);
384 if (status != NDIS_STATUS_SUCCESS)
385 {
386 KdPrint((__DRIVER_NAME " Could not get allocate memory for Adapter Resources 0x%x\n", status));
387 return NDIS_STATUS_RESOURCES;
388 }
389 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
390 nrl, (PUINT)&nrl_length);
391 if (status != NDIS_STATUS_SUCCESS)
392 {
393 KdPrint((__DRIVER_NAME " Could not get Adapter Resources 0x%x\n", status));
394 return NDIS_STATUS_RESOURCES;
395 }
396 xi->event_channel = 0;
397 xi->config_csum = 1;
398 xi->config_csum_rx_check = 1;
399 xi->config_sg = 1;
400 xi->config_gso = 61440;
401 xi->config_page = NULL;
402 xi->config_rx_interrupt_moderation = 0;
404 for (i = 0; i < nrl->Count; i++)
405 {
406 prd = &nrl->PartialDescriptors[i];
408 switch(prd->Type)
409 {
410 case CmResourceTypeInterrupt:
411 irq_vector = prd->u.Interrupt.Vector;
412 irq_level = (KIRQL)prd->u.Interrupt.Level;
413 irq_mode = (prd->Flags & CM_RESOURCE_INTERRUPT_LATCHED)?NdisInterruptLatched:NdisInterruptLevelSensitive;
414 KdPrint((__DRIVER_NAME " irq_vector = %03x, irq_level = %03x, irq_mode = %s\n", irq_vector, irq_level,
415 (irq_mode == NdisInterruptLatched)?"NdisInterruptLatched":"NdisInterruptLevelSensitive"));
416 break;
417 case CmResourceTypeMemory:
418 if (xi->config_page)
419 {
420 KdPrint(("More than one memory range\n"));
421 return NDIS_STATUS_RESOURCES;
422 }
423 else
424 {
425 status = NdisMMapIoSpace(&xi->config_page, MiniportAdapterHandle, prd->u.Memory.Start, prd->u.Memory.Length);
426 if (!NT_SUCCESS(status))
427 {
428 KdPrint(("NdisMMapIoSpace failed with 0x%x\n", status));
429 NdisFreeMemory(nrl, nrl_length, 0);
430 return NDIS_STATUS_RESOURCES;
431 }
432 }
433 break;
434 }
435 }
436 NdisFreeMemory(nrl, nrl_length, 0);
437 if (!xi->config_page)
438 {
439 KdPrint(("No config page given\n"));
440 return NDIS_STATUS_RESOURCES;
441 }
443 KeInitializeDpc(&xi->suspend_dpc, XenNet_SuspendResume, xi);
445 NdisMGetDeviceProperty(MiniportAdapterHandle, &xi->pdo, &xi->fdo,
446 &xi->lower_do, NULL, NULL);
447 xi->packet_filter = NDIS_PACKET_TYPE_PROMISCUOUS;
449 status = IoGetDeviceProperty(xi->pdo, DevicePropertyDeviceDescription,
450 NAME_SIZE, xi->dev_desc, &length);
451 if (!NT_SUCCESS(status))
452 {
453 KdPrint(("IoGetDeviceProperty failed with 0x%x\n", status));
454 status = NDIS_STATUS_FAILURE;
455 goto err;
456 }
458 ptr = xi->config_page;
459 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value)) != XEN_INIT_TYPE_END)
460 {
461 switch(type)
462 {
463 case XEN_INIT_TYPE_VECTORS:
464 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
465 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
466 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
467 {
468 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
469 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
470 FUNCTION_ERROR_EXIT();
471 return NDIS_STATUS_FAILURE;
472 }
473 else
474 memcpy(&xi->vectors, value, sizeof(XENPCI_VECTORS));
475 break;
476 case XEN_INIT_TYPE_STATE_PTR:
477 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
478 xi->device_state = (PXENPCI_DEVICE_STATE)value;
479 break;
480 case XEN_INIT_TYPE_ACTIVE:
481 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_ACTIVE\n"));
482 xi->inactive = FALSE;
483 break;
484 default:
485 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
486 break;
487 }
488 }
490 // now build config page
492 NdisOpenConfiguration(&status, &config_handle, WrapperConfigurationContext);
493 if (!NT_SUCCESS(status))
494 {
495 KdPrint(("Could not open config in registry (%08x)\n", status));
496 status = NDIS_STATUS_RESOURCES;
497 goto err;
498 }
500 NdisInitUnicodeString(&config_param_name, L"ScatterGather");
501 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
502 if (!NT_SUCCESS(status))
503 {
504 KdPrint(("Could not read ScatterGather value (%08x)\n", status));
505 xi->config_sg = 1;
506 }
507 else
508 {
509 KdPrint(("ScatterGather = %d\n", config_param->ParameterData.IntegerData));
510 xi->config_sg = config_param->ParameterData.IntegerData;
511 }
513 NdisInitUnicodeString(&config_param_name, L"LargeSendOffload");
514 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
515 if (!NT_SUCCESS(status))
516 {
517 KdPrint(("Could not read LargeSendOffload value (%08x)\n", status));
518 xi->config_gso = 0;
519 }
520 else
521 {
522 KdPrint(("LargeSendOffload = %d\n", config_param->ParameterData.IntegerData));
523 xi->config_gso = config_param->ParameterData.IntegerData;
524 if (xi->config_gso > 61440)
525 {
526 xi->config_gso = 61440;
527 KdPrint(("(clipped to %d)\n", xi->config_gso));
528 }
529 }
531 NdisInitUnicodeString(&config_param_name, L"ChecksumOffload");
532 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
533 if (!NT_SUCCESS(status))
534 {
535 KdPrint(("Could not read ChecksumOffload value (%08x)\n", status));
536 xi->config_csum = 1;
537 }
538 else
539 {
540 KdPrint(("ChecksumOffload = %d\n", config_param->ParameterData.IntegerData));
541 xi->config_csum = !!config_param->ParameterData.IntegerData;
542 }
544 NdisInitUnicodeString(&config_param_name, L"ChecksumOffloadRxCheck");
545 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
546 if (!NT_SUCCESS(status))
547 {
548 KdPrint(("Could not read ChecksumOffloadRxCheck value (%08x)\n", status));
549 xi->config_csum_rx_check = 1;
550 }
551 else
552 {
553 KdPrint(("ChecksumOffloadRxCheck = %d\n", config_param->ParameterData.IntegerData));
554 xi->config_csum_rx_check = !!config_param->ParameterData.IntegerData;
555 }
557 NdisInitUnicodeString(&config_param_name, L"MTU");
558 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
559 if (!NT_SUCCESS(status))
560 {
561 KdPrint(("Could not read MTU value (%08x)\n", status));
562 xi->config_mtu = 1500;
563 }
564 else
565 {
566 KdPrint(("MTU = %d\n", config_param->ParameterData.IntegerData));
567 xi->config_mtu = config_param->ParameterData.IntegerData;
568 }
570 NdisInitUnicodeString(&config_param_name, L"RxInterruptModeration");
571 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
572 if (!NT_SUCCESS(status))
573 {
574 KdPrint(("Could not read RxInterruptModeration value (%08x)\n", status));
575 xi->config_rx_interrupt_moderation = 1500;
576 }
577 else
578 {
579 KdPrint(("RxInterruptModeration = %d\n", config_param->ParameterData.IntegerData));
580 xi->config_rx_interrupt_moderation = config_param->ParameterData.IntegerData;
581 }
584 NdisReadNetworkAddress(&status, &network_address, &network_address_length, config_handle);
585 if (!NT_SUCCESS(status) || network_address_length != ETH_ALEN || !(((PUCHAR)network_address)[0] & 0x02))
586 {
587 KdPrint(("Could not read NetworkAddress value (%08x)\n", status));
588 memset(xi->curr_mac_addr, 0, ETH_ALEN);
589 }
590 else
591 {
592 memcpy(xi->curr_mac_addr, network_address, ETH_ALEN);
593 KdPrint((" Set MAC address from registry to %02X:%02X:%02X:%02X:%02X:%02X\n",
594 xi->curr_mac_addr[0], xi->curr_mac_addr[1], xi->curr_mac_addr[2],
595 xi->curr_mac_addr[3], xi->curr_mac_addr[4], xi->curr_mac_addr[5]));
596 }
598 xi->config_max_pkt_size = max(xi->config_mtu + XN_HDR_SIZE, xi->config_gso + XN_HDR_SIZE);
600 NdisCloseConfiguration(config_handle);
602 ptr = xi->config_page;
603 // two XEN_INIT_TYPE_RUNs means go straight to XenbusStateConnected - skip XenbusStateInitialised
604 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
605 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
606 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "tx-ring-ref", NULL, NULL);
607 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "rx-ring-ref", NULL, NULL);
608 #pragma warning(suppress:4054)
609 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL, "event-channel", (PVOID)XenNet_HandleEvent, xi);
610 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mac", NULL, NULL);
611 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !xi->config_csum);
612 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "request-rx-copy", "1", NULL);
613 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-rx-notify", "1", NULL);
614 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-no-csum-offload", buf, NULL);
615 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", (int)xi->config_sg);
616 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-sg", buf, NULL);
617 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !!xi->config_gso);
618 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-gso-tcpv4", buf, NULL);
619 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
621 status = xi->vectors.XenPci_XenConfigDevice(xi->vectors.context);
622 if (!NT_SUCCESS(status))
623 {
624 KdPrint(("Failed to complete device configuration (%08x)\n", status));
625 goto err;
626 }
628 status = XenNet_ConnectBackend(xi);
630 if (!NT_SUCCESS(status))
631 {
632 KdPrint(("Failed to complete device configuration (%08x)\n", status));
633 goto err;
634 }
636 status = NdisMInitializeScatterGatherDma(xi->adapter_handle, TRUE, xi->config_gso);
637 if (!NT_SUCCESS(status))
638 {
639 KdPrint(("NdisMInitializeScatterGatherDma failed (%08x)\n", status));
640 goto err;
641 }
643 XenNet_TxInit(xi);
644 XenNet_RxInit(xi);
646 xi->connected = TRUE;
648 KeMemoryBarrier(); // packets could be received anytime after we set Frontent to Connected
650 FUNCTION_EXIT();
652 return NDIS_STATUS_SUCCESS;
654 err:
655 NdisFreeMemory(xi, 0, 0);
656 *OpenErrorStatus = status;
657 FUNCTION_ERROR_EXIT();
658 return status;
659 }
661 VOID
662 XenNet_PnPEventNotify(
663 IN NDIS_HANDLE MiniportAdapterContext,
664 IN NDIS_DEVICE_PNP_EVENT PnPEvent,
665 IN PVOID InformationBuffer,
666 IN ULONG InformationBufferLength
667 )
668 {
669 UNREFERENCED_PARAMETER(MiniportAdapterContext);
670 UNREFERENCED_PARAMETER(PnPEvent);
671 UNREFERENCED_PARAMETER(InformationBuffer);
672 UNREFERENCED_PARAMETER(InformationBufferLength);
674 FUNCTION_CALLED();
675 }
677 /* Called when machine is shutting down, so just quiesce the HW and be done fast. */
678 VOID DDKAPI
679 XenNet_Shutdown(
680 IN NDIS_HANDLE MiniportAdapterContext
681 )
682 {
683 UNREFERENCED_PARAMETER(MiniportAdapterContext);
685 /* remember we are called at >= DIRQL here */
686 FUNCTION_CALLED();
687 }
689 /* Opposite of XenNet_Init */
690 VOID DDKAPI
691 XenNet_Halt(
692 IN NDIS_HANDLE MiniportAdapterContext
693 )
694 {
695 struct xennet_info *xi = MiniportAdapterContext;
697 FUNCTION_ENTER();
698 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
700 xi->shutting_down = TRUE;
701 KeMemoryBarrier(); /* make sure everyone sees that we are now shutting down */
703 XenNet_TxShutdown(xi);
704 XenNet_RxShutdown(xi);
706 xi->connected = FALSE;
707 KeMemoryBarrier(); /* make sure everyone sees that we are now disconnected */
709 xi->vectors.XenPci_XenShutdownDevice(xi->vectors.context);
711 // TODO: remove event channel xenbus entry (how?)
713 NdisFreeMemory(xi, 0, 0); // <= DISPATCH_LEVEL
715 FUNCTION_EXIT();
716 }
718 NDIS_STATUS
719 XenNet_Reset(
720 PBOOLEAN AddressingReset,
721 NDIS_HANDLE MiniportAdapterContext
722 )
723 {
724 UNREFERENCED_PARAMETER(MiniportAdapterContext);
726 *AddressingReset = FALSE;
727 return NDIS_STATUS_SUCCESS;
728 }
730 dma_driver_extension_t *dma_driver_extension;
732 NTSTATUS DDKAPI
733 DriverEntry(
734 PDRIVER_OBJECT DriverObject,
735 PUNICODE_STRING RegistryPath
736 )
737 {
738 NTSTATUS status;
739 NDIS_HANDLE ndis_wrapper_handle = NULL;
740 NDIS_MINIPORT_CHARACTERISTICS mini_chars;
742 FUNCTION_ENTER();
744 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_DMA_DRIVER_EXTENSION_MAGIC), sizeof(dma_driver_extension_t), &dma_driver_extension);
745 dma_driver_extension->need_virtual_address = NULL;
746 dma_driver_extension->get_alignment = NULL;
747 dma_driver_extension->max_sg_elements = 19; /* header + 18 fragments */
749 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
750 RtlZeroMemory(&mini_chars, sizeof(mini_chars));
752 KdPrint((__DRIVER_NAME " NdisGetVersion = %x\n", NdisGetVersion()));
754 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
755 NdisMInitializeWrapper(&ndis_wrapper_handle, DriverObject, RegistryPath, NULL);
756 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
757 if (!ndis_wrapper_handle)
758 {
759 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper failed\n"));
760 return NDIS_STATUS_FAILURE;
761 }
762 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper succeeded\n"));
764 /* NDIS 5.1 driver */
765 mini_chars.MajorNdisVersion = NDIS_MINIPORT_MAJOR_VERSION;
766 mini_chars.MinorNdisVersion = NDIS_MINIPORT_MINOR_VERSION;
768 KdPrint((__DRIVER_NAME " MajorNdisVersion = %d, MinorNdisVersion = %d\n", NDIS_MINIPORT_MAJOR_VERSION, NDIS_MINIPORT_MINOR_VERSION));
770 mini_chars.HaltHandler = XenNet_Halt;
771 mini_chars.InitializeHandler = XenNet_Init;
772 //mini_chars.ISRHandler = XenNet_InterruptIsr;
773 //mini_chars.HandleInterruptHandler = XenNet_InterruptDpc;
774 mini_chars.QueryInformationHandler = XenNet_QueryInformation;
775 mini_chars.ResetHandler = XenNet_Reset;
776 mini_chars.SetInformationHandler = XenNet_SetInformation;
777 /* added in v.4 -- use multiple pkts interface */
778 mini_chars.ReturnPacketHandler = XenNet_ReturnPacket;
779 mini_chars.SendPacketsHandler = XenNet_SendPackets;
781 #ifdef NDIS51_MINIPORT
782 /* added in v.5.1 */
783 mini_chars.PnPEventNotifyHandler = XenNet_PnPEventNotify;
784 mini_chars.AdapterShutdownHandler = XenNet_Shutdown;
785 #else
786 // something else here
787 #endif
789 /* set up upper-edge interface */
790 KdPrint((__DRIVER_NAME " about to call NdisMRegisterMiniport\n"));
791 status = NdisMRegisterMiniport(ndis_wrapper_handle, &mini_chars, sizeof(mini_chars));
792 KdPrint((__DRIVER_NAME " called NdisMRegisterMiniport\n"));
793 if (!NT_SUCCESS(status))
794 {
795 KdPrint((__DRIVER_NAME " NdisMRegisterMiniport failed, status = 0x%x\n", status));
796 NdisTerminateWrapper(ndis_wrapper_handle, NULL);
797 return status;
798 }
800 FUNCTION_EXIT();
802 return status;
803 }