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. None of the buffers must have 439 * an skb attached. 440 */ 441 static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue) 442 { 443 struct efx_tx_buffer *buffer; 444 unsigned int bytes_compl = 0; 445 unsigned int pkts_compl = 0; 446 447 /* Work backwards until we hit the original insert pointer value */ 448 while (tx_queue->insert_count != tx_queue->write_count) { 449 --tx_queue->insert_count; 450 buffer = __efx_tx_queue_get_insert_buffer(tx_queue); 451 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl); 452 } 453 } 454 455 /* 456 * Fallback to software TSO. 457 * 458 * This is used if we are unable to send a GSO packet through hardware TSO. 459 * This should only ever happen due to per-queue restrictions - unsupported 460 * packets should first be filtered by the feature flags. 461 * 462 * Returns 0 on success, error code otherwise. 463 */ 464 static int efx_tx_tso_fallback(struct efx_tx_queue *tx_queue, 465 struct sk_buff *skb) 466 { 467 struct sk_buff *segments, *next; 468 469 segments = skb_gso_segment(skb, 0); 470 if (IS_ERR(segments)) 471 return PTR_ERR(segments); 472 473 dev_kfree_skb_any(skb); 474 skb = segments; 475 476 while (skb) { 477 next = skb->next; 478 skb->next = NULL; 479 480 if (next) 481 skb->xmit_more = true; 482 efx_enqueue_skb(tx_queue, skb); 483 skb = next; 484 } 485 486 return 0; 487 } 488 489 /* 490 * Add a socket buffer to a TX queue 491 * 492 * This maps all fragments of a socket buffer for DMA and adds them to 493 * the TX queue. The queue's insert pointer will be incremented by 494 * the number of fragments in the socket buffer. 495 * 496 * If any DMA mapping fails, any mapped fragments will be unmapped, 497 * the queue's insert pointer will be restored to its original value. 498 * 499 * This function is split out from efx_hard_start_xmit to allow the 500 * loopback test to direct packets via specific TX queues. 501 * 502 * Returns NETDEV_TX_OK. 503 * You must hold netif_tx_lock() to call this function. 504 */ 505 netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb) 506 { 507 bool data_mapped = false; 508 unsigned int segments; 509 unsigned int skb_len; 510 int rc; 511 512 skb_len = skb->len; 513 segments = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 0; 514 if (segments == 1) 515 segments = 0; /* Don't use TSO for a single segment. */ 516 517 /* Handle TSO first - it's *possible* (although unlikely) that we might 518 * be passed a packet to segment that's smaller than the copybreak/PIO 519 * size limit. 520 */ 521 if (segments) { 522 EFX_WARN_ON_ONCE_PARANOID(!tx_queue->handle_tso); 523 rc = tx_queue->handle_tso(tx_queue, skb, &data_mapped); 524 if (rc == -EINVAL) { 525 rc = efx_tx_tso_fallback(tx_queue, skb); 526 tx_queue->tso_fallbacks++; 527 if (rc == 0) 528 return 0; 529 } 530 if (rc) 531 goto err; 532 #ifdef EFX_USE_PIO 533 } else if (skb_len <= efx_piobuf_size && !skb->xmit_more && 534 efx_nic_may_tx_pio(tx_queue)) { 535 /* Use PIO for short packets with an empty queue. */ 536 if (efx_enqueue_skb_pio(tx_queue, skb)) 537 goto err; 538 tx_queue->pio_packets++; 539 data_mapped = true; 540 #endif 541 } else if (skb->data_len && skb_len <= EFX_TX_CB_SIZE) { 542 /* Pad short packets or coalesce short fragmented packets. */ 543 if (efx_enqueue_skb_copy(tx_queue, skb)) 544 goto err; 545 tx_queue->cb_packets++; 546 data_mapped = true; 547 } 548 549 /* Map for DMA and create descriptors if we haven't done so already. */ 550 if (!data_mapped && (efx_tx_map_data(tx_queue, skb, segments))) 551 goto err; 552 553 /* Update BQL */ 554 netdev_tx_sent_queue(tx_queue->core_txq, skb_len); 555 556 /* Pass off to hardware */ 557 if (!skb->xmit_more || netif_xmit_stopped(tx_queue->core_txq)) { 558 struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue); 559 560 /* There could be packets left on the partner queue if those 561 * SKBs had skb->xmit_more 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 = skb->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 efx_tx_maybe_stop_queue(tx_queue); 581 582 return NETDEV_TX_OK; 583 584 585 err: 586 efx_enqueue_unwind(tx_queue); 587 dev_kfree_skb_any(skb); 588 return NETDEV_TX_OK; 589 } 590 591 /* Remove packets from the TX queue 592 * 593 * This removes packets from the TX queue, up to and including the 594 * specified index. 595 */ 596 static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue, 597 unsigned int index, 598 unsigned int *pkts_compl, 599 unsigned int *bytes_compl) 600 { 601 struct efx_nic *efx = tx_queue->efx; 602 unsigned int stop_index, read_ptr; 603 604 stop_index = (index + 1) & tx_queue->ptr_mask; 605 read_ptr = tx_queue->read_count & tx_queue->ptr_mask; 606 607 while (read_ptr != stop_index) { 608 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr]; 609 610 if (!(buffer->flags & EFX_TX_BUF_OPTION) && 611 unlikely(buffer->len == 0)) { 612 netif_err(efx, tx_err, efx->net_dev, 613 "TX queue %d spurious TX completion id %x\n", 614 tx_queue->queue, read_ptr); 615 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP); 616 return; 617 } 618 619 efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl); 620 621 ++tx_queue->read_count; 622 read_ptr = tx_queue->read_count & tx_queue->ptr_mask; 623 } 624 } 625 626 /* Initiate a packet transmission. We use one channel per CPU 627 * (sharing when we have more CPUs than channels). On Falcon, the TX 628 * completion events will be directed back to the CPU that transmitted 629 * the packet, which should be cache-efficient. 630 * 631 * Context: non-blocking. 632 * Note that returning anything other than NETDEV_TX_OK will cause the 633 * OS to free the skb. 634 */ 635 netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb, 636 struct net_device *net_dev) 637 { 638 struct efx_nic *efx = netdev_priv(net_dev); 639 struct efx_tx_queue *tx_queue; 640 unsigned index, type; 641 642 EFX_WARN_ON_PARANOID(!netif_device_present(net_dev)); 643 644 /* PTP "event" packet */ 645 if (unlikely(efx_xmit_with_hwtstamp(skb)) && 646 unlikely(efx_ptp_is_ptp_tx(efx, skb))) { 647 return efx_ptp_tx(efx, skb); 648 } 649 650 index = skb_get_queue_mapping(skb); 651 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0; 652 if (index >= efx->n_tx_channels) { 653 index -= efx->n_tx_channels; 654 type |= EFX_TXQ_TYPE_HIGHPRI; 655 } 656 tx_queue = efx_get_tx_queue(efx, index, type); 657 658 return efx_enqueue_skb(tx_queue, skb); 659 } 660 661 void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue) 662 { 663 struct efx_nic *efx = tx_queue->efx; 664 665 /* Must be inverse of queue lookup in efx_hard_start_xmit() */ 666 tx_queue->core_txq = 667 netdev_get_tx_queue(efx->net_dev, 668 tx_queue->queue / EFX_TXQ_TYPES + 669 ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ? 670 efx->n_tx_channels : 0)); 671 } 672 673 int efx_setup_tc(struct net_device *net_dev, enum tc_setup_type type, 674 void *type_data) 675 { 676 struct efx_nic *efx = netdev_priv(net_dev); 677 struct tc_mqprio_qopt *mqprio = type_data; 678 struct efx_channel *channel; 679 struct efx_tx_queue *tx_queue; 680 unsigned tc, num_tc; 681 int rc; 682 683 if (type != TC_SETUP_QDISC_MQPRIO) 684 return -EOPNOTSUPP; 685 686 num_tc = mqprio->num_tc; 687 688 if (num_tc > EFX_MAX_TX_TC) 689 return -EINVAL; 690 691 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 692 693 if (num_tc == net_dev->num_tc) 694 return 0; 695 696 for (tc = 0; tc < num_tc; tc++) { 697 net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels; 698 net_dev->tc_to_txq[tc].count = efx->n_tx_channels; 699 } 700 701 if (num_tc > net_dev->num_tc) { 702 /* Initialise high-priority queues as necessary */ 703 efx_for_each_channel(channel, efx) { 704 efx_for_each_possible_channel_tx_queue(tx_queue, 705 channel) { 706 if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI)) 707 continue; 708 if (!tx_queue->buffer) { 709 rc = efx_probe_tx_queue(tx_queue); 710 if (rc) 711 return rc; 712 } 713 if (!tx_queue->initialised) 714 efx_init_tx_queue(tx_queue); 715 efx_init_tx_queue_core_txq(tx_queue); 716 } 717 } 718 } else { 719 /* Reduce number of classes before number of queues */ 720 net_dev->num_tc = num_tc; 721 } 722 723 rc = netif_set_real_num_tx_queues(net_dev, 724 max_t(int, num_tc, 1) * 725 efx->n_tx_channels); 726 if (rc) 727 return rc; 728 729 /* Do not destroy high-priority queues when they become 730 * unused. We would have to flush them first, and it is 731 * fairly difficult to flush a subset of TX queues. Leave 732 * it to efx_fini_channels(). 733 */ 734 735 net_dev->num_tc = num_tc; 736 return 0; 737 } 738 739 void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index) 740 { 741 unsigned fill_level; 742 struct efx_nic *efx = tx_queue->efx; 743 struct efx_tx_queue *txq2; 744 unsigned int pkts_compl = 0, bytes_compl = 0; 745 746 EFX_WARN_ON_ONCE_PARANOID(index > tx_queue->ptr_mask); 747 748 efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl); 749 tx_queue->pkts_compl += pkts_compl; 750 tx_queue->bytes_compl += bytes_compl; 751 752 if (pkts_compl > 1) 753 ++tx_queue->merge_events; 754 755 /* See if we need to restart the netif queue. This memory 756 * barrier ensures that we write read_count (inside 757 * efx_dequeue_buffers()) before reading the queue status. 758 */ 759 smp_mb(); 760 if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) && 761 likely(efx->port_enabled) && 762 likely(netif_device_present(efx->net_dev))) { 763 txq2 = efx_tx_queue_partner(tx_queue); 764 fill_level = max(tx_queue->insert_count - tx_queue->read_count, 765 txq2->insert_count - txq2->read_count); 766 if (fill_level <= efx->txq_wake_thresh) 767 netif_tx_wake_queue(tx_queue->core_txq); 768 } 769 770 /* Check whether the hardware queue is now empty */ 771 if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) { 772 tx_queue->old_write_count = READ_ONCE(tx_queue->write_count); 773 if (tx_queue->read_count == tx_queue->old_write_count) { 774 smp_mb(); 775 tx_queue->empty_read_count = 776 tx_queue->read_count | EFX_EMPTY_COUNT_VALID; 777 } 778 } 779 } 780 781 static unsigned int efx_tx_cb_page_count(struct efx_tx_queue *tx_queue) 782 { 783 return DIV_ROUND_UP(tx_queue->ptr_mask + 1, PAGE_SIZE >> EFX_TX_CB_ORDER); 784 } 785 786 int efx_probe_tx_queue(struct efx_tx_queue *tx_queue) 787 { 788 struct efx_nic *efx = tx_queue->efx; 789 unsigned int entries; 790 int rc; 791 792 /* Create the smallest power-of-two aligned ring */ 793 entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE); 794 EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE); 795 tx_queue->ptr_mask = entries - 1; 796 797 netif_dbg(efx, probe, efx->net_dev, 798 "creating TX queue %d size %#x mask %#x\n", 799 tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask); 800 801 /* Allocate software ring */ 802 tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer), 803 GFP_KERNEL); 804 if (!tx_queue->buffer) 805 return -ENOMEM; 806 807 tx_queue->cb_page = kcalloc(efx_tx_cb_page_count(tx_queue), 808 sizeof(tx_queue->cb_page[0]), GFP_KERNEL); 809 if (!tx_queue->cb_page) { 810 rc = -ENOMEM; 811 goto fail1; 812 } 813 814 /* Allocate hardware ring */ 815 rc = efx_nic_probe_tx(tx_queue); 816 if (rc) 817 goto fail2; 818 819 return 0; 820 821 fail2: 822 kfree(tx_queue->cb_page); 823 tx_queue->cb_page = NULL; 824 fail1: 825 kfree(tx_queue->buffer); 826 tx_queue->buffer = NULL; 827 return rc; 828 } 829 830 void efx_init_tx_queue(struct efx_tx_queue *tx_queue) 831 { 832 struct efx_nic *efx = tx_queue->efx; 833 834 netif_dbg(efx, drv, efx->net_dev, 835 "initialising TX queue %d\n", tx_queue->queue); 836 837 tx_queue->insert_count = 0; 838 tx_queue->write_count = 0; 839 tx_queue->packet_write_count = 0; 840 tx_queue->old_write_count = 0; 841 tx_queue->read_count = 0; 842 tx_queue->old_read_count = 0; 843 tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID; 844 tx_queue->xmit_more_available = false; 845 tx_queue->timestamping = (efx_ptp_use_mac_tx_timestamps(efx) && 846 tx_queue->channel == efx_ptp_channel(efx)); 847 tx_queue->completed_desc_ptr = tx_queue->ptr_mask; 848 tx_queue->completed_timestamp_major = 0; 849 tx_queue->completed_timestamp_minor = 0; 850 851 /* Set up default function pointers. These may get replaced by 852 * efx_nic_init_tx() based off NIC/queue capabilities. 853 */ 854 tx_queue->handle_tso = efx_enqueue_skb_tso; 855 856 /* Set up TX descriptor ring */ 857 efx_nic_init_tx(tx_queue); 858 859 tx_queue->initialised = true; 860 } 861 862 void efx_fini_tx_queue(struct efx_tx_queue *tx_queue) 863 { 864 struct efx_tx_buffer *buffer; 865 866 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, 867 "shutting down TX queue %d\n", tx_queue->queue); 868 869 if (!tx_queue->buffer) 870 return; 871 872 /* Free any buffers left in the ring */ 873 while (tx_queue->read_count != tx_queue->write_count) { 874 unsigned int pkts_compl = 0, bytes_compl = 0; 875 buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask]; 876 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl); 877 878 ++tx_queue->read_count; 879 } 880 tx_queue->xmit_more_available = false; 881 netdev_tx_reset_queue(tx_queue->core_txq); 882 } 883 884 void efx_remove_tx_queue(struct efx_tx_queue *tx_queue) 885 { 886 int i; 887 888 if (!tx_queue->buffer) 889 return; 890 891 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, 892 "destroying TX queue %d\n", tx_queue->queue); 893 efx_nic_remove_tx(tx_queue); 894 895 if (tx_queue->cb_page) { 896 for (i = 0; i < efx_tx_cb_page_count(tx_queue); i++) 897 efx_nic_free_buffer(tx_queue->efx, 898 &tx_queue->cb_page[i]); 899 kfree(tx_queue->cb_page); 900 tx_queue->cb_page = NULL; 901 } 902 903 kfree(tx_queue->buffer); 904 tx_queue->buffer = NULL; 905 } 906