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(nv->page_pool, 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 struct page *page) 719 { 720 struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx]; 721 722 page_pool_fragment_page(page, FBNIC_PAGECNT_BIAS_MAX); 723 rx_buf->pagecnt_bias = FBNIC_PAGECNT_BIAS_MAX; 724 rx_buf->page = page; 725 } 726 727 static struct page *fbnic_page_pool_get(struct fbnic_ring *ring, 728 unsigned int idx) 729 { 730 struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx]; 731 732 rx_buf->pagecnt_bias--; 733 734 return rx_buf->page; 735 } 736 737 static void fbnic_page_pool_drain(struct fbnic_ring *ring, unsigned int idx, 738 struct fbnic_napi_vector *nv, int budget) 739 { 740 struct fbnic_rx_buf *rx_buf = &ring->rx_buf[idx]; 741 struct page *page = rx_buf->page; 742 743 if (!page_pool_unref_page(page, rx_buf->pagecnt_bias)) 744 page_pool_put_unrefed_page(nv->page_pool, page, -1, !!budget); 745 746 rx_buf->page = NULL; 747 } 748 749 static void fbnic_clean_twq(struct fbnic_napi_vector *nv, int napi_budget, 750 struct fbnic_q_triad *qt, s32 ts_head, s32 head0, 751 s32 head1) 752 { 753 if (head0 >= 0) 754 fbnic_clean_twq0(nv, napi_budget, &qt->sub0, false, head0); 755 else if (ts_head >= 0) 756 fbnic_clean_twq0(nv, napi_budget, &qt->sub0, false, ts_head); 757 758 if (head1 >= 0) { 759 qt->cmpl.deferred_head = -1; 760 if (napi_budget) 761 fbnic_clean_twq1(nv, true, &qt->sub1, false, head1); 762 else 763 qt->cmpl.deferred_head = head1; 764 } 765 } 766 767 static void 768 fbnic_clean_tcq(struct fbnic_napi_vector *nv, struct fbnic_q_triad *qt, 769 int napi_budget) 770 { 771 struct fbnic_ring *cmpl = &qt->cmpl; 772 s32 head1 = cmpl->deferred_head; 773 s32 head0 = -1, ts_head = -1; 774 __le64 *raw_tcd, done; 775 u32 head = cmpl->head; 776 777 done = (head & (cmpl->size_mask + 1)) ? 0 : cpu_to_le64(FBNIC_TCD_DONE); 778 raw_tcd = &cmpl->desc[head & cmpl->size_mask]; 779 780 /* Walk the completion queue collecting the heads reported by NIC */ 781 while ((*raw_tcd & cpu_to_le64(FBNIC_TCD_DONE)) == done) { 782 u64 tcd; 783 784 dma_rmb(); 785 786 tcd = le64_to_cpu(*raw_tcd); 787 788 switch (FIELD_GET(FBNIC_TCD_TYPE_MASK, tcd)) { 789 case FBNIC_TCD_TYPE_0: 790 if (tcd & FBNIC_TCD_TWQ1) 791 head1 = FIELD_GET(FBNIC_TCD_TYPE0_HEAD1_MASK, 792 tcd); 793 else 794 head0 = FIELD_GET(FBNIC_TCD_TYPE0_HEAD0_MASK, 795 tcd); 796 /* Currently all err status bits are related to 797 * timestamps and as those have yet to be added 798 * they are skipped for now. 799 */ 800 break; 801 case FBNIC_TCD_TYPE_1: 802 if (WARN_ON_ONCE(tcd & FBNIC_TCD_TWQ1)) 803 break; 804 805 fbnic_clean_tsq(nv, &qt->sub0, tcd, &ts_head, &head0); 806 break; 807 default: 808 break; 809 } 810 811 raw_tcd++; 812 head++; 813 if (!(head & cmpl->size_mask)) { 814 done ^= cpu_to_le64(FBNIC_TCD_DONE); 815 raw_tcd = &cmpl->desc[0]; 816 } 817 } 818 819 /* Record the current head/tail of the queue */ 820 if (cmpl->head != head) { 821 cmpl->head = head; 822 writel(head & cmpl->size_mask, cmpl->doorbell); 823 } 824 825 /* Unmap and free processed buffers */ 826 fbnic_clean_twq(nv, napi_budget, qt, ts_head, head0, head1); 827 } 828 829 static void fbnic_clean_bdq(struct fbnic_napi_vector *nv, int napi_budget, 830 struct fbnic_ring *ring, unsigned int hw_head) 831 { 832 unsigned int head = ring->head; 833 834 if (head == hw_head) 835 return; 836 837 do { 838 fbnic_page_pool_drain(ring, head, nv, napi_budget); 839 840 head++; 841 head &= ring->size_mask; 842 } while (head != hw_head); 843 844 ring->head = head; 845 } 846 847 static void fbnic_bd_prep(struct fbnic_ring *bdq, u16 id, struct page *page) 848 { 849 __le64 *bdq_desc = &bdq->desc[id * FBNIC_BD_FRAG_COUNT]; 850 dma_addr_t dma = page_pool_get_dma_addr(page); 851 u64 bd, i = FBNIC_BD_FRAG_COUNT; 852 853 bd = (FBNIC_BD_PAGE_ADDR_MASK & dma) | 854 FIELD_PREP(FBNIC_BD_PAGE_ID_MASK, id); 855 856 /* In the case that a page size is larger than 4K we will map a 857 * single page to multiple fragments. The fragments will be 858 * FBNIC_BD_FRAG_COUNT in size and the lower n bits will be use 859 * to indicate the individual fragment IDs. 860 */ 861 do { 862 *bdq_desc = cpu_to_le64(bd); 863 bd += FIELD_PREP(FBNIC_BD_DESC_ADDR_MASK, 1) | 864 FIELD_PREP(FBNIC_BD_DESC_ID_MASK, 1); 865 } while (--i); 866 } 867 868 static void fbnic_fill_bdq(struct fbnic_napi_vector *nv, struct fbnic_ring *bdq) 869 { 870 unsigned int count = fbnic_desc_unused(bdq); 871 unsigned int i = bdq->tail; 872 873 if (!count) 874 return; 875 876 do { 877 struct page *page; 878 879 page = page_pool_dev_alloc_pages(nv->page_pool); 880 if (!page) { 881 u64_stats_update_begin(&bdq->stats.syncp); 882 bdq->stats.rx.alloc_failed++; 883 u64_stats_update_end(&bdq->stats.syncp); 884 885 break; 886 } 887 888 fbnic_page_pool_init(bdq, i, page); 889 fbnic_bd_prep(bdq, i, page); 890 891 i++; 892 i &= bdq->size_mask; 893 894 count--; 895 } while (count); 896 897 if (bdq->tail != i) { 898 bdq->tail = i; 899 900 /* Force DMA writes to flush before writing to tail */ 901 dma_wmb(); 902 903 writel(i, bdq->doorbell); 904 } 905 } 906 907 static unsigned int fbnic_hdr_pg_start(unsigned int pg_off) 908 { 909 /* The headroom of the first header may be larger than FBNIC_RX_HROOM 910 * due to alignment. So account for that by just making the page 911 * offset 0 if we are starting at the first header. 912 */ 913 if (ALIGN(FBNIC_RX_HROOM, 128) > FBNIC_RX_HROOM && 914 pg_off == ALIGN(FBNIC_RX_HROOM, 128)) 915 return 0; 916 917 return pg_off - FBNIC_RX_HROOM; 918 } 919 920 static unsigned int fbnic_hdr_pg_end(unsigned int pg_off, unsigned int len) 921 { 922 /* Determine the end of the buffer by finding the start of the next 923 * and then subtracting the headroom from that frame. 924 */ 925 pg_off += len + FBNIC_RX_TROOM + FBNIC_RX_HROOM; 926 927 return ALIGN(pg_off, 128) - FBNIC_RX_HROOM; 928 } 929 930 static void fbnic_pkt_prepare(struct fbnic_napi_vector *nv, u64 rcd, 931 struct fbnic_pkt_buff *pkt, 932 struct fbnic_q_triad *qt) 933 { 934 unsigned int hdr_pg_idx = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd); 935 unsigned int hdr_pg_off = FIELD_GET(FBNIC_RCD_AL_BUFF_OFF_MASK, rcd); 936 struct page *page = fbnic_page_pool_get(&qt->sub0, hdr_pg_idx); 937 unsigned int len = FIELD_GET(FBNIC_RCD_AL_BUFF_LEN_MASK, rcd); 938 unsigned int frame_sz, hdr_pg_start, hdr_pg_end, headroom; 939 unsigned char *hdr_start; 940 941 /* data_hard_start should always be NULL when this is called */ 942 WARN_ON_ONCE(pkt->buff.data_hard_start); 943 944 /* Short-cut the end calculation if we know page is fully consumed */ 945 hdr_pg_end = FIELD_GET(FBNIC_RCD_AL_PAGE_FIN, rcd) ? 946 FBNIC_BD_FRAG_SIZE : fbnic_hdr_pg_end(hdr_pg_off, len); 947 hdr_pg_start = fbnic_hdr_pg_start(hdr_pg_off); 948 949 headroom = hdr_pg_off - hdr_pg_start + FBNIC_RX_PAD; 950 frame_sz = hdr_pg_end - hdr_pg_start; 951 xdp_init_buff(&pkt->buff, frame_sz, &qt->xdp_rxq); 952 hdr_pg_start += (FBNIC_RCD_AL_BUFF_FRAG_MASK & rcd) * 953 FBNIC_BD_FRAG_SIZE; 954 955 /* Sync DMA buffer */ 956 dma_sync_single_range_for_cpu(nv->dev, page_pool_get_dma_addr(page), 957 hdr_pg_start, frame_sz, 958 DMA_BIDIRECTIONAL); 959 960 /* Build frame around buffer */ 961 hdr_start = page_address(page) + hdr_pg_start; 962 net_prefetch(pkt->buff.data); 963 xdp_prepare_buff(&pkt->buff, hdr_start, headroom, 964 len - FBNIC_RX_PAD, true); 965 966 pkt->hwtstamp = 0; 967 pkt->add_frag_failed = false; 968 } 969 970 static void fbnic_add_rx_frag(struct fbnic_napi_vector *nv, u64 rcd, 971 struct fbnic_pkt_buff *pkt, 972 struct fbnic_q_triad *qt) 973 { 974 unsigned int pg_idx = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd); 975 unsigned int pg_off = FIELD_GET(FBNIC_RCD_AL_BUFF_OFF_MASK, rcd); 976 unsigned int len = FIELD_GET(FBNIC_RCD_AL_BUFF_LEN_MASK, rcd); 977 struct page *page = fbnic_page_pool_get(&qt->sub1, pg_idx); 978 unsigned int truesize; 979 bool added; 980 981 truesize = FIELD_GET(FBNIC_RCD_AL_PAGE_FIN, rcd) ? 982 FBNIC_BD_FRAG_SIZE - pg_off : ALIGN(len, 128); 983 984 pg_off += (FBNIC_RCD_AL_BUFF_FRAG_MASK & rcd) * 985 FBNIC_BD_FRAG_SIZE; 986 987 /* Sync DMA buffer */ 988 dma_sync_single_range_for_cpu(nv->dev, page_pool_get_dma_addr(page), 989 pg_off, truesize, DMA_BIDIRECTIONAL); 990 991 added = xdp_buff_add_frag(&pkt->buff, page_to_netmem(page), pg_off, len, 992 truesize); 993 if (unlikely(!added)) { 994 pkt->add_frag_failed = true; 995 netdev_err_once(nv->napi.dev, 996 "Failed to add fragment to xdp_buff\n"); 997 } 998 } 999 1000 static void fbnic_put_pkt_buff(struct fbnic_napi_vector *nv, 1001 struct fbnic_pkt_buff *pkt, int budget) 1002 { 1003 struct page *page; 1004 1005 if (!pkt->buff.data_hard_start) 1006 return; 1007 1008 if (xdp_buff_has_frags(&pkt->buff)) { 1009 struct skb_shared_info *shinfo; 1010 int nr_frags; 1011 1012 shinfo = xdp_get_shared_info_from_buff(&pkt->buff); 1013 nr_frags = shinfo->nr_frags; 1014 1015 while (nr_frags--) { 1016 page = skb_frag_page(&shinfo->frags[nr_frags]); 1017 page_pool_put_full_page(nv->page_pool, page, !!budget); 1018 } 1019 } 1020 1021 page = virt_to_page(pkt->buff.data_hard_start); 1022 page_pool_put_full_page(nv->page_pool, page, !!budget); 1023 } 1024 1025 static struct sk_buff *fbnic_build_skb(struct fbnic_napi_vector *nv, 1026 struct fbnic_pkt_buff *pkt) 1027 { 1028 struct sk_buff *skb; 1029 1030 skb = xdp_build_skb_from_buff(&pkt->buff); 1031 if (!skb) 1032 return NULL; 1033 1034 /* Add timestamp if present */ 1035 if (pkt->hwtstamp) 1036 skb_hwtstamps(skb)->hwtstamp = pkt->hwtstamp; 1037 1038 return skb; 1039 } 1040 1041 static long fbnic_pkt_tx(struct fbnic_napi_vector *nv, 1042 struct fbnic_pkt_buff *pkt) 1043 { 1044 struct fbnic_ring *ring = &nv->qt[0].sub1; 1045 int size, offset, nsegs = 1, data_len = 0; 1046 unsigned int tail = ring->tail; 1047 struct skb_shared_info *shinfo; 1048 skb_frag_t *frag = NULL; 1049 struct page *page; 1050 dma_addr_t dma; 1051 __le64 *twd; 1052 1053 if (unlikely(xdp_buff_has_frags(&pkt->buff))) { 1054 shinfo = xdp_get_shared_info_from_buff(&pkt->buff); 1055 nsegs += shinfo->nr_frags; 1056 data_len = shinfo->xdp_frags_size; 1057 frag = &shinfo->frags[0]; 1058 } 1059 1060 if (fbnic_desc_unused(ring) < nsegs) { 1061 u64_stats_update_begin(&ring->stats.syncp); 1062 ring->stats.dropped++; 1063 u64_stats_update_end(&ring->stats.syncp); 1064 return -FBNIC_XDP_CONSUME; 1065 } 1066 1067 page = virt_to_page(pkt->buff.data_hard_start); 1068 offset = offset_in_page(pkt->buff.data); 1069 dma = page_pool_get_dma_addr(page); 1070 1071 size = pkt->buff.data_end - pkt->buff.data; 1072 1073 while (nsegs--) { 1074 dma_sync_single_range_for_device(nv->dev, dma, offset, size, 1075 DMA_BIDIRECTIONAL); 1076 dma += offset; 1077 1078 ring->tx_buf[tail] = page; 1079 1080 twd = &ring->desc[tail]; 1081 *twd = cpu_to_le64(FIELD_PREP(FBNIC_TWD_ADDR_MASK, dma) | 1082 FIELD_PREP(FBNIC_TWD_LEN_MASK, size) | 1083 FIELD_PREP(FBNIC_TWD_TYPE_MASK, 1084 FBNIC_TWD_TYPE_AL)); 1085 1086 tail++; 1087 tail &= ring->size_mask; 1088 1089 if (!data_len) 1090 break; 1091 1092 offset = skb_frag_off(frag); 1093 page = skb_frag_page(frag); 1094 dma = page_pool_get_dma_addr(page); 1095 1096 size = skb_frag_size(frag); 1097 data_len -= size; 1098 frag++; 1099 } 1100 1101 *twd |= FBNIC_TWD_TYPE(LAST_AL); 1102 1103 ring->tail = tail; 1104 1105 return -FBNIC_XDP_TX; 1106 } 1107 1108 static void fbnic_pkt_commit_tail(struct fbnic_napi_vector *nv, 1109 unsigned int pkt_tail) 1110 { 1111 struct fbnic_ring *ring = &nv->qt[0].sub1; 1112 1113 /* Force DMA writes to flush before writing to tail */ 1114 dma_wmb(); 1115 1116 writel(pkt_tail, ring->doorbell); 1117 } 1118 1119 static struct sk_buff *fbnic_run_xdp(struct fbnic_napi_vector *nv, 1120 struct fbnic_pkt_buff *pkt) 1121 { 1122 struct fbnic_net *fbn = netdev_priv(nv->napi.dev); 1123 struct bpf_prog *xdp_prog; 1124 int act; 1125 1126 xdp_prog = READ_ONCE(fbn->xdp_prog); 1127 if (!xdp_prog) 1128 goto xdp_pass; 1129 1130 /* Should never happen, config paths enforce HDS threshold > MTU */ 1131 if (xdp_buff_has_frags(&pkt->buff) && !xdp_prog->aux->xdp_has_frags) 1132 return ERR_PTR(-FBNIC_XDP_LEN_ERR); 1133 1134 act = bpf_prog_run_xdp(xdp_prog, &pkt->buff); 1135 switch (act) { 1136 case XDP_PASS: 1137 xdp_pass: 1138 return fbnic_build_skb(nv, pkt); 1139 case XDP_TX: 1140 return ERR_PTR(fbnic_pkt_tx(nv, pkt)); 1141 default: 1142 bpf_warn_invalid_xdp_action(nv->napi.dev, xdp_prog, act); 1143 fallthrough; 1144 case XDP_ABORTED: 1145 trace_xdp_exception(nv->napi.dev, xdp_prog, act); 1146 fallthrough; 1147 case XDP_DROP: 1148 break; 1149 } 1150 1151 return ERR_PTR(-FBNIC_XDP_CONSUME); 1152 } 1153 1154 static enum pkt_hash_types fbnic_skb_hash_type(u64 rcd) 1155 { 1156 return (FBNIC_RCD_META_L4_TYPE_MASK & rcd) ? PKT_HASH_TYPE_L4 : 1157 (FBNIC_RCD_META_L3_TYPE_MASK & rcd) ? PKT_HASH_TYPE_L3 : 1158 PKT_HASH_TYPE_L2; 1159 } 1160 1161 static void fbnic_rx_tstamp(struct fbnic_napi_vector *nv, u64 rcd, 1162 struct fbnic_pkt_buff *pkt) 1163 { 1164 struct fbnic_net *fbn; 1165 u64 ns, ts; 1166 1167 if (!FIELD_GET(FBNIC_RCD_OPT_META_TS, rcd)) 1168 return; 1169 1170 fbn = netdev_priv(nv->napi.dev); 1171 ts = FIELD_GET(FBNIC_RCD_OPT_META_TS_MASK, rcd); 1172 ns = fbnic_ts40_to_ns(fbn, ts); 1173 1174 /* Add timestamp to shared info */ 1175 pkt->hwtstamp = ns_to_ktime(ns); 1176 } 1177 1178 static void fbnic_populate_skb_fields(struct fbnic_napi_vector *nv, 1179 u64 rcd, struct sk_buff *skb, 1180 struct fbnic_q_triad *qt, 1181 u64 *csum_cmpl, u64 *csum_none) 1182 { 1183 struct net_device *netdev = nv->napi.dev; 1184 struct fbnic_ring *rcq = &qt->cmpl; 1185 1186 fbnic_rx_csum(rcd, skb, rcq, csum_cmpl, csum_none); 1187 1188 if (netdev->features & NETIF_F_RXHASH) 1189 skb_set_hash(skb, 1190 FIELD_GET(FBNIC_RCD_META_RSS_HASH_MASK, rcd), 1191 fbnic_skb_hash_type(rcd)); 1192 1193 skb_record_rx_queue(skb, rcq->q_idx); 1194 } 1195 1196 static bool fbnic_rcd_metadata_err(u64 rcd) 1197 { 1198 return !!(FBNIC_RCD_META_UNCORRECTABLE_ERR_MASK & rcd); 1199 } 1200 1201 static int fbnic_clean_rcq(struct fbnic_napi_vector *nv, 1202 struct fbnic_q_triad *qt, int budget) 1203 { 1204 unsigned int packets = 0, bytes = 0, dropped = 0, alloc_failed = 0; 1205 u64 csum_complete = 0, csum_none = 0, length_errors = 0; 1206 s32 head0 = -1, head1 = -1, pkt_tail = -1; 1207 struct fbnic_ring *rcq = &qt->cmpl; 1208 struct fbnic_pkt_buff *pkt; 1209 __le64 *raw_rcd, done; 1210 u32 head = rcq->head; 1211 1212 done = (head & (rcq->size_mask + 1)) ? cpu_to_le64(FBNIC_RCD_DONE) : 0; 1213 raw_rcd = &rcq->desc[head & rcq->size_mask]; 1214 pkt = rcq->pkt; 1215 1216 /* Walk the completion queue collecting the heads reported by NIC */ 1217 while (likely(packets < budget)) { 1218 struct sk_buff *skb = ERR_PTR(-EINVAL); 1219 u64 rcd; 1220 1221 if ((*raw_rcd & cpu_to_le64(FBNIC_RCD_DONE)) == done) 1222 break; 1223 1224 dma_rmb(); 1225 1226 rcd = le64_to_cpu(*raw_rcd); 1227 1228 switch (FIELD_GET(FBNIC_RCD_TYPE_MASK, rcd)) { 1229 case FBNIC_RCD_TYPE_HDR_AL: 1230 head0 = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd); 1231 fbnic_pkt_prepare(nv, rcd, pkt, qt); 1232 1233 break; 1234 case FBNIC_RCD_TYPE_PAY_AL: 1235 head1 = FIELD_GET(FBNIC_RCD_AL_BUFF_PAGE_MASK, rcd); 1236 fbnic_add_rx_frag(nv, rcd, pkt, qt); 1237 1238 break; 1239 case FBNIC_RCD_TYPE_OPT_META: 1240 /* Only type 0 is currently supported */ 1241 if (FIELD_GET(FBNIC_RCD_OPT_META_TYPE_MASK, rcd)) 1242 break; 1243 1244 fbnic_rx_tstamp(nv, rcd, pkt); 1245 1246 /* We currently ignore the action table index */ 1247 break; 1248 case FBNIC_RCD_TYPE_META: 1249 if (unlikely(pkt->add_frag_failed)) 1250 skb = NULL; 1251 else if (likely(!fbnic_rcd_metadata_err(rcd))) 1252 skb = fbnic_run_xdp(nv, pkt); 1253 1254 /* Populate skb and invalidate XDP */ 1255 if (!IS_ERR_OR_NULL(skb)) { 1256 fbnic_populate_skb_fields(nv, rcd, skb, qt, 1257 &csum_complete, 1258 &csum_none); 1259 1260 packets++; 1261 bytes += skb->len; 1262 1263 napi_gro_receive(&nv->napi, skb); 1264 } else if (skb == ERR_PTR(-FBNIC_XDP_TX)) { 1265 pkt_tail = nv->qt[0].sub1.tail; 1266 bytes += xdp_get_buff_len(&pkt->buff); 1267 } else { 1268 if (!skb) { 1269 alloc_failed++; 1270 dropped++; 1271 } else if (skb == ERR_PTR(-FBNIC_XDP_LEN_ERR)) { 1272 length_errors++; 1273 } else { 1274 dropped++; 1275 } 1276 1277 fbnic_put_pkt_buff(nv, pkt, 1); 1278 } 1279 1280 pkt->buff.data_hard_start = NULL; 1281 1282 break; 1283 } 1284 1285 raw_rcd++; 1286 head++; 1287 if (!(head & rcq->size_mask)) { 1288 done ^= cpu_to_le64(FBNIC_RCD_DONE); 1289 raw_rcd = &rcq->desc[0]; 1290 } 1291 } 1292 1293 u64_stats_update_begin(&rcq->stats.syncp); 1294 rcq->stats.packets += packets; 1295 rcq->stats.bytes += bytes; 1296 /* Re-add ethernet header length (removed in fbnic_build_skb) */ 1297 rcq->stats.bytes += ETH_HLEN * packets; 1298 rcq->stats.dropped += dropped; 1299 rcq->stats.rx.alloc_failed += alloc_failed; 1300 rcq->stats.rx.csum_complete += csum_complete; 1301 rcq->stats.rx.csum_none += csum_none; 1302 rcq->stats.rx.length_errors += length_errors; 1303 u64_stats_update_end(&rcq->stats.syncp); 1304 1305 if (pkt_tail >= 0) 1306 fbnic_pkt_commit_tail(nv, pkt_tail); 1307 1308 /* Unmap and free processed buffers */ 1309 if (head0 >= 0) 1310 fbnic_clean_bdq(nv, budget, &qt->sub0, head0); 1311 fbnic_fill_bdq(nv, &qt->sub0); 1312 1313 if (head1 >= 0) 1314 fbnic_clean_bdq(nv, budget, &qt->sub1, head1); 1315 fbnic_fill_bdq(nv, &qt->sub1); 1316 1317 /* Record the current head/tail of the queue */ 1318 if (rcq->head != head) { 1319 rcq->head = head; 1320 writel(head & rcq->size_mask, rcq->doorbell); 1321 } 1322 1323 return packets; 1324 } 1325 1326 static void fbnic_nv_irq_disable(struct fbnic_napi_vector *nv) 1327 { 1328 struct fbnic_dev *fbd = nv->fbd; 1329 u32 v_idx = nv->v_idx; 1330 1331 fbnic_wr32(fbd, FBNIC_INTR_MASK_SET(v_idx / 32), 1 << (v_idx % 32)); 1332 } 1333 1334 static void fbnic_nv_irq_rearm(struct fbnic_napi_vector *nv) 1335 { 1336 struct fbnic_dev *fbd = nv->fbd; 1337 u32 v_idx = nv->v_idx; 1338 1339 fbnic_wr32(fbd, FBNIC_INTR_CQ_REARM(v_idx), 1340 FBNIC_INTR_CQ_REARM_INTR_UNMASK); 1341 } 1342 1343 static int fbnic_poll(struct napi_struct *napi, int budget) 1344 { 1345 struct fbnic_napi_vector *nv = container_of(napi, 1346 struct fbnic_napi_vector, 1347 napi); 1348 int i, j, work_done = 0; 1349 1350 for (i = 0; i < nv->txt_count; i++) 1351 fbnic_clean_tcq(nv, &nv->qt[i], budget); 1352 1353 for (j = 0; j < nv->rxt_count; j++, i++) 1354 work_done += fbnic_clean_rcq(nv, &nv->qt[i], budget); 1355 1356 if (work_done >= budget) 1357 return budget; 1358 1359 if (likely(napi_complete_done(napi, work_done))) 1360 fbnic_nv_irq_rearm(nv); 1361 1362 return work_done; 1363 } 1364 1365 irqreturn_t fbnic_msix_clean_rings(int __always_unused irq, void *data) 1366 { 1367 struct fbnic_napi_vector *nv = *(void **)data; 1368 1369 napi_schedule_irqoff(&nv->napi); 1370 1371 return IRQ_HANDLED; 1372 } 1373 1374 void fbnic_aggregate_ring_rx_counters(struct fbnic_net *fbn, 1375 struct fbnic_ring *rxr) 1376 { 1377 struct fbnic_queue_stats *stats = &rxr->stats; 1378 1379 /* Capture stats from queues before dissasociating them */ 1380 fbn->rx_stats.bytes += stats->bytes; 1381 fbn->rx_stats.packets += stats->packets; 1382 fbn->rx_stats.dropped += stats->dropped; 1383 fbn->rx_stats.rx.alloc_failed += stats->rx.alloc_failed; 1384 fbn->rx_stats.rx.csum_complete += stats->rx.csum_complete; 1385 fbn->rx_stats.rx.csum_none += stats->rx.csum_none; 1386 fbn->rx_stats.rx.length_errors += stats->rx.length_errors; 1387 /* Remember to add new stats here */ 1388 BUILD_BUG_ON(sizeof(fbn->rx_stats.rx) / 8 != 4); 1389 } 1390 1391 void fbnic_aggregate_ring_tx_counters(struct fbnic_net *fbn, 1392 struct fbnic_ring *txr) 1393 { 1394 struct fbnic_queue_stats *stats = &txr->stats; 1395 1396 /* Capture stats from queues before dissasociating them */ 1397 fbn->tx_stats.bytes += stats->bytes; 1398 fbn->tx_stats.packets += stats->packets; 1399 fbn->tx_stats.dropped += stats->dropped; 1400 fbn->tx_stats.twq.csum_partial += stats->twq.csum_partial; 1401 fbn->tx_stats.twq.lso += stats->twq.lso; 1402 fbn->tx_stats.twq.ts_lost += stats->twq.ts_lost; 1403 fbn->tx_stats.twq.ts_packets += stats->twq.ts_packets; 1404 fbn->tx_stats.twq.stop += stats->twq.stop; 1405 fbn->tx_stats.twq.wake += stats->twq.wake; 1406 /* Remember to add new stats here */ 1407 BUILD_BUG_ON(sizeof(fbn->tx_stats.twq) / 8 != 6); 1408 } 1409 1410 static void fbnic_aggregate_ring_xdp_counters(struct fbnic_net *fbn, 1411 struct fbnic_ring *xdpr) 1412 { 1413 struct fbnic_queue_stats *stats = &xdpr->stats; 1414 1415 if (!(xdpr->flags & FBNIC_RING_F_STATS)) 1416 return; 1417 1418 /* Capture stats from queues before dissasociating them */ 1419 fbn->rx_stats.bytes += stats->bytes; 1420 fbn->rx_stats.packets += stats->packets; 1421 fbn->rx_stats.dropped += stats->dropped; 1422 fbn->tx_stats.bytes += stats->bytes; 1423 fbn->tx_stats.packets += stats->packets; 1424 } 1425 1426 static void fbnic_remove_tx_ring(struct fbnic_net *fbn, 1427 struct fbnic_ring *txr) 1428 { 1429 if (!(txr->flags & FBNIC_RING_F_STATS)) 1430 return; 1431 1432 fbnic_aggregate_ring_tx_counters(fbn, txr); 1433 1434 /* Remove pointer to the Tx ring */ 1435 WARN_ON(fbn->tx[txr->q_idx] && fbn->tx[txr->q_idx] != txr); 1436 fbn->tx[txr->q_idx] = NULL; 1437 } 1438 1439 static void fbnic_remove_xdp_ring(struct fbnic_net *fbn, 1440 struct fbnic_ring *xdpr) 1441 { 1442 if (!(xdpr->flags & FBNIC_RING_F_STATS)) 1443 return; 1444 1445 fbnic_aggregate_ring_xdp_counters(fbn, xdpr); 1446 1447 /* Remove pointer to the Tx ring */ 1448 WARN_ON(fbn->tx[xdpr->q_idx] && fbn->tx[xdpr->q_idx] != xdpr); 1449 fbn->tx[xdpr->q_idx] = NULL; 1450 } 1451 1452 static void fbnic_remove_rx_ring(struct fbnic_net *fbn, 1453 struct fbnic_ring *rxr) 1454 { 1455 if (!(rxr->flags & FBNIC_RING_F_STATS)) 1456 return; 1457 1458 fbnic_aggregate_ring_rx_counters(fbn, rxr); 1459 1460 /* Remove pointer to the Rx ring */ 1461 WARN_ON(fbn->rx[rxr->q_idx] && fbn->rx[rxr->q_idx] != rxr); 1462 fbn->rx[rxr->q_idx] = NULL; 1463 } 1464 1465 static void fbnic_free_napi_vector(struct fbnic_net *fbn, 1466 struct fbnic_napi_vector *nv) 1467 { 1468 struct fbnic_dev *fbd = nv->fbd; 1469 int i, j; 1470 1471 for (i = 0; i < nv->txt_count; i++) { 1472 fbnic_remove_tx_ring(fbn, &nv->qt[i].sub0); 1473 fbnic_remove_xdp_ring(fbn, &nv->qt[i].sub1); 1474 fbnic_remove_tx_ring(fbn, &nv->qt[i].cmpl); 1475 } 1476 1477 for (j = 0; j < nv->rxt_count; j++, i++) { 1478 xdp_rxq_info_unreg(&nv->qt[i].xdp_rxq); 1479 fbnic_remove_rx_ring(fbn, &nv->qt[i].sub0); 1480 fbnic_remove_rx_ring(fbn, &nv->qt[i].sub1); 1481 fbnic_remove_rx_ring(fbn, &nv->qt[i].cmpl); 1482 } 1483 1484 fbnic_napi_free_irq(fbd, nv); 1485 page_pool_destroy(nv->page_pool); 1486 netif_napi_del(&nv->napi); 1487 fbn->napi[fbnic_napi_idx(nv)] = NULL; 1488 kfree(nv); 1489 } 1490 1491 void fbnic_free_napi_vectors(struct fbnic_net *fbn) 1492 { 1493 int i; 1494 1495 for (i = 0; i < fbn->num_napi; i++) 1496 if (fbn->napi[i]) 1497 fbnic_free_napi_vector(fbn, fbn->napi[i]); 1498 } 1499 1500 #define FBNIC_PAGE_POOL_FLAGS \ 1501 (PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV) 1502 1503 static int fbnic_alloc_nv_page_pool(struct fbnic_net *fbn, 1504 struct fbnic_napi_vector *nv) 1505 { 1506 struct page_pool_params pp_params = { 1507 .order = 0, 1508 .flags = FBNIC_PAGE_POOL_FLAGS, 1509 .pool_size = (fbn->hpq_size + fbn->ppq_size) * nv->rxt_count, 1510 .nid = NUMA_NO_NODE, 1511 .dev = nv->dev, 1512 .dma_dir = DMA_BIDIRECTIONAL, 1513 .offset = 0, 1514 .max_len = PAGE_SIZE, 1515 .napi = &nv->napi, 1516 .netdev = fbn->netdev, 1517 }; 1518 struct page_pool *pp; 1519 1520 /* Page pool cannot exceed a size of 32768. This doesn't limit the 1521 * pages on the ring but the number we can have cached waiting on 1522 * the next use. 1523 * 1524 * TBD: Can this be reduced further? Would a multiple of 1525 * NAPI_POLL_WEIGHT possibly make more sense? The question is how 1526 * may pages do we need to hold in reserve to get the best return 1527 * without hogging too much system memory. 1528 */ 1529 if (pp_params.pool_size > 32768) 1530 pp_params.pool_size = 32768; 1531 1532 pp = page_pool_create(&pp_params); 1533 if (IS_ERR(pp)) 1534 return PTR_ERR(pp); 1535 1536 nv->page_pool = pp; 1537 1538 return 0; 1539 } 1540 1541 static void fbnic_ring_init(struct fbnic_ring *ring, u32 __iomem *doorbell, 1542 int q_idx, u8 flags) 1543 { 1544 u64_stats_init(&ring->stats.syncp); 1545 ring->doorbell = doorbell; 1546 ring->q_idx = q_idx; 1547 ring->flags = flags; 1548 ring->deferred_head = -1; 1549 } 1550 1551 static int fbnic_alloc_napi_vector(struct fbnic_dev *fbd, struct fbnic_net *fbn, 1552 unsigned int v_count, unsigned int v_idx, 1553 unsigned int txq_count, unsigned int txq_idx, 1554 unsigned int rxq_count, unsigned int rxq_idx) 1555 { 1556 int txt_count = txq_count, rxt_count = rxq_count; 1557 u32 __iomem *uc_addr = fbd->uc_addr0; 1558 int xdp_count = 0, qt_count, err; 1559 struct fbnic_napi_vector *nv; 1560 struct fbnic_q_triad *qt; 1561 u32 __iomem *db; 1562 1563 /* We need to reserve at least one Tx Queue Triad for an XDP ring */ 1564 if (rxq_count) { 1565 xdp_count = 1; 1566 if (!txt_count) 1567 txt_count = 1; 1568 } 1569 1570 qt_count = txt_count + rxq_count; 1571 if (!qt_count) 1572 return -EINVAL; 1573 1574 /* If MMIO has already failed there are no rings to initialize */ 1575 if (!uc_addr) 1576 return -EIO; 1577 1578 /* Allocate NAPI vector and queue triads */ 1579 nv = kzalloc(struct_size(nv, qt, qt_count), GFP_KERNEL); 1580 if (!nv) 1581 return -ENOMEM; 1582 1583 /* Record queue triad counts */ 1584 nv->txt_count = txt_count; 1585 nv->rxt_count = rxt_count; 1586 1587 /* Provide pointer back to fbnic and MSI-X vectors */ 1588 nv->fbd = fbd; 1589 nv->v_idx = v_idx; 1590 1591 /* Tie napi to netdev */ 1592 fbn->napi[fbnic_napi_idx(nv)] = nv; 1593 netif_napi_add(fbn->netdev, &nv->napi, fbnic_poll); 1594 1595 /* Record IRQ to NAPI struct */ 1596 netif_napi_set_irq(&nv->napi, 1597 pci_irq_vector(to_pci_dev(fbd->dev), nv->v_idx)); 1598 1599 /* Tie nv back to PCIe dev */ 1600 nv->dev = fbd->dev; 1601 1602 /* Allocate page pool */ 1603 if (rxq_count) { 1604 err = fbnic_alloc_nv_page_pool(fbn, nv); 1605 if (err) 1606 goto napi_del; 1607 } 1608 1609 /* Request the IRQ for napi vector */ 1610 err = fbnic_napi_request_irq(fbd, nv); 1611 if (err) 1612 goto pp_destroy; 1613 1614 /* Initialize queue triads */ 1615 qt = nv->qt; 1616 1617 while (txt_count) { 1618 u8 flags = FBNIC_RING_F_CTX | FBNIC_RING_F_STATS; 1619 1620 /* Configure Tx queue */ 1621 db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TWQ0_TAIL]; 1622 1623 /* Assign Tx queue to netdev if applicable */ 1624 if (txq_count > 0) { 1625 1626 fbnic_ring_init(&qt->sub0, db, txq_idx, flags); 1627 fbn->tx[txq_idx] = &qt->sub0; 1628 txq_count--; 1629 } else { 1630 fbnic_ring_init(&qt->sub0, db, 0, 1631 FBNIC_RING_F_DISABLED); 1632 } 1633 1634 /* Configure XDP queue */ 1635 db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TWQ1_TAIL]; 1636 1637 /* Assign XDP queue to netdev if applicable 1638 * 1639 * The setup for this is in itself a bit different. 1640 * 1. We only need one XDP Tx queue per NAPI vector. 1641 * 2. We associate it to the first Rx queue index. 1642 * 3. The hardware side is associated based on the Tx Queue. 1643 * 4. The netdev queue is offset by FBNIC_MAX_TXQs. 1644 */ 1645 if (xdp_count > 0) { 1646 unsigned int xdp_idx = FBNIC_MAX_TXQS + rxq_idx; 1647 1648 fbnic_ring_init(&qt->sub1, db, xdp_idx, flags); 1649 fbn->tx[xdp_idx] = &qt->sub1; 1650 xdp_count--; 1651 } else { 1652 fbnic_ring_init(&qt->sub1, db, 0, 1653 FBNIC_RING_F_DISABLED); 1654 } 1655 1656 /* Configure Tx completion queue */ 1657 db = &uc_addr[FBNIC_QUEUE(txq_idx) + FBNIC_QUEUE_TCQ_HEAD]; 1658 fbnic_ring_init(&qt->cmpl, db, 0, 0); 1659 1660 /* Update Tx queue index */ 1661 txt_count--; 1662 txq_idx += v_count; 1663 1664 /* Move to next queue triad */ 1665 qt++; 1666 } 1667 1668 while (rxt_count) { 1669 /* Configure header queue */ 1670 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_BDQ_HPQ_TAIL]; 1671 fbnic_ring_init(&qt->sub0, db, 0, FBNIC_RING_F_CTX); 1672 1673 /* Configure payload queue */ 1674 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_BDQ_PPQ_TAIL]; 1675 fbnic_ring_init(&qt->sub1, db, 0, FBNIC_RING_F_CTX); 1676 1677 /* Configure Rx completion queue */ 1678 db = &uc_addr[FBNIC_QUEUE(rxq_idx) + FBNIC_QUEUE_RCQ_HEAD]; 1679 fbnic_ring_init(&qt->cmpl, db, rxq_idx, FBNIC_RING_F_STATS); 1680 fbn->rx[rxq_idx] = &qt->cmpl; 1681 1682 err = xdp_rxq_info_reg(&qt->xdp_rxq, fbn->netdev, rxq_idx, 1683 nv->napi.napi_id); 1684 if (err) 1685 goto free_ring_cur_qt; 1686 1687 /* Update Rx queue index */ 1688 rxt_count--; 1689 rxq_idx += v_count; 1690 1691 /* Move to next queue triad */ 1692 qt++; 1693 } 1694 1695 return 0; 1696 1697 while (rxt_count < nv->rxt_count) { 1698 qt--; 1699 1700 xdp_rxq_info_unreg(&qt->xdp_rxq); 1701 free_ring_cur_qt: 1702 fbnic_remove_rx_ring(fbn, &qt->sub0); 1703 fbnic_remove_rx_ring(fbn, &qt->sub1); 1704 fbnic_remove_rx_ring(fbn, &qt->cmpl); 1705 rxt_count++; 1706 } 1707 while (txt_count < nv->txt_count) { 1708 qt--; 1709 1710 fbnic_remove_tx_ring(fbn, &qt->sub0); 1711 fbnic_remove_xdp_ring(fbn, &qt->sub1); 1712 fbnic_remove_tx_ring(fbn, &qt->cmpl); 1713 1714 txt_count++; 1715 } 1716 fbnic_napi_free_irq(fbd, nv); 1717 pp_destroy: 1718 page_pool_destroy(nv->page_pool); 1719 napi_del: 1720 netif_napi_del(&nv->napi); 1721 fbn->napi[fbnic_napi_idx(nv)] = NULL; 1722 kfree(nv); 1723 return err; 1724 } 1725 1726 int fbnic_alloc_napi_vectors(struct fbnic_net *fbn) 1727 { 1728 unsigned int txq_idx = 0, rxq_idx = 0, v_idx = FBNIC_NON_NAPI_VECTORS; 1729 unsigned int num_tx = fbn->num_tx_queues; 1730 unsigned int num_rx = fbn->num_rx_queues; 1731 unsigned int num_napi = fbn->num_napi; 1732 struct fbnic_dev *fbd = fbn->fbd; 1733 int err; 1734 1735 /* Allocate 1 Tx queue per napi vector */ 1736 if (num_napi < FBNIC_MAX_TXQS && num_napi == num_tx + num_rx) { 1737 while (num_tx) { 1738 err = fbnic_alloc_napi_vector(fbd, fbn, 1739 num_napi, v_idx, 1740 1, txq_idx, 0, 0); 1741 if (err) 1742 goto free_vectors; 1743 1744 /* Update counts and index */ 1745 num_tx--; 1746 txq_idx++; 1747 1748 v_idx++; 1749 } 1750 } 1751 1752 /* Allocate Tx/Rx queue pairs per vector, or allocate remaining Rx */ 1753 while (num_rx | num_tx) { 1754 int tqpv = DIV_ROUND_UP(num_tx, num_napi - txq_idx); 1755 int rqpv = DIV_ROUND_UP(num_rx, num_napi - rxq_idx); 1756 1757 err = fbnic_alloc_napi_vector(fbd, fbn, num_napi, v_idx, 1758 tqpv, txq_idx, rqpv, rxq_idx); 1759 if (err) 1760 goto free_vectors; 1761 1762 /* Update counts and index */ 1763 num_tx -= tqpv; 1764 txq_idx++; 1765 1766 num_rx -= rqpv; 1767 rxq_idx++; 1768 1769 v_idx++; 1770 } 1771 1772 return 0; 1773 1774 free_vectors: 1775 fbnic_free_napi_vectors(fbn); 1776 1777 return -ENOMEM; 1778 } 1779 1780 static void fbnic_free_ring_resources(struct device *dev, 1781 struct fbnic_ring *ring) 1782 { 1783 kvfree(ring->buffer); 1784 ring->buffer = NULL; 1785 1786 /* If size is not set there are no descriptors present */ 1787 if (!ring->size) 1788 return; 1789 1790 dma_free_coherent(dev, ring->size, ring->desc, ring->dma); 1791 ring->size_mask = 0; 1792 ring->size = 0; 1793 } 1794 1795 static int fbnic_alloc_tx_ring_desc(struct fbnic_net *fbn, 1796 struct fbnic_ring *txr) 1797 { 1798 struct device *dev = fbn->netdev->dev.parent; 1799 size_t size; 1800 1801 /* Round size up to nearest 4K */ 1802 size = ALIGN(array_size(sizeof(*txr->desc), fbn->txq_size), 4096); 1803 1804 txr->desc = dma_alloc_coherent(dev, size, &txr->dma, 1805 GFP_KERNEL | __GFP_NOWARN); 1806 if (!txr->desc) 1807 return -ENOMEM; 1808 1809 /* txq_size should be a power of 2, so mask is just that -1 */ 1810 txr->size_mask = fbn->txq_size - 1; 1811 txr->size = size; 1812 1813 return 0; 1814 } 1815 1816 static int fbnic_alloc_tx_ring_buffer(struct fbnic_ring *txr) 1817 { 1818 size_t size = array_size(sizeof(*txr->tx_buf), txr->size_mask + 1); 1819 1820 txr->tx_buf = kvzalloc(size, GFP_KERNEL | __GFP_NOWARN); 1821 1822 return txr->tx_buf ? 0 : -ENOMEM; 1823 } 1824 1825 static int fbnic_alloc_tx_ring_resources(struct fbnic_net *fbn, 1826 struct fbnic_ring *txr) 1827 { 1828 struct device *dev = fbn->netdev->dev.parent; 1829 int err; 1830 1831 if (txr->flags & FBNIC_RING_F_DISABLED) 1832 return 0; 1833 1834 err = fbnic_alloc_tx_ring_desc(fbn, txr); 1835 if (err) 1836 return err; 1837 1838 if (!(txr->flags & FBNIC_RING_F_CTX)) 1839 return 0; 1840 1841 err = fbnic_alloc_tx_ring_buffer(txr); 1842 if (err) 1843 goto free_desc; 1844 1845 return 0; 1846 1847 free_desc: 1848 fbnic_free_ring_resources(dev, txr); 1849 return err; 1850 } 1851 1852 static int fbnic_alloc_rx_ring_desc(struct fbnic_net *fbn, 1853 struct fbnic_ring *rxr) 1854 { 1855 struct device *dev = fbn->netdev->dev.parent; 1856 size_t desc_size = sizeof(*rxr->desc); 1857 u32 rxq_size; 1858 size_t size; 1859 1860 switch (rxr->doorbell - fbnic_ring_csr_base(rxr)) { 1861 case FBNIC_QUEUE_BDQ_HPQ_TAIL: 1862 rxq_size = fbn->hpq_size / FBNIC_BD_FRAG_COUNT; 1863 desc_size *= FBNIC_BD_FRAG_COUNT; 1864 break; 1865 case FBNIC_QUEUE_BDQ_PPQ_TAIL: 1866 rxq_size = fbn->ppq_size / FBNIC_BD_FRAG_COUNT; 1867 desc_size *= FBNIC_BD_FRAG_COUNT; 1868 break; 1869 case FBNIC_QUEUE_RCQ_HEAD: 1870 rxq_size = fbn->rcq_size; 1871 break; 1872 default: 1873 return -EINVAL; 1874 } 1875 1876 /* Round size up to nearest 4K */ 1877 size = ALIGN(array_size(desc_size, rxq_size), 4096); 1878 1879 rxr->desc = dma_alloc_coherent(dev, size, &rxr->dma, 1880 GFP_KERNEL | __GFP_NOWARN); 1881 if (!rxr->desc) 1882 return -ENOMEM; 1883 1884 /* rxq_size should be a power of 2, so mask is just that -1 */ 1885 rxr->size_mask = rxq_size - 1; 1886 rxr->size = size; 1887 1888 return 0; 1889 } 1890 1891 static int fbnic_alloc_rx_ring_buffer(struct fbnic_ring *rxr) 1892 { 1893 size_t size = array_size(sizeof(*rxr->rx_buf), rxr->size_mask + 1); 1894 1895 if (rxr->flags & FBNIC_RING_F_CTX) 1896 size = sizeof(*rxr->rx_buf) * (rxr->size_mask + 1); 1897 else 1898 size = sizeof(*rxr->pkt); 1899 1900 rxr->rx_buf = kvzalloc(size, GFP_KERNEL | __GFP_NOWARN); 1901 1902 return rxr->rx_buf ? 0 : -ENOMEM; 1903 } 1904 1905 static int fbnic_alloc_rx_ring_resources(struct fbnic_net *fbn, 1906 struct fbnic_ring *rxr) 1907 { 1908 struct device *dev = fbn->netdev->dev.parent; 1909 int err; 1910 1911 err = fbnic_alloc_rx_ring_desc(fbn, rxr); 1912 if (err) 1913 return err; 1914 1915 err = fbnic_alloc_rx_ring_buffer(rxr); 1916 if (err) 1917 goto free_desc; 1918 1919 return 0; 1920 1921 free_desc: 1922 fbnic_free_ring_resources(dev, rxr); 1923 return err; 1924 } 1925 1926 static void fbnic_free_qt_resources(struct fbnic_net *fbn, 1927 struct fbnic_q_triad *qt) 1928 { 1929 struct device *dev = fbn->netdev->dev.parent; 1930 1931 fbnic_free_ring_resources(dev, &qt->cmpl); 1932 fbnic_free_ring_resources(dev, &qt->sub1); 1933 fbnic_free_ring_resources(dev, &qt->sub0); 1934 } 1935 1936 static int fbnic_alloc_tx_qt_resources(struct fbnic_net *fbn, 1937 struct fbnic_q_triad *qt) 1938 { 1939 struct device *dev = fbn->netdev->dev.parent; 1940 int err; 1941 1942 err = fbnic_alloc_tx_ring_resources(fbn, &qt->sub0); 1943 if (err) 1944 return err; 1945 1946 err = fbnic_alloc_tx_ring_resources(fbn, &qt->sub1); 1947 if (err) 1948 goto free_sub0; 1949 1950 err = fbnic_alloc_tx_ring_resources(fbn, &qt->cmpl); 1951 if (err) 1952 goto free_sub1; 1953 1954 return 0; 1955 1956 free_sub1: 1957 fbnic_free_ring_resources(dev, &qt->sub1); 1958 free_sub0: 1959 fbnic_free_ring_resources(dev, &qt->sub0); 1960 return err; 1961 } 1962 1963 static int fbnic_alloc_rx_qt_resources(struct fbnic_net *fbn, 1964 struct fbnic_q_triad *qt) 1965 { 1966 struct device *dev = fbn->netdev->dev.parent; 1967 int err; 1968 1969 err = fbnic_alloc_rx_ring_resources(fbn, &qt->sub0); 1970 if (err) 1971 return err; 1972 1973 err = fbnic_alloc_rx_ring_resources(fbn, &qt->sub1); 1974 if (err) 1975 goto free_sub0; 1976 1977 err = fbnic_alloc_rx_ring_resources(fbn, &qt->cmpl); 1978 if (err) 1979 goto free_sub1; 1980 1981 return 0; 1982 1983 free_sub1: 1984 fbnic_free_ring_resources(dev, &qt->sub1); 1985 free_sub0: 1986 fbnic_free_ring_resources(dev, &qt->sub0); 1987 return err; 1988 } 1989 1990 static void fbnic_free_nv_resources(struct fbnic_net *fbn, 1991 struct fbnic_napi_vector *nv) 1992 { 1993 int i, j; 1994 1995 /* Free Tx Resources */ 1996 for (i = 0; i < nv->txt_count; i++) 1997 fbnic_free_qt_resources(fbn, &nv->qt[i]); 1998 1999 for (j = 0; j < nv->rxt_count; j++, i++) { 2000 fbnic_free_qt_resources(fbn, &nv->qt[i]); 2001 xdp_rxq_info_unreg_mem_model(&nv->qt[i].xdp_rxq); 2002 } 2003 } 2004 2005 static int fbnic_alloc_nv_resources(struct fbnic_net *fbn, 2006 struct fbnic_napi_vector *nv) 2007 { 2008 int i, j, err; 2009 2010 /* Allocate Tx Resources */ 2011 for (i = 0; i < nv->txt_count; i++) { 2012 err = fbnic_alloc_tx_qt_resources(fbn, &nv->qt[i]); 2013 if (err) 2014 goto free_qt_resources; 2015 } 2016 2017 /* Allocate Rx Resources */ 2018 for (j = 0; j < nv->rxt_count; j++, i++) { 2019 /* Register XDP memory model for completion queue */ 2020 err = xdp_reg_mem_model(&nv->qt[i].xdp_rxq.mem, 2021 MEM_TYPE_PAGE_POOL, 2022 nv->page_pool); 2023 if (err) 2024 goto xdp_unreg_mem_model; 2025 2026 err = fbnic_alloc_rx_qt_resources(fbn, &nv->qt[i]); 2027 if (err) 2028 goto xdp_unreg_cur_model; 2029 } 2030 2031 return 0; 2032 2033 xdp_unreg_mem_model: 2034 while (j-- && i--) { 2035 fbnic_free_qt_resources(fbn, &nv->qt[i]); 2036 xdp_unreg_cur_model: 2037 xdp_rxq_info_unreg_mem_model(&nv->qt[i].xdp_rxq); 2038 } 2039 free_qt_resources: 2040 while (i--) 2041 fbnic_free_qt_resources(fbn, &nv->qt[i]); 2042 return err; 2043 } 2044 2045 void fbnic_free_resources(struct fbnic_net *fbn) 2046 { 2047 int i; 2048 2049 for (i = 0; i < fbn->num_napi; i++) 2050 fbnic_free_nv_resources(fbn, fbn->napi[i]); 2051 } 2052 2053 int fbnic_alloc_resources(struct fbnic_net *fbn) 2054 { 2055 int i, err = -ENODEV; 2056 2057 for (i = 0; i < fbn->num_napi; i++) { 2058 err = fbnic_alloc_nv_resources(fbn, fbn->napi[i]); 2059 if (err) 2060 goto free_resources; 2061 } 2062 2063 return 0; 2064 2065 free_resources: 2066 while (i--) 2067 fbnic_free_nv_resources(fbn, fbn->napi[i]); 2068 2069 return err; 2070 } 2071 2072 static void fbnic_set_netif_napi(struct fbnic_napi_vector *nv) 2073 { 2074 int i, j; 2075 2076 /* Associate Tx queue with NAPI */ 2077 for (i = 0; i < nv->txt_count; i++) { 2078 struct fbnic_q_triad *qt = &nv->qt[i]; 2079 2080 netif_queue_set_napi(nv->napi.dev, qt->sub0.q_idx, 2081 NETDEV_QUEUE_TYPE_TX, &nv->napi); 2082 } 2083 2084 /* Associate Rx queue with NAPI */ 2085 for (j = 0; j < nv->rxt_count; j++, i++) { 2086 struct fbnic_q_triad *qt = &nv->qt[i]; 2087 2088 netif_queue_set_napi(nv->napi.dev, qt->cmpl.q_idx, 2089 NETDEV_QUEUE_TYPE_RX, &nv->napi); 2090 } 2091 } 2092 2093 static void fbnic_reset_netif_napi(struct fbnic_napi_vector *nv) 2094 { 2095 int i, j; 2096 2097 /* Disassociate Tx queue from NAPI */ 2098 for (i = 0; i < nv->txt_count; i++) { 2099 struct fbnic_q_triad *qt = &nv->qt[i]; 2100 2101 netif_queue_set_napi(nv->napi.dev, qt->sub0.q_idx, 2102 NETDEV_QUEUE_TYPE_TX, NULL); 2103 } 2104 2105 /* Disassociate Rx queue from NAPI */ 2106 for (j = 0; j < nv->rxt_count; j++, i++) { 2107 struct fbnic_q_triad *qt = &nv->qt[i]; 2108 2109 netif_queue_set_napi(nv->napi.dev, qt->cmpl.q_idx, 2110 NETDEV_QUEUE_TYPE_RX, NULL); 2111 } 2112 } 2113 2114 int fbnic_set_netif_queues(struct fbnic_net *fbn) 2115 { 2116 int i, err; 2117 2118 err = netif_set_real_num_queues(fbn->netdev, fbn->num_tx_queues, 2119 fbn->num_rx_queues); 2120 if (err) 2121 return err; 2122 2123 for (i = 0; i < fbn->num_napi; i++) 2124 fbnic_set_netif_napi(fbn->napi[i]); 2125 2126 return 0; 2127 } 2128 2129 void fbnic_reset_netif_queues(struct fbnic_net *fbn) 2130 { 2131 int i; 2132 2133 for (i = 0; i < fbn->num_napi; i++) 2134 fbnic_reset_netif_napi(fbn->napi[i]); 2135 } 2136 2137 static void fbnic_disable_twq0(struct fbnic_ring *txr) 2138 { 2139 u32 twq_ctl = fbnic_ring_rd32(txr, FBNIC_QUEUE_TWQ0_CTL); 2140 2141 twq_ctl &= ~FBNIC_QUEUE_TWQ_CTL_ENABLE; 2142 2143 fbnic_ring_wr32(txr, FBNIC_QUEUE_TWQ0_CTL, twq_ctl); 2144 } 2145 2146 static void fbnic_disable_twq1(struct fbnic_ring *txr) 2147 { 2148 u32 twq_ctl = fbnic_ring_rd32(txr, FBNIC_QUEUE_TWQ1_CTL); 2149 2150 twq_ctl &= ~FBNIC_QUEUE_TWQ_CTL_ENABLE; 2151 2152 fbnic_ring_wr32(txr, FBNIC_QUEUE_TWQ1_CTL, twq_ctl); 2153 } 2154 2155 static void fbnic_disable_tcq(struct fbnic_ring *txr) 2156 { 2157 fbnic_ring_wr32(txr, FBNIC_QUEUE_TCQ_CTL, 0); 2158 fbnic_ring_wr32(txr, FBNIC_QUEUE_TIM_MASK, FBNIC_QUEUE_TIM_MASK_MASK); 2159 } 2160 2161 static void fbnic_disable_bdq(struct fbnic_ring *hpq, struct fbnic_ring *ppq) 2162 { 2163 u32 bdq_ctl = fbnic_ring_rd32(hpq, FBNIC_QUEUE_BDQ_CTL); 2164 2165 bdq_ctl &= ~FBNIC_QUEUE_BDQ_CTL_ENABLE; 2166 2167 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, bdq_ctl); 2168 } 2169 2170 static void fbnic_disable_rcq(struct fbnic_ring *rxr) 2171 { 2172 fbnic_ring_wr32(rxr, FBNIC_QUEUE_RCQ_CTL, 0); 2173 fbnic_ring_wr32(rxr, FBNIC_QUEUE_RIM_MASK, FBNIC_QUEUE_RIM_MASK_MASK); 2174 } 2175 2176 void fbnic_napi_disable(struct fbnic_net *fbn) 2177 { 2178 int i; 2179 2180 for (i = 0; i < fbn->num_napi; i++) { 2181 napi_disable(&fbn->napi[i]->napi); 2182 2183 fbnic_nv_irq_disable(fbn->napi[i]); 2184 } 2185 } 2186 2187 void fbnic_disable(struct fbnic_net *fbn) 2188 { 2189 struct fbnic_dev *fbd = fbn->fbd; 2190 int i, j, t; 2191 2192 for (i = 0; i < fbn->num_napi; i++) { 2193 struct fbnic_napi_vector *nv = fbn->napi[i]; 2194 2195 /* Disable Tx queue triads */ 2196 for (t = 0; t < nv->txt_count; t++) { 2197 struct fbnic_q_triad *qt = &nv->qt[t]; 2198 2199 fbnic_disable_twq0(&qt->sub0); 2200 fbnic_disable_twq1(&qt->sub1); 2201 fbnic_disable_tcq(&qt->cmpl); 2202 } 2203 2204 /* Disable Rx queue triads */ 2205 for (j = 0; j < nv->rxt_count; j++, t++) { 2206 struct fbnic_q_triad *qt = &nv->qt[t]; 2207 2208 fbnic_disable_bdq(&qt->sub0, &qt->sub1); 2209 fbnic_disable_rcq(&qt->cmpl); 2210 } 2211 } 2212 2213 fbnic_wrfl(fbd); 2214 } 2215 2216 static void fbnic_tx_flush(struct fbnic_dev *fbd) 2217 { 2218 netdev_warn(fbd->netdev, "triggering Tx flush\n"); 2219 2220 fbnic_rmw32(fbd, FBNIC_TMI_DROP_CTRL, FBNIC_TMI_DROP_CTRL_EN, 2221 FBNIC_TMI_DROP_CTRL_EN); 2222 } 2223 2224 static void fbnic_tx_flush_off(struct fbnic_dev *fbd) 2225 { 2226 fbnic_rmw32(fbd, FBNIC_TMI_DROP_CTRL, FBNIC_TMI_DROP_CTRL_EN, 0); 2227 } 2228 2229 struct fbnic_idle_regs { 2230 u32 reg_base; 2231 u8 reg_cnt; 2232 }; 2233 2234 static bool fbnic_all_idle(struct fbnic_dev *fbd, 2235 const struct fbnic_idle_regs *regs, 2236 unsigned int nregs) 2237 { 2238 unsigned int i, j; 2239 2240 for (i = 0; i < nregs; i++) { 2241 for (j = 0; j < regs[i].reg_cnt; j++) { 2242 if (fbnic_rd32(fbd, regs[i].reg_base + j) != ~0U) 2243 return false; 2244 } 2245 } 2246 return true; 2247 } 2248 2249 static void fbnic_idle_dump(struct fbnic_dev *fbd, 2250 const struct fbnic_idle_regs *regs, 2251 unsigned int nregs, const char *dir, int err) 2252 { 2253 unsigned int i, j; 2254 2255 netdev_err(fbd->netdev, "error waiting for %s idle %d\n", dir, err); 2256 for (i = 0; i < nregs; i++) 2257 for (j = 0; j < regs[i].reg_cnt; j++) 2258 netdev_err(fbd->netdev, "0x%04x: %08x\n", 2259 regs[i].reg_base + j, 2260 fbnic_rd32(fbd, regs[i].reg_base + j)); 2261 } 2262 2263 int fbnic_wait_all_queues_idle(struct fbnic_dev *fbd, bool may_fail) 2264 { 2265 static const struct fbnic_idle_regs tx[] = { 2266 { FBNIC_QM_TWQ_IDLE(0), FBNIC_QM_TWQ_IDLE_CNT, }, 2267 { FBNIC_QM_TQS_IDLE(0), FBNIC_QM_TQS_IDLE_CNT, }, 2268 { FBNIC_QM_TDE_IDLE(0), FBNIC_QM_TDE_IDLE_CNT, }, 2269 { FBNIC_QM_TCQ_IDLE(0), FBNIC_QM_TCQ_IDLE_CNT, }, 2270 }, rx[] = { 2271 { FBNIC_QM_HPQ_IDLE(0), FBNIC_QM_HPQ_IDLE_CNT, }, 2272 { FBNIC_QM_PPQ_IDLE(0), FBNIC_QM_PPQ_IDLE_CNT, }, 2273 { FBNIC_QM_RCQ_IDLE(0), FBNIC_QM_RCQ_IDLE_CNT, }, 2274 }; 2275 bool idle; 2276 int err; 2277 2278 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2, 500000, 2279 false, fbd, tx, ARRAY_SIZE(tx)); 2280 if (err == -ETIMEDOUT) { 2281 fbnic_tx_flush(fbd); 2282 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2283 2, 500000, false, 2284 fbd, tx, ARRAY_SIZE(tx)); 2285 fbnic_tx_flush_off(fbd); 2286 } 2287 if (err) { 2288 fbnic_idle_dump(fbd, tx, ARRAY_SIZE(tx), "Tx", err); 2289 if (may_fail) 2290 return err; 2291 } 2292 2293 err = read_poll_timeout_atomic(fbnic_all_idle, idle, idle, 2, 500000, 2294 false, fbd, rx, ARRAY_SIZE(rx)); 2295 if (err) 2296 fbnic_idle_dump(fbd, rx, ARRAY_SIZE(rx), "Rx", err); 2297 return err; 2298 } 2299 2300 void fbnic_flush(struct fbnic_net *fbn) 2301 { 2302 int i; 2303 2304 for (i = 0; i < fbn->num_napi; i++) { 2305 struct fbnic_napi_vector *nv = fbn->napi[i]; 2306 int j, t; 2307 2308 /* Flush any processed Tx Queue Triads and drop the rest */ 2309 for (t = 0; t < nv->txt_count; t++) { 2310 struct fbnic_q_triad *qt = &nv->qt[t]; 2311 struct netdev_queue *tx_queue; 2312 2313 /* Clean the work queues of unprocessed work */ 2314 fbnic_clean_twq0(nv, 0, &qt->sub0, true, qt->sub0.tail); 2315 fbnic_clean_twq1(nv, false, &qt->sub1, true, 2316 qt->sub1.tail); 2317 2318 /* Reset completion queue descriptor ring */ 2319 memset(qt->cmpl.desc, 0, qt->cmpl.size); 2320 2321 /* Nothing else to do if Tx queue is disabled */ 2322 if (qt->sub0.flags & FBNIC_RING_F_DISABLED) 2323 continue; 2324 2325 /* Reset BQL associated with Tx queue */ 2326 tx_queue = netdev_get_tx_queue(nv->napi.dev, 2327 qt->sub0.q_idx); 2328 netdev_tx_reset_queue(tx_queue); 2329 } 2330 2331 /* Flush any processed Rx Queue Triads and drop the rest */ 2332 for (j = 0; j < nv->rxt_count; j++, t++) { 2333 struct fbnic_q_triad *qt = &nv->qt[t]; 2334 2335 /* Clean the work queues of unprocessed work */ 2336 fbnic_clean_bdq(nv, 0, &qt->sub0, qt->sub0.tail); 2337 fbnic_clean_bdq(nv, 0, &qt->sub1, qt->sub1.tail); 2338 2339 /* Reset completion queue descriptor ring */ 2340 memset(qt->cmpl.desc, 0, qt->cmpl.size); 2341 2342 fbnic_put_pkt_buff(nv, qt->cmpl.pkt, 0); 2343 memset(qt->cmpl.pkt, 0, sizeof(struct fbnic_pkt_buff)); 2344 } 2345 } 2346 } 2347 2348 void fbnic_fill(struct fbnic_net *fbn) 2349 { 2350 int i; 2351 2352 for (i = 0; i < fbn->num_napi; i++) { 2353 struct fbnic_napi_vector *nv = fbn->napi[i]; 2354 int j, t; 2355 2356 /* Configure NAPI mapping and populate pages 2357 * in the BDQ rings to use for Rx 2358 */ 2359 for (j = 0, t = nv->txt_count; j < nv->rxt_count; j++, t++) { 2360 struct fbnic_q_triad *qt = &nv->qt[t]; 2361 2362 /* Populate the header and payload BDQs */ 2363 fbnic_fill_bdq(nv, &qt->sub0); 2364 fbnic_fill_bdq(nv, &qt->sub1); 2365 } 2366 } 2367 } 2368 2369 static void fbnic_enable_twq0(struct fbnic_ring *twq) 2370 { 2371 u32 log_size = fls(twq->size_mask); 2372 2373 if (!twq->size_mask) 2374 return; 2375 2376 /* Reset head/tail */ 2377 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_CTL, FBNIC_QUEUE_TWQ_CTL_RESET); 2378 twq->tail = 0; 2379 twq->head = 0; 2380 2381 /* Store descriptor ring address and size */ 2382 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_BAL, lower_32_bits(twq->dma)); 2383 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_BAH, upper_32_bits(twq->dma)); 2384 2385 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2386 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_SIZE, log_size & 0xf); 2387 2388 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ0_CTL, FBNIC_QUEUE_TWQ_CTL_ENABLE); 2389 } 2390 2391 static void fbnic_enable_twq1(struct fbnic_ring *twq) 2392 { 2393 u32 log_size = fls(twq->size_mask); 2394 2395 if (!twq->size_mask) 2396 return; 2397 2398 /* Reset head/tail */ 2399 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_CTL, FBNIC_QUEUE_TWQ_CTL_RESET); 2400 twq->tail = 0; 2401 twq->head = 0; 2402 2403 /* Store descriptor ring address and size */ 2404 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_BAL, lower_32_bits(twq->dma)); 2405 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_BAH, upper_32_bits(twq->dma)); 2406 2407 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2408 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_SIZE, log_size & 0xf); 2409 2410 fbnic_ring_wr32(twq, FBNIC_QUEUE_TWQ1_CTL, FBNIC_QUEUE_TWQ_CTL_ENABLE); 2411 } 2412 2413 static void fbnic_enable_tcq(struct fbnic_napi_vector *nv, 2414 struct fbnic_ring *tcq) 2415 { 2416 u32 log_size = fls(tcq->size_mask); 2417 2418 if (!tcq->size_mask) 2419 return; 2420 2421 /* Reset head/tail */ 2422 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_CTL, FBNIC_QUEUE_TCQ_CTL_RESET); 2423 tcq->tail = 0; 2424 tcq->head = 0; 2425 2426 /* Store descriptor ring address and size */ 2427 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_BAL, lower_32_bits(tcq->dma)); 2428 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_BAH, upper_32_bits(tcq->dma)); 2429 2430 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2431 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_SIZE, log_size & 0xf); 2432 2433 /* Store interrupt information for the completion queue */ 2434 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_CTL, nv->v_idx); 2435 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_THRESHOLD, tcq->size_mask / 2); 2436 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TIM_MASK, 0); 2437 2438 /* Enable queue */ 2439 fbnic_ring_wr32(tcq, FBNIC_QUEUE_TCQ_CTL, FBNIC_QUEUE_TCQ_CTL_ENABLE); 2440 } 2441 2442 static void fbnic_enable_bdq(struct fbnic_ring *hpq, struct fbnic_ring *ppq) 2443 { 2444 u32 bdq_ctl = FBNIC_QUEUE_BDQ_CTL_ENABLE; 2445 u32 log_size; 2446 2447 /* Reset head/tail */ 2448 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, FBNIC_QUEUE_BDQ_CTL_RESET); 2449 ppq->tail = 0; 2450 ppq->head = 0; 2451 hpq->tail = 0; 2452 hpq->head = 0; 2453 2454 log_size = fls(hpq->size_mask); 2455 2456 /* Store descriptor ring address and size */ 2457 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_BAL, lower_32_bits(hpq->dma)); 2458 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_BAH, upper_32_bits(hpq->dma)); 2459 2460 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2461 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_HPQ_SIZE, log_size & 0xf); 2462 2463 if (!ppq->size_mask) 2464 goto write_ctl; 2465 2466 log_size = fls(ppq->size_mask); 2467 2468 /* Add enabling of PPQ to BDQ control */ 2469 bdq_ctl |= FBNIC_QUEUE_BDQ_CTL_PPQ_ENABLE; 2470 2471 /* Store descriptor ring address and size */ 2472 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_BAL, lower_32_bits(ppq->dma)); 2473 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_BAH, upper_32_bits(ppq->dma)); 2474 fbnic_ring_wr32(ppq, FBNIC_QUEUE_BDQ_PPQ_SIZE, log_size & 0xf); 2475 2476 write_ctl: 2477 fbnic_ring_wr32(hpq, FBNIC_QUEUE_BDQ_CTL, bdq_ctl); 2478 } 2479 2480 static void fbnic_config_drop_mode_rcq(struct fbnic_napi_vector *nv, 2481 struct fbnic_ring *rcq) 2482 { 2483 u32 drop_mode, rcq_ctl; 2484 2485 drop_mode = FBNIC_QUEUE_RDE_CTL0_DROP_IMMEDIATE; 2486 2487 /* Specify packet layout */ 2488 rcq_ctl = FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_DROP_MODE_MASK, drop_mode) | 2489 FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_MIN_HROOM_MASK, FBNIC_RX_HROOM) | 2490 FIELD_PREP(FBNIC_QUEUE_RDE_CTL0_MIN_TROOM_MASK, FBNIC_RX_TROOM); 2491 2492 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RDE_CTL0, rcq_ctl); 2493 } 2494 2495 static void fbnic_config_rim_threshold(struct fbnic_ring *rcq, u16 nv_idx, u32 rx_desc) 2496 { 2497 u32 threshold; 2498 2499 /* Set the threhsold to half the ring size if rx_frames 2500 * is not configured 2501 */ 2502 threshold = rx_desc ? : rcq->size_mask / 2; 2503 2504 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_CTL, nv_idx); 2505 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_THRESHOLD, threshold); 2506 } 2507 2508 void fbnic_config_txrx_usecs(struct fbnic_napi_vector *nv, u32 arm) 2509 { 2510 struct fbnic_net *fbn = netdev_priv(nv->napi.dev); 2511 struct fbnic_dev *fbd = nv->fbd; 2512 u32 val = arm; 2513 2514 val |= FIELD_PREP(FBNIC_INTR_CQ_REARM_RCQ_TIMEOUT, fbn->rx_usecs) | 2515 FBNIC_INTR_CQ_REARM_RCQ_TIMEOUT_UPD_EN; 2516 val |= FIELD_PREP(FBNIC_INTR_CQ_REARM_TCQ_TIMEOUT, fbn->tx_usecs) | 2517 FBNIC_INTR_CQ_REARM_TCQ_TIMEOUT_UPD_EN; 2518 2519 fbnic_wr32(fbd, FBNIC_INTR_CQ_REARM(nv->v_idx), val); 2520 } 2521 2522 void fbnic_config_rx_frames(struct fbnic_napi_vector *nv) 2523 { 2524 struct fbnic_net *fbn = netdev_priv(nv->napi.dev); 2525 int i; 2526 2527 for (i = nv->txt_count; i < nv->rxt_count + nv->txt_count; i++) { 2528 struct fbnic_q_triad *qt = &nv->qt[i]; 2529 2530 fbnic_config_rim_threshold(&qt->cmpl, nv->v_idx, 2531 fbn->rx_max_frames * 2532 FBNIC_MIN_RXD_PER_FRAME); 2533 } 2534 } 2535 2536 static void fbnic_enable_rcq(struct fbnic_napi_vector *nv, 2537 struct fbnic_ring *rcq) 2538 { 2539 struct fbnic_net *fbn = netdev_priv(nv->napi.dev); 2540 u32 log_size = fls(rcq->size_mask); 2541 u32 hds_thresh = fbn->hds_thresh; 2542 u32 rcq_ctl = 0; 2543 2544 fbnic_config_drop_mode_rcq(nv, rcq); 2545 2546 /* Force lower bound on MAX_HEADER_BYTES. Below this, all frames should 2547 * be split at L4. It would also result in the frames being split at 2548 * L2/L3 depending on the frame size. 2549 */ 2550 if (fbn->hds_thresh < FBNIC_HDR_BYTES_MIN) { 2551 rcq_ctl = FBNIC_QUEUE_RDE_CTL0_EN_HDR_SPLIT; 2552 hds_thresh = FBNIC_HDR_BYTES_MIN; 2553 } 2554 2555 rcq_ctl |= FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PADLEN_MASK, FBNIC_RX_PAD) | 2556 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_MAX_HDR_MASK, hds_thresh) | 2557 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PAYLD_OFF_MASK, 2558 FBNIC_RX_PAYLD_OFFSET) | 2559 FIELD_PREP(FBNIC_QUEUE_RDE_CTL1_PAYLD_PG_CL_MASK, 2560 FBNIC_RX_PAYLD_PG_CL); 2561 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RDE_CTL1, rcq_ctl); 2562 2563 /* Reset head/tail */ 2564 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_CTL, FBNIC_QUEUE_RCQ_CTL_RESET); 2565 rcq->head = 0; 2566 rcq->tail = 0; 2567 2568 /* Store descriptor ring address and size */ 2569 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_BAL, lower_32_bits(rcq->dma)); 2570 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_BAH, upper_32_bits(rcq->dma)); 2571 2572 /* Write lower 4 bits of log size as 64K ring size is 0 */ 2573 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_SIZE, log_size & 0xf); 2574 2575 /* Store interrupt information for the completion queue */ 2576 fbnic_config_rim_threshold(rcq, nv->v_idx, fbn->rx_max_frames * 2577 FBNIC_MIN_RXD_PER_FRAME); 2578 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RIM_MASK, 0); 2579 2580 /* Enable queue */ 2581 fbnic_ring_wr32(rcq, FBNIC_QUEUE_RCQ_CTL, FBNIC_QUEUE_RCQ_CTL_ENABLE); 2582 } 2583 2584 void fbnic_enable(struct fbnic_net *fbn) 2585 { 2586 struct fbnic_dev *fbd = fbn->fbd; 2587 int i; 2588 2589 for (i = 0; i < fbn->num_napi; i++) { 2590 struct fbnic_napi_vector *nv = fbn->napi[i]; 2591 int j, t; 2592 2593 /* Setup Tx Queue Triads */ 2594 for (t = 0; t < nv->txt_count; t++) { 2595 struct fbnic_q_triad *qt = &nv->qt[t]; 2596 2597 fbnic_enable_twq0(&qt->sub0); 2598 fbnic_enable_twq1(&qt->sub1); 2599 fbnic_enable_tcq(nv, &qt->cmpl); 2600 } 2601 2602 /* Setup Rx Queue Triads */ 2603 for (j = 0; j < nv->rxt_count; j++, t++) { 2604 struct fbnic_q_triad *qt = &nv->qt[t]; 2605 2606 fbnic_enable_bdq(&qt->sub0, &qt->sub1); 2607 fbnic_config_drop_mode_rcq(nv, &qt->cmpl); 2608 fbnic_enable_rcq(nv, &qt->cmpl); 2609 } 2610 } 2611 2612 fbnic_wrfl(fbd); 2613 } 2614 2615 static void fbnic_nv_irq_enable(struct fbnic_napi_vector *nv) 2616 { 2617 fbnic_config_txrx_usecs(nv, FBNIC_INTR_CQ_REARM_INTR_UNMASK); 2618 } 2619 2620 void fbnic_napi_enable(struct fbnic_net *fbn) 2621 { 2622 u32 irqs[FBNIC_MAX_MSIX_VECS / 32] = {}; 2623 struct fbnic_dev *fbd = fbn->fbd; 2624 int i; 2625 2626 for (i = 0; i < fbn->num_napi; i++) { 2627 struct fbnic_napi_vector *nv = fbn->napi[i]; 2628 2629 napi_enable(&nv->napi); 2630 2631 fbnic_nv_irq_enable(nv); 2632 2633 /* Record bit used for NAPI IRQs so we can 2634 * set the mask appropriately 2635 */ 2636 irqs[nv->v_idx / 32] |= BIT(nv->v_idx % 32); 2637 } 2638 2639 /* Force the first interrupt on the device to guarantee 2640 * that any packets that may have been enqueued during the 2641 * bringup are processed. 2642 */ 2643 for (i = 0; i < ARRAY_SIZE(irqs); i++) { 2644 if (!irqs[i]) 2645 continue; 2646 fbnic_wr32(fbd, FBNIC_INTR_SET(i), irqs[i]); 2647 } 2648 2649 fbnic_wrfl(fbd); 2650 } 2651 2652 void fbnic_napi_depletion_check(struct net_device *netdev) 2653 { 2654 struct fbnic_net *fbn = netdev_priv(netdev); 2655 u32 irqs[FBNIC_MAX_MSIX_VECS / 32] = {}; 2656 struct fbnic_dev *fbd = fbn->fbd; 2657 int i, j, t; 2658 2659 for (i = 0; i < fbn->num_napi; i++) { 2660 struct fbnic_napi_vector *nv = fbn->napi[i]; 2661 2662 /* Find RQs which are completely out of pages */ 2663 for (t = nv->txt_count, j = 0; j < nv->rxt_count; j++, t++) { 2664 /* Assume 4 pages is always enough to fit a packet 2665 * and therefore generate a completion and an IRQ. 2666 */ 2667 if (fbnic_desc_used(&nv->qt[t].sub0) < 4 || 2668 fbnic_desc_used(&nv->qt[t].sub1) < 4) 2669 irqs[nv->v_idx / 32] |= BIT(nv->v_idx % 32); 2670 } 2671 } 2672 2673 for (i = 0; i < ARRAY_SIZE(irqs); i++) { 2674 if (!irqs[i]) 2675 continue; 2676 fbnic_wr32(fbd, FBNIC_INTR_MASK_CLEAR(i), irqs[i]); 2677 fbnic_wr32(fbd, FBNIC_INTR_SET(i), irqs[i]); 2678 } 2679 2680 fbnic_wrfl(fbd); 2681 } 2682