blob: edf52ec3feec40c36dcc409ae5b37fea27946f44 [file] [log] [blame]
Andrew Jefferye3e3c972021-05-26 14:37:07 +09301#pragma once
2
3#include "NVMeContext.hpp"
Ed Tanous18b61862025-01-30 10:56:28 -08004#include "NVMeSensor.hpp"
Andrew Jefferye3e3c972021-05-26 14:37:07 +09305
Ed Tanous1f978632023-02-28 18:16:39 -08006#include <boost/asio/io_context.hpp>
Andrew Jefferye3e3c972021-05-26 14:37:07 +09307#include <boost/asio/posix/stream_descriptor.hpp>
8
Ed Tanous18b61862025-01-30 10:56:28 -08009#include <cstddef>
10#include <memory>
Andrew Jeffery3cbd5a12022-07-18 16:32:11 +093011#include <thread>
12
Andrew Jefferye3e3c972021-05-26 14:37:07 +093013class NVMeBasicContext : public NVMeContext
14{
15 public:
Ed Tanous1f978632023-02-28 18:16:39 -080016 NVMeBasicContext(boost::asio::io_context& io, int rootBus);
Ed Tanous74cffa82022-01-25 13:00:28 -080017 ~NVMeBasicContext() override = default;
18 void pollNVMeDevices() override;
Andrew Jefferyb5d7a7f2022-05-02 11:57:03 +093019 void readAndProcessNVMeSensor() override;
Andrew Jeffery8c7074e2022-03-21 14:58:13 +103020 void processResponse(std::shared_ptr<NVMeSensor>& sensor, void* msg,
21 size_t len) override;
Andrew Jefferye3e3c972021-05-26 14:37:07 +093022
23 private:
Ed Tanous1f978632023-02-28 18:16:39 -080024 NVMeBasicContext(boost::asio::io_context& io, int rootBus, int cmdOut,
Andrew Jefferye3e3c972021-05-26 14:37:07 +093025 int streamIn, int streamOut, int cmdIn);
Ed Tanous1f978632023-02-28 18:16:39 -080026 boost::asio::io_context& io;
Andrew Jeffery3cbd5a12022-07-18 16:32:11 +093027
28 // The IO thread must be destructed after the stream descriptors, so
29 // initialise it first. http://eel.is/c++draft/class.base.init#note-6
30 //
31 // Providing a stop-source to the thread execution function isn't
32 // particularly useful as it will spend most of its time blocked in a system
33 // call - ioctl() for the actual device communication, or read() and write()
34 // on the pipes associated with reqStream and respStream. Rather than trying
35 // to force a stop, rely on read()/write() failures from closed pipes to
36 // coerce it to exit and thus allow completion of the join().
37 std::jthread thread;
38
39 // Destruction of the stream descriptors has the effect of issuing cancel(),
40 // destroying the closure of the callback where we might be carrying
41 // weak_ptrs to `this`.
42 // https://www.boost.org/doc/libs/1_79_0/doc/html/boost_asio/reference/posix__basic_descriptor/_basic_descriptor.html
Andrew Jefferye3e3c972021-05-26 14:37:07 +093043 boost::asio::posix::stream_descriptor reqStream;
44 boost::asio::posix::stream_descriptor respStream;
Andrew Jeffery7aeb1a52022-03-15 22:49:04 +103045
46 enum
47 {
48 NVME_MI_BASIC_SFLGS_DRIVE_NOT_READY = 0x40,
49 NVME_MI_BASIC_SFLGS_DRIVE_FUNCTIONAL = 0x20,
50 };
Andrew Jefferye3e3c972021-05-26 14:37:07 +093051};