xref: /freebsd/sys/dev/sfxge/sfxge_mcdi.c (revision fdafd315)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010-2016 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was developed in part by Philip Paeps under contract for
8  * Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * The views and conclusions contained in the software and documentation are
32  * those of the authors and should not be interpreted as representing official
33  * policies, either expressed or implied, of the FreeBSD Project.
34  */
35 
36 #include <sys/param.h>
37 #include <sys/condvar.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/syslog.h>
42 #include <sys/taskqueue.h>
43 #include <sys/malloc.h>
44 
45 #include "common/efx.h"
46 #include "common/efx_mcdi.h"
47 #include "common/efx_regs_mcdi.h"
48 
49 #include "sfxge.h"
50 
51 #if EFSYS_OPT_MCDI_LOGGING
52 #include <dev/pci/pcivar.h>
53 #endif
54 
55 #define	SFXGE_MCDI_POLL_INTERVAL_MIN 10		/* 10us in 1us units */
56 #define	SFXGE_MCDI_POLL_INTERVAL_MAX 100000	/* 100ms in 1us units */
57 
58 static void
sfxge_mcdi_timeout(struct sfxge_softc * sc)59 sfxge_mcdi_timeout(struct sfxge_softc *sc)
60 {
61 	device_t dev = sc->dev;
62 
63 	log(LOG_WARNING, "[%s%d] MC_TIMEOUT", device_get_name(dev),
64 		device_get_unit(dev));
65 
66 	EFSYS_PROBE(mcdi_timeout);
67 	sfxge_schedule_reset(sc);
68 }
69 
70 static void
sfxge_mcdi_poll(struct sfxge_softc * sc,uint32_t timeout_us)71 sfxge_mcdi_poll(struct sfxge_softc *sc, uint32_t timeout_us)
72 {
73 	efx_nic_t *enp;
74 	clock_t delay_total;
75 	clock_t delay_us;
76 	boolean_t aborted __diagused;
77 
78 	delay_total = 0;
79 	delay_us = SFXGE_MCDI_POLL_INTERVAL_MIN;
80 	enp = sc->enp;
81 
82 	do {
83 		if (efx_mcdi_request_poll(enp)) {
84 			EFSYS_PROBE1(mcdi_delay, clock_t, delay_total);
85 			return;
86 		}
87 
88 		if (delay_total > timeout_us) {
89 			aborted = efx_mcdi_request_abort(enp);
90 			KASSERT(aborted, ("abort failed"));
91 			sfxge_mcdi_timeout(sc);
92 			return;
93 		}
94 
95 		/* Spin or block depending on delay interval. */
96 		if (delay_us < 1000000)
97 			DELAY(delay_us);
98 		else
99 			pause("mcdi wait", delay_us * hz / 1000000);
100 
101 		delay_total += delay_us;
102 
103 		/* Exponentially back off the poll frequency. */
104 		delay_us = delay_us * 2;
105 		if (delay_us > SFXGE_MCDI_POLL_INTERVAL_MAX)
106 			delay_us = SFXGE_MCDI_POLL_INTERVAL_MAX;
107 
108 	} while (1);
109 }
110 
111 static void
sfxge_mcdi_execute(void * arg,efx_mcdi_req_t * emrp)112 sfxge_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
113 {
114 	struct sfxge_softc *sc;
115 	struct sfxge_mcdi *mcdi;
116 	uint32_t timeout_us = 0;
117 
118 	sc = (struct sfxge_softc *)arg;
119 	mcdi = &sc->mcdi;
120 
121 	SFXGE_MCDI_LOCK(mcdi);
122 
123 	KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
124 	    ("MCDI not initialized"));
125 
126 	/* Issue request and poll for completion. */
127 	efx_mcdi_get_timeout(sc->enp, emrp, &timeout_us);
128 	KASSERT(timeout_us > 0, ("MCDI timeout not initialized"));
129 
130 	efx_mcdi_request_start(sc->enp, emrp, B_FALSE);
131 	sfxge_mcdi_poll(sc, timeout_us);
132 
133 	SFXGE_MCDI_UNLOCK(mcdi);
134 }
135 
136 static void
sfxge_mcdi_ev_cpl(void * arg)137 sfxge_mcdi_ev_cpl(void *arg)
138 {
139 	struct sfxge_softc *sc;
140 	struct sfxge_mcdi *mcdi __diagused;
141 
142 	sc = (struct sfxge_softc *)arg;
143 	mcdi = &sc->mcdi;
144 
145 	KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
146 	    ("MCDI not initialized"));
147 
148 	/* We do not use MCDI completion, MCDI is simply polled */
149 }
150 
151 static void
sfxge_mcdi_exception(void * arg,efx_mcdi_exception_t eme)152 sfxge_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
153 {
154 	struct sfxge_softc *sc;
155 	device_t dev;
156 
157 	sc = (struct sfxge_softc *)arg;
158 	dev = sc->dev;
159 
160 	log(LOG_WARNING, "[%s%d] MC_%s", device_get_name(dev),
161 	    device_get_unit(dev),
162 	    (eme == EFX_MCDI_EXCEPTION_MC_REBOOT)
163 	    ? "REBOOT"
164 	    : (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT)
165 	    ? "BADASSERT" : "UNKNOWN");
166 
167 	EFSYS_PROBE(mcdi_exception);
168 
169 	sfxge_schedule_reset(sc);
170 }
171 
172 #if EFSYS_OPT_MCDI_LOGGING
173 
174 #define SFXGE_MCDI_LOG_BUF_SIZE 128
175 
176 static size_t
sfxge_mcdi_do_log(char * buffer,void * data,size_t data_size,size_t pfxsize,size_t position)177 sfxge_mcdi_do_log(char *buffer, void *data, size_t data_size,
178 		  size_t pfxsize, size_t position)
179 {
180 	uint32_t *words = data;
181 	size_t i;
182 
183 	for (i = 0; i < data_size; i += sizeof(*words)) {
184 		if (position + 2 * sizeof(*words) + 1 >= SFXGE_MCDI_LOG_BUF_SIZE) {
185 			buffer[position] = '\0';
186 			printf("%s \\\n", buffer);
187 			position = pfxsize;
188 		}
189 		snprintf(buffer + position, SFXGE_MCDI_LOG_BUF_SIZE - position,
190 			 " %08x", *words);
191 		words++;
192 		position += 2 * sizeof(uint32_t) + 1;
193 	}
194 	return (position);
195 }
196 
197 static void
sfxge_mcdi_logger(void * arg,efx_log_msg_t type,void * header,size_t header_size,void * data,size_t data_size)198 sfxge_mcdi_logger(void *arg, efx_log_msg_t type,
199 		  void *header, size_t header_size,
200 		  void *data, size_t data_size)
201 {
202 	struct sfxge_softc *sc = (struct sfxge_softc *)arg;
203 	char buffer[SFXGE_MCDI_LOG_BUF_SIZE];
204 	size_t pfxsize;
205 	size_t start;
206 
207 	if (!sc->mcdi_logging)
208 		return;
209 
210 	pfxsize = snprintf(buffer, sizeof(buffer),
211 			   "sfc %04x:%02x:%02x.%02x %s MCDI RPC %s:",
212 			   pci_get_domain(sc->dev),
213 			   pci_get_bus(sc->dev),
214 			   pci_get_slot(sc->dev),
215 			   pci_get_function(sc->dev),
216 			   device_get_nameunit(sc->dev),
217 			   type == EFX_LOG_MCDI_REQUEST ? "REQ" :
218 			   type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???");
219 	start = sfxge_mcdi_do_log(buffer, header, header_size,
220 				  pfxsize, pfxsize);
221 	start = sfxge_mcdi_do_log(buffer, data, data_size, pfxsize, start);
222 	if (start != pfxsize) {
223 		buffer[start] = '\0';
224 		printf("%s\n", buffer);
225 	}
226 }
227 
228 #endif
229 
230 int
sfxge_mcdi_ioctl(struct sfxge_softc * sc,sfxge_ioc_t * ip)231 sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip)
232 {
233 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
234 	struct sfxge_mcdi *mp = &(sc->mcdi);
235 	efx_mcdi_req_t emr;
236 	uint8_t *mcdibuf;
237 	int rc;
238 
239 	if (mp->state == SFXGE_MCDI_UNINITIALIZED) {
240 		rc = ENODEV;
241 		goto fail1;
242 	}
243 
244 	if (!(encp->enc_features & EFX_FEATURE_MCDI)) {
245 		rc = ENOTSUP;
246 		goto fail2;
247 	}
248 
249 	if (ip->u.mcdi.len > SFXGE_MCDI_MAX_PAYLOAD) {
250 		rc = EINVAL;
251 		goto fail3;
252 	}
253 
254 	mcdibuf = malloc(SFXGE_MCDI_MAX_PAYLOAD, M_TEMP, M_WAITOK | M_ZERO);
255 	if ((rc = copyin(ip->u.mcdi.payload, mcdibuf, ip->u.mcdi.len)) != 0) {
256 		goto fail5;
257 	}
258 
259 	emr.emr_cmd = ip->u.mcdi.cmd;
260 	emr.emr_in_buf = mcdibuf;
261 	emr.emr_in_length = ip->u.mcdi.len;
262 
263 	emr.emr_out_buf = mcdibuf;
264 	emr.emr_out_length = SFXGE_MCDI_MAX_PAYLOAD;
265 
266 	sfxge_mcdi_execute(sc, &emr);
267 
268 	ip->u.mcdi.rc = emr.emr_rc;
269 	ip->u.mcdi.cmd = emr.emr_cmd;
270 	ip->u.mcdi.len = emr.emr_out_length_used;
271 	if ((rc = copyout(mcdibuf, ip->u.mcdi.payload, ip->u.mcdi.len)) != 0) {
272 		goto fail6;
273 	}
274 
275 	/*
276 	 * Helpfully trigger a device reset in response to an MCDI_CMD_REBOOT
277 	 * Both ports will see ->emt_exception callbacks on the next MCDI poll
278 	 */
279 	if (ip->u.mcdi.cmd == MC_CMD_REBOOT) {
280 		EFSYS_PROBE(mcdi_ioctl_mc_reboot);
281 		/* sfxge_t->s_state_lock held */
282 		(void) sfxge_schedule_reset(sc);
283 	}
284 
285 	free(mcdibuf, M_TEMP);
286 
287 	return (0);
288 
289 fail6:
290 fail5:
291 	free(mcdibuf, M_TEMP);
292 fail3:
293 fail2:
294 fail1:
295 	return (rc);
296 }
297 
298 int
sfxge_mcdi_init(struct sfxge_softc * sc)299 sfxge_mcdi_init(struct sfxge_softc *sc)
300 {
301 	efx_nic_t *enp;
302 	struct sfxge_mcdi *mcdi;
303 	efx_mcdi_transport_t *emtp;
304 	efsys_mem_t *esmp;
305 	int max_msg_size;
306 	int rc;
307 
308 	enp = sc->enp;
309 	mcdi = &sc->mcdi;
310 	emtp = &mcdi->transport;
311 	esmp = &mcdi->mem;
312 	max_msg_size = sizeof (uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
313 
314 	KASSERT(mcdi->state == SFXGE_MCDI_UNINITIALIZED,
315 	    ("MCDI already initialized"));
316 
317 	SFXGE_MCDI_LOCK_INIT(mcdi, device_get_nameunit(sc->dev));
318 
319 	mcdi->state = SFXGE_MCDI_INITIALIZED;
320 
321 	if ((rc = sfxge_dma_alloc(sc, max_msg_size, esmp)) != 0)
322 		goto fail;
323 
324 	emtp->emt_context = sc;
325 	emtp->emt_dma_mem = esmp;
326 	emtp->emt_execute = sfxge_mcdi_execute;
327 	emtp->emt_ev_cpl = sfxge_mcdi_ev_cpl;
328 	emtp->emt_exception = sfxge_mcdi_exception;
329 #if EFSYS_OPT_MCDI_LOGGING
330 	emtp->emt_logger = sfxge_mcdi_logger;
331 	SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
332 		       SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
333 		       OID_AUTO, "mcdi_logging", CTLFLAG_RW,
334 		       &sc->mcdi_logging, 0,
335 		       "MCDI logging");
336 #endif
337 
338 	if ((rc = efx_mcdi_init(enp, emtp)) != 0)
339 		goto fail;
340 
341 	return (0);
342 
343 fail:
344 	SFXGE_MCDI_LOCK_DESTROY(mcdi);
345 	mcdi->state = SFXGE_MCDI_UNINITIALIZED;
346 	return (rc);
347 }
348 
349 void
sfxge_mcdi_fini(struct sfxge_softc * sc)350 sfxge_mcdi_fini(struct sfxge_softc *sc)
351 {
352 	struct sfxge_mcdi *mcdi;
353 	efx_nic_t *enp;
354 	efx_mcdi_transport_t *emtp;
355 	efsys_mem_t *esmp;
356 
357 	enp = sc->enp;
358 	mcdi = &sc->mcdi;
359 	emtp = &mcdi->transport;
360 	esmp = &mcdi->mem;
361 
362 	SFXGE_MCDI_LOCK(mcdi);
363 	KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
364 	    ("MCDI not initialized"));
365 
366 	efx_mcdi_fini(enp);
367 	bzero(emtp, sizeof(*emtp));
368 
369 	SFXGE_MCDI_UNLOCK(mcdi);
370 
371 	sfxge_dma_free(esmp);
372 
373 	SFXGE_MCDI_LOCK_DESTROY(mcdi);
374 }
375