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