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