2 * Copyright (c) 2016 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>
46 #include <sys/bitset.h>
49 #include <sys/limits.h>
51 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/queue.h>
56 #include <sys/sysctl.h>
57 #include <sys/taskqueue.h>
62 #include <machine/bus.h>
65 #include "ntb_transport.h"
68 BITSET_DEFINE(_qpset, QP_SETSIZE);
69 #define test_bit(pos, addr) BIT_ISSET(QP_SETSIZE, (pos), (addr))
70 #define set_bit(pos, addr) BIT_SET(QP_SETSIZE, (pos), (addr))
71 #define clear_bit(pos, addr) BIT_CLR(QP_SETSIZE, (pos), (addr))
72 #define ffs_bit(addr) BIT_FFS(QP_SETSIZE, (addr))
74 #define KTR_NTB KTR_SPARE3
76 #define NTB_TRANSPORT_VERSION 4
78 static SYSCTL_NODE(_hw, OID_AUTO, ntb_transport, CTLFLAG_RW, 0, "ntb_transport");
80 static unsigned g_ntb_transport_debug_level;
81 SYSCTL_UINT(_hw_ntb_transport, OID_AUTO, debug_level, CTLFLAG_RWTUN,
82 &g_ntb_transport_debug_level, 0,
83 "ntb_transport log level -- higher is more verbose");
84 #define ntb_printf(lvl, ...) do { \
85 if ((lvl) <= g_ntb_transport_debug_level) { \
86 printf(__VA_ARGS__); \
90 static unsigned transport_mtu = 0x10000;
92 static uint64_t max_mw_size;
93 SYSCTL_UQUAD(_hw_ntb_transport, OID_AUTO, max_mw_size, CTLFLAG_RDTUN, &max_mw_size, 0,
94 "If enabled (non-zero), limit the size of large memory windows. "
95 "Both sides of the NTB MUST set the same value here.");
97 static unsigned max_num_clients;
98 SYSCTL_UINT(_hw_ntb_transport, OID_AUTO, max_num_clients, CTLFLAG_RDTUN,
99 &max_num_clients, 0, "Maximum number of NTB transport clients. "
100 "0 (default) - use all available NTB memory windows; "
101 "positive integer N - Limit to N memory windows.");
103 static unsigned enable_xeon_watchdog;
104 SYSCTL_UINT(_hw_ntb_transport, OID_AUTO, enable_xeon_watchdog, CTLFLAG_RDTUN,
105 &enable_xeon_watchdog, 0, "If non-zero, write a register every second to "
106 "keep a watchdog from tearing down the NTB link");
108 STAILQ_HEAD(ntb_queue_list, ntb_queue_entry);
110 typedef uint32_t ntb_q_idx_t;
112 struct ntb_queue_entry {
113 /* ntb_queue list reference */
114 STAILQ_ENTRY(ntb_queue_entry) entry;
116 /* info on data to be transferred */
122 struct ntb_transport_qp *qp;
123 struct ntb_payload_header *x_hdr;
131 struct ntb_transport_qp {
132 struct ntb_transport_ctx *transport;
138 volatile bool link_is_up;
139 uint8_t qp_num; /* Only 64 QPs are allowed. 0-63 */
141 struct ntb_rx_info *rx_info;
142 struct ntb_rx_info *remote_rx_info;
144 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
145 void *data, int len);
146 struct ntb_queue_list tx_free_q;
147 struct mtx ntb_tx_free_q_lock;
149 bus_addr_t tx_mw_phys;
150 ntb_q_idx_t tx_index;
151 ntb_q_idx_t tx_max_entry;
152 uint64_t tx_max_frame;
154 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
155 void *data, int len);
156 struct ntb_queue_list rx_post_q;
157 struct ntb_queue_list rx_pend_q;
158 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
159 struct mtx ntb_rx_q_lock;
160 struct task rxc_db_work;
161 struct taskqueue *rxc_tq;
163 ntb_q_idx_t rx_index;
164 ntb_q_idx_t rx_max_entry;
165 uint64_t rx_max_frame;
167 void (*event_handler)(void *data, enum ntb_link_event status);
168 struct callout link_work;
169 struct callout rx_full;
171 uint64_t last_rx_no_buf;
176 uint64_t rx_ring_empty;
177 uint64_t rx_err_no_buf;
178 uint64_t rx_err_oflow;
182 uint64_t tx_ring_full;
183 uint64_t tx_err_no_buf;
188 struct ntb_transport_mw {
189 vm_paddr_t phys_addr;
192 size_t xlat_align_size;
193 bus_addr_t addr_limit;
194 /* Tx buff is off vbase / phys_addr */
198 /* Rx buff is off virt_addr / dma_addr */
203 struct ntb_transport_ctx {
206 struct ntb_transport_mw *mw_vec;
207 struct ntb_transport_qp *qp_vec;
208 struct _qpset qp_bitmap;
209 struct _qpset qp_bitmap_free;
212 volatile bool link_is_up;
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.
249 * Some NTB-using hardware have a watchdog to work around NTB hangs; if
250 * a register or doorbell isn't written every few seconds, the link is
251 * torn down. Write an otherwise unused register every few seconds to
252 * work around this watchdog.
254 NTBT_WATCHDOG_SPAD = 15
257 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
258 #define NTB_QP_DEF_NUM_ENTRIES 100
259 #define NTB_LINK_DOWN_TIMEOUT 10
261 static int ntb_transport_probe(device_t dev);
262 static int ntb_transport_attach(device_t dev);
263 static int ntb_transport_detach(device_t dev);
264 static void ntb_transport_init_queue(struct ntb_transport_ctx *nt,
265 unsigned int qp_num);
266 static int ntb_process_tx(struct ntb_transport_qp *qp,
267 struct ntb_queue_entry *entry);
268 static void ntb_transport_rxc_db(void *arg, int pending);
269 static int ntb_process_rxc(struct ntb_transport_qp *qp);
270 static void ntb_memcpy_rx(struct ntb_transport_qp *qp,
271 struct ntb_queue_entry *entry, void *offset);
272 static inline void ntb_rx_copy_callback(struct ntb_transport_qp *qp,
274 static void ntb_complete_rxc(struct ntb_transport_qp *qp);
275 static void ntb_transport_doorbell_callback(void *data, uint32_t vector);
276 static void ntb_transport_event_callback(void *data);
277 static void ntb_transport_link_work(void *arg);
278 static int ntb_set_mw(struct ntb_transport_ctx *, int num_mw, size_t size);
279 static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw);
280 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
281 unsigned int qp_num);
282 static void ntb_qp_link_work(void *arg);
283 static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt);
284 static void ntb_transport_link_cleanup_work(void *, int);
285 static void ntb_qp_link_down(struct ntb_transport_qp *qp);
286 static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp);
287 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp);
288 static void ntb_send_link_down(struct ntb_transport_qp *qp);
289 static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
290 struct ntb_queue_list *list);
291 static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock,
292 struct ntb_queue_list *list);
293 static struct ntb_queue_entry *ntb_list_mv(struct mtx *lock,
294 struct ntb_queue_list *from, struct ntb_queue_list *to);
295 static void xeon_link_watchdog_hb(void *);
297 static const struct ntb_ctx_ops ntb_transport_ops = {
298 .link_event = ntb_transport_event_callback,
299 .db_event = ntb_transport_doorbell_callback,
302 MALLOC_DEFINE(M_NTB_T, "ntb_transport", "ntb transport driver");
305 iowrite32(uint32_t val, void *addr)
308 bus_space_write_4(X86_BUS_SPACE_MEM, 0/* HACK */, (uintptr_t)addr,
312 /* Transport Init and teardown */
315 xeon_link_watchdog_hb(void *arg)
317 struct ntb_transport_ctx *nt;
320 NTB_SPAD_WRITE(nt->ntb, NTBT_WATCHDOG_SPAD, 0);
321 callout_reset(&nt->link_watchdog, 1 * hz, xeon_link_watchdog_hb, nt);
325 ntb_transport_probe(device_t dev)
328 device_set_desc(dev, "NTB Transport");
333 ntb_transport_attach(device_t dev)
335 struct ntb_transport_ctx *nt = device_get_softc(dev);
336 device_t ntb = device_get_parent(dev);
337 struct ntb_transport_mw *mw;
344 nt->mw_count = NTB_MW_COUNT(ntb);
345 nt->mw_vec = malloc(nt->mw_count * sizeof(*nt->mw_vec), M_NTB_T,
347 for (i = 0; i < nt->mw_count; i++) {
350 rc = NTB_MW_GET_RANGE(ntb, i, &mw->phys_addr, &mw->vbase,
351 &mw->phys_size, &mw->xlat_align, &mw->xlat_align_size,
358 mw->virt_addr = NULL;
361 rc = NTB_MW_SET_WC(nt->ntb, i, VM_MEMATTR_WRITE_COMBINING);
363 ntb_printf(0, "Unable to set mw%d caching\n", i);
366 qp_bitmap = NTB_DB_VALID_MASK(ntb);
367 nt->qp_count = flsll(qp_bitmap);
368 KASSERT(nt->qp_count != 0, ("bogus db bitmap"));
371 if (max_num_clients != 0 && max_num_clients < nt->qp_count)
372 nt->qp_count = max_num_clients;
373 else if (nt->mw_count < nt->qp_count)
374 nt->qp_count = nt->mw_count;
375 KASSERT(nt->qp_count <= QP_SETSIZE, ("invalid qp_count"));
377 nt->qp_vec = malloc(nt->qp_count * sizeof(*nt->qp_vec), M_NTB_T,
380 for (i = 0; i < nt->qp_count; i++) {
381 set_bit(i, &nt->qp_bitmap);
382 set_bit(i, &nt->qp_bitmap_free);
383 ntb_transport_init_queue(nt, i);
386 callout_init(&nt->link_work, 0);
387 callout_init(&nt->link_watchdog, 0);
388 TASK_INIT(&nt->link_cleanup, 0, ntb_transport_link_cleanup_work, nt);
390 rc = NTB_SET_CTX(ntb, nt, &ntb_transport_ops);
394 nt->link_is_up = false;
395 NTB_LINK_ENABLE(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
397 if (enable_xeon_watchdog != 0)
398 callout_reset(&nt->link_watchdog, 0, xeon_link_watchdog_hb, nt);
400 /* Attach children to this transport */
401 device_add_child(dev, NULL, -1);
402 bus_generic_attach(dev);
407 free(nt->qp_vec, M_NTB_T);
408 free(nt->mw_vec, M_NTB_T);
413 ntb_transport_detach(device_t dev)
415 struct ntb_transport_ctx *nt = device_get_softc(dev);
416 device_t ntb = nt->ntb;
417 struct _qpset qp_bitmap_alloc;
420 /* Detach & delete all children */
421 device_delete_children(dev);
423 ntb_transport_link_cleanup(nt);
424 taskqueue_drain(taskqueue_swi, &nt->link_cleanup);
425 callout_drain(&nt->link_work);
426 callout_drain(&nt->link_watchdog);
428 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc);
429 BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free);
431 /* Verify that all the QPs are freed */
432 for (i = 0; i < nt->qp_count; i++)
433 if (test_bit(i, &qp_bitmap_alloc))
434 ntb_transport_free_queue(&nt->qp_vec[i]);
436 NTB_LINK_DISABLE(ntb);
439 for (i = 0; i < nt->mw_count; i++)
442 free(nt->qp_vec, M_NTB_T);
443 free(nt->mw_vec, M_NTB_T);
448 ntb_transport_init_queue(struct ntb_transport_ctx *nt, unsigned int qp_num)
450 struct ntb_transport_mw *mw;
451 struct ntb_transport_qp *qp;
453 uint64_t mw_size, qp_offset;
455 unsigned num_qps_mw, mw_num, mw_count;
457 mw_count = nt->mw_count;
458 mw_num = QP_TO_MW(nt, qp_num);
459 mw = &nt->mw_vec[mw_num];
461 qp = &nt->qp_vec[qp_num];
465 qp->client_ready = false;
466 qp->event_handler = NULL;
467 ntb_qp_link_down_reset(qp);
469 if (mw_num < nt->qp_count % mw_count)
470 num_qps_mw = nt->qp_count / mw_count + 1;
472 num_qps_mw = nt->qp_count / mw_count;
474 mw_base = mw->phys_addr;
475 mw_size = mw->phys_size;
477 tx_size = mw_size / num_qps_mw;
478 qp_offset = tx_size * (qp_num / mw_count);
480 qp->tx_mw = mw->vbase + qp_offset;
481 KASSERT(qp->tx_mw != NULL, ("uh oh?"));
483 /* XXX Assumes that a vm_paddr_t is equivalent to bus_addr_t */
484 qp->tx_mw_phys = mw_base + qp_offset;
485 KASSERT(qp->tx_mw_phys != 0, ("uh oh?"));
487 tx_size -= sizeof(struct ntb_rx_info);
488 qp->rx_info = (void *)(qp->tx_mw + tx_size);
490 /* Due to house-keeping, there must be at least 2 buffs */
491 qp->tx_max_frame = qmin(transport_mtu, tx_size / 2);
492 qp->tx_max_entry = tx_size / qp->tx_max_frame;
494 callout_init(&qp->link_work, 0);
495 callout_init(&qp->rx_full, 1);
497 mtx_init(&qp->ntb_rx_q_lock, "ntb rx q", NULL, MTX_SPIN);
498 mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN);
499 mtx_init(&qp->tx_lock, "ntb transport tx", NULL, MTX_DEF);
500 TASK_INIT(&qp->rxc_db_work, 0, ntb_transport_rxc_db, qp);
501 qp->rxc_tq = taskqueue_create("ntbt_rx", M_WAITOK,
502 taskqueue_thread_enqueue, &qp->rxc_tq);
503 taskqueue_start_threads(&qp->rxc_tq, 1, PI_NET, "%s rx%d",
504 device_get_nameunit(nt->dev), qp_num);
506 STAILQ_INIT(&qp->rx_post_q);
507 STAILQ_INIT(&qp->rx_pend_q);
508 STAILQ_INIT(&qp->tx_free_q);
512 ntb_transport_free_queue(struct ntb_transport_qp *qp)
514 struct ntb_queue_entry *entry;
519 callout_drain(&qp->link_work);
521 NTB_DB_SET_MASK(qp->ntb, 1ull << qp->qp_num);
522 taskqueue_drain_all(qp->rxc_tq);
523 taskqueue_free(qp->rxc_tq);
526 qp->rx_handler = NULL;
527 qp->tx_handler = NULL;
528 qp->event_handler = NULL;
530 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q)))
531 free(entry, M_NTB_T);
533 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q)))
534 free(entry, M_NTB_T);
536 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
537 free(entry, M_NTB_T);
539 set_bit(qp->qp_num, &qp->transport->qp_bitmap_free);
543 * ntb_transport_create_queue - Create a new NTB transport layer queue
544 * @rx_handler: receive callback function
545 * @tx_handler: transmit callback function
546 * @event_handler: event callback function
548 * Create a new NTB transport layer queue and provide the queue with a callback
549 * routine for both transmit and receive. The receive callback routine will be
550 * used to pass up data when the transport has received it on the queue. The
551 * transmit callback routine will be called when the transport has completed the
552 * transmission of the data on the queue and the data is ready to be freed.
554 * RETURNS: pointer to newly created ntb_queue, NULL on error.
556 struct ntb_transport_qp *
557 ntb_transport_create_queue(void *data, device_t dev,
558 const struct ntb_queue_handlers *handlers)
560 struct ntb_transport_ctx *nt = device_get_softc(dev);
561 device_t ntb = device_get_parent(dev);
562 struct ntb_queue_entry *entry;
563 struct ntb_transport_qp *qp;
564 unsigned int free_queue;
567 free_queue = ffs_bit(&nt->qp_bitmap_free);
571 /* decrement free_queue to make it zero based */
574 qp = &nt->qp_vec[free_queue];
575 clear_bit(qp->qp_num, &nt->qp_bitmap_free);
577 qp->rx_handler = handlers->rx_handler;
578 qp->tx_handler = handlers->tx_handler;
579 qp->event_handler = handlers->event_handler;
581 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
582 entry = malloc(sizeof(*entry), M_NTB_T, M_WAITOK | M_ZERO);
583 entry->cb_data = data;
585 entry->len = transport_mtu;
587 ntb_list_add(&qp->ntb_rx_q_lock, entry, &qp->rx_pend_q);
590 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
591 entry = malloc(sizeof(*entry), M_NTB_T, M_WAITOK | M_ZERO);
593 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
596 NTB_DB_CLEAR(ntb, 1ull << qp->qp_num);
601 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
602 * @qp: NTB transport layer queue to be enabled
604 * Notify NTB transport layer of client readiness to use queue
607 ntb_transport_link_up(struct ntb_transport_qp *qp)
609 struct ntb_transport_ctx *nt = qp->transport;
611 qp->client_ready = true;
613 ntb_printf(2, "qp %d client ready\n", qp->qp_num);
616 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
624 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
625 * @qp: NTB transport layer queue the entry is to be enqueued on
626 * @cb: per buffer pointer for callback function to use
627 * @data: pointer to data buffer that will be sent
628 * @len: length of the data buffer
630 * Enqueue a new transmit buffer onto the transport queue from which a NTB
631 * payload will be transmitted. This assumes that a lock is being held to
632 * serialize access to the qp.
634 * RETURNS: An appropriate ERRNO error value on error, or zero for success.
637 ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
640 struct ntb_queue_entry *entry;
643 if (qp == NULL || !qp->link_is_up || len == 0) {
644 CTR0(KTR_NTB, "TX: link not up");
648 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
650 CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
654 CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
661 mtx_lock(&qp->tx_lock);
662 rc = ntb_process_tx(qp, entry);
663 mtx_unlock(&qp->tx_lock);
665 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
667 "TX: process_tx failed. Returning entry %p to tx_free_q",
674 ntb_tx_copy_callback(void *data)
676 struct ntb_queue_entry *entry = data;
677 struct ntb_transport_qp *qp = entry->qp;
678 struct ntb_payload_header *hdr = entry->x_hdr;
680 iowrite32(entry->flags | NTBT_DESC_DONE_FLAG, &hdr->flags);
681 CTR1(KTR_NTB, "TX: hdr %p set DESC_DONE", hdr);
683 NTB_PEER_DB_SET(qp->ntb, 1ull << qp->qp_num);
686 * The entry length can only be zero if the packet is intended to be a
687 * "link down" or similar. Since no payload is being sent in these
688 * cases, there is nothing to add to the completion queue.
690 if (entry->len > 0) {
691 qp->tx_bytes += entry->len;
694 qp->tx_handler(qp, qp->cb_data, entry->buf,
702 "TX: entry %p sent. hdr->ver = %u, hdr->flags = 0x%x, Returning "
703 "to tx_free_q", entry, hdr->ver, hdr->flags);
704 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
708 ntb_memcpy_tx(struct ntb_queue_entry *entry, void *offset)
711 CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset);
712 if (entry->buf != NULL) {
713 m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset);
716 * Ensure that the data is fully copied before setting the
722 ntb_tx_copy_callback(entry);
726 ntb_async_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
728 struct ntb_payload_header *hdr;
731 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
732 hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame -
733 sizeof(struct ntb_payload_header));
736 iowrite32(entry->len, &hdr->len);
737 iowrite32(qp->tx_pkts, &hdr->ver);
739 ntb_memcpy_tx(entry, offset);
743 ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
747 "TX: process_tx: tx_pkts=%lu, tx_index=%u, remote entry=%u",
748 qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry);
749 if (qp->tx_index == qp->remote_rx_info->entry) {
750 CTR0(KTR_NTB, "TX: ring full");
755 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
756 if (qp->tx_handler != NULL)
757 qp->tx_handler(qp, qp->cb_data, entry->buf,
763 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
765 "TX: frame too big. returning entry %p to tx_free_q",
769 CTR2(KTR_NTB, "TX: copying entry %p to index %u", entry, qp->tx_index);
770 ntb_async_tx(qp, entry);
773 qp->tx_index %= qp->tx_max_entry;
782 ntb_transport_rxc_db(void *arg, int pending __unused)
784 struct ntb_transport_qp *qp = arg;
787 CTR0(KTR_NTB, "RX: transport_rx");
789 while ((rc = ntb_process_rxc(qp)) == 0)
791 CTR1(KTR_NTB, "RX: process_rxc returned %d", rc);
793 if ((NTB_DB_READ(qp->ntb) & (1ull << qp->qp_num)) != 0) {
794 /* If db is set, clear it and check queue once more. */
795 NTB_DB_CLEAR(qp->ntb, 1ull << qp->qp_num);
801 ntb_process_rxc(struct ntb_transport_qp *qp)
803 struct ntb_payload_header *hdr;
804 struct ntb_queue_entry *entry;
807 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
808 hdr = (void *)(offset + qp->rx_max_frame -
809 sizeof(struct ntb_payload_header));
811 CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index);
812 if ((hdr->flags & NTBT_DESC_DONE_FLAG) == 0) {
813 CTR0(KTR_NTB, "RX: hdr not done");
818 if ((hdr->flags & NTBT_LINK_DOWN_FLAG) != 0) {
819 CTR0(KTR_NTB, "RX: link down");
820 ntb_qp_link_down(qp);
825 if (hdr->ver != (uint32_t)qp->rx_pkts) {
826 CTR2(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). "
827 "Returning entry to rx_pend_q", hdr->ver, qp->rx_pkts);
832 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
835 CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
838 callout_stop(&qp->rx_full);
839 CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
842 entry->index = qp->rx_index;
844 if (hdr->len > entry->len) {
845 CTR2(KTR_NTB, "RX: len too long. Wanted %ju got %ju",
846 (uintmax_t)hdr->len, (uintmax_t)entry->len);
850 entry->flags |= NTBT_DESC_DONE_FLAG;
852 ntb_complete_rxc(qp);
854 qp->rx_bytes += hdr->len;
857 CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
859 entry->len = hdr->len;
861 ntb_memcpy_rx(qp, entry, offset);
865 qp->rx_index %= qp->rx_max_entry;
870 ntb_memcpy_rx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
873 struct ifnet *ifp = entry->cb_data;
874 unsigned int len = entry->len;
876 CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset);
878 entry->buf = (void *)m_devget(offset, len, 0, ifp, NULL);
879 if (entry->buf == NULL)
880 entry->len = -ENOMEM;
882 /* Ensure that the data is globally visible before clearing the flag */
885 CTR2(KTR_NTB, "RX: copied entry %p to mbuf %p.", entry, entry->buf);
886 ntb_rx_copy_callback(qp, entry);
890 ntb_rx_copy_callback(struct ntb_transport_qp *qp, void *data)
892 struct ntb_queue_entry *entry;
895 entry->flags |= NTBT_DESC_DONE_FLAG;
896 ntb_complete_rxc(qp);
900 ntb_complete_rxc(struct ntb_transport_qp *qp)
902 struct ntb_queue_entry *entry;
906 CTR0(KTR_NTB, "RX: rx_completion_task");
908 mtx_lock_spin(&qp->ntb_rx_q_lock);
910 while (!STAILQ_EMPTY(&qp->rx_post_q)) {
911 entry = STAILQ_FIRST(&qp->rx_post_q);
912 if ((entry->flags & NTBT_DESC_DONE_FLAG) == 0)
915 entry->x_hdr->flags = 0;
916 iowrite32(entry->index, &qp->rx_info->entry);
918 STAILQ_REMOVE_HEAD(&qp->rx_post_q, entry);
924 * Re-initialize queue_entry for reuse; rx_handler takes
925 * ownership of the mbuf.
928 entry->len = transport_mtu;
929 entry->cb_data = qp->cb_data;
931 STAILQ_INSERT_TAIL(&qp->rx_pend_q, entry, entry);
933 mtx_unlock_spin(&qp->ntb_rx_q_lock);
935 CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m);
936 if (qp->rx_handler != NULL && qp->client_ready)
937 qp->rx_handler(qp, qp->cb_data, m, len);
941 mtx_lock_spin(&qp->ntb_rx_q_lock);
944 mtx_unlock_spin(&qp->ntb_rx_q_lock);
948 ntb_transport_doorbell_callback(void *data, uint32_t vector)
950 struct ntb_transport_ctx *nt = data;
951 struct ntb_transport_qp *qp;
952 struct _qpset db_bits;
956 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &db_bits);
957 BIT_NAND(QP_SETSIZE, &db_bits, &nt->qp_bitmap_free);
959 vec_mask = NTB_DB_VECTOR_MASK(nt->ntb, vector);
960 if ((vec_mask & (vec_mask - 1)) != 0)
961 vec_mask &= NTB_DB_READ(nt->ntb);
962 while (vec_mask != 0) {
963 qp_num = ffsll(vec_mask) - 1;
965 if (test_bit(qp_num, &db_bits)) {
966 qp = &nt->qp_vec[qp_num];
968 taskqueue_enqueue(qp->rxc_tq, &qp->rxc_db_work);
971 vec_mask &= ~(1ull << qp_num);
975 /* Link Event handler */
977 ntb_transport_event_callback(void *data)
979 struct ntb_transport_ctx *nt = data;
981 if (NTB_LINK_IS_UP(nt->ntb, NULL, NULL)) {
982 ntb_printf(1, "HW link up\n");
983 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
985 ntb_printf(1, "HW link down\n");
986 taskqueue_enqueue(taskqueue_swi, &nt->link_cleanup);
992 ntb_transport_link_work(void *arg)
994 struct ntb_transport_ctx *nt = arg;
995 device_t ntb = nt->ntb;
996 struct ntb_transport_qp *qp;
997 uint64_t val64, size;
1002 /* send the local info, in the opposite order of the way we read it */
1003 for (i = 0; i < nt->mw_count; i++) {
1004 size = nt->mw_vec[i].phys_size;
1006 if (max_mw_size != 0 && size > max_mw_size)
1009 NTB_PEER_SPAD_WRITE(ntb, NTBT_MW0_SZ_HIGH + (i * 2),
1011 NTB_PEER_SPAD_WRITE(ntb, NTBT_MW0_SZ_LOW + (i * 2), size);
1014 NTB_PEER_SPAD_WRITE(ntb, NTBT_NUM_MWS, nt->mw_count);
1016 NTB_PEER_SPAD_WRITE(ntb, NTBT_NUM_QPS, nt->qp_count);
1018 NTB_PEER_SPAD_WRITE(ntb, NTBT_VERSION, NTB_TRANSPORT_VERSION);
1020 /* Query the remote side for its info */
1022 NTB_SPAD_READ(ntb, NTBT_VERSION, &val);
1023 if (val != NTB_TRANSPORT_VERSION)
1026 NTB_SPAD_READ(ntb, NTBT_NUM_QPS, &val);
1027 if (val != nt->qp_count)
1030 NTB_SPAD_READ(ntb, NTBT_NUM_MWS, &val);
1031 if (val != nt->mw_count)
1034 for (i = 0; i < nt->mw_count; i++) {
1035 NTB_SPAD_READ(ntb, NTBT_MW0_SZ_HIGH + (i * 2), &val);
1036 val64 = (uint64_t)val << 32;
1038 NTB_SPAD_READ(ntb, NTBT_MW0_SZ_LOW + (i * 2), &val);
1041 rc = ntb_set_mw(nt, i, val64);
1046 nt->link_is_up = true;
1047 ntb_printf(1, "transport link up\n");
1049 for (i = 0; i < nt->qp_count; i++) {
1050 qp = &nt->qp_vec[i];
1052 ntb_transport_setup_qp_mw(nt, i);
1054 if (qp->client_ready)
1055 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1061 for (i = 0; i < nt->mw_count; i++)
1064 if (NTB_LINK_IS_UP(ntb, NULL, NULL))
1065 callout_reset(&nt->link_work,
1066 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt);
1070 ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, size_t size)
1072 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1073 size_t xlat_size, buff_size;
1079 xlat_size = roundup(size, mw->xlat_align_size);
1080 buff_size = xlat_size;
1082 /* No need to re-setup */
1083 if (mw->xlat_size == xlat_size)
1086 if (mw->buff_size != 0)
1087 ntb_free_mw(nt, num_mw);
1089 /* Alloc memory for receiving data. Must be aligned */
1090 mw->xlat_size = xlat_size;
1091 mw->buff_size = buff_size;
1093 mw->virt_addr = contigmalloc(mw->buff_size, M_NTB_T, M_ZERO, 0,
1094 mw->addr_limit, mw->xlat_align, 0);
1095 if (mw->virt_addr == NULL) {
1096 ntb_printf(0, "Unable to allocate MW buffer of size %zu/%zu\n",
1097 mw->buff_size, mw->xlat_size);
1102 /* TODO: replace with bus_space_* functions */
1103 mw->dma_addr = vtophys(mw->virt_addr);
1106 * Ensure that the allocation from contigmalloc is aligned as
1107 * requested. XXX: This may not be needed -- brought in for parity
1108 * with the Linux driver.
1110 if (mw->dma_addr % mw->xlat_align != 0) {
1112 "DMA memory 0x%jx not aligned to BAR size 0x%zx\n",
1113 (uintmax_t)mw->dma_addr, size);
1114 ntb_free_mw(nt, num_mw);
1118 /* Notify HW the memory location of the receive buffer */
1119 rc = NTB_MW_SET_TRANS(nt->ntb, num_mw, mw->dma_addr, mw->xlat_size);
1121 ntb_printf(0, "Unable to set mw%d translation\n", num_mw);
1122 ntb_free_mw(nt, num_mw);
1130 ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
1132 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1134 if (mw->virt_addr == NULL)
1137 NTB_MW_CLEAR_TRANS(nt->ntb, num_mw);
1138 contigfree(mw->virt_addr, mw->xlat_size, M_NTB_T);
1141 mw->virt_addr = NULL;
1145 ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, unsigned int qp_num)
1147 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
1148 struct ntb_transport_mw *mw;
1152 unsigned num_qps_mw, mw_num, mw_count;
1154 mw_count = nt->mw_count;
1155 mw_num = QP_TO_MW(nt, qp_num);
1156 mw = &nt->mw_vec[mw_num];
1158 if (mw->virt_addr == NULL)
1161 if (mw_num < nt->qp_count % mw_count)
1162 num_qps_mw = nt->qp_count / mw_count + 1;
1164 num_qps_mw = nt->qp_count / mw_count;
1166 rx_size = mw->xlat_size / num_qps_mw;
1167 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
1168 rx_size -= sizeof(struct ntb_rx_info);
1170 qp->remote_rx_info = (void*)(qp->rx_buff + rx_size);
1172 /* Due to house-keeping, there must be at least 2 buffs */
1173 qp->rx_max_frame = qmin(transport_mtu, rx_size / 2);
1174 qp->rx_max_entry = rx_size / qp->rx_max_frame;
1177 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
1179 /* Set up the hdr offsets with 0s */
1180 for (i = 0; i < qp->rx_max_entry; i++) {
1181 offset = (void *)(qp->rx_buff + qp->rx_max_frame * (i + 1) -
1182 sizeof(struct ntb_payload_header));
1183 memset(offset, 0, sizeof(struct ntb_payload_header));
1194 ntb_qp_link_work(void *arg)
1196 struct ntb_transport_qp *qp = arg;
1197 device_t ntb = qp->ntb;
1198 struct ntb_transport_ctx *nt = qp->transport;
1199 uint32_t val, dummy;
1201 NTB_SPAD_READ(ntb, NTBT_QP_LINKS, &val);
1203 NTB_PEER_SPAD_WRITE(ntb, NTBT_QP_LINKS, val | (1ull << qp->qp_num));
1205 /* query remote spad for qp ready bits */
1206 NTB_PEER_SPAD_READ(ntb, NTBT_QP_LINKS, &dummy);
1208 /* See if the remote side is up */
1209 if ((val & (1ull << qp->qp_num)) != 0) {
1210 ntb_printf(2, "qp %d link up\n", qp->qp_num);
1211 qp->link_is_up = true;
1213 if (qp->event_handler != NULL)
1214 qp->event_handler(qp->cb_data, NTB_LINK_UP);
1216 NTB_DB_CLEAR_MASK(ntb, 1ull << qp->qp_num);
1217 } else if (nt->link_is_up)
1218 callout_reset(&qp->link_work,
1219 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1222 /* Link down event*/
1224 ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
1226 struct ntb_transport_qp *qp;
1227 struct _qpset qp_bitmap_alloc;
1230 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc);
1231 BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free);
1233 /* Pass along the info to any clients */
1234 for (i = 0; i < nt->qp_count; i++)
1235 if (test_bit(i, &qp_bitmap_alloc)) {
1236 qp = &nt->qp_vec[i];
1237 ntb_qp_link_cleanup(qp);
1238 callout_drain(&qp->link_work);
1241 if (!nt->link_is_up)
1242 callout_drain(&nt->link_work);
1245 * The scratchpad registers keep the values if the remote side
1246 * goes down, blast them now to give them a sane value the next
1247 * time they are accessed
1249 for (i = 0; i < NTBT_MAX_SPAD; i++)
1250 NTB_SPAD_WRITE(nt->ntb, i, 0);
1254 ntb_transport_link_cleanup_work(void *arg, int pending __unused)
1257 ntb_transport_link_cleanup(arg);
1261 ntb_qp_link_down(struct ntb_transport_qp *qp)
1264 ntb_qp_link_cleanup(qp);
1268 ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
1271 qp->link_is_up = false;
1272 NTB_DB_SET_MASK(qp->ntb, 1ull << qp->qp_num);
1274 qp->tx_index = qp->rx_index = 0;
1275 qp->tx_bytes = qp->rx_bytes = 0;
1276 qp->tx_pkts = qp->rx_pkts = 0;
1278 qp->rx_ring_empty = 0;
1279 qp->tx_ring_full = 0;
1281 qp->rx_err_no_buf = qp->tx_err_no_buf = 0;
1282 qp->rx_err_oflow = qp->rx_err_ver = 0;
1286 ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1289 callout_drain(&qp->link_work);
1290 ntb_qp_link_down_reset(qp);
1292 if (qp->event_handler != NULL)
1293 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1296 /* Link commanded down */
1298 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1299 * @qp: NTB transport layer queue to be disabled
1301 * Notify NTB transport layer of client's desire to no longer receive data on
1302 * transport queue specified. It is the client's responsibility to ensure all
1303 * entries on queue are purged or otherwise handled appropriately.
1306 ntb_transport_link_down(struct ntb_transport_qp *qp)
1313 qp->client_ready = false;
1315 NTB_SPAD_READ(qp->ntb, NTBT_QP_LINKS, &val);
1317 NTB_PEER_SPAD_WRITE(qp->ntb, NTBT_QP_LINKS,
1318 val & ~(1 << qp->qp_num));
1321 ntb_send_link_down(qp);
1323 callout_drain(&qp->link_work);
1327 * ntb_transport_link_query - Query transport link state
1328 * @qp: NTB transport layer queue to be queried
1330 * Query connectivity to the remote system of the NTB transport queue
1332 * RETURNS: true for link up or false for link down
1335 ntb_transport_link_query(struct ntb_transport_qp *qp)
1340 return (qp->link_is_up);
1344 ntb_send_link_down(struct ntb_transport_qp *qp)
1346 struct ntb_queue_entry *entry;
1349 if (!qp->link_is_up)
1352 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1353 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1356 pause("NTB Wait for link down", hz / 10);
1362 entry->cb_data = NULL;
1365 entry->flags = NTBT_LINK_DOWN_FLAG;
1367 mtx_lock(&qp->tx_lock);
1368 rc = ntb_process_tx(qp, entry);
1369 mtx_unlock(&qp->tx_lock);
1371 printf("ntb: Failed to send link down\n");
1373 ntb_qp_link_down_reset(qp);
1377 /* List Management */
1380 ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1381 struct ntb_queue_list *list)
1384 mtx_lock_spin(lock);
1385 STAILQ_INSERT_TAIL(list, entry, entry);
1386 mtx_unlock_spin(lock);
1389 static struct ntb_queue_entry *
1390 ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1392 struct ntb_queue_entry *entry;
1394 mtx_lock_spin(lock);
1395 if (STAILQ_EMPTY(list)) {
1399 entry = STAILQ_FIRST(list);
1400 STAILQ_REMOVE_HEAD(list, entry);
1402 mtx_unlock_spin(lock);
1407 static struct ntb_queue_entry *
1408 ntb_list_mv(struct mtx *lock, struct ntb_queue_list *from,
1409 struct ntb_queue_list *to)
1411 struct ntb_queue_entry *entry;
1413 mtx_lock_spin(lock);
1414 if (STAILQ_EMPTY(from)) {
1418 entry = STAILQ_FIRST(from);
1419 STAILQ_REMOVE_HEAD(from, entry);
1420 STAILQ_INSERT_TAIL(to, entry, entry);
1423 mtx_unlock_spin(lock);
1428 * ntb_transport_qp_num - Query the qp number
1429 * @qp: NTB transport layer queue to be queried
1431 * Query qp number of the NTB transport queue
1433 * RETURNS: a zero based number specifying the qp number
1435 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1440 return (qp->qp_num);
1444 * ntb_transport_max_size - Query the max payload size of a qp
1445 * @qp: NTB transport layer queue to be queried
1447 * Query the maximum payload size permissible on the given qp
1449 * RETURNS: the max payload size of a qp
1452 ntb_transport_max_size(struct ntb_transport_qp *qp)
1458 return (qp->tx_max_frame - sizeof(struct ntb_payload_header));
1462 ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
1464 unsigned int head = qp->tx_index;
1465 unsigned int tail = qp->remote_rx_info->entry;
1467 return (tail >= head ? tail - head : qp->tx_max_entry + tail - head);
1470 static device_method_t ntb_transport_methods[] = {
1471 /* Device interface */
1472 DEVMETHOD(device_probe, ntb_transport_probe),
1473 DEVMETHOD(device_attach, ntb_transport_attach),
1474 DEVMETHOD(device_detach, ntb_transport_detach),
1478 devclass_t ntb_transport_devclass;
1479 static DEFINE_CLASS_0(ntb_transport, ntb_transport_driver,
1480 ntb_transport_methods, sizeof(struct ntb_transport_ctx));
1481 DRIVER_MODULE(ntb_transport, ntb_hw, ntb_transport_driver,
1482 ntb_transport_devclass, NULL, NULL);
1483 MODULE_DEPEND(ntb_transport, ntb, 1, 1, 1);
1484 MODULE_VERSION(ntb_transport, 1);