blob: 060c1702b0c72e3cfe7b39e3726385693a1b5594 [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
Vernon Mauerya3702c12019-05-22 13:20:59 -070017#include <bridgingcommands.hpp>
Vernon Mauery15419dd2019-05-24 09:40:30 -070018#include <ipmid/api.hpp>
Yong Lic3580e92019-08-15 14:36:47 +080019#include <ipmid/utils.hpp>
Yong Liedbb4082020-03-06 17:38:25 +080020#include <manufacturingcommands.hpp>
Vernon Mauerya3702c12019-05-22 13:20:59 -070021#include <phosphor-logging/log.hpp>
22#include <sdbusplus/bus.hpp>
23#include <sdbusplus/bus/match.hpp>
24#include <sdbusplus/message.hpp>
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +000025#include <storagecommands.hpp>
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +000026#include <user_channel/channel_layer.hpp>
James Feistfcd2d3a2020-05-28 10:38:15 -070027
28#include <bitset>
29#include <cstring>
Vernon Mauerya3702c12019-05-22 13:20:59 -070030#include <vector>
31
James Feistfcd2d3a2020-05-28 10:38:15 -070032static constexpr const char* wdtService = "xyz.openbmc_project.Watchdog";
33static constexpr const char* wdtInterface =
Yong Lic3580e92019-08-15 14:36:47 +080034 "xyz.openbmc_project.State.Watchdog";
James Feistfcd2d3a2020-05-28 10:38:15 -070035static constexpr const char* wdtObjPath = "/xyz/openbmc_project/watchdog/host0";
36static constexpr const char* wdtInterruptFlagProp =
Yong Lic3580e92019-08-15 14:36:47 +080037 "PreTimeoutInterruptOccurFlag";
38
James Feistfcd2d3a2020-05-28 10:38:15 -070039static constexpr const char* ipmbBus = "xyz.openbmc_project.Ipmi.Channel.Ipmb";
40static constexpr const char* ipmbObj = "/xyz/openbmc_project/Ipmi/Channel/Ipmb";
41static constexpr const char* ipmbIntf = "org.openbmc.Ipmb";
Vernon Mauerya3702c12019-05-22 13:20:59 -070042
43static Bridging bridging;
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +000044static bool eventMessageBufferFlag = false;
Vernon Mauerya3702c12019-05-22 13:20:59 -070045
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +000046void Bridging::clearResponseQueue()
47{
48 responseQueue.clear();
49}
50
Vernon Mauerya3702c12019-05-22 13:20:59 -070051/**
52 * @brief utils for checksum
53 */
James Feistfcd2d3a2020-05-28 10:38:15 -070054static bool ipmbChecksumValidate(const uint8_t* data, uint8_t length)
Vernon Mauerya3702c12019-05-22 13:20:59 -070055{
56 if (data == nullptr)
57 {
58 return false;
59 }
60
61 uint8_t checksum = 0;
62
63 for (uint8_t idx = 0; idx < length; idx++)
64 {
65 checksum += data[idx];
66 }
67
68 if (0 == checksum)
69 {
70 return true;
71 }
72
73 return false;
74}
75
James Feistfcd2d3a2020-05-28 10:38:15 -070076static uint8_t ipmbChecksumCompute(uint8_t* data, uint8_t length)
Vernon Mauerya3702c12019-05-22 13:20:59 -070077{
78 if (data == nullptr)
79 {
80 return 0;
81 }
82
83 uint8_t checksum = 0;
84
85 for (uint8_t idx = 0; idx < length; idx++)
86 {
87 checksum += data[idx];
88 }
89
90 checksum = (~checksum) + 1;
91 return checksum;
92}
93
Jason M. Bills97d1fb32025-04-23 08:57:35 -070094static inline bool ipmbConnectionHeaderChecksumValidate(
95 const ipmbHeader* ipmbHeader)
Vernon Mauerya3702c12019-05-22 13:20:59 -070096{
James Feistfcd2d3a2020-05-28 10:38:15 -070097 return ipmbChecksumValidate(reinterpret_cast<const uint8_t*>(ipmbHeader),
Vernon Mauerya3702c12019-05-22 13:20:59 -070098 ipmbConnectionHeaderLength);
99}
100
Jason M. Bills97d1fb32025-04-23 08:57:35 -0700101static inline bool ipmbDataChecksumValidate(const ipmbHeader* ipmbHeader,
102 size_t length)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700103{
James Feistfcd2d3a2020-05-28 10:38:15 -0700104 return ipmbChecksumValidate((reinterpret_cast<const uint8_t*>(ipmbHeader) +
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000105 ipmbConnectionHeaderLength),
106 (length - ipmbConnectionHeaderLength));
Vernon Mauerya3702c12019-05-22 13:20:59 -0700107}
108
PavanKumarIntel919075d2023-10-30 16:41:19 +0000109static bool isFrameValid(const ipmbHeader* frame, size_t length)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700110{
111 if ((length < ipmbMinFrameLength) || (length > ipmbMaxFrameLength))
112 {
113 return false;
114 }
115
116 if (false == ipmbConnectionHeaderChecksumValidate(frame))
117 {
118 return false;
119 }
120
121 if (false == ipmbDataChecksumValidate(frame, length))
122 {
123 return false;
124 }
125
126 return true;
127}
128
James Feistfcd2d3a2020-05-28 10:38:15 -0700129IpmbRequest::IpmbRequest(const ipmbHeader* ipmbBuffer, size_t bufferLength)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700130{
131 address = ipmbBuffer->Header.Req.address;
132 netFn = ipmbNetFnGet(ipmbBuffer->Header.Req.rsNetFnLUN);
133 rsLun = ipmbLunFromNetFnLunGet(ipmbBuffer->Header.Req.rsNetFnLUN);
134 rqSA = ipmbBuffer->Header.Req.rqSA;
135 seq = ipmbSeqGet(ipmbBuffer->Header.Req.rqSeqLUN);
136 rqLun = ipmbLunFromSeqLunGet(ipmbBuffer->Header.Req.rqSeqLUN);
137 cmd = ipmbBuffer->Header.Req.cmd;
138
Patrick Williams1bcced02024-08-16 15:20:24 -0400139 size_t dataLength =
140 bufferLength - (ipmbConnectionHeaderLength +
141 ipmbRequestDataHeaderLength + ipmbChecksumSize);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700142
143 if (dataLength > 0)
144 {
145 data.insert(data.end(), ipmbBuffer->Header.Req.data,
146 &ipmbBuffer->Header.Req.data[dataLength]);
147 }
148}
149
150IpmbResponse::IpmbResponse(uint8_t address, uint8_t netFn, uint8_t rqLun,
151 uint8_t rsSA, uint8_t seq, uint8_t rsLun,
152 uint8_t cmd, uint8_t completionCode,
James Feistfcd2d3a2020-05-28 10:38:15 -0700153 std::vector<uint8_t>& inputData) :
Patrick Williams1bcced02024-08-16 15:20:24 -0400154 address(address), netFn(netFn), rqLun(rqLun), rsSA(rsSA), seq(seq),
155 rsLun(rsLun), cmd(cmd), completionCode(completionCode)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700156{
157 data.reserve(ipmbMaxDataSize);
158
159 if (inputData.size() > 0)
160 {
161 data = std::move(inputData);
162 }
163}
164
James Feistfcd2d3a2020-05-28 10:38:15 -0700165void IpmbResponse::ipmbToi2cConstruct(uint8_t* buffer, size_t* bufferLength)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700166{
James Feistfcd2d3a2020-05-28 10:38:15 -0700167 ipmbHeader* ipmbBuffer = (ipmbHeader*)buffer;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700168
169 ipmbBuffer->Header.Resp.address = address;
170 ipmbBuffer->Header.Resp.rqNetFnLUN = ipmbNetFnLunSet(netFn, rqLun);
171 ipmbBuffer->Header.Resp.rsSA = rsSA;
172 ipmbBuffer->Header.Resp.rsSeqLUN = ipmbSeqLunSet(seq, rsLun);
173 ipmbBuffer->Header.Resp.cmd = cmd;
174 ipmbBuffer->Header.Resp.completionCode = completionCode;
175
176 ipmbBuffer->Header.Resp.checksum1 = ipmbChecksumCompute(
177 buffer, ipmbConnectionHeaderLength - ipmbChecksumSize);
178
179 if (data.size() > 0)
180 {
181 std::copy(
182 data.begin(), data.end(),
183 &buffer[ipmbConnectionHeaderLength + ipmbResponseDataHeaderLength]);
184 }
185
186 *bufferLength = data.size() + ipmbResponseDataHeaderLength +
187 ipmbConnectionHeaderLength + ipmbChecksumSize;
188
189 buffer[*bufferLength - ipmbChecksumSize] =
190 ipmbChecksumCompute(&buffer[ipmbChecksum2StartOffset],
191 (ipmbResponseDataHeaderLength + data.size()));
192}
193
Patrick Williamsf944d2e2022-07-22 19:26:52 -0500194void IpmbRequest::prepareRequest(sdbusplus::message_t& mesg)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700195{
196 mesg.append(ipmbMeChannelNum, netFn, rqLun, cmd, data);
197}
198
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530199static constexpr unsigned int makeCmdKey(unsigned int netFn, unsigned int cmd)
200{
201 return (netFn << 8) | cmd;
202}
203
204static constexpr bool isMeCmdAllowed(uint8_t netFn, uint8_t cmd)
205{
206 constexpr uint8_t netFnMeOEM = 0x2E;
Yong Liedbb4082020-03-06 17:38:25 +0800207 constexpr uint8_t netFnMeOEMGeneral = 0x3E;
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530208 constexpr uint8_t cmdMeOemSendRawPeci = 0x40;
209 constexpr uint8_t cmdMeOemAggSendRawPeci = 0x41;
210 constexpr uint8_t cmdMeOemCpuPkgConfWrite = 0x43;
211 constexpr uint8_t cmdMeOemCpuPciConfWrite = 0x45;
212 constexpr uint8_t cmdMeOemReadMemSmbus = 0x47;
213 constexpr uint8_t cmdMeOemWriteMemSmbus = 0x48;
214 constexpr uint8_t cmdMeOemSlotIpmb = 0x51;
Matt Simmering80d4d5f2023-02-15 15:18:51 -0800215 constexpr uint8_t cmdMeOemSlotI2cControllerWriteRead = 0x52;
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530216 constexpr uint8_t cmdMeOemSendRawPmbus = 0xD9;
217 constexpr uint8_t cmdMeOemUnlockMeRegion = 0xE7;
218 constexpr uint8_t cmdMeOemAggSendRawPmbus = 0xEC;
219
220 switch (makeCmdKey(netFn, cmd))
221 {
Matt Simmering80d4d5f2023-02-15 15:18:51 -0800222 // Restrict ME Controller write command
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530223 case makeCmdKey(ipmi::netFnApp, ipmi::app::cmdMasterWriteRead):
224 // Restrict ME OEM commands
225 case makeCmdKey(netFnMeOEM, cmdMeOemSendRawPeci):
226 case makeCmdKey(netFnMeOEM, cmdMeOemAggSendRawPeci):
227 case makeCmdKey(netFnMeOEM, cmdMeOemCpuPkgConfWrite):
228 case makeCmdKey(netFnMeOEM, cmdMeOemCpuPciConfWrite):
229 case makeCmdKey(netFnMeOEM, cmdMeOemReadMemSmbus):
230 case makeCmdKey(netFnMeOEM, cmdMeOemWriteMemSmbus):
Yong Liedbb4082020-03-06 17:38:25 +0800231 case makeCmdKey(netFnMeOEMGeneral, cmdMeOemSlotIpmb):
Matt Simmering80d4d5f2023-02-15 15:18:51 -0800232 case makeCmdKey(netFnMeOEMGeneral, cmdMeOemSlotI2cControllerWriteRead):
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530233 case makeCmdKey(netFnMeOEM, cmdMeOemSendRawPmbus):
234 case makeCmdKey(netFnMeOEM, cmdMeOemUnlockMeRegion):
235 case makeCmdKey(netFnMeOEM, cmdMeOemAggSendRawPmbus):
236 return false;
237 default:
238 return true;
239 }
240}
241
Patrick Williams1bcced02024-08-16 15:20:24 -0400242ipmi::Cc Bridging::handleIpmbChannel(
243 ipmi::Context::ptr& ctx, const uint8_t tracking,
244 const std::vector<uint8_t>& msgData, std::vector<uint8_t>& rspData)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700245{
Yong Liedbb4082020-03-06 17:38:25 +0800246 ipmi::Manufacturing mtm;
247
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000248 size_t msgLen = msgData.size();
249 if ((msgLen < ipmbMinFrameLength) || (msgLen > ipmbMaxFrameLength))
Vernon Mauerya3702c12019-05-22 13:20:59 -0700250 {
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000251 phosphor::logging::log<phosphor::logging::level::INFO>(
252 "handleIpmbChannel, IPMB data length is invalid");
253 return ipmi::ccReqDataLenInvalid;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700254 }
255
srikanta mondale95b0e52020-03-18 10:35:19 +0000256 // Bridging to ME requires Administrator lvl
257 if ((ctx->priv) != ipmi::Privilege::Admin)
258 {
259 return ipmi::ccInsufficientPrivilege;
260 }
261
James Feistfcd2d3a2020-05-28 10:38:15 -0700262 auto sendMsgReqData = reinterpret_cast<const ipmbHeader*>(msgData.data());
Vernon Mauerya3702c12019-05-22 13:20:59 -0700263
264 // allow bridging to ME only
Matt Simmering80d4d5f2023-02-15 15:18:51 -0800265 if (sendMsgReqData->Header.Req.address != ipmbMeTargetAddress)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700266 {
267 phosphor::logging::log<phosphor::logging::level::INFO>(
268 "handleIpmbChannel, IPMB address invalid");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000269 return ipmi::ccParmOutOfRange;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700270 }
271
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530272 constexpr uint8_t shiftLUN = 2;
Yong Liedbb4082020-03-06 17:38:25 +0800273 if (mtm.getMfgMode() == ipmi::SpecialMode::none)
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530274 {
Yong Liedbb4082020-03-06 17:38:25 +0800275 if (!isMeCmdAllowed((sendMsgReqData->Header.Req.rsNetFnLUN >> shiftLUN),
276 sendMsgReqData->Header.Req.cmd))
277 {
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000278 constexpr ipmi::Cc ccCmdNotSupportedInPresentState = 0xD5;
279 return ccCmdNotSupportedInPresentState;
Yong Liedbb4082020-03-06 17:38:25 +0800280 }
Richard Marian Thomaiyare646a252019-11-20 22:54:03 +0530281 }
282
Vernon Mauerya3702c12019-05-22 13:20:59 -0700283 // check allowed modes
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000284 if (tracking != modeNoTracking && tracking != modeTrackRequest)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700285 {
286 phosphor::logging::log<phosphor::logging::level::INFO>(
287 "handleIpmbChannel, mode not supported");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000288 return ipmi::ccParmOutOfRange;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700289 }
290
291 // check if request contains valid IPMB frame
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000292 if (!isFrameValid(sendMsgReqData, msgLen))
Vernon Mauerya3702c12019-05-22 13:20:59 -0700293 {
294 phosphor::logging::log<phosphor::logging::level::INFO>(
295 "handleIpmbChannel, IPMB frame invalid");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000296 return ipmi::ccParmOutOfRange;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700297 }
298
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000299 auto ipmbRequest = IpmbRequest(sendMsgReqData, msgLen);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700300
srikanta mondal36b3a872020-03-17 22:20:47 +0000301 typedef std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t,
302 std::vector<uint8_t>>
303 IPMBResponse;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700304
305 // send request to IPMB
srikanta mondal36b3a872020-03-17 22:20:47 +0000306 boost::system::error_code ec;
307 auto ipmbResponse = ctx->bus->yield_method_call<IPMBResponse>(
308 ctx->yield, ec, ipmbBus, ipmbObj, ipmbIntf, "sendRequest",
309 ipmbMeChannelNum, ipmbRequest.netFn, ipmbRequest.rqLun, ipmbRequest.cmd,
310 ipmbRequest.data);
311 if (ec)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700312 {
313 phosphor::logging::log<phosphor::logging::level::ERR>(
314 "handleIpmbChannel, dbus call exception");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000315 return ipmi::ccUnspecifiedError;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700316 }
317
Jason M. Bills48437462025-01-13 14:26:02 -0800318 std::vector<uint8_t> dataReceived{};
Vernon Mauerya3702c12019-05-22 13:20:59 -0700319 int status = -1;
320 uint8_t netFn = 0, lun = 0, cmd = 0, cc = 0;
321
322 std::tie(status, netFn, lun, cmd, cc, dataReceived) = ipmbResponse;
323
Patrick Williams1bcced02024-08-16 15:20:24 -0400324 auto respReceived =
325 IpmbResponse(ipmbRequest.rqSA, netFn, lun, ipmbRequest.address,
326 ipmbRequest.seq, lun, cmd, cc, dataReceived);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700327
328 // check IPMB layer status
329 if (status)
330 {
331 phosphor::logging::log<phosphor::logging::level::WARNING>(
332 "handleIpmbChannel, ipmb returned non zero status");
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000333 return ipmi::ccResponseError;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700334 }
335
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000336 switch (tracking)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700337 {
338 case modeNoTracking:
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000339 {
340 if (getResponseQueueSize() == responseQueueMaxSize)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700341 {
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000342 return ipmi::ccBusy;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700343 }
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000344 insertMessageInQueue(respReceived);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700345 break;
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000346 }
Vernon Mauerya3702c12019-05-22 13:20:59 -0700347 case modeTrackRequest:
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000348 {
349 size_t dataLength = 0;
350 respReceived.ipmbToi2cConstruct(rspData.data(), &dataLength);
351 // resizing the rspData to its correct length
352 rspData.resize(dataLength);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700353 break;
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000354 }
Vernon Mauerya3702c12019-05-22 13:20:59 -0700355 }
356
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000357 return ipmi::ccSuccess;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700358}
359
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000360void Bridging::insertMessageInQueue(IpmbResponse msg)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700361{
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000362 responseQueue.insert(responseQueue.end(), std::move(msg));
363}
Vernon Mauerya3702c12019-05-22 13:20:59 -0700364
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000365void Bridging::eraseMessageFromQueue()
366{
367 responseQueue.erase(responseQueue.begin());
368}
Vernon Mauerya3702c12019-05-22 13:20:59 -0700369
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000370IpmbResponse Bridging::getMessageFromQueue()
371{
372 return responseQueue.front();
373}
Vernon Mauerya3702c12019-05-22 13:20:59 -0700374
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000375/**
376 * @brief This command is used for bridging ipmi message between channels.
377 * @param channelNumber - channel number to send message to
378 * @param authenticationEnabled - authentication.
379 * @param encryptionEnabled - encryption
380 * @param Tracking - track request
381 * @param msg - message data
382 *
383 * @return IPMI completion code plus response data on success.
384 * - rspData - response data
385 **/
386ipmi::RspType<std::vector<uint8_t> // responseData
387 >
Vernon Mauerydcff1502022-09-28 11:12:46 -0700388 ipmiAppSendMessage(ipmi::Context::ptr& ctx, const uint4_t channelNumber,
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000389 const bool authenticationEnabled,
390 const bool encryptionEnabled, const uint2_t tracking,
James Feistfcd2d3a2020-05-28 10:38:15 -0700391 ipmi::message::Payload& msg)
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000392{
Vernon Mauerya3702c12019-05-22 13:20:59 -0700393 // check message fields:
394 // encryption not supported
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000395 if (encryptionEnabled)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700396 {
397 phosphor::logging::log<phosphor::logging::level::INFO>(
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000398 "ipmiAppSendMessage, encryption not supported");
399 return ipmi::responseParmOutOfRange();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700400 }
401
402 // authentication not supported
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000403 if (authenticationEnabled)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700404 {
405 phosphor::logging::log<phosphor::logging::level::INFO>(
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000406 "ipmiAppSendMessage, authentication not supported");
407 return ipmi::responseParmOutOfRange();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700408 }
409
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000410 ipmi::Cc returnVal;
411 std::vector<uint8_t> rspData(ipmbMaxFrameLength);
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000412 std::vector<uint8_t> unpackMsg;
413
Vernon Mauerydcff1502022-09-28 11:12:46 -0700414 auto channelNo = static_cast<uint8_t>(channelNumber);
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000415 // Get the channel number
416 switch (channelNo)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700417 {
418 // we only handle ipmb for now
419 case targetChannelIpmb:
420 case targetChannelOtherLan:
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000421 if (msg.unpack(unpackMsg) || !msg.fullyUnpacked())
422 {
423 return ipmi::responseReqDataLenInvalid();
424 }
425
426 returnVal = bridging.handleIpmbChannel(
Vernon Mauerydcff1502022-09-28 11:12:46 -0700427 ctx, static_cast<uint8_t>(tracking), unpackMsg, rspData);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700428 break;
429 // fall through to default
430 case targetChannelIcmb10:
431 case targetChannelIcmb09:
432 case targetChannelLan:
433 case targetChannelSerialModem:
434 case targetChannelPciSmbus:
435 case targetChannelSmbus10:
436 case targetChannelSmbus20:
437 case targetChannelSystemInterface:
438 default:
439 phosphor::logging::log<phosphor::logging::level::INFO>(
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000440 "ipmiAppSendMessage, TargetChannel invalid");
441 return ipmi::responseParmOutOfRange();
442 }
443 if (returnVal != ipmi::ccSuccess)
444 {
445 return ipmi::response(returnVal);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700446 }
447
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000448 return ipmi::responseSuccess(rspData);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700449}
450
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000451/**
452 * @brief This command is used to Get data from the receive message queue.
453 * This command should be executed executed via system interface only.
454 *
455 * @return IPMI completion code plus response data on success.
456 * - channelNumber
457 * - messageData
458 **/
459
460ipmi::RspType<uint8_t, // channelNumber
461 std::vector<uint8_t> // messageData
462 >
Vernon Mauerydcff1502022-09-28 11:12:46 -0700463 ipmiAppGetMessage(ipmi::Context::ptr& ctx)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700464{
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +0000465 ipmi::ChannelInfo chInfo;
466
467 try
468 {
469 getChannelInfo(ctx->channel, chInfo);
470 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500471 catch (const sdbusplus::exception_t& e)
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +0000472 {
473 phosphor::logging::log<phosphor::logging::level::ERR>(
474 "ipmiAppGetMessage: Failed to get Channel Info",
475 phosphor::logging::entry("MSG: %s", e.description()));
476 return ipmi::responseUnspecifiedError();
477 }
478 if (chInfo.mediumType !=
479 static_cast<uint8_t>(ipmi::EChannelMediumType::systemInterface))
480 {
481 phosphor::logging::log<phosphor::logging::level::ERR>(
482 "ipmiAppGetMessage: Error - supported only in System(SMS) "
483 "interface");
484 return ipmi::responseCommandNotAvailable();
485 }
486
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000487 uint8_t channelData = 0;
488 std::vector<uint8_t> res(ipmbMaxFrameLength);
489 size_t dataLength = 0;
490
491 if (!bridging.getResponseQueueSize())
Vernon Mauerya3702c12019-05-22 13:20:59 -0700492 {
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000493 constexpr ipmi::Cc ipmiGetMessageCmdDataNotAvailable = 0x80;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700494 phosphor::logging::log<phosphor::logging::level::INFO>(
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000495 "ipmiAppGetMessage, no data available");
496 return ipmi::response(ipmiGetMessageCmdDataNotAvailable);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700497 }
498
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000499 // channel number set.
500 channelData |= static_cast<uint8_t>(targetChannelSystemInterface) & 0x0F;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700501
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000502 // Priviledge level set.
503 channelData |= SYSTEM_INTERFACE & 0xF0;
Vernon Mauerya3702c12019-05-22 13:20:59 -0700504
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000505 // Get the first message from queue
506 auto respQueueItem = bridging.getMessageFromQueue();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700507
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000508 // construct response data.
509 respQueueItem.ipmbToi2cConstruct(res.data(), &dataLength);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700510
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000511 // Remove the message from queue
512 bridging.eraseMessageFromQueue();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700513
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000514 // resizing the rspData to its correct length
515 res.resize(dataLength);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700516
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000517 return ipmi::responseSuccess(channelData, res);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700518}
519
Yong Lic3580e92019-08-15 14:36:47 +0800520std::size_t Bridging::getResponseQueueSize()
Vernon Mauerya3702c12019-05-22 13:20:59 -0700521{
Yong Lic3580e92019-08-15 14:36:47 +0800522 return responseQueue.size();
523}
Vernon Mauerya3702c12019-05-22 13:20:59 -0700524
Yong Lic3580e92019-08-15 14:36:47 +0800525/**
526@brief This command is used to retrive present message available states.
527
528@return IPMI completion code plus Flags as response data on success.
529**/
Vernon Mauerydcff1502022-09-28 11:12:46 -0700530ipmi::RspType<std::bitset<8>> ipmiAppGetMessageFlags(ipmi::Context::ptr& ctx)
Yong Lic3580e92019-08-15 14:36:47 +0800531{
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +0000532 ipmi::ChannelInfo chInfo;
533
534 try
535 {
536 getChannelInfo(ctx->channel, chInfo);
537 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500538 catch (const sdbusplus::exception_t& e)
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +0000539 {
540 phosphor::logging::log<phosphor::logging::level::ERR>(
541 "ipmiAppGetMessageFlags: Failed to get Channel Info",
542 phosphor::logging::entry("MSG: %s", e.description()));
543 return ipmi::responseUnspecifiedError();
544 }
545 if (chInfo.mediumType !=
546 static_cast<uint8_t>(ipmi::EChannelMediumType::systemInterface))
547 {
548 phosphor::logging::log<phosphor::logging::level::ERR>(
549 "ipmiAppGetMessageFlags: Error - supported only in System(SMS) "
550 "interface");
551 return ipmi::responseCommandNotAvailable();
552 }
553
Yong Lic3580e92019-08-15 14:36:47 +0800554 std::bitset<8> getMsgFlagsRes;
555
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000556 // set event message buffer bit
557 if (!eventMessageBufferFlag)
558 {
559 getMsgFlagsRes.set(getMsgFlagEventMessageBit);
560 }
561 else
562 {
563 getMsgFlagsRes.reset(getMsgFlagEventMessageBit);
564 }
Yong Lic3580e92019-08-15 14:36:47 +0800565
566 // set message fields
567 if (bridging.getResponseQueueSize() > 0)
568 {
569 getMsgFlagsRes.set(getMsgFlagReceiveMessageBit);
570 }
571 else
572 {
573 getMsgFlagsRes.reset(getMsgFlagReceiveMessageBit);
574 }
575
576 try
577 {
578 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
579 ipmi::Value variant = ipmi::getDbusProperty(
580 *dbus, wdtService, wdtObjPath, wdtInterface, wdtInterruptFlagProp);
581 if (std::get<bool>(variant))
582 {
583 getMsgFlagsRes.set(getMsgFlagWatchdogPreTimeOutBit);
584 }
585 }
Patrick Williamsf944d2e2022-07-22 19:26:52 -0500586 catch (const sdbusplus::exception_t& e)
Yong Lic3580e92019-08-15 14:36:47 +0800587 {
588 phosphor::logging::log<phosphor::logging::level::ERR>(
589 "ipmiAppGetMessageFlags, dbus call exception");
590 return ipmi::responseUnspecifiedError();
591 }
592
593 return ipmi::responseSuccess(getMsgFlagsRes);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700594}
595
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000596/** @brief This command is used to flush unread data from the receive
597 * message queue
598 * @param receiveMessage - clear receive message queue
599 * @param eventMsgBufFull - clear event message buffer full
600 * @param reserved2 - reserved bit
601 * @param watchdogTimeout - clear watchdog pre-timeout interrupt flag
602 * @param reserved1 - reserved bit
603 * @param oem0 - clear OEM 0 data
604 * @param oem1 - clear OEM 1 data
605 * @param oem2 - clear OEM 2 data
606
607 * @return IPMI completion code on success
608 */
Patrick Williams1bcced02024-08-16 15:20:24 -0400609ipmi::RspType<> ipmiAppClearMessageFlags(
610 ipmi::Context::ptr& ctx, bool receiveMessage, bool eventMsgBufFull,
611 bool reserved2, bool watchdogTimeout, bool reserved1, bool /* oem0 */,
612 bool /* oem1 */, bool /* oem2 */)
Vernon Mauerya3702c12019-05-22 13:20:59 -0700613{
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +0000614 ipmi::ChannelInfo chInfo;
615
616 try
617 {
618 getChannelInfo(ctx->channel, chInfo);
619 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500620 catch (const sdbusplus::exception_t& e)
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +0000621 {
622 phosphor::logging::log<phosphor::logging::level::ERR>(
623 "ipmiAppClearMessageFlags: Failed to get Channel Info",
624 phosphor::logging::entry("MSG: %s", e.description()));
625 return ipmi::responseUnspecifiedError();
626 }
627 if (chInfo.mediumType !=
628 static_cast<uint8_t>(ipmi::EChannelMediumType::systemInterface))
629 {
630 phosphor::logging::log<phosphor::logging::level::ERR>(
631 "ipmiAppClearMessageFlags: Error - supported only in System(SMS) "
632 "interface");
633 return ipmi::responseCommandNotAvailable();
634 }
635
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000636 if (reserved1 || reserved2)
637 {
638 return ipmi::responseInvalidFieldRequest();
639 }
Vernon Mauerya3702c12019-05-22 13:20:59 -0700640
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000641 if (receiveMessage)
642 {
643 bridging.clearResponseQueue();
644 }
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000645
646 if (eventMessageBufferFlag != true && eventMsgBufFull == true)
647 {
648 eventMessageBufferFlag = true;
649 }
650
jayaprakash Mutyala43539cb2019-11-25 12:37:27 +0000651 try
652 {
Jayaprakash Mutyala52771792022-05-30 14:51:01 +0000653 if (watchdogTimeout)
654 {
655 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
656 ipmi::setDbusProperty(*dbus, wdtService, wdtObjPath, wdtInterface,
657 wdtInterruptFlagProp, false);
658 }
jayaprakash Mutyala43539cb2019-11-25 12:37:27 +0000659 }
Patrick Williamsf944d2e2022-07-22 19:26:52 -0500660 catch (const sdbusplus::exception_t& e)
jayaprakash Mutyala43539cb2019-11-25 12:37:27 +0000661 {
662 phosphor::logging::log<phosphor::logging::level::ERR>(
663 "ipmiAppClearMessageFlags: can't Clear/Set "
664 "PreTimeoutInterruptOccurFlag");
665 return ipmi::responseUnspecifiedError();
666 }
667
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000668 return ipmi::responseSuccess();
Vernon Mauerya3702c12019-05-22 13:20:59 -0700669}
670
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000671using systemEventType = std::tuple<
672 uint16_t, // Generator ID
673 uint32_t, // Timestamp
674 uint8_t, // Sensor Type
675 uint8_t, // EvM Rev
676 uint8_t, // Sensor Number
677 uint7_t, // Event Type
678 bool, // Event Direction
679 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize>>; // Event Data
680using oemTsEventType = std::tuple<
Patrick Williamsb37abfb2023-05-10 07:50:33 -0500681 uint32_t, // Timestamp
682 std::array<uint8_t, intel_oem::ipmi::sel::oemTsEventSize>>; // Event Data
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000683using oemEventType =
Patrick Williamsb37abfb2023-05-10 07:50:33 -0500684 std::array<uint8_t, intel_oem::ipmi::sel::oemEventSize>; // Event Data
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000685
686/** @brief implements of Read event message buffer command
687 *
688 * @returns IPMI completion code plus response data
689 * - recordID - SEL Record ID
690 * - recordType - Record Type
691 * - generatorID - Generator ID
692 * - timeStamp - Timestamp
693 * - sensorType - Sensor Type
694 * - eventMsgFormatRev - Event Message format version
695 * - sensorNumber - Sensor Number
696 * - eventType - Event Type
697 * - eventDir - Event Direction
698 * - eventData - Event Data field
699 */
Patrick Williamsb37abfb2023-05-10 07:50:33 -0500700ipmi::RspType<uint16_t, // Record ID
701 uint8_t, // Record Type
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000702 std::variant<systemEventType, oemTsEventType,
703 oemEventType>> // Record Content
Vernon Mauerydcff1502022-09-28 11:12:46 -0700704 ipmiAppReadEventMessageBuffer(ipmi::Context::ptr& ctx)
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000705{
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +0000706 ipmi::ChannelInfo chInfo;
707
708 try
709 {
710 getChannelInfo(ctx->channel, chInfo);
711 }
Patrick Williamsbd51e6a2021-10-06 13:09:44 -0500712 catch (const sdbusplus::exception_t& e)
Jayaprakash Mutyalacca21402020-07-16 12:14:10 +0000713 {
714 phosphor::logging::log<phosphor::logging::level::ERR>(
715 "ipmiAppReadEventMessageBuffer: Failed to get Channel Info",
716 phosphor::logging::entry("MSG: %s", e.description()));
717 return ipmi::responseUnspecifiedError();
718 }
719 if (chInfo.mediumType !=
720 static_cast<uint8_t>(ipmi::EChannelMediumType::systemInterface))
721 {
722 phosphor::logging::log<phosphor::logging::level::ERR>(
723 "ipmiAppReadEventMessageBuffer: Error - supported only in "
724 "System(SMS) interface");
725 return ipmi::responseCommandNotAvailable();
726 }
727
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000728 uint16_t recordId =
729 static_cast<uint16_t>(0x5555); // recordId: 0x55 << 8 | 0x55
730 uint16_t generatorId =
731 static_cast<uint16_t>(0xA741); // generatorId: 0xA7 << 8 | 0x41
732 constexpr uint8_t recordType = 0xC0;
733 constexpr uint8_t eventMsgFormatRev = 0x3A;
734 constexpr uint8_t sensorNumber = 0xFF;
735
736 // TODO need to be implemented.
737 std::array<uint8_t, intel_oem::ipmi::sel::systemEventSize> eventData{};
738 // All '0xFF' since unused.
739 eventData.fill(0xFF);
740
741 // Set the event message buffer flag
742 eventMessageBufferFlag = true;
743
744 return ipmi::responseSuccess(
745 recordId, recordType,
746 systemEventType{generatorId, 0, 0, eventMsgFormatRev, sensorNumber,
747 static_cast<uint7_t>(0), false, eventData});
748}
749
Vernon Mauerya3702c12019-05-22 13:20:59 -0700750static void register_bridging_functions() __attribute__((constructor));
751static void register_bridging_functions()
752{
jayaprakash Mutyala405f54a2019-10-18 18:23:27 +0000753 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
754 ipmi::app::cmdClearMessageFlags,
755 ipmi::Privilege::User, ipmiAppClearMessageFlags);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700756
Yong Lic3580e92019-08-15 14:36:47 +0800757 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
758 ipmi::app::cmdGetMessageFlags, ipmi::Privilege::User,
759 ipmiAppGetMessageFlags);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700760
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000761 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
762 ipmi::app::cmdGetMessage, ipmi::Privilege::User,
763 ipmiAppGetMessage);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700764
Deepak Kumar Sahu24d2d562019-07-14 16:05:19 +0000765 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
766 ipmi::app::cmdSendMessage, ipmi::Privilege::User,
767 ipmiAppSendMessage);
Vernon Mauerya3702c12019-05-22 13:20:59 -0700768
jayaprakash Mutyala5ba46872019-11-20 00:02:48 +0000769 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnApp,
770 ipmi::app::cmdReadEventMessageBuffer,
771 ipmi::Privilege::User, ipmiAppReadEventMessageBuffer);
772
Vernon Mauerya3702c12019-05-22 13:20:59 -0700773 return;
774}