xref: /linux/drivers/gpu/nova-core/mctp.rs (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
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     }
46 }
47 
48 impl MctpHeader {
49     /// Builds a single-packet MCTP header (`SOM=1`, `EOM=1`, `SEQ=0`, `SEID=0`).
50     pub(crate) fn single_packet() -> Self {
51         Self::zeroed().with_som(true).with_eom(true)
52     }
53 
54     /// Returns whether this is a complete single-packet message (`SOM=1` and `EOM=1`).
55     pub(crate) fn is_single_packet(self) -> bool {
56         self.som().into_bool() && self.eom().into_bool()
57     }
58 }
59 
60 /// MCTP message type for PCI vendor-defined messages.
61 const MSG_TYPE_VENDOR_PCI: u8 = 0x7e;
62 
63 bitfield! {
64     /// NVIDIA Vendor-Defined Message header over MCTP.
65     pub(crate) struct NvdmHeader(u32) {
66         /// NVDM message type.
67         31:24 nvdm_type ?=> NvdmType;
68         /// PCI vendor ID.
69         23:8 vendor_id;
70         /// MCTP vendor-defined message type.
71         6:0 msg_type;
72     }
73 }
74 
75 impl NvdmHeader {
76     /// Builds an NVDM header for the given message type.
77     pub(crate) fn new(nvdm_type: NvdmType) -> Self {
78         Self::zeroed()
79             .with_const_msg_type::<{ num::u8_as_u32(MSG_TYPE_VENDOR_PCI) }>()
80             .with_vendor_id(Vendor::NVIDIA.as_raw())
81             .with_nvdm_type(nvdm_type)
82     }
83 
84     /// Validates this header against the expected NVIDIA NVDM format and type.
85     pub(crate) fn validate(self, expected_type: NvdmType) -> bool {
86         u8::from(self.msg_type()) == MSG_TYPE_VENDOR_PCI
87             && u16::from(self.vendor_id()) == Vendor::NVIDIA.as_raw()
88             && matches!(self.nvdm_type(), Ok(nvdm_type) if nvdm_type == expected_type)
89     }
90 }
91