ReactOS  0.4.14-dev-317-g96040ec
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 
27 NTAPI
29  IN NDIS_HANDLE MiniportAdapterContext,
31  IN UINT Flags)
32 {
33  PE1000_ADAPTER Adapter = (PE1000_ADAPTER)MiniportAdapterContext;
35  ULONG TransmitLength;
36  PHYSICAL_ADDRESS TransmitBuffer;
38 
39  ASSERT(sgList != NULL);
40  ASSERT(sgList->NumberOfElements == 1);
41  ASSERT((sgList->Elements[0].Address.LowPart & 3) == 0);
42  ASSERT(sgList->Elements[0].Length <= MAXIMUM_FRAME_SIZE);
43 
44  if (Adapter->TxFull)
45  {
46  NDIS_DbgPrint(MIN_TRACE, ("All TX descriptors are full\n"));
47  return NDIS_STATUS_RESOURCES;
48  }
49 
50  TransmitLength = sgList->Elements[0].Length;
51  TransmitBuffer = sgList->Elements[0].Address;
52  Adapter->TransmitPackets[Adapter->CurrentTxDesc] = Packet;
53 
54  Status = NICTransmitPacket(Adapter, TransmitBuffer, TransmitLength);
56  {
57  NDIS_DbgPrint(MIN_TRACE, ("Transmit packet failed\n"));
58  return Status;
59  }
60 
61  return NDIS_STATUS_PENDING;
62 }
63 
64 VOID
65 NTAPI
67  IN NDIS_HANDLE MiniportAdapterContext)
68 {
69  PE1000_ADAPTER Adapter = (PE1000_ADAPTER)MiniportAdapterContext;
70 
71  ASSERT(Adapter != NULL);
72 
73  /* First disable sending / receiving */
74  NICDisableTxRx(Adapter);
75 
76  /* Then unregister interrupts */
77  NICUnregisterInterrupts(Adapter);
78 
79  /* Finally, free other resources (Ports, IO ranges,...) */
80  NICReleaseIoResources(Adapter);
81 
82  /* Destroy the adapter context */
83  NdisFreeMemory(Adapter, sizeof(*Adapter), 0);
84 }
85 
87 NTAPI
91  IN PNDIS_MEDIUM MediumArray,
95 {
96  PE1000_ADAPTER Adapter;
98  UINT i;
100  UINT ResourceListSize;
101  PCI_COMMON_CONFIG PciConfig;
102 
103  /* Make sure the medium is supported */
104  for (i = 0; i < MediumArraySize; i++)
105  {
106  if (MediumArray[i] == NdisMedium802_3)
107  {
109  break;
110  }
111  }
112 
113  if (i == MediumArraySize)
114  {
115  NDIS_DbgPrint(MIN_TRACE, ("802.3 medium was not found in the medium array\n"));
117  }
118 
119  ResourceList = NULL;
120  ResourceListSize = 0;
121 
122  /* Allocate our adapter context */
124  sizeof(*Adapter),
125  E1000_TAG);
127  {
128  NDIS_DbgPrint(MIN_TRACE, ("Failed to allocate adapter context (0x%x)\n", Status));
129  return NDIS_STATUS_RESOURCES;
130  }
131 
132  RtlZeroMemory(Adapter, sizeof(*Adapter));
133  Adapter->AdapterHandle = MiniportAdapterHandle;
134 
135  /* Notify NDIS of some characteristics of our NIC */
137  Adapter,
138  0,
141 
142  NdisReadPciSlotInformation(Adapter->AdapterHandle,
143  0,
144  FIELD_OFFSET(PCI_COMMON_CONFIG, VendorID),
145  &PciConfig, sizeof(PciConfig));
146 
147  Adapter->VendorID = PciConfig.VendorID;
148  Adapter->DeviceID = PciConfig.DeviceID;
149 
150  Adapter->SubsystemID = PciConfig.u.type0.SubSystemID;
151  Adapter->SubsystemVendorID = PciConfig.u.type0.SubVendorID;
152 
153 
154  if (!NICRecognizeHardware(Adapter))
155  {
156  NDIS_DbgPrint(MIN_TRACE, ("Hardware not recognized\n"));
158  goto Cleanup;
159  }
160 
161  /* Get our resources for IRQ and IO base information */
164  ResourceList,
165  &ResourceListSize);
167  {
168  NDIS_DbgPrint(MIN_TRACE, ("Unexpected failure of NdisMQueryAdapterResources (0x%x)\n", Status));
170  /* call NdisWriteErrorLogEntry */
171  goto Cleanup;
172  }
173 
175  ResourceListSize,
176  E1000_TAG);
178  {
179  NDIS_DbgPrint(MIN_TRACE, ("Failed to allocate resource list (0x%x)\n", Status));
180  /* call NdisWriteErrorLogEntry */
181  goto Cleanup;
182  }
183 
186  ResourceList,
187  &ResourceListSize);
189  {
190  NDIS_DbgPrint(MIN_TRACE, ("Unexpected failure of NdisMQueryAdapterResources (0x%x)\n", Status));
191  /* call NdisWriteErrorLogEntry */
192  goto Cleanup;
193  }
194 
195  ASSERT(ResourceList->Version == 1);
196  ASSERT(ResourceList->Revision == 1);
197 
199 
200  NdisFreeMemory(ResourceList, ResourceListSize, 0);
201  ResourceList = NULL;
202 
204  {
205  NDIS_DbgPrint(MIN_TRACE, ("Adapter didn't receive enough resources\n"));
206  goto Cleanup;
207  }
208 
209  /* Allocate the DMA resources */
211  FALSE, // 64bit is supported but can be buggy
214  {
215  NDIS_DbgPrint(MIN_TRACE, ("Unable to configure DMA\n"));
217  goto Cleanup;
218  }
219 
220  Status = NICAllocateIoResources(Adapter);
222  {
223  NDIS_DbgPrint(MIN_TRACE, ("Unable to allocate resources\n"));
225  goto Cleanup;
226  }
227 
228  /* Adapter setup */
229  Status = NICPowerOn(Adapter);
231  {
232  NDIS_DbgPrint(MIN_TRACE, ("Unable to power on NIC (0x%x)\n", Status));
233  goto Cleanup;
234  }
235 
236  Status = NICSoftReset(Adapter);
238  {
239  NDIS_DbgPrint(MIN_TRACE, ("Unable to reset the NIC (0x%x)\n", Status));
240  goto Cleanup;
241  }
242 
243  Status = NICGetPermanentMacAddress(Adapter, Adapter->PermanentMacAddress);
245  {
246  NDIS_DbgPrint(MIN_TRACE, ("Unable to get the fixed MAC address (0x%x)\n", Status));
247  goto Cleanup;
248  }
249 
250  RtlCopyMemory(Adapter->MulticastList[0].MacAddress, Adapter->PermanentMacAddress, IEEE_802_ADDR_LENGTH);
251 
252  NICUpdateMulticastList(Adapter);
253 
254  /* Update link state and speed */
255  NICUpdateLinkStatus(Adapter);
256 
257  /* We're ready to handle interrupts now */
258  Status = NICRegisterInterrupts(Adapter);
260  {
261  NDIS_DbgPrint(MIN_TRACE, ("Unable to register interrupt (0x%x)\n", Status));
262  goto Cleanup;
263  }
264 
265  /* Enable interrupts on the NIC */
266  Adapter->InterruptMask = DEFAULT_INTERRUPT_MASK;
267  Status = NICApplyInterruptMask(Adapter);
269  {
270  NDIS_DbgPrint(MIN_TRACE, ("Unable to apply interrupt mask (0x%x)\n", Status));
271  goto Cleanup;
272  }
273 
274  /* Turn on TX and RX now */
275  Status = NICEnableTxRx(Adapter);
277  {
278  NDIS_DbgPrint(MIN_TRACE, ("Unable to enable TX and RX (0x%x)\n", Status));
279  goto Cleanup;
280  }
281 
282  return NDIS_STATUS_SUCCESS;
283 
284 Cleanup:
285  if (ResourceList != NULL)
286  {
287  NdisFreeMemory(ResourceList, ResourceListSize, 0);
288  }
289 
290  MiniportHalt(Adapter);
291 
292  return Status;
293 }
294 
295 NTSTATUS
296 NTAPI
300 {
301  NDIS_HANDLE WrapperHandle;
302  NDIS_MINIPORT_CHARACTERISTICS Characteristics = { 0 };
304 
305  Characteristics.MajorNdisVersion = NDIS_MINIPORT_MAJOR_VERSION;
306  Characteristics.MinorNdisVersion = NDIS_MINIPORT_MINOR_VERSION;
307  Characteristics.CheckForHangHandler = NULL;
308  Characteristics.DisableInterruptHandler = NULL;
309  Characteristics.EnableInterruptHandler = NULL;
310  Characteristics.HaltHandler = MiniportHalt;
311  Characteristics.HandleInterruptHandler = MiniportHandleInterrupt;
312  Characteristics.InitializeHandler = MiniportInitialize;
313  Characteristics.ISRHandler = MiniportISR;
314  Characteristics.QueryInformationHandler = MiniportQueryInformation;
315  Characteristics.ReconfigureHandler = NULL;
316  Characteristics.ResetHandler = MiniportReset;
317  Characteristics.SendHandler = MiniportSend;
318  Characteristics.SetInformationHandler = MiniportSetInformation;
319  Characteristics.TransferDataHandler = NULL;
320  Characteristics.ReturnPacketHandler = NULL;
321  Characteristics.SendPacketsHandler = NULL;
322  Characteristics.AllocateCompleteHandler = NULL;
323 
325  if (!WrapperHandle)
326  {
327  return NDIS_STATUS_FAILURE;
328  }
329 
330  Status = NdisMRegisterMiniport(WrapperHandle, &Characteristics, sizeof(Characteristics));
332  {
333  NdisTerminateWrapper(WrapperHandle, 0);
334  return NDIS_STATUS_FAILURE;
335  }
336 
337  return NDIS_STATUS_SUCCESS;
338 }
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:562
#define IN
Definition: typedefs.h:38
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
_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
_In_ NDIS_HANDLE _In_ PNDIS_PACKET Packet
Definition: ndis.h:1548
NDIS_STATUS NTAPI MiniportReset(OUT PBOOLEAN AddressingReset, IN NDIS_HANDLE MiniportAdapterContext)
Definition: ndis.c:17
#define NDIS_STATUS_PENDING
Definition: ndis.h:347
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
* 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:340
NDIS_STATUS NTAPI NICEnableTxRx(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:605
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
const MUI_LANGUAGE_RESOURCE ResourceList[]
Definition: muilanguages.h:414
NTSTATUS NTAPI DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
Definition: ndis.c:297
_Must_inspect_result_ _Out_ PNDIS_STATUS _Out_ PNDIS_HANDLE _In_ NDIS_HANDLE WrapperConfigurationContext
Definition: ndis.h:3944
int NDIS_STATUS
Definition: ntddndis.h:471
NDIS_STATUS NTAPI NICInitializeAdapterResources(IN PE1000_ADAPTER Adapter, IN PNDIS_RESOURCE_LIST ResourceList)
Definition: hardware.c:272
NDIS_STATUS NTAPI NICUnregisterInterrupts(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:448
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
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
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
smooth NULL
Definition: ftsmooth.c:416
static PDRIVER_OBJECT DriverObject
Definition: template.c:42
VOID NTAPI NICUpdateLinkStatus(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:808
ULONG DebugTraceLevel
Definition: ndis.c:13
VOID EXPORT NdisTerminateWrapper(IN NDIS_HANDLE NdisWrapperHandle, IN PVOID SystemSpecific)
Definition: miniport.c:3012
#define NDIS_ATTRIBUTE_BUS_MASTER
Definition: ndis.h:586
VOID NTAPI MiniportHandleInterrupt(IN NDIS_HANDLE MiniportAdapterContext)
Definition: interrupt.c:40
#define IEEE_802_ADDR_LENGTH
Definition: e1000hw.h:11
BOOLEAN TxFull
Definition: nic.h:77
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define NDIS_STATUS_SUCCESS
Definition: ndis.h:346
NDIS_STATUS NTAPI NICPowerOn(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:540
char * PBOOLEAN
Definition: retypes.h:11
ULONG CurrentTxDesc
Definition: nic.h:75
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:88
NDIS_STATUS NTAPI NICDisableTxRx(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:672
enum _NDIS_MEDIUM * PNDIS_MEDIUM
NDIS_STATUS NTAPI MiniportSend(IN NDIS_HANDLE MiniportAdapterContext, IN PNDIS_PACKET Packet, IN UINT Flags)
Definition: ndis.c:28
NDIS_STATUS NTAPI NICRegisterInterrupts(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:423
VOID NTAPI MiniportHalt(IN NDIS_HANDLE MiniportAdapterContext)
Definition: ndis.c:66
NDIS_STATUS NTAPI NICApplyInterruptMask(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:768
static const WCHAR Cleanup[]
Definition: register.c:80
PNDIS_PACKET TransmitPackets[NUM_TRANSMIT_DESCRIPTORS]
Definition: nic.h:73
Status
Definition: gdiplustypes.h:24
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:232
ULONG EXPORT NdisReadPciSlotInformation(IN NDIS_HANDLE NdisAdapterHandle, IN ULONG SlotNumber, IN ULONG Offset, IN PVOID Buffer, IN ULONG Length)
Definition: hardware.c:180
NDIS_STATUS NTAPI NICTransmitPacket(IN PE1000_ADAPTER Adapter, IN PHYSICAL_ADDRESS PhysicalAddress, IN ULONG Length)
Definition: hardware.c:825
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:254
#define DEFAULT_INTERRUPT_MASK
Definition: nic.h:24
unsigned int UINT
Definition: ndis.h:50
NDIS_STATUS NTAPI NICReleaseIoResources(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:464
#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:722
#define OUT
Definition: typedefs.h:39
#define E1000_TAG
Definition: nic.h:17
struct _SCATTER_GATHER_LIST * PSCATTER_GATHER_LIST
Definition: iotypes.h:2096
#define NDIS_STATUS_RESOURCES
Definition: ndis.h:466
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define MIN_TRACE
Definition: debug.h:14
_In_ PUNICODE_STRING RegistryPath
Definition: wmip.h:27
#define NdisMInitializeWrapper(NdisWrapperHandle, SystemSpecific1, SystemSpecific2, SystemSpecific3)
Definition: ndis.h:5592
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
#define NDIS_PER_PACKET_INFO_FROM_PACKET(Packet, InfoType)
Definition: ndis.h:1351
NDIS_STATUS NTAPI NICGetPermanentMacAddress(IN PE1000_ADAPTER Adapter, OUT PUCHAR MacAddress)
Definition: hardware.c:692
#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:51
BOOLEAN NTAPI NICRecognizeHardware(IN PE1000_ADAPTER Adapter)
Definition: hardware.c:245