4 * Copyright (c) Intel Corporation. All rights reserved.
5 * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * NVMe-oF Target transport plugin API
38 #ifndef SPDK_NVMF_TRANSPORT_H_
39 #define SPDK_NVMF_TRANSPORT_H_
41 #include "spdk/bdev.h"
42 #include "spdk/nvme_spec.h"
43 #include "spdk/nvmf.h"
44 #include "spdk/nvmf_cmd.h"
45 #include "spdk/nvmf_spec.h"
46 #include "spdk/memory.h"
48 #define SPDK_NVMF_MAX_SGL_ENTRIES 16
50 /* The maximum number of buffers per request */
51 #define NVMF_REQ_MAX_BUFFERS (SPDK_NVMF_MAX_SGL_ENTRIES * 2)
53 /* AIO backend requires block size aligned data buffers,
54 * extra 4KiB aligned data buffer should work for most devices.
56 #define NVMF_DATA_BUFFER_ALIGNMENT VALUE_4KB
57 #define NVMF_DATA_BUFFER_MASK (NVMF_DATA_BUFFER_ALIGNMENT - 1LL)
60 struct spdk_nvmf_capsule_cmd nvmf_cmd
;
61 struct spdk_nvme_cmd nvme_cmd
;
62 struct spdk_nvmf_fabric_prop_set_cmd prop_set_cmd
;
63 struct spdk_nvmf_fabric_prop_get_cmd prop_get_cmd
;
64 struct spdk_nvmf_fabric_connect_cmd connect_cmd
;
66 SPDK_STATIC_ASSERT(sizeof(union nvmf_h2c_msg
) == 64, "Incorrect size");
69 struct spdk_nvme_cpl nvme_cpl
;
70 struct spdk_nvmf_fabric_prop_get_rsp prop_get_rsp
;
71 struct spdk_nvmf_fabric_connect_rsp connect_rsp
;
73 SPDK_STATIC_ASSERT(sizeof(union nvmf_c2h_msg
) == 16, "Incorrect size");
75 struct spdk_nvmf_dif_info
{
76 struct spdk_dif_ctx dif_ctx
;
77 bool dif_insert_or_strip
;
82 struct spdk_nvmf_request
{
83 struct spdk_nvmf_qpair
*qpair
;
85 enum spdk_nvme_data_transfer xfer
;
87 union nvmf_h2c_msg
*cmd
;
88 union nvmf_c2h_msg
*rsp
;
89 void *buffers
[NVMF_REQ_MAX_BUFFERS
];
90 struct iovec iov
[NVMF_REQ_MAX_BUFFERS
];
93 struct spdk_bdev_io_wait_entry bdev_io_wait
;
94 struct spdk_nvmf_dif_info dif
;
95 spdk_nvmf_nvme_passthru_cmd_cb cmd_cb_fn
;
96 struct spdk_nvmf_request
*first_fused_req
;
97 struct spdk_nvmf_request
*req_to_abort
;
98 struct spdk_poller
*poller
;
101 STAILQ_ENTRY(spdk_nvmf_request
) buf_link
;
102 TAILQ_ENTRY(spdk_nvmf_request
) link
;
105 enum spdk_nvmf_qpair_state
{
106 SPDK_NVMF_QPAIR_UNINITIALIZED
= 0,
107 SPDK_NVMF_QPAIR_ACTIVE
,
108 SPDK_NVMF_QPAIR_DEACTIVATING
,
109 SPDK_NVMF_QPAIR_ERROR
,
112 typedef void (*spdk_nvmf_state_change_done
)(void *cb_arg
, int status
);
114 struct spdk_nvmf_qpair
{
115 enum spdk_nvmf_qpair_state state
;
116 spdk_nvmf_state_change_done state_cb
;
119 struct spdk_nvmf_transport
*transport
;
120 struct spdk_nvmf_ctrlr
*ctrlr
;
121 struct spdk_nvmf_poll_group
*group
;
125 uint16_t sq_head_max
;
127 struct spdk_nvmf_request
*first_fused_req
;
129 TAILQ_HEAD(, spdk_nvmf_request
) outstanding
;
130 TAILQ_ENTRY(spdk_nvmf_qpair
) link
;
133 struct spdk_nvmf_transport_pg_cache_buf
{
134 STAILQ_ENTRY(spdk_nvmf_transport_pg_cache_buf
) link
;
137 struct spdk_nvmf_transport_poll_group
{
138 struct spdk_nvmf_transport
*transport
;
139 /* Requests that are waiting to obtain a data buffer */
140 STAILQ_HEAD(, spdk_nvmf_request
) pending_buf_queue
;
141 STAILQ_HEAD(, spdk_nvmf_transport_pg_cache_buf
) buf_cache
;
142 uint32_t buf_cache_count
;
143 uint32_t buf_cache_size
;
144 struct spdk_nvmf_poll_group
*group
;
145 TAILQ_ENTRY(spdk_nvmf_transport_poll_group
) link
;
148 struct spdk_nvmf_poll_group
{
149 struct spdk_thread
*thread
;
150 struct spdk_poller
*poller
;
152 TAILQ_HEAD(, spdk_nvmf_transport_poll_group
) tgroups
;
154 /* Array of poll groups indexed by subsystem id (sid) */
155 struct spdk_nvmf_subsystem_poll_group
*sgroups
;
156 uint32_t num_sgroups
;
158 /* All of the queue pairs that belong to this poll group */
159 TAILQ_HEAD(, spdk_nvmf_qpair
) qpairs
;
162 struct spdk_nvmf_poll_group_stat stat
;
164 spdk_nvmf_poll_group_destroy_done_fn destroy_cb_fn
;
165 void *destroy_cb_arg
;
167 TAILQ_ENTRY(spdk_nvmf_poll_group
) link
;
170 struct spdk_nvmf_listener
{
171 struct spdk_nvme_transport_id trid
;
174 TAILQ_ENTRY(spdk_nvmf_listener
) link
;
178 * A subset of struct spdk_nvme_ctrlr_data that are emulated by a fabrics device.
180 struct spdk_nvmf_ctrlr_data
{
182 struct spdk_nvme_cdata_sgls sgls
;
183 struct spdk_nvme_cdata_nvmf_specific nvmf_specific
;
186 struct spdk_nvmf_transport
{
187 struct spdk_nvmf_tgt
*tgt
;
188 const struct spdk_nvmf_transport_ops
*ops
;
189 struct spdk_nvmf_transport_opts opts
;
191 /* A mempool for transport related data transfers */
192 struct spdk_mempool
*data_buf_pool
;
194 TAILQ_HEAD(, spdk_nvmf_listener
) listeners
;
195 TAILQ_ENTRY(spdk_nvmf_transport
) link
;
198 struct spdk_nvmf_transport_ops
{
202 char name
[SPDK_NVMF_TRSTRING_MAX_LEN
];
207 enum spdk_nvme_transport_type type
;
210 * Initialize transport options to default value
212 void (*opts_init
)(struct spdk_nvmf_transport_opts
*opts
);
215 * Create a transport for the given transport opts
217 struct spdk_nvmf_transport
*(*create
)(struct spdk_nvmf_transport_opts
*opts
);
220 * Destroy the transport
222 int (*destroy
)(struct spdk_nvmf_transport
*transport
);
225 * Instruct the transport to accept new connections at the address
226 * provided. This may be called multiple times.
228 int (*listen
)(struct spdk_nvmf_transport
*transport
,
229 const struct spdk_nvme_transport_id
*trid
);
232 * Stop accepting new connections at the given address.
234 void (*stop_listen
)(struct spdk_nvmf_transport
*transport
,
235 const struct spdk_nvme_transport_id
*trid
);
238 * A listener has been associated with a subsystem with the given NQN.
239 * This is only a notification. Most transports will not need to take any
240 * action here, as the enforcement of the association is done in the generic
243 * The association is not considered complete until cb_fn is called. New
244 * connections on the listener targeting this subsystem will be rejected
247 * Pass a negated errno code to `cb_fn` to block the association. 0 to allow.
249 void (*listen_associate
)(struct spdk_nvmf_transport
*transport
,
250 const struct spdk_nvmf_subsystem
*subsystem
,
251 const struct spdk_nvme_transport_id
*trid
,
252 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn
,
256 * Check for new connections on the transport.
258 uint32_t (*accept
)(struct spdk_nvmf_transport
*transport
);
261 * Initialize subset of identify controller data.
263 void (*cdata_init
)(struct spdk_nvmf_transport
*transport
, struct spdk_nvmf_subsystem
*subsystem
,
264 struct spdk_nvmf_ctrlr_data
*cdata
);
267 * Fill out a discovery log entry for a specific listen address.
269 void (*listener_discover
)(struct spdk_nvmf_transport
*transport
,
270 struct spdk_nvme_transport_id
*trid
,
271 struct spdk_nvmf_discovery_log_page_entry
*entry
);
274 * Create a new poll group
276 struct spdk_nvmf_transport_poll_group
*(*poll_group_create
)(struct spdk_nvmf_transport
*transport
);
279 * Get the polling group of the queue pair optimal for the specific transport
281 struct spdk_nvmf_transport_poll_group
*(*get_optimal_poll_group
)(struct spdk_nvmf_qpair
*qpair
);
284 * Destroy a poll group
286 void (*poll_group_destroy
)(struct spdk_nvmf_transport_poll_group
*group
);
289 * Add a qpair to a poll group
291 int (*poll_group_add
)(struct spdk_nvmf_transport_poll_group
*group
,
292 struct spdk_nvmf_qpair
*qpair
);
295 * Remove a qpair from a poll group
297 int (*poll_group_remove
)(struct spdk_nvmf_transport_poll_group
*group
,
298 struct spdk_nvmf_qpair
*qpair
);
301 * Poll the group to process I/O
303 int (*poll_group_poll
)(struct spdk_nvmf_transport_poll_group
*group
);
306 * Free the request without sending a response
307 * to the originator. Release memory tied to this request.
309 int (*req_free
)(struct spdk_nvmf_request
*req
);
312 * Signal request completion, which sends a response
315 int (*req_complete
)(struct spdk_nvmf_request
*req
);
318 * Deinitialize a connection.
320 void (*qpair_fini
)(struct spdk_nvmf_qpair
*qpair
);
323 * Get the peer transport ID for the queue pair.
325 int (*qpair_get_peer_trid
)(struct spdk_nvmf_qpair
*qpair
,
326 struct spdk_nvme_transport_id
*trid
);
329 * Get the local transport ID for the queue pair.
331 int (*qpair_get_local_trid
)(struct spdk_nvmf_qpair
*qpair
,
332 struct spdk_nvme_transport_id
*trid
);
335 * Get the listener transport ID that accepted this qpair originally.
337 int (*qpair_get_listen_trid
)(struct spdk_nvmf_qpair
*qpair
,
338 struct spdk_nvme_transport_id
*trid
);
341 * Abort the request which the abort request specifies.
342 * This function can complete synchronously or asynchronously, but
343 * is expected to call spdk_nvmf_request_complete() in the end
346 void (*qpair_abort_request
)(struct spdk_nvmf_qpair
*qpair
,
347 struct spdk_nvmf_request
*req
);
350 * Get transport poll group statistics
352 int (*poll_group_get_stat
)(struct spdk_nvmf_tgt
*tgt
,
353 struct spdk_nvmf_transport_poll_group_stat
**stat
);
356 * Free transport poll group statistics previously allocated with poll_group_get_stat()
358 void (*poll_group_free_stat
)(struct spdk_nvmf_transport_poll_group_stat
*stat
);
362 * Register the operations for a given transport type.
364 * This function should be invoked by referencing the macro
365 * SPDK_NVMF_TRANSPORT_REGISTER macro in the transport's .c file.
367 * \param ops The operations associated with an NVMe-oF transport.
369 void spdk_nvmf_transport_register(const struct spdk_nvmf_transport_ops
*ops
);
371 int spdk_nvmf_ctrlr_connect(struct spdk_nvmf_request
*req
);
374 * Function to be called for each newly discovered qpair.
376 * \param tgt The nvmf target
377 * \param qpair The newly discovered qpair.
379 void spdk_nvmf_tgt_new_qpair(struct spdk_nvmf_tgt
*tgt
, struct spdk_nvmf_qpair
*qpair
);
382 * A subset of struct spdk_nvme_registers that are emulated by a fabrics device.
384 struct spdk_nvmf_registers
{
385 union spdk_nvme_cap_register cap
;
386 union spdk_nvme_vs_register vs
;
387 union spdk_nvme_cc_register cc
;
388 union spdk_nvme_csts_register csts
;
389 union spdk_nvme_aqa_register aqa
;
394 const struct spdk_nvmf_registers
*spdk_nvmf_ctrlr_get_regs(struct spdk_nvmf_ctrlr
*ctrlr
);
396 void spdk_nvmf_request_free_buffers(struct spdk_nvmf_request
*req
,
397 struct spdk_nvmf_transport_poll_group
*group
,
398 struct spdk_nvmf_transport
*transport
);
399 int spdk_nvmf_request_get_buffers(struct spdk_nvmf_request
*req
,
400 struct spdk_nvmf_transport_poll_group
*group
,
401 struct spdk_nvmf_transport
*transport
,
403 int spdk_nvmf_request_get_buffers_multi(struct spdk_nvmf_request
*req
,
404 struct spdk_nvmf_transport_poll_group
*group
,
405 struct spdk_nvmf_transport
*transport
,
406 uint32_t *lengths
, uint32_t num_lengths
);
408 bool spdk_nvmf_request_get_dif_ctx(struct spdk_nvmf_request
*req
, struct spdk_dif_ctx
*dif_ctx
);
410 void spdk_nvmf_request_exec(struct spdk_nvmf_request
*req
);
411 void spdk_nvmf_request_exec_fabrics(struct spdk_nvmf_request
*req
);
412 int spdk_nvmf_request_free(struct spdk_nvmf_request
*req
);
413 int spdk_nvmf_request_complete(struct spdk_nvmf_request
*req
);
416 * Remove the given qpair from the poll group.
418 * \param qpair The qpair to remove.
420 void spdk_nvmf_poll_group_remove(struct spdk_nvmf_qpair
*qpair
);
423 * Get the NVMe-oF subsystem associated with this controller.
425 * \param ctrlr The NVMe-oF controller
427 * \return The NVMe-oF subsystem
429 struct spdk_nvmf_subsystem
*
430 spdk_nvmf_ctrlr_get_subsystem(struct spdk_nvmf_ctrlr
*ctrlr
);
433 * Get the NVMe-oF controller ID.
435 * \param ctrlr The NVMe-oF controller
437 * \return The NVMe-oF controller ID
440 spdk_nvmf_ctrlr_get_id(struct spdk_nvmf_ctrlr
*ctrlr
);
442 static inline enum spdk_nvme_data_transfer
443 spdk_nvmf_req_get_xfer(struct spdk_nvmf_request
*req
) {
444 enum spdk_nvme_data_transfer xfer
;
445 struct spdk_nvme_cmd
*cmd
= &req
->cmd
->nvme_cmd
;
446 struct spdk_nvme_sgl_descriptor
*sgl
= &cmd
->dptr
.sgl1
;
448 /* Figure out data transfer direction */
449 if (cmd
->opc
== SPDK_NVME_OPC_FABRIC
)
451 xfer
= spdk_nvme_opc_get_data_transfer(req
->cmd
->nvmf_cmd
.fctype
);
454 xfer
= spdk_nvme_opc_get_data_transfer(cmd
->opc
);
457 if (xfer
== SPDK_NVME_DATA_NONE
)
462 /* Even for commands that may transfer data, they could have specified 0 length.
463 * We want those to show up with xfer SPDK_NVME_DATA_NONE.
465 switch (sgl
->generic
.type
)
467 case SPDK_NVME_SGL_TYPE_DATA_BLOCK
:
468 case SPDK_NVME_SGL_TYPE_BIT_BUCKET
:
469 case SPDK_NVME_SGL_TYPE_SEGMENT
:
470 case SPDK_NVME_SGL_TYPE_LAST_SEGMENT
:
471 case SPDK_NVME_SGL_TYPE_TRANSPORT_DATA_BLOCK
:
472 if (sgl
->unkeyed
.length
== 0) {
473 xfer
= SPDK_NVME_DATA_NONE
;
476 case SPDK_NVME_SGL_TYPE_KEYED_DATA_BLOCK
:
477 if (sgl
->keyed
.length
== 0) {
478 xfer
= SPDK_NVME_DATA_NONE
;
487 * Macro used to register new transports.
489 #define SPDK_NVMF_TRANSPORT_REGISTER(name, transport_ops) \
490 static void __attribute__((constructor)) _spdk_nvmf_transport_register_##name(void) \
492 spdk_nvmf_transport_register(transport_ops); \