1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */ 3 4 #include <linux/bitfield.h> 5 #include <linux/bpf.h> 6 #include <linux/bpf_trace.h> 7 #include <linux/iopoll.h> 8 #include <linux/pci.h> 9 #include <net/netdev_queues.h> 10 #include <net/page_pool/helpers.h> 11 #include <net/tcp.h> 12 #include <net/xdp.h> 13 14 #include "fbnic.h" 15 #include "fbnic_csr.h" 16 #include "fbnic_netdev.h" 17 #include "fbnic_txrx.h" 18 19 enum { 20 FBNIC_XDP_PASS = 0, 21 FBNIC_XDP_CONSUME, 22 FBNIC_XDP_TX, 23 FBNIC_XDP_LEN_ERR, 24 }; 25 26 enum { 27 FBNIC_XMIT_CB_TS = 0x01, 28 }; 29 30 struct fbnic_xmit_cb { 31 u32 bytecount; 32 u16 gso_segs; 33 u8 desc_count; 34 u8 flags; 35 int hw_head; 36 }; 37 38 #define FBNIC_XMIT_CB(__skb) ((struct fbnic_xmit_cb *)((__skb)->cb)) 39 40 static u32 __iomem *fbnic_ring_csr_base(const struct fbnic_ring *ring) 41 { 42 unsigned long csr_base = (unsigned long)ring->doorbell; 43 44 csr_base &= ~(FBNIC_QUEUE_STRIDE * sizeof(u32) - 1); 45 46 return (u32 __iomem *)csr_base; 47 } 48 49 static u32 fbnic_ring_rd32(struct fbnic_ring *ring, unsigned int csr) 50 { 51 u32 __iomem *csr_base = fbnic_ring_csr_base(ring); 52 53 return readl(csr_base + csr); 54 } 55 56 static void fbnic_ring_wr32(struct fbnic_ring *ring, unsigned int csr, u32 val) 57 { 58 u32 __iomem *csr_base = fbnic_ring_csr_base(ring); 59 60 writel(val, csr_base + csr); 61 } 62 63 /** 64 * fbnic_ts40_to_ns() - convert descriptor timestamp to PHC time 65 * @fbn: netdev priv of the FB NIC 66 * @ts40: timestamp read from a descriptor 67 * 68 * Return: u64 value of PHC time in nanoseconds 69 * 70 * Convert truncated 40 bit device timestamp as read from a descriptor 71 * to the full PHC time in nanoseconds. 72 */ 73 static __maybe_unused u64 fbnic_ts40_to_ns(struct fbnic_net *fbn, u64 ts40) 74 { 75 unsigned int s; 76 u64 time_ns; 77 s64 offset; 78 u8 ts_top; 79 u32 high; 80 81 do { 82 s = u64_stats_fetch_begin(&fbn->time_seq); 83 offset = READ_ONCE(fbn->time_offset); 84 } while (u64_stats_fetch_retry(&fbn->time_seq, s)); 85 86 high = READ_ONCE(fbn->time_high); 87 88 /* Bits 63..40 from periodic clock reads, 39..0 from ts40 */ 89 time_ns = (u64)(high >> 8) << 40 | ts40; 90 91 /* Compare bits 32-39 between periodic reads and ts40, 92 * see if HW clock may have wrapped since last read. We are sure 93 * that periodic reads are always at least ~1 minute behind, so 94 * this logic works perfectly fine. 95 */ 96 ts_top = ts40 >> 32; 97 if (ts_top < (u8)high && (u8)high - ts_top > U8_MAX / 2) 98 time_ns += 1ULL << 40; 99 100 return time_ns + offset; 101 } 102 103 static unsigned int fbnic_desc_unused(struct fbnic_ring *ring) 104 { 105 return (ring->head - ring->tail - 1) & ring->size_mask; 106 } 107 108 static unsigned int fbnic_desc_used(struct fbnic_ring *ring) 109 { 110 return (ring->tail - ring->head) & ring->size_mask; 111 } 112 113 static struct netdev_queue *txring_txq(const struct net_device *dev, 114 const struct fbnic_ring *ring) 115 { 116 return netdev_get_tx_queue(dev, ring->q_idx); 117 } 118 119 static int fbnic_maybe_stop_tx(const struct net_device *dev, 120 struct fbnic_ring *ring, 121 const unsigned int size) 122 { 123 struct netdev_queue *txq = txring_txq(dev, ring); 124 int res; 125 126 res = netif_txq_maybe_stop(txq, fbnic_desc_unused(ring), size, 127 FBNIC_TX_DESC_WAKEUP); 128 if (!res) { 129 u64_stats_update_begin(&ring->stats.syncp); 130 ring->stats.twq.stop++; 131 u64_stats_update_end(&ring->stats.syncp); 132 } 133 134 return !res; 135 } 136 137 static bool fbnic_tx_sent_queue(struct sk_buff *skb, struct fbnic_ring *ring) 138 { 139 struct netdev_queue *dev_queue = txring_txq(skb->dev, ring); 140 unsigned int bytecount = FBNIC_XMIT_CB(skb)->bytecount; 141 bool xmit_more = netdev_xmit_more(); 142 143 /* TBD: Request completion more often if xmit_more becomes large */ 144 145 return __netdev_tx_sent_queue(dev_queue, bytecount, xmit_more); 146 } 147 148 static void fbnic_unmap_single_twd(struct device *dev, __le64 *twd) 149 { 150 u64 raw_twd = le64_to_cpu(*twd); 151 unsigned int len; 152 dma_addr_t dma; 153 154 dma = FIELD_GET(FBNIC_TWD_ADDR_MASK, raw_twd); 155 len = FIELD_GET(FBNIC_TWD_LEN_MASK, raw_twd); 156 157 dma_unmap_single(dev, dma, len, DMA_TO_DEVICE); 158 } 159 160 static void fbnic_unmap_page_twd(struct device *dev, __le64 *twd) 161 { 162 u64 raw_twd = le64_to_cpu(*twd); 163 unsigned int len; 164 dma_addr_t dma; 165 166 dma = FIELD_GET(FBNIC_TWD_ADDR_MASK, raw_twd); 167 len = FIELD_GET(FBNIC_TWD_LEN_MASK, raw_twd); 168 169 dma_unmap_page(dev, dma, len, DMA_TO_DEVICE); 170 } 171 172 #define FBNIC_TWD_TYPE(_type) \ 173 cpu_to_le64(FIELD_PREP(FBNIC_TWD_TYPE_MASK, FBNIC_TWD_TYPE_##_type)) 174 175 static bool fbnic_tx_tstamp(struct sk_buff *skb) 176 { 177 struct fbnic_net *fbn; 178 179 if (!unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) 180 return false; 181 182 fbn = netdev_priv(skb->dev); 183 if (fbn->hwtstamp_config.tx_type == HWTSTAMP_TX_OFF) 184 return false; 185 186 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 187 FBNIC_XMIT_CB(skb)->flags |= FBNIC_XMIT_CB_TS; 188 FBNIC_XMIT_CB(skb)->hw_head = -1; 189 190 return true; 191 } 192 193 static bool 194 fbnic_tx_lso(struct fbnic_ring *ring, struct sk_buff *skb, 195 struct skb_shared_info *shinfo, __le64 *meta, 196 unsigned int *l2len, unsigned int *i3len) 197 { 198 unsigned int l3_type, l4_type, l4len, hdrlen; 199 unsigned char *l4hdr; 200 __be16 payload_len; 201 202 if (unlikely(skb_cow_head(skb, 0))) 203 return true; 204 205 if (shinfo->gso_type & SKB_GSO_PARTIAL) { 206 l3_type = FBNIC_TWD_L3_TYPE_OTHER; 207 } else if (!skb->encapsulation) { 208 if (ip_hdr(skb)->version == 4) 209 l3_type = FBNIC_TWD_L3_TYPE_IPV4; 210 else 211 l3_type = FBNIC_TWD_L3_TYPE_IPV6; 212 } else { 213 unsigned int o3len; 214 215 o3len = skb_inner_network_header(skb) - skb_network_header(skb); 216 *i3len -= o3len; 217 *meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_L3_OHLEN_MASK, 218 o3len / 2)); 219 l3_type = FBNIC_TWD_L3_TYPE_V6V6; 220 } 221 222 l4hdr = skb_checksum_start(skb); 223 payload_len = cpu_to_be16(skb->len - (l4hdr - skb->data)); 224 225 if (shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) { 226 struct tcphdr *tcph = (struct tcphdr *)l4hdr; 227 228 l4_type = FBNIC_TWD_L4_TYPE_TCP; 229 l4len = __tcp_hdrlen((struct tcphdr *)l4hdr); 230 csum_replace_by_diff(&tcph->check, (__force __wsum)payload_len); 231 } else { 232 struct udphdr *udph = (struct udphdr *)l4hdr; 233 234 l4_type = FBNIC_TWD_L4_TYPE_UDP; 235 l4len = sizeof(struct udphdr); 236 csum_replace_by_diff(&udph->check, (__force __wsum)payload_len); 237 } 238 239 hdrlen = (l4hdr - skb->data) + l4len; 240 *meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_L3_TYPE_MASK, l3_type) | 241 FIELD_PREP(FBNIC_TWD_L4_TYPE_MASK, l4_type) | 242 FIELD_PREP(FBNIC_TWD_L4_HLEN_MASK, l4len / 4) | 243 FIELD_PREP(FBNIC_TWD_MSS_MASK, shinfo->gso_size) | 244 FBNIC_TWD_FLAG_REQ_LSO); 245 246 FBNIC_XMIT_CB(skb)->bytecount += (shinfo->gso_segs - 1) * hdrlen; 247 FBNIC_XMIT_CB(skb)->gso_segs = shinfo->gso_segs; 248 249 u64_stats_update_begin(&ring->stats.syncp); 250 ring->stats.twq.lso += shinfo->gso_segs; 251 u64_stats_update_end(&ring->stats.syncp); 252 253 return false; 254 } 255 256 static bool 257 fbnic_tx_offloads(struct fbnic_ring *ring, struct sk_buff *skb, __le64 *meta) 258 { 259 struct skb_shared_info *shinfo = skb_shinfo(skb); 260 unsigned int l2len, i3len; 261 262 if (fbnic_tx_tstamp(skb)) 263 *meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_TS); 264 265 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) 266 return false; 267 268 l2len = skb_mac_header_len(skb); 269 i3len = skb_checksum_start(skb) - skb_network_header(skb); 270 271 *meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_CSUM_OFFSET_MASK, 272 skb->csum_offset / 2)); 273 274 if (shinfo->gso_size) { 275 if (fbnic_tx_lso(ring, skb, shinfo, meta, &l2len, &i3len)) 276 return true; 277 } else { 278 *meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_CSO); 279 u64_stats_update_begin(&ring->stats.syncp); 280 ring->stats.twq.csum_partial++; 281 u64_stats_update_end(&ring->stats.syncp); 282 } 283 284 *meta |= cpu_to_le64(FIELD_PREP(FBNIC_TWD_L2_HLEN_MASK, l2len / 2) | 285 FIELD_PREP(FBNIC_TWD_L3_IHLEN_MASK, i3len / 2)); 286 return false; 287 } 288 289 static void 290 fbnic_rx_csum(u64 rcd, struct sk_buff *skb, struct fbnic_ring *rcq, 291 u64 *csum_cmpl, u64 *csum_none) 292 { 293 skb_checksum_none_assert(skb); 294 295 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) { 296 (*csum_none)++; 297 return; 298 } 299 300 if (FIELD_GET(FBNIC_RCD_META_L4_CSUM_UNNECESSARY, rcd)) { 301 skb->ip_summed = CHECKSUM_UNNECESSARY; 302 } else { 303 u16 csum = FIELD_GET(FBNIC_RCD_META_L2_CSUM_MASK, rcd); 304 305 skb->ip_summed = CHECKSUM_COMPLETE; 306 skb->csum = (__force __wsum)csum; 307 (*csum_cmpl)++; 308 } 309 } 310 311 static bool 312 fbnic_tx_map(struct fbnic_ring *ring, struct sk_buff *skb, __le64 *meta) 313 { 314 struct device *dev = skb->dev->dev.parent; 315 unsigned int tail = ring->tail, first; 316 unsigned int size, data_len; 317 skb_frag_t *frag; 318 dma_addr_t dma; 319 __le64 *twd; 320 321 ring->tx_buf[tail] = skb; 322 323 tail++; 324 tail &= ring->size_mask; 325 first = tail; 326 327 size = skb_headlen(skb); 328 data_len = skb->data_len; 329 330 if (size > FIELD_MAX(FBNIC_TWD_LEN_MASK)) 331 goto dma_error; 332 333 dma = dma_map_single(dev, skb->data, size, DMA_TO_DEVICE); 334 335 for (frag = &skb_shinfo(skb)->frags[0];; frag++) { 336 twd = &ring->desc[tail]; 337 338 if (dma_mapping_error(dev, dma)) 339 goto dma_error; 340 341 *twd = cpu_to_le64(FIELD_PREP(FBNIC_TWD_ADDR_MASK, dma) | 342 FIELD_PREP(FBNIC_TWD_LEN_MASK, size) | 343 FIELD_PREP(FBNIC_TWD_TYPE_MASK, 344 FBNIC_TWD_TYPE_AL)); 345 346 tail++; 347 tail &= ring->size_mask; 348 349 if (!data_len) 350 break; 351 352 size = skb_frag_size(frag); 353 data_len -= size; 354 355 if (size > FIELD_MAX(FBNIC_TWD_LEN_MASK)) 356 goto dma_error; 357 358 dma = skb_frag_dma_map(dev, frag, 0, size, DMA_TO_DEVICE); 359 } 360 361 *twd |= FBNIC_TWD_TYPE(LAST_AL); 362 363 FBNIC_XMIT_CB(skb)->desc_count = ((twd - meta) + 1) & ring->size_mask; 364 365 ring->tail = tail; 366 367 /* Record SW timestamp */ 368 skb_tx_timestamp(skb); 369 370 /* Verify there is room for another packet */ 371 fbnic_maybe_stop_tx(skb->dev, ring, FBNIC_MAX_SKB_DESC); 372 373 if (fbnic_tx_sent_queue(skb, ring)) { 374 *meta |= cpu_to_le64(FBNIC_TWD_FLAG_REQ_COMPLETION); 375 376 /* Force DMA writes to flush before writing to tail */ 377 dma_wmb(); 378 379 writel(tail, ring->doorbell); 380 } 381 382 return false; 383 dma_error: 384 if (net_ratelimit()) 385 netdev_err(skb->dev, "TX DMA map failed\n"); 386 387 while (tail != first) { 388 tail--; 389 tail &= ring->size_mask; 390 twd = &ring->desc[tail]; 391 if (tail == first) 392 fbnic_unmap_single_twd(dev, twd); 393 else 394 fbnic_unmap_page_twd(dev, twd); 395 } 396 397 return true; 398 } 399 400 #define FBNIC_MIN_FRAME_LEN 60 401 402 static netdev_tx_t 403 fbnic_xmit_frame_ring(struct sk_buff *skb, struct fbnic_ring *ring) 404 { 405 __le64 *meta = &ring->desc[ring->tail]; 406 u16 desc_needed; 407 408 if (skb_put_padto(skb, FBNIC_MIN_FRAME_LEN)) 409 goto err_count; 410 411 /* Need: 1 descriptor per page, 412 * + 1 desc for skb_head, 413 * + 2 desc for metadata and timestamp metadata 414 * + 7 desc gap to keep tail from touching head 415 * otherwise try next time 416 */ 417 desc_needed = skb_shinfo(skb)->nr_frags + 10; 418 if (fbnic_maybe_stop_tx(skb->dev, ring, desc_needed)) 419 return NETDEV_TX_BUSY; 420 421 *meta = cpu_to_le64(FBNIC_TWD_FLAG_DEST_MAC); 422 423 /* Write all members within DWORD to condense this into 2 4B writes */ 424 FBNIC_XMIT_CB(skb)->bytecount = skb->len; 425 FBNIC_XMIT_CB(skb)->gso_segs = 1; 426 FBNIC_XMIT_CB(skb)->desc_count = 0; 427 FBNIC_XMIT_CB(skb)->flags = 0; 428 429 if (fbnic_tx_offloads(ring, skb, meta)) 430 goto err_free; 431 432 if (fbnic_tx_map(ring, skb, meta)) 433 goto err_free; 434 435 return NETDEV_TX_OK; 436 437 err_free: 438 dev_kfree_skb_any(skb); 439 err_count: 440 u64_stats_update_begin(&ring->stats.syncp); 441 ring->stats.dropped++; 442 u64_stats_update_end(&ring->stats.syncp); 443 return NETDEV_TX_OK; 444 } 445 446 netdev_tx_t fbnic_xmit_frame(struct sk_buff *skb, struct net_device *dev) 447 { 448 struct fbnic_net *fbn = netdev_priv(dev); 449 unsigned int q_map = skb->queue_mapping; 450 451 return fbnic_xmit_frame_ring(skb, fbn->tx[q_map]); 452 } 453 454 static netdev_features_t 455 fbnic_features_check_encap_gso(struct sk_buff *skb, struct net_device *dev, 456 netdev_features_t features, unsigned int l3len) 457 { 458 netdev_features_t skb_gso_features; 459 struct ipv6hdr *ip6_hdr; 460 unsigned char l4_hdr; 461 unsigned int start; 462 __be16 frag_off; 463 464 /* Require MANGLEID for GSO_PARTIAL of IPv4. 465 * In theory we could support TSO with single, innermost v4 header 466 * by pretending everything before it is L2, but that needs to be 467 * parsed case by case.. so leaving it for when the need arises. 468 */ 469 if (!(features & NETIF_F_TSO_MANGLEID)) 470 features &= ~NETIF_F_TSO; 471 472 skb_gso_features = skb_shinfo(skb)->gso_type; 473 skb_gso_features <<= NETIF_F_GSO_SHIFT; 474 475 /* We'd only clear the native GSO features, so don't bother validating 476 * if the match can only be on those supported thru GSO_PARTIAL. 477 */ 478 if (!(skb_gso_features & FBNIC_TUN_GSO_FEATURES)) 479 return features; 480 481 /* We can only do IPv6-in-IPv6, not v4-in-v6. It'd be nice 482 * to fall back to partial for this, or any failure below. 483 * This is just an optimization, UDPv4 will be caught later on. 484 */ 485 if (skb_gso_features & NETIF_F_TSO) 486 return features & ~FBNIC_TUN_GSO_FEATURES; 487 488 /* Inner headers multiple of 2 */ 489 if ((skb_inner_network_header(skb) - skb_network_header(skb)) % 2) 490 return features & ~FBNIC_TUN_GSO_FEATURES; 491 492 /* Encapsulated GSO packet, make 100% sure it's IPv6-in-IPv6. */ 493 ip6_hdr = ipv6_hdr(skb); 494 if (ip6_hdr->version != 6) 495 return features & ~FBNIC_TUN_GSO_FEATURES; 496 497 l4_hdr = ip6_hdr->nexthdr; 498 start = (unsigned char *)ip6_hdr - skb->data + sizeof(struct ipv6hdr); 499 start = ipv6_skip_exthdr(skb, start, &l4_hdr, &frag_off); 500 if (frag_off || l4_hdr != IPPROTO_IPV6 || 501 skb->data + start != skb_inner_network_header(skb)) 502 return features & ~FBNIC_TUN_GSO_FEATURES; 503 504 return features; 505 } 506 507 netdev_features_t 508 fbnic_features_check(struct sk_buff *skb, struct net_device *dev, 509 netdev_features_t features) 510 { 511 unsigned int l2len, l3len; 512 513 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) 514 return features; 515 516 l2len = skb_mac_header_len(skb); 517 l3len = skb_checksum_start(skb) - skb_network_header(skb); 518 519 /* Check header lengths are multiple of 2. 520 * In case of 6in6 we support longer headers (IHLEN + OHLEN) 521 * but keep things simple for now, 512B is plenty. 522 */ 523 if ((l2len | l3len | skb->csum_offset) % 2 || 524 !FIELD_FIT(FBNIC_TWD_L2_HLEN_MASK, l2len / 2) || 525 !FIELD_FIT(FBNIC_TWD_L3_IHLEN_MASK, l3len / 2) || 526 !FIELD_FIT(FBNIC_TWD_CSUM_OFFSET_MASK, skb->csum_offset / 2)) 527 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 528 529 if (likely(!skb->encapsulation) || !skb_is_gso(skb)) 530 return features; 531 532 return fbnic_features_check_encap_gso(skb, dev, features, l3len); 533 } 534 535 static void fbnic_clean_twq0(struct fbnic_napi_vector *nv, int napi_budget, 536 struct fbnic_ring *ring, bool discard, 537 unsigned int hw_head) 538 { 539 u64 total_bytes = 0, total_packets = 0, ts_lost = 0; 540 unsigned int head = ring->head; 541 struct netdev_queue *txq; 542 unsigned int clean_desc; 543 544 clean_desc = (hw_head - head) & ring->size_mask; 545 546 while (clean_desc) { 547 struct sk_buff *skb = ring->tx_buf[head]; 548 unsigned int desc_cnt; 549 550 desc_cnt = FBNIC_XMIT_CB(skb)->desc_count; 551 if (desc_cnt > clean_desc) 552 break; 553 554 if (unlikely(FBNIC_XMIT_CB(skb)->flags & FBNIC_XMIT_CB_TS)) { 555 FBNIC_XMIT_CB(skb)->hw_head = hw_head; 556 if (likely(!discard)) 557 break; 558 ts_lost++; 559 } 560 561 ring->tx_buf[head] = NULL; 562 563 clean_desc -= desc_cnt; 564 565 while (!(ring->desc[head] & FBNIC_TWD_TYPE(AL))) { 566 head++; 567 head &= ring->size_mask; 568 desc_cnt--; 569 } 570 571 fbnic_unmap_single_twd(nv->dev, &ring->desc[head]); 572 head++; 573 head &= ring->size_mask; 574 desc_cnt--; 575 576 while (desc_cnt--) { 577 fbnic_unmap_page_twd(nv->dev, &ring->desc[head]); 578 head++; 579 head &= ring->size_mask; 580 } 581 582 total_bytes += FBNIC_XMIT_CB(skb)->bytecount; 583 total_packets += FBNIC_XMIT_CB(skb)->gso_segs; 584 585 napi_consume_skb(skb, napi_budget); 586 } 587 588 if (!total_bytes) 589 return; 590 591 ring->head = head; 592 593 txq = txring_txq(nv->napi.dev, ring); 594 595 if (unlikely(discard)) { 596 u64_stats_update_begin(&ring->stats.syncp); 597 ring->stats.dropped += total_packets; 598 ring->stats.twq.ts_lost += ts_lost; 599 u64_stats_update_end(&ring->stats.syncp); 600 601 netdev_tx_completed_queue(txq, total_packets, total_bytes); 602 return; 603 } 604 605 u64_stats_update_begin(&ring->stats.syncp); 606 ring->stats.bytes += total_bytes; 607 ring->stats.packets += total_packets; 608 u64_stats_update_end(&ring->stats.syncp); 609 610 if (!netif_txq_completed_wake(txq, total_packets, total_bytes, 611 fbnic_desc_unused(ring), 612 FBNIC_TX_DESC_WAKEUP)) { 613 u64_stats_update_begin(&ring->stats.syncp); 614 ring->stats.twq.wake++; 615 u64_stats_update_end(&ring->stats.syncp); 616 } 617 } 618 619 static void fbnic_clean_twq1(struct fbnic_napi_vector *nv, bool pp_allow_direct, 620 struct fbnic_ring *ring, bool discard, 621 unsigned int hw_head) 622 { 623 u64 total_bytes = 0, total_packets = 0; 624 unsigned int head = ring->head; 625 626 while (hw_head != head) { 627 struct page *page; 628 u64 twd; 629 630 if (unlikely(!(ring->desc[head] & FBNIC_TWD_TYPE(AL)))) 631 goto next_desc; 632 633 twd = le64_to_cpu(ring->desc[head]); 634 page = ring->tx_buf[head]; 635 636 /* TYPE_AL is 2, TYPE_LAST_AL is 3. So this trick gives 637 * us one increment per packet, with no branches. 638 */ 639 total_packets += FIELD_GET(FBNIC_TWD_TYPE_MASK, twd) - 640 FBNIC_TWD_TYPE_AL; 641 total_bytes += FIELD_GET(FBNIC_TWD_LEN_MASK, twd); 642 643 page_pool_put_page(page->pp, page, -1, pp_allow_direct); 644 next_desc: 645 head++; 646 head &= ring->size_mask; 647 } 648 649 if (!total_bytes) 650 return; 651 652 ring->head = head; 653 654 if (discard) { 655 u64_stats_update_begin(&ring->stats.syncp); 656 ring->stats.dropped += total_packets; 657 u64_stats_update_end(&ring->stats.syncp); 658 return; 659 } 660 661 u64_stats_update_begin(&ring->stats.syncp); 662 ring->stats.bytes += total_bytes; 663 ring->stats.packets += total_packets; 664 u64_stats_update_end(&ring->stats.syncp); 665 } 666 667 static void fbnic_clean_tsq(struct fbnic_napi_vector *nv, 668 struct fbnic_ring *ring, 669 u64 tcd, int *ts_head, int *head0) 670 { 671 struct skb_shared_hwtstamps hwtstamp; 672 struct fbnic_net *fbn; 673 struct sk_buff *skb; 674 int head; 675 u64 ns; 676 677 head = (*ts_head < 0) ? ring->head : *ts_head; 678 679 do { 680 unsigned int desc_cnt; 681 682 if (head == ring->tail) { 683 if (unlikely(net_ratelimit())) 684 netdev_err(nv->napi.dev, 685 "Tx timestamp without matching packet\n"); 686 return; 687 } 688 689 skb = ring->tx_buf[head]; 690 desc_cnt = FBNIC_XMIT_CB(skb)->desc_count; 691 692 head += desc_cnt; 693 head &= ring->size_mask; 694 } while (!(FBNIC_XMIT_CB(skb)->flags & FBNIC_XMIT_CB_TS)); 695 696 fbn = netdev_priv(nv->napi.dev); 697 ns = fbnic_ts40_to_ns(fbn, FIELD_GET(FBNIC_TCD_TYPE1_TS_MASK, tcd)); 698 699 memset(&hwtstamp, 0, sizeof(hwtstamp)); 700 hwtstamp.hwtstamp = ns_to_ktime(ns); 701 702 *ts_head = head; 703 704 FBNIC_XMIT_CB(skb)->flags &= ~FBNIC_XMIT_CB_TS; 705 if (*head0 < 0) { 706 head = FBNIC_XMIT_CB(skb)->hw_head; 707 if (head >= 0) 708 *head0 = head; 709 } 710 711 skb_tstamp_tx(skb, &hwtstamp); 712 u64_stats_update_begin(&ring->stats.syncp); 713 ring->stats.twq.ts_packets++; 714 u64_stats_update_end(&ring->stats.syncp); 715 } 716 717 static void fbnic_page_pool_init(struct fbnic_ring *ring, unsigned int idx, 718 netmem_ref netmem) 719 { 720 struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx]; 721 722 page_pool_fragment_netmem(netmem, FBNIC_PAGECNT_BIAS_MAX); 723 rx_buf->pagecnt_bias = FBNIC_PAGECNT_BIAS_MAX; 724 rx_buf->netmem = netmem; 725 } 726 727 static struct page * 728 fbnic_page_pool_get_head(struct fbnic_q_triad *qt, unsigned int idx) 729 { 730 struct fbnic_rx_buf *rx_buf = &qt->sub0.rx_buf[idx]; 731 732 rx_buf->pagecnt_bias--; 733 734 /* sub0 is always fed system pages, from the NAPI-level page_pool */ 735 return netmem_to_page(rx_buf->netmem); 736 } 737 738 static netmem_ref 739 fbnic_page_pool_get_data(struct fbnic_q_triad *qt, unsigned int idx) 740 { 741 struct fbnic_rx_buf *rx_buf = &qt->sub1.rx_buf[idx]; 742 743 rx_buf->pagecnt_bias--; 744 745 return rx_buf->netmem; 746 } 747 748 static void fbnic_page_pool_drain(struct fbnic_ring *ring, unsigned int idx, 749 int budget) 750 { 751 struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx]; 752 netmem_ref netmem = rx_buf->netmem; 753 754 if (!page_pool_unref_netmem(netmem, rx_buf->pagecnt_bias)) 755 page_pool_put_unrefed_netmem(ring->page_pool, netmem, -1, 756 !!budget); 757 758 rx_buf->netmem = 0; 759 } 760 761 static void fbnic_clean_twq(struct fbnic_napi_vector *nv, int napi_budget, 762 struct fbnic_q_triad *qt, s32 ts_head, s32 head0, 763 s32 head1) 764 { 765 if (head0 >= 0) 766 fbnic_clean_twq0(nv, napi_budget, &qt->sub0, false, head0); 767 else if (ts_head >= 0) 768 fbnic_clean_twq0(nv, napi_budget, &qt->sub0, false, ts_head); 769 770 if (head1 >= 0) { 771 qt->cmpl.deferred_head = -1; 772 if (napi_budget) 773 fbnic_clean_twq1(nv, true, &qt->sub1, false, head1); 774 else 775 qt->cmpl.deferred_head = head1; 776 } 777 } 778 779 static void 780 fbnic_clean_tcq(struct fbnic_napi_vector *nv, struct fbnic_q_triad *qt, 781 int napi_budget) 782 { 783 struct fbnic_ring *cmpl = &qt->cmpl; 784 s32 head1 = cmpl->deferred_head; 785 s32 head0 = -1, ts_head = -1; 786 __le64 *raw_tcd, done; 787 u32 head = cmpl->head; 788 789 done = (head & (cmpl->size_mask + 1)) ? 0 : cpu_to_le64(FBNIC_TCD_DONE); 790 raw_tcd = &cmpl->desc[head & cmpl->size_mask]; 791 792 /* Walk the completion queue collecting the heads reported by NIC */ 793 while ((*raw_tcd & cpu_to_le64(FBNIC_TCD_DONE)) == done) { 794 u64 tcd; 795 796 dma_rmb(); 797 798 tcd = le64_to_cpu(*raw_tcd); 799 800 switch (FIELD_GET(FBNIC_TCD_TYPE_MASK, tcd)) { 801 case FBNIC_TCD_TYPE_0: 802 if (tcd & FBNIC_TCD_TWQ1) 803 head1 = FIELD_GET(FBNIC_TCD_TYPE0_HEAD1_MASK, 804 tcd); 805 else 806 head0 = FIELD_GET(FBNIC_TCD_TYPE0_HEAD0_MASK, 807 tcd); 808 /* Currently all err status bits are related to 809 * timestamps and as those have yet to be added 810 * they are skipped for now. 811 */ 812 break; 813 case FBNIC_TCD_TYPE_1: 814 if (WARN_ON_ONCE(tcd & FBNIC_TCD_TWQ1)) 815 break; 816 817 fbnic_clean_tsq(nv, &qt->sub0, tcd, &ts_head, &head0); 818 break; 819 default: 820 break; 821 } 822 823 raw_tcd++; 824 head++; 825 if (!(head & cmpl->size_mask)) { 826 done ^= cpu_to_le64(FBNIC_TCD_DONE); 827 raw_tcd = &cmpl->desc[0]; 828 } 829 } 830 831 /* Record the current head/tail of the queue */ 832 if (cmpl->head != head) { 833 cmpl->head = head; 834 writel(head & cmpl->size_mask, cmpl->doorbell); 835 } 836 837 /* Unmap and free processed buffers */ 838 fbnic_clean_twq(nv, napi_budget, qt, ts_head, head0, head1); 839 } 840 841 static void fbnic_clean_bdq(struct fbnic_ring *ring, unsigned int hw_head, 842 int napi_budget) 843 { 844 unsigned int head = ring->head; 845 846 if (head == hw_head) 847 return; 848 849 do { 850 fbnic_page_pool_drain(ring, head, napi_budget); 851 852 head++; 853 head &= ring->size_mask; 854 } while (head != hw_head); 855 856 ring->head = head; 857 } 858 859 static void fbnic_bd_prep(struct fbnic_ring *bdq, u16 id, netmem_ref netmem) 860 { 861 __le64 *bdq_desc = &bdq->desc[id * FBNIC_BD_FRAG_COUNT]; 862 dma_addr_t dma = page_pool_get_dma_addr_netmem(netmem); 863 u64 bd, i = FBNIC_BD_FRAG_COUNT; 864 865 bd = (FBNIC_BD_PAGE_ADDR_MASK & dma) | 866 FIELD_PREP(FBNIC_BD_PAGE_ID_MASK, id); 867 868 /* In the case that a page size is larger than 4K we will map a 869 * single page to multiple fragments. The fragments will be 870 * FBNIC_BD_FRAG_COUNT in size and the lower n bits will be use 871 * to indicate the individual fragment IDs. 872 */ 873 do { 874 *bdq_desc = cpu_to_le64(bd); 875 bd += FIELD_PREP(FBNIC_BD_DESC_ADDR_MASK, 1) | 876 FIELD_PREP(FBNIC_BD_DESC_ID_MASK, 1); 877 } while (--i); 878 } 879 880 static void fbnic_fill_bdq(struct fbnic_ring *bdq) 881 { 882 unsigned int count = fbnic_desc_unused(bdq); 883 unsigned int i = bdq->tail; 884 885 if (!count) 886 return; 887 888 do { 889 netmem_ref netmem; 890 891 netmem = page_pool_dev_alloc_netmems(bdq->page_pool); 892 if (!netmem) { 893 u64_stats_update_begin(&bdq->stats.syncp); 894 bdq->stats.rx.alloc_failed++; 895 u64_stats_update_end(&bdq->stats.syncp); 896 897 break; 898 } 899 900 fbnic_page_pool_init(bdq, i, netmem); 901 fbnic_bd_prep(bdq, i, netmem); 902 903 i++; 904 i &= bdq->size_mask; 905 906 count--; 907 } while (count); 908 909 if (bdq->tail != i) { 910 bdq->tail = i; 911 912 /* Force DMA writes to flush before writing to tail */ 913 dma_wmb(); 914 915 writel(i, bdq->doorbell); 916 } 917 } 918 919 static unsigned int fbnic_hdr_pg_start(unsigned int pg_off) 920 { 921 /* The headroom of the first header may be larger than FBNIC_RX_HROOM 922 * due to alignment. So account for that by just making the page 923 * offset 0 if we are starting at the first header. 924 */ 925 if (ALIGN(FBNIC_RX_HROOM, 128) > FBNIC_RX_HROOM && 926 pg_off == ALIGN(FBNIC_RX_HROOM, 128)) 927 return 0; 928 929 return pg_off - FBNIC_RX_HROOM; 930 } 931 932 static unsigned int fbnic_hdr_pg_end(unsigned int pg_off, unsigned int len) 933 { 934 /* Determine the end of the buffer by finding the start of the next 935 * and then subtracting the headroom from that frame. 936 */ 937 pg_off += len + FBNIC_RX_TROOM + FBNIC_RX_HROOM; 938 939 return ALIGN(pg_off, 128) - FBNIC_RX_HROOM; 940 } 941 942 static void fbnic_pkt_prepare(struct fbnic_napi_vector *nv, u64 rcd, 943 struct fbnic_pkt_buff *pkt, 944 struct fbnic_q_triad *qt) 945 { 946 unsigned int hdr_pg_idx = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd); 947 unsigned int hdr_pg_off = FIELD_GET(FBNIC_RCD_AL_BUFF_OFF_MASK, rcd); 948 struct page *page = fbnic_page_pool_get_head(qt, hdr_pg_idx); 949 unsigned int len = FIELD_GET(FBNIC_RCD_AL_BUFF_LEN_MASK, rcd); 950 unsigned int frame_sz, hdr_pg_start, hdr_pg_end, headroom; 951 unsigned char *hdr_start; 952 953 /* data_hard_start should always be NULL when this is called */ 954 WARN_ON_ONCE(pkt->buff.data_hard_start); 955 956 /* Short-cut the end calculation if we know page is fully consumed */ 957 hdr_pg_end = FIELD_GET(FBNIC_RCD_AL_PAGE_FIN, rcd) ? 958 FBNIC_BD_FRAG_SIZE : fbnic_hdr_pg_end(hdr_pg_off, len); 959 hdr_pg_start = fbnic_hdr_pg_start(hdr_pg_off); 960 961 headroom = hdr_pg_off - hdr_pg_start + FBNIC_RX_PAD; 962 frame_sz = hdr_pg_end - hdr_pg_start; 963 xdp_init_buff(&pkt->buff, frame_sz, &qt->xdp_rxq); 964 hdr_pg_start += (FBNIC_RCD_AL_BUFF_FRAG_MASK & rcd) * 965 FBNIC_BD_FRAG_SIZE; 966 967 /* Sync DMA buffer */ 968 dma_sync_single_range_for_cpu(nv->dev, page_pool_get_dma_addr(page), 969 hdr_pg_start, frame_sz, 970 DMA_BIDIRECTIONAL); 971 972 /* Build frame around buffer */ 973 hdr_start = page_address(page) + hdr_pg_start; 974 net_prefetch(pkt->buff.data); 975 xdp_prepare_buff(&pkt->buff, hdr_start, headroom, 976 len - FBNIC_RX_PAD, true); 977 978 pkt->hwtstamp = 0; 979 pkt->add_frag_failed = false; 980 } 981 982 static void fbnic_add_rx_frag(struct fbnic_napi_vector *nv, u64 rcd, 983 struct fbnic_pkt_buff *pkt, 984 struct fbnic_q_triad *qt) 985 { 986 unsigned int pg_idx = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd); 987 unsigned int pg_off = FIELD_GET(FBNIC_RCD_AL_BUFF_OFF_MASK, rcd); 988 unsigned int len = FIELD_GET(FBNIC_RCD_AL_BUFF_LEN_MASK, rcd); 989 netmem_ref netmem = fbnic_page_pool_get_data(qt, pg_idx); 990 unsigned int truesize; 991 bool added; 992 993 truesize = FIELD_GET(FBNIC_RCD_AL_PAGE_FIN, rcd) ? 994 FBNIC_BD_FRAG_SIZE - pg_off : ALIGN(len, 128); 995 996 pg_off += (FBNIC_RCD_AL_BUFF_FRAG_MASK & rcd) * 997 FBNIC_BD_FRAG_SIZE; 998 999 /* Sync DMA buffer */ 1000 page_pool_dma_sync_netmem_for_cpu(qt->sub1.page_pool, netmem, 1001 pg_off, truesize); 1002 1003 added = xdp_buff_add_frag(&pkt->buff, netmem, pg_off, len, truesize); 1004 if (unlikely(!added)) { 1005 pkt->add_frag_failed = true; 1006 netdev_err_once(nv->napi.dev, 1007 "Failed to add fragment to xdp_buff\n"); 1008 } 1009 } 1010 1011 static void fbnic_put_pkt_buff(struct fbnic_q_triad *qt, 1012 struct fbnic_pkt_buff *pkt, int budget) 1013 { 1014 struct page *page; 1015 1016 if (!pkt->buff.data_hard_start) 1017 return; 1018 1019 if (xdp_buff_has_frags(&pkt->buff)) { 1020 struct skb_shared_info *shinfo; 1021 netmem_ref netmem; 1022 int nr_frags; 1023 1024 shinfo = xdp_get_shared_info_from_buff(&pkt->buff); 1025 nr_frags = shinfo->nr_frags; 1026 1027 while (nr_frags--) { 1028 netmem = skb_frag_netmem(&shinfo->frags[nr_frags]); 1029 page_pool_put_full_netmem(qt->sub1.page_pool, netmem, 1030 !!budget); 1031 } 1032 } 1033 1034 page = virt_to_page(pkt->buff.data_hard_start); 1035 page_pool_put_full_page(qt->sub0.page_pool, page, !!budget); 1036 } 1037 1038 static struct sk_buff *fbnic_build_skb(struct fbnic_napi_vector *nv, 1039 struct fbnic_pkt_buff *pkt) 1040 { 1041 struct sk_buff *skb; 1042 1043 skb = xdp_build_skb_from_buff(&pkt->buff); 1044 if (!skb) 1045 return NULL; 1046 1047 /* Add timestamp if present */ 1048 if (pkt->hwtstamp) 1049 skb_hwtstamps(skb)->hwtstamp = pkt->hwtstamp; 1050 1051 return skb; 1052 } 1053 1054 static long fbnic_pkt_tx(struct fbnic_napi_vector *nv, 1055 struct fbnic_pkt_buff *pkt) 1056 { 1057 struct fbnic_ring *ring = &nv->qt[0].sub1; 1058 int size, offset, nsegs = 1, data_len = 0; 1059 unsigned int tail = ring->tail; 1060 struct skb_shared_info *shinfo; 1061 skb_frag_t *frag = NULL; 1062 struct page *page; 1063 dma_addr_t dma; 1064 __le64 *twd; 1065 1066 if (unlikely(xdp_buff_has_frags(&pkt->buff))) { 1067 shinfo = xdp_get_shared_info_from_buff(&pkt->buff); 1068 nsegs += shinfo->nr_frags; 1069 data_len = shinfo->xdp_frags_size; 1070 frag = &shinfo->frags[0]; 1071 } 1072 1073 if (fbnic_desc_unused(ring) < nsegs) { 1074 u64_stats_update_begin(&ring->stats.syncp); 1075 ring->stats.dropped++; 1076 u64_stats_update_end(&ring->stats.syncp); 1077 return -FBNIC_XDP_CONSUME; 1078 } 1079 1080 page = virt_to_page(pkt->buff.data_hard_start); 1081 offset = offset_in_page(pkt->buff.data); 1082 dma = page_pool_get_dma_addr(page); 1083 1084 size = pkt->buff.data_end - pkt->buff.data; 1085 1086 while (nsegs--) { 1087 dma_sync_single_range_for_device(nv->dev, dma, offset, size, 1088 DMA_BIDIRECTIONAL); 1089 dma += offset; 1090 1091 ring->tx_buf[tail] = page; 1092 1093 twd = &ring->desc[tail]; 1094 *twd = cpu_to_le64(FIELD_PREP(FBNIC_TWD_ADDR_MASK, dma) | 1095 FIELD_PREP(FBNIC_TWD_LEN_MASK, size) | 1096 FIELD_PREP(FBNIC_TWD_TYPE_MASK, 1097 FBNIC_TWD_TYPE_AL)); 1098 1099 tail++; 1100 tail &= ring->size_mask; 1101 1102 if (!data_len) 1103 break; 1104 1105 offset = skb_frag_off(frag); 1106 page = skb_frag_page(frag); 1107 dma = page_pool_get_dma_addr(page); 1108 1109 size = skb_frag_size(frag); 1110 data_len -= size; 1111 frag++; 1112 } 1113 1114 *twd |= FBNIC_TWD_TYPE(LAST_AL); 1115 1116 ring->tail = tail; 1117 1118 return -FBNIC_XDP_TX; 1119 } 1120 1121 static void fbnic_pkt_commit_tail(struct fbnic_napi_vector *nv, 1122 unsigned int pkt_tail) 1123 { 1124 struct fbnic_ring *ring = &nv->qt[0].sub1; 1125 1126 /* Force DMA writes to flush before writing to tail */ 1127 dma_wmb(); 1128 1129 writel(pkt_tail, ring->doorbell); 1130 } 1131 1132 static struct sk_buff *fbnic_run_xdp(struct fbnic_napi_vector *nv, 1133 struct fbnic_pkt_buff *pkt) 1134 { 1135 struct fbnic_net *fbn = netdev_priv(nv->napi.dev); 1136 struct bpf_prog *xdp_prog; 1137 int act; 1138 1139 xdp_prog = READ_ONCE(fbn->xdp_prog); 1140 if (!xdp_prog) 1141 goto xdp_pass; 1142 1143 /* Should never happen, config paths enforce HDS threshold > MTU */ 1144 if (xdp_buff_has_frags(&pkt->buff) && !xdp_prog->aux->xdp_has_frags) 1145 return ERR_PTR(-FBNIC_XDP_LEN_ERR); 1146 1147 act = bpf_prog_run_xdp(xdp_prog, &pkt->buff); 1148 switch (act) { 1149 case XDP_PASS: 1150 xdp_pass: 1151 return fbnic_build_skb(nv, pkt); 1152 case XDP_TX: 1153 return ERR_PTR(fbnic_pkt_tx(nv, pkt)); 1154 default: 1155 bpf_warn_invalid_xdp_action(nv->napi.dev, xdp_prog, act); 1156 fallthrough; 1157 case XDP_ABORTED: 1158 trace_xdp_exception(nv->napi.dev, xdp_prog, act); 1159 fallthrough; 1160 case XDP_DROP: 1161 break; 1162 } 1163 1164 return ERR_PTR(-FBNIC_XDP_CONSUME); 1165 } 1166 1167 static enum pkt_hash_types fbnic_skb_hash_type(u64 rcd) 1168 { 1169 return (FBNIC_RCD_META_L4_TYPE_MASK & rcd) ? PKT_HASH_TYPE_L4 : 1170 (FBNIC_RCD_META_L3_TYPE_MASK & rcd) ? PKT_HASH_TYPE_L3 : 1171 PKT_HASH_TYPE_L2; 1172 } 1173 1174 static void fbnic_rx_tstamp(struct fbnic_napi_vector *nv, u64 rcd, 1175 struct fbnic_pkt_buff *pkt) 1176 { 1177 struct fbnic_net *fbn; 1178 u64 ns, ts; 1179 1180 if (!FIELD_GET(FBNIC_RCD_OPT_META_TS, rcd)) 1181 return; 1182 1183 fbn = netdev_priv(nv->napi.dev); 1184 ts = FIELD_GET(FBNIC_RCD_OPT_META_TS_MASK, rcd); 1185 ns = fbnic_ts40_to_ns(fbn, ts); 1186 1187 /* Add timestamp to shared info */ 1188 pkt->hwtstamp = ns_to_ktime(ns); 1189 } 1190 1191 static void fbnic_populate_skb_fields(struct fbnic_napi_vector *nv, 1192 u64 rcd, struct sk_buff *skb, 1193 struct fbnic_q_triad *qt, 1194 u64 *csum_cmpl, u64 *csum_none) 1195 { 1196 struct net_device *netdev = nv->napi.dev; 1197 struct fbnic_ring *rcq = &qt->cmpl; 1198 1199 fbnic_rx_csum(rcd, skb, rcq, csum_cmpl, csum_none); 1200 1201 if (netdev->features & NETIF_F_RXHASH) 1202 skb_set_hash(skb, 1203 FIELD_GET(FBNIC_RCD_META_RSS_HASH_MASK, rcd), 1204 fbnic_skb_hash_type(rcd)); 1205 1206 skb_record_rx_queue(skb, rcq->q_idx); 1207 } 1208 1209 static bool fbnic_rcd_metadata_err(u64 rcd) 1210 { 1211 return !!(FBNIC_RCD_META_UNCORRECTABLE_ERR_MASK & rcd); 1212 } 1213 1214 static int fbnic_clean_rcq(struct fbnic_napi_vector *nv, 1215 struct fbnic_q_triad *qt, int budget) 1216 { 1217 unsigned int packets = 0, bytes = 0, dropped = 0, alloc_failed = 0; 1218 u64 csum_complete = 0, csum_none = 0, length_errors = 0; 1219 s32 head0 = -1, head1 = -1, pkt_tail = -1; 1220 struct fbnic_ring *rcq = &qt->cmpl; 1221 struct fbnic_pkt_buff *pkt; 1222 __le64 *raw_rcd, done; 1223 u32 head = rcq->head; 1224 1225 done = (head & (rcq->size_mask + 1)) ? cpu_to_le64(FBNIC_RCD_DONE) : 0; 1226 raw_rcd = &rcq->desc[head & rcq->size_mask]; 1227 pkt = rcq->pkt; 1228 1229 /* Walk the completion queue collecting the heads reported by NIC */ 1230 while (likely(packets < budget)) { 1231 struct sk_buff *skb = ERR_PTR(-EINVAL); 1232 u64 rcd; 1233 1234 if ((*raw_rcd & cpu_to_le64(FBNIC_RCD_DONE)) == done) 1235 break; 1236 1237 dma_rmb(); 1238 1239 rcd = le64_to_cpu(*raw_rcd); 1240 1241 switch (FIELD_GET(FBNIC_RCD_TYPE_MASK, rcd)) { 1242 case FBNIC_RCD_TYPE_HDR_AL: 1243 head0 = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd); 1244 fbnic_pkt_prepare(nv, rcd, pkt, qt); 1245 1246 break; 1247 case FBNIC_RCD_TYPE_PAY_AL: 1248 head1 = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd); 1249 fbnic_add_rx_frag(nv, rcd, pkt, qt); 1250 1251 break; 1252 case FBNIC_RCD_TYPE_OPT_META: 1253 /* Only type 0 is currently supported */ 1254 if (FIELD_GET(FBNIC_RCD_OPT_META_TYPE_MASK, rcd)) 1255 break; 1256 1257 fbnic_rx_tstamp(nv, rcd, pkt); 1258 1259 /* We currently ignore the action table index */ 1260 break; 1261 case FBNIC_RCD_TYPE_META: 1262 if (unlikely(pkt->add_frag_failed)) 1263 skb = NULL; 1264 else if (likely(!fbnic_rcd_metadata_err(rcd))) 1265 skb = fbnic_run_xdp(nv, pkt); 1266 1267 /* Populate skb and invalidate XDP */ 1268 if (!IS_ERR_OR_NULL(skb)) { 1269 fbnic_populate_skb_fields(nv, rcd, skb, qt, 1270 &csum_complete, 1271 &csum_none); 1272 1273 packets++; 1274 bytes += skb->len; 1275 1276 napi_gro_receive(&nv->napi, skb); 1277 } else if (skb == ERR_PTR(-FBNIC_XDP_TX)) { 1278 pkt_tail = nv->qt[0].sub1.tail; 1279 bytes += xdp_get_buff_len(&pkt->buff); 1280 } else { 1281 if (!skb) { 1282 alloc_failed++; 1283 dropped++; 1284 } else if (skb == ERR_PTR(-FBNIC_XDP_LEN_ERR)) { 1285 length_errors++; 1286 } else { 1287 dropped++; 1288 } 1289 1290 fbnic_put_pkt_buff(qt, pkt, 1); 1291 } 1292 1293 pkt->buff.data_hard_start = NULL; 1294 1295 break; 1296 } 1297 1298 raw_rcd++; 1299 head++; 1300 if (!(head & rcq->size_mask)) { 1301 done ^= cpu_to_le64(FBNIC_RCD_DONE); 1302 raw_rcd = &rcq->desc[0]; 1303 } 1304 } 1305 1306 u64_stats_update_begin(&rcq->stats.syncp); 1307 rcq->stats.packets += packets; 1308 rcq->stats.bytes += bytes; 1309 /* Re-add ethernet header length (removed in fbnic_build_skb) */ 1310 rcq->stats.bytes += ETH_HLEN * packets; 1311 rcq->stats.dropped += dropped; 1312 rcq->stats.rx.alloc_failed += alloc_failed; 1313 rcq->stats.rx.csum_complete += csum_complete; 1314 rcq->stats.rx.csum_none += csum_none; 1315 rcq->stats.rx.length_errors += length_errors; 1316 u64_stats_update_end(&rcq->stats.syncp); 1317 1318 if (pkt_tail >= 0) 1319 fbnic_pkt_commit_tail(nv, pkt_tail); 1320 1321 /* Unmap and free processed buffers */ 1322 if (head0 >= 0) 1323 fbnic_clean_bdq(&qt->sub0, head0, budget); 1324 fbnic_fill_bdq(&qt->sub0); 1325 1326 if (head1 >= 0) 1327 fbnic_clean_bdq(&qt->sub1, head1, budget); 1328 fbnic_fill_bdq(&qt->sub1); 1329 1330 /* Record the current head/tail of the queue */ 1331 if (rcq->head != head) { 1332 rcq->head = head; 1333 writel(head & rcq->size_mask, rcq->doorbell); 1334 } 1335 1336 return packets; 1337 } 1338 1339 static void fbnic_nv_irq_disable(struct fbnic_napi_vector *nv) 1340 { 1341 struct fbnic_dev *fbd = nv->fbd; 1342 u32 v_idx = nv->v_idx; 1343 1344 fbnic_wr32(fbd, FBNIC_INTR_MASK_SET(v_idx / 32), 1 << (v_idx % 32)); 1345 } 1346 1347 static void fbnic_nv_irq_rearm(struct fbnic_napi_vector *nv) 1348 { 1349 struct fbnic_dev *fbd = nv->fbd; 1350 u32 v_idx = nv->v_idx; 1351 1352 fbnic_wr32(fbd, FBNIC_INTR_CQ_REARM(v_idx), 1353 FBNIC_INTR_CQ_REARM_INTR_UNMASK); 1354 } 1355 1356 static int fbnic_poll(struct napi_struct *napi, int budget) 1357 { 1358 struct fbnic_napi_vector *nv = container_of(napi, 1359 struct fbnic_napi_vector, 1360 napi); 1361 int i, j, work_done = 0; 1362 1363 for (i = 0; i < nv->txt_count; i++) 1364 fbnic_clean_tcq(nv, &nv->qt[i], budget); 1365 1366 for (j = 0; j < nv->rxt_count; j++, i++) 1367 work_done += fbnic_clean_rcq(nv, &nv->qt[i], budget); 1368 1369 if (work_done >= budget) 1370 return budget; 1371 1372 if (likely(napi_complete_done(napi, work_done))) 1373 fbnic_nv_irq_rearm(nv); 1374 1375 return work_done; 1376 } 1377 1378 irqreturn_t fbnic_msix_clean_rings(int __always_unused irq, void *data) 1379 { 1380 struct fbnic_napi_vector *nv = *(void **)data; 1381 1382 napi_schedule_irqoff(&nv->napi); 1383 1384 return IRQ_HANDLED; 1385 } 1386 1387 void fbnic_aggregate_ring_rx_counters(struct fbnic_net *fbn, 1388 struct fbnic_ring *rxr) 1389 { 1390 struct fbnic_queue_stats *stats = &rxr->stats; 1391 1392 /* Capture stats from queues before dissasociating them */ 1393 fbn->rx_stats.bytes += stats->bytes; 1394 fbn->rx_stats.packets += stats->packets; 1395 fbn->rx_stats.dropped += stats->dropped; 1396 fbn->rx_stats.rx.alloc_failed += stats->rx.alloc_failed; 1397 fbn->rx_stats.rx.csum_complete += stats->rx.csum_complete; 1398 fbn->rx_stats.rx.csum_none += stats->rx.csum_none; 1399 fbn->rx_stats.rx.length_errors += stats->rx.length_errors; 1400 /* Remember to add new stats here */ 1401 BUILD_BUG_ON(sizeof(fbn->rx_stats.rx) / 8 != 4); 1402 } 1403 1404 void fbnic_aggregate_ring_tx_counters(struct fbnic_net *fbn, 1405 struct fbnic_ring *txr) 1406 { 1407 struct fbnic_queue_stats *stats = &txr->stats; 1408 1409 /* Capture stats from queues before dissasociating them */ 1410 fbn->tx_stats.bytes += stats->bytes; 1411 fbn->tx_stats.packets += stats->packets; 1412 fbn->tx_stats.dropped += stats->dropped; 1413 fbn->tx_stats.twq.csum_partial += stats->twq.csum_partial; 1414 fbn->tx_stats.twq.lso += stats->twq.lso; 1415 fbn->tx_stats.twq.ts_lost += stats->twq.ts_lost; 1416 fbn->tx_stats.twq.ts_packets += stats->twq.ts_packets; 1417 fbn->tx_stats.twq.stop += stats->twq.stop; 1418 fbn->tx_stats.twq.wake += stats->twq.wake; 1419 /* Remember to add new stats here */ 1420 BUILD_BUG_ON(sizeof(fbn->tx_stats.twq) / 8 != 6); 1421 } 1422 1423 static void fbnic_aggregate_ring_xdp_counters(struct fbnic_net *fbn, 1424 struct fbnic_ring *xdpr) 1425 { 1426 struct fbnic_queue_stats *stats = &xdpr->stats; 1427 1428 if (!(xdpr->flags & FBNIC_RING_F_STATS)) 1429 return; 1430 1431 /* Capture stats from queues before dissasociating them */ 1432 fbn->rx_stats.bytes += stats->bytes; 1433 fbn->rx_stats.packets += stats->packets; 1434 fbn->rx_stats.dropped += stats->dropped; 1435 fbn->tx_stats.bytes += stats->bytes; 1436 fbn->tx_stats.packets += stats->packets; 1437 } 1438 1439 static void fbnic_remove_tx_ring(struct fbnic_net *fbn, 1440 struct fbnic_ring *txr) 1441 { 1442 if (!(txr->flags & FBNIC_RING_F_STATS)) 1443 return; 1444 1445 fbnic_aggregate_ring_tx_counters(fbn, txr); 1446 1447 /* Remove pointer to the Tx ring */ 1448 WARN_ON(fbn->tx[txr->q_idx] && fbn->tx[txr->q_idx] != txr); 1449 fbn->tx[txr->q_idx] = NULL; 1450 } 1451 1452 static void fbnic_remove_xdp_ring(struct fbnic_net *fbn, 1453 struct fbnic_ring *xdpr) 1454 { 1455 if (!(xdpr->flags & FBNIC_RING_F_STATS)) 1456 return; 1457 1458 fbnic_aggregate_ring_xdp_counters(fbn, xdpr); 1459 1460 /* Remove pointer to the Tx ring */ 1461 WARN_ON(fbn->tx[xdpr->q_idx] && fbn->tx[xdpr->q_idx] != xdpr); 1462 fbn->tx[xdpr->q_idx] = NULL; 1463 } 1464 1465 static void fbnic_remove_rx_ring(struct fbnic_net *fbn, 1466 struct fbnic_ring *rxr) 1467 { 1468 if (!(rxr->flags & FBNIC_RING_F_STATS)) 1469 return; 1470 1471 fbnic_aggregate_ring_rx_counters(fbn, rxr); 1472 1473 /* Remove pointer to the Rx ring */ 1474 WARN_ON(fbn->rx[rxr->q_idx] && fbn->rx[rxr->q_idx] != rxr); 1475 fbn->rx[rxr->q_idx] = NULL; 1476 } 1477 1478 static void fbnic_free_qt_page_pools(struct fbnic_q_triad *qt) 1479 { 1480 page_pool_destroy(qt->sub0.page_pool); 1481 page_pool_destroy(qt->sub1.page_pool); 1482 } 1483 1484 static void fbnic_free_napi_vector(struct fbnic_net *fbn, 1485 struct fbnic_napi_vector *nv) 1486 { 1487 struct fbnic_dev *fbd = nv->fbd; 1488 int i, j; 1489 1490 for (i = 0; i < nv->txt_count; i++) { 1491 fbnic_remove_tx_ring(fbn, &nv->qt[i].sub0); 1492 fbnic_remove_xdp_ring(fbn, &nv->qt[i].sub1); 1493 fbnic_remove_tx_ring(fbn, &nv->qt[i].cmpl); 1494 } 1495 1496 for (j = 0; j < nv->rxt_count; j++, i++) { 1497 fbnic_remove_rx_ring(fbn, &nv->qt[i].sub0); 1498 fbnic_remove_rx_ring(fbn, &nv->qt[i].sub1); 1499 fbnic_remove_rx_ring(fbn, &nv->qt[i].cmpl); 1500 } 1501 1502 fbnic_napi_free_irq(fbd, nv); 1503 netif_napi_del_locked(&nv->napi); 1504 fbn->napi[fbnic_napi_idx(nv)] = NULL; 1505 kfree(nv); 1506 } 1507 1508 void fbnic_free_napi_vectors(struct fbnic_net *fbn) 1509 { 1510 int i; 1511 1512 for (i = 0; i < fbn->num_napi; i++) 1513 if (fbn->napi[i]) 1514 fbnic_free_napi_vector(fbn, fbn->napi[i]); 1515 } 1516 1517 static int 1518 fbnic_alloc_qt_page_pools(struct fbnic_net *fbn, struct fbnic_q_triad *qt, 1519 unsigned int rxq_idx) 1520 { 1521 struct page_pool_params pp_params = { 1522 .order = 0, 1523 .flags = PP_FLAG_DMA_MAP | 1524 PP_FLAG_DMA_SYNC_DEV, 1525 .pool_size = fbn->hpq_size + fbn->ppq_size, 1526 .nid = NUMA_NO_NODE, 1527 .dev = fbn->netdev->dev.parent, 1528 .dma_dir = DMA_BIDIRECTIONAL, 1529 .offset = 0, 1530 .max_len = PAGE_SIZE, 1531 .netdev = fbn->netdev, 1532 .queue_idx = rxq_idx, 1533 }; 1534 struct page_pool *pp; 1535 1536 /* Page pool cannot exceed a size of 32768. This doesn't limit the 1537 * pages on the ring but the number we can have cached waiting on 1538 * the next use. 1539 * 1540 * TBD: Can this be reduced further? Would a multiple of 1541 * NAPI_POLL_WEIGHT possibly make more sense? The question is how 1542 * may pages do we need to hold in reserve to get the best return 1543 * without hogging too much system memory. 1544 */ 1545 if (pp_params.pool_size > 32768) 1546 pp_params.pool_size = 32768; 1547 1548 pp = page_pool_create(&pp_params); 1549 if (IS_ERR(pp)) 1550 return PTR_ERR(pp); 1551 1552 qt->sub0.page_pool = pp; 1553 if (netif_rxq_has_unreadable_mp(fbn->netdev, rxq_idx)) { 1554 pp_params.flags |= PP_FLAG_ALLOW_UNREADABLE_NETMEM; 1555 pp_params.dma_dir = DMA_FROM_DEVICE; 1556 1557 pp = page_pool_create(&pp_params); 1558 if (IS_ERR(pp)) 1559 goto err_destroy_sub0; 1560 } else { 1561 page_pool_get(pp); 1562 } 1563 qt->sub1.page_pool = pp; 1564 1565 return 0; 1566 1567 err_destroy_sub0: 1568 page_pool_destroy(pp); 1569 return PTR_ERR(pp); 1570 } 1571 1572 static void fbnic_ring_init(struct fbnic_ring *ring, u32 __iomem *doorbell, 1573 int q_idx, u8 flags) 1574 { 1575 u64_stats_init(&ring->stats.syncp); 1576 ring->doorbell = doorbell; 1577 ring->q_idx = q_idx; 1578 ring->flags = flags; 1579 ring->deferred_head = -1; 1580 } 1581 1582 static int fbnic_alloc_napi_vector(struct fbnic_dev *fbd, struct fbnic_net *fbn, 1583 unsigned int v_count, unsigned int v_idx, 1584 unsigned int txq_count, unsigned int txq_idx, 1585 unsigned int rxq_count, unsigned int rxq_idx) 1586 { 1587 int txt_count = txq_count, rxt_count = rxq_count; 1588 u32 __iomem *uc_addr = fbd->uc_addr0; 1589 int xdp_count = 0, qt_count, err; 1590 struct fbnic_napi_vector *nv; 1591 struct fbnic_q_triad *qt; 1592 u32 __iomem *db; 1593 1594 /* We need to reserve at least one Tx Queue Triad for an XDP ring */ 1595 if (rxq_count) { 1596 xdp_count = 1; 1597 if (!txt_count) 1598 txt_count = 1; 1599 } 1600 1601 qt_count = txt_count + rxq_count; 1602 if (!qt_count) 1603 return -EINVAL; 1604 1605 /* If MMIO has already failed there are no rings to initialize */ 1606 if (!uc_addr) 1607 return -EIO; 1608 1609 /* Allocate NAPI vector and queue triads */ 1610 nv = kzalloc(struct_size(nv, qt, qt_count), GFP_KERNEL); 1611 if (!nv) 1612 return -ENOMEM; 1613 1614 /* Record queue triad counts */ 1615 nv->txt_count = txt_count; 1616 nv->rxt_count = rxt_count; 1617 1618 /* Provide pointer back to fbnic and MSI-X vectors */ 1619 nv->fbd = fbd; 1620 nv->v_idx = v_idx; 1621 1622 /* Tie napi to netdev */ 1623 fbn->napi[fbnic_napi_idx(nv)] = nv; 1624 netif_napi_add_locked(fbn->netdev, &nv->napi, fbnic_poll); 1625 1626 /* Record IRQ to NAPI struct */ 1627 netif_napi_set_irq_locked(&nv->napi, 1628 pci_irq_vector(to_pci_dev(fbd->dev), 1629 nv->v_idx)); 1630 1631 /* Tie nv back to PCIe dev */ 1632 nv->dev = fbd->dev; 1633 1634 /* Request the IRQ for napi vector */ 1635 err = fbnic_napi_request_irq(fbd, nv); 1636 if (err) 1637 goto napi_del; 1638 1639 /* Initialize queue triads */ 1640 qt = nv->qt; 1641 1642 while (txt_count) { 1643 u8 flags = FBNIC_RING_F_CTX | FBNIC_RING_F_STATS; 1644 1645 /* Configure Tx queue */ 1646 db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TWQ0_TAIL]; 1647 1648 /* Assign Tx queue to netdev if applicable */ 1649 if (txq_count > 0) { 1650 1651 fbnic_ring_init(&qt->sub0, db, txq_idx, flags); 1652 fbn->tx[txq_idx] = &qt->sub0; 1653 txq_count--; 1654 } else { 1655 fbnic_ring_init(&qt->sub0, db, 0, 1656 FBNIC_RING_F_DISABLED); 1657 } 1658 1659 /* Configure XDP queue */ 1660 db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TWQ1_TAIL]; 1661 1662 /* Assign XDP queue to netdev if applicable 1663 * 1664 * The setup for this is in itself a bit different. 1665 * 1. We only need one XDP Tx queue per NAPI vector. 1666 * 2. We associate it to the first Rx queue index. 1667 * 3. The hardware side is associated based on the Tx Queue. 1668 * 4. The netdev queue is offset by FBNIC_MAX_TXQs. 1669 */ 1670 if (xdp_count > 0) { 1671 unsigned int xdp_idx = FBNIC_MAX_TXQS + rxq_idx; 1672 1673 fbnic_ring_init(&qt->sub1, db, xdp_idx, flags); 1674 fbn->tx[xdp_idx] = &qt->sub1; 1675 xdp_count--; 1676 } else { 1677 fbnic_ring_init(&qt->sub1, db, 0, 1678 FBNIC_RING_F_DISABLED); 1679 } 1680 1681 /* Configure Tx completion queue */ 1682 db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TCQ_HEAD]; 1683 fbnic_ring_init(&qt->cmpl, db, 0, 0); 1684 1685 /* Update Tx queue index */ 1686 txt_count--; 1687 txq_idx += v_count; 1688 1689 /* Move to next queue triad */ 1690 qt++; 1691 } 1692 1693 while (rxt_count) { 1694 /* Configure header queue */ 1695 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_BDQ_HPQ_TAIL]; 1696 fbnic_ring_init(&qt->sub0, db, 0, FBNIC_RING_F_CTX); 1697 1698 /* Configure payload queue */ 1699 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_BDQ_PPQ_TAIL]; 1700 fbnic_ring_init(&qt->sub1, db, 0, FBNIC_RING_F_CTX); 1701 1702 /* Configure Rx completion queue */ 1703 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_RCQ_HEAD]; 1704 fbnic_ring_init(&qt->cmpl, db, rxq_idx, FBNIC_RING_F_STATS); 1705 fbn->rx[rxq_idx] = &qt->cmpl; 1706 1707 /* Update Rx queue index */ 1708 rxt_count--; 1709 rxq_idx += v_count; 1710 1711 /* Move to next queue triad */ 1712 qt++; 1713 } 1714 1715 return 0; 1716 1717 napi_del: 1718 netif_napi_del_locked(&nv->napi); 1719 fbn->napi[fbnic_napi_idx(nv)] = NULL; 1720 kfree(nv); 1721 return err; 1722 } 1723 1724 int fbnic_alloc_napi_vectors(struct fbnic_net *fbn) 1725 { 1726 unsigned int txq_idx = 0, rxq_idx = 0, v_idx = FBNIC_NON_NAPI_VECTORS; 1727 unsigned int num_tx = fbn->num_tx_queues; 1728 unsigned int num_rx = fbn->num_rx_queues; 1729 unsigned int num_napi = fbn->num_napi; 1730 struct fbnic_dev *fbd = fbn->fbd; 1731 int err; 1732 1733 /* Allocate 1 Tx queue per napi vector */ 1734 if (num_napi < FBNIC_MAX_TXQS && num_napi == num_tx + num_rx) { 1735 while (num_tx) { 1736 err = fbnic_alloc_napi_vector(fbd, fbn, 1737 num_napi, v_idx, 1738 1, txq_idx, 0, 0); 1739 if (err) 1740 goto free_vectors; 1741 1742 /* Update counts and index */ 1743 num_tx--; 1744 txq_idx++; 1745 1746 v_idx++; 1747 } 1748 } 1749 1750 /* Allocate Tx/Rx queue pairs per vector, or allocate remaining Rx */ 1751 while (num_rx | num_tx) { 1752 int tqpv = DIV_ROUND_UP(num_tx, num_napi - txq_idx); 1753 int rqpv = DIV_ROUND_UP(num_rx, num_napi - rxq_idx); 1754 1755 err = fbnic_alloc_napi_vector(fbd, fbn, num_napi, v_idx, 1756 tqpv, txq_idx, rqpv, rxq_idx); 1757 if (err) 1758 goto free_vectors; 1759 1760 /* Update counts and index */ 1761 num_tx -= tqpv; 1762 txq_idx++; 1763 1764 num_rx -= rqpv; 1765 rxq_idx++; 1766 1767 v_idx++; 1768 } 1769 1770 return 0; 1771 1772 free_vectors: 1773 fbnic_free_napi_vectors(fbn); 1774 1775 return -ENOMEM; 1776 } 1777 1778 static void fbnic_free_ring_resources(struct device *dev, 1779 struct fbnic_ring *ring) 1780 { 1781 kvfree(ring->buffer); 1782 ring->buffer = NULL; 1783 1784 /* If size is not set there are no descriptors present */ 1785 if (!ring->size) 1786 return; 1787 1788 dma_free_coherent(dev, ring->size, ring->desc, ring->dma); 1789 ring->size_mask = 0; 1790 ring->size = 0; 1791 } 1792 1793 static int fbnic_alloc_tx_ring_desc(struct fbnic_net *fbn, 1794 struct fbnic_ring *txr) 1795 { 1796 struct device *dev = fbn->netdev->dev.parent; 1797 size_t size; 1798 1799 /* Round size up to nearest 4K */ 1800 size = ALIGN(array_size(sizeof(*txr->desc), fbn->txq_size), 4096); 1801 1802 txr->desc = dma_alloc_coherent(dev, size, &txr->dma, 1803 GFP_KERNEL | __GFP_NOWARN); 1804 if (!txr->desc) 1805 return -ENOMEM; 1806 1807 /* txq_size should be a power of 2, so mask is just that -1 */ 1808 txr->size_mask = fbn->txq_size - 1; 1809 txr->size = size; 1810 1811 return 0; 1812 } 1813 1814 static int fbnic_alloc_tx_ring_buffer(struct fbnic_ring *txr) 1815 { 1816 size_t size = array_size(sizeof(*txr->tx_buf), txr->size_mask + 1); 1817 1818 txr->tx_buf = kvzalloc(size, GFP_KERNEL | __GFP_NOWARN); 1819 1820 return txr->tx_buf ? 0 : -ENOMEM; 1821 } 1822 1823 static int fbnic_alloc_tx_ring_resources(struct fbnic_net *fbn, 1824 struct fbnic_ring *txr) 1825 { 1826 struct device *dev = fbn->netdev->dev.parent; 1827 int err; 1828 1829 if (txr->flags & FBNIC_RING_F_DISABLED) 1830 return 0; 1831 1832 err = fbnic_alloc_tx_ring_desc(fbn, txr); 1833 if (err) 1834 return err; 1835 1836 if (!(txr->flags & FBNIC_RING_F_CTX)) 1837 return 0; 1838 1839 err = fbnic_alloc_tx_ring_buffer(txr); 1840 if (err) 1841 goto free_desc; 1842 1843 return 0; 1844 1845 free_desc: 1846 fbnic_free_ring_resources(dev, txr); 1847 return err; 1848 } 1849 1850 static int fbnic_alloc_rx_ring_desc(struct fbnic_net *fbn, 1851 struct fbnic_ring *rxr) 1852 { 1853 struct device *dev = fbn->netdev->dev.parent; 1854 size_t desc_size = sizeof(*rxr->desc); 1855 u32 rxq_size; 1856 size_t size; 1857 1858 switch (rxr->doorbell - fbnic_ring_csr_base(rxr)) { 1859 case FBNIC_QUEUE_BDQ_HPQ_TAIL: 1860 rxq_size = fbn->hpq_size / FBNIC_BD_FRAG_COUNT; 1861 desc_size *= FBNIC_BD_FRAG_COUNT; 1862 break; 1863 case FBNIC_QUEUE_BDQ_PPQ_TAIL: 1864 rxq_size = fbn->ppq_size / FBNIC_BD_FRAG_COUNT; 1865 desc_size *= FBNIC_BD_FRAG_COUNT; 1866 break; 1867 case FBNIC_QUEUE_RCQ_HEAD: 1868 rxq_size = fbn->rcq_size; 1869 break; 1870 default: 1871 return -EINVAL; 1872 } 1873 1874 /* Round size up to nearest 4K */ 1875 size = ALIGN(array_size(desc_size, rxq_size), 4096); 1876 1877 rxr->desc = dma_alloc_coherent(dev, size, &rxr->dma, 1878 GFP_KERNEL | __GFP_NOWARN); 1879 if (!rxr->desc) 1880 return -ENOMEM; 1881 1882 /* rxq_size should be a power of 2, so mask is just that -1 */ 1883 rxr->size_mask = rxq_size - 1; 1884 rxr->size = size; 1885 1886 return 0; 1887 } 1888 1889 static int fbnic_alloc_rx_ring_buffer(struct fbnic_ring *rxr) 1890 { 1891 size_t size = array_size(sizeof(*rxr->rx_buf), rxr->size_mask + 1); 1892 1893 if (rxr->flags & FBNIC_RING_F_CTX) 1894 size = sizeof(*rxr->rx_buf) * (rxr->size_mask + 1); 1895 else 1896 size = sizeof(*rxr->pkt); 1897 1898 rxr->rx_buf = kvzalloc(size, GFP_KERNEL | __GFP_NOWARN); 1899 1900 return rxr->rx_buf ? 0 : -ENOMEM; 1901 } 1902 1903 static int fbnic_alloc_rx_ring_resources(struct fbnic_net *fbn, 1904 struct fbnic_ring *rxr) 1905 { 1906 struct device *dev = fbn->netdev->dev.parent; 1907 int err; 1908 1909 err = fbnic_alloc_rx_ring_desc(fbn, rxr); 1910 if (err) 1911 return err; 1912 1913 err = fbnic_alloc_rx_ring_buffer(rxr); 1914 if (err) 1915 goto free_desc; 1916 1917 return 0; 1918 1919 free_desc: 1920 fbnic_free_ring_resources(dev, rxr); 1921 return err; 1922 } 1923 1924 static void fbnic_free_qt_resources(struct fbnic_net *fbn, 1925 struct fbnic_q_triad *qt) 1926 { 1927 struct device *dev = fbn->netdev->dev.parent; 1928 1929 fbnic_free_ring_resources(dev, &qt->cmpl); 1930 fbnic_free_ring_resources(dev, &qt->sub1); 1931 fbnic_free_ring_resources(dev, &qt->sub0); 1932 1933 if (xdp_rxq_info_is_reg(&qt->xdp_rxq)) { 1934 xdp_rxq_info_unreg_mem_model(&qt->xdp_rxq); 1935 xdp_rxq_info_unreg(&qt->xdp_rxq); 1936 fbnic_free_qt_page_pools(qt); 1937 } 1938 } 1939 1940 static int fbnic_alloc_tx_qt_resources(struct fbnic_net *fbn, 1941 struct fbnic_q_triad *qt) 1942 { 1943 struct device *dev = fbn->netdev->dev.parent; 1944 int err; 1945 1946 err = fbnic_alloc_tx_ring_resources(fbn, &qt->sub0); 1947 if (err) 1948 return err; 1949 1950 err = fbnic_alloc_tx_ring_resources(fbn, &qt->sub1); 1951 if (err) 1952 goto free_sub0; 1953 1954 err = fbnic_alloc_tx_ring_resources(fbn, &qt->cmpl); 1955 if (err) 1956 goto free_sub1; 1957 1958 return 0; 1959 1960 free_sub1: 1961 fbnic_free_ring_resources(dev, &qt->sub1); 1962 free_sub0: 1963 fbnic_free_ring_resources(dev, &qt->sub0); 1964 return err; 1965 } 1966 1967 static int fbnic_alloc_rx_qt_resources(struct fbnic_net *fbn, 1968 struct fbnic_napi_vector *nv, 1969 struct fbnic_q_triad *qt) 1970 { 1971 struct device *dev = fbn->netdev->dev.parent; 1972 int err; 1973 1974 err = fbnic_alloc_qt_page_pools(fbn, qt, qt->cmpl.q_idx); 1975 if (err) 1976 return err; 1977 1978 err = xdp_rxq_info_reg(&qt->xdp_rxq, fbn->netdev, qt->sub0.q_idx, 1979 nv->napi.napi_id); 1980 if (err) 1981 goto free_page_pools; 1982 1983 err = xdp_rxq_info_reg_mem_model(&qt->xdp_rxq, MEM_TYPE_PAGE_POOL, 1984 qt->sub0.page_pool); 1985 if (err) 1986 goto unreg_rxq; 1987 1988 err = fbnic_alloc_rx_ring_resources(fbn, &qt->sub0); 1989 if (err) 1990 goto unreg_mm; 1991 1992 err = fbnic_alloc_rx_ring_resources(fbn, &qt->sub1); 1993 if (err) 1994 goto free_sub0; 1995 1996 err = fbnic_alloc_rx_ring_resources(fbn, &qt->cmpl); 1997 if (err) 1998 goto free_sub1; 1999 2000 return 0; 2001 2002 free_sub1: 2003 fbnic_free_ring_resources(dev, &qt->sub1); 2004 free_sub0: 2005 fbnic_free_ring_resources(dev, &qt->sub0); 2006 unreg_mm: 2007 xdp_rxq_info_unreg_mem_model(&qt->xdp_rxq); 2008 unreg_rxq: 2009 xdp_rxq_info_unreg(&qt->xdp_rxq); 2010 free_page_pools: 2011 fbnic_free_qt_page_pools(qt); 2012 return err; 2013 } 2014 2015 static void fbnic_free_nv_resources(struct fbnic_net *fbn, 2016 struct fbnic_napi_vector *nv) 2017 { 2018 int i; 2019 2020 for (i = 0; i < nv->txt_count + nv->rxt_count; i++) 2021 fbnic_free_qt_resources(fbn, &nv->qt[i]); 2022 } 2023 2024 static int fbnic_alloc_nv_resources(struct fbnic_net *fbn, 2025 struct fbnic_napi_vector *nv) 2026 { 2027 int i, j, err; 2028 2029 /* Allocate Tx Resources */ 2030 for (i = 0; i < nv->txt_count; i++) { 2031 err = fbnic_alloc_tx_qt_resources(fbn, &nv->qt[i]); 2032 if (err) 2033 goto free_qt_resources; 2034 } 2035 2036 /* Allocate Rx Resources */ 2037 for (j = 0; j < nv->rxt_count; j++, i++) { 2038 err = fbnic_alloc_rx_qt_resources(fbn, nv, &nv->qt[i]); 2039 if (err) 2040 goto free_qt_resources; 2041 } 2042 2043 return 0; 2044 2045 free_qt_resources: 2046 while (i--) 2047 fbnic_free_qt_resources(fbn, &nv->qt[i]); 2048 return err; 2049 } 2050 2051 void fbnic_free_resources(struct fbnic_net *fbn) 2052 { 2053 int i; 2054 2055 for (i = 0; i < fbn->num_napi; i++) 2056 fbnic_free_nv_resources(fbn, fbn->napi[i]); 2057 } 2058 2059 int fbnic_alloc_resources(struct fbnic_net *fbn) 2060 { 2061 int i, err = -ENODEV; 2062 2063 for (i = 0; i < fbn->num_napi; i++) { 2064 err = fbnic_alloc_nv_resources(fbn, fbn->napi[i]); 2065 if (err) 2066 goto free_resources; 2067 } 2068 2069 return 0; 2070 2071 free_resources: 2072 while (i--) 2073 fbnic_free_nv_resources(fbn, fbn->napi[i]); 2074 2075 return err; 2076 } 2077 2078 static void fbnic_set_netif_napi(struct fbnic_napi_vector *nv) 2079 { 2080 int i, j; 2081 2082 /* Associate Tx queue with NAPI */ 2083 for (i = 0; i < nv->txt_count; i++) { 2084 struct fbnic_q_triad *qt = &nv->qt[i]; 2085 2086 netif_queue_set_napi(nv->napi.dev, qt->sub0.q_idx, 2087 NETDEV_QUEUE_TYPE_TX, &nv->napi); 2088 } 2089 2090 /* Associate Rx queue with NAPI */ 2091 for (j = 0; j < nv->rxt_count; j++, i++) { 2092 struct fbnic_q_triad *qt = &nv->qt[i]; 2093 2094 netif_queue_set_napi(nv->napi.dev, qt->cmpl.q_idx, 2095 NETDEV_QUEUE_TYPE_RX, &nv->napi); 2096 } 2097 } 2098 2099 static void fbnic_reset_netif_napi(struct fbnic_napi_vector *nv) 2100 { 2101 int i, j; 2102 2103 /* Disassociate Tx queue from NAPI */ 2104 for (i = 0; i < nv->txt_count; i++) { 2105 struct fbnic_q_triad *qt = &nv->qt[i]; 2106 2107 netif_queue_set_napi(nv->napi.dev, qt->sub0.q_idx, 2108 NETDEV_QUEUE_TYPE_TX, NULL); 2109 } 2110 2111 /* Disassociate Rx queue from NAPI */ 2112 for (j = 0; j < nv->rxt_count; j++, i++) { 2113 struct fbnic_q_triad *qt = &nv->qt[i]; 2114 2115 netif_queue_set_napi(nv->napi.dev, qt->cmpl.q_idx, 2116 NETDEV_QUEUE_TYPE_RX, NULL); 2117 } 2118 } 2119 2120 int fbnic_set_netif_queues(struct fbnic_net *fbn) 2121 { 2122 int i, err; 2123 2124 err = netif_set_real_num_queues(fbn->netdev, fbn->num_tx_queues, 2125 fbn->num_rx_queues); 2126 if (err) 2127 return err; 2128 2129 for (i = 0; i < fbn->num_napi; i++) 2130 fbnic_set_netif_napi(fbn->napi[i]); 2131 2132 return 0; 2133 } 2134 2135 void fbnic_reset_netif_queues(struct fbnic_net *fbn) 2136 { 2137 int i; 2138 2139 for (i = 0; i < fbn->num_napi; i++) 2140 fbnic_reset_netif_napi(fbn->napi[i]); 2141 } 2142 2143 static void fbnic_disable_twq0(struct fbnic_ring *txr) 2144 { 2145 u32 twq_ctl = fbnic_ring_rd32(txr, FBNIC_QUEUE_TWQ0_CTL); 2146 2147 twq_ctl &= ~FBNIC_QUEUE_TWQ_CTL_ENABLE; 2148 2149 fbnic_ring_wr32(txr, FBNIC_QUEUE_TWQ0_CTL, twq_ctl); 2150 } 2151 2152 static void fbnic_disable_twq1(struct fbnic_ring *txr) 2153 { 2154 u32 twq_ctl = fbnic_ring_rd32(txr, FBNIC_QUEUE_TWQ1_CTL); 2155 2156 twq_ctl &= ~FBNIC_QUEUE_TWQ_CTL_ENABLE; 2157 2158 fbnic_ring_wr32(txr, FBNIC_QUEUE_TWQ1_CTL, twq_ctl); 2159 } 2160 2161 static void fbnic_disable_tcq(struct fbnic_ring *txr) 2162 { 2163 fbnic_ring_wr32(txr, FBNIC_QUEUE_TCQ_CTL, 0); 2164 fbnic_ring_wr32(txr, FBNIC_QUEUE_TIM_MASK, FBNIC_QUEUE_TIM_MASK_MASK); 2165 } 2166 2167 static void fbnic_disable_bdq(struct fbnic_ring *hpq, struct fbnic_ring *ppq) 2168 { 2169 u32 bdq_ctl = fbnic_ring_rd32(hpq, FBNIC_QUEUE_BDQ_CTL); 2170 2171 bdq_ctl &= ~FBNIC_QUEUE_BDQ_CTL_ENABLE; 2172 2173 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, bdq_ctl); 2174 } 2175 2176 static void fbnic_disable_rcq(struct fbnic_ring *rxr) 2177 { 2178 fbnic_ring_wr32(rxr, FBNIC_QUEUE_RCQ_CTL, 0); 2179 fbnic_ring_wr32(rxr, FBNIC_QUEUE_RIM_MASK, FBNIC_QUEUE_RIM_MASK_MASK); 2180 } 2181 2182 void fbnic_napi_disable(struct fbnic_net *fbn) 2183 { 2184 int i; 2185 2186 for (i = 0; i < fbn->num_napi; i++) { 2187 napi_disable_locked(&fbn->napi[i]->napi); 2188 2189 fbnic_nv_irq_disable(fbn->napi[i]); 2190 } 2191 } 2192 2193 static void __fbnic_nv_disable(struct fbnic_napi_vector *nv) 2194 { 2195 int i, t; 2196 2197 /* Disable Tx queue triads */ 2198 for (t = 0; t < nv->txt_count; t++) { 2199 struct fbnic_q_triad *qt = &nv->qt[t]; 2200 2201 fbnic_disable_twq0(&qt->sub0); 2202 fbnic_disable_twq1(&qt->sub1); 2203 fbnic_disable_tcq(&qt->cmpl); 2204 } 2205 2206 /* Disable Rx queue triads */ 2207 for (i = 0; i < nv->rxt_count; i++, t++) { 2208 struct fbnic_q_triad *qt = &nv->qt[t]; 2209 2210 fbnic_disable_bdq(&qt->sub0, &qt->sub1); 2211 fbnic_disable_rcq(&qt->cmpl); 2212 } 2213 } 2214 2215 static void 2216 fbnic_nv_disable(struct fbnic_net *fbn, struct fbnic_napi_vector *nv) 2217 { 2218 __fbnic_nv_disable(nv); 2219 fbnic_wrfl(fbn->fbd); 2220 } 2221 2222 void fbnic_disable(struct fbnic_net *fbn) 2223 { 2224 struct fbnic_dev *fbd = fbn->fbd; 2225 int i; 2226 2227 for (i = 0; i < fbn->num_napi; i++) 2228 __fbnic_nv_disable(fbn->napi[i]); 2229 2230 fbnic_wrfl(fbd); 2231 } 2232 2233 static void fbnic_tx_flush(struct fbnic_dev *fbd) 2234 { 2235 netdev_warn(fbd->netdev, "triggering Tx flush\n"); 2236 2237 fbnic_rmw32(fbd, FBNIC_TMI_DROP_CTRL, FBNIC_TMI_DROP_CTRL_EN, 2238 FBNIC_TMI_DROP_CTRL_EN); 2239 } 2240 2241 static void fbnic_tx_flush_off(struct fbnic_dev *fbd) 2242 { 2243 fbnic_rmw32(fbd, FBNIC_TMI_DROP_CTRL, FBNIC_TMI_DROP_CTRL_EN, 0); 2244 } 2245 2246 struct fbnic_idle_regs { 2247 u32 reg_base; 2248 u8 reg_cnt; 2249 }; 2250 2251 static bool fbnic_all_idle(struct fbnic_dev *fbd, 2252 const struct fbnic_idle_regs *regs, 2253 unsigned int nregs) 2254 { 2255 unsigned int i, j; 2256 2257 for (i = 0; i < nregs; i++) { 2258 for (j = 0; j < regs[i].reg_cnt; j++) { 2259 if (fbnic_rd32(fbd, regs[i].reg_base + j) != ~0U) 2260 return false; 2261 } 2262 } 2263 return true; 2264 } 2265 2266 static void fbnic_idle_dump(struct fbnic_dev *fbd, 2267 const struct fbnic_idle_regs *regs, 2268 unsigned int nregs, const char *dir, int err) 2269 { 2270 unsigned int i, j; 2271 2272 netdev_err(fbd->netdev, "error waiting for %s idle %d\n", dir, err); 2273 for (i = 0; i < nregs; i++) 2274 for (j = 0; j < regs[i].reg_cnt; j++) 2275 netdev_err(fbd->netdev, "0x%04x: %08x\n", 2276 regs[i].reg_base + j, 2277 fbnic_rd32(fbd, regs[i].reg_base + j)); 2278 } 2279 2280 int fbnic_wait_all_queues_idle(struct fbnic_dev *fbd, bool may_fail) 2281 { 2282 static const struct fbnic_idle_regs tx[] = { 2283 { FBNIC_QM_TWQ_IDLE(0), FBNIC_QM_TWQ_IDLE_CNT, }, 2284 { FBNIC_QM_TQS_IDLE(0), FBNIC_QM_TQS_IDLE_CNT, }, 2285 { FBNIC_QM_TDE_IDLE(0), FBNIC_QM_TDE_IDLE_CNT, }, 2286 { FBNIC_QM_TCQ_IDLE(0), FBNIC_QM_TCQ_IDLE_CNT, }, 2287 }, rx[] = { 2288 { FBNIC_QM_HPQ_IDLE(0), FBNIC_QM_HPQ_IDLE_CNT, }, 2289 { FBNIC_QM_PPQ_IDLE(0), FBNIC_QM_PPQ_IDLE_CNT, }, 2290 { FBNIC_QM_RCQ_IDLE(0), FBNIC_QM_RCQ_IDLE_CNT, }, 2291 }; 2292 bool idle; 2293 int err; 2294 2295 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2, 500000, 2296 false, fbd, tx, ARRAY_SIZE(tx)); 2297 if (err == -ETIMEDOUT) { 2298 fbnic_tx_flush(fbd); 2299 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2300 2, 500000, false, 2301 fbd, tx, ARRAY_SIZE(tx)); 2302 fbnic_tx_flush_off(fbd); 2303 } 2304 if (err) { 2305 fbnic_idle_dump(fbd, tx, ARRAY_SIZE(tx), "Tx", err); 2306 if (may_fail) 2307 return err; 2308 } 2309 2310 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2, 500000, 2311 false, fbd, rx, ARRAY_SIZE(rx)); 2312 if (err) 2313 fbnic_idle_dump(fbd, rx, ARRAY_SIZE(rx), "Rx", err); 2314 return err; 2315 } 2316 2317 static int 2318 fbnic_wait_queue_idle(struct fbnic_net *fbn, bool rx, unsigned int idx) 2319 { 2320 static const unsigned int tx_regs[] = { 2321 FBNIC_QM_TWQ_IDLE(0), FBNIC_QM_TQS_IDLE(0), 2322 FBNIC_QM_TDE_IDLE(0), FBNIC_QM_TCQ_IDLE(0), 2323 }, rx_regs[] = { 2324 FBNIC_QM_HPQ_IDLE(0), FBNIC_QM_PPQ_IDLE(0), 2325 FBNIC_QM_RCQ_IDLE(0), 2326 }; 2327 struct fbnic_dev *fbd = fbn->fbd; 2328 unsigned int val, mask, off; 2329 const unsigned int *regs; 2330 unsigned int reg_cnt; 2331 int i, err; 2332 2333 regs = rx ? rx_regs : tx_regs; 2334 reg_cnt = rx ? ARRAY_SIZE(rx_regs) : ARRAY_SIZE(tx_regs); 2335 2336 off = idx / 32; 2337 mask = BIT(idx % 32); 2338 2339 for (i = 0; i < reg_cnt; i++) { 2340 err = read_poll_timeout_atomic(fbnic_rd32, val, val & mask, 2341 2, 500000, false, 2342 fbd, regs[i] + off); 2343 if (err) { 2344 netdev_err(fbd->netdev, 2345 "wait for queue %s%d idle failed 0x%04x(%d): %08x (mask: %08x)\n", 2346 rx ? "Rx" : "Tx", idx, regs[i] + off, i, 2347 val, mask); 2348 return err; 2349 } 2350 } 2351 2352 return 0; 2353 } 2354 2355 static void fbnic_nv_flush(struct fbnic_napi_vector *nv) 2356 { 2357 int j, t; 2358 2359 /* Flush any processed Tx Queue Triads and drop the rest */ 2360 for (t = 0; t < nv->txt_count; t++) { 2361 struct fbnic_q_triad *qt = &nv->qt[t]; 2362 struct netdev_queue *tx_queue; 2363 2364 /* Clean the work queues of unprocessed work */ 2365 fbnic_clean_twq0(nv, 0, &qt->sub0, true, qt->sub0.tail); 2366 fbnic_clean_twq1(nv, false, &qt->sub1, true, 2367 qt->sub1.tail); 2368 2369 /* Reset completion queue descriptor ring */ 2370 memset(qt->cmpl.desc, 0, qt->cmpl.size); 2371 2372 /* Nothing else to do if Tx queue is disabled */ 2373 if (qt->sub0.flags & FBNIC_RING_F_DISABLED) 2374 continue; 2375 2376 /* Reset BQL associated with Tx queue */ 2377 tx_queue = netdev_get_tx_queue(nv->napi.dev, 2378 qt->sub0.q_idx); 2379 netdev_tx_reset_queue(tx_queue); 2380 } 2381 2382 /* Flush any processed Rx Queue Triads and drop the rest */ 2383 for (j = 0; j < nv->rxt_count; j++, t++) { 2384 struct fbnic_q_triad *qt = &nv->qt[t]; 2385 2386 /* Clean the work queues of unprocessed work */ 2387 fbnic_clean_bdq(&qt->sub0, qt->sub0.tail, 0); 2388 fbnic_clean_bdq(&qt->sub1, qt->sub1.tail, 0); 2389 2390 /* Reset completion queue descriptor ring */ 2391 memset(qt->cmpl.desc, 0, qt->cmpl.size); 2392 2393 fbnic_put_pkt_buff(qt, qt->cmpl.pkt, 0); 2394 memset(qt->cmpl.pkt, 0, sizeof(struct fbnic_pkt_buff)); 2395 } 2396 } 2397 2398 void fbnic_flush(struct fbnic_net *fbn) 2399 { 2400 int i; 2401 2402 for (i = 0; i < fbn->num_napi; i++) 2403 fbnic_nv_flush(fbn->napi[i]); 2404 } 2405 2406 static void fbnic_nv_fill(struct fbnic_napi_vector *nv) 2407 { 2408 int j, t; 2409 2410 /* Configure NAPI mapping and populate pages 2411 * in the BDQ rings to use for Rx 2412 */ 2413 for (j = 0, t = nv->txt_count; j < nv->rxt_count; j++, t++) { 2414 struct fbnic_q_triad *qt = &nv->qt[t]; 2415 2416 /* Populate the header and payload BDQs */ 2417 fbnic_fill_bdq(&qt->sub0); 2418 fbnic_fill_bdq(&qt->sub1); 2419 } 2420 } 2421 2422 void fbnic_fill(struct fbnic_net *fbn) 2423 { 2424 int i; 2425 2426 for (i = 0; i < fbn->num_napi; i++) 2427 fbnic_nv_fill(fbn->napi[i]); 2428 } 2429 2430 static void fbnic_enable_twq0(struct fbnic_ring *twq) 2431 { 2432 u32 log_size = fls(twq->size_mask); 2433 2434 if (!twq->size_mask) 2435 return; 2436 2437 /* Reset head/tail */ 2438 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_CTL, FBNIC_QUEUE_TWQ_CTL_RESET); 2439 twq->tail = 0; 2440 twq->head = 0; 2441 2442 /* Store descriptor ring address and size */ 2443 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_BAL, lower_32_bits(twq->dma)); 2444 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_BAH, upper_32_bits(twq->dma)); 2445 2446 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2447 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_SIZE, log_size & 0xf); 2448 2449 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_CTL, FBNIC_QUEUE_TWQ_CTL_ENABLE); 2450 } 2451 2452 static void fbnic_enable_twq1(struct fbnic_ring *twq) 2453 { 2454 u32 log_size = fls(twq->size_mask); 2455 2456 if (!twq->size_mask) 2457 return; 2458 2459 /* Reset head/tail */ 2460 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_CTL, FBNIC_QUEUE_TWQ_CTL_RESET); 2461 twq->tail = 0; 2462 twq->head = 0; 2463 2464 /* Store descriptor ring address and size */ 2465 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_BAL, lower_32_bits(twq->dma)); 2466 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_BAH, upper_32_bits(twq->dma)); 2467 2468 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2469 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_SIZE, log_size & 0xf); 2470 2471 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_CTL, FBNIC_QUEUE_TWQ_CTL_ENABLE); 2472 } 2473 2474 static void fbnic_enable_tcq(struct fbnic_napi_vector *nv, 2475 struct fbnic_ring *tcq) 2476 { 2477 u32 log_size = fls(tcq->size_mask); 2478 2479 if (!tcq->size_mask) 2480 return; 2481 2482 /* Reset head/tail */ 2483 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_CTL, FBNIC_QUEUE_TCQ_CTL_RESET); 2484 tcq->tail = 0; 2485 tcq->head = 0; 2486 2487 /* Store descriptor ring address and size */ 2488 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_BAL, lower_32_bits(tcq->dma)); 2489 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_BAH, upper_32_bits(tcq->dma)); 2490 2491 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2492 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_SIZE, log_size & 0xf); 2493 2494 /* Store interrupt information for the completion queue */ 2495 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_CTL, nv->v_idx); 2496 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_THRESHOLD, tcq->size_mask / 2); 2497 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_MASK, 0); 2498 2499 /* Enable queue */ 2500 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_CTL, FBNIC_QUEUE_TCQ_CTL_ENABLE); 2501 } 2502 2503 static void fbnic_enable_bdq(struct fbnic_ring *hpq, struct fbnic_ring *ppq) 2504 { 2505 u32 bdq_ctl = FBNIC_QUEUE_BDQ_CTL_ENABLE; 2506 u32 log_size; 2507 2508 /* Reset head/tail */ 2509 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, FBNIC_QUEUE_BDQ_CTL_RESET); 2510 ppq->tail = 0; 2511 ppq->head = 0; 2512 hpq->tail = 0; 2513 hpq->head = 0; 2514 2515 log_size = fls(hpq->size_mask); 2516 2517 /* Store descriptor ring address and size */ 2518 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_BAL, lower_32_bits(hpq->dma)); 2519 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_BAH, upper_32_bits(hpq->dma)); 2520 2521 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2522 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_SIZE, log_size & 0xf); 2523 2524 if (!ppq->size_mask) 2525 goto write_ctl; 2526 2527 log_size = fls(ppq->size_mask); 2528 2529 /* Add enabling of PPQ to BDQ control */ 2530 bdq_ctl |= FBNIC_QUEUE_BDQ_CTL_PPQ_ENABLE; 2531 2532 /* Store descriptor ring address and size */ 2533 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_BAL, lower_32_bits(ppq->dma)); 2534 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_BAH, upper_32_bits(ppq->dma)); 2535 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_SIZE, log_size & 0xf); 2536 2537 write_ctl: 2538 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, bdq_ctl); 2539 } 2540 2541 static void fbnic_config_drop_mode_rcq(struct fbnic_napi_vector *nv, 2542 struct fbnic_ring *rcq) 2543 { 2544 u32 drop_mode, rcq_ctl; 2545 2546 drop_mode = FBNIC_QUEUE_RDE_CTL0_DROP_IMMEDIATE; 2547 2548 /* Specify packet layout */ 2549 rcq_ctl = FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_DROP_MODE_MASK, drop_mode) | 2550 FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_MIN_HROOM_MASK, FBNIC_RX_HROOM) | 2551 FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_MIN_TROOM_MASK, FBNIC_RX_TROOM); 2552 2553 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RDE_CTL0, rcq_ctl); 2554 } 2555 2556 static void fbnic_config_rim_threshold(struct fbnic_ring *rcq, u16 nv_idx, u32 rx_desc) 2557 { 2558 u32 threshold; 2559 2560 /* Set the threhsold to half the ring size if rx_frames 2561 * is not configured 2562 */ 2563 threshold = rx_desc ? : rcq->size_mask / 2; 2564 2565 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_CTL, nv_idx); 2566 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_THRESHOLD, threshold); 2567 } 2568 2569 void fbnic_config_txrx_usecs(struct fbnic_napi_vector *nv, u32 arm) 2570 { 2571 struct fbnic_net *fbn = netdev_priv(nv->napi.dev); 2572 struct fbnic_dev *fbd = nv->fbd; 2573 u32 val = arm; 2574 2575 val |= FIELD_PREP(FBNIC_INTR_CQ_REARM_RCQ_TIMEOUT, fbn->rx_usecs) | 2576 FBNIC_INTR_CQ_REARM_RCQ_TIMEOUT_UPD_EN; 2577 val |= FIELD_PREP(FBNIC_INTR_CQ_REARM_TCQ_TIMEOUT, fbn->tx_usecs) | 2578 FBNIC_INTR_CQ_REARM_TCQ_TIMEOUT_UPD_EN; 2579 2580 fbnic_wr32(fbd, FBNIC_INTR_CQ_REARM(nv->v_idx), val); 2581 } 2582 2583 void fbnic_config_rx_frames(struct fbnic_napi_vector *nv) 2584 { 2585 struct fbnic_net *fbn = netdev_priv(nv->napi.dev); 2586 int i; 2587 2588 for (i = nv->txt_count; i < nv->rxt_count + nv->txt_count; i++) { 2589 struct fbnic_q_triad *qt = &nv->qt[i]; 2590 2591 fbnic_config_rim_threshold(&qt->cmpl, nv->v_idx, 2592 fbn->rx_max_frames * 2593 FBNIC_MIN_RXD_PER_FRAME); 2594 } 2595 } 2596 2597 static void fbnic_enable_rcq(struct fbnic_napi_vector *nv, 2598 struct fbnic_ring *rcq) 2599 { 2600 struct fbnic_net *fbn = netdev_priv(nv->napi.dev); 2601 u32 log_size = fls(rcq->size_mask); 2602 u32 hds_thresh = fbn->hds_thresh; 2603 u32 rcq_ctl = 0; 2604 2605 fbnic_config_drop_mode_rcq(nv, rcq); 2606 2607 /* Force lower bound on MAX_HEADER_BYTES. Below this, all frames should 2608 * be split at L4. It would also result in the frames being split at 2609 * L2/L3 depending on the frame size. 2610 */ 2611 if (fbn->hds_thresh < FBNIC_HDR_BYTES_MIN) { 2612 rcq_ctl = FBNIC_QUEUE_RDE_CTL0_EN_HDR_SPLIT; 2613 hds_thresh = FBNIC_HDR_BYTES_MIN; 2614 } 2615 2616 rcq_ctl |= FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PADLEN_MASK, FBNIC_RX_PAD) | 2617 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_MAX_HDR_MASK, hds_thresh) | 2618 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PAYLD_OFF_MASK, 2619 FBNIC_RX_PAYLD_OFFSET) | 2620 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PAYLD_PG_CL_MASK, 2621 FBNIC_RX_PAYLD_PG_CL); 2622 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RDE_CTL1, rcq_ctl); 2623 2624 /* Reset head/tail */ 2625 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_CTL, FBNIC_QUEUE_RCQ_CTL_RESET); 2626 rcq->head = 0; 2627 rcq->tail = 0; 2628 2629 /* Store descriptor ring address and size */ 2630 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_BAL, lower_32_bits(rcq->dma)); 2631 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_BAH, upper_32_bits(rcq->dma)); 2632 2633 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2634 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_SIZE, log_size & 0xf); 2635 2636 /* Store interrupt information for the completion queue */ 2637 fbnic_config_rim_threshold(rcq, nv->v_idx, fbn->rx_max_frames * 2638 FBNIC_MIN_RXD_PER_FRAME); 2639 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_MASK, 0); 2640 2641 /* Enable queue */ 2642 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_CTL, FBNIC_QUEUE_RCQ_CTL_ENABLE); 2643 } 2644 2645 static void __fbnic_nv_enable(struct fbnic_napi_vector *nv) 2646 { 2647 int j, t; 2648 2649 /* Setup Tx Queue Triads */ 2650 for (t = 0; t < nv->txt_count; t++) { 2651 struct fbnic_q_triad *qt = &nv->qt[t]; 2652 2653 fbnic_enable_twq0(&qt->sub0); 2654 fbnic_enable_twq1(&qt->sub1); 2655 fbnic_enable_tcq(nv, &qt->cmpl); 2656 } 2657 2658 /* Setup Rx Queue Triads */ 2659 for (j = 0; j < nv->rxt_count; j++, t++) { 2660 struct fbnic_q_triad *qt = &nv->qt[t]; 2661 2662 page_pool_enable_direct_recycling(qt->sub0.page_pool, 2663 &nv->napi); 2664 page_pool_enable_direct_recycling(qt->sub1.page_pool, 2665 &nv->napi); 2666 2667 fbnic_enable_bdq(&qt->sub0, &qt->sub1); 2668 fbnic_config_drop_mode_rcq(nv, &qt->cmpl); 2669 fbnic_enable_rcq(nv, &qt->cmpl); 2670 } 2671 } 2672 2673 static void fbnic_nv_enable(struct fbnic_net *fbn, struct fbnic_napi_vector *nv) 2674 { 2675 __fbnic_nv_enable(nv); 2676 fbnic_wrfl(fbn->fbd); 2677 } 2678 2679 void fbnic_enable(struct fbnic_net *fbn) 2680 { 2681 struct fbnic_dev *fbd = fbn->fbd; 2682 int i; 2683 2684 for (i = 0; i < fbn->num_napi; i++) 2685 __fbnic_nv_enable(fbn->napi[i]); 2686 2687 fbnic_wrfl(fbd); 2688 } 2689 2690 static void fbnic_nv_irq_enable(struct fbnic_napi_vector *nv) 2691 { 2692 fbnic_config_txrx_usecs(nv, FBNIC_INTR_CQ_REARM_INTR_UNMASK); 2693 } 2694 2695 void fbnic_napi_enable(struct fbnic_net *fbn) 2696 { 2697 u32 irqs[FBNIC_MAX_MSIX_VECS / 32] = {}; 2698 struct fbnic_dev *fbd = fbn->fbd; 2699 int i; 2700 2701 for (i = 0; i < fbn->num_napi; i++) { 2702 struct fbnic_napi_vector *nv = fbn->napi[i]; 2703 2704 napi_enable_locked(&nv->napi); 2705 2706 fbnic_nv_irq_enable(nv); 2707 2708 /* Record bit used for NAPI IRQs so we can 2709 * set the mask appropriately 2710 */ 2711 irqs[nv->v_idx / 32] |= BIT(nv->v_idx % 32); 2712 } 2713 2714 /* Force the first interrupt on the device to guarantee 2715 * that any packets that may have been enqueued during the 2716 * bringup are processed. 2717 */ 2718 for (i = 0; i < ARRAY_SIZE(irqs); i++) { 2719 if (!irqs[i]) 2720 continue; 2721 fbnic_wr32(fbd, FBNIC_INTR_SET(i), irqs[i]); 2722 } 2723 2724 fbnic_wrfl(fbd); 2725 } 2726 2727 void fbnic_napi_depletion_check(struct net_device *netdev) 2728 { 2729 struct fbnic_net *fbn = netdev_priv(netdev); 2730 u32 irqs[FBNIC_MAX_MSIX_VECS / 32] = {}; 2731 struct fbnic_dev *fbd = fbn->fbd; 2732 int i, j, t; 2733 2734 for (i = 0; i < fbn->num_napi; i++) { 2735 struct fbnic_napi_vector *nv = fbn->napi[i]; 2736 2737 /* Find RQs which are completely out of pages */ 2738 for (t = nv->txt_count, j = 0; j < nv->rxt_count; j++, t++) { 2739 /* Assume 4 pages is always enough to fit a packet 2740 * and therefore generate a completion and an IRQ. 2741 */ 2742 if (fbnic_desc_used(&nv->qt[t].sub0) < 4 || 2743 fbnic_desc_used(&nv->qt[t].sub1) < 4) 2744 irqs[nv->v_idx / 32] |= BIT(nv->v_idx % 32); 2745 } 2746 } 2747 2748 for (i = 0; i < ARRAY_SIZE(irqs); i++) { 2749 if (!irqs[i]) 2750 continue; 2751 fbnic_wr32(fbd, FBNIC_INTR_MASK_CLEAR(i), irqs[i]); 2752 fbnic_wr32(fbd, FBNIC_INTR_SET(i), irqs[i]); 2753 } 2754 2755 fbnic_wrfl(fbd); 2756 } 2757 2758 static int fbnic_queue_mem_alloc(struct net_device *dev, void *qmem, int idx) 2759 { 2760 struct fbnic_net *fbn = netdev_priv(dev); 2761 const struct fbnic_q_triad *real; 2762 struct fbnic_q_triad *qt = qmem; 2763 struct fbnic_napi_vector *nv; 2764 2765 if (!netif_running(dev)) 2766 return fbnic_alloc_qt_page_pools(fbn, qt, idx); 2767 2768 real = container_of(fbn->rx[idx], struct fbnic_q_triad, cmpl); 2769 nv = fbn->napi[idx % fbn->num_napi]; 2770 2771 fbnic_ring_init(&qt->sub0, real->sub0.doorbell, real->sub0.q_idx, 2772 real->sub0.flags); 2773 fbnic_ring_init(&qt->sub1, real->sub1.doorbell, real->sub1.q_idx, 2774 real->sub1.flags); 2775 fbnic_ring_init(&qt->cmpl, real->cmpl.doorbell, real->cmpl.q_idx, 2776 real->cmpl.flags); 2777 2778 return fbnic_alloc_rx_qt_resources(fbn, nv, qt); 2779 } 2780 2781 static void fbnic_queue_mem_free(struct net_device *dev, void *qmem) 2782 { 2783 struct fbnic_net *fbn = netdev_priv(dev); 2784 struct fbnic_q_triad *qt = qmem; 2785 2786 if (!netif_running(dev)) 2787 fbnic_free_qt_page_pools(qt); 2788 else 2789 fbnic_free_qt_resources(fbn, qt); 2790 } 2791 2792 static void __fbnic_nv_restart(struct fbnic_net *fbn, 2793 struct fbnic_napi_vector *nv) 2794 { 2795 struct fbnic_dev *fbd = fbn->fbd; 2796 int i; 2797 2798 fbnic_nv_enable(fbn, nv); 2799 fbnic_nv_fill(nv); 2800 2801 napi_enable_locked(&nv->napi); 2802 fbnic_nv_irq_enable(nv); 2803 fbnic_wr32(fbd, FBNIC_INTR_SET(nv->v_idx / 32), BIT(nv->v_idx % 32)); 2804 fbnic_wrfl(fbd); 2805 2806 for (i = 0; i < nv->txt_count; i++) 2807 netif_wake_subqueue(fbn->netdev, nv->qt[i].sub0.q_idx); 2808 } 2809 2810 static int fbnic_queue_start(struct net_device *dev, void *qmem, int idx) 2811 { 2812 struct fbnic_net *fbn = netdev_priv(dev); 2813 struct fbnic_napi_vector *nv; 2814 struct fbnic_q_triad *real; 2815 2816 real = container_of(fbn->rx[idx], struct fbnic_q_triad, cmpl); 2817 nv = fbn->napi[idx % fbn->num_napi]; 2818 2819 fbnic_aggregate_ring_rx_counters(fbn, &real->sub0); 2820 fbnic_aggregate_ring_rx_counters(fbn, &real->sub1); 2821 fbnic_aggregate_ring_rx_counters(fbn, &real->cmpl); 2822 2823 memcpy(real, qmem, sizeof(*real)); 2824 2825 __fbnic_nv_restart(fbn, nv); 2826 2827 return 0; 2828 } 2829 2830 static int fbnic_queue_stop(struct net_device *dev, void *qmem, int idx) 2831 { 2832 struct fbnic_net *fbn = netdev_priv(dev); 2833 const struct fbnic_q_triad *real; 2834 struct fbnic_napi_vector *nv; 2835 int i, t; 2836 int err; 2837 2838 real = container_of(fbn->rx[idx], struct fbnic_q_triad, cmpl); 2839 nv = fbn->napi[idx % fbn->num_napi]; 2840 2841 napi_disable_locked(&nv->napi); 2842 fbnic_nv_irq_disable(nv); 2843 2844 for (i = 0; i < nv->txt_count; i++) 2845 netif_stop_subqueue(dev, nv->qt[i].sub0.q_idx); 2846 fbnic_nv_disable(fbn, nv); 2847 2848 for (t = 0; t < nv->txt_count + nv->rxt_count; t++) { 2849 err = fbnic_wait_queue_idle(fbn, t >= nv->txt_count, 2850 nv->qt[t].sub0.q_idx); 2851 if (err) 2852 goto err_restart; 2853 } 2854 2855 fbnic_synchronize_irq(fbn->fbd, nv->v_idx); 2856 fbnic_nv_flush(nv); 2857 2858 page_pool_disable_direct_recycling(real->sub0.page_pool); 2859 page_pool_disable_direct_recycling(real->sub1.page_pool); 2860 2861 memcpy(qmem, real, sizeof(*real)); 2862 2863 return 0; 2864 2865 err_restart: 2866 __fbnic_nv_restart(fbn, nv); 2867 return err; 2868 } 2869 2870 const struct netdev_queue_mgmt_ops fbnic_queue_mgmt_ops = { 2871 .ndo_queue_mem_size = sizeof(struct fbnic_q_triad), 2872 .ndo_queue_mem_alloc = fbnic_queue_mem_alloc, 2873 .ndo_queue_mem_free = fbnic_queue_mem_free, 2874 .ndo_queue_start = fbnic_queue_start, 2875 .ndo_queue_stop = fbnic_queue_stop, 2876 }; 2877