2 * Copyright (c) 2007-2009
3 * Damien Bergamini <damien.bergamini@free.fr>
5 * Benjamin Close <benjsc@FreeBSD.org>
6 * Copyright (c) 2008 Sam Leffler, Errno Consulting
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.
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.
22 * Driver for Intel WiFi Link 4965 and 1000/5000/6000 Series 802.11 network
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.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>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
48 #include <machine/clock.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.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>
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>
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_radiotap.h>
69 #include <net80211/ieee80211_regdomain.h>
70 #include <net80211/ieee80211_ratectl.h>
72 #include <dev/iwn/if_iwnreg.h>
73 #include <dev/iwn/if_iwnvar.h>
75 static int iwn_probe(device_t);
76 static int iwn_attach(device_t);
77 static const struct iwn_hal *iwn_hal_attach(struct iwn_softc *);
78 static void iwn_radiotap_attach(struct iwn_softc *);
79 static struct ieee80211vap *iwn_vap_create(struct ieee80211com *,
80 const char name[IFNAMSIZ], int unit, int opmode,
81 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
82 const uint8_t mac[IEEE80211_ADDR_LEN]);
83 static void iwn_vap_delete(struct ieee80211vap *);
84 static int iwn_cleanup(device_t);
85 static int iwn_detach(device_t);
86 static int iwn_nic_lock(struct iwn_softc *);
87 static int iwn_eeprom_lock(struct iwn_softc *);
88 static int iwn_init_otprom(struct iwn_softc *);
89 static int iwn_read_prom_data(struct iwn_softc *, uint32_t, void *, int);
90 static void iwn_dma_map_addr(void *, bus_dma_segment_t *, int, int);
91 static int iwn_dma_contig_alloc(struct iwn_softc *, struct iwn_dma_info *,
92 void **, bus_size_t, bus_size_t, int);
93 static void iwn_dma_contig_free(struct iwn_dma_info *);
94 static int iwn_alloc_sched(struct iwn_softc *);
95 static void iwn_free_sched(struct iwn_softc *);
96 static int iwn_alloc_kw(struct iwn_softc *);
97 static void iwn_free_kw(struct iwn_softc *);
98 static int iwn_alloc_ict(struct iwn_softc *);
99 static void iwn_free_ict(struct iwn_softc *);
100 static int iwn_alloc_fwmem(struct iwn_softc *);
101 static void iwn_free_fwmem(struct iwn_softc *);
102 static int iwn_alloc_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
103 static void iwn_reset_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
104 static void iwn_free_rx_ring(struct iwn_softc *, struct iwn_rx_ring *);
105 static int iwn_alloc_tx_ring(struct iwn_softc *, struct iwn_tx_ring *,
107 static void iwn_reset_tx_ring(struct iwn_softc *, struct iwn_tx_ring *);
108 static void iwn_free_tx_ring(struct iwn_softc *, struct iwn_tx_ring *);
109 static void iwn5000_ict_reset(struct iwn_softc *);
110 static int iwn_read_eeprom(struct iwn_softc *,
111 uint8_t macaddr[IEEE80211_ADDR_LEN]);
112 static void iwn4965_read_eeprom(struct iwn_softc *);
113 static void iwn4965_print_power_group(struct iwn_softc *, int);
114 static void iwn5000_read_eeprom(struct iwn_softc *);
115 static uint32_t iwn_eeprom_channel_flags(struct iwn_eeprom_chan *);
116 static void iwn_read_eeprom_band(struct iwn_softc *, int);
118 static void iwn_read_eeprom_ht40(struct iwn_softc *, int);
120 static void iwn_read_eeprom_channels(struct iwn_softc *, int,
122 static void iwn_read_eeprom_enhinfo(struct iwn_softc *);
123 static struct ieee80211_node *iwn_node_alloc(struct ieee80211vap *,
124 const uint8_t mac[IEEE80211_ADDR_LEN]);
125 static int iwn_media_change(struct ifnet *);
126 static int iwn_newstate(struct ieee80211vap *, enum ieee80211_state, int);
127 static void iwn_rx_phy(struct iwn_softc *, struct iwn_rx_desc *,
128 struct iwn_rx_data *);
129 static void iwn_timer_timeout(void *);
130 static void iwn_calib_reset(struct iwn_softc *);
131 static void iwn_rx_done(struct iwn_softc *, struct iwn_rx_desc *,
132 struct iwn_rx_data *);
134 static void iwn_rx_compressed_ba(struct iwn_softc *, struct iwn_rx_desc *,
135 struct iwn_rx_data *);
137 static void iwn_rx_statistics(struct iwn_softc *, struct iwn_rx_desc *,
138 struct iwn_rx_data *);
139 static void iwn4965_tx_done(struct iwn_softc *, struct iwn_rx_desc *,
140 struct iwn_rx_data *);
141 static void iwn5000_tx_done(struct iwn_softc *, struct iwn_rx_desc *,
142 struct iwn_rx_data *);
143 static void iwn_tx_done(struct iwn_softc *, struct iwn_rx_desc *, int,
145 static void iwn_cmd_done(struct iwn_softc *, struct iwn_rx_desc *);
146 static void iwn_notif_intr(struct iwn_softc *);
147 static void iwn_wakeup_intr(struct iwn_softc *);
148 static void iwn_rftoggle_intr(struct iwn_softc *);
149 static void iwn_fatal_intr(struct iwn_softc *);
150 static void iwn_intr(void *);
151 static void iwn4965_update_sched(struct iwn_softc *, int, int, uint8_t,
153 static void iwn5000_update_sched(struct iwn_softc *, int, int, uint8_t,
156 static void iwn5000_reset_sched(struct iwn_softc *, int, int);
158 static uint8_t iwn_plcp_signal(int);
159 static int iwn_tx_data(struct iwn_softc *, struct mbuf *,
160 struct ieee80211_node *, struct iwn_tx_ring *);
161 static int iwn_raw_xmit(struct ieee80211_node *, struct mbuf *,
162 const struct ieee80211_bpf_params *);
163 static void iwn_start(struct ifnet *);
164 static void iwn_start_locked(struct ifnet *);
165 static void iwn_watchdog(struct iwn_softc *sc);
166 static int iwn_ioctl(struct ifnet *, u_long, caddr_t);
167 static int iwn_cmd(struct iwn_softc *, int, const void *, int, int);
168 static int iwn4965_add_node(struct iwn_softc *, struct iwn_node_info *,
170 static int iwn5000_add_node(struct iwn_softc *, struct iwn_node_info *,
172 static int iwn_set_link_quality(struct iwn_softc *, uint8_t, int);
173 static int iwn_add_broadcast_node(struct iwn_softc *, int);
174 static int iwn_wme_update(struct ieee80211com *);
175 static void iwn_update_mcast(struct ifnet *);
176 static void iwn_set_led(struct iwn_softc *, uint8_t, uint8_t, uint8_t);
177 static int iwn_set_critical_temp(struct iwn_softc *);
178 static int iwn_set_timing(struct iwn_softc *, struct ieee80211_node *);
179 static void iwn4965_power_calibration(struct iwn_softc *, int);
180 static int iwn4965_set_txpower(struct iwn_softc *,
181 struct ieee80211_channel *, int);
182 static int iwn5000_set_txpower(struct iwn_softc *,
183 struct ieee80211_channel *, int);
184 static int iwn4965_get_rssi(struct iwn_softc *, struct iwn_rx_stat *);
185 static int iwn5000_get_rssi(struct iwn_softc *, struct iwn_rx_stat *);
186 static int iwn_get_noise(const struct iwn_rx_general_stats *);
187 static int iwn4965_get_temperature(struct iwn_softc *);
188 static int iwn5000_get_temperature(struct iwn_softc *);
189 static int iwn_init_sensitivity(struct iwn_softc *);
190 static void iwn_collect_noise(struct iwn_softc *,
191 const struct iwn_rx_general_stats *);
192 static int iwn4965_init_gains(struct iwn_softc *);
193 static int iwn5000_init_gains(struct iwn_softc *);
194 static int iwn4965_set_gains(struct iwn_softc *);
195 static int iwn5000_set_gains(struct iwn_softc *);
196 static void iwn_tune_sensitivity(struct iwn_softc *,
197 const struct iwn_rx_stats *);
198 static int iwn_send_sensitivity(struct iwn_softc *);
199 static int iwn_set_pslevel(struct iwn_softc *, int, int, int);
200 static int iwn_config(struct iwn_softc *);
201 static int iwn_scan(struct iwn_softc *);
202 static int iwn_auth(struct iwn_softc *, struct ieee80211vap *vap);
203 static int iwn_run(struct iwn_softc *, struct ieee80211vap *vap);
205 static int iwn_ampdu_rx_start(struct ieee80211com *,
206 struct ieee80211_node *, uint8_t);
207 static void iwn_ampdu_rx_stop(struct ieee80211com *,
208 struct ieee80211_node *, uint8_t);
209 static int iwn_ampdu_tx_start(struct ieee80211com *,
210 struct ieee80211_node *, uint8_t);
211 static void iwn_ampdu_tx_stop(struct ieee80211com *,
212 struct ieee80211_node *, uint8_t);
213 static void iwn4965_ampdu_tx_start(struct iwn_softc *,
214 struct ieee80211_node *, uint8_t, uint16_t);
215 static void iwn4965_ampdu_tx_stop(struct iwn_softc *, uint8_t, uint16_t);
216 static void iwn5000_ampdu_tx_start(struct iwn_softc *,
217 struct ieee80211_node *, uint8_t, uint16_t);
218 static void iwn5000_ampdu_tx_stop(struct iwn_softc *, uint8_t, uint16_t);
220 static int iwn5000_send_calib_results(struct iwn_softc *);
221 static int iwn5000_save_calib_result(struct iwn_softc *,
222 struct iwn_phy_calib *, int, int);
223 static void iwn5000_free_calib_results(struct iwn_softc *);
224 static int iwn5000_chrystal_calib(struct iwn_softc *);
225 static int iwn5000_send_calib_query(struct iwn_softc *, uint32_t);
226 static int iwn5000_rx_calib_result(struct iwn_softc *,
227 struct iwn_rx_desc *, struct iwn_rx_data *);
228 static int iwn5000_send_wimax_coex(struct iwn_softc *);
229 static int iwn4965_post_alive(struct iwn_softc *);
230 static int iwn5000_post_alive(struct iwn_softc *);
231 static int iwn4965_load_bootcode(struct iwn_softc *, const uint8_t *,
233 static int iwn4965_load_firmware(struct iwn_softc *);
234 static int iwn5000_load_firmware_section(struct iwn_softc *, uint32_t,
235 const uint8_t *, int);
236 static int iwn5000_load_firmware(struct iwn_softc *);
237 static int iwn_read_firmware_leg(struct iwn_softc *,
238 struct iwn_fw_info *);
239 static int iwn_read_firmware_tlv(struct iwn_softc *,
240 struct iwn_fw_info *, uint16_t);
241 static int iwn_read_firmware(struct iwn_softc *);
242 static int iwn_clock_wait(struct iwn_softc *);
243 static int iwn_apm_init(struct iwn_softc *);
244 static void iwn_apm_stop_master(struct iwn_softc *);
245 static void iwn_apm_stop(struct iwn_softc *);
246 static int iwn4965_nic_config(struct iwn_softc *);
247 static int iwn5000_nic_config(struct iwn_softc *);
248 static int iwn_hw_prepare(struct iwn_softc *);
249 static int iwn_hw_init(struct iwn_softc *);
250 static void iwn_hw_stop(struct iwn_softc *);
251 static void iwn_init_locked(struct iwn_softc *);
252 static void iwn_init(void *);
253 static void iwn_stop_locked(struct iwn_softc *);
254 static void iwn_stop(struct iwn_softc *);
255 static void iwn_scan_start(struct ieee80211com *);
256 static void iwn_scan_end(struct ieee80211com *);
257 static void iwn_set_channel(struct ieee80211com *);
258 static void iwn_scan_curchan(struct ieee80211_scan_state *, unsigned long);
259 static void iwn_scan_mindwell(struct ieee80211_scan_state *);
260 static struct iwn_eeprom_chan *iwn_find_eeprom_channel(struct iwn_softc *,
261 struct ieee80211_channel *);
262 static int iwn_setregdomain(struct ieee80211com *,
263 struct ieee80211_regdomain *, int,
264 struct ieee80211_channel []);
265 static void iwn_hw_reset(void *, int);
266 static void iwn_radio_on(void *, int);
267 static void iwn_radio_off(void *, int);
268 static void iwn_sysctlattach(struct iwn_softc *);
269 static int iwn_shutdown(device_t);
270 static int iwn_suspend(device_t);
271 static int iwn_resume(device_t);
276 IWN_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
277 IWN_DEBUG_RECV = 0x00000002, /* basic recv operation */
278 IWN_DEBUG_STATE = 0x00000004, /* 802.11 state transitions */
279 IWN_DEBUG_TXPOW = 0x00000008, /* tx power processing */
280 IWN_DEBUG_RESET = 0x00000010, /* reset processing */
281 IWN_DEBUG_OPS = 0x00000020, /* iwn_ops processing */
282 IWN_DEBUG_BEACON = 0x00000040, /* beacon handling */
283 IWN_DEBUG_WATCHDOG = 0x00000080, /* watchdog timeout */
284 IWN_DEBUG_INTR = 0x00000100, /* ISR */
285 IWN_DEBUG_CALIBRATE = 0x00000200, /* periodic calibration */
286 IWN_DEBUG_NODE = 0x00000400, /* node management */
287 IWN_DEBUG_LED = 0x00000800, /* led management */
288 IWN_DEBUG_CMD = 0x00001000, /* cmd submission */
289 IWN_DEBUG_FATAL = 0x80000000, /* fatal errors */
290 IWN_DEBUG_ANY = 0xffffffff
293 #define DPRINTF(sc, m, fmt, ...) do { \
294 if (sc->sc_debug & (m)) \
295 printf(fmt, __VA_ARGS__); \
298 static const char *iwn_intr_str(uint8_t);
300 #define DPRINTF(sc, m, fmt, ...) do { (void) sc; } while (0)
309 static const struct iwn_ident iwn_ident_table [] = {
310 { 0x8086, 0x4229, "Intel(R) PRO/Wireless 4965BGN" },
311 { 0x8086, 0x422D, "Intel(R) PRO/Wireless 4965BGN" },
312 { 0x8086, 0x4230, "Intel(R) PRO/Wireless 4965BGN" },
313 { 0x8086, 0x4233, "Intel(R) PRO/Wireless 4965BGN" },
314 { 0x8086, 0x4232, "Intel(R) PRO/Wireless 5100" },
315 { 0x8086, 0x4237, "Intel(R) PRO/Wireless 5100" },
316 { 0x8086, 0x423C, "Intel(R) PRO/Wireless 5150" },
317 { 0x8086, 0x423D, "Intel(R) PRO/Wireless 5150" },
318 { 0x8086, 0x4235, "Intel(R) PRO/Wireless 5300" },
319 { 0x8086, 0x4236, "Intel(R) PRO/Wireless 5300" },
320 { 0x8086, 0x423A, "Intel(R) PRO/Wireless 5350" },
321 { 0x8086, 0x423B, "Intel(R) PRO/Wireless 5350" },
322 { 0x8086, 0x0083, "Intel(R) PRO/Wireless 1000" },
323 { 0x8086, 0x0084, "Intel(R) PRO/Wireless 1000" },
324 { 0x8086, 0x008D, "Intel(R) PRO/Wireless 6000" },
325 { 0x8086, 0x008E, "Intel(R) PRO/Wireless 6000" },
326 { 0x8086, 0x4238, "Intel(R) PRO/Wireless 6000" },
327 { 0x8086, 0x4239, "Intel(R) PRO/Wireless 6000" },
328 { 0x8086, 0x422B, "Intel(R) PRO/Wireless 6000" },
329 { 0x8086, 0x422C, "Intel(R) PRO/Wireless 6000" },
330 { 0x8086, 0x0087, "Intel(R) PRO/Wireless 6250" },
331 { 0x8086, 0x0089, "Intel(R) PRO/Wireless 6250" },
332 { 0x8086, 0x0082, "Intel(R) PRO/Wireless 6205a" },
333 { 0x8086, 0x0085, "Intel(R) PRO/Wireless 6205a" },
335 { 0x8086, 0x008a, "Intel(R) PRO/Wireless 6205b" },
336 { 0x8086, 0x008b, "Intel(R) PRO/Wireless 6205b" },
337 { 0x8086, 0x008f, "Intel(R) PRO/Wireless 6205b" },
338 { 0x8086, 0x0090, "Intel(R) PRO/Wireless 6205b" },
339 { 0x8086, 0x0091, "Intel(R) PRO/Wireless 6205b" },
344 static const struct iwn_hal iwn4965_hal = {
345 iwn4965_load_firmware,
349 iwn4965_update_sched,
350 iwn4965_get_temperature,
358 iwn4965_ampdu_tx_start,
359 iwn4965_ampdu_tx_stop,
363 IWN4965_ID_BROADCAST,
366 IWN4965_FW_TEXT_MAXSZ,
367 IWN4965_FW_DATA_MAXSZ,
372 static const struct iwn_hal iwn5000_hal = {
373 iwn5000_load_firmware,
377 iwn5000_update_sched,
378 iwn5000_get_temperature,
386 iwn5000_ampdu_tx_start,
387 iwn5000_ampdu_tx_stop,
391 IWN5000_ID_BROADCAST,
394 IWN5000_FW_TEXT_MAXSZ,
395 IWN5000_FW_DATA_MAXSZ,
401 iwn_probe(device_t dev)
403 const struct iwn_ident *ident;
405 for (ident = iwn_ident_table; ident->name != NULL; ident++) {
406 if (pci_get_vendor(dev) == ident->vendor &&
407 pci_get_device(dev) == ident->device) {
408 device_set_desc(dev, ident->name);
416 iwn_attach(device_t dev)
418 struct iwn_softc *sc = (struct iwn_softc *)device_get_softc(dev);
419 struct ieee80211com *ic;
421 const struct iwn_hal *hal;
423 int i, error, result;
424 uint8_t macaddr[IEEE80211_ADDR_LEN];
429 * Get the offset of the PCI Express Capability Structure in PCI
430 * Configuration Space.
432 error = pci_find_extcap(dev, PCIY_EXPRESS, &sc->sc_cap_off);
434 device_printf(dev, "PCIe capability structure not found!\n");
438 /* Clear device-specific "PCI retry timeout" register (41h). */
439 pci_write_config(dev, 0x41, 0, 1);
441 /* Hardware bug workaround. */
442 tmp = pci_read_config(dev, PCIR_COMMAND, 1);
443 if (tmp & PCIM_CMD_INTxDIS) {
444 DPRINTF(sc, IWN_DEBUG_RESET, "%s: PCIe INTx Disable set\n",
446 tmp &= ~PCIM_CMD_INTxDIS;
447 pci_write_config(dev, PCIR_COMMAND, tmp, 1);
450 /* Enable bus-mastering. */
451 pci_enable_busmaster(dev);
453 sc->mem_rid = PCIR_BAR(0);
454 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
456 if (sc->mem == NULL ) {
457 device_printf(dev, "could not allocate memory resources\n");
462 sc->sc_st = rman_get_bustag(sc->mem);
463 sc->sc_sh = rman_get_bushandle(sc->mem);
465 if ((result = pci_msi_count(dev)) == 1 &&
466 pci_alloc_msi(dev, &result) == 0)
468 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
469 RF_ACTIVE | RF_SHAREABLE);
470 if (sc->irq == NULL) {
471 device_printf(dev, "could not allocate interrupt resource\n");
477 callout_init_mtx(&sc->sc_timer_to, &sc->sc_mtx, 0);
478 TASK_INIT(&sc->sc_reinit_task, 0, iwn_hw_reset, sc );
479 TASK_INIT(&sc->sc_radioon_task, 0, iwn_radio_on, sc );
480 TASK_INIT(&sc->sc_radiooff_task, 0, iwn_radio_off, sc );
482 /* Attach Hardware Abstraction Layer. */
483 hal = iwn_hal_attach(sc);
485 error = ENXIO; /* XXX: Wrong error code? */
489 error = iwn_hw_prepare(sc);
491 device_printf(dev, "hardware not ready, error %d\n", error);
495 /* Allocate DMA memory for firmware transfers. */
496 error = iwn_alloc_fwmem(sc);
499 "could not allocate memory for firmware, error %d\n",
504 /* Allocate "Keep Warm" page. */
505 error = iwn_alloc_kw(sc);
508 "could not allocate \"Keep Warm\" page, error %d\n", error);
512 /* Allocate ICT table for 5000 Series. */
513 if (sc->hw_type != IWN_HW_REV_TYPE_4965 &&
514 (error = iwn_alloc_ict(sc)) != 0) {
516 "%s: could not allocate ICT table, error %d\n",
521 /* Allocate TX scheduler "rings". */
522 error = iwn_alloc_sched(sc);
525 "could not allocate TX scheduler rings, error %d\n",
530 /* Allocate TX rings (16 on 4965AGN, 20 on 5000). */
531 for (i = 0; i < hal->ntxqs; i++) {
532 error = iwn_alloc_tx_ring(sc, &sc->txq[i], i);
535 "could not allocate Tx ring %d, error %d\n",
541 /* Allocate RX ring. */
542 error = iwn_alloc_rx_ring(sc, &sc->rxq);
545 "could not allocate Rx ring, error %d\n", error);
549 /* Clear pending interrupts. */
550 IWN_WRITE(sc, IWN_INT, 0xffffffff);
552 /* Count the number of available chains. */
554 ((sc->txchainmask >> 2) & 1) +
555 ((sc->txchainmask >> 1) & 1) +
556 ((sc->txchainmask >> 0) & 1);
558 ((sc->rxchainmask >> 2) & 1) +
559 ((sc->rxchainmask >> 1) & 1) +
560 ((sc->rxchainmask >> 0) & 1);
562 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
564 device_printf(dev, "can not allocate ifnet structure\n");
570 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
571 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
573 /* Set device capabilities. */
575 IEEE80211_C_STA /* station mode supported */
576 | IEEE80211_C_MONITOR /* monitor mode supported */
577 | IEEE80211_C_TXPMGT /* tx power management */
578 | IEEE80211_C_SHSLOT /* short slot time supported */
580 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
581 | IEEE80211_C_BGSCAN /* background scanning */
583 | IEEE80211_C_IBSS /* ibss/adhoc mode */
585 | IEEE80211_C_WME /* WME */
588 /* XXX disable until HT channel setup works */
590 IEEE80211_HTCAP_SMPS_ENA /* SM PS mode enabled */
591 | IEEE80211_HTCAP_CHWIDTH40 /* 40MHz channel width */
592 | IEEE80211_HTCAP_SHORTGI20 /* short GI in 20MHz */
593 | IEEE80211_HTCAP_SHORTGI40 /* short GI in 40MHz */
594 | IEEE80211_HTCAP_RXSTBC_2STREAM/* 1-2 spatial streams */
595 | IEEE80211_HTCAP_MAXAMSDU_3839 /* max A-MSDU length */
596 /* s/w capabilities */
597 | IEEE80211_HTC_HT /* HT operation */
598 | IEEE80211_HTC_AMPDU /* tx A-MPDU */
599 | IEEE80211_HTC_AMSDU /* tx A-MSDU */
602 /* Set HT capabilities. */
604 #if IWN_RBUF_SIZE == 8192
605 IEEE80211_HTCAP_AMSDU7935 |
607 IEEE80211_HTCAP_CBW20_40 |
608 IEEE80211_HTCAP_SGI20 |
609 IEEE80211_HTCAP_SGI40;
610 if (sc->hw_type != IWN_HW_REV_TYPE_4965)
611 ic->ic_htcaps |= IEEE80211_HTCAP_GF;
612 if (sc->hw_type == IWN_HW_REV_TYPE_6050)
613 ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_DYN;
615 ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_DIS;
618 /* Read MAC address, channels, etc from EEPROM. */
619 error = iwn_read_eeprom(sc, macaddr);
621 device_printf(dev, "could not read EEPROM, error %d\n",
626 device_printf(sc->sc_dev, "MIMO %dT%dR, %.4s, address %6D\n",
627 sc->ntxchains, sc->nrxchains, sc->eeprom_domain,
631 /* Set supported HT rates. */
632 ic->ic_sup_mcs[0] = 0xff;
633 if (sc->nrxchains > 1)
634 ic->ic_sup_mcs[1] = 0xff;
635 if (sc->nrxchains > 2)
636 ic->ic_sup_mcs[2] = 0xff;
639 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
641 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
642 ifp->if_init = iwn_init;
643 ifp->if_ioctl = iwn_ioctl;
644 ifp->if_start = iwn_start;
645 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
646 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
647 IFQ_SET_READY(&ifp->if_snd);
649 ieee80211_ifattach(ic, macaddr);
650 ic->ic_vap_create = iwn_vap_create;
651 ic->ic_vap_delete = iwn_vap_delete;
652 ic->ic_raw_xmit = iwn_raw_xmit;
653 ic->ic_node_alloc = iwn_node_alloc;
654 ic->ic_wme.wme_update = iwn_wme_update;
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;
663 ic->ic_ampdu_rx_start = iwn_ampdu_rx_start;
664 ic->ic_ampdu_rx_stop = iwn_ampdu_rx_stop;
665 ic->ic_ampdu_tx_start = iwn_ampdu_tx_start;
666 ic->ic_ampdu_tx_stop = iwn_ampdu_tx_stop;
669 iwn_radiotap_attach(sc);
670 iwn_sysctlattach(sc);
673 * Hook our interrupt after all initialization is complete.
675 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
676 NULL, iwn_intr, sc, &sc->sc_ih);
678 device_printf(dev, "could not set up interrupt, error %d\n",
683 ieee80211_announce(ic);
690 static const struct iwn_hal *
691 iwn_hal_attach(struct iwn_softc *sc)
693 sc->hw_type = (IWN_READ(sc, IWN_HW_REV) >> 4) & 0xf;
695 switch (sc->hw_type) {
696 case IWN_HW_REV_TYPE_4965:
697 sc->sc_hal = &iwn4965_hal;
698 sc->limits = &iwn4965_sensitivity_limits;
699 sc->fwname = "iwn4965fw";
700 sc->txchainmask = IWN_ANT_AB;
701 sc->rxchainmask = IWN_ANT_ABC;
703 case IWN_HW_REV_TYPE_5100:
704 sc->sc_hal = &iwn5000_hal;
705 sc->limits = &iwn5000_sensitivity_limits;
706 sc->fwname = "iwn5000fw";
707 sc->txchainmask = IWN_ANT_B;
708 sc->rxchainmask = IWN_ANT_AB;
709 sc->calib_init = IWN_CALIB_XTAL | IWN_CALIB_LO |
710 IWN_CALIB_TX_IQ | IWN_CALIB_TX_IQ_PERIODIC |
713 case IWN_HW_REV_TYPE_5150:
714 sc->sc_hal = &iwn5000_hal;
715 sc->limits = &iwn5150_sensitivity_limits;
716 sc->fwname = "iwn5150fw";
717 sc->txchainmask = IWN_ANT_A;
718 sc->rxchainmask = IWN_ANT_AB;
719 sc->calib_init = IWN_CALIB_DC | IWN_CALIB_LO |
720 IWN_CALIB_TX_IQ | IWN_CALIB_BASE_BAND;
722 case IWN_HW_REV_TYPE_5300:
723 case IWN_HW_REV_TYPE_5350:
724 sc->sc_hal = &iwn5000_hal;
725 sc->limits = &iwn5000_sensitivity_limits;
726 sc->fwname = "iwn5000fw";
727 sc->txchainmask = IWN_ANT_ABC;
728 sc->rxchainmask = IWN_ANT_ABC;
729 sc->calib_init = IWN_CALIB_XTAL | IWN_CALIB_LO |
730 IWN_CALIB_TX_IQ | IWN_CALIB_TX_IQ_PERIODIC |
733 case IWN_HW_REV_TYPE_1000:
734 sc->sc_hal = &iwn5000_hal;
735 sc->limits = &iwn1000_sensitivity_limits;
736 sc->fwname = "iwn1000fw";
737 sc->txchainmask = IWN_ANT_A;
738 sc->rxchainmask = IWN_ANT_AB;
739 sc->calib_init = IWN_CALIB_XTAL | IWN_CALIB_LO |
740 IWN_CALIB_TX_IQ | IWN_CALIB_TX_IQ_PERIODIC |
743 case IWN_HW_REV_TYPE_6000:
744 sc->sc_hal = &iwn5000_hal;
745 sc->limits = &iwn6000_sensitivity_limits;
746 sc->fwname = "iwn6000fw";
747 switch (pci_get_device(sc->sc_dev)) {
750 sc->sc_flags |= IWN_FLAG_INTERNAL_PA;
751 sc->txchainmask = IWN_ANT_BC;
752 sc->rxchainmask = IWN_ANT_BC;
755 sc->txchainmask = IWN_ANT_ABC;
756 sc->rxchainmask = IWN_ANT_ABC;
757 sc->calib_runtime = IWN_CALIB_DC;
760 sc->calib_init = IWN_CALIB_XTAL | IWN_CALIB_LO |
761 IWN_CALIB_TX_IQ | IWN_CALIB_BASE_BAND;
763 case IWN_HW_REV_TYPE_6050:
764 sc->sc_hal = &iwn5000_hal;
765 sc->limits = &iwn6000_sensitivity_limits;
766 sc->fwname = "iwn6050fw";
767 sc->txchainmask = IWN_ANT_AB;
768 sc->rxchainmask = IWN_ANT_AB;
769 sc->calib_init = IWN_CALIB_XTAL | IWN_CALIB_LO |
770 IWN_CALIB_TX_IQ | IWN_CALIB_BASE_BAND;
771 sc->calib_runtime = IWN_CALIB_DC;
773 case IWN_HW_REV_TYPE_6005:
774 sc->sc_hal = &iwn5000_hal;
775 sc->limits = &iwn6000_sensitivity_limits;
776 sc->fwname = "iwn6005fw";
777 sc->txchainmask = IWN_ANT_AB;
778 sc->rxchainmask = IWN_ANT_AB;
779 sc->calib_init = IWN_CALIB_XTAL | IWN_CALIB_LO |
780 IWN_CALIB_TX_IQ | IWN_CALIB_BASE_BAND;
781 sc->calib_runtime = IWN_CALIB_DC;
784 device_printf(sc->sc_dev, "adapter type %d not supported\n",
792 * Attach the interface to 802.11 radiotap.
795 iwn_radiotap_attach(struct iwn_softc *sc)
797 struct ifnet *ifp = sc->sc_ifp;
798 struct ieee80211com *ic = ifp->if_l2com;
800 ieee80211_radiotap_attach(ic,
801 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
802 IWN_TX_RADIOTAP_PRESENT,
803 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
804 IWN_RX_RADIOTAP_PRESENT);
807 static struct ieee80211vap *
808 iwn_vap_create(struct ieee80211com *ic,
809 const char name[IFNAMSIZ], int unit, int opmode, int flags,
810 const uint8_t bssid[IEEE80211_ADDR_LEN],
811 const uint8_t mac[IEEE80211_ADDR_LEN])
814 struct ieee80211vap *vap;
816 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */
818 ivp = (struct iwn_vap *) malloc(sizeof(struct iwn_vap),
819 M_80211_VAP, M_NOWAIT | M_ZERO);
823 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac);
824 vap->iv_bmissthreshold = 10; /* override default */
825 /* Override with driver methods. */
826 ivp->iv_newstate = vap->iv_newstate;
827 vap->iv_newstate = iwn_newstate;
829 ieee80211_ratectl_init(vap);
830 /* Complete setup. */
831 ieee80211_vap_attach(vap, iwn_media_change, ieee80211_media_status);
832 ic->ic_opmode = opmode;
837 iwn_vap_delete(struct ieee80211vap *vap)
839 struct iwn_vap *ivp = IWN_VAP(vap);
841 ieee80211_ratectl_deinit(vap);
842 ieee80211_vap_detach(vap);
843 free(ivp, M_80211_VAP);
847 iwn_cleanup(device_t dev)
849 struct iwn_softc *sc = device_get_softc(dev);
850 struct ifnet *ifp = sc->sc_ifp;
851 struct ieee80211com *ic;
857 ieee80211_draintask(ic, &sc->sc_reinit_task);
858 ieee80211_draintask(ic, &sc->sc_radioon_task);
859 ieee80211_draintask(ic, &sc->sc_radiooff_task);
862 callout_drain(&sc->sc_timer_to);
863 ieee80211_ifdetach(ic);
866 iwn5000_free_calib_results(sc);
868 /* Free DMA resources. */
869 iwn_free_rx_ring(sc, &sc->rxq);
870 if (sc->sc_hal != NULL)
871 for (i = 0; i < sc->sc_hal->ntxqs; i++)
872 iwn_free_tx_ring(sc, &sc->txq[i]);
879 if (sc->irq != NULL) {
880 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
881 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
882 if (sc->irq_rid == 1)
883 pci_release_msi(dev);
887 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem);
892 IWN_LOCK_DESTROY(sc);
897 iwn_detach(device_t dev)
904 iwn_nic_lock(struct iwn_softc *sc)
908 /* Request exclusive access to NIC. */
909 IWN_SETBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_MAC_ACCESS_REQ);
911 /* Spin until we actually get the lock. */
912 for (ntries = 0; ntries < 1000; ntries++) {
913 if ((IWN_READ(sc, IWN_GP_CNTRL) &
914 (IWN_GP_CNTRL_MAC_ACCESS_ENA | IWN_GP_CNTRL_SLEEP)) ==
915 IWN_GP_CNTRL_MAC_ACCESS_ENA)
923 iwn_nic_unlock(struct iwn_softc *sc)
925 IWN_CLRBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_MAC_ACCESS_REQ);
928 static __inline uint32_t
929 iwn_prph_read(struct iwn_softc *sc, uint32_t addr)
931 IWN_WRITE(sc, IWN_PRPH_RADDR, IWN_PRPH_DWORD | addr);
932 IWN_BARRIER_READ_WRITE(sc);
933 return IWN_READ(sc, IWN_PRPH_RDATA);
937 iwn_prph_write(struct iwn_softc *sc, uint32_t addr, uint32_t data)
939 IWN_WRITE(sc, IWN_PRPH_WADDR, IWN_PRPH_DWORD | addr);
940 IWN_BARRIER_WRITE(sc);
941 IWN_WRITE(sc, IWN_PRPH_WDATA, data);
945 iwn_prph_setbits(struct iwn_softc *sc, uint32_t addr, uint32_t mask)
947 iwn_prph_write(sc, addr, iwn_prph_read(sc, addr) | mask);
951 iwn_prph_clrbits(struct iwn_softc *sc, uint32_t addr, uint32_t mask)
953 iwn_prph_write(sc, addr, iwn_prph_read(sc, addr) & ~mask);
957 iwn_prph_write_region_4(struct iwn_softc *sc, uint32_t addr,
958 const uint32_t *data, int count)
960 for (; count > 0; count--, data++, addr += 4)
961 iwn_prph_write(sc, addr, *data);
964 static __inline uint32_t
965 iwn_mem_read(struct iwn_softc *sc, uint32_t addr)
967 IWN_WRITE(sc, IWN_MEM_RADDR, addr);
968 IWN_BARRIER_READ_WRITE(sc);
969 return IWN_READ(sc, IWN_MEM_RDATA);
973 iwn_mem_write(struct iwn_softc *sc, uint32_t addr, uint32_t data)
975 IWN_WRITE(sc, IWN_MEM_WADDR, addr);
976 IWN_BARRIER_WRITE(sc);
977 IWN_WRITE(sc, IWN_MEM_WDATA, data);
981 iwn_mem_write_2(struct iwn_softc *sc, uint32_t addr, uint16_t data)
985 tmp = iwn_mem_read(sc, addr & ~3);
987 tmp = (tmp & 0x0000ffff) | data << 16;
989 tmp = (tmp & 0xffff0000) | data;
990 iwn_mem_write(sc, addr & ~3, tmp);
994 iwn_mem_read_region_4(struct iwn_softc *sc, uint32_t addr, uint32_t *data,
997 for (; count > 0; count--, addr += 4)
998 *data++ = iwn_mem_read(sc, addr);
1001 static __inline void
1002 iwn_mem_set_region_4(struct iwn_softc *sc, uint32_t addr, uint32_t val,
1005 for (; count > 0; count--, addr += 4)
1006 iwn_mem_write(sc, addr, val);
1010 iwn_eeprom_lock(struct iwn_softc *sc)
1014 for (i = 0; i < 100; i++) {
1015 /* Request exclusive access to EEPROM. */
1016 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
1017 IWN_HW_IF_CONFIG_EEPROM_LOCKED);
1019 /* Spin until we actually get the lock. */
1020 for (ntries = 0; ntries < 100; ntries++) {
1021 if (IWN_READ(sc, IWN_HW_IF_CONFIG) &
1022 IWN_HW_IF_CONFIG_EEPROM_LOCKED)
1030 static __inline void
1031 iwn_eeprom_unlock(struct iwn_softc *sc)
1033 IWN_CLRBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_EEPROM_LOCKED);
1037 * Initialize access by host to One Time Programmable ROM.
1038 * NB: This kind of ROM can be found on 1000 or 6000 Series only.
1041 iwn_init_otprom(struct iwn_softc *sc)
1043 uint16_t prev, base, next;
1046 /* Wait for clock stabilization before accessing prph. */
1047 error = iwn_clock_wait(sc);
1051 error = iwn_nic_lock(sc);
1054 iwn_prph_setbits(sc, IWN_APMG_PS, IWN_APMG_PS_RESET_REQ);
1056 iwn_prph_clrbits(sc, IWN_APMG_PS, IWN_APMG_PS_RESET_REQ);
1059 /* Set auto clock gate disable bit for HW with OTP shadow RAM. */
1060 if (sc->hw_type != IWN_HW_REV_TYPE_1000) {
1061 IWN_SETBITS(sc, IWN_DBG_LINK_PWR_MGMT,
1062 IWN_RESET_LINK_PWR_MGMT_DIS);
1064 IWN_CLRBITS(sc, IWN_EEPROM_GP, IWN_EEPROM_GP_IF_OWNER);
1065 /* Clear ECC status. */
1066 IWN_SETBITS(sc, IWN_OTP_GP,
1067 IWN_OTP_GP_ECC_CORR_STTS | IWN_OTP_GP_ECC_UNCORR_STTS);
1070 * Find the block before last block (contains the EEPROM image)
1071 * for HW without OTP shadow RAM.
1073 if (sc->hw_type == IWN_HW_REV_TYPE_1000) {
1074 /* Switch to absolute addressing mode. */
1075 IWN_CLRBITS(sc, IWN_OTP_GP, IWN_OTP_GP_RELATIVE_ACCESS);
1077 for (count = 0; count < IWN1000_OTP_NBLOCKS; count++) {
1078 error = iwn_read_prom_data(sc, base, &next, 2);
1081 if (next == 0) /* End of linked-list. */
1084 base = le16toh(next);
1086 if (count == 0 || count == IWN1000_OTP_NBLOCKS)
1088 /* Skip "next" word. */
1089 sc->prom_base = prev + 1;
1095 iwn_read_prom_data(struct iwn_softc *sc, uint32_t addr, void *data, int count)
1099 uint8_t *out = data;
1101 addr += sc->prom_base;
1102 for (; count > 0; count -= 2, addr++) {
1103 IWN_WRITE(sc, IWN_EEPROM, addr << 2);
1104 for (ntries = 0; ntries < 10; ntries++) {
1105 val = IWN_READ(sc, IWN_EEPROM);
1106 if (val & IWN_EEPROM_READ_VALID)
1111 device_printf(sc->sc_dev,
1112 "timeout reading ROM at 0x%x\n", addr);
1115 if (sc->sc_flags & IWN_FLAG_HAS_OTPROM) {
1116 /* OTPROM, check for ECC errors. */
1117 tmp = IWN_READ(sc, IWN_OTP_GP);
1118 if (tmp & IWN_OTP_GP_ECC_UNCORR_STTS) {
1119 device_printf(sc->sc_dev,
1120 "OTPROM ECC error at 0x%x\n", addr);
1123 if (tmp & IWN_OTP_GP_ECC_CORR_STTS) {
1124 /* Correctable ECC error, clear bit. */
1125 IWN_SETBITS(sc, IWN_OTP_GP,
1126 IWN_OTP_GP_ECC_CORR_STTS);
1137 iwn_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1141 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
1142 *(bus_addr_t *)arg = segs[0].ds_addr;
1146 iwn_dma_contig_alloc(struct iwn_softc *sc, struct iwn_dma_info *dma,
1147 void **kvap, bus_size_t size, bus_size_t alignment, int flags)
1154 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment,
1155 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
1156 1, size, flags, NULL, NULL, &dma->tag);
1158 device_printf(sc->sc_dev,
1159 "%s: bus_dma_tag_create failed, error %d\n",
1163 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
1164 flags | BUS_DMA_ZERO, &dma->map);
1166 device_printf(sc->sc_dev,
1167 "%s: bus_dmamem_alloc failed, error %d\n", __func__, error);
1170 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr,
1171 size, iwn_dma_map_addr, &dma->paddr, flags);
1173 device_printf(sc->sc_dev,
1174 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
1182 iwn_dma_contig_free(dma);
1187 iwn_dma_contig_free(struct iwn_dma_info *dma)
1189 if (dma->tag != NULL) {
1190 if (dma->map != NULL) {
1191 if (dma->paddr == 0) {
1192 bus_dmamap_sync(dma->tag, dma->map,
1193 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
1194 bus_dmamap_unload(dma->tag, dma->map);
1196 bus_dmamem_free(dma->tag, &dma->vaddr, dma->map);
1198 bus_dma_tag_destroy(dma->tag);
1203 iwn_alloc_sched(struct iwn_softc *sc)
1205 /* TX scheduler rings must be aligned on a 1KB boundary. */
1206 return iwn_dma_contig_alloc(sc, &sc->sched_dma,
1207 (void **)&sc->sched, sc->sc_hal->schedsz, 1024, BUS_DMA_NOWAIT);
1211 iwn_free_sched(struct iwn_softc *sc)
1213 iwn_dma_contig_free(&sc->sched_dma);
1217 iwn_alloc_kw(struct iwn_softc *sc)
1219 /* "Keep Warm" page must be aligned on a 4KB boundary. */
1220 return iwn_dma_contig_alloc(sc, &sc->kw_dma, NULL, 4096, 4096,
1225 iwn_free_kw(struct iwn_softc *sc)
1227 iwn_dma_contig_free(&sc->kw_dma);
1231 iwn_alloc_ict(struct iwn_softc *sc)
1233 /* ICT table must be aligned on a 4KB boundary. */
1234 return iwn_dma_contig_alloc(sc, &sc->ict_dma,
1235 (void **)&sc->ict, IWN_ICT_SIZE, 4096, BUS_DMA_NOWAIT);
1239 iwn_free_ict(struct iwn_softc *sc)
1241 iwn_dma_contig_free(&sc->ict_dma);
1245 iwn_alloc_fwmem(struct iwn_softc *sc)
1247 /* Must be aligned on a 16-byte boundary. */
1248 return iwn_dma_contig_alloc(sc, &sc->fw_dma, NULL,
1249 sc->sc_hal->fwsz, 16, BUS_DMA_NOWAIT);
1253 iwn_free_fwmem(struct iwn_softc *sc)
1255 iwn_dma_contig_free(&sc->fw_dma);
1259 iwn_alloc_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
1266 /* Allocate RX descriptors (256-byte aligned). */
1267 size = IWN_RX_RING_COUNT * sizeof (uint32_t);
1268 error = iwn_dma_contig_alloc(sc, &ring->desc_dma,
1269 (void **)&ring->desc, size, 256, BUS_DMA_NOWAIT);
1271 device_printf(sc->sc_dev,
1272 "%s: could not allocate Rx ring DMA memory, error %d\n",
1277 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1278 BUS_SPACE_MAXADDR_32BIT,
1279 BUS_SPACE_MAXADDR, NULL, NULL, MJUMPAGESIZE, 1,
1280 MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL, &ring->data_dmat);
1282 device_printf(sc->sc_dev,
1283 "%s: bus_dma_tag_create_failed, error %d\n",
1288 /* Allocate RX status area (16-byte aligned). */
1289 error = iwn_dma_contig_alloc(sc, &ring->stat_dma,
1290 (void **)&ring->stat, sizeof (struct iwn_rx_status),
1291 16, BUS_DMA_NOWAIT);
1293 device_printf(sc->sc_dev,
1294 "%s: could not allocate Rx status DMA memory, error %d\n",
1300 * Allocate and map RX buffers.
1302 for (i = 0; i < IWN_RX_RING_COUNT; i++) {
1303 struct iwn_rx_data *data = &ring->data[i];
1306 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1308 device_printf(sc->sc_dev,
1309 "%s: bus_dmamap_create failed, error %d\n",
1314 data->m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
1315 if (data->m == NULL) {
1316 device_printf(sc->sc_dev,
1317 "%s: could not allocate rx mbuf\n", __func__);
1323 error = bus_dmamap_load(ring->data_dmat, data->map,
1324 mtod(data->m, caddr_t), MJUMPAGESIZE,
1325 iwn_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
1326 if (error != 0 && error != EFBIG) {
1327 device_printf(sc->sc_dev,
1328 "%s: bus_dmamap_load failed, error %d\n",
1331 error = ENOMEM; /* XXX unique code */
1334 bus_dmamap_sync(ring->data_dmat, data->map,
1335 BUS_DMASYNC_PREWRITE);
1337 /* Set physical address of RX buffer (256-byte aligned). */
1338 ring->desc[i] = htole32(paddr >> 8);
1340 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1341 BUS_DMASYNC_PREWRITE);
1344 iwn_free_rx_ring(sc, ring);
1349 iwn_reset_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
1353 if (iwn_nic_lock(sc) == 0) {
1354 IWN_WRITE(sc, IWN_FH_RX_CONFIG, 0);
1355 for (ntries = 0; ntries < 1000; ntries++) {
1356 if (IWN_READ(sc, IWN_FH_RX_STATUS) &
1357 IWN_FH_RX_STATUS_IDLE)
1364 DPRINTF(sc, IWN_DEBUG_ANY, "%s\n",
1365 "timeout resetting Rx ring");
1369 sc->last_rx_valid = 0;
1373 iwn_free_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring)
1377 iwn_dma_contig_free(&ring->desc_dma);
1378 iwn_dma_contig_free(&ring->stat_dma);
1380 for (i = 0; i < IWN_RX_RING_COUNT; i++) {
1381 struct iwn_rx_data *data = &ring->data[i];
1383 if (data->m != NULL) {
1384 bus_dmamap_sync(ring->data_dmat, data->map,
1385 BUS_DMASYNC_POSTREAD);
1386 bus_dmamap_unload(ring->data_dmat, data->map);
1389 if (data->map != NULL)
1390 bus_dmamap_destroy(ring->data_dmat, data->map);
1395 iwn_alloc_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring, int qid)
1405 /* Allocate TX descriptors (256-byte aligned.) */
1406 size = IWN_TX_RING_COUNT * sizeof(struct iwn_tx_desc);
1407 error = iwn_dma_contig_alloc(sc, &ring->desc_dma,
1408 (void **)&ring->desc, size, 256, BUS_DMA_NOWAIT);
1410 device_printf(sc->sc_dev,
1411 "%s: could not allocate TX ring DMA memory, error %d\n",
1417 * We only use rings 0 through 4 (4 EDCA + cmd) so there is no need
1418 * to allocate commands space for other rings.
1423 size = IWN_TX_RING_COUNT * sizeof(struct iwn_tx_cmd);
1424 error = iwn_dma_contig_alloc(sc, &ring->cmd_dma,
1425 (void **)&ring->cmd, size, 4, BUS_DMA_NOWAIT);
1427 device_printf(sc->sc_dev,
1428 "%s: could not allocate TX cmd DMA memory, error %d\n",
1433 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
1434 BUS_SPACE_MAXADDR_32BIT,
1435 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IWN_MAX_SCATTER - 1,
1436 MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL, &ring->data_dmat);
1438 device_printf(sc->sc_dev,
1439 "%s: bus_dma_tag_create_failed, error %d\n",
1444 paddr = ring->cmd_dma.paddr;
1445 for (i = 0; i < IWN_TX_RING_COUNT; i++) {
1446 struct iwn_tx_data *data = &ring->data[i];
1448 data->cmd_paddr = paddr;
1449 data->scratch_paddr = paddr + 12;
1450 paddr += sizeof (struct iwn_tx_cmd);
1452 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
1454 device_printf(sc->sc_dev,
1455 "%s: bus_dmamap_create failed, error %d\n",
1459 bus_dmamap_sync(ring->data_dmat, data->map,
1460 BUS_DMASYNC_PREWRITE);
1464 iwn_free_tx_ring(sc, ring);
1469 iwn_reset_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring)
1473 for (i = 0; i < IWN_TX_RING_COUNT; i++) {
1474 struct iwn_tx_data *data = &ring->data[i];
1476 if (data->m != NULL) {
1477 bus_dmamap_unload(ring->data_dmat, data->map);
1482 /* Clear TX descriptors. */
1483 memset(ring->desc, 0, ring->desc_dma.size);
1484 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
1485 BUS_DMASYNC_PREWRITE);
1486 sc->qfullmsk &= ~(1 << ring->qid);
1492 iwn_free_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring)
1496 iwn_dma_contig_free(&ring->desc_dma);
1497 iwn_dma_contig_free(&ring->cmd_dma);
1499 for (i = 0; i < IWN_TX_RING_COUNT; i++) {
1500 struct iwn_tx_data *data = &ring->data[i];
1502 if (data->m != NULL) {
1503 bus_dmamap_sync(ring->data_dmat, data->map,
1504 BUS_DMASYNC_POSTWRITE);
1505 bus_dmamap_unload(ring->data_dmat, data->map);
1508 if (data->map != NULL)
1509 bus_dmamap_destroy(ring->data_dmat, data->map);
1514 iwn5000_ict_reset(struct iwn_softc *sc)
1516 /* Disable interrupts. */
1517 IWN_WRITE(sc, IWN_INT_MASK, 0);
1519 /* Reset ICT table. */
1520 memset(sc->ict, 0, IWN_ICT_SIZE);
1523 /* Set physical address of ICT table (4KB aligned.) */
1524 DPRINTF(sc, IWN_DEBUG_RESET, "%s: enabling ICT\n", __func__);
1525 IWN_WRITE(sc, IWN_DRAM_INT_TBL, IWN_DRAM_INT_TBL_ENABLE |
1526 IWN_DRAM_INT_TBL_WRAP_CHECK | sc->ict_dma.paddr >> 12);
1528 /* Enable periodic RX interrupt. */
1529 sc->int_mask |= IWN_INT_RX_PERIODIC;
1530 /* Switch to ICT interrupt mode in driver. */
1531 sc->sc_flags |= IWN_FLAG_USE_ICT;
1533 /* Re-enable interrupts. */
1534 IWN_WRITE(sc, IWN_INT, 0xffffffff);
1535 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask);
1539 iwn_read_eeprom(struct iwn_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN])
1541 const struct iwn_hal *hal = sc->sc_hal;
1545 /* Check whether adapter has an EEPROM or an OTPROM. */
1546 if (sc->hw_type >= IWN_HW_REV_TYPE_1000 &&
1547 (IWN_READ(sc, IWN_OTP_GP) & IWN_OTP_GP_DEV_SEL_OTP))
1548 sc->sc_flags |= IWN_FLAG_HAS_OTPROM;
1549 DPRINTF(sc, IWN_DEBUG_RESET, "%s found\n",
1550 (sc->sc_flags & IWN_FLAG_HAS_OTPROM) ? "OTPROM" : "EEPROM");
1552 /* Adapter has to be powered on for EEPROM access to work. */
1553 error = iwn_apm_init(sc);
1555 device_printf(sc->sc_dev,
1556 "%s: could not power ON adapter, error %d\n",
1561 if ((IWN_READ(sc, IWN_EEPROM_GP) & 0x7) == 0) {
1562 device_printf(sc->sc_dev, "%s: bad ROM signature\n", __func__);
1565 error = iwn_eeprom_lock(sc);
1567 device_printf(sc->sc_dev,
1568 "%s: could not lock ROM, error %d\n",
1573 if (sc->sc_flags & IWN_FLAG_HAS_OTPROM) {
1574 error = iwn_init_otprom(sc);
1576 device_printf(sc->sc_dev,
1577 "%s: could not initialize OTPROM, error %d\n",
1583 iwn_read_prom_data(sc, IWN_EEPROM_RFCFG, &val, 2);
1584 sc->rfcfg = le16toh(val);
1585 DPRINTF(sc, IWN_DEBUG_RESET, "radio config=0x%04x\n", sc->rfcfg);
1587 /* Read MAC address. */
1588 iwn_read_prom_data(sc, IWN_EEPROM_MAC, macaddr, 6);
1590 /* Read adapter-specific information from EEPROM. */
1591 hal->read_eeprom(sc);
1593 iwn_apm_stop(sc); /* Power OFF adapter. */
1595 iwn_eeprom_unlock(sc);
1600 iwn4965_read_eeprom(struct iwn_softc *sc)
1606 /* Read regulatory domain (4 ASCII characters.) */
1607 iwn_read_prom_data(sc, IWN4965_EEPROM_DOMAIN, sc->eeprom_domain, 4);
1609 /* Read the list of authorized channels (20MHz ones only.) */
1610 for (i = 0; i < 5; i++) {
1611 addr = iwn4965_regulatory_bands[i];
1612 iwn_read_eeprom_channels(sc, i, addr);
1615 /* Read maximum allowed TX power for 2GHz and 5GHz bands. */
1616 iwn_read_prom_data(sc, IWN4965_EEPROM_MAXPOW, &val, 2);
1617 sc->maxpwr2GHz = val & 0xff;
1618 sc->maxpwr5GHz = val >> 8;
1619 /* Check that EEPROM values are within valid range. */
1620 if (sc->maxpwr5GHz < 20 || sc->maxpwr5GHz > 50)
1621 sc->maxpwr5GHz = 38;
1622 if (sc->maxpwr2GHz < 20 || sc->maxpwr2GHz > 50)
1623 sc->maxpwr2GHz = 38;
1624 DPRINTF(sc, IWN_DEBUG_RESET, "maxpwr 2GHz=%d 5GHz=%d\n",
1625 sc->maxpwr2GHz, sc->maxpwr5GHz);
1627 /* Read samples for each TX power group. */
1628 iwn_read_prom_data(sc, IWN4965_EEPROM_BANDS, sc->bands,
1631 /* Read voltage at which samples were taken. */
1632 iwn_read_prom_data(sc, IWN4965_EEPROM_VOLTAGE, &val, 2);
1633 sc->eeprom_voltage = (int16_t)le16toh(val);
1634 DPRINTF(sc, IWN_DEBUG_RESET, "voltage=%d (in 0.3V)\n",
1635 sc->eeprom_voltage);
1638 /* Print samples. */
1639 if (sc->sc_debug & IWN_DEBUG_ANY) {
1640 for (i = 0; i < IWN_NBANDS; i++)
1641 iwn4965_print_power_group(sc, i);
1648 iwn4965_print_power_group(struct iwn_softc *sc, int i)
1650 struct iwn4965_eeprom_band *band = &sc->bands[i];
1651 struct iwn4965_eeprom_chan_samples *chans = band->chans;
1654 printf("===band %d===\n", i);
1655 printf("chan lo=%d, chan hi=%d\n", band->lo, band->hi);
1656 printf("chan1 num=%d\n", chans[0].num);
1657 for (c = 0; c < 2; c++) {
1658 for (j = 0; j < IWN_NSAMPLES; j++) {
1659 printf("chain %d, sample %d: temp=%d gain=%d "
1660 "power=%d pa_det=%d\n", c, j,
1661 chans[0].samples[c][j].temp,
1662 chans[0].samples[c][j].gain,
1663 chans[0].samples[c][j].power,
1664 chans[0].samples[c][j].pa_det);
1667 printf("chan2 num=%d\n", chans[1].num);
1668 for (c = 0; c < 2; c++) {
1669 for (j = 0; j < IWN_NSAMPLES; j++) {
1670 printf("chain %d, sample %d: temp=%d gain=%d "
1671 "power=%d pa_det=%d\n", c, j,
1672 chans[1].samples[c][j].temp,
1673 chans[1].samples[c][j].gain,
1674 chans[1].samples[c][j].power,
1675 chans[1].samples[c][j].pa_det);
1682 iwn5000_read_eeprom(struct iwn_softc *sc)
1684 struct iwn5000_eeprom_calib_hdr hdr;
1686 uint32_t addr, base;
1690 /* Read regulatory domain (4 ASCII characters.) */
1691 iwn_read_prom_data(sc, IWN5000_EEPROM_REG, &val, 2);
1692 base = le16toh(val);
1693 iwn_read_prom_data(sc, base + IWN5000_EEPROM_DOMAIN,
1694 sc->eeprom_domain, 4);
1696 /* Read the list of authorized channels (20MHz ones only.) */
1697 for (i = 0; i < 5; i++) {
1698 addr = base + iwn5000_regulatory_bands[i];
1699 iwn_read_eeprom_channels(sc, i, addr);
1702 /* Read enhanced TX power information for 6000 Series. */
1703 if (sc->hw_type >= IWN_HW_REV_TYPE_6000)
1704 iwn_read_eeprom_enhinfo(sc);
1706 iwn_read_prom_data(sc, IWN5000_EEPROM_CAL, &val, 2);
1707 base = le16toh(val);
1708 iwn_read_prom_data(sc, base, &hdr, sizeof hdr);
1709 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
1710 "%s: calib version=%u pa type=%u voltage=%u\n",
1711 __func__, hdr.version, hdr.pa_type, le16toh(hdr.volt));
1712 sc->calib_ver = hdr.version;
1714 if (sc->hw_type == IWN_HW_REV_TYPE_5150) {
1715 /* Compute temperature offset. */
1716 iwn_read_prom_data(sc, base + IWN5000_EEPROM_TEMP, &val, 2);
1717 temp = le16toh(val);
1718 iwn_read_prom_data(sc, base + IWN5000_EEPROM_VOLT, &val, 2);
1719 volt = le16toh(val);
1720 sc->temp_off = temp - (volt / -5);
1721 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "temp=%d volt=%d offset=%dK\n",
1722 temp, volt, sc->temp_off);
1727 * Translate EEPROM flags to net80211.
1730 iwn_eeprom_channel_flags(struct iwn_eeprom_chan *channel)
1735 if ((channel->flags & IWN_EEPROM_CHAN_ACTIVE) == 0)
1736 nflags |= IEEE80211_CHAN_PASSIVE;
1737 if ((channel->flags & IWN_EEPROM_CHAN_IBSS) == 0)
1738 nflags |= IEEE80211_CHAN_NOADHOC;
1739 if (channel->flags & IWN_EEPROM_CHAN_RADAR) {
1740 nflags |= IEEE80211_CHAN_DFS;
1741 /* XXX apparently IBSS may still be marked */
1742 nflags |= IEEE80211_CHAN_NOADHOC;
1749 iwn_read_eeprom_band(struct iwn_softc *sc, int n)
1751 struct ifnet *ifp = sc->sc_ifp;
1752 struct ieee80211com *ic = ifp->if_l2com;
1753 struct iwn_eeprom_chan *channels = sc->eeprom_channels[n];
1754 const struct iwn_chan_band *band = &iwn_bands[n];
1755 struct ieee80211_channel *c;
1756 int i, chan, nflags;
1758 for (i = 0; i < band->nchan; i++) {
1759 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID)) {
1760 DPRINTF(sc, IWN_DEBUG_RESET,
1761 "skip chan %d flags 0x%x maxpwr %d\n",
1762 band->chan[i], channels[i].flags,
1763 channels[i].maxpwr);
1766 chan = band->chan[i];
1767 nflags = iwn_eeprom_channel_flags(&channels[i]);
1769 DPRINTF(sc, IWN_DEBUG_RESET,
1770 "add chan %d flags 0x%x maxpwr %d\n",
1771 chan, channels[i].flags, channels[i].maxpwr);
1773 c = &ic->ic_channels[ic->ic_nchans++];
1775 c->ic_maxregpower = channels[i].maxpwr;
1776 c->ic_maxpower = 2*c->ic_maxregpower;
1778 /* Save maximum allowed TX power for this channel. */
1779 sc->maxpwr[chan] = channels[i].maxpwr;
1781 if (n == 0) { /* 2GHz band */
1782 c->ic_freq = ieee80211_ieee2mhz(chan,
1785 /* G =>'s B is supported */
1786 c->ic_flags = IEEE80211_CHAN_B | nflags;
1788 c = &ic->ic_channels[ic->ic_nchans++];
1790 c->ic_flags = IEEE80211_CHAN_G | nflags;
1791 } else { /* 5GHz band */
1792 c->ic_freq = ieee80211_ieee2mhz(chan,
1794 c->ic_flags = IEEE80211_CHAN_A | nflags;
1795 sc->sc_flags |= IWN_FLAG_HAS_5GHZ;
1798 /* XXX no constraints on using HT20 */
1799 /* add HT20, HT40 added separately */
1800 c = &ic->ic_channels[ic->ic_nchans++];
1802 c->ic_flags |= IEEE80211_CHAN_HT20;
1803 /* XXX NARROW =>'s 1/2 and 1/4 width? */
1810 iwn_read_eeprom_ht40(struct iwn_softc *sc, int n)
1812 struct ifnet *ifp = sc->sc_ifp;
1813 struct ieee80211com *ic = ifp->if_l2com;
1814 struct iwn_eeprom_chan *channels = sc->eeprom_channels[n];
1815 const struct iwn_chan_band *band = &iwn_bands[n];
1816 struct ieee80211_channel *c, *cent, *extc;
1819 for (i = 0; i < band->nchan; i++) {
1820 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID) ||
1821 !(channels[i].flags & IWN_EEPROM_CHAN_WIDE)) {
1822 DPRINTF(sc, IWN_DEBUG_RESET,
1823 "skip chan %d flags 0x%x maxpwr %d\n",
1824 band->chan[i], channels[i].flags,
1825 channels[i].maxpwr);
1829 * Each entry defines an HT40 channel pair; find the
1830 * center channel, then the extension channel above.
1832 cent = ieee80211_find_channel_byieee(ic, band->chan[i],
1833 band->flags & ~IEEE80211_CHAN_HT);
1834 if (cent == NULL) { /* XXX shouldn't happen */
1835 device_printf(sc->sc_dev,
1836 "%s: no entry for channel %d\n",
1837 __func__, band->chan[i]);
1840 extc = ieee80211_find_channel(ic, cent->ic_freq+20,
1841 band->flags & ~IEEE80211_CHAN_HT);
1843 DPRINTF(sc, IWN_DEBUG_RESET,
1844 "skip chan %d, extension channel not found\n",
1849 DPRINTF(sc, IWN_DEBUG_RESET,
1850 "add ht40 chan %d flags 0x%x maxpwr %d\n",
1851 band->chan[i], channels[i].flags, channels[i].maxpwr);
1853 c = &ic->ic_channels[ic->ic_nchans++];
1855 c->ic_extieee = extc->ic_ieee;
1856 c->ic_flags &= ~IEEE80211_CHAN_HT;
1857 c->ic_flags |= IEEE80211_CHAN_HT40U;
1858 c = &ic->ic_channels[ic->ic_nchans++];
1860 c->ic_extieee = cent->ic_ieee;
1861 c->ic_flags &= ~IEEE80211_CHAN_HT;
1862 c->ic_flags |= IEEE80211_CHAN_HT40D;
1868 iwn_read_eeprom_channels(struct iwn_softc *sc, int n, uint32_t addr)
1870 struct ifnet *ifp = sc->sc_ifp;
1871 struct ieee80211com *ic = ifp->if_l2com;
1873 iwn_read_prom_data(sc, addr, &sc->eeprom_channels[n],
1874 iwn_bands[n].nchan * sizeof (struct iwn_eeprom_chan));
1877 iwn_read_eeprom_band(sc, n);
1880 iwn_read_eeprom_ht40(sc, n);
1882 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
1885 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
1888 iwn_read_eeprom_enhinfo(struct iwn_softc *sc)
1890 struct iwn_eeprom_enhinfo enhinfo[35];
1895 iwn_read_prom_data(sc, IWN5000_EEPROM_REG, &val, 2);
1896 base = le16toh(val);
1897 iwn_read_prom_data(sc, base + IWN6000_EEPROM_ENHINFO,
1898 enhinfo, sizeof enhinfo);
1900 memset(sc->enh_maxpwr, 0, sizeof sc->enh_maxpwr);
1901 for (i = 0; i < nitems(enhinfo); i++) {
1902 if (enhinfo[i].chan == 0 || enhinfo[i].reserved != 0)
1903 continue; /* Skip invalid entries. */
1906 if (sc->txchainmask & IWN_ANT_A)
1907 maxpwr = MAX(maxpwr, enhinfo[i].chain[0]);
1908 if (sc->txchainmask & IWN_ANT_B)
1909 maxpwr = MAX(maxpwr, enhinfo[i].chain[1]);
1910 if (sc->txchainmask & IWN_ANT_C)
1911 maxpwr = MAX(maxpwr, enhinfo[i].chain[2]);
1912 if (sc->ntxchains == 2)
1913 maxpwr = MAX(maxpwr, enhinfo[i].mimo2);
1914 else if (sc->ntxchains == 3)
1915 maxpwr = MAX(maxpwr, enhinfo[i].mimo3);
1916 maxpwr /= 2; /* Convert half-dBm to dBm. */
1918 DPRINTF(sc, IWN_DEBUG_RESET, "enhinfo %d, maxpwr=%d\n", i,
1920 sc->enh_maxpwr[i] = maxpwr;
1924 static struct ieee80211_node *
1925 iwn_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
1927 return malloc(sizeof (struct iwn_node), M_80211_NODE,M_NOWAIT | M_ZERO);
1931 iwn_media_change(struct ifnet *ifp)
1933 int error = ieee80211_media_change(ifp);
1934 /* NB: only the fixed rate can change and that doesn't need a reset */
1935 return (error == ENETRESET ? 0 : error);
1939 iwn_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1941 struct iwn_vap *ivp = IWN_VAP(vap);
1942 struct ieee80211com *ic = vap->iv_ic;
1943 struct iwn_softc *sc = ic->ic_ifp->if_softc;
1946 DPRINTF(sc, IWN_DEBUG_STATE, "%s: %s -> %s\n", __func__,
1947 ieee80211_state_name[vap->iv_state],
1948 ieee80211_state_name[nstate]);
1950 IEEE80211_UNLOCK(ic);
1952 callout_stop(&sc->sc_timer_to);
1955 case IEEE80211_S_ASSOC:
1956 if (vap->iv_state != IEEE80211_S_RUN)
1959 case IEEE80211_S_AUTH:
1960 if (vap->iv_state == IEEE80211_S_AUTH)
1964 * !AUTH -> AUTH transition requires state reset to handle
1965 * reassociations correctly.
1967 sc->rxon.associd = 0;
1968 sc->rxon.filter &= ~htole32(IWN_FILTER_BSS);
1969 iwn_calib_reset(sc);
1970 error = iwn_auth(sc, vap);
1973 case IEEE80211_S_RUN:
1975 * RUN -> RUN transition; Just restart the timers.
1977 if (vap->iv_state == IEEE80211_S_RUN &&
1978 vap->iv_opmode != IEEE80211_M_MONITOR) {
1979 iwn_calib_reset(sc);
1984 * !RUN -> RUN requires setting the association id
1985 * which is done with a firmware cmd. We also defer
1986 * starting the timers until that work is done.
1988 error = iwn_run(sc, vap);
1996 return ivp->iv_newstate(vap, nstate, arg);
2000 * Process an RX_PHY firmware notification. This is usually immediately
2001 * followed by an MPDU_RX_DONE notification.
2004 iwn_rx_phy(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2005 struct iwn_rx_data *data)
2007 struct iwn_rx_stat *stat = (struct iwn_rx_stat *)(desc + 1);
2009 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: received PHY stats\n", __func__);
2010 bus_dmamap_sync(sc->rxq.data_dmat, data->map, BUS_DMASYNC_POSTREAD);
2012 /* Save RX statistics, they will be used on MPDU_RX_DONE. */
2013 memcpy(&sc->last_rx_stat, stat, sizeof (*stat));
2014 sc->last_rx_valid = 1;
2018 iwn_timer_timeout(void *arg)
2020 struct iwn_softc *sc = arg;
2023 IWN_LOCK_ASSERT(sc);
2025 if (sc->calib_cnt && --sc->calib_cnt == 0) {
2026 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s\n",
2027 "send statistics request");
2028 (void) iwn_cmd(sc, IWN_CMD_GET_STATISTICS, &flags,
2030 sc->calib_cnt = 60; /* do calibration every 60s */
2032 iwn_watchdog(sc); /* NB: piggyback tx watchdog */
2033 callout_reset(&sc->sc_timer_to, hz, iwn_timer_timeout, sc);
2037 iwn_calib_reset(struct iwn_softc *sc)
2039 callout_reset(&sc->sc_timer_to, hz, iwn_timer_timeout, sc);
2040 sc->calib_cnt = 60; /* do calibration every 60s */
2044 * Process an RX_DONE (4965AGN only) or MPDU_RX_DONE firmware notification.
2045 * Each MPDU_RX_DONE notification must be preceded by an RX_PHY one.
2048 iwn_rx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2049 struct iwn_rx_data *data)
2051 const struct iwn_hal *hal = sc->sc_hal;
2052 struct ifnet *ifp = sc->sc_ifp;
2053 struct ieee80211com *ic = ifp->if_l2com;
2054 struct iwn_rx_ring *ring = &sc->rxq;
2055 struct ieee80211_frame *wh;
2056 struct ieee80211_node *ni;
2057 struct mbuf *m, *m1;
2058 struct iwn_rx_stat *stat;
2062 int error, len, rssi, nf;
2064 if (desc->type == IWN_MPDU_RX_DONE) {
2065 /* Check for prior RX_PHY notification. */
2066 if (!sc->last_rx_valid) {
2067 DPRINTF(sc, IWN_DEBUG_ANY,
2068 "%s: missing RX_PHY\n", __func__);
2072 sc->last_rx_valid = 0;
2073 stat = &sc->last_rx_stat;
2075 stat = (struct iwn_rx_stat *)(desc + 1);
2077 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
2079 if (stat->cfg_phy_len > IWN_STAT_MAXLEN) {
2080 device_printf(sc->sc_dev,
2081 "%s: invalid rx statistic header, len %d\n",
2082 __func__, stat->cfg_phy_len);
2086 if (desc->type == IWN_MPDU_RX_DONE) {
2087 struct iwn_rx_mpdu *mpdu = (struct iwn_rx_mpdu *)(desc + 1);
2088 head = (caddr_t)(mpdu + 1);
2089 len = le16toh(mpdu->len);
2091 head = (caddr_t)(stat + 1) + stat->cfg_phy_len;
2092 len = le16toh(stat->len);
2095 flags = le32toh(*(uint32_t *)(head + len));
2097 /* Discard frames with a bad FCS early. */
2098 if ((flags & IWN_RX_NOERROR) != IWN_RX_NOERROR) {
2099 DPRINTF(sc, IWN_DEBUG_RECV, "%s: rx flags error %x\n",
2104 /* Discard frames that are too short. */
2105 if (len < sizeof (*wh)) {
2106 DPRINTF(sc, IWN_DEBUG_RECV, "%s: frame too short: %d\n",
2112 /* XXX don't need mbuf, just dma buffer */
2113 m1 = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
2115 DPRINTF(sc, IWN_DEBUG_ANY, "%s: no mbuf to restock ring\n",
2120 bus_dmamap_unload(ring->data_dmat, data->map);
2122 error = bus_dmamap_load(ring->data_dmat, data->map,
2123 mtod(m1, caddr_t), MJUMPAGESIZE,
2124 iwn_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
2125 if (error != 0 && error != EFBIG) {
2126 device_printf(sc->sc_dev,
2127 "%s: bus_dmamap_load failed, error %d\n", __func__, error);
2135 /* Update RX descriptor. */
2136 ring->desc[ring->cur] = htole32(paddr >> 8);
2137 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
2138 BUS_DMASYNC_PREWRITE);
2140 /* Finalize mbuf. */
2141 m->m_pkthdr.rcvif = ifp;
2143 m->m_pkthdr.len = m->m_len = len;
2145 rssi = hal->get_rssi(sc, stat);
2147 /* Grab a reference to the source node. */
2148 wh = mtod(m, struct ieee80211_frame *);
2149 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
2150 nf = (ni != NULL && ni->ni_vap->iv_state == IEEE80211_S_RUN &&
2151 (ic->ic_flags & IEEE80211_F_SCAN) == 0) ? sc->noise : -95;
2153 if (ieee80211_radiotap_active(ic)) {
2154 struct iwn_rx_radiotap_header *tap = &sc->sc_rxtap;
2156 tap->wr_tsft = htole64(stat->tstamp);
2158 if (stat->flags & htole16(IWN_STAT_FLAG_SHPREAMBLE))
2159 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
2160 switch (stat->rate) {
2162 case 10: tap->wr_rate = 2; break;
2163 case 20: tap->wr_rate = 4; break;
2164 case 55: tap->wr_rate = 11; break;
2165 case 110: tap->wr_rate = 22; break;
2167 case 0xd: tap->wr_rate = 12; break;
2168 case 0xf: tap->wr_rate = 18; break;
2169 case 0x5: tap->wr_rate = 24; break;
2170 case 0x7: tap->wr_rate = 36; break;
2171 case 0x9: tap->wr_rate = 48; break;
2172 case 0xb: tap->wr_rate = 72; break;
2173 case 0x1: tap->wr_rate = 96; break;
2174 case 0x3: tap->wr_rate = 108; break;
2175 /* Unknown rate: should not happen. */
2176 default: tap->wr_rate = 0;
2178 tap->wr_dbm_antsignal = rssi;
2179 tap->wr_dbm_antnoise = nf;
2184 /* Send the frame to the 802.11 layer. */
2186 (void) ieee80211_input(ni, m, rssi - nf, nf);
2187 /* Node is no longer needed. */
2188 ieee80211_free_node(ni);
2190 (void) ieee80211_input_all(ic, m, rssi - nf, nf);
2196 /* Process an incoming Compressed BlockAck. */
2198 iwn_rx_compressed_ba(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2199 struct iwn_rx_data *data)
2201 struct iwn_compressed_ba *ba = (struct iwn_compressed_ba *)(desc + 1);
2202 struct iwn_tx_ring *txq;
2204 txq = &sc->txq[letoh16(ba->qid)];
2210 * Process an RX_STATISTICS or BEACON_STATISTICS firmware notification.
2211 * The latter is sent by the firmware after each received beacon.
2214 iwn_rx_statistics(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2215 struct iwn_rx_data *data)
2217 const struct iwn_hal *hal = sc->sc_hal;
2218 struct ifnet *ifp = sc->sc_ifp;
2219 struct ieee80211com *ic = ifp->if_l2com;
2220 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2221 struct iwn_calib_state *calib = &sc->calib;
2222 struct iwn_stats *stats = (struct iwn_stats *)(desc + 1);
2225 /* Beacon stats are meaningful only when associated and not scanning. */
2226 if (vap->iv_state != IEEE80211_S_RUN ||
2227 (ic->ic_flags & IEEE80211_F_SCAN))
2230 bus_dmamap_sync(sc->rxq.data_dmat, data->map, BUS_DMASYNC_POSTREAD);
2231 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: cmd %d\n", __func__, desc->type);
2232 iwn_calib_reset(sc); /* Reset TX power calibration timeout. */
2234 /* Test if temperature has changed. */
2235 if (stats->general.temp != sc->rawtemp) {
2236 /* Convert "raw" temperature to degC. */
2237 sc->rawtemp = stats->general.temp;
2238 temp = hal->get_temperature(sc);
2239 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: temperature %d\n",
2242 /* Update TX power if need be (4965AGN only.) */
2243 if (sc->hw_type == IWN_HW_REV_TYPE_4965)
2244 iwn4965_power_calibration(sc, temp);
2247 if (desc->type != IWN_BEACON_STATISTICS)
2248 return; /* Reply to a statistics request. */
2250 sc->noise = iwn_get_noise(&stats->rx.general);
2251 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: noise %d\n", __func__, sc->noise);
2253 /* Test that RSSI and noise are present in stats report. */
2254 if (le32toh(stats->rx.general.flags) != 1) {
2255 DPRINTF(sc, IWN_DEBUG_ANY, "%s\n",
2256 "received statistics without RSSI");
2260 if (calib->state == IWN_CALIB_STATE_ASSOC)
2261 iwn_collect_noise(sc, &stats->rx.general);
2262 else if (calib->state == IWN_CALIB_STATE_RUN)
2263 iwn_tune_sensitivity(sc, &stats->rx);
2267 * Process a TX_DONE firmware notification. Unfortunately, the 4965AGN
2268 * and 5000 adapters have different incompatible TX status formats.
2271 iwn4965_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2272 struct iwn_rx_data *data)
2274 struct iwn4965_tx_stat *stat = (struct iwn4965_tx_stat *)(desc + 1);
2275 struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf];
2277 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: "
2278 "qid %d idx %d retries %d nkill %d rate %x duration %d status %x\n",
2279 __func__, desc->qid, desc->idx, stat->ackfailcnt,
2280 stat->btkillcnt, stat->rate, le16toh(stat->duration),
2281 le32toh(stat->status));
2283 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
2284 iwn_tx_done(sc, desc, stat->ackfailcnt, le32toh(stat->status) & 0xff);
2288 iwn5000_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc,
2289 struct iwn_rx_data *data)
2291 struct iwn5000_tx_stat *stat = (struct iwn5000_tx_stat *)(desc + 1);
2292 struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf];
2294 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: "
2295 "qid %d idx %d retries %d nkill %d rate %x duration %d status %x\n",
2296 __func__, desc->qid, desc->idx, stat->ackfailcnt,
2297 stat->btkillcnt, stat->rate, le16toh(stat->duration),
2298 le32toh(stat->status));
2301 /* Reset TX scheduler slot. */
2302 iwn5000_reset_sched(sc, desc->qid & 0xf, desc->idx);
2305 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTREAD);
2306 iwn_tx_done(sc, desc, stat->ackfailcnt, le16toh(stat->status) & 0xff);
2310 * Adapter-independent backend for TX_DONE firmware notifications.
2313 iwn_tx_done(struct iwn_softc *sc, struct iwn_rx_desc *desc, int ackfailcnt,
2316 struct ifnet *ifp = sc->sc_ifp;
2317 struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf];
2318 struct iwn_tx_data *data = &ring->data[desc->idx];
2320 struct ieee80211_node *ni;
2321 struct ieee80211vap *vap;
2323 KASSERT(data->ni != NULL, ("no node"));
2325 /* Unmap and free mbuf. */
2326 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
2327 bus_dmamap_unload(ring->data_dmat, data->map);
2328 m = data->m, data->m = NULL;
2329 ni = data->ni, data->ni = NULL;
2332 if (m->m_flags & M_TXCB) {
2334 * Channels marked for "radar" require traffic to be received
2335 * to unlock before we can transmit. Until traffic is seen
2336 * any attempt to transmit is returned immediately with status
2337 * set to IWN_TX_FAIL_TX_LOCKED. Unfortunately this can easily
2338 * happen on first authenticate after scanning. To workaround
2339 * this we ignore a failure of this sort in AUTH state so the
2340 * 802.11 layer will fall back to using a timeout to wait for
2341 * the AUTH reply. This allows the firmware time to see
2342 * traffic so a subsequent retry of AUTH succeeds. It's
2343 * unclear why the firmware does not maintain state for
2344 * channels recently visited as this would allow immediate
2345 * use of the channel after a scan (where we see traffic).
2347 if (status == IWN_TX_FAIL_TX_LOCKED &&
2348 ni->ni_vap->iv_state == IEEE80211_S_AUTH)
2349 ieee80211_process_callback(ni, m, 0);
2351 ieee80211_process_callback(ni, m,
2352 (status & IWN_TX_FAIL) != 0);
2356 * Update rate control statistics for the node.
2358 if (status & 0x80) {
2360 ieee80211_ratectl_tx_complete(vap, ni,
2361 IEEE80211_RATECTL_TX_FAILURE, &ackfailcnt, NULL);
2363 ieee80211_ratectl_tx_complete(vap, ni,
2364 IEEE80211_RATECTL_TX_SUCCESS, &ackfailcnt, NULL);
2367 ieee80211_free_node(ni);
2369 sc->sc_tx_timer = 0;
2370 if (--ring->queued < IWN_TX_RING_LOMARK) {
2371 sc->qfullmsk &= ~(1 << ring->qid);
2372 if (sc->qfullmsk == 0 &&
2373 (ifp->if_drv_flags & IFF_DRV_OACTIVE)) {
2374 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2375 iwn_start_locked(ifp);
2381 * Process a "command done" firmware notification. This is where we wakeup
2382 * processes waiting for a synchronous command completion.
2385 iwn_cmd_done(struct iwn_softc *sc, struct iwn_rx_desc *desc)
2387 struct iwn_tx_ring *ring = &sc->txq[4];
2388 struct iwn_tx_data *data;
2390 if ((desc->qid & 0xf) != 4)
2391 return; /* Not a command ack. */
2393 data = &ring->data[desc->idx];
2395 /* If the command was mapped in an mbuf, free it. */
2396 if (data->m != NULL) {
2397 bus_dmamap_unload(ring->data_dmat, data->map);
2401 wakeup(&ring->desc[desc->idx]);
2405 * Process an INT_FH_RX or INT_SW_RX interrupt.
2408 iwn_notif_intr(struct iwn_softc *sc)
2410 struct ifnet *ifp = sc->sc_ifp;
2411 struct ieee80211com *ic = ifp->if_l2com;
2412 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
2415 bus_dmamap_sync(sc->rxq.stat_dma.tag, sc->rxq.stat_dma.map,
2416 BUS_DMASYNC_POSTREAD);
2418 hw = le16toh(sc->rxq.stat->closed_count) & 0xfff;
2419 while (sc->rxq.cur != hw) {
2420 struct iwn_rx_data *data = &sc->rxq.data[sc->rxq.cur];
2421 struct iwn_rx_desc *desc;
2423 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2424 BUS_DMASYNC_POSTREAD);
2425 desc = mtod(data->m, struct iwn_rx_desc *);
2427 DPRINTF(sc, IWN_DEBUG_RECV,
2428 "%s: qid %x idx %d flags %x type %d(%s) len %d\n",
2429 __func__, desc->qid & 0xf, desc->idx, desc->flags,
2430 desc->type, iwn_intr_str(desc->type),
2431 le16toh(desc->len));
2433 if (!(desc->qid & 0x80)) /* Reply to a command. */
2434 iwn_cmd_done(sc, desc);
2436 switch (desc->type) {
2438 iwn_rx_phy(sc, desc, data);
2441 case IWN_RX_DONE: /* 4965AGN only. */
2442 case IWN_MPDU_RX_DONE:
2443 /* An 802.11 frame has been received. */
2444 iwn_rx_done(sc, desc, data);
2448 case IWN_RX_COMPRESSED_BA:
2449 /* A Compressed BlockAck has been received. */
2450 iwn_rx_compressed_ba(sc, desc, data);
2455 /* An 802.11 frame has been transmitted. */
2456 sc->sc_hal->tx_done(sc, desc, data);
2459 case IWN_RX_STATISTICS:
2460 case IWN_BEACON_STATISTICS:
2461 iwn_rx_statistics(sc, desc, data);
2464 case IWN_BEACON_MISSED:
2466 struct iwn_beacon_missed *miss =
2467 (struct iwn_beacon_missed *)(desc + 1);
2470 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2471 BUS_DMASYNC_POSTREAD);
2472 misses = le32toh(miss->consecutive);
2474 /* XXX not sure why we're notified w/ zero */
2477 DPRINTF(sc, IWN_DEBUG_STATE,
2478 "%s: beacons missed %d/%d\n", __func__,
2479 misses, le32toh(miss->total));
2482 * If more than 5 consecutive beacons are missed,
2483 * reinitialize the sensitivity state machine.
2485 if (vap->iv_state == IEEE80211_S_RUN && misses > 5)
2486 (void) iwn_init_sensitivity(sc);
2487 if (misses >= vap->iv_bmissthreshold) {
2489 ieee80211_beacon_miss(ic);
2496 struct iwn_ucode_info *uc =
2497 (struct iwn_ucode_info *)(desc + 1);
2499 /* The microcontroller is ready. */
2500 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2501 BUS_DMASYNC_POSTREAD);
2502 DPRINTF(sc, IWN_DEBUG_RESET,
2503 "microcode alive notification version=%d.%d "
2504 "subtype=%x alive=%x\n", uc->major, uc->minor,
2505 uc->subtype, le32toh(uc->valid));
2507 if (le32toh(uc->valid) != 1) {
2508 device_printf(sc->sc_dev,
2509 "microcontroller initialization failed");
2512 if (uc->subtype == IWN_UCODE_INIT) {
2513 /* Save microcontroller report. */
2514 memcpy(&sc->ucode_info, uc, sizeof (*uc));
2516 /* Save the address of the error log in SRAM. */
2517 sc->errptr = le32toh(uc->errptr);
2520 case IWN_STATE_CHANGED:
2522 uint32_t *status = (uint32_t *)(desc + 1);
2525 * State change allows hardware switch change to be
2526 * noted. However, we handle this in iwn_intr as we
2527 * get both the enable/disble intr.
2529 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2530 BUS_DMASYNC_POSTREAD);
2531 DPRINTF(sc, IWN_DEBUG_INTR, "state changed to %x\n",
2535 case IWN_START_SCAN:
2537 struct iwn_start_scan *scan =
2538 (struct iwn_start_scan *)(desc + 1);
2540 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2541 BUS_DMASYNC_POSTREAD);
2542 DPRINTF(sc, IWN_DEBUG_ANY,
2543 "%s: scanning channel %d status %x\n",
2544 __func__, scan->chan, le32toh(scan->status));
2549 struct iwn_stop_scan *scan =
2550 (struct iwn_stop_scan *)(desc + 1);
2552 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
2553 BUS_DMASYNC_POSTREAD);
2554 DPRINTF(sc, IWN_DEBUG_STATE,
2555 "scan finished nchan=%d status=%d chan=%d\n",
2556 scan->nchan, scan->status, scan->chan);
2559 ieee80211_scan_next(vap);
2563 case IWN5000_CALIBRATION_RESULT:
2564 iwn5000_rx_calib_result(sc, desc, data);
2567 case IWN5000_CALIBRATION_DONE:
2568 sc->sc_flags |= IWN_FLAG_CALIB_DONE;
2573 sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT;
2576 /* Tell the firmware what we have processed. */
2577 hw = (hw == 0) ? IWN_RX_RING_COUNT - 1 : hw - 1;
2578 IWN_WRITE(sc, IWN_FH_RX_WPTR, hw & ~7);
2582 * Process an INT_WAKEUP interrupt raised when the microcontroller wakes up
2583 * from power-down sleep mode.
2586 iwn_wakeup_intr(struct iwn_softc *sc)
2590 DPRINTF(sc, IWN_DEBUG_RESET, "%s: ucode wakeup from power-down sleep\n",
2593 /* Wakeup RX and TX rings. */
2594 IWN_WRITE(sc, IWN_FH_RX_WPTR, sc->rxq.cur & ~7);
2595 for (qid = 0; qid < sc->sc_hal->ntxqs; qid++) {
2596 struct iwn_tx_ring *ring = &sc->txq[qid];
2597 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | ring->cur);
2602 iwn_rftoggle_intr(struct iwn_softc *sc)
2604 struct ifnet *ifp = sc->sc_ifp;
2605 struct ieee80211com *ic = ifp->if_l2com;
2606 uint32_t tmp = IWN_READ(sc, IWN_GP_CNTRL);
2608 IWN_LOCK_ASSERT(sc);
2610 device_printf(sc->sc_dev, "RF switch: radio %s\n",
2611 (tmp & IWN_GP_CNTRL_RFKILL) ? "enabled" : "disabled");
2612 if (tmp & IWN_GP_CNTRL_RFKILL)
2613 ieee80211_runtask(ic, &sc->sc_radioon_task);
2615 ieee80211_runtask(ic, &sc->sc_radiooff_task);
2619 * Dump the error log of the firmware when a firmware panic occurs. Although
2620 * we can't debug the firmware because it is neither open source nor free, it
2621 * can help us to identify certain classes of problems.
2624 iwn_fatal_intr(struct iwn_softc *sc)
2626 const struct iwn_hal *hal = sc->sc_hal;
2627 struct iwn_fw_dump dump;
2630 IWN_LOCK_ASSERT(sc);
2632 /* Force a complete recalibration on next init. */
2633 sc->sc_flags &= ~IWN_FLAG_CALIB_DONE;
2635 /* Check that the error log address is valid. */
2636 if (sc->errptr < IWN_FW_DATA_BASE ||
2637 sc->errptr + sizeof (dump) >
2638 IWN_FW_DATA_BASE + hal->fw_data_maxsz) {
2639 printf("%s: bad firmware error log address 0x%08x\n",
2640 __func__, sc->errptr);
2643 if (iwn_nic_lock(sc) != 0) {
2644 printf("%s: could not read firmware error log\n",
2648 /* Read firmware error log from SRAM. */
2649 iwn_mem_read_region_4(sc, sc->errptr, (uint32_t *)&dump,
2650 sizeof (dump) / sizeof (uint32_t));
2653 if (dump.valid == 0) {
2654 printf("%s: firmware error log is empty\n",
2658 printf("firmware error log:\n");
2659 printf(" error type = \"%s\" (0x%08X)\n",
2660 (dump.id < nitems(iwn_fw_errmsg)) ?
2661 iwn_fw_errmsg[dump.id] : "UNKNOWN",
2663 printf(" program counter = 0x%08X\n", dump.pc);
2664 printf(" source line = 0x%08X\n", dump.src_line);
2665 printf(" error data = 0x%08X%08X\n",
2666 dump.error_data[0], dump.error_data[1]);
2667 printf(" branch link = 0x%08X%08X\n",
2668 dump.branch_link[0], dump.branch_link[1]);
2669 printf(" interrupt link = 0x%08X%08X\n",
2670 dump.interrupt_link[0], dump.interrupt_link[1]);
2671 printf(" time = %u\n", dump.time[0]);
2673 /* Dump driver status (TX and RX rings) while we're here. */
2674 printf("driver status:\n");
2675 for (i = 0; i < hal->ntxqs; i++) {
2676 struct iwn_tx_ring *ring = &sc->txq[i];
2677 printf(" tx ring %2d: qid=%-2d cur=%-3d queued=%-3d\n",
2678 i, ring->qid, ring->cur, ring->queued);
2680 printf(" rx ring: cur=%d\n", sc->rxq.cur);
2686 struct iwn_softc *sc = arg;
2687 struct ifnet *ifp = sc->sc_ifp;
2688 uint32_t r1, r2, tmp;
2692 /* Disable interrupts. */
2693 IWN_WRITE(sc, IWN_INT_MASK, 0);
2695 /* Read interrupts from ICT (fast) or from registers (slow). */
2696 if (sc->sc_flags & IWN_FLAG_USE_ICT) {
2698 while (sc->ict[sc->ict_cur] != 0) {
2699 tmp |= sc->ict[sc->ict_cur];
2700 sc->ict[sc->ict_cur] = 0; /* Acknowledge. */
2701 sc->ict_cur = (sc->ict_cur + 1) % IWN_ICT_COUNT;
2704 if (tmp == 0xffffffff) /* Shouldn't happen. */
2706 else if (tmp & 0xc0000) /* Workaround a HW bug. */
2708 r1 = (tmp & 0xff00) << 16 | (tmp & 0xff);
2709 r2 = 0; /* Unused. */
2711 r1 = IWN_READ(sc, IWN_INT);
2712 if (r1 == 0xffffffff || (r1 & 0xfffffff0) == 0xa5a5a5a0)
2713 return; /* Hardware gone! */
2714 r2 = IWN_READ(sc, IWN_FH_INT);
2717 DPRINTF(sc, IWN_DEBUG_INTR, "interrupt reg1=%x reg2=%x\n", r1, r2);
2719 if (r1 == 0 && r2 == 0)
2720 goto done; /* Interrupt not for us. */
2722 /* Acknowledge interrupts. */
2723 IWN_WRITE(sc, IWN_INT, r1);
2724 if (!(sc->sc_flags & IWN_FLAG_USE_ICT))
2725 IWN_WRITE(sc, IWN_FH_INT, r2);
2727 if (r1 & IWN_INT_RF_TOGGLED) {
2728 iwn_rftoggle_intr(sc);
2731 if (r1 & IWN_INT_CT_REACHED) {
2732 device_printf(sc->sc_dev, "%s: critical temperature reached!\n",
2735 if (r1 & (IWN_INT_SW_ERR | IWN_INT_HW_ERR)) {
2737 ifp->if_flags &= ~IFF_UP;
2738 iwn_stop_locked(sc);
2741 if ((r1 & (IWN_INT_FH_RX | IWN_INT_SW_RX | IWN_INT_RX_PERIODIC)) ||
2742 (r2 & IWN_FH_INT_RX)) {
2743 if (sc->sc_flags & IWN_FLAG_USE_ICT) {
2744 if (r1 & (IWN_INT_FH_RX | IWN_INT_SW_RX))
2745 IWN_WRITE(sc, IWN_FH_INT, IWN_FH_INT_RX);
2746 IWN_WRITE_1(sc, IWN_INT_PERIODIC,
2747 IWN_INT_PERIODIC_DIS);
2749 if (r1 & (IWN_INT_FH_RX | IWN_INT_SW_RX)) {
2750 IWN_WRITE_1(sc, IWN_INT_PERIODIC,
2751 IWN_INT_PERIODIC_ENA);
2757 if ((r1 & IWN_INT_FH_TX) || (r2 & IWN_FH_INT_TX)) {
2758 if (sc->sc_flags & IWN_FLAG_USE_ICT)
2759 IWN_WRITE(sc, IWN_FH_INT, IWN_FH_INT_TX);
2760 wakeup(sc); /* FH DMA transfer completed. */
2763 if (r1 & IWN_INT_ALIVE)
2764 wakeup(sc); /* Firmware is alive. */
2766 if (r1 & IWN_INT_WAKEUP)
2767 iwn_wakeup_intr(sc);
2770 /* Re-enable interrupts. */
2771 if (ifp->if_flags & IFF_UP)
2772 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask);
2778 * Update TX scheduler ring when transmitting an 802.11 frame (4965AGN and
2779 * 5000 adapters use a slightly different format.)
2782 iwn4965_update_sched(struct iwn_softc *sc, int qid, int idx, uint8_t id,
2785 uint16_t *w = &sc->sched[qid * IWN4965_SCHED_COUNT + idx];
2787 *w = htole16(len + 8);
2788 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map,
2789 BUS_DMASYNC_PREWRITE);
2790 if (idx < IWN_SCHED_WINSZ) {
2791 *(w + IWN_TX_RING_COUNT) = *w;
2792 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map,
2793 BUS_DMASYNC_PREWRITE);
2798 iwn5000_update_sched(struct iwn_softc *sc, int qid, int idx, uint8_t id,
2801 uint16_t *w = &sc->sched[qid * IWN5000_SCHED_COUNT + idx];
2803 *w = htole16(id << 12 | (len + 8));
2805 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map,
2806 BUS_DMASYNC_PREWRITE);
2807 if (idx < IWN_SCHED_WINSZ) {
2808 *(w + IWN_TX_RING_COUNT) = *w;
2809 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map,
2810 BUS_DMASYNC_PREWRITE);
2816 iwn5000_reset_sched(struct iwn_softc *sc, int qid, int idx)
2818 uint16_t *w = &sc->sched[qid * IWN5000_SCHED_COUNT + idx];
2820 *w = (*w & htole16(0xf000)) | htole16(1);
2821 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map,
2822 BUS_DMASYNC_PREWRITE);
2823 if (idx < IWN_SCHED_WINSZ) {
2824 *(w + IWN_TX_RING_COUNT) = *w;
2825 bus_dmamap_sync(sc->sched_dma.tag, sc->sched_dma.map,
2826 BUS_DMASYNC_PREWRITE);
2832 iwn_plcp_signal(int rate) {
2835 for (i = 0; i < IWN_RIDX_MAX + 1; i++) {
2836 if (rate == iwn_rates[i].rate)
2844 iwn_tx_data(struct iwn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
2845 struct iwn_tx_ring *ring)
2847 const struct iwn_hal *hal = sc->sc_hal;
2848 const struct ieee80211_txparam *tp;
2849 const struct iwn_rate *rinfo;
2850 struct ieee80211vap *vap = ni->ni_vap;
2851 struct ieee80211com *ic = ni->ni_ic;
2852 struct iwn_node *wn = (void *)ni;
2853 struct iwn_tx_desc *desc;
2854 struct iwn_tx_data *data;
2855 struct iwn_tx_cmd *cmd;
2856 struct iwn_cmd_data *tx;
2857 struct ieee80211_frame *wh;
2858 struct ieee80211_key *k = NULL;
2860 bus_dma_segment_t segs[IWN_MAX_SCATTER];
2863 int totlen, error, pad, nsegs = 0, i, rate;
2864 uint8_t ridx, type, txant;
2866 IWN_LOCK_ASSERT(sc);
2868 wh = mtod(m, struct ieee80211_frame *);
2869 hdrlen = ieee80211_anyhdrsize(wh);
2870 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
2872 desc = &ring->desc[ring->cur];
2873 data = &ring->data[ring->cur];
2875 /* Choose a TX rate index. */
2876 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)];
2877 if (type == IEEE80211_FC0_TYPE_MGT)
2878 rate = tp->mgmtrate;
2879 else if (IEEE80211_IS_MULTICAST(wh->i_addr1))
2880 rate = tp->mcastrate;
2881 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
2882 rate = tp->ucastrate;
2884 /* XXX pass pktlen */
2885 (void) ieee80211_ratectl_rate(ni, NULL, 0);
2886 rate = ni->ni_txrate;
2888 ridx = iwn_plcp_signal(rate);
2889 rinfo = &iwn_rates[ridx];
2891 /* Encrypt the frame if need be. */
2892 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2893 k = ieee80211_crypto_encap(ni, m);
2898 /* Packet header may have moved, reset our local pointer. */
2899 wh = mtod(m, struct ieee80211_frame *);
2901 totlen = m->m_pkthdr.len;
2903 if (ieee80211_radiotap_active_vap(vap)) {
2904 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap;
2907 tap->wt_rate = rinfo->rate;
2909 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2911 ieee80211_radiotap_tx(vap, m);
2914 /* Prepare TX firmware command. */
2915 cmd = &ring->cmd[ring->cur];
2916 cmd->code = IWN_CMD_TX_DATA;
2918 cmd->qid = ring->qid;
2919 cmd->idx = ring->cur;
2921 tx = (struct iwn_cmd_data *)cmd->data;
2922 /* NB: No need to clear tx, all fields are reinitialized here. */
2923 tx->scratch = 0; /* clear "scratch" area */
2926 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
2927 flags |= IWN_TX_NEED_ACK;
2929 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
2930 (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_BAR))
2931 flags |= IWN_TX_IMM_BA; /* Cannot happen yet. */
2933 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG)
2934 flags |= IWN_TX_MORE_FRAG; /* Cannot happen yet. */
2936 /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
2937 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2938 /* NB: Group frames are sent using CCK in 802.11b/g. */
2939 if (totlen + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) {
2940 flags |= IWN_TX_NEED_RTS;
2941 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
2942 ridx >= IWN_RIDX_OFDM6) {
2943 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
2944 flags |= IWN_TX_NEED_CTS;
2945 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
2946 flags |= IWN_TX_NEED_RTS;
2948 if (flags & (IWN_TX_NEED_RTS | IWN_TX_NEED_CTS)) {
2949 if (sc->hw_type != IWN_HW_REV_TYPE_4965) {
2950 /* 5000 autoselects RTS/CTS or CTS-to-self. */
2951 flags &= ~(IWN_TX_NEED_RTS | IWN_TX_NEED_CTS);
2952 flags |= IWN_TX_NEED_PROTECTION;
2954 flags |= IWN_TX_FULL_TXOP;
2958 if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
2959 type != IEEE80211_FC0_TYPE_DATA)
2960 tx->id = hal->broadcast_id;
2964 if (type == IEEE80211_FC0_TYPE_MGT) {
2965 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2967 /* Tell HW to set timestamp in probe responses. */
2968 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2969 flags |= IWN_TX_INSERT_TSTAMP;
2971 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
2972 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
2973 tx->timeout = htole16(3);
2975 tx->timeout = htole16(2);
2977 tx->timeout = htole16(0);
2980 /* First segment length must be a multiple of 4. */
2981 flags |= IWN_TX_NEED_PADDING;
2982 pad = 4 - (hdrlen & 3);
2986 tx->len = htole16(totlen);
2988 tx->rts_ntries = 60;
2989 tx->data_ntries = 15;
2990 tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
2991 tx->plcp = rinfo->plcp;
2992 tx->rflags = rinfo->flags;
2993 if (tx->id == hal->broadcast_id) {
2994 /* Group or management frame. */
2996 /* XXX Alternate between antenna A and B? */
2997 txant = IWN_LSB(sc->txchainmask);
2998 tx->rflags |= IWN_RFLAG_ANT(txant);
3000 tx->linkq = IWN_RIDX_OFDM54 - ridx;
3001 flags |= IWN_TX_LINKQ; /* enable MRR */
3004 /* Set physical address of "scratch area". */
3005 tx->loaddr = htole32(IWN_LOADDR(data->scratch_paddr));
3006 tx->hiaddr = IWN_HIADDR(data->scratch_paddr);
3008 /* Copy 802.11 header in TX command. */
3009 memcpy((uint8_t *)(tx + 1), wh, hdrlen);
3011 /* Trim 802.11 header. */
3014 tx->flags = htole32(flags);
3017 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
3018 m, segs, &nsegs, BUS_DMA_NOWAIT);
3019 if (error == EFBIG) {
3020 /* too many fragments, linearize */
3021 mnew = m_collapse(m, M_DONTWAIT, IWN_MAX_SCATTER);
3023 device_printf(sc->sc_dev,
3024 "%s: could not defrag mbuf\n", __func__);
3029 error = bus_dmamap_load_mbuf_sg(ring->data_dmat,
3030 data->map, m, segs, &nsegs, BUS_DMA_NOWAIT);
3033 device_printf(sc->sc_dev,
3034 "%s: bus_dmamap_load_mbuf_sg failed, error %d\n",
3044 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n",
3045 __func__, ring->qid, ring->cur, m->m_pkthdr.len, nsegs);
3047 /* Fill TX descriptor. */
3048 desc->nsegs = 1 + nsegs;
3049 /* First DMA segment is used by the TX command. */
3050 desc->segs[0].addr = htole32(IWN_LOADDR(data->cmd_paddr));
3051 desc->segs[0].len = htole16(IWN_HIADDR(data->cmd_paddr) |
3052 (4 + sizeof (*tx) + hdrlen + pad) << 4);
3053 /* Other DMA segments are for data payload. */
3054 for (i = 1; i <= nsegs; i++) {
3055 desc->segs[i].addr = htole32(IWN_LOADDR(segs[i - 1].ds_addr));
3056 desc->segs[i].len = htole16(IWN_HIADDR(segs[i - 1].ds_addr) |
3057 segs[i - 1].ds_len << 4);
3060 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
3061 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3062 BUS_DMASYNC_PREWRITE);
3063 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3064 BUS_DMASYNC_PREWRITE);
3067 /* Update TX scheduler. */
3068 hal->update_sched(sc, ring->qid, ring->cur, tx->id, totlen);
3072 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
3073 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
3075 /* Mark TX ring as full if we reach a certain threshold. */
3076 if (++ring->queued > IWN_TX_RING_HIMARK)
3077 sc->qfullmsk |= 1 << ring->qid;
3083 iwn_tx_data_raw(struct iwn_softc *sc, struct mbuf *m,
3084 struct ieee80211_node *ni, struct iwn_tx_ring *ring,
3085 const struct ieee80211_bpf_params *params)
3087 const struct iwn_hal *hal = sc->sc_hal;
3088 const struct iwn_rate *rinfo;
3089 struct ifnet *ifp = sc->sc_ifp;
3090 struct ieee80211vap *vap = ni->ni_vap;
3091 struct ieee80211com *ic = ifp->if_l2com;
3092 struct iwn_tx_cmd *cmd;
3093 struct iwn_cmd_data *tx;
3094 struct ieee80211_frame *wh;
3095 struct iwn_tx_desc *desc;
3096 struct iwn_tx_data *data;
3099 bus_dma_segment_t segs[IWN_MAX_SCATTER];
3102 int totlen, error, pad, nsegs = 0, i, rate;
3103 uint8_t ridx, type, txant;
3105 IWN_LOCK_ASSERT(sc);
3107 wh = mtod(m, struct ieee80211_frame *);
3108 hdrlen = ieee80211_anyhdrsize(wh);
3109 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK;
3111 desc = &ring->desc[ring->cur];
3112 data = &ring->data[ring->cur];
3114 /* Choose a TX rate index. */
3115 rate = params->ibp_rate0;
3116 if (!ieee80211_isratevalid(ic->ic_rt, rate)) {
3117 /* XXX fall back to mcast/mgmt rate? */
3121 ridx = iwn_plcp_signal(rate);
3122 rinfo = &iwn_rates[ridx];
3124 totlen = m->m_pkthdr.len;
3126 /* Prepare TX firmware command. */
3127 cmd = &ring->cmd[ring->cur];
3128 cmd->code = IWN_CMD_TX_DATA;
3130 cmd->qid = ring->qid;
3131 cmd->idx = ring->cur;
3133 tx = (struct iwn_cmd_data *)cmd->data;
3134 /* NB: No need to clear tx, all fields are reinitialized here. */
3135 tx->scratch = 0; /* clear "scratch" area */
3138 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0)
3139 flags |= IWN_TX_NEED_ACK;
3140 if (params->ibp_flags & IEEE80211_BPF_RTS) {
3141 if (sc->hw_type != IWN_HW_REV_TYPE_4965) {
3142 /* 5000 autoselects RTS/CTS or CTS-to-self. */
3143 flags &= ~IWN_TX_NEED_RTS;
3144 flags |= IWN_TX_NEED_PROTECTION;
3146 flags |= IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP;
3148 if (params->ibp_flags & IEEE80211_BPF_CTS) {
3149 if (sc->hw_type != IWN_HW_REV_TYPE_4965) {
3150 /* 5000 autoselects RTS/CTS or CTS-to-self. */
3151 flags &= ~IWN_TX_NEED_CTS;
3152 flags |= IWN_TX_NEED_PROTECTION;
3154 flags |= IWN_TX_NEED_CTS | IWN_TX_FULL_TXOP;
3156 if (type == IEEE80211_FC0_TYPE_MGT) {
3157 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
3159 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
3160 flags |= IWN_TX_INSERT_TSTAMP;
3162 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ ||
3163 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ)
3164 tx->timeout = htole16(3);
3166 tx->timeout = htole16(2);
3168 tx->timeout = htole16(0);
3171 /* First segment length must be a multiple of 4. */
3172 flags |= IWN_TX_NEED_PADDING;
3173 pad = 4 - (hdrlen & 3);
3177 if (ieee80211_radiotap_active_vap(vap)) {
3178 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap;
3181 tap->wt_rate = rate;
3183 ieee80211_radiotap_tx(vap, m);
3186 tx->len = htole16(totlen);
3188 tx->id = hal->broadcast_id;
3189 tx->rts_ntries = params->ibp_try1;
3190 tx->data_ntries = params->ibp_try0;
3191 tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
3192 tx->plcp = rinfo->plcp;
3193 tx->rflags = rinfo->flags;
3194 /* Group or management frame. */
3196 txant = IWN_LSB(sc->txchainmask);
3197 tx->rflags |= IWN_RFLAG_ANT(txant);
3198 /* Set physical address of "scratch area". */
3199 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd);
3200 tx->loaddr = htole32(IWN_LOADDR(paddr));
3201 tx->hiaddr = IWN_HIADDR(paddr);
3203 /* Copy 802.11 header in TX command. */
3204 memcpy((uint8_t *)(tx + 1), wh, hdrlen);
3206 /* Trim 802.11 header. */
3209 tx->flags = htole32(flags);
3212 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map,
3213 m, segs, &nsegs, BUS_DMA_NOWAIT);
3214 if (error == EFBIG) {
3215 /* Too many fragments, linearize. */
3216 mnew = m_collapse(m, M_DONTWAIT, IWN_MAX_SCATTER);
3218 device_printf(sc->sc_dev,
3219 "%s: could not defrag mbuf\n", __func__);
3224 error = bus_dmamap_load_mbuf_sg(ring->data_dmat,
3225 data->map, m, segs, &nsegs, BUS_DMA_NOWAIT);
3228 device_printf(sc->sc_dev,
3229 "%s: bus_dmamap_load_mbuf_sg failed, error %d\n",
3239 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n",
3240 __func__, ring->qid, ring->cur, m->m_pkthdr.len, nsegs);
3242 /* Fill TX descriptor. */
3243 desc->nsegs = 1 + nsegs;
3244 /* First DMA segment is used by the TX command. */
3245 desc->segs[0].addr = htole32(IWN_LOADDR(data->cmd_paddr));
3246 desc->segs[0].len = htole16(IWN_HIADDR(data->cmd_paddr) |
3247 (4 + sizeof (*tx) + hdrlen + pad) << 4);
3248 /* Other DMA segments are for data payload. */
3249 for (i = 1; i <= nsegs; i++) {
3250 desc->segs[i].addr = htole32(IWN_LOADDR(segs[i - 1].ds_addr));
3251 desc->segs[i].len = htole16(IWN_HIADDR(segs[i - 1].ds_addr) |
3252 segs[i - 1].ds_len << 4);
3255 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
3256 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3257 BUS_DMASYNC_PREWRITE);
3258 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3259 BUS_DMASYNC_PREWRITE);
3262 /* Update TX scheduler. */
3263 hal->update_sched(sc, ring->qid, ring->cur, tx->id, totlen);
3267 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
3268 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
3270 /* Mark TX ring as full if we reach a certain threshold. */
3271 if (++ring->queued > IWN_TX_RING_HIMARK)
3272 sc->qfullmsk |= 1 << ring->qid;
3278 iwn_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3279 const struct ieee80211_bpf_params *params)
3281 struct ieee80211com *ic = ni->ni_ic;
3282 struct ifnet *ifp = ic->ic_ifp;
3283 struct iwn_softc *sc = ifp->if_softc;
3284 struct iwn_tx_ring *txq;
3287 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
3288 ieee80211_free_node(ni);
3295 txq = &sc->txq[M_WME_GETAC(m)];
3297 txq = &sc->txq[params->ibp_pri & 3];
3299 if (params == NULL) {
3301 * Legacy path; interpret frame contents to decide
3302 * precisely how to send the frame.
3304 error = iwn_tx_data(sc, m, ni, txq);
3307 * Caller supplied explicit parameters to use in
3308 * sending the frame.
3310 error = iwn_tx_data_raw(sc, m, ni, txq, params);
3313 /* NB: m is reclaimed on tx failure */
3314 ieee80211_free_node(ni);
3322 iwn_start(struct ifnet *ifp)
3324 struct iwn_softc *sc = ifp->if_softc;
3327 iwn_start_locked(ifp);
3332 iwn_start_locked(struct ifnet *ifp)
3334 struct iwn_softc *sc = ifp->if_softc;
3335 struct ieee80211_node *ni;
3336 struct iwn_tx_ring *txq;
3340 IWN_LOCK_ASSERT(sc);
3343 if (sc->qfullmsk != 0) {
3344 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3347 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
3350 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3351 pri = M_WME_GETAC(m);
3352 txq = &sc->txq[pri];
3353 if (iwn_tx_data(sc, m, ni, txq) != 0) {
3355 ieee80211_free_node(ni);
3358 sc->sc_tx_timer = 5;
3363 iwn_watchdog(struct iwn_softc *sc)
3365 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) {
3366 struct ifnet *ifp = sc->sc_ifp;
3367 struct ieee80211com *ic = ifp->if_l2com;
3369 if_printf(ifp, "device timeout\n");
3370 ieee80211_runtask(ic, &sc->sc_reinit_task);
3375 iwn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
3377 struct iwn_softc *sc = ifp->if_softc;
3378 struct ieee80211com *ic = ifp->if_l2com;
3379 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
3380 struct ifreq *ifr = (struct ifreq *) data;
3381 int error = 0, startall = 0, stop = 0;
3386 if (ifp->if_flags & IFF_UP) {
3387 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3388 iwn_init_locked(sc);
3389 if (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL)
3395 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3396 iwn_stop_locked(sc);
3400 ieee80211_start_all(ic);
3401 else if (vap != NULL && stop)
3402 ieee80211_stop(vap);
3405 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
3408 error = ether_ioctl(ifp, cmd, data);
3418 * Send a command to the firmware.
3421 iwn_cmd(struct iwn_softc *sc, int code, const void *buf, int size, int async)
3423 struct iwn_tx_ring *ring = &sc->txq[4];
3424 struct iwn_tx_desc *desc;
3425 struct iwn_tx_data *data;
3426 struct iwn_tx_cmd *cmd;
3431 IWN_LOCK_ASSERT(sc);
3433 desc = &ring->desc[ring->cur];
3434 data = &ring->data[ring->cur];
3437 if (size > sizeof cmd->data) {
3438 /* Command is too large to fit in a descriptor. */
3439 if (totlen > MCLBYTES)
3441 m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
3444 cmd = mtod(m, struct iwn_tx_cmd *);
3445 error = bus_dmamap_load(ring->data_dmat, data->map, cmd,
3446 totlen, iwn_dma_map_addr, &paddr, BUS_DMA_NOWAIT);
3453 cmd = &ring->cmd[ring->cur];
3454 paddr = data->cmd_paddr;
3459 cmd->qid = ring->qid;
3460 cmd->idx = ring->cur;
3461 memcpy(cmd->data, buf, size);
3464 desc->segs[0].addr = htole32(IWN_LOADDR(paddr));
3465 desc->segs[0].len = htole16(IWN_HIADDR(paddr) | totlen << 4);
3467 DPRINTF(sc, IWN_DEBUG_CMD, "%s: %s (0x%x) flags %d qid %d idx %d\n",
3468 __func__, iwn_intr_str(cmd->code), cmd->code,
3469 cmd->flags, cmd->qid, cmd->idx);
3471 if (size > sizeof cmd->data) {
3472 bus_dmamap_sync(ring->data_dmat, data->map,
3473 BUS_DMASYNC_PREWRITE);
3475 bus_dmamap_sync(ring->data_dmat, ring->cmd_dma.map,
3476 BUS_DMASYNC_PREWRITE);
3478 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
3479 BUS_DMASYNC_PREWRITE);
3482 /* Update TX scheduler. */
3483 sc->sc_hal->update_sched(sc, ring->qid, ring->cur, 0, 0);
3486 /* Kick command ring. */
3487 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT;
3488 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
3490 return async ? 0 : msleep(desc, &sc->sc_mtx, PCATCH, "iwncmd", hz);
3494 iwn4965_add_node(struct iwn_softc *sc, struct iwn_node_info *node, int async)
3496 struct iwn4965_node_info hnode;
3500 * We use the node structure for 5000 Series internally (it is
3501 * a superset of the one for 4965AGN). We thus copy the common
3502 * fields before sending the command.
3504 src = (caddr_t)node;
3505 dst = (caddr_t)&hnode;
3506 memcpy(dst, src, 48);
3507 /* Skip TSC, RX MIC and TX MIC fields from ``src''. */
3508 memcpy(dst + 48, src + 72, 20);
3509 return iwn_cmd(sc, IWN_CMD_ADD_NODE, &hnode, sizeof hnode, async);
3513 iwn5000_add_node(struct iwn_softc *sc, struct iwn_node_info *node, int async)
3515 /* Direct mapping. */
3516 return iwn_cmd(sc, IWN_CMD_ADD_NODE, node, sizeof (*node), async);
3520 static const uint8_t iwn_ridx_to_plcp[] = {
3521 10, 20, 55, 110, /* CCK */
3522 0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3, 0x3 /* OFDM R1-R4 */
3524 static const uint8_t iwn_siso_mcs_to_plcp[] = {
3525 0, 0, 0, 0, /* CCK */
3526 0, 0, 1, 2, 3, 4, 5, 6, 7 /* HT */
3528 static const uint8_t iwn_mimo_mcs_to_plcp[] = {
3529 0, 0, 0, 0, /* CCK */
3530 8, 8, 9, 10, 11, 12, 13, 14, 15 /* HT */
3533 static const uint8_t iwn_prev_ridx[] = {
3534 /* NB: allow fallback from CCK11 to OFDM9 and from OFDM6 to CCK5 */
3535 0, 0, 1, 5, /* CCK */
3536 2, 4, 3, 6, 7, 8, 9, 10, 10 /* OFDM */
3540 * Configure hardware link parameters for the specified
3541 * node operating on the specified channel.
3544 iwn_set_link_quality(struct iwn_softc *sc, uint8_t id, int async)
3546 struct ifnet *ifp = sc->sc_ifp;
3547 struct ieee80211com *ic = ifp->if_l2com;
3548 struct iwn_cmd_link_quality linkq;
3549 const struct iwn_rate *rinfo;
3551 uint8_t txant, ridx;
3553 /* Use the first valid TX antenna. */
3554 txant = IWN_LSB(sc->txchainmask);
3556 memset(&linkq, 0, sizeof linkq);
3558 linkq.antmsk_1stream = txant;
3559 linkq.antmsk_2stream = IWN_ANT_AB;
3560 linkq.ampdu_max = 31;
3561 linkq.ampdu_threshold = 3;
3562 linkq.ampdu_limit = htole16(4000); /* 4ms */
3565 if (IEEE80211_IS_CHAN_HT(c))
3569 if (id == IWN_ID_BSS)
3570 ridx = IWN_RIDX_OFDM54;
3571 else if (IEEE80211_IS_CHAN_A(ic->ic_curchan))
3572 ridx = IWN_RIDX_OFDM6;
3574 ridx = IWN_RIDX_CCK1;
3576 for (i = 0; i < IWN_MAX_TX_RETRIES; i++) {
3577 rinfo = &iwn_rates[ridx];
3579 if (IEEE80211_IS_CHAN_HT40(c)) {
3580 linkq.retry[i].plcp = iwn_mimo_mcs_to_plcp[ridx]
3582 linkq.retry[i].rflags = IWN_RFLAG_HT
3585 } else if (IEEE80211_IS_CHAN_HT(c)) {
3586 linkq.retry[i].plcp = iwn_siso_mcs_to_plcp[ridx]
3588 linkq.retry[i].rflags = IWN_RFLAG_HT;
3593 linkq.retry[i].plcp = rinfo->plcp;
3594 linkq.retry[i].rflags = rinfo->flags;
3596 linkq.retry[i].rflags |= IWN_RFLAG_ANT(txant);
3597 ridx = iwn_prev_ridx[ridx];
3600 if (sc->sc_debug & IWN_DEBUG_STATE) {
3601 printf("%s: set link quality for node %d, mimo %d ssmask %d\n",
3602 __func__, id, linkq.mimo, linkq.antmsk_1stream);
3603 printf("%s:", __func__);
3604 for (i = 0; i < IWN_MAX_TX_RETRIES; i++)
3605 printf(" %d:%x", linkq.retry[i].plcp,
3606 linkq.retry[i].rflags);
3610 return iwn_cmd(sc, IWN_CMD_LINK_QUALITY, &linkq, sizeof linkq, async);
3614 * Broadcast node is used to send group-addressed and management frames.
3617 iwn_add_broadcast_node(struct iwn_softc *sc, int async)
3619 const struct iwn_hal *hal = sc->sc_hal;
3620 struct ifnet *ifp = sc->sc_ifp;
3621 struct iwn_node_info node;
3624 memset(&node, 0, sizeof node);
3625 IEEE80211_ADDR_COPY(node.macaddr, ifp->if_broadcastaddr);
3626 node.id = hal->broadcast_id;
3627 DPRINTF(sc, IWN_DEBUG_RESET, "%s: adding broadcast node\n", __func__);
3628 error = hal->add_node(sc, &node, async);
3632 error = iwn_set_link_quality(sc, hal->broadcast_id, async);
3637 iwn_wme_update(struct ieee80211com *ic)
3639 #define IWN_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
3640 #define IWN_TXOP_TO_US(v) (v<<5)
3641 struct iwn_softc *sc = ic->ic_ifp->if_softc;
3642 struct iwn_edca_params cmd;
3645 memset(&cmd, 0, sizeof cmd);
3646 cmd.flags = htole32(IWN_EDCA_UPDATE);
3647 for (i = 0; i < WME_NUM_AC; i++) {
3648 const struct wmeParams *wmep =
3649 &ic->ic_wme.wme_chanParams.cap_wmeParams[i];
3650 cmd.ac[i].aifsn = wmep->wmep_aifsn;
3651 cmd.ac[i].cwmin = htole16(IWN_EXP2(wmep->wmep_logcwmin));
3652 cmd.ac[i].cwmax = htole16(IWN_EXP2(wmep->wmep_logcwmax));
3653 cmd.ac[i].txoplimit =
3654 htole16(IWN_TXOP_TO_US(wmep->wmep_txopLimit));
3656 IEEE80211_UNLOCK(ic);
3658 (void) iwn_cmd(sc, IWN_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1 /*async*/);
3662 #undef IWN_TXOP_TO_US
3667 iwn_update_mcast(struct ifnet *ifp)
3673 iwn_set_led(struct iwn_softc *sc, uint8_t which, uint8_t off, uint8_t on)
3675 struct iwn_cmd_led led;
3677 /* Clear microcode LED ownership. */
3678 IWN_CLRBITS(sc, IWN_LED, IWN_LED_BSM_CTRL);
3681 led.unit = htole32(10000); /* on/off in unit of 100ms */
3684 (void)iwn_cmd(sc, IWN_CMD_SET_LED, &led, sizeof led, 1);
3688 * Set the critical temperature at which the firmware will stop the radio
3692 iwn_set_critical_temp(struct iwn_softc *sc)
3694 struct iwn_critical_temp crit;
3697 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_CTEMP_STOP_RF);
3699 if (sc->hw_type == IWN_HW_REV_TYPE_5150)
3700 temp = (IWN_CTOK(110) - sc->temp_off) * -5;
3701 else if (sc->hw_type == IWN_HW_REV_TYPE_4965)
3702 temp = IWN_CTOK(110);
3705 memset(&crit, 0, sizeof crit);
3706 crit.tempR = htole32(temp);
3707 DPRINTF(sc, IWN_DEBUG_RESET, "setting critical temp to %d\n",
3709 return iwn_cmd(sc, IWN_CMD_SET_CRITICAL_TEMP, &crit, sizeof crit, 0);
3713 iwn_set_timing(struct iwn_softc *sc, struct ieee80211_node *ni)
3715 struct iwn_cmd_timing cmd;
3718 memset(&cmd, 0, sizeof cmd);
3719 memcpy(&cmd.tstamp, ni->ni_tstamp.data, sizeof (uint64_t));
3720 cmd.bintval = htole16(ni->ni_intval);
3721 cmd.lintval = htole16(10);
3723 /* Compute remaining time until next beacon. */
3724 val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */
3725 mod = le64toh(cmd.tstamp) % val;
3726 cmd.binitval = htole32((uint32_t)(val - mod));
3728 DPRINTF(sc, IWN_DEBUG_RESET, "timing bintval=%u tstamp=%ju, init=%u\n",
3729 ni->ni_intval, le64toh(cmd.tstamp), (uint32_t)(val - mod));
3731 return iwn_cmd(sc, IWN_CMD_TIMING, &cmd, sizeof cmd, 1);
3735 iwn4965_power_calibration(struct iwn_softc *sc, int temp)
3737 struct ifnet *ifp = sc->sc_ifp;
3738 struct ieee80211com *ic = ifp->if_l2com;
3740 /* Adjust TX power if need be (delta >= 3 degC.) */
3741 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: temperature %d->%d\n",
3742 __func__, sc->temp, temp);
3743 if (abs(temp - sc->temp) >= 3) {
3744 /* Record temperature of last calibration. */
3746 (void)iwn4965_set_txpower(sc, ic->ic_bsschan, 1);
3751 * Set TX power for current channel (each rate has its own power settings).
3752 * This function takes into account the regulatory information from EEPROM,
3753 * the current temperature and the current voltage.
3756 iwn4965_set_txpower(struct iwn_softc *sc, struct ieee80211_channel *ch,
3759 /* Fixed-point arithmetic division using a n-bit fractional part. */
3760 #define fdivround(a, b, n) \
3761 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n))
3762 /* Linear interpolation. */
3763 #define interpolate(x, x1, y1, x2, y2, n) \
3764 ((y1) + fdivround(((int)(x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n))
3766 static const int tdiv[IWN_NATTEN_GROUPS] = { 9, 8, 8, 8, 6 };
3767 struct ifnet *ifp = sc->sc_ifp;
3768 struct ieee80211com *ic = ifp->if_l2com;
3769 struct iwn_ucode_info *uc = &sc->ucode_info;
3770 struct iwn4965_cmd_txpower cmd;
3771 struct iwn4965_eeprom_chan_samples *chans;
3772 int32_t vdiff, tdiff;
3773 int i, c, grp, maxpwr;
3774 const uint8_t *rf_gain, *dsp_gain;
3777 /* Retrieve channel number. */
3778 chan = ieee80211_chan2ieee(ic, ch);
3779 DPRINTF(sc, IWN_DEBUG_RESET, "setting TX power for channel %d\n",
3782 memset(&cmd, 0, sizeof cmd);
3783 cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1;
3786 if (IEEE80211_IS_CHAN_5GHZ(ch)) {
3787 maxpwr = sc->maxpwr5GHz;
3788 rf_gain = iwn4965_rf_gain_5ghz;
3789 dsp_gain = iwn4965_dsp_gain_5ghz;
3791 maxpwr = sc->maxpwr2GHz;
3792 rf_gain = iwn4965_rf_gain_2ghz;
3793 dsp_gain = iwn4965_dsp_gain_2ghz;
3796 /* Compute voltage compensation. */
3797 vdiff = ((int32_t)le32toh(uc->volt) - sc->eeprom_voltage) / 7;
3802 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW,
3803 "%s: voltage compensation=%d (UCODE=%d, EEPROM=%d)\n",
3804 __func__, vdiff, le32toh(uc->volt), sc->eeprom_voltage);
3806 /* Get channel attenuation group. */
3807 if (chan <= 20) /* 1-20 */
3809 else if (chan <= 43) /* 34-43 */
3811 else if (chan <= 70) /* 44-70 */
3813 else if (chan <= 124) /* 71-124 */
3817 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW,
3818 "%s: chan %d, attenuation group=%d\n", __func__, chan, grp);
3820 /* Get channel sub-band. */
3821 for (i = 0; i < IWN_NBANDS; i++)
3822 if (sc->bands[i].lo != 0 &&
3823 sc->bands[i].lo <= chan && chan <= sc->bands[i].hi)
3825 if (i == IWN_NBANDS) /* Can't happen in real-life. */
3827 chans = sc->bands[i].chans;
3828 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW,
3829 "%s: chan %d sub-band=%d\n", __func__, chan, i);
3831 for (c = 0; c < 2; c++) {
3832 uint8_t power, gain, temp;
3833 int maxchpwr, pwr, ridx, idx;
3835 power = interpolate(chan,
3836 chans[0].num, chans[0].samples[c][1].power,
3837 chans[1].num, chans[1].samples[c][1].power, 1);
3838 gain = interpolate(chan,
3839 chans[0].num, chans[0].samples[c][1].gain,
3840 chans[1].num, chans[1].samples[c][1].gain, 1);
3841 temp = interpolate(chan,
3842 chans[0].num, chans[0].samples[c][1].temp,
3843 chans[1].num, chans[1].samples[c][1].temp, 1);
3844 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW,
3845 "%s: Tx chain %d: power=%d gain=%d temp=%d\n",
3846 __func__, c, power, gain, temp);
3848 /* Compute temperature compensation. */
3849 tdiff = ((sc->temp - temp) * 2) / tdiv[grp];
3850 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW,
3851 "%s: temperature compensation=%d (current=%d, EEPROM=%d)\n",
3852 __func__, tdiff, sc->temp, temp);
3854 for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++) {
3855 /* Convert dBm to half-dBm. */
3856 maxchpwr = sc->maxpwr[chan] * 2;
3858 maxchpwr -= 6; /* MIMO 2T: -3dB */
3862 /* Adjust TX power based on rate. */
3863 if ((ridx % 8) == 5)
3864 pwr -= 15; /* OFDM48: -7.5dB */
3865 else if ((ridx % 8) == 6)
3866 pwr -= 17; /* OFDM54: -8.5dB */
3867 else if ((ridx % 8) == 7)
3868 pwr -= 20; /* OFDM60: -10dB */
3870 pwr -= 10; /* Others: -5dB */
3872 /* Do not exceed channel max TX power. */
3876 idx = gain - (pwr - power) - tdiff - vdiff;
3877 if ((ridx / 8) & 1) /* MIMO */
3878 idx += (int32_t)le32toh(uc->atten[grp][c]);
3881 idx += 9; /* 5GHz */
3882 if (ridx == IWN_RIDX_MAX)
3885 /* Make sure idx stays in a valid range. */
3888 else if (idx > IWN4965_MAX_PWR_INDEX)
3889 idx = IWN4965_MAX_PWR_INDEX;
3891 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW,
3892 "%s: Tx chain %d, rate idx %d: power=%d\n",
3893 __func__, c, ridx, idx);
3894 cmd.power[ridx].rf_gain[c] = rf_gain[idx];
3895 cmd.power[ridx].dsp_gain[c] = dsp_gain[idx];
3899 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW,
3900 "%s: set tx power for chan %d\n", __func__, chan);
3901 return iwn_cmd(sc, IWN_CMD_TXPOWER, &cmd, sizeof cmd, async);
3908 iwn5000_set_txpower(struct iwn_softc *sc, struct ieee80211_channel *ch,
3911 struct iwn5000_cmd_txpower cmd;
3914 * TX power calibration is handled automatically by the firmware
3917 memset(&cmd, 0, sizeof cmd);
3918 cmd.global_limit = 2 * IWN5000_TXPOWER_MAX_DBM; /* 16 dBm */
3919 cmd.flags = IWN5000_TXPOWER_NO_CLOSED;
3920 cmd.srv_limit = IWN5000_TXPOWER_AUTO;
3921 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: setting TX power\n", __func__);
3922 return iwn_cmd(sc, IWN_CMD_TXPOWER_DBM, &cmd, sizeof cmd, async);
3926 * Retrieve the maximum RSSI (in dBm) among receivers.
3929 iwn4965_get_rssi(struct iwn_softc *sc, struct iwn_rx_stat *stat)
3931 struct iwn4965_rx_phystat *phy = (void *)stat->phybuf;
3935 mask = (le16toh(phy->antenna) >> 4) & IWN_ANT_ABC;
3936 agc = (le16toh(phy->agc) >> 7) & 0x7f;
3940 if (mask & IWN_ANT_A) /* Ant A */
3941 rssi = max(rssi, phy->rssi[0]);
3942 if (mask & IWN_ATH_B) /* Ant B */
3943 rssi = max(rssi, phy->rssi[2]);
3944 if (mask & IWN_ANT_C) /* Ant C */
3945 rssi = max(rssi, phy->rssi[4]);
3947 rssi = max(rssi, phy->rssi[0]);
3948 rssi = max(rssi, phy->rssi[2]);
3949 rssi = max(rssi, phy->rssi[4]);
3952 DPRINTF(sc, IWN_DEBUG_RECV, "%s: agc %d mask 0x%x rssi %d %d %d "
3953 "result %d\n", __func__, agc, mask,
3954 phy->rssi[0], phy->rssi[2], phy->rssi[4],
3955 rssi - agc - IWN_RSSI_TO_DBM);
3956 return rssi - agc - IWN_RSSI_TO_DBM;
3960 iwn5000_get_rssi(struct iwn_softc *sc, struct iwn_rx_stat *stat)
3962 struct iwn5000_rx_phystat *phy = (void *)stat->phybuf;
3966 agc = (le32toh(phy->agc) >> 9) & 0x7f;
3968 rssi = MAX(le16toh(phy->rssi[0]) & 0xff,
3969 le16toh(phy->rssi[1]) & 0xff);
3970 rssi = MAX(le16toh(phy->rssi[2]) & 0xff, rssi);
3972 DPRINTF(sc, IWN_DEBUG_RECV, "%s: agc %d rssi %d %d %d "
3973 "result %d\n", __func__, agc,
3974 phy->rssi[0], phy->rssi[1], phy->rssi[2],
3975 rssi - agc - IWN_RSSI_TO_DBM);
3976 return rssi - agc - IWN_RSSI_TO_DBM;
3980 * Retrieve the average noise (in dBm) among receivers.
3983 iwn_get_noise(const struct iwn_rx_general_stats *stats)
3985 int i, total, nbant, noise;
3988 for (i = 0; i < 3; i++) {
3989 if ((noise = le32toh(stats->noise[i]) & 0xff) == 0)
3994 /* There should be at least one antenna but check anyway. */
3995 return (nbant == 0) ? -127 : (total / nbant) - 107;
3999 * Compute temperature (in degC) from last received statistics.
4002 iwn4965_get_temperature(struct iwn_softc *sc)
4004 struct iwn_ucode_info *uc = &sc->ucode_info;
4005 int32_t r1, r2, r3, r4, temp;
4007 r1 = le32toh(uc->temp[0].chan20MHz);
4008 r2 = le32toh(uc->temp[1].chan20MHz);
4009 r3 = le32toh(uc->temp[2].chan20MHz);
4010 r4 = le32toh(sc->rawtemp);
4012 if (r1 == r3) /* Prevents division by 0 (should not happen.) */
4015 /* Sign-extend 23-bit R4 value to 32-bit. */
4016 r4 = (r4 << 8) >> 8;
4017 /* Compute temperature in Kelvin. */
4018 temp = (259 * (r4 - r2)) / (r3 - r1);
4019 temp = (temp * 97) / 100 + 8;
4021 DPRINTF(sc, IWN_DEBUG_ANY, "temperature %dK/%dC\n", temp,
4023 return IWN_KTOC(temp);
4027 iwn5000_get_temperature(struct iwn_softc *sc)
4032 * Temperature is not used by the driver for 5000 Series because
4033 * TX power calibration is handled by firmware. We export it to
4034 * users through the sensor framework though.
4036 temp = le32toh(sc->rawtemp);
4037 if (sc->hw_type == IWN_HW_REV_TYPE_5150) {
4038 temp = (temp / -5) + sc->temp_off;
4039 temp = IWN_KTOC(temp);
4045 * Initialize sensitivity calibration state machine.
4048 iwn_init_sensitivity(struct iwn_softc *sc)
4050 const struct iwn_hal *hal = sc->sc_hal;
4051 struct iwn_calib_state *calib = &sc->calib;
4055 /* Reset calibration state machine. */
4056 memset(calib, 0, sizeof (*calib));
4057 calib->state = IWN_CALIB_STATE_INIT;
4058 calib->cck_state = IWN_CCK_STATE_HIFA;
4059 /* Set initial correlation values. */
4060 calib->ofdm_x1 = sc->limits->min_ofdm_x1;
4061 calib->ofdm_mrc_x1 = sc->limits->min_ofdm_mrc_x1;
4062 calib->ofdm_x4 = sc->limits->min_ofdm_x4;
4063 calib->ofdm_mrc_x4 = sc->limits->min_ofdm_mrc_x4;
4064 calib->cck_x4 = 125;
4065 calib->cck_mrc_x4 = sc->limits->min_cck_mrc_x4;
4066 calib->energy_cck = sc->limits->energy_cck;
4068 /* Write initial sensitivity. */
4069 error = iwn_send_sensitivity(sc);
4073 /* Write initial gains. */
4074 error = hal->init_gains(sc);
4078 /* Request statistics at each beacon interval. */
4080 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: calibrate phy\n", __func__);
4081 return iwn_cmd(sc, IWN_CMD_GET_STATISTICS, &flags, sizeof flags, 1);
4085 * Collect noise and RSSI statistics for the first 20 beacons received
4086 * after association and use them to determine connected antennas and
4087 * to set differential gains.
4090 iwn_collect_noise(struct iwn_softc *sc,
4091 const struct iwn_rx_general_stats *stats)
4093 const struct iwn_hal *hal = sc->sc_hal;
4094 struct iwn_calib_state *calib = &sc->calib;
4098 /* Accumulate RSSI and noise for all 3 antennas. */
4099 for (i = 0; i < 3; i++) {
4100 calib->rssi[i] += le32toh(stats->rssi[i]) & 0xff;
4101 calib->noise[i] += le32toh(stats->noise[i]) & 0xff;
4103 /* NB: We update differential gains only once after 20 beacons. */
4104 if (++calib->nbeacons < 20)
4107 /* Determine highest average RSSI. */
4108 val = MAX(calib->rssi[0], calib->rssi[1]);
4109 val = MAX(calib->rssi[2], val);
4111 /* Determine which antennas are connected. */
4112 sc->chainmask = sc->rxchainmask;
4113 for (i = 0; i < 3; i++)
4114 if (val - calib->rssi[i] > 15 * 20)
4115 sc->chainmask &= ~(1 << i);
4116 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4117 "%s: RX chains mask: theoretical=0x%x, actual=0x%x\n",
4118 __func__, sc->rxchainmask, sc->chainmask);
4120 /* If none of the TX antennas are connected, keep at least one. */
4121 if ((sc->chainmask & sc->txchainmask) == 0)
4122 sc->chainmask |= IWN_LSB(sc->txchainmask);
4124 (void)hal->set_gains(sc);
4125 calib->state = IWN_CALIB_STATE_RUN;
4128 /* XXX Disable RX chains with no antennas connected. */
4129 sc->rxon.rxchain = htole16(IWN_RXCHAIN_SEL(sc->chainmask));
4130 (void)iwn_cmd(sc, IWN_CMD_RXON, &sc->rxon, hal->rxonsz, 1);
4135 /* Enable power-saving mode if requested by user. */
4136 if (sc->sc_ic.ic_flags & IEEE80211_F_PMGTON)
4137 (void)iwn_set_pslevel(sc, 0, 3, 1);
4142 iwn4965_init_gains(struct iwn_softc *sc)
4144 struct iwn_phy_calib_gain cmd;
4146 memset(&cmd, 0, sizeof cmd);
4147 cmd.code = IWN4965_PHY_CALIB_DIFF_GAIN;
4148 /* Differential gains initially set to 0 for all 3 antennas. */
4149 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4150 "%s: setting initial differential gains\n", __func__);
4151 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
4155 iwn5000_init_gains(struct iwn_softc *sc)
4157 struct iwn_phy_calib cmd;
4159 memset(&cmd, 0, sizeof cmd);
4160 cmd.code = IWN5000_PHY_CALIB_RESET_NOISE_GAIN;
4163 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4164 "%s: setting initial differential gains\n", __func__);
4165 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
4169 iwn4965_set_gains(struct iwn_softc *sc)
4171 struct iwn_calib_state *calib = &sc->calib;
4172 struct iwn_phy_calib_gain cmd;
4173 int i, delta, noise;
4175 /* Get minimal noise among connected antennas. */
4176 noise = INT_MAX; /* NB: There's at least one antenna. */
4177 for (i = 0; i < 3; i++)
4178 if (sc->chainmask & (1 << i))
4179 noise = MIN(calib->noise[i], noise);
4181 memset(&cmd, 0, sizeof cmd);
4182 cmd.code = IWN4965_PHY_CALIB_DIFF_GAIN;
4183 /* Set differential gains for connected antennas. */
4184 for (i = 0; i < 3; i++) {
4185 if (sc->chainmask & (1 << i)) {
4186 /* Compute attenuation (in unit of 1.5dB). */
4187 delta = (noise - (int32_t)calib->noise[i]) / 30;
4188 /* NB: delta <= 0 */
4189 /* Limit to [-4.5dB,0]. */
4190 cmd.gain[i] = MIN(abs(delta), 3);
4192 cmd.gain[i] |= 1 << 2; /* sign bit */
4195 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4196 "setting differential gains Ant A/B/C: %x/%x/%x (%x)\n",
4197 cmd.gain[0], cmd.gain[1], cmd.gain[2], sc->chainmask);
4198 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
4202 iwn5000_set_gains(struct iwn_softc *sc)
4204 struct iwn_calib_state *calib = &sc->calib;
4205 struct iwn_phy_calib_gain cmd;
4206 int i, ant, delta, div;
4208 /* We collected 20 beacons and !=6050 need a 1.5 factor. */
4209 div = (sc->hw_type == IWN_HW_REV_TYPE_6050) ? 20 : 30;
4211 memset(&cmd, 0, sizeof cmd);
4212 cmd.code = IWN5000_PHY_CALIB_NOISE_GAIN;
4215 /* Get first available RX antenna as referential. */
4216 ant = IWN_LSB(sc->rxchainmask);
4217 /* Set differential gains for other antennas. */
4218 for (i = ant + 1; i < 3; i++) {
4219 if (sc->chainmask & (1 << i)) {
4220 /* The delta is relative to antenna "ant". */
4221 delta = ((int32_t)calib->noise[ant] -
4222 (int32_t)calib->noise[i]) / div;
4223 /* Limit to [-4.5dB,+4.5dB]. */
4224 cmd.gain[i - 1] = MIN(abs(delta), 3);
4226 cmd.gain[i - 1] |= 1 << 2; /* sign bit */
4229 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4230 "setting differential gains Ant B/C: %x/%x (%x)\n",
4231 cmd.gain[0], cmd.gain[1], sc->chainmask);
4232 return iwn_cmd(sc, IWN_CMD_PHY_CALIB, &cmd, sizeof cmd, 1);
4236 * Tune RF RX sensitivity based on the number of false alarms detected
4237 * during the last beacon period.
4240 iwn_tune_sensitivity(struct iwn_softc *sc, const struct iwn_rx_stats *stats)
4242 #define inc(val, inc, max) \
4243 if ((val) < (max)) { \
4244 if ((val) < (max) - (inc)) \
4250 #define dec(val, dec, min) \
4251 if ((val) > (min)) { \
4252 if ((val) > (min) + (dec)) \
4259 const struct iwn_sensitivity_limits *limits = sc->limits;
4260 struct iwn_calib_state *calib = &sc->calib;
4261 uint32_t val, rxena, fa;
4262 uint32_t energy[3], energy_min;
4263 uint8_t noise[3], noise_ref;
4264 int i, needs_update = 0;
4266 /* Check that we've been enabled long enough. */
4267 rxena = le32toh(stats->general.load);
4271 /* Compute number of false alarms since last call for OFDM. */
4272 fa = le32toh(stats->ofdm.bad_plcp) - calib->bad_plcp_ofdm;
4273 fa += le32toh(stats->ofdm.fa) - calib->fa_ofdm;
4274 fa *= 200 * 1024; /* 200TU */
4276 /* Save counters values for next call. */
4277 calib->bad_plcp_ofdm = le32toh(stats->ofdm.bad_plcp);
4278 calib->fa_ofdm = le32toh(stats->ofdm.fa);
4280 if (fa > 50 * rxena) {
4281 /* High false alarm count, decrease sensitivity. */
4282 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4283 "%s: OFDM high false alarm count: %u\n", __func__, fa);
4284 inc(calib->ofdm_x1, 1, limits->max_ofdm_x1);
4285 inc(calib->ofdm_mrc_x1, 1, limits->max_ofdm_mrc_x1);
4286 inc(calib->ofdm_x4, 1, limits->max_ofdm_x4);
4287 inc(calib->ofdm_mrc_x4, 1, limits->max_ofdm_mrc_x4);
4289 } else if (fa < 5 * rxena) {
4290 /* Low false alarm count, increase sensitivity. */
4291 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4292 "%s: OFDM low false alarm count: %u\n", __func__, fa);
4293 dec(calib->ofdm_x1, 1, limits->min_ofdm_x1);
4294 dec(calib->ofdm_mrc_x1, 1, limits->min_ofdm_mrc_x1);
4295 dec(calib->ofdm_x4, 1, limits->min_ofdm_x4);
4296 dec(calib->ofdm_mrc_x4, 1, limits->min_ofdm_mrc_x4);
4299 /* Compute maximum noise among 3 receivers. */
4300 for (i = 0; i < 3; i++)
4301 noise[i] = (le32toh(stats->general.noise[i]) >> 8) & 0xff;
4302 val = MAX(noise[0], noise[1]);
4303 val = MAX(noise[2], val);
4304 /* Insert it into our samples table. */
4305 calib->noise_samples[calib->cur_noise_sample] = val;
4306 calib->cur_noise_sample = (calib->cur_noise_sample + 1) % 20;
4308 /* Compute maximum noise among last 20 samples. */
4309 noise_ref = calib->noise_samples[0];
4310 for (i = 1; i < 20; i++)
4311 noise_ref = MAX(noise_ref, calib->noise_samples[i]);
4313 /* Compute maximum energy among 3 receivers. */
4314 for (i = 0; i < 3; i++)
4315 energy[i] = le32toh(stats->general.energy[i]);
4316 val = MIN(energy[0], energy[1]);
4317 val = MIN(energy[2], val);
4318 /* Insert it into our samples table. */
4319 calib->energy_samples[calib->cur_energy_sample] = val;
4320 calib->cur_energy_sample = (calib->cur_energy_sample + 1) % 10;
4322 /* Compute minimum energy among last 10 samples. */
4323 energy_min = calib->energy_samples[0];
4324 for (i = 1; i < 10; i++)
4325 energy_min = MAX(energy_min, calib->energy_samples[i]);
4328 /* Compute number of false alarms since last call for CCK. */
4329 fa = le32toh(stats->cck.bad_plcp) - calib->bad_plcp_cck;
4330 fa += le32toh(stats->cck.fa) - calib->fa_cck;
4331 fa *= 200 * 1024; /* 200TU */
4333 /* Save counters values for next call. */
4334 calib->bad_plcp_cck = le32toh(stats->cck.bad_plcp);
4335 calib->fa_cck = le32toh(stats->cck.fa);
4337 if (fa > 50 * rxena) {
4338 /* High false alarm count, decrease sensitivity. */
4339 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4340 "%s: CCK high false alarm count: %u\n", __func__, fa);
4341 calib->cck_state = IWN_CCK_STATE_HIFA;
4344 if (calib->cck_x4 > 160) {
4345 calib->noise_ref = noise_ref;
4346 if (calib->energy_cck > 2)
4347 dec(calib->energy_cck, 2, energy_min);
4349 if (calib->cck_x4 < 160) {
4350 calib->cck_x4 = 161;
4353 inc(calib->cck_x4, 3, limits->max_cck_x4);
4355 inc(calib->cck_mrc_x4, 3, limits->max_cck_mrc_x4);
4357 } else if (fa < 5 * rxena) {
4358 /* Low false alarm count, increase sensitivity. */
4359 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4360 "%s: CCK low false alarm count: %u\n", __func__, fa);
4361 calib->cck_state = IWN_CCK_STATE_LOFA;
4364 if (calib->cck_state != IWN_CCK_STATE_INIT &&
4365 (((int32_t)calib->noise_ref - (int32_t)noise_ref) > 2 ||
4366 calib->low_fa > 100)) {
4367 inc(calib->energy_cck, 2, limits->min_energy_cck);
4368 dec(calib->cck_x4, 3, limits->min_cck_x4);
4369 dec(calib->cck_mrc_x4, 3, limits->min_cck_mrc_x4);
4372 /* Not worth to increase or decrease sensitivity. */
4373 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4374 "%s: CCK normal false alarm count: %u\n", __func__, fa);
4376 calib->noise_ref = noise_ref;
4378 if (calib->cck_state == IWN_CCK_STATE_HIFA) {
4379 /* Previous interval had many false alarms. */
4380 dec(calib->energy_cck, 8, energy_min);
4382 calib->cck_state = IWN_CCK_STATE_INIT;
4386 (void)iwn_send_sensitivity(sc);
4392 iwn_send_sensitivity(struct iwn_softc *sc)
4394 struct iwn_calib_state *calib = &sc->calib;
4395 struct iwn_sensitivity_cmd cmd;
4397 memset(&cmd, 0, sizeof cmd);
4398 cmd.which = IWN_SENSITIVITY_WORKTBL;
4399 /* OFDM modulation. */
4400 cmd.corr_ofdm_x1 = htole16(calib->ofdm_x1);
4401 cmd.corr_ofdm_mrc_x1 = htole16(calib->ofdm_mrc_x1);
4402 cmd.corr_ofdm_x4 = htole16(calib->ofdm_x4);
4403 cmd.corr_ofdm_mrc_x4 = htole16(calib->ofdm_mrc_x4);
4404 cmd.energy_ofdm = htole16(sc->limits->energy_ofdm);
4405 cmd.energy_ofdm_th = htole16(62);
4406 /* CCK modulation. */
4407 cmd.corr_cck_x4 = htole16(calib->cck_x4);
4408 cmd.corr_cck_mrc_x4 = htole16(calib->cck_mrc_x4);
4409 cmd.energy_cck = htole16(calib->energy_cck);
4410 /* Barker modulation: use default values. */
4411 cmd.corr_barker = htole16(190);
4412 cmd.corr_barker_mrc = htole16(390);
4414 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
4415 "%s: set sensitivity %d/%d/%d/%d/%d/%d/%d\n", __func__,
4416 calib->ofdm_x1, calib->ofdm_mrc_x1, calib->ofdm_x4,
4417 calib->ofdm_mrc_x4, calib->cck_x4,
4418 calib->cck_mrc_x4, calib->energy_cck);
4419 return iwn_cmd(sc, IWN_CMD_SET_SENSITIVITY, &cmd, sizeof cmd, 1);
4423 * Set STA mode power saving level (between 0 and 5).
4424 * Level 0 is CAM (Continuously Aware Mode), 5 is for maximum power saving.
4427 iwn_set_pslevel(struct iwn_softc *sc, int dtim, int level, int async)
4429 const struct iwn_pmgt *pmgt;
4430 struct iwn_pmgt_cmd cmd;
4431 uint32_t max, skip_dtim;
4435 /* Select which PS parameters to use. */
4437 pmgt = &iwn_pmgt[0][level];
4438 else if (dtim <= 10)
4439 pmgt = &iwn_pmgt[1][level];
4441 pmgt = &iwn_pmgt[2][level];
4443 memset(&cmd, 0, sizeof cmd);
4444 if (level != 0) /* not CAM */
4445 cmd.flags |= htole16(IWN_PS_ALLOW_SLEEP);
4447 cmd.flags |= htole16(IWN_PS_FAST_PD);
4448 /* Retrieve PCIe Active State Power Management (ASPM). */
4449 tmp = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
4450 if (!(tmp & 0x1)) /* L0s Entry disabled. */
4451 cmd.flags |= htole16(IWN_PS_PCI_PMGT);
4452 cmd.rxtimeout = htole32(pmgt->rxtimeout * 1024);
4453 cmd.txtimeout = htole32(pmgt->txtimeout * 1024);
4459 skip_dtim = pmgt->skip_dtim;
4460 if (skip_dtim != 0) {
4461 cmd.flags |= htole16(IWN_PS_SLEEP_OVER_DTIM);
4462 max = pmgt->intval[4];
4463 if (max == (uint32_t)-1)
4464 max = dtim * (skip_dtim + 1);
4465 else if (max > dtim)
4466 max = (max / dtim) * dtim;
4469 for (i = 0; i < 5; i++)
4470 cmd.intval[i] = htole32(MIN(max, pmgt->intval[i]));
4472 DPRINTF(sc, IWN_DEBUG_RESET, "setting power saving level to %d\n",
4474 return iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &cmd, sizeof cmd, async);
4478 iwn_config(struct iwn_softc *sc)
4480 const struct iwn_hal *hal = sc->sc_hal;
4481 struct ifnet *ifp = sc->sc_ifp;
4482 struct ieee80211com *ic = ifp->if_l2com;
4483 struct iwn_bluetooth bluetooth;
4488 /* Configure valid TX chains for 5000 Series. */
4489 if (sc->hw_type != IWN_HW_REV_TYPE_4965) {
4490 txmask = htole32(sc->txchainmask);
4491 DPRINTF(sc, IWN_DEBUG_RESET,
4492 "%s: configuring valid TX chains 0x%x\n", __func__, txmask);
4493 error = iwn_cmd(sc, IWN5000_CMD_TX_ANT_CONFIG, &txmask,
4496 device_printf(sc->sc_dev,
4497 "%s: could not configure valid TX chains, "
4498 "error %d\n", __func__, error);
4503 /* Configure bluetooth coexistence. */
4504 memset(&bluetooth, 0, sizeof bluetooth);
4505 bluetooth.flags = IWN_BT_COEX_CHAN_ANN | IWN_BT_COEX_BT_PRIO;
4506 bluetooth.lead_time = IWN_BT_LEAD_TIME_DEF;
4507 bluetooth.max_kill = IWN_BT_MAX_KILL_DEF;
4508 DPRINTF(sc, IWN_DEBUG_RESET, "%s: config bluetooth coexistence\n",
4510 error = iwn_cmd(sc, IWN_CMD_BT_COEX, &bluetooth, sizeof bluetooth, 0);
4512 device_printf(sc->sc_dev,
4513 "%s: could not configure bluetooth coexistence, error %d\n",
4518 /* Set mode, channel, RX filter and enable RX. */
4519 memset(&sc->rxon, 0, sizeof (struct iwn_rxon));
4520 IEEE80211_ADDR_COPY(sc->rxon.myaddr, IF_LLADDR(ifp));
4521 IEEE80211_ADDR_COPY(sc->rxon.wlap, IF_LLADDR(ifp));
4522 sc->rxon.chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
4523 sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF);
4524 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan))
4525 sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ);
4526 switch (ic->ic_opmode) {
4527 case IEEE80211_M_STA:
4528 sc->rxon.mode = IWN_MODE_STA;
4529 sc->rxon.filter = htole32(IWN_FILTER_MULTICAST);
4531 case IEEE80211_M_MONITOR:
4532 sc->rxon.mode = IWN_MODE_MONITOR;
4533 sc->rxon.filter = htole32(IWN_FILTER_MULTICAST |
4534 IWN_FILTER_CTL | IWN_FILTER_PROMISC);
4537 /* Should not get there. */
4540 sc->rxon.cck_mask = 0x0f; /* not yet negotiated */
4541 sc->rxon.ofdm_mask = 0xff; /* not yet negotiated */
4542 sc->rxon.ht_single_mask = 0xff;
4543 sc->rxon.ht_dual_mask = 0xff;
4544 sc->rxon.ht_triple_mask = 0xff;
4546 IWN_RXCHAIN_VALID(sc->rxchainmask) |
4547 IWN_RXCHAIN_MIMO_COUNT(2) |
4548 IWN_RXCHAIN_IDLE_COUNT(2);
4549 sc->rxon.rxchain = htole16(rxchain);
4550 DPRINTF(sc, IWN_DEBUG_RESET, "%s: setting configuration\n", __func__);
4551 error = iwn_cmd(sc, IWN_CMD_RXON, &sc->rxon, hal->rxonsz, 0);
4553 device_printf(sc->sc_dev,
4554 "%s: RXON command failed\n", __func__);
4558 error = iwn_add_broadcast_node(sc, 0);
4560 device_printf(sc->sc_dev,
4561 "%s: could not add broadcast node\n", __func__);
4565 /* Configuration has changed, set TX power accordingly. */
4566 error = hal->set_txpower(sc, ic->ic_curchan, 0);
4568 device_printf(sc->sc_dev,
4569 "%s: could not set TX power\n", __func__);
4573 error = iwn_set_critical_temp(sc);
4575 device_printf(sc->sc_dev,
4576 "%s: ccould not set critical temperature\n", __func__);
4580 /* Set power saving level to CAM during initialization. */
4581 error = iwn_set_pslevel(sc, 0, 0, 0);
4583 device_printf(sc->sc_dev,
4584 "%s: could not set power saving level\n", __func__);
4591 iwn_scan(struct iwn_softc *sc)
4593 struct ifnet *ifp = sc->sc_ifp;
4594 struct ieee80211com *ic = ifp->if_l2com;
4595 struct ieee80211_scan_state *ss = ic->ic_scan; /*XXX*/
4596 struct iwn_scan_hdr *hdr;
4597 struct iwn_cmd_data *tx;
4598 struct iwn_scan_essid *essid;
4599 struct iwn_scan_chan *chan;
4600 struct ieee80211_frame *wh;
4601 struct ieee80211_rateset *rs;
4602 struct ieee80211_channel *c;
4603 int buflen, error, nrates;
4605 uint8_t *buf, *frm, txant;
4607 buf = malloc(IWN_SCAN_MAXSZ, M_DEVBUF, M_NOWAIT | M_ZERO);
4609 device_printf(sc->sc_dev,
4610 "%s: could not allocate buffer for scan command\n",
4614 hdr = (struct iwn_scan_hdr *)buf;
4617 * Move to the next channel if no frames are received within 10ms
4618 * after sending the probe request.
4620 hdr->quiet_time = htole16(10); /* timeout in milliseconds */
4621 hdr->quiet_threshold = htole16(1); /* min # of packets */
4623 /* Select antennas for scanning. */
4625 IWN_RXCHAIN_VALID(sc->rxchainmask) |
4626 IWN_RXCHAIN_FORCE_MIMO_SEL(sc->rxchainmask) |
4627 IWN_RXCHAIN_DRIVER_FORCE;
4628 if (IEEE80211_IS_CHAN_A(ic->ic_curchan) &&
4629 sc->hw_type == IWN_HW_REV_TYPE_4965) {
4630 /* Ant A must be avoided in 5GHz because of an HW bug. */
4631 rxchain |= IWN_RXCHAIN_FORCE_SEL(IWN_ANT_BC);
4632 } else /* Use all available RX antennas. */
4633 rxchain |= IWN_RXCHAIN_FORCE_SEL(sc->rxchainmask);
4634 hdr->rxchain = htole16(rxchain);
4635 hdr->filter = htole32(IWN_FILTER_MULTICAST | IWN_FILTER_BEACON);
4637 tx = (struct iwn_cmd_data *)(hdr + 1);
4638 tx->flags = htole32(IWN_TX_AUTO_SEQ);
4639 tx->id = sc->sc_hal->broadcast_id;
4640 tx->lifetime = htole32(IWN_LIFETIME_INFINITE);
4642 if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) {
4643 /* Send probe requests at 6Mbps. */
4644 tx->plcp = iwn_rates[IWN_RIDX_OFDM6].plcp;
4645 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
4647 hdr->flags = htole32(IWN_RXON_24GHZ | IWN_RXON_AUTO);
4648 /* Send probe requests at 1Mbps. */
4649 tx->plcp = iwn_rates[IWN_RIDX_CCK1].plcp;
4650 tx->rflags = IWN_RFLAG_CCK;
4651 rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
4653 /* Use the first valid TX antenna. */
4654 txant = IWN_LSB(sc->txchainmask);
4655 tx->rflags |= IWN_RFLAG_ANT(txant);
4657 essid = (struct iwn_scan_essid *)(tx + 1);
4658 if (ss->ss_ssid[0].len != 0) {
4659 essid[0].id = IEEE80211_ELEMID_SSID;
4660 essid[0].len = ss->ss_ssid[0].len;
4661 memcpy(essid[0].data, ss->ss_ssid[0].ssid, ss->ss_ssid[0].len);
4665 * Build a probe request frame. Most of the following code is a
4666 * copy & paste of what is done in net80211.
4668 wh = (struct ieee80211_frame *)(essid + 20);
4669 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
4670 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
4671 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
4672 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr);
4673 IEEE80211_ADDR_COPY(wh->i_addr2, IF_LLADDR(ifp));
4674 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr);
4675 *(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */
4676 *(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */
4678 frm = (uint8_t *)(wh + 1);
4681 *frm++ = IEEE80211_ELEMID_SSID;
4682 *frm++ = ss->ss_ssid[0].len;
4683 memcpy(frm, ss->ss_ssid[0].ssid, ss->ss_ssid[0].len);
4684 frm += ss->ss_ssid[0].len;
4686 /* Add supported rates IE. */
4687 *frm++ = IEEE80211_ELEMID_RATES;
4688 nrates = rs->rs_nrates;
4689 if (nrates > IEEE80211_RATE_SIZE)
4690 nrates = IEEE80211_RATE_SIZE;
4692 memcpy(frm, rs->rs_rates, nrates);
4695 /* Add supported xrates IE. */
4696 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
4697 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
4698 *frm++ = IEEE80211_ELEMID_XRATES;
4699 *frm++ = (uint8_t)nrates;
4700 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
4704 /* Set length of probe request. */
4705 tx->len = htole16(frm - (uint8_t *)wh);
4708 chan = (struct iwn_scan_chan *)frm;
4709 chan->chan = htole16(ieee80211_chan2ieee(ic, c));
4711 if (ss->ss_nssid > 0)
4712 chan->flags |= htole32(IWN_CHAN_NPBREQS(1));
4713 chan->dsp_gain = 0x6e;
4714 if (IEEE80211_IS_CHAN_5GHZ(c) &&
4715 !(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
4716 chan->rf_gain = 0x3b;
4717 chan->active = htole16(24);
4718 chan->passive = htole16(110);
4719 chan->flags |= htole32(IWN_CHAN_ACTIVE);
4720 } else if (IEEE80211_IS_CHAN_5GHZ(c)) {
4721 chan->rf_gain = 0x3b;
4722 chan->active = htole16(24);
4723 if (sc->rxon.associd)
4724 chan->passive = htole16(78);
4726 chan->passive = htole16(110);
4727 hdr->crc_threshold = 0xffff;
4728 } else if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) {
4729 chan->rf_gain = 0x28;
4730 chan->active = htole16(36);
4731 chan->passive = htole16(120);
4732 chan->flags |= htole32(IWN_CHAN_ACTIVE);
4734 chan->rf_gain = 0x28;
4735 chan->active = htole16(36);
4736 if (sc->rxon.associd)
4737 chan->passive = htole16(88);
4739 chan->passive = htole16(120);
4740 hdr->crc_threshold = 0xffff;
4743 DPRINTF(sc, IWN_DEBUG_STATE,
4744 "%s: chan %u flags 0x%x rf_gain 0x%x "
4745 "dsp_gain 0x%x active 0x%x passive 0x%x\n", __func__,
4746 chan->chan, chan->flags, chan->rf_gain, chan->dsp_gain,
4747 chan->active, chan->passive);
4751 buflen = (uint8_t *)chan - buf;
4752 hdr->len = htole16(buflen);
4754 DPRINTF(sc, IWN_DEBUG_STATE, "sending scan command nchan=%d\n",
4756 error = iwn_cmd(sc, IWN_CMD_SCAN, buf, buflen, 1);
4757 free(buf, M_DEVBUF);
4762 iwn_auth(struct iwn_softc *sc, struct ieee80211vap *vap)
4764 const struct iwn_hal *hal = sc->sc_hal;
4765 struct ifnet *ifp = sc->sc_ifp;
4766 struct ieee80211com *ic = ifp->if_l2com;
4767 struct ieee80211_node *ni = vap->iv_bss;
4770 sc->calib.state = IWN_CALIB_STATE_INIT;
4772 /* Update adapter configuration. */
4773 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4774 sc->rxon.chan = htole16(ieee80211_chan2ieee(ic, ni->ni_chan));
4775 sc->rxon.flags = htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF);
4776 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
4777 sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ);
4778 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4779 sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
4780 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4781 sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
4782 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) {
4783 sc->rxon.cck_mask = 0;
4784 sc->rxon.ofdm_mask = 0x15;
4785 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) {
4786 sc->rxon.cck_mask = 0x03;
4787 sc->rxon.ofdm_mask = 0;
4789 /* XXX assume 802.11b/g */
4790 sc->rxon.cck_mask = 0x0f;
4791 sc->rxon.ofdm_mask = 0x15;
4793 DPRINTF(sc, IWN_DEBUG_STATE,
4794 "%s: config chan %d mode %d flags 0x%x cck 0x%x ofdm 0x%x "
4795 "ht_single 0x%x ht_dual 0x%x rxchain 0x%x "
4796 "myaddr %6D wlap %6D bssid %6D associd %d filter 0x%x\n",
4798 le16toh(sc->rxon.chan), sc->rxon.mode, le32toh(sc->rxon.flags),
4799 sc->rxon.cck_mask, sc->rxon.ofdm_mask,
4800 sc->rxon.ht_single_mask, sc->rxon.ht_dual_mask,
4801 le16toh(sc->rxon.rxchain),
4802 sc->rxon.myaddr, ":", sc->rxon.wlap, ":", sc->rxon.bssid, ":",
4803 le16toh(sc->rxon.associd), le32toh(sc->rxon.filter));
4804 error = iwn_cmd(sc, IWN_CMD_RXON, &sc->rxon, hal->rxonsz, 1);
4806 device_printf(sc->sc_dev,
4807 "%s: RXON command failed, error %d\n", __func__, error);
4811 /* Configuration has changed, set TX power accordingly. */
4812 error = hal->set_txpower(sc, ni->ni_chan, 1);
4814 device_printf(sc->sc_dev,
4815 "%s: could not set Tx power, error %d\n", __func__, error);
4819 * Reconfiguring RXON clears the firmware nodes table so we must
4820 * add the broadcast node again.
4822 error = iwn_add_broadcast_node(sc, 1);
4824 device_printf(sc->sc_dev,
4825 "%s: could not add broadcast node, error %d\n",
4833 * Configure the adapter for associated state.
4836 iwn_run(struct iwn_softc *sc, struct ieee80211vap *vap)
4838 #define MS(v,x) (((v) & x) >> x##_S)
4839 const struct iwn_hal *hal = sc->sc_hal;
4840 struct ifnet *ifp = sc->sc_ifp;
4841 struct ieee80211com *ic = ifp->if_l2com;
4842 struct ieee80211_node *ni = vap->iv_bss;
4843 struct iwn_node_info node;
4846 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
4847 /* Link LED blinks while monitoring. */
4848 iwn_set_led(sc, IWN_LED_LINK, 20, 20);
4851 error = iwn_set_timing(sc, ni);
4853 device_printf(sc->sc_dev,
4854 "%s: could not set timing, error %d\n", __func__, error);
4858 /* Update adapter configuration. */
4859 IEEE80211_ADDR_COPY(sc->rxon.bssid, ni->ni_bssid);
4860 sc->rxon.chan = htole16(ieee80211_chan2ieee(ic, ni->ni_chan));
4861 sc->rxon.associd = htole16(IEEE80211_AID(ni->ni_associd));
4862 /* Short preamble and slot time are negotiated when associating. */
4863 sc->rxon.flags &= ~htole32(IWN_RXON_SHPREAMBLE | IWN_RXON_SHSLOT);
4864 sc->rxon.flags |= htole32(IWN_RXON_TSF | IWN_RXON_CTS_TO_SELF);
4865 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
4866 sc->rxon.flags |= htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ);
4868 sc->rxon.flags &= ~htole32(IWN_RXON_AUTO | IWN_RXON_24GHZ);
4869 if (ic->ic_flags & IEEE80211_F_SHSLOT)
4870 sc->rxon.flags |= htole32(IWN_RXON_SHSLOT);
4871 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
4872 sc->rxon.flags |= htole32(IWN_RXON_SHPREAMBLE);
4873 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) {
4874 sc->rxon.cck_mask = 0;
4875 sc->rxon.ofdm_mask = 0x15;
4876 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) {
4877 sc->rxon.cck_mask = 0x03;
4878 sc->rxon.ofdm_mask = 0;
4880 /* XXX assume 802.11b/g */
4881 sc->rxon.cck_mask = 0x0f;
4882 sc->rxon.ofdm_mask = 0x15;
4885 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
4886 sc->rxon.flags &= ~htole32(IWN_RXON_HT);
4887 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
4888 sc->rxon.flags |= htole32(IWN_RXON_HT40U);
4889 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
4890 sc->rxon.flags |= htole32(IWN_RXON_HT40D);
4892 sc->rxon.flags |= htole32(IWN_RXON_HT20);
4893 sc->rxon.rxchain = htole16(
4894 IWN_RXCHAIN_VALID(3)
4895 | IWN_RXCHAIN_MIMO_COUNT(3)
4896 | IWN_RXCHAIN_IDLE_COUNT(1)
4897 | IWN_RXCHAIN_MIMO_FORCE);
4899 maxrxampdu = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
4900 ampdudensity = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
4902 maxrxampdu = ampdudensity = 0;
4904 sc->rxon.filter |= htole32(IWN_FILTER_BSS);
4906 DPRINTF(sc, IWN_DEBUG_STATE,
4907 "%s: config chan %d mode %d flags 0x%x cck 0x%x ofdm 0x%x "
4908 "ht_single 0x%x ht_dual 0x%x rxchain 0x%x "
4909 "myaddr %6D wlap %6D bssid %6D associd %d filter 0x%x\n",
4911 le16toh(sc->rxon.chan), sc->rxon.mode, le32toh(sc->rxon.flags),
4912 sc->rxon.cck_mask, sc->rxon.ofdm_mask,
4913 sc->rxon.ht_single_mask, sc->rxon.ht_dual_mask,
4914 le16toh(sc->rxon.rxchain),
4915 sc->rxon.myaddr, ":", sc->rxon.wlap, ":", sc->rxon.bssid, ":",
4916 le16toh(sc->rxon.associd), le32toh(sc->rxon.filter));
4917 error = iwn_cmd(sc, IWN_CMD_RXON, &sc->rxon, hal->rxonsz, 1);
4919 device_printf(sc->sc_dev,
4920 "%s: could not update configuration, error %d\n",
4925 /* Configuration has changed, set TX power accordingly. */
4926 error = hal->set_txpower(sc, ni->ni_chan, 1);
4928 device_printf(sc->sc_dev,
4929 "%s: could not set Tx power, error %d\n", __func__, error);
4934 memset(&node, 0, sizeof node);
4935 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr);
4936 node.id = IWN_ID_BSS;
4938 node.htflags = htole32(IWN_AMDPU_SIZE_FACTOR(3) |
4939 IWN_AMDPU_DENSITY(5)); /* 2us */
4941 DPRINTF(sc, IWN_DEBUG_STATE, "%s: add BSS node, id %d htflags 0x%x\n",
4942 __func__, node.id, le32toh(node.htflags));
4943 error = hal->add_node(sc, &node, 1);
4945 device_printf(sc->sc_dev, "could not add BSS node\n");
4948 DPRINTF(sc, IWN_DEBUG_STATE, "setting link quality for node %d\n",
4950 error = iwn_set_link_quality(sc, node.id, 1);
4952 device_printf(sc->sc_dev,
4953 "%s: could not setup MRR for node %d, error %d\n",
4954 __func__, node.id, error);
4958 error = iwn_init_sensitivity(sc);
4960 device_printf(sc->sc_dev,
4961 "%s: could not set sensitivity, error %d\n",
4966 /* Start periodic calibration timer. */
4967 sc->calib.state = IWN_CALIB_STATE_ASSOC;
4968 iwn_calib_reset(sc);
4970 /* Link LED always on while associated. */
4971 iwn_set_led(sc, IWN_LED_LINK, 0, 1);
4979 * This function is called by upper layer when an ADDBA request is received
4980 * from another STA and before the ADDBA response is sent.
4983 iwn_ampdu_rx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
4986 struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid];
4987 struct iwn_softc *sc = ic->ic_softc;
4988 struct iwn_node *wn = (void *)ni;
4989 struct iwn_node_info node;
4991 memset(&node, 0, sizeof node);
4993 node.control = IWN_NODE_UPDATE;
4994 node.flags = IWN_FLAG_SET_ADDBA;
4995 node.addba_tid = tid;
4996 node.addba_ssn = htole16(ba->ba_winstart);
4997 DPRINTF(sc, IWN_DEBUG_RECV, "ADDBA RA=%d TID=%d SSN=%d\n",
4998 wn->id, tid, ba->ba_winstart));
4999 return sc->sc_hal->add_node(sc, &node, 1);
5003 * This function is called by upper layer on teardown of an HT-immediate
5004 * Block Ack agreement (eg. uppon receipt of a DELBA frame.)
5007 iwn_ampdu_rx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
5010 struct iwn_softc *sc = ic->ic_softc;
5011 struct iwn_node *wn = (void *)ni;
5012 struct iwn_node_info node;
5014 memset(&node, 0, sizeof node);
5016 node.control = IWN_NODE_UPDATE;
5017 node.flags = IWN_FLAG_SET_DELBA;
5018 node.delba_tid = tid;
5019 DPRINTF(sc, IWN_DEBUG_RECV, "DELBA RA=%d TID=%d\n", wn->id, tid);
5020 (void)sc->sc_hal->add_node(sc, &node, 1);
5024 * This function is called by upper layer when an ADDBA response is received
5028 iwn_ampdu_tx_start(struct ieee80211com *ic, struct ieee80211_node *ni,
5031 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
5032 struct iwn_softc *sc = ic->ic_softc;
5033 const struct iwn_hal *hal = sc->sc_hal;
5034 struct iwn_node *wn = (void *)ni;
5035 struct iwn_node_info node;
5038 /* Enable TX for the specified RA/TID. */
5039 wn->disable_tid &= ~(1 << tid);
5040 memset(&node, 0, sizeof node);
5042 node.control = IWN_NODE_UPDATE;
5043 node.flags = IWN_FLAG_SET_DISABLE_TID;
5044 node.disable_tid = htole16(wn->disable_tid);
5045 error = hal->add_node(sc, &node, 1);
5049 if ((error = iwn_nic_lock(sc)) != 0)
5051 hal->ampdu_tx_start(sc, ni, tid, ba->ba_winstart);
5057 iwn_ampdu_tx_stop(struct ieee80211com *ic, struct ieee80211_node *ni,
5060 struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
5061 struct iwn_softc *sc = ic->ic_softc;
5064 error = iwn_nic_lock(sc);
5067 sc->sc_hal->ampdu_tx_stop(sc, tid, ba->ba_winstart);
5072 iwn4965_ampdu_tx_start(struct iwn_softc *sc, struct ieee80211_node *ni,
5073 uint8_t tid, uint16_t ssn)
5075 struct iwn_node *wn = (void *)ni;
5078 /* Stop TX scheduler while we're changing its configuration. */
5079 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
5080 IWN4965_TXQ_STATUS_CHGACT);
5082 /* Assign RA/TID translation to the queue. */
5083 iwn_mem_write_2(sc, sc->sched_base + IWN4965_SCHED_TRANS_TBL(qid),
5086 /* Enable chain-building mode for the queue. */
5087 iwn_prph_setbits(sc, IWN4965_SCHED_QCHAIN_SEL, 1 << qid);
5089 /* Set starting sequence number from the ADDBA request. */
5090 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | (ssn & 0xff));
5091 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), ssn);
5093 /* Set scheduler window size. */
5094 iwn_mem_write(sc, sc->sched_base + IWN4965_SCHED_QUEUE_OFFSET(qid),
5096 /* Set scheduler frame limit. */
5097 iwn_mem_write(sc, sc->sched_base + IWN4965_SCHED_QUEUE_OFFSET(qid) + 4,
5098 IWN_SCHED_LIMIT << 16);
5100 /* Enable interrupts for the queue. */
5101 iwn_prph_setbits(sc, IWN4965_SCHED_INTR_MASK, 1 << qid);
5103 /* Mark the queue as active. */
5104 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
5105 IWN4965_TXQ_STATUS_ACTIVE | IWN4965_TXQ_STATUS_AGGR_ENA |
5106 iwn_tid2fifo[tid] << 1);
5110 iwn4965_ampdu_tx_stop(struct iwn_softc *sc, uint8_t tid, uint16_t ssn)
5114 /* Stop TX scheduler while we're changing its configuration. */
5115 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
5116 IWN4965_TXQ_STATUS_CHGACT);
5118 /* Set starting sequence number from the ADDBA request. */
5119 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | (ssn & 0xff));
5120 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), ssn);
5122 /* Disable interrupts for the queue. */
5123 iwn_prph_clrbits(sc, IWN4965_SCHED_INTR_MASK, 1 << qid);
5125 /* Mark the queue as inactive. */
5126 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
5127 IWN4965_TXQ_STATUS_INACTIVE | iwn_tid2fifo[tid] << 1);
5131 iwn5000_ampdu_tx_start(struct iwn_softc *sc, struct ieee80211_node *ni,
5132 uint8_t tid, uint16_t ssn)
5134 struct iwn_node *wn = (void *)ni;
5137 /* Stop TX scheduler while we're changing its configuration. */
5138 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
5139 IWN5000_TXQ_STATUS_CHGACT);
5141 /* Assign RA/TID translation to the queue. */
5142 iwn_mem_write_2(sc, sc->sched_base + IWN5000_SCHED_TRANS_TBL(qid),
5145 /* Enable chain-building mode for the queue. */
5146 iwn_prph_setbits(sc, IWN5000_SCHED_QCHAIN_SEL, 1 << qid);
5148 /* Enable aggregation for the queue. */
5149 iwn_prph_setbits(sc, IWN5000_SCHED_AGGR_SEL, 1 << qid);
5151 /* Set starting sequence number from the ADDBA request. */
5152 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | (ssn & 0xff));
5153 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), ssn);
5155 /* Set scheduler window size and frame limit. */
5156 iwn_mem_write(sc, sc->sched_base + IWN5000_SCHED_QUEUE_OFFSET(qid) + 4,
5157 IWN_SCHED_LIMIT << 16 | IWN_SCHED_WINSZ);
5159 /* Enable interrupts for the queue. */
5160 iwn_prph_setbits(sc, IWN5000_SCHED_INTR_MASK, 1 << qid);
5162 /* Mark the queue as active. */
5163 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
5164 IWN5000_TXQ_STATUS_ACTIVE | iwn_tid2fifo[tid]);
5168 iwn5000_ampdu_tx_stop(struct iwn_softc *sc, uint8_t tid, uint16_t ssn)
5172 /* Stop TX scheduler while we're changing its configuration. */
5173 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
5174 IWN5000_TXQ_STATUS_CHGACT);
5176 /* Disable aggregation for the queue. */
5177 iwn_prph_clrbits(sc, IWN5000_SCHED_AGGR_SEL, 1 << qid);
5179 /* Set starting sequence number from the ADDBA request. */
5180 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | (ssn & 0xff));
5181 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), ssn);
5183 /* Disable interrupts for the queue. */
5184 iwn_prph_clrbits(sc, IWN5000_SCHED_INTR_MASK, 1 << qid);
5186 /* Mark the queue as inactive. */
5187 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
5188 IWN5000_TXQ_STATUS_INACTIVE | iwn_tid2fifo[tid]);
5193 * Send calibration results to the runtime firmware. These results were
5194 * obtained on first boot from the initialization firmware, or by reading
5195 * the EEPROM for crystal calibration.
5198 iwn5000_send_calib_results(struct iwn_softc *sc)
5200 struct iwn_calib_info *calib_result;
5203 for (idx = 0; idx < IWN_CALIB_NUM; idx++) {
5204 calib_result = &sc->calib_results[idx];
5206 /* No support for this type of calibration. */
5207 if ((sc->calib_init & (1 << idx)) == 0)
5210 /* No calibration result available. */
5211 if (calib_result->buf == NULL)
5214 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
5215 "%s: send calibration result idx=%d, len=%d\n",
5216 __func__, idx, calib_result->len);
5218 error = iwn_cmd(sc, IWN_CMD_PHY_CALIB, calib_result->buf,
5219 calib_result->len, 0);
5221 device_printf(sc->sc_dev,
5222 "%s: could not send calibration result "
5223 "idx=%d, error=%d\n",
5224 __func__, idx, error);
5232 * Save calibration result at the given index. The index determines
5233 * in which order the results are sent to the runtime firmware.
5236 iwn5000_save_calib_result(struct iwn_softc *sc, struct iwn_phy_calib *calib,
5239 struct iwn_calib_info *calib_result = &sc->calib_results[idx];
5241 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
5242 "%s: saving calibration result code=%d, idx=%d, len=%d\n",
5243 __func__, calib->code, idx, len);
5245 if (calib_result->buf != NULL)
5246 free(calib_result->buf, M_DEVBUF);
5248 calib_result->buf = malloc(len, M_DEVBUF, M_NOWAIT);
5249 if (calib_result->buf == NULL) {
5250 device_printf(sc->sc_dev,
5251 "%s: not enough memory for calibration result "
5252 "code=%d, len=%d\n", __func__, calib->code, len);
5256 calib_result->len = len;
5257 memcpy(calib_result->buf, calib, len);
5262 iwn5000_free_calib_results(struct iwn_softc *sc)
5264 struct iwn_calib_info *calib_result;
5267 for (idx = 0; idx < IWN_CALIB_NUM; idx++) {
5268 calib_result = &sc->calib_results[idx];
5270 if (calib_result->buf != NULL)
5271 free(calib_result->buf, M_DEVBUF);
5273 calib_result->buf = NULL;
5274 calib_result->len = 0;
5279 * Obtain the crystal calibration result from the EEPROM.
5282 iwn5000_chrystal_calib(struct iwn_softc *sc)
5284 struct iwn5000_phy_calib_crystal cmd;
5285 uint32_t base, crystal;
5288 /* Read crystal calibration. */
5289 iwn_read_prom_data(sc, IWN5000_EEPROM_CAL, &val, 2);
5290 base = le16toh(val);
5291 iwn_read_prom_data(sc, base + IWN5000_EEPROM_CRYSTAL, &crystal,
5293 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: crystal calibration=0x%08x\n",
5294 __func__, le32toh(crystal));
5296 memset(&cmd, 0, sizeof cmd);
5297 cmd.code = IWN5000_PHY_CALIB_CRYSTAL;
5300 cmd.cap_pin[0] = le32toh(crystal) & 0xff;
5301 cmd.cap_pin[1] = (le32toh(crystal) >> 16) & 0xff;
5303 return iwn5000_save_calib_result(sc, (struct iwn_phy_calib *)&cmd,
5304 sizeof cmd, IWN_CALIB_IDX_XTAL);
5308 * Query calibration results from the initialization firmware. We do this
5309 * only once at first boot.
5312 iwn5000_send_calib_query(struct iwn_softc *sc, uint32_t cfg)
5314 #define CALIB_INIT_CFG 0xffffffff;
5315 struct iwn5000_calib_config cmd;
5318 memset(&cmd, 0, sizeof cmd);
5319 cmd.ucode.once.enable = CALIB_INIT_CFG;
5321 cmd.ucode.once.start = CALIB_INIT_CFG;
5322 cmd.ucode.once.send = CALIB_INIT_CFG;
5323 cmd.ucode.flags = CALIB_INIT_CFG;
5325 cmd.ucode.once.start = cfg;
5327 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
5328 "%s: query calibration results, cfg %x\n", __func__, cfg);
5330 error = iwn_cmd(sc, IWN5000_CMD_CALIB_CONFIG, &cmd, sizeof cmd, 0);
5334 /* Wait at most two seconds for calibration to complete. */
5335 if (!(sc->sc_flags & IWN_FLAG_CALIB_DONE))
5336 error = msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", 2 * hz);
5339 #undef CALIB_INIT_CFG
5343 * Process a CALIBRATION_RESULT notification sent by the initialization
5344 * firmware on response to a CMD_CALIB_CONFIG command.
5347 iwn5000_rx_calib_result(struct iwn_softc *sc, struct iwn_rx_desc *desc,
5348 struct iwn_rx_data *data)
5350 #define FRAME_SIZE_MASK 0x3fff
5351 struct iwn_phy_calib *calib = (struct iwn_phy_calib *)(desc + 1);
5354 bus_dmamap_sync(sc->rxq.data_dmat, data->map, BUS_DMASYNC_POSTREAD);
5355 len = (le32toh(desc->len) & FRAME_SIZE_MASK);
5357 /* Remove length field itself. */
5361 * Determine the order in which the results will be send to the
5364 switch (calib->code) {
5365 case IWN5000_PHY_CALIB_DC:
5366 idx = IWN_CALIB_IDX_DC;
5368 case IWN5000_PHY_CALIB_LO:
5369 idx = IWN_CALIB_IDX_LO;
5371 case IWN5000_PHY_CALIB_TX_IQ:
5372 idx = IWN_CALIB_IDX_TX_IQ;
5374 case IWN5000_PHY_CALIB_TX_IQ_PERIODIC:
5375 idx = IWN_CALIB_IDX_TX_IQ_PERIODIC;
5377 case IWN5000_PHY_CALIB_BASE_BAND:
5378 idx = IWN_CALIB_IDX_BASE_BAND;
5381 DPRINTF(sc, IWN_DEBUG_CALIBRATE,
5382 "%s: unknown calibration code=%d\n", __func__, calib->code);
5385 return iwn5000_save_calib_result(sc, calib, len, idx);
5386 #undef FRAME_SIZE_MASK
5390 iwn5000_send_wimax_coex(struct iwn_softc *sc)
5392 struct iwn5000_wimax_coex wimax;
5395 if (sc->hw_type == IWN_HW_REV_TYPE_6050) {
5396 /* Enable WiMAX coexistence for combo adapters. */
5398 IWN_WIMAX_COEX_ASSOC_WA_UNMASK |
5399 IWN_WIMAX_COEX_UNASSOC_WA_UNMASK |
5400 IWN_WIMAX_COEX_STA_TABLE_VALID |
5401 IWN_WIMAX_COEX_ENABLE;
5402 memcpy(wimax.events, iwn6050_wimax_events,
5403 sizeof iwn6050_wimax_events);
5407 /* Disable WiMAX coexistence. */
5409 memset(wimax.events, 0, sizeof wimax.events);
5411 DPRINTF(sc, IWN_DEBUG_RESET, "%s: Configuring WiMAX coexistence\n",
5413 return iwn_cmd(sc, IWN5000_CMD_WIMAX_COEX, &wimax, sizeof wimax, 0);
5417 * This function is called after the runtime firmware notifies us of its
5418 * readiness (called in a process context.)
5421 iwn4965_post_alive(struct iwn_softc *sc)
5425 if ((error = iwn_nic_lock(sc)) != 0)
5428 /* Clear TX scheduler state in SRAM. */
5429 sc->sched_base = iwn_prph_read(sc, IWN_SCHED_SRAM_ADDR);
5430 iwn_mem_set_region_4(sc, sc->sched_base + IWN4965_SCHED_CTX_OFF, 0,
5431 IWN4965_SCHED_CTX_LEN / sizeof (uint32_t));
5433 /* Set physical address of TX scheduler rings (1KB aligned.) */
5434 iwn_prph_write(sc, IWN4965_SCHED_DRAM_ADDR, sc->sched_dma.paddr >> 10);
5436 IWN_SETBITS(sc, IWN_FH_TX_CHICKEN, IWN_FH_TX_CHICKEN_SCHED_RETRY);
5438 /* Disable chain mode for all our 16 queues. */
5439 iwn_prph_write(sc, IWN4965_SCHED_QCHAIN_SEL, 0);
5441 for (qid = 0; qid < IWN4965_NTXQUEUES; qid++) {
5442 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_RDPTR(qid), 0);
5443 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | 0);
5445 /* Set scheduler window size. */
5446 iwn_mem_write(sc, sc->sched_base +
5447 IWN4965_SCHED_QUEUE_OFFSET(qid), IWN_SCHED_WINSZ);
5448 /* Set scheduler frame limit. */
5449 iwn_mem_write(sc, sc->sched_base +
5450 IWN4965_SCHED_QUEUE_OFFSET(qid) + 4,
5451 IWN_SCHED_LIMIT << 16);
5454 /* Enable interrupts for all our 16 queues. */
5455 iwn_prph_write(sc, IWN4965_SCHED_INTR_MASK, 0xffff);
5456 /* Identify TX FIFO rings (0-7). */
5457 iwn_prph_write(sc, IWN4965_SCHED_TXFACT, 0xff);
5459 /* Mark TX rings (4 EDCA + cmd + 2 HCCA) as active. */
5460 for (qid = 0; qid < 7; qid++) {
5461 static uint8_t qid2fifo[] = { 3, 2, 1, 0, 4, 5, 6 };
5462 iwn_prph_write(sc, IWN4965_SCHED_QUEUE_STATUS(qid),
5463 IWN4965_TXQ_STATUS_ACTIVE | qid2fifo[qid] << 1);
5470 * This function is called after the initialization or runtime firmware
5471 * notifies us of its readiness (called in a process context.)
5474 iwn5000_post_alive(struct iwn_softc *sc)
5478 /* Switch to using ICT interrupt mode. */
5479 iwn5000_ict_reset(sc);
5481 error = iwn_nic_lock(sc);
5485 /* Clear TX scheduler state in SRAM. */
5486 sc->sched_base = iwn_prph_read(sc, IWN_SCHED_SRAM_ADDR);
5487 iwn_mem_set_region_4(sc, sc->sched_base + IWN5000_SCHED_CTX_OFF, 0,
5488 IWN5000_SCHED_CTX_LEN / sizeof (uint32_t));
5490 /* Set physical address of TX scheduler rings (1KB aligned.) */
5491 iwn_prph_write(sc, IWN5000_SCHED_DRAM_ADDR, sc->sched_dma.paddr >> 10);
5493 IWN_SETBITS(sc, IWN_FH_TX_CHICKEN, IWN_FH_TX_CHICKEN_SCHED_RETRY);
5495 /* Enable chain mode for all queues, except command queue. */
5496 iwn_prph_write(sc, IWN5000_SCHED_QCHAIN_SEL, 0xfffef);
5497 iwn_prph_write(sc, IWN5000_SCHED_AGGR_SEL, 0);
5499 for (qid = 0; qid < IWN5000_NTXQUEUES; qid++) {
5500 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_RDPTR(qid), 0);
5501 IWN_WRITE(sc, IWN_HBUS_TARG_WRPTR, qid << 8 | 0);
5503 iwn_mem_write(sc, sc->sched_base +
5504 IWN5000_SCHED_QUEUE_OFFSET(qid), 0);
5505 /* Set scheduler window size and frame limit. */
5506 iwn_mem_write(sc, sc->sched_base +
5507 IWN5000_SCHED_QUEUE_OFFSET(qid) + 4,
5508 IWN_SCHED_LIMIT << 16 | IWN_SCHED_WINSZ);
5511 /* Enable interrupts for all our 20 queues. */
5512 iwn_prph_write(sc, IWN5000_SCHED_INTR_MASK, 0xfffff);
5513 /* Identify TX FIFO rings (0-7). */
5514 iwn_prph_write(sc, IWN5000_SCHED_TXFACT, 0xff);
5516 /* Mark TX rings (4 EDCA + cmd + 2 HCCA) as active. */
5517 for (qid = 0; qid < 7; qid++) {
5518 static uint8_t qid2fifo[] = { 3, 2, 1, 0, 7, 5, 6 };
5519 iwn_prph_write(sc, IWN5000_SCHED_QUEUE_STATUS(qid),
5520 IWN5000_TXQ_STATUS_ACTIVE | qid2fifo[qid]);
5524 /* Configure WiMAX coexistence for combo adapters. */
5525 error = iwn5000_send_wimax_coex(sc);
5527 device_printf(sc->sc_dev,
5528 "%s: could not configure WiMAX coexistence, error %d\n",
5533 if (!(sc->sc_flags & IWN_FLAG_CALIB_DONE)) {
5535 * Start calibration by setting and sending the chrystal
5536 * calibration first, this must be done before we are able
5537 * to query the other calibration results.
5539 error = iwn5000_chrystal_calib(sc);
5541 device_printf(sc->sc_dev,
5542 "%s: could not set chrystal calibration, "
5543 "error=%d\n", __func__, error);
5546 error = iwn5000_send_calib_results(sc);
5548 device_printf(sc->sc_dev,
5549 "%s: could not send chrystal calibration, "
5550 "error=%d\n", __func__, error);
5555 * Query other calibration results from the initialization
5558 error = iwn5000_send_calib_query(sc, 0);
5560 device_printf(sc->sc_dev,
5561 "%s: could not query calibration, error=%d\n",
5567 * We have the calibration results now, reboot with the
5568 * runtime firmware (call ourselves recursively!)
5571 error = iwn_hw_init(sc);
5574 * Send calibration results obtained from the initialization
5575 * firmware to the runtime firmware.
5577 error = iwn5000_send_calib_results(sc);
5580 * Tell the runtime firmware to do certain calibration types.
5582 if (sc->calib_runtime != 0) {
5583 error = iwn5000_send_calib_query(sc, sc->calib_runtime);
5585 device_printf(sc->sc_dev,
5586 "%s: could not send query calibration, "
5587 "error=%d, cfg=%x\n", __func__, error,
5596 * The firmware boot code is small and is intended to be copied directly into
5597 * the NIC internal memory (no DMA transfer.)
5600 iwn4965_load_bootcode(struct iwn_softc *sc, const uint8_t *ucode, int size)
5604 size /= sizeof (uint32_t);
5606 error = iwn_nic_lock(sc);
5610 /* Copy microcode image into NIC memory. */
5611 iwn_prph_write_region_4(sc, IWN_BSM_SRAM_BASE,
5612 (const uint32_t *)ucode, size);
5614 iwn_prph_write(sc, IWN_BSM_WR_MEM_SRC, 0);
5615 iwn_prph_write(sc, IWN_BSM_WR_MEM_DST, IWN_FW_TEXT_BASE);
5616 iwn_prph_write(sc, IWN_BSM_WR_DWCOUNT, size);
5618 /* Start boot load now. */
5619 iwn_prph_write(sc, IWN_BSM_WR_CTRL, IWN_BSM_WR_CTRL_START);
5621 /* Wait for transfer to complete. */
5622 for (ntries = 0; ntries < 1000; ntries++) {
5623 if (!(iwn_prph_read(sc, IWN_BSM_WR_CTRL) &
5624 IWN_BSM_WR_CTRL_START))
5628 if (ntries == 1000) {
5629 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
5635 /* Enable boot after power up. */
5636 iwn_prph_write(sc, IWN_BSM_WR_CTRL, IWN_BSM_WR_CTRL_START_EN);
5643 iwn4965_load_firmware(struct iwn_softc *sc)
5645 struct iwn_fw_info *fw = &sc->fw;
5646 struct iwn_dma_info *dma = &sc->fw_dma;
5649 /* Copy initialization sections into pre-allocated DMA-safe memory. */
5650 memcpy(dma->vaddr, fw->init.data, fw->init.datasz);
5651 bus_dmamap_sync(sc->fw_dma.tag, dma->map, BUS_DMASYNC_PREWRITE);
5652 memcpy(dma->vaddr + IWN4965_FW_DATA_MAXSZ,
5653 fw->init.text, fw->init.textsz);
5654 bus_dmamap_sync(sc->fw_dma.tag, dma->map, BUS_DMASYNC_PREWRITE);
5656 /* Tell adapter where to find initialization sections. */
5657 error = iwn_nic_lock(sc);
5660 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_ADDR, dma->paddr >> 4);
5661 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_SIZE, fw->init.datasz);
5662 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_ADDR,
5663 (dma->paddr + IWN4965_FW_DATA_MAXSZ) >> 4);
5664 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_SIZE, fw->init.textsz);
5667 /* Load firmware boot code. */
5668 error = iwn4965_load_bootcode(sc, fw->boot.text, fw->boot.textsz);
5670 device_printf(sc->sc_dev, "%s: could not load boot firmware\n",
5674 /* Now press "execute". */
5675 IWN_WRITE(sc, IWN_RESET, 0);
5677 /* Wait at most one second for first alive notification. */
5678 error = msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", hz);
5680 device_printf(sc->sc_dev,
5681 "%s: timeout waiting for adapter to initialize, error %d\n",
5686 /* Retrieve current temperature for initial TX power calibration. */
5687 sc->rawtemp = sc->ucode_info.temp[3].chan20MHz;
5688 sc->temp = iwn4965_get_temperature(sc);
5690 /* Copy runtime sections into pre-allocated DMA-safe memory. */
5691 memcpy(dma->vaddr, fw->main.data, fw->main.datasz);
5692 bus_dmamap_sync(sc->fw_dma.tag, dma->map, BUS_DMASYNC_PREWRITE);
5693 memcpy(dma->vaddr + IWN4965_FW_DATA_MAXSZ,
5694 fw->main.text, fw->main.textsz);
5695 bus_dmamap_sync(sc->fw_dma.tag, dma->map, BUS_DMASYNC_PREWRITE);
5697 /* Tell adapter where to find runtime sections. */
5698 error = iwn_nic_lock(sc);
5702 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_ADDR, dma->paddr >> 4);
5703 iwn_prph_write(sc, IWN_BSM_DRAM_DATA_SIZE, fw->main.datasz);
5704 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_ADDR,
5705 (dma->paddr + IWN4965_FW_DATA_MAXSZ) >> 4);
5706 iwn_prph_write(sc, IWN_BSM_DRAM_TEXT_SIZE,
5707 IWN_FW_UPDATED | fw->main.textsz);
5714 iwn5000_load_firmware_section(struct iwn_softc *sc, uint32_t dst,
5715 const uint8_t *section, int size)
5717 struct iwn_dma_info *dma = &sc->fw_dma;
5720 /* Copy firmware section into pre-allocated DMA-safe memory. */
5721 memcpy(dma->vaddr, section, size);
5722 bus_dmamap_sync(sc->fw_dma.tag, dma->map, BUS_DMASYNC_PREWRITE);
5724 error = iwn_nic_lock(sc);
5728 IWN_WRITE(sc, IWN_FH_TX_CONFIG(IWN_SRVC_DMACHNL),
5729 IWN_FH_TX_CONFIG_DMA_PAUSE);
5731 IWN_WRITE(sc, IWN_FH_SRAM_ADDR(IWN_SRVC_DMACHNL), dst);
5732 IWN_WRITE(sc, IWN_FH_TFBD_CTRL0(IWN_SRVC_DMACHNL),
5733 IWN_LOADDR(dma->paddr));
5734 IWN_WRITE(sc, IWN_FH_TFBD_CTRL1(IWN_SRVC_DMACHNL),
5735 IWN_HIADDR(dma->paddr) << 28 | size);
5736 IWN_WRITE(sc, IWN_FH_TXBUF_STATUS(IWN_SRVC_DMACHNL),
5737 IWN_FH_TXBUF_STATUS_TBNUM(1) |
5738 IWN_FH_TXBUF_STATUS_TBIDX(1) |
5739 IWN_FH_TXBUF_STATUS_TFBD_VALID);
5741 /* Kick Flow Handler to start DMA transfer. */
5742 IWN_WRITE(sc, IWN_FH_TX_CONFIG(IWN_SRVC_DMACHNL),
5743 IWN_FH_TX_CONFIG_DMA_ENA | IWN_FH_TX_CONFIG_CIRQ_HOST_ENDTFD);
5747 /* Wait at most five seconds for FH DMA transfer to complete. */
5748 return msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", hz);
5752 iwn5000_load_firmware(struct iwn_softc *sc)
5754 struct iwn_fw_part *fw;
5757 /* Load the initialization firmware on first boot only. */
5758 fw = (sc->sc_flags & IWN_FLAG_CALIB_DONE) ?
5759 &sc->fw.main : &sc->fw.init;
5761 error = iwn5000_load_firmware_section(sc, IWN_FW_TEXT_BASE,
5762 fw->text, fw->textsz);
5764 device_printf(sc->sc_dev,
5765 "%s: could not load firmware %s section, error %d\n",
5766 __func__, ".text", error);
5769 error = iwn5000_load_firmware_section(sc, IWN_FW_DATA_BASE,
5770 fw->data, fw->datasz);
5772 device_printf(sc->sc_dev,
5773 "%s: could not load firmware %s section, error %d\n",
5774 __func__, ".data", error);
5778 /* Now press "execute". */
5779 IWN_WRITE(sc, IWN_RESET, 0);
5784 * Extract text and data sections from a legacy firmware image.
5787 iwn_read_firmware_leg(struct iwn_softc *sc, struct iwn_fw_info *fw)
5789 const uint32_t *ptr;
5793 ptr = (const uint32_t *)sc->fw_fp->data;
5794 rev = le32toh(*ptr++);
5796 /* Check firmware API version. */
5797 if (IWN_FW_API(rev) <= 1) {
5798 device_printf(sc->sc_dev,
5799 "%s: bad firmware, need API version >=2\n", __func__);
5802 if (IWN_FW_API(rev) >= 3) {
5803 /* Skip build number (version 2 header). */
5807 if (fw->size < hdrlen) {
5808 device_printf(sc->sc_dev,
5809 "%s: firmware file too short: %zu bytes\n",
5810 __func__, fw->size);
5813 fw->main.textsz = le32toh(*ptr++);
5814 fw->main.datasz = le32toh(*ptr++);
5815 fw->init.textsz = le32toh(*ptr++);
5816 fw->init.datasz = le32toh(*ptr++);
5817 fw->boot.textsz = le32toh(*ptr++);
5819 /* Check that all firmware sections fit. */
5820 if (fw->size < hdrlen + fw->main.textsz + fw->main.datasz +
5821 fw->init.textsz + fw->init.datasz + fw->boot.textsz) {
5822 device_printf(sc->sc_dev,
5823 "%s: firmware file too short: %zu bytes\n",
5824 __func__, fw->size);
5828 /* Get pointers to firmware sections. */
5829 fw->main.text = (const uint8_t *)ptr;
5830 fw->main.data = fw->main.text + fw->main.textsz;
5831 fw->init.text = fw->main.data + fw->main.datasz;
5832 fw->init.data = fw->init.text + fw->init.textsz;
5833 fw->boot.text = fw->init.data + fw->init.datasz;
5839 * Extract text and data sections from a TLV firmware image.
5842 iwn_read_firmware_tlv(struct iwn_softc *sc, struct iwn_fw_info *fw,
5845 const struct iwn_fw_tlv_hdr *hdr;
5846 const struct iwn_fw_tlv *tlv;
5847 const uint8_t *ptr, *end;
5851 if (fw->size < sizeof (*hdr)) {
5852 device_printf(sc->sc_dev,
5853 "%s: firmware file too short: %zu bytes\n",
5854 __func__, fw->size);
5857 hdr = (const struct iwn_fw_tlv_hdr *)fw->data;
5858 if (hdr->signature != htole32(IWN_FW_SIGNATURE)) {
5859 device_printf(sc->sc_dev,
5860 "%s: bad firmware file signature 0x%08x\n",
5861 __func__, le32toh(hdr->signature));
5866 * Select the closest supported alternative that is less than
5867 * or equal to the specified one.
5869 altmask = le64toh(hdr->altmask);
5870 while (alt > 0 && !(altmask & (1ULL << alt)))
5871 alt--; /* Downgrade. */
5873 ptr = (const uint8_t *)(hdr + 1);
5874 end = (const uint8_t *)(fw->data + fw->size);
5876 /* Parse type-length-value fields. */
5877 while (ptr + sizeof (*tlv) <= end) {
5878 tlv = (const struct iwn_fw_tlv *)ptr;
5879 len = le32toh(tlv->len);
5881 ptr += sizeof (*tlv);
5882 if (ptr + len > end) {
5883 device_printf(sc->sc_dev,
5884 "%s: firmware file too short: %zu bytes\n",
5885 __func__, fw->size);
5888 /* Skip other alternatives. */
5889 if (tlv->alt != 0 && tlv->alt != htole16(alt))
5892 switch (le16toh(tlv->type)) {
5893 case IWN_FW_TLV_MAIN_TEXT:
5894 fw->main.text = ptr;
5895 fw->main.textsz = len;
5897 case IWN_FW_TLV_MAIN_DATA:
5898 fw->main.data = ptr;
5899 fw->main.datasz = len;
5901 case IWN_FW_TLV_INIT_TEXT:
5902 fw->init.text = ptr;
5903 fw->init.textsz = len;
5905 case IWN_FW_TLV_INIT_DATA:
5906 fw->init.data = ptr;
5907 fw->init.datasz = len;
5909 case IWN_FW_TLV_BOOT_TEXT:
5910 fw->boot.text = ptr;
5911 fw->boot.textsz = len;
5914 DPRINTF(sc, IWN_DEBUG_RESET,
5915 "%s: TLV type %d not handled\n",
5916 __func__, le16toh(tlv->type));
5919 next: /* TLV fields are 32-bit aligned. */
5920 ptr += (len + 3) & ~3;
5926 iwn_read_firmware(struct iwn_softc *sc)
5928 const struct iwn_hal *hal = sc->sc_hal;
5929 struct iwn_fw_info *fw = &sc->fw;
5934 memset(fw, 0, sizeof (*fw));
5936 /* Read firmware image from filesystem. */
5937 sc->fw_fp = firmware_get(sc->fwname);
5938 if (sc->fw_fp == NULL) {
5939 device_printf(sc->sc_dev,
5940 "%s: could not load firmare image \"%s\"\n", __func__,
5947 fw->size = sc->fw_fp->datasize;
5948 fw->data = (const uint8_t *)sc->fw_fp->data;
5949 if (fw->size < sizeof (uint32_t)) {
5950 device_printf(sc->sc_dev,
5951 "%s: firmware file too short: %zu bytes\n",
5952 __func__, fw->size);
5956 /* Retrieve text and data sections. */
5957 if (*(const uint32_t *)fw->data != 0) /* Legacy image. */
5958 error = iwn_read_firmware_leg(sc, fw);
5960 error = iwn_read_firmware_tlv(sc, fw, 1);
5962 device_printf(sc->sc_dev,
5963 "%s: could not read firmware sections\n", __func__);
5967 /* Make sure text and data sections fit in hardware memory. */
5968 if (fw->main.textsz > hal->fw_text_maxsz ||
5969 fw->main.datasz > hal->fw_data_maxsz ||
5970 fw->init.textsz > hal->fw_text_maxsz ||
5971 fw->init.datasz > hal->fw_data_maxsz ||
5972 fw->boot.textsz > IWN_FW_BOOT_TEXT_MAXSZ ||
5973 (fw->boot.textsz & 3) != 0) {
5974 device_printf(sc->sc_dev,
5975 "%s: firmware sections too large\n", __func__);
5979 /* We can proceed with loading the firmware. */
5984 iwn_clock_wait(struct iwn_softc *sc)
5988 /* Set "initialization complete" bit. */
5989 IWN_SETBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_INIT_DONE);
5991 /* Wait for clock stabilization. */
5992 for (ntries = 0; ntries < 2500; ntries++) {
5993 if (IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_MAC_CLOCK_READY)
5997 device_printf(sc->sc_dev,
5998 "%s: timeout waiting for clock stabilization\n", __func__);
6003 iwn_apm_init(struct iwn_softc *sc)
6008 /* Disable L0s exit timer (NMI bug workaround.) */
6009 IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_DIS_L0S_TIMER);
6010 /* Don't wait for ICH L0s (ICH bug workaround.) */
6011 IWN_SETBITS(sc, IWN_GIO_CHICKEN, IWN_GIO_CHICKEN_L1A_NO_L0S_RX);
6013 /* Set FH wait threshold to max (HW bug under stress workaround.) */
6014 IWN_SETBITS(sc, IWN_DBG_HPET_MEM, 0xffff0000);
6016 /* Enable HAP INTA to move adapter from L1a to L0s. */
6017 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_HAP_WAKE_L1A);
6019 /* Retrieve PCIe Active State Power Management (ASPM). */
6020 tmp = pci_read_config(sc->sc_dev, sc->sc_cap_off + 0x10, 1);
6021 /* Workaround for HW instability in PCIe L0->L0s->L1 transition. */
6022 if (tmp & 0x02) /* L1 Entry enabled. */
6023 IWN_SETBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
6025 IWN_CLRBITS(sc, IWN_GIO, IWN_GIO_L0S_ENA);
6027 if (sc->hw_type != IWN_HW_REV_TYPE_4965 &&
6028 sc->hw_type <= IWN_HW_REV_TYPE_1000)
6029 IWN_SETBITS(sc, IWN_ANA_PLL, IWN_ANA_PLL_INIT);
6031 /* Wait for clock stabilization before accessing prph. */
6032 error = iwn_clock_wait(sc);
6036 error = iwn_nic_lock(sc);
6040 if (sc->hw_type == IWN_HW_REV_TYPE_4965) {
6041 /* Enable DMA and BSM (Bootstrap State Machine.) */
6042 iwn_prph_write(sc, IWN_APMG_CLK_EN,
6043 IWN_APMG_CLK_CTRL_DMA_CLK_RQT |
6044 IWN_APMG_CLK_CTRL_BSM_CLK_RQT);
6047 iwn_prph_write(sc, IWN_APMG_CLK_EN,
6048 IWN_APMG_CLK_CTRL_DMA_CLK_RQT);
6052 /* Disable L1-Active. */
6053 iwn_prph_setbits(sc, IWN_APMG_PCI_STT, IWN_APMG_PCI_STT_L1A_DIS);
6060 iwn_apm_stop_master(struct iwn_softc *sc)
6064 /* Stop busmaster DMA activity. */
6065 IWN_SETBITS(sc, IWN_RESET, IWN_RESET_STOP_MASTER);
6066 for (ntries = 0; ntries < 100; ntries++) {
6067 if (IWN_READ(sc, IWN_RESET) & IWN_RESET_MASTER_DISABLED)
6071 device_printf(sc->sc_dev, "%s: timeout waiting for master\n",
6076 iwn_apm_stop(struct iwn_softc *sc)
6078 iwn_apm_stop_master(sc);
6080 /* Reset the entire device. */
6081 IWN_SETBITS(sc, IWN_RESET, IWN_RESET_SW);
6083 /* Clear "initialization complete" bit. */
6084 IWN_CLRBITS(sc, IWN_GP_CNTRL, IWN_GP_CNTRL_INIT_DONE);
6088 iwn4965_nic_config(struct iwn_softc *sc)
6090 if (IWN_RFCFG_TYPE(sc->rfcfg) == 1) {
6092 * I don't believe this to be correct but this is what the
6093 * vendor driver is doing. Probably the bits should not be
6094 * shifted in IWN_RFCFG_*.
6096 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
6097 IWN_RFCFG_TYPE(sc->rfcfg) |
6098 IWN_RFCFG_STEP(sc->rfcfg) |
6099 IWN_RFCFG_DASH(sc->rfcfg));
6101 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
6102 IWN_HW_IF_CONFIG_RADIO_SI | IWN_HW_IF_CONFIG_MAC_SI);
6107 iwn5000_nic_config(struct iwn_softc *sc)
6112 if (IWN_RFCFG_TYPE(sc->rfcfg) < 3) {
6113 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
6114 IWN_RFCFG_TYPE(sc->rfcfg) |
6115 IWN_RFCFG_STEP(sc->rfcfg) |
6116 IWN_RFCFG_DASH(sc->rfcfg));
6118 IWN_SETBITS(sc, IWN_HW_IF_CONFIG,
6119 IWN_HW_IF_CONFIG_RADIO_SI | IWN_HW_IF_CONFIG_MAC_SI);
6121 error = iwn_nic_lock(sc);
6124 iwn_prph_setbits(sc, IWN_APMG_PS, IWN_APMG_PS_EARLY_PWROFF_DIS);
6126 if (sc->hw_type == IWN_HW_REV_TYPE_1000) {
6128 * Select first Switching Voltage Regulator (1.32V) to
6129 * solve a stability issue related to noisy DC2DC line
6130 * in the silicon of 1000 Series.
6132 tmp = iwn_prph_read(sc, IWN_APMG_DIGITAL_SVR);
6133 tmp &= ~IWN_APMG_DIGITAL_SVR_VOLTAGE_MASK;
6134 tmp |= IWN_APMG_DIGITAL_SVR_VOLTAGE_1_32;
6135 iwn_prph_write(sc, IWN_APMG_DIGITAL_SVR, tmp);
6139 if (sc->sc_flags & IWN_FLAG_INTERNAL_PA) {
6140 /* Use internal power amplifier only. */
6141 IWN_WRITE(sc, IWN_GP_DRIVER, IWN_GP_DRIVER_RADIO_2X2_IPA);
6143 if (sc->hw_type == IWN_HW_REV_TYPE_6050 && sc->calib_ver >= 6) {
6144 /* Indicate that ROM calibration version is >=6. */
6145 IWN_SETBITS(sc, IWN_GP_DRIVER, IWN_GP_DRIVER_CALIB_VER6);
6151 * Take NIC ownership over Intel Active Management Technology (AMT).
6154 iwn_hw_prepare(struct iwn_softc *sc)
6158 /* Check if hardware is ready. */
6159 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_NIC_READY);
6160 for (ntries = 0; ntries < 5; ntries++) {
6161 if (IWN_READ(sc, IWN_HW_IF_CONFIG) &
6162 IWN_HW_IF_CONFIG_NIC_READY)
6167 /* Hardware not ready, force into ready state. */
6168 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_PREPARE);
6169 for (ntries = 0; ntries < 15000; ntries++) {
6170 if (!(IWN_READ(sc, IWN_HW_IF_CONFIG) &
6171 IWN_HW_IF_CONFIG_PREPARE_DONE))
6175 if (ntries == 15000)
6178 /* Hardware should be ready now. */
6179 IWN_SETBITS(sc, IWN_HW_IF_CONFIG, IWN_HW_IF_CONFIG_NIC_READY);
6180 for (ntries = 0; ntries < 5; ntries++) {
6181 if (IWN_READ(sc, IWN_HW_IF_CONFIG) &
6182 IWN_HW_IF_CONFIG_NIC_READY)
6190 iwn_hw_init(struct iwn_softc *sc)
6192 const struct iwn_hal *hal = sc->sc_hal;
6193 int error, chnl, qid;
6195 /* Clear pending interrupts. */
6196 IWN_WRITE(sc, IWN_INT, 0xffffffff);
6198 error = iwn_apm_init(sc);
6200 device_printf(sc->sc_dev,
6201 "%s: could not power ON adapter, error %d\n",
6206 /* Select VMAIN power source. */
6207 error = iwn_nic_lock(sc);
6210 iwn_prph_clrbits(sc, IWN_APMG_PS, IWN_APMG_PS_PWR_SRC_MASK);
6213 /* Perform adapter-specific initialization. */
6214 error = hal->nic_config(sc);
6218 /* Initialize RX ring. */
6219 error = iwn_nic_lock(sc);
6222 IWN_WRITE(sc, IWN_FH_RX_CONFIG, 0);
6223 IWN_WRITE(sc, IWN_FH_RX_WPTR, 0);
6224 /* Set physical address of RX ring (256-byte aligned.) */
6225 IWN_WRITE(sc, IWN_FH_RX_BASE, sc->rxq.desc_dma.paddr >> 8);
6226 /* Set physical address of RX status (16-byte aligned.) */
6227 IWN_WRITE(sc, IWN_FH_STATUS_WPTR, sc->rxq.stat_dma.paddr >> 4);
6229 IWN_WRITE(sc, IWN_FH_RX_CONFIG,
6230 IWN_FH_RX_CONFIG_ENA |
6231 IWN_FH_RX_CONFIG_IGN_RXF_EMPTY | /* HW bug workaround */
6232 IWN_FH_RX_CONFIG_IRQ_DST_HOST |
6233 IWN_FH_RX_CONFIG_SINGLE_FRAME |
6234 IWN_FH_RX_CONFIG_RB_TIMEOUT(0) |
6235 IWN_FH_RX_CONFIG_NRBD(IWN_RX_RING_COUNT_LOG));
6237 IWN_WRITE(sc, IWN_FH_RX_WPTR, (IWN_RX_RING_COUNT - 1) & ~7);
6239 error = iwn_nic_lock(sc);
6243 /* Initialize TX scheduler. */
6244 iwn_prph_write(sc, hal->sched_txfact_addr, 0);
6246 /* Set physical address of "keep warm" page (16-byte aligned.) */
6247 IWN_WRITE(sc, IWN_FH_KW_ADDR, sc->kw_dma.paddr >> 4);
6249 /* Initialize TX rings. */
6250 for (qid = 0; qid < hal->ntxqs; qid++) {
6251 struct iwn_tx_ring *txq = &sc->txq[qid];
6253 /* Set physical address of TX ring (256-byte aligned.) */
6254 IWN_WRITE(sc, IWN_FH_CBBC_QUEUE(qid),
6255 txq->desc_dma.paddr >> 8);
6259 /* Enable DMA channels. */
6260 for (chnl = 0; chnl < hal->ndmachnls; chnl++) {
6261 IWN_WRITE(sc, IWN_FH_TX_CONFIG(chnl),
6262 IWN_FH_TX_CONFIG_DMA_ENA |
6263 IWN_FH_TX_CONFIG_DMA_CREDIT_ENA);
6266 /* Clear "radio off" and "commands blocked" bits. */
6267 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
6268 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_CMD_BLOCKED);
6270 /* Clear pending interrupts. */
6271 IWN_WRITE(sc, IWN_INT, 0xffffffff);
6272 /* Enable interrupt coalescing. */
6273 IWN_WRITE(sc, IWN_INT_COALESCING, 512 / 8);
6274 /* Enable interrupts. */
6275 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask);
6277 /* _Really_ make sure "radio off" bit is cleared! */
6278 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
6279 IWN_WRITE(sc, IWN_UCODE_GP1_CLR, IWN_UCODE_GP1_RFKILL);
6281 error = hal->load_firmware(sc);
6283 device_printf(sc->sc_dev,
6284 "%s: could not load firmware, error %d\n",
6288 /* Wait at most one second for firmware alive notification. */
6289 error = msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", hz);
6291 device_printf(sc->sc_dev,
6292 "%s: timeout waiting for adapter to initialize, error %d\n",
6296 /* Do post-firmware initialization. */
6297 return hal->post_alive(sc);
6301 iwn_hw_stop(struct iwn_softc *sc)
6303 const struct iwn_hal *hal = sc->sc_hal;
6305 int chnl, qid, ntries;
6307 IWN_WRITE(sc, IWN_RESET, IWN_RESET_NEVO);
6309 /* Disable interrupts. */
6310 IWN_WRITE(sc, IWN_INT_MASK, 0);
6311 IWN_WRITE(sc, IWN_INT, 0xffffffff);
6312 IWN_WRITE(sc, IWN_FH_INT, 0xffffffff);
6313 sc->sc_flags &= ~IWN_FLAG_USE_ICT;
6315 /* Make sure we no longer hold the NIC lock. */
6318 /* Stop TX scheduler. */
6319 iwn_prph_write(sc, hal->sched_txfact_addr, 0);
6321 /* Stop all DMA channels. */
6322 if (iwn_nic_lock(sc) == 0) {
6323 for (chnl = 0; chnl < hal->ndmachnls; chnl++) {
6324 IWN_WRITE(sc, IWN_FH_TX_CONFIG(chnl), 0);
6325 for (ntries = 0; ntries < 200; ntries++) {
6326 tmp = IWN_READ(sc, IWN_FH_TX_STATUS);
6327 if ((tmp & IWN_FH_TX_STATUS_IDLE(chnl)) ==
6328 IWN_FH_TX_STATUS_IDLE(chnl))
6337 iwn_reset_rx_ring(sc, &sc->rxq);
6339 /* Reset all TX rings. */
6340 for (qid = 0; qid < hal->ntxqs; qid++)
6341 iwn_reset_tx_ring(sc, &sc->txq[qid]);
6343 if (iwn_nic_lock(sc) == 0) {
6344 iwn_prph_write(sc, IWN_APMG_CLK_DIS,
6345 IWN_APMG_CLK_CTRL_DMA_CLK_RQT);
6350 /* Power OFF adapter. */
6355 iwn_init_locked(struct iwn_softc *sc)
6357 struct ifnet *ifp = sc->sc_ifp;
6360 IWN_LOCK_ASSERT(sc);
6362 error = iwn_hw_prepare(sc);
6364 device_printf(sc->sc_dev, "%s: hardware not ready, eror %d\n",
6369 /* Initialize interrupt mask to default value. */
6370 sc->int_mask = IWN_INT_MASK_DEF;
6371 sc->sc_flags &= ~IWN_FLAG_USE_ICT;
6373 /* Check that the radio is not disabled by hardware switch. */
6374 if (!(IWN_READ(sc, IWN_GP_CNTRL) & IWN_GP_CNTRL_RFKILL)) {
6375 device_printf(sc->sc_dev,
6376 "radio is disabled by hardware switch\n");
6378 /* Enable interrupts to get RF toggle notifications. */
6379 IWN_WRITE(sc, IWN_INT, 0xffffffff);
6380 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask);
6384 /* Read firmware images from the filesystem. */
6385 error = iwn_read_firmware(sc);
6387 device_printf(sc->sc_dev,
6388 "%s: could not read firmware, error %d\n",
6393 /* Initialize hardware and upload firmware. */
6394 error = iwn_hw_init(sc);
6395 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD);
6398 device_printf(sc->sc_dev,
6399 "%s: could not initialize hardware, error %d\n",
6404 /* Configure adapter now that it is ready. */
6405 error = iwn_config(sc);
6407 device_printf(sc->sc_dev,
6408 "%s: could not configure device, error %d\n",
6413 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
6414 ifp->if_drv_flags |= IFF_DRV_RUNNING;
6419 iwn_stop_locked(sc);
6425 struct iwn_softc *sc = arg;
6426 struct ifnet *ifp = sc->sc_ifp;
6427 struct ieee80211com *ic = ifp->if_l2com;
6430 iwn_init_locked(sc);
6433 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6434 ieee80211_start_all(ic);
6438 iwn_stop_locked(struct iwn_softc *sc)
6440 struct ifnet *ifp = sc->sc_ifp;
6442 IWN_LOCK_ASSERT(sc);
6444 sc->sc_tx_timer = 0;
6445 callout_stop(&sc->sc_timer_to);
6446 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
6448 /* Power OFF hardware. */
6453 iwn_stop(struct iwn_softc *sc)
6456 iwn_stop_locked(sc);
6461 * Callback from net80211 to start a scan.
6464 iwn_scan_start(struct ieee80211com *ic)
6466 struct ifnet *ifp = ic->ic_ifp;
6467 struct iwn_softc *sc = ifp->if_softc;
6470 /* make the link LED blink while we're scanning */
6471 iwn_set_led(sc, IWN_LED_LINK, 20, 2);
6476 * Callback from net80211 to terminate a scan.
6479 iwn_scan_end(struct ieee80211com *ic)
6481 struct ifnet *ifp = ic->ic_ifp;
6482 struct iwn_softc *sc = ifp->if_softc;
6483 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
6486 if (vap->iv_state == IEEE80211_S_RUN) {
6487 /* Set link LED to ON status if we are associated */
6488 iwn_set_led(sc, IWN_LED_LINK, 0, 1);
6494 * Callback from net80211 to force a channel change.
6497 iwn_set_channel(struct ieee80211com *ic)
6499 const struct ieee80211_channel *c = ic->ic_curchan;
6500 struct ifnet *ifp = ic->ic_ifp;
6501 struct iwn_softc *sc = ifp->if_softc;
6504 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq);
6505 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags);
6506 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq);
6507 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags);
6512 * Callback from net80211 to start scanning of the current channel.
6515 iwn_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell)
6517 struct ieee80211vap *vap = ss->ss_vap;
6518 struct iwn_softc *sc = vap->iv_ic->ic_ifp->if_softc;
6522 error = iwn_scan(sc);
6525 ieee80211_cancel_scan(vap);
6529 * Callback from net80211 to handle the minimum dwell time being met.
6530 * The intent is to terminate the scan but we just let the firmware
6531 * notify us when it's finished as we have no safe way to abort it.
6534 iwn_scan_mindwell(struct ieee80211_scan_state *ss)
6536 /* NB: don't try to abort scan; wait for firmware to finish */
6539 static struct iwn_eeprom_chan *
6540 iwn_find_eeprom_channel(struct iwn_softc *sc, struct ieee80211_channel *c)
6544 for (j = 0; j < 7; j++) {
6545 for (i = 0; i < iwn_bands[j].nchan; i++) {
6546 if (iwn_bands[j].chan[i] == c->ic_ieee)
6547 return &sc->eeprom_channels[j][i];
6555 * Enforce flags read from EEPROM.
6558 iwn_setregdomain(struct ieee80211com *ic, struct ieee80211_regdomain *rd,
6559 int nchan, struct ieee80211_channel chans[])
6561 struct iwn_softc *sc = ic->ic_ifp->if_softc;
6564 for (i = 0; i < nchan; i++) {
6565 struct ieee80211_channel *c = &chans[i];
6566 struct iwn_eeprom_chan *channel;
6568 channel = iwn_find_eeprom_channel(sc, c);
6569 if (channel == NULL) {
6570 if_printf(ic->ic_ifp,
6571 "%s: invalid channel %u freq %u/0x%x\n",
6572 __func__, c->ic_ieee, c->ic_freq, c->ic_flags);
6575 c->ic_flags |= iwn_eeprom_channel_flags(channel);
6582 iwn_hw_reset(void *arg0, int pending)
6584 struct iwn_softc *sc = arg0;
6585 struct ifnet *ifp = sc->sc_ifp;
6586 struct ieee80211com *ic = ifp->if_l2com;
6590 ieee80211_notify_radio(ic, 1);
6594 iwn_radio_on(void *arg0, int pending)
6596 struct iwn_softc *sc = arg0;
6597 struct ifnet *ifp = sc->sc_ifp;
6598 struct ieee80211com *ic = ifp->if_l2com;
6599 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
6603 ieee80211_init(vap);
6608 iwn_radio_off(void *arg0, int pending)
6610 struct iwn_softc *sc = arg0;
6611 struct ifnet *ifp = sc->sc_ifp;
6612 struct ieee80211com *ic = ifp->if_l2com;
6613 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
6617 ieee80211_stop(vap);
6619 /* Enable interrupts to get RF toggle notification. */
6621 IWN_WRITE(sc, IWN_INT, 0xffffffff);
6622 IWN_WRITE(sc, IWN_INT_MASK, sc->int_mask);
6627 iwn_sysctlattach(struct iwn_softc *sc)
6629 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
6630 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
6634 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
6635 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "control debugging printfs");
6640 iwn_shutdown(device_t dev)
6642 struct iwn_softc *sc = device_get_softc(dev);
6649 iwn_suspend(device_t dev)
6651 struct iwn_softc *sc = device_get_softc(dev);
6652 struct ifnet *ifp = sc->sc_ifp;
6653 struct ieee80211com *ic = ifp->if_l2com;
6654 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
6658 ieee80211_stop(vap);
6663 iwn_resume(device_t dev)
6665 struct iwn_softc *sc = device_get_softc(dev);
6666 struct ifnet *ifp = sc->sc_ifp;
6667 struct ieee80211com *ic = ifp->if_l2com;
6668 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
6670 /* Clear device-specific "PCI retry timeout" register (41h). */
6671 pci_write_config(dev, 0x41, 0, 1);
6673 if (ifp->if_flags & IFF_UP) {
6676 ieee80211_init(vap);
6677 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6685 iwn_intr_str(uint8_t cmd)
6689 case IWN_UC_READY: return "UC_READY";
6690 case IWN_ADD_NODE_DONE: return "ADD_NODE_DONE";
6691 case IWN_TX_DONE: return "TX_DONE";
6692 case IWN_START_SCAN: return "START_SCAN";
6693 case IWN_STOP_SCAN: return "STOP_SCAN";
6694 case IWN_RX_STATISTICS: return "RX_STATS";
6695 case IWN_BEACON_STATISTICS: return "BEACON_STATS";
6696 case IWN_STATE_CHANGED: return "STATE_CHANGED";
6697 case IWN_BEACON_MISSED: return "BEACON_MISSED";
6698 case IWN_RX_PHY: return "RX_PHY";
6699 case IWN_MPDU_RX_DONE: return "MPDU_RX_DONE";
6700 case IWN_RX_DONE: return "RX_DONE";
6702 /* Command Notifications */
6703 case IWN_CMD_RXON: return "IWN_CMD_RXON";
6704 case IWN_CMD_RXON_ASSOC: return "IWN_CMD_RXON_ASSOC";
6705 case IWN_CMD_EDCA_PARAMS: return "IWN_CMD_EDCA_PARAMS";
6706 case IWN_CMD_TIMING: return "IWN_CMD_TIMING";
6707 case IWN_CMD_LINK_QUALITY: return "IWN_CMD_LINK_QUALITY";
6708 case IWN_CMD_SET_LED: return "IWN_CMD_SET_LED";
6709 case IWN5000_CMD_WIMAX_COEX: return "IWN5000_CMD_WIMAX_COEX";
6710 case IWN5000_CMD_CALIB_CONFIG: return "IWN5000_CMD_CALIB_CONFIG";
6711 case IWN5000_CMD_CALIB_RESULT: return "IWN5000_CMD_CALIB_RESULT";
6712 case IWN5000_CMD_CALIB_COMPLETE: return "IWN5000_CMD_CALIB_COMPLETE";
6713 case IWN_CMD_SET_POWER_MODE: return "IWN_CMD_SET_POWER_MODE";
6714 case IWN_CMD_SCAN: return "IWN_CMD_SCAN";
6715 case IWN_CMD_SCAN_RESULTS: return "IWN_CMD_SCAN_RESULTS";
6716 case IWN_CMD_TXPOWER: return "IWN_CMD_TXPOWER";
6717 case IWN_CMD_TXPOWER_DBM: return "IWN_CMD_TXPOWER_DBM";
6718 case IWN5000_CMD_TX_ANT_CONFIG: return "IWN5000_CMD_TX_ANT_CONFIG";
6719 case IWN_CMD_BT_COEX: return "IWN_CMD_BT_COEX";
6720 case IWN_CMD_SET_CRITICAL_TEMP: return "IWN_CMD_SET_CRITICAL_TEMP";
6721 case IWN_CMD_SET_SENSITIVITY: return "IWN_CMD_SET_SENSITIVITY";
6722 case IWN_CMD_PHY_CALIB: return "IWN_CMD_PHY_CALIB";
6724 return "UNKNOWN INTR NOTIF/CMD";
6726 #endif /* IWN_DEBUG */
6728 static device_method_t iwn_methods[] = {
6729 /* Device interface */
6730 DEVMETHOD(device_probe, iwn_probe),
6731 DEVMETHOD(device_attach, iwn_attach),
6732 DEVMETHOD(device_detach, iwn_detach),
6733 DEVMETHOD(device_shutdown, iwn_shutdown),
6734 DEVMETHOD(device_suspend, iwn_suspend),
6735 DEVMETHOD(device_resume, iwn_resume),
6739 static driver_t iwn_driver = {
6742 sizeof (struct iwn_softc)
6744 static devclass_t iwn_devclass;
6746 DRIVER_MODULE(iwn, pci, iwn_driver, iwn_devclass, 0, 0);
6747 MODULE_DEPEND(iwn, pci, 1, 1, 1);
6748 MODULE_DEPEND(iwn, firmware, 1, 1, 1);
6749 MODULE_DEPEND(iwn, wlan, 1, 1, 1);