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