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