1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */ 3 4 #include <linux/ip.h> 5 #include <linux/ipv6.h> 6 #include <linux/if_vlan.h> 7 #include <net/ip6_checksum.h> 8 9 #include "ionic.h" 10 #include "ionic_lif.h" 11 #include "ionic_txrx.h" 12 13 14 static bool ionic_tx_service(struct ionic_cq *cq, struct ionic_cq_info *cq_info); 15 16 static inline void ionic_txq_post(struct ionic_queue *q, bool ring_dbell, 17 ionic_desc_cb cb_func, void *cb_arg) 18 { 19 DEBUG_STATS_TXQ_POST(q, ring_dbell); 20 21 ionic_q_post(q, ring_dbell, cb_func, cb_arg); 22 } 23 24 static inline void ionic_rxq_post(struct ionic_queue *q, bool ring_dbell, 25 ionic_desc_cb cb_func, void *cb_arg) 26 { 27 ionic_q_post(q, ring_dbell, cb_func, cb_arg); 28 29 DEBUG_STATS_RX_BUFF_CNT(q); 30 } 31 32 static inline struct netdev_queue *q_to_ndq(struct ionic_queue *q) 33 { 34 return netdev_get_tx_queue(q->lif->netdev, q->index); 35 } 36 37 static void ionic_rx_buf_reset(struct ionic_buf_info *buf_info) 38 { 39 buf_info->page = NULL; 40 buf_info->page_offset = 0; 41 buf_info->dma_addr = 0; 42 } 43 44 static int ionic_rx_page_alloc(struct ionic_queue *q, 45 struct ionic_buf_info *buf_info) 46 { 47 struct net_device *netdev = q->lif->netdev; 48 struct ionic_rx_stats *stats; 49 struct device *dev; 50 51 dev = q->dev; 52 stats = q_to_rx_stats(q); 53 54 if (unlikely(!buf_info)) { 55 net_err_ratelimited("%s: %s invalid buf_info in alloc\n", 56 netdev->name, q->name); 57 return -EINVAL; 58 } 59 60 buf_info->page = alloc_pages(IONIC_PAGE_GFP_MASK, 0); 61 if (unlikely(!buf_info->page)) { 62 net_err_ratelimited("%s: %s page alloc failed\n", 63 netdev->name, q->name); 64 stats->alloc_err++; 65 return -ENOMEM; 66 } 67 buf_info->page_offset = 0; 68 69 buf_info->dma_addr = dma_map_page(dev, buf_info->page, buf_info->page_offset, 70 IONIC_PAGE_SIZE, DMA_FROM_DEVICE); 71 if (unlikely(dma_mapping_error(dev, buf_info->dma_addr))) { 72 __free_pages(buf_info->page, 0); 73 ionic_rx_buf_reset(buf_info); 74 net_err_ratelimited("%s: %s dma map failed\n", 75 netdev->name, q->name); 76 stats->dma_map_err++; 77 return -EIO; 78 } 79 80 return 0; 81 } 82 83 static void ionic_rx_page_free(struct ionic_queue *q, 84 struct ionic_buf_info *buf_info) 85 { 86 struct net_device *netdev = q->lif->netdev; 87 struct device *dev = q->dev; 88 89 if (unlikely(!buf_info)) { 90 net_err_ratelimited("%s: %s invalid buf_info in free\n", 91 netdev->name, q->name); 92 return; 93 } 94 95 if (!buf_info->page) 96 return; 97 98 dma_unmap_page(dev, buf_info->dma_addr, IONIC_PAGE_SIZE, DMA_FROM_DEVICE); 99 __free_pages(buf_info->page, 0); 100 ionic_rx_buf_reset(buf_info); 101 } 102 103 static bool ionic_rx_buf_recycle(struct ionic_queue *q, 104 struct ionic_buf_info *buf_info, u32 used) 105 { 106 u32 size; 107 108 /* don't re-use pages allocated in low-mem condition */ 109 if (page_is_pfmemalloc(buf_info->page)) 110 return false; 111 112 /* don't re-use buffers from non-local numa nodes */ 113 if (page_to_nid(buf_info->page) != numa_mem_id()) 114 return false; 115 116 size = ALIGN(used, IONIC_PAGE_SPLIT_SZ); 117 buf_info->page_offset += size; 118 if (buf_info->page_offset >= IONIC_PAGE_SIZE) 119 return false; 120 121 get_page(buf_info->page); 122 123 return true; 124 } 125 126 static struct sk_buff *ionic_rx_frags(struct ionic_queue *q, 127 struct ionic_desc_info *desc_info, 128 struct ionic_rxq_comp *comp) 129 { 130 struct net_device *netdev = q->lif->netdev; 131 struct ionic_buf_info *buf_info; 132 struct ionic_rx_stats *stats; 133 struct device *dev = q->dev; 134 struct sk_buff *skb; 135 unsigned int i; 136 u16 frag_len; 137 u16 len; 138 139 stats = q_to_rx_stats(q); 140 141 buf_info = &desc_info->bufs[0]; 142 len = le16_to_cpu(comp->len); 143 144 prefetch(buf_info->page); 145 146 skb = napi_get_frags(&q_to_qcq(q)->napi); 147 if (unlikely(!skb)) { 148 net_warn_ratelimited("%s: SKB alloc failed on %s!\n", 149 netdev->name, q->name); 150 stats->alloc_err++; 151 return NULL; 152 } 153 154 i = comp->num_sg_elems + 1; 155 do { 156 if (unlikely(!buf_info->page)) { 157 dev_kfree_skb(skb); 158 return NULL; 159 } 160 161 frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset); 162 len -= frag_len; 163 164 dma_sync_single_for_cpu(dev, 165 buf_info->dma_addr + buf_info->page_offset, 166 frag_len, DMA_FROM_DEVICE); 167 168 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 169 buf_info->page, buf_info->page_offset, frag_len, 170 IONIC_PAGE_SIZE); 171 172 if (!ionic_rx_buf_recycle(q, buf_info, frag_len)) { 173 dma_unmap_page(dev, buf_info->dma_addr, 174 IONIC_PAGE_SIZE, DMA_FROM_DEVICE); 175 ionic_rx_buf_reset(buf_info); 176 } 177 178 buf_info++; 179 180 i--; 181 } while (i > 0); 182 183 return skb; 184 } 185 186 static struct sk_buff *ionic_rx_copybreak(struct ionic_queue *q, 187 struct ionic_desc_info *desc_info, 188 struct ionic_rxq_comp *comp) 189 { 190 struct net_device *netdev = q->lif->netdev; 191 struct ionic_buf_info *buf_info; 192 struct ionic_rx_stats *stats; 193 struct device *dev = q->dev; 194 struct sk_buff *skb; 195 u16 len; 196 197 stats = q_to_rx_stats(q); 198 199 buf_info = &desc_info->bufs[0]; 200 len = le16_to_cpu(comp->len); 201 202 skb = napi_alloc_skb(&q_to_qcq(q)->napi, len); 203 if (unlikely(!skb)) { 204 net_warn_ratelimited("%s: SKB alloc failed on %s!\n", 205 netdev->name, q->name); 206 stats->alloc_err++; 207 return NULL; 208 } 209 210 if (unlikely(!buf_info->page)) { 211 dev_kfree_skb(skb); 212 return NULL; 213 } 214 215 dma_sync_single_for_cpu(dev, buf_info->dma_addr + buf_info->page_offset, 216 len, DMA_FROM_DEVICE); 217 skb_copy_to_linear_data(skb, page_address(buf_info->page) + buf_info->page_offset, len); 218 dma_sync_single_for_device(dev, buf_info->dma_addr + buf_info->page_offset, 219 len, DMA_FROM_DEVICE); 220 221 skb_put(skb, len); 222 skb->protocol = eth_type_trans(skb, q->lif->netdev); 223 224 return skb; 225 } 226 227 static void ionic_rx_clean(struct ionic_queue *q, 228 struct ionic_desc_info *desc_info, 229 struct ionic_cq_info *cq_info, 230 void *cb_arg) 231 { 232 struct ionic_rxq_comp *comp = cq_info->rxcq; 233 struct net_device *netdev = q->lif->netdev; 234 struct ionic_qcq *qcq = q_to_qcq(q); 235 struct ionic_rx_stats *stats; 236 struct sk_buff *skb; 237 238 stats = q_to_rx_stats(q); 239 240 if (comp->status) { 241 stats->dropped++; 242 return; 243 } 244 245 stats->pkts++; 246 stats->bytes += le16_to_cpu(comp->len); 247 248 if (le16_to_cpu(comp->len) <= q->lif->rx_copybreak) 249 skb = ionic_rx_copybreak(q, desc_info, comp); 250 else 251 skb = ionic_rx_frags(q, desc_info, comp); 252 253 if (unlikely(!skb)) { 254 stats->dropped++; 255 return; 256 } 257 258 skb_record_rx_queue(skb, q->index); 259 260 if (likely(netdev->features & NETIF_F_RXHASH)) { 261 switch (comp->pkt_type_color & IONIC_RXQ_COMP_PKT_TYPE_MASK) { 262 case IONIC_PKT_TYPE_IPV4: 263 case IONIC_PKT_TYPE_IPV6: 264 skb_set_hash(skb, le32_to_cpu(comp->rss_hash), 265 PKT_HASH_TYPE_L3); 266 break; 267 case IONIC_PKT_TYPE_IPV4_TCP: 268 case IONIC_PKT_TYPE_IPV6_TCP: 269 case IONIC_PKT_TYPE_IPV4_UDP: 270 case IONIC_PKT_TYPE_IPV6_UDP: 271 skb_set_hash(skb, le32_to_cpu(comp->rss_hash), 272 PKT_HASH_TYPE_L4); 273 break; 274 } 275 } 276 277 if (likely(netdev->features & NETIF_F_RXCSUM)) { 278 if (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_CALC) { 279 skb->ip_summed = CHECKSUM_COMPLETE; 280 skb->csum = (__force __wsum)le16_to_cpu(comp->csum); 281 stats->csum_complete++; 282 } 283 } else { 284 stats->csum_none++; 285 } 286 287 if (unlikely((comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_TCP_BAD) || 288 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_UDP_BAD) || 289 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_IP_BAD))) 290 stats->csum_error++; 291 292 if (likely(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) && 293 (comp->csum_flags & IONIC_RXQ_COMP_CSUM_F_VLAN)) { 294 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 295 le16_to_cpu(comp->vlan_tci)); 296 stats->vlan_stripped++; 297 } 298 299 if (le16_to_cpu(comp->len) <= q->lif->rx_copybreak) 300 napi_gro_receive(&qcq->napi, skb); 301 else 302 napi_gro_frags(&qcq->napi); 303 } 304 305 static bool ionic_rx_service(struct ionic_cq *cq, struct ionic_cq_info *cq_info) 306 { 307 struct ionic_rxq_comp *comp = cq_info->rxcq; 308 struct ionic_queue *q = cq->bound_q; 309 struct ionic_desc_info *desc_info; 310 311 if (!color_match(comp->pkt_type_color, cq->done_color)) 312 return false; 313 314 /* check for empty queue */ 315 if (q->tail_idx == q->head_idx) 316 return false; 317 318 if (q->tail_idx != le16_to_cpu(comp->comp_index)) 319 return false; 320 321 desc_info = &q->info[q->tail_idx]; 322 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1); 323 324 /* clean the related q entry, only one per qc completion */ 325 ionic_rx_clean(q, desc_info, cq_info, desc_info->cb_arg); 326 327 desc_info->cb = NULL; 328 desc_info->cb_arg = NULL; 329 330 return true; 331 } 332 333 void ionic_rx_fill(struct ionic_queue *q) 334 { 335 struct net_device *netdev = q->lif->netdev; 336 struct ionic_desc_info *desc_info; 337 struct ionic_rxq_sg_desc *sg_desc; 338 struct ionic_rxq_sg_elem *sg_elem; 339 struct ionic_buf_info *buf_info; 340 struct ionic_rxq_desc *desc; 341 unsigned int remain_len; 342 unsigned int frag_len; 343 unsigned int nfrags; 344 unsigned int i, j; 345 unsigned int len; 346 347 len = netdev->mtu + ETH_HLEN + VLAN_HLEN; 348 349 for (i = ionic_q_space_avail(q); i; i--) { 350 nfrags = 0; 351 remain_len = len; 352 desc_info = &q->info[q->head_idx]; 353 desc = desc_info->desc; 354 buf_info = &desc_info->bufs[0]; 355 356 if (!buf_info->page) { /* alloc a new buffer? */ 357 if (unlikely(ionic_rx_page_alloc(q, buf_info))) { 358 desc->addr = 0; 359 desc->len = 0; 360 return; 361 } 362 } 363 364 /* fill main descriptor - buf[0] */ 365 desc->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset); 366 frag_len = min_t(u16, len, IONIC_PAGE_SIZE - buf_info->page_offset); 367 desc->len = cpu_to_le16(frag_len); 368 remain_len -= frag_len; 369 buf_info++; 370 nfrags++; 371 372 /* fill sg descriptors - buf[1..n] */ 373 sg_desc = desc_info->sg_desc; 374 for (j = 0; remain_len > 0 && j < q->max_sg_elems; j++) { 375 sg_elem = &sg_desc->elems[j]; 376 if (!buf_info->page) { /* alloc a new sg buffer? */ 377 if (unlikely(ionic_rx_page_alloc(q, buf_info))) { 378 sg_elem->addr = 0; 379 sg_elem->len = 0; 380 return; 381 } 382 } 383 384 sg_elem->addr = cpu_to_le64(buf_info->dma_addr + buf_info->page_offset); 385 frag_len = min_t(u16, remain_len, IONIC_PAGE_SIZE - buf_info->page_offset); 386 sg_elem->len = cpu_to_le16(frag_len); 387 remain_len -= frag_len; 388 buf_info++; 389 nfrags++; 390 } 391 392 /* clear end sg element as a sentinel */ 393 if (j < q->max_sg_elems) { 394 sg_elem = &sg_desc->elems[j]; 395 memset(sg_elem, 0, sizeof(*sg_elem)); 396 } 397 398 desc->opcode = (nfrags > 1) ? IONIC_RXQ_DESC_OPCODE_SG : 399 IONIC_RXQ_DESC_OPCODE_SIMPLE; 400 desc_info->nbufs = nfrags; 401 402 ionic_rxq_post(q, false, ionic_rx_clean, NULL); 403 } 404 405 ionic_dbell_ring(q->lif->kern_dbpage, q->hw_type, 406 q->dbval | q->head_idx); 407 } 408 409 void ionic_rx_empty(struct ionic_queue *q) 410 { 411 struct ionic_desc_info *desc_info; 412 struct ionic_buf_info *buf_info; 413 unsigned int i, j; 414 415 for (i = 0; i < q->num_descs; i++) { 416 desc_info = &q->info[i]; 417 for (j = 0; j < IONIC_RX_MAX_SG_ELEMS + 1; j++) { 418 buf_info = &desc_info->bufs[j]; 419 if (buf_info->page) 420 ionic_rx_page_free(q, buf_info); 421 } 422 423 desc_info->nbufs = 0; 424 desc_info->cb = NULL; 425 desc_info->cb_arg = NULL; 426 } 427 428 q->head_idx = 0; 429 q->tail_idx = 0; 430 } 431 432 static void ionic_dim_update(struct ionic_qcq *qcq) 433 { 434 struct dim_sample dim_sample; 435 struct ionic_lif *lif; 436 unsigned int qi; 437 438 if (!qcq->intr.dim_coal_hw) 439 return; 440 441 lif = qcq->q.lif; 442 qi = qcq->cq.bound_q->index; 443 444 ionic_intr_coal_init(lif->ionic->idev.intr_ctrl, 445 lif->rxqcqs[qi]->intr.index, 446 qcq->intr.dim_coal_hw); 447 448 dim_update_sample(qcq->cq.bound_intr->rearm_count, 449 lif->txqstats[qi].pkts, 450 lif->txqstats[qi].bytes, 451 &dim_sample); 452 453 net_dim(&qcq->dim, dim_sample); 454 } 455 456 int ionic_tx_napi(struct napi_struct *napi, int budget) 457 { 458 struct ionic_qcq *qcq = napi_to_qcq(napi); 459 struct ionic_cq *cq = napi_to_cq(napi); 460 struct ionic_dev *idev; 461 struct ionic_lif *lif; 462 u32 work_done = 0; 463 u32 flags = 0; 464 465 lif = cq->bound_q->lif; 466 idev = &lif->ionic->idev; 467 468 work_done = ionic_cq_service(cq, budget, 469 ionic_tx_service, NULL, NULL); 470 471 if (work_done < budget && napi_complete_done(napi, work_done)) { 472 ionic_dim_update(qcq); 473 flags |= IONIC_INTR_CRED_UNMASK; 474 cq->bound_intr->rearm_count++; 475 } 476 477 if (work_done || flags) { 478 flags |= IONIC_INTR_CRED_RESET_COALESCE; 479 ionic_intr_credits(idev->intr_ctrl, 480 cq->bound_intr->index, 481 work_done, flags); 482 } 483 484 DEBUG_STATS_NAPI_POLL(qcq, work_done); 485 486 return work_done; 487 } 488 489 int ionic_rx_napi(struct napi_struct *napi, int budget) 490 { 491 struct ionic_qcq *qcq = napi_to_qcq(napi); 492 struct ionic_cq *cq = napi_to_cq(napi); 493 struct ionic_dev *idev; 494 struct ionic_lif *lif; 495 u16 rx_fill_threshold; 496 u32 work_done = 0; 497 u32 flags = 0; 498 499 lif = cq->bound_q->lif; 500 idev = &lif->ionic->idev; 501 502 work_done = ionic_cq_service(cq, budget, 503 ionic_rx_service, NULL, NULL); 504 505 rx_fill_threshold = min_t(u16, IONIC_RX_FILL_THRESHOLD, 506 cq->num_descs / IONIC_RX_FILL_DIV); 507 if (work_done && ionic_q_space_avail(cq->bound_q) >= rx_fill_threshold) 508 ionic_rx_fill(cq->bound_q); 509 510 if (work_done < budget && napi_complete_done(napi, work_done)) { 511 ionic_dim_update(qcq); 512 flags |= IONIC_INTR_CRED_UNMASK; 513 cq->bound_intr->rearm_count++; 514 } 515 516 if (work_done || flags) { 517 flags |= IONIC_INTR_CRED_RESET_COALESCE; 518 ionic_intr_credits(idev->intr_ctrl, 519 cq->bound_intr->index, 520 work_done, flags); 521 } 522 523 DEBUG_STATS_NAPI_POLL(qcq, work_done); 524 525 return work_done; 526 } 527 528 int ionic_txrx_napi(struct napi_struct *napi, int budget) 529 { 530 struct ionic_qcq *qcq = napi_to_qcq(napi); 531 struct ionic_cq *rxcq = napi_to_cq(napi); 532 unsigned int qi = rxcq->bound_q->index; 533 struct ionic_dev *idev; 534 struct ionic_lif *lif; 535 struct ionic_cq *txcq; 536 u16 rx_fill_threshold; 537 u32 rx_work_done = 0; 538 u32 tx_work_done = 0; 539 u32 flags = 0; 540 541 lif = rxcq->bound_q->lif; 542 idev = &lif->ionic->idev; 543 txcq = &lif->txqcqs[qi]->cq; 544 545 tx_work_done = ionic_cq_service(txcq, IONIC_TX_BUDGET_DEFAULT, 546 ionic_tx_service, NULL, NULL); 547 548 rx_work_done = ionic_cq_service(rxcq, budget, 549 ionic_rx_service, NULL, NULL); 550 551 rx_fill_threshold = min_t(u16, IONIC_RX_FILL_THRESHOLD, 552 rxcq->num_descs / IONIC_RX_FILL_DIV); 553 if (rx_work_done && ionic_q_space_avail(rxcq->bound_q) >= rx_fill_threshold) 554 ionic_rx_fill(rxcq->bound_q); 555 556 if (rx_work_done < budget && napi_complete_done(napi, rx_work_done)) { 557 ionic_dim_update(qcq); 558 flags |= IONIC_INTR_CRED_UNMASK; 559 rxcq->bound_intr->rearm_count++; 560 } 561 562 if (rx_work_done || flags) { 563 flags |= IONIC_INTR_CRED_RESET_COALESCE; 564 ionic_intr_credits(idev->intr_ctrl, rxcq->bound_intr->index, 565 tx_work_done + rx_work_done, flags); 566 } 567 568 DEBUG_STATS_NAPI_POLL(qcq, rx_work_done); 569 DEBUG_STATS_NAPI_POLL(qcq, tx_work_done); 570 571 return rx_work_done; 572 } 573 574 static dma_addr_t ionic_tx_map_single(struct ionic_queue *q, 575 void *data, size_t len) 576 { 577 struct ionic_tx_stats *stats = q_to_tx_stats(q); 578 struct device *dev = q->dev; 579 dma_addr_t dma_addr; 580 581 dma_addr = dma_map_single(dev, data, len, DMA_TO_DEVICE); 582 if (dma_mapping_error(dev, dma_addr)) { 583 net_warn_ratelimited("%s: DMA single map failed on %s!\n", 584 q->lif->netdev->name, q->name); 585 stats->dma_map_err++; 586 return 0; 587 } 588 return dma_addr; 589 } 590 591 static dma_addr_t ionic_tx_map_frag(struct ionic_queue *q, 592 const skb_frag_t *frag, 593 size_t offset, size_t len) 594 { 595 struct ionic_tx_stats *stats = q_to_tx_stats(q); 596 struct device *dev = q->dev; 597 dma_addr_t dma_addr; 598 599 dma_addr = skb_frag_dma_map(dev, frag, offset, len, DMA_TO_DEVICE); 600 if (dma_mapping_error(dev, dma_addr)) { 601 net_warn_ratelimited("%s: DMA frag map failed on %s!\n", 602 q->lif->netdev->name, q->name); 603 stats->dma_map_err++; 604 } 605 return dma_addr; 606 } 607 608 static void ionic_tx_clean(struct ionic_queue *q, 609 struct ionic_desc_info *desc_info, 610 struct ionic_cq_info *cq_info, 611 void *cb_arg) 612 { 613 struct ionic_txq_sg_desc *sg_desc = desc_info->sg_desc; 614 struct ionic_txq_sg_elem *elem = sg_desc->elems; 615 struct ionic_tx_stats *stats = q_to_tx_stats(q); 616 struct ionic_txq_desc *desc = desc_info->desc; 617 struct device *dev = q->dev; 618 u8 opcode, flags, nsge; 619 u16 queue_index; 620 unsigned int i; 621 u64 addr; 622 623 decode_txq_desc_cmd(le64_to_cpu(desc->cmd), 624 &opcode, &flags, &nsge, &addr); 625 626 /* use unmap_single only if either this is not TSO, 627 * or this is first descriptor of a TSO 628 */ 629 if (opcode != IONIC_TXQ_DESC_OPCODE_TSO || 630 flags & IONIC_TXQ_DESC_FLAG_TSO_SOT) 631 dma_unmap_single(dev, (dma_addr_t)addr, 632 le16_to_cpu(desc->len), DMA_TO_DEVICE); 633 else 634 dma_unmap_page(dev, (dma_addr_t)addr, 635 le16_to_cpu(desc->len), DMA_TO_DEVICE); 636 637 for (i = 0; i < nsge; i++, elem++) 638 dma_unmap_page(dev, (dma_addr_t)le64_to_cpu(elem->addr), 639 le16_to_cpu(elem->len), DMA_TO_DEVICE); 640 641 if (cb_arg) { 642 struct sk_buff *skb = cb_arg; 643 u32 len = skb->len; 644 645 queue_index = skb_get_queue_mapping(skb); 646 if (unlikely(__netif_subqueue_stopped(q->lif->netdev, 647 queue_index))) { 648 netif_wake_subqueue(q->lif->netdev, queue_index); 649 q->wake++; 650 } 651 dev_kfree_skb_any(skb); 652 stats->clean++; 653 netdev_tx_completed_queue(q_to_ndq(q), 1, len); 654 } 655 } 656 657 static bool ionic_tx_service(struct ionic_cq *cq, struct ionic_cq_info *cq_info) 658 { 659 struct ionic_txq_comp *comp = cq_info->txcq; 660 struct ionic_queue *q = cq->bound_q; 661 struct ionic_desc_info *desc_info; 662 u16 index; 663 664 if (!color_match(comp->color, cq->done_color)) 665 return false; 666 667 /* clean the related q entries, there could be 668 * several q entries completed for each cq completion 669 */ 670 do { 671 desc_info = &q->info[q->tail_idx]; 672 index = q->tail_idx; 673 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1); 674 ionic_tx_clean(q, desc_info, cq_info, desc_info->cb_arg); 675 desc_info->cb = NULL; 676 desc_info->cb_arg = NULL; 677 } while (index != le16_to_cpu(comp->comp_index)); 678 679 return true; 680 } 681 682 void ionic_tx_flush(struct ionic_cq *cq) 683 { 684 struct ionic_dev *idev = &cq->lif->ionic->idev; 685 u32 work_done; 686 687 work_done = ionic_cq_service(cq, cq->num_descs, 688 ionic_tx_service, NULL, NULL); 689 if (work_done) 690 ionic_intr_credits(idev->intr_ctrl, cq->bound_intr->index, 691 work_done, IONIC_INTR_CRED_RESET_COALESCE); 692 } 693 694 void ionic_tx_empty(struct ionic_queue *q) 695 { 696 struct ionic_desc_info *desc_info; 697 698 /* walk the not completed tx entries, if any */ 699 while (q->head_idx != q->tail_idx) { 700 desc_info = &q->info[q->tail_idx]; 701 q->tail_idx = (q->tail_idx + 1) & (q->num_descs - 1); 702 ionic_tx_clean(q, desc_info, NULL, desc_info->cb_arg); 703 desc_info->cb = NULL; 704 desc_info->cb_arg = NULL; 705 } 706 } 707 708 static int ionic_tx_tcp_inner_pseudo_csum(struct sk_buff *skb) 709 { 710 int err; 711 712 err = skb_cow_head(skb, 0); 713 if (err) 714 return err; 715 716 if (skb->protocol == cpu_to_be16(ETH_P_IP)) { 717 inner_ip_hdr(skb)->check = 0; 718 inner_tcp_hdr(skb)->check = 719 ~csum_tcpudp_magic(inner_ip_hdr(skb)->saddr, 720 inner_ip_hdr(skb)->daddr, 721 0, IPPROTO_TCP, 0); 722 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) { 723 inner_tcp_hdr(skb)->check = 724 ~csum_ipv6_magic(&inner_ipv6_hdr(skb)->saddr, 725 &inner_ipv6_hdr(skb)->daddr, 726 0, IPPROTO_TCP, 0); 727 } 728 729 return 0; 730 } 731 732 static int ionic_tx_tcp_pseudo_csum(struct sk_buff *skb) 733 { 734 int err; 735 736 err = skb_cow_head(skb, 0); 737 if (err) 738 return err; 739 740 if (skb->protocol == cpu_to_be16(ETH_P_IP)) { 741 ip_hdr(skb)->check = 0; 742 tcp_hdr(skb)->check = 743 ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 744 ip_hdr(skb)->daddr, 745 0, IPPROTO_TCP, 0); 746 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) { 747 tcp_v6_gso_csum_prep(skb); 748 } 749 750 return 0; 751 } 752 753 static void ionic_tx_tso_post(struct ionic_queue *q, struct ionic_txq_desc *desc, 754 struct sk_buff *skb, 755 dma_addr_t addr, u8 nsge, u16 len, 756 unsigned int hdrlen, unsigned int mss, 757 bool outer_csum, 758 u16 vlan_tci, bool has_vlan, 759 bool start, bool done) 760 { 761 u8 flags = 0; 762 u64 cmd; 763 764 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0; 765 flags |= outer_csum ? IONIC_TXQ_DESC_FLAG_ENCAP : 0; 766 flags |= start ? IONIC_TXQ_DESC_FLAG_TSO_SOT : 0; 767 flags |= done ? IONIC_TXQ_DESC_FLAG_TSO_EOT : 0; 768 769 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_TSO, flags, nsge, addr); 770 desc->cmd = cpu_to_le64(cmd); 771 desc->len = cpu_to_le16(len); 772 desc->vlan_tci = cpu_to_le16(vlan_tci); 773 desc->hdr_len = cpu_to_le16(hdrlen); 774 desc->mss = cpu_to_le16(mss); 775 776 if (done) { 777 skb_tx_timestamp(skb); 778 netdev_tx_sent_queue(q_to_ndq(q), skb->len); 779 ionic_txq_post(q, !netdev_xmit_more(), ionic_tx_clean, skb); 780 } else { 781 ionic_txq_post(q, false, ionic_tx_clean, NULL); 782 } 783 } 784 785 static struct ionic_txq_desc *ionic_tx_tso_next(struct ionic_queue *q, 786 struct ionic_txq_sg_elem **elem) 787 { 788 struct ionic_txq_sg_desc *sg_desc = q->info[q->head_idx].txq_sg_desc; 789 struct ionic_txq_desc *desc = q->info[q->head_idx].txq_desc; 790 791 *elem = sg_desc->elems; 792 return desc; 793 } 794 795 static int ionic_tx_tso(struct ionic_queue *q, struct sk_buff *skb) 796 { 797 struct ionic_tx_stats *stats = q_to_tx_stats(q); 798 struct ionic_desc_info *rewind_desc_info; 799 struct ionic_txq_sg_elem *elem; 800 struct device *dev = q->dev; 801 struct ionic_txq_desc *desc; 802 unsigned int frag_left = 0; 803 unsigned int offset = 0; 804 u16 abort = q->head_idx; 805 unsigned int len_left; 806 dma_addr_t desc_addr; 807 unsigned int hdrlen; 808 unsigned int nfrags; 809 unsigned int seglen; 810 u64 total_bytes = 0; 811 u64 total_pkts = 0; 812 u16 rewind = abort; 813 unsigned int left; 814 unsigned int len; 815 unsigned int mss; 816 skb_frag_t *frag; 817 bool start, done; 818 bool outer_csum; 819 dma_addr_t addr; 820 bool has_vlan; 821 u16 desc_len; 822 u8 desc_nsge; 823 u16 vlan_tci; 824 bool encap; 825 int err; 826 827 mss = skb_shinfo(skb)->gso_size; 828 nfrags = skb_shinfo(skb)->nr_frags; 829 len_left = skb->len - skb_headlen(skb); 830 outer_csum = (skb_shinfo(skb)->gso_type & SKB_GSO_GRE_CSUM) || 831 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM); 832 has_vlan = !!skb_vlan_tag_present(skb); 833 vlan_tci = skb_vlan_tag_get(skb); 834 encap = skb->encapsulation; 835 836 /* Preload inner-most TCP csum field with IP pseudo hdr 837 * calculated with IP length set to zero. HW will later 838 * add in length to each TCP segment resulting from the TSO. 839 */ 840 841 if (encap) 842 err = ionic_tx_tcp_inner_pseudo_csum(skb); 843 else 844 err = ionic_tx_tcp_pseudo_csum(skb); 845 if (err) 846 return err; 847 848 if (encap) 849 hdrlen = skb_inner_transport_header(skb) - skb->data + 850 inner_tcp_hdrlen(skb); 851 else 852 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb); 853 854 seglen = hdrlen + mss; 855 left = skb_headlen(skb); 856 857 desc = ionic_tx_tso_next(q, &elem); 858 start = true; 859 860 /* Chop skb->data up into desc segments */ 861 862 while (left > 0) { 863 len = min(seglen, left); 864 frag_left = seglen - len; 865 desc_addr = ionic_tx_map_single(q, skb->data + offset, len); 866 if (dma_mapping_error(dev, desc_addr)) 867 goto err_out_abort; 868 desc_len = len; 869 desc_nsge = 0; 870 left -= len; 871 offset += len; 872 if (nfrags > 0 && frag_left > 0) 873 continue; 874 done = (nfrags == 0 && left == 0); 875 ionic_tx_tso_post(q, desc, skb, 876 desc_addr, desc_nsge, desc_len, 877 hdrlen, mss, 878 outer_csum, 879 vlan_tci, has_vlan, 880 start, done); 881 total_pkts++; 882 total_bytes += start ? len : len + hdrlen; 883 desc = ionic_tx_tso_next(q, &elem); 884 start = false; 885 seglen = mss; 886 } 887 888 /* Chop skb frags into desc segments */ 889 890 for (frag = skb_shinfo(skb)->frags; len_left; frag++) { 891 offset = 0; 892 left = skb_frag_size(frag); 893 len_left -= left; 894 nfrags--; 895 stats->frags++; 896 897 while (left > 0) { 898 if (frag_left > 0) { 899 len = min(frag_left, left); 900 frag_left -= len; 901 addr = ionic_tx_map_frag(q, frag, offset, len); 902 if (dma_mapping_error(dev, addr)) 903 goto err_out_abort; 904 elem->addr = cpu_to_le64(addr); 905 elem->len = cpu_to_le16(len); 906 elem++; 907 desc_nsge++; 908 left -= len; 909 offset += len; 910 if (nfrags > 0 && frag_left > 0) 911 continue; 912 done = (nfrags == 0 && left == 0); 913 ionic_tx_tso_post(q, desc, skb, desc_addr, 914 desc_nsge, desc_len, 915 hdrlen, mss, outer_csum, 916 vlan_tci, has_vlan, 917 start, done); 918 total_pkts++; 919 total_bytes += start ? len : len + hdrlen; 920 desc = ionic_tx_tso_next(q, &elem); 921 start = false; 922 } else { 923 len = min(mss, left); 924 frag_left = mss - len; 925 desc_addr = ionic_tx_map_frag(q, frag, 926 offset, len); 927 if (dma_mapping_error(dev, desc_addr)) 928 goto err_out_abort; 929 desc_len = len; 930 desc_nsge = 0; 931 left -= len; 932 offset += len; 933 if (nfrags > 0 && frag_left > 0) 934 continue; 935 done = (nfrags == 0 && left == 0); 936 ionic_tx_tso_post(q, desc, skb, desc_addr, 937 desc_nsge, desc_len, 938 hdrlen, mss, outer_csum, 939 vlan_tci, has_vlan, 940 start, done); 941 total_pkts++; 942 total_bytes += start ? len : len + hdrlen; 943 desc = ionic_tx_tso_next(q, &elem); 944 start = false; 945 } 946 } 947 } 948 949 stats->pkts += total_pkts; 950 stats->bytes += total_bytes; 951 stats->tso++; 952 stats->tso_bytes += total_bytes; 953 954 return 0; 955 956 err_out_abort: 957 while (rewind != q->head_idx) { 958 rewind_desc_info = &q->info[rewind]; 959 ionic_tx_clean(q, rewind_desc_info, NULL, NULL); 960 rewind = (rewind + 1) & (q->num_descs - 1); 961 } 962 q->head_idx = abort; 963 964 return -ENOMEM; 965 } 966 967 static int ionic_tx_calc_csum(struct ionic_queue *q, struct sk_buff *skb) 968 { 969 struct ionic_txq_desc *desc = q->info[q->head_idx].txq_desc; 970 struct ionic_tx_stats *stats = q_to_tx_stats(q); 971 struct device *dev = q->dev; 972 dma_addr_t dma_addr; 973 bool has_vlan; 974 u8 flags = 0; 975 bool encap; 976 u64 cmd; 977 978 has_vlan = !!skb_vlan_tag_present(skb); 979 encap = skb->encapsulation; 980 981 dma_addr = ionic_tx_map_single(q, skb->data, skb_headlen(skb)); 982 if (dma_mapping_error(dev, dma_addr)) 983 return -ENOMEM; 984 985 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0; 986 flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0; 987 988 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_PARTIAL, 989 flags, skb_shinfo(skb)->nr_frags, dma_addr); 990 desc->cmd = cpu_to_le64(cmd); 991 desc->len = cpu_to_le16(skb_headlen(skb)); 992 desc->csum_start = cpu_to_le16(skb_checksum_start_offset(skb)); 993 desc->csum_offset = cpu_to_le16(skb->csum_offset); 994 if (has_vlan) { 995 desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb)); 996 stats->vlan_inserted++; 997 } 998 999 if (skb_csum_is_sctp(skb)) 1000 stats->crc32_csum++; 1001 else 1002 stats->csum++; 1003 1004 return 0; 1005 } 1006 1007 static int ionic_tx_calc_no_csum(struct ionic_queue *q, struct sk_buff *skb) 1008 { 1009 struct ionic_txq_desc *desc = q->info[q->head_idx].txq_desc; 1010 struct ionic_tx_stats *stats = q_to_tx_stats(q); 1011 struct device *dev = q->dev; 1012 dma_addr_t dma_addr; 1013 bool has_vlan; 1014 u8 flags = 0; 1015 bool encap; 1016 u64 cmd; 1017 1018 has_vlan = !!skb_vlan_tag_present(skb); 1019 encap = skb->encapsulation; 1020 1021 dma_addr = ionic_tx_map_single(q, skb->data, skb_headlen(skb)); 1022 if (dma_mapping_error(dev, dma_addr)) 1023 return -ENOMEM; 1024 1025 flags |= has_vlan ? IONIC_TXQ_DESC_FLAG_VLAN : 0; 1026 flags |= encap ? IONIC_TXQ_DESC_FLAG_ENCAP : 0; 1027 1028 cmd = encode_txq_desc_cmd(IONIC_TXQ_DESC_OPCODE_CSUM_NONE, 1029 flags, skb_shinfo(skb)->nr_frags, dma_addr); 1030 desc->cmd = cpu_to_le64(cmd); 1031 desc->len = cpu_to_le16(skb_headlen(skb)); 1032 if (has_vlan) { 1033 desc->vlan_tci = cpu_to_le16(skb_vlan_tag_get(skb)); 1034 stats->vlan_inserted++; 1035 } 1036 1037 stats->csum_none++; 1038 1039 return 0; 1040 } 1041 1042 static int ionic_tx_skb_frags(struct ionic_queue *q, struct sk_buff *skb) 1043 { 1044 struct ionic_txq_sg_desc *sg_desc = q->info[q->head_idx].txq_sg_desc; 1045 unsigned int len_left = skb->len - skb_headlen(skb); 1046 struct ionic_txq_sg_elem *elem = sg_desc->elems; 1047 struct ionic_tx_stats *stats = q_to_tx_stats(q); 1048 struct device *dev = q->dev; 1049 dma_addr_t dma_addr; 1050 skb_frag_t *frag; 1051 u16 len; 1052 1053 for (frag = skb_shinfo(skb)->frags; len_left; frag++, elem++) { 1054 len = skb_frag_size(frag); 1055 elem->len = cpu_to_le16(len); 1056 dma_addr = ionic_tx_map_frag(q, frag, 0, len); 1057 if (dma_mapping_error(dev, dma_addr)) 1058 return -ENOMEM; 1059 elem->addr = cpu_to_le64(dma_addr); 1060 len_left -= len; 1061 stats->frags++; 1062 } 1063 1064 return 0; 1065 } 1066 1067 static int ionic_tx(struct ionic_queue *q, struct sk_buff *skb) 1068 { 1069 struct ionic_tx_stats *stats = q_to_tx_stats(q); 1070 int err; 1071 1072 /* set up the initial descriptor */ 1073 if (skb->ip_summed == CHECKSUM_PARTIAL) 1074 err = ionic_tx_calc_csum(q, skb); 1075 else 1076 err = ionic_tx_calc_no_csum(q, skb); 1077 if (err) 1078 return err; 1079 1080 /* add frags */ 1081 err = ionic_tx_skb_frags(q, skb); 1082 if (err) 1083 return err; 1084 1085 skb_tx_timestamp(skb); 1086 stats->pkts++; 1087 stats->bytes += skb->len; 1088 1089 netdev_tx_sent_queue(q_to_ndq(q), skb->len); 1090 ionic_txq_post(q, !netdev_xmit_more(), ionic_tx_clean, skb); 1091 1092 return 0; 1093 } 1094 1095 static int ionic_tx_descs_needed(struct ionic_queue *q, struct sk_buff *skb) 1096 { 1097 struct ionic_tx_stats *stats = q_to_tx_stats(q); 1098 int err; 1099 1100 /* If TSO, need roundup(skb->len/mss) descs */ 1101 if (skb_is_gso(skb)) 1102 return (skb->len / skb_shinfo(skb)->gso_size) + 1; 1103 1104 /* If non-TSO, just need 1 desc and nr_frags sg elems */ 1105 if (skb_shinfo(skb)->nr_frags <= q->max_sg_elems) 1106 return 1; 1107 1108 /* Too many frags, so linearize */ 1109 err = skb_linearize(skb); 1110 if (err) 1111 return err; 1112 1113 stats->linearize++; 1114 1115 /* Need 1 desc and zero sg elems */ 1116 return 1; 1117 } 1118 1119 static int ionic_maybe_stop_tx(struct ionic_queue *q, int ndescs) 1120 { 1121 int stopped = 0; 1122 1123 if (unlikely(!ionic_q_has_space(q, ndescs))) { 1124 netif_stop_subqueue(q->lif->netdev, q->index); 1125 q->stop++; 1126 stopped = 1; 1127 1128 /* Might race with ionic_tx_clean, check again */ 1129 smp_rmb(); 1130 if (ionic_q_has_space(q, ndescs)) { 1131 netif_wake_subqueue(q->lif->netdev, q->index); 1132 stopped = 0; 1133 } 1134 } 1135 1136 return stopped; 1137 } 1138 1139 netdev_tx_t ionic_start_xmit(struct sk_buff *skb, struct net_device *netdev) 1140 { 1141 u16 queue_index = skb_get_queue_mapping(skb); 1142 struct ionic_lif *lif = netdev_priv(netdev); 1143 struct ionic_queue *q; 1144 int ndescs; 1145 int err; 1146 1147 if (unlikely(!test_bit(IONIC_LIF_F_UP, lif->state))) { 1148 dev_kfree_skb(skb); 1149 return NETDEV_TX_OK; 1150 } 1151 1152 if (unlikely(queue_index >= lif->nxqs)) 1153 queue_index = 0; 1154 q = &lif->txqcqs[queue_index]->q; 1155 1156 ndescs = ionic_tx_descs_needed(q, skb); 1157 if (ndescs < 0) 1158 goto err_out_drop; 1159 1160 if (unlikely(ionic_maybe_stop_tx(q, ndescs))) 1161 return NETDEV_TX_BUSY; 1162 1163 if (skb_is_gso(skb)) 1164 err = ionic_tx_tso(q, skb); 1165 else 1166 err = ionic_tx(q, skb); 1167 1168 if (err) 1169 goto err_out_drop; 1170 1171 /* Stop the queue if there aren't descriptors for the next packet. 1172 * Since our SG lists per descriptor take care of most of the possible 1173 * fragmentation, we don't need to have many descriptors available. 1174 */ 1175 ionic_maybe_stop_tx(q, 4); 1176 1177 return NETDEV_TX_OK; 1178 1179 err_out_drop: 1180 q->stop++; 1181 q->drop++; 1182 dev_kfree_skb(skb); 1183 return NETDEV_TX_OK; 1184 } 1185