1 /* $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 2014 genua mbh <info@genua.de> 5 * Copyright (c) 2014 Fixup Software Ltd. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /*- 21 * Based on BSD-licensed source modules in the Linux iwlwifi driver, 22 * which were used as the reference documentation for this implementation. 23 * 24 * Driver version we are currently based off of is 25 * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd) 26 * 27 *********************************************************************** 28 * 29 * This file is provided under a dual BSD/GPLv2 license. When using or 30 * redistributing this file, you may do so under either license. 31 * 32 * GPL LICENSE SUMMARY 33 * 34 * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved. 35 * 36 * This program is free software; you can redistribute it and/or modify 37 * it under the terms of version 2 of the GNU General Public License as 38 * published by the Free Software Foundation. 39 * 40 * This program is distributed in the hope that it will be useful, but 41 * WITHOUT ANY WARRANTY; without even the implied warranty of 42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 43 * General Public License for more details. 44 * 45 * You should have received a copy of the GNU General Public License 46 * along with this program; if not, write to the Free Software 47 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 48 * USA 49 * 50 * The full GNU General Public License is included in this distribution 51 * in the file called COPYING. 52 * 53 * Contact Information: 54 * Intel Linux Wireless <ilw@linux.intel.com> 55 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 56 * 57 * 58 * BSD LICENSE 59 * 60 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved. 61 * All rights reserved. 62 * 63 * Redistribution and use in source and binary forms, with or without 64 * modification, are permitted provided that the following conditions 65 * are met: 66 * 67 * * Redistributions of source code must retain the above copyright 68 * notice, this list of conditions and the following disclaimer. 69 * * Redistributions in binary form must reproduce the above copyright 70 * notice, this list of conditions and the following disclaimer in 71 * the documentation and/or other materials provided with the 72 * distribution. 73 * * Neither the name Intel Corporation nor the names of its 74 * contributors may be used to endorse or promote products derived 75 * from this software without specific prior written permission. 76 * 77 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 78 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 79 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 80 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 81 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 82 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 83 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 84 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 85 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 86 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 87 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 88 */ 89 90 /*- 91 * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr> 92 * 93 * Permission to use, copy, modify, and distribute this software for any 94 * purpose with or without fee is hereby granted, provided that the above 95 * copyright notice and this permission notice appear in all copies. 96 * 97 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 98 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 99 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 100 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 101 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 102 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 103 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 104 */ 105 #include <sys/cdefs.h> 106 __FBSDID("$FreeBSD$"); 107 108 #include <sys/param.h> 109 #include <sys/bus.h> 110 #include <sys/endian.h> 111 #include <sys/firmware.h> 112 #include <sys/kernel.h> 113 #include <sys/malloc.h> 114 #include <sys/mbuf.h> 115 #include <sys/mutex.h> 116 #include <sys/module.h> 117 #include <sys/proc.h> 118 #include <sys/rman.h> 119 #include <sys/socket.h> 120 #include <sys/sockio.h> 121 #include <sys/sysctl.h> 122 #include <sys/linker.h> 123 124 #include <machine/endian.h> 125 126 #include <bus/pci/pcivar.h> 127 #include <bus/pci/pcireg.h> 128 129 #include <net/bpf.h> 130 131 #include <net/if.h> 132 #include <net/if_var.h> 133 #include <net/if_arp.h> 134 #include <net/ethernet.h> 135 #include <net/if_dl.h> 136 #include <net/if_media.h> 137 #include <net/if_types.h> 138 #include <net/ifq_var.h> 139 140 #include <netinet/in.h> 141 #include <netinet/in_systm.h> 142 #include <netinet/if_ether.h> 143 #include <netinet/ip.h> 144 145 #include <netproto/802_11/ieee80211_var.h> 146 #include <netproto/802_11/ieee80211_regdomain.h> 147 #include <netproto/802_11/ieee80211_ratectl.h> 148 #include <netproto/802_11/ieee80211_radiotap.h> 149 150 #include "if_iwmreg.h" 151 #include "if_iwmvar.h" 152 #include "if_iwm_debug.h" 153 #include "if_iwm_pcie_trans.h" 154 155 /* 156 * This is a subset of what's in linux iwlwifi/pcie/trans.c. 157 * The rest can be migrated out into here once they're no longer in 158 * if_iwm.c. 159 */ 160 161 /* 162 * basic device access 163 */ 164 165 uint32_t 166 iwm_read_prph(struct iwm_softc *sc, uint32_t addr) 167 { 168 IWM_WRITE(sc, 169 IWM_HBUS_TARG_PRPH_RADDR, ((addr & 0x000fffff) | (3 << 24))); 170 IWM_BARRIER_READ_WRITE(sc); 171 return IWM_READ(sc, IWM_HBUS_TARG_PRPH_RDAT); 172 } 173 174 void 175 iwm_write_prph(struct iwm_softc *sc, uint32_t addr, uint32_t val) 176 { 177 IWM_WRITE(sc, 178 IWM_HBUS_TARG_PRPH_WADDR, ((addr & 0x000fffff) | (3 << 24))); 179 IWM_BARRIER_WRITE(sc); 180 IWM_WRITE(sc, IWM_HBUS_TARG_PRPH_WDAT, val); 181 } 182 183 #ifdef IWM_DEBUG 184 /* iwlwifi: pcie/trans.c */ 185 int 186 iwm_read_mem(struct iwm_softc *sc, uint32_t addr, void *buf, int dwords) 187 { 188 int offs, ret = 0; 189 uint32_t *vals = buf; 190 191 if (iwm_nic_lock(sc)) { 192 IWM_WRITE(sc, IWM_HBUS_TARG_MEM_RADDR, addr); 193 for (offs = 0; offs < dwords; offs++) 194 vals[offs] = IWM_READ(sc, IWM_HBUS_TARG_MEM_RDAT); 195 iwm_nic_unlock(sc); 196 } else { 197 ret = EBUSY; 198 } 199 return ret; 200 } 201 #endif 202 203 /* iwlwifi: pcie/trans.c */ 204 int 205 iwm_write_mem(struct iwm_softc *sc, uint32_t addr, const void *buf, int dwords) 206 { 207 int offs; 208 const uint32_t *vals = buf; 209 210 if (iwm_nic_lock(sc)) { 211 IWM_WRITE(sc, IWM_HBUS_TARG_MEM_WADDR, addr); 212 /* WADDR auto-increments */ 213 for (offs = 0; offs < dwords; offs++) { 214 uint32_t val = vals ? vals[offs] : 0; 215 IWM_WRITE(sc, IWM_HBUS_TARG_MEM_WDAT, val); 216 } 217 iwm_nic_unlock(sc); 218 } else { 219 IWM_DPRINTF(sc, IWM_DEBUG_TRANS, 220 "%s: write_mem failed\n", __func__); 221 return EBUSY; 222 } 223 return 0; 224 } 225 226 int 227 iwm_write_mem32(struct iwm_softc *sc, uint32_t addr, uint32_t val) 228 { 229 return iwm_write_mem(sc, addr, &val, 1); 230 } 231 232 int 233 iwm_poll_bit(struct iwm_softc *sc, int reg, 234 uint32_t bits, uint32_t mask, int timo) 235 { 236 for (;;) { 237 if ((IWM_READ(sc, reg) & mask) == (bits & mask)) { 238 return 1; 239 } 240 if (timo < 10) { 241 return 0; 242 } 243 timo -= 10; 244 DELAY(10); 245 } 246 } 247 248 int 249 iwm_nic_lock(struct iwm_softc *sc) 250 { 251 int rv = 0; 252 253 IWM_SETBITS(sc, IWM_CSR_GP_CNTRL, 254 IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ); 255 256 if (iwm_poll_bit(sc, IWM_CSR_GP_CNTRL, 257 IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN, 258 IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY 259 | IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP, 15000)) { 260 rv = 1; 261 } else { 262 /* jolt */ 263 IWM_WRITE(sc, IWM_CSR_RESET, IWM_CSR_RESET_REG_FLAG_FORCE_NMI); 264 } 265 266 return rv; 267 } 268 269 void 270 iwm_nic_unlock(struct iwm_softc *sc) 271 { 272 IWM_CLRBITS(sc, IWM_CSR_GP_CNTRL, 273 IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ); 274 } 275 276 void 277 iwm_set_bits_mask_prph(struct iwm_softc *sc, 278 uint32_t reg, uint32_t bits, uint32_t mask) 279 { 280 uint32_t val; 281 282 /* XXX: no error path? */ 283 if (iwm_nic_lock(sc)) { 284 val = iwm_read_prph(sc, reg) & mask; 285 val |= bits; 286 iwm_write_prph(sc, reg, val); 287 iwm_nic_unlock(sc); 288 } 289 } 290 291 void 292 iwm_set_bits_prph(struct iwm_softc *sc, uint32_t reg, uint32_t bits) 293 { 294 iwm_set_bits_mask_prph(sc, reg, bits, ~0); 295 } 296 297 void 298 iwm_clear_bits_prph(struct iwm_softc *sc, uint32_t reg, uint32_t bits) 299 { 300 iwm_set_bits_mask_prph(sc, reg, 0, ~bits); 301 } 302 303 /* 304 * High-level hardware frobbing routines 305 */ 306 307 void 308 iwm_enable_rfkill_int(struct iwm_softc *sc) 309 { 310 sc->sc_intmask = IWM_CSR_INT_BIT_RF_KILL; 311 IWM_WRITE(sc, IWM_CSR_INT_MASK, sc->sc_intmask); 312 } 313 314 int 315 iwm_check_rfkill(struct iwm_softc *sc) 316 { 317 uint32_t v; 318 int rv; 319 320 /* 321 * "documentation" is not really helpful here: 322 * 27: HW_RF_KILL_SW 323 * Indicates state of (platform's) hardware RF-Kill switch 324 * 325 * But apparently when it's off, it's on ... 326 */ 327 v = IWM_READ(sc, IWM_CSR_GP_CNTRL); 328 rv = (v & IWM_CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW) == 0; 329 if (rv) { 330 sc->sc_flags |= IWM_FLAG_RFKILL; 331 } else { 332 sc->sc_flags &= ~IWM_FLAG_RFKILL; 333 } 334 335 return rv; 336 } 337 338 339 #define IWM_HW_READY_TIMEOUT 50 340 int 341 iwm_set_hw_ready(struct iwm_softc *sc) 342 { 343 IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG, 344 IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY); 345 346 return iwm_poll_bit(sc, IWM_CSR_HW_IF_CONFIG_REG, 347 IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, 348 IWM_CSR_HW_IF_CONFIG_REG_BIT_NIC_READY, 349 IWM_HW_READY_TIMEOUT); 350 } 351 #undef IWM_HW_READY_TIMEOUT 352 353 int 354 iwm_prepare_card_hw(struct iwm_softc *sc) 355 { 356 int rv = 0; 357 int t = 0; 358 359 IWM_DPRINTF(sc, IWM_DEBUG_RESET, "->%s\n", __func__); 360 if (iwm_set_hw_ready(sc)) 361 goto out; 362 363 /* If HW is not ready, prepare the conditions to check again */ 364 IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG, 365 IWM_CSR_HW_IF_CONFIG_REG_PREPARE); 366 367 do { 368 if (iwm_set_hw_ready(sc)) 369 goto out; 370 DELAY(200); 371 t += 200; 372 } while (t < 150000); 373 374 rv = ETIMEDOUT; 375 376 out: 377 IWM_DPRINTF(sc, IWM_DEBUG_RESET, "<-%s\n", __func__); 378 return rv; 379 } 380 381 void 382 iwm_apm_config(struct iwm_softc *sc) 383 { 384 uint16_t reg; 385 386 #if defined(__DragonFly__) 387 reg = pci_read_config(sc->sc_dev, PCIER_LINKCTRL, sizeof(reg)); 388 if (reg & PCIEM_LNKCTL_ASPM_L1) { 389 #else 390 reg = pci_read_config(sc->sc_dev, PCIER_LINK_CTL, sizeof(reg)); 391 if (reg & PCIEM_LINK_CTL_ASPMC_L1) { 392 #endif 393 /* Um the Linux driver prints "Disabling L0S for this one ... */ 394 IWM_SETBITS(sc, IWM_CSR_GIO_REG, 395 IWM_CSR_GIO_REG_VAL_L0S_ENABLED); 396 } else { 397 /* ... and "Enabling" here */ 398 IWM_CLRBITS(sc, IWM_CSR_GIO_REG, 399 IWM_CSR_GIO_REG_VAL_L0S_ENABLED); 400 } 401 } 402 403 /* 404 * Start up NIC's basic functionality after it has been reset 405 * (e.g. after platform boot, or shutdown via iwm_pcie_apm_stop()) 406 * NOTE: This does not load uCode nor start the embedded processor 407 */ 408 int 409 iwm_apm_init(struct iwm_softc *sc) 410 { 411 int error = 0; 412 413 IWM_DPRINTF(sc, IWM_DEBUG_RESET, "iwm apm start\n"); 414 415 /* Disable L0S exit timer (platform NMI Work/Around) */ 416 IWM_SETBITS(sc, IWM_CSR_GIO_CHICKEN_BITS, 417 IWM_CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER); 418 419 /* 420 * Disable L0s without affecting L1; 421 * don't wait for ICH L0s (ICH bug W/A) 422 */ 423 IWM_SETBITS(sc, IWM_CSR_GIO_CHICKEN_BITS, 424 IWM_CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX); 425 426 /* Set FH wait threshold to maximum (HW error during stress W/A) */ 427 IWM_SETBITS(sc, IWM_CSR_DBG_HPET_MEM_REG, IWM_CSR_DBG_HPET_MEM_REG_VAL); 428 429 /* 430 * Enable HAP INTA (interrupt from management bus) to 431 * wake device's PCI Express link L1a -> L0s 432 */ 433 IWM_SETBITS(sc, IWM_CSR_HW_IF_CONFIG_REG, 434 IWM_CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A); 435 436 iwm_apm_config(sc); 437 438 #if 0 /* not for 7k */ 439 /* Configure analog phase-lock-loop before activating to D0A */ 440 if (trans->cfg->base_params->pll_cfg_val) 441 IWM_SETBITS(trans, IWM_CSR_ANA_PLL_CFG, 442 trans->cfg->base_params->pll_cfg_val); 443 #endif 444 445 /* 446 * Set "initialization complete" bit to move adapter from 447 * D0U* --> D0A* (powered-up active) state. 448 */ 449 IWM_SETBITS(sc, IWM_CSR_GP_CNTRL, IWM_CSR_GP_CNTRL_REG_FLAG_INIT_DONE); 450 451 /* 452 * Wait for clock stabilization; once stabilized, access to 453 * device-internal resources is supported, e.g. iwm_write_prph() 454 * and accesses to uCode SRAM. 455 */ 456 if (!iwm_poll_bit(sc, IWM_CSR_GP_CNTRL, 457 IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 458 IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000)) { 459 device_printf(sc->sc_dev, 460 "timeout waiting for clock stabilization\n"); 461 462 goto out; 463 } 464 465 if (sc->host_interrupt_operation_mode) { 466 /* 467 * This is a bit of an abuse - This is needed for 7260 / 3160 468 * only check host_interrupt_operation_mode even if this is 469 * not related to host_interrupt_operation_mode. 470 * 471 * Enable the oscillator to count wake up time for L1 exit. This 472 * consumes slightly more power (100uA) - but allows to be sure 473 * that we wake up from L1 on time. 474 * 475 * This looks weird: read twice the same register, discard the 476 * value, set a bit, and yet again, read that same register 477 * just to discard the value. But that's the way the hardware 478 * seems to like it. 479 */ 480 iwm_read_prph(sc, IWM_OSC_CLK); 481 iwm_read_prph(sc, IWM_OSC_CLK); 482 iwm_set_bits_prph(sc, IWM_OSC_CLK, IWM_OSC_CLK_FORCE_CONTROL); 483 iwm_read_prph(sc, IWM_OSC_CLK); 484 iwm_read_prph(sc, IWM_OSC_CLK); 485 } 486 487 /* 488 * Enable DMA clock and wait for it to stabilize. 489 * 490 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits 491 * do not disable clocks. This preserves any hardware bits already 492 * set by default in "CLK_CTRL_REG" after reset. 493 */ 494 iwm_write_prph(sc, IWM_APMG_CLK_EN_REG, IWM_APMG_CLK_VAL_DMA_CLK_RQT); 495 //kpause("iwmapm", 0, mstohz(20), NULL); 496 DELAY(20); 497 498 /* Disable L1-Active */ 499 iwm_set_bits_prph(sc, IWM_APMG_PCIDEV_STT_REG, 500 IWM_APMG_PCIDEV_STT_VAL_L1_ACT_DIS); 501 502 /* Clear the interrupt in APMG if the NIC is in RFKILL */ 503 iwm_write_prph(sc, IWM_APMG_RTC_INT_STT_REG, 504 IWM_APMG_RTC_INT_STT_RFKILL); 505 506 out: 507 if (error) 508 device_printf(sc->sc_dev, "apm init error %d\n", error); 509 return error; 510 } 511 512 /* iwlwifi/pcie/trans.c */ 513 void 514 iwm_apm_stop(struct iwm_softc *sc) 515 { 516 /* stop device's busmaster DMA activity */ 517 IWM_SETBITS(sc, IWM_CSR_RESET, IWM_CSR_RESET_REG_FLAG_STOP_MASTER); 518 519 if (!iwm_poll_bit(sc, IWM_CSR_RESET, 520 IWM_CSR_RESET_REG_FLAG_MASTER_DISABLED, 521 IWM_CSR_RESET_REG_FLAG_MASTER_DISABLED, 100)) 522 device_printf(sc->sc_dev, "timeout waiting for master\n"); 523 IWM_DPRINTF(sc, IWM_DEBUG_TRANS, "%s: iwm apm stop\n", __func__); 524 } 525 526 /* iwlwifi pcie/trans.c */ 527 int 528 iwm_start_hw(struct iwm_softc *sc) 529 { 530 int error; 531 532 if ((error = iwm_prepare_card_hw(sc)) != 0) 533 return error; 534 535 /* Reset the entire device */ 536 IWM_WRITE(sc, IWM_CSR_RESET, 537 IWM_CSR_RESET_REG_FLAG_SW_RESET | 538 IWM_CSR_RESET_REG_FLAG_NEVO_RESET); 539 DELAY(10); 540 541 if ((error = iwm_apm_init(sc)) != 0) 542 return error; 543 544 iwm_enable_rfkill_int(sc); 545 iwm_check_rfkill(sc); 546 547 return 0; 548 } 549 550 /* iwlwifi pcie/trans.c (always main power) */ 551 void 552 iwm_set_pwr(struct iwm_softc *sc) 553 { 554 iwm_set_bits_mask_prph(sc, IWM_APMG_PS_CTRL_REG, 555 IWM_APMG_PS_CTRL_VAL_PWR_SRC_VMAIN, ~IWM_APMG_PS_CTRL_MSK_PWR_SRC); 556 } 557 558 /* iwlwifi pcie/rx.c */ 559 int 560 iwm_pcie_rx_stop(struct iwm_softc *sc) 561 { 562 563 IWM_WRITE(sc, IWM_FH_MEM_RCSR_CHNL0_CONFIG_REG, 0); 564 return (iwm_poll_bit(sc, IWM_FH_MEM_RSSR_RX_STATUS_REG, 565 IWM_FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 566 IWM_FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 567 1000)); 568 } 569