libxnvme_queue.h#

Enums#

xnvme_queue_opts#

enum xnvme_queue_opts#

Command Queue initialization options.

Values:

enumerator XNVME_QUEUE_IOPOLL#

XNVME_QUEUE_IOPOLL: queue. is polled for completions.

enumerator XNVME_QUEUE_SQPOLL#

XNVME_QUEUE_SQPOLL: queue. is polled for submissions XNVME_QUEUE_P2P_CQ_MIRROR: completions land in GPU memory, mirrored to the host.

enumerator XNVME_QUEUE_P2P_CQ_MIRROR#
enumerator XNVME_QUEUE_SQ_HOSTMEM#

XNVME_QUEUE_SQ_HOSTMEM: a GPU-issued queue submits from host memory.

enumerator XNVME_QUEUE_REAP#

XNVME_QUEUE_REAP: completions are reaped with xnvme_queue_reap(), no callback is called.

Functions#

xnvme_queue_drain#

int xnvme_queue_drain(struct xnvme_queue *queue)#

Process outstanding commands on the given xnvme_queue until it is empty.

Calls xnvme_queue_poke() until no command is outstanding. On a queue initialized with XNVME_QUEUE_REAP it returns -EINVAL, as xnvme_queue_poke() does.

Parameters:
  • queue – Pointer to the xnvme_queue to wait/process commands on

Returns:

On success, number of commands processed, may be 0. On error, negative errno is returned.

xnvme_queue_get_capacity#

uint32_t xnvme_queue_get_capacity(struct xnvme_queue *queue)#

Get the capacity of the given xnvme_queue.

Parameters:
  • queue – Pointer to the xnvme_queue to query for capacity

Returns:

On success, capacity of given xnvme_queue text is returned. On error, 0 is returned e.g. errors are silent

xnvme_queue_get_cmd_ctx#

struct xnvme_cmd_ctx *xnvme_queue_get_cmd_ctx(struct xnvme_queue *queue)#

Retrieve a command-context from the given queue for async.

command execution with the queue

Note

The command-context is managed by the queue, thus, return it to the queue via xnvme_queue_put_cmd_ctx

Note

This is not thread-safe

Parameters:
  • queue – Pointer to the xnvme_queue to retrieve a command-context for

Returns:

On success, a command-context is returned. On error, NULL is returned and errno is set to indicate the error.

xnvme_queue_get_completion_fd#

int xnvme_queue_get_completion_fd(struct xnvme_queue *queue)#

Get the completion event fd on the given xnvme_queue.

Parameters:
  • queue – Pointer to the xnvme_queue to query for outstanding commands

Returns:

On success, an eventfd() file descriptor is returned. On error, negative errno is returned.

xnvme_queue_get_outstanding#

uint32_t xnvme_queue_get_outstanding(struct xnvme_queue *queue)#

Get the number of outstanding commands on the given xnvme_queue.

Parameters:
  • queue – Pointer to the xnvme_queue to query for outstanding commands

Returns:

On success, number of outstanding commands are returned. On error, 0 is returned e.g. errors are silent

xnvme_queue_init#

int xnvme_queue_init(struct xnvme_dev *dev, uint16_t capacity, int opts, struct xnvme_queue **queue)#

Allocate a Command Queue for asynchronous command submission and completion.

How the queue delivers completions is fixed here. With XNVME_QUEUE_REAP in ‘opts’, completed commands are retrieved with xnvme_queue_reap(). Without it, xnvme_queue_poke() calls the callback assigned with xnvme_queue_set_cb() for each of them.

Note

Control plane: serialised with the other control-plane calls, not with I/O on the same device, queue or buffer; see Thread Safety in the API documentation

Parameters:
  • dev – Device handle (xnvme_dev) obtained with xnvme_dev_open()

  • capacity – Maximum number of outstanding commands on the initialized queue, note that it must be a power of 2 within the range [1,2048]

  • opts – Queue options, see xnvme_queue_opts

  • queue – [out] Pointer-pointer to the xnvme_queue to initialize

Returns:

On success, 0 is returned. On error, negative errno is returned.

xnvme_queue_poke#

int xnvme_queue_poke(struct xnvme_queue *queue, uint32_t max)#

Process completions of commands on the given xnvme_queue.

Calls the callback of each completed command, see xnvme_queue_set_cb(). Set ‘max’ to limit number of completions, 0 means no max. On a queue initialized with XNVME_QUEUE_REAP it returns -EINVAL.

Parameters:
  • queue – Pointer to the xnvme_queue to poke for completions

  • max – The max number of completions to complete

Returns:

On success, number of completions processed, may be 0. On error, negative errno is returned.

xnvme_queue_put_cmd_ctx#

int xnvme_queue_put_cmd_ctx(struct xnvme_queue *queue, struct xnvme_cmd_ctx *ctx)#

Hand back a command-context previously retrieve using xnvme_queue_get_cmd_ctx.

Note

This function is not thread-safe

Parameters:
  • queue – Pointer to the xnvme_queue to hand back the command-context to

  • ctx – Pointer to command context (xnvme_cmd_ctx)

Returns:

On success, 0 is returned. On error, negative errno is returned.

xnvme_queue_reap#

int xnvme_queue_reap(struct xnvme_queue *queue, struct xnvme_cmd_ctx **completions, uint32_t max, uint32_t *ncompleted)#

Retrieve completed commands of the given xnvme_queue.

Stores a pointer to the command-context of each completed command in ‘completions’, up to ‘max’ of them, and the number it stored in ‘ncompleted’, which is 0 when none have completed. Each command-context carries the completion in cpl and the pointer assigned with xnvme_cmd_ctx_set_data(), and is returned to the queue with xnvme_queue_put_cmd_ctx() or submitted again. A command that failed is a completion like any other, with its status in cpl.

‘ncompleted’ is assigned on every return, and the command-contexts it counts are handled as those of a successful call, also when an error is returned. An error reports the state of the queue, never the status of a command. Three errors leave the queue intact, and the caller calls xnvme_queue_reap() again:

  • -EINTR: a signal interrupted the call

  • -EAGAIN: the backend lacked the resources to submit or retrieve at the time

  • -EBUSY: the device is busy, such as a controller being reset

Any other error means the queue failed, and the queue is torn down with xnvme_queue_term(). The commands not handed back by then are indeterminate: they may or may not have been carried out.

Only a queue initialized with XNVME_QUEUE_REAP delivers completions this way, and no callback is called on such a queue. For any other queue, and for a ‘max’ of 0, xnvme_queue_reap() returns -EINVAL with ‘ncompleted’ 0.

Parameters:
  • queue – Pointer to the xnvme_queue to retrieve completed commands of

  • completions – [out] Array of at least ‘max’ pointers, receiving the command-contexts

  • max – The most command-contexts to retrieve; must be above zero

  • ncompleted – [out] The number of command-contexts stored in ‘completions’, may be 0; assigned on every return

Returns:

On success, 0 is returned. On error, negative errno is returned: -EINTR, -EAGAIN or -EBUSY when the queue is intact and the call is to be repeated, -EINVAL for a queue without XNVME_QUEUE_REAP or a ‘max’ of 0, and any other when the queue failed.

xnvme_queue_set_cb#

int xnvme_queue_set_cb(struct xnvme_queue *queue, xnvme_queue_cb cb, void *cb_arg)#

Assign a callback-function and argument to be used with the xnvme_cmd_ctx of the queue.

The callback is kept in every command context of the queue, and invoked from xnvme_queue_poke() and xnvme_queue_drain() for each command completed, until the queue is terminated or the callback is assigned again. On a queue initialized with XNVME_QUEUE_REAP it returns -EINVAL, as no callback is called on such a queue.

Parameters:
  • queue – The xnvme_queue to assign default callback function for

  • cb – The callback function to use

  • cb_arg – The callback argument to use

Returns:

On success, 0 is returned. On error, negative errno is returned.

xnvme_queue_term#

int xnvme_queue_term(struct xnvme_queue *queue)#

Tear down the given xnvme_queue.

Note

Control plane: serialised with the other control-plane calls, not with I/O on the same device, queue or buffer; see Thread Safety in the API documentation

Parameters:
  • queue – Pointer to the xnvme_queue to tear down

Returns:

On success, 0 is returned. On error, negative errno is returned.

Header#

/**
 * SPDX-FileCopyrightText: Samsung Electronics Co., Ltd
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * @file libxnvme_queue.h
 */

/**
 * Opaque Command Queue Handle to be initialized by xnvme_queue_init()
 *
 * @see xnvme_queue_init
 * @see xnvme_queue_term
 *
 * @struct xnvme_queue
 */
struct xnvme_queue;

struct xnvme_cmd_ctx;

/**
 * Command Queue initialization options
 *
 * @enum xnvme_queue_opts
 */
enum xnvme_queue_opts {
	XNVME_QUEUE_IOPOLL = 0x1,      ///< XNVME_QUEUE_IOPOLL: queue. is polled for completions
	XNVME_QUEUE_SQPOLL = 0x1 << 1, ///< XNVME_QUEUE_SQPOLL: queue. is polled for submissions
	/// XNVME_QUEUE_P2P_CQ_MIRROR: completions land in GPU memory, mirrored to the host
	XNVME_QUEUE_P2P_CQ_MIRROR = 0x1 << 2,
	/// XNVME_QUEUE_SQ_HOSTMEM: a GPU-issued queue submits from host memory
	XNVME_QUEUE_SQ_HOSTMEM = 0x1 << 3,
	/// XNVME_QUEUE_REAP: completions are reaped with xnvme_queue_reap(), no callback is called
	XNVME_QUEUE_REAP = 0x1 << 4,
};

/**
 * Allocate a Command Queue for asynchronous command submission and completion
 *
 * How the queue delivers completions is fixed here. With ::XNVME_QUEUE_REAP in 'opts', completed
 * commands are retrieved with xnvme_queue_reap(). Without it, xnvme_queue_poke() calls the
 * callback assigned with xnvme_queue_set_cb() for each of them.
 *
 * @note Control plane: serialised with the other control-plane calls, not with I/O on the
 * same device, queue or buffer; see Thread Safety in the API documentation
 *
 * @param dev Device handle (::xnvme_dev) obtained with xnvme_dev_open()
 * @param capacity Maximum number of outstanding commands on the initialized queue, note that it
 * must be a power of 2 within the range [1,2048]
 * @param opts Queue options, see ::xnvme_queue_opts
 * @param[out] queue Pointer-pointer to the ::xnvme_queue to initialize
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 */
int
xnvme_queue_init(struct xnvme_dev *dev, uint16_t capacity, int opts, struct xnvme_queue **queue);

/**
 * Get the capacity of the given ::xnvme_queue
 *
 * @param queue Pointer to the ::xnvme_queue to query for capacity
 *
 * @return On success, capacity of given ::xnvme_queue text is returned. On error, 0 is returned
 * e.g. errors are silent
 */
uint32_t
xnvme_queue_get_capacity(struct xnvme_queue *queue);

/**
 * Get the number of outstanding commands on the given ::xnvme_queue
 *
 * @param queue Pointer to the ::xnvme_queue to query for outstanding commands
 *
 * @return On success, number of outstanding commands are returned. On error, 0 is returned e.g.
 * errors are silent
 */
uint32_t
xnvme_queue_get_outstanding(struct xnvme_queue *queue);

/**
 * Tear down the given ::xnvme_queue
 *
 * @note Control plane: serialised with the other control-plane calls, not with I/O on the
 * same device, queue or buffer; see Thread Safety in the API documentation
 *
 * @param queue Pointer to the ::xnvme_queue to tear down
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 *
 * @destroys queue
 */
int
xnvme_queue_term(struct xnvme_queue *queue);

/**
 * Process completions of commands on the given ::xnvme_queue
 *
 * Calls the callback of each completed command, see xnvme_queue_set_cb(). Set 'max' to limit
 * number of completions, 0 means no max. On a queue initialized with ::XNVME_QUEUE_REAP it
 * returns -EINVAL.
 *
 * @param queue Pointer to the ::xnvme_queue to poke for completions
 * @param max The max number of completions to complete
 *
 * @return On success, number of completions processed, may be 0. On error, negative `errno` is
 * returned.
 */
int
xnvme_queue_poke(struct xnvme_queue *queue, uint32_t max);

/**
 * Process outstanding commands on the given ::xnvme_queue until it is empty
 *
 * Calls xnvme_queue_poke() until no command is outstanding. On a queue initialized with
 * ::XNVME_QUEUE_REAP it returns -EINVAL, as xnvme_queue_poke() does.
 *
 * @param queue Pointer to the ::xnvme_queue to wait/process commands on
 *
 * @return On success, number of commands processed, may be 0. On error, negative `errno` is
 * returned.
 */
int
xnvme_queue_drain(struct xnvme_queue *queue);

/**
 * Retrieve completed commands of the given ::xnvme_queue
 *
 * Stores a pointer to the command-context of each completed command in 'completions', up to
 * 'max' of them, and the number it stored in 'ncompleted', which is 0 when none have completed.
 * Each command-context carries the completion in `cpl` and the pointer assigned with
 * xnvme_cmd_ctx_set_data(), and is returned to the queue with xnvme_queue_put_cmd_ctx() or
 * submitted again. A command that failed is a completion like any other, with its status in
 * `cpl`.
 *
 * 'ncompleted' is assigned on every return, and the command-contexts it counts are handled as
 * those of a successful call, also when an error is returned. An error reports the state of the
 * queue, never the status of a command. Three errors leave the queue intact, and the caller
 * calls xnvme_queue_reap() again:
 *
 * - -EINTR: a signal interrupted the call
 * - -EAGAIN: the backend lacked the resources to submit or retrieve at the time
 * - -EBUSY: the device is busy, such as a controller being reset
 *
 * Any other error means the queue failed, and the queue is torn down with xnvme_queue_term().
 * The commands not handed back by then are indeterminate: they may or may not have been carried
 * out.
 *
 * Only a queue initialized with ::XNVME_QUEUE_REAP delivers completions this way, and no
 * callback is called on such a queue. For any other queue, and for a 'max' of 0,
 * xnvme_queue_reap() returns -EINVAL with 'ncompleted' 0.
 *
 * @param queue Pointer to the ::xnvme_queue to retrieve completed commands of
 * @param[out] completions Array of at least 'max' pointers, receiving the command-contexts
 * @param max The most command-contexts to retrieve; must be above zero
 * @param[out] ncompleted The number of command-contexts stored in 'completions', may be 0;
 * assigned on every return
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned: -EINTR, -EAGAIN
 * or -EBUSY when the queue is intact and the call is to be repeated, -EINVAL for a queue
 * without ::XNVME_QUEUE_REAP or a 'max' of 0, and any other when the queue failed.
 *
 * @len completions max
 * @len completions ncompleted
 * @errno partial
 */
int
xnvme_queue_reap(struct xnvme_queue *queue, struct xnvme_cmd_ctx **completions, uint32_t max,
		 uint32_t *ncompleted);

/**
 * Retrieve a command-context from the given queue for async. command execution with the queue
 *
 * @note The command-context is managed by the queue, thus, return it to the queue via
 * ::xnvme_queue_put_cmd_ctx
 *
 * @note This is not thread-safe
 *
 * @param queue Pointer to the ::xnvme_queue to retrieve a command-context for
 *
 * @return On success, a command-context is returned. On error, NULL is returned and `errno` is set
 * to indicate the error.
 */
struct xnvme_cmd_ctx *
xnvme_queue_get_cmd_ctx(struct xnvme_queue *queue);

/**
 * Hand back a command-context previously retrieve using ::xnvme_queue_get_cmd_ctx
 *
 * @note This function is not thread-safe
 *
 * @param queue Pointer to the ::xnvme_queue to hand back the command-context to
 * @param ctx Pointer to command context (::xnvme_cmd_ctx)
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 */
int
xnvme_queue_put_cmd_ctx(struct xnvme_queue *queue, struct xnvme_cmd_ctx *ctx);

/**
 * Signature of function used with Command Queues for async. callback upon command-completion
 */
typedef void (*xnvme_queue_cb)(struct xnvme_cmd_ctx *ctx, void *opaque);

/**
 * Assign a callback-function and argument to be used with the ::xnvme_cmd_ctx of the queue
 *
 * The callback is kept in every command context of the queue, and invoked from
 * xnvme_queue_poke() and xnvme_queue_drain() for each command completed, until the queue is
 * terminated or the callback is assigned again. On a queue initialized with
 * ::XNVME_QUEUE_REAP it returns -EINVAL, as no callback is called on such a queue.
 *
 * @param queue The ::xnvme_queue to assign default callback function for
 * @param cb The callback function to use
 * @param cb_arg The callback argument to use
 *
 * @return On success, 0 is returned. On error, negative `errno` is returned.
 *
 * @lifetime cb handle
 */
int
xnvme_queue_set_cb(struct xnvme_queue *queue, xnvme_queue_cb cb, void *cb_arg);

/**
 * Get the completion event fd on the given ::xnvme_queue
 *
 * @param queue Pointer to the ::xnvme_queue to query for outstanding commands
 *
 * @return On success, an eventfd() file descriptor is returned. On error, negative `errno`
 * is returned.
 */
int
xnvme_queue_get_completion_fd(struct xnvme_queue *queue);