IPMBIfc.c 14 KB

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