2 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 This program and the accompanying materials are licensed and made available
10 under the terms and conditions of the BSD License which accompanies this
11 distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
15 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <IndustryStandard/PciExpress21.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/MemoryAllocationLib.h>
24 #include "BasePciCapLib.h"
28 Compare a standalone PCI_CAP_KEY against a PCI_CAP containing an embedded
31 @param[in] PciCapKey Pointer to the bare PCI_CAP_KEY.
33 @param[in] PciCap Pointer to the PCI_CAP with the embedded PCI_CAP_KEY.
35 @retval <0 If PciCapKey compares less than PciCap->Key.
37 @retval 0 If PciCapKey compares equal to PciCap->Key.
39 @retval >0 If PciCapKey compares greater than PciCap->Key.
45 IN CONST VOID
*PciCapKey
,
49 CONST PCI_CAP_KEY
*Key1
;
50 CONST PCI_CAP_KEY
*Key2
;
53 Key2
= &((CONST PCI_CAP
*)PciCap
)->Key
;
55 if (Key1
->Domain
< Key2
->Domain
) {
58 if (Key1
->Domain
> Key2
->Domain
) {
61 if (Key1
->CapId
< Key2
->CapId
) {
64 if (Key1
->CapId
> Key2
->CapId
) {
67 if (Key1
->Instance
< Key2
->Instance
) {
70 if (Key1
->Instance
> Key2
->Instance
) {
78 Compare two PCI_CAP objects based on PCI_CAP.Key.
80 @param[in] PciCap1 Pointer to the first PCI_CAP.
82 @param[in] PciCap2 Pointer to the second PCI_CAP.
84 @retval <0 If PciCap1 compares less than PciCap2.
86 @retval 0 If PciCap1 compares equal to PciCap2.
88 @retval >0 If PciCap1 compares greater than PciCap2.
94 IN CONST VOID
*PciCap1
,
95 IN CONST VOID
*PciCap2
98 CONST PCI_CAP_KEY
*PciCap1Key
;
100 PciCap1Key
= &((CONST PCI_CAP
*)PciCap1
)->Key
;
101 return ComparePciCapKey (PciCap1Key
, PciCap2
);
106 Compare the standalone UINT16 config space offset of a capability header
107 against a PCI_CAP containing an embedded Offset.
109 @param[in] CapHdrOffset Pointer to the bare UINT16 config space offset.
111 @param[in] PciCap Pointer to the PCI_CAP with the embedded Offset.
113 @retval <0 If CapHdrOffset compares less than PciCap->Offset.
115 @retval 0 If CapHdrOffset compares equal to PciCap->Offset.
117 @retval >0 If CapHdrOffset compares greater than PciCap->Offset.
122 ComparePciCapOffsetKey (
123 IN CONST VOID
*CapHdrOffset
,
124 IN CONST VOID
*PciCap
130 Offset1
= *(CONST UINT16
*)CapHdrOffset
;
131 Offset2
= ((CONST PCI_CAP
*)PciCap
)->Offset
;
133 // Note: both Offset1 and Offset2 are promoted to INT32 below, and the
134 // subtraction takes place between INT32 values.
136 return Offset1
- Offset2
;
141 Compare two PCI_CAP objects based on PCI_CAP.Offset.
143 @param[in] PciCap1 Pointer to the first PCI_CAP.
145 @param[in] PciCap2 Pointer to the second PCI_CAP.
147 @retval <0 If PciCap1 compares less than PciCap2.
149 @retval 0 If PciCap1 compares equal to PciCap2.
151 @retval >0 If PciCap1 compares greater than PciCap2.
156 ComparePciCapOffset (
157 IN CONST VOID
*PciCap1
,
158 IN CONST VOID
*PciCap2
164 Offset1
= ((CONST PCI_CAP
*)PciCap1
)->Offset
;
165 Offset2
= ((CONST PCI_CAP
*)PciCap2
)->Offset
;
167 // Note: both Offset1 and Offset2 are promoted to INT32 below, and the
168 // subtraction takes place between INT32 values.
170 return Offset1
- Offset2
;
175 Insert a new instance of the PCI capability given by (Domain, CapId) in
178 @param[in,out] CapList The PCI_CAP_LIST into which the new PCI_CAP
179 should be inserted. CapList will own the new
182 @param[in,out] CapHdrOffsets Link the new PCI_CAP structure into the
183 (non-owning) CapHdrOffsets collection as well.
184 CapHdrOffsets orders the PCI_CAP structures
185 based on the PCI_CAP.Offset member, and enables
186 the calculation of PCI_CAP.MaxSizeHint.
188 @param[in] Domain Whether the capability is normal or extended.
190 @param[in] CapId Capability ID (specific to Domain).
192 @param[in] Offset Config space offset at which the standard
193 header of the capability starts. The caller is
194 responsible for ensuring that Offset be DWORD
195 aligned. The caller is also responsible for
196 ensuring that Offset be within the config space
197 identified by Domain.
199 @param[in] Version The version number of the capability. The
200 caller is responsible for passing 0 as Version
201 if Domain is PciCapNormal.
203 @retval RETURN_SUCCESS Insertion successful.
205 @retval RETURN_OUT_OF_RESOURCES Memory allocation failed.
207 @retval RETURN_DEVICE_ERROR A PCI_CAP with Offset is already linked by
208 CapHdrOffsets. This indicates a loop in the
209 capabilities list being parsed.
214 IN OUT PCI_CAP_LIST
*CapList
,
215 IN OUT ORDERED_COLLECTION
*CapHdrOffsets
,
216 IN PCI_CAP_DOMAIN Domain
,
223 RETURN_STATUS Status
;
224 ORDERED_COLLECTION_ENTRY
*PciCapEntry
;
225 PCI_CAP
*InstanceZero
;
227 ASSERT ((Offset
& 0x3) == 0);
228 ASSERT (Offset
< (Domain
== PciCapNormal
?
229 PCI_MAX_CONFIG_OFFSET
: PCI_EXP_MAX_CONFIG_OFFSET
));
230 ASSERT (Domain
== PciCapExtended
|| Version
== 0);
233 // Set InstanceZero to suppress incorrect compiler/analyzer warnings.
238 // Allocate PciCap, and populate it assuming it is the first occurrence of
239 // (Domain, CapId). Note that PciCap->MaxSizeHint is not assigned the final
242 PciCap
= AllocatePool (sizeof *PciCap
);
243 if (PciCap
== NULL
) {
244 return RETURN_OUT_OF_RESOURCES
;
246 PciCap
->Key
.Domain
= Domain
;
247 PciCap
->Key
.CapId
= CapId
;
248 PciCap
->Key
.Instance
= 0;
249 PciCap
->NumInstancesUnion
.NumInstances
= 1;
250 PciCap
->Offset
= Offset
;
251 PciCap
->MaxSizeHint
= 0;
252 PciCap
->Version
= Version
;
255 // Add PciCap to CapList.
257 Status
= OrderedCollectionInsert (CapList
->Capabilities
, &PciCapEntry
,
259 if (RETURN_ERROR (Status
)) {
260 if (Status
== RETURN_OUT_OF_RESOURCES
) {
263 ASSERT (Status
== RETURN_ALREADY_STARTED
);
265 // PciCap is not the first instance of (Domain, CapId). Add it as a new
266 // instance, taking the current instance count from Instance#0. Note that
267 // we don't bump the instance count maintained in Instance#0 just yet, to
268 // keep rollback on errors simple.
270 InstanceZero
= OrderedCollectionUserStruct (PciCapEntry
);
271 PciCap
->Key
.Instance
= InstanceZero
->NumInstancesUnion
.NumInstances
;
272 PciCap
->NumInstancesUnion
.InstanceZero
= InstanceZero
;
274 ASSERT (PciCap
->Key
.Instance
> 0);
275 Status
= OrderedCollectionInsert (CapList
->Capabilities
, &PciCapEntry
,
277 if (Status
== RETURN_OUT_OF_RESOURCES
) {
282 // At this point, PciCap has been inserted in CapList->Capabilities, either
283 // with Instance==0 or with Instance>0. PciCapEntry is the iterator that
286 ASSERT_RETURN_ERROR (Status
);
289 // Link PciCap into CapHdrOffsets too, to order it globally based on config
290 // space offset. Note that partial overlaps between capability headers is not
291 // possible: Offset is DWORD aligned, normal capability headers are 16-bit
292 // wide, and extended capability headers are 32-bit wide. Therefore any two
293 // capability headers either are distinct or start at the same offset
294 // (implying a loop in the respective capabilities list).
296 Status
= OrderedCollectionInsert (CapHdrOffsets
, NULL
, PciCap
);
297 if (RETURN_ERROR (Status
)) {
298 if (Status
== RETURN_ALREADY_STARTED
) {
300 // Loop found; map return status accordingly.
302 Status
= RETURN_DEVICE_ERROR
;
304 goto DeletePciCapFromCapList
;
308 // Now we can bump the instance count maintained in Instance#0, if PciCap is
309 // not the first instance of (Domain, CapId).
311 if (PciCap
->Key
.Instance
> 0) {
312 InstanceZero
->NumInstancesUnion
.NumInstances
++;
314 return RETURN_SUCCESS
;
316 DeletePciCapFromCapList
:
317 OrderedCollectionDelete (CapList
->Capabilities
, PciCapEntry
, NULL
);
327 Calculate the MaxSizeHint member for a PCI_CAP object.
329 CalculatePciCapMaxSizeHint() may only be called once all capability instances
330 have been successfully processed by InsertPciCap().
332 @param[in,out] PciCap The PCI_CAP object for which to calculate the
333 MaxSizeHint member. The caller is responsible for
334 passing a PCI_CAP object that has been created by a
335 successful invocation of InsertPciCap().
337 @param[in] NextPciCap If NextPciCap is NULL, then the caller is responsible
338 for PciCap to represent the capability instance with
339 the highest header offset in all config space. If
340 NextPciCap is not NULL, then the caller is responsible
341 for (a) having created NextPciCap with a successful
342 invocation of InsertPciCap(), and (b) NextPciCap being
343 the direct successor of PciCap in config space offset
344 order, as ordered by ComparePciCapOffset().
348 CalculatePciCapMaxSizeHint (
349 IN OUT PCI_CAP
*PciCap
,
350 IN PCI_CAP
*NextPciCap OPTIONAL
353 UINT16 ConfigSpaceSize
;
355 ConfigSpaceSize
= (PciCap
->Key
.Domain
== PciCapNormal
?
356 PCI_MAX_CONFIG_OFFSET
: PCI_EXP_MAX_CONFIG_OFFSET
);
358 // The following is guaranteed by the interface contract on
359 // CalculatePciCapMaxSizeHint().
361 ASSERT (NextPciCap
== NULL
|| PciCap
->Offset
< NextPciCap
->Offset
);
363 // The following is guaranteed by the interface contract on InsertPciCap().
365 ASSERT (PciCap
->Offset
< ConfigSpaceSize
);
367 // Thus we can safely subtract PciCap->Offset from either of
369 // - and NextPciCap->Offset (if NextPciCap is not NULL).
371 // PciCap extends from PciCap->Offset to NextPciCap->Offset (if any), except
372 // it cannot cross config space boundary.
374 if (NextPciCap
== NULL
|| NextPciCap
->Offset
>= ConfigSpaceSize
) {
375 PciCap
->MaxSizeHint
= ConfigSpaceSize
- PciCap
->Offset
;
378 PciCap
->MaxSizeHint
= NextPciCap
->Offset
- PciCap
->Offset
;
383 Debug dump a PCI_CAP_LIST object at the DEBUG_VERBOSE level.
385 @param[in] CapList The PCI_CAP_LIST object to dump.
390 DebugDumpPciCapList (
391 IN PCI_CAP_LIST
*CapList
395 ORDERED_COLLECTION_ENTRY
*PciCapEntry
;
397 for (PciCapEntry
= OrderedCollectionMin (CapList
->Capabilities
);
399 PciCapEntry
= OrderedCollectionNext (PciCapEntry
)) {
401 RETURN_STATUS Status
;
404 PciCap
= OrderedCollectionUserStruct (PciCapEntry
);
405 Status
= PciCapGetInfo (PciCap
, &Info
);
407 // PciCapGetInfo() cannot fail in this library instance.
409 ASSERT_RETURN_ERROR (Status
);
411 DEBUG ((DEBUG_VERBOSE
,
412 "%a:%a: %a 0x%04x %03u/%03u v0x%x @0x%03x+0x%03x\n", gEfiCallerBaseName
,
413 __FUNCTION__
, (Info
.Domain
== PciCapNormal
? "Norm" : "Extd"),
414 Info
.CapId
, Info
.Instance
, Info
.NumInstances
, Info
.Version
, Info
.Offset
,
422 Empty a collection of PCI_CAP structures, optionally releasing the referenced
423 PCI_CAP structures themselves. Release the collection at last.
425 @param[in,out] PciCapCollection The collection to empty and release.
427 @param[in] FreePciCap TRUE if the PCI_CAP structures linked by
428 PciCapCollection should be released. When
429 FALSE, the caller is responsible for
430 retaining at least one reference to each
431 PCI_CAP structure originally linked by
436 EmptyAndUninitPciCapCollection (
437 IN OUT ORDERED_COLLECTION
*PciCapCollection
,
438 IN BOOLEAN FreePciCap
441 ORDERED_COLLECTION_ENTRY
*PciCapEntry
;
442 ORDERED_COLLECTION_ENTRY
*NextEntry
;
444 for (PciCapEntry
= OrderedCollectionMin (PciCapCollection
);
446 PciCapEntry
= NextEntry
) {
449 NextEntry
= OrderedCollectionNext (PciCapEntry
);
450 OrderedCollectionDelete (PciCapCollection
, PciCapEntry
, (VOID
**)&PciCap
);
455 OrderedCollectionUninit (PciCapCollection
);
460 Parse the capabilities lists (both normal and extended, as applicable) of a
463 If the PCI device has no capabilities, that per se will not fail
464 PciCapListInit(); an empty capabilities list will be represented.
466 If the PCI device is found to be PCI Express, then an attempt will be made to
467 parse the extended capabilities list as well. If the first extended config
468 space access -- via PciDevice->ReadConfig() with SourceOffset=0x100 and
469 Size=4 -- fails, that per se will not fail PciCapListInit(); the device will
470 be assumed to have no extended capabilities.
472 @param[in] PciDevice Implementation-specific unique representation of the
473 PCI device in the PCI hierarchy.
475 @param[out] CapList Opaque data structure that holds an in-memory
476 representation of the parsed capabilities lists of
479 @retval RETURN_SUCCESS The capabilities lists have been parsed from
482 @retval RETURN_OUT_OF_RESOURCES Memory allocation failed.
484 @retval RETURN_DEVICE_ERROR A loop or some other kind of invalid pointer
485 was detected in the capabilities lists of
488 @return Error codes propagated from
489 PciDevice->ReadConfig().
494 IN PCI_CAP_DEV
*PciDevice
,
495 OUT PCI_CAP_LIST
**CapList
498 PCI_CAP_LIST
*OutCapList
;
499 RETURN_STATUS Status
;
500 ORDERED_COLLECTION
*CapHdrOffsets
;
502 BOOLEAN DeviceIsExpress
;
503 ORDERED_COLLECTION_ENTRY
*OffsetEntry
;
506 // Allocate the output structure.
508 OutCapList
= AllocatePool (sizeof *OutCapList
);
509 if (OutCapList
== NULL
) {
510 return RETURN_OUT_OF_RESOURCES
;
513 // The OutCapList->Capabilities collection owns the PCI_CAP structures and
514 // orders them based on PCI_CAP.Key.
516 OutCapList
->Capabilities
= OrderedCollectionInit (ComparePciCap
,
518 if (OutCapList
->Capabilities
== NULL
) {
519 Status
= RETURN_OUT_OF_RESOURCES
;
524 // The (temporary) CapHdrOffsets collection only references PCI_CAP
525 // structures, and orders them based on PCI_CAP.Offset.
527 CapHdrOffsets
= OrderedCollectionInit (ComparePciCapOffset
,
528 ComparePciCapOffsetKey
);
529 if (CapHdrOffsets
== NULL
) {
530 Status
= RETURN_OUT_OF_RESOURCES
;
531 goto FreeCapabilities
;
535 // Whether the device is PCI Express depends on the normal capability with
536 // identifier EFI_PCI_CAPABILITY_ID_PCIEXP.
538 DeviceIsExpress
= FALSE
;
541 // Check whether a normal capabilities list is present. If there's none,
542 // that's not an error; we'll just return OutCapList->Capabilities empty.
544 Status
= PciDevice
->ReadConfig (PciDevice
, PCI_PRIMARY_STATUS_OFFSET
,
545 &PciStatusReg
, sizeof PciStatusReg
);
546 if (RETURN_ERROR (Status
)) {
547 goto FreeCapHdrOffsets
;
549 if ((PciStatusReg
& EFI_PCI_STATUS_CAPABILITY
) != 0) {
550 UINT8 NormalCapHdrOffset
;
553 // Fetch the start offset of the normal capabilities list.
555 Status
= PciDevice
->ReadConfig (PciDevice
, PCI_CAPBILITY_POINTER_OFFSET
,
556 &NormalCapHdrOffset
, sizeof NormalCapHdrOffset
);
557 if (RETURN_ERROR (Status
)) {
558 goto FreeCapHdrOffsets
;
562 // Traverse the normal capabilities list.
564 NormalCapHdrOffset
&= 0xFC;
565 while (NormalCapHdrOffset
> 0) {
566 EFI_PCI_CAPABILITY_HDR NormalCapHdr
;
568 Status
= PciDevice
->ReadConfig (PciDevice
, NormalCapHdrOffset
,
569 &NormalCapHdr
, sizeof NormalCapHdr
);
570 if (RETURN_ERROR (Status
)) {
571 goto FreeCapHdrOffsets
;
574 Status
= InsertPciCap (OutCapList
, CapHdrOffsets
, PciCapNormal
,
575 NormalCapHdr
.CapabilityID
, NormalCapHdrOffset
, 0);
576 if (RETURN_ERROR (Status
)) {
577 goto FreeCapHdrOffsets
;
580 if (NormalCapHdr
.CapabilityID
== EFI_PCI_CAPABILITY_ID_PCIEXP
) {
581 DeviceIsExpress
= TRUE
;
583 NormalCapHdrOffset
= NormalCapHdr
.NextItemPtr
& 0xFC;
588 // If the device has been found PCI Express, attempt to traverse the extended
589 // capabilities list. It starts right after the normal config space.
591 if (DeviceIsExpress
) {
592 UINT16 ExtendedCapHdrOffset
;
594 ExtendedCapHdrOffset
= PCI_MAX_CONFIG_OFFSET
;
595 while (ExtendedCapHdrOffset
> 0) {
596 PCI_EXPRESS_EXTENDED_CAPABILITIES_HEADER ExtendedCapHdr
;
598 Status
= PciDevice
->ReadConfig (PciDevice
, ExtendedCapHdrOffset
,
599 &ExtendedCapHdr
, sizeof ExtendedCapHdr
);
601 // If the first extended config space access fails, assume the device has
602 // no extended capabilities. If the first extended config space access
603 // succeeds but we read an "all bits zero" extended capability header,
604 // that means (by spec) the device has no extended capabilities.
606 if (ExtendedCapHdrOffset
== PCI_MAX_CONFIG_OFFSET
&&
607 (RETURN_ERROR (Status
) ||
608 IsZeroBuffer (&ExtendedCapHdr
, sizeof ExtendedCapHdr
))) {
611 if (RETURN_ERROR (Status
)) {
612 goto FreeCapHdrOffsets
;
615 Status
= InsertPciCap (OutCapList
, CapHdrOffsets
, PciCapExtended
,
616 (UINT16
)ExtendedCapHdr
.CapabilityId
, ExtendedCapHdrOffset
,
617 (UINT8
)ExtendedCapHdr
.CapabilityVersion
);
618 if (RETURN_ERROR (Status
)) {
619 goto FreeCapHdrOffsets
;
622 ExtendedCapHdrOffset
= ExtendedCapHdr
.NextCapabilityOffset
& 0xFFC;
623 if (ExtendedCapHdrOffset
> 0 &&
624 ExtendedCapHdrOffset
< PCI_MAX_CONFIG_OFFSET
) {
626 // Invalid capability pointer.
628 Status
= RETURN_DEVICE_ERROR
;
629 goto FreeCapHdrOffsets
;
635 // Both capabilities lists have been parsed; compute the PCI_CAP.MaxSizeHint
636 // members if at least one capability has been found. In parallel, evacuate
637 // the CapHdrOffsets collection.
639 // At first, set OffsetEntry to the iterator of the PCI_CAP object with the
640 // lowest Offset (if such exists).
642 OffsetEntry
= OrderedCollectionMin (CapHdrOffsets
);
643 if (OffsetEntry
!= NULL
) {
644 ORDERED_COLLECTION_ENTRY
*NextOffsetEntry
;
648 // Initialize NextOffsetEntry to the iterator of the PCI_CAP object with
649 // the second lowest Offset (if such exists).
651 NextOffsetEntry
= OrderedCollectionNext (OffsetEntry
);
653 // Calculate MaxSizeHint for all PCI_CAP objects except the one with the
656 while (NextOffsetEntry
!= NULL
) {
659 OrderedCollectionDelete (CapHdrOffsets
, OffsetEntry
, (VOID
**)&PciCap
);
660 NextPciCap
= OrderedCollectionUserStruct (NextOffsetEntry
);
661 CalculatePciCapMaxSizeHint (PciCap
, NextPciCap
);
663 OffsetEntry
= NextOffsetEntry
;
664 NextOffsetEntry
= OrderedCollectionNext (OffsetEntry
);
667 // Calculate MaxSizeHint for the PCI_CAP object with the highest Offset.
669 OrderedCollectionDelete (CapHdrOffsets
, OffsetEntry
, (VOID
**)&PciCap
);
670 CalculatePciCapMaxSizeHint (PciCap
, NULL
);
672 ASSERT (OrderedCollectionIsEmpty (CapHdrOffsets
));
673 OrderedCollectionUninit (CapHdrOffsets
);
675 DebugDumpPciCapList (OutCapList
);
676 *CapList
= OutCapList
;
677 return RETURN_SUCCESS
;
680 EmptyAndUninitPciCapCollection (CapHdrOffsets
, FALSE
);
683 EmptyAndUninitPciCapCollection (OutCapList
->Capabilities
, TRUE
);
686 FreePool (OutCapList
);
688 ASSERT (RETURN_ERROR (Status
));
689 DEBUG ((DEBUG_ERROR
, "%a:%a: %r\n", gEfiCallerBaseName
, __FUNCTION__
,
696 Free the resources used by CapList.
698 @param[in] CapList The PCI_CAP_LIST object to free, originally produced by
704 IN PCI_CAP_LIST
*CapList
707 EmptyAndUninitPciCapCollection (CapList
->Capabilities
, TRUE
);
713 Locate a capability instance in the parsed capabilities lists.
715 @param[in] CapList The PCI_CAP_LIST object produced by PciCapListInit().
717 @param[in] Domain Distinguishes whether CapId is 8-bit wide and
718 interpreted in normal config space, or 16-bit wide and
719 interpreted in extended config space. Capability ID
720 definitions are relative to domain.
722 @param[in] CapId Capability identifier to look up.
724 @param[in] Instance Domain and CapId may identify a multi-instance
725 capability. When Instance is zero, the first instance of
726 the capability is located (in list traversal order --
727 which may not mean increasing config space offset
728 order). Higher Instance values locate subsequent
729 instances of the same capability (in list traversal
732 @param[out] Cap The capability instance that matches the search
733 criteria. Cap is owned by CapList and becomes invalid
734 when CapList is freed with PciCapListUninit().
735 PciCapListFindCap() may be called with Cap set to NULL,
736 in order to test the existence of a specific capability
739 @retval RETURN_SUCCESS The capability instance identified by (Domain,
740 CapId, Instance) has been found.
742 @retval RETURN_NOT_FOUND The requested (Domain, CapId, Instance) capability
743 instance does not exist.
748 IN PCI_CAP_LIST
*CapList
,
749 IN PCI_CAP_DOMAIN Domain
,
752 OUT PCI_CAP
**Cap OPTIONAL
756 ORDERED_COLLECTION_ENTRY
*PciCapEntry
;
760 Key
.Instance
= Instance
;
762 PciCapEntry
= OrderedCollectionFind (CapList
->Capabilities
, &Key
);
763 if (PciCapEntry
== NULL
) {
764 return RETURN_NOT_FOUND
;
767 *Cap
= OrderedCollectionUserStruct (PciCapEntry
);
769 return RETURN_SUCCESS
;
774 Locate the first instance of the capability given by (Domain, CapId) such
775 that the instance's Version is greater than or equal to MinVersion.
777 This is a convenience function that may save client code calls to
778 PciCapListFindCap() and PciCapGetInfo().
780 @param[in] CapList The PCI_CAP_LIST object produced by PciCapListInit().
782 @param[in] Domain Distinguishes whether CapId is 8-bit wide and
783 interpreted in normal config space, or 16-bit wide and
784 interpreted in extended config space. Capability ID
785 definitions are relative to domain.
787 @param[in] CapId Capability identifier to look up.
789 @param[in] MinVersion The minimum version that the capability instance is
790 required to have. Note that all capability instances
791 in Domain=PciCapNormal have Version=0.
793 @param[out] Cap The first capability instance that matches the search
794 criteria. Cap is owned by CapList and becomes invalid
795 when CapList is freed with PciCapListUninit().
796 PciCapListFindCapVersion() may be called with Cap set
797 to NULL, in order just to test whether the search
798 criteria are satisfiable.
800 @retval RETURN_SUCCESS The first capability instance matching (Domain,
801 CapId, MinVersion) has been located.
803 @retval RETURN_NOT_FOUND No capability instance matches (Domain, CapId,
808 PciCapListFindCapVersion (
809 IN PCI_CAP_LIST
*CapList
,
810 IN PCI_CAP_DOMAIN Domain
,
813 OUT PCI_CAP
**Cap OPTIONAL
817 ORDERED_COLLECTION_ENTRY
*PciCapEntry
;
820 // Start the version checks at Instance#0 of (Domain, CapId).
826 for (PciCapEntry
= OrderedCollectionFind (CapList
->Capabilities
, &Key
);
828 PciCapEntry
= OrderedCollectionNext (PciCapEntry
)) {
831 PciCap
= OrderedCollectionUserStruct (PciCapEntry
);
833 // PCI_CAP.Key ordering keeps instances of the same (Domain, CapId)
834 // adjacent to each other, so stop searching if either Domain or CapId
837 if (PciCap
->Key
.Domain
!= Domain
|| PciCap
->Key
.CapId
!= CapId
) {
840 if (PciCap
->Version
>= MinVersion
) {
847 return RETURN_SUCCESS
;
850 return RETURN_NOT_FOUND
;
855 Get information about a PCI Capability instance.
857 @param[in] Cap The capability instance to get info about, located with
858 PciCapListFindCap*().
860 @param[out] Info A PCI_CAP_INFO structure that describes the properties of
863 @retval RETURN_SUCCESS Fields of Info have been set.
865 @return Unspecified error codes, if filling in Info failed
872 OUT PCI_CAP_INFO
*Info
875 PCI_CAP
*InstanceZero
;
877 ASSERT (Info
!= NULL
);
879 InstanceZero
= (Cap
->Key
.Instance
== 0 ? Cap
:
880 Cap
->NumInstancesUnion
.InstanceZero
);
882 Info
->Domain
= Cap
->Key
.Domain
;
883 Info
->CapId
= Cap
->Key
.CapId
;
884 Info
->NumInstances
= InstanceZero
->NumInstancesUnion
.NumInstances
;
885 Info
->Instance
= Cap
->Key
.Instance
;
886 Info
->Offset
= Cap
->Offset
;
887 Info
->MaxSizeHint
= Cap
->MaxSizeHint
;
888 Info
->Version
= Cap
->Version
;
890 return RETURN_SUCCESS
;
895 Read a slice of a capability instance.
897 The function performs as few config space accesses as possible (without
898 attempting 64-bit wide accesses). PciCapRead() performs bounds checking on
899 SourceOffsetInCap and Size, and only invokes PciDevice->ReadConfig() if the
900 requested transfer falls within Cap.
902 @param[in] PciDevice Implementation-specific unique representation
903 of the PCI device in the PCI hierarchy.
905 @param[in] Cap The capability instance to read, located with
906 PciCapListFindCap*().
908 @param[in] SourceOffsetInCap Source offset relative to the capability
909 header to start reading from. A zero value
910 refers to the first byte of the capability
913 @param[out] DestinationBuffer Buffer to store the read data to.
915 @param[in] Size The number of bytes to transfer.
917 @retval RETURN_SUCCESS Size bytes have been transferred from Cap to
920 @retval RETURN_BAD_BUFFER_SIZE Reading Size bytes starting from
921 SourceOffsetInCap would not (entirely) be
922 contained within Cap, as suggested by
923 PCI_CAP_INFO.MaxSizeHint. No bytes have been
926 @return Error codes propagated from
927 PciDevice->ReadConfig(). Fewer than Size
928 bytes may have been read.
933 IN PCI_CAP_DEV
*PciDevice
,
935 IN UINT16 SourceOffsetInCap
,
936 OUT VOID
*DestinationBuffer
,
941 // Note: all UINT16 values are promoted to INT32 below, and addition and
942 // comparison take place between INT32 values.
944 if (SourceOffsetInCap
+ Size
> Cap
->MaxSizeHint
) {
945 return RETURN_BAD_BUFFER_SIZE
;
947 return PciDevice
->ReadConfig (PciDevice
, Cap
->Offset
+ SourceOffsetInCap
,
948 DestinationBuffer
, Size
);
953 Write a slice of a capability instance.
955 The function performs as few config space accesses as possible (without
956 attempting 64-bit wide accesses). PciCapWrite() performs bounds checking on
957 DestinationOffsetInCap and Size, and only invokes PciDevice->WriteConfig() if
958 the requested transfer falls within Cap.
960 @param[in] PciDevice Implementation-specific unique
961 representation of the PCI device in the
964 @param[in] Cap The capability instance to write, located
965 with PciCapListFindCap*().
967 @param[in] DestinationOffsetInCap Destination offset relative to the
968 capability header to start writing at. A
969 zero value refers to the first byte of the
972 @param[in] SourceBuffer Buffer to read the data to be stored from.
974 @param[in] Size The number of bytes to transfer.
976 @retval RETURN_SUCCESS Size bytes have been transferred from
979 @retval RETURN_BAD_BUFFER_SIZE Writing Size bytes starting at
980 DestinationOffsetInCap would not (entirely)
981 be contained within Cap, as suggested by
982 PCI_CAP_INFO.MaxSizeHint. No bytes have been
985 @return Error codes propagated from
986 PciDevice->WriteConfig(). Fewer than Size
987 bytes may have been written.
992 IN PCI_CAP_DEV
*PciDevice
,
994 IN UINT16 DestinationOffsetInCap
,
995 IN VOID
*SourceBuffer
,
1000 // Note: all UINT16 values are promoted to INT32 below, and addition and
1001 // comparison take place between INT32 values.
1003 if (DestinationOffsetInCap
+ Size
> Cap
->MaxSizeHint
) {
1004 return RETURN_BAD_BUFFER_SIZE
;
1006 return PciDevice
->WriteConfig (PciDevice
,
1007 Cap
->Offset
+ DestinationOffsetInCap
, SourceBuffer
,