common/sfc_efx/base: handle MCDI events on Riverhead
[dpdk.git] / drivers / common / sfc_efx / base / ef10_mcdi.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2020 Xilinx, Inc.
4  * Copyright(c) 2012-2019 Solarflare Communications Inc.
5  */
6
7 #include "efx.h"
8 #include "efx_impl.h"
9
10
11 #if EFSYS_OPT_RIVERHEAD || EFX_OPTS_EF10()
12
13 #if EFSYS_OPT_MCDI
14
15 #ifndef WITH_MCDI_V2
16 #error "WITH_MCDI_V2 required for EF10 MCDIv2 commands."
17 #endif
18
19
20         __checkReturn   efx_rc_t
21 ef10_mcdi_init(
22         __in            efx_nic_t *enp,
23         __in            const efx_mcdi_transport_t *emtp)
24 {
25         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
26         efsys_mem_t *esmp = emtp->emt_dma_mem;
27         efx_dword_t dword;
28         efx_rc_t rc;
29
30         EFSYS_ASSERT(EFX_FAMILY_IS_EF100(enp) || EFX_FAMILY_IS_EF10(enp));
31         EFSYS_ASSERT(enp->en_features & EFX_FEATURE_MCDI_DMA);
32
33         /*
34          * All EF10 firmware supports MCDIv2 and MCDIv1.
35          * Medford BootROM supports MCDIv2 and MCDIv1.
36          * Huntington BootROM supports MCDIv1 only.
37          */
38         emip->emi_max_version = 2;
39
40         /* A host DMA buffer is required for EF10 MCDI */
41         if (esmp == NULL) {
42                 rc = EINVAL;
43                 goto fail1;
44         }
45
46         /*
47          * Ensure that the MC doorbell is in a known state before issuing MCDI
48          * commands. The recovery algorithm requires that the MC command buffer
49          * must be 256 byte aligned. See bug24769.
50          */
51         if ((EFSYS_MEM_ADDR(esmp) & 0xFF) != 0) {
52                 rc = EINVAL;
53                 goto fail2;
54         }
55         EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 1);
56         switch (enp->en_family) {
57 #if EFSYS_OPT_RIVERHEAD
58         case EFX_FAMILY_RIVERHEAD:
59                 EFX_BAR_WRITED(enp, ER_GZ_MC_DB_HWRD_REG, &dword, B_FALSE);
60                 break;
61 #endif  /* EFSYS_OPT_RIVERHEAD */
62         default:
63                 EFX_BAR_WRITED(enp, ER_DZ_MC_DB_HWRD_REG, &dword, B_FALSE);
64                 break;
65         }
66
67         /* Save initial MC reboot status */
68         (void) ef10_mcdi_poll_reboot(enp);
69
70         /* Start a new epoch (allow fresh MCDI requests to succeed) */
71         efx_mcdi_new_epoch(enp);
72
73         return (0);
74
75 fail2:
76         EFSYS_PROBE(fail2);
77 fail1:
78         EFSYS_PROBE1(fail1, efx_rc_t, rc);
79
80         return (rc);
81 }
82
83                         void
84 ef10_mcdi_fini(
85         __in            efx_nic_t *enp)
86 {
87         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
88
89         emip->emi_new_epoch = B_FALSE;
90 }
91
92 /*
93  * In older firmware all commands are processed in a single thread, so a long
94  * running command for one PCIe function can block processing for another
95  * function (see bug 61269).
96  *
97  * In newer firmware that supports multithreaded MCDI processing, we can extend
98  * the timeout for long-running requests which we know firmware may choose to
99  * process in a background thread.
100  */
101 #define EF10_MCDI_CMD_TIMEOUT_US        (10 * 1000 * 1000)
102 #define EF10_MCDI_CMD_LONG_TIMEOUT_US   (60 * 1000 * 1000)
103
104                         void
105 ef10_mcdi_get_timeout(
106         __in            efx_nic_t *enp,
107         __in            efx_mcdi_req_t *emrp,
108         __out           uint32_t *timeoutp)
109 {
110         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
111
112         switch (emrp->emr_cmd) {
113         case MC_CMD_POLL_BIST:
114         case MC_CMD_NVRAM_ERASE:
115         case MC_CMD_LICENSING_V3:
116         case MC_CMD_NVRAM_UPDATE_FINISH:
117                 if (encp->enc_nvram_update_verify_result_supported != B_FALSE) {
118                         /*
119                          * Potentially longer running commands, which firmware
120                          * may choose to process in a background thread.
121                          */
122                         *timeoutp = EF10_MCDI_CMD_LONG_TIMEOUT_US;
123                         break;
124                 }
125                 /* FALLTHRU */
126         default:
127                 *timeoutp = EF10_MCDI_CMD_TIMEOUT_US;
128                 break;
129         }
130 }
131
132                         void
133 ef10_mcdi_send_request(
134         __in                    efx_nic_t *enp,
135         __in_bcount(hdr_len)    void *hdrp,
136         __in                    size_t hdr_len,
137         __in_bcount(sdu_len)    void *sdup,
138         __in                    size_t sdu_len)
139 {
140         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
141         efsys_mem_t *esmp = emtp->emt_dma_mem;
142         efx_dword_t dword;
143         unsigned int pos;
144
145         EFSYS_ASSERT(EFX_FAMILY_IS_EF100(enp) || EFX_FAMILY_IS_EF10(enp));
146
147         /* Write the header */
148         for (pos = 0; pos < hdr_len; pos += sizeof (efx_dword_t)) {
149                 dword = *(efx_dword_t *)((uint8_t *)hdrp + pos);
150                 EFSYS_MEM_WRITED(esmp, pos, &dword);
151         }
152
153         /* Write the payload */
154         for (pos = 0; pos < sdu_len; pos += sizeof (efx_dword_t)) {
155                 dword = *(efx_dword_t *)((uint8_t *)sdup + pos);
156                 EFSYS_MEM_WRITED(esmp, hdr_len + pos, &dword);
157         }
158
159         /* Guarantee ordering of memory (MCDI request) and PIO (MC doorbell) */
160         EFSYS_DMA_SYNC_FOR_DEVICE(esmp, 0, hdr_len + sdu_len);
161         EFSYS_PIO_WRITE_BARRIER();
162
163         /* Ring the doorbell to post the command DMA address to the MC */
164         EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0,
165             EFSYS_MEM_ADDR(esmp) >> 32);
166         switch (enp->en_family) {
167 #if EFSYS_OPT_RIVERHEAD
168         case EFX_FAMILY_RIVERHEAD:
169                 EFX_BAR_WRITED(enp, ER_GZ_MC_DB_LWRD_REG, &dword, B_FALSE);
170                 break;
171 #endif  /* EFSYS_OPT_RIVERHEAD */
172         default:
173                 EFX_BAR_WRITED(enp, ER_DZ_MC_DB_LWRD_REG, &dword, B_FALSE);
174                 break;
175         }
176
177         EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0,
178             EFSYS_MEM_ADDR(esmp) & 0xffffffff);
179         switch (enp->en_family) {
180 #if EFSYS_OPT_RIVERHEAD
181         case EFX_FAMILY_RIVERHEAD:
182                 EFX_BAR_WRITED(enp, ER_GZ_MC_DB_HWRD_REG, &dword, B_FALSE);
183                 break;
184 #endif  /* EFSYS_OPT_RIVERHEAD */
185         default:
186                 EFX_BAR_WRITED(enp, ER_DZ_MC_DB_HWRD_REG, &dword, B_FALSE);
187                 break;
188         }
189 }
190
191         __checkReturn   boolean_t
192 ef10_mcdi_poll_response(
193         __in            efx_nic_t *enp)
194 {
195         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
196         efsys_mem_t *esmp = emtp->emt_dma_mem;
197         efx_dword_t hdr;
198
199         EFSYS_MEM_READD(esmp, 0, &hdr);
200         EFSYS_MEM_READ_BARRIER();
201
202         return (EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE) ? B_TRUE : B_FALSE);
203 }
204
205                         void
206 ef10_mcdi_read_response(
207         __in                    efx_nic_t *enp,
208         __out_bcount(length)    void *bufferp,
209         __in                    size_t offset,
210         __in                    size_t length)
211 {
212         const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
213         efsys_mem_t *esmp = emtp->emt_dma_mem;
214         unsigned int pos = 0;
215         efx_dword_t data;
216         size_t remaining = length;
217
218         while (remaining > 0) {
219                 size_t chunk = MIN(remaining, sizeof (data));
220
221                 EFSYS_MEM_READD(esmp, offset + pos, &data);
222                 memcpy((uint8_t *)bufferp + pos, &data, chunk);
223                 pos += chunk;
224                 remaining -= chunk;
225         }
226 }
227
228                         efx_rc_t
229 ef10_mcdi_poll_reboot(
230         __in            efx_nic_t *enp)
231 {
232         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
233         efx_dword_t dword;
234         uint32_t old_status;
235         uint32_t new_status;
236         efx_rc_t rc;
237
238         old_status = emip->emi_mc_reboot_status;
239
240         EFX_STATIC_ASSERT(ER_DZ_BIU_MC_SFT_STATUS_REG_OFST ==
241             ER_GZ_MC_SFT_STATUS_OFST);
242
243         /* Update MC reboot status word */
244         EFX_BAR_TBL_READD(enp, ER_DZ_BIU_MC_SFT_STATUS_REG, 0, &dword, B_FALSE);
245         new_status = dword.ed_u32[0];
246
247         /* MC has rebooted if the value has changed */
248         if (new_status != old_status) {
249                 emip->emi_mc_reboot_status = new_status;
250
251                 /*
252                  * FIXME: Ignore detected MC REBOOT for now.
253                  *
254                  * The Siena support for checking for MC reboot from status
255                  * flags is broken - see comments in siena_mcdi_poll_reboot().
256                  * As the generic MCDI code is shared the EF10 reboot
257                  * detection suffers similar problems.
258                  *
259                  * Do not report an error when the boot status changes until
260                  * this can be handled by common code drivers (and reworked to
261                  * support Siena too).
262                  */
263                 _NOTE(CONSTANTCONDITION)
264                 if (B_FALSE) {
265                         rc = EIO;
266                         goto fail1;
267                 }
268         }
269
270         return (0);
271
272 fail1:
273         EFSYS_PROBE1(fail1, efx_rc_t, rc);
274
275         return (rc);
276 }
277
278         __checkReturn   efx_rc_t
279 ef10_mcdi_feature_supported(
280         __in            efx_nic_t *enp,
281         __in            efx_mcdi_feature_id_t id,
282         __out           boolean_t *supportedp)
283 {
284         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
285         uint32_t privilege_mask = encp->enc_privilege_mask;
286         efx_rc_t rc;
287
288         EFSYS_ASSERT(EFX_FAMILY_IS_EF100(enp) || EFX_FAMILY_IS_EF10(enp));
289
290         /*
291          * Use privilege mask state at MCDI attach.
292          */
293
294         switch (id) {
295         case EFX_MCDI_FEATURE_FW_UPDATE:
296                 /*
297                  * Admin privilege must be used prior to introduction of
298                  * specific flag.
299                  */
300                 *supportedp =
301                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, ADMIN);
302                 break;
303         case EFX_MCDI_FEATURE_LINK_CONTROL:
304                 /*
305                  * Admin privilege used prior to introduction of
306                  * specific flag.
307                  */
308                 *supportedp =
309                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, LINK) ||
310                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, ADMIN);
311                 break;
312         case EFX_MCDI_FEATURE_MACADDR_CHANGE:
313                 /*
314                  * Admin privilege must be used prior to introduction of
315                  * mac spoofing privilege (at v4.6), which is used up to
316                  * introduction of change mac spoofing privilege (at v4.7)
317                  */
318                 *supportedp =
319                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, CHANGE_MAC) ||
320                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, MAC_SPOOFING) ||
321                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, ADMIN);
322                 break;
323         case EFX_MCDI_FEATURE_MAC_SPOOFING:
324                 /*
325                  * Admin privilege must be used prior to introduction of
326                  * mac spoofing privilege (at v4.6), which is used up to
327                  * introduction of mac spoofing TX privilege (at v4.7)
328                  */
329                 *supportedp =
330                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, MAC_SPOOFING_TX) ||
331                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, MAC_SPOOFING) ||
332                     EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, ADMIN);
333                 break;
334         default:
335                 rc = ENOTSUP;
336                 goto fail1;
337         }
338
339         return (0);
340
341 fail1:
342         EFSYS_PROBE1(fail1, efx_rc_t, rc);
343
344         return (rc);
345 }
346
347 #endif  /* EFSYS_OPT_MCDI */
348
349 #endif  /* EFSYS_OPT_RIVERHEAD || EFX_OPTS_EF10() */