net/sfc: discard scattered packet on Rx correctly
[dpdk.git] / drivers / net / sfc / sfc_rx.c
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <rte_mempool.h>
31
32 #include "efx.h"
33
34 #include "sfc.h"
35 #include "sfc_debug.h"
36 #include "sfc_log.h"
37 #include "sfc_ev.h"
38 #include "sfc_rx.h"
39 #include "sfc_tweak.h"
40
41 /*
42  * Maximum number of Rx queue flush attempt in the case of failure or
43  * flush timeout
44  */
45 #define SFC_RX_QFLUSH_ATTEMPTS          (3)
46
47 /*
48  * Time to wait between event queue polling attempts when waiting for Rx
49  * queue flush done or failed events.
50  */
51 #define SFC_RX_QFLUSH_POLL_WAIT_MS      (1)
52
53 /*
54  * Maximum number of event queue polling attempts when waiting for Rx queue
55  * flush done or failed events. It defines Rx queue flush attempt timeout
56  * together with SFC_RX_QFLUSH_POLL_WAIT_MS.
57  */
58 #define SFC_RX_QFLUSH_POLL_ATTEMPTS     (2000)
59
60 void
61 sfc_rx_qflush_done(struct sfc_rxq *rxq)
62 {
63         rxq->state |= SFC_RXQ_FLUSHED;
64         rxq->state &= ~SFC_RXQ_FLUSHING;
65 }
66
67 void
68 sfc_rx_qflush_failed(struct sfc_rxq *rxq)
69 {
70         rxq->state |= SFC_RXQ_FLUSH_FAILED;
71         rxq->state &= ~SFC_RXQ_FLUSHING;
72 }
73
74 static void
75 sfc_rx_qrefill(struct sfc_rxq *rxq)
76 {
77         unsigned int free_space;
78         unsigned int bulks;
79         void *objs[SFC_RX_REFILL_BULK];
80         efsys_dma_addr_t addr[RTE_DIM(objs)];
81         unsigned int added = rxq->added;
82         unsigned int id;
83         unsigned int i;
84         struct sfc_rx_sw_desc *rxd;
85         struct rte_mbuf *m;
86         uint8_t port_id = rxq->port_id;
87
88         free_space = EFX_RXQ_LIMIT(rxq->ptr_mask + 1) -
89                 (added - rxq->completed);
90         bulks = free_space / RTE_DIM(objs);
91
92         id = added & rxq->ptr_mask;
93         while (bulks-- > 0) {
94                 if (rte_mempool_get_bulk(rxq->refill_mb_pool, objs,
95                                          RTE_DIM(objs)) < 0) {
96                         /*
97                          * It is hardly a safe way to increment counter
98                          * from different contexts, but all PMDs do it.
99                          */
100                         rxq->evq->sa->eth_dev->data->rx_mbuf_alloc_failed +=
101                                 RTE_DIM(objs);
102                         break;
103                 }
104
105                 for (i = 0; i < RTE_DIM(objs);
106                      ++i, id = (id + 1) & rxq->ptr_mask) {
107                         m = objs[i];
108
109                         rxd = &rxq->sw_desc[id];
110                         rxd->mbuf = m;
111
112                         rte_mbuf_refcnt_set(m, 1);
113                         m->data_off = RTE_PKTMBUF_HEADROOM;
114                         m->next = NULL;
115                         m->nb_segs = 1;
116                         m->port = port_id;
117
118                         addr[i] = rte_pktmbuf_mtophys(m);
119                 }
120
121                 efx_rx_qpost(rxq->common, addr, rxq->buf_size,
122                              RTE_DIM(objs), rxq->completed, added);
123                 added += RTE_DIM(objs);
124         }
125
126         /* Push doorbell if something is posted */
127         if (rxq->added != added) {
128                 rxq->added = added;
129                 efx_rx_qpush(rxq->common, added, &rxq->pushed);
130         }
131 }
132
133 uint16_t
134 sfc_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
135 {
136         struct sfc_rxq *rxq = rx_queue;
137         unsigned int completed;
138         unsigned int prefix_size = rxq->prefix_size;
139         unsigned int done_pkts = 0;
140         boolean_t discard_next = B_FALSE;
141
142         if (unlikely((rxq->state & SFC_RXQ_RUNNING) == 0))
143                 return 0;
144
145         sfc_ev_qpoll(rxq->evq);
146
147         completed = rxq->completed;
148         while (completed != rxq->pending && done_pkts < nb_pkts) {
149                 unsigned int id;
150                 struct sfc_rx_sw_desc *rxd;
151                 struct rte_mbuf *m;
152                 unsigned int seg_len;
153                 unsigned int desc_flags;
154
155                 id = completed++ & rxq->ptr_mask;
156                 rxd = &rxq->sw_desc[id];
157                 m = rxd->mbuf;
158                 desc_flags = rxd->flags;
159
160                 if (discard_next)
161                         goto discard;
162
163                 if (desc_flags & (EFX_ADDR_MISMATCH | EFX_DISCARD))
164                         goto discard;
165
166                 if (desc_flags & EFX_PKT_CONT)
167                         goto discard;
168
169                 if (desc_flags & EFX_PKT_PREFIX_LEN) {
170                         uint16_t tmp_size;
171                         int rc __rte_unused;
172
173                         rc = efx_pseudo_hdr_pkt_length_get(rxq->common,
174                                 rte_pktmbuf_mtod(m, uint8_t *), &tmp_size);
175                         SFC_ASSERT(rc == 0);
176                         seg_len = tmp_size;
177                 } else {
178                         seg_len = rxd->size - prefix_size;
179                 }
180
181                 m->data_off += prefix_size;
182                 rte_pktmbuf_data_len(m) = seg_len;
183                 rte_pktmbuf_pkt_len(m) = seg_len;
184
185                 m->packet_type = RTE_PTYPE_L2_ETHER;
186
187                 *rx_pkts++ = m;
188                 done_pkts++;
189                 continue;
190
191 discard:
192                 discard_next = ((desc_flags & EFX_PKT_CONT) != 0);
193                 rte_mempool_put(rxq->refill_mb_pool, m);
194                 rxd->mbuf = NULL;
195         }
196
197         rxq->completed = completed;
198
199         sfc_rx_qrefill(rxq);
200
201         return done_pkts;
202 }
203
204 static void
205 sfc_rx_qpurge(struct sfc_rxq *rxq)
206 {
207         unsigned int i;
208         struct sfc_rx_sw_desc *rxd;
209
210         for (i = rxq->completed; i != rxq->added; ++i) {
211                 rxd = &rxq->sw_desc[i & rxq->ptr_mask];
212                 rte_mempool_put(rxq->refill_mb_pool, rxd->mbuf);
213                 rxd->mbuf = NULL;
214         }
215 }
216
217 static void
218 sfc_rx_qflush(struct sfc_adapter *sa, unsigned int sw_index)
219 {
220         struct sfc_rxq *rxq;
221         unsigned int retry_count;
222         unsigned int wait_count;
223
224         rxq = sa->rxq_info[sw_index].rxq;
225         SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
226
227         /*
228          * Retry Rx queue flushing in the case of flush failed or
229          * timeout. In the worst case it can delay for 6 seconds.
230          */
231         for (retry_count = 0;
232              ((rxq->state & SFC_RXQ_FLUSHED) == 0) &&
233              (retry_count < SFC_RX_QFLUSH_ATTEMPTS);
234              ++retry_count) {
235                 if (efx_rx_qflush(rxq->common) != 0) {
236                         rxq->state |= SFC_RXQ_FLUSH_FAILED;
237                         break;
238                 }
239                 rxq->state &= ~SFC_RXQ_FLUSH_FAILED;
240                 rxq->state |= SFC_RXQ_FLUSHING;
241
242                 /*
243                  * Wait for Rx queue flush done or failed event at least
244                  * SFC_RX_QFLUSH_POLL_WAIT_MS milliseconds and not more
245                  * than 2 seconds (SFC_RX_QFLUSH_POLL_WAIT_MS multiplied
246                  * by SFC_RX_QFLUSH_POLL_ATTEMPTS).
247                  */
248                 wait_count = 0;
249                 do {
250                         rte_delay_ms(SFC_RX_QFLUSH_POLL_WAIT_MS);
251                         sfc_ev_qpoll(rxq->evq);
252                 } while ((rxq->state & SFC_RXQ_FLUSHING) &&
253                          (wait_count++ < SFC_RX_QFLUSH_POLL_ATTEMPTS));
254
255                 if (rxq->state & SFC_RXQ_FLUSHING)
256                         sfc_err(sa, "RxQ %u flush timed out", sw_index);
257
258                 if (rxq->state & SFC_RXQ_FLUSH_FAILED)
259                         sfc_err(sa, "RxQ %u flush failed", sw_index);
260
261                 if (rxq->state & SFC_RXQ_FLUSHED)
262                         sfc_info(sa, "RxQ %u flushed", sw_index);
263         }
264
265         sfc_rx_qpurge(rxq);
266 }
267
268 int
269 sfc_rx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
270 {
271         struct sfc_rxq_info *rxq_info;
272         struct sfc_rxq *rxq;
273         struct sfc_evq *evq;
274         int rc;
275
276         sfc_log_init(sa, "sw_index=%u", sw_index);
277
278         SFC_ASSERT(sw_index < sa->rxq_count);
279
280         rxq_info = &sa->rxq_info[sw_index];
281         rxq = rxq_info->rxq;
282         SFC_ASSERT(rxq->state == SFC_RXQ_INITIALIZED);
283
284         evq = rxq->evq;
285
286         rc = sfc_ev_qstart(sa, evq->evq_index);
287         if (rc != 0)
288                 goto fail_ev_qstart;
289
290         rc = efx_rx_qcreate(sa->nic, rxq->hw_index, 0, rxq_info->type,
291                             &rxq->mem, rxq_info->entries,
292                             0 /* not used on EF10 */, evq->common,
293                             &rxq->common);
294         if (rc != 0)
295                 goto fail_rx_qcreate;
296
297         efx_rx_qenable(rxq->common);
298
299         rxq->pending = rxq->completed = rxq->added = rxq->pushed = 0;
300
301         rxq->state |= (SFC_RXQ_STARTED | SFC_RXQ_RUNNING);
302
303         sfc_rx_qrefill(rxq);
304
305         if (sw_index == 0) {
306                 rc = efx_mac_filter_default_rxq_set(sa->nic, rxq->common,
307                                                     B_FALSE);
308                 if (rc != 0)
309                         goto fail_mac_filter_default_rxq_set;
310         }
311
312         /* It seems to be used by DPDK for debug purposes only ('rte_ether') */
313         sa->eth_dev->data->rx_queue_state[sw_index] =
314                 RTE_ETH_QUEUE_STATE_STARTED;
315
316         return 0;
317
318 fail_mac_filter_default_rxq_set:
319         sfc_rx_qflush(sa, sw_index);
320
321 fail_rx_qcreate:
322         sfc_ev_qstop(sa, evq->evq_index);
323
324 fail_ev_qstart:
325         return rc;
326 }
327
328 void
329 sfc_rx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
330 {
331         struct sfc_rxq_info *rxq_info;
332         struct sfc_rxq *rxq;
333
334         sfc_log_init(sa, "sw_index=%u", sw_index);
335
336         SFC_ASSERT(sw_index < sa->rxq_count);
337
338         rxq_info = &sa->rxq_info[sw_index];
339         rxq = rxq_info->rxq;
340         SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
341
342         /* It seems to be used by DPDK for debug purposes only ('rte_ether') */
343         sa->eth_dev->data->rx_queue_state[sw_index] =
344                 RTE_ETH_QUEUE_STATE_STOPPED;
345
346         rxq->state &= ~SFC_RXQ_RUNNING;
347
348         if (sw_index == 0)
349                 efx_mac_filter_default_rxq_clear(sa->nic);
350
351         sfc_rx_qflush(sa, sw_index);
352
353         rxq->state = SFC_RXQ_INITIALIZED;
354
355         efx_rx_qdestroy(rxq->common);
356
357         sfc_ev_qstop(sa, rxq->evq->evq_index);
358 }
359
360 static int
361 sfc_rx_qcheck_conf(struct sfc_adapter *sa,
362                    const struct rte_eth_rxconf *rx_conf)
363 {
364         int rc = 0;
365
366         if (rx_conf->rx_thresh.pthresh != 0 ||
367             rx_conf->rx_thresh.hthresh != 0 ||
368             rx_conf->rx_thresh.wthresh != 0) {
369                 sfc_err(sa,
370                         "RxQ prefetch/host/writeback thresholds are not supported");
371                 rc = EINVAL;
372         }
373
374         if (rx_conf->rx_free_thresh != 0) {
375                 sfc_err(sa, "RxQ free threshold is not supported");
376                 rc = EINVAL;
377         }
378
379         if (rx_conf->rx_drop_en == 0) {
380                 sfc_err(sa, "RxQ drop disable is not supported");
381                 rc = EINVAL;
382         }
383
384         if (rx_conf->rx_deferred_start != 0) {
385                 sfc_err(sa, "RxQ deferred start is not supported");
386                 rc = EINVAL;
387         }
388
389         return rc;
390 }
391
392 static unsigned int
393 sfc_rx_mbuf_data_alignment(struct rte_mempool *mb_pool)
394 {
395         uint32_t data_off;
396         uint32_t order;
397
398         /* The mbuf object itself is always cache line aligned */
399         order = rte_bsf32(RTE_CACHE_LINE_SIZE);
400
401         /* Data offset from mbuf object start */
402         data_off = sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mb_pool) +
403                 RTE_PKTMBUF_HEADROOM;
404
405         order = MIN(order, rte_bsf32(data_off));
406
407         return 1u << (order - 1);
408 }
409
410 static uint16_t
411 sfc_rx_mb_pool_buf_size(struct sfc_adapter *sa, struct rte_mempool *mb_pool)
412 {
413         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
414         const uint32_t nic_align_start = MAX(1, encp->enc_rx_buf_align_start);
415         const uint32_t nic_align_end = MAX(1, encp->enc_rx_buf_align_end);
416         uint16_t buf_size;
417         unsigned int buf_aligned;
418         unsigned int start_alignment;
419         unsigned int end_padding_alignment;
420
421         /* Below it is assumed that both alignments are power of 2 */
422         SFC_ASSERT(rte_is_power_of_2(nic_align_start));
423         SFC_ASSERT(rte_is_power_of_2(nic_align_end));
424
425         /*
426          * mbuf is always cache line aligned, double-check
427          * that it meets rx buffer start alignment requirements.
428          */
429
430         /* Start from mbuf pool data room size */
431         buf_size = rte_pktmbuf_data_room_size(mb_pool);
432
433         /* Remove headroom */
434         if (buf_size <= RTE_PKTMBUF_HEADROOM) {
435                 sfc_err(sa,
436                         "RxQ mbuf pool %s object data room size %u is smaller than headroom %u",
437                         mb_pool->name, buf_size, RTE_PKTMBUF_HEADROOM);
438                 return 0;
439         }
440         buf_size -= RTE_PKTMBUF_HEADROOM;
441
442         /* Calculate guaranteed data start alignment */
443         buf_aligned = sfc_rx_mbuf_data_alignment(mb_pool);
444
445         /* Reserve space for start alignment */
446         if (buf_aligned < nic_align_start) {
447                 start_alignment = nic_align_start - buf_aligned;
448                 if (buf_size <= start_alignment) {
449                         sfc_err(sa,
450                                 "RxQ mbuf pool %s object data room size %u is insufficient for headroom %u and buffer start alignment %u required by NIC",
451                                 mb_pool->name,
452                                 rte_pktmbuf_data_room_size(mb_pool),
453                                 RTE_PKTMBUF_HEADROOM, start_alignment);
454                         return 0;
455                 }
456                 buf_aligned = nic_align_start;
457                 buf_size -= start_alignment;
458         } else {
459                 start_alignment = 0;
460         }
461
462         /* Make sure that end padding does not write beyond the buffer */
463         if (buf_aligned < nic_align_end) {
464                 /*
465                  * Estimate space which can be lost. If guarnteed buffer
466                  * size is odd, lost space is (nic_align_end - 1). More
467                  * accurate formula is below.
468                  */
469                 end_padding_alignment = nic_align_end -
470                         MIN(buf_aligned, 1u << (rte_bsf32(buf_size) - 1));
471                 if (buf_size <= end_padding_alignment) {
472                         sfc_err(sa,
473                                 "RxQ mbuf pool %s object data room size %u is insufficient for headroom %u, buffer start alignment %u and end padding alignment %u required by NIC",
474                                 mb_pool->name,
475                                 rte_pktmbuf_data_room_size(mb_pool),
476                                 RTE_PKTMBUF_HEADROOM, start_alignment,
477                                 end_padding_alignment);
478                         return 0;
479                 }
480                 buf_size -= end_padding_alignment;
481         } else {
482                 /*
483                  * Start is aligned the same or better than end,
484                  * just align length.
485                  */
486                 buf_size = P2ALIGN(buf_size, nic_align_end);
487         }
488
489         return buf_size;
490 }
491
492 int
493 sfc_rx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
494              uint16_t nb_rx_desc, unsigned int socket_id,
495              const struct rte_eth_rxconf *rx_conf,
496              struct rte_mempool *mb_pool)
497 {
498         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
499         int rc;
500         uint16_t buf_size;
501         struct sfc_rxq_info *rxq_info;
502         unsigned int evq_index;
503         struct sfc_evq *evq;
504         struct sfc_rxq *rxq;
505
506         rc = sfc_rx_qcheck_conf(sa, rx_conf);
507         if (rc != 0)
508                 goto fail_bad_conf;
509
510         buf_size = sfc_rx_mb_pool_buf_size(sa, mb_pool);
511         if (buf_size == 0) {
512                 sfc_err(sa, "RxQ %u mbuf pool object size is too small",
513                         sw_index);
514                 rc = EINVAL;
515                 goto fail_bad_conf;
516         }
517
518         if ((buf_size < sa->port.pdu + encp->enc_rx_prefix_size) &&
519             !sa->eth_dev->data->dev_conf.rxmode.enable_scatter) {
520                 sfc_err(sa, "Rx scatter is disabled and RxQ %u mbuf pool "
521                         "object size is too small", sw_index);
522                 sfc_err(sa, "RxQ %u calculated Rx buffer size is %u vs "
523                         "PDU size %u plus Rx prefix %u bytes",
524                         sw_index, buf_size, (unsigned int)sa->port.pdu,
525                         encp->enc_rx_prefix_size);
526                 rc = EINVAL;
527                 goto fail_bad_conf;
528         }
529
530         SFC_ASSERT(sw_index < sa->rxq_count);
531         rxq_info = &sa->rxq_info[sw_index];
532
533         SFC_ASSERT(nb_rx_desc <= rxq_info->max_entries);
534         rxq_info->entries = nb_rx_desc;
535         rxq_info->type = EFX_RXQ_TYPE_DEFAULT;
536
537         evq_index = sfc_evq_index_by_rxq_sw_index(sa, sw_index);
538
539         rc = sfc_ev_qinit(sa, evq_index, rxq_info->entries, socket_id);
540         if (rc != 0)
541                 goto fail_ev_qinit;
542
543         evq = sa->evq_info[evq_index].evq;
544
545         rc = ENOMEM;
546         rxq = rte_zmalloc_socket("sfc-rxq", sizeof(*rxq), RTE_CACHE_LINE_SIZE,
547                                  socket_id);
548         if (rxq == NULL)
549                 goto fail_rxq_alloc;
550
551         rc = sfc_dma_alloc(sa, "rxq", sw_index, EFX_RXQ_SIZE(rxq_info->entries),
552                            socket_id, &rxq->mem);
553         if (rc != 0)
554                 goto fail_dma_alloc;
555
556         rc = ENOMEM;
557         rxq->sw_desc = rte_calloc_socket("sfc-rxq-sw_desc", rxq_info->entries,
558                                          sizeof(*rxq->sw_desc),
559                                          RTE_CACHE_LINE_SIZE, socket_id);
560         if (rxq->sw_desc == NULL)
561                 goto fail_desc_alloc;
562
563         evq->rxq = rxq;
564         rxq->evq = evq;
565         rxq->ptr_mask = rxq_info->entries - 1;
566         rxq->refill_mb_pool = mb_pool;
567         rxq->buf_size = buf_size;
568         rxq->hw_index = sw_index;
569         rxq->port_id = sa->eth_dev->data->port_id;
570
571         /* Cache limits required on datapath in RxQ structure */
572         rxq->batch_max = encp->enc_rx_batch_max;
573         rxq->prefix_size = encp->enc_rx_prefix_size;
574
575         rxq->state = SFC_RXQ_INITIALIZED;
576
577         rxq_info->rxq = rxq;
578
579         return 0;
580
581 fail_desc_alloc:
582         sfc_dma_free(sa, &rxq->mem);
583
584 fail_dma_alloc:
585         rte_free(rxq);
586
587 fail_rxq_alloc:
588         sfc_ev_qfini(sa, evq_index);
589
590 fail_ev_qinit:
591         rxq_info->entries = 0;
592
593 fail_bad_conf:
594         sfc_log_init(sa, "failed %d", rc);
595         return rc;
596 }
597
598 void
599 sfc_rx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
600 {
601         struct sfc_rxq_info *rxq_info;
602         struct sfc_rxq *rxq;
603
604         SFC_ASSERT(sw_index < sa->rxq_count);
605
606         rxq_info = &sa->rxq_info[sw_index];
607
608         rxq = rxq_info->rxq;
609         SFC_ASSERT(rxq->state == SFC_RXQ_INITIALIZED);
610
611         rxq_info->rxq = NULL;
612         rxq_info->entries = 0;
613
614         rte_free(rxq->sw_desc);
615         sfc_dma_free(sa, &rxq->mem);
616         rte_free(rxq);
617 }
618
619 int
620 sfc_rx_start(struct sfc_adapter *sa)
621 {
622         unsigned int sw_index;
623         int rc;
624
625         sfc_log_init(sa, "rxq_count=%u", sa->rxq_count);
626
627         rc = efx_rx_init(sa->nic);
628         if (rc != 0)
629                 goto fail_rx_init;
630
631         for (sw_index = 0; sw_index < sa->rxq_count; ++sw_index) {
632                 rc = sfc_rx_qstart(sa, sw_index);
633                 if (rc != 0)
634                         goto fail_rx_qstart;
635         }
636
637         return 0;
638
639 fail_rx_qstart:
640         while (sw_index-- > 0)
641                 sfc_rx_qstop(sa, sw_index);
642
643         efx_rx_fini(sa->nic);
644
645 fail_rx_init:
646         sfc_log_init(sa, "failed %d", rc);
647         return rc;
648 }
649
650 void
651 sfc_rx_stop(struct sfc_adapter *sa)
652 {
653         unsigned int sw_index;
654
655         sfc_log_init(sa, "rxq_count=%u", sa->rxq_count);
656
657         sw_index = sa->rxq_count;
658         while (sw_index-- > 0) {
659                 if (sa->rxq_info[sw_index].rxq != NULL)
660                         sfc_rx_qstop(sa, sw_index);
661         }
662
663         efx_rx_fini(sa->nic);
664 }
665
666 static int
667 sfc_rx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
668 {
669         struct sfc_rxq_info *rxq_info = &sa->rxq_info[sw_index];
670         unsigned int max_entries;
671
672         max_entries = EFX_RXQ_MAXNDESCS;
673         SFC_ASSERT(rte_is_power_of_2(max_entries));
674
675         rxq_info->max_entries = max_entries;
676
677         return 0;
678 }
679
680 static int
681 sfc_rx_check_mode(struct sfc_adapter *sa, struct rte_eth_rxmode *rxmode)
682 {
683         int rc = 0;
684
685         switch (rxmode->mq_mode) {
686         case ETH_MQ_RX_NONE:
687                 /* No special checks are required */
688                 break;
689         default:
690                 sfc_err(sa, "Rx multi-queue mode %u not supported",
691                         rxmode->mq_mode);
692                 rc = EINVAL;
693         }
694
695         if (rxmode->header_split) {
696                 sfc_err(sa, "Header split on Rx not supported");
697                 rc = EINVAL;
698         }
699
700         if (rxmode->hw_vlan_filter) {
701                 sfc_err(sa, "HW VLAN filtering not supported");
702                 rc = EINVAL;
703         }
704
705         if (rxmode->hw_vlan_strip) {
706                 sfc_err(sa, "HW VLAN stripping not supported");
707                 rc = EINVAL;
708         }
709
710         if (rxmode->hw_vlan_extend) {
711                 sfc_err(sa,
712                         "Q-in-Q HW VLAN stripping not supported");
713                 rc = EINVAL;
714         }
715
716         if (!rxmode->hw_strip_crc) {
717                 sfc_warn(sa,
718                          "FCS stripping control not supported - always stripped");
719                 rxmode->hw_strip_crc = 1;
720         }
721
722         if (rxmode->enable_scatter) {
723                 sfc_err(sa, "Scatter on Rx not supported");
724                 rc = EINVAL;
725         }
726
727         if (rxmode->enable_lro) {
728                 sfc_err(sa, "LRO not supported");
729                 rc = EINVAL;
730         }
731
732         return rc;
733 }
734
735 /**
736  * Initialize Rx subsystem.
737  *
738  * Called at device configuration stage when number of receive queues is
739  * specified together with other device level receive configuration.
740  *
741  * It should be used to allocate NUMA-unaware resources.
742  */
743 int
744 sfc_rx_init(struct sfc_adapter *sa)
745 {
746         struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
747         unsigned int sw_index;
748         int rc;
749
750         rc = sfc_rx_check_mode(sa, &dev_conf->rxmode);
751         if (rc != 0)
752                 goto fail_check_mode;
753
754         sa->rxq_count = sa->eth_dev->data->nb_rx_queues;
755
756         rc = ENOMEM;
757         sa->rxq_info = rte_calloc_socket("sfc-rxqs", sa->rxq_count,
758                                          sizeof(struct sfc_rxq_info), 0,
759                                          sa->socket_id);
760         if (sa->rxq_info == NULL)
761                 goto fail_rxqs_alloc;
762
763         for (sw_index = 0; sw_index < sa->rxq_count; ++sw_index) {
764                 rc = sfc_rx_qinit_info(sa, sw_index);
765                 if (rc != 0)
766                         goto fail_rx_qinit_info;
767         }
768
769         return 0;
770
771 fail_rx_qinit_info:
772         rte_free(sa->rxq_info);
773         sa->rxq_info = NULL;
774
775 fail_rxqs_alloc:
776         sa->rxq_count = 0;
777 fail_check_mode:
778         sfc_log_init(sa, "failed %d", rc);
779         return rc;
780 }
781
782 /**
783  * Shutdown Rx subsystem.
784  *
785  * Called at device close stage, for example, before device
786  * reconfiguration or shutdown.
787  */
788 void
789 sfc_rx_fini(struct sfc_adapter *sa)
790 {
791         unsigned int sw_index;
792
793         sw_index = sa->rxq_count;
794         while (sw_index-- > 0) {
795                 if (sa->rxq_info[sw_index].rxq != NULL)
796                         sfc_rx_qfini(sa, sw_index);
797         }
798
799         rte_free(sa->rxq_info);
800         sa->rxq_info = NULL;
801         sa->rxq_count = 0;
802 }