-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathipmbbridged.hpp
358 lines (287 loc) · 8.39 KB
/
ipmbbridged.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Copyright 2018 Intel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ipmbdefines.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/container/flat_set.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <sdbusplus/message.hpp>
#include <optional>
#include <vector>
extern "C"
{
#include <i2c/smbus.h>
#include <linux/i2c-dev.h>
}
#ifndef IPMBBRIDGED_HPP
#define IPMBBRIDGED_HPP
/**
* @brief Ipmb return status codes (sendRequest API call)
*/
enum class ipmbResponseStatus
{
success = 0,
error = 1,
invalid_param = 2,
busy = 3,
timeout = 4,
};
/**
* @brief Ipmb outstanding requests defines
*/
constexpr int ipmbMaxOutstandingRequestsCount = 64;
constexpr int ipmbNumberOfTries = 6;
constexpr uint64_t ipmbRequestRetryTimeout = 250; // ms
/**
* @brief Ipmb I2C communication
*/
constexpr uint8_t ipmbI2cNumberOfRetries = 2;
/**
* @brief Ipmb broadcast address
*/
constexpr uint8_t broadcastAddress = 0x0;
/**
* @brief Ipmb defines
*/
constexpr size_t ipmbMaxDataSize = 256;
constexpr size_t ipmbConnectionHeaderLength = 3;
constexpr size_t ipmbResponseDataHeaderLength = 4;
constexpr size_t ipmbRequestDataHeaderLength = 3;
constexpr size_t ipmbAddressSize = 1;
constexpr size_t ipmbPktLenSize = 1;
constexpr size_t ipmbChecksumSize = 1;
constexpr size_t ipmbChecksum2StartOffset = 3;
constexpr ssize_t ipmbMinFrameLength = 7;
constexpr ssize_t ipmbMaxFrameLength =
ipmbPktLenSize + ipmbConnectionHeaderLength + ipmbResponseDataHeaderLength +
ipmbChecksumSize + ipmbMaxDataSize;
/**
* @brief Ipmb misc
*/
constexpr uint8_t ipmbNetFnResponseMask = 0x01;
constexpr uint8_t ipmbLunMask = 0x03;
constexpr uint8_t ipmbRsLun = 0x0;
/**
* @brief Ipmb setters
*/
constexpr uint8_t ipmbNetFnLunSet(uint8_t netFn, uint8_t lun)
{
return ((netFn << 2) | (lun & ipmbLunMask));
}
constexpr uint8_t ipmbSeqLunSet(uint8_t seq, uint8_t lun)
{
return ((seq << 2) | (lun & ipmbLunMask));
}
constexpr uint8_t ipmbAddressTo7BitSet(uint8_t address)
{
return address >> 1;
}
constexpr uint8_t ipmbRespNetFn(uint8_t netFn)
{
return netFn |= 1;
}
/**
* @brief Ipmb getters
*/
constexpr uint8_t ipmbNetFnGet(uint8_t netFnLun)
{
return netFnLun >> 2;
}
constexpr uint8_t ipmbLunFromNetFnLunGet(uint8_t netFnLun)
{
return netFnLun & ipmbLunMask;
}
constexpr uint8_t ipmbSeqGet(uint8_t seqNumLun)
{
return seqNumLun >> 2;
}
constexpr uint8_t ipmbLunFromSeqLunGet(uint8_t seqNumLun)
{
return seqNumLun & ipmbLunMask;
}
/**
* @brief Ipmb checkers
*/
constexpr bool ipmbIsResponse(IPMB_HEADER* ipmbHeader)
{
return ipmbNetFnGet(ipmbHeader->Header.Resp.rqNetFnLUN) &
ipmbNetFnResponseMask;
}
/**
* @brief Ipmb request state
*/
enum class ipmbRequestState
{
invalid,
valid,
matched,
};
/**
* @brief Channel types
*/
enum class ipmbChannelType
{
ipmb = 0,
me = 1
};
/**
* @brief IpmbResponse declaration
*/
struct IpmbResponse
{
uint8_t address;
uint8_t netFn;
uint8_t rqLun;
uint8_t rsSA;
uint8_t seq;
uint8_t rsLun;
uint8_t cmd;
uint8_t completionCode;
std::vector<uint8_t> data;
IpmbResponse();
IpmbResponse(uint8_t address, uint8_t netFn, uint8_t rqLun, uint8_t rsSA,
uint8_t seq, uint8_t rsLun, uint8_t cmd,
uint8_t completionCode, const std::vector<uint8_t>& inputData);
void i2cToIpmbConstruct(IPMB_HEADER* ipmbBuffer, size_t bufferLength);
std::shared_ptr<std::vector<uint8_t>> ipmbToi2cConstruct();
};
/**
* @brief IpmbRequest declaration
*/
struct IpmbRequest
{
uint8_t address;
uint8_t netFn;
uint8_t rsLun;
uint8_t rqSA;
uint8_t seq;
uint8_t rqLun;
uint8_t cmd;
std::vector<uint8_t> data;
size_t dataLength;
ipmbRequestState state;
std::optional<boost::asio::steady_timer> timer;
std::unique_ptr<IpmbResponse> matchedResponse;
// creates empty request with empty timer object
IpmbRequest();
IpmbRequest(uint8_t address, uint8_t netFn, uint8_t rsLun, uint8_t rqSA,
uint8_t seq, uint8_t rqLun, uint8_t cmd,
const std::vector<uint8_t>& inputData);
IpmbRequest(const IpmbRequest&) = delete;
IpmbRequest& operator=(const IpmbRequest&) = delete;
std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
returnMatchedResponse();
std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
returnStatusResponse(int status);
void i2cToIpmbConstruct(IPMB_HEADER* ipmbBuffer, size_t bufferLength);
int ipmbToi2cConstruct(std::vector<uint8_t>& buffer);
};
/**
* @brief Command filtering class declaration
*
* This feature provides simple mechanism for filtering out commands - which are
* not implemented in IPMI - on IPMB level, in order to reduce DBus traffic
*/
class IpmbCommandFilter
{
public:
// function checking if netFn & cmd combination exist in blocked command
// list
bool isBlocked(const uint8_t reqNetFn, const uint8_t cmd);
// function adding netfFn & cmd combination to the blocked command list
void addFilter(const uint8_t reqNetFn, const uint8_t cmd);
private:
boost::container::flat_set<std::pair<uint8_t, uint8_t>> unhandledCommands;
};
/**
* @brief Command filtering defines
*/
constexpr uint8_t ipmbIpmiInvalidCmd = 0xC1;
constexpr uint8_t ipmbIpmiCmdRespNotProvided = 0xCE;
constexpr uint8_t ipmbReqNetFnFromRespNetFn(uint8_t reqNetFn)
{
return reqNetFn & ~ipmbNetFnResponseMask;
}
/**
* @brief IpmbChannel class declaration
*/
class IpmbChannel
{
public:
IpmbChannel(boost::asio::io_context& io, uint8_t ipmbBmcTargetAddress,
uint8_t ipmbRqTargetAddress, uint8_t channelIdx,
std::shared_ptr<IpmbCommandFilter> commandFilter);
IpmbChannel(const IpmbChannel&) = delete;
IpmbChannel& operator=(const IpmbChannel&) = delete;
int ipmbChannelInit(const char* ipmbI2cTarget);
int ipmbChannelUpdateTargetAddress(const uint8_t newBmcTargetAddr);
bool seqNumGet(uint8_t& seq);
ipmbChannelType getChannelType();
uint8_t getBusId();
uint8_t getDevIndex();
uint8_t getChannelIdx();
uint8_t getBmcTargetAddress();
uint8_t getRqTargetAddress();
void addFilter(const uint8_t respNetFn, const uint8_t cmd);
void processI2cEvent();
void ipmbSendI2cFrame(std::shared_ptr<std::vector<uint8_t>> buffer,
size_t retriesAttempted);
std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
requestAdd(boost::asio::yield_context& yield,
std::shared_ptr<IpmbRequest> requestToSend);
private:
boost::asio::posix::stream_descriptor i2cTargetDescriptor;
int ipmbi2cTargetFd;
uint8_t ipmbBmcTargetAddress;
uint8_t ipmbRqTargetAddress;
uint8_t ipmbBusId;
uint8_t channelIdx;
std::shared_ptr<IpmbCommandFilter> commandFilter;
// array storing outstanding requests
std::array<std::shared_ptr<IpmbRequest>, ipmbMaxOutstandingRequestsCount>
outstandingRequests;
void requestTimerCallback(std::shared_ptr<IpmbRequest> request,
std::shared_ptr<std::vector<uint8_t>> buffer);
void responseMatch(std::unique_ptr<IpmbResponse>& response);
void makeRequestInvalid(IpmbRequest& request);
void makeRequestValid(std::shared_ptr<IpmbRequest> request);
};
/**
* @brief ioWrite class declaration
*/
class ioWrite
{
public:
ioWrite(std::vector<uint8_t>& buffer)
{
i2cmsg[0].addr = ipmbAddressTo7BitSet(buffer[0]);
i2cmsg[0].len = buffer.size() - ipmbAddressSize;
i2cmsg[0].buf = buffer.data() + ipmbAddressSize;
msgRdwr.msgs = i2cmsg;
msgRdwr.nmsgs = 1;
};
int name()
{
return static_cast<int>(I2C_RDWR);
}
void* data()
{
return &msgRdwr;
}
private:
i2c_rdwr_ioctl_data msgRdwr = {};
i2c_msg i2cmsg[1] = {};
};
#endif