2 Library class to work with PCI capabilities in PCI config space.
4 Provides functions to parse capabilities lists, and to locate, describe, read
5 and write capabilities. PCI config space access is abstracted away.
7 Copyright (C) 2018, Red Hat, Inc.
9 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #ifndef __PCI_CAP_LIB_H__
13 #define __PCI_CAP_LIB_H__
15 #include <Uefi/UefiBaseType.h>
18 // Base structure for representing a PCI device -- down to the PCI function
19 // level -- for the purposes of this library class. This is a forward
20 // declaration that is completed below. Concrete implementations are supposed
21 // to inherit and extend this type.
23 typedef struct PCI_CAP_DEV PCI_CAP_DEV
;
26 Read the config space of a given PCI device (both normal and extended).
28 PCI_CAP_DEV_READ_CONFIG performs as few config space accesses as possible
29 (without attempting 64-bit wide accesses).
31 PCI_CAP_DEV_READ_CONFIG returns an unspecified error if accessing Size bytes
32 from SourceOffset exceeds the config space limit of the PCI device. Fewer
33 than Size bytes may have been read in this case.
35 @param[in] PciDevice Implementation-specific unique representation
36 of the PCI device in the PCI hierarchy.
38 @param[in] SourceOffset Source offset in the config space of the PCI
39 device to start reading from.
41 @param[out] DestinationBuffer Buffer to store the read data to.
43 @param[in] Size The number of bytes to transfer.
45 @retval RETURN_SUCCESS Size bytes have been transferred from config space to
48 @return Unspecified error codes. Fewer than Size bytes may
53 (EFIAPI
*PCI_CAP_DEV_READ_CONFIG
)(
54 IN PCI_CAP_DEV
*PciDevice
,
55 IN UINT16 SourceOffset
,
56 OUT VOID
*DestinationBuffer
,
61 Write the config space of a given PCI device (both normal and extended).
63 PCI_CAP_DEV_WRITE_CONFIG performs as few config space accesses as possible
64 (without attempting 64-bit wide accesses).
66 PCI_CAP_DEV_WRITE_CONFIG returns an unspecified error if accessing Size bytes
67 at DestinationOffset exceeds the config space limit of the PCI device. Fewer
68 than Size bytes may have been written in this case.
70 @param[in] PciDevice Implementation-specific unique representation
71 of the PCI device in the PCI hierarchy.
73 @param[in] DestinationOffset Destination offset in the config space of the
74 PCI device to start writing at.
76 @param[in] SourceBuffer Buffer to read the data to be stored from.
78 @param[in] Size The number of bytes to transfer.
80 @retval RETURN_SUCCESS Size bytes have been transferred from SourceBuffer to
83 @return Unspecified error codes. Fewer than Size bytes may
88 (EFIAPI
*PCI_CAP_DEV_WRITE_CONFIG
)(
89 IN PCI_CAP_DEV
*PciDevice
,
90 IN UINT16 DestinationOffset
,
91 IN VOID
*SourceBuffer
,
96 // Complete the PCI_CAP_DEV type here. The base abstraction only requires
97 // config space accessors.
100 PCI_CAP_DEV_READ_CONFIG ReadConfig
;
101 PCI_CAP_DEV_WRITE_CONFIG WriteConfig
;
105 // Opaque data structure representing parsed PCI Capabilities Lists.
107 typedef struct PCI_CAP_LIST PCI_CAP_LIST
;
110 // Opaque data structure representing a PCI Capability in a parsed Capability
113 typedef struct PCI_CAP PCI_CAP
;
116 // Distinguishes whether a Capability ID is 8-bit wide and interpreted in
117 // normal config space, or 16-bit wide and interpreted in extended config
118 // space. Capability ID definitions are relative to domain.
126 // Public data structure that PciCapGetInfo() fills in about a PCI_CAP object.
129 PCI_CAP_DOMAIN Domain
;
132 // The capability identified by Domain and CapId may have multiple instances
133 // in config space. NumInstances provides the total count of occurrences of
134 // the capability. It is always positive.
138 // Instance is the serial number, in capabilities list traversal order (not
139 // necessarily config space offset order), of the one capability instance
140 // that PciCapGetInfo() is reporting about. Instance is always smaller than
145 // The offset in config space at which the capability header of the
146 // capability instance starts.
150 // The deduced maximum size of the capability instance, including the
151 // capability header. This hint is an upper bound, calculated -- without
152 // regard to the internal structure of the capability -- from (a) the next
153 // lowest offset in configuration space that is known to be used by another
154 // capability, and (b) from the end of the config space identified by Domain,
155 // whichever is lower.
159 // The version number of the capability instance. Always zero when Domain is
166 Parse the capabilities lists (both normal and extended, as applicable) of a
169 If the PCI device has no capabilities, that per se will not fail
170 PciCapListInit(); an empty capabilities list will be represented.
172 If the PCI device is found to be PCI Express, then an attempt will be made to
173 parse the extended capabilities list as well. If the first extended config
174 space access -- via PciDevice->ReadConfig() with SourceOffset=0x100 and
175 Size=4 -- fails, that per se will not fail PciCapListInit(); the device will
176 be assumed to have no extended capabilities.
178 @param[in] PciDevice Implementation-specific unique representation of the
179 PCI device in the PCI hierarchy.
181 @param[out] CapList Opaque data structure that holds an in-memory
182 representation of the parsed capabilities lists of
185 @retval RETURN_SUCCESS The capabilities lists have been parsed from
188 @retval RETURN_OUT_OF_RESOURCES Memory allocation failed.
190 @retval RETURN_DEVICE_ERROR A loop or some other kind of invalid pointer
191 was detected in the capabilities lists of
194 @return Error codes propagated from
195 PciDevice->ReadConfig().
200 IN PCI_CAP_DEV
*PciDevice
,
201 OUT PCI_CAP_LIST
**CapList
205 Free the resources used by CapList.
207 @param[in] CapList The PCI_CAP_LIST object to free, originally produced by
213 IN PCI_CAP_LIST
*CapList
217 Locate a capability instance in the parsed capabilities lists.
219 @param[in] CapList The PCI_CAP_LIST object produced by PciCapListInit().
221 @param[in] Domain Distinguishes whether CapId is 8-bit wide and
222 interpreted in normal config space, or 16-bit wide and
223 interpreted in extended config space. Capability ID
224 definitions are relative to domain.
226 @param[in] CapId Capability identifier to look up.
228 @param[in] Instance Domain and CapId may identify a multi-instance
229 capability. When Instance is zero, the first instance of
230 the capability is located (in list traversal order --
231 which may not mean increasing config space offset
232 order). Higher Instance values locate subsequent
233 instances of the same capability (in list traversal
236 @param[out] Cap The capability instance that matches the search
237 criteria. Cap is owned by CapList and becomes invalid
238 when CapList is freed with PciCapListUninit().
239 PciCapListFindCap() may be called with Cap set to NULL,
240 in order to test the existence of a specific capability
243 @retval RETURN_SUCCESS The capability instance identified by (Domain,
244 CapId, Instance) has been found.
246 @retval RETURN_NOT_FOUND The requested (Domain, CapId, Instance) capability
247 instance does not exist.
252 IN PCI_CAP_LIST
*CapList
,
253 IN PCI_CAP_DOMAIN Domain
,
256 OUT PCI_CAP
**Cap OPTIONAL
260 Locate the first instance of the capability given by (Domain, CapId) such
261 that the instance's Version is greater than or equal to MinVersion.
263 This is a convenience function that may save client code calls to
264 PciCapListFindCap() and PciCapGetInfo().
266 @param[in] CapList The PCI_CAP_LIST object produced by PciCapListInit().
268 @param[in] Domain Distinguishes whether CapId is 8-bit wide and
269 interpreted in normal config space, or 16-bit wide and
270 interpreted in extended config space. Capability ID
271 definitions are relative to domain.
273 @param[in] CapId Capability identifier to look up.
275 @param[in] MinVersion The minimum version that the capability instance is
276 required to have. Note that all capability instances
277 in Domain=PciCapNormal have Version=0.
279 @param[out] Cap The first capability instance that matches the search
280 criteria. Cap is owned by CapList and becomes invalid
281 when CapList is freed with PciCapListUninit().
282 PciCapListFindCapVersion() may be called with Cap set
283 to NULL, in order just to test whether the search
284 criteria are satisfiable.
286 @retval RETURN_SUCCESS The first capability instance matching (Domain,
287 CapId, MinVersion) has been located.
289 @retval RETURN_NOT_FOUND No capability instance matches (Domain, CapId,
294 PciCapListFindCapVersion (
295 IN PCI_CAP_LIST
*CapList
,
296 IN PCI_CAP_DOMAIN Domain
,
299 OUT PCI_CAP
**Cap OPTIONAL
303 Get information about a PCI Capability instance.
305 @param[in] Cap The capability instance to get info about, located with
306 PciCapListFindCap*().
308 @param[out] Info A PCI_CAP_INFO structure that describes the properties of
311 @retval RETURN_SUCCESS Fields of Info have been set.
313 @return Unspecified error codes, if filling in Info failed
320 OUT PCI_CAP_INFO
*Info
324 Read a slice of a capability instance.
326 The function performs as few config space accesses as possible (without
327 attempting 64-bit wide accesses). PciCapRead() performs bounds checking on
328 SourceOffsetInCap and Size, and only invokes PciDevice->ReadConfig() if the
329 requested transfer falls within Cap.
331 @param[in] PciDevice Implementation-specific unique representation
332 of the PCI device in the PCI hierarchy.
334 @param[in] Cap The capability instance to read, located with
335 PciCapListFindCap*().
337 @param[in] SourceOffsetInCap Source offset relative to the capability
338 header to start reading from. A zero value
339 refers to the first byte of the capability
342 @param[out] DestinationBuffer Buffer to store the read data to.
344 @param[in] Size The number of bytes to transfer.
346 @retval RETURN_SUCCESS Size bytes have been transferred from Cap to
349 @retval RETURN_BAD_BUFFER_SIZE Reading Size bytes starting from
350 SourceOffsetInCap would not (entirely) be
351 contained within Cap, as suggested by
352 PCI_CAP_INFO.MaxSizeHint. No bytes have been
355 @return Error codes propagated from
356 PciDevice->ReadConfig(). Fewer than Size
357 bytes may have been read.
362 IN PCI_CAP_DEV
*PciDevice
,
364 IN UINT16 SourceOffsetInCap
,
365 OUT VOID
*DestinationBuffer
,
370 Write a slice of a capability instance.
372 The function performs as few config space accesses as possible (without
373 attempting 64-bit wide accesses). PciCapWrite() performs bounds checking on
374 DestinationOffsetInCap and Size, and only invokes PciDevice->WriteConfig() if
375 the requested transfer falls within Cap.
377 @param[in] PciDevice Implementation-specific unique
378 representation of the PCI device in the
381 @param[in] Cap The capability instance to write, located
382 with PciCapListFindCap*().
384 @param[in] DestinationOffsetInCap Destination offset relative to the
385 capability header to start writing at. A
386 zero value refers to the first byte of the
389 @param[in] SourceBuffer Buffer to read the data to be stored from.
391 @param[in] Size The number of bytes to transfer.
393 @retval RETURN_SUCCESS Size bytes have been transferred from
396 @retval RETURN_BAD_BUFFER_SIZE Writing Size bytes starting at
397 DestinationOffsetInCap would not (entirely)
398 be contained within Cap, as suggested by
399 PCI_CAP_INFO.MaxSizeHint. No bytes have been
402 @return Error codes propagated from
403 PciDevice->WriteConfig(). Fewer than Size
404 bytes may have been written.
409 IN PCI_CAP_DEV
*PciDevice
,
411 IN UINT16 DestinationOffsetInCap
,
412 IN VOID
*SourceBuffer
,
416 #endif // __PCI_CAP_LIB_H__