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