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