2 * Copyright (c) 2016-2017 Alexander Motin <mav@FreeBSD.org>
3 * Copyright (C) 2013 Intel Corporation
4 * Copyright (C) 2015 EMC Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * The Non-Transparent Bridge (NTB) is a device that allows you to connect
31 * two or more systems using a PCI-e links, providing remote memory access.
33 * This module contains a transport for sending and receiving messages by
34 * writing to remote memory window(s) provided by underlying NTB device.
36 * NOTE: Much of the code in this module is shared with Linux. Any patches may
37 * be picked up and redistributed in Linux with a dual GPL/BSD license.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
48 #include <sys/limits.h>
50 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/queue.h>
55 #include <sys/sysctl.h>
56 #include <sys/taskqueue.h>
61 #include <machine/bus.h>
64 #include "ntb_transport.h"
66 #define KTR_NTB KTR_SPARE3
68 #define NTB_TRANSPORT_VERSION 4
70 static SYSCTL_NODE(_hw, OID_AUTO, ntb_transport, CTLFLAG_RW, 0, "ntb_transport");
72 static unsigned g_ntb_transport_debug_level;
73 SYSCTL_UINT(_hw_ntb_transport, OID_AUTO, debug_level, CTLFLAG_RWTUN,
74 &g_ntb_transport_debug_level, 0,
75 "ntb_transport log level -- higher is more verbose");
76 #define ntb_printf(lvl, ...) do { \
77 if ((lvl) <= g_ntb_transport_debug_level) { \
78 printf(__VA_ARGS__); \
82 static unsigned transport_mtu = 0x10000;
84 static uint64_t max_mw_size = 256*1024*1024;
85 SYSCTL_UQUAD(_hw_ntb_transport, OID_AUTO, max_mw_size, CTLFLAG_RDTUN, &max_mw_size, 0,
86 "If enabled (non-zero), limit the size of large memory windows. "
87 "Both sides of the NTB MUST set the same value here.");
89 static unsigned enable_xeon_watchdog;
90 SYSCTL_UINT(_hw_ntb_transport, OID_AUTO, enable_xeon_watchdog, CTLFLAG_RDTUN,
91 &enable_xeon_watchdog, 0, "If non-zero, write a register every second to "
92 "keep a watchdog from tearing down the NTB link");
94 STAILQ_HEAD(ntb_queue_list, ntb_queue_entry);
96 typedef uint32_t ntb_q_idx_t;
98 struct ntb_queue_entry {
99 /* ntb_queue list reference */
100 STAILQ_ENTRY(ntb_queue_entry) entry;
102 /* info on data to be transferred */
108 struct ntb_transport_qp *qp;
109 struct ntb_payload_header *x_hdr;
117 struct ntb_transport_qp {
118 struct ntb_transport_ctx *transport;
124 volatile bool link_is_up;
125 uint8_t qp_num; /* Only 64 QPs are allowed. 0-63 */
127 struct ntb_rx_info *rx_info;
128 struct ntb_rx_info *remote_rx_info;
130 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
131 void *data, int len);
132 struct ntb_queue_list tx_free_q;
133 struct mtx ntb_tx_free_q_lock;
135 bus_addr_t tx_mw_phys;
136 ntb_q_idx_t tx_index;
137 ntb_q_idx_t tx_max_entry;
138 uint64_t tx_max_frame;
140 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
141 void *data, int len);
142 struct ntb_queue_list rx_post_q;
143 struct ntb_queue_list rx_pend_q;
144 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
145 struct mtx ntb_rx_q_lock;
146 struct task rxc_db_work;
147 struct taskqueue *rxc_tq;
149 ntb_q_idx_t rx_index;
150 ntb_q_idx_t rx_max_entry;
151 uint64_t rx_max_frame;
153 void (*event_handler)(void *data, enum ntb_link_event status);
154 struct callout link_work;
155 struct callout rx_full;
157 uint64_t last_rx_no_buf;
162 uint64_t rx_ring_empty;
163 uint64_t rx_err_no_buf;
164 uint64_t rx_err_oflow;
168 uint64_t tx_ring_full;
169 uint64_t tx_err_no_buf;
174 struct ntb_transport_mw {
175 vm_paddr_t phys_addr;
178 size_t xlat_align_size;
179 bus_addr_t addr_limit;
180 /* Tx buff is vbase / phys_addr / tx_size */
183 /* Rx buff is virt_addr / dma_addr / rx_size */
184 bus_dma_tag_t dma_tag;
185 bus_dmamap_t dma_map;
189 /* rx_size increased to size alignment requirements of the hardware. */
193 struct ntb_transport_child {
198 struct ntb_transport_child *next;
201 struct ntb_transport_ctx {
203 struct ntb_transport_child *child;
204 struct ntb_transport_mw *mw_vec;
205 struct ntb_transport_qp *qp_vec;
210 volatile bool link_is_up;
211 enum ntb_speed link_speed;
212 enum ntb_width link_width;
213 struct callout link_work;
214 struct callout link_watchdog;
215 struct task link_cleanup;
219 NTBT_DESC_DONE_FLAG = 1 << 0,
220 NTBT_LINK_DOWN_FLAG = 1 << 1,
223 struct ntb_payload_header {
231 * The order of this enum is part of the remote protocol. Do not
232 * reorder without bumping protocol version (and it's probably best
233 * to keep the protocol in lock-step with the Linux NTB driver.
240 * N.B.: transport_link_work assumes MW1 enums = MW0 + 2.
248 * Some NTB-using hardware have a watchdog to work around NTB hangs; if
249 * a register or doorbell isn't written every few seconds, the link is
250 * torn down. Write an otherwise unused register every few seconds to
251 * work around this watchdog.
253 NTBT_WATCHDOG_SPAD = 15
257 * Compart version of sratchpad protocol, using twice less registers.
260 NTBTC_PARAMS = 0, /* NUM_QPS << 24 + NUM_MWS << 16 + VERSION */
261 NTBTC_QP_LINKS, /* QP links status */
262 NTBTC_MW0_SZ, /* MW size limited to 32 bits. */
265 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
266 #define NTB_QP_DEF_NUM_ENTRIES 100
267 #define NTB_LINK_DOWN_TIMEOUT 100
269 static int ntb_transport_probe(device_t dev);
270 static int ntb_transport_attach(device_t dev);
271 static int ntb_transport_detach(device_t dev);
272 static void ntb_transport_init_queue(struct ntb_transport_ctx *nt,
273 unsigned int qp_num);
274 static int ntb_process_tx(struct ntb_transport_qp *qp,
275 struct ntb_queue_entry *entry);
276 static void ntb_transport_rxc_db(void *arg, int pending);
277 static int ntb_process_rxc(struct ntb_transport_qp *qp);
278 static void ntb_memcpy_rx(struct ntb_transport_qp *qp,
279 struct ntb_queue_entry *entry, void *offset);
280 static inline void ntb_rx_copy_callback(struct ntb_transport_qp *qp,
282 static void ntb_complete_rxc(struct ntb_transport_qp *qp);
283 static void ntb_transport_doorbell_callback(void *data, uint32_t vector);
284 static void ntb_transport_event_callback(void *data);
285 static void ntb_transport_link_work(void *arg);
286 static int ntb_set_mw(struct ntb_transport_ctx *, int num_mw, size_t size);
287 static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw);
288 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
289 unsigned int qp_num);
290 static void ntb_qp_link_work(void *arg);
291 static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt);
292 static void ntb_transport_link_cleanup_work(void *, int);
293 static void ntb_qp_link_down(struct ntb_transport_qp *qp);
294 static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp);
295 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp);
296 static void ntb_send_link_down(struct ntb_transport_qp *qp);
297 static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
298 struct ntb_queue_list *list);
299 static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock,
300 struct ntb_queue_list *list);
301 static struct ntb_queue_entry *ntb_list_mv(struct mtx *lock,
302 struct ntb_queue_list *from, struct ntb_queue_list *to);
303 static void xeon_link_watchdog_hb(void *);
305 static const struct ntb_ctx_ops ntb_transport_ops = {
306 .link_event = ntb_transport_event_callback,
307 .db_event = ntb_transport_doorbell_callback,
310 MALLOC_DEFINE(M_NTB_T, "ntb_transport", "ntb transport driver");
313 iowrite32(uint32_t val, void *addr)
316 bus_space_write_4(X86_BUS_SPACE_MEM, 0/* HACK */, (uintptr_t)addr,
320 /* Transport Init and teardown */
323 xeon_link_watchdog_hb(void *arg)
325 struct ntb_transport_ctx *nt;
328 ntb_spad_write(nt->dev, NTBT_WATCHDOG_SPAD, 0);
329 callout_reset(&nt->link_watchdog, 1 * hz, xeon_link_watchdog_hb, nt);
333 ntb_transport_probe(device_t dev)
336 device_set_desc(dev, "NTB Transport");
341 ntb_transport_attach(device_t dev)
343 struct ntb_transport_ctx *nt = device_get_softc(dev);
344 struct ntb_transport_child **cpp = &nt->child;
345 struct ntb_transport_child *nc;
346 struct ntb_transport_mw *mw;
348 int rc, i, db_count, spad_count, qp, qpu, qpo, qpt;
351 char *n, *np, *c, *name;
354 nt->mw_count = ntb_mw_count(dev);
355 spad_count = ntb_spad_count(dev);
356 db_bitmap = ntb_db_valid_mask(dev);
357 db_count = flsll(db_bitmap);
358 KASSERT(db_bitmap == (1 << db_count) - 1,
359 ("Doorbells are not sequential (%jx).\n", db_bitmap));
361 if (nt->mw_count == 0) {
362 device_printf(dev, "At least 1 memory window required.\n");
365 nt->compact = (spad_count < 4 + 2 * nt->mw_count);
366 snprintf(buf, sizeof(buf), "hint.%s.%d.compact", device_get_name(dev),
367 device_get_unit(dev));
368 TUNABLE_INT_FETCH(buf, &nt->compact);
370 if (spad_count < 3) {
371 device_printf(dev, "At least 3 scratchpads required.\n");
374 if (spad_count < 2 + nt->mw_count) {
375 nt->mw_count = spad_count - 2;
376 device_printf(dev, "Scratchpads enough only for %d "
377 "memory windows.\n", nt->mw_count);
380 if (spad_count < 6) {
381 device_printf(dev, "At least 6 scratchpads required.\n");
384 if (spad_count < 4 + 2 * nt->mw_count) {
385 nt->mw_count = (spad_count - 4) / 2;
386 device_printf(dev, "Scratchpads enough only for %d "
387 "memory windows.\n", nt->mw_count);
390 if (db_bitmap == 0) {
391 device_printf(dev, "At least one doorbell required.\n");
395 nt->mw_vec = malloc(nt->mw_count * sizeof(*nt->mw_vec), M_NTB_T,
397 for (i = 0; i < nt->mw_count; i++) {
400 rc = ntb_mw_get_range(dev, i, &mw->phys_addr, &mw->vbase,
401 &mw->phys_size, &mw->xlat_align, &mw->xlat_align_size,
406 mw->tx_size = mw->phys_size;
407 if (max_mw_size != 0 && mw->tx_size > max_mw_size) {
408 device_printf(dev, "Memory window %d limited from "
409 "%ju to %ju\n", i, (uintmax_t)mw->tx_size,
411 mw->tx_size = max_mw_size;
413 if (nt->compact && mw->tx_size > UINT32_MAX) {
414 device_printf(dev, "Memory window %d is too big "
415 "(%ju)\n", i, (uintmax_t)mw->tx_size);
422 mw->virt_addr = NULL;
425 rc = ntb_mw_set_wc(dev, i, VM_MEMATTR_WRITE_COMBINING);
427 ntb_printf(0, "Unable to set mw%d caching\n", i);
430 * Try to preallocate receive memory early, since there may
431 * be not enough contiguous memory later. It is quite likely
432 * that NTB windows are symmetric and this allocation remain,
433 * but even if not, we will just reallocate it later.
435 ntb_set_mw(nt, i, mw->tx_size);
439 qpo = imin(db_count, nt->mw_count);
442 snprintf(buf, sizeof(buf), "hint.%s.%d.config", device_get_name(dev),
443 device_get_unit(dev));
444 TUNABLE_STR_FETCH(buf, cfg, sizeof(cfg));
447 while ((c = strsep(&n, ",")) != NULL) {
449 name = strsep(&np, ":");
450 if (name != NULL && name[0] == 0)
452 qp = (np && np[0] != 0) ? strtol(np, NULL, 10) : qpo - qpu;
456 if (qp > qpt - qpu) {
457 device_printf(dev, "Not enough resources for config\n");
461 nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
465 nc->dev = device_add_child(dev, name, -1);
466 if (nc->dev == NULL) {
467 device_printf(dev, "Can not add child.\n");
470 device_set_ivars(nc->dev, nc);
475 device_printf(dev, "%d \"%s\": queues %d",
478 printf("-%d", qpu + qp - 1);
487 nt->qp_vec = malloc(nt->qp_count * sizeof(*nt->qp_vec), M_NTB_T,
490 for (i = 0; i < nt->qp_count; i++)
491 ntb_transport_init_queue(nt, i);
493 callout_init(&nt->link_work, 0);
494 callout_init(&nt->link_watchdog, 0);
495 TASK_INIT(&nt->link_cleanup, 0, ntb_transport_link_cleanup_work, nt);
496 nt->link_is_up = false;
498 rc = ntb_set_ctx(dev, nt, &ntb_transport_ops);
502 ntb_link_enable(dev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
504 for (i = 0; i < nt->mw_count; i++) {
506 rc = ntb_mw_set_trans(nt->dev, i, mw->dma_addr, mw->buff_size);
508 ntb_printf(0, "load time mw%d xlat fails, rc %d\n", i, rc);
511 if (enable_xeon_watchdog != 0)
512 callout_reset(&nt->link_watchdog, 0, xeon_link_watchdog_hb, nt);
514 bus_generic_attach(dev);
518 free(nt->qp_vec, M_NTB_T);
519 free(nt->mw_vec, M_NTB_T);
524 ntb_transport_detach(device_t dev)
526 struct ntb_transport_ctx *nt = device_get_softc(dev);
527 struct ntb_transport_child **cpp = &nt->child;
528 struct ntb_transport_child *nc;
531 while ((nc = *cpp) != NULL) {
533 error = device_delete_child(dev, nc->dev);
538 KASSERT(nt->qp_bitmap == 0,
539 ("Some queues not freed on detach (%jx)", nt->qp_bitmap));
541 ntb_transport_link_cleanup(nt);
542 taskqueue_drain(taskqueue_swi, &nt->link_cleanup);
543 callout_drain(&nt->link_work);
544 callout_drain(&nt->link_watchdog);
546 ntb_link_disable(dev);
549 for (i = 0; i < nt->mw_count; i++)
552 free(nt->qp_vec, M_NTB_T);
553 free(nt->mw_vec, M_NTB_T);
558 ntb_transport_print_child(device_t dev, device_t child)
560 struct ntb_transport_child *nc = device_get_ivars(child);
563 retval = bus_print_child_header(dev, child);
565 printf(" queue %d", nc->qpoff);
567 printf("-%d", nc->qpoff + nc->qpcnt - 1);
569 retval += printf(" at consumer %d", nc->consumer);
570 retval += bus_print_child_domain(dev, child);
571 retval += bus_print_child_footer(dev, child);
577 ntb_transport_child_location_str(device_t dev, device_t child, char *buf,
580 struct ntb_transport_child *nc = device_get_ivars(child);
582 snprintf(buf, buflen, "consumer=%d", nc->consumer);
587 ntb_transport_queue_count(device_t dev)
589 struct ntb_transport_child *nc = device_get_ivars(dev);
595 ntb_transport_init_queue(struct ntb_transport_ctx *nt, unsigned int qp_num)
597 struct ntb_transport_mw *mw;
598 struct ntb_transport_qp *qp;
602 unsigned num_qps_mw, mw_num, mw_count;
604 mw_count = nt->mw_count;
605 mw_num = QP_TO_MW(nt, qp_num);
606 mw = &nt->mw_vec[mw_num];
608 qp = &nt->qp_vec[qp_num];
612 qp->client_ready = false;
613 qp->event_handler = NULL;
614 ntb_qp_link_down_reset(qp);
616 if (mw_num < nt->qp_count % mw_count)
617 num_qps_mw = nt->qp_count / mw_count + 1;
619 num_qps_mw = nt->qp_count / mw_count;
621 mw_base = mw->phys_addr;
623 tx_size = mw->tx_size / num_qps_mw;
624 qp_offset = tx_size * (qp_num / mw_count);
626 qp->tx_mw = mw->vbase + qp_offset;
627 KASSERT(qp->tx_mw != NULL, ("uh oh?"));
629 /* XXX Assumes that a vm_paddr_t is equivalent to bus_addr_t */
630 qp->tx_mw_phys = mw_base + qp_offset;
631 KASSERT(qp->tx_mw_phys != 0, ("uh oh?"));
633 tx_size -= sizeof(struct ntb_rx_info);
634 qp->rx_info = (void *)(qp->tx_mw + tx_size);
636 /* Due to house-keeping, there must be at least 2 buffs */
637 qp->tx_max_frame = qmin(transport_mtu, tx_size / 2);
638 qp->tx_max_entry = tx_size / qp->tx_max_frame;
640 callout_init(&qp->link_work, 0);
641 callout_init(&qp->rx_full, 1);
643 mtx_init(&qp->ntb_rx_q_lock, "ntb rx q", NULL, MTX_SPIN);
644 mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN);
645 mtx_init(&qp->tx_lock, "ntb transport tx", NULL, MTX_DEF);
646 TASK_INIT(&qp->rxc_db_work, 0, ntb_transport_rxc_db, qp);
647 qp->rxc_tq = taskqueue_create("ntbt_rx", M_WAITOK,
648 taskqueue_thread_enqueue, &qp->rxc_tq);
649 taskqueue_start_threads(&qp->rxc_tq, 1, PI_NET, "%s rx%d",
650 device_get_nameunit(nt->dev), qp_num);
652 STAILQ_INIT(&qp->rx_post_q);
653 STAILQ_INIT(&qp->rx_pend_q);
654 STAILQ_INIT(&qp->tx_free_q);
658 ntb_transport_free_queue(struct ntb_transport_qp *qp)
660 struct ntb_transport_ctx *nt = qp->transport;
661 struct ntb_queue_entry *entry;
663 callout_drain(&qp->link_work);
665 ntb_db_set_mask(qp->dev, 1ull << qp->qp_num);
666 taskqueue_drain_all(qp->rxc_tq);
667 taskqueue_free(qp->rxc_tq);
670 qp->rx_handler = NULL;
671 qp->tx_handler = NULL;
672 qp->event_handler = NULL;
674 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q)))
675 free(entry, M_NTB_T);
677 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q)))
678 free(entry, M_NTB_T);
680 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
681 free(entry, M_NTB_T);
683 nt->qp_bitmap &= ~(1 << qp->qp_num);
687 * ntb_transport_create_queue - Create a new NTB transport layer queue
688 * @rx_handler: receive callback function
689 * @tx_handler: transmit callback function
690 * @event_handler: event callback function
692 * Create a new NTB transport layer queue and provide the queue with a callback
693 * routine for both transmit and receive. The receive callback routine will be
694 * used to pass up data when the transport has received it on the queue. The
695 * transmit callback routine will be called when the transport has completed the
696 * transmission of the data on the queue and the data is ready to be freed.
698 * RETURNS: pointer to newly created ntb_queue, NULL on error.
700 struct ntb_transport_qp *
701 ntb_transport_create_queue(device_t dev, int q,
702 const struct ntb_queue_handlers *handlers, void *data)
704 struct ntb_transport_child *nc = device_get_ivars(dev);
705 struct ntb_transport_ctx *nt = device_get_softc(device_get_parent(dev));
706 struct ntb_queue_entry *entry;
707 struct ntb_transport_qp *qp;
710 if (q < 0 || q >= nc->qpcnt)
713 qp = &nt->qp_vec[nc->qpoff + q];
714 nt->qp_bitmap |= (1 << qp->qp_num);
716 qp->rx_handler = handlers->rx_handler;
717 qp->tx_handler = handlers->tx_handler;
718 qp->event_handler = handlers->event_handler;
720 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
721 entry = malloc(sizeof(*entry), M_NTB_T, M_WAITOK | M_ZERO);
722 entry->cb_data = data;
724 entry->len = transport_mtu;
726 ntb_list_add(&qp->ntb_rx_q_lock, entry, &qp->rx_pend_q);
729 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
730 entry = malloc(sizeof(*entry), M_NTB_T, M_WAITOK | M_ZERO);
732 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
735 ntb_db_clear(dev, 1ull << qp->qp_num);
740 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
741 * @qp: NTB transport layer queue to be enabled
743 * Notify NTB transport layer of client readiness to use queue
746 ntb_transport_link_up(struct ntb_transport_qp *qp)
748 struct ntb_transport_ctx *nt = qp->transport;
750 qp->client_ready = true;
752 ntb_printf(2, "qp %d client ready\n", qp->qp_num);
755 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
763 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
764 * @qp: NTB transport layer queue the entry is to be enqueued on
765 * @cb: per buffer pointer for callback function to use
766 * @data: pointer to data buffer that will be sent
767 * @len: length of the data buffer
769 * Enqueue a new transmit buffer onto the transport queue from which a NTB
770 * payload will be transmitted. This assumes that a lock is being held to
771 * serialize access to the qp.
773 * RETURNS: An appropriate ERRNO error value on error, or zero for success.
776 ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
779 struct ntb_queue_entry *entry;
782 if (!qp->link_is_up || len == 0) {
783 CTR0(KTR_NTB, "TX: link not up");
787 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
789 CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
793 CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
800 mtx_lock(&qp->tx_lock);
801 rc = ntb_process_tx(qp, entry);
802 mtx_unlock(&qp->tx_lock);
804 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
806 "TX: process_tx failed. Returning entry %p to tx_free_q",
813 ntb_tx_copy_callback(void *data)
815 struct ntb_queue_entry *entry = data;
816 struct ntb_transport_qp *qp = entry->qp;
817 struct ntb_payload_header *hdr = entry->x_hdr;
819 iowrite32(entry->flags | NTBT_DESC_DONE_FLAG, &hdr->flags);
820 CTR1(KTR_NTB, "TX: hdr %p set DESC_DONE", hdr);
822 ntb_peer_db_set(qp->dev, 1ull << qp->qp_num);
825 * The entry length can only be zero if the packet is intended to be a
826 * "link down" or similar. Since no payload is being sent in these
827 * cases, there is nothing to add to the completion queue.
829 if (entry->len > 0) {
830 qp->tx_bytes += entry->len;
833 qp->tx_handler(qp, qp->cb_data, entry->buf,
841 "TX: entry %p sent. hdr->ver = %u, hdr->flags = 0x%x, Returning "
842 "to tx_free_q", entry, hdr->ver, hdr->flags);
843 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
847 ntb_memcpy_tx(struct ntb_queue_entry *entry, void *offset)
850 CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset);
851 if (entry->buf != NULL) {
852 m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset);
855 * Ensure that the data is fully copied before setting the
861 ntb_tx_copy_callback(entry);
865 ntb_async_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
867 struct ntb_payload_header *hdr;
870 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
871 hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame -
872 sizeof(struct ntb_payload_header));
875 iowrite32(entry->len, &hdr->len);
876 iowrite32(qp->tx_pkts, &hdr->ver);
878 ntb_memcpy_tx(entry, offset);
882 ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
886 "TX: process_tx: tx_pkts=%lu, tx_index=%u, remote entry=%u",
887 qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry);
888 if (qp->tx_index == qp->remote_rx_info->entry) {
889 CTR0(KTR_NTB, "TX: ring full");
894 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
895 if (qp->tx_handler != NULL)
896 qp->tx_handler(qp, qp->cb_data, entry->buf,
902 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
904 "TX: frame too big. returning entry %p to tx_free_q",
908 CTR2(KTR_NTB, "TX: copying entry %p to index %u", entry, qp->tx_index);
909 ntb_async_tx(qp, entry);
912 qp->tx_index %= qp->tx_max_entry;
921 ntb_transport_rxc_db(void *arg, int pending __unused)
923 struct ntb_transport_qp *qp = arg;
924 uint64_t qp_mask = 1ull << qp->qp_num;
927 CTR0(KTR_NTB, "RX: transport_rx");
929 while ((rc = ntb_process_rxc(qp)) == 0)
931 CTR1(KTR_NTB, "RX: process_rxc returned %d", rc);
933 if ((ntb_db_read(qp->dev) & qp_mask) != 0) {
934 /* If db is set, clear it and check queue once more. */
935 ntb_db_clear(qp->dev, qp_mask);
939 ntb_db_clear_mask(qp->dev, qp_mask);
943 ntb_process_rxc(struct ntb_transport_qp *qp)
945 struct ntb_payload_header *hdr;
946 struct ntb_queue_entry *entry;
949 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
950 hdr = (void *)(offset + qp->rx_max_frame -
951 sizeof(struct ntb_payload_header));
953 CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index);
954 if ((hdr->flags & NTBT_DESC_DONE_FLAG) == 0) {
955 CTR0(KTR_NTB, "RX: hdr not done");
960 if ((hdr->flags & NTBT_LINK_DOWN_FLAG) != 0) {
961 CTR0(KTR_NTB, "RX: link down");
962 ntb_qp_link_down(qp);
967 if (hdr->ver != (uint32_t)qp->rx_pkts) {
968 CTR2(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). "
969 "Returning entry to rx_pend_q", hdr->ver, qp->rx_pkts);
974 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
977 CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
980 callout_stop(&qp->rx_full);
981 CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
984 entry->index = qp->rx_index;
986 if (hdr->len > entry->len) {
987 CTR2(KTR_NTB, "RX: len too long. Wanted %ju got %ju",
988 (uintmax_t)hdr->len, (uintmax_t)entry->len);
992 entry->flags |= NTBT_DESC_DONE_FLAG;
994 ntb_complete_rxc(qp);
996 qp->rx_bytes += hdr->len;
999 CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
1001 entry->len = hdr->len;
1003 ntb_memcpy_rx(qp, entry, offset);
1007 qp->rx_index %= qp->rx_max_entry;
1012 ntb_memcpy_rx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
1015 struct ifnet *ifp = entry->cb_data;
1016 unsigned int len = entry->len;
1018 CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset);
1020 entry->buf = (void *)m_devget(offset, len, 0, ifp, NULL);
1021 if (entry->buf == NULL)
1022 entry->len = -ENOMEM;
1024 /* Ensure that the data is globally visible before clearing the flag */
1027 CTR2(KTR_NTB, "RX: copied entry %p to mbuf %p.", entry, entry->buf);
1028 ntb_rx_copy_callback(qp, entry);
1032 ntb_rx_copy_callback(struct ntb_transport_qp *qp, void *data)
1034 struct ntb_queue_entry *entry;
1037 entry->flags |= NTBT_DESC_DONE_FLAG;
1038 ntb_complete_rxc(qp);
1042 ntb_complete_rxc(struct ntb_transport_qp *qp)
1044 struct ntb_queue_entry *entry;
1048 CTR0(KTR_NTB, "RX: rx_completion_task");
1050 mtx_lock_spin(&qp->ntb_rx_q_lock);
1052 while (!STAILQ_EMPTY(&qp->rx_post_q)) {
1053 entry = STAILQ_FIRST(&qp->rx_post_q);
1054 if ((entry->flags & NTBT_DESC_DONE_FLAG) == 0)
1057 entry->x_hdr->flags = 0;
1058 iowrite32(entry->index, &qp->rx_info->entry);
1060 STAILQ_REMOVE_HEAD(&qp->rx_post_q, entry);
1066 * Re-initialize queue_entry for reuse; rx_handler takes
1067 * ownership of the mbuf.
1070 entry->len = transport_mtu;
1071 entry->cb_data = qp->cb_data;
1073 STAILQ_INSERT_TAIL(&qp->rx_pend_q, entry, entry);
1075 mtx_unlock_spin(&qp->ntb_rx_q_lock);
1077 CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m);
1078 if (qp->rx_handler != NULL && qp->client_ready)
1079 qp->rx_handler(qp, qp->cb_data, m, len);
1083 mtx_lock_spin(&qp->ntb_rx_q_lock);
1086 mtx_unlock_spin(&qp->ntb_rx_q_lock);
1090 ntb_transport_doorbell_callback(void *data, uint32_t vector)
1092 struct ntb_transport_ctx *nt = data;
1093 struct ntb_transport_qp *qp;
1097 vec_mask = ntb_db_vector_mask(nt->dev, vector);
1098 vec_mask &= nt->qp_bitmap;
1099 if ((vec_mask & (vec_mask - 1)) != 0)
1100 vec_mask &= ntb_db_read(nt->dev);
1101 if (vec_mask != 0) {
1102 ntb_db_set_mask(nt->dev, vec_mask);
1103 ntb_db_clear(nt->dev, vec_mask);
1105 while (vec_mask != 0) {
1106 qp_num = ffsll(vec_mask) - 1;
1108 qp = &nt->qp_vec[qp_num];
1110 taskqueue_enqueue(qp->rxc_tq, &qp->rxc_db_work);
1112 vec_mask &= ~(1ull << qp_num);
1116 /* Link Event handler */
1118 ntb_transport_event_callback(void *data)
1120 struct ntb_transport_ctx *nt = data;
1122 if (ntb_link_is_up(nt->dev, &nt->link_speed, &nt->link_width)) {
1123 ntb_printf(1, "HW link up\n");
1124 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
1126 ntb_printf(1, "HW link down\n");
1127 taskqueue_enqueue(taskqueue_swi, &nt->link_cleanup);
1133 ntb_transport_link_work(void *arg)
1135 struct ntb_transport_ctx *nt = arg;
1136 struct ntb_transport_mw *mw;
1137 device_t dev = nt->dev;
1138 struct ntb_transport_qp *qp;
1139 uint64_t val64, size;
1144 /* send the local info, in the opposite order of the way we read it */
1146 for (i = 0; i < nt->mw_count; i++) {
1147 size = nt->mw_vec[i].tx_size;
1148 KASSERT(size <= UINT32_MAX, ("size too big (%jx)", size));
1149 ntb_peer_spad_write(dev, NTBTC_MW0_SZ + i, size);
1151 ntb_peer_spad_write(dev, NTBTC_QP_LINKS, 0);
1152 ntb_peer_spad_write(dev, NTBTC_PARAMS,
1153 (nt->qp_count << 24) | (nt->mw_count << 16) |
1154 NTB_TRANSPORT_VERSION);
1156 for (i = 0; i < nt->mw_count; i++) {
1157 size = nt->mw_vec[i].tx_size;
1158 ntb_peer_spad_write(dev, NTBT_MW0_SZ_HIGH + (i * 2),
1160 ntb_peer_spad_write(dev, NTBT_MW0_SZ_LOW + (i * 2), size);
1162 ntb_peer_spad_write(dev, NTBT_NUM_MWS, nt->mw_count);
1163 ntb_peer_spad_write(dev, NTBT_NUM_QPS, nt->qp_count);
1164 ntb_peer_spad_write(dev, NTBT_QP_LINKS, 0);
1165 ntb_peer_spad_write(dev, NTBT_VERSION, NTB_TRANSPORT_VERSION);
1168 /* Query the remote side for its info */
1171 ntb_spad_read(dev, NTBTC_PARAMS, &val);
1172 if (val != ((nt->qp_count << 24) | (nt->mw_count << 16) |
1173 NTB_TRANSPORT_VERSION))
1176 ntb_spad_read(dev, NTBT_VERSION, &val);
1177 if (val != NTB_TRANSPORT_VERSION)
1180 ntb_spad_read(dev, NTBT_NUM_QPS, &val);
1181 if (val != nt->qp_count)
1184 ntb_spad_read(dev, NTBT_NUM_MWS, &val);
1185 if (val != nt->mw_count)
1189 for (i = 0; i < nt->mw_count; i++) {
1191 ntb_spad_read(dev, NTBTC_MW0_SZ + i, &val);
1194 ntb_spad_read(dev, NTBT_MW0_SZ_HIGH + (i * 2), &val);
1195 val64 = (uint64_t)val << 32;
1197 ntb_spad_read(dev, NTBT_MW0_SZ_LOW + (i * 2), &val);
1201 mw = &nt->mw_vec[i];
1202 mw->rx_size = val64;
1203 val64 = roundup(val64, mw->xlat_align_size);
1204 if (mw->buff_size != val64) {
1206 rc = ntb_set_mw(nt, i, val64);
1208 ntb_printf(0, "link up set mw%d fails, rc %d\n",
1213 /* Notify HW the memory location of the receive buffer */
1214 rc = ntb_mw_set_trans(nt->dev, i, mw->dma_addr,
1217 ntb_printf(0, "link up mw%d xlat fails, rc %d\n",
1224 nt->link_is_up = true;
1225 ntb_printf(1, "transport link up\n");
1227 for (i = 0; i < nt->qp_count; i++) {
1228 qp = &nt->qp_vec[i];
1230 ntb_transport_setup_qp_mw(nt, i);
1232 if (qp->client_ready)
1233 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1239 for (i = 0; i < nt->mw_count; i++)
1242 if (ntb_link_is_up(dev, &nt->link_speed, &nt->link_width))
1243 callout_reset(&nt->link_work,
1244 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt);
1247 struct ntb_load_cb_args {
1253 ntb_load_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
1255 struct ntb_load_cb_args *cba = (struct ntb_load_cb_args *)xsc;
1257 if (!(cba->error = error))
1258 cba->addr = segs[0].ds_addr;
1262 ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, size_t size)
1264 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1265 struct ntb_load_cb_args cba;
1271 buff_size = roundup(size, mw->xlat_align_size);
1273 /* No need to re-setup */
1274 if (mw->buff_size == buff_size)
1277 if (mw->buff_size != 0)
1278 ntb_free_mw(nt, num_mw);
1280 /* Alloc memory for receiving data. Must be aligned */
1281 mw->buff_size = buff_size;
1283 if (bus_dma_tag_create(bus_get_dma_tag(nt->dev), mw->xlat_align, 0,
1284 mw->addr_limit, BUS_SPACE_MAXADDR,
1285 NULL, NULL, mw->buff_size, 1, mw->buff_size,
1286 0, NULL, NULL, &mw->dma_tag)) {
1287 ntb_printf(0, "Unable to create MW tag of size %zu\n",
1292 if (bus_dmamem_alloc(mw->dma_tag, (void **)&mw->virt_addr,
1293 BUS_DMA_WAITOK | BUS_DMA_ZERO, &mw->dma_map)) {
1294 bus_dma_tag_destroy(mw->dma_tag);
1295 ntb_printf(0, "Unable to allocate MW buffer of size %zu\n",
1300 if (bus_dmamap_load(mw->dma_tag, mw->dma_map, mw->virt_addr,
1301 mw->buff_size, ntb_load_cb, &cba, BUS_DMA_NOWAIT) || cba.error) {
1302 bus_dmamem_free(mw->dma_tag, mw->virt_addr, mw->dma_map);
1303 bus_dma_tag_destroy(mw->dma_tag);
1304 ntb_printf(0, "Unable to load MW buffer of size %zu\n",
1309 mw->dma_addr = cba.addr;
1315 ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
1317 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1319 if (mw->virt_addr == NULL)
1322 ntb_mw_clear_trans(nt->dev, num_mw);
1323 bus_dmamap_unload(mw->dma_tag, mw->dma_map);
1324 bus_dmamem_free(mw->dma_tag, mw->virt_addr, mw->dma_map);
1325 bus_dma_tag_destroy(mw->dma_tag);
1327 mw->virt_addr = NULL;
1331 ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, unsigned int qp_num)
1333 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
1334 struct ntb_transport_mw *mw;
1338 unsigned num_qps_mw, mw_num, mw_count;
1340 mw_count = nt->mw_count;
1341 mw_num = QP_TO_MW(nt, qp_num);
1342 mw = &nt->mw_vec[mw_num];
1344 if (mw->virt_addr == NULL)
1347 if (mw_num < nt->qp_count % mw_count)
1348 num_qps_mw = nt->qp_count / mw_count + 1;
1350 num_qps_mw = nt->qp_count / mw_count;
1352 rx_size = mw->rx_size / num_qps_mw;
1353 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
1354 rx_size -= sizeof(struct ntb_rx_info);
1356 qp->remote_rx_info = (void*)(qp->rx_buff + rx_size);
1358 /* Due to house-keeping, there must be at least 2 buffs */
1359 qp->rx_max_frame = qmin(transport_mtu, rx_size / 2);
1360 qp->rx_max_entry = rx_size / qp->rx_max_frame;
1363 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
1365 /* Set up the hdr offsets with 0s */
1366 for (i = 0; i < qp->rx_max_entry; i++) {
1367 offset = (void *)(qp->rx_buff + qp->rx_max_frame * (i + 1) -
1368 sizeof(struct ntb_payload_header));
1369 memset(offset, 0, sizeof(struct ntb_payload_header));
1380 ntb_qp_link_work(void *arg)
1382 struct ntb_transport_qp *qp = arg;
1383 device_t dev = qp->dev;
1384 struct ntb_transport_ctx *nt = qp->transport;
1388 /* Report queues that are up on our side */
1389 for (i = 0, val = 0; i < nt->qp_count; i++) {
1390 if (nt->qp_vec[i].client_ready)
1393 ntb_peer_spad_write(dev, NTBT_QP_LINKS, val);
1395 /* See if the remote side is up */
1396 ntb_spad_read(dev, NTBT_QP_LINKS, &val);
1397 if ((val & (1ull << qp->qp_num)) != 0) {
1398 ntb_printf(2, "qp %d link up\n", qp->qp_num);
1399 qp->link_is_up = true;
1401 if (qp->event_handler != NULL)
1402 qp->event_handler(qp->cb_data, NTB_LINK_UP);
1404 ntb_db_clear_mask(dev, 1ull << qp->qp_num);
1405 } else if (nt->link_is_up)
1406 callout_reset(&qp->link_work,
1407 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1410 /* Link down event*/
1412 ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
1414 struct ntb_transport_qp *qp;
1417 callout_drain(&nt->link_work);
1420 /* Pass along the info to any clients */
1421 for (i = 0; i < nt->qp_count; i++) {
1422 if ((nt->qp_bitmap & (1 << i)) != 0) {
1423 qp = &nt->qp_vec[i];
1424 ntb_qp_link_cleanup(qp);
1425 callout_drain(&qp->link_work);
1430 * The scratchpad registers keep the values if the remote side
1431 * goes down, blast them now to give them a sane value the next
1432 * time they are accessed
1434 ntb_spad_clear(nt->dev);
1438 ntb_transport_link_cleanup_work(void *arg, int pending __unused)
1441 ntb_transport_link_cleanup(arg);
1445 ntb_qp_link_down(struct ntb_transport_qp *qp)
1448 ntb_qp_link_cleanup(qp);
1452 ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
1455 qp->link_is_up = false;
1456 ntb_db_set_mask(qp->dev, 1ull << qp->qp_num);
1458 qp->tx_index = qp->rx_index = 0;
1459 qp->tx_bytes = qp->rx_bytes = 0;
1460 qp->tx_pkts = qp->rx_pkts = 0;
1462 qp->rx_ring_empty = 0;
1463 qp->tx_ring_full = 0;
1465 qp->rx_err_no_buf = qp->tx_err_no_buf = 0;
1466 qp->rx_err_oflow = qp->rx_err_ver = 0;
1470 ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1473 callout_drain(&qp->link_work);
1474 ntb_qp_link_down_reset(qp);
1476 if (qp->event_handler != NULL)
1477 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1480 /* Link commanded down */
1482 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1483 * @qp: NTB transport layer queue to be disabled
1485 * Notify NTB transport layer of client's desire to no longer receive data on
1486 * transport queue specified. It is the client's responsibility to ensure all
1487 * entries on queue are purged or otherwise handled appropriately.
1490 ntb_transport_link_down(struct ntb_transport_qp *qp)
1492 struct ntb_transport_ctx *nt = qp->transport;
1496 qp->client_ready = false;
1497 for (i = 0, val = 0; i < nt->qp_count; i++) {
1498 if (nt->qp_vec[i].client_ready)
1501 ntb_peer_spad_write(qp->dev, NTBT_QP_LINKS, val);
1504 ntb_send_link_down(qp);
1506 callout_drain(&qp->link_work);
1510 * ntb_transport_link_query - Query transport link state
1511 * @qp: NTB transport layer queue to be queried
1513 * Query connectivity to the remote system of the NTB transport queue
1515 * RETURNS: true for link up or false for link down
1518 ntb_transport_link_query(struct ntb_transport_qp *qp)
1521 return (qp->link_is_up);
1525 * ntb_transport_link_speed - Query transport link speed
1526 * @qp: NTB transport layer queue to be queried
1528 * Query connection speed to the remote system of the NTB transport queue
1530 * RETURNS: link speed in bits per second
1533 ntb_transport_link_speed(struct ntb_transport_qp *qp)
1535 struct ntb_transport_ctx *nt = qp->transport;
1538 if (!nt->link_is_up)
1540 switch (nt->link_speed) {
1541 case NTB_SPEED_GEN1:
1542 rate = 2500000000 * 8 / 10;
1544 case NTB_SPEED_GEN2:
1545 rate = 5000000000 * 8 / 10;
1547 case NTB_SPEED_GEN3:
1548 rate = 8000000000 * 128 / 130;
1550 case NTB_SPEED_GEN4:
1551 rate = 16000000000 * 128 / 130;
1556 if (nt->link_width <= 0)
1558 return (rate * nt->link_width);
1562 ntb_send_link_down(struct ntb_transport_qp *qp)
1564 struct ntb_queue_entry *entry;
1567 if (!qp->link_is_up)
1570 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1571 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1574 pause("NTB Wait for link down", hz / 10);
1580 entry->cb_data = NULL;
1583 entry->flags = NTBT_LINK_DOWN_FLAG;
1585 mtx_lock(&qp->tx_lock);
1586 rc = ntb_process_tx(qp, entry);
1587 mtx_unlock(&qp->tx_lock);
1589 printf("ntb: Failed to send link down\n");
1591 ntb_qp_link_down_reset(qp);
1595 /* List Management */
1598 ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1599 struct ntb_queue_list *list)
1602 mtx_lock_spin(lock);
1603 STAILQ_INSERT_TAIL(list, entry, entry);
1604 mtx_unlock_spin(lock);
1607 static struct ntb_queue_entry *
1608 ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1610 struct ntb_queue_entry *entry;
1612 mtx_lock_spin(lock);
1613 if (STAILQ_EMPTY(list)) {
1617 entry = STAILQ_FIRST(list);
1618 STAILQ_REMOVE_HEAD(list, entry);
1620 mtx_unlock_spin(lock);
1625 static struct ntb_queue_entry *
1626 ntb_list_mv(struct mtx *lock, struct ntb_queue_list *from,
1627 struct ntb_queue_list *to)
1629 struct ntb_queue_entry *entry;
1631 mtx_lock_spin(lock);
1632 if (STAILQ_EMPTY(from)) {
1636 entry = STAILQ_FIRST(from);
1637 STAILQ_REMOVE_HEAD(from, entry);
1638 STAILQ_INSERT_TAIL(to, entry, entry);
1641 mtx_unlock_spin(lock);
1646 * ntb_transport_qp_num - Query the qp number
1647 * @qp: NTB transport layer queue to be queried
1649 * Query qp number of the NTB transport queue
1651 * RETURNS: a zero based number specifying the qp number
1653 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1656 return (qp->qp_num);
1660 * ntb_transport_max_size - Query the max payload size of a qp
1661 * @qp: NTB transport layer queue to be queried
1663 * Query the maximum payload size permissible on the given qp
1665 * RETURNS: the max payload size of a qp
1668 ntb_transport_max_size(struct ntb_transport_qp *qp)
1671 return (qp->tx_max_frame - sizeof(struct ntb_payload_header));
1675 ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
1677 unsigned int head = qp->tx_index;
1678 unsigned int tail = qp->remote_rx_info->entry;
1680 return (tail >= head ? tail - head : qp->tx_max_entry + tail - head);
1683 static device_method_t ntb_transport_methods[] = {
1684 /* Device interface */
1685 DEVMETHOD(device_probe, ntb_transport_probe),
1686 DEVMETHOD(device_attach, ntb_transport_attach),
1687 DEVMETHOD(device_detach, ntb_transport_detach),
1689 DEVMETHOD(bus_child_location_str, ntb_transport_child_location_str),
1690 DEVMETHOD(bus_print_child, ntb_transport_print_child),
1694 devclass_t ntb_transport_devclass;
1695 static DEFINE_CLASS_0(ntb_transport, ntb_transport_driver,
1696 ntb_transport_methods, sizeof(struct ntb_transport_ctx));
1697 DRIVER_MODULE(ntb_transport, ntb_hw, ntb_transport_driver,
1698 ntb_transport_devclass, NULL, NULL);
1699 MODULE_DEPEND(ntb_transport, ntb, 1, 1, 1);
1700 MODULE_VERSION(ntb_transport, 1);