ntb_transport.c revision 290679
1/*- 2 * Copyright (C) 2013 Intel Corporation 3 * Copyright (C) 2015 EMC Corporation 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/dev/ntb/if_ntb/if_ntb.c 290679 2015-11-11 18:54:58Z cem $"); 30 31#include <sys/param.h> 32#include <sys/kernel.h> 33#include <sys/systm.h> 34#include <sys/bitset.h> 35#include <sys/bus.h> 36#include <sys/ktr.h> 37#include <sys/limits.h> 38#include <sys/lock.h> 39#include <sys/malloc.h> 40#include <sys/module.h> 41#include <sys/mutex.h> 42#include <sys/queue.h> 43#include <sys/socket.h> 44#include <sys/sockio.h> 45#include <sys/sysctl.h> 46#include <sys/taskqueue.h> 47 48#include <net/if.h> 49#include <net/if_media.h> 50#include <net/if_types.h> 51#include <net/if_var.h> 52#include <net/bpf.h> 53#include <net/ethernet.h> 54 55#include <vm/vm.h> 56#include <vm/pmap.h> 57 58#include <machine/bus.h> 59#include <machine/cpufunc.h> 60#include <machine/pmap.h> 61 62#include "../ntb_hw/ntb_hw.h" 63 64/* 65 * The Non-Transparent Bridge (NTB) is a device on some Intel processors that 66 * allows you to connect two systems using a PCI-e link. 67 * 68 * This module contains a protocol for sending and receiving messages, and 69 * exposes that protocol through a simulated ethernet device called ntb. 70 * 71 * NOTE: Much of the code in this module is shared with Linux. Any patches may 72 * be picked up and redistributed in Linux with a dual GPL/BSD license. 73 */ 74 75#define QP_SETSIZE 64 76BITSET_DEFINE(_qpset, QP_SETSIZE); 77#define test_bit(pos, addr) BIT_ISSET(QP_SETSIZE, (pos), (addr)) 78#define set_bit(pos, addr) BIT_SET(QP_SETSIZE, (pos), (addr)) 79#define clear_bit(pos, addr) BIT_CLR(QP_SETSIZE, (pos), (addr)) 80#define ffs_bit(addr) BIT_FFS(QP_SETSIZE, (addr)) 81 82#define KTR_NTB KTR_SPARE3 83 84#define NTB_TRANSPORT_VERSION 4 85#define NTB_RX_MAX_PKTS 64 86#define NTB_RXQ_SIZE 300 87 88enum ntb_link_event { 89 NTB_LINK_DOWN = 0, 90 NTB_LINK_UP, 91}; 92 93static unsigned int transport_mtu = 0x10000 + ETHER_HDR_LEN + ETHER_CRC_LEN; 94 95static uint64_t max_mw_size; 96SYSCTL_UQUAD(_hw_ntb, OID_AUTO, max_mw_size, CTLFLAG_RDTUN, &max_mw_size, 0, 97 "If enabled (non-zero), limit the size of large memory windows. " 98 "Both sides of the NTB MUST set the same value here."); 99 100static unsigned int max_num_clients; 101SYSCTL_UINT(_hw_ntb, OID_AUTO, max_num_clients, CTLFLAG_RDTUN, 102 &max_num_clients, 0, "Maximum number of NTB transport clients. " 103 "0 (default) - use all available NTB memory windows; " 104 "positive integer N - Limit to N memory windows."); 105 106STAILQ_HEAD(ntb_queue_list, ntb_queue_entry); 107 108typedef unsigned ntb_q_idx_t; 109 110struct ntb_queue_entry { 111 /* ntb_queue list reference */ 112 STAILQ_ENTRY(ntb_queue_entry) entry; 113 114 /* info on data to be transferred */ 115 void *cb_data; 116 void *buf; 117 unsigned len; 118 unsigned flags; 119 120 struct ntb_transport_qp *qp; 121 struct ntb_payload_header *x_hdr; 122 ntb_q_idx_t index; 123}; 124 125struct ntb_rx_info { 126 ntb_q_idx_t entry; 127}; 128 129struct ntb_transport_qp { 130 struct ntb_transport_ctx *transport; 131 struct ntb_softc *ntb; 132 133 void *cb_data; 134 135 bool client_ready; 136 bool link_is_up; 137 uint8_t qp_num; /* Only 64 QPs are allowed. 0-63 */ 138 139 struct ntb_rx_info *rx_info; 140 struct ntb_rx_info *remote_rx_info; 141 142 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data, 143 void *data, int len); 144 struct ntb_queue_list tx_free_q; 145 struct mtx ntb_tx_free_q_lock; 146 caddr_t tx_mw; 147 bus_addr_t tx_mw_phys; 148 ntb_q_idx_t tx_index; 149 ntb_q_idx_t tx_max_entry; 150 uint64_t tx_max_frame; 151 152 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data, 153 void *data, int len); 154 struct ntb_queue_list rx_post_q; 155 struct ntb_queue_list rx_pend_q; 156 struct ntb_queue_list rx_free_q; 157 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */ 158 struct mtx ntb_rx_q_lock; 159 struct task rx_completion_task; 160 struct task rxc_db_work; 161 caddr_t rx_buff; 162 ntb_q_idx_t rx_index; 163 ntb_q_idx_t rx_max_entry; 164 uint64_t rx_max_frame; 165 166 void (*event_handler)(void *data, enum ntb_link_event status); 167 struct callout link_work; 168 struct callout queue_full; 169 struct callout rx_full; 170 171 uint64_t last_rx_no_buf; 172 173 /* Stats */ 174 uint64_t rx_bytes; 175 uint64_t rx_pkts; 176 uint64_t rx_ring_empty; 177 uint64_t rx_err_no_buf; 178 uint64_t rx_err_oflow; 179 uint64_t rx_err_ver; 180 uint64_t tx_bytes; 181 uint64_t tx_pkts; 182 uint64_t tx_ring_full; 183 uint64_t tx_err_no_buf; 184}; 185 186struct ntb_queue_handlers { 187 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data, 188 void *data, int len); 189 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data, 190 void *data, int len); 191 void (*event_handler)(void *data, enum ntb_link_event status); 192}; 193 194struct ntb_transport_mw { 195 vm_paddr_t phys_addr; 196 size_t phys_size; 197 size_t xlat_align; 198 size_t xlat_align_size; 199 /* Tx buff is off vbase / phys_addr */ 200 caddr_t vbase; 201 size_t xlat_size; 202 size_t buff_size; 203 /* Rx buff is off virt_addr / dma_addr */ 204 caddr_t virt_addr; 205 bus_addr_t dma_addr; 206}; 207 208struct ntb_transport_ctx { 209 struct ntb_softc *ntb; 210 struct ifnet *ifp; 211 struct ntb_transport_mw mw_vec[NTB_MAX_NUM_MW]; 212 struct ntb_transport_qp *qp_vec; 213 struct _qpset qp_bitmap; 214 struct _qpset qp_bitmap_free; 215 unsigned mw_count; 216 unsigned qp_count; 217 enum ntb_link_event link_is_up; 218 struct callout link_work; 219 uint64_t bufsize; 220 u_char eaddr[ETHER_ADDR_LEN]; 221 struct mtx tx_lock; 222 struct mtx rx_lock; 223 224 /* The hardcoded single queuepair in ntb_setup_interface() */ 225 struct ntb_transport_qp *qp; 226}; 227 228static struct ntb_transport_ctx net_softc; 229 230enum { 231 IF_NTB_DESC_DONE_FLAG = 1 << 0, 232 IF_NTB_LINK_DOWN_FLAG = 1 << 1, 233}; 234 235struct ntb_payload_header { 236 uint64_t ver; 237 uint64_t len; 238 uint64_t flags; 239}; 240 241enum { 242 /* 243 * The order of this enum is part of the if_ntb remote protocol. Do 244 * not reorder without bumping protocol version (and it's probably best 245 * to keep the protocol in lock-step with the Linux NTB driver. 246 */ 247 IF_NTB_VERSION = 0, 248 IF_NTB_QP_LINKS, 249 IF_NTB_NUM_QPS, 250 IF_NTB_NUM_MWS, 251 /* 252 * N.B.: transport_link_work assumes MW1 enums = MW0 + 2. 253 */ 254 IF_NTB_MW0_SZ_HIGH, 255 IF_NTB_MW0_SZ_LOW, 256 IF_NTB_MW1_SZ_HIGH, 257 IF_NTB_MW1_SZ_LOW, 258 IF_NTB_MAX_SPAD, 259}; 260 261#define QP_TO_MW(nt, qp) ((qp) % nt->mw_count) 262#define NTB_QP_DEF_NUM_ENTRIES 100 263#define NTB_LINK_DOWN_TIMEOUT 10 264 265static int ntb_handle_module_events(struct module *m, int what, void *arg); 266static int ntb_setup_interface(void); 267static int ntb_teardown_interface(void); 268static void ntb_net_init(void *arg); 269static int ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data); 270static void ntb_start(struct ifnet *ifp); 271static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, 272 void *data, int len); 273static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, 274 void *data, int len); 275static void ntb_net_event_handler(void *data, enum ntb_link_event status); 276static int ntb_transport_init(struct ntb_softc *ntb); 277static void ntb_transport_free(struct ntb_transport_ctx *); 278static void ntb_transport_init_queue(struct ntb_transport_ctx *nt, 279 unsigned int qp_num); 280static void ntb_transport_free_queue(struct ntb_transport_qp *qp); 281static struct ntb_transport_qp *ntb_transport_create_queue(void *data, 282 struct ntb_softc *pdev, const struct ntb_queue_handlers *handlers); 283static void ntb_transport_link_up(struct ntb_transport_qp *qp); 284static int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, 285 void *data, unsigned int len); 286static int ntb_process_tx(struct ntb_transport_qp *qp, 287 struct ntb_queue_entry *entry); 288static void ntb_memcpy_tx(struct ntb_transport_qp *qp, 289 struct ntb_queue_entry *entry, void *offset); 290static void ntb_qp_full(void *arg); 291static void ntb_transport_rxc_db(void *arg, int pending); 292static int ntb_process_rxc(struct ntb_transport_qp *qp); 293static void ntb_memcpy_rx(struct ntb_transport_qp *qp, 294 struct ntb_queue_entry *entry, void *offset); 295static inline void ntb_rx_copy_callback(struct ntb_transport_qp *qp, 296 void *data); 297static void ntb_complete_rxc(void *arg, int pending); 298static void ntb_transport_doorbell_callback(void *data, uint32_t vector); 299static void ntb_transport_event_callback(void *data); 300static void ntb_transport_link_work(void *arg); 301static int ntb_set_mw(struct ntb_transport_ctx *, int num_mw, size_t size); 302static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw); 303static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, 304 unsigned int qp_num); 305static void ntb_qp_link_work(void *arg); 306static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt); 307static void ntb_qp_link_down(struct ntb_transport_qp *qp); 308static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp); 309static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp); 310static void ntb_transport_link_down(struct ntb_transport_qp *qp); 311static void ntb_send_link_down(struct ntb_transport_qp *qp); 312static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry, 313 struct ntb_queue_list *list); 314static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock, 315 struct ntb_queue_list *list); 316static struct ntb_queue_entry *ntb_list_mv(struct mtx *lock, 317 struct ntb_queue_list *from, struct ntb_queue_list *to); 318static void create_random_local_eui48(u_char *eaddr); 319static unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp); 320 321static const struct ntb_ctx_ops ntb_transport_ops = { 322 .link_event = ntb_transport_event_callback, 323 .db_event = ntb_transport_doorbell_callback, 324}; 325 326MALLOC_DEFINE(M_NTB_IF, "if_ntb", "ntb network driver"); 327 328/* Module setup and teardown */ 329static int 330ntb_handle_module_events(struct module *m, int what, void *arg) 331{ 332 int err = 0; 333 334 switch (what) { 335 case MOD_LOAD: 336 err = ntb_setup_interface(); 337 break; 338 case MOD_UNLOAD: 339 err = ntb_teardown_interface(); 340 break; 341 default: 342 err = EOPNOTSUPP; 343 break; 344 } 345 return (err); 346} 347 348static moduledata_t if_ntb_mod = { 349 "if_ntb", 350 ntb_handle_module_events, 351 NULL 352}; 353 354DECLARE_MODULE(if_ntb, if_ntb_mod, SI_SUB_KLD, SI_ORDER_ANY); 355MODULE_DEPEND(if_ntb, ntb_hw, 1, 1, 1); 356 357static int 358ntb_setup_interface(void) 359{ 360 struct ifnet *ifp; 361 struct ntb_queue_handlers handlers = { ntb_net_rx_handler, 362 ntb_net_tx_handler, ntb_net_event_handler }; 363 int rc; 364 365 net_softc.ntb = devclass_get_softc(devclass_find("ntb_hw"), 0); 366 if (net_softc.ntb == NULL) { 367 printf("ntb: Cannot find devclass\n"); 368 return (ENXIO); 369 } 370 371 rc = ntb_transport_init(net_softc.ntb); 372 if (rc != 0) { 373 printf("ntb: Cannot init transport: %d\n", rc); 374 return (rc); 375 } 376 377 ifp = net_softc.ifp = if_alloc(IFT_ETHER); 378 if (ifp == NULL) { 379 ntb_transport_free(&net_softc); 380 printf("ntb: Cannot allocate ifnet structure\n"); 381 return (ENOMEM); 382 } 383 384 net_softc.qp = ntb_transport_create_queue(ifp, net_softc.ntb, 385 &handlers); 386 if_initname(ifp, "ntb", 0); 387 ifp->if_init = ntb_net_init; 388 ifp->if_softc = &net_softc; 389 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX; 390 ifp->if_ioctl = ntb_ioctl; 391 ifp->if_start = ntb_start; 392 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 393 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 394 IFQ_SET_READY(&ifp->if_snd); 395 create_random_local_eui48(net_softc.eaddr); 396 ether_ifattach(ifp, net_softc.eaddr); 397 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_JUMBO_MTU; 398 ifp->if_capenable = ifp->if_capabilities; 399 400 ntb_transport_link_up(net_softc.qp); 401 net_softc.bufsize = ntb_transport_max_size(net_softc.qp) + 402 sizeof(struct ether_header); 403 return (0); 404} 405 406static int 407ntb_teardown_interface(void) 408{ 409 410 if (net_softc.qp != NULL) 411 ntb_transport_link_down(net_softc.qp); 412 413 if (net_softc.ifp != NULL) { 414 ether_ifdetach(net_softc.ifp); 415 if_free(net_softc.ifp); 416 } 417 418 if (net_softc.qp != NULL) { 419 ntb_transport_free_queue(net_softc.qp); 420 ntb_transport_free(&net_softc); 421 } 422 423 return (0); 424} 425 426/* Network device interface */ 427 428static void 429ntb_net_init(void *arg) 430{ 431 struct ntb_transport_ctx *ntb_softc = arg; 432 struct ifnet *ifp = ntb_softc->ifp; 433 434 ifp->if_drv_flags |= IFF_DRV_RUNNING; 435 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 436 ifp->if_flags |= IFF_UP; 437 if_link_state_change(ifp, LINK_STATE_UP); 438} 439 440static int 441ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 442{ 443 struct ntb_transport_ctx *nt = ifp->if_softc; 444 struct ifreq *ifr = (struct ifreq *)data; 445 int error = 0; 446 447 switch (command) { 448 case SIOCSIFMTU: 449 { 450 if (ifr->ifr_mtu > ntb_transport_max_size(nt->qp) - 451 ETHER_HDR_LEN - ETHER_CRC_LEN) { 452 error = EINVAL; 453 break; 454 } 455 456 ifp->if_mtu = ifr->ifr_mtu; 457 break; 458 } 459 default: 460 error = ether_ioctl(ifp, command, data); 461 break; 462 } 463 464 return (error); 465} 466 467 468static void 469ntb_start(struct ifnet *ifp) 470{ 471 struct mbuf *m_head; 472 struct ntb_transport_ctx *nt = ifp->if_softc; 473 int rc; 474 475 mtx_lock(&nt->tx_lock); 476 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 477 CTR0(KTR_NTB, "TX: ntb_start"); 478 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { 479 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 480 CTR1(KTR_NTB, "TX: start mbuf %p", m_head); 481 rc = ntb_transport_tx_enqueue(nt->qp, m_head, m_head, 482 m_length(m_head, NULL)); 483 if (rc != 0) { 484 CTR1(KTR_NTB, 485 "TX: could not tx mbuf %p. Returning to snd q", 486 m_head); 487 if (rc == EAGAIN) { 488 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 489 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 490 callout_reset(&nt->qp->queue_full, hz / 1000, 491 ntb_qp_full, ifp); 492 } 493 break; 494 } 495 496 } 497 mtx_unlock(&nt->tx_lock); 498} 499 500/* Network Device Callbacks */ 501static void 502ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data, 503 int len) 504{ 505 506 m_freem(data); 507 CTR1(KTR_NTB, "TX: tx_handler freeing mbuf %p", data); 508} 509 510static void 511ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data, 512 int len) 513{ 514 struct mbuf *m = data; 515 struct ifnet *ifp = qp_data; 516 517 CTR0(KTR_NTB, "RX: rx handler"); 518 (*ifp->if_input)(ifp, m); 519} 520 521static void 522ntb_net_event_handler(void *data, enum ntb_link_event status) 523{ 524 struct ifnet *ifp; 525 526 ifp = data; 527 (void)ifp; 528 529 /* XXX The Linux driver munges with the carrier status here. */ 530 531 switch (status) { 532 case NTB_LINK_DOWN: 533 break; 534 case NTB_LINK_UP: 535 break; 536 default: 537 panic("Bogus ntb_link_event %u\n", status); 538 } 539} 540 541/* Transport Init and teardown */ 542 543static int 544ntb_transport_init(struct ntb_softc *ntb) 545{ 546 struct ntb_transport_ctx *nt = &net_softc; 547 struct ntb_transport_mw *mw; 548 uint64_t qp_bitmap; 549 int rc; 550 unsigned i; 551 552 nt->mw_count = ntb_mw_count(ntb); 553 for (i = 0; i < nt->mw_count; i++) { 554 mw = &nt->mw_vec[i]; 555 556 rc = ntb_mw_get_range(ntb, i, &mw->phys_addr, &mw->vbase, 557 &mw->phys_size, &mw->xlat_align, &mw->xlat_align_size); 558 if (rc != 0) 559 goto err; 560 561 mw->buff_size = 0; 562 mw->xlat_size = 0; 563 mw->virt_addr = NULL; 564 mw->dma_addr = 0; 565 } 566 567 qp_bitmap = ntb_db_valid_mask(ntb); 568 nt->qp_count = flsll(qp_bitmap); 569 KASSERT(nt->qp_count != 0, ("bogus db bitmap")); 570 nt->qp_count -= 1; 571 572 if (max_num_clients != 0 && max_num_clients < nt->qp_count) 573 nt->qp_count = max_num_clients; 574 else if (nt->mw_count < nt->qp_count) 575 nt->qp_count = nt->mw_count; 576 KASSERT(nt->qp_count <= QP_SETSIZE, ("invalid qp_count")); 577 578 mtx_init(&nt->tx_lock, "ntb transport tx", NULL, MTX_DEF); 579 mtx_init(&nt->rx_lock, "ntb transport rx", NULL, MTX_DEF); 580 581 nt->qp_vec = malloc(nt->qp_count * sizeof(*nt->qp_vec), M_NTB_IF, 582 M_WAITOK | M_ZERO); 583 584 for (i = 0; i < nt->qp_count; i++) { 585 set_bit(i, &nt->qp_bitmap); 586 set_bit(i, &nt->qp_bitmap_free); 587 ntb_transport_init_queue(nt, i); 588 } 589 590 callout_init(&nt->link_work, 0); 591 592 rc = ntb_set_ctx(ntb, nt, &ntb_transport_ops); 593 if (rc != 0) 594 goto err; 595 596 nt->link_is_up = false; 597 ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO); 598 ntb_link_event(ntb); 599 return (0); 600 601err: 602 free(nt->qp_vec, M_NTB_IF); 603 nt->qp_vec = NULL; 604 return (rc); 605} 606 607static void 608ntb_transport_free(struct ntb_transport_ctx *nt) 609{ 610 struct ntb_softc *ntb = nt->ntb; 611 struct _qpset qp_bitmap_alloc; 612 uint8_t i; 613 614 ntb_transport_link_cleanup(nt); 615 616 callout_drain(&nt->link_work); 617 618 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc); 619 BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free); 620 621 /* Verify that all the QPs are freed */ 622 for (i = 0; i < nt->qp_count; i++) 623 if (test_bit(i, &qp_bitmap_alloc)) 624 ntb_transport_free_queue(&nt->qp_vec[i]); 625 626 ntb_link_disable(ntb); 627 ntb_clear_ctx(ntb); 628 629 for (i = 0; i < nt->mw_count; i++) 630 ntb_free_mw(nt, i); 631 632 free(nt->qp_vec, M_NTB_IF); 633} 634 635static void 636ntb_transport_init_queue(struct ntb_transport_ctx *nt, unsigned int qp_num) 637{ 638 struct ntb_transport_mw *mw; 639 struct ntb_transport_qp *qp; 640 vm_paddr_t mw_base; 641 uint64_t mw_size, qp_offset; 642 size_t tx_size; 643 unsigned num_qps_mw, mw_num, mw_count; 644 645 mw_count = nt->mw_count; 646 mw_num = QP_TO_MW(nt, qp_num); 647 mw = &nt->mw_vec[mw_num]; 648 649 qp = &nt->qp_vec[qp_num]; 650 qp->qp_num = qp_num; 651 qp->transport = nt; 652 qp->ntb = nt->ntb; 653 qp->client_ready = false; 654 qp->event_handler = NULL; 655 ntb_qp_link_down_reset(qp); 656 657 if (nt->qp_count % mw_count && mw_num + 1 < nt->qp_count / mw_count) 658 num_qps_mw = nt->qp_count / mw_count + 1; 659 else 660 num_qps_mw = nt->qp_count / mw_count; 661 662 mw_base = mw->phys_addr; 663 mw_size = mw->phys_size; 664 665 tx_size = mw_size / num_qps_mw; 666 qp_offset = tx_size * qp_num / mw_count; 667 668 qp->tx_mw = mw->vbase + qp_offset; 669 KASSERT(qp->tx_mw != NULL, ("uh oh?")); 670 671 /* XXX Assumes that a vm_paddr_t is equivalent to bus_addr_t */ 672 qp->tx_mw_phys = mw_base + qp_offset; 673 KASSERT(qp->tx_mw_phys != 0, ("uh oh?")); 674 675 tx_size -= sizeof(struct ntb_rx_info); 676 qp->rx_info = (void *)(qp->tx_mw + tx_size); 677 678 /* Due to house-keeping, there must be at least 2 buffs */ 679 qp->tx_max_frame = qmin(tx_size / 2, 680 transport_mtu + sizeof(struct ntb_payload_header)); 681 qp->tx_max_entry = tx_size / qp->tx_max_frame; 682 683 callout_init(&qp->link_work, 0); 684 callout_init(&qp->queue_full, 1); 685 callout_init(&qp->rx_full, 1); 686 687 mtx_init(&qp->ntb_rx_q_lock, "ntb rx q", NULL, MTX_SPIN); 688 mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN); 689 TASK_INIT(&qp->rx_completion_task, 0, ntb_complete_rxc, qp); 690 TASK_INIT(&qp->rxc_db_work, 0, ntb_transport_rxc_db, qp); 691 692 STAILQ_INIT(&qp->rx_post_q); 693 STAILQ_INIT(&qp->rx_pend_q); 694 STAILQ_INIT(&qp->rx_free_q); 695 STAILQ_INIT(&qp->tx_free_q); 696} 697 698static void 699ntb_transport_free_queue(struct ntb_transport_qp *qp) 700{ 701 struct ntb_queue_entry *entry; 702 703 if (qp == NULL) 704 return; 705 706 callout_drain(&qp->link_work); 707 708 ntb_db_set_mask(qp->ntb, 1ull << qp->qp_num); 709 taskqueue_drain(taskqueue_swi, &qp->rxc_db_work); 710 taskqueue_drain(taskqueue_swi, &qp->rx_completion_task); 711 712 qp->cb_data = NULL; 713 qp->rx_handler = NULL; 714 qp->tx_handler = NULL; 715 qp->event_handler = NULL; 716 717 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q))) 718 free(entry, M_NTB_IF); 719 720 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) 721 free(entry, M_NTB_IF); 722 723 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) 724 free(entry, M_NTB_IF); 725 726 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q))) 727 free(entry, M_NTB_IF); 728 729 set_bit(qp->qp_num, &qp->transport->qp_bitmap_free); 730} 731 732/** 733 * ntb_transport_create_queue - Create a new NTB transport layer queue 734 * @rx_handler: receive callback function 735 * @tx_handler: transmit callback function 736 * @event_handler: event callback function 737 * 738 * Create a new NTB transport layer queue and provide the queue with a callback 739 * routine for both transmit and receive. The receive callback routine will be 740 * used to pass up data when the transport has received it on the queue. The 741 * transmit callback routine will be called when the transport has completed the 742 * transmission of the data on the queue and the data is ready to be freed. 743 * 744 * RETURNS: pointer to newly created ntb_queue, NULL on error. 745 */ 746static struct ntb_transport_qp * 747ntb_transport_create_queue(void *data, struct ntb_softc *ntb, 748 const struct ntb_queue_handlers *handlers) 749{ 750 struct ntb_queue_entry *entry; 751 struct ntb_transport_qp *qp; 752 struct ntb_transport_ctx *nt; 753 unsigned int free_queue; 754 int i; 755 756 nt = ntb_get_ctx(ntb, NULL); 757 KASSERT(nt != NULL, ("bogus")); 758 759 free_queue = ffs_bit(&nt->qp_bitmap); 760 if (free_queue == 0) 761 return (NULL); 762 763 /* decrement free_queue to make it zero based */ 764 free_queue--; 765 766 qp = &nt->qp_vec[free_queue]; 767 clear_bit(1ull << qp->qp_num, &nt->qp_bitmap_free); 768 qp->cb_data = data; 769 qp->rx_handler = handlers->rx_handler; 770 qp->tx_handler = handlers->tx_handler; 771 qp->event_handler = handlers->event_handler; 772 773 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) { 774 entry = malloc(sizeof(*entry), M_NTB_IF, M_WAITOK | M_ZERO); 775 entry->cb_data = nt->ifp; 776 entry->buf = NULL; 777 entry->len = transport_mtu; 778 ntb_list_add(&qp->ntb_rx_q_lock, entry, &qp->rx_pend_q); 779 } 780 781 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) { 782 entry = malloc(sizeof(*entry), M_NTB_IF, M_WAITOK | M_ZERO); 783 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q); 784 } 785 786 ntb_db_clear(ntb, 1ull << qp->qp_num); 787 ntb_db_clear_mask(ntb, 1ull << qp->qp_num); 788 return (qp); 789} 790 791/** 792 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue 793 * @qp: NTB transport layer queue to be enabled 794 * 795 * Notify NTB transport layer of client readiness to use queue 796 */ 797static void 798ntb_transport_link_up(struct ntb_transport_qp *qp) 799{ 800 801 if (qp == NULL) 802 return; 803 804 qp->client_ready = true; 805 if (bootverbose) 806 if_printf(qp->transport->ifp, "qp client ready\n"); 807 808 if (qp->transport->link_is_up) 809 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp); 810} 811 812 813 814/* Transport Tx */ 815 816/** 817 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry 818 * @qp: NTB transport layer queue the entry is to be enqueued on 819 * @cb: per buffer pointer for callback function to use 820 * @data: pointer to data buffer that will be sent 821 * @len: length of the data buffer 822 * 823 * Enqueue a new transmit buffer onto the transport queue from which a NTB 824 * payload will be transmitted. This assumes that a lock is being held to 825 * serialize access to the qp. 826 * 827 * RETURNS: An appropriate ERRNO error value on error, or zero for success. 828 */ 829static int 830ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data, 831 unsigned int len) 832{ 833 struct ntb_queue_entry *entry; 834 int rc; 835 836 if (qp == NULL || !qp->link_is_up || len == 0) { 837 CTR0(KTR_NTB, "TX: link not up"); 838 return (EINVAL); 839 } 840 841 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q); 842 if (entry == NULL) { 843 CTR0(KTR_NTB, "TX: could not get entry from tx_free_q"); 844 qp->tx_err_no_buf++; 845 return (EBUSY); 846 } 847 CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry); 848 849 entry->cb_data = cb; 850 entry->buf = data; 851 entry->len = len; 852 entry->flags = 0; 853 854 rc = ntb_process_tx(qp, entry); 855 if (rc != 0) { 856 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q); 857 CTR1(KTR_NTB, 858 "TX: process_tx failed. Returning entry %p to tx_free_q", 859 entry); 860 } 861 return (rc); 862} 863 864static int 865ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry) 866{ 867 void *offset; 868 869 offset = (char *)qp->tx_mw + qp->tx_max_frame * qp->tx_index; 870 CTR3(KTR_NTB, 871 "TX: process_tx: tx_pkts=%u, tx_index=%u, remote entry=%u", 872 qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry); 873 if (qp->tx_index == qp->remote_rx_info->entry) { 874 CTR0(KTR_NTB, "TX: ring full"); 875 qp->tx_ring_full++; 876 return (EAGAIN); 877 } 878 879 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) { 880 if (qp->tx_handler != NULL) 881 qp->tx_handler(qp, qp->cb_data, entry->buf, 882 EIO); 883 884 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q); 885 CTR1(KTR_NTB, 886 "TX: frame too big. returning entry %p to tx_free_q", 887 entry); 888 return (0); 889 } 890 CTR2(KTR_NTB, "TX: copying entry %p to offset %p", entry, offset); 891 ntb_memcpy_tx(qp, entry, offset); 892 893 qp->tx_index++; 894 qp->tx_index %= qp->tx_max_entry; 895 896 qp->tx_pkts++; 897 898 return (0); 899} 900 901static void 902ntb_memcpy_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry, 903 void *offset) 904{ 905 struct ntb_payload_header *hdr; 906 907 /* This piece is from Linux' ntb_async_tx() */ 908 hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame - 909 sizeof(struct ntb_payload_header)); 910 entry->x_hdr = hdr; 911 hdr->len = entry->len; /* TODO: replace with bus_space_write */ 912 hdr->ver = qp->tx_pkts; /* TODO: replace with bus_space_write */ 913 914 /* This piece is ntb_memcpy_tx() */ 915 CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset); 916 if (entry->buf != NULL) { 917 m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset); 918 919 /* 920 * Ensure that the data is fully copied before setting the 921 * flags 922 */ 923 wmb(); 924 } 925 926 /* The rest is ntb_tx_copy_callback() */ 927 /* TODO: replace with bus_space_write */ 928 hdr->flags = entry->flags | IF_NTB_DESC_DONE_FLAG; 929 930 ntb_peer_db_set(qp->ntb, 1ull << qp->qp_num); 931 932 /* 933 * The entry length can only be zero if the packet is intended to be a 934 * "link down" or similar. Since no payload is being sent in these 935 * cases, there is nothing to add to the completion queue. 936 */ 937 if (entry->len > 0) { 938 qp->tx_bytes += entry->len; 939 940 if (qp->tx_handler) 941 qp->tx_handler(qp, qp->cb_data, entry->cb_data, 942 entry->len); 943 } 944 945 CTR2(KTR_NTB, 946 "TX: entry %p sent. hdr->ver = %d, Returning to tx_free_q", entry, 947 hdr->ver); 948 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q); 949} 950 951static void 952ntb_qp_full(void *arg) 953{ 954 955 CTR0(KTR_NTB, "TX: qp_full callout"); 956 ntb_start(arg); 957} 958 959/* Transport Rx */ 960static void 961ntb_transport_rxc_db(void *arg, int pending __unused) 962{ 963 struct ntb_transport_qp *qp = arg; 964 ntb_q_idx_t i; 965 int rc; 966 967 /* 968 * Limit the number of packets processed in a single interrupt to 969 * provide fairness to others 970 */ 971 CTR0(KTR_NTB, "RX: transport_rx"); 972 mtx_lock(&qp->transport->rx_lock); 973 for (i = 0; i < qp->rx_max_entry; i++) { 974 rc = ntb_process_rxc(qp); 975 if (rc != 0) { 976 CTR0(KTR_NTB, "RX: process_rxc failed"); 977 break; 978 } 979 } 980 mtx_unlock(&qp->transport->rx_lock); 981 982 if (i == qp->rx_max_entry) 983 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work); 984 else if ((ntb_db_read(qp->ntb) & (1ull << qp->qp_num)) != 0) { 985 /* If db is set, clear it and read it back to commit clear. */ 986 ntb_db_clear(qp->ntb, 1ull << qp->qp_num); 987 (void)ntb_db_read(qp->ntb); 988 989 /* 990 * An interrupt may have arrived between finishing 991 * ntb_process_rxc and clearing the doorbell bit: there might 992 * be some more work to do. 993 */ 994 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work); 995 } 996} 997 998static int 999ntb_process_rxc(struct ntb_transport_qp *qp) 1000{ 1001 struct ntb_payload_header *hdr; 1002 struct ntb_queue_entry *entry; 1003 void *offset; 1004 1005 offset = (void *) 1006 ((char *)qp->rx_buff + qp->rx_max_frame * qp->rx_index); 1007 hdr = (void *) 1008 ((char *)offset + qp->rx_max_frame - 1009 sizeof(struct ntb_payload_header)); 1010 1011 CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index); 1012 if ((hdr->flags & IF_NTB_DESC_DONE_FLAG) == 0) { 1013 CTR0(KTR_NTB, "RX: hdr not done"); 1014 qp->rx_ring_empty++; 1015 return (EAGAIN); 1016 } 1017 1018 if ((hdr->flags & IF_NTB_LINK_DOWN_FLAG) != 0) { 1019 CTR0(KTR_NTB, "RX: link down"); 1020 ntb_qp_link_down(qp); 1021 hdr->flags = 0; 1022 return (EAGAIN); 1023 } 1024 1025 if (hdr->ver != (uint32_t)qp->rx_pkts) { 1026 CTR2(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). " 1027 "Returning entry %p to rx_pend_q", hdr->ver, qp->rx_pkts); 1028 qp->rx_err_ver++; 1029 return (EIO); 1030 } 1031 1032 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q); 1033 if (entry == NULL) { 1034 qp->rx_err_no_buf++; 1035 CTR0(KTR_NTB, "RX: No entries in rx_pend_q"); 1036 return (EAGAIN); 1037 } 1038 callout_stop(&qp->rx_full); 1039 CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry); 1040 1041 entry->x_hdr = hdr; 1042 entry->index = qp->rx_index; 1043 1044 if (hdr->len > entry->len) { 1045 CTR2(KTR_NTB, "RX: len too long. Wanted %ju got %ju", 1046 (uintmax_t)hdr->len, (uintmax_t)entry->len); 1047 qp->rx_err_oflow++; 1048 1049 entry->len = -EIO; 1050 entry->flags |= IF_NTB_DESC_DONE_FLAG; 1051 1052 taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task); 1053 } else { 1054 qp->rx_bytes += hdr->len; 1055 qp->rx_pkts++; 1056 1057 CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts); 1058 1059 entry->len = hdr->len; 1060 1061 ntb_memcpy_rx(qp, entry, offset); 1062 } 1063 1064 qp->rx_index++; 1065 qp->rx_index %= qp->rx_max_entry; 1066 return (0); 1067} 1068 1069static void 1070ntb_memcpy_rx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry, 1071 void *offset) 1072{ 1073 struct ifnet *ifp = entry->cb_data; 1074 unsigned int len = entry->len; 1075 struct mbuf *m; 1076 1077 CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset); 1078 m = m_devget(offset, len, 0, ifp, NULL); 1079 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID; 1080 1081 entry->buf = (void *)m; 1082 1083 /* Ensure that the data is globally visible before clearing the flag */ 1084 wmb(); 1085 1086 CTR2(KTR_NTB, "RX: copied entry %p to mbuf %p.", entry, m); 1087 ntb_rx_copy_callback(qp, entry); 1088} 1089 1090static inline void 1091ntb_rx_copy_callback(struct ntb_transport_qp *qp, void *data) 1092{ 1093 struct ntb_queue_entry *entry; 1094 1095 entry = data; 1096 entry->flags |= IF_NTB_DESC_DONE_FLAG; 1097 taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task); 1098} 1099 1100static void 1101ntb_complete_rxc(void *arg, int pending) 1102{ 1103 struct ntb_transport_qp *qp = arg; 1104 struct ntb_queue_entry *entry; 1105 struct mbuf *m; 1106 unsigned len; 1107 1108 CTR0(KTR_NTB, "RX: rx_completion_task"); 1109 1110 mtx_lock_spin(&qp->ntb_rx_q_lock); 1111 1112 while (!STAILQ_EMPTY(&qp->rx_post_q)) { 1113 entry = STAILQ_FIRST(&qp->rx_post_q); 1114 if ((entry->flags & IF_NTB_DESC_DONE_FLAG) == 0) 1115 break; 1116 1117 entry->x_hdr->flags = 0; 1118 /* XXX bus_space_write */ 1119 qp->rx_info->entry = entry->index; 1120 1121 len = entry->len; 1122 m = entry->buf; 1123 1124 STAILQ_REMOVE_HEAD(&qp->rx_post_q, entry); 1125 STAILQ_INSERT_TAIL(&qp->rx_free_q, entry, entry); 1126 1127 mtx_unlock_spin(&qp->ntb_rx_q_lock); 1128 1129 CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m); 1130 if (qp->rx_handler != NULL && qp->client_ready) 1131 qp->rx_handler(qp, qp->cb_data, m, len); 1132 1133 mtx_lock_spin(&qp->ntb_rx_q_lock); 1134 } 1135 1136 mtx_unlock_spin(&qp->ntb_rx_q_lock); 1137} 1138 1139static void 1140ntb_transport_doorbell_callback(void *data, uint32_t vector) 1141{ 1142 struct ntb_transport_ctx *nt = data; 1143 struct ntb_transport_qp *qp; 1144 struct _qpset db_bits; 1145 uint64_t vec_mask; 1146 unsigned qp_num; 1147 1148 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &db_bits); 1149 BIT_NAND(QP_SETSIZE, &db_bits, &nt->qp_bitmap_free); 1150 1151 vec_mask = ntb_db_vector_mask(nt->ntb, vector); 1152 while (vec_mask != 0) { 1153 qp_num = ffsll(vec_mask) - 1; 1154 1155 if (test_bit(qp_num, &db_bits)) { 1156 qp = &nt->qp_vec[qp_num]; 1157 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work); 1158 } 1159 1160 vec_mask &= ~(1ull << qp_num); 1161 } 1162} 1163 1164/* Link Event handler */ 1165static void 1166ntb_transport_event_callback(void *data) 1167{ 1168 struct ntb_transport_ctx *nt = data; 1169 1170 if (ntb_link_is_up(nt->ntb, NULL, NULL)) { 1171 if (bootverbose) 1172 if_printf(nt->ifp, "HW link up\n"); 1173 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt); 1174 } else { 1175 if (bootverbose) 1176 if_printf(nt->ifp, "HW link down\n"); 1177 ntb_transport_link_cleanup(nt); 1178 } 1179} 1180 1181/* Link bring up */ 1182static void 1183ntb_transport_link_work(void *arg) 1184{ 1185 struct ntb_transport_ctx *nt = arg; 1186 struct ntb_softc *ntb = nt->ntb; 1187 struct ntb_transport_qp *qp; 1188 uint64_t val64, size; 1189 uint32_t val; 1190 unsigned i; 1191 int rc; 1192 1193 /* send the local info, in the opposite order of the way we read it */ 1194 for (i = 0; i < nt->mw_count; i++) { 1195 size = nt->mw_vec[i].phys_size; 1196 1197 if (max_mw_size != 0 && size > max_mw_size) 1198 size = max_mw_size; 1199 1200 ntb_peer_spad_write(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2), 1201 size >> 32); 1202 ntb_peer_spad_write(ntb, IF_NTB_MW0_SZ_LOW + (i * 2), size); 1203 } 1204 1205 ntb_peer_spad_write(ntb, IF_NTB_NUM_MWS, nt->mw_count); 1206 1207 ntb_peer_spad_write(ntb, IF_NTB_NUM_QPS, nt->qp_count); 1208 1209 ntb_peer_spad_write(ntb, IF_NTB_VERSION, NTB_TRANSPORT_VERSION); 1210 1211 /* Query the remote side for its info */ 1212 val = 0; 1213 ntb_spad_read(ntb, IF_NTB_VERSION, &val); 1214 if (val != NTB_TRANSPORT_VERSION) 1215 goto out; 1216 1217 ntb_spad_read(ntb, IF_NTB_NUM_QPS, &val); 1218 if (val != nt->qp_count) 1219 goto out; 1220 1221 ntb_spad_read(ntb, IF_NTB_NUM_MWS, &val); 1222 if (val != nt->mw_count) 1223 goto out; 1224 1225 for (i = 0; i < nt->mw_count; i++) { 1226 ntb_spad_read(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2), &val); 1227 val64 = (uint64_t)val << 32; 1228 1229 ntb_spad_read(ntb, IF_NTB_MW0_SZ_LOW + (i * 2), &val); 1230 val64 |= val; 1231 1232 rc = ntb_set_mw(nt, i, val64); 1233 if (rc != 0) 1234 goto free_mws; 1235 } 1236 1237 nt->link_is_up = true; 1238 if (bootverbose) 1239 if_printf(nt->ifp, "transport link up\n"); 1240 1241 for (i = 0; i < nt->qp_count; i++) { 1242 qp = &nt->qp_vec[i]; 1243 1244 ntb_transport_setup_qp_mw(nt, i); 1245 1246 if (qp->client_ready) 1247 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp); 1248 } 1249 1250 return; 1251 1252free_mws: 1253 for (i = 0; i < nt->mw_count; i++) 1254 ntb_free_mw(nt, i); 1255out: 1256 if (ntb_link_is_up(ntb, NULL, NULL)) 1257 callout_reset(&nt->link_work, 1258 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt); 1259} 1260 1261static int 1262ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, size_t size) 1263{ 1264 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw]; 1265 size_t xlat_size, buff_size; 1266 int rc; 1267 1268 if (size == 0) 1269 return (EINVAL); 1270 1271 xlat_size = roundup(size, mw->xlat_align_size); 1272 buff_size = roundup(size, mw->xlat_align); 1273 1274 /* No need to re-setup */ 1275 if (mw->xlat_size == xlat_size) 1276 return (0); 1277 1278 if (mw->buff_size != 0) 1279 ntb_free_mw(nt, num_mw); 1280 1281 /* Alloc memory for receiving data. Must be aligned */ 1282 mw->xlat_size = xlat_size; 1283 mw->buff_size = buff_size; 1284 1285 mw->virt_addr = contigmalloc(mw->buff_size, M_NTB_IF, M_ZERO, 0, 1286 BUS_SPACE_MAXADDR, mw->xlat_align, 0); 1287 if (mw->virt_addr == NULL) { 1288 mw->xlat_size = 0; 1289 mw->buff_size = 0; 1290 printf("ntb: Unable to allocate MW buffer of size %zu\n", 1291 mw->xlat_size); 1292 return (ENOMEM); 1293 } 1294 /* TODO: replace with bus_space_* functions */ 1295 mw->dma_addr = vtophys(mw->virt_addr); 1296 1297 /* 1298 * Ensure that the allocation from contigmalloc is aligned as 1299 * requested. XXX: This may not be needed -- brought in for parity 1300 * with the Linux driver. 1301 */ 1302 if (mw->dma_addr % mw->xlat_align != 0) { 1303 if_printf(nt->ifp, 1304 "DMA memory 0x%jx not aligned to BAR size 0x%zx\n", 1305 (uintmax_t)mw->dma_addr, size); 1306 ntb_free_mw(nt, num_mw); 1307 return (ENOMEM); 1308 } 1309 1310 /* Notify HW the memory location of the receive buffer */ 1311 rc = ntb_mw_set_trans(nt->ntb, num_mw, mw->dma_addr, mw->xlat_size); 1312 if (rc) { 1313 if_printf(nt->ifp, "Unable to set mw%d translation", num_mw); 1314 ntb_free_mw(nt, num_mw); 1315 return (rc); 1316 } 1317 1318 return (0); 1319} 1320 1321static void 1322ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw) 1323{ 1324 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw]; 1325 1326 if (mw->virt_addr == NULL) 1327 return; 1328 1329 ntb_mw_clear_trans(nt->ntb, num_mw); 1330 contigfree(mw->virt_addr, mw->xlat_size, M_NTB_IF); 1331 mw->xlat_size = 0; 1332 mw->buff_size = 0; 1333 mw->virt_addr = NULL; 1334} 1335 1336static int 1337ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, unsigned int qp_num) 1338{ 1339 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num]; 1340 struct ntb_transport_mw *mw; 1341 void *offset; 1342 ntb_q_idx_t i; 1343 size_t rx_size; 1344 unsigned num_qps_mw, mw_num, mw_count; 1345 1346 mw_count = nt->mw_count; 1347 mw_num = QP_TO_MW(nt, qp_num); 1348 mw = &nt->mw_vec[mw_num]; 1349 1350 if (mw->virt_addr == NULL) 1351 return (ENOMEM); 1352 1353 if (nt->qp_count % mw_count && mw_num + 1 < nt->qp_count / mw_count) 1354 num_qps_mw = nt->qp_count / mw_count + 1; 1355 else 1356 num_qps_mw = nt->qp_count / mw_count; 1357 1358 rx_size = mw->xlat_size / num_qps_mw; 1359 qp->rx_buff = mw->virt_addr + rx_size * qp_num / mw_count; 1360 rx_size -= sizeof(struct ntb_rx_info); 1361 1362 qp->remote_rx_info = (void*)(qp->rx_buff + rx_size); 1363 1364 /* Due to house-keeping, there must be at least 2 buffs */ 1365 qp->rx_max_frame = qmin(rx_size / 2, 1366 transport_mtu + sizeof(struct ntb_payload_header)); 1367 qp->rx_max_entry = rx_size / qp->rx_max_frame; 1368 qp->rx_index = 0; 1369 1370 qp->remote_rx_info->entry = qp->rx_max_entry - 1; 1371 1372 /* Set up the hdr offsets with 0s */ 1373 for (i = 0; i < qp->rx_max_entry; i++) { 1374 offset = (void *)(qp->rx_buff + qp->rx_max_frame * (i + 1) - 1375 sizeof(struct ntb_payload_header)); 1376 memset(offset, 0, sizeof(struct ntb_payload_header)); 1377 } 1378 1379 qp->rx_pkts = 0; 1380 qp->tx_pkts = 0; 1381 qp->tx_index = 0; 1382 1383 return (0); 1384} 1385 1386static void 1387ntb_qp_link_work(void *arg) 1388{ 1389 struct ntb_transport_qp *qp = arg; 1390 struct ntb_softc *ntb = qp->ntb; 1391 struct ntb_transport_ctx *nt = qp->transport; 1392 uint32_t val, dummy; 1393 1394 ntb_spad_read(ntb, IF_NTB_QP_LINKS, &val); 1395 1396 ntb_peer_spad_write(ntb, IF_NTB_QP_LINKS, val | (1ull << qp->qp_num)); 1397 1398 /* query remote spad for qp ready bits */ 1399 ntb_peer_spad_read(ntb, IF_NTB_QP_LINKS, &dummy); 1400 1401 /* See if the remote side is up */ 1402 if ((val & (1ull << qp->qp_num)) != 0) { 1403 if (bootverbose) 1404 if_printf(nt->ifp, "qp link up\n"); 1405 qp->link_is_up = true; 1406 1407 if (qp->event_handler != NULL) 1408 qp->event_handler(qp->cb_data, NTB_LINK_UP); 1409 1410 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work); 1411 } else if (nt->link_is_up) 1412 callout_reset(&qp->link_work, 1413 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp); 1414} 1415 1416/* Link down event*/ 1417static void 1418ntb_transport_link_cleanup(struct ntb_transport_ctx *nt) 1419{ 1420 struct ntb_transport_qp *qp; 1421 struct _qpset qp_bitmap_alloc; 1422 unsigned i; 1423 1424 BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc); 1425 BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free); 1426 1427 /* Pass along the info to any clients */ 1428 for (i = 0; i < nt->qp_count; i++) 1429 if (test_bit(i, &qp_bitmap_alloc)) { 1430 qp = &nt->qp_vec[i]; 1431 ntb_qp_link_cleanup(qp); 1432 callout_drain(&qp->link_work); 1433 } 1434 1435 if (!nt->link_is_up) 1436 callout_drain(&nt->link_work); 1437 1438 /* 1439 * The scratchpad registers keep the values if the remote side 1440 * goes down, blast them now to give them a sane value the next 1441 * time they are accessed 1442 */ 1443 for (i = 0; i < IF_NTB_MAX_SPAD; i++) 1444 ntb_spad_write(nt->ntb, i, 0); 1445} 1446 1447 1448static void 1449ntb_qp_link_down(struct ntb_transport_qp *qp) 1450{ 1451 1452 ntb_qp_link_cleanup(qp); 1453} 1454 1455static void 1456ntb_qp_link_down_reset(struct ntb_transport_qp *qp) 1457{ 1458 1459 qp->link_is_up = false; 1460 1461 qp->tx_index = qp->rx_index = 0; 1462 qp->tx_bytes = qp->rx_bytes = 0; 1463 qp->tx_pkts = qp->rx_pkts = 0; 1464 1465 qp->rx_ring_empty = 0; 1466 qp->tx_ring_full = 0; 1467 1468 qp->rx_err_no_buf = qp->tx_err_no_buf = 0; 1469 qp->rx_err_oflow = qp->rx_err_ver = 0; 1470} 1471 1472static void 1473ntb_qp_link_cleanup(struct ntb_transport_qp *qp) 1474{ 1475 struct ntb_transport_ctx *nt = qp->transport; 1476 1477 callout_drain(&qp->link_work); 1478 ntb_qp_link_down_reset(qp); 1479 1480 if (qp->event_handler != NULL) 1481 qp->event_handler(qp->cb_data, NTB_LINK_DOWN); 1482 1483 if (nt->link_is_up) 1484 callout_reset(&qp->link_work, 1485 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp); 1486} 1487 1488/* Link commanded down */ 1489/** 1490 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data 1491 * @qp: NTB transport layer queue to be disabled 1492 * 1493 * Notify NTB transport layer of client's desire to no longer receive data on 1494 * transport queue specified. It is the client's responsibility to ensure all 1495 * entries on queue are purged or otherwise handled appropriately. 1496 */ 1497static void 1498ntb_transport_link_down(struct ntb_transport_qp *qp) 1499{ 1500 uint32_t val; 1501 1502 if (qp == NULL) 1503 return; 1504 1505 qp->client_ready = false; 1506 1507 ntb_spad_read(qp->ntb, IF_NTB_QP_LINKS, &val); 1508 1509 ntb_peer_spad_write(qp->ntb, IF_NTB_QP_LINKS, 1510 val & ~(1 << qp->qp_num)); 1511 1512 if (qp->link_is_up) 1513 ntb_send_link_down(qp); 1514 else 1515 callout_drain(&qp->link_work); 1516} 1517 1518static void 1519ntb_send_link_down(struct ntb_transport_qp *qp) 1520{ 1521 struct ntb_queue_entry *entry; 1522 int i, rc; 1523 1524 if (!qp->link_is_up) 1525 return; 1526 1527 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) { 1528 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q); 1529 if (entry != NULL) 1530 break; 1531 pause("NTB Wait for link down", hz / 10); 1532 } 1533 1534 if (entry == NULL) 1535 return; 1536 1537 entry->cb_data = NULL; 1538 entry->buf = NULL; 1539 entry->len = 0; 1540 entry->flags = IF_NTB_LINK_DOWN_FLAG; 1541 1542 mtx_lock(&qp->transport->tx_lock); 1543 rc = ntb_process_tx(qp, entry); 1544 if (rc != 0) 1545 printf("ntb: Failed to send link down\n"); 1546 mtx_unlock(&qp->transport->tx_lock); 1547 1548 ntb_qp_link_down_reset(qp); 1549} 1550 1551 1552/* List Management */ 1553 1554static void 1555ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry, 1556 struct ntb_queue_list *list) 1557{ 1558 1559 mtx_lock_spin(lock); 1560 STAILQ_INSERT_TAIL(list, entry, entry); 1561 mtx_unlock_spin(lock); 1562} 1563 1564static struct ntb_queue_entry * 1565ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list) 1566{ 1567 struct ntb_queue_entry *entry; 1568 1569 mtx_lock_spin(lock); 1570 if (STAILQ_EMPTY(list)) { 1571 entry = NULL; 1572 goto out; 1573 } 1574 entry = STAILQ_FIRST(list); 1575 STAILQ_REMOVE_HEAD(list, entry); 1576out: 1577 mtx_unlock_spin(lock); 1578 1579 return (entry); 1580} 1581 1582static struct ntb_queue_entry * 1583ntb_list_mv(struct mtx *lock, struct ntb_queue_list *from, 1584 struct ntb_queue_list *to) 1585{ 1586 struct ntb_queue_entry *entry; 1587 1588 mtx_lock_spin(lock); 1589 if (STAILQ_EMPTY(from)) { 1590 entry = NULL; 1591 goto out; 1592 } 1593 entry = STAILQ_FIRST(from); 1594 STAILQ_REMOVE_HEAD(from, entry); 1595 STAILQ_INSERT_TAIL(to, entry, entry); 1596 1597out: 1598 mtx_unlock_spin(lock); 1599 return (entry); 1600} 1601 1602/* Helper functions */ 1603/* TODO: This too should really be part of the kernel */ 1604#define EUI48_MULTICAST 1 << 0 1605#define EUI48_LOCALLY_ADMINISTERED 1 << 1 1606static void 1607create_random_local_eui48(u_char *eaddr) 1608{ 1609 static uint8_t counter = 0; 1610 uint32_t seed = ticks; 1611 1612 eaddr[0] = EUI48_LOCALLY_ADMINISTERED; 1613 memcpy(&eaddr[1], &seed, sizeof(uint32_t)); 1614 eaddr[5] = counter++; 1615} 1616 1617/** 1618 * ntb_transport_max_size - Query the max payload size of a qp 1619 * @qp: NTB transport layer queue to be queried 1620 * 1621 * Query the maximum payload size permissible on the given qp 1622 * 1623 * RETURNS: the max payload size of a qp 1624 */ 1625static unsigned int 1626ntb_transport_max_size(struct ntb_transport_qp *qp) 1627{ 1628 1629 if (qp == NULL) 1630 return (0); 1631 1632 return (qp->tx_max_frame - sizeof(struct ntb_payload_header)); 1633} 1634