summaryrefslogtreecommitdiff
path: root/include/linux/pci-tsm.h
blob: d7b078d5e2726aef0e5d0636d7aad85e874f0175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __PCI_TSM_H
#define __PCI_TSM_H
#include <linux/mutex.h>
#include <linux/pci.h>

struct pci_tsm;
struct tsm_dev;

/*
 * struct pci_tsm_ops - manage confidential links and security state
 * @link_ops: Coordinate PCIe SPDM and IDE establishment via a platform TSM.
 *	      Provide a secure session transport for TDISP state management
 *	      (typically bare metal physical function operations).
 * @devsec_ops: Lock, unlock, and interrogate the security state of the
 *		function via the platform TSM (typically virtual function
 *		operations).
 *
 * This operations are mutually exclusive either a tsm_dev instance
 * manages physical link properties or it manages function security
 * states like TDISP lock/unlock.
 */
struct pci_tsm_ops {
	/*
	 * struct pci_tsm_link_ops - Manage physical link and the TSM/DSM session
	 * @probe: establish context with the TSM (allocate / wrap 'struct
	 *	   pci_tsm') for follow-on link operations
	 * @remove: destroy link operations context
	 * @connect: establish / validate a secure connection (e.g. IDE)
	 *	     with the device
	 * @disconnect: teardown the secure link
	 *
	 * Context: @probe, @remove, @connect, and @disconnect run under
	 * pci_tsm_rwsem held for write to sync with TSM unregistration and
	 * mutual exclusion of @connect and @disconnect. @connect and
	 * @disconnect additionally run under the DSM lock (struct
	 * pci_tsm_pf0::lock) as well as @probe and @remove of the subfunctions.
	 */
	struct_group_tagged(pci_tsm_link_ops, link_ops,
		struct pci_tsm *(*probe)(struct tsm_dev *tsm_dev,
					 struct pci_dev *pdev);
		void (*remove)(struct pci_tsm *tsm);
		int (*connect)(struct pci_dev *pdev);
		void (*disconnect)(struct pci_dev *pdev);
	);

	/*
	 * struct pci_tsm_devsec_ops - Manage the security state of the function
	 * @lock: establish context with the TSM (allocate / wrap 'struct
	 *	  pci_tsm') for follow-on security state transitions from the
	 *	  LOCKED state
	 * @unlock: destroy TSM context and return device to UNLOCKED state
	 *
	 * Context: @lock and @unlock run under pci_tsm_rwsem held for write to
	 * sync with TSM unregistration and each other
	 */
	struct_group_tagged(pci_tsm_devsec_ops, devsec_ops,
		struct pci_tsm *(*lock)(struct tsm_dev *tsm_dev,
					struct pci_dev *pdev);
		void (*unlock)(struct pci_tsm *tsm);
	);
};

/**
 * struct pci_tsm - Core TSM context for a given PCIe endpoint
 * @pdev: Back ref to device function, distinguishes type of pci_tsm context
 * @dsm_dev: PCI Device Security Manager for link operations on @pdev
 * @tsm_dev: PCI TEE Security Manager device for Link Confidentiality or Device
 *	     Function Security operations
 *
 * This structure is wrapped by low level TSM driver data and returned by
 * probe()/lock(), it is freed by the corresponding remove()/unlock().
 *
 * For link operations it serves to cache the association between a Device
 * Security Manager (DSM) and the functions that manager can assign to a TVM.
 * That can be "self", for assigning function0 of a TEE I/O device, a
 * sub-function (SR-IOV virtual function, or non-function0
 * multifunction-device), or a downstream endpoint (PCIe upstream switch-port as
 * DSM).
 */
struct pci_tsm {
	struct pci_dev *pdev;
	struct pci_dev *dsm_dev;
	struct tsm_dev *tsm_dev;
};

/**
 * struct pci_tsm_pf0 - Physical Function 0 TDISP link context
 * @base_tsm: generic core "tsm" context
 * @lock: mutual exclustion for pci_tsm_ops invocation
 * @doe_mb: PCIe Data Object Exchange mailbox
 */
struct pci_tsm_pf0 {
	struct pci_tsm base_tsm;
	struct mutex lock;
	struct pci_doe_mb *doe_mb;
};

/* physical function0 and capable of 'connect' */
static inline bool is_pci_tsm_pf0(struct pci_dev *pdev)
{
	if (!pdev)
		return false;

	if (!pci_is_pcie(pdev))
		return false;

	if (pdev->is_virtfn)
		return false;

	/*
	 * Allow for a Device Security Manager (DSM) associated with function0
	 * of an Endpoint to coordinate TDISP requests for other functions
	 * (physical or virtual) of the device, or allow for an Upstream Port
	 * DSM to accept TDISP requests for the Endpoints downstream of the
	 * switch.
	 */
	switch (pci_pcie_type(pdev)) {
	case PCI_EXP_TYPE_ENDPOINT:
	case PCI_EXP_TYPE_UPSTREAM:
	case PCI_EXP_TYPE_RC_END:
		if (pdev->ide_cap || (pdev->devcap & PCI_EXP_DEVCAP_TEE))
			break;
		fallthrough;
	default:
		return false;
	}

	return PCI_FUNC(pdev->devfn) == 0;
}

#ifdef CONFIG_PCI_TSM
int pci_tsm_register(struct tsm_dev *tsm_dev);
void pci_tsm_unregister(struct tsm_dev *tsm_dev);
int pci_tsm_link_constructor(struct pci_dev *pdev, struct pci_tsm *tsm,
			     struct tsm_dev *tsm_dev);
int pci_tsm_pf0_constructor(struct pci_dev *pdev, struct pci_tsm_pf0 *tsm,
			    struct tsm_dev *tsm_dev);
void pci_tsm_pf0_destructor(struct pci_tsm_pf0 *tsm);
int pci_tsm_doe_transfer(struct pci_dev *pdev, u8 type, const void *req,
			 size_t req_sz, void *resp, size_t resp_sz);
#else
static inline int pci_tsm_register(struct tsm_dev *tsm_dev)
{
	return 0;
}
static inline void pci_tsm_unregister(struct tsm_dev *tsm_dev)
{
}
#endif
#endif /*__PCI_TSM_H */