xref: /freebsd/sys/dev/sfxge/common/siena_mcdi.c (revision bd81e07d2761cf1c13063eb49a5c0cb4a6951318)
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$");
33 
34 #include "efsys.h"
35 #include "efx.h"
36 #include "efx_impl.h"
37 
38 #if EFSYS_OPT_SIENA && EFSYS_OPT_MCDI
39 
40 #define	SIENA_MCDI_PDU(_emip)			\
41 	(((emip)->emi_port == 1)		\
42 	? MC_SMEM_P0_PDU_OFST >> 2		\
43 	: MC_SMEM_P1_PDU_OFST >> 2)
44 
45 #define	SIENA_MCDI_DOORBELL(_emip)		\
46 	(((emip)->emi_port == 1)		\
47 	? MC_SMEM_P0_DOORBELL_OFST >> 2		\
48 	: MC_SMEM_P1_DOORBELL_OFST >> 2)
49 
50 #define	SIENA_MCDI_STATUS(_emip)		\
51 	(((emip)->emi_port == 1)		\
52 	? MC_SMEM_P0_STATUS_OFST >> 2		\
53 	: MC_SMEM_P1_STATUS_OFST >> 2)
54 
55 
56 			void
57 siena_mcdi_request_copyin(
58 	__in		efx_nic_t *enp,
59 	__in		efx_mcdi_req_t *emrp,
60 	__in		unsigned int seq,
61 	__in		boolean_t ev_cpl,
62 	__in		boolean_t new_epoch)
63 {
64 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
65 	efx_dword_t dword;
66 	unsigned int xflags;
67 	unsigned int pdur;
68 	unsigned int dbr;
69 	unsigned int pos;
70 
71 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
72 	_NOTE(ARGUNUSED(new_epoch))
73 
74 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
75 	pdur = SIENA_MCDI_PDU(emip);
76 	dbr = SIENA_MCDI_DOORBELL(emip);
77 
78 	xflags = 0;
79 	if (ev_cpl)
80 		xflags |= MCDI_HEADER_XFLAGS_EVREQ;
81 
82 	/* Construct the header in shared memory */
83 	EFX_POPULATE_DWORD_6(dword,
84 			    MCDI_HEADER_CODE, emrp->emr_cmd,
85 			    MCDI_HEADER_RESYNC, 1,
86 			    MCDI_HEADER_DATALEN, emrp->emr_in_length,
87 			    MCDI_HEADER_SEQ, seq,
88 			    MCDI_HEADER_RESPONSE, 0,
89 			    MCDI_HEADER_XFLAGS, xflags);
90 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, pdur, &dword, B_TRUE);
91 
92 	for (pos = 0; pos < emrp->emr_in_length; pos += sizeof (efx_dword_t)) {
93 		memcpy(&dword, MCDI_IN(*emrp, efx_dword_t, pos),
94 		    MIN(sizeof (dword), emrp->emr_in_length - pos));
95 		EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM,
96 		    pdur + 1 + (pos >> 2), &dword, B_FALSE);
97 	}
98 
99 	/* Ring the doorbell */
100 	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xd004be11);
101 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, dbr, &dword, B_FALSE);
102 }
103 
104 			void
105 siena_mcdi_request_copyout(
106 	__in		efx_nic_t *enp,
107 	__in		efx_mcdi_req_t *emrp)
108 {
109 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
110 	unsigned int pos;
111 	unsigned int pdur;
112 	efx_dword_t data;
113 
114 	pdur = SIENA_MCDI_PDU(emip);
115 
116 	/* Copy payload out if caller supplied buffer */
117 	if (emrp->emr_out_buf != NULL) {
118 		size_t bytes = MIN(emrp->emr_out_length_used,
119 				    emrp->emr_out_length);
120 		for (pos = 0; pos < bytes; pos += sizeof (efx_dword_t)) {
121 			EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM,
122 			    pdur + 1 + (pos >> 2), &data, B_FALSE);
123 			memcpy(MCDI_OUT(*emrp, efx_dword_t, pos), &data,
124 			    MIN(sizeof (data), bytes - pos));
125 		}
126 	}
127 }
128 
129 			int
130 siena_mcdi_poll_reboot(
131 	__in		efx_nic_t *enp)
132 {
133 #ifndef EFX_GRACEFUL_MC_REBOOT
134  	/*
135 	 * This function is not being used properly.
136 	 * Until its callers are fixed, it should always return 0.
137 	 */
138 	_NOTE(ARGUNUSED(enp))
139 	return (0);
140 #else
141 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
142 	unsigned int rebootr;
143 	efx_dword_t dword;
144 	uint32_t value;
145 
146 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
147 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
148 	rebootr = SIENA_MCDI_STATUS(emip);
149 
150 	EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM, rebootr, &dword, B_FALSE);
151 	value = EFX_DWORD_FIELD(dword, EFX_DWORD_0);
152 
153 	if (value == 0)
154 		return (0);
155 
156 	EFX_ZERO_DWORD(dword);
157 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, rebootr, &dword, B_FALSE);
158 
159 	if (value == MC_STATUS_DWORD_ASSERT)
160 		return (EINTR);
161 	else
162 		return (EIO);
163 #endif
164 }
165 
166 	__checkReturn	boolean_t
167 siena_mcdi_request_poll(
168 	__in		efx_nic_t *enp)
169 {
170 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
171 	efx_mcdi_req_t *emrp;
172 	efx_dword_t dword;
173 	unsigned int pdur;
174 	unsigned int seq;
175 	unsigned int length;
176 	int state;
177 	int rc;
178 
179 	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
180 
181 	/* Serialise against post-watchdog efx_mcdi_ev* */
182 	EFSYS_LOCK(enp->en_eslp, state);
183 
184 	EFSYS_ASSERT(emip->emi_pending_req != NULL);
185 	EFSYS_ASSERT(!emip->emi_ev_cpl);
186 	emrp = emip->emi_pending_req;
187 
188 	/* Check for reboot atomically w.r.t efx_mcdi_request_start */
189 	if (emip->emi_poll_cnt++ == 0) {
190 		if ((rc = siena_mcdi_poll_reboot(enp)) != 0) {
191 			emip->emi_pending_req = NULL;
192 			EFSYS_UNLOCK(enp->en_eslp, state);
193 
194 			goto fail1;
195 		}
196 	}
197 
198 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
199 	pdur = SIENA_MCDI_PDU(emip);
200 
201 	/* Read the command header */
202 	EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM, pdur, &dword, B_FALSE);
203 	if (EFX_DWORD_FIELD(dword, MCDI_HEADER_RESPONSE) == 0) {
204 		EFSYS_UNLOCK(enp->en_eslp, state);
205 		return (B_FALSE);
206 	}
207 
208 	/* Request complete */
209 	emip->emi_pending_req = NULL;
210 	seq = (emip->emi_seq - 1) & EFX_MASK32(MCDI_HEADER_SEQ);
211 
212 	/* Check for synchronous reboot */
213 	if (EFX_DWORD_FIELD(dword, MCDI_HEADER_ERROR) != 0 &&
214 	    EFX_DWORD_FIELD(dword, MCDI_HEADER_DATALEN) == 0) {
215 		/* Consume status word */
216 		EFSYS_SPIN(EFX_MCDI_STATUS_SLEEP_US);
217 		siena_mcdi_poll_reboot(enp);
218 		EFSYS_UNLOCK(enp->en_eslp, state);
219 		rc = EIO;
220 		goto fail2;
221 	}
222 
223 	EFSYS_UNLOCK(enp->en_eslp, state);
224 
225 	/* Check that the returned data is consistent */
226 	if (EFX_DWORD_FIELD(dword, MCDI_HEADER_CODE) != emrp->emr_cmd ||
227 	    EFX_DWORD_FIELD(dword, MCDI_HEADER_SEQ) != seq) {
228 		/* Response is for a different request */
229 		rc = EIO;
230 		goto fail3;
231 	}
232 
233 	length = EFX_DWORD_FIELD(dword, MCDI_HEADER_DATALEN);
234 	if (EFX_DWORD_FIELD(dword, MCDI_HEADER_ERROR)) {
235 		efx_dword_t errdword;
236 		int errcode;
237 
238 		EFSYS_ASSERT3U(length, ==, 4);
239 		EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM,
240 		    pdur + 1 + (MC_CMD_ERR_CODE_OFST >> 2),
241 		    &errdword, B_FALSE);
242 		errcode = EFX_DWORD_FIELD(errdword, EFX_DWORD_0);
243 		rc = efx_mcdi_request_errcode(errcode);
244 		if (!emrp->emr_quiet) {
245 			EFSYS_PROBE2(mcdi_err, int, emrp->emr_cmd,
246 			    int, errcode);
247 		}
248 		goto fail4;
249 
250 	} else {
251 		emrp->emr_out_length_used = length;
252 		emrp->emr_rc = 0;
253 		siena_mcdi_request_copyout(enp, emrp);
254 	}
255 
256 	goto out;
257 
258 fail4:
259 	if (!emrp->emr_quiet)
260 		EFSYS_PROBE(fail4);
261 fail3:
262 	if (!emrp->emr_quiet)
263 		EFSYS_PROBE(fail3);
264 fail2:
265 	if (!emrp->emr_quiet)
266 		EFSYS_PROBE(fail2);
267 fail1:
268 	if (!emrp->emr_quiet)
269 		EFSYS_PROBE1(fail1, int, rc);
270 
271 	/* Fill out error state */
272 	emrp->emr_rc = rc;
273 	emrp->emr_out_length_used = 0;
274 
275 	/* Reboot/Assertion */
276 	if (rc == EIO || rc == EINTR)
277 		efx_mcdi_raise_exception(enp, emrp, rc);
278 
279 out:
280 	return (B_TRUE);
281 }
282 
283 	__checkReturn	int
284 siena_mcdi_init(
285 	__in		efx_nic_t *enp,
286 	__in		const efx_mcdi_transport_t *mtp)
287 {
288 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
289 	efx_oword_t oword;
290 	unsigned int portnum;
291 	int rc;
292 
293 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
294 
295 	/* Determine the port number to use for MCDI */
296 	EFX_BAR_READO(enp, FR_AZ_CS_DEBUG_REG, &oword);
297 	portnum = EFX_OWORD_FIELD(oword, FRF_CZ_CS_PORT_NUM);
298 
299 	if (portnum == 0) {
300 		/* Presumably booted from ROM; only MCDI port 1 will work */
301 		emip->emi_port = 1;
302 	} else if (portnum <= 2) {
303 		emip->emi_port = portnum;
304 	} else {
305 		rc = EINVAL;
306 		goto fail1;
307 	}
308 
309 	/*
310 	 * Wipe the atomic reboot status so subsequent MCDI requests succeed.
311 	 * BOOT_STATUS is preserved so eno_nic_probe() can boot out of the
312 	 * assertion handler.
313 	 */
314 	(void) siena_mcdi_poll_reboot(enp);
315 
316 	return (0);
317 
318 fail1:
319 	EFSYS_PROBE1(fail1, int, rc);
320 
321 	return (rc);
322 }
323 
324 			void
325 siena_mcdi_fini(
326 	__in		efx_nic_t *enp)
327 {
328 }
329 
330 	__checkReturn	int
331 siena_mcdi_fw_update_supported(
332 	__in		efx_nic_t *enp,
333 	__out		boolean_t *supportedp)
334 {
335 	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
336 
337 	*supportedp = B_TRUE;
338 
339 	return (0);
340 }
341 
342 	__checkReturn	int
343 siena_mcdi_macaddr_change_supported(
344 	__in		efx_nic_t *enp,
345 	__out		boolean_t *supportedp)
346 {
347 	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
348 
349 	*supportedp = B_TRUE;
350 
351 	return (0);
352 }
353 
354 #endif	/* EFSYS_OPT_SIENA && EFSYS_OPT_MCDI */
355