1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2021 Microsoft Corp. 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 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/bus.h> 34 #include <sys/kernel.h> 35 #include <sys/kthread.h> 36 #include <sys/malloc.h> 37 #include <sys/mbuf.h> 38 #include <sys/smp.h> 39 #include <sys/socket.h> 40 #include <sys/sockio.h> 41 #include <sys/time.h> 42 #include <sys/eventhandler.h> 43 44 #include <machine/bus.h> 45 #include <machine/resource.h> 46 #include <machine/in_cksum.h> 47 48 #include <net/if.h> 49 #include <net/if_var.h> 50 #include <net/if_types.h> 51 #include <net/if_vlan_var.h> 52 #ifdef RSS 53 #include <net/rss_config.h> 54 #endif 55 56 #include <netinet/in_systm.h> 57 #include <netinet/in.h> 58 #include <netinet/if_ether.h> 59 #include <netinet/ip.h> 60 #include <netinet/ip6.h> 61 #include <netinet/tcp.h> 62 #include <netinet/udp.h> 63 64 #include "mana.h" 65 #include "mana_sysctl.h" 66 67 static int mana_up(struct mana_port_context *apc); 68 static int mana_down(struct mana_port_context *apc); 69 70 extern unsigned int mana_tx_req_size; 71 extern unsigned int mana_rx_req_size; 72 extern unsigned int mana_rx_refill_threshold; 73 74 static void 75 mana_rss_key_fill(void *k, size_t size) 76 { 77 static bool rss_key_generated = false; 78 static uint8_t rss_key[MANA_HASH_KEY_SIZE]; 79 80 KASSERT(size <= MANA_HASH_KEY_SIZE, 81 ("Request more buytes than MANA RSS key can hold")); 82 83 if (!rss_key_generated) { 84 arc4random_buf(rss_key, MANA_HASH_KEY_SIZE); 85 rss_key_generated = true; 86 } 87 memcpy(k, rss_key, size); 88 } 89 90 static int 91 mana_ifmedia_change(if_t ifp __unused) 92 { 93 return EOPNOTSUPP; 94 } 95 96 static void 97 mana_ifmedia_status(if_t ifp, struct ifmediareq *ifmr) 98 { 99 struct mana_port_context *apc = if_getsoftc(ifp); 100 101 if (!apc) { 102 if_printf(ifp, "Port not available\n"); 103 return; 104 } 105 106 MANA_APC_LOCK_LOCK(apc); 107 108 ifmr->ifm_status = IFM_AVALID; 109 ifmr->ifm_active = IFM_ETHER; 110 111 if (!apc->port_is_up) { 112 MANA_APC_LOCK_UNLOCK(apc); 113 mana_dbg(NULL, "Port %u link is down\n", apc->port_idx); 114 return; 115 } 116 117 ifmr->ifm_status |= IFM_ACTIVE; 118 ifmr->ifm_active |= IFM_100G_DR | IFM_FDX; 119 120 MANA_APC_LOCK_UNLOCK(apc); 121 } 122 123 static uint64_t 124 mana_get_counter(if_t ifp, ift_counter cnt) 125 { 126 struct mana_port_context *apc = if_getsoftc(ifp); 127 struct mana_port_stats *stats = &apc->port_stats; 128 129 switch (cnt) { 130 case IFCOUNTER_IPACKETS: 131 return (counter_u64_fetch(stats->rx_packets)); 132 case IFCOUNTER_OPACKETS: 133 return (counter_u64_fetch(stats->tx_packets)); 134 case IFCOUNTER_IBYTES: 135 return (counter_u64_fetch(stats->rx_bytes)); 136 case IFCOUNTER_OBYTES: 137 return (counter_u64_fetch(stats->tx_bytes)); 138 case IFCOUNTER_IQDROPS: 139 return (counter_u64_fetch(stats->rx_drops)); 140 case IFCOUNTER_OQDROPS: 141 return (counter_u64_fetch(stats->tx_drops)); 142 default: 143 return (if_get_counter_default(ifp, cnt)); 144 } 145 } 146 147 static void 148 mana_qflush(if_t ifp) 149 { 150 if_qflush(ifp); 151 } 152 153 int 154 mana_restart(struct mana_port_context *apc) 155 { 156 int rc = 0; 157 158 MANA_APC_LOCK_LOCK(apc); 159 if (apc->port_is_up) 160 mana_down(apc); 161 162 rc = mana_up(apc); 163 MANA_APC_LOCK_UNLOCK(apc); 164 165 return (rc); 166 } 167 168 static int 169 mana_ioctl(if_t ifp, u_long command, caddr_t data) 170 { 171 struct mana_port_context *apc = if_getsoftc(ifp); 172 struct ifrsskey *ifrk; 173 struct ifrsshash *ifrh; 174 struct ifreq *ifr; 175 uint16_t new_mtu; 176 int rc = 0, mask; 177 178 switch (command) { 179 case SIOCSIFMTU: 180 ifr = (struct ifreq *)data; 181 new_mtu = ifr->ifr_mtu; 182 if (if_getmtu(ifp) == new_mtu) 183 break; 184 if ((new_mtu + 18 > MAX_FRAME_SIZE) || 185 (new_mtu + 18 < MIN_FRAME_SIZE)) { 186 if_printf(ifp, "Invalid MTU. new_mtu: %d, " 187 "max allowed: %d, min allowed: %d\n", 188 new_mtu, MAX_FRAME_SIZE - 18, MIN_FRAME_SIZE - 18); 189 return EINVAL; 190 } 191 MANA_APC_LOCK_LOCK(apc); 192 if (apc->port_is_up) 193 mana_down(apc); 194 195 apc->frame_size = new_mtu + 18; 196 if_setmtu(ifp, new_mtu); 197 mana_dbg(NULL, "Set MTU to %d\n", new_mtu); 198 199 rc = mana_up(apc); 200 MANA_APC_LOCK_UNLOCK(apc); 201 break; 202 203 case SIOCSIFFLAGS: 204 if (if_getflags(ifp) & IFF_UP) { 205 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) { 206 MANA_APC_LOCK_LOCK(apc); 207 if (!apc->port_is_up) 208 rc = mana_up(apc); 209 MANA_APC_LOCK_UNLOCK(apc); 210 } 211 } else { 212 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 213 MANA_APC_LOCK_LOCK(apc); 214 if (apc->port_is_up) 215 mana_down(apc); 216 MANA_APC_LOCK_UNLOCK(apc); 217 } 218 } 219 break; 220 221 case SIOCSIFCAP: 222 MANA_APC_LOCK_LOCK(apc); 223 ifr = (struct ifreq *)data; 224 /* 225 * Fix up requested capabilities w/ supported capabilities, 226 * since the supported capabilities could have been changed. 227 */ 228 mask = (ifr->ifr_reqcap & if_getcapabilities(ifp)) ^ 229 if_getcapenable(ifp); 230 231 if (mask & IFCAP_TXCSUM) { 232 if_togglecapenable(ifp, IFCAP_TXCSUM); 233 if_togglehwassist(ifp, (CSUM_TCP | CSUM_UDP | CSUM_IP)); 234 235 if ((IFCAP_TSO4 & if_getcapenable(ifp)) && 236 !(IFCAP_TXCSUM & if_getcapenable(ifp))) { 237 mask &= ~IFCAP_TSO4; 238 if_setcapenablebit(ifp, 0, IFCAP_TSO4); 239 if_sethwassistbits(ifp, 0, CSUM_IP_TSO); 240 mana_warn(NULL, 241 "Also disabled tso4 due to -txcsum.\n"); 242 } 243 } 244 245 if (mask & IFCAP_TXCSUM_IPV6) { 246 if_togglecapenable(ifp, IFCAP_TXCSUM_IPV6); 247 if_togglehwassist(ifp, (CSUM_UDP_IPV6 | CSUM_TCP_IPV6)); 248 249 if ((IFCAP_TSO6 & if_getcapenable(ifp)) && 250 !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) { 251 mask &= ~IFCAP_TSO6; 252 if_setcapenablebit(ifp, 0, IFCAP_TSO6); 253 if_sethwassistbits(ifp, 0, CSUM_IP6_TSO); 254 mana_warn(ifp, 255 "Also disabled tso6 due to -txcsum6.\n"); 256 } 257 } 258 259 if (mask & IFCAP_RXCSUM) 260 if_togglecapenable(ifp, IFCAP_RXCSUM); 261 /* We can't diff IPv6 packets from IPv4 packets on RX path. */ 262 if (mask & IFCAP_RXCSUM_IPV6) 263 if_togglecapenable(ifp, IFCAP_RXCSUM_IPV6); 264 265 if (mask & IFCAP_LRO) 266 if_togglecapenable(ifp, IFCAP_LRO); 267 268 if (mask & IFCAP_TSO4) { 269 if (!(IFCAP_TSO4 & if_getcapenable(ifp)) && 270 !(IFCAP_TXCSUM & if_getcapenable(ifp))) { 271 MANA_APC_LOCK_UNLOCK(apc); 272 if_printf(ifp, "Enable txcsum first.\n"); 273 rc = EAGAIN; 274 goto out; 275 } 276 if_togglecapenable(ifp, IFCAP_TSO4); 277 if_togglehwassist(ifp, CSUM_IP_TSO); 278 } 279 280 if (mask & IFCAP_TSO6) { 281 if (!(IFCAP_TSO6 & if_getcapenable(ifp)) && 282 !(IFCAP_TXCSUM_IPV6 & if_getcapenable(ifp))) { 283 MANA_APC_LOCK_UNLOCK(apc); 284 if_printf(ifp, "Enable txcsum6 first.\n"); 285 rc = EAGAIN; 286 goto out; 287 } 288 if_togglecapenable(ifp, IFCAP_TSO6); 289 if_togglehwassist(ifp, CSUM_IP6_TSO); 290 } 291 292 MANA_APC_LOCK_UNLOCK(apc); 293 out: 294 break; 295 296 case SIOCSIFMEDIA: 297 case SIOCGIFMEDIA: 298 case SIOCGIFXMEDIA: 299 ifr = (struct ifreq *)data; 300 rc = ifmedia_ioctl(ifp, ifr, &apc->media, command); 301 break; 302 303 case SIOCGIFRSSKEY: 304 ifrk = (struct ifrsskey *)data; 305 ifrk->ifrk_func = RSS_FUNC_TOEPLITZ; 306 ifrk->ifrk_keylen = MANA_HASH_KEY_SIZE; 307 memcpy(ifrk->ifrk_key, apc->hashkey, MANA_HASH_KEY_SIZE); 308 break; 309 310 case SIOCGIFRSSHASH: 311 ifrh = (struct ifrsshash *)data; 312 ifrh->ifrh_func = RSS_FUNC_TOEPLITZ; 313 ifrh->ifrh_types = 314 RSS_TYPE_TCP_IPV4 | 315 RSS_TYPE_UDP_IPV4 | 316 RSS_TYPE_TCP_IPV6 | 317 RSS_TYPE_UDP_IPV6; 318 break; 319 320 default: 321 rc = ether_ioctl(ifp, command, data); 322 break; 323 } 324 325 return (rc); 326 } 327 328 static inline void 329 mana_alloc_counters(counter_u64_t *begin, int size) 330 { 331 counter_u64_t *end = (counter_u64_t *)((char *)begin + size); 332 333 for (; begin < end; ++begin) 334 *begin = counter_u64_alloc(M_WAITOK); 335 } 336 337 static inline void 338 mana_free_counters(counter_u64_t *begin, int size) 339 { 340 counter_u64_t *end = (counter_u64_t *)((char *)begin + size); 341 342 for (; begin < end; ++begin) 343 counter_u64_free(*begin); 344 } 345 346 static bool 347 mana_can_tx(struct gdma_queue *wq) 348 { 349 return mana_gd_wq_avail_space(wq) >= MAX_TX_WQE_SIZE; 350 } 351 352 static inline int 353 mana_tx_map_mbuf(struct mana_port_context *apc, 354 struct mana_send_buf_info *tx_info, 355 struct mbuf **m_head, struct mana_tx_package *tp, 356 struct mana_stats *tx_stats) 357 { 358 struct gdma_dev *gd = apc->ac->gdma_dev; 359 bus_dma_segment_t segs[MAX_MBUF_FRAGS]; 360 struct mbuf *m = *m_head; 361 int err, nsegs, i; 362 363 err = bus_dmamap_load_mbuf_sg(apc->tx_buf_tag, tx_info->dma_map, 364 m, segs, &nsegs, BUS_DMA_NOWAIT); 365 if (err == EFBIG) { 366 struct mbuf *m_new; 367 368 counter_u64_add(tx_stats->collapse, 1); 369 m_new = m_collapse(m, M_NOWAIT, MAX_MBUF_FRAGS); 370 if (unlikely(m_new == NULL)) { 371 counter_u64_add(tx_stats->collapse_err, 1); 372 return ENOBUFS; 373 } else { 374 *m_head = m = m_new; 375 } 376 377 mana_warn(NULL, 378 "Too many segs in orig mbuf, m_collapse called\n"); 379 380 err = bus_dmamap_load_mbuf_sg(apc->tx_buf_tag, 381 tx_info->dma_map, m, segs, &nsegs, BUS_DMA_NOWAIT); 382 } 383 if (!err) { 384 for (i = 0; i < nsegs; i++) { 385 tp->wqe_req.sgl[i].address = segs[i].ds_addr; 386 tp->wqe_req.sgl[i].mem_key = gd->gpa_mkey; 387 tp->wqe_req.sgl[i].size = segs[i].ds_len; 388 } 389 tp->wqe_req.num_sge = nsegs; 390 391 tx_info->mbuf = *m_head; 392 393 bus_dmamap_sync(apc->tx_buf_tag, tx_info->dma_map, 394 BUS_DMASYNC_PREWRITE); 395 } 396 397 return err; 398 } 399 400 static inline void 401 mana_tx_unmap_mbuf(struct mana_port_context *apc, 402 struct mana_send_buf_info *tx_info) 403 { 404 bus_dmamap_sync(apc->tx_buf_tag, tx_info->dma_map, 405 BUS_DMASYNC_POSTWRITE); 406 bus_dmamap_unload(apc->tx_buf_tag, tx_info->dma_map); 407 if (tx_info->mbuf) { 408 m_freem(tx_info->mbuf); 409 tx_info->mbuf = NULL; 410 } 411 } 412 413 static inline int 414 mana_load_rx_mbuf(struct mana_port_context *apc, struct mana_rxq *rxq, 415 struct mana_recv_buf_oob *rx_oob, bool alloc_mbuf) 416 { 417 bus_dma_segment_t segs[1]; 418 struct mbuf *mbuf; 419 int nsegs, err; 420 uint32_t mlen; 421 422 if (alloc_mbuf) { 423 mbuf = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, rxq->datasize); 424 if (unlikely(mbuf == NULL)) { 425 mbuf = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 426 if (unlikely(mbuf == NULL)) { 427 return ENOMEM; 428 } 429 mlen = MCLBYTES; 430 } else { 431 mlen = rxq->datasize; 432 } 433 434 mbuf->m_pkthdr.len = mbuf->m_len = mlen; 435 } else { 436 if (rx_oob->mbuf) { 437 mbuf = rx_oob->mbuf; 438 mlen = rx_oob->mbuf->m_pkthdr.len; 439 } else { 440 return ENOMEM; 441 } 442 } 443 444 err = bus_dmamap_load_mbuf_sg(apc->rx_buf_tag, rx_oob->dma_map, 445 mbuf, segs, &nsegs, BUS_DMA_NOWAIT); 446 447 if (unlikely((err != 0) || (nsegs != 1))) { 448 mana_warn(NULL, "Failed to map mbuf, error: %d, " 449 "nsegs: %d\n", err, nsegs); 450 counter_u64_add(rxq->stats.dma_mapping_err, 1); 451 goto error; 452 } 453 454 bus_dmamap_sync(apc->rx_buf_tag, rx_oob->dma_map, 455 BUS_DMASYNC_PREREAD); 456 457 rx_oob->mbuf = mbuf; 458 rx_oob->num_sge = 1; 459 rx_oob->sgl[0].address = segs[0].ds_addr; 460 rx_oob->sgl[0].size = mlen; 461 rx_oob->sgl[0].mem_key = apc->ac->gdma_dev->gpa_mkey; 462 463 return 0; 464 465 error: 466 m_freem(mbuf); 467 return EFAULT; 468 } 469 470 static inline void 471 mana_unload_rx_mbuf(struct mana_port_context *apc, struct mana_rxq *rxq, 472 struct mana_recv_buf_oob *rx_oob, bool free_mbuf) 473 { 474 bus_dmamap_sync(apc->rx_buf_tag, rx_oob->dma_map, 475 BUS_DMASYNC_POSTREAD); 476 bus_dmamap_unload(apc->rx_buf_tag, rx_oob->dma_map); 477 478 if (free_mbuf && rx_oob->mbuf) { 479 m_freem(rx_oob->mbuf); 480 rx_oob->mbuf = NULL; 481 } 482 } 483 484 485 /* Use couple mbuf PH_loc spaces for l3 and l4 protocal type */ 486 #define MANA_L3_PROTO(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[0]) 487 #define MANA_L4_PROTO(_mbuf) ((_mbuf)->m_pkthdr.PH_loc.sixteen[1]) 488 489 #define MANA_TXQ_FULL (IFF_DRV_RUNNING | IFF_DRV_OACTIVE) 490 491 static void 492 mana_xmit(struct mana_txq *txq) 493 { 494 enum mana_tx_pkt_format pkt_fmt = MANA_SHORT_PKT_FMT; 495 struct mana_send_buf_info *tx_info; 496 if_t ndev = txq->ndev; 497 struct mbuf *mbuf; 498 struct mana_port_context *apc = if_getsoftc(ndev); 499 unsigned int tx_queue_size = apc->tx_queue_size; 500 struct mana_port_stats *port_stats = &apc->port_stats; 501 struct gdma_dev *gd = apc->ac->gdma_dev; 502 uint64_t packets, bytes; 503 uint16_t next_to_use; 504 struct mana_tx_package pkg = {}; 505 struct mana_stats *tx_stats; 506 struct gdma_queue *gdma_sq; 507 struct mana_cq *cq; 508 int err, len; 509 bool is_tso; 510 511 gdma_sq = txq->gdma_sq; 512 cq = &apc->tx_qp[txq->idx].tx_cq; 513 tx_stats = &txq->stats; 514 515 packets = 0; 516 bytes = 0; 517 next_to_use = txq->next_to_use; 518 519 while ((mbuf = drbr_peek(ndev, txq->txq_br)) != NULL) { 520 if (!apc->port_is_up || 521 (if_getdrvflags(ndev) & MANA_TXQ_FULL) != IFF_DRV_RUNNING) { 522 drbr_putback(ndev, txq->txq_br, mbuf); 523 break; 524 } 525 526 if (!mana_can_tx(gdma_sq)) { 527 /* SQ is full. Set the IFF_DRV_OACTIVE flag */ 528 if_setdrvflagbits(apc->ndev, IFF_DRV_OACTIVE, 0); 529 counter_u64_add(tx_stats->stop, 1); 530 uint64_t stops = counter_u64_fetch(tx_stats->stop); 531 uint64_t wakeups = counter_u64_fetch(tx_stats->wakeup); 532 #define MANA_TXQ_STOP_THRESHOLD 50 533 if (stops > MANA_TXQ_STOP_THRESHOLD && wakeups > 0 && 534 stops > wakeups && txq->alt_txq_idx == txq->idx) { 535 txq->alt_txq_idx = 536 (txq->idx + (stops / wakeups)) 537 % apc->num_queues; 538 counter_u64_add(tx_stats->alt_chg, 1); 539 } 540 541 drbr_putback(ndev, txq->txq_br, mbuf); 542 543 taskqueue_enqueue(cq->cleanup_tq, &cq->cleanup_task); 544 break; 545 } 546 547 tx_info = &txq->tx_buf_info[next_to_use]; 548 549 memset(&pkg, 0, sizeof(struct mana_tx_package)); 550 pkg.wqe_req.sgl = pkg.sgl_array; 551 552 err = mana_tx_map_mbuf(apc, tx_info, &mbuf, &pkg, tx_stats); 553 if (unlikely(err)) { 554 mana_dbg(NULL, 555 "Failed to map tx mbuf, err %d\n", err); 556 557 counter_u64_add(tx_stats->dma_mapping_err, 1); 558 559 /* The mbuf is still there. Free it */ 560 m_freem(mbuf); 561 /* Advance the drbr queue */ 562 drbr_advance(ndev, txq->txq_br); 563 continue; 564 } 565 566 pkg.tx_oob.s_oob.vcq_num = cq->gdma_id; 567 pkg.tx_oob.s_oob.vsq_frame = txq->vsq_frame; 568 569 if (txq->vp_offset > MANA_SHORT_VPORT_OFFSET_MAX) { 570 pkg.tx_oob.l_oob.long_vp_offset = txq->vp_offset; 571 pkt_fmt = MANA_LONG_PKT_FMT; 572 } else { 573 pkg.tx_oob.s_oob.short_vp_offset = txq->vp_offset; 574 } 575 576 pkg.tx_oob.s_oob.pkt_fmt = pkt_fmt; 577 578 if (pkt_fmt == MANA_SHORT_PKT_FMT) 579 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_short_oob); 580 else 581 pkg.wqe_req.inline_oob_size = sizeof(struct mana_tx_oob); 582 583 pkg.wqe_req.inline_oob_data = &pkg.tx_oob; 584 pkg.wqe_req.flags = 0; 585 pkg.wqe_req.client_data_unit = 0; 586 587 is_tso = false; 588 if (mbuf->m_pkthdr.csum_flags & CSUM_TSO) { 589 is_tso = true; 590 591 if (MANA_L3_PROTO(mbuf) == ETHERTYPE_IP) 592 pkg.tx_oob.s_oob.is_outer_ipv4 = 1; 593 else 594 pkg.tx_oob.s_oob.is_outer_ipv6 = 1; 595 596 pkg.tx_oob.s_oob.comp_iphdr_csum = 1; 597 pkg.tx_oob.s_oob.comp_tcp_csum = 1; 598 pkg.tx_oob.s_oob.trans_off = mbuf->m_pkthdr.l3hlen; 599 600 pkg.wqe_req.client_data_unit = mbuf->m_pkthdr.tso_segsz; 601 pkg.wqe_req.flags = GDMA_WR_OOB_IN_SGL | GDMA_WR_PAD_BY_SGE0; 602 } else if (mbuf->m_pkthdr.csum_flags & 603 (CSUM_IP_UDP | CSUM_IP_TCP | CSUM_IP6_UDP | CSUM_IP6_TCP)) { 604 if (MANA_L3_PROTO(mbuf) == ETHERTYPE_IP) { 605 pkg.tx_oob.s_oob.is_outer_ipv4 = 1; 606 pkg.tx_oob.s_oob.comp_iphdr_csum = 1; 607 } else { 608 pkg.tx_oob.s_oob.is_outer_ipv6 = 1; 609 } 610 611 if (MANA_L4_PROTO(mbuf) == IPPROTO_TCP) { 612 pkg.tx_oob.s_oob.comp_tcp_csum = 1; 613 pkg.tx_oob.s_oob.trans_off = 614 mbuf->m_pkthdr.l3hlen; 615 } else { 616 pkg.tx_oob.s_oob.comp_udp_csum = 1; 617 } 618 } else if (mbuf->m_pkthdr.csum_flags & CSUM_IP) { 619 pkg.tx_oob.s_oob.is_outer_ipv4 = 1; 620 pkg.tx_oob.s_oob.comp_iphdr_csum = 1; 621 } else { 622 if (MANA_L3_PROTO(mbuf) == ETHERTYPE_IP) 623 pkg.tx_oob.s_oob.is_outer_ipv4 = 1; 624 else if (MANA_L3_PROTO(mbuf) == ETHERTYPE_IPV6) 625 pkg.tx_oob.s_oob.is_outer_ipv6 = 1; 626 } 627 628 len = mbuf->m_pkthdr.len; 629 630 err = mana_gd_post_work_request(gdma_sq, &pkg.wqe_req, 631 (struct gdma_posted_wqe_info *)&tx_info->wqe_inf); 632 if (unlikely(err)) { 633 /* Should not happen */ 634 if_printf(ndev, "Failed to post TX OOB: %d\n", err); 635 636 mana_tx_unmap_mbuf(apc, tx_info); 637 638 drbr_advance(ndev, txq->txq_br); 639 continue; 640 } 641 642 next_to_use = MANA_IDX_NEXT(next_to_use, tx_queue_size); 643 644 (void)atomic_inc_return(&txq->pending_sends); 645 646 drbr_advance(ndev, txq->txq_br); 647 648 mana_gd_wq_ring_doorbell(gd->gdma_context, gdma_sq); 649 650 packets++; 651 bytes += len; 652 653 if (is_tso) { 654 txq->tso_pkts++; 655 txq->tso_bytes += len; 656 } 657 } 658 659 counter_enter(); 660 counter_u64_add_protected(tx_stats->packets, packets); 661 counter_u64_add_protected(port_stats->tx_packets, packets); 662 counter_u64_add_protected(tx_stats->bytes, bytes); 663 counter_u64_add_protected(port_stats->tx_bytes, bytes); 664 counter_exit(); 665 666 txq->next_to_use = next_to_use; 667 } 668 669 static void 670 mana_xmit_taskfunc(void *arg, int pending) 671 { 672 struct mana_txq *txq = (struct mana_txq *)arg; 673 if_t ndev = txq->ndev; 674 struct mana_port_context *apc = if_getsoftc(ndev); 675 676 while (!drbr_empty(ndev, txq->txq_br) && apc->port_is_up && 677 (if_getdrvflags(ndev) & MANA_TXQ_FULL) == IFF_DRV_RUNNING) { 678 mtx_lock(&txq->txq_mtx); 679 mana_xmit(txq); 680 mtx_unlock(&txq->txq_mtx); 681 } 682 } 683 684 #define PULLUP_HDR(m, len) \ 685 do { \ 686 if (unlikely((m)->m_len < (len))) { \ 687 (m) = m_pullup((m), (len)); \ 688 if ((m) == NULL) \ 689 return (NULL); \ 690 } \ 691 } while (0) 692 693 /* 694 * If this function failed, the mbuf would be freed. 695 */ 696 static inline struct mbuf * 697 mana_tso_fixup(struct mbuf *mbuf) 698 { 699 struct ether_vlan_header *eh = mtod(mbuf, struct ether_vlan_header *); 700 struct tcphdr *th; 701 uint16_t etype; 702 int ehlen; 703 704 if (eh->evl_encap_proto == ntohs(ETHERTYPE_VLAN)) { 705 etype = ntohs(eh->evl_proto); 706 ehlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 707 } else { 708 etype = ntohs(eh->evl_encap_proto); 709 ehlen = ETHER_HDR_LEN; 710 } 711 712 if (etype == ETHERTYPE_IP) { 713 struct ip *ip; 714 int iphlen; 715 716 PULLUP_HDR(mbuf, ehlen + sizeof(*ip)); 717 ip = mtodo(mbuf, ehlen); 718 iphlen = ip->ip_hl << 2; 719 mbuf->m_pkthdr.l3hlen = ehlen + iphlen; 720 721 PULLUP_HDR(mbuf, ehlen + iphlen + sizeof(*th)); 722 th = mtodo(mbuf, ehlen + iphlen); 723 724 ip->ip_len = 0; 725 ip->ip_sum = 0; 726 th->th_sum = in_pseudo(ip->ip_src.s_addr, 727 ip->ip_dst.s_addr, htons(IPPROTO_TCP)); 728 } else if (etype == ETHERTYPE_IPV6) { 729 struct ip6_hdr *ip6; 730 731 PULLUP_HDR(mbuf, ehlen + sizeof(*ip6) + sizeof(*th)); 732 ip6 = mtodo(mbuf, ehlen); 733 if (ip6->ip6_nxt != IPPROTO_TCP) { 734 /* Realy something wrong, just return */ 735 mana_dbg(NULL, "TSO mbuf not TCP, freed.\n"); 736 m_freem(mbuf); 737 return NULL; 738 } 739 mbuf->m_pkthdr.l3hlen = ehlen + sizeof(*ip6); 740 741 th = mtodo(mbuf, ehlen + sizeof(*ip6)); 742 743 ip6->ip6_plen = 0; 744 th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0); 745 } else { 746 /* CSUM_TSO is set but not IP protocol. */ 747 mana_warn(NULL, "TSO mbuf not right, freed.\n"); 748 m_freem(mbuf); 749 return NULL; 750 } 751 752 MANA_L3_PROTO(mbuf) = etype; 753 754 return (mbuf); 755 } 756 757 /* 758 * If this function failed, the mbuf would be freed. 759 */ 760 static inline struct mbuf * 761 mana_mbuf_csum_check(struct mbuf *mbuf) 762 { 763 struct ether_vlan_header *eh = mtod(mbuf, struct ether_vlan_header *); 764 struct mbuf *mbuf_next; 765 uint16_t etype; 766 int offset; 767 int ehlen; 768 769 if (eh->evl_encap_proto == ntohs(ETHERTYPE_VLAN)) { 770 etype = ntohs(eh->evl_proto); 771 ehlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN; 772 } else { 773 etype = ntohs(eh->evl_encap_proto); 774 ehlen = ETHER_HDR_LEN; 775 } 776 777 mbuf_next = m_getptr(mbuf, ehlen, &offset); 778 779 MANA_L4_PROTO(mbuf) = 0; 780 if (etype == ETHERTYPE_IP) { 781 const struct ip *ip; 782 int iphlen; 783 784 ip = (struct ip *)(mtodo(mbuf_next, offset)); 785 iphlen = ip->ip_hl << 2; 786 mbuf->m_pkthdr.l3hlen = ehlen + iphlen; 787 788 MANA_L4_PROTO(mbuf) = ip->ip_p; 789 } else if (etype == ETHERTYPE_IPV6) { 790 const struct ip6_hdr *ip6; 791 792 ip6 = (struct ip6_hdr *)(mtodo(mbuf_next, offset)); 793 mbuf->m_pkthdr.l3hlen = ehlen + sizeof(*ip6); 794 795 MANA_L4_PROTO(mbuf) = ip6->ip6_nxt; 796 } else { 797 MANA_L4_PROTO(mbuf) = 0; 798 } 799 800 MANA_L3_PROTO(mbuf) = etype; 801 802 return (mbuf); 803 } 804 805 static int 806 mana_start_xmit(if_t ifp, struct mbuf *m) 807 { 808 struct mana_port_context *apc = if_getsoftc(ifp); 809 struct mana_txq *txq; 810 int is_drbr_empty; 811 uint16_t txq_id; 812 int err; 813 814 if (unlikely((!apc->port_is_up) || 815 (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)) 816 return ENODEV; 817 818 if (m->m_pkthdr.csum_flags & CSUM_TSO) { 819 m = mana_tso_fixup(m); 820 if (unlikely(m == NULL)) { 821 counter_enter(); 822 counter_u64_add_protected(apc->port_stats.tx_drops, 1); 823 counter_exit(); 824 return EIO; 825 } 826 } else { 827 m = mana_mbuf_csum_check(m); 828 if (unlikely(m == NULL)) { 829 counter_enter(); 830 counter_u64_add_protected(apc->port_stats.tx_drops, 1); 831 counter_exit(); 832 return EIO; 833 } 834 } 835 836 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) { 837 uint32_t hash = m->m_pkthdr.flowid; 838 txq_id = apc->indir_table[(hash) & MANA_INDIRECT_TABLE_MASK] % 839 apc->num_queues; 840 } else { 841 txq_id = m->m_pkthdr.flowid % apc->num_queues; 842 } 843 844 if (apc->enable_tx_altq) 845 txq_id = apc->tx_qp[txq_id].txq.alt_txq_idx; 846 847 txq = &apc->tx_qp[txq_id].txq; 848 849 is_drbr_empty = drbr_empty(ifp, txq->txq_br); 850 err = drbr_enqueue(ifp, txq->txq_br, m); 851 if (unlikely(err)) { 852 mana_warn(NULL, "txq %u failed to enqueue: %d\n", 853 txq_id, err); 854 taskqueue_enqueue(txq->enqueue_tq, &txq->enqueue_task); 855 return err; 856 } 857 858 if (is_drbr_empty && mtx_trylock(&txq->txq_mtx)) { 859 mana_xmit(txq); 860 mtx_unlock(&txq->txq_mtx); 861 } else { 862 taskqueue_enqueue(txq->enqueue_tq, &txq->enqueue_task); 863 } 864 865 return 0; 866 } 867 868 static void 869 mana_cleanup_port_context(struct mana_port_context *apc) 870 { 871 bus_dma_tag_destroy(apc->tx_buf_tag); 872 bus_dma_tag_destroy(apc->rx_buf_tag); 873 apc->rx_buf_tag = NULL; 874 875 free(apc->rxqs, M_DEVBUF); 876 apc->rxqs = NULL; 877 878 mana_free_counters((counter_u64_t *)&apc->port_stats, 879 sizeof(struct mana_port_stats)); 880 } 881 882 static int 883 mana_init_port_context(struct mana_port_context *apc) 884 { 885 device_t dev = apc->ac->gdma_dev->gdma_context->dev; 886 uint32_t tso_maxsize; 887 int err; 888 889 tso_maxsize = MANA_TSO_MAX_SZ; 890 891 /* Create DMA tag for tx bufs */ 892 err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 893 1, 0, /* alignment, boundary */ 894 BUS_SPACE_MAXADDR, /* lowaddr */ 895 BUS_SPACE_MAXADDR, /* highaddr */ 896 NULL, NULL, /* filter, filterarg */ 897 tso_maxsize, /* maxsize */ 898 MAX_MBUF_FRAGS, /* nsegments */ 899 tso_maxsize, /* maxsegsize */ 900 0, /* flags */ 901 NULL, NULL, /* lockfunc, lockfuncarg*/ 902 &apc->tx_buf_tag); 903 if (unlikely(err)) { 904 device_printf(dev, "Feiled to create TX DMA tag\n"); 905 return err; 906 } 907 908 /* Create DMA tag for rx bufs */ 909 err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */ 910 64, 0, /* alignment, boundary */ 911 BUS_SPACE_MAXADDR, /* lowaddr */ 912 BUS_SPACE_MAXADDR, /* highaddr */ 913 NULL, NULL, /* filter, filterarg */ 914 MJUMPAGESIZE, /* maxsize */ 915 1, /* nsegments */ 916 MJUMPAGESIZE, /* maxsegsize */ 917 0, /* flags */ 918 NULL, NULL, /* lockfunc, lockfuncarg*/ 919 &apc->rx_buf_tag); 920 if (unlikely(err)) { 921 device_printf(dev, "Feiled to create RX DMA tag\n"); 922 return err; 923 } 924 925 apc->rxqs = mallocarray(apc->num_queues, sizeof(struct mana_rxq *), 926 M_DEVBUF, M_WAITOK | M_ZERO); 927 928 return 0; 929 } 930 931 static int 932 mana_send_request(struct mana_context *ac, void *in_buf, 933 uint32_t in_len, void *out_buf, uint32_t out_len) 934 { 935 struct gdma_context *gc = ac->gdma_dev->gdma_context; 936 struct gdma_resp_hdr *resp = out_buf; 937 struct gdma_req_hdr *req = in_buf; 938 device_t dev = gc->dev; 939 static atomic_t activity_id; 940 int err; 941 942 req->dev_id = gc->mana.dev_id; 943 req->activity_id = atomic_inc_return(&activity_id); 944 945 mana_dbg(NULL, "activity_id = %u\n", activity_id); 946 947 err = mana_gd_send_request(gc, in_len, in_buf, out_len, 948 out_buf); 949 if (err || resp->status) { 950 device_printf(dev, "Failed to send mana message: %d, 0x%x\n", 951 err, resp->status); 952 return err ? err : EPROTO; 953 } 954 955 if (req->dev_id.as_uint32 != resp->dev_id.as_uint32 || 956 req->activity_id != resp->activity_id) { 957 device_printf(dev, 958 "Unexpected mana message response: %x,%x,%x,%x\n", 959 req->dev_id.as_uint32, resp->dev_id.as_uint32, 960 req->activity_id, resp->activity_id); 961 return EPROTO; 962 } 963 964 return 0; 965 } 966 967 static int 968 mana_verify_resp_hdr(const struct gdma_resp_hdr *resp_hdr, 969 const enum mana_command_code expected_code, 970 const uint32_t min_size) 971 { 972 if (resp_hdr->response.msg_type != expected_code) 973 return EPROTO; 974 975 if (resp_hdr->response.msg_version < GDMA_MESSAGE_V1) 976 return EPROTO; 977 978 if (resp_hdr->response.msg_size < min_size) 979 return EPROTO; 980 981 return 0; 982 } 983 984 static int 985 mana_query_device_cfg(struct mana_context *ac, uint32_t proto_major_ver, 986 uint32_t proto_minor_ver, uint32_t proto_micro_ver, 987 uint16_t *max_num_vports) 988 { 989 struct gdma_context *gc = ac->gdma_dev->gdma_context; 990 struct mana_query_device_cfg_resp resp = {}; 991 struct mana_query_device_cfg_req req = {}; 992 device_t dev = gc->dev; 993 int err = 0; 994 995 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_DEV_CONFIG, 996 sizeof(req), sizeof(resp)); 997 req.proto_major_ver = proto_major_ver; 998 req.proto_minor_ver = proto_minor_ver; 999 req.proto_micro_ver = proto_micro_ver; 1000 1001 err = mana_send_request(ac, &req, sizeof(req), &resp, sizeof(resp)); 1002 if (err) { 1003 device_printf(dev, "Failed to query config: %d", err); 1004 return err; 1005 } 1006 1007 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_DEV_CONFIG, 1008 sizeof(resp)); 1009 if (err || resp.hdr.status) { 1010 device_printf(dev, "Invalid query result: %d, 0x%x\n", err, 1011 resp.hdr.status); 1012 if (!err) 1013 err = EPROTO; 1014 return err; 1015 } 1016 1017 *max_num_vports = resp.max_num_vports; 1018 1019 mana_dbg(NULL, "mana max_num_vports from device = %d\n", 1020 *max_num_vports); 1021 1022 return 0; 1023 } 1024 1025 static int 1026 mana_query_vport_cfg(struct mana_port_context *apc, uint32_t vport_index, 1027 uint32_t *max_sq, uint32_t *max_rq, uint32_t *num_indir_entry) 1028 { 1029 struct mana_query_vport_cfg_resp resp = {}; 1030 struct mana_query_vport_cfg_req req = {}; 1031 int err; 1032 1033 mana_gd_init_req_hdr(&req.hdr, MANA_QUERY_VPORT_CONFIG, 1034 sizeof(req), sizeof(resp)); 1035 1036 req.vport_index = vport_index; 1037 1038 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1039 sizeof(resp)); 1040 if (err) 1041 return err; 1042 1043 err = mana_verify_resp_hdr(&resp.hdr, MANA_QUERY_VPORT_CONFIG, 1044 sizeof(resp)); 1045 if (err) 1046 return err; 1047 1048 if (resp.hdr.status) 1049 return EPROTO; 1050 1051 *max_sq = resp.max_num_sq; 1052 *max_rq = resp.max_num_rq; 1053 *num_indir_entry = resp.num_indirection_ent; 1054 1055 apc->port_handle = resp.vport; 1056 memcpy(apc->mac_addr, resp.mac_addr, ETHER_ADDR_LEN); 1057 1058 return 0; 1059 } 1060 1061 void 1062 mana_uncfg_vport(struct mana_port_context *apc) 1063 { 1064 apc->vport_use_count--; 1065 if (apc->vport_use_count < 0) { 1066 mana_err(NULL, 1067 "WARNING: vport_use_count less than 0: %u\n", 1068 apc->vport_use_count); 1069 } 1070 } 1071 1072 int 1073 mana_cfg_vport(struct mana_port_context *apc, uint32_t protection_dom_id, 1074 uint32_t doorbell_pg_id) 1075 { 1076 struct mana_config_vport_resp resp = {}; 1077 struct mana_config_vport_req req = {}; 1078 int err; 1079 1080 /* This function is used to program the Ethernet port in the hardware 1081 * table. It can be called from the Ethernet driver or the RDMA driver. 1082 * 1083 * For Ethernet usage, the hardware supports only one active user on a 1084 * physical port. The driver checks on the port usage before programming 1085 * the hardware when creating the RAW QP (RDMA driver) or exposing the 1086 * device to kernel NET layer (Ethernet driver). 1087 * 1088 * Because the RDMA driver doesn't know in advance which QP type the 1089 * user will create, it exposes the device with all its ports. The user 1090 * may not be able to create RAW QP on a port if this port is already 1091 * in used by the Ethernet driver from the kernel. 1092 * 1093 * This physical port limitation only applies to the RAW QP. For RC QP, 1094 * the hardware doesn't have this limitation. The user can create RC 1095 * QPs on a physical port up to the hardware limits independent of the 1096 * Ethernet usage on the same port. 1097 */ 1098 if (apc->vport_use_count > 0) { 1099 return EBUSY; 1100 } 1101 apc->vport_use_count++; 1102 1103 mana_gd_init_req_hdr(&req.hdr, MANA_CONFIG_VPORT_TX, 1104 sizeof(req), sizeof(resp)); 1105 req.vport = apc->port_handle; 1106 req.pdid = protection_dom_id; 1107 req.doorbell_pageid = doorbell_pg_id; 1108 1109 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1110 sizeof(resp)); 1111 if (err) { 1112 if_printf(apc->ndev, "Failed to configure vPort: %d\n", err); 1113 goto out; 1114 } 1115 1116 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_TX, 1117 sizeof(resp)); 1118 if (err || resp.hdr.status) { 1119 if_printf(apc->ndev, "Failed to configure vPort: %d, 0x%x\n", 1120 err, resp.hdr.status); 1121 if (!err) 1122 err = EPROTO; 1123 1124 goto out; 1125 } 1126 1127 apc->tx_shortform_allowed = resp.short_form_allowed; 1128 apc->tx_vp_offset = resp.tx_vport_offset; 1129 1130 if_printf(apc->ndev, "Configured vPort %ju PD %u DB %u\n", 1131 apc->port_handle, protection_dom_id, doorbell_pg_id); 1132 1133 out: 1134 if (err) 1135 mana_uncfg_vport(apc); 1136 1137 return err; 1138 } 1139 1140 static int 1141 mana_cfg_vport_steering(struct mana_port_context *apc, 1142 enum TRI_STATE rx, 1143 bool update_default_rxobj, bool update_key, 1144 bool update_tab) 1145 { 1146 uint16_t num_entries = MANA_INDIRECT_TABLE_SIZE; 1147 struct mana_cfg_rx_steer_req *req = NULL; 1148 struct mana_cfg_rx_steer_resp resp = {}; 1149 if_t ndev = apc->ndev; 1150 mana_handle_t *req_indir_tab; 1151 uint32_t req_buf_size; 1152 int err; 1153 1154 req_buf_size = sizeof(*req) + sizeof(mana_handle_t) * num_entries; 1155 req = malloc(req_buf_size, M_DEVBUF, M_WAITOK | M_ZERO); 1156 1157 mana_gd_init_req_hdr(&req->hdr, MANA_CONFIG_VPORT_RX, req_buf_size, 1158 sizeof(resp)); 1159 1160 req->vport = apc->port_handle; 1161 req->num_indir_entries = num_entries; 1162 req->indir_tab_offset = sizeof(*req); 1163 req->rx_enable = rx; 1164 req->rss_enable = apc->rss_state; 1165 req->update_default_rxobj = update_default_rxobj; 1166 req->update_hashkey = update_key; 1167 req->update_indir_tab = update_tab; 1168 req->default_rxobj = apc->default_rxobj; 1169 1170 if (update_key) 1171 memcpy(&req->hashkey, apc->hashkey, MANA_HASH_KEY_SIZE); 1172 1173 if (update_tab) { 1174 req_indir_tab = (mana_handle_t *)(req + 1); 1175 memcpy(req_indir_tab, apc->rxobj_table, 1176 req->num_indir_entries * sizeof(mana_handle_t)); 1177 } 1178 1179 err = mana_send_request(apc->ac, req, req_buf_size, &resp, 1180 sizeof(resp)); 1181 if (err) { 1182 if_printf(ndev, "Failed to configure vPort RX: %d\n", err); 1183 goto out; 1184 } 1185 1186 err = mana_verify_resp_hdr(&resp.hdr, MANA_CONFIG_VPORT_RX, 1187 sizeof(resp)); 1188 if (err) { 1189 if_printf(ndev, "vPort RX configuration failed: %d\n", err); 1190 goto out; 1191 } 1192 1193 if (resp.hdr.status) { 1194 if_printf(ndev, "vPort RX configuration failed: 0x%x\n", 1195 resp.hdr.status); 1196 err = EPROTO; 1197 } 1198 1199 if_printf(ndev, "Configured steering vPort %ju entries %u\n", 1200 apc->port_handle, num_entries); 1201 1202 out: 1203 free(req, M_DEVBUF); 1204 return err; 1205 } 1206 1207 int 1208 mana_create_wq_obj(struct mana_port_context *apc, 1209 mana_handle_t vport, 1210 uint32_t wq_type, struct mana_obj_spec *wq_spec, 1211 struct mana_obj_spec *cq_spec, 1212 mana_handle_t *wq_obj) 1213 { 1214 struct mana_create_wqobj_resp resp = {}; 1215 struct mana_create_wqobj_req req = {}; 1216 if_t ndev = apc->ndev; 1217 int err; 1218 1219 mana_gd_init_req_hdr(&req.hdr, MANA_CREATE_WQ_OBJ, 1220 sizeof(req), sizeof(resp)); 1221 req.vport = vport; 1222 req.wq_type = wq_type; 1223 req.wq_gdma_region = wq_spec->gdma_region; 1224 req.cq_gdma_region = cq_spec->gdma_region; 1225 req.wq_size = wq_spec->queue_size; 1226 req.cq_size = cq_spec->queue_size; 1227 req.cq_moderation_ctx_id = cq_spec->modr_ctx_id; 1228 req.cq_parent_qid = cq_spec->attached_eq; 1229 1230 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1231 sizeof(resp)); 1232 if (err) { 1233 if_printf(ndev, "Failed to create WQ object: %d\n", err); 1234 goto out; 1235 } 1236 1237 err = mana_verify_resp_hdr(&resp.hdr, MANA_CREATE_WQ_OBJ, 1238 sizeof(resp)); 1239 if (err || resp.hdr.status) { 1240 if_printf(ndev, "Failed to create WQ object: %d, 0x%x\n", err, 1241 resp.hdr.status); 1242 if (!err) 1243 err = EPROTO; 1244 goto out; 1245 } 1246 1247 if (resp.wq_obj == INVALID_MANA_HANDLE) { 1248 if_printf(ndev, "Got an invalid WQ object handle\n"); 1249 err = EPROTO; 1250 goto out; 1251 } 1252 1253 *wq_obj = resp.wq_obj; 1254 wq_spec->queue_index = resp.wq_id; 1255 cq_spec->queue_index = resp.cq_id; 1256 1257 return 0; 1258 out: 1259 return err; 1260 } 1261 1262 void 1263 mana_destroy_wq_obj(struct mana_port_context *apc, uint32_t wq_type, 1264 mana_handle_t wq_obj) 1265 { 1266 struct mana_destroy_wqobj_resp resp = {}; 1267 struct mana_destroy_wqobj_req req = {}; 1268 if_t ndev = apc->ndev; 1269 int err; 1270 1271 mana_gd_init_req_hdr(&req.hdr, MANA_DESTROY_WQ_OBJ, 1272 sizeof(req), sizeof(resp)); 1273 req.wq_type = wq_type; 1274 req.wq_obj_handle = wq_obj; 1275 1276 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1277 sizeof(resp)); 1278 if (err) { 1279 if_printf(ndev, "Failed to destroy WQ object: %d\n", err); 1280 return; 1281 } 1282 1283 err = mana_verify_resp_hdr(&resp.hdr, MANA_DESTROY_WQ_OBJ, 1284 sizeof(resp)); 1285 if (err || resp.hdr.status) 1286 if_printf(ndev, "Failed to destroy WQ object: %d, 0x%x\n", 1287 err, resp.hdr.status); 1288 } 1289 1290 static void 1291 mana_destroy_eq(struct mana_context *ac) 1292 { 1293 struct gdma_context *gc = ac->gdma_dev->gdma_context; 1294 struct gdma_queue *eq; 1295 int i; 1296 1297 if (!ac->eqs) 1298 return; 1299 1300 for (i = 0; i < gc->max_num_queues; i++) { 1301 eq = ac->eqs[i].eq; 1302 if (!eq) 1303 continue; 1304 1305 mana_gd_destroy_queue(gc, eq); 1306 } 1307 1308 free(ac->eqs, M_DEVBUF); 1309 ac->eqs = NULL; 1310 } 1311 1312 static int 1313 mana_create_eq(struct mana_context *ac) 1314 { 1315 struct gdma_dev *gd = ac->gdma_dev; 1316 struct gdma_context *gc = gd->gdma_context; 1317 struct gdma_queue_spec spec = {}; 1318 int err; 1319 int i; 1320 1321 ac->eqs = mallocarray(gc->max_num_queues, sizeof(struct mana_eq), 1322 M_DEVBUF, M_WAITOK | M_ZERO); 1323 1324 spec.type = GDMA_EQ; 1325 spec.monitor_avl_buf = false; 1326 spec.queue_size = EQ_SIZE; 1327 spec.eq.callback = NULL; 1328 spec.eq.context = ac->eqs; 1329 spec.eq.log2_throttle_limit = LOG2_EQ_THROTTLE; 1330 1331 for (i = 0; i < gc->max_num_queues; i++) { 1332 err = mana_gd_create_mana_eq(gd, &spec, &ac->eqs[i].eq); 1333 if (err) 1334 goto out; 1335 } 1336 1337 return 0; 1338 out: 1339 mana_destroy_eq(ac); 1340 return err; 1341 } 1342 1343 static int 1344 mana_fence_rq(struct mana_port_context *apc, struct mana_rxq *rxq) 1345 { 1346 struct mana_fence_rq_resp resp = {}; 1347 struct mana_fence_rq_req req = {}; 1348 int err; 1349 1350 init_completion(&rxq->fence_event); 1351 1352 mana_gd_init_req_hdr(&req.hdr, MANA_FENCE_RQ, 1353 sizeof(req), sizeof(resp)); 1354 req.wq_obj_handle = rxq->rxobj; 1355 1356 err = mana_send_request(apc->ac, &req, sizeof(req), &resp, 1357 sizeof(resp)); 1358 if (err) { 1359 if_printf(apc->ndev, "Failed to fence RQ %u: %d\n", 1360 rxq->rxq_idx, err); 1361 return err; 1362 } 1363 1364 err = mana_verify_resp_hdr(&resp.hdr, MANA_FENCE_RQ, sizeof(resp)); 1365 if (err || resp.hdr.status) { 1366 if_printf(apc->ndev, "Failed to fence RQ %u: %d, 0x%x\n", 1367 rxq->rxq_idx, err, resp.hdr.status); 1368 if (!err) 1369 err = EPROTO; 1370 1371 return err; 1372 } 1373 1374 if (wait_for_completion_timeout(&rxq->fence_event, 10 * hz)) { 1375 if_printf(apc->ndev, "Failed to fence RQ %u: timed out\n", 1376 rxq->rxq_idx); 1377 return ETIMEDOUT; 1378 } 1379 1380 return 0; 1381 } 1382 1383 static void 1384 mana_fence_rqs(struct mana_port_context *apc) 1385 { 1386 unsigned int rxq_idx; 1387 struct mana_rxq *rxq; 1388 int err; 1389 1390 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) { 1391 rxq = apc->rxqs[rxq_idx]; 1392 err = mana_fence_rq(apc, rxq); 1393 1394 /* In case of any error, use sleep instead. */ 1395 if (err) 1396 gdma_msleep(100); 1397 } 1398 } 1399 1400 static int 1401 mana_move_wq_tail(struct gdma_queue *wq, uint32_t num_units) 1402 { 1403 uint32_t used_space_old; 1404 uint32_t used_space_new; 1405 1406 used_space_old = wq->head - wq->tail; 1407 used_space_new = wq->head - (wq->tail + num_units); 1408 1409 if (used_space_new > used_space_old) { 1410 mana_err(NULL, 1411 "WARNING: new used space %u greater than old one %u\n", 1412 used_space_new, used_space_old); 1413 return ERANGE; 1414 } 1415 1416 wq->tail += num_units; 1417 return 0; 1418 } 1419 1420 static void 1421 mana_poll_tx_cq(struct mana_cq *cq) 1422 { 1423 struct gdma_comp *completions = cq->gdma_comp_buf; 1424 struct gdma_posted_wqe_info *wqe_info; 1425 struct mana_send_buf_info *tx_info; 1426 unsigned int pkt_transmitted = 0; 1427 unsigned int wqe_unit_cnt = 0; 1428 struct mana_txq *txq = cq->txq; 1429 struct mana_port_context *apc; 1430 unsigned int tx_queue_size; 1431 uint16_t next_to_complete; 1432 if_t ndev; 1433 int comp_read; 1434 int txq_idx = txq->idx; 1435 int i; 1436 int sa_drop = 0; 1437 1438 struct gdma_queue *gdma_wq; 1439 unsigned int avail_space; 1440 bool txq_full = false; 1441 1442 ndev = txq->ndev; 1443 apc = if_getsoftc(ndev); 1444 tx_queue_size = apc->tx_queue_size; 1445 1446 comp_read = mana_gd_poll_cq(cq->gdma_cq, completions, 1447 CQE_POLLING_BUFFER); 1448 1449 if (comp_read < 1) 1450 return; 1451 1452 next_to_complete = txq->next_to_complete; 1453 1454 for (i = 0; i < comp_read; i++) { 1455 struct mana_tx_comp_oob *cqe_oob; 1456 1457 if (!completions[i].is_sq) { 1458 mana_err(NULL, "WARNING: Not for SQ\n"); 1459 return; 1460 } 1461 1462 cqe_oob = (struct mana_tx_comp_oob *)completions[i].cqe_data; 1463 if (cqe_oob->cqe_hdr.client_type != 1464 MANA_CQE_COMPLETION) { 1465 mana_err(NULL, 1466 "WARNING: Invalid CQE client type %u\n", 1467 cqe_oob->cqe_hdr.client_type); 1468 return; 1469 } 1470 1471 switch (cqe_oob->cqe_hdr.cqe_type) { 1472 case CQE_TX_OKAY: 1473 break; 1474 1475 case CQE_TX_SA_DROP: 1476 case CQE_TX_MTU_DROP: 1477 case CQE_TX_INVALID_OOB: 1478 case CQE_TX_INVALID_ETH_TYPE: 1479 case CQE_TX_HDR_PROCESSING_ERROR: 1480 case CQE_TX_VF_DISABLED: 1481 case CQE_TX_VPORT_IDX_OUT_OF_RANGE: 1482 case CQE_TX_VPORT_DISABLED: 1483 case CQE_TX_VLAN_TAGGING_VIOLATION: 1484 sa_drop ++; 1485 mana_dbg(NULL, 1486 "TX: txq %d CQE error %d, ntc = %d, " 1487 "pending sends = %d: err ignored.\n", 1488 txq_idx, cqe_oob->cqe_hdr.cqe_type, 1489 next_to_complete, txq->pending_sends); 1490 counter_u64_add(txq->stats.cqe_err, 1); 1491 break; 1492 1493 default: 1494 /* If the CQE type is unknown, log a debug msg, 1495 * and still free the mbuf, etc. 1496 */ 1497 mana_dbg(NULL, 1498 "ERROR: TX: Unknown CQE type %d\n", 1499 cqe_oob->cqe_hdr.cqe_type); 1500 counter_u64_add(txq->stats.cqe_unknown_type, 1); 1501 break; 1502 } 1503 if (txq->gdma_txq_id != completions[i].wq_num) { 1504 mana_dbg(NULL, 1505 "txq gdma id not match completion wq num: " 1506 "%d != %d\n", 1507 txq->gdma_txq_id, completions[i].wq_num); 1508 break; 1509 } 1510 1511 tx_info = &txq->tx_buf_info[next_to_complete]; 1512 if (!tx_info->mbuf) { 1513 mana_err(NULL, 1514 "WARNING: txq %d Empty mbuf on tx_info: %u, " 1515 "ntu = %u, pending_sends = %d, " 1516 "transmitted = %d, sa_drop = %d, i = %d, comp_read = %d\n", 1517 txq_idx, next_to_complete, txq->next_to_use, 1518 txq->pending_sends, pkt_transmitted, sa_drop, 1519 i, comp_read); 1520 break; 1521 } 1522 1523 wqe_info = &tx_info->wqe_inf; 1524 wqe_unit_cnt += wqe_info->wqe_size_in_bu; 1525 1526 mana_tx_unmap_mbuf(apc, tx_info); 1527 mb(); 1528 1529 next_to_complete = 1530 MANA_IDX_NEXT(next_to_complete, tx_queue_size); 1531 1532 pkt_transmitted++; 1533 } 1534 1535 txq->next_to_complete = next_to_complete; 1536 1537 if (wqe_unit_cnt == 0) { 1538 mana_err(NULL, 1539 "WARNING: TX ring not proceeding!\n"); 1540 return; 1541 } 1542 1543 mana_move_wq_tail(txq->gdma_sq, wqe_unit_cnt); 1544 1545 /* Ensure tail updated before checking q stop */ 1546 wmb(); 1547 1548 gdma_wq = txq->gdma_sq; 1549 avail_space = mana_gd_wq_avail_space(gdma_wq); 1550 1551 1552 if ((if_getdrvflags(ndev) & MANA_TXQ_FULL) == MANA_TXQ_FULL) { 1553 txq_full = true; 1554 } 1555 1556 /* Ensure checking txq_full before apc->port_is_up. */ 1557 rmb(); 1558 1559 if (txq_full && apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) { 1560 /* Grab the txq lock and re-test */ 1561 mtx_lock(&txq->txq_mtx); 1562 avail_space = mana_gd_wq_avail_space(gdma_wq); 1563 1564 if ((if_getdrvflags(ndev) & MANA_TXQ_FULL) == MANA_TXQ_FULL && 1565 apc->port_is_up && avail_space >= MAX_TX_WQE_SIZE) { 1566 /* Clear the Q full flag */ 1567 if_setdrvflagbits(apc->ndev, IFF_DRV_RUNNING, 1568 IFF_DRV_OACTIVE); 1569 counter_u64_add(txq->stats.wakeup, 1); 1570 if (txq->alt_txq_idx != txq->idx) { 1571 uint64_t stops = counter_u64_fetch(txq->stats.stop); 1572 uint64_t wakeups = counter_u64_fetch(txq->stats.wakeup); 1573 /* Reset alt_txq_idx back if it is not overloaded */ 1574 if (stops < wakeups) { 1575 txq->alt_txq_idx = txq->idx; 1576 counter_u64_add(txq->stats.alt_reset, 1); 1577 } 1578 } 1579 rmb(); 1580 /* Schedule a tx enqueue task */ 1581 taskqueue_enqueue(txq->enqueue_tq, &txq->enqueue_task); 1582 } 1583 mtx_unlock(&txq->txq_mtx); 1584 } 1585 1586 if (atomic_sub_return(pkt_transmitted, &txq->pending_sends) < 0) 1587 mana_err(NULL, 1588 "WARNING: TX %d pending_sends error: %d\n", 1589 txq->idx, txq->pending_sends); 1590 1591 cq->work_done = pkt_transmitted; 1592 } 1593 1594 static void 1595 mana_post_pkt_rxq(struct mana_rxq *rxq, 1596 struct mana_recv_buf_oob *recv_buf_oob) 1597 { 1598 int err; 1599 1600 err = mana_gd_post_work_request(rxq->gdma_rq, &recv_buf_oob->wqe_req, 1601 &recv_buf_oob->wqe_inf); 1602 if (err) { 1603 mana_err(NULL, "WARNING: rxq %u post pkt err %d\n", 1604 rxq->rxq_idx, err); 1605 return; 1606 } 1607 1608 if (recv_buf_oob->wqe_inf.wqe_size_in_bu != 1) { 1609 mana_err(NULL, "WARNING: rxq %u wqe_size_in_bu %u\n", 1610 rxq->rxq_idx, recv_buf_oob->wqe_inf.wqe_size_in_bu); 1611 } 1612 } 1613 1614 static void 1615 mana_rx_mbuf(struct mbuf *mbuf, struct mana_rxcomp_oob *cqe, 1616 struct mana_rxq *rxq) 1617 { 1618 struct mana_stats *rx_stats = &rxq->stats; 1619 if_t ndev = rxq->ndev; 1620 uint32_t pkt_len = cqe->ppi[0].pkt_len; 1621 uint16_t rxq_idx = rxq->rxq_idx; 1622 struct mana_port_context *apc; 1623 bool do_lro = false; 1624 bool do_if_input; 1625 1626 apc = if_getsoftc(ndev); 1627 rxq->rx_cq.work_done++; 1628 1629 if (!mbuf) { 1630 return; 1631 } 1632 1633 mbuf->m_flags |= M_PKTHDR; 1634 mbuf->m_pkthdr.len = pkt_len; 1635 mbuf->m_len = pkt_len; 1636 mbuf->m_pkthdr.rcvif = ndev; 1637 1638 if ((if_getcapenable(ndev) & IFCAP_RXCSUM || 1639 if_getcapenable(ndev) & IFCAP_RXCSUM_IPV6) && 1640 (cqe->rx_iphdr_csum_succeed)) { 1641 mbuf->m_pkthdr.csum_flags = CSUM_IP_CHECKED; 1642 mbuf->m_pkthdr.csum_flags |= CSUM_IP_VALID; 1643 if (cqe->rx_tcp_csum_succeed || cqe->rx_udp_csum_succeed) { 1644 mbuf->m_pkthdr.csum_flags |= 1645 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); 1646 mbuf->m_pkthdr.csum_data = 0xffff; 1647 1648 if (cqe->rx_tcp_csum_succeed) 1649 do_lro = true; 1650 } 1651 } 1652 1653 if (cqe->rx_hashtype != 0) { 1654 mbuf->m_pkthdr.flowid = cqe->ppi[0].pkt_hash; 1655 1656 uint16_t hashtype = cqe->rx_hashtype; 1657 if (hashtype & NDIS_HASH_IPV4_MASK) { 1658 hashtype &= NDIS_HASH_IPV4_MASK; 1659 switch (hashtype) { 1660 case NDIS_HASH_TCP_IPV4: 1661 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV4); 1662 break; 1663 case NDIS_HASH_UDP_IPV4: 1664 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV4); 1665 break; 1666 default: 1667 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV4); 1668 } 1669 } else if (hashtype & NDIS_HASH_IPV6_MASK) { 1670 hashtype &= NDIS_HASH_IPV6_MASK; 1671 switch (hashtype) { 1672 case NDIS_HASH_TCP_IPV6: 1673 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_TCP_IPV6); 1674 break; 1675 case NDIS_HASH_TCP_IPV6_EX: 1676 M_HASHTYPE_SET(mbuf, 1677 M_HASHTYPE_RSS_TCP_IPV6_EX); 1678 break; 1679 case NDIS_HASH_UDP_IPV6: 1680 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_UDP_IPV6); 1681 break; 1682 case NDIS_HASH_UDP_IPV6_EX: 1683 M_HASHTYPE_SET(mbuf, 1684 M_HASHTYPE_RSS_UDP_IPV6_EX); 1685 break; 1686 default: 1687 M_HASHTYPE_SET(mbuf, M_HASHTYPE_RSS_IPV6); 1688 } 1689 } else { 1690 M_HASHTYPE_SET(mbuf, M_HASHTYPE_OPAQUE_HASH); 1691 } 1692 } else { 1693 mbuf->m_pkthdr.flowid = rxq_idx; 1694 M_HASHTYPE_SET(mbuf, M_HASHTYPE_NONE); 1695 } 1696 1697 do_if_input = true; 1698 if ((if_getcapenable(ndev) & IFCAP_LRO) && do_lro) { 1699 rxq->lro_tried++; 1700 if (rxq->lro.lro_cnt != 0 && 1701 tcp_lro_rx(&rxq->lro, mbuf, 0) == 0) 1702 do_if_input = false; 1703 else 1704 rxq->lro_failed++; 1705 } 1706 if (do_if_input) { 1707 if_input(ndev, mbuf); 1708 } 1709 1710 counter_enter(); 1711 counter_u64_add_protected(rx_stats->packets, 1); 1712 counter_u64_add_protected(apc->port_stats.rx_packets, 1); 1713 counter_u64_add_protected(rx_stats->bytes, pkt_len); 1714 counter_u64_add_protected(apc->port_stats.rx_bytes, pkt_len); 1715 counter_exit(); 1716 } 1717 1718 static int 1719 mana_refill_rx_mbufs(struct mana_port_context *apc, 1720 struct mana_rxq *rxq, uint32_t num) 1721 { 1722 struct mana_recv_buf_oob *rxbuf_oob; 1723 uint32_t next_to_refill; 1724 uint32_t i; 1725 int err; 1726 1727 next_to_refill = rxq->next_to_refill; 1728 1729 for (i = 0; i < num; i++) { 1730 if (next_to_refill == rxq->buf_index) { 1731 mana_warn(NULL, "refilling index reached current, " 1732 "aborted! rxq %u, oob idx %u\n", 1733 rxq->rxq_idx, next_to_refill); 1734 break; 1735 } 1736 1737 rxbuf_oob = &rxq->rx_oobs[next_to_refill]; 1738 1739 if (likely(rxbuf_oob->mbuf == NULL)) { 1740 err = mana_load_rx_mbuf(apc, rxq, rxbuf_oob, true); 1741 } else { 1742 mana_warn(NULL, "mbuf not null when refilling, " 1743 "rxq %u, oob idx %u, reusing\n", 1744 rxq->rxq_idx, next_to_refill); 1745 err = mana_load_rx_mbuf(apc, rxq, rxbuf_oob, false); 1746 } 1747 1748 if (unlikely(err != 0)) { 1749 mana_dbg(NULL, 1750 "failed to load rx mbuf, err = %d, rxq = %u\n", 1751 err, rxq->rxq_idx); 1752 counter_u64_add(rxq->stats.mbuf_alloc_fail, 1); 1753 break; 1754 } 1755 1756 mana_post_pkt_rxq(rxq, rxbuf_oob); 1757 1758 next_to_refill = MANA_IDX_NEXT(next_to_refill, 1759 rxq->num_rx_buf); 1760 } 1761 1762 if (likely(i != 0)) { 1763 struct gdma_context *gc = 1764 rxq->gdma_rq->gdma_dev->gdma_context; 1765 1766 mana_gd_wq_ring_doorbell(gc, rxq->gdma_rq); 1767 } 1768 1769 if (unlikely(i < num)) { 1770 counter_u64_add(rxq->stats.partial_refill, 1); 1771 mana_dbg(NULL, 1772 "refilled rxq %u with only %u mbufs (%u requested)\n", 1773 rxq->rxq_idx, i, num); 1774 } 1775 1776 rxq->next_to_refill = next_to_refill; 1777 return (i); 1778 } 1779 1780 static void 1781 mana_process_rx_cqe(struct mana_rxq *rxq, struct mana_cq *cq, 1782 struct gdma_comp *cqe) 1783 { 1784 struct mana_rxcomp_oob *oob = (struct mana_rxcomp_oob *)cqe->cqe_data; 1785 struct mana_recv_buf_oob *rxbuf_oob; 1786 if_t ndev = rxq->ndev; 1787 struct mana_port_context *apc; 1788 struct mbuf *old_mbuf; 1789 uint32_t refill_required; 1790 uint32_t curr, pktlen; 1791 1792 switch (oob->cqe_hdr.cqe_type) { 1793 case CQE_RX_OKAY: 1794 break; 1795 1796 case CQE_RX_TRUNCATED: 1797 apc = if_getsoftc(ndev); 1798 counter_u64_add(apc->port_stats.rx_drops, 1); 1799 rxbuf_oob = &rxq->rx_oobs[rxq->buf_index]; 1800 if_printf(ndev, "Dropped a truncated packet\n"); 1801 goto drop; 1802 1803 case CQE_RX_COALESCED_4: 1804 if_printf(ndev, "RX coalescing is unsupported\n"); 1805 return; 1806 1807 case CQE_RX_OBJECT_FENCE: 1808 complete(&rxq->fence_event); 1809 return; 1810 1811 default: 1812 if_printf(ndev, "Unknown RX CQE type = %d\n", 1813 oob->cqe_hdr.cqe_type); 1814 return; 1815 } 1816 1817 if (oob->cqe_hdr.cqe_type != CQE_RX_OKAY) 1818 return; 1819 1820 pktlen = oob->ppi[0].pkt_len; 1821 1822 if (pktlen == 0) { 1823 /* data packets should never have packetlength of zero */ 1824 if_printf(ndev, "RX pkt len=0, rq=%u, cq=%u, rxobj=0x%jx\n", 1825 rxq->gdma_id, cq->gdma_id, rxq->rxobj); 1826 return; 1827 } 1828 1829 curr = rxq->buf_index; 1830 rxbuf_oob = &rxq->rx_oobs[curr]; 1831 if (rxbuf_oob->wqe_inf.wqe_size_in_bu != 1) { 1832 mana_err(NULL, "WARNING: Rx Incorrect complete " 1833 "WQE size %u\n", 1834 rxbuf_oob->wqe_inf.wqe_size_in_bu); 1835 } 1836 1837 apc = if_getsoftc(ndev); 1838 1839 old_mbuf = rxbuf_oob->mbuf; 1840 1841 /* Unload DMA map for the old mbuf */ 1842 mana_unload_rx_mbuf(apc, rxq, rxbuf_oob, false); 1843 /* Clear the mbuf pointer to avoid reuse */ 1844 rxbuf_oob->mbuf = NULL; 1845 1846 mana_rx_mbuf(old_mbuf, oob, rxq); 1847 1848 drop: 1849 mana_move_wq_tail(rxq->gdma_rq, rxbuf_oob->wqe_inf.wqe_size_in_bu); 1850 1851 rxq->buf_index = MANA_IDX_NEXT(rxq->buf_index, rxq->num_rx_buf); 1852 1853 /* Check if refill is needed */ 1854 refill_required = MANA_GET_SPACE(rxq->next_to_refill, 1855 rxq->buf_index, rxq->num_rx_buf); 1856 1857 if (refill_required >= rxq->refill_thresh) { 1858 /* Refill empty rx_oobs with new mbufs */ 1859 mana_refill_rx_mbufs(apc, rxq, refill_required); 1860 } 1861 } 1862 1863 static void 1864 mana_poll_rx_cq(struct mana_cq *cq) 1865 { 1866 struct gdma_comp *comp = cq->gdma_comp_buf; 1867 int comp_read, i; 1868 1869 comp_read = mana_gd_poll_cq(cq->gdma_cq, comp, CQE_POLLING_BUFFER); 1870 KASSERT(comp_read <= CQE_POLLING_BUFFER, 1871 ("comp_read %d great than buf size %d", 1872 comp_read, CQE_POLLING_BUFFER)); 1873 1874 for (i = 0; i < comp_read; i++) { 1875 if (comp[i].is_sq == true) { 1876 mana_err(NULL, 1877 "WARNING: CQE not for receive queue\n"); 1878 return; 1879 } 1880 1881 /* verify recv cqe references the right rxq */ 1882 if (comp[i].wq_num != cq->rxq->gdma_id) { 1883 mana_err(NULL, 1884 "WARNING: Received CQE %d not for " 1885 "this receive queue %d\n", 1886 comp[i].wq_num, cq->rxq->gdma_id); 1887 return; 1888 } 1889 1890 mana_process_rx_cqe(cq->rxq, cq, &comp[i]); 1891 } 1892 1893 tcp_lro_flush_all(&cq->rxq->lro); 1894 } 1895 1896 static void 1897 mana_cq_handler(void *context, struct gdma_queue *gdma_queue) 1898 { 1899 struct mana_cq *cq = context; 1900 uint8_t arm_bit; 1901 1902 KASSERT(cq->gdma_cq == gdma_queue, 1903 ("cq do not match %p, %p", cq->gdma_cq, gdma_queue)); 1904 1905 if (cq->type == MANA_CQ_TYPE_RX) { 1906 mana_poll_rx_cq(cq); 1907 } else { 1908 mana_poll_tx_cq(cq); 1909 } 1910 1911 if (cq->work_done < cq->budget && cq->do_not_ring_db == false) 1912 arm_bit = SET_ARM_BIT; 1913 else 1914 arm_bit = 0; 1915 1916 mana_gd_ring_cq(gdma_queue, arm_bit); 1917 } 1918 1919 #define MANA_POLL_BUDGET 256 1920 #define MANA_RX_BUDGET 8 1921 #define MANA_TX_BUDGET 8 1922 1923 static void 1924 mana_poll(void *arg, int pending) 1925 { 1926 struct mana_cq *cq = arg; 1927 int i; 1928 1929 cq->work_done = 0; 1930 if (cq->type == MANA_CQ_TYPE_RX) { 1931 cq->budget = MANA_RX_BUDGET; 1932 } else { 1933 cq->budget = MANA_TX_BUDGET; 1934 } 1935 1936 for (i = 0; i < MANA_POLL_BUDGET; i++) { 1937 /* 1938 * If this is the last loop, set the budget big enough 1939 * so it will arm the CQ any way. 1940 */ 1941 if (i == (MANA_POLL_BUDGET - 1)) 1942 cq->budget = CQE_POLLING_BUFFER + 1; 1943 1944 mana_cq_handler(cq, cq->gdma_cq); 1945 1946 if (cq->work_done < cq->budget) 1947 break; 1948 1949 cq->work_done = 0; 1950 } 1951 } 1952 1953 static void 1954 mana_schedule_task(void *arg, struct gdma_queue *gdma_queue) 1955 { 1956 struct mana_cq *cq = arg; 1957 1958 taskqueue_enqueue(cq->cleanup_tq, &cq->cleanup_task); 1959 } 1960 1961 static void 1962 mana_deinit_cq(struct mana_port_context *apc, struct mana_cq *cq) 1963 { 1964 struct gdma_dev *gd = apc->ac->gdma_dev; 1965 1966 if (!cq->gdma_cq) 1967 return; 1968 1969 /* Drain cleanup taskqueue */ 1970 if (cq->cleanup_tq) { 1971 while (taskqueue_cancel(cq->cleanup_tq, 1972 &cq->cleanup_task, NULL)) { 1973 taskqueue_drain(cq->cleanup_tq, 1974 &cq->cleanup_task); 1975 } 1976 1977 taskqueue_free(cq->cleanup_tq); 1978 } 1979 1980 mana_gd_destroy_queue(gd->gdma_context, cq->gdma_cq); 1981 } 1982 1983 static void 1984 mana_deinit_txq(struct mana_port_context *apc, struct mana_txq *txq) 1985 { 1986 struct gdma_dev *gd = apc->ac->gdma_dev; 1987 struct mana_send_buf_info *txbuf_info; 1988 uint32_t pending_sends; 1989 int i; 1990 1991 if (!txq->gdma_sq) 1992 return; 1993 1994 if ((pending_sends = atomic_read(&txq->pending_sends)) > 0) { 1995 mana_err(NULL, 1996 "WARNING: txq pending sends not zero: %u\n", 1997 pending_sends); 1998 } 1999 2000 if (txq->next_to_use != txq->next_to_complete) { 2001 mana_err(NULL, 2002 "WARNING: txq buf not completed, " 2003 "next use %u, next complete %u\n", 2004 txq->next_to_use, txq->next_to_complete); 2005 } 2006 2007 /* Flush buf ring. Grab txq mtx lock */ 2008 if (txq->txq_br) { 2009 mtx_lock(&txq->txq_mtx); 2010 drbr_flush(apc->ndev, txq->txq_br); 2011 mtx_unlock(&txq->txq_mtx); 2012 buf_ring_free(txq->txq_br, M_DEVBUF); 2013 } 2014 2015 /* Drain taskqueue */ 2016 if (txq->enqueue_tq) { 2017 while (taskqueue_cancel(txq->enqueue_tq, 2018 &txq->enqueue_task, NULL)) { 2019 taskqueue_drain(txq->enqueue_tq, 2020 &txq->enqueue_task); 2021 } 2022 2023 taskqueue_free(txq->enqueue_tq); 2024 } 2025 2026 if (txq->tx_buf_info) { 2027 /* Free all mbufs which are still in-flight */ 2028 for (i = 0; i < apc->tx_queue_size; i++) { 2029 txbuf_info = &txq->tx_buf_info[i]; 2030 if (txbuf_info->mbuf) { 2031 mana_tx_unmap_mbuf(apc, txbuf_info); 2032 } 2033 } 2034 2035 free(txq->tx_buf_info, M_DEVBUF); 2036 } 2037 2038 mana_free_counters((counter_u64_t *)&txq->stats, 2039 sizeof(txq->stats)); 2040 2041 mana_gd_destroy_queue(gd->gdma_context, txq->gdma_sq); 2042 2043 mtx_destroy(&txq->txq_mtx); 2044 } 2045 2046 static void 2047 mana_destroy_txq(struct mana_port_context *apc) 2048 { 2049 int i; 2050 2051 if (!apc->tx_qp) 2052 return; 2053 2054 for (i = 0; i < apc->num_queues; i++) { 2055 mana_destroy_wq_obj(apc, GDMA_SQ, apc->tx_qp[i].tx_object); 2056 2057 mana_deinit_cq(apc, &apc->tx_qp[i].tx_cq); 2058 2059 mana_deinit_txq(apc, &apc->tx_qp[i].txq); 2060 } 2061 2062 free(apc->tx_qp, M_DEVBUF); 2063 apc->tx_qp = NULL; 2064 } 2065 2066 static int 2067 mana_create_txq(struct mana_port_context *apc, if_t net) 2068 { 2069 struct mana_context *ac = apc->ac; 2070 struct gdma_dev *gd = ac->gdma_dev; 2071 struct mana_obj_spec wq_spec; 2072 struct mana_obj_spec cq_spec; 2073 struct gdma_queue_spec spec; 2074 struct gdma_context *gc; 2075 struct mana_txq *txq; 2076 struct mana_cq *cq; 2077 uint32_t txq_size; 2078 uint32_t cq_size; 2079 int err; 2080 int i; 2081 2082 apc->tx_qp = mallocarray(apc->num_queues, sizeof(struct mana_tx_qp), 2083 M_DEVBUF, M_WAITOK | M_ZERO); 2084 2085 /* The minimum size of the WQE is 32 bytes, hence 2086 * apc->tx_queue_size represents the maximum number of WQEs 2087 * the SQ can store. This value is then used to size other queues 2088 * to prevent overflow. 2089 * Also note that the txq_size is always going to be page aligned, 2090 * as min val of apc->tx_queue_size is 128 and that would make 2091 * txq_size 128 * 32 = 4096 and the other higher values of 2092 * apc->tx_queue_size are always power of two. 2093 */ 2094 txq_size = apc->tx_queue_size * 32; 2095 KASSERT(IS_ALIGNED(txq_size, PAGE_SIZE), 2096 ("txq size not page aligned")); 2097 2098 cq_size = apc->tx_queue_size * COMP_ENTRY_SIZE; 2099 cq_size = ALIGN(cq_size, PAGE_SIZE); 2100 2101 gc = gd->gdma_context; 2102 2103 for (i = 0; i < apc->num_queues; i++) { 2104 apc->tx_qp[i].tx_object = INVALID_MANA_HANDLE; 2105 2106 /* Create SQ */ 2107 txq = &apc->tx_qp[i].txq; 2108 2109 txq->ndev = net; 2110 txq->vp_offset = apc->tx_vp_offset; 2111 txq->idx = i; 2112 txq->alt_txq_idx = i; 2113 2114 memset(&spec, 0, sizeof(spec)); 2115 spec.type = GDMA_SQ; 2116 spec.monitor_avl_buf = true; 2117 spec.queue_size = txq_size; 2118 err = mana_gd_create_mana_wq_cq(gd, &spec, &txq->gdma_sq); 2119 if (err) 2120 goto out; 2121 2122 /* Create SQ's CQ */ 2123 cq = &apc->tx_qp[i].tx_cq; 2124 cq->type = MANA_CQ_TYPE_TX; 2125 2126 cq->txq = txq; 2127 2128 memset(&spec, 0, sizeof(spec)); 2129 spec.type = GDMA_CQ; 2130 spec.monitor_avl_buf = false; 2131 spec.queue_size = cq_size; 2132 spec.cq.callback = mana_schedule_task; 2133 spec.cq.parent_eq = ac->eqs[i].eq; 2134 spec.cq.context = cq; 2135 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq); 2136 if (err) 2137 goto out; 2138 2139 memset(&wq_spec, 0, sizeof(wq_spec)); 2140 memset(&cq_spec, 0, sizeof(cq_spec)); 2141 2142 wq_spec.gdma_region = txq->gdma_sq->mem_info.dma_region_handle; 2143 wq_spec.queue_size = txq->gdma_sq->queue_size; 2144 2145 cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle; 2146 cq_spec.queue_size = cq->gdma_cq->queue_size; 2147 cq_spec.modr_ctx_id = 0; 2148 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id; 2149 2150 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_SQ, 2151 &wq_spec, &cq_spec, &apc->tx_qp[i].tx_object); 2152 2153 if (err) 2154 goto out; 2155 2156 txq->gdma_sq->id = wq_spec.queue_index; 2157 cq->gdma_cq->id = cq_spec.queue_index; 2158 2159 txq->gdma_sq->mem_info.dma_region_handle = 2160 GDMA_INVALID_DMA_REGION; 2161 cq->gdma_cq->mem_info.dma_region_handle = 2162 GDMA_INVALID_DMA_REGION; 2163 2164 txq->gdma_txq_id = txq->gdma_sq->id; 2165 2166 cq->gdma_id = cq->gdma_cq->id; 2167 2168 mana_dbg(NULL, 2169 "txq %d, txq gdma id %d, txq cq gdma id %d\n", 2170 i, txq->gdma_txq_id, cq->gdma_id); 2171 2172 if (cq->gdma_id >= gc->max_num_cqs) { 2173 if_printf(net, "CQ id %u too large.\n", cq->gdma_id); 2174 err = EINVAL; 2175 goto out; 2176 } 2177 2178 gc->cq_table[cq->gdma_id] = cq->gdma_cq; 2179 2180 /* Initialize tx specific data */ 2181 txq->tx_buf_info = malloc(apc->tx_queue_size * 2182 sizeof(struct mana_send_buf_info), 2183 M_DEVBUF, M_WAITOK | M_ZERO); 2184 2185 snprintf(txq->txq_mtx_name, nitems(txq->txq_mtx_name), 2186 "mana:tx(%d)", i); 2187 mtx_init(&txq->txq_mtx, txq->txq_mtx_name, NULL, MTX_DEF); 2188 2189 txq->txq_br = buf_ring_alloc(4 * apc->tx_queue_size, 2190 M_DEVBUF, M_WAITOK, &txq->txq_mtx); 2191 2192 /* Allocate taskqueue for deferred send */ 2193 TASK_INIT(&txq->enqueue_task, 0, mana_xmit_taskfunc, txq); 2194 txq->enqueue_tq = taskqueue_create_fast("mana_tx_enque", 2195 M_NOWAIT, taskqueue_thread_enqueue, &txq->enqueue_tq); 2196 if (unlikely(txq->enqueue_tq == NULL)) { 2197 if_printf(net, 2198 "Unable to create tx %d enqueue task queue\n", i); 2199 err = ENOMEM; 2200 goto out; 2201 } 2202 taskqueue_start_threads(&txq->enqueue_tq, 1, PI_NET, 2203 "mana txq p%u-tx%d", apc->port_idx, i); 2204 2205 mana_alloc_counters((counter_u64_t *)&txq->stats, 2206 sizeof(txq->stats)); 2207 2208 /* Allocate and start the cleanup task on CQ */ 2209 cq->do_not_ring_db = false; 2210 2211 NET_TASK_INIT(&cq->cleanup_task, 0, mana_poll, cq); 2212 cq->cleanup_tq = 2213 taskqueue_create_fast("mana tx cq cleanup", 2214 M_WAITOK, taskqueue_thread_enqueue, 2215 &cq->cleanup_tq); 2216 2217 if (apc->last_tx_cq_bind_cpu < 0) 2218 apc->last_tx_cq_bind_cpu = CPU_FIRST(); 2219 cq->cpu = apc->last_tx_cq_bind_cpu; 2220 apc->last_tx_cq_bind_cpu = CPU_NEXT(apc->last_tx_cq_bind_cpu); 2221 2222 if (apc->bind_cleanup_thread_cpu) { 2223 cpuset_t cpu_mask; 2224 CPU_SETOF(cq->cpu, &cpu_mask); 2225 taskqueue_start_threads_cpuset(&cq->cleanup_tq, 2226 1, PI_NET, &cpu_mask, 2227 "mana cq p%u-tx%u-cpu%d", 2228 apc->port_idx, txq->idx, cq->cpu); 2229 } else { 2230 taskqueue_start_threads(&cq->cleanup_tq, 1, 2231 PI_NET, "mana cq p%u-tx%u", 2232 apc->port_idx, txq->idx); 2233 } 2234 2235 mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT); 2236 } 2237 2238 return 0; 2239 out: 2240 mana_destroy_txq(apc); 2241 return err; 2242 } 2243 2244 static void 2245 mana_destroy_rxq(struct mana_port_context *apc, struct mana_rxq *rxq, 2246 bool validate_state) 2247 { 2248 struct gdma_context *gc = apc->ac->gdma_dev->gdma_context; 2249 struct mana_recv_buf_oob *rx_oob; 2250 int i; 2251 2252 if (!rxq) 2253 return; 2254 2255 if (validate_state) { 2256 /* 2257 * XXX Cancel and drain cleanup task queue here. 2258 */ 2259 ; 2260 } 2261 2262 mana_destroy_wq_obj(apc, GDMA_RQ, rxq->rxobj); 2263 2264 mana_deinit_cq(apc, &rxq->rx_cq); 2265 2266 mana_free_counters((counter_u64_t *)&rxq->stats, 2267 sizeof(rxq->stats)); 2268 2269 /* Free LRO resources */ 2270 tcp_lro_free(&rxq->lro); 2271 2272 for (i = 0; i < rxq->num_rx_buf; i++) { 2273 rx_oob = &rxq->rx_oobs[i]; 2274 2275 if (rx_oob->mbuf) 2276 mana_unload_rx_mbuf(apc, rxq, rx_oob, true); 2277 2278 bus_dmamap_destroy(apc->rx_buf_tag, rx_oob->dma_map); 2279 } 2280 2281 if (rxq->gdma_rq) 2282 mana_gd_destroy_queue(gc, rxq->gdma_rq); 2283 2284 free(rxq, M_DEVBUF); 2285 } 2286 2287 #define MANA_WQE_HEADER_SIZE 16 2288 #define MANA_WQE_SGE_SIZE 16 2289 2290 static int 2291 mana_alloc_rx_wqe(struct mana_port_context *apc, 2292 struct mana_rxq *rxq, uint32_t *rxq_size, uint32_t *cq_size) 2293 { 2294 struct mana_recv_buf_oob *rx_oob; 2295 uint32_t buf_idx; 2296 int err; 2297 2298 if (rxq->datasize == 0 || rxq->datasize > PAGE_SIZE) { 2299 mana_err(NULL, 2300 "WARNING: Invalid rxq datasize %u\n", rxq->datasize); 2301 } 2302 2303 *rxq_size = 0; 2304 *cq_size = 0; 2305 2306 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) { 2307 rx_oob = &rxq->rx_oobs[buf_idx]; 2308 memset(rx_oob, 0, sizeof(*rx_oob)); 2309 2310 err = bus_dmamap_create(apc->rx_buf_tag, 0, 2311 &rx_oob->dma_map); 2312 if (err) { 2313 mana_err(NULL, 2314 "Failed to create rx DMA map for buf %d\n", 2315 buf_idx); 2316 return err; 2317 } 2318 2319 err = mana_load_rx_mbuf(apc, rxq, rx_oob, true); 2320 if (err) { 2321 mana_err(NULL, 2322 "Failed to create rx DMA map for buf %d\n", 2323 buf_idx); 2324 bus_dmamap_destroy(apc->rx_buf_tag, rx_oob->dma_map); 2325 return err; 2326 } 2327 2328 rx_oob->wqe_req.sgl = rx_oob->sgl; 2329 rx_oob->wqe_req.num_sge = rx_oob->num_sge; 2330 rx_oob->wqe_req.inline_oob_size = 0; 2331 rx_oob->wqe_req.inline_oob_data = NULL; 2332 rx_oob->wqe_req.flags = 0; 2333 rx_oob->wqe_req.client_data_unit = 0; 2334 2335 *rxq_size += ALIGN(MANA_WQE_HEADER_SIZE + 2336 MANA_WQE_SGE_SIZE * rx_oob->num_sge, 32); 2337 *cq_size += COMP_ENTRY_SIZE; 2338 } 2339 2340 return 0; 2341 } 2342 2343 static int 2344 mana_push_wqe(struct mana_rxq *rxq) 2345 { 2346 struct mana_recv_buf_oob *rx_oob; 2347 uint32_t buf_idx; 2348 int err; 2349 2350 for (buf_idx = 0; buf_idx < rxq->num_rx_buf; buf_idx++) { 2351 rx_oob = &rxq->rx_oobs[buf_idx]; 2352 2353 err = mana_gd_post_and_ring(rxq->gdma_rq, &rx_oob->wqe_req, 2354 &rx_oob->wqe_inf); 2355 if (err) 2356 return ENOSPC; 2357 } 2358 2359 return 0; 2360 } 2361 2362 static struct mana_rxq * 2363 mana_create_rxq(struct mana_port_context *apc, uint32_t rxq_idx, 2364 struct mana_eq *eq, if_t ndev) 2365 { 2366 struct gdma_dev *gd = apc->ac->gdma_dev; 2367 struct mana_obj_spec wq_spec; 2368 struct mana_obj_spec cq_spec; 2369 struct gdma_queue_spec spec; 2370 struct mana_cq *cq = NULL; 2371 uint32_t cq_size, rq_size; 2372 struct gdma_context *gc; 2373 struct mana_rxq *rxq; 2374 int err; 2375 2376 gc = gd->gdma_context; 2377 2378 rxq = malloc(sizeof(*rxq) + 2379 apc->rx_queue_size * sizeof(struct mana_recv_buf_oob), 2380 M_DEVBUF, M_WAITOK | M_ZERO); 2381 rxq->ndev = ndev; 2382 rxq->num_rx_buf = apc->rx_queue_size; 2383 rxq->rxq_idx = rxq_idx; 2384 /* 2385 * Minimum size is MCLBYTES(2048) bytes for a mbuf cluster. 2386 * Now we just allow maximum size of 4096. 2387 */ 2388 rxq->datasize = ALIGN(apc->frame_size, MCLBYTES); 2389 if (rxq->datasize > MAX_FRAME_SIZE) 2390 rxq->datasize = MAX_FRAME_SIZE; 2391 2392 mana_dbg(NULL, "Setting rxq %d datasize %d\n", 2393 rxq_idx, rxq->datasize); 2394 2395 /* 2396 * Two steps to set the mbuf refill_thresh. 2397 * 1) If mana_rx_refill_threshold is set, honor it. 2398 * Set to default value otherwise. 2399 * 2) Select the smaller of 1) above and 1/4 of the 2400 * rx buffer size. 2401 */ 2402 if (mana_rx_refill_threshold != 0) 2403 rxq->refill_thresh = mana_rx_refill_threshold; 2404 else 2405 rxq->refill_thresh = MANA_RX_REFILL_THRESH; 2406 rxq->refill_thresh = min_t(uint32_t, 2407 rxq->num_rx_buf / 4, rxq->refill_thresh); 2408 2409 mana_dbg(NULL, "Setting rxq %d refill thresh %u\n", 2410 rxq_idx, rxq->refill_thresh); 2411 2412 rxq->rxobj = INVALID_MANA_HANDLE; 2413 2414 err = mana_alloc_rx_wqe(apc, rxq, &rq_size, &cq_size); 2415 if (err) 2416 goto out; 2417 2418 /* Create LRO for the RQ */ 2419 if (if_getcapenable(ndev) & IFCAP_LRO) { 2420 err = tcp_lro_init(&rxq->lro); 2421 if (err) { 2422 if_printf(ndev, "Failed to create LRO for rxq %d\n", 2423 rxq_idx); 2424 } else { 2425 rxq->lro.ifp = ndev; 2426 } 2427 } 2428 2429 mana_alloc_counters((counter_u64_t *)&rxq->stats, 2430 sizeof(rxq->stats)); 2431 2432 rq_size = ALIGN(rq_size, PAGE_SIZE); 2433 cq_size = ALIGN(cq_size, PAGE_SIZE); 2434 2435 /* Create RQ */ 2436 memset(&spec, 0, sizeof(spec)); 2437 spec.type = GDMA_RQ; 2438 spec.monitor_avl_buf = true; 2439 spec.queue_size = rq_size; 2440 err = mana_gd_create_mana_wq_cq(gd, &spec, &rxq->gdma_rq); 2441 if (err) 2442 goto out; 2443 2444 /* Create RQ's CQ */ 2445 cq = &rxq->rx_cq; 2446 cq->type = MANA_CQ_TYPE_RX; 2447 cq->rxq = rxq; 2448 2449 memset(&spec, 0, sizeof(spec)); 2450 spec.type = GDMA_CQ; 2451 spec.monitor_avl_buf = false; 2452 spec.queue_size = cq_size; 2453 spec.cq.callback = mana_schedule_task; 2454 spec.cq.parent_eq = eq->eq; 2455 spec.cq.context = cq; 2456 err = mana_gd_create_mana_wq_cq(gd, &spec, &cq->gdma_cq); 2457 if (err) 2458 goto out; 2459 2460 memset(&wq_spec, 0, sizeof(wq_spec)); 2461 memset(&cq_spec, 0, sizeof(cq_spec)); 2462 wq_spec.gdma_region = rxq->gdma_rq->mem_info.dma_region_handle; 2463 wq_spec.queue_size = rxq->gdma_rq->queue_size; 2464 2465 cq_spec.gdma_region = cq->gdma_cq->mem_info.dma_region_handle; 2466 cq_spec.queue_size = cq->gdma_cq->queue_size; 2467 cq_spec.modr_ctx_id = 0; 2468 cq_spec.attached_eq = cq->gdma_cq->cq.parent->id; 2469 2470 err = mana_create_wq_obj(apc, apc->port_handle, GDMA_RQ, 2471 &wq_spec, &cq_spec, &rxq->rxobj); 2472 if (err) 2473 goto out; 2474 2475 rxq->gdma_rq->id = wq_spec.queue_index; 2476 cq->gdma_cq->id = cq_spec.queue_index; 2477 2478 rxq->gdma_rq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION; 2479 cq->gdma_cq->mem_info.dma_region_handle = GDMA_INVALID_DMA_REGION; 2480 2481 rxq->gdma_id = rxq->gdma_rq->id; 2482 cq->gdma_id = cq->gdma_cq->id; 2483 2484 err = mana_push_wqe(rxq); 2485 if (err) 2486 goto out; 2487 2488 if (cq->gdma_id >= gc->max_num_cqs) { 2489 err = EINVAL; 2490 goto out; 2491 } 2492 2493 gc->cq_table[cq->gdma_id] = cq->gdma_cq; 2494 2495 /* Allocate and start the cleanup task on CQ */ 2496 cq->do_not_ring_db = false; 2497 2498 NET_TASK_INIT(&cq->cleanup_task, 0, mana_poll, cq); 2499 cq->cleanup_tq = 2500 taskqueue_create_fast("mana rx cq cleanup", 2501 M_WAITOK, taskqueue_thread_enqueue, 2502 &cq->cleanup_tq); 2503 2504 if (apc->last_rx_cq_bind_cpu < 0) 2505 apc->last_rx_cq_bind_cpu = CPU_FIRST(); 2506 cq->cpu = apc->last_rx_cq_bind_cpu; 2507 apc->last_rx_cq_bind_cpu = CPU_NEXT(apc->last_rx_cq_bind_cpu); 2508 2509 if (apc->bind_cleanup_thread_cpu) { 2510 cpuset_t cpu_mask; 2511 CPU_SETOF(cq->cpu, &cpu_mask); 2512 taskqueue_start_threads_cpuset(&cq->cleanup_tq, 2513 1, PI_NET, &cpu_mask, 2514 "mana cq p%u-rx%u-cpu%d", 2515 apc->port_idx, rxq->rxq_idx, cq->cpu); 2516 } else { 2517 taskqueue_start_threads(&cq->cleanup_tq, 1, 2518 PI_NET, "mana cq p%u-rx%u", 2519 apc->port_idx, rxq->rxq_idx); 2520 } 2521 2522 mana_gd_ring_cq(cq->gdma_cq, SET_ARM_BIT); 2523 out: 2524 if (!err) 2525 return rxq; 2526 2527 if_printf(ndev, "Failed to create RXQ: err = %d\n", err); 2528 2529 mana_destroy_rxq(apc, rxq, false); 2530 2531 if (cq) 2532 mana_deinit_cq(apc, cq); 2533 2534 return NULL; 2535 } 2536 2537 static int 2538 mana_add_rx_queues(struct mana_port_context *apc, if_t ndev) 2539 { 2540 struct mana_context *ac = apc->ac; 2541 struct mana_rxq *rxq; 2542 int err = 0; 2543 int i; 2544 2545 for (i = 0; i < apc->num_queues; i++) { 2546 rxq = mana_create_rxq(apc, i, &ac->eqs[i], ndev); 2547 if (!rxq) { 2548 err = ENOMEM; 2549 goto out; 2550 } 2551 2552 apc->rxqs[i] = rxq; 2553 } 2554 2555 apc->default_rxobj = apc->rxqs[0]->rxobj; 2556 out: 2557 return err; 2558 } 2559 2560 static void 2561 mana_destroy_vport(struct mana_port_context *apc) 2562 { 2563 struct mana_rxq *rxq; 2564 uint32_t rxq_idx; 2565 2566 for (rxq_idx = 0; rxq_idx < apc->num_queues; rxq_idx++) { 2567 rxq = apc->rxqs[rxq_idx]; 2568 if (!rxq) 2569 continue; 2570 2571 mana_destroy_rxq(apc, rxq, true); 2572 apc->rxqs[rxq_idx] = NULL; 2573 } 2574 2575 mana_destroy_txq(apc); 2576 2577 mana_uncfg_vport(apc); 2578 } 2579 2580 static int 2581 mana_create_vport(struct mana_port_context *apc, if_t net) 2582 { 2583 struct gdma_dev *gd = apc->ac->gdma_dev; 2584 int err; 2585 2586 apc->default_rxobj = INVALID_MANA_HANDLE; 2587 2588 err = mana_cfg_vport(apc, gd->pdid, gd->doorbell); 2589 if (err) 2590 return err; 2591 2592 return mana_create_txq(apc, net); 2593 } 2594 2595 2596 static void mana_rss_table_init(struct mana_port_context *apc) 2597 { 2598 int i; 2599 2600 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) 2601 apc->indir_table[i] = i % apc->num_queues; 2602 } 2603 2604 int mana_config_rss(struct mana_port_context *apc, enum TRI_STATE rx, 2605 bool update_hash, bool update_tab) 2606 { 2607 uint32_t queue_idx; 2608 int err; 2609 int i; 2610 2611 if (update_tab) { 2612 for (i = 0; i < MANA_INDIRECT_TABLE_SIZE; i++) { 2613 queue_idx = apc->indir_table[i]; 2614 apc->rxobj_table[i] = apc->rxqs[queue_idx]->rxobj; 2615 } 2616 } 2617 2618 err = mana_cfg_vport_steering(apc, rx, true, update_hash, update_tab); 2619 if (err) 2620 return err; 2621 2622 mana_fence_rqs(apc); 2623 2624 return 0; 2625 } 2626 2627 static int 2628 mana_init_port(if_t ndev) 2629 { 2630 struct mana_port_context *apc = if_getsoftc(ndev); 2631 uint32_t max_txq, max_rxq, max_queues; 2632 int port_idx = apc->port_idx; 2633 uint32_t num_indirect_entries; 2634 int err; 2635 2636 err = mana_init_port_context(apc); 2637 if (err) 2638 return err; 2639 2640 err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq, 2641 &num_indirect_entries); 2642 if (err) { 2643 if_printf(ndev, "Failed to query info for vPort %d\n", 2644 port_idx); 2645 goto reset_apc; 2646 } 2647 2648 max_queues = min_t(uint32_t, max_txq, max_rxq); 2649 if (apc->max_queues > max_queues) 2650 apc->max_queues = max_queues; 2651 2652 if (apc->num_queues > apc->max_queues) 2653 apc->num_queues = apc->max_queues; 2654 2655 return 0; 2656 2657 reset_apc: 2658 bus_dma_tag_destroy(apc->rx_buf_tag); 2659 apc->rx_buf_tag = NULL; 2660 free(apc->rxqs, M_DEVBUF); 2661 apc->rxqs = NULL; 2662 return err; 2663 } 2664 2665 int 2666 mana_alloc_queues(if_t ndev) 2667 { 2668 struct mana_port_context *apc = if_getsoftc(ndev); 2669 int err; 2670 2671 err = mana_create_vport(apc, ndev); 2672 if (err) 2673 return err; 2674 2675 err = mana_add_rx_queues(apc, ndev); 2676 if (err) 2677 goto destroy_vport; 2678 2679 apc->rss_state = apc->num_queues > 1 ? TRI_STATE_TRUE : TRI_STATE_FALSE; 2680 2681 mana_rss_table_init(apc); 2682 2683 err = mana_config_rss(apc, TRI_STATE_TRUE, true, true); 2684 if (err) 2685 goto destroy_vport; 2686 2687 return 0; 2688 2689 destroy_vport: 2690 mana_destroy_vport(apc); 2691 return err; 2692 } 2693 2694 static int 2695 mana_up(struct mana_port_context *apc) 2696 { 2697 int err; 2698 2699 mana_dbg(NULL, "mana_up called\n"); 2700 2701 err = mana_alloc_queues(apc->ndev); 2702 if (err) { 2703 mana_err(NULL, "Faile alloc mana queues: %d\n", err); 2704 return err; 2705 } 2706 2707 /* Add queue specific sysctl */ 2708 mana_sysctl_add_queues(apc); 2709 2710 apc->port_is_up = true; 2711 2712 /* Ensure port state updated before txq state */ 2713 wmb(); 2714 2715 if_link_state_change(apc->ndev, LINK_STATE_UP); 2716 if_setdrvflagbits(apc->ndev, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 2717 2718 return 0; 2719 } 2720 2721 2722 static void 2723 mana_init(void *arg) 2724 { 2725 struct mana_port_context *apc = (struct mana_port_context *)arg; 2726 2727 MANA_APC_LOCK_LOCK(apc); 2728 if (!apc->port_is_up) { 2729 mana_up(apc); 2730 } 2731 MANA_APC_LOCK_UNLOCK(apc); 2732 } 2733 2734 static int 2735 mana_dealloc_queues(if_t ndev) 2736 { 2737 struct mana_port_context *apc = if_getsoftc(ndev); 2738 struct mana_txq *txq; 2739 int i, err; 2740 2741 if (apc->port_is_up) 2742 return EINVAL; 2743 2744 /* No packet can be transmitted now since apc->port_is_up is false. 2745 * There is still a tiny chance that mana_poll_tx_cq() can re-enable 2746 * a txq because it may not timely see apc->port_is_up being cleared 2747 * to false, but it doesn't matter since mana_start_xmit() drops any 2748 * new packets due to apc->port_is_up being false. 2749 * 2750 * Drain all the in-flight TX packets 2751 */ 2752 for (i = 0; i < apc->num_queues; i++) { 2753 txq = &apc->tx_qp[i].txq; 2754 2755 struct mana_cq *tx_cq = &apc->tx_qp[i].tx_cq; 2756 struct mana_cq *rx_cq = &(apc->rxqs[i]->rx_cq); 2757 2758 tx_cq->do_not_ring_db = true; 2759 rx_cq->do_not_ring_db = true; 2760 2761 /* Schedule a cleanup task */ 2762 taskqueue_enqueue(tx_cq->cleanup_tq, &tx_cq->cleanup_task); 2763 2764 while (atomic_read(&txq->pending_sends) > 0) 2765 usleep_range(1000, 2000); 2766 } 2767 2768 /* We're 100% sure the queues can no longer be woken up, because 2769 * we're sure now mana_poll_tx_cq() can't be running. 2770 */ 2771 2772 apc->rss_state = TRI_STATE_FALSE; 2773 err = mana_config_rss(apc, TRI_STATE_FALSE, false, false); 2774 if (err) { 2775 if_printf(ndev, "Failed to disable vPort: %d\n", err); 2776 return err; 2777 } 2778 2779 mana_destroy_vport(apc); 2780 2781 return 0; 2782 } 2783 2784 static int 2785 mana_down(struct mana_port_context *apc) 2786 { 2787 int err = 0; 2788 2789 apc->port_st_save = apc->port_is_up; 2790 apc->port_is_up = false; 2791 2792 /* Ensure port state updated before txq state */ 2793 wmb(); 2794 2795 if (apc->port_st_save) { 2796 if_setdrvflagbits(apc->ndev, IFF_DRV_OACTIVE, 2797 IFF_DRV_RUNNING); 2798 if_link_state_change(apc->ndev, LINK_STATE_DOWN); 2799 2800 mana_sysctl_free_queues(apc); 2801 2802 err = mana_dealloc_queues(apc->ndev); 2803 if (err) { 2804 if_printf(apc->ndev, 2805 "Failed to bring down mana interface: %d\n", err); 2806 } 2807 } 2808 2809 return err; 2810 } 2811 2812 int 2813 mana_detach(if_t ndev) 2814 { 2815 struct mana_port_context *apc = if_getsoftc(ndev); 2816 int err; 2817 2818 ether_ifdetach(ndev); 2819 2820 if (!apc) 2821 return 0; 2822 2823 MANA_APC_LOCK_LOCK(apc); 2824 err = mana_down(apc); 2825 MANA_APC_LOCK_UNLOCK(apc); 2826 2827 mana_cleanup_port_context(apc); 2828 2829 MANA_APC_LOCK_DESTROY(apc); 2830 2831 free(apc, M_DEVBUF); 2832 2833 return err; 2834 } 2835 2836 static unsigned int 2837 mana_get_tx_queue_size(int port_idx, unsigned int request_size) 2838 { 2839 unsigned int new_size; 2840 2841 if (request_size == 0) 2842 /* Uninitialized */ 2843 new_size = DEF_SEND_BUFFERS_PER_QUEUE; 2844 else 2845 new_size = roundup_pow_of_two(request_size); 2846 2847 if (new_size < MIN_SEND_BUFFERS_PER_QUEUE || 2848 new_size > MAX_SEND_BUFFERS_PER_QUEUE) { 2849 mana_info(NULL, "mana port %d: requested tx buffer " 2850 "size %u out of allowable range (%u - %u), " 2851 "setting to default\n", 2852 port_idx, request_size, 2853 MIN_SEND_BUFFERS_PER_QUEUE, 2854 MAX_SEND_BUFFERS_PER_QUEUE); 2855 new_size = DEF_SEND_BUFFERS_PER_QUEUE; 2856 } 2857 mana_info(NULL, "mana port %d: tx buffer size %u " 2858 "(%u requested)\n", 2859 port_idx, new_size, request_size); 2860 2861 return (new_size); 2862 } 2863 2864 static unsigned int 2865 mana_get_rx_queue_size(int port_idx, unsigned int request_size) 2866 { 2867 unsigned int new_size; 2868 2869 if (request_size == 0) 2870 /* Uninitialized */ 2871 new_size = DEF_RX_BUFFERS_PER_QUEUE; 2872 else 2873 new_size = roundup_pow_of_two(request_size); 2874 2875 if (new_size < MIN_RX_BUFFERS_PER_QUEUE || 2876 new_size > MAX_RX_BUFFERS_PER_QUEUE) { 2877 mana_info(NULL, "mana port %d: requested rx buffer " 2878 "size %u out of allowable range (%u - %u), " 2879 "setting to default\n", 2880 port_idx, request_size, 2881 MIN_RX_BUFFERS_PER_QUEUE, 2882 MAX_RX_BUFFERS_PER_QUEUE); 2883 new_size = DEF_RX_BUFFERS_PER_QUEUE; 2884 } 2885 mana_info(NULL, "mana port %d: rx buffer size %u " 2886 "(%u requested)\n", 2887 port_idx, new_size, request_size); 2888 2889 return (new_size); 2890 } 2891 2892 static int 2893 mana_probe_port(struct mana_context *ac, int port_idx, 2894 if_t *ndev_storage) 2895 { 2896 struct gdma_context *gc = ac->gdma_dev->gdma_context; 2897 struct mana_port_context *apc; 2898 uint32_t hwassist; 2899 if_t ndev; 2900 int err; 2901 2902 ndev = if_alloc_dev(IFT_ETHER, gc->dev); 2903 *ndev_storage = ndev; 2904 2905 apc = malloc(sizeof(*apc), M_DEVBUF, M_WAITOK | M_ZERO); 2906 apc->ac = ac; 2907 apc->ndev = ndev; 2908 apc->max_queues = gc->max_num_queues; 2909 apc->num_queues = min_t(unsigned int, 2910 gc->max_num_queues, MANA_MAX_NUM_QUEUES); 2911 apc->tx_queue_size = mana_get_tx_queue_size(port_idx, 2912 mana_tx_req_size); 2913 apc->rx_queue_size = mana_get_rx_queue_size(port_idx, 2914 mana_rx_req_size); 2915 apc->port_handle = INVALID_MANA_HANDLE; 2916 apc->port_idx = port_idx; 2917 apc->frame_size = DEFAULT_FRAME_SIZE; 2918 apc->last_tx_cq_bind_cpu = -1; 2919 apc->last_rx_cq_bind_cpu = -1; 2920 apc->vport_use_count = 0; 2921 2922 MANA_APC_LOCK_INIT(apc); 2923 2924 if_initname(ndev, device_get_name(gc->dev), port_idx); 2925 if_setdev(ndev,gc->dev); 2926 if_setsoftc(ndev, apc); 2927 2928 if_setflags(ndev, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 2929 if_setinitfn(ndev, mana_init); 2930 if_settransmitfn(ndev, mana_start_xmit); 2931 if_setqflushfn(ndev, mana_qflush); 2932 if_setioctlfn(ndev, mana_ioctl); 2933 if_setgetcounterfn(ndev, mana_get_counter); 2934 2935 if_setmtu(ndev, ETHERMTU); 2936 if_setbaudrate(ndev, IF_Gbps(100)); 2937 2938 mana_rss_key_fill(apc->hashkey, MANA_HASH_KEY_SIZE); 2939 2940 err = mana_init_port(ndev); 2941 if (err) 2942 goto reset_apc; 2943 2944 if_setcapabilitiesbit(ndev, 2945 IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 | 2946 IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | 2947 IFCAP_TSO4 | IFCAP_TSO6 | 2948 IFCAP_LRO | IFCAP_LINKSTATE, 0); 2949 2950 /* Enable all available capabilities by default. */ 2951 if_setcapenable(ndev, if_getcapabilities(ndev)); 2952 2953 /* TSO parameters */ 2954 if_sethwtsomax(ndev, MANA_TSO_MAX_SZ - 2955 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)); 2956 if_sethwtsomaxsegcount(ndev, MAX_MBUF_FRAGS); 2957 if_sethwtsomaxsegsize(ndev, PAGE_SIZE); 2958 2959 hwassist = 0; 2960 if (if_getcapenable(ndev) & (IFCAP_TSO4 | IFCAP_TSO6)) 2961 hwassist |= CSUM_TSO; 2962 if (if_getcapenable(ndev) & IFCAP_TXCSUM) 2963 hwassist |= (CSUM_TCP | CSUM_UDP | CSUM_IP); 2964 if (if_getcapenable(ndev) & IFCAP_TXCSUM_IPV6) 2965 hwassist |= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6); 2966 mana_dbg(NULL, "set hwassist 0x%x\n", hwassist); 2967 if_sethwassist(ndev, hwassist); 2968 2969 ifmedia_init(&apc->media, IFM_IMASK, 2970 mana_ifmedia_change, mana_ifmedia_status); 2971 ifmedia_add(&apc->media, IFM_ETHER | IFM_AUTO, 0, NULL); 2972 ifmedia_set(&apc->media, IFM_ETHER | IFM_AUTO); 2973 2974 ether_ifattach(ndev, apc->mac_addr); 2975 2976 /* Initialize statistics */ 2977 mana_alloc_counters((counter_u64_t *)&apc->port_stats, 2978 sizeof(struct mana_port_stats)); 2979 mana_sysctl_add_port(apc); 2980 2981 /* Tell the stack that the interface is not active */ 2982 if_setdrvflagbits(ndev, IFF_DRV_OACTIVE, IFF_DRV_RUNNING); 2983 2984 return 0; 2985 2986 reset_apc: 2987 free(apc, M_DEVBUF); 2988 *ndev_storage = NULL; 2989 if_printf(ndev, "Failed to probe vPort %d: %d\n", port_idx, err); 2990 if_free(ndev); 2991 return err; 2992 } 2993 2994 int mana_probe(struct gdma_dev *gd) 2995 { 2996 struct gdma_context *gc = gd->gdma_context; 2997 device_t dev = gc->dev; 2998 struct mana_context *ac; 2999 int err; 3000 int i; 3001 3002 device_printf(dev, "%s protocol version: %d.%d.%d\n", DEVICE_NAME, 3003 MANA_MAJOR_VERSION, MANA_MINOR_VERSION, MANA_MICRO_VERSION); 3004 3005 err = mana_gd_register_device(gd); 3006 if (err) 3007 return err; 3008 3009 ac = malloc(sizeof(*ac), M_DEVBUF, M_WAITOK | M_ZERO); 3010 ac->gdma_dev = gd; 3011 ac->num_ports = 1; 3012 gd->driver_data = ac; 3013 3014 err = mana_create_eq(ac); 3015 if (err) 3016 goto out; 3017 3018 err = mana_query_device_cfg(ac, MANA_MAJOR_VERSION, MANA_MINOR_VERSION, 3019 MANA_MICRO_VERSION, &ac->num_ports); 3020 if (err) 3021 goto out; 3022 3023 if (ac->num_ports > MAX_PORTS_IN_MANA_DEV) 3024 ac->num_ports = MAX_PORTS_IN_MANA_DEV; 3025 3026 for (i = 0; i < ac->num_ports; i++) { 3027 err = mana_probe_port(ac, i, &ac->ports[i]); 3028 if (err) { 3029 device_printf(dev, 3030 "Failed to probe mana port %d\n", i); 3031 break; 3032 } 3033 } 3034 3035 out: 3036 if (err) 3037 mana_remove(gd); 3038 3039 return err; 3040 } 3041 3042 void 3043 mana_remove(struct gdma_dev *gd) 3044 { 3045 struct gdma_context *gc = gd->gdma_context; 3046 struct mana_context *ac = gd->driver_data; 3047 device_t dev = gc->dev; 3048 if_t ndev; 3049 int i; 3050 3051 for (i = 0; i < ac->num_ports; i++) { 3052 ndev = ac->ports[i]; 3053 if (!ndev) { 3054 if (i == 0) 3055 device_printf(dev, "No net device to remove\n"); 3056 goto out; 3057 } 3058 3059 mana_detach(ndev); 3060 3061 if_free(ndev); 3062 } 3063 3064 mana_destroy_eq(ac); 3065 3066 out: 3067 mana_gd_deregister_device(gd); 3068 gd->driver_data = NULL; 3069 gd->gdma_context = NULL; 3070 free(ac, M_DEVBUF); 3071 } 3072