2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
4 * Copyright (c) 2005-2006 Sam Leffler, Errno Consulting
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: head/sys/dev/iwi/if_iwi.c 298818 2016-04-29 22:14:11Z avos $
33 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
34 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <sys/sockio.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/module.h>
48 #include <sys/endian.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/linker.h>
53 #include <sys/firmware.h>
54 #include <sys/taskqueue.h>
55 #if defined(__DragonFly__)
56 #include <sys/devfs.h>
59 #if !defined(__DragonFly__)
60 #include <machine/bus.h>
61 #include <machine/resource.h>
65 #if defined(__DragonFly__)
66 #include <bus/pci/pcivar.h>
67 #include <bus/pci/pcireg.h>
69 #include <dev/pci/pcireg.h>
70 #include <dev/pci/pcivar.h>
75 #include <net/if_var.h>
76 #include <net/if_arp.h>
77 #include <net/ethernet.h>
78 #include <net/if_dl.h>
79 #include <net/if_media.h>
80 #include <net/if_types.h>
82 #if defined(__DragonFly__)
83 #include <netproto/802_11/ieee80211_var.h>
84 #include <netproto/802_11/ieee80211_radiotap.h>
85 #include <netproto/802_11/ieee80211_input.h>
86 #include <netproto/802_11/ieee80211_regdomain.h>
88 #include <net80211/ieee80211_var.h>
89 #include <net80211/ieee80211_radiotap.h>
90 #include <net80211/ieee80211_input.h>
91 #include <net80211/ieee80211_regdomain.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip.h>
98 #include <netinet/if_ether.h>
100 #if defined(__DragonFly__)
101 #include "if_iwireg.h"
102 #include "if_iwivar.h"
104 #include <dev/iwi/if_iwireg.h>
105 #include <dev/iwi/if_iwivar.h>
110 #define DPRINTF(x) do { if (iwi_debug > 0) kprintf x; } while (0)
111 #define DPRINTFN(n, x) do { if (iwi_debug >= (n)) kprintf x; } while (0)
113 SYSCTL_INT(_debug
, OID_AUTO
, iwi
, CTLFLAG_RW
, &iwi_debug
, 0, "iwi debug level");
115 static const char *iwi_fw_states
[] = {
116 "IDLE", /* IWI_FW_IDLE */
117 "LOADING", /* IWI_FW_LOADING */
118 "ASSOCIATING", /* IWI_FW_ASSOCIATING */
119 "DISASSOCIATING", /* IWI_FW_DISASSOCIATING */
120 "SCANNING", /* IWI_FW_SCANNING */
124 #define DPRINTFN(n, x)
127 MODULE_DEPEND(iwi
, pci
, 1, 1, 1);
128 MODULE_DEPEND(iwi
, wlan
, 1, 1, 1);
129 MODULE_DEPEND(iwi
, firmware
, 1, 1, 1);
143 static const struct iwi_ident iwi_ident_table
[] = {
144 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" },
145 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" },
146 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" },
147 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" },
152 static struct ieee80211vap
*iwi_vap_create(struct ieee80211com
*,
153 const char [IFNAMSIZ
], int, enum ieee80211_opmode
, int,
154 const uint8_t [IEEE80211_ADDR_LEN
],
155 const uint8_t [IEEE80211_ADDR_LEN
]);
156 static void iwi_vap_delete(struct ieee80211vap
*);
157 static void iwi_dma_map_addr(void *, bus_dma_segment_t
*, int, int);
158 static int iwi_alloc_cmd_ring(struct iwi_softc
*, struct iwi_cmd_ring
*,
160 static void iwi_reset_cmd_ring(struct iwi_softc
*, struct iwi_cmd_ring
*);
161 static void iwi_free_cmd_ring(struct iwi_softc
*, struct iwi_cmd_ring
*);
162 static int iwi_alloc_tx_ring(struct iwi_softc
*, struct iwi_tx_ring
*,
163 int, bus_addr_t
, bus_addr_t
);
164 static void iwi_reset_tx_ring(struct iwi_softc
*, struct iwi_tx_ring
*);
165 static void iwi_free_tx_ring(struct iwi_softc
*, struct iwi_tx_ring
*);
166 static int iwi_alloc_rx_ring(struct iwi_softc
*, struct iwi_rx_ring
*,
168 static void iwi_reset_rx_ring(struct iwi_softc
*, struct iwi_rx_ring
*);
169 static void iwi_free_rx_ring(struct iwi_softc
*, struct iwi_rx_ring
*);
170 static struct ieee80211_node
*iwi_node_alloc(struct ieee80211vap
*,
171 const uint8_t [IEEE80211_ADDR_LEN
]);
172 static void iwi_node_free(struct ieee80211_node
*);
173 static void iwi_media_status(struct ifnet
*, struct ifmediareq
*);
174 static int iwi_newstate(struct ieee80211vap
*, enum ieee80211_state
, int);
175 static void iwi_wme_init(struct iwi_softc
*);
176 static int iwi_wme_setparams(struct iwi_softc
*);
177 static int iwi_wme_update(struct ieee80211com
*);
178 static uint16_t iwi_read_prom_word(struct iwi_softc
*, uint8_t);
179 static void iwi_frame_intr(struct iwi_softc
*, struct iwi_rx_data
*, int,
181 static void iwi_notification_intr(struct iwi_softc
*, struct iwi_notif
*);
182 static void iwi_rx_intr(struct iwi_softc
*);
183 static void iwi_tx_intr(struct iwi_softc
*, struct iwi_tx_ring
*);
184 static void iwi_intr(void *);
185 static int iwi_cmd(struct iwi_softc
*, uint8_t, void *, uint8_t);
186 static void iwi_write_ibssnode(struct iwi_softc
*, const u_int8_t
[], int);
187 static int iwi_tx_start(struct iwi_softc
*, struct mbuf
*,
188 struct ieee80211_node
*, int);
189 static int iwi_raw_xmit(struct ieee80211_node
*, struct mbuf
*,
190 const struct ieee80211_bpf_params
*);
191 static void iwi_start(struct iwi_softc
*);
192 static int iwi_transmit(struct ieee80211com
*, struct mbuf
*);
193 static void iwi_watchdog(void *);
194 static void iwi_parent(struct ieee80211com
*);
195 static void iwi_stop_master(struct iwi_softc
*);
196 static int iwi_reset(struct iwi_softc
*);
197 static int iwi_load_ucode(struct iwi_softc
*, const struct iwi_fw
*);
198 static int iwi_load_firmware(struct iwi_softc
*, const struct iwi_fw
*);
199 static void iwi_release_fw_dma(struct iwi_softc
*sc
);
200 static int iwi_config(struct iwi_softc
*);
201 static int iwi_get_firmware(struct iwi_softc
*, enum ieee80211_opmode
);
202 static void iwi_put_firmware(struct iwi_softc
*);
203 static void iwi_monitor_scan(void *, int);
204 static int iwi_scanchan(struct iwi_softc
*, unsigned long, int);
205 static void iwi_scan_start(struct ieee80211com
*);
206 static void iwi_scan_end(struct ieee80211com
*);
207 static void iwi_set_channel(struct ieee80211com
*);
208 static void iwi_scan_curchan(struct ieee80211_scan_state
*, unsigned long maxdwell
);
209 static void iwi_scan_mindwell(struct ieee80211_scan_state
*);
210 static int iwi_auth_and_assoc(struct iwi_softc
*, struct ieee80211vap
*);
211 static void iwi_disassoc(void *, int);
212 static int iwi_disassociate(struct iwi_softc
*, int quiet
);
213 static void iwi_init_locked(struct iwi_softc
*);
214 static void iwi_init(void *);
215 static int iwi_init_fw_dma(struct iwi_softc
*, int);
216 static void iwi_stop_locked(void *);
217 static void iwi_stop(struct iwi_softc
*);
218 static void iwi_restart(void *, int);
219 static int iwi_getrfkill(struct iwi_softc
*);
220 static void iwi_radio_on(void *, int);
221 static void iwi_radio_off(void *, int);
222 static void iwi_sysctlattach(struct iwi_softc
*);
223 static void iwi_led_event(struct iwi_softc
*, int);
224 static void iwi_ledattach(struct iwi_softc
*);
226 static int iwi_probe(device_t
);
227 static int iwi_attach(device_t
);
228 static int iwi_detach(device_t
);
229 static int iwi_shutdown(device_t
);
230 static int iwi_suspend(device_t
);
231 static int iwi_resume(device_t
);
233 static device_method_t iwi_methods
[] = {
234 /* Device interface */
235 DEVMETHOD(device_probe
, iwi_probe
),
236 DEVMETHOD(device_attach
, iwi_attach
),
237 DEVMETHOD(device_detach
, iwi_detach
),
238 DEVMETHOD(device_shutdown
, iwi_shutdown
),
239 DEVMETHOD(device_suspend
, iwi_suspend
),
240 DEVMETHOD(device_resume
, iwi_resume
),
245 static driver_t iwi_driver
= {
248 sizeof (struct iwi_softc
)
251 static devclass_t iwi_devclass
;
253 DRIVER_MODULE(iwi
, pci
, iwi_driver
, iwi_devclass
, NULL
, NULL
);
255 MODULE_VERSION(iwi
, 1);
257 static __inline
uint8_t
258 MEM_READ_1(struct iwi_softc
*sc
, uint32_t addr
)
260 CSR_WRITE_4(sc
, IWI_CSR_INDIRECT_ADDR
, addr
);
261 return CSR_READ_1(sc
, IWI_CSR_INDIRECT_DATA
);
264 static __inline
uint32_t
265 MEM_READ_4(struct iwi_softc
*sc
, uint32_t addr
)
267 CSR_WRITE_4(sc
, IWI_CSR_INDIRECT_ADDR
, addr
);
268 return CSR_READ_4(sc
, IWI_CSR_INDIRECT_DATA
);
272 iwi_probe(device_t dev
)
274 const struct iwi_ident
*ident
;
276 for (ident
= iwi_ident_table
; ident
->name
!= NULL
; ident
++) {
277 if (pci_get_vendor(dev
) == ident
->vendor
&&
278 pci_get_device(dev
) == ident
->device
) {
279 device_set_desc(dev
, ident
->name
);
280 return (BUS_PROBE_DEFAULT
);
287 iwi_attach(device_t dev
)
289 struct iwi_softc
*sc
= device_get_softc(dev
);
290 struct ieee80211com
*ic
= &sc
->sc_ic
;
292 uint8_t bands
[IEEE80211_MODE_BYTES
];
298 mbufq_init(&sc
->sc_snd
, ifqmaxlen
);
300 #if defined(__DragonFly__)
301 devfs_clone_bitmap_init(&sc
->sc_unr
);
303 sc
->sc_unr
= new_unrhdr(1, IWI_MAX_IBSSNODE
-1, &sc
->sc_mtx
);
306 TASK_INIT(&sc
->sc_radiontask
, 0, iwi_radio_on
, sc
);
307 TASK_INIT(&sc
->sc_radiofftask
, 0, iwi_radio_off
, sc
);
308 TASK_INIT(&sc
->sc_restarttask
, 0, iwi_restart
, sc
);
309 TASK_INIT(&sc
->sc_disassoctask
, 0, iwi_disassoc
, sc
);
310 TASK_INIT(&sc
->sc_monitortask
, 0, iwi_monitor_scan
, sc
);
312 #if defined(__DragonFly__)
313 callout_init_lk(&sc
->sc_wdtimer
, &sc
->sc_lock
);
314 callout_init_lk(&sc
->sc_rftimer
, &sc
->sc_lock
);
316 callout_init_mtx(&sc
->sc_wdtimer
, &sc
->sc_mtx
, 0);
317 callout_init_mtx(&sc
->sc_rftimer
, &sc
->sc_mtx
, 0);
320 pci_write_config(dev
, 0x41, 0, 1);
322 /* enable bus-mastering */
323 pci_enable_busmaster(dev
);
326 sc
->mem
= bus_alloc_resource_any(dev
, SYS_RES_MEMORY
, &i
, RF_ACTIVE
);
327 if (sc
->mem
== NULL
) {
328 device_printf(dev
, "could not allocate memory resource\n");
332 sc
->sc_st
= rman_get_bustag(sc
->mem
);
333 sc
->sc_sh
= rman_get_bushandle(sc
->mem
);
336 sc
->irq
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
, &i
,
337 RF_ACTIVE
| RF_SHAREABLE
);
338 if (sc
->irq
== NULL
) {
339 device_printf(dev
, "could not allocate interrupt resource\n");
343 if (iwi_reset(sc
) != 0) {
344 device_printf(dev
, "could not reset adapter\n");
351 if (iwi_alloc_cmd_ring(sc
, &sc
->cmdq
, IWI_CMD_RING_COUNT
) != 0) {
352 device_printf(dev
, "could not allocate Cmd ring\n");
356 for (i
= 0; i
< 4; i
++) {
357 error
= iwi_alloc_tx_ring(sc
, &sc
->txq
[i
], IWI_TX_RING_COUNT
,
358 IWI_CSR_TX1_RIDX
+ i
* 4,
359 IWI_CSR_TX1_WIDX
+ i
* 4);
361 device_printf(dev
, "could not allocate Tx ring %d\n",
367 if (iwi_alloc_rx_ring(sc
, &sc
->rxq
, IWI_RX_RING_COUNT
) != 0) {
368 device_printf(dev
, "could not allocate Rx ring\n");
375 ic
->ic_name
= device_get_nameunit(dev
);
376 ic
->ic_opmode
= IEEE80211_M_STA
;
377 ic
->ic_phytype
= IEEE80211_T_OFDM
; /* not only, but not used */
379 /* set device capabilities */
381 IEEE80211_C_STA
/* station mode supported */
382 | IEEE80211_C_IBSS
/* IBSS mode supported */
383 | IEEE80211_C_MONITOR
/* monitor mode supported */
384 | IEEE80211_C_PMGT
/* power save supported */
385 | IEEE80211_C_SHPREAMBLE
/* short preamble supported */
386 | IEEE80211_C_WPA
/* 802.11i */
387 | IEEE80211_C_WME
/* 802.11e */
389 | IEEE80211_C_BGSCAN
/* capable of bg scanning */
393 /* read MAC address from EEPROM */
394 val
= iwi_read_prom_word(sc
, IWI_EEPROM_MAC
+ 0);
395 ic
->ic_macaddr
[0] = val
& 0xff;
396 ic
->ic_macaddr
[1] = val
>> 8;
397 val
= iwi_read_prom_word(sc
, IWI_EEPROM_MAC
+ 1);
398 ic
->ic_macaddr
[2] = val
& 0xff;
399 ic
->ic_macaddr
[3] = val
>> 8;
400 val
= iwi_read_prom_word(sc
, IWI_EEPROM_MAC
+ 2);
401 ic
->ic_macaddr
[4] = val
& 0xff;
402 ic
->ic_macaddr
[5] = val
>> 8;
404 memset(bands
, 0, sizeof(bands
));
405 setbit(bands
, IEEE80211_MODE_11B
);
406 setbit(bands
, IEEE80211_MODE_11G
);
407 if (pci_get_device(dev
) >= 0x4223)
408 setbit(bands
, IEEE80211_MODE_11A
);
409 ieee80211_init_channels(ic
, NULL
, bands
);
411 ieee80211_ifattach(ic
);
412 /* override default methods */
413 ic
->ic_node_alloc
= iwi_node_alloc
;
414 sc
->sc_node_free
= ic
->ic_node_free
;
415 ic
->ic_node_free
= iwi_node_free
;
416 ic
->ic_raw_xmit
= iwi_raw_xmit
;
417 ic
->ic_scan_start
= iwi_scan_start
;
418 ic
->ic_scan_end
= iwi_scan_end
;
419 ic
->ic_set_channel
= iwi_set_channel
;
420 ic
->ic_scan_curchan
= iwi_scan_curchan
;
421 ic
->ic_scan_mindwell
= iwi_scan_mindwell
;
422 ic
->ic_wme
.wme_update
= iwi_wme_update
;
424 ic
->ic_vap_create
= iwi_vap_create
;
425 ic
->ic_vap_delete
= iwi_vap_delete
;
426 ic
->ic_transmit
= iwi_transmit
;
427 ic
->ic_parent
= iwi_parent
;
429 ieee80211_radiotap_attach(ic
,
430 &sc
->sc_txtap
.wt_ihdr
, sizeof(sc
->sc_txtap
),
431 IWI_TX_RADIOTAP_PRESENT
,
432 &sc
->sc_rxtap
.wr_ihdr
, sizeof(sc
->sc_rxtap
),
433 IWI_RX_RADIOTAP_PRESENT
);
435 iwi_sysctlattach(sc
);
439 * Hook our interrupt after all initialization is complete.
441 #if defined(__DragonFly__)
442 error
= bus_setup_intr(dev
, sc
->irq
, INTR_MPSAFE
,
443 iwi_intr
, sc
, &sc
->sc_ih
, &wlan_global_serializer
);
445 error
= bus_setup_intr(dev
, sc
->irq
, INTR_TYPE_NET
| INTR_MPSAFE
,
446 NULL
, iwi_intr
, sc
, &sc
->sc_ih
);
449 device_printf(dev
, "could not set up interrupt\n");
454 ieee80211_announce(ic
);
464 iwi_detach(device_t dev
)
466 struct iwi_softc
*sc
= device_get_softc(dev
);
467 struct ieee80211com
*ic
= &sc
->sc_ic
;
469 bus_teardown_intr(dev
, sc
->irq
, sc
->sc_ih
);
471 /* NB: do early to drain any pending tasks */
472 ieee80211_draintask(ic
, &sc
->sc_radiontask
);
473 ieee80211_draintask(ic
, &sc
->sc_radiofftask
);
474 ieee80211_draintask(ic
, &sc
->sc_restarttask
);
475 ieee80211_draintask(ic
, &sc
->sc_disassoctask
);
476 ieee80211_draintask(ic
, &sc
->sc_monitortask
);
480 ieee80211_ifdetach(ic
);
482 iwi_put_firmware(sc
);
483 iwi_release_fw_dma(sc
);
485 iwi_free_cmd_ring(sc
, &sc
->cmdq
);
486 iwi_free_tx_ring(sc
, &sc
->txq
[0]);
487 iwi_free_tx_ring(sc
, &sc
->txq
[1]);
488 iwi_free_tx_ring(sc
, &sc
->txq
[2]);
489 iwi_free_tx_ring(sc
, &sc
->txq
[3]);
490 iwi_free_rx_ring(sc
, &sc
->rxq
);
492 bus_release_resource(dev
, SYS_RES_IRQ
, rman_get_rid(sc
->irq
), sc
->irq
);
494 bus_release_resource(dev
, SYS_RES_MEMORY
, rman_get_rid(sc
->mem
),
497 #if defined(__DragonFly__)
498 devfs_clone_bitmap_uninit(&sc
->sc_unr
);
500 delete_unrhdr(sc
->sc_unr
);
502 mbufq_drain(&sc
->sc_snd
);
504 IWI_LOCK_DESTROY(sc
);
509 static struct ieee80211vap
*
510 iwi_vap_create(struct ieee80211com
*ic
, const char name
[IFNAMSIZ
], int unit
,
511 enum ieee80211_opmode opmode
, int flags
,
512 const uint8_t bssid
[IEEE80211_ADDR_LEN
],
513 const uint8_t mac
[IEEE80211_ADDR_LEN
])
515 struct iwi_softc
*sc
= ic
->ic_softc
;
517 struct ieee80211vap
*vap
;
520 if (!TAILQ_EMPTY(&ic
->ic_vaps
)) /* only one at a time */
523 * Get firmware image (and possibly dma memory) on mode change.
525 if (iwi_get_firmware(sc
, opmode
))
527 /* allocate DMA memory for mapping firmware image */
529 if (sc
->fw_boot
.size
> i
)
530 i
= sc
->fw_boot
.size
;
531 /* XXX do we dma the ucode as well ? */
532 if (sc
->fw_uc
.size
> i
)
534 if (iwi_init_fw_dma(sc
, i
))
537 ivp
= kmalloc(sizeof(struct iwi_vap
), M_80211_VAP
, M_WAITOK
| M_ZERO
);
539 ieee80211_vap_setup(ic
, vap
, name
, unit
, opmode
, flags
, bssid
);
540 /* override the default, the setting comes from the linux driver */
541 vap
->iv_bmissthreshold
= 24;
542 /* override with driver methods */
543 ivp
->iwi_newstate
= vap
->iv_newstate
;
544 vap
->iv_newstate
= iwi_newstate
;
547 ieee80211_vap_attach(vap
, ieee80211_media_change
, iwi_media_status
,
549 ic
->ic_opmode
= opmode
;
554 iwi_vap_delete(struct ieee80211vap
*vap
)
556 struct iwi_vap
*ivp
= IWI_VAP(vap
);
558 ieee80211_vap_detach(vap
);
559 kfree(ivp
, M_80211_VAP
);
563 iwi_dma_map_addr(void *arg
, bus_dma_segment_t
*segs
, int nseg
, int error
)
568 KASSERT(nseg
== 1, ("too many DMA segments, %d should be 1", nseg
));
570 *(bus_addr_t
*)arg
= segs
[0].ds_addr
;
574 iwi_alloc_cmd_ring(struct iwi_softc
*sc
, struct iwi_cmd_ring
*ring
, int count
)
580 ring
->cur
= ring
->next
= 0;
582 #if defined(__DragonFly__)
583 error
= bus_dma_tag_create(NULL
, 4, 0,
584 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
585 count
* IWI_CMD_DESC_SIZE
, 1, count
* IWI_CMD_DESC_SIZE
,
586 0 , &ring
->desc_dmat
);
588 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 4, 0,
589 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
590 count
* IWI_CMD_DESC_SIZE
, 1, count
* IWI_CMD_DESC_SIZE
, 0,
591 NULL
, NULL
, &ring
->desc_dmat
);
594 device_printf(sc
->sc_dev
, "could not create desc DMA tag\n");
598 error
= bus_dmamem_alloc(ring
->desc_dmat
, (void **)&ring
->desc
,
599 BUS_DMA_NOWAIT
| BUS_DMA_ZERO
, &ring
->desc_map
);
601 device_printf(sc
->sc_dev
, "could not allocate DMA memory\n");
605 error
= bus_dmamap_load(ring
->desc_dmat
, ring
->desc_map
, ring
->desc
,
606 count
* IWI_CMD_DESC_SIZE
, iwi_dma_map_addr
, &ring
->physaddr
, 0);
608 device_printf(sc
->sc_dev
, "could not load desc DMA map\n");
614 fail
: iwi_free_cmd_ring(sc
, ring
);
619 iwi_reset_cmd_ring(struct iwi_softc
*sc
, struct iwi_cmd_ring
*ring
)
622 ring
->cur
= ring
->next
= 0;
626 iwi_free_cmd_ring(struct iwi_softc
*sc
, struct iwi_cmd_ring
*ring
)
628 if (ring
->desc
!= NULL
) {
629 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
,
630 BUS_DMASYNC_POSTWRITE
);
631 bus_dmamap_unload(ring
->desc_dmat
, ring
->desc_map
);
632 bus_dmamem_free(ring
->desc_dmat
, ring
->desc
, ring
->desc_map
);
635 if (ring
->desc_dmat
!= NULL
)
636 bus_dma_tag_destroy(ring
->desc_dmat
);
640 iwi_alloc_tx_ring(struct iwi_softc
*sc
, struct iwi_tx_ring
*ring
, int count
,
641 bus_addr_t csr_ridx
, bus_addr_t csr_widx
)
647 ring
->cur
= ring
->next
= 0;
648 ring
->csr_ridx
= csr_ridx
;
649 ring
->csr_widx
= csr_widx
;
651 #if defined(__DragonFly__)
652 error
= bus_dma_tag_create(NULL
, 4, 0, BUS_SPACE_MAXADDR_32BIT
,
653 BUS_SPACE_MAXADDR
, NULL
, NULL
, count
* IWI_TX_DESC_SIZE
, 1,
654 count
* IWI_TX_DESC_SIZE
, 0, &ring
->desc_dmat
);
656 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 4, 0,
657 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
658 count
* IWI_TX_DESC_SIZE
, 1, count
* IWI_TX_DESC_SIZE
, 0, NULL
,
659 NULL
, &ring
->desc_dmat
);
662 device_printf(sc
->sc_dev
, "could not create desc DMA tag\n");
666 error
= bus_dmamem_alloc(ring
->desc_dmat
, (void **)&ring
->desc
,
667 BUS_DMA_NOWAIT
| BUS_DMA_ZERO
, &ring
->desc_map
);
669 device_printf(sc
->sc_dev
, "could not allocate DMA memory\n");
673 error
= bus_dmamap_load(ring
->desc_dmat
, ring
->desc_map
, ring
->desc
,
674 count
* IWI_TX_DESC_SIZE
, iwi_dma_map_addr
, &ring
->physaddr
, 0);
676 device_printf(sc
->sc_dev
, "could not load desc DMA map\n");
680 ring
->data
= kmalloc(count
* sizeof (struct iwi_tx_data
), M_DEVBUF
,
682 if (ring
->data
== NULL
) {
683 device_printf(sc
->sc_dev
, "could not allocate soft data\n");
688 #if defined(__DragonFly__)
689 error
= bus_dma_tag_create(NULL
, 1, 0, BUS_SPACE_MAXADDR_32BIT
,
690 BUS_SPACE_MAXADDR
, NULL
, NULL
, MCLBYTES
, IWI_MAX_NSEG
,
691 MCLBYTES
, 0, &ring
->data_dmat
);
693 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 1, 0,
694 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
, MCLBYTES
,
695 IWI_MAX_NSEG
, MCLBYTES
, 0, NULL
, NULL
, &ring
->data_dmat
);
698 device_printf(sc
->sc_dev
, "could not create data DMA tag\n");
702 for (i
= 0; i
< count
; i
++) {
703 error
= bus_dmamap_create(ring
->data_dmat
, 0,
706 device_printf(sc
->sc_dev
, "could not create DMA map\n");
713 fail
: iwi_free_tx_ring(sc
, ring
);
718 iwi_reset_tx_ring(struct iwi_softc
*sc
, struct iwi_tx_ring
*ring
)
720 struct iwi_tx_data
*data
;
723 for (i
= 0; i
< ring
->count
; i
++) {
724 data
= &ring
->data
[i
];
726 if (data
->m
!= NULL
) {
727 bus_dmamap_sync(ring
->data_dmat
, data
->map
,
728 BUS_DMASYNC_POSTWRITE
);
729 bus_dmamap_unload(ring
->data_dmat
, data
->map
);
734 if (data
->ni
!= NULL
) {
735 ieee80211_free_node(data
->ni
);
741 ring
->cur
= ring
->next
= 0;
745 iwi_free_tx_ring(struct iwi_softc
*sc
, struct iwi_tx_ring
*ring
)
747 struct iwi_tx_data
*data
;
750 if (ring
->desc
!= NULL
) {
751 bus_dmamap_sync(ring
->desc_dmat
, ring
->desc_map
,
752 BUS_DMASYNC_POSTWRITE
);
753 bus_dmamap_unload(ring
->desc_dmat
, ring
->desc_map
);
754 bus_dmamem_free(ring
->desc_dmat
, ring
->desc
, ring
->desc_map
);
757 if (ring
->desc_dmat
!= NULL
)
758 bus_dma_tag_destroy(ring
->desc_dmat
);
760 if (ring
->data
!= NULL
) {
761 for (i
= 0; i
< ring
->count
; i
++) {
762 data
= &ring
->data
[i
];
764 if (data
->m
!= NULL
) {
765 bus_dmamap_sync(ring
->data_dmat
, data
->map
,
766 BUS_DMASYNC_POSTWRITE
);
767 bus_dmamap_unload(ring
->data_dmat
, data
->map
);
771 if (data
->ni
!= NULL
)
772 ieee80211_free_node(data
->ni
);
774 if (data
->map
!= NULL
)
775 bus_dmamap_destroy(ring
->data_dmat
, data
->map
);
778 kfree(ring
->data
, M_DEVBUF
);
781 if (ring
->data_dmat
!= NULL
)
782 bus_dma_tag_destroy(ring
->data_dmat
);
786 iwi_alloc_rx_ring(struct iwi_softc
*sc
, struct iwi_rx_ring
*ring
, int count
)
788 struct iwi_rx_data
*data
;
794 ring
->data
= kmalloc(count
* sizeof (struct iwi_rx_data
), M_DEVBUF
,
796 if (ring
->data
== NULL
) {
797 device_printf(sc
->sc_dev
, "could not allocate soft data\n");
802 #if defined(__DragonFly__)
803 error
= bus_dma_tag_create(NULL
, 1, 0, BUS_SPACE_MAXADDR_32BIT
,
804 BUS_SPACE_MAXADDR
, NULL
, NULL
, MCLBYTES
, 1, MCLBYTES
,
805 0, &ring
->data_dmat
);
807 error
= bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 1, 0,
808 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
, MCLBYTES
,
809 1, MCLBYTES
, 0, NULL
, NULL
, &ring
->data_dmat
);
812 device_printf(sc
->sc_dev
, "could not create data DMA tag\n");
816 for (i
= 0; i
< count
; i
++) {
817 data
= &ring
->data
[i
];
819 error
= bus_dmamap_create(ring
->data_dmat
, 0, &data
->map
);
821 device_printf(sc
->sc_dev
, "could not create DMA map\n");
825 data
->m
= m_getcl(M_NOWAIT
, MT_DATA
, M_PKTHDR
);
826 if (data
->m
== NULL
) {
827 device_printf(sc
->sc_dev
,
828 "could not allocate rx mbuf\n");
833 error
= bus_dmamap_load(ring
->data_dmat
, data
->map
,
834 mtod(data
->m
, void *), MCLBYTES
, iwi_dma_map_addr
,
837 device_printf(sc
->sc_dev
,
838 "could not load rx buf DMA map");
842 data
->reg
= IWI_CSR_RX_BASE
+ i
* 4;
847 fail
: iwi_free_rx_ring(sc
, ring
);
852 iwi_reset_rx_ring(struct iwi_softc
*sc
, struct iwi_rx_ring
*ring
)
858 iwi_free_rx_ring(struct iwi_softc
*sc
, struct iwi_rx_ring
*ring
)
860 struct iwi_rx_data
*data
;
863 if (ring
->data
!= NULL
) {
864 for (i
= 0; i
< ring
->count
; i
++) {
865 data
= &ring
->data
[i
];
867 if (data
->m
!= NULL
) {
868 bus_dmamap_sync(ring
->data_dmat
, data
->map
,
869 BUS_DMASYNC_POSTREAD
);
870 bus_dmamap_unload(ring
->data_dmat
, data
->map
);
874 if (data
->map
!= NULL
)
875 bus_dmamap_destroy(ring
->data_dmat
, data
->map
);
878 kfree(ring
->data
, M_DEVBUF
);
881 if (ring
->data_dmat
!= NULL
)
882 bus_dma_tag_destroy(ring
->data_dmat
);
886 iwi_shutdown(device_t dev
)
888 struct iwi_softc
*sc
= device_get_softc(dev
);
891 iwi_put_firmware(sc
); /* ??? XXX */
897 iwi_suspend(device_t dev
)
899 struct iwi_softc
*sc
= device_get_softc(dev
);
900 struct ieee80211com
*ic
= &sc
->sc_ic
;
902 ieee80211_suspend_all(ic
);
907 iwi_resume(device_t dev
)
909 struct iwi_softc
*sc
= device_get_softc(dev
);
910 struct ieee80211com
*ic
= &sc
->sc_ic
;
912 pci_write_config(dev
, 0x41, 0, 1);
914 ieee80211_resume_all(ic
);
918 static struct ieee80211_node
*
919 iwi_node_alloc(struct ieee80211vap
*vap
, const uint8_t mac
[IEEE80211_ADDR_LEN
])
923 in
= kmalloc(sizeof (struct iwi_node
), M_80211_NODE
, M_INTWAIT
| M_ZERO
);
926 /* XXX assign sta table entry for adhoc */
933 iwi_node_free(struct ieee80211_node
*ni
)
935 struct ieee80211com
*ic
= ni
->ni_ic
;
936 struct iwi_softc
*sc
= ic
->ic_softc
;
937 struct iwi_node
*in
= (struct iwi_node
*)ni
;
939 if (in
->in_station
!= -1) {
940 #if defined(__DragonFly__)
941 DPRINTF(("%s mac %s station %u\n", __func__
,
942 ether_sprintf(ni
->ni_macaddr
), in
->in_station
));
944 DPRINTF(("%s mac %6D station %u\n", __func__
,
945 ni
->ni_macaddr
, ":", in
->in_station
));
947 #if defined(__DragonFly__)
948 devfs_clone_bitmap_put(&sc
->sc_unr
, in
->in_station
);
950 free_unr(sc
->sc_unr
, in
->in_station
);
954 sc
->sc_node_free(ni
);
958 * Convert h/w rate code to IEEE rate code.
961 iwi_cvtrate(int iwirate
)
964 case IWI_RATE_DS1
: return 2;
965 case IWI_RATE_DS2
: return 4;
966 case IWI_RATE_DS5
: return 11;
967 case IWI_RATE_DS11
: return 22;
968 case IWI_RATE_OFDM6
: return 12;
969 case IWI_RATE_OFDM9
: return 18;
970 case IWI_RATE_OFDM12
: return 24;
971 case IWI_RATE_OFDM18
: return 36;
972 case IWI_RATE_OFDM24
: return 48;
973 case IWI_RATE_OFDM36
: return 72;
974 case IWI_RATE_OFDM48
: return 96;
975 case IWI_RATE_OFDM54
: return 108;
981 * The firmware automatically adapts the transmit speed. We report its current
985 iwi_media_status(struct ifnet
*ifp
, struct ifmediareq
*imr
)
987 struct ieee80211vap
*vap
= ifp
->if_softc
;
988 struct ieee80211com
*ic
= vap
->iv_ic
;
989 struct iwi_softc
*sc
= ic
->ic_softc
;
990 struct ieee80211_node
*ni
;
992 /* read current transmission rate from adapter */
993 ni
= ieee80211_ref_node(vap
->iv_bss
);
995 iwi_cvtrate(CSR_READ_4(sc
, IWI_CSR_CURRENT_TX_RATE
));
996 ieee80211_free_node(ni
);
997 ieee80211_media_status(ifp
, imr
);
1001 iwi_newstate(struct ieee80211vap
*vap
, enum ieee80211_state nstate
, int arg
)
1003 struct iwi_vap
*ivp
= IWI_VAP(vap
);
1004 struct ieee80211com
*ic
= vap
->iv_ic
;
1005 struct iwi_softc
*sc
= ic
->ic_softc
;
1008 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__
,
1009 ieee80211_state_name
[vap
->iv_state
],
1010 ieee80211_state_name
[nstate
], sc
->flags
));
1012 IEEE80211_UNLOCK(ic
);
1015 case IEEE80211_S_INIT
:
1017 * NB: don't try to do this if iwi_stop_master has
1018 * shutdown the firmware and disabled interrupts.
1020 if (vap
->iv_state
== IEEE80211_S_RUN
&&
1021 (sc
->flags
& IWI_FLAG_FW_INITED
))
1022 iwi_disassociate(sc
, 0);
1024 case IEEE80211_S_AUTH
:
1025 iwi_auth_and_assoc(sc
, vap
);
1027 case IEEE80211_S_RUN
:
1028 if (vap
->iv_opmode
== IEEE80211_M_IBSS
&&
1029 vap
->iv_state
== IEEE80211_S_SCAN
) {
1031 * XXX when joining an ibss network we are called
1032 * with a SCAN -> RUN transition on scan complete.
1033 * Use that to call iwi_auth_and_assoc. On completing
1034 * the join we are then called again with an
1035 * AUTH -> RUN transition and we want to do nothing.
1036 * This is all totally bogus and needs to be redone.
1038 iwi_auth_and_assoc(sc
, vap
);
1039 } else if (vap
->iv_opmode
== IEEE80211_M_MONITOR
)
1040 ieee80211_runtask(ic
, &sc
->sc_monitortask
);
1042 case IEEE80211_S_ASSOC
:
1044 * If we are transitioning from AUTH then just wait
1045 * for the ASSOC status to come back from the firmware.
1046 * Otherwise we need to issue the association request.
1048 if (vap
->iv_state
== IEEE80211_S_AUTH
)
1050 iwi_auth_and_assoc(sc
, vap
);
1057 return ivp
->iwi_newstate(vap
, nstate
, arg
);
1061 * WME parameters coming from IEEE 802.11e specification. These values are
1062 * already declared in ieee80211_proto.c, but they are static so they can't
1065 static const struct wmeParams iwi_wme_cck_params
[WME_NUM_AC
] = {
1066 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */
1067 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */
1068 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */
1069 { 0, 2, 3, 4, 102 } /* WME_AC_VO */
1072 static const struct wmeParams iwi_wme_ofdm_params
[WME_NUM_AC
] = {
1073 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */
1074 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */
1075 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */
1076 { 0, 2, 2, 3, 47 } /* WME_AC_VO */
1078 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
1079 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1082 iwi_wme_init(struct iwi_softc
*sc
)
1084 const struct wmeParams
*wmep
;
1087 memset(sc
->wme
, 0, sizeof sc
->wme
);
1088 for (ac
= 0; ac
< WME_NUM_AC
; ac
++) {
1089 /* set WME values for CCK modulation */
1090 wmep
= &iwi_wme_cck_params
[ac
];
1091 sc
->wme
[1].aifsn
[ac
] = wmep
->wmep_aifsn
;
1092 sc
->wme
[1].cwmin
[ac
] = IWI_EXP2(wmep
->wmep_logcwmin
);
1093 sc
->wme
[1].cwmax
[ac
] = IWI_EXP2(wmep
->wmep_logcwmax
);
1094 sc
->wme
[1].burst
[ac
] = IWI_USEC(wmep
->wmep_txopLimit
);
1095 sc
->wme
[1].acm
[ac
] = wmep
->wmep_acm
;
1097 /* set WME values for OFDM modulation */
1098 wmep
= &iwi_wme_ofdm_params
[ac
];
1099 sc
->wme
[2].aifsn
[ac
] = wmep
->wmep_aifsn
;
1100 sc
->wme
[2].cwmin
[ac
] = IWI_EXP2(wmep
->wmep_logcwmin
);
1101 sc
->wme
[2].cwmax
[ac
] = IWI_EXP2(wmep
->wmep_logcwmax
);
1102 sc
->wme
[2].burst
[ac
] = IWI_USEC(wmep
->wmep_txopLimit
);
1103 sc
->wme
[2].acm
[ac
] = wmep
->wmep_acm
;
1108 iwi_wme_setparams(struct iwi_softc
*sc
)
1110 struct ieee80211com
*ic
= &sc
->sc_ic
;
1111 const struct wmeParams
*wmep
;
1114 for (ac
= 0; ac
< WME_NUM_AC
; ac
++) {
1115 /* set WME values for current operating mode */
1116 wmep
= &ic
->ic_wme
.wme_chanParams
.cap_wmeParams
[ac
];
1117 sc
->wme
[0].aifsn
[ac
] = wmep
->wmep_aifsn
;
1118 sc
->wme
[0].cwmin
[ac
] = IWI_EXP2(wmep
->wmep_logcwmin
);
1119 sc
->wme
[0].cwmax
[ac
] = IWI_EXP2(wmep
->wmep_logcwmax
);
1120 sc
->wme
[0].burst
[ac
] = IWI_USEC(wmep
->wmep_txopLimit
);
1121 sc
->wme
[0].acm
[ac
] = wmep
->wmep_acm
;
1124 DPRINTF(("Setting WME parameters\n"));
1125 return iwi_cmd(sc
, IWI_CMD_SET_WME_PARAMS
, sc
->wme
, sizeof sc
->wme
);
1131 iwi_wme_update(struct ieee80211com
*ic
)
1133 struct iwi_softc
*sc
= ic
->ic_softc
;
1134 struct ieee80211vap
*vap
= TAILQ_FIRST(&ic
->ic_vaps
);
1138 * We may be called to update the WME parameters in
1139 * the adapter at various places. If we're already
1140 * associated then initiate the request immediately;
1141 * otherwise we assume the params will get sent down
1142 * to the adapter as part of the work iwi_auth_and_assoc
1145 if (vap
->iv_state
== IEEE80211_S_RUN
) {
1147 iwi_wme_setparams(sc
);
1154 iwi_wme_setie(struct iwi_softc
*sc
)
1156 struct ieee80211_wme_info wme
;
1158 memset(&wme
, 0, sizeof wme
);
1159 wme
.wme_id
= IEEE80211_ELEMID_VENDOR
;
1160 wme
.wme_len
= sizeof (struct ieee80211_wme_info
) - 2;
1161 wme
.wme_oui
[0] = 0x00;
1162 wme
.wme_oui
[1] = 0x50;
1163 wme
.wme_oui
[2] = 0xf2;
1164 wme
.wme_type
= WME_OUI_TYPE
;
1165 wme
.wme_subtype
= WME_INFO_OUI_SUBTYPE
;
1166 wme
.wme_version
= WME_VERSION
;
1169 DPRINTF(("Setting WME IE (len=%u)\n", wme
.wme_len
));
1170 return iwi_cmd(sc
, IWI_CMD_SET_WMEIE
, &wme
, sizeof wme
);
1174 * Read 16 bits at address 'addr' from the serial EEPROM.
1177 iwi_read_prom_word(struct iwi_softc
*sc
, uint8_t addr
)
1183 /* clock C once before the first command */
1184 IWI_EEPROM_CTL(sc
, 0);
1185 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1186 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_C
);
1187 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1189 /* write start bit (1) */
1190 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_D
);
1191 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_D
| IWI_EEPROM_C
);
1193 /* write READ opcode (10) */
1194 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_D
);
1195 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_D
| IWI_EEPROM_C
);
1196 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1197 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_C
);
1199 /* write address A7-A0 */
1200 for (n
= 7; n
>= 0; n
--) {
1201 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
|
1202 (((addr
>> n
) & 1) << IWI_EEPROM_SHIFT_D
));
1203 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
|
1204 (((addr
>> n
) & 1) << IWI_EEPROM_SHIFT_D
) | IWI_EEPROM_C
);
1207 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1209 /* read data Q15-Q0 */
1211 for (n
= 15; n
>= 0; n
--) {
1212 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_C
);
1213 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1214 tmp
= MEM_READ_4(sc
, IWI_MEM_EEPROM_CTL
);
1215 val
|= ((tmp
& IWI_EEPROM_Q
) >> IWI_EEPROM_SHIFT_Q
) << n
;
1218 IWI_EEPROM_CTL(sc
, 0);
1220 /* clear Chip Select and clock C */
1221 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1222 IWI_EEPROM_CTL(sc
, 0);
1223 IWI_EEPROM_CTL(sc
, IWI_EEPROM_C
);
1229 iwi_setcurchan(struct iwi_softc
*sc
, int chan
)
1231 struct ieee80211com
*ic
= &sc
->sc_ic
;
1234 ieee80211_radiotap_chan_change(ic
);
1238 iwi_frame_intr(struct iwi_softc
*sc
, struct iwi_rx_data
*data
, int i
,
1239 struct iwi_frame
*frame
)
1241 struct ieee80211com
*ic
= &sc
->sc_ic
;
1242 struct mbuf
*mnew
, *m
;
1243 struct ieee80211_node
*ni
;
1244 int type
, error
, framelen
;
1248 framelen
= le16toh(frame
->len
);
1249 if (framelen
< IEEE80211_MIN_LEN
|| framelen
> MCLBYTES
) {
1251 * XXX >MCLBYTES is bogus as it means the h/w dma'd
1252 * out of bounds; need to figure out how to limit
1253 * frame size in the firmware
1257 ("drop rx frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1258 le16toh(frame
->len
), frame
->chan
, frame
->rssi
,
1263 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u rssi_dbm=%u\n",
1264 le16toh(frame
->len
), frame
->chan
, frame
->rssi
, frame
->rssi_dbm
));
1266 if (frame
->chan
!= sc
->curchan
)
1267 iwi_setcurchan(sc
, frame
->chan
);
1270 * Try to allocate a new mbuf for this ring element and load it before
1271 * processing the current mbuf. If the ring element cannot be loaded,
1272 * drop the received packet and reuse the old mbuf. In the unlikely
1273 * case that the old mbuf can't be reloaded either, explicitly panic.
1275 mnew
= m_getcl(M_NOWAIT
, MT_DATA
, M_PKTHDR
);
1277 #if defined(__DragonFly__)
1280 counter_u64_add(ic
->ic_ierrors
, 1);
1285 bus_dmamap_unload(sc
->rxq
.data_dmat
, data
->map
);
1287 error
= bus_dmamap_load(sc
->rxq
.data_dmat
, data
->map
,
1288 mtod(mnew
, void *), MCLBYTES
, iwi_dma_map_addr
, &data
->physaddr
,
1293 /* try to reload the old mbuf */
1294 error
= bus_dmamap_load(sc
->rxq
.data_dmat
, data
->map
,
1295 mtod(data
->m
, void *), MCLBYTES
, iwi_dma_map_addr
,
1296 &data
->physaddr
, 0);
1298 /* very unlikely that it will fail... */
1299 panic("%s: could not load old rx mbuf",
1300 device_get_name(sc
->sc_dev
));
1302 #if defined(__DragonFly__)
1305 counter_u64_add(ic
->ic_ierrors
, 1);
1311 * New mbuf successfully loaded, update Rx ring and continue
1316 CSR_WRITE_4(sc
, data
->reg
, data
->physaddr
);
1319 m
->m_pkthdr
.len
= m
->m_len
= sizeof (struct iwi_hdr
) +
1320 sizeof (struct iwi_frame
) + framelen
;
1322 m_adj(m
, sizeof (struct iwi_hdr
) + sizeof (struct iwi_frame
));
1324 rssi
= frame
->rssi_dbm
;
1326 if (ieee80211_radiotap_active(ic
)) {
1327 struct iwi_rx_radiotap_header
*tap
= &sc
->sc_rxtap
;
1330 tap
->wr_antsignal
= rssi
;
1331 tap
->wr_antnoise
= nf
;
1332 tap
->wr_rate
= iwi_cvtrate(frame
->rate
);
1333 tap
->wr_antenna
= frame
->antenna
;
1337 ni
= ieee80211_find_rxnode(ic
, mtod(m
, struct ieee80211_frame_min
*));
1339 type
= ieee80211_input(ni
, m
, rssi
, nf
);
1340 ieee80211_free_node(ni
);
1342 type
= ieee80211_input_all(ic
, m
, rssi
, nf
);
1345 if (sc
->sc_softled
) {
1347 * Blink for any data frame. Otherwise do a
1348 * heartbeat-style blink when idle. The latter
1349 * is mainly for station mode where we depend on
1350 * periodic beacon frames to trigger the poll event.
1352 if (type
== IEEE80211_FC0_TYPE_DATA
) {
1353 sc
->sc_rxrate
= frame
->rate
;
1354 iwi_led_event(sc
, IWI_LED_RX
);
1355 } else if (ticks
- sc
->sc_ledevent
>= sc
->sc_ledidle
)
1356 iwi_led_event(sc
, IWI_LED_POLL
);
1361 * Check for an association response frame to see if QoS
1362 * has been negotiated. We parse just enough to figure
1363 * out if we're supposed to use QoS. The proper solution
1364 * is to pass the frame up so ieee80211_input can do the
1365 * work but that's made hard by how things currently are
1366 * done in the driver.
1369 iwi_checkforqos(struct ieee80211vap
*vap
,
1370 const struct ieee80211_frame
*wh
, int len
)
1372 #define SUBTYPE(wh) ((wh)->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
1373 const uint8_t *frm
, *efrm
, *wme
;
1374 struct ieee80211_node
*ni
;
1375 uint16_t capinfo
, status
, associd
;
1377 /* NB: +8 for capinfo, status, associd, and first ie */
1378 if (!(sizeof(*wh
)+8 < len
&& len
< IEEE80211_MAX_LEN
) ||
1379 SUBTYPE(wh
) != IEEE80211_FC0_SUBTYPE_ASSOC_RESP
)
1382 * asresp frame format
1383 * [2] capability information
1385 * [2] association ID
1386 * [tlv] supported rates
1387 * [tlv] extended supported rates
1390 frm
= (const uint8_t *)&wh
[1];
1391 efrm
= ((const uint8_t *) wh
) + len
;
1393 capinfo
= le16toh(*(const uint16_t *)frm
);
1395 status
= le16toh(*(const uint16_t *)frm
);
1397 associd
= le16toh(*(const uint16_t *)frm
);
1401 while (efrm
- frm
> 1) {
1402 IEEE80211_VERIFY_LENGTH(efrm
- frm
, frm
[1] + 2, return);
1404 case IEEE80211_ELEMID_VENDOR
:
1412 ni
= ieee80211_ref_node(vap
->iv_bss
);
1413 ni
->ni_capinfo
= capinfo
;
1414 ni
->ni_associd
= associd
& 0x3fff;
1416 ni
->ni_flags
|= IEEE80211_NODE_QOS
;
1418 ni
->ni_flags
&= ~IEEE80211_NODE_QOS
;
1419 ieee80211_free_node(ni
);
1424 iwi_notif_link_quality(struct iwi_softc
*sc
, struct iwi_notif
*notif
)
1426 struct iwi_notif_link_quality
*lq
;
1429 len
= le16toh(notif
->len
);
1431 DPRINTFN(5, ("Notification (%u) - len=%d, sizeof=%zu\n",
1434 sizeof(struct iwi_notif_link_quality
)
1437 /* enforce length */
1438 if (len
!= sizeof(struct iwi_notif_link_quality
)) {
1439 DPRINTFN(5, ("Notification: (%u) too short (%d)\n",
1445 lq
= (struct iwi_notif_link_quality
*)(notif
+ 1);
1446 memcpy(&sc
->sc_linkqual
, lq
, sizeof(sc
->sc_linkqual
));
1447 sc
->sc_linkqual_valid
= 1;
1451 * Task queue callbacks for iwi_notification_intr used to avoid LOR's.
1455 iwi_notification_intr(struct iwi_softc
*sc
, struct iwi_notif
*notif
)
1457 struct ieee80211com
*ic
= &sc
->sc_ic
;
1458 struct ieee80211vap
*vap
= TAILQ_FIRST(&ic
->ic_vaps
);
1459 struct iwi_notif_scan_channel
*chan
;
1460 struct iwi_notif_scan_complete
*scan
;
1461 struct iwi_notif_authentication
*auth
;
1462 struct iwi_notif_association
*assoc
;
1463 struct iwi_notif_beacon_state
*beacon
;
1465 switch (notif
->type
) {
1466 case IWI_NOTIF_TYPE_SCAN_CHANNEL
:
1467 chan
= (struct iwi_notif_scan_channel
*)(notif
+ 1);
1469 DPRINTFN(3, ("Scan of channel %u complete (%u)\n",
1470 ieee80211_ieee2mhz(chan
->nchan
, 0), chan
->nchan
));
1472 /* Reset the timer, the scan is still going */
1473 sc
->sc_state_timer
= 3;
1476 case IWI_NOTIF_TYPE_SCAN_COMPLETE
:
1477 scan
= (struct iwi_notif_scan_complete
*)(notif
+ 1);
1479 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan
->nchan
,
1482 IWI_STATE_END(sc
, IWI_FW_SCANNING
);
1485 * Monitor mode works by doing a passive scan to set
1486 * the channel and enable rx. Because we don't want
1487 * to abort a scan lest the firmware crash we scan
1488 * for a short period of time and automatically restart
1489 * the scan when notified the sweep has completed.
1491 if (vap
->iv_opmode
== IEEE80211_M_MONITOR
) {
1492 ieee80211_runtask(ic
, &sc
->sc_monitortask
);
1496 if (scan
->status
== IWI_SCAN_COMPLETED
) {
1497 /* NB: don't need to defer, net80211 does it for us */
1498 ieee80211_scan_next(vap
);
1502 case IWI_NOTIF_TYPE_AUTHENTICATION
:
1503 auth
= (struct iwi_notif_authentication
*)(notif
+ 1);
1504 switch (auth
->state
) {
1505 case IWI_AUTH_SUCCESS
:
1506 DPRINTFN(2, ("Authentication succeeeded\n"));
1507 ieee80211_new_state(vap
, IEEE80211_S_ASSOC
, -1);
1511 * These are delivered as an unsolicited deauth
1512 * (e.g. due to inactivity) or in response to an
1513 * associate request.
1515 sc
->flags
&= ~IWI_FLAG_ASSOCIATED
;
1516 if (vap
->iv_state
!= IEEE80211_S_RUN
) {
1517 DPRINTFN(2, ("Authentication failed\n"));
1518 vap
->iv_stats
.is_rx_auth_fail
++;
1519 IWI_STATE_END(sc
, IWI_FW_ASSOCIATING
);
1521 DPRINTFN(2, ("Deauthenticated\n"));
1522 vap
->iv_stats
.is_rx_deauth
++;
1524 ieee80211_new_state(vap
, IEEE80211_S_SCAN
, -1);
1526 case IWI_AUTH_SENT_1
:
1527 case IWI_AUTH_RECV_2
:
1528 case IWI_AUTH_SEQ1_PASS
:
1530 case IWI_AUTH_SEQ1_FAIL
:
1531 DPRINTFN(2, ("Initial authentication handshake failed; "
1532 "you probably need shared key\n"));
1533 vap
->iv_stats
.is_rx_auth_fail
++;
1534 IWI_STATE_END(sc
, IWI_FW_ASSOCIATING
);
1535 /* XXX retry shared key when in auto */
1538 device_printf(sc
->sc_dev
,
1539 "unknown authentication state %u\n", auth
->state
);
1544 case IWI_NOTIF_TYPE_ASSOCIATION
:
1545 assoc
= (struct iwi_notif_association
*)(notif
+ 1);
1546 switch (assoc
->state
) {
1547 case IWI_AUTH_SUCCESS
:
1548 /* re-association, do nothing */
1550 case IWI_ASSOC_SUCCESS
:
1551 DPRINTFN(2, ("Association succeeded\n"));
1552 sc
->flags
|= IWI_FLAG_ASSOCIATED
;
1553 IWI_STATE_END(sc
, IWI_FW_ASSOCIATING
);
1554 iwi_checkforqos(vap
,
1555 (const struct ieee80211_frame
*)(assoc
+1),
1556 le16toh(notif
->len
) - sizeof(*assoc
) - 1);
1557 ieee80211_new_state(vap
, IEEE80211_S_RUN
, -1);
1559 case IWI_ASSOC_INIT
:
1560 sc
->flags
&= ~IWI_FLAG_ASSOCIATED
;
1561 switch (sc
->fw_state
) {
1562 case IWI_FW_ASSOCIATING
:
1563 DPRINTFN(2, ("Association failed\n"));
1564 IWI_STATE_END(sc
, IWI_FW_ASSOCIATING
);
1565 ieee80211_new_state(vap
, IEEE80211_S_SCAN
, -1);
1568 case IWI_FW_DISASSOCIATING
:
1569 DPRINTFN(2, ("Dissassociated\n"));
1570 IWI_STATE_END(sc
, IWI_FW_DISASSOCIATING
);
1571 vap
->iv_stats
.is_rx_disassoc
++;
1572 ieee80211_new_state(vap
, IEEE80211_S_SCAN
, -1);
1577 device_printf(sc
->sc_dev
,
1578 "unknown association state %u\n", assoc
->state
);
1583 case IWI_NOTIF_TYPE_BEACON
:
1584 /* XXX check struct length */
1585 beacon
= (struct iwi_notif_beacon_state
*)(notif
+ 1);
1587 DPRINTFN(5, ("Beacon state (%u, %u)\n",
1588 beacon
->state
, le32toh(beacon
->number
)));
1590 if (beacon
->state
== IWI_BEACON_MISS
) {
1592 * The firmware notifies us of every beacon miss
1593 * so we need to track the count against the
1594 * configured threshold before notifying the
1596 * XXX try to roam, drop assoc only on much higher count
1598 if (le32toh(beacon
->number
) >= vap
->iv_bmissthreshold
) {
1599 DPRINTF(("Beacon miss: %u >= %u\n",
1600 le32toh(beacon
->number
),
1601 vap
->iv_bmissthreshold
));
1602 vap
->iv_stats
.is_beacon_miss
++;
1604 * It's pointless to notify the 802.11 layer
1605 * as it'll try to send a probe request (which
1606 * we'll discard) and then timeout and drop us
1607 * into scan state. Instead tell the firmware
1608 * to disassociate and then on completion we'll
1609 * kick the state machine to scan.
1611 ieee80211_runtask(ic
, &sc
->sc_disassoctask
);
1616 case IWI_NOTIF_TYPE_CALIBRATION
:
1617 case IWI_NOTIF_TYPE_NOISE
:
1619 DPRINTFN(5, ("Notification (%u)\n", notif
->type
));
1621 case IWI_NOTIF_TYPE_LINK_QUALITY
:
1622 iwi_notif_link_quality(sc
, notif
);
1626 DPRINTF(("unknown notification type %u flags 0x%x len %u\n",
1627 notif
->type
, notif
->flags
, le16toh(notif
->len
)));
1633 iwi_rx_intr(struct iwi_softc
*sc
)
1635 struct iwi_rx_data
*data
;
1636 struct iwi_hdr
*hdr
;
1639 hw
= CSR_READ_4(sc
, IWI_CSR_RX_RIDX
);
1641 for (; sc
->rxq
.cur
!= hw
;) {
1642 data
= &sc
->rxq
.data
[sc
->rxq
.cur
];
1644 bus_dmamap_sync(sc
->rxq
.data_dmat
, data
->map
,
1645 BUS_DMASYNC_POSTREAD
);
1647 hdr
= mtod(data
->m
, struct iwi_hdr
*);
1649 switch (hdr
->type
) {
1650 case IWI_HDR_TYPE_FRAME
:
1651 iwi_frame_intr(sc
, data
, sc
->rxq
.cur
,
1652 (struct iwi_frame
*)(hdr
+ 1));
1655 case IWI_HDR_TYPE_NOTIF
:
1656 iwi_notification_intr(sc
,
1657 (struct iwi_notif
*)(hdr
+ 1));
1661 device_printf(sc
->sc_dev
, "unknown hdr type %u\n",
1665 DPRINTFN(15, ("rx done idx=%u\n", sc
->rxq
.cur
));
1667 sc
->rxq
.cur
= (sc
->rxq
.cur
+ 1) % IWI_RX_RING_COUNT
;
1670 /* tell the firmware what we have processed */
1671 hw
= (hw
== 0) ? IWI_RX_RING_COUNT
- 1 : hw
- 1;
1672 CSR_WRITE_4(sc
, IWI_CSR_RX_WIDX
, hw
);
1676 iwi_tx_intr(struct iwi_softc
*sc
, struct iwi_tx_ring
*txq
)
1678 struct iwi_tx_data
*data
;
1681 hw
= CSR_READ_4(sc
, txq
->csr_ridx
);
1683 while (txq
->next
!= hw
) {
1684 data
= &txq
->data
[txq
->next
];
1685 DPRINTFN(15, ("tx done idx=%u\n", txq
->next
));
1686 bus_dmamap_sync(txq
->data_dmat
, data
->map
,
1687 BUS_DMASYNC_POSTWRITE
);
1688 bus_dmamap_unload(txq
->data_dmat
, data
->map
);
1689 ieee80211_tx_complete(data
->ni
, data
->m
, 0);
1693 txq
->next
= (txq
->next
+ 1) % IWI_TX_RING_COUNT
;
1695 sc
->sc_tx_timer
= 0;
1697 iwi_led_event(sc
, IWI_LED_TX
);
1702 iwi_fatal_error_intr(struct iwi_softc
*sc
)
1704 struct ieee80211com
*ic
= &sc
->sc_ic
;
1705 struct ieee80211vap
*vap
= TAILQ_FIRST(&ic
->ic_vaps
);
1707 device_printf(sc
->sc_dev
, "firmware error\n");
1709 ieee80211_cancel_scan(vap
);
1710 ieee80211_runtask(ic
, &sc
->sc_restarttask
);
1712 sc
->flags
&= ~IWI_FLAG_BUSY
;
1713 sc
->sc_busy_timer
= 0;
1718 iwi_radio_off_intr(struct iwi_softc
*sc
)
1721 ieee80211_runtask(&sc
->sc_ic
, &sc
->sc_radiofftask
);
1727 struct iwi_softc
*sc
= arg
;
1733 if ((r
= CSR_READ_4(sc
, IWI_CSR_INTR
)) == 0 || r
== 0xffffffff) {
1738 /* acknowledge interrupts */
1739 CSR_WRITE_4(sc
, IWI_CSR_INTR
, r
);
1741 if (r
& IWI_INTR_FATAL_ERROR
) {
1742 iwi_fatal_error_intr(sc
);
1746 if (r
& IWI_INTR_FW_INITED
) {
1747 if (!(r
& (IWI_INTR_FATAL_ERROR
| IWI_INTR_PARITY_ERROR
)))
1751 if (r
& IWI_INTR_RADIO_OFF
)
1752 iwi_radio_off_intr(sc
);
1754 if (r
& IWI_INTR_CMD_DONE
) {
1755 sc
->flags
&= ~IWI_FLAG_BUSY
;
1756 sc
->sc_busy_timer
= 0;
1760 if (r
& IWI_INTR_TX1_DONE
)
1761 iwi_tx_intr(sc
, &sc
->txq
[0]);
1763 if (r
& IWI_INTR_TX2_DONE
)
1764 iwi_tx_intr(sc
, &sc
->txq
[1]);
1766 if (r
& IWI_INTR_TX3_DONE
)
1767 iwi_tx_intr(sc
, &sc
->txq
[2]);
1769 if (r
& IWI_INTR_TX4_DONE
)
1770 iwi_tx_intr(sc
, &sc
->txq
[3]);
1772 if (r
& IWI_INTR_RX_DONE
)
1775 if (r
& IWI_INTR_PARITY_ERROR
) {
1776 /* XXX rate-limit */
1777 device_printf(sc
->sc_dev
, "parity error\n");
1784 iwi_cmd(struct iwi_softc
*sc
, uint8_t type
, void *data
, uint8_t len
)
1786 struct iwi_cmd_desc
*desc
;
1788 IWI_LOCK_ASSERT(sc
);
1790 if (sc
->flags
& IWI_FLAG_BUSY
) {
1791 device_printf(sc
->sc_dev
, "%s: cmd %d not sent, busy\n",
1795 sc
->flags
|= IWI_FLAG_BUSY
;
1796 sc
->sc_busy_timer
= 2;
1798 desc
= &sc
->cmdq
.desc
[sc
->cmdq
.cur
];
1800 desc
->hdr
.type
= IWI_HDR_TYPE_COMMAND
;
1801 desc
->hdr
.flags
= IWI_HDR_FLAG_IRQ
;
1804 memcpy(desc
->data
, data
, len
);
1806 bus_dmamap_sync(sc
->cmdq
.desc_dmat
, sc
->cmdq
.desc_map
,
1807 BUS_DMASYNC_PREWRITE
);
1809 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc
->cmdq
.cur
,
1812 sc
->cmdq
.cur
= (sc
->cmdq
.cur
+ 1) % IWI_CMD_RING_COUNT
;
1813 CSR_WRITE_4(sc
, IWI_CSR_CMD_WIDX
, sc
->cmdq
.cur
);
1815 #if defined(__DragonFly__)
1816 return lksleep(sc
, &sc
->sc_lock
, 0, "iwicmd", hz
);
1818 return msleep(sc
, &sc
->sc_mtx
, 0, "iwicmd", hz
);
1823 iwi_write_ibssnode(struct iwi_softc
*sc
,
1824 const u_int8_t addr
[IEEE80211_ADDR_LEN
], int entry
)
1826 struct iwi_ibssnode node
;
1828 /* write node information into NIC memory */
1829 memset(&node
, 0, sizeof node
);
1830 IEEE80211_ADDR_COPY(node
.bssid
, addr
);
1831 #if defined(__DragonFly__)
1832 DPRINTF(("%s mac %s station %u\n", __func__
, ether_sprintf(node
.bssid
),
1835 DPRINTF(("%s mac %6D station %u\n", __func__
, node
.bssid
, ":", entry
));
1838 CSR_WRITE_REGION_1(sc
,
1839 IWI_CSR_NODE_BASE
+ entry
* sizeof node
,
1840 (uint8_t *)&node
, sizeof node
);
1844 iwi_tx_start(struct iwi_softc
*sc
, struct mbuf
*m0
, struct ieee80211_node
*ni
,
1847 struct ieee80211vap
*vap
= ni
->ni_vap
;
1848 struct ieee80211com
*ic
= ni
->ni_ic
;
1849 struct iwi_node
*in
= (struct iwi_node
*)ni
;
1850 const struct ieee80211_frame
*wh
;
1851 struct ieee80211_key
*k
;
1852 const struct chanAccParams
*cap
;
1853 struct iwi_tx_ring
*txq
= &sc
->txq
[ac
];
1854 struct iwi_tx_data
*data
;
1855 struct iwi_tx_desc
*desc
;
1857 bus_dma_segment_t segs
[IWI_MAX_NSEG
];
1858 int error
, nsegs
, hdrlen
, i
;
1859 int ismcast
, flags
, xflags
, staid
;
1861 IWI_LOCK_ASSERT(sc
);
1862 wh
= mtod(m0
, const struct ieee80211_frame
*);
1863 /* NB: only data frames use this path */
1864 hdrlen
= ieee80211_hdrsize(wh
);
1865 ismcast
= IEEE80211_IS_MULTICAST(wh
->i_addr1
);
1869 flags
|= IWI_DATA_FLAG_NEED_ACK
;
1870 if (vap
->iv_flags
& IEEE80211_F_SHPREAMBLE
)
1871 flags
|= IWI_DATA_FLAG_SHPREAMBLE
;
1872 if (IEEE80211_QOS_HAS_SEQ(wh
)) {
1873 xflags
|= IWI_DATA_XFLAG_QOS
;
1874 cap
= &ic
->ic_wme
.wme_chanParams
;
1875 if (!cap
->cap_wmeParams
[ac
].wmep_noackPolicy
)
1876 flags
&= ~IWI_DATA_FLAG_NEED_ACK
;
1880 * This is only used in IBSS mode where the firmware expect an index
1881 * in a h/w table instead of a destination address.
1883 if (vap
->iv_opmode
== IEEE80211_M_IBSS
) {
1885 if (in
->in_station
== -1) {
1886 #if defined(__DragonFly__)
1887 in
->in_station
= devfs_clone_bitmap_get(&sc
->sc_unr
,
1888 IWI_MAX_IBSSNODE
-1);
1890 in
->in_station
= alloc_unr(sc
->sc_unr
);
1892 if (in
->in_station
== -1) {
1893 /* h/w table is full */
1894 if_inc_counter(ni
->ni_vap
->iv_ifp
,
1895 IFCOUNTER_OERRORS
, 1);
1897 ieee80211_free_node(ni
);
1900 iwi_write_ibssnode(sc
,
1901 ni
->ni_macaddr
, in
->in_station
);
1903 staid
= in
->in_station
;
1906 * Multicast addresses have no associated node
1907 * so there will be no station entry. We reserve
1908 * entry 0 for one mcast address and use that.
1909 * If there are many being used this will be
1910 * expensive and we'll need to do a better job
1911 * but for now this handles the broadcast case.
1913 if (!IEEE80211_ADDR_EQ(wh
->i_addr1
, sc
->sc_mcast
)) {
1914 IEEE80211_ADDR_COPY(sc
->sc_mcast
, wh
->i_addr1
);
1915 iwi_write_ibssnode(sc
, sc
->sc_mcast
, 0);
1922 if (wh
->i_fc
[1] & IEEE80211_FC1_PROTECTED
) {
1923 k
= ieee80211_crypto_encap(ni
, m0
);
1929 /* packet header may have moved, reset our local pointer */
1930 wh
= mtod(m0
, struct ieee80211_frame
*);
1933 if (ieee80211_radiotap_active_vap(vap
)) {
1934 struct iwi_tx_radiotap_header
*tap
= &sc
->sc_txtap
;
1938 ieee80211_radiotap_tx(vap
, m0
);
1941 data
= &txq
->data
[txq
->cur
];
1942 desc
= &txq
->desc
[txq
->cur
];
1944 /* save and trim IEEE802.11 header */
1945 m_copydata(m0
, 0, hdrlen
, (caddr_t
)&desc
->wh
);
1948 #if defined(__DragonFly__)
1949 error
= bus_dmamap_load_mbuf_segment(txq
->data_dmat
, data
->map
,
1950 m0
, segs
, 1, &nsegs
, BUS_DMA_NOWAIT
);
1952 error
= bus_dmamap_load_mbuf_sg(txq
->data_dmat
, data
->map
, m0
, segs
,
1955 if (error
!= 0 && error
!= EFBIG
) {
1956 device_printf(sc
->sc_dev
, "could not map mbuf (error %d)\n",
1962 mnew
= m_defrag(m0
, M_NOWAIT
);
1964 device_printf(sc
->sc_dev
,
1965 "could not defragment mbuf\n");
1971 #if defined(__DragonFly__)
1972 error
= bus_dmamap_load_mbuf_segment(txq
->data_dmat
,
1973 data
->map
, m0
, segs
, 1, &nsegs
, BUS_DMA_NOWAIT
);
1975 error
= bus_dmamap_load_mbuf_sg(txq
->data_dmat
, data
->map
,
1976 m0
, segs
, &nsegs
, 0);
1979 device_printf(sc
->sc_dev
,
1980 "could not map mbuf (error %d)\n", error
);
1989 desc
->hdr
.type
= IWI_HDR_TYPE_DATA
;
1990 desc
->hdr
.flags
= IWI_HDR_FLAG_IRQ
;
1991 desc
->station
= staid
;
1992 desc
->cmd
= IWI_DATA_CMD_TX
;
1993 desc
->len
= htole16(m0
->m_pkthdr
.len
);
1994 desc
->flags
= flags
;
1995 desc
->xflags
= xflags
;
1998 if (vap
->iv_flags
& IEEE80211_F_PRIVACY
)
1999 desc
->wep_txkey
= vap
->iv_def_txkey
;
2002 desc
->flags
|= IWI_DATA_FLAG_NO_WEP
;
2004 desc
->nseg
= htole32(nsegs
);
2005 for (i
= 0; i
< nsegs
; i
++) {
2006 desc
->seg_addr
[i
] = htole32(segs
[i
].ds_addr
);
2007 desc
->seg_len
[i
] = htole16(segs
[i
].ds_len
);
2010 bus_dmamap_sync(txq
->data_dmat
, data
->map
, BUS_DMASYNC_PREWRITE
);
2011 bus_dmamap_sync(txq
->desc_dmat
, txq
->desc_map
, BUS_DMASYNC_PREWRITE
);
2013 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
2014 ac
, txq
->cur
, le16toh(desc
->len
), nsegs
));
2017 txq
->cur
= (txq
->cur
+ 1) % IWI_TX_RING_COUNT
;
2018 CSR_WRITE_4(sc
, txq
->csr_widx
, txq
->cur
);
2024 iwi_raw_xmit(struct ieee80211_node
*ni
, struct mbuf
*m
,
2025 const struct ieee80211_bpf_params
*params
)
2027 /* no support; just discard */
2029 ieee80211_free_node(ni
);
2034 iwi_transmit(struct ieee80211com
*ic
, struct mbuf
*m
)
2036 struct iwi_softc
*sc
= ic
->ic_softc
;
2041 if (!sc
->sc_running
) {
2045 error
= mbufq_enqueue(&sc
->sc_snd
, m
);
2056 iwi_start(struct iwi_softc
*sc
)
2059 struct ieee80211_node
*ni
;
2062 IWI_LOCK_ASSERT(sc
);
2064 while ((m
= mbufq_dequeue(&sc
->sc_snd
)) != NULL
) {
2065 ac
= M_WME_GETAC(m
);
2066 if (sc
->txq
[ac
].queued
> IWI_TX_RING_COUNT
- 8) {
2067 /* there is no place left in this ring; tail drop */
2069 mbufq_prepend(&sc
->sc_snd
, m
);
2072 ni
= (struct ieee80211_node
*) m
->m_pkthdr
.rcvif
;
2073 if (iwi_tx_start(sc
, m
, ni
, ac
) != 0) {
2074 ieee80211_free_node(ni
);
2075 if_inc_counter(ni
->ni_vap
->iv_ifp
,
2076 IFCOUNTER_OERRORS
, 1);
2079 sc
->sc_tx_timer
= 5;
2084 iwi_watchdog(void *arg
)
2086 struct iwi_softc
*sc
= arg
;
2087 struct ieee80211com
*ic
= &sc
->sc_ic
;
2089 IWI_LOCK_ASSERT(sc
);
2091 if (sc
->sc_tx_timer
> 0) {
2092 if (--sc
->sc_tx_timer
== 0) {
2093 device_printf(sc
->sc_dev
, "device timeout\n");
2094 #if defined(__DragonFly__)
2097 counter_u64_add(ic
->ic_oerrors
, 1);
2099 ieee80211_runtask(ic
, &sc
->sc_restarttask
);
2102 if (sc
->sc_state_timer
> 0) {
2103 if (--sc
->sc_state_timer
== 0) {
2104 device_printf(sc
->sc_dev
,
2105 "firmware stuck in state %d, resetting\n",
2107 if (sc
->fw_state
== IWI_FW_SCANNING
)
2108 ieee80211_cancel_scan(TAILQ_FIRST(&ic
->ic_vaps
));
2109 ieee80211_runtask(ic
, &sc
->sc_restarttask
);
2110 sc
->sc_state_timer
= 3;
2113 if (sc
->sc_busy_timer
> 0) {
2114 if (--sc
->sc_busy_timer
== 0) {
2115 device_printf(sc
->sc_dev
,
2116 "firmware command timeout, resetting\n");
2117 ieee80211_runtask(ic
, &sc
->sc_restarttask
);
2120 callout_reset(&sc
->sc_wdtimer
, hz
, iwi_watchdog
, sc
);
2124 iwi_parent(struct ieee80211com
*ic
)
2126 struct iwi_softc
*sc
= ic
->ic_softc
;
2131 if (ic
->ic_nrunning
> 0) {
2132 if (!sc
->sc_running
) {
2133 iwi_init_locked(sc
);
2136 } else if (sc
->sc_running
)
2137 iwi_stop_locked(sc
);
2140 ieee80211_start_all(ic
);
2144 iwi_stop_master(struct iwi_softc
*sc
)
2149 /* disable interrupts */
2150 CSR_WRITE_4(sc
, IWI_CSR_INTR_MASK
, 0);
2152 CSR_WRITE_4(sc
, IWI_CSR_RST
, IWI_RST_STOP_MASTER
);
2153 for (ntries
= 0; ntries
< 5; ntries
++) {
2154 if (CSR_READ_4(sc
, IWI_CSR_RST
) & IWI_RST_MASTER_DISABLED
)
2159 device_printf(sc
->sc_dev
, "timeout waiting for master\n");
2161 tmp
= CSR_READ_4(sc
, IWI_CSR_RST
);
2162 CSR_WRITE_4(sc
, IWI_CSR_RST
, tmp
| IWI_RST_PRINCETON_RESET
);
2164 sc
->flags
&= ~IWI_FLAG_FW_INITED
;
2168 iwi_reset(struct iwi_softc
*sc
)
2173 iwi_stop_master(sc
);
2175 tmp
= CSR_READ_4(sc
, IWI_CSR_CTL
);
2176 CSR_WRITE_4(sc
, IWI_CSR_CTL
, tmp
| IWI_CTL_INIT
);
2178 CSR_WRITE_4(sc
, IWI_CSR_READ_INT
, IWI_READ_INT_INIT_HOST
);
2180 /* wait for clock stabilization */
2181 for (ntries
= 0; ntries
< 1000; ntries
++) {
2182 if (CSR_READ_4(sc
, IWI_CSR_CTL
) & IWI_CTL_CLOCK_READY
)
2186 if (ntries
== 1000) {
2187 device_printf(sc
->sc_dev
,
2188 "timeout waiting for clock stabilization\n");
2192 tmp
= CSR_READ_4(sc
, IWI_CSR_RST
);
2193 CSR_WRITE_4(sc
, IWI_CSR_RST
, tmp
| IWI_RST_SOFT_RESET
);
2197 tmp
= CSR_READ_4(sc
, IWI_CSR_CTL
);
2198 CSR_WRITE_4(sc
, IWI_CSR_CTL
, tmp
| IWI_CTL_INIT
);
2200 /* clear NIC memory */
2201 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_ADDR
, 0);
2202 for (i
= 0; i
< 0xc000; i
++)
2203 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, 0);
2208 static const struct iwi_firmware_ohdr
*
2209 iwi_setup_ofw(struct iwi_softc
*sc
, struct iwi_fw
*fw
)
2211 const struct firmware
*fp
= fw
->fp
;
2212 const struct iwi_firmware_ohdr
*hdr
;
2214 if (fp
->datasize
< sizeof (struct iwi_firmware_ohdr
)) {
2215 device_printf(sc
->sc_dev
, "image '%s' too small\n", fp
->name
);
2218 hdr
= (const struct iwi_firmware_ohdr
*)fp
->data
;
2219 if ((IWI_FW_GET_MAJOR(le32toh(hdr
->version
)) != IWI_FW_REQ_MAJOR
) ||
2220 (IWI_FW_GET_MINOR(le32toh(hdr
->version
)) != IWI_FW_REQ_MINOR
)) {
2221 device_printf(sc
->sc_dev
, "version for '%s' %d.%d != %d.%d\n",
2222 fp
->name
, IWI_FW_GET_MAJOR(le32toh(hdr
->version
)),
2223 IWI_FW_GET_MINOR(le32toh(hdr
->version
)), IWI_FW_REQ_MAJOR
,
2227 fw
->data
= ((const char *) fp
->data
) + sizeof(struct iwi_firmware_ohdr
);
2228 fw
->size
= fp
->datasize
- sizeof(struct iwi_firmware_ohdr
);
2229 fw
->name
= fp
->name
;
2233 static const struct iwi_firmware_ohdr
*
2234 iwi_setup_oucode(struct iwi_softc
*sc
, struct iwi_fw
*fw
)
2236 const struct iwi_firmware_ohdr
*hdr
;
2238 hdr
= iwi_setup_ofw(sc
, fw
);
2239 if (hdr
!= NULL
&& le32toh(hdr
->mode
) != IWI_FW_MODE_UCODE
) {
2240 device_printf(sc
->sc_dev
, "%s is not a ucode image\n",
2248 iwi_getfw(struct iwi_fw
*fw
, const char *fwname
,
2249 struct iwi_fw
*uc
, const char *ucname
)
2252 fw
->fp
= firmware_get(fwname
);
2253 /* NB: pre-3.0 ucode is packaged separately */
2254 if (uc
->fp
== NULL
&& fw
->fp
!= NULL
&& fw
->fp
->version
< 300)
2255 uc
->fp
= firmware_get(ucname
);
2259 * Get the required firmware images if not already loaded.
2260 * Note that we hold firmware images so long as the device
2261 * is marked up in case we need to reload them on device init.
2262 * This is necessary because we re-init the device sometimes
2263 * from a context where we cannot read from the filesystem
2264 * (e.g. from the taskqueue thread when rfkill is re-enabled).
2265 * XXX return 0 on success, 1 on error.
2267 * NB: the order of get'ing and put'ing images here is
2268 * intentional to support handling firmware images bundled
2269 * by operating mode and/or all together in one file with
2270 * the boot firmware as "master".
2273 iwi_get_firmware(struct iwi_softc
*sc
, enum ieee80211_opmode opmode
)
2275 const struct iwi_firmware_hdr
*hdr
;
2276 const struct firmware
*fp
;
2278 /* invalidate cached firmware on mode change */
2279 if (sc
->fw_mode
!= opmode
)
2280 iwi_put_firmware(sc
);
2283 case IEEE80211_M_STA
:
2284 iwi_getfw(&sc
->fw_fw
, "iwi_bss", &sc
->fw_uc
, "iwi_ucode_bss");
2286 case IEEE80211_M_IBSS
:
2287 iwi_getfw(&sc
->fw_fw
, "iwi_ibss", &sc
->fw_uc
, "iwi_ucode_ibss");
2289 case IEEE80211_M_MONITOR
:
2290 iwi_getfw(&sc
->fw_fw
, "iwi_monitor",
2291 &sc
->fw_uc
, "iwi_ucode_monitor");
2294 device_printf(sc
->sc_dev
, "unknown opmode %d\n", opmode
);
2299 device_printf(sc
->sc_dev
, "could not load firmware\n");
2302 if (fp
->version
< 300) {
2304 * Firmware prior to 3.0 was packaged as separate
2305 * boot, firmware, and ucode images. Verify the
2306 * ucode image was read in, retrieve the boot image
2307 * if needed, and check version stamps for consistency.
2308 * The version stamps in the data are also checked
2309 * above; this is a bit paranoid but is a cheap
2310 * safeguard against mis-packaging.
2312 if (sc
->fw_uc
.fp
== NULL
) {
2313 device_printf(sc
->sc_dev
, "could not load ucode\n");
2316 if (sc
->fw_boot
.fp
== NULL
) {
2317 sc
->fw_boot
.fp
= firmware_get("iwi_boot");
2318 if (sc
->fw_boot
.fp
== NULL
) {
2319 device_printf(sc
->sc_dev
,
2320 "could not load boot firmware\n");
2324 if (sc
->fw_boot
.fp
->version
!= sc
->fw_fw
.fp
->version
||
2325 sc
->fw_boot
.fp
->version
!= sc
->fw_uc
.fp
->version
) {
2326 device_printf(sc
->sc_dev
,
2327 "firmware version mismatch: "
2328 "'%s' is %d, '%s' is %d, '%s' is %d\n",
2329 sc
->fw_boot
.fp
->name
, sc
->fw_boot
.fp
->version
,
2330 sc
->fw_uc
.fp
->name
, sc
->fw_uc
.fp
->version
,
2331 sc
->fw_fw
.fp
->name
, sc
->fw_fw
.fp
->version
2336 * Check and setup each image.
2338 if (iwi_setup_oucode(sc
, &sc
->fw_uc
) == NULL
||
2339 iwi_setup_ofw(sc
, &sc
->fw_boot
) == NULL
||
2340 iwi_setup_ofw(sc
, &sc
->fw_fw
) == NULL
)
2344 * Check and setup combined image.
2346 if (fp
->datasize
< sizeof(struct iwi_firmware_hdr
)) {
2347 device_printf(sc
->sc_dev
, "image '%s' too small\n",
2351 hdr
= (const struct iwi_firmware_hdr
*)fp
->data
;
2352 if (fp
->datasize
< sizeof(*hdr
) + le32toh(hdr
->bsize
) + le32toh(hdr
->usize
)
2353 + le32toh(hdr
->fsize
)) {
2354 device_printf(sc
->sc_dev
, "image '%s' too small (2)\n",
2358 sc
->fw_boot
.data
= ((const char *) fp
->data
) + sizeof(*hdr
);
2359 sc
->fw_boot
.size
= le32toh(hdr
->bsize
);
2360 sc
->fw_boot
.name
= fp
->name
;
2361 sc
->fw_uc
.data
= sc
->fw_boot
.data
+ sc
->fw_boot
.size
;
2362 sc
->fw_uc
.size
= le32toh(hdr
->usize
);
2363 sc
->fw_uc
.name
= fp
->name
;
2364 sc
->fw_fw
.data
= sc
->fw_uc
.data
+ sc
->fw_uc
.size
;
2365 sc
->fw_fw
.size
= le32toh(hdr
->fsize
);
2366 sc
->fw_fw
.name
= fp
->name
;
2369 device_printf(sc
->sc_dev
, "boot %d ucode %d fw %d bytes\n",
2370 sc
->fw_boot
.size
, sc
->fw_uc
.size
, sc
->fw_fw
.size
);
2373 sc
->fw_mode
= opmode
;
2376 iwi_put_firmware(sc
);
2381 iwi_put_fw(struct iwi_fw
*fw
)
2383 if (fw
->fp
!= NULL
) {
2384 firmware_put(fw
->fp
, FIRMWARE_UNLOAD
);
2393 * Release any cached firmware images.
2396 iwi_put_firmware(struct iwi_softc
*sc
)
2398 iwi_put_fw(&sc
->fw_uc
);
2399 iwi_put_fw(&sc
->fw_fw
);
2400 iwi_put_fw(&sc
->fw_boot
);
2404 iwi_load_ucode(struct iwi_softc
*sc
, const struct iwi_fw
*fw
)
2408 const char *uc
= fw
->data
;
2409 size_t size
= fw
->size
;
2410 int i
, ntries
, error
;
2412 IWI_LOCK_ASSERT(sc
);
2414 CSR_WRITE_4(sc
, IWI_CSR_RST
, CSR_READ_4(sc
, IWI_CSR_RST
) |
2415 IWI_RST_STOP_MASTER
);
2416 for (ntries
= 0; ntries
< 5; ntries
++) {
2417 if (CSR_READ_4(sc
, IWI_CSR_RST
) & IWI_RST_MASTER_DISABLED
)
2422 device_printf(sc
->sc_dev
, "timeout waiting for master\n");
2427 MEM_WRITE_4(sc
, 0x3000e0, 0x80000000);
2430 tmp
= CSR_READ_4(sc
, IWI_CSR_RST
);
2431 tmp
&= ~IWI_RST_PRINCETON_RESET
;
2432 CSR_WRITE_4(sc
, IWI_CSR_RST
, tmp
);
2435 MEM_WRITE_4(sc
, 0x3000e0, 0);
2437 MEM_WRITE_4(sc
, IWI_MEM_EEPROM_EVENT
, 1);
2439 MEM_WRITE_4(sc
, IWI_MEM_EEPROM_EVENT
, 0);
2441 MEM_WRITE_1(sc
, 0x200000, 0x00);
2442 MEM_WRITE_1(sc
, 0x200000, 0x40);
2445 /* write microcode into adapter memory */
2446 for (w
= (const uint16_t *)uc
; size
> 0; w
++, size
-= 2)
2447 MEM_WRITE_2(sc
, 0x200010, htole16(*w
));
2449 MEM_WRITE_1(sc
, 0x200000, 0x00);
2450 MEM_WRITE_1(sc
, 0x200000, 0x80);
2452 /* wait until we get an answer */
2453 for (ntries
= 0; ntries
< 100; ntries
++) {
2454 if (MEM_READ_1(sc
, 0x200000) & 1)
2458 if (ntries
== 100) {
2459 device_printf(sc
->sc_dev
,
2460 "timeout waiting for ucode to initialize\n");
2465 /* read the answer or the firmware will not initialize properly */
2466 for (i
= 0; i
< 7; i
++)
2467 MEM_READ_4(sc
, 0x200004);
2469 MEM_WRITE_1(sc
, 0x200000, 0x00);
2475 /* macro to handle unaligned little endian data in firmware image */
2476 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2479 iwi_load_firmware(struct iwi_softc
*sc
, const struct iwi_fw
*fw
)
2482 uint32_t sentinel
, ctl
, src
, dst
, sum
, len
, mlen
, tmp
;
2485 IWI_LOCK_ASSERT(sc
);
2487 /* copy firmware image to DMA memory */
2488 memcpy(sc
->fw_virtaddr
, fw
->data
, fw
->size
);
2490 /* make sure the adapter will get up-to-date values */
2491 bus_dmamap_sync(sc
->fw_dmat
, sc
->fw_map
, BUS_DMASYNC_PREWRITE
);
2493 /* tell the adapter where the command blocks are stored */
2494 MEM_WRITE_4(sc
, 0x3000a0, 0x27000);
2497 * Store command blocks into adapter's internal memory using register
2498 * indirections. The adapter will read the firmware image through DMA
2499 * using information stored in command blocks.
2501 src
= sc
->fw_physaddr
;
2502 p
= sc
->fw_virtaddr
;
2504 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_ADDR
, 0x27000);
2507 dst
= GETLE32(p
); p
+= 4; src
+= 4;
2508 len
= GETLE32(p
); p
+= 4; src
+= 4;
2512 mlen
= min(len
, IWI_CB_MAXDATALEN
);
2514 ctl
= IWI_CB_DEFAULT_CTL
| mlen
;
2515 sum
= ctl
^ src
^ dst
;
2517 /* write a command block */
2518 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, ctl
);
2519 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, src
);
2520 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, dst
);
2521 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, sum
);
2529 /* write a fictive final command block (sentinel) */
2530 sentinel
= CSR_READ_4(sc
, IWI_CSR_AUTOINC_ADDR
);
2531 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, 0);
2533 tmp
= CSR_READ_4(sc
, IWI_CSR_RST
);
2534 tmp
&= ~(IWI_RST_MASTER_DISABLED
| IWI_RST_STOP_MASTER
);
2535 CSR_WRITE_4(sc
, IWI_CSR_RST
, tmp
);
2537 /* tell the adapter to start processing command blocks */
2538 MEM_WRITE_4(sc
, 0x3000a4, 0x540100);
2540 /* wait until the adapter reaches the sentinel */
2541 for (ntries
= 0; ntries
< 400; ntries
++) {
2542 if (MEM_READ_4(sc
, 0x3000d0) >= sentinel
)
2546 /* sync dma, just in case */
2547 bus_dmamap_sync(sc
->fw_dmat
, sc
->fw_map
, BUS_DMASYNC_POSTWRITE
);
2548 if (ntries
== 400) {
2549 device_printf(sc
->sc_dev
,
2550 "timeout processing command blocks for %s firmware\n",
2555 /* we're done with command blocks processing */
2556 MEM_WRITE_4(sc
, 0x3000a4, 0x540c00);
2558 /* allow interrupts so we know when the firmware is ready */
2559 CSR_WRITE_4(sc
, IWI_CSR_INTR_MASK
, IWI_INTR_MASK
);
2561 /* tell the adapter to initialize the firmware */
2562 CSR_WRITE_4(sc
, IWI_CSR_RST
, 0);
2564 tmp
= CSR_READ_4(sc
, IWI_CSR_CTL
);
2565 CSR_WRITE_4(sc
, IWI_CSR_CTL
, tmp
| IWI_CTL_ALLOW_STANDBY
);
2567 /* wait at most one second for firmware initialization to complete */
2568 #if defined(__DragonFly__)
2569 if ((error
= lksleep(sc
, &sc
->sc_lock
, 0, "iwiinit", hz
)) != 0) {
2571 if ((error
= msleep(sc
, &sc
->sc_mtx
, 0, "iwiinit", hz
)) != 0) {
2573 device_printf(sc
->sc_dev
, "timeout waiting for %s firmware "
2574 "initialization to complete\n", fw
->name
);
2581 iwi_setpowermode(struct iwi_softc
*sc
, struct ieee80211vap
*vap
)
2585 if (vap
->iv_flags
& IEEE80211_F_PMGTON
) {
2586 /* XXX set more fine-grained operation */
2587 data
= htole32(IWI_POWER_MODE_MAX
);
2589 data
= htole32(IWI_POWER_MODE_CAM
);
2591 DPRINTF(("Setting power mode to %u\n", le32toh(data
)));
2592 return iwi_cmd(sc
, IWI_CMD_SET_POWER_MODE
, &data
, sizeof data
);
2596 iwi_setwepkeys(struct iwi_softc
*sc
, struct ieee80211vap
*vap
)
2598 struct iwi_wep_key wepkey
;
2599 struct ieee80211_key
*wk
;
2602 for (i
= 0; i
< IEEE80211_WEP_NKID
; i
++) {
2603 wk
= &vap
->iv_nw_keys
[i
];
2605 wepkey
.cmd
= IWI_WEP_KEY_CMD_SETKEY
;
2607 wepkey
.len
= wk
->wk_keylen
;
2608 memset(wepkey
.key
, 0, sizeof wepkey
.key
);
2609 memcpy(wepkey
.key
, wk
->wk_key
, wk
->wk_keylen
);
2610 DPRINTF(("Setting wep key index %u len %u\n", wepkey
.idx
,
2612 error
= iwi_cmd(sc
, IWI_CMD_SET_WEP_KEY
, &wepkey
,
2621 iwi_config(struct iwi_softc
*sc
)
2623 struct ieee80211com
*ic
= &sc
->sc_ic
;
2624 struct iwi_configuration config
;
2625 struct iwi_rateset rs
;
2626 struct iwi_txpower power
;
2630 IWI_LOCK_ASSERT(sc
);
2632 #if defined(__DragonFly__)
2633 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic
->ic_macaddr
)));
2635 DPRINTF(("Setting MAC address to %6D\n", ic
->ic_macaddr
, ":"));
2637 error
= iwi_cmd(sc
, IWI_CMD_SET_MAC_ADDRESS
, ic
->ic_macaddr
,
2638 IEEE80211_ADDR_LEN
);
2642 memset(&config
, 0, sizeof config
);
2643 config
.bluetooth_coexistence
= sc
->bluetooth
;
2644 config
.silence_threshold
= 0x1e;
2645 config
.antenna
= sc
->antenna
;
2646 config
.multicast_enabled
= 1;
2647 config
.answer_pbreq
= (ic
->ic_opmode
== IEEE80211_M_IBSS
) ? 1 : 0;
2648 config
.disable_unicast_decryption
= 1;
2649 config
.disable_multicast_decryption
= 1;
2650 if (ic
->ic_opmode
== IEEE80211_M_MONITOR
) {
2651 config
.allow_invalid_frames
= 1;
2652 config
.allow_beacon_and_probe_resp
= 1;
2653 config
.allow_mgt
= 1;
2655 DPRINTF(("Configuring adapter\n"));
2656 error
= iwi_cmd(sc
, IWI_CMD_SET_CONFIG
, &config
, sizeof config
);
2659 if (ic
->ic_opmode
== IEEE80211_M_IBSS
) {
2660 power
.mode
= IWI_MODE_11B
;
2662 for (i
= 0; i
< 11; i
++) {
2663 power
.chan
[i
].chan
= i
+ 1;
2664 power
.chan
[i
].power
= IWI_TXPOWER_MAX
;
2666 DPRINTF(("Setting .11b channels tx power\n"));
2667 error
= iwi_cmd(sc
, IWI_CMD_SET_TX_POWER
, &power
, sizeof power
);
2671 power
.mode
= IWI_MODE_11G
;
2672 DPRINTF(("Setting .11g channels tx power\n"));
2673 error
= iwi_cmd(sc
, IWI_CMD_SET_TX_POWER
, &power
, sizeof power
);
2678 memset(&rs
, 0, sizeof rs
);
2679 rs
.mode
= IWI_MODE_11G
;
2680 rs
.type
= IWI_RATESET_TYPE_SUPPORTED
;
2681 rs
.nrates
= ic
->ic_sup_rates
[IEEE80211_MODE_11G
].rs_nrates
;
2682 memcpy(rs
.rates
, ic
->ic_sup_rates
[IEEE80211_MODE_11G
].rs_rates
,
2684 DPRINTF(("Setting .11bg supported rates (%u)\n", rs
.nrates
));
2685 error
= iwi_cmd(sc
, IWI_CMD_SET_RATES
, &rs
, sizeof rs
);
2689 memset(&rs
, 0, sizeof rs
);
2690 rs
.mode
= IWI_MODE_11A
;
2691 rs
.type
= IWI_RATESET_TYPE_SUPPORTED
;
2692 rs
.nrates
= ic
->ic_sup_rates
[IEEE80211_MODE_11A
].rs_nrates
;
2693 memcpy(rs
.rates
, ic
->ic_sup_rates
[IEEE80211_MODE_11A
].rs_rates
,
2695 DPRINTF(("Setting .11a supported rates (%u)\n", rs
.nrates
));
2696 error
= iwi_cmd(sc
, IWI_CMD_SET_RATES
, &rs
, sizeof rs
);
2700 data
= htole32(karc4random());
2701 DPRINTF(("Setting initialization vector to %u\n", le32toh(data
)));
2702 error
= iwi_cmd(sc
, IWI_CMD_SET_IV
, &data
, sizeof data
);
2706 /* enable adapter */
2707 DPRINTF(("Enabling adapter\n"));
2708 return iwi_cmd(sc
, IWI_CMD_ENABLE
, NULL
, 0);
2711 static __inline
void
2712 set_scan_type(struct iwi_scan_ext
*scan
, int ix
, int scan_type
)
2714 uint8_t *st
= &scan
->scan_type
[ix
/ 2];
2716 *st
= (*st
& 0xf0) | ((scan_type
& 0xf) << 0);
2718 *st
= (*st
& 0x0f) | ((scan_type
& 0xf) << 4);
2722 scan_type(const struct ieee80211_scan_state
*ss
,
2723 const struct ieee80211_channel
*chan
)
2725 /* We can only set one essid for a directed scan */
2726 if (ss
->ss_nssid
!= 0)
2727 return IWI_SCAN_TYPE_BDIRECTED
;
2728 if ((ss
->ss_flags
& IEEE80211_SCAN_ACTIVE
) &&
2729 (chan
->ic_flags
& IEEE80211_CHAN_PASSIVE
) == 0)
2730 return IWI_SCAN_TYPE_BROADCAST
;
2731 return IWI_SCAN_TYPE_PASSIVE
;
2735 scan_band(const struct ieee80211_channel
*c
)
2737 return IEEE80211_IS_CHAN_5GHZ(c
) ? IWI_CHAN_5GHZ
: IWI_CHAN_2GHZ
;
2741 iwi_monitor_scan(void *arg
, int npending
)
2743 struct iwi_softc
*sc
= arg
;
2747 (void) iwi_scanchan(sc
, 2000, 0);
2752 * Start a scan on the current channel or all channels.
2755 iwi_scanchan(struct iwi_softc
*sc
, unsigned long maxdwell
, int allchan
)
2757 struct ieee80211com
*ic
= &sc
->sc_ic
;
2758 struct ieee80211_channel
*chan
;
2759 struct ieee80211_scan_state
*ss
;
2760 struct iwi_scan_ext scan
;
2763 IWI_LOCK_ASSERT(sc
);
2764 if (sc
->fw_state
== IWI_FW_SCANNING
) {
2766 * This should not happen as we only trigger scan_next after
2769 DPRINTF(("%s: called too early - still scanning\n", __func__
));
2772 IWI_STATE_BEGIN(sc
, IWI_FW_SCANNING
);
2776 memset(&scan
, 0, sizeof scan
);
2777 scan
.full_scan_index
= htole32(++sc
->sc_scangen
);
2778 scan
.dwell_time
[IWI_SCAN_TYPE_PASSIVE
] = htole16(maxdwell
);
2779 if (ic
->ic_flags_ext
& IEEE80211_FEXT_BGSCAN
) {
2781 * Use very short dwell times for when we send probe request
2782 * frames. Without this bg scans hang. Ideally this should
2783 * be handled with early-termination as done by net80211 but
2784 * that's not feasible (aborting a scan is problematic).
2786 scan
.dwell_time
[IWI_SCAN_TYPE_BROADCAST
] = htole16(30);
2787 scan
.dwell_time
[IWI_SCAN_TYPE_BDIRECTED
] = htole16(30);
2789 scan
.dwell_time
[IWI_SCAN_TYPE_BROADCAST
] = htole16(maxdwell
);
2790 scan
.dwell_time
[IWI_SCAN_TYPE_BDIRECTED
] = htole16(maxdwell
);
2793 /* We can only set one essid for a directed scan */
2794 if (ss
->ss_nssid
!= 0) {
2795 error
= iwi_cmd(sc
, IWI_CMD_SET_ESSID
, ss
->ss_ssid
[0].ssid
,
2796 ss
->ss_ssid
[0].len
);
2802 int i
, next
, band
, b
, bstart
;
2804 * Convert scan list to run-length encoded channel list
2805 * the firmware requires (preserving the order setup by
2806 * net80211). The first entry in each run specifies the
2807 * band and the count of items in the run.
2809 next
= 0; /* next open slot */
2810 bstart
= 0; /* NB: not needed, silence compiler */
2811 band
= -1; /* NB: impossible value */
2812 KASSERT(ss
->ss_last
> 0, ("no channels"));
2813 for (i
= 0; i
< ss
->ss_last
; i
++) {
2814 chan
= ss
->ss_chans
[i
];
2815 b
= scan_band(chan
);
2818 scan
.channels
[bstart
] =
2819 (next
- bstart
) | band
;
2820 /* NB: this allocates a slot for the run-len */
2821 band
= b
, bstart
= next
++;
2823 if (next
>= IWI_SCAN_CHANNELS
) {
2824 DPRINTF(("truncating scan list\n"));
2827 scan
.channels
[next
] = ieee80211_chan2ieee(ic
, chan
);
2828 set_scan_type(&scan
, next
, scan_type(ss
, chan
));
2831 scan
.channels
[bstart
] = (next
- bstart
) | band
;
2833 /* Scan the current channel only */
2834 chan
= ic
->ic_curchan
;
2835 scan
.channels
[0] = 1 | scan_band(chan
);
2836 scan
.channels
[1] = ieee80211_chan2ieee(ic
, chan
);
2837 set_scan_type(&scan
, 1, scan_type(ss
, chan
));
2840 if (iwi_debug
> 0) {
2841 static const char *scantype
[8] =
2842 { "PSTOP", "PASV", "DIR", "BCAST", "BDIR", "5", "6", "7" };
2844 kprintf("Scan request: index %u dwell %d/%d/%d\n"
2845 , le32toh(scan
.full_scan_index
)
2846 , le16toh(scan
.dwell_time
[IWI_SCAN_TYPE_PASSIVE
])
2847 , le16toh(scan
.dwell_time
[IWI_SCAN_TYPE_BROADCAST
])
2848 , le16toh(scan
.dwell_time
[IWI_SCAN_TYPE_BDIRECTED
])
2852 int run
= scan
.channels
[i
];
2855 kprintf("Scan %d %s channels:", run
& 0x3f,
2856 run
& IWI_CHAN_2GHZ
? "2.4GHz" : "5GHz");
2857 for (run
&= 0x3f, i
++; run
> 0; run
--, i
++) {
2858 uint8_t type
= scan
.scan_type
[i
/2];
2859 kprintf(" %u/%s", scan
.channels
[i
],
2860 scantype
[(i
& 1 ? type
: type
>>4) & 7]);
2863 } while (i
< IWI_SCAN_CHANNELS
);
2867 return (iwi_cmd(sc
, IWI_CMD_SCAN_EXT
, &scan
, sizeof scan
));
2871 iwi_set_sensitivity(struct iwi_softc
*sc
, int8_t rssi_dbm
)
2873 struct iwi_sensitivity sens
;
2875 DPRINTF(("Setting sensitivity to %d\n", rssi_dbm
));
2877 memset(&sens
, 0, sizeof sens
);
2878 sens
.rssi
= htole16(rssi_dbm
);
2879 return iwi_cmd(sc
, IWI_CMD_SET_SENSITIVITY
, &sens
, sizeof sens
);
2883 iwi_auth_and_assoc(struct iwi_softc
*sc
, struct ieee80211vap
*vap
)
2885 struct ieee80211com
*ic
= vap
->iv_ic
;
2886 struct ifnet
*ifp
= vap
->iv_ifp
;
2887 struct ieee80211_node
*ni
;
2888 struct iwi_configuration config
;
2889 struct iwi_associate
*assoc
= &sc
->assoc
;
2890 struct iwi_rateset rs
;
2895 IWI_LOCK_ASSERT(sc
);
2897 ni
= ieee80211_ref_node(vap
->iv_bss
);
2899 if (sc
->flags
& IWI_FLAG_ASSOCIATED
) {
2900 DPRINTF(("Already associated\n"));
2904 IWI_STATE_BEGIN(sc
, IWI_FW_ASSOCIATING
);
2908 if (IEEE80211_IS_CHAN_A(ic
->ic_curchan
))
2909 mode
= IWI_MODE_11A
;
2910 else if (IEEE80211_IS_CHAN_G(ic
->ic_curchan
))
2911 mode
= IWI_MODE_11G
;
2912 if (IEEE80211_IS_CHAN_B(ic
->ic_curchan
))
2913 mode
= IWI_MODE_11B
;
2915 if (IEEE80211_IS_CHAN_2GHZ(ic
->ic_curchan
)) {
2916 memset(&config
, 0, sizeof config
);
2917 config
.bluetooth_coexistence
= sc
->bluetooth
;
2918 config
.antenna
= sc
->antenna
;
2919 config
.multicast_enabled
= 1;
2920 if (mode
== IWI_MODE_11G
)
2921 config
.use_protection
= 1;
2922 config
.answer_pbreq
=
2923 (vap
->iv_opmode
== IEEE80211_M_IBSS
) ? 1 : 0;
2924 config
.disable_unicast_decryption
= 1;
2925 config
.disable_multicast_decryption
= 1;
2926 DPRINTF(("Configuring adapter\n"));
2927 error
= iwi_cmd(sc
, IWI_CMD_SET_CONFIG
, &config
, sizeof config
);
2933 if (iwi_debug
> 0) {
2934 kprintf("Setting ESSID to ");
2935 ieee80211_print_essid(ni
->ni_essid
, ni
->ni_esslen
);
2939 error
= iwi_cmd(sc
, IWI_CMD_SET_ESSID
, ni
->ni_essid
, ni
->ni_esslen
);
2943 error
= iwi_setpowermode(sc
, vap
);
2947 data
= htole32(vap
->iv_rtsthreshold
);
2948 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data
)));
2949 error
= iwi_cmd(sc
, IWI_CMD_SET_RTS_THRESHOLD
, &data
, sizeof data
);
2953 data
= htole32(vap
->iv_fragthreshold
);
2954 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data
)));
2955 error
= iwi_cmd(sc
, IWI_CMD_SET_FRAG_THRESHOLD
, &data
, sizeof data
);
2959 /* the rate set has already been "negotiated" */
2960 memset(&rs
, 0, sizeof rs
);
2962 rs
.type
= IWI_RATESET_TYPE_NEGOTIATED
;
2963 rs
.nrates
= ni
->ni_rates
.rs_nrates
;
2964 if (rs
.nrates
> IWI_RATESET_SIZE
) {
2965 DPRINTF(("Truncating negotiated rate set from %u\n",
2967 rs
.nrates
= IWI_RATESET_SIZE
;
2969 memcpy(rs
.rates
, ni
->ni_rates
.rs_rates
, rs
.nrates
);
2970 DPRINTF(("Setting negotiated rates (%u)\n", rs
.nrates
));
2971 error
= iwi_cmd(sc
, IWI_CMD_SET_RATES
, &rs
, sizeof rs
);
2975 memset(assoc
, 0, sizeof *assoc
);
2977 if ((vap
->iv_flags
& IEEE80211_F_WME
) && ni
->ni_ies
.wme_ie
!= NULL
) {
2978 /* NB: don't treat WME setup as failure */
2979 if (iwi_wme_setparams(sc
) == 0 && iwi_wme_setie(sc
) == 0)
2980 assoc
->policy
|= htole16(IWI_POLICY_WME
);
2981 /* XXX complain on failure? */
2984 if (vap
->iv_appie_wpa
!= NULL
) {
2985 struct ieee80211_appie
*ie
= vap
->iv_appie_wpa
;
2987 DPRINTF(("Setting optional IE (len=%u)\n", ie
->ie_len
));
2988 error
= iwi_cmd(sc
, IWI_CMD_SET_OPTIE
, ie
->ie_data
, ie
->ie_len
);
2993 error
= iwi_set_sensitivity(sc
, ic
->ic_node_getrssi(ni
));
2998 assoc
->chan
= ic
->ic_curchan
->ic_ieee
;
3000 * NB: do not arrange for shared key auth w/o privacy
3001 * (i.e. a wep key); it causes a firmware error.
3003 if ((vap
->iv_flags
& IEEE80211_F_PRIVACY
) &&
3004 ni
->ni_authmode
== IEEE80211_AUTH_SHARED
) {
3005 assoc
->auth
= IWI_AUTH_SHARED
;
3007 * It's possible to have privacy marked but no default
3008 * key setup. This typically is due to a user app bug
3009 * but if we blindly grab the key the firmware will
3010 * barf so avoid it for now.
3012 if (vap
->iv_def_txkey
!= IEEE80211_KEYIX_NONE
)
3013 assoc
->auth
|= vap
->iv_def_txkey
<< 4;
3015 error
= iwi_setwepkeys(sc
, vap
);
3019 if (vap
->iv_flags
& IEEE80211_F_WPA
)
3020 assoc
->policy
|= htole16(IWI_POLICY_WPA
);
3021 if (vap
->iv_opmode
== IEEE80211_M_IBSS
&& ni
->ni_tstamp
.tsf
== 0)
3022 assoc
->type
= IWI_HC_IBSS_START
;
3024 assoc
->type
= IWI_HC_ASSOC
;
3025 memcpy(assoc
->tstamp
, ni
->ni_tstamp
.data
, 8);
3027 if (vap
->iv_opmode
== IEEE80211_M_IBSS
)
3028 capinfo
= IEEE80211_CAPINFO_IBSS
;
3030 capinfo
= IEEE80211_CAPINFO_ESS
;
3031 if (vap
->iv_flags
& IEEE80211_F_PRIVACY
)
3032 capinfo
|= IEEE80211_CAPINFO_PRIVACY
;
3033 if ((ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
) &&
3034 IEEE80211_IS_CHAN_2GHZ(ic
->ic_curchan
))
3035 capinfo
|= IEEE80211_CAPINFO_SHORT_PREAMBLE
;
3036 if (ni
->ni_capinfo
& IEEE80211_CAPINFO_SHORT_SLOTTIME
)
3037 capinfo
|= IEEE80211_CAPINFO_SHORT_SLOTTIME
;
3038 assoc
->capinfo
= htole16(capinfo
);
3040 assoc
->lintval
= htole16(ic
->ic_lintval
);
3041 assoc
->intval
= htole16(ni
->ni_intval
);
3042 IEEE80211_ADDR_COPY(assoc
->bssid
, ni
->ni_bssid
);
3043 if (vap
->iv_opmode
== IEEE80211_M_IBSS
)
3044 IEEE80211_ADDR_COPY(assoc
->dst
, ifp
->if_broadcastaddr
);
3046 IEEE80211_ADDR_COPY(assoc
->dst
, ni
->ni_bssid
);
3048 #if defined(__DragonFly__)
3049 DPRINTF(("%s bssid %s dst %s channel %u policy 0x%x "
3050 "auth %u capinfo 0x%x lintval %u bintval %u\n",
3051 assoc
->type
== IWI_HC_IBSS_START
? "Start" : "Join",
3052 ether_sprintf(assoc
->bssid
), ether_sprintf(assoc
->dst
),
3053 assoc
->chan
, le16toh(assoc
->policy
), assoc
->auth
,
3054 le16toh(assoc
->capinfo
), le16toh(assoc
->lintval
),
3055 le16toh(assoc
->intval
)));
3057 DPRINTF(("%s bssid %6D dst %6D channel %u policy 0x%x "
3058 "auth %u capinfo 0x%x lintval %u bintval %u\n",
3059 assoc
->type
== IWI_HC_IBSS_START
? "Start" : "Join",
3060 assoc
->bssid
, ":", assoc
->dst
, ":",
3061 assoc
->chan
, le16toh(assoc
->policy
), assoc
->auth
,
3062 le16toh(assoc
->capinfo
), le16toh(assoc
->lintval
),
3063 le16toh(assoc
->intval
)));
3065 error
= iwi_cmd(sc
, IWI_CMD_ASSOCIATE
, assoc
, sizeof *assoc
);
3067 ieee80211_free_node(ni
);
3069 IWI_STATE_END(sc
, IWI_FW_ASSOCIATING
);
3075 iwi_disassoc(void *arg
, int pending
)
3077 struct iwi_softc
*sc
= arg
;
3081 iwi_disassociate(sc
, 0);
3086 iwi_disassociate(struct iwi_softc
*sc
, int quiet
)
3088 struct iwi_associate
*assoc
= &sc
->assoc
;
3090 if ((sc
->flags
& IWI_FLAG_ASSOCIATED
) == 0) {
3091 DPRINTF(("Not associated\n"));
3095 IWI_STATE_BEGIN(sc
, IWI_FW_DISASSOCIATING
);
3098 assoc
->type
= IWI_HC_DISASSOC_QUIET
;
3100 assoc
->type
= IWI_HC_DISASSOC
;
3102 #if defined(__DragonFly__)
3103 DPRINTF(("Trying to disassociate from %s channel %u\n",
3104 ether_sprintf(assoc
->bssid
), assoc
->chan
));
3106 DPRINTF(("Trying to disassociate from %6D channel %u\n",
3107 assoc
->bssid
, ":", assoc
->chan
));
3109 return iwi_cmd(sc
, IWI_CMD_ASSOCIATE
, assoc
, sizeof *assoc
);
3113 * release dma resources for the firmware
3116 iwi_release_fw_dma(struct iwi_softc
*sc
)
3118 if (sc
->fw_flags
& IWI_FW_HAVE_PHY
)
3119 bus_dmamap_unload(sc
->fw_dmat
, sc
->fw_map
);
3120 if (sc
->fw_flags
& IWI_FW_HAVE_MAP
)
3121 bus_dmamem_free(sc
->fw_dmat
, sc
->fw_virtaddr
, sc
->fw_map
);
3122 if (sc
->fw_flags
& IWI_FW_HAVE_DMAT
)
3123 bus_dma_tag_destroy(sc
->fw_dmat
);
3126 sc
->fw_dma_size
= 0;
3129 sc
->fw_physaddr
= 0;
3130 sc
->fw_virtaddr
= NULL
;
3134 * allocate the dma descriptor for the firmware.
3135 * Return 0 on success, 1 on error.
3136 * Must be called unlocked, protected by IWI_FLAG_FW_LOADING.
3139 iwi_init_fw_dma(struct iwi_softc
*sc
, int size
)
3141 if (sc
->fw_dma_size
>= size
)
3143 #if defined(__DragonFly__)
3144 if (bus_dma_tag_create(NULL
, 4, 0, BUS_SPACE_MAXADDR_32BIT
,
3145 BUS_SPACE_MAXADDR
, NULL
, NULL
, size
, 1, size
,
3146 0, &sc
->fw_dmat
) != 0) {
3148 if (bus_dma_tag_create(bus_get_dma_tag(sc
->sc_dev
), 4, 0,
3149 BUS_SPACE_MAXADDR_32BIT
, BUS_SPACE_MAXADDR
, NULL
, NULL
,
3150 size
, 1, size
, 0, NULL
, NULL
, &sc
->fw_dmat
) != 0) {
3152 device_printf(sc
->sc_dev
,
3153 "could not create firmware DMA tag\n");
3156 sc
->fw_flags
|= IWI_FW_HAVE_DMAT
;
3157 if (bus_dmamem_alloc(sc
->fw_dmat
, &sc
->fw_virtaddr
, 0,
3158 &sc
->fw_map
) != 0) {
3159 device_printf(sc
->sc_dev
,
3160 "could not allocate firmware DMA memory\n");
3163 sc
->fw_flags
|= IWI_FW_HAVE_MAP
;
3164 if (bus_dmamap_load(sc
->fw_dmat
, sc
->fw_map
, sc
->fw_virtaddr
,
3165 size
, iwi_dma_map_addr
, &sc
->fw_physaddr
, 0) != 0) {
3166 device_printf(sc
->sc_dev
, "could not load firmware DMA map\n");
3169 sc
->fw_flags
|= IWI_FW_HAVE_PHY
;
3170 sc
->fw_dma_size
= size
;
3174 iwi_release_fw_dma(sc
);
3179 iwi_init_locked(struct iwi_softc
*sc
)
3181 struct iwi_rx_data
*data
;
3184 IWI_LOCK_ASSERT(sc
);
3186 if (sc
->fw_state
== IWI_FW_LOADING
) {
3187 device_printf(sc
->sc_dev
, "%s: already loading\n", __func__
);
3188 return; /* XXX: condvar? */
3191 iwi_stop_locked(sc
);
3193 IWI_STATE_BEGIN(sc
, IWI_FW_LOADING
);
3195 if (iwi_reset(sc
) != 0) {
3196 device_printf(sc
->sc_dev
, "could not reset adapter\n");
3199 if (iwi_load_firmware(sc
, &sc
->fw_boot
) != 0) {
3200 device_printf(sc
->sc_dev
,
3201 "could not load boot firmware %s\n", sc
->fw_boot
.name
);
3204 if (iwi_load_ucode(sc
, &sc
->fw_uc
) != 0) {
3205 device_printf(sc
->sc_dev
,
3206 "could not load microcode %s\n", sc
->fw_uc
.name
);
3210 iwi_stop_master(sc
);
3212 CSR_WRITE_4(sc
, IWI_CSR_CMD_BASE
, sc
->cmdq
.physaddr
);
3213 CSR_WRITE_4(sc
, IWI_CSR_CMD_SIZE
, sc
->cmdq
.count
);
3214 CSR_WRITE_4(sc
, IWI_CSR_CMD_WIDX
, sc
->cmdq
.cur
);
3216 CSR_WRITE_4(sc
, IWI_CSR_TX1_BASE
, sc
->txq
[0].physaddr
);
3217 CSR_WRITE_4(sc
, IWI_CSR_TX1_SIZE
, sc
->txq
[0].count
);
3218 CSR_WRITE_4(sc
, IWI_CSR_TX1_WIDX
, sc
->txq
[0].cur
);
3220 CSR_WRITE_4(sc
, IWI_CSR_TX2_BASE
, sc
->txq
[1].physaddr
);
3221 CSR_WRITE_4(sc
, IWI_CSR_TX2_SIZE
, sc
->txq
[1].count
);
3222 CSR_WRITE_4(sc
, IWI_CSR_TX2_WIDX
, sc
->txq
[1].cur
);
3224 CSR_WRITE_4(sc
, IWI_CSR_TX3_BASE
, sc
->txq
[2].physaddr
);
3225 CSR_WRITE_4(sc
, IWI_CSR_TX3_SIZE
, sc
->txq
[2].count
);
3226 CSR_WRITE_4(sc
, IWI_CSR_TX3_WIDX
, sc
->txq
[2].cur
);
3228 CSR_WRITE_4(sc
, IWI_CSR_TX4_BASE
, sc
->txq
[3].physaddr
);
3229 CSR_WRITE_4(sc
, IWI_CSR_TX4_SIZE
, sc
->txq
[3].count
);
3230 CSR_WRITE_4(sc
, IWI_CSR_TX4_WIDX
, sc
->txq
[3].cur
);
3232 for (i
= 0; i
< sc
->rxq
.count
; i
++) {
3233 data
= &sc
->rxq
.data
[i
];
3234 CSR_WRITE_4(sc
, data
->reg
, data
->physaddr
);
3237 CSR_WRITE_4(sc
, IWI_CSR_RX_WIDX
, sc
->rxq
.count
- 1);
3239 if (iwi_load_firmware(sc
, &sc
->fw_fw
) != 0) {
3240 device_printf(sc
->sc_dev
,
3241 "could not load main firmware %s\n", sc
->fw_fw
.name
);
3244 sc
->flags
|= IWI_FLAG_FW_INITED
;
3246 IWI_STATE_END(sc
, IWI_FW_LOADING
);
3248 if (iwi_config(sc
) != 0) {
3249 device_printf(sc
->sc_dev
, "unable to enable adapter\n");
3253 callout_reset(&sc
->sc_wdtimer
, hz
, iwi_watchdog
, sc
);
3257 IWI_STATE_END(sc
, IWI_FW_LOADING
);
3259 iwi_stop_locked(sc
);
3263 iwi_init(void *priv
)
3265 struct iwi_softc
*sc
= priv
;
3266 struct ieee80211com
*ic
= &sc
->sc_ic
;
3270 iwi_init_locked(sc
);
3274 ieee80211_start_all(ic
);
3278 iwi_stop_locked(void *priv
)
3280 struct iwi_softc
*sc
= priv
;
3282 IWI_LOCK_ASSERT(sc
);
3286 if (sc
->sc_softled
) {
3287 callout_stop(&sc
->sc_ledtimer
);
3288 sc
->sc_blinking
= 0;
3290 callout_stop(&sc
->sc_wdtimer
);
3291 callout_stop(&sc
->sc_rftimer
);
3293 iwi_stop_master(sc
);
3295 CSR_WRITE_4(sc
, IWI_CSR_RST
, IWI_RST_SOFT_RESET
);
3298 iwi_reset_cmd_ring(sc
, &sc
->cmdq
);
3299 iwi_reset_tx_ring(sc
, &sc
->txq
[0]);
3300 iwi_reset_tx_ring(sc
, &sc
->txq
[1]);
3301 iwi_reset_tx_ring(sc
, &sc
->txq
[2]);
3302 iwi_reset_tx_ring(sc
, &sc
->txq
[3]);
3303 iwi_reset_rx_ring(sc
, &sc
->rxq
);
3305 sc
->sc_tx_timer
= 0;
3306 sc
->sc_state_timer
= 0;
3307 sc
->sc_busy_timer
= 0;
3308 sc
->flags
&= ~(IWI_FLAG_BUSY
| IWI_FLAG_ASSOCIATED
);
3309 sc
->fw_state
= IWI_FW_IDLE
;
3314 iwi_stop(struct iwi_softc
*sc
)
3319 iwi_stop_locked(sc
);
3324 iwi_restart(void *arg
, int npending
)
3326 struct iwi_softc
*sc
= arg
;
3332 * Return whether or not the radio is enabled in hardware
3333 * (i.e. the rfkill switch is "off").
3336 iwi_getrfkill(struct iwi_softc
*sc
)
3338 return (CSR_READ_4(sc
, IWI_CSR_IO
) & IWI_IO_RADIO_ENABLED
) == 0;
3342 iwi_radio_on(void *arg
, int pending
)
3344 struct iwi_softc
*sc
= arg
;
3345 struct ieee80211com
*ic
= &sc
->sc_ic
;
3347 device_printf(sc
->sc_dev
, "radio turned on\n");
3350 ieee80211_notify_radio(ic
, 1);
3354 iwi_rfkill_poll(void *arg
)
3356 struct iwi_softc
*sc
= arg
;
3358 IWI_LOCK_ASSERT(sc
);
3361 * Check for a change in rfkill state. We get an
3362 * interrupt when a radio is disabled but not when
3363 * it is enabled so we must poll for the latter.
3365 if (!iwi_getrfkill(sc
)) {
3366 ieee80211_runtask(&sc
->sc_ic
, &sc
->sc_radiontask
);
3369 callout_reset(&sc
->sc_rftimer
, 2*hz
, iwi_rfkill_poll
, sc
);
3373 iwi_radio_off(void *arg
, int pending
)
3375 struct iwi_softc
*sc
= arg
;
3376 struct ieee80211com
*ic
= &sc
->sc_ic
;
3379 device_printf(sc
->sc_dev
, "radio turned off\n");
3381 ieee80211_notify_radio(ic
, 0);
3384 iwi_stop_locked(sc
);
3385 iwi_rfkill_poll(sc
);
3390 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS
)
3392 struct iwi_softc
*sc
= arg1
;
3393 uint32_t size
, buf
[128];
3395 memset(buf
, 0, sizeof buf
);
3397 if (!(sc
->flags
& IWI_FLAG_FW_INITED
))
3398 return SYSCTL_OUT(req
, buf
, sizeof buf
);
3400 size
= min(CSR_READ_4(sc
, IWI_CSR_TABLE0_SIZE
), 128 - 1);
3401 CSR_READ_REGION_4(sc
, IWI_CSR_TABLE0_BASE
, &buf
[1], size
);
3403 return SYSCTL_OUT(req
, buf
, size
);
3407 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS
)
3409 struct iwi_softc
*sc
= arg1
;
3410 int val
= !iwi_getrfkill(sc
);
3412 return SYSCTL_OUT(req
, &val
, sizeof val
);
3419 iwi_sysctlattach(struct iwi_softc
*sc
)
3421 struct sysctl_ctx_list
*ctx
= device_get_sysctl_ctx(sc
->sc_dev
);
3422 struct sysctl_oid
*tree
= device_get_sysctl_tree(sc
->sc_dev
);
3424 SYSCTL_ADD_PROC(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
, "radio",
3425 CTLTYPE_INT
| CTLFLAG_RD
, sc
, 0, iwi_sysctl_radio
, "I",
3426 "radio transmitter switch state (0=off, 1=on)");
3428 SYSCTL_ADD_PROC(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
, "stats",
3429 CTLTYPE_OPAQUE
| CTLFLAG_RD
, sc
, 0, iwi_sysctl_stats
, "S",
3433 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
, "bluetooth",
3434 CTLFLAG_RW
, &sc
->bluetooth
, 0, "bluetooth coexistence");
3436 sc
->antenna
= IWI_ANTENNA_AUTO
;
3437 SYSCTL_ADD_INT(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
, "antenna",
3438 CTLFLAG_RW
, &sc
->antenna
, 0, "antenna (0=auto)");
3444 * Different cards have different capabilities. Some have three
3445 * led's while others have only one. The linux ipw driver defines
3446 * led's for link state (associated or not), band (11a, 11g, 11b),
3447 * and for link activity. We use one led and vary the blink rate
3448 * according to the tx/rx traffic a la the ath driver.
3451 static __inline
uint32_t
3452 iwi_toggle_event(uint32_t r
)
3454 return r
&~ (IWI_RST_STANDBY
| IWI_RST_GATE_ODMA
|
3455 IWI_RST_GATE_IDMA
| IWI_RST_GATE_ADMA
);
3459 iwi_read_event(struct iwi_softc
*sc
)
3461 return MEM_READ_4(sc
, IWI_MEM_EEPROM_EVENT
);
3465 iwi_write_event(struct iwi_softc
*sc
, uint32_t v
)
3467 MEM_WRITE_4(sc
, IWI_MEM_EEPROM_EVENT
, v
);
3471 iwi_led_done(void *arg
)
3473 struct iwi_softc
*sc
= arg
;
3475 sc
->sc_blinking
= 0;
3479 * Turn the activity LED off: flip the pin and then set a timer so no
3480 * update will happen for the specified duration.
3483 iwi_led_off(void *arg
)
3485 struct iwi_softc
*sc
= arg
;
3488 v
= iwi_read_event(sc
);
3489 v
&= ~sc
->sc_ledpin
;
3490 iwi_write_event(sc
, iwi_toggle_event(v
));
3491 callout_reset(&sc
->sc_ledtimer
, sc
->sc_ledoff
, iwi_led_done
, sc
);
3495 * Blink the LED according to the specified on/off times.
3498 iwi_led_blink(struct iwi_softc
*sc
, int on
, int off
)
3502 v
= iwi_read_event(sc
);
3504 iwi_write_event(sc
, iwi_toggle_event(v
));
3505 sc
->sc_blinking
= 1;
3506 sc
->sc_ledoff
= off
;
3507 callout_reset(&sc
->sc_ledtimer
, on
, iwi_led_off
, sc
);
3511 iwi_led_event(struct iwi_softc
*sc
, int event
)
3513 /* NB: on/off times from the Atheros NDIS driver, w/ permission */
3514 static const struct {
3515 u_int rate
; /* tx/rx iwi rate */
3516 u_int16_t timeOn
; /* LED on time (ms) */
3517 u_int16_t timeOff
; /* LED off time (ms) */
3519 { IWI_RATE_OFDM54
, 40, 10 },
3520 { IWI_RATE_OFDM48
, 44, 11 },
3521 { IWI_RATE_OFDM36
, 50, 13 },
3522 { IWI_RATE_OFDM24
, 57, 14 },
3523 { IWI_RATE_OFDM18
, 67, 16 },
3524 { IWI_RATE_OFDM12
, 80, 20 },
3525 { IWI_RATE_DS11
, 100, 25 },
3526 { IWI_RATE_OFDM9
, 133, 34 },
3527 { IWI_RATE_OFDM6
, 160, 40 },
3528 { IWI_RATE_DS5
, 200, 50 },
3529 { 6, 240, 58 }, /* XXX 3Mb/s if it existed */
3530 { IWI_RATE_DS2
, 267, 66 },
3531 { IWI_RATE_DS1
, 400, 100 },
3532 { 0, 500, 130 }, /* unknown rate/polling */
3535 int j
= 0; /* XXX silence compiler */
3537 sc
->sc_ledevent
= ticks
; /* time of last event */
3538 if (sc
->sc_blinking
) /* don't interrupt active blink */
3542 j
= nitems(blinkrates
)-1;
3545 /* read current transmission rate from adapter */
3546 txrate
= CSR_READ_4(sc
, IWI_CSR_CURRENT_TX_RATE
);
3547 if (blinkrates
[sc
->sc_txrix
].rate
!= txrate
) {
3548 for (j
= 0; j
< nitems(blinkrates
)-1; j
++)
3549 if (blinkrates
[j
].rate
== txrate
)
3556 if (blinkrates
[sc
->sc_rxrix
].rate
!= sc
->sc_rxrate
) {
3557 for (j
= 0; j
< nitems(blinkrates
)-1; j
++)
3558 if (blinkrates
[j
].rate
== sc
->sc_rxrate
)
3565 /* XXX beware of overflow */
3566 iwi_led_blink(sc
, (blinkrates
[j
].timeOn
* hz
) / 1000,
3567 (blinkrates
[j
].timeOff
* hz
) / 1000);
3571 iwi_sysctl_softled(SYSCTL_HANDLER_ARGS
)
3573 struct iwi_softc
*sc
= arg1
;
3574 int softled
= sc
->sc_softled
;
3577 error
= sysctl_handle_int(oidp
, &softled
, 0, req
);
3578 if (error
|| !req
->newptr
)
3580 softled
= (softled
!= 0);
3581 if (softled
!= sc
->sc_softled
) {
3583 uint32_t v
= iwi_read_event(sc
);
3584 v
&= ~sc
->sc_ledpin
;
3585 iwi_write_event(sc
, iwi_toggle_event(v
));
3587 sc
->sc_softled
= softled
;
3593 iwi_ledattach(struct iwi_softc
*sc
)
3595 struct sysctl_ctx_list
*ctx
= device_get_sysctl_ctx(sc
->sc_dev
);
3596 struct sysctl_oid
*tree
= device_get_sysctl_tree(sc
->sc_dev
);
3598 sc
->sc_blinking
= 0;
3599 sc
->sc_ledstate
= 1;
3600 sc
->sc_ledidle
= (2700*hz
)/1000; /* 2.7sec */
3601 #if defined(__DragonFly__)
3602 callout_init_lk(&sc
->sc_ledtimer
, &sc
->sc_lock
);
3604 callout_init_mtx(&sc
->sc_ledtimer
, &sc
->sc_mtx
, 0);
3607 SYSCTL_ADD_PROC(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
3608 "softled", CTLTYPE_INT
| CTLFLAG_RW
, sc
, 0,
3609 iwi_sysctl_softled
, "I", "enable/disable software LED support");
3610 SYSCTL_ADD_UINT(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
3611 "ledpin", CTLFLAG_RW
, &sc
->sc_ledpin
, 0,
3612 "pin setting to turn activity LED on");
3613 SYSCTL_ADD_UINT(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
3614 "ledidle", CTLFLAG_RW
, &sc
->sc_ledidle
, 0,
3615 "idle time for inactivity LED (ticks)");
3616 /* XXX for debugging */
3617 SYSCTL_ADD_UINT(ctx
, SYSCTL_CHILDREN(tree
), OID_AUTO
,
3618 "nictype", CTLFLAG_RD
, &sc
->sc_nictype
, 0,
3619 "NIC type from EEPROM");
3621 sc
->sc_ledpin
= IWI_RST_LED_ACTIVITY
;
3624 sc
->sc_nictype
= (iwi_read_prom_word(sc
, IWI_EEPROM_NIC
) >> 8) & 0xff;
3625 if (sc
->sc_nictype
== 1) {
3627 * NB: led's are reversed.
3629 sc
->sc_ledpin
= IWI_RST_LED_ASSOCIATED
;
3634 iwi_scan_start(struct ieee80211com
*ic
)
3640 iwi_set_channel(struct ieee80211com
*ic
)
3642 struct iwi_softc
*sc
= ic
->ic_softc
;
3644 if (sc
->fw_state
== IWI_FW_IDLE
)
3645 iwi_setcurchan(sc
, ic
->ic_curchan
->ic_ieee
);
3649 iwi_scan_curchan(struct ieee80211_scan_state
*ss
, unsigned long maxdwell
)
3651 struct ieee80211vap
*vap
= ss
->ss_vap
;
3652 struct iwi_softc
*sc
= vap
->iv_ic
->ic_softc
;
3656 if (iwi_scanchan(sc
, maxdwell
, 0))
3657 ieee80211_cancel_scan(vap
);
3662 iwi_scan_mindwell(struct ieee80211_scan_state
*ss
)
3664 /* NB: don't try to abort scan; wait for firmware to finish */
3668 iwi_scan_end(struct ieee80211com
*ic
)
3670 struct iwi_softc
*sc
= ic
->ic_softc
;
3674 sc
->flags
&= ~IWI_FLAG_CHANNEL_SCAN
;
3675 /* NB: make sure we're still scanning */
3676 if (sc
->fw_state
== IWI_FW_SCANNING
)
3677 iwi_cmd(sc
, IWI_CMD_ABORT_SCAN
, NULL
, 0);