Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "NVMeContext.hpp" |
| 4 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 5 | #include <boost/asio/io_context.hpp> |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 6 | #include <boost/asio/posix/stream_descriptor.hpp> |
| 7 | |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 8 | #include <thread> |
| 9 | |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 10 | class NVMeBasicContext : public NVMeContext |
| 11 | { |
| 12 | public: |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 13 | NVMeBasicContext(boost::asio::io_context& io, int rootBus); |
Ed Tanous | 74cffa8 | 2022-01-25 13:00:28 -0800 | [diff] [blame] | 14 | ~NVMeBasicContext() override = default; |
| 15 | void pollNVMeDevices() override; |
Andrew Jeffery | b5d7a7f | 2022-05-02 11:57:03 +0930 | [diff] [blame] | 16 | void readAndProcessNVMeSensor() override; |
Andrew Jeffery | 8c7074e | 2022-03-21 14:58:13 +1030 | [diff] [blame] | 17 | void processResponse(std::shared_ptr<NVMeSensor>& sensor, void* msg, |
| 18 | size_t len) override; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 19 | |
| 20 | private: |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 21 | NVMeBasicContext(boost::asio::io_context& io, int rootBus, int cmdOut, |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 22 | int streamIn, int streamOut, int cmdIn); |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 23 | boost::asio::io_context& io; |
Andrew Jeffery | 3cbd5a1 | 2022-07-18 16:32:11 +0930 | [diff] [blame] | 24 | |
| 25 | // The IO thread must be destructed after the stream descriptors, so |
| 26 | // initialise it first. http://eel.is/c++draft/class.base.init#note-6 |
| 27 | // |
| 28 | // Providing a stop-source to the thread execution function isn't |
| 29 | // particularly useful as it will spend most of its time blocked in a system |
| 30 | // call - ioctl() for the actual device communication, or read() and write() |
| 31 | // on the pipes associated with reqStream and respStream. Rather than trying |
| 32 | // to force a stop, rely on read()/write() failures from closed pipes to |
| 33 | // coerce it to exit and thus allow completion of the join(). |
| 34 | std::jthread thread; |
| 35 | |
| 36 | // Destruction of the stream descriptors has the effect of issuing cancel(), |
| 37 | // destroying the closure of the callback where we might be carrying |
| 38 | // weak_ptrs to `this`. |
| 39 | // 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] | 40 | boost::asio::posix::stream_descriptor reqStream; |
| 41 | boost::asio::posix::stream_descriptor respStream; |
Andrew Jeffery | 7aeb1a5 | 2022-03-15 22:49:04 +1030 | [diff] [blame] | 42 | |
| 43 | enum |
| 44 | { |
| 45 | NVME_MI_BASIC_SFLGS_DRIVE_NOT_READY = 0x40, |
| 46 | NVME_MI_BASIC_SFLGS_DRIVE_FUNCTIONAL = 0x20, |
| 47 | }; |
Andrew Jeffery | e3e3c97 | 2021-05-26 14:37:07 +0930 | [diff] [blame] | 48 | }; |