IPMBIfc.c 15 KB

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