This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
263 lines (245 loc) · 8.65 KB
/
main.cpp
File metadata and controls
263 lines (245 loc) · 8.65 KB
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
/*
// Copyright (c) 2021 Intel Corporation
//
// 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 "drive.hpp"
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <mctp_wrapper.hpp>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
class Application;
struct DeviceUpdateHandler
{
DeviceUpdateHandler(Application& appn, mctpw::BindingType binding) :
app(appn), bindingType(binding)
{
}
void operator()(void*, const mctpw::Event& evt,
boost::asio::yield_context& yield);
Application& app;
mctpw::BindingType bindingType;
};
class Application
{
using DriveMap =
std::unordered_map<mctpw::eid_t, std::shared_ptr<nvmemi::Drive>>;
public:
Application() :
ioContext(std::make_shared<boost::asio::io_context>()),
signals(*ioContext, SIGINT, SIGTERM), pollTimer(nullptr)
{
}
void init()
{
signals.async_wait([this](const boost::system::error_code&,
const int&) { this->ioContext->stop(); });
dbusConnection =
std::make_shared<sdbusplus::asio::connection>(*ioContext);
objectServer =
std::make_shared<sdbusplus::asio::object_server>(dbusConnection);
dbusConnection->request_name(serviceName);
boost::asio::spawn(
*ioContext, [this](boost::asio::yield_context yield) {
constexpr auto bindingType = mctpw::BindingType::mctpOverSmBus;
mctpw::MCTPConfiguration config(mctpw::MessageType::nvmeMgmtMsg,
bindingType);
auto wrapper = std::make_shared<mctpw::MCTPWrapper>(
this->dbusConnection, config,
DeviceUpdateHandler(*this, bindingType));
mctpWrappers.emplace(bindingType, wrapper);
wrapper->detectMctpEndpoints(yield);
for (auto& [eid, service] : wrapper->getEndpointMap())
{
auto drive = std::make_shared<nvmemi::Drive>(
getDriveName(wrapper, eid), eid, *this->objectServer,
wrapper);
this->drives.emplace(eid, drive);
}
if (!this->drives.empty())
{
resumeHealthStatusPolling();
}
});
if (auto envPtr = std::getenv("NVME_DEBUG"))
{
std::string value(envPtr);
if (value == "1")
{
initializeHealthStatusPollIntf();
}
}
}
std::string getDriveName(std::shared_ptr<mctpw::MCTPWrapper> wrapper,
mctpw::eid_t eid)
{
std::optional<std::string> driveLocation =
wrapper->getDeviceLocation(eid);
if (driveLocation.has_value())
{
return "NVMe_" + driveLocation.value();
}
std::string driveName =
"NVMeDrive" + std::to_string(this->driveCounter);
this->driveCounter++;
return driveName;
}
static void doPoll(boost::asio::yield_context yield, Application* app)
{
while (app->pollTimer != nullptr)
{
boost::system::error_code ec;
app->pollTimer->expires_after(subsystemHsPollInterval);
app->pollTimer->async_wait(yield[ec]);
if (ec == boost::asio::error::operation_aborted)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Poll timer aborted");
return;
}
else if (ec)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Sensor poll timer failed");
return;
}
DriveMap copyDrives(app->drives);
for (auto& [eid, drive] : copyDrives)
{
drive->pollSubsystemHealthStatus(yield);
}
}
phosphor::logging::log<phosphor::logging::level::WARNING>(
"Drive polling task stopped. Timer is null now");
}
void pauseHealthStatusPolling()
{
if (pollTimer)
{
pollTimer->cancel();
pollTimer = nullptr;
}
phosphor::logging::log<phosphor::logging::level::INFO>(
"health status polling paused");
}
void resumeHealthStatusPolling()
{
if (!pollTimer)
{
pollTimer = std::make_shared<boost::asio::steady_timer>(*ioContext);
boost::asio::spawn(*ioContext,
[this](boost::asio::yield_context yield) {
doPoll(yield, this);
});
}
phosphor::logging::log<phosphor::logging::level::INFO>(
"health status polling resumed");
}
void initializeHealthStatusPollIntf()
{
if (healthStatusPollInterface != nullptr)
{
phosphor::logging::log<phosphor::logging::level::DEBUG>(
"healthStatusPollInterface already initialized");
return;
}
const char* objPath = "/xyz/openbmc_project/healthstatus";
healthStatusPollInterface = objectServer->add_unique_interface(
objPath, "xyz.openbmc_project.NVM.HealthStatusPoll");
healthStatusPollInterface->register_method(
"PauseHealthStatusPoll", [this](const bool pause) {
if (pause)
{
pauseHealthStatusPolling();
}
else
{
resumeHealthStatusPolling();
}
});
healthStatusPollInterface->initialize();
}
void run()
{
this->ioContext->run();
}
private:
std::shared_ptr<boost::asio::io_context> ioContext;
boost::asio::signal_set signals;
std::shared_ptr<sdbusplus::asio::connection> dbusConnection{};
std::shared_ptr<sdbusplus::asio::object_server> objectServer{};
std::unique_ptr<sdbusplus::asio::dbus_interface> healthStatusPollInterface =
nullptr;
std::unordered_map<mctpw::BindingType, std::shared_ptr<mctpw::MCTPWrapper>>
mctpWrappers{};
DriveMap drives{};
size_t driveCounter = 1;
std::shared_ptr<boost::asio::steady_timer> pollTimer;
static constexpr const char* serviceName = "xyz.openbmc_project.nvme_mi";
static const inline std::chrono::seconds subsystemHsPollInterval{1};
friend struct DeviceUpdateHandler;
};
void DeviceUpdateHandler::operator()(
void*, const mctpw::Event& evt,
[[maybe_unused]] boost::asio::yield_context& wrapperContext)
{
switch (evt.type)
{
case mctpw::Event::EventType::deviceAdded: {
auto wrapper = app.mctpWrappers.at(bindingType);
auto drive = std::make_shared<nvmemi::Drive>(
app.getDriveName(wrapper, evt.eid), evt.eid, *app.objectServer,
wrapper);
app.drives.emplace(evt.eid, drive);
phosphor::logging::log<phosphor::logging::level::INFO>(
"New drive inserted",
phosphor::logging::entry("EID=%d", evt.eid));
if (app.drives.size() == 1)
{
app.resumeHealthStatusPolling();
}
}
break;
case mctpw::Event::EventType::deviceRemoved: {
if (app.drives.erase(evt.eid) == 1)
{
phosphor::logging::log<phosphor::logging::level::INFO>(
"Drive removed",
phosphor::logging::entry("EID=%d", evt.eid));
}
else
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"No drive found mapped to eid",
phosphor::logging::entry("EID=%d", evt.eid));
}
// Timer cancellation if all drives are removed
if (app.drives.empty())
{
app.pauseHealthStatusPolling();
}
}
break;
default:
break;
}
}
int main()
{
Application app;
app.init();
app.run();
return 0;
}