2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
37 #include <sys/endian.h>
38 #include <sys/firmware.h>
39 #include <sys/kernel.h>
40 #include <sys/interrupt.h>
42 #include <sys/module.h>
43 #include <sys/sysctl.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/ifq_var.h>
55 #include <net/netmsg2.h>
57 #include <netproto/802_11/ieee80211_var.h>
58 #include <netproto/802_11/ieee80211_radiotap.h>
60 #include <bus/pci/pcireg.h>
61 #include <bus/pci/pcivar.h>
63 #include "if_iwlvar.h"
64 #include "iwl2100reg.h"
65 #include "iwl2100var.h"
67 #define IWL2100_INIT_F_ENABLE 0x1
68 #define IWL2100_INIT_F_IBSSCHAN 0x2
70 #define sc_tx_th sc_u_tx_th.u_tx_th
71 #define sc_rx_th sc_u_rx_th.u_rx_th
73 static void iwl2100_init(void *);
74 static int iwl2100_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
75 static void iwl2100_start(struct ifnet *);
76 static void iwl2100_watchdog(struct ifnet *);
77 static int iwl2100_newstate(struct ieee80211com *, enum ieee80211_state, int);
78 static int iwl2100_media_change(struct ifnet *);
79 static void iwl2100_media_status(struct ifnet *, struct ifmediareq *);
80 static void iwl2100_stop(struct iwl2100_softc *);
81 static void iwl2100_restart(struct iwl2100_softc *);
82 static void iwl2100_reinit(struct iwl2100_softc *);
84 static void iwl2100_intr(void *);
85 static void iwl2100_txeof(struct iwl2100_softc *);
86 static void iwl2100_rxeof(struct iwl2100_softc *);
87 static void iwl2100_rxeof_status(struct iwl2100_softc *, int);
88 static void iwl2100_rxeof_note(struct iwl2100_softc *, int);
89 static void iwl2100_rxeof_cmd(struct iwl2100_softc *, int);
90 static void iwl2100_rxeof_data(struct iwl2100_softc *, int);
92 static void iwl2100_init_dispatch(struct netmsg *);
93 static void iwl2100_reinit_dispatch(struct netmsg *);
94 static void iwl2100_stop_dispatch(struct netmsg *);
95 static void iwl2100_newstate_dispatch(struct netmsg *);
96 static void iwl2100_scanend_dispatch(struct netmsg *);
97 static void iwl2100_restart_dispatch(struct netmsg *);
98 static void iwl2100_bmiss_dispatch(struct netmsg *);
100 static void iwl2100_stop_callouts(struct iwl2100_softc *);
101 static void iwl2100_restart_bmiss(void *);
102 static void iwl2100_ibss_bssid(void *);
103 static void iwl2100_reinit_callout(void *);
105 static int iwl2100_dma_alloc(device_t);
106 static void iwl2100_dma_free(device_t);
107 static int iwl2100_dma_mbuf_create(device_t);
108 static void iwl2100_dma_mbuf_destroy(device_t, int, int);
109 static int iwl2100_init_tx_ring(struct iwl2100_softc *);
110 static int iwl2100_init_rx_ring(struct iwl2100_softc *);
111 static void iwl2100_free_tx_ring(struct iwl2100_softc *);
112 static void iwl2100_free_rx_ring(struct iwl2100_softc *);
114 static int iwl2100_alloc_cmd(struct iwl2100_softc *);
115 static void iwl2100_free_cmd(struct iwl2100_softc *);
116 static int iwl2100_wait_cmd(struct iwl2100_softc *);
118 static void iwl2100_rxdesc_setup(struct iwl2100_softc *, int);
119 static int iwl2100_newbuf(struct iwl2100_softc *, int, int);
120 static int iwl2100_encap(struct iwl2100_softc *, struct mbuf *);
122 static void iwl2100_chan_change(struct iwl2100_softc *,
123 const struct ieee80211_channel *);
125 static int iwl2100_alloc_firmware(struct iwl2100_softc *,
126 enum ieee80211_opmode);
127 static void iwl2100_free_firmware(struct iwl2100_softc *);
128 static int iwl2100_load_firmware(struct iwl2100_softc *,
129 enum ieee80211_opmode);
130 static int iwl2100_load_fw_ucode(struct iwl2100_softc *,
131 const struct iwl2100_firmware *);
132 static int iwl2100_load_fw_data(struct iwl2100_softc *,
133 const struct iwl2100_firmware *);
134 static int iwl2100_init_firmware(struct iwl2100_softc *);
136 static int iwl2100_read_ord2(struct iwl2100_softc *, uint32_t,
138 static uint32_t iwl2100_read_ord1(struct iwl2100_softc *, uint32_t);
139 static void iwl2100_write_ord1(struct iwl2100_softc *, uint32_t, uint32_t);
141 static int iwl2100_reset(struct iwl2100_softc *);
142 static int iwl2100_hw_reset(struct iwl2100_softc *);
143 static int iwl2100_rfkilled(struct iwl2100_softc *);
145 static int iwl2100_scan(struct iwl2100_softc *);
146 static int iwl2100_auth(struct iwl2100_softc *);
147 static int iwl2100_ibss(struct iwl2100_softc *);
149 static int iwl2100_hw_init(struct iwl2100_softc *, const uint8_t *,
150 const uint8_t *, uint8_t, uint32_t);
151 static void iwl2100_hw_stop(struct iwl2100_softc *);
152 static int iwl2100_config(struct iwl2100_softc *, const uint8_t *,
153 const uint8_t *, uint8_t, int);
154 static int iwl2100_start_scan(struct iwl2100_softc *, uint32_t, uint32_t);
156 static int iwl2100_config_op(struct iwl2100_softc *, uint32_t);
157 static int iwl2100_set_addr(struct iwl2100_softc *, const uint8_t *);
158 static int iwl2100_set_opmode(struct iwl2100_softc *,
159 enum ieee80211_opmode);
160 static int iwl2100_set_80211(struct iwl2100_softc *);
161 static int iwl2100_set_basicrates(struct iwl2100_softc *);
162 static int iwl2100_set_txrates(struct iwl2100_softc *);
163 static int iwl2100_set_powersave(struct iwl2100_softc *, int);
164 static int iwl2100_set_rtsthreshold(struct iwl2100_softc *, uint16_t);
165 static int iwl2100_set_bssid(struct iwl2100_softc *, const uint8_t *);
166 static int iwl2100_set_essid(struct iwl2100_softc *, const uint8_t *, int);
167 static int iwl2100_set_auth_ciphers(struct iwl2100_softc *,
168 enum ieee80211_authmode);
169 static int iwl2100_set_wepkey(struct iwl2100_softc *,
170 const struct ieee80211_key *);
171 static int iwl2100_set_weptxkey(struct iwl2100_softc *, ieee80211_keyix);
172 static int iwl2100_set_privacy(struct iwl2100_softc *, int);
173 static int iwl2100_set_chan(struct iwl2100_softc *,
174 const struct ieee80211_channel *);
175 static int iwl2100_set_scanopt(struct iwl2100_softc *, uint32_t, uint32_t);
176 static int iwl2100_set_scan(struct iwl2100_softc *);
177 static int iwl2100_set_optie(struct iwl2100_softc *, void *, uint16_t);
178 static int iwl2100_set_bintval(struct iwl2100_softc *, uint16_t);
179 static int iwl2100_set_txpower(struct iwl2100_softc *, uint16_t);
182 iwl2100_config_done(struct iwl2100_softc *sc)
184 return iwl2100_config_op(sc, IWL2100_CMD_CONF_DONE);
188 iwl2100_config_start(struct iwl2100_softc *sc)
190 return iwl2100_config_op(sc, IWL2100_CMD_CONF_START);
194 iwl2100_restart_done(struct iwl2100_softc *sc)
196 callout_stop(&sc->sc_restart_bmiss);
197 sc->sc_flags &= ~IWL2100_F_RESTARTING;
201 iwl2100_attach(device_t dev)
203 struct iwl2100_softc *sc = device_get_softc(dev);
204 struct ieee80211com *ic = &sc->sc_ic;
205 struct ifnet *ifp = &ic->ic_if;
211 * Clear the retry timeout PCI configuration register to keep
212 * PCI TX retries from interfering with C3 CPU state.
214 pci_write_config(dev, IWL2100_PCIR_RETRY_TIMEOUT, 0, 1);
217 * Allocate DMA stuffs
219 error = iwl2100_dma_alloc(dev);
223 /* Disable interrupts */
224 CSR_WRITE_4(sc, IWL2100_INTR_MASK, 0);
227 * SW reset before reading EEPROM
229 error = iwl2100_reset(sc);
234 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
235 ifp->if_init = iwl2100_init;
236 ifp->if_ioctl = iwl2100_ioctl;
237 ifp->if_start = iwl2100_start;
238 ifp->if_watchdog = iwl2100_watchdog;
239 ifq_set_maxlen(&ifp->if_snd, IWL2100_TX_USED_MAX);
240 ifq_set_ready(&ifp->if_snd);
243 device_printf(dev, "eeprom\n");
244 for (i = 0; i < 128; ++i) {
245 if (i != 0 && i % 8 == 0)
247 val = iwl_read_eeprom(&sc->iwlcom, i);
248 kprintf("%04x ", val);
253 /* IBSS channel mask */
254 sc->sc_ibss_chans = iwl_read_eeprom(&sc->iwlcom,
255 IWL2100_EEPROM_IBSS_CHANS) & IWL2100_CFG_CHANMASK;
257 /* BSS channel mask */
258 sc->sc_bss_chans = iwl_read_eeprom(&sc->iwlcom, IWL2100_EEPROM_CHANS);
263 for (i = 0; i < ETHER_ADDR_LEN / 2; ++i) {
264 val = iwl_read_eeprom(&sc->iwlcom, IWL2100_EEPROM_MAC + i);
265 ic->ic_myaddr[i * 2] = val >> 8;
266 ic->ic_myaddr[(i * 2) + 1] = val & 0xff;
270 * Set supported channels
272 for (i = 0; i < 14; ++i) {
273 if (sc->sc_bss_chans & (1 << i)) {
276 ic->ic_channels[chan].ic_freq =
277 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
278 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_B;
282 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwl_rateset_11b;
283 ic->ic_phytype = IEEE80211_T_DS;
284 ic->ic_caps = IEEE80211_C_MONITOR |
286 IEEE80211_C_SHPREAMBLE |
288 ic->ic_caps_ext = IEEE80211_CEXT_AUTOSCAN;
289 ic->ic_state = IEEE80211_S_INIT;
290 ic->ic_opmode = IEEE80211_M_STA;
292 ieee80211_ifattach(ic);
295 * ieee80211_frame will be stripped on TX path, so only
296 * extra space needs to be reserved.
298 ic->ic_headroom = sizeof(struct iwl2100_tx_hdr) -
299 sizeof(struct ieee80211_frame);
301 sc->sc_newstate = ic->ic_newstate;
302 ic->ic_newstate = iwl2100_newstate;
304 ieee80211_media_init(ic, iwl2100_media_change, iwl2100_media_status);
306 error = bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE,
307 iwl2100_intr, sc, &sc->sc_irq_handle,
310 device_printf(dev, "can't setup intr\n");
311 ieee80211_ifdetach(ic);
315 ifp->if_cpuid = rman_get_cpuid(sc->sc_irq_res);
316 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
321 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
322 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
325 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(uint32_t));
326 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
327 sc->sc_tx_th.wt_ihdr.it_present = htole32(IWL2100_TX_RADIOTAP_PRESENT);
329 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(uint32_t));
330 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
331 sc->sc_rx_th.wr_ihdr.it_present = htole32(IWL2100_RX_RADIOTAP_PRESENT);
333 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
334 htole16(IEEE80211_CHAN_B);
337 * Create worker thread and initialize all necessary messages
339 iwl_create_thread(&sc->iwlcom, device_get_unit(dev));
341 iwlmsg_init(&sc->sc_scanend_msg, &netisr_adone_rport,
342 iwl2100_scanend_dispatch, sc);
343 iwlmsg_init(&sc->sc_restart_msg, &netisr_adone_rport,
344 iwl2100_restart_dispatch, sc);
345 iwlmsg_init(&sc->sc_bmiss_msg, &netisr_adone_rport,
346 iwl2100_bmiss_dispatch, sc);
347 iwlmsg_init(&sc->sc_reinit_msg, &netisr_adone_rport,
348 iwl2100_reinit_dispatch, sc);
350 iwlmsg_init(&sc->sc_assoc_msg, &netisr_adone_rport,
351 iwl2100_newstate_dispatch, sc);
352 sc->sc_assoc_msg.iwlm_nstate = IEEE80211_S_ASSOC;
353 sc->sc_assoc_msg.iwlm_arg = -1;
355 iwlmsg_init(&sc->sc_run_msg, &netisr_adone_rport,
356 iwl2100_newstate_dispatch, sc);
357 sc->sc_run_msg.iwlm_nstate = IEEE80211_S_RUN;
358 sc->sc_run_msg.iwlm_arg = -1;
361 * Initialize callouts
363 callout_init(&sc->sc_restart_bmiss);
364 callout_init(&sc->sc_ibss);
365 callout_init(&sc->sc_reinit);
367 /* Add sysctl node */
368 SYSCTL_ADD_UINT(&sc->sc_sysctl_ctx,
369 SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
370 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug flags");
373 ieee80211_announce(ic);
378 iwl2100_detach(device_t dev)
380 struct iwl2100_softc *sc = device_get_softc(dev);
382 if (device_is_attached(dev)) {
383 struct ifnet *ifp = &sc->sc_ic.ic_if;
385 lwkt_serialize_enter(ifp->if_serializer);
387 sc->sc_flags |= IWL2100_F_DETACH;
389 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
390 iwl_destroy_thread(&sc->iwlcom);
392 lwkt_serialize_exit(ifp->if_serializer);
394 iwl2100_free_firmware(sc);
397 ieee80211_ifdetach(&sc->sc_ic);
399 iwl2100_dma_free(dev);
403 iwl2100_shutdown(device_t dev)
405 struct iwl2100_softc *sc = device_get_softc(dev);
406 struct ifnet *ifp = &sc->sc_ic.ic_if;
408 lwkt_serialize_enter(ifp->if_serializer);
410 lwkt_serialize_exit(ifp->if_serializer);
416 iwl2100_stop(struct iwl2100_softc *sc)
420 ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
422 iwl2100_stop_callouts(sc);
424 iwlmsg_init(&msg, &sc->sc_reply_port, iwl2100_stop_dispatch, sc);
425 lwkt_domsg(&sc->sc_thread_port, &msg.iwlm_nmsg.nm_lmsg, 0);
429 iwl2100_stop_dispatch(struct netmsg *nmsg)
431 struct iwlmsg *msg = (struct iwlmsg *)nmsg;
432 struct iwl2100_softc *sc = msg->iwlm_softc;
434 ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
436 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1);
438 lwkt_replymsg(&nmsg->nm_lmsg, 0);
442 iwl2100_hw_stop(struct iwl2100_softc *sc)
444 struct ifnet *ifp = &sc->sc_ic.ic_if;
446 ASSERT_SERIALIZED(ifp->if_serializer);
447 KKASSERT(curthread == &sc->sc_thread);
449 callout_stop(&sc->sc_reinit);
451 /* Disable interrupts */
452 CSR_WRITE_4(sc, IWL2100_INTR_MASK, 0);
457 iwl2100_hw_reset(sc);
463 iwl2100_free_tx_ring(sc);
464 iwl2100_free_rx_ring(sc);
466 /* NOTE: MUST after iwl2100_free_tx_ring() */
467 iwl2100_free_cmd(sc);
470 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
473 sc->sc_flags &= ~(IWL2100_F_WAITCMD |
476 IWL2100_F_RESTARTING |
483 iwl2100_reset(struct iwl2100_softc *sc)
490 #define WAIT_MAX 1000
492 CSR_WRITE_4(sc, IWL2100_RESET, IWL2100_RESET_SW);
493 for (i = 0; i < WAIT_MAX; ++i) {
495 if (CSR_READ_4(sc, IWL2100_RESET) & IWL2100_RESET_DONE)
499 if_printf(&sc->sc_ic.ic_if, "sw reset timed out\n");
506 * Move to D0 state, wait clock to become stable
508 #define WAIT_MAX 10000
510 CSR_WRITE_4(sc, IWL2100_CTRL, IWL2100_CTRL_INITDONE);
511 for (i = 0; i < WAIT_MAX; ++i) {
513 if (CSR_READ_4(sc, IWL2100_CTRL) & IWL2100_CTRL_CLKREADY)
517 if_printf(&sc->sc_ic.ic_if, "can't stablize clock\n");
526 CSR_SETBITS_4(sc, IWL2100_CTRL, IWL2100_CTRL_STANDBY);
531 iwl2100_dma_alloc(device_t dev)
533 struct iwl2100_softc *sc = device_get_softc(dev);
534 struct iwl2100_tx_ring *tr = &sc->sc_txring;
535 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
539 * Create top level DMA tag
541 error = bus_dma_tag_create(NULL, 1, 0,
542 BUS_SPACE_MAXADDR_32BIT,
546 BUS_SPACE_UNRESTRICTED,
547 BUS_SPACE_MAXSIZE_32BIT,
550 device_printf(dev, "can't create DMA tag\n");
555 * Create DMA stuffs for TX desc ring
557 error = iwl_dma_mem_create(dev, sc->sc_dtag, IWL2100_TXRING_SIZE,
558 &tr->tr_dtag, (void **)&tr->tr_desc,
559 &tr->tr_paddr, &tr->tr_dmap);
561 device_printf(dev, "can't create DMA memory for "
567 * Create DMA stuffs for RX desc ring
569 error = iwl_dma_mem_create(dev, sc->sc_dtag, IWL2100_RXRING_SIZE,
570 &rr->rr_dtag, (void **)&rr->rr_desc,
571 &rr->rr_paddr, &rr->rr_dmap);
573 device_printf(dev, "can't create DMA memory for "
579 * Create DMA stuffs for RX status ring
581 error = iwl_dma_mem_create(dev, sc->sc_dtag, IWL2100_RXSTATUS_SIZE,
582 &rr->rr_st_dtag, (void **)&rr->rr_status,
583 &rr->rr_st_paddr, &rr->rr_st_dmap);
585 device_printf(dev, "can't create DMA memory for "
591 * Create mbuf DMA stuffs
593 error = iwl2100_dma_mbuf_create(dev);
601 iwl2100_dma_free(device_t dev)
603 struct iwl2100_softc *sc = device_get_softc(dev);
604 struct iwl2100_tx_ring *tr = &sc->sc_txring;
605 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
607 /* Free DMA stuffs for TX desc ring */
608 iwl_dma_mem_destroy(tr->tr_dtag, tr->tr_desc, tr->tr_dmap);
610 /* Free DMA stuffs for RX desc ring */
611 iwl_dma_mem_destroy(rr->rr_dtag, rr->rr_desc, rr->rr_dmap);
613 /* Free DMA stuffs for RX status ring */
614 iwl_dma_mem_destroy(rr->rr_st_dtag, rr->rr_status, rr->rr_st_dmap);
616 /* Free DMA stuffs for mbufs */
617 iwl2100_dma_mbuf_destroy(dev, IWL2100_TX_NDESC, IWL2100_RX_NDESC);
619 /* Free top level DMA tag */
620 if (sc->sc_dtag != NULL)
621 bus_dma_tag_destroy(sc->sc_dtag);
625 iwl2100_dma_mbuf_create(device_t dev)
627 struct iwl2100_softc *sc = device_get_softc(dev);
628 struct iwl2100_tx_ring *tr = &sc->sc_txring;
629 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
633 * Create mbuf DMA tag
635 error = bus_dma_tag_create(sc->sc_dtag, 1, 0,
636 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
638 MCLBYTES, IWL2100_NSEG_MAX,
639 BUS_SPACE_MAXSIZE_32BIT,
640 BUS_DMA_ALLOCNOW, &sc->sc_mbuf_dtag);
642 device_printf(dev, "can't create mbuf DMA tag\n");
647 * Create spare DMA map for RX mbufs
649 error = bus_dmamap_create(sc->sc_mbuf_dtag, 0, &rr->rr_tmp_dmap);
651 device_printf(dev, "can't create spare mbuf DMA map\n");
652 bus_dma_tag_destroy(sc->sc_mbuf_dtag);
653 sc->sc_mbuf_dtag = NULL;
658 * Create DMA maps for RX mbufs
660 for (i = 0; i < IWL2100_RX_NDESC; ++i) {
661 error = bus_dmamap_create(sc->sc_mbuf_dtag, 0,
662 &rr->rr_buf[i].rb_dmap);
664 device_printf(dev, "can't create %d RX mbuf "
666 iwl2100_dma_mbuf_destroy(dev, 0, i);
672 * Create DMA maps for TX mbufs
674 for (i = 0; i < IWL2100_TX_NDESC; ++i) {
675 error = bus_dmamap_create(sc->sc_mbuf_dtag, 0,
676 &tr->tr_buf[i].tb_dmap);
678 device_printf(dev, "can't create %d TX mbuf "
680 iwl2100_dma_mbuf_destroy(dev, i, IWL2100_RX_NDESC);
688 iwl2100_dma_mbuf_destroy(device_t dev, int tx_done, int rx_done)
690 struct iwl2100_softc *sc = device_get_softc(dev);
691 struct iwl2100_tx_ring *tr = &sc->sc_txring;
692 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
695 if (sc->sc_mbuf_dtag == NULL)
699 * Destroy DMA maps for RX mbufs
701 for (i = 0; i < rx_done; ++i) {
702 struct iwl2100_rxbuf *rb = &rr->rr_buf[i];
704 KASSERT(rb->rb_mbuf == NULL, ("RX mbuf is not freed yet"));
705 bus_dmamap_destroy(sc->sc_mbuf_dtag, rb->rb_dmap);
709 * Destroy DMA maps for TX mbufs
711 for (i = 0; i < tx_done; ++i) {
712 struct iwl2100_txbuf *tb = &tr->tr_buf[i];
714 KASSERT(tb->tb_mbuf == NULL, ("TX mbuf is not freed yet"));
715 bus_dmamap_destroy(sc->sc_mbuf_dtag, tb->tb_dmap);
719 * Destroy spare mbuf DMA map
721 bus_dmamap_destroy(sc->sc_mbuf_dtag, rr->rr_tmp_dmap);
724 * Destroy mbuf DMA tag
726 bus_dma_tag_destroy(sc->sc_mbuf_dtag);
727 sc->sc_mbuf_dtag = NULL;
731 iwl2100_init(void *xsc)
733 struct iwl2100_softc *sc = xsc;
736 ASSERT_SERIALIZED(sc->sc_ic.ic_if.if_serializer);
738 iwl2100_stop_callouts(sc);
740 iwlmsg_init(&msg, &sc->sc_reply_port, iwl2100_init_dispatch, sc);
741 lwkt_domsg(&sc->sc_thread_port, &msg.iwlm_nmsg.nm_lmsg, 0);
745 iwl2100_init_dispatch(struct netmsg *nmsg)
747 struct iwlmsg *msg = (struct iwlmsg *)nmsg;
748 struct iwl2100_softc *sc = msg->iwlm_softc;
749 struct ieee80211com *ic = &sc->sc_ic;
750 struct ifnet *ifp = &ic->ic_if;
751 int error = 0, flags;
753 ASSERT_SERIALIZED(ifp->if_serializer);
755 if (sc->sc_flags & IWL2100_F_DETACH)
758 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
760 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
763 * Workaround for dummy firmware:
764 * Don't enable hardware too early, since
765 * once it is enabled, it will start scanning.
769 flags = IWL2100_INIT_F_ENABLE;
772 /* Always put the device into a known state */
773 error = iwl2100_hw_init(sc, NULL,
774 ic->ic_des_essid, ic->ic_des_esslen, flags);
778 if (sc->sc_flags & IWL2100_F_ZERO_CMD) {
779 if_printf(ifp, "zero cmd, reinit 1s later\n");
782 callout_reset(&sc->sc_reinit, hz, iwl2100_reinit_callout, sc);
786 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
787 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
788 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
790 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
795 lwkt_replymsg(&nmsg->nm_lmsg, error);
799 iwl2100_ioctl(struct ifnet *ifp, u_long cmd, caddr_t req, struct ucred *cr)
801 struct iwl2100_softc *sc = ifp->if_softc;
804 ASSERT_SERIALIZED(ifp->if_serializer);
806 if (sc->sc_flags & IWL2100_F_DETACH)
811 if (ifp->if_flags & IFF_UP) {
812 if ((ifp->if_flags & IFF_RUNNING) == 0)
815 if (ifp->if_flags & IFF_RUNNING) {
819 * Stop callouts explicitly, since
820 * if reinitialization is happening,
821 * IFF_RUNNING will not be turned on.
823 iwl2100_stop_callouts(sc);
828 error = ieee80211_ioctl(&sc->sc_ic, cmd, req, cr);
832 if (error == ENETRESET) {
833 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
834 (IFF_UP | IFF_RUNNING))
842 iwl2100_start(struct ifnet *ifp)
844 struct iwl2100_softc *sc = ifp->if_softc;
845 struct ieee80211com *ic = &sc->sc_ic;
846 struct iwl2100_tx_ring *tr = &sc->sc_txring;
849 ASSERT_SERIALIZED(ifp->if_serializer);
851 if (sc->sc_flags & IWL2100_F_DETACH) {
852 ieee80211_drain_mgtq(&ic->ic_mgtq);
853 ifq_purge(&ifp->if_snd);
857 if ((ifp->if_flags & (IFF_OACTIVE | IFF_RUNNING)) != IFF_RUNNING)
860 if ((sc->sc_flags & IWL2100_F_IFSTART) == 0) {
861 ifq_purge(&ifp->if_snd);
865 while (tr->tr_used < IWL2100_TX_USED_MAX) {
866 struct ieee80211_frame *wh;
867 struct ieee80211_node *ni;
868 struct ether_header *eh;
871 m = ifq_dequeue(&ifp->if_snd, NULL);
875 if (m->m_len < sizeof(*eh)) {
876 m = m_pullup(m, sizeof(*eh));
882 eh = mtod(m, struct ether_header *);
884 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
895 m = ieee80211_encap(ic, m, ni);
897 ieee80211_free_node(ni);
902 if (ic->ic_rawbpf != NULL)
903 bpf_mtap(ic->ic_rawbpf, m);
905 wh = mtod(m, struct ieee80211_frame *);
906 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
907 if (ieee80211_crypto_encap(ic, ni, m) == NULL) {
908 ieee80211_free_node(ni);
918 if (sc->sc_drvbpf != NULL) {
919 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
920 sc->sc_tx_th.wt_flags = IEEE80211_RADIOTAP_F_WEP;
922 sc->sc_tx_th.wt_flags = 0;
923 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_tx_th,
926 wh = NULL; /* Catch any invalid use */
928 ieee80211_free_node(ni);
930 if (iwl2100_encap(sc, m)) {
939 if (tr->tr_used >= IWL2100_TX_USED_MAX)
940 ifp->if_flags |= IFF_OACTIVE;
943 bus_dmamap_sync(tr->tr_dtag, tr->tr_dmap, BUS_DMASYNC_PREWRITE);
944 CSR_WRITE_4(sc, IWL2100_TXQ_WRITE_IDX, tr->tr_index);
948 ieee80211_drain_mgtq(&ic->ic_mgtq);
953 iwl2100_watchdog(struct ifnet *ifp)
955 struct iwl2100_softc *sc = ifp->if_softc;
957 ASSERT_SERIALIZED(ifp->if_serializer);
959 if (sc->sc_flags & IWL2100_F_DETACH)
962 if (sc->sc_tx_timer) {
963 if (--sc->sc_tx_timer == 0) {
964 if_printf(ifp, "watchdog timeout!\n");
972 ieee80211_watchdog(&sc->sc_ic);
976 iwl2100_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
978 struct ifnet *ifp = &ic->ic_if;
979 struct iwl2100_softc *sc = ifp->if_softc;
982 ASSERT_SERIALIZED(ifp->if_serializer);
984 iwlmsg_init(&msg, &sc->sc_reply_port, iwl2100_newstate_dispatch, sc);
985 msg.iwlm_nstate = nstate;
988 return lwkt_domsg(&sc->sc_thread_port, &msg.iwlm_nmsg.nm_lmsg, 0);
992 iwl2100_newstate_dispatch(struct netmsg *nmsg)
994 struct iwlmsg *msg = (struct iwlmsg *)nmsg;
995 struct iwl2100_softc *sc = msg->iwlm_softc;
996 struct ieee80211com *ic = &sc->sc_ic;
998 struct ifnet *ifp = &ic->ic_if;
1000 enum ieee80211_state nstate, ostate;
1001 int arg = msg->iwlm_arg, error = 0;
1003 ASSERT_SERIALIZED(ifp->if_serializer);
1005 nstate = msg->iwlm_nstate;
1006 ostate = ic->ic_state;
1008 sc->sc_flags &= ~IWL2100_F_IFSTART;
1011 iwl2100_chan_change(sc, ic->ic_curchan);
1013 callout_stop(&sc->sc_ibss);
1014 iwl2100_restart_done(sc);
1016 if (nstate == IEEE80211_S_INIT)
1019 if (sc->sc_flags & IWL2100_F_DETACH) {
1021 * Except for INIT, we skip rest of the
1022 * state changes during detaching
1027 if (ic->ic_opmode == IEEE80211_M_STA) {
1028 if (nstate == IEEE80211_S_AUTH)
1029 error = iwl2100_auth(sc);
1030 else if (nstate == IEEE80211_S_RUN)
1031 sc->sc_flags |= IWL2100_F_IFSTART;
1032 } else if (ic->ic_opmode == IEEE80211_M_IBSS) {
1033 if (nstate == IEEE80211_S_RUN) {
1034 DPRINTF(sc, IWL2100_DBG_IBSS, "%s",
1035 "start/join ibss\n");
1038 * IWL2100_F_IFSTART can't be turned on
1039 * until BSSID generated by the firmware
1042 * XXX only if we started the IBSS
1044 error = iwl2100_ibss(sc);
1049 error = sc->sc_newstate(ic, nstate, arg);
1052 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1054 * Don't use 'nstate' here, since for IBSS
1055 * mode 802.11 layer may enter RUN state in
1056 * a recursive manner, i.e. when we reach
1057 * here, nstate != ic->ic_state
1059 if (ic->ic_state == IEEE80211_S_SCAN &&
1060 ic->ic_state != ostate) {
1061 DPRINTF(sc, IWL2100_DBG_SCAN, "%s",
1063 error = iwl2100_scan(sc);
1068 lwkt_replymsg(&nmsg->nm_lmsg, error);
1072 iwl2100_media_change(struct ifnet *ifp)
1076 ASSERT_SERIALIZED(ifp->if_serializer);
1078 error = ieee80211_media_change(ifp);
1079 if (error != ENETRESET)
1082 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
1083 iwl2100_init(ifp->if_softc);
1088 iwl2100_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1090 struct iwl2100_softc *sc = ifp->if_softc;
1092 if (sc->sc_flags & IWL2100_F_IFSTART) {
1093 struct ieee80211_node *ni = sc->sc_ic.ic_bss;
1097 txrate = iwl2100_read_ord1(sc, IWL2100_ORD1_TXRATE) & 0xf;
1098 if (ni->ni_rates.rs_nrates < 4)
1099 nrates = ni->ni_rates.rs_nrates;
1101 for (i = 0; i < nrates; ++i) {
1102 if ((1 << i) & txrate)
1106 ieee80211_media_status(ifp, imr);
1110 iwl2100_intr(void *xsc)
1112 struct iwl2100_softc *sc = xsc;
1113 struct ifnet *ifp = &sc->sc_ic.ic_if;
1114 uint32_t intr_status;
1116 ASSERT_SERIALIZED(ifp->if_serializer);
1118 if ((sc->sc_flags & IWL2100_F_INITED) == 0)
1121 intr_status = CSR_READ_4(sc, IWL2100_INTR_STATUS);
1122 if (intr_status == 0xffffffff) /* not for us */
1125 if ((intr_status & IWL2100_INTRS) == 0) /* not interested */
1128 sc->sc_flags |= IWL2100_F_IN_INTR;
1130 /* Disable interrupts */
1131 CSR_WRITE_4(sc, IWL2100_INTR_MASK, 0);
1133 if (intr_status & IWL2100_INTR_EFATAL) {
1134 uint32_t error_info;
1136 if_printf(ifp, "intr fatal error\n");
1137 CSR_WRITE_4(sc, IWL2100_INTR_STATUS, IWL2100_INTR_EFATAL);
1139 error_info = IND_READ_4(sc, IWL2100_IND_ERROR_INFO);
1140 IND_READ_4(sc, error_info & IWL2100_IND_ERRORADDR_MASK);
1142 callout_stop(&sc->sc_reinit);
1145 /* Leave interrupts disabled */
1149 if (intr_status & IWL2100_INTR_EPARITY) {
1150 if_printf(ifp, "intr parity error\n");
1151 CSR_WRITE_4(sc, IWL2100_INTR_STATUS, IWL2100_INTR_EPARITY);
1154 if (intr_status & IWL2100_INTR_RX) {
1155 CSR_WRITE_4(sc, IWL2100_INTR_STATUS, IWL2100_INTR_RX);
1160 if (intr_status & IWL2100_INTR_TX) {
1161 CSR_WRITE_4(sc, IWL2100_INTR_STATUS, IWL2100_INTR_TX);
1165 if (intr_status & IWL2100_INTR_FW_INITED)
1166 CSR_WRITE_4(sc, IWL2100_INTR_STATUS, IWL2100_INTR_FW_INITED);
1167 if (intr_status & IWL2100_INTR_CMD_DONE)
1168 CSR_WRITE_4(sc, IWL2100_INTR_STATUS, IWL2100_INTR_CMD_DONE);
1170 /* Enable interrupts */
1171 CSR_WRITE_4(sc, IWL2100_INTR_MASK, IWL2100_INTRS);
1173 sc->sc_flags &= ~IWL2100_F_IN_INTR;
1177 iwl2100_hw_reset(struct iwl2100_softc *sc)
1184 * - Make GPIO3 firmware writable
1188 CSR_WRITE_4(sc, IWL2100_GPIO,
1189 IWL2100_GPIO_3_EN | IWL2100_GPIO_3_FWWR |
1190 IWL2100_GPIO_1_EN | IWL2100_GPIO_LEDOFF);
1197 CSR_WRITE_4(sc, IWL2100_RESET, IWL2100_RESET_STOP_MASTER);
1198 for (i = 0; i < WAIT_MAX; ++i) {
1201 if (CSR_READ_4(sc, IWL2100_RESET) &
1202 IWL2100_RESET_MASTER_STOPPED)
1205 if (i == WAIT_MAX) {
1206 if_printf(&sc->sc_ic.ic_if, "can't stop master\n");
1212 CSR_WRITE_4(sc, IWL2100_RESET, IWL2100_RESET_SW);
1217 iwl2100_alloc_firmware(struct iwl2100_softc *sc, enum ieee80211_opmode opmode)
1222 enum ieee80211_opmode opmode;
1223 struct iwl2100_firmware *fw;
1225 { "", IWL2100_FW_M_STA, IEEE80211_M_STA,
1227 { "-i", IWL2100_FW_M_IBSS, IEEE80211_M_IBSS,
1229 { "-p", IWL2100_FW_M_MONITOR, IEEE80211_M_MONITOR,
1230 &sc->sc_fw_monitor },
1231 { NULL, 0, 0, NULL }
1233 struct ifnet *ifp = &sc->sc_ic.ic_if;
1234 const struct iwl2100_fwimg_hdr *hdr;
1235 struct iwl2100_firmware *fw = NULL;
1236 struct fw_image *image;
1240 for (i = 0; fw_arr[i].fw != NULL; ++i) {
1243 if (fw_arr[i].opmode == opmode) {
1244 if (fw->fw_image != NULL)
1250 KASSERT(fw_arr[i].fw != NULL, ("unsupported opmode %u", opmode));
1252 ksnprintf(filename, sizeof(filename), IWL2100_FW_PATH,
1256 * Release the serializer to avoid possible dead lock
1258 lwkt_serialize_exit(ifp->if_serializer);
1259 image = firmware_image_load(filename, NULL);
1260 lwkt_serialize_enter(ifp->if_serializer);
1264 fw->fw_image = image;
1271 hdr = (const struct iwl2100_fwimg_hdr *)image->fw_image;
1272 if ((hdr->version & 0xff) != 1) {
1273 if_printf(ifp, "%s unsupported firmware version %d",
1274 image->fw_name, hdr->version & 0xff);
1278 if (hdr->mode != fw_arr[i].mode) {
1279 if_printf(ifp, "%s contains %d mode firmware, should be %d\n",
1280 image->fw_name, hdr->mode, fw_arr[i].mode);
1284 if (hdr->data_size + hdr->ucode_size + sizeof(*hdr) !=
1287 "%s size mismatch, %zu/hdr %zu\n",
1288 image->fw_name, fw->fw_image->fw_imglen,
1289 hdr->data_size + hdr->ucode_size + sizeof(*hdr));
1293 fw->fw_data = (const uint8_t *)(hdr + 1);
1294 fw->fw_data_size = hdr->data_size;
1295 fw->fw_ucode = fw->fw_data + fw->fw_data_size;
1296 fw->fw_ucode_size = hdr->ucode_size;
1300 firmware_image_unload(fw->fw_image);
1301 bzero(fw, sizeof(*fw));
1307 iwl2100_free_firmware(struct iwl2100_softc *sc)
1309 struct iwl2100_firmware *fw_arr[] =
1310 { &sc->sc_fw_sta, &sc->sc_fw_ibss, &sc->sc_fw_monitor, NULL };
1313 for (i = 0; fw_arr[i] != NULL; ++i) {
1314 struct iwl2100_firmware *fw = fw_arr[i];
1316 if (fw->fw_image != NULL) {
1317 firmware_image_unload(fw->fw_image);
1318 bzero(fw, sizeof(*fw));
1324 iwl2100_load_firmware(struct iwl2100_softc *sc, enum ieee80211_opmode opmode)
1326 static const struct {
1330 { IWL2100_SHMEM0, IWL2100_SHMEM0_SIZE },
1331 { IWL2100_SHMEM1, IWL2100_SHMEM1_SIZE },
1332 { IWL2100_SHMEM2, IWL2100_SHMEM2_SIZE },
1333 { IWL2100_SHMEM3, IWL2100_SHMEM3_SIZE },
1334 { IWL2100_SHMEM_INTR, IWL2100_SHMEM_INTR_SIZE },
1337 const struct iwl2100_firmware *fw = NULL;
1341 * Pick up the firmware image corresponding to
1342 * the current operation mode
1345 case IEEE80211_M_STA:
1346 fw = &sc->sc_fw_sta;
1348 case IEEE80211_M_IBSS:
1349 fw = &sc->sc_fw_ibss;
1351 case IEEE80211_M_MONITOR:
1352 fw = &sc->sc_fw_monitor;
1355 panic("unsupported opmode %d", opmode);
1358 KASSERT(fw->fw_image != NULL,
1359 ("opmode %d firmware image is not allocated yet\n", opmode));
1362 error = iwl2100_load_fw_ucode(sc, fw);
1367 error = iwl2100_reset(sc);
1372 error = iwl2100_load_fw_data(sc, fw);
1376 /* Clear shared memory */
1377 for (i = 0; share_mem[i].size != 0; ++i) {
1378 uint32_t addr = share_mem[i].addr;
1381 for (j = 0; j < share_mem[i].size; j += 4)
1382 IND_WRITE_4(sc, addr + j, 0);
1388 #define IND_WRITE_FLUSH_2(sc, reg, val) \
1390 IND_WRITE_2((sc), (reg), (val)); \
1391 CSR_READ_4((sc), 0); \
1394 #define IND_WRITE_FLUSH_1(sc, reg, val) \
1396 IND_WRITE_1((sc), (reg), (val)); \
1397 CSR_READ_4((sc), 0); \
1400 /* XXX need more comment */
1402 iwl2100_load_fw_ucode(struct iwl2100_softc *sc,
1403 const struct iwl2100_firmware *fw)
1405 struct iwl2100_ucode_resp resp;
1410 IND_WRITE_4(sc, IWL2100_IND_HALT, IWL2100_IND_HALT_HOLD);
1412 /* Allow ARC to run */
1413 CSR_WRITE_4(sc, IWL2100_RESET, 0);
1415 IND_WRITE_FLUSH_2(sc, IWL2100_IND_CTRL, 0x703);
1416 IND_WRITE_FLUSH_2(sc, IWL2100_IND_CTRL, 0x707);
1418 IND_WRITE_FLUSH_1(sc, 0x210014, 0x72);
1419 IND_WRITE_FLUSH_1(sc, 0x210014, 0x72);
1421 IND_WRITE_FLUSH_1(sc, 0x210000, 0x40);
1422 IND_WRITE_FLUSH_1(sc, 0x210000, 0);
1423 IND_WRITE_FLUSH_1(sc, 0x210000, 0x40);
1426 for (i = 0; i < fw->fw_ucode_size; ++i, ++p)
1427 IND_WRITE_1(sc, 0x210010, *p);
1429 IND_WRITE_FLUSH_1(sc, 0x210000, 0);
1430 IND_WRITE_FLUSH_1(sc, 0x210000, 0);
1431 IND_WRITE_FLUSH_1(sc, 0x210000, 0x80);
1434 IND_WRITE_FLUSH_2(sc, IWL2100_IND_CTRL, 0x703);
1435 IND_WRITE_FLUSH_2(sc, IWL2100_IND_CTRL, 0x707);
1437 IND_WRITE_FLUSH_1(sc, 0x210014, 0x72);
1438 IND_WRITE_FLUSH_1(sc, 0x210014, 0x72);
1440 IND_WRITE_FLUSH_1(sc, 0x210000, 0);
1441 IND_WRITE_1(sc, 0x210000, 0x80);
1444 for (i = 0; i < WAIT_MAX; ++i) {
1447 if (IND_READ_1(sc, 0x210000) & 0x1)
1450 if (i == WAIT_MAX) {
1451 if_printf(&sc->sc_ic.ic_if,
1452 "wait ucode symbol init timed out\n");
1458 for (i = 0; i < WAIT_MAX; ++i) {
1459 uint16_t *r = (uint16_t *)&resp;
1462 for (j = 0; j < sizeof(resp) / 2; ++j, ++r)
1463 *r = IND_READ_2(sc, 0x210004);
1465 if (resp.cmd_id == 1 && resp.ucode_valid == 1)
1469 if (i == WAIT_MAX) {
1470 if_printf(&sc->sc_ic.ic_if,
1471 "wait ucode response timed out\n");
1477 IND_WRITE_4(sc, IWL2100_IND_HALT, 0);
1480 if_printf(&sc->sc_ic.ic_if, "ucode rev.%d date %d.%d.20%02d "
1481 "time %02d:%02d\n", resp.ucode_rev,
1482 resp.date_time[0], resp.date_time[1],
1483 resp.date_time[2], resp.date_time[3],
1490 #undef IND_WRITE_FLUSH_1
1491 #undef IND_WRITE_FLUSH_2
1494 iwl2100_load_fw_data(struct iwl2100_softc *sc,
1495 const struct iwl2100_firmware *fw)
1497 const uint8_t *p = fw->fw_data;
1500 while (w < fw->fw_data_size) {
1501 const struct iwl2100_fwdata_hdr *h;
1504 h = (const struct iwl2100_fwdata_hdr *)p;
1505 if (h->len > 32 || h->len == 0) {
1506 if_printf(&sc->sc_ic.ic_if,
1507 "firmware image data corrupted\n");
1510 if ((h->addr & 0x3) || (h->len & 0x3)) {
1511 if_printf(&sc->sc_ic.ic_if,
1512 "firmware image data with unaligned "
1513 "address %#x or length %#x\n",
1518 hlen = sizeof(*h) + h->len - 1;
1519 if (w + hlen > fw->fw_data_size) {
1520 if_printf(&sc->sc_ic.ic_if,
1521 "firmware image data size mismatch\n");
1525 CSR_WRITE_4(sc, IWL2100_AUTOINC_ADDR, h->addr);
1526 for (i = 0; i < h->len; i += 4) {
1527 CSR_WRITE_4(sc, IWL2100_AUTOINC_DATA,
1528 *(const uint32_t *)&h->data[i]);
1534 KKASSERT(w == fw->fw_data_size);
1540 iwl2100_free_tx_ring(struct iwl2100_softc *sc)
1542 struct iwl2100_tx_ring *tr = &sc->sc_txring;
1545 for (i = 0; i < IWL2100_TX_NDESC; ++i) {
1546 struct iwl2100_txbuf *tb = &tr->tr_buf[i];
1548 if (tb->tb_mbuf != NULL) {
1549 bus_dmamap_unload(sc->sc_mbuf_dtag, tb->tb_dmap);
1550 if (tb->tb_flags & IWL2100_TBF_CMDBUF) {
1551 KKASSERT(tb->tb_mbuf == sc->sc_cmd);
1552 tb->tb_flags &= ~IWL2100_TBF_CMDBUF;
1554 m_freem(tb->tb_mbuf);
1560 bzero(tr->tr_desc, IWL2100_TXRING_SIZE);
1561 bus_dmamap_sync(tr->tr_dtag, tr->tr_dmap, BUS_DMASYNC_PREWRITE);
1569 iwl2100_free_rx_ring(struct iwl2100_softc *sc)
1571 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
1574 for (i = 0; i < IWL2100_RX_NDESC; ++i) {
1575 struct iwl2100_rxbuf *rb = &rr->rr_buf[i];
1577 if (rb->rb_mbuf != NULL) {
1578 bus_dmamap_unload(sc->sc_mbuf_dtag, rb->rb_dmap);
1579 m_freem(rb->rb_mbuf);
1584 bzero(rr->rr_desc, IWL2100_RXRING_SIZE);
1585 bus_dmamap_sync(rr->rr_dtag, rr->rr_dmap, BUS_DMASYNC_PREWRITE);
1587 bzero(rr->rr_status, IWL2100_RXSTATUS_SIZE);
1588 bus_dmamap_sync(rr->rr_st_dtag, rr->rr_st_dmap, BUS_DMASYNC_PREWRITE);
1594 iwl2100_free_cmd(struct iwl2100_softc *sc)
1596 if (sc->sc_cmd != NULL) {
1597 m_freem(sc->sc_cmd);
1603 iwl2100_init_tx_ring(struct iwl2100_softc *sc)
1605 struct iwl2100_tx_ring *tr = &sc->sc_txring;
1611 bzero(tr->tr_desc, IWL2100_TXRING_SIZE);
1612 bus_dmamap_sync(tr->tr_dtag, tr->tr_dmap, BUS_DMASYNC_PREWRITE);
1614 CSR_WRITE_4(sc, IWL2100_TXQ_ADDR, tr->tr_paddr);
1615 CSR_WRITE_4(sc, IWL2100_TXQ_SIZE, IWL2100_TX_NDESC);
1616 CSR_WRITE_4(sc, IWL2100_TXQ_READ_IDX, 0);
1617 CSR_WRITE_4(sc, IWL2100_TXQ_WRITE_IDX, tr->tr_index);
1623 iwl2100_init_rx_ring(struct iwl2100_softc *sc)
1625 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
1628 for (i = 0; i < IWL2100_RX_NDESC; ++i) {
1629 error = iwl2100_newbuf(sc, i, 1);
1633 bus_dmamap_sync(rr->rr_st_dtag, rr->rr_st_dmap, BUS_DMASYNC_PREWRITE);
1634 bus_dmamap_sync(rr->rr_dtag, rr->rr_dmap, BUS_DMASYNC_PREWRITE);
1636 rr->rr_index = IWL2100_RX_NDESC - 1;
1638 CSR_WRITE_4(sc, IWL2100_RXQ_ADDR, rr->rr_paddr);
1639 CSR_WRITE_4(sc, IWL2100_RXQ_SIZE, IWL2100_RX_NDESC);
1640 CSR_WRITE_4(sc, IWL2100_RXQ_READ_IDX, 0);
1641 CSR_WRITE_4(sc, IWL2100_RXQ_WRITE_IDX, rr->rr_index);
1643 CSR_WRITE_4(sc, IWL2100_RX_STATUS_ADDR, rr->rr_st_paddr);
1649 iwl2100_alloc_cmd(struct iwl2100_softc *sc)
1651 KKASSERT(sc->sc_cmd == NULL);
1653 sc->sc_cmd = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
1654 if (sc->sc_cmd == NULL)
1660 iwl2100_newbuf(struct iwl2100_softc *sc, int buf_idx, int init)
1662 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
1663 struct iwl2100_rxbuf *rb;
1664 struct iwl_dmamap_ctx ctx;
1665 bus_dma_segment_t seg;
1670 KKASSERT(buf_idx < IWL2100_RX_NDESC);
1671 rb = &rr->rr_buf[buf_idx];
1673 m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
1678 if_printf(&sc->sc_ic.ic_if, "m_getcl failed\n");
1684 m->m_len = m->m_pkthdr.len = MCLBYTES;
1687 * Try load RX mbuf into temporary DMA map
1691 error = bus_dmamap_load_mbuf(sc->sc_mbuf_dtag, rr->rr_tmp_dmap, m,
1692 iwl_dma_buf_addr, &ctx,
1693 init ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT);
1694 if (error || ctx.nsegs == 0) {
1696 bus_dmamap_unload(sc->sc_mbuf_dtag, rr->rr_tmp_dmap);
1698 if_printf(&sc->sc_ic.ic_if, "too many segments?!\n");
1703 if_printf(&sc->sc_ic.ic_if, "can't load RX mbuf\n");
1711 bus_dmamap_unload(sc->sc_mbuf_dtag, rb->rb_dmap);
1713 rb->rb_paddr = seg.ds_addr;
1716 * Swap RX buf's DMA map with the loaded temporary one
1719 rb->rb_dmap = rr->rr_tmp_dmap;
1720 rr->rr_tmp_dmap = dmap;
1724 iwl2100_rxdesc_setup(sc, buf_idx);
1729 iwl2100_rxdesc_setup(struct iwl2100_softc *sc, int buf_idx)
1731 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
1732 struct iwl2100_rxbuf *rb;
1733 struct iwl2100_desc *d;
1734 struct iwl2100_rx_status *st;
1736 KKASSERT(buf_idx < IWL2100_RX_NDESC);
1737 rb = &rr->rr_buf[buf_idx];
1739 st = &rr->rr_status[buf_idx];
1740 bzero(st, sizeof(*st));
1742 d = &rr->rr_desc[buf_idx];
1743 bzero(d, sizeof(*d));
1744 d->d_paddr = rb->rb_paddr;
1745 d->d_len = MCLBYTES;
1749 iwl2100_init_firmware(struct iwl2100_softc *sc)
1752 struct ifnet *ifp = &sc->sc_ic.ic_if;
1757 ASSERT_SERIALIZED(ifp->if_serializer);
1759 CSR_WRITE_4(sc, IWL2100_GPIO,
1760 IWL2100_GPIO_3_EN | IWL2100_GPIO_3_FWWR |
1761 IWL2100_GPIO_1_EN | IWL2100_GPIO_LEDOFF);
1762 CSR_WRITE_4(sc, IWL2100_RESET, 0);
1765 * Wait for firmware to be initialized
1767 #define WAIT_MAX 5000
1769 for (i = 0; i < WAIT_MAX; ++i) {
1772 intr = CSR_READ_4(sc, IWL2100_INTR_STATUS);
1773 if (intr & IWL2100_INTR_FW_INITED) {
1774 CSR_WRITE_4(sc, IWL2100_INTR_STATUS,
1775 IWL2100_INTR_FW_INITED);
1778 if (intr & (IWL2100_INTR_EFATAL | IWL2100_INTR_EPARITY)) {
1779 CSR_WRITE_4(sc, IWL2100_INTR_STATUS,
1780 IWL2100_INTR_EFATAL | IWL2100_INTR_EPARITY);
1784 intr = CSR_READ_4(sc, IWL2100_INTR_STATUS) & IWL2100_INTRS;
1785 if (intr & CSR_READ_4(sc, IWL2100_INTR_MASK))
1786 CSR_WRITE_4(sc, IWL2100_INTR_STATUS, intr);
1788 if (i == WAIT_MAX) {
1789 if_printf(&sc->sc_ic.ic_if,
1790 "firmware initialization timed out\n");
1796 /* Enable GPIO1/3 and allow firmware to write to them */
1797 CSR_SETBITS_4(sc, IWL2100_GPIO,
1798 IWL2100_GPIO_1_EN | IWL2100_GPIO_1_FWWR |
1799 IWL2100_GPIO_3_EN | IWL2100_GPIO_3_FWWR);
1804 iwl2100_read_ord2(struct iwl2100_softc *sc, uint32_t ofs, void *buf0, int buflen)
1806 uint8_t *buf = buf0;
1807 uint32_t addr, info;
1811 #define IND_ALIGN_MASK 0x3
1813 addr = IND_READ_4(sc, sc->sc_ord2 + (ofs << 3));
1814 info = IND_READ_4(sc, sc->sc_ord2 + (ofs << 3) + sizeof(addr));
1816 len = info & 0xffff;
1819 if ((len * i) < buflen)
1823 i = addr & IND_ALIGN_MASK;
1824 addr &= ~IND_ALIGN_MASK;
1828 KKASSERT(i < IND_ALIGN);
1829 if (buflen + i < IND_ALIGN)
1835 CSR_WRITE_4(sc, IWL2100_IND_ADDR, addr);
1836 for (; i < lim; ++i, ++buf)
1837 *buf = CSR_READ_1(sc, IWL2100_IND_DATA + i);
1839 KKASSERT(buflen >= r);
1847 len = buflen & ~IND_ALIGN_MASK;
1848 buflen &= IND_ALIGN_MASK;
1851 CSR_WRITE_4(sc, IWL2100_AUTOINC_ADDR, addr);
1852 for (i = 0; i < len; i += 4, addr += 4, buf += 4) {
1853 *((uint32_t *)buf) =
1854 CSR_READ_4(sc, IWL2100_AUTOINC_DATA);
1858 CSR_WRITE_4(sc, IWL2100_IND_ADDR, addr);
1859 for (i = 0; i < buflen; ++i, ++buf)
1860 *buf = CSR_READ_1(sc, IWL2100_IND_DATA + i);
1866 #undef IND_ALIGN_MASK
1870 iwl2100_read_ord1(struct iwl2100_softc *sc, uint32_t ofs)
1874 addr = IND_READ_4(sc, sc->sc_ord1 + (ofs << 2));
1875 return IND_READ_4(sc, addr);
1879 iwl2100_write_ord1(struct iwl2100_softc *sc, uint32_t ofs, uint32_t val)
1883 addr = IND_READ_4(sc, sc->sc_ord1 + (ofs << 2));
1884 IND_WRITE_4(sc, addr, val);
1888 iwl2100_rfkilled(struct iwl2100_softc *sc)
1892 if ((sc->sc_caps & IWL2100_C_RFKILL) == 0)
1897 for (i = 0; i < TEST_MAX; ++i) {
1900 if (CSR_READ_4(sc, IWL2100_GPIO) & IWL2100_GPIO_RFKILLED)
1903 if (i != TEST_MAX) {
1904 if_printf(&sc->sc_ic.ic_if, "RF killed\n");
1914 iwl2100_set_addr(struct iwl2100_softc *sc, const uint8_t *eaddr)
1916 struct iwl2100_cmd *cmd;
1919 if (sc->sc_flags & IWL2100_F_WAITCMD) {
1920 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
1924 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
1925 bzero(cmd, sizeof(*cmd));
1927 cmd->c_cmd = IWL2100_CMD_SET_ADDR;
1928 cmd->c_param_len = IEEE80211_ADDR_LEN;
1929 IEEE80211_ADDR_COPY(cmd->c_param, eaddr);
1931 error = iwl2100_wait_cmd(sc);
1933 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
1940 iwl2100_set_opmode(struct iwl2100_softc *sc, enum ieee80211_opmode opmode)
1942 struct iwl2100_cmd *cmd;
1945 if (sc->sc_flags & IWL2100_F_WAITCMD) {
1946 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
1950 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
1951 bzero(cmd, sizeof(*cmd));
1953 cmd->c_cmd = IWL2100_CMD_SET_OPMODE;
1954 cmd->c_param_len = sizeof(cmd->c_param[0]);
1956 case IEEE80211_M_STA:
1957 cmd->c_param[0] = IWL2100_OPMODE_STA;
1959 case IEEE80211_M_IBSS:
1960 cmd->c_param[0] = IWL2100_OPMODE_IBSS;
1962 case IEEE80211_M_MONITOR:
1963 /* YYY ipw2100 leave this unset */
1964 cmd->c_param[0] = IWL2100_OPMODE_MONITOR;
1967 panic("unsupported opmode %d", opmode);
1971 error = iwl2100_wait_cmd(sc);
1973 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
1980 iwl2100_set_80211(struct iwl2100_softc *sc)
1982 struct ieee80211com *ic = &sc->sc_ic;
1983 struct iwl2100_cmd *cmd;
1986 if (sc->sc_flags & IWL2100_F_WAITCMD) {
1987 if_printf(&ic->ic_if, "there is command pending\n");
1991 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
1992 bzero(cmd, sizeof(*cmd));
1994 cmd->c_cmd = IWL2100_CMD_SET_80211;
1995 cmd->c_param_len = sizeof(cmd->c_param[0]) * 3;
1996 cmd->c_param[0] = IWL2100_CFG_IBSS | IWL2100_CFG_STA |
1997 IWL2100_CFG_8021X | IWL2100_CFG_AUTO_PREAMBLE;
1998 if (ic->ic_opmode == IEEE80211_M_IBSS)
1999 cmd->c_param[0] |= IWL2100_CFG_IBSS_AUTO_START;
2000 else if (ic->ic_opmode == IEEE80211_M_MONITOR) /* YYY not ipw2100 */
2001 cmd->c_param[0] |= IWL2100_CFG_MONITOR;
2002 cmd->c_param[1] = IWL2100_CFG_CHANMASK; /* XXX sc->sc_bss_chans */
2003 cmd->c_param[2] = IWL2100_CFG_CHANMASK; /* YYY sc->sc_ibss_chans */
2005 error = iwl2100_wait_cmd(sc);
2007 if_printf(&ic->ic_if, "%s failed\n", __func__);
2014 iwl2100_set_basicrates(struct iwl2100_softc *sc)
2016 struct iwl2100_cmd *cmd;
2019 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2020 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2024 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2025 bzero(cmd, sizeof(*cmd));
2028 * This configuration does not seem to have any effects
2029 * on probe-req and assoc-req frames.
2031 cmd->c_cmd = IWL2100_CMD_SET_BASICRATES;
2032 cmd->c_param_len = sizeof(cmd->c_param[0]);
2033 cmd->c_param[0] = 0x3; /* 1Mbps and 2Mbps. XXX from caller */
2035 error = iwl2100_wait_cmd(sc);
2037 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2044 iwl2100_set_txrates(struct iwl2100_softc *sc)
2046 struct ieee80211com *ic = &sc->sc_ic;
2047 struct iwl2100_cmd *cmd;
2051 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2052 if_printf(&ic->ic_if, "there is command pending\n");
2056 /* Calculate TX rate mask. XXX let caller do this */
2057 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE)
2058 rate_mask = 1 << ic->ic_fixed_rate;
2060 rate_mask = 0xf; /* all 11b rates */
2061 KKASSERT((rate_mask & ~0xf) == 0);
2066 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2067 bzero(cmd, sizeof(*cmd));
2069 cmd->c_cmd = IWL2100_CMD_SET_TXRATES;
2070 cmd->c_param_len = sizeof(cmd->c_param[0]);
2071 cmd->c_param[0] = rate_mask;
2073 error = iwl2100_wait_cmd(sc);
2075 if_printf(&ic->ic_if, "%s failed\n", __func__);
2082 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2083 bzero(cmd, sizeof(*cmd));
2085 cmd->c_cmd = IWL2100_CMD_SET_MSDU_TXRATES;
2086 cmd->c_param_len = sizeof(cmd->c_param[0]);
2087 cmd->c_param[0] = rate_mask;
2089 error = iwl2100_wait_cmd(sc);
2091 if_printf(&ic->ic_if, "%s failed\n", __func__);
2098 iwl2100_set_powersave(struct iwl2100_softc *sc, int on)
2100 struct iwl2100_cmd *cmd;
2103 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2104 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2108 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2109 bzero(cmd, sizeof(*cmd));
2111 cmd->c_cmd = IWL2100_CMD_SET_POWERSAVE;
2112 cmd->c_param_len = sizeof(cmd->c_param[0]);
2113 cmd->c_param[0] = on; /* XXX power level? */
2115 error = iwl2100_wait_cmd(sc);
2117 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2124 iwl2100_set_rtsthreshold(struct iwl2100_softc *sc, uint16_t rtsthreshold)
2126 struct iwl2100_cmd *cmd;
2129 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2130 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2134 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2135 bzero(cmd, sizeof(*cmd));
2137 cmd->c_cmd = IWL2100_CMD_SET_RTSTHRESHOLD;
2138 cmd->c_param_len = sizeof(cmd->c_param[0]);
2139 if (rtsthreshold == IEEE80211_RTS_MAX) {
2140 /* Disable RTS threshold */
2141 cmd->c_param[0] = IWL2100_RTS_MAX;
2143 if (rtsthreshold >= IWL2100_RTS_MAX)
2144 rtsthreshold = IWL2100_RTS_MAX - 1;
2145 cmd->c_param[0] = rtsthreshold;
2148 error = iwl2100_wait_cmd(sc);
2150 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2157 iwl2100_set_bssid(struct iwl2100_softc *sc, const uint8_t *bssid)
2159 struct iwl2100_cmd *cmd;
2162 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2163 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2167 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2168 bzero(cmd, sizeof(*cmd));
2170 cmd->c_cmd = IWL2100_CMD_SET_BSSID;
2171 if (bssid != NULL) {
2172 cmd->c_param_len = IEEE80211_ADDR_LEN;
2173 IEEE80211_ADDR_COPY(cmd->c_param, bssid);
2176 error = iwl2100_wait_cmd(sc);
2178 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2185 iwl2100_set_essid(struct iwl2100_softc *sc, const uint8_t *essid, int essid_len)
2187 struct iwl2100_cmd *cmd;
2190 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2191 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2195 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2196 bzero(cmd, sizeof(*cmd));
2198 cmd->c_cmd = IWL2100_CMD_SET_ESSID;
2199 if (essid != NULL) {
2200 KKASSERT(essid_len <= sizeof(cmd->c_param));
2201 cmd->c_param_len = essid_len;
2203 bcopy(essid, cmd->c_param, essid_len);
2206 error = iwl2100_wait_cmd(sc);
2208 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2215 iwl2100_set_auth_ciphers(struct iwl2100_softc *sc,
2216 enum ieee80211_authmode authmode)
2218 struct iwl2100_cmdparam_sec *sec;
2219 struct iwl2100_cmd *cmd;
2222 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2223 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2227 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2228 bzero(cmd, sizeof(*cmd));
2230 cmd->c_cmd = IWL2100_CMD_SET_SECURITY;
2231 cmd->c_param_len = sizeof(*sec);
2232 sec = (struct iwl2100_cmdparam_sec *)cmd->c_param;
2234 sec->sec_cipher_mask = IWL2100_CIPHER_NONE |
2235 IWL2100_CIPHER_WEP40 |
2236 IWL2100_CIPHER_TKIP |
2237 IWL2100_CIPHER_CCMP |
2238 IWL2100_CIPHER_WEP104;
2239 if (authmode == IEEE80211_AUTH_SHARED)
2240 sec->sec_authmode = IWL2100_AUTH_SHARED;
2242 sec->sec_authmode = IWL2100_AUTH_OPEN;
2244 error = iwl2100_wait_cmd(sc);
2246 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2253 iwl2100_set_wepkey(struct iwl2100_softc *sc, const struct ieee80211_key *k)
2255 struct iwl2100_cmdparam_wepkey *key;
2256 struct iwl2100_cmd *cmd;
2259 if (k->wk_keylen > IWL2100_KEYDATA_SIZE)
2262 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2263 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2267 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2268 bzero(cmd, sizeof(*cmd));
2270 cmd->c_cmd = IWL2100_CMD_SET_WEPKEY;
2271 cmd->c_param_len = sizeof(*key);
2272 key = (struct iwl2100_cmdparam_wepkey *)cmd->c_param;
2273 key->key_index = k->wk_keyix;
2274 key->key_len = k->wk_keylen;
2275 bcopy(k->wk_key, key->key_data, key->key_len);
2277 error = iwl2100_wait_cmd(sc);
2279 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2286 iwl2100_set_weptxkey(struct iwl2100_softc *sc, ieee80211_keyix txkey)
2288 struct iwl2100_cmd *cmd;
2291 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2292 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2296 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2297 bzero(cmd, sizeof(*cmd));
2299 cmd->c_cmd = IWL2100_CMD_SET_WEPTXKEY;
2300 cmd->c_param_len = sizeof(cmd->c_param[0]);
2301 cmd->c_param[0] = txkey;
2303 error = iwl2100_wait_cmd(sc);
2305 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2312 iwl2100_set_privacy(struct iwl2100_softc *sc, int on)
2314 struct iwl2100_cmd *cmd;
2317 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2318 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2322 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2323 bzero(cmd, sizeof(*cmd));
2325 cmd->c_cmd = IWL2100_CMD_SET_PRIVACY;
2326 cmd->c_param_len = sizeof(cmd->c_param[0]);
2327 cmd->c_param[0] = on ? IWL2100_PRIVACY_ENABLE : 0;
2329 error = iwl2100_wait_cmd(sc);
2331 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2338 iwl2100_wait_cmd(struct iwl2100_softc *sc)
2340 struct ifnet *ifp = &sc->sc_ic.ic_if;
2341 struct iwl2100_tx_ring *tr = &sc->sc_txring;
2342 struct mbuf *m = sc->sc_cmd;
2343 struct iwl_dmamap_ctx ctx;
2344 bus_dma_segment_t seg;
2345 struct iwl2100_desc *d;
2346 struct iwl2100_txbuf *tb;
2349 ASSERT_SERIALIZED(ifp->if_serializer);
2351 KKASSERT(tr->tr_index < IWL2100_TX_NDESC);
2352 tb = &tr->tr_buf[tr->tr_index];
2356 error = bus_dmamap_load_mbuf(sc->sc_mbuf_dtag, tb->tb_dmap, m,
2357 iwl_dma_buf_addr, &ctx, BUS_DMA_WAITOK);
2358 if (error || ctx.nsegs == 0) {
2360 bus_dmamap_unload(sc->sc_mbuf_dtag, tb->tb_dmap);
2362 if_printf(ifp, "too many segments?!\n");
2365 if_printf(ifp, "can't load RX mbuf\n");
2368 tb->tb_mbuf = sc->sc_cmd;
2369 tb->tb_flags |= IWL2100_TBF_CMDBUF;
2371 d = &tr->tr_desc[tr->tr_index];
2372 d->d_paddr = seg.ds_addr;
2373 d->d_len = sizeof(struct iwl2100_cmd);
2375 d->d_flags = IWL2100_TXD_F_INTR | IWL2100_TXD_F_CMD;
2377 KKASSERT(tr->tr_used < IWL2100_TX_NDESC);
2379 tr->tr_index = (tr->tr_index + 1) % IWL2100_TX_NDESC;
2381 bus_dmamap_sync(tr->tr_dtag, tr->tr_dmap, BUS_DMASYNC_PREWRITE);
2383 CSR_WRITE_4(sc, IWL2100_TXQ_WRITE_IDX, tr->tr_index);
2385 if (sc->sc_flags & IWL2100_F_IN_INTR)
2386 panic("sleep in interrupt thread");
2388 sc->sc_flags |= IWL2100_F_WAITCMD;
2389 error = zsleep(sc, ifp->if_serializer, 0, "iwlcmd", 2 * hz);
2391 sc->sc_flags &= ~IWL2100_F_WAITCMD;
2392 if (sc->sc_flags & IWL2100_F_ERROR) {
2393 if_printf(ifp, "error happened when waiting "
2394 "command to be done\n");
2402 iwl2100_rxeof(struct iwl2100_softc *sc)
2404 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
2405 struct ifnet *ifp = &sc->sc_ic.ic_if;
2408 hwidx = CSR_READ_4(sc, IWL2100_RXQ_READ_IDX);
2409 CSR_READ_4(sc, IWL2100_RXQ_WRITE_IDX);
2411 if (hwidx >= IWL2100_RX_NDESC) {
2412 if_printf(ifp, "invalid hardware RX index %d\n", hwidx);
2416 KKASSERT(rr->rr_index < IWL2100_RX_NDESC);
2417 i = (rr->rr_index + 1) % IWL2100_RX_NDESC;
2418 while (hwidx != i) {
2419 struct iwl2100_rx_status *st = &rr->rr_status[i];
2420 struct iwl2100_rxbuf *rb = &rr->rr_buf[i];
2423 bus_dmamap_sync(rr->rr_st_dtag, rr->rr_st_dmap,
2424 BUS_DMASYNC_POSTREAD);
2425 frame_type = st->r_status & IWL2100_RXS_TYPE_MASK;
2427 bus_dmamap_sync(sc->sc_mbuf_dtag, rb->rb_dmap,
2428 BUS_DMASYNC_POSTREAD);
2429 switch (frame_type) {
2430 case IWL2100_RXS_TYPE_CMD:
2431 iwl2100_rxeof_cmd(sc, i);
2434 case IWL2100_RXS_TYPE_STATUS:
2435 iwl2100_rxeof_status(sc, i);
2438 case IWL2100_RXS_TYPE_NOTE:
2439 iwl2100_rxeof_note(sc, i);
2442 case IWL2100_RXS_TYPE_DATA:
2443 case IWL2100_RXS_TYPE_DATA1:
2444 iwl2100_rxeof_data(sc, i);
2448 if_printf(ifp, "unknown frame type: %d\n", frame_type);
2449 iwl2100_rxdesc_setup(sc, i);
2452 i = (i + 1) % IWL2100_RX_NDESC;
2454 bus_dmamap_sync(rr->rr_st_dtag, rr->rr_st_dmap, BUS_DMASYNC_POSTREAD);
2455 bus_dmamap_sync(rr->rr_dtag, rr->rr_dmap, BUS_DMASYNC_POSTREAD);
2458 rr->rr_index = IWL2100_RX_NDESC - 1;
2460 rr->rr_index = i - 1;
2461 CSR_WRITE_4(sc, IWL2100_RXQ_WRITE_IDX, rr->rr_index);
2465 iwl2100_txeof(struct iwl2100_softc *sc)
2467 struct iwl2100_tx_ring *tr = &sc->sc_txring;
2468 struct ifnet *ifp = &sc->sc_ic.ic_if;
2471 hwidx = CSR_READ_4(sc, IWL2100_TXQ_READ_IDX);
2472 CSR_READ_4(sc, IWL2100_TXQ_WRITE_IDX);
2473 if (hwidx >= IWL2100_TX_NDESC) {
2474 if_printf(ifp, "invalid hardware TX index %d\n", hwidx);
2478 KKASSERT(tr->tr_coll < IWL2100_TX_NDESC);
2479 while (tr->tr_used) {
2480 struct iwl2100_txbuf *tb;
2482 if (tr->tr_coll == hwidx)
2485 tb = &tr->tr_buf[tr->tr_coll];
2486 if (tb->tb_mbuf == NULL)
2489 bus_dmamap_unload(sc->sc_mbuf_dtag, tb->tb_dmap);
2490 if (tb->tb_flags & IWL2100_TBF_CMDBUF) {
2491 tb->tb_flags &= ~IWL2100_TBF_CMDBUF;
2492 KKASSERT(tb->tb_mbuf == sc->sc_cmd);
2494 m_freem(tb->tb_mbuf);
2498 tr->tr_coll = (tr->tr_coll + 1) % IWL2100_TX_NDESC;
2500 KKASSERT(tr->tr_used > 0);
2504 if (tr->tr_used < IWL2100_TX_USED_MAX) {
2505 if (tr->tr_used == 0) {
2506 KKASSERT(tr->tr_coll == tr->tr_index);
2507 sc->sc_tx_timer = 0;
2510 ifp->if_flags &= ~IFF_OACTIVE;
2516 iwl2100_config(struct iwl2100_softc *sc, const uint8_t *bssid,
2517 const uint8_t *essid, uint8_t esslen, int ibss_chan)
2519 struct ieee80211com *ic = &sc->sc_ic;
2520 struct ifnet *ifp = &ic->ic_if;
2523 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
2524 error = iwl2100_set_chan(sc, ic->ic_curchan);
2526 if_printf(ifp, "can't set mon channel\n");
2531 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2532 error = iwl2100_set_addr(sc, ic->ic_myaddr);
2534 if_printf(ifp, "can't set MAC address\n");
2538 error = iwl2100_set_opmode(sc, ic->ic_opmode);
2540 if_printf(ifp, "can't set opmode\n");
2545 KKASSERT(ic->ic_opmode == IEEE80211_M_IBSS);
2546 error = iwl2100_set_chan(sc, ic->ic_curchan);
2548 if_printf(ifp, "can't set ibss channel\n");
2553 error = iwl2100_set_80211(sc);
2555 if_printf(ifp, "can't set 802.11 config\n");
2559 error = iwl2100_set_basicrates(sc);
2561 if_printf(ifp, "can't set basicrates\n");
2565 error = iwl2100_set_txrates(sc);
2567 if_printf(ifp, "can't set TX rates\n");
2571 error = iwl2100_set_powersave(sc, ic->ic_flags & IEEE80211_F_PMGTON);
2573 if_printf(ifp, "can't turn off powersave\n");
2577 error = iwl2100_set_rtsthreshold(sc, ic->ic_rtsthreshold);
2579 if_printf(ifp, "can't set RTS threshold\n");
2583 error = iwl2100_set_bssid(sc, bssid);
2585 if_printf(ifp, "can't set bssid\n");
2589 error = iwl2100_set_essid(sc, essid, esslen);
2591 if_printf(ifp, "can't set essid\n");
2595 error = iwl2100_set_auth_ciphers(sc, ic->ic_bss->ni_authmode);
2597 if_printf(ifp, "can't set authmode and ciphers\n");
2601 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2602 ieee80211_keyix txkey = IEEE80211_KEYIX_NONE;
2605 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
2606 const struct ieee80211_key *k = &ic->ic_nw_keys[i];
2608 if (k->wk_keyix == IEEE80211_KEYIX_NONE)
2611 error = iwl2100_set_wepkey(sc, k);
2612 if (error == E2BIG) {
2615 if_printf(ifp, "can't set wepkey\n");
2618 txkey = k->wk_keyix;
2621 if (txkey != IEEE80211_KEYIX_NONE) {
2623 * Found some valid WEP keys.
2625 * If WEP TX key index from 802.11 layer is not
2626 * set, then use the first valid WEP key as TX
2629 if (ic->ic_def_txkey != IEEE80211_KEYIX_NONE)
2630 txkey = ic->ic_def_txkey;
2632 error = iwl2100_set_weptxkey(sc, txkey);
2634 if_printf(ifp, "can't set weptxkey\n");
2640 error = iwl2100_set_privacy(sc, ic->ic_flags & IEEE80211_F_PRIVACY);
2642 if_printf(ifp, "can't set privacy\n");
2646 error = iwl2100_set_optie(sc, ic->ic_opt_ie, ic->ic_opt_ie_len);
2648 if (error != E2BIG) {
2649 if_printf(ifp, "can't set opt ie\n");
2654 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2655 error = iwl2100_set_bintval(sc, ic->ic_bss->ni_intval);
2657 if_printf(ifp, "can't set bintval\n");
2661 error = iwl2100_set_txpower(sc, 32 /* XXX */);
2663 if_printf(ifp, "can't set txpwr\n");
2671 iwl2100_config_op(struct iwl2100_softc *sc, uint32_t op)
2673 struct iwl2100_cmd *cmd;
2676 KASSERT(op == IWL2100_CMD_CONF_DONE || op == IWL2100_CMD_CONF_START,
2677 ("unknown config_op %u", op));
2679 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2680 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2684 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2685 bzero(cmd, sizeof(*cmd));
2688 error = iwl2100_wait_cmd(sc);
2690 if_printf(&sc->sc_ic.ic_if, "%s(%u) failed\n", __func__, op);
2694 iwl2100_read_ord1(sc, IWL2100_ORD1_CONF_START); /* dummy read */
2699 iwl2100_set_chan(struct iwl2100_softc *sc, const struct ieee80211_channel *c)
2701 struct ieee80211com *ic = &sc->sc_ic;
2702 struct iwl2100_cmd *cmd;
2706 KKASSERT(ic->ic_opmode != IEEE80211_M_STA);
2708 chan = ieee80211_chan2ieee(ic, c);
2709 if (chan == IEEE80211_CHAN_ANY) {
2710 if_printf(&ic->ic_if, "invalid channel!\n");
2714 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2715 if_printf(&ic->ic_if, "there is command pending\n");
2719 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2720 bzero(cmd, sizeof(*cmd));
2722 cmd->c_cmd = IWL2100_CMD_SET_CHAN;
2723 cmd->c_param_len = sizeof(cmd->c_param[0]);
2724 cmd->c_param[0] = chan;
2726 error = iwl2100_wait_cmd(sc);
2728 if_printf(&ic->ic_if, "%s failed\n", __func__);
2735 iwl2100_set_scanopt(struct iwl2100_softc *sc, uint32_t chans, uint32_t flags)
2737 struct ieee80211com *ic = &sc->sc_ic;
2738 struct iwl2100_cmd *cmd;
2741 KKASSERT(ic->ic_opmode != IEEE80211_M_MONITOR);
2743 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2744 if_printf(&ic->ic_if, "there is command pending\n");
2748 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2749 bzero(cmd, sizeof(*cmd));
2753 * 1) IWL2100_SCANOPT_NOASSOC is ignored by firmware, but same
2754 * function could be achieved by clearing bssid.
2755 * 2) Channel mask is ignored by firmware, if NIC is in STA opmode.
2757 * We leave the correct configuration here just with the hope
2758 * that one day firmware could do better.
2760 cmd->c_cmd = IWL2100_CMD_SET_SCANOPT;
2761 cmd->c_param_len = sizeof(cmd->c_param[0]) * 2;
2762 cmd->c_param[0] = flags | IWL2100_SCANOPT_MIXED;
2763 cmd->c_param[1] = chans;
2765 error = iwl2100_wait_cmd(sc);
2767 if_printf(&ic->ic_if, "%s failed\n", __func__);
2774 iwl2100_set_scan(struct iwl2100_softc *sc)
2776 struct ieee80211com *ic = &sc->sc_ic;
2777 struct iwl2100_cmd *cmd;
2780 KKASSERT(ic->ic_opmode != IEEE80211_M_MONITOR);
2782 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2783 if_printf(&ic->ic_if, "there is command pending\n");
2787 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2788 bzero(cmd, sizeof(*cmd));
2790 cmd->c_cmd = IWL2100_CMD_SCAN;
2791 cmd->c_param_len = sizeof(cmd->c_param[0]);
2793 error = iwl2100_wait_cmd(sc);
2795 if_printf(&ic->ic_if, "%s failed\n", __func__);
2802 iwl2100_set_optie(struct iwl2100_softc *sc, void *optie, uint16_t optie_len)
2804 struct iwl2100_cmd *cmd;
2805 struct iwl2100_cmdparam_ie *ie;
2808 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2809 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2813 if (optie_len > IWL2100_OPTIE_MAX) {
2814 if_printf(&sc->sc_ic.ic_if, "optie too long\n");
2818 if (optie == NULL || optie_len == 0)
2821 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2822 bzero(cmd, sizeof(*cmd));
2824 cmd->c_cmd = IWL2100_CMD_SET_IE;
2825 cmd->c_param_len = sizeof(*ie);
2826 ie = (struct iwl2100_cmdparam_ie *)cmd->c_param;
2827 ie->ie_optlen = optie_len;
2828 bcopy(optie, ie->ie_opt, optie_len);
2830 error = iwl2100_wait_cmd(sc);
2832 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2839 iwl2100_set_bintval(struct iwl2100_softc *sc, uint16_t bintval)
2841 struct iwl2100_cmd *cmd;
2844 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2845 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2849 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2850 bzero(cmd, sizeof(*cmd));
2852 cmd->c_cmd = IWL2100_CMD_SET_BINTVAL;
2853 cmd->c_param_len = sizeof(cmd->c_param[0]);
2854 cmd->c_param[0] = bintval;
2856 error = iwl2100_wait_cmd(sc);
2858 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2865 iwl2100_set_txpower(struct iwl2100_softc *sc, uint16_t txpower)
2867 struct iwl2100_cmd *cmd;
2870 if (sc->sc_flags & IWL2100_F_WAITCMD) {
2871 if_printf(&sc->sc_ic.ic_if, "there is command pending\n");
2875 cmd = mtod(sc->sc_cmd, struct iwl2100_cmd *);
2876 bzero(cmd, sizeof(*cmd));
2878 cmd->c_cmd = IWL2100_CMD_SET_TXPOWER;
2879 cmd->c_param_len = sizeof(cmd->c_param[0]);
2880 cmd->c_param[0] = txpower;
2882 error = iwl2100_wait_cmd(sc);
2884 if_printf(&sc->sc_ic.ic_if, "%s failed\n", __func__);
2891 iwl2100_rxeof_status(struct iwl2100_softc *sc, int i)
2893 struct ieee80211com *ic = &sc->sc_ic;
2894 struct ifnet *ifp = &ic->ic_if;
2895 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
2896 struct iwl2100_rx_status *st = &rr->rr_status[i];
2897 struct iwl2100_rxbuf *rb = &rr->rr_buf[i];
2898 struct mbuf *m = rb->rb_mbuf;
2901 if (st->r_len != sizeof(status)) {
2902 if_printf(ifp, "invalid status frame len %u\n", st->r_len);
2906 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2909 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0)
2910 sc->sc_flags &= ~IWL2100_F_SCANNING;
2912 status = *mtod(m, uint32_t *);
2913 DPRINTF(sc, IWL2100_DBG_STATUS, "status 0x%08x\n", status);
2916 case IWL2100_STATUS_SCANDONE:
2917 if (ic->ic_flags & IEEE80211_F_SCAN) {
2919 * To make sure that firmware has iterated all
2920 * of the channels, we wait for the second scan
2921 * done status change.
2923 if (sc->sc_flags & IWL2100_F_SCANNING) {
2924 iwlmsg_send(&sc->sc_scanend_msg,
2925 &sc->sc_thread_port);
2927 sc->sc_flags |= IWL2100_F_SCANNING;
2932 case IWL2100_STATUS_RUNNING:
2933 iwl2100_restart_done(sc);
2934 if (ic->ic_state == IEEE80211_S_ASSOC) {
2935 KKASSERT(ic->ic_opmode == IEEE80211_M_STA);
2936 iwlmsg_send(&sc->sc_run_msg, &sc->sc_thread_port);
2937 } else if (ic->ic_state == IEEE80211_S_RUN) {
2938 if (ic->ic_opmode == IEEE80211_M_STA) {
2939 DPRINTF(sc, IWL2100_DBG_RESTART, "%s",
2941 sc->sc_flags |= IWL2100_F_IFSTART;
2944 KKASSERT(ic->ic_opmode == IEEE80211_M_IBSS);
2945 callout_reset(&sc->sc_ibss, (100 * hz) / 1000,
2946 iwl2100_ibss_bssid, sc);
2951 case IWL2100_STATUS_BMISS:
2952 if (ic->ic_opmode == IEEE80211_M_STA) {
2953 DPRINTF(sc, IWL2100_DBG_SCAN, "%s", "bmiss\n");
2954 iwlmsg_send(&sc->sc_bmiss_msg, &sc->sc_thread_port);
2958 case IWL2100_STATUS_SCANNING:
2959 if (ic->ic_opmode == IEEE80211_M_STA &&
2960 ic->ic_state == IEEE80211_S_RUN) {
2961 /* Firmware error happens */
2962 iwl2100_restart(sc);
2967 iwl2100_rxdesc_setup(sc, i);
2971 iwl2100_rxeof_note(struct iwl2100_softc *sc, int i)
2973 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
2974 struct iwl2100_rx_status *st = &rr->rr_status[i];
2975 struct iwl2100_rxbuf *rb = &rr->rr_buf[i];
2976 struct mbuf *m = rb->rb_mbuf;
2977 struct ieee80211com *ic = &sc->sc_ic;
2978 struct iwl2100_note *note;
2980 if (st->r_len < sizeof(*note)) {
2981 if_printf(&ic->ic_if, "invalid note frame len %u\n", st->r_len);
2985 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2988 note = mtod(m, struct iwl2100_note *);
2989 DPRINTF(sc, IWL2100_DBG_NOTE, "note subtype %u, size %u\n",
2990 note->nt_subtype, note->nt_size);
2992 if (note->nt_subtype == 19 /* XXX */ &&
2993 ic->ic_state == IEEE80211_S_AUTH) {
2994 KKASSERT(ic->ic_opmode == IEEE80211_M_STA);
2995 iwlmsg_send(&sc->sc_assoc_msg, &sc->sc_thread_port);
2998 iwl2100_rxdesc_setup(sc, i);
3002 iwl2100_rxeof_cmd(struct iwl2100_softc *sc, int i)
3004 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
3005 struct iwl2100_rx_status *st = &rr->rr_status[i];
3006 struct iwl2100_rxbuf *rb = &rr->rr_buf[i];
3007 struct mbuf *m = rb->rb_mbuf;
3008 struct iwl2100_cmd *cmd;
3010 if (st->r_len != sizeof(*cmd)) {
3011 if_printf(&sc->sc_ic.ic_if,
3012 "invalid cmd done frame len %u\n", st->r_len);
3016 cmd = mtod(m, struct iwl2100_cmd *);
3017 DPRINTF(sc, IWL2100_DBG_CMD, "cmd %u\n", cmd->c_cmd);
3018 if (cmd->c_cmd == 0)
3019 sc->sc_flags |= IWL2100_F_ZERO_CMD;
3022 iwl2100_rxdesc_setup(sc, i);
3026 iwl2100_rxeof_data(struct iwl2100_softc *sc, int i)
3028 struct ieee80211com *ic = &sc->sc_ic;
3029 struct ifnet *ifp = &ic->ic_if;
3030 struct iwl2100_rx_ring *rr = &sc->sc_rxring;
3031 struct iwl2100_rx_status *st = &rr->rr_status[i];
3032 struct iwl2100_rxbuf *rb = &rr->rr_buf[i];
3033 struct mbuf *m = rb->rb_mbuf;
3034 struct ieee80211_frame_min *wh;
3035 struct ieee80211_node *ni;
3036 int frame_len, rssi;
3037 const struct ieee80211_channel *c;
3040 * Gather all necessary information from status ring _here_,
3041 * since the following iwl2100_newbuf() will clear them out.
3044 frame_len = st->r_len;
3046 if (iwl2100_newbuf(sc, i, 0)) {
3053 m->m_pkthdr.rcvif = ifp;
3054 m->m_len = m->m_pkthdr.len = frame_len;
3056 wh = mtod(m, struct ieee80211_frame_min *);
3057 ni = ieee80211_find_rxnode(ic, wh);
3062 if (sc->sc_drvbpf != NULL) {
3063 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3064 sc->sc_rx_th.wr_flags = IEEE80211_RADIOTAP_F_WEP;
3066 sc->sc_rx_th.wr_flags = 0;
3068 sc->sc_rx_th.wr_antsignal = rssi + IWL2100_NOISE_FLOOR;
3069 sc->sc_rx_th.wr_antnoise = IWL2100_NOISE_FLOOR;
3071 bpf_ptap(sc->sc_drvbpf, m, &sc->sc_rx_th, sc->sc_rx_th_len);
3074 ieee80211_input(ic, m, ni, rssi, 0);
3075 ieee80211_free_node(ni);
3077 if (c != ic->ic_curchan) /* Happen during scanning */
3078 iwl2100_chan_change(sc, ic->ic_curchan);
3082 iwl2100_scanend_dispatch(struct netmsg *nmsg)
3084 struct iwlmsg *msg = (struct iwlmsg *)nmsg;
3085 struct iwl2100_softc *sc = msg->iwlm_softc;
3086 struct ieee80211com *ic = &sc->sc_ic;
3087 struct ifnet *ifp = &ic->ic_if;
3089 ASSERT_SERIALIZED(ifp->if_serializer);
3091 if (sc->sc_flags & IWL2100_F_DETACH)
3094 if (ifp->if_flags & IFF_RUNNING) {
3095 ieee80211_end_scan(ic);
3096 sc->sc_flags &= ~IWL2100_F_SCANNING;
3099 lwkt_replymsg(&nmsg->nm_lmsg, 0);
3103 iwl2100_hw_init(struct iwl2100_softc *sc, const uint8_t *bssid,
3104 const uint8_t *essid, uint8_t esslen, uint32_t flags)
3106 struct ieee80211com *ic = &sc->sc_ic;
3107 struct ifnet *ifp = &ic->ic_if;
3111 ASSERT_SERIALIZED(ifp->if_serializer);
3112 KKASSERT(curthread == &sc->sc_thread);
3114 iwl2100_hw_stop(sc);
3116 error = iwl2100_alloc_firmware(sc, ic->ic_opmode);
3118 if_printf(ifp, "can't allocate firmware\n");
3122 error = iwl2100_load_firmware(sc, ic->ic_opmode);
3124 if_printf(ifp, "can't load firmware\n");
3128 error = iwl2100_alloc_cmd(sc);
3130 if_printf(ifp, "can't allocate cmd\n");
3134 error = iwl2100_init_tx_ring(sc);
3136 if_printf(ifp, "can't init TX ring\n");
3140 error = iwl2100_init_rx_ring(sc);
3142 if_printf(ifp, "can't init RX ring\n");
3146 error = iwl2100_init_firmware(sc);
3148 if_printf(ifp, "can't initialize firmware\n");
3152 sc->sc_ord1 = CSR_READ_4(sc, IWL2100_ORD1_ADDR);
3153 sc->sc_ord2 = CSR_READ_4(sc, IWL2100_ORD2_ADDR);
3155 db_addr = iwl2100_read_ord1(sc, IWL2100_ORD1_DBADDR);
3156 if ((IND_READ_4(sc, db_addr + 0x20) >> 24) & 0x1)
3157 sc->sc_caps &= ~IWL2100_C_RFKILL;
3159 sc->sc_caps |= IWL2100_C_RFKILL;
3161 /* Unlock firmware */
3162 iwl2100_write_ord1(sc, IWL2100_ORD1_FWLOCK, 0);
3164 if (iwl2100_rfkilled(sc)) {
3169 /* Let interrupt handler run */
3170 sc->sc_flags |= IWL2100_F_INITED;
3172 /* Enable interrupts */
3173 CSR_WRITE_4(sc, IWL2100_INTR_MASK, IWL2100_INTRS);
3175 error = iwl2100_config(sc, bssid, essid, esslen,
3176 flags & IWL2100_INIT_F_IBSSCHAN);
3180 if (flags & IWL2100_INIT_F_ENABLE) {
3181 error = iwl2100_config_done(sc);
3183 if_printf(ifp, "can't complete config\n");
3188 ifp->if_flags &= ~IFF_OACTIVE;
3189 ifp->if_flags |= IFF_RUNNING;
3197 iwl2100_start_scan(struct iwl2100_softc *sc, uint32_t chans, uint32_t flags)
3203 * Firmware always starts scanning once config is done
3205 error = iwl2100_set_scanopt(sc, chans, flags);
3207 if_printf(&sc->sc_ic.ic_if, "can't set scan opt\n");
3211 error = iwl2100_set_scan(sc);
3213 if_printf(&sc->sc_ic.ic_if, "can't set bcast scanning\n");
3220 iwl2100_scan(struct iwl2100_softc *sc)
3222 struct ieee80211com *ic = &sc->sc_ic;
3223 uint32_t chans, flags;
3226 KKASSERT(ic->ic_opmode != IEEE80211_M_MONITOR);
3228 error = iwl2100_hw_init(sc, NULL,
3229 ic->ic_des_essid, ic->ic_des_esslen, IWL2100_INIT_F_ENABLE);
3233 if (ic->ic_opmode == IEEE80211_M_STA) {
3234 chans = sc->sc_bss_chans;
3235 flags = IWL2100_SCANOPT_NOASSOC;
3238 * Normally # of IBSS channels is less than BSS's
3239 * but it seems IBSS mode works on all BSS channels
3242 chans = sc->sc_ibss_chans;
3244 chans = sc->sc_bss_chans;
3247 * Don't set NOASSOC scan option, it seems that
3248 * firmware will disable itself after scanning
3249 * if this flag is set. After all, we are in
3250 * IBSS mode, which does not have concept of
3256 /* See NOTE in iwl2100_set_scanopt() */
3257 error = iwl2100_start_scan(sc, chans, flags);
3264 iwl2100_auth(struct iwl2100_softc *sc)
3266 struct ieee80211com *ic = &sc->sc_ic;
3267 struct ieee80211_node *ni = ic->ic_bss;
3271 KKASSERT(ic->ic_opmode == IEEE80211_M_STA);
3273 chan = ieee80211_chan2ieee(ic, ic->ic_curchan);
3274 if (chan == IEEE80211_CHAN_ANY) {
3275 if_printf(&ic->ic_if, "invalid curchan\n");
3279 error = iwl2100_hw_init(sc, ni->ni_bssid,
3280 ni->ni_essid, ni->ni_esslen, IWL2100_INIT_F_ENABLE);
3284 /* See NOTE in iwl2100_set_scanopt() */
3285 error = iwl2100_start_scan(sc, 1 << (chan - 1), 0);
3292 iwl2100_ibss(struct iwl2100_softc *sc)
3294 struct ieee80211com *ic = &sc->sc_ic;
3295 struct ieee80211_node *ni = ic->ic_bss;
3297 return iwl2100_hw_init(sc, ni->ni_bssid,
3298 ni->ni_essid, ni->ni_esslen,
3299 IWL2100_INIT_F_ENABLE | IWL2100_INIT_F_IBSSCHAN);
3303 iwl2100_encap(struct iwl2100_softc *sc, struct mbuf *m)
3305 struct iwl2100_tx_ring *tr = &sc->sc_txring;
3306 struct iwl2100_tx_hdr *th;
3307 struct ieee80211_frame *wh;
3308 struct iwl_dmamap_ctx ctx;
3309 bus_dma_segment_t segs[IWL2100_NSEG_MAX];
3310 uint8_t src[IEEE80211_ADDR_LEN], dst[IEEE80211_ADDR_LEN];
3312 int maxsegs, i, first_idx, last_idx, error, host_enc;
3315 * Save necessary information and strip 802.11 header
3317 wh = mtod(m, struct ieee80211_frame *);
3318 IEEE80211_ADDR_COPY(src, wh->i_addr2);
3319 if (sc->sc_ic.ic_opmode == IEEE80211_M_STA)
3320 IEEE80211_ADDR_COPY(dst, wh->i_addr3);
3322 IEEE80211_ADDR_COPY(dst, wh->i_addr1);
3323 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
3327 m_adj(m, sizeof(*wh));
3330 * Prepend and setup hardware TX header
3332 M_PREPEND(m, sizeof(*th), MB_DONTWAIT);
3334 if_printf(&sc->sc_ic.ic_if, "prepend TX header failed\n");
3337 th = mtod(m, struct iwl2100_tx_hdr *);
3339 bzero(th, sizeof(*th));
3340 th->th_cmd = IWL2100_CMD_TX_DATA;
3341 th->th_host_enc = host_enc;
3342 IEEE80211_ADDR_COPY(th->th_src, src);
3343 IEEE80211_ADDR_COPY(th->th_dst, dst);
3346 * Load mbuf into DMA map
3348 maxsegs = IWL2100_TX_USED_MAX - tr->tr_used;
3349 if (maxsegs > IWL2100_NSEG_MAX)
3350 maxsegs = IWL2100_NSEG_MAX;
3352 KKASSERT(tr->tr_index < IWL2100_TX_NDESC);
3353 first_idx = tr->tr_index;
3354 dmap = tr->tr_buf[first_idx].tb_dmap;
3356 ctx.nsegs = maxsegs;
3358 error = bus_dmamap_load_mbuf(sc->sc_mbuf_dtag, dmap, m,
3359 iwl_dma_buf_addr, &ctx, BUS_DMA_NOWAIT);
3360 if (!error && ctx.nsegs == 0) {
3361 bus_dmamap_unload(sc->sc_mbuf_dtag, dmap);
3364 if (error && error != EFBIG) {
3365 if_printf(&sc->sc_ic.ic_if, "can't load TX mbuf, error %d\n",
3369 if (error) { /* error == EFBIG */
3372 m_new = m_defrag(m, MB_DONTWAIT);
3373 if (m_new == NULL) {
3374 if_printf(&sc->sc_ic.ic_if, "can't defrag TX mbuf\n");
3381 ctx.nsegs = maxsegs;
3383 error = bus_dmamap_load_mbuf(sc->sc_mbuf_dtag, dmap, m,
3384 iwl_dma_buf_addr, &ctx,
3386 if (error || ctx.nsegs == 0) {
3387 if (ctx.nsegs == 0) {
3388 bus_dmamap_unload(sc->sc_mbuf_dtag, dmap);
3391 if_printf(&sc->sc_ic.ic_if,
3392 "can't load defraged TX mbuf\n");
3396 bus_dmamap_sync(sc->sc_mbuf_dtag, dmap, BUS_DMASYNC_PREWRITE);
3402 for (i = 0; i < ctx.nsegs; ++i) {
3403 struct iwl2100_desc *d = &tr->tr_desc[tr->tr_index];
3405 d->d_paddr = segs[i].ds_addr;
3406 d->d_len = segs[i].ds_len;
3410 d->d_nfrag = ctx.nsegs;
3412 if (i == ctx.nsegs - 1) {
3413 d->d_flags = IWL2100_TXD_F_INTR;
3414 last_idx = tr->tr_index;
3416 d->d_flags = IWL2100_TXD_F_NOTLAST;
3419 tr->tr_index = (tr->tr_index + 1) % IWL2100_TX_NDESC;
3421 KKASSERT(last_idx >= 0);
3423 tr->tr_buf[first_idx].tb_dmap = tr->tr_buf[last_idx].tb_dmap;
3424 tr->tr_buf[last_idx].tb_dmap = dmap;
3425 tr->tr_buf[last_idx].tb_mbuf = m;
3427 tr->tr_used += ctx.nsegs;
3428 KKASSERT(tr->tr_used <= IWL2100_TX_USED_MAX);
3438 iwl2100_restart_dispatch(struct netmsg *nmsg)
3440 struct iwlmsg *msg = (struct iwlmsg *)nmsg;
3441 struct iwl2100_softc *sc = msg->iwlm_softc;
3442 struct ieee80211com *ic = &sc->sc_ic;
3443 struct ifnet *ifp = &ic->ic_if;
3446 ASSERT_SERIALIZED(ifp->if_serializer);
3448 if (sc->sc_flags & IWL2100_F_DETACH)
3451 if ((ifp->if_flags & IFF_RUNNING) == 0)
3454 if (msg->iwlm_arg != sc->sc_state_age) {
3456 * Restarting was triggered in old 802.11 state
3457 * Don't do anything, this is a staled restarting.
3462 if (ic->ic_state != IEEE80211_S_RUN) {
3463 if_printf(ifp, "restart happened when not in RUN state\n");
3468 * iwl2100_auth() may release slizer, so stop all
3469 * callouts to prevent them from misfiring.
3471 callout_stop(&sc->sc_restart_bmiss);
3472 callout_stop(&sc->sc_ibss);
3474 if (ic->ic_opmode == IEEE80211_M_STA) {
3475 error = iwl2100_auth(sc);
3480 * Start software beacon missing to handle missing
3481 * firmware bmiss status change when we restarting
3483 callout_reset(&sc->sc_restart_bmiss, IEEE80211_TU_TO_TICKS(
3484 2 * ic->ic_bmissthreshold * ic->ic_bss->ni_intval),
3485 iwl2100_restart_bmiss, sc);
3486 } else if (ic->ic_opmode == IEEE80211_M_IBSS) {
3487 error = iwl2100_ibss(sc);
3492 /* Turn on restarting flag before reply this message */
3493 sc->sc_flags |= IWL2100_F_RESTARTING;
3495 lwkt_replymsg(&nmsg->nm_lmsg, error);
3499 iwl2100_restart(struct iwl2100_softc *sc)
3501 if ((sc->sc_flags & (IWL2100_F_RESTARTING | IWL2100_F_DETACH)) == 0) {
3502 struct iwlmsg *msg = &sc->sc_restart_msg;
3503 struct lwkt_msg *lmsg = &msg->iwlm_nmsg.nm_lmsg;
3505 DPRINTF(sc, IWL2100_DBG_RESTART, "%s", "restart\n");
3506 if (lmsg->ms_flags & MSGF_DONE) {
3507 sc->sc_flags &= ~IWL2100_F_IFSTART;
3508 msg->iwlm_arg = sc->sc_state_age;
3509 lwkt_sendmsg(&sc->sc_thread_port, lmsg);
3515 iwl2100_bmiss_dispatch(struct netmsg *nmsg)
3517 struct iwlmsg *msg = (struct iwlmsg *)nmsg;
3518 struct iwl2100_softc *sc = msg->iwlm_softc;
3519 struct ieee80211com *ic = &sc->sc_ic;
3520 struct ifnet *ifp = &ic->ic_if;
3522 ASSERT_SERIALIZED(ifp->if_serializer);
3524 if (sc->sc_flags & IWL2100_F_DETACH)
3527 if (ifp->if_flags & IFF_RUNNING) {
3529 * Fake a ic_bmiss_count to make sure that
3530 * ieee80211_beacon_miss() will do its job
3532 ic->ic_bmiss_count = ic->ic_bmiss_max;
3533 ieee80211_beacon_miss(ic);
3536 lwkt_replymsg(&nmsg->nm_lmsg, 0);
3540 iwl2100_restart_bmiss(void *xsc)
3542 struct iwl2100_softc *sc = xsc;
3543 struct ifnet *ifp = &sc->sc_ic.ic_if;
3545 lwkt_serialize_enter(ifp->if_serializer);
3547 if (sc->sc_flags & IWL2100_F_DETACH)
3550 if ((ifp->if_flags & IFF_RUNNING) == 0)
3553 if (sc->sc_flags & IWL2100_F_RESTARTING) {
3554 DPRINTF(sc, IWL2100_DBG_SCAN | IWL2100_DBG_RESTART, "%s",
3556 iwlmsg_send(&sc->sc_bmiss_msg, &sc->sc_thread_port);
3559 lwkt_serialize_exit(ifp->if_serializer);
3563 iwl2100_ibss_bssid(void *xsc)
3565 struct iwl2100_softc *sc = xsc;
3566 struct ieee80211com *ic = &sc->sc_ic;
3567 struct ifnet *ifp = &ic->ic_if;
3569 lwkt_serialize_enter(ifp->if_serializer);
3571 if (sc->sc_flags & IWL2100_F_DETACH)
3574 if ((ifp->if_flags & IFF_RUNNING) == 0)
3577 if (ic->ic_state == IEEE80211_S_RUN &&
3578 ic->ic_opmode == IEEE80211_M_IBSS) {
3579 uint8_t bssid[IEEE80211_ADDR_LEN];
3582 len = iwl2100_read_ord2(sc, IWL2100_ORD2_BSSID,
3583 bssid, sizeof(bssid));
3584 if (len < (int)sizeof(bssid)) {
3585 if_printf(ifp, "can't get IBSS bssid\n");
3587 DPRINTF(sc, IWL2100_DBG_IBSS, "IBSS bssid: %6D\n",
3589 IEEE80211_ADDR_COPY(ic->ic_bss->ni_bssid, bssid);
3591 sc->sc_flags |= IWL2100_F_IFSTART;
3596 lwkt_serialize_exit(ifp->if_serializer);
3600 iwl2100_reinit(struct iwl2100_softc *sc)
3602 struct ifnet *ifp = &sc->sc_ic.ic_if;
3604 callout_stop(&sc->sc_restart_bmiss);
3605 callout_stop(&sc->sc_ibss);
3607 ifp->if_flags &= ~IFF_RUNNING;
3610 sc->sc_flags &= ~IWL2100_F_INITED;
3611 sc->sc_tx_timer = 0;
3613 /* Mark error happened, and wake up the pending command */
3614 sc->sc_flags |= IWL2100_F_ERROR;
3617 if ((sc->sc_flags & IWL2100_F_DETACH) == 0) {
3619 * Schedule complete initialization,
3620 * i.e. blow away current state
3622 iwlmsg_send(&sc->sc_reinit_msg, &sc->sc_thread_port);
3627 iwl2100_reinit_dispatch(struct netmsg *nmsg)
3629 struct iwlmsg *msg = (struct iwlmsg *)nmsg;
3630 struct iwl2100_softc *sc = msg->iwlm_softc;
3631 struct ifnet *ifp = &sc->sc_ic.ic_if;
3633 ASSERT_SERIALIZED(ifp->if_serializer);
3636 * NOTE: Reply ASAP, so reinit msg could be used if error intr
3637 * happened again during following iwl2100_init()
3639 lwkt_replymsg(&nmsg->nm_lmsg, 0);
3641 if (sc->sc_flags & IWL2100_F_DETACH)
3644 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == IFF_UP)
3649 iwl2100_reinit_callout(void *xsc)
3651 struct iwl2100_softc *sc = xsc;
3652 struct ifnet *ifp = &sc->sc_ic.ic_if;
3654 lwkt_serialize_enter(ifp->if_serializer);
3655 if ((sc->sc_flags & IWL2100_F_DETACH) == 0)
3657 lwkt_serialize_exit(ifp->if_serializer);
3661 iwl2100_chan_change(struct iwl2100_softc *sc, const struct ieee80211_channel *c)
3663 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
3664 htole16(c->ic_freq);
3668 iwl2100_stop_callouts(struct iwl2100_softc *sc)
3670 callout_stop(&sc->sc_restart_bmiss);
3671 callout_stop(&sc->sc_ibss);
3672 callout_stop(&sc->sc_reinit);