MsgHndlrTask.c 29 KB

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