win-pvdrivers

view xennet/xennet.c @ 576:dc0a293c870c

Fixed a problem where shutdown was hanging if there were packets in the tx ring
author James Harper <james.harper@bendigoit.com.au>
date Thu May 21 00:04:46 2009 +1000 (2009-05-21)
parents 8ab116a713f4
children bdb2d6b163f3
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 (suspend_resume_state_pdo != xi->device_state->suspend_resume_state_fdo)
284 {
285 KeInsertQueueDpc(&xi->suspend_dpc, NULL, NULL);
286 }
287 else if (xi->connected && !xi->inactive && suspend_resume_state_pdo != SR_STATE_RESUMING)
288 {
289 KeInsertQueueDpc(&xi->tx_dpc, NULL, NULL);
290 KeInsertQueueDpc(&xi->rx_dpc, NULL, NULL);
291 }
292 //FUNCTION_EXIT();
293 return TRUE;
294 }
296 // Called at <= DISPATCH_LEVEL
297 static DDKAPI NDIS_STATUS
298 XenNet_Init(
299 OUT PNDIS_STATUS OpenErrorStatus,
300 OUT PUINT SelectedMediumIndex,
301 IN PNDIS_MEDIUM MediumArray,
302 IN UINT MediumArraySize,
303 IN NDIS_HANDLE MiniportAdapterHandle,
304 IN NDIS_HANDLE WrapperConfigurationContext
305 )
306 {
307 NDIS_STATUS status;
308 BOOLEAN medium_found = FALSE;
309 struct xennet_info *xi = NULL;
310 UINT nrl_length;
311 PNDIS_RESOURCE_LIST nrl;
312 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
313 KIRQL irq_level = 0;
314 ULONG irq_vector = 0;
315 ULONG irq_mode = 0;
316 NDIS_HANDLE config_handle;
317 NDIS_STRING config_param_name;
318 PNDIS_CONFIGURATION_PARAMETER config_param;
319 ULONG i;
320 PUCHAR ptr;
321 UCHAR type;
322 PCHAR setting, value;
323 ULONG length;
324 CHAR buf[128];
325 PVOID network_address;
326 UINT network_address_length;
328 UNREFERENCED_PARAMETER(OpenErrorStatus);
330 FUNCTION_ENTER();
332 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
334 length = NdisReadPciSlotInformation(MiniportAdapterHandle, 0, 0, &buf, 128);
335 KdPrint((__DRIVER_NAME " NdisReadPciSlotInformation = %d\n", length));
336 /* deal with medium stuff */
337 for (i = 0; i < MediumArraySize; i++)
338 {
339 if (MediumArray[i] == NdisMedium802_3)
340 {
341 medium_found = TRUE;
342 break;
343 }
344 }
345 if (!medium_found)
346 {
347 KdPrint(("NIC_MEDIA_TYPE not in MediumArray\n"));
348 return NDIS_STATUS_UNSUPPORTED_MEDIA;
349 }
350 *SelectedMediumIndex = i;
352 /* Alloc memory for adapter private info */
353 status = NdisAllocateMemoryWithTag((PVOID)&xi, sizeof(*xi), XENNET_POOL_TAG);
354 if (!NT_SUCCESS(status))
355 {
356 KdPrint(("NdisAllocateMemoryWithTag failed with 0x%x\n", status));
357 status = NDIS_STATUS_RESOURCES;
358 goto err;
359 }
360 RtlZeroMemory(xi, sizeof(*xi));
361 xi->adapter_handle = MiniportAdapterHandle;
362 xi->rx_target = RX_DFL_MIN_TARGET;
363 xi->rx_min_target = RX_DFL_MIN_TARGET;
364 xi->rx_max_target = RX_MAX_TARGET;
365 xi->inactive = TRUE;
366 NdisMSetAttributesEx(xi->adapter_handle, (NDIS_HANDLE) xi, 0,
367 #ifdef NDIS51_MINIPORT
368 NDIS_ATTRIBUTE_USES_SAFE_BUFFER_APIS
369 #endif
370 |NDIS_ATTRIBUTE_DESERIALIZE
371 |NDIS_ATTRIBUTE_SURPRISE_REMOVE_OK
372 |NDIS_ATTRIBUTE_BUS_MASTER,
373 NdisInterfaceInternal);
374 xi->multicast_list_size = 0;
376 nrl_length = 0;
377 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
378 NULL, (PUINT)&nrl_length);
379 KdPrint((__DRIVER_NAME " nrl_length = %d\n", nrl_length));
380 status = NdisAllocateMemoryWithTag((PVOID)&nrl, nrl_length, XENNET_POOL_TAG);
381 if (status != NDIS_STATUS_SUCCESS)
382 {
383 KdPrint((__DRIVER_NAME " Could not get allocate memory for Adapter Resources 0x%x\n", status));
384 return NDIS_STATUS_RESOURCES;
385 }
386 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
387 nrl, (PUINT)&nrl_length);
388 if (status != NDIS_STATUS_SUCCESS)
389 {
390 KdPrint((__DRIVER_NAME " Could not get Adapter Resources 0x%x\n", status));
391 return NDIS_STATUS_RESOURCES;
392 }
393 xi->event_channel = 0;
394 xi->config_csum = 1;
395 xi->config_csum_rx_check = 1;
396 xi->config_sg = 1;
397 xi->config_gso = 61440;
398 xi->config_page = NULL;
399 xi->config_rx_interrupt_moderation = 0;
401 for (i = 0; i < nrl->Count; i++)
402 {
403 prd = &nrl->PartialDescriptors[i];
405 switch(prd->Type)
406 {
407 case CmResourceTypeInterrupt:
408 irq_vector = prd->u.Interrupt.Vector;
409 irq_level = (KIRQL)prd->u.Interrupt.Level;
410 irq_mode = (prd->Flags & CM_RESOURCE_INTERRUPT_LATCHED)?NdisInterruptLatched:NdisInterruptLevelSensitive;
411 KdPrint((__DRIVER_NAME " irq_vector = %03x, irq_level = %03x, irq_mode = %s\n", irq_vector, irq_level,
412 (irq_mode == NdisInterruptLatched)?"NdisInterruptLatched":"NdisInterruptLevelSensitive"));
413 break;
414 case CmResourceTypeMemory:
415 if (xi->config_page)
416 {
417 KdPrint(("More than one memory range\n"));
418 return NDIS_STATUS_RESOURCES;
419 }
420 else
421 {
422 status = NdisMMapIoSpace(&xi->config_page, MiniportAdapterHandle, prd->u.Memory.Start, prd->u.Memory.Length);
423 if (!NT_SUCCESS(status))
424 {
425 KdPrint(("NdisMMapIoSpace failed with 0x%x\n", status));
426 NdisFreeMemory(nrl, nrl_length, 0);
427 return NDIS_STATUS_RESOURCES;
428 }
429 }
430 break;
431 }
432 }
433 NdisFreeMemory(nrl, nrl_length, 0);
434 if (!xi->config_page)
435 {
436 KdPrint(("No config page given\n"));
437 return NDIS_STATUS_RESOURCES;
438 }
440 KeInitializeDpc(&xi->suspend_dpc, XenNet_SuspendResume, xi);
442 NdisMGetDeviceProperty(MiniportAdapterHandle, &xi->pdo, &xi->fdo,
443 &xi->lower_do, NULL, NULL);
444 xi->packet_filter = NDIS_PACKET_TYPE_PROMISCUOUS;
446 status = IoGetDeviceProperty(xi->pdo, DevicePropertyDeviceDescription,
447 NAME_SIZE, xi->dev_desc, &length);
448 if (!NT_SUCCESS(status))
449 {
450 KdPrint(("IoGetDeviceProperty failed with 0x%x\n", status));
451 status = NDIS_STATUS_FAILURE;
452 goto err;
453 }
455 ptr = xi->config_page;
456 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value)) != XEN_INIT_TYPE_END)
457 {
458 switch(type)
459 {
460 case XEN_INIT_TYPE_VECTORS:
461 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
462 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
463 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
464 {
465 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
466 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
467 FUNCTION_ERROR_EXIT();
468 return NDIS_STATUS_FAILURE;
469 }
470 else
471 memcpy(&xi->vectors, value, sizeof(XENPCI_VECTORS));
472 break;
473 case XEN_INIT_TYPE_STATE_PTR:
474 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
475 xi->device_state = (PXENPCI_DEVICE_STATE)value;
476 break;
477 case XEN_INIT_TYPE_ACTIVE:
478 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_ACTIVE\n"));
479 xi->inactive = FALSE;
480 break;
481 default:
482 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
483 break;
484 }
485 }
487 // now build config page
489 NdisOpenConfiguration(&status, &config_handle, WrapperConfigurationContext);
490 if (!NT_SUCCESS(status))
491 {
492 KdPrint(("Could not open config in registry (%08x)\n", status));
493 status = NDIS_STATUS_RESOURCES;
494 goto err;
495 }
497 NdisInitUnicodeString(&config_param_name, L"ScatterGather");
498 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
499 if (!NT_SUCCESS(status))
500 {
501 KdPrint(("Could not read ScatterGather value (%08x)\n", status));
502 xi->config_sg = 1;
503 }
504 else
505 {
506 KdPrint(("ScatterGather = %d\n", config_param->ParameterData.IntegerData));
507 xi->config_sg = config_param->ParameterData.IntegerData;
508 }
510 NdisInitUnicodeString(&config_param_name, L"LargeSendOffload");
511 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
512 if (!NT_SUCCESS(status))
513 {
514 KdPrint(("Could not read LargeSendOffload value (%08x)\n", status));
515 xi->config_gso = 0;
516 }
517 else
518 {
519 KdPrint(("LargeSendOffload = %d\n", config_param->ParameterData.IntegerData));
520 xi->config_gso = config_param->ParameterData.IntegerData;
521 if (xi->config_gso > 61440)
522 {
523 xi->config_gso = 61440;
524 KdPrint(("(clipped to %d)\n", xi->config_gso));
525 }
526 }
528 NdisInitUnicodeString(&config_param_name, L"ChecksumOffload");
529 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
530 if (!NT_SUCCESS(status))
531 {
532 KdPrint(("Could not read ChecksumOffload value (%08x)\n", status));
533 xi->config_csum = 1;
534 }
535 else
536 {
537 KdPrint(("ChecksumOffload = %d\n", config_param->ParameterData.IntegerData));
538 xi->config_csum = !!config_param->ParameterData.IntegerData;
539 }
541 NdisInitUnicodeString(&config_param_name, L"ChecksumOffloadRxCheck");
542 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
543 if (!NT_SUCCESS(status))
544 {
545 KdPrint(("Could not read ChecksumOffloadRxCheck value (%08x)\n", status));
546 xi->config_csum_rx_check = 1;
547 }
548 else
549 {
550 KdPrint(("ChecksumOffloadRxCheck = %d\n", config_param->ParameterData.IntegerData));
551 xi->config_csum_rx_check = !!config_param->ParameterData.IntegerData;
552 }
554 NdisInitUnicodeString(&config_param_name, L"MTU");
555 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
556 if (!NT_SUCCESS(status))
557 {
558 KdPrint(("Could not read MTU value (%08x)\n", status));
559 xi->config_mtu = 1500;
560 }
561 else
562 {
563 KdPrint(("MTU = %d\n", config_param->ParameterData.IntegerData));
564 xi->config_mtu = config_param->ParameterData.IntegerData;
565 }
567 NdisInitUnicodeString(&config_param_name, L"RxInterruptModeration");
568 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
569 if (!NT_SUCCESS(status))
570 {
571 KdPrint(("Could not read RxInterruptModeration value (%08x)\n", status));
572 xi->config_rx_interrupt_moderation = 1500;
573 }
574 else
575 {
576 KdPrint(("RxInterruptModeration = %d\n", config_param->ParameterData.IntegerData));
577 xi->config_rx_interrupt_moderation = config_param->ParameterData.IntegerData;
578 }
581 NdisReadNetworkAddress(&status, &network_address, &network_address_length, config_handle);
582 if (!NT_SUCCESS(status) || network_address_length != ETH_ALEN || !(((PUCHAR)network_address)[0] & 0x02))
583 {
584 KdPrint(("Could not read NetworkAddress value (%08x)\n", status));
585 memset(xi->curr_mac_addr, 0, ETH_ALEN);
586 }
587 else
588 {
589 memcpy(xi->curr_mac_addr, network_address, ETH_ALEN);
590 KdPrint((" Set MAC address from registry to %02X:%02X:%02X:%02X:%02X:%02X\n",
591 xi->curr_mac_addr[0], xi->curr_mac_addr[1], xi->curr_mac_addr[2],
592 xi->curr_mac_addr[3], xi->curr_mac_addr[4], xi->curr_mac_addr[5]));
593 }
595 xi->config_max_pkt_size = max(xi->config_mtu + XN_HDR_SIZE, xi->config_gso + XN_HDR_SIZE);
597 NdisCloseConfiguration(config_handle);
599 ptr = xi->config_page;
600 // two XEN_INIT_TYPE_RUNs means go straight to XenbusStateConnected - skip XenbusStateInitialised
601 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
602 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
603 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "tx-ring-ref", NULL, NULL);
604 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "rx-ring-ref", NULL, NULL);
605 #pragma warning(suppress:4054)
606 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL, "event-channel", (PVOID)XenNet_HandleEvent, xi);
607 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mac", NULL, NULL);
608 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !xi->config_csum);
609 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "request-rx-copy", "1", NULL);
610 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-rx-notify", "1", NULL);
611 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-no-csum-offload", buf, NULL);
612 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", (int)xi->config_sg);
613 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-sg", buf, NULL);
614 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !!xi->config_gso);
615 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-gso-tcpv4", buf, NULL);
616 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
618 status = xi->vectors.XenPci_XenConfigDevice(xi->vectors.context);
619 if (!NT_SUCCESS(status))
620 {
621 KdPrint(("Failed to complete device configuration (%08x)\n", status));
622 goto err;
623 }
625 status = XenNet_ConnectBackend(xi);
627 if (!NT_SUCCESS(status))
628 {
629 KdPrint(("Failed to complete device configuration (%08x)\n", status));
630 goto err;
631 }
633 status = NdisMInitializeScatterGatherDma(xi->adapter_handle, TRUE, xi->config_gso);
634 if (!NT_SUCCESS(status))
635 {
636 KdPrint(("NdisMInitializeScatterGatherDma failed (%08x)\n", status));
637 goto err;
638 }
640 XenNet_TxInit(xi);
641 XenNet_RxInit(xi);
643 xi->connected = TRUE;
645 KeMemoryBarrier(); // packets could be received anytime after we set Frontent to Connected
647 FUNCTION_EXIT();
649 return NDIS_STATUS_SUCCESS;
651 err:
652 NdisFreeMemory(xi, 0, 0);
653 *OpenErrorStatus = status;
654 FUNCTION_ERROR_EXIT();
655 return status;
656 }
658 VOID
659 XenNet_PnPEventNotify(
660 IN NDIS_HANDLE MiniportAdapterContext,
661 IN NDIS_DEVICE_PNP_EVENT PnPEvent,
662 IN PVOID InformationBuffer,
663 IN ULONG InformationBufferLength
664 )
665 {
666 UNREFERENCED_PARAMETER(MiniportAdapterContext);
667 UNREFERENCED_PARAMETER(PnPEvent);
668 UNREFERENCED_PARAMETER(InformationBuffer);
669 UNREFERENCED_PARAMETER(InformationBufferLength);
671 FUNCTION_CALLED();
672 }
674 /* Called when machine is shutting down, so just quiesce the HW and be done fast. */
675 VOID DDKAPI
676 XenNet_Shutdown(
677 IN NDIS_HANDLE MiniportAdapterContext
678 )
679 {
680 UNREFERENCED_PARAMETER(MiniportAdapterContext);
682 /* remember we are called at >= DIRQL here */
683 FUNCTION_CALLED();
684 }
686 /* Opposite of XenNet_Init */
687 VOID DDKAPI
688 XenNet_Halt(
689 IN NDIS_HANDLE MiniportAdapterContext
690 )
691 {
692 struct xennet_info *xi = MiniportAdapterContext;
694 FUNCTION_ENTER();
695 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
697 xi->shutting_down = TRUE;
698 KeMemoryBarrier(); /* make sure everyone sees that we are now shutting down */
700 XenNet_TxShutdown(xi);
701 XenNet_RxShutdown(xi);
703 xi->connected = FALSE;
704 KeMemoryBarrier(); /* make sure everyone sees that we are now disconnected */
706 xi->vectors.XenPci_XenShutdownDevice(xi->vectors.context);
708 // TODO: remove event channel xenbus entry (how?)
710 NdisFreeMemory(xi, 0, 0); // <= DISPATCH_LEVEL
712 FUNCTION_EXIT();
713 }
715 NDIS_STATUS
716 XenNet_Reset(
717 PBOOLEAN AddressingReset,
718 NDIS_HANDLE MiniportAdapterContext
719 )
720 {
721 UNREFERENCED_PARAMETER(MiniportAdapterContext);
723 *AddressingReset = FALSE;
724 return NDIS_STATUS_SUCCESS;
725 }
727 dma_driver_extension_t *dma_driver_extension;
729 NTSTATUS DDKAPI
730 DriverEntry(
731 PDRIVER_OBJECT DriverObject,
732 PUNICODE_STRING RegistryPath
733 )
734 {
735 NTSTATUS status;
736 NDIS_HANDLE ndis_wrapper_handle = NULL;
737 NDIS_MINIPORT_CHARACTERISTICS mini_chars;
739 FUNCTION_ENTER();
741 IoAllocateDriverObjectExtension(DriverObject, UlongToPtr(XEN_DMA_DRIVER_EXTENSION_MAGIC), sizeof(dma_driver_extension_t), &dma_driver_extension);
742 dma_driver_extension->need_virtual_address = NULL;
743 dma_driver_extension->get_alignment = NULL;
744 dma_driver_extension->max_sg_elements = 19; /* header + 18 fragments */
746 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
747 RtlZeroMemory(&mini_chars, sizeof(mini_chars));
749 KdPrint((__DRIVER_NAME " NdisGetVersion = %x\n", NdisGetVersion()));
751 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
752 NdisMInitializeWrapper(&ndis_wrapper_handle, DriverObject, RegistryPath, NULL);
753 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
754 if (!ndis_wrapper_handle)
755 {
756 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper failed\n"));
757 return NDIS_STATUS_FAILURE;
758 }
759 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper succeeded\n"));
761 /* NDIS 5.1 driver */
762 mini_chars.MajorNdisVersion = NDIS_MINIPORT_MAJOR_VERSION;
763 mini_chars.MinorNdisVersion = NDIS_MINIPORT_MINOR_VERSION;
765 KdPrint((__DRIVER_NAME " MajorNdisVersion = %d, MinorNdisVersion = %d\n", NDIS_MINIPORT_MAJOR_VERSION, NDIS_MINIPORT_MINOR_VERSION));
767 mini_chars.HaltHandler = XenNet_Halt;
768 mini_chars.InitializeHandler = XenNet_Init;
769 //mini_chars.ISRHandler = XenNet_InterruptIsr;
770 //mini_chars.HandleInterruptHandler = XenNet_InterruptDpc;
771 mini_chars.QueryInformationHandler = XenNet_QueryInformation;
772 mini_chars.ResetHandler = XenNet_Reset;
773 mini_chars.SetInformationHandler = XenNet_SetInformation;
774 /* added in v.4 -- use multiple pkts interface */
775 mini_chars.ReturnPacketHandler = XenNet_ReturnPacket;
776 mini_chars.SendPacketsHandler = XenNet_SendPackets;
778 #ifdef NDIS51_MINIPORT
779 /* added in v.5.1 */
780 mini_chars.PnPEventNotifyHandler = XenNet_PnPEventNotify;
781 mini_chars.AdapterShutdownHandler = XenNet_Shutdown;
782 #else
783 // something else here
784 #endif
786 /* set up upper-edge interface */
787 KdPrint((__DRIVER_NAME " about to call NdisMRegisterMiniport\n"));
788 status = NdisMRegisterMiniport(ndis_wrapper_handle, &mini_chars, sizeof(mini_chars));
789 KdPrint((__DRIVER_NAME " called NdisMRegisterMiniport\n"));
790 if (!NT_SUCCESS(status))
791 {
792 KdPrint((__DRIVER_NAME " NdisMRegisterMiniport failed, status = 0x%x\n", status));
793 NdisTerminateWrapper(ndis_wrapper_handle, NULL);
794 return status;
795 }
797 FUNCTION_EXIT();
799 return status;
800 }