MsgHndlrTask.c 28 KB

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