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