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