win-pvdrivers

view xennet/xennet.c @ 538:e75bb8d68370

Lots more changes...
author James Harper <james.harper@bendigoit.com.au>
date Tue Mar 03 09:51:24 2009 +1100 (2009-03-03)
parents 2a74ac2f43bb
children b0b8be2d30c0
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 XenNet_TxResumeStart(xi);
212 XenNet_RxResumeStart(xi);
213 XenNet_ConnectBackend(xi);
214 XenNet_RxResumeEnd(xi);
215 XenNet_TxResumeEnd(xi);
216 KdPrint((__DRIVER_NAME " *Setting suspend_resume_state_fdo = %d\n", xi->device_state->suspend_resume_state_pdo));
217 xi->device_state->suspend_resume_state_fdo = xi->device_state->suspend_resume_state_pdo;
218 KdPrint((__DRIVER_NAME " *Notifying event channel %d\n", xi->device_state->pdo_event_channel));
219 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
221 FUNCTION_EXIT();
223 }
225 static VOID
226 XenNet_SuspendResume(PKDPC dpc, PVOID context, PVOID arg1, PVOID arg2)
227 {
228 struct xennet_info *xi = context;
229 PIO_WORKITEM work_item;
230 KIRQL old_irql;
232 UNREFERENCED_PARAMETER(dpc);
233 UNREFERENCED_PARAMETER(arg1);
234 UNREFERENCED_PARAMETER(arg2);
236 FUNCTION_ENTER();
238 switch (xi->device_state->suspend_resume_state_pdo)
239 {
240 case SR_STATE_SUSPENDING:
241 KdPrint((__DRIVER_NAME " New state SUSPENDING\n"));
242 KeAcquireSpinLock(&xi->rx_lock, &old_irql);
243 if (xi->tx_id_free == NET_TX_RING_SIZE)
244 {
245 xi->device_state->suspend_resume_state_fdo = SR_STATE_SUSPENDING;
246 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xi->device_state->pdo_event_channel));
247 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
248 }
249 KeReleaseSpinLock(&xi->rx_lock, old_irql);
250 break;
251 case SR_STATE_RESUMING:
252 KdPrint((__DRIVER_NAME " New state SR_STATE_RESUMING\n"));
253 work_item = IoAllocateWorkItem(xi->fdo);
254 IoQueueWorkItem(work_item, XenNet_Resume, DelayedWorkQueue, xi);
255 break;
256 default:
257 KdPrint((__DRIVER_NAME " New state %d\n", xi->device_state->suspend_resume_state_fdo));
258 xi->device_state->suspend_resume_state_fdo = xi->device_state->suspend_resume_state_pdo;
259 KdPrint((__DRIVER_NAME " Notifying event channel %d\n", xi->device_state->pdo_event_channel));
260 xi->vectors.EvtChn_Notify(xi->vectors.context, xi->device_state->pdo_event_channel);
261 break;
262 }
263 KeMemoryBarrier();
265 FUNCTION_EXIT();
266 }
268 static DDKAPI BOOLEAN
269 XenNet_HandleEvent(PVOID context)
270 {
271 struct xennet_info *xi = context;
272 ULONG suspend_resume_state_pdo;
274 //FUNCTION_ENTER();
275 suspend_resume_state_pdo = xi->device_state->suspend_resume_state_pdo;
276 KeMemoryBarrier();
277 // KdPrint((__DRIVER_NAME " connected = %d, inactive = %d, suspend_resume_state_pdo = %d\n",
278 // xi->connected, xi->inactive, suspend_resume_state_pdo));
279 if (suspend_resume_state_pdo != xi->device_state->suspend_resume_state_fdo)
280 {
281 KeInsertQueueDpc(&xi->suspend_dpc, NULL, NULL);
282 }
283 else if (xi->connected && !xi->inactive && suspend_resume_state_pdo != SR_STATE_RESUMING)
284 {
285 KeInsertQueueDpc(&xi->tx_dpc, NULL, NULL);
286 KeInsertQueueDpc(&xi->rx_dpc, NULL, NULL);
287 }
288 //FUNCTION_EXIT();
289 return TRUE;
290 }
292 // Called at <= DISPATCH_LEVEL
293 static DDKAPI NDIS_STATUS
294 XenNet_Init(
295 OUT PNDIS_STATUS OpenErrorStatus,
296 OUT PUINT SelectedMediumIndex,
297 IN PNDIS_MEDIUM MediumArray,
298 IN UINT MediumArraySize,
299 IN NDIS_HANDLE MiniportAdapterHandle,
300 IN NDIS_HANDLE WrapperConfigurationContext
301 )
302 {
303 NDIS_STATUS status;
304 BOOLEAN medium_found = FALSE;
305 struct xennet_info *xi = NULL;
306 UINT nrl_length;
307 PNDIS_RESOURCE_LIST nrl;
308 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
309 KIRQL irq_level = 0;
310 ULONG irq_vector = 0;
311 ULONG irq_mode = 0;
312 NDIS_HANDLE config_handle;
313 NDIS_STRING config_param_name;
314 PNDIS_CONFIGURATION_PARAMETER config_param;
315 ULONG i;
316 PUCHAR ptr;
317 UCHAR type;
318 PCHAR setting, value;
319 ULONG length;
320 CHAR buf[128];
321 PVOID network_address;
322 UINT network_address_length;
324 UNREFERENCED_PARAMETER(OpenErrorStatus);
326 FUNCTION_ENTER();
328 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
330 length = NdisReadPciSlotInformation(MiniportAdapterHandle, 0, 0, &buf, 128);
331 KdPrint((__DRIVER_NAME " NdisReadPciSlotInformation = %d\n", length));
332 /* deal with medium stuff */
333 for (i = 0; i < MediumArraySize; i++)
334 {
335 if (MediumArray[i] == NdisMedium802_3)
336 {
337 medium_found = TRUE;
338 break;
339 }
340 }
341 if (!medium_found)
342 {
343 KdPrint(("NIC_MEDIA_TYPE not in MediumArray\n"));
344 return NDIS_STATUS_UNSUPPORTED_MEDIA;
345 }
346 *SelectedMediumIndex = i;
348 /* Alloc memory for adapter private info */
349 status = NdisAllocateMemoryWithTag((PVOID)&xi, sizeof(*xi), XENNET_POOL_TAG);
350 if (!NT_SUCCESS(status))
351 {
352 KdPrint(("NdisAllocateMemoryWithTag failed with 0x%x\n", status));
353 status = NDIS_STATUS_RESOURCES;
354 goto err;
355 }
356 RtlZeroMemory(xi, sizeof(*xi));
357 xi->adapter_handle = MiniportAdapterHandle;
358 xi->rx_target = RX_DFL_MIN_TARGET;
359 xi->rx_min_target = RX_DFL_MIN_TARGET;
360 xi->rx_max_target = RX_MAX_TARGET;
361 xi->inactive = TRUE;
362 NdisMSetAttributesEx(xi->adapter_handle, (NDIS_HANDLE) xi, 0,
363 #ifdef NDIS51_MINIPORT
364 NDIS_ATTRIBUTE_USES_SAFE_BUFFER_APIS
365 #endif
366 |NDIS_ATTRIBUTE_DESERIALIZE
367 |NDIS_ATTRIBUTE_SURPRISE_REMOVE_OK
368 |NDIS_ATTRIBUTE_BUS_MASTER,
369 NdisInterfaceInternal);
370 xi->multicast_list_size = 0;
372 nrl_length = 0;
373 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
374 NULL, (PUINT)&nrl_length);
375 KdPrint((__DRIVER_NAME " nrl_length = %d\n", nrl_length));
376 status = NdisAllocateMemoryWithTag((PVOID)&nrl, nrl_length, XENNET_POOL_TAG);
377 if (status != NDIS_STATUS_SUCCESS)
378 {
379 KdPrint((__DRIVER_NAME " Could not get allocate memory for Adapter Resources 0x%x\n", status));
380 return NDIS_STATUS_RESOURCES;
381 }
382 NdisMQueryAdapterResources(&status, WrapperConfigurationContext,
383 nrl, (PUINT)&nrl_length);
384 if (status != NDIS_STATUS_SUCCESS)
385 {
386 KdPrint((__DRIVER_NAME " Could not get Adapter Resources 0x%x\n", status));
387 return NDIS_STATUS_RESOURCES;
388 }
389 xi->event_channel = 0;
390 xi->config_csum = 1;
391 xi->config_csum_rx_check = 1;
392 xi->config_sg = 1;
393 xi->config_gso = 61440;
394 xi->config_page = NULL;
395 xi->config_rx_interrupt_moderation = 0;
397 for (i = 0; i < nrl->Count; i++)
398 {
399 prd = &nrl->PartialDescriptors[i];
401 switch(prd->Type)
402 {
403 case CmResourceTypeInterrupt:
404 irq_vector = prd->u.Interrupt.Vector;
405 irq_level = (KIRQL)prd->u.Interrupt.Level;
406 irq_mode = (prd->Flags & CM_RESOURCE_INTERRUPT_LATCHED)?NdisInterruptLatched:NdisInterruptLevelSensitive;
407 KdPrint((__DRIVER_NAME " irq_vector = %03x, irq_level = %03x, irq_mode = %s\n", irq_vector, irq_level,
408 (irq_mode == NdisInterruptLatched)?"NdisInterruptLatched":"NdisInterruptLevelSensitive"));
409 break;
410 case CmResourceTypeMemory:
411 if (xi->config_page)
412 {
413 KdPrint(("More than one memory range\n"));
414 return NDIS_STATUS_RESOURCES;
415 }
416 else
417 {
418 status = NdisMMapIoSpace(&xi->config_page, MiniportAdapterHandle, prd->u.Memory.Start, prd->u.Memory.Length);
419 if (!NT_SUCCESS(status))
420 {
421 KdPrint(("NdisMMapIoSpace failed with 0x%x\n", status));
422 NdisFreeMemory(nrl, nrl_length, 0);
423 return NDIS_STATUS_RESOURCES;
424 }
425 }
426 break;
427 }
428 }
429 NdisFreeMemory(nrl, nrl_length, 0);
430 if (!xi->config_page)
431 {
432 KdPrint(("No config page given\n"));
433 return NDIS_STATUS_RESOURCES;
434 }
436 KeInitializeDpc(&xi->suspend_dpc, XenNet_SuspendResume, xi);
438 NdisMGetDeviceProperty(MiniportAdapterHandle, &xi->pdo, &xi->fdo,
439 &xi->lower_do, NULL, NULL);
440 xi->packet_filter = NDIS_PACKET_TYPE_PROMISCUOUS;
442 status = IoGetDeviceProperty(xi->pdo, DevicePropertyDeviceDescription,
443 NAME_SIZE, xi->dev_desc, &length);
444 if (!NT_SUCCESS(status))
445 {
446 KdPrint(("IoGetDeviceProperty failed with 0x%x\n", status));
447 status = NDIS_STATUS_FAILURE;
448 goto err;
449 }
451 ptr = xi->config_page;
452 while((type = GET_XEN_INIT_RSP(&ptr, (PVOID)&setting, (PVOID)&value, (PVOID)&value)) != XEN_INIT_TYPE_END)
453 {
454 switch(type)
455 {
456 case XEN_INIT_TYPE_VECTORS:
457 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_VECTORS\n"));
458 if (((PXENPCI_VECTORS)value)->length != sizeof(XENPCI_VECTORS) ||
459 ((PXENPCI_VECTORS)value)->magic != XEN_DATA_MAGIC)
460 {
461 KdPrint((__DRIVER_NAME " vectors mismatch (magic = %08x, length = %d)\n",
462 ((PXENPCI_VECTORS)value)->magic, ((PXENPCI_VECTORS)value)->length));
463 FUNCTION_ERROR_EXIT();
464 return NDIS_STATUS_FAILURE;
465 }
466 else
467 memcpy(&xi->vectors, value, sizeof(XENPCI_VECTORS));
468 break;
469 case XEN_INIT_TYPE_STATE_PTR:
470 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_DEVICE_STATE - %p\n", PtrToUlong(value)));
471 xi->device_state = (PXENPCI_DEVICE_STATE)value;
472 break;
473 case XEN_INIT_TYPE_ACTIVE:
474 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_ACTIVE\n"));
475 xi->inactive = FALSE;
476 break;
477 default:
478 KdPrint((__DRIVER_NAME " XEN_INIT_TYPE_%d\n", type));
479 break;
480 }
481 }
483 // now build config page
485 NdisOpenConfiguration(&status, &config_handle, WrapperConfigurationContext);
486 if (!NT_SUCCESS(status))
487 {
488 KdPrint(("Could not open config in registry (%08x)\n", status));
489 status = NDIS_STATUS_RESOURCES;
490 goto err;
491 }
493 NdisInitUnicodeString(&config_param_name, L"ScatterGather");
494 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
495 if (!NT_SUCCESS(status))
496 {
497 KdPrint(("Could not read ScatterGather value (%08x)\n", status));
498 xi->config_sg = 1;
499 }
500 else
501 {
502 KdPrint(("ScatterGather = %d\n", config_param->ParameterData.IntegerData));
503 xi->config_sg = config_param->ParameterData.IntegerData;
504 }
506 NdisInitUnicodeString(&config_param_name, L"LargeSendOffload");
507 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
508 if (!NT_SUCCESS(status))
509 {
510 KdPrint(("Could not read LargeSendOffload value (%08x)\n", status));
511 xi->config_gso = 0;
512 }
513 else
514 {
515 KdPrint(("LargeSendOffload = %d\n", config_param->ParameterData.IntegerData));
516 xi->config_gso = config_param->ParameterData.IntegerData;
517 if (xi->config_gso > 61440)
518 {
519 xi->config_gso = 61440;
520 KdPrint(("(clipped to %d)\n", xi->config_gso));
521 }
522 }
524 NdisInitUnicodeString(&config_param_name, L"ChecksumOffload");
525 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
526 if (!NT_SUCCESS(status))
527 {
528 KdPrint(("Could not read ChecksumOffload value (%08x)\n", status));
529 xi->config_csum = 1;
530 }
531 else
532 {
533 KdPrint(("ChecksumOffload = %d\n", config_param->ParameterData.IntegerData));
534 xi->config_csum = !!config_param->ParameterData.IntegerData;
535 }
537 NdisInitUnicodeString(&config_param_name, L"ChecksumOffloadRxCheck");
538 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
539 if (!NT_SUCCESS(status))
540 {
541 KdPrint(("Could not read ChecksumOffloadRxCheck value (%08x)\n", status));
542 xi->config_csum_rx_check = 1;
543 }
544 else
545 {
546 KdPrint(("ChecksumOffloadRxCheck = %d\n", config_param->ParameterData.IntegerData));
547 xi->config_csum_rx_check = !!config_param->ParameterData.IntegerData;
548 }
550 NdisInitUnicodeString(&config_param_name, L"MTU");
551 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
552 if (!NT_SUCCESS(status))
553 {
554 KdPrint(("Could not read MTU value (%08x)\n", status));
555 xi->config_mtu = 1500;
556 }
557 else
558 {
559 KdPrint(("MTU = %d\n", config_param->ParameterData.IntegerData));
560 xi->config_mtu = config_param->ParameterData.IntegerData;
561 }
563 NdisInitUnicodeString(&config_param_name, L"RxInterruptModeration");
564 NdisReadConfiguration(&status, &config_param, config_handle, &config_param_name, NdisParameterInteger);
565 if (!NT_SUCCESS(status))
566 {
567 KdPrint(("Could not read RxInterruptModeration value (%08x)\n", status));
568 xi->config_rx_interrupt_moderation = 1500;
569 }
570 else
571 {
572 KdPrint(("RxInterruptModeration = %d\n", config_param->ParameterData.IntegerData));
573 xi->config_rx_interrupt_moderation = config_param->ParameterData.IntegerData;
574 }
577 NdisReadNetworkAddress(&status, &network_address, &network_address_length, config_handle);
578 if (!NT_SUCCESS(status) || network_address_length != ETH_ALEN || !(((PUCHAR)network_address)[0] & 0x02))
579 {
580 KdPrint(("Could not read NetworkAddress value (%08x)\n", status));
581 memset(xi->curr_mac_addr, 0, ETH_ALEN);
582 }
583 else
584 {
585 memcpy(xi->curr_mac_addr, network_address, ETH_ALEN);
586 KdPrint((" Set MAC address from registry to %02X:%02X:%02X:%02X:%02X:%02X\n",
587 xi->curr_mac_addr[0], xi->curr_mac_addr[1], xi->curr_mac_addr[2],
588 xi->curr_mac_addr[3], xi->curr_mac_addr[4], xi->curr_mac_addr[5]));
589 }
591 xi->config_max_pkt_size = max(xi->config_mtu + XN_HDR_SIZE, xi->config_gso + XN_HDR_SIZE);
593 NdisCloseConfiguration(config_handle);
595 ptr = xi->config_page;
596 // two XEN_INIT_TYPE_RUNs means go straight to XenbusStateConnected - skip XenbusStateInitialised
597 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
598 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RUN, NULL, NULL, NULL);
599 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "tx-ring-ref", NULL, NULL);
600 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "rx-ring-ref", NULL, NULL);
601 #pragma warning(suppress:4054)
602 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL, "event-channel", (PVOID)XenNet_HandleEvent, xi);
603 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "mac", NULL, NULL);
604 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !xi->config_csum);
605 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "request-rx-copy", "1", NULL);
606 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-rx-notify", "1", NULL);
607 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-no-csum-offload", buf, NULL);
608 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", (int)xi->config_sg);
609 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-sg", buf, NULL);
610 RtlStringCbPrintfA(buf, ARRAY_SIZE(buf), "%d", !!xi->config_gso);
611 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_WRITE_STRING, "feature-gso-tcpv4", buf, NULL);
612 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL, NULL);
614 status = xi->vectors.XenPci_XenConfigDevice(xi->vectors.context);
615 if (!NT_SUCCESS(status))
616 {
617 KdPrint(("Failed to complete device configuration (%08x)\n", status));
618 goto err;
619 }
621 status = XenNet_ConnectBackend(xi);
623 if (!NT_SUCCESS(status))
624 {
625 KdPrint(("Failed to complete device configuration (%08x)\n", status));
626 goto err;
627 }
629 status = NdisMInitializeScatterGatherDma(xi->adapter_handle, TRUE, xi->config_gso);
630 if (!NT_SUCCESS(status))
631 {
632 KdPrint(("NdisMInitializeScatterGatherDma failed (%08x)\n", status));
633 goto err;
634 }
636 XenNet_TxInit(xi);
637 XenNet_RxInit(xi);
639 xi->connected = TRUE;
641 KeMemoryBarrier(); // packets could be received anytime after we set Frontent to Connected
643 FUNCTION_EXIT();
645 return NDIS_STATUS_SUCCESS;
647 err:
648 NdisFreeMemory(xi, 0, 0);
649 *OpenErrorStatus = status;
650 FUNCTION_ERROR_EXIT();
651 return status;
652 }
654 VOID
655 XenNet_PnPEventNotify(
656 IN NDIS_HANDLE MiniportAdapterContext,
657 IN NDIS_DEVICE_PNP_EVENT PnPEvent,
658 IN PVOID InformationBuffer,
659 IN ULONG InformationBufferLength
660 )
661 {
662 UNREFERENCED_PARAMETER(MiniportAdapterContext);
663 UNREFERENCED_PARAMETER(PnPEvent);
664 UNREFERENCED_PARAMETER(InformationBuffer);
665 UNREFERENCED_PARAMETER(InformationBufferLength);
667 FUNCTION_CALLED();
668 }
670 /* Called when machine is shutting down, so just quiesce the HW and be done fast. */
671 VOID DDKAPI
672 XenNet_Shutdown(
673 IN NDIS_HANDLE MiniportAdapterContext
674 )
675 {
676 UNREFERENCED_PARAMETER(MiniportAdapterContext);
678 /* remember we are called at >= DIRQL here */
679 FUNCTION_CALLED();
680 }
682 /* Opposite of XenNet_Init */
683 VOID DDKAPI
684 XenNet_Halt(
685 IN NDIS_HANDLE MiniportAdapterContext
686 )
687 {
688 struct xennet_info *xi = MiniportAdapterContext;
690 FUNCTION_ENTER();
691 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
693 xi->connected = FALSE;
694 KeMemoryBarrier(); /* make sure everyone sees that we are now shut down */
696 XenNet_TxShutdown(xi);
697 XenNet_RxShutdown(xi);
699 xi->vectors.XenPci_XenShutdownDevice(xi->vectors.context);
701 // TODO: remove event channel xenbus entry (how?)
703 NdisFreeMemory(xi, 0, 0); // <= DISPATCH_LEVEL
705 FUNCTION_EXIT();
706 }
708 NDIS_STATUS
709 XenNet_Reset(
710 PBOOLEAN AddressingReset,
711 NDIS_HANDLE MiniportAdapterContext
712 )
713 {
714 UNREFERENCED_PARAMETER(MiniportAdapterContext);
716 *AddressingReset = FALSE;
717 return NDIS_STATUS_SUCCESS;
718 }
720 NTSTATUS DDKAPI
721 DriverEntry(
722 PDRIVER_OBJECT DriverObject,
723 PUNICODE_STRING RegistryPath
724 )
725 {
726 NTSTATUS status;
727 NDIS_HANDLE ndis_wrapper_handle = NULL;
728 NDIS_MINIPORT_CHARACTERISTICS mini_chars;
730 FUNCTION_ENTER();
732 KdPrint((__DRIVER_NAME " DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath));
733 RtlZeroMemory(&mini_chars, sizeof(mini_chars));
735 KdPrint((__DRIVER_NAME " NdisGetVersion = %x\n", NdisGetVersion()));
737 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
738 NdisMInitializeWrapper(&ndis_wrapper_handle, DriverObject, RegistryPath, NULL);
739 KdPrint((__DRIVER_NAME " ndis_wrapper_handle = %p\n", ndis_wrapper_handle));
740 if (!ndis_wrapper_handle)
741 {
742 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper failed\n"));
743 return NDIS_STATUS_FAILURE;
744 }
745 KdPrint((__DRIVER_NAME " NdisMInitializeWrapper succeeded\n"));
747 /* NDIS 5.1 driver */
748 mini_chars.MajorNdisVersion = NDIS_MINIPORT_MAJOR_VERSION;
749 mini_chars.MinorNdisVersion = NDIS_MINIPORT_MINOR_VERSION;
751 KdPrint((__DRIVER_NAME " MajorNdisVersion = %d, MinorNdisVersion = %d\n", NDIS_MINIPORT_MAJOR_VERSION, NDIS_MINIPORT_MINOR_VERSION));
753 mini_chars.HaltHandler = XenNet_Halt;
754 mini_chars.InitializeHandler = XenNet_Init;
755 //mini_chars.ISRHandler = XenNet_InterruptIsr;
756 //mini_chars.HandleInterruptHandler = XenNet_InterruptDpc;
757 mini_chars.QueryInformationHandler = XenNet_QueryInformation;
758 mini_chars.ResetHandler = XenNet_Reset;
759 mini_chars.SetInformationHandler = XenNet_SetInformation;
760 /* added in v.4 -- use multiple pkts interface */
761 mini_chars.ReturnPacketHandler = XenNet_ReturnPacket;
762 mini_chars.SendPacketsHandler = XenNet_SendPackets;
764 #ifdef NDIS51_MINIPORT
765 /* added in v.5.1 */
766 mini_chars.PnPEventNotifyHandler = XenNet_PnPEventNotify;
767 mini_chars.AdapterShutdownHandler = XenNet_Shutdown;
768 #else
769 // something else here
770 #endif
772 /* set up upper-edge interface */
773 KdPrint((__DRIVER_NAME " about to call NdisMRegisterMiniport\n"));
774 status = NdisMRegisterMiniport(ndis_wrapper_handle, &mini_chars, sizeof(mini_chars));
775 KdPrint((__DRIVER_NAME " called NdisMRegisterMiniport\n"));
776 if (!NT_SUCCESS(status))
777 {
778 KdPrint((__DRIVER_NAME " NdisMRegisterMiniport failed, status = 0x%x\n", status));
779 NdisTerminateWrapper(ndis_wrapper_handle, NULL);
780 return status;
781 }
783 FUNCTION_EXIT();
785 return status;
786 }