blob: 0922438d5c416729af15659046760b7a32969327 [file] [log] [blame]
Vernon Mauerya3702c12019-05-22 13:20:59 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Yong Lic3580e92019-08-15 14:36:47 +080017#include <bitset>
Vernon Mauerya3702c12019-05-22 13:20:59 -070018#include <bridgingcommands.hpp>
19#include <cstring>
Vernon Mauery15419dd2019-05-24 09:40:30 -070020#include <ipmid/api.hpp>
Yong Lic3580e92019-08-15 14:36:47 +080021#include <ipmid/utils.hpp>
Yong Liedbb4082020-03-06 17:38:25 +080022#include <manufacturingcommands.hpp>
Vernon Mauerya3702c12019-05-22 13:20:59 -070023#include <phosphor-logging/log.hpp>
24#include <sdbusplus/bus.hpp>
25#include <sdbusplus/bus/match.hpp>
26#include <sdbusplus/message.hpp>
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +000027#include <storagecommands.hpp>
Vernon Mauerya3702c12019-05-22 13:20:59 -070028#include <vector>
29
Yong Lic3580e92019-08-15 14:36:47 +080030static constexpr const char *wdtService = "xyz.openbmc_project.Watchdog";
31static constexpr const char *wdtInterface =
32 "xyz.openbmc_project.State.Watchdog";
33static constexpr const char *wdtObjPath = "/xyz/openbmc_project/watchdog/host0";
34static constexpr const char *wdtInterruptFlagProp =
35 "PreTimeoutInterruptOccurFlag";
36
Vernon Mauerya3702c12019-05-22 13:20:59 -070037static constexpr const char *ipmbBus = "xyz.openbmc_project.Ipmi.Channel.Ipmb";
38static constexpr const char *ipmbObj = "/xyz/openbmc_project/Ipmi/Channel/Ipmb";
39static constexpr const char *ipmbIntf = "org.openbmc.Ipmb";
40
41static Bridging bridging;
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +000042static bool eventMessageBufferFlag = false;
Vernon Mauerya3702c12019-05-22 13:20:59 -070043
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +000044void Bridging::clearResponseQueue()
45{
46 responseQueue.clear();
47}
48
Vernon Mauerya3702c12019-05-22 13:20:59 -070049/**
50 * @brief utils for checksum
51 */
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +000052static bool ipmbChecksumValidate(const uint8_t *data, uint8_t length)
Vernon Mauerya3702c12019-05-22 13:20:59 -070053{
54 if (data == nullptr)
55 {
56 return false;
57 }
58
59 uint8_t checksum = 0;
60
61 for (uint8_t idx = 0; idx < length; idx++)
62 {
63 checksum += data[idx];
64 }
65
66 if (0 == checksum)
67 {
68 return true;
69 }
70
71 return false;
72}
73
74static uint8_t ipmbChecksumCompute(uint8_t *data, uint8_t length)
75{
76 if (data == nullptr)
77 {
78 return 0;
79 }
80
81 uint8_t checksum = 0;
82
83 for (uint8_t idx = 0; idx < length; idx++)
84 {
85 checksum += data[idx];
86 }
87
88 checksum = (~checksum) + 1;
89 return checksum;
90}
91
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +000092static inline bool
93 ipmbConnectionHeaderChecksumValidate(const ipmbHeader *ipmbHeader)
Vernon Mauerya3702c12019-05-22 13:20:59 -070094{
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +000095 return ipmbChecksumValidate(reinterpret_cast<const uint8_t *>(ipmbHeader),
Vernon Mauerya3702c12019-05-22 13:20:59 -070096 ipmbConnectionHeaderLength);
97}
98
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +000099static inline bool ipmbDataChecksumValidate(const ipmbHeader *ipmbHeader,
Vernon Mauerya3702c12019-05-22 13:20:59 -0700100 uint8_t length)
101{
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000102 return ipmbChecksumValidate((reinterpret_cast<const uint8_t *>(ipmbHeader) +
103 ipmbConnectionHeaderLength),
104 (length - ipmbConnectionHeaderLength));
Vernon Mauerya3702c12019-05-22 13:20:59 -0700105}
106
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000107static bool isFrameValid(const ipmbHeader *frame, uint8_t length)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700108{
109 if ((length < ipmbMinFrameLength) || (length > ipmbMaxFrameLength))
110 {
111 return false;
112 }
113
114 if (false == ipmbConnectionHeaderChecksumValidate(frame))
115 {
116 return false;
117 }
118
119 if (false == ipmbDataChecksumValidate(frame, length))
120 {
121 return false;
122 }
123
124 return true;
125}
126
127IpmbRequest::IpmbRequest(const ipmbHeader *ipmbBuffer, size_t bufferLength)
128{
129 address = ipmbBuffer->Header.Req.address;
130 netFn = ipmbNetFnGet(ipmbBuffer->Header.Req.rsNetFnLUN);
131 rsLun = ipmbLunFromNetFnLunGet(ipmbBuffer->Header.Req.rsNetFnLUN);
132 rqSA = ipmbBuffer->Header.Req.rqSA;
133 seq = ipmbSeqGet(ipmbBuffer->Header.Req.rqSeqLUN);
134 rqLun = ipmbLunFromSeqLunGet(ipmbBuffer->Header.Req.rqSeqLUN);
135 cmd = ipmbBuffer->Header.Req.cmd;
136
137 size_t dataLength =
138 bufferLength - (ipmbConnectionHeaderLength +
139 ipmbRequestDataHeaderLength + ipmbChecksumSize);
140
141 if (dataLength > 0)
142 {
143 data.insert(data.end(), ipmbBuffer->Header.Req.data,
144 &ipmbBuffer->Header.Req.data[dataLength]);
145 }
146}
147
148IpmbResponse::IpmbResponse(uint8_t address, uint8_t netFn, uint8_t rqLun,
149 uint8_t rsSA, uint8_t seq, uint8_t rsLun,
150 uint8_t cmd, uint8_t completionCode,
151 std::vector<uint8_t> &inputData) :
152 address(address),
153 netFn(netFn), rqLun(rqLun), rsSA(rsSA), seq(seq), rsLun(rsLun), cmd(cmd),
154 completionCode(completionCode)
155{
156 data.reserve(ipmbMaxDataSize);
157
158 if (inputData.size() > 0)
159 {
160 data = std::move(inputData);
161 }
162}
163
164void IpmbResponse::ipmbToi2cConstruct(uint8_t *buffer, size_t *bufferLength)
165{
166 ipmbHeader *ipmbBuffer = (ipmbHeader *)buffer;
167
168 ipmbBuffer->Header.Resp.address = address;
169 ipmbBuffer->Header.Resp.rqNetFnLUN = ipmbNetFnLunSet(netFn, rqLun);
170 ipmbBuffer->Header.Resp.rsSA = rsSA;
171 ipmbBuffer->Header.Resp.rsSeqLUN = ipmbSeqLunSet(seq, rsLun);
172 ipmbBuffer->Header.Resp.cmd = cmd;
173 ipmbBuffer->Header.Resp.completionCode = completionCode;
174
175 ipmbBuffer->Header.Resp.checksum1 = ipmbChecksumCompute(
176 buffer, ipmbConnectionHeaderLength - ipmbChecksumSize);
177
178 if (data.size() > 0)
179 {
180 std::copy(
181 data.begin(), data.end(),
182 &buffer[ipmbConnectionHeaderLength + ipmbResponseDataHeaderLength]);
183 }
184
185 *bufferLength = data.size() + ipmbResponseDataHeaderLength +
186 ipmbConnectionHeaderLength + ipmbChecksumSize;
187
188 buffer[*bufferLength - ipmbChecksumSize] =
189 ipmbChecksumCompute(&buffer[ipmbChecksum2StartOffset],
190 (ipmbResponseDataHeaderLength + data.size()));
191}
192
193void IpmbRequest::prepareRequest(sdbusplus::message::message &mesg)
194{
195 mesg.append(ipmbMeChannelNum, netFn, rqLun, cmd, data);
196}
197
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530198static constexpr unsigned int makeCmdKey(unsigned int netFn, unsigned int cmd)
199{
200 return (netFn << 8) | cmd;
201}
202
203static constexpr bool isMeCmdAllowed(uint8_t netFn, uint8_t cmd)
204{
205 constexpr uint8_t netFnMeOEM = 0x2E;
Yong Liedbb4082020-03-06 17:38:25 +0800206 constexpr uint8_t netFnMeOEMGeneral = 0x3E;
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530207 constexpr uint8_t cmdMeOemSendRawPeci = 0x40;
208 constexpr uint8_t cmdMeOemAggSendRawPeci = 0x41;
209 constexpr uint8_t cmdMeOemCpuPkgConfWrite = 0x43;
210 constexpr uint8_t cmdMeOemCpuPciConfWrite = 0x45;
211 constexpr uint8_t cmdMeOemReadMemSmbus = 0x47;
212 constexpr uint8_t cmdMeOemWriteMemSmbus = 0x48;
213 constexpr uint8_t cmdMeOemSlotIpmb = 0x51;
214 constexpr uint8_t cmdMeOemSlotI2cMasterWriteRead = 0x52;
215 constexpr uint8_t cmdMeOemSendRawPmbus = 0xD9;
216 constexpr uint8_t cmdMeOemUnlockMeRegion = 0xE7;
217 constexpr uint8_t cmdMeOemAggSendRawPmbus = 0xEC;
218
219 switch (makeCmdKey(netFn, cmd))
220 {
221 // Restrict ME Master write command
222 case makeCmdKey(ipmi::netFnApp, ipmi::app::cmdMasterWriteRead):
223 // Restrict ME OEM commands
224 case makeCmdKey(netFnMeOEM, cmdMeOemSendRawPeci):
225 case makeCmdKey(netFnMeOEM, cmdMeOemAggSendRawPeci):
226 case makeCmdKey(netFnMeOEM, cmdMeOemCpuPkgConfWrite):
227 case makeCmdKey(netFnMeOEM, cmdMeOemCpuPciConfWrite):
228 case makeCmdKey(netFnMeOEM, cmdMeOemReadMemSmbus):
229 case makeCmdKey(netFnMeOEM, cmdMeOemWriteMemSmbus):
Yong Liedbb4082020-03-06 17:38:25 +0800230 case makeCmdKey(netFnMeOEMGeneral, cmdMeOemSlotIpmb):
231 case makeCmdKey(netFnMeOEMGeneral, cmdMeOemSlotI2cMasterWriteRead):
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530232 case makeCmdKey(netFnMeOEM, cmdMeOemSendRawPmbus):
233 case makeCmdKey(netFnMeOEM, cmdMeOemUnlockMeRegion):
234 case makeCmdKey(netFnMeOEM, cmdMeOemAggSendRawPmbus):
235 return false;
236 default:
237 return true;
238 }
239}
240
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000241ipmi::Cc Bridging::handleIpmbChannel(const uint8_t tracking,
242 const std::vector<uint8_t> &msgData,
243 std::vector<uint8_t> &rspData)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700244{
Yong Liedbb4082020-03-06 17:38:25 +0800245 ipmi::Manufacturing mtm;
246
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000247 size_t msgLen = msgData.size();
248 if ((msgLen < ipmbMinFrameLength) || (msgLen > ipmbMaxFrameLength))
Vernon Mauerya3702c12019-05-22 13:20:59 -0700249 {
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000250 phosphor::logging::log<phosphor::logging::level::INFO>(
251 "handleIpmbChannel, IPMB data length is invalid");
252 return ipmi::ccReqDataLenInvalid;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700253 }
254
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000255 auto sendMsgReqData = reinterpret_cast<const ipmbHeader *>(msgData.data());
Vernon Mauerya3702c12019-05-22 13:20:59 -0700256 // TODO: check privilege lvl. Bridging to ME requires Administrator lvl
257
258 // allow bridging to ME only
259 if (sendMsgReqData->Header.Req.address != ipmbMeSlaveAddress)
260 {
261 phosphor::logging::log<phosphor::logging::level::INFO>(
262 "handleIpmbChannel, IPMB address invalid");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000263 return ipmi::ccParmOutOfRange;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700264 }
265
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530266 constexpr uint8_t shiftLUN = 2;
Yong Liedbb4082020-03-06 17:38:25 +0800267 if (mtm.getMfgMode() == ipmi::SpecialMode::none)
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530268 {
Yong Liedbb4082020-03-06 17:38:25 +0800269 if (!isMeCmdAllowed((sendMsgReqData->Header.Req.rsNetFnLUN >> shiftLUN),
270 sendMsgReqData->Header.Req.cmd))
271 {
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000272 constexpr ipmi::Cc ccCmdNotSupportedInPresentState = 0xD5;
273 return ccCmdNotSupportedInPresentState;
Yong Liedbb4082020-03-06 17:38:25 +0800274 }
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530275 }
276
Vernon Mauerya3702c12019-05-22 13:20:59 -0700277 // check allowed modes
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000278 if (tracking != modeNoTracking && tracking != modeTrackRequest)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700279 {
280 phosphor::logging::log<phosphor::logging::level::INFO>(
281 "handleIpmbChannel, mode not supported");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000282 return ipmi::ccParmOutOfRange;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700283 }
284
285 // check if request contains valid IPMB frame
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000286 if (!isFrameValid(sendMsgReqData, msgLen))
Vernon Mauerya3702c12019-05-22 13:20:59 -0700287 {
288 phosphor::logging::log<phosphor::logging::level::INFO>(
289 "handleIpmbChannel, IPMB frame invalid");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000290 return ipmi::ccParmOutOfRange;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700291 }
292
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000293 auto ipmbRequest = IpmbRequest(sendMsgReqData, msgLen);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700294
295 std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
296 ipmbResponse;
297
298 // send request to IPMB
299 try
300 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700301 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700302 auto mesg =
Vernon Mauery15419dd2019-05-24 09:40:30 -0700303 dbus->new_method_call(ipmbBus, ipmbObj, ipmbIntf, "sendRequest");
Vernon Mauerya3702c12019-05-22 13:20:59 -0700304 ipmbRequest.prepareRequest(mesg);
Vernon Mauery15419dd2019-05-24 09:40:30 -0700305 auto ret = dbus->call(mesg);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700306 ret.read(ipmbResponse);
307 }
308 catch (sdbusplus::exception::SdBusError &e)
309 {
310 phosphor::logging::log<phosphor::logging::level::ERR>(
311 "handleIpmbChannel, dbus call exception");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000312 return ipmi::ccUnspecifiedError;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700313 }
314
315 std::vector<uint8_t> dataReceived(0);
316 int status = -1;
317 uint8_t netFn = 0, lun = 0, cmd = 0, cc = 0;
318
319 std::tie(status, netFn, lun, cmd, cc, dataReceived) = ipmbResponse;
320
321 auto respReceived =
322 IpmbResponse(ipmbRequest.rqSA, netFn, lun, ipmbRequest.address,
323 ipmbRequest.seq, lun, cmd, cc, dataReceived);
324
325 // check IPMB layer status
326 if (status)
327 {
328 phosphor::logging::log<phosphor::logging::level::WARNING>(
329 "handleIpmbChannel, ipmb returned non zero status");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000330 return ipmi::ccResponseError;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700331 }
332
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000333 switch (tracking)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700334 {
335 case modeNoTracking:
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000336 {
337 if (getResponseQueueSize() == responseQueueMaxSize)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700338 {
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000339 return ipmi::ccBusy;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700340 }
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000341 insertMessageInQueue(respReceived);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700342 break;
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000343 }
Vernon Mauerya3702c12019-05-22 13:20:59 -0700344 case modeTrackRequest:
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000345 {
346 size_t dataLength = 0;
347 respReceived.ipmbToi2cConstruct(rspData.data(), &dataLength);
348 // resizing the rspData to its correct length
349 rspData.resize(dataLength);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700350 break;
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000351 }
Vernon Mauerya3702c12019-05-22 13:20:59 -0700352 default:
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000353 {
Vernon Mauerya3702c12019-05-22 13:20:59 -0700354 phosphor::logging::log<phosphor::logging::level::INFO>(
355 "handleIpmbChannel, mode not supported");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000356 return ipmi::ccParmOutOfRange;
357 }
Vernon Mauerya3702c12019-05-22 13:20:59 -0700358 }
359
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000360 return ipmi::ccSuccess;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700361}
362
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000363void Bridging::insertMessageInQueue(IpmbResponse msg)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700364{
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000365 responseQueue.insert(responseQueue.end(), std::move(msg));
366}
Vernon Mauerya3702c12019-05-22 13:20:59 -0700367
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000368void Bridging::eraseMessageFromQueue()
369{
370 responseQueue.erase(responseQueue.begin());
371}
Vernon Mauerya3702c12019-05-22 13:20:59 -0700372
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000373IpmbResponse Bridging::getMessageFromQueue()
374{
375 return responseQueue.front();
376}
Vernon Mauerya3702c12019-05-22 13:20:59 -0700377
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000378/**
379 * @brief This command is used for bridging ipmi message between channels.
380 * @param channelNumber - channel number to send message to
381 * @param authenticationEnabled - authentication.
382 * @param encryptionEnabled - encryption
383 * @param Tracking - track request
384 * @param msg - message data
385 *
386 * @return IPMI completion code plus response data on success.
387 * - rspData - response data
388 **/
389ipmi::RspType<std::vector<uint8_t> // responseData
390 >
391 ipmiAppSendMessage(const uint4_t channelNumber,
392 const bool authenticationEnabled,
393 const bool encryptionEnabled, const uint2_t tracking,
394 ipmi::message::Payload &msg)
395{
Vernon Mauerya3702c12019-05-22 13:20:59 -0700396 // check message fields:
397 // encryption not supported
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000398 if (encryptionEnabled)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700399 {
400 phosphor::logging::log<phosphor::logging::level::INFO>(
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000401 "ipmiAppSendMessage, encryption not supported");
402 return ipmi::responseParmOutOfRange();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700403 }
404
405 // authentication not supported
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000406 if (authenticationEnabled)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700407 {
408 phosphor::logging::log<phosphor::logging::level::INFO>(
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000409 "ipmiAppSendMessage, authentication not supported");
410 return ipmi::responseParmOutOfRange();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700411 }
412
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000413 ipmi::Cc returnVal;
414 std::vector<uint8_t> rspData(ipmbMaxFrameLength);
415 size_t dataLength = 0;
416 std::vector<uint8_t> unpackMsg;
417
418 auto channelNo = static_cast<const uint8_t>(channelNumber);
419 // Get the channel number
420 switch (channelNo)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700421 {
422 // we only handle ipmb for now
423 case targetChannelIpmb:
424 case targetChannelOtherLan:
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000425 if (msg.unpack(unpackMsg) || !msg.fullyUnpacked())
426 {
427 return ipmi::responseReqDataLenInvalid();
428 }
429
430 returnVal = bridging.handleIpmbChannel(
431 static_cast<const uint8_t>(tracking), unpackMsg, rspData);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700432 break;
433 // fall through to default
434 case targetChannelIcmb10:
435 case targetChannelIcmb09:
436 case targetChannelLan:
437 case targetChannelSerialModem:
438 case targetChannelPciSmbus:
439 case targetChannelSmbus10:
440 case targetChannelSmbus20:
441 case targetChannelSystemInterface:
442 default:
443 phosphor::logging::log<phosphor::logging::level::INFO>(
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000444 "ipmiAppSendMessage, TargetChannel invalid");
445 return ipmi::responseParmOutOfRange();
446 }
447 if (returnVal != ipmi::ccSuccess)
448 {
449 return ipmi::response(returnVal);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700450 }
451
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000452 return ipmi::responseSuccess(rspData);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700453}
454
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000455/**
456 * @brief This command is used to Get data from the receive message queue.
457 * This command should be executed executed via system interface only.
458 *
459 * @return IPMI completion code plus response data on success.
460 * - channelNumber
461 * - messageData
462 **/
463
464ipmi::RspType<uint8_t, // channelNumber
465 std::vector<uint8_t> // messageData
466 >
467 ipmiAppGetMessage()
Vernon Mauerya3702c12019-05-22 13:20:59 -0700468{
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000469 uint8_t channelData = 0;
470 std::vector<uint8_t> res(ipmbMaxFrameLength);
471 size_t dataLength = 0;
472
473 if (!bridging.getResponseQueueSize())
Vernon Mauerya3702c12019-05-22 13:20:59 -0700474 {
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000475 constexpr ipmi::Cc ipmiGetMessageCmdDataNotAvailable = 0x80;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700476 phosphor::logging::log<phosphor::logging::level::INFO>(
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000477 "ipmiAppGetMessage, no data available");
478 return ipmi::response(ipmiGetMessageCmdDataNotAvailable);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700479 }
480
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000481 // channel number set.
482 channelData |= static_cast<uint8_t>(targetChannelSystemInterface) & 0x0F;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700483
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000484 // Priviledge level set.
485 channelData |= SYSTEM_INTERFACE & 0xF0;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700486
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000487 // Get the first message from queue
488 auto respQueueItem = bridging.getMessageFromQueue();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700489
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000490 // construct response data.
491 respQueueItem.ipmbToi2cConstruct(res.data(), &dataLength);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700492
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000493 // Remove the message from queue
494 bridging.eraseMessageFromQueue();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700495
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000496 // resizing the rspData to its correct length
497 res.resize(dataLength);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700498
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000499 return ipmi::responseSuccess(channelData, res);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700500}
501
Yong Lic3580e92019-08-15 14:36:47 +0800502std::size_t Bridging::getResponseQueueSize()
Vernon Mauerya3702c12019-05-22 13:20:59 -0700503{
Yong Lic3580e92019-08-15 14:36:47 +0800504 return responseQueue.size();
505}
Vernon Mauerya3702c12019-05-22 13:20:59 -0700506
Yong Lic3580e92019-08-15 14:36:47 +0800507/**
508@brief This command is used to retrive present message available states.
509
510@return IPMI completion code plus Flags as response data on success.
511**/
512ipmi::RspType<std::bitset<8>> ipmiAppGetMessageFlags()
513{
514 std::bitset<8> getMsgFlagsRes;
515
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000516 // set event message buffer bit
517 if (!eventMessageBufferFlag)
518 {
519 getMsgFlagsRes.set(getMsgFlagEventMessageBit);
520 }
521 else
522 {
523 getMsgFlagsRes.reset(getMsgFlagEventMessageBit);
524 }
Yong Lic3580e92019-08-15 14:36:47 +0800525
526 // set message fields
527 if (bridging.getResponseQueueSize() > 0)
528 {
529 getMsgFlagsRes.set(getMsgFlagReceiveMessageBit);
530 }
531 else
532 {
533 getMsgFlagsRes.reset(getMsgFlagReceiveMessageBit);
534 }
535
536 try
537 {
538 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
539 ipmi::Value variant = ipmi::getDbusProperty(
540 *dbus, wdtService, wdtObjPath, wdtInterface, wdtInterruptFlagProp);
541 if (std::get<bool>(variant))
542 {
543 getMsgFlagsRes.set(getMsgFlagWatchdogPreTimeOutBit);
544 }
545 }
546 catch (sdbusplus::exception::SdBusError &e)
547 {
548 phosphor::logging::log<phosphor::logging::level::ERR>(
549 "ipmiAppGetMessageFlags, dbus call exception");
550 return ipmi::responseUnspecifiedError();
551 }
552
553 return ipmi::responseSuccess(getMsgFlagsRes);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700554}
555
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000556/** @brief This command is used to flush unread data from the receive
557 * message queue
558 * @param receiveMessage - clear receive message queue
559 * @param eventMsgBufFull - clear event message buffer full
560 * @param reserved2 - reserved bit
561 * @param watchdogTimeout - clear watchdog pre-timeout interrupt flag
562 * @param reserved1 - reserved bit
563 * @param oem0 - clear OEM 0 data
564 * @param oem1 - clear OEM 1 data
565 * @param oem2 - clear OEM 2 data
566
567 * @return IPMI completion code on success
568 */
569ipmi::RspType<> ipmiAppClearMessageFlags(bool receiveMessage,
570 bool eventMsgBufFull, bool reserved2,
571 bool watchdogTimeout, bool reserved1,
572 bool oem0, bool oem1, bool oem2)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700573{
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000574 if (reserved1 || reserved2)
575 {
576 return ipmi::responseInvalidFieldRequest();
577 }
Vernon Mauerya3702c12019-05-22 13:20:59 -0700578
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000579 if (receiveMessage)
580 {
581 bridging.clearResponseQueue();
582 }
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000583
584 if (eventMessageBufferFlag != true && eventMsgBufFull == true)
585 {
586 eventMessageBufferFlag = true;
587 }
588
jayaprakash Mutyala43539cb2019-11-25 12:37:27 +0000589 try
590 {
591 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
592 ipmi::setDbusProperty(*dbus, wdtService, wdtObjPath, wdtInterface,
593 wdtInterruptFlagProp, false);
594 }
595 catch (const sdbusplus::exception::SdBusError &e)
596 {
597 phosphor::logging::log<phosphor::logging::level::ERR>(
598 "ipmiAppClearMessageFlags: can't Clear/Set "
599 "PreTimeoutInterruptOccurFlag");
600 return ipmi::responseUnspecifiedError();
601 }
602
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000603 return ipmi::responseSuccess();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700604}
605
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000606using systemEventType = std::tuple<
607 uint16_t, // Generator ID
608 uint32_t, // Timestamp
609 uint8_t, // Sensor Type
610 uint8_t, // EvM Rev
611 uint8_t, // Sensor Number
612 uint7_t, // Event Type
613 bool, // Event Direction
614 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
615using oemTsEventType = std::tuple<
616 uint32_t, // Timestamp
617 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
618using oemEventType =
619 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
620
621/** @brief implements of Read event message buffer command
622 *
623 * @returns IPMI completion code plus response data
624 * - recordID - SEL Record ID
625 * - recordType - Record Type
626 * - generatorID - Generator ID
627 * - timeStamp - Timestamp
628 * - sensorType - Sensor Type
629 * - eventMsgFormatRev - Event Message format version
630 * - sensorNumber - Sensor Number
631 * - eventType - Event Type
632 * - eventDir - Event Direction
633 * - eventData - Event Data field
634 */
635ipmi::RspType<uint16_t, // Record ID
636 uint8_t, // Record Type
637 std::variant<systemEventType, oemTsEventType,
638 oemEventType>> // Record Content
639 ipmiAppReadEventMessageBuffer()
640{
641 uint16_t recordId =
642 static_cast<uint16_t>(0x5555); // recordId: 0x55 << 8 | 0x55
643 uint16_t generatorId =
644 static_cast<uint16_t>(0xA741); // generatorId: 0xA7 << 8 | 0x41
645 constexpr uint8_t recordType = 0xC0;
646 constexpr uint8_t eventMsgFormatRev = 0x3A;
647 constexpr uint8_t sensorNumber = 0xFF;
648
649 // TODO need to be implemented.
650 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
651 // All '0xFF' since unused.
652 eventData.fill(0xFF);
653
654 // Set the event message buffer flag
655 eventMessageBufferFlag = true;
656
657 return ipmi::responseSuccess(
658 recordId, recordType,
659 systemEventType{generatorId, 0, 0, eventMsgFormatRev, sensorNumber,
660 static_cast<uint7_t>(0), false, eventData});
661}
662
Vernon Mauerya3702c12019-05-22 13:20:59 -0700663static void register_bridging_functions() __attribute__((constructor));
664static void register_bridging_functions()
665{
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000666 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
667 ipmi::app::cmdClearMessageFlags,
668 ipmi::Privilege::User, ipmiAppClearMessageFlags);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700669
Yong Lic3580e92019-08-15 14:36:47 +0800670 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
671 ipmi::app::cmdGetMessageFlags, ipmi::Privilege::User,
672 ipmiAppGetMessageFlags);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700673
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000674 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
675 ipmi::app::cmdGetMessage, ipmi::Privilege::User,
676 ipmiAppGetMessage);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700677
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000678 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
679 ipmi::app::cmdSendMessage, ipmi::Privilege::User,
680 ipmiAppSendMessage);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700681
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000682 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
683 ipmi::app::cmdReadEventMessageBuffer,
684 ipmi::Privilege::User, ipmiAppReadEventMessageBuffer);
685
Vernon Mauerya3702c12019-05-22 13:20:59 -0700686 return;
687}