net/sfc: discard scattered packet on Rx correctly
[dpdk.git] / drivers / net / sfc / sfc_ev.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_debug.h>
31 #include <rte_cycles.h>
32 #include <rte_alarm.h>
33
34 #include "efx.h"
35
36 #include "sfc.h"
37 #include "sfc_debug.h"
38 #include "sfc_log.h"
39 #include "sfc_ev.h"
40 #include "sfc_rx.h"
41
42
43 /* Initial delay when waiting for event queue init complete event */
44 #define SFC_EVQ_INIT_BACKOFF_START_US   (1)
45 /* Maximum delay between event queue polling attempts */
46 #define SFC_EVQ_INIT_BACKOFF_MAX_US     (10 * 1000)
47 /* Event queue init approx timeout */
48 #define SFC_EVQ_INIT_TIMEOUT_US         (2 * US_PER_S)
49
50 /* Management event queue polling period in microseconds */
51 #define SFC_MGMT_EV_QPOLL_PERIOD_US     (US_PER_S)
52
53
54 static boolean_t
55 sfc_ev_initialized(void *arg)
56 {
57         struct sfc_evq *evq = arg;
58
59         /* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
60         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
61                    evq->init_state == SFC_EVQ_STARTED);
62
63         evq->init_state = SFC_EVQ_STARTED;
64
65         return B_FALSE;
66 }
67
68 static boolean_t
69 sfc_ev_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
70           uint32_t size, uint16_t flags)
71 {
72         struct sfc_evq *evq = arg;
73         struct sfc_rxq *rxq;
74         unsigned int stop;
75         unsigned int pending_id;
76         unsigned int delta;
77         unsigned int i;
78         struct sfc_rx_sw_desc *rxd;
79
80         if (unlikely(evq->exception))
81                 goto done;
82
83         rxq = evq->rxq;
84
85         SFC_ASSERT(rxq != NULL);
86         SFC_ASSERT(rxq->evq == evq);
87         SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
88
89         stop = (id + 1) & rxq->ptr_mask;
90         pending_id = rxq->pending & rxq->ptr_mask;
91         delta = (stop >= pending_id) ? (stop - pending_id) :
92                 (rxq->ptr_mask + 1 - pending_id + stop);
93
94         if (delta == 0) {
95                 /*
96                  * Rx event with no new descriptors done and zero length
97                  * is used to abort scattered packet when there is no room
98                  * for the tail.
99                  */
100                 if (unlikely(size != 0)) {
101                         evq->exception = B_TRUE;
102                         sfc_err(evq->sa,
103                                 "EVQ %u RxQ %u invalid RX abort "
104                                 "(id=%#x size=%u flags=%#x); needs restart\n",
105                                 evq->evq_index, sfc_rxq_sw_index(rxq),
106                                 id, size, flags);
107                         goto done;
108                 }
109
110                 /* Add discard flag to the first fragment */
111                 rxq->sw_desc[pending_id].flags |= EFX_DISCARD;
112                 /* Remove continue flag from the last fragment */
113                 rxq->sw_desc[id].flags &= ~EFX_PKT_CONT;
114         } else if (unlikely(delta > rxq->batch_max)) {
115                 evq->exception = B_TRUE;
116
117                 sfc_err(evq->sa,
118                         "EVQ %u RxQ %u completion out of order "
119                         "(id=%#x delta=%u flags=%#x); needs restart\n",
120                         evq->evq_index, sfc_rxq_sw_index(rxq), id, delta,
121                         flags);
122
123                 goto done;
124         }
125
126         for (i = pending_id; i != stop; i = (i + 1) & rxq->ptr_mask) {
127                 rxd = &rxq->sw_desc[i];
128
129                 rxd->flags = flags;
130
131                 SFC_ASSERT(size < (1 << 16));
132                 rxd->size = (uint16_t)size;
133         }
134
135         rxq->pending += delta;
136
137 done:
138         return B_FALSE;
139 }
140
141 static boolean_t
142 sfc_ev_tx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id)
143 {
144         struct sfc_evq *evq = arg;
145
146         sfc_err(evq->sa, "EVQ %u unexpected Tx event", evq->evq_index);
147         return B_TRUE;
148 }
149
150 static boolean_t
151 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
152                  __rte_unused uint32_t data)
153 {
154         struct sfc_evq *evq = arg;
155
156         if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
157                 return B_FALSE;
158
159         evq->exception = B_TRUE;
160         sfc_warn(evq->sa,
161                  "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
162                  " needs recovery",
163                  (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
164                  (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
165                  (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
166                  (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
167                  (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
168                  (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
169                  (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
170                  (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
171                  "UNKNOWN",
172                  code, data, evq->evq_index);
173
174         return B_TRUE;
175 }
176
177 static boolean_t
178 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
179 {
180         struct sfc_evq *evq = arg;
181         struct sfc_rxq *rxq;
182
183         rxq = evq->rxq;
184         SFC_ASSERT(rxq != NULL);
185         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
186         SFC_ASSERT(rxq->evq == evq);
187         sfc_rx_qflush_done(rxq);
188
189         return B_FALSE;
190 }
191
192 static boolean_t
193 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
194 {
195         struct sfc_evq *evq = arg;
196         struct sfc_rxq *rxq;
197
198         rxq = evq->rxq;
199         SFC_ASSERT(rxq != NULL);
200         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
201         SFC_ASSERT(rxq->evq == evq);
202         sfc_rx_qflush_failed(rxq);
203
204         return B_FALSE;
205 }
206
207 static boolean_t
208 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
209 {
210         struct sfc_evq *evq = arg;
211
212         sfc_err(evq->sa, "EVQ %u unexpected Tx flush done event",
213                 evq->evq_index);
214         return B_TRUE;
215 }
216
217 static boolean_t
218 sfc_ev_software(void *arg, uint16_t magic)
219 {
220         struct sfc_evq *evq = arg;
221
222         sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
223                 evq->evq_index, magic);
224         return B_TRUE;
225 }
226
227 static boolean_t
228 sfc_ev_sram(void *arg, uint32_t code)
229 {
230         struct sfc_evq *evq = arg;
231
232         sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
233                 evq->evq_index, code);
234         return B_TRUE;
235 }
236
237 static boolean_t
238 sfc_ev_wake_up(void *arg, uint32_t index)
239 {
240         struct sfc_evq *evq = arg;
241
242         sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
243                 evq->evq_index, index);
244         return B_TRUE;
245 }
246
247 static boolean_t
248 sfc_ev_timer(void *arg, uint32_t index)
249 {
250         struct sfc_evq *evq = arg;
251
252         sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
253                 evq->evq_index, index);
254         return B_TRUE;
255 }
256
257 static boolean_t
258 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
259 {
260         struct sfc_evq *evq = arg;
261         struct sfc_adapter *sa = evq->sa;
262         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
263         struct rte_eth_link new_link;
264
265         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
266
267         sfc_port_link_mode_to_info(link_mode, &new_link);
268         rte_atomic64_set((rte_atomic64_t *)dev_link, *(uint64_t *)&new_link);
269
270         return B_FALSE;
271 }
272
273 static const efx_ev_callbacks_t sfc_ev_callbacks = {
274         .eec_initialized        = sfc_ev_initialized,
275         .eec_rx                 = sfc_ev_rx,
276         .eec_tx                 = sfc_ev_tx,
277         .eec_exception          = sfc_ev_exception,
278         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
279         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
280         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
281         .eec_software           = sfc_ev_software,
282         .eec_sram               = sfc_ev_sram,
283         .eec_wake_up            = sfc_ev_wake_up,
284         .eec_timer              = sfc_ev_timer,
285         .eec_link_change        = sfc_ev_link_change,
286 };
287
288
289 void
290 sfc_ev_qpoll(struct sfc_evq *evq)
291 {
292         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
293                    evq->init_state == SFC_EVQ_STARTING);
294
295         /* Synchronize the DMA memory for reading not required */
296
297         efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
298
299         /* Poll-mode driver does not re-prime the event queue for interrupts */
300 }
301
302 void
303 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
304 {
305         if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
306                 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
307
308                 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
309                         sfc_ev_qpoll(mgmt_evq);
310
311                 rte_spinlock_unlock(&sa->mgmt_evq_lock);
312         }
313 }
314
315 int
316 sfc_ev_qprime(struct sfc_evq *evq)
317 {
318         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
319         return efx_ev_qprime(evq->common, evq->read_ptr);
320 }
321
322 int
323 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
324 {
325         const struct sfc_evq_info *evq_info;
326         struct sfc_evq *evq;
327         efsys_mem_t *esmp;
328         unsigned int total_delay_us;
329         unsigned int delay_us;
330         int rc;
331
332         sfc_log_init(sa, "sw_index=%u", sw_index);
333
334         evq_info = &sa->evq_info[sw_index];
335         evq = evq_info->evq;
336         esmp = &evq->mem;
337
338         /* Clear all events */
339         (void)memset((void *)esmp->esm_base, 0xff,
340                      EFX_EVQ_SIZE(evq_info->entries));
341
342         /* Create the common code event queue */
343         rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
344                             0 /* unused on EF10 */, 0,
345                             EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
346                             EFX_EVQ_FLAGS_NOTIFY_DISABLED,
347                             &evq->common);
348         if (rc != 0)
349                 goto fail_ev_qcreate;
350
351         evq->init_state = SFC_EVQ_STARTING;
352
353         /* Wait for the initialization event */
354         total_delay_us = 0;
355         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
356         do {
357                 (void)sfc_ev_qpoll(evq);
358
359                 /* Check to see if the initialization complete indication
360                  * posted by the hardware.
361                  */
362                 if (evq->init_state == SFC_EVQ_STARTED)
363                         goto done;
364
365                 /* Give event queue some time to init */
366                 rte_delay_us(delay_us);
367
368                 total_delay_us += delay_us;
369
370                 /* Exponential backoff */
371                 delay_us *= 2;
372                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
373                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
374
375         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
376
377         rc = ETIMEDOUT;
378         goto fail_timedout;
379
380 done:
381         return 0;
382
383 fail_timedout:
384         evq->init_state = SFC_EVQ_INITIALIZED;
385         efx_ev_qdestroy(evq->common);
386
387 fail_ev_qcreate:
388         sfc_log_init(sa, "failed %d", rc);
389         return rc;
390 }
391
392 void
393 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
394 {
395         const struct sfc_evq_info *evq_info;
396         struct sfc_evq *evq;
397
398         sfc_log_init(sa, "sw_index=%u", sw_index);
399
400         SFC_ASSERT(sw_index < sa->evq_count);
401
402         evq_info = &sa->evq_info[sw_index];
403         evq = evq_info->evq;
404
405         if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
406                 return;
407
408         evq->init_state = SFC_EVQ_INITIALIZED;
409         evq->read_ptr = 0;
410         evq->exception = B_FALSE;
411
412         efx_ev_qdestroy(evq->common);
413 }
414
415 static void
416 sfc_ev_mgmt_periodic_qpoll(void *arg)
417 {
418         struct sfc_adapter *sa = arg;
419         int rc;
420
421         sfc_ev_mgmt_qpoll(sa);
422
423         rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
424                                sfc_ev_mgmt_periodic_qpoll, sa);
425         if (rc != 0)
426                 sfc_panic(sa,
427                           "cannot rearm management EVQ polling alarm (rc=%d)",
428                           rc);
429 }
430
431 static void
432 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
433 {
434         sfc_ev_mgmt_periodic_qpoll(sa);
435 }
436
437 static void
438 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
439 {
440         rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
441 }
442
443 int
444 sfc_ev_start(struct sfc_adapter *sa)
445 {
446         int rc;
447
448         sfc_log_init(sa, "entry");
449
450         rc = efx_ev_init(sa->nic);
451         if (rc != 0)
452                 goto fail_ev_init;
453
454         /* Start management EVQ used for global events */
455         rte_spinlock_lock(&sa->mgmt_evq_lock);
456
457         rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
458         if (rc != 0)
459                 goto fail_mgmt_evq_start;
460
461         rte_spinlock_unlock(&sa->mgmt_evq_lock);
462
463         /*
464          * Start management EVQ polling. If interrupts are disabled
465          * (not used), it is required to process link status change
466          * and other device level events to avoid unrecoverable
467          * error because the event queue overflow.
468          */
469         sfc_ev_mgmt_periodic_qpoll_start(sa);
470
471         /*
472          * Rx/Tx event queues are started/stopped when corresponding
473          * Rx/Tx queue is started/stopped.
474          */
475
476         return 0;
477
478 fail_mgmt_evq_start:
479         rte_spinlock_unlock(&sa->mgmt_evq_lock);
480         efx_ev_fini(sa->nic);
481
482 fail_ev_init:
483         sfc_log_init(sa, "failed %d", rc);
484         return rc;
485 }
486
487 void
488 sfc_ev_stop(struct sfc_adapter *sa)
489 {
490         unsigned int sw_index;
491
492         sfc_log_init(sa, "entry");
493
494         sfc_ev_mgmt_periodic_qpoll_stop(sa);
495
496         /* Make sure that all event queues are stopped */
497         sw_index = sa->evq_count;
498         while (sw_index-- > 0) {
499                 if (sw_index == sa->mgmt_evq_index) {
500                         /* Locks are required for the management EVQ */
501                         rte_spinlock_lock(&sa->mgmt_evq_lock);
502                         sfc_ev_qstop(sa, sa->mgmt_evq_index);
503                         rte_spinlock_unlock(&sa->mgmt_evq_lock);
504                 } else {
505                         sfc_ev_qstop(sa, sw_index);
506                 }
507         }
508
509         efx_ev_fini(sa->nic);
510 }
511
512 int
513 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
514              unsigned int entries, int socket_id)
515 {
516         struct sfc_evq_info *evq_info;
517         struct sfc_evq *evq;
518         int rc;
519
520         sfc_log_init(sa, "sw_index=%u", sw_index);
521
522         evq_info = &sa->evq_info[sw_index];
523
524         SFC_ASSERT(rte_is_power_of_2(entries));
525         SFC_ASSERT(entries <= evq_info->max_entries);
526         evq_info->entries = entries;
527
528         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
529                                  socket_id);
530         if (evq == NULL)
531                 return ENOMEM;
532
533         evq->sa = sa;
534         evq->evq_index = sw_index;
535
536         /* Allocate DMA space */
537         rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
538                            socket_id, &evq->mem);
539         if (rc != 0)
540                 return rc;
541
542         evq->init_state = SFC_EVQ_INITIALIZED;
543
544         evq_info->evq = evq;
545
546         return 0;
547 }
548
549 void
550 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
551 {
552         struct sfc_evq *evq;
553
554         sfc_log_init(sa, "sw_index=%u", sw_index);
555
556         evq = sa->evq_info[sw_index].evq;
557
558         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
559
560         sa->evq_info[sw_index].evq = NULL;
561
562         sfc_dma_free(sa, &evq->mem);
563
564         rte_free(evq);
565 }
566
567 static int
568 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
569 {
570         struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
571         unsigned int max_entries;
572
573         sfc_log_init(sa, "sw_index=%u", sw_index);
574
575         max_entries = sfc_evq_max_entries(sa, sw_index);
576         SFC_ASSERT(rte_is_power_of_2(max_entries));
577
578         evq_info->max_entries = max_entries;
579
580         return 0;
581 }
582
583 static void
584 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
585 {
586         sfc_log_init(sa, "sw_index=%u", sw_index);
587
588         /* Nothing to cleanup */
589 }
590
591 int
592 sfc_ev_init(struct sfc_adapter *sa)
593 {
594         int rc;
595         unsigned int sw_index;
596
597         sfc_log_init(sa, "entry");
598
599         sa->evq_count = sfc_ev_qcount(sa);
600         sa->mgmt_evq_index = 0;
601         rte_spinlock_init(&sa->mgmt_evq_lock);
602
603         /* Allocate EVQ info array */
604         rc = ENOMEM;
605         sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
606                                          sizeof(struct sfc_evq_info), 0,
607                                          sa->socket_id);
608         if (sa->evq_info == NULL)
609                 goto fail_evqs_alloc;
610
611         for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
612                 rc = sfc_ev_qinit_info(sa, sw_index);
613                 if (rc != 0)
614                         goto fail_ev_qinit_info;
615         }
616
617         rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
618                           sa->socket_id);
619         if (rc != 0)
620                 goto fail_mgmt_evq_init;
621
622         /*
623          * Rx/Tx event queues are created/destroyed when corresponding
624          * Rx/Tx queue is created/destroyed.
625          */
626
627         return 0;
628
629 fail_mgmt_evq_init:
630 fail_ev_qinit_info:
631         while (sw_index-- > 0)
632                 sfc_ev_qfini_info(sa, sw_index);
633
634         rte_free(sa->evq_info);
635         sa->evq_info = NULL;
636
637 fail_evqs_alloc:
638         sa->evq_count = 0;
639         sfc_log_init(sa, "failed %d", rc);
640         return rc;
641 }
642
643 void
644 sfc_ev_fini(struct sfc_adapter *sa)
645 {
646         int sw_index;
647
648         sfc_log_init(sa, "entry");
649
650         /* Cleanup all event queues */
651         sw_index = sa->evq_count;
652         while (--sw_index >= 0) {
653                 if (sa->evq_info[sw_index].evq != NULL)
654                         sfc_ev_qfini(sa, sw_index);
655                 sfc_ev_qfini_info(sa, sw_index);
656         }
657
658         rte_free(sa->evq_info);
659         sa->evq_info = NULL;
660         sa->evq_count = 0;
661 }