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