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