1 /*- 2 * Copyright (c) 2007-2009 3 * Damien Bergamini <damien.bergamini@free.fr> 4 * Copyright (c) 2008 5 * Benjamin Close <benjsc@FreeBSD.org> 6 * Copyright (c) 2008 Sam Leffler, Errno Consulting 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 /* 22 * Driver for Intel WiFi Link 4965 and 1000/5000/6000 Series 802.11 network 23 * adapters. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/sockio.h> 31 #include <sys/sysctl.h> 32 #include <sys/mbuf.h> 33 #include <sys/kernel.h> 34 #include <sys/socket.h> 35 #include <sys/systm.h> 36 #include <sys/malloc.h> 37 #include <sys/bus.h> 38 #include <sys/rman.h> 39 #include <sys/endian.h> 40 #include <sys/firmware.h> 41 #include <sys/limits.h> 42 #include <sys/module.h> 43 #include <sys/queue.h> 44 #include <sys/taskqueue.h> 45 46 #include <machine/bus.h> 47 #include <machine/resource.h> 48 #include <machine/clock.h> 49 50 #include <dev/pci/pcireg.h> 51 #include <dev/pci/pcivar.h> 52 53 #include <net/bpf.h> 54 #include <net/if.h> 55 #include <net/if_arp.h> 56 #include <net/ethernet.h> 57 #include <net/if_dl.h> 58 #include <net/if_media.h> 59 #include <net/if_types.h> 60 61 #include <netinet/in.h> 62 #include <netinet/in_systm.h> 63 #include <netinet/in_var.h> 64 #include <netinet/if_ether.h> 65 #include <netinet/ip.h> 66 67 #include <net80211/ieee80211_var.h> 68 #include <net80211/ieee80211_radiotap.h> 69 #include <net80211/ieee80211_regdomain.h> 70 #include <net80211/ieee80211_ratectl.h> 71 72 #include <dev/iwn/if_iwnreg.h> 73 #include <dev/iwn/if_iwnvar.h> 74 75 struct iwn_ident { 76 uint16_t vendor; 77 uint16_t device; 78 const char *name; 79 }; 80 81 static const struct iwn_ident iwn_ident_table[] = { 82 { 0x8086, 0x0082, "Intel(R) Centrino(R) Advanced-N 6205" }, 83 { 0x8086, 0x0083, "Intel(R) Centrino(R) Wireless-N 1000" }, 84 { 0x8086, 0x0084, "Intel(R) Centrino(R) Wireless-N 1000" }, 85 { 0x8086, 0x0085, "Intel(R) Centrino(R) Advanced-N 6205" }, 86 { 0x8086, 0x0087, "Intel(R) Centrino(R) Advanced-N + WiMAX 6250" }, 87 { 0x8086, 0x0089, "Intel(R) Centrino(R) Advanced-N + WiMAX 6250" }, 88 { 0x8086, 0x008a, "Intel(R) Centrino(R) Wireless-N 1030" }, 89 { 0x8086, 0x008b, "Intel(R) Centrino(R) Wireless-N 1030" }, 90 { 0x8086, 0x0090, "Intel(R) Centrino(R) Advanced-N 6230" }, 91 { 0x8086, 0x0091, "Intel(R) Centrino(R) Advanced-N 6230" }, 92 { 0x8086, 0x4229, "Intel(R) Wireless WiFi Link 4965" }, 93 { 0x8086, 0x422b, "Intel(R) Centrino(R) Ultimate-N 6300" }, 94 { 0x8086, 0x422c, "Intel(R) Centrino(R) Advanced-N 6200" }, 95 { 0x8086, 0x422d, "Intel(R) Wireless WiFi Link 4965" }, 96 { 0x8086, 0x4230, "Intel(R) Wireless WiFi Link 4965" }, 97 { 0x8086, 0x4232, "Intel(R) WiFi Link 5100" }, 98 { 0x8086, 0x4233, "Intel(R) Wireless WiFi Link 4965" }, 99 { 0x8086, 0x4235, "Intel(R) Ultimate N WiFi Link 5300" }, 100 { 0x8086, 0x4236, "Intel(R) Ultimate N WiFi Link 5300" }, 101 { 0x8086, 0x4237, "Intel(R) WiFi Link 5100" }, 102 { 0x8086, 0x4238, "Intel(R) Centrino(R) Ultimate-N 6300" }, 103 { 0x8086, 0x4239, "Intel(R) Centrino(R) Advanced-N 6200" }, 104 { 0x8086, 0x423a, "Intel(R) WiMAX/WiFi Link 5350" }, 105 { 0x8086, 0x423b, "Intel(R) WiMAX/WiFi Link 5350" }, 106 { 0x8086, 0x423c, "Intel(R) WiMAX/WiFi Link 5150" }, 107 { 0x8086, 0x423d, "Intel(R) WiMAX/WiFi Link 5150" }, 108 { 0, 0, NULL } 109 }; 110 111 static int iwn_probe(device_t); 112 static int iwn_attach(device_t); 113 static int iwn4965_attach(struct iwn_softc *, uint16_t); 114 static int iwn5000_attach(struct iwn_softc *, uint16_t); 115 static void iwn_radiotap_attach(struct iwn_softc *); 116 static void iwn_sysctlattach(struct iwn_softc *); 117 static struct ieee80211vap *iwn_vap_create(struct ieee80211com *, 118 const char name[IFNAMSIZ], int unit, int opmode, 119 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 120 const uint8_t mac[IEEE80211_ADDR_LEN]); 121 static void iwn_vap_delete(struct ieee80211vap *); 122 static int iwn_detach(device_t); 123 static int iwn_shutdown(device_t); 124 static int iwn_suspend(device_t); 125 static int iwn_resume(device_t); 126 static int iwn_nic_lock(struct iwn_softc *); 127 static int iwn_eeprom_lock(struct iwn_softc *); 128 static int iwn_init_otprom(struct iwn_softc *); 129 static int iwn_read_prom_data(struct iwn_softc *, uint32_t, void *, int); 130 static void iwn_dma_map_addr(void *, bus_dma_segment_t *, int, int); 131 static int iwn_dma_contig_alloc(struct iwn_softc *, struct iwn_dma_info *, 132 void **, bus_size_t, bus_size_t); 133 static void iwn_dma_contig_free(struct iwn_dma_info *); 134 static int iwn_alloc_sched(struct iwn_softc *); 135 static void iwn_free_sched(struct iwn_softc *); 136 static int iwn_alloc_kw(struct iwn_softc *); 137 static void iwn_free_kw(struct iwn_softc *); 138 static int iwn_alloc_ict(struct iwn_softc *); 139 static void iwn_free_ict(struct iwn_softc *); 140 static int iwn_alloc_fwmem(struct iwn_softc *); 141 static void iwn_free_fwmem(struct iwn_softc *); 142 static int iwn_alloc_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 143 static void iwn_reset_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 144 static void iwn_free_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 145 static int iwn_alloc_tx_ring(struct iwn_softc *, struct iwn_tx_ring *, 146 int); 147 static void iwn_reset_tx_ring(struct iwn_softc *, struct iwn_tx_ring *); 148 static void iwn_free_tx_ring(struct iwn_softc *, struct iwn_tx_ring *); 149 static void iwn5000_ict_reset(struct iwn_softc *); 150 static int iwn_read_eeprom(struct iwn_softc *, 151 uint8_t macaddr[IEEE80211_ADDR_LEN]); 152 static void iwn4965_read_eeprom(struct iwn_softc *); 153 static void iwn4965_print_power_group(struct iwn_softc *, int); 154 static void iwn5000_read_eeprom(struct iwn_softc *); 155 static uint32_t iwn_eeprom_channel_flags(struct iwn_eeprom_chan *); 156 static void iwn_read_eeprom_band(struct iwn_softc *, int); 157 static void iwn_read_eeprom_ht40(struct iwn_softc *, int); 158 static void iwn_read_eeprom_channels(struct iwn_softc *, int, uint32_t); 159 static struct iwn_eeprom_chan *iwn_find_eeprom_channel(struct iwn_softc *, 160 struct ieee80211_channel *); 161 static int iwn_setregdomain(struct ieee80211com *, 162 struct ieee80211_regdomain *, int, 163 struct ieee80211_channel[]); 164 static void iwn_read_eeprom_enhinfo(struct iwn_softc *); 165 static struct ieee80211_node *iwn_node_alloc(struct ieee80211vap *, 166 const uint8_t mac[IEEE80211_ADDR_LEN]); 167 static void iwn_newassoc(struct ieee80211_node *, int); 168 static int iwn_media_change(struct ifnet *); 169 static int iwn_newstate(struct ieee80211vap *, enum ieee80211_state, int); 170 static void iwn_calib_timeout(void *); 171 static void iwn_rx_phy(struct iwn_softc *, struct iwn_rx_desc *, 172 struct iwn_rx_data *); 173 static void iwn_rx_done(struct iwn_softc *, struct iwn_rx_desc *, 174 struct iwn_rx_data *); 175 static void iwn_rx_compressed_ba(struct iwn_softc *, struct iwn_rx_desc *, 176 struct iwn_rx_data *); 177 static void iwn5000_rx_calib_results(struct iwn_softc *, 178 struct iwn_rx_desc *, struct iwn_rx_data *); 179 static void iwn_rx_statistics(struct iwn_softc *, struct iwn_rx_desc *, 180 struct iwn_rx_data *); 181 static void iwn4965_tx_done(struct iwn_softc *, struct iwn_rx_desc *, 182 struct iwn_rx_data *); 183 static void iwn5000_tx_done(struct iwn_softc *, struct iwn_rx_desc *, 184 struct iwn_rx_data *); 185 static void iwn_tx_done(struct iwn_softc *, struct iwn_rx_desc *, int, 186 uint8_t); 187 static void iwn_ampdu_tx_done(struct iwn_softc *, int, int, int, void *); 188 static void iwn_cmd_done(struct iwn_softc *, struct iwn_rx_desc *); 189 static void iwn_notif_intr(struct iwn_softc *); 190 static void iwn_wakeup_intr(struct iwn_softc *); 191 static void iwn_rftoggle_intr(struct iwn_softc *); 192 static void iwn_fatal_intr(struct iwn_softc *); 193 static void iwn_intr(void *); 194 static void iwn4965_update_sched(struct iwn_softc *, int, int, uint8_t, 195 uint16_t); 196 static void iwn5000_update_sched(struct iwn_softc *, int, int, uint8_t, 197 uint16_t); 198 #ifdef notyet 199 static void iwn5000_reset_sched(struct iwn_softc *, int, int); 200 #endif 201 static int iwn_tx_data(struct iwn_softc *, struct mbuf *, 202 struct ieee80211_node *); 203 static int iwn_tx_data_raw(struct iwn_softc *, struct mbuf *, 204 struct ieee80211_node *, 205 const struct ieee80211_bpf_params *params); 206 static int iwn_raw_xmit(struct ieee80211_node *, struct mbuf *, 207 const struct ieee80211_bpf_params *); 208 static void iwn_start(struct ifnet *); 209 static void iwn_start_locked(struct ifnet *); 210 static void iwn_watchdog(void *); 211 static int iwn_ioctl(struct ifnet *, u_long, caddr_t); 212 static int iwn_cmd(struct iwn_softc *, int, const void *, int, int); 213 static int iwn4965_add_node(struct iwn_softc *, struct iwn_node_info *, 214 int); 215 static int iwn5000_add_node(struct iwn_softc *, struct iwn_node_info *, 216 int); 217 static int iwn_set_link_quality(struct iwn_softc *, 218 struct ieee80211_node *); 219 static int iwn_add_broadcast_node(struct iwn_softc *, int); 220 static int iwn_updateedca(struct ieee80211com *); 221 static void iwn_update_mcast(struct ifnet *); 222 static void iwn_set_led(struct iwn_softc *, uint8_t, uint8_t, uint8_t); 223 static int iwn_set_critical_temp(struct iwn_softc *); 224 static int iwn_set_timing(struct iwn_softc *, struct ieee80211_node *); 225 static void iwn4965_power_calibration(struct iwn_softc *, int); 226 static int iwn4965_set_txpower(struct iwn_softc *, 227 struct ieee80211_channel *, int); 228 static int iwn5000_set_txpower(struct iwn_softc *, 229 struct ieee80211_channel *, int); 230 static int iwn4965_get_rssi(struct iwn_softc *, struct iwn_rx_stat *); 231 static int iwn5000_get_rssi(struct iwn_softc *, struct iwn_rx_stat *); 232 static int iwn_get_noise(const struct iwn_rx_general_stats *); 233 static int iwn4965_get_temperature(struct iwn_softc *); 234 static int iwn5000_get_temperature(struct iwn_softc *); 235 static int iwn_init_sensitivity(struct iwn_softc *); 236 static void iwn_collect_noise(struct iwn_softc *, 237 const struct iwn_rx_general_stats *); 238 static int iwn4965_init_gains(struct iwn_softc *); 239 static int iwn5000_init_gains(struct iwn_softc *); 240 static int iwn4965_set_gains(struct iwn_softc *); 241 static int iwn5000_set_gains(struct iwn_softc *); 242 static void iwn_tune_sensitivity(struct iwn_softc *, 243 const struct iwn_rx_stats *); 244 static int iwn_send_sensitivity(struct iwn_softc *); 245 static int iwn_set_pslevel(struct iwn_softc *, int, int, int); 246 static int iwn_send_btcoex(struct iwn_softc *); 247 static int iwn_send_advanced_btcoex(struct iwn_softc *); 248 static int iwn_config(struct iwn_softc *); 249 static uint8_t *ieee80211_add_ssid(uint8_t *, const uint8_t *, u_int); 250 static int iwn_scan(struct iwn_softc *); 251 static int iwn_auth(struct iwn_softc *, struct ieee80211vap *vap); 252 static int iwn_run(struct iwn_softc *, struct ieee80211vap *vap); 253 static int iwn_ampdu_rx_start(struct ieee80211_node *, 254 struct ieee80211_rx_ampdu *, int, int, int); 255 static void iwn_ampdu_rx_stop(struct ieee80211_node *, 256 struct ieee80211_rx_ampdu *); 257 static int iwn_addba_request(struct ieee80211_node *, 258 struct ieee80211_tx_ampdu *, int, int, int); 259 static int iwn_addba_response(struct ieee80211_node *, 260 struct ieee80211_tx_ampdu *, int, int, int); 261 static int iwn_ampdu_tx_start(struct ieee80211com *, 262 struct ieee80211_node *, uint8_t); 263 static void iwn_ampdu_tx_stop(struct ieee80211_node *, 264 struct ieee80211_tx_ampdu *); 265 static void iwn4965_ampdu_tx_start(struct iwn_softc *, 266 struct ieee80211_node *, int, uint8_t, uint16_t); 267 static void iwn4965_ampdu_tx_stop(struct iwn_softc *, int, 268 uint8_t, uint16_t); 269 static void iwn5000_ampdu_tx_start(struct iwn_softc *, 270 struct ieee80211_node *, int, uint8_t, uint16_t); 271 static void iwn5000_ampdu_tx_stop(struct iwn_softc *, int, 272 uint8_t, uint16_t); 273 static int iwn5000_query_calibration(struct iwn_softc *); 274 static int iwn5000_send_calibration(struct iwn_softc *); 275 static int iwn5000_send_wimax_coex(struct iwn_softc *); 276 static int iwn5000_crystal_calib(struct iwn_softc *); 277 static int iwn5000_temp_offset_calib(struct iwn_softc *); 278 static int iwn4965_post_alive(struct iwn_softc *); 279 static int iwn5000_post_alive(struct iwn_softc *); 280 static int iwn4965_load_bootcode(struct iwn_softc *, const uint8_t *, 281 int); 282 static int iwn4965_load_firmware(struct iwn_softc *); 283 static int iwn5000_load_firmware_section(struct iwn_softc *, uint32_t, 284 const uint8_t *, int); 285 static int iwn5000_load_firmware(struct iwn_softc *); 286 static int iwn_read_firmware_leg(struct iwn_softc *, 287 struct iwn_fw_info *); 288 static int iwn_read_firmware_tlv(struct iwn_softc *, 289 struct iwn_fw_info *, uint16_t); 290 static int iwn_read_firmware(struct iwn_softc *); 291 static int iwn_clock_wait(struct iwn_softc *); 292 static int iwn_apm_init(struct iwn_softc *); 293 static void iwn_apm_stop_master(struct iwn_softc *); 294 static void iwn_apm_stop(struct iwn_softc *); 295 static int iwn4965_nic_config(struct iwn_softc *); 296 static int iwn5000_nic_config(struct iwn_softc *); 297 static int iwn_hw_prepare(struct iwn_softc *); 298 static int iwn_hw_init(struct iwn_softc *); 299 static void iwn_hw_stop(struct iwn_softc *); 300 static void iwn_radio_on(void *, int); 301 static void iwn_radio_off(void *, int); 302 static void iwn_init_locked(struct iwn_softc *); 303 static void iwn_init(void *); 304 static void iwn_stop_locked(struct iwn_softc *); 305 static void iwn_stop(struct iwn_softc *); 306 static void iwn_scan_start(struct ieee80211com *); 307 static void iwn_scan_end(struct ieee80211com *); 308 static void iwn_set_channel(struct ieee80211com *); 309 static void iwn_scan_curchan(struct ieee80211_scan_state *, unsigned long); 310 static void iwn_scan_mindwell(struct ieee80211_scan_state *); 311 static void iwn_hw_reset(void *, int); 312 313 #define IWN_DEBUG 314 #ifdef IWN_DEBUG 315 enum { 316 IWN_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 317 IWN_DEBUG_RECV = 0x00000002, /* basic recv operation */ 318 IWN_DEBUG_STATE = 0x00000004, /* 802.11 state transitions */ 319 IWN_DEBUG_TXPOW = 0x00000008, /* tx power processing */ 320 IWN_DEBUG_RESET = 0x00000010, /* reset processing */ 321 IWN_DEBUG_OPS = 0x00000020, /* iwn_ops processing */ 322 IWN_DEBUG_BEACON = 0x00000040, /* beacon handling */ 323 IWN_DEBUG_WATCHDOG = 0x00000080, /* watchdog timeout */ 324 IWN_DEBUG_INTR = 0x00000100, /* ISR */ 325 IWN_DEBUG_CALIBRATE = 0x00000200, /* periodic calibration */ 326 IWN_DEBUG_NODE = 0x00000400, /* node management */ 327 IWN_DEBUG_LED = 0x00000800, /* led management */ 328 IWN_DEBUG_CMD = 0x00001000, /* cmd submission */ 329 IWN_DEBUG_FATAL = 0x80000000, /* fatal errors */ 330 IWN_DEBUG_ANY = 0xffffffff 331 }; 332 333 #define DPRINTF(sc, m, fmt, ...) do { \ 334 if (sc->sc_debug & (m)) \ 335 printf(fmt, __VA_ARGS__); \ 336 } while (0) 337 338 static const char * 339 iwn_intr_str(uint8_t cmd) 340 { 341 switch (cmd) { 342 /* Notifications */ 343 case IWN_UC_READY: return "UC_READY"; 344 case IWN_ADD_NODE_DONE: return "ADD_NODE_DONE"; 345 case IWN_TX_DONE: return "TX_DONE"; 346 case IWN_START_SCAN: return "START_SCAN"; 347 case IWN_STOP_SCAN: return "STOP_SCAN"; 348 case IWN_RX_STATISTICS: return "RX_STATS"; 349 case IWN_BEACON_STATISTICS: return "BEACON_STATS"; 350 case IWN_STATE_CHANGED: return "STATE_CHANGED"; 351 case IWN_BEACON_MISSED: return "BEACON_MISSED"; 352 case IWN_RX_PHY: return "RX_PHY"; 353 case IWN_MPDU_RX_DONE: return "MPDU_RX_DONE"; 354 case IWN_RX_DONE: return "RX_DONE"; 355 356 /* Command Notifications */ 357 case IWN_CMD_RXON: return "IWN_CMD_RXON"; 358 case IWN_CMD_RXON_ASSOC: return "IWN_CMD_RXON_ASSOC"; 359 case IWN_CMD_EDCA_PARAMS: return "IWN_CMD_EDCA_PARAMS"; 360 case IWN_CMD_TIMING: return "IWN_CMD_TIMING"; 361 case IWN_CMD_LINK_QUALITY: return "IWN_CMD_LINK_QUALITY"; 362 case IWN_CMD_SET_LED: return "IWN_CMD_SET_LED"; 363 case IWN5000_CMD_WIMAX_COEX: return "IWN5000_CMD_WIMAX_COEX"; 364 case IWN5000_CMD_CALIB_CONFIG: return "IWN5000_CMD_CALIB_CONFIG"; 365 case IWN5000_CMD_CALIB_RESULT: return "IWN5000_CMD_CALIB_RESULT"; 366 case IWN5000_CMD_CALIB_COMPLETE: return "IWN5000_CMD_CALIB_COMPLETE"; 367 case IWN_CMD_SET_POWER_MODE: return "IWN_CMD_SET_POWER_MODE"; 368 case IWN_CMD_SCAN: return "IWN_CMD_SCAN"; 369 case IWN_CMD_SCAN_RESULTS: return "IWN_CMD_SCAN_RESULTS"; 370 case IWN_CMD_TXPOWER: return "IWN_CMD_TXPOWER"; 371 case IWN_CMD_TXPOWER_DBM: return "IWN_CMD_TXPOWER_DBM"; 372 case IWN5000_CMD_TX_ANT_CONFIG: return "IWN5000_CMD_TX_ANT_CONFIG"; 373 case IWN_CMD_BT_COEX: return "IWN_CMD_BT_COEX"; 374 case IWN_CMD_SET_CRITICAL_TEMP: return "IWN_CMD_SET_CRITICAL_TEMP"; 375 case IWN_CMD_SET_SENSITIVITY: return "IWN_CMD_SET_SENSITIVITY"; 376 case IWN_CMD_PHY_CALIB: return "IWN_CMD_PHY_CALIB"; 377 } 378 return "UNKNOWN INTR NOTIF/CMD"; 379 } 380 #else 381 #define DPRINTF(sc, m, fmt, ...) do { (void) sc; } while (0) 382 #endif 383 384 static device_method_t iwn_methods[] = { 385 /* Device interface */ 386 DEVMETHOD(device_probe, iwn_probe), 387 DEVMETHOD(device_attach, iwn_attach), 388 DEVMETHOD(device_detach, iwn_detach), 389 DEVMETHOD(device_shutdown, iwn_shutdown), 390 DEVMETHOD(device_suspend, iwn_suspend), 391 DEVMETHOD(device_resume, iwn_resume), 392 { 0, 0 } 393 }; 394 395 static driver_t iwn_driver = { 396 "iwn", 397 iwn_methods, 398 sizeof(struct iwn_softc) 399 }; 400 static devclass_t iwn_devclass; 401 402 DRIVER_MODULE(iwn, pci, iwn_driver, iwn_devclass, 0, 0); 403 404 MODULE_VERSION(iwn, 1); 405 406 MODULE_DEPEND(iwn, firmware, 1, 1, 1); 407 MODULE_DEPEND(iwn, pci, 1, 1, 1); 408 MODULE_DEPEND(iwn, wlan, 1, 1, 1); 409 410 static int 411 iwn_probe(device_t dev) 412 { 413 const struct iwn_ident *ident; 414 415 for (ident = iwn_ident_table; ident->name != NULL; ident++) { 416 if (pci_get_vendor(dev) == ident->vendor && 417 pci_get_device(dev) == ident->device) { 418 device_set_desc(dev, ident->name); 419 return 0; 420 } 421 } 422 return ENXIO; 423 } 424 425 static int 426 iwn_attach(device_t dev) 427 { 428 struct iwn_softc *sc = (struct iwn_softc *)device_get_softc(dev); 429 struct ieee80211com *ic; 430 struct ifnet *ifp; 431 uint32_t reg; 432 int i, error, result; 433 uint8_t macaddr[IEEE80211_ADDR_LEN]; 434 435 sc->sc_dev = dev; 436 437 /* 438 * Get the offset of the PCI Express Capability Structure in PCI 439 * Configuration Space. 440 */ 441 error = pci_find_cap(dev, PCIY_EXPRESS, &sc->sc_cap_off); 442 if (error != 0) { 443 device_printf(dev, "PCIe capability structure not found!\n"); 444 return error; 445 } 446 447 /* Clear device-specific "PCI retry timeout" register (41h). */ 448 pci_write_config(dev, 0x41, 0, 1); 449 450 /* Hardware bug workaround. */ 451 reg = pci_read_config(dev, PCIR_COMMAND, 1); 452 if (reg & PCIM_CMD_INTxDIS) { 453 DPRINTF(sc, IWN_DEBUG_RESET, "%s: PCIe INTx Disable set\n", 454 __func__); 455 reg &= ~PCIM_CMD_INTxDIS; 456 pci_write_config(dev, PCIR_COMMAND, reg, 1); 457 } 458 459 /* Enable bus-mastering. */ 460 pci_enable_busmaster(dev); 461 462 sc->mem_rid = PCIR_BAR(0); 463 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 464 RF_ACTIVE); 465 if (sc->mem == NULL) { 466 device_printf(dev, "can't map mem space\n"); 467 error = ENOMEM; 468 return error; 469 } 470 sc->sc_st = rman_get_bustag(sc->mem); 471 sc->sc_sh = rman_get_bushandle(sc->mem); 472 473 sc->irq_rid = 0; 474 if ((result = pci_msi_count(dev)) == 1 && 475 pci_alloc_msi(dev, &result) == 0) 476 sc->irq_rid = 1; 477 /* Install interrupt handler. */ 478 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 479 RF_ACTIVE | RF_SHAREABLE); 480 if (sc->irq == NULL) { 481 device_printf(dev, "can't map interrupt\n"); 482 error = ENOMEM; 483 goto fail; 484 } 485 486 IWN_LOCK_INIT(sc); 487 488 /* Read hardware revision and attach. */ 489 sc->hw_type = (IWN_READ(sc, IWN_HW_REV) >> 4) & 0xf; 490 if (sc->hw_type == IWN_HW_REV_TYPE_4965) 491 error = iwn4965_attach(sc, pci_get_device(dev)); 492 else 493 error = iwn5000_attach(sc, pci_get_device(dev)); 494 if (error != 0) { 495 device_printf(dev, "could not attach device, error %d\n", 496 error); 497 goto fail; 498 } 499 500 if ((error = iwn_hw_prepare(sc)) != 0) { 501 device_printf(dev, "hardware not ready, error %d\n", error); 502 goto fail; 503 } 504 505 /* Allocate DMA memory for firmware transfers. */ 506 if ((error = iwn_alloc_fwmem(sc)) != 0) { 507 device_printf(dev, 508 "could not allocate memory for firmware, error %d\n", 509 error); 510 goto fail; 511 } 512 513 /* Allocate "Keep Warm" page. */ 514 if ((error = iwn_alloc_kw(sc)) != 0) { 515 device_printf(dev, 516 "could not allocate keep warm page, error %d\n", error); 517 goto fail; 518 } 519 520 /* Allocate ICT table for 5000 Series. */ 521 if (sc->hw_type != IWN_HW_REV_TYPE_4965 && 522 (error = iwn_alloc_ict(sc)) != 0) { 523 device_printf(dev, "could not allocate ICT table, error %d\n", 524 error); 525 goto fail; 526 } 527 528 /* Allocate TX scheduler "rings". */ 529 if ((error = iwn_alloc_sched(sc)) != 0) { 530 device_printf(dev, 531 "could not allocate TX scheduler rings, error %d\n", error); 532 goto fail; 533 } 534 535 /* Allocate TX rings (16 on 4965AGN, 20 on >=5000). */ 536 for (i = 0; i < sc->ntxqs; i++) { 537 if ((error = iwn_alloc_tx_ring(sc, &sc->txq[i], i)) != 0) { 538 device_printf(dev, 539 "could not allocate TX ring %d, error %d\n", i, 540 error); 541 goto fail; 542 } 543 } 544 545 /* Allocate RX ring. */ 546 if ((error = iwn_alloc_rx_ring(sc, &sc->rxq)) != 0) { 547 device_printf(dev, "could not allocate RX ring, error %d\n", 548 error); 549 goto fail; 550 } 551 552 /* Clear pending interrupts. */ 553 IWN_WRITE(sc, IWN_INT, 0xffffffff); 554 555 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 556 if (ifp == NULL) { 557 device_printf(dev, "can not allocate ifnet structure\n"); 558 goto fail; 559 } 560 561 ic = ifp->if_l2com; 562 ic->ic_ifp = ifp; 563 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 564 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 565 566 /* Set device capabilities. */ 567 ic->ic_caps = 568 IEEE80211_C_STA /* station mode supported */ 569 | IEEE80211_C_MONITOR /* monitor mode supported */ 570 | IEEE80211_C_BGSCAN /* background scanning */ 571 | IEEE80211_C_TXPMGT /* tx power management */ 572 | IEEE80211_C_SHSLOT /* short slot time supported */ 573 | IEEE80211_C_WPA 574 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 575 #if 0 576 | IEEE80211_C_IBSS /* ibss/adhoc mode */ 577 #endif 578 | IEEE80211_C_WME /* WME */ 579 ; 580 581 /* Read MAC address, channels, etc from EEPROM. */ 582 if ((error = iwn_read_eeprom(sc, macaddr)) != 0) { 583 device_printf(dev, "could not read EEPROM, error %d\n", 584 error); 585 goto fail; 586 } 587 588 /* Count the number of available chains. */ 589 sc->ntxchains = 590 ((sc->txchainmask >> 2) & 1) + 591 ((sc->txchainmask >> 1) & 1) + 592 ((sc->txchainmask >> 0) & 1); 593 sc->nrxchains = 594 ((sc->rxchainmask >> 2) & 1) + 595 ((sc->rxchainmask >> 1) & 1) + 596 ((sc->rxchainmask >> 0) & 1); 597 if (bootverbose) { 598 device_printf(dev, "MIMO %dT%dR, %.4s, address %6D\n", 599 sc->ntxchains, sc->nrxchains, sc->eeprom_domain, 600 macaddr, ":"); 601 } 602 603 if (sc->sc_flags & IWN_FLAG_HAS_11N) { 604 ic->ic_rxstream = sc->nrxchains; 605 ic->ic_txstream = sc->ntxchains; 606 ic->ic_htcaps = 607 IEEE80211_HTCAP_SMPS_OFF /* SMPS mode disabled */ 608 | IEEE80211_HTCAP_SHORTGI20 /* short GI in 20MHz */ 609 | IEEE80211_HTCAP_CHWIDTH40 /* 40MHz channel width*/ 610 | IEEE80211_HTCAP_SHORTGI40 /* short GI in 40MHz */ 611 #ifdef notyet 612 | IEEE80211_HTCAP_GREENFIELD 613 #if IWN_RBUF_SIZE == 8192 614 | IEEE80211_HTCAP_MAXAMSDU_7935 /* max A-MSDU length */ 615 #else 616 | IEEE80211_HTCAP_MAXAMSDU_3839 /* max A-MSDU length */ 617 #endif 618 #endif 619 /* s/w capabilities */ 620 | IEEE80211_HTC_HT /* HT operation */ 621 | IEEE80211_HTC_AMPDU /* tx A-MPDU */ 622 #ifdef notyet 623 | IEEE80211_HTC_AMSDU /* tx A-MSDU */ 624 #endif 625 ; 626 } 627 628 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 629 ifp->if_softc = sc; 630 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 631 ifp->if_init = iwn_init; 632 ifp->if_ioctl = iwn_ioctl; 633 ifp->if_start = iwn_start; 634 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 635 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 636 IFQ_SET_READY(&ifp->if_snd); 637 638 ieee80211_ifattach(ic, macaddr); 639 ic->ic_vap_create = iwn_vap_create; 640 ic->ic_vap_delete = iwn_vap_delete; 641 ic->ic_raw_xmit = iwn_raw_xmit; 642 ic->ic_node_alloc = iwn_node_alloc; 643 sc->sc_ampdu_rx_start = ic->ic_ampdu_rx_start; 644 ic->ic_ampdu_rx_start = iwn_ampdu_rx_start; 645 sc->sc_ampdu_rx_stop = ic->ic_ampdu_rx_stop; 646 ic->ic_ampdu_rx_stop = iwn_ampdu_rx_stop; 647 sc->sc_addba_request = ic->ic_addba_request; 648 ic->ic_addba_request = iwn_addba_request; 649 sc->sc_addba_response = ic->ic_addba_response; 650 ic->ic_addba_response = iwn_addba_response; 651 sc->sc_addba_stop = ic->ic_addba_stop; 652 ic->ic_addba_stop = iwn_ampdu_tx_stop; 653 ic->ic_newassoc = iwn_newassoc; 654 ic->ic_wme.wme_update = iwn_updateedca; 655 ic->ic_update_mcast = iwn_update_mcast; 656 ic->ic_scan_start = iwn_scan_start; 657 ic->ic_scan_end = iwn_scan_end; 658 ic->ic_set_channel = iwn_set_channel; 659 ic->ic_scan_curchan = iwn_scan_curchan; 660 ic->ic_scan_mindwell = iwn_scan_mindwell; 661 ic->ic_setregdomain = iwn_setregdomain; 662 663 iwn_radiotap_attach(sc); 664 665 callout_init_mtx(&sc->calib_to, &sc->sc_mtx, 0); 666 callout_init_mtx(&sc->watchdog_to, &sc->sc_mtx, 0); 667 TASK_INIT(&sc->sc_reinit_task, 0, iwn_hw_reset, sc); 668 TASK_INIT(&sc->sc_radioon_task, 0, iwn_radio_on, sc); 669 TASK_INIT(&sc->sc_radiooff_task, 0, iwn_radio_off, sc); 670 671 iwn_sysctlattach(sc); 672 673 /* 674 * Hook our interrupt after all initialization is complete. 675 */ 676 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 677 NULL, iwn_intr, sc, &sc->sc_ih); 678 if (error != 0) { 679 device_printf(dev, "can't establish interrupt, error %d\n", 680 error); 681 goto fail; 682 } 683 684 if (bootverbose) 685 ieee80211_announce(ic); 686 return 0; 687 fail: 688 iwn_detach(dev); 689 return error; 690 } 691 692 static int 693 iwn4965_attach(struct iwn_softc *sc, uint16_t pid) 694 { 695 struct iwn_ops *ops = &sc->ops; 696 697 ops->load_firmware = iwn4965_load_firmware; 698 ops->read_eeprom = iwn4965_read_eeprom; 699 ops->post_alive = iwn4965_post_alive; 700 ops->nic_config = iwn4965_nic_config; 701 ops->update_sched = iwn4965_update_sched; 702 ops->get_temperature = iwn4965_get_temperature; 703 ops->get_rssi = iwn4965_get_rssi; 704 ops->set_txpower = iwn4965_set_txpower; 705 ops->init_gains = iwn4965_init_gains; 706 ops->set_gains = iwn4965_set_gains; 707 ops->add_node = iwn4965_add_node; 708 ops->tx_done = iwn4965_tx_done; 709 ops->ampdu_tx_start = iwn4965_ampdu_tx_start; 710 ops->ampdu_tx_stop = iwn4965_ampdu_tx_stop; 711 sc->ntxqs = IWN4965_NTXQUEUES; 712 sc->firstaggqueue = IWN4965_FIRSTAGGQUEUE; 713 sc->ndmachnls = IWN4965_NDMACHNLS; 714 sc->broadcast_id = IWN4965_ID_BROADCAST; 715 sc->rxonsz = IWN4965_RXONSZ; 716 sc->schedsz = IWN4965_SCHEDSZ; 717 sc->fw_text_maxsz = IWN4965_FW_TEXT_MAXSZ; 718 sc->fw_data_maxsz = IWN4965_FW_DATA_MAXSZ; 719 sc->fwsz = IWN4965_FWSZ; 720 sc->sched_txfact_addr = IWN4965_SCHED_TXFACT; 721 sc->limits = &iwn4965_sensitivity_limits; 722 sc->fwname = "iwn4965fw"; 723 /* Override chains masks, ROM is known to be broken. */ 724 sc->txchainmask = IWN_ANT_AB; 725 sc->rxchainmask = IWN_ANT_ABC; 726 727 return 0; 728 } 729 730 static int 731 iwn5000_attach(struct iwn_softc *sc, uint16_t pid) 732 { 733 struct iwn_ops *ops = &sc->ops; 734 735 ops->load_firmware = iwn5000_load_firmware; 736 ops->read_eeprom = iwn5000_read_eeprom; 737 ops->post_alive = iwn5000_post_alive; 738 ops->nic_config = iwn5000_nic_config; 739 ops->update_sched = iwn5000_update_sched; 740 ops->get_temperature = iwn5000_get_temperature; 741 ops->get_rssi = iwn5000_get_rssi; 742 ops->set_txpower = iwn5000_set_txpower; 743 ops->init_gains = iwn5000_init_gains; 744 ops->set_gains = iwn5000_set_gains; 745 ops->add_node = iwn5000_add_node; 746 ops->tx_done = iwn5000_tx_done; 747 ops->ampdu_tx_start = iwn5000_ampdu_tx_start; 748 ops->ampdu_tx_stop = iwn5000_ampdu_tx_stop; 749 sc->ntxqs = IWN5000_NTXQUEUES; 750 sc->firstaggqueue = IWN5000_FIRSTAGGQUEUE; 751 sc->ndmachnls = IWN5000_NDMACHNLS; 752 sc->broadcast_id = IWN5000_ID_BROADCAST; 753 sc->rxonsz = IWN5000_RXONSZ; 754 sc->schedsz = IWN5000_SCHEDSZ; 755 sc->fw_text_maxsz = IWN5000_FW_TEXT_MAXSZ; 756 sc->fw_data_maxsz = IWN5000_FW_DATA_MAXSZ; 757 sc->fwsz = IWN5000_FWSZ; 758 sc->sched_txfact_addr = IWN5000_SCHED_TXFACT; 759 sc->reset_noise_gain = IWN5000_PHY_CALIB_RESET_NOISE_GAIN; 760 sc->noise_gain = IWN5000_PHY_CALIB_NOISE_GAIN; 761 762 switch (sc->hw_type) { 763 case IWN_HW_REV_TYPE_5100: 764 sc->limits = &iwn5000_sensitivity_limits; 765 sc->fwname = "iwn5000fw"; 766 /* Override chains masks, ROM is known to be broken. */ 767 sc->txchainmask = IWN_ANT_B; 768 sc->rxchainmask = IWN_ANT_AB; 769 break; 770 case IWN_HW_REV_TYPE_5150: 771 sc->limits = &iwn5150_sensitivity_limits; 772 sc->fwname = "iwn5150fw"; 773 break; 774 case IWN_HW_REV_TYPE_5300: 775 case IWN_HW_REV_TYPE_5350: 776 sc->limits = &iwn5000_sensitivity_limits; 777 sc->fwname = "iwn5000fw"; 778 break; 779 case IWN_HW_REV_TYPE_1000: 780 sc->limits = &iwn1000_sensitivity_limits; 781 sc->fwname = "iwn1000fw"; 782 break; 783 case IWN_HW_REV_TYPE_6000: 784 sc->limits = &iwn6000_sensitivity_limits; 785 sc->fwname = "iwn6000fw"; 786 if (pid == 0x422c || pid == 0x4239) { 787 sc->sc_flags |= IWN_FLAG_INTERNAL_PA; 788 /* Override chains masks, ROM is known to be broken. */ 789 sc->txchainmask = IWN_ANT_BC; 790 sc->rxchainmask = IWN_ANT_BC; 791 } 792 break; 793 case IWN_HW_REV_TYPE_6050: 794 sc->limits = &iwn6000_sensitivity_limits; 795 sc->fwname = "iwn6050fw"; 796 /* Override chains masks, ROM is known to be broken. */ 797 sc->txchainmask = IWN_ANT_AB; 798 sc->rxchainmask = IWN_ANT_AB; 799 break; 800 case IWN_HW_REV_TYPE_6005: 801 sc->limits = &iwn6000_sensitivity_limits; 802 if (pid != 0x0082 && pid != 0x0085) { 803 sc->fwname = "iwn6000g2bfw"; 804 sc->sc_flags |= IWN_FLAG_ADV_BTCOEX; 805 } else 806 sc->fwname = "iwn6000g2afw"; 807 break; 808 default: 809 device_printf(sc->sc_dev, "adapter type %d not supported\n", 810 sc->hw_type); 811 return ENOTSUP; 812 } 813 return 0; 814 } 815 816 /* 817 * Attach the interface to 802.11 radiotap. 818 */ 819 static void 820 iwn_radiotap_attach(struct iwn_softc *sc) 821 { 822 struct ifnet *ifp = sc->sc_ifp; 823 struct ieee80211com *ic = ifp->if_l2com; 824 825 ieee80211_radiotap_attach(ic, 826 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 827 IWN_TX_RADIOTAP_PRESENT, 828 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 829 IWN_RX_RADIOTAP_PRESENT); 830 } 831 832 static void 833 iwn_sysctlattach(struct iwn_softc *sc) 834 { 835 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 836 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 837 838 #ifdef IWN_DEBUG 839 sc->sc_debug = 0; 840 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 841 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "control debugging printfs"); 842 #endif 843 } 844 845 static struct ieee80211vap * 846 iwn_vap_create(struct ieee80211com *ic, 847 const char name[IFNAMSIZ], int unit, int opmode, int flags, 848 const uint8_t bssid[IEEE80211_ADDR_LEN], 849 const uint8_t mac[IEEE80211_ADDR_LEN]) 850 { 851 struct iwn_vap *ivp; 852 struct ieee80211vap *vap; 853 854 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 855 return NULL; 856 ivp = (struct iwn_vap *) malloc(sizeof(struct iwn_vap), 857 M_80211_VAP, M_NOWAIT | M_ZERO); 858 if (ivp == NULL) 859 return NULL; 860 vap = &ivp->iv_vap; 861 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 862 vap->iv_bmissthreshold = 10; /* override default */ 863 /* Override with driver methods. */ 864 ivp->iv_newstate = vap->iv_newstate; 865 vap->iv_newstate = iwn_newstate; 866 867 ieee80211_ratectl_init(vap); 868 /* Complete setup. */ 869 ieee80211_vap_attach(vap, iwn_media_change, ieee80211_media_status); 870 ic->ic_opmode = opmode; 871 return vap; 872 } 873 874 static void 875 iwn_vap_delete(struct ieee80211vap *vap) 876 { 877 struct iwn_vap *ivp = IWN_VAP(vap); 878 879 ieee80211_ratectl_deinit(vap); 880 ieee80211_vap_detach(vap); 881 free(ivp, M_80211_VAP); 882 } 883 884 static int 885 iwn_detach(device_t dev) 886 { 887 struct iwn_softc *sc = device_get_softc(dev); 888 struct ifnet *ifp = sc->sc_ifp; 889 struct ieee80211com *ic; 890 int qid; 891 892 if (ifp != NULL) { 893 ic = ifp->if_l2com; 894 895 ieee80211_draintask(ic, &sc->sc_reinit_task); 896 ieee80211_draintask(ic, &sc->sc_radioon_task); 897 ieee80211_draintask(ic, &sc->sc_radiooff_task); 898 899 iwn_stop(sc); 900 callout_drain(&sc->watchdog_to); 901 callout_drain(&sc->calib_to); 902 ieee80211_ifdetach(ic); 903 } 904 905 /* Uninstall interrupt handler. */ 906 if (sc->irq != NULL) { 907 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 908 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 909 if (sc->irq_rid == 1) 910 pci_release_msi(dev); 911 } 912 913 /* Free DMA resources. */ 914 iwn_free_rx_ring(sc, &sc->rxq); 915 for (qid = 0; qid < sc->ntxqs; qid++) 916 iwn_free_tx_ring(sc, &sc->txq[qid]); 917 iwn_free_sched(sc); 918 iwn_free_kw(sc); 919 if (sc->ict != NULL) 920 iwn_free_ict(sc); 921 iwn_free_fwmem(sc); 922 923 if (sc->mem != NULL) 924 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 925 926 if (ifp != NULL) 927 if_free(ifp); 928 929 IWN_LOCK_DESTROY(sc); 930 return 0; 931 } 932 933 static int 934 iwn_shutdown(device_t dev) 935 { 936 struct iwn_softc *sc = device_get_softc(dev); 937 938 iwn_stop(sc); 939 return 0; 940 } 941 942 static int 943 iwn_suspend(device_t dev) 944 { 945 struct iwn_softc *sc = device_get_softc(dev); 946 struct ifnet *ifp = sc->sc_ifp; 947 struct ieee80211com *ic = ifp->if_l2com; 948 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 949 950 iwn_stop(sc); 951 if (vap != NULL) 952 ieee80211_stop(vap); 953 return 0; 954 } 955 956 static int 957 iwn_resume(device_t dev) 958 { 959 struct iwn_softc *sc = device_get_softc(dev); 960 struct ifnet *ifp = sc->sc_ifp; 961 struct ieee80211com *ic = ifp->if_l2com; 962 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 963 964 /* Clear device-specific "PCI retry timeout" register (41h). */ 965 pci_write_config(dev, 0x41, 0, 1); 966 967 if (ifp->if_flags & IFF_UP) { 968 iwn_init(sc); 969 if (vap != NULL) 970 ieee80211_init(vap); 971 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 972 iwn_start(ifp); 973 } 974 return 0; 975 } 976 977 static int 978 iwn_nic_lock(struct iwn_softc *sc) 979 { 980 int ntries; 981 982 /* Request exclusive access to NIC. */ 983 IWN_SETBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_MAC_ACCESS_REQ); 984 985 /* Spin until we actually get the lock. */ 986 for (ntries = 0; ntries < 1000; ntries++) { 987 if ((IWN_READ(sc, IWN_GP_CNTRL) & 988 (IWN_GP_CNTRL_MAC_ACCESS_ENA | IWN_GP_CNTRL_SLEEP)) == 989 IWN_GP_CNTRL_MAC_ACCESS_ENA) 990 return 0; 991 DELAY(10); 992 } 993 return ETIMEDOUT; 994 } 995 996 static __inline void 997 iwn_nic_unlock(struct iwn_softc *sc) 998 { 999 IWN_CLRBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_MAC_ACCESS_REQ); 1000 } 1001 1002 static __inline uint32_t 1003 iwn_prph_read(struct iwn_softc *sc, uint32_t addr) 1004 { 1005 IWN_WRITE(sc, IWN_PRPH_RADDR, IWN_PRPH_DWORD | addr); 1006 IWN_BARRIER_READ_WRITE(sc); 1007 return IWN_READ(sc, IWN_PRPH_RDATA); 1008 } 1009 1010 static __inline void 1011 iwn_prph_write(struct iwn_softc *sc, uint32_t addr, uint32_t data) 1012 { 1013 IWN_WRITE(sc, IWN_PRPH_WADDR, IWN_PRPH_DWORD | addr); 1014 IWN_BARRIER_WRITE(sc); 1015 IWN_WRITE(sc, IWN_PRPH_WDATA, data); 1016 } 1017 1018 static __inline void 1019 iwn_prph_setbits(struct iwn_softc *sc, uint32_t addr, uint32_t mask) 1020 { 1021 iwn_prph_write(sc, addr, iwn_prph_read(sc, addr) | mask); 1022 } 1023 1024 static __inline void 1025 iwn_prph_clrbits(struct iwn_softc *sc, uint32_t addr, uint32_t mask) 1026 { 1027 iwn_prph_write(sc, addr, iwn_prph_read(sc, addr) & ~mask); 1028 } 1029 1030 static __inline void 1031 iwn_prph_write_region_4(struct iwn_softc *sc, uint32_t addr, 1032 const uint32_t *data, int count) 1033 { 1034 for (; count > 0; count--, data++, addr += 4) 1035 iwn_prph_write(sc, addr, *data); 1036 } 1037 1038 static __inline uint32_t 1039 iwn_mem_read(struct iwn_softc *sc, uint32_t addr) 1040 { 1041 IWN_WRITE(sc, IWN_MEM_RADDR, addr); 1042 IWN_BARRIER_READ_WRITE(sc); 1043 return IWN_READ(sc, IWN_MEM_RDATA); 1044 } 1045 1046 static __inline void 1047 iwn_mem_write(struct iwn_softc *sc, uint32_t addr, uint32_t data) 1048 { 1049 IWN_WRITE(sc, IWN_MEM_WADDR, addr); 1050 IWN_BARRIER_WRITE(sc); 1051 IWN_WRITE(sc, IWN_MEM_WDATA, data); 1052 } 1053 1054 static __inline void 1055 iwn_mem_write_2(struct iwn_softc *sc, uint32_t addr, uint16_t data) 1056 { 1057 uint32_t tmp; 1058 1059 tmp = iwn_mem_read(sc, addr & ~3); 1060 if (addr & 3) 1061 tmp = (tmp & 0x0000ffff) | data << 16; 1062 else 1063 tmp = (tmp & 0xffff0000) | data; 1064 iwn_mem_write(sc, addr & ~3, tmp); 1065 } 1066 1067 static __inline void 1068 iwn_mem_read_region_4(struct iwn_softc *sc, uint32_t addr, uint32_t *data, 1069 int count) 1070 { 1071 for (; count > 0; count--, addr += 4) 1072 *data++ = iwn_mem_read(sc, addr); 1073 } 1074 1075 static __inline void 1076 iwn_mem_set_region_4(struct iwn_softc *sc, uint32_t addr, uint32_t val, 1077 int count) 1078 { 1079 for (; count > 0; count--, addr += 4) 1080 iwn_mem_write(sc, addr, val); 1081 } 1082 1083 static int 1084 iwn_eeprom_lock(struct iwn_softc *sc) 1085 { 1086 int i, ntries; 1087 1088 for (i = 0; i < 100; i++) { 1089 /* Request exclusive access to EEPROM. */ 1090 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, 1091 IWN_HW_IF_CONFIG_EEPROM_LOCKED); 1092 1093 /* Spin until we actually get the lock. */ 1094 for (ntries = 0; ntries < 100; ntries++) { 1095 if (IWN_READ(sc, IWN_HW_IF_CONFIG) & 1096 IWN_HW_IF_CONFIG_EEPROM_LOCKED) 1097 return 0; 1098 DELAY(10); 1099 } 1100 } 1101 return ETIMEDOUT; 1102 } 1103 1104 static __inline void 1105 iwn_eeprom_unlock(struct iwn_softc *sc) 1106 { 1107 IWN_CLRBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_EEPROM_LOCKED); 1108 } 1109 1110 /* 1111 * Initialize access by host to One Time Programmable ROM. 1112 * NB: This kind of ROM can be found on 1000 or 6000 Series only. 1113 */ 1114 static int 1115 iwn_init_otprom(struct iwn_softc *sc) 1116 { 1117 uint16_t prev, base, next; 1118 int count, error; 1119 1120 /* Wait for clock stabilization before accessing prph. */ 1121 if ((error = iwn_clock_wait(sc)) != 0) 1122 return error; 1123 1124 if ((error = iwn_nic_lock(sc)) != 0) 1125 return error; 1126 iwn_prph_setbits(sc, IWN_APMG_PS, IWN_APMG_PS_RESET_REQ); 1127 DELAY(5); 1128 iwn_prph_clrbits(sc, IWN_APMG_PS, IWN_APMG_PS_RESET_REQ); 1129 iwn_nic_unlock(sc); 1130 1131 /* Set auto clock gate disable bit for HW with OTP shadow RAM. */ 1132 if (sc->hw_type != IWN_HW_REV_TYPE_1000) { 1133 IWN_SETBITS(sc, IWN_DBG_LINK_PWR_MGMT, 1134 IWN_RESET_LINK_PWR_MGMT_DIS); 1135 } 1136 IWN_CLRBITS(sc, IWN_EEPROM_GP, IWN_EEPROM_GP_IF_OWNER); 1137 /* Clear ECC status. */ 1138 IWN_SETBITS(sc, IWN_OTP_GP, 1139 IWN_OTP_GP_ECC_CORR_STTS | IWN_OTP_GP_ECC_UNCORR_STTS); 1140 1141 /* 1142 * Find the block before last block (contains the EEPROM image) 1143 * for HW without OTP shadow RAM. 1144 */ 1145 if (sc->hw_type == IWN_HW_REV_TYPE_1000) { 1146 /* Switch to absolute addressing mode. */ 1147 IWN_CLRBITS(sc, IWN_OTP_GP, IWN_OTP_GP_RELATIVE_ACCESS); 1148 base = prev = 0; 1149 for (count = 0; count < IWN1000_OTP_NBLOCKS; count++) { 1150 error = iwn_read_prom_data(sc, base, &next, 2); 1151 if (error != 0) 1152 return error; 1153 if (next == 0) /* End of linked-list. */ 1154 break; 1155 prev = base; 1156 base = le16toh(next); 1157 } 1158 if (count == 0 || count == IWN1000_OTP_NBLOCKS) 1159 return EIO; 1160 /* Skip "next" word. */ 1161 sc->prom_base = prev + 1; 1162 } 1163 return 0; 1164 } 1165 1166 static int 1167 iwn_read_prom_data(struct iwn_softc *sc, uint32_t addr, void *data, int count) 1168 { 1169 uint8_t *out = data; 1170 uint32_t val, tmp; 1171 int ntries; 1172 1173 addr += sc->prom_base; 1174 for (; count > 0; count -= 2, addr++) { 1175 IWN_WRITE(sc, IWN_EEPROM, addr << 2); 1176 for (ntries = 0; ntries < 10; ntries++) { 1177 val = IWN_READ(sc, IWN_EEPROM); 1178 if (val & IWN_EEPROM_READ_VALID) 1179 break; 1180 DELAY(5); 1181 } 1182 if (ntries == 10) { 1183 device_printf(sc->sc_dev, 1184 "timeout reading ROM at 0x%x\n", addr); 1185 return ETIMEDOUT; 1186 } 1187 if (sc->sc_flags & IWN_FLAG_HAS_OTPROM) { 1188 /* OTPROM, check for ECC errors. */ 1189 tmp = IWN_READ(sc, IWN_OTP_GP); 1190 if (tmp & IWN_OTP_GP_ECC_UNCORR_STTS) { 1191 device_printf(sc->sc_dev, 1192 "OTPROM ECC error at 0x%x\n", addr); 1193 return EIO; 1194 } 1195 if (tmp & IWN_OTP_GP_ECC_CORR_STTS) { 1196 /* Correctable ECC error, clear bit. */ 1197 IWN_SETBITS(sc, IWN_OTP_GP, 1198 IWN_OTP_GP_ECC_CORR_STTS); 1199 } 1200 } 1201 *out++ = val >> 16; 1202 if (count > 1) 1203 *out++ = val >> 24; 1204 } 1205 return 0; 1206 } 1207 1208 static void 1209 iwn_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1210 { 1211 if (error != 0) 1212 return; 1213 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs)); 1214 *(bus_addr_t *)arg = segs[0].ds_addr; 1215 } 1216 1217 static int 1218 iwn_dma_contig_alloc(struct iwn_softc *sc, struct iwn_dma_info *dma, 1219 void **kvap, bus_size_t size, bus_size_t alignment) 1220 { 1221 int error; 1222 1223 dma->tag = NULL; 1224 dma->size = size; 1225 1226 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment, 1227 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size, 1228 1, size, BUS_DMA_NOWAIT, NULL, NULL, &dma->tag); 1229 if (error != 0) 1230 goto fail; 1231 1232 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr, 1233 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map); 1234 if (error != 0) 1235 goto fail; 1236 1237 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size, 1238 iwn_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT); 1239 if (error != 0) 1240 goto fail; 1241 1242 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 1243 1244 if (kvap != NULL) 1245 *kvap = dma->vaddr; 1246 1247 return 0; 1248 1249 fail: iwn_dma_contig_free(dma); 1250 return error; 1251 } 1252 1253 static void 1254 iwn_dma_contig_free(struct iwn_dma_info *dma) 1255 { 1256 if (dma->map != NULL) { 1257 if (dma->vaddr != NULL) { 1258 bus_dmamap_sync(dma->tag, dma->map, 1259 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1260 bus_dmamap_unload(dma->tag, dma->map); 1261 bus_dmamem_free(dma->tag, &dma->vaddr, dma->map); 1262 dma->vaddr = NULL; 1263 } 1264 bus_dmamap_destroy(dma->tag, dma->map); 1265 dma->map = NULL; 1266 } 1267 if (dma->tag != NULL) { 1268 bus_dma_tag_destroy(dma->tag); 1269 dma->tag = NULL; 1270 } 1271 } 1272 1273 static int 1274 iwn_alloc_sched(struct iwn_softc *sc) 1275 { 1276 /* TX scheduler rings must be aligned on a 1KB boundary. */ 1277 return iwn_dma_contig_alloc(sc, &sc->sched_dma, (void **)&sc->sched, 1278 sc->schedsz, 1024); 1279 } 1280 1281 static void 1282 iwn_free_sched(struct iwn_softc *sc) 1283 { 1284 iwn_dma_contig_free(&sc->sched_dma); 1285 } 1286 1287 static int 1288 iwn_alloc_kw(struct iwn_softc *sc) 1289 { 1290 /* "Keep Warm" page must be aligned on a 4KB boundary. */ 1291 return iwn_dma_contig_alloc(sc, &sc->kw_dma, NULL, 4096, 4096); 1292 } 1293 1294 static void 1295 iwn_free_kw(struct iwn_softc *sc) 1296 { 1297 iwn_dma_contig_free(&sc->kw_dma); 1298 } 1299 1300 static int 1301 iwn_alloc_ict(struct iwn_softc *sc) 1302 { 1303 /* ICT table must be aligned on a 4KB boundary. */ 1304 return iwn_dma_contig_alloc(sc, &sc->ict_dma, (void **)&sc->ict, 1305 IWN_ICT_SIZE, 4096); 1306 } 1307 1308 static void 1309 iwn_free_ict(struct iwn_softc *sc) 1310 { 1311 iwn_dma_contig_free(&sc->ict_dma); 1312 } 1313 1314 static int 1315 iwn_alloc_fwmem(struct iwn_softc *sc) 1316 { 1317 /* Must be aligned on a 16-byte boundary. */ 1318 return iwn_dma_contig_alloc(sc, &sc->fw_dma, NULL, sc->fwsz, 16); 1319 } 1320 1321 static void 1322 iwn_free_fwmem(struct iwn_softc *sc) 1323 { 1324 iwn_dma_contig_free(&sc->fw_dma); 1325 } 1326 1327 static int 1328 iwn_alloc_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 1329 { 1330 bus_size_t size; 1331 int i, error; 1332 1333 ring->cur = 0; 1334 1335 /* Allocate RX descriptors (256-byte aligned). */ 1336 size = IWN_RX_RING_COUNT * sizeof (uint32_t); 1337 error = iwn_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc, 1338 size, 256); 1339 if (error != 0) { 1340 device_printf(sc->sc_dev, 1341 "%s: could not allocate RX ring DMA memory, error %d\n", 1342 __func__, error); 1343 goto fail; 1344 } 1345 1346 /* Allocate RX status area (16-byte aligned). */ 1347 error = iwn_dma_contig_alloc(sc, &ring->stat_dma, (void **)&ring->stat, 1348 sizeof (struct iwn_rx_status), 16); 1349 if (error != 0) { 1350 device_printf(sc->sc_dev, 1351 "%s: could not allocate RX status DMA memory, error %d\n", 1352 __func__, error); 1353 goto fail; 1354 } 1355 1356 /* Create RX buffer DMA tag. */ 1357 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 1358 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 1359 IWN_RBUF_SIZE, 1, IWN_RBUF_SIZE, BUS_DMA_NOWAIT, NULL, NULL, 1360 &ring->data_dmat); 1361 if (error != 0) { 1362 device_printf(sc->sc_dev, 1363 "%s: could not create RX buf DMA tag, error %d\n", 1364 __func__, error); 1365 goto fail; 1366 } 1367 1368 /* 1369 * Allocate and map RX buffers. 1370 */ 1371 for (i = 0; i < IWN_RX_RING_COUNT; i++) { 1372 struct iwn_rx_data *data = &ring->data[i]; 1373 bus_addr_t paddr; 1374 1375 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 1376 if (error != 0) { 1377 device_printf(sc->sc_dev, 1378 "%s: could not create RX buf DMA map, error %d\n", 1379 __func__, error); 1380 goto fail; 1381 } 1382 1383 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, 1384 IWN_RBUF_SIZE); 1385 if (data->m == NULL) { 1386 device_printf(sc->sc_dev, 1387 "%s: could not allocate RX mbuf\n", __func__); 1388 error = ENOBUFS; 1389 goto fail; 1390 } 1391 1392 error = bus_dmamap_load(ring->data_dmat, data->map, 1393 mtod(data->m, void *), IWN_RBUF_SIZE, iwn_dma_map_addr, 1394 &paddr, BUS_DMA_NOWAIT); 1395 if (error != 0 && error != EFBIG) { 1396 device_printf(sc->sc_dev, 1397 "%s: can't not map mbuf, error %d\n", __func__, 1398 error); 1399 goto fail; 1400 } 1401 1402 /* Set physical address of RX buffer (256-byte aligned). */ 1403 ring->desc[i] = htole32(paddr >> 8); 1404 } 1405 1406 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 1407 BUS_DMASYNC_PREWRITE); 1408 1409 return 0; 1410 1411 fail: iwn_free_rx_ring(sc, ring); 1412 return error; 1413 } 1414 1415 static void 1416 iwn_reset_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 1417 { 1418 int ntries; 1419 1420 if (iwn_nic_lock(sc) == 0) { 1421 IWN_WRITE(sc, IWN_FH_RX_CONFIG, 0); 1422 for (ntries = 0; ntries < 1000; ntries++) { 1423 if (IWN_READ(sc, IWN_FH_RX_STATUS) & 1424 IWN_FH_RX_STATUS_IDLE) 1425 break; 1426 DELAY(10); 1427 } 1428 iwn_nic_unlock(sc); 1429 } 1430 ring->cur = 0; 1431 sc->last_rx_valid = 0; 1432 } 1433 1434 static void 1435 iwn_free_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 1436 { 1437 int i; 1438 1439 iwn_dma_contig_free(&ring->desc_dma); 1440 iwn_dma_contig_free(&ring->stat_dma); 1441 1442 for (i = 0; i < IWN_RX_RING_COUNT; i++) { 1443 struct iwn_rx_data *data = &ring->data[i]; 1444 1445 if (data->m != NULL) { 1446 bus_dmamap_sync(ring->data_dmat, data->map, 1447 BUS_DMASYNC_POSTREAD); 1448 bus_dmamap_unload(ring->data_dmat, data->map); 1449 m_freem(data->m); 1450 data->m = NULL; 1451 } 1452 if (data->map != NULL) 1453 bus_dmamap_destroy(ring->data_dmat, data->map); 1454 } 1455 if (ring->data_dmat != NULL) { 1456 bus_dma_tag_destroy(ring->data_dmat); 1457 ring->data_dmat = NULL; 1458 } 1459 } 1460 1461 static int 1462 iwn_alloc_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring, int qid) 1463 { 1464 bus_addr_t paddr; 1465 bus_size_t size; 1466 int i, error; 1467 1468 ring->qid = qid; 1469 ring->queued = 0; 1470 ring->cur = 0; 1471 1472 /* Allocate TX descriptors (256-byte aligned). */ 1473 size = IWN_TX_RING_COUNT * sizeof (struct iwn_tx_desc); 1474 error = iwn_dma_contig_alloc(sc, &ring->desc_dma, (void **)&ring->desc, 1475 size, 256); 1476 if (error != 0) { 1477 device_printf(sc->sc_dev, 1478 "%s: could not allocate TX ring DMA memory, error %d\n", 1479 __func__, error); 1480 goto fail; 1481 } 1482 1483 size = IWN_TX_RING_COUNT * sizeof (struct iwn_tx_cmd); 1484 error = iwn_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd, 1485 size, 4); 1486 if (error != 0) { 1487 device_printf(sc->sc_dev, 1488 "%s: could not allocate TX cmd DMA memory, error %d\n", 1489 __func__, error); 1490 goto fail; 1491 } 1492 1493 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 1494 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1495 IWN_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL, 1496 &ring->data_dmat); 1497 if (error != 0) { 1498 device_printf(sc->sc_dev, 1499 "%s: could not create TX buf DMA tag, error %d\n", 1500 __func__, error); 1501 goto fail; 1502 } 1503 1504 paddr = ring->cmd_dma.paddr; 1505 for (i = 0; i < IWN_TX_RING_COUNT; i++) { 1506 struct iwn_tx_data *data = &ring->data[i]; 1507 1508 data->cmd_paddr = paddr; 1509 data->scratch_paddr = paddr + 12; 1510 paddr += sizeof (struct iwn_tx_cmd); 1511 1512 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 1513 if (error != 0) { 1514 device_printf(sc->sc_dev, 1515 "%s: could not create TX buf DMA map, error %d\n", 1516 __func__, error); 1517 goto fail; 1518 } 1519 } 1520 return 0; 1521 1522 fail: iwn_free_tx_ring(sc, ring); 1523 return error; 1524 } 1525 1526 static void 1527 iwn_reset_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring) 1528 { 1529 int i; 1530 1531 for (i = 0; i < IWN_TX_RING_COUNT; i++) { 1532 struct iwn_tx_data *data = &ring->data[i]; 1533 1534 if (data->m != NULL) { 1535 bus_dmamap_sync(ring->data_dmat, data->map, 1536 BUS_DMASYNC_POSTWRITE); 1537 bus_dmamap_unload(ring->data_dmat, data->map); 1538 m_freem(data->m); 1539 data->m = NULL; 1540 } 1541 } 1542 /* Clear TX descriptors. */ 1543 memset(ring->desc, 0, ring->desc_dma.size); 1544 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 1545 BUS_DMASYNC_PREWRITE); 1546 sc->qfullmsk &= ~(1 << ring->qid); 1547 ring->queued = 0; 1548 ring->cur = 0; 1549 } 1550 1551 static void 1552 iwn_free_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring) 1553 { 1554 int i; 1555 1556 iwn_dma_contig_free(&ring->desc_dma); 1557 iwn_dma_contig_free(&ring->cmd_dma); 1558 1559 for (i = 0; i < IWN_TX_RING_COUNT; i++) { 1560 struct iwn_tx_data *data = &ring->data[i]; 1561 1562 if (data->m != NULL) { 1563 bus_dmamap_sync(ring->data_dmat, data->map, 1564 BUS_DMASYNC_POSTWRITE); 1565 bus_dmamap_unload(ring->data_dmat, data->map); 1566 m_freem(data->m); 1567 } 1568 if (data->map != NULL) 1569 bus_dmamap_destroy(ring->data_dmat, data->map); 1570 } 1571 if (ring->data_dmat != NULL) { 1572 bus_dma_tag_destroy(ring->data_dmat); 1573 ring->data_dmat = NULL; 1574 } 1575 } 1576 1577 static void 1578 iwn5000_ict_reset(struct iwn_softc *sc) 1579 { 1580 /* Disable interrupts. */ 1581 IWN_WRITE(sc, IWN_INT_MASK, 0); 1582 1583 /* Reset ICT table. */ 1584 memset(sc->ict, 0, IWN_ICT_SIZE); 1585 sc->ict_cur = 0; 1586 1587 /* Set physical address of ICT table (4KB aligned). */ 1588 DPRINTF(sc, IWN_DEBUG_RESET, "%s: enabling ICT\n", __func__); 1589 IWN_WRITE(sc, IWN_DRAM_INT_TBL, IWN_DRAM_INT_TBL_ENABLE | 1590 IWN_DRAM_INT_TBL_WRAP_CHECK | sc->ict_dma.paddr >> 12); 1591 1592 /* Enable periodic RX interrupt. */ 1593 sc->int_mask |= IWN_INT_RX_PERIODIC; 1594 /* Switch to ICT interrupt mode in driver. */ 1595 sc->sc_flags |= IWN_FLAG_USE_ICT; 1596 1597 /* Re-enable interrupts. */ 1598 IWN_WRITE(sc, IWN_INT, 0xffffffff); 1599 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask); 1600 } 1601 1602 static int 1603 iwn_read_eeprom(struct iwn_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN]) 1604 { 1605 struct iwn_ops *ops = &sc->ops; 1606 uint16_t val; 1607 int error; 1608 1609 /* Check whether adapter has an EEPROM or an OTPROM. */ 1610 if (sc->hw_type >= IWN_HW_REV_TYPE_1000 && 1611 (IWN_READ(sc, IWN_OTP_GP) & IWN_OTP_GP_DEV_SEL_OTP)) 1612 sc->sc_flags |= IWN_FLAG_HAS_OTPROM; 1613 DPRINTF(sc, IWN_DEBUG_RESET, "%s found\n", 1614 (sc->sc_flags & IWN_FLAG_HAS_OTPROM) ? "OTPROM" : "EEPROM"); 1615 1616 /* Adapter has to be powered on for EEPROM access to work. */ 1617 if ((error = iwn_apm_init(sc)) != 0) { 1618 device_printf(sc->sc_dev, 1619 "%s: could not power ON adapter, error %d\n", __func__, 1620 error); 1621 return error; 1622 } 1623 1624 if ((IWN_READ(sc, IWN_EEPROM_GP) & 0x7) == 0) { 1625 device_printf(sc->sc_dev, "%s: bad ROM signature\n", __func__); 1626 return EIO; 1627 } 1628 if ((error = iwn_eeprom_lock(sc)) != 0) { 1629 device_printf(sc->sc_dev, "%s: could not lock ROM, error %d\n", 1630 __func__, error); 1631 return error; 1632 } 1633 if (sc->sc_flags & IWN_FLAG_HAS_OTPROM) { 1634 if ((error = iwn_init_otprom(sc)) != 0) { 1635 device_printf(sc->sc_dev, 1636 "%s: could not initialize OTPROM, error %d\n", 1637 __func__, error); 1638 return error; 1639 } 1640 } 1641 1642 iwn_read_prom_data(sc, IWN_EEPROM_SKU_CAP, &val, 2); 1643 DPRINTF(sc, IWN_DEBUG_RESET, "SKU capabilities=0x%04x\n", le16toh(val)); 1644 /* Check if HT support is bonded out. */ 1645 if (val & htole16(IWN_EEPROM_SKU_CAP_11N)) 1646 sc->sc_flags |= IWN_FLAG_HAS_11N; 1647 1648 iwn_read_prom_data(sc, IWN_EEPROM_RFCFG, &val, 2); 1649 sc->rfcfg = le16toh(val); 1650 DPRINTF(sc, IWN_DEBUG_RESET, "radio config=0x%04x\n", sc->rfcfg); 1651 /* Read Tx/Rx chains from ROM unless it's known to be broken. */ 1652 if (sc->txchainmask == 0) 1653 sc->txchainmask = IWN_RFCFG_TXANTMSK(sc->rfcfg); 1654 if (sc->rxchainmask == 0) 1655 sc->rxchainmask = IWN_RFCFG_RXANTMSK(sc->rfcfg); 1656 1657 /* Read MAC address. */ 1658 iwn_read_prom_data(sc, IWN_EEPROM_MAC, macaddr, 6); 1659 1660 /* Read adapter-specific information from EEPROM. */ 1661 ops->read_eeprom(sc); 1662 1663 iwn_apm_stop(sc); /* Power OFF adapter. */ 1664 1665 iwn_eeprom_unlock(sc); 1666 return 0; 1667 } 1668 1669 static void 1670 iwn4965_read_eeprom(struct iwn_softc *sc) 1671 { 1672 uint32_t addr; 1673 uint16_t val; 1674 int i; 1675 1676 /* Read regulatory domain (4 ASCII characters). */ 1677 iwn_read_prom_data(sc, IWN4965_EEPROM_DOMAIN, sc->eeprom_domain, 4); 1678 1679 /* Read the list of authorized channels (20MHz ones only). */ 1680 for (i = 0; i < 7; i++) { 1681 addr = iwn4965_regulatory_bands[i]; 1682 iwn_read_eeprom_channels(sc, i, addr); 1683 } 1684 1685 /* Read maximum allowed TX power for 2GHz and 5GHz bands. */ 1686 iwn_read_prom_data(sc, IWN4965_EEPROM_MAXPOW, &val, 2); 1687 sc->maxpwr2GHz = val & 0xff; 1688 sc->maxpwr5GHz = val >> 8; 1689 /* Check that EEPROM values are within valid range. */ 1690 if (sc->maxpwr5GHz < 20 || sc->maxpwr5GHz > 50) 1691 sc->maxpwr5GHz = 38; 1692 if (sc->maxpwr2GHz < 20 || sc->maxpwr2GHz > 50) 1693 sc->maxpwr2GHz = 38; 1694 DPRINTF(sc, IWN_DEBUG_RESET, "maxpwr 2GHz=%d 5GHz=%d\n", 1695 sc->maxpwr2GHz, sc->maxpwr5GHz); 1696 1697 /* Read samples for each TX power group. */ 1698 iwn_read_prom_data(sc, IWN4965_EEPROM_BANDS, sc->bands, 1699 sizeof sc->bands); 1700 1701 /* Read voltage at which samples were taken. */ 1702 iwn_read_prom_data(sc, IWN4965_EEPROM_VOLTAGE, &val, 2); 1703 sc->eeprom_voltage = (int16_t)le16toh(val); 1704 DPRINTF(sc, IWN_DEBUG_RESET, "voltage=%d (in 0.3V)\n", 1705 sc->eeprom_voltage); 1706 1707 #ifdef IWN_DEBUG 1708 /* Print samples. */ 1709 if (sc->sc_debug & IWN_DEBUG_ANY) { 1710 for (i = 0; i < IWN_NBANDS; i++) 1711 iwn4965_print_power_group(sc, i); 1712 } 1713 #endif 1714 } 1715 1716 #ifdef IWN_DEBUG 1717 static void 1718 iwn4965_print_power_group(struct iwn_softc *sc, int i) 1719 { 1720 struct iwn4965_eeprom_band *band = &sc->bands[i]; 1721 struct iwn4965_eeprom_chan_samples *chans = band->chans; 1722 int j, c; 1723 1724 printf("===band %d===\n", i); 1725 printf("chan lo=%d, chan hi=%d\n", band->lo, band->hi); 1726 printf("chan1 num=%d\n", chans[0].num); 1727 for (c = 0; c < 2; c++) { 1728 for (j = 0; j < IWN_NSAMPLES; j++) { 1729 printf("chain %d, sample %d: temp=%d gain=%d " 1730 "power=%d pa_det=%d\n", c, j, 1731 chans[0].samples[c][j].temp, 1732 chans[0].samples[c][j].gain, 1733 chans[0].samples[c][j].power, 1734 chans[0].samples[c][j].pa_det); 1735 } 1736 } 1737 printf("chan2 num=%d\n", chans[1].num); 1738 for (c = 0; c < 2; c++) { 1739 for (j = 0; j < IWN_NSAMPLES; j++) { 1740 printf("chain %d, sample %d: temp=%d gain=%d " 1741 "power=%d pa_det=%d\n", c, j, 1742 chans[1].samples[c][j].temp, 1743 chans[1].samples[c][j].gain, 1744 chans[1].samples[c][j].power, 1745 chans[1].samples[c][j].pa_det); 1746 } 1747 } 1748 } 1749 #endif 1750 1751 static void 1752 iwn5000_read_eeprom(struct iwn_softc *sc) 1753 { 1754 struct iwn5000_eeprom_calib_hdr hdr; 1755 int32_t volt; 1756 uint32_t base, addr; 1757 uint16_t val; 1758 int i; 1759 1760 /* Read regulatory domain (4 ASCII characters). */ 1761 iwn_read_prom_data(sc, IWN5000_EEPROM_REG, &val, 2); 1762 base = le16toh(val); 1763 iwn_read_prom_data(sc, base + IWN5000_EEPROM_DOMAIN, 1764 sc->eeprom_domain, 4); 1765 1766 /* Read the list of authorized channels (20MHz ones only). */ 1767 for (i = 0; i < 7; i++) { 1768 if (sc->hw_type >= IWN_HW_REV_TYPE_6000) 1769 addr = base + iwn6000_regulatory_bands[i]; 1770 else 1771 addr = base + iwn5000_regulatory_bands[i]; 1772 iwn_read_eeprom_channels(sc, i, addr); 1773 } 1774 1775 /* Read enhanced TX power information for 6000 Series. */ 1776 if (sc->hw_type >= IWN_HW_REV_TYPE_6000) 1777 iwn_read_eeprom_enhinfo(sc); 1778 1779 iwn_read_prom_data(sc, IWN5000_EEPROM_CAL, &val, 2); 1780 base = le16toh(val); 1781 iwn_read_prom_data(sc, base, &hdr, sizeof hdr); 1782 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 1783 "%s: calib version=%u pa type=%u voltage=%u\n", __func__, 1784 hdr.version, hdr.pa_type, le16toh(hdr.volt)); 1785 sc->calib_ver = hdr.version; 1786 1787 if (sc->hw_type == IWN_HW_REV_TYPE_5150) { 1788 /* Compute temperature offset. */ 1789 iwn_read_prom_data(sc, base + IWN5000_EEPROM_TEMP, &val, 2); 1790 sc->eeprom_temp = le16toh(val); 1791 iwn_read_prom_data(sc, base + IWN5000_EEPROM_VOLT, &val, 2); 1792 volt = le16toh(val); 1793 sc->temp_off = sc->eeprom_temp - (volt / -5); 1794 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "temp=%d volt=%d offset=%dK\n", 1795 sc->eeprom_temp, volt, sc->temp_off); 1796 } else { 1797 /* Read crystal calibration. */ 1798 iwn_read_prom_data(sc, base + IWN5000_EEPROM_CRYSTAL, 1799 &sc->eeprom_crystal, sizeof (uint32_t)); 1800 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "crystal calibration 0x%08x\n", 1801 le32toh(sc->eeprom_crystal)); 1802 } 1803 } 1804 1805 /* 1806 * Translate EEPROM flags to net80211. 1807 */ 1808 static uint32_t 1809 iwn_eeprom_channel_flags(struct iwn_eeprom_chan *channel) 1810 { 1811 uint32_t nflags; 1812 1813 nflags = 0; 1814 if ((channel->flags & IWN_EEPROM_CHAN_ACTIVE) == 0) 1815 nflags |= IEEE80211_CHAN_PASSIVE; 1816 if ((channel->flags & IWN_EEPROM_CHAN_IBSS) == 0) 1817 nflags |= IEEE80211_CHAN_NOADHOC; 1818 if (channel->flags & IWN_EEPROM_CHAN_RADAR) { 1819 nflags |= IEEE80211_CHAN_DFS; 1820 /* XXX apparently IBSS may still be marked */ 1821 nflags |= IEEE80211_CHAN_NOADHOC; 1822 } 1823 1824 return nflags; 1825 } 1826 1827 static void 1828 iwn_read_eeprom_band(struct iwn_softc *sc, int n) 1829 { 1830 struct ifnet *ifp = sc->sc_ifp; 1831 struct ieee80211com *ic = ifp->if_l2com; 1832 struct iwn_eeprom_chan *channels = sc->eeprom_channels[n]; 1833 const struct iwn_chan_band *band = &iwn_bands[n]; 1834 struct ieee80211_channel *c; 1835 uint8_t chan; 1836 int i, nflags; 1837 1838 for (i = 0; i < band->nchan; i++) { 1839 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID)) { 1840 DPRINTF(sc, IWN_DEBUG_RESET, 1841 "skip chan %d flags 0x%x maxpwr %d\n", 1842 band->chan[i], channels[i].flags, 1843 channels[i].maxpwr); 1844 continue; 1845 } 1846 chan = band->chan[i]; 1847 nflags = iwn_eeprom_channel_flags(&channels[i]); 1848 1849 c = &ic->ic_channels[ic->ic_nchans++]; 1850 c->ic_ieee = chan; 1851 c->ic_maxregpower = channels[i].maxpwr; 1852 c->ic_maxpower = 2*c->ic_maxregpower; 1853 1854 if (n == 0) { /* 2GHz band */ 1855 c->ic_freq = ieee80211_ieee2mhz(chan, IEEE80211_CHAN_G); 1856 /* G =>'s B is supported */ 1857 c->ic_flags = IEEE80211_CHAN_B | nflags; 1858 c = &ic->ic_channels[ic->ic_nchans++]; 1859 c[0] = c[-1]; 1860 c->ic_flags = IEEE80211_CHAN_G | nflags; 1861 } else { /* 5GHz band */ 1862 c->ic_freq = ieee80211_ieee2mhz(chan, IEEE80211_CHAN_A); 1863 c->ic_flags = IEEE80211_CHAN_A | nflags; 1864 } 1865 1866 /* Save maximum allowed TX power for this channel. */ 1867 sc->maxpwr[chan] = channels[i].maxpwr; 1868 1869 DPRINTF(sc, IWN_DEBUG_RESET, 1870 "add chan %d flags 0x%x maxpwr %d\n", chan, 1871 channels[i].flags, channels[i].maxpwr); 1872 1873 if (sc->sc_flags & IWN_FLAG_HAS_11N) { 1874 /* add HT20, HT40 added separately */ 1875 c = &ic->ic_channels[ic->ic_nchans++]; 1876 c[0] = c[-1]; 1877 c->ic_flags |= IEEE80211_CHAN_HT20; 1878 } 1879 } 1880 } 1881 1882 static void 1883 iwn_read_eeprom_ht40(struct iwn_softc *sc, int n) 1884 { 1885 struct ifnet *ifp = sc->sc_ifp; 1886 struct ieee80211com *ic = ifp->if_l2com; 1887 struct iwn_eeprom_chan *channels = sc->eeprom_channels[n]; 1888 const struct iwn_chan_band *band = &iwn_bands[n]; 1889 struct ieee80211_channel *c, *cent, *extc; 1890 uint8_t chan; 1891 int i, nflags; 1892 1893 if (!(sc->sc_flags & IWN_FLAG_HAS_11N)) 1894 return; 1895 1896 for (i = 0; i < band->nchan; i++) { 1897 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID)) { 1898 DPRINTF(sc, IWN_DEBUG_RESET, 1899 "skip chan %d flags 0x%x maxpwr %d\n", 1900 band->chan[i], channels[i].flags, 1901 channels[i].maxpwr); 1902 continue; 1903 } 1904 chan = band->chan[i]; 1905 nflags = iwn_eeprom_channel_flags(&channels[i]); 1906 1907 /* 1908 * Each entry defines an HT40 channel pair; find the 1909 * center channel, then the extension channel above. 1910 */ 1911 cent = ieee80211_find_channel_byieee(ic, chan, 1912 (n == 5 ? IEEE80211_CHAN_G : IEEE80211_CHAN_A)); 1913 if (cent == NULL) { /* XXX shouldn't happen */ 1914 device_printf(sc->sc_dev, 1915 "%s: no entry for channel %d\n", __func__, chan); 1916 continue; 1917 } 1918 extc = ieee80211_find_channel(ic, cent->ic_freq+20, 1919 (n == 5 ? IEEE80211_CHAN_G : IEEE80211_CHAN_A)); 1920 if (extc == NULL) { 1921 DPRINTF(sc, IWN_DEBUG_RESET, 1922 "%s: skip chan %d, extension channel not found\n", 1923 __func__, chan); 1924 continue; 1925 } 1926 1927 DPRINTF(sc, IWN_DEBUG_RESET, 1928 "add ht40 chan %d flags 0x%x maxpwr %d\n", 1929 chan, channels[i].flags, channels[i].maxpwr); 1930 1931 c = &ic->ic_channels[ic->ic_nchans++]; 1932 c[0] = cent[0]; 1933 c->ic_extieee = extc->ic_ieee; 1934 c->ic_flags &= ~IEEE80211_CHAN_HT; 1935 c->ic_flags |= IEEE80211_CHAN_HT40U | nflags; 1936 c = &ic->ic_channels[ic->ic_nchans++]; 1937 c[0] = extc[0]; 1938 c->ic_extieee = cent->ic_ieee; 1939 c->ic_flags &= ~IEEE80211_CHAN_HT; 1940 c->ic_flags |= IEEE80211_CHAN_HT40D | nflags; 1941 } 1942 } 1943 1944 static void 1945 iwn_read_eeprom_channels(struct iwn_softc *sc, int n, uint32_t addr) 1946 { 1947 struct ifnet *ifp = sc->sc_ifp; 1948 struct ieee80211com *ic = ifp->if_l2com; 1949 1950 iwn_read_prom_data(sc, addr, &sc->eeprom_channels[n], 1951 iwn_bands[n].nchan * sizeof (struct iwn_eeprom_chan)); 1952 1953 if (n < 5) 1954 iwn_read_eeprom_band(sc, n); 1955 else 1956 iwn_read_eeprom_ht40(sc, n); 1957 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans); 1958 } 1959 1960 static struct iwn_eeprom_chan * 1961 iwn_find_eeprom_channel(struct iwn_softc *sc, struct ieee80211_channel *c) 1962 { 1963 int band, chan, i, j; 1964 1965 if (IEEE80211_IS_CHAN_HT40(c)) { 1966 band = IEEE80211_IS_CHAN_5GHZ(c) ? 6 : 5; 1967 if (IEEE80211_IS_CHAN_HT40D(c)) 1968 chan = c->ic_extieee; 1969 else 1970 chan = c->ic_ieee; 1971 for (i = 0; i < iwn_bands[band].nchan; i++) { 1972 if (iwn_bands[band].chan[i] == chan) 1973 return &sc->eeprom_channels[band][i]; 1974 } 1975 } else { 1976 for (j = 0; j < 5; j++) { 1977 for (i = 0; i < iwn_bands[j].nchan; i++) { 1978 if (iwn_bands[j].chan[i] == c->ic_ieee) 1979 return &sc->eeprom_channels[j][i]; 1980 } 1981 } 1982 } 1983 return NULL; 1984 } 1985 1986 /* 1987 * Enforce flags read from EEPROM. 1988 */ 1989 static int 1990 iwn_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd, 1991 int nchan, struct ieee80211_channel chans[]) 1992 { 1993 struct iwn_softc *sc = ic->ic_ifp->if_softc; 1994 int i; 1995 1996 for (i = 0; i < nchan; i++) { 1997 struct ieee80211_channel *c = &chans[i]; 1998 struct iwn_eeprom_chan *channel; 1999 2000 channel = iwn_find_eeprom_channel(sc, c); 2001 if (channel == NULL) { 2002 if_printf(ic->ic_ifp, 2003 "%s: invalid channel %u freq %u/0x%x\n", 2004 __func__, c->ic_ieee, c->ic_freq, c->ic_flags); 2005 return EINVAL; 2006 } 2007 c->ic_flags |= iwn_eeprom_channel_flags(channel); 2008 } 2009 2010 return 0; 2011 } 2012 2013 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) 2014 2015 static void 2016 iwn_read_eeprom_enhinfo(struct iwn_softc *sc) 2017 { 2018 struct iwn_eeprom_enhinfo enhinfo[35]; 2019 struct ifnet *ifp = sc->sc_ifp; 2020 struct ieee80211com *ic = ifp->if_l2com; 2021 struct ieee80211_channel *c; 2022 uint16_t val, base; 2023 int8_t maxpwr; 2024 uint8_t flags; 2025 int i, j; 2026 2027 iwn_read_prom_data(sc, IWN5000_EEPROM_REG, &val, 2); 2028 base = le16toh(val); 2029 iwn_read_prom_data(sc, base + IWN6000_EEPROM_ENHINFO, 2030 enhinfo, sizeof enhinfo); 2031 2032 for (i = 0; i < nitems(enhinfo); i++) { 2033 flags = enhinfo[i].flags; 2034 if (!(flags & IWN_ENHINFO_VALID)) 2035 continue; /* Skip invalid entries. */ 2036 2037 maxpwr = 0; 2038 if (sc->txchainmask & IWN_ANT_A) 2039 maxpwr = MAX(maxpwr, enhinfo[i].chain[0]); 2040 if (sc->txchainmask & IWN_ANT_B) 2041 maxpwr = MAX(maxpwr, enhinfo[i].chain[1]); 2042 if (sc->txchainmask & IWN_ANT_C) 2043 maxpwr = MAX(maxpwr, enhinfo[i].chain[2]); 2044 if (sc->ntxchains == 2) 2045 maxpwr = MAX(maxpwr, enhinfo[i].mimo2); 2046 else if (sc->ntxchains == 3) 2047 maxpwr = MAX(maxpwr, enhinfo[i].mimo3); 2048 2049 for (j = 0; j < ic->ic_nchans; j++) { 2050 c = &ic->ic_channels[j]; 2051 if ((flags & IWN_ENHINFO_5GHZ)) { 2052 if (!IEEE80211_IS_CHAN_A(c)) 2053 continue; 2054 } else if ((flags & IWN_ENHINFO_OFDM)) { 2055 if (!IEEE80211_IS_CHAN_G(c)) 2056 continue; 2057 } else if (!IEEE80211_IS_CHAN_B(c)) 2058 continue; 2059 if ((flags & IWN_ENHINFO_HT40)) { 2060 if (!IEEE80211_IS_CHAN_HT40(c)) 2061 continue; 2062 } else { 2063 if (IEEE80211_IS_CHAN_HT40(c)) 2064 continue; 2065 } 2066 if (enhinfo[i].chan != 0 && 2067 enhinfo[i].chan != c->ic_ieee) 2068 continue; 2069 2070 DPRINTF(sc, IWN_DEBUG_RESET, 2071 "channel %d(%x), maxpwr %d\n", c->ic_ieee, 2072 c->ic_flags, maxpwr / 2); 2073 c->ic_maxregpower = maxpwr / 2; 2074 c->ic_maxpower = maxpwr; 2075 } 2076 } 2077 } 2078 2079 static struct ieee80211_node * 2080 iwn_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN]) 2081 { 2082 return malloc(sizeof (struct iwn_node), M_80211_NODE,M_NOWAIT | M_ZERO); 2083 } 2084 2085 static __inline int 2086 rate2plcp(int rate) 2087 { 2088 switch (rate & 0xff) { 2089 case 12: return 0xd; 2090 case 18: return 0xf; 2091 case 24: return 0x5; 2092 case 36: return 0x7; 2093 case 48: return 0x9; 2094 case 72: return 0xb; 2095 case 96: return 0x1; 2096 case 108: return 0x3; 2097 case 2: return 10; 2098 case 4: return 20; 2099 case 11: return 55; 2100 case 22: return 110; 2101 } 2102 return 0; 2103 } 2104 2105 static void 2106 iwn_newassoc(struct ieee80211_node *ni, int isnew) 2107 { 2108 struct ieee80211com *ic = ni->ni_ic; 2109 struct iwn_softc *sc = ic->ic_ifp->if_softc; 2110 struct iwn_node *wn = (void *)ni; 2111 uint8_t txant1, txant2; 2112 int i, plcp, rate, ridx; 2113 2114 /* Use the first valid TX antenna. */ 2115 txant1 = IWN_LSB(sc->txchainmask); 2116 txant2 = IWN_LSB(sc->txchainmask & ~txant1); 2117 2118 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 2119 ridx = ni->ni_rates.rs_nrates - 1; 2120 for (i = ni->ni_htrates.rs_nrates - 1; i >= 0; i--) { 2121 plcp = ni->ni_htrates.rs_rates[i] | IWN_RFLAG_MCS; 2122 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) { 2123 plcp |= IWN_RFLAG_HT40; 2124 if (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40) 2125 plcp |= IWN_RFLAG_SGI; 2126 } else if (ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20) 2127 plcp |= IWN_RFLAG_SGI; 2128 if (i > 7) 2129 plcp |= IWN_RFLAG_ANT(txant1 | txant2); 2130 else 2131 plcp |= IWN_RFLAG_ANT(txant1); 2132 if (ridx >= 0) { 2133 rate = ni->ni_rates.rs_rates[ridx]; 2134 rate &= IEEE80211_RATE_VAL; 2135 wn->ridx[rate] = plcp; 2136 } 2137 wn->ridx[IEEE80211_RATE_MCS | i] = plcp; 2138 ridx--; 2139 } 2140 } else { 2141 for (i = 0; i < ni->ni_rates.rs_nrates; i++) { 2142 rate = ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL; 2143 2144 plcp = rate2plcp(rate); 2145 ridx = ic->ic_rt->rateCodeToIndex[rate]; 2146 if (ridx < IWN_RIDX_OFDM6 && 2147 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 2148 plcp |= IWN_RFLAG_CCK; 2149 plcp |= IWN_RFLAG_ANT(txant1); 2150 wn->ridx[rate] = htole32(plcp); 2151 } 2152 } 2153 } 2154 2155 static int 2156 iwn_media_change(struct ifnet *ifp) 2157 { 2158 int error; 2159 2160 error = ieee80211_media_change(ifp); 2161 /* NB: only the fixed rate can change and that doesn't need a reset */ 2162 return (error == ENETRESET ? 0 : error); 2163 } 2164 2165 static int 2166 iwn_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 2167 { 2168 struct iwn_vap *ivp = IWN_VAP(vap); 2169 struct ieee80211com *ic = vap->iv_ic; 2170 struct iwn_softc *sc = ic->ic_ifp->if_softc; 2171 int error = 0; 2172 2173 DPRINTF(sc, IWN_DEBUG_STATE, "%s: %s -> %s\n", __func__, 2174 ieee80211_state_name[vap->iv_state], ieee80211_state_name[nstate]); 2175 2176 IEEE80211_UNLOCK(ic); 2177 IWN_LOCK(sc); 2178 callout_stop(&sc->calib_to); 2179 2180 switch (nstate) { 2181 case IEEE80211_S_ASSOC: 2182 if (vap->iv_state != IEEE80211_S_RUN) 2183 break; 2184 /* FALLTHROUGH */ 2185 case IEEE80211_S_AUTH: 2186 if (vap->iv_state == IEEE80211_S_AUTH) 2187 break; 2188 2189 /* 2190 * !AUTH -> AUTH transition requires state reset to handle 2191 * reassociations correctly. 2192 */ 2193 sc->rxon.associd = 0; 2194 sc->rxon.filter &= ~htole32(IWN_FILTER_BSS); 2195 sc->calib.state = IWN_CALIB_STATE_INIT; 2196 2197 if ((error = iwn_auth(sc, vap)) != 0) { 2198 device_printf(sc->sc_dev, 2199 "%s: could not move to auth state\n", __func__); 2200 } 2201 break; 2202 2203 case IEEE80211_S_RUN: 2204 /* 2205 * RUN -> RUN transition; Just restart the timers. 2206 */ 2207 if (vap->iv_state == IEEE80211_S_RUN) { 2208 sc->calib_cnt = 0; 2209 break; 2210 } 2211 2212 /* 2213 * !RUN -> RUN requires setting the association id 2214 * which is done with a firmware cmd. We also defer 2215 * starting the timers until that work is done. 2216 */ 2217 if ((error = iwn_run(sc, vap)) != 0) { 2218 device_printf(sc->sc_dev, 2219 "%s: could not move to run state\n", __func__); 2220 } 2221 break; 2222 2223 case IEEE80211_S_INIT: 2224 sc->calib.state = IWN_CALIB_STATE_INIT; 2225 break; 2226 2227 default: 2228 break; 2229 } 2230 IWN_UNLOCK(sc); 2231 IEEE80211_LOCK(ic); 2232 if (error != 0) 2233 return error; 2234 return ivp->iv_newstate(vap, nstate, arg); 2235 } 2236 2237 static void 2238 iwn_calib_timeout(void *arg) 2239 { 2240 struct iwn_softc *sc = arg; 2241 2242 IWN_LOCK_ASSERT(sc); 2243 2244 /* Force automatic TX power calibration every 60 secs. */ 2245 if (++sc->calib_cnt >= 120) { 2246 uint32_t flags = 0; 2247 2248 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s\n", 2249 "sending request for statistics"); 2250 (void)iwn_cmd(sc, IWN_CMD_GET_STATISTICS, &flags, 2251 sizeof flags, 1); 2252 sc->calib_cnt = 0; 2253 } 2254 callout_reset(&sc->calib_to, msecs_to_ticks(500), iwn_calib_timeout, 2255 sc); 2256 } 2257 2258 /* 2259 * Process an RX_PHY firmware notification. This is usually immediately 2260 * followed by an MPDU_RX_DONE notification. 2261 */ 2262 static void 2263 iwn_rx_phy(struct iwn_softc *sc, struct iwn_rx_desc *desc, 2264 struct iwn_rx_data *data) 2265 { 2266 struct iwn_rx_stat *stat = (struct iwn_rx_stat *)(desc + 1); 2267 2268 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: received PHY stats\n", __func__); 2269 bus_dmamap_sync(sc->rxq.data_dmat, data->map, BUS_DMASYNC_POSTREAD); 2270 2271 /* Save RX statistics, they will be used on MPDU_RX_DONE. */ 2272 memcpy(&sc->last_rx_stat, stat, sizeof (*stat)); 2273 sc->last_rx_valid = 1; 2274 } 2275 2276 /* 2277 * Process an RX_DONE (4965AGN only) or MPDU_RX_DONE firmware notification. 2278 * Each MPDU_RX_DONE notification must be preceded by an RX_PHY one. 2279 */ 2280 static void 2281 iwn_rx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc, 2282 struct iwn_rx_data *data) 2283 { 2284 struct iwn_ops *ops = &sc->ops; 2285 struct ifnet *ifp = sc->sc_ifp; 2286 struct ieee80211com *ic = ifp->if_l2com; 2287 struct iwn_rx_ring *ring = &sc->rxq; 2288 struct ieee80211_frame *wh; 2289 struct ieee80211_node *ni; 2290 struct mbuf *m, *m1; 2291 struct iwn_rx_stat *stat; 2292 caddr_t head; 2293 bus_addr_t paddr; 2294 uint32_t flags; 2295 int error, len, rssi, nf; 2296 2297 if (desc->type == IWN_MPDU_RX_DONE) { 2298 /* Check for prior RX_PHY notification. */ 2299 if (!sc->last_rx_valid) { 2300 DPRINTF(sc, IWN_DEBUG_ANY, 2301 "%s: missing RX_PHY\n", __func__); 2302 return; 2303 } 2304 stat = &sc->last_rx_stat; 2305 } else 2306 stat = (struct iwn_rx_stat *)(desc + 1); 2307 2308 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD); 2309 2310 if (stat->cfg_phy_len > IWN_STAT_MAXLEN) { 2311 device_printf(sc->sc_dev, 2312 "%s: invalid RX statistic header, len %d\n", __func__, 2313 stat->cfg_phy_len); 2314 return; 2315 } 2316 if (desc->type == IWN_MPDU_RX_DONE) { 2317 struct iwn_rx_mpdu *mpdu = (struct iwn_rx_mpdu *)(desc + 1); 2318 head = (caddr_t)(mpdu + 1); 2319 len = le16toh(mpdu->len); 2320 } else { 2321 head = (caddr_t)(stat + 1) + stat->cfg_phy_len; 2322 len = le16toh(stat->len); 2323 } 2324 2325 flags = le32toh(*(uint32_t *)(head + len)); 2326 2327 /* Discard frames with a bad FCS early. */ 2328 if ((flags & IWN_RX_NOERROR) != IWN_RX_NOERROR) { 2329 DPRINTF(sc, IWN_DEBUG_RECV, "%s: RX flags error %x\n", 2330 __func__, flags); 2331 ifp->if_ierrors++; 2332 return; 2333 } 2334 /* Discard frames that are too short. */ 2335 if (len < sizeof (*wh)) { 2336 DPRINTF(sc, IWN_DEBUG_RECV, "%s: frame too short: %d\n", 2337 __func__, len); 2338 ifp->if_ierrors++; 2339 return; 2340 } 2341 2342 m1 = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, IWN_RBUF_SIZE); 2343 if (m1 == NULL) { 2344 DPRINTF(sc, IWN_DEBUG_ANY, "%s: no mbuf to restock ring\n", 2345 __func__); 2346 ifp->if_ierrors++; 2347 return; 2348 } 2349 bus_dmamap_unload(ring->data_dmat, data->map); 2350 2351 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m1, void *), 2352 IWN_RBUF_SIZE, iwn_dma_map_addr, &paddr, BUS_DMA_NOWAIT); 2353 if (error != 0 && error != EFBIG) { 2354 device_printf(sc->sc_dev, 2355 "%s: bus_dmamap_load failed, error %d\n", __func__, error); 2356 m_freem(m1); 2357 2358 /* Try to reload the old mbuf. */ 2359 error = bus_dmamap_load(ring->data_dmat, data->map, 2360 mtod(data->m, void *), IWN_RBUF_SIZE, iwn_dma_map_addr, 2361 &paddr, BUS_DMA_NOWAIT); 2362 if (error != 0 && error != EFBIG) { 2363 panic("%s: could not load old RX mbuf", __func__); 2364 } 2365 /* Physical address may have changed. */ 2366 ring->desc[ring->cur] = htole32(paddr >> 8); 2367 bus_dmamap_sync(ring->data_dmat, ring->desc_dma.map, 2368 BUS_DMASYNC_PREWRITE); 2369 ifp->if_ierrors++; 2370 return; 2371 } 2372 2373 m = data->m; 2374 data->m = m1; 2375 /* Update RX descriptor. */ 2376 ring->desc[ring->cur] = htole32(paddr >> 8); 2377 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 2378 BUS_DMASYNC_PREWRITE); 2379 2380 /* Finalize mbuf. */ 2381 m->m_pkthdr.rcvif = ifp; 2382 m->m_data = head; 2383 m->m_pkthdr.len = m->m_len = len; 2384 2385 /* Grab a reference to the source node. */ 2386 wh = mtod(m, struct ieee80211_frame *); 2387 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 2388 nf = (ni != NULL && ni->ni_vap->iv_state == IEEE80211_S_RUN && 2389 (ic->ic_flags & IEEE80211_F_SCAN) == 0) ? sc->noise : -95; 2390 2391 rssi = ops->get_rssi(sc, stat); 2392 2393 if (ieee80211_radiotap_active(ic)) { 2394 struct iwn_rx_radiotap_header *tap = &sc->sc_rxtap; 2395 2396 tap->wr_flags = 0; 2397 if (stat->flags & htole16(IWN_STAT_FLAG_SHPREAMBLE)) 2398 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 2399 tap->wr_dbm_antsignal = (int8_t)rssi; 2400 tap->wr_dbm_antnoise = (int8_t)nf; 2401 tap->wr_tsft = stat->tstamp; 2402 switch (stat->rate) { 2403 /* CCK rates. */ 2404 case 10: tap->wr_rate = 2; break; 2405 case 20: tap->wr_rate = 4; break; 2406 case 55: tap->wr_rate = 11; break; 2407 case 110: tap->wr_rate = 22; break; 2408 /* OFDM rates. */ 2409 case 0xd: tap->wr_rate = 12; break; 2410 case 0xf: tap->wr_rate = 18; break; 2411 case 0x5: tap->wr_rate = 24; break; 2412 case 0x7: tap->wr_rate = 36; break; 2413 case 0x9: tap->wr_rate = 48; break; 2414 case 0xb: tap->wr_rate = 72; break; 2415 case 0x1: tap->wr_rate = 96; break; 2416 case 0x3: tap->wr_rate = 108; break; 2417 /* Unknown rate: should not happen. */ 2418 default: tap->wr_rate = 0; 2419 } 2420 } 2421 2422 IWN_UNLOCK(sc); 2423 2424 /* Send the frame to the 802.11 layer. */ 2425 if (ni != NULL) { 2426 if (ni->ni_flags & IEEE80211_NODE_HT) 2427 m->m_flags |= M_AMPDU; 2428 (void)ieee80211_input(ni, m, rssi - nf, nf); 2429 /* Node is no longer needed. */ 2430 ieee80211_free_node(ni); 2431 } else 2432 (void)ieee80211_input_all(ic, m, rssi - nf, nf); 2433 2434 IWN_LOCK(sc); 2435 } 2436 2437 /* Process an incoming Compressed BlockAck. */ 2438 static void 2439 iwn_rx_compressed_ba(struct iwn_softc *sc, struct iwn_rx_desc *desc, 2440 struct iwn_rx_data *data) 2441 { 2442 struct ifnet *ifp = sc->sc_ifp; 2443 struct iwn_node *wn; 2444 struct ieee80211_node *ni; 2445 struct iwn_compressed_ba *ba = (struct iwn_compressed_ba *)(desc + 1); 2446 struct iwn_tx_ring *txq; 2447 struct ieee80211_tx_ampdu *tap; 2448 uint64_t bitmap; 2449 uint8_t tid; 2450 int ackfailcnt = 0, i, shift; 2451 2452 bus_dmamap_sync(sc->rxq.data_dmat, data->map, BUS_DMASYNC_POSTREAD); 2453 2454 txq = &sc->txq[le16toh(ba->qid)]; 2455 tap = sc->qid2tap[le16toh(ba->qid)]; 2456 tid = WME_AC_TO_TID(tap->txa_ac); 2457 ni = tap->txa_ni; 2458 wn = (void *)ni; 2459 2460 if (wn->agg[tid].bitmap == 0) 2461 return; 2462 2463 shift = wn->agg[tid].startidx - ((le16toh(ba->seq) >> 4) & 0xff); 2464 if (shift < 0) 2465 shift += 0x100; 2466 2467 if (wn->agg[tid].nframes > (64 - shift)) 2468 return; 2469 2470 bitmap = (le64toh(ba->bitmap) >> shift) & wn->agg[tid].bitmap; 2471 for (i = 0; bitmap; i++) { 2472 if ((bitmap & 1) == 0) { 2473 ifp->if_oerrors++; 2474 ieee80211_ratectl_tx_complete(ni->ni_vap, ni, 2475 IEEE80211_RATECTL_TX_FAILURE, &ackfailcnt, NULL); 2476 } else { 2477 ifp->if_opackets++; 2478 ieee80211_ratectl_tx_complete(ni->ni_vap, ni, 2479 IEEE80211_RATECTL_TX_SUCCESS, &ackfailcnt, NULL); 2480 } 2481 bitmap >>= 1; 2482 } 2483 } 2484 2485 /* 2486 * Process a CALIBRATION_RESULT notification sent by the initialization 2487 * firmware on response to a CMD_CALIB_CONFIG command (5000 only). 2488 */ 2489 static void 2490 iwn5000_rx_calib_results(struct iwn_softc *sc, struct iwn_rx_desc *desc, 2491 struct iwn_rx_data *data) 2492 { 2493 struct iwn_phy_calib *calib = (struct iwn_phy_calib *)(desc + 1); 2494 int len, idx = -1; 2495 2496 /* Runtime firmware should not send such a notification. */ 2497 if (sc->sc_flags & IWN_FLAG_CALIB_DONE) 2498 return; 2499 2500 len = (le32toh(desc->len) & 0x3fff) - 4; 2501 bus_dmamap_sync(sc->rxq.data_dmat, data->map, BUS_DMASYNC_POSTREAD); 2502 2503 switch (calib->code) { 2504 case IWN5000_PHY_CALIB_DC: 2505 if ((sc->sc_flags & IWN_FLAG_INTERNAL_PA) == 0 && 2506 (sc->hw_type == IWN_HW_REV_TYPE_5150 || 2507 sc->hw_type >= IWN_HW_REV_TYPE_6000)) 2508 idx = 0; 2509 break; 2510 case IWN5000_PHY_CALIB_LO: 2511 idx = 1; 2512 break; 2513 case IWN5000_PHY_CALIB_TX_IQ: 2514 idx = 2; 2515 break; 2516 case IWN5000_PHY_CALIB_TX_IQ_PERIODIC: 2517 if (sc->hw_type < IWN_HW_REV_TYPE_6000 && 2518 sc->hw_type != IWN_HW_REV_TYPE_5150) 2519 idx = 3; 2520 break; 2521 case IWN5000_PHY_CALIB_BASE_BAND: 2522 idx = 4; 2523 break; 2524 } 2525 if (idx == -1) /* Ignore other results. */ 2526 return; 2527 2528 /* Save calibration result. */ 2529 if (sc->calibcmd[idx].buf != NULL) 2530 free(sc->calibcmd[idx].buf, M_DEVBUF); 2531 sc->calibcmd[idx].buf = malloc(len, M_DEVBUF, M_NOWAIT); 2532 if (sc->calibcmd[idx].buf == NULL) { 2533 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 2534 "not enough memory for calibration result %d\n", 2535 calib->code); 2536 return; 2537 } 2538 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 2539 "saving calibration result code=%d len=%d\n", calib->code, len); 2540 sc->calibcmd[idx].len = len; 2541 memcpy(sc->calibcmd[idx].buf, calib, len); 2542 } 2543 2544 /* 2545 * Process an RX_STATISTICS or BEACON_STATISTICS firmware notification. 2546 * The latter is sent by the firmware after each received beacon. 2547 */ 2548 static void 2549 iwn_rx_statistics(struct iwn_softc *sc, struct iwn_rx_desc *desc, 2550 struct iwn_rx_data *data) 2551 { 2552 struct iwn_ops *ops = &sc->ops; 2553 struct ifnet *ifp = sc->sc_ifp; 2554 struct ieee80211com *ic = ifp->if_l2com; 2555 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2556 struct iwn_calib_state *calib = &sc->calib; 2557 struct iwn_stats *stats = (struct iwn_stats *)(desc + 1); 2558 int temp; 2559 2560 /* Ignore statistics received during a scan. */ 2561 if (vap->iv_state != IEEE80211_S_RUN || 2562 (ic->ic_flags & IEEE80211_F_SCAN)) 2563 return; 2564 2565 bus_dmamap_sync(sc->rxq.data_dmat, data->map, BUS_DMASYNC_POSTREAD); 2566 2567 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: received statistics, cmd %d\n", 2568 __func__, desc->type); 2569 sc->calib_cnt = 0; /* Reset TX power calibration timeout. */ 2570 2571 /* Test if temperature has changed. */ 2572 if (stats->general.temp != sc->rawtemp) { 2573 /* Convert "raw" temperature to degC. */ 2574 sc->rawtemp = stats->general.temp; 2575 temp = ops->get_temperature(sc); 2576 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: temperature %d\n", 2577 __func__, temp); 2578 2579 /* Update TX power if need be (4965AGN only). */ 2580 if (sc->hw_type == IWN_HW_REV_TYPE_4965) 2581 iwn4965_power_calibration(sc, temp); 2582 } 2583 2584 if (desc->type != IWN_BEACON_STATISTICS) 2585 return; /* Reply to a statistics request. */ 2586 2587 sc->noise = iwn_get_noise(&stats->rx.general); 2588 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: noise %d\n", __func__, sc->noise); 2589 2590 /* Test that RSSI and noise are present in stats report. */ 2591 if (le32toh(stats->rx.general.flags) != 1) { 2592 DPRINTF(sc, IWN_DEBUG_ANY, "%s\n", 2593 "received statistics without RSSI"); 2594 return; 2595 } 2596 2597 if (calib->state == IWN_CALIB_STATE_ASSOC) 2598 iwn_collect_noise(sc, &stats->rx.general); 2599 else if (calib->state == IWN_CALIB_STATE_RUN) 2600 iwn_tune_sensitivity(sc, &stats->rx); 2601 } 2602 2603 /* 2604 * Process a TX_DONE firmware notification. Unfortunately, the 4965AGN 2605 * and 5000 adapters have different incompatible TX status formats. 2606 */ 2607 static void 2608 iwn4965_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc, 2609 struct iwn_rx_data *data) 2610 { 2611 struct iwn4965_tx_stat *stat = (struct iwn4965_tx_stat *)(desc + 1); 2612 struct iwn_tx_ring *ring; 2613 int qid; 2614 2615 qid = desc->qid & 0xf; 2616 ring = &sc->txq[qid]; 2617 2618 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: " 2619 "qid %d idx %d retries %d nkill %d rate %x duration %d status %x\n", 2620 __func__, desc->qid, desc->idx, stat->ackfailcnt, 2621 stat->btkillcnt, stat->rate, le16toh(stat->duration), 2622 le32toh(stat->status)); 2623 2624 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD); 2625 if (qid >= sc->firstaggqueue) { 2626 iwn_ampdu_tx_done(sc, qid, desc->idx, stat->nframes, 2627 &stat->status); 2628 } else { 2629 iwn_tx_done(sc, desc, stat->ackfailcnt, 2630 le32toh(stat->status) & 0xff); 2631 } 2632 } 2633 2634 static void 2635 iwn5000_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc, 2636 struct iwn_rx_data *data) 2637 { 2638 struct iwn5000_tx_stat *stat = (struct iwn5000_tx_stat *)(desc + 1); 2639 struct iwn_tx_ring *ring; 2640 int qid; 2641 2642 qid = desc->qid & 0xf; 2643 ring = &sc->txq[qid]; 2644 2645 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: " 2646 "qid %d idx %d retries %d nkill %d rate %x duration %d status %x\n", 2647 __func__, desc->qid, desc->idx, stat->ackfailcnt, 2648 stat->btkillcnt, stat->rate, le16toh(stat->duration), 2649 le32toh(stat->status)); 2650 2651 #ifdef notyet 2652 /* Reset TX scheduler slot. */ 2653 iwn5000_reset_sched(sc, desc->qid & 0xf, desc->idx); 2654 #endif 2655 2656 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD); 2657 if (qid >= sc->firstaggqueue) { 2658 iwn_ampdu_tx_done(sc, qid, desc->idx, stat->nframes, 2659 &stat->status); 2660 } else { 2661 iwn_tx_done(sc, desc, stat->ackfailcnt, 2662 le16toh(stat->status) & 0xff); 2663 } 2664 } 2665 2666 /* 2667 * Adapter-independent backend for TX_DONE firmware notifications. 2668 */ 2669 static void 2670 iwn_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc, int ackfailcnt, 2671 uint8_t status) 2672 { 2673 struct ifnet *ifp = sc->sc_ifp; 2674 struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf]; 2675 struct iwn_tx_data *data = &ring->data[desc->idx]; 2676 struct mbuf *m; 2677 struct ieee80211_node *ni; 2678 struct ieee80211vap *vap; 2679 2680 KASSERT(data->ni != NULL, ("no node")); 2681 2682 /* Unmap and free mbuf. */ 2683 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE); 2684 bus_dmamap_unload(ring->data_dmat, data->map); 2685 m = data->m, data->m = NULL; 2686 ni = data->ni, data->ni = NULL; 2687 vap = ni->ni_vap; 2688 2689 if (m->m_flags & M_TXCB) { 2690 /* 2691 * Channels marked for "radar" require traffic to be received 2692 * to unlock before we can transmit. Until traffic is seen 2693 * any attempt to transmit is returned immediately with status 2694 * set to IWN_TX_FAIL_TX_LOCKED. Unfortunately this can easily 2695 * happen on first authenticate after scanning. To workaround 2696 * this we ignore a failure of this sort in AUTH state so the 2697 * 802.11 layer will fall back to using a timeout to wait for 2698 * the AUTH reply. This allows the firmware time to see 2699 * traffic so a subsequent retry of AUTH succeeds. It's 2700 * unclear why the firmware does not maintain state for 2701 * channels recently visited as this would allow immediate 2702 * use of the channel after a scan (where we see traffic). 2703 */ 2704 if (status == IWN_TX_FAIL_TX_LOCKED && 2705 ni->ni_vap->iv_state == IEEE80211_S_AUTH) 2706 ieee80211_process_callback(ni, m, 0); 2707 else 2708 ieee80211_process_callback(ni, m, 2709 (status & IWN_TX_FAIL) != 0); 2710 } 2711 2712 /* 2713 * Update rate control statistics for the node. 2714 */ 2715 if (status & IWN_TX_FAIL) { 2716 ifp->if_oerrors++; 2717 ieee80211_ratectl_tx_complete(vap, ni, 2718 IEEE80211_RATECTL_TX_FAILURE, &ackfailcnt, NULL); 2719 } else { 2720 ifp->if_opackets++; 2721 ieee80211_ratectl_tx_complete(vap, ni, 2722 IEEE80211_RATECTL_TX_SUCCESS, &ackfailcnt, NULL); 2723 } 2724 m_freem(m); 2725 ieee80211_free_node(ni); 2726 2727 sc->sc_tx_timer = 0; 2728 if (--ring->queued < IWN_TX_RING_LOMARK) { 2729 sc->qfullmsk &= ~(1 << ring->qid); 2730 if (sc->qfullmsk == 0 && 2731 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) { 2732 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2733 iwn_start_locked(ifp); 2734 } 2735 } 2736 } 2737 2738 /* 2739 * Process a "command done" firmware notification. This is where we wakeup 2740 * processes waiting for a synchronous command completion. 2741 */ 2742 static void 2743 iwn_cmd_done(struct iwn_softc *sc, struct iwn_rx_desc *desc) 2744 { 2745 struct iwn_tx_ring *ring = &sc->txq[4]; 2746 struct iwn_tx_data *data; 2747 2748 if ((desc->qid & 0xf) != 4) 2749 return; /* Not a command ack. */ 2750 2751 data = &ring->data[desc->idx]; 2752 2753 /* If the command was mapped in an mbuf, free it. */ 2754 if (data->m != NULL) { 2755 bus_dmamap_sync(ring->data_dmat, data->map, 2756 BUS_DMASYNC_POSTWRITE); 2757 bus_dmamap_unload(ring->data_dmat, data->map); 2758 m_freem(data->m); 2759 data->m = NULL; 2760 } 2761 wakeup(&ring->desc[desc->idx]); 2762 } 2763 2764 static void 2765 iwn_ampdu_tx_done(struct iwn_softc *sc, int qid, int idx, int nframes, 2766 void *stat) 2767 { 2768 struct ifnet *ifp = sc->sc_ifp; 2769 struct iwn_tx_ring *ring = &sc->txq[qid]; 2770 struct iwn_tx_data *data; 2771 struct mbuf *m; 2772 struct iwn_node *wn; 2773 struct ieee80211_node *ni; 2774 struct ieee80211vap *vap; 2775 struct ieee80211_tx_ampdu *tap; 2776 uint64_t bitmap; 2777 uint32_t *status = stat; 2778 uint16_t *aggstatus = stat; 2779 uint8_t tid; 2780 int bit, i, lastidx, seqno, shift, start; 2781 2782 #ifdef NOT_YET 2783 if (nframes == 1) { 2784 if ((*status & 0xff) != 1 && (*status & 0xff) != 2) 2785 printf("ieee80211_send_bar()\n"); 2786 } 2787 #endif 2788 2789 bitmap = 0; 2790 start = idx; 2791 for (i = 0; i < nframes; i++) { 2792 if (le16toh(aggstatus[i * 2]) & 0xc) 2793 continue; 2794 2795 idx = le16toh(aggstatus[2*i + 1]) & 0xff; 2796 bit = idx - start; 2797 shift = 0; 2798 if (bit >= 64) { 2799 shift = 0x100 - idx + start; 2800 bit = 0; 2801 start = idx; 2802 } else if (bit <= -64) 2803 bit = 0x100 - start + idx; 2804 else if (bit < 0) { 2805 shift = start - idx; 2806 start = idx; 2807 bit = 0; 2808 } 2809 bitmap = bitmap << shift; 2810 bitmap |= 1ULL << bit; 2811 } 2812 tap = sc->qid2tap[qid]; 2813 tid = WME_AC_TO_TID(tap->txa_ac); 2814 wn = (void *)tap->txa_ni; 2815 wn->agg[tid].bitmap = bitmap; 2816 wn->agg[tid].startidx = start; 2817 wn->agg[tid].nframes = nframes; 2818 2819 seqno = le32toh(*(status + nframes)) & 0xfff; 2820 for (lastidx = (seqno & 0xff); ring->read != lastidx;) { 2821 data = &ring->data[ring->read]; 2822 2823 KASSERT(data->ni != NULL, ("no node")); 2824 2825 /* Unmap and free mbuf. */ 2826 bus_dmamap_sync(ring->data_dmat, data->map, 2827 BUS_DMASYNC_POSTWRITE); 2828 bus_dmamap_unload(ring->data_dmat, data->map); 2829 m = data->m, data->m = NULL; 2830 ni = data->ni, data->ni = NULL; 2831 vap = ni->ni_vap; 2832 2833 if (m->m_flags & M_TXCB) 2834 ieee80211_process_callback(ni, m, 1); 2835 2836 m_freem(m); 2837 ieee80211_free_node(ni); 2838 2839 ring->queued--; 2840 ring->read = (ring->read + 1) % IWN_TX_RING_COUNT; 2841 } 2842 2843 sc->sc_tx_timer = 0; 2844 if (ring->queued < IWN_TX_RING_LOMARK) { 2845 sc->qfullmsk &= ~(1 << ring->qid); 2846 if (sc->qfullmsk == 0 && 2847 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) { 2848 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2849 iwn_start_locked(ifp); 2850 } 2851 } 2852 } 2853 2854 /* 2855 * Process an INT_FH_RX or INT_SW_RX interrupt. 2856 */ 2857 static void 2858 iwn_notif_intr(struct iwn_softc *sc) 2859 { 2860 struct iwn_ops *ops = &sc->ops; 2861 struct ifnet *ifp = sc->sc_ifp; 2862 struct ieee80211com *ic = ifp->if_l2com; 2863 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2864 uint16_t hw; 2865 2866 bus_dmamap_sync(sc->rxq.stat_dma.tag, sc->rxq.stat_dma.map, 2867 BUS_DMASYNC_POSTREAD); 2868 2869 hw = le16toh(sc->rxq.stat->closed_count) & 0xfff; 2870 while (sc->rxq.cur != hw) { 2871 struct iwn_rx_data *data = &sc->rxq.data[sc->rxq.cur]; 2872 struct iwn_rx_desc *desc; 2873 2874 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2875 BUS_DMASYNC_POSTREAD); 2876 desc = mtod(data->m, struct iwn_rx_desc *); 2877 2878 DPRINTF(sc, IWN_DEBUG_RECV, 2879 "%s: qid %x idx %d flags %x type %d(%s) len %d\n", 2880 __func__, desc->qid & 0xf, desc->idx, desc->flags, 2881 desc->type, iwn_intr_str(desc->type), 2882 le16toh(desc->len)); 2883 2884 if (!(desc->qid & 0x80)) /* Reply to a command. */ 2885 iwn_cmd_done(sc, desc); 2886 2887 switch (desc->type) { 2888 case IWN_RX_PHY: 2889 iwn_rx_phy(sc, desc, data); 2890 break; 2891 2892 case IWN_RX_DONE: /* 4965AGN only. */ 2893 case IWN_MPDU_RX_DONE: 2894 /* An 802.11 frame has been received. */ 2895 iwn_rx_done(sc, desc, data); 2896 break; 2897 2898 case IWN_RX_COMPRESSED_BA: 2899 /* A Compressed BlockAck has been received. */ 2900 iwn_rx_compressed_ba(sc, desc, data); 2901 break; 2902 2903 case IWN_TX_DONE: 2904 /* An 802.11 frame has been transmitted. */ 2905 ops->tx_done(sc, desc, data); 2906 break; 2907 2908 case IWN_RX_STATISTICS: 2909 case IWN_BEACON_STATISTICS: 2910 iwn_rx_statistics(sc, desc, data); 2911 break; 2912 2913 case IWN_BEACON_MISSED: 2914 { 2915 struct iwn_beacon_missed *miss = 2916 (struct iwn_beacon_missed *)(desc + 1); 2917 int misses; 2918 2919 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2920 BUS_DMASYNC_POSTREAD); 2921 misses = le32toh(miss->consecutive); 2922 2923 DPRINTF(sc, IWN_DEBUG_STATE, 2924 "%s: beacons missed %d/%d\n", __func__, 2925 misses, le32toh(miss->total)); 2926 /* 2927 * If more than 5 consecutive beacons are missed, 2928 * reinitialize the sensitivity state machine. 2929 */ 2930 if (vap->iv_state == IEEE80211_S_RUN && 2931 (ic->ic_flags & IEEE80211_F_SCAN) != 0) { 2932 if (misses > 5) 2933 (void)iwn_init_sensitivity(sc); 2934 if (misses >= vap->iv_bmissthreshold) { 2935 IWN_UNLOCK(sc); 2936 ieee80211_beacon_miss(ic); 2937 IWN_LOCK(sc); 2938 } 2939 } 2940 break; 2941 } 2942 case IWN_UC_READY: 2943 { 2944 struct iwn_ucode_info *uc = 2945 (struct iwn_ucode_info *)(desc + 1); 2946 2947 /* The microcontroller is ready. */ 2948 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2949 BUS_DMASYNC_POSTREAD); 2950 DPRINTF(sc, IWN_DEBUG_RESET, 2951 "microcode alive notification version=%d.%d " 2952 "subtype=%x alive=%x\n", uc->major, uc->minor, 2953 uc->subtype, le32toh(uc->valid)); 2954 2955 if (le32toh(uc->valid) != 1) { 2956 device_printf(sc->sc_dev, 2957 "microcontroller initialization failed"); 2958 break; 2959 } 2960 if (uc->subtype == IWN_UCODE_INIT) { 2961 /* Save microcontroller report. */ 2962 memcpy(&sc->ucode_info, uc, sizeof (*uc)); 2963 } 2964 /* Save the address of the error log in SRAM. */ 2965 sc->errptr = le32toh(uc->errptr); 2966 break; 2967 } 2968 case IWN_STATE_CHANGED: 2969 { 2970 uint32_t *status = (uint32_t *)(desc + 1); 2971 2972 /* 2973 * State change allows hardware switch change to be 2974 * noted. However, we handle this in iwn_intr as we 2975 * get both the enable/disble intr. 2976 */ 2977 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2978 BUS_DMASYNC_POSTREAD); 2979 DPRINTF(sc, IWN_DEBUG_INTR, "state changed to %x\n", 2980 le32toh(*status)); 2981 break; 2982 } 2983 case IWN_START_SCAN: 2984 { 2985 struct iwn_start_scan *scan = 2986 (struct iwn_start_scan *)(desc + 1); 2987 2988 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 2989 BUS_DMASYNC_POSTREAD); 2990 DPRINTF(sc, IWN_DEBUG_ANY, 2991 "%s: scanning channel %d status %x\n", 2992 __func__, scan->chan, le32toh(scan->status)); 2993 break; 2994 } 2995 case IWN_STOP_SCAN: 2996 { 2997 struct iwn_stop_scan *scan = 2998 (struct iwn_stop_scan *)(desc + 1); 2999 3000 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 3001 BUS_DMASYNC_POSTREAD); 3002 DPRINTF(sc, IWN_DEBUG_STATE, 3003 "scan finished nchan=%d status=%d chan=%d\n", 3004 scan->nchan, scan->status, scan->chan); 3005 3006 IWN_UNLOCK(sc); 3007 ieee80211_scan_next(vap); 3008 IWN_LOCK(sc); 3009 break; 3010 } 3011 case IWN5000_CALIBRATION_RESULT: 3012 iwn5000_rx_calib_results(sc, desc, data); 3013 break; 3014 3015 case IWN5000_CALIBRATION_DONE: 3016 sc->sc_flags |= IWN_FLAG_CALIB_DONE; 3017 wakeup(sc); 3018 break; 3019 } 3020 3021 sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT; 3022 } 3023 3024 /* Tell the firmware what we have processed. */ 3025 hw = (hw == 0) ? IWN_RX_RING_COUNT - 1 : hw - 1; 3026 IWN_WRITE(sc, IWN_FH_RX_WPTR, hw & ~7); 3027 } 3028 3029 /* 3030 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up 3031 * from power-down sleep mode. 3032 */ 3033 static void 3034 iwn_wakeup_intr(struct iwn_softc *sc) 3035 { 3036 int qid; 3037 3038 DPRINTF(sc, IWN_DEBUG_RESET, "%s: ucode wakeup from power-down sleep\n", 3039 __func__); 3040 3041 /* Wakeup RX and TX rings. */ 3042 IWN_WRITE(sc, IWN_FH_RX_WPTR, sc->rxq.cur & ~7); 3043 for (qid = 0; qid < sc->ntxqs; qid++) { 3044 struct iwn_tx_ring *ring = &sc->txq[qid]; 3045 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | ring->cur); 3046 } 3047 } 3048 3049 static void 3050 iwn_rftoggle_intr(struct iwn_softc *sc) 3051 { 3052 struct ifnet *ifp = sc->sc_ifp; 3053 struct ieee80211com *ic = ifp->if_l2com; 3054 uint32_t tmp = IWN_READ(sc, IWN_GP_CNTRL); 3055 3056 IWN_LOCK_ASSERT(sc); 3057 3058 device_printf(sc->sc_dev, "RF switch: radio %s\n", 3059 (tmp & IWN_GP_CNTRL_RFKILL) ? "enabled" : "disabled"); 3060 if (tmp & IWN_GP_CNTRL_RFKILL) 3061 ieee80211_runtask(ic, &sc->sc_radioon_task); 3062 else 3063 ieee80211_runtask(ic, &sc->sc_radiooff_task); 3064 } 3065 3066 /* 3067 * Dump the error log of the firmware when a firmware panic occurs. Although 3068 * we can't debug the firmware because it is neither open source nor free, it 3069 * can help us to identify certain classes of problems. 3070 */ 3071 static void 3072 iwn_fatal_intr(struct iwn_softc *sc) 3073 { 3074 struct iwn_fw_dump dump; 3075 int i; 3076 3077 IWN_LOCK_ASSERT(sc); 3078 3079 /* Force a complete recalibration on next init. */ 3080 sc->sc_flags &= ~IWN_FLAG_CALIB_DONE; 3081 3082 /* Check that the error log address is valid. */ 3083 if (sc->errptr < IWN_FW_DATA_BASE || 3084 sc->errptr + sizeof (dump) > 3085 IWN_FW_DATA_BASE + sc->fw_data_maxsz) { 3086 printf("%s: bad firmware error log address 0x%08x\n", __func__, 3087 sc->errptr); 3088 return; 3089 } 3090 if (iwn_nic_lock(sc) != 0) { 3091 printf("%s: could not read firmware error log\n", __func__); 3092 return; 3093 } 3094 /* Read firmware error log from SRAM. */ 3095 iwn_mem_read_region_4(sc, sc->errptr, (uint32_t *)&dump, 3096 sizeof (dump) / sizeof (uint32_t)); 3097 iwn_nic_unlock(sc); 3098 3099 if (dump.valid == 0) { 3100 printf("%s: firmware error log is empty\n", __func__); 3101 return; 3102 } 3103 printf("firmware error log:\n"); 3104 printf(" error type = \"%s\" (0x%08X)\n", 3105 (dump.id < nitems(iwn_fw_errmsg)) ? 3106 iwn_fw_errmsg[dump.id] : "UNKNOWN", 3107 dump.id); 3108 printf(" program counter = 0x%08X\n", dump.pc); 3109 printf(" source line = 0x%08X\n", dump.src_line); 3110 printf(" error data = 0x%08X%08X\n", 3111 dump.error_data[0], dump.error_data[1]); 3112 printf(" branch link = 0x%08X%08X\n", 3113 dump.branch_link[0], dump.branch_link[1]); 3114 printf(" interrupt link = 0x%08X%08X\n", 3115 dump.interrupt_link[0], dump.interrupt_link[1]); 3116 printf(" time = %u\n", dump.time[0]); 3117 3118 /* Dump driver status (TX and RX rings) while we're here. */ 3119 printf("driver status:\n"); 3120 for (i = 0; i < sc->ntxqs; i++) { 3121 struct iwn_tx_ring *ring = &sc->txq[i]; 3122 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n", 3123 i, ring->qid, ring->cur, ring->queued); 3124 } 3125 printf(" rx ring: cur=%d\n", sc->rxq.cur); 3126 } 3127 3128 static void 3129 iwn_intr(void *arg) 3130 { 3131 struct iwn_softc *sc = arg; 3132 struct ifnet *ifp = sc->sc_ifp; 3133 uint32_t r1, r2, tmp; 3134 3135 IWN_LOCK(sc); 3136 3137 /* Disable interrupts. */ 3138 IWN_WRITE(sc, IWN_INT_MASK, 0); 3139 3140 /* Read interrupts from ICT (fast) or from registers (slow). */ 3141 if (sc->sc_flags & IWN_FLAG_USE_ICT) { 3142 tmp = 0; 3143 while (sc->ict[sc->ict_cur] != 0) { 3144 tmp |= sc->ict[sc->ict_cur]; 3145 sc->ict[sc->ict_cur] = 0; /* Acknowledge. */ 3146 sc->ict_cur = (sc->ict_cur + 1) % IWN_ICT_COUNT; 3147 } 3148 tmp = le32toh(tmp); 3149 if (tmp == 0xffffffff) /* Shouldn't happen. */ 3150 tmp = 0; 3151 else if (tmp & 0xc0000) /* Workaround a HW bug. */ 3152 tmp |= 0x8000; 3153 r1 = (tmp & 0xff00) << 16 | (tmp & 0xff); 3154 r2 = 0; /* Unused. */ 3155 } else { 3156 r1 = IWN_READ(sc, IWN_INT); 3157 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0) 3158 return; /* Hardware gone! */ 3159 r2 = IWN_READ(sc, IWN_FH_INT); 3160 } 3161 3162 DPRINTF(sc, IWN_DEBUG_INTR, "interrupt reg1=%x reg2=%x\n", r1, r2); 3163 3164 if (r1 == 0 && r2 == 0) 3165 goto done; /* Interrupt not for us. */ 3166 3167 /* Acknowledge interrupts. */ 3168 IWN_WRITE(sc, IWN_INT, r1); 3169 if (!(sc->sc_flags & IWN_FLAG_USE_ICT)) 3170 IWN_WRITE(sc, IWN_FH_INT, r2); 3171 3172 if (r1 & IWN_INT_RF_TOGGLED) { 3173 iwn_rftoggle_intr(sc); 3174 goto done; 3175 } 3176 if (r1 & IWN_INT_CT_REACHED) { 3177 device_printf(sc->sc_dev, "%s: critical temperature reached!\n", 3178 __func__); 3179 } 3180 if (r1 & (IWN_INT_SW_ERR | IWN_INT_HW_ERR)) { 3181 device_printf(sc->sc_dev, "%s: fatal firmware error\n", 3182 __func__); 3183 /* Dump firmware error log and stop. */ 3184 iwn_fatal_intr(sc); 3185 ifp->if_flags &= ~IFF_UP; 3186 iwn_stop_locked(sc); 3187 goto done; 3188 } 3189 if ((r1 & (IWN_INT_FH_RX | IWN_INT_SW_RX | IWN_INT_RX_PERIODIC)) || 3190 (r2 & IWN_FH_INT_RX)) { 3191 if (sc->sc_flags & IWN_FLAG_USE_ICT) { 3192 if (r1 & (IWN_INT_FH_RX | IWN_INT_SW_RX)) 3193 IWN_WRITE(sc, IWN_FH_INT, IWN_FH_INT_RX); 3194 IWN_WRITE_1(sc, IWN_INT_PERIODIC, 3195 IWN_INT_PERIODIC_DIS); 3196 iwn_notif_intr(sc); 3197 if (r1 & (IWN_INT_FH_RX | IWN_INT_SW_RX)) { 3198 IWN_WRITE_1(sc, IWN_INT_PERIODIC, 3199 IWN_INT_PERIODIC_ENA); 3200 } 3201 } else 3202 iwn_notif_intr(sc); 3203 } 3204 3205 if ((r1 & IWN_INT_FH_TX) || (r2 & IWN_FH_INT_TX)) { 3206 if (sc->sc_flags & IWN_FLAG_USE_ICT) 3207 IWN_WRITE(sc, IWN_FH_INT, IWN_FH_INT_TX); 3208 wakeup(sc); /* FH DMA transfer completed. */ 3209 } 3210 3211 if (r1 & IWN_INT_ALIVE) 3212 wakeup(sc); /* Firmware is alive. */ 3213 3214 if (r1 & IWN_INT_WAKEUP) 3215 iwn_wakeup_intr(sc); 3216 3217 done: 3218 /* Re-enable interrupts. */ 3219 if (ifp->if_flags & IFF_UP) 3220 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask); 3221 3222 IWN_UNLOCK(sc); 3223 } 3224 3225 /* 3226 * Update TX scheduler ring when transmitting an 802.11 frame (4965AGN and 3227 * 5000 adapters use a slightly different format). 3228 */ 3229 static void 3230 iwn4965_update_sched(struct iwn_softc *sc, int qid, int idx, uint8_t id, 3231 uint16_t len) 3232 { 3233 uint16_t *w = &sc->sched[qid * IWN4965_SCHED_COUNT + idx]; 3234 3235 *w = htole16(len + 8); 3236 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map, 3237 BUS_DMASYNC_PREWRITE); 3238 if (idx < IWN_SCHED_WINSZ) { 3239 *(w + IWN_TX_RING_COUNT) = *w; 3240 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map, 3241 BUS_DMASYNC_PREWRITE); 3242 } 3243 } 3244 3245 static void 3246 iwn5000_update_sched(struct iwn_softc *sc, int qid, int idx, uint8_t id, 3247 uint16_t len) 3248 { 3249 uint16_t *w = &sc->sched[qid * IWN5000_SCHED_COUNT + idx]; 3250 3251 *w = htole16(id << 12 | (len + 8)); 3252 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map, 3253 BUS_DMASYNC_PREWRITE); 3254 if (idx < IWN_SCHED_WINSZ) { 3255 *(w + IWN_TX_RING_COUNT) = *w; 3256 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map, 3257 BUS_DMASYNC_PREWRITE); 3258 } 3259 } 3260 3261 #ifdef notyet 3262 static void 3263 iwn5000_reset_sched(struct iwn_softc *sc, int qid, int idx) 3264 { 3265 uint16_t *w = &sc->sched[qid * IWN5000_SCHED_COUNT + idx]; 3266 3267 *w = (*w & htole16(0xf000)) | htole16(1); 3268 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map, 3269 BUS_DMASYNC_PREWRITE); 3270 if (idx < IWN_SCHED_WINSZ) { 3271 *(w + IWN_TX_RING_COUNT) = *w; 3272 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map, 3273 BUS_DMASYNC_PREWRITE); 3274 } 3275 } 3276 #endif 3277 3278 static int 3279 iwn_tx_data(struct iwn_softc *sc, struct mbuf *m, struct ieee80211_node *ni) 3280 { 3281 struct iwn_ops *ops = &sc->ops; 3282 const struct ieee80211_txparam *tp; 3283 struct ieee80211vap *vap = ni->ni_vap; 3284 struct ieee80211com *ic = ni->ni_ic; 3285 struct iwn_node *wn = (void *)ni; 3286 struct iwn_tx_ring *ring; 3287 struct iwn_tx_desc *desc; 3288 struct iwn_tx_data *data; 3289 struct iwn_tx_cmd *cmd; 3290 struct iwn_cmd_data *tx; 3291 struct ieee80211_frame *wh; 3292 struct ieee80211_key *k = NULL; 3293 struct mbuf *m1; 3294 uint32_t flags; 3295 uint16_t qos; 3296 u_int hdrlen; 3297 bus_dma_segment_t *seg, segs[IWN_MAX_SCATTER]; 3298 uint8_t tid, ridx, txant, type; 3299 int ac, i, totlen, error, pad, nsegs = 0, rate; 3300 3301 IWN_LOCK_ASSERT(sc); 3302 3303 wh = mtod(m, struct ieee80211_frame *); 3304 hdrlen = ieee80211_anyhdrsize(wh); 3305 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 3306 3307 /* Select EDCA Access Category and TX ring for this frame. */ 3308 if (IEEE80211_QOS_HAS_SEQ(wh)) { 3309 qos = ((const struct ieee80211_qosframe *)wh)->i_qos[0]; 3310 tid = qos & IEEE80211_QOS_TID; 3311 } else { 3312 qos = 0; 3313 tid = 0; 3314 } 3315 ac = M_WME_GETAC(m); 3316 3317 if (IEEE80211_QOS_HAS_SEQ(wh) && 3318 IEEE80211_AMPDU_RUNNING(&ni->ni_tx_ampdu[ac])) { 3319 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[ac]; 3320 3321 ring = &sc->txq[*(int *)tap->txa_private]; 3322 *(uint16_t *)wh->i_seq = 3323 htole16(ni->ni_txseqs[tid] << IEEE80211_SEQ_SEQ_SHIFT); 3324 ni->ni_txseqs[tid]++; 3325 } else { 3326 ring = &sc->txq[ac]; 3327 } 3328 desc = &ring->desc[ring->cur]; 3329 data = &ring->data[ring->cur]; 3330 3331 /* Choose a TX rate index. */ 3332 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 3333 if (type == IEEE80211_FC0_TYPE_MGT) 3334 rate = tp->mgmtrate; 3335 else if (IEEE80211_IS_MULTICAST(wh->i_addr1)) 3336 rate = tp->mcastrate; 3337 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 3338 rate = tp->ucastrate; 3339 else { 3340 /* XXX pass pktlen */ 3341 (void) ieee80211_ratectl_rate(ni, NULL, 0); 3342 rate = ni->ni_txrate; 3343 } 3344 ridx = ic->ic_rt->rateCodeToIndex[rate]; 3345 3346 /* Encrypt the frame if need be. */ 3347 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 3348 /* Retrieve key for TX. */ 3349 k = ieee80211_crypto_encap(ni, m); 3350 if (k == NULL) { 3351 m_freem(m); 3352 return ENOBUFS; 3353 } 3354 /* 802.11 header may have moved. */ 3355 wh = mtod(m, struct ieee80211_frame *); 3356 } 3357 totlen = m->m_pkthdr.len; 3358 3359 if (ieee80211_radiotap_active_vap(vap)) { 3360 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap; 3361 3362 tap->wt_flags = 0; 3363 tap->wt_rate = rate; 3364 if (k != NULL) 3365 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 3366 3367 ieee80211_radiotap_tx(vap, m); 3368 } 3369 3370 /* Prepare TX firmware command. */ 3371 cmd = &ring->cmd[ring->cur]; 3372 cmd->code = IWN_CMD_TX_DATA; 3373 cmd->flags = 0; 3374 cmd->qid = ring->qid; 3375 cmd->idx = ring->cur; 3376 3377 tx = (struct iwn_cmd_data *)cmd->data; 3378 /* NB: No need to clear tx, all fields are reinitialized here. */ 3379 tx->scratch = 0; /* clear "scratch" area */ 3380 3381 flags = 0; 3382 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 3383 /* Unicast frame, check if an ACK is expected. */ 3384 if (!qos || (qos & IEEE80211_QOS_ACKPOLICY) != 3385 IEEE80211_QOS_ACKPOLICY_NOACK) 3386 flags |= IWN_TX_NEED_ACK; 3387 } 3388 if ((wh->i_fc[0] & 3389 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) == 3390 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR)) 3391 flags |= IWN_TX_IMM_BA; /* Cannot happen yet. */ 3392 3393 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) 3394 flags |= IWN_TX_MORE_FRAG; /* Cannot happen yet. */ 3395 3396 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */ 3397 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 3398 /* NB: Group frames are sent using CCK in 802.11b/g. */ 3399 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) { 3400 flags |= IWN_TX_NEED_RTS; 3401 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 3402 ridx >= IWN_RIDX_OFDM6) { 3403 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 3404 flags |= IWN_TX_NEED_CTS; 3405 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 3406 flags |= IWN_TX_NEED_RTS; 3407 } 3408 if (flags & (IWN_TX_NEED_RTS | IWN_TX_NEED_CTS)) { 3409 if (sc->hw_type != IWN_HW_REV_TYPE_4965) { 3410 /* 5000 autoselects RTS/CTS or CTS-to-self. */ 3411 flags &= ~(IWN_TX_NEED_RTS | IWN_TX_NEED_CTS); 3412 flags |= IWN_TX_NEED_PROTECTION; 3413 } else 3414 flags |= IWN_TX_FULL_TXOP; 3415 } 3416 } 3417 3418 if (IEEE80211_IS_MULTICAST(wh->i_addr1) || 3419 type != IEEE80211_FC0_TYPE_DATA) 3420 tx->id = sc->broadcast_id; 3421 else 3422 tx->id = wn->id; 3423 3424 if (type == IEEE80211_FC0_TYPE_MGT) { 3425 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 3426 3427 /* Tell HW to set timestamp in probe responses. */ 3428 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 3429 flags |= IWN_TX_INSERT_TSTAMP; 3430 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 3431 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 3432 tx->timeout = htole16(3); 3433 else 3434 tx->timeout = htole16(2); 3435 } else 3436 tx->timeout = htole16(0); 3437 3438 if (hdrlen & 3) { 3439 /* First segment length must be a multiple of 4. */ 3440 flags |= IWN_TX_NEED_PADDING; 3441 pad = 4 - (hdrlen & 3); 3442 } else 3443 pad = 0; 3444 3445 tx->len = htole16(totlen); 3446 tx->tid = tid; 3447 tx->rts_ntries = 60; 3448 tx->data_ntries = 15; 3449 tx->lifetime = htole32(IWN_LIFETIME_INFINITE); 3450 tx->rate = wn->ridx[rate]; 3451 if (tx->id == sc->broadcast_id) { 3452 /* Group or management frame. */ 3453 tx->linkq = 0; 3454 /* XXX Alternate between antenna A and B? */ 3455 txant = IWN_LSB(sc->txchainmask); 3456 tx->rate |= htole32(IWN_RFLAG_ANT(txant)); 3457 } else { 3458 tx->linkq = ni->ni_rates.rs_nrates - ridx - 1; 3459 flags |= IWN_TX_LINKQ; /* enable MRR */ 3460 } 3461 /* Set physical address of "scratch area". */ 3462 tx->loaddr = htole32(IWN_LOADDR(data->scratch_paddr)); 3463 tx->hiaddr = IWN_HIADDR(data->scratch_paddr); 3464 3465 /* Copy 802.11 header in TX command. */ 3466 memcpy((uint8_t *)(tx + 1), wh, hdrlen); 3467 3468 /* Trim 802.11 header. */ 3469 m_adj(m, hdrlen); 3470 tx->security = 0; 3471 tx->flags = htole32(flags); 3472 3473 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, segs, 3474 &nsegs, BUS_DMA_NOWAIT); 3475 if (error != 0) { 3476 if (error != EFBIG) { 3477 device_printf(sc->sc_dev, 3478 "%s: can't map mbuf (error %d)\n", __func__, error); 3479 m_freem(m); 3480 return error; 3481 } 3482 /* Too many DMA segments, linearize mbuf. */ 3483 m1 = m_collapse(m, M_DONTWAIT, IWN_MAX_SCATTER); 3484 if (m1 == NULL) { 3485 device_printf(sc->sc_dev, 3486 "%s: could not defrag mbuf\n", __func__); 3487 m_freem(m); 3488 return ENOBUFS; 3489 } 3490 m = m1; 3491 3492 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, 3493 segs, &nsegs, BUS_DMA_NOWAIT); 3494 if (error != 0) { 3495 device_printf(sc->sc_dev, 3496 "%s: can't map mbuf (error %d)\n", __func__, error); 3497 m_freem(m); 3498 return error; 3499 } 3500 } 3501 3502 data->m = m; 3503 data->ni = ni; 3504 3505 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n", 3506 __func__, ring->qid, ring->cur, m->m_pkthdr.len, nsegs); 3507 3508 /* Fill TX descriptor. */ 3509 desc->nsegs = 1; 3510 if (m->m_len != 0) 3511 desc->nsegs += nsegs; 3512 /* First DMA segment is used by the TX command. */ 3513 desc->segs[0].addr = htole32(IWN_LOADDR(data->cmd_paddr)); 3514 desc->segs[0].len = htole16(IWN_HIADDR(data->cmd_paddr) | 3515 (4 + sizeof (*tx) + hdrlen + pad) << 4); 3516 /* Other DMA segments are for data payload. */ 3517 seg = &segs[0]; 3518 for (i = 1; i <= nsegs; i++) { 3519 desc->segs[i].addr = htole32(IWN_LOADDR(seg->ds_addr)); 3520 desc->segs[i].len = htole16(IWN_HIADDR(seg->ds_addr) | 3521 seg->ds_len << 4); 3522 seg++; 3523 } 3524 3525 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 3526 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map, 3527 BUS_DMASYNC_PREWRITE); 3528 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 3529 BUS_DMASYNC_PREWRITE); 3530 3531 /* Update TX scheduler. */ 3532 if (ring->qid >= sc->firstaggqueue) 3533 ops->update_sched(sc, ring->qid, ring->cur, tx->id, totlen); 3534 3535 /* Kick TX ring. */ 3536 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 3537 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur); 3538 3539 /* Mark TX ring as full if we reach a certain threshold. */ 3540 if (++ring->queued > IWN_TX_RING_HIMARK) 3541 sc->qfullmsk |= 1 << ring->qid; 3542 3543 return 0; 3544 } 3545 3546 static int 3547 iwn_tx_data_raw(struct iwn_softc *sc, struct mbuf *m, 3548 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params) 3549 { 3550 struct iwn_ops *ops = &sc->ops; 3551 struct ifnet *ifp = sc->sc_ifp; 3552 struct ieee80211vap *vap = ni->ni_vap; 3553 struct ieee80211com *ic = ifp->if_l2com; 3554 struct iwn_tx_cmd *cmd; 3555 struct iwn_cmd_data *tx; 3556 struct ieee80211_frame *wh; 3557 struct iwn_tx_ring *ring; 3558 struct iwn_tx_desc *desc; 3559 struct iwn_tx_data *data; 3560 struct mbuf *m1; 3561 bus_dma_segment_t *seg, segs[IWN_MAX_SCATTER]; 3562 uint32_t flags; 3563 u_int hdrlen; 3564 int ac, totlen, error, pad, nsegs = 0, i, rate; 3565 uint8_t ridx, type, txant; 3566 3567 IWN_LOCK_ASSERT(sc); 3568 3569 wh = mtod(m, struct ieee80211_frame *); 3570 hdrlen = ieee80211_anyhdrsize(wh); 3571 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 3572 3573 ac = params->ibp_pri & 3; 3574 3575 ring = &sc->txq[ac]; 3576 desc = &ring->desc[ring->cur]; 3577 data = &ring->data[ring->cur]; 3578 3579 /* Choose a TX rate index. */ 3580 rate = params->ibp_rate0; 3581 ridx = ic->ic_rt->rateCodeToIndex[rate]; 3582 if (ridx == (uint8_t)-1) { 3583 /* XXX fall back to mcast/mgmt rate? */ 3584 m_freem(m); 3585 return EINVAL; 3586 } 3587 3588 totlen = m->m_pkthdr.len; 3589 3590 /* Prepare TX firmware command. */ 3591 cmd = &ring->cmd[ring->cur]; 3592 cmd->code = IWN_CMD_TX_DATA; 3593 cmd->flags = 0; 3594 cmd->qid = ring->qid; 3595 cmd->idx = ring->cur; 3596 3597 tx = (struct iwn_cmd_data *)cmd->data; 3598 /* NB: No need to clear tx, all fields are reinitialized here. */ 3599 tx->scratch = 0; /* clear "scratch" area */ 3600 3601 flags = 0; 3602 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 3603 flags |= IWN_TX_NEED_ACK; 3604 if (params->ibp_flags & IEEE80211_BPF_RTS) { 3605 if (sc->hw_type != IWN_HW_REV_TYPE_4965) { 3606 /* 5000 autoselects RTS/CTS or CTS-to-self. */ 3607 flags &= ~IWN_TX_NEED_RTS; 3608 flags |= IWN_TX_NEED_PROTECTION; 3609 } else 3610 flags |= IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP; 3611 } 3612 if (params->ibp_flags & IEEE80211_BPF_CTS) { 3613 if (sc->hw_type != IWN_HW_REV_TYPE_4965) { 3614 /* 5000 autoselects RTS/CTS or CTS-to-self. */ 3615 flags &= ~IWN_TX_NEED_CTS; 3616 flags |= IWN_TX_NEED_PROTECTION; 3617 } else 3618 flags |= IWN_TX_NEED_CTS | IWN_TX_FULL_TXOP; 3619 } 3620 if (type == IEEE80211_FC0_TYPE_MGT) { 3621 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 3622 3623 /* Tell HW to set timestamp in probe responses. */ 3624 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 3625 flags |= IWN_TX_INSERT_TSTAMP; 3626 3627 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 3628 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 3629 tx->timeout = htole16(3); 3630 else 3631 tx->timeout = htole16(2); 3632 } else 3633 tx->timeout = htole16(0); 3634 3635 if (hdrlen & 3) { 3636 /* First segment length must be a multiple of 4. */ 3637 flags |= IWN_TX_NEED_PADDING; 3638 pad = 4 - (hdrlen & 3); 3639 } else 3640 pad = 0; 3641 3642 if (ieee80211_radiotap_active_vap(vap)) { 3643 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap; 3644 3645 tap->wt_flags = 0; 3646 tap->wt_rate = rate; 3647 3648 ieee80211_radiotap_tx(vap, m); 3649 } 3650 3651 tx->len = htole16(totlen); 3652 tx->tid = 0; 3653 tx->id = sc->broadcast_id; 3654 tx->rts_ntries = params->ibp_try1; 3655 tx->data_ntries = params->ibp_try0; 3656 tx->lifetime = htole32(IWN_LIFETIME_INFINITE); 3657 tx->rate = htole32(rate2plcp(rate)); 3658 if (ridx < IWN_RIDX_OFDM6 && 3659 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 3660 tx->rate |= htole32(IWN_RFLAG_CCK); 3661 /* Group or management frame. */ 3662 tx->linkq = 0; 3663 txant = IWN_LSB(sc->txchainmask); 3664 tx->rate |= htole32(IWN_RFLAG_ANT(txant)); 3665 /* Set physical address of "scratch area". */ 3666 tx->loaddr = htole32(IWN_LOADDR(data->scratch_paddr)); 3667 tx->hiaddr = IWN_HIADDR(data->scratch_paddr); 3668 3669 /* Copy 802.11 header in TX command. */ 3670 memcpy((uint8_t *)(tx + 1), wh, hdrlen); 3671 3672 /* Trim 802.11 header. */ 3673 m_adj(m, hdrlen); 3674 tx->security = 0; 3675 tx->flags = htole32(flags); 3676 3677 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, segs, 3678 &nsegs, BUS_DMA_NOWAIT); 3679 if (error != 0) { 3680 if (error != EFBIG) { 3681 device_printf(sc->sc_dev, 3682 "%s: can't map mbuf (error %d)\n", __func__, error); 3683 m_freem(m); 3684 return error; 3685 } 3686 /* Too many DMA segments, linearize mbuf. */ 3687 m1 = m_collapse(m, M_DONTWAIT, IWN_MAX_SCATTER); 3688 if (m1 == NULL) { 3689 device_printf(sc->sc_dev, 3690 "%s: could not defrag mbuf\n", __func__); 3691 m_freem(m); 3692 return ENOBUFS; 3693 } 3694 m = m1; 3695 3696 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m, 3697 segs, &nsegs, BUS_DMA_NOWAIT); 3698 if (error != 0) { 3699 device_printf(sc->sc_dev, 3700 "%s: can't map mbuf (error %d)\n", __func__, error); 3701 m_freem(m); 3702 return error; 3703 } 3704 } 3705 3706 data->m = m; 3707 data->ni = ni; 3708 3709 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n", 3710 __func__, ring->qid, ring->cur, m->m_pkthdr.len, nsegs); 3711 3712 /* Fill TX descriptor. */ 3713 desc->nsegs = 1; 3714 if (m->m_len != 0) 3715 desc->nsegs += nsegs; 3716 /* First DMA segment is used by the TX command. */ 3717 desc->segs[0].addr = htole32(IWN_LOADDR(data->cmd_paddr)); 3718 desc->segs[0].len = htole16(IWN_HIADDR(data->cmd_paddr) | 3719 (4 + sizeof (*tx) + hdrlen + pad) << 4); 3720 /* Other DMA segments are for data payload. */ 3721 seg = &segs[0]; 3722 for (i = 1; i <= nsegs; i++) { 3723 desc->segs[i].addr = htole32(IWN_LOADDR(seg->ds_addr)); 3724 desc->segs[i].len = htole16(IWN_HIADDR(seg->ds_addr) | 3725 seg->ds_len << 4); 3726 seg++; 3727 } 3728 3729 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 3730 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map, 3731 BUS_DMASYNC_PREWRITE); 3732 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 3733 BUS_DMASYNC_PREWRITE); 3734 3735 /* Update TX scheduler. */ 3736 if (ring->qid >= sc->firstaggqueue) 3737 ops->update_sched(sc, ring->qid, ring->cur, tx->id, totlen); 3738 3739 /* Kick TX ring. */ 3740 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 3741 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur); 3742 3743 /* Mark TX ring as full if we reach a certain threshold. */ 3744 if (++ring->queued > IWN_TX_RING_HIMARK) 3745 sc->qfullmsk |= 1 << ring->qid; 3746 3747 return 0; 3748 } 3749 3750 static int 3751 iwn_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 3752 const struct ieee80211_bpf_params *params) 3753 { 3754 struct ieee80211com *ic = ni->ni_ic; 3755 struct ifnet *ifp = ic->ic_ifp; 3756 struct iwn_softc *sc = ifp->if_softc; 3757 int error = 0; 3758 3759 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 3760 ieee80211_free_node(ni); 3761 m_freem(m); 3762 return ENETDOWN; 3763 } 3764 3765 IWN_LOCK(sc); 3766 if (params == NULL) { 3767 /* 3768 * Legacy path; interpret frame contents to decide 3769 * precisely how to send the frame. 3770 */ 3771 error = iwn_tx_data(sc, m, ni); 3772 } else { 3773 /* 3774 * Caller supplied explicit parameters to use in 3775 * sending the frame. 3776 */ 3777 error = iwn_tx_data_raw(sc, m, ni, params); 3778 } 3779 if (error != 0) { 3780 /* NB: m is reclaimed on tx failure */ 3781 ieee80211_free_node(ni); 3782 ifp->if_oerrors++; 3783 } 3784 sc->sc_tx_timer = 5; 3785 3786 IWN_UNLOCK(sc); 3787 return error; 3788 } 3789 3790 static void 3791 iwn_start(struct ifnet *ifp) 3792 { 3793 struct iwn_softc *sc = ifp->if_softc; 3794 3795 IWN_LOCK(sc); 3796 iwn_start_locked(ifp); 3797 IWN_UNLOCK(sc); 3798 } 3799 3800 static void 3801 iwn_start_locked(struct ifnet *ifp) 3802 { 3803 struct iwn_softc *sc = ifp->if_softc; 3804 struct ieee80211_node *ni; 3805 struct mbuf *m; 3806 3807 IWN_LOCK_ASSERT(sc); 3808 3809 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 3810 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) 3811 return; 3812 3813 for (;;) { 3814 if (sc->qfullmsk != 0) { 3815 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 3816 break; 3817 } 3818 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 3819 if (m == NULL) 3820 break; 3821 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 3822 if (iwn_tx_data(sc, m, ni) != 0) { 3823 ieee80211_free_node(ni); 3824 ifp->if_oerrors++; 3825 continue; 3826 } 3827 sc->sc_tx_timer = 5; 3828 } 3829 } 3830 3831 static void 3832 iwn_watchdog(void *arg) 3833 { 3834 struct iwn_softc *sc = arg; 3835 struct ifnet *ifp = sc->sc_ifp; 3836 struct ieee80211com *ic = ifp->if_l2com; 3837 3838 IWN_LOCK_ASSERT(sc); 3839 3840 KASSERT(ifp->if_drv_flags & IFF_DRV_RUNNING, ("not running")); 3841 3842 if (sc->sc_tx_timer > 0) { 3843 if (--sc->sc_tx_timer == 0) { 3844 if_printf(ifp, "device timeout\n"); 3845 ieee80211_runtask(ic, &sc->sc_reinit_task); 3846 return; 3847 } 3848 } 3849 callout_reset(&sc->watchdog_to, hz, iwn_watchdog, sc); 3850 } 3851 3852 static int 3853 iwn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 3854 { 3855 struct iwn_softc *sc = ifp->if_softc; 3856 struct ieee80211com *ic = ifp->if_l2com; 3857 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 3858 struct ifreq *ifr = (struct ifreq *) data; 3859 int error = 0, startall = 0, stop = 0; 3860 3861 switch (cmd) { 3862 case SIOCGIFADDR: 3863 error = ether_ioctl(ifp, cmd, data); 3864 break; 3865 case SIOCSIFFLAGS: 3866 IWN_LOCK(sc); 3867 if (ifp->if_flags & IFF_UP) { 3868 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 3869 iwn_init_locked(sc); 3870 if (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL) 3871 startall = 1; 3872 else 3873 stop = 1; 3874 } 3875 } else { 3876 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 3877 iwn_stop_locked(sc); 3878 } 3879 IWN_UNLOCK(sc); 3880 if (startall) 3881 ieee80211_start_all(ic); 3882 else if (vap != NULL && stop) 3883 ieee80211_stop(vap); 3884 break; 3885 case SIOCGIFMEDIA: 3886 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 3887 break; 3888 default: 3889 error = EINVAL; 3890 break; 3891 } 3892 return error; 3893 } 3894 3895 /* 3896 * Send a command to the firmware. 3897 */ 3898 static int 3899 iwn_cmd(struct iwn_softc *sc, int code, const void *buf, int size, int async) 3900 { 3901 struct iwn_tx_ring *ring = &sc->txq[4]; 3902 struct iwn_tx_desc *desc; 3903 struct iwn_tx_data *data; 3904 struct iwn_tx_cmd *cmd; 3905 struct mbuf *m; 3906 bus_addr_t paddr; 3907 int totlen, error; 3908 3909 if (async == 0) 3910 IWN_LOCK_ASSERT(sc); 3911 3912 desc = &ring->desc[ring->cur]; 3913 data = &ring->data[ring->cur]; 3914 totlen = 4 + size; 3915 3916 if (size > sizeof cmd->data) { 3917 /* Command is too large to fit in a descriptor. */ 3918 if (totlen > MCLBYTES) 3919 return EINVAL; 3920 m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 3921 if (m == NULL) 3922 return ENOMEM; 3923 cmd = mtod(m, struct iwn_tx_cmd *); 3924 error = bus_dmamap_load(ring->data_dmat, data->map, cmd, 3925 totlen, iwn_dma_map_addr, &paddr, BUS_DMA_NOWAIT); 3926 if (error != 0) { 3927 m_freem(m); 3928 return error; 3929 } 3930 data->m = m; 3931 } else { 3932 cmd = &ring->cmd[ring->cur]; 3933 paddr = data->cmd_paddr; 3934 } 3935 3936 cmd->code = code; 3937 cmd->flags = 0; 3938 cmd->qid = ring->qid; 3939 cmd->idx = ring->cur; 3940 memcpy(cmd->data, buf, size); 3941 3942 desc->nsegs = 1; 3943 desc->segs[0].addr = htole32(IWN_LOADDR(paddr)); 3944 desc->segs[0].len = htole16(IWN_HIADDR(paddr) | totlen << 4); 3945 3946 DPRINTF(sc, IWN_DEBUG_CMD, "%s: %s (0x%x) flags %d qid %d idx %d\n", 3947 __func__, iwn_intr_str(cmd->code), cmd->code, 3948 cmd->flags, cmd->qid, cmd->idx); 3949 3950 if (size > sizeof cmd->data) { 3951 bus_dmamap_sync(ring->data_dmat, data->map, 3952 BUS_DMASYNC_PREWRITE); 3953 } else { 3954 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map, 3955 BUS_DMASYNC_PREWRITE); 3956 } 3957 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 3958 BUS_DMASYNC_PREWRITE); 3959 3960 /* Kick command ring. */ 3961 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 3962 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur); 3963 3964 return async ? 0 : msleep(desc, &sc->sc_mtx, PCATCH, "iwncmd", hz); 3965 } 3966 3967 static int 3968 iwn4965_add_node(struct iwn_softc *sc, struct iwn_node_info *node, int async) 3969 { 3970 struct iwn4965_node_info hnode; 3971 caddr_t src, dst; 3972 3973 /* 3974 * We use the node structure for 5000 Series internally (it is 3975 * a superset of the one for 4965AGN). We thus copy the common 3976 * fields before sending the command. 3977 */ 3978 src = (caddr_t)node; 3979 dst = (caddr_t)&hnode; 3980 memcpy(dst, src, 48); 3981 /* Skip TSC, RX MIC and TX MIC fields from ``src''. */ 3982 memcpy(dst + 48, src + 72, 20); 3983 return iwn_cmd(sc, IWN_CMD_ADD_NODE, &hnode, sizeof hnode, async); 3984 } 3985 3986 static int 3987 iwn5000_add_node(struct iwn_softc *sc, struct iwn_node_info *node, int async) 3988 { 3989 /* Direct mapping. */ 3990 return iwn_cmd(sc, IWN_CMD_ADD_NODE, node, sizeof (*node), async); 3991 } 3992 3993 static int 3994 iwn_set_link_quality(struct iwn_softc *sc, struct ieee80211_node *ni) 3995 { 3996 struct iwn_node *wn = (void *)ni; 3997 struct ieee80211_rateset *rs = &ni->ni_rates; 3998 struct iwn_cmd_link_quality linkq; 3999 uint8_t txant; 4000 int i, rate, txrate; 4001 4002 /* Use the first valid TX antenna. */ 4003 txant = IWN_LSB(sc->txchainmask); 4004 4005 memset(&linkq, 0, sizeof linkq); 4006 linkq.id = wn->id; 4007 linkq.antmsk_1stream = txant; 4008 linkq.antmsk_2stream = IWN_ANT_AB; 4009 linkq.ampdu_max = 64; 4010 linkq.ampdu_threshold = 3; 4011 linkq.ampdu_limit = htole16(4000); /* 4ms */ 4012 4013 /* Start at highest available bit-rate. */ 4014 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) 4015 txrate = ni->ni_htrates.rs_nrates - 1; 4016 else 4017 txrate = rs->rs_nrates - 1; 4018 for (i = 0; i < IWN_MAX_TX_RETRIES; i++) { 4019 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) 4020 rate = IEEE80211_RATE_MCS | txrate; 4021 else 4022 rate = rs->rs_rates[txrate] & IEEE80211_RATE_VAL; 4023 linkq.retry[i] = wn->ridx[rate]; 4024 4025 if ((le32toh(wn->ridx[rate]) & IWN_RFLAG_MCS) && 4026 (le32toh(wn->ridx[rate]) & 0xff) > 7) 4027 linkq.mimo = i + 1; 4028 4029 /* Next retry at immediate lower bit-rate. */ 4030 if (txrate > 0) 4031 txrate--; 4032 } 4033 return iwn_cmd(sc, IWN_CMD_LINK_QUALITY, &linkq, sizeof linkq, 1); 4034 } 4035 4036 /* 4037 * Broadcast node is used to send group-addressed and management frames. 4038 */ 4039 static int 4040 iwn_add_broadcast_node(struct iwn_softc *sc, int async) 4041 { 4042 struct iwn_ops *ops = &sc->ops; 4043 struct ifnet *ifp = sc->sc_ifp; 4044 struct ieee80211com *ic = ifp->if_l2com; 4045 struct iwn_node_info node; 4046 struct iwn_cmd_link_quality linkq; 4047 uint8_t txant; 4048 int i, error; 4049 4050 memset(&node, 0, sizeof node); 4051 IEEE80211_ADDR_COPY(node.macaddr, ifp->if_broadcastaddr); 4052 node.id = sc->broadcast_id; 4053 DPRINTF(sc, IWN_DEBUG_RESET, "%s: adding broadcast node\n", __func__); 4054 if ((error = ops->add_node(sc, &node, async)) != 0) 4055 return error; 4056 4057 /* Use the first valid TX antenna. */ 4058 txant = IWN_LSB(sc->txchainmask); 4059 4060 memset(&linkq, 0, sizeof linkq); 4061 linkq.id = sc->broadcast_id; 4062 linkq.antmsk_1stream = txant; 4063 linkq.antmsk_2stream = IWN_ANT_AB; 4064 linkq.ampdu_max = 64; 4065 linkq.ampdu_threshold = 3; 4066 linkq.ampdu_limit = htole16(4000); /* 4ms */ 4067 4068 /* Use lowest mandatory bit-rate. */ 4069 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) 4070 linkq.retry[0] = htole32(0xd); 4071 else 4072 linkq.retry[0] = htole32(10 | IWN_RFLAG_CCK); 4073 linkq.retry[0] |= htole32(IWN_RFLAG_ANT(txant)); 4074 /* Use same bit-rate for all TX retries. */ 4075 for (i = 1; i < IWN_MAX_TX_RETRIES; i++) { 4076 linkq.retry[i] = linkq.retry[0]; 4077 } 4078 return iwn_cmd(sc, IWN_CMD_LINK_QUALITY, &linkq, sizeof linkq, async); 4079 } 4080 4081 static int 4082 iwn_updateedca(struct ieee80211com *ic) 4083 { 4084 #define IWN_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */ 4085 struct iwn_softc *sc = ic->ic_ifp->if_softc; 4086 struct iwn_edca_params cmd; 4087 int aci; 4088 4089 memset(&cmd, 0, sizeof cmd); 4090 cmd.flags = htole32(IWN_EDCA_UPDATE); 4091 for (aci = 0; aci < WME_NUM_AC; aci++) { 4092 const struct wmeParams *ac = 4093 &ic->ic_wme.wme_chanParams.cap_wmeParams[aci]; 4094 cmd.ac[aci].aifsn = ac->wmep_aifsn; 4095 cmd.ac[aci].cwmin = htole16(IWN_EXP2(ac->wmep_logcwmin)); 4096 cmd.ac[aci].cwmax = htole16(IWN_EXP2(ac->wmep_logcwmax)); 4097 cmd.ac[aci].txoplimit = 4098 htole16(IEEE80211_TXOP_TO_US(ac->wmep_txopLimit)); 4099 } 4100 IEEE80211_UNLOCK(ic); 4101 IWN_LOCK(sc); 4102 (void)iwn_cmd(sc, IWN_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1); 4103 IWN_UNLOCK(sc); 4104 IEEE80211_LOCK(ic); 4105 return 0; 4106 #undef IWN_EXP2 4107 } 4108 4109 static void 4110 iwn_update_mcast(struct ifnet *ifp) 4111 { 4112 /* Ignore */ 4113 } 4114 4115 static void 4116 iwn_set_led(struct iwn_softc *sc, uint8_t which, uint8_t off, uint8_t on) 4117 { 4118 struct iwn_cmd_led led; 4119 4120 /* Clear microcode LED ownership. */ 4121 IWN_CLRBITS(sc, IWN_LED, IWN_LED_BSM_CTRL); 4122 4123 led.which = which; 4124 led.unit = htole32(10000); /* on/off in unit of 100ms */ 4125 led.off = off; 4126 led.on = on; 4127 (void)iwn_cmd(sc, IWN_CMD_SET_LED, &led, sizeof led, 1); 4128 } 4129 4130 /* 4131 * Set the critical temperature at which the firmware will stop the radio 4132 * and notify us. 4133 */ 4134 static int 4135 iwn_set_critical_temp(struct iwn_softc *sc) 4136 { 4137 struct iwn_critical_temp crit; 4138 int32_t temp; 4139 4140 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_CTEMP_STOP_RF); 4141 4142 if (sc->hw_type == IWN_HW_REV_TYPE_5150) 4143 temp = (IWN_CTOK(110) - sc->temp_off) * -5; 4144 else if (sc->hw_type == IWN_HW_REV_TYPE_4965) 4145 temp = IWN_CTOK(110); 4146 else 4147 temp = 110; 4148 memset(&crit, 0, sizeof crit); 4149 crit.tempR = htole32(temp); 4150 DPRINTF(sc, IWN_DEBUG_RESET, "setting critical temp to %d\n", temp); 4151 return iwn_cmd(sc, IWN_CMD_SET_CRITICAL_TEMP, &crit, sizeof crit, 0); 4152 } 4153 4154 static int 4155 iwn_set_timing(struct iwn_softc *sc, struct ieee80211_node *ni) 4156 { 4157 struct iwn_cmd_timing cmd; 4158 uint64_t val, mod; 4159 4160 memset(&cmd, 0, sizeof cmd); 4161 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t)); 4162 cmd.bintval = htole16(ni->ni_intval); 4163 cmd.lintval = htole16(10); 4164 4165 /* Compute remaining time until next beacon. */ 4166 val = (uint64_t)ni->ni_intval * IEEE80211_DUR_TU; 4167 mod = le64toh(cmd.tstamp) % val; 4168 cmd.binitval = htole32((uint32_t)(val - mod)); 4169 4170 DPRINTF(sc, IWN_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n", 4171 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod)); 4172 4173 return iwn_cmd(sc, IWN_CMD_TIMING, &cmd, sizeof cmd, 1); 4174 } 4175 4176 static void 4177 iwn4965_power_calibration(struct iwn_softc *sc, int temp) 4178 { 4179 struct ifnet *ifp = sc->sc_ifp; 4180 struct ieee80211com *ic = ifp->if_l2com; 4181 4182 /* Adjust TX power if need be (delta >= 3 degC). */ 4183 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: temperature %d->%d\n", 4184 __func__, sc->temp, temp); 4185 if (abs(temp - sc->temp) >= 3) { 4186 /* Record temperature of last calibration. */ 4187 sc->temp = temp; 4188 (void)iwn4965_set_txpower(sc, ic->ic_bsschan, 1); 4189 } 4190 } 4191 4192 /* 4193 * Set TX power for current channel (each rate has its own power settings). 4194 * This function takes into account the regulatory information from EEPROM, 4195 * the current temperature and the current voltage. 4196 */ 4197 static int 4198 iwn4965_set_txpower(struct iwn_softc *sc, struct ieee80211_channel *ch, 4199 int async) 4200 { 4201 /* Fixed-point arithmetic division using a n-bit fractional part. */ 4202 #define fdivround(a, b, n) \ 4203 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 4204 /* Linear interpolation. */ 4205 #define interpolate(x, x1, y1, x2, y2, n) \ 4206 ((y1) + fdivround(((int)(x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 4207 4208 static const int tdiv[IWN_NATTEN_GROUPS] = { 9, 8, 8, 8, 6 }; 4209 struct iwn_ucode_info *uc = &sc->ucode_info; 4210 struct iwn4965_cmd_txpower cmd; 4211 struct iwn4965_eeprom_chan_samples *chans; 4212 const uint8_t *rf_gain, *dsp_gain; 4213 int32_t vdiff, tdiff; 4214 int i, c, grp, maxpwr; 4215 uint8_t chan; 4216 4217 /* Retrieve current channel from last RXON. */ 4218 chan = sc->rxon.chan; 4219 DPRINTF(sc, IWN_DEBUG_RESET, "setting TX power for channel %d\n", 4220 chan); 4221 4222 memset(&cmd, 0, sizeof cmd); 4223 cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1; 4224 cmd.chan = chan; 4225 4226 if (IEEE80211_IS_CHAN_5GHZ(ch)) { 4227 maxpwr = sc->maxpwr5GHz; 4228 rf_gain = iwn4965_rf_gain_5ghz; 4229 dsp_gain = iwn4965_dsp_gain_5ghz; 4230 } else { 4231 maxpwr = sc->maxpwr2GHz; 4232 rf_gain = iwn4965_rf_gain_2ghz; 4233 dsp_gain = iwn4965_dsp_gain_2ghz; 4234 } 4235 4236 /* Compute voltage compensation. */ 4237 vdiff = ((int32_t)le32toh(uc->volt) - sc->eeprom_voltage) / 7; 4238 if (vdiff > 0) 4239 vdiff *= 2; 4240 if (abs(vdiff) > 2) 4241 vdiff = 0; 4242 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 4243 "%s: voltage compensation=%d (UCODE=%d, EEPROM=%d)\n", 4244 __func__, vdiff, le32toh(uc->volt), sc->eeprom_voltage); 4245 4246 /* Get channel attenuation group. */ 4247 if (chan <= 20) /* 1-20 */ 4248 grp = 4; 4249 else if (chan <= 43) /* 34-43 */ 4250 grp = 0; 4251 else if (chan <= 70) /* 44-70 */ 4252 grp = 1; 4253 else if (chan <= 124) /* 71-124 */ 4254 grp = 2; 4255 else /* 125-200 */ 4256 grp = 3; 4257 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 4258 "%s: chan %d, attenuation group=%d\n", __func__, chan, grp); 4259 4260 /* Get channel sub-band. */ 4261 for (i = 0; i < IWN_NBANDS; i++) 4262 if (sc->bands[i].lo != 0 && 4263 sc->bands[i].lo <= chan && chan <= sc->bands[i].hi) 4264 break; 4265 if (i == IWN_NBANDS) /* Can't happen in real-life. */ 4266 return EINVAL; 4267 chans = sc->bands[i].chans; 4268 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 4269 "%s: chan %d sub-band=%d\n", __func__, chan, i); 4270 4271 for (c = 0; c < 2; c++) { 4272 uint8_t power, gain, temp; 4273 int maxchpwr, pwr, ridx, idx; 4274 4275 power = interpolate(chan, 4276 chans[0].num, chans[0].samples[c][1].power, 4277 chans[1].num, chans[1].samples[c][1].power, 1); 4278 gain = interpolate(chan, 4279 chans[0].num, chans[0].samples[c][1].gain, 4280 chans[1].num, chans[1].samples[c][1].gain, 1); 4281 temp = interpolate(chan, 4282 chans[0].num, chans[0].samples[c][1].temp, 4283 chans[1].num, chans[1].samples[c][1].temp, 1); 4284 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 4285 "%s: Tx chain %d: power=%d gain=%d temp=%d\n", 4286 __func__, c, power, gain, temp); 4287 4288 /* Compute temperature compensation. */ 4289 tdiff = ((sc->temp - temp) * 2) / tdiv[grp]; 4290 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 4291 "%s: temperature compensation=%d (current=%d, EEPROM=%d)\n", 4292 __func__, tdiff, sc->temp, temp); 4293 4294 for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++) { 4295 /* Convert dBm to half-dBm. */ 4296 maxchpwr = sc->maxpwr[chan] * 2; 4297 if ((ridx / 8) & 1) 4298 maxchpwr -= 6; /* MIMO 2T: -3dB */ 4299 4300 pwr = maxpwr; 4301 4302 /* Adjust TX power based on rate. */ 4303 if ((ridx % 8) == 5) 4304 pwr -= 15; /* OFDM48: -7.5dB */ 4305 else if ((ridx % 8) == 6) 4306 pwr -= 17; /* OFDM54: -8.5dB */ 4307 else if ((ridx % 8) == 7) 4308 pwr -= 20; /* OFDM60: -10dB */ 4309 else 4310 pwr -= 10; /* Others: -5dB */ 4311 4312 /* Do not exceed channel max TX power. */ 4313 if (pwr > maxchpwr) 4314 pwr = maxchpwr; 4315 4316 idx = gain - (pwr - power) - tdiff - vdiff; 4317 if ((ridx / 8) & 1) /* MIMO */ 4318 idx += (int32_t)le32toh(uc->atten[grp][c]); 4319 4320 if (cmd.band == 0) 4321 idx += 9; /* 5GHz */ 4322 if (ridx == IWN_RIDX_MAX) 4323 idx += 5; /* CCK */ 4324 4325 /* Make sure idx stays in a valid range. */ 4326 if (idx < 0) 4327 idx = 0; 4328 else if (idx > IWN4965_MAX_PWR_INDEX) 4329 idx = IWN4965_MAX_PWR_INDEX; 4330 4331 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 4332 "%s: Tx chain %d, rate idx %d: power=%d\n", 4333 __func__, c, ridx, idx); 4334 cmd.power[ridx].rf_gain[c] = rf_gain[idx]; 4335 cmd.power[ridx].dsp_gain[c] = dsp_gain[idx]; 4336 } 4337 } 4338 4339 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 4340 "%s: set tx power for chan %d\n", __func__, chan); 4341 return iwn_cmd(sc, IWN_CMD_TXPOWER, &cmd, sizeof cmd, async); 4342 4343 #undef interpolate 4344 #undef fdivround 4345 } 4346 4347 static int 4348 iwn5000_set_txpower(struct iwn_softc *sc, struct ieee80211_channel *ch, 4349 int async) 4350 { 4351 struct iwn5000_cmd_txpower cmd; 4352 4353 /* 4354 * TX power calibration is handled automatically by the firmware 4355 * for 5000 Series. 4356 */ 4357 memset(&cmd, 0, sizeof cmd); 4358 cmd.global_limit = 2 * IWN5000_TXPOWER_MAX_DBM; /* 16 dBm */ 4359 cmd.flags = IWN5000_TXPOWER_NO_CLOSED; 4360 cmd.srv_limit = IWN5000_TXPOWER_AUTO; 4361 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: setting TX power\n", __func__); 4362 return iwn_cmd(sc, IWN_CMD_TXPOWER_DBM, &cmd, sizeof cmd, async); 4363 } 4364 4365 /* 4366 * Retrieve the maximum RSSI (in dBm) among receivers. 4367 */ 4368 static int 4369 iwn4965_get_rssi(struct iwn_softc *sc, struct iwn_rx_stat *stat) 4370 { 4371 struct iwn4965_rx_phystat *phy = (void *)stat->phybuf; 4372 uint8_t mask, agc; 4373 int rssi; 4374 4375 mask = (le16toh(phy->antenna) >> 4) & IWN_ANT_ABC; 4376 agc = (le16toh(phy->agc) >> 7) & 0x7f; 4377 4378 rssi = 0; 4379 if (mask & IWN_ANT_A) 4380 rssi = MAX(rssi, phy->rssi[0]); 4381 if (mask & IWN_ANT_B) 4382 rssi = MAX(rssi, phy->rssi[2]); 4383 if (mask & IWN_ANT_C) 4384 rssi = MAX(rssi, phy->rssi[4]); 4385 4386 DPRINTF(sc, IWN_DEBUG_RECV, 4387 "%s: agc %d mask 0x%x rssi %d %d %d result %d\n", __func__, agc, 4388 mask, phy->rssi[0], phy->rssi[2], phy->rssi[4], 4389 rssi - agc - IWN_RSSI_TO_DBM); 4390 return rssi - agc - IWN_RSSI_TO_DBM; 4391 } 4392 4393 static int 4394 iwn5000_get_rssi(struct iwn_softc *sc, struct iwn_rx_stat *stat) 4395 { 4396 struct iwn5000_rx_phystat *phy = (void *)stat->phybuf; 4397 uint8_t agc; 4398 int rssi; 4399 4400 agc = (le32toh(phy->agc) >> 9) & 0x7f; 4401 4402 rssi = MAX(le16toh(phy->rssi[0]) & 0xff, 4403 le16toh(phy->rssi[1]) & 0xff); 4404 rssi = MAX(le16toh(phy->rssi[2]) & 0xff, rssi); 4405 4406 DPRINTF(sc, IWN_DEBUG_RECV, 4407 "%s: agc %d rssi %d %d %d result %d\n", __func__, agc, 4408 phy->rssi[0], phy->rssi[1], phy->rssi[2], 4409 rssi - agc - IWN_RSSI_TO_DBM); 4410 return rssi - agc - IWN_RSSI_TO_DBM; 4411 } 4412 4413 /* 4414 * Retrieve the average noise (in dBm) among receivers. 4415 */ 4416 static int 4417 iwn_get_noise(const struct iwn_rx_general_stats *stats) 4418 { 4419 int i, total, nbant, noise; 4420 4421 total = nbant = 0; 4422 for (i = 0; i < 3; i++) { 4423 if ((noise = le32toh(stats->noise[i]) & 0xff) == 0) 4424 continue; 4425 total += noise; 4426 nbant++; 4427 } 4428 /* There should be at least one antenna but check anyway. */ 4429 return (nbant == 0) ? -127 : (total / nbant) - 107; 4430 } 4431 4432 /* 4433 * Compute temperature (in degC) from last received statistics. 4434 */ 4435 static int 4436 iwn4965_get_temperature(struct iwn_softc *sc) 4437 { 4438 struct iwn_ucode_info *uc = &sc->ucode_info; 4439 int32_t r1, r2, r3, r4, temp; 4440 4441 r1 = le32toh(uc->temp[0].chan20MHz); 4442 r2 = le32toh(uc->temp[1].chan20MHz); 4443 r3 = le32toh(uc->temp[2].chan20MHz); 4444 r4 = le32toh(sc->rawtemp); 4445 4446 if (r1 == r3) /* Prevents division by 0 (should not happen). */ 4447 return 0; 4448 4449 /* Sign-extend 23-bit R4 value to 32-bit. */ 4450 r4 = ((r4 & 0xffffff) ^ 0x800000) - 0x800000; 4451 /* Compute temperature in Kelvin. */ 4452 temp = (259 * (r4 - r2)) / (r3 - r1); 4453 temp = (temp * 97) / 100 + 8; 4454 4455 DPRINTF(sc, IWN_DEBUG_ANY, "temperature %dK/%dC\n", temp, 4456 IWN_KTOC(temp)); 4457 return IWN_KTOC(temp); 4458 } 4459 4460 static int 4461 iwn5000_get_temperature(struct iwn_softc *sc) 4462 { 4463 int32_t temp; 4464 4465 /* 4466 * Temperature is not used by the driver for 5000 Series because 4467 * TX power calibration is handled by firmware. 4468 */ 4469 temp = le32toh(sc->rawtemp); 4470 if (sc->hw_type == IWN_HW_REV_TYPE_5150) { 4471 temp = (temp / -5) + sc->temp_off; 4472 temp = IWN_KTOC(temp); 4473 } 4474 return temp; 4475 } 4476 4477 /* 4478 * Initialize sensitivity calibration state machine. 4479 */ 4480 static int 4481 iwn_init_sensitivity(struct iwn_softc *sc) 4482 { 4483 struct iwn_ops *ops = &sc->ops; 4484 struct iwn_calib_state *calib = &sc->calib; 4485 uint32_t flags; 4486 int error; 4487 4488 /* Reset calibration state machine. */ 4489 memset(calib, 0, sizeof (*calib)); 4490 calib->state = IWN_CALIB_STATE_INIT; 4491 calib->cck_state = IWN_CCK_STATE_HIFA; 4492 /* Set initial correlation values. */ 4493 calib->ofdm_x1 = sc->limits->min_ofdm_x1; 4494 calib->ofdm_mrc_x1 = sc->limits->min_ofdm_mrc_x1; 4495 calib->ofdm_x4 = sc->limits->min_ofdm_x4; 4496 calib->ofdm_mrc_x4 = sc->limits->min_ofdm_mrc_x4; 4497 calib->cck_x4 = 125; 4498 calib->cck_mrc_x4 = sc->limits->min_cck_mrc_x4; 4499 calib->energy_cck = sc->limits->energy_cck; 4500 4501 /* Write initial sensitivity. */ 4502 if ((error = iwn_send_sensitivity(sc)) != 0) 4503 return error; 4504 4505 /* Write initial gains. */ 4506 if ((error = ops->init_gains(sc)) != 0) 4507 return error; 4508 4509 /* Request statistics at each beacon interval. */ 4510 flags = 0; 4511 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: sending request for statistics\n", 4512 __func__); 4513 return iwn_cmd(sc, IWN_CMD_GET_STATISTICS, &flags, sizeof flags, 1); 4514 } 4515 4516 /* 4517 * Collect noise and RSSI statistics for the first 20 beacons received 4518 * after association and use them to determine connected antennas and 4519 * to set differential gains. 4520 */ 4521 static void 4522 iwn_collect_noise(struct iwn_softc *sc, 4523 const struct iwn_rx_general_stats *stats) 4524 { 4525 struct iwn_ops *ops = &sc->ops; 4526 struct iwn_calib_state *calib = &sc->calib; 4527 uint32_t val; 4528 int i; 4529 4530 /* Accumulate RSSI and noise for all 3 antennas. */ 4531 for (i = 0; i < 3; i++) { 4532 calib->rssi[i] += le32toh(stats->rssi[i]) & 0xff; 4533 calib->noise[i] += le32toh(stats->noise[i]) & 0xff; 4534 } 4535 /* NB: We update differential gains only once after 20 beacons. */ 4536 if (++calib->nbeacons < 20) 4537 return; 4538 4539 /* Determine highest average RSSI. */ 4540 val = MAX(calib->rssi[0], calib->rssi[1]); 4541 val = MAX(calib->rssi[2], val); 4542 4543 /* Determine which antennas are connected. */ 4544 sc->chainmask = sc->rxchainmask; 4545 for (i = 0; i < 3; i++) 4546 if (val - calib->rssi[i] > 15 * 20) 4547 sc->chainmask &= ~(1 << i); 4548 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4549 "%s: RX chains mask: theoretical=0x%x, actual=0x%x\n", 4550 __func__, sc->rxchainmask, sc->chainmask); 4551 4552 /* If none of the TX antennas are connected, keep at least one. */ 4553 if ((sc->chainmask & sc->txchainmask) == 0) 4554 sc->chainmask |= IWN_LSB(sc->txchainmask); 4555 4556 (void)ops->set_gains(sc); 4557 calib->state = IWN_CALIB_STATE_RUN; 4558 4559 #ifdef notyet 4560 /* XXX Disable RX chains with no antennas connected. */ 4561 sc->rxon.rxchain = htole16(IWN_RXCHAIN_SEL(sc->chainmask)); 4562 (void)iwn_cmd(sc, IWN_CMD_RXON, &sc->rxon, sc->rxonsz, 1); 4563 #endif 4564 4565 #if 0 4566 /* XXX: not yet */ 4567 /* Enable power-saving mode if requested by user. */ 4568 if (sc->sc_ic.ic_flags & IEEE80211_F_PMGTON) 4569 (void)iwn_set_pslevel(sc, 0, 3, 1); 4570 #endif 4571 } 4572 4573 static int 4574 iwn4965_init_gains(struct iwn_softc *sc) 4575 { 4576 struct iwn_phy_calib_gain cmd; 4577 4578 memset(&cmd, 0, sizeof cmd); 4579 cmd.code = IWN4965_PHY_CALIB_DIFF_GAIN; 4580 /* Differential gains initially set to 0 for all 3 antennas. */ 4581 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4582 "%s: setting initial differential gains\n", __func__); 4583 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1); 4584 } 4585 4586 static int 4587 iwn5000_init_gains(struct iwn_softc *sc) 4588 { 4589 struct iwn_phy_calib cmd; 4590 4591 memset(&cmd, 0, sizeof cmd); 4592 cmd.code = sc->reset_noise_gain; 4593 cmd.ngroups = 1; 4594 cmd.isvalid = 1; 4595 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4596 "%s: setting initial differential gains\n", __func__); 4597 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1); 4598 } 4599 4600 static int 4601 iwn4965_set_gains(struct iwn_softc *sc) 4602 { 4603 struct iwn_calib_state *calib = &sc->calib; 4604 struct iwn_phy_calib_gain cmd; 4605 int i, delta, noise; 4606 4607 /* Get minimal noise among connected antennas. */ 4608 noise = INT_MAX; /* NB: There's at least one antenna. */ 4609 for (i = 0; i < 3; i++) 4610 if (sc->chainmask & (1 << i)) 4611 noise = MIN(calib->noise[i], noise); 4612 4613 memset(&cmd, 0, sizeof cmd); 4614 cmd.code = IWN4965_PHY_CALIB_DIFF_GAIN; 4615 /* Set differential gains for connected antennas. */ 4616 for (i = 0; i < 3; i++) { 4617 if (sc->chainmask & (1 << i)) { 4618 /* Compute attenuation (in unit of 1.5dB). */ 4619 delta = (noise - (int32_t)calib->noise[i]) / 30; 4620 /* NB: delta <= 0 */ 4621 /* Limit to [-4.5dB,0]. */ 4622 cmd.gain[i] = MIN(abs(delta), 3); 4623 if (delta < 0) 4624 cmd.gain[i] |= 1 << 2; /* sign bit */ 4625 } 4626 } 4627 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4628 "setting differential gains Ant A/B/C: %x/%x/%x (%x)\n", 4629 cmd.gain[0], cmd.gain[1], cmd.gain[2], sc->chainmask); 4630 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1); 4631 } 4632 4633 static int 4634 iwn5000_set_gains(struct iwn_softc *sc) 4635 { 4636 struct iwn_calib_state *calib = &sc->calib; 4637 struct iwn_phy_calib_gain cmd; 4638 int i, ant, div, delta; 4639 4640 /* We collected 20 beacons and !=6050 need a 1.5 factor. */ 4641 div = (sc->hw_type == IWN_HW_REV_TYPE_6050) ? 20 : 30; 4642 4643 memset(&cmd, 0, sizeof cmd); 4644 cmd.code = sc->noise_gain; 4645 cmd.ngroups = 1; 4646 cmd.isvalid = 1; 4647 /* Get first available RX antenna as referential. */ 4648 ant = IWN_LSB(sc->rxchainmask); 4649 /* Set differential gains for other antennas. */ 4650 for (i = ant + 1; i < 3; i++) { 4651 if (sc->chainmask & (1 << i)) { 4652 /* The delta is relative to antenna "ant". */ 4653 delta = ((int32_t)calib->noise[ant] - 4654 (int32_t)calib->noise[i]) / div; 4655 /* Limit to [-4.5dB,+4.5dB]. */ 4656 cmd.gain[i - 1] = MIN(abs(delta), 3); 4657 if (delta < 0) 4658 cmd.gain[i - 1] |= 1 << 2; /* sign bit */ 4659 } 4660 } 4661 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4662 "setting differential gains Ant B/C: %x/%x (%x)\n", 4663 cmd.gain[0], cmd.gain[1], sc->chainmask); 4664 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1); 4665 } 4666 4667 /* 4668 * Tune RF RX sensitivity based on the number of false alarms detected 4669 * during the last beacon period. 4670 */ 4671 static void 4672 iwn_tune_sensitivity(struct iwn_softc *sc, const struct iwn_rx_stats *stats) 4673 { 4674 #define inc(val, inc, max) \ 4675 if ((val) < (max)) { \ 4676 if ((val) < (max) - (inc)) \ 4677 (val) += (inc); \ 4678 else \ 4679 (val) = (max); \ 4680 needs_update = 1; \ 4681 } 4682 #define dec(val, dec, min) \ 4683 if ((val) > (min)) { \ 4684 if ((val) > (min) + (dec)) \ 4685 (val) -= (dec); \ 4686 else \ 4687 (val) = (min); \ 4688 needs_update = 1; \ 4689 } 4690 4691 const struct iwn_sensitivity_limits *limits = sc->limits; 4692 struct iwn_calib_state *calib = &sc->calib; 4693 uint32_t val, rxena, fa; 4694 uint32_t energy[3], energy_min; 4695 uint8_t noise[3], noise_ref; 4696 int i, needs_update = 0; 4697 4698 /* Check that we've been enabled long enough. */ 4699 if ((rxena = le32toh(stats->general.load)) == 0) 4700 return; 4701 4702 /* Compute number of false alarms since last call for OFDM. */ 4703 fa = le32toh(stats->ofdm.bad_plcp) - calib->bad_plcp_ofdm; 4704 fa += le32toh(stats->ofdm.fa) - calib->fa_ofdm; 4705 fa *= 200 * IEEE80211_DUR_TU; /* 200TU */ 4706 4707 /* Save counters values for next call. */ 4708 calib->bad_plcp_ofdm = le32toh(stats->ofdm.bad_plcp); 4709 calib->fa_ofdm = le32toh(stats->ofdm.fa); 4710 4711 if (fa > 50 * rxena) { 4712 /* High false alarm count, decrease sensitivity. */ 4713 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4714 "%s: OFDM high false alarm count: %u\n", __func__, fa); 4715 inc(calib->ofdm_x1, 1, limits->max_ofdm_x1); 4716 inc(calib->ofdm_mrc_x1, 1, limits->max_ofdm_mrc_x1); 4717 inc(calib->ofdm_x4, 1, limits->max_ofdm_x4); 4718 inc(calib->ofdm_mrc_x4, 1, limits->max_ofdm_mrc_x4); 4719 4720 } else if (fa < 5 * rxena) { 4721 /* Low false alarm count, increase sensitivity. */ 4722 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4723 "%s: OFDM low false alarm count: %u\n", __func__, fa); 4724 dec(calib->ofdm_x1, 1, limits->min_ofdm_x1); 4725 dec(calib->ofdm_mrc_x1, 1, limits->min_ofdm_mrc_x1); 4726 dec(calib->ofdm_x4, 1, limits->min_ofdm_x4); 4727 dec(calib->ofdm_mrc_x4, 1, limits->min_ofdm_mrc_x4); 4728 } 4729 4730 /* Compute maximum noise among 3 receivers. */ 4731 for (i = 0; i < 3; i++) 4732 noise[i] = (le32toh(stats->general.noise[i]) >> 8) & 0xff; 4733 val = MAX(noise[0], noise[1]); 4734 val = MAX(noise[2], val); 4735 /* Insert it into our samples table. */ 4736 calib->noise_samples[calib->cur_noise_sample] = val; 4737 calib->cur_noise_sample = (calib->cur_noise_sample + 1) % 20; 4738 4739 /* Compute maximum noise among last 20 samples. */ 4740 noise_ref = calib->noise_samples[0]; 4741 for (i = 1; i < 20; i++) 4742 noise_ref = MAX(noise_ref, calib->noise_samples[i]); 4743 4744 /* Compute maximum energy among 3 receivers. */ 4745 for (i = 0; i < 3; i++) 4746 energy[i] = le32toh(stats->general.energy[i]); 4747 val = MIN(energy[0], energy[1]); 4748 val = MIN(energy[2], val); 4749 /* Insert it into our samples table. */ 4750 calib->energy_samples[calib->cur_energy_sample] = val; 4751 calib->cur_energy_sample = (calib->cur_energy_sample + 1) % 10; 4752 4753 /* Compute minimum energy among last 10 samples. */ 4754 energy_min = calib->energy_samples[0]; 4755 for (i = 1; i < 10; i++) 4756 energy_min = MAX(energy_min, calib->energy_samples[i]); 4757 energy_min += 6; 4758 4759 /* Compute number of false alarms since last call for CCK. */ 4760 fa = le32toh(stats->cck.bad_plcp) - calib->bad_plcp_cck; 4761 fa += le32toh(stats->cck.fa) - calib->fa_cck; 4762 fa *= 200 * IEEE80211_DUR_TU; /* 200TU */ 4763 4764 /* Save counters values for next call. */ 4765 calib->bad_plcp_cck = le32toh(stats->cck.bad_plcp); 4766 calib->fa_cck = le32toh(stats->cck.fa); 4767 4768 if (fa > 50 * rxena) { 4769 /* High false alarm count, decrease sensitivity. */ 4770 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4771 "%s: CCK high false alarm count: %u\n", __func__, fa); 4772 calib->cck_state = IWN_CCK_STATE_HIFA; 4773 calib->low_fa = 0; 4774 4775 if (calib->cck_x4 > 160) { 4776 calib->noise_ref = noise_ref; 4777 if (calib->energy_cck > 2) 4778 dec(calib->energy_cck, 2, energy_min); 4779 } 4780 if (calib->cck_x4 < 160) { 4781 calib->cck_x4 = 161; 4782 needs_update = 1; 4783 } else 4784 inc(calib->cck_x4, 3, limits->max_cck_x4); 4785 4786 inc(calib->cck_mrc_x4, 3, limits->max_cck_mrc_x4); 4787 4788 } else if (fa < 5 * rxena) { 4789 /* Low false alarm count, increase sensitivity. */ 4790 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4791 "%s: CCK low false alarm count: %u\n", __func__, fa); 4792 calib->cck_state = IWN_CCK_STATE_LOFA; 4793 calib->low_fa++; 4794 4795 if (calib->cck_state != IWN_CCK_STATE_INIT && 4796 (((int32_t)calib->noise_ref - (int32_t)noise_ref) > 2 || 4797 calib->low_fa > 100)) { 4798 inc(calib->energy_cck, 2, limits->min_energy_cck); 4799 dec(calib->cck_x4, 3, limits->min_cck_x4); 4800 dec(calib->cck_mrc_x4, 3, limits->min_cck_mrc_x4); 4801 } 4802 } else { 4803 /* Not worth to increase or decrease sensitivity. */ 4804 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4805 "%s: CCK normal false alarm count: %u\n", __func__, fa); 4806 calib->low_fa = 0; 4807 calib->noise_ref = noise_ref; 4808 4809 if (calib->cck_state == IWN_CCK_STATE_HIFA) { 4810 /* Previous interval had many false alarms. */ 4811 dec(calib->energy_cck, 8, energy_min); 4812 } 4813 calib->cck_state = IWN_CCK_STATE_INIT; 4814 } 4815 4816 if (needs_update) 4817 (void)iwn_send_sensitivity(sc); 4818 #undef dec 4819 #undef inc 4820 } 4821 4822 static int 4823 iwn_send_sensitivity(struct iwn_softc *sc) 4824 { 4825 struct iwn_calib_state *calib = &sc->calib; 4826 struct iwn_enhanced_sensitivity_cmd cmd; 4827 int len; 4828 4829 memset(&cmd, 0, sizeof cmd); 4830 len = sizeof (struct iwn_sensitivity_cmd); 4831 cmd.which = IWN_SENSITIVITY_WORKTBL; 4832 /* OFDM modulation. */ 4833 cmd.corr_ofdm_x1 = htole16(calib->ofdm_x1); 4834 cmd.corr_ofdm_mrc_x1 = htole16(calib->ofdm_mrc_x1); 4835 cmd.corr_ofdm_x4 = htole16(calib->ofdm_x4); 4836 cmd.corr_ofdm_mrc_x4 = htole16(calib->ofdm_mrc_x4); 4837 cmd.energy_ofdm = htole16(sc->limits->energy_ofdm); 4838 cmd.energy_ofdm_th = htole16(62); 4839 /* CCK modulation. */ 4840 cmd.corr_cck_x4 = htole16(calib->cck_x4); 4841 cmd.corr_cck_mrc_x4 = htole16(calib->cck_mrc_x4); 4842 cmd.energy_cck = htole16(calib->energy_cck); 4843 /* Barker modulation: use default values. */ 4844 cmd.corr_barker = htole16(190); 4845 cmd.corr_barker_mrc = htole16(390); 4846 4847 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 4848 "%s: set sensitivity %d/%d/%d/%d/%d/%d/%d\n", __func__, 4849 calib->ofdm_x1, calib->ofdm_mrc_x1, calib->ofdm_x4, 4850 calib->ofdm_mrc_x4, calib->cck_x4, 4851 calib->cck_mrc_x4, calib->energy_cck); 4852 4853 if (!(sc->sc_flags & IWN_FLAG_ENH_SENS)) 4854 goto send; 4855 /* Enhanced sensitivity settings. */ 4856 len = sizeof (struct iwn_enhanced_sensitivity_cmd); 4857 cmd.ofdm_det_slope_mrc = htole16(668); 4858 cmd.ofdm_det_icept_mrc = htole16(4); 4859 cmd.ofdm_det_slope = htole16(486); 4860 cmd.ofdm_det_icept = htole16(37); 4861 cmd.cck_det_slope_mrc = htole16(853); 4862 cmd.cck_det_icept_mrc = htole16(4); 4863 cmd.cck_det_slope = htole16(476); 4864 cmd.cck_det_icept = htole16(99); 4865 send: 4866 return iwn_cmd(sc, IWN_CMD_SET_SENSITIVITY, &cmd, len, 1); 4867 } 4868 4869 /* 4870 * Set STA mode power saving level (between 0 and 5). 4871 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving. 4872 */ 4873 static int 4874 iwn_set_pslevel(struct iwn_softc *sc, int dtim, int level, int async) 4875 { 4876 struct iwn_pmgt_cmd cmd; 4877 const struct iwn_pmgt *pmgt; 4878 uint32_t max, skip_dtim; 4879 uint32_t reg; 4880 int i; 4881 4882 /* Select which PS parameters to use. */ 4883 if (dtim <= 2) 4884 pmgt = &iwn_pmgt[0][level]; 4885 else if (dtim <= 10) 4886 pmgt = &iwn_pmgt[1][level]; 4887 else 4888 pmgt = &iwn_pmgt[2][level]; 4889 4890 memset(&cmd, 0, sizeof cmd); 4891 if (level != 0) /* not CAM */ 4892 cmd.flags |= htole16(IWN_PS_ALLOW_SLEEP); 4893 if (level == 5) 4894 cmd.flags |= htole16(IWN_PS_FAST_PD); 4895 /* Retrieve PCIe Active State Power Management (ASPM). */ 4896 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1); 4897 if (!(reg & 0x1)) /* L0s Entry disabled. */ 4898 cmd.flags |= htole16(IWN_PS_PCI_PMGT); 4899 cmd.rxtimeout = htole32(pmgt->rxtimeout * 1024); 4900 cmd.txtimeout = htole32(pmgt->txtimeout * 1024); 4901 4902 if (dtim == 0) { 4903 dtim = 1; 4904 skip_dtim = 0; 4905 } else 4906 skip_dtim = pmgt->skip_dtim; 4907 if (skip_dtim != 0) { 4908 cmd.flags |= htole16(IWN_PS_SLEEP_OVER_DTIM); 4909 max = pmgt->intval[4]; 4910 if (max == (uint32_t)-1) 4911 max = dtim * (skip_dtim + 1); 4912 else if (max > dtim) 4913 max = (max / dtim) * dtim; 4914 } else 4915 max = dtim; 4916 for (i = 0; i < 5; i++) 4917 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i])); 4918 4919 DPRINTF(sc, IWN_DEBUG_RESET, "setting power saving level to %d\n", 4920 level); 4921 return iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async); 4922 } 4923 4924 static int 4925 iwn_send_btcoex(struct iwn_softc *sc) 4926 { 4927 struct iwn_bluetooth cmd; 4928 4929 memset(&cmd, 0, sizeof cmd); 4930 cmd.flags = IWN_BT_COEX_CHAN_ANN | IWN_BT_COEX_BT_PRIO; 4931 cmd.lead_time = IWN_BT_LEAD_TIME_DEF; 4932 cmd.max_kill = IWN_BT_MAX_KILL_DEF; 4933 DPRINTF(sc, IWN_DEBUG_RESET, "%s: configuring bluetooth coexistence\n", 4934 __func__); 4935 return iwn_cmd(sc, IWN_CMD_BT_COEX, &cmd, sizeof(cmd), 0); 4936 } 4937 4938 static int 4939 iwn_send_advanced_btcoex(struct iwn_softc *sc) 4940 { 4941 static const uint32_t btcoex_3wire[12] = { 4942 0xaaaaaaaa, 0xaaaaaaaa, 0xaeaaaaaa, 0xaaaaaaaa, 4943 0xcc00ff28, 0x0000aaaa, 0xcc00aaaa, 0x0000aaaa, 4944 0xc0004000, 0x00004000, 0xf0005000, 0xf0005000, 4945 }; 4946 struct iwn6000_btcoex_config btconfig; 4947 struct iwn_btcoex_priotable btprio; 4948 struct iwn_btcoex_prot btprot; 4949 int error, i; 4950 4951 memset(&btconfig, 0, sizeof btconfig); 4952 btconfig.flags = 145; 4953 btconfig.max_kill = 5; 4954 btconfig.bt3_t7_timer = 1; 4955 btconfig.kill_ack = htole32(0xffff0000); 4956 btconfig.kill_cts = htole32(0xffff0000); 4957 btconfig.sample_time = 2; 4958 btconfig.bt3_t2_timer = 0xc; 4959 for (i = 0; i < 12; i++) 4960 btconfig.lookup_table[i] = htole32(btcoex_3wire[i]); 4961 btconfig.valid = htole16(0xff); 4962 btconfig.prio_boost = 0xf0; 4963 DPRINTF(sc, IWN_DEBUG_RESET, 4964 "%s: configuring advanced bluetooth coexistence\n", __func__); 4965 error = iwn_cmd(sc, IWN_CMD_BT_COEX, &btconfig, sizeof(btconfig), 1); 4966 if (error != 0) 4967 return error; 4968 4969 memset(&btprio, 0, sizeof btprio); 4970 btprio.calib_init1 = 0x6; 4971 btprio.calib_init2 = 0x7; 4972 btprio.calib_periodic_low1 = 0x2; 4973 btprio.calib_periodic_low2 = 0x3; 4974 btprio.calib_periodic_high1 = 0x4; 4975 btprio.calib_periodic_high2 = 0x5; 4976 btprio.dtim = 0x6; 4977 btprio.scan52 = 0x8; 4978 btprio.scan24 = 0xa; 4979 error = iwn_cmd(sc, IWN_CMD_BT_COEX_PRIOTABLE, &btprio, sizeof(btprio), 4980 1); 4981 if (error != 0) 4982 return error; 4983 4984 /* Force BT state machine change. */ 4985 memset(&btprot, 0, sizeof btprio); 4986 btprot.open = 1; 4987 btprot.type = 1; 4988 error = iwn_cmd(sc, IWN_CMD_BT_COEX_PROT, &btprot, sizeof(btprot), 1); 4989 if (error != 0) 4990 return error; 4991 btprot.open = 0; 4992 return iwn_cmd(sc, IWN_CMD_BT_COEX_PROT, &btprot, sizeof(btprot), 1); 4993 } 4994 4995 static int 4996 iwn_config(struct iwn_softc *sc) 4997 { 4998 struct iwn_ops *ops = &sc->ops; 4999 struct ifnet *ifp = sc->sc_ifp; 5000 struct ieee80211com *ic = ifp->if_l2com; 5001 uint32_t txmask; 5002 uint16_t rxchain; 5003 int error; 5004 5005 if (sc->hw_type == IWN_HW_REV_TYPE_6005) { 5006 /* Set radio temperature sensor offset. */ 5007 error = iwn5000_temp_offset_calib(sc); 5008 if (error != 0) { 5009 device_printf(sc->sc_dev, 5010 "%s: could not set temperature offset\n", __func__); 5011 return error; 5012 } 5013 } 5014 5015 /* Configure valid TX chains for >=5000 Series. */ 5016 if (sc->hw_type != IWN_HW_REV_TYPE_4965) { 5017 txmask = htole32(sc->txchainmask); 5018 DPRINTF(sc, IWN_DEBUG_RESET, 5019 "%s: configuring valid TX chains 0x%x\n", __func__, txmask); 5020 error = iwn_cmd(sc, IWN5000_CMD_TX_ANT_CONFIG, &txmask, 5021 sizeof txmask, 0); 5022 if (error != 0) { 5023 device_printf(sc->sc_dev, 5024 "%s: could not configure valid TX chains, " 5025 "error %d\n", __func__, error); 5026 return error; 5027 } 5028 } 5029 5030 /* Configure bluetooth coexistence. */ 5031 if (sc->sc_flags & IWN_FLAG_ADV_BTCOEX) 5032 error = iwn_send_advanced_btcoex(sc); 5033 else 5034 error = iwn_send_btcoex(sc); 5035 if (error != 0) { 5036 device_printf(sc->sc_dev, 5037 "%s: could not configure bluetooth coexistence, error %d\n", 5038 __func__, error); 5039 return error; 5040 } 5041 5042 /* Set mode, channel, RX filter and enable RX. */ 5043 memset(&sc->rxon, 0, sizeof (struct iwn_rxon)); 5044 IEEE80211_ADDR_COPY(sc->rxon.myaddr, IF_LLADDR(ifp)); 5045 IEEE80211_ADDR_COPY(sc->rxon.wlap, IF_LLADDR(ifp)); 5046 sc->rxon.chan = ieee80211_chan2ieee(ic, ic->ic_curchan); 5047 sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF); 5048 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 5049 sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ); 5050 switch (ic->ic_opmode) { 5051 case IEEE80211_M_STA: 5052 sc->rxon.mode = IWN_MODE_STA; 5053 sc->rxon.filter = htole32(IWN_FILTER_MULTICAST); 5054 break; 5055 case IEEE80211_M_MONITOR: 5056 sc->rxon.mode = IWN_MODE_MONITOR; 5057 sc->rxon.filter = htole32(IWN_FILTER_MULTICAST | 5058 IWN_FILTER_CTL | IWN_FILTER_PROMISC); 5059 break; 5060 default: 5061 /* Should not get there. */ 5062 break; 5063 } 5064 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */ 5065 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */ 5066 sc->rxon.ht_single_mask = 0xff; 5067 sc->rxon.ht_dual_mask = 0xff; 5068 sc->rxon.ht_triple_mask = 0xff; 5069 rxchain = 5070 IWN_RXCHAIN_VALID(sc->rxchainmask) | 5071 IWN_RXCHAIN_MIMO_COUNT(2) | 5072 IWN_RXCHAIN_IDLE_COUNT(2); 5073 sc->rxon.rxchain = htole16(rxchain); 5074 DPRINTF(sc, IWN_DEBUG_RESET, "%s: setting configuration\n", __func__); 5075 error = iwn_cmd(sc, IWN_CMD_RXON, &sc->rxon, sc->rxonsz, 0); 5076 if (error != 0) { 5077 device_printf(sc->sc_dev, "%s: RXON command failed\n", 5078 __func__); 5079 return error; 5080 } 5081 5082 if ((error = iwn_add_broadcast_node(sc, 0)) != 0) { 5083 device_printf(sc->sc_dev, "%s: could not add broadcast node\n", 5084 __func__); 5085 return error; 5086 } 5087 5088 /* Configuration has changed, set TX power accordingly. */ 5089 if ((error = ops->set_txpower(sc, ic->ic_curchan, 0)) != 0) { 5090 device_printf(sc->sc_dev, "%s: could not set TX power\n", 5091 __func__); 5092 return error; 5093 } 5094 5095 if ((error = iwn_set_critical_temp(sc)) != 0) { 5096 device_printf(sc->sc_dev, 5097 "%s: could not set critical temperature\n", __func__); 5098 return error; 5099 } 5100 5101 /* Set power saving level to CAM during initialization. */ 5102 if ((error = iwn_set_pslevel(sc, 0, 0, 0)) != 0) { 5103 device_printf(sc->sc_dev, 5104 "%s: could not set power saving level\n", __func__); 5105 return error; 5106 } 5107 return 0; 5108 } 5109 5110 /* 5111 * Add an ssid element to a frame. 5112 */ 5113 static uint8_t * 5114 ieee80211_add_ssid(uint8_t *frm, const uint8_t *ssid, u_int len) 5115 { 5116 *frm++ = IEEE80211_ELEMID_SSID; 5117 *frm++ = len; 5118 memcpy(frm, ssid, len); 5119 return frm + len; 5120 } 5121 5122 static int 5123 iwn_scan(struct iwn_softc *sc) 5124 { 5125 struct ifnet *ifp = sc->sc_ifp; 5126 struct ieee80211com *ic = ifp->if_l2com; 5127 struct ieee80211_scan_state *ss = ic->ic_scan; /*XXX*/ 5128 struct ieee80211_node *ni = ss->ss_vap->iv_bss; 5129 struct iwn_scan_hdr *hdr; 5130 struct iwn_cmd_data *tx; 5131 struct iwn_scan_essid *essid; 5132 struct iwn_scan_chan *chan; 5133 struct ieee80211_frame *wh; 5134 struct ieee80211_rateset *rs; 5135 struct ieee80211_channel *c; 5136 uint8_t *buf, *frm; 5137 uint16_t rxchain; 5138 uint8_t txant; 5139 int buflen, error; 5140 5141 buf = malloc(IWN_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO); 5142 if (buf == NULL) { 5143 device_printf(sc->sc_dev, 5144 "%s: could not allocate buffer for scan command\n", 5145 __func__); 5146 return ENOMEM; 5147 } 5148 hdr = (struct iwn_scan_hdr *)buf; 5149 /* 5150 * Move to the next channel if no frames are received within 10ms 5151 * after sending the probe request. 5152 */ 5153 hdr->quiet_time = htole16(10); /* timeout in milliseconds */ 5154 hdr->quiet_threshold = htole16(1); /* min # of packets */ 5155 5156 /* Select antennas for scanning. */ 5157 rxchain = 5158 IWN_RXCHAIN_VALID(sc->rxchainmask) | 5159 IWN_RXCHAIN_FORCE_MIMO_SEL(sc->rxchainmask) | 5160 IWN_RXCHAIN_DRIVER_FORCE; 5161 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) && 5162 sc->hw_type == IWN_HW_REV_TYPE_4965) { 5163 /* Ant A must be avoided in 5GHz because of an HW bug. */ 5164 rxchain |= IWN_RXCHAIN_FORCE_SEL(IWN_ANT_B); 5165 } else /* Use all available RX antennas. */ 5166 rxchain |= IWN_RXCHAIN_FORCE_SEL(sc->rxchainmask); 5167 hdr->rxchain = htole16(rxchain); 5168 hdr->filter = htole32(IWN_FILTER_MULTICAST | IWN_FILTER_BEACON); 5169 5170 tx = (struct iwn_cmd_data *)(hdr + 1); 5171 tx->flags = htole32(IWN_TX_AUTO_SEQ); 5172 tx->id = sc->broadcast_id; 5173 tx->lifetime = htole32(IWN_LIFETIME_INFINITE); 5174 5175 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) { 5176 /* Send probe requests at 6Mbps. */ 5177 tx->rate = htole32(0xd); 5178 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A]; 5179 } else { 5180 hdr->flags = htole32(IWN_RXON_24GHZ | IWN_RXON_AUTO); 5181 if (sc->hw_type == IWN_HW_REV_TYPE_4965 && 5182 sc->rxon.associd && sc->rxon.chan > 14) 5183 tx->rate = htole32(0xd); 5184 else { 5185 /* Send probe requests at 1Mbps. */ 5186 tx->rate = htole32(10 | IWN_RFLAG_CCK); 5187 } 5188 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G]; 5189 } 5190 /* Use the first valid TX antenna. */ 5191 txant = IWN_LSB(sc->txchainmask); 5192 tx->rate |= htole32(IWN_RFLAG_ANT(txant)); 5193 5194 essid = (struct iwn_scan_essid *)(tx + 1); 5195 if (ss->ss_ssid[0].len != 0) { 5196 essid[0].id = IEEE80211_ELEMID_SSID; 5197 essid[0].len = ss->ss_ssid[0].len; 5198 memcpy(essid[0].data, ss->ss_ssid[0].ssid, ss->ss_ssid[0].len); 5199 } 5200 /* 5201 * Build a probe request frame. Most of the following code is a 5202 * copy & paste of what is done in net80211. 5203 */ 5204 wh = (struct ieee80211_frame *)(essid + 20); 5205 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 5206 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 5207 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 5208 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 5209 IEEE80211_ADDR_COPY(wh->i_addr2, IF_LLADDR(ifp)); 5210 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr); 5211 *(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */ 5212 *(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */ 5213 5214 frm = (uint8_t *)(wh + 1); 5215 frm = ieee80211_add_ssid(frm, NULL, 0); 5216 frm = ieee80211_add_rates(frm, rs); 5217 if (rs->rs_nrates > IEEE80211_RATE_SIZE) 5218 frm = ieee80211_add_xrates(frm, rs); 5219 if (ic->ic_htcaps & IEEE80211_HTC_HT) 5220 frm = ieee80211_add_htcap(frm, ni); 5221 5222 /* Set length of probe request. */ 5223 tx->len = htole16(frm - (uint8_t *)wh); 5224 5225 c = ic->ic_curchan; 5226 chan = (struct iwn_scan_chan *)frm; 5227 chan->chan = htole16(ieee80211_chan2ieee(ic, c)); 5228 chan->flags = 0; 5229 if (ss->ss_nssid > 0) 5230 chan->flags |= htole32(IWN_CHAN_NPBREQS(1)); 5231 chan->dsp_gain = 0x6e; 5232 if (IEEE80211_IS_CHAN_5GHZ(c) && 5233 !(c->ic_flags & IEEE80211_CHAN_PASSIVE)) { 5234 chan->rf_gain = 0x3b; 5235 chan->active = htole16(24); 5236 chan->passive = htole16(110); 5237 chan->flags |= htole32(IWN_CHAN_ACTIVE); 5238 } else if (IEEE80211_IS_CHAN_5GHZ(c)) { 5239 chan->rf_gain = 0x3b; 5240 chan->active = htole16(24); 5241 if (sc->rxon.associd) 5242 chan->passive = htole16(78); 5243 else 5244 chan->passive = htole16(110); 5245 hdr->crc_threshold = 0xffff; 5246 } else if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) { 5247 chan->rf_gain = 0x28; 5248 chan->active = htole16(36); 5249 chan->passive = htole16(120); 5250 chan->flags |= htole32(IWN_CHAN_ACTIVE); 5251 } else { 5252 chan->rf_gain = 0x28; 5253 chan->active = htole16(36); 5254 if (sc->rxon.associd) 5255 chan->passive = htole16(88); 5256 else 5257 chan->passive = htole16(120); 5258 hdr->crc_threshold = 0xffff; 5259 } 5260 5261 DPRINTF(sc, IWN_DEBUG_STATE, 5262 "%s: chan %u flags 0x%x rf_gain 0x%x " 5263 "dsp_gain 0x%x active 0x%x passive 0x%x\n", __func__, 5264 chan->chan, chan->flags, chan->rf_gain, chan->dsp_gain, 5265 chan->active, chan->passive); 5266 5267 hdr->nchan++; 5268 chan++; 5269 buflen = (uint8_t *)chan - buf; 5270 hdr->len = htole16(buflen); 5271 5272 DPRINTF(sc, IWN_DEBUG_STATE, "sending scan command nchan=%d\n", 5273 hdr->nchan); 5274 error = iwn_cmd(sc, IWN_CMD_SCAN, buf, buflen, 1); 5275 free(buf, M_DEVBUF); 5276 return error; 5277 } 5278 5279 static int 5280 iwn_auth(struct iwn_softc *sc, struct ieee80211vap *vap) 5281 { 5282 struct iwn_ops *ops = &sc->ops; 5283 struct ifnet *ifp = sc->sc_ifp; 5284 struct ieee80211com *ic = ifp->if_l2com; 5285 struct ieee80211_node *ni = vap->iv_bss; 5286 int error; 5287 5288 /* Update adapter configuration. */ 5289 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid); 5290 sc->rxon.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 5291 sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF); 5292 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 5293 sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ); 5294 if (ic->ic_flags & IEEE80211_F_SHSLOT) 5295 sc->rxon.flags |= htole32(IWN_RXON_SHSLOT); 5296 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 5297 sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE); 5298 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) { 5299 sc->rxon.cck_mask = 0; 5300 sc->rxon.ofdm_mask = 0x15; 5301 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) { 5302 sc->rxon.cck_mask = 0x03; 5303 sc->rxon.ofdm_mask = 0; 5304 } else { 5305 /* Assume 802.11b/g. */ 5306 sc->rxon.cck_mask = 0x0f; 5307 sc->rxon.ofdm_mask = 0x15; 5308 } 5309 DPRINTF(sc, IWN_DEBUG_STATE, "rxon chan %d flags %x cck %x ofdm %x\n", 5310 sc->rxon.chan, sc->rxon.flags, sc->rxon.cck_mask, 5311 sc->rxon.ofdm_mask); 5312 error = iwn_cmd(sc, IWN_CMD_RXON, &sc->rxon, sc->rxonsz, 1); 5313 if (error != 0) { 5314 device_printf(sc->sc_dev, "%s: RXON command failed, error %d\n", 5315 __func__, error); 5316 return error; 5317 } 5318 5319 /* Configuration has changed, set TX power accordingly. */ 5320 if ((error = ops->set_txpower(sc, ni->ni_chan, 1)) != 0) { 5321 device_printf(sc->sc_dev, 5322 "%s: could not set TX power, error %d\n", __func__, error); 5323 return error; 5324 } 5325 /* 5326 * Reconfiguring RXON clears the firmware nodes table so we must 5327 * add the broadcast node again. 5328 */ 5329 if ((error = iwn_add_broadcast_node(sc, 1)) != 0) { 5330 device_printf(sc->sc_dev, 5331 "%s: could not add broadcast node, error %d\n", __func__, 5332 error); 5333 return error; 5334 } 5335 return 0; 5336 } 5337 5338 static int 5339 iwn_run(struct iwn_softc *sc, struct ieee80211vap *vap) 5340 { 5341 struct iwn_ops *ops = &sc->ops; 5342 struct ifnet *ifp = sc->sc_ifp; 5343 struct ieee80211com *ic = ifp->if_l2com; 5344 struct ieee80211_node *ni = vap->iv_bss; 5345 struct iwn_node_info node; 5346 uint32_t htflags = 0; 5347 int error; 5348 5349 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 5350 /* Link LED blinks while monitoring. */ 5351 iwn_set_led(sc, IWN_LED_LINK, 5, 5); 5352 return 0; 5353 } 5354 if ((error = iwn_set_timing(sc, ni)) != 0) { 5355 device_printf(sc->sc_dev, 5356 "%s: could not set timing, error %d\n", __func__, error); 5357 return error; 5358 } 5359 5360 /* Update adapter configuration. */ 5361 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid); 5362 sc->rxon.associd = htole16(IEEE80211_AID(ni->ni_associd)); 5363 sc->rxon.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 5364 sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF); 5365 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 5366 sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ); 5367 if (ic->ic_flags & IEEE80211_F_SHSLOT) 5368 sc->rxon.flags |= htole32(IWN_RXON_SHSLOT); 5369 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 5370 sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE); 5371 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) { 5372 sc->rxon.cck_mask = 0; 5373 sc->rxon.ofdm_mask = 0x15; 5374 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) { 5375 sc->rxon.cck_mask = 0x03; 5376 sc->rxon.ofdm_mask = 0; 5377 } else { 5378 /* Assume 802.11b/g. */ 5379 sc->rxon.cck_mask = 0x0f; 5380 sc->rxon.ofdm_mask = 0x15; 5381 } 5382 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 5383 htflags |= IWN_RXON_HT_PROTMODE(ic->ic_curhtprotmode); 5384 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) { 5385 switch (ic->ic_curhtprotmode) { 5386 case IEEE80211_HTINFO_OPMODE_HT20PR: 5387 htflags |= IWN_RXON_HT_MODEPURE40; 5388 break; 5389 default: 5390 htflags |= IWN_RXON_HT_MODEMIXED; 5391 break; 5392 } 5393 } 5394 if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan)) 5395 htflags |= IWN_RXON_HT_HT40MINUS; 5396 } 5397 sc->rxon.flags |= htole32(htflags); 5398 sc->rxon.filter |= htole32(IWN_FILTER_BSS); 5399 DPRINTF(sc, IWN_DEBUG_STATE, "rxon chan %d flags %x\n", 5400 sc->rxon.chan, sc->rxon.flags); 5401 error = iwn_cmd(sc, IWN_CMD_RXON, &sc->rxon, sc->rxonsz, 1); 5402 if (error != 0) { 5403 device_printf(sc->sc_dev, 5404 "%s: could not update configuration, error %d\n", __func__, 5405 error); 5406 return error; 5407 } 5408 5409 /* Configuration has changed, set TX power accordingly. */ 5410 if ((error = ops->set_txpower(sc, ni->ni_chan, 1)) != 0) { 5411 device_printf(sc->sc_dev, 5412 "%s: could not set TX power, error %d\n", __func__, error); 5413 return error; 5414 } 5415 5416 /* Fake a join to initialize the TX rate. */ 5417 ((struct iwn_node *)ni)->id = IWN_ID_BSS; 5418 iwn_newassoc(ni, 1); 5419 5420 /* Add BSS node. */ 5421 memset(&node, 0, sizeof node); 5422 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr); 5423 node.id = IWN_ID_BSS; 5424 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 5425 switch (ni->ni_htcap & IEEE80211_HTCAP_SMPS) { 5426 case IEEE80211_HTCAP_SMPS_ENA: 5427 node.htflags |= htole32(IWN_SMPS_MIMO_DIS); 5428 break; 5429 case IEEE80211_HTCAP_SMPS_DYNAMIC: 5430 node.htflags |= htole32(IWN_SMPS_MIMO_PROT); 5431 break; 5432 } 5433 node.htflags |= htole32(IWN_AMDPU_SIZE_FACTOR(3) | 5434 IWN_AMDPU_DENSITY(5)); /* 4us */ 5435 if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) 5436 node.htflags |= htole32(IWN_NODE_HT40); 5437 } 5438 DPRINTF(sc, IWN_DEBUG_STATE, "%s: adding BSS node\n", __func__); 5439 error = ops->add_node(sc, &node, 1); 5440 if (error != 0) { 5441 device_printf(sc->sc_dev, 5442 "%s: could not add BSS node, error %d\n", __func__, error); 5443 return error; 5444 } 5445 DPRINTF(sc, IWN_DEBUG_STATE, "%s: setting link quality for node %d\n", 5446 __func__, node.id); 5447 if ((error = iwn_set_link_quality(sc, ni)) != 0) { 5448 device_printf(sc->sc_dev, 5449 "%s: could not setup link quality for node %d, error %d\n", 5450 __func__, node.id, error); 5451 return error; 5452 } 5453 5454 if ((error = iwn_init_sensitivity(sc)) != 0) { 5455 device_printf(sc->sc_dev, 5456 "%s: could not set sensitivity, error %d\n", __func__, 5457 error); 5458 return error; 5459 } 5460 /* Start periodic calibration timer. */ 5461 sc->calib.state = IWN_CALIB_STATE_ASSOC; 5462 sc->calib_cnt = 0; 5463 callout_reset(&sc->calib_to, msecs_to_ticks(500), iwn_calib_timeout, 5464 sc); 5465 5466 /* Link LED always on while associated. */ 5467 iwn_set_led(sc, IWN_LED_LINK, 0, 1); 5468 return 0; 5469 } 5470 5471 /* 5472 * This function is called by upper layer when an ADDBA request is received 5473 * from another STA and before the ADDBA response is sent. 5474 */ 5475 static int 5476 iwn_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap, 5477 int baparamset, int batimeout, int baseqctl) 5478 { 5479 #define MS(_v, _f) (((_v) & _f) >> _f##_S) 5480 struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc; 5481 struct iwn_ops *ops = &sc->ops; 5482 struct iwn_node *wn = (void *)ni; 5483 struct iwn_node_info node; 5484 uint16_t ssn; 5485 uint8_t tid; 5486 int error; 5487 5488 tid = MS(le16toh(baparamset), IEEE80211_BAPS_TID); 5489 ssn = MS(le16toh(baseqctl), IEEE80211_BASEQ_START); 5490 5491 memset(&node, 0, sizeof node); 5492 node.id = wn->id; 5493 node.control = IWN_NODE_UPDATE; 5494 node.flags = IWN_FLAG_SET_ADDBA; 5495 node.addba_tid = tid; 5496 node.addba_ssn = htole16(ssn); 5497 DPRINTF(sc, IWN_DEBUG_RECV, "ADDBA RA=%d TID=%d SSN=%d\n", 5498 wn->id, tid, ssn); 5499 error = ops->add_node(sc, &node, 1); 5500 if (error != 0) 5501 return error; 5502 return sc->sc_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl); 5503 #undef MS 5504 } 5505 5506 /* 5507 * This function is called by upper layer on teardown of an HT-immediate 5508 * Block Ack agreement (eg. uppon receipt of a DELBA frame). 5509 */ 5510 static void 5511 iwn_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap) 5512 { 5513 struct ieee80211com *ic = ni->ni_ic; 5514 struct iwn_softc *sc = ic->ic_ifp->if_softc; 5515 struct iwn_ops *ops = &sc->ops; 5516 struct iwn_node *wn = (void *)ni; 5517 struct iwn_node_info node; 5518 uint8_t tid; 5519 5520 /* XXX: tid as an argument */ 5521 for (tid = 0; tid < WME_NUM_TID; tid++) { 5522 if (&ni->ni_rx_ampdu[tid] == rap) 5523 break; 5524 } 5525 5526 memset(&node, 0, sizeof node); 5527 node.id = wn->id; 5528 node.control = IWN_NODE_UPDATE; 5529 node.flags = IWN_FLAG_SET_DELBA; 5530 node.delba_tid = tid; 5531 DPRINTF(sc, IWN_DEBUG_RECV, "DELBA RA=%d TID=%d\n", wn->id, tid); 5532 (void)ops->add_node(sc, &node, 1); 5533 sc->sc_ampdu_rx_stop(ni, rap); 5534 } 5535 5536 static int 5537 iwn_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap, 5538 int dialogtoken, int baparamset, int batimeout) 5539 { 5540 struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc; 5541 int qid; 5542 5543 for (qid = sc->firstaggqueue; qid < sc->ntxqs; qid++) { 5544 if (sc->qid2tap[qid] == NULL) 5545 break; 5546 } 5547 if (qid == sc->ntxqs) { 5548 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: not free aggregation queue\n", 5549 __func__); 5550 return 0; 5551 } 5552 tap->txa_private = malloc(sizeof(int), M_DEVBUF, M_NOWAIT); 5553 if (tap->txa_private == NULL) { 5554 device_printf(sc->sc_dev, 5555 "%s: failed to alloc TX aggregation structure\n", __func__); 5556 return 0; 5557 } 5558 sc->qid2tap[qid] = tap; 5559 *(int *)tap->txa_private = qid; 5560 return sc->sc_addba_request(ni, tap, dialogtoken, baparamset, 5561 batimeout); 5562 } 5563 5564 static int 5565 iwn_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap, 5566 int code, int baparamset, int batimeout) 5567 { 5568 struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc; 5569 int qid = *(int *)tap->txa_private; 5570 uint8_t tid = WME_AC_TO_TID(tap->txa_ac); 5571 int ret; 5572 5573 if (code == IEEE80211_STATUS_SUCCESS) { 5574 ni->ni_txseqs[tid] = tap->txa_start & 0xfff; 5575 ret = iwn_ampdu_tx_start(ni->ni_ic, ni, tid); 5576 if (ret != 1) 5577 return ret; 5578 } else { 5579 sc->qid2tap[qid] = NULL; 5580 free(tap->txa_private, M_DEVBUF); 5581 tap->txa_private = NULL; 5582 } 5583 return sc->sc_addba_response(ni, tap, code, baparamset, batimeout); 5584 } 5585 5586 /* 5587 * This function is called by upper layer when an ADDBA response is received 5588 * from another STA. 5589 */ 5590 static int 5591 iwn_ampdu_tx_start(struct ieee80211com *ic, struct ieee80211_node *ni, 5592 uint8_t tid) 5593 { 5594 struct ieee80211_tx_ampdu *tap = &ni->ni_tx_ampdu[TID_TO_WME_AC(tid)]; 5595 struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc; 5596 struct iwn_ops *ops = &sc->ops; 5597 struct iwn_node *wn = (void *)ni; 5598 struct iwn_node_info node; 5599 int error, qid; 5600 5601 /* Enable TX for the specified RA/TID. */ 5602 wn->disable_tid &= ~(1 << tid); 5603 memset(&node, 0, sizeof node); 5604 node.id = wn->id; 5605 node.control = IWN_NODE_UPDATE; 5606 node.flags = IWN_FLAG_SET_DISABLE_TID; 5607 node.disable_tid = htole16(wn->disable_tid); 5608 error = ops->add_node(sc, &node, 1); 5609 if (error != 0) 5610 return 0; 5611 5612 if ((error = iwn_nic_lock(sc)) != 0) 5613 return 0; 5614 qid = *(int *)tap->txa_private; 5615 ops->ampdu_tx_start(sc, ni, qid, tid, tap->txa_start & 0xfff); 5616 iwn_nic_unlock(sc); 5617 5618 iwn_set_link_quality(sc, ni); 5619 return 1; 5620 } 5621 5622 static void 5623 iwn_ampdu_tx_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap) 5624 { 5625 struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc; 5626 struct iwn_ops *ops = &sc->ops; 5627 uint8_t tid = WME_AC_TO_TID(tap->txa_ac); 5628 int qid; 5629 5630 if (tap->txa_private == NULL) 5631 return; 5632 5633 qid = *(int *)tap->txa_private; 5634 if (iwn_nic_lock(sc) != 0) 5635 return; 5636 ops->ampdu_tx_stop(sc, qid, tid, tap->txa_start & 0xfff); 5637 iwn_nic_unlock(sc); 5638 sc->qid2tap[qid] = NULL; 5639 free(tap->txa_private, M_DEVBUF); 5640 tap->txa_private = NULL; 5641 } 5642 5643 static void 5644 iwn4965_ampdu_tx_start(struct iwn_softc *sc, struct ieee80211_node *ni, 5645 int qid, uint8_t tid, uint16_t ssn) 5646 { 5647 struct iwn_node *wn = (void *)ni; 5648 5649 /* Stop TX scheduler while we're changing its configuration. */ 5650 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid), 5651 IWN4965_TXQ_STATUS_CHGACT); 5652 5653 /* Assign RA/TID translation to the queue. */ 5654 iwn_mem_write_2(sc, sc->sched_base + IWN4965_SCHED_TRANS_TBL(qid), 5655 wn->id << 4 | tid); 5656 5657 /* Enable chain-building mode for the queue. */ 5658 iwn_prph_setbits(sc, IWN4965_SCHED_QCHAIN_SEL, 1 << qid); 5659 5660 /* Set starting sequence number from the ADDBA request. */ 5661 sc->txq[qid].cur = sc->txq[qid].read = (ssn & 0xff); 5662 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | (ssn & 0xff)); 5663 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), ssn); 5664 5665 /* Set scheduler window size. */ 5666 iwn_mem_write(sc, sc->sched_base + IWN4965_SCHED_QUEUE_OFFSET(qid), 5667 IWN_SCHED_WINSZ); 5668 /* Set scheduler frame limit. */ 5669 iwn_mem_write(sc, sc->sched_base + IWN4965_SCHED_QUEUE_OFFSET(qid) + 4, 5670 IWN_SCHED_LIMIT << 16); 5671 5672 /* Enable interrupts for the queue. */ 5673 iwn_prph_setbits(sc, IWN4965_SCHED_INTR_MASK, 1 << qid); 5674 5675 /* Mark the queue as active. */ 5676 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid), 5677 IWN4965_TXQ_STATUS_ACTIVE | IWN4965_TXQ_STATUS_AGGR_ENA | 5678 iwn_tid2fifo[tid] << 1); 5679 } 5680 5681 static void 5682 iwn4965_ampdu_tx_stop(struct iwn_softc *sc, int qid, uint8_t tid, uint16_t ssn) 5683 { 5684 /* Stop TX scheduler while we're changing its configuration. */ 5685 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid), 5686 IWN4965_TXQ_STATUS_CHGACT); 5687 5688 /* Set starting sequence number from the ADDBA request. */ 5689 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | (ssn & 0xff)); 5690 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), ssn); 5691 5692 /* Disable interrupts for the queue. */ 5693 iwn_prph_clrbits(sc, IWN4965_SCHED_INTR_MASK, 1 << qid); 5694 5695 /* Mark the queue as inactive. */ 5696 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid), 5697 IWN4965_TXQ_STATUS_INACTIVE | iwn_tid2fifo[tid] << 1); 5698 } 5699 5700 static void 5701 iwn5000_ampdu_tx_start(struct iwn_softc *sc, struct ieee80211_node *ni, 5702 int qid, uint8_t tid, uint16_t ssn) 5703 { 5704 struct iwn_node *wn = (void *)ni; 5705 5706 /* Stop TX scheduler while we're changing its configuration. */ 5707 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid), 5708 IWN5000_TXQ_STATUS_CHGACT); 5709 5710 /* Assign RA/TID translation to the queue. */ 5711 iwn_mem_write_2(sc, sc->sched_base + IWN5000_SCHED_TRANS_TBL(qid), 5712 wn->id << 4 | tid); 5713 5714 /* Enable chain-building mode for the queue. */ 5715 iwn_prph_setbits(sc, IWN5000_SCHED_QCHAIN_SEL, 1 << qid); 5716 5717 /* Enable aggregation for the queue. */ 5718 iwn_prph_setbits(sc, IWN5000_SCHED_AGGR_SEL, 1 << qid); 5719 5720 /* Set starting sequence number from the ADDBA request. */ 5721 sc->txq[qid].cur = sc->txq[qid].read = (ssn & 0xff); 5722 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | (ssn & 0xff)); 5723 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), ssn); 5724 5725 /* Set scheduler window size and frame limit. */ 5726 iwn_mem_write(sc, sc->sched_base + IWN5000_SCHED_QUEUE_OFFSET(qid) + 4, 5727 IWN_SCHED_LIMIT << 16 | IWN_SCHED_WINSZ); 5728 5729 /* Enable interrupts for the queue. */ 5730 iwn_prph_setbits(sc, IWN5000_SCHED_INTR_MASK, 1 << qid); 5731 5732 /* Mark the queue as active. */ 5733 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid), 5734 IWN5000_TXQ_STATUS_ACTIVE | iwn_tid2fifo[tid]); 5735 } 5736 5737 static void 5738 iwn5000_ampdu_tx_stop(struct iwn_softc *sc, int qid, uint8_t tid, uint16_t ssn) 5739 { 5740 /* Stop TX scheduler while we're changing its configuration. */ 5741 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid), 5742 IWN5000_TXQ_STATUS_CHGACT); 5743 5744 /* Disable aggregation for the queue. */ 5745 iwn_prph_clrbits(sc, IWN5000_SCHED_AGGR_SEL, 1 << qid); 5746 5747 /* Set starting sequence number from the ADDBA request. */ 5748 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | (ssn & 0xff)); 5749 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), ssn); 5750 5751 /* Disable interrupts for the queue. */ 5752 iwn_prph_clrbits(sc, IWN5000_SCHED_INTR_MASK, 1 << qid); 5753 5754 /* Mark the queue as inactive. */ 5755 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid), 5756 IWN5000_TXQ_STATUS_INACTIVE | iwn_tid2fifo[tid]); 5757 } 5758 5759 /* 5760 * Query calibration tables from the initialization firmware. We do this 5761 * only once at first boot. Called from a process context. 5762 */ 5763 static int 5764 iwn5000_query_calibration(struct iwn_softc *sc) 5765 { 5766 struct iwn5000_calib_config cmd; 5767 int error; 5768 5769 memset(&cmd, 0, sizeof cmd); 5770 cmd.ucode.once.enable = 0xffffffff; 5771 cmd.ucode.once.start = 0xffffffff; 5772 cmd.ucode.once.send = 0xffffffff; 5773 cmd.ucode.flags = 0xffffffff; 5774 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: sending calibration query\n", 5775 __func__); 5776 error = iwn_cmd(sc, IWN5000_CMD_CALIB_CONFIG, &cmd, sizeof cmd, 0); 5777 if (error != 0) 5778 return error; 5779 5780 /* Wait at most two seconds for calibration to complete. */ 5781 if (!(sc->sc_flags & IWN_FLAG_CALIB_DONE)) 5782 error = msleep(sc, &sc->sc_mtx, PCATCH, "iwncal", 2 * hz); 5783 return error; 5784 } 5785 5786 /* 5787 * Send calibration results to the runtime firmware. These results were 5788 * obtained on first boot from the initialization firmware. 5789 */ 5790 static int 5791 iwn5000_send_calibration(struct iwn_softc *sc) 5792 { 5793 int idx, error; 5794 5795 for (idx = 0; idx < 5; idx++) { 5796 if (sc->calibcmd[idx].buf == NULL) 5797 continue; /* No results available. */ 5798 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 5799 "send calibration result idx=%d len=%d\n", idx, 5800 sc->calibcmd[idx].len); 5801 error = iwn_cmd(sc, IWN_CMD_PHY_CALIB, sc->calibcmd[idx].buf, 5802 sc->calibcmd[idx].len, 0); 5803 if (error != 0) { 5804 device_printf(sc->sc_dev, 5805 "%s: could not send calibration result, error %d\n", 5806 __func__, error); 5807 return error; 5808 } 5809 } 5810 return 0; 5811 } 5812 5813 static int 5814 iwn5000_send_wimax_coex(struct iwn_softc *sc) 5815 { 5816 struct iwn5000_wimax_coex wimax; 5817 5818 #ifdef notyet 5819 if (sc->hw_type == IWN_HW_REV_TYPE_6050) { 5820 /* Enable WiMAX coexistence for combo adapters. */ 5821 wimax.flags = 5822 IWN_WIMAX_COEX_ASSOC_WA_UNMASK | 5823 IWN_WIMAX_COEX_UNASSOC_WA_UNMASK | 5824 IWN_WIMAX_COEX_STA_TABLE_VALID | 5825 IWN_WIMAX_COEX_ENABLE; 5826 memcpy(wimax.events, iwn6050_wimax_events, 5827 sizeof iwn6050_wimax_events); 5828 } else 5829 #endif 5830 { 5831 /* Disable WiMAX coexistence. */ 5832 wimax.flags = 0; 5833 memset(wimax.events, 0, sizeof wimax.events); 5834 } 5835 DPRINTF(sc, IWN_DEBUG_RESET, "%s: Configuring WiMAX coexistence\n", 5836 __func__); 5837 return iwn_cmd(sc, IWN5000_CMD_WIMAX_COEX, &wimax, sizeof wimax, 0); 5838 } 5839 5840 static int 5841 iwn5000_crystal_calib(struct iwn_softc *sc) 5842 { 5843 struct iwn5000_phy_calib_crystal cmd; 5844 5845 memset(&cmd, 0, sizeof cmd); 5846 cmd.code = IWN5000_PHY_CALIB_CRYSTAL; 5847 cmd.ngroups = 1; 5848 cmd.isvalid = 1; 5849 cmd.cap_pin[0] = le32toh(sc->eeprom_crystal) & 0xff; 5850 cmd.cap_pin[1] = (le32toh(sc->eeprom_crystal) >> 16) & 0xff; 5851 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "sending crystal calibration %d, %d\n", 5852 cmd.cap_pin[0], cmd.cap_pin[1]); 5853 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 0); 5854 } 5855 5856 static int 5857 iwn5000_temp_offset_calib(struct iwn_softc *sc) 5858 { 5859 struct iwn5000_phy_calib_temp_offset cmd; 5860 5861 memset(&cmd, 0, sizeof cmd); 5862 cmd.code = IWN5000_PHY_CALIB_TEMP_OFFSET; 5863 cmd.ngroups = 1; 5864 cmd.isvalid = 1; 5865 if (sc->eeprom_temp != 0) 5866 cmd.offset = htole16(sc->eeprom_temp); 5867 else 5868 cmd.offset = htole16(IWN_DEFAULT_TEMP_OFFSET); 5869 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "setting radio sensor offset to %d\n", 5870 le16toh(cmd.offset)); 5871 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 0); 5872 } 5873 5874 /* 5875 * This function is called after the runtime firmware notifies us of its 5876 * readiness (called in a process context). 5877 */ 5878 static int 5879 iwn4965_post_alive(struct iwn_softc *sc) 5880 { 5881 int error, qid; 5882 5883 if ((error = iwn_nic_lock(sc)) != 0) 5884 return error; 5885 5886 /* Clear TX scheduler state in SRAM. */ 5887 sc->sched_base = iwn_prph_read(sc, IWN_SCHED_SRAM_ADDR); 5888 iwn_mem_set_region_4(sc, sc->sched_base + IWN4965_SCHED_CTX_OFF, 0, 5889 IWN4965_SCHED_CTX_LEN / sizeof (uint32_t)); 5890 5891 /* Set physical address of TX scheduler rings (1KB aligned). */ 5892 iwn_prph_write(sc, IWN4965_SCHED_DRAM_ADDR, sc->sched_dma.paddr >> 10); 5893 5894 IWN_SETBITS(sc, IWN_FH_TX_CHICKEN, IWN_FH_TX_CHICKEN_SCHED_RETRY); 5895 5896 /* Disable chain mode for all our 16 queues. */ 5897 iwn_prph_write(sc, IWN4965_SCHED_QCHAIN_SEL, 0); 5898 5899 for (qid = 0; qid < IWN4965_NTXQUEUES; qid++) { 5900 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), 0); 5901 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | 0); 5902 5903 /* Set scheduler window size. */ 5904 iwn_mem_write(sc, sc->sched_base + 5905 IWN4965_SCHED_QUEUE_OFFSET(qid), IWN_SCHED_WINSZ); 5906 /* Set scheduler frame limit. */ 5907 iwn_mem_write(sc, sc->sched_base + 5908 IWN4965_SCHED_QUEUE_OFFSET(qid) + 4, 5909 IWN_SCHED_LIMIT << 16); 5910 } 5911 5912 /* Enable interrupts for all our 16 queues. */ 5913 iwn_prph_write(sc, IWN4965_SCHED_INTR_MASK, 0xffff); 5914 /* Identify TX FIFO rings (0-7). */ 5915 iwn_prph_write(sc, IWN4965_SCHED_TXFACT, 0xff); 5916 5917 /* Mark TX rings (4 EDCA + cmd + 2 HCCA) as active. */ 5918 for (qid = 0; qid < 7; qid++) { 5919 static uint8_t qid2fifo[] = { 3, 2, 1, 0, 4, 5, 6 }; 5920 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid), 5921 IWN4965_TXQ_STATUS_ACTIVE | qid2fifo[qid] << 1); 5922 } 5923 iwn_nic_unlock(sc); 5924 return 0; 5925 } 5926 5927 /* 5928 * This function is called after the initialization or runtime firmware 5929 * notifies us of its readiness (called in a process context). 5930 */ 5931 static int 5932 iwn5000_post_alive(struct iwn_softc *sc) 5933 { 5934 int error, qid; 5935 5936 /* Switch to using ICT interrupt mode. */ 5937 iwn5000_ict_reset(sc); 5938 5939 if ((error = iwn_nic_lock(sc)) != 0) 5940 return error; 5941 5942 /* Clear TX scheduler state in SRAM. */ 5943 sc->sched_base = iwn_prph_read(sc, IWN_SCHED_SRAM_ADDR); 5944 iwn_mem_set_region_4(sc, sc->sched_base + IWN5000_SCHED_CTX_OFF, 0, 5945 IWN5000_SCHED_CTX_LEN / sizeof (uint32_t)); 5946 5947 /* Set physical address of TX scheduler rings (1KB aligned). */ 5948 iwn_prph_write(sc, IWN5000_SCHED_DRAM_ADDR, sc->sched_dma.paddr >> 10); 5949 5950 IWN_SETBITS(sc, IWN_FH_TX_CHICKEN, IWN_FH_TX_CHICKEN_SCHED_RETRY); 5951 5952 /* Enable chain mode for all queues, except command queue. */ 5953 iwn_prph_write(sc, IWN5000_SCHED_QCHAIN_SEL, 0xfffef); 5954 iwn_prph_write(sc, IWN5000_SCHED_AGGR_SEL, 0); 5955 5956 for (qid = 0; qid < IWN5000_NTXQUEUES; qid++) { 5957 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), 0); 5958 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | 0); 5959 5960 iwn_mem_write(sc, sc->sched_base + 5961 IWN5000_SCHED_QUEUE_OFFSET(qid), 0); 5962 /* Set scheduler window size and frame limit. */ 5963 iwn_mem_write(sc, sc->sched_base + 5964 IWN5000_SCHED_QUEUE_OFFSET(qid) + 4, 5965 IWN_SCHED_LIMIT << 16 | IWN_SCHED_WINSZ); 5966 } 5967 5968 /* Enable interrupts for all our 20 queues. */ 5969 iwn_prph_write(sc, IWN5000_SCHED_INTR_MASK, 0xfffff); 5970 /* Identify TX FIFO rings (0-7). */ 5971 iwn_prph_write(sc, IWN5000_SCHED_TXFACT, 0xff); 5972 5973 /* Mark TX rings (4 EDCA + cmd + 2 HCCA) as active. */ 5974 for (qid = 0; qid < 7; qid++) { 5975 static uint8_t qid2fifo[] = { 3, 2, 1, 0, 7, 5, 6 }; 5976 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid), 5977 IWN5000_TXQ_STATUS_ACTIVE | qid2fifo[qid]); 5978 } 5979 iwn_nic_unlock(sc); 5980 5981 /* Configure WiMAX coexistence for combo adapters. */ 5982 error = iwn5000_send_wimax_coex(sc); 5983 if (error != 0) { 5984 device_printf(sc->sc_dev, 5985 "%s: could not configure WiMAX coexistence, error %d\n", 5986 __func__, error); 5987 return error; 5988 } 5989 if (sc->hw_type != IWN_HW_REV_TYPE_5150) { 5990 /* Perform crystal calibration. */ 5991 error = iwn5000_crystal_calib(sc); 5992 if (error != 0) { 5993 device_printf(sc->sc_dev, 5994 "%s: crystal calibration failed, error %d\n", 5995 __func__, error); 5996 return error; 5997 } 5998 } 5999 if (!(sc->sc_flags & IWN_FLAG_CALIB_DONE)) { 6000 /* Query calibration from the initialization firmware. */ 6001 if ((error = iwn5000_query_calibration(sc)) != 0) { 6002 device_printf(sc->sc_dev, 6003 "%s: could not query calibration, error %d\n", 6004 __func__, error); 6005 return error; 6006 } 6007 /* 6008 * We have the calibration results now, reboot with the 6009 * runtime firmware (call ourselves recursively!) 6010 */ 6011 iwn_hw_stop(sc); 6012 error = iwn_hw_init(sc); 6013 } else { 6014 /* Send calibration results to runtime firmware. */ 6015 error = iwn5000_send_calibration(sc); 6016 } 6017 return error; 6018 } 6019 6020 /* 6021 * The firmware boot code is small and is intended to be copied directly into 6022 * the NIC internal memory (no DMA transfer). 6023 */ 6024 static int 6025 iwn4965_load_bootcode(struct iwn_softc *sc, const uint8_t *ucode, int size) 6026 { 6027 int error, ntries; 6028 6029 size /= sizeof (uint32_t); 6030 6031 if ((error = iwn_nic_lock(sc)) != 0) 6032 return error; 6033 6034 /* Copy microcode image into NIC memory. */ 6035 iwn_prph_write_region_4(sc, IWN_BSM_SRAM_BASE, 6036 (const uint32_t *)ucode, size); 6037 6038 iwn_prph_write(sc, IWN_BSM_WR_MEM_SRC, 0); 6039 iwn_prph_write(sc, IWN_BSM_WR_MEM_DST, IWN_FW_TEXT_BASE); 6040 iwn_prph_write(sc, IWN_BSM_WR_DWCOUNT, size); 6041 6042 /* Start boot load now. */ 6043 iwn_prph_write(sc, IWN_BSM_WR_CTRL, IWN_BSM_WR_CTRL_START); 6044 6045 /* Wait for transfer to complete. */ 6046 for (ntries = 0; ntries < 1000; ntries++) { 6047 if (!(iwn_prph_read(sc, IWN_BSM_WR_CTRL) & 6048 IWN_BSM_WR_CTRL_START)) 6049 break; 6050 DELAY(10); 6051 } 6052 if (ntries == 1000) { 6053 device_printf(sc->sc_dev, "%s: could not load boot firmware\n", 6054 __func__); 6055 iwn_nic_unlock(sc); 6056 return ETIMEDOUT; 6057 } 6058 6059 /* Enable boot after power up. */ 6060 iwn_prph_write(sc, IWN_BSM_WR_CTRL, IWN_BSM_WR_CTRL_START_EN); 6061 6062 iwn_nic_unlock(sc); 6063 return 0; 6064 } 6065 6066 static int 6067 iwn4965_load_firmware(struct iwn_softc *sc) 6068 { 6069 struct iwn_fw_info *fw = &sc->fw; 6070 struct iwn_dma_info *dma = &sc->fw_dma; 6071 int error; 6072 6073 /* Copy initialization sections into pre-allocated DMA-safe memory. */ 6074 memcpy(dma->vaddr, fw->init.data, fw->init.datasz); 6075 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 6076 memcpy(dma->vaddr + IWN4965_FW_DATA_MAXSZ, 6077 fw->init.text, fw->init.textsz); 6078 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 6079 6080 /* Tell adapter where to find initialization sections. */ 6081 if ((error = iwn_nic_lock(sc)) != 0) 6082 return error; 6083 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_ADDR, dma->paddr >> 4); 6084 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_SIZE, fw->init.datasz); 6085 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_ADDR, 6086 (dma->paddr + IWN4965_FW_DATA_MAXSZ) >> 4); 6087 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_SIZE, fw->init.textsz); 6088 iwn_nic_unlock(sc); 6089 6090 /* Load firmware boot code. */ 6091 error = iwn4965_load_bootcode(sc, fw->boot.text, fw->boot.textsz); 6092 if (error != 0) { 6093 device_printf(sc->sc_dev, "%s: could not load boot firmware\n", 6094 __func__); 6095 return error; 6096 } 6097 /* Now press "execute". */ 6098 IWN_WRITE(sc, IWN_RESET, 0); 6099 6100 /* Wait at most one second for first alive notification. */ 6101 if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", hz)) != 0) { 6102 device_printf(sc->sc_dev, 6103 "%s: timeout waiting for adapter to initialize, error %d\n", 6104 __func__, error); 6105 return error; 6106 } 6107 6108 /* Retrieve current temperature for initial TX power calibration. */ 6109 sc->rawtemp = sc->ucode_info.temp[3].chan20MHz; 6110 sc->temp = iwn4965_get_temperature(sc); 6111 6112 /* Copy runtime sections into pre-allocated DMA-safe memory. */ 6113 memcpy(dma->vaddr, fw->main.data, fw->main.datasz); 6114 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 6115 memcpy(dma->vaddr + IWN4965_FW_DATA_MAXSZ, 6116 fw->main.text, fw->main.textsz); 6117 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 6118 6119 /* Tell adapter where to find runtime sections. */ 6120 if ((error = iwn_nic_lock(sc)) != 0) 6121 return error; 6122 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_ADDR, dma->paddr >> 4); 6123 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_SIZE, fw->main.datasz); 6124 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_ADDR, 6125 (dma->paddr + IWN4965_FW_DATA_MAXSZ) >> 4); 6126 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_SIZE, 6127 IWN_FW_UPDATED | fw->main.textsz); 6128 iwn_nic_unlock(sc); 6129 6130 return 0; 6131 } 6132 6133 static int 6134 iwn5000_load_firmware_section(struct iwn_softc *sc, uint32_t dst, 6135 const uint8_t *section, int size) 6136 { 6137 struct iwn_dma_info *dma = &sc->fw_dma; 6138 int error; 6139 6140 /* Copy firmware section into pre-allocated DMA-safe memory. */ 6141 memcpy(dma->vaddr, section, size); 6142 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 6143 6144 if ((error = iwn_nic_lock(sc)) != 0) 6145 return error; 6146 6147 IWN_WRITE(sc, IWN_FH_TX_CONFIG(IWN_SRVC_DMACHNL), 6148 IWN_FH_TX_CONFIG_DMA_PAUSE); 6149 6150 IWN_WRITE(sc, IWN_FH_SRAM_ADDR(IWN_SRVC_DMACHNL), dst); 6151 IWN_WRITE(sc, IWN_FH_TFBD_CTRL0(IWN_SRVC_DMACHNL), 6152 IWN_LOADDR(dma->paddr)); 6153 IWN_WRITE(sc, IWN_FH_TFBD_CTRL1(IWN_SRVC_DMACHNL), 6154 IWN_HIADDR(dma->paddr) << 28 | size); 6155 IWN_WRITE(sc, IWN_FH_TXBUF_STATUS(IWN_SRVC_DMACHNL), 6156 IWN_FH_TXBUF_STATUS_TBNUM(1) | 6157 IWN_FH_TXBUF_STATUS_TBIDX(1) | 6158 IWN_FH_TXBUF_STATUS_TFBD_VALID); 6159 6160 /* Kick Flow Handler to start DMA transfer. */ 6161 IWN_WRITE(sc, IWN_FH_TX_CONFIG(IWN_SRVC_DMACHNL), 6162 IWN_FH_TX_CONFIG_DMA_ENA | IWN_FH_TX_CONFIG_CIRQ_HOST_ENDTFD); 6163 6164 iwn_nic_unlock(sc); 6165 6166 /* Wait at most five seconds for FH DMA transfer to complete. */ 6167 return msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", 5 * hz); 6168 } 6169 6170 static int 6171 iwn5000_load_firmware(struct iwn_softc *sc) 6172 { 6173 struct iwn_fw_part *fw; 6174 int error; 6175 6176 /* Load the initialization firmware on first boot only. */ 6177 fw = (sc->sc_flags & IWN_FLAG_CALIB_DONE) ? 6178 &sc->fw.main : &sc->fw.init; 6179 6180 error = iwn5000_load_firmware_section(sc, IWN_FW_TEXT_BASE, 6181 fw->text, fw->textsz); 6182 if (error != 0) { 6183 device_printf(sc->sc_dev, 6184 "%s: could not load firmware %s section, error %d\n", 6185 __func__, ".text", error); 6186 return error; 6187 } 6188 error = iwn5000_load_firmware_section(sc, IWN_FW_DATA_BASE, 6189 fw->data, fw->datasz); 6190 if (error != 0) { 6191 device_printf(sc->sc_dev, 6192 "%s: could not load firmware %s section, error %d\n", 6193 __func__, ".data", error); 6194 return error; 6195 } 6196 6197 /* Now press "execute". */ 6198 IWN_WRITE(sc, IWN_RESET, 0); 6199 return 0; 6200 } 6201 6202 /* 6203 * Extract text and data sections from a legacy firmware image. 6204 */ 6205 static int 6206 iwn_read_firmware_leg(struct iwn_softc *sc, struct iwn_fw_info *fw) 6207 { 6208 const uint32_t *ptr; 6209 size_t hdrlen = 24; 6210 uint32_t rev; 6211 6212 ptr = (const uint32_t *)fw->data; 6213 rev = le32toh(*ptr++); 6214 6215 /* Check firmware API version. */ 6216 if (IWN_FW_API(rev) <= 1) { 6217 device_printf(sc->sc_dev, 6218 "%s: bad firmware, need API version >=2\n", __func__); 6219 return EINVAL; 6220 } 6221 if (IWN_FW_API(rev) >= 3) { 6222 /* Skip build number (version 2 header). */ 6223 hdrlen += 4; 6224 ptr++; 6225 } 6226 if (fw->size < hdrlen) { 6227 device_printf(sc->sc_dev, "%s: firmware too short: %zu bytes\n", 6228 __func__, fw->size); 6229 return EINVAL; 6230 } 6231 fw->main.textsz = le32toh(*ptr++); 6232 fw->main.datasz = le32toh(*ptr++); 6233 fw->init.textsz = le32toh(*ptr++); 6234 fw->init.datasz = le32toh(*ptr++); 6235 fw->boot.textsz = le32toh(*ptr++); 6236 6237 /* Check that all firmware sections fit. */ 6238 if (fw->size < hdrlen + fw->main.textsz + fw->main.datasz + 6239 fw->init.textsz + fw->init.datasz + fw->boot.textsz) { 6240 device_printf(sc->sc_dev, "%s: firmware too short: %zu bytes\n", 6241 __func__, fw->size); 6242 return EINVAL; 6243 } 6244 6245 /* Get pointers to firmware sections. */ 6246 fw->main.text = (const uint8_t *)ptr; 6247 fw->main.data = fw->main.text + fw->main.textsz; 6248 fw->init.text = fw->main.data + fw->main.datasz; 6249 fw->init.data = fw->init.text + fw->init.textsz; 6250 fw->boot.text = fw->init.data + fw->init.datasz; 6251 return 0; 6252 } 6253 6254 /* 6255 * Extract text and data sections from a TLV firmware image. 6256 */ 6257 static int 6258 iwn_read_firmware_tlv(struct iwn_softc *sc, struct iwn_fw_info *fw, 6259 uint16_t alt) 6260 { 6261 const struct iwn_fw_tlv_hdr *hdr; 6262 const struct iwn_fw_tlv *tlv; 6263 const uint8_t *ptr, *end; 6264 uint64_t altmask; 6265 uint32_t len, tmp; 6266 6267 if (fw->size < sizeof (*hdr)) { 6268 device_printf(sc->sc_dev, "%s: firmware too short: %zu bytes\n", 6269 __func__, fw->size); 6270 return EINVAL; 6271 } 6272 hdr = (const struct iwn_fw_tlv_hdr *)fw->data; 6273 if (hdr->signature != htole32(IWN_FW_SIGNATURE)) { 6274 device_printf(sc->sc_dev, "%s: bad firmware signature 0x%08x\n", 6275 __func__, le32toh(hdr->signature)); 6276 return EINVAL; 6277 } 6278 DPRINTF(sc, IWN_DEBUG_RESET, "FW: \"%.64s\", build 0x%x\n", hdr->descr, 6279 le32toh(hdr->build)); 6280 6281 /* 6282 * Select the closest supported alternative that is less than 6283 * or equal to the specified one. 6284 */ 6285 altmask = le64toh(hdr->altmask); 6286 while (alt > 0 && !(altmask & (1ULL << alt))) 6287 alt--; /* Downgrade. */ 6288 DPRINTF(sc, IWN_DEBUG_RESET, "using alternative %d\n", alt); 6289 6290 ptr = (const uint8_t *)(hdr + 1); 6291 end = (const uint8_t *)(fw->data + fw->size); 6292 6293 /* Parse type-length-value fields. */ 6294 while (ptr + sizeof (*tlv) <= end) { 6295 tlv = (const struct iwn_fw_tlv *)ptr; 6296 len = le32toh(tlv->len); 6297 6298 ptr += sizeof (*tlv); 6299 if (ptr + len > end) { 6300 device_printf(sc->sc_dev, 6301 "%s: firmware too short: %zu bytes\n", __func__, 6302 fw->size); 6303 return EINVAL; 6304 } 6305 /* Skip other alternatives. */ 6306 if (tlv->alt != 0 && tlv->alt != htole16(alt)) 6307 goto next; 6308 6309 switch (le16toh(tlv->type)) { 6310 case IWN_FW_TLV_MAIN_TEXT: 6311 fw->main.text = ptr; 6312 fw->main.textsz = len; 6313 break; 6314 case IWN_FW_TLV_MAIN_DATA: 6315 fw->main.data = ptr; 6316 fw->main.datasz = len; 6317 break; 6318 case IWN_FW_TLV_INIT_TEXT: 6319 fw->init.text = ptr; 6320 fw->init.textsz = len; 6321 break; 6322 case IWN_FW_TLV_INIT_DATA: 6323 fw->init.data = ptr; 6324 fw->init.datasz = len; 6325 break; 6326 case IWN_FW_TLV_BOOT_TEXT: 6327 fw->boot.text = ptr; 6328 fw->boot.textsz = len; 6329 break; 6330 case IWN_FW_TLV_ENH_SENS: 6331 if (!len) 6332 sc->sc_flags |= IWN_FLAG_ENH_SENS; 6333 break; 6334 case IWN_FW_TLV_PHY_CALIB: 6335 tmp = htole32(*ptr); 6336 if (tmp < 253) { 6337 sc->reset_noise_gain = tmp; 6338 sc->noise_gain = tmp + 1; 6339 } 6340 break; 6341 default: 6342 DPRINTF(sc, IWN_DEBUG_RESET, 6343 "TLV type %d not handled\n", le16toh(tlv->type)); 6344 break; 6345 } 6346 next: /* TLV fields are 32-bit aligned. */ 6347 ptr += (len + 3) & ~3; 6348 } 6349 return 0; 6350 } 6351 6352 static int 6353 iwn_read_firmware(struct iwn_softc *sc) 6354 { 6355 struct iwn_fw_info *fw = &sc->fw; 6356 int error; 6357 6358 IWN_UNLOCK(sc); 6359 6360 memset(fw, 0, sizeof (*fw)); 6361 6362 /* Read firmware image from filesystem. */ 6363 sc->fw_fp = firmware_get(sc->fwname); 6364 if (sc->fw_fp == NULL) { 6365 device_printf(sc->sc_dev, "%s: could not read firmware %s\n", 6366 __func__, sc->fwname); 6367 IWN_LOCK(sc); 6368 return EINVAL; 6369 } 6370 IWN_LOCK(sc); 6371 6372 fw->size = sc->fw_fp->datasize; 6373 fw->data = (const uint8_t *)sc->fw_fp->data; 6374 if (fw->size < sizeof (uint32_t)) { 6375 device_printf(sc->sc_dev, "%s: firmware too short: %zu bytes\n", 6376 __func__, fw->size); 6377 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD); 6378 sc->fw_fp = NULL; 6379 return EINVAL; 6380 } 6381 6382 /* Retrieve text and data sections. */ 6383 if (*(const uint32_t *)fw->data != 0) /* Legacy image. */ 6384 error = iwn_read_firmware_leg(sc, fw); 6385 else 6386 error = iwn_read_firmware_tlv(sc, fw, 1); 6387 if (error != 0) { 6388 device_printf(sc->sc_dev, 6389 "%s: could not read firmware sections, error %d\n", 6390 __func__, error); 6391 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD); 6392 sc->fw_fp = NULL; 6393 return error; 6394 } 6395 6396 /* Make sure text and data sections fit in hardware memory. */ 6397 if (fw->main.textsz > sc->fw_text_maxsz || 6398 fw->main.datasz > sc->fw_data_maxsz || 6399 fw->init.textsz > sc->fw_text_maxsz || 6400 fw->init.datasz > sc->fw_data_maxsz || 6401 fw->boot.textsz > IWN_FW_BOOT_TEXT_MAXSZ || 6402 (fw->boot.textsz & 3) != 0) { 6403 device_printf(sc->sc_dev, "%s: firmware sections too large\n", 6404 __func__); 6405 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD); 6406 sc->fw_fp = NULL; 6407 return EINVAL; 6408 } 6409 6410 /* We can proceed with loading the firmware. */ 6411 return 0; 6412 } 6413 6414 static int 6415 iwn_clock_wait(struct iwn_softc *sc) 6416 { 6417 int ntries; 6418 6419 /* Set "initialization complete" bit. */ 6420 IWN_SETBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_INIT_DONE); 6421 6422 /* Wait for clock stabilization. */ 6423 for (ntries = 0; ntries < 2500; ntries++) { 6424 if (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_MAC_CLOCK_READY) 6425 return 0; 6426 DELAY(10); 6427 } 6428 device_printf(sc->sc_dev, 6429 "%s: timeout waiting for clock stabilization\n", __func__); 6430 return ETIMEDOUT; 6431 } 6432 6433 static int 6434 iwn_apm_init(struct iwn_softc *sc) 6435 { 6436 uint32_t reg; 6437 int error; 6438 6439 /* Disable L0s exit timer (NMI bug workaround). */ 6440 IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_DIS_L0S_TIMER); 6441 /* Don't wait for ICH L0s (ICH bug workaround). */ 6442 IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_L1A_NO_L0S_RX); 6443 6444 /* Set FH wait threshold to max (HW bug under stress workaround). */ 6445 IWN_SETBITS(sc, IWN_DBG_HPET_MEM, 0xffff0000); 6446 6447 /* Enable HAP INTA to move adapter from L1a to L0s. */ 6448 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_HAP_WAKE_L1A); 6449 6450 /* Retrieve PCIe Active State Power Management (ASPM). */ 6451 reg = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1); 6452 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */ 6453 if (reg & 0x02) /* L1 Entry enabled. */ 6454 IWN_SETBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA); 6455 else 6456 IWN_CLRBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA); 6457 6458 if (sc->hw_type != IWN_HW_REV_TYPE_4965 && 6459 sc->hw_type <= IWN_HW_REV_TYPE_1000) 6460 IWN_SETBITS(sc, IWN_ANA_PLL, IWN_ANA_PLL_INIT); 6461 6462 /* Wait for clock stabilization before accessing prph. */ 6463 if ((error = iwn_clock_wait(sc)) != 0) 6464 return error; 6465 6466 if ((error = iwn_nic_lock(sc)) != 0) 6467 return error; 6468 if (sc->hw_type == IWN_HW_REV_TYPE_4965) { 6469 /* Enable DMA and BSM (Bootstrap State Machine). */ 6470 iwn_prph_write(sc, IWN_APMG_CLK_EN, 6471 IWN_APMG_CLK_CTRL_DMA_CLK_RQT | 6472 IWN_APMG_CLK_CTRL_BSM_CLK_RQT); 6473 } else { 6474 /* Enable DMA. */ 6475 iwn_prph_write(sc, IWN_APMG_CLK_EN, 6476 IWN_APMG_CLK_CTRL_DMA_CLK_RQT); 6477 } 6478 DELAY(20); 6479 /* Disable L1-Active. */ 6480 iwn_prph_setbits(sc, IWN_APMG_PCI_STT, IWN_APMG_PCI_STT_L1A_DIS); 6481 iwn_nic_unlock(sc); 6482 6483 return 0; 6484 } 6485 6486 static void 6487 iwn_apm_stop_master(struct iwn_softc *sc) 6488 { 6489 int ntries; 6490 6491 /* Stop busmaster DMA activity. */ 6492 IWN_SETBITS(sc, IWN_RESET, IWN_RESET_STOP_MASTER); 6493 for (ntries = 0; ntries < 100; ntries++) { 6494 if (IWN_READ(sc, IWN_RESET) & IWN_RESET_MASTER_DISABLED) 6495 return; 6496 DELAY(10); 6497 } 6498 device_printf(sc->sc_dev, "%s: timeout waiting for master\n", __func__); 6499 } 6500 6501 static void 6502 iwn_apm_stop(struct iwn_softc *sc) 6503 { 6504 iwn_apm_stop_master(sc); 6505 6506 /* Reset the entire device. */ 6507 IWN_SETBITS(sc, IWN_RESET, IWN_RESET_SW); 6508 DELAY(10); 6509 /* Clear "initialization complete" bit. */ 6510 IWN_CLRBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_INIT_DONE); 6511 } 6512 6513 static int 6514 iwn4965_nic_config(struct iwn_softc *sc) 6515 { 6516 if (IWN_RFCFG_TYPE(sc->rfcfg) == 1) { 6517 /* 6518 * I don't believe this to be correct but this is what the 6519 * vendor driver is doing. Probably the bits should not be 6520 * shifted in IWN_RFCFG_*. 6521 */ 6522 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, 6523 IWN_RFCFG_TYPE(sc->rfcfg) | 6524 IWN_RFCFG_STEP(sc->rfcfg) | 6525 IWN_RFCFG_DASH(sc->rfcfg)); 6526 } 6527 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, 6528 IWN_HW_IF_CONFIG_RADIO_SI | IWN_HW_IF_CONFIG_MAC_SI); 6529 return 0; 6530 } 6531 6532 static int 6533 iwn5000_nic_config(struct iwn_softc *sc) 6534 { 6535 uint32_t tmp; 6536 int error; 6537 6538 if (IWN_RFCFG_TYPE(sc->rfcfg) < 3) { 6539 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, 6540 IWN_RFCFG_TYPE(sc->rfcfg) | 6541 IWN_RFCFG_STEP(sc->rfcfg) | 6542 IWN_RFCFG_DASH(sc->rfcfg)); 6543 } 6544 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, 6545 IWN_HW_IF_CONFIG_RADIO_SI | IWN_HW_IF_CONFIG_MAC_SI); 6546 6547 if ((error = iwn_nic_lock(sc)) != 0) 6548 return error; 6549 iwn_prph_setbits(sc, IWN_APMG_PS, IWN_APMG_PS_EARLY_PWROFF_DIS); 6550 6551 if (sc->hw_type == IWN_HW_REV_TYPE_1000) { 6552 /* 6553 * Select first Switching Voltage Regulator (1.32V) to 6554 * solve a stability issue related to noisy DC2DC line 6555 * in the silicon of 1000 Series. 6556 */ 6557 tmp = iwn_prph_read(sc, IWN_APMG_DIGITAL_SVR); 6558 tmp &= ~IWN_APMG_DIGITAL_SVR_VOLTAGE_MASK; 6559 tmp |= IWN_APMG_DIGITAL_SVR_VOLTAGE_1_32; 6560 iwn_prph_write(sc, IWN_APMG_DIGITAL_SVR, tmp); 6561 } 6562 iwn_nic_unlock(sc); 6563 6564 if (sc->sc_flags & IWN_FLAG_INTERNAL_PA) { 6565 /* Use internal power amplifier only. */ 6566 IWN_WRITE(sc, IWN_GP_DRIVER, IWN_GP_DRIVER_RADIO_2X2_IPA); 6567 } 6568 if ((sc->hw_type == IWN_HW_REV_TYPE_6050 || 6569 sc->hw_type == IWN_HW_REV_TYPE_6005) && sc->calib_ver >= 6) { 6570 /* Indicate that ROM calibration version is >=6. */ 6571 IWN_SETBITS(sc, IWN_GP_DRIVER, IWN_GP_DRIVER_CALIB_VER6); 6572 } 6573 if (sc->hw_type == IWN_HW_REV_TYPE_6005) 6574 IWN_SETBITS(sc, IWN_GP_DRIVER, IWN_GP_DRIVER_6050_1X2); 6575 return 0; 6576 } 6577 6578 /* 6579 * Take NIC ownership over Intel Active Management Technology (AMT). 6580 */ 6581 static int 6582 iwn_hw_prepare(struct iwn_softc *sc) 6583 { 6584 int ntries; 6585 6586 /* Check if hardware is ready. */ 6587 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_NIC_READY); 6588 for (ntries = 0; ntries < 5; ntries++) { 6589 if (IWN_READ(sc, IWN_HW_IF_CONFIG) & 6590 IWN_HW_IF_CONFIG_NIC_READY) 6591 return 0; 6592 DELAY(10); 6593 } 6594 6595 /* Hardware not ready, force into ready state. */ 6596 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_PREPARE); 6597 for (ntries = 0; ntries < 15000; ntries++) { 6598 if (!(IWN_READ(sc, IWN_HW_IF_CONFIG) & 6599 IWN_HW_IF_CONFIG_PREPARE_DONE)) 6600 break; 6601 DELAY(10); 6602 } 6603 if (ntries == 15000) 6604 return ETIMEDOUT; 6605 6606 /* Hardware should be ready now. */ 6607 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_NIC_READY); 6608 for (ntries = 0; ntries < 5; ntries++) { 6609 if (IWN_READ(sc, IWN_HW_IF_CONFIG) & 6610 IWN_HW_IF_CONFIG_NIC_READY) 6611 return 0; 6612 DELAY(10); 6613 } 6614 return ETIMEDOUT; 6615 } 6616 6617 static int 6618 iwn_hw_init(struct iwn_softc *sc) 6619 { 6620 struct iwn_ops *ops = &sc->ops; 6621 int error, chnl, qid; 6622 6623 /* Clear pending interrupts. */ 6624 IWN_WRITE(sc, IWN_INT, 0xffffffff); 6625 6626 if ((error = iwn_apm_init(sc)) != 0) { 6627 device_printf(sc->sc_dev, 6628 "%s: could not power ON adapter, error %d\n", __func__, 6629 error); 6630 return error; 6631 } 6632 6633 /* Select VMAIN power source. */ 6634 if ((error = iwn_nic_lock(sc)) != 0) 6635 return error; 6636 iwn_prph_clrbits(sc, IWN_APMG_PS, IWN_APMG_PS_PWR_SRC_MASK); 6637 iwn_nic_unlock(sc); 6638 6639 /* Perform adapter-specific initialization. */ 6640 if ((error = ops->nic_config(sc)) != 0) 6641 return error; 6642 6643 /* Initialize RX ring. */ 6644 if ((error = iwn_nic_lock(sc)) != 0) 6645 return error; 6646 IWN_WRITE(sc, IWN_FH_RX_CONFIG, 0); 6647 IWN_WRITE(sc, IWN_FH_RX_WPTR, 0); 6648 /* Set physical address of RX ring (256-byte aligned). */ 6649 IWN_WRITE(sc, IWN_FH_RX_BASE, sc->rxq.desc_dma.paddr >> 8); 6650 /* Set physical address of RX status (16-byte aligned). */ 6651 IWN_WRITE(sc, IWN_FH_STATUS_WPTR, sc->rxq.stat_dma.paddr >> 4); 6652 /* Enable RX. */ 6653 IWN_WRITE(sc, IWN_FH_RX_CONFIG, 6654 IWN_FH_RX_CONFIG_ENA | 6655 IWN_FH_RX_CONFIG_IGN_RXF_EMPTY | /* HW bug workaround */ 6656 IWN_FH_RX_CONFIG_IRQ_DST_HOST | 6657 IWN_FH_RX_CONFIG_SINGLE_FRAME | 6658 IWN_FH_RX_CONFIG_RB_TIMEOUT(0) | 6659 IWN_FH_RX_CONFIG_NRBD(IWN_RX_RING_COUNT_LOG)); 6660 iwn_nic_unlock(sc); 6661 IWN_WRITE(sc, IWN_FH_RX_WPTR, (IWN_RX_RING_COUNT - 1) & ~7); 6662 6663 if ((error = iwn_nic_lock(sc)) != 0) 6664 return error; 6665 6666 /* Initialize TX scheduler. */ 6667 iwn_prph_write(sc, sc->sched_txfact_addr, 0); 6668 6669 /* Set physical address of "keep warm" page (16-byte aligned). */ 6670 IWN_WRITE(sc, IWN_FH_KW_ADDR, sc->kw_dma.paddr >> 4); 6671 6672 /* Initialize TX rings. */ 6673 for (qid = 0; qid < sc->ntxqs; qid++) { 6674 struct iwn_tx_ring *txq = &sc->txq[qid]; 6675 6676 /* Set physical address of TX ring (256-byte aligned). */ 6677 IWN_WRITE(sc, IWN_FH_CBBC_QUEUE(qid), 6678 txq->desc_dma.paddr >> 8); 6679 } 6680 iwn_nic_unlock(sc); 6681 6682 /* Enable DMA channels. */ 6683 for (chnl = 0; chnl < sc->ndmachnls; chnl++) { 6684 IWN_WRITE(sc, IWN_FH_TX_CONFIG(chnl), 6685 IWN_FH_TX_CONFIG_DMA_ENA | 6686 IWN_FH_TX_CONFIG_DMA_CREDIT_ENA); 6687 } 6688 6689 /* Clear "radio off" and "commands blocked" bits. */ 6690 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL); 6691 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_CMD_BLOCKED); 6692 6693 /* Clear pending interrupts. */ 6694 IWN_WRITE(sc, IWN_INT, 0xffffffff); 6695 /* Enable interrupt coalescing. */ 6696 IWN_WRITE(sc, IWN_INT_COALESCING, 512 / 8); 6697 /* Enable interrupts. */ 6698 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask); 6699 6700 /* _Really_ make sure "radio off" bit is cleared! */ 6701 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL); 6702 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL); 6703 6704 /* Enable shadow registers. */ 6705 if (sc->hw_type >= IWN_HW_REV_TYPE_6000) 6706 IWN_SETBITS(sc, IWN_SHADOW_REG_CTRL, 0x800fffff); 6707 6708 if ((error = ops->load_firmware(sc)) != 0) { 6709 device_printf(sc->sc_dev, 6710 "%s: could not load firmware, error %d\n", __func__, 6711 error); 6712 return error; 6713 } 6714 /* Wait at most one second for firmware alive notification. */ 6715 if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", hz)) != 0) { 6716 device_printf(sc->sc_dev, 6717 "%s: timeout waiting for adapter to initialize, error %d\n", 6718 __func__, error); 6719 return error; 6720 } 6721 /* Do post-firmware initialization. */ 6722 return ops->post_alive(sc); 6723 } 6724 6725 static void 6726 iwn_hw_stop(struct iwn_softc *sc) 6727 { 6728 int chnl, qid, ntries; 6729 6730 IWN_WRITE(sc, IWN_RESET, IWN_RESET_NEVO); 6731 6732 /* Disable interrupts. */ 6733 IWN_WRITE(sc, IWN_INT_MASK, 0); 6734 IWN_WRITE(sc, IWN_INT, 0xffffffff); 6735 IWN_WRITE(sc, IWN_FH_INT, 0xffffffff); 6736 sc->sc_flags &= ~IWN_FLAG_USE_ICT; 6737 6738 /* Make sure we no longer hold the NIC lock. */ 6739 iwn_nic_unlock(sc); 6740 6741 /* Stop TX scheduler. */ 6742 iwn_prph_write(sc, sc->sched_txfact_addr, 0); 6743 6744 /* Stop all DMA channels. */ 6745 if (iwn_nic_lock(sc) == 0) { 6746 for (chnl = 0; chnl < sc->ndmachnls; chnl++) { 6747 IWN_WRITE(sc, IWN_FH_TX_CONFIG(chnl), 0); 6748 for (ntries = 0; ntries < 200; ntries++) { 6749 if (IWN_READ(sc, IWN_FH_TX_STATUS) & 6750 IWN_FH_TX_STATUS_IDLE(chnl)) 6751 break; 6752 DELAY(10); 6753 } 6754 } 6755 iwn_nic_unlock(sc); 6756 } 6757 6758 /* Stop RX ring. */ 6759 iwn_reset_rx_ring(sc, &sc->rxq); 6760 6761 /* Reset all TX rings. */ 6762 for (qid = 0; qid < sc->ntxqs; qid++) 6763 iwn_reset_tx_ring(sc, &sc->txq[qid]); 6764 6765 if (iwn_nic_lock(sc) == 0) { 6766 iwn_prph_write(sc, IWN_APMG_CLK_DIS, 6767 IWN_APMG_CLK_CTRL_DMA_CLK_RQT); 6768 iwn_nic_unlock(sc); 6769 } 6770 DELAY(5); 6771 /* Power OFF adapter. */ 6772 iwn_apm_stop(sc); 6773 } 6774 6775 static void 6776 iwn_radio_on(void *arg0, int pending) 6777 { 6778 struct iwn_softc *sc = arg0; 6779 struct ifnet *ifp = sc->sc_ifp; 6780 struct ieee80211com *ic = ifp->if_l2com; 6781 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 6782 6783 if (vap != NULL) { 6784 iwn_init(sc); 6785 ieee80211_init(vap); 6786 } 6787 } 6788 6789 static void 6790 iwn_radio_off(void *arg0, int pending) 6791 { 6792 struct iwn_softc *sc = arg0; 6793 struct ifnet *ifp = sc->sc_ifp; 6794 struct ieee80211com *ic = ifp->if_l2com; 6795 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 6796 6797 iwn_stop(sc); 6798 if (vap != NULL) 6799 ieee80211_stop(vap); 6800 6801 /* Enable interrupts to get RF toggle notification. */ 6802 IWN_LOCK(sc); 6803 IWN_WRITE(sc, IWN_INT, 0xffffffff); 6804 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask); 6805 IWN_UNLOCK(sc); 6806 } 6807 6808 static void 6809 iwn_init_locked(struct iwn_softc *sc) 6810 { 6811 struct ifnet *ifp = sc->sc_ifp; 6812 int error; 6813 6814 IWN_LOCK_ASSERT(sc); 6815 6816 if ((error = iwn_hw_prepare(sc)) != 0) { 6817 device_printf(sc->sc_dev, "%s: hardware not ready, error %d\n", 6818 __func__, error); 6819 goto fail; 6820 } 6821 6822 /* Initialize interrupt mask to default value. */ 6823 sc->int_mask = IWN_INT_MASK_DEF; 6824 sc->sc_flags &= ~IWN_FLAG_USE_ICT; 6825 6826 /* Check that the radio is not disabled by hardware switch. */ 6827 if (!(IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL)) { 6828 device_printf(sc->sc_dev, 6829 "radio is disabled by hardware switch\n"); 6830 /* Enable interrupts to get RF toggle notifications. */ 6831 IWN_WRITE(sc, IWN_INT, 0xffffffff); 6832 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask); 6833 return; 6834 } 6835 6836 /* Read firmware images from the filesystem. */ 6837 if ((error = iwn_read_firmware(sc)) != 0) { 6838 device_printf(sc->sc_dev, 6839 "%s: could not read firmware, error %d\n", __func__, 6840 error); 6841 goto fail; 6842 } 6843 6844 /* Initialize hardware and upload firmware. */ 6845 error = iwn_hw_init(sc); 6846 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD); 6847 sc->fw_fp = NULL; 6848 if (error != 0) { 6849 device_printf(sc->sc_dev, 6850 "%s: could not initialize hardware, error %d\n", __func__, 6851 error); 6852 goto fail; 6853 } 6854 6855 /* Configure adapter now that it is ready. */ 6856 if ((error = iwn_config(sc)) != 0) { 6857 device_printf(sc->sc_dev, 6858 "%s: could not configure device, error %d\n", __func__, 6859 error); 6860 goto fail; 6861 } 6862 6863 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 6864 ifp->if_drv_flags |= IFF_DRV_RUNNING; 6865 6866 callout_reset(&sc->watchdog_to, hz, iwn_watchdog, sc); 6867 return; 6868 6869 fail: iwn_stop_locked(sc); 6870 } 6871 6872 static void 6873 iwn_init(void *arg) 6874 { 6875 struct iwn_softc *sc = arg; 6876 struct ifnet *ifp = sc->sc_ifp; 6877 struct ieee80211com *ic = ifp->if_l2com; 6878 6879 IWN_LOCK(sc); 6880 iwn_init_locked(sc); 6881 IWN_UNLOCK(sc); 6882 6883 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 6884 ieee80211_start_all(ic); 6885 } 6886 6887 static void 6888 iwn_stop_locked(struct iwn_softc *sc) 6889 { 6890 struct ifnet *ifp = sc->sc_ifp; 6891 6892 IWN_LOCK_ASSERT(sc); 6893 6894 sc->sc_tx_timer = 0; 6895 callout_stop(&sc->watchdog_to); 6896 callout_stop(&sc->calib_to); 6897 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 6898 6899 /* Power OFF hardware. */ 6900 iwn_hw_stop(sc); 6901 } 6902 6903 static void 6904 iwn_stop(struct iwn_softc *sc) 6905 { 6906 IWN_LOCK(sc); 6907 iwn_stop_locked(sc); 6908 IWN_UNLOCK(sc); 6909 } 6910 6911 /* 6912 * Callback from net80211 to start a scan. 6913 */ 6914 static void 6915 iwn_scan_start(struct ieee80211com *ic) 6916 { 6917 struct ifnet *ifp = ic->ic_ifp; 6918 struct iwn_softc *sc = ifp->if_softc; 6919 6920 IWN_LOCK(sc); 6921 /* make the link LED blink while we're scanning */ 6922 iwn_set_led(sc, IWN_LED_LINK, 20, 2); 6923 IWN_UNLOCK(sc); 6924 } 6925 6926 /* 6927 * Callback from net80211 to terminate a scan. 6928 */ 6929 static void 6930 iwn_scan_end(struct ieee80211com *ic) 6931 { 6932 struct ifnet *ifp = ic->ic_ifp; 6933 struct iwn_softc *sc = ifp->if_softc; 6934 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 6935 6936 IWN_LOCK(sc); 6937 if (vap->iv_state == IEEE80211_S_RUN) { 6938 /* Set link LED to ON status if we are associated */ 6939 iwn_set_led(sc, IWN_LED_LINK, 0, 1); 6940 } 6941 IWN_UNLOCK(sc); 6942 } 6943 6944 /* 6945 * Callback from net80211 to force a channel change. 6946 */ 6947 static void 6948 iwn_set_channel(struct ieee80211com *ic) 6949 { 6950 const struct ieee80211_channel *c = ic->ic_curchan; 6951 struct ifnet *ifp = ic->ic_ifp; 6952 struct iwn_softc *sc = ifp->if_softc; 6953 6954 IWN_LOCK(sc); 6955 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq); 6956 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags); 6957 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq); 6958 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags); 6959 IWN_UNLOCK(sc); 6960 } 6961 6962 /* 6963 * Callback from net80211 to start scanning of the current channel. 6964 */ 6965 static void 6966 iwn_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 6967 { 6968 struct ieee80211vap *vap = ss->ss_vap; 6969 struct iwn_softc *sc = vap->iv_ic->ic_ifp->if_softc; 6970 int error; 6971 6972 IWN_LOCK(sc); 6973 error = iwn_scan(sc); 6974 IWN_UNLOCK(sc); 6975 if (error != 0) 6976 ieee80211_cancel_scan(vap); 6977 } 6978 6979 /* 6980 * Callback from net80211 to handle the minimum dwell time being met. 6981 * The intent is to terminate the scan but we just let the firmware 6982 * notify us when it's finished as we have no safe way to abort it. 6983 */ 6984 static void 6985 iwn_scan_mindwell(struct ieee80211_scan_state *ss) 6986 { 6987 /* NB: don't try to abort scan; wait for firmware to finish */ 6988 } 6989 6990 static void 6991 iwn_hw_reset(void *arg0, int pending) 6992 { 6993 struct iwn_softc *sc = arg0; 6994 struct ifnet *ifp = sc->sc_ifp; 6995 struct ieee80211com *ic = ifp->if_l2com; 6996 6997 iwn_stop(sc); 6998 iwn_init(sc); 6999 ieee80211_notify_radio(ic, 1); 7000 } 7001