code:
=============================
BTT - Block Translation Table
=============================

1. Introduction
===============

Persistent memory based storage is able to perform IO at byte (or more
accurately, cache line) granularity. However, we often want to expose such
storage as traditional block devices. The block drivers for persistent memory
will do exactly this. However, they do not provide any atomicity guarantees.
Traditional SSDs typically provide protection against torn sectors in hardware,
using stored energy in capacitors to complete in-flight block writes, or perhaps
in firmware. We don't have this luxury with persistent memory - if a write is in
progress, and we experience a power failure, the block will contain a mix of old
and new data. Applications may not be prepared to handle such a scenario.

The Block Translation Table (BTT) provides atomic sector update semantics for
persistent memory devices, so that applications that rely on sector writes not
being torn can continue to do so. The BTT manifests itself as a stacked block
device, and reserves a portion of the underlying storage for its metadata. At
the heart of it, is an indirection table that re-maps all the blocks on the
volume. It can be thought of as an extremely simple file system that only
provides atomic sector updates.

2. Static Layout
================

The underlying storage on which a BTT can be laid out is not limited in any way.
The BTT, however, splits the available space into chunks of up to 512 GiB,
called "Arenas".

Each arena follows the same layout for its metadata, and all references in an
arena are internal to it (with the exception of one field that points to the
next arena). The following depicts the "On-disk" metadata layout::

    Backing Store     +------->  Arena
  +---------------+   |   +------------------+
  |               |   |   | Arena info block |
  |    Arena 0    +---+   |       4K         |
  |     512G      |       +------------------+
  |               |       |                  |
  +---------------+       |                  |
  |               |       |                  |
  |    Arena 1    |       |   Data Blocks    |
  |     512G      |       |                  |
  |               |       |                  |
  +---------------+       |                  |
  |       .       |       |                  |
  |       .       |       |                  |
  |       .       |       |                  |
  |               |       |                  |
  |               |       |                  |
  +---------------+       +------------------+
                          |                  |
                          |     BTT Map      |
                          |                  |
                          |                  |
                          +------------------+
                          |                  |
                          |     BTT Flog     |
                          |                  |
                          +------------------+
                          | Info block copy  |
                          |       4K         |
                          +------------------+

3. Theory of Operation
======================

a. The BTT Map
--------------

The map is a simple lookup/indirection table that maps an LBA to an internal
block. Each map entry is 32 bits. The two most significant bits are special
flags, and the remaining form the internal block number.

======== =============================================================
Bit      Description
======== =============================================================
31 - 30	 Error and Zero flags - Used in the following way::

	   == ==  ====================================================
	   31 30  Description
	   == ==  ====================================================
	   0  0	  Initial state. Reads return zeroes; Premap = Postmap
	   0  1	  Zero state: Reads return zeroes
	   1  0	  Error state: Reads fail; Writes clear 'E' bit
	   1  1	  Normal Block – has valid postmap
	   == ==  ====================================================

29 - 0	 Mappings to internal 'postmap' blocks
======== =============================================================

Some of the terminology that will be subsequently used:

============	================================================================
External LBA	LBA as made visible to upper layers.
ABA		Arena Block Address - Block offset/number within an arena
Premap ABA	The block offset into an arena, which was decided upon by range
		checking the External LBA
Postmap ABA	The block number in the "Data Blocks" area obtained after
		indirection from the map
nfree		The number of free blocks that are maintained at any given time.
		This is the number of concurrent writes that can happen to the
		arena.
============	================================================================

For example, after adding a BTT, we surface a disk of 1024G. We get a read for
the external LBA at 768G. This falls into the second arena, and of the 512G
worth of blocks that this arena contributes, this block is at 256G. Thus, the
premap ABA is 256G. We now refer to the map, and find out the mapping for block
'X' (256G) points to block 'Y', say '64'. Thus the postmap ABA is 64.

b. The BTT Flog
---------------

The BTT provides sector atomicity by making every write an "allocating write",
i.e. Every write goes to a "free" block. A running list of free blocks is
maintained in the form of the BTT flog. 'Flog' is a combination of the words
"free list" and "log". The flog contains 'nfree' entries, and an entry contains:

========  =====================================================================
lba       The premap ABA that is being written to
old_map   The old postmap ABA - after 'this' write completes, this will be a
	  free block.
new_map   The new postmap ABA. The map will up updated to reflect this
	  lba->postmap_aba mapping, but we log it here in case we have to
	  recover.
seq	  Sequence number to mark which of the 2 sections of this flog entry is
	  valid/newest. It cycles between 01->10->11->01 (binary) under normal
	  operation, with 00 indicating an uninitialized state.
lba'	  alternate lba entry
old_map'  alternate old postmap entry
new_map'  alternate new postmap entry
seq'	  alternate sequence number.
========  =====================================================================

Each of the above fields is 32-bit, making one entry 32 bytes. Entries are also
padded to 64 bytes to avoid cache line sharing or aliasing. Flog updates are
done such that for any entry being written, it:
a. overwrites the 'old' section in the entry based on sequence numbers
b. writes the 'new' section such that the sequence number is written last.

c. The concept of lanes
-----------------------

While 'nfree' describes the number of concurrent IOs an arena can process
concurrently, 'nlanes' is the number of IOs the BTT device as a whole can
process::

	nlanes = min(nfree, num_cpus)

A lane number is obtained at the start of any IO, and is used for indexing into
all the on-disk and in-memory data structures for the duration of the IO. If
there are more CPUs than the max number of available lanes, than lanes are
protected by spinlocks.

d. In-memory data structure: Read Tracking Table (RTT)
------------------------------------------------------

Consider a case where we have two threads, one doing reads and the other,
writes. We can hit a condition where the writer thread grabs a free block to do
a new IO, but the (slow) reader thread is still reading from it. In other words,
the reader consulted a map entry, and started reading the corresponding block. A
writer started writing to the same external LBA, and finished the write updating
the map for that external LBA to point to its new postmap ABA. At this point the
internal, postmap block that the reader is (still) reading has been inserted
into the list of free blocks. If another write comes in for the same LBA, it can
grab this free block, and start writing to it, causing the reader to read
incorrect data. To prevent this, we introduce the RTT.

The RTT is a simple, per arena table with 'nfree' entries. Every reader inserts
into rtt[lane_number], the postmap ABA it is reading, and clears it after the
read is complete. Every writer thread, after grabbing a free block, checks the
RTT for its presence. If the postmap free block is in the RTT, it waits till the
reader clears the RTT entry, and only then starts writing to it.

e. In-memory data structure: map locks
--------------------------------------

Consider a case where two writer threads are writing to the same LBA. There can
be a race in the following sequence of steps::

	free[lane] = map[premap_aba]
	map[premap_aba] = postmap_aba

Both threads can update their respective free[lane] with the same old, freed
postmap_aba. This has made the layout inconsistent by losing a free entry, and
at the same time, duplicating another free entry for two lanes.

To solve this, we could have a single map lock (per arena) that has to be taken
before performing the above sequence, but we feel that could be too contentious.
Instead we use an array of (nfree) map_locks that is indexed by
(premap_aba modulo nfree).

f. Reconstruction from the Flog
-------------------------------

On startup, we analyze the BTT flog to create our list of free blocks. We walk
through all the entries, and for each lane, of the set of two possible
'sections', we always look at the most recent one only (based on the sequence
number). The reconstruction rules/steps are simple:

- Read map[log_entry.lba].
- If log_entry.new matches the map entry, then log_entry.old is free.
- If log_entry.new does not match the map entry, then log_entry.new is free.
  (This case can only be caused by power-fails/unsafe shutdowns)

g. Summarizing - Read and Write flows
-------------------------------------

Read:

1.  Convert external LBA to arena number + pre-map ABA
2.  Get a lane (and take lane_lock)
3.  Read map to get the entry for this pre-map ABA
4.  Enter post-map ABA into RTT[lane]
5.  If TRIM flag set in map, return zeroes, and end IO (go to step 8)
6.  If ERROR flag set in map, end IO with EIO (go to step 8)
7.  Read data from this block
8.  Remove post-map ABA entry from RTT[lane]
9.  Release lane (and lane_lock)

Write:

1.  Convert external LBA to Arena number + pre-map ABA
2.  Get a lane (and take lane_lock)
3.  Use lane to index into in-memory free list and obtain a new block, next flog
    index, next sequence number
4.  Scan the RTT to check if free block is present, and spin/wait if it is.
5.  Write data to this free block
6.  Read map to get the existing post-map ABA entry for this pre-map ABA
7.  Write flog entry: [premap_aba / old postmap_aba / new postmap_aba / seq_num]
8.  Write new post-map ABA into map.
9.  Write old post-map entry into the free list
10. Calculate next sequence number and write into the free list entry
11. Release lane (and lane_lock)

4. Error Handling
=================

An arena would be in an error state if any of the metadata is corrupted
irrecoverably, either due to a bug or a media error. The following conditions
indicate an error:

- Info block checksum does not match (and recovering from the copy also fails)
- All internal available blocks are not uniquely and entirely addressed by the
  sum of mapped blocks and free blocks (from the BTT flog).
- Rebuilding free list from the flog reveals missing/duplicate/impossible
  entries
- A map entry is out of bounds

If any of these error conditions are encountered, the arena is put into a read
only state using a flag in the info block.

5. Usage
========

The BTT can be set up on any disk (namespace) exposed by the libnvdimm subsystem
(pmem, or blk mode). The easiest way to set up such a namespace is using the
'ndctl' utility [1]:

For example, the ndctl command line to setup a btt with a 4k sector size is::

    ndctl create-namespace -f -e namespace0.0 -m sector -l 4k

See ndctl create-namespace --help for more options.

[1]: https://github.com/pmem/ndctl


code:
.. SPDX-License-Identifier: GPL-2.0

==================================
NVDIMM Runtime Firmware Activation
==================================

Some persistent memory devices run a firmware locally on the device /
"DIMM" to perform tasks like media management, capacity provisioning,
and health monitoring. The process of updating that firmware typically
involves a reboot because it has implications for in-flight memory
transactions. However, reboots are disruptive and at least the Intel
persistent memory platform implementation, described by the Intel ACPI
DSM specification [1], has added support for activating firmware at
runtime.

A native sysfs interface is implemented in libnvdimm to allow platform
to advertise and control their local runtime firmware activation
capability.

The libnvdimm bus object, ndbusX, implements an ndbusX/firmware/activate
attribute that shows the state of the firmware activation as one of 'idle',
'armed', 'overflow', and 'busy'.

- idle:
  No devices are set / armed to activate firmware

- armed:
  At least one device is armed

- busy:
  In the busy state armed devices are in the process of transitioning
  back to idle and completing an activation cycle.

- overflow:
  If the platform has a concept of incremental work needed to perform
  the activation it could be the case that too many DIMMs are armed for
  activation. In that scenario the potential for firmware activation to
  timeout is indicated by the 'overflow' state.

The 'ndbusX/firmware/activate' property can be written with a value of
either 'live', or 'quiesce'. A value of 'quiesce' triggers the kernel to
run firmware activation from within the equivalent of the hibernation
'freeze' state where drivers and applications are notified to stop their
modifications of system memory. A value of 'live' attempts
firmware activation without this hibernation cycle. The
'ndbusX/firmware/activate' property will be elided completely if no
firmware activation capability is detected.

Another property 'ndbusX/firmware/capability' indicates a value of
'live' or 'quiesce', where 'live' indicates that the firmware
does not require or inflict any quiesce period on the system to update
firmware. A capability value of 'quiesce' indicates that firmware does
expect and injects a quiet period for the memory controller, but 'live'
may still be written to 'ndbusX/firmware/activate' as an override to
assume the risk of racing firmware update with in-flight device and
application activity. The 'ndbusX/firmware/capability' property will be
elided completely if no firmware activation capability is detected.

The libnvdimm memory-device / DIMM object, nmemX, implements
'nmemX/firmware/activate' and 'nmemX/firmware/result' attributes to
communicate the per-device firmware activation state. Similar to the
'ndbusX/firmware/activate' attribute, the 'nmemX/firmware/activate'
attribute indicates 'idle', 'armed', or 'busy'. The state transitions
from 'armed' to 'idle' when the system is prepared to activate firmware,
firmware staged + state set to armed, and 'ndbusX/firmware/activate' is
triggered. After that activation event the nmemX/firmware/result
attribute reflects the state of the last activation as one of:

- none:
  No runtime activation triggered since the last time the device was reset

- success:
  The last runtime activation completed successfully.

- fail:
  The last runtime activation failed for device-specific reasons.

- not_staged:
  The last runtime activation failed due to a sequencing error of the
  firmware image not being staged.

- need_reset:
  Runtime firmware activation failed, but the firmware can still be
  activated via the legacy method of power-cycling the system.

[1]: https://docs.pmem.io/persistent-memory/

repo_name: angery/linux


code:
.. SPDX-License-Identifier: GPL-2.0

===================================
Non-Volatile Memory Device (NVDIMM)
===================================

.. toctree::
   :maxdepth: 1

   nvdimm
   btt
   security
   firmware-activate


code:
===============================
LIBNVDIMM: Non-Volatile Devices
===============================

libnvdimm - kernel / libndctl - userspace helper library

nvdimm@lists.linux.dev

Version 13

.. contents:

	Glossary
	Overview
	    Supporting Documents
	    Git Trees
	LIBNVDIMM PMEM
	    PMEM-REGIONs, Atomic Sectors, and DAX
	Example NVDIMM Platform
	LIBNVDIMM Kernel Device Model and LIBNDCTL Userspace API
	    LIBNDCTL: Context
	        libndctl: instantiate a new library context example
	    LIBNVDIMM/LIBNDCTL: Bus
	        libnvdimm: control class device in /sys/class
	        libnvdimm: bus
	        libndctl: bus enumeration example
	    LIBNVDIMM/LIBNDCTL: DIMM (NMEM)
	        libnvdimm: DIMM (NMEM)
	        libndctl: DIMM enumeration example
	    LIBNVDIMM/LIBNDCTL: Region
	        libnvdimm: region
	        libndctl: region enumeration example
	        Why Not Encode the Region Type into the Region Name?
	        How Do I Determine the Major Type of a Region?
	    LIBNVDIMM/LIBNDCTL: Namespace
	        libnvdimm: namespace
	        libndctl: namespace enumeration example
	        libndctl: namespace creation example
	        Why the Term "namespace"?
	    LIBNVDIMM/LIBNDCTL: Block Translation Table "btt"
	        libnvdimm: btt layout
	        libndctl: btt creation example
	Summary LIBNDCTL Diagram

Glossary
========

PMEM:
  A system-physical-address range where writes are persistent.  A
  block device composed of PMEM is capable of DAX.  A PMEM address range
  may span an interleave of several DIMMs.

DPA:
  DIMM Physical Address, is a DIMM-relative offset.  With one DIMM in
  the system there would be a 1:1 system-physical-address:DPA association.
  Once more DIMMs are added a memory controller interleave must be
  decoded to determine the DPA associated with a given
  system-physical-address.

DAX:
  File system extensions to bypass the page cache and block layer to
  mmap persistent memory, from a PMEM block device, directly into a
  process address space.

DSM:
  Device Specific Method: ACPI method to control specific
  device - in this case the firmware.

DCR:
  NVDIMM Control Region Structure defined in ACPI 6 Section 5.2.25.5.
  It defines a vendor-id, device-id, and interface format for a given DIMM.

BTT:
  Block Translation Table: Persistent memory is byte addressable.
  Existing software may have an expectation that the power-fail-atomicity
  of writes is at least one sector, 512 bytes.  The BTT is an indirection
  table with atomic update semantics to front a PMEM block device
  driver and present arbitrary atomic sector sizes.

LABEL:
  Metadata stored on a DIMM device that partitions and identifies
  (persistently names) capacity allocated to different PMEM namespaces. It
  also indicates whether an address abstraction like a BTT is applied to
  the namepsace.  Note that traditional partition tables, GPT/MBR, are
  layered on top of a PMEM namespace, or an address abstraction like BTT
  if present, but partition support is deprecated going forward.

Overview
========

The LIBNVDIMM subsystem provides support for PMEM described by platform
firmware or a device driver. On ACPI based systems the platform firmware
conveys persistent memory resource via the ACPI NFIT "NVDIMM Firmware
Interface Table" in ACPI 6. While the LIBNVDIMM subsystem implementation
is generic and supports pre-NFIT platforms, it was guided by the
superset of capabilities need to support this ACPI 6 definition for
NVDIMM resources. The original implementation supported the
block-window-aperture capability described in the NFIT, but that support
has since been abandoned and never shipped in a product.

Supporting Documents
--------------------

ACPI 6:
	https://www.uefi.org/sites/default/files/resources/ACPI_6.0.pdf
NVDIMM Namespace:
	https://pmem.io/documents/NVDIMM_Namespace_Spec.pdf
DSM Interface Example:
	https://pmem.io/documents/NVDIMM_DSM_Interface_Example.pdf
Driver Writer's Guide:
	https://pmem.io/documents/NVDIMM_Driver_Writers_Guide.pdf

Git Trees
---------

LIBNVDIMM:
	https://git.kernel.org/cgit/linux/kernel/git/nvdimm/nvdimm.git
LIBNDCTL:
	https://github.com/pmem/ndctl.git

LIBNVDIMM PMEM
==============

Prior to the arrival of the NFIT, non-volatile memory was described to a
system in various ad-hoc ways.  Usually only the bare minimum was
provided, namely, a single system-physical-address range where writes
are expected to be durable after a system power loss.  Now, the NFIT
specification standardizes not only the description of PMEM, but also
platform message-passing entry points for control and configuration.

PMEM (nd_pmem.ko): Drives a system-physical-address range.  This range is
contiguous in system memory and may be interleaved (hardware memory controller
striped) across multiple DIMMs.  When interleaved the platform may optionally
provide details of which DIMMs are participating in the interleave.

It is worth noting that when the labeling capability is detected (a EFI
namespace label index block is found), then no block device is created
by default as userspace needs to do at least one allocation of DPA to
the PMEM range.  In contrast ND_NAMESPACE_IO ranges, once registered,
can be immediately attached to nd_pmem. This latter mode is called
label-less or "legacy".

PMEM-REGIONs, Atomic Sectors, and DAX
-------------------------------------

For the cases where an application or filesystem still needs atomic sector
update guarantees it can register a BTT on a PMEM device or partition.  See
LIBNVDIMM/NDCTL: Block Translation Table "btt"

Example NVDIMM Platform
=======================

For the remainder of this document the following diagram will be
referenced for any example sysfs layouts::

                               (a)               (b)           DIMM
            +-------------------+--------+--------+--------+
  +------+  |       pm0.0       |  free  | pm1.0  |  free  |    0
  | imc0 +--+- - - region0- - - +--------+        +--------+
  +--+---+  |       pm0.0       |  free  | pm1.0  |  free  |    1
     |      +-------------------+--------v        v--------+
  +--+---+                               |                 |
  | cpu0 |                                     region1
  +--+---+                               |                 |
     |      +----------------------------^        ^--------+
  +--+---+  |           free             | pm1.0  |  free  |    2
  | imc1 +--+----------------------------|        +--------+
  +------+  |           free             | pm1.0  |  free  |    3
            +----------------------------+--------+--------+

In this platform we have four DIMMs and two memory controllers in one
socket.  Each PMEM interleave set is identified by a region device with
a dynamically assigned id.

    1. The first portion of DIMM0 and DIMM1 are interleaved as REGION0. A
       single PMEM namespace is created in the REGION0-SPA-range that spans most
       of DIMM0 and DIMM1 with a user-specified name of "pm0.0". Some of that
       interleaved system-physical-address range is left free for
       another PMEM namespace to be defined.

    2. In the last portion of DIMM0 and DIMM1 we have an interleaved
       system-physical-address range, REGION1, that spans those two DIMMs as
       well as DIMM2 and DIMM3.  Some of REGION1 is allocated to a PMEM namespace
       named "pm1.0".

    This bus is provided by the kernel under the device
    /sys/devices/platform/nfit_test.0 when the nfit_test.ko module from
    tools/testing/nvdimm is loaded. This module is a unit test for
    LIBNVDIMM and the  acpi_nfit.ko driver.

LIBNVDIMM Kernel Device Model and LIBNDCTL Userspace API
========================================================

What follows is a description of the LIBNVDIMM sysfs layout and a
corresponding object hierarchy diagram as viewed through the LIBNDCTL
API.  The example sysfs paths and diagrams are relative to the Example
NVDIMM Platform which is also the LIBNVDIMM bus used in the LIBNDCTL unit
test.

LIBNDCTL: Context
-----------------

Every API call in the LIBNDCTL library requires a context that holds the
logging parameters and other library instance state.  The library is
based on the libabc template:

	https://git.kernel.org/cgit/linux/kernel/git/kay/libabc.git

LIBNDCTL: instantiate a new library context example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

::

	struct ndctl_ctx *ctx;

	if (ndctl_new(&ctx) == 0)
		return ctx;
	else
		return NULL;

LIBNVDIMM/LIBNDCTL: Bus
-----------------------

A bus has a 1:1 relationship with an NFIT.  The current expectation for
ACPI based systems is that there is only ever one platform-global NFIT.
That said, it is trivial to register multiple NFITs, the specification
does not preclude it.  The infrastructure supports multiple busses and
we use this capability to test multiple NFIT configurations in the unit
test.

LIBNVDIMM: control class device in /sys/class
---------------------------------------------

This character device accepts DSM messages to be passed to DIMM
identified by its NFIT handle::

	/sys/class/nd/ndctl0
	|-- dev
	|-- device -> ../../../ndbus0
	|-- subsystem -> ../../../../../../../class/nd

LIBNVDIMM: bus
--------------

::

	struct nvdimm_bus *nvdimm_bus_register(struct device *parent,
	       struct nvdimm_bus_descriptor *nfit_desc);

::

	/sys/devices/platform/nfit_test.0/ndbus0
	|-- commands
	|-- nd
	|-- nfit
	|-- nmem0
	|-- nmem1
	|-- nmem2
	|-- nmem3
	|-- power
	|-- provider
	|-- region0
	|-- region1
	|-- region2
	|-- region3
	|-- region4
	|-- region5
	|-- uevent
	`-- wait_probe

LIBNDCTL: bus enumeration example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Find the bus handle that describes the bus from Example NVDIMM Platform::

	static struct ndctl_bus *get_bus_by_provider(struct ndctl_ctx *ctx,
			const char *provider)
	{
		struct ndctl_bus *bus;

		ndctl_bus_foreach(ctx, bus)
			if (strcmp(provider, ndctl_bus_get_provider(bus)) == 0)
				return bus;

		return NULL;
	}

	bus = get_bus_by_provider(ctx, "nfit_test.0");

LIBNVDIMM/LIBNDCTL: DIMM (NMEM)
-------------------------------

The DIMM device provides a character device for sending commands to
hardware, and it is a container for LABELs.  If the DIMM is defined by
NFIT then an optional 'nfit' attribute sub-directory is available to add
NFIT-specifics.

Note that the kernel device name for "DIMMs" is "nmemX".  The NFIT
describes these devices via "Memory Device to System Physical Address
Range Mapping Structure", and there is no requirement that they actually
be physical DIMMs, so we use a more generic name.

LIBNVDIMM: DIMM (NMEM)
^^^^^^^^^^^^^^^^^^^^^^

::

	struct nvdimm *nvdimm_create(struct nvdimm_bus *nvdimm_bus, void *provider_data,
			const struct attribute_group **groups, unsigned long flags,
			unsigned long *dsm_mask);

::

	/sys/devices/platform/nfit_test.0/ndbus0
	|-- nmem0
	|   |-- available_slots
	|   |-- commands
	|   |-- dev
	|   |-- devtype
	|   |-- driver -> ../../../../../bus/nd/drivers/nvdimm
	|   |-- modalias
	|   |-- nfit
	|   |   |-- device
	|   |   |-- format
	|   |   |-- handle
	|   |   |-- phys_id
	|   |   |-- rev_id
	|   |   |-- serial
	|   |   `-- vendor
	|   |-- state
	|   |-- subsystem -> ../../../../../bus/nd
	|   `-- uevent
	|-- nmem1
	[..]

LIBNDCTL: DIMM enumeration example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Note, in this example we are assuming NFIT-defined DIMMs which are
identified by an "nfit_handle" a 32-bit value where:

   - Bit 3:0 DIMM number within the memory channel
   - Bit 7:4 memory channel number
   - Bit 11:8 memory controller ID
   - Bit 15:12 socket ID (within scope of a Node controller if node
     controller is present)
   - Bit 27:16 Node Controller ID
   - Bit 31:28 Reserved

::

	static struct ndctl_dimm *get_dimm_by_handle(struct ndctl_bus *bus,
	       unsigned int handle)
	{
		struct ndctl_dimm *dimm;

		ndctl_dimm_foreach(bus, dimm)
			if (ndctl_dimm_get_handle(dimm) == handle)
				return dimm;

		return NULL;
	}

	#define DIMM_HANDLE(n, s, i, c, d) \
		(((n & 0xfff) << 16) | ((s & 0xf) << 12) | ((i & 0xf) << 8) \
		 | ((c & 0xf) << 4) | (d & 0xf))

	dimm = get_dimm_by_handle(bus, DIMM_HANDLE(0, 0, 0, 0, 0));

LIBNVDIMM/LIBNDCTL: Region
--------------------------

A generic REGION device is registered for each PMEM interleave-set /
range. Per the example there are 2 PMEM regions on the "nfit_test.0"
bus. The primary role of regions are to be a container of "mappings".  A
mapping is a tuple of <DIMM, DPA-start-offset, length>.

LIBNVDIMM provides a built-in driver for REGION devices.  This driver
is responsible for all parsing LABELs, if present, and then emitting NAMESPACE
devices for the nd_pmem driver to consume.

In addition to the generic attributes of "mapping"s, "interleave_ways"
and "size" the REGION device also exports some convenience attributes.
"nstype" indicates the integer type of namespace-device this region
emits, "devtype" duplicates the DEVTYPE variable stored by udev at the
'add' event, "modalias" duplicates the MODALIAS variable stored by udev
at the 'add' event, and finally, the optional "spa_index" is provided in
the case where the region is defined by a SPA.

LIBNVDIMM: region::

	struct nd_region *nvdimm_pmem_region_create(struct nvdimm_bus *nvdimm_bus,
			struct nd_region_desc *ndr_desc);

::

	/sys/devices/platform/nfit_test.0/ndbus0
	|-- region0
	|   |-- available_size
	|   |-- btt0
	|   |-- btt_seed
	|   |-- devtype
	|   |-- driver -> ../../../../../bus/nd/drivers/nd_region
	|   |-- init_namespaces
	|   |-- mapping0
	|   |-- mapping1
	|   |-- mappings
	|   |-- modalias
	|   |-- namespace0.0
	|   |-- namespace_seed
	|   |-- numa_node
	|   |-- nfit
	|   |   `-- spa_index
	|   |-- nstype
	|   |-- set_cookie
	|   |-- size
	|   |-- subsystem -> ../../../../../bus/nd
	|   `-- uevent
	|-- region1
	[..]

LIBNDCTL: region enumeration example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sample region retrieval routines based on NFIT-unique data like
"spa_index" (interleave set id).

::

	static struct ndctl_region *get_pmem_region_by_spa_index(struct ndctl_bus *bus,
			unsigned int spa_index)
	{
		struct ndctl_region *region;

		ndctl_region_foreach(bus, region) {
			if (ndctl_region_get_type(region) != ND_DEVICE_REGION_PMEM)
				continue;
			if (ndctl_region_get_spa_index(region) == spa_index)
				return region;
		}
		return NULL;
	}

LIBNVDIMM/LIBNDCTL: Namespace
-----------------------------

A REGION, after resolving DPA aliasing and LABEL specified boundaries, surfaces
one or more "namespace" devices.  The arrival of a "namespace" device currently
triggers the nd_pmem driver to load and register a disk/block device.

LIBNVDIMM: namespace
^^^^^^^^^^^^^^^^^^^^

Here is a sample layout from the 2 major types of NAMESPACE where namespace0.0
represents DIMM-info-backed PMEM (note that it has a 'uuid' attribute), and
namespace1.0 represents an anonymous PMEM namespace (note that has no 'uuid'
attribute due to not support a LABEL)

::

	/sys/devices/platform/nfit_test.0/ndbus0/region0/namespace0.0
	|-- alt_name
	|-- devtype
	|-- dpa_extents
	|-- force_raw
	|-- modalias
	|-- numa_node
	|-- resource
	|-- size
	|-- subsystem -> ../../../../../../bus/nd
	|-- type
	|-- uevent
	`-- uuid
	/sys/devices/platform/nfit_test.1/ndbus1/region1/namespace1.0
	|-- block
	|   `-- pmem0
	|-- devtype
	|-- driver -> ../../../../../../bus/nd/drivers/pmem
	|-- force_raw
	|-- modalias
	|-- numa_node
	|-- resource
	|-- size
	|-- subsystem -> ../../../../../../bus/nd
	|-- type
	`-- uevent

LIBNDCTL: namespace enumeration example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Namespaces are indexed relative to their parent region, example below.
These indexes are mostly static from boot to boot, but subsystem makes
no guarantees in this regard.  For a static namespace identifier use its
'uuid' attribute.

::

  static struct ndctl_namespace
  *get_namespace_by_id(struct ndctl_region *region, unsigned int id)
  {
          struct ndctl_namespace *ndns;

          ndctl_namespace_foreach(region, ndns)
                  if (ndctl_namespace_get_id(ndns) == id)
                          return ndns;

          return NULL;
  }

LIBNDCTL: namespace creation example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Idle namespaces are automatically created by the kernel if a given
region has enough available capacity to create a new namespace.
Namespace instantiation involves finding an idle namespace and
configuring it.  For the most part the setting of namespace attributes
can occur in any order, the only constraint is that 'uuid' must be set
before 'size'.  This enables the kernel to track DPA allocations
internally with a static identifier::

  static int configure_namespace(struct ndctl_region *region,
                  struct ndctl_namespace *ndns,
                  struct namespace_parameters *parameters)
  {
          char devname[50];

          snprintf(devname, sizeof(devname), "namespace%d.%d",
                          ndctl_region_get_id(region), paramaters->id);

          ndctl_namespace_set_alt_name(ndns, devname);
          /* 'uuid' must be set prior to setting size! */
          ndctl_namespace_set_uuid(ndns, paramaters->uuid);
          ndctl_namespace_set_size(ndns, paramaters->size);
          /* unlike pmem namespaces, blk namespaces have a sector size */
          if (parameters->lbasize)
                  ndctl_namespace_set_sector_size(ndns, parameters->lbasize);
          ndctl_namespace_enable(ndns);
  }

Why the Term "namespace"?
^^^^^^^^^^^^^^^^^^^^^^^^^

    1. Why not "volume" for instance?  "volume" ran the risk of confusing
       ND (libnvdimm subsystem) to a volume manager like device-mapper.

    2. The term originated to describe the sub-devices that can be created
       within a NVME controller (see the nvme specification:
       https://www.nvmexpress.org/specifications/), and NFIT namespaces are
       meant to parallel the capabilities and configurability of
       NVME-namespaces.

LIBNVDIMM/LIBNDCTL: Block Translation Table "btt"
-------------------------------------------------

A BTT (design document: https://pmem.io/2014/09/23/btt.html) is a
personality driver for a namespace that fronts entire namespace as an
'address abstraction'.

LIBNVDIMM: btt layout
^^^^^^^^^^^^^^^^^^^^^

Every region will start out with at least one BTT device which is the
seed device.  To activate it set the "namespace", "uuid", and
"sector_size" attributes and then bind the device to the nd_pmem or
nd_blk driver depending on the region type::

	/sys/devices/platform/nfit_test.1/ndbus0/region0/btt0/
	|-- namespace
	|-- delete
	|-- devtype
	|-- modalias
	|-- numa_node
	|-- sector_size
	|-- subsystem -> ../../../../../bus/nd
	|-- uevent
	`-- uuid

LIBNDCTL: btt creation example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Similar to namespaces an idle BTT device is automatically created per
region.  Each time this "seed" btt device is configured and enabled a new
seed is created.  Creating a BTT configuration involves two steps of
finding and idle BTT and assigning it to consume a namespace.

::

	static struct ndctl_btt *get_idle_btt(struct ndctl_region *region)
	{
		struct ndctl_btt *btt;

		ndctl_btt_foreach(region, btt)
			if (!ndctl_btt_is_enabled(btt)
					&& !ndctl_btt_is_configured(btt))
				return btt;

		return NULL;
	}

	static int configure_btt(struct ndctl_region *region,
			struct btt_parameters *parameters)
	{
		btt = get_idle_btt(region);

		ndctl_btt_set_uuid(btt, parameters->uuid);
		ndctl_btt_set_sector_size(btt, parameters->sector_size);
		ndctl_btt_set_namespace(btt, parameters->ndns);
		/* turn off raw mode device */
		ndctl_namespace_disable(parameters->ndns);
		/* turn on btt access */
		ndctl_btt_enable(btt);
	}

Once instantiated a new inactive btt seed device will appear underneath
the region.

Once a "namespace" is removed from a BTT that instance of the BTT device
will be deleted or otherwise reset to default values.  This deletion is
only at the device model level.  In order to destroy a BTT the "info
block" needs to be destroyed.  Note, that to destroy a BTT the media
needs to be written in raw mode.  By default, the kernel will autodetect
the presence of a BTT and disable raw mode.  This autodetect behavior
can be suppressed by enabling raw mode for the namespace via the
ndctl_namespace_set_raw_mode() API.

Summary LIBNDCTL Diagram
------------------------

For the given example above, here is the view of the objects as seen by the
LIBNDCTL API::

              +---+
              |CTX|
              +-+-+
                |
  +-------+     |
  | DIMM0 <-+   |      +---------+   +--------------+  +---------------+
  +-------+ |   |    +-> REGION0 +---> NAMESPACE0.0 +--> PMEM8 "pm0.0" |
  | DIMM1 <-+ +-v--+ | +---------+   +--------------+  +---------------+
  +-------+ +-+BUS0+-| +---------+   +--------------+  +----------------------+
  | DIMM2 <-+ +----+ +-> REGION1 +---> NAMESPACE1.0 +--> PMEM6 "pm1.0" | BTT1 |
  +-------+ |        | +---------+   +--------------+  +---------------+------+
  | DIMM3 <-+
  +-------+


code:
===============
NVDIMM Security
===============

1. Introduction
---------------

With the introduction of Intel Device Specific Methods (DSM) v1.8
specification [1], security DSMs are introduced. The spec added the following
security DSMs: "get security state", "set passphrase", "disable passphrase",
"unlock unit", "freeze lock", "secure erase", and "overwrite". A security_ops
data structure has been added to struct dimm in order to support the security
operations and generic APIs are exposed to allow vendor neutral operations.

2. Sysfs Interface
------------------
The "security" sysfs attribute is provided in the nvdimm sysfs directory. For
example:
/sys/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0012:00/ndbus0/nmem0/security

The "show" attribute of that attribute will display the security state for
that DIMM. The following states are available: disabled, unlocked, locked,
frozen, and overwrite. If security is not supported, the sysfs attribute
will not be visible.

The "store" attribute takes several commands when it is being written to
in order to support some of the security functionalities:
update <old_keyid> <new_keyid> - enable or update passphrase.
disable <keyid> - disable enabled security and remove key.
freeze - freeze changing of security states.
erase <keyid> - delete existing user encryption key.
overwrite <keyid> - wipe the entire nvdimm.
master_update <keyid> <new_keyid> - enable or update master passphrase.
master_erase <keyid> - delete existing user encryption key.

3. Key Management
-----------------

The key is associated to the payload by the DIMM id. For example:
# cat /sys/devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0012:00/ndbus0/nmem0/nfit/id
8089-a2-1740-00000133
The DIMM id would be provided along with the key payload (passphrase) to
the kernel.

The security keys are managed on the basis of a single key per DIMM. The
key "passphrase" is expected to be 32bytes long. This is similar to the ATA
security specification [2]. A key is initially acquired via the request_key()
kernel API call during nvdimm unlock. It is up to the user to make sure that
all the keys are in the kernel user keyring for unlock.

A nvdimm encrypted-key of format enc32 has the description format of:
nvdimm:<bus-provider-specific-unique-id>

See file ``Documentation/security/keys/trusted-encrypted.rst`` for creating
encrypted-keys of enc32 format. TPM usage with a master trusted key is
preferred for sealing the encrypted-keys.

4. Unlocking
------------
When the DIMMs are being enumerated by the kernel, the kernel will attempt to
retrieve the key from the kernel user keyring. This is the only time
a locked DIMM can be unlocked. Once unlocked, the DIMM will remain unlocked
until reboot. Typically an entity (i.e. shell script) will inject all the
relevant encrypted-keys into the kernel user keyring during the initramfs phase.
This provides the unlock function access to all the related keys that contain
the passphrase for the respective nvdimms.  It is also recommended that the
keys are injected before libnvdimm is loaded by modprobe.

5. Update
---------
When doing an update, it is expected that the existing key is removed from
the kernel user keyring and reinjected as different (old) key. It's irrelevant
what the key description is for the old key since we are only interested in the
keyid when doing the update operation. It is also expected that the new key
is injected with the description format described from earlier in this
document.  The update command written to the sysfs attribute will be with
the format:
update <old keyid> <new keyid>

If there is no old keyid due to a security enabling, then a 0 should be
passed in.

6. Freeze
---------
The freeze operation does not require any keys. The security config can be
frozen by a user with root privelege.

7. Disable
----------
The security disable command format is:
disable <keyid>

An key with the current passphrase payload that is tied to the nvdimm should be
in the kernel user keyring.

8. Secure Erase
---------------
The command format for doing a secure erase is:
erase <keyid>

An key with the current passphrase payload that is tied to the nvdimm should be
in the kernel user keyring.

9. Overwrite
------------
The command format for doing an overwrite is:
overwrite <keyid>

Overwrite can be done without a key if security is not enabled. A key serial
of 0 can be passed in to indicate no key.

The sysfs attribute "security" can be polled to wait on overwrite completion.
Overwrite can last tens of minutes or more depending on nvdimm size.

An encrypted-key with the current user passphrase that is tied to the nvdimm
should be injected and its keyid should be passed in via sysfs.

10. Master Update
-----------------
The command format for doing a master update is:
update <old keyid> <new keyid>

The operating mechanism for master update is identical to update except the
master passphrase key is passed to the kernel. The master passphrase key
is just another encrypted-key.

This command is only available when security is disabled.

11. Master Erase
----------------
The command format for doing a master erase is:
master_erase <current keyid>

This command has the same operating mechanism as erase except the master
passphrase key is passed to the kernel. The master passphrase key is just
another encrypted-key.

This command is only available when the master security is enabled, indicated
by the extended security status.

[1]: https://pmem.io/documents/NVDIMM_DSM_Interface-V1.8.pdf

[2]: http://www.t13.org/documents/UploadedDocuments/docs2006/e05179r4-ACS-SecurityClarifications.pdf


code:
.. SPDX-License-Identifier: GPL-2.0

===============
NVMEM Subsystem
===============

 Srinivas Kandagatla <srinivas.kandagatla@linaro.org>

This document explains the NVMEM Framework along with the APIs provided,
and how to use it.

1. Introduction
===============
*NVMEM* is the abbreviation for Non Volatile Memory layer. It is used to
retrieve configuration of SOC or Device specific data from non volatile
memories like eeprom, efuses and so on.

Before this framework existed, NVMEM drivers like eeprom were stored in
drivers/misc, where they all had to duplicate pretty much the same code to
register a sysfs file, allow in-kernel users to access the content of the
devices they were driving, etc.

This was also a problem as far as other in-kernel users were involved, since
the solutions used were pretty much different from one driver to another, there
was a rather big abstraction leak.

This framework aims at solve these problems. It also introduces DT
representation for consumer devices to go get the data they require (MAC
Addresses, SoC/Revision ID, part numbers, and so on) from the NVMEMs.

NVMEM Providers
+++++++++++++++

NVMEM provider refers to an entity that implements methods to initialize, read
and write the non-volatile memory.

2. Registering/Unregistering the NVMEM provider
===============================================

A NVMEM provider can register with NVMEM core by supplying relevant
nvmem configuration to nvmem_register(), on success core would return a valid
nvmem_device pointer.

nvmem_unregister(nvmem) is used to unregister a previously registered provider.

For example, a simple nvram case::

  static int brcm_nvram_probe(struct platform_device *pdev)
  {
	struct nvmem_config config = {
		.name = "brcm-nvram",
		.reg_read = brcm_nvram_read,
	};
	...
	config.dev = &pdev->dev;
	config.priv = priv;
	config.size = resource_size(res);

	devm_nvmem_register(&config);
  }

Users of board files can define and register nvmem cells using the
nvmem_cell_table struct::

  static struct nvmem_cell_info foo_nvmem_cells[] = {
	{
		.name		= "macaddr",
		.offset		= 0x7f00,
		.bytes		= ETH_ALEN,
	}
  };

  static struct nvmem_cell_table foo_nvmem_cell_table = {
	.nvmem_name		= "i2c-eeprom",
	.cells			= foo_nvmem_cells,
	.ncells			= ARRAY_SIZE(foo_nvmem_cells),
  };

  nvmem_add_cell_table(&foo_nvmem_cell_table);

Additionally it is possible to create nvmem cell lookup entries and register
them with the nvmem framework from machine code as shown in the example below::

  static struct nvmem_cell_lookup foo_nvmem_lookup = {
	.nvmem_name		= "i2c-eeprom",
	.cell_name		= "macaddr",
	.dev_id			= "foo_mac.0",
	.con_id			= "mac-address",
  };

  nvmem_add_cell_lookups(&foo_nvmem_lookup, 1);

NVMEM Consumers
+++++++++++++++

NVMEM consumers are the entities which make use of the NVMEM provider to
read from and to NVMEM.

3. NVMEM cell based consumer APIs
=================================

NVMEM cells are the data entries/fields in the NVMEM.
The NVMEM framework provides 3 APIs to read/write NVMEM cells::

  struct nvmem_cell *nvmem_cell_get(struct device *dev, const char *name);
  struct nvmem_cell *devm_nvmem_cell_get(struct device *dev, const char *name);

  void nvmem_cell_put(struct nvmem_cell *cell);
  void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);

  void *nvmem_cell_read(struct nvmem_cell *cell, ssize_t *len);
  int nvmem_cell_write(struct nvmem_cell *cell, void *buf, ssize_t len);

`*nvmem_cell_get()` apis will get a reference to nvmem cell for a given id,
and nvmem_cell_read/write() can then read or write to the cell.
Once the usage of the cell is finished the consumer should call
`*nvmem_cell_put()` to free all the allocation memory for the cell.

4. Direct NVMEM device based consumer APIs
==========================================

In some instances it is necessary to directly read/write the NVMEM.
To facilitate such consumers NVMEM framework provides below apis::

  struct nvmem_device *nvmem_device_get(struct device *dev, const char *name);
  struct nvmem_device *devm_nvmem_device_get(struct device *dev,
					   const char *name);
  struct nvmem_device *nvmem_device_find(void *data,
			int (*match)(struct device *dev, const void *data));
  void nvmem_device_put(struct nvmem_device *nvmem);
  int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
		      size_t bytes, void *buf);
  int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
		       size_t bytes, void *buf);
  int nvmem_device_cell_read(struct nvmem_device *nvmem,
			   struct nvmem_cell_info *info, void *buf);
  int nvmem_device_cell_write(struct nvmem_device *nvmem,
			    struct nvmem_cell_info *info, void *buf);

Before the consumers can read/write NVMEM directly, it should get hold
of nvmem_controller from one of the `*nvmem_device_get()` api.

The difference between these apis and cell based apis is that these apis always
take nvmem_device as parameter.

5. Releasing a reference to the NVMEM
=====================================

When a consumer no longer needs the NVMEM, it has to release the reference
to the NVMEM it has obtained using the APIs mentioned in the above section.
The NVMEM framework provides 2 APIs to release a reference to the NVMEM::

  void nvmem_cell_put(struct nvmem_cell *cell);
  void devm_nvmem_cell_put(struct device *dev, struct nvmem_cell *cell);
  void nvmem_device_put(struct nvmem_device *nvmem);
  void devm_nvmem_device_put(struct device *dev, struct nvmem_device *nvmem);

Both these APIs are used to release a reference to the NVMEM and
devm_nvmem_cell_put and devm_nvmem_device_put destroys the devres associated
with this NVMEM.

Userspace
+++++++++

6. Userspace binary interface
==============================

Userspace can read/write the raw NVMEM file located at::

	/sys/bus/nvmem/devices/*/nvmem

ex::

  hexdump /sys/bus/nvmem/devices/qfprom0/nvmem

  0000000 0000 0000 0000 0000 0000 0000 0000 0000
  *
  00000a0 db10 2240 0000 e000 0c00 0c00 0000 0c00
  0000000 0000 0000 0000 0000 0000 0000 0000 0000
  ...
  *
  0001000

7. DeviceTree Binding
=====================

See Documentation/devicetree/bindings/nvmem/nvmem.txt


code:
===============================
PARPORT interface documentation
===============================

:Time-stamp: <2000-02-24 13:30:20 twaugh>

Described here are the following functions:

Global functions::
  parport_register_driver
  parport_unregister_driver
  parport_enumerate
  parport_register_device
  parport_unregister_device
  parport_claim
  parport_claim_or_block
  parport_release
  parport_yield
  parport_yield_blocking
  parport_wait_peripheral
  parport_poll_peripheral
  parport_wait_event
  parport_negotiate
  parport_read
  parport_write
  parport_open
  parport_close
  parport_device_id
  parport_device_coords
  parport_find_class
  parport_find_device
  parport_set_timeout

Port functions (can be overridden by low-level drivers):

  SPP::
    port->ops->read_data
    port->ops->write_data
    port->ops->read_status
    port->ops->read_control
    port->ops->write_control
    port->ops->frob_control
    port->ops->enable_irq
    port->ops->disable_irq
    port->ops->data_forward
    port->ops->data_reverse

  EPP::
    port->ops->epp_write_data
    port->ops->epp_read_data
    port->ops->epp_write_addr
    port->ops->epp_read_addr

  ECP::
    port->ops->ecp_write_data
    port->ops->ecp_read_data
    port->ops->ecp_write_addr

  Other::
    port->ops->nibble_read_data
    port->ops->byte_read_data
    port->ops->compat_write_data

The parport subsystem comprises ``parport`` (the core port-sharing
code), and a variety of low-level drivers that actually do the port
accesses.  Each low-level driver handles a particular style of port
(PC, Amiga, and so on).

The parport interface to the device driver author can be broken down
into global functions and port functions.

The global functions are mostly for communicating between the device
driver and the parport subsystem: acquiring a list of available ports,
claiming a port for exclusive use, and so on.  They also include
``generic`` functions for doing standard things that will work on any
IEEE 1284-capable architecture.

The port functions are provided by the low-level drivers, although the
core parport module provides generic ``defaults`` for some routines.
The port functions can be split into three groups: SPP, EPP, and ECP.

SPP (Standard Parallel Port) functions modify so-called ``SPP``
registers: data, status, and control.  The hardware may not actually
have registers exactly like that, but the PC does and this interface is
modelled after common PC implementations.  Other low-level drivers may
be able to emulate most of the functionality.

EPP (Enhanced Parallel Port) functions are provided for reading and
writing in IEEE 1284 EPP mode, and ECP (Extended Capabilities Port)
functions are used for IEEE 1284 ECP mode. (What about BECP? Does
anyone care?)

Hardware assistance for EPP and/or ECP transfers may or may not be
available, and if it is available it may or may not be used.  If
hardware is not used, the transfer will be software-driven.  In order
to cope with peripherals that only tenuously support IEEE 1284, a
low-level driver specific function is provided, for altering 'fudge
factors'.

Global functions
================

parport_register_driver - register a device driver with parport
---------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_driver {
		const char *name;
		void (*attach) (struct parport *);
		void (*detach) (struct parport *);
		struct parport_driver *next;
	};
	int parport_register_driver (struct parport_driver *driver);

DESCRIPTION
^^^^^^^^^^^

In order to be notified about parallel ports when they are detected,
parport_register_driver should be called.  Your driver will
immediately be notified of all ports that have already been detected,
and of each new port as low-level drivers are loaded.

A ``struct parport_driver`` contains the textual name of your driver,
a pointer to a function to handle new ports, and a pointer to a
function to handle ports going away due to a low-level driver
unloading.  Ports will only be detached if they are not being used
(i.e. there are no devices registered on them).

The visible parts of the ``struct parport *`` argument given to
attach/detach are::

	struct parport
	{
		struct parport *next; /* next parport in list */
		const char *name;     /* port's name */
		unsigned int modes;   /* bitfield of hardware modes */
		struct parport_device_info probe_info;
				/* IEEE1284 info */
		int number;           /* parport index */
		struct parport_operations *ops;
		...
	};

There are other members of the structure, but they should not be
touched.

The ``modes`` member summarises the capabilities of the underlying
hardware.  It consists of flags which may be bitwise-ored together:

  ============================= ===============================================
  PARPORT_MODE_PCSPP		IBM PC registers are available,
				i.e. functions that act on data,
				control and status registers are
				probably writing directly to the
				hardware.
  PARPORT_MODE_TRISTATE		The data drivers may be turned off.
				This allows the data lines to be used
				for reverse (peripheral to host)
				transfers.
  PARPORT_MODE_COMPAT		The hardware can assist with
				compatibility-mode (printer)
				transfers, i.e. compat_write_block.
  PARPORT_MODE_EPP		The hardware can assist with EPP
				transfers.
  PARPORT_MODE_ECP		The hardware can assist with ECP
				transfers.
  PARPORT_MODE_DMA		The hardware can use DMA, so you might
				want to pass ISA DMA-able memory
				(i.e. memory allocated using the
				GFP_DMA flag with kmalloc) to the
				low-level driver in order to take
				advantage of it.
  ============================= ===============================================

There may be other flags in ``modes`` as well.

The contents of ``modes`` is advisory only.  For example, if the
hardware is capable of DMA, and PARPORT_MODE_DMA is in ``modes``, it
doesn't necessarily mean that DMA will always be used when possible.
Similarly, hardware that is capable of assisting ECP transfers won't
necessarily be used.

RETURN VALUE
^^^^^^^^^^^^

Zero on success, otherwise an error code.

ERRORS
^^^^^^

None. (Can it fail? Why return int?)

EXAMPLE
^^^^^^^

::

	static void lp_attach (struct parport *port)
	{
		...
		private = kmalloc (...);
		dev[count++] = parport_register_device (...);
		...
	}

	static void lp_detach (struct parport *port)
	{
		...
	}

	static struct parport_driver lp_driver = {
		"lp",
		lp_attach,
		lp_detach,
		NULL /* always put NULL here */
	};

	int lp_init (void)
	{
		...
		if (parport_register_driver (&lp_driver)) {
			/* Failed; nothing we can do. */
			return -EIO;
		}
		...
	}

SEE ALSO
^^^^^^^^

parport_unregister_driver, parport_register_device, parport_enumerate

parport_unregister_driver - tell parport to forget about this driver
--------------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_driver {
		const char *name;
		void (*attach) (struct parport *);
		void (*detach) (struct parport *);
		struct parport_driver *next;
	};
	void parport_unregister_driver (struct parport_driver *driver);

DESCRIPTION
^^^^^^^^^^^

This tells parport not to notify the device driver of new ports or of
ports going away.  Registered devices belonging to that driver are NOT
unregistered: parport_unregister_device must be used for each one.

EXAMPLE
^^^^^^^

::

	void cleanup_module (void)
	{
		...
		/* Stop notifications. */
		parport_unregister_driver (&lp_driver);

		/* Unregister devices. */
		for (i = 0; i < NUM_DEVS; i++)
			parport_unregister_device (dev[i]);
		...
	}

SEE ALSO
^^^^^^^^

parport_register_driver, parport_enumerate

parport_enumerate - retrieve a list of parallel ports (DEPRECATED)
------------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport *parport_enumerate (void);

DESCRIPTION
^^^^^^^^^^^

Retrieve the first of a list of valid parallel ports for this machine.
Successive parallel ports can be found using the ``struct parport
*next`` element of the ``struct parport *`` that is returned.  If ``next``
is NULL, there are no more parallel ports in the list.  The number of
ports in the list will not exceed PARPORT_MAX.

RETURN VALUE
^^^^^^^^^^^^

A ``struct parport *`` describing a valid parallel port for the machine,
or NULL if there are none.

ERRORS
^^^^^^

This function can return NULL to indicate that there are no parallel
ports to use.

EXAMPLE
^^^^^^^

::

	int detect_device (void)
	{
		struct parport *port;

		for (port = parport_enumerate ();
		port != NULL;
		port = port->next) {
			/* Try to detect a device on the port... */
			...
		}
		}

		...
	}

NOTES
^^^^^

parport_enumerate is deprecated; parport_register_driver should be
used instead.

SEE ALSO
^^^^^^^^

parport_register_driver, parport_unregister_driver

parport_register_device - register to use a port
------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	typedef int (*preempt_func) (void *handle);
	typedef void (*wakeup_func) (void *handle);
	typedef int (*irq_func) (int irq, void *handle, struct pt_regs *);

	struct pardevice *parport_register_device(struct parport *port,
						  const char *name,
						  preempt_func preempt,
						  wakeup_func wakeup,
						  irq_func irq,
						  int flags,
						  void *handle);

DESCRIPTION
^^^^^^^^^^^

Use this function to register your device driver on a parallel port
(``port``).  Once you have done that, you will be able to use
parport_claim and parport_release in order to use the port.

The (``name``) argument is the name of the device that appears in /proc
filesystem. The string must be valid for the whole lifetime of the
device (until parport_unregister_device is called).

This function will register three callbacks into your driver:
``preempt``, ``wakeup`` and ``irq``.  Each of these may be NULL in order to
indicate that you do not want a callback.

When the ``preempt`` function is called, it is because another driver
wishes to use the parallel port.  The ``preempt`` function should return
non-zero if the parallel port cannot be released yet -- if zero is
returned, the port is lost to another driver and the port must be
re-claimed before use.

The ``wakeup`` function is called once another driver has released the
port and no other driver has yet claimed it.  You can claim the
parallel port from within the ``wakeup`` function (in which case the
claim is guaranteed to succeed), or choose not to if you don't need it
now.

If an interrupt occurs on the parallel port your driver has claimed,
the ``irq`` function will be called. (Write something about shared
interrupts here.)

The ``handle`` is a pointer to driver-specific data, and is passed to
the callback functions.

``flags`` may be a bitwise combination of the following flags:

  ===================== =================================================
        Flag            Meaning
  ===================== =================================================
  PARPORT_DEV_EXCL	The device cannot share the parallel port at all.
			Use this only when absolutely necessary.
  ===================== =================================================

The typedefs are not actually defined -- they are only shown in order
to make the function prototype more readable.

The visible parts of the returned ``struct pardevice`` are::

	struct pardevice {
		struct parport *port;	/* Associated port */
		void *private;		/* Device driver's 'handle' */
		...
	};

RETURN VALUE
^^^^^^^^^^^^

A ``struct pardevice *``: a handle to the registered parallel port
device that can be used for parport_claim, parport_release, etc.

ERRORS
^^^^^^

A return value of NULL indicates that there was a problem registering
a device on that port.

EXAMPLE
^^^^^^^

::

	static int preempt (void *handle)
	{
		if (busy_right_now)
			return 1;

		must_reclaim_port = 1;
		return 0;
	}

	static void wakeup (void *handle)
	{
		struct toaster *private = handle;
		struct pardevice *dev = private->dev;
		if (!dev) return; /* avoid races */

		if (want_port)
			parport_claim (dev);
	}

	static int toaster_detect (struct toaster *private, struct parport *port)
	{
		private->dev = parport_register_device (port, "toaster", preempt,
							wakeup, NULL, 0,
							private);
		if (!private->dev)
			/* Couldn't register with parport. */
			return -EIO;

		must_reclaim_port = 0;
		busy_right_now = 1;
		parport_claim_or_block (private->dev);
		...
		/* Don't need the port while the toaster warms up. */
		busy_right_now = 0;
		...
		busy_right_now = 1;
		if (must_reclaim_port) {
			parport_claim_or_block (private->dev);
			must_reclaim_port = 0;
		}
		...
	}

SEE ALSO
^^^^^^^^

parport_unregister_device, parport_claim

parport_unregister_device - finish using a port
-----------------------------------------------

SYNPOPSIS

::

	#include <linux/parport.h>

	void parport_unregister_device (struct pardevice *dev);

DESCRIPTION
^^^^^^^^^^^

This function is the opposite of parport_register_device.  After using
parport_unregister_device, ``dev`` is no longer a valid device handle.

You should not unregister a device that is currently claimed, although
if you do it will be released automatically.

EXAMPLE
^^^^^^^

::

	...
	kfree (dev->private); /* before we lose the pointer */
	parport_unregister_device (dev);
	...

SEE ALSO
^^^^^^^^

parport_unregister_driver

parport_claim, parport_claim_or_block - claim the parallel port for a device
----------------------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	int parport_claim (struct pardevice *dev);
	int parport_claim_or_block (struct pardevice *dev);

DESCRIPTION
^^^^^^^^^^^

These functions attempt to gain control of the parallel port on which
``dev`` is registered.  ``parport_claim`` does not block, but
``parport_claim_or_block`` may do. (Put something here about blocking
interruptibly or non-interruptibly.)

You should not try to claim a port that you have already claimed.

RETURN VALUE
^^^^^^^^^^^^

A return value of zero indicates that the port was successfully
claimed, and the caller now has possession of the parallel port.

If ``parport_claim_or_block`` blocks before returning successfully, the
return value is positive.

ERRORS
^^^^^^

========== ==========================================================
  -EAGAIN  The port is unavailable at the moment, but another attempt
           to claim it may succeed.
========== ==========================================================

SEE ALSO
^^^^^^^^

parport_release

parport_release - release the parallel port
-------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	void parport_release (struct pardevice *dev);

DESCRIPTION
^^^^^^^^^^^

Once a parallel port device has been claimed, it can be released using
``parport_release``.  It cannot fail, but you should not release a
device that you do not have possession of.

EXAMPLE
^^^^^^^

::

	static size_t write (struct pardevice *dev, const void *buf,
			size_t len)
	{
		...
		written = dev->port->ops->write_ecp_data (dev->port, buf,
							len);
		parport_release (dev);
		...
	}

SEE ALSO
^^^^^^^^

change_mode, parport_claim, parport_claim_or_block, parport_yield

parport_yield, parport_yield_blocking - temporarily release a parallel port
---------------------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	int parport_yield (struct pardevice *dev)
	int parport_yield_blocking (struct pardevice *dev);

DESCRIPTION
^^^^^^^^^^^

When a driver has control of a parallel port, it may allow another
driver to temporarily ``borrow`` it.  ``parport_yield`` does not block;
``parport_yield_blocking`` may do.

RETURN VALUE
^^^^^^^^^^^^

A return value of zero indicates that the caller still owns the port
and the call did not block.

A positive return value from ``parport_yield_blocking`` indicates that
the caller still owns the port and the call blocked.

A return value of -EAGAIN indicates that the caller no longer owns the
port, and it must be re-claimed before use.

ERRORS
^^^^^^

========= ==========================================================
  -EAGAIN  Ownership of the parallel port was given away.
========= ==========================================================

SEE ALSO
^^^^^^^^

parport_release

parport_wait_peripheral - wait for status lines, up to 35ms
-----------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	int parport_wait_peripheral (struct parport *port,
				     unsigned char mask,
				     unsigned char val);

DESCRIPTION
^^^^^^^^^^^

Wait for the status lines in mask to match the values in val.

RETURN VALUE
^^^^^^^^^^^^

======== ==========================================================
 -EINTR  a signal is pending
      0  the status lines in mask have values in val
      1  timed out while waiting (35ms elapsed)
======== ==========================================================

SEE ALSO
^^^^^^^^

parport_poll_peripheral

parport_poll_peripheral - wait for status lines, in usec
--------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	int parport_poll_peripheral (struct parport *port,
				     unsigned char mask,
				     unsigned char val,
				     int usec);

DESCRIPTION
^^^^^^^^^^^

Wait for the status lines in mask to match the values in val.

RETURN VALUE
^^^^^^^^^^^^

======== ==========================================================
 -EINTR  a signal is pending
      0  the status lines in mask have values in val
      1  timed out while waiting (usec microseconds have elapsed)
======== ==========================================================

SEE ALSO
^^^^^^^^

parport_wait_peripheral

parport_wait_event - wait for an event on a port
------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	int parport_wait_event (struct parport *port, signed long timeout)

DESCRIPTION
^^^^^^^^^^^

Wait for an event (e.g. interrupt) on a port.  The timeout is in
jiffies.

RETURN VALUE
^^^^^^^^^^^^

======= ==========================================================
      0  success
     <0  error (exit as soon as possible)
     >0  timed out
======= ==========================================================

parport_negotiate - perform IEEE 1284 negotiation
-------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	int parport_negotiate (struct parport *, int mode);

DESCRIPTION
^^^^^^^^^^^

Perform IEEE 1284 negotiation.

RETURN VALUE
^^^^^^^^^^^^

======= ==========================================================
     0  handshake OK; IEEE 1284 peripheral and mode available
    -1  handshake failed; peripheral not compliant (or none present)
     1  handshake OK; IEEE 1284 peripheral present but mode not
        available
======= ==========================================================

SEE ALSO
^^^^^^^^

parport_read, parport_write

parport_read - read data from device
------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	ssize_t parport_read (struct parport *, void *buf, size_t len);

DESCRIPTION
^^^^^^^^^^^

Read data from device in current IEEE 1284 transfer mode.  This only
works for modes that support reverse data transfer.

RETURN VALUE
^^^^^^^^^^^^

If negative, an error code; otherwise the number of bytes transferred.

SEE ALSO
^^^^^^^^

parport_write, parport_negotiate

parport_write - write data to device
------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	ssize_t parport_write (struct parport *, const void *buf, size_t len);

DESCRIPTION
^^^^^^^^^^^

Write data to device in current IEEE 1284 transfer mode.  This only
works for modes that support forward data transfer.

RETURN VALUE
^^^^^^^^^^^^

If negative, an error code; otherwise the number of bytes transferred.

SEE ALSO
^^^^^^^^

parport_read, parport_negotiate

parport_open - register device for particular device number
-----------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct pardevice *parport_open (int devnum, const char *name,
				        int (*pf) (void *),
					void (*kf) (void *),
					void (*irqf) (int, void *,
						      struct pt_regs *),
					int flags, void *handle);

DESCRIPTION
^^^^^^^^^^^

This is like parport_register_device but takes a device number instead
of a pointer to a struct parport.

RETURN VALUE
^^^^^^^^^^^^

See parport_register_device.  If no device is associated with devnum,
NULL is returned.

SEE ALSO
^^^^^^^^

parport_register_device

parport_close - unregister device for particular device number
--------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	void parport_close (struct pardevice *dev);

DESCRIPTION
^^^^^^^^^^^

This is the equivalent of parport_unregister_device for parport_open.

SEE ALSO
^^^^^^^^

parport_unregister_device, parport_open

parport_device_id - obtain IEEE 1284 Device ID
----------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	ssize_t parport_device_id (int devnum, char *buffer, size_t len);

DESCRIPTION
^^^^^^^^^^^

Obtains the IEEE 1284 Device ID associated with a given device.

RETURN VALUE
^^^^^^^^^^^^

If negative, an error code; otherwise, the number of bytes of buffer
that contain the device ID.  The format of the device ID is as
follows::

	[length][ID]

The first two bytes indicate the inclusive length of the entire Device
ID, and are in big-endian order.  The ID is a sequence of pairs of the
form::

	key:value;

NOTES
^^^^^

Many devices have ill-formed IEEE 1284 Device IDs.

SEE ALSO
^^^^^^^^

parport_find_class, parport_find_device

parport_device_coords - convert device number to device coordinates
-------------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	int parport_device_coords (int devnum, int *parport, int *mux,
				   int *daisy);

DESCRIPTION
^^^^^^^^^^^

Convert between device number (zero-based) and device coordinates
(port, multiplexor, daisy chain address).

RETURN VALUE
^^^^^^^^^^^^

Zero on success, in which case the coordinates are (``*parport``, ``*mux``,
``*daisy``).

SEE ALSO
^^^^^^^^

parport_open, parport_device_id

parport_find_class - find a device by its class
-----------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	typedef enum {
		PARPORT_CLASS_LEGACY = 0,       /* Non-IEEE1284 device */
		PARPORT_CLASS_PRINTER,
		PARPORT_CLASS_MODEM,
		PARPORT_CLASS_NET,
		PARPORT_CLASS_HDC,              /* Hard disk controller */
		PARPORT_CLASS_PCMCIA,
		PARPORT_CLASS_MEDIA,            /* Multimedia device */
		PARPORT_CLASS_FDC,              /* Floppy disk controller */
		PARPORT_CLASS_PORTS,
		PARPORT_CLASS_SCANNER,
		PARPORT_CLASS_DIGCAM,
		PARPORT_CLASS_OTHER,            /* Anything else */
		PARPORT_CLASS_UNSPEC,           /* No CLS field in ID */
		PARPORT_CLASS_SCSIADAPTER
	} parport_device_class;

	int parport_find_class (parport_device_class cls, int from);

DESCRIPTION
^^^^^^^^^^^

Find a device by class.  The search starts from device number from+1.

RETURN VALUE
^^^^^^^^^^^^

The device number of the next device in that class, or -1 if no such
device exists.

NOTES
^^^^^

Example usage::

	int devnum = -1;
	while ((devnum = parport_find_class (PARPORT_CLASS_DIGCAM, devnum)) != -1) {
		struct pardevice *dev = parport_open (devnum, ...);
		...
	}

SEE ALSO
^^^^^^^^

parport_find_device, parport_open, parport_device_id

parport_find_device - find a device by its class
------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	int parport_find_device (const char *mfg, const char *mdl, int from);

DESCRIPTION
^^^^^^^^^^^

Find a device by vendor and model.  The search starts from device
number from+1.

RETURN VALUE
^^^^^^^^^^^^

The device number of the next device matching the specifications, or
-1 if no such device exists.

NOTES
^^^^^

Example usage::

	int devnum = -1;
	while ((devnum = parport_find_device ("IOMEGA", "ZIP+", devnum)) != -1) {
		struct pardevice *dev = parport_open (devnum, ...);
		...
	}

SEE ALSO
^^^^^^^^

parport_find_class, parport_open, parport_device_id

parport_set_timeout - set the inactivity timeout
------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	long parport_set_timeout (struct pardevice *dev, long inactivity);

DESCRIPTION
^^^^^^^^^^^

Set the inactivity timeout, in jiffies, for a registered device.  The
previous timeout is returned.

RETURN VALUE
^^^^^^^^^^^^

The previous timeout, in jiffies.

NOTES
^^^^^

Some of the port->ops functions for a parport may take time, owing to
delays at the peripheral.  After the peripheral has not responded for
``inactivity`` jiffies, a timeout will occur and the blocking function
will return.

A timeout of 0 jiffies is a special case: the function must do as much
as it can without blocking or leaving the hardware in an unknown
state.  If port operations are performed from within an interrupt
handler, for instance, a timeout of 0 jiffies should be used.

Once set for a registered device, the timeout will remain at the set
value until set again.

SEE ALSO
^^^^^^^^

port->ops->xxx_read/write_yyy

PORT FUNCTIONS
==============

The functions in the port->ops structure (struct parport_operations)
are provided by the low-level driver responsible for that port.

port->ops->read_data - read the data register
---------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		unsigned char (*read_data) (struct parport *port);
		...
	};

DESCRIPTION
^^^^^^^^^^^

If port->modes contains the PARPORT_MODE_TRISTATE flag and the
PARPORT_CONTROL_DIRECTION bit in the control register is set, this
returns the value on the data pins.  If port->modes contains the
PARPORT_MODE_TRISTATE flag and the PARPORT_CONTROL_DIRECTION bit is
not set, the return value _may_ be the last value written to the data
register.  Otherwise the return value is undefined.

SEE ALSO
^^^^^^^^

write_data, read_status, write_control

port->ops->write_data - write the data register
-----------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		void (*write_data) (struct parport *port, unsigned char d);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Writes to the data register.  May have side-effects (a STROBE pulse,
for instance).

SEE ALSO
^^^^^^^^

read_data, read_status, write_control

port->ops->read_status - read the status register
-------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		unsigned char (*read_status) (struct parport *port);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Reads from the status register.  This is a bitmask:

- PARPORT_STATUS_ERROR (printer fault, "nFault")
- PARPORT_STATUS_SELECT (on-line, "Select")
- PARPORT_STATUS_PAPEROUT (no paper, "PError")
- PARPORT_STATUS_ACK (handshake, "nAck")
- PARPORT_STATUS_BUSY (busy, "Busy")

There may be other bits set.

SEE ALSO
^^^^^^^^

read_data, write_data, write_control

port->ops->read_control - read the control register
---------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		unsigned char (*read_control) (struct parport *port);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Returns the last value written to the control register (either from
write_control or frob_control).  No port access is performed.

SEE ALSO
^^^^^^^^

read_data, write_data, read_status, write_control

port->ops->write_control - write the control register
-----------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		void (*write_control) (struct parport *port, unsigned char s);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Writes to the control register. This is a bitmask::

				  _______
	- PARPORT_CONTROL_STROBE (nStrobe)
				  _______
	- PARPORT_CONTROL_AUTOFD (nAutoFd)
				_____
	- PARPORT_CONTROL_INIT (nInit)
				  _________
	- PARPORT_CONTROL_SELECT (nSelectIn)

SEE ALSO
^^^^^^^^

read_data, write_data, read_status, frob_control

port->ops->frob_control - write control register bits
-----------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		unsigned char (*frob_control) (struct parport *port,
					unsigned char mask,
					unsigned char val);
		...
	};

DESCRIPTION
^^^^^^^^^^^

This is equivalent to reading from the control register, masking out
the bits in mask, exclusive-or'ing with the bits in val, and writing
the result to the control register.

As some ports don't allow reads from the control port, a software copy
of its contents is maintained, so frob_control is in fact only one
port access.

SEE ALSO
^^^^^^^^

read_data, write_data, read_status, write_control

port->ops->enable_irq - enable interrupt generation
---------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		void (*enable_irq) (struct parport *port);
		...
	};

DESCRIPTION
^^^^^^^^^^^

The parallel port hardware is instructed to generate interrupts at
appropriate moments, although those moments are
architecture-specific.  For the PC architecture, interrupts are
commonly generated on the rising edge of nAck.

SEE ALSO
^^^^^^^^

disable_irq

port->ops->disable_irq - disable interrupt generation
-----------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		void (*disable_irq) (struct parport *port);
		...
	};

DESCRIPTION
^^^^^^^^^^^

The parallel port hardware is instructed not to generate interrupts.
The interrupt itself is not masked.

SEE ALSO
^^^^^^^^

enable_irq

port->ops->data_forward - enable data drivers
---------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		void (*data_forward) (struct parport *port);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Enables the data line drivers, for 8-bit host-to-peripheral
communications.

SEE ALSO
^^^^^^^^

data_reverse

port->ops->data_reverse - tristate the buffer
---------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		void (*data_reverse) (struct parport *port);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Places the data bus in a high impedance state, if port->modes has the
PARPORT_MODE_TRISTATE bit set.

SEE ALSO
^^^^^^^^

data_forward

port->ops->epp_write_data - write EPP data
------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*epp_write_data) (struct parport *port, const void *buf,
					size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Writes data in EPP mode, and returns the number of bytes written.

The ``flags`` parameter may be one or more of the following,
bitwise-or'ed together:

======================= =================================================
PARPORT_EPP_FAST	Use fast transfers. Some chips provide 16-bit and
			32-bit registers.  However, if a transfer
			times out, the return value may be unreliable.
======================= =================================================

SEE ALSO
^^^^^^^^

epp_read_data, epp_write_addr, epp_read_addr

port->ops->epp_read_data - read EPP data
----------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*epp_read_data) (struct parport *port, void *buf,
					size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Reads data in EPP mode, and returns the number of bytes read.

The ``flags`` parameter may be one or more of the following,
bitwise-or'ed together:

======================= =================================================
PARPORT_EPP_FAST	Use fast transfers. Some chips provide 16-bit and
			32-bit registers.  However, if a transfer
			times out, the return value may be unreliable.
======================= =================================================

SEE ALSO
^^^^^^^^

epp_write_data, epp_write_addr, epp_read_addr

port->ops->epp_write_addr - write EPP address
---------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*epp_write_addr) (struct parport *port,
					const void *buf, size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Writes EPP addresses (8 bits each), and returns the number written.

The ``flags`` parameter may be one or more of the following,
bitwise-or'ed together:

======================= =================================================
PARPORT_EPP_FAST	Use fast transfers. Some chips provide 16-bit and
			32-bit registers.  However, if a transfer
			times out, the return value may be unreliable.
======================= =================================================

(Does PARPORT_EPP_FAST make sense for this function?)

SEE ALSO
^^^^^^^^

epp_write_data, epp_read_data, epp_read_addr

port->ops->epp_read_addr - read EPP address
-------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*epp_read_addr) (struct parport *port, void *buf,
					size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Reads EPP addresses (8 bits each), and returns the number read.

The ``flags`` parameter may be one or more of the following,
bitwise-or'ed together:

======================= =================================================
PARPORT_EPP_FAST	Use fast transfers. Some chips provide 16-bit and
			32-bit registers.  However, if a transfer
			times out, the return value may be unreliable.
======================= =================================================

(Does PARPORT_EPP_FAST make sense for this function?)

SEE ALSO
^^^^^^^^

epp_write_data, epp_read_data, epp_write_addr

port->ops->ecp_write_data - write a block of ECP data
-----------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*ecp_write_data) (struct parport *port,
					const void *buf, size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Writes a block of ECP data.  The ``flags`` parameter is ignored.

RETURN VALUE
^^^^^^^^^^^^

The number of bytes written.

SEE ALSO
^^^^^^^^

ecp_read_data, ecp_write_addr

port->ops->ecp_read_data - read a block of ECP data
---------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*ecp_read_data) (struct parport *port,
					void *buf, size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Reads a block of ECP data.  The ``flags`` parameter is ignored.

RETURN VALUE
^^^^^^^^^^^^

The number of bytes read.  NB. There may be more unread data in a
FIFO.  Is there a way of stunning the FIFO to prevent this?

SEE ALSO
^^^^^^^^

ecp_write_block, ecp_write_addr

port->ops->ecp_write_addr - write a block of ECP addresses
----------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*ecp_write_addr) (struct parport *port,
					const void *buf, size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Writes a block of ECP addresses.  The ``flags`` parameter is ignored.

RETURN VALUE
^^^^^^^^^^^^

The number of bytes written.

NOTES
^^^^^

This may use a FIFO, and if so shall not return until the FIFO is empty.

SEE ALSO
^^^^^^^^

ecp_read_data, ecp_write_data

port->ops->nibble_read_data - read a block of data in nibble mode
-----------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*nibble_read_data) (struct parport *port,
					void *buf, size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Reads a block of data in nibble mode.  The ``flags`` parameter is ignored.

RETURN VALUE
^^^^^^^^^^^^

The number of whole bytes read.

SEE ALSO
^^^^^^^^

byte_read_data, compat_write_data

port->ops->byte_read_data - read a block of data in byte mode
-------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*byte_read_data) (struct parport *port,
					void *buf, size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Reads a block of data in byte mode.  The ``flags`` parameter is ignored.

RETURN VALUE
^^^^^^^^^^^^

The number of bytes read.

SEE ALSO
^^^^^^^^

nibble_read_data, compat_write_data

port->ops->compat_write_data - write a block of data in compatibility mode
--------------------------------------------------------------------------

SYNOPSIS
^^^^^^^^

::

	#include <linux/parport.h>

	struct parport_operations {
		...
		size_t (*compat_write_data) (struct parport *port,
					const void *buf, size_t len, int flags);
		...
	};

DESCRIPTION
^^^^^^^^^^^

Writes a block of data in compatibility mode.  The ``flags`` parameter
is ignored.

RETURN VALUE
^^^^^^^^^^^^

The number of bytes written.

SEE ALSO
^^^^^^^^

nibble_read_data, byte_read_data


code:
.. SPDX-License-Identifier: GPL-2.0

============================================
The Linux PCI driver implementer's API guide
============================================

.. class:: toc-title

	   Table of contents

.. toctree::
   :maxdepth: 2

   pci
   p2pdma

.. only::  subproject and html

   Indices
   =======

   * :ref:`genindex`


code:
.. SPDX-License-Identifier: GPL-2.0

============================
PCI Peer-to-Peer DMA Support
============================

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.

One of the biggest issues is that PCI doesn't require forwarding
transactions between hierarchy domains, and in PCIe, each Root Port
defines a separate hierarchy domain. To make things worse, there is no
simple way to determine if a given Root Complex supports this or not.
(See PCIe r4.0, sec 1.3.1). Therefore, as of this writing, the kernel
only supports doing P2P when the endpoints involved are all behind the
same PCI bridge, as such devices are all in the same PCI hierarchy
domain, and the spec guarantees that all transactions within the
hierarchy will be routable, but it does not require routing
between hierarchies.

The second issue is that to make use of existing interfaces in Linux,
memory that is used for P2P transactions needs to be backed by struct
pages. However, PCI BARs are not typically cache coherent so there are
a few corner case gotchas with these pages so developers need to
be careful about what they do with them.

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.

Provider Drivers
----------------

A provider simply needs to register a BAR (or a portion of a BAR)
as a P2P DMA resource using :c:func:`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 :c:func:`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.

Client Drivers
--------------

A client driver typically only has to conditionally change its DMA map
routine to use the mapping function :c:func:`pci_p2pdma_map_sg()` instead
of the usual :c:func:`dma_map_sg()` function. Memory mapped in this
way does not need to be unmapped.

The client may also, optionally, make use of
:c:func:`is_pci_p2pdma_page()` to determine when to use the P2P mapping
functions and when to use the regular mapping functions. In some
situations, it may be more appropriate to use a flag to indicate a
given request is P2P memory and map appropriately. It is important to
ensure that struct pages that back P2P memory stay out of code that
does not have support for them as other code may treat the pages as
regular memory which may not be appropriate.

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
:c:func:`pci_p2pdma_distance()` otherwise it may find a memory provider
that's compatible with all clients using  :c:func:`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
:c:func:`pci_alloc_p2pmem()` and :c:func:`pci_free_p2pmem()` to
allocate P2P memory from the provider. :c:func:`pci_p2pmem_alloc_sgl()`
and :c:func:`pci_p2pmem_free_sgl()` are convenience functions for
allocating scatter-gather lists with P2P memory.

Struct Page Caveats
-------------------

Driver writers should be very careful about not passing these special
struct pages to code that isn't prepared for it. At this time, the kernel
interfaces do not have any checks for ensuring this. This obviously
precludes passing these pages to userspace.

P2P memory is also technically IO memory but should never have any side
effects behind it. Thus, the order of loads and stores should not be important
and ioreadX(), iowriteX() and friends should not be necessary.

P2P DMA Support Library
=======================

.. kernel-doc:: drivers/pci/p2pdma.c
   :export:


code:
PCI Support Library
-------------------

.. kernel-doc:: drivers/pci/pci.c
   :export:

.. kernel-doc:: drivers/pci/pci-driver.c
   :export:

.. kernel-doc:: drivers/pci/remove.c
   :export:

.. kernel-doc:: drivers/pci/search.c
   :export:

.. kernel-doc:: drivers/pci/msi/msi.c
   :export:

.. kernel-doc:: drivers/pci/bus.c
   :export:

.. kernel-doc:: drivers/pci/access.c
   :export:

.. kernel-doc:: drivers/pci/irq.c
   :export:

.. kernel-doc:: drivers/pci/probe.c
   :export:

.. kernel-doc:: drivers/pci/slot.c
   :export:

.. kernel-doc:: drivers/pci/rom.c
   :export:

.. kernel-doc:: drivers/pci/iov.c
   :export:

.. kernel-doc:: drivers/pci/pci-sysfs.c
   :internal:

PCI Hotplug Support Library
---------------------------

.. kernel-doc:: drivers/pci/hotplug/pci_hotplug_core.c
   :export:


code:
.. SPDX-License-Identifier: GPL-2.0

=====================
Generic PHY Framework
=====================

.. toctree::

   phy
   samsung-usb2

.. only::  subproject and html

   Indices
   =======

   * :ref:`genindex`