# PCI

---

# The Linux PCI driver implementer's API guide

_Peios Kernel / Hardware and drivers / Driver development / PCI_

> Reference for The Linux PCI driver implementer's API guide.

<a id="rst-the-linux-pci-driver-implementer-s-api-guide"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

**Table of contents**

- [PCI Support Library](</kernel/hardware/driver-api/pci/pci.md>)
- [PCI Peer-to-Peer DMA Support](</kernel/hardware/driver-api/pci/p2pdma.md>)
- [PCI Trusted Execution Environment Security Manager (TSM)](</kernel/hardware/driver-api/pci/tsm.md>)

---

# PCI Peer-to-Peer DMA Support

_Peios Kernel / Hardware and drivers / Driver development / PCI_

> The PCI bus has pretty decent support for performing DMA transfers between two devices on the bus. This type of transaction is henceforth called Peer-to-Peer (or P2P). However, there are a number of issues that make P2P transactions tricky…

<a id="rst-pci-peer-to-peer-dma-support"></a>

<!-- SPDX-License-Identifier: GPL-2.0 -->

The PCI bus has pretty decent support for performing DMA transfers between two devices on the bus. This type of transaction is henceforth called Peer-to-Peer (or P2P). However, there are a number of issues that make P2P transactions tricky to do in a perfectly safe way.

For PCIe the routing of Transaction Layer Packets (TLPs) is well-defined up until they reach a host bridge or root port. If the path includes PCIe switches then based on the ACS settings the transaction can route entirely within the PCIe hierarchy and never reach the root port. The kernel will evaluate the PCIe topology and always permit P2P in these well-defined cases.

However, if the P2P transaction reaches the host bridge then it might have to hairpin back out the same root port, be routed inside the CPU SOC to another PCIe root port, or routed internally to the SOC.

The PCIe specification doesn't define the forwarding of transactions between hierarchy domains and kernel defaults to blocking such routing. There is an allow list to allow detecting known-good HW, in which case P2P between any two PCIe devices will be permitted.

Since P2P inherently is doing transactions between two devices it requires two drivers to be co-operating inside the kernel. The providing driver has to convey its MMIO to the consuming driver. To meet the driver model lifecycle rules the MMIO must have all DMA mapping removed, all CPU accesses prevented, all page table mappings undone before the providing driver completes remove().

This requires the providing and consuming driver to actively work together to guarantee that the consuming driver has stopped using the MMIO during a removal cycle. This is done by either a synchronous invalidation shutdown or waiting for all usage refcounts to reach zero.

At the lowest level the P2P subsystem offers a naked struct p2p_provider that delegates lifecycle management to the providing driver. It is expected that drivers using this option will wrap their MMIO memory in DMABUF and use DMABUF to provide an invalidation shutdown. These MMIO addresess have no struct page, and if used with mmap() must create special PTEs. As such there are very few kernel uAPIs that can accept pointers to them; in particular they cannot be used with read()/write(), including O_DIRECT.

Building on this, the subsystem offers a layer to wrap the MMIO in a ZONE_DEVICE pgmap of MEMORY_DEVICE_PCI_P2PDMA to create struct pages. The lifecycle of pgmap ensures that when the pgmap is destroyed all other drivers have stopped using the MMIO. This option works with O_DIRECT flows, in some cases, if the underlying subsystem supports handling MEMORY_DEVICE_PCI_P2PDMA through FOLL_PCI_P2PDMA. The use of FOLL_LONGTERM is prevented. As this relies on pgmap it also relies on architecture support along with alignment and minimum size limitations.

<a id="rst-driver-writer-s-guide"></a>

## Driver Writer's Guide

In a given P2P implementation there may be three or more different types of kernel drivers in play:

- Provider - A driver which provides or publishes P2P resources like memory or doorbell registers to other drivers.

- Client - A driver which makes use of a resource by setting up a DMA transaction to or from it.

- Orchestrator - A driver which orchestrates the flow of data between clients and providers.

In many cases there could be overlap between these three types (i.e., it may be typical for a driver to be both a provider and a client).

For example, in the NVMe Target Copy Offload implementation:

- The NVMe PCI driver is both a client, provider and orchestrator in that it exposes any CMB (Controller Memory Buffer) as a P2P memory resource (provider), it accepts P2P memory pages as buffers in requests to be used directly (client) and it can also make use of the CMB as submission queue entries (orchestrator).

- The RDMA driver is a client in this arrangement so that an RNIC can DMA directly to the memory exposed by the NVMe device.

- The NVMe Target driver (nvmet) can orchestrate the data from the RNIC to the P2P memory (CMB) and then to the NVMe device (and vice versa).

This is currently the only arrangement supported by the kernel but one could imagine slight tweaks to this that would allow for the same functionality. For example, if a specific RNIC added a BAR with some memory behind it, its driver could add support as a P2P provider and then the NVMe Target could use the RNIC's memory instead of the CMB in cases where the NVMe cards in use do not have CMB support.

<a id="rst-provider-drivers"></a>

### Provider Drivers

A provider simply needs to register a BAR (or a portion of a BAR) as a P2P DMA resource using `pci_p2pdma_add_resource()`. This will register struct pages for all the specified memory.

After that it may optionally publish all of its resources as P2P memory using `pci_p2pmem_publish()`. This will allow any orchestrator drivers to find and use the memory. When marked in this way, the resource must be regular memory with no side effects.

For the time being this is fairly rudimentary in that all resources are typically going to be P2P memory. Future work will likely expand this to include other types of resources like doorbells.

<a id="rst-client-drivers"></a>

### Client Drivers

A client driver only has to use the mapping API `dma_map_sg()` and `dma_unmap_sg()` functions as usual, and the implementation will do the right thing for the P2P capable memory.

<a id="rst-orchestrator-drivers"></a>

### Orchestrator Drivers

The first task an orchestrator driver must do is compile a list of all client devices that will be involved in a given transaction. For example, the NVMe Target driver creates a list including the namespace block device and the RNIC in use. If the orchestrator has access to a specific P2P provider to use it may check compatibility using `pci_p2pdma_distance()` otherwise it may find a memory provider that's compatible with all clients using  `pci_p2pmem_find()`. If more than one provider is supported, the one nearest to all the clients will be chosen first. If more than one provider is an equal distance away, the one returned will be chosen at random (it is not an arbitrary but truly random). This function returns the PCI device to use for the provider with a reference taken and therefore when it's no longer needed it should be returned with pci_dev_put().

Once a provider is selected, the orchestrator can then use `pci_alloc_p2pmem()` and `pci_free_p2pmem()` to allocate P2P memory from the provider. `pci_p2pmem_alloc_sgl()` and `pci_p2pmem_free_sgl()` are convenience functions for allocating scatter-gather lists with P2P memory.

<a id="rst-struct-page-caveats"></a>

### Struct Page Caveats

While the MEMORY_DEVICE_PCI_P2PDMA pages can be installed in VMAs, pin_user_pages() and related will not return them unless FOLL_PCI_P2PDMA is set.

The MEMORY_DEVICE_PCI_P2PDMA pages require care to support in the kernel. The KVA is still MMIO and must still be accessed through the normal readX()/writeX()/etc helpers. Direct CPU access (e.g. memcpy) is forbidden, just like any other MMIO mapping. While this will actually work on some architectures, others will experience corruption or just crash in the kernel. Supporting FOLL_PCI_P2PDMA in a subsystem requires scrubbing it to ensure no CPU access happens.

<a id="rst-usage-with-dmabuf"></a>

## Usage With DMABUF

DMABUF provides an alternative to the above struct page-based client/provider/orchestrator system and should be used when struct page doesn't exist. In this mode the exporting driver will wrap some of its MMIO in a DMABUF and give the DMABUF FD to userspace.

Userspace can then pass the FD to an importing driver which will ask the exporting driver to map it to the importer.

In this case the initiator and target pci_devices are known and the P2P subsystem is used to determine the mapping type. The phys_addr_t-based DMA API is used to establish the dma_addr_t.

Lifecycle is controlled by DMABUF move_notify(). When the exporting driver wants to remove() it must deliver an invalidation shutdown to all DMABUF importing drivers through move_notify() and synchronously DMA unmap all the MMIO.

No importing driver can continue to have a DMA map to the MMIO after the exporting driver has destroyed its p2p_provider.

<a id="rst-p2p-dma-support-library"></a>

## P2P DMA Support Library


#### <a id="c-pcim_p2pdma_init"></a>`pcim_p2pdma_init()`

```c
int pcim_p2pdma_init(struct pci_dev *pdev)
```

Initialise peer-to-peer DMA providers

**Parameters**

- `struct pci_dev *pdev`: The PCI device to enable P2PDMA for

**Description**

This function initializes the peer-to-peer DMA infrastructure
for a PCI device. It allocates and sets up the necessary data
structures to support P2PDMA operations, including mapping type
tracking.

#### <a id="c-pcim_p2pdma_provider"></a>`pcim_p2pdma_provider()`

```c
struct p2pdma_provider * pcim_p2pdma_provider(struct pci_dev *pdev, int bar)
```

Get peer-to-peer DMA provider

**Parameters**

- `struct pci_dev *pdev`: The PCI device to enable P2PDMA for
- `int bar`: BAR index to get provider

**Description**

This function gets peer-to-peer DMA provider for a PCI device. The lifetime
of the provider (and of course the MMIO) is bound to the lifetime of the
driver. A driver calling this function must ensure that all references to the
provider, and any DMA mappings created for any MMIO, are all cleaned up
before the driver `remove()` completes.

Since P2P is almost always shared with a second driver this means some system
to notify, invalidate and revoke the MMIO's DMA must be in place to use this
function. For example a revoke can be built using DMABUF.

#### <a id="c-pci_p2pdma_add_resource"></a>`pci_p2pdma_add_resource()`

```c
int pci_p2pdma_add_resource(struct pci_dev *pdev, int bar, size_t size, u64 offset)
```

add memory for use as p2p memory

**Parameters**

- `struct pci_dev *pdev`: the device to add the memory to
- `int bar`: PCI BAR to add
- `size_t size`: size of the memory to add, may be zero to use the whole BAR
- `u64 offset`: offset into the PCI BAR

**Description**

The memory will be given ZONE_DEVICE struct pages so that it may
be used with any DMA request.

#### <a id="c-pci_p2pdma_distance_many"></a>`pci_p2pdma_distance_many()`

```c
int pci_p2pdma_distance_many(struct pci_dev *provider, struct device **clients, int num_clients, bool verbose)
```

Determine the cumulative distance between a p2pdma provider and the clients in use.

**Parameters**

- `struct pci_dev *provider`: p2pdma provider to check against the client list
- `struct device **clients`: array of devices to check (NULL-terminated)
- `int num_clients`: number of clients in the array
- `bool verbose`: if true, print warnings for devices when we return -1

**Description**

Returns -1 if any of the clients are not compatible, otherwise returns a
positive number where a lower number is the preferable choice. (If there's
one client that's the same as the provider it will return 0, which is best
choice).

"compatible" means the provider and the clients are either all behind
the same PCI root port or the host bridges connected to each of the devices
are listed in the 'pci_p2pdma_whitelist'.

#### <a id="c-pci_p2pmem_find_many"></a>`pci_p2pmem_find_many()`

```c
struct pci_dev * pci_p2pmem_find_many(struct device **clients, int num_clients)
```

find a peer-to-peer DMA memory device compatible with the specified list of clients and shortest distance

**Parameters**

- `struct device **clients`: array of devices to check (NULL-terminated)
- `int num_clients`: number of client devices in the list

**Description**

If multiple devices are behind the same switch, the one "closest" to the
client devices in use will be chosen first. (So if one of the providers is
the same as one of the clients, that provider will be used ahead of any
other providers that are unrelated). If multiple providers are an equal
distance away, one will be chosen at random.

Returns a pointer to the PCI device with a reference taken (use pci_dev_put
to return the reference) or NULL if no compatible device is found. The
found provider will also be assigned to the client list.

#### <a id="c-pci_alloc_p2pmem"></a>`pci_alloc_p2pmem()`

```c
void * pci_alloc_p2pmem(struct pci_dev *pdev, size_t size)
```

allocate peer-to-peer DMA memory

**Parameters**

- `struct pci_dev *pdev`: the device to allocate memory from
- `size_t size`: number of bytes to allocate

**Description**

Returns the allocated memory or NULL on error.

#### <a id="c-pci_free_p2pmem"></a>`pci_free_p2pmem()`

```c
void pci_free_p2pmem(struct pci_dev *pdev, void *addr, size_t size)
```

free peer-to-peer DMA memory

**Parameters**

- `struct pci_dev *pdev`: the device the memory was allocated from
- `void *addr`: address of the memory that was allocated
- `size_t size`: number of bytes that were allocated

#### <a id="c-pci_p2pmem_virt_to_bus"></a>`pci_p2pmem_virt_to_bus()`

```c
pci_bus_addr_t pci_p2pmem_virt_to_bus(struct pci_dev *pdev, void *addr)
```

return the PCI bus address for a given virtual address obtained with [`pci_alloc_p2pmem()`](#c-pci_alloc_p2pmem)

**Parameters**

- `struct pci_dev *pdev`: the device the memory was allocated from
- `void *addr`: address of the memory that was allocated

#### <a id="c-pci_p2pmem_alloc_sgl"></a>`pci_p2pmem_alloc_sgl()`

```c
struct scatterlist * pci_p2pmem_alloc_sgl(struct pci_dev *pdev, unsigned int *nents, u32 length)
```

allocate peer-to-peer DMA memory in a scatterlist

**Parameters**

- `struct pci_dev *pdev`: the device to allocate memory from
- `unsigned int *nents`: the number of SG entries in the list
- `u32 length`: number of bytes to allocate

**Return**

`NULL` on error or `struct scatterlist` pointer and **nents** on success

#### <a id="c-pci_p2pmem_free_sgl"></a>`pci_p2pmem_free_sgl()`

```c
void pci_p2pmem_free_sgl(struct pci_dev *pdev, struct scatterlist *sgl)
```

free a scatterlist allocated by [`pci_p2pmem_alloc_sgl()`](#c-pci_p2pmem_alloc_sgl)

**Parameters**

- `struct pci_dev *pdev`: the device to allocate memory from
- `struct scatterlist *sgl`: the allocated scatterlist

#### <a id="c-pci_p2pmem_publish"></a>`pci_p2pmem_publish()`

```c
void pci_p2pmem_publish(struct pci_dev *pdev, bool publish)
```

publish the peer-to-peer DMA memory for use by other devices with `pci_p2pmem_find()`

**Parameters**

- `struct pci_dev *pdev`: the device with peer-to-peer DMA memory to publish
- `bool publish`: set to true to publish the memory, false to unpublish it

**Description**

Published memory can be used by other PCI device drivers for
peer-2-peer DMA operations. Non-published memory is reserved for
exclusive use of the device driver that registers the peer-to-peer
memory.

#### <a id="c-pci_p2pdma_enable_store"></a>`pci_p2pdma_enable_store()`

```c
int pci_p2pdma_enable_store(const char *page, struct pci_dev **p2p_dev, bool *use_p2pdma)
```

parse a configfs/sysfs attribute store to enable p2pdma

**Parameters**

- `const char *page`: contents of the value to be stored
- `struct pci_dev **p2p_dev`: returns the PCI device that was selected to be used
  (if one was specified in the stored value)
- `bool *use_p2pdma`: returns whether to enable p2pdma or not

**Description**

Parses an attribute value to decide whether to enable p2pdma.
The value can select a PCI device (using its full BDF device
name) or a boolean (in any format [`kstrtobool()`](/kernel/internals/core-api/kernel-api.md#c-kstrtobool) accepts). A false
value disables p2pdma, a true value expects the caller
to automatically find a compatible device and specifying a PCI device
expects the caller to use the specific provider.

[`pci_p2pdma_enable_show()`](#c-pci_p2pdma_enable_show) should be used as the show operation for
the attribute.

Returns 0 on success

#### <a id="c-pci_p2pdma_enable_show"></a>`pci_p2pdma_enable_show()`

```c
ssize_t pci_p2pdma_enable_show(char *page, struct pci_dev *p2p_dev, bool use_p2pdma)
```

show a configfs/sysfs attribute indicating whether p2pdma is enabled

**Parameters**

- `char *page`: contents of the stored value
- `struct pci_dev *p2p_dev`: the selected p2p device (NULL if no device is selected)
- `bool use_p2pdma`: whether p2pdma has been enabled

**Description**

Attributes that use [`pci_p2pdma_enable_store()`](#c-pci_p2pdma_enable_store) should use this function
to show the value of the attribute.

Returns 0 on success

---

# PCI Support Library

_Peios Kernel / Hardware and drivers / Driver development / PCI_

> Reference for PCI Support Library.

<a id="rst-pci-support-library"></a>

## PCI Support Library


#### <a id="c-pci_bus_max_busnr"></a>`pci_bus_max_busnr()`

```c
unsigned char pci_bus_max_busnr(struct pci_bus *bus)
```

returns maximum PCI bus number of given bus' children

**Parameters**

- `struct pci_bus *bus`: pointer to PCI bus structure to search

**Description**

Given a PCI bus, returns the highest PCI bus number present in the set
including the given PCI bus and its list of child PCI buses.

#### <a id="c-pci_status_get_and_clear_errors"></a>`pci_status_get_and_clear_errors()`

```c
int pci_status_get_and_clear_errors(struct pci_dev *pdev)
```

return and clear error bits in PCI_STATUS

**Parameters**

- `struct pci_dev *pdev`: the PCI device

**Description**

Returns error bits set in PCI_STATUS and clears them.

#### <a id="c-pci_find_capability"></a>`pci_find_capability()`

```c
u8 pci_find_capability(struct pci_dev *dev, int cap)
```

query for devices' capabilities

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `int cap`: capability code

**Description**

Tell if a device supports a given PCI capability.
Returns the address of the requested capability structure within the
device's PCI configuration space or 0 in case the device does not
support it.  Possible values for **cap** include:

`PCI_CAP_ID_PM`           Power Management
`PCI_CAP_ID_AGP`          Accelerated Graphics Port
`PCI_CAP_ID_VPD`          Vital Product Data
`PCI_CAP_ID_SLOTID`       Slot Identification
`PCI_CAP_ID_MSI`          Message Signalled Interrupts
`PCI_CAP_ID_CHSWP`        CompactPCI HotSwap
`PCI_CAP_ID_PCIX`         PCI-X
`PCI_CAP_ID_EXP`          PCI Express

#### <a id="c-pci_bus_find_capability"></a>`pci_bus_find_capability()`

```c
u8 pci_bus_find_capability(struct pci_bus *bus, unsigned int devfn, int cap)
```

query for devices' capabilities

**Parameters**

- `struct pci_bus *bus`: the PCI bus to query
- `unsigned int devfn`: PCI device to query
- `int cap`: capability code

**Description**

Like [`pci_find_capability()`](#c-pci_find_capability) but works for PCI devices that do not have a
pci_dev structure set up yet.

Returns the address of the requested capability structure within the
device's PCI configuration space or 0 in case the device does not
support it.

#### <a id="c-pci_find_next_ext_capability"></a>`pci_find_next_ext_capability()`

```c
u16 pci_find_next_ext_capability(struct pci_dev *dev, u16 start, int cap)
```

Find an extended capability

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `u16 start`: address at which to start looking (0 to start at beginning of list)
- `int cap`: capability code

**Description**

Returns the address of the next matching extended capability structure
within the device's PCI configuration space or 0 if the device does
not support it.  Some capabilities can occur several times, e.g., the
vendor-specific capability, and this provides a way to find them all.

#### <a id="c-pci_find_ext_capability"></a>`pci_find_ext_capability()`

```c
u16 pci_find_ext_capability(struct pci_dev *dev, int cap)
```

Find an extended capability

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `int cap`: capability code

**Description**

Returns the address of the requested extended capability structure
within the device's PCI configuration space or 0 if the device does
not support it.  Possible values for **cap** include:

`PCI_EXT_CAP_ID_ERR`         Advanced Error Reporting
`PCI_EXT_CAP_ID_VC`          Virtual Channel
`PCI_EXT_CAP_ID_DSN`         Device Serial Number
`PCI_EXT_CAP_ID_PWR`         Power Budgeting

#### <a id="c-pci_get_dsn"></a>`pci_get_dsn()`

```c
u64 pci_get_dsn(struct pci_dev *dev)
```

Read and return the 8-byte Device Serial Number

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Looks up the PCI_EXT_CAP_ID_DSN and reads the 8 bytes of the Device Serial
Number.

Returns the DSN, or zero if the capability does not exist.

#### <a id="c-pci_find_next_ht_capability"></a>`pci_find_next_ht_capability()`

```c
u8 pci_find_next_ht_capability(struct pci_dev *dev, u8 pos, int ht_cap)
```

query a device's HyperTransport capabilities

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `u8 pos`: Position from which to continue searching
- `int ht_cap`: HyperTransport capability code

**Description**

To be used in conjunction with [`pci_find_ht_capability()`](#c-pci_find_ht_capability) to search for
all capabilities matching **ht_cap**. **pos** should always be a value returned
from [`pci_find_ht_capability()`](#c-pci_find_ht_capability).

NB. To be 100% safe against broken PCI devices, the caller should take
steps to avoid an infinite loop.

#### <a id="c-pci_find_ht_capability"></a>`pci_find_ht_capability()`

```c
u8 pci_find_ht_capability(struct pci_dev *dev, int ht_cap)
```

query a device's HyperTransport capabilities

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `int ht_cap`: HyperTransport capability code

**Description**

Tell if a device supports a given HyperTransport capability.
Returns an address within the device's PCI configuration space
or 0 in case the device does not support the request capability.
The address points to the PCI capability, of type PCI_CAP_ID_HT,
which has a HyperTransport capability matching **ht_cap**.

#### <a id="c-pci_find_vsec_capability"></a>`pci_find_vsec_capability()`

```c
u16 pci_find_vsec_capability(struct pci_dev *dev, u16 vendor, int cap)
```

Find a vendor-specific extended capability

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `u16 vendor`: Vendor ID for which capability is defined
- `int cap`: Vendor-specific capability ID

**Description**

If **dev** has Vendor ID **vendor**, search for a VSEC capability with
VSEC ID **cap**. If found, return the capability offset in
config space; otherwise return 0.

#### <a id="c-pci_find_dvsec_capability"></a>`pci_find_dvsec_capability()`

```c
u16 pci_find_dvsec_capability(struct pci_dev *dev, u16 vendor, u16 dvsec)
```

Find DVSEC for vendor

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `u16 vendor`: Vendor ID to match for the DVSEC
- `u16 dvsec`: Designated Vendor-specific capability ID

**Description**

If DVSEC has Vendor ID **vendor** and DVSEC ID **dvsec** return the capability
offset in config space; otherwise return 0.

#### <a id="c-pci_find_parent_resource"></a>`pci_find_parent_resource()`

```c
struct resource * pci_find_parent_resource(const struct pci_dev *dev, struct resource *res)
```

return resource region of parent bus of given region

**Parameters**

- `const struct pci_dev *dev`: PCI device structure contains resources to be searched
- `struct resource *res`: child resource record for which parent is sought

**Description**

For given resource region of given device, return the resource region of
parent bus the given region is contained in.

#### <a id="c-pci_find_resource"></a>`pci_find_resource()`

```c
struct resource * pci_find_resource(struct pci_dev *dev, struct resource *res)
```

Return matching PCI device resource

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `struct resource *res`: Resource to look for

**Description**

Goes over standard PCI resources (BARs) and checks if the given resource
is partially or fully contained in any of them. In that case the
matching resource is returned, `NULL` otherwise.

#### <a id="c-pci_platform_power_transition"></a>`pci_platform_power_transition()`

```c
int pci_platform_power_transition(struct pci_dev *dev, pci_power_t state)
```

Use platform to change device power state

**Parameters**

- `struct pci_dev *dev`: PCI device to handle.
- `pci_power_t state`: State to put the device into.

#### <a id="c-pci_set_power_state"></a>`pci_set_power_state()`

```c
int pci_set_power_state(struct pci_dev *dev, pci_power_t state)
```

Set the power state of a PCI device

**Parameters**

- `struct pci_dev *dev`: PCI device to handle.
- `pci_power_t state`: PCI power state (D0, D1, D2, D3hot) to put the device into.

**Description**

Transition a device to a new power state, using the platform firmware and/or
the device's PCI PM registers.

RETURN VALUE:
-EINVAL if the requested state is invalid.
-EIO if device does not support PCI PM or its PM capabilities register has a
wrong version, or device doesn't support the requested state.
0 if the transition is to D1 or D2 but D1 and D2 are not supported.
0 if device already is in the requested state.
0 if the transition is to D3 but D3 is not supported.
0 if device's power state has been successfully changed.

#### <a id="c-pci_save_state"></a>`pci_save_state()`

```c
int pci_save_state(struct pci_dev *dev)
```

save the PCI configuration space of a device before suspending

**Parameters**

- `struct pci_dev *dev`: PCI device that we're dealing with

#### <a id="c-pci_restore_state"></a>`pci_restore_state()`

```c
void pci_restore_state(struct pci_dev *dev)
```

Restore the saved state of a PCI device

**Parameters**

- `struct pci_dev *dev`: PCI device that we're dealing with

#### <a id="c-pci_store_saved_state"></a>`pci_store_saved_state()`

```c
struct pci_saved_state * pci_store_saved_state(struct pci_dev *dev)
```

Allocate and return an opaque struct containing the device saved state.

**Parameters**

- `struct pci_dev *dev`: PCI device that we're dealing with

**Description**

Return NULL if no state or error.

#### <a id="c-pci_load_saved_state"></a>`pci_load_saved_state()`

```c
int pci_load_saved_state(struct pci_dev *dev, struct pci_saved_state *state)
```

Reload the provided save state into struct pci_dev.

**Parameters**

- `struct pci_dev *dev`: PCI device that we're dealing with
- `struct pci_saved_state *state`: Saved state returned from [`pci_store_saved_state()`](#c-pci_store_saved_state)

#### <a id="c-pci_load_and_free_saved_state"></a>`pci_load_and_free_saved_state()`

```c
int pci_load_and_free_saved_state(struct pci_dev *dev, struct pci_saved_state **state)
```

Reload the save state pointed to by state, and free the memory allocated for it.

**Parameters**

- `struct pci_dev *dev`: PCI device that we're dealing with
- `struct pci_saved_state **state`: Pointer to saved state returned from [`pci_store_saved_state()`](#c-pci_store_saved_state)

#### <a id="c-pci_reenable_device"></a>`pci_reenable_device()`

```c
int pci_reenable_device(struct pci_dev *dev)
```

Resume abandoned device

**Parameters**

- `struct pci_dev *dev`: PCI device to be resumed

**NOTE**

This function is a backend of `pci_default_resume()` and is not supposed
to be called by normal code, write proper resume handler and use it instead.

#### <a id="c-pci_enable_device_mem"></a>`pci_enable_device_mem()`

```c
int pci_enable_device_mem(struct pci_dev *dev)
```

Initialize a device for use with Memory space

**Parameters**

- `struct pci_dev *dev`: PCI device to be initialized

**Description**

Initialize device before it's used by a driver. Ask low-level code
to enable Memory resources. Wake up the device if it was suspended.
Beware, this function can fail.

#### <a id="c-pci_enable_device"></a>`pci_enable_device()`

```c
int pci_enable_device(struct pci_dev *dev)
```

Initialize device before it's used by a driver.

**Parameters**

- `struct pci_dev *dev`: PCI device to be initialized

**Description**

Initialize device before it's used by a driver. Ask low-level code
to enable I/O and memory. Wake up the device if it was suspended.
Beware, this function can fail.

Note we don't actually enable the device many times if we call
this function repeatedly (we just increment the count).

#### <a id="c-pci_disable_device"></a>`pci_disable_device()`

```c
void pci_disable_device(struct pci_dev *dev)
```

Disable PCI device after use

**Parameters**

- `struct pci_dev *dev`: PCI device to be disabled

**Description**

Signal to the system that the PCI device is not in use by the system
anymore.  This only involves disabling PCI bus-mastering, if active.

Note we don't actually disable the device until all callers of
[`pci_enable_device()`](#c-pci_enable_device) have called [`pci_disable_device()`](#c-pci_disable_device).

#### <a id="c-pci_set_pcie_reset_state"></a>`pci_set_pcie_reset_state()`

```c
int pci_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
```

set reset state for device dev

**Parameters**

- `struct pci_dev *dev`: the PCIe device reset
- `enum pcie_reset_state state`: Reset state to enter into

**Description**

Sets the PCI reset state for the device.

#### <a id="c-pci_pme_capable"></a>`pci_pme_capable()`

```c
bool pci_pme_capable(struct pci_dev *dev, pci_power_t state)
```

check the capability of PCI device to generate PME#

**Parameters**

- `struct pci_dev *dev`: PCI device to handle.
- `pci_power_t state`: PCI state from which device will issue PME#.

#### <a id="c-pci_pme_active"></a>`pci_pme_active()`

```c
void pci_pme_active(struct pci_dev *dev, bool enable)
```

enable or disable PCI device's PME# function

**Parameters**

- `struct pci_dev *dev`: PCI device to handle.
- `bool enable`: 'true' to enable PME# generation; 'false' to disable it.

**Description**

The caller must verify that the device is capable of generating PME# before
calling this function with **enable** equal to 'true'.

#### <a id="c-pci_enable_wake"></a>`pci_enable_wake()`

```c
int pci_enable_wake(struct pci_dev *pci_dev, pci_power_t state, bool enable)
```

change wakeup settings for a PCI device

**Parameters**

- `struct pci_dev *pci_dev`: Target device
- `pci_power_t state`: PCI state from which device will issue wakeup events
- `bool enable`: Whether or not to enable event generation

**Description**

If **enable** is set, check `device_may_wakeup()` for the device before calling
`__pci_enable_wake()` for it.

#### <a id="c-pci_wake_from_d3"></a>`pci_wake_from_d3()`

```c
int pci_wake_from_d3(struct pci_dev *dev, bool enable)
```

enable/disable device to wake up from D3_hot or D3_cold

**Parameters**

- `struct pci_dev *dev`: PCI device to prepare
- `bool enable`: True to enable wake-up event generation; false to disable

**Description**

Many drivers want the device to wake up the system from D3_hot or D3_cold
and this function allows them to set that up cleanly - [`pci_enable_wake()`](#c-pci_enable_wake)
should not be called twice in a row to enable wake-up due to PCI PM vs ACPI
ordering constraints.

This function only returns error code if the device is not allowed to wake
up the system from sleep or it is not capable of generating PME# from both
D3_hot and D3_cold and the platform is unable to enable wake-up power for it.

#### <a id="c-pci_prepare_to_sleep"></a>`pci_prepare_to_sleep()`

```c
int pci_prepare_to_sleep(struct pci_dev *dev)
```

prepare PCI device for system-wide transition into a sleep state

**Parameters**

- `struct pci_dev *dev`: Device to handle.

**Description**

Choose the power state appropriate for the device depending on whether
it can wake up the system and/or is power manageable by the platform
(PCI_D3hot is the default) and put the device into that state.

#### <a id="c-pci_back_from_sleep"></a>`pci_back_from_sleep()`

```c
int pci_back_from_sleep(struct pci_dev *dev)
```

turn PCI device on during system-wide transition into working state

**Parameters**

- `struct pci_dev *dev`: Device to handle.

**Description**

Disable device's system wake-up capability and put it into D0.

#### <a id="c-pci_dev_run_wake"></a>`pci_dev_run_wake()`

```c
bool pci_dev_run_wake(struct pci_dev *dev)
```

Check if device can generate run-time wake-up events.

**Parameters**

- `struct pci_dev *dev`: Device to check.

**Description**

Return true if the device itself is capable of generating wake-up events
(through the platform or using the native PCIe PME) or if the device supports
PME and one of its upstream bridges can generate wake-up events.

#### <a id="c-pci_choose_state"></a>`pci_choose_state()`

```c
pci_power_t pci_choose_state(struct pci_dev *dev, pm_message_t state)
```

Choose the power state of a PCI device.

**Parameters**

- `struct pci_dev *dev`: Target PCI device.
- `pm_message_t state`: Target state for the whole system.

**Description**

Returns PCI power state suitable for **dev** and **state**.

#### <a id="c-pci_d3cold_enable"></a>`pci_d3cold_enable()`

```c
void pci_d3cold_enable(struct pci_dev *dev)
```

Enable D3cold for device

**Parameters**

- `struct pci_dev *dev`: PCI device to handle

**Description**

This function can be used in drivers to enable D3cold from the device
they handle.  It also updates upstream PCI bridge PM capabilities
accordingly.

#### <a id="c-pci_d3cold_disable"></a>`pci_d3cold_disable()`

```c
void pci_d3cold_disable(struct pci_dev *dev)
```

Disable D3cold for device

**Parameters**

- `struct pci_dev *dev`: PCI device to handle

**Description**

This function can be used in drivers to disable D3cold from the device
they handle.  It also updates upstream PCI bridge PM capabilities
accordingly.

#### <a id="c-pci_enable_atomic_ops_to_root"></a>`pci_enable_atomic_ops_to_root()`

```c
int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask)
```

enable AtomicOp requests to root port

**Parameters**

- `struct pci_dev *dev`: the PCI device
- `u32 cap_mask`: mask of desired AtomicOp sizes, including one or more of:
  PCI_EXP_DEVCAP2_ATOMIC_COMP32
  PCI_EXP_DEVCAP2_ATOMIC_COMP64
  PCI_EXP_DEVCAP2_ATOMIC_COMP128

**Description**

Return 0 if all upstream bridges support AtomicOp routing, egress
blocking is disabled on all upstream ports, and the root port supports
the requested completion capabilities (32-bit, 64-bit and/or 128-bit
AtomicOp completion), or negative otherwise.

#### <a id="c-pci_release_region"></a>`pci_release_region()`

```c
void pci_release_region(struct pci_dev *pdev, int bar)
```

Release a PCI bar

**Parameters**

- `struct pci_dev *pdev`: PCI device whose resources were previously reserved by
  [`pci_request_region()`](#c-pci_request_region)
- `int bar`: BAR to release

**Description**

Releases the PCI I/O and memory resources previously reserved by a
successful call to [`pci_request_region()`](#c-pci_request_region).  Call this function only
after all use of the PCI regions has ceased.

#### <a id="c-pci_request_region"></a>`pci_request_region()`

```c
int pci_request_region(struct pci_dev *pdev, int bar, const char *name)
```

Reserve PCI I/O and memory resource

**Parameters**

- `struct pci_dev *pdev`: PCI device whose resources are to be reserved
- `int bar`: BAR to be reserved
- `const char *name`: name of the driver requesting the resource

**Return**

0 on success, negative error code on failure.

**Description**

Mark the PCI region associated with PCI device **pdev** BAR **bar** as being
reserved by owner **name**. Do not access any address inside the PCI regions
unless this call returns successfully.

Returns 0 on success, or `EBUSY` on error.  A warning
message is also printed on failure.

#### <a id="c-pci_release_selected_regions"></a>`pci_release_selected_regions()`

```c
void pci_release_selected_regions(struct pci_dev *pdev, int bars)
```

Release selected PCI I/O and memory resources

**Parameters**

- `struct pci_dev *pdev`: PCI device whose resources were previously reserved
- `int bars`: Bitmask of BARs to be released

**Description**

Release selected PCI I/O and memory resources previously reserved.
Call this function only after all use of the PCI regions has ceased.

#### <a id="c-pci_request_selected_regions"></a>`pci_request_selected_regions()`

```c
int pci_request_selected_regions(struct pci_dev *pdev, int bars, const char *name)
```

Reserve selected PCI I/O and memory resources

**Parameters**

- `struct pci_dev *pdev`: PCI device whose resources are to be reserved
- `int bars`: Bitmask of BARs to be requested
- `const char *name`: Name of the driver requesting the resources

**Return**

0 on success, negative error code on failure.

#### <a id="c-pci_request_selected_regions_exclusive"></a>`pci_request_selected_regions_exclusive()`

```c
int pci_request_selected_regions_exclusive(struct pci_dev *pdev, int bars, const char *name)
```

Request regions exclusively

**Parameters**

- `struct pci_dev *pdev`: PCI device to request regions from
- `int bars`: bit mask of BARs to request
- `const char *name`: name of the driver requesting the resources

**Return**

0 on success, negative error code on failure.

#### <a id="c-pci_release_regions"></a>`pci_release_regions()`

```c
void pci_release_regions(struct pci_dev *pdev)
```

Release reserved PCI I/O and memory resources

**Parameters**

- `struct pci_dev *pdev`: PCI device whose resources were previously reserved by
  [`pci_request_regions()`](#c-pci_request_regions)

**Description**

Releases all PCI I/O and memory resources previously reserved by a
successful call to [`pci_request_regions()`](#c-pci_request_regions).  Call this function only
after all use of the PCI regions has ceased.

#### <a id="c-pci_request_regions"></a>`pci_request_regions()`

```c
int pci_request_regions(struct pci_dev *pdev, const char *name)
```

Reserve PCI I/O and memory resources

**Parameters**

- `struct pci_dev *pdev`: PCI device whose resources are to be reserved
- `const char *name`: name of the driver requesting the resources

**Description**

Mark all PCI regions associated with PCI device **pdev** as being reserved by
owner **name**. Do not access any address inside the PCI regions unless this
call returns successfully.

Returns 0 on success, or `EBUSY` on error.  A warning
message is also printed on failure.

#### <a id="c-pci_request_regions_exclusive"></a>`pci_request_regions_exclusive()`

```c
int pci_request_regions_exclusive(struct pci_dev *pdev, const char *name)
```

Reserve PCI I/O and memory resources

**Parameters**

- `struct pci_dev *pdev`: PCI device whose resources are to be reserved
- `const char *name`: name of the driver requesting the resources

**Return**

0 on success, negative error code on failure.

**Description**

Mark all PCI regions associated with PCI device **pdev** as being reserved
by owner **name**. Do not access any address inside the PCI regions
unless this call returns successfully.

[`pci_request_regions_exclusive()`](#c-pci_request_regions_exclusive) will mark the region so that /dev/mem
and the sysfs MMIO access will not be allowed.

Returns 0 on success, or `EBUSY` on error.  A warning message is also
printed on failure.

#### <a id="c-pci_remap_iospace"></a>`pci_remap_iospace()`

```c
int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
```

Remap the memory mapped I/O space

**Parameters**

- `const struct resource *res`: Resource describing the I/O space
- `phys_addr_t phys_addr`: physical address of range to be mapped

**Description**

Remap the memory mapped I/O space described by the **res** and the CPU
physical address **phys_addr** into virtual address space.  Only
architectures that have memory mapped IO functions defined (and the
PCI_IOBASE value defined) should call this function.

#### <a id="c-pci_unmap_iospace"></a>`pci_unmap_iospace()`

```c
void pci_unmap_iospace(struct resource *res)
```

Unmap the memory mapped I/O space

**Parameters**

- `struct resource *res`: resource to be unmapped

**Description**

Unmap the CPU virtual address **res** from virtual address space.  Only
architectures that have memory mapped IO functions defined (and the
PCI_IOBASE value defined) should call this function.

#### <a id="c-pci_set_master"></a>`pci_set_master()`

```c
void pci_set_master(struct pci_dev *dev)
```

enables bus-mastering for device dev

**Parameters**

- `struct pci_dev *dev`: the PCI device to enable

**Description**

Enables bus-mastering on the device and calls `pcibios_set_master()`
to do the needed arch specific settings.

#### <a id="c-pci_clear_master"></a>`pci_clear_master()`

```c
void pci_clear_master(struct pci_dev *dev)
```

disables bus-mastering for device dev

**Parameters**

- `struct pci_dev *dev`: the PCI device to disable

#### <a id="c-pci_set_cacheline_size"></a>`pci_set_cacheline_size()`

```c
int pci_set_cacheline_size(struct pci_dev *dev)
```

ensure the CACHE_LINE_SIZE register is programmed

**Parameters**

- `struct pci_dev *dev`: the PCI device for which MWI is to be enabled

**Description**

Helper function for pci_set_mwi.
Originally copied from drivers/net/acenic.c.
Copyright 1998-2001 by Jes Sorensen, \<jes@trained-monkey.org>.

**Return**

An appropriate -ERRNO error value on error, or zero for success.

#### <a id="c-pci_set_mwi"></a>`pci_set_mwi()`

```c
int pci_set_mwi(struct pci_dev *dev)
```

enables memory-write-invalidate PCI transaction

**Parameters**

- `struct pci_dev *dev`: the PCI device for which MWI is enabled

**Description**

Enables the Memory-Write-Invalidate transaction in `PCI_COMMAND`.

**Return**

An appropriate -ERRNO error value on error, or zero for success.

#### <a id="c-pci_try_set_mwi"></a>`pci_try_set_mwi()`

```c
int pci_try_set_mwi(struct pci_dev *dev)
```

enables memory-write-invalidate PCI transaction

**Parameters**

- `struct pci_dev *dev`: the PCI device for which MWI is enabled

**Description**

Enables the Memory-Write-Invalidate transaction in `PCI_COMMAND`.
Callers are not required to check the return value.

**Return**

An appropriate -ERRNO error value on error, or zero for success.

#### <a id="c-pci_clear_mwi"></a>`pci_clear_mwi()`

```c
void pci_clear_mwi(struct pci_dev *dev)
```

disables Memory-Write-Invalidate for device dev

**Parameters**

- `struct pci_dev *dev`: the PCI device to disable

**Description**

Disables PCI Memory-Write-Invalidate transaction on the device

#### <a id="c-pci_intx"></a>`pci_intx()`

```c
void pci_intx(struct pci_dev *pdev, int enable)
```

enables/disables PCI INTx for device dev

**Parameters**

- `struct pci_dev *pdev`: the PCI device to operate on
- `int enable`: boolean: whether to enable or disable PCI INTx

**Description**

Enables/disables PCI INTx for device **pdev**

#### <a id="c-pci_wait_for_pending_transaction"></a>`pci_wait_for_pending_transaction()`

```c
int pci_wait_for_pending_transaction(struct pci_dev *dev)
```

wait for pending transaction

**Parameters**

- `struct pci_dev *dev`: the PCI device to operate on

**Description**

Return 0 if transaction is pending 1 otherwise.

#### <a id="c-pcie_flr"></a>`pcie_flr()`

```c
int pcie_flr(struct pci_dev *dev)
```

initiate a PCIe function level reset

**Parameters**

- `struct pci_dev *dev`: device to reset

**Description**

Initiate a function level reset unconditionally on **dev** without
checking any flags and DEVCAP

#### <a id="c-pcie_reset_flr"></a>`pcie_reset_flr()`

```c
int pcie_reset_flr(struct pci_dev *dev, bool probe)
```

initiate a PCIe function level reset

**Parameters**

- `struct pci_dev *dev`: device to reset
- `bool probe`: if true, return 0 if device can be reset this way

**Description**

Initiate a function level reset on **dev**.

#### <a id="c-pci_bridge_secondary_bus_reset"></a>`pci_bridge_secondary_bus_reset()`

```c
int pci_bridge_secondary_bus_reset(struct pci_dev *dev)
```

Reset the secondary bus on a PCI bridge.

**Parameters**

- `struct pci_dev *dev`: Bridge device

**Description**

Use the bridge control register to assert reset on the secondary bus.
Devices on the secondary bus are left in power-on state.

#### <a id="c-__pci_reset_function_locked"></a>`__pci_reset_function_locked()`

```c
int __pci_reset_function_locked(struct pci_dev *dev)
```

reset a PCI device function while holding the **dev** mutex lock.

**Parameters**

- `struct pci_dev *dev`: PCI device to reset

**Description**

Some devices allow an individual function to be reset without affecting
other functions in the same device.  The PCI device must be responsive
to PCI config space in order to use this function.

The device function is presumed to be unused and the caller is holding
the device mutex lock when this function is called.

Resetting the device will make the contents of PCI configuration space
random, so any caller of this must be prepared to reinitialise the
device including MSI, bus mastering, BARs, decoding IO and memory spaces,
etc.

**Context**

The caller must hold the device lock.

**Return**

0 if the device function was successfully reset or negative if the
device doesn't support resetting a single function.

#### <a id="c-pci_reset_function"></a>`pci_reset_function()`

```c
int pci_reset_function(struct pci_dev *dev)
```

quiesce and reset a PCI device function

**Parameters**

- `struct pci_dev *dev`: PCI device to reset

**Description**

Some devices allow an individual function to be reset without affecting
other functions in the same device.  The PCI device must be responsive
to PCI config space in order to use this function.

This function does not just reset the PCI portion of a device, but
clears all the state associated with the device.  This function differs
from [`__pci_reset_function_locked()`](#c-__pci_reset_function_locked) in that it saves and restores device state
over the reset and takes the PCI device lock.

Returns 0 if the device function was successfully reset or negative if the
device doesn't support resetting a single function.

#### <a id="c-pci_reset_function_locked"></a>`pci_reset_function_locked()`

```c
int pci_reset_function_locked(struct pci_dev *dev)
```

quiesce and reset a PCI device function

**Parameters**

- `struct pci_dev *dev`: PCI device to reset

**Description**

Some devices allow an individual function to be reset without affecting
other functions in the same device.  The PCI device must be responsive
to PCI config space in order to use this function.

This function does not just reset the PCI portion of a device, but
clears all the state associated with the device.  This function differs
from [`__pci_reset_function_locked()`](#c-__pci_reset_function_locked) in that it saves and restores device state
over the reset.  It also differs from [`pci_reset_function()`](#c-pci_reset_function) in that it
requires the PCI device lock to be held.

**Context**

The caller must hold the device lock.

**Return**

0 if the device function was successfully reset or negative if the
device doesn't support resetting a single function.

#### <a id="c-pci_try_reset_function"></a>`pci_try_reset_function()`

```c
int pci_try_reset_function(struct pci_dev *dev)
```

quiesce and reset a PCI device function

**Parameters**

- `struct pci_dev *dev`: PCI device to reset

**Description**

Same as above, except return -EAGAIN if unable to lock device.

#### <a id="c-pci_probe_reset_slot"></a>`pci_probe_reset_slot()`

```c
int pci_probe_reset_slot(struct pci_slot *slot)
```

probe whether a PCI slot can be reset

**Parameters**

- `struct pci_slot *slot`: PCI slot to probe

**Description**

Return 0 if slot can be reset, negative if a slot reset is not supported.

#### <a id="c-pci_probe_reset_bus"></a>`pci_probe_reset_bus()`

```c
int pci_probe_reset_bus(struct pci_bus *bus)
```

probe whether a PCI bus can be reset

**Parameters**

- `struct pci_bus *bus`: PCI bus to probe

**Description**

Return 0 if bus can be reset, negative if a bus reset is not supported.

#### <a id="c-pci_reset_bus"></a>`pci_reset_bus()`

```c
int pci_reset_bus(struct pci_dev *pdev)
```

Try to reset a PCI bus

**Parameters**

- `struct pci_dev *pdev`: top level PCI device to reset via slot/bus

**Description**

Same as above except return -EAGAIN if the bus cannot be locked

#### <a id="c-pcix_get_max_mmrbc"></a>`pcix_get_max_mmrbc()`

```c
int pcix_get_max_mmrbc(struct pci_dev *dev)
```

get PCI-X maximum designed memory read byte count

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Returns mmrbc: maximum designed memory read count in bytes or
appropriate error value.

#### <a id="c-pcix_get_mmrbc"></a>`pcix_get_mmrbc()`

```c
int pcix_get_mmrbc(struct pci_dev *dev)
```

get PCI-X maximum memory read byte count

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Returns mmrbc: maximum memory read count in bytes or appropriate error
value.

#### <a id="c-pcix_set_mmrbc"></a>`pcix_set_mmrbc()`

```c
int pcix_set_mmrbc(struct pci_dev *dev, int mmrbc)
```

set PCI-X maximum memory read byte count

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `int mmrbc`: maximum memory read count in bytes
  valid values are 512, 1024, 2048, 4096

**Description**

If possible sets maximum memory read byte count, some bridges have errata
that prevent this.

#### <a id="c-pcie_get_readrq"></a>`pcie_get_readrq()`

```c
int pcie_get_readrq(struct pci_dev *dev)
```

get PCI Express read request size

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Returns maximum memory read request in bytes or appropriate error value.

#### <a id="c-pcie_set_readrq"></a>`pcie_set_readrq()`

```c
int pcie_set_readrq(struct pci_dev *dev, int rq)
```

set PCI Express maximum memory read request

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `int rq`: maximum memory read count in bytes
  valid values are 128, 256, 512, 1024, 2048, 4096

**Description**

If possible sets maximum memory read request in bytes

#### <a id="c-pcie_get_mps"></a>`pcie_get_mps()`

```c
int pcie_get_mps(struct pci_dev *dev)
```

get PCI Express maximum payload size

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Returns maximum payload size in bytes

#### <a id="c-pcie_set_mps"></a>`pcie_set_mps()`

```c
int pcie_set_mps(struct pci_dev *dev, int mps)
```

set PCI Express maximum payload size

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `int mps`: maximum payload size in bytes
  valid values are 128, 256, 512, 1024, 2048, 4096

**Description**

If possible sets maximum payload size

#### <a id="c-pcie_bandwidth_available"></a>`pcie_bandwidth_available()`

```c
u32 pcie_bandwidth_available(struct pci_dev *dev, struct pci_dev **limiting_dev, enum pci_bus_speed *speed, enum pcie_link_width *width)
```

determine minimum link settings of a PCIe device and its bandwidth limitation

**Parameters**

- `struct pci_dev *dev`: PCI device to query
- `struct pci_dev **limiting_dev`: storage for device causing the bandwidth limitation
- `enum pci_bus_speed *speed`: storage for speed of limiting device
- `enum pcie_link_width *width`: storage for width of limiting device

**Description**

Walk up the PCI device chain and find the point where the minimum
bandwidth is available.  Return the bandwidth available there and (if
limiting_dev, speed, and width pointers are supplied) information about
that point.  The bandwidth returned is in Mb/s, i.e., megabits/second of
raw bandwidth.

#### <a id="c-pcie_get_speed_cap"></a>`pcie_get_speed_cap()`

```c
enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *dev)
```

query for the PCI device's link speed capability

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Query the PCI device speed capability.

**Return**

the maximum link speed supported by the device.

#### <a id="c-pcie_get_width_cap"></a>`pcie_get_width_cap()`

```c
enum pcie_link_width pcie_get_width_cap(struct pci_dev *dev)
```

query for the PCI device's link width capability

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Query the PCI device width capability.  Return the maximum link width
supported by the device.

#### <a id="c-pcie_print_link_status"></a>`pcie_print_link_status()`

```c
void pcie_print_link_status(struct pci_dev *dev)
```

Report the PCI device's link speed and width

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Report the available bandwidth at the device.

#### <a id="c-pci_select_bars"></a>`pci_select_bars()`

```c
int pci_select_bars(struct pci_dev *dev, unsigned long flags)
```

Make BAR mask from the type of resource

**Parameters**

- `struct pci_dev *dev`: the PCI device for which BAR mask is made
- `unsigned long flags`: resource type mask to be selected

**Description**

This helper routine makes bar mask from the type of resource.

#### <a id="c-pci_bus_find_emul_domain_nr"></a>`pci_bus_find_emul_domain_nr()`

```c
int pci_bus_find_emul_domain_nr(u32 hint, u32 min, u32 max)
```

allocate a PCI domain number per constraints

**Parameters**

- `u32 hint`: desired domain, 0 if any ID in the range of **min** to **max** is acceptable
- `u32 min`: minimum allowable domain
- `u32 max`: maximum allowable domain, no IDs higher than INT_MAX will be returned



#### <a id="c-pci_iomap_range"></a>`pci_iomap_range()`

```c
void __iomem * pci_iomap_range(struct pci_dev *dev, int bar, unsigned long offset, unsigned long maxlen)
```

create a virtual mapping cookie for a PCI BAR

**Parameters**

- `struct pci_dev *dev`: PCI device that owns the BAR
- `int bar`: BAR number
- `unsigned long offset`: map memory at the given offset in BAR
- `unsigned long maxlen`: max length of the memory to map

**Description**

Using this function you will get a __iomem address to your device BAR.
You can access it using ioread*() and iowrite*(). These functions hide
the details if this is a MMIO or PIO address space and will just do what
you expect from them in the correct way.

**maxlen** specifies the maximum length to map. If you want to get access to
the complete BAR from offset to the end, pass `0` here.

#### <a id="c-pci_iomap_wc_range"></a>`pci_iomap_wc_range()`

```c
void __iomem * pci_iomap_wc_range(struct pci_dev *dev, int bar, unsigned long offset, unsigned long maxlen)
```

create a virtual WC mapping cookie for a PCI BAR

**Parameters**

- `struct pci_dev *dev`: PCI device that owns the BAR
- `int bar`: BAR number
- `unsigned long offset`: map memory at the given offset in BAR
- `unsigned long maxlen`: max length of the memory to map

**Description**

Using this function you will get a __iomem address to your device BAR.
You can access it using ioread*() and iowrite*(). These functions hide
the details if this is a MMIO or PIO address space and will just do what
you expect from them in the correct way. When possible write combining
is used.

**maxlen** specifies the maximum length to map. If you want to get access to
the complete BAR from offset to the end, pass `0` here.

#### <a id="c-pci_iomap"></a>`pci_iomap()`

```c
void __iomem * pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
```

create a virtual mapping cookie for a PCI BAR

**Parameters**

- `struct pci_dev *dev`: PCI device that owns the BAR
- `int bar`: BAR number
- `unsigned long maxlen`: length of the memory to map

**Description**

Using this function you will get a __iomem address to your device BAR.
You can access it using ioread*() and iowrite*(). These functions hide
the details if this is a MMIO or PIO address space and will just do what
you expect from them in the correct way.

**maxlen** specifies the maximum length to map. If you want to get access to
the complete BAR without checking for its length first, pass `0` here.

#### <a id="c-pci_iomap_wc"></a>`pci_iomap_wc()`

```c
void __iomem * pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
```

create a virtual WC mapping cookie for a PCI BAR

**Parameters**

- `struct pci_dev *dev`: PCI device that owns the BAR
- `int bar`: BAR number
- `unsigned long maxlen`: length of the memory to map

**Description**

Using this function you will get a __iomem address to your device BAR.
You can access it using ioread*() and iowrite*(). These functions hide
the details if this is a MMIO or PIO address space and will just do what
you expect from them in the correct way. When possible write combining
is used.

**maxlen** specifies the maximum length to map. If you want to get access to
the complete BAR without checking for its length first, pass `0` here.



#### <a id="c-devm_pci_remap_iospace"></a>`devm_pci_remap_iospace()`

```c
int devm_pci_remap_iospace(struct device *dev, const struct resource *res, phys_addr_t phys_addr)
```

Managed [`pci_remap_iospace()`](#c-pci_remap_iospace)

**Parameters**

- `struct device *dev`: Generic device to remap IO address for
- `const struct resource *res`: Resource describing the I/O space
- `phys_addr_t phys_addr`: physical address of range to be mapped

**Description**

Managed [`pci_remap_iospace()`](#c-pci_remap_iospace).  Map is automatically unmapped on driver
detach.

#### <a id="c-devm_pci_remap_cfgspace"></a>`devm_pci_remap_cfgspace()`

```c
void __iomem * devm_pci_remap_cfgspace(struct device *dev, resource_size_t offset, resource_size_t size)
```

Managed `pci_remap_cfgspace()`

**Parameters**

- `struct device *dev`: Generic device to remap IO address for
- `resource_size_t offset`: Resource address to map
- `resource_size_t size`: Size of map

**Description**

Managed `pci_remap_cfgspace()`.  Map is automatically unmapped on driver
detach.

#### <a id="c-devm_pci_remap_cfg_resource"></a>`devm_pci_remap_cfg_resource()`

```c
void __iomem * devm_pci_remap_cfg_resource(struct device *dev, struct resource *res)
```

check, request region and ioremap cfg resource

**Parameters**

- `struct device *dev`: generic device to handle the resource for
- `struct resource *res`: configuration space resource to be handled

**Description**

Checks that a resource is a valid memory region, requests the memory
region and ioremaps with `pci_remap_cfgspace()` API that ensures the
proper PCI configuration space memory attributes are guaranteed.

All operations are managed and will be undone on driver detach.

Returns a pointer to the remapped memory or an `IOMEM_ERR_PTR()` encoded error
code on failure. Usage example:

```text
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
base = devm_pci_remap_cfg_resource(&pdev->dev, res);
if (IS_ERR(base))
        return PTR_ERR(base);
```

#### <a id="c-pcim_set_mwi"></a>`pcim_set_mwi()`

```c
int pcim_set_mwi(struct pci_dev *pdev)
```

a device-managed [`pci_set_mwi()`](#c-pci_set_mwi)

**Parameters**

- `struct pci_dev *pdev`: the PCI device for which MWI is enabled

**Description**

Managed [`pci_set_mwi()`](#c-pci_set_mwi).

**Return**

An appropriate -ERRNO error value on error, or zero for success.

#### <a id="c-pcim_intx"></a>`pcim_intx()`

```c
int pcim_intx(struct pci_dev *pdev, int enable)
```

managed [`pci_intx()`](#c-pci_intx)

**Parameters**

- `struct pci_dev *pdev`: the PCI device to operate on
- `int enable`: boolean: whether to enable or disable PCI INTx

**Return**

0 on success, -ENOMEM on error.

**Description**

Enable/disable PCI INTx for device **pdev**.
Restore the original state on driver detach.

#### <a id="c-pcim_enable_device"></a>`pcim_enable_device()`

```c
int pcim_enable_device(struct pci_dev *pdev)
```

Managed [`pci_enable_device()`](#c-pci_enable_device)

**Parameters**

- `struct pci_dev *pdev`: PCI device to be initialized

**Return**

0 on success, negative error code on failure.

**Description**

Managed [`pci_enable_device()`](#c-pci_enable_device). Device will automatically be disabled on
driver detach.

#### <a id="c-pcim_pin_device"></a>`pcim_pin_device()`

```c
void pcim_pin_device(struct pci_dev *pdev)
```

Pin managed PCI device

**Parameters**

- `struct pci_dev *pdev`: PCI device to pin

**Description**

Pin managed PCI device **pdev**. Pinned device won't be disabled on driver
detach. **pdev** must have been enabled with [`pcim_enable_device()`](#c-pcim_enable_device).

#### <a id="c-pcim_iomap_table"></a>`pcim_iomap_table()`

```c
void __iomem * const * pcim_iomap_table(struct pci_dev *pdev)
```

access iomap allocation table (DEPRECATED)

**Parameters**

- `struct pci_dev *pdev`: PCI device to access iomap table for

**Return**

Const pointer to array of __iomem pointers on success, NULL on failure.

**Description**

Access iomap allocation table for **dev**.  If iomap table doesn't
exist and **pdev** is managed, it will be allocated.  All iomaps
recorded in the iomap table are automatically unmapped on driver
detach.

This function might sleep when the table is first allocated but can
be safely called without context and guaranteed to succeed once
allocated.

This function is DEPRECATED. Do not use it in new code. Instead, obtain a
mapping's address directly from one of the pcim_* mapping functions. For

**example**

void __iomem \*mappy = pcim_iomap(pdev, bar, length);

#### <a id="c-pcim_iomap"></a>`pcim_iomap()`

```c
void __iomem * pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
```

Managed [`pcim_iomap()`](#c-pcim_iomap)

**Parameters**

- `struct pci_dev *pdev`: PCI device to iomap for
- `int bar`: BAR to iomap
- `unsigned long maxlen`: Maximum length of iomap

**Return**

__iomem pointer on success, NULL on failure.

**Description**

Managed [`pci_iomap()`](#c-pci_iomap). Map is automatically unmapped on driver detach. If
desired, unmap manually only with [`pcim_iounmap()`](#c-pcim_iounmap).

This SHOULD only be used once per BAR.

**NOTE**

Contrary to the other pcim_* functions, this function does not return an
`IOMEM_ERR_PTR()` on failure, but a simple NULL. This is done for backwards
compatibility.

#### <a id="c-pcim_iounmap"></a>`pcim_iounmap()`

```c
void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
```

Managed `pci_iounmap()`

**Parameters**

- `struct pci_dev *pdev`: PCI device to iounmap for
- `void __iomem *addr`: Address to unmap

**Description**

Managed `pci_iounmap()`. **addr** must have been mapped using a pcim_* mapping
function.

#### <a id="c-pcim_iomap_region"></a>`pcim_iomap_region()`

```c
void __iomem * pcim_iomap_region(struct pci_dev *pdev, int bar, const char *name)
```

Request and iomap a PCI BAR

**Parameters**

- `struct pci_dev *pdev`: PCI device to map IO resources for
- `int bar`: Index of a BAR to map
- `const char *name`: Name of the driver requesting the resource

**Return**

__iomem pointer on success, an IOMEM_ERR_PTR on failure.

**Description**

Mapping and region will get automatically released on driver detach. If
desired, release manually only with [`pcim_iounmap_region()`](#c-pcim_iounmap_region).

#### <a id="c-pcim_iounmap_region"></a>`pcim_iounmap_region()`

```c
void pcim_iounmap_region(struct pci_dev *pdev, int bar)
```

Unmap and release a PCI BAR

**Parameters**

- `struct pci_dev *pdev`: PCI device to operate on
- `int bar`: Index of BAR to unmap and release

**Description**

Unmap a BAR and release its region manually. Only pass BARs that were
previously mapped by [`pcim_iomap_region()`](#c-pcim_iomap_region).

#### <a id="c-pcim_iomap_regions"></a>`pcim_iomap_regions()`

```c
int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
```

Request and iomap PCI BARs (DEPRECATED)

**Parameters**

- `struct pci_dev *pdev`: PCI device to map IO resources for
- `int mask`: Mask of BARs to request and iomap
- `const char *name`: Name of the driver requesting the resources

**Return**

0 on success, negative error code on failure.

**Description**

Request and iomap regions specified by **mask**.

This function is DEPRECATED. Do not use it in new code.
Use [`pcim_iomap_region()`](#c-pcim_iomap_region) instead.

#### <a id="c-pcim_request_region"></a>`pcim_request_region()`

```c
int pcim_request_region(struct pci_dev *pdev, int bar, const char *name)
```

Request a PCI BAR

**Parameters**

- `struct pci_dev *pdev`: PCI device to request region for
- `int bar`: Index of BAR to request
- `const char *name`: Name of the driver requesting the resource

**Return**

0 on success, a negative error code on failure.

**Description**

Request region specified by **bar**.

The region will automatically be released on driver detach. If desired,
release manually only with `pcim_release_region()`.

#### <a id="c-pcim_request_all_regions"></a>`pcim_request_all_regions()`

```c
int pcim_request_all_regions(struct pci_dev *pdev, const char *name)
```

Request all regions

**Parameters**

- `struct pci_dev *pdev`: PCI device to map IO resources for
- `const char *name`: name of the driver requesting the resources

**Return**

0 on success, negative error code on failure.

**Description**

Requested regions will automatically be released at driver detach. If
desired, release individual regions with `pcim_release_region()` or all of
them at once with `pcim_release_all_regions()`.

#### <a id="c-pcim_iomap_range"></a>`pcim_iomap_range()`

```c
void __iomem * pcim_iomap_range(struct pci_dev *pdev, int bar, unsigned long offset, unsigned long len)
```

Create a ranged __iomap mapping within a PCI BAR

**Parameters**

- `struct pci_dev *pdev`: PCI device to map IO resources for
- `int bar`: Index of the BAR
- `unsigned long offset`: Offset from the begin of the BAR
- `unsigned long len`: Length in bytes for the mapping

**Return**

__iomem pointer on success, an IOMEM_ERR_PTR on failure.

**Description**

Creates a new IO-Mapping within the specified **bar**, ranging from **offset** to
**offset** + **len**.

The mapping will automatically get unmapped on driver detach. If desired,
release manually only with [`pcim_iounmap()`](#c-pcim_iounmap).



#### <a id="c-pci_add_dynid"></a>`pci_add_dynid()`

```c
int pci_add_dynid(struct pci_driver *drv, unsigned int vendor, unsigned int device, unsigned int subvendor, unsigned int subdevice, unsigned int class, unsigned int class_mask, unsigned long driver_data)
```

add a new PCI device ID to this driver and re-probe devices

**Parameters**

- `struct pci_driver *drv`: target pci driver
- `unsigned int vendor`: PCI vendor ID
- `unsigned int device`: PCI device ID
- `unsigned int subvendor`: PCI subvendor ID
- `unsigned int subdevice`: PCI subdevice ID
- `unsigned int class`: PCI class
- `unsigned int class_mask`: PCI class mask
- `unsigned long driver_data`: private driver data

**Description**

Adds a new dynamic pci device ID to this driver and causes the
driver to probe for all devices again.  **drv** must have been
registered prior to calling this function.

**Context**

Does GFP_KERNEL allocation.

**Return**

0 on success, -errno on failure.

#### <a id="c-pci_match_id"></a>`pci_match_id()`

```c
const struct pci_device_id * pci_match_id(const struct pci_device_id *ids, struct pci_dev *dev)
```

See if a PCI device matches a given pci_id table

**Parameters**

- `const struct pci_device_id *ids`: array of PCI device ID structures to search in
- `struct pci_dev *dev`: the PCI device structure to match against.

**Description**

Used by a driver to check whether a PCI device is in its list of
supported devices.  Returns the matching pci_device_id structure or
`NULL` if there is no match.

Deprecated; don't use this as it will not catch any dynamic IDs
that a driver might want to check for.

#### <a id="c-__pci_register_driver"></a>`__pci_register_driver()`

```c
int __pci_register_driver(struct pci_driver *drv, struct module *owner, const char *mod_name)
```

register a new pci driver

**Parameters**

- `struct pci_driver *drv`: the driver structure to register
- `struct module *owner`: owner module of drv
- `const char *mod_name`: module name string

**Description**

Adds the driver structure to the list of registered drivers.
Returns a negative value on error, otherwise 0.
If no error occurred, the driver remains registered even if
no device was claimed during registration.

#### <a id="c-pci_unregister_driver"></a>`pci_unregister_driver()`

```c
void pci_unregister_driver(struct pci_driver *drv)
```

unregister a pci driver

**Parameters**

- `struct pci_driver *drv`: the driver structure to unregister

**Description**

Deletes the driver structure from the list of registered PCI drivers,
gives it a chance to clean up by calling its `remove()` function for
each device it was responsible for, and marks those devices as
driverless.

#### <a id="c-pci_dev_driver"></a>`pci_dev_driver()`

```c
struct pci_driver * pci_dev_driver(const struct pci_dev *dev)
```

get the pci_driver of a device

**Parameters**

- `const struct pci_dev *dev`: the device to query

**Description**

Returns the appropriate pci_driver structure or `NULL` if there is no
registered driver for the device.

#### <a id="c-pci_dev_get"></a>`pci_dev_get()`

```c
struct pci_dev * pci_dev_get(struct pci_dev *dev)
```

increments the reference count of the pci device structure

**Parameters**

- `struct pci_dev *dev`: the device being referenced

**Description**

Each live reference to a device should be refcounted.

Drivers for PCI devices should normally record such references in
their `probe()` methods, when they bind to a device, and release
them by calling [`pci_dev_put()`](#c-pci_dev_put), in their `disconnect()` methods.

A pointer to the device with the incremented reference counter is returned.

#### <a id="c-pci_dev_put"></a>`pci_dev_put()`

```c
void pci_dev_put(struct pci_dev *dev)
```

release a use of the pci device structure

**Parameters**

- `struct pci_dev *dev`: device that's been disconnected

**Description**

Must be called when a user of a device is finished with it.  When the last
user of the device calls this function, the memory of the device is freed.



#### <a id="c-pci_stop_and_remove_bus_device"></a>`pci_stop_and_remove_bus_device()`

```c
void pci_stop_and_remove_bus_device(struct pci_dev *dev)
```

remove a PCI device and any children

**Parameters**

- `struct pci_dev *dev`: the device to remove

**Description**

Remove a PCI device from the device lists, informing the drivers
that the device has been removed.  We also remove any subordinate
buses and children in a depth-first manner.

For each device we remove, delete the device structure from the
device lists, remove the /proc entry, and notify userspace
(/sbin/hotplug).



#### <a id="c-pci_find_bus"></a>`pci_find_bus()`

```c
struct pci_bus * pci_find_bus(int domain, int busnr)
```

locate PCI bus from a given domain and bus number

**Parameters**

- `int domain`: number of PCI domain to search
- `int busnr`: number of desired PCI bus

**Description**

Given a PCI bus number and domain number, the desired PCI bus is located
in the global list of PCI buses.  If the bus is found, a pointer to its
data structure is returned.  If no bus is found, `NULL` is returned.

#### <a id="c-pci_find_next_bus"></a>`pci_find_next_bus()`

```c
struct pci_bus * pci_find_next_bus(const struct pci_bus *from)
```

begin or continue searching for a PCI bus

**Parameters**

- `const struct pci_bus *from`: Previous PCI bus found, or `NULL` for new search.

**Description**

Iterates through the list of known PCI buses.  A new search is
initiated by passing `NULL` as the **from** argument.  Otherwise if
**from** is not `NULL`, searches continue from next device on the
global list.

#### <a id="c-pci_get_slot"></a>`pci_get_slot()`

```c
struct pci_dev * pci_get_slot(struct pci_bus *bus, unsigned int devfn)
```

locate PCI device for a given PCI slot

**Parameters**

- `struct pci_bus *bus`: PCI bus on which desired PCI device resides
- `unsigned int devfn`: encodes number of PCI slot in which the desired PCI
  device resides and the logical device number within that slot
  in case of multi-function devices.

**Description**

Given a PCI bus and slot/function number, the desired PCI device
is located in the list of PCI devices.
If the device is found, its reference count is increased and this
function returns a pointer to its data structure.  The caller must
decrement the reference count by calling [`pci_dev_put()`](#c-pci_dev_put).
If no device is found, `NULL` is returned.

#### <a id="c-pci_get_domain_bus_and_slot"></a>`pci_get_domain_bus_and_slot()`

```c
struct pci_dev * pci_get_domain_bus_and_slot(int domain, unsigned int bus, unsigned int devfn)
```

locate PCI device for a given PCI domain (segment), bus, and slot

**Parameters**

- `int domain`: PCI domain/segment on which the PCI device resides.
- `unsigned int bus`: PCI bus on which desired PCI device resides
- `unsigned int devfn`: encodes number of PCI slot in which the desired PCI device
  resides and the logical device number within that slot in case of
  multi-function devices.

**Description**

Given a PCI domain, bus, and slot/function number, the desired PCI
device is located in the list of PCI devices. If the device is
found, its reference count is increased and this function returns a
pointer to its data structure.  The caller must decrement the
reference count by calling [`pci_dev_put()`](#c-pci_dev_put).  If no device is found,
`NULL` is returned.

#### <a id="c-pci_get_subsys"></a>`pci_get_subsys()`

```c
struct pci_dev * pci_get_subsys(unsigned int vendor, unsigned int device, unsigned int ss_vendor, unsigned int ss_device, struct pci_dev *from)
```

begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id

**Parameters**

- `unsigned int vendor`: PCI vendor id to match, or `PCI_ANY_ID` to match all vendor ids
- `unsigned int device`: PCI device id to match, or `PCI_ANY_ID` to match all device ids
- `unsigned int ss_vendor`: PCI subsystem vendor id to match, or `PCI_ANY_ID` to match all vendor ids
- `unsigned int ss_device`: PCI subsystem device id to match, or `PCI_ANY_ID` to match all device ids
- `struct pci_dev *from`: Previous PCI device found in search, or `NULL` for new search.

**Description**

Iterates through the list of known PCI devices.  If a PCI device is found
with a matching **vendor**, **device**, **ss_vendor** and **ss_device**, a pointer to its
device structure is returned, and the reference count to the device is
incremented.  Otherwise, `NULL` is returned.  A new search is initiated by
passing `NULL` as the **from** argument.  Otherwise if **from** is not `NULL`,
searches continue from next device on the global list.
The reference count for **from** is always decremented if it is not `NULL`.

#### <a id="c-pci_get_device"></a>`pci_get_device()`

```c
struct pci_dev * pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
```

begin or continue searching for a PCI device by vendor/device id

**Parameters**

- `unsigned int vendor`: PCI vendor id to match, or `PCI_ANY_ID` to match all vendor ids
- `unsigned int device`: PCI device id to match, or `PCI_ANY_ID` to match all device ids
- `struct pci_dev *from`: Previous PCI device found in search, or `NULL` for new search.

**Description**

Iterates through the list of known PCI devices.  If a PCI device is
found with a matching **vendor** and **device**, the reference count to the
device is incremented and a pointer to its device structure is returned.
Otherwise, `NULL` is returned.  A new search is initiated by passing `NULL`
as the **from** argument.  Otherwise if **from** is not `NULL`, searches continue
from next device on the global list.  The reference count for **from** is
always decremented if it is not `NULL`.

#### <a id="c-pci_get_class"></a>`pci_get_class()`

```c
struct pci_dev * pci_get_class(unsigned int class, struct pci_dev *from)
```

begin or continue searching for a PCI device by class

**Parameters**

- `unsigned int class`: search for a PCI device with this class designation
- `struct pci_dev *from`: Previous PCI device found in search, or `NULL` for new search.

**Description**

Iterates through the list of known PCI devices.  If a PCI device is
found with a matching **class**, the reference count to the device is
incremented and a pointer to its device structure is returned.
Otherwise, `NULL` is returned.
A new search is initiated by passing `NULL` as the **from** argument.
Otherwise if **from** is not `NULL`, searches continue from next device
on the global list.  The reference count for **from** is always decremented
if it is not `NULL`.

#### <a id="c-pci_get_base_class"></a>`pci_get_base_class()`

```c
struct pci_dev * pci_get_base_class(unsigned int class, struct pci_dev *from)
```

searching for a PCI device by matching against the base class code only

**Parameters**

- `unsigned int class`: search for a PCI device with this base class code
- `struct pci_dev *from`: Previous PCI device found in search, or `NULL` for new search.

**Description**

Iterates through the list of known PCI devices. If a PCI device is found
with a matching base class code, the reference count to the device is
incremented. See `pci_match_one_device()` to figure out how does this works.
A new search is initiated by passing `NULL` as the **from** argument.
Otherwise if **from** is not `NULL`, searches continue from next device on the
global list. The reference count for **from** is always decremented if it is
not `NULL`.

**Return**

A pointer to a matched PCI device, `NULL` Otherwise.

#### <a id="c-pci_dev_present"></a>`pci_dev_present()`

```c
int pci_dev_present(const struct pci_device_id *ids)
```

Returns 1 if device matching the device list is present, 0 if not.

**Parameters**

- `const struct pci_device_id *ids`: A pointer to a null terminated list of struct pci_device_id structures
  that describe the type of PCI device the caller is trying to find.

**Description**

Obvious fact: You do not have a reference to any device that might be found
by this function, so if that device is removed from the system right after
this function is finished, the value will be stale.  Use this function to
find devices that are usually built into a system, or for a general hint as
to if another device happens to be present at this specific moment in time.



#### <a id="c-pci_msi_mask_irq"></a>`pci_msi_mask_irq()`

```c
void pci_msi_mask_irq(struct irq_data *data)
```

Generic IRQ chip callback to mask PCI/MSI interrupts

**Parameters**

- `struct irq_data *data`: pointer to irqdata associated to that interrupt

#### <a id="c-pci_msi_unmask_irq"></a>`pci_msi_unmask_irq()`

```c
void pci_msi_unmask_irq(struct irq_data *data)
```

Generic IRQ chip callback to unmask PCI/MSI interrupts

**Parameters**

- `struct irq_data *data`: pointer to irqdata associated to that interrupt

#### <a id="c-pci_msi_vec_count"></a>`pci_msi_vec_count()`

```c
int pci_msi_vec_count(struct pci_dev *dev)
```

Return the number of MSI vectors a device can send

**Parameters**

- `struct pci_dev *dev`: device to report about

**Description**

This function returns the number of MSI vectors a device requested via
Multiple Message Capable register. It returns a negative errno if the
device is not capable sending MSI interrupts. Otherwise, the call succeeds
and returns a power of two, up to a maximum of 2^5 (32), according to the
MSI specification.



#### <a id="c-pci_bus_alloc_resource"></a>`pci_bus_alloc_resource()`

```c
int pci_bus_alloc_resource(struct pci_bus *bus, struct resource *res, resource_size_t size, resource_size_t align, resource_size_t min, unsigned long type_mask, resource_alignf alignf, void *alignf_data)
```

allocate a resource from a parent bus

**Parameters**

- `struct pci_bus *bus`: PCI bus
- `struct resource *res`: resource to allocate
- `resource_size_t size`: size of resource to allocate
- `resource_size_t align`: alignment of resource to allocate
- `resource_size_t min`: minimum /proc/iomem address to allocate
- `unsigned long type_mask`: IORESOURCE_* type flags
- `resource_alignf alignf`: resource alignment function
- `void *alignf_data`: data argument for resource alignment function

**Description**

Given the PCI bus a device resides on, the size, minimum address,
alignment and type, try to find an acceptable resource allocation
for a specific device resource.

#### <a id="c-pci_bus_add_device"></a>`pci_bus_add_device()`

```c
void pci_bus_add_device(struct pci_dev *dev)
```

start driver for a single device

**Parameters**

- `struct pci_dev *dev`: device to add

**Description**

This adds add sysfs entries and start device drivers

#### <a id="c-pci_bus_add_devices"></a>`pci_bus_add_devices()`

```c
void pci_bus_add_devices(const struct pci_bus *bus)
```

start driver for PCI devices

**Parameters**

- `const struct pci_bus *bus`: bus to check for new devices

**Description**

Start driver for PCI devices and add some sysfs entries.

#### <a id="c-pci_walk_bus"></a>`pci_walk_bus()`

```c
void pci_walk_bus(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), void *userdata)
```

walk devices on/under bus, calling callback.

**Parameters**

- `struct pci_bus *top`: bus whose devices should be walked
- `int (*cb)(struct pci_dev *, void *)`: callback to be called for each device found
- `void *userdata`: arbitrary pointer to be passed to callback

**Description**

Walk the given bus, including any bridged devices
on buses under this bus.  Call the provided callback
on each device found.

We check the return of **cb** each time. If it returns anything
other than 0, we break out.

#### <a id="c-pci_walk_bus_reverse"></a>`pci_walk_bus_reverse()`

```c
void pci_walk_bus_reverse(struct pci_bus *top, int (*cb)(struct pci_dev *, void *), void *userdata)
```

walk devices on/under bus, calling callback.

**Parameters**

- `struct pci_bus *top`: bus whose devices should be walked
- `int (*cb)(struct pci_dev *, void *)`: callback to be called for each device found
- `void *userdata`: arbitrary pointer to be passed to callback

**Description**

Same semantics as [`pci_walk_bus()`](#c-pci_walk_bus), but walks the bus in reverse order.



#### <a id="c-pci_bus_set_ops"></a>`pci_bus_set_ops()`

```c
struct pci_ops * pci_bus_set_ops(struct pci_bus *bus, struct pci_ops *ops)
```

Set raw operations of pci bus

**Parameters**

- `struct pci_bus *bus`: pci bus struct
- `struct pci_ops *ops`: new raw operations

**Description**

Return previous raw operations

#### <a id="c-pci_cfg_access_lock"></a>`pci_cfg_access_lock()`

```c
void pci_cfg_access_lock(struct pci_dev *dev)
```

Lock PCI config reads/writes

**Parameters**

- `struct pci_dev *dev`: pci device struct

**Description**

When access is locked, any userspace reads or writes to config
space and concurrent lock requests will sleep until access is
allowed via [`pci_cfg_access_unlock()`](#c-pci_cfg_access_unlock) again.

#### <a id="c-pci_cfg_access_trylock"></a>`pci_cfg_access_trylock()`

```c
bool pci_cfg_access_trylock(struct pci_dev *dev)
```

try to lock PCI config reads/writes

**Parameters**

- `struct pci_dev *dev`: pci device struct

**Description**

Same as pci_cfg_access_lock, but will return 0 if access is
already locked, 1 otherwise. This function can be used from
atomic contexts.

#### <a id="c-pci_cfg_access_unlock"></a>`pci_cfg_access_unlock()`

```c
void pci_cfg_access_unlock(struct pci_dev *dev)
```

Unlock PCI config reads/writes

**Parameters**

- `struct pci_dev *dev`: pci device struct

**Description**

This function allows PCI config accesses to resume.



#### <a id="c-pci_request_irq"></a>`pci_request_irq()`

```c
int pci_request_irq(struct pci_dev *dev, unsigned int nr, irq_handler_t handler, irq_handler_t thread_fn, void *dev_id, const char *fmt, ...)
```

allocate an interrupt line for a PCI device

**Parameters**

- `struct pci_dev *dev`: PCI device to operate on
- `unsigned int nr`: device-relative interrupt vector index (0-based).
- `irq_handler_t handler`: Function to be called when the IRQ occurs.
  Primary handler for threaded interrupts.
  If NULL and thread_fn != NULL the default primary handler is
  installed.
- `irq_handler_t thread_fn`: Function called from the IRQ handler thread
  If NULL, no IRQ thread is created
- `void *dev_id`: Cookie passed back to the handler function
- `const char *fmt`: Printf-like format string naming the handler
- `...`: variable arguments

**Description**

This call allocates interrupt resources and enables the interrupt line and
IRQ handling. From the point this call is made **handler** and **thread_fn** may
be invoked.  All interrupts requested using this function might be shared.

**dev_id** must not be NULL and must be globally unique.

#### <a id="c-pci_free_irq"></a>`pci_free_irq()`

```c
void pci_free_irq(struct pci_dev *dev, unsigned int nr, void *dev_id)
```

free an interrupt allocated with pci_request_irq

**Parameters**

- `struct pci_dev *dev`: PCI device to operate on
- `unsigned int nr`: device-relative interrupt vector index (0-based).
- `void *dev_id`: Device identity to free

**Description**

Remove an interrupt handler. The handler is removed and if the interrupt
line is no longer in use by any driver it is disabled.  The caller must
ensure the interrupt is disabled on the device before calling this function.
The function does not return until any executing interrupts for this IRQ
have completed.

This function must not be called from interrupt context.

#### <a id="c-pci_common_swizzle"></a>`pci_common_swizzle()`

```c
u8 pci_common_swizzle(struct pci_dev *dev, u8 *pinp)
```

swizzle INTx all the way to root bridge

**Parameters**

- `struct pci_dev *dev`: the PCI device
- `u8 *pinp`: pointer to the INTx pin value (1=INTA, 2=INTB, 3=INTD, 4=INTD)

**Description**

Perform INTx swizzling for a device.  This traverses through all PCI-to-PCI
bridges all the way up to a PCI root bus.

#### <a id="c-pci_check_and_mask_intx"></a>`pci_check_and_mask_intx()`

```c
bool pci_check_and_mask_intx(struct pci_dev *dev)
```

mask INTx on pending interrupt

**Parameters**

- `struct pci_dev *dev`: the PCI device to operate on

**Description**

Check if the device dev has its INTx line asserted, mask it and return
true in that case. False is returned if no interrupt was pending.

#### <a id="c-pci_check_and_unmask_intx"></a>`pci_check_and_unmask_intx()`

```c
bool pci_check_and_unmask_intx(struct pci_dev *dev)
```

unmask INTx if no interrupt is pending

**Parameters**

- `struct pci_dev *dev`: the PCI device to operate on

**Description**

Check if the device dev has its INTx line asserted, unmask it if not and
return true. False is returned and the mask remains active if there was
still an interrupt pending.



#### <a id="c-pcie_relaxed_ordering_enabled"></a>`pcie_relaxed_ordering_enabled()`

```c
bool pcie_relaxed_ordering_enabled(struct pci_dev *dev)
```

Probe for PCIe relaxed ordering enable

**Parameters**

- `struct pci_dev *dev`: PCI device to query

**Description**

Returns true if the device has enabled relaxed ordering attribute.

#### <a id="c-pci_scan_slot"></a>`pci_scan_slot()`

```c
int pci_scan_slot(struct pci_bus *bus, int devfn)
```

Scan a PCI slot on a bus for devices

**Parameters**

- `struct pci_bus *bus`: PCI bus to scan
- `int devfn`: slot number to scan (must have zero function)

**Description**

Scan a PCI slot on the specified PCI bus for devices, adding
discovered devices to the **bus->devices** list.  New devices
will not have is_added set.

Returns the number of new devices found.

#### <a id="c-pci_scan_child_bus"></a>`pci_scan_child_bus()`

```c
unsigned int pci_scan_child_bus(struct pci_bus *bus)
```

Scan devices below a bus

**Parameters**

- `struct pci_bus *bus`: Bus to scan for devices

**Description**

Scans devices below **bus** including subordinate buses. Returns new
subordinate number including all the found devices.

#### <a id="c-pci_rescan_bus"></a>`pci_rescan_bus()`

```c
unsigned int pci_rescan_bus(struct pci_bus *bus)
```

Scan a PCI bus for devices

**Parameters**

- `struct pci_bus *bus`: PCI bus to scan

**Description**

Scan a PCI bus and child buses for new devices, add them,
and enable them.

Returns the max number of subordinate bus discovered.



#### <a id="c-pci_create_slot"></a>`pci_create_slot()`

```c
struct pci_slot * pci_create_slot(struct pci_bus *parent, int slot_nr, const char *name, struct hotplug_slot *hotplug)
```

create or increment refcount for physical PCI slot

**Parameters**

- `struct pci_bus *parent`: struct pci_bus of parent bridge
- `int slot_nr`: PCI_SLOT(pci_dev->devfn) or -1 for placeholder
- `const char *name`: user visible string presented in /sys/bus/pci/slots/\<name>
- `struct hotplug_slot *hotplug`: set if caller is hotplug driver, NULL otherwise

**Description**

PCI slots have first class attributes such as address, speed, width,
and a `struct pci_slot` is used to manage them. This interface will
either return a new `struct pci_slot` to the caller, or if the pci_slot
already exists, its refcount will be incremented.

Slots are uniquely identified by a **pci_bus**, **slot_nr** tuple.

There are known platforms with broken firmware that assign the same
name to multiple slots. Workaround these broken platforms by renaming
the slots on behalf of the caller. If firmware assigns name N to
multiple slots:

The first slot is assigned N
The second slot is assigned N-1
The third slot is assigned N-2
etc.

Placeholder slots:
In most cases, **pci_bus**, **slot_nr** will be sufficient to uniquely identify
a slot. There is one notable exception - pSeries (rpaphp), where the
**slot_nr** cannot be determined until a device is actually inserted into
the slot. In this scenario, the caller may pass -1 for **slot_nr**.

The following semantics are imposed when the caller passes **slot_nr** ==
-1. First, we no longer check for an existing `struct` pci_slot, as there
may be many slots with **slot_nr** of -1.  The other change in semantics is
user-visible, which is the 'address' parameter presented in sysfs will
consist solely of a dddd:bb tuple, where dddd is the PCI domain of the
`struct` pci_bus and bb is the bus number. In other words, the devfn of
the 'placeholder' slot will not be displayed.

#### <a id="c-pci_destroy_slot"></a>`pci_destroy_slot()`

```c
void pci_destroy_slot(struct pci_slot *slot)
```

decrement refcount for physical PCI slot

**Parameters**

- `struct pci_slot *slot`: struct pci_slot to decrement

**Description**

`struct` pci_slot is refcounted, so destroying them is really easy; we
just call kobject_put on its kobj and let our release methods do the
rest.



#### <a id="c-pci_rebar_bytes_to_size"></a>`pci_rebar_bytes_to_size()`

```c
int pci_rebar_bytes_to_size(u64 bytes)
```

Convert size in bytes to PCI BAR Size

**Parameters**

- `u64 bytes`: size in bytes

**Description**

Convert size in bytes to encoded BAR Size in Resizable BAR Capability
(PCIe r6.2, sec. 7.8.6.3).

**Return**

encoded BAR Size as defined in the PCIe spec (0=1MB, 31=128TB)

#### <a id="c-pci_rebar_size_to_bytes"></a>`pci_rebar_size_to_bytes()`

```c
resource_size_t pci_rebar_size_to_bytes(int size)
```

Convert encoded BAR Size to size in bytes

**Parameters**

- `int size`: encoded BAR Size as defined in the PCIe spec (0=1MB, 31=128TB)

**Return**

BAR size in bytes

#### <a id="c-pci_rebar_get_possible_sizes"></a>`pci_rebar_get_possible_sizes()`

```c
u64 pci_rebar_get_possible_sizes(struct pci_dev *pdev, int bar)
```

get possible sizes for Resizable BAR

**Parameters**

- `struct pci_dev *pdev`: PCI device
- `int bar`: BAR to query

**Description**

Get the possible sizes of a resizable BAR as bitmask.

**Return**

A bitmask of possible sizes (bit 0=1MB, bit 31=128TB), or `0` if
BAR isn't resizable.

#### <a id="c-pci_rebar_size_supported"></a>`pci_rebar_size_supported()`

```c
bool pci_rebar_size_supported(struct pci_dev *pdev, int bar, int size)
```

check if size is supported for BAR

**Parameters**

- `struct pci_dev *pdev`: PCI device
- `int bar`: BAR to check
- `int size`: encoded size as defined in the PCIe spec (0=1MB, 31=128TB)

**Return**

`true` if **bar** is resizable and **size** is supported, otherwise
`false`.

#### <a id="c-pci_rebar_get_max_size"></a>`pci_rebar_get_max_size()`

```c
int pci_rebar_get_max_size(struct pci_dev *pdev, int bar)
```

get the maximum supported size of a BAR

**Parameters**

- `struct pci_dev *pdev`: PCI device
- `int bar`: BAR to query

**Description**

Get the largest supported size of a resizable BAR as a size.

**Return**

the encoded maximum BAR size as defined in the PCIe spec
(0=1MB, 31=128TB), or `-NOENT` on error.

#### <a id="c-pci_resize_resource"></a>`pci_resize_resource()`

```c
int pci_resize_resource(struct pci_dev *dev, int resno, int size, int exclude_bars)
```

reconfigure a Resizable BAR and resources

**Parameters**

- `struct pci_dev *dev`: the PCI device
- `int resno`: index of the BAR to be resized
- `int size`: new size as defined in the spec (0=1MB, 31=128TB)
- `int exclude_bars`: a mask of BARs that should not be released

**Description**

Reconfigure **resno** to **size** and re-run resource assignment algorithm
with the new size.

Prior to resize, release **dev** resources that share a bridge window with
**resno**.  This unpins the bridge window resource to allow changing it.

The caller may prevent releasing a particular BAR by providing
**exclude_bars** mask, but this may result in the resize operation failing
due to insufficient space.

**Return**

0 on success, or negative on error. In case of an error, the
resources are restored to their original places.



#### <a id="c-pci_enable_rom"></a>`pci_enable_rom()`

```c
int pci_enable_rom(struct pci_dev *pdev)
```

enable ROM decoding for a PCI device

**Parameters**

- `struct pci_dev *pdev`: PCI device to enable

**Description**

Enable ROM decoding on **dev**.  This involves simply turning on the last
bit of the PCI ROM BAR.  Note that some cards may share address decoders
between the ROM and other resources, so enabling it may disable access
to MMIO registers or other card memory.

#### <a id="c-pci_disable_rom"></a>`pci_disable_rom()`

```c
void pci_disable_rom(struct pci_dev *pdev)
```

disable ROM decoding for a PCI device

**Parameters**

- `struct pci_dev *pdev`: PCI device to disable

**Description**

Disable ROM decoding on a PCI device by turning off the last bit in the
ROM BAR.

#### <a id="c-pci_map_rom"></a>`pci_map_rom()`

```c
void __iomem * pci_map_rom(struct pci_dev *pdev, size_t *size)
```

map a PCI ROM to kernel space

**Parameters**

- `struct pci_dev *pdev`: pointer to pci device struct
- `size_t *size`: pointer to receive size of pci window over ROM

**Return**

kernel virtual pointer to image of ROM

**Description**

Map a PCI ROM into kernel space. If ROM is boot video ROM,
the shadow BIOS copy will be returned instead of the
actual ROM.

#### <a id="c-pci_unmap_rom"></a>`pci_unmap_rom()`

```c
void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
```

unmap the ROM from kernel space

**Parameters**

- `struct pci_dev *pdev`: pointer to pci device struct
- `void __iomem *rom`: virtual address of the previous mapping

**Description**

Remove a mapping of a previously mapped ROM



#### <a id="c-pci_iov_get_pf_drvdata"></a>`pci_iov_get_pf_drvdata()`

```c
void * pci_iov_get_pf_drvdata(struct pci_dev *dev, struct pci_driver *pf_driver)
```

Return the drvdata of a PF

**Parameters**

- `struct pci_dev *dev`: VF pci_dev
- `struct pci_driver *pf_driver`: Device driver required to own the PF

**Description**

This must be called from a context that ensures that a VF driver is attached.
The value returned is invalid once the VF driver completes its `remove()`
callback.

Locking is achieved by the driver core. A VF driver cannot be probed until
[`pci_enable_sriov()`](#c-pci_enable_sriov) is called and [`pci_disable_sriov()`](#c-pci_disable_sriov) does not return until
all VF drivers have completed their `remove()`.

The PF driver must call [`pci_disable_sriov()`](#c-pci_disable_sriov) before it begins to destroy the
drvdata.

#### <a id="c-pci_enable_sriov"></a>`pci_enable_sriov()`

```c
int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn)
```

enable the SR-IOV capability

**Parameters**

- `struct pci_dev *dev`: the PCI device
- `int nr_virtfn`: number of virtual functions to enable

**Description**

Returns 0 on success, or negative on failure.

#### <a id="c-pci_disable_sriov"></a>`pci_disable_sriov()`

```c
void pci_disable_sriov(struct pci_dev *dev)
```

disable the SR-IOV capability

**Parameters**

- `struct pci_dev *dev`: the PCI device

#### <a id="c-pci_num_vf"></a>`pci_num_vf()`

```c
int pci_num_vf(struct pci_dev *dev)
```

return number of VFs associated with a PF device_release_driver

**Parameters**

- `struct pci_dev *dev`: the PCI device

**Description**

Returns number of VFs, or 0 if SR-IOV is not enabled.

#### <a id="c-pci_vfs_assigned"></a>`pci_vfs_assigned()`

```c
int pci_vfs_assigned(struct pci_dev *dev)
```

returns number of VFs are assigned to a guest

**Parameters**

- `struct pci_dev *dev`: the PCI device

**Description**

Returns number of VFs belonging to this device that are assigned to a guest.
If device is not a physical function returns 0.

#### <a id="c-pci_sriov_set_totalvfs"></a>`pci_sriov_set_totalvfs()`

```c
int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs)
```

- reduce the TotalVFs available

**Parameters**

- `struct pci_dev *dev`: the PCI PF device
- `u16 numvfs`: number that should be used for TotalVFs supported

**Description**

Should be called from PF driver's probe routine with
device's mutex held.

Returns 0 if PF is an SRIOV-capable device and
value of numvfs valid. If not a PF return -ENOSYS;
if numvfs is invalid return -EINVAL;
if VFs already enabled, return -EBUSY.

#### <a id="c-pci_sriov_get_totalvfs"></a>`pci_sriov_get_totalvfs()`

```c
int pci_sriov_get_totalvfs(struct pci_dev *dev)
```

- get total VFs supported on this device

**Parameters**

- `struct pci_dev *dev`: the PCI PF device

**Description**

For a PCIe device with SRIOV support, return the PCIe
SRIOV capability value of TotalVFs or the value of driver_max_VFs
if the driver reduced it.  Otherwise 0.

#### <a id="c-pci_sriov_configure_simple"></a>`pci_sriov_configure_simple()`

```c
int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn)
```

helper to configure SR-IOV

**Parameters**

- `struct pci_dev *dev`: the PCI device
- `int nr_virtfn`: number of virtual functions to enable, 0 to disable

**Description**

Enable or disable SR-IOV for devices that don't require any PF setup
before enabling SR-IOV.  Return value is negative on error, or number of
VFs allocated on success.

#### <a id="c-pci_iov_vf_bar_set_size"></a>`pci_iov_vf_bar_set_size()`

```c
int pci_iov_vf_bar_set_size(struct pci_dev *dev, int resno, int size)
```

set a new size for a VF BAR

**Parameters**

- `struct pci_dev *dev`: the PCI device
- `int resno`: the resource number
- `int size`: new size as defined in the spec (0=1MB, 31=128TB)

**Description**

Set the new size of a VF BAR that supports VF resizable BAR capability.
Unlike [`pci_resize_resource()`](#c-pci_resize_resource), this does not cause the resource that
reserves the MMIO space (originally up to total_VFs) to be resized, which
means that following calls to [`pci_enable_sriov()`](#c-pci_enable_sriov) can fail if the resources
no longer fit.

**Return**

0 on success, or negative on failure.

#### <a id="c-pci_iov_vf_bar_get_sizes"></a>`pci_iov_vf_bar_get_sizes()`

```c
u32 pci_iov_vf_bar_get_sizes(struct pci_dev *dev, int resno, int num_vfs)
```

get VF BAR sizes allowing to create up to num_vfs

**Parameters**

- `struct pci_dev *dev`: the PCI device
- `int resno`: the resource number
- `int num_vfs`: number of VFs

**Description**

Get the sizes of a VF resizable BAR that can accommodate **num_vfs** within
the currently assigned size of the resource **resno**.

**Return**

A bitmask of sizes in format defined in the spec (bit 0=1MB,
bit 31=128TB).



#### <a id="c-pci_read_legacy_io"></a>`pci_read_legacy_io()`

```c
ssize_t pci_read_legacy_io(struct file *filp, struct kobject *kobj, const struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
```

read byte(s) from legacy I/O port space

**Parameters**

- `struct file *filp`: open sysfs file
- `struct kobject *kobj`: kobject corresponding to file to read from
- `const struct bin_attribute *bin_attr`: struct bin_attribute for this file
- `char *buf`: buffer to store results
- `loff_t off`: offset into legacy I/O port space
- `size_t count`: number of bytes to read

**Description**

Reads 1, 2, or 4 bytes from legacy I/O port space using an arch specific
callback routine (pci_legacy_read).

#### <a id="c-pci_write_legacy_io"></a>`pci_write_legacy_io()`

```c
ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, const struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
```

write byte(s) to legacy I/O port space

**Parameters**

- `struct file *filp`: open sysfs file
- `struct kobject *kobj`: kobject corresponding to file to read from
- `const struct bin_attribute *bin_attr`: struct bin_attribute for this file
- `char *buf`: buffer containing value to be written
- `loff_t off`: offset into legacy I/O port space
- `size_t count`: number of bytes to write

**Description**

Writes 1, 2, or 4 bytes from legacy I/O port space using an arch specific
callback routine (pci_legacy_write).

#### <a id="c-pci_mmap_legacy_mem"></a>`pci_mmap_legacy_mem()`

```c
int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, const struct bin_attribute *attr, struct vm_area_struct *vma)
```

map legacy PCI memory into user memory space

**Parameters**

- `struct file *filp`: open sysfs file
- `struct kobject *kobj`: kobject corresponding to device to be mapped
- `const struct bin_attribute *attr`: struct bin_attribute for this file
- `struct vm_area_struct *vma`: struct vm_area_struct passed to mmap

**Description**

Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap
legacy memory space (first meg of bus space) into application virtual
memory space.

#### <a id="c-pci_mmap_legacy_io"></a>`pci_mmap_legacy_io()`

```c
int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, const struct bin_attribute *attr, struct vm_area_struct *vma)
```

map legacy PCI IO into user memory space

**Parameters**

- `struct file *filp`: open sysfs file
- `struct kobject *kobj`: kobject corresponding to device to be mapped
- `const struct bin_attribute *attr`: struct bin_attribute for this file
- `struct vm_area_struct *vma`: struct vm_area_struct passed to mmap

**Description**

Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap
legacy IO space (first meg of bus space) into application virtual
memory space. Returns -ENOSYS if the operation isn't supported

#### <a id="c-pci_adjust_legacy_attr"></a>`pci_adjust_legacy_attr()`

```c
void pci_adjust_legacy_attr(struct pci_bus *b, enum pci_mmap_state mmap_type)
```

adjustment of legacy file attributes

**Parameters**

- `struct pci_bus *b`: bus to create files under
- `enum pci_mmap_state mmap_type`: I/O port or memory

**Description**

Stub implementation. Can be overridden by arch if necessary.

#### <a id="c-pci_create_legacy_files"></a>`pci_create_legacy_files()`

```c
void pci_create_legacy_files(struct pci_bus *b)
```

create legacy I/O port and memory files

**Parameters**

- `struct pci_bus *b`: bus to create files under

**Description**

Some platforms allow access to legacy I/O port and ISA memory space on
a per-bus basis.  This routine creates the files and ties them into
their associated read, write and mmap files from pci-sysfs.c

On error unwind, but don't propagate the error to the caller
as it is ok to set up the PCI bus without these files.

#### <a id="c-pci_mmap_resource"></a>`pci_mmap_resource()`

```c
int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *attr, struct vm_area_struct *vma, int write_combine)
```

map a PCI resource into user memory space

**Parameters**

- `struct kobject *kobj`: kobject for mapping
- `const struct bin_attribute *attr`: struct bin_attribute for the file being mapped
- `struct vm_area_struct *vma`: struct vm_area_struct passed into the mmap
- `int write_combine`: 1 for write_combine mapping

**Description**

Use the regular PCI mapping routines to map a PCI resource into userspace.

#### <a id="c-pci_remove_resource_files"></a>`pci_remove_resource_files()`

```c
void pci_remove_resource_files(struct pci_dev *pdev)
```

cleanup resource files

**Parameters**

- `struct pci_dev *pdev`: dev to cleanup

**Description**

If we created resource files for **pdev**, remove them from sysfs and
free their resources.

#### <a id="c-pci_create_resource_files"></a>`pci_create_resource_files()`

```c
int pci_create_resource_files(struct pci_dev *pdev)
```

create resource files in sysfs for **dev**

**Parameters**

- `struct pci_dev *pdev`: dev in question

**Description**

Walk the resources in **pdev** creating files for each resource available.

#### <a id="c-pci_write_rom"></a>`pci_write_rom()`

```c
ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, const struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
```

used to enable access to the PCI ROM display

**Parameters**

- `struct file *filp`: sysfs file
- `struct kobject *kobj`: kernel object handle
- `const struct bin_attribute *bin_attr`: struct bin_attribute for this file
- `char *buf`: user input
- `loff_t off`: file offset
- `size_t count`: number of byte in input

**Description**

writing anything except 0 enables it

#### <a id="c-pci_read_rom"></a>`pci_read_rom()`

```c
ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, const struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
```

read a PCI ROM

**Parameters**

- `struct file *filp`: sysfs file
- `struct kobject *kobj`: kernel object handle
- `const struct bin_attribute *bin_attr`: struct bin_attribute for this file
- `char *buf`: where to put the data we read from the ROM
- `loff_t off`: file offset
- `size_t count`: number of bytes to read

**Description**

Put **count** bytes starting at **off** into **buf** from the ROM in the PCI
device corresponding to **kobj**.

#### <a id="c-pci_remove_sysfs_dev_files"></a>`pci_remove_sysfs_dev_files()`

```c
void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
```

cleanup PCI specific sysfs files

**Parameters**

- `struct pci_dev *pdev`: device whose entries we should free

**Description**

Cleanup when **pdev** is removed from sysfs.



#### <a id="c-pcie_tph_get_st_table_loc"></a>`pcie_tph_get_st_table_loc()`

```c
u32 pcie_tph_get_st_table_loc(struct pci_dev *pdev)
```

Return the device's ST table location

**Parameters**

- `struct pci_dev *pdev`: PCI device to query

**Return**

PCI_TPH_LOC_NONE - Not present
PCI_TPH_LOC_CAP  - Located in the TPH Requester Extended Capability
PCI_TPH_LOC_MSIX - Located in the MSI-X Table

#### <a id="c-pcie_tph_get_cpu_st"></a>`pcie_tph_get_cpu_st()`

```c
int pcie_tph_get_cpu_st(struct pci_dev *pdev, enum tph_mem_type mem_type, unsigned int cpu_uid, u16 *tag)
```

Retrieve Steering Tag for a target memory associated with a specific CPU

**Parameters**

- `struct pci_dev *pdev`: PCI device
- `enum tph_mem_type mem_type`: target memory type (volatile or persistent RAM)
- `unsigned int cpu_uid`: associated CPU id
- `u16 *tag`: Steering Tag to be returned

**Description**

Return the Steering Tag for a target memory that is associated with a
specific CPU as indicated by cpu_uid.

**Return**

0 if success, otherwise negative value (-errno)

#### <a id="c-pcie_tph_set_st_entry"></a>`pcie_tph_set_st_entry()`

```c
int pcie_tph_set_st_entry(struct pci_dev *pdev, unsigned int index, u16 tag)
```

Set Steering Tag in the ST table entry

**Parameters**

- `struct pci_dev *pdev`: PCI device
- `unsigned int index`: ST table entry index
- `u16 tag`: Steering Tag to be written

**Description**

Figure out the proper location of ST table, either in the MSI-X table or
in the TPH Extended Capability space, and write the Steering Tag into
the ST entry pointed by index.

**Return**

0 if success, otherwise negative value (-errno)

#### <a id="c-pcie_disable_tph"></a>`pcie_disable_tph()`

```c
void pcie_disable_tph(struct pci_dev *pdev)
```

Turn off TPH support for device

**Parameters**

- `struct pci_dev *pdev`: PCI device

**Return**

none

#### <a id="c-pcie_enable_tph"></a>`pcie_enable_tph()`

```c
int pcie_enable_tph(struct pci_dev *pdev, int mode)
```

Enable TPH support for device using a specific ST mode

**Parameters**

- `struct pci_dev *pdev`: PCI device
- `int mode`: ST mode to enable. Current supported modes include:

**Description**

- PCI_TPH_ST_NS_MODE: NO ST Mode
- PCI_TPH_ST_IV_MODE: Interrupt Vector Mode
- PCI_TPH_ST_DS_MODE: Device Specific Mode

Check whether the mode is actually supported by the device before enabling
and return an error if not. Additionally determine what types of requests,
TPH or extended TPH, can be issued by the device based on its TPH requester
capability and the Root Port's completer capability.

**Return**

0 on success, otherwise negative value (-errno)


<a id="rst-pci-hotplug-support-library"></a>

## PCI Hotplug Support Library


#### <a id="c-__pci_hp_register"></a>`__pci_hp_register()`

```c
int __pci_hp_register(struct hotplug_slot *slot, struct pci_bus *bus, int devnr, const char *name, struct module *owner, const char *mod_name)
```

register a hotplug_slot with the PCI hotplug subsystem

**Parameters**

- `struct hotplug_slot *slot`: pointer to the `struct hotplug_slot` to register
- `struct pci_bus *bus`: bus this slot is on
- `int devnr`: device number
- `const char *name`: name registered with kobject core
- `struct module *owner`: caller module owner
- `const char *mod_name`: caller module name

**Description**

Prepares a hotplug slot for in-kernel use and immediately publishes it to
user space in one go.  Drivers may alternatively carry out the two steps
separately by invoking `pci_hp_initialize()` and [`pci_hp_add()`](#c-pci_hp_add).

Returns 0 if successful, anything else for an error.

#### <a id="c-__pci_hp_initialize"></a>`__pci_hp_initialize()`

```c
int __pci_hp_initialize(struct hotplug_slot *slot, struct pci_bus *bus, int devnr, const char *name, struct module *owner, const char *mod_name)
```

prepare hotplug slot for in-kernel use

**Parameters**

- `struct hotplug_slot *slot`: pointer to the `struct hotplug_slot` to initialize
- `struct pci_bus *bus`: bus this slot is on
- `int devnr`: slot number
- `const char *name`: name registered with kobject core
- `struct module *owner`: caller module owner
- `const char *mod_name`: caller module name

**Description**

Allocate and fill in a PCI slot for use by a hotplug driver.  Once this has
been called, the driver may invoke `hotplug_slot_name()` to get the slot's
unique name.  The driver must be prepared to handle a ->reset_slot callback
from this point on.

Returns 0 on success or a negative int on error.

#### <a id="c-pci_hp_add"></a>`pci_hp_add()`

```c
int pci_hp_add(struct hotplug_slot *slot)
```

publish hotplug slot to user space

**Parameters**

- `struct hotplug_slot *slot`: pointer to the `struct hotplug_slot` to publish

**Description**

Make a hotplug slot's sysfs interface available and inform user space of its
addition by sending a uevent.  The hotplug driver must be prepared to handle
all `struct hotplug_slot_ops` callbacks from this point on.

Returns 0 on success or a negative int on error.

#### <a id="c-pci_hp_deregister"></a>`pci_hp_deregister()`

```c
void pci_hp_deregister(struct hotplug_slot *slot)
```

deregister a hotplug_slot with the PCI hotplug subsystem

**Parameters**

- `struct hotplug_slot *slot`: pointer to the `struct hotplug_slot` to deregister

**Description**

The **slot** must have been registered with the pci hotplug subsystem
previously with a call to `pci_hp_register()`.

#### <a id="c-pci_hp_del"></a>`pci_hp_del()`

```c
void pci_hp_del(struct hotplug_slot *slot)
```

unpublish hotplug slot from user space

**Parameters**

- `struct hotplug_slot *slot`: pointer to the `struct hotplug_slot` to unpublish

**Description**

Remove a hotplug slot's sysfs interface.

#### <a id="c-pci_hp_destroy"></a>`pci_hp_destroy()`

```c
void pci_hp_destroy(struct hotplug_slot *slot)
```

remove hotplug slot from in-kernel use

**Parameters**

- `struct hotplug_slot *slot`: pointer to the `struct hotplug_slot` to destroy

**Description**

Destroy a PCI slot used by a hotplug driver.  Once this has been called,
the driver may no longer invoke `hotplug_slot_name()` to get the slot's
unique name.  The driver no longer needs to handle a ->reset_slot callback
from this point on.

---

# PCI Trusted Execution Environment Security Manager (TSM)

_Peios Kernel / Hardware and drivers / Driver development / PCI_

> Reference for PCI Trusted Execution Environment Security Manager (TSM).

<a id="rst-pci-trusted-execution-environment-security-manager-tsm"></a>

<a id="rst-subsystem-interfaces"></a>

## Subsystem Interfaces

<!-- SPDX-License-Identifier: GPL-2.0 -->

<!-- This data file has been placed in the public domain. -->

<!-- Derived from the Unicode character mappings available from
<http://www.w3.org/2003/entities/xml/>.
Processed by unicode2rstsubs.py, part of Docutils:
<https://docutils.sourceforge.io>. -->


#### <a id="c-struct-pci_ide_partner"></a>`struct pci_ide_partner`

Per port pair Selective IDE Stream settings

**Definition**

```c
struct pci_ide_partner {
    u16 rid_start;
    u16 rid_end;
    u8 stream_index;
    struct pci_bus_region mem_assoc;
    struct pci_bus_region pref_assoc;
    unsigned int default_stream:1;
    unsigned int setup:1;
    unsigned int enable:1;
};
```

**Members**

- `rid_start`: Partner Port Requester ID range start
- `rid_end`: Partner Port Requester ID range end (inclusive)
- `stream_index`: Selective IDE Stream Register Block selection
- `mem_assoc`: PCI bus memory address association for targeting peer partner
- `pref_assoc`: PCI bus prefetchable memory address association for
  targeting peer partner
- `default_stream`: Endpoint uses this stream for all upstream TLPs regardless of
  address and RID association registers
- `setup`: flag to track whether to run [`pci_ide_stream_teardown()`](#c-pci_ide_stream_teardown) for this
  partner slot
- `enable`: flag whether to run [`pci_ide_stream_disable()`](#c-pci_ide_stream_disable) for this partner slot

**Description**

By default, [`pci_ide_stream_alloc()`](#c-pci_ide_stream_alloc) initializes **mem_assoc** and **pref_assoc**
with the immediate ancestor downstream port memory ranges (i.e. Type 1
Configuration Space Header values). Caller may zero size ({0, -1}) the range
to drop it from consideration at [`pci_ide_stream_setup()`](#c-pci_ide_stream_setup) time.

#### <a id="c-struct-pci_ide_regs"></a>`struct pci_ide_regs`

Hardware register association settings for Selective IDE Streams

**Definition**

```c
struct pci_ide_regs {
    u32 rid1;
    u32 rid2;
    struct {
        u32 assoc1;
        u32 assoc2;
        u32 assoc3;
    } addr[2];
    int nr_addr;
};
```

**Members**

- `rid1`: IDE RID Association Register 1
- `rid2`: IDE RID Association Register 2
- `addr`: Up to two address association blocks (IDE Address Association Register
  1 through 3) for MMIO and prefetchable MMIO
- `nr_addr`: Number of address association blocks initialized

**Description**

See `pci_ide_stream_to_regs()`

#### <a id="c-struct-pci_ide"></a>`struct pci_ide`

PCIe Selective IDE Stream descriptor

**Definition**

```c
struct pci_ide {
    struct pci_dev *pdev;
    struct pci_ide_partner partner[PCI_IDE_PARTNER_MAX];
    u8 host_bridge_stream;
    int stream_id;
    const char *name;
};
```

**Members**

- `pdev`: PCIe Endpoint in the pci_ide_partner pair
- `partner`: per-partner settings
- `host_bridge_stream`: allocated from host bridge **ide_stream_ida** pool
- `stream_id`: unique Stream ID (within Partner Port pairing)
- `name`: name of the established Selective IDE Stream in sysfs

**Description**

Negative **stream_id** values indicate "uninitialized" on the
expectation that with TSM established IDE the TSM owns the stream_id
allocation.



#### <a id="c-pci_ide_stream_alloc"></a>`pci_ide_stream_alloc()`

```c
struct pci_ide * pci_ide_stream_alloc(struct pci_dev *pdev)
```

Reserve stream indices and probe for settings

**Parameters**

- `struct pci_dev *pdev`: IDE capable PCIe Endpoint Physical Function

**Description**

Retrieve the Requester ID range of **pdev** for programming its Root
Port IDE RID Association registers, and conversely retrieve the
Requester ID of the Root Port for programming **pdev**'s IDE RID
Association registers.

Allocate a Selective IDE Stream Register Block instance per port.

Allocate a platform stream resource from the associated host bridge.
Retrieve stream association parameters for Requester ID range and
address range restrictions for the stream.

#### <a id="c-pci_ide_stream_free"></a>`pci_ide_stream_free()`

```c
void pci_ide_stream_free(struct pci_ide *ide)
```

unwind [`pci_ide_stream_alloc()`](#c-pci_ide_stream_alloc)

**Parameters**

- `struct pci_ide *ide`: idle IDE settings descriptor

**Description**

Free all of the stream index (register block) allocations acquired by
[`pci_ide_stream_alloc()`](#c-pci_ide_stream_alloc). The stream represented by **ide** is assumed to
be unregistered and not instantiated in any device.

#### <a id="c-pci_ide_stream_release"></a>`pci_ide_stream_release()`

```c
void pci_ide_stream_release(struct pci_ide *ide)
```

unwind and release an **ide** context

**Parameters**

- `struct pci_ide *ide`: partially or fully registered IDE settings descriptor

**Description**

In support of automatic cleanup of IDE setup routines perform IDE
teardown in expected reverse order of setup and with respect to which
aspects of IDE setup have successfully completed.

Be careful that setup order mirrors this shutdown order. Otherwise,
open code releasing the IDE context.

#### <a id="c-pci_ide_stream_register"></a>`pci_ide_stream_register()`

```c
int pci_ide_stream_register(struct pci_ide *ide)
```

Prepare to activate an IDE Stream

**Parameters**

- `struct pci_ide *ide`: IDE settings descriptor

**Description**

After a Stream ID has been acquired for **ide**, record the presence of
the stream in sysfs. The expectation is that **ide** is immutable while
registered.

#### <a id="c-pci_ide_stream_unregister"></a>`pci_ide_stream_unregister()`

```c
void pci_ide_stream_unregister(struct pci_ide *ide)
```

unwind [`pci_ide_stream_register()`](#c-pci_ide_stream_register)

**Parameters**

- `struct pci_ide *ide`: idle IDE settings descriptor

**Description**

In preparation for freeing **ide**, remove sysfs enumeration for the
stream.

#### <a id="c-pci_ide_stream_setup"></a>`pci_ide_stream_setup()`

```c
void pci_ide_stream_setup(struct pci_dev *pdev, struct pci_ide *ide)
```

program settings to Selective IDE Stream registers

**Parameters**

- `struct pci_dev *pdev`: PCIe device object for either a Root Port or Endpoint Partner Port
- `struct pci_ide *ide`: registered IDE settings descriptor

**Description**

When **pdev** is a PCI_EXP_TYPE_ENDPOINT then the PCI_IDE_EP partner
settings are written to **pdev**'s Selective IDE Stream register block,
and when **pdev** is a PCI_EXP_TYPE_ROOT_PORT, the PCI_IDE_RP settings
are selected.

#### <a id="c-pci_ide_stream_teardown"></a>`pci_ide_stream_teardown()`

```c
void pci_ide_stream_teardown(struct pci_dev *pdev, struct pci_ide *ide)
```

disable the stream and clear all settings

**Parameters**

- `struct pci_dev *pdev`: PCIe device object for either a Root Port or Endpoint Partner Port
- `struct pci_ide *ide`: registered IDE settings descriptor

**Description**

For stream destruction, zero all registers that may have been written
by [`pci_ide_stream_setup()`](#c-pci_ide_stream_setup). Consider [`pci_ide_stream_disable()`](#c-pci_ide_stream_disable) to leave
settings in place while temporarily disabling the stream.

#### <a id="c-pci_ide_stream_enable"></a>`pci_ide_stream_enable()`

```c
int pci_ide_stream_enable(struct pci_dev *pdev, struct pci_ide *ide)
```

enable a Selective IDE Stream

**Parameters**

- `struct pci_dev *pdev`: PCIe device object for either a Root Port or Endpoint Partner Port
- `struct pci_ide *ide`: registered and setup IDE settings descriptor

**Description**

Activate the stream by writing to the Selective IDE Stream Control
Register.

Note that the state may go "insecure" at any point after returning 0, but
those events are equivalent to a "link down" event and handled via
asynchronous error reporting.

Caller is responsible to clear the enable bit in the -ENXIO case.

**Return**

0 if the stream successfully entered the "secure" state, and -EINVAL
if **ide** is invalid, and -ENXIO if the stream fails to enter the secure state.

#### <a id="c-pci_ide_stream_disable"></a>`pci_ide_stream_disable()`

```c
void pci_ide_stream_disable(struct pci_dev *pdev, struct pci_ide *ide)
```

disable a Selective IDE Stream

**Parameters**

- `struct pci_dev *pdev`: PCIe device object for either a Root Port or Endpoint Partner Port
- `struct pci_ide *ide`: registered and setup IDE settings descriptor

**Description**

Clear the Selective IDE Stream Control Register, but leave all other
registers untouched.

#### <a id="c-pci_ide_set_nr_streams"></a>`pci_ide_set_nr_streams()`

```c
void pci_ide_set_nr_streams(struct pci_host_bridge *hb, u16 nr)
```

sets size of the pool of IDE Stream resources

**Parameters**

- `struct pci_host_bridge *hb`: host bridge boundary for the stream pool
- `u16 nr`: number of streams

**Description**

Platform PCI init and/or expert test module use only. Limit IDE
Stream establishment by setting the number of stream resources
available at the host bridge. Platform init code must set this before
the first [`pci_ide_stream_alloc()`](#c-pci_ide_stream_alloc) call if the platform has less than the
default of 256 streams per host-bridge.

The "PCI_IDE" symbol namespace is required because this is typically
a detail that is settled in early PCI init. I.e. this export is not
for endpoint drivers.



#### <a id="c-struct-pci_tdi"></a>`struct pci_tdi`

Core TEE I/O Device Interface (TDI) context

**Definition**

```c
struct pci_tdi {
    struct pci_dev *pdev;
    struct kvm *kvm;
    u32 tdi_id;
};
```

**Members**

- `pdev`: host side representation of guest-side TDI
- `kvm`: TEE VM context of bound TDI
- `tdi_id`: Identifier (virtual BDF) for the TDI as referenced by the TSM and DSM

#### <a id="c-struct-pci_tsm"></a>`struct pci_tsm`

Core TSM context for a given PCIe endpoint

**Definition**

```c
struct pci_tsm {
    struct pci_dev *pdev;
    struct pci_dev *dsm_dev;
    struct tsm_dev *tsm_dev;
    struct pci_tdi *tdi;
};
```

**Members**

- `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
- `tdi`: TDI context established by the **bind** link operation

**Description**

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).

#### <a id="c-struct-pci_tsm_pf0"></a>`struct pci_tsm_pf0`

Physical Function 0 TDISP link context

**Definition**

```c
struct pci_tsm_pf0 {
    struct pci_tsm base_tsm;
    struct mutex lock;
    struct pci_doe_mb *doe_mb;
};
```

**Members**

- `base_tsm`: generic core "tsm" context
- `lock`: mutual exclustion for pci_tsm_ops invocation
- `doe_mb`: PCIe Data Object Exchange mailbox

#### <a id="c-enum-pci_tsm_req_scope"></a>`enum pci_tsm_req_scope`

Scope of guest requests to be validated by TSM

**Constants**

- `PCI_TSM_REQ_INFO`: Read-only, without side effects, request for
  typical TDISP collateral information like Device Interface Reports.
  No device secrets are permitted, and no device state is changed.
- `PCI_TSM_REQ_STATE_CHANGE`: Request to change the TDISP state from
  UNLOCKED->LOCKED, LOCKED->RUN, or other architecture specific state
  changes to support those transitions for a TDI. No other (unrelated
  to TDISP) device / host state, configuration, or data change is
  permitted.
- `PCI_TSM_REQ_DEBUG_READ`: Read-only request for debug information

  A method to facilitate TVM information retrieval outside of typical
  TDISP operational requirements. No device secrets are permitted.
- `PCI_TSM_REQ_DEBUG_WRITE`: Device state changes for debug purposes

  The request may affect the operational state of the device outside of
  the TDISP operational model. If allowed, requires CAP_SYS_RAW_IO, and
  will taint the kernel.

**Description**

Guest requests are a transport for a TVM to communicate with a TSM + DSM for
a given TDI. A TSM driver is responsible for maintaining the kernel security
model and limit commands that may affect the host, or are otherwise outside
the typical TDISP operational model.



#### <a id="c-pci_tsm_bind"></a>`pci_tsm_bind()`

```c
int pci_tsm_bind(struct pci_dev *pdev, struct kvm *kvm, u32 tdi_id)
```

Bind **pdev** as a TDI for **kvm**

**Parameters**

- `struct pci_dev *pdev`: PCI device function to bind
- `struct kvm *kvm`: Private memory attach context
- `u32 tdi_id`: Identifier (virtual BDF) for the TDI as referenced by the TSM and DSM

**Description**

Returns 0 on success, or a negative error code on failure.

**Context**

Caller is responsible for constraining the bind lifetime to the
registered state of the device. For example, [`pci_tsm_bind()`](#c-pci_tsm_bind) /
`pci_tsm_unbind()` limited to the VFIO driver bound state of the device.

#### <a id="c-pci_tsm_guest_req"></a>`pci_tsm_guest_req()`

```c
ssize_t pci_tsm_guest_req(struct pci_dev *pdev, enum pci_tsm_req_scope scope, sockptr_t req_in, size_t in_len, sockptr_t req_out, size_t out_len, u64 *tsm_code)
```

helper to marshal guest requests to the TSM driver

**Parameters**

- `struct pci_dev *pdev`:

  **pdev** representing a bound tdi
- `enum pci_tsm_req_scope scope`: caller asserts this passthrough request is limited to TDISP operations
- `sockptr_t req_in`: Input payload forwarded from the guest
- `size_t in_len`: Length of **req_in**
- `sockptr_t req_out`: Output payload buffer response to the guest
- `size_t out_len`: Length of **req_out** on input, bytes filled in **req_out** on output
- `u64 *tsm_code`: Optional TSM arch specific result code for the guest TSM

**Description**

This is a common entry point for requests triggered by userspace KVM-exit
service handlers responding to TDI information or state change requests. The
scope parameter limits requests to TDISP state management, or limited debug.
This path is only suitable for commands and results that are the host kernel
has no use, the host is only facilitating guest to TSM communication.

Returns 0 on success and -error on failure and positive "residue" on success
but **req_out** is filled with less then **out_len**, or **req_out** is NULL and a
residue number of bytes were not consumed from **req_in**.  On success or
failure **tsm_code** may be populated with a TSM implementation specific result
code for the guest to consume.

**Context**

Caller is responsible for calling this within the [`pci_tsm_bind()`](#c-pci_tsm_bind)
state of the TDI.

#### <a id="c-pci_tsm_tdi_constructor"></a>`pci_tsm_tdi_constructor()`

```c
void pci_tsm_tdi_constructor(struct pci_dev *pdev, struct pci_tdi *tdi, struct kvm *kvm, u32 tdi_id)
```

base 'struct pci_tdi' initialization for link TSMs

**Parameters**

- `struct pci_dev *pdev`: PCI device function representing the TDI
- `struct pci_tdi *tdi`: context to initialize
- `struct kvm *kvm`: Private memory attach context
- `u32 tdi_id`: Identifier (virtual BDF) for the TDI as referenced by the TSM and DSM

#### <a id="c-pci_tsm_link_constructor"></a>`pci_tsm_link_constructor()`

```c
int pci_tsm_link_constructor(struct pci_dev *pdev, struct pci_tsm *tsm, struct tsm_dev *tsm_dev)
```

base 'struct pci_tsm' initialization for link TSMs

**Parameters**

- `struct pci_dev *pdev`: The PCI device
- `struct pci_tsm *tsm`: context to initialize
- `struct tsm_dev *tsm_dev`: Platform TEE Security Manager, initiator of security operations

#### <a id="c-pci_tsm_pf0_constructor"></a>`pci_tsm_pf0_constructor()`

```c
int pci_tsm_pf0_constructor(struct pci_dev *pdev, struct pci_tsm_pf0 *tsm, struct tsm_dev *tsm_dev)
```

common 'struct pci_tsm_pf0' (DSM) initialization

**Parameters**

- `struct pci_dev *pdev`: Physical Function 0 PCI device (as indicated by `is_pci_tsm_pf0()`)
- `struct pci_tsm_pf0 *tsm`: context to initialize
- `struct tsm_dev *tsm_dev`: Platform TEE Security Manager, initiator of security operations
