2 * Copyright (c) 2010-2016 Solarflare Communications Inc.
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/condvar.h>
40 #include <sys/mutex.h>
42 #include <sys/syslog.h>
43 #include <sys/taskqueue.h>
44 #include <sys/malloc.h>
46 #include "common/efx.h"
47 #include "common/efx_mcdi.h"
48 #include "common/efx_regs_mcdi.h"
52 #if EFSYS_OPT_MCDI_LOGGING
53 #include <dev/pci/pcivar.h>
56 #define SFXGE_MCDI_POLL_INTERVAL_MIN 10 /* 10us in 1us units */
57 #define SFXGE_MCDI_POLL_INTERVAL_MAX 100000 /* 100ms in 1us units */
60 sfxge_mcdi_timeout(struct sfxge_softc *sc)
62 device_t dev = sc->dev;
64 log(LOG_WARNING, "[%s%d] MC_TIMEOUT", device_get_name(dev),
65 device_get_unit(dev));
67 EFSYS_PROBE(mcdi_timeout);
68 sfxge_schedule_reset(sc);
72 sfxge_mcdi_poll(struct sfxge_softc *sc, uint32_t timeout_us)
80 delay_us = SFXGE_MCDI_POLL_INTERVAL_MIN;
84 if (efx_mcdi_request_poll(enp)) {
85 EFSYS_PROBE1(mcdi_delay, clock_t, delay_total);
89 if (delay_total > timeout_us) {
90 aborted = efx_mcdi_request_abort(enp);
91 KASSERT(aborted, ("abort failed"));
92 sfxge_mcdi_timeout(sc);
96 /* Spin or block depending on delay interval. */
97 if (delay_us < 1000000)
100 pause("mcdi wait", delay_us * hz / 1000000);
102 delay_total += delay_us;
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;
113 sfxge_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
115 struct sfxge_softc *sc;
116 struct sfxge_mcdi *mcdi;
117 uint32_t timeout_us = 0;
119 sc = (struct sfxge_softc *)arg;
122 SFXGE_MCDI_LOCK(mcdi);
124 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
125 ("MCDI not initialized"));
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"));
131 efx_mcdi_request_start(sc->enp, emrp, B_FALSE);
132 sfxge_mcdi_poll(sc, timeout_us);
134 SFXGE_MCDI_UNLOCK(mcdi);
138 sfxge_mcdi_ev_cpl(void *arg)
140 struct sfxge_softc *sc;
141 struct sfxge_mcdi *mcdi;
143 sc = (struct sfxge_softc *)arg;
146 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
147 ("MCDI not initialized"));
149 /* We do not use MCDI completion, MCDI is simply polled */
153 sfxge_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
155 struct sfxge_softc *sc;
158 sc = (struct sfxge_softc *)arg;
161 log(LOG_WARNING, "[%s%d] MC_%s", device_get_name(dev),
162 device_get_unit(dev),
163 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT)
165 : (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT)
166 ? "BADASSERT" : "UNKNOWN");
168 EFSYS_PROBE(mcdi_exception);
170 sfxge_schedule_reset(sc);
173 #if EFSYS_OPT_MCDI_LOGGING
175 #define SFXGE_MCDI_LOG_BUF_SIZE 128
178 sfxge_mcdi_do_log(char *buffer, void *data, size_t data_size,
179 size_t pfxsize, size_t position)
181 uint32_t *words = data;
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);
190 snprintf(buffer + position, SFXGE_MCDI_LOG_BUF_SIZE - position,
193 position += 2 * sizeof(uint32_t) + 1;
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)
203 struct sfxge_softc *sc = (struct sfxge_softc *)arg;
204 char buffer[SFXGE_MCDI_LOG_BUF_SIZE];
208 if (!sc->mcdi_logging)
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,
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);
232 sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip)
234 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
235 struct sfxge_mcdi *mp = &(sc->mcdi);
240 if (mp->state == SFXGE_MCDI_UNINITIALIZED) {
245 if (!(encp->enc_features & EFX_FEATURE_MCDI)) {
250 if (ip->u.mcdi.len > SFXGE_MCDI_MAX_PAYLOAD) {
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) {
260 emr.emr_cmd = ip->u.mcdi.cmd;
261 emr.emr_in_buf = mcdibuf;
262 emr.emr_in_length = ip->u.mcdi.len;
264 emr.emr_out_buf = mcdibuf;
265 emr.emr_out_length = SFXGE_MCDI_MAX_PAYLOAD;
267 sfxge_mcdi_execute(sc, &emr);
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) {
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
280 if (ip->u.mcdi.cmd == MC_CMD_REBOOT) {
282 EFSYS_PROBE(mcdi_ioctl_mc_reboot);
283 /* sfxge_t->s_state_lock held */
284 (void) sfxge_schedule_reset(sc);
287 free(mcdibuf, M_TEMP);
293 free(mcdibuf, M_TEMP);
302 sfxge_mcdi_init(struct sfxge_softc *sc)
305 struct sfxge_mcdi *mcdi;
306 efx_mcdi_transport_t *emtp;
313 emtp = &mcdi->transport;
315 max_msg_size = sizeof (uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
317 KASSERT(mcdi->state == SFXGE_MCDI_UNINITIALIZED,
318 ("MCDI already initialized"));
320 SFXGE_MCDI_LOCK_INIT(mcdi, device_get_nameunit(sc->dev));
322 mcdi->state = SFXGE_MCDI_INITIALIZED;
324 if ((rc = sfxge_dma_alloc(sc, max_msg_size, esmp)) != 0)
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,
341 if ((rc = efx_mcdi_init(enp, emtp)) != 0)
347 SFXGE_MCDI_LOCK_DESTROY(mcdi);
348 mcdi->state = SFXGE_MCDI_UNINITIALIZED;
353 sfxge_mcdi_fini(struct sfxge_softc *sc)
355 struct sfxge_mcdi *mcdi;
357 efx_mcdi_transport_t *emtp;
362 emtp = &mcdi->transport;
365 SFXGE_MCDI_LOCK(mcdi);
366 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
367 ("MCDI not initialized"));
370 bzero(emtp, sizeof(*emtp));
372 SFXGE_MCDI_UNLOCK(mcdi);
374 sfxge_dma_free(esmp);
376 SFXGE_MCDI_LOCK_DESTROY(mcdi);