net/sfc/base: move port config to ef10 NIC board config
[dpdk.git] / drivers / net / sfc / base / hunt_nic.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2012-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  */
6
7 #include "efx.h"
8 #include "efx_impl.h"
9 #if EFSYS_OPT_MON_MCDI
10 #include "mcdi_mon.h"
11 #endif
12
13 #if EFSYS_OPT_HUNTINGTON
14
15 #include "ef10_tlv_layout.h"
16
17 static  __checkReturn   efx_rc_t
18 hunt_nic_get_required_pcie_bandwidth(
19         __in            efx_nic_t *enp,
20         __out           uint32_t *bandwidth_mbpsp)
21 {
22         uint32_t port_modes;
23         uint32_t max_port_mode;
24         uint32_t bandwidth;
25         efx_rc_t rc;
26
27         /*
28          * On Huntington, the firmware may not give us the current port mode, so
29          * we need to go by the set of available port modes and assume the most
30          * capable mode is in use.
31          */
32
33         if ((rc = efx_mcdi_get_port_modes(enp, &port_modes, NULL)) != 0) {
34                 /* No port mode info available */
35                 bandwidth = 0;
36                 goto out;
37         }
38
39         if (port_modes & (1 << TLV_PORT_MODE_40G_40G)) {
40                 /*
41                  * This needs the full PCIe bandwidth (and could use
42                  * more) - roughly 64 Gbit/s for 8 lanes of Gen3.
43                  */
44                 if ((rc = efx_nic_calculate_pcie_link_bandwidth(8,
45                             EFX_PCIE_LINK_SPEED_GEN3, &bandwidth)) != 0)
46                         goto fail1;
47         } else {
48                 if (port_modes & (1 << TLV_PORT_MODE_40G)) {
49                         max_port_mode = TLV_PORT_MODE_40G;
50                 } else if (port_modes & (1 << TLV_PORT_MODE_10G_10G_10G_10G)) {
51                         max_port_mode = TLV_PORT_MODE_10G_10G_10G_10G;
52                 } else {
53                         /* Assume two 10G ports */
54                         max_port_mode = TLV_PORT_MODE_10G_10G;
55                 }
56
57                 if ((rc = ef10_nic_get_port_mode_bandwidth(max_port_mode,
58                                                             &bandwidth)) != 0)
59                         goto fail2;
60         }
61
62 out:
63         *bandwidth_mbpsp = bandwidth;
64
65         return (0);
66
67 fail2:
68         EFSYS_PROBE(fail2);
69 fail1:
70         EFSYS_PROBE1(fail1, efx_rc_t, rc);
71
72         return (rc);
73 }
74
75         __checkReturn   efx_rc_t
76 hunt_board_cfg(
77         __in            efx_nic_t *enp)
78 {
79         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
80         uint8_t mac_addr[6] = { 0 };
81         uint32_t board_type = 0;
82         ef10_link_state_t els;
83         efx_port_t *epp = &(enp->en_port);
84         uint32_t pf;
85         uint32_t vf;
86         uint32_t mask;
87         uint32_t flags;
88         uint32_t sysclk, dpcpu_clk;
89         uint32_t base, nvec;
90         uint32_t bandwidth;
91         efx_rc_t rc;
92
93         /* Huntington has a fixed 8Kbyte VI window size */
94         EFX_STATIC_ASSERT(ER_DZ_EVQ_RPTR_REG_STEP       == 8192);
95         EFX_STATIC_ASSERT(ER_DZ_EVQ_TMR_REG_STEP        == 8192);
96         EFX_STATIC_ASSERT(ER_DZ_RX_DESC_UPD_REG_STEP    == 8192);
97         EFX_STATIC_ASSERT(ER_DZ_TX_DESC_UPD_REG_STEP    == 8192);
98         EFX_STATIC_ASSERT(ER_DZ_TX_PIOBUF_STEP          == 8192);
99
100         EFX_STATIC_ASSERT(1U << EFX_VI_WINDOW_SHIFT_8K  == 8192);
101         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_8K;
102
103         /*
104          * Get PCIe function number from firmware (used for
105          * per-function privilege and dynamic config info).
106          *  - PCIe PF: pf = PF number, vf = 0xffff.
107          *  - PCIe VF: pf = parent PF, vf = VF number.
108          */
109         if ((rc = efx_mcdi_get_function_info(enp, &pf, &vf)) != 0)
110                 goto fail1;
111
112         encp->enc_pf = pf;
113         encp->enc_vf = vf;
114
115         /* MAC address for this function */
116         if (EFX_PCI_FUNCTION_IS_PF(encp)) {
117                 rc = efx_mcdi_get_mac_address_pf(enp, mac_addr);
118                 if ((rc == 0) && (mac_addr[0] & 0x02)) {
119                         /*
120                          * If the static config does not include a global MAC
121                          * address pool then the board may return a locally
122                          * administered MAC address (this should only happen on
123                          * incorrectly programmed boards).
124                          */
125                         rc = EINVAL;
126                 }
127         } else {
128                 rc = efx_mcdi_get_mac_address_vf(enp, mac_addr);
129         }
130         if (rc != 0)
131                 goto fail2;
132
133         EFX_MAC_ADDR_COPY(encp->enc_mac_addr, mac_addr);
134
135         /* Board configuration */
136         rc = efx_mcdi_get_board_cfg(enp, &board_type, NULL, NULL);
137         if (rc != 0) {
138                 /* Unprivileged functions may not be able to read board cfg */
139                 if (rc == EACCES)
140                         board_type = 0;
141                 else
142                         goto fail3;
143         }
144
145         encp->enc_board_type = board_type;
146         encp->enc_clk_mult = 1; /* not used for Huntington */
147
148         /* Fill out fields in enp->en_port and enp->en_nic_cfg from MCDI */
149         if ((rc = efx_mcdi_get_phy_cfg(enp)) != 0)
150                 goto fail4;
151
152         /* Obtain the default PHY advertised capabilities */
153         if ((rc = ef10_phy_get_link(enp, &els)) != 0)
154                 goto fail5;
155         epp->ep_default_adv_cap_mask = els.els_adv_cap_mask;
156         epp->ep_adv_cap_mask = els.els_adv_cap_mask;
157
158         /*
159          * Enable firmware workarounds for hardware errata.
160          * Expected responses are:
161          *  - 0 (zero):
162          *      Success: workaround enabled or disabled as requested.
163          *  - MC_CMD_ERR_ENOSYS (reported as ENOTSUP):
164          *      Firmware does not support the MC_CMD_WORKAROUND request.
165          *      (assume that the workaround is not supported).
166          *  - MC_CMD_ERR_ENOENT (reported as ENOENT):
167          *      Firmware does not support the requested workaround.
168          *  - MC_CMD_ERR_EPERM  (reported as EACCES):
169          *      Unprivileged function cannot enable/disable workarounds.
170          *
171          * See efx_mcdi_request_errcode() for MCDI error translations.
172          */
173
174         /*
175          * If the bug35388 workaround is enabled, then use an indirect access
176          * method to avoid unsafe EVQ writes.
177          */
178         rc = efx_mcdi_set_workaround(enp, MC_CMD_WORKAROUND_BUG35388, B_TRUE,
179             NULL);
180         if ((rc == 0) || (rc == EACCES))
181                 encp->enc_bug35388_workaround = B_TRUE;
182         else if ((rc == ENOTSUP) || (rc == ENOENT))
183                 encp->enc_bug35388_workaround = B_FALSE;
184         else
185                 goto fail6;
186
187         /*
188          * If the bug41750 workaround is enabled, then do not test interrupts,
189          * as the test will fail (seen with Greenport controllers).
190          */
191         rc = efx_mcdi_set_workaround(enp, MC_CMD_WORKAROUND_BUG41750, B_TRUE,
192             NULL);
193         if (rc == 0) {
194                 encp->enc_bug41750_workaround = B_TRUE;
195         } else if (rc == EACCES) {
196                 /* Assume a controller with 40G ports needs the workaround. */
197                 if (epp->ep_default_adv_cap_mask & EFX_PHY_CAP_40000FDX)
198                         encp->enc_bug41750_workaround = B_TRUE;
199                 else
200                         encp->enc_bug41750_workaround = B_FALSE;
201         } else if ((rc == ENOTSUP) || (rc == ENOENT)) {
202                 encp->enc_bug41750_workaround = B_FALSE;
203         } else {
204                 goto fail7;
205         }
206         if (EFX_PCI_FUNCTION_IS_VF(encp)) {
207                 /* Interrupt testing does not work for VFs. See bug50084. */
208                 encp->enc_bug41750_workaround = B_TRUE;
209         }
210
211         /*
212          * If the bug26807 workaround is enabled, then firmware has enabled
213          * support for chained multicast filters. Firmware will reset (FLR)
214          * functions which have filters in the hardware filter table when the
215          * workaround is enabled/disabled.
216          *
217          * We must recheck if the workaround is enabled after inserting the
218          * first hardware filter, in case it has been changed since this check.
219          */
220         rc = efx_mcdi_set_workaround(enp, MC_CMD_WORKAROUND_BUG26807,
221             B_TRUE, &flags);
222         if (rc == 0) {
223                 encp->enc_bug26807_workaround = B_TRUE;
224                 if (flags & (1 << MC_CMD_WORKAROUND_EXT_OUT_FLR_DONE_LBN)) {
225                         /*
226                          * Other functions had installed filters before the
227                          * workaround was enabled, and they have been reset
228                          * by firmware.
229                          */
230                         EFSYS_PROBE(bug26807_workaround_flr_done);
231                         /* FIXME: bump MC warm boot count ? */
232                 }
233         } else if (rc == EACCES) {
234                 /*
235                  * Unprivileged functions cannot enable the workaround in older
236                  * firmware.
237                  */
238                 encp->enc_bug26807_workaround = B_FALSE;
239         } else if ((rc == ENOTSUP) || (rc == ENOENT)) {
240                 encp->enc_bug26807_workaround = B_FALSE;
241         } else {
242                 goto fail8;
243         }
244
245         /* Get clock frequencies (in MHz). */
246         if ((rc = efx_mcdi_get_clock(enp, &sysclk, &dpcpu_clk)) != 0)
247                 goto fail9;
248
249         /*
250          * The Huntington timer quantum is 1536 sysclk cycles, documented for
251          * the EV_TMR_VAL field of EV_TIMER_TBL. Scale for MHz and ns units.
252          */
253         encp->enc_evq_timer_quantum_ns = 1536000UL / sysclk; /* 1536 cycles */
254         if (encp->enc_bug35388_workaround) {
255                 encp->enc_evq_timer_max_us = (encp->enc_evq_timer_quantum_ns <<
256                 ERF_DD_EVQ_IND_TIMER_VAL_WIDTH) / 1000;
257         } else {
258                 encp->enc_evq_timer_max_us = (encp->enc_evq_timer_quantum_ns <<
259                 FRF_CZ_TC_TIMER_VAL_WIDTH) / 1000;
260         }
261
262         encp->enc_bug61265_workaround = B_FALSE; /* Medford only */
263
264         /* Check capabilities of running datapath firmware */
265         if ((rc = ef10_get_datapath_caps(enp)) != 0)
266                 goto fail10;
267
268         /* Alignment for receive packet DMA buffers */
269         encp->enc_rx_buf_align_start = 1;
270         encp->enc_rx_buf_align_end = 64; /* RX DMA end padding */
271
272         /* Alignment for WPTR updates */
273         encp->enc_rx_push_align = EF10_RX_WPTR_ALIGN;
274
275         /*
276          * Maximum number of exclusive RSS contexts which can be allocated. The
277          * hardware supports 64, but 6 are reserved for shared contexts. They
278          * are a global resource so not all may be available.
279          */
280         encp->enc_rx_scale_max_exclusive_contexts = 58;
281
282         encp->enc_tx_dma_desc_size_max = EFX_MASK32(ESF_DZ_RX_KER_BYTE_CNT);
283         /* No boundary crossing limits */
284         encp->enc_tx_dma_desc_boundary = 0;
285
286         /*
287          * Set resource limits for MC_CMD_ALLOC_VIS. Note that we cannot use
288          * MC_CMD_GET_RESOURCE_LIMITS here as that reports the available
289          * resources (allocated to this PCIe function), which is zero until
290          * after we have allocated VIs.
291          */
292         encp->enc_evq_limit = 1024;
293         encp->enc_rxq_limit = EFX_RXQ_LIMIT_TARGET;
294         encp->enc_txq_limit = EFX_TXQ_LIMIT_TARGET;
295
296         /*
297          * The workaround for bug35388 uses the top bit of transmit queue
298          * descriptor writes, preventing the use of 4096 descriptor TXQs.
299          */
300         encp->enc_txq_max_ndescs = encp->enc_bug35388_workaround ? 2048 : 4096;
301
302         encp->enc_buftbl_limit = 0xFFFFFFFF;
303
304         EFX_STATIC_ASSERT(HUNT_PIOBUF_NBUFS <= EF10_MAX_PIOBUF_NBUFS);
305         encp->enc_piobuf_limit = HUNT_PIOBUF_NBUFS;
306         encp->enc_piobuf_size = HUNT_PIOBUF_SIZE;
307         encp->enc_piobuf_min_alloc_size = HUNT_MIN_PIO_ALLOC_SIZE;
308
309         /*
310          * Get the current privilege mask. Note that this may be modified
311          * dynamically, so this value is informational only. DO NOT use
312          * the privilege mask to check for sufficient privileges, as that
313          * can result in time-of-check/time-of-use bugs.
314          */
315         if ((rc = ef10_get_privilege_mask(enp, &mask)) != 0)
316                 goto fail11;
317         encp->enc_privilege_mask = mask;
318
319         /* Get interrupt vector limits */
320         if ((rc = efx_mcdi_get_vector_cfg(enp, &base, &nvec, NULL)) != 0) {
321                 if (EFX_PCI_FUNCTION_IS_PF(encp))
322                         goto fail12;
323
324                 /* Ignore error (cannot query vector limits from a VF). */
325                 base = 0;
326                 nvec = 1024;
327         }
328         encp->enc_intr_vec_base = base;
329         encp->enc_intr_limit = nvec;
330
331         /*
332          * Maximum number of bytes into the frame the TCP header can start for
333          * firmware assisted TSO to work.
334          */
335         encp->enc_tx_tso_tcp_header_offset_limit = EF10_TCP_HEADER_OFFSET_LIMIT;
336
337         if ((rc = hunt_nic_get_required_pcie_bandwidth(enp, &bandwidth)) != 0)
338                 goto fail13;
339         encp->enc_required_pcie_bandwidth_mbps = bandwidth;
340
341         /* All Huntington devices have a PCIe Gen3, 8 lane connector */
342         encp->enc_max_pcie_link_gen = EFX_PCIE_LINK_SPEED_GEN3;
343
344         return (0);
345
346 fail13:
347         EFSYS_PROBE(fail13);
348 fail12:
349         EFSYS_PROBE(fail12);
350 fail11:
351         EFSYS_PROBE(fail11);
352 fail10:
353         EFSYS_PROBE(fail10);
354 fail9:
355         EFSYS_PROBE(fail9);
356 fail8:
357         EFSYS_PROBE(fail8);
358 fail7:
359         EFSYS_PROBE(fail7);
360 fail6:
361         EFSYS_PROBE(fail6);
362 fail5:
363         EFSYS_PROBE(fail5);
364 fail4:
365         EFSYS_PROBE(fail4);
366 fail3:
367         EFSYS_PROBE(fail3);
368 fail2:
369         EFSYS_PROBE(fail2);
370 fail1:
371         EFSYS_PROBE1(fail1, efx_rc_t, rc);
372
373         return (rc);
374 }
375
376
377 #endif  /* EFSYS_OPT_HUNTINGTON */