1 /*- 2 * Copyright (c) 2010-2015 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 #define SFXGE_MCDI_WATCHDOG_INTERVAL 10000000 /* 10s in 1us units */ 59 60 static void 61 sfxge_mcdi_timeout(struct sfxge_softc *sc) 62 { 63 device_t dev = sc->dev; 64 65 log(LOG_WARNING, "[%s%d] MC_TIMEOUT", device_get_name(dev), 66 device_get_unit(dev)); 67 68 EFSYS_PROBE(mcdi_timeout); 69 sfxge_schedule_reset(sc); 70 } 71 72 static void 73 sfxge_mcdi_poll(struct sfxge_softc *sc) 74 { 75 efx_nic_t *enp; 76 clock_t delay_total; 77 clock_t delay_us; 78 boolean_t aborted; 79 80 delay_total = 0; 81 delay_us = SFXGE_MCDI_POLL_INTERVAL_MIN; 82 enp = sc->enp; 83 84 do { 85 if (efx_mcdi_request_poll(enp)) { 86 EFSYS_PROBE1(mcdi_delay, clock_t, delay_total); 87 return; 88 } 89 90 if (delay_total > SFXGE_MCDI_WATCHDOG_INTERVAL) { 91 aborted = efx_mcdi_request_abort(enp); 92 KASSERT(aborted, ("abort failed")); 93 sfxge_mcdi_timeout(sc); 94 return; 95 } 96 97 /* Spin or block depending on delay interval. */ 98 if (delay_us < 1000000) 99 DELAY(delay_us); 100 else 101 pause("mcdi wait", delay_us * hz / 1000000); 102 103 delay_total += delay_us; 104 105 /* Exponentially back off the poll frequency. */ 106 delay_us = delay_us * 2; 107 if (delay_us > SFXGE_MCDI_POLL_INTERVAL_MAX) 108 delay_us = SFXGE_MCDI_POLL_INTERVAL_MAX; 109 110 } while (1); 111 } 112 113 static void 114 sfxge_mcdi_execute(void *arg, efx_mcdi_req_t *emrp) 115 { 116 struct sfxge_softc *sc; 117 struct sfxge_mcdi *mcdi; 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_request_start(sc->enp, emrp, B_FALSE); 129 sfxge_mcdi_poll(sc); 130 131 SFXGE_MCDI_UNLOCK(mcdi); 132 } 133 134 static void 135 sfxge_mcdi_ev_cpl(void *arg) 136 { 137 struct sfxge_softc *sc; 138 struct sfxge_mcdi *mcdi; 139 140 sc = (struct sfxge_softc *)arg; 141 mcdi = &sc->mcdi; 142 143 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED, 144 ("MCDI not initialized")); 145 146 /* We do not use MCDI completion, MCDI is simply polled */ 147 } 148 149 static void 150 sfxge_mcdi_exception(void *arg, efx_mcdi_exception_t eme) 151 { 152 struct sfxge_softc *sc; 153 device_t dev; 154 155 sc = (struct sfxge_softc *)arg; 156 dev = sc->dev; 157 158 log(LOG_WARNING, "[%s%d] MC_%s", device_get_name(dev), 159 device_get_unit(dev), 160 (eme == EFX_MCDI_EXCEPTION_MC_REBOOT) 161 ? "REBOOT" 162 : (eme == EFX_MCDI_EXCEPTION_MC_BADASSERT) 163 ? "BADASSERT" : "UNKNOWN"); 164 165 EFSYS_PROBE(mcdi_exception); 166 167 sfxge_schedule_reset(sc); 168 } 169 170 #if EFSYS_OPT_MCDI_LOGGING 171 172 #define SFXGE_MCDI_LOG_BUF_SIZE 128 173 174 static size_t 175 sfxge_mcdi_do_log(char *buffer, void *data, size_t data_size, 176 size_t pfxsize, size_t position) 177 { 178 uint32_t *words = data; 179 size_t i; 180 181 for (i = 0; i < data_size; i += sizeof(*words)) { 182 if (position + 2 * sizeof(*words) + 1 >= SFXGE_MCDI_LOG_BUF_SIZE) { 183 buffer[position] = '\0'; 184 printf("%s \\\n", buffer); 185 position = pfxsize; 186 } 187 snprintf(buffer + position, SFXGE_MCDI_LOG_BUF_SIZE - position, 188 " %08x", *words); 189 words++; 190 position += 2 * sizeof(uint32_t) + 1; 191 } 192 return (position); 193 } 194 195 static void 196 sfxge_mcdi_logger(void *arg, efx_log_msg_t type, 197 void *header, size_t header_size, 198 void *data, size_t data_size) 199 { 200 struct sfxge_softc *sc = (struct sfxge_softc *)arg; 201 char buffer[SFXGE_MCDI_LOG_BUF_SIZE]; 202 size_t pfxsize; 203 size_t start; 204 205 if (!sc->mcdi_logging) 206 return; 207 208 pfxsize = snprintf(buffer, sizeof(buffer), 209 "sfc %04x:%02x:%02x.%02x %s MCDI RPC %s:", 210 pci_get_domain(sc->dev), 211 pci_get_bus(sc->dev), 212 pci_get_slot(sc->dev), 213 pci_get_function(sc->dev), 214 device_get_nameunit(sc->dev), 215 type == EFX_LOG_MCDI_REQUEST ? "REQ" : 216 type == EFX_LOG_MCDI_RESPONSE ? "RESP" : "???"); 217 start = sfxge_mcdi_do_log(buffer, header, header_size, 218 pfxsize, pfxsize); 219 start = sfxge_mcdi_do_log(buffer, data, data_size, pfxsize, start); 220 if (start != pfxsize) { 221 buffer[start] = '\0'; 222 printf("%s\n", buffer); 223 } 224 } 225 226 #endif 227 228 int 229 sfxge_mcdi_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ip) 230 { 231 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp); 232 struct sfxge_mcdi *mp = &(sc->mcdi); 233 efx_mcdi_req_t emr; 234 uint8_t *mcdibuf; 235 int rc; 236 237 if (mp->state == SFXGE_MCDI_UNINITIALIZED) { 238 rc = ENODEV; 239 goto fail1; 240 } 241 242 if (!(encp->enc_features & EFX_FEATURE_MCDI)) { 243 rc = ENOTSUP; 244 goto fail2; 245 } 246 247 if (ip->u.mcdi.len > SFXGE_MCDI_MAX_PAYLOAD) { 248 rc = EINVAL; 249 goto fail3; 250 } 251 252 mcdibuf = malloc(SFXGE_MCDI_MAX_PAYLOAD, M_TEMP, M_WAITOK | M_ZERO); 253 if (mcdibuf == NULL) { 254 rc = ENOMEM; 255 goto fail4; 256 } 257 if ((rc = copyin(ip->u.mcdi.payload, mcdibuf, ip->u.mcdi.len)) != 0) { 258 goto fail5; 259 } 260 261 emr.emr_cmd = ip->u.mcdi.cmd; 262 emr.emr_in_buf = mcdibuf; 263 emr.emr_in_length = ip->u.mcdi.len; 264 265 emr.emr_out_buf = mcdibuf; 266 emr.emr_out_length = SFXGE_MCDI_MAX_PAYLOAD; 267 268 sfxge_mcdi_execute(sc, &emr); 269 270 ip->u.mcdi.rc = emr.emr_rc; 271 ip->u.mcdi.cmd = emr.emr_cmd; 272 ip->u.mcdi.len = emr.emr_out_length_used; 273 if ((rc = copyout(mcdibuf, ip->u.mcdi.payload, ip->u.mcdi.len)) != 0) { 274 goto fail6; 275 } 276 277 /* 278 * Helpfully trigger a device reset in response to an MCDI_CMD_REBOOT 279 * Both ports will see ->emt_exception callbacks on the next MCDI poll 280 */ 281 if (ip->u.mcdi.cmd == MC_CMD_REBOOT) { 282 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 fail4: 296 fail3: 297 fail2: 298 fail1: 299 return (rc); 300 } 301 302 303 int 304 sfxge_mcdi_init(struct sfxge_softc *sc) 305 { 306 efx_nic_t *enp; 307 struct sfxge_mcdi *mcdi; 308 efx_mcdi_transport_t *emtp; 309 efsys_mem_t *esmp; 310 int max_msg_size; 311 int rc; 312 313 enp = sc->enp; 314 mcdi = &sc->mcdi; 315 emtp = &mcdi->transport; 316 esmp = &mcdi->mem; 317 max_msg_size = sizeof (uint32_t) + MCDI_CTL_SDU_LEN_MAX_V2; 318 319 KASSERT(mcdi->state == SFXGE_MCDI_UNINITIALIZED, 320 ("MCDI already initialized")); 321 322 SFXGE_MCDI_LOCK_INIT(mcdi, device_get_nameunit(sc->dev)); 323 324 mcdi->state = SFXGE_MCDI_INITIALIZED; 325 326 if ((rc = sfxge_dma_alloc(sc, max_msg_size, esmp)) != 0) 327 goto fail; 328 329 emtp->emt_context = sc; 330 emtp->emt_dma_mem = esmp; 331 emtp->emt_execute = sfxge_mcdi_execute; 332 emtp->emt_ev_cpl = sfxge_mcdi_ev_cpl; 333 emtp->emt_exception = sfxge_mcdi_exception; 334 #if EFSYS_OPT_MCDI_LOGGING 335 emtp->emt_logger = sfxge_mcdi_logger; 336 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev), 337 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 338 OID_AUTO, "mcdi_logging", CTLFLAG_RW, 339 &sc->mcdi_logging, 0, 340 "MCDI logging"); 341 #endif 342 343 if ((rc = efx_mcdi_init(enp, emtp)) != 0) 344 goto fail; 345 346 return (0); 347 348 fail: 349 SFXGE_MCDI_LOCK_DESTROY(mcdi); 350 mcdi->state = SFXGE_MCDI_UNINITIALIZED; 351 return (rc); 352 } 353 354 void 355 sfxge_mcdi_fini(struct sfxge_softc *sc) 356 { 357 struct sfxge_mcdi *mcdi; 358 efx_nic_t *enp; 359 efx_mcdi_transport_t *emtp; 360 efsys_mem_t *esmp; 361 362 enp = sc->enp; 363 mcdi = &sc->mcdi; 364 emtp = &mcdi->transport; 365 esmp = &mcdi->mem; 366 367 SFXGE_MCDI_LOCK(mcdi); 368 KASSERT(mcdi->state == SFXGE_MCDI_INITIALIZED, 369 ("MCDI not initialized")); 370 371 efx_mcdi_fini(enp); 372 bzero(emtp, sizeof(*emtp)); 373 374 SFXGE_MCDI_UNLOCK(mcdi); 375 376 sfxge_dma_free(esmp); 377 378 SFXGE_MCDI_LOCK_DESTROY(mcdi); 379 } 380