OemNicLib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /** @file
  2. *
  3. * Copyright (c) 2018, Hisilicon Limited. All rights reserved.
  4. * Copyright (c) 2017, Linaro Limited. All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause-Patent
  7. *
  8. **/
  9. #include <Uefi.h>
  10. #include <Library/CpldIoLib.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/I2CLib.h>
  13. #include <Library/IoLib.h>
  14. #include <Library/OemNicLib.h>
  15. #define CPU2_SFP2_100G_CARD_OFFSET 0x25
  16. #define SOCKET1_NET_PORT_100G 1
  17. #define SOCKET0_NET_PORT_NUM 4
  18. #define SOCKET1_NET_PORT_NUM 2
  19. #define CARD_PRESENT_100G (BIT7)
  20. #define EEPROM_I2C_PORT 4
  21. #define EEPROM_PAGE_SIZE 0x40
  22. #define MAC_ADDR_LEN 6
  23. #define I2C_OFFSET_EEPROM_ETH0 (0xc00)
  24. #define I2C_SLAVEADDR_EEPROM (0x52)
  25. #define SRAM_NIC_NCL1_OFFSET_ADDRESS 0xA0E87FE0
  26. #define SRAM_NIC_NCL2_OFFSET_ADDRESS 0xA0E87FE4
  27. #pragma pack(1)
  28. typedef struct {
  29. UINT16 Crc16;
  30. UINT16 MacLen;
  31. UINT8 Mac[MAC_ADDR_LEN];
  32. } NIC_MAC_ADDRESS;
  33. #pragma pack()
  34. ETH_PRODUCT_DESC gEthPdtDesc[ETH_MAX_PORT] =
  35. {
  36. {TRUE, ETH_SPEED_10KM, ETH_FULL_DUPLEX, ETH_INVALID, ETH_INVALID},
  37. {TRUE, ETH_SPEED_10KM, ETH_FULL_DUPLEX, ETH_INVALID, ETH_INVALID},
  38. {FALSE, ETH_INVALID, ETH_INVALID, ETH_INVALID, ETH_INVALID},
  39. {FALSE, ETH_INVALID, ETH_INVALID, ETH_INVALID, ETH_INVALID},
  40. {TRUE, ETH_SPEED_1000M, ETH_FULL_DUPLEX, ETH_PHY_MVL88E1512_ID, 0},
  41. {TRUE, ETH_SPEED_1000M, ETH_FULL_DUPLEX, ETH_PHY_MVL88E1512_ID, 1},
  42. {FALSE, ETH_INVALID, ETH_INVALID, ETH_INVALID, ETH_INVALID},
  43. {FALSE, ETH_INVALID, ETH_INVALID, ETH_INVALID, ETH_INVALID}
  44. };
  45. UINT16 CrcTable16[256] = {
  46. 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
  47. 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
  48. 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
  49. 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
  50. 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
  51. 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
  52. 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
  53. 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
  54. 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
  55. 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
  56. 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
  57. 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
  58. 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
  59. 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
  60. 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
  61. 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
  62. 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
  63. 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
  64. 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
  65. 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
  66. 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
  67. 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  68. 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
  69. 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
  70. 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
  71. 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
  72. 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
  73. 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
  74. 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
  75. 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
  76. 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
  77. 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0,
  78. };
  79. UINT16 MakeCrcCheckSum (
  80. UINT8 *Buffer,
  81. UINT32 Length
  82. )
  83. {
  84. UINT16 StartCRC = 0;
  85. if (Length > SIZE_512KB) {
  86. return 0;
  87. }
  88. if (Buffer == NULL) {
  89. return 0;
  90. }
  91. while (Length) {
  92. StartCRC = CrcTable16 [((UINT8) ((StartCRC >> 8) & 0xff)) ^ *(Buffer++)] ^
  93. ((UINT16) (StartCRC << 8));
  94. Length--;
  95. }
  96. return StartCRC;
  97. }
  98. EFI_STATUS
  99. OemGetMacE2prom(
  100. IN UINT32 Port,
  101. OUT UINT8 *Addr
  102. )
  103. {
  104. I2C_DEVICE I2cDev = {0};
  105. EFI_STATUS Status;
  106. UINT16 I2cOffset;
  107. UINT16 Crc16;
  108. NIC_MAC_ADDRESS MacDesc = {0};
  109. UINT16 RemainderMacOffset;
  110. UINT16 LessSizeOfPage;
  111. UINT32 I = 0;
  112. Status = I2CInit (0, EEPROM_I2C_PORT, Normal);
  113. if (EFI_ERROR (Status))
  114. {
  115. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Call I2CInit failed! p1=0x%x.\n",
  116. __FUNCTION__, __LINE__, Status));
  117. return Status;
  118. }
  119. I2cOffset = I2C_OFFSET_EEPROM_ETH0 + (Port * sizeof (NIC_MAC_ADDRESS));
  120. I2cDev.DeviceType = DEVICE_TYPE_E2PROM;
  121. I2cDev.Port = EEPROM_I2C_PORT;
  122. I2cDev.SlaveDeviceAddress = I2C_SLAVEADDR_EEPROM;
  123. I2cDev.Socket = 0;
  124. RemainderMacOffset = I2cOffset % EEPROM_PAGE_SIZE;
  125. LessSizeOfPage = EEPROM_PAGE_SIZE - RemainderMacOffset;
  126. //The length of NIC_MAC_ADDRESS is 10 bytes long,
  127. //It surly less than EEPROM page size, so we could
  128. //code as below, check the address whether across the page boundary,
  129. //and split the data when across page boundary.
  130. if (sizeof (NIC_MAC_ADDRESS) <= LessSizeOfPage) {
  131. Status = I2CRead (&I2cDev, I2cOffset, sizeof (NIC_MAC_ADDRESS), (UINT8 *) &MacDesc);
  132. } else {
  133. Status = I2CRead (&I2cDev, I2cOffset, LessSizeOfPage, (UINT8 *) &MacDesc);
  134. if (!EFI_ERROR (Status)) {
  135. Status |= I2CRead (
  136. &I2cDev,
  137. I2cOffset + LessSizeOfPage,
  138. sizeof (NIC_MAC_ADDRESS) - LessSizeOfPage,
  139. (UINT8 *) &MacDesc + LessSizeOfPage
  140. );
  141. }
  142. }
  143. if (EFI_ERROR (Status)) {
  144. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Call I2cRead failed! p1=0x%x.\n",
  145. __FUNCTION__, __LINE__, Status));
  146. return Status;
  147. }
  148. Crc16 = MakeCrcCheckSum (
  149. (UINT8 *)&(MacDesc.MacLen),
  150. sizeof (MacDesc.MacLen) + sizeof (MacDesc.Mac)
  151. );
  152. if ((Crc16 != MacDesc.Crc16) || (Crc16 == 0)) {
  153. return EFI_NOT_FOUND;
  154. }
  155. for (I = 0; I < MAC_ADDR_LEN; I++) {
  156. Addr[I] = MacDesc.Mac[I];
  157. }
  158. return EFI_SUCCESS;
  159. }
  160. EFI_STATUS
  161. OemSetMacE2prom (
  162. IN UINT32 Port,
  163. IN UINT8 *Addr
  164. )
  165. {
  166. I2C_DEVICE I2cDev = {0};
  167. EFI_STATUS Status;
  168. UINT16 I2cOffset;
  169. NIC_MAC_ADDRESS MacDesc = {0};
  170. UINT32 I;
  171. UINT16 RemainderMacOffset;
  172. UINT16 LessSizeOfPage;
  173. I = 0;
  174. MacDesc.MacLen = MAC_ADDR_LEN;
  175. for (I = 0; I < MAC_ADDR_LEN; I++) {
  176. MacDesc.Mac[I] = Addr[I];
  177. }
  178. MacDesc.Crc16 = MakeCrcCheckSum (
  179. (UINT8 *)&(MacDesc.MacLen),
  180. sizeof (MacDesc.MacLen) + MAC_ADDR_LEN
  181. );
  182. Status = I2CInit (0, EEPROM_I2C_PORT, Normal);
  183. if (EFI_ERROR (Status)) {
  184. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Call I2CInit failed! p1=0x%x.\n",
  185. __FUNCTION__, __LINE__, Status));
  186. return Status;
  187. }
  188. I2cOffset = I2C_OFFSET_EEPROM_ETH0 + (Port * sizeof (NIC_MAC_ADDRESS));
  189. I2cDev.DeviceType = DEVICE_TYPE_E2PROM;
  190. I2cDev.Port = EEPROM_I2C_PORT;
  191. I2cDev.SlaveDeviceAddress = I2C_SLAVEADDR_EEPROM;
  192. I2cDev.Socket = 0;
  193. RemainderMacOffset = I2cOffset % EEPROM_PAGE_SIZE;
  194. LessSizeOfPage = EEPROM_PAGE_SIZE - RemainderMacOffset;
  195. //The length of NIC_MAC_ADDRESS is 10 bytes long,
  196. //It surly less than EEPROM page size, so we could
  197. //code as below, check the address whether across the page boundary,
  198. //and split the data when across page boundary.
  199. if (sizeof (NIC_MAC_ADDRESS) <= LessSizeOfPage) {
  200. Status = I2CWrite (
  201. &I2cDev,
  202. I2cOffset,
  203. sizeof (NIC_MAC_ADDRESS),
  204. (UINT8 *) &MacDesc
  205. );
  206. } else {
  207. Status = I2CWrite (&I2cDev, I2cOffset, LessSizeOfPage, (UINT8 *) &MacDesc);
  208. if (!EFI_ERROR (Status)) {
  209. Status |= I2CWrite (
  210. &I2cDev,
  211. I2cOffset + LessSizeOfPage,
  212. sizeof (NIC_MAC_ADDRESS) - LessSizeOfPage,
  213. (UINT8 *) &MacDesc + LessSizeOfPage
  214. );
  215. }
  216. }
  217. if (EFI_ERROR (Status)) {
  218. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Call I2cWrite failed! p1=0x%x.\n",
  219. __FUNCTION__, __LINE__, Status));
  220. return Status;
  221. }
  222. return EFI_SUCCESS;
  223. }
  224. EFI_STATUS
  225. EFIAPI
  226. OemGetMac (
  227. IN OUT EFI_MAC_ADDRESS *Mac,
  228. IN UINTN Port
  229. )
  230. {
  231. EFI_STATUS Status;
  232. if (Mac == NULL) {
  233. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Mac buffer is null!\n",
  234. __FUNCTION__, __LINE__));
  235. return EFI_INVALID_PARAMETER;
  236. }
  237. Status = OemGetMacE2prom (Port, Mac->Addr);
  238. if (EFI_ERROR (Status)) {
  239. DEBUG ((DEBUG_ERROR,
  240. "[%a]:[%dL] Cannot get MAC from EEPROM, Status: %r; using default MAC.\n",
  241. __FUNCTION__, __LINE__, Status));
  242. Mac->Addr[0] = 0xFF;
  243. Mac->Addr[1] = 0xFF;
  244. Mac->Addr[2] = 0xFF;
  245. Mac->Addr[3] = 0xFF;
  246. Mac->Addr[4] = 0xFF;
  247. Mac->Addr[5] = 0xFF;
  248. return EFI_SUCCESS;
  249. }
  250. return EFI_SUCCESS;
  251. }
  252. EFI_STATUS
  253. EFIAPI
  254. OemSetMac (
  255. IN EFI_MAC_ADDRESS *Mac,
  256. IN UINTN Port
  257. )
  258. {
  259. EFI_STATUS Status;
  260. if (Mac == NULL) {
  261. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Mac buffer is null!\n",
  262. __FUNCTION__, __LINE__));
  263. return EFI_INVALID_PARAMETER;
  264. }
  265. Status = OemSetMacE2prom (Port, Mac->Addr);
  266. if (EFI_ERROR (Status)) {
  267. DEBUG ((DEBUG_ERROR, "[%a]:[%dL] Set mac failed!\n", __FUNCTION__, __LINE__));
  268. return Status;
  269. }
  270. return EFI_SUCCESS;
  271. }
  272. UINT32
  273. OemEthFindFirstSP (
  274. VOID
  275. )
  276. {
  277. UINT32 I;
  278. for (I = 0; I < ETH_MAX_PORT; I++) {
  279. if (gEthPdtDesc[I].Valid == TRUE) {
  280. return I;
  281. }
  282. }
  283. return ETH_INVALID;
  284. }
  285. ETH_PRODUCT_DESC *
  286. OemEthInit (
  287. UINT32 port
  288. )
  289. {
  290. return (ETH_PRODUCT_DESC *)(&(gEthPdtDesc[port]));
  291. }
  292. BOOLEAN
  293. OemIsInitEth (
  294. UINT32 Port
  295. )
  296. {
  297. return TRUE;
  298. }
  299. EFI_STATUS
  300. ConfigCDR (
  301. UINT32 Socket
  302. )
  303. {
  304. return EFI_SUCCESS;
  305. }
  306. UINT32
  307. OemGetNclConfOffset (
  308. UINT32 Socket
  309. )
  310. {
  311. UINT32 ConfigurationOffset;
  312. if (Socket == 0) {
  313. // For 1st socket, the NCL configuration offset is 0
  314. ConfigurationOffset = 0;
  315. MmioWrite32 (SRAM_NIC_NCL1_OFFSET_ADDRESS, ConfigurationOffset);
  316. return ConfigurationOffset;
  317. }
  318. // For 2nd Socket
  319. if ((ReadCpldReg (CPU2_SFP2_100G_CARD_OFFSET) & CARD_PRESENT_100G) != 0) {
  320. ConfigurationOffset = SIZE_128KB;
  321. } else {
  322. ConfigurationOffset = SIZE_64KB;
  323. }
  324. MmioWrite32 (SRAM_NIC_NCL2_OFFSET_ADDRESS, ConfigurationOffset);
  325. return ConfigurationOffset;
  326. }
  327. UINT32
  328. OemGetNetPortNum (
  329. UINT32 Socket
  330. )
  331. {
  332. if (Socket == 0){
  333. return SOCKET0_NET_PORT_NUM;
  334. }
  335. if ((ReadCpldReg (CPU2_SFP2_100G_CARD_OFFSET) & CARD_PRESENT_100G) != 0) {
  336. return SOCKET1_NET_PORT_100G;
  337. } else {
  338. return SOCKET1_NET_PORT_NUM;
  339. }
  340. }