xref: /linux/drivers/gpu/nova-core/mctp.rs (revision cb8fef4b07f71da01843457b66e579ad248a625e)
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::{
11     bitfield,
12     pci::Vendor,
13     prelude::*, //
14 };
15 
16 use crate::{
17     bounded_enum,
18     num, //
19 };
20 
21 bounded_enum! {
22     /// NVDM message type identifiers carried over MCTP.
23     #[derive(Debug, Clone, Copy, PartialEq, Eq)]
24     pub(crate) enum NvdmType with TryFrom<Bounded<u32, 8>> {
25         /// PRC (Product Reconfiguration Control) message.
26         Prc = 0x13,
27         /// Chain of Trust boot message.
28         Cot = 0x14,
29         /// FSP command response.
30         FspResponse = 0x15,
31     }
32 }
33 
34 bitfield! {
35     /// MCTP transport header for NVIDIA firmware messages.
36     pub(crate) struct MctpHeader(u32) {
37         /// Start-of-message bit.
38         31:31 som;
39         /// End-of-message bit.
40         30:30 eom;
41         /// Packet sequence number.
42         29:28 seq;
43         /// Source endpoint ID.
44         23:16 seid;
45         /// Destination endpoint ID.
46         15:8 deid;
47         /// MCTP header version.
48         3:0 version;
49     }
50 }
51 
52 impl MctpHeader {
53     /// The MCTP header version that this driver uses.
54     const VERSION: u32 = 1;
55 
56     /// Builds the MCTP header of a message that fits in one packet: `SOM` and `EOM` set, the
57     /// version set, and every other field zero.
58     pub(crate) fn single_packet() -> Self {
59         Self::zeroed()
60             .with_const_version::<{ Self::VERSION }>()
61             .with_const_deid::<0>()
62             .with_som(true)
63             .with_eom(true)
64     }
65 
66     /// Returns whether this is a complete single-packet message (`SOM=1` and `EOM=1`).
67     pub(crate) fn is_single_packet(self) -> bool {
68         self.som().into_bool() && self.eom().into_bool()
69     }
70 }
71 
72 /// MCTP message type for PCI vendor-defined messages.
73 const MSG_TYPE_VENDOR_PCI: u8 = 0x7e;
74 
75 bitfield! {
76     /// NVIDIA Vendor-Defined Message header over MCTP.
77     pub(crate) struct NvdmHeader(u32) {
78         /// NVDM message type.
79         31:24 nvdm_type ?=> NvdmType;
80         /// PCI vendor ID.
81         23:8 vendor_id;
82         /// MCTP vendor-defined message type.
83         6:0 msg_type;
84     }
85 }
86 
87 impl NvdmHeader {
88     /// Builds an NVDM header for the given message type.
89     pub(crate) fn new(nvdm_type: NvdmType) -> Self {
90         Self::zeroed()
91             .with_const_msg_type::<{ num::u8_as_u32(MSG_TYPE_VENDOR_PCI) }>()
92             .with_vendor_id(Vendor::NVIDIA.as_raw())
93             .with_nvdm_type(nvdm_type)
94     }
95 
96     /// Validates this header against the expected NVIDIA NVDM format and type.
97     pub(crate) fn validate(self, expected_type: NvdmType) -> bool {
98         u8::from(self.msg_type()) == MSG_TYPE_VENDOR_PCI
99             && u16::from(self.vendor_id()) == Vendor::NVIDIA.as_raw()
100             && matches!(self.nvdm_type(), Ok(nvdm_type) if nvdm_type == expected_type)
101     }
102 }
103