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