ReactOS  0.4.15-dev-4574-g434cf85
ndis.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Intel PRO/1000 Driver
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Driver entrypoint
5  * COPYRIGHT: Copyright 2013 Cameron Gutman (cameron.gutman@reactos.org)
6  * Copyright 2018 Mark Jansen (mark.jansen@reactos.org)
7  */
8 
9 #include "nic.h"
10 
11 #include <debug.h>
12 
14 
16 NTAPI
18  OUT PBOOLEAN AddressingReset,
19  IN NDIS_HANDLE MiniportAdapterContext)
20 {
21  *AddressingReset = FALSE;
23  return NDIS_STATUS_FAILURE;
24 }
25 
26 VOID
27 NTAPI
29  IN NDIS_HANDLE MiniportAdapterContext)
30 {
31  PE1000_ADAPTER Adapter = (PE1000_ADAPTER)MiniportAdapterContext;
32 
33  ASSERT(Adapter != NULL);
34 
35  /* First disable sending / receiving */
36  NICDisableTxRx(Adapter);
37 
38  /* Then unregister interrupts */
39  NICUnregisterInterrupts(Adapter);
40 
41  /* Finally, free other resources (Ports, IO ranges,...) */
42  NICReleaseIoResources(Adapter);
43 
44  /* Destroy the adapter context */
45  NdisFreeMemory(Adapter, sizeof(*Adapter), 0);
46 }
47 
49 NTAPI
53  IN PNDIS_MEDIUM MediumArray,
57 {
58  PE1000_ADAPTER Adapter;
60  UINT i;
62  UINT ResourceListSize;
63  PCI_COMMON_CONFIG PciConfig;
64 
65  /* Make sure the medium is supported */
66  for (i = 0; i < MediumArraySize; i++)
67  {
68  if (MediumArray[i] == NdisMedium802_3)
69  {
71  break;
72  }
73  }
74 
75  if (i == MediumArraySize)
76  {
77  NDIS_DbgPrint(MIN_TRACE, ("802.3 medium was not found in the medium array\n"));
79  }
80 
82  ResourceListSize = 0;
83 
84  /* Allocate our adapter context */
86  sizeof(*Adapter),
87  E1000_TAG);
89  {
90  NDIS_DbgPrint(MIN_TRACE, ("Failed to allocate adapter context (0x%x)\n", Status));
91  return NDIS_STATUS_RESOURCES;
92  }
93 
94  RtlZeroMemory(Adapter, sizeof(*Adapter));
95  Adapter->AdapterHandle = MiniportAdapterHandle;
96 
97  /* Notify NDIS of some characteristics of our NIC */
99  Adapter,
100  0,
103 
104  NdisReadPciSlotInformation(Adapter->AdapterHandle,
105  0,
106  FIELD_OFFSET(PCI_COMMON_CONFIG, VendorID),
107  &PciConfig, sizeof(PciConfig));
108 
109  Adapter->VendorID = PciConfig.VendorID;
110  Adapter->DeviceID = PciConfig.DeviceID;
111 
112  Adapter->SubsystemID = PciConfig.u.type0.SubSystemID;
113  Adapter->SubsystemVendorID = PciConfig.u.type0.SubVendorID;
114 
115 
116  if (!NICRecognizeHardware(Adapter))
117  {
118  NDIS_DbgPrint(MIN_TRACE, ("Hardware not recognized\n"));
120  goto Cleanup;
121  }
122 
123  /* Get our resources for IRQ and IO base information */
126  ResourceList,
127  &ResourceListSize);
129  {
130  NDIS_DbgPrint(MIN_TRACE, ("Unexpected failure of NdisMQueryAdapterResources (0x%x)\n", Status));
132  /* call NdisWriteErrorLogEntry */
133  goto Cleanup;
134  }
135 
137  ResourceListSize,
138  E1000_TAG);
140  {
141  NDIS_DbgPrint(MIN_TRACE, ("Failed to allocate resource list (0x%x)\n", Status));
142  /* call NdisWriteErrorLogEntry */
143  goto Cleanup;
144  }
145 
148  ResourceList,
149  &ResourceListSize);
151  {
152  NDIS_DbgPrint(MIN_TRACE, ("Unexpected failure of NdisMQueryAdapterResources (0x%x)\n", Status));
153  /* call NdisWriteErrorLogEntry */
154  goto Cleanup;
155  }
156 
157  ASSERT(ResourceList->Version == 1);
158  ASSERT(ResourceList->Revision == 1);
159 
161 
162  NdisFreeMemory(ResourceList, ResourceListSize, 0);
163  ResourceList = NULL;
164 
166  {
167  NDIS_DbgPrint(MIN_TRACE, ("Adapter didn't receive enough resources\n"));
168  goto Cleanup;
169  }
170 
171  /* Allocate the DMA resources */
173  FALSE, // 64bit is supported but can be buggy
176  {
177  NDIS_DbgPrint(MIN_TRACE, ("Unable to configure DMA\n"));
179  goto Cleanup;
180  }
181 
182  Status = NICAllocateIoResources(Adapter);
184  {
185  NDIS_DbgPrint(MIN_TRACE, ("Unable to allocate resources\n"));
187  goto Cleanup;
188  }
189 
190  /* Adapter setup */
191  Status = NICPowerOn(Adapter);
193  {
194  NDIS_DbgPrint(MIN_TRACE, ("Unable to power on NIC (0x%x)\n", Status));
195  goto Cleanup;
196  }
197 
198  Status = NICSoftReset(Adapter);
200  {
201  NDIS_DbgPrint(MIN_TRACE, ("Unable to reset the NIC (0x%x)\n", Status));
202  goto Cleanup;
203  }
204 
205  Status = NICGetPermanentMacAddress(Adapter, Adapter->PermanentMacAddress);
207  {
208  NDIS_DbgPrint(MIN_TRACE, ("Unable to get the fixed MAC address (0x%x)\n", Status));
209  goto Cleanup;
210  }
211 
212  RtlCopyMemory(Adapter->MulticastList[0].MacAddress, Adapter->PermanentMacAddress, IEEE_802_ADDR_LENGTH);
213 
214  NICUpdateMulticastList(Adapter);
215 
216  /* Update link state and speed */
217  NICUpdateLinkStatus(Adapter);
218 
219  /* We're ready to handle interrupts now */
220  Status = NICRegisterInterrupts(Adapter);
222  {
223  NDIS_DbgPrint(MIN_TRACE, ("Unable to register interrupt (0x%x)\n", Status));
224  goto Cleanup;
225  }
226 
227  /* Enable interrupts on the NIC */
228  Adapter->InterruptMask = DEFAULT_INTERRUPT_MASK;
229  NICApplyInterruptMask(Adapter);
230 
231  /* Turn on TX and RX now */
232  Status = NICEnableTxRx(Adapter);
234  {
235  NDIS_DbgPrint(MIN_TRACE, ("Unable to enable TX and RX (0x%x)\n", Status));
236  goto Cleanup;
237  }
238 
239  return NDIS_STATUS_SUCCESS;
240 
241 Cleanup:
242  if (ResourceList != NULL)
243  {
244  NdisFreeMemory(ResourceList, ResourceListSize, 0);
245  }
246 
247  MiniportHalt(Adapter);
248 
249  return Status;
250 }
251 
252 NTSTATUS
253 NTAPI
257 {
258  NDIS_HANDLE WrapperHandle;
259  NDIS_MINIPORT_CHARACTERISTICS Characteristics = { 0 };
261 
262  Characteristics.MajorNdisVersion = NDIS_MINIPORT_MAJOR_VERSION;
263  Characteristics.MinorNdisVersion = NDIS_MINIPORT_MINOR_VERSION;
264  Characteristics.CheckForHangHandler = NULL;
265  Characteristics.DisableInterruptHandler = NULL;
266  Characteristics.EnableInterruptHandler = NULL;
267  Characteristics.HaltHandler = MiniportHalt;
268  Characteristics.HandleInterruptHandler = MiniportHandleInterrupt;
269  Characteristics.InitializeHandler = MiniportInitialize;
270  Characteristics.ISRHandler = MiniportISR;
271  Characteristics.QueryInformationHandler = MiniportQueryInformation;
272  Characteristics.ReconfigureHandler = NULL;
273  Characteristics.ResetHandler = MiniportReset;
274  Characteristics.SendHandler = MiniportSend;
275  Characteristics.SetInformationHandler = MiniportSetInformation;
276  Characteristics.TransferDataHandler = NULL;
277  Characteristics.ReturnPacketHandler = NULL;
278  Characteristics.SendPacketsHandler = NULL;
279  Characteristics.AllocateCompleteHandler = NULL;
280 
282  if (!WrapperHandle)
283  {
284  return NDIS_STATUS_FAILURE;
285  }
286 
287  Status = NdisMRegisterMiniport(WrapperHandle, &Characteristics, sizeof(Characteristics));
289  {
290  NdisTerminateWrapper(WrapperHandle, 0);
291  return NDIS_STATUS_FAILURE;
292  }
293 
294  return NDIS_STATUS_SUCCESS;
295 }
VOID NTAPI MiniportISR(OUT PBOOLEAN InterruptRecognized, OUT PBOOLEAN QueueMiniportHandleInterrupt, IN NDIS_HANDLE MiniportAdapterContext)
Definition: interrupt.c:16
NDIS_STATUS NTAPI NICSoftReset(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:531
#define IN
Definition: typedefs.h:39
_Must_inspect_result_ _Out_ PNDIS_STATUS _Out_ PNDIS_STATUS OpenErrorStatus
Definition: ndis.h:6008
VOID EXPORT NdisMQueryAdapterResources(OUT PNDIS_STATUS Status, IN NDIS_HANDLE WrapperConfigurationContext, OUT PNDIS_RESOURCE_LIST ResourceList, IN OUT PUINT BufferSize)
Definition: hardware.c:103
NDIS_STATUS NTAPI MiniportReset(OUT PBOOLEAN AddressingReset, IN NDIS_HANDLE MiniportAdapterContext)
Definition: ndis.c:17
NDIS_STATUS EXPORT NdisAllocateMemoryWithTag(OUT PVOID *VirtualAddress, IN UINT Length, IN ULONG Tag)
Definition: memory.c:21
struct _E1000_ADAPTER * PE1000_ADAPTER
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:213
* PNDIS_STATUS
Definition: ndis.h:45
_In_ NDIS_HANDLE MiniportAdapterHandle
Definition: ndis.h:4668
NDIS_STATUS NTAPI NICAllocateIoResources(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:309
NDIS_STATUS NTAPI NICEnableTxRx(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:574
NDIS_STATUS EXPORT NdisMInitializeScatterGatherDma(IN NDIS_HANDLE MiniportAdapterHandle, IN BOOLEAN Dma64BitAddresses, IN ULONG MaximumPhysicalMapping)
Definition: io.c:1169
_Must_inspect_result_ _Out_ PNDIS_STATUS _Out_ PNDIS_STATUS _Out_ PNDIS_HANDLE _Out_ PUINT SelectedMediumIndex
Definition: ndis.h:6008
_Must_inspect_result_ _Out_ PNDIS_STATUS _Out_ PNDIS_STATUS _Out_ PNDIS_HANDLE _Out_ PUINT _In_ UINT MediumArraySize
Definition: ndis.h:6013
#define NDIS_STATUS_UNSUPPORTED_MEDIA
Definition: ndis.h:490
_Must_inspect_result_ _In_ WDFIORESREQLIST _In_opt_ PWDF_OBJECT_ATTRIBUTES _Out_ WDFIORESLIST * ResourceList
Definition: wdfresource.h:304
NTSTATUS NTAPI DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
Definition: ndis.c:254
_Must_inspect_result_ _Out_ PNDIS_STATUS _Out_ PNDIS_HANDLE _In_ NDIS_HANDLE WrapperConfigurationContext
Definition: ndis.h:3944
int NDIS_STATUS
Definition: ntddndis.h:475
NDIS_STATUS NTAPI NICInitializeAdapterResources(IN PE1000_ADAPTER Adapter, IN PNDIS_RESOURCE_LIST ResourceList)
Definition: hardware.c:241
NDIS_STATUS NTAPI NICUnregisterInterrupts(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:417
NDIS_STATUS NTAPI MiniportSend(IN NDIS_HANDLE MiniportAdapterContext, IN PNDIS_PACKET Packet, IN UINT Flags)
Definition: ndis.c:42
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define FALSE
Definition: types.h:117
VOID NTAPI NICUpdateLinkStatus(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:738
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
Definition: wdfdriver.h:213
ULONG DebugTraceLevel
Definition: ndis.c:13
Status
Definition: gdiplustypes.h:24
VOID EXPORT NdisTerminateWrapper(IN NDIS_HANDLE NdisWrapperHandle, IN PVOID SystemSpecific)
Definition: miniport.c:3012
#define NDIS_ATTRIBUTE_BUS_MASTER
Definition: ndis.h:586
#define ASSERT(a)
Definition: mode.c:44
VOID NTAPI MiniportHandleInterrupt(IN NDIS_HANDLE MiniportAdapterContext)
Definition: interrupt.c:46
#define IEEE_802_ADDR_LENGTH
Definition: e1000hw.h:11
FORCEINLINE VOID NICApplyInterruptMask(_In_ PE1000_ADAPTER Adapter)
Definition: nic.h:243
#define NDIS_STATUS_SUCCESS
Definition: ndis.h:346
NDIS_STATUS NTAPI NICPowerOn(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:509
char * PBOOLEAN
Definition: retypes.h:11
NDIS_STATUS NTAPI MiniportInitialize(OUT PNDIS_STATUS OpenErrorStatus, OUT PUINT SelectedMediumIndex, IN PNDIS_MEDIUM MediumArray, IN UINT MediumArraySize, IN NDIS_HANDLE MiniportAdapterHandle, IN NDIS_HANDLE WrapperConfigurationContext)
Definition: ndis.c:50
NDIS_STATUS NTAPI NICDisableTxRx(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:641
enum _NDIS_MEDIUM * PNDIS_MEDIUM
NDIS_STATUS NTAPI NICRegisterInterrupts(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:392
VOID NTAPI MiniportHalt(IN NDIS_HANDLE MiniportAdapterContext)
Definition: ndis.c:28
static const WCHAR Cleanup[]
Definition: register.c:80
NDIS_STATUS NTAPI MiniportSetInformation(IN NDIS_HANDLE MiniportAdapterContext, IN NDIS_OID Oid, IN PVOID InformationBuffer, IN ULONG InformationBufferLength, OUT PULONG BytesRead, OUT PULONG BytesNeeded)
Definition: info.c:276
ULONG EXPORT NdisReadPciSlotInformation(IN NDIS_HANDLE NdisAdapterHandle, IN ULONG SlotNumber, IN ULONG Offset, IN PVOID Buffer, IN ULONG Length)
Definition: hardware.c:180
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define DEFAULT_INTERRUPT_MASK
Definition: nic.h:24
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
NDIS_STATUS NTAPI NICReleaseIoResources(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:433
#define NDIS_DbgPrint(_t_, _x_)
Definition: debug.h:40
#define UNIMPLEMENTED_DBGBREAK(...)
Definition: debug.h:57
NDIS_STATUS NTAPI NICUpdateMulticastList(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:691
#define OUT
Definition: typedefs.h:40
#define E1000_TAG
Definition: nic.h:17
#define NDIS_STATUS_RESOURCES
Definition: ndis.h:466
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define MIN_TRACE
Definition: debug.h:14
#define NdisMInitializeWrapper(NdisWrapperHandle, SystemSpecific1, SystemSpecific2, SystemSpecific3)
Definition: ndis.h:5592
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
NDIS_STATUS EXPORT NdisMRegisterMiniport(IN NDIS_HANDLE NdisWrapperHandle, IN PNDIS_MINIPORT_CHARACTERISTICS MiniportCharacteristics, IN UINT CharacteristicsLength)
Definition: miniport.c:2637
#define MAXIMUM_FRAME_SIZE
Definition: nic.h:19
NDIS_STATUS NTAPI NICGetPermanentMacAddress(IN PE1000_ADAPTER Adapter, OUT PUCHAR MacAddress)
Definition: hardware.c:661
#define NDIS_STATUS_FAILURE
Definition: ndis.h:465
VOID EXPORT NdisFreeMemory(IN PVOID VirtualAddress, IN UINT Length, IN UINT MemoryFlags)
Definition: memory.c:110
VOID EXPORT NdisMSetAttributesEx(IN NDIS_HANDLE MiniportAdapterHandle, IN NDIS_HANDLE MiniportAdapterContext, IN UINT CheckForHangTimeInSeconds OPTIONAL, IN ULONG AttributeFlags, IN NDIS_INTERFACE_TYPE AdapterType)
Definition: miniport.c:2883
unsigned int * PUINT
Definition: ndis.h:50
NDIS_STATUS NTAPI MiniportQueryInformation(IN NDIS_HANDLE MiniportAdapterContext, IN NDIS_OID Oid, IN PVOID InformationBuffer, IN ULONG InformationBufferLength, OUT PULONG BytesWritten, OUT PULONG BytesNeeded)
Definition: info.c:73
BOOLEAN NTAPI NICRecognizeHardware(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:214