xref: /freebsd/sys/dev/sfxge/common/siena_mcdi.c (revision c6a33c8e88c5684876e670c8189d03ad25108d8a)
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 #if EFSYS_OPT_MCDI_LOGGING
65 	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
66 #endif
67 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
68 	efx_dword_t hdr;
69 	efx_dword_t dword;
70 	unsigned int xflags;
71 	unsigned int pdur;
72 	unsigned int dbr;
73 	unsigned int pos;
74 
75 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
76 	_NOTE(ARGUNUSED(new_epoch))
77 
78 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
79 	pdur = SIENA_MCDI_PDU(emip);
80 	dbr = SIENA_MCDI_DOORBELL(emip);
81 
82 	xflags = 0;
83 	if (ev_cpl)
84 		xflags |= MCDI_HEADER_XFLAGS_EVREQ;
85 
86 	/* Construct the header in shared memory */
87 	EFX_POPULATE_DWORD_6(hdr,
88 			    MCDI_HEADER_CODE, emrp->emr_cmd,
89 			    MCDI_HEADER_RESYNC, 1,
90 			    MCDI_HEADER_DATALEN, emrp->emr_in_length,
91 			    MCDI_HEADER_SEQ, seq,
92 			    MCDI_HEADER_RESPONSE, 0,
93 			    MCDI_HEADER_XFLAGS, xflags);
94 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, pdur, &hdr, B_TRUE);
95 
96 #if EFSYS_OPT_MCDI_LOGGING
97 	if (emtp->emt_logger != NULL) {
98 		emtp->emt_logger(emtp->emt_context, EFX_LOG_MCDI_REQUEST,
99 		    &hdr, sizeof (hdr),
100 		    emrp->emr_in_buf, emrp->emr_in_length);
101 	}
102 #endif /* EFSYS_OPT_MCDI_LOGGING */
103 
104 	/* Construct the payload */
105 	for (pos = 0; pos < emrp->emr_in_length; pos += sizeof (efx_dword_t)) {
106 		memcpy(&dword, MCDI_IN(*emrp, efx_dword_t, pos),
107 		    MIN(sizeof (dword), emrp->emr_in_length - pos));
108 		EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM,
109 		    pdur + 1 + (pos >> 2), &dword, B_FALSE);
110 	}
111 
112 	/* Ring the doorbell */
113 	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xd004be11);
114 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, dbr, &dword, B_FALSE);
115 }
116 
117 			void
118 siena_mcdi_request_copyout(
119 	__in		efx_nic_t *enp,
120 	__in		efx_mcdi_req_t *emrp)
121 {
122 #if EFSYS_OPT_MCDI_LOGGING
123 	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
124 	efx_dword_t hdr;
125 #endif
126 	size_t bytes = MIN(emrp->emr_out_length_used, emrp->emr_out_length);
127 
128 	/* Copy payload out if caller supplied buffer */
129 	if (emrp->emr_out_buf != NULL) {
130 		siena_mcdi_read_response(enp, emrp->emr_out_buf,
131 		    sizeof (efx_dword_t), bytes);
132 	}
133 
134 #if EFSYS_OPT_MCDI_LOGGING
135 	if (emtp->emt_logger != NULL) {
136 		siena_mcdi_read_response(enp, &hdr, 0, sizeof (hdr));
137 
138 		emtp->emt_logger(emtp->emt_context,
139 		    EFX_LOG_MCDI_RESPONSE,
140 		    &hdr, sizeof (hdr),
141 		    emrp->emr_out_buf, bytes);
142 	}
143 #endif /* EFSYS_OPT_MCDI_LOGGING */
144 }
145 
146 			efx_rc_t
147 siena_mcdi_poll_reboot(
148 	__in		efx_nic_t *enp)
149 {
150 #ifndef EFX_GRACEFUL_MC_REBOOT
151  	/*
152 	 * This function is not being used properly.
153 	 * Until its callers are fixed, it should always return 0.
154 	 */
155 	_NOTE(ARGUNUSED(enp))
156 	return (0);
157 #else
158 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
159 	unsigned int rebootr;
160 	efx_dword_t dword;
161 	uint32_t value;
162 
163 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
164 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
165 	rebootr = SIENA_MCDI_STATUS(emip);
166 
167 	EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM, rebootr, &dword, B_FALSE);
168 	value = EFX_DWORD_FIELD(dword, EFX_DWORD_0);
169 
170 	if (value == 0)
171 		return (0);
172 
173 	EFX_ZERO_DWORD(dword);
174 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, rebootr, &dword, B_FALSE);
175 
176 	if (value == MC_STATUS_DWORD_ASSERT)
177 		return (EINTR);
178 	else
179 		return (EIO);
180 #endif
181 }
182 
183 static	__checkReturn	boolean_t
184 siena_mcdi_poll_response(
185 	__in		efx_nic_t *enp)
186 {
187 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
188 	efx_dword_t hdr;
189 	unsigned int pdur;
190 
191 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
192 	pdur = SIENA_MCDI_PDU(emip);
193 
194 	EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM, pdur, &hdr, B_FALSE);
195 	return (EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE) ? B_TRUE : B_FALSE);
196 }
197 
198 			void
199 siena_mcdi_read_response(
200 	__in		efx_nic_t *enp,
201 	__out		void *bufferp,
202 	__in		size_t offset,
203 	__in		size_t length)
204 {
205 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
206 	unsigned int pdur;
207 	unsigned int pos;
208 	efx_dword_t data;
209 
210 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
211 	pdur = SIENA_MCDI_PDU(emip);
212 
213 	for (pos = 0; pos < length; pos += sizeof (efx_dword_t)) {
214 		EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM,
215 		    pdur + ((offset + pos) >> 2), &data, B_FALSE);
216 		memcpy((uint8_t *)bufferp + pos, &data,
217 		    MIN(sizeof (data), length - pos));
218 	}
219 }
220 
221 	__checkReturn	boolean_t
222 siena_mcdi_request_poll(
223 	__in		efx_nic_t *enp)
224 {
225 #if EFSYS_OPT_MCDI_LOGGING
226 	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
227 #endif
228 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
229 	efx_mcdi_req_t *emrp;
230 	efx_dword_t hdr;
231 	unsigned int hdr_len;
232 	unsigned int data_len;
233 	unsigned int seq;
234 	int state;
235 	efx_rc_t rc;
236 
237 	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
238 
239 	/* Serialise against post-watchdog efx_mcdi_ev* */
240 	EFSYS_LOCK(enp->en_eslp, state);
241 
242 	EFSYS_ASSERT(emip->emi_pending_req != NULL);
243 	EFSYS_ASSERT(!emip->emi_ev_cpl);
244 	emrp = emip->emi_pending_req;
245 
246 	/* Check for reboot atomically w.r.t efx_mcdi_request_start */
247 	if (emip->emi_poll_cnt++ == 0) {
248 		if ((rc = siena_mcdi_poll_reboot(enp)) != 0) {
249 			emip->emi_pending_req = NULL;
250 			EFSYS_UNLOCK(enp->en_eslp, state);
251 
252 			goto fail1;
253 		}
254 	}
255 
256 	/* Check if a response is available */
257 	if (siena_mcdi_poll_response(enp) == B_FALSE) {
258 		EFSYS_UNLOCK(enp->en_eslp, state);
259 		return (B_FALSE);
260 	}
261 
262 	/* Read the response header */
263 	hdr_len = sizeof (hdr);
264 	siena_mcdi_read_response(enp, &hdr, 0, hdr_len);
265 
266 	/* Request complete */
267 	emip->emi_pending_req = NULL;
268 	seq = (emip->emi_seq - 1) & EFX_MASK32(MCDI_HEADER_SEQ);
269 
270 	/* Check for synchronous reboot */
271 	if (EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR) != 0 &&
272 	    EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN) == 0) {
273 		/* Consume status word */
274 		EFSYS_SPIN(EFX_MCDI_STATUS_SLEEP_US);
275 		siena_mcdi_poll_reboot(enp);
276 		EFSYS_UNLOCK(enp->en_eslp, state);
277 		rc = EIO;
278 		goto fail2;
279 	}
280 
281 	EFSYS_UNLOCK(enp->en_eslp, state);
282 
283 	/* Check that the returned data is consistent */
284 	if (EFX_DWORD_FIELD(hdr, MCDI_HEADER_CODE) != emrp->emr_cmd ||
285 	    EFX_DWORD_FIELD(hdr, MCDI_HEADER_SEQ) != seq) {
286 		/* Response is for a different request */
287 		rc = EIO;
288 		goto fail3;
289 	}
290 
291 	data_len = EFX_DWORD_FIELD(hdr, MCDI_HEADER_DATALEN);
292 	if (EFX_DWORD_FIELD(hdr, MCDI_HEADER_ERROR)) {
293 		efx_dword_t err;
294 		int err_code = MC_CMD_ERR_EPROTO;
295 		unsigned int err_len = MIN(data_len, sizeof (err));
296 
297 		/* Read error code */
298 		siena_mcdi_read_response(enp, &err, hdr_len, err_len);
299 
300 		if (err_len >= MC_CMD_ERR_CODE_OFST + sizeof (efx_dword_t))
301 			err_code = EFX_DWORD_FIELD(err, EFX_DWORD_0);
302 
303 #if EFSYS_OPT_MCDI_LOGGING
304 		if (emtp->emt_logger != NULL) {
305 			emtp->emt_logger(emtp->emt_context,
306 			    EFX_LOG_MCDI_RESPONSE,
307 			    &hdr, hdr_len,
308 			    &err, err_len);
309 		}
310 #endif /* EFSYS_OPT_MCDI_LOGGING */
311 
312 		rc = efx_mcdi_request_errcode(err_code);
313 		if (!emrp->emr_quiet) {
314 			EFSYS_PROBE2(mcdi_err, int, emrp->emr_cmd,
315 			    int, err_code);
316 		}
317 		goto fail4;
318 
319 	} else {
320 		emrp->emr_out_length_used = data_len;
321 		emrp->emr_rc = 0;
322 		siena_mcdi_request_copyout(enp, emrp);
323 	}
324 
325 	goto out;
326 
327 fail4:
328 	if (!emrp->emr_quiet)
329 		EFSYS_PROBE(fail4);
330 fail3:
331 	if (!emrp->emr_quiet)
332 		EFSYS_PROBE(fail3);
333 fail2:
334 	if (!emrp->emr_quiet)
335 		EFSYS_PROBE(fail2);
336 fail1:
337 	if (!emrp->emr_quiet)
338 		EFSYS_PROBE1(fail1, efx_rc_t, rc);
339 
340 	/* Fill out error state */
341 	emrp->emr_rc = rc;
342 	emrp->emr_out_length_used = 0;
343 
344 	/* Reboot/Assertion */
345 	if (rc == EIO || rc == EINTR)
346 		efx_mcdi_raise_exception(enp, emrp, rc);
347 
348 out:
349 	return (B_TRUE);
350 }
351 
352 	__checkReturn	efx_rc_t
353 siena_mcdi_init(
354 	__in		efx_nic_t *enp,
355 	__in		const efx_mcdi_transport_t *mtp)
356 {
357 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
358 	efx_oword_t oword;
359 	unsigned int portnum;
360 	efx_rc_t rc;
361 
362 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
363 
364 	/* Determine the port number to use for MCDI */
365 	EFX_BAR_READO(enp, FR_AZ_CS_DEBUG_REG, &oword);
366 	portnum = EFX_OWORD_FIELD(oword, FRF_CZ_CS_PORT_NUM);
367 
368 	if (portnum == 0) {
369 		/* Presumably booted from ROM; only MCDI port 1 will work */
370 		emip->emi_port = 1;
371 	} else if (portnum <= 2) {
372 		emip->emi_port = portnum;
373 	} else {
374 		rc = EINVAL;
375 		goto fail1;
376 	}
377 
378 	/*
379 	 * Wipe the atomic reboot status so subsequent MCDI requests succeed.
380 	 * BOOT_STATUS is preserved so eno_nic_probe() can boot out of the
381 	 * assertion handler.
382 	 */
383 	(void) siena_mcdi_poll_reboot(enp);
384 
385 	return (0);
386 
387 fail1:
388 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
389 
390 	return (rc);
391 }
392 
393 			void
394 siena_mcdi_fini(
395 	__in		efx_nic_t *enp)
396 {
397 }
398 
399 	__checkReturn	efx_rc_t
400 siena_mcdi_fw_update_supported(
401 	__in		efx_nic_t *enp,
402 	__out		boolean_t *supportedp)
403 {
404 	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
405 
406 	*supportedp = B_TRUE;
407 
408 	return (0);
409 }
410 
411 	__checkReturn	efx_rc_t
412 siena_mcdi_macaddr_change_supported(
413 	__in		efx_nic_t *enp,
414 	__out		boolean_t *supportedp)
415 {
416 	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
417 
418 	*supportedp = B_TRUE;
419 
420 	return (0);
421 }
422 
423 	__checkReturn	efx_rc_t
424 siena_mcdi_link_control_supported(
425 	__in		efx_nic_t *enp,
426 	__out		boolean_t *supportedp)
427 {
428 	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
429 
430 	*supportedp = B_TRUE;
431 
432 	return (0);
433 }
434 
435 #endif	/* EFSYS_OPT_SIENA && EFSYS_OPT_MCDI */
436