xref: /freebsd/sys/dev/sfxge/common/siena_mcdi.c (revision 807b6a646a0a0dbc258bf239468b5d9f901d1f92)
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 "efx.h"
35 #include "efx_impl.h"
36 
37 #if EFSYS_OPT_SIENA && EFSYS_OPT_MCDI
38 
39 #define	SIENA_MCDI_PDU(_emip)			\
40 	(((emip)->emi_port == 1)		\
41 	? MC_SMEM_P0_PDU_OFST >> 2		\
42 	: MC_SMEM_P1_PDU_OFST >> 2)
43 
44 #define	SIENA_MCDI_DOORBELL(_emip)		\
45 	(((emip)->emi_port == 1)		\
46 	? MC_SMEM_P0_DOORBELL_OFST >> 2		\
47 	: MC_SMEM_P1_DOORBELL_OFST >> 2)
48 
49 #define	SIENA_MCDI_STATUS(_emip)		\
50 	(((emip)->emi_port == 1)		\
51 	? MC_SMEM_P0_STATUS_OFST >> 2		\
52 	: MC_SMEM_P1_STATUS_OFST >> 2)
53 
54 
55 static			void
56 siena_mcdi_send_request(
57 	__in		efx_nic_t *enp,
58 	__in		void *hdrp,
59 	__in		size_t hdr_len,
60 	__in		void *sdup,
61 	__in		size_t sdu_len)
62 {
63 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
64 	efx_dword_t dword;
65 	unsigned int pdur;
66 	unsigned int dbr;
67 	unsigned int pos;
68 
69 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
70 
71 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
72 	pdur = SIENA_MCDI_PDU(emip);
73 	dbr = SIENA_MCDI_DOORBELL(emip);
74 
75 	/* Write the header */
76 	EFSYS_ASSERT3U(hdr_len, ==, sizeof (efx_dword_t));
77 	dword = *(efx_dword_t *)hdrp;
78 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, pdur, &dword, B_TRUE);
79 
80 	/* Write the payload */
81 	for (pos = 0; pos < sdu_len; pos += sizeof (efx_dword_t)) {
82 		dword = *(efx_dword_t *)((uint8_t *)sdup + pos);
83 		EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM,
84 		    pdur + 1 + (pos >> 2), &dword, B_FALSE);
85 	}
86 
87 	/* Ring the doorbell */
88 	EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xd004be11);
89 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, dbr, &dword, B_FALSE);
90 }
91 
92 			void
93 siena_mcdi_request_copyin(
94 	__in		efx_nic_t *enp,
95 	__in		efx_mcdi_req_t *emrp,
96 	__in		unsigned int seq,
97 	__in		boolean_t ev_cpl,
98 	__in		boolean_t new_epoch)
99 {
100 #if EFSYS_OPT_MCDI_LOGGING
101 	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
102 #endif
103 	efx_dword_t hdr;
104 	size_t hdr_len;
105 	unsigned int xflags;
106 
107 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
108 	_NOTE(ARGUNUSED(new_epoch))
109 
110 	xflags = 0;
111 	if (ev_cpl)
112 		xflags |= MCDI_HEADER_XFLAGS_EVREQ;
113 
114 	/* Construct the header */
115 	hdr_len = sizeof (hdr);
116 	EFX_POPULATE_DWORD_6(hdr,
117 			    MCDI_HEADER_CODE, emrp->emr_cmd,
118 			    MCDI_HEADER_RESYNC, 1,
119 			    MCDI_HEADER_DATALEN, emrp->emr_in_length,
120 			    MCDI_HEADER_SEQ, seq,
121 			    MCDI_HEADER_RESPONSE, 0,
122 			    MCDI_HEADER_XFLAGS, xflags);
123 
124 #if EFSYS_OPT_MCDI_LOGGING
125 	if (emtp->emt_logger != NULL) {
126 		emtp->emt_logger(emtp->emt_context, EFX_LOG_MCDI_REQUEST,
127 		    &hdr, sizeof (hdr),
128 		    emrp->emr_in_buf, emrp->emr_in_length);
129 	}
130 #endif /* EFSYS_OPT_MCDI_LOGGING */
131 
132 	siena_mcdi_send_request(enp, &hdr, hdr_len,
133 	    emrp->emr_in_buf, emrp->emr_in_length);
134 }
135 
136 			void
137 siena_mcdi_request_copyout(
138 	__in		efx_nic_t *enp,
139 	__in		efx_mcdi_req_t *emrp)
140 {
141 #if EFSYS_OPT_MCDI_LOGGING
142 	const efx_mcdi_transport_t *emtp = enp->en_mcdi.em_emtp;
143 	efx_dword_t hdr;
144 #endif
145 	size_t bytes = MIN(emrp->emr_out_length_used, emrp->emr_out_length);
146 
147 	/* Copy payload out if caller supplied buffer */
148 	if (emrp->emr_out_buf != NULL) {
149 		siena_mcdi_read_response(enp, emrp->emr_out_buf,
150 		    sizeof (efx_dword_t), bytes);
151 	}
152 
153 #if EFSYS_OPT_MCDI_LOGGING
154 	if (emtp->emt_logger != NULL) {
155 		siena_mcdi_read_response(enp, &hdr, 0, sizeof (hdr));
156 
157 		emtp->emt_logger(emtp->emt_context,
158 		    EFX_LOG_MCDI_RESPONSE,
159 		    &hdr, sizeof (hdr),
160 		    emrp->emr_out_buf, bytes);
161 	}
162 #endif /* EFSYS_OPT_MCDI_LOGGING */
163 }
164 
165 			efx_rc_t
166 siena_mcdi_poll_reboot(
167 	__in		efx_nic_t *enp)
168 {
169 #ifndef EFX_GRACEFUL_MC_REBOOT
170  	/*
171 	 * This function is not being used properly.
172 	 * Until its callers are fixed, it should always return 0.
173 	 */
174 	_NOTE(ARGUNUSED(enp))
175 	return (0);
176 #else
177 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
178 	unsigned int rebootr;
179 	efx_dword_t dword;
180 	uint32_t value;
181 
182 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
183 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
184 	rebootr = SIENA_MCDI_STATUS(emip);
185 
186 	EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM, rebootr, &dword, B_FALSE);
187 	value = EFX_DWORD_FIELD(dword, EFX_DWORD_0);
188 
189 	if (value == 0)
190 		return (0);
191 
192 	EFX_ZERO_DWORD(dword);
193 	EFX_BAR_TBL_WRITED(enp, FR_CZ_MC_TREG_SMEM, rebootr, &dword, B_FALSE);
194 
195 	if (value == MC_STATUS_DWORD_ASSERT)
196 		return (EINTR);
197 	else
198 		return (EIO);
199 #endif
200 }
201 
202 extern	__checkReturn	boolean_t
203 siena_mcdi_poll_response(
204 	__in		efx_nic_t *enp)
205 {
206 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
207 	efx_dword_t hdr;
208 	unsigned int pdur;
209 
210 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
211 	pdur = SIENA_MCDI_PDU(emip);
212 
213 	EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM, pdur, &hdr, B_FALSE);
214 	return (EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE) ? B_TRUE : B_FALSE);
215 }
216 
217 			void
218 siena_mcdi_read_response(
219 	__in			efx_nic_t *enp,
220 	__out_bcount(length)	void *bufferp,
221 	__in			size_t offset,
222 	__in			size_t length)
223 {
224 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
225 	unsigned int pdur;
226 	unsigned int pos;
227 	efx_dword_t data;
228 
229 	EFSYS_ASSERT(emip->emi_port == 1 || emip->emi_port == 2);
230 	pdur = SIENA_MCDI_PDU(emip);
231 
232 	for (pos = 0; pos < length; pos += sizeof (efx_dword_t)) {
233 		EFX_BAR_TBL_READD(enp, FR_CZ_MC_TREG_SMEM,
234 		    pdur + ((offset + pos) >> 2), &data, B_FALSE);
235 		memcpy((uint8_t *)bufferp + pos, &data,
236 		    MIN(sizeof (data), length - pos));
237 	}
238 }
239 
240 	__checkReturn	efx_rc_t
241 siena_mcdi_init(
242 	__in		efx_nic_t *enp,
243 	__in		const efx_mcdi_transport_t *mtp)
244 {
245 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
246 	efx_oword_t oword;
247 	unsigned int portnum;
248 	efx_rc_t rc;
249 
250 	EFSYS_ASSERT(enp->en_family == EFX_FAMILY_SIENA);
251 
252 	/* Determine the port number to use for MCDI */
253 	EFX_BAR_READO(enp, FR_AZ_CS_DEBUG_REG, &oword);
254 	portnum = EFX_OWORD_FIELD(oword, FRF_CZ_CS_PORT_NUM);
255 
256 	if (portnum == 0) {
257 		/* Presumably booted from ROM; only MCDI port 1 will work */
258 		emip->emi_port = 1;
259 	} else if (portnum <= 2) {
260 		emip->emi_port = portnum;
261 	} else {
262 		rc = EINVAL;
263 		goto fail1;
264 	}
265 
266 	/* Siena BootROM and firmware only support MCDIv1 */
267 	emip->emi_max_version = 1;
268 
269 	/*
270 	 * Wipe the atomic reboot status so subsequent MCDI requests succeed.
271 	 * BOOT_STATUS is preserved so eno_nic_probe() can boot out of the
272 	 * assertion handler.
273 	 */
274 	(void) siena_mcdi_poll_reboot(enp);
275 
276 	return (0);
277 
278 fail1:
279 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
280 
281 	return (rc);
282 }
283 
284 			void
285 siena_mcdi_fini(
286 	__in		efx_nic_t *enp)
287 {
288 }
289 
290 	__checkReturn	efx_rc_t
291 siena_mcdi_feature_supported(
292 	__in		efx_nic_t *enp,
293 	__in		efx_mcdi_feature_id_t id,
294 	__out		boolean_t *supportedp)
295 {
296 	efx_rc_t rc;
297 
298 	EFSYS_ASSERT3U(enp->en_family, ==, EFX_FAMILY_SIENA);
299 
300 	switch (id) {
301 	case EFX_MCDI_FEATURE_FW_UPDATE:
302 	case EFX_MCDI_FEATURE_LINK_CONTROL:
303 	case EFX_MCDI_FEATURE_MACADDR_CHANGE:
304 	case EFX_MCDI_FEATURE_MAC_SPOOFING:
305 		*supportedp = B_TRUE;
306 		break;
307 	default:
308 		rc = ENOTSUP;
309 		goto fail1;
310 		break;
311 	}
312 
313 	return (0);
314 
315 fail1:
316 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
317 
318 	return (rc);
319 }
320 
321 #endif	/* EFSYS_OPT_SIENA && EFSYS_OPT_MCDI */
322