xref: /freebsd/sys/dev/sfxge/common/medford_nic.c (revision 243e928310d073338c5ec089f0dce238a80b9866)
1 /*-
2  * Copyright (c) 2015-2016 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 
38 #if EFSYS_OPT_MEDFORD
39 
40 static	__checkReturn	efx_rc_t
41 efx_mcdi_get_rxdp_config(
42 	__in		efx_nic_t *enp,
43 	__out		uint32_t *end_paddingp)
44 {
45 	efx_mcdi_req_t req;
46 	uint8_t payload[MAX(MC_CMD_GET_RXDP_CONFIG_IN_LEN,
47 			    MC_CMD_GET_RXDP_CONFIG_OUT_LEN)];
48 	uint32_t end_padding;
49 	efx_rc_t rc;
50 
51 	memset(payload, 0, sizeof (payload));
52 	req.emr_cmd = MC_CMD_GET_RXDP_CONFIG;
53 	req.emr_in_buf = payload;
54 	req.emr_in_length = MC_CMD_GET_RXDP_CONFIG_IN_LEN;
55 	req.emr_out_buf = payload;
56 	req.emr_out_length = MC_CMD_GET_RXDP_CONFIG_OUT_LEN;
57 
58 	efx_mcdi_execute(enp, &req);
59 	if (req.emr_rc != 0) {
60 		rc = req.emr_rc;
61 		goto fail1;
62 	}
63 
64 	if (MCDI_OUT_DWORD_FIELD(req, GET_RXDP_CONFIG_OUT_DATA,
65 				    GET_RXDP_CONFIG_OUT_PAD_HOST_DMA) == 0) {
66 		/* RX DMA end padding is disabled */
67 		end_padding = 0;
68 	} else {
69 		switch(MCDI_OUT_DWORD_FIELD(req, GET_RXDP_CONFIG_OUT_DATA,
70 					    GET_RXDP_CONFIG_OUT_PAD_HOST_LEN)) {
71 		case MC_CMD_SET_RXDP_CONFIG_IN_PAD_HOST_64:
72 			end_padding = 64;
73 			break;
74 		case MC_CMD_SET_RXDP_CONFIG_IN_PAD_HOST_128:
75 			end_padding = 128;
76 			break;
77 		case MC_CMD_SET_RXDP_CONFIG_IN_PAD_HOST_256:
78 			end_padding = 256;
79 			break;
80 		default:
81 			rc = ENOTSUP;
82 			goto fail2;
83 		}
84 	}
85 
86 	*end_paddingp = end_padding;
87 
88 	return (0);
89 
90 fail2:
91 	EFSYS_PROBE(fail2);
92 fail1:
93 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
94 
95 	return (rc);
96 }
97 
98 static	__checkReturn	efx_rc_t
99 medford_nic_get_required_pcie_bandwidth(
100 	__in		efx_nic_t *enp,
101 	__out		uint32_t *bandwidth_mbpsp)
102 {
103 	uint32_t port_modes;
104 	uint32_t current_mode;
105 	uint32_t bandwidth;
106 	efx_rc_t rc;
107 
108 	if ((rc = efx_mcdi_get_port_modes(enp, &port_modes,
109 				    &current_mode)) != 0) {
110 		/* No port mode info available. */
111 		bandwidth = 0;
112 		goto out;
113 	}
114 
115 	if ((rc = ef10_nic_get_port_mode_bandwidth(current_mode,
116 						    &bandwidth)) != 0)
117 		goto fail1;
118 
119 out:
120 	*bandwidth_mbpsp = bandwidth;
121 
122 	return (0);
123 
124 fail1:
125 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
126 
127 	return (rc);
128 }
129 
130 	__checkReturn	efx_rc_t
131 medford_board_cfg(
132 	__in		efx_nic_t *enp)
133 {
134 	efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
135 	efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
136 	uint8_t mac_addr[6] = { 0 };
137 	uint32_t board_type = 0;
138 	ef10_link_state_t els;
139 	efx_port_t *epp = &(enp->en_port);
140 	uint32_t port;
141 	uint32_t pf;
142 	uint32_t vf;
143 	uint32_t mask;
144 	uint32_t sysclk, dpcpu_clk;
145 	uint32_t base, nvec;
146 	uint32_t end_padding;
147 	uint32_t bandwidth;
148 	efx_rc_t rc;
149 
150 	/*
151 	 * FIXME: Likely to be incomplete and incorrect.
152 	 * Parts of this should be shared with Huntington.
153 	 */
154 
155 	if ((rc = efx_mcdi_get_port_assignment(enp, &port)) != 0)
156 		goto fail1;
157 
158 	/*
159 	 * NOTE: The MCDI protocol numbers ports from zero.
160 	 * The common code MCDI interface numbers ports from one.
161 	 */
162 	emip->emi_port = port + 1;
163 
164 	if ((rc = ef10_external_port_mapping(enp, port,
165 		    &encp->enc_external_port)) != 0)
166 		goto fail2;
167 
168 	/*
169 	 * Get PCIe function number from firmware (used for
170 	 * per-function privilege and dynamic config info).
171 	 *  - PCIe PF: pf = PF number, vf = 0xffff.
172 	 *  - PCIe VF: pf = parent PF, vf = VF number.
173 	 */
174 	if ((rc = efx_mcdi_get_function_info(enp, &pf, &vf)) != 0)
175 		goto fail3;
176 
177 	encp->enc_pf = pf;
178 	encp->enc_vf = vf;
179 
180 	/* MAC address for this function */
181 	if (EFX_PCI_FUNCTION_IS_PF(encp)) {
182 		rc = efx_mcdi_get_mac_address_pf(enp, mac_addr);
183 #if EFSYS_OPT_ALLOW_UNCONFIGURED_NIC
184 		/* Disable static config checking for Medford NICs, ONLY
185 		 * for manufacturing test and setup at the factory, to
186 		 * allow the static config to be installed.
187 		 */
188 #else /* EFSYS_OPT_ALLOW_UNCONFIGURED_NIC */
189 		if ((rc == 0) && (mac_addr[0] & 0x02)) {
190 			/*
191 			 * If the static config does not include a global MAC
192 			 * address pool then the board may return a locally
193 			 * administered MAC address (this should only happen on
194 			 * incorrectly programmed boards).
195 			 */
196 			rc = EINVAL;
197 		}
198 #endif /* EFSYS_OPT_ALLOW_UNCONFIGURED_NIC */
199 	} else {
200 		rc = efx_mcdi_get_mac_address_vf(enp, mac_addr);
201 	}
202 	if (rc != 0)
203 		goto fail4;
204 
205 	EFX_MAC_ADDR_COPY(encp->enc_mac_addr, mac_addr);
206 
207 	/* Board configuration */
208 	rc = efx_mcdi_get_board_cfg(enp, &board_type, NULL, NULL);
209 	if (rc != 0) {
210 		/* Unprivileged functions may not be able to read board cfg */
211 		if (rc == EACCES)
212 			board_type = 0;
213 		else
214 			goto fail5;
215 	}
216 
217 	encp->enc_board_type = board_type;
218 	encp->enc_clk_mult = 1; /* not used for Medford */
219 
220 	/* Fill out fields in enp->en_port and enp->en_nic_cfg from MCDI */
221 	if ((rc = efx_mcdi_get_phy_cfg(enp)) != 0)
222 		goto fail6;
223 
224 	/* Obtain the default PHY advertised capabilities */
225 	if ((rc = ef10_phy_get_link(enp, &els)) != 0)
226 		goto fail7;
227 	epp->ep_default_adv_cap_mask = els.els_adv_cap_mask;
228 	epp->ep_adv_cap_mask = els.els_adv_cap_mask;
229 
230 	if (EFX_PCI_FUNCTION_IS_VF(encp)) {
231 		/*
232 		 * Interrupt testing does not work for VFs. See bug50084.
233 		 * FIXME: Does this still  apply to Medford?
234 		 */
235 		encp->enc_bug41750_workaround = B_TRUE;
236 	}
237 
238 	/* Chained multicast is always enabled on Medford */
239 	encp->enc_bug26807_workaround = B_TRUE;
240 
241 	/* Get clock frequencies (in MHz). */
242 	if ((rc = efx_mcdi_get_clock(enp, &sysclk, &dpcpu_clk)) != 0)
243 		goto fail8;
244 
245 	/*
246 	 * The Medford timer quantum is 1536 dpcpu_clk cycles, documented for
247 	 * the EV_TMR_VAL field of EV_TIMER_TBL. Scale for MHz and ns units.
248 	 */
249 	encp->enc_evq_timer_quantum_ns = 1536000UL / dpcpu_clk; /* 1536 cycles */
250 	encp->enc_evq_timer_max_us = (encp->enc_evq_timer_quantum_ns <<
251 		    FRF_CZ_TC_TIMER_VAL_WIDTH) / 1000;
252 
253 	/* Check capabilities of running datapath firmware */
254 	if ((rc = ef10_get_datapath_caps(enp)) != 0)
255 	    goto fail9;
256 
257 	/* Alignment for receive packet DMA buffers */
258 	encp->enc_rx_buf_align_start = 1;
259 
260 	/* Get the RX DMA end padding alignment configuration */
261 	if ((rc = efx_mcdi_get_rxdp_config(enp, &end_padding)) != 0)
262 		goto fail10;
263 	encp->enc_rx_buf_align_end = end_padding;
264 
265 	/* Alignment for WPTR updates */
266 	encp->enc_rx_push_align = EF10_RX_WPTR_ALIGN;
267 
268 	/*
269 	 * Set resource limits for MC_CMD_ALLOC_VIS. Note that we cannot use
270 	 * MC_CMD_GET_RESOURCE_LIMITS here as that reports the available
271 	 * resources (allocated to this PCIe function), which is zero until
272 	 * after we have allocated VIs.
273 	 */
274 	encp->enc_evq_limit = 1024;
275 	encp->enc_rxq_limit = EFX_RXQ_LIMIT_TARGET;
276 	encp->enc_txq_limit = EFX_TXQ_LIMIT_TARGET;
277 
278 	encp->enc_buftbl_limit = 0xFFFFFFFF;
279 
280 	encp->enc_piobuf_limit = MEDFORD_PIOBUF_NBUFS;
281 	encp->enc_piobuf_size = MEDFORD_PIOBUF_SIZE;
282 	encp->enc_piobuf_min_alloc_size = MEDFORD_MIN_PIO_ALLOC_SIZE;
283 
284 	/*
285 	 * Get the current privilege mask. Note that this may be modified
286 	 * dynamically, so this value is informational only. DO NOT use
287 	 * the privilege mask to check for sufficient privileges, as that
288 	 * can result in time-of-check/time-of-use bugs.
289 	 */
290 	if ((rc = ef10_get_privilege_mask(enp, &mask)) != 0)
291 		goto fail11;
292 	encp->enc_privilege_mask = mask;
293 
294 	/* Get interrupt vector limits */
295 	if ((rc = efx_mcdi_get_vector_cfg(enp, &base, &nvec, NULL)) != 0) {
296 		if (EFX_PCI_FUNCTION_IS_PF(encp))
297 			goto fail12;
298 
299 		/* Ignore error (cannot query vector limits from a VF). */
300 		base = 0;
301 		nvec = 1024;
302 	}
303 	encp->enc_intr_vec_base = base;
304 	encp->enc_intr_limit = nvec;
305 
306 	/*
307 	 * Maximum number of bytes into the frame the TCP header can start for
308 	 * firmware assisted TSO to work.
309 	 */
310 	encp->enc_tx_tso_tcp_header_offset_limit = EF10_TCP_HEADER_OFFSET_LIMIT;
311 
312 	/*
313 	 * Medford stores a single global copy of VPD, not per-PF as on
314 	 * Huntington.
315 	 */
316 	encp->enc_vpd_is_global = B_TRUE;
317 
318 	rc = medford_nic_get_required_pcie_bandwidth(enp, &bandwidth);
319 	if (rc != 0)
320 		goto fail13;
321 	encp->enc_required_pcie_bandwidth_mbps = bandwidth;
322 	encp->enc_max_pcie_link_gen = EFX_PCIE_LINK_SPEED_GEN3;
323 
324 	return (0);
325 
326 fail13:
327 	EFSYS_PROBE(fail13);
328 fail12:
329 	EFSYS_PROBE(fail12);
330 fail11:
331 	EFSYS_PROBE(fail11);
332 fail10:
333 	EFSYS_PROBE(fail10);
334 fail9:
335 	EFSYS_PROBE(fail9);
336 fail8:
337 	EFSYS_PROBE(fail8);
338 fail7:
339 	EFSYS_PROBE(fail7);
340 fail6:
341 	EFSYS_PROBE(fail6);
342 fail5:
343 	EFSYS_PROBE(fail5);
344 fail4:
345 	EFSYS_PROBE(fail4);
346 fail3:
347 	EFSYS_PROBE(fail3);
348 fail2:
349 	EFSYS_PROBE(fail2);
350 fail1:
351 	EFSYS_PROBE1(fail1, efx_rc_t, rc);
352 
353 	return (rc);
354 }
355 
356 #endif	/* EFSYS_OPT_MEDFORD */
357