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