2 * Copyright (c) 2010-2015 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 #define SFXGE_MCDI_POLL_INTERVAL_MIN 10 /* 10us in 1us units */
53 #define SFXGE_MCDI_POLL_INTERVAL_MAX 100000 /* 100ms in 1us units */
54 #define SFXGE_MCDI_WATCHDOG_INTERVAL 10000000 /* 10s in 1us units */
57 sfxge_mcdi_timeout(struct sfxge_softc *sc)
59 device_t dev = sc->dev;
61 log(LOG_WARNING, "[%s%d] MC_TIMEOUT", device_get_name(dev),
62 device_get_unit(dev));
64 EFSYS_PROBE(mcdi_timeout);
65 sfxge_schedule_reset(sc);
69 sfxge_mcdi_poll(struct sfxge_softc *sc)
77 delay_us = SFXGE_MCDI_POLL_INTERVAL_MIN;
81 if (efx_mcdi_request_poll(enp)) {
82 EFSYS_PROBE1(mcdi_delay, clock_t, delay_total);
86 if (delay_total > SFXGE_MCDI_WATCHDOG_INTERVAL) {
87 aborted = efx_mcdi_request_abort(enp);
88 KASSERT(aborted, ("abort failed"));
89 sfxge_mcdi_timeout(sc);
93 /* Spin or block depending on delay interval. */
94 if (delay_us < 1000000)
97 pause("mcdi wait", delay_us * hz / 1000000);
99 delay_total += delay_us;
101 /* Exponentially back off the poll frequency. */
102 delay_us = delay_us * 2;
103 if (delay_us > SFXGE_MCDI_POLL_INTERVAL_MAX)
104 delay_us = SFXGE_MCDI_POLL_INTERVAL_MAX;
110 sfxge_mcdi_execute(void *arg, efx_mcdi_req_t *emrp)
112 struct sfxge_softc *sc;
113 struct sfxge_mcdi *mcdi;
115 sc = (struct sfxge_softc *)arg;
118 SFXGE_MCDI_LOCK(mcdi);
120 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
121 ("MCDI not initialized"));
123 /* Issue request and poll for completion. */
124 efx_mcdi_request_start(sc->enp, emrp, B_FALSE);
127 SFXGE_MCDI_UNLOCK(mcdi);
131 sfxge_mcdi_ev_cpl(void *arg)
133 struct sfxge_softc *sc;
134 struct sfxge_mcdi *mcdi;
136 sc = (struct sfxge_softc *)arg;
139 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
140 ("MCDI not initialized"));
142 /* We do not use MCDI completion, MCDI is simply polled */
146 sfxge_mcdi_exception(void *arg, efx_mcdi_exception_t eme)
148 struct sfxge_softc *sc;
151 sc = (struct sfxge_softc *)arg;
154 log(LOG_WARNING, "[%s%d] MC_%s", device_get_name(dev),
155 device_get_unit(dev),
156 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT)
158 : (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT)
159 ? "BADASSERT" : "UNKNOWN");
161 EFSYS_PROBE(mcdi_exception);
163 sfxge_schedule_reset(sc);
167 sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip)
169 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
170 struct sfxge_mcdi *mp = &(sc->mcdi);
175 if (mp->state == SFXGE_MCDI_UNINITIALIZED) {
180 if (!(encp->enc_features & EFX_FEATURE_MCDI)) {
185 if (ip->u.mcdi.len > SFXGE_MCDI_MAX_PAYLOAD) {
190 mcdibuf = malloc(SFXGE_MCDI_MAX_PAYLOAD, M_TEMP, M_WAITOK | M_ZERO);
191 if (mcdibuf == NULL) {
195 if ((rc = copyin(ip->u.mcdi.payload, mcdibuf, ip->u.mcdi.len)) != 0) {
199 emr.emr_cmd = ip->u.mcdi.cmd;
200 emr.emr_in_buf = mcdibuf;
201 emr.emr_in_length = ip->u.mcdi.len;
203 emr.emr_out_buf = mcdibuf;
204 emr.emr_out_length = SFXGE_MCDI_MAX_PAYLOAD;
206 sfxge_mcdi_execute(sc, &emr);
208 ip->u.mcdi.rc = emr.emr_rc;
209 ip->u.mcdi.cmd = emr.emr_cmd;
210 ip->u.mcdi.len = emr.emr_out_length_used;
211 if ((rc = copyout(mcdibuf, ip->u.mcdi.payload, ip->u.mcdi.len)) != 0) {
216 * Helpfully trigger a device reset in response to an MCDI_CMD_REBOOT
217 * Both ports will see ->emt_exception callbacks on the next MCDI poll
219 if (ip->u.mcdi.cmd == MC_CMD_REBOOT) {
221 EFSYS_PROBE(mcdi_ioctl_mc_reboot);
222 /* sfxge_t->s_state_lock held */
223 (void) sfxge_schedule_reset(sc);
226 free(mcdibuf, M_TEMP);
232 free(mcdibuf, M_TEMP);
242 sfxge_mcdi_init(struct sfxge_softc *sc)
245 struct sfxge_mcdi *mcdi;
246 efx_mcdi_transport_t *emtp;
253 emtp = &mcdi->transport;
255 max_msg_size = sizeof (uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2;
257 KASSERT(mcdi->state == SFXGE_MCDI_UNINITIALIZED,
258 ("MCDI already initialized"));
260 SFXGE_MCDI_LOCK_INIT(mcdi, device_get_nameunit(sc->dev));
262 mcdi->state = SFXGE_MCDI_INITIALIZED;
264 if ((rc = sfxge_dma_alloc(sc, max_msg_size, esmp)) != 0)
267 emtp->emt_context = sc;
268 emtp->emt_dma_mem = esmp;
269 emtp->emt_execute = sfxge_mcdi_execute;
270 emtp->emt_ev_cpl = sfxge_mcdi_ev_cpl;
271 emtp->emt_exception = sfxge_mcdi_exception;
273 if ((rc = efx_mcdi_init(enp, emtp)) != 0)
279 SFXGE_MCDI_LOCK_DESTROY(mcdi);
280 mcdi->state = SFXGE_MCDI_UNINITIALIZED;
285 sfxge_mcdi_fini(struct sfxge_softc *sc)
287 struct sfxge_mcdi *mcdi;
289 efx_mcdi_transport_t *emtp;
294 emtp = &mcdi->transport;
297 SFXGE_MCDI_LOCK(mcdi);
298 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED,
299 ("MCDI not initialized"));
302 bzero(emtp, sizeof(*emtp));
304 SFXGE_MCDI_UNLOCK(mcdi);
306 sfxge_dma_free(esmp);
308 SFXGE_MCDI_LOCK_DESTROY(mcdi);