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/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/condvar.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/proc.h> 44 #include <sys/syslog.h> 45 #include <sys/taskqueue.h> 46 #include <sys/malloc.h> 47 48 #include "common/efx.h" 49 #include "common/efx_mcdi.h" 50 #include "common/efx_regs_mcdi.h" 51 52 #include "sfxge.h" 53 54 #if EFSYS_OPT_MCDI_LOGGING 55 #include <dev/pci/pcivar.h> 56 #endif 57 58 #define SFXGE_MCDI_POLL_INTERVAL_MIN 10 /* 10us in 1us units */ 59 #define SFXGE_MCDI_POLL_INTERVAL_MAX 100000 /* 100ms in 1us units */ 60 61 static void 62 sfxge_mcdi_timeout(struct sfxge_softc *sc) 63 { 64 device_t dev = sc->dev; 65 66 log(LOG_WARNING, "[%s%d] MC_TIMEOUT", device_get_name(dev), 67 device_get_unit(dev)); 68 69 EFSYS_PROBE(mcdi_timeout); 70 sfxge_schedule_reset(sc); 71 } 72 73 static void 74 sfxge_mcdi_poll(struct sfxge_softc *sc, uint32_t timeout_us) 75 { 76 efx_nic_t *enp; 77 clock_t delay_total; 78 clock_t delay_us; 79 boolean_t aborted __diagused; 80 81 delay_total = 0; 82 delay_us = SFXGE_MCDI_POLL_INTERVAL_MIN; 83 enp = sc->enp; 84 85 do { 86 if (efx_mcdi_request_poll(enp)) { 87 EFSYS_PROBE1(mcdi_delay, clock_t, delay_total); 88 return; 89 } 90 91 if (delay_total > timeout_us) { 92 aborted = efx_mcdi_request_abort(enp); 93 KASSERT(aborted, ("abort failed")); 94 sfxge_mcdi_timeout(sc); 95 return; 96 } 97 98 /* Spin or block depending on delay interval. */ 99 if (delay_us < 1000000) 100 DELAY(delay_us); 101 else 102 pause("mcdi wait", delay_us * hz / 1000000); 103 104 delay_total += delay_us; 105 106 /* Exponentially back off the poll frequency. */ 107 delay_us = delay_us * 2; 108 if (delay_us > SFXGE_MCDI_POLL_INTERVAL_MAX) 109 delay_us = SFXGE_MCDI_POLL_INTERVAL_MAX; 110 111 } while (1); 112 } 113 114 static void 115 sfxge_mcdi_execute(void *arg, efx_mcdi_req_t *emrp) 116 { 117 struct sfxge_softc *sc; 118 struct sfxge_mcdi *mcdi; 119 uint32_t timeout_us = 0; 120 121 sc = (struct sfxge_softc *)arg; 122 mcdi = &sc->mcdi; 123 124 SFXGE_MCDI_LOCK(mcdi); 125 126 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED, 127 ("MCDI not initialized")); 128 129 /* Issue request and poll for completion. */ 130 efx_mcdi_get_timeout(sc->enp, emrp, &timeout_us); 131 KASSERT(timeout_us > 0, ("MCDI timeout not initialized")); 132 133 efx_mcdi_request_start(sc->enp, emrp, B_FALSE); 134 sfxge_mcdi_poll(sc, timeout_us); 135 136 SFXGE_MCDI_UNLOCK(mcdi); 137 } 138 139 static void 140 sfxge_mcdi_ev_cpl(void *arg) 141 { 142 struct sfxge_softc *sc; 143 struct sfxge_mcdi *mcdi __diagused; 144 145 sc = (struct sfxge_softc *)arg; 146 mcdi = &sc->mcdi; 147 148 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED, 149 ("MCDI not initialized")); 150 151 /* We do not use MCDI completion, MCDI is simply polled */ 152 } 153 154 static void 155 sfxge_mcdi_exception(void *arg, efx_mcdi_exception_t eme) 156 { 157 struct sfxge_softc *sc; 158 device_t dev; 159 160 sc = (struct sfxge_softc *)arg; 161 dev = sc->dev; 162 163 log(LOG_WARNING, "[%s%d] MC_%s", device_get_name(dev), 164 device_get_unit(dev), 165 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) 166 ? "REBOOT" 167 : (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) 168 ? "BADASSERT" : "UNKNOWN"); 169 170 EFSYS_PROBE(mcdi_exception); 171 172 sfxge_schedule_reset(sc); 173 } 174 175 #if EFSYS_OPT_MCDI_LOGGING 176 177 #define SFXGE_MCDI_LOG_BUF_SIZE 128 178 179 static size_t 180 sfxge_mcdi_do_log(char *buffer, void *data, size_t data_size, 181 size_t pfxsize, size_t position) 182 { 183 uint32_t *words = data; 184 size_t i; 185 186 for (i = 0; i < data_size; i += sizeof(*words)) { 187 if (position + 2 * sizeof(*words) + 1 >= SFXGE_MCDI_LOG_BUF_SIZE) { 188 buffer[position] = '\0'; 189 printf("%s \\\n", buffer); 190 position = pfxsize; 191 } 192 snprintf(buffer + position, SFXGE_MCDI_LOG_BUF_SIZE - position, 193 " %08x", *words); 194 words++; 195 position += 2 * sizeof(uint32_t) + 1; 196 } 197 return (position); 198 } 199 200 static void 201 sfxge_mcdi_logger(void *arg, efx_log_msg_t type, 202 void *header, size_t header_size, 203 void *data, size_t data_size) 204 { 205 struct sfxge_softc *sc = (struct sfxge_softc *)arg; 206 char buffer[SFXGE_MCDI_LOG_BUF_SIZE]; 207 size_t pfxsize; 208 size_t start; 209 210 if (!sc->mcdi_logging) 211 return; 212 213 pfxsize = snprintf(buffer, sizeof(buffer), 214 "sfc %04x:%02x:%02x.%02x %s MCDI RPC %s:", 215 pci_get_domain(sc->dev), 216 pci_get_bus(sc->dev), 217 pci_get_slot(sc->dev), 218 pci_get_function(sc->dev), 219 device_get_nameunit(sc->dev), 220 type == EFX_LOG_MCDI_REQUEST ? "REQ" : 221 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???"); 222 start = sfxge_mcdi_do_log(buffer, header, header_size, 223 pfxsize, pfxsize); 224 start = sfxge_mcdi_do_log(buffer, data, data_size, pfxsize, start); 225 if (start != pfxsize) { 226 buffer[start] = '\0'; 227 printf("%s\n", buffer); 228 } 229 } 230 231 #endif 232 233 int 234 sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip) 235 { 236 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp); 237 struct sfxge_mcdi *mp = &(sc->mcdi); 238 efx_mcdi_req_t emr; 239 uint8_t *mcdibuf; 240 int rc; 241 242 if (mp->state == SFXGE_MCDI_UNINITIALIZED) { 243 rc = ENODEV; 244 goto fail1; 245 } 246 247 if (!(encp->enc_features & EFX_FEATURE_MCDI)) { 248 rc = ENOTSUP; 249 goto fail2; 250 } 251 252 if (ip->u.mcdi.len > SFXGE_MCDI_MAX_PAYLOAD) { 253 rc = EINVAL; 254 goto fail3; 255 } 256 257 mcdibuf = malloc(SFXGE_MCDI_MAX_PAYLOAD, M_TEMP, M_WAITOK | M_ZERO); 258 if ((rc = copyin(ip->u.mcdi.payload, mcdibuf, ip->u.mcdi.len)) != 0) { 259 goto fail5; 260 } 261 262 emr.emr_cmd = ip->u.mcdi.cmd; 263 emr.emr_in_buf = mcdibuf; 264 emr.emr_in_length = ip->u.mcdi.len; 265 266 emr.emr_out_buf = mcdibuf; 267 emr.emr_out_length = SFXGE_MCDI_MAX_PAYLOAD; 268 269 sfxge_mcdi_execute(sc, &emr); 270 271 ip->u.mcdi.rc = emr.emr_rc; 272 ip->u.mcdi.cmd = emr.emr_cmd; 273 ip->u.mcdi.len = emr.emr_out_length_used; 274 if ((rc = copyout(mcdibuf, ip->u.mcdi.payload, ip->u.mcdi.len)) != 0) { 275 goto fail6; 276 } 277 278 /* 279 * Helpfully trigger a device reset in response to an MCDI_CMD_REBOOT 280 * Both ports will see ->emt_exception callbacks on the next MCDI poll 281 */ 282 if (ip->u.mcdi.cmd == MC_CMD_REBOOT) { 283 EFSYS_PROBE(mcdi_ioctl_mc_reboot); 284 /* sfxge_t->s_state_lock held */ 285 (void) sfxge_schedule_reset(sc); 286 } 287 288 free(mcdibuf, M_TEMP); 289 290 return (0); 291 292 fail6: 293 fail5: 294 free(mcdibuf, M_TEMP); 295 fail3: 296 fail2: 297 fail1: 298 return (rc); 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