xref: /linux/drivers/gpu/nova-core/mctp.rs (revision 0eaed89c18aeedf0898baf2dbf5ff027c6795152)
1*944a0fedSJohn Hubbard // SPDX-License-Identifier: GPL-2.0
2*944a0fedSJohn Hubbard // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3*944a0fedSJohn Hubbard 
4*944a0fedSJohn Hubbard //! MCTP/NVDM protocol types for NVIDIA GPU firmware communication.
5*944a0fedSJohn Hubbard //!
6*944a0fedSJohn Hubbard //! MCTP (Management Component Transport Protocol) carries NVDM (NVIDIA
7*944a0fedSJohn Hubbard //! Data Model) messages between the kernel driver and GPU firmware processors
8*944a0fedSJohn Hubbard //! such as FSP and GSP.
9*944a0fedSJohn Hubbard 
10*944a0fedSJohn Hubbard use kernel::pci::Vendor;
11*944a0fedSJohn Hubbard 
12*944a0fedSJohn Hubbard /// NVDM message type identifiers carried over MCTP.
13*944a0fedSJohn Hubbard #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
14*944a0fedSJohn Hubbard #[repr(u8)]
15*944a0fedSJohn Hubbard pub(crate) enum NvdmType {
16*944a0fedSJohn Hubbard     #[default]
17*944a0fedSJohn Hubbard     /// Chain of Trust boot message.
18*944a0fedSJohn Hubbard     Cot = 0x14,
19*944a0fedSJohn Hubbard     /// FSP command response.
20*944a0fedSJohn Hubbard     FspResponse = 0x15,
21*944a0fedSJohn Hubbard }
22*944a0fedSJohn Hubbard 
23*944a0fedSJohn Hubbard impl TryFrom<u8> for NvdmType {
24*944a0fedSJohn Hubbard     type Error = u8;
25*944a0fedSJohn Hubbard 
26*944a0fedSJohn Hubbard     fn try_from(value: u8) -> Result<Self, Self::Error> {
27*944a0fedSJohn Hubbard         match value {
28*944a0fedSJohn Hubbard             x if x == u8::from(Self::Cot) => Ok(Self::Cot),
29*944a0fedSJohn Hubbard             x if x == u8::from(Self::FspResponse) => Ok(Self::FspResponse),
30*944a0fedSJohn Hubbard             _ => Err(value),
31*944a0fedSJohn Hubbard         }
32*944a0fedSJohn Hubbard     }
33*944a0fedSJohn Hubbard }
34*944a0fedSJohn Hubbard 
35*944a0fedSJohn Hubbard impl From<NvdmType> for u8 {
36*944a0fedSJohn Hubbard     fn from(value: NvdmType) -> Self {
37*944a0fedSJohn Hubbard         value as u8
38*944a0fedSJohn Hubbard     }
39*944a0fedSJohn Hubbard }
40*944a0fedSJohn Hubbard 
41*944a0fedSJohn Hubbard bitfield! {
42*944a0fedSJohn Hubbard     pub(crate) struct MctpHeader(u32), "MCTP transport header for NVIDIA firmware messages." {
43*944a0fedSJohn Hubbard         31:31 som as bool, "Start-of-message bit.";
44*944a0fedSJohn Hubbard         30:30 eom as bool, "End-of-message bit.";
45*944a0fedSJohn Hubbard         29:28 seq as u8, "Packet sequence number.";
46*944a0fedSJohn Hubbard         23:16 seid as u8, "Source endpoint ID.";
47*944a0fedSJohn Hubbard     }
48*944a0fedSJohn Hubbard }
49*944a0fedSJohn Hubbard 
50*944a0fedSJohn Hubbard impl MctpHeader {
51*944a0fedSJohn Hubbard     /// Builds a single-packet MCTP header (`SOM=1`, `EOM=1`, `SEQ=0`, `SEID=0`).
52*944a0fedSJohn Hubbard     pub(crate) fn single_packet() -> Self {
53*944a0fedSJohn Hubbard         Self::default().set_som(true).set_eom(true)
54*944a0fedSJohn Hubbard     }
55*944a0fedSJohn Hubbard 
56*944a0fedSJohn Hubbard     /// Returns whether this is a complete single-packet message (`SOM=1` and `EOM=1`).
57*944a0fedSJohn Hubbard     pub(crate) fn is_single_packet(self) -> bool {
58*944a0fedSJohn Hubbard         self.som() && self.eom()
59*944a0fedSJohn Hubbard     }
60*944a0fedSJohn Hubbard }
61*944a0fedSJohn Hubbard 
62*944a0fedSJohn Hubbard /// MCTP message type for PCI vendor-defined messages.
63*944a0fedSJohn Hubbard const MSG_TYPE_VENDOR_PCI: u8 = 0x7e;
64*944a0fedSJohn Hubbard 
65*944a0fedSJohn Hubbard bitfield! {
66*944a0fedSJohn Hubbard     pub(crate) struct NvdmHeader(u32), "NVIDIA Vendor-Defined Message header over MCTP." {
67*944a0fedSJohn Hubbard         31:24 nvdm_type as u8 ?=> NvdmType, "NVDM message type.";
68*944a0fedSJohn Hubbard         23:8 vendor_id as u16, "PCI vendor ID.";
69*944a0fedSJohn Hubbard         6:0 msg_type as u8, "MCTP vendor-defined message type.";
70*944a0fedSJohn Hubbard     }
71*944a0fedSJohn Hubbard }
72*944a0fedSJohn Hubbard 
73*944a0fedSJohn Hubbard impl NvdmHeader {
74*944a0fedSJohn Hubbard     /// Builds an NVDM header for the given message type.
75*944a0fedSJohn Hubbard     pub(crate) fn new(nvdm_type: NvdmType) -> Self {
76*944a0fedSJohn Hubbard         Self::default()
77*944a0fedSJohn Hubbard             .set_msg_type(MSG_TYPE_VENDOR_PCI)
78*944a0fedSJohn Hubbard             .set_vendor_id(Vendor::NVIDIA.as_raw())
79*944a0fedSJohn Hubbard             .set_nvdm_type(nvdm_type)
80*944a0fedSJohn Hubbard     }
81*944a0fedSJohn Hubbard 
82*944a0fedSJohn Hubbard     /// Validates this header against the expected NVIDIA NVDM format and type.
83*944a0fedSJohn Hubbard     pub(crate) fn validate(self, expected_type: NvdmType) -> bool {
84*944a0fedSJohn Hubbard         self.msg_type() == MSG_TYPE_VENDOR_PCI
85*944a0fedSJohn Hubbard             && self.vendor_id() == Vendor::NVIDIA.as_raw()
86*944a0fedSJohn Hubbard             && matches!(self.nvdm_type(), Ok(nvdm_type) if nvdm_type == expected_type)
87*944a0fedSJohn Hubbard     }
88*944a0fedSJohn Hubbard }
89