Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Driver for Solarflare Solarstorm network controllers and boards |
| 3 | * Copyright 2005-2006 Fen Systems Ltd. |
Ben Hutchings | 0a6f40c | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4 | * Copyright 2005-2011 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/socket.h> |
| 12 | #include <linux/in.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 14 | #include <linux/ip.h> |
| 15 | #include <linux/tcp.h> |
| 16 | #include <linux/udp.h> |
Paul Gortmaker | 70c7160 | 2011-05-22 16:47:17 -0400 | [diff] [blame] | 17 | #include <linux/prefetch.h> |
Paul Gortmaker | 6eb07ca | 2011-09-15 19:46:05 -0400 | [diff] [blame] | 18 | #include <linux/moduleparam.h> |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 19 | #include <net/ip.h> |
| 20 | #include <net/checksum.h> |
| 21 | #include "net_driver.h" |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 22 | #include "efx.h" |
Ben Hutchings | 744093c | 2009-11-29 15:12:08 +0000 | [diff] [blame] | 23 | #include "nic.h" |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 24 | #include "selftest.h" |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 25 | #include "workarounds.h" |
| 26 | |
| 27 | /* Number of RX descriptors pushed at once. */ |
| 28 | #define EFX_RX_BATCH 8 |
| 29 | |
Ben Hutchings | 272baee | 2013-01-29 23:33:14 +0000 | [diff] [blame] | 30 | /* Maximum length for an RX descriptor sharing a page */ |
| 31 | #define EFX_RX_HALF_PAGE ((PAGE_SIZE >> 1) - sizeof(struct efx_rx_page_state) \ |
| 32 | - EFX_PAGE_IP_ALIGN) |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 33 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 34 | /* Size of buffer allocated for skb header area. */ |
| 35 | #define EFX_SKB_HEADERS 64u |
| 36 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 37 | /* This is the percentage fill level below which new RX descriptors |
| 38 | * will be added to the RX descriptor ring. |
| 39 | */ |
David Riddoch | 6423518 | 2012-04-11 13:12:41 +0100 | [diff] [blame] | 40 | static unsigned int rx_refill_threshold; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 41 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 42 | /* Each packet can consume up to ceil(max_frame_len / buffer_size) buffers */ |
| 43 | #define EFX_RX_MAX_FRAGS DIV_ROUND_UP(EFX_MAX_FRAME_LEN(EFX_MAX_MTU), \ |
| 44 | EFX_RX_USR_BUF_SIZE) |
| 45 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 46 | /* |
| 47 | * RX maximum head room required. |
| 48 | * |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 49 | * This must be at least 1 to prevent overflow, plus one packet-worth |
| 50 | * to allow pipelined receives. |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 51 | */ |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 52 | #define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 53 | |
Ben Hutchings | b184f16 | 2013-01-29 23:33:15 +0000 | [diff] [blame] | 54 | static inline u8 *efx_rx_buf_va(struct efx_rx_buffer *buf) |
Ben Hutchings | 39c9cf0 | 2010-06-23 11:31:28 +0000 | [diff] [blame] | 55 | { |
Ben Hutchings | b184f16 | 2013-01-29 23:33:15 +0000 | [diff] [blame] | 56 | return page_address(buf->page) + buf->page_offset; |
Steve Hodgson | a526f14 | 2011-02-24 23:45:16 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static inline u32 efx_rx_buf_hash(const u8 *eh) |
| 60 | { |
| 61 | /* The ethernet header is always directly after any hash. */ |
Ben Hutchings | 39c9cf0 | 2010-06-23 11:31:28 +0000 | [diff] [blame] | 62 | #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || NET_IP_ALIGN % 4 == 0 |
Steve Hodgson | a526f14 | 2011-02-24 23:45:16 +0000 | [diff] [blame] | 63 | return __le32_to_cpup((const __le32 *)(eh - 4)); |
Ben Hutchings | 39c9cf0 | 2010-06-23 11:31:28 +0000 | [diff] [blame] | 64 | #else |
Steve Hodgson | a526f14 | 2011-02-24 23:45:16 +0000 | [diff] [blame] | 65 | const u8 *data = eh - 4; |
Ben Hutchings | 0beaca2 | 2012-01-05 18:54:04 +0000 | [diff] [blame] | 66 | return (u32)data[0] | |
| 67 | (u32)data[1] << 8 | |
| 68 | (u32)data[2] << 16 | |
| 69 | (u32)data[3] << 24; |
Ben Hutchings | 39c9cf0 | 2010-06-23 11:31:28 +0000 | [diff] [blame] | 70 | #endif |
| 71 | } |
| 72 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 73 | static inline struct efx_rx_buffer * |
| 74 | efx_rx_buf_next(struct efx_rx_queue *rx_queue, struct efx_rx_buffer *rx_buf) |
| 75 | { |
| 76 | if (unlikely(rx_buf == efx_rx_buffer(rx_queue, rx_queue->ptr_mask))) |
| 77 | return efx_rx_buffer(rx_queue, 0); |
| 78 | else |
| 79 | return rx_buf + 1; |
| 80 | } |
| 81 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 82 | /** |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 83 | * efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 84 | * |
| 85 | * @rx_queue: Efx RX queue |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 86 | * |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 87 | * This allocates memory for EFX_RX_BATCH receive buffers, maps them for DMA, |
| 88 | * and populates struct efx_rx_buffers for each one. Return a negative error |
| 89 | * code or 0 on success. If a single page can be split between two buffers, |
| 90 | * then the page will either be inserted fully, or not at at all. |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 91 | */ |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 92 | static int efx_init_rx_buffers(struct efx_rx_queue *rx_queue) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 93 | { |
| 94 | struct efx_nic *efx = rx_queue->efx; |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 95 | struct efx_rx_buffer *rx_buf; |
| 96 | struct page *page; |
Ben Hutchings | b590ace | 2013-01-10 23:51:54 +0000 | [diff] [blame] | 97 | unsigned int page_offset; |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 98 | struct efx_rx_page_state *state; |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 99 | dma_addr_t dma_addr; |
| 100 | unsigned index, count; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 101 | |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 102 | /* We can split a page between two buffers */ |
| 103 | BUILD_BUG_ON(EFX_RX_BATCH & 1); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 104 | |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 105 | for (count = 0; count < EFX_RX_BATCH; ++count) { |
| 106 | page = alloc_pages(__GFP_COLD | __GFP_COMP | GFP_ATOMIC, |
| 107 | efx->rx_buffer_order); |
| 108 | if (unlikely(page == NULL)) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 109 | return -ENOMEM; |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 110 | dma_addr = dma_map_page(&efx->pci_dev->dev, page, 0, |
Ben Hutchings | 272baee | 2013-01-29 23:33:14 +0000 | [diff] [blame] | 111 | PAGE_SIZE << efx->rx_buffer_order, |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 112 | DMA_FROM_DEVICE); |
| 113 | if (unlikely(dma_mapping_error(&efx->pci_dev->dev, dma_addr))) { |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 114 | __free_pages(page, efx->rx_buffer_order); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 115 | return -EIO; |
| 116 | } |
Ben Hutchings | b8e0251 | 2012-09-06 23:54:15 +0100 | [diff] [blame] | 117 | state = page_address(page); |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 118 | state->refcnt = 0; |
| 119 | state->dma_addr = dma_addr; |
| 120 | |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 121 | dma_addr += sizeof(struct efx_rx_page_state); |
Ben Hutchings | b590ace | 2013-01-10 23:51:54 +0000 | [diff] [blame] | 122 | page_offset = sizeof(struct efx_rx_page_state); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 123 | |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 124 | split: |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 125 | index = rx_queue->added_count & rx_queue->ptr_mask; |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 126 | rx_buf = efx_rx_buffer(rx_queue, index); |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 127 | rx_buf->dma_addr = dma_addr + EFX_PAGE_IP_ALIGN; |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 128 | rx_buf->page = page; |
Ben Hutchings | c73e787 | 2013-03-05 17:49:39 +0000 | [diff] [blame] | 129 | rx_buf->page_offset = page_offset + EFX_PAGE_IP_ALIGN; |
Ben Hutchings | 272baee | 2013-01-29 23:33:14 +0000 | [diff] [blame] | 130 | rx_buf->len = efx->rx_dma_len; |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 131 | rx_buf->flags = 0; |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 132 | ++rx_queue->added_count; |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 133 | ++state->refcnt; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 134 | |
Ben Hutchings | 272baee | 2013-01-29 23:33:14 +0000 | [diff] [blame] | 135 | if ((~count & 1) && (efx->rx_dma_len <= EFX_RX_HALF_PAGE)) { |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 136 | /* Use the second half of the page */ |
| 137 | get_page(page); |
| 138 | dma_addr += (PAGE_SIZE >> 1); |
Ben Hutchings | b590ace | 2013-01-10 23:51:54 +0000 | [diff] [blame] | 139 | page_offset += (PAGE_SIZE >> 1); |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 140 | ++count; |
| 141 | goto split; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 148 | static void efx_unmap_rx_buffer(struct efx_nic *efx, |
Ben Hutchings | 3a68f19 | 2012-12-20 18:48:20 +0000 | [diff] [blame] | 149 | struct efx_rx_buffer *rx_buf, |
| 150 | unsigned int used_len) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 151 | { |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 152 | if (rx_buf->page) { |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 153 | struct efx_rx_page_state *state; |
| 154 | |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 155 | state = page_address(rx_buf->page); |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 156 | if (--state->refcnt == 0) { |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 157 | dma_unmap_page(&efx->pci_dev->dev, |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 158 | state->dma_addr, |
Ben Hutchings | 272baee | 2013-01-29 23:33:14 +0000 | [diff] [blame] | 159 | PAGE_SIZE << efx->rx_buffer_order, |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 160 | DMA_FROM_DEVICE); |
Ben Hutchings | 3a68f19 | 2012-12-20 18:48:20 +0000 | [diff] [blame] | 161 | } else if (used_len) { |
| 162 | dma_sync_single_for_cpu(&efx->pci_dev->dev, |
| 163 | rx_buf->dma_addr, used_len, |
| 164 | DMA_FROM_DEVICE); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 165 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 169 | static void efx_free_rx_buffer(struct efx_nic *efx, |
| 170 | struct efx_rx_buffer *rx_buf) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 171 | { |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 172 | if (rx_buf->page) { |
| 173 | __free_pages(rx_buf->page, efx->rx_buffer_order); |
| 174 | rx_buf->page = NULL; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 178 | static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue, |
| 179 | struct efx_rx_buffer *rx_buf) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 180 | { |
Ben Hutchings | 3a68f19 | 2012-12-20 18:48:20 +0000 | [diff] [blame] | 181 | efx_unmap_rx_buffer(rx_queue->efx, rx_buf, 0); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 182 | efx_free_rx_buffer(rx_queue->efx, rx_buf); |
| 183 | } |
| 184 | |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 185 | /* Attempt to resurrect the other receive buffer that used to share this page, |
| 186 | * which had previously been passed up to the kernel and freed. */ |
| 187 | static void efx_resurrect_rx_buffer(struct efx_rx_queue *rx_queue, |
| 188 | struct efx_rx_buffer *rx_buf) |
| 189 | { |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 190 | struct efx_rx_page_state *state = page_address(rx_buf->page); |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 191 | struct efx_rx_buffer *new_buf; |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 192 | unsigned fill_level, index; |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 193 | |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 194 | /* +1 because efx_rx_packet() incremented removed_count. +1 because |
| 195 | * we'd like to insert an additional descriptor whilst leaving |
| 196 | * EFX_RXD_HEAD_ROOM for the non-recycle path */ |
| 197 | fill_level = (rx_queue->added_count - rx_queue->removed_count + 2); |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 198 | if (unlikely(fill_level > rx_queue->max_fill)) { |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 199 | /* We could place "state" on a list, and drain the list in |
| 200 | * efx_fast_push_rx_descriptors(). For now, this will do. */ |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | ++state->refcnt; |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 205 | get_page(rx_buf->page); |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 206 | |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 207 | index = rx_queue->added_count & rx_queue->ptr_mask; |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 208 | new_buf = efx_rx_buffer(rx_queue, index); |
Steve Hodgson | 62b330b | 2010-06-01 11:20:53 +0000 | [diff] [blame] | 209 | new_buf->dma_addr = rx_buf->dma_addr ^ (PAGE_SIZE >> 1); |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 210 | new_buf->page = rx_buf->page; |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 211 | new_buf->len = rx_buf->len; |
| 212 | ++rx_queue->added_count; |
| 213 | } |
| 214 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 215 | /* Recycle buffers directly back into the rx_queue. There is always |
| 216 | * room to add these buffer, because we've just popped them. |
| 217 | */ |
| 218 | static void efx_recycle_rx_buffers(struct efx_channel *channel, |
| 219 | struct efx_rx_buffer *rx_buf, |
| 220 | unsigned int n_frags) |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 221 | { |
| 222 | struct efx_nic *efx = channel->efx; |
Ben Hutchings | f7d12cd | 2010-09-10 06:41:47 +0000 | [diff] [blame] | 223 | struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel); |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 224 | struct efx_rx_buffer *new_buf; |
| 225 | unsigned index; |
| 226 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 227 | do { |
| 228 | rx_buf->flags = 0; |
Ben Hutchings | db33956 | 2011-08-26 18:05:11 +0100 | [diff] [blame] | 229 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 230 | if (efx->rx_dma_len <= EFX_RX_HALF_PAGE && |
| 231 | page_count(rx_buf->page) == 1) |
| 232 | efx_resurrect_rx_buffer(rx_queue, rx_buf); |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 233 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 234 | index = rx_queue->added_count & rx_queue->ptr_mask; |
| 235 | new_buf = efx_rx_buffer(rx_queue, index); |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 236 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 237 | memcpy(new_buf, rx_buf, sizeof(*new_buf)); |
| 238 | rx_buf->page = NULL; |
| 239 | ++rx_queue->added_count; |
| 240 | |
| 241 | rx_buf = efx_rx_buf_next(rx_queue, rx_buf); |
| 242 | } while (--n_frags); |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 245 | /** |
| 246 | * efx_fast_push_rx_descriptors - push new RX descriptors quickly |
| 247 | * @rx_queue: RX descriptor queue |
Ben Hutchings | 49ce9c2 | 2012-07-10 10:56:00 +0000 | [diff] [blame] | 248 | * |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 249 | * This will aim to fill the RX descriptor queue up to |
David Riddoch | da9ca50 | 2012-04-11 13:09:24 +0100 | [diff] [blame] | 250 | * @rx_queue->@max_fill. If there is insufficient atomic |
Steve Hodgson | 90d683a | 2010-06-01 11:19:39 +0000 | [diff] [blame] | 251 | * memory to do so, a slow fill will be scheduled. |
| 252 | * |
| 253 | * The caller must provide serialisation (none is used here). In practise, |
| 254 | * this means this function must run from the NAPI handler, or be called |
| 255 | * when NAPI is disabled. |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 256 | */ |
Steve Hodgson | 90d683a | 2010-06-01 11:19:39 +0000 | [diff] [blame] | 257 | void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 258 | { |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 259 | unsigned fill_level; |
| 260 | int space, rc = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 261 | |
Steve Hodgson | 90d683a | 2010-06-01 11:19:39 +0000 | [diff] [blame] | 262 | /* Calculate current fill level, and exit if we don't need to fill */ |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 263 | fill_level = (rx_queue->added_count - rx_queue->removed_count); |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 264 | EFX_BUG_ON_PARANOID(fill_level > rx_queue->efx->rxq_entries); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 265 | if (fill_level >= rx_queue->fast_fill_trigger) |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 266 | goto out; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 267 | |
| 268 | /* Record minimum fill level */ |
Ben Hutchings | b347564 | 2008-05-16 21:15:49 +0100 | [diff] [blame] | 269 | if (unlikely(fill_level < rx_queue->min_fill)) { |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 270 | if (fill_level) |
| 271 | rx_queue->min_fill = fill_level; |
Ben Hutchings | b347564 | 2008-05-16 21:15:49 +0100 | [diff] [blame] | 272 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 273 | |
David Riddoch | da9ca50 | 2012-04-11 13:09:24 +0100 | [diff] [blame] | 274 | space = rx_queue->max_fill - fill_level; |
David Riddoch | 6423518 | 2012-04-11 13:12:41 +0100 | [diff] [blame] | 275 | EFX_BUG_ON_PARANOID(space < EFX_RX_BATCH); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 276 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 277 | netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev, |
| 278 | "RX queue %d fast-filling descriptor ring from" |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 279 | " level %d to level %d\n", |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 280 | efx_rx_queue_index(rx_queue), fill_level, |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 281 | rx_queue->max_fill); |
| 282 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 283 | |
| 284 | do { |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 285 | rc = efx_init_rx_buffers(rx_queue); |
Steve Hodgson | f7d6f37 | 2010-06-01 11:33:17 +0000 | [diff] [blame] | 286 | if (unlikely(rc)) { |
| 287 | /* Ensure that we don't leave the rx queue empty */ |
| 288 | if (rx_queue->added_count == rx_queue->removed_count) |
| 289 | efx_schedule_slow_fill(rx_queue); |
| 290 | goto out; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 291 | } |
| 292 | } while ((space -= EFX_RX_BATCH) >= EFX_RX_BATCH); |
| 293 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 294 | netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev, |
| 295 | "RX queue %d fast-filled descriptor ring " |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 296 | "to level %d\n", efx_rx_queue_index(rx_queue), |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 297 | rx_queue->added_count - rx_queue->removed_count); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 298 | |
| 299 | out: |
Steve Hodgson | 2445580 | 2010-06-01 11:20:34 +0000 | [diff] [blame] | 300 | if (rx_queue->notified_count != rx_queue->added_count) |
| 301 | efx_nic_notify_rx_desc(rx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 302 | } |
| 303 | |
Steve Hodgson | 90d683a | 2010-06-01 11:19:39 +0000 | [diff] [blame] | 304 | void efx_rx_slow_fill(unsigned long context) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 305 | { |
Steve Hodgson | 90d683a | 2010-06-01 11:19:39 +0000 | [diff] [blame] | 306 | struct efx_rx_queue *rx_queue = (struct efx_rx_queue *)context; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 307 | |
Steve Hodgson | 90d683a | 2010-06-01 11:19:39 +0000 | [diff] [blame] | 308 | /* Post an event to cause NAPI to run and refill the queue */ |
Ben Hutchings | 2ae75da | 2012-02-07 23:49:52 +0000 | [diff] [blame] | 309 | efx_nic_generate_fill_event(rx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 310 | ++rx_queue->slow_fill_count; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 313 | static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue, |
| 314 | struct efx_rx_buffer *rx_buf, |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 315 | int len) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 316 | { |
| 317 | struct efx_nic *efx = rx_queue->efx; |
| 318 | unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding; |
| 319 | |
| 320 | if (likely(len <= max_len)) |
| 321 | return; |
| 322 | |
| 323 | /* The packet must be discarded, but this is only a fatal error |
| 324 | * if the caller indicated it was |
| 325 | */ |
Ben Hutchings | db33956 | 2011-08-26 18:05:11 +0100 | [diff] [blame] | 326 | rx_buf->flags |= EFX_RX_PKT_DISCARD; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 327 | |
| 328 | if ((len > rx_buf->len) && EFX_WORKAROUND_8071(efx)) { |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 329 | if (net_ratelimit()) |
| 330 | netif_err(efx, rx_err, efx->net_dev, |
| 331 | " RX queue %d seriously overlength " |
| 332 | "RX event (0x%x > 0x%x+0x%x). Leaking\n", |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 333 | efx_rx_queue_index(rx_queue), len, max_len, |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 334 | efx->type->rx_buffer_padding); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 335 | efx_schedule_reset(efx, RESET_TYPE_RX_RECOVERY); |
| 336 | } else { |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 337 | if (net_ratelimit()) |
| 338 | netif_err(efx, rx_err, efx->net_dev, |
| 339 | " RX queue %d overlength RX event " |
| 340 | "(0x%x > 0x%x)\n", |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 341 | efx_rx_queue_index(rx_queue), len, max_len); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 342 | } |
| 343 | |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 344 | efx_rx_queue_channel(rx_queue)->n_rx_overlength++; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 345 | } |
| 346 | |
Ben Hutchings | 61321d9 | 2012-02-25 01:58:35 +0000 | [diff] [blame] | 347 | /* Pass a received packet up through GRO. GRO can handle pages |
| 348 | * regardless of checksum state and skbs with a good checksum. |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 349 | */ |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 350 | static void |
| 351 | efx_rx_packet_gro(struct efx_channel *channel, struct efx_rx_buffer *rx_buf, |
| 352 | unsigned int n_frags, u8 *eh) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 353 | { |
Herbert Xu | da3bc07 | 2009-01-18 21:50:16 -0800 | [diff] [blame] | 354 | struct napi_struct *napi = &channel->napi_str; |
Ben Hutchings | 18e1d2b | 2009-10-29 07:21:24 +0000 | [diff] [blame] | 355 | gro_result_t gro_result; |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 356 | struct efx_nic *efx = channel->efx; |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 357 | struct sk_buff *skb; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 358 | |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 359 | skb = napi_get_frags(napi); |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 360 | if (unlikely(!skb)) { |
| 361 | while (n_frags--) { |
| 362 | put_page(rx_buf->page); |
| 363 | rx_buf->page = NULL; |
| 364 | rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf); |
| 365 | } |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 366 | return; |
| 367 | } |
Ben Hutchings | 1241e95 | 2009-11-23 16:02:25 +0000 | [diff] [blame] | 368 | |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 369 | if (efx->net_dev->features & NETIF_F_RXHASH) |
| 370 | skb->rxhash = efx_rx_buf_hash(eh); |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 371 | skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ? |
| 372 | CHECKSUM_UNNECESSARY : CHECKSUM_NONE); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 373 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 374 | for (;;) { |
| 375 | skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, |
| 376 | rx_buf->page, rx_buf->page_offset, |
| 377 | rx_buf->len); |
| 378 | rx_buf->page = NULL; |
| 379 | skb->len += rx_buf->len; |
| 380 | if (skb_shinfo(skb)->nr_frags == n_frags) |
| 381 | break; |
| 382 | |
| 383 | rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf); |
| 384 | } |
| 385 | |
| 386 | skb->data_len = skb->len; |
| 387 | skb->truesize += n_frags * efx->rx_buffer_truesize; |
| 388 | |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 389 | skb_record_rx_queue(skb, channel->rx_queue.core_index); |
Ben Hutchings | 3eadb7b | 2009-11-23 16:02:40 +0000 | [diff] [blame] | 390 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 391 | gro_result = napi_gro_frags(napi); |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 392 | if (gro_result != GRO_DROP) |
Ben Hutchings | 18e1d2b | 2009-10-29 07:21:24 +0000 | [diff] [blame] | 393 | channel->irq_mod_score += 2; |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 396 | /* Allocate and construct an SKB around page fragments */ |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 397 | static struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel, |
| 398 | struct efx_rx_buffer *rx_buf, |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 399 | unsigned int n_frags, |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 400 | u8 *eh, int hdr_len) |
| 401 | { |
| 402 | struct efx_nic *efx = channel->efx; |
| 403 | struct sk_buff *skb; |
| 404 | |
| 405 | /* Allocate an SKB to store the headers */ |
| 406 | skb = netdev_alloc_skb(efx->net_dev, hdr_len + EFX_PAGE_SKB_ALIGN); |
| 407 | if (unlikely(skb == NULL)) |
| 408 | return NULL; |
| 409 | |
| 410 | EFX_BUG_ON_PARANOID(rx_buf->len < hdr_len); |
| 411 | |
| 412 | skb_reserve(skb, EFX_PAGE_SKB_ALIGN); |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 413 | memcpy(__skb_put(skb, hdr_len), eh, hdr_len); |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 414 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 415 | /* Append the remaining page(s) onto the frag list */ |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 416 | if (rx_buf->len > hdr_len) { |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 417 | rx_buf->page_offset += hdr_len; |
| 418 | rx_buf->len -= hdr_len; |
| 419 | |
| 420 | for (;;) { |
| 421 | skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, |
| 422 | rx_buf->page, rx_buf->page_offset, |
| 423 | rx_buf->len); |
| 424 | rx_buf->page = NULL; |
| 425 | skb->len += rx_buf->len; |
| 426 | skb->data_len += rx_buf->len; |
| 427 | if (skb_shinfo(skb)->nr_frags == n_frags) |
| 428 | break; |
| 429 | |
| 430 | rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf); |
| 431 | } |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 432 | } else { |
| 433 | __free_pages(rx_buf->page, efx->rx_buffer_order); |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 434 | rx_buf->page = NULL; |
| 435 | n_frags = 0; |
Ben Hutchings | 18e1d2b | 2009-10-29 07:21:24 +0000 | [diff] [blame] | 436 | } |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 437 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 438 | skb->truesize += n_frags * efx->rx_buffer_truesize; |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 439 | |
| 440 | /* Move past the ethernet header */ |
| 441 | skb->protocol = eth_type_trans(skb, efx->net_dev); |
| 442 | |
| 443 | return skb; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 446 | void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index, |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 447 | unsigned int n_frags, unsigned int len, u16 flags) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 448 | { |
| 449 | struct efx_nic *efx = rx_queue->efx; |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 450 | struct efx_channel *channel = efx_rx_queue_channel(rx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 451 | struct efx_rx_buffer *rx_buf; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 452 | |
| 453 | rx_buf = efx_rx_buffer(rx_queue, index); |
Ben Hutchings | db33956 | 2011-08-26 18:05:11 +0100 | [diff] [blame] | 454 | rx_buf->flags |= flags; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 455 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 456 | /* Validate the number of fragments and completed length */ |
| 457 | if (n_frags == 1) { |
| 458 | efx_rx_packet__check_len(rx_queue, rx_buf, len); |
| 459 | } else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) || |
| 460 | unlikely(len <= (n_frags - 1) * EFX_RX_USR_BUF_SIZE) || |
| 461 | unlikely(len > n_frags * EFX_RX_USR_BUF_SIZE) || |
| 462 | unlikely(!efx->rx_scatter)) { |
| 463 | /* If this isn't an explicit discard request, either |
| 464 | * the hardware or the driver is broken. |
| 465 | */ |
| 466 | WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD)); |
| 467 | rx_buf->flags |= EFX_RX_PKT_DISCARD; |
| 468 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 469 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 470 | netif_vdbg(efx, rx_status, efx->net_dev, |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 471 | "RX queue %d received ids %x-%x len %d %s%s\n", |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 472 | efx_rx_queue_index(rx_queue), index, |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 473 | (index + n_frags - 1) & rx_queue->ptr_mask, len, |
Ben Hutchings | db33956 | 2011-08-26 18:05:11 +0100 | [diff] [blame] | 474 | (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "", |
| 475 | (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : ""); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 476 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 477 | /* Discard packet, if instructed to do so. Process the |
| 478 | * previous receive first. |
| 479 | */ |
Ben Hutchings | db33956 | 2011-08-26 18:05:11 +0100 | [diff] [blame] | 480 | if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) { |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 481 | efx_rx_flush_packet(channel); |
| 482 | efx_recycle_rx_buffers(channel, rx_buf, n_frags); |
| 483 | return; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 486 | if (n_frags == 1) |
| 487 | rx_buf->len = len; |
| 488 | |
Ben Hutchings | 3a68f19 | 2012-12-20 18:48:20 +0000 | [diff] [blame] | 489 | /* Release and/or sync DMA mapping - assumes all RX buffers |
| 490 | * consumed in-order per RX queue |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 491 | */ |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 492 | efx_unmap_rx_buffer(efx, rx_buf, rx_buf->len); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 493 | |
| 494 | /* Prefetch nice and early so data will (hopefully) be in cache by |
| 495 | * the time we look at it. |
| 496 | */ |
Ben Hutchings | 5036b7c | 2013-01-29 23:33:15 +0000 | [diff] [blame] | 497 | prefetch(efx_rx_buf_va(rx_buf)); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 498 | |
Ben Hutchings | b74e3e8 | 2013-01-29 23:33:15 +0000 | [diff] [blame] | 499 | rx_buf->page_offset += efx->type->rx_buffer_hash_size; |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 500 | rx_buf->len -= efx->type->rx_buffer_hash_size; |
| 501 | |
| 502 | if (n_frags > 1) { |
| 503 | /* Release/sync DMA mapping for additional fragments. |
| 504 | * Fix length for last fragment. |
| 505 | */ |
| 506 | unsigned int tail_frags = n_frags - 1; |
| 507 | |
| 508 | for (;;) { |
| 509 | rx_buf = efx_rx_buf_next(rx_queue, rx_buf); |
| 510 | if (--tail_frags == 0) |
| 511 | break; |
| 512 | efx_unmap_rx_buffer(efx, rx_buf, EFX_RX_USR_BUF_SIZE); |
| 513 | } |
| 514 | rx_buf->len = len - (n_frags - 1) * EFX_RX_USR_BUF_SIZE; |
| 515 | efx_unmap_rx_buffer(efx, rx_buf, rx_buf->len); |
| 516 | } |
Ben Hutchings | b74e3e8 | 2013-01-29 23:33:15 +0000 | [diff] [blame] | 517 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 518 | /* Pipeline receives so that we give time for packet headers to be |
| 519 | * prefetched into cache. |
| 520 | */ |
Ben Hutchings | ff734ef | 2013-01-29 23:33:14 +0000 | [diff] [blame] | 521 | efx_rx_flush_packet(channel); |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 522 | channel->rx_pkt_n_frags = n_frags; |
| 523 | channel->rx_pkt_index = index; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 526 | static void efx_rx_deliver(struct efx_channel *channel, u8 *eh, |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 527 | struct efx_rx_buffer *rx_buf, |
| 528 | unsigned int n_frags) |
Ben Hutchings | 1ddceb4 | 2012-01-23 22:41:30 +0000 | [diff] [blame] | 529 | { |
| 530 | struct sk_buff *skb; |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 531 | u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS); |
Ben Hutchings | 1ddceb4 | 2012-01-23 22:41:30 +0000 | [diff] [blame] | 532 | |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 533 | skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len); |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 534 | if (unlikely(skb == NULL)) { |
| 535 | efx_free_rx_buffer(channel->efx, rx_buf); |
| 536 | return; |
| 537 | } |
| 538 | skb_record_rx_queue(skb, channel->rx_queue.core_index); |
Ben Hutchings | 1ddceb4 | 2012-01-23 22:41:30 +0000 | [diff] [blame] | 539 | |
| 540 | /* Set the SKB flags */ |
| 541 | skb_checksum_none_assert(skb); |
| 542 | |
Stuart Hodgson | c31e5f9 | 2012-07-18 09:52:11 +0100 | [diff] [blame] | 543 | if (channel->type->receive_skb) |
Ben Hutchings | 4a74dc65 | 2013-03-05 20:13:54 +0000 | [diff] [blame] | 544 | if (channel->type->receive_skb(channel, skb)) |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 545 | return; |
Ben Hutchings | 1ddceb4 | 2012-01-23 22:41:30 +0000 | [diff] [blame] | 546 | |
Ben Hutchings | 4a74dc65 | 2013-03-05 20:13:54 +0000 | [diff] [blame] | 547 | /* Pass the packet up */ |
| 548 | netif_receive_skb(skb); |
Ben Hutchings | 1ddceb4 | 2012-01-23 22:41:30 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 551 | /* Handle a received packet. Second half: Touches packet payload. */ |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 552 | void __efx_rx_packet(struct efx_channel *channel) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 553 | { |
| 554 | struct efx_nic *efx = channel->efx; |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 555 | struct efx_rx_buffer *rx_buf = |
| 556 | efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index); |
Ben Hutchings | b74e3e8 | 2013-01-29 23:33:15 +0000 | [diff] [blame] | 557 | u8 *eh = efx_rx_buf_va(rx_buf); |
Ben Hutchings | 604f604 | 2010-06-25 07:05:33 +0000 | [diff] [blame] | 558 | |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 559 | /* If we're in loopback test, then pass the packet directly to the |
| 560 | * loopback layer, and free the rx_buf here |
| 561 | */ |
| 562 | if (unlikely(efx->loopback_selftest)) { |
Steve Hodgson | a526f14 | 2011-02-24 23:45:16 +0000 | [diff] [blame] | 563 | efx_loopback_rx_packet(efx, eh, rx_buf->len); |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 564 | efx_free_rx_buffer(efx, rx_buf); |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 565 | goto out; |
Ben Hutchings | 3273c2e | 2008-05-07 13:36:19 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Ben Hutchings | abfe903 | 2011-04-05 15:00:02 +0100 | [diff] [blame] | 568 | if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM))) |
Ben Hutchings | db33956 | 2011-08-26 18:05:11 +0100 | [diff] [blame] | 569 | rx_buf->flags &= ~EFX_RX_PKT_CSUMMED; |
Ben Hutchings | ab3cf6d | 2011-04-01 22:20:06 +0100 | [diff] [blame] | 570 | |
Alexandre Rames | 97d48a1 | 2013-01-11 12:26:21 +0000 | [diff] [blame] | 571 | if (!channel->type->receive_skb) |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 572 | efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh); |
Ben Hutchings | 1ddceb4 | 2012-01-23 22:41:30 +0000 | [diff] [blame] | 573 | else |
Ben Hutchings | 85740cdf | 2013-01-29 23:33:15 +0000 | [diff] [blame^] | 574 | efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags); |
| 575 | out: |
| 576 | channel->rx_pkt_n_frags = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | int efx_probe_rx_queue(struct efx_rx_queue *rx_queue) |
| 580 | { |
| 581 | struct efx_nic *efx = rx_queue->efx; |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 582 | unsigned int entries; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 583 | int rc; |
| 584 | |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 585 | /* Create the smallest power-of-two aligned ring */ |
| 586 | entries = max(roundup_pow_of_two(efx->rxq_entries), EFX_MIN_DMAQ_SIZE); |
| 587 | EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE); |
| 588 | rx_queue->ptr_mask = entries - 1; |
| 589 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 590 | netif_dbg(efx, probe, efx->net_dev, |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 591 | "creating RX queue %d size %#x mask %#x\n", |
| 592 | efx_rx_queue_index(rx_queue), efx->rxq_entries, |
| 593 | rx_queue->ptr_mask); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 594 | |
| 595 | /* Allocate RX buffers */ |
Thomas Meyer | c2e4e25 | 2011-12-02 12:36:13 +0000 | [diff] [blame] | 596 | rx_queue->buffer = kcalloc(entries, sizeof(*rx_queue->buffer), |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 597 | GFP_KERNEL); |
Ben Hutchings | 8831da7 | 2008-09-01 12:47:48 +0100 | [diff] [blame] | 598 | if (!rx_queue->buffer) |
| 599 | return -ENOMEM; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 600 | |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 601 | rc = efx_nic_probe_rx(rx_queue); |
Ben Hutchings | 8831da7 | 2008-09-01 12:47:48 +0100 | [diff] [blame] | 602 | if (rc) { |
| 603 | kfree(rx_queue->buffer); |
| 604 | rx_queue->buffer = NULL; |
| 605 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 606 | return rc; |
| 607 | } |
| 608 | |
Ben Hutchings | bc3c90a | 2008-09-01 12:48:46 +0100 | [diff] [blame] | 609 | void efx_init_rx_queue(struct efx_rx_queue *rx_queue) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 610 | { |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 611 | struct efx_nic *efx = rx_queue->efx; |
David Riddoch | 6423518 | 2012-04-11 13:12:41 +0100 | [diff] [blame] | 612 | unsigned int max_fill, trigger, max_trigger; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 613 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 614 | netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev, |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 615 | "initialising RX queue %d\n", efx_rx_queue_index(rx_queue)); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 616 | |
| 617 | /* Initialise ptr fields */ |
| 618 | rx_queue->added_count = 0; |
| 619 | rx_queue->notified_count = 0; |
| 620 | rx_queue->removed_count = 0; |
| 621 | rx_queue->min_fill = -1U; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 622 | |
| 623 | /* Initialise limit fields */ |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 624 | max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM; |
David Riddoch | 6423518 | 2012-04-11 13:12:41 +0100 | [diff] [blame] | 625 | max_trigger = max_fill - EFX_RX_BATCH; |
| 626 | if (rx_refill_threshold != 0) { |
| 627 | trigger = max_fill * min(rx_refill_threshold, 100U) / 100U; |
| 628 | if (trigger > max_trigger) |
| 629 | trigger = max_trigger; |
| 630 | } else { |
| 631 | trigger = max_trigger; |
| 632 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 633 | |
| 634 | rx_queue->max_fill = max_fill; |
| 635 | rx_queue->fast_fill_trigger = trigger; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 636 | |
| 637 | /* Set up RX descriptor ring */ |
Ben Hutchings | 9f2cb71 | 2012-02-08 00:11:20 +0000 | [diff] [blame] | 638 | rx_queue->enabled = true; |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 639 | efx_nic_init_rx(rx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | void efx_fini_rx_queue(struct efx_rx_queue *rx_queue) |
| 643 | { |
| 644 | int i; |
| 645 | struct efx_rx_buffer *rx_buf; |
| 646 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 647 | netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev, |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 648 | "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue)); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 649 | |
Ben Hutchings | 9f2cb71 | 2012-02-08 00:11:20 +0000 | [diff] [blame] | 650 | /* A flush failure might have left rx_queue->enabled */ |
| 651 | rx_queue->enabled = false; |
| 652 | |
Steve Hodgson | 90d683a | 2010-06-01 11:19:39 +0000 | [diff] [blame] | 653 | del_timer_sync(&rx_queue->slow_fill); |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 654 | efx_nic_fini_rx(rx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 655 | |
| 656 | /* Release RX buffers NB start at index 0 not current HW ptr */ |
| 657 | if (rx_queue->buffer) { |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 658 | for (i = 0; i <= rx_queue->ptr_mask; i++) { |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 659 | rx_buf = efx_rx_buffer(rx_queue, i); |
| 660 | efx_fini_rx_buffer(rx_queue, rx_buf); |
| 661 | } |
| 662 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | void efx_remove_rx_queue(struct efx_rx_queue *rx_queue) |
| 666 | { |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 667 | netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev, |
Ben Hutchings | ba1e8a3 | 2010-09-10 06:41:36 +0000 | [diff] [blame] | 668 | "destroying RX queue %d\n", efx_rx_queue_index(rx_queue)); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 669 | |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 670 | efx_nic_remove_rx(rx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 671 | |
| 672 | kfree(rx_queue->buffer); |
| 673 | rx_queue->buffer = NULL; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 674 | } |
| 675 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 676 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 677 | module_param(rx_refill_threshold, uint, 0444); |
| 678 | MODULE_PARM_DESC(rx_refill_threshold, |
David Riddoch | 6423518 | 2012-04-11 13:12:41 +0100 | [diff] [blame] | 679 | "RX descriptor ring refill threshold (%)"); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 680 | |