| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 1 | /* |
| Ed Tanous | b5e823f | 2025-10-09 20:28:42 -0400 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | */ |
| 5 | |
| 6 | #pragma once |
| 7 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 8 | #include <MctpAsioEndpoint.hpp> |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 9 | #include <OcpMctpVdm.hpp> |
| 10 | #include <boost/asio/generic/datagram_protocol.hpp> |
| 11 | #include <boost/asio/io_context.hpp> |
| 12 | #include <boost/asio/steady_timer.hpp> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 13 | #include <boost/circular_buffer.hpp> |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 14 | #include <boost/container/devector.hpp> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 15 | #include <boost/container/flat_map.hpp> |
| 16 | #include <boost/container/small_vector.hpp> |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 17 | |
| 18 | #include <cstddef> |
| 19 | #include <cstdint> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 20 | #include <expected> |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 21 | #include <functional> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 22 | #include <iostream> |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 23 | #include <memory> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 24 | #include <queue> |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 25 | #include <span> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 26 | #include <system_error> |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 27 | #include <unordered_map> |
| 28 | #include <utility> |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 29 | |
| 30 | namespace mctp |
| 31 | { |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 32 | class MctpRequester |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 33 | { |
| 34 | public: |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 35 | MctpRequester() = delete; |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 36 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 37 | MctpRequester(const MctpRequester&) = delete; |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 38 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 39 | MctpRequester(MctpRequester&&) = delete; |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 40 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 41 | MctpRequester& operator=(const MctpRequester&) = delete; |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 42 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 43 | MctpRequester& operator=(MctpRequester&&) = delete; |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 44 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 45 | explicit MctpRequester(boost::asio::io_context& ctx); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 46 | |
| 47 | void sendRecvMsg(uint8_t eid, std::span<const uint8_t> reqMsg, |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 48 | std::move_only_function<void(const std::error_code&, |
| 49 | std::span<const uint8_t>)> |
| 50 | callback); |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 51 | |
| 52 | private: |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 53 | using cb_t = std::move_only_function<void(const std::error_code&, |
| 54 | std::span<const uint8_t>)>; |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 55 | |
| 56 | static constexpr size_t maxMessageSize = 65536 + 256; |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 57 | static constexpr uint8_t msgType = ocp::accelerator_management::messageType; |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 58 | |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 59 | struct RequestContext |
| 60 | { |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 61 | std::vector<uint8_t> reqMsg; |
| 62 | cb_t callback; |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 63 | |
| 64 | RequestContext(const RequestContext&) = delete; |
| 65 | RequestContext& operator=(const RequestContext&) = delete; |
| 66 | |
| 67 | RequestContext(RequestContext&&) = default; |
| 68 | RequestContext& operator=(RequestContext&&) = default; |
| 69 | ~RequestContext() = default; |
| 70 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 71 | explicit RequestContext(std::span<const uint8_t> req, cb_t&& cb) : |
| 72 | reqMsg(req.begin(), req.end()), callback(std::move(cb)) |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 73 | {} |
| 74 | }; |
| 75 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 76 | struct EidContext |
| 77 | { |
| 78 | boost::asio::steady_timer timer; |
| 79 | uint8_t iid{}; |
| 80 | boost::container::devector<RequestContext> queue; |
| 81 | EidContext(boost::asio::io_context& io) : timer{io}, iid{0xFF} {} |
| 82 | EidContext(EidContext&&) noexcept = default; |
| 83 | EidContext& operator=(EidContext&&) noexcept = default; |
| 84 | EidContext& operator=(const EidContext&) = delete; |
| 85 | EidContext(const EidContext&) = delete; |
| 86 | ~EidContext() = default; |
| 87 | }; |
| 88 | |
| 89 | std::optional<uint8_t> getNextIid(uint8_t eid); |
| 90 | void startReceive(); |
| 91 | void processRecvMsg(const boost::system::error_code& ec, size_t length); |
| 92 | void handleSendMsgCompletion(uint8_t eid, |
| 93 | const boost::system::error_code& ec, |
| 94 | size_t length); |
| 95 | |
| 96 | void handleResult(uint8_t eid, const std::error_code& ec, |
| 97 | std::span<const uint8_t> buffer); |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 98 | void processQueue(uint8_t eid); |
| 99 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 100 | boost::asio::io_context& io; |
| 101 | boost::asio::generic::datagram_protocol::endpoint sendEndPoint; |
| 102 | |
| 103 | boost::asio::generic::datagram_protocol::socket mctpSocket; |
| 104 | std::array<uint8_t, maxMessageSize> buffer{}; |
| 105 | MctpAsioEndpoint recvEndPoint; |
| 106 | std::unordered_map<uint8_t, EidContext> requestContextQueues; |
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 107 | }; |
| 108 | |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 109 | } // namespace mctp |