IPMBIfc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Brief: Implement IPMB send and receive function in this file, include primary IPMB and secondary IPMB.
  3. * Author: Jimbo_Zhang@outlook.com
  4. * Date: 2019-9-9
  5. */
  6. /* Standard includes. */
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <pthread.h>
  12. #include <fcntl.h>
  13. #include <sys/socket.h>
  14. #include "com_IPMI_LANIfc.h"
  15. #include "com_IPMI_RMCP.h"
  16. #include "RMCP.h"
  17. #include "LANIfc.h"
  18. #include "com_IPMIDefs.h"
  19. #include "RMCP.h"
  20. #include "MD.h"
  21. #include <netdb.h> /* getaddrinfo(3) et al. */
  22. #include <netinet/in.h> /* sockaddr_in & sockaddr_in6 definition. */
  23. #include <net/if.h>
  24. #include <sys/prctl.h>
  25. #include "main.h"
  26. #include "Session.h"
  27. #include "com_IPMI_SDR.h"
  28. #include "com_IPMI_Storage.h"
  29. #include "SDR.h"
  30. #include <sys/syscall.h> /*此头必须带上*/
  31. pid_t gettid()
  32. {
  33. return syscall(SYS_gettid); /*这才是内涵*/
  34. }
  35. //static int sendIPMBPkt(uint32_t ipmb_bus, uint8_t* i2c_send_buf, uint8_t size);
  36. static uint8_t ValidateIPMBChkSums ( uint8_t* Data, uint8_t Size);
  37. static void ProcessIPMBReq ( MsgPkt_T* pReq);
  38. uint8_t IsResponseMatch (MsgPkt_T* pReq, MsgPkt_T* pRes);
  39. static void* RecvIPMBPkt (void *pArg);
  40. int gFd_PrimaryIpmbIfcQ, gFd_PrimaryIpmbResQ;
  41. int gFd_SecondaryIpmbIfcQ, gFd_SecondaryIpmbResQ;
  42. int gFd_Primary, gFd_Secondary;
  43. pthread_mutex_t primary_mutex, secondary_mutex;
  44. /*!
  45. \brief IPMB Task. Generating primary_IPMB task and secondary_IPMB task in this task.
  46. \param: 0: primary ipmb, 1: secondary ipmb
  47. \retval none
  48. */
  49. void *IPMBIfcTask(void *Param)
  50. {
  51. // int i;
  52. MsgPkt_T RcvMsgPkt;
  53. uint8_t dev_name[10] = {0};
  54. uint8_t IPMB_IFC_Q[32] = {0};
  55. uint8_t IPMB_RES_Q[32] = {0};
  56. uint8_t OwnerAddr;
  57. int fd_IpmbDev;
  58. int fd_IpmbIfcQ, fd_IpmbResQ;
  59. uint8_t ipmbSelect = *(uint8_t*)Param;
  60. int RetVal;
  61. pthread_mutex_t *pMutex;
  62. //printf("ipmbSelect: %d\n", ipmbSelect);
  63. //Primary IPMB
  64. if((ipmbSelect == 0) && g_BMCInfo.IpmiConfig.PrimaryIPMBSupport)
  65. {
  66. printf("Primary IPMBIfcTask start...\n");
  67. strcpy(IPMB_IFC_Q, PRIMARY_IPMB_IFC_Q);
  68. strcpy(IPMB_RES_Q, PRIMARY_IPMB_RES_Q);
  69. sprintf(dev_name, "/dev/i2c%d", g_BMCInfo.IpmiConfig.PrimaryIPMBBus);
  70. OwnerAddr = g_BMCInfo.IpmiConfig.PrimaryIPMBAddr;
  71. pMutex = &primary_mutex;
  72. }
  73. //Secondary IPMB
  74. else if((ipmbSelect == 1) && g_BMCInfo.IpmiConfig.SecondaryIPMBSupport)
  75. {
  76. printf("Secondary IPMBIfcTask start...\n");
  77. strcpy(IPMB_IFC_Q, SECONDARY_IPMB_IFC_Q);
  78. strcpy(IPMB_RES_Q, SECONDARY_IPMB_RES_Q);
  79. sprintf(dev_name, "/dev/i2c%d", g_BMCInfo.IpmiConfig.SecondaryIPMBBus);
  80. OwnerAddr = g_BMCInfo.IpmiConfig.SecondaryIPMBAddr;
  81. pMutex = &secondary_mutex;
  82. }
  83. else
  84. {
  85. printf("Invalid param! %d\n", ipmbSelect);
  86. return (void *)-1;
  87. }
  88. //create IPMB_IFC_Q
  89. if(-1 != access(IPMB_IFC_Q, F_OK))
  90. {
  91. remove(IPMB_IFC_Q);
  92. }
  93. if(0 != mkfifo (IPMB_IFC_Q, 0777))
  94. {
  95. printf("%s: Create %s fifo failed! %s\n", __FUNCTION__, IPMB_IFC_Q, strerror(errno));
  96. return (void*)-1;
  97. }
  98. fd_IpmbIfcQ = open (IPMB_IFC_Q, O_RDWR);
  99. if(-1 == fd_IpmbIfcQ)
  100. {
  101. printf("%s: Open %s fifo failed! %s\n", __FUNCTION__, IPMB_IFC_Q, strerror(errno));
  102. return (void*)-1;
  103. }
  104. //create IPMB_RES_Q
  105. if(-1 != access(IPMB_RES_Q, F_OK))
  106. {
  107. remove(IPMB_RES_Q);
  108. }
  109. if(0 != mkfifo (IPMB_RES_Q, 0777))
  110. {
  111. printf("%s: Create %s fifo failed! %s\n", __FUNCTION__, IPMB_RES_Q, strerror(errno));
  112. return (void*)-1;
  113. }
  114. fd_IpmbResQ = open (IPMB_RES_Q, O_RDWR);
  115. if(-1 == fd_IpmbResQ)
  116. {
  117. printf("%s: Open %s fifo failed! %s\n", __FUNCTION__, IPMB_RES_Q, strerror(errno));
  118. return (void*)-1;
  119. }
  120. fd_IpmbDev = open (dev_name, O_RDWR);
  121. if(-1 == fd_IpmbDev)
  122. {
  123. printf("%s: Open IPMB failed! %s\n", __FUNCTION__, strerror(errno));
  124. return (void*)-1;
  125. }
  126. //set IPMB address
  127. if(0 != stm32_i2c_set_addr(fd_IpmbDev, OwnerAddr))
  128. printf("Set ipmb address fail.\n");
  129. if(ipmbSelect == 0)
  130. {
  131. gFd_PrimaryIpmbIfcQ = fd_IpmbIfcQ;
  132. gFd_PrimaryIpmbResQ = fd_IpmbResQ;
  133. gFd_Primary = fd_IpmbDev;
  134. }
  135. else if(ipmbSelect == 1)
  136. {
  137. gFd_SecondaryIpmbIfcQ = fd_IpmbIfcQ;
  138. gFd_SecondaryIpmbResQ = fd_IpmbResQ;
  139. gFd_Secondary = fd_IpmbDev;
  140. }
  141. pthread_mutex_init(pMutex, NULL);
  142. /*Create a thread to recv IPMB Pkt */
  143. gThreadIndex++;
  144. if(0 != pthread_create(&gThreadIDs[gThreadIndex],NULL,RecvIPMBPkt,Param))
  145. {
  146. printf("%s: Create RecvIPMBPkt thread failed! %s\n", __FUNCTION__, strerror(errno));
  147. return (void *)-1;
  148. }
  149. while(1)
  150. {
  151. while(GetMsg(fd_IpmbIfcQ, &RcvMsgPkt, WAIT_INFINITE) != 0);
  152. // printf("======== IPMB Get message ========\n");
  153. // printf("gFd_LanIfcQ %d, gFd_LanResQ %d, gFd_MsgHndlrIfc %d\n",
  154. // gFd_LanIfcQ,gFd_LanResQ,gFd_MsgHndlrIfc);
  155. switch(RcvMsgPkt.Param)
  156. {
  157. case PARAM_REQUEST:
  158. //printf("---> IPMBIfcTask/PARAM_REQUEST\n");
  159. ProcessIPMBReq (&RcvMsgPkt);
  160. break;
  161. case PARAM_BRIDGE:
  162. //printf("---> IPMBIfcTask/PARAM_BRIDGE, channel: %d\n", RcvMsgPkt.Channel);
  163. /* Send the response */
  164. if(RcvMsgPkt.Channel == PRIMARY_IPMB_CHANNEL)
  165. {
  166. RetVal = stm32_i2c_master_write(gFd_Primary, RcvMsgPkt.Data[0], &RcvMsgPkt.Data[1], RcvMsgPkt.Size-1);
  167. printf("PriTx: ");
  168. }
  169. else if(RcvMsgPkt.Channel == SECONDARY_IPMB_CHANNEL)
  170. {
  171. RetVal = stm32_i2c_master_write(gFd_Secondary, RcvMsgPkt.Data[0], &RcvMsgPkt.Data[1], RcvMsgPkt.Size-1);
  172. printf("SecTx: ");
  173. }
  174. else
  175. {
  176. printf("IPMBIfc.c: IPMB channel error. %#x\r\n", RcvMsgPkt.Channel);
  177. }
  178. int i;
  179. for(i=0;i<RcvMsgPkt.Size;i++)
  180. printf("%#x ", RcvMsgPkt.Data[i]);
  181. printf("\n");
  182. if (RetVal < 0)
  183. {
  184. printf ("IPMBIfc.c : Unable to send a IPMI Bridge Message\n");
  185. RespondSendMessage (&RcvMsgPkt, STATUS_FAIL);
  186. }
  187. else
  188. {
  189. RespondSendMessage (&RcvMsgPkt, STATUS_OK);
  190. }
  191. break;
  192. default:
  193. printf("Unknow message param %#x\r\n", RcvMsgPkt.Param);
  194. break;
  195. }
  196. }
  197. }
  198. /**
  199. * @brief Process the request posted by IPMB driver.
  200. * @param pReq - Request message pointer.
  201. **/
  202. static void
  203. ProcessIPMBReq ( MsgPkt_T* pReq)
  204. {
  205. MsgPkt_T ResPkt;
  206. uint8_t ReqLen;
  207. IPMIMsgHdr_T* pIPMIMsgReq = (IPMIMsgHdr_T*) pReq->Data;
  208. int RetVal = 0;
  209. ReqLen = pReq->Size;
  210. /* Validate Checksums */
  211. if (TRUE != ValidateIPMBChkSums ((uint8_t*)pIPMIMsgReq, ReqLen))
  212. {
  213. printf ("IPMBIfc.c : IPMB Checksum validation failed\r\n");
  214. return;
  215. }
  216. pReq->Param = PARAM_IFC;
  217. pReq->Cmd = pIPMIMsgReq->Cmd;
  218. pReq->NetFnLUN = pIPMIMsgReq->NetFnLUN;
  219. if(pReq->Channel == PRIMARY_IPMB_CHANNEL)
  220. pReq->SrcQ = gFd_PrimaryIpmbResQ;
  221. else
  222. pReq->SrcQ = gFd_SecondaryIpmbResQ;
  223. if(0 == ValidateMsgHdr(pReq))
  224. {
  225. /* Post the message to message handler Task */
  226. if (0 != PostMsg (gFd_MsgHndlrIfc, pReq))
  227. {
  228. printf("IPMB post message to MsgHndlrTask fail!\n");;
  229. }
  230. }
  231. else
  232. {
  233. //printf("Bridge response to Originator\n");
  234. return;
  235. }
  236. /* Post the message to message handler Task */
  237. //PostMsg (gFd_MsgHndlrIfc, pReq);
  238. if(pReq->ResTimeOut < 1) pReq->ResTimeOut = 10;
  239. do
  240. {
  241. if(pReq->Channel == PRIMARY_IPMB_CHANNEL)
  242. RetVal = GetMsg (gFd_PrimaryIpmbResQ, &ResPkt, pReq->ResTimeOut);
  243. else
  244. RetVal = GetMsg (gFd_SecondaryIpmbResQ, &ResPkt, pReq->ResTimeOut);
  245. }while(!RetVal && !IsResponseMatch(pReq, &ResPkt) );
  246. if (0 != RetVal)
  247. {
  248. printf("Warning: Process IPMB request fail, return timeout.\r\n");
  249. ResPkt.Param = PARAM_NORMAL_RESPONSE;
  250. ResPkt.Size = sizeof(IPMIMsgHdr_T)+2;
  251. SwapIPMIMsgHdr(((IPMIMsgHdr_T*)pReq->Data), ((IPMIMsgHdr_T*)ResPkt.Data));
  252. ResPkt.Data[sizeof(IPMIMsgHdr_T)] = CC_TIMEOUT;
  253. ResPkt.Data[sizeof(IPMIMsgHdr_T)+1] = CalculateCheckSum2(ResPkt.Data, sizeof(IPMIMsgHdr_T)+1);
  254. }
  255. /* If its not normal IPMI Response just return */
  256. if (PARAM_NO_RESPONSE == ResPkt.Param)
  257. {
  258. printf ("IPMBIfc.c : IPMB request packet dropped, not processed\n");
  259. return;
  260. }
  261. /* Send the response */
  262. if(pReq->Channel == PRIMARY_IPMB_CHANNEL)
  263. {
  264. pthread_mutex_lock(&primary_mutex);
  265. RetVal = stm32_i2c_master_write(gFd_Primary, ResPkt.Data[0], &ResPkt.Data[1], ResPkt.Size-1);
  266. pthread_mutex_unlock(&primary_mutex);
  267. printf("PriTx: ");
  268. }
  269. else if(pReq->Channel == SECONDARY_IPMB_CHANNEL)
  270. {
  271. pthread_mutex_lock(&secondary_mutex);
  272. RetVal = stm32_i2c_master_write(gFd_Secondary, ResPkt.Data[0], &ResPkt.Data[1], ResPkt.Size-1);
  273. pthread_mutex_unlock(&secondary_mutex);
  274. printf("SecTx: ");
  275. }
  276. else
  277. {
  278. printf("IPMBIfc.c: IPMB channel error. %#x\r\n", pReq->Channel);
  279. }
  280. int i;
  281. for(i=0;i<ResPkt.Size;i++)
  282. printf("%#x ", ResPkt.Data[i]);
  283. printf("\n");
  284. if (0 != RetVal)
  285. {
  286. printf ("IPMBIfc.c : Unable to send a IPMI Response\n");
  287. }
  288. return;
  289. }
  290. /**
  291. * @brief Validate IPMB Checksums.
  292. * @param Data - Buffer to be validated.
  293. * @param Size - Size of the buffer.
  294. * @return TRUE if valid, FALSE if error.
  295. **/
  296. static uint8_t ValidateIPMBChkSums ( uint8_t* Data, uint8_t Size)
  297. {
  298. uint8_t i;
  299. uint8_t chksum;
  300. /* Check IPMB message min size */
  301. if (Size < MIN_IPMB_MSG_LENGTH)
  302. {
  303. printf("IPMBIfc.c: Invalid IPMB Message Length\r\n");
  304. return FALSE;
  305. }
  306. /* Validate the checksum1 */
  307. chksum = 0;
  308. for (i = 0; i < 3; i++)
  309. {
  310. chksum += *(Data + i);
  311. }
  312. if (chksum != 0)
  313. {
  314. printf ("IPMBIfc.c: Invalid checksum 1, size = %d\n",Size);
  315. for(i = 0; i < Size;i++)
  316. printf("%x ",Data[i]);
  317. printf("\n");
  318. return FALSE;
  319. }
  320. /* Validate the checksum2 */
  321. chksum = 0;
  322. for (i = 3; i < Size; i++)
  323. {
  324. chksum += *(Data + i);
  325. }
  326. if (chksum != 0)
  327. {
  328. printf("IPMBIfc.c: Invalid checksum 2, size = %d\n",Size);
  329. for(i = 0; i < Size;i++)
  330. printf("%x ",Data[i]);
  331. printf("\n");
  332. return FALSE;
  333. }
  334. return TRUE;
  335. }
  336. /*-----------------------------------------------------
  337. * @fn IsResponseMatch
  338. * @brief Checks if the Response Message corresponds to
  339. * the Request Message
  340. *
  341. * @param pReq : IPMI Request Message Packet
  342. * @param pRes : IPMI Response Message Packet
  343. *
  344. * @return 1 if match
  345. * 0 if mismatch
  346. *----------------------------------------------------*/
  347. uint8_t IsResponseMatch (MsgPkt_T* pReq, MsgPkt_T* pRes)
  348. {
  349. if( ( ((((IPMIMsgHdr_T*)pRes->Data)->RqSeqLUN) >> 2) != ((((IPMIMsgHdr_T*)pReq->Data)->RqSeqLUN) >> 2) ) &&
  350. ( (pReq->NetFnLUN & 0x4) && (PARAM_NO_RESPONSE != pRes->Param) ) )
  351. {
  352. return FALSE;
  353. }
  354. return TRUE;
  355. }
  356. /**
  357. * @fn RecvIPMBPkt
  358. * @brief This function receives IPMB request packet and post the message to
  359. * IPMB interface queue
  360. **/
  361. static void* RecvIPMBPkt (void *Param)
  362. {
  363. MsgPkt_T IPMBReqPkt;
  364. int IPMBSlaveFd;
  365. int retval;
  366. uint8_t ipmbSelect = *(uint8_t*)Param;
  367. int i;
  368. prctl(PR_SET_NAME, __FUNCTION__, 0, 0, 0);
  369. int curThreadIndex = 0;
  370. pthread_mutex_t *pMutex;
  371. memset(&IPMBReqPkt,0,sizeof(MsgPkt_T));
  372. if(0 == ipmbSelect)
  373. {
  374. printf ("Primary IPMB Receiver Started...\n");
  375. IPMBReqPkt.Data [0] = g_BMCInfo.IpmiConfig.PrimaryIPMBAddr;
  376. IPMBReqPkt.Channel = PRIMARY_IPMB_CHANNEL;
  377. IPMBSlaveFd = gFd_Primary;
  378. pMutex = &primary_mutex;
  379. printf("Primary: %p\n", RecvIPMBPkt);
  380. }
  381. else if(1 == ipmbSelect)
  382. {
  383. printf ("Secondary IPMB Receiver Started...\n");
  384. IPMBReqPkt.Data [0] = g_BMCInfo.IpmiConfig.SecondaryIPMBAddr;
  385. IPMBReqPkt.Channel = SECONDARY_IPMB_CHANNEL;
  386. IPMBSlaveFd = gFd_Secondary;
  387. pMutex = &secondary_mutex;
  388. printf("Secondary: %p\n", RecvIPMBPkt);
  389. }
  390. /* Loop forever */
  391. while (1)
  392. {
  393. //printf("ppid: %d, pid: %d, tid: %d, i: %d, ipmbSelect: %d\n", getppid(), getpid(), gettid(), i++, ipmbSelect);
  394. pthread_mutex_lock(pMutex);
  395. pthread_mutex_unlock(pMutex);
  396. retval = stm32_i2c_slave_recv(IPMBSlaveFd, &IPMBReqPkt.Data[1]);
  397. if( retval > 5)
  398. {
  399. IPMBReqPkt.Param = PARAM_REQUEST;
  400. IPMBReqPkt.Size = retval + 1; /* +1 to include BMC Slave address */
  401. int cnt;
  402. if(0 == ipmbSelect)
  403. printf("\nPriRx: ");
  404. else
  405. printf("\nSecRx: ");
  406. for(cnt=0;cnt < IPMBReqPkt.Size; cnt++)
  407. printf("%#x ", IPMBReqPkt.Data[cnt]);
  408. printf("\n");
  409. /* Post the IPMB Request message to IPMBIfc Queue */
  410. if(ipmbSelect == 0)
  411. {
  412. if (0 != PostMsg (gFd_PrimaryIpmbIfcQ, &IPMBReqPkt))
  413. {
  414. printf ("IPMBIfc.c : Error posting message to IPMBIfc_Q\n");
  415. }
  416. }
  417. else
  418. {
  419. if (0 != PostMsg (gFd_SecondaryIpmbIfcQ, &IPMBReqPkt))
  420. {
  421. printf ("IPMBIfc.c : Error posting message to IPMBIfc_Q\n");
  422. }
  423. }
  424. }
  425. }
  426. return (void*)-1;
  427. }