2 * Copyright (C) 2013 Intel Corporation
3 * Copyright (C) 2015 EMC Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/bitset.h>
37 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/if_var.h>
53 #include <net/ethernet.h>
58 #include <machine/bus.h>
59 #include <machine/cpufunc.h>
61 #include <netinet/in.h>
62 #include <netinet/ip.h>
64 #include "../ntb_hw/ntb_hw.h"
67 * The Non-Transparent Bridge (NTB) is a device on some Intel processors that
68 * allows you to connect two systems using a PCI-e link.
70 * This module contains a protocol for sending and receiving messages, and
71 * exposes that protocol through a simulated ethernet device called ntb.
73 * NOTE: Much of the code in this module is shared with Linux. Any patches may
74 * be picked up and redistributed in Linux with a dual GPL/BSD license.
78 BITSET_DEFINE(_qpset, QP_SETSIZE);
79 #define test_bit(pos, addr) BIT_ISSET(QP_SETSIZE, (pos), (addr))
80 #define set_bit(pos, addr) BIT_SET(QP_SETSIZE, (pos), (addr))
81 #define clear_bit(pos, addr) BIT_CLR(QP_SETSIZE, (pos), (addr))
82 #define ffs_bit(addr) BIT_FFS(QP_SETSIZE, (addr))
84 #define KTR_NTB KTR_SPARE3
86 #define NTB_TRANSPORT_VERSION 4
87 #define NTB_RX_MAX_PKTS 64
88 #define NTB_RXQ_SIZE 300
95 static SYSCTL_NODE(_hw, OID_AUTO, if_ntb, CTLFLAG_RW, 0, "if_ntb");
97 static unsigned g_if_ntb_debug_level;
98 SYSCTL_UINT(_hw_if_ntb, OID_AUTO, debug_level, CTLFLAG_RWTUN,
99 &g_if_ntb_debug_level, 0, "if_ntb log level -- higher is more verbose");
100 #define ntb_printf(lvl, ...) do { \
101 if ((lvl) <= g_if_ntb_debug_level) { \
102 if_printf(nt->ifp, __VA_ARGS__); \
106 static unsigned transport_mtu = IP_MAXPACKET + ETHER_HDR_LEN + ETHER_CRC_LEN;
108 static uint64_t max_mw_size;
109 SYSCTL_UQUAD(_hw_if_ntb, OID_AUTO, max_mw_size, CTLFLAG_RDTUN, &max_mw_size, 0,
110 "If enabled (non-zero), limit the size of large memory windows. "
111 "Both sides of the NTB MUST set the same value here.");
113 static unsigned max_num_clients;
114 SYSCTL_UINT(_hw_if_ntb, OID_AUTO, max_num_clients, CTLFLAG_RDTUN,
115 &max_num_clients, 0, "Maximum number of NTB transport clients. "
116 "0 (default) - use all available NTB memory windows; "
117 "positive integer N - Limit to N memory windows.");
119 static unsigned enable_xeon_watchdog;
120 SYSCTL_UINT(_hw_if_ntb, OID_AUTO, enable_xeon_watchdog, CTLFLAG_RDTUN,
121 &enable_xeon_watchdog, 0, "If non-zero, write a register every second to "
122 "keep a watchdog from tearing down the NTB link");
124 STAILQ_HEAD(ntb_queue_list, ntb_queue_entry);
126 typedef uint32_t ntb_q_idx_t;
128 struct ntb_queue_entry {
129 /* ntb_queue list reference */
130 STAILQ_ENTRY(ntb_queue_entry) entry;
132 /* info on data to be transferred */
138 struct ntb_transport_qp *qp;
139 struct ntb_payload_header *x_hdr;
147 struct ntb_transport_qp {
148 struct ntb_transport_ctx *transport;
149 struct ntb_softc *ntb;
154 volatile bool link_is_up;
155 uint8_t qp_num; /* Only 64 QPs are allowed. 0-63 */
157 struct ntb_rx_info *rx_info;
158 struct ntb_rx_info *remote_rx_info;
160 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
161 void *data, int len);
162 struct ntb_queue_list tx_free_q;
163 struct mtx ntb_tx_free_q_lock;
165 bus_addr_t tx_mw_phys;
166 ntb_q_idx_t tx_index;
167 ntb_q_idx_t tx_max_entry;
168 uint64_t tx_max_frame;
170 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
171 void *data, int len);
172 struct ntb_queue_list rx_post_q;
173 struct ntb_queue_list rx_pend_q;
174 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
175 struct mtx ntb_rx_q_lock;
176 struct task rx_completion_task;
177 struct task rxc_db_work;
179 ntb_q_idx_t rx_index;
180 ntb_q_idx_t rx_max_entry;
181 uint64_t rx_max_frame;
183 void (*event_handler)(void *data, enum ntb_link_event status);
184 struct callout link_work;
185 struct callout queue_full;
186 struct callout rx_full;
188 uint64_t last_rx_no_buf;
193 uint64_t rx_ring_empty;
194 uint64_t rx_err_no_buf;
195 uint64_t rx_err_oflow;
199 uint64_t tx_ring_full;
200 uint64_t tx_err_no_buf;
203 struct ntb_queue_handlers {
204 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
205 void *data, int len);
206 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
207 void *data, int len);
208 void (*event_handler)(void *data, enum ntb_link_event status);
211 struct ntb_transport_mw {
212 vm_paddr_t phys_addr;
215 size_t xlat_align_size;
216 bus_addr_t addr_limit;
217 /* Tx buff is off vbase / phys_addr */
221 /* Rx buff is off virt_addr / dma_addr */
226 struct ntb_transport_ctx {
227 struct ntb_softc *ntb;
229 struct ntb_transport_mw mw_vec[NTB_MAX_NUM_MW];
230 struct ntb_transport_qp *qp_vec;
231 struct _qpset qp_bitmap;
232 struct _qpset qp_bitmap_free;
235 volatile bool link_is_up;
236 struct callout link_work;
237 struct callout link_watchdog;
238 struct task link_cleanup;
240 u_char eaddr[ETHER_ADDR_LEN];
244 /* The hardcoded single queuepair in ntb_setup_interface() */
245 struct ntb_transport_qp *qp;
248 static struct ntb_transport_ctx net_softc;
251 IF_NTB_DESC_DONE_FLAG = 1 << 0,
252 IF_NTB_LINK_DOWN_FLAG = 1 << 1,
255 struct ntb_payload_header {
263 * The order of this enum is part of the if_ntb remote protocol. Do
264 * not reorder without bumping protocol version (and it's probably best
265 * to keep the protocol in lock-step with the Linux NTB driver.
272 * N.B.: transport_link_work assumes MW1 enums = MW0 + 2.
281 * Some NTB-using hardware have a watchdog to work around NTB hangs; if
282 * a register or doorbell isn't written every few seconds, the link is
283 * torn down. Write an otherwise unused register every few seconds to
284 * work around this watchdog.
286 IF_NTB_WATCHDOG_SPAD = 15
288 CTASSERT(IF_NTB_WATCHDOG_SPAD < XEON_SPAD_COUNT &&
289 IF_NTB_WATCHDOG_SPAD < ATOM_SPAD_COUNT);
291 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
292 #define NTB_QP_DEF_NUM_ENTRIES 100
293 #define NTB_LINK_DOWN_TIMEOUT 10
295 static int ntb_handle_module_events(struct module *m, int what, void *arg);
296 static int ntb_setup_interface(void);
297 static int ntb_teardown_interface(void);
298 static void ntb_net_init(void *arg);
299 static int ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
300 static void ntb_start(struct ifnet *ifp);
301 static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
302 void *data, int len);
303 static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
304 void *data, int len);
305 static void ntb_net_event_handler(void *data, enum ntb_link_event status);
306 static int ntb_transport_probe(struct ntb_softc *ntb);
307 static void ntb_transport_free(struct ntb_transport_ctx *);
308 static void ntb_transport_init_queue(struct ntb_transport_ctx *nt,
309 unsigned int qp_num);
310 static void ntb_transport_free_queue(struct ntb_transport_qp *qp);
311 static struct ntb_transport_qp *ntb_transport_create_queue(void *data,
312 struct ntb_softc *pdev, const struct ntb_queue_handlers *handlers);
313 static void ntb_transport_link_up(struct ntb_transport_qp *qp);
314 static int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb,
315 void *data, unsigned int len);
316 static int ntb_process_tx(struct ntb_transport_qp *qp,
317 struct ntb_queue_entry *entry);
318 static void ntb_memcpy_tx(struct ntb_transport_qp *qp,
319 struct ntb_queue_entry *entry, void *offset);
320 static void ntb_qp_full(void *arg);
321 static void ntb_transport_rxc_db(void *arg, int pending);
322 static int ntb_process_rxc(struct ntb_transport_qp *qp);
323 static void ntb_memcpy_rx(struct ntb_transport_qp *qp,
324 struct ntb_queue_entry *entry, void *offset);
325 static inline void ntb_rx_copy_callback(struct ntb_transport_qp *qp,
327 static void ntb_complete_rxc(void *arg, int pending);
328 static void ntb_transport_doorbell_callback(void *data, uint32_t vector);
329 static void ntb_transport_event_callback(void *data);
330 static void ntb_transport_link_work(void *arg);
331 static int ntb_set_mw(struct ntb_transport_ctx *, int num_mw, size_t size);
332 static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw);
333 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
334 unsigned int qp_num);
335 static void ntb_qp_link_work(void *arg);
336 static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt);
337 static void ntb_transport_link_cleanup_work(void *, int);
338 static void ntb_qp_link_down(struct ntb_transport_qp *qp);
339 static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp);
340 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp);
341 static void ntb_transport_link_down(struct ntb_transport_qp *qp);
342 static void ntb_send_link_down(struct ntb_transport_qp *qp);
343 static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
344 struct ntb_queue_list *list);
345 static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock,
346 struct ntb_queue_list *list);
347 static struct ntb_queue_entry *ntb_list_mv(struct mtx *lock,
348 struct ntb_queue_list *from, struct ntb_queue_list *to);
349 static void create_random_local_eui48(u_char *eaddr);
350 static unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp);
351 static void xeon_link_watchdog_hb(void *);
353 static const struct ntb_ctx_ops ntb_transport_ops = {
354 .link_event = ntb_transport_event_callback,
355 .db_event = ntb_transport_doorbell_callback,
358 MALLOC_DEFINE(M_NTB_IF, "if_ntb", "ntb network driver");
361 iowrite32(uint32_t val, void *addr)
364 bus_space_write_4(X86_BUS_SPACE_MEM, 0/* HACK */, (uintptr_t)addr,
368 /* Module setup and teardown */
370 ntb_handle_module_events(struct module *m, int what, void *arg)
376 err = ntb_setup_interface();
379 err = ntb_teardown_interface();
388 static moduledata_t if_ntb_mod = {
390 ntb_handle_module_events,
394 DECLARE_MODULE(if_ntb, if_ntb_mod, SI_SUB_KLD, SI_ORDER_ANY);
395 MODULE_DEPEND(if_ntb, ntb_hw, 1, 1, 1);
398 ntb_setup_interface(void)
401 struct ntb_queue_handlers handlers = { ntb_net_rx_handler,
402 ntb_net_tx_handler, ntb_net_event_handler };
405 net_softc.ntb = devclass_get_softc(devclass_find("ntb_hw"), 0);
406 if (net_softc.ntb == NULL) {
407 printf("ntb: Cannot find devclass\n");
411 ifp = net_softc.ifp = if_alloc(IFT_ETHER);
413 ntb_transport_free(&net_softc);
414 printf("ntb: Cannot allocate ifnet structure\n");
417 if_initname(ifp, "ntb", 0);
419 rc = ntb_transport_probe(net_softc.ntb);
421 printf("ntb: Cannot init transport: %d\n", rc);
422 if_free(net_softc.ifp);
426 net_softc.qp = ntb_transport_create_queue(ifp, net_softc.ntb,
428 ifp->if_init = ntb_net_init;
429 ifp->if_softc = &net_softc;
430 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
431 ifp->if_ioctl = ntb_ioctl;
432 ifp->if_start = ntb_start;
433 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
434 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
435 IFQ_SET_READY(&ifp->if_snd);
436 create_random_local_eui48(net_softc.eaddr);
437 ether_ifattach(ifp, net_softc.eaddr);
438 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_JUMBO_MTU;
439 ifp->if_capenable = ifp->if_capabilities;
440 ifp->if_mtu = ntb_transport_max_size(net_softc.qp) - ETHER_HDR_LEN -
443 ntb_transport_link_up(net_softc.qp);
444 net_softc.bufsize = ntb_transport_max_size(net_softc.qp) +
445 sizeof(struct ether_header);
450 ntb_teardown_interface(void)
453 if (net_softc.qp != NULL) {
454 ntb_transport_link_down(net_softc.qp);
456 ntb_transport_free_queue(net_softc.qp);
457 ntb_transport_free(&net_softc);
460 if (net_softc.ifp != NULL) {
461 ether_ifdetach(net_softc.ifp);
462 if_free(net_softc.ifp);
463 net_softc.ifp = NULL;
469 /* Network device interface */
472 ntb_net_init(void *arg)
474 struct ntb_transport_ctx *ntb_softc = arg;
475 struct ifnet *ifp = ntb_softc->ifp;
477 ifp->if_drv_flags |= IFF_DRV_RUNNING;
478 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
479 ifp->if_flags |= IFF_UP;
480 if_link_state_change(ifp, LINK_STATE_UP);
484 ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
486 struct ntb_transport_ctx *nt = ifp->if_softc;
487 struct ifreq *ifr = (struct ifreq *)data;
493 if (ifr->ifr_mtu > ntb_transport_max_size(nt->qp) -
494 ETHER_HDR_LEN - ETHER_CRC_LEN) {
499 ifp->if_mtu = ifr->ifr_mtu;
503 error = ether_ioctl(ifp, command, data);
512 ntb_start(struct ifnet *ifp)
515 struct ntb_transport_ctx *nt = ifp->if_softc;
518 mtx_lock(&nt->tx_lock);
519 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
520 CTR0(KTR_NTB, "TX: ntb_start");
521 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
522 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
523 CTR1(KTR_NTB, "TX: start mbuf %p", m_head);
524 rc = ntb_transport_tx_enqueue(nt->qp, m_head, m_head,
525 m_length(m_head, NULL));
528 "TX: could not tx mbuf %p. Returning to snd q",
531 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
532 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
533 callout_reset(&nt->qp->queue_full, hz / 1000,
540 mtx_unlock(&nt->tx_lock);
543 /* Network Device Callbacks */
545 ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
550 CTR1(KTR_NTB, "TX: tx_handler freeing mbuf %p", data);
554 ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
557 struct mbuf *m = data;
558 struct ifnet *ifp = qp_data;
560 CTR0(KTR_NTB, "RX: rx handler");
561 (*ifp->if_input)(ifp, m);
565 ntb_net_event_handler(void *data, enum ntb_link_event status)
572 /* XXX The Linux driver munges with the carrier status here. */
580 panic("Bogus ntb_link_event %u\n", status);
584 /* Transport Init and teardown */
587 xeon_link_watchdog_hb(void *arg)
589 struct ntb_transport_ctx *nt;
592 ntb_spad_write(nt->ntb, IF_NTB_WATCHDOG_SPAD, 0);
593 callout_reset(&nt->link_watchdog, 1 * hz, xeon_link_watchdog_hb, nt);
597 ntb_transport_probe(struct ntb_softc *ntb)
599 struct ntb_transport_ctx *nt = &net_softc;
600 struct ntb_transport_mw *mw;
605 nt->mw_count = ntb_mw_count(ntb);
606 for (i = 0; i < nt->mw_count; i++) {
609 rc = ntb_mw_get_range(ntb, i, &mw->phys_addr, &mw->vbase,
610 &mw->phys_size, &mw->xlat_align, &mw->xlat_align_size,
617 mw->virt_addr = NULL;
620 rc = ntb_mw_set_wc(nt->ntb, i, VM_MEMATTR_WRITE_COMBINING);
622 ntb_printf(0, "Unable to set mw%d caching\n", i);
625 qp_bitmap = ntb_db_valid_mask(ntb);
626 nt->qp_count = flsll(qp_bitmap);
627 KASSERT(nt->qp_count != 0, ("bogus db bitmap"));
630 if (max_num_clients != 0 && max_num_clients < nt->qp_count)
631 nt->qp_count = max_num_clients;
632 else if (nt->mw_count < nt->qp_count)
633 nt->qp_count = nt->mw_count;
634 KASSERT(nt->qp_count <= QP_SETSIZE, ("invalid qp_count"));
636 mtx_init(&nt->tx_lock, "ntb transport tx", NULL, MTX_DEF);
637 mtx_init(&nt->rx_lock, "ntb transport rx", NULL, MTX_DEF);
639 nt->qp_vec = malloc(nt->qp_count * sizeof(*nt->qp_vec), M_NTB_IF,
642 for (i = 0; i < nt->qp_count; i++) {
643 set_bit(i, &nt->qp_bitmap);
644 set_bit(i, &nt->qp_bitmap_free);
645 ntb_transport_init_queue(nt, i);
648 callout_init(&nt->link_work, 0);
649 callout_init(&nt->link_watchdog, 0);
650 TASK_INIT(&nt->link_cleanup, 0, ntb_transport_link_cleanup_work, nt);
652 rc = ntb_set_ctx(ntb, nt, &ntb_transport_ops);
656 nt->link_is_up = false;
657 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
660 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
661 if (enable_xeon_watchdog != 0)
662 callout_reset(&nt->link_watchdog, 0, xeon_link_watchdog_hb, nt);
666 free(nt->qp_vec, M_NTB_IF);
672 ntb_transport_free(struct ntb_transport_ctx *nt)
674 struct ntb_softc *ntb = nt->ntb;
675 struct _qpset qp_bitmap_alloc;
678 ntb_transport_link_cleanup(nt);
679 taskqueue_drain(taskqueue_swi, &nt->link_cleanup);
680 callout_drain(&nt->link_work);
681 callout_drain(&nt->link_watchdog);
683 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc);
684 BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free);
686 /* Verify that all the QPs are freed */
687 for (i = 0; i < nt->qp_count; i++)
688 if (test_bit(i, &qp_bitmap_alloc))
689 ntb_transport_free_queue(&nt->qp_vec[i]);
691 ntb_link_disable(ntb);
694 for (i = 0; i < nt->mw_count; i++)
697 free(nt->qp_vec, M_NTB_IF);
701 ntb_transport_init_queue(struct ntb_transport_ctx *nt, unsigned int qp_num)
703 struct ntb_transport_mw *mw;
704 struct ntb_transport_qp *qp;
706 uint64_t mw_size, qp_offset;
708 unsigned num_qps_mw, mw_num, mw_count;
710 mw_count = nt->mw_count;
711 mw_num = QP_TO_MW(nt, qp_num);
712 mw = &nt->mw_vec[mw_num];
714 qp = &nt->qp_vec[qp_num];
718 qp->client_ready = false;
719 qp->event_handler = NULL;
720 ntb_qp_link_down_reset(qp);
722 if (nt->qp_count % mw_count && mw_num + 1 < nt->qp_count / mw_count)
723 num_qps_mw = nt->qp_count / mw_count + 1;
725 num_qps_mw = nt->qp_count / mw_count;
727 mw_base = mw->phys_addr;
728 mw_size = mw->phys_size;
730 tx_size = mw_size / num_qps_mw;
731 qp_offset = tx_size * (qp_num / mw_count);
733 qp->tx_mw = mw->vbase + qp_offset;
734 KASSERT(qp->tx_mw != NULL, ("uh oh?"));
736 /* XXX Assumes that a vm_paddr_t is equivalent to bus_addr_t */
737 qp->tx_mw_phys = mw_base + qp_offset;
738 KASSERT(qp->tx_mw_phys != 0, ("uh oh?"));
740 tx_size -= sizeof(struct ntb_rx_info);
741 qp->rx_info = (void *)(qp->tx_mw + tx_size);
743 /* Due to house-keeping, there must be at least 2 buffs */
744 qp->tx_max_frame = qmin(tx_size / 2,
745 transport_mtu + sizeof(struct ntb_payload_header));
746 qp->tx_max_entry = tx_size / qp->tx_max_frame;
748 callout_init(&qp->link_work, 0);
749 callout_init(&qp->queue_full, 1);
750 callout_init(&qp->rx_full, 1);
752 mtx_init(&qp->ntb_rx_q_lock, "ntb rx q", NULL, MTX_SPIN);
753 mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN);
754 TASK_INIT(&qp->rx_completion_task, 0, ntb_complete_rxc, qp);
755 TASK_INIT(&qp->rxc_db_work, 0, ntb_transport_rxc_db, qp);
757 STAILQ_INIT(&qp->rx_post_q);
758 STAILQ_INIT(&qp->rx_pend_q);
759 STAILQ_INIT(&qp->tx_free_q);
761 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
765 ntb_transport_free_queue(struct ntb_transport_qp *qp)
767 struct ntb_queue_entry *entry;
772 callout_drain(&qp->link_work);
774 ntb_db_set_mask(qp->ntb, 1ull << qp->qp_num);
775 taskqueue_drain(taskqueue_swi, &qp->rxc_db_work);
776 taskqueue_drain(taskqueue_swi, &qp->rx_completion_task);
779 qp->rx_handler = NULL;
780 qp->tx_handler = NULL;
781 qp->event_handler = NULL;
783 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q)))
784 free(entry, M_NTB_IF);
786 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q)))
787 free(entry, M_NTB_IF);
789 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
790 free(entry, M_NTB_IF);
792 set_bit(qp->qp_num, &qp->transport->qp_bitmap_free);
796 * ntb_transport_create_queue - Create a new NTB transport layer queue
797 * @rx_handler: receive callback function
798 * @tx_handler: transmit callback function
799 * @event_handler: event callback function
801 * Create a new NTB transport layer queue and provide the queue with a callback
802 * routine for both transmit and receive. The receive callback routine will be
803 * used to pass up data when the transport has received it on the queue. The
804 * transmit callback routine will be called when the transport has completed the
805 * transmission of the data on the queue and the data is ready to be freed.
807 * RETURNS: pointer to newly created ntb_queue, NULL on error.
809 static struct ntb_transport_qp *
810 ntb_transport_create_queue(void *data, struct ntb_softc *ntb,
811 const struct ntb_queue_handlers *handlers)
813 struct ntb_queue_entry *entry;
814 struct ntb_transport_qp *qp;
815 struct ntb_transport_ctx *nt;
816 unsigned int free_queue;
819 nt = ntb_get_ctx(ntb, NULL);
820 KASSERT(nt != NULL, ("bogus"));
822 free_queue = ffs_bit(&nt->qp_bitmap);
826 /* decrement free_queue to make it zero based */
829 qp = &nt->qp_vec[free_queue];
830 clear_bit(qp->qp_num, &nt->qp_bitmap_free);
832 qp->rx_handler = handlers->rx_handler;
833 qp->tx_handler = handlers->tx_handler;
834 qp->event_handler = handlers->event_handler;
836 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
837 entry = malloc(sizeof(*entry), M_NTB_IF, M_WAITOK | M_ZERO);
838 entry->cb_data = nt->ifp;
840 entry->len = transport_mtu;
841 ntb_list_add(&qp->ntb_rx_q_lock, entry, &qp->rx_pend_q);
844 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
845 entry = malloc(sizeof(*entry), M_NTB_IF, M_WAITOK | M_ZERO);
846 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
849 ntb_db_clear(ntb, 1ull << qp->qp_num);
850 ntb_db_clear_mask(ntb, 1ull << qp->qp_num);
855 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
856 * @qp: NTB transport layer queue to be enabled
858 * Notify NTB transport layer of client readiness to use queue
861 ntb_transport_link_up(struct ntb_transport_qp *qp)
863 struct ntb_transport_ctx *nt;
868 qp->client_ready = true;
871 ntb_printf(2, "qp client ready\n");
873 if (qp->transport->link_is_up)
874 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
882 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
883 * @qp: NTB transport layer queue the entry is to be enqueued on
884 * @cb: per buffer pointer for callback function to use
885 * @data: pointer to data buffer that will be sent
886 * @len: length of the data buffer
888 * Enqueue a new transmit buffer onto the transport queue from which a NTB
889 * payload will be transmitted. This assumes that a lock is being held to
890 * serialize access to the qp.
892 * RETURNS: An appropriate ERRNO error value on error, or zero for success.
895 ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
898 struct ntb_queue_entry *entry;
901 if (qp == NULL || !qp->link_is_up || len == 0) {
902 CTR0(KTR_NTB, "TX: link not up");
906 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
908 CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
912 CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
919 rc = ntb_process_tx(qp, entry);
921 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
923 "TX: process_tx failed. Returning entry %p to tx_free_q",
930 ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
934 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
936 "TX: process_tx: tx_pkts=%lu, tx_index=%u, remote entry=%u",
937 qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry);
938 if (qp->tx_index == qp->remote_rx_info->entry) {
939 CTR0(KTR_NTB, "TX: ring full");
944 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
945 if (qp->tx_handler != NULL)
946 qp->tx_handler(qp, qp->cb_data, entry->buf,
952 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
954 "TX: frame too big. returning entry %p to tx_free_q",
958 CTR2(KTR_NTB, "TX: copying entry %p to offset %p", entry, offset);
959 ntb_memcpy_tx(qp, entry, offset);
962 qp->tx_index %= qp->tx_max_entry;
970 ntb_memcpy_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
973 struct ntb_payload_header *hdr;
975 /* This piece is from Linux' ntb_async_tx() */
976 hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame -
977 sizeof(struct ntb_payload_header));
979 iowrite32(entry->len, &hdr->len);
980 iowrite32(qp->tx_pkts, &hdr->ver);
982 /* This piece is ntb_memcpy_tx() */
983 CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset);
984 if (entry->buf != NULL) {
985 m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset);
988 * Ensure that the data is fully copied before setting the
994 /* The rest is ntb_tx_copy_callback() */
995 iowrite32(entry->flags | IF_NTB_DESC_DONE_FLAG, &hdr->flags);
996 CTR1(KTR_NTB, "TX: hdr %p set DESC_DONE", hdr);
998 ntb_peer_db_set(qp->ntb, 1ull << qp->qp_num);
1001 * The entry length can only be zero if the packet is intended to be a
1002 * "link down" or similar. Since no payload is being sent in these
1003 * cases, there is nothing to add to the completion queue.
1005 if (entry->len > 0) {
1006 qp->tx_bytes += entry->len;
1009 qp->tx_handler(qp, qp->cb_data, entry->buf,
1012 m_freem(entry->buf);
1017 "TX: entry %p sent. hdr->ver = %u, hdr->flags = 0x%x, Returning "
1018 "to tx_free_q", entry, hdr->ver, hdr->flags);
1019 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
1023 ntb_qp_full(void *arg)
1026 CTR0(KTR_NTB, "TX: qp_full callout");
1032 ntb_transport_rxc_db(void *arg, int pending __unused)
1034 struct ntb_transport_qp *qp = arg;
1039 * Limit the number of packets processed in a single interrupt to
1040 * provide fairness to others
1042 CTR0(KTR_NTB, "RX: transport_rx");
1043 mtx_lock(&qp->transport->rx_lock);
1044 for (i = 0; i < qp->rx_max_entry; i++) {
1045 rc = ntb_process_rxc(qp);
1047 CTR0(KTR_NTB, "RX: process_rxc failed");
1051 mtx_unlock(&qp->transport->rx_lock);
1053 if (i == qp->rx_max_entry)
1054 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1055 else if ((ntb_db_read(qp->ntb) & (1ull << qp->qp_num)) != 0) {
1056 /* If db is set, clear it and read it back to commit clear. */
1057 ntb_db_clear(qp->ntb, 1ull << qp->qp_num);
1058 (void)ntb_db_read(qp->ntb);
1061 * An interrupt may have arrived between finishing
1062 * ntb_process_rxc and clearing the doorbell bit: there might
1063 * be some more work to do.
1065 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1070 ntb_process_rxc(struct ntb_transport_qp *qp)
1072 struct ntb_payload_header *hdr;
1073 struct ntb_queue_entry *entry;
1076 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1077 hdr = (void *)(offset + qp->rx_max_frame -
1078 sizeof(struct ntb_payload_header));
1080 CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index);
1081 if ((hdr->flags & IF_NTB_DESC_DONE_FLAG) == 0) {
1082 CTR0(KTR_NTB, "RX: hdr not done");
1083 qp->rx_ring_empty++;
1087 if ((hdr->flags & IF_NTB_LINK_DOWN_FLAG) != 0) {
1088 CTR0(KTR_NTB, "RX: link down");
1089 ntb_qp_link_down(qp);
1094 if (hdr->ver != (uint32_t)qp->rx_pkts) {
1095 CTR2(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). "
1096 "Returning entry to rx_pend_q", hdr->ver, qp->rx_pkts);
1101 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
1102 if (entry == NULL) {
1103 qp->rx_err_no_buf++;
1104 CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
1107 callout_stop(&qp->rx_full);
1108 CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
1111 entry->index = qp->rx_index;
1113 if (hdr->len > entry->len) {
1114 CTR2(KTR_NTB, "RX: len too long. Wanted %ju got %ju",
1115 (uintmax_t)hdr->len, (uintmax_t)entry->len);
1119 entry->flags |= IF_NTB_DESC_DONE_FLAG;
1121 taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
1123 qp->rx_bytes += hdr->len;
1126 CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
1128 entry->len = hdr->len;
1130 ntb_memcpy_rx(qp, entry, offset);
1134 qp->rx_index %= qp->rx_max_entry;
1139 ntb_memcpy_rx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
1142 struct ifnet *ifp = entry->cb_data;
1143 unsigned int len = entry->len;
1146 CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset);
1147 m = m_devget(offset, len, 0, ifp, NULL);
1148 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID;
1150 entry->buf = (void *)m;
1152 /* Ensure that the data is globally visible before clearing the flag */
1155 CTR2(KTR_NTB, "RX: copied entry %p to mbuf %p.", entry, m);
1156 ntb_rx_copy_callback(qp, entry);
1160 ntb_rx_copy_callback(struct ntb_transport_qp *qp, void *data)
1162 struct ntb_queue_entry *entry;
1165 entry->flags |= IF_NTB_DESC_DONE_FLAG;
1166 taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
1170 ntb_complete_rxc(void *arg, int pending)
1172 struct ntb_transport_qp *qp = arg;
1173 struct ntb_queue_entry *entry;
1177 CTR0(KTR_NTB, "RX: rx_completion_task");
1179 mtx_lock_spin(&qp->ntb_rx_q_lock);
1181 while (!STAILQ_EMPTY(&qp->rx_post_q)) {
1182 entry = STAILQ_FIRST(&qp->rx_post_q);
1183 if ((entry->flags & IF_NTB_DESC_DONE_FLAG) == 0)
1186 entry->x_hdr->flags = 0;
1187 iowrite32(entry->index, &qp->rx_info->entry);
1189 STAILQ_REMOVE_HEAD(&qp->rx_post_q, entry);
1195 * Re-initialize queue_entry for reuse; rx_handler takes
1196 * ownership of the mbuf.
1199 entry->len = transport_mtu;
1200 entry->cb_data = qp->transport->ifp;
1202 STAILQ_INSERT_TAIL(&qp->rx_pend_q, entry, entry);
1204 mtx_unlock_spin(&qp->ntb_rx_q_lock);
1206 CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m);
1207 if (qp->rx_handler != NULL && qp->client_ready)
1208 qp->rx_handler(qp, qp->cb_data, m, len);
1212 mtx_lock_spin(&qp->ntb_rx_q_lock);
1215 mtx_unlock_spin(&qp->ntb_rx_q_lock);
1219 ntb_transport_doorbell_callback(void *data, uint32_t vector)
1221 struct ntb_transport_ctx *nt = data;
1222 struct ntb_transport_qp *qp;
1223 struct _qpset db_bits;
1227 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &db_bits);
1228 BIT_NAND(QP_SETSIZE, &db_bits, &nt->qp_bitmap_free);
1230 vec_mask = ntb_db_vector_mask(nt->ntb, vector);
1231 while (vec_mask != 0) {
1232 qp_num = ffsll(vec_mask) - 1;
1234 if (test_bit(qp_num, &db_bits)) {
1235 qp = &nt->qp_vec[qp_num];
1236 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1239 vec_mask &= ~(1ull << qp_num);
1243 /* Link Event handler */
1245 ntb_transport_event_callback(void *data)
1247 struct ntb_transport_ctx *nt = data;
1249 if (ntb_link_is_up(nt->ntb, NULL, NULL)) {
1250 ntb_printf(1, "HW link up\n");
1251 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
1253 ntb_printf(1, "HW link down\n");
1254 taskqueue_enqueue(taskqueue_swi, &nt->link_cleanup);
1260 ntb_transport_link_work(void *arg)
1262 struct ntb_transport_ctx *nt = arg;
1263 struct ntb_softc *ntb = nt->ntb;
1264 struct ntb_transport_qp *qp;
1265 uint64_t val64, size;
1270 /* send the local info, in the opposite order of the way we read it */
1271 for (i = 0; i < nt->mw_count; i++) {
1272 size = nt->mw_vec[i].phys_size;
1274 if (max_mw_size != 0 && size > max_mw_size)
1277 ntb_peer_spad_write(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2),
1279 ntb_peer_spad_write(ntb, IF_NTB_MW0_SZ_LOW + (i * 2), size);
1282 ntb_peer_spad_write(ntb, IF_NTB_NUM_MWS, nt->mw_count);
1284 ntb_peer_spad_write(ntb, IF_NTB_NUM_QPS, nt->qp_count);
1286 ntb_peer_spad_write(ntb, IF_NTB_VERSION, NTB_TRANSPORT_VERSION);
1288 /* Query the remote side for its info */
1290 ntb_spad_read(ntb, IF_NTB_VERSION, &val);
1291 if (val != NTB_TRANSPORT_VERSION)
1294 ntb_spad_read(ntb, IF_NTB_NUM_QPS, &val);
1295 if (val != nt->qp_count)
1298 ntb_spad_read(ntb, IF_NTB_NUM_MWS, &val);
1299 if (val != nt->mw_count)
1302 for (i = 0; i < nt->mw_count; i++) {
1303 ntb_spad_read(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2), &val);
1304 val64 = (uint64_t)val << 32;
1306 ntb_spad_read(ntb, IF_NTB_MW0_SZ_LOW + (i * 2), &val);
1309 rc = ntb_set_mw(nt, i, val64);
1314 nt->link_is_up = true;
1315 ntb_printf(1, "transport link up\n");
1317 for (i = 0; i < nt->qp_count; i++) {
1318 qp = &nt->qp_vec[i];
1320 ntb_transport_setup_qp_mw(nt, i);
1322 if (qp->client_ready)
1323 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1329 for (i = 0; i < nt->mw_count; i++)
1332 if (ntb_link_is_up(ntb, NULL, NULL))
1333 callout_reset(&nt->link_work,
1334 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt);
1338 ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, size_t size)
1340 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1341 size_t xlat_size, buff_size;
1347 xlat_size = roundup(size, mw->xlat_align_size);
1348 buff_size = xlat_size;
1350 /* No need to re-setup */
1351 if (mw->xlat_size == xlat_size)
1354 if (mw->buff_size != 0)
1355 ntb_free_mw(nt, num_mw);
1357 /* Alloc memory for receiving data. Must be aligned */
1358 mw->xlat_size = xlat_size;
1359 mw->buff_size = buff_size;
1361 mw->virt_addr = contigmalloc(mw->buff_size, M_NTB_IF, M_ZERO, 0,
1362 mw->addr_limit, mw->xlat_align, 0);
1363 if (mw->virt_addr == NULL) {
1364 ntb_printf(0, "Unable to allocate MW buffer of size %zu/%zu\n",
1365 mw->buff_size, mw->xlat_size);
1370 /* TODO: replace with bus_space_* functions */
1371 mw->dma_addr = vtophys(mw->virt_addr);
1374 * Ensure that the allocation from contigmalloc is aligned as
1375 * requested. XXX: This may not be needed -- brought in for parity
1376 * with the Linux driver.
1378 if (mw->dma_addr % mw->xlat_align != 0) {
1380 "DMA memory 0x%jx not aligned to BAR size 0x%zx\n",
1381 (uintmax_t)mw->dma_addr, size);
1382 ntb_free_mw(nt, num_mw);
1386 /* Notify HW the memory location of the receive buffer */
1387 rc = ntb_mw_set_trans(nt->ntb, num_mw, mw->dma_addr, mw->xlat_size);
1389 ntb_printf(0, "Unable to set mw%d translation\n", num_mw);
1390 ntb_free_mw(nt, num_mw);
1398 ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
1400 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1402 if (mw->virt_addr == NULL)
1405 ntb_mw_clear_trans(nt->ntb, num_mw);
1406 contigfree(mw->virt_addr, mw->xlat_size, M_NTB_IF);
1409 mw->virt_addr = NULL;
1413 ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, unsigned int qp_num)
1415 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
1416 struct ntb_transport_mw *mw;
1420 unsigned num_qps_mw, mw_num, mw_count;
1422 mw_count = nt->mw_count;
1423 mw_num = QP_TO_MW(nt, qp_num);
1424 mw = &nt->mw_vec[mw_num];
1426 if (mw->virt_addr == NULL)
1429 if (nt->qp_count % mw_count && mw_num + 1 < nt->qp_count / mw_count)
1430 num_qps_mw = nt->qp_count / mw_count + 1;
1432 num_qps_mw = nt->qp_count / mw_count;
1434 rx_size = mw->xlat_size / num_qps_mw;
1435 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
1436 rx_size -= sizeof(struct ntb_rx_info);
1438 qp->remote_rx_info = (void*)(qp->rx_buff + rx_size);
1440 /* Due to house-keeping, there must be at least 2 buffs */
1441 qp->rx_max_frame = qmin(rx_size / 2,
1442 transport_mtu + sizeof(struct ntb_payload_header));
1443 qp->rx_max_entry = rx_size / qp->rx_max_frame;
1446 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
1448 /* Set up the hdr offsets with 0s */
1449 for (i = 0; i < qp->rx_max_entry; i++) {
1450 offset = (void *)(qp->rx_buff + qp->rx_max_frame * (i + 1) -
1451 sizeof(struct ntb_payload_header));
1452 memset(offset, 0, sizeof(struct ntb_payload_header));
1463 ntb_qp_link_work(void *arg)
1465 struct ntb_transport_qp *qp = arg;
1466 struct ntb_softc *ntb = qp->ntb;
1467 struct ntb_transport_ctx *nt = qp->transport;
1468 uint32_t val, dummy;
1470 ntb_spad_read(ntb, IF_NTB_QP_LINKS, &val);
1472 ntb_peer_spad_write(ntb, IF_NTB_QP_LINKS, val | (1ull << qp->qp_num));
1474 /* query remote spad for qp ready bits */
1475 ntb_peer_spad_read(ntb, IF_NTB_QP_LINKS, &dummy);
1477 /* See if the remote side is up */
1478 if ((val & (1ull << qp->qp_num)) != 0) {
1479 ntb_printf(2, "qp link up\n");
1480 qp->link_is_up = true;
1482 if (qp->event_handler != NULL)
1483 qp->event_handler(qp->cb_data, NTB_LINK_UP);
1485 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1486 } else if (nt->link_is_up)
1487 callout_reset(&qp->link_work,
1488 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1491 /* Link down event*/
1493 ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
1495 struct ntb_transport_qp *qp;
1496 struct _qpset qp_bitmap_alloc;
1499 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc);
1500 BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free);
1502 /* Pass along the info to any clients */
1503 for (i = 0; i < nt->qp_count; i++)
1504 if (test_bit(i, &qp_bitmap_alloc)) {
1505 qp = &nt->qp_vec[i];
1506 ntb_qp_link_cleanup(qp);
1507 callout_drain(&qp->link_work);
1510 if (!nt->link_is_up)
1511 callout_drain(&nt->link_work);
1514 * The scratchpad registers keep the values if the remote side
1515 * goes down, blast them now to give them a sane value the next
1516 * time they are accessed
1518 for (i = 0; i < IF_NTB_MAX_SPAD; i++)
1519 ntb_spad_write(nt->ntb, i, 0);
1523 ntb_transport_link_cleanup_work(void *arg, int pending __unused)
1526 ntb_transport_link_cleanup(arg);
1530 ntb_qp_link_down(struct ntb_transport_qp *qp)
1533 ntb_qp_link_cleanup(qp);
1537 ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
1540 qp->link_is_up = false;
1542 qp->tx_index = qp->rx_index = 0;
1543 qp->tx_bytes = qp->rx_bytes = 0;
1544 qp->tx_pkts = qp->rx_pkts = 0;
1546 qp->rx_ring_empty = 0;
1547 qp->tx_ring_full = 0;
1549 qp->rx_err_no_buf = qp->tx_err_no_buf = 0;
1550 qp->rx_err_oflow = qp->rx_err_ver = 0;
1554 ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1556 struct ntb_transport_ctx *nt = qp->transport;
1558 callout_drain(&qp->link_work);
1559 ntb_qp_link_down_reset(qp);
1561 if (qp->event_handler != NULL)
1562 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1565 callout_reset(&qp->link_work,
1566 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1569 /* Link commanded down */
1571 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1572 * @qp: NTB transport layer queue to be disabled
1574 * Notify NTB transport layer of client's desire to no longer receive data on
1575 * transport queue specified. It is the client's responsibility to ensure all
1576 * entries on queue are purged or otherwise handled appropriately.
1579 ntb_transport_link_down(struct ntb_transport_qp *qp)
1586 qp->client_ready = false;
1588 ntb_spad_read(qp->ntb, IF_NTB_QP_LINKS, &val);
1590 ntb_peer_spad_write(qp->ntb, IF_NTB_QP_LINKS,
1591 val & ~(1 << qp->qp_num));
1594 ntb_send_link_down(qp);
1596 callout_drain(&qp->link_work);
1600 ntb_send_link_down(struct ntb_transport_qp *qp)
1602 struct ntb_queue_entry *entry;
1605 if (!qp->link_is_up)
1608 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1609 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1612 pause("NTB Wait for link down", hz / 10);
1618 entry->cb_data = NULL;
1621 entry->flags = IF_NTB_LINK_DOWN_FLAG;
1623 mtx_lock(&qp->transport->tx_lock);
1624 rc = ntb_process_tx(qp, entry);
1626 printf("ntb: Failed to send link down\n");
1627 mtx_unlock(&qp->transport->tx_lock);
1629 ntb_qp_link_down_reset(qp);
1633 /* List Management */
1636 ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1637 struct ntb_queue_list *list)
1640 mtx_lock_spin(lock);
1641 STAILQ_INSERT_TAIL(list, entry, entry);
1642 mtx_unlock_spin(lock);
1645 static struct ntb_queue_entry *
1646 ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1648 struct ntb_queue_entry *entry;
1650 mtx_lock_spin(lock);
1651 if (STAILQ_EMPTY(list)) {
1655 entry = STAILQ_FIRST(list);
1656 STAILQ_REMOVE_HEAD(list, entry);
1658 mtx_unlock_spin(lock);
1663 static struct ntb_queue_entry *
1664 ntb_list_mv(struct mtx *lock, struct ntb_queue_list *from,
1665 struct ntb_queue_list *to)
1667 struct ntb_queue_entry *entry;
1669 mtx_lock_spin(lock);
1670 if (STAILQ_EMPTY(from)) {
1674 entry = STAILQ_FIRST(from);
1675 STAILQ_REMOVE_HEAD(from, entry);
1676 STAILQ_INSERT_TAIL(to, entry, entry);
1679 mtx_unlock_spin(lock);
1683 /* Helper functions */
1684 /* TODO: This too should really be part of the kernel */
1685 #define EUI48_MULTICAST 1 << 0
1686 #define EUI48_LOCALLY_ADMINISTERED 1 << 1
1688 create_random_local_eui48(u_char *eaddr)
1690 static uint8_t counter = 0;
1691 uint32_t seed = ticks;
1693 eaddr[0] = EUI48_LOCALLY_ADMINISTERED;
1694 memcpy(&eaddr[1], &seed, sizeof(uint32_t));
1695 eaddr[5] = counter++;
1699 * ntb_transport_max_size - Query the max payload size of a qp
1700 * @qp: NTB transport layer queue to be queried
1702 * Query the maximum payload size permissible on the given qp
1704 * RETURNS: the max payload size of a qp
1707 ntb_transport_max_size(struct ntb_transport_qp *qp)
1713 return (qp->tx_max_frame - sizeof(struct ntb_payload_header));