Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / net / ethernet / sfc / rx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2005-2006 Fen Systems Ltd.
5  * Copyright 2005-2013 Solarflare Communications Inc.
6  */
7
8 #include <linux/socket.h>
9 #include <linux/in.h>
10 #include <linux/slab.h>
11 #include <linux/ip.h>
12 #include <linux/ipv6.h>
13 #include <linux/tcp.h>
14 #include <linux/udp.h>
15 #include <linux/prefetch.h>
16 #include <linux/moduleparam.h>
17 #include <linux/iommu.h>
18 #include <net/ip.h>
19 #include <net/checksum.h>
20 #include "net_driver.h"
21 #include "efx.h"
22 #include "filter.h"
23 #include "nic.h"
24 #include "selftest.h"
25 #include "workarounds.h"
26
27 /* Preferred number of descriptors to fill at once */
28 #define EFX_RX_PREFERRED_BATCH 8U
29
30 /* Number of RX buffers to recycle pages for.  When creating the RX page recycle
31  * ring, this number is divided by the number of buffers per page to calculate
32  * the number of pages to store in the RX page recycle ring.
33  */
34 #define EFX_RECYCLE_RING_SIZE_IOMMU 4096
35 #define EFX_RECYCLE_RING_SIZE_NOIOMMU (2 * EFX_RX_PREFERRED_BATCH)
36
37 /* Size of buffer allocated for skb header area. */
38 #define EFX_SKB_HEADERS  128u
39
40 /* This is the percentage fill level below which new RX descriptors
41  * will be added to the RX descriptor ring.
42  */
43 static unsigned int rx_refill_threshold;
44
45 /* Each packet can consume up to ceil(max_frame_len / buffer_size) buffers */
46 #define EFX_RX_MAX_FRAGS DIV_ROUND_UP(EFX_MAX_FRAME_LEN(EFX_MAX_MTU), \
47                                       EFX_RX_USR_BUF_SIZE)
48
49 /*
50  * RX maximum head room required.
51  *
52  * This must be at least 1 to prevent overflow, plus one packet-worth
53  * to allow pipelined receives.
54  */
55 #define EFX_RXD_HEAD_ROOM (1 + EFX_RX_MAX_FRAGS)
56
57 static inline u8 *efx_rx_buf_va(struct efx_rx_buffer *buf)
58 {
59         return page_address(buf->page) + buf->page_offset;
60 }
61
62 static inline u32 efx_rx_buf_hash(struct efx_nic *efx, const u8 *eh)
63 {
64 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
65         return __le32_to_cpup((const __le32 *)(eh + efx->rx_packet_hash_offset));
66 #else
67         const u8 *data = eh + efx->rx_packet_hash_offset;
68         return (u32)data[0]       |
69                (u32)data[1] << 8  |
70                (u32)data[2] << 16 |
71                (u32)data[3] << 24;
72 #endif
73 }
74
75 static inline struct efx_rx_buffer *
76 efx_rx_buf_next(struct efx_rx_queue *rx_queue, struct efx_rx_buffer *rx_buf)
77 {
78         if (unlikely(rx_buf == efx_rx_buffer(rx_queue, rx_queue->ptr_mask)))
79                 return efx_rx_buffer(rx_queue, 0);
80         else
81                 return rx_buf + 1;
82 }
83
84 static inline void efx_sync_rx_buffer(struct efx_nic *efx,
85                                       struct efx_rx_buffer *rx_buf,
86                                       unsigned int len)
87 {
88         dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr, len,
89                                 DMA_FROM_DEVICE);
90 }
91
92 void efx_rx_config_page_split(struct efx_nic *efx)
93 {
94         efx->rx_page_buf_step = ALIGN(efx->rx_dma_len + efx->rx_ip_align,
95                                       EFX_RX_BUF_ALIGNMENT);
96         efx->rx_bufs_per_page = efx->rx_buffer_order ? 1 :
97                 ((PAGE_SIZE - sizeof(struct efx_rx_page_state)) /
98                  efx->rx_page_buf_step);
99         efx->rx_buffer_truesize = (PAGE_SIZE << efx->rx_buffer_order) /
100                 efx->rx_bufs_per_page;
101         efx->rx_pages_per_batch = DIV_ROUND_UP(EFX_RX_PREFERRED_BATCH,
102                                                efx->rx_bufs_per_page);
103 }
104
105 /* Check the RX page recycle ring for a page that can be reused. */
106 static struct page *efx_reuse_page(struct efx_rx_queue *rx_queue)
107 {
108         struct efx_nic *efx = rx_queue->efx;
109         struct page *page;
110         struct efx_rx_page_state *state;
111         unsigned index;
112
113         index = rx_queue->page_remove & rx_queue->page_ptr_mask;
114         page = rx_queue->page_ring[index];
115         if (page == NULL)
116                 return NULL;
117
118         rx_queue->page_ring[index] = NULL;
119         /* page_remove cannot exceed page_add. */
120         if (rx_queue->page_remove != rx_queue->page_add)
121                 ++rx_queue->page_remove;
122
123         /* If page_count is 1 then we hold the only reference to this page. */
124         if (page_count(page) == 1) {
125                 ++rx_queue->page_recycle_count;
126                 return page;
127         } else {
128                 state = page_address(page);
129                 dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
130                                PAGE_SIZE << efx->rx_buffer_order,
131                                DMA_FROM_DEVICE);
132                 put_page(page);
133                 ++rx_queue->page_recycle_failed;
134         }
135
136         return NULL;
137 }
138
139 /**
140  * efx_init_rx_buffers - create EFX_RX_BATCH page-based RX buffers
141  *
142  * @rx_queue:           Efx RX queue
143  *
144  * This allocates a batch of pages, maps them for DMA, and populates
145  * struct efx_rx_buffers for each one. Return a negative error code or
146  * 0 on success. If a single page can be used for multiple buffers,
147  * then the page will either be inserted fully, or not at all.
148  */
149 static int efx_init_rx_buffers(struct efx_rx_queue *rx_queue, bool atomic)
150 {
151         struct efx_nic *efx = rx_queue->efx;
152         struct efx_rx_buffer *rx_buf;
153         struct page *page;
154         unsigned int page_offset;
155         struct efx_rx_page_state *state;
156         dma_addr_t dma_addr;
157         unsigned index, count;
158
159         count = 0;
160         do {
161                 page = efx_reuse_page(rx_queue);
162                 if (page == NULL) {
163                         page = alloc_pages(__GFP_COMP |
164                                            (atomic ? GFP_ATOMIC : GFP_KERNEL),
165                                            efx->rx_buffer_order);
166                         if (unlikely(page == NULL))
167                                 return -ENOMEM;
168                         dma_addr =
169                                 dma_map_page(&efx->pci_dev->dev, page, 0,
170                                              PAGE_SIZE << efx->rx_buffer_order,
171                                              DMA_FROM_DEVICE);
172                         if (unlikely(dma_mapping_error(&efx->pci_dev->dev,
173                                                        dma_addr))) {
174                                 __free_pages(page, efx->rx_buffer_order);
175                                 return -EIO;
176                         }
177                         state = page_address(page);
178                         state->dma_addr = dma_addr;
179                 } else {
180                         state = page_address(page);
181                         dma_addr = state->dma_addr;
182                 }
183
184                 dma_addr += sizeof(struct efx_rx_page_state);
185                 page_offset = sizeof(struct efx_rx_page_state);
186
187                 do {
188                         index = rx_queue->added_count & rx_queue->ptr_mask;
189                         rx_buf = efx_rx_buffer(rx_queue, index);
190                         rx_buf->dma_addr = dma_addr + efx->rx_ip_align;
191                         rx_buf->page = page;
192                         rx_buf->page_offset = page_offset + efx->rx_ip_align;
193                         rx_buf->len = efx->rx_dma_len;
194                         rx_buf->flags = 0;
195                         ++rx_queue->added_count;
196                         get_page(page);
197                         dma_addr += efx->rx_page_buf_step;
198                         page_offset += efx->rx_page_buf_step;
199                 } while (page_offset + efx->rx_page_buf_step <= PAGE_SIZE);
200
201                 rx_buf->flags = EFX_RX_BUF_LAST_IN_PAGE;
202         } while (++count < efx->rx_pages_per_batch);
203
204         return 0;
205 }
206
207 /* Unmap a DMA-mapped page.  This function is only called for the final RX
208  * buffer in a page.
209  */
210 static void efx_unmap_rx_buffer(struct efx_nic *efx,
211                                 struct efx_rx_buffer *rx_buf)
212 {
213         struct page *page = rx_buf->page;
214
215         if (page) {
216                 struct efx_rx_page_state *state = page_address(page);
217                 dma_unmap_page(&efx->pci_dev->dev,
218                                state->dma_addr,
219                                PAGE_SIZE << efx->rx_buffer_order,
220                                DMA_FROM_DEVICE);
221         }
222 }
223
224 static void efx_free_rx_buffers(struct efx_rx_queue *rx_queue,
225                                 struct efx_rx_buffer *rx_buf,
226                                 unsigned int num_bufs)
227 {
228         do {
229                 if (rx_buf->page) {
230                         put_page(rx_buf->page);
231                         rx_buf->page = NULL;
232                 }
233                 rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
234         } while (--num_bufs);
235 }
236
237 /* Attempt to recycle the page if there is an RX recycle ring; the page can
238  * only be added if this is the final RX buffer, to prevent pages being used in
239  * the descriptor ring and appearing in the recycle ring simultaneously.
240  */
241 static void efx_recycle_rx_page(struct efx_channel *channel,
242                                 struct efx_rx_buffer *rx_buf)
243 {
244         struct page *page = rx_buf->page;
245         struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
246         struct efx_nic *efx = rx_queue->efx;
247         unsigned index;
248
249         /* Only recycle the page after processing the final buffer. */
250         if (!(rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE))
251                 return;
252
253         index = rx_queue->page_add & rx_queue->page_ptr_mask;
254         if (rx_queue->page_ring[index] == NULL) {
255                 unsigned read_index = rx_queue->page_remove &
256                         rx_queue->page_ptr_mask;
257
258                 /* The next slot in the recycle ring is available, but
259                  * increment page_remove if the read pointer currently
260                  * points here.
261                  */
262                 if (read_index == index)
263                         ++rx_queue->page_remove;
264                 rx_queue->page_ring[index] = page;
265                 ++rx_queue->page_add;
266                 return;
267         }
268         ++rx_queue->page_recycle_full;
269         efx_unmap_rx_buffer(efx, rx_buf);
270         put_page(rx_buf->page);
271 }
272
273 static void efx_fini_rx_buffer(struct efx_rx_queue *rx_queue,
274                                struct efx_rx_buffer *rx_buf)
275 {
276         /* Release the page reference we hold for the buffer. */
277         if (rx_buf->page)
278                 put_page(rx_buf->page);
279
280         /* If this is the last buffer in a page, unmap and free it. */
281         if (rx_buf->flags & EFX_RX_BUF_LAST_IN_PAGE) {
282                 efx_unmap_rx_buffer(rx_queue->efx, rx_buf);
283                 efx_free_rx_buffers(rx_queue, rx_buf, 1);
284         }
285         rx_buf->page = NULL;
286 }
287
288 /* Recycle the pages that are used by buffers that have just been received. */
289 static void efx_recycle_rx_pages(struct efx_channel *channel,
290                                  struct efx_rx_buffer *rx_buf,
291                                  unsigned int n_frags)
292 {
293         struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
294
295         do {
296                 efx_recycle_rx_page(channel, rx_buf);
297                 rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
298         } while (--n_frags);
299 }
300
301 static void efx_discard_rx_packet(struct efx_channel *channel,
302                                   struct efx_rx_buffer *rx_buf,
303                                   unsigned int n_frags)
304 {
305         struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
306
307         efx_recycle_rx_pages(channel, rx_buf, n_frags);
308
309         efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
310 }
311
312 /**
313  * efx_fast_push_rx_descriptors - push new RX descriptors quickly
314  * @rx_queue:           RX descriptor queue
315  *
316  * This will aim to fill the RX descriptor queue up to
317  * @rx_queue->@max_fill. If there is insufficient atomic
318  * memory to do so, a slow fill will be scheduled.
319  *
320  * The caller must provide serialisation (none is used here). In practise,
321  * this means this function must run from the NAPI handler, or be called
322  * when NAPI is disabled.
323  */
324 void efx_fast_push_rx_descriptors(struct efx_rx_queue *rx_queue, bool atomic)
325 {
326         struct efx_nic *efx = rx_queue->efx;
327         unsigned int fill_level, batch_size;
328         int space, rc = 0;
329
330         if (!rx_queue->refill_enabled)
331                 return;
332
333         /* Calculate current fill level, and exit if we don't need to fill */
334         fill_level = (rx_queue->added_count - rx_queue->removed_count);
335         EFX_WARN_ON_ONCE_PARANOID(fill_level > rx_queue->efx->rxq_entries);
336         if (fill_level >= rx_queue->fast_fill_trigger)
337                 goto out;
338
339         /* Record minimum fill level */
340         if (unlikely(fill_level < rx_queue->min_fill)) {
341                 if (fill_level)
342                         rx_queue->min_fill = fill_level;
343         }
344
345         batch_size = efx->rx_pages_per_batch * efx->rx_bufs_per_page;
346         space = rx_queue->max_fill - fill_level;
347         EFX_WARN_ON_ONCE_PARANOID(space < batch_size);
348
349         netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
350                    "RX queue %d fast-filling descriptor ring from"
351                    " level %d to level %d\n",
352                    efx_rx_queue_index(rx_queue), fill_level,
353                    rx_queue->max_fill);
354
355
356         do {
357                 rc = efx_init_rx_buffers(rx_queue, atomic);
358                 if (unlikely(rc)) {
359                         /* Ensure that we don't leave the rx queue empty */
360                         efx_schedule_slow_fill(rx_queue);
361                         goto out;
362                 }
363         } while ((space -= batch_size) >= batch_size);
364
365         netif_vdbg(rx_queue->efx, rx_status, rx_queue->efx->net_dev,
366                    "RX queue %d fast-filled descriptor ring "
367                    "to level %d\n", efx_rx_queue_index(rx_queue),
368                    rx_queue->added_count - rx_queue->removed_count);
369
370  out:
371         if (rx_queue->notified_count != rx_queue->added_count)
372                 efx_nic_notify_rx_desc(rx_queue);
373 }
374
375 void efx_rx_slow_fill(struct timer_list *t)
376 {
377         struct efx_rx_queue *rx_queue = from_timer(rx_queue, t, slow_fill);
378
379         /* Post an event to cause NAPI to run and refill the queue */
380         efx_nic_generate_fill_event(rx_queue);
381         ++rx_queue->slow_fill_count;
382 }
383
384 static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
385                                      struct efx_rx_buffer *rx_buf,
386                                      int len)
387 {
388         struct efx_nic *efx = rx_queue->efx;
389         unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
390
391         if (likely(len <= max_len))
392                 return;
393
394         /* The packet must be discarded, but this is only a fatal error
395          * if the caller indicated it was
396          */
397         rx_buf->flags |= EFX_RX_PKT_DISCARD;
398
399         if (net_ratelimit())
400                 netif_err(efx, rx_err, efx->net_dev,
401                           "RX queue %d overlength RX event (%#x > %#x)\n",
402                           efx_rx_queue_index(rx_queue), len, max_len);
403
404         efx_rx_queue_channel(rx_queue)->n_rx_overlength++;
405 }
406
407 /* Pass a received packet up through GRO.  GRO can handle pages
408  * regardless of checksum state and skbs with a good checksum.
409  */
410 static void
411 efx_rx_packet_gro(struct efx_channel *channel, struct efx_rx_buffer *rx_buf,
412                   unsigned int n_frags, u8 *eh)
413 {
414         struct napi_struct *napi = &channel->napi_str;
415         gro_result_t gro_result;
416         struct efx_nic *efx = channel->efx;
417         struct sk_buff *skb;
418
419         skb = napi_get_frags(napi);
420         if (unlikely(!skb)) {
421                 struct efx_rx_queue *rx_queue;
422
423                 rx_queue = efx_channel_get_rx_queue(channel);
424                 efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
425                 return;
426         }
427
428         if (efx->net_dev->features & NETIF_F_RXHASH)
429                 skb_set_hash(skb, efx_rx_buf_hash(efx, eh),
430                              PKT_HASH_TYPE_L3);
431         skb->ip_summed = ((rx_buf->flags & EFX_RX_PKT_CSUMMED) ?
432                           CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
433         skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
434
435         for (;;) {
436                 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
437                                    rx_buf->page, rx_buf->page_offset,
438                                    rx_buf->len);
439                 rx_buf->page = NULL;
440                 skb->len += rx_buf->len;
441                 if (skb_shinfo(skb)->nr_frags == n_frags)
442                         break;
443
444                 rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
445         }
446
447         skb->data_len = skb->len;
448         skb->truesize += n_frags * efx->rx_buffer_truesize;
449
450         skb_record_rx_queue(skb, channel->rx_queue.core_index);
451
452         gro_result = napi_gro_frags(napi);
453         if (gro_result != GRO_DROP)
454                 channel->irq_mod_score += 2;
455 }
456
457 /* Allocate and construct an SKB around page fragments */
458 static struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel,
459                                      struct efx_rx_buffer *rx_buf,
460                                      unsigned int n_frags,
461                                      u8 *eh, int hdr_len)
462 {
463         struct efx_nic *efx = channel->efx;
464         struct sk_buff *skb;
465
466         /* Allocate an SKB to store the headers */
467         skb = netdev_alloc_skb(efx->net_dev,
468                                efx->rx_ip_align + efx->rx_prefix_size +
469                                hdr_len);
470         if (unlikely(skb == NULL)) {
471                 atomic_inc(&efx->n_rx_noskb_drops);
472                 return NULL;
473         }
474
475         EFX_WARN_ON_ONCE_PARANOID(rx_buf->len < hdr_len);
476
477         memcpy(skb->data + efx->rx_ip_align, eh - efx->rx_prefix_size,
478                efx->rx_prefix_size + hdr_len);
479         skb_reserve(skb, efx->rx_ip_align + efx->rx_prefix_size);
480         __skb_put(skb, hdr_len);
481
482         /* Append the remaining page(s) onto the frag list */
483         if (rx_buf->len > hdr_len) {
484                 rx_buf->page_offset += hdr_len;
485                 rx_buf->len -= hdr_len;
486
487                 for (;;) {
488                         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
489                                            rx_buf->page, rx_buf->page_offset,
490                                            rx_buf->len);
491                         rx_buf->page = NULL;
492                         skb->len += rx_buf->len;
493                         skb->data_len += rx_buf->len;
494                         if (skb_shinfo(skb)->nr_frags == n_frags)
495                                 break;
496
497                         rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
498                 }
499         } else {
500                 __free_pages(rx_buf->page, efx->rx_buffer_order);
501                 rx_buf->page = NULL;
502                 n_frags = 0;
503         }
504
505         skb->truesize += n_frags * efx->rx_buffer_truesize;
506
507         /* Move past the ethernet header */
508         skb->protocol = eth_type_trans(skb, efx->net_dev);
509
510         skb_mark_napi_id(skb, &channel->napi_str);
511
512         return skb;
513 }
514
515 void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
516                    unsigned int n_frags, unsigned int len, u16 flags)
517 {
518         struct efx_nic *efx = rx_queue->efx;
519         struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
520         struct efx_rx_buffer *rx_buf;
521
522         rx_queue->rx_packets++;
523
524         rx_buf = efx_rx_buffer(rx_queue, index);
525         rx_buf->flags |= flags;
526
527         /* Validate the number of fragments and completed length */
528         if (n_frags == 1) {
529                 if (!(flags & EFX_RX_PKT_PREFIX_LEN))
530                         efx_rx_packet__check_len(rx_queue, rx_buf, len);
531         } else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) ||
532                    unlikely(len <= (n_frags - 1) * efx->rx_dma_len) ||
533                    unlikely(len > n_frags * efx->rx_dma_len) ||
534                    unlikely(!efx->rx_scatter)) {
535                 /* If this isn't an explicit discard request, either
536                  * the hardware or the driver is broken.
537                  */
538                 WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD));
539                 rx_buf->flags |= EFX_RX_PKT_DISCARD;
540         }
541
542         netif_vdbg(efx, rx_status, efx->net_dev,
543                    "RX queue %d received ids %x-%x len %d %s%s\n",
544                    efx_rx_queue_index(rx_queue), index,
545                    (index + n_frags - 1) & rx_queue->ptr_mask, len,
546                    (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "",
547                    (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : "");
548
549         /* Discard packet, if instructed to do so.  Process the
550          * previous receive first.
551          */
552         if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) {
553                 efx_rx_flush_packet(channel);
554                 efx_discard_rx_packet(channel, rx_buf, n_frags);
555                 return;
556         }
557
558         if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN))
559                 rx_buf->len = len;
560
561         /* Release and/or sync the DMA mapping - assumes all RX buffers
562          * consumed in-order per RX queue.
563          */
564         efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
565
566         /* Prefetch nice and early so data will (hopefully) be in cache by
567          * the time we look at it.
568          */
569         prefetch(efx_rx_buf_va(rx_buf));
570
571         rx_buf->page_offset += efx->rx_prefix_size;
572         rx_buf->len -= efx->rx_prefix_size;
573
574         if (n_frags > 1) {
575                 /* Release/sync DMA mapping for additional fragments.
576                  * Fix length for last fragment.
577                  */
578                 unsigned int tail_frags = n_frags - 1;
579
580                 for (;;) {
581                         rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
582                         if (--tail_frags == 0)
583                                 break;
584                         efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len);
585                 }
586                 rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len;
587                 efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
588         }
589
590         /* All fragments have been DMA-synced, so recycle pages. */
591         rx_buf = efx_rx_buffer(rx_queue, index);
592         efx_recycle_rx_pages(channel, rx_buf, n_frags);
593
594         /* Pipeline receives so that we give time for packet headers to be
595          * prefetched into cache.
596          */
597         efx_rx_flush_packet(channel);
598         channel->rx_pkt_n_frags = n_frags;
599         channel->rx_pkt_index = index;
600 }
601
602 static void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
603                            struct efx_rx_buffer *rx_buf,
604                            unsigned int n_frags)
605 {
606         struct sk_buff *skb;
607         u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS);
608
609         skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len);
610         if (unlikely(skb == NULL)) {
611                 struct efx_rx_queue *rx_queue;
612
613                 rx_queue = efx_channel_get_rx_queue(channel);
614                 efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
615                 return;
616         }
617         skb_record_rx_queue(skb, channel->rx_queue.core_index);
618
619         /* Set the SKB flags */
620         skb_checksum_none_assert(skb);
621         if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED)) {
622                 skb->ip_summed = CHECKSUM_UNNECESSARY;
623                 skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
624         }
625
626         efx_rx_skb_attach_timestamp(channel, skb);
627
628         if (channel->type->receive_skb)
629                 if (channel->type->receive_skb(channel, skb))
630                         return;
631
632         /* Pass the packet up */
633         if (channel->rx_list != NULL)
634                 /* Add to list, will pass up later */
635                 list_add_tail(&skb->list, channel->rx_list);
636         else
637                 /* No list, so pass it up now */
638                 netif_receive_skb(skb);
639 }
640
641 /* Handle a received packet.  Second half: Touches packet payload. */
642 void __efx_rx_packet(struct efx_channel *channel)
643 {
644         struct efx_nic *efx = channel->efx;
645         struct efx_rx_buffer *rx_buf =
646                 efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index);
647         u8 *eh = efx_rx_buf_va(rx_buf);
648
649         /* Read length from the prefix if necessary.  This already
650          * excludes the length of the prefix itself.
651          */
652         if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN)
653                 rx_buf->len = le16_to_cpup((__le16 *)
654                                            (eh + efx->rx_packet_len_offset));
655
656         /* If we're in loopback test, then pass the packet directly to the
657          * loopback layer, and free the rx_buf here
658          */
659         if (unlikely(efx->loopback_selftest)) {
660                 struct efx_rx_queue *rx_queue;
661
662                 efx_loopback_rx_packet(efx, eh, rx_buf->len);
663                 rx_queue = efx_channel_get_rx_queue(channel);
664                 efx_free_rx_buffers(rx_queue, rx_buf,
665                                     channel->rx_pkt_n_frags);
666                 goto out;
667         }
668
669         if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
670                 rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
671
672         if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb)
673                 efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh);
674         else
675                 efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags);
676 out:
677         channel->rx_pkt_n_frags = 0;
678 }
679
680 int efx_probe_rx_queue(struct efx_rx_queue *rx_queue)
681 {
682         struct efx_nic *efx = rx_queue->efx;
683         unsigned int entries;
684         int rc;
685
686         /* Create the smallest power-of-two aligned ring */
687         entries = max(roundup_pow_of_two(efx->rxq_entries), EFX_MIN_DMAQ_SIZE);
688         EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
689         rx_queue->ptr_mask = entries - 1;
690
691         netif_dbg(efx, probe, efx->net_dev,
692                   "creating RX queue %d size %#x mask %#x\n",
693                   efx_rx_queue_index(rx_queue), efx->rxq_entries,
694                   rx_queue->ptr_mask);
695
696         /* Allocate RX buffers */
697         rx_queue->buffer = kcalloc(entries, sizeof(*rx_queue->buffer),
698                                    GFP_KERNEL);
699         if (!rx_queue->buffer)
700                 return -ENOMEM;
701
702         rc = efx_nic_probe_rx(rx_queue);
703         if (rc) {
704                 kfree(rx_queue->buffer);
705                 rx_queue->buffer = NULL;
706         }
707
708         return rc;
709 }
710
711 static void efx_init_rx_recycle_ring(struct efx_nic *efx,
712                                      struct efx_rx_queue *rx_queue)
713 {
714         unsigned int bufs_in_recycle_ring, page_ring_size;
715
716         /* Set the RX recycle ring size */
717 #ifdef CONFIG_PPC64
718         bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
719 #else
720         if (iommu_present(&pci_bus_type))
721                 bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_IOMMU;
722         else
723                 bufs_in_recycle_ring = EFX_RECYCLE_RING_SIZE_NOIOMMU;
724 #endif /* CONFIG_PPC64 */
725
726         page_ring_size = roundup_pow_of_two(bufs_in_recycle_ring /
727                                             efx->rx_bufs_per_page);
728         rx_queue->page_ring = kcalloc(page_ring_size,
729                                       sizeof(*rx_queue->page_ring), GFP_KERNEL);
730         rx_queue->page_ptr_mask = page_ring_size - 1;
731 }
732
733 void efx_init_rx_queue(struct efx_rx_queue *rx_queue)
734 {
735         struct efx_nic *efx = rx_queue->efx;
736         unsigned int max_fill, trigger, max_trigger;
737
738         netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
739                   "initialising RX queue %d\n", efx_rx_queue_index(rx_queue));
740
741         /* Initialise ptr fields */
742         rx_queue->added_count = 0;
743         rx_queue->notified_count = 0;
744         rx_queue->removed_count = 0;
745         rx_queue->min_fill = -1U;
746         efx_init_rx_recycle_ring(efx, rx_queue);
747
748         rx_queue->page_remove = 0;
749         rx_queue->page_add = rx_queue->page_ptr_mask + 1;
750         rx_queue->page_recycle_count = 0;
751         rx_queue->page_recycle_failed = 0;
752         rx_queue->page_recycle_full = 0;
753
754         /* Initialise limit fields */
755         max_fill = efx->rxq_entries - EFX_RXD_HEAD_ROOM;
756         max_trigger =
757                 max_fill - efx->rx_pages_per_batch * efx->rx_bufs_per_page;
758         if (rx_refill_threshold != 0) {
759                 trigger = max_fill * min(rx_refill_threshold, 100U) / 100U;
760                 if (trigger > max_trigger)
761                         trigger = max_trigger;
762         } else {
763                 trigger = max_trigger;
764         }
765
766         rx_queue->max_fill = max_fill;
767         rx_queue->fast_fill_trigger = trigger;
768         rx_queue->refill_enabled = true;
769
770         /* Set up RX descriptor ring */
771         efx_nic_init_rx(rx_queue);
772 }
773
774 void efx_fini_rx_queue(struct efx_rx_queue *rx_queue)
775 {
776         int i;
777         struct efx_nic *efx = rx_queue->efx;
778         struct efx_rx_buffer *rx_buf;
779
780         netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
781                   "shutting down RX queue %d\n", efx_rx_queue_index(rx_queue));
782
783         del_timer_sync(&rx_queue->slow_fill);
784
785         /* Release RX buffers from the current read ptr to the write ptr */
786         if (rx_queue->buffer) {
787                 for (i = rx_queue->removed_count; i < rx_queue->added_count;
788                      i++) {
789                         unsigned index = i & rx_queue->ptr_mask;
790                         rx_buf = efx_rx_buffer(rx_queue, index);
791                         efx_fini_rx_buffer(rx_queue, rx_buf);
792                 }
793         }
794
795         /* Unmap and release the pages in the recycle ring. Remove the ring. */
796         for (i = 0; i <= rx_queue->page_ptr_mask; i++) {
797                 struct page *page = rx_queue->page_ring[i];
798                 struct efx_rx_page_state *state;
799
800                 if (page == NULL)
801                         continue;
802
803                 state = page_address(page);
804                 dma_unmap_page(&efx->pci_dev->dev, state->dma_addr,
805                                PAGE_SIZE << efx->rx_buffer_order,
806                                DMA_FROM_DEVICE);
807                 put_page(page);
808         }
809         kfree(rx_queue->page_ring);
810         rx_queue->page_ring = NULL;
811 }
812
813 void efx_remove_rx_queue(struct efx_rx_queue *rx_queue)
814 {
815         netif_dbg(rx_queue->efx, drv, rx_queue->efx->net_dev,
816                   "destroying RX queue %d\n", efx_rx_queue_index(rx_queue));
817
818         efx_nic_remove_rx(rx_queue);
819
820         kfree(rx_queue->buffer);
821         rx_queue->buffer = NULL;
822 }
823
824
825 module_param(rx_refill_threshold, uint, 0444);
826 MODULE_PARM_DESC(rx_refill_threshold,
827                  "RX descriptor ring refill threshold (%)");
828
829 #ifdef CONFIG_RFS_ACCEL
830
831 static void efx_filter_rfs_work(struct work_struct *data)
832 {
833         struct efx_async_filter_insertion *req = container_of(data, struct efx_async_filter_insertion,
834                                                               work);
835         struct efx_nic *efx = netdev_priv(req->net_dev);
836         struct efx_channel *channel = efx_get_channel(efx, req->rxq_index);
837         int slot_idx = req - efx->rps_slot;
838         struct efx_arfs_rule *rule;
839         u16 arfs_id = 0;
840         int rc;
841
842         rc = efx->type->filter_insert(efx, &req->spec, true);
843         if (rc >= 0)
844                 rc %= efx->type->max_rx_ip_filters;
845         if (efx->rps_hash_table) {
846                 spin_lock_bh(&efx->rps_hash_lock);
847                 rule = efx_rps_hash_find(efx, &req->spec);
848                 /* The rule might have already gone, if someone else's request
849                  * for the same spec was already worked and then expired before
850                  * we got around to our work.  In that case we have nothing
851                  * tying us to an arfs_id, meaning that as soon as the filter
852                  * is considered for expiry it will be removed.
853                  */
854                 if (rule) {
855                         if (rc < 0)
856                                 rule->filter_id = EFX_ARFS_FILTER_ID_ERROR;
857                         else
858                                 rule->filter_id = rc;
859                         arfs_id = rule->arfs_id;
860                 }
861                 spin_unlock_bh(&efx->rps_hash_lock);
862         }
863         if (rc >= 0) {
864                 /* Remember this so we can check whether to expire the filter
865                  * later.
866                  */
867                 mutex_lock(&efx->rps_mutex);
868                 channel->rps_flow_id[rc] = req->flow_id;
869                 ++channel->rfs_filters_added;
870                 mutex_unlock(&efx->rps_mutex);
871
872                 if (req->spec.ether_type == htons(ETH_P_IP))
873                         netif_info(efx, rx_status, efx->net_dev,
874                                    "steering %s %pI4:%u:%pI4:%u to queue %u [flow %u filter %d id %u]\n",
875                                    (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
876                                    req->spec.rem_host, ntohs(req->spec.rem_port),
877                                    req->spec.loc_host, ntohs(req->spec.loc_port),
878                                    req->rxq_index, req->flow_id, rc, arfs_id);
879                 else
880                         netif_info(efx, rx_status, efx->net_dev,
881                                    "steering %s [%pI6]:%u:[%pI6]:%u to queue %u [flow %u filter %d id %u]\n",
882                                    (req->spec.ip_proto == IPPROTO_TCP) ? "TCP" : "UDP",
883                                    req->spec.rem_host, ntohs(req->spec.rem_port),
884                                    req->spec.loc_host, ntohs(req->spec.loc_port),
885                                    req->rxq_index, req->flow_id, rc, arfs_id);
886         }
887
888         /* Release references */
889         clear_bit(slot_idx, &efx->rps_slot_map);
890         dev_put(req->net_dev);
891 }
892
893 int efx_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
894                    u16 rxq_index, u32 flow_id)
895 {
896         struct efx_nic *efx = netdev_priv(net_dev);
897         struct efx_async_filter_insertion *req;
898         struct efx_arfs_rule *rule;
899         struct flow_keys fk;
900         int slot_idx;
901         bool new;
902         int rc;
903
904         /* find a free slot */
905         for (slot_idx = 0; slot_idx < EFX_RPS_MAX_IN_FLIGHT; slot_idx++)
906                 if (!test_and_set_bit(slot_idx, &efx->rps_slot_map))
907                         break;
908         if (slot_idx >= EFX_RPS_MAX_IN_FLIGHT)
909                 return -EBUSY;
910
911         if (flow_id == RPS_FLOW_ID_INVALID) {
912                 rc = -EINVAL;
913                 goto out_clear;
914         }
915
916         if (!skb_flow_dissect_flow_keys(skb, &fk, 0)) {
917                 rc = -EPROTONOSUPPORT;
918                 goto out_clear;
919         }
920
921         if (fk.basic.n_proto != htons(ETH_P_IP) && fk.basic.n_proto != htons(ETH_P_IPV6)) {
922                 rc = -EPROTONOSUPPORT;
923                 goto out_clear;
924         }
925         if (fk.control.flags & FLOW_DIS_IS_FRAGMENT) {
926                 rc = -EPROTONOSUPPORT;
927                 goto out_clear;
928         }
929
930         req = efx->rps_slot + slot_idx;
931         efx_filter_init_rx(&req->spec, EFX_FILTER_PRI_HINT,
932                            efx->rx_scatter ? EFX_FILTER_FLAG_RX_SCATTER : 0,
933                            rxq_index);
934         req->spec.match_flags =
935                 EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_IP_PROTO |
936                 EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_LOC_PORT |
937                 EFX_FILTER_MATCH_REM_HOST | EFX_FILTER_MATCH_REM_PORT;
938         req->spec.ether_type = fk.basic.n_proto;
939         req->spec.ip_proto = fk.basic.ip_proto;
940
941         if (fk.basic.n_proto == htons(ETH_P_IP)) {
942                 req->spec.rem_host[0] = fk.addrs.v4addrs.src;
943                 req->spec.loc_host[0] = fk.addrs.v4addrs.dst;
944         } else {
945                 memcpy(req->spec.rem_host, &fk.addrs.v6addrs.src,
946                        sizeof(struct in6_addr));
947                 memcpy(req->spec.loc_host, &fk.addrs.v6addrs.dst,
948                        sizeof(struct in6_addr));
949         }
950
951         req->spec.rem_port = fk.ports.src;
952         req->spec.loc_port = fk.ports.dst;
953
954         if (efx->rps_hash_table) {
955                 /* Add it to ARFS hash table */
956                 spin_lock(&efx->rps_hash_lock);
957                 rule = efx_rps_hash_add(efx, &req->spec, &new);
958                 if (!rule) {
959                         rc = -ENOMEM;
960                         goto out_unlock;
961                 }
962                 if (new)
963                         rule->arfs_id = efx->rps_next_id++ % RPS_NO_FILTER;
964                 rc = rule->arfs_id;
965                 /* Skip if existing or pending filter already does the right thing */
966                 if (!new && rule->rxq_index == rxq_index &&
967                     rule->filter_id >= EFX_ARFS_FILTER_ID_PENDING)
968                         goto out_unlock;
969                 rule->rxq_index = rxq_index;
970                 rule->filter_id = EFX_ARFS_FILTER_ID_PENDING;
971                 spin_unlock(&efx->rps_hash_lock);
972         } else {
973                 /* Without an ARFS hash table, we just use arfs_id 0 for all
974                  * filters.  This means if multiple flows hash to the same
975                  * flow_id, all but the most recently touched will be eligible
976                  * for expiry.
977                  */
978                 rc = 0;
979         }
980
981         /* Queue the request */
982         dev_hold(req->net_dev = net_dev);
983         INIT_WORK(&req->work, efx_filter_rfs_work);
984         req->rxq_index = rxq_index;
985         req->flow_id = flow_id;
986         schedule_work(&req->work);
987         return rc;
988 out_unlock:
989         spin_unlock(&efx->rps_hash_lock);
990 out_clear:
991         clear_bit(slot_idx, &efx->rps_slot_map);
992         return rc;
993 }
994
995 bool __efx_filter_rfs_expire(struct efx_nic *efx, unsigned int quota)
996 {
997         bool (*expire_one)(struct efx_nic *efx, u32 flow_id, unsigned int index);
998         unsigned int channel_idx, index, size;
999         u32 flow_id;
1000
1001         if (!mutex_trylock(&efx->rps_mutex))
1002                 return false;
1003         expire_one = efx->type->filter_rfs_expire_one;
1004         channel_idx = efx->rps_expire_channel;
1005         index = efx->rps_expire_index;
1006         size = efx->type->max_rx_ip_filters;
1007         while (quota--) {
1008                 struct efx_channel *channel = efx_get_channel(efx, channel_idx);
1009                 flow_id = channel->rps_flow_id[index];
1010
1011                 if (flow_id != RPS_FLOW_ID_INVALID &&
1012                     expire_one(efx, flow_id, index)) {
1013                         netif_info(efx, rx_status, efx->net_dev,
1014                                    "expired filter %d [queue %u flow %u]\n",
1015                                    index, channel_idx, flow_id);
1016                         channel->rps_flow_id[index] = RPS_FLOW_ID_INVALID;
1017                 }
1018                 if (++index == size) {
1019                         if (++channel_idx == efx->n_channels)
1020                                 channel_idx = 0;
1021                         index = 0;
1022                 }
1023         }
1024         efx->rps_expire_channel = channel_idx;
1025         efx->rps_expire_index = index;
1026
1027         mutex_unlock(&efx->rps_mutex);
1028         return true;
1029 }
1030
1031 #endif /* CONFIG_RFS_ACCEL */
1032
1033 /**
1034  * efx_filter_is_mc_recipient - test whether spec is a multicast recipient
1035  * @spec: Specification to test
1036  *
1037  * Return: %true if the specification is a non-drop RX filter that
1038  * matches a local MAC address I/G bit value of 1 or matches a local
1039  * IPv4 or IPv6 address value in the respective multicast address
1040  * range.  Otherwise %false.
1041  */
1042 bool efx_filter_is_mc_recipient(const struct efx_filter_spec *spec)
1043 {
1044         if (!(spec->flags & EFX_FILTER_FLAG_RX) ||
1045             spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
1046                 return false;
1047
1048         if (spec->match_flags &
1049             (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG) &&
1050             is_multicast_ether_addr(spec->loc_mac))
1051                 return true;
1052
1053         if ((spec->match_flags &
1054              (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
1055             (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
1056                 if (spec->ether_type == htons(ETH_P_IP) &&
1057                     ipv4_is_multicast(spec->loc_host[0]))
1058                         return true;
1059                 if (spec->ether_type == htons(ETH_P_IPV6) &&
1060                     ((const u8 *)spec->loc_host)[0] == 0xff)
1061                         return true;
1062         }
1063
1064         return false;
1065 }