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