IPMBIfc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. switch(RcvMsgPkt.Param)
  153. {
  154. case PARAM_REQUEST:
  155. ProcessIPMBReq (&RcvMsgPkt);
  156. break;
  157. case PARAM_BRIDGE:
  158. printf("---> BRIDGING_REQUEST, channel: %d\n", RcvMsgPkt.Channel);
  159. int i;
  160. for(i=0;i<RcvMsgPkt.Size;i++)
  161. printf("%#x ", RcvMsgPkt.Data[i]);
  162. printf("\n");
  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. }
  168. else if(RcvMsgPkt.Channel == SECONDARY_IPMB_CHANNEL)
  169. {
  170. RetVal = stm32_i2c_master_write(gFd_Secondary, RcvMsgPkt.Data[0], &RcvMsgPkt.Data[1], RcvMsgPkt.Size-1);
  171. }
  172. else
  173. {
  174. printf("IPMBIfc.c: IPMB channel error. %#x\r\n", RcvMsgPkt.Channel);
  175. }
  176. if (RetVal < 0)
  177. {
  178. printf ("IPMBIfc.c : Unable to send a IPMI Bridge Message\n");
  179. RespondSendMessage (&RcvMsgPkt, STATUS_FAIL);
  180. }
  181. else
  182. {
  183. RespondSendMessage (&RcvMsgPkt, STATUS_OK);
  184. }
  185. break;
  186. default:
  187. printf("Unknow message param %#x\r\n", RcvMsgPkt.Param);
  188. break;
  189. }
  190. }
  191. }
  192. /**
  193. * @brief Process the request posted by IPMB driver.
  194. * @param pReq - Request message pointer.
  195. **/
  196. static void
  197. ProcessIPMBReq ( MsgPkt_T* pReq)
  198. {
  199. MsgPkt_T ResPkt;
  200. uint8_t ReqLen;
  201. IPMIMsgHdr_T* pIPMIMsgReq = (IPMIMsgHdr_T*) pReq->Data;
  202. int RetVal = 0;
  203. ReqLen = pReq->Size;
  204. /* Validate Checksums */
  205. if (TRUE != ValidateIPMBChkSums ((uint8_t*)pIPMIMsgReq, ReqLen))
  206. {
  207. printf ("IPMBIfc.c : IPMB Checksum validation failed\r\n");
  208. return;
  209. }
  210. pReq->Param = PARAM_IFC;
  211. pReq->Cmd = pIPMIMsgReq->Cmd;
  212. pReq->NetFnLUN = pIPMIMsgReq->NetFnLUN;
  213. if(pReq->Channel == PRIMARY_IPMB_CHANNEL)
  214. pReq->SrcQ = gFd_PrimaryIpmbResQ;
  215. else
  216. pReq->SrcQ = gFd_SecondaryIpmbResQ;
  217. /* Post the message to message handler Task */
  218. PostMsg (gFd_MsgHndlrIfc, pReq);
  219. if(pReq->ResTimeOut < 1) pReq->ResTimeOut = 10;
  220. do
  221. {
  222. if(pReq->Channel == PRIMARY_IPMB_CHANNEL)
  223. RetVal = GetMsg (gFd_PrimaryIpmbResQ, &ResPkt, pReq->ResTimeOut);
  224. else
  225. RetVal = GetMsg (gFd_SecondaryIpmbResQ, &ResPkt, pReq->ResTimeOut);
  226. }while(!RetVal && !IsResponseMatch(pReq, &ResPkt) );
  227. if (0 != RetVal)
  228. {
  229. printf("Warning: Process IPMB request fail, return timeout.\r\n");
  230. ResPkt.Param = PARAM_NORMAL_RESPONSE;
  231. ResPkt.Size = sizeof(IPMIMsgHdr_T)+2;
  232. SwapIPMIMsgHdr(((IPMIMsgHdr_T*)pReq->Data), ((IPMIMsgHdr_T*)ResPkt.Data));
  233. ResPkt.Data[sizeof(IPMIMsgHdr_T)] = CC_TIMEOUT;
  234. ResPkt.Data[sizeof(IPMIMsgHdr_T)+1] = CalculateCheckSum2(ResPkt.Data, sizeof(IPMIMsgHdr_T)+1);
  235. }
  236. /* If its not normal IPMI Response just return */
  237. if (PARAM_NO_RESPONSE == ResPkt.Param)
  238. {
  239. printf ("IPMBIfc.c : IPMB request packet dropped, not processed\n");
  240. return;
  241. }
  242. /* Send the response */
  243. if(pReq->Channel == PRIMARY_IPMB_CHANNEL)
  244. {
  245. pthread_mutex_lock(&primary_mutex);
  246. RetVal = stm32_i2c_master_write(gFd_Primary, ResPkt.Data[0], &ResPkt.Data[1], ResPkt.Size-1);
  247. pthread_mutex_unlock(&primary_mutex);
  248. printf("PriTx: ");
  249. }
  250. else if(pReq->Channel == SECONDARY_IPMB_CHANNEL)
  251. {
  252. pthread_mutex_lock(&secondary_mutex);
  253. RetVal = stm32_i2c_master_write(gFd_Secondary, ResPkt.Data[0], &ResPkt.Data[1], ResPkt.Size-1);
  254. pthread_mutex_unlock(&secondary_mutex);
  255. printf("SecTx: ");
  256. }
  257. else
  258. {
  259. printf("IPMBIfc.c: IPMB channel error. %#x\r\n", pReq->Channel);
  260. }
  261. int i;
  262. for(i=0;i<ResPkt.Size;i++)
  263. printf("%#x ", ResPkt.Data[i]);
  264. printf("\n");
  265. if (0 != RetVal)
  266. {
  267. printf ("IPMBIfc.c : Unable to send a IPMI Response\n");
  268. }
  269. return;
  270. }
  271. /**
  272. * @brief Validate IPMB Checksums.
  273. * @param Data - Buffer to be validated.
  274. * @param Size - Size of the buffer.
  275. * @return TRUE if valid, FALSE if error.
  276. **/
  277. static uint8_t ValidateIPMBChkSums ( uint8_t* Data, uint8_t Size)
  278. {
  279. uint8_t i;
  280. uint8_t chksum;
  281. /* Check IPMB message min size */
  282. if (Size < MIN_IPMB_MSG_LENGTH)
  283. {
  284. printf("IPMBIfc.c: Invalid IPMB Message Length\r\n");
  285. return FALSE;
  286. }
  287. /* Validate the checksum1 */
  288. chksum = 0;
  289. for (i = 0; i < 3; i++)
  290. {
  291. chksum += *(Data + i);
  292. }
  293. if (chksum != 0)
  294. {
  295. printf ("IPMBIfc.c: Invalid checksum 1, size = %d\n",Size);
  296. for(i = 0; i < Size;i++)
  297. printf("%x ",Data[i]);
  298. printf("\n");
  299. return FALSE;
  300. }
  301. /* Validate the checksum2 */
  302. chksum = 0;
  303. for (i = 3; i < Size; i++)
  304. {
  305. chksum += *(Data + i);
  306. }
  307. if (chksum != 0)
  308. {
  309. printf("IPMBIfc.c: Invalid checksum 2, size = %d\n",Size);
  310. for(i = 0; i < Size;i++)
  311. printf("%x ",Data[i]);
  312. printf("\n");
  313. return FALSE;
  314. }
  315. return TRUE;
  316. }
  317. /*-----------------------------------------------------
  318. * @fn IsResponseMatch
  319. * @brief Checks if the Response Message corresponds to
  320. * the Request Message
  321. *
  322. * @param pReq : IPMI Request Message Packet
  323. * @param pRes : IPMI Response Message Packet
  324. *
  325. * @return 1 if match
  326. * 0 if mismatch
  327. *----------------------------------------------------*/
  328. uint8_t IsResponseMatch (MsgPkt_T* pReq, MsgPkt_T* pRes)
  329. {
  330. if( ( ((((IPMIMsgHdr_T*)pRes->Data)->RqSeqLUN) >> 2) != ((((IPMIMsgHdr_T*)pReq->Data)->RqSeqLUN) >> 2) ) &&
  331. ( (pReq->NetFnLUN & 0x4) && (PARAM_NO_RESPONSE != pRes->Param) ) )
  332. {
  333. return FALSE;
  334. }
  335. return TRUE;
  336. }
  337. /**
  338. * @fn RecvIPMBPkt
  339. * @brief This function receives IPMB request packet and post the message to
  340. * IPMB interface queue
  341. **/
  342. static void* RecvIPMBPkt (void *Param)
  343. {
  344. MsgPkt_T IPMBReqPkt;
  345. int IPMBSlaveFd;
  346. int retval;
  347. uint8_t ipmbSelect = *(uint8_t*)Param;
  348. int i;
  349. prctl(PR_SET_NAME, __FUNCTION__, 0, 0, 0);
  350. int curThreadIndex = 0;
  351. pthread_mutex_t *pMutex;
  352. memset(&IPMBReqPkt,0,sizeof(MsgPkt_T));
  353. if(0 == ipmbSelect)
  354. {
  355. printf ("Primary IPMB Receiver Started...\n");
  356. IPMBReqPkt.Data [0] = g_BMCInfo.IpmiConfig.PrimaryIPMBAddr;
  357. IPMBReqPkt.Channel = PRIMARY_IPMB_CHANNEL;
  358. IPMBSlaveFd = gFd_Primary;
  359. pMutex = &primary_mutex;
  360. printf("Primary: %p\n", RecvIPMBPkt);
  361. }
  362. else if(1 == ipmbSelect)
  363. {
  364. printf ("Secondary IPMB Receiver Started...\n");
  365. IPMBReqPkt.Data [0] = g_BMCInfo.IpmiConfig.SecondaryIPMBAddr;
  366. IPMBReqPkt.Channel = SECONDARY_IPMB_CHANNEL;
  367. IPMBSlaveFd = gFd_Secondary;
  368. pMutex = &secondary_mutex;
  369. printf("Secondary: %p\n", RecvIPMBPkt);
  370. }
  371. /* Loop forever */
  372. while (1)
  373. {
  374. //printf("ppid: %d, pid: %d, tid: %d, i: %d, ipmbSelect: %d\n", getppid(), getpid(), gettid(), i++, ipmbSelect);
  375. pthread_mutex_lock(pMutex);
  376. pthread_mutex_unlock(pMutex);
  377. retval = stm32_i2c_slave_recv(IPMBSlaveFd, &IPMBReqPkt.Data[1]);
  378. if( retval > 5)
  379. {
  380. IPMBReqPkt.Param = PARAM_REQUEST;
  381. IPMBReqPkt.Size = retval + 1; /* +1 to include BMC Slave address */
  382. int cnt;
  383. if(0 == ipmbSelect)
  384. printf("\nPriRx: ");
  385. else
  386. printf("\nSecRx: ");
  387. for(cnt=0;cnt < IPMBReqPkt.Size; cnt++)
  388. printf("%#x ", IPMBReqPkt.Data[cnt]);
  389. printf("\n");
  390. /* Post the IPMB Request message to IPMBIfc Queue */
  391. if(ipmbSelect == 0)
  392. {
  393. if (0 != PostMsg (gFd_PrimaryIpmbIfcQ, &IPMBReqPkt))
  394. {
  395. printf ("IPMBIfc.c : Error posting message to IPMBIfc_Q\n");
  396. }
  397. }
  398. else
  399. {
  400. if (0 != PostMsg (gFd_SecondaryIpmbIfcQ, &IPMBReqPkt))
  401. {
  402. printf ("IPMBIfc.c : Error posting message to IPMBIfc_Q\n");
  403. }
  404. }
  405. }
  406. }
  407. return (void*)-1;
  408. }