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 #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 __diagused; 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 __diagused; 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 EFSYS_PROBE(mcdi_ioctl_mc_reboot); 282 /* sfxge_t->s_state_lock held */ 283 (void) sfxge_schedule_reset(sc); 284 } 285 286 free(mcdibuf, M_TEMP); 287 288 return (0); 289 290 fail6: 291 fail5: 292 free(mcdibuf, M_TEMP); 293 fail3: 294 fail2: 295 fail1: 296 return (rc); 297 } 298 299 int 300 sfxge_mcdi_init(struct sfxge_softc *sc) 301 { 302 efx_nic_t *enp; 303 struct sfxge_mcdi *mcdi; 304 efx_mcdi_transport_t *emtp; 305 efsys_mem_t *esmp; 306 int max_msg_size; 307 int rc; 308 309 enp = sc->enp; 310 mcdi = &sc->mcdi; 311 emtp = &mcdi->transport; 312 esmp = &mcdi->mem; 313 max_msg_size = sizeof (uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2; 314 315 KASSERT(mcdi->state == SFXGE_MCDI_UNINITIALIZED, 316 ("MCDI already initialized")); 317 318 SFXGE_MCDI_LOCK_INIT(mcdi, device_get_nameunit(sc->dev)); 319 320 mcdi->state = SFXGE_MCDI_INITIALIZED; 321 322 if ((rc = sfxge_dma_alloc(sc, max_msg_size, esmp)) != 0) 323 goto fail; 324 325 emtp->emt_context = sc; 326 emtp->emt_dma_mem = esmp; 327 emtp->emt_execute = sfxge_mcdi_execute; 328 emtp->emt_ev_cpl = sfxge_mcdi_ev_cpl; 329 emtp->emt_exception = sfxge_mcdi_exception; 330 #if EFSYS_OPT_MCDI_LOGGING 331 emtp->emt_logger = sfxge_mcdi_logger; 332 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev), 333 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 334 OID_AUTO, "mcdi_logging", CTLFLAG_RW, 335 &sc->mcdi_logging, 0, 336 "MCDI logging"); 337 #endif 338 339 if ((rc = efx_mcdi_init(enp, emtp)) != 0) 340 goto fail; 341 342 return (0); 343 344 fail: 345 SFXGE_MCDI_LOCK_DESTROY(mcdi); 346 mcdi->state = SFXGE_MCDI_UNINITIALIZED; 347 return (rc); 348 } 349 350 void 351 sfxge_mcdi_fini(struct sfxge_softc *sc) 352 { 353 struct sfxge_mcdi *mcdi; 354 efx_nic_t *enp; 355 efx_mcdi_transport_t *emtp; 356 efsys_mem_t *esmp; 357 358 enp = sc->enp; 359 mcdi = &sc->mcdi; 360 emtp = &mcdi->transport; 361 esmp = &mcdi->mem; 362 363 SFXGE_MCDI_LOCK(mcdi); 364 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED, 365 ("MCDI not initialized")); 366 367 efx_mcdi_fini(enp); 368 bzero(emtp, sizeof(*emtp)); 369 370 SFXGE_MCDI_UNLOCK(mcdi); 371 372 sfxge_dma_free(esmp); 373 374 SFXGE_MCDI_LOCK_DESTROY(mcdi); 375 } 376