Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "NVMeContext.hpp" |
Ed Tanous | 18b6186 | 2025-01-30 10:56:28 -0800 | [diff] [blame^] | 4 | #include "NVMeSensor.hpp" |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 5 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 6 | #include <boost/asio/io_context.hpp> |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 7 | #include <boost/asio/posix/stream_descriptor.hpp> |
| 8 | |
Ed Tanous | 18b6186 | 2025-01-30 10:56:28 -0800 | [diff] [blame^] | 9 | #include <cstddef> |
| 10 | #include <memory> |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 11 | #include <thread> |
| 12 | |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 13 | class NVMeBasicContext : public NVMeContext |
| 14 | { |
| 15 | public: |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 16 | NVMeBasicContext(boost::asio::io_context& io, int rootBus); |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 17 | ~NVMeBasicContext() override = default; |
| 18 | void pollNVMeDevices() override; |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 19 | void readAndProcessNVMeSensor() override; |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 20 | void processResponse(std::shared_ptr<NVMeSensor>& sensor, void* msg, |
| 21 | size_t len) override; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 22 | |
| 23 | private: |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 24 | NVMeBasicContext(boost::asio::io_context& io, int rootBus, int cmdOut, |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 25 | int streamIn, int streamOut, int cmdIn); |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 26 | boost::asio::io_context& io; |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 27 | |
| 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 Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 43 | boost::asio::posix::stream_descriptor reqStream; |
| 44 | boost::asio::posix::stream_descriptor respStream; |
Andrew Jeffery | 7aeb1a5 | 2022-03-15 22:49:04 +1030 | [diff] [blame] | 45 | |
| 46 | enum |
| 47 | { |
| 48 | NVME_MI_BASIC_SFLGS_DRIVE_NOT_READY = 0x40, |
| 49 | NVME_MI_BASIC_SFLGS_DRIVE_FUNCTIONAL = 0x20, |
| 50 | }; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 51 | }; |