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