MsgHndlrTask.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * Brief: Receive message from Interface and send response to original interface.
  3. Process all IPMI standard command and oem command.
  4. * Author: Jimbo_Zhang@outlook.com
  5. * Date: 2019-9-13, the Mid-autumn Festival;
  6. */
  7. #include <stdio.h>
  8. #include <sys/stat.h>
  9. #include <sys/socket.h>
  10. #include <sys/un.h>
  11. #include <sys/prctl.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <pthread.h>
  19. #include "MsgHndlr.h"
  20. #include "PDKCmds.h"
  21. #include "App.h"
  22. #include "PICMG.h"
  23. #include "Bridge.h"
  24. #include "SensorEvent.h"
  25. #include "Chassis.h"
  26. #include "Storage.h"
  27. #include "cmdselect.h"
  28. #include "com_IPMI_App.h"
  29. #include "com_IPMI_Storage.h"
  30. #include "com_IPMI_SDR.h"
  31. //#include "main.h"
  32. #include <string.h>
  33. #include "main.h"
  34. static void ProcessIPMIReq (MsgPkt_T* pReq, MsgPkt_T* pRes);
  35. static void PendingBridgeResTimerTask (void);
  36. static void PendingSeqNoTimerTask (void);
  37. MsgHndlrTbl_T m_MsgHndlrTbl [] = //notice!
  38. {
  39. { NETFN_APP, g_App_CmdHndlr },
  40. { NETFN_CHASSIS, g_Chassis_CmdHndlr },
  41. { NETFN_BRIDGE, g_Bridge_CmdHndlr },
  42. { NETFN_SENSOR, g_SensorEvent_CmdHndlr },
  43. { NETFN_STORAGE, g_Storage_CmdHndlr },
  44. // { NETFN_TRANSPORT, g_Config_CmdHndlr },
  45. // { NETFN_AMI, (CmdHndlrMap_T*)g_AMI_CmdHndlr },
  46. { 0, 0 },
  47. };
  48. GroupExtnMsgHndlrTbl_T m_GroupExtnMsgHndlrTbl [] = //jimbo add
  49. {
  50. {0, 0},
  51. };
  52. TimerTaskTbl_T m_TimerTaskTbl [20] =
  53. {
  54. // { 1, PEFTimerTask },
  55. // { 1, PETAckTimerTask },
  56. // { 1, PEFStartDlyTimerTask },
  57. { 1, SessionTimerTask },
  58. { 1, PendingBridgeResTimerTask },
  59. { 1, PendingSeqNoTimerTask },
  60. // { 1, FlashTimerTask },
  61. // #if FRB_SUPPORT == 1
  62. // { 1, FRB3TimerTask },
  63. // #endif /* #if FRB_SUPPORT == 1 */
  64. //
  65. // #if SERIAL_MODEM_CONNECTION_ACTIVITY != UNIMPLEMENTED
  66. // { 2, SerialModemPingTask },
  67. // #endif /* SERIAL_MODEM_CONNECTION_ACTIVITY */
  68. //
  69. // { 1, MonitorPassword },
  70. { 1, UDSSessionTimerTask },
  71. };
  72. //PendingBridgedResTbl_T m_PendingBridgedResTbl[MAX_PENDING_BRIDGE_RES];
  73. PendingBridgedResTbl_T m_PendingBridgedResTbl[MAX_PENDING_BRIDGE_TBL][MAX_PENDING_BRIDGE_RES];
  74. PendingSeqNoTbl_T m_PendingSeqNoTbl[16][MAX_PENDING_SEQ_NO];
  75. KCSBridgeResInfo_T m_KCSBridgeResInfo;
  76. int gFd_MsgHndlrIfc;
  77. TLS_T g_tls;
  78. /*!
  79. \brief Message handler Task. Process all standard and oem ipmi message form interface, and send response back.
  80. \param[in] none
  81. \param[out] none
  82. \retval none
  83. */
  84. void *MsgHndlrTask( void *pArg )
  85. {
  86. MsgPkt_T reqMsgPkt;
  87. MsgPkt_T resMsgPkt;
  88. uint8_t channelinit = 0xF;
  89. printf("MsgHndlrTask start...\r\n");
  90. prctl(PR_SET_NAME,__FUNCTION__,0,0,0);
  91. //create
  92. if(-1 != access(MSG_HNDLR_Q, F_OK))
  93. {
  94. remove(MSG_HNDLR_Q);
  95. }
  96. if(0 != mkfifo (MSG_HNDLR_Q, 0777))
  97. {
  98. printf("%s: Create %s fifo failed! %s\n", __FUNCTION__, MSG_HNDLR_Q, strerror(errno));
  99. return (void*)-1;
  100. }
  101. gFd_MsgHndlrIfc = open (MSG_HNDLR_Q, O_RDWR);
  102. if(-1 == gFd_MsgHndlrIfc)
  103. {
  104. printf("%s: Open %s fifo failed! %s\n", __FUNCTION__, MSG_HNDLR_Q, strerror(errno));
  105. return (void*)-1;
  106. }
  107. pthread_key_create(&g_tls.CurSessionID,NULL);
  108. pthread_key_create(&g_tls.CurPrivLevel,NULL);
  109. pthread_key_create(&g_tls.CurChannel,NULL);
  110. pthread_key_create(&g_tls.CurKCSIfcNum,NULL);
  111. pthread_key_create(&g_tls.CurSessionType,NULL);
  112. pthread_key_create(&g_tls.OwnerLUN,NULL);
  113. pthread_setspecific(g_tls.CurChannel,&channelinit);
  114. while(1)
  115. {
  116. while(GetMsg(gFd_MsgHndlrIfc, &reqMsgPkt, WAIT_INFINITE) != 0);
  117. // printf("Mrx: ");
  118. // int i;
  119. // for(i=0;i<reqMsgPkt.Size;i++)
  120. // printf("%#x ", reqMsgPkt.Data[i]);
  121. // printf("\n");
  122. pthread_setspecific(g_tls.CurChannel,&reqMsgPkt.Channel);
  123. pthread_setspecific(g_tls.CurPrivLevel,&reqMsgPkt.Privilege);
  124. pthread_setspecific(g_tls.CurSessionID,&reqMsgPkt.SessionID);
  125. pthread_setspecific(g_tls.CurSessionType,&reqMsgPkt.SessionType);
  126. pthread_setspecific(g_tls.OwnerLUN,&reqMsgPkt.NetFnLUN);
  127. ProcessIPMIReq(&reqMsgPkt, &resMsgPkt);
  128. //update checksum2
  129. resMsgPkt.Data[resMsgPkt.Size-1] = CalculateCheckSum2(resMsgPkt.Data, resMsgPkt.Size-1);
  130. if(g_BMCInfo.IpmiConfig.UDSIfcSupport == 0x01 && (reqMsgPkt.SrcQ == gFdUdsRes))
  131. {
  132. if (resMsgPkt.Size == sizeof(IPMIUDSMsg_T) && (NORMAL_RESPONSE == resMsgPkt.Param) )
  133. {
  134. continue;
  135. }
  136. resMsgPkt.Size = resMsgPkt.Size+1;
  137. SwapUDSIPMIMsg(&reqMsgPkt,&resMsgPkt);
  138. resMsgPkt.Data[resMsgPkt.Size] = 0;
  139. }
  140. if(SYS_IFC_CHANNEL == reqMsgPkt.Channel)
  141. {
  142. /* Skip current request if no response from Command Handler */
  143. if (!resMsgPkt.Size && (NO_RESPONSE == resMsgPkt.Param) )
  144. {
  145. continue;
  146. }
  147. resMsgPkt.SessionID = reqMsgPkt.SessionID;
  148. }
  149. // printf("Mtx: ");
  150. // for(i=0;i<resMsgPkt.Size;i++)
  151. // printf("%#x ", resMsgPkt.Data[i]);
  152. // printf("\n");
  153. //send response pkt to source queue.
  154. PostMsg(resMsgPkt.SrcQ, &resMsgPkt);
  155. }
  156. }
  157. /**
  158. *@fn ProcessIPMIReq
  159. *@brief Processes the requested IPMI command
  160. *@param pReq Request of the command
  161. *@param pRes Response for the requested command
  162. *@return none
  163. */
  164. static void
  165. ProcessIPMIReq (MsgPkt_T* pReq, MsgPkt_T* pRes)
  166. {
  167. CmdHndlrMap_T * pCmdHndlrMap;
  168. uint32_t HdrOffset = 0;
  169. uint8_t CmdOverride = 1;
  170. int8_t MsgHndlrMapGot=0;
  171. uint8_t PBTbl = PRIMARY_PB_TBL;
  172. // msgHndlr_dbg_printf ("Processing IPMI Packet.\r\n");
  173. /* Set the Cmd and Net function in response packet */
  174. pRes->Cmd = pReq->Cmd;
  175. pRes->NetFnLUN = pReq->NetFnLUN;
  176. pRes->Channel = pReq->Channel;
  177. pRes->SrcQ = pReq->SrcQ;
  178. /* Normal IPMI Command response */
  179. pRes->Param = PARAM_NORMAL_RESPONSE;
  180. if ((SYS_IFC_CHANNEL != pReq->Channel) && (USB_CHANNEL != pReq->Channel) && (UDS_CHANNEL != pReq->Channel))
  181. {
  182. //printf("log 1, pReq->Channel: %#x\n", pReq->Channel);
  183. HdrOffset = sizeof (IPMIMsgHdr_T);
  184. pReq->Size = pReq->Size - HdrOffset - 1;
  185. }
  186. if(UDS_CHANNEL == pReq->Channel)
  187. {
  188. printf("log 2, pReq->Size: %d, HdrOffset: %d\n", pReq->Size, HdrOffset);
  189. HdrOffset = sizeof(IPMIUDSMsg_T);
  190. pReq->Size = pReq->Size -HdrOffset - 1;
  191. }
  192. SwapIPMIMsgHdr((IPMIMsgHdr_T*)pReq->Data, (IPMIMsgHdr_T*)pRes->Data);
  193. pRes->Size = HdrOffset + sizeof(uint8_t);
  194. pthread_setspecific(g_tls.CurSessionID,&pReq->SessionID);
  195. if(IsCommandEnabled(NET_FN(pReq->NetFnLUN), &pReq->Data[HdrOffset], pReq->Cmd) != 0)
  196. {
  197. pRes->Data [HdrOffset] = CC_INV_CMD;
  198. printf ("Invalid Net Function 0x%x or Invalid Command 0x%x\n",NET_FN(pReq->NetFnLUN), pReq->Cmd);
  199. pRes->Size += 2;
  200. return;
  201. }
  202. //Get oem command map
  203. MsgHndlrMapGot = PDKGetOEMMsgHndlrMap(NET_FN(pReq->NetFnLUN),&pCmdHndlrMap);
  204. if(MsgHndlrMapGot == 0)
  205. {
  206. CmdOverride = GetCmdHndlr(pReq,pRes,pCmdHndlrMap,HdrOffset,CmdOverride,&pCmdHndlrMap);
  207. }
  208. //get standard ipmi command map
  209. if((CmdOverride == 0 || MsgHndlrMapGot == -1) ) //OEM commands have no command map.
  210. {
  211. if (0 != GetMsgHndlrMap (NET_FN (pReq->NetFnLUN), &pCmdHndlrMap))
  212. {
  213. // if(pBMCInfo->IpmiConfig.GrpExtnSupport == 1)
  214. // {
  215. if (0 != GroupExtnGetMsgHndlrMap (NET_FN (pReq->NetFnLUN), pReq->Data [HdrOffset], &pCmdHndlrMap) )
  216. {
  217. pRes->Data [HdrOffset] = CC_INV_CMD;
  218. printf ("MsgHndlr.c : Invalid Net Function 0x%x or Invalid Command 0x%x\n",NET_FN(pReq->NetFnLUN), pReq->Cmd);
  219. pRes->Size += 2;
  220. return;
  221. }
  222. // }
  223. // else
  224. // {
  225. // pRes->Data [HdrOffset] = CC_INV_CMD;
  226. // printf ("MsgHndlr.c : Invalid Net Function 0x%x\n",NET_FN(pReq->NetFnLUN));
  227. // pRes->Size += 2;
  228. // return;
  229. // }
  230. }
  231. if(GetCmdHndlr(pReq,pRes,pCmdHndlrMap,HdrOffset,CmdOverride,&pCmdHndlrMap) == FALSE)
  232. {
  233. pRes->Data [HdrOffset] = CC_INV_CMD;
  234. pRes->Size += 2;
  235. //printf("log 3\n");
  236. return;
  237. }
  238. }
  239. /* Check for the request size */
  240. if (0xff != pCmdHndlrMap->ReqLen)
  241. {
  242. /* Check for invalid request size */
  243. if (pCmdHndlrMap->ReqLen != pReq->Size)
  244. {
  245. printf("Warning: Request data len error, require %d\n", pCmdHndlrMap->ReqLen);
  246. pRes->Data [HdrOffset] = CC_REQ_INV_LEN;
  247. pRes->Size += 2;
  248. return;
  249. }
  250. }
  251. pRes->Size = pCmdHndlrMap->CmdHndlr (&pReq->Data [HdrOffset], pReq->Size, &pRes->Data [HdrOffset]) + HdrOffset + 1;
  252. // //Patch for sensor Owner ID not equal IPMB address bug.
  253. // if((NET_FN(pReq->NetFnLUN) == NETFN_STORAGE) && (pReq->Cmd == CMD_GET_SDR)
  254. // && (((GetSDRReq_T*)&pReq->Data[6])->Offset == 5) && (pRes->Data [HdrOffset] == CC_NORMAL))
  255. // {
  256. // pRes->Data [HdrOffset+3] = ((IPMIMsgHdr_T*)(pReq->Data))->ResAddr; //modify sensor owner id
  257. // }
  258. //send message command
  259. if( (CMD_SEND_MSG == pReq->Cmd) && (NETFN_APP == pReq->NetFnLUN >> 2))
  260. {
  261. //printf("log 4\n");
  262. int Offset = 0;
  263. uint8_t SeqNum = g_BMCInfo.SendMsgSeqNum;
  264. if ((0 == pRes->Size) &&
  265. ((g_BMCInfo.IpmiConfig.PrimaryIPMBSupport == 0x01 && PRIMARY_IPMB_CHANNEL == pRes->Channel) ||
  266. (g_BMCInfo.IpmiConfig.SecondaryIPMBSupport == 0x01 && SECONDARY_IPMB_CHANNEL == pRes->Channel)) )
  267. {
  268. pRes->Param = PARAM_NO_RESPONSE;
  269. Offset = HdrOffset + 2;
  270. }
  271. else if (HdrOffset == pRes->Size)
  272. {
  273. Offset = HdrOffset + 1;
  274. }
  275. PBTbl = ( ((pReq->Data[sizeof (IPMIMsgHdr_T)] & 0x0F) == SECONDARY_IPMB_CHANNEL) ? SECONDARY_PB_TBL : PRIMARY_PB_TBL );
  276. while(TRUE)
  277. {
  278. if ( (TRUE == m_PendingBridgedResTbl[PBTbl][SeqNum].Used) &&
  279. (0 == memcmp (&m_PendingBridgedResTbl[PBTbl][SeqNum].ReqMsgHdr, &pReq->Data[Offset], sizeof (IPMIMsgHdr_T))) )
  280. {
  281. memcpy (&m_PendingBridgedResTbl[PBTbl][SeqNum].ResMsgHdr, pRes->Data, sizeof (IPMIMsgHdr_T));
  282. break;
  283. }
  284. else
  285. {
  286. SeqNum = (SeqNum - 1) & 0x3F;
  287. if(SeqNum == g_BMCInfo.SendMsgSeqNum)
  288. {
  289. break;
  290. }
  291. }
  292. }
  293. }
  294. //printf("log 5\n");
  295. return;
  296. }
  297. /**
  298. *@fn CalculateChecksum2
  299. *@brief Calculates the checksum
  300. *@param Pkt Pointer to the data for the checksum to be calculated
  301. *@param Len Size of data for checksum calculation
  302. *@return Returns the checksum value
  303. */
  304. uint8_t CalculateCheckSum2 (uint8_t* Pkt, uint32_t Len)
  305. {
  306. uint8_t Sum;
  307. uint32_t i;
  308. /* Get Checksum 2 */
  309. Sum = 0;
  310. for (i = 3; i < Len; i++)
  311. {
  312. Sum += Pkt [i];
  313. }
  314. return (uint8_t)(0xFF & (0x100 - Sum));
  315. }
  316. /**
  317. *@fn GetMsgHndlrMap
  318. *@brief Gets the exact command Handler by comparing NetFn
  319. *@param Netfn -NetFunction of the Cmd to execute
  320. *@param pCmdHndlrMap Pointer to the Command Handler
  321. *@return Returns 0 on success
  322. * Returns -1 on failure
  323. */
  324. int
  325. GetMsgHndlrMap (uint8_t NetFn, CmdHndlrMap_T ** pCmdHndlrMap)
  326. {
  327. int i;
  328. /* Get the command handler corresponding to the net function */
  329. for (i = 0; i < sizeof(m_MsgHndlrTbl)/sizeof(m_MsgHndlrTbl[0]); i++) //pBMCInfo->MsgHndlrTblSize
  330. {
  331. if (m_MsgHndlrTbl [i].NetFn == NetFn) { break; }
  332. }
  333. /* Check if we have not found our net function */
  334. if (i == sizeof(m_MsgHndlrTbl)/sizeof(m_MsgHndlrTbl[0]) )
  335. {
  336. return -1;
  337. }
  338. /* Get the handler corresponding to the command */
  339. *pCmdHndlrMap = (CmdHndlrMap_T*)m_MsgHndlrTbl [i].CmdHndlrMap;
  340. return 0;
  341. }
  342. /**
  343. *@fn GetCmdHndlr
  344. *@brief Picks up the exact command to execute by comparing Cmd no.
  345. *@param pReq Request buffer for the command
  346. *@param pRes Response buffer for the command
  347. *@param pCmdHndlrMap
  348. *@param HdrOffset
  349. *@param CmdOverride
  350. *@param CmdHndlr
  351. *@return Returns TRUE on success
  352. * Returns FALSE on failure
  353. */
  354. int GetCmdHndlr(MsgPkt_T* pReq,MsgPkt_T* pRes,CmdHndlrMap_T* pCmdHndlrMap,
  355. uint32_t HdrOffset,uint8_t CmdOverride,CmdHndlrMap_T** CmdHndrl )
  356. {
  357. int i=0;
  358. while (1)
  359. {
  360. /**
  361. * If we reached the end of the Command Handler map - invalid command
  362. **/
  363. if (0 == pCmdHndlrMap->CmdHndlr)
  364. {
  365. if(CmdOverride == FALSE)
  366. {
  367. pRes->Data [HdrOffset] = CC_INV_CMD;
  368. printf( "Warning: MsgHndlr.c : Invalid Command %#x\r\n", pReq->Cmd );
  369. }
  370. return FALSE;
  371. }
  372. if (pCmdHndlrMap->Cmd == pReq->Cmd)
  373. {
  374. break;
  375. }
  376. i++;
  377. pCmdHndlrMap++;
  378. }
  379. *CmdHndrl = pCmdHndlrMap;
  380. return TRUE;
  381. }
  382. /**
  383. *@fn GroupExtnGetMsgHndlrMap
  384. *@brief Gets the exact command Handler by comparing NetFn
  385. *@param Netfn -NetFunction of the Cmd to execute
  386. *@GroupExtnCode - Group Extension code
  387. *@param pCmdHndlrMap Pointer to the Command Handler
  388. *@return Returns 0 on success
  389. * Returns -1 on failure
  390. */
  391. int
  392. GroupExtnGetMsgHndlrMap (uint8_t NetFn, uint8_t GroupExtnCode, CmdHndlrMap_T ** pCmdHndlrMap)
  393. {
  394. int i;
  395. /* Get the command handler corresponding to the net function */
  396. for (i = 0; i < sizeof (m_GroupExtnMsgHndlrTbl) / sizeof (m_GroupExtnMsgHndlrTbl [0]); i++)
  397. {
  398. if ((m_GroupExtnMsgHndlrTbl [i].NetFn == NetFn) && (m_GroupExtnMsgHndlrTbl [i].GroupExtnCode == GroupExtnCode))
  399. {
  400. break;
  401. }
  402. }
  403. /* Check if we have not found our net function */
  404. if (i == sizeof (m_GroupExtnMsgHndlrTbl) / sizeof (m_GroupExtnMsgHndlrTbl[0]))
  405. {
  406. return -1;
  407. }
  408. // printf("---> GroupExtnGetMsgHndlrMap successful, i=%d\n",i);
  409. /* Get the handler corresponding to the command */
  410. *pCmdHndlrMap = (CmdHndlrMap_T*)m_GroupExtnMsgHndlrTbl [i].CmdHndlrMap;
  411. return 0;
  412. }
  413. /*------------------------------------------------------------------
  414. *@fn RespondSendMessage
  415. *@brief Frames the Response packet when a IPMB destination is
  416. * unavialable
  417. *
  418. *@param pReq: Request Message Packet address
  419. *@param Status Status of SendIPMBPkt method
  420. *@param BMCInst: BMC Instance Number
  421. *
  422. *@return none
  423. *-----------------------------------------------------------------*/
  424. void RespondSendMessage ( MsgPkt_T* pReq, uint8_t Status)
  425. {
  426. uint8_t PBTbl = PRIMARY_PB_TBL;
  427. MsgPkt_T ResPkt;
  428. IPMIMsgHdr_T* pIPMIResHdr = ( IPMIMsgHdr_T*)ResPkt.Data;
  429. IPMIMsgHdr_T* pIPMIReqHdr = ( IPMIMsgHdr_T*)pReq->Data;
  430. uint8_t SeqNum = NET_FN(pIPMIReqHdr->RqSeqLUN);
  431. int QueueFd;
  432. /* Check for pending responses */
  433. PBTbl = ( (pReq->Channel == SECONDARY_IPMB_CHANNEL) ? SECONDARY_PB_TBL : PRIMARY_PB_TBL );
  434. if ( (TRUE == m_PendingBridgedResTbl[PBTbl][SeqNum].Used) &&
  435. (NET_FN(pIPMIReqHdr->RqSeqLUN) == SeqNum) &&
  436. (NET_FN(pIPMIReqHdr->NetFnLUN) == NET_FN(m_PendingBridgedResTbl[PBTbl][SeqNum].ReqMsgHdr.NetFnLUN )) &&
  437. (pIPMIReqHdr->Cmd == m_PendingBridgedResTbl[PBTbl][SeqNum].ReqMsgHdr.Cmd) &&
  438. (pIPMIReqHdr->ResAddr == m_PendingBridgedResTbl[PBTbl][SeqNum].ReqMsgHdr.ResAddr) )
  439. {
  440. memcpy (pIPMIResHdr, &m_PendingBridgedResTbl[PBTbl][SeqNum].ResMsgHdr.IPMIMsgHdr, sizeof (IPMIMsgHdr_T));
  441. if (STATUS_OK == Status)
  442. {
  443. if ( ( m_PendingBridgedResTbl[PBTbl][SeqNum].DestQ == gFd_PrimaryIpmbIfcQ) ||
  444. (m_PendingBridgedResTbl[PBTbl][SeqNum].DestQ == gFd_SecondaryIpmbIfcQ) ||
  445. (ORIGIN_SENDMSG != m_PendingBridgedResTbl[PBTbl][SeqNum].OriginSrc) )
  446. {
  447. return;
  448. }
  449. ResPkt.Data [sizeof(IPMIMsgHdr_T)] = CC_NORMAL;
  450. }
  451. else if (STATUS_FAIL == Status)
  452. {
  453. ResPkt.Data [sizeof(IPMIMsgHdr_T)] = CC_NO_ACK_FROM_SLAVE;
  454. }
  455. else
  456. {
  457. ResPkt.Data [sizeof(IPMIMsgHdr_T)] = CC_UNSPECIFIED_ERR;
  458. }
  459. ResPkt.Size = sizeof (IPMIMsgHdr_T) + 1 + 1; // IPMI Header + Completion Code + Second Checksum
  460. ResPkt.Cmd = pIPMIResHdr->Cmd;
  461. ResPkt.NetFnLUN = pIPMIReqHdr->NetFnLUN;
  462. ResPkt.Channel = pReq->Channel;
  463. /* Calculate the Second CheckSum */
  464. ResPkt.Data[ResPkt.Size - 1] = CalculateCheckSum2 (ResPkt.Data, ResPkt.Size-1);
  465. ResPkt.Param = BRIDGING_REQUEST;
  466. if (m_PendingBridgedResTbl[PBTbl][SeqNum].DestQ == gFd_LanIfcQ)
  467. {
  468. QueueFd = gFd_LanResQ;
  469. }
  470. // else if (0 == strcmp ((char *)m_PendingBridgedResTbl[PBTbl][SeqNum].DestQ, SerialQueueName))
  471. // {
  472. // //ResPkt.SessionID = m_PendingBridgedResTbl[i].ResMsgHdr.RqSeqLUN;
  473. // strcpy (QueueName, SERIAL_RES_Q);
  474. // }
  475. else if ( (m_PendingBridgedResTbl[PBTbl][SeqNum].DestQ == gFd_PrimaryIpmbIfcQ) ||
  476. (m_PendingBridgedResTbl[PBTbl][SeqNum].DestQ == gFd_SecondaryIpmbIfcQ) )
  477. {
  478. QueueFd = m_PendingBridgedResTbl[PBTbl][SeqNum].DestQ;
  479. }
  480. else
  481. {
  482. /* PDK Hook to format Pending Bridge Response Packet for other destinations */
  483. // if(g_PDKHandle[PDK_FORMATBRIDGERESPKT] != NULL)
  484. {
  485. SwapIPMIMsgHdr ( pIPMIReqHdr, pIPMIResHdr);
  486. // ( (void (*)(MsgPkt_T *, int) ) g_PDKHandle[PDK_FORMATBRIDGERESPKT]) ( &ResPkt, BMCInst);
  487. }
  488. QueueFd = m_PendingBridgedResTbl[PBTbl][SeqNum].DestQ;
  489. }
  490. if (STATUS_OK != Status)
  491. {
  492. m_PendingBridgedResTbl[PBTbl][SeqNum].Used = FALSE;
  493. }
  494. /* Post the data to Destination Interface queue */
  495. PostMsg (QueueFd, &ResPkt);
  496. }
  497. }
  498. /**
  499. *@fn PendingBridgeResTimerTask
  500. *@brief Sends the timeout message to response queue
  501. * if the message does not turn out within send message timeout
  502. *@return none
  503. */
  504. static void
  505. PendingBridgeResTimerTask (void)
  506. {
  507. uint8_t i;
  508. uint8_t PBTbl = PRIMARY_PB_TBL;
  509. int QueueFd = 0;
  510. printf("-> PendingBridgeResTimerTask\n");
  511. /* Check for any pending responses */
  512. for( PBTbl=0; PBTbl < MAX_PENDING_BRIDGE_TBL; PBTbl++)
  513. {
  514. for (i = 0; i < sizeof (m_PendingBridgedResTbl[0])/sizeof (m_PendingBridgedResTbl[0][0]); i++)
  515. {
  516. if (TRUE == m_PendingBridgedResTbl[PBTbl][i].Used)
  517. {
  518. m_PendingBridgedResTbl[PBTbl][i].TimeOut--;
  519. if (0 == m_PendingBridgedResTbl[PBTbl][i].TimeOut)
  520. {
  521. MsgPkt_T Timeout;
  522. IPMIMsgHdr_T* pIPMIMsgHdr = (IPMIMsgHdr_T*) Timeout.Data;
  523. /* Fill the response packet */
  524. SwapIPMIMsgHdr (&m_PendingBridgedResTbl[PBTbl][i].ReqMsgHdr, pIPMIMsgHdr);
  525. // slog(LANQueueName,"%s%d",LAN_IFC_Q,BMCInst);
  526. // sprintf(PrimaryIPMBQueueName,"%s%d",IPMB_PRIMARY_IFC_Q,BMCInst);
  527. // sprintf(SecondaryIPMBQueueName,"%s%d",IPMB_SECONDARY_IFC_Q,BMCInst);
  528. // sprintf(SerialQueueName,"%s%d",SERIAL_IFC_Q,BMCInst);
  529. if(PBTbl == PRIMARY_PB_TBL)
  530. {
  531. pIPMIMsgHdr->ReqAddr = g_BMCInfo.IpmiConfig.PrimaryIPMBAddr;
  532. }
  533. else if(PBTbl == SECONDARY_PB_TBL)
  534. {
  535. pIPMIMsgHdr->ReqAddr = g_BMCInfo.IpmiConfig.SecondaryIPMBAddr;
  536. }
  537. else
  538. {
  539. printf("Invalid PBTbl\n");;//pIPMIMsgHdr->ReqAddr = pBMCInfo->IpmiConfig.BMCSlaveAddr;
  540. }
  541. Timeout.Data [sizeof(IPMIMsgHdr_T)] = CC_TIMEOUT;
  542. Timeout.Size = sizeof (IPMIMsgHdr_T) + 1 + 1; // IPMI Header + Completion Code + Second Checksum
  543. /* Calculate the Second CheckSum */
  544. Timeout.Data[Timeout.Size - 1] = CalculateCheckSum2 (Timeout.Data, Timeout.Size-1);
  545. Timeout.Param = BRIDGING_REQUEST;
  546. if (m_PendingBridgedResTbl[PBTbl][i].DestQ == gFd_LanIfcQ)
  547. {
  548. int j;
  549. for (j = Timeout.Size - 1; j >= 0; --j)
  550. {
  551. Timeout.Data [j+1] = Timeout.Data [j];
  552. }
  553. Timeout.Data[0] = m_PendingBridgedResTbl[PBTbl][i].SrcSessionHandle;
  554. Timeout.Size++;
  555. Timeout.Cmd = PAYLOAD_IPMI_MSG;
  556. QueueFd = gFd_LanIfcQ;
  557. }
  558. // else if (0 == strcmp ((char *)m_PendingBridgedResTbl[PBTbl][i].DestQ, SerialQueueName))
  559. // {
  560. // int j;
  561. // for (j = Timeout.Size - 1; j >= 0; --j)
  562. // {
  563. // Timeout.Data [j+1] = Timeout.Data [j];
  564. // }
  565. // Timeout.Data[0] = m_PendingBridgedResTbl[PBTbl][i].SrcSessionHandle;
  566. // Timeout.Size++;
  567. // Timeout.Cmd = PAYLOAD_IPMI_MSG;
  568. // strcpy(QueueName, SERIAL_IFC_Q);
  569. // }
  570. else if (g_BMCInfo.IpmiConfig.PrimaryIPMBSupport == 1 && m_PendingBridgedResTbl[PBTbl][i].DestQ == gFd_PrimaryIpmbIfcQ)
  571. {
  572. int j;
  573. for (j = Timeout.Size - 1; j >= 0; --j)
  574. {
  575. Timeout.Data [j + sizeof (IPMIMsgHdr_T) + 1] = Timeout.Data [j];
  576. }
  577. memcpy (Timeout.Data, &m_PendingBridgedResTbl[PBTbl][i].ResMsgHdr, sizeof (IPMIMsgHdr_T));
  578. Timeout.Data[sizeof (IPMIMsgHdr_T)] = CC_NORMAL;
  579. Timeout.Size++;
  580. QueueFd = gFd_PrimaryIpmbIfcQ;
  581. }
  582. else if (g_BMCInfo.IpmiConfig.SecondaryIPMBSupport == 1 && m_PendingBridgedResTbl[PBTbl][i].DestQ == gFd_SecondaryIpmbIfcQ)
  583. {
  584. int j;
  585. for (j = Timeout.Size - 1; j >= 0; --j)
  586. {
  587. Timeout.Data [j + sizeof (IPMIMsgHdr_T) + 1] = Timeout.Data [j];
  588. }
  589. memcpy (Timeout.Data, &m_PendingBridgedResTbl[PBTbl][i].ResMsgHdr.IPMIMsgHdr, sizeof (IPMIMsgHdr_T));
  590. Timeout.Data[sizeof (IPMIMsgHdr_T)] = CC_NORMAL;
  591. Timeout.Size++;
  592. QueueFd = gFd_SecondaryIpmbIfcQ;
  593. }
  594. else if (m_PendingBridgedResTbl[PBTbl][i].DestQ = gFdUdsIfc)
  595. {
  596. int j;
  597. UDSSessionTbl_T *pUDSSessionInfo = NULL;
  598. for (j = sizeof (IPMIMsgHdr_T); j < Timeout.Size ;j++)
  599. {
  600. Timeout.Data [j + sizeof (IPMIUDSMsg_T)-sizeof (IPMIMsgHdr_T)] = Timeout.Data [j];
  601. }
  602. Timeout.Size = Timeout.Size + sizeof (IPMIUDSMsg_T)- sizeof (IPMIMsgHdr_T);
  603. m_PendingBridgedResTbl[PBTbl][i].ResMsgHdr.UDSMsgHdr.IPMIMsgLen = Timeout.Size;
  604. memcpy (Timeout.Data, &m_PendingBridgedResTbl[PBTbl][i].ResMsgHdr.UDSMsgHdr, sizeof (IPMIUDSMsg_T));
  605. Timeout.NetFnLUN = m_PendingBridgedResTbl[PBTbl][i].ResMsgHdr.UDSMsgHdr.NetFnLUN;
  606. Timeout.SessionID = m_PendingBridgedResTbl[PBTbl][i].ResMsgHdr.UDSMsgHdr.SessionID;
  607. Timeout.Cmd = m_PendingBridgedResTbl[PBTbl][i].ResMsgHdr.UDSMsgHdr.Cmd;
  608. pUDSSessionInfo = GetUDSSessionInfo (UDS_SESSION_ID_INFO,&Timeout.SessionID);
  609. if(pUDSSessionInfo != NULL)
  610. {
  611. Timeout.Socket = pUDSSessionInfo->UDSSocket;
  612. }
  613. QueueFd = m_PendingBridgedResTbl[PBTbl][i].DestQ;
  614. }
  615. else
  616. {
  617. int j;
  618. for (j = Timeout.Size - 1; j >= 0; --j)
  619. {
  620. Timeout.Data [j + sizeof (IPMIMsgHdr_T) + 1] = Timeout.Data [j];
  621. }
  622. memcpy (Timeout.Data, &m_PendingBridgedResTbl[PBTbl][i].ResMsgHdr.IPMIMsgHdr, sizeof (IPMIMsgHdr_T));
  623. Timeout.Data[sizeof (IPMIMsgHdr_T)] = CC_TIMEOUT;
  624. QueueFd = m_PendingBridgedResTbl[PBTbl][i].DestQ;
  625. }
  626. /* Post the data to Destination Interface queue */
  627. PostMsg (QueueFd, &Timeout);
  628. m_PendingBridgedResTbl[PBTbl][i].Used = FALSE;
  629. printf( "MsgHndlr: clean pending index = %d.\n", i );
  630. }
  631. }
  632. }
  633. }
  634. }
  635. /**
  636. *@fn PendingSeqNoTimerTask
  637. *@brief Timertask which helpful in IPMB sequence number validation
  638. *@return none
  639. */
  640. static void
  641. PendingSeqNoTimerTask (void)
  642. {
  643. uint8_t i,j;
  644. printf("-> PendingSeqNoTimerTask\n");
  645. /* Check for any Sequence Number expiraied */
  646. for (i = 0; i < sizeof (m_PendingSeqNoTbl)/sizeof (m_PendingSeqNoTbl[0]); i++)
  647. {
  648. for (j = 0; j < sizeof (m_PendingSeqNoTbl[0])/sizeof (m_PendingSeqNoTbl[0][0]); j++)
  649. {
  650. if (TRUE == m_PendingSeqNoTbl[i][j].Used)
  651. {
  652. m_PendingSeqNoTbl[i][j].TimeOut--;
  653. if (0 == m_PendingSeqNoTbl[i][j].TimeOut)
  654. {
  655. m_PendingSeqNoTbl[i][j].Used = FALSE;
  656. }
  657. }
  658. }
  659. }
  660. }
  661. /**
  662. *@fn SwapIPMIMsgHdr
  663. *@brief Swaps the header and copies into response
  664. *@param pIPMIMsgReq Header of the Request
  665. *@param pIPMIMsgRes Header of the response
  666. *@return none
  667. */
  668. void
  669. SwapIPMIMsgHdr (IPMIMsgHdr_T* pIPMIMsgReq, IPMIMsgHdr_T* pIPMIMsgRes)
  670. {
  671. pIPMIMsgRes->ResAddr = pIPMIMsgReq->ReqAddr;
  672. pIPMIMsgRes->NetFnLUN = (pIPMIMsgReq->NetFnLUN & 0xFC) + 0x04;
  673. pIPMIMsgRes->NetFnLUN |= pIPMIMsgReq->RqSeqLUN & 0x03;
  674. /* Calculate the Checksum for above two bytes */
  675. pIPMIMsgRes->ChkSum = (~(pIPMIMsgRes->ResAddr + pIPMIMsgRes->NetFnLUN) + 1);
  676. pIPMIMsgRes->ReqAddr = pIPMIMsgReq->ResAddr;
  677. pIPMIMsgRes->RqSeqLUN = (pIPMIMsgReq->RqSeqLUN & 0xFC);
  678. pIPMIMsgRes->RqSeqLUN |= (pIPMIMsgReq->NetFnLUN & 0x03);
  679. pIPMIMsgRes->Cmd = pIPMIMsgReq->Cmd;
  680. return;
  681. }
  682. /**
  683. * *@fn Swap UDSIPMIMsg
  684. * *@brief Swaps the header and copies into response
  685. * *@param pIPMIMsgReq Header of the Request
  686. * *@param pIPMIMsgRes Header of the response
  687. * *@return none
  688. * */
  689. void
  690. SwapUDSIPMIMsg ( MsgPkt_T* pIPMIMsgReq, MsgPkt_T* pIPMIMsgRes)
  691. {
  692. IPMIUDSMsg_T *pIPMIUDSMsgRes = (IPMIUDSMsg_T *)&pIPMIMsgRes->Data[0];
  693. IPMIUDSMsg_T *pIPMIUDSMsgReq = (IPMIUDSMsg_T *)&pIPMIMsgReq->Data[0];
  694. pIPMIMsgRes->NetFnLUN = pIPMIMsgReq->NetFnLUN;
  695. // pIPMIMsgRes->Privilege = pIPMIMsgReq->Privilege;
  696. pIPMIMsgRes->Cmd = pIPMIMsgReq->Cmd;
  697. pIPMIMsgRes->Channel = pIPMIMsgReq->Channel;
  698. pIPMIMsgRes->SessionID = pIPMIMsgReq->SessionID;
  699. pIPMIMsgRes->Socket = pIPMIMsgReq->Socket;
  700. pIPMIUDSMsgRes->NetFnLUN = pIPMIMsgReq->NetFnLUN;
  701. // pIPMIUDSMsgRes->Privilege = pIPMIMsgReq->Privilege;
  702. pIPMIUDSMsgRes->Cmd = pIPMIMsgReq->Cmd;
  703. pIPMIUDSMsgRes->ChannelNum = pIPMIMsgReq->Channel;
  704. pIPMIUDSMsgRes->SessionID = pIPMIMsgReq->SessionID;
  705. //pIPMIUDSMsgRes->AuthFlag = pIPMIUDSMsgReq->AuthFlag;
  706. pIPMIUDSMsgRes->IPMIMsgLen = pIPMIMsgRes->Size;
  707. // strcpy( (char *)pIPMIUDSMsgRes->UserName, (char *)pIPMIUDSMsgReq->UserName);
  708. // memcpy(pIPMIUDSMsgRes->IPAddr, pIPMIUDSMsgReq->IPAddr, 16);
  709. return;
  710. }