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