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;
209 volatile bool link_is_up;
210 enum ntb_speed link_speed;
211 enum ntb_width link_width;
212 struct callout link_work;
213 struct callout link_watchdog;
214 struct task link_cleanup;
218 NTBT_DESC_DONE_FLAG = 1 << 0,
219 NTBT_LINK_DOWN_FLAG = 1 << 1,
222 struct ntb_payload_header {
230 * The order of this enum is part of the remote protocol. Do not
231 * reorder without bumping protocol version (and it's probably best
232 * to keep the protocol in lock-step with the Linux NTB driver.
239 * N.B.: transport_link_work assumes MW1 enums = MW0 + 2.
247 * Some NTB-using hardware have a watchdog to work around NTB hangs; if
248 * a register or doorbell isn't written every few seconds, the link is
249 * torn down. Write an otherwise unused register every few seconds to
250 * work around this watchdog.
252 NTBT_WATCHDOG_SPAD = 15
255 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
256 #define NTB_QP_DEF_NUM_ENTRIES 100
257 #define NTB_LINK_DOWN_TIMEOUT 100
259 static int ntb_transport_probe(device_t dev);
260 static int ntb_transport_attach(device_t dev);
261 static int ntb_transport_detach(device_t dev);
262 static void ntb_transport_init_queue(struct ntb_transport_ctx *nt,
263 unsigned int qp_num);
264 static int ntb_process_tx(struct ntb_transport_qp *qp,
265 struct ntb_queue_entry *entry);
266 static void ntb_transport_rxc_db(void *arg, int pending);
267 static int ntb_process_rxc(struct ntb_transport_qp *qp);
268 static void ntb_memcpy_rx(struct ntb_transport_qp *qp,
269 struct ntb_queue_entry *entry, void *offset);
270 static inline void ntb_rx_copy_callback(struct ntb_transport_qp *qp,
272 static void ntb_complete_rxc(struct ntb_transport_qp *qp);
273 static void ntb_transport_doorbell_callback(void *data, uint32_t vector);
274 static void ntb_transport_event_callback(void *data);
275 static void ntb_transport_link_work(void *arg);
276 static int ntb_set_mw(struct ntb_transport_ctx *, int num_mw, size_t size);
277 static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw);
278 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
279 unsigned int qp_num);
280 static void ntb_qp_link_work(void *arg);
281 static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt);
282 static void ntb_transport_link_cleanup_work(void *, int);
283 static void ntb_qp_link_down(struct ntb_transport_qp *qp);
284 static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp);
285 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp);
286 static void ntb_send_link_down(struct ntb_transport_qp *qp);
287 static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
288 struct ntb_queue_list *list);
289 static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock,
290 struct ntb_queue_list *list);
291 static struct ntb_queue_entry *ntb_list_mv(struct mtx *lock,
292 struct ntb_queue_list *from, struct ntb_queue_list *to);
293 static void xeon_link_watchdog_hb(void *);
295 static const struct ntb_ctx_ops ntb_transport_ops = {
296 .link_event = ntb_transport_event_callback,
297 .db_event = ntb_transport_doorbell_callback,
300 MALLOC_DEFINE(M_NTB_T, "ntb_transport", "ntb transport driver");
303 iowrite32(uint32_t val, void *addr)
306 bus_space_write_4(X86_BUS_SPACE_MEM, 0/* HACK */, (uintptr_t)addr,
310 /* Transport Init and teardown */
313 xeon_link_watchdog_hb(void *arg)
315 struct ntb_transport_ctx *nt;
318 ntb_spad_write(nt->dev, NTBT_WATCHDOG_SPAD, 0);
319 callout_reset(&nt->link_watchdog, 1 * hz, xeon_link_watchdog_hb, nt);
323 ntb_transport_probe(device_t dev)
326 device_set_desc(dev, "NTB Transport");
331 ntb_transport_attach(device_t dev)
333 struct ntb_transport_ctx *nt = device_get_softc(dev);
334 struct ntb_transport_child **cpp = &nt->child;
335 struct ntb_transport_child *nc;
336 struct ntb_transport_mw *mw;
338 int rc, i, db_count, spad_count, qp, qpu, qpo, qpt;
341 char *n, *np, *c, *name;
344 nt->mw_count = ntb_mw_count(dev);
345 spad_count = ntb_spad_count(dev);
346 db_bitmap = ntb_db_valid_mask(dev);
347 db_count = flsll(db_bitmap);
348 KASSERT(db_bitmap == (1 << db_count) - 1,
349 ("Doorbells are not sequential (%jx).\n", db_bitmap));
351 if (nt->mw_count == 0) {
352 device_printf(dev, "At least 1 memory window required.\n");
355 if (spad_count < 6) {
356 device_printf(dev, "At least 6 scratchpads required.\n");
359 if (spad_count < 4 + 2 * nt->mw_count) {
360 nt->mw_count = (spad_count - 4) / 2;
361 device_printf(dev, "Scratchpads enough only for %d "
362 "memory windows.\n", nt->mw_count);
364 if (db_bitmap == 0) {
365 device_printf(dev, "At least one doorbell required.\n");
369 nt->mw_vec = malloc(nt->mw_count * sizeof(*nt->mw_vec), M_NTB_T,
371 for (i = 0; i < nt->mw_count; i++) {
374 rc = ntb_mw_get_range(dev, i, &mw->phys_addr, &mw->vbase,
375 &mw->phys_size, &mw->xlat_align, &mw->xlat_align_size,
380 mw->tx_size = mw->phys_size;
381 if (max_mw_size != 0 && mw->tx_size > max_mw_size) {
382 device_printf(dev, "Memory window %d limited from "
383 "%ju to %ju\n", i, (uintmax_t)mw->phys_size,
385 mw->tx_size = max_mw_size;
390 mw->virt_addr = NULL;
393 rc = ntb_mw_set_wc(dev, i, VM_MEMATTR_WRITE_COMBINING);
395 ntb_printf(0, "Unable to set mw%d caching\n", i);
398 * Try to preallocate receive memory early, since there may
399 * be not enough contiguous memory later. It is quite likely
400 * that NTB windows are symmetric and this allocation remain,
401 * but even if not, we will just reallocate it later.
403 ntb_set_mw(nt, i, mw->tx_size);
407 qpo = imin(db_count, nt->mw_count);
410 snprintf(buf, sizeof(buf), "hint.%s.%d.config", device_get_name(dev),
411 device_get_unit(dev));
412 TUNABLE_STR_FETCH(buf, cfg, sizeof(cfg));
415 while ((c = strsep(&n, ",")) != NULL) {
417 name = strsep(&np, ":");
418 if (name != NULL && name[0] == 0)
420 qp = (np && np[0] != 0) ? strtol(np, NULL, 10) : qpo - qpu;
424 if (qp > qpt - qpu) {
425 device_printf(dev, "Not enough resources for config\n");
429 nc = malloc(sizeof(*nc), M_DEVBUF, M_WAITOK | M_ZERO);
433 nc->dev = device_add_child(dev, name, -1);
434 if (nc->dev == NULL) {
435 device_printf(dev, "Can not add child.\n");
438 device_set_ivars(nc->dev, nc);
443 device_printf(dev, "%d \"%s\": queues %d",
446 printf("-%d", qpu + qp - 1);
455 nt->qp_vec = malloc(nt->qp_count * sizeof(*nt->qp_vec), M_NTB_T,
458 for (i = 0; i < nt->qp_count; i++)
459 ntb_transport_init_queue(nt, i);
461 callout_init(&nt->link_work, 0);
462 callout_init(&nt->link_watchdog, 0);
463 TASK_INIT(&nt->link_cleanup, 0, ntb_transport_link_cleanup_work, nt);
464 nt->link_is_up = false;
466 rc = ntb_set_ctx(dev, nt, &ntb_transport_ops);
470 ntb_link_enable(dev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
472 for (i = 0; i < nt->mw_count; i++) {
474 rc = ntb_mw_set_trans(nt->dev, i, mw->dma_addr, mw->buff_size);
476 ntb_printf(0, "load time mw%d xlat fails, rc %d\n", i, rc);
479 if (enable_xeon_watchdog != 0)
480 callout_reset(&nt->link_watchdog, 0, xeon_link_watchdog_hb, nt);
482 bus_generic_attach(dev);
486 free(nt->qp_vec, M_NTB_T);
487 free(nt->mw_vec, M_NTB_T);
492 ntb_transport_detach(device_t dev)
494 struct ntb_transport_ctx *nt = device_get_softc(dev);
495 struct ntb_transport_child **cpp = &nt->child;
496 struct ntb_transport_child *nc;
499 while ((nc = *cpp) != NULL) {
501 error = device_delete_child(dev, nc->dev);
506 KASSERT(nt->qp_bitmap == 0,
507 ("Some queues not freed on detach (%jx)", nt->qp_bitmap));
509 ntb_transport_link_cleanup(nt);
510 taskqueue_drain(taskqueue_swi, &nt->link_cleanup);
511 callout_drain(&nt->link_work);
512 callout_drain(&nt->link_watchdog);
514 ntb_link_disable(dev);
517 for (i = 0; i < nt->mw_count; i++)
520 free(nt->qp_vec, M_NTB_T);
521 free(nt->mw_vec, M_NTB_T);
526 ntb_transport_print_child(device_t dev, device_t child)
528 struct ntb_transport_child *nc = device_get_ivars(child);
531 retval = bus_print_child_header(dev, child);
533 printf(" queue %d", nc->qpoff);
535 printf("-%d", nc->qpoff + nc->qpcnt - 1);
537 retval += printf(" at consumer %d", nc->consumer);
538 retval += bus_print_child_domain(dev, child);
539 retval += bus_print_child_footer(dev, child);
545 ntb_transport_child_location_str(device_t dev, device_t child, char *buf,
548 struct ntb_transport_child *nc = device_get_ivars(child);
550 snprintf(buf, buflen, "consumer=%d", nc->consumer);
555 ntb_transport_queue_count(device_t dev)
557 struct ntb_transport_child *nc = device_get_ivars(dev);
563 ntb_transport_init_queue(struct ntb_transport_ctx *nt, unsigned int qp_num)
565 struct ntb_transport_mw *mw;
566 struct ntb_transport_qp *qp;
570 unsigned num_qps_mw, mw_num, mw_count;
572 mw_count = nt->mw_count;
573 mw_num = QP_TO_MW(nt, qp_num);
574 mw = &nt->mw_vec[mw_num];
576 qp = &nt->qp_vec[qp_num];
580 qp->client_ready = false;
581 qp->event_handler = NULL;
582 ntb_qp_link_down_reset(qp);
584 if (mw_num < nt->qp_count % mw_count)
585 num_qps_mw = nt->qp_count / mw_count + 1;
587 num_qps_mw = nt->qp_count / mw_count;
589 mw_base = mw->phys_addr;
591 tx_size = mw->tx_size / num_qps_mw;
592 qp_offset = tx_size * (qp_num / mw_count);
594 qp->tx_mw = mw->vbase + qp_offset;
595 KASSERT(qp->tx_mw != NULL, ("uh oh?"));
597 /* XXX Assumes that a vm_paddr_t is equivalent to bus_addr_t */
598 qp->tx_mw_phys = mw_base + qp_offset;
599 KASSERT(qp->tx_mw_phys != 0, ("uh oh?"));
601 tx_size -= sizeof(struct ntb_rx_info);
602 qp->rx_info = (void *)(qp->tx_mw + tx_size);
604 /* Due to house-keeping, there must be at least 2 buffs */
605 qp->tx_max_frame = qmin(transport_mtu, tx_size / 2);
606 qp->tx_max_entry = tx_size / qp->tx_max_frame;
608 callout_init(&qp->link_work, 0);
609 callout_init(&qp->rx_full, 1);
611 mtx_init(&qp->ntb_rx_q_lock, "ntb rx q", NULL, MTX_SPIN);
612 mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN);
613 mtx_init(&qp->tx_lock, "ntb transport tx", NULL, MTX_DEF);
614 TASK_INIT(&qp->rxc_db_work, 0, ntb_transport_rxc_db, qp);
615 qp->rxc_tq = taskqueue_create("ntbt_rx", M_WAITOK,
616 taskqueue_thread_enqueue, &qp->rxc_tq);
617 taskqueue_start_threads(&qp->rxc_tq, 1, PI_NET, "%s rx%d",
618 device_get_nameunit(nt->dev), qp_num);
620 STAILQ_INIT(&qp->rx_post_q);
621 STAILQ_INIT(&qp->rx_pend_q);
622 STAILQ_INIT(&qp->tx_free_q);
626 ntb_transport_free_queue(struct ntb_transport_qp *qp)
628 struct ntb_transport_ctx *nt = qp->transport;
629 struct ntb_queue_entry *entry;
631 callout_drain(&qp->link_work);
633 ntb_db_set_mask(qp->dev, 1ull << qp->qp_num);
634 taskqueue_drain_all(qp->rxc_tq);
635 taskqueue_free(qp->rxc_tq);
638 qp->rx_handler = NULL;
639 qp->tx_handler = NULL;
640 qp->event_handler = NULL;
642 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q)))
643 free(entry, M_NTB_T);
645 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q)))
646 free(entry, M_NTB_T);
648 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
649 free(entry, M_NTB_T);
651 nt->qp_bitmap &= ~(1 << qp->qp_num);
655 * ntb_transport_create_queue - Create a new NTB transport layer queue
656 * @rx_handler: receive callback function
657 * @tx_handler: transmit callback function
658 * @event_handler: event callback function
660 * Create a new NTB transport layer queue and provide the queue with a callback
661 * routine for both transmit and receive. The receive callback routine will be
662 * used to pass up data when the transport has received it on the queue. The
663 * transmit callback routine will be called when the transport has completed the
664 * transmission of the data on the queue and the data is ready to be freed.
666 * RETURNS: pointer to newly created ntb_queue, NULL on error.
668 struct ntb_transport_qp *
669 ntb_transport_create_queue(device_t dev, int q,
670 const struct ntb_queue_handlers *handlers, void *data)
672 struct ntb_transport_child *nc = device_get_ivars(dev);
673 struct ntb_transport_ctx *nt = device_get_softc(device_get_parent(dev));
674 struct ntb_queue_entry *entry;
675 struct ntb_transport_qp *qp;
678 if (q < 0 || q >= nc->qpcnt)
681 qp = &nt->qp_vec[nc->qpoff + q];
682 nt->qp_bitmap |= (1 << qp->qp_num);
684 qp->rx_handler = handlers->rx_handler;
685 qp->tx_handler = handlers->tx_handler;
686 qp->event_handler = handlers->event_handler;
688 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
689 entry = malloc(sizeof(*entry), M_NTB_T, M_WAITOK | M_ZERO);
690 entry->cb_data = data;
692 entry->len = transport_mtu;
694 ntb_list_add(&qp->ntb_rx_q_lock, entry, &qp->rx_pend_q);
697 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
698 entry = malloc(sizeof(*entry), M_NTB_T, M_WAITOK | M_ZERO);
700 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
703 ntb_db_clear(dev, 1ull << qp->qp_num);
708 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
709 * @qp: NTB transport layer queue to be enabled
711 * Notify NTB transport layer of client readiness to use queue
714 ntb_transport_link_up(struct ntb_transport_qp *qp)
716 struct ntb_transport_ctx *nt = qp->transport;
718 qp->client_ready = true;
720 ntb_printf(2, "qp %d client ready\n", qp->qp_num);
723 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
731 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
732 * @qp: NTB transport layer queue the entry is to be enqueued on
733 * @cb: per buffer pointer for callback function to use
734 * @data: pointer to data buffer that will be sent
735 * @len: length of the data buffer
737 * Enqueue a new transmit buffer onto the transport queue from which a NTB
738 * payload will be transmitted. This assumes that a lock is being held to
739 * serialize access to the qp.
741 * RETURNS: An appropriate ERRNO error value on error, or zero for success.
744 ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
747 struct ntb_queue_entry *entry;
750 if (!qp->link_is_up || len == 0) {
751 CTR0(KTR_NTB, "TX: link not up");
755 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
757 CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
761 CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
768 mtx_lock(&qp->tx_lock);
769 rc = ntb_process_tx(qp, entry);
770 mtx_unlock(&qp->tx_lock);
772 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
774 "TX: process_tx failed. Returning entry %p to tx_free_q",
781 ntb_tx_copy_callback(void *data)
783 struct ntb_queue_entry *entry = data;
784 struct ntb_transport_qp *qp = entry->qp;
785 struct ntb_payload_header *hdr = entry->x_hdr;
787 iowrite32(entry->flags | NTBT_DESC_DONE_FLAG, &hdr->flags);
788 CTR1(KTR_NTB, "TX: hdr %p set DESC_DONE", hdr);
790 ntb_peer_db_set(qp->dev, 1ull << qp->qp_num);
793 * The entry length can only be zero if the packet is intended to be a
794 * "link down" or similar. Since no payload is being sent in these
795 * cases, there is nothing to add to the completion queue.
797 if (entry->len > 0) {
798 qp->tx_bytes += entry->len;
801 qp->tx_handler(qp, qp->cb_data, entry->buf,
809 "TX: entry %p sent. hdr->ver = %u, hdr->flags = 0x%x, Returning "
810 "to tx_free_q", entry, hdr->ver, hdr->flags);
811 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
815 ntb_memcpy_tx(struct ntb_queue_entry *entry, void *offset)
818 CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset);
819 if (entry->buf != NULL) {
820 m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset);
823 * Ensure that the data is fully copied before setting the
829 ntb_tx_copy_callback(entry);
833 ntb_async_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
835 struct ntb_payload_header *hdr;
838 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
839 hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame -
840 sizeof(struct ntb_payload_header));
843 iowrite32(entry->len, &hdr->len);
844 iowrite32(qp->tx_pkts, &hdr->ver);
846 ntb_memcpy_tx(entry, offset);
850 ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
854 "TX: process_tx: tx_pkts=%lu, tx_index=%u, remote entry=%u",
855 qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry);
856 if (qp->tx_index == qp->remote_rx_info->entry) {
857 CTR0(KTR_NTB, "TX: ring full");
862 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
863 if (qp->tx_handler != NULL)
864 qp->tx_handler(qp, qp->cb_data, entry->buf,
870 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
872 "TX: frame too big. returning entry %p to tx_free_q",
876 CTR2(KTR_NTB, "TX: copying entry %p to index %u", entry, qp->tx_index);
877 ntb_async_tx(qp, entry);
880 qp->tx_index %= qp->tx_max_entry;
889 ntb_transport_rxc_db(void *arg, int pending __unused)
891 struct ntb_transport_qp *qp = arg;
892 uint64_t qp_mask = 1ull << qp->qp_num;
895 CTR0(KTR_NTB, "RX: transport_rx");
897 while ((rc = ntb_process_rxc(qp)) == 0)
899 CTR1(KTR_NTB, "RX: process_rxc returned %d", rc);
901 if ((ntb_db_read(qp->dev) & qp_mask) != 0) {
902 /* If db is set, clear it and check queue once more. */
903 ntb_db_clear(qp->dev, qp_mask);
907 ntb_db_clear_mask(qp->dev, qp_mask);
911 ntb_process_rxc(struct ntb_transport_qp *qp)
913 struct ntb_payload_header *hdr;
914 struct ntb_queue_entry *entry;
917 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
918 hdr = (void *)(offset + qp->rx_max_frame -
919 sizeof(struct ntb_payload_header));
921 CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index);
922 if ((hdr->flags & NTBT_DESC_DONE_FLAG) == 0) {
923 CTR0(KTR_NTB, "RX: hdr not done");
928 if ((hdr->flags & NTBT_LINK_DOWN_FLAG) != 0) {
929 CTR0(KTR_NTB, "RX: link down");
930 ntb_qp_link_down(qp);
935 if (hdr->ver != (uint32_t)qp->rx_pkts) {
936 CTR2(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). "
937 "Returning entry to rx_pend_q", hdr->ver, qp->rx_pkts);
942 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
945 CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
948 callout_stop(&qp->rx_full);
949 CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
952 entry->index = qp->rx_index;
954 if (hdr->len > entry->len) {
955 CTR2(KTR_NTB, "RX: len too long. Wanted %ju got %ju",
956 (uintmax_t)hdr->len, (uintmax_t)entry->len);
960 entry->flags |= NTBT_DESC_DONE_FLAG;
962 ntb_complete_rxc(qp);
964 qp->rx_bytes += hdr->len;
967 CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
969 entry->len = hdr->len;
971 ntb_memcpy_rx(qp, entry, offset);
975 qp->rx_index %= qp->rx_max_entry;
980 ntb_memcpy_rx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
983 struct ifnet *ifp = entry->cb_data;
984 unsigned int len = entry->len;
986 CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset);
988 entry->buf = (void *)m_devget(offset, len, 0, ifp, NULL);
989 if (entry->buf == NULL)
990 entry->len = -ENOMEM;
992 /* Ensure that the data is globally visible before clearing the flag */
995 CTR2(KTR_NTB, "RX: copied entry %p to mbuf %p.", entry, entry->buf);
996 ntb_rx_copy_callback(qp, entry);
1000 ntb_rx_copy_callback(struct ntb_transport_qp *qp, void *data)
1002 struct ntb_queue_entry *entry;
1005 entry->flags |= NTBT_DESC_DONE_FLAG;
1006 ntb_complete_rxc(qp);
1010 ntb_complete_rxc(struct ntb_transport_qp *qp)
1012 struct ntb_queue_entry *entry;
1016 CTR0(KTR_NTB, "RX: rx_completion_task");
1018 mtx_lock_spin(&qp->ntb_rx_q_lock);
1020 while (!STAILQ_EMPTY(&qp->rx_post_q)) {
1021 entry = STAILQ_FIRST(&qp->rx_post_q);
1022 if ((entry->flags & NTBT_DESC_DONE_FLAG) == 0)
1025 entry->x_hdr->flags = 0;
1026 iowrite32(entry->index, &qp->rx_info->entry);
1028 STAILQ_REMOVE_HEAD(&qp->rx_post_q, entry);
1034 * Re-initialize queue_entry for reuse; rx_handler takes
1035 * ownership of the mbuf.
1038 entry->len = transport_mtu;
1039 entry->cb_data = qp->cb_data;
1041 STAILQ_INSERT_TAIL(&qp->rx_pend_q, entry, entry);
1043 mtx_unlock_spin(&qp->ntb_rx_q_lock);
1045 CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m);
1046 if (qp->rx_handler != NULL && qp->client_ready)
1047 qp->rx_handler(qp, qp->cb_data, m, len);
1051 mtx_lock_spin(&qp->ntb_rx_q_lock);
1054 mtx_unlock_spin(&qp->ntb_rx_q_lock);
1058 ntb_transport_doorbell_callback(void *data, uint32_t vector)
1060 struct ntb_transport_ctx *nt = data;
1061 struct ntb_transport_qp *qp;
1065 vec_mask = ntb_db_vector_mask(nt->dev, vector);
1066 vec_mask &= nt->qp_bitmap;
1067 if ((vec_mask & (vec_mask - 1)) != 0)
1068 vec_mask &= ntb_db_read(nt->dev);
1069 if (vec_mask != 0) {
1070 ntb_db_set_mask(nt->dev, vec_mask);
1071 ntb_db_clear(nt->dev, vec_mask);
1073 while (vec_mask != 0) {
1074 qp_num = ffsll(vec_mask) - 1;
1076 qp = &nt->qp_vec[qp_num];
1078 taskqueue_enqueue(qp->rxc_tq, &qp->rxc_db_work);
1080 vec_mask &= ~(1ull << qp_num);
1084 /* Link Event handler */
1086 ntb_transport_event_callback(void *data)
1088 struct ntb_transport_ctx *nt = data;
1090 if (ntb_link_is_up(nt->dev, &nt->link_speed, &nt->link_width)) {
1091 ntb_printf(1, "HW link up\n");
1092 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
1094 ntb_printf(1, "HW link down\n");
1095 taskqueue_enqueue(taskqueue_swi, &nt->link_cleanup);
1101 ntb_transport_link_work(void *arg)
1103 struct ntb_transport_ctx *nt = arg;
1104 struct ntb_transport_mw *mw;
1105 device_t dev = nt->dev;
1106 struct ntb_transport_qp *qp;
1107 uint64_t val64, size;
1112 /* send the local info, in the opposite order of the way we read it */
1113 for (i = 0; i < nt->mw_count; i++) {
1114 size = nt->mw_vec[i].tx_size;
1115 ntb_peer_spad_write(dev, NTBT_MW0_SZ_HIGH + (i * 2),
1117 ntb_peer_spad_write(dev, NTBT_MW0_SZ_LOW + (i * 2), size);
1119 ntb_peer_spad_write(dev, NTBT_NUM_MWS, nt->mw_count);
1120 ntb_peer_spad_write(dev, NTBT_NUM_QPS, nt->qp_count);
1121 ntb_peer_spad_write(dev, NTBT_QP_LINKS, 0);
1122 ntb_peer_spad_write(dev, NTBT_VERSION, NTB_TRANSPORT_VERSION);
1124 /* Query the remote side for its info */
1126 ntb_spad_read(dev, NTBT_VERSION, &val);
1127 if (val != NTB_TRANSPORT_VERSION)
1130 ntb_spad_read(dev, NTBT_NUM_QPS, &val);
1131 if (val != nt->qp_count)
1134 ntb_spad_read(dev, NTBT_NUM_MWS, &val);
1135 if (val != nt->mw_count)
1138 for (i = 0; i < nt->mw_count; i++) {
1139 ntb_spad_read(dev, NTBT_MW0_SZ_HIGH + (i * 2), &val);
1140 val64 = (uint64_t)val << 32;
1142 ntb_spad_read(dev, NTBT_MW0_SZ_LOW + (i * 2), &val);
1145 mw = &nt->mw_vec[i];
1146 mw->rx_size = val64;
1147 val64 = roundup(val64, mw->xlat_align_size);
1148 if (mw->buff_size != val64) {
1150 rc = ntb_set_mw(nt, i, val64);
1152 ntb_printf(0, "link up set mw%d fails, rc %d\n",
1157 /* Notify HW the memory location of the receive buffer */
1158 rc = ntb_mw_set_trans(nt->dev, i, mw->dma_addr,
1161 ntb_printf(0, "link up mw%d xlat fails, rc %d\n",
1168 nt->link_is_up = true;
1169 ntb_printf(1, "transport link up\n");
1171 for (i = 0; i < nt->qp_count; i++) {
1172 qp = &nt->qp_vec[i];
1174 ntb_transport_setup_qp_mw(nt, i);
1176 if (qp->client_ready)
1177 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1183 for (i = 0; i < nt->mw_count; i++)
1186 if (ntb_link_is_up(dev, &nt->link_speed, &nt->link_width))
1187 callout_reset(&nt->link_work,
1188 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt);
1191 struct ntb_load_cb_args {
1197 ntb_load_cb(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
1199 struct ntb_load_cb_args *cba = (struct ntb_load_cb_args *)xsc;
1201 if (!(cba->error = error))
1202 cba->addr = segs[0].ds_addr;
1206 ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, size_t size)
1208 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1209 struct ntb_load_cb_args cba;
1215 buff_size = roundup(size, mw->xlat_align_size);
1217 /* No need to re-setup */
1218 if (mw->buff_size == buff_size)
1221 if (mw->buff_size != 0)
1222 ntb_free_mw(nt, num_mw);
1224 /* Alloc memory for receiving data. Must be aligned */
1225 mw->buff_size = buff_size;
1227 if (bus_dma_tag_create(bus_get_dma_tag(nt->dev), mw->xlat_align, 0,
1228 mw->addr_limit, BUS_SPACE_MAXADDR,
1229 NULL, NULL, mw->buff_size, 1, mw->buff_size,
1230 0, NULL, NULL, &mw->dma_tag)) {
1231 ntb_printf(0, "Unable to create MW tag of size %zu\n",
1236 if (bus_dmamem_alloc(mw->dma_tag, (void **)&mw->virt_addr,
1237 BUS_DMA_WAITOK | BUS_DMA_ZERO, &mw->dma_map)) {
1238 bus_dma_tag_destroy(mw->dma_tag);
1239 ntb_printf(0, "Unable to allocate MW buffer of size %zu\n",
1244 if (bus_dmamap_load(mw->dma_tag, mw->dma_map, mw->virt_addr,
1245 mw->buff_size, ntb_load_cb, &cba, BUS_DMA_NOWAIT) || cba.error) {
1246 bus_dmamem_free(mw->dma_tag, mw->virt_addr, mw->dma_map);
1247 bus_dma_tag_destroy(mw->dma_tag);
1248 ntb_printf(0, "Unable to load MW buffer of size %zu\n",
1253 mw->dma_addr = cba.addr;
1259 ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
1261 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1263 if (mw->virt_addr == NULL)
1266 ntb_mw_clear_trans(nt->dev, num_mw);
1267 bus_dmamap_unload(mw->dma_tag, mw->dma_map);
1268 bus_dmamem_free(mw->dma_tag, mw->virt_addr, mw->dma_map);
1269 bus_dma_tag_destroy(mw->dma_tag);
1271 mw->virt_addr = NULL;
1275 ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, unsigned int qp_num)
1277 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
1278 struct ntb_transport_mw *mw;
1282 unsigned num_qps_mw, mw_num, mw_count;
1284 mw_count = nt->mw_count;
1285 mw_num = QP_TO_MW(nt, qp_num);
1286 mw = &nt->mw_vec[mw_num];
1288 if (mw->virt_addr == NULL)
1291 if (mw_num < nt->qp_count % mw_count)
1292 num_qps_mw = nt->qp_count / mw_count + 1;
1294 num_qps_mw = nt->qp_count / mw_count;
1296 rx_size = mw->rx_size / num_qps_mw;
1297 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
1298 rx_size -= sizeof(struct ntb_rx_info);
1300 qp->remote_rx_info = (void*)(qp->rx_buff + rx_size);
1302 /* Due to house-keeping, there must be at least 2 buffs */
1303 qp->rx_max_frame = qmin(transport_mtu, rx_size / 2);
1304 qp->rx_max_entry = rx_size / qp->rx_max_frame;
1307 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
1309 /* Set up the hdr offsets with 0s */
1310 for (i = 0; i < qp->rx_max_entry; i++) {
1311 offset = (void *)(qp->rx_buff + qp->rx_max_frame * (i + 1) -
1312 sizeof(struct ntb_payload_header));
1313 memset(offset, 0, sizeof(struct ntb_payload_header));
1324 ntb_qp_link_work(void *arg)
1326 struct ntb_transport_qp *qp = arg;
1327 device_t dev = qp->dev;
1328 struct ntb_transport_ctx *nt = qp->transport;
1332 /* Report queues that are up on our side */
1333 for (i = 0, val = 0; i < nt->qp_count; i++) {
1334 if (nt->qp_vec[i].client_ready)
1337 ntb_peer_spad_write(dev, NTBT_QP_LINKS, val);
1339 /* See if the remote side is up */
1340 ntb_spad_read(dev, NTBT_QP_LINKS, &val);
1341 if ((val & (1ull << qp->qp_num)) != 0) {
1342 ntb_printf(2, "qp %d link up\n", qp->qp_num);
1343 qp->link_is_up = true;
1345 if (qp->event_handler != NULL)
1346 qp->event_handler(qp->cb_data, NTB_LINK_UP);
1348 ntb_db_clear_mask(dev, 1ull << qp->qp_num);
1349 } else if (nt->link_is_up)
1350 callout_reset(&qp->link_work,
1351 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1354 /* Link down event*/
1356 ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
1358 struct ntb_transport_qp *qp;
1361 callout_drain(&nt->link_work);
1364 /* Pass along the info to any clients */
1365 for (i = 0; i < nt->qp_count; i++) {
1366 if ((nt->qp_bitmap & (1 << i)) != 0) {
1367 qp = &nt->qp_vec[i];
1368 ntb_qp_link_cleanup(qp);
1369 callout_drain(&qp->link_work);
1374 * The scratchpad registers keep the values if the remote side
1375 * goes down, blast them now to give them a sane value the next
1376 * time they are accessed
1378 ntb_spad_clear(nt->dev);
1382 ntb_transport_link_cleanup_work(void *arg, int pending __unused)
1385 ntb_transport_link_cleanup(arg);
1389 ntb_qp_link_down(struct ntb_transport_qp *qp)
1392 ntb_qp_link_cleanup(qp);
1396 ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
1399 qp->link_is_up = false;
1400 ntb_db_set_mask(qp->dev, 1ull << qp->qp_num);
1402 qp->tx_index = qp->rx_index = 0;
1403 qp->tx_bytes = qp->rx_bytes = 0;
1404 qp->tx_pkts = qp->rx_pkts = 0;
1406 qp->rx_ring_empty = 0;
1407 qp->tx_ring_full = 0;
1409 qp->rx_err_no_buf = qp->tx_err_no_buf = 0;
1410 qp->rx_err_oflow = qp->rx_err_ver = 0;
1414 ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1417 callout_drain(&qp->link_work);
1418 ntb_qp_link_down_reset(qp);
1420 if (qp->event_handler != NULL)
1421 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1424 /* Link commanded down */
1426 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1427 * @qp: NTB transport layer queue to be disabled
1429 * Notify NTB transport layer of client's desire to no longer receive data on
1430 * transport queue specified. It is the client's responsibility to ensure all
1431 * entries on queue are purged or otherwise handled appropriately.
1434 ntb_transport_link_down(struct ntb_transport_qp *qp)
1436 struct ntb_transport_ctx *nt = qp->transport;
1440 qp->client_ready = false;
1441 for (i = 0, val = 0; i < nt->qp_count; i++) {
1442 if (nt->qp_vec[i].client_ready)
1445 ntb_peer_spad_write(qp->dev, NTBT_QP_LINKS, val);
1448 ntb_send_link_down(qp);
1450 callout_drain(&qp->link_work);
1454 * ntb_transport_link_query - Query transport link state
1455 * @qp: NTB transport layer queue to be queried
1457 * Query connectivity to the remote system of the NTB transport queue
1459 * RETURNS: true for link up or false for link down
1462 ntb_transport_link_query(struct ntb_transport_qp *qp)
1465 return (qp->link_is_up);
1469 * ntb_transport_link_speed - Query transport link speed
1470 * @qp: NTB transport layer queue to be queried
1472 * Query connection speed to the remote system of the NTB transport queue
1474 * RETURNS: link speed in bits per second
1477 ntb_transport_link_speed(struct ntb_transport_qp *qp)
1479 struct ntb_transport_ctx *nt = qp->transport;
1482 if (!nt->link_is_up)
1484 switch (nt->link_speed) {
1485 case NTB_SPEED_GEN1:
1486 rate = 2500000000 * 8 / 10;
1488 case NTB_SPEED_GEN2:
1489 rate = 5000000000 * 8 / 10;
1491 case NTB_SPEED_GEN3:
1492 rate = 8000000000 * 128 / 130;
1494 case NTB_SPEED_GEN4:
1495 rate = 16000000000 * 128 / 130;
1500 if (nt->link_width <= 0)
1502 return (rate * nt->link_width);
1506 ntb_send_link_down(struct ntb_transport_qp *qp)
1508 struct ntb_queue_entry *entry;
1511 if (!qp->link_is_up)
1514 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1515 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1518 pause("NTB Wait for link down", hz / 10);
1524 entry->cb_data = NULL;
1527 entry->flags = NTBT_LINK_DOWN_FLAG;
1529 mtx_lock(&qp->tx_lock);
1530 rc = ntb_process_tx(qp, entry);
1531 mtx_unlock(&qp->tx_lock);
1533 printf("ntb: Failed to send link down\n");
1535 ntb_qp_link_down_reset(qp);
1539 /* List Management */
1542 ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1543 struct ntb_queue_list *list)
1546 mtx_lock_spin(lock);
1547 STAILQ_INSERT_TAIL(list, entry, entry);
1548 mtx_unlock_spin(lock);
1551 static struct ntb_queue_entry *
1552 ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1554 struct ntb_queue_entry *entry;
1556 mtx_lock_spin(lock);
1557 if (STAILQ_EMPTY(list)) {
1561 entry = STAILQ_FIRST(list);
1562 STAILQ_REMOVE_HEAD(list, entry);
1564 mtx_unlock_spin(lock);
1569 static struct ntb_queue_entry *
1570 ntb_list_mv(struct mtx *lock, struct ntb_queue_list *from,
1571 struct ntb_queue_list *to)
1573 struct ntb_queue_entry *entry;
1575 mtx_lock_spin(lock);
1576 if (STAILQ_EMPTY(from)) {
1580 entry = STAILQ_FIRST(from);
1581 STAILQ_REMOVE_HEAD(from, entry);
1582 STAILQ_INSERT_TAIL(to, entry, entry);
1585 mtx_unlock_spin(lock);
1590 * ntb_transport_qp_num - Query the qp number
1591 * @qp: NTB transport layer queue to be queried
1593 * Query qp number of the NTB transport queue
1595 * RETURNS: a zero based number specifying the qp number
1597 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1600 return (qp->qp_num);
1604 * ntb_transport_max_size - Query the max payload size of a qp
1605 * @qp: NTB transport layer queue to be queried
1607 * Query the maximum payload size permissible on the given qp
1609 * RETURNS: the max payload size of a qp
1612 ntb_transport_max_size(struct ntb_transport_qp *qp)
1615 return (qp->tx_max_frame - sizeof(struct ntb_payload_header));
1619 ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
1621 unsigned int head = qp->tx_index;
1622 unsigned int tail = qp->remote_rx_info->entry;
1624 return (tail >= head ? tail - head : qp->tx_max_entry + tail - head);
1627 static device_method_t ntb_transport_methods[] = {
1628 /* Device interface */
1629 DEVMETHOD(device_probe, ntb_transport_probe),
1630 DEVMETHOD(device_attach, ntb_transport_attach),
1631 DEVMETHOD(device_detach, ntb_transport_detach),
1633 DEVMETHOD(bus_child_location_str, ntb_transport_child_location_str),
1634 DEVMETHOD(bus_print_child, ntb_transport_print_child),
1638 devclass_t ntb_transport_devclass;
1639 static DEFINE_CLASS_0(ntb_transport, ntb_transport_driver,
1640 ntb_transport_methods, sizeof(struct ntb_transport_ctx));
1641 DRIVER_MODULE(ntb_transport, ntb_hw, ntb_transport_driver,
1642 ntb_transport_devclass, NULL, NULL);
1643 MODULE_DEPEND(ntb_transport, ntb, 1, 1, 1);
1644 MODULE_VERSION(ntb_transport, 1);