2 * Copyright (c) 2004, 2005
3 * Damien Bergamini <damien.bergamini@free.fr>.
4 * Copyright (c) 2004, 2005
5 * Andrew Atrens <atrens@nortelnetworks.com>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $DragonFly: src/sys/dev/netif/iwi/if_iwi.c,v 1.1 2005/03/06 05:02:02 dillon Exp $
36 #include <sys/cdefs.h>
39 * Intel(R) PRO/Wireless 2200BG/2915ABG driver
40 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
43 #include <sys/param.h>
44 #include <sys/sysctl.h>
45 #include <sys/sockio.h>
47 #include <sys/kernel.h>
48 #include <sys/kthread.h>
49 #include <sys/socket.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
54 #include <sys/endian.h>
56 #include <sys/ucred.h>
57 #include <sys/thread2.h>
59 #include <machine/bus.h>
60 #include <machine/resource.h>
61 #include <machine/clock.h>
64 #include <bus/pci/pcireg.h>
65 #include <bus/pci/pcivar.h>
69 #include <net/if_arp.h>
70 #include <net/ifq_var.h>
71 #include <net/ethernet.h>
72 #include <net/if_dl.h>
73 #include <net/if_media.h>
74 #include <net/if_types.h>
75 #include <net/ifq_var.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/if_ether.h>
84 #include <netproto/ipx/ipx.h>
85 #include <netproto/ipx/ipx_if.h>
88 #include <netproto/802_11/ieee80211_var.h>
89 #include <netproto/802_11/ieee80211_ioctl.h>
90 #include <netproto/802_11/ieee80211_radiotap.h>
91 #include <netproto/802_11/if_wavelan_ieee.h>
93 #include "if_iwireg.h"
94 #include "if_iwivar.h"
97 #define DPRINTF(x) if (sc->debug_level > 0) printf x
98 #define DPRINTFN(n, x) if (sc->debug_level >= (n)) printf x
102 #define DPRINTFN(n, x)
105 MODULE_DEPEND(iwi, pci, 1, 1, 1);
106 MODULE_DEPEND(iwi, wlan, 1, 1, 1);
108 struct iwi_dump_buffer {
118 static const struct iwi_ident iwi_ident_table[] = {
119 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG MiniPCI" },
120 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG MiniPCI" },
121 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG MiniPCI" },
126 static const struct ieee80211_rateset iwi_rateset_11a =
127 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
129 static const struct ieee80211_rateset iwi_rateset_11b =
130 { 4, { 2, 4, 11, 22 } };
132 static const struct ieee80211_rateset iwi_rateset_11g =
133 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
135 static int iwi_dma_alloc(struct iwi_softc *);
136 static void iwi_release(struct iwi_softc *);
137 static int iwi_media_change(struct ifnet *);
138 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
139 static u_int16_t iwi_read_prom_word(struct iwi_softc *, u_int8_t);
140 static int iwi_newstate(struct ieee80211com *,
141 enum ieee80211_state, int);
142 static void iwi_fix_channel(struct iwi_softc *, struct mbuf *);
143 static void iwi_frame_intr(struct iwi_softc *,
144 struct iwi_rx_buf *, int, struct iwi_frame *);
145 static void iwi_notification_intr(struct iwi_softc *,
147 static void iwi_rx_intr(struct iwi_softc *);
148 static void iwi_tx_intr(struct iwi_softc *);
149 static void iwi_intr(void *);
150 static void iwi_dma_map_buf(void *, bus_dma_segment_t *, int,
152 static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int);
153 static int iwi_cmd(struct iwi_softc *, u_int8_t, void *, u_int8_t,
155 static int iwi_tx_start(struct ifnet *, struct mbuf *,
156 struct ieee80211_node *);
157 static void iwi_start(struct ifnet *);
158 static void iwi_watchdog(struct ifnet *);
159 static int iwi_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *cr);
160 static void iwi_stop_master(struct iwi_softc *);
161 static int iwi_reset(struct iwi_softc *);
162 static int iwi_load_ucode(struct iwi_softc *, void *, int);
163 static int iwi_load_firmware(struct iwi_softc *, void *, int);
164 static int iwi_cache_firmware(struct iwi_softc *, void *, int);
165 static void iwi_free_firmware(struct iwi_softc *);
166 static int iwi_config(struct iwi_softc *);
167 static int iwi_scan(struct iwi_softc *);
168 static int iwi_auth_and_assoc(struct iwi_softc *);
169 static void iwi_init(void *);
170 static void iwi_init_locked(void *);
171 static void iwi_stop(void *);
172 static void iwi_dump_fw_event_log(struct iwi_softc *sc);
173 static void iwi_dump_fw_error_log(struct iwi_softc *sc);
174 static u_int8_t iwi_find_station(struct iwi_softc *sc, u_int8_t *mac);
175 static int8_t iwi_cache_station(struct iwi_softc *sc, u_int8_t *mac);
176 static int iwi_adapter_config(struct iwi_softc *sc, int is_a, int cmd_wait);
178 static int iwi_sysctl_bt_coexist(SYSCTL_HANDLER_ARGS);
179 static int iwi_sysctl_bg_autodetect(SYSCTL_HANDLER_ARGS);
180 static int iwi_sysctl_cts_to_self(SYSCTL_HANDLER_ARGS);
181 static int iwi_sysctl_antenna_diversity(SYSCTL_HANDLER_ARGS);
182 static int iwi_sysctl_radio(SYSCTL_HANDLER_ARGS);
183 static int iwi_sysctl_stats(SYSCTL_HANDLER_ARGS);
184 static int iwi_sysctl_dump_logs(SYSCTL_HANDLER_ARGS);
185 static int iwi_sysctl_neg_best_rates_first(SYSCTL_HANDLER_ARGS);
186 static int iwi_sysctl_disable_unicast_decryption(SYSCTL_HANDLER_ARGS);
187 static int iwi_sysctl_disable_multicast_decryption(SYSCTL_HANDLER_ARGS);
189 static __inline u_int8_t MEM_READ_1(struct iwi_softc *sc, u_int32_t addr)
191 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
192 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
195 static __inline u_int32_t MEM_READ_4(struct iwi_softc *sc, u_int32_t addr)
197 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
198 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
201 static int iwi_probe(device_t);
202 static int iwi_attach(device_t);
203 static int iwi_detach(device_t);
204 static int iwi_shutdown(device_t);
205 static int iwi_suspend(device_t);
206 static int iwi_resume(device_t);
208 static device_method_t iwi_methods[] = {
209 /* Device interface */
210 DEVMETHOD(device_probe, iwi_probe),
211 DEVMETHOD(device_attach, iwi_attach),
212 DEVMETHOD(device_detach, iwi_detach),
213 DEVMETHOD(device_shutdown, iwi_shutdown),
214 DEVMETHOD(device_suspend, iwi_suspend),
215 DEVMETHOD(device_resume, iwi_resume),
220 static driver_t iwi_driver = {
223 sizeof (struct iwi_softc),
229 static devclass_t iwi_devclass;
231 DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, 0, 0);
234 iwi_probe(device_t dev)
236 const struct iwi_ident *ident;
238 for (ident = iwi_ident_table; ident->name != NULL; ident++) {
239 if (pci_get_vendor(dev) == ident->vendor &&
240 pci_get_device(dev) == ident->device) {
241 device_set_desc(dev, ident->name);
249 iwi_fw_monitor(void *arg)
251 struct iwi_softc *sc = (struct iwi_softc *)arg;
254 error = tsleep(IWI_FW_WAKE_MONITOR(sc), 0, "iwifwm", 0 );
256 if ( sc->flags & IWI_FLAG_EXIT ) {
257 sc->flags &= ~( IWI_FLAG_EXIT );
259 } else if ( sc->flags & IWI_FLAG_RESET ) {
260 device_printf(sc->sc_dev, "firmware reset\n");
261 for ( boff = 1; sc->flags & IWI_FLAG_RESET ; boff++ ) {
262 if ( sc->debug_level > 0 )
263 iwi_dump_fw_error_log(sc);
265 if ((sc->flags & IWI_FLAG_FW_INITED))
266 sc->flags &= ~( IWI_FLAG_RESET );
267 error = tsleep( IWI_FW_CMD_ACKED(sc), 0,
268 "iwirun", boff * hz );
273 wakeup(IWI_FW_MON_EXIT(sc));
278 iwi_start_fw_monitor_thread( struct iwi_softc *sc )
280 if (kthread_create(iwi_fw_monitor, sc, &sc->event_thread,
281 "%s%d:fw-monitor", device_get_name(sc->sc_dev),
282 device_get_unit(sc->sc_dev))) {
283 device_printf (sc->sc_dev,
284 "unable to create firmware monitor thread.\n");
290 /* Base Address Register */
291 #define IWI_PCI_BAR0 0x10
294 iwi_attach(device_t dev)
296 struct iwi_softc *sc = device_get_softc(dev);
297 struct ieee80211com *ic = &sc->sc_ic;
298 struct ifnet *ifp = &ic->ic_if;
304 IWI_LOCK_INIT( &sc->sc_lock );
305 IWI_LOCK_INIT( &sc->sc_intrlock );
307 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
308 device_printf(dev, "chip is in D%d power mode "
309 "-- setting to D0\n", pci_get_powerstate(dev));
310 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
313 pci_write_config(dev, 0x41, 0, 1);
315 /* enable bus-mastering */
316 pci_enable_busmaster(dev);
318 sc->num_stations = 0;
320 /* map the register window */
322 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
323 if (sc->mem == NULL) {
324 device_printf(dev, "could not allocate memory resource\n");
328 sc->sc_st = rman_get_bustag(sc->mem);
329 sc->sc_sh = rman_get_bushandle(sc->mem);
332 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE |
334 if (sc->irq == NULL) {
335 device_printf(dev, "could not allocate interrupt resource\n");
339 if (iwi_reset(sc) != 0) {
340 device_printf(dev, "could not reset adapter\n");
344 if (iwi_start_fw_monitor_thread(sc) ) {
345 device_printf(dev, "could not start f/w reset thread\n");
349 if (iwi_dma_alloc(sc) != 0) {
350 device_printf(dev, "could not allocate DMA resources\n");
354 ic->ic_phytype = IEEE80211_T_OFDM;
355 ic->ic_opmode = IEEE80211_M_STA;
356 ic->ic_state = IEEE80211_S_INIT;
358 /* set device capabilities */
359 ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_PMGT | IEEE80211_C_WEP |
360 IEEE80211_C_TXPMGT | IEEE80211_C_SHPREAMBLE;
362 /* read MAC address from EEPROM */
363 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
364 ic->ic_myaddr[0] = val >> 8;
365 ic->ic_myaddr[1] = val & 0xff;
366 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
367 ic->ic_myaddr[2] = val >> 8;
368 ic->ic_myaddr[3] = val & 0xff;
369 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
370 ic->ic_myaddr[4] = val >> 8;
371 ic->ic_myaddr[5] = val & 0xff;
373 if (pci_get_device(dev) != 0x4220) {
374 /* set supported .11a rates */
375 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
377 /* set supported .11a channels */
378 for (i = 36; i <= 64; i += 4) {
379 ic->ic_channels[i].ic_freq =
380 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
381 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
383 for (i = 149; i <= 165; i += 4) {
384 ic->ic_channels[i].ic_freq =
385 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
386 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
390 /* set supported .11b and .11g rates */
391 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
392 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
394 /* set supported .11b and .11g channels (1 through 14) */
395 for (i = 1; i <= 14; i++) {
396 ic->ic_channels[i].ic_freq =
397 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
398 ic->ic_channels[i].ic_flags =
399 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
400 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
403 /* default to authmode OPEN */
404 sc->authmode = IEEE80211_AUTH_OPEN;
406 /* IBSS channel undefined for now */
407 ic->ic_ibss_chan = &ic->ic_channels[0];
410 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
411 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
412 ifp->if_init = iwi_init_locked;
413 ifp->if_ioctl = iwi_ioctl;
414 ifp->if_start = iwi_start;
415 ifp->if_watchdog = iwi_watchdog;
416 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
417 ifq_set_ready(&ifp->if_snd);
419 ieee80211_ifattach(ifp);
420 /* override state transition machine */
421 sc->sc_newstate = ic->ic_newstate;
422 ic->ic_newstate = iwi_newstate;
423 ieee80211_media_init(ifp, iwi_media_change, iwi_media_status);
425 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
426 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
428 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
429 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
430 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
432 sc->sc_txtap_len = sizeof sc->sc_txtapu;
433 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
434 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
437 * Hook our interrupt after all initialization is complete
439 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE,
440 iwi_intr, sc, &sc->sc_ih);
442 device_printf(dev, "could not set up interrupt\n");
449 * use -1 to indicate 'default / not set'
452 sc->enable_bg_autodetect = -1;
453 sc->enable_bt_coexist = -1;
454 sc->enable_cts_to_self = -1;
455 sc->antenna_diversity = -1;
456 sc->enable_neg_best_first = -1;
457 sc->disable_unicast_decryption = -1;
458 sc->disable_multicast_decryption = -1;
460 sysctl_ctx_init(&sc->sysctl_ctx);
461 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
462 SYSCTL_STATIC_CHILDREN(_hw),
464 device_get_nameunit(dev),
468 if (sc->sysctl_tree == NULL) {
473 SYSCTL_ADD_INT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
474 OID_AUTO, "debug", CTLFLAG_RW, &sc->debug_level, 0,
475 "Set driver debug level (0 = off)");
477 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
478 SYSCTL_CHILDREN(sc->sysctl_tree),
479 OID_AUTO, "cts_to_self", CTLTYPE_INT|CTLFLAG_RW,
480 (void *)sc, 0, iwi_sysctl_cts_to_self, "I",
481 "Enable cts to self [0 = Off] [1 = On] [-1 = Auto]" );
483 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
484 SYSCTL_CHILDREN(sc->sysctl_tree),
485 OID_AUTO, "antenna_diversity", CTLTYPE_INT|CTLFLAG_RW,
486 (void *)sc, 0, iwi_sysctl_antenna_diversity,
487 "I", "Set antenna diversity [0 = Both] "
488 "[1 = Antenna A] [3 = Antenna B] [-1 = Auto]" );
490 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
491 SYSCTL_CHILDREN(sc->sysctl_tree),
492 OID_AUTO, "bluetooth_coexist", CTLTYPE_INT|CTLFLAG_RW,
493 (void *)sc, 0, iwi_sysctl_bt_coexist,
494 "I", "Enable bluetooth coexistence heuristics "
495 "[0 = Off] [1 = On] [-1 = Auto]" );
497 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
498 SYSCTL_CHILDREN(sc->sysctl_tree),
499 OID_AUTO, "bg_autodetect", CTLTYPE_INT|CTLFLAG_RW,
500 (void *)sc, 0, iwi_sysctl_bg_autodetect,
501 "I", "Set b/g autodetect [0 = Off] [1 = On] [-1 = Auto]" );
504 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
505 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "radio",
506 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I",
507 "Radio transmitter switch");
509 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
510 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "stats",
511 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats,
512 "S,iwi_dump_buffer", "statistics");
514 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
515 SYSCTL_CHILDREN(sc->sysctl_tree),
516 OID_AUTO, "firmware_logs", CTLTYPE_INT|CTLFLAG_RW,
517 (void *)sc, 0, iwi_sysctl_dump_logs, "I", "Dump firmware logs");
519 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
520 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
521 "neg_best_rates_first",
522 CTLTYPE_INT | CTLFLAG_RW, sc, 0,
523 iwi_sysctl_neg_best_rates_first, "I",
524 "Negotiate highest rates first.");
526 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
527 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
528 "disable_unicast_decrypt",
529 CTLTYPE_INT | CTLFLAG_RW, sc, 0,
530 iwi_sysctl_disable_unicast_decryption, "I",
531 "Disable unicast decryption.");
533 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
534 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
535 "disable_multicast_decrypt",
536 CTLTYPE_INT | CTLFLAG_RW, sc, 0,
537 iwi_sysctl_disable_multicast_decryption, "I",
538 "Disable multicast decryption.");
542 fail: iwi_detach(dev);
547 iwi_detach(device_t dev)
549 struct iwi_softc *sc = device_get_softc(dev);
550 struct ifnet *ifp = &sc->sc_ic.ic_if;
554 sc->flags |= IWI_FLAG_EXIT;
555 wakeup(IWI_FW_WAKE_MONITOR(sc)); /* Stop firmware monitor. */
557 (void) tsleep(IWI_FW_MON_EXIT(sc), 0, "iwiexi", 10 * hz );
563 iwi_free_firmware(sc);
565 if ( sc->sysctl_tree ) {
567 sysctl_ctx_free(&sc->sysctl_ctx);
577 ieee80211_ifdetach(ifp);
581 if (sc->irq != NULL) {
582 bus_teardown_intr(dev, sc->irq, sc->sc_ih);
583 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
587 bus_release_resource(dev, SYS_RES_MEMORY, IWI_PCI_BAR0,
590 IWI_LOCK_DESTROY(&(sc->sc_lock));
591 IWI_LOCK_DESTROY(&(sc->sc_intrlock));
597 iwi_dma_alloc(struct iwi_softc *sc)
601 error = bus_dma_tag_create(NULL, /* parent */
603 BUS_SPACE_MAXADDR_32BIT,
607 BUS_SPACE_MAXSIZE_32BIT,
609 &sc->iwi_parent_tag );
611 device_printf(sc->sc_dev, "could not create parent tag\n");
615 * Allocate and map Tx ring
617 error = bus_dma_tag_create(sc->iwi_parent_tag, 4, 0, BUS_SPACE_MAXADDR_32BIT,
618 BUS_SPACE_MAXADDR, NULL, NULL,
619 sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE, 1,
620 sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE,
621 BUS_DMA_ALLOCNOW, &sc->tx_ring_dmat);
623 device_printf(sc->sc_dev, "could not create tx ring DMA tag\n");
627 error = bus_dmamem_alloc(sc->tx_ring_dmat,(void **) &sc->tx_desc,
628 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->tx_ring_map);
630 device_printf(sc->sc_dev,
631 "could not allocate tx ring DMA memory\n");
635 error = bus_dmamap_load(sc->tx_ring_dmat, sc->tx_ring_map,
636 sc->tx_desc, sizeof (struct iwi_tx_desc) * IWI_TX_RING_SIZE,
637 iwi_dma_map_addr, &sc->tx_ring_pa, 0);
639 device_printf(sc->sc_dev, "could not load tx ring DMA map\n");
644 * Allocate and map command ring
646 error = bus_dma_tag_create(sc->iwi_parent_tag, 4, 0, BUS_SPACE_MAXADDR_32BIT,
647 BUS_SPACE_MAXADDR, NULL, NULL,
648 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE, 1,
649 sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE,
653 device_printf(sc->sc_dev,
654 "could not create command ring DMA tag\n");
658 error = bus_dmamem_alloc(sc->cmd_ring_dmat, (void **)&sc->cmd_desc,
659 BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->cmd_ring_map);
661 device_printf(sc->sc_dev,
662 "could not allocate command ring DMA memory\n");
666 error = bus_dmamap_load(sc->cmd_ring_dmat, sc->cmd_ring_map,
667 sc->cmd_desc, sizeof (struct iwi_cmd_desc) * IWI_CMD_RING_SIZE,
668 iwi_dma_map_addr, &sc->cmd_ring_pa, 0);
670 device_printf(sc->sc_dev,
671 "could not load command ring DMA map\n");
676 * Allocate Tx buffers DMA maps
678 error = bus_dma_tag_create(sc->iwi_parent_tag, 1, 0, BUS_SPACE_MAXADDR_32BIT,
679 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IWI_MAX_NSEG, MCLBYTES,
680 BUS_DMA_ALLOCNOW, &sc->tx_buf_dmat);
682 device_printf(sc->sc_dev, "could not create tx buf DMA tag\n");
686 for (i = 0; i < IWI_TX_RING_SIZE; i++) {
687 error = bus_dmamap_create(sc->tx_buf_dmat, 0,
690 device_printf(sc->sc_dev,
691 "could not create tx buf DMA map");
697 * Allocate and map Rx buffers
699 error = bus_dma_tag_create(sc->iwi_parent_tag, 1, 0, BUS_SPACE_MAXADDR_32BIT,
700 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES,
701 BUS_DMA_ALLOCNOW, &sc->rx_buf_dmat);
703 device_printf(sc->sc_dev, "could not create rx buf DMA tag\n");
707 for (i = 0; i < IWI_RX_RING_SIZE; i++) {
709 error = bus_dmamap_create(sc->rx_buf_dmat, 0,
712 device_printf(sc->sc_dev,
713 "could not create rx buf DMA map");
717 sc->rx_buf[i].m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
718 if (sc->rx_buf[i].m == NULL) {
719 device_printf(sc->sc_dev,
720 "could not allocate rx mbuf\n");
725 error = bus_dmamap_load(sc->rx_buf_dmat, sc->rx_buf[i].map,
726 mtod(sc->rx_buf[i].m, void *), MCLBYTES, iwi_dma_map_addr,
727 &sc->rx_buf[i].physaddr, 0);
729 device_printf(sc->sc_dev,
730 "could not load rx buf DMA map");
737 fail: iwi_release(sc);
742 iwi_release(struct iwi_softc *sc)
746 if (sc->tx_ring_dmat != NULL) {
747 if (sc->tx_desc != NULL) {
748 bus_dmamap_sync(sc->tx_ring_dmat, sc->tx_ring_map,
749 BUS_DMASYNC_POSTWRITE);
750 bus_dmamap_unload(sc->tx_ring_dmat, sc->tx_ring_map);
751 bus_dmamem_free(sc->tx_ring_dmat, sc->tx_desc,
754 bus_dma_tag_destroy(sc->tx_ring_dmat);
757 if (sc->cmd_ring_dmat != NULL) {
758 if (sc->cmd_desc != NULL) {
759 bus_dmamap_sync(sc->cmd_ring_dmat, sc->cmd_ring_map,
760 BUS_DMASYNC_POSTWRITE);
761 bus_dmamap_unload(sc->cmd_ring_dmat, sc->cmd_ring_map);
762 bus_dmamem_free(sc->cmd_ring_dmat, sc->cmd_desc,
765 bus_dma_tag_destroy(sc->cmd_ring_dmat);
768 if (sc->tx_buf_dmat != NULL) {
769 for (i = 0; i < IWI_TX_RING_SIZE; i++) {
770 if (sc->tx_buf[i].m != NULL) {
771 bus_dmamap_sync(sc->tx_buf_dmat,
772 sc->tx_buf[i].map, BUS_DMASYNC_POSTWRITE);
773 bus_dmamap_unload(sc->tx_buf_dmat,
775 m_freem(sc->tx_buf[i].m);
777 bus_dmamap_destroy(sc->tx_buf_dmat, sc->tx_buf[i].map);
779 bus_dma_tag_destroy(sc->tx_buf_dmat);
782 if (sc->rx_buf_dmat != NULL) {
783 for (i = 0; i < IWI_RX_RING_SIZE; i++) {
784 if (sc->rx_buf[i].m != NULL) {
785 bus_dmamap_sync(sc->rx_buf_dmat,
786 sc->rx_buf[i].map, BUS_DMASYNC_POSTREAD);
787 bus_dmamap_unload(sc->rx_buf_dmat,
789 m_freem(sc->rx_buf[i].m);
791 bus_dmamap_destroy(sc->rx_buf_dmat, sc->rx_buf[i].map);
793 bus_dma_tag_destroy(sc->rx_buf_dmat);
795 if ( sc->iwi_parent_tag != NULL ) {
796 bus_dma_tag_destroy(sc->iwi_parent_tag);
801 iwi_shutdown(device_t dev)
803 struct iwi_softc *sc = device_get_softc(dev);
816 iwi_suspend(device_t dev)
818 struct iwi_softc *sc = device_get_softc(dev);
832 iwi_resume(device_t dev)
834 struct iwi_softc *sc = device_get_softc(dev);
835 struct ifnet *ifp = &sc->sc_ic.ic_if;
840 pci_write_config(dev, 0x41, 0, 1);
842 if (ifp->if_flags & IFF_UP) {
843 ifp->if_init(ifp->if_softc);
844 if (ifp->if_flags & IFF_RUNNING)
854 iwi_media_change(struct ifnet *ifp)
856 struct iwi_softc *sc = ifp->if_softc;
862 error = ieee80211_media_change(ifp);
863 if (error != ENETRESET) {
867 error = 0; /* clear ENETRESET */
869 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)){
871 error = tsleep( IWI_FW_CMD_ACKED(sc), 0, "iwirun", hz );
881 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
883 struct iwi_softc *sc = ifp->if_softc;
884 struct ieee80211com *ic = &sc->sc_ic;
885 #define N(a) (sizeof (a) / sizeof (a[0]))
886 static const struct {
892 { IWI_RATE_DS5, 11 },
893 { IWI_RATE_DS11, 22 },
894 { IWI_RATE_OFDM6, 12 },
895 { IWI_RATE_OFDM9, 18 },
896 { IWI_RATE_OFDM12, 24 },
897 { IWI_RATE_OFDM18, 36 },
898 { IWI_RATE_OFDM24, 48 },
899 { IWI_RATE_OFDM36, 72 },
900 { IWI_RATE_OFDM48, 96 },
901 { IWI_RATE_OFDM54, 108 },
906 imr->ifm_status = IFM_AVALID;
907 imr->ifm_active = IFM_IEEE80211;
908 if (ic->ic_state == IEEE80211_S_RUN)
909 imr->ifm_status |= IFM_ACTIVE;
911 /* read current transmission rate from adapter */
912 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE);
914 /* convert rate to 802.11 rate */
915 for (i = 0; i < N(rates) && rates[i].val != val ; i++);
916 rate = (i < N(rates)) ? rates[i].rate : 0;
918 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
919 switch (ic->ic_opmode) {
920 case IEEE80211_M_STA:
923 case IEEE80211_M_IBSS:
924 imr->ifm_active |= IFM_IEEE80211_ADHOC;
927 case IEEE80211_M_MONITOR:
928 imr->ifm_active |= IFM_IEEE80211_MONITOR;
931 case IEEE80211_M_AHDEMO:
932 case IEEE80211_M_HOSTAP:
933 /* should not get there */
940 iwi_disassociate( struct iwi_softc *sc )
942 sc->assoc.type = 2; /* DISASSOCIATE */
943 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &sc->assoc, sizeof sc->assoc, 0);
948 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg __unused)
950 struct iwi_softc *sc = ic->ic_softc;
953 case IEEE80211_S_SCAN:
954 if ( sc->flags & IWI_FLAG_ASSOCIATED ) {
955 sc->flags &= ~( IWI_FLAG_ASSOCIATED );
956 iwi_disassociate(sc);
957 (void) tsleep( IWI_FW_DEASSOCIATED(sc),
961 if ( !(sc->flags & IWI_FLAG_SCANNING) &&
962 !(sc->flags & IWI_FLAG_RF_DISABLED) ) {
967 case IEEE80211_S_AUTH:
968 if ( sc->flags & IWI_FLAG_ASSOCIATED ) {
969 sc->flags &= ~( IWI_FLAG_ASSOCIATED );
970 iwi_disassociate(sc);
971 (void) tsleep( IWI_FW_DEASSOCIATED(sc), 0,
975 if ( iwi_auth_and_assoc(sc) != 0 )
976 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
979 case IEEE80211_S_RUN:
980 if (sc->flags & IWI_FLAG_SCAN_COMPLETE) {
981 sc->flags &= ~(IWI_FLAG_SCAN_COMPLETE);
982 if (ic->ic_opmode == IEEE80211_M_IBSS ||
983 ic->ic_opmode == IEEE80211_M_MONITOR ) {
985 * In IBSS mode, following an end_scan
986 * the ieee80211 stack state machine transitions
987 * straight to 'run' state. This is out of
988 * step with the firmware which requires
989 * an association first. Flip our state from
990 * RUN back to AUTH to allow us to tell the
991 * firmware to associate.
993 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
998 case IEEE80211_S_ASSOC:
1000 case IEEE80211_S_INIT:
1001 sc->flags &= ~( IWI_FLAG_SCANNING | IWI_FLAG_ASSOCIATED );
1005 ic->ic_state = nstate;
1010 * Read 16 bits at address 'addr' from the serial EEPROM.
1011 * DON'T PLAY WITH THIS CODE UNLESS YOU KNOW *EXACTLY* WHAT YOU'RE DOING!
1014 iwi_read_prom_word(struct iwi_softc *sc, u_int8_t addr)
1020 /* Clock C once before the first command */
1021 IWI_EEPROM_CTL(sc, 0);
1022 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1023 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1024 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1026 /* Write start bit (1) */
1027 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1028 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1030 /* Write READ opcode (10) */
1031 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1032 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1033 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1034 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1036 /* Write address A7-A0 */
1037 for (n = 7; n >= 0; n--) {
1038 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1039 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1040 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1041 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1044 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1046 /* Read data Q15-Q0 */
1048 for (n = 15; n >= 0; n--) {
1049 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1050 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1051 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1052 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1055 IWI_EEPROM_CTL(sc, 0);
1057 /* Clear Chip Select and clock C */
1058 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1059 IWI_EEPROM_CTL(sc, 0);
1060 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1062 return be16toh(val);
1066 * XXX: Hack to set the current channel to the value advertised in beacons or
1067 * probe responses. Only used during AP detection.
1070 iwi_fix_channel(struct iwi_softc *sc, struct mbuf *m)
1072 struct ieee80211com *ic = &sc->sc_ic;
1073 struct ieee80211_frame *wh;
1075 u_int8_t *frm, *efrm;
1077 wh = mtod(m, struct ieee80211_frame *);
1079 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1082 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1084 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1085 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1089 * Cache station entries from beacons and probes.
1091 if ( iwi_find_station(sc, wh->i_addr2) == 0xff )
1092 iwi_cache_station(sc, wh->i_addr2);
1094 frm = (u_int8_t *)(wh + 1);
1095 efrm = mtod(m, u_int8_t *) + m->m_len;
1097 frm += 12; /* skip tstamp, bintval and capinfo fields */
1099 { /* XXX - debugging code */
1102 printf("Frame -->");
1103 for ( ptr = frm, cnt = 0 ; ptr < efrm ; ptr++, cnt++ ) {
1106 printf("0x%-2.2x ", *ptr);
1108 printf("<-- End Frame\n");
1112 while (frm < efrm) {
1113 if (*frm == IEEE80211_ELEMID_DSPARMS)
1114 #if IEEE80211_CHAN_MAX < 255
1115 if (frm[2] <= IEEE80211_CHAN_MAX)
1117 ic->ic_bss->ni_chan = &ic->ic_channels[frm[2]];
1119 frm += frm[1] + 2; /* advance to the next tag */
1124 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_buf *buf, int i,
1125 struct iwi_frame *frame)
1127 struct ieee80211com *ic = &sc->sc_ic;
1128 struct ifnet *ifp = &ic->ic_if;
1130 struct ieee80211_frame *wh;
1131 struct ieee80211_node *ni;
1134 DPRINTFN(5, ("RX!DATA!%u!%u!%u\n", le16toh(frame->len), frame->chan,
1137 if (le16toh(frame->len) < sizeof (struct ieee80211_frame_min) ||
1138 le16toh(frame->len) > MCLBYTES) {
1139 device_printf(sc->sc_dev, "bad frame length\n");
1143 bus_dmamap_unload(sc->rx_buf_dmat, buf->map);
1147 m->m_pkthdr.rcvif = ifp;
1148 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1149 sizeof (struct iwi_frame) + le16toh(frame->len);
1151 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1153 wh = mtod(m, struct ieee80211_frame *);
1154 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1156 * Hardware decrypts the frame itself but leaves the WEP bit
1157 * set in the 802.11 header and don't remove the iv and crc
1160 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1161 bcopy(wh, (char *)wh + IEEE80211_WEP_IVLEN +
1162 IEEE80211_WEP_KIDLEN, sizeof (struct ieee80211_frame));
1163 m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
1164 m_adj(m, -IEEE80211_WEP_CRCLEN);
1165 wh = mtod(m, struct ieee80211_frame *);
1168 if (sc->sc_drvbpf != NULL) {
1169 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1172 tap->wr_rate = frame->rate;
1174 htole16(ic->ic_channels[frame->chan].ic_freq);
1175 tap->wr_chan_flags =
1176 htole16(ic->ic_channels[frame->chan].ic_flags);
1177 tap->wr_antsignal = frame->signal;
1178 tap->wr_antnoise = frame->noise;
1179 tap->wr_antenna = frame->antenna;
1181 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1184 if (ic->ic_state == IEEE80211_S_SCAN)
1185 iwi_fix_channel(sc, m);
1187 if (ic->ic_opmode != IEEE80211_M_STA) {
1188 ni = ieee80211_find_node(ic, wh->i_addr2);
1190 ni = ieee80211_ref_node(ic->ic_bss);
1192 ni = ieee80211_ref_node(ic->ic_bss);
1194 /* Send the frame to the upper layer */
1195 ieee80211_input(ifp, m, ni, IWI_RSSIDBM2RAW(frame->rssi_dbm), 0);
1197 if (ni == ic->ic_bss)
1198 ieee80211_unref_node(&ni);
1200 ieee80211_free_node(ic, ni);
1202 buf->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1203 if (buf->m == NULL) {
1204 device_printf(sc->sc_dev, "could not allocate rx mbuf\n");
1208 error = bus_dmamap_load(sc->rx_buf_dmat, buf->map, mtod(buf->m, void *),
1209 MCLBYTES, iwi_dma_map_addr, &buf->physaddr, 0);
1211 device_printf(sc->sc_dev, "could not load rx buf DMA map\n");
1217 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, buf->physaddr);
1221 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1223 struct ieee80211com *ic = &sc->sc_ic;
1224 struct ifnet *ifp = &ic->ic_if;
1225 struct iwi_notif_scan_channel *chan;
1226 struct iwi_notif_scan_complete *scan;
1227 struct iwi_notif_authentication *auth;
1228 struct iwi_notif_association *assoc;
1230 switch (notif->type) {
1231 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1232 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1234 DPRINTFN(2, ("Scan channel (%u)\n", chan->nchan));
1237 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1238 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1240 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1243 sc->flags &= ~(IWI_FLAG_SCANNING);
1244 sc->flags |= IWI_FLAG_SCAN_COMPLETE;
1246 if ( sc->flags & IWI_FLAG_SCAN_ABORT ) {
1247 sc->flags &= ~(IWI_FLAG_SCAN_ABORT);
1248 wakeup(IWI_FW_SCAN_COMPLETED(sc));
1250 ieee80211_end_scan(ifp);
1251 wakeup(IWI_FW_SCAN_COMPLETED(sc));
1255 case IWI_NOTIF_TYPE_AUTHENTICATION:
1256 auth = (struct iwi_notif_authentication *)(notif + 1);
1258 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1260 switch (auth->state) {
1261 case IWI_AUTHENTICATED:
1262 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1265 case IWI_DEAUTHENTICATED:
1266 ieee80211_begin_scan(ifp);/* not necessary */
1270 device_printf(sc->sc_dev,
1271 "unknown authentication state %u\n", auth->state);
1275 case IWI_NOTIF_TYPE_ASSOCIATION:
1276 assoc = (struct iwi_notif_association *)(notif + 1);
1278 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1281 switch (assoc->state) {
1282 case IWI_ASSOCIATED:
1283 sc->flags |= IWI_FLAG_ASSOCIATED;
1284 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1287 case IWI_DEASSOCIATED:
1288 sc->flags &= ~(IWI_FLAG_ASSOCIATED);
1289 wakeup(IWI_FW_DEASSOCIATED(sc));
1290 ieee80211_begin_scan(ifp);/* probably not necessary */
1294 device_printf(sc->sc_dev,
1295 "unknown association state %u\n", assoc->state);
1299 case IWI_NOTIF_TYPE_CALIBRATION:
1300 DPRINTFN(5, ("Notification calib (%u)\n", notif->type));
1302 case IWI_NOTIF_TYPE_BEACON:
1303 DPRINTFN(5, ("Notification beacon (%u)\n", notif->type));
1305 case IWI_NOTIF_TYPE_NOISE:
1306 DPRINTFN(5, ("Notification noise (%u)\n", notif->type));
1310 device_printf(sc->sc_dev, "unknown notification type %u\n",
1316 iwi_rx_intr(struct iwi_softc *sc)
1318 struct iwi_rx_buf *buf;
1319 struct iwi_hdr *hdr;
1322 r = CSR_READ_4(sc, IWI_CSR_RX_READ_INDEX);
1324 for (i = (sc->rx_cur + 1) % IWI_RX_RING_SIZE; i != r;
1325 i = (i + 1) % IWI_RX_RING_SIZE) {
1327 buf = &sc->rx_buf[i];
1329 bus_dmamap_sync(sc->rx_buf_dmat, buf->map,
1330 BUS_DMASYNC_POSTREAD);
1332 hdr = mtod(buf->m, struct iwi_hdr *);
1334 switch (hdr->type) {
1335 case IWI_HDR_TYPE_FRAME:
1336 iwi_frame_intr(sc, buf, i,
1337 (struct iwi_frame *)(hdr + 1));
1340 case IWI_HDR_TYPE_NOTIF:
1341 iwi_notification_intr(sc,
1342 (struct iwi_notif *)(hdr + 1));
1346 device_printf(sc->sc_dev, "unknown hdr type %u\n",
1351 /* Tell the firmware what we have processed */
1352 sc->rx_cur = (r == 0) ? IWI_RX_RING_SIZE - 1 : r - 1;
1353 CSR_WRITE_4(sc, IWI_CSR_RX_WRITE_INDEX, sc->rx_cur);
1357 iwi_tx_intr(struct iwi_softc *sc)
1359 struct ieee80211com *ic = &sc->sc_ic;
1360 struct ifnet *ifp = &ic->ic_if;
1361 struct iwi_tx_buf *buf;
1364 r = CSR_READ_4(sc, IWI_CSR_TX1_READ_INDEX);
1366 bus_dmamap_sync(sc->tx_ring_dmat, sc->tx_ring_map, BUS_DMASYNC_POSTWRITE);
1369 for (i = (sc->tx_old + 1) % IWI_TX_RING_SIZE; i != r;
1370 i = (i + 1) % IWI_TX_RING_SIZE) {
1372 buf = &sc->tx_buf[i];
1374 bus_dmamap_sync(sc->tx_buf_dmat, buf->map,
1375 BUS_DMASYNC_POSTWRITE);
1376 bus_dmamap_unload(sc->tx_buf_dmat, buf->map);
1379 if (buf->ni != ic->ic_bss)
1380 ieee80211_free_node(ic, buf->ni);
1385 /* kill watchdog timer */
1386 sc->sc_tx_timer = 0;
1389 /* Remember what the firmware has processed */
1390 sc->tx_old = (r == 0) ? IWI_TX_RING_SIZE - 1 : r - 1;
1392 /* Call start() since some buffer descriptors have been released */
1393 ifp->if_flags &= ~IFF_OACTIVE;
1394 (*ifp->if_start)(ifp);
1400 struct iwi_softc *sc = arg;
1401 struct ieee80211com *ic = &sc->sc_ic;
1402 struct ifnet *ifp = &ic->ic_if;
1409 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) {
1414 /* Disable interrupts */
1415 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1417 DPRINTFN(8, ("INTR!0x%08x\n", r));
1419 sc->flags &= ~(IWI_FLAG_RF_DISABLED);
1421 if ( r & IWI_INTR_FATAL_ERROR ) {
1422 if ( !(sc->flags & (IWI_FLAG_RESET | IWI_FLAG_EXIT))) {
1423 sc->flags |= IWI_FLAG_RESET;
1424 wakeup(IWI_FW_WAKE_MONITOR(sc));
1428 if (r & IWI_INTR_PARITY_ERROR) {
1429 device_printf(sc->sc_dev, "fatal error\n");
1430 sc->sc_ic.ic_if.if_flags &= ~IFF_UP;
1436 if (r & IWI_INTR_FW_INITED) {
1437 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1438 wakeup(IWI_FW_INITIALIZED(sc));
1441 if (r & IWI_INTR_RADIO_OFF) {
1442 DPRINTF(("radio transmitter off\n"));
1443 sc->sc_ic.ic_if.if_flags &= ~IFF_UP;
1447 sc->flags |= IWI_FLAG_RF_DISABLED;
1450 if (r & IWI_INTR_RX_TRANSFER)
1453 if (r & IWI_INTR_CMD_TRANSFER)
1454 wakeup(IWI_FW_CMD_ACKED(sc));
1456 if (r & IWI_INTR_TX1_TRANSFER)
1459 if (r & ~(IWI_HANDLED_INTR_MASK))
1460 device_printf(sc->sc_dev,
1461 "unhandled interrupt(s) INTR!0x%08x\n",
1462 r & ~(IWI_HANDLED_INTR_MASK));
1464 /* Acknowledge interrupts */
1465 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1467 /* Re-enable interrupts */
1468 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
1472 if ((ifp->if_flags & IFF_RUNNING) && !ifq_is_empty(&ifp->if_snd))
1476 struct iwi_dma_mapping {
1477 bus_dma_segment_t segs[IWI_MAX_NSEG];
1483 iwi_dma_map_buf(void *arg, bus_dma_segment_t *segs, int nseg,
1484 bus_size_t mapsize, int error)
1486 struct iwi_dma_mapping *map = arg;
1491 KASSERT(nseg <= IWI_MAX_NSEG, ("too many DMA segments %d", nseg));
1493 bcopy(segs, map->segs, nseg * sizeof (bus_dma_segment_t));
1495 map->mapsize = mapsize;
1499 iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg __unused, int error)
1502 printf("iwi: fatal DMA mapping error !!!\n");
1506 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1508 *(bus_addr_t *)arg = segs[0].ds_addr;
1512 iwi_cmd(struct iwi_softc *sc, u_int8_t type, void *data, u_int8_t len,
1515 struct iwi_cmd_desc *desc;
1517 DPRINTFN(2, ("TX!CMD!%u!%u\n", type, len));
1519 desc = &sc->cmd_desc[sc->cmd_cur];
1520 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1521 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1524 bcopy(data, desc->data, len);
1526 bus_dmamap_sync(sc->cmd_ring_dmat, sc->cmd_ring_map,
1527 BUS_DMASYNC_PREWRITE);
1529 sc->cmd_cur = (sc->cmd_cur + 1) % IWI_CMD_RING_SIZE;
1530 CSR_WRITE_4(sc, IWI_CSR_CMD_WRITE_INDEX, sc->cmd_cur);
1532 return async ? 0 : tsleep( IWI_FW_CMD_ACKED(sc), 0, "iwicmd", hz);
1536 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni)
1538 struct iwi_softc *sc = ifp->if_softc;
1539 struct ieee80211com *ic = &sc->sc_ic;
1540 struct ieee80211_frame *wh;
1541 struct iwi_tx_buf *buf;
1542 struct iwi_tx_desc *desc;
1543 struct iwi_dma_mapping map;
1547 IWI_IPLLOCK_INFO; /* XXX still need old ipl locking mech. here */
1550 if (sc->sc_drvbpf != NULL) {
1551 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1554 tap->wt_chan_freq = htole16(ic->ic_bss->ni_chan->ic_freq);
1555 tap->wt_chan_flags = htole16(ic->ic_bss->ni_chan->ic_flags);
1557 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1560 buf = &sc->tx_buf[sc->tx_cur];
1561 desc = &sc->tx_desc[sc->tx_cur];
1563 wh = mtod(m0, struct ieee80211_frame *);
1565 if ( (id = iwi_find_station( sc, wh->i_addr1 ) ) == 0xff )
1566 id = iwi_cache_station( sc, wh->i_addr1 );
1568 bzero( desc, sizeof (struct iwi_tx_desc) );
1569 desc->station_number = id;
1571 /* trim IEEE802.11 header */
1572 m_adj(m0, sizeof (struct ieee80211_frame));
1574 error = bus_dmamap_load_mbuf(sc->tx_buf_dmat, buf->map, m0,
1575 iwi_dma_map_buf, &map, BUS_DMA_NOWAIT);
1576 if (error != 0 && error != EFBIG) {
1577 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1584 mnew = m_defrag(m0, MB_DONTWAIT);
1586 device_printf(sc->sc_dev,
1587 "could not defragment mbuf\n");
1594 error = bus_dmamap_load_mbuf(sc->tx_buf_dmat, buf->map, m0,
1595 iwi_dma_map_buf, &map, BUS_DMA_NOWAIT);
1597 device_printf(sc->sc_dev,
1598 "could not map mbuf (error %d)\n", error);
1608 desc->hdr.type = IWI_HDR_TYPE_DATA;
1609 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1610 desc->cmd = IWI_DATA_CMD_TX;
1611 desc->len = htole16(m0->m_pkthdr.len);
1613 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1614 if (!IEEE80211_IS_MULTICAST(wh->i_addr1))
1615 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1616 } else if (!IEEE80211_IS_MULTICAST(wh->i_addr3))
1617 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1619 if (ic->ic_flags & IEEE80211_F_WEPON) {
1620 wh->i_fc[1] |= IEEE80211_FC1_WEP;
1621 desc->wep_txkey = ic->ic_wep_txkey;
1623 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1625 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1626 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1628 bcopy(wh, &desc->wh, sizeof (struct ieee80211_frame));
1629 desc->nseg = htole32(map.nseg);
1630 for (i = 0; i < map.nseg; i++) {
1631 desc->seg_addr[i] = htole32(map.segs[i].ds_addr);
1632 desc->seg_len[i] = htole32(map.segs[i].ds_len);
1635 bus_dmamap_sync(sc->tx_buf_dmat, buf->map, BUS_DMASYNC_PREWRITE);
1636 bus_dmamap_sync(sc->tx_ring_dmat, sc->tx_ring_map,
1637 BUS_DMASYNC_PREWRITE);
1639 DPRINTFN(5, ("TX!DATA!%u!%u\n", desc->len, desc->nseg));
1641 /* Inform firmware about this new packet */
1643 sc->tx_cur = (sc->tx_cur + 1) % IWI_TX_RING_SIZE;
1644 CSR_WRITE_4(sc, IWI_CSR_TX1_WRITE_INDEX, sc->tx_cur);
1651 iwi_start(struct ifnet *ifp)
1653 struct iwi_softc *sc = ifp->if_softc;
1654 struct ieee80211com *ic = &sc->sc_ic;
1656 struct ieee80211_node *ni;
1658 if (ic->ic_state != IEEE80211_S_RUN) {
1663 m0 = ifq_poll(&ifp->if_snd);
1667 m0 = ifq_dequeue(&ifp->if_snd);
1669 if (sc->tx_queued >= IWI_TX_RING_SIZE - 4) {
1670 IF_PREPEND(&ifp->if_snd, m0);
1671 ifp->if_flags |= IFF_OACTIVE;
1679 m0 = ieee80211_encap(ifp, m0, &ni);
1683 if (ic->ic_rawbpf != NULL)
1684 bpf_mtap(ic->ic_rawbpf, m0);
1686 if (iwi_tx_start(ifp, m0, ni) != 0) {
1687 if (ni != NULL && ni != ic->ic_bss)
1688 ieee80211_free_node(ic, ni);
1692 /* start watchdog timer */
1693 sc->sc_tx_timer = 5;
1700 iwi_watchdog(struct ifnet *ifp)
1702 struct iwi_softc *sc = ifp->if_softc;
1706 if (sc->sc_tx_timer > 0) {
1707 if (--sc->sc_tx_timer == 0) {
1708 if_printf(ifp, "device timeout\n");
1709 wakeup(IWI_FW_WAKE_MONITOR(sc));
1715 ieee80211_watchdog(ifp);
1720 iwi_wi_ioctl_get(struct ifnet *ifp, caddr_t data)
1724 struct iwi_softc *sc;
1728 ifr = (struct ifreq *)data;
1729 error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1733 switch (wreq.wi_type) {
1734 case WI_RID_READ_APS:
1735 ieee80211_begin_scan(ifp);
1736 (void) tsleep(IWI_FW_SCAN_COMPLETED(sc),
1737 PPAUSE|PCATCH, "ssidscan", hz * 2);
1738 ieee80211_end_scan(ifp);
1751 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1753 struct iwi_softc *sc = ifp->if_softc;
1755 struct ieee80211req *ireq;
1765 * Handle this here instead of in net80211_ioctl.c
1766 * so that we can lock (IWI_LOCK) the call to
1769 ifa = (struct ifaddr *) data;
1770 switch (ifa->ifa_addr->sa_family) {
1773 if ((ifp->if_flags & IFF_UP) == 0) {
1774 ifp->if_flags |= IFF_UP;
1775 ifp->if_init(ifp->if_softc);
1777 arp_ifinit(ifp, ifa);
1781 #warning "IPX support has not been tested"
1783 * XXX - This code is probably wrong,
1784 * but has been copied many times.
1787 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1788 struct arpcom *ac = (struct arpcom *)ifp;
1790 if (ipx_nullhost(*ina))
1791 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
1793 bcopy((caddr_t) ina->x_host.c_host,
1794 (caddr_t) ac->ac_enaddr,
1795 sizeof(ac->ac_enaddr));
1800 if ((ifp->if_flags & IFF_UP) == 0) {
1801 ifp->if_flags |= IFF_UP;
1802 ifp->if_init(ifp->if_softc);
1809 if (ifp->if_flags & IFF_UP) {
1810 if (!(ifp->if_flags & IFF_RUNNING)) {
1812 error = tsleep(IWI_FW_CMD_ACKED(sc), 0,
1816 if (ifp->if_flags & IFF_RUNNING) {
1823 case SIOCSLOADIBSSFW:
1824 /* only super-user can do that! */
1825 if ((error = suser(curthread)) != 0)
1828 ifr = (struct ifreq *)data;
1829 error = iwi_cache_firmware(sc, ifr->ifr_data,
1830 (cmd == SIOCSLOADIBSSFW) ? 1 : 0);
1834 /* only super-user can do that! */
1835 if ((error = suser(curthread)) != 0)
1838 ifp->if_flags &= ~IFF_UP;
1840 iwi_free_firmware(sc);
1844 ireq = (struct ieee80211req *)data;
1845 switch (ireq->i_type) {
1846 case IEEE80211_IOC_AUTHMODE:
1847 ireq->i_val = sc->authmode;
1851 error = ieee80211_ioctl(ifp, cmd, data, cr);
1856 /* only super-user can do that! */
1857 if ((error = suser(curthread)) != 0)
1860 ireq = (struct ieee80211req *)data;
1861 switch (ireq->i_type) {
1862 case IEEE80211_IOC_AUTHMODE:
1863 sc->authmode = ireq->i_val;
1867 error = ieee80211_ioctl(ifp, cmd, data, cr);
1870 case SIOCGIFGENERIC:
1871 if (sc->flags & IWI_FLAG_FW_INITED) {
1872 error = iwi_wi_ioctl_get(ifp, data);
1874 error = ieee80211_ioctl(ifp, cmd, data, cr);
1877 if (error != ENOTTY)
1881 error = ieee80211_ioctl(ifp, cmd, data, cr);
1884 if (error == ENETRESET) {
1886 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1887 (IFF_UP | IFF_RUNNING)) {
1889 error = tsleep(IWI_FW_CMD_ACKED(sc), 0, "iwirun", hz);
1899 iwi_abort_scan( struct iwi_softc *sc )
1901 sc->flags |= IWI_FLAG_SCAN_ABORT;
1902 return iwi_cmd(sc, IWI_CMD_SCAN_ABORT, NULL, 0, 1);
1906 iwi_stop_master(struct iwi_softc *sc)
1911 * If the master is busy scanning, we will occasionally
1912 * timeout waiting for it (the master) to stop. Make the
1913 * 'stopping' process more robust by ceasing all scans
1914 * prior to asking for the stop.
1916 if ( ( sc->flags & IWI_FLAG_SCANNING ) &&
1917 !( sc->flags & IWI_FLAG_RF_DISABLED ) ) {
1919 if (( sc->flags & IWI_FLAG_SCAN_ABORT ) &&
1920 !( sc->flags & IWI_FLAG_RF_DISABLED )) {
1921 (void) tsleep(IWI_FW_SCAN_COMPLETED(sc), 0,
1925 /* Disable interrupts */
1927 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1929 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1930 for (ntries = 0; ntries < 5; ntries++) {
1931 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1935 if (ntries == 5 && sc->debug_level > 0)
1936 device_printf(sc->sc_dev, "timeout waiting for master\n");
1938 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1939 IWI_RST_PRINCETON_RESET);
1941 sc->flags &= ~IWI_FLAG_FW_INITED;
1945 iwi_reset(struct iwi_softc *sc)
1949 iwi_stop_master(sc);
1951 /* Move adapter to D0 state */
1952 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1955 /* Initialize Phase-Locked Level (PLL) */
1956 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1958 /* Wait for clock stabilization */
1959 for (ntries = 0; ntries < 1000; ntries++) {
1960 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1964 if (ntries == 1000) {
1968 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1973 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1977 /* Clear NIC memory */
1978 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1980 for (i = 0; i < 0xc000; i++) {
1981 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1984 sc->num_stations = 0;
1989 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
1994 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1995 IWI_RST_STOP_MASTER);
1996 for (ntries = 0; ntries < 5; ntries++) {
1997 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
2002 device_printf(sc->sc_dev, "timeout waiting for master\n");
2006 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
2008 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
2009 ~IWI_RST_PRINCETON_RESET);
2011 MEM_WRITE_4(sc, 0x3000e0, 0);
2013 MEM_WRITE_4(sc, 0x300004, 1);
2015 MEM_WRITE_4(sc, 0x300004, 0);
2017 MEM_WRITE_1(sc, 0x200000, 0x00);
2018 MEM_WRITE_1(sc, 0x200000, 0x40);
2021 /* Adapter is buggy, we must set the address for each word */
2022 for (w = uc; size > 0; w++, size -= 2)
2023 MEM_WRITE_2(sc, 0x200010, *w);
2025 MEM_WRITE_1(sc, 0x200000, 0x00);
2026 MEM_WRITE_1(sc, 0x200000, 0x80);
2028 /* Wait until we get a response in the uc queue */
2029 for (ntries = 0; ntries < 100; ntries++) {
2030 if (MEM_READ_1(sc, 0x200000) & 1)
2034 if (ntries == 100) {
2035 device_printf(sc->sc_dev,
2036 "timeout waiting for ucode to initialize\n");
2040 /* Empty the uc queue or the firmware will not initialize properly */
2041 for (i = 0; i < 7; i++)
2042 MEM_READ_4(sc, 0x200004);
2044 MEM_WRITE_1(sc, 0x200000, 0x00);
2049 /* macro to handle unaligned little endian data in firmware image */
2050 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2052 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
2056 bus_addr_t physaddr;
2059 u_int32_t sentinel, ctl, src, dst, sum, len, mlen;
2060 int ntries, error = 0;
2062 sc->flags &= ~(IWI_FLAG_FW_INITED);
2064 /* Allocate DMA memory for storing firmware image */
2065 error = bus_dma_tag_create(sc->iwi_parent_tag, 1, 0, BUS_SPACE_MAXADDR_32BIT,
2066 BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, BUS_DMA_ALLOCNOW, &dmat);
2068 device_printf(sc->sc_dev,
2069 "could not create firmware DMA tag\n");
2074 * We cannot map fw directly because of some hardware constraints on
2075 * the mapping address.
2077 error = bus_dmamem_alloc(dmat, &virtaddr, BUS_DMA_WAITOK, &map);
2079 device_printf(sc->sc_dev,
2080 "could not allocate firmware DMA memory\n");
2084 error = bus_dmamap_load(dmat, map, virtaddr, size, iwi_dma_map_addr,
2087 device_printf(sc->sc_dev, "could not load firmware DMA map\n");
2091 /* Copy firmware image to DMA memory */
2092 bcopy(fw, virtaddr, size);
2094 /* Make sure the adapter will get up-to-date values */
2095 bus_dmamap_sync(dmat, map, BUS_DMASYNC_PREWRITE);
2097 /* Tell the adapter where the command blocks are stored */
2098 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2101 * Store command blocks into adapter's internal memory using register
2102 * indirections. The adapter will read the firmware image through DMA
2103 * using information stored in command blocks.
2108 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2111 dst = GETLE32(p); p += 4; src += 4;
2112 len = GETLE32(p); p += 4; src += 4;
2116 mlen = min(len, IWI_CB_MAXDATALEN);
2118 ctl = IWI_CB_DEFAULT_CTL | mlen;
2119 sum = ctl ^ src ^ dst;
2121 /* Write a command block */
2122 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2123 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src);
2124 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst);
2125 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2133 /* Write a fictive final command block (sentinel) */
2134 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2135 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2137 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
2138 ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
2140 /* Tell the adapter to start processing command blocks */
2141 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2143 /* Wait until the adapter has processed all command blocks */
2144 for (ntries = 0; ntries < 400; ntries++) {
2145 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2149 if (ntries == 400) {
2150 device_printf(sc->sc_dev,
2151 "timeout processing command blocks\n");
2157 /* We're done with command blocks processing */
2158 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2160 /* Allow interrupts so we know when the firmware is inited */
2161 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2163 /* Tell the adapter to initialize the firmware */
2164 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2165 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
2166 IWI_CTL_ALLOW_STANDBY);
2168 /* Wait at most one second for firmware initialization to complete */
2169 if ((error = tsleep(IWI_FW_INITIALIZED(sc), 0, "iwiini", hz)) != 0) {
2170 device_printf(sc->sc_dev, "timeout waiting for firmware "
2171 "initialization to complete\n");
2175 fail4: bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTWRITE);
2176 bus_dmamap_unload(dmat, map);
2177 fail3: bus_dmamem_free(dmat, virtaddr, map);
2178 fail2: bus_dma_tag_destroy(dmat);
2184 * Store firmware into kernel memory so we can download it when we need to,
2185 * e.g when the adapter wakes up from suspend mode.
2188 iwi_cache_firmware(struct iwi_softc *sc, void *data, int is_ibss)
2190 struct iwi_firmware *kfw = &sc->fw;
2191 struct iwi_firmware ufw;
2194 iwi_free_firmware(sc);
2197 * mutex(9): no mutexes should be held across functions which access
2198 * memory in userspace, such as copyin(9) [...]
2201 if ((error = copyin(data, &ufw, sizeof ufw)) != 0)
2204 kfw->boot_size = ufw.boot_size;
2205 kfw->ucode_size = ufw.ucode_size;
2206 kfw->main_size = ufw.main_size;
2208 kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_WAITOK);
2209 if (kfw->boot == NULL) {
2214 kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_WAITOK);
2215 if (kfw->ucode == NULL) {
2220 kfw->main = malloc(kfw->main_size, M_DEVBUF, M_WAITOK);
2221 if (kfw->main == NULL) {
2226 if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0)
2229 if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0)
2232 if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0)
2235 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
2236 kfw->boot_size, kfw->ucode_size, kfw->main_size));
2239 sc->flags |= IWI_FLAG_FW_CACHED;
2240 sc->flags |= is_ibss ? IWI_FLAG_FW_IBSS : 0;
2243 fail4: free(kfw->boot, M_DEVBUF);
2244 fail3: free(kfw->ucode, M_DEVBUF);
2245 fail2: free(kfw->main, M_DEVBUF);
2252 iwi_free_firmware(struct iwi_softc *sc)
2254 if (!(sc->flags & IWI_FLAG_FW_CACHED))
2257 free(sc->fw.boot, M_DEVBUF);
2258 free(sc->fw.ucode, M_DEVBUF);
2259 free(sc->fw.main, M_DEVBUF);
2261 sc->flags &= ~( IWI_FLAG_FW_CACHED | IWI_FLAG_FW_IBSS );
2265 iwi_adapter_config(struct iwi_softc *sc, int is_a, int cmd_wait)
2267 struct iwi_configuration config;
2269 bzero(&config, sizeof config);
2270 config.enable_multicast = 1;
2271 config.noise_reported = 1;
2273 config.bg_autodetect =
2275 ( sc->enable_bg_autodetect != 0 ) ) ? 1 : 0; /* default: on */
2277 config.bluetooth_coexistence =
2278 ( sc->enable_bt_coexist != 0 ) ? 1 : 0; /* default: on */
2280 config.enable_cts_to_self =
2281 ( sc->enable_cts_to_self > 0 ) ? 1 : 0; /* default: off */
2283 if (sc->antenna_diversity > 0 ) { /* default: BOTH */
2284 switch( sc->antenna_diversity ) {
2286 config.antenna_diversity = sc->antenna_diversity;
2290 config.disable_unicast_decryption =
2291 ( sc->disable_unicast_decryption != 0 ) ? 1 : 0; /* default: on */
2293 config.disable_multicast_decryption =
2294 ( sc->disable_multicast_decryption != 0 ) ? 1 : 0;/* default: on */
2297 if ( sc->debug_level > 0 ) {
2298 printf("config.bluetooth_coexistence = %d\n",
2299 config.bluetooth_coexistence );
2300 printf("config.bg_autodetect = %d\n",
2301 config.bg_autodetect );
2302 printf("config.enable_cts_to_self = %d\n",
2303 config.enable_cts_to_self );
2304 printf("config.antenna_diversity = %d\n",
2305 config.antenna_diversity );
2306 printf("config.disable_unicast_decryption = %d\n",
2307 config.disable_unicast_decryption );
2308 printf("config.disable_multicast_decryption = %d\n",
2309 config.disable_multicast_decryption );
2310 printf("config.neg_best_rates_first = %d\n",
2311 sc->enable_neg_best_first );
2314 return iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
2315 sizeof config, cmd_wait );
2319 iwi_config(struct iwi_softc *sc)
2321 struct ieee80211com *ic = &sc->sc_ic;
2322 struct ifnet *ifp = &ic->ic_if;
2323 struct iwi_rateset rs;
2324 struct iwi_txpower power;
2325 struct ieee80211_wepkey *k;
2326 struct iwi_wep_key wepkey;
2330 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2331 DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":"));
2332 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2333 IEEE80211_ADDR_LEN, 0);
2337 DPRINTF(("Configuring adapter\n"));
2338 if ((error = iwi_adapter_config(sc, 1, 0)) != 0)
2341 data = htole32(IWI_POWER_MODE_CAM);
2342 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2343 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2347 data = htole32(ic->ic_rtsthreshold);
2348 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2349 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2353 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2354 power.mode = IWI_MODE_11B;
2356 for (i = 0; i < 11; i++) {
2357 power.chan[i].chan = i + 1;
2358 power.chan[i].power = IWI_TXPOWER_MAX;
2360 DPRINTF(("Setting .11b channels tx power\n"));
2361 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2366 power.mode = IWI_MODE_11G;
2367 DPRINTF(("Setting .11g channels tx power\n"));
2368 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2374 rs.mode = IWI_MODE_11G;
2375 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2376 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2377 bcopy(ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, rs.rates,
2379 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2380 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2384 rs.mode = IWI_MODE_11A;
2385 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2386 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2387 bcopy(ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, rs.rates,
2389 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2390 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2394 data = htole32(arc4random());
2395 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2396 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2400 if (ic->ic_flags & IEEE80211_F_WEPON) {
2402 for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) {
2403 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2405 wepkey.len = k->wk_len;
2406 bzero(wepkey.key, sizeof wepkey.key);
2407 bcopy(k->wk_key, wepkey.key, k->wk_len);
2408 DPRINTF(("Setting wep key index %u len %u\n",
2409 wepkey.idx, wepkey.len));
2410 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2417 /* Enable adapter */
2418 DPRINTF(("Enabling adapter\n"));
2419 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2423 iwi_scan(struct iwi_softc *sc)
2425 struct ieee80211com *ic = &sc->sc_ic;
2426 struct iwi_scan scan;
2429 int do_5ghz_scan = 0;
2431 sc->scan_counter++; /* track the number of scans started */
2433 sc->flags |= IWI_FLAG_SCANNING;
2435 bzero(&scan, sizeof scan);
2438 * Alternate two broadcast scans with
2439 * two broadcast/direct scans.
2441 if ( sc->scan_counter & 2 ) {
2442 scan.type = IWI_SCAN_TYPE_BROADCAST_AND_DIRECT;
2443 scan.intval = htole16(100);
2445 scan.type = IWI_SCAN_TYPE_BROADCAST;
2446 scan.intval = htole16(40);
2452 * If we have .11a capable adapter, and
2453 * - we are in .11a mode, or
2454 * - we are in auto mode and this is an odd numbered scan
2455 * then do a 5GHz scan, otherwise do a 2GHz scan.
2457 if ( ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates > 0 ) {
2458 if (( ic->ic_curmode == IEEE80211_MODE_11A ) ||
2459 (( ic->ic_curmode == IEEE80211_MODE_AUTO ) &&
2460 ( sc->scan_counter & 1)))
2464 if ( do_5ghz_scan ) {
2465 DPRINTF(("Scanning 5GHz band\n"));
2466 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2467 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2468 isset(ic->ic_chan_active, i)) {
2473 *(p - count) = IWI_CHAN_5GHZ | count;
2475 DPRINTF(("Scanning 2GHz band\n"));
2476 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2477 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2478 isset(ic->ic_chan_active, i)) {
2483 *(p - count) = IWI_CHAN_2GHZ | count;
2485 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1);
2489 iwi_auth_and_assoc(struct iwi_softc *sc)
2491 struct ieee80211com *ic = &sc->sc_ic;
2492 struct ifnet *ifp = &ic->ic_if;
2493 struct ieee80211_node *ni = ic->ic_bss;
2494 struct iwi_rateset rs;
2498 if ( ( sc->flags & IWI_FLAG_FW_IBSS ) &&
2499 !( ni->ni_capinfo & IEEE80211_CAPINFO_IBSS ) ) {
2500 return -1; /* IBSS F/W requires network ibss capability */
2503 DPRINTF(("Configuring adapter\n"));
2504 if ((error = iwi_adapter_config(sc,
2505 IEEE80211_IS_CHAN_5GHZ(ni->ni_chan), 1)) != 0)
2509 if (sc->debug_level > 0) {
2510 printf("Setting ESSID to ");
2511 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2515 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2519 /* the rate set has already been "negotiated" */
2520 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2522 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2523 rs.nrates = ni->ni_rates.rs_nrates;
2524 if ( sc->enable_neg_best_first != 1 ) {
2525 bcopy(ni->ni_rates.rs_rates, rs.rates, rs.nrates);
2527 for ( x = 0 ; x < rs.nrates; x++ ) {
2529 * Present the firmware with the most favourable
2530 * of the negotiated rates first.
2532 rs.rates[rs.nrates-x-1] = ni->ni_rates.rs_rates[x];
2536 if ( sc->debug_level > 0 ) {
2537 printf("Setting negotiated rates (%u) : ", rs.nrates);
2538 for ( x = 0 ; x < rs.nrates; x++ ) {
2539 printf("%d ", rs.rates[x]);
2544 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2548 data = htole32(ni->ni_rssi);
2549 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2550 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2554 bzero(&sc->assoc, sizeof sc->assoc);
2555 sc->assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2557 sc->assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2558 if (sc->authmode == IEEE80211_AUTH_SHARED)
2559 sc->assoc.auth = (ic->ic_wep_txkey << 4) | IWI_AUTH_SHARED;
2560 bcopy(ni->ni_tstamp, sc->assoc.tstamp, 8);
2561 sc->assoc.capinfo = htole16(ni->ni_capinfo);
2562 sc->assoc.lintval = htole16(ic->ic_lintval);
2563 sc->assoc.intval = htole16(ni->ni_intval);
2564 IEEE80211_ADDR_COPY(sc->assoc.bssid, ni->ni_bssid);
2565 if ( ic->ic_opmode == IEEE80211_M_IBSS )
2566 IEEE80211_ADDR_COPY(sc->assoc.dst, ifp->if_broadcastaddr);
2568 IEEE80211_ADDR_COPY(sc->assoc.dst, ni->ni_bssid);
2570 DPRINTF(("Trying to associate to %6D channel %u auth %u\n",
2571 sc->assoc.bssid, ":", sc->assoc.chan, sc->assoc.auth));
2572 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &sc->assoc, sizeof sc->assoc, 1);
2576 iwi_init(void *priv)
2578 struct iwi_softc *sc = priv;
2579 struct ieee80211com *ic = &sc->sc_ic;
2580 struct ifnet *ifp = &ic->ic_if;
2581 struct iwi_firmware *fw = &sc->fw;
2584 /* exit immediately if firmware has not been ioctl'd */
2585 if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
2586 ifp->if_flags &= ~IFF_UP;
2592 if (iwi_reset(sc) != 0) {
2593 device_printf(sc->sc_dev, "could not reset adapter\n");
2597 if (iwi_load_firmware(sc, fw->boot, fw->boot_size) != 0) {
2598 device_printf(sc->sc_dev, "could not load boot firmware\n");
2602 if (iwi_load_ucode(sc, fw->ucode, fw->ucode_size) != 0) {
2603 device_printf(sc->sc_dev, "could not load microcode\n");
2607 iwi_stop_master(sc);
2611 sc->tx_old = IWI_TX_RING_SIZE - 1;
2613 sc->rx_cur = IWI_RX_RING_SIZE - 1;
2615 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmd_ring_pa);
2616 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, IWI_CMD_RING_SIZE);
2617 CSR_WRITE_4(sc, IWI_CSR_CMD_READ_INDEX, 0);
2618 CSR_WRITE_4(sc, IWI_CSR_CMD_WRITE_INDEX, sc->cmd_cur);
2620 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->tx_ring_pa);
2621 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, IWI_TX_RING_SIZE);
2622 CSR_WRITE_4(sc, IWI_CSR_TX1_READ_INDEX, 0);
2623 CSR_WRITE_4(sc, IWI_CSR_TX1_WRITE_INDEX, sc->tx_cur);
2625 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->tx_ring_pa);
2626 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, IWI_TX_RING_SIZE);
2627 CSR_WRITE_4(sc, IWI_CSR_TX2_READ_INDEX, 0);
2628 CSR_WRITE_4(sc, IWI_CSR_TX2_WRITE_INDEX, 0);
2630 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->tx_ring_pa);
2631 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, IWI_TX_RING_SIZE);
2632 CSR_WRITE_4(sc, IWI_CSR_TX3_READ_INDEX, 0);
2633 CSR_WRITE_4(sc, IWI_CSR_TX3_WRITE_INDEX, 0);
2635 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->tx_ring_pa);
2636 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, IWI_TX_RING_SIZE);
2637 CSR_WRITE_4(sc, IWI_CSR_TX4_READ_INDEX, 0);
2638 CSR_WRITE_4(sc, IWI_CSR_TX4_WRITE_INDEX, 0);
2640 for (i = 0; i < IWI_RX_RING_SIZE; i++)
2641 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
2642 sc->rx_buf[i].physaddr);
2647 CSR_WRITE_4(sc, IWI_CSR_RX_WRITE_INDEX, sc->rx_cur);
2648 CSR_WRITE_4(sc, IWI_CSR_RX_READ_INDEX, 0);
2650 if (iwi_load_firmware(sc, fw->main, fw->main_size) != 0) {
2651 device_printf(sc->sc_dev, "could not load main firmware\n");
2656 * Force the opmode based on what firmware is loaded. This
2657 * stops folks from killing the firmware by asking it to
2658 * do something it doesn't support.
2660 if ( ic->ic_opmode != IEEE80211_M_MONITOR ) {
2661 ic->ic_opmode = ( sc->flags & IWI_FLAG_FW_IBSS )
2662 ? IEEE80211_M_IBSS : IEEE80211_M_STA;
2665 sc->flags |= IWI_FLAG_FW_INITED;
2667 sc->flags &= ~( IWI_FLAG_SCANNING |
2668 IWI_FLAG_SCAN_COMPLETE |
2669 IWI_FLAG_SCAN_ABORT |
2670 IWI_FLAG_ASSOCIATED );
2672 if (iwi_config(sc) != 0) {
2673 device_printf(sc->sc_dev, "device configuration failed\n");
2677 if ( ic->ic_opmode != IEEE80211_M_MONITOR ) {
2678 ieee80211_begin_scan(ifp);
2679 ifp->if_flags &= ~IFF_OACTIVE;
2680 ifp->if_flags |= IFF_RUNNING;
2682 ieee80211_begin_scan(ifp);
2683 ifp->if_flags &= ~IFF_OACTIVE;
2684 ifp->if_flags |= IFF_RUNNING;
2690 if ( !(sc->flags & IWI_FLAG_RESET) )
2691 ifp->if_flags &= ~IFF_UP;
2696 iwi_init_locked(void *priv)
2698 struct iwi_softc *sc = priv;
2706 iwi_stop(void *priv)
2708 struct iwi_softc *sc = priv;
2709 struct ieee80211com *ic = &sc->sc_ic;
2710 struct ifnet *ifp = &ic->ic_if;
2711 struct iwi_tx_buf *buf;
2714 iwi_stop_master(sc);
2715 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2718 * Release Tx buffers
2720 for (i = 0; i < IWI_TX_RING_SIZE; i++) {
2721 buf = &sc->tx_buf[i];
2723 if (buf->m != NULL) {
2724 bus_dmamap_sync(sc->tx_buf_dmat, buf->map,
2725 BUS_DMASYNC_POSTWRITE);
2726 bus_dmamap_unload(sc->tx_buf_dmat, buf->map);
2730 if (buf->ni != NULL) {
2731 if (buf->ni != ic->ic_bss)
2732 ieee80211_free_node(ic, buf->ni);
2739 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2741 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2745 iwi_cache_station(struct iwi_softc *sc, u_int8_t *mac)
2747 int i, x, base, elemsize = sizeof(struct iwi_fw_station);
2748 for (i = 0; i < sc->num_stations; i++)
2749 if (!memcmp(sc->stations[i], mac, IEEE80211_ADDR_LEN))
2751 if (i == IWI_FW_MAX_STATIONS)
2753 memcpy(sc->stations[i], mac, IEEE80211_ADDR_LEN);
2754 for (x = 0, base = IWI_STATION_TABLE + (i * elemsize) ;
2755 x < IEEE80211_ADDR_LEN ; x++ ) {
2756 CSR_WRITE_1(sc, base + x, mac[x]);
2758 if ( (i + 1) > sc->num_stations )
2764 iwi_find_station(struct iwi_softc *sc, u_int8_t *mac)
2767 for (i = 0; i < sc->num_stations; i++)
2768 if (!memcmp(sc->stations[i], mac, IEEE80211_ADDR_LEN))
2774 iwi_error_desc(u_int32_t val)
2777 case IWI_FW_ERROR_OK:
2779 case IWI_FW_ERROR_FAIL:
2781 case IWI_FW_ERROR_MEMORY_UNDERFLOW:
2782 return "MEMORY_UNDERFLOW";
2783 case IWI_FW_ERROR_MEMORY_OVERFLOW:
2784 return "MEMORY_OVERFLOW";
2785 case IWI_FW_ERROR_BAD_PARAM:
2786 return "BAD_PARAMETER";
2787 case IWI_FW_ERROR_BAD_CHECKSUM:
2788 return "BAD_CHECKSUM";
2789 case IWI_FW_ERROR_NMI_INTERRUPT:
2790 return "NMI_INTERRUPT";
2791 case IWI_FW_ERROR_BAD_DATABASE:
2792 return "BAD_DATABASE";
2793 case IWI_FW_ERROR_ALLOC_FAIL:
2794 return "ALLOC_FAIL";
2795 case IWI_FW_ERROR_DMA_UNDERRUN:
2796 return "DMA_UNDERRUN";
2797 case IWI_FW_ERROR_DMA_STATUS:
2798 return "DMA_STATUS";
2799 case IWI_FW_ERROR_DINOSTATUS_ERROR:
2800 return "DINOSTATUS_ERROR";
2801 case IWI_FW_ERROR_EEPROMSTATUS_ERROR:
2802 return "EEPROMSTATUS_ERROR";
2803 case IWI_FW_ERROR_SYSASSERT:
2805 case IWI_FW_ERROR_FATAL_ERROR:
2808 return "UNKNOWN_ERROR";
2813 iwi_dump_fw_event_log(struct iwi_softc *sc)
2815 u_int32_t ev, time, data, i, count, base;
2816 base = CSR_READ_4(sc, IWI_FW_EVENT_LOG);
2817 count = MEM_READ_4(sc, base);
2818 if ( count > 0 && (sc->flags & IWI_FLAG_FW_INITED) ) {
2819 printf("Reading %d event log entries from base address 0x%x.\n",
2821 if (IWI_FW_EVENT_START_OFFSET <= count * IWI_FW_EVENT_ELEM_SIZE)
2822 device_printf(sc->sc_dev,"Start IWI Event Log Dump:\n");
2823 for (i = IWI_FW_EVENT_START_OFFSET;
2824 i <= count * IWI_FW_EVENT_ELEM_SIZE;
2825 i += IWI_FW_EVENT_ELEM_SIZE) {
2826 ev = MEM_READ_4(sc, base + i);
2827 time = MEM_READ_4(sc, base + i + 1 * sizeof(u_int32_t));
2828 data = MEM_READ_4(sc, base + i + 2 * sizeof(u_int32_t));
2829 printf("%d %8p %8.8d\n", time, (void *) data, ev);
2832 printf("There are no entries in the firmware event log.\n");
2837 iwi_dump_fw_error_log(struct iwi_softc *sc)
2840 int32_t count, base;
2841 base = CSR_READ_4(sc, IWI_FW_ERROR_LOG);
2842 count = MEM_READ_4(sc, base);
2843 if ( count > 0 && (sc->flags & IWI_FLAG_FW_INITED) ) {
2844 printf("Reading %d error log entries "
2845 "from base address 0x%p.\n", count, (void *)base);
2846 for ( i = IWI_FW_ERROR_START_OFFSET;
2847 i <= count * IWI_FW_EVENT_ELEM_SIZE;
2848 i += IWI_FW_ERROR_ELEM_SIZE ) {
2851 iwi_error_desc(MEM_READ_4(sc, base + i)));
2852 printf(" time(%8.8d)", MEM_READ_4(sc, base + i + 4));
2853 for ( elems = 2 ; elems < 7 ; elems++ ) {
2854 printf(" %8p", (void *)
2855 MEM_READ_4(sc, base + i + (4 * elems)));
2863 iwi_sysctl_cts_to_self(SYSCTL_HANDLER_ARGS)
2865 struct iwi_softc *sc = (void *)arg1;
2866 int cts_to_self = sc->enable_cts_to_self;
2867 int error = sysctl_handle_int(oidp, &cts_to_self, 0, req);
2869 (void)arg2; /* silence WARNS == 6 */
2871 if ( !error && req->newptr && cts_to_self != sc->enable_cts_to_self ) {
2872 switch ( cts_to_self ) {
2873 case -1: case 0: case 1:
2874 sc->enable_cts_to_self = cts_to_self;
2875 error = iwi_adapter_config(sc, 0, 0);
2884 iwi_sysctl_antenna_diversity(SYSCTL_HANDLER_ARGS)
2886 struct iwi_softc *sc = (void *)arg1;
2887 int antenna_diversity = sc->antenna_diversity;
2888 int error = sysctl_handle_int(oidp, &antenna_diversity, 0, req);
2890 (void)arg2; /* silence WARNS == 6 */
2892 if ( !error && req->newptr && antenna_diversity != sc->antenna_diversity ) {
2893 switch ( antenna_diversity ) {
2894 case 1: case 3: case 0: case -1:
2895 sc->antenna_diversity = antenna_diversity;
2896 error = iwi_adapter_config(sc, 0, 0);
2904 iwi_sysctl_bg_autodetect(SYSCTL_HANDLER_ARGS)
2906 struct iwi_softc *sc = (void *)arg1;
2907 int bg_autodetect = sc->enable_bg_autodetect;
2908 int error = sysctl_handle_int(oidp, &bg_autodetect, 0, req);
2910 (void)arg2; /* silence WARNS == 6 */
2912 if ( !error && req->newptr && bg_autodetect != sc->enable_bg_autodetect ) {
2913 switch ( bg_autodetect ) {
2914 case 1: case 0: case -1:
2915 sc->enable_bg_autodetect = bg_autodetect;
2916 error = iwi_adapter_config(sc, 0, 0);
2924 iwi_sysctl_bt_coexist(SYSCTL_HANDLER_ARGS)
2926 struct iwi_softc *sc = (void *)arg1;
2927 int bt_coexist = sc->enable_bt_coexist;
2928 int error = sysctl_handle_int(oidp, &bt_coexist, 0, req);
2930 (void)arg2; /* silence WARNS == 6 */
2932 if ( !error && req->newptr && bt_coexist != sc->enable_bt_coexist ) {
2933 switch ( bt_coexist ) {
2934 case 1: case 0: case -1:
2935 sc->enable_bt_coexist = bt_coexist;
2936 error = iwi_adapter_config(sc, 0, 0);
2944 iwi_sysctl_dump_logs(SYSCTL_HANDLER_ARGS)
2946 struct iwi_softc *sc = arg1;
2948 int error = sysctl_handle_int(oidp, &result, 0, req);
2950 (void)arg2; /* silence WARNS == 6 */
2952 if (!error && req->newptr && result == 1) {
2953 iwi_dump_fw_event_log(sc);
2954 iwi_dump_fw_error_log(sc);
2960 iwi_sysctl_stats(SYSCTL_HANDLER_ARGS)
2962 struct iwi_softc *sc = arg1;
2964 struct iwi_dump_buffer dump;
2966 (void)arg2; /* silence WARNS == 6 */
2967 (void)oidp; /* silence WARNS == 6 */
2969 if (!(sc->flags & IWI_FLAG_FW_INITED)) {
2970 bzero(dump.buf, sizeof dump.buf);
2971 return SYSCTL_OUT(req, &dump, sizeof dump);
2974 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
2975 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &dump.buf[1], size);
2977 return SYSCTL_OUT(req, &dump, sizeof dump);
2981 iwi_sysctl_radio(SYSCTL_HANDLER_ARGS)
2983 struct iwi_softc *sc = arg1;
2986 (void)arg2; /* silence WARNS == 6 */
2987 (void)oidp; /* silence WARNS == 6 */
2989 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0;
2990 return SYSCTL_OUT(req, &val, sizeof val);
2994 iwi_sysctl_neg_best_rates_first(SYSCTL_HANDLER_ARGS)
2996 struct iwi_softc *sc = arg1;
2997 int best_first = sc->enable_neg_best_first;
2998 int error = sysctl_handle_int(oidp, &best_first, 0, req);
3000 (void)arg2; /* silence WARNS == 6 */
3001 (void)oidp; /* silence WARNS == 6 */
3003 if ( !error && req->newptr && best_first != sc->enable_neg_best_first ) {
3004 switch ( best_first ) {
3005 case 1: case 0: case -1:
3006 sc->enable_neg_best_first = best_first;
3014 iwi_sysctl_disable_unicast_decryption(SYSCTL_HANDLER_ARGS)
3016 struct iwi_softc *sc = arg1;
3017 int disable_uni = sc->disable_unicast_decryption;
3018 int error = sysctl_handle_int(oidp, &disable_uni, 0, req);
3020 (void)arg2; /* silence WARNS == 6 */
3021 (void)oidp; /* silence WARNS == 6 */
3023 if (!error && req->newptr && disable_uni != sc->disable_unicast_decryption) {
3024 switch ( disable_uni ) {
3025 case 1: case 0: case -1:
3026 sc->disable_unicast_decryption = disable_uni;
3034 iwi_sysctl_disable_multicast_decryption(SYSCTL_HANDLER_ARGS)
3036 struct iwi_softc *sc = arg1;
3037 int disable_mul = sc->disable_multicast_decryption;
3038 int error = sysctl_handle_int(oidp, &disable_mul, 0, req);
3040 (void)arg2; /* silence WARNS == 6 */
3041 (void)oidp; /* silence WARNS == 6 */
3043 if (!error && req->newptr && disable_mul!=sc->disable_multicast_decryption){
3044 switch ( disable_mul ) {
3045 case 1: case 0: case -1:
3046 sc->disable_multicast_decryption = disable_mul;