1 /**************************************************************************** 2 * Driver for Solarflare network controllers and boards 3 * Copyright 2005-2006 Fen Systems Ltd. 4 * Copyright 2005-2013 Solarflare Communications Inc. 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 as published 8 * by the Free Software Foundation, incorporated herein by reference. 9 */ 10 11 #include <linux/pci.h> 12 #include <linux/tcp.h> 13 #include <linux/ip.h> 14 #include <linux/in.h> 15 #include <linux/ipv6.h> 16 #include <linux/slab.h> 17 #include <net/ipv6.h> 18 #include <linux/if_ether.h> 19 #include <linux/highmem.h> 20 #include <linux/cache.h> 21 #include "net_driver.h" 22 #include "efx.h" 23 #include "io.h" 24 #include "nic.h" 25 #include "tx.h" 26 #include "workarounds.h" 27 #include "ef10_regs.h" 28 29 #ifdef EFX_USE_PIO 30 31 #define EFX_PIOBUF_SIZE_DEF ALIGN(256, L1_CACHE_BYTES) 32 unsigned int efx_piobuf_size __read_mostly = EFX_PIOBUF_SIZE_DEF; 33 34 #endif /* EFX_USE_PIO */ 35 36 static inline u8 *efx_tx_get_copy_buffer(struct efx_tx_queue *tx_queue, 37 struct efx_tx_buffer *buffer) 38 { 39 unsigned int index = efx_tx_queue_get_insert_index(tx_queue); 40 struct efx_buffer *page_buf = 41 &tx_queue->cb_page[index >> (PAGE_SHIFT - EFX_TX_CB_ORDER)]; 42 unsigned int offset = 43 ((index << EFX_TX_CB_ORDER) + NET_IP_ALIGN) & (PAGE_SIZE - 1); 44 45 if (unlikely(!page_buf->addr) && 46 efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE, 47 GFP_ATOMIC)) 48 return NULL; 49 buffer->dma_addr = page_buf->dma_addr + offset; 50 buffer->unmap_len = 0; 51 return (u8 *)page_buf->addr + offset; 52 } 53 54 u8 *efx_tx_get_copy_buffer_limited(struct efx_tx_queue *tx_queue, 55 struct efx_tx_buffer *buffer, size_t len) 56 { 57 if (len > EFX_TX_CB_SIZE) 58 return NULL; 59 return efx_tx_get_copy_buffer(tx_queue, buffer); 60 } 61 62 static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue, 63 struct efx_tx_buffer *buffer, 64 unsigned int *pkts_compl, 65 unsigned int *bytes_compl) 66 { 67 if (buffer->unmap_len) { 68 struct device *dma_dev = &tx_queue->efx->pci_dev->dev; 69 dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset; 70 if (buffer->flags & EFX_TX_BUF_MAP_SINGLE) 71 dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len, 72 DMA_TO_DEVICE); 73 else 74 dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len, 75 DMA_TO_DEVICE); 76 buffer->unmap_len = 0; 77 } 78 79 if (buffer->flags & EFX_TX_BUF_SKB) { 80 struct sk_buff *skb = (struct sk_buff *)buffer->skb; 81 82 EFX_WARN_ON_PARANOID(!pkts_compl || !bytes_compl); 83 (*pkts_compl)++; 84 (*bytes_compl) += skb->len; 85 if (tx_queue->timestamping && 86 (tx_queue->completed_timestamp_major || 87 tx_queue->completed_timestamp_minor)) { 88 struct skb_shared_hwtstamps hwtstamp; 89 90 hwtstamp.hwtstamp = 91 efx_ptp_nic_to_kernel_time(tx_queue); 92 skb_tstamp_tx(skb, &hwtstamp); 93 94 tx_queue->completed_timestamp_major = 0; 95 tx_queue->completed_timestamp_minor = 0; 96 } 97 dev_consume_skb_any((struct sk_buff *)buffer->skb); 98 netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev, 99 "TX queue %d transmission id %x complete\n", 100 tx_queue->queue, tx_queue->read_count); 101 } 102 103 buffer->len = 0; 104 buffer->flags = 0; 105 } 106 107 unsigned int efx_tx_max_skb_descs(struct efx_nic *efx) 108 { 109 /* Header and payload descriptor for each output segment, plus 110 * one for every input fragment boundary within a segment 111 */ 112 unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS; 113 114 /* Possibly one more per segment for option descriptors */ 115 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) 116 max_descs += EFX_TSO_MAX_SEGS; 117 118 /* Possibly more for PCIe page boundaries within input fragments */ 119 if (PAGE_SIZE > EFX_PAGE_SIZE) 120 max_descs += max_t(unsigned int, MAX_SKB_FRAGS, 121 DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE)); 122 123 return max_descs; 124 } 125 126 static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1) 127 { 128 /* We need to consider both queues that the net core sees as one */ 129 struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1); 130 struct efx_nic *efx = txq1->efx; 131 unsigned int fill_level; 132 133 fill_level = max(txq1->insert_count - txq1->old_read_count, 134 txq2->insert_count - txq2->old_read_count); 135 if (likely(fill_level < efx->txq_stop_thresh)) 136 return; 137 138 /* We used the stale old_read_count above, which gives us a 139 * pessimistic estimate of the fill level (which may even 140 * validly be >= efx->txq_entries). Now try again using 141 * read_count (more likely to be a cache miss). 142 * 143 * If we read read_count and then conditionally stop the 144 * queue, it is possible for the completion path to race with 145 * us and complete all outstanding descriptors in the middle, 146 * after which there will be no more completions to wake it. 147 * Therefore we stop the queue first, then read read_count 148 * (with a memory barrier to ensure the ordering), then 149 * restart the queue if the fill level turns out to be low 150 * enough. 151 */ 152 netif_tx_stop_queue(txq1->core_txq); 153 smp_mb(); 154 txq1->old_read_count = READ_ONCE(txq1->read_count); 155 txq2->old_read_count = READ_ONCE(txq2->read_count); 156 157 fill_level = max(txq1->insert_count - txq1->old_read_count, 158 txq2->insert_count - txq2->old_read_count); 159 EFX_WARN_ON_ONCE_PARANOID(fill_level >= efx->txq_entries); 160 if (likely(fill_level < efx->txq_stop_thresh)) { 161 smp_mb(); 162 if (likely(!efx->loopback_selftest)) 163 netif_tx_start_queue(txq1->core_txq); 164 } 165 } 166 167 static int efx_enqueue_skb_copy(struct efx_tx_queue *tx_queue, 168 struct sk_buff *skb) 169 { 170 unsigned int copy_len = skb->len; 171 struct efx_tx_buffer *buffer; 172 u8 *copy_buffer; 173 int rc; 174 175 EFX_WARN_ON_ONCE_PARANOID(copy_len > EFX_TX_CB_SIZE); 176 177 buffer = efx_tx_queue_get_insert_buffer(tx_queue); 178 179 copy_buffer = efx_tx_get_copy_buffer(tx_queue, buffer); 180 if (unlikely(!copy_buffer)) 181 return -ENOMEM; 182 183 rc = skb_copy_bits(skb, 0, copy_buffer, copy_len); 184 EFX_WARN_ON_PARANOID(rc); 185 buffer->len = copy_len; 186 187 buffer->skb = skb; 188 buffer->flags = EFX_TX_BUF_SKB; 189 190 ++tx_queue->insert_count; 191 return rc; 192 } 193 194 #ifdef EFX_USE_PIO 195 196 struct efx_short_copy_buffer { 197 int used; 198 u8 buf[L1_CACHE_BYTES]; 199 }; 200 201 /* Copy to PIO, respecting that writes to PIO buffers must be dword aligned. 202 * Advances piobuf pointer. Leaves additional data in the copy buffer. 203 */ 204 static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf, 205 u8 *data, int len, 206 struct efx_short_copy_buffer *copy_buf) 207 { 208 int block_len = len & ~(sizeof(copy_buf->buf) - 1); 209 210 __iowrite64_copy(*piobuf, data, block_len >> 3); 211 *piobuf += block_len; 212 len -= block_len; 213 214 if (len) { 215 data += block_len; 216 BUG_ON(copy_buf->used); 217 BUG_ON(len > sizeof(copy_buf->buf)); 218 memcpy(copy_buf->buf, data, len); 219 copy_buf->used = len; 220 } 221 } 222 223 /* Copy to PIO, respecting dword alignment, popping data from copy buffer first. 224 * Advances piobuf pointer. Leaves additional data in the copy buffer. 225 */ 226 static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf, 227 u8 *data, int len, 228 struct efx_short_copy_buffer *copy_buf) 229 { 230 if (copy_buf->used) { 231 /* if the copy buffer is partially full, fill it up and write */ 232 int copy_to_buf = 233 min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len); 234 235 memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf); 236 copy_buf->used += copy_to_buf; 237 238 /* if we didn't fill it up then we're done for now */ 239 if (copy_buf->used < sizeof(copy_buf->buf)) 240 return; 241 242 __iowrite64_copy(*piobuf, copy_buf->buf, 243 sizeof(copy_buf->buf) >> 3); 244 *piobuf += sizeof(copy_buf->buf); 245 data += copy_to_buf; 246 len -= copy_to_buf; 247 copy_buf->used = 0; 248 } 249 250 efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf); 251 } 252 253 static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf, 254 struct efx_short_copy_buffer *copy_buf) 255 { 256 /* if there's anything in it, write the whole buffer, including junk */ 257 if (copy_buf->used) 258 __iowrite64_copy(piobuf, copy_buf->buf, 259 sizeof(copy_buf->buf) >> 3); 260 } 261 262 /* Traverse skb structure and copy fragments in to PIO buffer. 263 * Advances piobuf pointer. 264 */ 265 static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb, 266 u8 __iomem **piobuf, 267 struct efx_short_copy_buffer *copy_buf) 268 { 269 int i; 270 271 efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb), 272 copy_buf); 273 274 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) { 275 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 276 u8 *vaddr; 277 278 vaddr = kmap_atomic(skb_frag_page(f)); 279 280 efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset, 281 skb_frag_size(f), copy_buf); 282 kunmap_atomic(vaddr); 283 } 284 285 EFX_WARN_ON_ONCE_PARANOID(skb_shinfo(skb)->frag_list); 286 } 287 288 static int efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue, 289 struct sk_buff *skb) 290 { 291 struct efx_tx_buffer *buffer = 292 efx_tx_queue_get_insert_buffer(tx_queue); 293 u8 __iomem *piobuf = tx_queue->piobuf; 294 295 /* Copy to PIO buffer. Ensure the writes are padded to the end 296 * of a cache line, as this is required for write-combining to be 297 * effective on at least x86. 298 */ 299 300 if (skb_shinfo(skb)->nr_frags) { 301 /* The size of the copy buffer will ensure all writes 302 * are the size of a cache line. 303 */ 304 struct efx_short_copy_buffer copy_buf; 305 306 copy_buf.used = 0; 307 308 efx_skb_copy_bits_to_pio(tx_queue->efx, skb, 309 &piobuf, ©_buf); 310 efx_flush_copy_buffer(tx_queue->efx, piobuf, ©_buf); 311 } else { 312 /* Pad the write to the size of a cache line. 313 * We can do this because we know the skb_shared_info struct is 314 * after the source, and the destination buffer is big enough. 315 */ 316 BUILD_BUG_ON(L1_CACHE_BYTES > 317 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 318 __iowrite64_copy(tx_queue->piobuf, skb->data, 319 ALIGN(skb->len, L1_CACHE_BYTES) >> 3); 320 } 321 322 buffer->skb = skb; 323 buffer->flags = EFX_TX_BUF_SKB | EFX_TX_BUF_OPTION; 324 325 EFX_POPULATE_QWORD_5(buffer->option, 326 ESF_DZ_TX_DESC_IS_OPT, 1, 327 ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO, 328 ESF_DZ_TX_PIO_CONT, 0, 329 ESF_DZ_TX_PIO_BYTE_CNT, skb->len, 330 ESF_DZ_TX_PIO_BUF_ADDR, 331 tx_queue->piobuf_offset); 332 ++tx_queue->insert_count; 333 return 0; 334 } 335 #endif /* EFX_USE_PIO */ 336 337 static struct efx_tx_buffer *efx_tx_map_chunk(struct efx_tx_queue *tx_queue, 338 dma_addr_t dma_addr, 339 size_t len) 340 { 341 const struct efx_nic_type *nic_type = tx_queue->efx->type; 342 struct efx_tx_buffer *buffer; 343 unsigned int dma_len; 344 345 /* Map the fragment taking account of NIC-dependent DMA limits. */ 346 do { 347 buffer = efx_tx_queue_get_insert_buffer(tx_queue); 348 dma_len = nic_type->tx_limit_len(tx_queue, dma_addr, len); 349 350 buffer->len = dma_len; 351 buffer->dma_addr = dma_addr; 352 buffer->flags = EFX_TX_BUF_CONT; 353 len -= dma_len; 354 dma_addr += dma_len; 355 ++tx_queue->insert_count; 356 } while (len); 357 358 return buffer; 359 } 360 361 /* Map all data from an SKB for DMA and create descriptors on the queue. 362 */ 363 static int efx_tx_map_data(struct efx_tx_queue *tx_queue, struct sk_buff *skb, 364 unsigned int segment_count) 365 { 366 struct efx_nic *efx = tx_queue->efx; 367 struct device *dma_dev = &efx->pci_dev->dev; 368 unsigned int frag_index, nr_frags; 369 dma_addr_t dma_addr, unmap_addr; 370 unsigned short dma_flags; 371 size_t len, unmap_len; 372 373 nr_frags = skb_shinfo(skb)->nr_frags; 374 frag_index = 0; 375 376 /* Map header data. */ 377 len = skb_headlen(skb); 378 dma_addr = dma_map_single(dma_dev, skb->data, len, DMA_TO_DEVICE); 379 dma_flags = EFX_TX_BUF_MAP_SINGLE; 380 unmap_len = len; 381 unmap_addr = dma_addr; 382 383 if (unlikely(dma_mapping_error(dma_dev, dma_addr))) 384 return -EIO; 385 386 if (segment_count) { 387 /* For TSO we need to put the header in to a separate 388 * descriptor. Map this separately if necessary. 389 */ 390 size_t header_len = skb_transport_header(skb) - skb->data + 391 (tcp_hdr(skb)->doff << 2u); 392 393 if (header_len != len) { 394 tx_queue->tso_long_headers++; 395 efx_tx_map_chunk(tx_queue, dma_addr, header_len); 396 len -= header_len; 397 dma_addr += header_len; 398 } 399 } 400 401 /* Add descriptors for each fragment. */ 402 do { 403 struct efx_tx_buffer *buffer; 404 skb_frag_t *fragment; 405 406 buffer = efx_tx_map_chunk(tx_queue, dma_addr, len); 407 408 /* The final descriptor for a fragment is responsible for 409 * unmapping the whole fragment. 410 */ 411 buffer->flags = EFX_TX_BUF_CONT | dma_flags; 412 buffer->unmap_len = unmap_len; 413 buffer->dma_offset = buffer->dma_addr - unmap_addr; 414 415 if (frag_index >= nr_frags) { 416 /* Store SKB details with the final buffer for 417 * the completion. 418 */ 419 buffer->skb = skb; 420 buffer->flags = EFX_TX_BUF_SKB | dma_flags; 421 return 0; 422 } 423 424 /* Move on to the next fragment. */ 425 fragment = &skb_shinfo(skb)->frags[frag_index++]; 426 len = skb_frag_size(fragment); 427 dma_addr = skb_frag_dma_map(dma_dev, fragment, 428 0, len, DMA_TO_DEVICE); 429 dma_flags = 0; 430 unmap_len = len; 431 unmap_addr = dma_addr; 432 433 if (unlikely(dma_mapping_error(dma_dev, dma_addr))) 434 return -EIO; 435 } while (1); 436 } 437 438 /* Remove buffers put into a tx_queue for the current packet. 439 * None of the buffers must have an skb attached. 440 */ 441 static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue, 442 unsigned int insert_count) 443 { 444 struct efx_tx_buffer *buffer; 445 unsigned int bytes_compl = 0; 446 unsigned int pkts_compl = 0; 447 448 /* Work backwards until we hit the original insert pointer value */ 449 while (tx_queue->insert_count != insert_count) { 450 --tx_queue->insert_count; 451 buffer = __efx_tx_queue_get_insert_buffer(tx_queue); 452 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl); 453 } 454 } 455 456 /* 457 * Fallback to software TSO. 458 * 459 * This is used if we are unable to send a GSO packet through hardware TSO. 460 * This should only ever happen due to per-queue restrictions - unsupported 461 * packets should first be filtered by the feature flags. 462 * 463 * Returns 0 on success, error code otherwise. 464 */ 465 static int efx_tx_tso_fallback(struct efx_tx_queue *tx_queue, 466 struct sk_buff *skb) 467 { 468 struct sk_buff *segments, *next; 469 470 segments = skb_gso_segment(skb, 0); 471 if (IS_ERR(segments)) 472 return PTR_ERR(segments); 473 474 dev_consume_skb_any(skb); 475 skb = segments; 476 477 while (skb) { 478 next = skb->next; 479 skb->next = NULL; 480 481 efx_enqueue_skb(tx_queue, skb); 482 skb = next; 483 } 484 485 return 0; 486 } 487 488 /* 489 * Add a socket buffer to a TX queue 490 * 491 * This maps all fragments of a socket buffer for DMA and adds them to 492 * the TX queue. The queue's insert pointer will be incremented by 493 * the number of fragments in the socket buffer. 494 * 495 * If any DMA mapping fails, any mapped fragments will be unmapped, 496 * the queue's insert pointer will be restored to its original value. 497 * 498 * This function is split out from efx_hard_start_xmit to allow the 499 * loopback test to direct packets via specific TX queues. 500 * 501 * Returns NETDEV_TX_OK. 502 * You must hold netif_tx_lock() to call this function. 503 */ 504 netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb) 505 { 506 unsigned int old_insert_count = tx_queue->insert_count; 507 bool xmit_more = netdev_xmit_more(); 508 bool data_mapped = false; 509 unsigned int segments; 510 unsigned int skb_len; 511 int rc; 512 513 skb_len = skb->len; 514 segments = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 0; 515 if (segments == 1) 516 segments = 0; /* Don't use TSO for a single segment. */ 517 518 /* Handle TSO first - it's *possible* (although unlikely) that we might 519 * be passed a packet to segment that's smaller than the copybreak/PIO 520 * size limit. 521 */ 522 if (segments) { 523 EFX_WARN_ON_ONCE_PARANOID(!tx_queue->handle_tso); 524 rc = tx_queue->handle_tso(tx_queue, skb, &data_mapped); 525 if (rc == -EINVAL) { 526 rc = efx_tx_tso_fallback(tx_queue, skb); 527 tx_queue->tso_fallbacks++; 528 if (rc == 0) 529 return 0; 530 } 531 if (rc) 532 goto err; 533 #ifdef EFX_USE_PIO 534 } else if (skb_len <= efx_piobuf_size && !xmit_more && 535 efx_nic_may_tx_pio(tx_queue)) { 536 /* Use PIO for short packets with an empty queue. */ 537 if (efx_enqueue_skb_pio(tx_queue, skb)) 538 goto err; 539 tx_queue->pio_packets++; 540 data_mapped = true; 541 #endif 542 } else if (skb->data_len && skb_len <= EFX_TX_CB_SIZE) { 543 /* Pad short packets or coalesce short fragmented packets. */ 544 if (efx_enqueue_skb_copy(tx_queue, skb)) 545 goto err; 546 tx_queue->cb_packets++; 547 data_mapped = true; 548 } 549 550 /* Map for DMA and create descriptors if we haven't done so already. */ 551 if (!data_mapped && (efx_tx_map_data(tx_queue, skb, segments))) 552 goto err; 553 554 efx_tx_maybe_stop_queue(tx_queue); 555 556 /* Pass off to hardware */ 557 if (__netdev_tx_sent_queue(tx_queue->core_txq, skb_len, xmit_more)) { 558 struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue); 559 560 /* There could be packets left on the partner queue if 561 * xmit_more was set. If we do not push those they 562 * could be left for a long time and cause a netdev watchdog. 563 */ 564 if (txq2->xmit_more_available) 565 efx_nic_push_buffers(txq2); 566 567 efx_nic_push_buffers(tx_queue); 568 } else { 569 tx_queue->xmit_more_available = xmit_more; 570 } 571 572 if (segments) { 573 tx_queue->tso_bursts++; 574 tx_queue->tso_packets += segments; 575 tx_queue->tx_packets += segments; 576 } else { 577 tx_queue->tx_packets++; 578 } 579 580 return NETDEV_TX_OK; 581 582 583 err: 584 efx_enqueue_unwind(tx_queue, old_insert_count); 585 dev_kfree_skb_any(skb); 586 587 /* If we're not expecting another transmit and we had something to push 588 * on this queue or a partner queue then we need to push here to get the 589 * previous packets out. 590 */ 591 if (!xmit_more) { 592 struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue); 593 594 if (txq2->xmit_more_available) 595 efx_nic_push_buffers(txq2); 596 597 efx_nic_push_buffers(tx_queue); 598 } 599 600 return NETDEV_TX_OK; 601 } 602 603 /* Remove packets from the TX queue 604 * 605 * This removes packets from the TX queue, up to and including the 606 * specified index. 607 */ 608 static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue, 609 unsigned int index, 610 unsigned int *pkts_compl, 611 unsigned int *bytes_compl) 612 { 613 struct efx_nic *efx = tx_queue->efx; 614 unsigned int stop_index, read_ptr; 615 616 stop_index = (index + 1) & tx_queue->ptr_mask; 617 read_ptr = tx_queue->read_count & tx_queue->ptr_mask; 618 619 while (read_ptr != stop_index) { 620 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr]; 621 622 if (!(buffer->flags & EFX_TX_BUF_OPTION) && 623 unlikely(buffer->len == 0)) { 624 netif_err(efx, tx_err, efx->net_dev, 625 "TX queue %d spurious TX completion id %x\n", 626 tx_queue->queue, read_ptr); 627 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP); 628 return; 629 } 630 631 efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl); 632 633 ++tx_queue->read_count; 634 read_ptr = tx_queue->read_count & tx_queue->ptr_mask; 635 } 636 } 637 638 /* Initiate a packet transmission. We use one channel per CPU 639 * (sharing when we have more CPUs than channels). On Falcon, the TX 640 * completion events will be directed back to the CPU that transmitted 641 * the packet, which should be cache-efficient. 642 * 643 * Context: non-blocking. 644 * Note that returning anything other than NETDEV_TX_OK will cause the 645 * OS to free the skb. 646 */ 647 netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb, 648 struct net_device *net_dev) 649 { 650 struct efx_nic *efx = netdev_priv(net_dev); 651 struct efx_tx_queue *tx_queue; 652 unsigned index, type; 653 654 EFX_WARN_ON_PARANOID(!netif_device_present(net_dev)); 655 656 /* PTP "event" packet */ 657 if (unlikely(efx_xmit_with_hwtstamp(skb)) && 658 unlikely(efx_ptp_is_ptp_tx(efx, skb))) { 659 return efx_ptp_tx(efx, skb); 660 } 661 662 index = skb_get_queue_mapping(skb); 663 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0; 664 if (index >= efx->n_tx_channels) { 665 index -= efx->n_tx_channels; 666 type |= EFX_TXQ_TYPE_HIGHPRI; 667 } 668 tx_queue = efx_get_tx_queue(efx, index, type); 669 670 return efx_enqueue_skb(tx_queue, skb); 671 } 672 673 void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue) 674 { 675 struct efx_nic *efx = tx_queue->efx; 676 677 /* Must be inverse of queue lookup in efx_hard_start_xmit() */ 678 tx_queue->core_txq = 679 netdev_get_tx_queue(efx->net_dev, 680 tx_queue->queue / EFX_TXQ_TYPES + 681 ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ? 682 efx->n_tx_channels : 0)); 683 } 684 685 int efx_setup_tc(struct net_device *net_dev, enum tc_setup_type type, 686 void *type_data) 687 { 688 struct efx_nic *efx = netdev_priv(net_dev); 689 struct tc_mqprio_qopt *mqprio = type_data; 690 struct efx_channel *channel; 691 struct efx_tx_queue *tx_queue; 692 unsigned tc, num_tc; 693 int rc; 694 695 if (type != TC_SETUP_QDISC_MQPRIO) 696 return -EOPNOTSUPP; 697 698 num_tc = mqprio->num_tc; 699 700 if (num_tc > EFX_MAX_TX_TC) 701 return -EINVAL; 702 703 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 704 705 if (num_tc == net_dev->num_tc) 706 return 0; 707 708 for (tc = 0; tc < num_tc; tc++) { 709 net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels; 710 net_dev->tc_to_txq[tc].count = efx->n_tx_channels; 711 } 712 713 if (num_tc > net_dev->num_tc) { 714 /* Initialise high-priority queues as necessary */ 715 efx_for_each_channel(channel, efx) { 716 efx_for_each_possible_channel_tx_queue(tx_queue, 717 channel) { 718 if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI)) 719 continue; 720 if (!tx_queue->buffer) { 721 rc = efx_probe_tx_queue(tx_queue); 722 if (rc) 723 return rc; 724 } 725 if (!tx_queue->initialised) 726 efx_init_tx_queue(tx_queue); 727 efx_init_tx_queue_core_txq(tx_queue); 728 } 729 } 730 } else { 731 /* Reduce number of classes before number of queues */ 732 net_dev->num_tc = num_tc; 733 } 734 735 rc = netif_set_real_num_tx_queues(net_dev, 736 max_t(int, num_tc, 1) * 737 efx->n_tx_channels); 738 if (rc) 739 return rc; 740 741 /* Do not destroy high-priority queues when they become 742 * unused. We would have to flush them first, and it is 743 * fairly difficult to flush a subset of TX queues. Leave 744 * it to efx_fini_channels(). 745 */ 746 747 net_dev->num_tc = num_tc; 748 return 0; 749 } 750 751 void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index) 752 { 753 unsigned fill_level; 754 struct efx_nic *efx = tx_queue->efx; 755 struct efx_tx_queue *txq2; 756 unsigned int pkts_compl = 0, bytes_compl = 0; 757 758 EFX_WARN_ON_ONCE_PARANOID(index > tx_queue->ptr_mask); 759 760 efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl); 761 tx_queue->pkts_compl += pkts_compl; 762 tx_queue->bytes_compl += bytes_compl; 763 764 if (pkts_compl > 1) 765 ++tx_queue->merge_events; 766 767 /* See if we need to restart the netif queue. This memory 768 * barrier ensures that we write read_count (inside 769 * efx_dequeue_buffers()) before reading the queue status. 770 */ 771 smp_mb(); 772 if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) && 773 likely(efx->port_enabled) && 774 likely(netif_device_present(efx->net_dev))) { 775 txq2 = efx_tx_queue_partner(tx_queue); 776 fill_level = max(tx_queue->insert_count - tx_queue->read_count, 777 txq2->insert_count - txq2->read_count); 778 if (fill_level <= efx->txq_wake_thresh) 779 netif_tx_wake_queue(tx_queue->core_txq); 780 } 781 782 /* Check whether the hardware queue is now empty */ 783 if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) { 784 tx_queue->old_write_count = READ_ONCE(tx_queue->write_count); 785 if (tx_queue->read_count == tx_queue->old_write_count) { 786 smp_mb(); 787 tx_queue->empty_read_count = 788 tx_queue->read_count | EFX_EMPTY_COUNT_VALID; 789 } 790 } 791 } 792 793 static unsigned int efx_tx_cb_page_count(struct efx_tx_queue *tx_queue) 794 { 795 return DIV_ROUND_UP(tx_queue->ptr_mask + 1, PAGE_SIZE >> EFX_TX_CB_ORDER); 796 } 797 798 int efx_probe_tx_queue(struct efx_tx_queue *tx_queue) 799 { 800 struct efx_nic *efx = tx_queue->efx; 801 unsigned int entries; 802 int rc; 803 804 /* Create the smallest power-of-two aligned ring */ 805 entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE); 806 EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE); 807 tx_queue->ptr_mask = entries - 1; 808 809 netif_dbg(efx, probe, efx->net_dev, 810 "creating TX queue %d size %#x mask %#x\n", 811 tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask); 812 813 /* Allocate software ring */ 814 tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer), 815 GFP_KERNEL); 816 if (!tx_queue->buffer) 817 return -ENOMEM; 818 819 tx_queue->cb_page = kcalloc(efx_tx_cb_page_count(tx_queue), 820 sizeof(tx_queue->cb_page[0]), GFP_KERNEL); 821 if (!tx_queue->cb_page) { 822 rc = -ENOMEM; 823 goto fail1; 824 } 825 826 /* Allocate hardware ring */ 827 rc = efx_nic_probe_tx(tx_queue); 828 if (rc) 829 goto fail2; 830 831 return 0; 832 833 fail2: 834 kfree(tx_queue->cb_page); 835 tx_queue->cb_page = NULL; 836 fail1: 837 kfree(tx_queue->buffer); 838 tx_queue->buffer = NULL; 839 return rc; 840 } 841 842 void efx_init_tx_queue(struct efx_tx_queue *tx_queue) 843 { 844 struct efx_nic *efx = tx_queue->efx; 845 846 netif_dbg(efx, drv, efx->net_dev, 847 "initialising TX queue %d\n", tx_queue->queue); 848 849 tx_queue->insert_count = 0; 850 tx_queue->write_count = 0; 851 tx_queue->packet_write_count = 0; 852 tx_queue->old_write_count = 0; 853 tx_queue->read_count = 0; 854 tx_queue->old_read_count = 0; 855 tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID; 856 tx_queue->xmit_more_available = false; 857 tx_queue->timestamping = (efx_ptp_use_mac_tx_timestamps(efx) && 858 tx_queue->channel == efx_ptp_channel(efx)); 859 tx_queue->completed_desc_ptr = tx_queue->ptr_mask; 860 tx_queue->completed_timestamp_major = 0; 861 tx_queue->completed_timestamp_minor = 0; 862 863 /* Set up default function pointers. These may get replaced by 864 * efx_nic_init_tx() based off NIC/queue capabilities. 865 */ 866 tx_queue->handle_tso = efx_enqueue_skb_tso; 867 868 /* Set up TX descriptor ring */ 869 efx_nic_init_tx(tx_queue); 870 871 tx_queue->initialised = true; 872 } 873 874 void efx_fini_tx_queue(struct efx_tx_queue *tx_queue) 875 { 876 struct efx_tx_buffer *buffer; 877 878 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, 879 "shutting down TX queue %d\n", tx_queue->queue); 880 881 if (!tx_queue->buffer) 882 return; 883 884 /* Free any buffers left in the ring */ 885 while (tx_queue->read_count != tx_queue->write_count) { 886 unsigned int pkts_compl = 0, bytes_compl = 0; 887 buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask]; 888 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl); 889 890 ++tx_queue->read_count; 891 } 892 tx_queue->xmit_more_available = false; 893 netdev_tx_reset_queue(tx_queue->core_txq); 894 } 895 896 void efx_remove_tx_queue(struct efx_tx_queue *tx_queue) 897 { 898 int i; 899 900 if (!tx_queue->buffer) 901 return; 902 903 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, 904 "destroying TX queue %d\n", tx_queue->queue); 905 efx_nic_remove_tx(tx_queue); 906 907 if (tx_queue->cb_page) { 908 for (i = 0; i < efx_tx_cb_page_count(tx_queue); i++) 909 efx_nic_free_buffer(tx_queue->efx, 910 &tx_queue->cb_page[i]); 911 kfree(tx_queue->cb_page); 912 tx_queue->cb_page = NULL; 913 } 914 915 kfree(tx_queue->buffer); 916 tx_queue->buffer = NULL; 917 } 918