2 * ReactOS AMD PCNet Driver
4 * Copyright (C) 2003 Vizzini <vizzini@plasmic.com>
5 * Copyright (C) 2004 Filip Navara <navaraf@reactos.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * 09-Sep-2003 vizzini - Created
23 * 10-Oct-2004 navaraf - Fix receive to work on VMware adapters (
24 * need to set busmaster bit on PCI).
25 * - Indicate receive completition.
26 * - Implement packet transmitting.
27 * - Don't read slot number from registry and
28 * report itself as NDIS 5.0 miniport.
29 * 11-Oct-2004 navaraf - Fix nasty bugs in halt code path.
30 * 17-Oct-2004 navaraf - Add multicast support.
31 * - Add media state detection support.
32 * - Protect the adapter context with spinlock
33 * and move code talking to card to inside
34 * NdisMSynchronizeWithInterrupt calls where
38 * - this assumes a 32-bit machine
52 IN PDRIVER_OBJECT DriverObject
,
53 IN PUNICODE_STRING RegistryPath
);
57 MiniportHandleInterrupt(
58 IN NDIS_HANDLE MiniportAdapterContext
)
60 * FUNCTION: Handle an interrupt if told to by MiniportISR
62 * MiniportAdapterContext: context specified to NdisMSetAttributes
64 * - Called by NDIS at DISPATCH_LEVEL
67 PADAPTER Adapter
= (PADAPTER
)MiniportAdapterContext
;
72 NdisDprAcquireSpinLock(&Adapter
->Lock
);
74 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
75 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
77 DPRINT("CSR0 is 0x%x\n", Data
);
79 while(Data
& CSR0_INTR
)
81 /* Clear interrupt flags early to avoid race conditions. */
82 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, Data
);
86 DPRINT("error: %x\n", Data
& (CSR0_MERR
|CSR0_BABL
|CSR0_CERR
|CSR0_MISS
));
88 Adapter
->Statistics
.XmtCollisions
++;
96 DPRINT("receive interrupt\n");
100 PRECEIVE_DESCRIPTOR Descriptor
= Adapter
->ReceiveDescriptorRingVirt
+ Adapter
->CurrentReceiveDescriptorIndex
;
104 if(Descriptor
->FLAGS
& RD_OWN
)
106 DPRINT("no more receive descriptors to process\n");
110 if(Descriptor
->FLAGS
& RD_ERR
)
112 DPRINT("receive descriptor error: 0x%x\n", Descriptor
->FLAGS
);
113 if (Descriptor
->FLAGS
& RD_BUFF
)
114 Adapter
->Statistics
.RcvBufferErrors
++;
115 if (Descriptor
->FLAGS
& RD_CRC
)
116 Adapter
->Statistics
.RcvCrcErrors
++;
117 if (Descriptor
->FLAGS
& RD_OFLO
)
118 Adapter
->Statistics
.RcvOverflowErrors
++;
119 if (Descriptor
->FLAGS
& RD_FRAM
)
120 Adapter
->Statistics
.RcvFramingErrors
++;
124 if(!((Descriptor
->FLAGS
& RD_STP
) && (Descriptor
->FLAGS
& RD_ENP
)))
126 DPRINT("receive descriptor not start&end: 0x%x\n", Descriptor
->FLAGS
);
130 Buffer
= Adapter
->ReceiveBufferPtrVirt
+ Adapter
->CurrentReceiveDescriptorIndex
* BUFFER_SIZE
;
131 ByteCount
= Descriptor
->MCNT
& 0xfff;
133 DPRINT("Indicating a %d-byte packet (index %d)\n", ByteCount
, Adapter
->CurrentReceiveDescriptorIndex
);
135 NdisMEthIndicateReceive(Adapter
->MiniportAdapterHandle
, 0, Buffer
, 14, Buffer
+14, ByteCount
-14, ByteCount
-14);
136 NdisMEthIndicateReceiveComplete(Adapter
->MiniportAdapterHandle
);
138 RtlZeroMemory(Descriptor
, sizeof(RECEIVE_DESCRIPTOR
));
140 (ULONG
)(Adapter
->ReceiveBufferPtrPhys
+ Adapter
->CurrentReceiveDescriptorIndex
* BUFFER_SIZE
);
141 Descriptor
->BCNT
= (-BUFFER_SIZE
) | 0xf000;
142 Descriptor
->FLAGS
|= RD_OWN
;
144 Adapter
->CurrentReceiveDescriptorIndex
++;
145 Adapter
->CurrentReceiveDescriptorIndex
%= NUMBER_OF_BUFFERS
;
147 Adapter
->Statistics
.RcvGoodFrames
++;
152 PTRANSMIT_DESCRIPTOR Descriptor
;
154 DPRINT("transmit interrupt\n");
156 while (Adapter
->CurrentTransmitStartIndex
!=
157 Adapter
->CurrentTransmitEndIndex
)
159 Descriptor
= Adapter
->TransmitDescriptorRingVirt
+ Adapter
->CurrentTransmitStartIndex
;
161 DPRINT("buffer %d flags %x flags2 %x\n",
162 Adapter
->CurrentTransmitStartIndex
,
163 Descriptor
->FLAGS
, Descriptor
->FLAGS2
);
165 if (Descriptor
->FLAGS
& TD1_OWN
)
167 DPRINT("non-TXed buffer\n");
171 if (Descriptor
->FLAGS
& TD1_STP
)
173 if (Descriptor
->FLAGS
& TD1_ONE
)
174 Adapter
->Statistics
.XmtOneRetry
++;
175 else if (Descriptor
->FLAGS
& TD1_MORE
)
176 Adapter
->Statistics
.XmtMoreThanOneRetry
++;
179 if (Descriptor
->FLAGS
& TD1_ERR
)
181 DPRINT("major error: %x\n", Descriptor
->FLAGS2
);
182 if (Descriptor
->FLAGS2
& TD2_RTRY
)
183 Adapter
->Statistics
.XmtRetryErrors
++;
184 if (Descriptor
->FLAGS2
& TD2_LCAR
)
185 Adapter
->Statistics
.XmtLossesOfCarrier
++;
186 if (Descriptor
->FLAGS2
& TD2_LCOL
)
187 Adapter
->Statistics
.XmtLateCollisions
++;
188 if (Descriptor
->FLAGS2
& TD2_EXDEF
)
189 Adapter
->Statistics
.XmtExcessiveDefferals
++;
190 if (Descriptor
->FLAGS2
& TD2_UFLO
)
191 Adapter
->Statistics
.XmtBufferUnderflows
++;
192 if (Descriptor
->FLAGS2
& TD2_BUFF
)
193 Adapter
->Statistics
.XmtBufferErrors
++;
197 Adapter
->CurrentTransmitStartIndex
++;
198 Adapter
->CurrentTransmitStartIndex
%= NUMBER_OF_BUFFERS
;
200 Adapter
->Statistics
.XmtGoodFrames
++;
202 NdisMSendResourcesAvailable(Adapter
->MiniportAdapterHandle
);
204 if(Data
& ~(CSR0_ERR
| CSR0_IDON
| CSR0_RINT
| CSR0_TINT
))
206 DPRINT("UNHANDLED INTERRUPT CSR0 0x%x\n", Data
);
209 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
212 /* re-enable interrupts */
213 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, CSR0_IENA
);
215 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
216 DPRINT("CSR0 is now 0x%x\n", Data
);
218 NdisDprReleaseSpinLock(&Adapter
->Lock
);
225 * FUNCTION: Detect the PCNET NIC in the configured slot and query its I/O address and interrupt vector
227 * MiniportAdapterContext: context supplied to NdisMSetAttributes
229 * NDIS_STATUS_FAILURE on a general error
230 * NDIS_STATUS_ADAPTER_NOT_FOUND on not finding the adapter
231 * NDIS_STATUS_SUCCESS on succes
238 /* Detect the card in the configured slot */
239 Status
= NdisReadPciSlotInformation(Adapter
->MiniportAdapterHandle
, 0, PCI_PCIID
, &buf32
, 4);
242 Status
= NDIS_STATUS_FAILURE
;
243 DPRINT("NdisReadPciSlotInformation failed\n");
250 Status
= NDIS_STATUS_ADAPTER_NOT_FOUND
;
251 DPRINT("card in slot isn't our: 0x%x\n", 0, buf32
);
256 /* set busmaster and io space enable bits */
257 buf32
= PCI_BMEN
| PCI_IOEN
;
258 NdisWritePciSlotInformation(Adapter
->MiniportAdapterHandle
, 0, PCI_COMMAND
, &buf32
, 4);
260 /* get IO base physical address */
262 Status
= NdisReadPciSlotInformation(Adapter
->MiniportAdapterHandle
, 0, PCI_IOBAR
, &buf32
, 4);
265 Status
= NDIS_STATUS_FAILURE
;
266 DPRINT("NdisReadPciSlotInformation failed\n");
273 DPRINT("No base i/o address set\n");
274 return NDIS_STATUS_FAILURE
;
277 buf32
&= ~1; /* even up address - comes out odd for some reason */
279 DPRINT("detected io address 0x%x\n", buf32
);
280 Adapter
->IoBaseAddress
= buf32
;
282 /* get interrupt vector */
283 Status
= NdisReadPciSlotInformation(Adapter
->MiniportAdapterHandle
, 0, PCI_ILR
, &buf8
, 1);
286 Status
= NDIS_STATUS_FAILURE
;
287 DPRINT1("NdisReadPciSlotInformation failed\n");
292 DPRINT("interrupt: 0x%x\n", buf8
);
293 Adapter
->InterruptVector
= buf8
;
295 return NDIS_STATUS_SUCCESS
;
299 MiAllocateSharedMemory(
302 * FUNCTION: Allocate all shared memory used by the miniport
304 * Adapter: Pointer to the miniport's adapter object
306 * NDIS_STATUS_RESOURCES on insufficient memory
307 * NDIS_STATUS_SUCCESS on success
310 PTRANSMIT_DESCRIPTOR TransmitDescriptor
;
311 PRECEIVE_DESCRIPTOR ReceiveDescriptor
;
312 NDIS_PHYSICAL_ADDRESS PhysicalAddress
;
315 /* allocate the initialization block */
316 Adapter
->InitializationBlockLength
= sizeof(INITIALIZATION_BLOCK
);
317 NdisMAllocateSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->InitializationBlockLength
,
318 FALSE
, (PVOID
*)&Adapter
->InitializationBlockVirt
, &PhysicalAddress
);
319 if(!Adapter
->InitializationBlockVirt
)
321 DPRINT1("insufficient resources\n");
323 return NDIS_STATUS_RESOURCES
;
326 if(((ULONG
)Adapter
->InitializationBlockVirt
& 0x00000003) != 0)
328 DPRINT("address 0x%x not dword-aligned\n", Adapter
->InitializationBlockVirt
);
330 return NDIS_STATUS_RESOURCES
;
333 Adapter
->InitializationBlockPhys
= (PINITIALIZATION_BLOCK
)NdisGetPhysicalAddressLow(PhysicalAddress
);
335 /* allocate the transport descriptor ring */
336 Adapter
->TransmitDescriptorRingLength
= sizeof(TRANSMIT_DESCRIPTOR
) * NUMBER_OF_BUFFERS
;
337 NdisMAllocateSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->TransmitDescriptorRingLength
,
338 FALSE
, (PVOID
*)&Adapter
->TransmitDescriptorRingVirt
, &PhysicalAddress
);
339 if(!Adapter
->TransmitDescriptorRingVirt
)
341 DPRINT1("insufficient resources\n");
343 return NDIS_STATUS_RESOURCES
;
346 if(((ULONG
)Adapter
->TransmitDescriptorRingVirt
& 0x00000003) != 0)
348 DPRINT("address 0x%x not dword-aligned\n", Adapter
->TransmitDescriptorRingVirt
);
350 return NDIS_STATUS_RESOURCES
;
353 Adapter
->TransmitDescriptorRingPhys
= (PTRANSMIT_DESCRIPTOR
)NdisGetPhysicalAddressLow(PhysicalAddress
);
354 RtlZeroMemory(Adapter
->TransmitDescriptorRingVirt
, sizeof(TRANSMIT_DESCRIPTOR
) * NUMBER_OF_BUFFERS
);
356 /* allocate the receive descriptor ring */
357 Adapter
->ReceiveDescriptorRingLength
= sizeof(RECEIVE_DESCRIPTOR
) * NUMBER_OF_BUFFERS
;
358 NdisMAllocateSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->ReceiveDescriptorRingLength
,
359 FALSE
, (PVOID
*)&Adapter
->ReceiveDescriptorRingVirt
, &PhysicalAddress
);
360 if(!Adapter
->ReceiveDescriptorRingVirt
)
362 DPRINT1("insufficient resources\n");
364 return NDIS_STATUS_RESOURCES
;
367 if(((ULONG
)Adapter
->ReceiveDescriptorRingVirt
& 0x00000003) != 0)
369 DPRINT("address 0x%x not dword-aligned\n", Adapter
->ReceiveDescriptorRingVirt
);
371 return NDIS_STATUS_RESOURCES
;
374 Adapter
->ReceiveDescriptorRingPhys
= (PRECEIVE_DESCRIPTOR
)NdisGetPhysicalAddressLow(PhysicalAddress
);
375 RtlZeroMemory(Adapter
->ReceiveDescriptorRingVirt
, sizeof(RECEIVE_DESCRIPTOR
) * NUMBER_OF_BUFFERS
);
377 /* allocate transmit buffers */
378 Adapter
->TransmitBufferLength
= BUFFER_SIZE
* NUMBER_OF_BUFFERS
;
379 NdisMAllocateSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->TransmitBufferLength
,
380 FALSE
, (PVOID
*)&Adapter
->TransmitBufferPtrVirt
, &PhysicalAddress
);
381 if(!Adapter
->TransmitBufferPtrVirt
)
383 DPRINT1("insufficient resources\n");
385 return NDIS_STATUS_RESOURCES
;
388 if(((ULONG
)Adapter
->TransmitBufferPtrVirt
& 0x00000003) != 0)
390 DPRINT("address 0x%x not dword-aligned\n", Adapter
->TransmitBufferPtrVirt
);
392 return NDIS_STATUS_RESOURCES
;
395 Adapter
->TransmitBufferPtrPhys
= (PCHAR
)NdisGetPhysicalAddressLow(PhysicalAddress
);
396 RtlZeroMemory(Adapter
->TransmitBufferPtrVirt
, BUFFER_SIZE
* NUMBER_OF_BUFFERS
);
398 /* allocate receive buffers */
399 Adapter
->ReceiveBufferLength
= BUFFER_SIZE
* NUMBER_OF_BUFFERS
;
400 NdisMAllocateSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->ReceiveBufferLength
,
401 FALSE
, (PVOID
*)&Adapter
->ReceiveBufferPtrVirt
, &PhysicalAddress
);
402 if(!Adapter
->ReceiveBufferPtrVirt
)
404 DPRINT1("insufficient resources\n");
406 return NDIS_STATUS_RESOURCES
;
409 if(((ULONG
)Adapter
->ReceiveBufferPtrVirt
& 0x00000003) != 0)
411 DPRINT("address 0x%x not dword-aligned\n", Adapter
->ReceiveBufferPtrVirt
);
413 return NDIS_STATUS_RESOURCES
;
416 Adapter
->ReceiveBufferPtrPhys
= (PCHAR
)NdisGetPhysicalAddressLow(PhysicalAddress
);
417 RtlZeroMemory(Adapter
->ReceiveBufferPtrVirt
, BUFFER_SIZE
* NUMBER_OF_BUFFERS
);
419 /* initialize tx descriptors */
420 TransmitDescriptor
= Adapter
->TransmitDescriptorRingVirt
;
421 for(i
= 0; i
< NUMBER_OF_BUFFERS
; i
++)
423 (TransmitDescriptor
+i
)->TBADR
= (ULONG
)Adapter
->TransmitBufferPtrPhys
+ i
* BUFFER_SIZE
;
424 (TransmitDescriptor
+i
)->BCNT
= 0xf000 | -BUFFER_SIZE
; /* 2's compliment + set top 4 bits */
425 (TransmitDescriptor
+i
)->FLAGS
= TD1_STP
| TD1_ENP
;
428 DPRINT("transmit ring initialized\n");
431 ReceiveDescriptor
= Adapter
->ReceiveDescriptorRingVirt
;
432 for(i
= 0; i
< NUMBER_OF_BUFFERS
; i
++)
434 (ReceiveDescriptor
+i
)->RBADR
= (ULONG
)Adapter
->ReceiveBufferPtrPhys
+ i
* BUFFER_SIZE
;
435 (ReceiveDescriptor
+i
)->BCNT
= 0xf000 | -BUFFER_SIZE
; /* 2's compliment + set top 4 bits */
436 (ReceiveDescriptor
+i
)->FLAGS
= RD_OWN
;
439 DPRINT("receive ring initialized\n");
441 return NDIS_STATUS_SUCCESS
;
445 MiPrepareInitializationBlock(
448 * FUNCTION: Initialize the initialization block
450 * Adapter: pointer to the miniport's adapter object
455 RtlZeroMemory(Adapter
->InitializationBlockVirt
, sizeof(INITIALIZATION_BLOCK
));
457 /* read burned-in address from card */
458 for(i
= 0; i
< 6; i
++)
459 NdisRawReadPortUchar(Adapter
->PortOffset
+ i
, Adapter
->InitializationBlockVirt
->PADR
+ i
);
460 DPRINT("MAC address: %02x-%02x-%02x-%02x-%02x-%02x\n",
461 Adapter
->InitializationBlockVirt
->PADR
[0],
462 Adapter
->InitializationBlockVirt
->PADR
[1],
463 Adapter
->InitializationBlockVirt
->PADR
[2],
464 Adapter
->InitializationBlockVirt
->PADR
[3],
465 Adapter
->InitializationBlockVirt
->PADR
[4],
466 Adapter
->InitializationBlockVirt
->PADR
[5]);
468 /* set up receive ring */
469 DPRINT("Receive ring physical address: 0x%x\n", Adapter
->ReceiveDescriptorRingPhys
);
470 Adapter
->InitializationBlockVirt
->RDRA
= (ULONG
)Adapter
->ReceiveDescriptorRingPhys
;
471 Adapter
->InitializationBlockVirt
->RLEN
= (LOG_NUMBER_OF_BUFFERS
<< 4) & 0xf0;
473 /* set up transmit ring */
474 DPRINT("Transmit ring physical address: 0x%x\n", Adapter
->TransmitDescriptorRingPhys
);
475 Adapter
->InitializationBlockVirt
->TDRA
= (ULONG
)Adapter
->TransmitDescriptorRingPhys
;
476 Adapter
->InitializationBlockVirt
->TLEN
= (LOG_NUMBER_OF_BUFFERS
<< 4) & 0xf0;
483 * FUNCTION: Free all allocated shared memory
485 * Adapter: pointer to the miniport's adapter struct
488 NDIS_PHYSICAL_ADDRESS PhysicalAddress
;
490 PhysicalAddress
.u
.HighPart
= 0;
492 if(Adapter
->InitializationBlockVirt
)
494 PhysicalAddress
.u
.LowPart
= (ULONG
)Adapter
->InitializationBlockPhys
;
495 NdisMFreeSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->InitializationBlockLength
,
496 FALSE
, Adapter
->InitializationBlockVirt
, PhysicalAddress
);
499 if(Adapter
->TransmitDescriptorRingVirt
)
501 PhysicalAddress
.u
.LowPart
= (ULONG
)Adapter
->TransmitDescriptorRingPhys
;
502 NdisMFreeSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->TransmitDescriptorRingLength
,
503 FALSE
, Adapter
->TransmitDescriptorRingVirt
, PhysicalAddress
);
506 if(Adapter
->ReceiveDescriptorRingVirt
)
508 PhysicalAddress
.u
.LowPart
= (ULONG
)Adapter
->ReceiveDescriptorRingPhys
;
509 NdisMFreeSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->ReceiveDescriptorRingLength
,
510 FALSE
, Adapter
->ReceiveDescriptorRingVirt
, PhysicalAddress
);
513 if(Adapter
->TransmitBufferPtrVirt
)
515 PhysicalAddress
.u
.LowPart
= (ULONG
)Adapter
->TransmitBufferPtrPhys
;
516 NdisMFreeSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->TransmitBufferLength
,
517 FALSE
, Adapter
->TransmitBufferPtrVirt
, PhysicalAddress
);
520 if(Adapter
->ReceiveBufferPtrVirt
)
522 PhysicalAddress
.u
.LowPart
= (ULONG
)Adapter
->ReceiveBufferPtrPhys
;
523 NdisMFreeSharedMemory(Adapter
->MiniportAdapterHandle
, Adapter
->ReceiveBufferLength
,
524 FALSE
, Adapter
->ReceiveBufferPtrVirt
, PhysicalAddress
);
531 IN PVOID SynchronizeContext
)
533 * FUNCTION: Stop the adapter
535 * SynchronizeContext: Adapter context
538 PADAPTER Adapter
= (PADAPTER
)SynchronizeContext
;
539 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
540 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, CSR0_STOP
);
547 IN NDIS_HANDLE MiniportAdapterContext
)
549 * FUNCTION: Stop the adapter and release any per-adapter resources
551 * MiniportAdapterContext: context specified to NdisMSetAttributes
553 * - Called by NDIS at PASSIVE_LEVEL
556 PADAPTER Adapter
= (PADAPTER
)MiniportAdapterContext
;
557 BOOLEAN TimerCancelled
;
562 /* stop the media detection timer */
563 NdisMCancelTimer(&Adapter
->MediaDetectionTimer
, &TimerCancelled
);
566 NdisMSynchronizeWithInterrupt(&Adapter
->InterruptObject
, MiSyncStop
, Adapter
);
568 /* deregister the interrupt */
569 NdisMDeregisterInterrupt(&Adapter
->InterruptObject
);
571 /* deregister i/o port range */
572 NdisMDeregisterIoPortRange(Adapter
->MiniportAdapterHandle
, Adapter
->IoBaseAddress
, NUMBER_OF_PORTS
, (PVOID
)Adapter
->PortOffset
);
574 /* free shared memory */
575 MiFreeSharedMemory(Adapter
);
577 /* free map registers */
578 NdisMFreeMapRegisters(Adapter
->MiniportAdapterHandle
);
581 NdisFreeSpinLock(&Adapter
->Lock
);
583 /* free the adapter */
584 NdisFreeMemory(Adapter
, 0, 0);
589 MiSyncMediaDetection(
590 IN PVOID SynchronizeContext
)
592 * FUNCTION: Stop the adapter
594 * SynchronizeContext: Adapter context
597 PADAPTER Adapter
= (PADAPTER
)SynchronizeContext
;
598 NDIS_MEDIA_STATE MediaState
= MiGetMediaState(Adapter
);
601 DPRINT("MediaState: %d\n", MediaState
);
602 if (MediaState
!= Adapter
->MediaState
)
604 Adapter
->MediaState
= MediaState
;
612 MiniportMediaDetectionTimer(
613 IN PVOID SystemSpecific1
,
614 IN PVOID FunctionContext
,
615 IN PVOID SystemSpecific2
,
616 IN PVOID SystemSpecific3
)
618 * FUNCTION: Periodially query media state
620 * FunctionContext: Adapter context
622 * - Called by NDIS at DISPATCH_LEVEL
625 PADAPTER Adapter
= (PADAPTER
)FunctionContext
;
627 if (NdisMSynchronizeWithInterrupt(&Adapter
->InterruptObject
,
628 MiSyncMediaDetection
,
631 NdisMIndicateStatus(Adapter
->MiniportAdapterHandle
,
632 Adapter
->MediaState
== NdisMediaStateConnected
?
633 NDIS_STATUS_MEDIA_CONNECT
: NDIS_STATUS_MEDIA_DISCONNECT
,
635 NdisMIndicateStatusComplete(Adapter
->MiniportAdapterHandle
);
643 * FUNCTION: Initialize and start the PCNET chip
645 * Adapter: pointer to the miniport's adapter struct
647 * - should be coded to detect failure and return an error
648 * - the vmware virtual lance chip doesn't support 32-bit i/o so don't do that.
656 * first reset the chip - 32-bit reset followed by 16-bit reset. if it's in 32-bit mode, it'll reset
657 * twice. if it's in 16-bit mode, the first read will be nonsense and the second will be a reset. the
658 * card is reset by reading from the reset register. on reset it's in 16-bit i/o mode.
660 NdisRawReadPortUshort(Adapter
->PortOffset
+ RESET32
, &Data
);
661 NdisRawReadPortUshort(Adapter
->PortOffset
+ RESET16
, &Data
);
664 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
665 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, CSR0_STOP
);
667 /* pause for 1ms so the chip will have time to reset */
668 NdisStallExecution(1);
670 DPRINT("chip stopped\n");
672 /* set the software style to 2 (32 bits) */
673 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR58
);
674 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
678 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, Data
);
680 /* set up csr4: auto transmit pad, disable polling, disable transmit interrupt, dmaplus */
681 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR4
);
682 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
684 Data
|= CSR4_APAD_XMT
| /* CSR4_DPOLL |*/ CSR4_TXSTRTM
| CSR4_DMAPLUS
;
685 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, Data
);
687 /* set up bcr18: burst read/write enable */
688 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, BCR18
);
689 NdisRawReadPortUshort(Adapter
->PortOffset
+ BDP
, &Data
);
691 Data
|= BCR18_BREADE
| BCR18_BWRITE
;
692 NdisRawWritePortUshort(Adapter
->PortOffset
+ BDP
, Data
);
694 /* set up csr1 and csr2 with init block */
695 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR1
);
696 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, (USHORT
)((ULONG
)Adapter
->InitializationBlockPhys
& 0xffff));
697 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR2
);
698 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, (USHORT
)((ULONG
)Adapter
->InitializationBlockPhys
>> 16) & 0xffff);
700 DPRINT("programmed with init block\n");
704 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR15
);
705 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, Data
);
707 /* load init block and start the card */
708 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
709 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, CSR0_STRT
|CSR0_INIT
|CSR0_IENA
);
711 /* detect the media state */
712 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, BCR4
);
713 NdisRawWritePortUshort(Adapter
->PortOffset
+ BDP
, BCR4_LNKSTE
|BCR4_FDLSE
);
714 Adapter
->MediaState
= MiGetMediaState(Adapter
);
716 DPRINT("card started\n");
718 Adapter
->Flags
&= ~RESET_IN_PROGRESS
;
726 * FUNCTION: Test the NIC
728 * Adapter: pointer to the miniport's adapter struct
730 * TRUE if the test succeeds
733 * - this is where to add diagnostics. This is called
734 * at the very end of initialization.
741 /* see if we can read/write now */
742 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
743 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
744 DPRINT("Port 0x%x RAP 0x%x CSR0 0x%x RDP 0x%x, Interupt status register is 0x%x\n", Adapter
->PortOffset
, RAP
, CSR0
, RDP
, Data
);
747 for(i
= 0; i
< 6; i
++)
748 NdisRawReadPortUchar(Adapter
->PortOffset
+ i
, &address
[i
]);
750 DPRINT("burned-in address: %x:%x:%x:%x:%x:%x\n", address
[0], address
[1], address
[2], address
[3], address
[4], address
[5]);
751 /* Read status flags from CSR0 */
752 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
753 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
754 DPRINT("CSR0: 0x%x\n", Data
);
756 /* Read status flags from CSR3 */
757 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR3
);
758 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
760 DPRINT("CSR3: 0x%x\n", Data
);
761 /* Read status flags from CSR4 */
762 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR4
);
763 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
764 DPRINT("CSR4: 0x%x\n", Data
);
766 /* Read status flags from CSR5 */
767 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR5
);
768 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
769 DPRINT("CSR5: 0x%x\n", Data
);
771 /* Read status flags from CSR6 */
772 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR6
);
773 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
774 DPRINT("CSR6: 0x%x\n", Data
);
783 OUT PNDIS_STATUS OpenErrorStatus
,
784 OUT PUINT SelectedMediumIndex
,
785 IN PNDIS_MEDIUM MediumArray
,
786 IN UINT MediumArraySize
,
787 IN NDIS_HANDLE MiniportAdapterHandle
,
788 IN NDIS_HANDLE WrapperConfigurationContext
)
790 * FUNCTION: Initialize a new miniport
792 * OpenErrorStatus: pointer to a var to return status info in
793 * SelectedMediumIndex: index of the selected medium (will be NdisMedium802_3)
794 * MediumArray: array of media that we can pick from
795 * MediumArraySize: size of MediumArray
796 * MiniportAdapterHandle: NDIS-assigned handle for this miniport instance
797 * WrapperConfigurationContext: temporary NDIS-assigned handle for passing
798 * to configuration APIs
800 * NDIS_STATUS_SUCCESS on success
801 * NDIS_STATUS_FAILURE on general failure
802 * NDIS_STATUS_UNSUPPORTED_MEDIA on not finding 802_3 in the MediaArray
803 * NDIS_STATUS_RESOURCES on insufficient system resources
804 * NDIS_STATUS_ADAPTER_NOT_FOUND on not finding the adapter
806 * - Called by NDIS at PASSIVE_LEVEL, once per detected card
807 * - Will int 3 on failure of MiTestCard if DBG=1
811 PADAPTER Adapter
= 0;
812 NDIS_STATUS Status
= NDIS_STATUS_FAILURE
;
813 BOOLEAN InterruptRegistered
= FALSE
;
816 for(i
= 0; i
< MediumArraySize
; i
++)
817 if(MediumArray
[i
] == NdisMedium802_3
)
820 if(i
== MediumArraySize
)
822 Status
= NDIS_STATUS_UNSUPPORTED_MEDIA
;
823 DPRINT1("unsupported media\n");
825 *OpenErrorStatus
= Status
;
829 *SelectedMediumIndex
= i
;
831 /* allocate our adapter struct */
832 Status
= NdisAllocateMemoryWithTag((PVOID
*)&Adapter
, sizeof(ADAPTER
), PCNET_TAG
);
833 if(Status
!= NDIS_STATUS_SUCCESS
)
835 Status
= NDIS_STATUS_RESOURCES
;
836 DPRINT1("Insufficient resources\n");
838 *OpenErrorStatus
= Status
;
842 RtlZeroMemory(Adapter
, sizeof(ADAPTER
));
844 Adapter
->MiniportAdapterHandle
= MiniportAdapterHandle
;
846 /* register our adapter structwith ndis */
847 NdisMSetAttributesEx(Adapter
->MiniportAdapterHandle
, Adapter
, 0, NDIS_ATTRIBUTE_BUS_MASTER
, NdisInterfacePci
);
851 /* Card-specific detection and setup */
852 Status
= MiQueryCard(Adapter
);
853 if(Status
!= NDIS_STATUS_SUCCESS
)
855 DPRINT1("MiQueryCard failed\n");
856 Status
= NDIS_STATUS_ADAPTER_NOT_FOUND
;
861 /* register an IO port range */
862 Status
= NdisMRegisterIoPortRange((PVOID
*)&Adapter
->PortOffset
, Adapter
->MiniportAdapterHandle
,
863 (UINT
)Adapter
->IoBaseAddress
, NUMBER_OF_PORTS
);
864 if(Status
!= NDIS_STATUS_SUCCESS
)
866 DPRINT1("NdisMRegisterIoPortRange failed: 0x%x\n", Status
);
871 /* Allocate map registers */
872 Status
= NdisMAllocateMapRegisters(Adapter
->MiniportAdapterHandle
, 0,
873 NDIS_DMA_32BITS
, 8, BUFFER_SIZE
);
874 if(Status
!= NDIS_STATUS_SUCCESS
)
876 DPRINT1("NdisMAllocateMapRegisters failed: 0x%x\n", Status
);
881 /* set up the interrupt */
882 Status
= NdisMRegisterInterrupt(&Adapter
->InterruptObject
, Adapter
->MiniportAdapterHandle
, Adapter
->InterruptVector
,
883 Adapter
->InterruptVector
, TRUE
, TRUE
, NdisInterruptLevelSensitive
);
884 if(Status
!= NDIS_STATUS_SUCCESS
)
886 DPRINT("NdisMRegisterInterrupt failed: 0x%x\n", Status
);
891 InterruptRegistered
= TRUE
;
893 /* Allocate and initialize shared data structures */
894 Status
= MiAllocateSharedMemory(Adapter
);
895 if(Status
!= NDIS_STATUS_SUCCESS
)
897 Status
= NDIS_STATUS_RESOURCES
;
898 DPRINT("MiAllocateSharedMemory failed", Status
);
903 /* set up the initialization block */
904 MiPrepareInitializationBlock(Adapter
);
906 DPRINT("Interrupt registered successfully\n");
908 /* Initialize and start the chip */
911 NdisAllocateSpinLock(&Adapter
->Lock
);
913 Status
= NDIS_STATUS_SUCCESS
;
917 if(Status
!= NDIS_STATUS_SUCCESS
&& Adapter
)
919 DPRINT("Error; freeing stuff\n");
921 NdisMFreeMapRegisters(Adapter
->MiniportAdapterHandle
); /* doesn't hurt to free if we never alloc'd? */
923 if(Adapter
->PortOffset
)
924 NdisMDeregisterIoPortRange(Adapter
->MiniportAdapterHandle
, Adapter
->IoBaseAddress
, NUMBER_OF_PORTS
, (PVOID
)Adapter
->PortOffset
);
926 if(InterruptRegistered
)
927 NdisMDeregisterInterrupt(&Adapter
->InterruptObject
);
929 MiFreeSharedMemory(Adapter
);
931 NdisFreeMemory(Adapter
, 0, 0);
934 if(Status
== NDIS_STATUS_SUCCESS
)
936 NdisMInitializeTimer(&Adapter
->MediaDetectionTimer
,
937 Adapter
->MiniportAdapterHandle
,
938 MiniportMediaDetectionTimer
,
940 NdisMSetPeriodicTimer(&Adapter
->MediaDetectionTimer
,
941 MEDIA_DETECTION_INTERVAL
);
945 if(!MiTestCard(Adapter
))
949 DPRINT("returning 0x%x\n", Status
);
950 *OpenErrorStatus
= Status
;
957 OUT PBOOLEAN InterruptRecognized
,
958 OUT PBOOLEAN QueueMiniportHandleInterrupt
,
959 IN NDIS_HANDLE MiniportAdapterContext
)
961 * FUNCTION: Miniport interrupt service routine
963 * InterruptRecognized: the interrupt was ours
964 * QueueMiniportHandleInterrupt: whether to queue a DPC to handle this interrupt
965 * MiniportAdapterContext: the context originally passed to NdisMSetAttributes
967 * - called by NDIS at DIRQL
968 * - by setting QueueMiniportHandleInterrupt to TRUE, MiniportHandleInterrupt
974 PADAPTER Adapter
= (PADAPTER
)MiniportAdapterContext
;
978 /* save the old RAP value */
979 NdisRawReadPortUshort(Adapter
->PortOffset
+ RAP
, &Rap
);
982 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
983 NdisRawReadPortUshort(Adapter
->PortOffset
+ RDP
, &Data
);
985 if(!(Data
& CSR0_INTR
))
987 DPRINT("not our interrupt.\n");
988 *InterruptRecognized
= FALSE
;
989 *QueueMiniportHandleInterrupt
= FALSE
;
993 DPRINT("detected our interrupt\n");
995 /* disable interrupts */
996 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
997 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, 0);
999 *InterruptRecognized
= TRUE
;
1000 *QueueMiniportHandleInterrupt
= TRUE
;
1003 /* restore the rap */
1004 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, Rap
);
1010 OUT PBOOLEAN AddressingReset
,
1011 IN NDIS_HANDLE MiniportAdapterContext
)
1013 * FUNCTION: Reset the miniport
1015 * AddressingReset: Whether or not we want NDIS to subsequently call MiniportSetInformation
1016 * to reset our addresses and filters
1017 * MiniportAdapterContext: context originally passed to NdisMSetAttributes
1019 * NDIS_STATUS_SUCCESS on all requests
1021 * - Called by NDIS at PASSIVE_LEVEL when it thinks we need a reset
1026 /* MiniportReset doesn't do anything at the moment... perhaps this should be fixed. */
1028 *AddressingReset
= FALSE
;
1029 return NDIS_STATUS_SUCCESS
;
1034 MiSyncStartTransmit(
1035 IN PVOID SynchronizeContext
)
1037 * FUNCTION: Stop the adapter
1039 * SynchronizeContext: Adapter context
1042 PADAPTER Adapter
= (PADAPTER
)SynchronizeContext
;
1043 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, CSR0
);
1044 NdisRawWritePortUshort(Adapter
->PortOffset
+ RDP
, CSR0_IENA
| CSR0_TDMD
);
1051 IN NDIS_HANDLE MiniportAdapterContext
,
1052 IN PNDIS_PACKET Packet
,
1055 * FUNCTION: Called by NDIS when it has a packet for the NIC to send out
1057 * MiniportAdapterContext: context originally input to NdisMSetAttributes
1058 * Packet: The NDIS_PACKET to be sent
1059 * Flags: Flags associated with Packet
1061 * NDIS_STATUS_SUCCESS on processed requests
1062 * NDIS_STATUS_RESOURCES if there's no place in buffer ring
1064 * - Called by NDIS at DISPATCH_LEVEL
1067 PADAPTER Adapter
= (PADAPTER
)MiniportAdapterContext
;
1068 PTRANSMIT_DESCRIPTOR Desc
;
1069 PNDIS_BUFFER NdisBuffer
;
1071 UINT TotalPacketLength
, SourceLength
, Position
= 0;
1075 NdisDprAcquireSpinLock(&Adapter
->Lock
);
1077 /* Check if we have free entry in our circular buffer. */
1078 if ((Adapter
->CurrentTransmitEndIndex
+ 1 ==
1079 Adapter
->CurrentTransmitStartIndex
) ||
1080 (Adapter
->CurrentTransmitEndIndex
== NUMBER_OF_BUFFERS
- 1 &&
1081 Adapter
->CurrentTransmitStartIndex
== 0))
1083 DPRINT1("No free space in circular buffer\n");
1084 NdisDprReleaseSpinLock(&Adapter
->Lock
);
1085 return NDIS_STATUS_RESOURCES
;
1088 Desc
= Adapter
->TransmitDescriptorRingVirt
+ Adapter
->CurrentTransmitEndIndex
;
1090 NdisQueryPacket(Packet
, NULL
, NULL
, &NdisBuffer
, &TotalPacketLength
);
1091 ASSERT(TotalPacketLength
<= BUFFER_SIZE
);
1093 DPRINT("TotalPacketLength: %x\n", TotalPacketLength
);
1097 NdisQueryBuffer(NdisBuffer
, &SourceBuffer
, &SourceLength
);
1099 DPRINT("Buffer: %x Length: %x\n", SourceBuffer
, SourceLength
);
1101 RtlCopyMemory(Adapter
->TransmitBufferPtrVirt
+
1102 Adapter
->CurrentTransmitEndIndex
* BUFFER_SIZE
+ Position
,
1103 SourceBuffer
, SourceLength
);
1105 Position
+= SourceLength
;
1107 NdisGetNextBuffer(NdisBuffer
, &NdisBuffer
);
1112 PUCHAR Ptr
= Adapter
->TransmitBufferPtrVirt
+
1113 Adapter
->CurrentTransmitEndIndex
* BUFFER_SIZE
;
1114 for (Position
= 0; Position
< TotalPacketLength
; Position
++)
1116 if (Position
% 16 == 0)
1118 DbgPrint("%x ", *Ptr
++);
1124 Adapter
->CurrentTransmitEndIndex
++;
1125 Adapter
->CurrentTransmitEndIndex
%= NUMBER_OF_BUFFERS
;
1127 Desc
->FLAGS
= TD1_OWN
| TD1_STP
| TD1_ENP
;
1128 Desc
->BCNT
= 0xf000 | -TotalPacketLength
;
1130 NdisMSynchronizeWithInterrupt(&Adapter
->InterruptObject
, MiSyncStartTransmit
, Adapter
);
1132 NdisDprReleaseSpinLock(&Adapter
->Lock
);
1134 return NDIS_STATUS_SUCCESS
;
1139 MiEthernetCrc(UCHAR
*Address
)
1141 * FUNCTION: Calculate Ethernet CRC32
1143 * Address: 6-byte ethernet address
1145 * The calculated CRC32 value.
1148 UINT Counter
, Length
;
1151 for (Length
= 0; Length
< 6; Length
++)
1153 Value
^= *Address
++;
1154 for (Counter
= 0; Counter
< 8; Counter
++)
1157 Value
^= (Value
& 1) * 0xedb88320;
1174 NdisZeroMemory(Adapter
->InitializationBlockVirt
->LADR
, 8);
1175 for (Index
= 0; Index
< AddressCount
; Index
++)
1177 CrcIndex
= MiEthernetCrc(Addresses
) >> 26;
1178 Adapter
->InitializationBlockVirt
->LADR
[CrcIndex
>> 3] |= 1 << (CrcIndex
& 15);
1182 /* FIXME: The specification mentions we need to reload the init block here. */
1184 return NDIS_STATUS_SUCCESS
;
1189 MiGetMediaState(PADAPTER Adapter
)
1191 * FUNCTION: Determine the link state
1193 * Adapter: Adapter context
1195 * NdisMediaStateConnected if the cable is connected
1196 * NdisMediaStateDisconnected if the cable is disconnected
1200 NdisRawWritePortUshort(Adapter
->PortOffset
+ RAP
, BCR4
);
1201 NdisRawReadPortUshort(Adapter
->PortOffset
+ BDP
, &Data
);
1202 return Data
& BCR4_LEDOUT
? NdisMediaStateConnected
: NdisMediaStateDisconnected
;
1208 IN PDRIVER_OBJECT DriverObject
,
1209 IN PUNICODE_STRING RegistryPath
)
1211 * FUNCTION: Start this driver
1213 * DriverObject: Pointer to the system-allocated driver object
1214 * RegistryPath: Pointer to our SCM database entry
1216 * NDIS_STATUS_SUCCESS on success
1217 * NDIS_STATUS_FAILURE on failure
1219 * - Called by the I/O manager when the driver starts at PASSIVE_LEVEL
1220 * - TODO: convert this to NTSTATUS return values
1223 NDIS_HANDLE WrapperHandle
;
1224 NDIS_MINIPORT_CHARACTERISTICS Characteristics
;
1227 RtlZeroMemory(&Characteristics
, sizeof(Characteristics
));
1228 Characteristics
.MajorNdisVersion
= NDIS_MINIPORT_MAJOR_VERSION
;
1229 Characteristics
.MinorNdisVersion
= NDIS_MINIPORT_MINOR_VERSION
;
1230 Characteristics
.HaltHandler
= MiniportHalt
;
1231 Characteristics
.HandleInterruptHandler
= MiniportHandleInterrupt
;
1232 Characteristics
.InitializeHandler
= MiniportInitialize
;
1233 Characteristics
.ISRHandler
= MiniportISR
;
1234 Characteristics
.QueryInformationHandler
= MiniportQueryInformation
;
1235 Characteristics
.ResetHandler
= MiniportReset
;
1236 Characteristics
.SetInformationHandler
= MiniportSetInformation
;
1237 Characteristics
.SendHandler
= MiniportSend
;
1239 NdisMInitializeWrapper(&WrapperHandle
, DriverObject
, RegistryPath
, 0);
1241 Status
= NdisMRegisterMiniport(WrapperHandle
, &Characteristics
, sizeof(Characteristics
));
1242 if(Status
!= NDIS_STATUS_SUCCESS
)
1244 NdisTerminateWrapper(WrapperHandle
, 0);
1245 return NDIS_STATUS_FAILURE
;
1248 return NDIS_STATUS_SUCCESS
;