am3eth.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. am3eth.c
  5. Abstract:
  6. This module implements the CPSW Ethernet Controller on TI AM335x SoCs.
  7. Author:
  8. Evan Green 20-Mar-2015
  9. Environment:
  10. Kernel
  11. --*/
  12. //
  13. // ------------------------------------------------------------------- Includes
  14. //
  15. #include <minoca/kernel/driver.h>
  16. #include <minoca/net/netdrv.h>
  17. #include "am3eth.h"
  18. //
  19. // ---------------------------------------------------------------- Definitions
  20. //
  21. //
  22. // ------------------------------------------------------ Data Type Definitions
  23. //
  24. //
  25. // ----------------------------------------------- Internal Function Prototypes
  26. //
  27. KSTATUS
  28. A3eAddDevice (
  29. PVOID Driver,
  30. PSTR DeviceId,
  31. PSTR ClassId,
  32. PSTR CompatibleIds,
  33. PVOID DeviceToken
  34. );
  35. VOID
  36. A3eDispatchStateChange (
  37. PIRP Irp,
  38. PVOID DeviceContext,
  39. PVOID IrpContext
  40. );
  41. VOID
  42. A3eDispatchOpen (
  43. PIRP Irp,
  44. PVOID DeviceContext,
  45. PVOID IrpContext
  46. );
  47. VOID
  48. A3eDispatchClose (
  49. PIRP Irp,
  50. PVOID DeviceContext,
  51. PVOID IrpContext
  52. );
  53. VOID
  54. A3eDispatchIo (
  55. PIRP Irp,
  56. PVOID DeviceContext,
  57. PVOID IrpContext
  58. );
  59. VOID
  60. A3eDispatchSystemControl (
  61. PIRP Irp,
  62. PVOID DeviceContext,
  63. PVOID IrpContext
  64. );
  65. VOID
  66. A3eDestroyLink (
  67. PVOID DeviceContext
  68. );
  69. KSTATUS
  70. A3epProcessResourceRequirements (
  71. PIRP Irp
  72. );
  73. KSTATUS
  74. A3epStartDevice (
  75. PIRP Irp,
  76. PA3E_DEVICE Device
  77. );
  78. //
  79. // -------------------------------------------------------------------- Globals
  80. //
  81. PDRIVER A3eDriver = NULL;
  82. //
  83. // ------------------------------------------------------------------ Functions
  84. //
  85. KSTATUS
  86. DriverEntry (
  87. PDRIVER Driver
  88. )
  89. /*++
  90. Routine Description:
  91. This routine is the entry point for the e100 driver. It registers its other
  92. dispatch functions, and performs driver-wide initialization.
  93. Arguments:
  94. Driver - Supplies a pointer to the driver object.
  95. Return Value:
  96. STATUS_SUCCESS on success.
  97. Failure code on error.
  98. --*/
  99. {
  100. DRIVER_FUNCTION_TABLE FunctionTable;
  101. KSTATUS Status;
  102. A3eDriver = Driver;
  103. RtlZeroMemory(&FunctionTable, sizeof(DRIVER_FUNCTION_TABLE));
  104. FunctionTable.Version = DRIVER_FUNCTION_TABLE_VERSION;
  105. FunctionTable.AddDevice = A3eAddDevice;
  106. FunctionTable.DispatchStateChange = A3eDispatchStateChange;
  107. FunctionTable.DispatchOpen = A3eDispatchOpen;
  108. FunctionTable.DispatchClose = A3eDispatchClose;
  109. FunctionTable.DispatchIo = A3eDispatchIo;
  110. FunctionTable.DispatchSystemControl = A3eDispatchSystemControl;
  111. Status = IoRegisterDriverFunctions(Driver, &FunctionTable);
  112. return Status;
  113. }
  114. KSTATUS
  115. A3eAddDevice (
  116. PVOID Driver,
  117. PSTR DeviceId,
  118. PSTR ClassId,
  119. PSTR CompatibleIds,
  120. PVOID DeviceToken
  121. )
  122. /*++
  123. Routine Description:
  124. This routine is called when a device is detected for which the e100 driver
  125. acts as the function driver. The driver will attach itself to the stack.
  126. Arguments:
  127. Driver - Supplies a pointer to the driver being called.
  128. DeviceId - Supplies a pointer to a string with the device ID.
  129. ClassId - Supplies a pointer to a string containing the device's class ID.
  130. CompatibleIds - Supplies a pointer to a string containing device IDs
  131. that would be compatible with this device.
  132. DeviceToken - Supplies an opaque token that the driver can use to identify
  133. the device in the system. This token should be used when attaching to
  134. the stack.
  135. Return Value:
  136. STATUS_SUCCESS on success.
  137. Failure code if the driver was unsuccessful in attaching itself.
  138. --*/
  139. {
  140. PA3E_DEVICE Device;
  141. KSTATUS Status;
  142. Device = MmAllocateNonPagedPool(sizeof(A3E_DEVICE), A3E_ALLOCATION_TAG);
  143. if (Device == NULL) {
  144. Status = STATUS_INSUFFICIENT_RESOURCES;
  145. goto AddDeviceEnd;
  146. }
  147. RtlZeroMemory(Device, sizeof(A3E_DEVICE));
  148. Device->TxInterruptHandle = INVALID_HANDLE;
  149. Device->RxInterruptHandle = INVALID_HANDLE;
  150. Device->OsDevice = DeviceToken;
  151. Status = IoAttachDriverToDevice(Driver, DeviceToken, Device);
  152. if (!KSUCCESS(Status)) {
  153. goto AddDeviceEnd;
  154. }
  155. AddDeviceEnd:
  156. if (!KSUCCESS(Status)) {
  157. if (Device != NULL) {
  158. MmFreeNonPagedPool(Device);
  159. Device = NULL;
  160. }
  161. }
  162. return Status;
  163. }
  164. VOID
  165. A3eDispatchStateChange (
  166. PIRP Irp,
  167. PVOID DeviceContext,
  168. PVOID IrpContext
  169. )
  170. /*++
  171. Routine Description:
  172. This routine handles State Change IRPs.
  173. Arguments:
  174. Irp - Supplies a pointer to the I/O request packet.
  175. DeviceContext - Supplies the context pointer supplied by the driver when it
  176. attached itself to the driver stack. Presumably this pointer contains
  177. driver-specific device context.
  178. IrpContext - Supplies the context pointer supplied by the driver when
  179. the IRP was created.
  180. Return Value:
  181. None.
  182. --*/
  183. {
  184. KSTATUS Status;
  185. ASSERT(Irp->MajorCode == IrpMajorStateChange);
  186. if (Irp->Direction == IrpUp) {
  187. switch (Irp->MinorCode) {
  188. case IrpMinorQueryResources:
  189. Status = A3epProcessResourceRequirements(Irp);
  190. if (!KSUCCESS(Status)) {
  191. IoCompleteIrp(A3eDriver, Irp, Status);
  192. }
  193. break;
  194. case IrpMinorStartDevice:
  195. Status = A3epStartDevice(Irp, DeviceContext);
  196. if (!KSUCCESS(Status)) {
  197. IoCompleteIrp(A3eDriver, Irp, Status);
  198. }
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. return;
  205. }
  206. VOID
  207. A3eDispatchOpen (
  208. PIRP Irp,
  209. PVOID DeviceContext,
  210. PVOID IrpContext
  211. )
  212. /*++
  213. Routine Description:
  214. This routine handles Open IRPs.
  215. Arguments:
  216. Irp - Supplies a pointer to the I/O request packet.
  217. DeviceContext - Supplies the context pointer supplied by the driver when it
  218. attached itself to the driver stack. Presumably this pointer contains
  219. driver-specific device context.
  220. IrpContext - Supplies the context pointer supplied by the driver when
  221. the IRP was created.
  222. Return Value:
  223. None.
  224. --*/
  225. {
  226. return;
  227. }
  228. VOID
  229. A3eDispatchClose (
  230. PIRP Irp,
  231. PVOID DeviceContext,
  232. PVOID IrpContext
  233. )
  234. /*++
  235. Routine Description:
  236. This routine handles Close IRPs.
  237. Arguments:
  238. Irp - Supplies a pointer to the I/O request packet.
  239. DeviceContext - Supplies the context pointer supplied by the driver when it
  240. attached itself to the driver stack. Presumably this pointer contains
  241. driver-specific device context.
  242. IrpContext - Supplies the context pointer supplied by the driver when
  243. the IRP was created.
  244. Return Value:
  245. None.
  246. --*/
  247. {
  248. return;
  249. }
  250. VOID
  251. A3eDispatchIo (
  252. PIRP Irp,
  253. PVOID DeviceContext,
  254. PVOID IrpContext
  255. )
  256. /*++
  257. Routine Description:
  258. This routine handles I/O IRPs.
  259. Arguments:
  260. Irp - Supplies a pointer to the I/O request packet.
  261. DeviceContext - Supplies the context pointer supplied by the driver when it
  262. attached itself to the driver stack. Presumably this pointer contains
  263. driver-specific device context.
  264. IrpContext - Supplies the context pointer supplied by the driver when
  265. the IRP was created.
  266. Return Value:
  267. None.
  268. --*/
  269. {
  270. return;
  271. }
  272. VOID
  273. A3eDispatchSystemControl (
  274. PIRP Irp,
  275. PVOID DeviceContext,
  276. PVOID IrpContext
  277. )
  278. /*++
  279. Routine Description:
  280. This routine handles System Control IRPs.
  281. Arguments:
  282. Irp - Supplies a pointer to the I/O request packet.
  283. DeviceContext - Supplies the context pointer supplied by the driver when it
  284. attached itself to the driver stack. Presumably this pointer contains
  285. driver-specific device context.
  286. IrpContext - Supplies the context pointer supplied by the driver when
  287. the IRP was created.
  288. Return Value:
  289. None.
  290. --*/
  291. {
  292. PA3E_DEVICE Device;
  293. PSYSTEM_CONTROL_DEVICE_INFORMATION DeviceInformationRequest;
  294. KSTATUS Status;
  295. ASSERT(Irp->MajorCode == IrpMajorSystemControl);
  296. Device = DeviceContext;
  297. if (Irp->Direction == IrpDown) {
  298. switch (Irp->MinorCode) {
  299. case IrpMinorSystemControlDeviceInformation:
  300. DeviceInformationRequest = Irp->U.SystemControl.SystemContext;
  301. Status = NetGetSetLinkDeviceInformation(
  302. Device->NetworkLink,
  303. &(DeviceInformationRequest->Uuid),
  304. DeviceInformationRequest->Data,
  305. &(DeviceInformationRequest->DataSize),
  306. DeviceInformationRequest->Set);
  307. IoCompleteIrp(A3eDriver, Irp, Status);
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. return;
  314. }
  315. KSTATUS
  316. A3epAddNetworkDevice (
  317. PA3E_DEVICE Device
  318. )
  319. /*++
  320. Routine Description:
  321. This routine adds the device to core networking's available links.
  322. Arguments:
  323. Device - Supplies a pointer to the device to add.
  324. Return Value:
  325. Status code.
  326. --*/
  327. {
  328. PNET_PACKET_SIZE_INFORMATION PacketSizeInformation;
  329. NET_LINK_PROPERTIES Properties;
  330. KSTATUS Status;
  331. if (Device->NetworkLink != NULL) {
  332. Status = STATUS_SUCCESS;
  333. goto AddNetworkDeviceEnd;
  334. }
  335. //
  336. // Add a link to the core networking library.
  337. //
  338. RtlZeroMemory(&Properties, sizeof(NET_LINK_PROPERTIES));
  339. Properties.Version = NET_LINK_PROPERTIES_VERSION;
  340. Properties.TransmitAlignment = Device->DataAlignment;
  341. Properties.Device = Device->OsDevice;
  342. Properties.DeviceContext = Device;
  343. PacketSizeInformation = &(Properties.PacketSizeInformation);
  344. PacketSizeInformation->MaxPacketSize = Device->ReceiveFrameDataSize;
  345. PacketSizeInformation->MinPacketSize = A3E_TRANSMIT_MINIMUM_PACKET_SIZE;
  346. Properties.DataLinkType = NetDomainEthernet;
  347. Properties.MaxPhysicalAddress = MAX_ULONG;
  348. Properties.PhysicalAddress.Domain = NetDomainEthernet;
  349. ASSERT(Device->MacAddressAssigned != FALSE);
  350. RtlCopyMemory(&(Properties.PhysicalAddress.Address),
  351. &(Device->MacAddress),
  352. sizeof(Device->MacAddress));
  353. Properties.Interface.Send = A3eSend;
  354. Properties.Interface.GetSetInformation = A3eGetSetInformation;
  355. Properties.Interface.DestroyLink = A3eDestroyLink;
  356. Status = NetAddLink(&Properties, &(Device->NetworkLink));
  357. if (!KSUCCESS(Status)) {
  358. goto AddNetworkDeviceEnd;
  359. }
  360. AddNetworkDeviceEnd:
  361. if (!KSUCCESS(Status)) {
  362. if (Device->NetworkLink != NULL) {
  363. NetRemoveLink(Device->NetworkLink);
  364. Device->NetworkLink = NULL;
  365. }
  366. }
  367. return Status;
  368. }
  369. VOID
  370. A3eDestroyLink (
  371. PVOID DeviceContext
  372. )
  373. /*++
  374. Routine Description:
  375. This routine notifies the device layer that the networking core is in the
  376. process of destroying the link and will no longer call into the device for
  377. this link. This allows the device layer to release any context that was
  378. supporting the device link interface.
  379. Arguments:
  380. DeviceContext - Supplies a pointer to the device context associated with
  381. the link being destroyed.
  382. Return Value:
  383. None.
  384. --*/
  385. {
  386. return;
  387. }
  388. //
  389. // --------------------------------------------------------- Internal Functions
  390. //
  391. KSTATUS
  392. A3epProcessResourceRequirements (
  393. PIRP Irp
  394. )
  395. /*++
  396. Routine Description:
  397. This routine filters through the resource requirements presented by the
  398. bus for an e100 LAN controller. It adds an interrupt vector requirement for
  399. any interrupt line requested.
  400. Arguments:
  401. Irp - Supplies a pointer to the I/O request packet.
  402. Return Value:
  403. Status code.
  404. --*/
  405. {
  406. PRESOURCE_CONFIGURATION_LIST Requirements;
  407. KSTATUS Status;
  408. RESOURCE_REQUIREMENT VectorRequirement;
  409. ASSERT((Irp->MajorCode == IrpMajorStateChange) &&
  410. (Irp->MinorCode == IrpMinorQueryResources));
  411. //
  412. // Initialize a nice interrupt vector requirement in preparation.
  413. //
  414. RtlZeroMemory(&VectorRequirement, sizeof(RESOURCE_REQUIREMENT));
  415. VectorRequirement.Type = ResourceTypeInterruptVector;
  416. VectorRequirement.Minimum = 0;
  417. VectorRequirement.Maximum = -1;
  418. VectorRequirement.Length = 1;
  419. //
  420. // Loop through all configuration lists, creating a vector for each line.
  421. //
  422. Requirements = Irp->U.QueryResources.ResourceRequirements;
  423. Status = IoCreateAndAddInterruptVectorsForLines(Requirements,
  424. &VectorRequirement);
  425. if (!KSUCCESS(Status)) {
  426. goto ProcessResourceRequirementsEnd;
  427. }
  428. ProcessResourceRequirementsEnd:
  429. return Status;
  430. }
  431. KSTATUS
  432. A3epStartDevice (
  433. PIRP Irp,
  434. PA3E_DEVICE Device
  435. )
  436. /*++
  437. Routine Description:
  438. This routine starts the TI AM335x CPSW Ethernet device.
  439. Arguments:
  440. Irp - Supplies a pointer to the start IRP.
  441. Device - Supplies a pointer to the device information.
  442. Return Value:
  443. Status code.
  444. --*/
  445. {
  446. ULONG AlignmentOffset;
  447. PRESOURCE_ALLOCATION Allocation;
  448. PRESOURCE_ALLOCATION_LIST AllocationList;
  449. IO_CONNECT_INTERRUPT_PARAMETERS Connect;
  450. PRESOURCE_ALLOCATION ControllerBase;
  451. PHYSICAL_ADDRESS EndAddress;
  452. HANDLE InterruptHandles[2];
  453. PRESOURCE_ALLOCATION LineAllocation;
  454. ULONG PageSize;
  455. PHYSICAL_ADDRESS PhysicalAddress;
  456. ULONG Size;
  457. KSTATUS Status;
  458. ControllerBase = NULL;
  459. //
  460. // Loop through the allocated resources to get the controller base and the
  461. // interrupt.
  462. //
  463. AllocationList = Irp->U.StartDevice.ProcessorLocalResources;
  464. Allocation = IoGetNextResourceAllocation(AllocationList, NULL);
  465. while (Allocation != NULL) {
  466. //
  467. // If the resource is an interrupt vector, then it should have an
  468. // owning interrupt line allocation.
  469. //
  470. if (Allocation->Type == ResourceTypeInterruptVector) {
  471. ASSERT(Allocation->OwningAllocation != NULL);
  472. //
  473. // Save the line and vector number.
  474. //
  475. LineAllocation = Allocation->OwningAllocation;
  476. if (Device->InterruptResourcesFound == 0) {
  477. Device->TxInterruptLine = LineAllocation->Allocation;
  478. Device->TxInterruptVector = Allocation->Allocation;
  479. Device->InterruptResourcesFound += 1;
  480. } else if (Device->InterruptResourcesFound == 1) {
  481. Device->RxInterruptLine = LineAllocation->Allocation;
  482. Device->RxInterruptVector = Allocation->Allocation;
  483. Device->InterruptResourcesFound += 1;
  484. }
  485. //
  486. // Look for the first physical address reservation, the registers.
  487. //
  488. } else if (Allocation->Type == ResourceTypePhysicalAddressSpace) {
  489. if (ControllerBase == NULL) {
  490. ControllerBase = Allocation;
  491. }
  492. }
  493. //
  494. // Get the next allocation in the list.
  495. //
  496. Allocation = IoGetNextResourceAllocation(AllocationList, Allocation);
  497. }
  498. //
  499. // Fail to start if the controller base was not found.
  500. //
  501. if (ControllerBase == NULL) {
  502. Status = STATUS_INVALID_CONFIGURATION;
  503. goto StartDeviceEnd;
  504. }
  505. //
  506. // Map the controller.
  507. //
  508. if (Device->ControllerBase == NULL) {
  509. //
  510. // Page align the mapping request.
  511. //
  512. PageSize = MmPageSize();
  513. PhysicalAddress = ControllerBase->Allocation;
  514. EndAddress = PhysicalAddress + ControllerBase->Length;
  515. ASSERT(ControllerBase->Length >= A3E_REGISTERS_SIZE);
  516. PhysicalAddress = ALIGN_RANGE_DOWN(PhysicalAddress, PageSize);
  517. AlignmentOffset = ControllerBase->Allocation - PhysicalAddress;
  518. EndAddress = ALIGN_RANGE_UP(EndAddress, PageSize);
  519. Size = (ULONG)(EndAddress - PhysicalAddress);
  520. Device->ControllerBase = MmMapPhysicalAddress(PhysicalAddress,
  521. Size,
  522. TRUE,
  523. FALSE,
  524. TRUE);
  525. if (Device->ControllerBase == NULL) {
  526. Status = STATUS_NO_MEMORY;
  527. goto StartDeviceEnd;
  528. }
  529. Device->ControllerBase += AlignmentOffset;
  530. Device->ControllerBasePhysical = ControllerBase->Allocation;
  531. }
  532. ASSERT(Device->ControllerBase != NULL);
  533. //
  534. // Allocate the controller structures.
  535. //
  536. Status = A3epInitializeDeviceStructures(Device);
  537. if (!KSUCCESS(Status)) {
  538. goto StartDeviceEnd;
  539. }
  540. if (Device->InterruptResourcesFound != 2) {
  541. RtlDebugPrint("A3E: Missing interrupt resources\n");
  542. Status = STATUS_NOT_READY;
  543. goto StartDeviceEnd;
  544. }
  545. //
  546. // Attempt to connect the interrupts.
  547. //
  548. Device->InterruptRunLevel = RunLevelMaxDevice;
  549. ASSERT(Device->TxInterruptHandle == INVALID_HANDLE);
  550. RtlZeroMemory(&Connect, sizeof(IO_CONNECT_INTERRUPT_PARAMETERS));
  551. Connect.Version = IO_CONNECT_INTERRUPT_PARAMETERS_VERSION;
  552. Connect.Device = Irp->Device;
  553. Connect.LineNumber = Device->TxInterruptLine;
  554. Connect.Vector = Device->TxInterruptVector;
  555. Connect.InterruptServiceRoutine = A3epTxInterruptService;
  556. Connect.LowLevelServiceRoutine = A3epInterruptServiceWorker;
  557. Connect.Context = Device;
  558. Connect.Interrupt = &(Device->TxInterruptHandle);
  559. Status = IoConnectInterrupt(&Connect);
  560. if (!KSUCCESS(Status)) {
  561. goto StartDeviceEnd;
  562. }
  563. ASSERT(Device->RxInterruptHandle == INVALID_HANDLE);
  564. Connect.LineNumber = Device->RxInterruptLine;
  565. Connect.Vector = Device->RxInterruptVector;
  566. Connect.InterruptServiceRoutine = A3epRxInterruptService;
  567. Connect.Interrupt = &(Device->RxInterruptHandle);
  568. Status = IoConnectInterrupt(&Connect);
  569. if (!KSUCCESS(Status)) {
  570. goto StartDeviceEnd;
  571. }
  572. InterruptHandles[0] = Device->TxInterruptHandle;
  573. InterruptHandles[1] = Device->RxInterruptHandle;
  574. Device->InterruptRunLevel = IoGetInterruptRunLevel(InterruptHandles, 2);
  575. //
  576. // Start up the controller.
  577. //
  578. Status = A3epResetDevice(Device);
  579. if (!KSUCCESS(Status)) {
  580. goto StartDeviceEnd;
  581. }
  582. ASSERT(Device->NetworkLink != NULL);
  583. StartDeviceEnd:
  584. return Status;
  585. }