1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright (C) 2015-2019 Netronome Systems, Inc. */ 3 4 #include <linux/bpf_trace.h> 5 #include <linux/netdevice.h> 6 #include <linux/bitfield.h> 7 #include <net/xfrm.h> 8 9 #include "../nfp_app.h" 10 #include "../nfp_net.h" 11 #include "../nfp_net_dp.h" 12 #include "../nfp_net_xsk.h" 13 #include "../crypto/crypto.h" 14 #include "../crypto/fw.h" 15 #include "nfd3.h" 16 17 /* Transmit processing 18 * 19 * One queue controller peripheral queue is used for transmit. The 20 * driver en-queues packets for transmit by advancing the write 21 * pointer. The device indicates that packets have transmitted by 22 * advancing the read pointer. The driver maintains a local copy of 23 * the read and write pointer in @struct nfp_net_tx_ring. The driver 24 * keeps @wr_p in sync with the queue controller write pointer and can 25 * determine how many packets have been transmitted by comparing its 26 * copy of the read pointer @rd_p with the read pointer maintained by 27 * the queue controller peripheral. 28 */ 29 30 /* Wrappers for deciding when to stop and restart TX queues */ 31 static int nfp_nfd3_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring) 32 { 33 return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4); 34 } 35 36 static int nfp_nfd3_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring) 37 { 38 return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1); 39 } 40 41 /** 42 * nfp_nfd3_tx_ring_stop() - stop tx ring 43 * @nd_q: netdev queue 44 * @tx_ring: driver tx queue structure 45 * 46 * Safely stop TX ring. Remember that while we are running .start_xmit() 47 * someone else may be cleaning the TX ring completions so we need to be 48 * extra careful here. 49 */ 50 static void 51 nfp_nfd3_tx_ring_stop(struct netdev_queue *nd_q, 52 struct nfp_net_tx_ring *tx_ring) 53 { 54 netif_tx_stop_queue(nd_q); 55 56 /* We can race with the TX completion out of NAPI so recheck */ 57 smp_mb(); 58 if (unlikely(nfp_nfd3_tx_ring_should_wake(tx_ring))) 59 netif_tx_start_queue(nd_q); 60 } 61 62 /** 63 * nfp_nfd3_tx_tso() - Set up Tx descriptor for LSO 64 * @r_vec: per-ring structure 65 * @txbuf: Pointer to driver soft TX descriptor 66 * @txd: Pointer to HW TX descriptor 67 * @skb: Pointer to SKB 68 * @md_bytes: Prepend length 69 * 70 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs. 71 * Return error on packet header greater than maximum supported LSO header size. 72 */ 73 static void 74 nfp_nfd3_tx_tso(struct nfp_net_r_vector *r_vec, struct nfp_nfd3_tx_buf *txbuf, 75 struct nfp_nfd3_tx_desc *txd, struct sk_buff *skb, u32 md_bytes) 76 { 77 u32 l3_offset, l4_offset, hdrlen; 78 u16 mss; 79 80 if (!skb_is_gso(skb)) 81 return; 82 83 if (!skb->encapsulation) { 84 l3_offset = skb_network_offset(skb); 85 l4_offset = skb_transport_offset(skb); 86 hdrlen = skb_tcp_all_headers(skb); 87 } else { 88 l3_offset = skb_inner_network_offset(skb); 89 l4_offset = skb_inner_transport_offset(skb); 90 hdrlen = skb_inner_tcp_all_headers(skb); 91 } 92 93 txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs; 94 txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1); 95 96 mss = skb_shinfo(skb)->gso_size & NFD3_DESC_TX_MSS_MASK; 97 txd->l3_offset = l3_offset - md_bytes; 98 txd->l4_offset = l4_offset - md_bytes; 99 txd->lso_hdrlen = hdrlen - md_bytes; 100 txd->mss = cpu_to_le16(mss); 101 txd->flags |= NFD3_DESC_TX_LSO; 102 103 u64_stats_update_begin(&r_vec->tx_sync); 104 r_vec->tx_lso++; 105 u64_stats_update_end(&r_vec->tx_sync); 106 } 107 108 /** 109 * nfp_nfd3_tx_csum() - Set TX CSUM offload flags in TX descriptor 110 * @dp: NFP Net data path struct 111 * @r_vec: per-ring structure 112 * @txbuf: Pointer to driver soft TX descriptor 113 * @txd: Pointer to TX descriptor 114 * @skb: Pointer to SKB 115 * 116 * This function sets the TX checksum flags in the TX descriptor based 117 * on the configuration and the protocol of the packet to be transmitted. 118 */ 119 static void 120 nfp_nfd3_tx_csum(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec, 121 struct nfp_nfd3_tx_buf *txbuf, struct nfp_nfd3_tx_desc *txd, 122 struct sk_buff *skb) 123 { 124 struct ipv6hdr *ipv6h; 125 struct iphdr *iph; 126 u8 l4_hdr; 127 128 if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM)) 129 return; 130 131 if (skb->ip_summed != CHECKSUM_PARTIAL) 132 return; 133 134 txd->flags |= NFD3_DESC_TX_CSUM; 135 if (skb->encapsulation) 136 txd->flags |= NFD3_DESC_TX_ENCAP; 137 138 iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb); 139 ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb); 140 141 if (iph->version == 4) { 142 txd->flags |= NFD3_DESC_TX_IP4_CSUM; 143 l4_hdr = iph->protocol; 144 } else if (ipv6h->version == 6) { 145 l4_hdr = ipv6h->nexthdr; 146 } else { 147 nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version); 148 return; 149 } 150 151 switch (l4_hdr) { 152 case IPPROTO_TCP: 153 txd->flags |= NFD3_DESC_TX_TCP_CSUM; 154 break; 155 case IPPROTO_UDP: 156 txd->flags |= NFD3_DESC_TX_UDP_CSUM; 157 break; 158 default: 159 nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr); 160 return; 161 } 162 163 u64_stats_update_begin(&r_vec->tx_sync); 164 if (skb->encapsulation) 165 r_vec->hw_csum_tx_inner += txbuf->pkt_cnt; 166 else 167 r_vec->hw_csum_tx += txbuf->pkt_cnt; 168 u64_stats_update_end(&r_vec->tx_sync); 169 } 170 171 static int nfp_nfd3_prep_tx_meta(struct nfp_net_dp *dp, struct sk_buff *skb, 172 u64 tls_handle, bool *ipsec) 173 { 174 struct metadata_dst *md_dst = skb_metadata_dst(skb); 175 struct nfp_ipsec_offload offload_info; 176 unsigned char *data; 177 bool vlan_insert; 178 u32 meta_id = 0; 179 int md_bytes; 180 181 #ifdef CONFIG_NFP_NET_IPSEC 182 if (xfrm_offload(skb)) 183 *ipsec = nfp_net_ipsec_tx_prep(dp, skb, &offload_info); 184 #endif 185 186 if (unlikely(md_dst && md_dst->type != METADATA_HW_PORT_MUX)) 187 md_dst = NULL; 188 189 vlan_insert = skb_vlan_tag_present(skb) && (dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN_V2); 190 191 if (!(md_dst || tls_handle || vlan_insert || *ipsec)) 192 return 0; 193 194 md_bytes = sizeof(meta_id) + 195 !!md_dst * NFP_NET_META_PORTID_SIZE + 196 !!tls_handle * NFP_NET_META_CONN_HANDLE_SIZE + 197 vlan_insert * NFP_NET_META_VLAN_SIZE + 198 *ipsec * NFP_NET_META_IPSEC_FIELD_SIZE; /* IPsec has 12 bytes of metadata */ 199 200 if (unlikely(skb_cow_head(skb, md_bytes))) 201 return -ENOMEM; 202 203 data = skb_push(skb, md_bytes) + md_bytes; 204 if (md_dst) { 205 data -= NFP_NET_META_PORTID_SIZE; 206 put_unaligned_be32(md_dst->u.port_info.port_id, data); 207 meta_id = NFP_NET_META_PORTID; 208 } 209 if (tls_handle) { 210 /* conn handle is opaque, we just use u64 to be able to quickly 211 * compare it to zero 212 */ 213 data -= NFP_NET_META_CONN_HANDLE_SIZE; 214 memcpy(data, &tls_handle, sizeof(tls_handle)); 215 meta_id <<= NFP_NET_META_FIELD_SIZE; 216 meta_id |= NFP_NET_META_CONN_HANDLE; 217 } 218 if (vlan_insert) { 219 data -= NFP_NET_META_VLAN_SIZE; 220 /* data type of skb->vlan_proto is __be16 221 * so it fills metadata without calling put_unaligned_be16 222 */ 223 memcpy(data, &skb->vlan_proto, sizeof(skb->vlan_proto)); 224 put_unaligned_be16(skb_vlan_tag_get(skb), data + sizeof(skb->vlan_proto)); 225 meta_id <<= NFP_NET_META_FIELD_SIZE; 226 meta_id |= NFP_NET_META_VLAN; 227 } 228 if (*ipsec) { 229 /* IPsec has three consecutive 4-bit IPsec metadata types, 230 * so in total IPsec has three 4 bytes of metadata. 231 */ 232 data -= NFP_NET_META_IPSEC_SIZE; 233 put_unaligned_be32(offload_info.seq_hi, data); 234 data -= NFP_NET_META_IPSEC_SIZE; 235 put_unaligned_be32(offload_info.seq_low, data); 236 data -= NFP_NET_META_IPSEC_SIZE; 237 put_unaligned_be32(offload_info.handle - 1, data); 238 meta_id <<= NFP_NET_META_IPSEC_FIELD_SIZE; 239 meta_id |= NFP_NET_META_IPSEC << 8 | NFP_NET_META_IPSEC << 4 | NFP_NET_META_IPSEC; 240 } 241 242 data -= sizeof(meta_id); 243 put_unaligned_be32(meta_id, data); 244 245 return md_bytes; 246 } 247 248 /** 249 * nfp_nfd3_tx() - Main transmit entry point 250 * @skb: SKB to transmit 251 * @netdev: netdev structure 252 * 253 * Return: NETDEV_TX_OK on success. 254 */ 255 netdev_tx_t nfp_nfd3_tx(struct sk_buff *skb, struct net_device *netdev) 256 { 257 struct nfp_net *nn = netdev_priv(netdev); 258 int f, nr_frags, wr_idx, md_bytes; 259 struct nfp_net_tx_ring *tx_ring; 260 struct nfp_net_r_vector *r_vec; 261 struct nfp_nfd3_tx_buf *txbuf; 262 struct nfp_nfd3_tx_desc *txd; 263 struct netdev_queue *nd_q; 264 const skb_frag_t *frag; 265 struct nfp_net_dp *dp; 266 dma_addr_t dma_addr; 267 unsigned int fsize; 268 u64 tls_handle = 0; 269 bool ipsec = false; 270 u16 qidx; 271 272 dp = &nn->dp; 273 qidx = skb_get_queue_mapping(skb); 274 tx_ring = &dp->tx_rings[qidx]; 275 r_vec = tx_ring->r_vec; 276 277 nr_frags = skb_shinfo(skb)->nr_frags; 278 279 if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) { 280 nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n", 281 qidx, tx_ring->wr_p, tx_ring->rd_p); 282 nd_q = netdev_get_tx_queue(dp->netdev, qidx); 283 netif_tx_stop_queue(nd_q); 284 nfp_net_tx_xmit_more_flush(tx_ring); 285 u64_stats_update_begin(&r_vec->tx_sync); 286 r_vec->tx_busy++; 287 u64_stats_update_end(&r_vec->tx_sync); 288 return NETDEV_TX_BUSY; 289 } 290 291 skb = nfp_net_tls_tx(dp, r_vec, skb, &tls_handle, &nr_frags); 292 if (unlikely(!skb)) { 293 nfp_net_tx_xmit_more_flush(tx_ring); 294 return NETDEV_TX_OK; 295 } 296 297 md_bytes = nfp_nfd3_prep_tx_meta(dp, skb, tls_handle, &ipsec); 298 if (unlikely(md_bytes < 0)) 299 goto err_flush; 300 301 /* Start with the head skbuf */ 302 dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb), 303 DMA_TO_DEVICE); 304 if (dma_mapping_error(dp->dev, dma_addr)) 305 goto err_dma_err; 306 307 wr_idx = D_IDX(tx_ring, tx_ring->wr_p); 308 309 /* Stash the soft descriptor of the head then initialize it */ 310 txbuf = &tx_ring->txbufs[wr_idx]; 311 txbuf->skb = skb; 312 txbuf->dma_addr = dma_addr; 313 txbuf->fidx = -1; 314 txbuf->pkt_cnt = 1; 315 txbuf->real_len = skb->len; 316 317 /* Build TX descriptor */ 318 txd = &tx_ring->txds[wr_idx]; 319 txd->offset_eop = (nr_frags ? 0 : NFD3_DESC_TX_EOP) | md_bytes; 320 txd->dma_len = cpu_to_le16(skb_headlen(skb)); 321 nfp_desc_set_dma_addr_40b(txd, dma_addr); 322 txd->data_len = cpu_to_le16(skb->len); 323 324 txd->flags = 0; 325 txd->mss = 0; 326 txd->lso_hdrlen = 0; 327 328 /* Do not reorder - tso may adjust pkt cnt, vlan may override fields */ 329 nfp_nfd3_tx_tso(r_vec, txbuf, txd, skb, md_bytes); 330 nfp_nfd3_tx_csum(dp, r_vec, txbuf, txd, skb); 331 if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) { 332 txd->flags |= NFD3_DESC_TX_VLAN; 333 txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb)); 334 } 335 336 if (ipsec) 337 nfp_nfd3_ipsec_tx(txd, skb); 338 /* Gather DMA */ 339 if (nr_frags > 0) { 340 __le64 second_half; 341 342 /* all descs must match except for in addr, length and eop */ 343 second_half = txd->vals8[1]; 344 345 for (f = 0; f < nr_frags; f++) { 346 frag = &skb_shinfo(skb)->frags[f]; 347 fsize = skb_frag_size(frag); 348 349 dma_addr = skb_frag_dma_map(dp->dev, frag, 0, 350 fsize, DMA_TO_DEVICE); 351 if (dma_mapping_error(dp->dev, dma_addr)) 352 goto err_unmap; 353 354 wr_idx = D_IDX(tx_ring, wr_idx + 1); 355 tx_ring->txbufs[wr_idx].skb = skb; 356 tx_ring->txbufs[wr_idx].dma_addr = dma_addr; 357 tx_ring->txbufs[wr_idx].fidx = f; 358 359 txd = &tx_ring->txds[wr_idx]; 360 txd->dma_len = cpu_to_le16(fsize); 361 nfp_desc_set_dma_addr_40b(txd, dma_addr); 362 txd->offset_eop = md_bytes | 363 ((f == nr_frags - 1) ? NFD3_DESC_TX_EOP : 0); 364 txd->vals8[1] = second_half; 365 } 366 367 u64_stats_update_begin(&r_vec->tx_sync); 368 r_vec->tx_gather++; 369 u64_stats_update_end(&r_vec->tx_sync); 370 } 371 372 skb_tx_timestamp(skb); 373 374 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx); 375 376 tx_ring->wr_p += nr_frags + 1; 377 if (nfp_nfd3_tx_ring_should_stop(tx_ring)) 378 nfp_nfd3_tx_ring_stop(nd_q, tx_ring); 379 380 tx_ring->wr_ptr_add += nr_frags + 1; 381 if (__netdev_tx_sent_queue(nd_q, txbuf->real_len, netdev_xmit_more())) 382 nfp_net_tx_xmit_more_flush(tx_ring); 383 384 return NETDEV_TX_OK; 385 386 err_unmap: 387 while (--f >= 0) { 388 frag = &skb_shinfo(skb)->frags[f]; 389 dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr, 390 skb_frag_size(frag), DMA_TO_DEVICE); 391 tx_ring->txbufs[wr_idx].skb = NULL; 392 tx_ring->txbufs[wr_idx].dma_addr = 0; 393 tx_ring->txbufs[wr_idx].fidx = -2; 394 wr_idx = wr_idx - 1; 395 if (wr_idx < 0) 396 wr_idx += tx_ring->cnt; 397 } 398 dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr, 399 skb_headlen(skb), DMA_TO_DEVICE); 400 tx_ring->txbufs[wr_idx].skb = NULL; 401 tx_ring->txbufs[wr_idx].dma_addr = 0; 402 tx_ring->txbufs[wr_idx].fidx = -2; 403 err_dma_err: 404 nn_dp_warn(dp, "Failed to map DMA TX buffer\n"); 405 err_flush: 406 nfp_net_tx_xmit_more_flush(tx_ring); 407 u64_stats_update_begin(&r_vec->tx_sync); 408 r_vec->tx_errors++; 409 u64_stats_update_end(&r_vec->tx_sync); 410 nfp_net_tls_tx_undo(skb, tls_handle); 411 dev_kfree_skb_any(skb); 412 return NETDEV_TX_OK; 413 } 414 415 /** 416 * nfp_nfd3_tx_complete() - Handled completed TX packets 417 * @tx_ring: TX ring structure 418 * @budget: NAPI budget (only used as bool to determine if in NAPI context) 419 */ 420 void nfp_nfd3_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget) 421 { 422 struct nfp_net_r_vector *r_vec = tx_ring->r_vec; 423 struct nfp_net_dp *dp = &r_vec->nfp_net->dp; 424 u32 done_pkts = 0, done_bytes = 0; 425 struct netdev_queue *nd_q; 426 u32 qcp_rd_p; 427 int todo; 428 429 if (tx_ring->wr_p == tx_ring->rd_p) 430 return; 431 432 /* Work out how many descriptors have been transmitted */ 433 qcp_rd_p = nfp_net_read_tx_cmpl(tx_ring, dp); 434 435 if (qcp_rd_p == tx_ring->qcp_rd_p) 436 return; 437 438 todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p); 439 440 while (todo--) { 441 const skb_frag_t *frag; 442 struct nfp_nfd3_tx_buf *tx_buf; 443 struct sk_buff *skb; 444 int fidx, nr_frags; 445 int idx; 446 447 idx = D_IDX(tx_ring, tx_ring->rd_p++); 448 tx_buf = &tx_ring->txbufs[idx]; 449 450 skb = tx_buf->skb; 451 if (!skb) 452 continue; 453 454 nr_frags = skb_shinfo(skb)->nr_frags; 455 fidx = tx_buf->fidx; 456 457 if (fidx == -1) { 458 /* unmap head */ 459 dma_unmap_single(dp->dev, tx_buf->dma_addr, 460 skb_headlen(skb), DMA_TO_DEVICE); 461 462 done_pkts += tx_buf->pkt_cnt; 463 done_bytes += tx_buf->real_len; 464 } else { 465 /* unmap fragment */ 466 frag = &skb_shinfo(skb)->frags[fidx]; 467 dma_unmap_page(dp->dev, tx_buf->dma_addr, 468 skb_frag_size(frag), DMA_TO_DEVICE); 469 } 470 471 /* check for last gather fragment */ 472 if (fidx == nr_frags - 1) 473 napi_consume_skb(skb, budget); 474 475 tx_buf->dma_addr = 0; 476 tx_buf->skb = NULL; 477 tx_buf->fidx = -2; 478 } 479 480 tx_ring->qcp_rd_p = qcp_rd_p; 481 482 u64_stats_update_begin(&r_vec->tx_sync); 483 r_vec->tx_bytes += done_bytes; 484 r_vec->tx_pkts += done_pkts; 485 u64_stats_update_end(&r_vec->tx_sync); 486 487 if (!dp->netdev) 488 return; 489 490 nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx); 491 netdev_tx_completed_queue(nd_q, done_pkts, done_bytes); 492 if (nfp_nfd3_tx_ring_should_wake(tx_ring)) { 493 /* Make sure TX thread will see updated tx_ring->rd_p */ 494 smp_mb(); 495 496 if (unlikely(netif_tx_queue_stopped(nd_q))) 497 netif_tx_wake_queue(nd_q); 498 } 499 500 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt, 501 "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n", 502 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt); 503 } 504 505 static bool nfp_nfd3_xdp_complete(struct nfp_net_tx_ring *tx_ring) 506 { 507 struct nfp_net_r_vector *r_vec = tx_ring->r_vec; 508 struct nfp_net_dp *dp = &r_vec->nfp_net->dp; 509 u32 done_pkts = 0, done_bytes = 0; 510 bool done_all; 511 int idx, todo; 512 u32 qcp_rd_p; 513 514 /* Work out how many descriptors have been transmitted */ 515 qcp_rd_p = nfp_net_read_tx_cmpl(tx_ring, dp); 516 517 if (qcp_rd_p == tx_ring->qcp_rd_p) 518 return true; 519 520 todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p); 521 522 done_all = todo <= NFP_NET_XDP_MAX_COMPLETE; 523 todo = min(todo, NFP_NET_XDP_MAX_COMPLETE); 524 525 tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo); 526 527 done_pkts = todo; 528 while (todo--) { 529 idx = D_IDX(tx_ring, tx_ring->rd_p); 530 tx_ring->rd_p++; 531 532 done_bytes += tx_ring->txbufs[idx].real_len; 533 } 534 535 u64_stats_update_begin(&r_vec->tx_sync); 536 r_vec->tx_bytes += done_bytes; 537 r_vec->tx_pkts += done_pkts; 538 u64_stats_update_end(&r_vec->tx_sync); 539 540 WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt, 541 "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n", 542 tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt); 543 544 return done_all; 545 } 546 547 /* Receive processing 548 */ 549 550 static void * 551 nfp_nfd3_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr) 552 { 553 void *frag; 554 555 if (!dp->xdp_prog) { 556 frag = napi_alloc_frag(dp->fl_bufsz); 557 if (unlikely(!frag)) 558 return NULL; 559 } else { 560 struct page *page; 561 562 page = dev_alloc_page(); 563 if (unlikely(!page)) 564 return NULL; 565 frag = page_address(page); 566 } 567 568 *dma_addr = nfp_net_dma_map_rx(dp, frag); 569 if (dma_mapping_error(dp->dev, *dma_addr)) { 570 nfp_net_free_frag(frag, dp->xdp_prog); 571 nn_dp_warn(dp, "Failed to map DMA RX buffer\n"); 572 return NULL; 573 } 574 575 return frag; 576 } 577 578 /** 579 * nfp_nfd3_rx_give_one() - Put mapped skb on the software and hardware rings 580 * @dp: NFP Net data path struct 581 * @rx_ring: RX ring structure 582 * @frag: page fragment buffer 583 * @dma_addr: DMA address of skb mapping 584 */ 585 static void 586 nfp_nfd3_rx_give_one(const struct nfp_net_dp *dp, 587 struct nfp_net_rx_ring *rx_ring, 588 void *frag, dma_addr_t dma_addr) 589 { 590 unsigned int wr_idx; 591 592 wr_idx = D_IDX(rx_ring, rx_ring->wr_p); 593 594 nfp_net_dma_sync_dev_rx(dp, dma_addr); 595 596 /* Stash SKB and DMA address away */ 597 rx_ring->rxbufs[wr_idx].frag = frag; 598 rx_ring->rxbufs[wr_idx].dma_addr = dma_addr; 599 600 /* Fill freelist descriptor */ 601 rx_ring->rxds[wr_idx].fld.reserved = 0; 602 rx_ring->rxds[wr_idx].fld.meta_len_dd = 0; 603 /* DMA address is expanded to 48-bit width in freelist for NFP3800, 604 * so the *_48b macro is used accordingly, it's also OK to fill 605 * a 40-bit address since the top 8 bits are get set to 0. 606 */ 607 nfp_desc_set_dma_addr_48b(&rx_ring->rxds[wr_idx].fld, 608 dma_addr + dp->rx_dma_off); 609 610 rx_ring->wr_p++; 611 if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) { 612 /* Update write pointer of the freelist queue. Make 613 * sure all writes are flushed before telling the hardware. 614 */ 615 wmb(); 616 nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH); 617 } 618 } 619 620 /** 621 * nfp_nfd3_rx_ring_fill_freelist() - Give buffers from the ring to FW 622 * @dp: NFP Net data path struct 623 * @rx_ring: RX ring to fill 624 */ 625 void nfp_nfd3_rx_ring_fill_freelist(struct nfp_net_dp *dp, 626 struct nfp_net_rx_ring *rx_ring) 627 { 628 unsigned int i; 629 630 if (nfp_net_has_xsk_pool_slow(dp, rx_ring->idx)) 631 return nfp_net_xsk_rx_ring_fill_freelist(rx_ring); 632 633 for (i = 0; i < rx_ring->cnt - 1; i++) 634 nfp_nfd3_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag, 635 rx_ring->rxbufs[i].dma_addr); 636 } 637 638 /** 639 * nfp_nfd3_rx_csum_has_errors() - group check if rxd has any csum errors 640 * @flags: RX descriptor flags field in CPU byte order 641 */ 642 static int nfp_nfd3_rx_csum_has_errors(u16 flags) 643 { 644 u16 csum_all_checked, csum_all_ok; 645 646 csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL; 647 csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK; 648 649 return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT); 650 } 651 652 /** 653 * nfp_nfd3_rx_csum() - set SKB checksum field based on RX descriptor flags 654 * @dp: NFP Net data path struct 655 * @r_vec: per-ring structure 656 * @rxd: Pointer to RX descriptor 657 * @meta: Parsed metadata prepend 658 * @skb: Pointer to SKB 659 */ 660 void 661 nfp_nfd3_rx_csum(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec, 662 const struct nfp_net_rx_desc *rxd, 663 const struct nfp_meta_parsed *meta, struct sk_buff *skb) 664 { 665 skb_checksum_none_assert(skb); 666 667 if (!(dp->netdev->features & NETIF_F_RXCSUM)) 668 return; 669 670 if (meta->csum_type) { 671 skb->ip_summed = meta->csum_type; 672 skb->csum = meta->csum; 673 u64_stats_update_begin(&r_vec->rx_sync); 674 r_vec->hw_csum_rx_complete++; 675 u64_stats_update_end(&r_vec->rx_sync); 676 return; 677 } 678 679 if (nfp_nfd3_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) { 680 u64_stats_update_begin(&r_vec->rx_sync); 681 r_vec->hw_csum_rx_error++; 682 u64_stats_update_end(&r_vec->rx_sync); 683 return; 684 } 685 686 /* Assume that the firmware will never report inner CSUM_OK unless outer 687 * L4 headers were successfully parsed. FW will always report zero UDP 688 * checksum as CSUM_OK. 689 */ 690 if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK || 691 rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) { 692 __skb_incr_checksum_unnecessary(skb); 693 u64_stats_update_begin(&r_vec->rx_sync); 694 r_vec->hw_csum_rx_ok++; 695 u64_stats_update_end(&r_vec->rx_sync); 696 } 697 698 if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK || 699 rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) { 700 __skb_incr_checksum_unnecessary(skb); 701 u64_stats_update_begin(&r_vec->rx_sync); 702 r_vec->hw_csum_rx_inner_ok++; 703 u64_stats_update_end(&r_vec->rx_sync); 704 } 705 } 706 707 static void 708 nfp_nfd3_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta, 709 unsigned int type, __be32 *hash) 710 { 711 if (!(netdev->features & NETIF_F_RXHASH)) 712 return; 713 714 switch (type) { 715 case NFP_NET_RSS_IPV4: 716 case NFP_NET_RSS_IPV6: 717 case NFP_NET_RSS_IPV6_EX: 718 meta->hash_type = PKT_HASH_TYPE_L3; 719 break; 720 default: 721 meta->hash_type = PKT_HASH_TYPE_L4; 722 break; 723 } 724 725 meta->hash = get_unaligned_be32(hash); 726 } 727 728 static void 729 nfp_nfd3_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta, 730 void *data, struct nfp_net_rx_desc *rxd) 731 { 732 struct nfp_net_rx_hash *rx_hash = data; 733 734 if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS)) 735 return; 736 737 nfp_nfd3_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type), 738 &rx_hash->hash); 739 } 740 741 bool 742 nfp_nfd3_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta, 743 void *data, void *pkt, unsigned int pkt_len, int meta_len) 744 { 745 u32 meta_info, vlan_info; 746 747 meta_info = get_unaligned_be32(data); 748 data += 4; 749 750 while (meta_info) { 751 switch (meta_info & NFP_NET_META_FIELD_MASK) { 752 case NFP_NET_META_HASH: 753 meta_info >>= NFP_NET_META_FIELD_SIZE; 754 nfp_nfd3_set_hash(netdev, meta, 755 meta_info & NFP_NET_META_FIELD_MASK, 756 (__be32 *)data); 757 data += 4; 758 break; 759 case NFP_NET_META_MARK: 760 meta->mark = get_unaligned_be32(data); 761 data += 4; 762 break; 763 case NFP_NET_META_VLAN: 764 vlan_info = get_unaligned_be32(data); 765 if (FIELD_GET(NFP_NET_META_VLAN_STRIP, vlan_info)) { 766 meta->vlan.stripped = true; 767 meta->vlan.tpid = FIELD_GET(NFP_NET_META_VLAN_TPID_MASK, 768 vlan_info); 769 meta->vlan.tci = FIELD_GET(NFP_NET_META_VLAN_TCI_MASK, 770 vlan_info); 771 } 772 data += 4; 773 break; 774 case NFP_NET_META_PORTID: 775 meta->portid = get_unaligned_be32(data); 776 data += 4; 777 break; 778 case NFP_NET_META_CSUM: 779 meta->csum_type = CHECKSUM_COMPLETE; 780 meta->csum = 781 (__force __wsum)__get_unaligned_cpu32(data); 782 data += 4; 783 break; 784 case NFP_NET_META_RESYNC_INFO: 785 if (nfp_net_tls_rx_resync_req(netdev, data, pkt, 786 pkt_len)) 787 return false; 788 data += sizeof(struct nfp_net_tls_resync_req); 789 break; 790 #ifdef CONFIG_NFP_NET_IPSEC 791 case NFP_NET_META_IPSEC: 792 /* Note: IPsec packet will have zero saidx, so need add 1 793 * to indicate packet is IPsec packet within driver. 794 */ 795 meta->ipsec_saidx = get_unaligned_be32(data) + 1; 796 data += 4; 797 break; 798 #endif 799 default: 800 return true; 801 } 802 803 meta_info >>= NFP_NET_META_FIELD_SIZE; 804 } 805 806 return data != pkt; 807 } 808 809 static void 810 nfp_nfd3_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec, 811 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf, 812 struct sk_buff *skb) 813 { 814 u64_stats_update_begin(&r_vec->rx_sync); 815 r_vec->rx_drops++; 816 /* If we have both skb and rxbuf the replacement buffer allocation 817 * must have failed, count this as an alloc failure. 818 */ 819 if (skb && rxbuf) 820 r_vec->rx_replace_buf_alloc_fail++; 821 u64_stats_update_end(&r_vec->rx_sync); 822 823 /* skb is build based on the frag, free_skb() would free the frag 824 * so to be able to reuse it we need an extra ref. 825 */ 826 if (skb && rxbuf && skb->head == rxbuf->frag) 827 page_ref_inc(virt_to_head_page(rxbuf->frag)); 828 if (rxbuf) 829 nfp_nfd3_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr); 830 if (skb) 831 dev_kfree_skb_any(skb); 832 } 833 834 static bool 835 nfp_nfd3_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring, 836 struct nfp_net_tx_ring *tx_ring, 837 struct nfp_net_rx_buf *rxbuf, unsigned int dma_off, 838 unsigned int pkt_len, bool *completed) 839 { 840 unsigned int dma_map_sz = dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA; 841 struct nfp_nfd3_tx_buf *txbuf; 842 struct nfp_nfd3_tx_desc *txd; 843 int wr_idx; 844 845 /* Reject if xdp_adjust_tail grow packet beyond DMA area */ 846 if (pkt_len + dma_off > dma_map_sz) 847 return false; 848 849 if (unlikely(nfp_net_tx_full(tx_ring, 1))) { 850 if (!*completed) { 851 nfp_nfd3_xdp_complete(tx_ring); 852 *completed = true; 853 } 854 855 if (unlikely(nfp_net_tx_full(tx_ring, 1))) { 856 nfp_nfd3_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf, 857 NULL); 858 return false; 859 } 860 } 861 862 wr_idx = D_IDX(tx_ring, tx_ring->wr_p); 863 864 /* Stash the soft descriptor of the head then initialize it */ 865 txbuf = &tx_ring->txbufs[wr_idx]; 866 867 nfp_nfd3_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr); 868 869 txbuf->frag = rxbuf->frag; 870 txbuf->dma_addr = rxbuf->dma_addr; 871 txbuf->fidx = -1; 872 txbuf->pkt_cnt = 1; 873 txbuf->real_len = pkt_len; 874 875 dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off, 876 pkt_len, DMA_BIDIRECTIONAL); 877 878 /* Build TX descriptor */ 879 txd = &tx_ring->txds[wr_idx]; 880 txd->offset_eop = NFD3_DESC_TX_EOP; 881 txd->dma_len = cpu_to_le16(pkt_len); 882 nfp_desc_set_dma_addr_40b(txd, rxbuf->dma_addr + dma_off); 883 txd->data_len = cpu_to_le16(pkt_len); 884 885 txd->flags = 0; 886 txd->mss = 0; 887 txd->lso_hdrlen = 0; 888 889 tx_ring->wr_p++; 890 tx_ring->wr_ptr_add++; 891 return true; 892 } 893 894 /** 895 * nfp_nfd3_rx() - receive up to @budget packets on @rx_ring 896 * @rx_ring: RX ring to receive from 897 * @budget: NAPI budget 898 * 899 * Note, this function is separated out from the napi poll function to 900 * more cleanly separate packet receive code from other bookkeeping 901 * functions performed in the napi poll function. 902 * 903 * Return: Number of packets received. 904 */ 905 static int nfp_nfd3_rx(struct nfp_net_rx_ring *rx_ring, int budget) 906 { 907 struct nfp_net_r_vector *r_vec = rx_ring->r_vec; 908 struct nfp_net_dp *dp = &r_vec->nfp_net->dp; 909 struct nfp_net_tx_ring *tx_ring; 910 struct bpf_prog *xdp_prog; 911 int idx, pkts_polled = 0; 912 bool xdp_tx_cmpl = false; 913 unsigned int true_bufsz; 914 struct sk_buff *skb; 915 struct xdp_buff xdp; 916 917 xdp_prog = READ_ONCE(dp->xdp_prog); 918 true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz; 919 xdp_init_buff(&xdp, PAGE_SIZE - NFP_NET_RX_BUF_HEADROOM, 920 &rx_ring->xdp_rxq); 921 tx_ring = r_vec->xdp_ring; 922 923 while (pkts_polled < budget) { 924 unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off; 925 struct nfp_net_rx_buf *rxbuf; 926 struct nfp_net_rx_desc *rxd; 927 struct nfp_meta_parsed meta; 928 bool redir_egress = false; 929 struct net_device *netdev; 930 dma_addr_t new_dma_addr; 931 u32 meta_len_xdp = 0; 932 void *new_frag; 933 934 idx = D_IDX(rx_ring, rx_ring->rd_p); 935 936 rxd = &rx_ring->rxds[idx]; 937 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD)) 938 break; 939 940 /* Memory barrier to ensure that we won't do other reads 941 * before the DD bit. 942 */ 943 dma_rmb(); 944 945 memset(&meta, 0, sizeof(meta)); 946 947 rx_ring->rd_p++; 948 pkts_polled++; 949 950 rxbuf = &rx_ring->rxbufs[idx]; 951 /* < meta_len > 952 * <-- [rx_offset] --> 953 * --------------------------------------------------------- 954 * | [XX] | metadata | packet | XXXX | 955 * --------------------------------------------------------- 956 * <---------------- data_len ---------------> 957 * 958 * The rx_offset is fixed for all packets, the meta_len can vary 959 * on a packet by packet basis. If rx_offset is set to zero 960 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the 961 * buffer and is immediately followed by the packet (no [XX]). 962 */ 963 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK; 964 data_len = le16_to_cpu(rxd->rxd.data_len); 965 pkt_len = data_len - meta_len; 966 967 pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off; 968 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC) 969 pkt_off += meta_len; 970 else 971 pkt_off += dp->rx_offset; 972 meta_off = pkt_off - meta_len; 973 974 /* Stats update */ 975 u64_stats_update_begin(&r_vec->rx_sync); 976 r_vec->rx_pkts++; 977 r_vec->rx_bytes += pkt_len; 978 u64_stats_update_end(&r_vec->rx_sync); 979 980 if (unlikely(meta_len > NFP_NET_MAX_PREPEND || 981 (dp->rx_offset && meta_len > dp->rx_offset))) { 982 nn_dp_warn(dp, "oversized RX packet metadata %u\n", 983 meta_len); 984 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL); 985 continue; 986 } 987 988 nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off, 989 data_len); 990 991 if (!dp->chained_metadata_format) { 992 nfp_nfd3_set_hash_desc(dp->netdev, &meta, 993 rxbuf->frag + meta_off, rxd); 994 } else if (meta_len) { 995 if (unlikely(nfp_nfd3_parse_meta(dp->netdev, &meta, 996 rxbuf->frag + meta_off, 997 rxbuf->frag + pkt_off, 998 pkt_len, meta_len))) { 999 nn_dp_warn(dp, "invalid RX packet metadata\n"); 1000 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, 1001 NULL); 1002 continue; 1003 } 1004 } 1005 1006 if (xdp_prog && !meta.portid) { 1007 void *orig_data = rxbuf->frag + pkt_off; 1008 unsigned int dma_off; 1009 int act; 1010 1011 xdp_prepare_buff(&xdp, 1012 rxbuf->frag + NFP_NET_RX_BUF_HEADROOM, 1013 pkt_off - NFP_NET_RX_BUF_HEADROOM, 1014 pkt_len, true); 1015 1016 act = bpf_prog_run_xdp(xdp_prog, &xdp); 1017 1018 pkt_len = xdp.data_end - xdp.data; 1019 pkt_off += xdp.data - orig_data; 1020 1021 switch (act) { 1022 case XDP_PASS: 1023 meta_len_xdp = xdp.data - xdp.data_meta; 1024 break; 1025 case XDP_TX: 1026 dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM; 1027 if (unlikely(!nfp_nfd3_tx_xdp_buf(dp, rx_ring, 1028 tx_ring, 1029 rxbuf, 1030 dma_off, 1031 pkt_len, 1032 &xdp_tx_cmpl))) 1033 trace_xdp_exception(dp->netdev, 1034 xdp_prog, act); 1035 continue; 1036 default: 1037 bpf_warn_invalid_xdp_action(dp->netdev, xdp_prog, act); 1038 fallthrough; 1039 case XDP_ABORTED: 1040 trace_xdp_exception(dp->netdev, xdp_prog, act); 1041 fallthrough; 1042 case XDP_DROP: 1043 nfp_nfd3_rx_give_one(dp, rx_ring, rxbuf->frag, 1044 rxbuf->dma_addr); 1045 continue; 1046 } 1047 } 1048 1049 if (likely(!meta.portid)) { 1050 netdev = dp->netdev; 1051 } else if (meta.portid == NFP_META_PORT_ID_CTRL) { 1052 struct nfp_net *nn = netdev_priv(dp->netdev); 1053 1054 nfp_app_ctrl_rx_raw(nn->app, rxbuf->frag + pkt_off, 1055 pkt_len); 1056 nfp_nfd3_rx_give_one(dp, rx_ring, rxbuf->frag, 1057 rxbuf->dma_addr); 1058 continue; 1059 } else { 1060 struct nfp_net *nn; 1061 1062 nn = netdev_priv(dp->netdev); 1063 netdev = nfp_app_dev_get(nn->app, meta.portid, 1064 &redir_egress); 1065 if (unlikely(!netdev)) { 1066 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, 1067 NULL); 1068 continue; 1069 } 1070 1071 if (nfp_netdev_is_nfp_repr(netdev)) 1072 nfp_repr_inc_rx_stats(netdev, pkt_len); 1073 } 1074 1075 skb = build_skb(rxbuf->frag, true_bufsz); 1076 if (unlikely(!skb)) { 1077 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL); 1078 continue; 1079 } 1080 new_frag = nfp_nfd3_napi_alloc_one(dp, &new_dma_addr); 1081 if (unlikely(!new_frag)) { 1082 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, skb); 1083 continue; 1084 } 1085 1086 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr); 1087 1088 nfp_nfd3_rx_give_one(dp, rx_ring, new_frag, new_dma_addr); 1089 1090 skb_reserve(skb, pkt_off); 1091 skb_put(skb, pkt_len); 1092 1093 skb->mark = meta.mark; 1094 skb_set_hash(skb, meta.hash, meta.hash_type); 1095 1096 skb_record_rx_queue(skb, rx_ring->idx); 1097 skb->protocol = eth_type_trans(skb, netdev); 1098 1099 nfp_nfd3_rx_csum(dp, r_vec, rxd, &meta, skb); 1100 1101 #ifdef CONFIG_TLS_DEVICE 1102 if (rxd->rxd.flags & PCIE_DESC_RX_DECRYPTED) { 1103 skb->decrypted = true; 1104 u64_stats_update_begin(&r_vec->rx_sync); 1105 r_vec->hw_tls_rx++; 1106 u64_stats_update_end(&r_vec->rx_sync); 1107 } 1108 #endif 1109 1110 if (unlikely(!nfp_net_vlan_strip(skb, rxd, &meta))) { 1111 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, NULL, skb); 1112 continue; 1113 } 1114 1115 #ifdef CONFIG_NFP_NET_IPSEC 1116 if (meta.ipsec_saidx != 0 && unlikely(nfp_net_ipsec_rx(&meta, skb))) { 1117 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, NULL, skb); 1118 continue; 1119 } 1120 #endif 1121 1122 if (meta_len_xdp) 1123 skb_metadata_set(skb, meta_len_xdp); 1124 1125 if (likely(!redir_egress)) { 1126 napi_gro_receive(&rx_ring->r_vec->napi, skb); 1127 } else { 1128 skb->dev = netdev; 1129 skb_reset_network_header(skb); 1130 __skb_push(skb, ETH_HLEN); 1131 dev_queue_xmit(skb); 1132 } 1133 } 1134 1135 if (xdp_prog) { 1136 if (tx_ring->wr_ptr_add) 1137 nfp_net_tx_xmit_more_flush(tx_ring); 1138 else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) && 1139 !xdp_tx_cmpl) 1140 if (!nfp_nfd3_xdp_complete(tx_ring)) 1141 pkts_polled = budget; 1142 } 1143 1144 return pkts_polled; 1145 } 1146 1147 /** 1148 * nfp_nfd3_poll() - napi poll function 1149 * @napi: NAPI structure 1150 * @budget: NAPI budget 1151 * 1152 * Return: number of packets polled. 1153 */ 1154 int nfp_nfd3_poll(struct napi_struct *napi, int budget) 1155 { 1156 struct nfp_net_r_vector *r_vec = 1157 container_of(napi, struct nfp_net_r_vector, napi); 1158 unsigned int pkts_polled = 0; 1159 1160 if (r_vec->tx_ring) 1161 nfp_nfd3_tx_complete(r_vec->tx_ring, budget); 1162 if (r_vec->rx_ring) 1163 pkts_polled = nfp_nfd3_rx(r_vec->rx_ring, budget); 1164 1165 if (pkts_polled < budget) 1166 if (napi_complete_done(napi, pkts_polled)) 1167 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry); 1168 1169 if (r_vec->nfp_net->rx_coalesce_adapt_on && r_vec->rx_ring) { 1170 struct dim_sample dim_sample = {}; 1171 unsigned int start; 1172 u64 pkts, bytes; 1173 1174 do { 1175 start = u64_stats_fetch_begin(&r_vec->rx_sync); 1176 pkts = r_vec->rx_pkts; 1177 bytes = r_vec->rx_bytes; 1178 } while (u64_stats_fetch_retry(&r_vec->rx_sync, start)); 1179 1180 dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample); 1181 net_dim(&r_vec->rx_dim, dim_sample); 1182 } 1183 1184 if (r_vec->nfp_net->tx_coalesce_adapt_on && r_vec->tx_ring) { 1185 struct dim_sample dim_sample = {}; 1186 unsigned int start; 1187 u64 pkts, bytes; 1188 1189 do { 1190 start = u64_stats_fetch_begin(&r_vec->tx_sync); 1191 pkts = r_vec->tx_pkts; 1192 bytes = r_vec->tx_bytes; 1193 } while (u64_stats_fetch_retry(&r_vec->tx_sync, start)); 1194 1195 dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample); 1196 net_dim(&r_vec->tx_dim, dim_sample); 1197 } 1198 1199 return pkts_polled; 1200 } 1201 1202 /* Control device data path 1203 */ 1204 1205 bool 1206 nfp_nfd3_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec, 1207 struct sk_buff *skb, bool old) 1208 { 1209 unsigned int real_len = skb->len, meta_len = 0; 1210 struct nfp_net_tx_ring *tx_ring; 1211 struct nfp_nfd3_tx_buf *txbuf; 1212 struct nfp_nfd3_tx_desc *txd; 1213 struct nfp_net_dp *dp; 1214 dma_addr_t dma_addr; 1215 int wr_idx; 1216 1217 dp = &r_vec->nfp_net->dp; 1218 tx_ring = r_vec->tx_ring; 1219 1220 if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) { 1221 nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n"); 1222 goto err_free; 1223 } 1224 1225 if (unlikely(nfp_net_tx_full(tx_ring, 1))) { 1226 u64_stats_update_begin(&r_vec->tx_sync); 1227 r_vec->tx_busy++; 1228 u64_stats_update_end(&r_vec->tx_sync); 1229 if (!old) 1230 __skb_queue_tail(&r_vec->queue, skb); 1231 else 1232 __skb_queue_head(&r_vec->queue, skb); 1233 return true; 1234 } 1235 1236 if (nfp_app_ctrl_has_meta(nn->app)) { 1237 if (unlikely(skb_headroom(skb) < 8)) { 1238 nn_dp_warn(dp, "CTRL TX on skb without headroom\n"); 1239 goto err_free; 1240 } 1241 meta_len = 8; 1242 put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4)); 1243 put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4)); 1244 } 1245 1246 /* Start with the head skbuf */ 1247 dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb), 1248 DMA_TO_DEVICE); 1249 if (dma_mapping_error(dp->dev, dma_addr)) 1250 goto err_dma_warn; 1251 1252 wr_idx = D_IDX(tx_ring, tx_ring->wr_p); 1253 1254 /* Stash the soft descriptor of the head then initialize it */ 1255 txbuf = &tx_ring->txbufs[wr_idx]; 1256 txbuf->skb = skb; 1257 txbuf->dma_addr = dma_addr; 1258 txbuf->fidx = -1; 1259 txbuf->pkt_cnt = 1; 1260 txbuf->real_len = real_len; 1261 1262 /* Build TX descriptor */ 1263 txd = &tx_ring->txds[wr_idx]; 1264 txd->offset_eop = meta_len | NFD3_DESC_TX_EOP; 1265 txd->dma_len = cpu_to_le16(skb_headlen(skb)); 1266 nfp_desc_set_dma_addr_40b(txd, dma_addr); 1267 txd->data_len = cpu_to_le16(skb->len); 1268 1269 txd->flags = 0; 1270 txd->mss = 0; 1271 txd->lso_hdrlen = 0; 1272 1273 tx_ring->wr_p++; 1274 tx_ring->wr_ptr_add++; 1275 nfp_net_tx_xmit_more_flush(tx_ring); 1276 1277 return false; 1278 1279 err_dma_warn: 1280 nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n"); 1281 err_free: 1282 u64_stats_update_begin(&r_vec->tx_sync); 1283 r_vec->tx_errors++; 1284 u64_stats_update_end(&r_vec->tx_sync); 1285 dev_kfree_skb_any(skb); 1286 return false; 1287 } 1288 1289 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec) 1290 { 1291 struct sk_buff *skb; 1292 1293 while ((skb = __skb_dequeue(&r_vec->queue))) 1294 if (nfp_nfd3_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true)) 1295 return; 1296 } 1297 1298 static bool 1299 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len) 1300 { 1301 u32 meta_type, meta_tag; 1302 1303 if (!nfp_app_ctrl_has_meta(nn->app)) 1304 return !meta_len; 1305 1306 if (meta_len != 8) 1307 return false; 1308 1309 meta_type = get_unaligned_be32(data); 1310 meta_tag = get_unaligned_be32(data + 4); 1311 1312 return (meta_type == NFP_NET_META_PORTID && 1313 meta_tag == NFP_META_PORT_ID_CTRL); 1314 } 1315 1316 static bool 1317 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp, 1318 struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring) 1319 { 1320 unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off; 1321 struct nfp_net_rx_buf *rxbuf; 1322 struct nfp_net_rx_desc *rxd; 1323 dma_addr_t new_dma_addr; 1324 struct sk_buff *skb; 1325 void *new_frag; 1326 int idx; 1327 1328 idx = D_IDX(rx_ring, rx_ring->rd_p); 1329 1330 rxd = &rx_ring->rxds[idx]; 1331 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD)) 1332 return false; 1333 1334 /* Memory barrier to ensure that we won't do other reads 1335 * before the DD bit. 1336 */ 1337 dma_rmb(); 1338 1339 rx_ring->rd_p++; 1340 1341 rxbuf = &rx_ring->rxbufs[idx]; 1342 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK; 1343 data_len = le16_to_cpu(rxd->rxd.data_len); 1344 pkt_len = data_len - meta_len; 1345 1346 pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off; 1347 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC) 1348 pkt_off += meta_len; 1349 else 1350 pkt_off += dp->rx_offset; 1351 meta_off = pkt_off - meta_len; 1352 1353 /* Stats update */ 1354 u64_stats_update_begin(&r_vec->rx_sync); 1355 r_vec->rx_pkts++; 1356 r_vec->rx_bytes += pkt_len; 1357 u64_stats_update_end(&r_vec->rx_sync); 1358 1359 nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off, data_len); 1360 1361 if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) { 1362 nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n", 1363 meta_len); 1364 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL); 1365 return true; 1366 } 1367 1368 skb = build_skb(rxbuf->frag, dp->fl_bufsz); 1369 if (unlikely(!skb)) { 1370 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL); 1371 return true; 1372 } 1373 new_frag = nfp_nfd3_napi_alloc_one(dp, &new_dma_addr); 1374 if (unlikely(!new_frag)) { 1375 nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, skb); 1376 return true; 1377 } 1378 1379 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr); 1380 1381 nfp_nfd3_rx_give_one(dp, rx_ring, new_frag, new_dma_addr); 1382 1383 skb_reserve(skb, pkt_off); 1384 skb_put(skb, pkt_len); 1385 1386 nfp_app_ctrl_rx(nn->app, skb); 1387 1388 return true; 1389 } 1390 1391 static bool nfp_ctrl_rx(struct nfp_net_r_vector *r_vec) 1392 { 1393 struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring; 1394 struct nfp_net *nn = r_vec->nfp_net; 1395 struct nfp_net_dp *dp = &nn->dp; 1396 unsigned int budget = 512; 1397 1398 while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring) && budget--) 1399 continue; 1400 1401 return budget; 1402 } 1403 1404 void nfp_nfd3_ctrl_poll(struct tasklet_struct *t) 1405 { 1406 struct nfp_net_r_vector *r_vec = from_tasklet(r_vec, t, tasklet); 1407 1408 spin_lock(&r_vec->lock); 1409 nfp_nfd3_tx_complete(r_vec->tx_ring, 0); 1410 __nfp_ctrl_tx_queued(r_vec); 1411 spin_unlock(&r_vec->lock); 1412 1413 if (nfp_ctrl_rx(r_vec)) { 1414 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry); 1415 } else { 1416 tasklet_schedule(&r_vec->tasklet); 1417 nn_dp_warn(&r_vec->nfp_net->dp, 1418 "control message budget exceeded!\n"); 1419 } 1420 } 1421