ef10_mcdi.c revision 291985
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 291985 2015-12-08 06:25:52Z 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	efx_dword_t hdr[2];
312	unsigned int hdr_len;
313	unsigned int data_len;
314	unsigned int seq;
315	unsigned int cmd;
316	int state;
317	efx_rc_t rc;
318
319	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
320
321	/* Serialise against post-watchdog efx_mcdi_ev* */
322	EFSYS_LOCK(enp->en_eslp, state);
323
324	EFSYS_ASSERT(emip->emi_pending_req != NULL);
325	EFSYS_ASSERT(!emip->emi_ev_cpl);
326	emrp = emip->emi_pending_req;
327
328	/* Check if a response is available */
329	if (hunt_mcdi_poll_response(enp) == B_FALSE) {
330		EFSYS_UNLOCK(enp->en_eslp, state);
331		return (B_FALSE);
332	}
333
334	/* Read the response header */
335	hdr_len = sizeof (hdr[0]);
336	hunt_mcdi_read_response(enp, &hdr[0], 0, hdr_len);
337
338	if (EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_CODE) == MC_CMD_V2_EXTN) {
339		hunt_mcdi_read_response(enp, &hdr[1], hdr_len, sizeof (hdr[1]));
340		hdr_len += sizeof (hdr[1]);
341
342		cmd = EFX_DWORD_FIELD(hdr[1], MC_CMD_V2_EXTN_IN_EXTENDED_CMD);
343		data_len =
344		    EFX_DWORD_FIELD(hdr[1], MC_CMD_V2_EXTN_IN_ACTUAL_LEN);
345	} else {
346		cmd = EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_CODE);
347		data_len = EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_DATALEN);
348	}
349
350	/* Request complete */
351	emip->emi_pending_req = NULL;
352	seq = (emip->emi_seq - 1) & EFX_MASK32(MCDI_HEADER_SEQ);
353
354	/* Check for synchronous reboot */
355	if (EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_ERROR) != 0 && data_len == 0) {
356		/* The MC has rebooted since the request was sent. */
357		EFSYS_SPIN(EFX_MCDI_STATUS_SLEEP_US);
358		hunt_mcdi_poll_reboot(enp);
359
360		EFSYS_UNLOCK(enp->en_eslp, state);
361		rc = EIO;
362		goto fail1;
363	}
364
365	/* Ensure stale MCDI requests fail after an MC reboot. */
366	emip->emi_new_epoch = B_FALSE;
367
368	EFSYS_UNLOCK(enp->en_eslp, state);
369
370	/* Check that the returned data is consistent */
371	if (cmd != emrp->emr_cmd ||
372	    EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_SEQ) != seq) {
373		/* Response is for a different request */
374		rc = EIO;
375		goto fail2;
376	}
377	if (EFX_DWORD_FIELD(hdr[0], MCDI_HEADER_ERROR)) {
378		efx_dword_t err[2];
379		unsigned int err_len = MIN(data_len, sizeof (err));
380		int err_code = MC_CMD_ERR_EPROTO;
381		int err_arg = 0;
382
383		/* Read error code (and arg num for MCDI v2 commands) */
384		hunt_mcdi_read_response(enp, &err[0], hdr_len, err_len);
385
386		if (err_len >= MC_CMD_ERR_CODE_OFST + sizeof (efx_dword_t))
387			err_code = EFX_DWORD_FIELD(err[0], EFX_DWORD_0);
388
389		if (err_len >= MC_CMD_ERR_ARG_OFST + sizeof (efx_dword_t))
390			err_arg = EFX_DWORD_FIELD(err[1], EFX_DWORD_0);
391
392#if EFSYS_OPT_MCDI_LOGGING
393		if (emtp->emt_logger != NULL) {
394			emtp->emt_logger(emtp->emt_context,
395			    EFX_LOG_MCDI_RESPONSE,
396			    &hdr, hdr_len,
397			    &err, err_len);
398		}
399#endif /* EFSYS_OPT_MCDI_LOGGING */
400
401		rc = efx_mcdi_request_errcode(err_code);
402		if (!emrp->emr_quiet) {
403			EFSYS_PROBE3(mcdi_err_arg, int, emrp->emr_cmd,
404			    int, err_code, int, err_arg);
405		}
406		goto fail3;
407
408	} else {
409		emrp->emr_out_length_used = data_len;
410		emrp->emr_rc = 0;
411		hunt_mcdi_request_copyout(enp, emrp);
412	}
413
414	goto out;
415
416fail3:
417	if (!emrp->emr_quiet)
418		EFSYS_PROBE(fail3);
419fail2:
420	if (!emrp->emr_quiet)
421		EFSYS_PROBE(fail2);
422fail1:
423	if (!emrp->emr_quiet)
424		EFSYS_PROBE1(fail1, efx_rc_t, rc);
425
426	/* Fill out error state */
427	emrp->emr_rc = rc;
428	emrp->emr_out_length_used = 0;
429
430	/* Reboot/Assertion */
431	if (rc == EIO || rc == EINTR)
432		efx_mcdi_raise_exception(enp, emrp, rc);
433
434out:
435	return (B_TRUE);
436}
437
438			efx_rc_t
439hunt_mcdi_poll_reboot(
440	__in		efx_nic_t *enp)
441{
442	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
443	efx_dword_t dword;
444	uint32_t old_status;
445	uint32_t new_status;
446	efx_rc_t rc;
447
448	old_status = emip->emi_mc_reboot_status;
449
450	/* Update MC reboot status word */
451	EFX_BAR_TBL_READD(enp, ER_DZ_BIU_MC_SFT_STATUS_REG, 0, &dword, B_FALSE);
452	new_status = dword.ed_u32[0];
453
454	/* MC has rebooted if the value has changed */
455	if (new_status != old_status) {
456		emip->emi_mc_reboot_status = new_status;
457
458		/*
459		 * FIXME: Ignore detected MC REBOOT for now.
460		 *
461		 * The Siena support for checking for MC reboot from status
462		 * flags is broken - see comments in siena_mcdi_poll_reboot().
463		 * As the generic MCDI code is shared the Huntington reboot
464		 * detection suffers similar problems.
465		 *
466		 * Do not report an error when the boot status changes until
467		 * this can be handled by common code drivers (and reworked to
468		 * support Siena too).
469		 */
470		if (B_FALSE) {
471			rc = EIO;
472			goto fail1;
473		}
474	}
475
476	return (0);
477
478fail1:
479	EFSYS_PROBE1(fail1, efx_rc_t, rc);
480
481	return (rc);
482}
483
484	__checkReturn	efx_rc_t
485hunt_mcdi_fw_update_supported(
486	__in		efx_nic_t *enp,
487	__out		boolean_t *supportedp)
488{
489	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
490
491	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
492
493	/*
494	 * Use privilege mask state at MCDI attach.
495	 * Admin privilege must be used prior to introduction of
496	 * specific flag.
497	 */
498	*supportedp = (encp->enc_privilege_mask &
499	    MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN)
500	    == MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN;
501
502	return (0);
503}
504
505	__checkReturn	efx_rc_t
506hunt_mcdi_macaddr_change_supported(
507	__in		efx_nic_t *enp,
508	__out		boolean_t *supportedp)
509{
510	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
511	uint32_t privilege_mask = encp->enc_privilege_mask;
512
513	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
514
515	/*
516	 * Use privilege mask state at MCDI attach.
517	 * Admin privilege must be used prior to introduction of
518	 * specific flag (at v4.6).
519	 */
520	*supportedp =
521	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING) ==
522	    MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING) ||
523	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN) ==
524	    MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN);
525
526	return (0);
527}
528
529	__checkReturn	efx_rc_t
530hunt_mcdi_link_control_supported(
531	__in		efx_nic_t *enp,
532	__out		boolean_t *supportedp)
533{
534	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
535	uint32_t privilege_mask = encp->enc_privilege_mask;
536
537	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_HUNTINGTON);
538
539	/*
540	 * Use privilege mask state at MCDI attach.
541	 * Admin privilege used prior to introduction of
542	 * specific flag.
543	 */
544	*supportedp =
545	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK) ==
546	    MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK) ||
547	    ((privilege_mask & MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN) ==
548	    MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN);
549
550	return (0);
551}
552
553#endif	/* EFSYS_OPT_MCDI */
554
555#endif	/* EFSYS_OPT_HUNTINGTON */
556