ntb_transport.c revision 304347
1/*-
2 * Copyright (c) 2016 Alexander Motin <mav@FreeBSD.org>
3 * Copyright (C) 2013 Intel Corporation
4 * Copyright (C) 2015 EMC Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29/*
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.
32 *
33 * This module contains a transport for sending and receiving messages by
34 * writing to remote memory window(s) provided by underlying NTB device.
35 *
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.
38 */
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: stable/11/sys/dev/ntb/ntb_transport.c 304347 2016-08-18 09:19:01Z mav $");
42
43#include <sys/param.h>
44#include <sys/kernel.h>
45#include <sys/systm.h>
46#include <sys/bitset.h>
47#include <sys/bus.h>
48#include <sys/ktr.h>
49#include <sys/limits.h>
50#include <sys/lock.h>
51#include <sys/malloc.h>
52#include <sys/mbuf.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>
58
59#include <vm/vm.h>
60#include <vm/pmap.h>
61
62#include <machine/bus.h>
63
64#include "ntb.h"
65#include "ntb_transport.h"
66
67#define QP_SETSIZE	64
68BITSET_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))
73
74#define KTR_NTB KTR_SPARE3
75
76#define NTB_TRANSPORT_VERSION	4
77
78static SYSCTL_NODE(_hw, OID_AUTO, ntb_transport, CTLFLAG_RW, 0, "ntb_transport");
79
80static unsigned g_ntb_transport_debug_level;
81SYSCTL_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__);			\
87	}						\
88} while (0)
89
90static unsigned transport_mtu = 0x10000;
91
92static uint64_t max_mw_size;
93SYSCTL_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.");
96
97static unsigned max_num_clients;
98SYSCTL_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.");
102
103static unsigned enable_xeon_watchdog;
104SYSCTL_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");
107
108STAILQ_HEAD(ntb_queue_list, ntb_queue_entry);
109
110typedef uint32_t ntb_q_idx_t;
111
112struct ntb_queue_entry {
113	/* ntb_queue list reference */
114	STAILQ_ENTRY(ntb_queue_entry) entry;
115
116	/* info on data to be transferred */
117	void		*cb_data;
118	void		*buf;
119	uint32_t	len;
120	uint32_t	flags;
121
122	struct ntb_transport_qp		*qp;
123	struct ntb_payload_header	*x_hdr;
124	ntb_q_idx_t	index;
125};
126
127struct ntb_rx_info {
128	ntb_q_idx_t	entry;
129};
130
131struct ntb_transport_qp {
132	struct ntb_transport_ctx	*transport;
133	device_t		 ntb;
134
135	void			*cb_data;
136
137	bool			client_ready;
138	volatile bool		link_is_up;
139	uint8_t			qp_num;	/* Only 64 QPs are allowed.  0-63 */
140
141	struct ntb_rx_info	*rx_info;
142	struct ntb_rx_info	*remote_rx_info;
143
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;
148	caddr_t			tx_mw;
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;
153
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		rx_completion_task;
161	struct task		rxc_db_work;
162	caddr_t			rx_buff;
163	ntb_q_idx_t		rx_index;
164	ntb_q_idx_t		rx_max_entry;
165	uint64_t		rx_max_frame;
166
167	void (*event_handler)(void *data, enum ntb_link_event status);
168	struct callout		link_work;
169	struct callout		rx_full;
170
171	uint64_t		last_rx_no_buf;
172
173	/* Stats */
174	uint64_t		rx_bytes;
175	uint64_t		rx_pkts;
176	uint64_t		rx_ring_empty;
177	uint64_t		rx_err_no_buf;
178	uint64_t		rx_err_oflow;
179	uint64_t		rx_err_ver;
180	uint64_t		tx_bytes;
181	uint64_t		tx_pkts;
182	uint64_t		tx_ring_full;
183	uint64_t		tx_err_no_buf;
184};
185
186struct ntb_transport_mw {
187	vm_paddr_t	phys_addr;
188	size_t		phys_size;
189	size_t		xlat_align;
190	size_t		xlat_align_size;
191	bus_addr_t	addr_limit;
192	/* Tx buff is off vbase / phys_addr */
193	caddr_t		vbase;
194	size_t		xlat_size;
195	size_t		buff_size;
196	/* Rx buff is off virt_addr / dma_addr */
197	caddr_t		virt_addr;
198	bus_addr_t	dma_addr;
199};
200
201struct ntb_transport_ctx {
202	device_t		 ntb;
203	struct ntb_transport_mw	*mw_vec;
204	struct ntb_transport_qp	*qp_vec;
205	struct _qpset		qp_bitmap;
206	struct _qpset		qp_bitmap_free;
207	unsigned		mw_count;
208	unsigned		qp_count;
209	volatile bool		link_is_up;
210	struct callout		link_work;
211	struct callout		link_watchdog;
212	struct task		link_cleanup;
213	struct mtx		tx_lock;
214	struct mtx		rx_lock;
215};
216
217enum {
218	NTBT_DESC_DONE_FLAG = 1 << 0,
219	NTBT_LINK_DOWN_FLAG = 1 << 1,
220};
221
222struct ntb_payload_header {
223	ntb_q_idx_t ver;
224	uint32_t len;
225	uint32_t flags;
226};
227
228enum {
229	/*
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.
233	 */
234	NTBT_VERSION = 0,
235	NTBT_QP_LINKS,
236	NTBT_NUM_QPS,
237	NTBT_NUM_MWS,
238	/*
239	 * N.B.: transport_link_work assumes MW1 enums = MW0 + 2.
240	 */
241	NTBT_MW0_SZ_HIGH,
242	NTBT_MW0_SZ_LOW,
243	NTBT_MW1_SZ_HIGH,
244	NTBT_MW1_SZ_LOW,
245	NTBT_MAX_SPAD,
246
247	/*
248	 * Some NTB-using hardware have a watchdog to work around NTB hangs; if
249	 * a register or doorbell isn't written every few seconds, the link is
250	 * torn down.  Write an otherwise unused register every few seconds to
251	 * work around this watchdog.
252	 */
253	NTBT_WATCHDOG_SPAD = 15
254};
255
256#define QP_TO_MW(nt, qp)	((qp) % nt->mw_count)
257#define NTB_QP_DEF_NUM_ENTRIES	100
258#define NTB_LINK_DOWN_TIMEOUT	10
259
260static int ntb_transport_probe(device_t dev);
261static int ntb_transport_attach(device_t dev);
262static int ntb_transport_detach(device_t dev);
263static void ntb_transport_init_queue(struct ntb_transport_ctx *nt,
264    unsigned int qp_num);
265static int ntb_process_tx(struct ntb_transport_qp *qp,
266    struct ntb_queue_entry *entry);
267static void ntb_memcpy_tx(struct ntb_transport_qp *qp,
268    struct ntb_queue_entry *entry, void *offset);
269static void ntb_transport_rxc_db(void *arg, int pending);
270static int ntb_process_rxc(struct ntb_transport_qp *qp);
271static void ntb_memcpy_rx(struct ntb_transport_qp *qp,
272    struct ntb_queue_entry *entry, void *offset);
273static inline void ntb_rx_copy_callback(struct ntb_transport_qp *qp,
274    void *data);
275static void ntb_complete_rxc(void *arg, int pending);
276static void ntb_transport_doorbell_callback(void *data, uint32_t vector);
277static void ntb_transport_event_callback(void *data);
278static void ntb_transport_link_work(void *arg);
279static int ntb_set_mw(struct ntb_transport_ctx *, int num_mw, size_t size);
280static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw);
281static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
282    unsigned int qp_num);
283static void ntb_qp_link_work(void *arg);
284static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt);
285static void ntb_transport_link_cleanup_work(void *, int);
286static void ntb_qp_link_down(struct ntb_transport_qp *qp);
287static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp);
288static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp);
289static void ntb_send_link_down(struct ntb_transport_qp *qp);
290static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
291    struct ntb_queue_list *list);
292static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock,
293    struct ntb_queue_list *list);
294static struct ntb_queue_entry *ntb_list_mv(struct mtx *lock,
295    struct ntb_queue_list *from, struct ntb_queue_list *to);
296static void xeon_link_watchdog_hb(void *);
297
298static const struct ntb_ctx_ops ntb_transport_ops = {
299	.link_event = ntb_transport_event_callback,
300	.db_event = ntb_transport_doorbell_callback,
301};
302
303MALLOC_DEFINE(M_NTB_T, "ntb_transport", "ntb transport driver");
304
305static inline void
306iowrite32(uint32_t val, void *addr)
307{
308
309	bus_space_write_4(X86_BUS_SPACE_MEM, 0/* HACK */, (uintptr_t)addr,
310	    val);
311}
312
313/* Transport Init and teardown */
314
315static void
316xeon_link_watchdog_hb(void *arg)
317{
318	struct ntb_transport_ctx *nt;
319
320	nt = arg;
321	NTB_SPAD_WRITE(nt->ntb, NTBT_WATCHDOG_SPAD, 0);
322	callout_reset(&nt->link_watchdog, 1 * hz, xeon_link_watchdog_hb, nt);
323}
324
325static int
326ntb_transport_probe(device_t dev)
327{
328
329	device_set_desc(dev, "NTB Transport");
330	return (0);
331}
332
333static int
334ntb_transport_attach(device_t dev)
335{
336	struct ntb_transport_ctx *nt = device_get_softc(dev);
337	device_t ntb = device_get_parent(dev);
338	struct ntb_transport_mw *mw;
339	uint64_t qp_bitmap;
340	int rc;
341	unsigned i;
342
343	nt->ntb = ntb;
344	nt->mw_count = NTB_MW_COUNT(ntb);
345	nt->mw_vec = malloc(nt->mw_count * sizeof(*nt->mw_vec), M_NTB_T,
346	    M_WAITOK | M_ZERO);
347	for (i = 0; i < nt->mw_count; i++) {
348		mw = &nt->mw_vec[i];
349
350		rc = NTB_MW_GET_RANGE(ntb, i, &mw->phys_addr, &mw->vbase,
351		    &mw->phys_size, &mw->xlat_align, &mw->xlat_align_size,
352		    &mw->addr_limit);
353		if (rc != 0)
354			goto err;
355
356		mw->buff_size = 0;
357		mw->xlat_size = 0;
358		mw->virt_addr = NULL;
359		mw->dma_addr = 0;
360
361		rc = NTB_MW_SET_WC(nt->ntb, i, VM_MEMATTR_WRITE_COMBINING);
362		if (rc)
363			ntb_printf(0, "Unable to set mw%d caching\n", i);
364	}
365
366	qp_bitmap = NTB_DB_VALID_MASK(ntb);
367	nt->qp_count = flsll(qp_bitmap);
368	KASSERT(nt->qp_count != 0, ("bogus db bitmap"));
369	nt->qp_count -= 1;
370
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"));
376
377	mtx_init(&nt->tx_lock, "ntb transport tx", NULL, MTX_DEF);
378	mtx_init(&nt->rx_lock, "ntb transport rx", NULL, MTX_DEF);
379
380	nt->qp_vec = malloc(nt->qp_count * sizeof(*nt->qp_vec), M_NTB_T,
381	    M_WAITOK | M_ZERO);
382
383	for (i = 0; i < nt->qp_count; i++) {
384		set_bit(i, &nt->qp_bitmap);
385		set_bit(i, &nt->qp_bitmap_free);
386		ntb_transport_init_queue(nt, i);
387	}
388
389	callout_init(&nt->link_work, 0);
390	callout_init(&nt->link_watchdog, 0);
391	TASK_INIT(&nt->link_cleanup, 0, ntb_transport_link_cleanup_work, nt);
392
393	rc = NTB_SET_CTX(ntb, nt, &ntb_transport_ops);
394	if (rc != 0)
395		goto err;
396
397	nt->link_is_up = false;
398	NTB_LINK_ENABLE(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
399
400	callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
401	if (enable_xeon_watchdog != 0)
402		callout_reset(&nt->link_watchdog, 0, xeon_link_watchdog_hb, nt);
403
404	/* Attach children to this transport */
405	device_add_child(dev, NULL, -1);
406	bus_generic_attach(dev);
407
408	return (0);
409
410err:
411	free(nt->qp_vec, M_NTB_T);
412	free(nt->mw_vec, M_NTB_T);
413	return (rc);
414}
415
416static int
417ntb_transport_detach(device_t dev)
418{
419	struct ntb_transport_ctx *nt = device_get_softc(dev);
420	device_t ntb = nt->ntb;
421	struct _qpset qp_bitmap_alloc;
422	uint8_t i;
423
424	/* Detach & delete all children */
425	device_delete_children(dev);
426
427	ntb_transport_link_cleanup(nt);
428	taskqueue_drain(taskqueue_swi, &nt->link_cleanup);
429	callout_drain(&nt->link_work);
430	callout_drain(&nt->link_watchdog);
431
432	BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc);
433	BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free);
434
435	/* Verify that all the QPs are freed */
436	for (i = 0; i < nt->qp_count; i++)
437		if (test_bit(i, &qp_bitmap_alloc))
438			ntb_transport_free_queue(&nt->qp_vec[i]);
439
440	NTB_LINK_DISABLE(ntb);
441	NTB_CLEAR_CTX(ntb);
442
443	for (i = 0; i < nt->mw_count; i++)
444		ntb_free_mw(nt, i);
445
446	free(nt->qp_vec, M_NTB_T);
447	free(nt->mw_vec, M_NTB_T);
448	return (0);
449}
450
451static void
452ntb_transport_init_queue(struct ntb_transport_ctx *nt, unsigned int qp_num)
453{
454	struct ntb_transport_mw *mw;
455	struct ntb_transport_qp *qp;
456	vm_paddr_t mw_base;
457	uint64_t mw_size, qp_offset;
458	size_t tx_size;
459	unsigned num_qps_mw, mw_num, mw_count;
460
461	mw_count = nt->mw_count;
462	mw_num = QP_TO_MW(nt, qp_num);
463	mw = &nt->mw_vec[mw_num];
464
465	qp = &nt->qp_vec[qp_num];
466	qp->qp_num = qp_num;
467	qp->transport = nt;
468	qp->ntb = nt->ntb;
469	qp->client_ready = false;
470	qp->event_handler = NULL;
471	ntb_qp_link_down_reset(qp);
472
473	if (mw_num < nt->qp_count % mw_count)
474		num_qps_mw = nt->qp_count / mw_count + 1;
475	else
476		num_qps_mw = nt->qp_count / mw_count;
477
478	mw_base = mw->phys_addr;
479	mw_size = mw->phys_size;
480
481	tx_size = mw_size / num_qps_mw;
482	qp_offset = tx_size * (qp_num / mw_count);
483
484	qp->tx_mw = mw->vbase + qp_offset;
485	KASSERT(qp->tx_mw != NULL, ("uh oh?"));
486
487	/* XXX Assumes that a vm_paddr_t is equivalent to bus_addr_t */
488	qp->tx_mw_phys = mw_base + qp_offset;
489	KASSERT(qp->tx_mw_phys != 0, ("uh oh?"));
490
491	tx_size -= sizeof(struct ntb_rx_info);
492	qp->rx_info = (void *)(qp->tx_mw + tx_size);
493
494	/* Due to house-keeping, there must be at least 2 buffs */
495	qp->tx_max_frame = qmin(tx_size / 2,
496	    transport_mtu + sizeof(struct ntb_payload_header));
497	qp->tx_max_entry = tx_size / qp->tx_max_frame;
498
499	callout_init(&qp->link_work, 0);
500	callout_init(&qp->rx_full, 1);
501
502	mtx_init(&qp->ntb_rx_q_lock, "ntb rx q", NULL, MTX_SPIN);
503	mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN);
504	TASK_INIT(&qp->rx_completion_task, 0, ntb_complete_rxc, qp);
505	TASK_INIT(&qp->rxc_db_work, 0, ntb_transport_rxc_db, qp);
506
507	STAILQ_INIT(&qp->rx_post_q);
508	STAILQ_INIT(&qp->rx_pend_q);
509	STAILQ_INIT(&qp->tx_free_q);
510
511	callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
512}
513
514void
515ntb_transport_free_queue(struct ntb_transport_qp *qp)
516{
517	struct ntb_queue_entry *entry;
518
519	if (qp == NULL)
520		return;
521
522	callout_drain(&qp->link_work);
523
524	NTB_DB_SET_MASK(qp->ntb, 1ull << qp->qp_num);
525	taskqueue_drain(taskqueue_swi, &qp->rxc_db_work);
526	taskqueue_drain(taskqueue_swi, &qp->rx_completion_task);
527
528	qp->cb_data = NULL;
529	qp->rx_handler = NULL;
530	qp->tx_handler = NULL;
531	qp->event_handler = NULL;
532
533	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q)))
534		free(entry, M_NTB_T);
535
536	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q)))
537		free(entry, M_NTB_T);
538
539	while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
540		free(entry, M_NTB_T);
541
542	set_bit(qp->qp_num, &qp->transport->qp_bitmap_free);
543}
544
545/**
546 * ntb_transport_create_queue - Create a new NTB transport layer queue
547 * @rx_handler: receive callback function
548 * @tx_handler: transmit callback function
549 * @event_handler: event callback function
550 *
551 * Create a new NTB transport layer queue and provide the queue with a callback
552 * routine for both transmit and receive.  The receive callback routine will be
553 * used to pass up data when the transport has received it on the queue.   The
554 * transmit callback routine will be called when the transport has completed the
555 * transmission of the data on the queue and the data is ready to be freed.
556 *
557 * RETURNS: pointer to newly created ntb_queue, NULL on error.
558 */
559struct ntb_transport_qp *
560ntb_transport_create_queue(void *data, device_t dev,
561    const struct ntb_queue_handlers *handlers)
562{
563	struct ntb_transport_ctx *nt = device_get_softc(dev);
564	device_t ntb = device_get_parent(dev);
565	struct ntb_queue_entry *entry;
566	struct ntb_transport_qp *qp;
567	unsigned int free_queue;
568	int i;
569
570	free_queue = ffs_bit(&nt->qp_bitmap_free);
571	if (free_queue == 0)
572		return (NULL);
573
574	/* decrement free_queue to make it zero based */
575	free_queue--;
576
577	qp = &nt->qp_vec[free_queue];
578	clear_bit(qp->qp_num, &nt->qp_bitmap_free);
579	qp->cb_data = data;
580	qp->rx_handler = handlers->rx_handler;
581	qp->tx_handler = handlers->tx_handler;
582	qp->event_handler = handlers->event_handler;
583
584	for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
585		entry = malloc(sizeof(*entry), M_NTB_T, M_WAITOK | M_ZERO);
586		entry->cb_data = data;
587		entry->buf = NULL;
588		entry->len = transport_mtu;
589		ntb_list_add(&qp->ntb_rx_q_lock, entry, &qp->rx_pend_q);
590	}
591
592	for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
593		entry = malloc(sizeof(*entry), M_NTB_T, M_WAITOK | M_ZERO);
594		ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
595	}
596
597	NTB_DB_CLEAR(ntb, 1ull << qp->qp_num);
598	NTB_DB_CLEAR_MASK(ntb, 1ull << qp->qp_num);
599	return (qp);
600}
601
602/**
603 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
604 * @qp: NTB transport layer queue to be enabled
605 *
606 * Notify NTB transport layer of client readiness to use queue
607 */
608void
609ntb_transport_link_up(struct ntb_transport_qp *qp)
610{
611	struct ntb_transport_ctx *nt = qp->transport;
612
613	qp->client_ready = true;
614
615	ntb_printf(2, "qp %d client ready\n", qp->qp_num);
616
617	if (nt->link_is_up)
618		callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
619}
620
621
622
623/* Transport Tx */
624
625/**
626 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
627 * @qp: NTB transport layer queue the entry is to be enqueued on
628 * @cb: per buffer pointer for callback function to use
629 * @data: pointer to data buffer that will be sent
630 * @len: length of the data buffer
631 *
632 * Enqueue a new transmit buffer onto the transport queue from which a NTB
633 * payload will be transmitted.  This assumes that a lock is being held to
634 * serialize access to the qp.
635 *
636 * RETURNS: An appropriate ERRNO error value on error, or zero for success.
637 */
638int
639ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
640    unsigned int len)
641{
642	struct ntb_queue_entry *entry;
643	int rc;
644
645	if (qp == NULL || !qp->link_is_up || len == 0) {
646		CTR0(KTR_NTB, "TX: link not up");
647		return (EINVAL);
648	}
649
650	entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
651	if (entry == NULL) {
652		CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
653		qp->tx_err_no_buf++;
654		return (EBUSY);
655	}
656	CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
657
658	entry->cb_data = cb;
659	entry->buf = data;
660	entry->len = len;
661	entry->flags = 0;
662
663	mtx_lock(&qp->transport->tx_lock);
664	rc = ntb_process_tx(qp, entry);
665	mtx_unlock(&qp->transport->tx_lock);
666	if (rc != 0) {
667		ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
668		CTR1(KTR_NTB,
669		    "TX: process_tx failed. Returning entry %p to tx_free_q",
670		    entry);
671	}
672	return (rc);
673}
674
675static int
676ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
677{
678	void *offset;
679
680	offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
681	CTR3(KTR_NTB,
682	    "TX: process_tx: tx_pkts=%lu, tx_index=%u, remote entry=%u",
683	    qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry);
684	if (qp->tx_index == qp->remote_rx_info->entry) {
685		CTR0(KTR_NTB, "TX: ring full");
686		qp->tx_ring_full++;
687		return (EAGAIN);
688	}
689
690	if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
691		if (qp->tx_handler != NULL)
692			qp->tx_handler(qp, qp->cb_data, entry->buf,
693			    EIO);
694		else
695			m_freem(entry->buf);
696
697		entry->buf = NULL;
698		ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
699		CTR1(KTR_NTB,
700		    "TX: frame too big. returning entry %p to tx_free_q",
701		    entry);
702		return (0);
703	}
704	CTR2(KTR_NTB, "TX: copying entry %p to offset %p", entry, offset);
705	ntb_memcpy_tx(qp, entry, offset);
706
707	qp->tx_index++;
708	qp->tx_index %= qp->tx_max_entry;
709
710	qp->tx_pkts++;
711
712	return (0);
713}
714
715static void
716ntb_memcpy_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
717    void *offset)
718{
719	struct ntb_payload_header *hdr;
720
721	/* This piece is from Linux' ntb_async_tx() */
722	hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame -
723	    sizeof(struct ntb_payload_header));
724	entry->x_hdr = hdr;
725	iowrite32(entry->len, &hdr->len);
726	iowrite32(qp->tx_pkts, &hdr->ver);
727
728	/* This piece is ntb_memcpy_tx() */
729	CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset);
730	if (entry->buf != NULL) {
731		m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset);
732
733		/*
734		 * Ensure that the data is fully copied before setting the
735		 * flags
736		 */
737		wmb();
738	}
739
740	/* The rest is ntb_tx_copy_callback() */
741	iowrite32(entry->flags | NTBT_DESC_DONE_FLAG, &hdr->flags);
742	CTR1(KTR_NTB, "TX: hdr %p set DESC_DONE", hdr);
743
744	NTB_PEER_DB_SET(qp->ntb, 1ull << qp->qp_num);
745
746	/*
747	 * The entry length can only be zero if the packet is intended to be a
748	 * "link down" or similar.  Since no payload is being sent in these
749	 * cases, there is nothing to add to the completion queue.
750	 */
751	if (entry->len > 0) {
752		qp->tx_bytes += entry->len;
753
754		if (qp->tx_handler)
755			qp->tx_handler(qp, qp->cb_data, entry->buf,
756			    entry->len);
757		else
758			m_freem(entry->buf);
759		entry->buf = NULL;
760	}
761
762	CTR3(KTR_NTB,
763	    "TX: entry %p sent. hdr->ver = %u, hdr->flags = 0x%x, Returning "
764	    "to tx_free_q", entry, hdr->ver, hdr->flags);
765	ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
766}
767
768/* Transport Rx */
769static void
770ntb_transport_rxc_db(void *arg, int pending __unused)
771{
772	struct ntb_transport_qp *qp = arg;
773	ntb_q_idx_t i;
774	int rc;
775
776	/*
777	 * Limit the number of packets processed in a single interrupt to
778	 * provide fairness to others
779	 */
780	CTR0(KTR_NTB, "RX: transport_rx");
781	mtx_lock(&qp->transport->rx_lock);
782	for (i = 0; i < qp->rx_max_entry; i++) {
783		rc = ntb_process_rxc(qp);
784		if (rc != 0) {
785			CTR0(KTR_NTB, "RX: process_rxc failed");
786			break;
787		}
788	}
789	mtx_unlock(&qp->transport->rx_lock);
790
791	if (i == qp->rx_max_entry)
792		taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
793	else if ((NTB_DB_READ(qp->ntb) & (1ull << qp->qp_num)) != 0) {
794		/* If db is set, clear it and read it back to commit clear. */
795		NTB_DB_CLEAR(qp->ntb, 1ull << qp->qp_num);
796		(void)NTB_DB_READ(qp->ntb);
797
798		/*
799		 * An interrupt may have arrived between finishing
800		 * ntb_process_rxc and clearing the doorbell bit: there might
801		 * be some more work to do.
802		 */
803		taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
804	}
805}
806
807static int
808ntb_process_rxc(struct ntb_transport_qp *qp)
809{
810	struct ntb_payload_header *hdr;
811	struct ntb_queue_entry *entry;
812	caddr_t offset;
813
814	offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
815	hdr = (void *)(offset + qp->rx_max_frame -
816	    sizeof(struct ntb_payload_header));
817
818	CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index);
819	if ((hdr->flags & NTBT_DESC_DONE_FLAG) == 0) {
820		CTR0(KTR_NTB, "RX: hdr not done");
821		qp->rx_ring_empty++;
822		return (EAGAIN);
823	}
824
825	if ((hdr->flags & NTBT_LINK_DOWN_FLAG) != 0) {
826		CTR0(KTR_NTB, "RX: link down");
827		ntb_qp_link_down(qp);
828		hdr->flags = 0;
829		return (EAGAIN);
830	}
831
832	if (hdr->ver != (uint32_t)qp->rx_pkts) {
833		CTR2(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). "
834		    "Returning entry to rx_pend_q", hdr->ver, qp->rx_pkts);
835		qp->rx_err_ver++;
836		return (EIO);
837	}
838
839	entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
840	if (entry == NULL) {
841		qp->rx_err_no_buf++;
842		CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
843		return (EAGAIN);
844	}
845	callout_stop(&qp->rx_full);
846	CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
847
848	entry->x_hdr = hdr;
849	entry->index = qp->rx_index;
850
851	if (hdr->len > entry->len) {
852		CTR2(KTR_NTB, "RX: len too long. Wanted %ju got %ju",
853		    (uintmax_t)hdr->len, (uintmax_t)entry->len);
854		qp->rx_err_oflow++;
855
856		entry->len = -EIO;
857		entry->flags |= NTBT_DESC_DONE_FLAG;
858
859		taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
860	} else {
861		qp->rx_bytes += hdr->len;
862		qp->rx_pkts++;
863
864		CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
865
866		entry->len = hdr->len;
867
868		ntb_memcpy_rx(qp, entry, offset);
869	}
870
871	qp->rx_index++;
872	qp->rx_index %= qp->rx_max_entry;
873	return (0);
874}
875
876static void
877ntb_memcpy_rx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
878    void *offset)
879{
880	struct ifnet *ifp = entry->cb_data;
881	unsigned int len = entry->len;
882
883	CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset);
884
885	entry->buf = (void *)m_devget(offset, len, 0, ifp, NULL);
886
887	/* Ensure that the data is globally visible before clearing the flag */
888	wmb();
889
890	CTR2(KTR_NTB, "RX: copied entry %p to mbuf %p.", entry, m);
891	ntb_rx_copy_callback(qp, entry);
892}
893
894static inline void
895ntb_rx_copy_callback(struct ntb_transport_qp *qp, void *data)
896{
897	struct ntb_queue_entry *entry;
898
899	entry = data;
900	entry->flags |= NTBT_DESC_DONE_FLAG;
901	taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
902}
903
904static void
905ntb_complete_rxc(void *arg, int pending)
906{
907	struct ntb_transport_qp *qp = arg;
908	struct ntb_queue_entry *entry;
909	struct mbuf *m;
910	unsigned len;
911
912	CTR0(KTR_NTB, "RX: rx_completion_task");
913
914	mtx_lock_spin(&qp->ntb_rx_q_lock);
915
916	while (!STAILQ_EMPTY(&qp->rx_post_q)) {
917		entry = STAILQ_FIRST(&qp->rx_post_q);
918		if ((entry->flags & NTBT_DESC_DONE_FLAG) == 0)
919			break;
920
921		entry->x_hdr->flags = 0;
922		iowrite32(entry->index, &qp->rx_info->entry);
923
924		STAILQ_REMOVE_HEAD(&qp->rx_post_q, entry);
925
926		len = entry->len;
927		m = entry->buf;
928
929		/*
930		 * Re-initialize queue_entry for reuse; rx_handler takes
931		 * ownership of the mbuf.
932		 */
933		entry->buf = NULL;
934		entry->len = transport_mtu;
935		entry->cb_data = qp->cb_data;
936
937		STAILQ_INSERT_TAIL(&qp->rx_pend_q, entry, entry);
938
939		mtx_unlock_spin(&qp->ntb_rx_q_lock);
940
941		CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m);
942		if (qp->rx_handler != NULL && qp->client_ready)
943			qp->rx_handler(qp, qp->cb_data, m, len);
944		else
945			m_freem(m);
946
947		mtx_lock_spin(&qp->ntb_rx_q_lock);
948	}
949
950	mtx_unlock_spin(&qp->ntb_rx_q_lock);
951}
952
953static void
954ntb_transport_doorbell_callback(void *data, uint32_t vector)
955{
956	struct ntb_transport_ctx *nt = data;
957	struct ntb_transport_qp *qp;
958	struct _qpset db_bits;
959	uint64_t vec_mask;
960	unsigned qp_num;
961
962	BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &db_bits);
963	BIT_NAND(QP_SETSIZE, &db_bits, &nt->qp_bitmap_free);
964
965	vec_mask = NTB_DB_VECTOR_MASK(nt->ntb, vector);
966	while (vec_mask != 0) {
967		qp_num = ffsll(vec_mask) - 1;
968
969		if (test_bit(qp_num, &db_bits)) {
970			qp = &nt->qp_vec[qp_num];
971			taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
972		}
973
974		vec_mask &= ~(1ull << qp_num);
975	}
976}
977
978/* Link Event handler */
979static void
980ntb_transport_event_callback(void *data)
981{
982	struct ntb_transport_ctx *nt = data;
983
984	if (NTB_LINK_IS_UP(nt->ntb, NULL, NULL)) {
985		ntb_printf(1, "HW link up\n");
986		callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
987	} else {
988		ntb_printf(1, "HW link down\n");
989		taskqueue_enqueue(taskqueue_swi, &nt->link_cleanup);
990	}
991}
992
993/* Link bring up */
994static void
995ntb_transport_link_work(void *arg)
996{
997	struct ntb_transport_ctx *nt = arg;
998	device_t ntb = nt->ntb;
999	struct ntb_transport_qp *qp;
1000	uint64_t val64, size;
1001	uint32_t val;
1002	unsigned i;
1003	int rc;
1004
1005	/* send the local info, in the opposite order of the way we read it */
1006	for (i = 0; i < nt->mw_count; i++) {
1007		size = nt->mw_vec[i].phys_size;
1008
1009		if (max_mw_size != 0 && size > max_mw_size)
1010			size = max_mw_size;
1011
1012		NTB_PEER_SPAD_WRITE(ntb, NTBT_MW0_SZ_HIGH + (i * 2),
1013		    size >> 32);
1014		NTB_PEER_SPAD_WRITE(ntb, NTBT_MW0_SZ_LOW + (i * 2), size);
1015	}
1016
1017	NTB_PEER_SPAD_WRITE(ntb, NTBT_NUM_MWS, nt->mw_count);
1018
1019	NTB_PEER_SPAD_WRITE(ntb, NTBT_NUM_QPS, nt->qp_count);
1020
1021	NTB_PEER_SPAD_WRITE(ntb, NTBT_VERSION, NTB_TRANSPORT_VERSION);
1022
1023	/* Query the remote side for its info */
1024	val = 0;
1025	NTB_SPAD_READ(ntb, NTBT_VERSION, &val);
1026	if (val != NTB_TRANSPORT_VERSION)
1027		goto out;
1028
1029	NTB_SPAD_READ(ntb, NTBT_NUM_QPS, &val);
1030	if (val != nt->qp_count)
1031		goto out;
1032
1033	NTB_SPAD_READ(ntb, NTBT_NUM_MWS, &val);
1034	if (val != nt->mw_count)
1035		goto out;
1036
1037	for (i = 0; i < nt->mw_count; i++) {
1038		NTB_SPAD_READ(ntb, NTBT_MW0_SZ_HIGH + (i * 2), &val);
1039		val64 = (uint64_t)val << 32;
1040
1041		NTB_SPAD_READ(ntb, NTBT_MW0_SZ_LOW + (i * 2), &val);
1042		val64 |= val;
1043
1044		rc = ntb_set_mw(nt, i, val64);
1045		if (rc != 0)
1046			goto free_mws;
1047	}
1048
1049	nt->link_is_up = true;
1050	ntb_printf(1, "transport link up\n");
1051
1052	for (i = 0; i < nt->qp_count; i++) {
1053		qp = &nt->qp_vec[i];
1054
1055		ntb_transport_setup_qp_mw(nt, i);
1056
1057		if (qp->client_ready)
1058			callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1059	}
1060
1061	return;
1062
1063free_mws:
1064	for (i = 0; i < nt->mw_count; i++)
1065		ntb_free_mw(nt, i);
1066out:
1067	if (NTB_LINK_IS_UP(ntb, NULL, NULL))
1068		callout_reset(&nt->link_work,
1069		    NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt);
1070}
1071
1072static int
1073ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, size_t size)
1074{
1075	struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1076	size_t xlat_size, buff_size;
1077	int rc;
1078
1079	if (size == 0)
1080		return (EINVAL);
1081
1082	xlat_size = roundup(size, mw->xlat_align_size);
1083	buff_size = xlat_size;
1084
1085	/* No need to re-setup */
1086	if (mw->xlat_size == xlat_size)
1087		return (0);
1088
1089	if (mw->buff_size != 0)
1090		ntb_free_mw(nt, num_mw);
1091
1092	/* Alloc memory for receiving data.  Must be aligned */
1093	mw->xlat_size = xlat_size;
1094	mw->buff_size = buff_size;
1095
1096	mw->virt_addr = contigmalloc(mw->buff_size, M_NTB_T, M_ZERO, 0,
1097	    mw->addr_limit, mw->xlat_align, 0);
1098	if (mw->virt_addr == NULL) {
1099		ntb_printf(0, "Unable to allocate MW buffer of size %zu/%zu\n",
1100		    mw->buff_size, mw->xlat_size);
1101		mw->xlat_size = 0;
1102		mw->buff_size = 0;
1103		return (ENOMEM);
1104	}
1105	/* TODO: replace with bus_space_* functions */
1106	mw->dma_addr = vtophys(mw->virt_addr);
1107
1108	/*
1109	 * Ensure that the allocation from contigmalloc is aligned as
1110	 * requested.  XXX: This may not be needed -- brought in for parity
1111	 * with the Linux driver.
1112	 */
1113	if (mw->dma_addr % mw->xlat_align != 0) {
1114		ntb_printf(0,
1115		    "DMA memory 0x%jx not aligned to BAR size 0x%zx\n",
1116		    (uintmax_t)mw->dma_addr, size);
1117		ntb_free_mw(nt, num_mw);
1118		return (ENOMEM);
1119	}
1120
1121	/* Notify HW the memory location of the receive buffer */
1122	rc = NTB_MW_SET_TRANS(nt->ntb, num_mw, mw->dma_addr, mw->xlat_size);
1123	if (rc) {
1124		ntb_printf(0, "Unable to set mw%d translation\n", num_mw);
1125		ntb_free_mw(nt, num_mw);
1126		return (rc);
1127	}
1128
1129	return (0);
1130}
1131
1132static void
1133ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
1134{
1135	struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1136
1137	if (mw->virt_addr == NULL)
1138		return;
1139
1140	NTB_MW_CLEAR_TRANS(nt->ntb, num_mw);
1141	contigfree(mw->virt_addr, mw->xlat_size, M_NTB_T);
1142	mw->xlat_size = 0;
1143	mw->buff_size = 0;
1144	mw->virt_addr = NULL;
1145}
1146
1147static int
1148ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, unsigned int qp_num)
1149{
1150	struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
1151	struct ntb_transport_mw *mw;
1152	void *offset;
1153	ntb_q_idx_t i;
1154	size_t rx_size;
1155	unsigned num_qps_mw, mw_num, mw_count;
1156
1157	mw_count = nt->mw_count;
1158	mw_num = QP_TO_MW(nt, qp_num);
1159	mw = &nt->mw_vec[mw_num];
1160
1161	if (mw->virt_addr == NULL)
1162		return (ENOMEM);
1163
1164	if (mw_num < nt->qp_count % mw_count)
1165		num_qps_mw = nt->qp_count / mw_count + 1;
1166	else
1167		num_qps_mw = nt->qp_count / mw_count;
1168
1169	rx_size = mw->xlat_size / num_qps_mw;
1170	qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
1171	rx_size -= sizeof(struct ntb_rx_info);
1172
1173	qp->remote_rx_info = (void*)(qp->rx_buff + rx_size);
1174
1175	/* Due to house-keeping, there must be at least 2 buffs */
1176	qp->rx_max_frame = qmin(rx_size / 2,
1177	    transport_mtu + sizeof(struct ntb_payload_header));
1178	qp->rx_max_entry = rx_size / qp->rx_max_frame;
1179	qp->rx_index = 0;
1180
1181	qp->remote_rx_info->entry = qp->rx_max_entry - 1;
1182
1183	/* Set up the hdr offsets with 0s */
1184	for (i = 0; i < qp->rx_max_entry; i++) {
1185		offset = (void *)(qp->rx_buff + qp->rx_max_frame * (i + 1) -
1186		    sizeof(struct ntb_payload_header));
1187		memset(offset, 0, sizeof(struct ntb_payload_header));
1188	}
1189
1190	qp->rx_pkts = 0;
1191	qp->tx_pkts = 0;
1192	qp->tx_index = 0;
1193
1194	return (0);
1195}
1196
1197static void
1198ntb_qp_link_work(void *arg)
1199{
1200	struct ntb_transport_qp *qp = arg;
1201	device_t ntb = qp->ntb;
1202	struct ntb_transport_ctx *nt = qp->transport;
1203	uint32_t val, dummy;
1204
1205	NTB_SPAD_READ(ntb, NTBT_QP_LINKS, &val);
1206
1207	NTB_PEER_SPAD_WRITE(ntb, NTBT_QP_LINKS, val | (1ull << qp->qp_num));
1208
1209	/* query remote spad for qp ready bits */
1210	NTB_PEER_SPAD_READ(ntb, NTBT_QP_LINKS, &dummy);
1211
1212	/* See if the remote side is up */
1213	if ((val & (1ull << qp->qp_num)) != 0) {
1214		ntb_printf(2, "qp %d link up\n", qp->qp_num);
1215		qp->link_is_up = true;
1216
1217		if (qp->event_handler != NULL)
1218			qp->event_handler(qp->cb_data, NTB_LINK_UP);
1219
1220		taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1221	} else if (nt->link_is_up)
1222		callout_reset(&qp->link_work,
1223		    NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1224}
1225
1226/* Link down event*/
1227static void
1228ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
1229{
1230	struct ntb_transport_qp *qp;
1231	struct _qpset qp_bitmap_alloc;
1232	unsigned i;
1233
1234	BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc);
1235	BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free);
1236
1237	/* Pass along the info to any clients */
1238	for (i = 0; i < nt->qp_count; i++)
1239		if (test_bit(i, &qp_bitmap_alloc)) {
1240			qp = &nt->qp_vec[i];
1241			ntb_qp_link_cleanup(qp);
1242			callout_drain(&qp->link_work);
1243		}
1244
1245	if (!nt->link_is_up)
1246		callout_drain(&nt->link_work);
1247
1248	/*
1249	 * The scratchpad registers keep the values if the remote side
1250	 * goes down, blast them now to give them a sane value the next
1251	 * time they are accessed
1252	 */
1253	for (i = 0; i < NTBT_MAX_SPAD; i++)
1254		NTB_SPAD_WRITE(nt->ntb, i, 0);
1255}
1256
1257static void
1258ntb_transport_link_cleanup_work(void *arg, int pending __unused)
1259{
1260
1261	ntb_transport_link_cleanup(arg);
1262}
1263
1264static void
1265ntb_qp_link_down(struct ntb_transport_qp *qp)
1266{
1267
1268	ntb_qp_link_cleanup(qp);
1269}
1270
1271static void
1272ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
1273{
1274
1275	qp->link_is_up = false;
1276
1277	qp->tx_index = qp->rx_index = 0;
1278	qp->tx_bytes = qp->rx_bytes = 0;
1279	qp->tx_pkts = qp->rx_pkts = 0;
1280
1281	qp->rx_ring_empty = 0;
1282	qp->tx_ring_full = 0;
1283
1284	qp->rx_err_no_buf = qp->tx_err_no_buf = 0;
1285	qp->rx_err_oflow = qp->rx_err_ver = 0;
1286}
1287
1288static void
1289ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1290{
1291	struct ntb_transport_ctx *nt = qp->transport;
1292
1293	callout_drain(&qp->link_work);
1294	ntb_qp_link_down_reset(qp);
1295
1296	if (qp->event_handler != NULL)
1297		qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1298
1299	if (nt->link_is_up)
1300		callout_reset(&qp->link_work,
1301		    NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1302}
1303
1304/* Link commanded down */
1305/**
1306 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1307 * @qp: NTB transport layer queue to be disabled
1308 *
1309 * Notify NTB transport layer of client's desire to no longer receive data on
1310 * transport queue specified.  It is the client's responsibility to ensure all
1311 * entries on queue are purged or otherwise handled appropriately.
1312 */
1313void
1314ntb_transport_link_down(struct ntb_transport_qp *qp)
1315{
1316	uint32_t val;
1317
1318	if (qp == NULL)
1319		return;
1320
1321	qp->client_ready = false;
1322
1323	NTB_SPAD_READ(qp->ntb, NTBT_QP_LINKS, &val);
1324
1325	NTB_PEER_SPAD_WRITE(qp->ntb, NTBT_QP_LINKS,
1326	   val & ~(1 << qp->qp_num));
1327
1328	if (qp->link_is_up)
1329		ntb_send_link_down(qp);
1330	else
1331		callout_drain(&qp->link_work);
1332}
1333
1334/**
1335 * ntb_transport_link_query - Query transport link state
1336 * @qp: NTB transport layer queue to be queried
1337 *
1338 * Query connectivity to the remote system of the NTB transport queue
1339 *
1340 * RETURNS: true for link up or false for link down
1341 */
1342bool
1343ntb_transport_link_query(struct ntb_transport_qp *qp)
1344{
1345	if (qp == NULL)
1346		return (false);
1347
1348	return (qp->link_is_up);
1349}
1350
1351static void
1352ntb_send_link_down(struct ntb_transport_qp *qp)
1353{
1354	struct ntb_queue_entry *entry;
1355	int i, rc;
1356
1357	if (!qp->link_is_up)
1358		return;
1359
1360	for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1361		entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1362		if (entry != NULL)
1363			break;
1364		pause("NTB Wait for link down", hz / 10);
1365	}
1366
1367	if (entry == NULL)
1368		return;
1369
1370	entry->cb_data = NULL;
1371	entry->buf = NULL;
1372	entry->len = 0;
1373	entry->flags = NTBT_LINK_DOWN_FLAG;
1374
1375	mtx_lock(&qp->transport->tx_lock);
1376	rc = ntb_process_tx(qp, entry);
1377	if (rc != 0)
1378		printf("ntb: Failed to send link down\n");
1379	mtx_unlock(&qp->transport->tx_lock);
1380
1381	ntb_qp_link_down_reset(qp);
1382}
1383
1384
1385/* List Management */
1386
1387static void
1388ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1389    struct ntb_queue_list *list)
1390{
1391
1392	mtx_lock_spin(lock);
1393	STAILQ_INSERT_TAIL(list, entry, entry);
1394	mtx_unlock_spin(lock);
1395}
1396
1397static struct ntb_queue_entry *
1398ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1399{
1400	struct ntb_queue_entry *entry;
1401
1402	mtx_lock_spin(lock);
1403	if (STAILQ_EMPTY(list)) {
1404		entry = NULL;
1405		goto out;
1406	}
1407	entry = STAILQ_FIRST(list);
1408	STAILQ_REMOVE_HEAD(list, entry);
1409out:
1410	mtx_unlock_spin(lock);
1411
1412	return (entry);
1413}
1414
1415static struct ntb_queue_entry *
1416ntb_list_mv(struct mtx *lock, struct ntb_queue_list *from,
1417    struct ntb_queue_list *to)
1418{
1419	struct ntb_queue_entry *entry;
1420
1421	mtx_lock_spin(lock);
1422	if (STAILQ_EMPTY(from)) {
1423		entry = NULL;
1424		goto out;
1425	}
1426	entry = STAILQ_FIRST(from);
1427	STAILQ_REMOVE_HEAD(from, entry);
1428	STAILQ_INSERT_TAIL(to, entry, entry);
1429
1430out:
1431	mtx_unlock_spin(lock);
1432	return (entry);
1433}
1434
1435/**
1436 * ntb_transport_qp_num - Query the qp number
1437 * @qp: NTB transport layer queue to be queried
1438 *
1439 * Query qp number of the NTB transport queue
1440 *
1441 * RETURNS: a zero based number specifying the qp number
1442 */
1443unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1444{
1445	if (qp == NULL)
1446		return 0;
1447
1448	return (qp->qp_num);
1449}
1450
1451/**
1452 * ntb_transport_max_size - Query the max payload size of a qp
1453 * @qp: NTB transport layer queue to be queried
1454 *
1455 * Query the maximum payload size permissible on the given qp
1456 *
1457 * RETURNS: the max payload size of a qp
1458 */
1459unsigned int
1460ntb_transport_max_size(struct ntb_transport_qp *qp)
1461{
1462
1463	if (qp == NULL)
1464		return (0);
1465
1466	return (qp->tx_max_frame - sizeof(struct ntb_payload_header));
1467}
1468
1469unsigned int
1470ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
1471{
1472	unsigned int head = qp->tx_index;
1473	unsigned int tail = qp->remote_rx_info->entry;
1474
1475	return (tail >= head ? tail - head : qp->tx_max_entry + tail - head);
1476}
1477
1478static device_method_t ntb_transport_methods[] = {
1479	/* Device interface */
1480	DEVMETHOD(device_probe,     ntb_transport_probe),
1481	DEVMETHOD(device_attach,    ntb_transport_attach),
1482	DEVMETHOD(device_detach,    ntb_transport_detach),
1483	DEVMETHOD_END
1484};
1485
1486devclass_t ntb_transport_devclass;
1487static DEFINE_CLASS_0(ntb_transport, ntb_transport_driver,
1488    ntb_transport_methods, sizeof(struct ntb_transport_ctx));
1489DRIVER_MODULE(ntb_transport, ntb_hw, ntb_transport_driver,
1490    ntb_transport_devclass, NULL, NULL);
1491MODULE_DEPEND(ntb_transport, ntb, 1, 1, 1);
1492MODULE_VERSION(ntb_transport, 1);
1493