1 /* $NetBSD: if_iwi.c,v 1.81 2009/05/06 09:25:15 cegger Exp $ */
4 * Copyright (c) 2004, 2005
5 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved.
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
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.81 2009/05/06 09:25:15 cegger Exp $");
34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
40 #include <sys/param.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
49 #include <sys/kauth.h>
52 #include <machine/endian.h>
55 #include <dev/firmload.h>
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcidevs.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_ether.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
71 #include <net80211/ieee80211_var.h>
72 #include <net80211/ieee80211_radiotap.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
79 #include <crypto/arc4/arc4.h>
81 #include <dev/pci/if_iwireg.h>
82 #include <dev/pci/if_iwivar.h>
85 #define DPRINTF(x) if (iwi_debug > 0) printf x
86 #define DPRINTFN(n, x) if (iwi_debug >= (n)) printf x
90 #define DPRINTFN(n, x)
93 /* Permit loading the Intel firmware */
94 static int iwi_accept_eula
;
96 static int iwi_match(device_t
, cfdata_t
, void *);
97 static void iwi_attach(device_t
, device_t
, void *);
98 static int iwi_detach(device_t
, int);
100 static int iwi_alloc_cmd_ring(struct iwi_softc
*, struct iwi_cmd_ring
*,
102 static void iwi_reset_cmd_ring(struct iwi_softc
*, struct iwi_cmd_ring
*);
103 static void iwi_free_cmd_ring(struct iwi_softc
*, struct iwi_cmd_ring
*);
104 static int iwi_alloc_tx_ring(struct iwi_softc
*, struct iwi_tx_ring
*,
105 int, bus_size_t
, bus_size_t
);
106 static void iwi_reset_tx_ring(struct iwi_softc
*, struct iwi_tx_ring
*);
107 static void iwi_free_tx_ring(struct iwi_softc
*, struct iwi_tx_ring
*);
109 iwi_alloc_rx_buf(struct iwi_softc
*sc
);
110 static int iwi_alloc_rx_ring(struct iwi_softc
*, struct iwi_rx_ring
*,
112 static void iwi_reset_rx_ring(struct iwi_softc
*, struct iwi_rx_ring
*);
113 static void iwi_free_rx_ring(struct iwi_softc
*, struct iwi_rx_ring
*);
115 static struct ieee80211_node
*iwi_node_alloc(struct ieee80211_node_table
*);
116 static void iwi_node_free(struct ieee80211_node
*);
118 static int iwi_cvtrate(int);
119 static int iwi_media_change(struct ifnet
*);
120 static void iwi_media_status(struct ifnet
*, struct ifmediareq
*);
121 static int iwi_wme_update(struct ieee80211com
*);
122 static uint16_t iwi_read_prom_word(struct iwi_softc
*, uint8_t);
123 static int iwi_newstate(struct ieee80211com
*, enum ieee80211_state
, int);
124 static void iwi_fix_channel(struct ieee80211com
*, struct mbuf
*);
125 static void iwi_frame_intr(struct iwi_softc
*, struct iwi_rx_data
*, int,
127 static void iwi_notification_intr(struct iwi_softc
*, struct iwi_notif
*);
128 static void iwi_cmd_intr(struct iwi_softc
*);
129 static void iwi_rx_intr(struct iwi_softc
*);
130 static void iwi_tx_intr(struct iwi_softc
*, struct iwi_tx_ring
*);
131 static int iwi_intr(void *);
132 static int iwi_cmd(struct iwi_softc
*, uint8_t, void *, uint8_t, int);
133 static void iwi_write_ibssnode(struct iwi_softc
*, const struct iwi_node
*);
134 static int iwi_tx_start(struct ifnet
*, struct mbuf
*, struct ieee80211_node
*,
136 static void iwi_start(struct ifnet
*);
137 static void iwi_watchdog(struct ifnet
*);
139 static int iwi_alloc_unr(struct iwi_softc
*);
140 static void iwi_free_unr(struct iwi_softc
*, int);
142 static int iwi_get_table0(struct iwi_softc
*, uint32_t *);
144 static int iwi_ioctl(struct ifnet
*, u_long
, void *);
145 static void iwi_stop_master(struct iwi_softc
*);
146 static int iwi_reset(struct iwi_softc
*);
147 static int iwi_load_ucode(struct iwi_softc
*, void *, int);
148 static int iwi_load_firmware(struct iwi_softc
*, void *, int);
149 static int iwi_cache_firmware(struct iwi_softc
*);
150 static void iwi_free_firmware(struct iwi_softc
*);
151 static int iwi_config(struct iwi_softc
*);
152 static int iwi_set_chan(struct iwi_softc
*, struct ieee80211_channel
*);
153 static int iwi_scan(struct iwi_softc
*);
154 static int iwi_auth_and_assoc(struct iwi_softc
*);
155 static int iwi_init(struct ifnet
*);
156 static void iwi_stop(struct ifnet
*, int);
157 static int iwi_getrfkill(struct iwi_softc
*);
158 static void iwi_led_set(struct iwi_softc
*, uint32_t, int);
159 static void iwi_sysctlattach(struct iwi_softc
*);
162 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
164 static const struct ieee80211_rateset iwi_rateset_11a
=
165 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
167 static const struct ieee80211_rateset iwi_rateset_11b
=
168 { 4, { 2, 4, 11, 22 } };
170 static const struct ieee80211_rateset iwi_rateset_11g
=
171 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
173 static inline uint8_t
174 MEM_READ_1(struct iwi_softc
*sc
, uint32_t addr
)
176 CSR_WRITE_4(sc
, IWI_CSR_INDIRECT_ADDR
, addr
);
177 return CSR_READ_1(sc
, IWI_CSR_INDIRECT_DATA
);
180 static inline uint32_t
181 MEM_READ_4(struct iwi_softc
*sc
, uint32_t addr
)
183 CSR_WRITE_4(sc
, IWI_CSR_INDIRECT_ADDR
, addr
);
184 return CSR_READ_4(sc
, IWI_CSR_INDIRECT_DATA
);
187 CFATTACH_DECL_NEW(iwi
, sizeof (struct iwi_softc
), iwi_match
, iwi_attach
,
191 iwi_match(device_t parent
, cfdata_t match
, void *aux
)
193 struct pci_attach_args
*pa
= aux
;
195 if (PCI_VENDOR(pa
->pa_id
) != PCI_VENDOR_INTEL
)
198 if (PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_INTEL_PRO_WL_2200BG
||
199 PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_INTEL_PRO_WL_2225BG
||
200 PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1
||
201 PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2
)
207 /* Base Address Register */
208 #define IWI_PCI_BAR0 0x10
211 iwi_attach(device_t parent
, device_t self
, void *aux
)
213 struct iwi_softc
*sc
= device_private(self
);
214 struct ieee80211com
*ic
= &sc
->sc_ic
;
215 struct ifnet
*ifp
= &sc
->sc_if
;
216 struct pci_attach_args
*pa
= aux
;
219 bus_space_tag_t memt
;
220 bus_space_handle_t memh
;
221 pci_intr_handle_t ih
;
224 int error
, revision
, i
;
227 sc
->sc_pct
= pa
->pa_pc
;
228 sc
->sc_pcitag
= pa
->pa_tag
;
230 pci_devinfo(pa
->pa_id
, pa
->pa_class
, 0, devinfo
, sizeof devinfo
);
231 revision
= PCI_REVISION(pa
->pa_class
);
232 aprint_normal(": %s (rev. 0x%02x)\n", devinfo
, revision
);
234 /* clear unit numbers allocated to IBSS */
238 if ((error
= pci_activate(pa
->pa_pc
, pa
->pa_tag
, self
,
239 NULL
)) && error
!= EOPNOTSUPP
) {
240 aprint_error_dev(self
, "cannot activate %d\n", error
);
244 /* enable bus-mastering */
245 data
= pci_conf_read(sc
->sc_pct
, sc
->sc_pcitag
, PCI_COMMAND_STATUS_REG
);
246 data
|= PCI_COMMAND_MASTER_ENABLE
;
247 pci_conf_write(sc
->sc_pct
, sc
->sc_pcitag
, PCI_COMMAND_STATUS_REG
, data
);
249 /* map the register window */
250 error
= pci_mapreg_map(pa
, IWI_PCI_BAR0
, PCI_MAPREG_TYPE_MEM
|
251 PCI_MAPREG_MEM_TYPE_32BIT
, 0, &memt
, &memh
, NULL
, &sc
->sc_sz
);
253 aprint_error_dev(self
, "could not map memory space\n");
259 sc
->sc_dmat
= pa
->pa_dmat
;
261 /* disable interrupts */
262 CSR_WRITE_4(sc
, IWI_CSR_INTR_MASK
, 0);
264 if (pci_intr_map(pa
, &ih
) != 0) {
265 aprint_error_dev(self
, "could not map interrupt\n");
269 intrstr
= pci_intr_string(sc
->sc_pct
, ih
);
270 sc
->sc_ih
= pci_intr_establish(sc
->sc_pct
, ih
, IPL_NET
, iwi_intr
, sc
);
271 if (sc
->sc_ih
== NULL
) {
272 aprint_error_dev(self
, "could not establish interrupt");
274 aprint_error(" at %s", intrstr
);
278 aprint_normal_dev(self
, "interrupting at %s\n", intrstr
);
280 if (iwi_reset(sc
) != 0) {
281 pci_intr_disestablish(sc
->sc_pct
, sc
->sc_ih
);
282 aprint_error_dev(self
, "could not reset adapter\n");
287 ic
->ic_wme
.wme_update
= iwi_wme_update
;
288 ic
->ic_phytype
= IEEE80211_T_OFDM
; /* not only, but not used */
289 ic
->ic_opmode
= IEEE80211_M_STA
; /* default to BSS mode */
290 ic
->ic_state
= IEEE80211_S_INIT
;
292 sc
->sc_fwname
= "ipw2200-bss.fw";
294 /* set device capabilities */
296 IEEE80211_C_IBSS
| /* IBSS mode supported */
297 IEEE80211_C_MONITOR
| /* monitor mode supported */
298 IEEE80211_C_TXPMGT
| /* tx power management */
299 IEEE80211_C_SHPREAMBLE
| /* short preamble supported */
300 IEEE80211_C_SHSLOT
| /* short slot time supported */
301 IEEE80211_C_WPA
| /* 802.11i */
302 IEEE80211_C_WME
; /* 802.11e */
304 /* read MAC address from EEPROM */
305 val
= iwi_read_prom_word(sc
, IWI_EEPROM_MAC
+ 0);
306 ic
->ic_myaddr
[0] = val
& 0xff;
307 ic
->ic_myaddr
[1] = val
>> 8;
308 val
= iwi_read_prom_word(sc
, IWI_EEPROM_MAC
+ 1);
309 ic
->ic_myaddr
[2] = val
& 0xff;
310 ic
->ic_myaddr
[3] = val
>> 8;
311 val
= iwi_read_prom_word(sc
, IWI_EEPROM_MAC
+ 2);
312 ic
->ic_myaddr
[4] = val
& 0xff;
313 ic
->ic_myaddr
[5] = val
>> 8;
315 aprint_verbose_dev(self
, "802.11 address %s\n",
316 ether_sprintf(ic
->ic_myaddr
));
318 /* read the NIC type from EEPROM */
319 val
= iwi_read_prom_word(sc
, IWI_EEPROM_NIC_TYPE
);
320 sc
->nictype
= val
& 0xff;
322 DPRINTF(("%s: NIC type %d\n", device_xname(self
), sc
->nictype
));
324 if (PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1
||
325 PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2
) {
326 /* set supported .11a rates (2915ABG only) */
327 ic
->ic_sup_rates
[IEEE80211_MODE_11A
] = iwi_rateset_11a
;
329 /* set supported .11a channels */
330 for (i
= 36; i
<= 64; i
+= 4) {
331 ic
->ic_channels
[i
].ic_freq
=
332 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
333 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
335 for (i
= 149; i
<= 165; i
+= 4) {
336 ic
->ic_channels
[i
].ic_freq
=
337 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
338 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
342 /* set supported .11b and .11g rates */
343 ic
->ic_sup_rates
[IEEE80211_MODE_11B
] = iwi_rateset_11b
;
344 ic
->ic_sup_rates
[IEEE80211_MODE_11G
] = iwi_rateset_11g
;
346 /* set supported .11b and .11g channels (1 through 14) */
347 for (i
= 1; i
<= 14; i
++) {
348 ic
->ic_channels
[i
].ic_freq
=
349 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_2GHZ
);
350 ic
->ic_channels
[i
].ic_flags
=
351 IEEE80211_CHAN_CCK
| IEEE80211_CHAN_OFDM
|
352 IEEE80211_CHAN_DYN
| IEEE80211_CHAN_2GHZ
;
356 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
357 ifp
->if_init
= iwi_init
;
358 ifp
->if_stop
= iwi_stop
;
359 ifp
->if_ioctl
= iwi_ioctl
;
360 ifp
->if_start
= iwi_start
;
361 ifp
->if_watchdog
= iwi_watchdog
;
362 IFQ_SET_READY(&ifp
->if_snd
);
363 memcpy(ifp
->if_xname
, device_xname(self
), IFNAMSIZ
);
366 ieee80211_ifattach(ic
);
367 /* override default methods */
368 ic
->ic_node_alloc
= iwi_node_alloc
;
369 sc
->sc_node_free
= ic
->ic_node_free
;
370 ic
->ic_node_free
= iwi_node_free
;
371 /* override state transition machine */
372 sc
->sc_newstate
= ic
->ic_newstate
;
373 ic
->ic_newstate
= iwi_newstate
;
374 ieee80211_media_init(ic
, iwi_media_change
, iwi_media_status
);
379 if (iwi_alloc_cmd_ring(sc
, &sc
->cmdq
, IWI_CMD_RING_COUNT
) != 0) {
380 aprint_error_dev(self
, "could not allocate command ring\n");
384 error
= iwi_alloc_tx_ring(sc
, &sc
->txq
[0], IWI_TX_RING_COUNT
,
385 IWI_CSR_TX1_RIDX
, IWI_CSR_TX1_WIDX
);
387 aprint_error_dev(self
, "could not allocate Tx ring 1\n");
391 error
= iwi_alloc_tx_ring(sc
, &sc
->txq
[1], IWI_TX_RING_COUNT
,
392 IWI_CSR_TX2_RIDX
, IWI_CSR_TX2_WIDX
);
394 aprint_error_dev(self
, "could not allocate Tx ring 2\n");
398 error
= iwi_alloc_tx_ring(sc
, &sc
->txq
[2], IWI_TX_RING_COUNT
,
399 IWI_CSR_TX3_RIDX
, IWI_CSR_TX3_WIDX
);
401 aprint_error_dev(self
, "could not allocate Tx ring 3\n");
405 error
= iwi_alloc_tx_ring(sc
, &sc
->txq
[3], IWI_TX_RING_COUNT
,
406 IWI_CSR_TX4_RIDX
, IWI_CSR_TX4_WIDX
);
408 aprint_error_dev(self
, "could not allocate Tx ring 4\n");
412 if (iwi_alloc_rx_ring(sc
, &sc
->rxq
, IWI_RX_RING_COUNT
) != 0) {
413 aprint_error_dev(self
, "could not allocate Rx ring\n");
418 bpfattach2(ifp
, DLT_IEEE802_11_RADIO
,
419 sizeof (struct ieee80211_frame
) + 64, &sc
->sc_drvbpf
);
421 sc
->sc_rxtap_len
= sizeof sc
->sc_rxtapu
;
422 sc
->sc_rxtap
.wr_ihdr
.it_len
= htole16(sc
->sc_rxtap_len
);
423 sc
->sc_rxtap
.wr_ihdr
.it_present
= htole32(IWI_RX_RADIOTAP_PRESENT
);
425 sc
->sc_txtap_len
= sizeof sc
->sc_txtapu
;
426 sc
->sc_txtap
.wt_ihdr
.it_len
= htole16(sc
->sc_txtap_len
);
427 sc
->sc_txtap
.wt_ihdr
.it_present
= htole32(IWI_TX_RADIOTAP_PRESENT
);
430 iwi_sysctlattach(sc
);
432 if (pmf_device_register(self
, NULL
, NULL
))
433 pmf_class_network_register(self
, ifp
);
435 aprint_error_dev(self
, "couldn't establish power handler\n");
437 ieee80211_announce(ic
);
441 fail
: iwi_detach(self
, 0);
445 iwi_detach(device_t self
, int flags
)
447 struct iwi_softc
*sc
= device_private(self
);
448 struct ifnet
*ifp
= &sc
->sc_if
;
450 pmf_device_deregister(self
);
455 iwi_free_firmware(sc
);
457 ieee80211_ifdetach(&sc
->sc_ic
);
461 iwi_free_cmd_ring(sc
, &sc
->cmdq
);
462 iwi_free_tx_ring(sc
, &sc
->txq
[0]);
463 iwi_free_tx_ring(sc
, &sc
->txq
[1]);
464 iwi_free_tx_ring(sc
, &sc
->txq
[2]);
465 iwi_free_tx_ring(sc
, &sc
->txq
[3]);
466 iwi_free_rx_ring(sc
, &sc
->rxq
);
468 if (sc
->sc_ih
!= NULL
) {
469 pci_intr_disestablish(sc
->sc_pct
, sc
->sc_ih
);
473 bus_space_unmap(sc
->sc_st
, sc
->sc_sh
, sc
->sc_sz
);
479 iwi_alloc_cmd_ring(struct iwi_softc
*sc
, struct iwi_cmd_ring
*ring
,
486 ring
->cur
= ring
->next
= 0;
489 * Allocate and map command ring
491 error
= bus_dmamap_create(sc
->sc_dmat
,
492 IWI_CMD_DESC_SIZE
* count
, 1,
493 IWI_CMD_DESC_SIZE
* count
, 0,
494 BUS_DMA_NOWAIT
, &ring
->desc_map
);
496 aprint_error_dev(sc
->sc_dev
,
497 "could not create command ring DMA map\n");
498 ring
->desc_map
= NULL
;
502 error
= bus_dmamem_alloc(sc
->sc_dmat
,
503 IWI_CMD_DESC_SIZE
* count
, PAGE_SIZE
, 0,
504 &sc
->cmdq
.desc_seg
, 1, &nsegs
, BUS_DMA_NOWAIT
);
506 aprint_error_dev(sc
->sc_dev
,
507 "could not allocate command ring DMA memory\n");
511 error
= bus_dmamem_map(sc
->sc_dmat
, &sc
->cmdq
.desc_seg
, nsegs
,
512 IWI_CMD_DESC_SIZE
* count
,
513 (void **)&sc
->cmdq
.desc
, BUS_DMA_NOWAIT
);
515 aprint_error_dev(sc
->sc_dev
,
516 "could not map command ring DMA memory\n");
520 error
= bus_dmamap_load(sc
->sc_dmat
, sc
->cmdq
.desc_map
, sc
->cmdq
.desc
,
521 IWI_CMD_DESC_SIZE
* count
, NULL
,
524 aprint_error_dev(sc
->sc_dev
,
525 "could not load command ring DMA map\n");
529 memset(sc
->cmdq
.desc
, 0,
530 IWI_CMD_DESC_SIZE
* count
);
538 iwi_reset_cmd_ring(struct iwi_softc
*sc
, struct iwi_cmd_ring
*ring
)
542 for (i
= ring
->next
; i
!= ring
->cur
;) {
543 bus_dmamap_sync(sc
->sc_dmat
, sc
->cmdq
.desc_map
,
544 i
* IWI_CMD_DESC_SIZE
, IWI_CMD_DESC_SIZE
,
545 BUS_DMASYNC_POSTWRITE
);
547 wakeup(&ring
->desc
[i
]);
548 i
= (i
+ 1) % ring
->count
;
552 ring
->cur
= ring
->next
= 0;
556 iwi_free_cmd_ring(struct iwi_softc
*sc
, struct iwi_cmd_ring
*ring
)
558 if (ring
->desc_map
!= NULL
) {
559 if (ring
->desc
!= NULL
) {
560 bus_dmamap_unload(sc
->sc_dmat
, ring
->desc_map
);
561 bus_dmamem_unmap(sc
->sc_dmat
, (void *)ring
->desc
,
562 IWI_CMD_DESC_SIZE
* ring
->count
);
563 bus_dmamem_free(sc
->sc_dmat
, &ring
->desc_seg
, 1);
565 bus_dmamap_destroy(sc
->sc_dmat
, ring
->desc_map
);
570 iwi_alloc_tx_ring(struct iwi_softc
*sc
, struct iwi_tx_ring
*ring
,
571 int count
, bus_size_t csr_ridx
, bus_size_t csr_widx
)
577 ring
->cur
= ring
->next
= 0;
578 ring
->csr_ridx
= csr_ridx
;
579 ring
->csr_widx
= csr_widx
;
582 * Allocate and map Tx ring
584 error
= bus_dmamap_create(sc
->sc_dmat
,
585 IWI_TX_DESC_SIZE
* count
, 1,
586 IWI_TX_DESC_SIZE
* count
, 0, BUS_DMA_NOWAIT
,
589 aprint_error_dev(sc
->sc_dev
,
590 "could not create tx ring DMA map\n");
591 ring
->desc_map
= NULL
;
595 error
= bus_dmamem_alloc(sc
->sc_dmat
,
596 IWI_TX_DESC_SIZE
* count
, PAGE_SIZE
, 0,
597 &ring
->desc_seg
, 1, &nsegs
, BUS_DMA_NOWAIT
);
599 aprint_error_dev(sc
->sc_dev
,
600 "could not allocate tx ring DMA memory\n");
604 error
= bus_dmamem_map(sc
->sc_dmat
, &ring
->desc_seg
, nsegs
,
605 IWI_TX_DESC_SIZE
* count
,
606 (void **)&ring
->desc
, BUS_DMA_NOWAIT
);
608 aprint_error_dev(sc
->sc_dev
,
609 "could not map tx ring DMA memory\n");
613 error
= bus_dmamap_load(sc
->sc_dmat
, ring
->desc_map
, ring
->desc
,
614 IWI_TX_DESC_SIZE
* count
, NULL
,
617 aprint_error_dev(sc
->sc_dev
,
618 "could not load tx ring DMA map\n");
622 memset(ring
->desc
, 0, IWI_TX_DESC_SIZE
* count
);
624 ring
->data
= malloc(count
* sizeof (struct iwi_tx_data
), M_DEVBUF
,
626 if (ring
->data
== NULL
) {
627 aprint_error_dev(sc
->sc_dev
, "could not allocate soft data\n");
634 * Allocate Tx buffers DMA maps
636 for (i
= 0; i
< count
; i
++) {
637 error
= bus_dmamap_create(sc
->sc_dmat
, MCLBYTES
, IWI_MAX_NSEG
,
638 MCLBYTES
, 0, BUS_DMA_NOWAIT
, &ring
->data
[i
].map
);
640 aprint_error_dev(sc
->sc_dev
,
641 "could not create tx buf DMA map");
642 ring
->data
[i
].map
= NULL
;
652 iwi_reset_tx_ring(struct iwi_softc
*sc
, struct iwi_tx_ring
*ring
)
654 struct iwi_tx_data
*data
;
657 for (i
= 0; i
< ring
->count
; i
++) {
658 data
= &ring
->data
[i
];
660 if (data
->m
!= NULL
) {
665 if (data
->map
!= NULL
) {
666 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
667 data
->map
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
668 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
671 if (data
->ni
!= NULL
) {
672 ieee80211_free_node(data
->ni
);
678 ring
->cur
= ring
->next
= 0;
682 iwi_free_tx_ring(struct iwi_softc
*sc
, struct iwi_tx_ring
*ring
)
685 struct iwi_tx_data
*data
;
687 if (ring
->desc_map
!= NULL
) {
688 if (ring
->desc
!= NULL
) {
689 bus_dmamap_unload(sc
->sc_dmat
, ring
->desc_map
);
690 bus_dmamem_unmap(sc
->sc_dmat
, (void *)ring
->desc
,
691 IWI_TX_DESC_SIZE
* ring
->count
);
692 bus_dmamem_free(sc
->sc_dmat
, &ring
->desc_seg
, 1);
694 bus_dmamap_destroy(sc
->sc_dmat
, ring
->desc_map
);
697 for (i
= 0; i
< ring
->count
; i
++) {
698 data
= &ring
->data
[i
];
700 if (data
->m
!= NULL
) {
704 if (data
->map
!= NULL
) {
705 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
706 bus_dmamap_destroy(sc
->sc_dmat
, data
->map
);
712 iwi_alloc_rx_ring(struct iwi_softc
*sc
, struct iwi_rx_ring
*ring
, int count
)
719 ring
->data
= malloc(count
* sizeof (struct iwi_rx_data
), M_DEVBUF
,
721 if (ring
->data
== NULL
) {
722 aprint_error_dev(sc
->sc_dev
, "could not allocate soft data\n");
730 * Allocate and map Rx buffers
732 for (i
= 0; i
< count
; i
++) {
734 error
= bus_dmamap_create(sc
->sc_dmat
, MCLBYTES
, 1, MCLBYTES
,
735 0, BUS_DMA_WAITOK
| BUS_DMA_ALLOCNOW
, &ring
->data
[i
].map
);
737 aprint_error_dev(sc
->sc_dev
,
738 "could not create rx buf DMA map");
739 ring
->data
[i
].map
= NULL
;
743 if ((ring
->data
[i
].m
= iwi_alloc_rx_buf(sc
)) == NULL
) {
748 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, ring
->data
[i
].map
,
749 ring
->data
[i
].m
, BUS_DMA_READ
| BUS_DMA_NOWAIT
);
751 aprint_error_dev(sc
->sc_dev
,
752 "could not load rx buffer DMA map\n");
756 bus_dmamap_sync(sc
->sc_dmat
, ring
->data
[i
].map
, 0,
757 ring
->data
[i
].map
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
766 iwi_reset_rx_ring(struct iwi_softc
*sc
, struct iwi_rx_ring
*ring
)
772 iwi_free_rx_ring(struct iwi_softc
*sc
, struct iwi_rx_ring
*ring
)
775 struct iwi_rx_data
*data
;
777 for (i
= 0; i
< ring
->count
; i
++) {
778 data
= &ring
->data
[i
];
780 if (data
->m
!= NULL
) {
784 if (data
->map
!= NULL
) {
785 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
786 bus_dmamap_destroy(sc
->sc_dmat
, data
->map
);
792 static struct ieee80211_node
*
793 iwi_node_alloc(struct ieee80211_node_table
*nt
)
797 in
= malloc(sizeof (struct iwi_node
), M_80211_NODE
, M_NOWAIT
| M_ZERO
);
807 iwi_alloc_unr(struct iwi_softc
*sc
)
811 for (i
= 0; i
< IWI_MAX_IBSSNODE
- 1; i
++)
812 if ((sc
->sc_unr
& (1 << i
)) == 0) {
813 sc
->sc_unr
|= 1 << i
;
821 iwi_free_unr(struct iwi_softc
*sc
, int r
)
824 sc
->sc_unr
&= 1 << r
;
828 iwi_node_free(struct ieee80211_node
*ni
)
830 struct ieee80211com
*ic
= ni
->ni_ic
;
831 struct iwi_softc
*sc
= ic
->ic_ifp
->if_softc
;
832 struct iwi_node
*in
= (struct iwi_node
*)ni
;
834 if (in
->in_station
!= -1)
835 iwi_free_unr(sc
, in
->in_station
);
837 sc
->sc_node_free(ni
);
841 iwi_media_change(struct ifnet
*ifp
)
845 error
= ieee80211_media_change(ifp
);
846 if (error
!= ENETRESET
)
849 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) == (IFF_UP
| IFF_RUNNING
))
856 * Convert h/w rate code to IEEE rate code.
859 iwi_cvtrate(int iwirate
)
862 case IWI_RATE_DS1
: return 2;
863 case IWI_RATE_DS2
: return 4;
864 case IWI_RATE_DS5
: return 11;
865 case IWI_RATE_DS11
: return 22;
866 case IWI_RATE_OFDM6
: return 12;
867 case IWI_RATE_OFDM9
: return 18;
868 case IWI_RATE_OFDM12
: return 24;
869 case IWI_RATE_OFDM18
: return 36;
870 case IWI_RATE_OFDM24
: return 48;
871 case IWI_RATE_OFDM36
: return 72;
872 case IWI_RATE_OFDM48
: return 96;
873 case IWI_RATE_OFDM54
: return 108;
879 * The firmware automatically adapts the transmit speed. We report its current
883 iwi_media_status(struct ifnet
*ifp
, struct ifmediareq
*imr
)
885 struct iwi_softc
*sc
= ifp
->if_softc
;
886 struct ieee80211com
*ic
= &sc
->sc_ic
;
889 imr
->ifm_status
= IFM_AVALID
;
890 imr
->ifm_active
= IFM_IEEE80211
;
891 if (ic
->ic_state
== IEEE80211_S_RUN
)
892 imr
->ifm_status
|= IFM_ACTIVE
;
894 /* read current transmission rate from adapter */
895 rate
= iwi_cvtrate(CSR_READ_4(sc
, IWI_CSR_CURRENT_TX_RATE
));
896 imr
->ifm_active
|= ieee80211_rate2media(ic
, rate
, ic
->ic_curmode
);
898 switch (ic
->ic_opmode
) {
899 case IEEE80211_M_STA
:
902 case IEEE80211_M_IBSS
:
903 imr
->ifm_active
|= IFM_IEEE80211_ADHOC
;
906 case IEEE80211_M_MONITOR
:
907 imr
->ifm_active
|= IFM_IEEE80211_MONITOR
;
910 case IEEE80211_M_AHDEMO
:
911 case IEEE80211_M_HOSTAP
:
912 /* should not get there */
918 iwi_newstate(struct ieee80211com
*ic
, enum ieee80211_state nstate
, int arg
)
920 struct iwi_softc
*sc
= ic
->ic_ifp
->if_softc
;
922 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__
,
923 ieee80211_state_name
[ic
->ic_state
],
924 ieee80211_state_name
[nstate
], sc
->flags
));
927 case IEEE80211_S_SCAN
:
928 if (sc
->flags
& IWI_FLAG_SCANNING
)
931 ieee80211_node_table_reset(&ic
->ic_scan
);
932 ic
->ic_flags
|= IEEE80211_F_SCAN
| IEEE80211_F_ASCAN
;
933 sc
->flags
|= IWI_FLAG_SCANNING
;
934 /* blink the led while scanning */
935 iwi_led_set(sc
, IWI_LED_ASSOCIATED
, 1);
939 case IEEE80211_S_AUTH
:
940 iwi_auth_and_assoc(sc
);
943 case IEEE80211_S_RUN
:
944 if (ic
->ic_opmode
== IEEE80211_M_IBSS
)
945 ieee80211_new_state(ic
, IEEE80211_S_AUTH
, -1);
946 else if (ic
->ic_opmode
== IEEE80211_M_MONITOR
)
947 iwi_set_chan(sc
, ic
->ic_ibss_chan
);
949 return (*sc
->sc_newstate
)(ic
, nstate
,
950 IEEE80211_FC0_SUBTYPE_ASSOC_RESP
);
952 case IEEE80211_S_ASSOC
:
953 iwi_led_set(sc
, IWI_LED_ASSOCIATED
, 0);
956 case IEEE80211_S_INIT
:
957 sc
->flags
&= ~IWI_FLAG_SCANNING
;
958 return (*sc
->sc_newstate
)(ic
, nstate
, arg
);
961 ic
->ic_state
= nstate
;
966 * WME parameters coming from IEEE 802.11e specification. These values are
967 * already declared in ieee80211_proto.c, but they are static so they can't
970 static const struct wmeParams iwi_wme_cck_params
[WME_NUM_AC
] = {
971 { 0, 3, 5, 7, 0, 0, }, /* WME_AC_BE */
972 { 0, 3, 5, 10, 0, 0, }, /* WME_AC_BK */
973 { 0, 2, 4, 5, 188, 0, }, /* WME_AC_VI */
974 { 0, 2, 3, 4, 102, 0, }, /* WME_AC_VO */
977 static const struct wmeParams iwi_wme_ofdm_params
[WME_NUM_AC
] = {
978 { 0, 3, 4, 6, 0, 0, }, /* WME_AC_BE */
979 { 0, 3, 4, 10, 0, 0, }, /* WME_AC_BK */
980 { 0, 2, 3, 4, 94, 0, }, /* WME_AC_VI */
981 { 0, 2, 2, 3, 47, 0, }, /* WME_AC_VO */
985 iwi_wme_update(struct ieee80211com
*ic
)
987 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
988 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
989 struct iwi_softc
*sc
= ic
->ic_ifp
->if_softc
;
990 struct iwi_wme_params wme
[3];
991 const struct wmeParams
*wmep
;
995 * We shall not override firmware default WME values if WME is not
998 if (!(ic
->ic_flags
& IEEE80211_F_WME
))
1001 for (ac
= 0; ac
< WME_NUM_AC
; ac
++) {
1002 /* set WME values for current operating mode */
1003 wmep
= &ic
->ic_wme
.wme_chanParams
.cap_wmeParams
[ac
];
1004 wme
[0].aifsn
[ac
] = wmep
->wmep_aifsn
;
1005 wme
[0].cwmin
[ac
] = IWI_EXP2(wmep
->wmep_logcwmin
);
1006 wme
[0].cwmax
[ac
] = IWI_EXP2(wmep
->wmep_logcwmax
);
1007 wme
[0].burst
[ac
] = IWI_USEC(wmep
->wmep_txopLimit
);
1008 wme
[0].acm
[ac
] = wmep
->wmep_acm
;
1010 /* set WME values for CCK modulation */
1011 wmep
= &iwi_wme_cck_params
[ac
];
1012 wme
[1].aifsn
[ac
] = wmep
->wmep_aifsn
;
1013 wme
[1].cwmin
[ac
] = IWI_EXP2(wmep
->wmep_logcwmin
);
1014 wme
[1].cwmax
[ac
] = IWI_EXP2(wmep
->wmep_logcwmax
);
1015 wme
[1].burst
[ac
] = IWI_USEC(wmep
->wmep_txopLimit
);
1016 wme
[1].acm
[ac
] = wmep
->wmep_acm
;
1018 /* set WME values for OFDM modulation */
1019 wmep
= &iwi_wme_ofdm_params
[ac
];
1020 wme
[2].aifsn
[ac
] = wmep
->wmep_aifsn
;
1021 wme
[2].cwmin
[ac
] = IWI_EXP2(wmep
->wmep_logcwmin
);
1022 wme
[2].cwmax
[ac
] = IWI_EXP2(wmep
->wmep_logcwmax
);
1023 wme
[2].burst
[ac
] = IWI_USEC(wmep
->wmep_txopLimit
);
1024 wme
[2].acm
[ac
] = wmep
->wmep_acm
;
1027 DPRINTF(("Setting WME parameters\n"));
1028 return iwi_cmd(sc
, IWI_CMD_SET_WME_PARAMS
, wme
, sizeof wme
, 1);
1034 * Read 16 bits at address 'addr' from the serial EEPROM.
1037 iwi_read_prom_word(struct iwi_softc
*sc
, uint8_t addr
)
1043 /* Clock C once before the first command */
1044 IWI_EEPROM_CTL(sc
, 0);
1045 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1046 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_C
);
1047 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1049 /* Write start bit (1) */
1050 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_D
);
1051 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_D
| IWI_EEPROM_C
);
1053 /* Write READ opcode (10) */
1054 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_D
);
1055 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_D
| IWI_EEPROM_C
);
1056 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1057 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_C
);
1059 /* Write address A7-A0 */
1060 for (n
= 7; n
>= 0; n
--) {
1061 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
|
1062 (((addr
>> n
) & 1) << IWI_EEPROM_SHIFT_D
));
1063 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
|
1064 (((addr
>> n
) & 1) << IWI_EEPROM_SHIFT_D
) | IWI_EEPROM_C
);
1067 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1069 /* Read data Q15-Q0 */
1071 for (n
= 15; n
>= 0; n
--) {
1072 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
| IWI_EEPROM_C
);
1073 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1074 tmp
= MEM_READ_4(sc
, IWI_MEM_EEPROM_CTL
);
1075 val
|= ((tmp
& IWI_EEPROM_Q
) >> IWI_EEPROM_SHIFT_Q
) << n
;
1078 IWI_EEPROM_CTL(sc
, 0);
1080 /* Clear Chip Select and clock C */
1081 IWI_EEPROM_CTL(sc
, IWI_EEPROM_S
);
1082 IWI_EEPROM_CTL(sc
, 0);
1083 IWI_EEPROM_CTL(sc
, IWI_EEPROM_C
);
1089 * XXX: Hack to set the current channel to the value advertised in beacons or
1090 * probe responses. Only used during AP detection.
1093 iwi_fix_channel(struct ieee80211com
*ic
, struct mbuf
*m
)
1095 struct ieee80211_frame
*wh
;
1097 uint8_t *frm
, *efrm
;
1099 wh
= mtod(m
, struct ieee80211_frame
*);
1101 if ((wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
) != IEEE80211_FC0_TYPE_MGT
)
1104 subtype
= wh
->i_fc
[0] & IEEE80211_FC0_SUBTYPE_MASK
;
1106 if (subtype
!= IEEE80211_FC0_SUBTYPE_BEACON
&&
1107 subtype
!= IEEE80211_FC0_SUBTYPE_PROBE_RESP
)
1110 frm
= (uint8_t *)(wh
+ 1);
1111 efrm
= mtod(m
, uint8_t *) + m
->m_len
;
1113 frm
+= 12; /* skip tstamp, bintval and capinfo fields */
1114 while (frm
< efrm
) {
1115 if (*frm
== IEEE80211_ELEMID_DSPARMS
)
1116 #if IEEE80211_CHAN_MAX < 255
1117 if (frm
[2] <= IEEE80211_CHAN_MAX
)
1119 ic
->ic_curchan
= &ic
->ic_channels
[frm
[2]];
1125 static struct mbuf
*
1126 iwi_alloc_rx_buf(struct iwi_softc
*sc
)
1130 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1132 aprint_error_dev(sc
->sc_dev
, "could not allocate rx mbuf\n");
1136 MCLGET(m
, M_DONTWAIT
);
1137 if (!(m
->m_flags
& M_EXT
)) {
1138 aprint_error_dev(sc
->sc_dev
,
1139 "could not allocate rx mbuf cluster\n");
1144 m
->m_pkthdr
.len
= m
->m_len
= m
->m_ext
.ext_size
;
1149 iwi_frame_intr(struct iwi_softc
*sc
, struct iwi_rx_data
*data
, int i
,
1150 struct iwi_frame
*frame
)
1152 struct ieee80211com
*ic
= &sc
->sc_ic
;
1153 struct ifnet
*ifp
= ic
->ic_ifp
;
1154 struct mbuf
*m
, *m_new
;
1155 struct ieee80211_frame
*wh
;
1156 struct ieee80211_node
*ni
;
1159 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
1160 le16toh(frame
->len
), frame
->chan
, frame
->rssi_dbm
));
1162 if (le16toh(frame
->len
) < sizeof (struct ieee80211_frame
) ||
1163 le16toh(frame
->len
) > MCLBYTES
) {
1164 DPRINTF(("%s: bad frame length\n", device_xname(sc
->sc_dev
)));
1170 * Try to allocate a new mbuf for this ring element and
1171 * load it before processing the current mbuf. If the ring
1172 * element cannot be reloaded, drop the received packet
1173 * and reuse the old mbuf. In the unlikely case that
1174 * the old mbuf can't be reloaded either, explicitly panic.
1176 * XXX Reorganize buffer by moving elements from the logical
1177 * end of the ring to the front instead of dropping.
1179 if ((m_new
= iwi_alloc_rx_buf(sc
)) == NULL
) {
1184 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
1186 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, data
->map
, m_new
,
1187 BUS_DMA_READ
| BUS_DMA_NOWAIT
);
1189 aprint_error_dev(sc
->sc_dev
,
1190 "could not load rx buf DMA map\n");
1193 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, data
->map
,
1194 data
->m
, BUS_DMA_READ
| BUS_DMA_NOWAIT
);
1196 panic("%s: unable to remap rx buf",
1197 device_xname(sc
->sc_dev
));
1202 * New mbuf successfully loaded, update RX ring and continue
1207 CSR_WRITE_4(sc
, IWI_CSR_RX_BASE
+ i
* 4, data
->map
->dm_segs
[0].ds_addr
);
1210 m
->m_pkthdr
.rcvif
= ifp
;
1211 m
->m_pkthdr
.len
= m
->m_len
= sizeof (struct iwi_hdr
) +
1212 sizeof (struct iwi_frame
) + le16toh(frame
->len
);
1214 m_adj(m
, sizeof (struct iwi_hdr
) + sizeof (struct iwi_frame
));
1216 if (ic
->ic_state
== IEEE80211_S_SCAN
)
1217 iwi_fix_channel(ic
, m
);
1220 if (sc
->sc_drvbpf
!= NULL
) {
1221 struct iwi_rx_radiotap_header
*tap
= &sc
->sc_rxtap
;
1224 tap
->wr_rate
= iwi_cvtrate(frame
->rate
);
1226 htole16(ic
->ic_channels
[frame
->chan
].ic_freq
);
1227 tap
->wr_chan_flags
=
1228 htole16(ic
->ic_channels
[frame
->chan
].ic_flags
);
1229 tap
->wr_antsignal
= frame
->signal
;
1230 tap
->wr_antenna
= frame
->antenna
;
1232 bpf_mtap2(sc
->sc_drvbpf
, tap
, sc
->sc_rxtap_len
, m
);
1235 wh
= mtod(m
, struct ieee80211_frame
*);
1236 ni
= ieee80211_find_rxnode(ic
, (struct ieee80211_frame_min
*)wh
);
1238 /* Send the frame to the upper layer */
1239 ieee80211_input(ic
, m
, ni
, frame
->rssi_dbm
, 0);
1241 /* node is no longer needed */
1242 ieee80211_free_node(ni
);
1246 iwi_notification_intr(struct iwi_softc
*sc
, struct iwi_notif
*notif
)
1248 struct ieee80211com
*ic
= &sc
->sc_ic
;
1249 struct iwi_notif_scan_channel
*chan
;
1250 struct iwi_notif_scan_complete
*scan
;
1251 struct iwi_notif_authentication
*auth
;
1252 struct iwi_notif_association
*assoc
;
1253 struct iwi_notif_beacon_state
*beacon
;
1255 switch (notif
->type
) {
1256 case IWI_NOTIF_TYPE_SCAN_CHANNEL
:
1257 chan
= (struct iwi_notif_scan_channel
*)(notif
+ 1);
1259 DPRINTFN(2, ("Scan of channel %u complete (%u)\n",
1260 ic
->ic_channels
[chan
->nchan
].ic_freq
, chan
->nchan
));
1263 case IWI_NOTIF_TYPE_SCAN_COMPLETE
:
1264 scan
= (struct iwi_notif_scan_complete
*)(notif
+ 1);
1266 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan
->nchan
,
1269 /* monitor mode uses scan to set the channel ... */
1270 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
1271 sc
->flags
&= ~IWI_FLAG_SCANNING
;
1272 ieee80211_end_scan(ic
);
1274 iwi_set_chan(sc
, ic
->ic_ibss_chan
);
1277 case IWI_NOTIF_TYPE_AUTHENTICATION
:
1278 auth
= (struct iwi_notif_authentication
*)(notif
+ 1);
1280 DPRINTFN(2, ("Authentication (%u)\n", auth
->state
));
1282 switch (auth
->state
) {
1283 case IWI_AUTH_SUCCESS
:
1284 ieee80211_node_authorize(ic
->ic_bss
);
1285 ieee80211_new_state(ic
, IEEE80211_S_ASSOC
, -1);
1292 aprint_error_dev(sc
->sc_dev
,
1293 "unknown authentication state %u\n", auth
->state
);
1297 case IWI_NOTIF_TYPE_ASSOCIATION
:
1298 assoc
= (struct iwi_notif_association
*)(notif
+ 1);
1300 DPRINTFN(2, ("Association (%u, %u)\n", assoc
->state
,
1303 switch (assoc
->state
) {
1304 case IWI_AUTH_SUCCESS
:
1305 /* re-association, do nothing */
1308 case IWI_ASSOC_SUCCESS
:
1309 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
1312 case IWI_ASSOC_FAIL
:
1313 ieee80211_begin_scan(ic
, 1);
1317 aprint_error_dev(sc
->sc_dev
,
1318 "unknown association state %u\n", assoc
->state
);
1322 case IWI_NOTIF_TYPE_BEACON
:
1323 beacon
= (struct iwi_notif_beacon_state
*)(notif
+ 1);
1325 if (beacon
->state
== IWI_BEACON_MISS
) {
1326 DPRINTFN(5, ("%s: %u beacon(s) missed\n",
1327 device_xname(sc
->sc_dev
), le32toh(beacon
->number
)));
1331 case IWI_NOTIF_TYPE_FRAG_LENGTH
:
1332 case IWI_NOTIF_TYPE_LINK_QUALITY
:
1333 case IWI_NOTIF_TYPE_TGI_TX_KEY
:
1334 case IWI_NOTIF_TYPE_CALIBRATION
:
1335 case IWI_NOTIF_TYPE_NOISE
:
1336 DPRINTFN(5, ("Notification (%u)\n", notif
->type
));
1340 DPRINTF(("%s: unknown notification type %u flags 0x%x len %d\n",
1341 device_xname(sc
->sc_dev
), notif
->type
, notif
->flags
,
1342 le16toh(notif
->len
)));
1347 iwi_cmd_intr(struct iwi_softc
*sc
)
1351 hw
= CSR_READ_4(sc
, IWI_CSR_CMD_RIDX
);
1353 bus_dmamap_sync(sc
->sc_dmat
, sc
->cmdq
.desc_map
,
1354 sc
->cmdq
.next
* IWI_CMD_DESC_SIZE
, IWI_CMD_DESC_SIZE
,
1355 BUS_DMASYNC_POSTWRITE
);
1357 wakeup(&sc
->cmdq
.desc
[sc
->cmdq
.next
]);
1359 sc
->cmdq
.next
= (sc
->cmdq
.next
+ 1) % sc
->cmdq
.count
;
1361 if (--sc
->cmdq
.queued
> 0) {
1362 CSR_WRITE_4(sc
, IWI_CSR_CMD_WIDX
, (sc
->cmdq
.next
+ 1) % sc
->cmdq
.count
);
1367 iwi_rx_intr(struct iwi_softc
*sc
)
1369 struct iwi_rx_data
*data
;
1370 struct iwi_hdr
*hdr
;
1373 hw
= CSR_READ_4(sc
, IWI_CSR_RX_RIDX
);
1375 for (; sc
->rxq
.cur
!= hw
;) {
1376 data
= &sc
->rxq
.data
[sc
->rxq
.cur
];
1378 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
1379 data
->map
->dm_mapsize
, BUS_DMASYNC_POSTREAD
);
1381 hdr
= mtod(data
->m
, struct iwi_hdr
*);
1383 switch (hdr
->type
) {
1384 case IWI_HDR_TYPE_FRAME
:
1385 iwi_frame_intr(sc
, data
, sc
->rxq
.cur
,
1386 (struct iwi_frame
*)(hdr
+ 1));
1389 case IWI_HDR_TYPE_NOTIF
:
1390 iwi_notification_intr(sc
,
1391 (struct iwi_notif
*)(hdr
+ 1));
1395 aprint_error_dev(sc
->sc_dev
, "unknown hdr type %u\n",
1399 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
1400 data
->map
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
1402 DPRINTFN(15, ("rx done idx=%u\n", sc
->rxq
.cur
));
1404 sc
->rxq
.cur
= (sc
->rxq
.cur
+ 1) % sc
->rxq
.count
;
1407 /* Tell the firmware what we have processed */
1408 hw
= (hw
== 0) ? sc
->rxq
.count
- 1 : hw
- 1;
1409 CSR_WRITE_4(sc
, IWI_CSR_RX_WIDX
, hw
);
1413 iwi_tx_intr(struct iwi_softc
*sc
, struct iwi_tx_ring
*txq
)
1415 struct ifnet
*ifp
= &sc
->sc_if
;
1416 struct iwi_tx_data
*data
;
1419 hw
= CSR_READ_4(sc
, txq
->csr_ridx
);
1421 for (; txq
->next
!= hw
;) {
1422 data
= &txq
->data
[txq
->next
];
1424 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
1425 data
->map
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
1426 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
1429 ieee80211_free_node(data
->ni
);
1432 DPRINTFN(15, ("tx done idx=%u\n", txq
->next
));
1437 txq
->next
= (txq
->next
+ 1) % txq
->count
;
1440 sc
->sc_tx_timer
= 0;
1441 ifp
->if_flags
&= ~IFF_OACTIVE
;
1443 /* Call start() since some buffer descriptors have been released */
1444 (*ifp
->if_start
)(ifp
);
1450 struct iwi_softc
*sc
= arg
;
1453 if ((r
= CSR_READ_4(sc
, IWI_CSR_INTR
)) == 0 || r
== 0xffffffff)
1456 /* Acknowledge interrupts */
1457 CSR_WRITE_4(sc
, IWI_CSR_INTR
, r
);
1459 if (r
& IWI_INTR_FATAL_ERROR
) {
1460 aprint_error_dev(sc
->sc_dev
, "fatal error\n");
1461 sc
->sc_ic
.ic_ifp
->if_flags
&= ~IFF_UP
;
1462 iwi_stop(&sc
->sc_if
, 1);
1466 if (r
& IWI_INTR_FW_INITED
) {
1467 if (!(r
& (IWI_INTR_FATAL_ERROR
| IWI_INTR_PARITY_ERROR
)))
1471 if (r
& IWI_INTR_RADIO_OFF
) {
1472 DPRINTF(("radio transmitter off\n"));
1473 sc
->sc_ic
.ic_ifp
->if_flags
&= ~IFF_UP
;
1474 iwi_stop(&sc
->sc_if
, 1);
1478 if (r
& IWI_INTR_CMD_DONE
)
1481 if (r
& IWI_INTR_TX1_DONE
)
1482 iwi_tx_intr(sc
, &sc
->txq
[0]);
1484 if (r
& IWI_INTR_TX2_DONE
)
1485 iwi_tx_intr(sc
, &sc
->txq
[1]);
1487 if (r
& IWI_INTR_TX3_DONE
)
1488 iwi_tx_intr(sc
, &sc
->txq
[2]);
1490 if (r
& IWI_INTR_TX4_DONE
)
1491 iwi_tx_intr(sc
, &sc
->txq
[3]);
1493 if (r
& IWI_INTR_RX_DONE
)
1496 if (r
& IWI_INTR_PARITY_ERROR
)
1497 aprint_error_dev(sc
->sc_dev
, "parity error\n");
1503 iwi_cmd(struct iwi_softc
*sc
, uint8_t type
, void *data
, uint8_t len
,
1506 struct iwi_cmd_desc
*desc
;
1508 desc
= &sc
->cmdq
.desc
[sc
->cmdq
.cur
];
1510 desc
->hdr
.type
= IWI_HDR_TYPE_COMMAND
;
1511 desc
->hdr
.flags
= IWI_HDR_FLAG_IRQ
;
1514 memcpy(desc
->data
, data
, len
);
1516 bus_dmamap_sync(sc
->sc_dmat
, sc
->cmdq
.desc_map
,
1517 sc
->cmdq
.cur
* IWI_CMD_DESC_SIZE
,
1518 IWI_CMD_DESC_SIZE
, BUS_DMASYNC_PREWRITE
);
1520 DPRINTFN(2, ("sending command idx=%u type=%u len=%u async=%d\n",
1521 sc
->cmdq
.cur
, type
, len
, async
));
1523 sc
->cmdq
.cur
= (sc
->cmdq
.cur
+ 1) % sc
->cmdq
.count
;
1525 if (++sc
->cmdq
.queued
== 1)
1526 CSR_WRITE_4(sc
, IWI_CSR_CMD_WIDX
, sc
->cmdq
.cur
);
1528 return async
? 0 : tsleep(desc
, 0, "iwicmd", hz
);
1532 iwi_write_ibssnode(struct iwi_softc
*sc
, const struct iwi_node
*in
)
1534 struct iwi_ibssnode node
;
1536 /* write node information into NIC memory */
1537 memset(&node
, 0, sizeof node
);
1538 IEEE80211_ADDR_COPY(node
.bssid
, in
->in_node
.ni_macaddr
);
1540 CSR_WRITE_REGION_1(sc
,
1541 IWI_CSR_NODE_BASE
+ in
->in_station
* sizeof node
,
1542 (uint8_t *)&node
, sizeof node
);
1546 iwi_tx_start(struct ifnet
*ifp
, struct mbuf
*m0
, struct ieee80211_node
*ni
,
1549 struct iwi_softc
*sc
= ifp
->if_softc
;
1550 struct ieee80211com
*ic
= &sc
->sc_ic
;
1551 struct iwi_node
*in
= (struct iwi_node
*)ni
;
1552 struct ieee80211_frame
*wh
;
1553 struct ieee80211_key
*k
;
1554 const struct chanAccParams
*cap
;
1555 struct iwi_tx_ring
*txq
= &sc
->txq
[ac
];
1556 struct iwi_tx_data
*data
;
1557 struct iwi_tx_desc
*desc
;
1559 int error
, hdrlen
, i
, noack
= 0;
1561 wh
= mtod(m0
, struct ieee80211_frame
*);
1563 if (wh
->i_fc
[0] & IEEE80211_FC0_SUBTYPE_QOS
) {
1564 hdrlen
= sizeof (struct ieee80211_qosframe
);
1565 cap
= &ic
->ic_wme
.wme_chanParams
;
1566 noack
= cap
->cap_wmeParams
[ac
].wmep_noackPolicy
;
1568 hdrlen
= sizeof (struct ieee80211_frame
);
1571 * This is only used in IBSS mode where the firmware expect an index
1572 * in a h/w table instead of a destination address.
1574 if (ic
->ic_opmode
== IEEE80211_M_IBSS
&& in
->in_station
== -1) {
1575 in
->in_station
= iwi_alloc_unr(sc
);
1577 if (in
->in_station
== -1) { /* h/w table is full */
1579 ieee80211_free_node(ni
);
1583 iwi_write_ibssnode(sc
, in
);
1586 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
) {
1587 k
= ieee80211_crypto_encap(ic
, ni
, m0
);
1593 /* packet header may have moved, reset our local pointer */
1594 wh
= mtod(m0
, struct ieee80211_frame
*);
1598 if (sc
->sc_drvbpf
!= NULL
) {
1599 struct iwi_tx_radiotap_header
*tap
= &sc
->sc_txtap
;
1602 tap
->wt_chan_freq
= htole16(ic
->ic_ibss_chan
->ic_freq
);
1603 tap
->wt_chan_flags
= htole16(ic
->ic_ibss_chan
->ic_flags
);
1605 bpf_mtap2(sc
->sc_drvbpf
, tap
, sc
->sc_txtap_len
, m0
);
1609 data
= &txq
->data
[txq
->cur
];
1610 desc
= &txq
->desc
[txq
->cur
];
1612 /* save and trim IEEE802.11 header */
1613 m_copydata(m0
, 0, hdrlen
, (void *)&desc
->wh
);
1616 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, data
->map
, m0
,
1617 BUS_DMA_WRITE
| BUS_DMA_NOWAIT
);
1618 if (error
!= 0 && error
!= EFBIG
) {
1619 aprint_error_dev(sc
->sc_dev
, "could not map mbuf (error %d)\n",
1625 /* too many fragments, linearize */
1627 MGETHDR(mnew
, M_DONTWAIT
, MT_DATA
);
1633 M_COPY_PKTHDR(mnew
, m0
);
1635 /* If the data won't fit in the header, get a cluster */
1636 if (m0
->m_pkthdr
.len
> MHLEN
) {
1637 MCLGET(mnew
, M_DONTWAIT
);
1638 if (!(mnew
->m_flags
& M_EXT
)) {
1644 m_copydata(m0
, 0, m0
->m_pkthdr
.len
, mtod(mnew
, void *));
1646 mnew
->m_len
= mnew
->m_pkthdr
.len
;
1649 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, data
->map
, m0
,
1650 BUS_DMA_WRITE
| BUS_DMA_NOWAIT
);
1652 aprint_error_dev(sc
->sc_dev
,
1653 "could not map mbuf (error %d)\n", error
);
1662 desc
->hdr
.type
= IWI_HDR_TYPE_DATA
;
1663 desc
->hdr
.flags
= IWI_HDR_FLAG_IRQ
;
1665 (ic
->ic_opmode
== IEEE80211_M_IBSS
) ? in
->in_station
: 0;
1666 desc
->cmd
= IWI_DATA_CMD_TX
;
1667 desc
->len
= htole16(m0
->m_pkthdr
.len
);
1671 if (!noack
&& !IEEE80211_IS_MULTICAST(desc
->wh
.i_addr1
))
1672 desc
->flags
|= IWI_DATA_FLAG_NEED_ACK
;
1675 if (ic
->ic_flags
& IEEE80211_F_PRIVACY
) {
1676 desc
->wh
.i_fc
[1] |= IEEE80211_FC1_WEP
;
1677 desc
->wep_txkey
= ic
->ic_crypto
.cs_def_txkey
;
1680 desc
->flags
|= IWI_DATA_FLAG_NO_WEP
;
1682 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
1683 desc
->flags
|= IWI_DATA_FLAG_SHPREAMBLE
;
1685 if (desc
->wh
.i_fc
[0] & IEEE80211_FC0_SUBTYPE_QOS
)
1686 desc
->xflags
|= IWI_DATA_XFLAG_QOS
;
1688 if (ic
->ic_curmode
== IEEE80211_MODE_11B
)
1689 desc
->xflags
|= IWI_DATA_XFLAG_CCK
;
1691 desc
->nseg
= htole32(data
->map
->dm_nsegs
);
1692 for (i
= 0; i
< data
->map
->dm_nsegs
; i
++) {
1693 desc
->seg_addr
[i
] = htole32(data
->map
->dm_segs
[i
].ds_addr
);
1694 desc
->seg_len
[i
] = htole16(data
->map
->dm_segs
[i
].ds_len
);
1697 bus_dmamap_sync(sc
->sc_dmat
, txq
->desc_map
,
1698 txq
->cur
* IWI_TX_DESC_SIZE
,
1699 IWI_TX_DESC_SIZE
, BUS_DMASYNC_PREWRITE
);
1701 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0, data
->map
->dm_mapsize
,
1702 BUS_DMASYNC_PREWRITE
);
1704 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1705 ac
, txq
->cur
, le16toh(desc
->len
), le32toh(desc
->nseg
)));
1707 /* Inform firmware about this new packet */
1709 txq
->cur
= (txq
->cur
+ 1) % txq
->count
;
1710 CSR_WRITE_4(sc
, txq
->csr_widx
, txq
->cur
);
1716 iwi_start(struct ifnet
*ifp
)
1718 struct iwi_softc
*sc
= ifp
->if_softc
;
1719 struct ieee80211com
*ic
= &sc
->sc_ic
;
1721 struct ether_header
*eh
;
1722 struct ieee80211_node
*ni
;
1725 if (ic
->ic_state
!= IEEE80211_S_RUN
)
1729 IF_DEQUEUE(&ifp
->if_snd
, m0
);
1733 if (m0
->m_len
< sizeof (struct ether_header
) &&
1734 (m0
= m_pullup(m0
, sizeof (struct ether_header
))) == NULL
) {
1739 eh
= mtod(m0
, struct ether_header
*);
1740 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1747 /* classify mbuf so we can find which tx ring to use */
1748 if (ieee80211_classify(ic
, m0
, ni
) != 0) {
1750 ieee80211_free_node(ni
);
1755 /* no QoS encapsulation for EAPOL frames */
1756 ac
= (eh
->ether_type
!= htons(ETHERTYPE_PAE
)) ?
1757 M_WME_GETAC(m0
) : WME_AC_BE
;
1759 if (sc
->txq
[ac
].queued
> sc
->txq
[ac
].count
- 8) {
1760 /* there is no place left in this ring */
1761 IF_PREPEND(&ifp
->if_snd
, m0
);
1762 ifp
->if_flags
|= IFF_OACTIVE
;
1767 if (ifp
->if_bpf
!= NULL
)
1768 bpf_mtap(ifp
->if_bpf
, m0
);
1771 m0
= ieee80211_encap(ic
, m0
, ni
);
1773 ieee80211_free_node(ni
);
1779 if (ic
->ic_rawbpf
!= NULL
)
1780 bpf_mtap(ic
->ic_rawbpf
, m0
);
1783 if (iwi_tx_start(ifp
, m0
, ni
, ac
) != 0) {
1784 ieee80211_free_node(ni
);
1789 /* start watchdog timer */
1790 sc
->sc_tx_timer
= 5;
1796 iwi_watchdog(struct ifnet
*ifp
)
1798 struct iwi_softc
*sc
= ifp
->if_softc
;
1802 if (sc
->sc_tx_timer
> 0) {
1803 if (--sc
->sc_tx_timer
== 0) {
1804 aprint_error_dev(sc
->sc_dev
, "device timeout\n");
1806 ifp
->if_flags
&= ~IFF_UP
;
1813 ieee80211_watchdog(&sc
->sc_ic
);
1817 iwi_get_table0(struct iwi_softc
*sc
, uint32_t *tbl
)
1819 uint32_t size
, buf
[128];
1821 if (!(sc
->flags
& IWI_FLAG_FW_INITED
)) {
1822 memset(buf
, 0, sizeof buf
);
1823 return copyout(buf
, tbl
, sizeof buf
);
1826 size
= min(CSR_READ_4(sc
, IWI_CSR_TABLE0_SIZE
), 128 - 1);
1827 CSR_READ_REGION_4(sc
, IWI_CSR_TABLE0_BASE
, &buf
[1], size
);
1829 return copyout(buf
, tbl
, sizeof buf
);
1833 iwi_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
1835 #define IS_RUNNING(ifp) \
1836 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
1838 struct iwi_softc
*sc
= ifp
->if_softc
;
1839 struct ieee80211com
*ic
= &sc
->sc_ic
;
1840 struct ifreq
*ifr
= (struct ifreq
*)data
;
1848 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
1850 if (ifp
->if_flags
& IFF_UP
) {
1851 if (!(ifp
->if_flags
& IFF_RUNNING
))
1854 if (ifp
->if_flags
& IFF_RUNNING
)
1861 /* XXX no h/w multicast filter? --dyoung */
1862 if ((error
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
1863 /* setup multicast filter, etc */
1869 error
= iwi_get_table0(sc
, (uint32_t *)ifr
->ifr_data
);
1873 val
= !iwi_getrfkill(sc
);
1874 error
= copyout(&val
, (int *)ifr
->ifr_data
, sizeof val
);
1878 if (ifr
->ifr_media
& IFM_IEEE80211_ADHOC
) {
1879 sc
->sc_fwname
= "ipw2200-ibss.fw";
1880 } else if (ifr
->ifr_media
& IFM_IEEE80211_MONITOR
) {
1881 sc
->sc_fwname
= "ipw2200-sniffer.fw";
1883 sc
->sc_fwname
= "ipw2200-bss.fw";
1885 error
= iwi_cache_firmware(sc
);
1891 error
= ieee80211_ioctl(&sc
->sc_ic
, cmd
, data
);
1893 if (error
== ENETRESET
) {
1894 if (IS_RUNNING(ifp
) &&
1895 (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
))
1907 iwi_stop_master(struct iwi_softc
*sc
)
1911 /* Disable interrupts */
1912 CSR_WRITE_4(sc
, IWI_CSR_INTR_MASK
, 0);
1914 CSR_WRITE_4(sc
, IWI_CSR_RST
, IWI_RST_STOP_MASTER
);
1915 for (ntries
= 0; ntries
< 5; ntries
++) {
1916 if (CSR_READ_4(sc
, IWI_CSR_RST
) & IWI_RST_MASTER_DISABLED
)
1921 aprint_error_dev(sc
->sc_dev
, "timeout waiting for master\n");
1923 CSR_WRITE_4(sc
, IWI_CSR_RST
, CSR_READ_4(sc
, IWI_CSR_RST
) |
1924 IWI_RST_PRINCETON_RESET
);
1926 sc
->flags
&= ~IWI_FLAG_FW_INITED
;
1930 iwi_reset(struct iwi_softc
*sc
)
1934 iwi_stop_master(sc
);
1936 /* Move adapter to D0 state */
1937 CSR_WRITE_4(sc
, IWI_CSR_CTL
, CSR_READ_4(sc
, IWI_CSR_CTL
) |
1940 /* Initialize Phase-Locked Level (PLL) */
1941 CSR_WRITE_4(sc
, IWI_CSR_READ_INT
, IWI_READ_INT_INIT_HOST
);
1943 /* Wait for clock stabilization */
1944 for (ntries
= 0; ntries
< 1000; ntries
++) {
1945 if (CSR_READ_4(sc
, IWI_CSR_CTL
) & IWI_CTL_CLOCK_READY
)
1949 if (ntries
== 1000) {
1950 aprint_error_dev(sc
->sc_dev
,
1951 "timeout waiting for clock stabilization\n");
1955 CSR_WRITE_4(sc
, IWI_CSR_RST
, CSR_READ_4(sc
, IWI_CSR_RST
) |
1960 CSR_WRITE_4(sc
, IWI_CSR_CTL
, CSR_READ_4(sc
, IWI_CSR_CTL
) |
1963 /* Clear NIC memory */
1964 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_ADDR
, 0);
1965 for (i
= 0; i
< 0xc000; i
++)
1966 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, 0);
1972 iwi_load_ucode(struct iwi_softc
*sc
, void *uc
, int size
)
1977 CSR_WRITE_4(sc
, IWI_CSR_RST
, CSR_READ_4(sc
, IWI_CSR_RST
) |
1978 IWI_RST_STOP_MASTER
);
1979 for (ntries
= 0; ntries
< 5; ntries
++) {
1980 if (CSR_READ_4(sc
, IWI_CSR_RST
) & IWI_RST_MASTER_DISABLED
)
1985 aprint_error_dev(sc
->sc_dev
, "timeout waiting for master\n");
1989 MEM_WRITE_4(sc
, 0x3000e0, 0x80000000);
1991 CSR_WRITE_4(sc
, IWI_CSR_RST
, CSR_READ_4(sc
, IWI_CSR_RST
) &
1992 ~IWI_RST_PRINCETON_RESET
);
1994 MEM_WRITE_4(sc
, 0x3000e0, 0);
1996 MEM_WRITE_4(sc
, 0x300004, 1);
1998 MEM_WRITE_4(sc
, 0x300004, 0);
2000 MEM_WRITE_1(sc
, 0x200000, 0x00);
2001 MEM_WRITE_1(sc
, 0x200000, 0x40);
2004 /* Adapter is buggy, we must set the address for each word */
2005 for (w
= uc
; size
> 0; w
++, size
-= 2)
2006 MEM_WRITE_2(sc
, 0x200010, htole16(*w
));
2008 MEM_WRITE_1(sc
, 0x200000, 0x00);
2009 MEM_WRITE_1(sc
, 0x200000, 0x80);
2011 /* Wait until we get a response in the uc queue */
2012 for (ntries
= 0; ntries
< 100; ntries
++) {
2013 if (MEM_READ_1(sc
, 0x200000) & 1)
2017 if (ntries
== 100) {
2018 aprint_error_dev(sc
->sc_dev
,
2019 "timeout waiting for ucode to initialize\n");
2023 /* Empty the uc queue or the firmware will not initialize properly */
2024 for (i
= 0; i
< 7; i
++)
2025 MEM_READ_4(sc
, 0x200004);
2027 MEM_WRITE_1(sc
, 0x200000, 0x00);
2032 /* macro to handle unaligned little endian data in firmware image */
2033 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2035 iwi_load_firmware(struct iwi_softc
*sc
, void *fw
, int size
)
2039 uint32_t sentinel
, ctl
, sum
;
2040 uint32_t cs
, sl
, cd
, cl
;
2041 int ntries
, nsegs
, error
;
2044 nsegs
= atop((vaddr_t
)fw
+size
-1) - atop((vaddr_t
)fw
) + 1;
2046 /* Create a DMA map for the firmware image */
2047 error
= bus_dmamap_create(sc
->sc_dmat
, size
, nsegs
, size
, 0,
2048 BUS_DMA_NOWAIT
, &map
);
2050 aprint_error_dev(sc
->sc_dev
,
2051 "could not create firmware DMA map\n");
2056 error
= bus_dmamap_load(sc
->sc_dmat
, map
, fw
, size
, NULL
,
2057 BUS_DMA_NOWAIT
| BUS_DMA_WRITE
);
2059 aprint_error_dev(sc
->sc_dev
, "could not load fw dma map(%d)\n",
2064 /* Make sure the adapter will get up-to-date values */
2065 bus_dmamap_sync(sc
->sc_dmat
, map
, 0, size
, BUS_DMASYNC_PREWRITE
);
2067 /* Tell the adapter where the command blocks are stored */
2068 MEM_WRITE_4(sc
, 0x3000a0, 0x27000);
2071 * Store command blocks into adapter's internal memory using register
2072 * indirections. The adapter will read the firmware image through DMA
2073 * using information stored in command blocks.
2077 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_ADDR
, 0x27000);
2084 cs
= map
->dm_segs
[sn
].ds_addr
;
2085 sl
= map
->dm_segs
[sn
].ds_len
;
2089 cd
= GETLE32(p
); p
+= 4; cs
+= 4; sl
-= 4;
2090 cl
= GETLE32(p
); p
+= 4; cs
+= 4; sl
-= 4;
2092 while (sl
> 0 && cl
> 0) {
2093 int len
= min(cl
, sl
);
2100 int mlen
= min(len
, IWI_CB_MAXDATALEN
);
2102 ctl
= IWI_CB_DEFAULT_CTL
| mlen
;
2103 sum
= ctl
^ cs
^ cd
;
2105 /* Write a command block */
2106 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, ctl
);
2107 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, cs
);
2108 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, cd
);
2109 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, sum
);
2118 /* Write a fictive final command block (sentinel) */
2119 sentinel
= CSR_READ_4(sc
, IWI_CSR_AUTOINC_ADDR
);
2120 CSR_WRITE_4(sc
, IWI_CSR_AUTOINC_DATA
, 0);
2122 CSR_WRITE_4(sc
, IWI_CSR_RST
, CSR_READ_4(sc
, IWI_CSR_RST
) &
2123 ~(IWI_RST_MASTER_DISABLED
| IWI_RST_STOP_MASTER
));
2125 /* Tell the adapter to start processing command blocks */
2126 MEM_WRITE_4(sc
, 0x3000a4, 0x540100);
2128 /* Wait until the adapter has processed all command blocks */
2129 for (ntries
= 0; ntries
< 400; ntries
++) {
2130 if (MEM_READ_4(sc
, 0x3000d0) >= sentinel
)
2134 if (ntries
== 400) {
2135 aprint_error_dev(sc
->sc_dev
, "timeout processing cb\n");
2140 /* We're done with command blocks processing */
2141 MEM_WRITE_4(sc
, 0x3000a4, 0x540c00);
2143 /* Allow interrupts so we know when the firmware is inited */
2144 CSR_WRITE_4(sc
, IWI_CSR_INTR_MASK
, IWI_INTR_MASK
);
2146 /* Tell the adapter to initialize the firmware */
2147 CSR_WRITE_4(sc
, IWI_CSR_RST
, 0);
2148 CSR_WRITE_4(sc
, IWI_CSR_CTL
, CSR_READ_4(sc
, IWI_CSR_CTL
) |
2149 IWI_CTL_ALLOW_STANDBY
);
2151 /* Wait at most one second for firmware initialization to complete */
2152 if ((error
= tsleep(sc
, 0, "iwiinit", hz
)) != 0) {
2153 aprint_error_dev(sc
->sc_dev
,
2154 "timeout waiting for firmware initialization to complete\n");
2159 bus_dmamap_sync(sc
->sc_dmat
, map
, 0, size
, BUS_DMASYNC_POSTWRITE
);
2160 bus_dmamap_unload(sc
->sc_dmat
, map
);
2163 bus_dmamap_destroy(sc
->sc_dmat
, map
);
2170 * Store firmware into kernel memory so we can download it when we need to,
2171 * e.g when the adapter wakes up from suspend mode.
2174 iwi_cache_firmware(struct iwi_softc
*sc
)
2176 struct iwi_firmware
*kfw
= &sc
->fw
;
2177 firmware_handle_t fwh
;
2178 const struct iwi_firmware_hdr
*hdr
;
2183 if (iwi_accept_eula
== 0) {
2184 aprint_error_dev(sc
->sc_dev
,
2185 "EULA not accepted; please see the iwi(4) man page.\n");
2189 iwi_free_firmware(sc
);
2190 error
= firmware_open("if_iwi", sc
->sc_fwname
, &fwh
);
2192 aprint_error_dev(sc
->sc_dev
, "firmware_open failed\n");
2196 size
= firmware_get_size(fwh
);
2197 if (size
< sizeof(struct iwi_firmware_hdr
)) {
2198 aprint_error_dev(sc
->sc_dev
, "image '%s' has no header\n",
2204 sc
->sc_blob
= firmware_malloc(size
);
2205 if (sc
->sc_blob
== NULL
) {
2207 firmware_close(fwh
);
2211 error
= firmware_read(fwh
, 0, sc
->sc_blob
, size
);
2212 firmware_close(fwh
);
2217 hdr
= (const struct iwi_firmware_hdr
*)sc
->sc_blob
;
2218 if (size
< sizeof(struct iwi_firmware_hdr
) + hdr
->bsize
+ hdr
->usize
+ hdr
->fsize
) {
2219 aprint_error_dev(sc
->sc_dev
, "image '%s' too small\n",
2225 hdr
= (const struct iwi_firmware_hdr
*)sc
->sc_blob
;
2226 DPRINTF(("firmware version = %d\n", le32toh(hdr
->version
)));
2227 if ((IWI_FW_GET_MAJOR(le32toh(hdr
->version
)) != IWI_FW_REQ_MAJOR
) ||
2228 (IWI_FW_GET_MINOR(le32toh(hdr
->version
)) != IWI_FW_REQ_MINOR
)) {
2229 aprint_error_dev(sc
->sc_dev
,
2230 "version for '%s' %d.%d != %d.%d\n", sc
->sc_fwname
,
2231 IWI_FW_GET_MAJOR(le32toh(hdr
->version
)),
2232 IWI_FW_GET_MINOR(le32toh(hdr
->version
)),
2233 IWI_FW_REQ_MAJOR
, IWI_FW_REQ_MINOR
);
2238 kfw
->boot_size
= hdr
->bsize
;
2239 kfw
->ucode_size
= hdr
->usize
;
2240 kfw
->main_size
= hdr
->fsize
;
2242 fw
= sc
->sc_blob
+ sizeof(struct iwi_firmware_hdr
);
2244 fw
+= kfw
->boot_size
;
2246 fw
+= kfw
->ucode_size
;
2249 DPRINTF(("Firmware cached: boot %p, ucode %p, main %p\n",
2250 kfw
->boot
, kfw
->ucode
, kfw
->main
));
2251 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
2252 kfw
->boot_size
, kfw
->ucode_size
, kfw
->main_size
));
2254 sc
->flags
|= IWI_FLAG_FW_CACHED
;
2259 fail2
: firmware_free(sc
->sc_blob
, 0);
2265 iwi_free_firmware(struct iwi_softc
*sc
)
2268 if (!(sc
->flags
& IWI_FLAG_FW_CACHED
))
2271 firmware_free(sc
->sc_blob
, 0);
2273 sc
->flags
&= ~IWI_FLAG_FW_CACHED
;
2277 iwi_config(struct iwi_softc
*sc
)
2279 struct ieee80211com
*ic
= &sc
->sc_ic
;
2280 struct ifnet
*ifp
= &sc
->sc_if
;
2281 struct iwi_configuration config
;
2282 struct iwi_rateset rs
;
2283 struct iwi_txpower power
;
2284 struct ieee80211_key
*wk
;
2285 struct iwi_wep_key wepkey
;
2287 int error
, nchan
, i
;
2289 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, CLLADDR(ifp
->if_sadl
));
2290 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic
->ic_myaddr
)));
2291 error
= iwi_cmd(sc
, IWI_CMD_SET_MAC_ADDRESS
, ic
->ic_myaddr
,
2292 IEEE80211_ADDR_LEN
, 0);
2296 memset(&config
, 0, sizeof config
);
2297 config
.bluetooth_coexistence
= sc
->bluetooth
;
2298 config
.antenna
= sc
->antenna
;
2299 config
.silence_threshold
= 0x1e;
2300 config
.multicast_enabled
= 1;
2301 config
.answer_pbreq
= (ic
->ic_opmode
== IEEE80211_M_IBSS
) ? 1 : 0;
2302 config
.disable_unicast_decryption
= 1;
2303 config
.disable_multicast_decryption
= 1;
2304 DPRINTF(("Configuring adapter\n"));
2305 error
= iwi_cmd(sc
, IWI_CMD_SET_CONFIGURATION
, &config
, sizeof config
,
2310 data
= htole32(IWI_POWER_MODE_CAM
);
2311 DPRINTF(("Setting power mode to %u\n", le32toh(data
)));
2312 error
= iwi_cmd(sc
, IWI_CMD_SET_POWER_MODE
, &data
, sizeof data
, 0);
2316 data
= htole32(ic
->ic_rtsthreshold
);
2317 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data
)));
2318 error
= iwi_cmd(sc
, IWI_CMD_SET_RTS_THRESHOLD
, &data
, sizeof data
, 0);
2322 data
= htole32(ic
->ic_fragthreshold
);
2323 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data
)));
2324 error
= iwi_cmd(sc
, IWI_CMD_SET_FRAG_THRESHOLD
, &data
, sizeof data
, 0);
2329 * Set default Tx power for 802.11b/g and 802.11a channels.
2332 for (i
= 0; i
<= IEEE80211_CHAN_MAX
; i
++) {
2333 if (!IEEE80211_IS_CHAN_2GHZ(&ic
->ic_channels
[i
]))
2335 power
.chan
[nchan
].chan
= i
;
2336 power
.chan
[nchan
].power
= IWI_TXPOWER_MAX
;
2339 power
.nchan
= nchan
;
2341 power
.mode
= IWI_MODE_11G
;
2342 DPRINTF(("Setting .11g channels tx power\n"));
2343 error
= iwi_cmd(sc
, IWI_CMD_SET_TX_POWER
, &power
, sizeof power
, 0);
2347 power
.mode
= IWI_MODE_11B
;
2348 DPRINTF(("Setting .11b channels tx power\n"));
2349 error
= iwi_cmd(sc
, IWI_CMD_SET_TX_POWER
, &power
, sizeof power
, 0);
2354 for (i
= 0; i
<= IEEE80211_CHAN_MAX
; i
++) {
2355 if (!IEEE80211_IS_CHAN_5GHZ(&ic
->ic_channels
[i
]))
2357 power
.chan
[nchan
].chan
= i
;
2358 power
.chan
[nchan
].power
= IWI_TXPOWER_MAX
;
2361 power
.nchan
= nchan
;
2363 if (nchan
> 0) { /* 2915ABG only */
2364 power
.mode
= IWI_MODE_11A
;
2365 DPRINTF(("Setting .11a channels tx power\n"));
2366 error
= iwi_cmd(sc
, IWI_CMD_SET_TX_POWER
, &power
, sizeof power
,
2372 rs
.mode
= IWI_MODE_11G
;
2373 rs
.type
= IWI_RATESET_TYPE_SUPPORTED
;
2374 rs
.nrates
= ic
->ic_sup_rates
[IEEE80211_MODE_11G
].rs_nrates
;
2375 memcpy(rs
.rates
, ic
->ic_sup_rates
[IEEE80211_MODE_11G
].rs_rates
,
2377 DPRINTF(("Setting .11bg supported rates (%u)\n", rs
.nrates
));
2378 error
= iwi_cmd(sc
, IWI_CMD_SET_RATES
, &rs
, sizeof rs
, 0);
2382 rs
.mode
= IWI_MODE_11A
;
2383 rs
.type
= IWI_RATESET_TYPE_SUPPORTED
;
2384 rs
.nrates
= ic
->ic_sup_rates
[IEEE80211_MODE_11A
].rs_nrates
;
2385 memcpy(rs
.rates
, ic
->ic_sup_rates
[IEEE80211_MODE_11A
].rs_rates
,
2387 DPRINTF(("Setting .11a supported rates (%u)\n", rs
.nrates
));
2388 error
= iwi_cmd(sc
, IWI_CMD_SET_RATES
, &rs
, sizeof rs
, 0);
2392 /* if we have a desired ESSID, set it now */
2393 if (ic
->ic_des_esslen
!= 0) {
2395 if (iwi_debug
> 0) {
2396 printf("Setting desired ESSID to ");
2397 ieee80211_print_essid(ic
->ic_des_essid
,
2402 error
= iwi_cmd(sc
, IWI_CMD_SET_ESSID
, ic
->ic_des_essid
,
2403 ic
->ic_des_esslen
, 0);
2408 data
= htole32(arc4random());
2409 DPRINTF(("Setting initialization vector to %u\n", le32toh(data
)));
2410 error
= iwi_cmd(sc
, IWI_CMD_SET_IV
, &data
, sizeof data
, 0);
2414 if (ic
->ic_flags
& IEEE80211_F_PRIVACY
) {
2415 /* XXX iwi_setwepkeys? */
2416 for (i
= 0; i
< IEEE80211_WEP_NKID
; i
++) {
2417 wk
= &ic
->ic_crypto
.cs_nw_keys
[i
];
2419 wepkey
.cmd
= IWI_WEP_KEY_CMD_SETKEY
;
2421 wepkey
.len
= wk
->wk_keylen
;
2422 memset(wepkey
.key
, 0, sizeof wepkey
.key
);
2423 memcpy(wepkey
.key
, wk
->wk_key
, wk
->wk_keylen
);
2424 DPRINTF(("Setting wep key index %u len %u\n",
2425 wepkey
.idx
, wepkey
.len
));
2426 error
= iwi_cmd(sc
, IWI_CMD_SET_WEP_KEY
, &wepkey
,
2433 /* Enable adapter */
2434 DPRINTF(("Enabling adapter\n"));
2435 return iwi_cmd(sc
, IWI_CMD_ENABLE
, NULL
, 0, 0);
2439 iwi_set_chan(struct iwi_softc
*sc
, struct ieee80211_channel
*chan
)
2441 struct ieee80211com
*ic
= &sc
->sc_ic
;
2442 struct iwi_scan_v2 scan
;
2444 (void)memset(&scan
, 0, sizeof scan
);
2446 scan
.dwelltime
[IWI_SCAN_TYPE_PASSIVE
] = htole16(2000);
2447 scan
.channels
[0] = 1 |
2448 (IEEE80211_IS_CHAN_5GHZ(chan
) ? IWI_CHAN_5GHZ
: IWI_CHAN_2GHZ
);
2449 scan
.channels
[1] = ieee80211_chan2ieee(ic
, chan
);
2450 iwi_scan_type_set(scan
, 1, IWI_SCAN_TYPE_PASSIVE
);
2452 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic
, chan
)));
2453 return iwi_cmd(sc
, IWI_CMD_SCAN_V2
, &scan
, sizeof scan
, 1);
2457 iwi_scan(struct iwi_softc
*sc
)
2459 struct ieee80211com
*ic
= &sc
->sc_ic
;
2460 struct iwi_scan_v2 scan
;
2465 (void)memset(&scan
, 0, sizeof scan
);
2466 scan
.dwelltime
[IWI_SCAN_TYPE_ACTIVE_BROADCAST
] =
2467 htole16(sc
->dwelltime
);
2468 scan
.dwelltime
[IWI_SCAN_TYPE_ACTIVE_BDIRECT
] =
2469 htole16(sc
->dwelltime
);
2471 /* tell the firmware about the desired essid */
2472 if (ic
->ic_des_esslen
) {
2475 DPRINTF(("%s: Setting adapter desired ESSID to %s\n",
2476 __func__
, ic
->ic_des_essid
));
2478 error
= iwi_cmd(sc
, IWI_CMD_SET_ESSID
,
2479 ic
->ic_des_essid
, ic
->ic_des_esslen
, 1);
2483 type
= IWI_SCAN_TYPE_ACTIVE_BDIRECT
;
2485 type
= IWI_SCAN_TYPE_ACTIVE_BROADCAST
;
2488 p
= &scan
.channels
[0];
2490 for (i
= 0; i
<= IEEE80211_CHAN_MAX
; i
++) {
2491 if (IEEE80211_IS_CHAN_5GHZ(&ic
->ic_channels
[i
]) &&
2492 isset(ic
->ic_chan_active
, i
)) {
2496 iwi_scan_type_set(scan
, idx
, type
);
2500 *(p
- count
) = IWI_CHAN_5GHZ
| count
;
2505 for (i
= 0; i
<= IEEE80211_CHAN_MAX
; i
++) {
2506 if (IEEE80211_IS_CHAN_2GHZ(&ic
->ic_channels
[i
]) &&
2507 isset(ic
->ic_chan_active
, i
)) {
2511 iwi_scan_type_set(scan
, idx
, type
);
2514 *(p
- count
) = IWI_CHAN_2GHZ
| count
;
2516 DPRINTF(("Start scanning\n"));
2517 return iwi_cmd(sc
, IWI_CMD_SCAN_V2
, &scan
, sizeof scan
, 1);
2521 iwi_auth_and_assoc(struct iwi_softc
*sc
)
2523 struct ieee80211com
*ic
= &sc
->sc_ic
;
2524 struct ieee80211_node
*ni
= ic
->ic_bss
;
2525 struct ifnet
*ifp
= &sc
->sc_if
;
2526 struct ieee80211_wme_info wme
;
2527 struct iwi_configuration config
;
2528 struct iwi_associate assoc
;
2529 struct iwi_rateset rs
;
2534 memset(&config
, 0, sizeof config
);
2535 config
.bluetooth_coexistence
= sc
->bluetooth
;
2536 config
.antenna
= sc
->antenna
;
2537 config
.multicast_enabled
= 1;
2538 config
.silence_threshold
= 0x1e;
2539 if (ic
->ic_curmode
== IEEE80211_MODE_11G
)
2540 config
.use_protection
= 1;
2541 config
.answer_pbreq
= (ic
->ic_opmode
== IEEE80211_M_IBSS
) ? 1 : 0;
2542 config
.disable_unicast_decryption
= 1;
2543 config
.disable_multicast_decryption
= 1;
2545 DPRINTF(("Configuring adapter\n"));
2546 error
= iwi_cmd(sc
, IWI_CMD_SET_CONFIGURATION
, &config
,
2552 if (iwi_debug
> 0) {
2553 aprint_debug_dev(sc
->sc_dev
, "Setting ESSID to ");
2554 ieee80211_print_essid(ni
->ni_essid
, ni
->ni_esslen
);
2558 error
= iwi_cmd(sc
, IWI_CMD_SET_ESSID
, ni
->ni_essid
, ni
->ni_esslen
, 1);
2562 /* the rate set has already been "negotiated" */
2563 rs
.mode
= IEEE80211_IS_CHAN_5GHZ(ni
->ni_chan
) ? IWI_MODE_11A
:
2565 rs
.type
= IWI_RATESET_TYPE_NEGOTIATED
;
2566 rs
.nrates
= ni
->ni_rates
.rs_nrates
;
2568 if (rs
.nrates
> IWI_RATESET_SIZE
) {
2569 DPRINTF(("Truncating negotiated rate set from %u\n",
2571 rs
.nrates
= IWI_RATESET_SIZE
;
2573 memcpy(rs
.rates
, ni
->ni_rates
.rs_rates
, rs
.nrates
);
2574 DPRINTF(("Setting negotiated rates (%u)\n", rs
.nrates
));
2575 error
= iwi_cmd(sc
, IWI_CMD_SET_RATES
, &rs
, sizeof rs
, 1);
2579 if ((ic
->ic_flags
& IEEE80211_F_WME
) && ni
->ni_wme_ie
!= NULL
) {
2580 wme
.wme_id
= IEEE80211_ELEMID_VENDOR
;
2581 wme
.wme_len
= sizeof (struct ieee80211_wme_info
) - 2;
2582 wme
.wme_oui
[0] = 0x00;
2583 wme
.wme_oui
[1] = 0x50;
2584 wme
.wme_oui
[2] = 0xf2;
2585 wme
.wme_type
= WME_OUI_TYPE
;
2586 wme
.wme_subtype
= WME_INFO_OUI_SUBTYPE
;
2587 wme
.wme_version
= WME_VERSION
;
2590 DPRINTF(("Setting WME IE (len=%u)\n", wme
.wme_len
));
2591 error
= iwi_cmd(sc
, IWI_CMD_SET_WMEIE
, &wme
, sizeof wme
, 1);
2596 if (ic
->ic_opt_ie
!= NULL
) {
2597 DPRINTF(("Setting optional IE (len=%u)\n", ic
->ic_opt_ie_len
));
2598 error
= iwi_cmd(sc
, IWI_CMD_SET_OPTIE
, ic
->ic_opt_ie
,
2599 ic
->ic_opt_ie_len
, 1);
2603 data
= htole32(ni
->ni_rssi
);
2604 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni
->ni_rssi
));
2605 error
= iwi_cmd(sc
, IWI_CMD_SET_SENSITIVITY
, &data
, sizeof data
, 1);
2609 memset(&assoc
, 0, sizeof assoc
);
2610 if (IEEE80211_IS_CHAN_A(ni
->ni_chan
))
2611 assoc
.mode
= IWI_MODE_11A
;
2612 else if (IEEE80211_IS_CHAN_G(ni
->ni_chan
))
2613 assoc
.mode
= IWI_MODE_11G
;
2614 else if (IEEE80211_IS_CHAN_B(ni
->ni_chan
))
2615 assoc
.mode
= IWI_MODE_11B
;
2617 assoc
.chan
= ieee80211_chan2ieee(ic
, ni
->ni_chan
);
2619 if (ni
->ni_authmode
== IEEE80211_AUTH_SHARED
)
2620 assoc
.auth
= (ic
->ic_crypto
.cs_def_txkey
<< 4) | IWI_AUTH_SHARED
;
2622 if (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
)
2623 assoc
.plen
= IWI_ASSOC_SHPREAMBLE
;
2625 if ((ic
->ic_flags
& IEEE80211_F_WME
) && ni
->ni_wme_ie
!= NULL
)
2626 assoc
.policy
|= htole16(IWI_POLICY_WME
);
2627 if (ic
->ic_flags
& IEEE80211_F_WPA
)
2628 assoc
.policy
|= htole16(IWI_POLICY_WPA
);
2629 if (ic
->ic_opmode
== IEEE80211_M_IBSS
&& ni
->ni_tstamp
.tsf
== 0)
2630 assoc
.type
= IWI_HC_IBSS_START
;
2632 assoc
.type
= IWI_HC_ASSOC
;
2633 memcpy(assoc
.tstamp
, ni
->ni_tstamp
.data
, 8);
2635 if (ic
->ic_opmode
== IEEE80211_M_IBSS
)
2636 capinfo
= IEEE80211_CAPINFO_IBSS
;
2638 capinfo
= IEEE80211_CAPINFO_ESS
;
2639 if (ic
->ic_flags
& IEEE80211_F_PRIVACY
)
2640 capinfo
|= IEEE80211_CAPINFO_PRIVACY
;
2641 if ((ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
) &&
2642 IEEE80211_IS_CHAN_2GHZ(ni
->ni_chan
))
2643 capinfo
|= IEEE80211_CAPINFO_SHORT_PREAMBLE
;
2644 if (ic
->ic_flags
& IEEE80211_F_SHSLOT
)
2645 capinfo
|= IEEE80211_CAPINFO_SHORT_SLOTTIME
;
2646 assoc
.capinfo
= htole16(capinfo
);
2648 assoc
.lintval
= htole16(ic
->ic_lintval
);
2649 assoc
.intval
= htole16(ni
->ni_intval
);
2650 IEEE80211_ADDR_COPY(assoc
.bssid
, ni
->ni_bssid
);
2651 if (ic
->ic_opmode
== IEEE80211_M_IBSS
)
2652 IEEE80211_ADDR_COPY(assoc
.dst
, ifp
->if_broadcastaddr
);
2654 IEEE80211_ADDR_COPY(assoc
.dst
, ni
->ni_bssid
);
2656 DPRINTF(("%s bssid %s dst %s channel %u policy 0x%x "
2657 "auth %u capinfo 0x%x lintval %u bintval %u\n",
2658 assoc
.type
== IWI_HC_IBSS_START
? "Start" : "Join",
2659 ether_sprintf(assoc
.bssid
), ether_sprintf(assoc
.dst
),
2660 assoc
.chan
, le16toh(assoc
.policy
), assoc
.auth
,
2661 le16toh(assoc
.capinfo
), le16toh(assoc
.lintval
),
2662 le16toh(assoc
.intval
)));
2664 return iwi_cmd(sc
, IWI_CMD_ASSOCIATE
, &assoc
, sizeof assoc
, 1);
2668 iwi_init(struct ifnet
*ifp
)
2670 struct iwi_softc
*sc
= ifp
->if_softc
;
2671 struct ieee80211com
*ic
= &sc
->sc_ic
;
2672 struct iwi_firmware
*fw
= &sc
->fw
;
2675 /* exit immediately if firmware has not been ioctl'd */
2676 if (!(sc
->flags
& IWI_FLAG_FW_CACHED
)) {
2677 if ((error
= iwi_cache_firmware(sc
)) != 0) {
2678 aprint_error_dev(sc
->sc_dev
,
2679 "could not cache the firmware\n");
2686 if ((error
= iwi_reset(sc
)) != 0) {
2687 aprint_error_dev(sc
->sc_dev
, "could not reset adapter\n");
2691 if ((error
= iwi_load_firmware(sc
, fw
->boot
, fw
->boot_size
)) != 0) {
2692 aprint_error_dev(sc
->sc_dev
, "could not load boot firmware\n");
2696 if ((error
= iwi_load_ucode(sc
, fw
->ucode
, fw
->ucode_size
)) != 0) {
2697 aprint_error_dev(sc
->sc_dev
, "could not load microcode\n");
2701 iwi_stop_master(sc
);
2703 CSR_WRITE_4(sc
, IWI_CSR_CMD_BASE
, sc
->cmdq
.desc_map
->dm_segs
[0].ds_addr
);
2704 CSR_WRITE_4(sc
, IWI_CSR_CMD_SIZE
, sc
->cmdq
.count
);
2705 CSR_WRITE_4(sc
, IWI_CSR_CMD_WIDX
, sc
->cmdq
.cur
);
2707 CSR_WRITE_4(sc
, IWI_CSR_TX1_BASE
, sc
->txq
[0].desc_map
->dm_segs
[0].ds_addr
);
2708 CSR_WRITE_4(sc
, IWI_CSR_TX1_SIZE
, sc
->txq
[0].count
);
2709 CSR_WRITE_4(sc
, IWI_CSR_TX1_WIDX
, sc
->txq
[0].cur
);
2711 CSR_WRITE_4(sc
, IWI_CSR_TX2_BASE
, sc
->txq
[1].desc_map
->dm_segs
[0].ds_addr
);
2712 CSR_WRITE_4(sc
, IWI_CSR_TX2_SIZE
, sc
->txq
[1].count
);
2713 CSR_WRITE_4(sc
, IWI_CSR_TX2_WIDX
, sc
->txq
[1].cur
);
2715 CSR_WRITE_4(sc
, IWI_CSR_TX3_BASE
, sc
->txq
[2].desc_map
->dm_segs
[0].ds_addr
);
2716 CSR_WRITE_4(sc
, IWI_CSR_TX3_SIZE
, sc
->txq
[2].count
);
2717 CSR_WRITE_4(sc
, IWI_CSR_TX3_WIDX
, sc
->txq
[2].cur
);
2719 CSR_WRITE_4(sc
, IWI_CSR_TX4_BASE
, sc
->txq
[3].desc_map
->dm_segs
[0].ds_addr
);
2720 CSR_WRITE_4(sc
, IWI_CSR_TX4_SIZE
, sc
->txq
[3].count
);
2721 CSR_WRITE_4(sc
, IWI_CSR_TX4_WIDX
, sc
->txq
[3].cur
);
2723 for (i
= 0; i
< sc
->rxq
.count
; i
++)
2724 CSR_WRITE_4(sc
, IWI_CSR_RX_BASE
+ i
* 4,
2725 sc
->rxq
.data
[i
].map
->dm_segs
[0].ds_addr
);
2727 CSR_WRITE_4(sc
, IWI_CSR_RX_WIDX
, sc
->rxq
.count
-1);
2729 if ((error
= iwi_load_firmware(sc
, fw
->main
, fw
->main_size
)) != 0) {
2730 aprint_error_dev(sc
->sc_dev
, "could not load main firmware\n");
2734 sc
->flags
|= IWI_FLAG_FW_INITED
;
2736 if ((error
= iwi_config(sc
)) != 0) {
2737 aprint_error_dev(sc
->sc_dev
, "device configuration failed\n");
2741 ic
->ic_state
= IEEE80211_S_INIT
;
2743 ifp
->if_flags
&= ~IFF_OACTIVE
;
2744 ifp
->if_flags
|= IFF_RUNNING
;
2746 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
2747 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
2748 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
2750 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
2754 fail
: ifp
->if_flags
&= ~IFF_UP
;
2762 * Return whether or not the radio is enabled in hardware
2763 * (i.e. the rfkill switch is "off").
2766 iwi_getrfkill(struct iwi_softc
*sc
)
2768 return (CSR_READ_4(sc
, IWI_CSR_IO
) & IWI_IO_RADIO_ENABLED
) == 0;
2772 iwi_sysctl_radio(SYSCTLFN_ARGS
)
2774 struct sysctlnode node
;
2775 struct iwi_softc
*sc
;
2779 sc
= (struct iwi_softc
*)node
.sysctl_data
;
2781 val
= !iwi_getrfkill(sc
);
2783 node
.sysctl_data
= &val
;
2784 error
= sysctl_lookup(SYSCTLFN_CALL(&node
));
2786 if (error
|| newp
== NULL
)
2793 SYSCTL_SETUP(sysctl_iwi
, "sysctl iwi(4) subtree setup")
2796 const struct sysctlnode
*rnode
;
2797 const struct sysctlnode
*cnode
;
2799 if ((rc
= sysctl_createv(clog
, 0, NULL
, &rnode
,
2800 CTLFLAG_PERMANENT
, CTLTYPE_NODE
, "hw", NULL
,
2801 NULL
, 0, NULL
, 0, CTL_HW
, CTL_EOL
)) != 0)
2804 if ((rc
= sysctl_createv(clog
, 0, &rnode
, &rnode
,
2805 CTLFLAG_PERMANENT
, CTLTYPE_NODE
, "iwi",
2806 SYSCTL_DESCR("iwi global controls"),
2807 NULL
, 0, NULL
, 0, CTL_CREATE
, CTL_EOL
)) != 0)
2810 /* control debugging printfs */
2811 if ((rc
= sysctl_createv(clog
, 0, &rnode
, &cnode
,
2812 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
, CTLTYPE_INT
,
2813 "debug", SYSCTL_DESCR("Enable debugging output"),
2814 NULL
, 0, &iwi_debug
, 0, CTL_CREATE
, CTL_EOL
)) != 0)
2819 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__
, rc
);
2822 #endif /* IWI_DEBUG */
2828 iwi_sysctlattach(struct iwi_softc
*sc
)
2831 const struct sysctlnode
*rnode
;
2832 const struct sysctlnode
*cnode
;
2834 struct sysctllog
**clog
= &sc
->sc_sysctllog
;
2836 if ((rc
= sysctl_createv(clog
, 0, NULL
, &rnode
,
2837 CTLFLAG_PERMANENT
, CTLTYPE_NODE
, "hw", NULL
,
2838 NULL
, 0, NULL
, 0, CTL_HW
, CTL_EOL
)) != 0)
2841 if ((rc
= sysctl_createv(clog
, 0, &rnode
, &rnode
,
2842 CTLFLAG_PERMANENT
, CTLTYPE_NODE
, device_xname(sc
->sc_dev
),
2843 SYSCTL_DESCR("iwi controls and statistics"),
2844 NULL
, 0, NULL
, 0, CTL_CREATE
, CTL_EOL
)) != 0)
2847 if ((rc
= sysctl_createv(clog
, 0, &rnode
, &cnode
,
2848 CTLFLAG_PERMANENT
, CTLTYPE_INT
, "radio",
2849 SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"),
2850 iwi_sysctl_radio
, 0, sc
, 0, CTL_CREATE
, CTL_EOL
)) != 0)
2853 sc
->dwelltime
= 100;
2854 if ((rc
= sysctl_createv(clog
, 0, &rnode
, &cnode
,
2855 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
, CTLTYPE_INT
,
2856 "dwell", SYSCTL_DESCR("channel dwell time (ms) for AP/station scanning"),
2857 NULL
, 0, &sc
->dwelltime
, 0, CTL_CREATE
, CTL_EOL
)) != 0)
2861 if ((rc
= sysctl_createv(clog
, 0, &rnode
, &cnode
,
2862 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
, CTLTYPE_INT
,
2863 "bluetooth", SYSCTL_DESCR("bluetooth coexistence"),
2864 NULL
, 0, &sc
->bluetooth
, 0, CTL_CREATE
, CTL_EOL
)) != 0)
2867 sc
->antenna
= IWI_ANTENNA_AUTO
;
2868 if ((rc
= sysctl_createv(clog
, 0, &rnode
, &cnode
,
2869 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
, CTLTYPE_INT
,
2870 "antenna", SYSCTL_DESCR("antenna (0=auto)"),
2871 NULL
, 0, &sc
->antenna
, 0, CTL_CREATE
, CTL_EOL
)) != 0)
2876 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__
, rc
);
2880 iwi_stop(struct ifnet
*ifp
, int disable
)
2882 struct iwi_softc
*sc
= ifp
->if_softc
;
2883 struct ieee80211com
*ic
= &sc
->sc_ic
;
2887 iwi_stop_master(sc
);
2888 CSR_WRITE_4(sc
, IWI_CSR_RST
, IWI_RST_SW_RESET
);
2891 iwi_reset_cmd_ring(sc
, &sc
->cmdq
);
2892 iwi_reset_tx_ring(sc
, &sc
->txq
[0]);
2893 iwi_reset_tx_ring(sc
, &sc
->txq
[1]);
2894 iwi_reset_tx_ring(sc
, &sc
->txq
[2]);
2895 iwi_reset_tx_ring(sc
, &sc
->txq
[3]);
2896 iwi_reset_rx_ring(sc
, &sc
->rxq
);
2899 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);
2901 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
2905 iwi_led_set(struct iwi_softc
*sc
, uint32_t state
, int toggle
)
2909 val
= MEM_READ_4(sc
, IWI_MEM_EVENT_CTL
);
2911 switch (sc
->nictype
) {
2913 /* special NIC type: reversed leds */
2914 if (state
== IWI_LED_ACTIVITY
) {
2915 state
&= ~IWI_LED_ACTIVITY
;
2916 state
|= IWI_LED_ASSOCIATED
;
2917 } else if (state
== IWI_LED_ASSOCIATED
) {
2918 state
&= ~IWI_LED_ASSOCIATED
;
2919 state
|= IWI_LED_ACTIVITY
;
2921 /* and ignore toggle effect */
2928 val
= (toggle
&& (val
& state
)) ? val
& ~state
: val
| state
;
2931 aprint_normal_dev(sc
->sc_dev
, "unknown NIC type %d\n",
2937 MEM_WRITE_4(sc
, IWI_MEM_EVENT_CTL
, val
);
2942 SYSCTL_SETUP(sysctl_hw_iwi_accept_eula_setup
, "sysctl hw.iwi.accept_eula")
2944 const struct sysctlnode
*rnode
;
2945 const struct sysctlnode
*cnode
;
2947 sysctl_createv(NULL
, 0, NULL
, &rnode
,
2955 sysctl_createv(NULL
, 0, &rnode
, &rnode
,
2957 CTLTYPE_NODE
, "iwi",
2961 CTL_CREATE
, CTL_EOL
);
2963 sysctl_createv(NULL
, 0, &rnode
, &cnode
,
2964 CTLFLAG_PERMANENT
| CTLFLAG_READWRITE
,
2965 CTLTYPE_INT
, "accept_eula",
2966 SYSCTL_DESCR("Accept Intel EULA and permit use of iwi(4) firmware"),
2968 &iwi_accept_eula
, sizeof(iwi_accept_eula
),
2969 CTL_CREATE
, CTL_EOL
);