ef10_mcdi.c revision 293757
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 293757 2016-01-12 13:39:25Z arybchik $");
33
34#include "efsys.h"
35#include "efx.h"
36#include "efx_impl.h"
37
38
39#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
40
41#if EFSYS_OPT_MCDI
42
43#ifndef WITH_MCDI_V2
44#error "WITH_MCDI_V2 required for EF10 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
80ef10_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		    enp->en_family == EFX_FAMILY_MEDFORD);
90	EFSYS_ASSERT(enp->en_features & EFX_FEATURE_MCDI_DMA);
91
92	/* A host DMA buffer is required for EF10 MCDI */
93	if (esmp == NULL) {
94		rc = EINVAL;
95		goto fail1;
96	}
97
98	/*
99	 * Ensure that the MC doorbell is in a known state before issuing MCDI
100	 * commands. The recovery algorithm requires that the MC command buffer
101	 * must be 256 byte aligned. See bug24769.
102	 */
103	if ((EFSYS_MEM_ADDR(esmp) & 0xFF) != 0) {
104		rc = EINVAL;
105		goto fail2;
106	}
107	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 1);
108	EFX_BAR_WRITED(enp, ER_DZ_MC_DB_HWRD_REG, &dword, B_FALSE);
109
110	/* Save initial MC reboot status */
111	(void) ef10_mcdi_poll_reboot(enp);
112
113	/* Start a new epoch (allow fresh MCDI requests to succeed) */
114	efx_mcdi_new_epoch(enp);
115
116	return (0);
117
118fail2:
119	EFSYS_PROBE(fail2);
120fail1:
121	EFSYS_PROBE1(fail1, efx_rc_t, rc);
122
123	return (rc);
124}
125
126			void
127ef10_mcdi_fini(
128	__in		efx_nic_t *enp)
129{
130	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
131
132	emip->emi_new_epoch = B_FALSE;
133}
134
135			void
136ef10_mcdi_request_copyin(
137	__in		efx_nic_t *enp,
138	__in		efx_mcdi_req_t *emrp,
139	__in		unsigned int seq,
140	__in		boolean_t ev_cpl,
141	__in		boolean_t new_epoch)
142{
143	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
144	efsys_mem_t *esmp = emtp->emt_dma_mem;
145	efx_mcdi_header_type_t hdr_type;
146	efx_dword_t dword;
147	efx_dword_t hdr[2];
148	unsigned int xflags;
149	unsigned int pos;
150	size_t offset;
151
152	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
153		    enp->en_family == EFX_FAMILY_MEDFORD);
154
155	xflags = 0;
156	if (ev_cpl)
157		xflags |= MCDI_HEADER_XFLAGS_EVREQ;
158
159	offset = 0;
160
161	hdr_type = EFX_MCDI_HEADER_TYPE(emrp->emr_cmd,
162	    MAX(emrp->emr_in_length, emrp->emr_out_length));
163
164	if (hdr_type == EFX_MCDI_HEADER_TYPE_V2) {
165		/* Construct MCDI v2 header */
166		EFX_POPULATE_DWORD_8(hdr[0],
167		    MCDI_HEADER_CODE, MC_CMD_V2_EXTN,
168		    MCDI_HEADER_RESYNC, 1,
169		    MCDI_HEADER_DATALEN, 0,
170		    MCDI_HEADER_SEQ, seq,
171		    MCDI_HEADER_NOT_EPOCH, new_epoch ? 0 : 1,
172		    MCDI_HEADER_ERROR, 0,
173		    MCDI_HEADER_RESPONSE, 0,
174		    MCDI_HEADER_XFLAGS, xflags);
175		EFSYS_MEM_WRITED(esmp, offset, &hdr[0]);
176		offset += sizeof (efx_dword_t);
177
178		EFX_POPULATE_DWORD_2(hdr[1],
179		    MC_CMD_V2_EXTN_IN_EXTENDED_CMD, emrp->emr_cmd,
180		    MC_CMD_V2_EXTN_IN_ACTUAL_LEN, emrp->emr_in_length);
181		EFSYS_MEM_WRITED(esmp, offset, &hdr[1]);
182		offset += sizeof (efx_dword_t);
183	} else {
184		/* Construct MCDI v1 header */
185		EFX_POPULATE_DWORD_8(hdr[0],
186		    MCDI_HEADER_CODE, emrp->emr_cmd,
187		    MCDI_HEADER_RESYNC, 1,
188		    MCDI_HEADER_DATALEN, emrp->emr_in_length,
189		    MCDI_HEADER_SEQ, seq,
190		    MCDI_HEADER_NOT_EPOCH, new_epoch ? 0 : 1,
191		    MCDI_HEADER_ERROR, 0,
192		    MCDI_HEADER_RESPONSE, 0,
193		    MCDI_HEADER_XFLAGS, xflags);
194		EFSYS_MEM_WRITED(esmp, 0, &hdr[0]);
195		offset += sizeof (efx_dword_t);
196	}
197
198#if EFSYS_OPT_MCDI_LOGGING
199	if (emtp->emt_logger != NULL) {
200		emtp->emt_logger(emtp->emt_context, EFX_LOG_MCDI_REQUEST,
201		    &hdr, offset,
202		    emrp->emr_in_buf, emrp->emr_in_length);
203	}
204#endif /* EFSYS_OPT_MCDI_LOGGING */
205
206	/* Construct the payload */
207	for (pos = 0; pos < emrp->emr_in_length; pos += sizeof (efx_dword_t)) {
208		memcpy(&dword, MCDI_IN(*emrp, efx_dword_t, pos),
209		    MIN(sizeof (dword), emrp->emr_in_length - pos));
210		EFSYS_MEM_WRITED(esmp, offset + pos, &dword);
211	}
212
213	/* Ring the doorbell to post the command DMA address to the MC */
214	EFSYS_ASSERT((EFSYS_MEM_ADDR(esmp) & 0xFF) == 0);
215
216	/* Guarantee ordering of memory (MCDI request) and PIO (MC doorbell) */
217	EFSYS_DMA_SYNC_FOR_DEVICE(esmp, 0, offset + emrp->emr_in_length);
218	EFSYS_PIO_WRITE_BARRIER();
219
220	EFX_POPULATE_DWORD_1(dword,
221	    EFX_DWORD_0, EFSYS_MEM_ADDR(esmp) >> 32);
222	EFX_BAR_WRITED(enp, ER_DZ_MC_DB_LWRD_REG, &dword, B_FALSE);
223
224	EFX_POPULATE_DWORD_1(dword,
225	    EFX_DWORD_0, EFSYS_MEM_ADDR(esmp) & 0xffffffff);
226	EFX_BAR_WRITED(enp, ER_DZ_MC_DB_HWRD_REG, &dword, B_FALSE);
227}
228
229			void
230ef10_mcdi_request_copyout(
231	__in		efx_nic_t *enp,
232	__in		efx_mcdi_req_t *emrp)
233{
234#if EFSYS_OPT_MCDI_LOGGING
235	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
236#endif /* EFSYS_OPT_MCDI_LOGGING */
237	efx_dword_t hdr[2];
238	unsigned int hdr_len;
239	size_t bytes;
240
241	if (emrp->emr_out_buf == NULL)
242		return;
243
244	/* Read the command header to detect MCDI response format */
245	hdr_len = sizeof (hdr[0]);
246	ef10_mcdi_read_response(enp, &hdr[0], 0, hdr_len);
247	if (EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_CODE) == MC_CMD_V2_EXTN) {
248		/*
249		 * Read the actual payload length. The length given in the event
250		 * is only correct for responses with the V1 format.
251		 */
252		ef10_mcdi_read_response(enp, &hdr[1], hdr_len, sizeof (hdr[1]));
253		hdr_len += sizeof (hdr[1]);
254
255		emrp->emr_out_length_used = EFX_DWORD_FIELD(hdr[1],
256					    MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
257	}
258
259	/* Copy payload out into caller supplied buffer */
260	bytes = MIN(emrp->emr_out_length_used, emrp->emr_out_length);
261	ef10_mcdi_read_response(enp, emrp->emr_out_buf, hdr_len, bytes);
262
263#if EFSYS_OPT_MCDI_LOGGING
264	if (emtp->emt_logger != NULL) {
265		emtp->emt_logger(emtp->emt_context,
266		    EFX_LOG_MCDI_RESPONSE,
267		    &hdr, hdr_len,
268		    emrp->emr_out_buf, bytes);
269	}
270#endif /* EFSYS_OPT_MCDI_LOGGING */
271}
272
273	__checkReturn	boolean_t
274ef10_mcdi_poll_response(
275	__in		efx_nic_t *enp)
276{
277	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
278	efsys_mem_t *esmp = emtp->emt_dma_mem;
279	efx_dword_t hdr;
280
281	EFSYS_MEM_READD(esmp, 0, &hdr);
282	return (EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE) ? B_TRUE : B_FALSE);
283}
284
285			void
286ef10_mcdi_read_response(
287	__in		efx_nic_t *enp,
288	__out		void *bufferp,
289	__in		size_t offset,
290	__in		size_t length)
291{
292	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
293	efsys_mem_t *esmp = emtp->emt_dma_mem;
294	unsigned int pos;
295	efx_dword_t data;
296
297	for (pos = 0; pos < length; pos += sizeof (efx_dword_t)) {
298		EFSYS_MEM_READD(esmp, offset + pos, &data);
299		memcpy((uint8_t *)bufferp + pos, &data,
300		    MIN(sizeof (data), length - pos));
301	}
302}
303
304			efx_rc_t
305ef10_mcdi_poll_reboot(
306	__in		efx_nic_t *enp)
307{
308	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
309	efx_dword_t dword;
310	uint32_t old_status;
311	uint32_t new_status;
312	efx_rc_t rc;
313
314	old_status = emip->emi_mc_reboot_status;
315
316	/* Update MC reboot status word */
317	EFX_BAR_TBL_READD(enp, ER_DZ_BIU_MC_SFT_STATUS_REG, 0, &dword, B_FALSE);
318	new_status = dword.ed_u32[0];
319
320	/* MC has rebooted if the value has changed */
321	if (new_status != old_status) {
322		emip->emi_mc_reboot_status = new_status;
323
324		/*
325		 * FIXME: Ignore detected MC REBOOT for now.
326		 *
327		 * The Siena support for checking for MC reboot from status
328		 * flags is broken - see comments in siena_mcdi_poll_reboot().
329		 * As the generic MCDI code is shared the EF10 reboot
330		 * detection suffers similar problems.
331		 *
332		 * Do not report an error when the boot status changes until
333		 * this can be handled by common code drivers (and reworked to
334		 * support Siena too).
335		 */
336		if (B_FALSE) {
337			rc = EIO;
338			goto fail1;
339		}
340	}
341
342	return (0);
343
344fail1:
345	EFSYS_PROBE1(fail1, efx_rc_t, rc);
346
347	return (rc);
348}
349
350	__checkReturn	efx_rc_t
351ef10_mcdi_feature_supported(
352	__in		efx_nic_t *enp,
353	__in		efx_mcdi_feature_id_t id,
354	__out		boolean_t *supportedp)
355{
356	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
357	uint32_t privilege_mask = encp->enc_privilege_mask;
358	efx_rc_t rc;
359
360	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
361		    enp->en_family == EFX_FAMILY_MEDFORD);
362
363	/*
364	 * Use privilege mask state at MCDI attach.
365	 */
366
367	switch (id) {
368	case EFX_MCDI_FEATURE_FW_UPDATE:
369		/*
370		 * Admin privilege must be used prior to introduction of
371		 * specific flag.
372		 */
373		*supportedp =
374		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, ADMIN);
375		break;
376	case EFX_MCDI_FEATURE_LINK_CONTROL:
377		/*
378		 * Admin privilege used prior to introduction of
379		 * specific flag.
380		 */
381		*supportedp =
382		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, LINK) ||
383		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, ADMIN);
384		break;
385	case EFX_MCDI_FEATURE_MACADDR_CHANGE:
386		/*
387		 * Admin privilege must be used prior to introduction of
388		 * mac spoofing privilege (at v4.6), which is used up to
389		 * introduction of change mac spoofing privilege (at v4.7)
390		 */
391		*supportedp =
392		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, CHANGE_MAC) ||
393		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, MAC_SPOOFING) ||
394		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, ADMIN);
395		break;
396	case EFX_MCDI_FEATURE_MAC_SPOOFING:
397		/*
398		 * Admin privilege must be used prior to introduction of
399		 * mac spoofing privilege (at v4.6), which is used up to
400		 * introduction of mac spoofing TX privilege (at v4.7)
401		 */
402		*supportedp =
403		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, MAC_SPOOFING_TX) ||
404		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, MAC_SPOOFING) ||
405		    EFX_MCDI_HAVE_PRIVILEGE(privilege_mask, ADMIN);
406		break;
407	default:
408		rc = ENOTSUP;
409		goto fail1;
410		break;
411	}
412
413	return (0);
414
415fail1:
416	EFSYS_PROBE1(fail1, efx_rc_t, rc);
417
418	return (rc);
419}
420
421#endif	/* EFSYS_OPT_MCDI */
422
423#endif	/* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */
424