ef10_mcdi.c revision 292008
1/*-
2 * Copyright (c) 2012-2015 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: head/sys/dev/sfxge/common/hunt_mcdi.c 292008 2015-12-09 06:24:22Z arybchik $");
33
34#include "efsys.h"
35#include "efx.h"
36#include "efx_impl.h"
37
38
39#if EFSYS_OPT_HUNTINGTON
40
41#if EFSYS_OPT_MCDI
42
43#ifndef WITH_MCDI_V2
44#error "WITH_MCDI_V2 required for Huntington MCDIv2 commands."
45#endif
46
47typedef enum efx_mcdi_header_type_e {
48	EFX_MCDI_HEADER_TYPE_V1, /* MCDIv0 (BootROM), MCDIv1 commands */
49	EFX_MCDI_HEADER_TYPE_V2, /* MCDIv2 commands */
50} efx_mcdi_header_type_t;
51
52/*
53 * Return the header format to use for sending an MCDI request.
54 *
55 * An MCDIv1 (Siena compatible) command should use MCDIv2 encapsulation if the
56 * request input buffer or response output buffer are too large for the MCDIv1
57 * format. An MCDIv2 command must always be sent using MCDIv2 encapsulation.
58 */
59#define	EFX_MCDI_HEADER_TYPE(_cmd, _length)				\
60	((((_cmd) & ~EFX_MASK32(MCDI_HEADER_CODE)) ||			\
61	((_length) & ~EFX_MASK32(MCDI_HEADER_DATALEN)))	?		\
62	EFX_MCDI_HEADER_TYPE_V2	: EFX_MCDI_HEADER_TYPE_V1)
63
64
65/*
66 * MCDI Header NOT_EPOCH flag
67 * ==========================
68 * A new epoch begins at initial startup or after an MC reboot, and defines when
69 * the MC should reject stale MCDI requests.
70 *
71 * The first MCDI request sent by the host should contain NOT_EPOCH=0, and all
72 * subsequent requests (until the next MC reboot) should contain NOT_EPOCH=1.
73 *
74 * After rebooting the MC will fail all requests with NOT_EPOCH=1 by writing a
75 * response with ERROR=1 and DATALEN=0 until a request is seen with NOT_EPOCH=0.
76 */
77
78
79	__checkReturn	efx_rc_t
80hunt_mcdi_init(
81	__in		efx_nic_t *enp,
82	__in		const efx_mcdi_transport_t *emtp)
83{
84	efsys_mem_t *esmp = emtp->emt_dma_mem;
85	efx_dword_t dword;
86	efx_rc_t rc;
87
88	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON);
89	EFSYS_ASSERT(enp->en_features & EFX_FEATURE_MCDI_DMA);
90
91	/* A host DMA buffer is required for Huntington MCDI */
92	if (esmp == NULL) {
93		rc = EINVAL;
94		goto fail1;
95	}
96
97	/*
98	 * Ensure that the MC doorbell is in a known state before issuing MCDI
99	 * commands. The recovery algorithm requires that the MC command buffer
100	 * must be 256 byte aligned. See bug24769.
101	 */
102	if ((EFSYS_MEM_ADDR(esmp) & 0xFF) != 0) {
103		rc = EINVAL;
104		goto fail2;
105	}
106	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 1);
107	EFX_BAR_WRITED(enp, ER_DZ_MC_DB_HWRD_REG, &dword, B_FALSE);
108
109	/* Save initial MC reboot status */
110	(void) hunt_mcdi_poll_reboot(enp);
111
112	/* Start a new epoch (allow fresh MCDI requests to succeed) */
113	efx_mcdi_new_epoch(enp);
114
115	return (0);
116
117fail2:
118	EFSYS_PROBE(fail2);
119fail1:
120	EFSYS_PROBE1(fail1, efx_rc_t, rc);
121
122	return (rc);
123}
124
125			void
126hunt_mcdi_fini(
127	__in		efx_nic_t *enp)
128{
129	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
130
131	emip->emi_new_epoch = B_FALSE;
132}
133
134			void
135hunt_mcdi_request_copyin(
136	__in		efx_nic_t *enp,
137	__in		efx_mcdi_req_t *emrp,
138	__in		unsigned int seq,
139	__in		boolean_t ev_cpl,
140	__in		boolean_t new_epoch)
141{
142	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
143	efsys_mem_t *esmp = emtp->emt_dma_mem;
144	efx_mcdi_header_type_t hdr_type;
145	efx_dword_t dword;
146	efx_dword_t hdr[2];
147	unsigned int xflags;
148	unsigned int pos;
149	size_t offset;
150
151	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
152
153	xflags = 0;
154	if (ev_cpl)
155		xflags |= MCDI_HEADER_XFLAGS_EVREQ;
156
157	offset = 0;
158
159	hdr_type = EFX_MCDI_HEADER_TYPE(emrp->emr_cmd,
160	    MAX(emrp->emr_in_length, emrp->emr_out_length));
161
162	if (hdr_type == EFX_MCDI_HEADER_TYPE_V2) {
163		/* Construct MCDI v2 header */
164		EFX_POPULATE_DWORD_8(hdr[0],
165		    MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
166		    MCDI_HEADER_RESYNC, 1,
167		    MCDI_HEADER_DATALEN, 0,
168		    MCDI_HEADER_SEQ, seq,
169		    MCDI_HEADER_NOT_EPOCH, new_epoch ? 0 : 1,
170		    MCDI_HEADER_ERROR, 0,
171		    MCDI_HEADER_RESPONSE, 0,
172		    MCDI_HEADER_XFLAGS, xflags);
173		EFSYS_MEM_WRITED(esmp, offset, &hdr[0]);
174		offset += sizeof (efx_dword_t);
175
176		EFX_POPULATE_DWORD_2(hdr[1],
177		    MC_CMD_V2_EXTN_IN_EXTENDED_CMD, emrp->emr_cmd,
178		    MC_CMD_V2_EXTN_IN_ACTUAL_LEN, emrp->emr_in_length);
179		EFSYS_MEM_WRITED(esmp, offset, &hdr[1]);
180		offset += sizeof (efx_dword_t);
181	} else {
182		/* Construct MCDI v1 header */
183		EFX_POPULATE_DWORD_8(hdr[0],
184		    MCDI_HEADER_CODE, emrp->emr_cmd,
185		    MCDI_HEADER_RESYNC, 1,
186		    MCDI_HEADER_DATALEN, emrp->emr_in_length,
187		    MCDI_HEADER_SEQ, seq,
188		    MCDI_HEADER_NOT_EPOCH, new_epoch ? 0 : 1,
189		    MCDI_HEADER_ERROR, 0,
190		    MCDI_HEADER_RESPONSE, 0,
191		    MCDI_HEADER_XFLAGS, xflags);
192		EFSYS_MEM_WRITED(esmp, 0, &hdr[0]);
193		offset += sizeof (efx_dword_t);
194	}
195
196#if EFSYS_OPT_MCDI_LOGGING
197	if (emtp->emt_logger != NULL) {
198		emtp->emt_logger(emtp->emt_context, EFX_LOG_MCDI_REQUEST,
199		    &hdr, offset,
200		    emrp->emr_in_buf, emrp->emr_in_length);
201	}
202#endif /* EFSYS_OPT_MCDI_LOGGING */
203
204	/* Construct the payload */
205	for (pos = 0; pos < emrp->emr_in_length; pos += sizeof (efx_dword_t)) {
206		memcpy(&dword, MCDI_IN(*emrp, efx_dword_t, pos),
207		    MIN(sizeof (dword), emrp->emr_in_length - pos));
208		EFSYS_MEM_WRITED(esmp, offset + pos, &dword);
209	}
210
211	/* Ring the doorbell to post the command DMA address to the MC */
212	EFSYS_ASSERT((EFSYS_MEM_ADDR(esmp) & 0xFF) == 0);
213
214	/* Guarantee ordering of memory (MCDI request) and PIO (MC doorbell) */
215	EFSYS_DMA_SYNC_FOR_DEVICE(esmp, 0, offset + emrp->emr_in_length);
216	EFSYS_PIO_WRITE_BARRIER();
217
218	EFX_POPULATE_DWORD_1(dword,
219	    EFX_DWORD_0, EFSYS_MEM_ADDR(esmp) >> 32);
220	EFX_BAR_WRITED(enp, ER_DZ_MC_DB_LWRD_REG, &dword, B_FALSE);
221
222	EFX_POPULATE_DWORD_1(dword,
223	    EFX_DWORD_0, EFSYS_MEM_ADDR(esmp) & 0xffffffff);
224	EFX_BAR_WRITED(enp, ER_DZ_MC_DB_HWRD_REG, &dword, B_FALSE);
225}
226
227			void
228hunt_mcdi_request_copyout(
229	__in		efx_nic_t *enp,
230	__in		efx_mcdi_req_t *emrp)
231{
232#if EFSYS_OPT_MCDI_LOGGING
233	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
234#endif /* EFSYS_OPT_MCDI_LOGGING */
235	efx_dword_t hdr[2];
236	unsigned int hdr_len;
237	size_t bytes;
238
239	if (emrp->emr_out_buf == NULL)
240		return;
241
242	/* Read the command header to detect MCDI response format */
243	hdr_len = sizeof (hdr[0]);
244	hunt_mcdi_read_response(enp, &hdr[0], 0, hdr_len);
245	if (EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_CODE) == MC_CMD_V2_EXTN) {
246		/*
247		 * Read the actual payload length. The length given in the event
248		 * is only correct for responses with the V1 format.
249		 */
250		hunt_mcdi_read_response(enp, &hdr[1], hdr_len, sizeof (hdr[1]));
251		hdr_len += sizeof (hdr[1]);
252
253		emrp->emr_out_length_used = EFX_DWORD_FIELD(hdr[1],
254					    MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
255	}
256
257	/* Copy payload out into caller supplied buffer */
258	bytes = MIN(emrp->emr_out_length_used, emrp->emr_out_length);
259	hunt_mcdi_read_response(enp, emrp->emr_out_buf, hdr_len, bytes);
260
261#if EFSYS_OPT_MCDI_LOGGING
262	if (emtp->emt_logger != NULL) {
263		emtp->emt_logger(emtp->emt_context,
264		    EFX_LOG_MCDI_RESPONSE,
265		    &hdr, hdr_len,
266		    emrp->emr_out_buf, bytes);
267	}
268#endif /* EFSYS_OPT_MCDI_LOGGING */
269}
270
271static	__checkReturn	boolean_t
272hunt_mcdi_poll_response(
273	__in		efx_nic_t *enp)
274{
275	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
276	efsys_mem_t *esmp = emtp->emt_dma_mem;
277	efx_dword_t hdr;
278
279	EFSYS_MEM_READD(esmp, 0, &hdr);
280	return (EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE) ? B_TRUE : B_FALSE);
281}
282
283			void
284hunt_mcdi_read_response(
285	__in		efx_nic_t *enp,
286	__out		void *bufferp,
287	__in		size_t offset,
288	__in		size_t length)
289{
290	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
291	efsys_mem_t *esmp = emtp->emt_dma_mem;
292	unsigned int pos;
293	efx_dword_t data;
294
295	for (pos = 0; pos < length; pos += sizeof (efx_dword_t)) {
296		EFSYS_MEM_READD(esmp, offset + pos, &data);
297		memcpy((uint8_t *)bufferp + pos, &data,
298		    MIN(sizeof (data), length - pos));
299	}
300}
301
302	__checkReturn	boolean_t
303hunt_mcdi_request_poll(
304	__in		efx_nic_t *enp)
305{
306#if EFSYS_OPT_MCDI_LOGGING
307	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
308#endif /* EFSYS_OPT_MCDI_LOGGING */
309	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
310	efx_mcdi_req_t *emrp;
311	int state;
312	efx_rc_t rc;
313
314	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
315
316	/* Serialise against post-watchdog efx_mcdi_ev* */
317	EFSYS_LOCK(enp->en_eslp, state);
318
319	EFSYS_ASSERT(emip->emi_pending_req != NULL);
320	EFSYS_ASSERT(!emip->emi_ev_cpl);
321	emrp = emip->emi_pending_req;
322
323	/* Check if a response is available */
324	if (hunt_mcdi_poll_response(enp) == B_FALSE) {
325		EFSYS_UNLOCK(enp->en_eslp, state);
326		return (B_FALSE);
327	}
328
329	/* Read the response header */
330	efx_mcdi_read_response_header(enp, emrp);
331
332	/* Request complete */
333	emip->emi_pending_req = NULL;
334
335	/* Ensure stale MCDI requests fail after an MC reboot. */
336	emip->emi_new_epoch = B_FALSE;
337
338	EFSYS_UNLOCK(enp->en_eslp, state);
339
340	if ((rc = emrp->emr_rc) != 0)
341		goto fail1;
342
343	hunt_mcdi_request_copyout(enp, emrp);
344	goto out;
345
346fail1:
347	if (!emrp->emr_quiet)
348		EFSYS_PROBE1(fail1, efx_rc_t, rc);
349
350	/* Reboot/Assertion */
351	if (rc == EIO || rc == EINTR)
352		efx_mcdi_raise_exception(enp, emrp, rc);
353
354out:
355	return (B_TRUE);
356}
357
358			efx_rc_t
359hunt_mcdi_poll_reboot(
360	__in		efx_nic_t *enp)
361{
362	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
363	efx_dword_t dword;
364	uint32_t old_status;
365	uint32_t new_status;
366	efx_rc_t rc;
367
368	old_status = emip->emi_mc_reboot_status;
369
370	/* Update MC reboot status word */
371	EFX_BAR_TBL_READD(enp, ER_DZ_BIU_MC_SFT_STATUS_REG, 0, &dword, B_FALSE);
372	new_status = dword.ed_u32[0];
373
374	/* MC has rebooted if the value has changed */
375	if (new_status != old_status) {
376		emip->emi_mc_reboot_status = new_status;
377
378		/*
379		 * FIXME: Ignore detected MC REBOOT for now.
380		 *
381		 * The Siena support for checking for MC reboot from status
382		 * flags is broken - see comments in siena_mcdi_poll_reboot().
383		 * As the generic MCDI code is shared the Huntington reboot
384		 * detection suffers similar problems.
385		 *
386		 * Do not report an error when the boot status changes until
387		 * this can be handled by common code drivers (and reworked to
388		 * support Siena too).
389		 */
390		if (B_FALSE) {
391			rc = EIO;
392			goto fail1;
393		}
394	}
395
396	return (0);
397
398fail1:
399	EFSYS_PROBE1(fail1, efx_rc_t, rc);
400
401	return (rc);
402}
403
404	__checkReturn	efx_rc_t
405hunt_mcdi_fw_update_supported(
406	__in		efx_nic_t *enp,
407	__out		boolean_t *supportedp)
408{
409	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
410
411	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
412
413	/*
414	 * Use privilege mask state at MCDI attach.
415	 * Admin privilege must be used prior to introduction of
416	 * specific flag.
417	 */
418	*supportedp = (encp->enc_privilege_mask &
419	    MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN)
420	    == MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN;
421
422	return (0);
423}
424
425	__checkReturn	efx_rc_t
426hunt_mcdi_macaddr_change_supported(
427	__in		efx_nic_t *enp,
428	__out		boolean_t *supportedp)
429{
430	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
431	uint32_t privilege_mask = encp->enc_privilege_mask;
432
433	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
434
435	/*
436	 * Use privilege mask state at MCDI attach.
437	 * Admin privilege must be used prior to introduction of
438	 * mac spoofing privilege (at v4.6), which is used up to
439	 * introduction of change mac spoofing privilege (at v4.7)
440	 */
441	*supportedp =
442	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_CHANGE_MAC) ==
443	    MC_CMD_PRIVILEGE_MASK_IN_GRP_CHANGE_MAC) ||
444	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING) ==
445	    MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING) ||
446	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN) ==
447	    MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN);
448
449	return (0);
450}
451
452	__checkReturn	efx_rc_t
453hunt_mcdi_mac_spoofing_supported(
454	__in		efx_nic_t *enp,
455	__out		boolean_t *supportedp)
456{
457	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
458	uint32_t privilege_mask = encp->enc_privilege_mask;
459
460	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
461
462	/*
463	 * Use privilege mask state at MCDI attach.
464	 * Admin privilege must be used prior to introduction of
465	 * mac spoofing privilege (at v4.6), which is used up to
466	 * introduction of mac spoofing TX privilege (at v4.7)
467	 */
468	*supportedp =
469	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX) ==
470	    MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING_TX) ||
471	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING) ==
472	    MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING) ||
473	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN) ==
474	    MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN);
475
476	return (0);
477}
478
479
480	__checkReturn	efx_rc_t
481hunt_mcdi_link_control_supported(
482	__in		efx_nic_t *enp,
483	__out		boolean_t *supportedp)
484{
485	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
486	uint32_t privilege_mask = encp->enc_privilege_mask;
487
488	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
489
490	/*
491	 * Use privilege mask state at MCDI attach.
492	 * Admin privilege used prior to introduction of
493	 * specific flag.
494	 */
495	*supportedp =
496	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK) ==
497	    MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK) ||
498	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN) ==
499	    MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN);
500
501	return (0);
502}
503
504#endif	/* EFSYS_OPT_MCDI */
505
506#endif	/* EFSYS_OPT_HUNTINGTON */
507