common/sfc_efx/base: support UDP tunnel operations for EF100
[dpdk.git] / drivers / common / sfc_efx / base / rhead_nic.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2020 Xilinx, Inc.
4  * Copyright(c) 2018-2019 Solarflare Communications Inc.
5  */
6
7 #include "efx.h"
8 #include "efx_impl.h"
9
10
11 #if EFSYS_OPT_RIVERHEAD
12
13         __checkReturn   efx_rc_t
14 rhead_board_cfg(
15         __in            efx_nic_t *enp)
16 {
17         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
18         uint32_t end_padding;
19         uint32_t bandwidth;
20         efx_rc_t rc;
21
22         if ((rc = efx_mcdi_nic_board_cfg(enp)) != 0)
23                 goto fail1;
24
25         /*
26          * The tunnel encapsulation initialization happens unconditionally
27          * for now.
28          */
29         encp->enc_tunnel_encapsulations_supported =
30             (1u << EFX_TUNNEL_PROTOCOL_VXLAN) |
31             (1u << EFX_TUNNEL_PROTOCOL_GENEVE) |
32             (1u << EFX_TUNNEL_PROTOCOL_NVGRE);
33
34         /*
35          * Software limitation inherited from EF10. This limit is not
36          * increased since the hardware does not report this limit, it is
37          * handled internally resulting in a tunnel add error when there is no
38          * space for more UDP tunnels.
39          */
40         encp->enc_tunnel_config_udp_entries_max = EFX_TUNNEL_MAXNENTRIES;
41
42         encp->enc_clk_mult = 1; /* not used for Riverhead */
43
44         /*
45          * FIXME There are TxSend and TxSeg descriptors on Riverhead.
46          * TxSeg is bigger than TxSend.
47          */
48         encp->enc_tx_dma_desc_size_max = EFX_MASK32(ESF_GZ_TX_SEND_LEN);
49         /* No boundary crossing limits */
50         encp->enc_tx_dma_desc_boundary = 0;
51
52         /*
53          * Initialise design parameters to either a runtime value read from
54          * the design parameters area or the well known default value
55          * (see SF-119689-TC section 4.4 for details).
56          * FIXME: Read design parameters area values.
57          */
58         encp->enc_tx_tso_max_header_ndescs =
59             ESE_EF100_DP_GZ_TSO_MAX_HDR_NUM_SEGS_DEFAULT;
60         encp->enc_tx_tso_max_header_length =
61             ESE_EF100_DP_GZ_TSO_MAX_HDR_LEN_DEFAULT;
62         encp->enc_tx_tso_max_payload_ndescs =
63             ESE_EF100_DP_GZ_TSO_MAX_PAYLOAD_NUM_SEGS_DEFAULT;
64         encp->enc_tx_tso_max_payload_length =
65             ESE_EF100_DP_GZ_TSO_MAX_PAYLOAD_LEN_DEFAULT;
66         encp->enc_tx_tso_max_nframes =
67             ESE_EF100_DP_GZ_TSO_MAX_NUM_FRAMES_DEFAULT;
68
69         /*
70          * Riverhead does not put any restrictions on TCP header offset limit.
71          */
72         encp->enc_tx_tso_tcp_header_offset_limit = UINT32_MAX;
73
74         /*
75          * Set resource limits for MC_CMD_ALLOC_VIS. Note that we cannot use
76          * MC_CMD_GET_RESOURCE_LIMITS here as that reports the available
77          * resources (allocated to this PCIe function), which is zero until
78          * after we have allocated VIs.
79          */
80         encp->enc_evq_limit = 1024;
81         encp->enc_rxq_limit = EFX_RXQ_LIMIT_TARGET;
82         encp->enc_txq_limit = EFX_TXQ_LIMIT_TARGET;
83
84         encp->enc_buftbl_limit = UINT32_MAX;
85
86         /*
87          * Riverhead event queue creation completes
88          * immediately (no initial event).
89          */
90         encp->enc_evq_init_done_ev_supported = B_FALSE;
91
92         /*
93          * Enable firmware workarounds for hardware errata.
94          * Expected responses are:
95          *  - 0 (zero):
96          *      Success: workaround enabled or disabled as requested.
97          *  - MC_CMD_ERR_ENOSYS (reported as ENOTSUP):
98          *      Firmware does not support the MC_CMD_WORKAROUND request.
99          *      (assume that the workaround is not supported).
100          *  - MC_CMD_ERR_ENOENT (reported as ENOENT):
101          *      Firmware does not support the requested workaround.
102          *  - MC_CMD_ERR_EPERM  (reported as EACCES):
103          *      Unprivileged function cannot enable/disable workarounds.
104          *
105          * See efx_mcdi_request_errcode() for MCDI error translations.
106          */
107
108         /*
109          * Replay engine on Riverhead should suppress duplicate packets
110          * (e.g. because of exact multicast and all-multicast filters
111          * match) to the same RxQ.
112          */
113         encp->enc_bug26807_workaround = B_FALSE;
114
115         /*
116          * Checksums for TSO sends should always be correct on Riverhead.
117          * FIXME: revisit when TSO support is implemented.
118          */
119         encp->enc_bug61297_workaround = B_FALSE;
120
121         encp->enc_evq_max_nevs = RHEAD_EVQ_MAXNEVS;
122         encp->enc_evq_min_nevs = RHEAD_EVQ_MINNEVS;
123         encp->enc_rxq_max_ndescs = RHEAD_RXQ_MAXNDESCS;
124         encp->enc_rxq_min_ndescs = RHEAD_RXQ_MINNDESCS;
125         encp->enc_txq_max_ndescs = RHEAD_TXQ_MAXNDESCS;
126         encp->enc_txq_min_ndescs = RHEAD_TXQ_MINNDESCS;
127
128         /* Riverhead FW does not support event queue timers yet. */
129         encp->enc_evq_timer_quantum_ns = 0;
130         encp->enc_evq_timer_max_us = 0;
131
132         encp->enc_ev_desc_size = RHEAD_EVQ_DESC_SIZE;
133         encp->enc_rx_desc_size = RHEAD_RXQ_DESC_SIZE;
134         encp->enc_tx_desc_size = RHEAD_TXQ_DESC_SIZE;
135
136         /* No required alignment for WPTR updates */
137         encp->enc_rx_push_align = 1;
138
139         /* Riverhead supports a single Rx prefix size. */
140         encp->enc_rx_prefix_size = ESE_GZ_RX_PKT_PREFIX_LEN;
141
142         /* Alignment for receive packet DMA buffers. */
143         encp->enc_rx_buf_align_start = 1;
144
145         /* Get the RX DMA end padding alignment configuration. */
146         if ((rc = efx_mcdi_get_rxdp_config(enp, &end_padding)) != 0) {
147                 if (rc != EACCES)
148                         goto fail2;
149
150                 /* Assume largest tail padding size supported by hardware. */
151                 end_padding = 128;
152         }
153         encp->enc_rx_buf_align_end = end_padding;
154
155         /*
156          * Riverhead stores a single global copy of VPD, not per-PF as on
157          * Huntington.
158          */
159         encp->enc_vpd_is_global = B_TRUE;
160
161         rc = ef10_nic_get_port_mode_bandwidth(enp, &bandwidth);
162         if (rc != 0)
163                 goto fail3;
164         encp->enc_required_pcie_bandwidth_mbps = bandwidth;
165         encp->enc_max_pcie_link_gen = EFX_PCIE_LINK_SPEED_GEN3;
166
167         return (0);
168
169 fail3:
170         EFSYS_PROBE(fail3);
171 fail2:
172         EFSYS_PROBE(fail2);
173 fail1:
174         EFSYS_PROBE1(fail1, efx_rc_t, rc);
175
176         return (rc);
177 }
178
179         __checkReturn   efx_rc_t
180 rhead_nic_probe(
181         __in            efx_nic_t *enp)
182 {
183         const efx_nic_ops_t *enop = enp->en_enop;
184         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
185         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
186         efx_rc_t rc;
187
188         EFSYS_ASSERT(EFX_FAMILY_IS_EF100(enp));
189
190         /* Read and clear any assertion state */
191         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
192                 goto fail1;
193
194         /* Exit the assertion handler */
195         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
196                 if (rc != EACCES)
197                         goto fail2;
198
199         if ((rc = efx_mcdi_drv_attach(enp, B_TRUE)) != 0)
200                 goto fail3;
201
202         /* Get remaining controller-specific board config */
203         if ((rc = enop->eno_board_cfg(enp)) != 0)
204                 goto fail4;
205
206         /*
207          * Set default driver config limits (based on board config).
208          *
209          * FIXME: For now allocate a fixed number of VIs which is likely to be
210          * sufficient and small enough to allow multiple functions on the same
211          * port.
212          */
213         edcp->edc_min_vi_count = edcp->edc_max_vi_count =
214             MIN(128, MAX(encp->enc_rxq_limit, encp->enc_txq_limit));
215
216         /*
217          * The client driver must configure and enable PIO buffer support,
218          * but there is no PIO support on Riverhead anyway.
219          */
220         edcp->edc_max_piobuf_count = 0;
221         edcp->edc_pio_alloc_size = 0;
222
223 #if EFSYS_OPT_MAC_STATS
224         /* Wipe the MAC statistics */
225         if ((rc = efx_mcdi_mac_stats_clear(enp)) != 0)
226                 goto fail5;
227 #endif
228
229 #if EFSYS_OPT_LOOPBACK
230         if ((rc = efx_mcdi_get_loopback_modes(enp)) != 0)
231                 goto fail6;
232 #endif
233
234         return (0);
235
236 #if EFSYS_OPT_LOOPBACK
237 fail6:
238         EFSYS_PROBE(fail6);
239 #endif
240 #if EFSYS_OPT_MAC_STATS
241 fail5:
242         EFSYS_PROBE(fail5);
243 #endif
244 fail4:
245         EFSYS_PROBE(fail4);
246 fail3:
247         EFSYS_PROBE(fail3);
248 fail2:
249         EFSYS_PROBE(fail2);
250 fail1:
251         EFSYS_PROBE1(fail1, efx_rc_t, rc);
252
253         return (rc);
254 }
255
256         __checkReturn   efx_rc_t
257 rhead_nic_set_drv_limits(
258         __inout         efx_nic_t *enp,
259         __in            efx_drv_limits_t *edlp)
260 {
261         const efx_nic_cfg_t *encp = efx_nic_cfg_get(enp);
262         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
263         uint32_t min_evq_count, max_evq_count;
264         uint32_t min_rxq_count, max_rxq_count;
265         uint32_t min_txq_count, max_txq_count;
266         efx_rc_t rc;
267
268         if (edlp == NULL) {
269                 rc = EINVAL;
270                 goto fail1;
271         }
272
273         /* Get minimum required and maximum usable VI limits */
274         min_evq_count = MIN(edlp->edl_min_evq_count, encp->enc_evq_limit);
275         min_rxq_count = MIN(edlp->edl_min_rxq_count, encp->enc_rxq_limit);
276         min_txq_count = MIN(edlp->edl_min_txq_count, encp->enc_txq_limit);
277
278         edcp->edc_min_vi_count =
279             MAX(min_evq_count, MAX(min_rxq_count, min_txq_count));
280
281         max_evq_count = MIN(edlp->edl_max_evq_count, encp->enc_evq_limit);
282         max_rxq_count = MIN(edlp->edl_max_rxq_count, encp->enc_rxq_limit);
283         max_txq_count = MIN(edlp->edl_max_txq_count, encp->enc_txq_limit);
284
285         edcp->edc_max_vi_count =
286             MAX(max_evq_count, MAX(max_rxq_count, max_txq_count));
287
288         /* There is no PIO support on Riverhead */
289         edcp->edc_max_piobuf_count = 0;
290         edcp->edc_pio_alloc_size = 0;
291
292         return (0);
293
294 fail1:
295         EFSYS_PROBE1(fail1, efx_rc_t, rc);
296
297         return (rc);
298 }
299
300         __checkReturn   efx_rc_t
301 rhead_nic_reset(
302         __in            efx_nic_t *enp)
303 {
304         efx_rc_t rc;
305
306         /* ef10_nic_reset() is called to recover from BADASSERT failures. */
307         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
308                 goto fail1;
309         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
310                 goto fail2;
311
312         if ((rc = efx_mcdi_entity_reset(enp)) != 0)
313                 goto fail3;
314
315         /* Clear RX/TX DMA queue errors */
316         enp->en_reset_flags &= ~(EFX_RESET_RXQ_ERR | EFX_RESET_TXQ_ERR);
317
318         return (0);
319
320 fail3:
321         EFSYS_PROBE(fail3);
322 fail2:
323         EFSYS_PROBE(fail2);
324 fail1:
325         EFSYS_PROBE1(fail1, efx_rc_t, rc);
326
327         return (rc);
328 }
329
330         __checkReturn   efx_rc_t
331 rhead_nic_init(
332         __in            efx_nic_t *enp)
333 {
334         const efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
335         uint32_t min_vi_count, max_vi_count;
336         uint32_t vi_count, vi_base, vi_shift;
337         uint32_t vi_window_size;
338         efx_rc_t rc;
339
340         EFSYS_ASSERT(EFX_FAMILY_IS_EF100(enp));
341         EFSYS_ASSERT3U(edcp->edc_max_piobuf_count, ==, 0);
342
343         /* Enable reporting of some events (e.g. link change) */
344         if ((rc = efx_mcdi_log_ctrl(enp)) != 0)
345                 goto fail1;
346
347         min_vi_count = edcp->edc_min_vi_count;
348         max_vi_count = edcp->edc_max_vi_count;
349
350         /* Ensure that the previously attached driver's VIs are freed */
351         if ((rc = efx_mcdi_free_vis(enp)) != 0)
352                 goto fail2;
353
354         /*
355          * Reserve VI resources (EVQ+RXQ+TXQ) for this PCIe function. If this
356          * fails then retrying the request for fewer VI resources may succeed.
357          */
358         vi_count = 0;
359         if ((rc = efx_mcdi_alloc_vis(enp, min_vi_count, max_vi_count,
360                     &vi_base, &vi_count, &vi_shift)) != 0)
361                 goto fail3;
362
363         EFSYS_PROBE2(vi_alloc, uint32_t, vi_base, uint32_t, vi_count);
364
365         if (vi_count < min_vi_count) {
366                 rc = ENOMEM;
367                 goto fail4;
368         }
369
370         enp->en_arch.ef10.ena_vi_base = vi_base;
371         enp->en_arch.ef10.ena_vi_count = vi_count;
372         enp->en_arch.ef10.ena_vi_shift = vi_shift;
373
374         EFSYS_ASSERT3U(enp->en_nic_cfg.enc_vi_window_shift, !=,
375             EFX_VI_WINDOW_SHIFT_INVALID);
376         EFSYS_ASSERT3U(enp->en_nic_cfg.enc_vi_window_shift, <=,
377             EFX_VI_WINDOW_SHIFT_64K);
378         vi_window_size = 1U << enp->en_nic_cfg.enc_vi_window_shift;
379
380         /* Save UC memory mapping details */
381         enp->en_arch.ef10.ena_uc_mem_map_offset = 0;
382         enp->en_arch.ef10.ena_uc_mem_map_size =
383             vi_window_size * enp->en_arch.ef10.ena_vi_count;
384
385         /* No WC memory mapping since PIO is not supported */
386         enp->en_arch.ef10.ena_pio_write_vi_base = 0;
387         enp->en_arch.ef10.ena_wc_mem_map_offset = 0;
388         enp->en_arch.ef10.ena_wc_mem_map_size = 0;
389
390         enp->en_vport_id = EVB_PORT_ID_NULL;
391
392         enp->en_nic_cfg.enc_mcdi_max_payload_length = MCDI_CTL_SDU_LEN_MAX_V2;
393
394         return (0);
395
396 fail4:
397         EFSYS_PROBE(fail4);
398
399         (void) efx_mcdi_free_vis(enp);
400
401 fail3:
402         EFSYS_PROBE(fail3);
403 fail2:
404         EFSYS_PROBE(fail2);
405 fail1:
406         EFSYS_PROBE1(fail1, efx_rc_t, rc);
407
408         return (rc);
409 }
410
411         __checkReturn   efx_rc_t
412 rhead_nic_get_vi_pool(
413         __in            efx_nic_t *enp,
414         __out           uint32_t *vi_countp)
415 {
416         /*
417          * Report VIs that the client driver can use.
418          * Do not include VIs used for PIO buffer writes.
419          */
420         *vi_countp = enp->en_arch.ef10.ena_vi_count;
421
422         return (0);
423 }
424
425         __checkReturn   efx_rc_t
426 rhead_nic_get_bar_region(
427         __in            efx_nic_t *enp,
428         __in            efx_nic_region_t region,
429         __out           uint32_t *offsetp,
430         __out           size_t *sizep)
431 {
432         efx_rc_t rc;
433
434         EFSYS_ASSERT(EFX_FAMILY_IS_EF100(enp));
435
436         /*
437          * TODO: Specify host memory mapping alignment and granularity
438          * in efx_drv_limits_t so that they can be taken into account
439          * when allocating extra VIs for PIO writes.
440          */
441         switch (region) {
442         case EFX_REGION_VI:
443                 /* UC mapped memory BAR region for VI registers */
444                 *offsetp = enp->en_arch.ef10.ena_uc_mem_map_offset;
445                 *sizep = enp->en_arch.ef10.ena_uc_mem_map_size;
446                 break;
447
448         case EFX_REGION_PIO_WRITE_VI:
449                 /* WC mapped memory BAR region for piobuf writes */
450                 *offsetp = enp->en_arch.ef10.ena_wc_mem_map_offset;
451                 *sizep = enp->en_arch.ef10.ena_wc_mem_map_size;
452                 break;
453
454         default:
455                 rc = EINVAL;
456                 goto fail1;
457         }
458
459         return (0);
460
461 fail1:
462         EFSYS_PROBE1(fail1, efx_rc_t, rc);
463
464         return (rc);
465 }
466
467         __checkReturn   boolean_t
468 rhead_nic_hw_unavailable(
469         __in            efx_nic_t *enp)
470 {
471         efx_dword_t dword;
472
473         if (enp->en_reset_flags & EFX_RESET_HW_UNAVAIL)
474                 return (B_TRUE);
475
476         EFX_BAR_FCW_READD(enp, ER_GZ_MC_SFT_STATUS, &dword);
477         if (EFX_DWORD_FIELD(dword, EFX_DWORD_0) == 0xffffffff)
478                 goto unavail;
479
480         return (B_FALSE);
481
482 unavail:
483         rhead_nic_set_hw_unavailable(enp);
484
485         return (B_TRUE);
486 }
487
488                         void
489 rhead_nic_set_hw_unavailable(
490         __in            efx_nic_t *enp)
491 {
492         EFSYS_PROBE(hw_unavail);
493         enp->en_reset_flags |= EFX_RESET_HW_UNAVAIL;
494 }
495
496                         void
497 rhead_nic_fini(
498         __in            efx_nic_t *enp)
499 {
500         (void) efx_mcdi_free_vis(enp);
501         enp->en_arch.ef10.ena_vi_count = 0;
502 }
503
504                         void
505 rhead_nic_unprobe(
506         __in            efx_nic_t *enp)
507 {
508         (void) efx_mcdi_drv_attach(enp, B_FALSE);
509 }
510
511 #if EFSYS_OPT_DIAG
512
513         __checkReturn   efx_rc_t
514 rhead_nic_register_test(
515         __in            efx_nic_t *enp)
516 {
517         efx_rc_t rc;
518
519         /* FIXME */
520         _NOTE(ARGUNUSED(enp))
521         _NOTE(CONSTANTCONDITION)
522         if (B_FALSE) {
523                 rc = ENOTSUP;
524                 goto fail1;
525         }
526         /* FIXME */
527
528         return (0);
529
530 fail1:
531         EFSYS_PROBE1(fail1, efx_rc_t, rc);
532
533         return (rc);
534 }
535
536 #endif  /* EFSYS_OPT_DIAG */
537
538         __checkReturn                   efx_rc_t
539 rhead_nic_xilinx_cap_tbl_read_ef100_locator(
540         __in                            efsys_bar_t *esbp,
541         __in                            efsys_dma_addr_t offset,
542         __out                           efx_bar_region_t *ebrp)
543 {
544         efx_oword_t entry;
545         uint32_t rev;
546         uint32_t len;
547         efx_rc_t rc;
548
549         /*
550          * Xilinx Capabilities Table requires 32bit aligned reads.
551          * See SF-119689-TC section 4.2.2 "Discovery Steps".
552          */
553         EFSYS_BAR_READD(esbp, offset +
554                         (EFX_LOW_BIT(ESF_GZ_CFGBAR_ENTRY_FORMAT) / 8),
555                         &entry.eo_dword[0], B_FALSE);
556         EFSYS_BAR_READD(esbp, offset +
557                         (EFX_LOW_BIT(ESF_GZ_CFGBAR_ENTRY_SIZE) / 8),
558                         &entry.eo_dword[1], B_FALSE);
559
560         rev = EFX_OWORD_FIELD32(entry, ESF_GZ_CFGBAR_ENTRY_REV);
561         len = EFX_OWORD_FIELD32(entry, ESF_GZ_CFGBAR_ENTRY_SIZE);
562
563         if (rev != ESE_GZ_CFGBAR_ENTRY_REV_EF100 ||
564             len < ESE_GZ_CFGBAR_ENTRY_SIZE_EF100) {
565                 rc = EINVAL;
566                 goto fail1;
567         }
568
569         EFSYS_BAR_READD(esbp, offset +
570                         (EFX_LOW_BIT(ESF_GZ_CFGBAR_EF100_BAR) / 8),
571                         &entry.eo_dword[2], B_FALSE);
572
573         ebrp->ebr_index = EFX_OWORD_FIELD32(entry, ESF_GZ_CFGBAR_EF100_BAR);
574         ebrp->ebr_offset = EFX_OWORD_FIELD32(entry,
575                         ESF_GZ_CFGBAR_EF100_FUNC_CTL_WIN_OFF) <<
576                         ESE_GZ_EF100_FUNC_CTL_WIN_OFF_SHIFT;
577         ebrp->ebr_type = EFX_BAR_TYPE_MEM;
578         ebrp->ebr_length = 0;
579
580         return (0);
581
582 fail1:
583         EFSYS_PROBE1(fail1, efx_rc_t, rc);
584
585         return (rc);
586 }
587
588 #endif  /* EFSYS_OPT_RIVERHEAD */