1 // SPDX-License-Identifier: GPL-2.0 2 /* Marvell RVU Ethernet driver 3 * 4 * Copyright (C) 2020 Marvell. 5 * 6 */ 7 8 #include <linux/etherdevice.h> 9 #include <net/ip.h> 10 #include <net/tso.h> 11 #include <linux/bpf.h> 12 #include <linux/bpf_trace.h> 13 #include <net/ip6_checksum.h> 14 #include <net/xfrm.h> 15 #include <net/xdp.h> 16 17 #include "otx2_reg.h" 18 #include "otx2_common.h" 19 #include "otx2_struct.h" 20 #include "otx2_txrx.h" 21 #include "otx2_ptp.h" 22 #include "cn10k.h" 23 #include "otx2_xsk.h" 24 25 #define CQE_ADDR(CQ, idx) ((CQ)->cqe_base + ((CQ)->cqe_size * (idx))) 26 #define PTP_PORT 0x13F 27 /* PTPv2 header Original Timestamp starts at byte offset 34 and 28 * contains 6 byte seconds field and 4 byte nano seconds field. 29 */ 30 #define PTP_SYNC_SEC_OFFSET 34 31 32 DEFINE_STATIC_KEY_FALSE(cn10k_ipsec_sa_enabled); 33 34 static int otx2_get_free_sqe(struct otx2_snd_queue *sq) 35 { 36 return (sq->cons_head - sq->head - 1 + sq->sqe_cnt) 37 & (sq->sqe_cnt - 1); 38 } 39 40 static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf, 41 struct bpf_prog *prog, 42 struct nix_cqe_rx_s *cqe, 43 struct otx2_cq_queue *cq, 44 bool *need_xdp_flush); 45 46 static void otx2_sq_set_sqe_base(struct otx2_snd_queue *sq, 47 struct sk_buff *skb) 48 { 49 if (static_branch_unlikely(&cn10k_ipsec_sa_enabled) && 50 (xfrm_offload(skb))) 51 sq->sqe_base = sq->sqe_ring->base + sq->sqe_size + 52 (sq->head * (sq->sqe_size * 2)); 53 else 54 sq->sqe_base = sq->sqe->base; 55 } 56 57 static int otx2_nix_cq_op_status(struct otx2_nic *pfvf, 58 struct otx2_cq_queue *cq) 59 { 60 u64 incr = (u64)(cq->cq_idx) << 32; 61 u64 status; 62 63 status = otx2_atomic64_fetch_add(incr, pfvf->cq_op_addr); 64 65 if (unlikely(status & BIT_ULL(CQ_OP_STAT_OP_ERR) || 66 status & BIT_ULL(CQ_OP_STAT_CQ_ERR))) { 67 dev_err(pfvf->dev, "CQ stopped due to error"); 68 return -EINVAL; 69 } 70 71 cq->cq_tail = status & 0xFFFFF; 72 cq->cq_head = (status >> 20) & 0xFFFFF; 73 if (cq->cq_tail < cq->cq_head) 74 cq->pend_cqe = (cq->cqe_cnt - cq->cq_head) + 75 cq->cq_tail; 76 else 77 cq->pend_cqe = cq->cq_tail - cq->cq_head; 78 79 return 0; 80 } 81 82 static struct nix_cqe_hdr_s *otx2_get_next_cqe(struct otx2_cq_queue *cq) 83 { 84 struct nix_cqe_hdr_s *cqe_hdr; 85 86 cqe_hdr = (struct nix_cqe_hdr_s *)CQE_ADDR(cq, cq->cq_head); 87 if (cqe_hdr->cqe_type == NIX_XQE_TYPE_INVALID) 88 return NULL; 89 90 cq->cq_head++; 91 cq->cq_head &= (cq->cqe_cnt - 1); 92 93 return cqe_hdr; 94 } 95 96 static unsigned int frag_num(unsigned int i) 97 { 98 #ifdef __BIG_ENDIAN 99 return (i & ~3) + 3 - (i & 3); 100 #else 101 return i; 102 #endif 103 } 104 105 static void otx2_xdp_snd_pkt_handler(struct otx2_nic *pfvf, 106 struct otx2_snd_queue *sq, 107 struct nix_cqe_tx_s *cqe, 108 int *xsk_frames) 109 { 110 struct nix_send_comp_s *snd_comp = &cqe->comp; 111 struct sg_list *sg; 112 113 sg = &sq->sg[snd_comp->sqe_id]; 114 if (sg->flags & OTX2_AF_XDP_FRAME) { 115 (*xsk_frames)++; 116 return; 117 } 118 119 if (sg->flags & OTX2_XDP_REDIRECT) 120 otx2_dma_unmap_page(pfvf, sg->dma_addr[0], sg->size[0], DMA_TO_DEVICE); 121 xdp_return_frame((struct xdp_frame *)sg->skb); 122 sg->skb = (u64)NULL; 123 } 124 125 static void otx2_snd_pkt_handler(struct otx2_nic *pfvf, 126 struct otx2_cq_queue *cq, 127 struct otx2_snd_queue *sq, 128 struct nix_cqe_tx_s *cqe, 129 int budget, int *tx_pkts, int *tx_bytes) 130 { 131 struct nix_send_comp_s *snd_comp = &cqe->comp; 132 struct skb_shared_hwtstamps ts; 133 struct sk_buff *skb = NULL; 134 u64 timestamp, tsns; 135 struct sg_list *sg; 136 int err; 137 138 if (unlikely(snd_comp->status) && netif_msg_tx_err(pfvf)) 139 net_err_ratelimited("%s: TX%d: Error in send CQ status:%x\n", 140 pfvf->netdev->name, cq->cint_idx, 141 snd_comp->status); 142 143 sg = &sq->sg[snd_comp->sqe_id]; 144 skb = (struct sk_buff *)sg->skb; 145 if (unlikely(!skb)) 146 return; 147 148 if (skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS) { 149 timestamp = ((u64 *)sq->timestamps->base)[snd_comp->sqe_id]; 150 if (timestamp != 1) { 151 timestamp = pfvf->ptp->convert_tx_ptp_tstmp(timestamp); 152 err = otx2_ptp_tstamp2time(pfvf, timestamp, &tsns); 153 if (!err) { 154 memset(&ts, 0, sizeof(ts)); 155 ts.hwtstamp = ns_to_ktime(tsns); 156 skb_tstamp_tx(skb, &ts); 157 } 158 } 159 } 160 161 *tx_bytes += skb->len; 162 (*tx_pkts)++; 163 otx2_dma_unmap_skb_frags(pfvf, sg); 164 napi_consume_skb(skb, budget); 165 sg->skb = (u64)NULL; 166 } 167 168 static void otx2_set_rxtstamp(struct otx2_nic *pfvf, 169 struct sk_buff *skb, void *data) 170 { 171 u64 timestamp, tsns; 172 int err; 173 174 if (!(pfvf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED)) 175 return; 176 177 timestamp = pfvf->ptp->convert_rx_ptp_tstmp(*(u64 *)data); 178 /* The first 8 bytes is the timestamp */ 179 err = otx2_ptp_tstamp2time(pfvf, timestamp, &tsns); 180 if (err) 181 return; 182 183 skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(tsns); 184 } 185 186 static bool otx2_skb_add_frag(struct otx2_nic *pfvf, struct sk_buff *skb, 187 u64 iova, int len, struct nix_rx_parse_s *parse, 188 int qidx) 189 { 190 struct page *page; 191 int off = 0; 192 void *va; 193 194 va = phys_to_virt(otx2_iova_to_phys(pfvf->iommu_domain, iova)); 195 196 if (likely(!skb_shinfo(skb)->nr_frags)) { 197 /* Check if data starts at some nonzero offset 198 * from the start of the buffer. For now the 199 * only possible offset is 8 bytes in the case 200 * where packet is prepended by a timestamp. 201 */ 202 if (parse->laptr) { 203 otx2_set_rxtstamp(pfvf, skb, va); 204 off = OTX2_HW_TIMESTAMP_LEN; 205 } 206 } 207 208 page = virt_to_page(va); 209 if (likely(skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)) { 210 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 211 va - page_address(page) + off, 212 len - off, pfvf->rbsize); 213 return true; 214 } 215 216 /* If more than MAX_SKB_FRAGS fragments are received then 217 * give back those buffer pointers to hardware for reuse. 218 */ 219 pfvf->hw_ops->aura_freeptr(pfvf, qidx, iova & ~0x07ULL); 220 221 return false; 222 } 223 224 static void otx2_set_rxhash(struct otx2_nic *pfvf, 225 struct nix_cqe_rx_s *cqe, struct sk_buff *skb) 226 { 227 enum pkt_hash_types hash_type = PKT_HASH_TYPE_NONE; 228 struct otx2_rss_info *rss; 229 u32 hash = 0; 230 231 if (!(pfvf->netdev->features & NETIF_F_RXHASH)) 232 return; 233 234 rss = &pfvf->hw.rss_info; 235 if (rss->flowkey_cfg) { 236 if (rss->flowkey_cfg & 237 ~(NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6)) 238 hash_type = PKT_HASH_TYPE_L4; 239 else 240 hash_type = PKT_HASH_TYPE_L3; 241 hash = cqe->hdr.flow_tag; 242 } 243 skb_set_hash(skb, hash, hash_type); 244 } 245 246 static void otx2_free_rcv_seg(struct otx2_nic *pfvf, struct nix_cqe_rx_s *cqe, 247 int qidx) 248 { 249 struct nix_rx_sg_s *sg = &cqe->sg; 250 void *end, *start; 251 u64 *seg_addr; 252 int seg; 253 254 start = (void *)sg; 255 end = start + ((cqe->parse.desc_sizem1 + 1) * 16); 256 while (start < end) { 257 sg = (struct nix_rx_sg_s *)start; 258 seg_addr = &sg->seg_addr; 259 for (seg = 0; seg < sg->segs; seg++, seg_addr++) 260 pfvf->hw_ops->aura_freeptr(pfvf, qidx, 261 *seg_addr & ~0x07ULL); 262 start += sizeof(*sg); 263 } 264 } 265 266 static bool otx2_check_rcv_errors(struct otx2_nic *pfvf, 267 struct nix_cqe_rx_s *cqe, int qidx) 268 { 269 struct otx2_drv_stats *stats = &pfvf->hw.drv_stats; 270 struct nix_rx_parse_s *parse = &cqe->parse; 271 272 if (netif_msg_rx_err(pfvf)) 273 netdev_err(pfvf->netdev, 274 "RQ%d: Error pkt with errlev:0x%x errcode:0x%x\n", 275 qidx, parse->errlev, parse->errcode); 276 277 if (parse->errlev == NPC_ERRLVL_RE) { 278 switch (parse->errcode) { 279 case ERRCODE_FCS: 280 case ERRCODE_FCS_RCV: 281 atomic_inc(&stats->rx_fcs_errs); 282 break; 283 case ERRCODE_UNDERSIZE: 284 atomic_inc(&stats->rx_undersize_errs); 285 break; 286 case ERRCODE_OVERSIZE: 287 atomic_inc(&stats->rx_oversize_errs); 288 break; 289 case ERRCODE_OL2_LEN_MISMATCH: 290 atomic_inc(&stats->rx_len_errs); 291 break; 292 default: 293 atomic_inc(&stats->rx_other_errs); 294 break; 295 } 296 } else if (parse->errlev == NPC_ERRLVL_NIX) { 297 switch (parse->errcode) { 298 case ERRCODE_OL3_LEN: 299 case ERRCODE_OL4_LEN: 300 case ERRCODE_IL3_LEN: 301 case ERRCODE_IL4_LEN: 302 atomic_inc(&stats->rx_len_errs); 303 break; 304 case ERRCODE_OL4_CSUM: 305 case ERRCODE_IL4_CSUM: 306 atomic_inc(&stats->rx_csum_errs); 307 break; 308 default: 309 atomic_inc(&stats->rx_other_errs); 310 break; 311 } 312 } else { 313 atomic_inc(&stats->rx_other_errs); 314 /* For now ignore all the NPC parser errors and 315 * pass the packets to stack. 316 */ 317 return false; 318 } 319 320 /* If RXALL is enabled pass on packets to stack. */ 321 if (pfvf->netdev->features & NETIF_F_RXALL) 322 return false; 323 324 /* Free buffer back to pool */ 325 if (cqe->sg.segs) 326 otx2_free_rcv_seg(pfvf, cqe, qidx); 327 return true; 328 } 329 330 static void otx2_rcv_pkt_handler(struct otx2_nic *pfvf, 331 struct napi_struct *napi, 332 struct otx2_cq_queue *cq, 333 struct nix_cqe_rx_s *cqe, bool *need_xdp_flush) 334 { 335 struct nix_rx_parse_s *parse = &cqe->parse; 336 struct nix_rx_sg_s *sg = &cqe->sg; 337 struct sk_buff *skb = NULL; 338 void *end, *start; 339 u64 *seg_addr; 340 u16 *seg_size; 341 int seg; 342 343 if (unlikely(parse->errlev || parse->errcode)) { 344 if (otx2_check_rcv_errors(pfvf, cqe, cq->cq_idx)) 345 return; 346 } 347 348 if (pfvf->xdp_prog) 349 if (otx2_xdp_rcv_pkt_handler(pfvf, pfvf->xdp_prog, cqe, cq, need_xdp_flush)) 350 return; 351 352 skb = napi_get_frags(napi); 353 if (unlikely(!skb)) 354 return; 355 356 start = (void *)sg; 357 end = start + ((cqe->parse.desc_sizem1 + 1) * 16); 358 while (start < end) { 359 sg = (struct nix_rx_sg_s *)start; 360 seg_addr = &sg->seg_addr; 361 seg_size = (void *)sg; 362 for (seg = 0; seg < sg->segs; seg++, seg_addr++) { 363 if (otx2_skb_add_frag(pfvf, skb, *seg_addr, 364 seg_size[seg], parse, cq->cq_idx)) 365 cq->pool_ptrs++; 366 } 367 start += sizeof(*sg); 368 } 369 otx2_set_rxhash(pfvf, cqe, skb); 370 371 if (!(pfvf->flags & OTX2_FLAG_REP_MODE_ENABLED)) { 372 skb_record_rx_queue(skb, cq->cq_idx); 373 if (pfvf->netdev->features & NETIF_F_RXCSUM) 374 skb->ip_summed = CHECKSUM_UNNECESSARY; 375 } 376 377 if (pfvf->flags & OTX2_FLAG_TC_MARK_ENABLED) 378 skb->mark = parse->match_id; 379 380 skb_mark_for_recycle(skb); 381 382 napi_gro_frags(napi); 383 } 384 385 static int otx2_rx_napi_handler(struct otx2_nic *pfvf, 386 struct napi_struct *napi, 387 struct otx2_cq_queue *cq, int budget) 388 { 389 bool need_xdp_flush = false; 390 struct nix_cqe_rx_s *cqe; 391 int processed_cqe = 0; 392 393 if (cq->pend_cqe >= budget) 394 goto process_cqe; 395 396 if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe) 397 return 0; 398 399 process_cqe: 400 while (likely(processed_cqe < budget) && cq->pend_cqe) { 401 cqe = (struct nix_cqe_rx_s *)CQE_ADDR(cq, cq->cq_head); 402 if (cqe->hdr.cqe_type == NIX_XQE_TYPE_INVALID || 403 !cqe->sg.seg_addr) { 404 if (!processed_cqe) 405 return 0; 406 break; 407 } 408 cq->cq_head++; 409 cq->cq_head &= (cq->cqe_cnt - 1); 410 411 otx2_rcv_pkt_handler(pfvf, napi, cq, cqe, &need_xdp_flush); 412 413 cqe->hdr.cqe_type = NIX_XQE_TYPE_INVALID; 414 cqe->sg.seg_addr = 0x00; 415 processed_cqe++; 416 cq->pend_cqe--; 417 } 418 if (need_xdp_flush) 419 xdp_do_flush(); 420 421 /* Free CQEs to HW */ 422 otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR, 423 ((u64)cq->cq_idx << 32) | processed_cqe); 424 425 return processed_cqe; 426 } 427 428 int otx2_refill_pool_ptrs(void *dev, struct otx2_cq_queue *cq) 429 { 430 struct otx2_nic *pfvf = dev; 431 int cnt = cq->pool_ptrs; 432 dma_addr_t bufptr; 433 434 while (cq->pool_ptrs) { 435 if (otx2_alloc_buffer(pfvf, cq, &bufptr)) 436 break; 437 otx2_aura_freeptr(pfvf, cq->cq_idx, bufptr + OTX2_HEAD_ROOM); 438 cq->pool_ptrs--; 439 } 440 441 return cnt - cq->pool_ptrs; 442 } 443 444 static void otx2_zc_submit_pkts(struct otx2_nic *pfvf, struct xsk_buff_pool *xsk_pool, 445 int *xsk_frames, int qidx, int budget) 446 { 447 if (*xsk_frames) 448 xsk_tx_completed(xsk_pool, *xsk_frames); 449 450 if (xsk_uses_need_wakeup(xsk_pool)) 451 xsk_set_tx_need_wakeup(xsk_pool); 452 453 otx2_zc_napi_handler(pfvf, xsk_pool, qidx, budget); 454 } 455 456 static int otx2_tx_napi_handler(struct otx2_nic *pfvf, 457 struct otx2_cq_queue *cq, int budget) 458 { 459 int tx_pkts = 0, tx_bytes = 0, qidx; 460 struct otx2_snd_queue *sq; 461 struct nix_cqe_tx_s *cqe; 462 struct net_device *ndev; 463 int processed_cqe = 0; 464 int xsk_frames = 0; 465 466 qidx = cq->cq_idx - pfvf->hw.rx_queues; 467 sq = &pfvf->qset.sq[qidx]; 468 469 if (cq->pend_cqe >= budget) 470 goto process_cqe; 471 472 if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe) { 473 if (sq->xsk_pool) 474 otx2_zc_submit_pkts(pfvf, sq->xsk_pool, &xsk_frames, 475 qidx, budget); 476 return 0; 477 } 478 479 process_cqe: 480 481 while (likely(processed_cqe < budget) && cq->pend_cqe) { 482 cqe = (struct nix_cqe_tx_s *)otx2_get_next_cqe(cq); 483 if (unlikely(!cqe)) { 484 if (!processed_cqe) 485 return 0; 486 break; 487 } 488 489 if (cq->cq_type == CQ_XDP) 490 otx2_xdp_snd_pkt_handler(pfvf, sq, cqe, &xsk_frames); 491 else 492 otx2_snd_pkt_handler(pfvf, cq, &pfvf->qset.sq[qidx], 493 cqe, budget, &tx_pkts, &tx_bytes); 494 495 cqe->hdr.cqe_type = NIX_XQE_TYPE_INVALID; 496 processed_cqe++; 497 cq->pend_cqe--; 498 499 sq->cons_head++; 500 sq->cons_head &= (sq->sqe_cnt - 1); 501 } 502 503 /* Free CQEs to HW */ 504 otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR, 505 ((u64)cq->cq_idx << 32) | processed_cqe); 506 507 #if IS_ENABLED(CONFIG_RVU_ESWITCH) 508 if (pfvf->flags & OTX2_FLAG_REP_MODE_ENABLED) 509 ndev = pfvf->reps[qidx]->netdev; 510 else 511 #endif 512 ndev = pfvf->netdev; 513 514 if (likely(tx_pkts)) { 515 struct netdev_queue *txq; 516 517 qidx = cq->cq_idx - pfvf->hw.rx_queues; 518 519 if (qidx >= pfvf->hw.tx_queues) 520 qidx -= pfvf->hw.xdp_queues; 521 if (pfvf->flags & OTX2_FLAG_REP_MODE_ENABLED) 522 qidx = 0; 523 txq = netdev_get_tx_queue(ndev, qidx); 524 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes); 525 /* Check if queue was stopped earlier due to ring full */ 526 smp_mb(); 527 if (netif_tx_queue_stopped(txq) && 528 netif_carrier_ok(ndev)) 529 netif_tx_wake_queue(txq); 530 } 531 532 if (sq->xsk_pool) 533 otx2_zc_submit_pkts(pfvf, sq->xsk_pool, &xsk_frames, qidx, budget); 534 535 return 0; 536 } 537 538 static void otx2_adjust_adaptive_coalese(struct otx2_nic *pfvf, struct otx2_cq_poll *cq_poll) 539 { 540 struct dim_sample dim_sample = { 0 }; 541 u64 rx_frames, rx_bytes; 542 u64 tx_frames, tx_bytes; 543 544 rx_frames = OTX2_GET_RX_STATS(RX_BCAST) + OTX2_GET_RX_STATS(RX_MCAST) + 545 OTX2_GET_RX_STATS(RX_UCAST); 546 rx_bytes = OTX2_GET_RX_STATS(RX_OCTS); 547 tx_bytes = OTX2_GET_TX_STATS(TX_OCTS); 548 tx_frames = OTX2_GET_TX_STATS(TX_UCAST); 549 550 dim_update_sample(pfvf->napi_events, 551 rx_frames + tx_frames, 552 rx_bytes + tx_bytes, 553 &dim_sample); 554 net_dim(&cq_poll->dim, &dim_sample); 555 } 556 557 int otx2_napi_handler(struct napi_struct *napi, int budget) 558 { 559 struct otx2_cq_queue *rx_cq = NULL; 560 struct otx2_cq_queue *cq = NULL; 561 struct otx2_pool *pool = NULL; 562 struct otx2_cq_poll *cq_poll; 563 int workdone = 0, cq_idx, i; 564 struct otx2_qset *qset; 565 struct otx2_nic *pfvf; 566 int filled_cnt = -1; 567 568 cq_poll = container_of(napi, struct otx2_cq_poll, napi); 569 pfvf = (struct otx2_nic *)cq_poll->dev; 570 qset = &pfvf->qset; 571 572 for (i = 0; i < CQS_PER_CINT; i++) { 573 cq_idx = cq_poll->cq_ids[i]; 574 if (unlikely(cq_idx == CINT_INVALID_CQ)) 575 continue; 576 cq = &qset->cq[cq_idx]; 577 if (cq->cq_type == CQ_RX) { 578 rx_cq = cq; 579 workdone += otx2_rx_napi_handler(pfvf, napi, 580 cq, budget); 581 } else { 582 workdone += otx2_tx_napi_handler(pfvf, cq, budget); 583 } 584 } 585 586 if (rx_cq && rx_cq->pool_ptrs) 587 filled_cnt = pfvf->hw_ops->refill_pool_ptrs(pfvf, rx_cq); 588 589 /* Clear the IRQ */ 590 otx2_write64(pfvf, NIX_LF_CINTX_INT(cq_poll->cint_idx), BIT_ULL(0)); 591 592 if (workdone < budget && napi_complete_done(napi, workdone)) { 593 /* If interface is going down, don't re-enable IRQ */ 594 if (pfvf->flags & OTX2_FLAG_INTF_DOWN) 595 return workdone; 596 597 /* Adjust irq coalese using net_dim */ 598 if (pfvf->flags & OTX2_FLAG_ADPTV_INT_COAL_ENABLED) 599 otx2_adjust_adaptive_coalese(pfvf, cq_poll); 600 601 if (likely(cq)) 602 pool = &pfvf->qset.pool[cq->cq_idx]; 603 604 if (unlikely(!filled_cnt)) { 605 struct refill_work *work; 606 struct delayed_work *dwork; 607 608 if (likely(cq)) { 609 work = &pfvf->refill_wrk[cq->cq_idx]; 610 dwork = &work->pool_refill_work; 611 /* Schedule a task if no other task is running */ 612 if (!cq->refill_task_sched) { 613 work->napi = napi; 614 cq->refill_task_sched = true; 615 schedule_delayed_work(dwork, 616 msecs_to_jiffies(100)); 617 } 618 /* Call wake-up for not able to fill buffers */ 619 if (pool->xsk_pool) 620 xsk_set_rx_need_wakeup(pool->xsk_pool); 621 } 622 } else { 623 /* Clear wake-up, since buffers are filled successfully */ 624 if (pool && pool->xsk_pool) 625 xsk_clear_rx_need_wakeup(pool->xsk_pool); 626 /* Re-enable interrupts */ 627 otx2_write64(pfvf, 628 NIX_LF_CINTX_ENA_W1S(cq_poll->cint_idx), 629 BIT_ULL(0)); 630 } 631 } 632 return workdone; 633 } 634 EXPORT_SYMBOL(otx2_napi_handler); 635 636 void otx2_sqe_flush(void *dev, struct otx2_snd_queue *sq, 637 int size, int qidx) 638 { 639 u64 status; 640 641 /* Packet data stores should finish before SQE is flushed to HW */ 642 dma_wmb(); 643 644 do { 645 memcpy(sq->lmt_addr, sq->sqe_base, size); 646 status = otx2_lmt_flush(sq->io_addr); 647 } while (status == 0); 648 649 sq->head++; 650 sq->head &= (sq->sqe_cnt - 1); 651 } 652 653 /* Add SQE scatter/gather subdescriptor structure */ 654 static bool otx2_sqe_add_sg(struct otx2_nic *pfvf, struct otx2_snd_queue *sq, 655 struct sk_buff *skb, int num_segs, int *offset) 656 { 657 struct nix_sqe_sg_s *sg = NULL; 658 u64 dma_addr, *iova = NULL; 659 u16 *sg_lens = NULL; 660 int seg, len; 661 662 sq->sg[sq->head].num_segs = 0; 663 664 for (seg = 0; seg < num_segs; seg++) { 665 if ((seg % MAX_SEGS_PER_SG) == 0) { 666 sg = (struct nix_sqe_sg_s *)(sq->sqe_base + *offset); 667 sg->ld_type = NIX_SEND_LDTYPE_LDD; 668 sg->subdc = NIX_SUBDC_SG; 669 sg->segs = 0; 670 sg_lens = (void *)sg; 671 iova = (void *)sg + sizeof(*sg); 672 /* Next subdc always starts at a 16byte boundary. 673 * So if sg->segs is whether 2 or 3, offset += 16bytes. 674 */ 675 if ((num_segs - seg) >= (MAX_SEGS_PER_SG - 1)) 676 *offset += sizeof(*sg) + (3 * sizeof(u64)); 677 else 678 *offset += sizeof(*sg) + sizeof(u64); 679 } 680 dma_addr = otx2_dma_map_skb_frag(pfvf, skb, seg, &len); 681 if (dma_mapping_error(pfvf->dev, dma_addr)) 682 return false; 683 684 sg_lens[frag_num(seg % MAX_SEGS_PER_SG)] = len; 685 sg->segs++; 686 *iova++ = dma_addr; 687 688 /* Save DMA mapping info for later unmapping */ 689 sq->sg[sq->head].dma_addr[seg] = dma_addr; 690 sq->sg[sq->head].size[seg] = len; 691 sq->sg[sq->head].num_segs++; 692 } 693 694 sq->sg[sq->head].skb = (u64)skb; 695 return true; 696 } 697 698 /* Add SQE extended header subdescriptor */ 699 static void otx2_sqe_add_ext(struct otx2_nic *pfvf, struct otx2_snd_queue *sq, 700 struct sk_buff *skb, int *offset) 701 { 702 struct nix_sqe_ext_s *ext; 703 704 ext = (struct nix_sqe_ext_s *)(sq->sqe_base + *offset); 705 ext->subdc = NIX_SUBDC_EXT; 706 if (skb_shinfo(skb)->gso_size) { 707 ext->lso = 1; 708 ext->lso_sb = skb_tcp_all_headers(skb); 709 ext->lso_mps = skb_shinfo(skb)->gso_size; 710 711 /* Only TSOv4 and TSOv6 GSO offloads are supported */ 712 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { 713 ext->lso_format = pfvf->hw.lso_tsov4_idx; 714 715 /* HW adds payload size to 'ip_hdr->tot_len' while 716 * sending TSO segment, hence set payload length 717 * in IP header of the packet to just header length. 718 */ 719 ip_hdr(skb)->tot_len = 720 htons(ext->lso_sb - skb_network_offset(skb)); 721 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) { 722 ext->lso_format = pfvf->hw.lso_tsov6_idx; 723 ipv6_hdr(skb)->payload_len = htons(tcp_hdrlen(skb)); 724 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 725 __be16 l3_proto = vlan_get_protocol(skb); 726 struct udphdr *udph = udp_hdr(skb); 727 __be16 iplen; 728 729 ext->lso_sb = skb_transport_offset(skb) + 730 sizeof(struct udphdr); 731 732 /* HW adds payload size to length fields in IP and 733 * UDP headers while segmentation, hence adjust the 734 * lengths to just header sizes. 735 */ 736 iplen = htons(ext->lso_sb - skb_network_offset(skb)); 737 if (l3_proto == htons(ETH_P_IP)) { 738 ip_hdr(skb)->tot_len = iplen; 739 ext->lso_format = pfvf->hw.lso_udpv4_idx; 740 } else { 741 ipv6_hdr(skb)->payload_len = iplen; 742 ext->lso_format = pfvf->hw.lso_udpv6_idx; 743 } 744 745 udph->len = htons(sizeof(struct udphdr)); 746 } 747 } else if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) { 748 ext->tstmp = 1; 749 } 750 751 #define OTX2_VLAN_PTR_OFFSET (ETH_HLEN - ETH_TLEN) 752 if (skb_vlan_tag_present(skb)) { 753 if (skb->vlan_proto == htons(ETH_P_8021Q)) { 754 ext->vlan1_ins_ena = 1; 755 ext->vlan1_ins_ptr = OTX2_VLAN_PTR_OFFSET; 756 ext->vlan1_ins_tci = skb_vlan_tag_get(skb); 757 } else if (skb->vlan_proto == htons(ETH_P_8021AD)) { 758 ext->vlan0_ins_ena = 1; 759 ext->vlan0_ins_ptr = OTX2_VLAN_PTR_OFFSET; 760 ext->vlan0_ins_tci = skb_vlan_tag_get(skb); 761 } 762 } 763 764 *offset += sizeof(*ext); 765 } 766 767 static void otx2_sqe_add_mem(struct otx2_snd_queue *sq, int *offset, 768 int alg, u64 iova, int ptp_offset, 769 u64 base_ns, bool udp_csum_crt) 770 { 771 struct nix_sqe_mem_s *mem; 772 773 mem = (struct nix_sqe_mem_s *)(sq->sqe_base + *offset); 774 mem->subdc = NIX_SUBDC_MEM; 775 mem->alg = alg; 776 mem->wmem = 1; /* wait for the memory operation */ 777 mem->addr = iova; 778 779 if (ptp_offset) { 780 mem->start_offset = ptp_offset; 781 mem->udp_csum_crt = !!udp_csum_crt; 782 mem->base_ns = base_ns; 783 mem->step_type = 1; 784 } 785 786 *offset += sizeof(*mem); 787 } 788 789 /* Add SQE header subdescriptor structure */ 790 static void otx2_sqe_add_hdr(struct otx2_nic *pfvf, struct otx2_snd_queue *sq, 791 struct nix_sqe_hdr_s *sqe_hdr, 792 struct sk_buff *skb, u16 qidx) 793 { 794 int proto = 0; 795 796 /* Check if SQE was framed before, if yes then no need to 797 * set these constants again and again. 798 */ 799 if (!sqe_hdr->total) { 800 /* Don't free Tx buffers to Aura */ 801 sqe_hdr->df = 1; 802 sqe_hdr->aura = sq->aura_id; 803 /* Post a CQE Tx after pkt transmission */ 804 sqe_hdr->pnc = 1; 805 sqe_hdr->sq = (qidx >= pfvf->hw.tx_queues) ? 806 qidx + pfvf->hw.xdp_queues : qidx; 807 } 808 sqe_hdr->total = skb->len; 809 /* Set SQE identifier which will be used later for freeing SKB */ 810 sqe_hdr->sqe_id = sq->head; 811 812 /* Offload TCP/UDP checksum to HW */ 813 if (skb->ip_summed == CHECKSUM_PARTIAL) { 814 sqe_hdr->ol3ptr = skb_network_offset(skb); 815 sqe_hdr->ol4ptr = skb_transport_offset(skb); 816 /* get vlan protocol Ethertype */ 817 if (eth_type_vlan(skb->protocol)) 818 skb->protocol = vlan_get_protocol(skb); 819 820 if (skb->protocol == htons(ETH_P_IP)) { 821 proto = ip_hdr(skb)->protocol; 822 /* In case of TSO, HW needs this to be explicitly set. 823 * So set this always, instead of adding a check. 824 */ 825 sqe_hdr->ol3type = NIX_SENDL3TYPE_IP4_CKSUM; 826 } else if (skb->protocol == htons(ETH_P_IPV6)) { 827 proto = ipv6_hdr(skb)->nexthdr; 828 sqe_hdr->ol3type = NIX_SENDL3TYPE_IP6; 829 } 830 831 if (proto == IPPROTO_TCP) 832 sqe_hdr->ol4type = NIX_SENDL4TYPE_TCP_CKSUM; 833 else if (proto == IPPROTO_UDP) 834 sqe_hdr->ol4type = NIX_SENDL4TYPE_UDP_CKSUM; 835 } 836 } 837 838 static int otx2_dma_map_tso_skb(struct otx2_nic *pfvf, 839 struct otx2_snd_queue *sq, 840 struct sk_buff *skb, int sqe, int hdr_len) 841 { 842 int num_segs = skb_shinfo(skb)->nr_frags + 1; 843 struct sg_list *sg = &sq->sg[sqe]; 844 u64 dma_addr; 845 int seg, len; 846 847 sg->num_segs = 0; 848 849 /* Get payload length at skb->data */ 850 len = skb_headlen(skb) - hdr_len; 851 852 for (seg = 0; seg < num_segs; seg++) { 853 /* Skip skb->data, if there is no payload */ 854 if (!seg && !len) 855 continue; 856 dma_addr = otx2_dma_map_skb_frag(pfvf, skb, seg, &len); 857 if (dma_mapping_error(pfvf->dev, dma_addr)) 858 goto unmap; 859 860 /* Save DMA mapping info for later unmapping */ 861 sg->dma_addr[sg->num_segs] = dma_addr; 862 sg->size[sg->num_segs] = len; 863 sg->num_segs++; 864 } 865 return 0; 866 unmap: 867 otx2_dma_unmap_skb_frags(pfvf, sg); 868 return -EINVAL; 869 } 870 871 static u64 otx2_tso_frag_dma_addr(struct otx2_snd_queue *sq, 872 struct sk_buff *skb, int seg, 873 u64 seg_addr, int hdr_len, int sqe) 874 { 875 struct sg_list *sg = &sq->sg[sqe]; 876 const skb_frag_t *frag; 877 int offset; 878 879 if (seg < 0) 880 return sg->dma_addr[0] + (seg_addr - (u64)skb->data); 881 882 frag = &skb_shinfo(skb)->frags[seg]; 883 offset = seg_addr - (u64)skb_frag_address(frag); 884 if (skb_headlen(skb) - hdr_len) 885 seg++; 886 return sg->dma_addr[seg] + offset; 887 } 888 889 static void otx2_sqe_tso_add_sg(struct otx2_snd_queue *sq, 890 struct sg_list *list, int *offset) 891 { 892 struct nix_sqe_sg_s *sg = NULL; 893 u16 *sg_lens = NULL; 894 u64 *iova = NULL; 895 int seg; 896 897 /* Add SG descriptors with buffer addresses */ 898 for (seg = 0; seg < list->num_segs; seg++) { 899 if ((seg % MAX_SEGS_PER_SG) == 0) { 900 sg = (struct nix_sqe_sg_s *)(sq->sqe_base + *offset); 901 sg->ld_type = NIX_SEND_LDTYPE_LDD; 902 sg->subdc = NIX_SUBDC_SG; 903 sg->segs = 0; 904 sg_lens = (void *)sg; 905 iova = (void *)sg + sizeof(*sg); 906 /* Next subdc always starts at a 16byte boundary. 907 * So if sg->segs is whether 2 or 3, offset += 16bytes. 908 */ 909 if ((list->num_segs - seg) >= (MAX_SEGS_PER_SG - 1)) 910 *offset += sizeof(*sg) + (3 * sizeof(u64)); 911 else 912 *offset += sizeof(*sg) + sizeof(u64); 913 } 914 sg_lens[frag_num(seg % MAX_SEGS_PER_SG)] = list->size[seg]; 915 *iova++ = list->dma_addr[seg]; 916 sg->segs++; 917 } 918 } 919 920 static void otx2_sq_append_tso(struct otx2_nic *pfvf, struct otx2_snd_queue *sq, 921 struct sk_buff *skb, u16 qidx) 922 { 923 struct netdev_queue *txq = netdev_get_tx_queue(pfvf->netdev, qidx); 924 int hdr_len, tcp_data, seg_len, pkt_len, offset; 925 struct nix_sqe_hdr_s *sqe_hdr; 926 int first_sqe = sq->head; 927 struct sg_list list; 928 struct tso_t tso; 929 930 hdr_len = tso_start(skb, &tso); 931 932 /* Map SKB's fragments to DMA. 933 * It's done here to avoid mapping for every TSO segment's packet. 934 */ 935 if (otx2_dma_map_tso_skb(pfvf, sq, skb, first_sqe, hdr_len)) { 936 dev_kfree_skb_any(skb); 937 return; 938 } 939 940 netdev_tx_sent_queue(txq, skb->len); 941 942 tcp_data = skb->len - hdr_len; 943 while (tcp_data > 0) { 944 char *hdr; 945 946 seg_len = min_t(int, skb_shinfo(skb)->gso_size, tcp_data); 947 tcp_data -= seg_len; 948 949 /* Set SQE's SEND_HDR */ 950 memset(sq->sqe_base, 0, sq->sqe_size); 951 sqe_hdr = (struct nix_sqe_hdr_s *)(sq->sqe_base); 952 otx2_sqe_add_hdr(pfvf, sq, sqe_hdr, skb, qidx); 953 offset = sizeof(*sqe_hdr); 954 955 /* Add TSO segment's pkt header */ 956 hdr = sq->tso_hdrs->base + (sq->head * TSO_HEADER_SIZE); 957 tso_build_hdr(skb, hdr, &tso, seg_len, tcp_data == 0); 958 list.dma_addr[0] = 959 sq->tso_hdrs->iova + (sq->head * TSO_HEADER_SIZE); 960 list.size[0] = hdr_len; 961 list.num_segs = 1; 962 963 /* Add TSO segment's payload data fragments */ 964 pkt_len = hdr_len; 965 while (seg_len > 0) { 966 int size; 967 968 size = min_t(int, tso.size, seg_len); 969 970 list.size[list.num_segs] = size; 971 list.dma_addr[list.num_segs] = 972 otx2_tso_frag_dma_addr(sq, skb, 973 tso.next_frag_idx - 1, 974 (u64)tso.data, hdr_len, 975 first_sqe); 976 list.num_segs++; 977 pkt_len += size; 978 seg_len -= size; 979 tso_build_data(skb, &tso, size); 980 } 981 sqe_hdr->total = pkt_len; 982 otx2_sqe_tso_add_sg(sq, &list, &offset); 983 984 /* DMA mappings and skb needs to be freed only after last 985 * TSO segment is transmitted out. So set 'PNC' only for 986 * last segment. Also point last segment's sqe_id to first 987 * segment's SQE index where skb address and DMA mappings 988 * are saved. 989 */ 990 if (!tcp_data) { 991 sqe_hdr->pnc = 1; 992 sqe_hdr->sqe_id = first_sqe; 993 sq->sg[first_sqe].skb = (u64)skb; 994 } else { 995 sqe_hdr->pnc = 0; 996 } 997 998 sqe_hdr->sizem1 = (offset / 16) - 1; 999 1000 /* Flush SQE to HW */ 1001 pfvf->hw_ops->sqe_flush(pfvf, sq, offset, qidx); 1002 } 1003 } 1004 1005 static bool is_hw_tso_supported(struct otx2_nic *pfvf, 1006 struct sk_buff *skb) 1007 { 1008 int payload_len, last_seg_size; 1009 1010 if (test_bit(HW_TSO, &pfvf->hw.cap_flag)) 1011 return true; 1012 1013 /* On 96xx A0, HW TSO not supported */ 1014 if (!is_96xx_B0(pfvf->pdev)) 1015 return false; 1016 1017 /* HW has an issue due to which when the payload of the last LSO 1018 * segment is shorter than 16 bytes, some header fields may not 1019 * be correctly modified, hence don't offload such TSO segments. 1020 */ 1021 1022 payload_len = skb->len - skb_tcp_all_headers(skb); 1023 last_seg_size = payload_len % skb_shinfo(skb)->gso_size; 1024 if (last_seg_size && last_seg_size < 16) 1025 return false; 1026 1027 return true; 1028 } 1029 1030 static int otx2_get_sqe_count(struct otx2_nic *pfvf, struct sk_buff *skb) 1031 { 1032 if (!skb_shinfo(skb)->gso_size) 1033 return 1; 1034 1035 /* HW TSO */ 1036 if (is_hw_tso_supported(pfvf, skb)) 1037 return 1; 1038 1039 /* SW TSO */ 1040 return skb_shinfo(skb)->gso_segs; 1041 } 1042 1043 static bool otx2_validate_network_transport(struct sk_buff *skb) 1044 { 1045 if ((ip_hdr(skb)->protocol == IPPROTO_UDP) || 1046 (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)) { 1047 struct udphdr *udph = udp_hdr(skb); 1048 1049 if (udph->source == htons(PTP_PORT) && 1050 udph->dest == htons(PTP_PORT)) 1051 return true; 1052 } 1053 1054 return false; 1055 } 1056 1057 static bool otx2_ptp_is_sync(struct sk_buff *skb, int *offset, bool *udp_csum_crt) 1058 { 1059 struct ethhdr *eth = (struct ethhdr *)(skb->data); 1060 u16 nix_offload_hlen = 0, inner_vhlen = 0; 1061 bool udp_hdr_present = false, is_sync; 1062 u8 *data = skb->data, *msgtype; 1063 __be16 proto = eth->h_proto; 1064 int network_depth = 0; 1065 1066 /* NIX is programmed to offload outer VLAN header 1067 * in case of single vlan protocol field holds Network header ETH_IP/V6 1068 * in case of stacked vlan protocol field holds Inner vlan (8100) 1069 */ 1070 if (skb->dev->features & NETIF_F_HW_VLAN_CTAG_TX && 1071 skb->dev->features & NETIF_F_HW_VLAN_STAG_TX) { 1072 if (skb->vlan_proto == htons(ETH_P_8021AD)) { 1073 /* Get vlan protocol */ 1074 proto = __vlan_get_protocol(skb, eth->h_proto, NULL); 1075 /* SKB APIs like skb_transport_offset does not include 1076 * offloaded vlan header length. Need to explicitly add 1077 * the length 1078 */ 1079 nix_offload_hlen = VLAN_HLEN; 1080 inner_vhlen = VLAN_HLEN; 1081 } else if (skb->vlan_proto == htons(ETH_P_8021Q)) { 1082 nix_offload_hlen = VLAN_HLEN; 1083 } 1084 } else if (eth_type_vlan(eth->h_proto)) { 1085 proto = __vlan_get_protocol(skb, eth->h_proto, &network_depth); 1086 } 1087 1088 switch (ntohs(proto)) { 1089 case ETH_P_1588: 1090 if (network_depth) 1091 *offset = network_depth; 1092 else 1093 *offset = ETH_HLEN + nix_offload_hlen + 1094 inner_vhlen; 1095 break; 1096 case ETH_P_IP: 1097 case ETH_P_IPV6: 1098 if (!otx2_validate_network_transport(skb)) 1099 return false; 1100 1101 *offset = nix_offload_hlen + skb_transport_offset(skb) + 1102 sizeof(struct udphdr); 1103 udp_hdr_present = true; 1104 1105 } 1106 1107 msgtype = data + *offset; 1108 /* Check PTP messageId is SYNC or not */ 1109 is_sync = !(*msgtype & 0xf); 1110 if (is_sync) 1111 *udp_csum_crt = udp_hdr_present; 1112 else 1113 *offset = 0; 1114 1115 return is_sync; 1116 } 1117 1118 static void otx2_set_txtstamp(struct otx2_nic *pfvf, struct sk_buff *skb, 1119 struct otx2_snd_queue *sq, int *offset) 1120 { 1121 struct ethhdr *eth = (struct ethhdr *)(skb->data); 1122 struct ptpv2_tstamp *origin_tstamp; 1123 bool udp_csum_crt = false; 1124 unsigned int udphoff; 1125 struct timespec64 ts; 1126 int ptp_offset = 0; 1127 __wsum skb_csum; 1128 u64 iova; 1129 1130 if (unlikely(!skb_shinfo(skb)->gso_size && 1131 (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))) { 1132 if (unlikely(pfvf->flags & OTX2_FLAG_PTP_ONESTEP_SYNC && 1133 otx2_ptp_is_sync(skb, &ptp_offset, &udp_csum_crt))) { 1134 origin_tstamp = (struct ptpv2_tstamp *) 1135 ((u8 *)skb->data + ptp_offset + 1136 PTP_SYNC_SEC_OFFSET); 1137 ts = ns_to_timespec64(pfvf->ptp->tstamp); 1138 origin_tstamp->seconds_msb = htons((ts.tv_sec >> 32) & 0xffff); 1139 origin_tstamp->seconds_lsb = htonl(ts.tv_sec & 0xffffffff); 1140 origin_tstamp->nanoseconds = htonl(ts.tv_nsec); 1141 /* Point to correction field in PTP packet */ 1142 ptp_offset += 8; 1143 1144 /* When user disables hw checksum, stack calculates the csum, 1145 * but it does not cover ptp timestamp which is added later. 1146 * Recalculate the checksum manually considering the timestamp. 1147 */ 1148 if (udp_csum_crt) { 1149 struct udphdr *uh = udp_hdr(skb); 1150 1151 if (skb->ip_summed != CHECKSUM_PARTIAL && uh->check != 0) { 1152 udphoff = skb_transport_offset(skb); 1153 uh->check = 0; 1154 skb_csum = skb_checksum(skb, udphoff, skb->len - udphoff, 1155 0); 1156 if (ntohs(eth->h_proto) == ETH_P_IPV6) 1157 uh->check = csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 1158 &ipv6_hdr(skb)->daddr, 1159 skb->len - udphoff, 1160 ipv6_hdr(skb)->nexthdr, 1161 skb_csum); 1162 else 1163 uh->check = csum_tcpudp_magic(ip_hdr(skb)->saddr, 1164 ip_hdr(skb)->daddr, 1165 skb->len - udphoff, 1166 IPPROTO_UDP, 1167 skb_csum); 1168 } 1169 } 1170 } else { 1171 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 1172 } 1173 iova = sq->timestamps->iova + (sq->head * sizeof(u64)); 1174 otx2_sqe_add_mem(sq, offset, NIX_SENDMEMALG_E_SETTSTMP, iova, 1175 ptp_offset, pfvf->ptp->base_ns, udp_csum_crt); 1176 } else { 1177 skb_tx_timestamp(skb); 1178 } 1179 } 1180 1181 bool otx2_sq_append_skb(void *dev, struct netdev_queue *txq, 1182 struct otx2_snd_queue *sq, 1183 struct sk_buff *skb, u16 qidx) 1184 { 1185 int offset, num_segs, free_desc; 1186 struct nix_sqe_hdr_s *sqe_hdr; 1187 struct otx2_nic *pfvf = dev; 1188 bool ret; 1189 1190 /* Check if there is enough room between producer 1191 * and consumer index. 1192 */ 1193 free_desc = otx2_get_free_sqe(sq); 1194 if (free_desc < sq->sqe_thresh) 1195 return false; 1196 1197 if (free_desc < otx2_get_sqe_count(pfvf, skb)) 1198 return false; 1199 1200 num_segs = skb_shinfo(skb)->nr_frags + 1; 1201 1202 /* If SKB doesn't fit in a single SQE, linearize it. 1203 * TODO: Consider adding JUMP descriptor instead. 1204 */ 1205 1206 if (unlikely(num_segs > OTX2_MAX_FRAGS_IN_SQE)) { 1207 if (__skb_linearize(skb)) { 1208 dev_kfree_skb_any(skb); 1209 return true; 1210 } 1211 num_segs = skb_shinfo(skb)->nr_frags + 1; 1212 } 1213 1214 if (skb_shinfo(skb)->gso_size && !is_hw_tso_supported(pfvf, skb)) { 1215 /* Insert vlan tag before giving pkt to tso */ 1216 if (skb_vlan_tag_present(skb)) { 1217 skb = __vlan_hwaccel_push_inside(skb); 1218 if (!skb) 1219 return true; 1220 } 1221 otx2_sq_append_tso(pfvf, sq, skb, qidx); 1222 return true; 1223 } 1224 1225 /* Set sqe base address */ 1226 otx2_sq_set_sqe_base(sq, skb); 1227 1228 /* Set SQE's SEND_HDR. 1229 * Do not clear the first 64bit as it contains constant info. 1230 */ 1231 memset(sq->sqe_base + 8, 0, sq->sqe_size - 8); 1232 sqe_hdr = (struct nix_sqe_hdr_s *)(sq->sqe_base); 1233 otx2_sqe_add_hdr(pfvf, sq, sqe_hdr, skb, qidx); 1234 offset = sizeof(*sqe_hdr); 1235 1236 /* Add extended header if needed */ 1237 otx2_sqe_add_ext(pfvf, sq, skb, &offset); 1238 1239 /* Add SG subdesc with data frags */ 1240 if (static_branch_unlikely(&cn10k_ipsec_sa_enabled) && 1241 (xfrm_offload(skb))) 1242 ret = otx2_sqe_add_sg_ipsec(pfvf, sq, skb, num_segs, &offset); 1243 else 1244 ret = otx2_sqe_add_sg(pfvf, sq, skb, num_segs, &offset); 1245 1246 if (!ret) { 1247 otx2_dma_unmap_skb_frags(pfvf, &sq->sg[sq->head]); 1248 return false; 1249 } 1250 1251 otx2_set_txtstamp(pfvf, skb, sq, &offset); 1252 1253 sqe_hdr->sizem1 = (offset / 16) - 1; 1254 1255 if (static_branch_unlikely(&cn10k_ipsec_sa_enabled) && 1256 (xfrm_offload(skb))) 1257 return cn10k_ipsec_transmit(pfvf, txq, sq, skb, num_segs, 1258 offset); 1259 1260 netdev_tx_sent_queue(txq, skb->len); 1261 1262 /* Flush SQE to HW */ 1263 pfvf->hw_ops->sqe_flush(pfvf, sq, offset, qidx); 1264 return true; 1265 } 1266 EXPORT_SYMBOL(otx2_sq_append_skb); 1267 1268 void otx2_cleanup_rx_cqes(struct otx2_nic *pfvf, struct otx2_cq_queue *cq, int qidx) 1269 { 1270 struct nix_cqe_rx_s *cqe; 1271 struct otx2_pool *pool; 1272 int processed_cqe = 0; 1273 u16 pool_id; 1274 u64 iova; 1275 1276 pool_id = otx2_get_pool_idx(pfvf, AURA_NIX_RQ, qidx); 1277 pool = &pfvf->qset.pool[pool_id]; 1278 1279 if (pfvf->xdp_prog) { 1280 if (pool->page_pool) 1281 xdp_rxq_info_unreg_mem_model(&cq->xdp_rxq); 1282 1283 xdp_rxq_info_unreg(&cq->xdp_rxq); 1284 } 1285 1286 if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe) 1287 return; 1288 1289 while (cq->pend_cqe) { 1290 cqe = (struct nix_cqe_rx_s *)otx2_get_next_cqe(cq); 1291 processed_cqe++; 1292 cq->pend_cqe--; 1293 1294 if (!cqe) 1295 continue; 1296 if (cqe->sg.segs > 1) { 1297 otx2_free_rcv_seg(pfvf, cqe, cq->cq_idx); 1298 continue; 1299 } 1300 iova = cqe->sg.seg_addr - OTX2_HEAD_ROOM; 1301 1302 otx2_free_bufs(pfvf, pool, iova, pfvf->rbsize); 1303 } 1304 1305 /* Free CQEs to HW */ 1306 otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR, 1307 ((u64)cq->cq_idx << 32) | processed_cqe); 1308 } 1309 1310 void otx2_cleanup_tx_cqes(struct otx2_nic *pfvf, struct otx2_cq_queue *cq) 1311 { 1312 int tx_pkts = 0, tx_bytes = 0; 1313 struct sk_buff *skb = NULL; 1314 struct otx2_snd_queue *sq; 1315 struct nix_cqe_tx_s *cqe; 1316 struct netdev_queue *txq; 1317 int processed_cqe = 0; 1318 struct sg_list *sg; 1319 int qidx; 1320 1321 qidx = cq->cq_idx - pfvf->hw.rx_queues; 1322 sq = &pfvf->qset.sq[qidx]; 1323 1324 if (otx2_nix_cq_op_status(pfvf, cq) || !cq->pend_cqe) 1325 return; 1326 1327 while (cq->pend_cqe) { 1328 cqe = (struct nix_cqe_tx_s *)otx2_get_next_cqe(cq); 1329 processed_cqe++; 1330 cq->pend_cqe--; 1331 1332 if (!cqe) 1333 continue; 1334 sg = &sq->sg[cqe->comp.sqe_id]; 1335 skb = (struct sk_buff *)sg->skb; 1336 if (skb) { 1337 tx_bytes += skb->len; 1338 tx_pkts++; 1339 otx2_dma_unmap_skb_frags(pfvf, sg); 1340 dev_kfree_skb_any(skb); 1341 sg->skb = (u64)NULL; 1342 } 1343 } 1344 1345 if (likely(tx_pkts)) { 1346 if (qidx >= pfvf->hw.tx_queues) 1347 qidx -= pfvf->hw.xdp_queues; 1348 txq = netdev_get_tx_queue(pfvf->netdev, qidx); 1349 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes); 1350 } 1351 /* Free CQEs to HW */ 1352 otx2_write64(pfvf, NIX_LF_CQ_OP_DOOR, 1353 ((u64)cq->cq_idx << 32) | processed_cqe); 1354 } 1355 1356 int otx2_rxtx_enable(struct otx2_nic *pfvf, bool enable) 1357 { 1358 struct msg_req *msg; 1359 int err; 1360 1361 mutex_lock(&pfvf->mbox.lock); 1362 if (enable) 1363 msg = otx2_mbox_alloc_msg_nix_lf_start_rx(&pfvf->mbox); 1364 else 1365 msg = otx2_mbox_alloc_msg_nix_lf_stop_rx(&pfvf->mbox); 1366 1367 if (!msg) { 1368 mutex_unlock(&pfvf->mbox.lock); 1369 return -ENOMEM; 1370 } 1371 1372 err = otx2_sync_mbox_msg(&pfvf->mbox); 1373 mutex_unlock(&pfvf->mbox.lock); 1374 return err; 1375 } 1376 1377 void otx2_free_pending_sqe(struct otx2_nic *pfvf) 1378 { 1379 int tx_pkts = 0, tx_bytes = 0; 1380 struct sk_buff *skb = NULL; 1381 struct otx2_snd_queue *sq; 1382 struct netdev_queue *txq; 1383 struct sg_list *sg; 1384 int sq_idx, sqe; 1385 1386 for (sq_idx = 0; sq_idx < pfvf->hw.tx_queues; sq_idx++) { 1387 sq = &pfvf->qset.sq[sq_idx]; 1388 for (sqe = 0; sqe < sq->sqe_cnt; sqe++) { 1389 sg = &sq->sg[sqe]; 1390 skb = (struct sk_buff *)sg->skb; 1391 if (skb) { 1392 tx_bytes += skb->len; 1393 tx_pkts++; 1394 otx2_dma_unmap_skb_frags(pfvf, sg); 1395 dev_kfree_skb_any(skb); 1396 sg->skb = (u64)NULL; 1397 } 1398 } 1399 1400 if (!tx_pkts) 1401 continue; 1402 txq = netdev_get_tx_queue(pfvf->netdev, sq_idx); 1403 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes); 1404 tx_pkts = 0; 1405 tx_bytes = 0; 1406 } 1407 } 1408 1409 static void otx2_xdp_sqe_add_sg(struct otx2_snd_queue *sq, 1410 struct xdp_frame *xdpf, 1411 u64 dma_addr, int len, int *offset, u16 flags) 1412 { 1413 struct nix_sqe_sg_s *sg = NULL; 1414 u64 *iova = NULL; 1415 1416 sg = (struct nix_sqe_sg_s *)(sq->sqe_base + *offset); 1417 sg->ld_type = NIX_SEND_LDTYPE_LDD; 1418 sg->subdc = NIX_SUBDC_SG; 1419 sg->segs = 1; 1420 sg->seg1_size = len; 1421 iova = (void *)sg + sizeof(*sg); 1422 *iova = dma_addr; 1423 *offset += sizeof(*sg) + sizeof(u64); 1424 1425 sq->sg[sq->head].dma_addr[0] = dma_addr; 1426 sq->sg[sq->head].size[0] = len; 1427 sq->sg[sq->head].num_segs = 1; 1428 sq->sg[sq->head].flags = flags; 1429 sq->sg[sq->head].skb = (u64)xdpf; 1430 } 1431 1432 int otx2_read_free_sqe(struct otx2_nic *pfvf, u16 qidx) 1433 { 1434 struct otx2_snd_queue *sq; 1435 int free_sqe; 1436 1437 sq = &pfvf->qset.sq[qidx]; 1438 free_sqe = otx2_get_free_sqe(sq); 1439 if (free_sqe < sq->sqe_thresh) { 1440 netdev_warn(pfvf->netdev, "No free sqe for Send queue%d\n", qidx); 1441 return 0; 1442 } 1443 1444 return free_sqe - sq->sqe_thresh; 1445 } 1446 1447 bool otx2_xdp_sq_append_pkt(struct otx2_nic *pfvf, struct xdp_frame *xdpf, 1448 u64 iova, int len, u16 qidx, u16 flags) 1449 { 1450 struct nix_sqe_hdr_s *sqe_hdr; 1451 struct otx2_snd_queue *sq; 1452 int offset, free_sqe; 1453 1454 sq = &pfvf->qset.sq[qidx]; 1455 free_sqe = otx2_get_free_sqe(sq); 1456 if (free_sqe < sq->sqe_thresh) 1457 return false; 1458 1459 memset(sq->sqe_base + 8, 0, sq->sqe_size - 8); 1460 1461 sqe_hdr = (struct nix_sqe_hdr_s *)(sq->sqe_base); 1462 1463 if (!sqe_hdr->total) { 1464 sqe_hdr->aura = sq->aura_id; 1465 sqe_hdr->df = 1; 1466 sqe_hdr->sq = qidx; 1467 sqe_hdr->pnc = 1; 1468 } 1469 sqe_hdr->total = len; 1470 sqe_hdr->sqe_id = sq->head; 1471 1472 offset = sizeof(*sqe_hdr); 1473 1474 otx2_xdp_sqe_add_sg(sq, xdpf, iova, len, &offset, flags); 1475 sqe_hdr->sizem1 = (offset / 16) - 1; 1476 pfvf->hw_ops->sqe_flush(pfvf, sq, offset, qidx); 1477 1478 return true; 1479 } 1480 1481 static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf, 1482 struct bpf_prog *prog, 1483 struct nix_cqe_rx_s *cqe, 1484 struct otx2_cq_queue *cq, 1485 bool *need_xdp_flush) 1486 { 1487 struct xdp_buff xdp, *xsk_buff = NULL; 1488 unsigned char *hard_start; 1489 struct otx2_pool *pool; 1490 struct xdp_frame *xdpf; 1491 int qidx = cq->cq_idx; 1492 struct page *page; 1493 u64 iova, pa; 1494 u32 act; 1495 int err; 1496 1497 pool = &pfvf->qset.pool[qidx]; 1498 1499 if (pool->xsk_pool) { 1500 xsk_buff = pool->xdp[--cq->rbpool->xdp_top]; 1501 if (!xsk_buff) 1502 return false; 1503 1504 xsk_buff->data_end = xsk_buff->data + cqe->sg.seg_size; 1505 act = bpf_prog_run_xdp(prog, xsk_buff); 1506 goto handle_xdp_verdict; 1507 } 1508 1509 iova = cqe->sg.seg_addr - OTX2_HEAD_ROOM; 1510 pa = otx2_iova_to_phys(pfvf->iommu_domain, iova); 1511 page = virt_to_page(phys_to_virt(pa)); 1512 1513 xdp_init_buff(&xdp, pfvf->rbsize, &cq->xdp_rxq); 1514 1515 hard_start = (unsigned char *)phys_to_virt(pa); 1516 xdp_prepare_buff(&xdp, hard_start, OTX2_HEAD_ROOM, 1517 cqe->sg.seg_size, false); 1518 1519 act = bpf_prog_run_xdp(prog, &xdp); 1520 1521 handle_xdp_verdict: 1522 switch (act) { 1523 case XDP_PASS: 1524 break; 1525 case XDP_TX: 1526 qidx += pfvf->hw.tx_queues; 1527 cq->pool_ptrs++; 1528 xdpf = xdp_convert_buff_to_frame(&xdp); 1529 return otx2_xdp_sq_append_pkt(pfvf, xdpf, 1530 cqe->sg.seg_addr, 1531 cqe->sg.seg_size, 1532 qidx, OTX2_XDP_TX); 1533 case XDP_REDIRECT: 1534 cq->pool_ptrs++; 1535 if (xsk_buff) { 1536 err = xdp_do_redirect(pfvf->netdev, xsk_buff, prog); 1537 if (!err) { 1538 *need_xdp_flush = true; 1539 return true; 1540 } 1541 return false; 1542 } 1543 1544 err = xdp_do_redirect(pfvf->netdev, &xdp, prog); 1545 if (!err) { 1546 *need_xdp_flush = true; 1547 return true; 1548 } 1549 1550 otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize, 1551 DMA_FROM_DEVICE); 1552 xdpf = xdp_convert_buff_to_frame(&xdp); 1553 xdp_return_frame(xdpf); 1554 break; 1555 default: 1556 bpf_warn_invalid_xdp_action(pfvf->netdev, prog, act); 1557 break; 1558 case XDP_ABORTED: 1559 if (xsk_buff) 1560 xsk_buff_free(xsk_buff); 1561 trace_xdp_exception(pfvf->netdev, prog, act); 1562 break; 1563 case XDP_DROP: 1564 cq->pool_ptrs++; 1565 if (xsk_buff) { 1566 xsk_buff_free(xsk_buff); 1567 } else if (page->pp) { 1568 page_pool_recycle_direct(pool->page_pool, page); 1569 } else { 1570 otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize, 1571 DMA_FROM_DEVICE); 1572 put_page(page); 1573 } 1574 return true; 1575 } 1576 return false; 1577 } 1578