nvidia-gpu: add support for communication to the endpoint

The commit uses MCTP VDM protocol to read temperature sensor value from
the gpu.

The MCTP VDM protocol is an extension of the OCP Accelerator Management
Interface specification. [1]

Tested: Build an image for gb200nvl-obmc machine with the following
patches cherry picked. This patches are needed to enable the mctp stack.

https://gerrit.openbmc.org/c/openbmc/openbmc/+/79422

Restart the nvidiagpusensor service.
```
root@gb200nvl-obmc:~# systemctl start xyz.openbmc_project.nvidiagpusensor.service
```

The app is detecting entity-manager configuration on gb200nvl-obmc
machine. The app is also able to detect all the endpoints from the mctp
service dbus tree. The app is reading temperature sensor value from gpu
correctly and the temperature sensor is also present on redfish.

```
$ curl -k -u 'root:0penBmc' https://10.137.203.137/redfish/v1/Chassis/NVIDIA_GB200_1/Sensors/temperature_NVIDIA_GB200_GPU
{
  "@odata.id": "/redfish/v1/Chassis/NVIDIA_GB200_1/Sensors/temperature_NVIDIA_GB200_GPU",
  "@odata.type": "#Sensor.v1_2_0.Sensor",
  "Id": "temperature_NVIDIA_GB200_GPU",
  "Name": "NVIDIA GB200 GPU",
  "Reading": 36.4375,
  "ReadingRangeMax": 127.0,
  "ReadingRangeMin": -128.0,
  "ReadingType": "Temperature",
  "ReadingUnits": "Cel",
  "Status": {
    "Health": "OK",
    "State": "Enabled"
  }
}%

root@gb200nvl-obmc:~# busctl tree xyz.openbmc_project.GpuSensor
└─ /xyz
  └─ /xyz/openbmc_project
    └─ /xyz/openbmc_project/sensors
      └─ /xyz/openbmc_project/sensors/temperature
        └─ /xyz/openbmc_project/sensors/temperature/NVIDIA_GB200_GPU

root@gb200nvl-obmc:~# busctl introspect xyz.openbmc_project.GpuSensor /xyz/openbmc_project/sensors/temperature/NVIDIA_GB200_GPU
NAME                                                  TYPE      SIGNATURE RESULT/VALUE                             FLAGS
org.freedesktop.DBus.Introspectable                   interface -         -                                        -
.Introspect                                           method    -         s                                        -
org.freedesktop.DBus.Peer                             interface -         -                                        -
.GetMachineId                                         method    -         s                                        -
.Ping                                                 method    -         -                                        -
org.freedesktop.DBus.Properties                       interface -         -                                        -
.Get                                                  method    ss        v                                        -
.GetAll                                               method    s         a{sv}                                    -
.Set                                                  method    ssv       -                                        -
.PropertiesChanged                                    signal    sa{sv}as  -                                        -
xyz.openbmc_project.Association.Definitions           interface -         -                                        -
.Associations                                         property  a(sss)    1 "chassis" "all_sensors" "/xyz/openbmc… emits-change
xyz.openbmc_project.Sensor.Value                      interface -         -                                        -
.MaxValue                                             property  d         127                                      emits-change
.MinValue                                             property  d         -128                                     emits-change
.Unit                                                 property  s         "xyz.openbmc_project.Sensor.Value.Unit.… emits-change
.Value                                                property  d         36.3125                                  emits-change writable
xyz.openbmc_project.Sensor.ValueMutability            interface -         -                                        -
.Mutable                                              property  b         true                                     emits-change
xyz.openbmc_project.State.Decorator.Availability      interface -         -                                        -
.Available                                            property  b         true                                     emits-change writable
xyz.openbmc_project.State.Decorator.OperationalStatus interface -         -                                        -
.Functional                                           property  b         true                                     emits-change
```

[1] https://www.opencompute.org/documents/ocp-gpu-accelerator-management-interfaces-v1-pdf

Change-Id: Ied938b9e5c19751ee283b4b948e16c905c78fb48
Signed-off-by: Harshit Aghera <haghera@nvidia.com>
diff --git a/src/nvidia-gpu/MctpRequester.cpp b/src/nvidia-gpu/MctpRequester.cpp
new file mode 100644
index 0000000..024f8cc
--- /dev/null
+++ b/src/nvidia-gpu/MctpRequester.cpp
@@ -0,0 +1,162 @@
+/*
+ * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
+ * AFFILIATES. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "MctpRequester.hpp"
+
+#include <linux/mctp.h>
+#include <sys/socket.h>
+
+#include <OcpMctpVdm.hpp>
+#include <boost/asio/buffer.hpp>
+#include <boost/asio/error.hpp>
+#include <boost/asio/generic/datagram_protocol.hpp>
+#include <boost/asio/io_context.hpp>
+#include <boost/asio/steady_timer.hpp>
+#include <phosphor-logging/lg2.hpp>
+
+#include <cerrno>
+#include <cstddef>
+#include <cstdint>
+#include <cstring>
+#include <functional>
+#include <span>
+#include <utility>
+
+using namespace std::literals;
+
+namespace mctp
+{
+
+MctpRequester::MctpRequester(boost::asio::io_context& ctx) :
+    mctpSocket(ctx, boost::asio::generic::datagram_protocol{AF_MCTP, 0}),
+    expiryTimer(ctx)
+{}
+
+void MctpRequester::processRecvMsg(
+    uint8_t eid, const std::span<const uint8_t> reqMsg,
+    const std::span<uint8_t> respMsg, const boost::system::error_code& ec,
+    const size_t /*length*/)
+{
+    expiryTimer.cancel();
+
+    if (ec)
+    {
+        lg2::error(
+            "MctpRequester failed to receive data from the MCTP socket - ErrorCode={EC}, Error={ER}.",
+            "EC", ec.value(), "ER", ec.message());
+        completionCallback(EIO);
+        return;
+    }
+
+    const auto* respAddr =
+        // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+        reinterpret_cast<const struct sockaddr_mctp*>(recvEndPoint.data());
+
+    if (respAddr->smctp_type != msgType)
+    {
+        lg2::error("MctpRequester: Message type mismatch");
+        completionCallback(EPROTO);
+        return;
+    }
+
+    uint8_t respEid = respAddr->smctp_addr.s_addr;
+
+    if (respEid != eid)
+    {
+        lg2::error(
+            "MctpRequester: EID mismatch - expected={EID}, received={REID}",
+            "EID", eid, "REID", respEid);
+        completionCallback(EPROTO);
+        return;
+    }
+
+    if (respMsg.size() > sizeof(ocp::accelerator_management::BindingPciVid))
+    {
+        const auto* reqHdr =
+            // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+            reinterpret_cast<const ocp::accelerator_management::BindingPciVid*>(
+                reqMsg.data());
+
+        uint8_t reqInstanceId = reqHdr->instance_id &
+                                ocp::accelerator_management::instanceIdBitMask;
+        const auto* respHdr =
+            // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+            reinterpret_cast<const ocp::accelerator_management::BindingPciVid*>(
+                respMsg.data());
+
+        uint8_t respInstanceId = respHdr->instance_id &
+                                 ocp::accelerator_management::instanceIdBitMask;
+
+        if (reqInstanceId != respInstanceId)
+        {
+            lg2::error(
+                "MctpRequester: Instance ID mismatch - request={REQ}, response={RESP}",
+                "REQ", static_cast<int>(reqInstanceId), "RESP",
+                static_cast<int>(respInstanceId));
+            completionCallback(EPROTO);
+            return;
+        }
+    }
+
+    completionCallback(0);
+}
+
+void MctpRequester::handleSendMsgCompletion(
+    uint8_t eid, const std::span<const uint8_t> reqMsg,
+    std::span<uint8_t> respMsg, const boost::system::error_code& ec,
+    size_t /* length */)
+{
+    if (ec)
+    {
+        lg2::error(
+            "MctpRequester failed to send data from the MCTP socket - ErrorCode={EC}, Error={ER}.",
+            "EC", ec.value(), "ER", ec.message());
+        completionCallback(EIO);
+        return;
+    }
+
+    expiryTimer.expires_after(2s);
+
+    expiryTimer.async_wait([this](const boost::system::error_code& ec) {
+        if (ec != boost::asio::error::operation_aborted)
+        {
+            completionCallback(ETIME);
+        }
+    });
+
+    mctpSocket.async_receive_from(
+        boost::asio::mutable_buffer(respMsg), recvEndPoint,
+        std::bind_front(&MctpRequester::processRecvMsg, this, eid, reqMsg,
+                        respMsg));
+}
+
+void MctpRequester::sendRecvMsg(
+    uint8_t eid, const std::span<const uint8_t> reqMsg,
+    std::span<uint8_t> respMsg, std::move_only_function<void(int)> callback)
+{
+    if (reqMsg.size() < sizeof(ocp::accelerator_management::BindingPciVid))
+    {
+        lg2::error("MctpRequester: Message too small");
+        callback(EPROTO);
+        return;
+    }
+
+    completionCallback = std::move(callback);
+
+    struct sockaddr_mctp addr{};
+    addr.smctp_family = AF_MCTP;
+    addr.smctp_addr.s_addr = eid;
+    addr.smctp_type = msgType;
+    addr.smctp_tag = MCTP_TAG_OWNER;
+
+    sendEndPoint = {&addr, sizeof(addr)};
+
+    mctpSocket.async_send_to(
+        boost::asio::const_buffer(reqMsg), sendEndPoint,
+        std::bind_front(&MctpRequester::handleSendMsgCompletion, this, eid,
+                        reqMsg, respMsg));
+}
+} // namespace mctp