MsgHndlrTask.c 29 KB

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