Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 1 | #include "NVMeBasicContext.hpp" |
| 2 | |
| 3 | #include <endian.h> |
| 4 | #include <sys/ioctl.h> |
| 5 | #include <unistd.h> |
| 6 | |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 7 | #include <FileHandle.hpp> |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 8 | #include <boost/asio/read.hpp> |
| 9 | #include <boost/asio/streambuf.hpp> |
| 10 | #include <boost/asio/write.hpp> |
| 11 | |
| 12 | #include <cassert> |
| 13 | #include <cerrno> |
| 14 | #include <cinttypes> |
| 15 | #include <cstdio> |
| 16 | #include <cstring> |
| 17 | #include <system_error> |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 18 | |
| 19 | extern "C" |
| 20 | { |
| 21 | #include <i2c/smbus.h> |
| 22 | #include <linux/i2c-dev.h> |
| 23 | } |
| 24 | |
| 25 | /* |
| 26 | * NVMe-MI Basic Management Command |
| 27 | * |
| 28 | * https://nvmexpress.org/wp-content/uploads/NVMe_Management_-_Technical_Note_on_Basic_Management_Command.pdf |
| 29 | */ |
| 30 | |
| 31 | static std::shared_ptr<std::array<uint8_t, 6>> |
| 32 | encodeBasicQuery(int bus, uint8_t device, uint8_t offset) |
| 33 | { |
| 34 | if (bus < 0) |
| 35 | { |
| 36 | throw std::domain_error("Invalid bus argument"); |
| 37 | } |
| 38 | |
| 39 | /* bus + address + command */ |
| 40 | uint32_t busle = htole32(static_cast<uint32_t>(bus)); |
| 41 | auto command = |
| 42 | std::make_shared<std::array<uint8_t, sizeof(busle) + 1 + 1>>(); |
| 43 | memcpy(command->data(), &busle, sizeof(busle)); |
| 44 | (*command)[sizeof(busle) + 0] = device; |
| 45 | (*command)[sizeof(busle) + 1] = offset; |
| 46 | |
| 47 | return command; |
| 48 | } |
| 49 | |
| 50 | static void decodeBasicQuery(const std::array<uint8_t, 6>& req, int& bus, |
| 51 | uint8_t& device, uint8_t& offset) |
| 52 | { |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 53 | uint32_t busle = 0; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 54 | |
| 55 | memcpy(&busle, req.data(), sizeof(busle)); |
| 56 | bus = le32toh(busle); |
| 57 | device = req[sizeof(busle) + 0]; |
| 58 | offset = req[sizeof(busle) + 1]; |
| 59 | } |
| 60 | |
Andrew Jeffery | 1a14302 | 2022-07-19 14:18:24 +0930 | [diff] [blame] | 61 | static void execBasicQuery(int bus, uint8_t addr, uint8_t cmd, |
| 62 | std::vector<uint8_t>& resp) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 63 | { |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 64 | int32_t size = 0; |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 65 | std::filesystem::path devpath = "/dev/i2c-" + std::to_string(bus); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 66 | |
chaul.ampere | 0fe0229 | 2022-07-21 06:37:39 +0000 | [diff] [blame] | 67 | try |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 68 | { |
chaul.ampere | 0fe0229 | 2022-07-21 06:37:39 +0000 | [diff] [blame] | 69 | FileHandle fileHandle(devpath); |
| 70 | |
| 71 | /* Select the target device */ |
| 72 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 73 | if (::ioctl(fileHandle.handle(), I2C_SLAVE, addr) == -1) |
| 74 | { |
| 75 | std::cerr << "Failed to configure device address 0x" << std::hex |
| 76 | << (int)addr << " for bus " << std::dec << bus << ": " |
| 77 | << strerror(errno) << "\n"; |
| 78 | resp.resize(0); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | resp.resize(UINT8_MAX + 1); |
| 83 | |
| 84 | /* Issue the NVMe MI basic command */ |
| 85 | size = i2c_smbus_read_block_data(fileHandle.handle(), cmd, resp.data()); |
| 86 | if (size < 0) |
| 87 | { |
| 88 | std::cerr << "Failed to read block data from device 0x" << std::hex |
| 89 | << (int)addr << " on bus " << std::dec << bus << ": " |
| 90 | << strerror(errno) << "\n"; |
| 91 | resp.resize(0); |
| 92 | } |
| 93 | else if (size > UINT8_MAX + 1) |
| 94 | { |
| 95 | std::cerr << "Unexpected message length from device 0x" << std::hex |
| 96 | << (int)addr << " on bus " << std::dec << bus << ": " |
| 97 | << size << " (" << UINT8_MAX << ")\n"; |
| 98 | resp.resize(0); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | resp.resize(size); |
| 103 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 104 | } |
chaul.ampere | 0fe0229 | 2022-07-21 06:37:39 +0000 | [diff] [blame] | 105 | catch (const std::out_of_range& e) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 106 | { |
chaul.ampere | 0fe0229 | 2022-07-21 06:37:39 +0000 | [diff] [blame] | 107 | std::cerr << "Failed to create file handle for bus " << std::dec << bus |
| 108 | << ": " << e.what() << "\n"; |
Andrew Jeffery | 1a14302 | 2022-07-19 14:18:24 +0930 | [diff] [blame] | 109 | resp.resize(0); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 110 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 111 | } |
| 112 | |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 113 | static ssize_t processBasicQueryStream(FileHandle& in, FileHandle& out) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 114 | { |
| 115 | std::vector<uint8_t> resp{}; |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 116 | ssize_t rc = 0; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 117 | |
| 118 | while (true) |
| 119 | { |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 120 | uint8_t device = 0; |
| 121 | uint8_t offset = 0; |
| 122 | uint8_t len = 0; |
| 123 | int bus = 0; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 124 | |
| 125 | /* bus + address + command */ |
| 126 | std::array<uint8_t, sizeof(uint32_t) + 1 + 1> req{}; |
| 127 | |
| 128 | /* Read the command parameters */ |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 129 | ssize_t rc = ::read(in.handle(), req.data(), req.size()); |
| 130 | if (rc != static_cast<ssize_t>(req.size())) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 131 | { |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 132 | std::cerr << "Failed to read request from in descriptor " |
| 133 | << strerror(errno) << "\n"; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 134 | if (rc != 0) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 135 | { |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 136 | return -errno; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 137 | } |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 138 | return -EIO; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | decodeBasicQuery(req, bus, device, offset); |
| 142 | |
| 143 | /* Execute the query */ |
Andrew Jeffery | 1a14302 | 2022-07-19 14:18:24 +0930 | [diff] [blame] | 144 | execBasicQuery(bus, device, offset, resp); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 145 | |
| 146 | /* Write out the response length */ |
Andrew Jeffery | 1a14302 | 2022-07-19 14:18:24 +0930 | [diff] [blame] | 147 | len = resp.size(); |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 148 | rc = ::write(out.handle(), &len, sizeof(len)); |
| 149 | if (rc != sizeof(len)) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 150 | { |
Andrew Jeffery | 9ca98ec | 2021-11-02 09:50:47 +1030 | [diff] [blame] | 151 | std::cerr << "Failed to write block (" << std::dec << len |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 152 | << ") length to out descriptor: " |
| 153 | << strerror(static_cast<int>(-rc)) << "\n"; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 154 | if (rc != 0) |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 155 | { |
| 156 | return -errno; |
| 157 | } |
| 158 | return -EIO; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* Write out the response data */ |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 162 | std::vector<uint8_t>::iterator cursor = resp.begin(); |
| 163 | while (cursor != resp.end()) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 164 | { |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 165 | size_t lenRemaining = std::distance(cursor, resp.end()); |
| 166 | ssize_t egress = ::write(out.handle(), &(*cursor), lenRemaining); |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 167 | if (egress == -1) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 168 | { |
Andrew Jeffery | 9ca98ec | 2021-11-02 09:50:47 +1030 | [diff] [blame] | 169 | std::cerr << "Failed to write block data of length " << std::dec |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 170 | << lenRemaining << " to out pipe: " << strerror(errno) |
| 171 | << "\n"; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 172 | if (rc != 0) |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 173 | { |
| 174 | return -errno; |
| 175 | } |
| 176 | return -EIO; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | cursor += egress; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 183 | return rc; |
| 184 | } |
| 185 | |
| 186 | /* Throws std::error_code on failure */ |
| 187 | /* FIXME: Probably shouldn't do fallible stuff in a constructor */ |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 188 | NVMeBasicContext::NVMeBasicContext(boost::asio::io_context& io, int rootBus) : |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 189 | NVMeContext::NVMeContext(io, rootBus), io(io), reqStream(io), respStream(io) |
| 190 | { |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 191 | std::array<int, 2> responsePipe{}; |
| 192 | std::array<int, 2> requestPipe{}; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 193 | |
| 194 | /* Set up inter-thread communication */ |
| 195 | if (::pipe(requestPipe.data()) == -1) |
| 196 | { |
| 197 | std::cerr << "Failed to create request pipe: " << strerror(errno) |
| 198 | << "\n"; |
| 199 | throw std::error_code(errno, std::system_category()); |
| 200 | } |
| 201 | |
| 202 | if (::pipe(responsePipe.data()) == -1) |
| 203 | { |
| 204 | std::cerr << "Failed to create response pipe: " << strerror(errno) |
| 205 | << "\n"; |
| 206 | |
| 207 | if (::close(requestPipe[0]) == -1) |
| 208 | { |
| 209 | std::cerr << "Failed to close write fd of request pipe: " |
| 210 | << strerror(errno) << "\n"; |
| 211 | } |
| 212 | |
| 213 | if (::close(requestPipe[1]) == -1) |
| 214 | { |
| 215 | std::cerr << "Failed to close read fd of request pipe: " |
| 216 | << strerror(errno) << "\n"; |
| 217 | } |
| 218 | |
| 219 | throw std::error_code(errno, std::system_category()); |
| 220 | } |
| 221 | |
| 222 | reqStream.assign(requestPipe[1]); |
Ed Tanous | 7303063 | 2022-01-14 10:09:47 -0800 | [diff] [blame] | 223 | FileHandle streamIn(requestPipe[0]); |
| 224 | FileHandle streamOut(responsePipe[1]); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 225 | respStream.assign(responsePipe[0]); |
| 226 | |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 227 | thread = std::jthread([streamIn{std::move(streamIn)}, |
| 228 | streamOut{std::move(streamOut)}]() mutable { |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 229 | ssize_t rc = 0; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 230 | |
| 231 | if ((rc = processBasicQueryStream(streamIn, streamOut)) < 0) |
| 232 | { |
| 233 | std::cerr << "Failure while processing query stream: " |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 234 | << strerror(static_cast<int>(-rc)) << "\n"; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 235 | } |
| 236 | |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 237 | std::cerr << "Terminating basic query thread\n"; |
| 238 | }); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 239 | } |
| 240 | |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 241 | void NVMeBasicContext::readAndProcessNVMeSensor() |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 242 | { |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 243 | if (pollCursor == sensors.end()) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 244 | { |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 245 | this->pollNVMeDevices(); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 249 | std::shared_ptr<NVMeSensor> sensor = *pollCursor++; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 250 | |
Andrew Jeffery | 3859c7f | 2021-10-29 15:51:16 +1030 | [diff] [blame] | 251 | if (!sensor->readingStateGood()) |
| 252 | { |
| 253 | sensor->markAvailable(false); |
| 254 | sensor->updateValue(std::numeric_limits<double>::quiet_NaN()); |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 255 | readAndProcessNVMeSensor(); |
Andrew Jeffery | 3859c7f | 2021-10-29 15:51:16 +1030 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | |
Andrew Jeffery | 14108bb | 2022-03-21 15:00:16 +1030 | [diff] [blame] | 259 | /* Potentially defer sampling the sensor sensor if it is in error */ |
| 260 | if (!sensor->sample()) |
| 261 | { |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 262 | readAndProcessNVMeSensor(); |
Andrew Jeffery | 14108bb | 2022-03-21 15:00:16 +1030 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 266 | auto command = encodeBasicQuery(sensor->bus, 0x6a, 0x00); |
| 267 | |
| 268 | /* Issue the request */ |
| 269 | boost::asio::async_write( |
| 270 | reqStream, boost::asio::buffer(command->data(), command->size()), |
Ed Tanous | 76b2bc7 | 2022-02-18 09:48:16 -0800 | [diff] [blame] | 271 | [command](boost::system::error_code ec, std::size_t) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 272 | if (ec) |
| 273 | { |
| 274 | std::cerr << "Got error writing basic query: " << ec << "\n"; |
| 275 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 276 | }); |
| 277 | |
Andrew Jeffery | 84c1687 | 2022-03-15 21:50:59 +1030 | [diff] [blame] | 278 | auto response = std::make_shared<boost::asio::streambuf>(); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 279 | response->prepare(1); |
| 280 | |
| 281 | /* Gather the response and dispatch for parsing */ |
| 282 | boost::asio::async_read( |
| 283 | respStream, *response, |
| 284 | [response](const boost::system::error_code& ec, std::size_t n) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 285 | if (ec) |
| 286 | { |
| 287 | std::cerr << "Got error completing basic query: " << ec << "\n"; |
| 288 | return static_cast<std::size_t>(0); |
| 289 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 290 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 291 | if (n == 0) |
| 292 | { |
| 293 | return static_cast<std::size_t>(1); |
| 294 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 295 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 296 | std::istream is(response.get()); |
| 297 | size_t len = static_cast<std::size_t>(is.peek()); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 298 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 299 | if (n > len + 1) |
| 300 | { |
| 301 | std::cerr << "Query stream has become unsynchronised: " |
| 302 | << "n: " << n << ", " |
| 303 | << "len: " << len << "\n"; |
| 304 | return static_cast<std::size_t>(0); |
| 305 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 306 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 307 | if (n == len + 1) |
| 308 | { |
| 309 | return static_cast<std::size_t>(0); |
| 310 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 311 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 312 | if (n > 1) |
| 313 | { |
| 314 | return len + 1 - n; |
| 315 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 316 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 317 | response->prepare(len); |
| 318 | return len; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 319 | }, |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 320 | [weakSelf{weak_from_this()}, sensor, response]( |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 321 | const boost::system::error_code& ec, std::size_t length) mutable { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 322 | if (ec) |
| 323 | { |
| 324 | std::cerr << "Got error reading basic query: " << ec << "\n"; |
| 325 | return; |
| 326 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 327 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 328 | if (length == 0) |
| 329 | { |
| 330 | std::cerr << "Invalid message length: " << length << "\n"; |
| 331 | return; |
| 332 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 333 | |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 334 | if (auto self = weakSelf.lock()) |
| 335 | { |
| 336 | /* Deserialise the response */ |
| 337 | response->consume(1); /* Drop the length byte */ |
| 338 | std::istream is(response.get()); |
| 339 | std::vector<char> data(response->size()); |
| 340 | is.read(data.data(), response->size()); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 341 | |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 342 | /* Update the sensor */ |
| 343 | self->processResponse(sensor, data.data(), data.size()); |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 344 | |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 345 | /* Enqueue processing of the next sensor */ |
| 346 | self->readAndProcessNVMeSensor(); |
| 347 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 348 | }); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void NVMeBasicContext::pollNVMeDevices() |
| 352 | { |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 353 | pollCursor = sensors.begin(); |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 354 | |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame^] | 355 | scanTimer.expires_after(std::chrono::seconds(1)); |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 356 | scanTimer.async_wait([weakSelf{weak_from_this()}]( |
| 357 | const boost::system::error_code errorCode) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 358 | if (errorCode == boost::asio::error::operation_aborted) |
| 359 | { |
| 360 | return; |
| 361 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 362 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 363 | if (errorCode) |
| 364 | { |
| 365 | std::cerr << errorCode.message() << "\n"; |
| 366 | return; |
| 367 | } |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 368 | |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 369 | if (auto self = weakSelf.lock()) |
| 370 | { |
| 371 | self->readAndProcessNVMeSensor(); |
| 372 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 373 | }); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static double getTemperatureReading(int8_t reading) |
| 377 | { |
| 378 | if (reading == static_cast<int8_t>(0x80) || |
| 379 | reading == static_cast<int8_t>(0x81)) |
| 380 | { |
| 381 | // 0x80 = No temperature data or temperature data is more the 5 s |
| 382 | // old 0x81 = Temperature sensor failure |
| 383 | return std::numeric_limits<double>::quiet_NaN(); |
| 384 | } |
| 385 | |
| 386 | return reading; |
| 387 | } |
| 388 | |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 389 | void NVMeBasicContext::processResponse(std::shared_ptr<NVMeSensor>& sensor, |
| 390 | void* msg, size_t len) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 391 | { |
Andrew Jeffery | 14108bb | 2022-03-21 15:00:16 +1030 | [diff] [blame] | 392 | if (msg == nullptr || len < 6) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 393 | { |
Andrew Jeffery | 14108bb | 2022-03-21 15:00:16 +1030 | [diff] [blame] | 394 | sensor->incrementError(); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
| 398 | uint8_t* messageData = static_cast<uint8_t*>(msg); |
Andrew Jeffery | 7aeb1a5 | 2022-03-15 22:49:04 +1030 | [diff] [blame] | 399 | |
| 400 | uint8_t status = messageData[0]; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 401 | if (((status & NVME_MI_BASIC_SFLGS_DRIVE_NOT_READY) != 0) || |
| 402 | ((status & NVME_MI_BASIC_SFLGS_DRIVE_FUNCTIONAL) == 0)) |
Andrew Jeffery | 7aeb1a5 | 2022-03-15 22:49:04 +1030 | [diff] [blame] | 403 | { |
| 404 | sensor->markFunctional(false); |
| 405 | return; |
| 406 | } |
| 407 | |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 408 | double value = getTemperatureReading(messageData[2]); |
Andrew Jeffery | 14108bb | 2022-03-21 15:00:16 +1030 | [diff] [blame] | 409 | if (!std::isfinite(value)) |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 410 | { |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 411 | sensor->incrementError(); |
Andrew Jeffery | 14108bb | 2022-03-21 15:00:16 +1030 | [diff] [blame] | 412 | return; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 413 | } |
Andrew Jeffery | 14108bb | 2022-03-21 15:00:16 +1030 | [diff] [blame] | 414 | |
| 415 | sensor->updateValue(value); |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 416 | } |