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