if_iwi.c revision 1.62.14.7 1 /* $NetBSD: if_iwi.c,v 1.62.14.7 2007/12/08 16:21:27 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2005
5 * Damien Bergamini <damien.bergamini (at) free.fr>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.62.14.7 2007/12/08 16:21:27 jmcneill Exp $");
32
33 /*-
34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver
35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm
36 */
37
38 #include "bpfilter.h"
39
40 #include <sys/param.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/mbuf.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/conf.h>
49 #include <sys/kauth.h>
50
51 #include <sys/bus.h>
52 #include <machine/endian.h>
53 #include <sys/intr.h>
54
55 #include <dev/firmload.h>
56
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcidevs.h>
60
61 #if NBPFILTER > 0
62 #include <net/bpf.h>
63 #endif
64 #include <net/if.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_ether.h>
68 #include <net/if_media.h>
69 #include <net/if_types.h>
70
71 #include <net80211/ieee80211_var.h>
72 #include <net80211/ieee80211_radiotap.h>
73
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78
79 #include <crypto/arc4/arc4.h>
80
81 #include <dev/pci/if_iwireg.h>
82 #include <dev/pci/if_iwivar.h>
83
84 #ifdef IWI_DEBUG
85 #define DPRINTF(x) if (iwi_debug > 0) printf x
86 #define DPRINTFN(n, x) if (iwi_debug >= (n)) printf x
87 int iwi_debug = 4;
88 #else
89 #define DPRINTF(x)
90 #define DPRINTFN(n, x)
91 #endif
92
93 static int iwi_match(device_t, struct cfdata *, void *);
94 static void iwi_attach(device_t, device_t, void *);
95 static int iwi_detach(device_t, int);
96
97 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *,
98 int);
99 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
100 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *);
101 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *,
102 int, bus_addr_t, bus_size_t);
103 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
104 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *);
105 static struct mbuf *
106 iwi_alloc_rx_buf(struct iwi_softc *sc);
107 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *,
108 int);
109 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
110 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *);
111
112 static struct ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *);
113 static void iwi_node_free(struct ieee80211_node *);
114
115 static int iwi_cvtrate(int);
116 static int iwi_media_change(struct ifnet *);
117 static void iwi_media_status(struct ifnet *, struct ifmediareq *);
118 static int iwi_wme_update(struct ieee80211com *);
119 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t);
120 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int);
121 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *);
122 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int,
123 struct iwi_frame *);
124 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *);
125 static void iwi_cmd_intr(struct iwi_softc *);
126 static void iwi_rx_intr(struct iwi_softc *);
127 static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *);
128 static int iwi_intr(void *);
129 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int);
130 static void iwi_write_ibssnode(struct iwi_softc *, const struct iwi_node *);
131 static int iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *,
132 int);
133 static void iwi_start(struct ifnet *);
134 static void iwi_watchdog(struct ifnet *);
135
136 static int iwi_alloc_unr(struct iwi_softc *);
137 static void iwi_free_unr(struct iwi_softc *, int);
138
139 static int iwi_get_table0(struct iwi_softc *, uint32_t *);
140
141 static int iwi_ioctl(struct ifnet *, u_long, void *);
142 static void iwi_stop_master(struct iwi_softc *);
143 static int iwi_reset(struct iwi_softc *);
144 static int iwi_load_ucode(struct iwi_softc *, void *, int);
145 static int iwi_load_firmware(struct iwi_softc *, void *, int);
146 static int iwi_cache_firmware(struct iwi_softc *);
147 static void iwi_free_firmware(struct iwi_softc *);
148 static int iwi_config(struct iwi_softc *);
149 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *);
150 static int iwi_scan(struct iwi_softc *);
151 static int iwi_auth_and_assoc(struct iwi_softc *);
152 static int iwi_init(struct ifnet *);
153 static void iwi_stop(struct ifnet *, int);
154 static int iwi_getrfkill(struct iwi_softc *);
155 static void iwi_led_set(struct iwi_softc *, uint32_t, int);
156 static void iwi_sysctlattach(struct iwi_softc *);
157
158 /*
159 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
160 */
161 static const struct ieee80211_rateset iwi_rateset_11a =
162 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
163
164 static const struct ieee80211_rateset iwi_rateset_11b =
165 { 4, { 2, 4, 11, 22 } };
166
167 static const struct ieee80211_rateset iwi_rateset_11g =
168 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
169
170 static inline uint8_t
171 MEM_READ_1(struct iwi_softc *sc, uint32_t addr)
172 {
173 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
174 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA);
175 }
176
177 static inline uint32_t
178 MEM_READ_4(struct iwi_softc *sc, uint32_t addr)
179 {
180 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr);
181 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA);
182 }
183
184 CFATTACH_DECL_NEW(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach,
185 iwi_detach, NULL);
186
187 static int
188 iwi_match(device_t parent, struct cfdata *match, void *aux)
189 {
190 struct pci_attach_args *pa = aux;
191
192 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
193 return 0;
194
195 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG ||
196 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2225BG ||
197 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
198 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2)
199 return 1;
200
201 return 0;
202 }
203
204 static bool
205 iwi_pci_resume(device_t dv)
206 {
207 struct iwi_softc *sc = device_private(dv);
208
209 pci_disable_retry(sc->sc_pct, sc->sc_pcitag);
210
211 return true;
212 }
213
214 /* Base Address Register */
215 #define IWI_PCI_BAR0 0x10
216
217 static void
218 iwi_attach(device_t parent, device_t self, void *aux)
219 {
220 struct iwi_softc *sc = device_private(self);
221 struct ieee80211com *ic = &sc->sc_ic;
222 struct ifnet *ifp = &sc->sc_if;
223 struct pci_attach_args *pa = aux;
224 const char *intrstr;
225 char devinfo[256];
226 bus_space_tag_t memt;
227 bus_space_handle_t memh;
228 pci_intr_handle_t ih;
229 pcireg_t data;
230 uint16_t val;
231 int error, revision, i;
232
233 sc->sc_dev = self;
234 sc->sc_pct = pa->pa_pc;
235 sc->sc_pcitag = pa->pa_tag;
236
237 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
238 revision = PCI_REVISION(pa->pa_class);
239 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
240
241 pci_disable_retry(sc->sc_pct, sc->sc_pcitag);
242
243 /* clear unit numbers allocated to IBSS */
244 sc->sc_unr = 0;
245
246 /* power up chip */
247 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc,
248 NULL)) && error != EOPNOTSUPP) {
249 aprint_error_dev(self, "cannot activate %d\n", error);
250 return;
251 }
252
253 /* enable bus-mastering */
254 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
255 data |= PCI_COMMAND_MASTER_ENABLE;
256 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
257
258 /* map the register window */
259 error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
260 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz);
261 if (error != 0) {
262 aprint_error_dev(self, "could not map memory space\n");
263 return;
264 }
265
266 sc->sc_st = memt;
267 sc->sc_sh = memh;
268 sc->sc_dmat = pa->pa_dmat;
269
270 /* disable interrupts */
271 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
272
273 if (pci_intr_map(pa, &ih) != 0) {
274 aprint_error_dev(self, "could not map interrupt\n");
275 return;
276 }
277
278 intrstr = pci_intr_string(sc->sc_pct, ih);
279 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc);
280 if (sc->sc_ih == NULL) {
281 aprint_error_dev(self, "could not establish interrupt");
282 if (intrstr != NULL)
283 aprint_error(" at %s", intrstr);
284 aprint_error("\n");
285 return;
286 }
287 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
288
289 if (iwi_reset(sc) != 0) {
290 aprint_error_dev(self, "could not reset adapter\n");
291 return;
292 }
293
294 /*
295 * Allocate rings.
296 */
297 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) {
298 aprint_error_dev(self, "could not allocate command ring\n");
299 goto fail;
300 }
301
302 error = iwi_alloc_tx_ring(sc, &sc->txq[0], IWI_TX_RING_COUNT,
303 IWI_CSR_TX1_RIDX, IWI_CSR_TX1_WIDX);
304 if (error != 0) {
305 aprint_error_dev(self, "could not allocate Tx ring 1\n");
306 goto fail;
307 }
308
309 error = iwi_alloc_tx_ring(sc, &sc->txq[1], IWI_TX_RING_COUNT,
310 IWI_CSR_TX2_RIDX, IWI_CSR_TX2_WIDX);
311 if (error != 0) {
312 aprint_error_dev(self, "could not allocate Tx ring 2\n");
313 goto fail;
314 }
315
316 error = iwi_alloc_tx_ring(sc, &sc->txq[2], IWI_TX_RING_COUNT,
317 IWI_CSR_TX3_RIDX, IWI_CSR_TX3_WIDX);
318 if (error != 0) {
319 aprint_error_dev(self, "could not allocate Tx ring 3\n");
320 goto fail;
321 }
322
323 error = iwi_alloc_tx_ring(sc, &sc->txq[3], IWI_TX_RING_COUNT,
324 IWI_CSR_TX4_RIDX, IWI_CSR_TX4_WIDX);
325 if (error != 0) {
326 aprint_error_dev(self, "could not allocate Tx ring 4\n");
327 goto fail;
328 }
329
330 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) {
331 aprint_error_dev(self, "could not allocate Rx ring\n");
332 goto fail;
333 }
334
335 ic->ic_ifp = ifp;
336 ic->ic_wme.wme_update = iwi_wme_update;
337 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
338 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
339 ic->ic_state = IEEE80211_S_INIT;
340
341 sc->sc_fwname = "iwi-bss.fw";
342
343 /* set device capabilities */
344 ic->ic_caps =
345 IEEE80211_C_IBSS | /* IBSS mode supported */
346 IEEE80211_C_MONITOR | /* monitor mode supported */
347 IEEE80211_C_TXPMGT | /* tx power management */
348 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
349 IEEE80211_C_SHSLOT | /* short slot time supported */
350 IEEE80211_C_WPA | /* 802.11i */
351 IEEE80211_C_WME; /* 802.11e */
352
353 /* read MAC address from EEPROM */
354 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0);
355 ic->ic_myaddr[0] = val & 0xff;
356 ic->ic_myaddr[1] = val >> 8;
357 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1);
358 ic->ic_myaddr[2] = val & 0xff;
359 ic->ic_myaddr[3] = val >> 8;
360 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2);
361 ic->ic_myaddr[4] = val & 0xff;
362 ic->ic_myaddr[5] = val >> 8;
363
364 aprint_verbose_dev(self, "802.11 address %s\n",
365 ether_sprintf(ic->ic_myaddr));
366
367 /* read the NIC type from EEPROM */
368 val = iwi_read_prom_word(sc, IWI_EEPROM_NIC_TYPE);
369 sc->nictype = val & 0xff;
370
371 DPRINTF(("%s: NIC type %d\n", device_xname(self), sc->nictype));
372
373 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 ||
374 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) {
375 /* set supported .11a rates (2915ABG only) */
376 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a;
377
378 /* set supported .11a channels */
379 for (i = 36; i <= 64; i += 4) {
380 ic->ic_channels[i].ic_freq =
381 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
382 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
383 }
384 for (i = 149; i <= 165; i += 4) {
385 ic->ic_channels[i].ic_freq =
386 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
387 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
388 }
389 }
390
391 /* set supported .11b and .11g rates */
392 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b;
393 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g;
394
395 /* set supported .11b and .11g channels (1 through 14) */
396 for (i = 1; i <= 14; i++) {
397 ic->ic_channels[i].ic_freq =
398 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
399 ic->ic_channels[i].ic_flags =
400 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
401 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
402 }
403
404 ifp->if_softc = sc;
405 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
406 ifp->if_init = iwi_init;
407 ifp->if_stop = iwi_stop;
408 ifp->if_ioctl = iwi_ioctl;
409 ifp->if_start = iwi_start;
410 ifp->if_watchdog = iwi_watchdog;
411 IFQ_SET_READY(&ifp->if_snd);
412 memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
413
414 if_attach(ifp);
415 ieee80211_ifattach(ic);
416 /* override default methods */
417 ic->ic_node_alloc = iwi_node_alloc;
418 sc->sc_node_free = ic->ic_node_free;
419 ic->ic_node_free = iwi_node_free;
420 /* override state transition machine */
421 sc->sc_newstate = ic->ic_newstate;
422 ic->ic_newstate = iwi_newstate;
423 ieee80211_media_init(ic, iwi_media_change, iwi_media_status);
424
425 #if NBPFILTER > 0
426 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
427 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
428
429 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
430 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
431 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT);
432
433 sc->sc_txtap_len = sizeof sc->sc_txtapu;
434 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
435 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT);
436 #endif
437
438 iwi_sysctlattach(sc);
439
440 if (!pmf_device_register(self, NULL, iwi_pci_resume))
441 aprint_error_dev(self, "couldn't establish power handler\n");
442 else
443 pmf_class_network_register(self, ifp);
444
445 ieee80211_announce(ic);
446
447 return;
448
449 fail: iwi_detach(self, 0);
450 }
451
452 static int
453 iwi_detach(device_t self, int flags)
454 {
455 struct iwi_softc *sc = device_private(self);
456 struct ifnet *ifp = &sc->sc_if;
457
458 pmf_device_deregister(self);
459
460 if (ifp != NULL)
461 iwi_stop(ifp, 1);
462
463 iwi_free_firmware(sc);
464
465 ieee80211_ifdetach(&sc->sc_ic);
466 if (ifp != NULL)
467 if_detach(ifp);
468
469 iwi_free_cmd_ring(sc, &sc->cmdq);
470 iwi_free_tx_ring(sc, &sc->txq[0]);
471 iwi_free_tx_ring(sc, &sc->txq[1]);
472 iwi_free_tx_ring(sc, &sc->txq[2]);
473 iwi_free_tx_ring(sc, &sc->txq[3]);
474 iwi_free_rx_ring(sc, &sc->rxq);
475
476 if (sc->sc_ih != NULL) {
477 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
478 sc->sc_ih = NULL;
479 }
480
481 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
482
483 return 0;
484 }
485
486 static int
487 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring,
488 int count)
489 {
490 int error, nsegs;
491
492 ring->count = count;
493 ring->queued = 0;
494 ring->cur = ring->next = 0;
495
496 /*
497 * Allocate and map command ring
498 */
499 error = bus_dmamap_create(sc->sc_dmat,
500 IWI_CMD_DESC_SIZE * count, 1,
501 IWI_CMD_DESC_SIZE * count, 0,
502 BUS_DMA_NOWAIT, &ring->desc_map);
503 if (error != 0) {
504 aprint_error_dev(sc->sc_dev,
505 "could not create command ring DMA map\n");
506 goto fail;
507 }
508
509 error = bus_dmamem_alloc(sc->sc_dmat,
510 IWI_CMD_DESC_SIZE * count, PAGE_SIZE, 0,
511 &sc->cmdq.desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
512 if (error != 0) {
513 aprint_error_dev(sc->sc_dev,
514 "could not allocate command ring DMA memory\n");
515 goto fail;
516 }
517
518 error = bus_dmamem_map(sc->sc_dmat, &sc->cmdq.desc_seg, nsegs,
519 IWI_CMD_DESC_SIZE * count,
520 (void **)&sc->cmdq.desc, BUS_DMA_NOWAIT);
521 if (error != 0) {
522 aprint_error_dev(sc->sc_dev,
523 "could not map command ring DMA memory\n");
524 goto fail;
525 }
526
527 error = bus_dmamap_load(sc->sc_dmat, sc->cmdq.desc_map, sc->cmdq.desc,
528 IWI_CMD_DESC_SIZE * count, NULL,
529 BUS_DMA_NOWAIT);
530 if (error != 0) {
531 aprint_error_dev(sc->sc_dev,
532 "could not load command ring DMA map\n");
533 goto fail;
534 }
535
536 memset(sc->cmdq.desc, 0,
537 IWI_CMD_DESC_SIZE * count);
538
539 return 0;
540
541 fail: iwi_free_cmd_ring(sc, ring);
542 return error;
543 }
544
545 static void
546 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
547 {
548 int i;
549
550 for (i = ring->next; i != ring->cur;) {
551 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
552 i * IWI_CMD_DESC_SIZE, IWI_CMD_DESC_SIZE,
553 BUS_DMASYNC_POSTWRITE);
554
555 wakeup(&ring->desc[i]);
556 i = (i + 1) % ring->count;
557 }
558
559 ring->queued = 0;
560 ring->cur = ring->next = 0;
561 }
562
563 static void
564 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring)
565 {
566 if (ring->desc_map != NULL) {
567 if (ring->desc != NULL) {
568 bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
569 bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc,
570 IWI_CMD_DESC_SIZE * ring->count);
571 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
572 }
573 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
574 }
575 }
576
577 static int
578 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring,
579 int count, bus_size_t csr_ridx, bus_size_t csr_widx)
580 {
581 int i, error, nsegs;
582
583 ring->count = count;
584 ring->queued = 0;
585 ring->cur = ring->next = 0;
586 ring->csr_ridx = csr_ridx;
587 ring->csr_widx = csr_widx;
588
589 /*
590 * Allocate and map Tx ring
591 */
592 error = bus_dmamap_create(sc->sc_dmat,
593 IWI_TX_DESC_SIZE * count, 1,
594 IWI_TX_DESC_SIZE * count, 0, BUS_DMA_NOWAIT,
595 &ring->desc_map);
596 if (error != 0) {
597 aprint_error_dev(sc->sc_dev,
598 "could not create tx ring DMA map\n");
599 goto fail;
600 }
601
602 error = bus_dmamem_alloc(sc->sc_dmat,
603 IWI_TX_DESC_SIZE * count, PAGE_SIZE, 0,
604 &ring->desc_seg, 1, &nsegs, BUS_DMA_NOWAIT);
605 if (error != 0) {
606 aprint_error_dev(sc->sc_dev,
607 "could not allocate tx ring DMA memory\n");
608 goto fail;
609 }
610
611 error = bus_dmamem_map(sc->sc_dmat, &ring->desc_seg, nsegs,
612 IWI_TX_DESC_SIZE * count,
613 (void **)&ring->desc, BUS_DMA_NOWAIT);
614 if (error != 0) {
615 aprint_error_dev(sc->sc_dev,
616 "could not map tx ring DMA memory\n");
617 goto fail;
618 }
619
620 error = bus_dmamap_load(sc->sc_dmat, ring->desc_map, ring->desc,
621 IWI_TX_DESC_SIZE * count, NULL,
622 BUS_DMA_NOWAIT);
623 if (error != 0) {
624 aprint_error_dev(sc->sc_dev,
625 "could not load tx ring DMA map\n");
626 goto fail;
627 }
628
629 memset(ring->desc, 0, IWI_TX_DESC_SIZE * count);
630
631 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF,
632 M_NOWAIT | M_ZERO);
633 if (ring->data == NULL) {
634 aprint_error_dev(sc->sc_dev, "could not allocate soft data\n");
635 error = ENOMEM;
636 goto fail;
637 }
638
639 /*
640 * Allocate Tx buffers DMA maps
641 */
642 for (i = 0; i < count; i++) {
643 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG,
644 MCLBYTES, 0, BUS_DMA_NOWAIT, &ring->data[i].map);
645 if (error != 0) {
646 aprint_error_dev(sc->sc_dev,
647 "could not create tx buf DMA map");
648 goto fail;
649 }
650 }
651 return 0;
652
653 fail: iwi_free_tx_ring(sc, ring);
654 return error;
655 }
656
657 static void
658 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
659 {
660 struct iwi_tx_data *data;
661 int i;
662
663 for (i = 0; i < ring->count; i++) {
664 data = &ring->data[i];
665
666 if (data->m != NULL) {
667 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
668 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
669 bus_dmamap_unload(sc->sc_dmat, data->map);
670 m_freem(data->m);
671 data->m = NULL;
672 }
673
674 if (data->ni != NULL) {
675 ieee80211_free_node(data->ni);
676 data->ni = NULL;
677 }
678 }
679
680 ring->queued = 0;
681 ring->cur = ring->next = 0;
682 }
683
684 static void
685 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring)
686 {
687 int i;
688
689 if (ring->desc_map != NULL) {
690 if (ring->desc != NULL) {
691 bus_dmamap_unload(sc->sc_dmat, ring->desc_map);
692 bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc,
693 IWI_TX_DESC_SIZE * ring->count);
694 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1);
695 }
696 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map);
697 }
698
699 for (i = 0; i < ring->count; i++) {
700 if (ring->data[i].m != NULL) {
701 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
702 m_freem(ring->data[i].m);
703 }
704 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
705 }
706 }
707
708 static int
709 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count)
710 {
711 int i, error;
712
713 ring->count = count;
714 ring->cur = 0;
715
716 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF,
717 M_NOWAIT | M_ZERO);
718 if (ring->data == NULL) {
719 aprint_error_dev(sc->sc_dev, "could not allocate soft data\n");
720 error = ENOMEM;
721 goto fail;
722 }
723
724 /*
725 * Allocate and map Rx buffers
726 */
727 for (i = 0; i < count; i++) {
728
729 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
730 0, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &ring->data[i].map);
731 if (error != 0) {
732 aprint_error_dev(sc->sc_dev,
733 "could not create rx buf DMA map");
734 goto fail;
735 }
736
737 if ((ring->data[i].m = iwi_alloc_rx_buf(sc)) == NULL) {
738 error = ENOMEM;
739 goto fail;
740 }
741
742 error = bus_dmamap_load_mbuf(sc->sc_dmat, ring->data[i].map,
743 ring->data[i].m, BUS_DMA_READ | BUS_DMA_NOWAIT);
744 if (error != 0) {
745 aprint_error_dev(sc->sc_dev,
746 "could not load rx buffer DMA map\n");
747 goto fail;
748 }
749
750 bus_dmamap_sync(sc->sc_dmat, ring->data[i].map, 0,
751 ring->data[i].map->dm_mapsize, BUS_DMASYNC_PREREAD);
752 }
753
754 return 0;
755
756 fail: iwi_free_rx_ring(sc, ring);
757 return error;
758 }
759
760 static void
761 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
762 {
763 ring->cur = 0;
764 }
765
766 static void
767 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring)
768 {
769 int i;
770
771 for (i = 0; i < ring->count; i++) {
772 if (ring->data[i].m != NULL) {
773 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map);
774 m_freem(ring->data[i].m);
775 }
776 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map);
777 }
778 }
779
780 static struct ieee80211_node *
781 iwi_node_alloc(struct ieee80211_node_table *nt)
782 {
783 struct iwi_node *in;
784
785 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO);
786 if (in == NULL)
787 return NULL;
788
789 in->in_station = -1;
790
791 return &in->in_node;
792 }
793
794 static int
795 iwi_alloc_unr(struct iwi_softc *sc)
796 {
797 int i;
798
799 for (i = 0; i < IWI_MAX_IBSSNODE - 1; i++)
800 if ((sc->sc_unr & (1 << i)) == 0) {
801 sc->sc_unr |= 1 << i;
802 return i;
803 }
804
805 return -1;
806 }
807
808 static void
809 iwi_free_unr(struct iwi_softc *sc, int r)
810 {
811
812 sc->sc_unr &= 1 << r;
813 }
814
815 static void
816 iwi_node_free(struct ieee80211_node *ni)
817 {
818 struct ieee80211com *ic = ni->ni_ic;
819 struct iwi_softc *sc = ic->ic_ifp->if_softc;
820 struct iwi_node *in = (struct iwi_node *)ni;
821
822 if (in->in_station != -1)
823 iwi_free_unr(sc, in->in_station);
824
825 sc->sc_node_free(ni);
826 }
827
828 static int
829 iwi_media_change(struct ifnet *ifp)
830 {
831 int error;
832
833 error = ieee80211_media_change(ifp);
834 if (error != ENETRESET)
835 return error;
836
837 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
838 iwi_init(ifp);
839
840 return 0;
841 }
842
843 /*
844 * Convert h/w rate code to IEEE rate code.
845 */
846 static int
847 iwi_cvtrate(int iwirate)
848 {
849 switch (iwirate) {
850 case IWI_RATE_DS1: return 2;
851 case IWI_RATE_DS2: return 4;
852 case IWI_RATE_DS5: return 11;
853 case IWI_RATE_DS11: return 22;
854 case IWI_RATE_OFDM6: return 12;
855 case IWI_RATE_OFDM9: return 18;
856 case IWI_RATE_OFDM12: return 24;
857 case IWI_RATE_OFDM18: return 36;
858 case IWI_RATE_OFDM24: return 48;
859 case IWI_RATE_OFDM36: return 72;
860 case IWI_RATE_OFDM48: return 96;
861 case IWI_RATE_OFDM54: return 108;
862 }
863 return 0;
864 }
865
866 /*
867 * The firmware automatically adapts the transmit speed. We report its current
868 * value here.
869 */
870 static void
871 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr)
872 {
873 struct iwi_softc *sc = ifp->if_softc;
874 struct ieee80211com *ic = &sc->sc_ic;
875 int rate;
876
877 imr->ifm_status = IFM_AVALID;
878 imr->ifm_active = IFM_IEEE80211;
879 if (ic->ic_state == IEEE80211_S_RUN)
880 imr->ifm_status |= IFM_ACTIVE;
881
882 /* read current transmission rate from adapter */
883 rate = iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE));
884 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode);
885
886 switch (ic->ic_opmode) {
887 case IEEE80211_M_STA:
888 break;
889
890 case IEEE80211_M_IBSS:
891 imr->ifm_active |= IFM_IEEE80211_ADHOC;
892 break;
893
894 case IEEE80211_M_MONITOR:
895 imr->ifm_active |= IFM_IEEE80211_MONITOR;
896 break;
897
898 case IEEE80211_M_AHDEMO:
899 case IEEE80211_M_HOSTAP:
900 /* should not get there */
901 break;
902 }
903 }
904
905 static int
906 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
907 {
908 struct iwi_softc *sc = ic->ic_ifp->if_softc;
909
910 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__,
911 ieee80211_state_name[ic->ic_state],
912 ieee80211_state_name[nstate], sc->flags));
913
914 switch (nstate) {
915 case IEEE80211_S_SCAN:
916 if (sc->flags & IWI_FLAG_SCANNING)
917 break;
918
919 ieee80211_node_table_reset(&ic->ic_scan);
920 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
921 sc->flags |= IWI_FLAG_SCANNING;
922 /* blink the led while scanning */
923 iwi_led_set(sc, IWI_LED_ASSOCIATED, 1);
924 iwi_scan(sc);
925 break;
926
927 case IEEE80211_S_AUTH:
928 iwi_auth_and_assoc(sc);
929 break;
930
931 case IEEE80211_S_RUN:
932 if (ic->ic_opmode == IEEE80211_M_IBSS)
933 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
934 else if (ic->ic_opmode == IEEE80211_M_MONITOR)
935 iwi_set_chan(sc, ic->ic_ibss_chan);
936
937 return (*sc->sc_newstate)(ic, nstate,
938 IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
939
940 case IEEE80211_S_ASSOC:
941 iwi_led_set(sc, IWI_LED_ASSOCIATED, 0);
942 break;
943
944 case IEEE80211_S_INIT:
945 sc->flags &= ~IWI_FLAG_SCANNING;
946 return (*sc->sc_newstate)(ic, nstate, arg);
947 }
948
949 ic->ic_state = nstate;
950 return 0;
951 }
952
953 /*
954 * WME parameters coming from IEEE 802.11e specification. These values are
955 * already declared in ieee80211_proto.c, but they are static so they can't
956 * be reused here.
957 */
958 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = {
959 { 0, 3, 5, 7, 0, 0, }, /* WME_AC_BE */
960 { 0, 3, 5, 10, 0, 0, }, /* WME_AC_BK */
961 { 0, 2, 4, 5, 188, 0, }, /* WME_AC_VI */
962 { 0, 2, 3, 4, 102, 0, }, /* WME_AC_VO */
963 };
964
965 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = {
966 { 0, 3, 4, 6, 0, 0, }, /* WME_AC_BE */
967 { 0, 3, 4, 10, 0, 0, }, /* WME_AC_BK */
968 { 0, 2, 3, 4, 94, 0, }, /* WME_AC_VI */
969 { 0, 2, 2, 3, 47, 0, }, /* WME_AC_VO */
970 };
971
972 static int
973 iwi_wme_update(struct ieee80211com *ic)
974 {
975 #define IWI_EXP2(v) htole16((1 << (v)) - 1)
976 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
977 struct iwi_softc *sc = ic->ic_ifp->if_softc;
978 struct iwi_wme_params wme[3];
979 const struct wmeParams *wmep;
980 int ac;
981
982 /*
983 * We shall not override firmware default WME values if WME is not
984 * actually enabled.
985 */
986 if (!(ic->ic_flags & IEEE80211_F_WME))
987 return 0;
988
989 for (ac = 0; ac < WME_NUM_AC; ac++) {
990 /* set WME values for current operating mode */
991 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
992 wme[0].aifsn[ac] = wmep->wmep_aifsn;
993 wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
994 wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
995 wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
996 wme[0].acm[ac] = wmep->wmep_acm;
997
998 /* set WME values for CCK modulation */
999 wmep = &iwi_wme_cck_params[ac];
1000 wme[1].aifsn[ac] = wmep->wmep_aifsn;
1001 wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1002 wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1003 wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1004 wme[1].acm[ac] = wmep->wmep_acm;
1005
1006 /* set WME values for OFDM modulation */
1007 wmep = &iwi_wme_ofdm_params[ac];
1008 wme[2].aifsn[ac] = wmep->wmep_aifsn;
1009 wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin);
1010 wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax);
1011 wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit);
1012 wme[2].acm[ac] = wmep->wmep_acm;
1013 }
1014
1015 DPRINTF(("Setting WME parameters\n"));
1016 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, wme, sizeof wme, 1);
1017 #undef IWI_USEC
1018 #undef IWI_EXP2
1019 }
1020
1021 /*
1022 * Read 16 bits at address 'addr' from the serial EEPROM.
1023 */
1024 static uint16_t
1025 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr)
1026 {
1027 uint32_t tmp;
1028 uint16_t val;
1029 int n;
1030
1031 /* Clock C once before the first command */
1032 IWI_EEPROM_CTL(sc, 0);
1033 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1034 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1035 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1036
1037 /* Write start bit (1) */
1038 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1039 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1040
1041 /* Write READ opcode (10) */
1042 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D);
1043 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C);
1044 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1045 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1046
1047 /* Write address A7-A0 */
1048 for (n = 7; n >= 0; n--) {
1049 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1050 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D));
1051 IWI_EEPROM_CTL(sc, IWI_EEPROM_S |
1052 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C);
1053 }
1054
1055 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1056
1057 /* Read data Q15-Q0 */
1058 val = 0;
1059 for (n = 15; n >= 0; n--) {
1060 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C);
1061 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1062 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL);
1063 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n;
1064 }
1065
1066 IWI_EEPROM_CTL(sc, 0);
1067
1068 /* Clear Chip Select and clock C */
1069 IWI_EEPROM_CTL(sc, IWI_EEPROM_S);
1070 IWI_EEPROM_CTL(sc, 0);
1071 IWI_EEPROM_CTL(sc, IWI_EEPROM_C);
1072
1073 return val;
1074 }
1075
1076 /*
1077 * XXX: Hack to set the current channel to the value advertised in beacons or
1078 * probe responses. Only used during AP detection.
1079 */
1080 static void
1081 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m)
1082 {
1083 struct ieee80211_frame *wh;
1084 uint8_t subtype;
1085 uint8_t *frm, *efrm;
1086
1087 wh = mtod(m, struct ieee80211_frame *);
1088
1089 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
1090 return;
1091
1092 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
1093
1094 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON &&
1095 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1096 return;
1097
1098 frm = (uint8_t *)(wh + 1);
1099 efrm = mtod(m, uint8_t *) + m->m_len;
1100
1101 frm += 12; /* skip tstamp, bintval and capinfo fields */
1102 while (frm < efrm) {
1103 if (*frm == IEEE80211_ELEMID_DSPARMS)
1104 #if IEEE80211_CHAN_MAX < 255
1105 if (frm[2] <= IEEE80211_CHAN_MAX)
1106 #endif
1107 ic->ic_curchan = &ic->ic_channels[frm[2]];
1108
1109 frm += frm[1] + 2;
1110 }
1111 }
1112
1113 static struct mbuf *
1114 iwi_alloc_rx_buf(struct iwi_softc *sc)
1115 {
1116 struct mbuf *m;
1117
1118 MGETHDR(m, M_DONTWAIT, MT_DATA);
1119 if (m == NULL) {
1120 aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n");
1121 return NULL;
1122 }
1123
1124 MCLGET(m, M_DONTWAIT);
1125 if (!(m->m_flags & M_EXT)) {
1126 aprint_error_dev(sc->sc_dev,
1127 "could not allocate rx mbuf cluster\n");
1128 m_freem(m);
1129 return NULL;
1130 }
1131
1132 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
1133 return m;
1134 }
1135
1136 static void
1137 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i,
1138 struct iwi_frame *frame)
1139 {
1140 struct ieee80211com *ic = &sc->sc_ic;
1141 struct ifnet *ifp = ic->ic_ifp;
1142 struct mbuf *m, *m_new;
1143 struct ieee80211_frame *wh;
1144 struct ieee80211_node *ni;
1145 int error;
1146
1147 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n",
1148 le16toh(frame->len), frame->chan, frame->rssi_dbm));
1149
1150 if (le16toh(frame->len) < sizeof (struct ieee80211_frame) ||
1151 le16toh(frame->len) > MCLBYTES) {
1152 DPRINTF(("%s: bad frame length\n", device_xname(sc->sc_dev)));
1153 ifp->if_ierrors++;
1154 return;
1155 }
1156
1157 /*
1158 * Try to allocate a new mbuf for this ring element and
1159 * load it before processing the current mbuf. If the ring
1160 * element cannot be reloaded, drop the received packet
1161 * and reuse the old mbuf. In the unlikely case that
1162 * the old mbuf can't be reloaded either, explicitly panic.
1163 *
1164 * XXX Reorganize buffer by moving elements from the logical
1165 * end of the ring to the front instead of dropping.
1166 */
1167 if ((m_new = iwi_alloc_rx_buf(sc)) == NULL) {
1168 ifp->if_ierrors++;
1169 return;
1170 }
1171
1172 bus_dmamap_unload(sc->sc_dmat, data->map);
1173
1174 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m_new,
1175 BUS_DMA_READ | BUS_DMA_NOWAIT);
1176 if (error != 0) {
1177 aprint_error_dev(sc->sc_dev,
1178 "could not load rx buf DMA map\n");
1179 m_freem(m_new);
1180 ifp->if_ierrors++;
1181 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map,
1182 data->m, BUS_DMA_READ | BUS_DMA_NOWAIT);
1183 if (error)
1184 panic("%s: unable to remap rx buf",
1185 device_xname(sc->sc_dev));
1186 return;
1187 }
1188
1189 /*
1190 * New mbuf successfully loaded, update RX ring and continue
1191 * processing.
1192 */
1193 m = data->m;
1194 data->m = m_new;
1195 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, data->map->dm_segs[0].ds_addr);
1196
1197 /* Finalize mbuf */
1198 m->m_pkthdr.rcvif = ifp;
1199 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) +
1200 sizeof (struct iwi_frame) + le16toh(frame->len);
1201
1202 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame));
1203
1204 if (ic->ic_state == IEEE80211_S_SCAN)
1205 iwi_fix_channel(ic, m);
1206
1207 #if NBPFILTER > 0
1208 if (sc->sc_drvbpf != NULL) {
1209 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap;
1210
1211 tap->wr_flags = 0;
1212 tap->wr_rate = iwi_cvtrate(frame->rate);
1213 tap->wr_chan_freq =
1214 htole16(ic->ic_channels[frame->chan].ic_freq);
1215 tap->wr_chan_flags =
1216 htole16(ic->ic_channels[frame->chan].ic_flags);
1217 tap->wr_antsignal = frame->signal;
1218 tap->wr_antenna = frame->antenna;
1219
1220 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1221 }
1222 #endif
1223 wh = mtod(m, struct ieee80211_frame *);
1224 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1225
1226 /* Send the frame to the upper layer */
1227 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0);
1228
1229 /* node is no longer needed */
1230 ieee80211_free_node(ni);
1231 }
1232
1233 static void
1234 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif)
1235 {
1236 struct ieee80211com *ic = &sc->sc_ic;
1237 struct iwi_notif_scan_channel *chan;
1238 struct iwi_notif_scan_complete *scan;
1239 struct iwi_notif_authentication *auth;
1240 struct iwi_notif_association *assoc;
1241 struct iwi_notif_beacon_state *beacon;
1242
1243 switch (notif->type) {
1244 case IWI_NOTIF_TYPE_SCAN_CHANNEL:
1245 chan = (struct iwi_notif_scan_channel *)(notif + 1);
1246
1247 DPRINTFN(2, ("Scan of channel %u complete (%u)\n",
1248 ic->ic_channels[chan->nchan].ic_freq, chan->nchan));
1249 break;
1250
1251 case IWI_NOTIF_TYPE_SCAN_COMPLETE:
1252 scan = (struct iwi_notif_scan_complete *)(notif + 1);
1253
1254 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan,
1255 scan->status));
1256
1257 /* monitor mode uses scan to set the channel ... */
1258 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1259 sc->flags &= ~IWI_FLAG_SCANNING;
1260 ieee80211_end_scan(ic);
1261 } else
1262 iwi_set_chan(sc, ic->ic_ibss_chan);
1263 break;
1264
1265 case IWI_NOTIF_TYPE_AUTHENTICATION:
1266 auth = (struct iwi_notif_authentication *)(notif + 1);
1267
1268 DPRINTFN(2, ("Authentication (%u)\n", auth->state));
1269
1270 switch (auth->state) {
1271 case IWI_AUTH_SUCCESS:
1272 ieee80211_node_authorize(ic->ic_bss);
1273 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
1274 break;
1275
1276 case IWI_AUTH_FAIL:
1277 break;
1278
1279 default:
1280 aprint_error_dev(sc->sc_dev,
1281 "unknown authentication state %u\n", auth->state);
1282 }
1283 break;
1284
1285 case IWI_NOTIF_TYPE_ASSOCIATION:
1286 assoc = (struct iwi_notif_association *)(notif + 1);
1287
1288 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state,
1289 assoc->status));
1290
1291 switch (assoc->state) {
1292 case IWI_AUTH_SUCCESS:
1293 /* re-association, do nothing */
1294 break;
1295
1296 case IWI_ASSOC_SUCCESS:
1297 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1298 break;
1299
1300 case IWI_ASSOC_FAIL:
1301 ieee80211_begin_scan(ic, 1);
1302 break;
1303
1304 default:
1305 aprint_error_dev(sc->sc_dev,
1306 "unknown association state %u\n", assoc->state);
1307 }
1308 break;
1309
1310 case IWI_NOTIF_TYPE_BEACON:
1311 beacon = (struct iwi_notif_beacon_state *)(notif + 1);
1312
1313 if (beacon->state == IWI_BEACON_MISS) {
1314 DPRINTFN(5, ("%s: %u beacon(s) missed\n",
1315 device_xname(sc->sc_dev), le32toh(beacon->number)));
1316 }
1317 break;
1318
1319 case IWI_NOTIF_TYPE_FRAG_LENGTH:
1320 case IWI_NOTIF_TYPE_LINK_QUALITY:
1321 case IWI_NOTIF_TYPE_TGI_TX_KEY:
1322 case IWI_NOTIF_TYPE_CALIBRATION:
1323 case IWI_NOTIF_TYPE_NOISE:
1324 DPRINTFN(5, ("Notification (%u)\n", notif->type));
1325 break;
1326
1327 default:
1328 DPRINTF(("%s: unknown notification type %u flags 0x%x len %d\n",
1329 device_xname(sc->sc_dev), notif->type, notif->flags,
1330 le16toh(notif->len)));
1331 }
1332 }
1333
1334 static void
1335 iwi_cmd_intr(struct iwi_softc *sc)
1336 {
1337 uint32_t hw;
1338
1339 hw = CSR_READ_4(sc, IWI_CSR_CMD_RIDX);
1340
1341 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
1342 sc->cmdq.next * IWI_CMD_DESC_SIZE, IWI_CMD_DESC_SIZE,
1343 BUS_DMASYNC_POSTWRITE);
1344
1345 wakeup(&sc->cmdq.desc[sc->cmdq.next]);
1346
1347 sc->cmdq.next = (sc->cmdq.next + 1) % sc->cmdq.count;
1348
1349 if (--sc->cmdq.queued > 0) {
1350 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, (sc->cmdq.next + 1) % sc->cmdq.count);
1351 }
1352 }
1353
1354 static void
1355 iwi_rx_intr(struct iwi_softc *sc)
1356 {
1357 struct iwi_rx_data *data;
1358 struct iwi_hdr *hdr;
1359 uint32_t hw;
1360
1361 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX);
1362
1363 for (; sc->rxq.cur != hw;) {
1364 data = &sc->rxq.data[sc->rxq.cur];
1365
1366 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1367 data->map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1368
1369 hdr = mtod(data->m, struct iwi_hdr *);
1370
1371 switch (hdr->type) {
1372 case IWI_HDR_TYPE_FRAME:
1373 iwi_frame_intr(sc, data, sc->rxq.cur,
1374 (struct iwi_frame *)(hdr + 1));
1375 break;
1376
1377 case IWI_HDR_TYPE_NOTIF:
1378 iwi_notification_intr(sc,
1379 (struct iwi_notif *)(hdr + 1));
1380 break;
1381
1382 default:
1383 aprint_error_dev(sc->sc_dev, "unknown hdr type %u\n",
1384 hdr->type);
1385 }
1386
1387 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1388 data->map->dm_mapsize, BUS_DMASYNC_PREREAD);
1389
1390 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1391
1392 sc->rxq.cur = (sc->rxq.cur + 1) % sc->rxq.count;
1393 }
1394
1395 /* Tell the firmware what we have processed */
1396 hw = (hw == 0) ? sc->rxq.count - 1 : hw - 1;
1397 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw);
1398 }
1399
1400 static void
1401 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq)
1402 {
1403 struct ifnet *ifp = &sc->sc_if;
1404 struct iwi_tx_data *data;
1405 uint32_t hw;
1406
1407 hw = CSR_READ_4(sc, txq->csr_ridx);
1408
1409 for (; txq->next != hw;) {
1410 data = &txq->data[txq->next];
1411
1412 bus_dmamap_sync(sc->sc_dmat, data->map, 0,
1413 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1414 bus_dmamap_unload(sc->sc_dmat, data->map);
1415 m_freem(data->m);
1416 data->m = NULL;
1417 ieee80211_free_node(data->ni);
1418 data->ni = NULL;
1419
1420 DPRINTFN(15, ("tx done idx=%u\n", txq->next));
1421
1422 ifp->if_opackets++;
1423
1424 txq->queued--;
1425 txq->next = (txq->next + 1) % txq->count;
1426 }
1427
1428 sc->sc_tx_timer = 0;
1429 ifp->if_flags &= ~IFF_OACTIVE;
1430
1431 /* Call start() since some buffer descriptors have been released */
1432 (*ifp->if_start)(ifp);
1433 }
1434
1435 static int
1436 iwi_intr(void *arg)
1437 {
1438 struct iwi_softc *sc = arg;
1439 uint32_t r;
1440
1441 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff)
1442 return 0;
1443
1444 /* Acknowledge interrupts */
1445 CSR_WRITE_4(sc, IWI_CSR_INTR, r);
1446
1447 if (r & IWI_INTR_FATAL_ERROR) {
1448 aprint_error_dev(sc->sc_dev, "fatal error\n");
1449 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1450 iwi_stop(&sc->sc_if, 1);
1451 return (1);
1452 }
1453
1454 if (r & IWI_INTR_FW_INITED) {
1455 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)))
1456 wakeup(sc);
1457 }
1458
1459 if (r & IWI_INTR_RADIO_OFF) {
1460 DPRINTF(("radio transmitter off\n"));
1461 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1462 iwi_stop(&sc->sc_if, 1);
1463 return (1);
1464 }
1465
1466 if (r & IWI_INTR_CMD_DONE)
1467 iwi_cmd_intr(sc);
1468
1469 if (r & IWI_INTR_TX1_DONE)
1470 iwi_tx_intr(sc, &sc->txq[0]);
1471
1472 if (r & IWI_INTR_TX2_DONE)
1473 iwi_tx_intr(sc, &sc->txq[1]);
1474
1475 if (r & IWI_INTR_TX3_DONE)
1476 iwi_tx_intr(sc, &sc->txq[2]);
1477
1478 if (r & IWI_INTR_TX4_DONE)
1479 iwi_tx_intr(sc, &sc->txq[3]);
1480
1481 if (r & IWI_INTR_RX_DONE)
1482 iwi_rx_intr(sc);
1483
1484 if (r & IWI_INTR_PARITY_ERROR)
1485 aprint_error_dev(sc->sc_dev, "parity error\n");
1486
1487 return 1;
1488 }
1489
1490 static int
1491 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len,
1492 int async)
1493 {
1494 struct iwi_cmd_desc *desc;
1495
1496 desc = &sc->cmdq.desc[sc->cmdq.cur];
1497
1498 desc->hdr.type = IWI_HDR_TYPE_COMMAND;
1499 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1500 desc->type = type;
1501 desc->len = len;
1502 memcpy(desc->data, data, len);
1503
1504 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map,
1505 sc->cmdq.cur * IWI_CMD_DESC_SIZE,
1506 IWI_CMD_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1507
1508 DPRINTFN(2, ("sending command idx=%u type=%u len=%u async=%d\n",
1509 sc->cmdq.cur, type, len, async));
1510
1511 sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count;
1512
1513 if (++sc->cmdq.queued == 1)
1514 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
1515
1516 return async ? 0 : tsleep(desc, 0, "iwicmd", hz);
1517 }
1518
1519 static void
1520 iwi_write_ibssnode(struct iwi_softc *sc, const struct iwi_node *in)
1521 {
1522 struct iwi_ibssnode node;
1523
1524 /* write node information into NIC memory */
1525 memset(&node, 0, sizeof node);
1526 IEEE80211_ADDR_COPY(node.bssid, in->in_node.ni_macaddr);
1527
1528 CSR_WRITE_REGION_1(sc,
1529 IWI_CSR_NODE_BASE + in->in_station * sizeof node,
1530 (uint8_t *)&node, sizeof node);
1531 }
1532
1533 static int
1534 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni,
1535 int ac)
1536 {
1537 struct iwi_softc *sc = ifp->if_softc;
1538 struct ieee80211com *ic = &sc->sc_ic;
1539 struct iwi_node *in = (struct iwi_node *)ni;
1540 struct ieee80211_frame *wh;
1541 struct ieee80211_key *k;
1542 const struct chanAccParams *cap;
1543 struct iwi_tx_ring *txq = &sc->txq[ac];
1544 struct iwi_tx_data *data;
1545 struct iwi_tx_desc *desc;
1546 struct mbuf *mnew;
1547 int error, hdrlen, i, noack = 0;
1548
1549 wh = mtod(m0, struct ieee80211_frame *);
1550
1551 if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
1552 hdrlen = sizeof (struct ieee80211_qosframe);
1553 cap = &ic->ic_wme.wme_chanParams;
1554 noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1555 } else
1556 hdrlen = sizeof (struct ieee80211_frame);
1557
1558 /*
1559 * This is only used in IBSS mode where the firmware expect an index
1560 * in a h/w table instead of a destination address.
1561 */
1562 if (ic->ic_opmode == IEEE80211_M_IBSS && in->in_station == -1) {
1563 in->in_station = iwi_alloc_unr(sc);
1564
1565 if (in->in_station == -1) { /* h/w table is full */
1566 m_freem(m0);
1567 ieee80211_free_node(ni);
1568 ifp->if_oerrors++;
1569 return 0;
1570 }
1571 iwi_write_ibssnode(sc, in);
1572 }
1573
1574 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1575 k = ieee80211_crypto_encap(ic, ni, m0);
1576 if (k == NULL) {
1577 m_freem(m0);
1578 return ENOBUFS;
1579 }
1580
1581 /* packet header may have moved, reset our local pointer */
1582 wh = mtod(m0, struct ieee80211_frame *);
1583 }
1584
1585 #if NBPFILTER > 0
1586 if (sc->sc_drvbpf != NULL) {
1587 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap;
1588
1589 tap->wt_flags = 0;
1590 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq);
1591 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags);
1592
1593 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1594 }
1595 #endif
1596
1597 data = &txq->data[txq->cur];
1598 desc = &txq->desc[txq->cur];
1599
1600 /* save and trim IEEE802.11 header */
1601 m_copydata(m0, 0, hdrlen, (void *)&desc->wh);
1602 m_adj(m0, hdrlen);
1603
1604 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1605 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1606 if (error != 0 && error != EFBIG) {
1607 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n",
1608 error);
1609 m_freem(m0);
1610 return error;
1611 }
1612 if (error != 0) {
1613 /* too many fragments, linearize */
1614
1615 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1616 if (mnew == NULL) {
1617 m_freem(m0);
1618 return ENOMEM;
1619 }
1620
1621 M_COPY_PKTHDR(mnew, m0);
1622
1623 /* If the data won't fit in the header, get a cluster */
1624 if (m0->m_pkthdr.len > MHLEN) {
1625 MCLGET(mnew, M_DONTWAIT);
1626 if (!(mnew->m_flags & M_EXT)) {
1627 m_freem(m0);
1628 m_freem(mnew);
1629 return ENOMEM;
1630 }
1631 }
1632 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *));
1633 m_freem(m0);
1634 mnew->m_len = mnew->m_pkthdr.len;
1635 m0 = mnew;
1636
1637 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1638 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1639 if (error != 0) {
1640 aprint_error_dev(sc->sc_dev,
1641 "could not map mbuf (error %d)\n", error);
1642 m_freem(m0);
1643 return error;
1644 }
1645 }
1646
1647 data->m = m0;
1648 data->ni = ni;
1649
1650 desc->hdr.type = IWI_HDR_TYPE_DATA;
1651 desc->hdr.flags = IWI_HDR_FLAG_IRQ;
1652 desc->station =
1653 (ic->ic_opmode == IEEE80211_M_IBSS) ? in->in_station : 0;
1654 desc->cmd = IWI_DATA_CMD_TX;
1655 desc->len = htole16(m0->m_pkthdr.len);
1656 desc->flags = 0;
1657 desc->xflags = 0;
1658
1659 if (!noack && !IEEE80211_IS_MULTICAST(desc->wh.i_addr1))
1660 desc->flags |= IWI_DATA_FLAG_NEED_ACK;
1661
1662 #if 0
1663 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
1664 desc->wh.i_fc[1] |= IEEE80211_FC1_WEP;
1665 desc->wep_txkey = ic->ic_crypto.cs_def_txkey;
1666 } else
1667 #endif
1668 desc->flags |= IWI_DATA_FLAG_NO_WEP;
1669
1670 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1671 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE;
1672
1673 if (desc->wh.i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS)
1674 desc->xflags |= IWI_DATA_XFLAG_QOS;
1675
1676 if (ic->ic_curmode == IEEE80211_MODE_11B)
1677 desc->xflags |= IWI_DATA_XFLAG_CCK;
1678
1679 desc->nseg = htole32(data->map->dm_nsegs);
1680 for (i = 0; i < data->map->dm_nsegs; i++) {
1681 desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr);
1682 desc->seg_len[i] = htole16(data->map->dm_segs[i].ds_len);
1683 }
1684
1685 bus_dmamap_sync(sc->sc_dmat, txq->desc_map,
1686 txq->cur * IWI_TX_DESC_SIZE,
1687 IWI_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE);
1688
1689 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize,
1690 BUS_DMASYNC_PREWRITE);
1691
1692 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n",
1693 ac, txq->cur, le16toh(desc->len), le32toh(desc->nseg)));
1694
1695 /* Inform firmware about this new packet */
1696 txq->queued++;
1697 txq->cur = (txq->cur + 1) % txq->count;
1698 CSR_WRITE_4(sc, txq->csr_widx, txq->cur);
1699
1700 return 0;
1701 }
1702
1703 static void
1704 iwi_start(struct ifnet *ifp)
1705 {
1706 struct iwi_softc *sc = ifp->if_softc;
1707 struct ieee80211com *ic = &sc->sc_ic;
1708 struct mbuf *m0;
1709 struct ether_header *eh;
1710 struct ieee80211_node *ni;
1711 int ac;
1712
1713 if (ic->ic_state != IEEE80211_S_RUN)
1714 return;
1715
1716 for (;;) {
1717 IF_DEQUEUE(&ifp->if_snd, m0);
1718 if (m0 == NULL)
1719 break;
1720
1721 if (m0->m_len < sizeof (struct ether_header) &&
1722 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) {
1723 ifp->if_oerrors++;
1724 continue;
1725 }
1726
1727 eh = mtod(m0, struct ether_header *);
1728 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1729 if (ni == NULL) {
1730 m_freem(m0);
1731 ifp->if_oerrors++;
1732 continue;
1733 }
1734
1735 /* classify mbuf so we can find which tx ring to use */
1736 if (ieee80211_classify(ic, m0, ni) != 0) {
1737 m_freem(m0);
1738 ieee80211_free_node(ni);
1739 ifp->if_oerrors++;
1740 continue;
1741 }
1742
1743 /* no QoS encapsulation for EAPOL frames */
1744 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1745 M_WME_GETAC(m0) : WME_AC_BE;
1746
1747 if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
1748 /* there is no place left in this ring */
1749 IF_PREPEND(&ifp->if_snd, m0);
1750 ifp->if_flags |= IFF_OACTIVE;
1751 break;
1752 }
1753
1754 #if NBPFILTER > 0
1755 if (ifp->if_bpf != NULL)
1756 bpf_mtap(ifp->if_bpf, m0);
1757 #endif
1758
1759 m0 = ieee80211_encap(ic, m0, ni);
1760 if (m0 == NULL) {
1761 ieee80211_free_node(ni);
1762 ifp->if_oerrors++;
1763 continue;
1764 }
1765
1766 #if NBPFILTER > 0
1767 if (ic->ic_rawbpf != NULL)
1768 bpf_mtap(ic->ic_rawbpf, m0);
1769 #endif
1770
1771 if (iwi_tx_start(ifp, m0, ni, ac) != 0) {
1772 ieee80211_free_node(ni);
1773 ifp->if_oerrors++;
1774 break;
1775 }
1776
1777 /* start watchdog timer */
1778 sc->sc_tx_timer = 5;
1779 ifp->if_timer = 1;
1780 }
1781 }
1782
1783 static void
1784 iwi_watchdog(struct ifnet *ifp)
1785 {
1786 struct iwi_softc *sc = ifp->if_softc;
1787
1788 ifp->if_timer = 0;
1789
1790 if (sc->sc_tx_timer > 0) {
1791 if (--sc->sc_tx_timer == 0) {
1792 aprint_error_dev(sc->sc_dev, "device timeout\n");
1793 ifp->if_oerrors++;
1794 ifp->if_flags &= ~IFF_UP;
1795 iwi_stop(ifp, 1);
1796 return;
1797 }
1798 ifp->if_timer = 1;
1799 }
1800
1801 ieee80211_watchdog(&sc->sc_ic);
1802 }
1803
1804 static int
1805 iwi_get_table0(struct iwi_softc *sc, uint32_t *tbl)
1806 {
1807 uint32_t size, buf[128];
1808
1809 if (!(sc->flags & IWI_FLAG_FW_INITED)) {
1810 memset(buf, 0, sizeof buf);
1811 return copyout(buf, tbl, sizeof buf);
1812 }
1813
1814 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1);
1815 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size);
1816
1817 return copyout(buf, tbl, sizeof buf);
1818 }
1819
1820 static int
1821 iwi_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1822 {
1823 #define IS_RUNNING(ifp) \
1824 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
1825
1826 struct iwi_softc *sc = ifp->if_softc;
1827 struct ieee80211com *ic = &sc->sc_ic;
1828 struct ifreq *ifr = (struct ifreq *)data;
1829 int s, error = 0;
1830 int val;
1831
1832 s = splnet();
1833
1834 switch (cmd) {
1835 case SIOCSIFFLAGS:
1836 if (ifp->if_flags & IFF_UP) {
1837 if (!(ifp->if_flags & IFF_RUNNING))
1838 iwi_init(ifp);
1839 } else {
1840 if (ifp->if_flags & IFF_RUNNING)
1841 iwi_stop(ifp, 1);
1842 }
1843 break;
1844
1845 case SIOCADDMULTI:
1846 case SIOCDELMULTI:
1847 /* XXX no h/w multicast filter? --dyoung */
1848 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1849 /* setup multicast filter, etc */
1850 error = 0;
1851 }
1852 break;
1853
1854 case SIOCGTABLE0:
1855 error = iwi_get_table0(sc, (uint32_t *)ifr->ifr_data);
1856 break;
1857
1858 case SIOCGRADIO:
1859 val = !iwi_getrfkill(sc);
1860 error = copyout(&val, (int *)ifr->ifr_data, sizeof val);
1861 break;
1862
1863 case SIOCSIFMEDIA:
1864 if (ifr->ifr_media & IFM_IEEE80211_ADHOC) {
1865 sc->sc_fwname = "iwi-ibss.fw";
1866 } else if (ifr->ifr_media & IFM_IEEE80211_MONITOR) {
1867 sc->sc_fwname = "iwi-sniffer.fw";
1868 } else {
1869 sc->sc_fwname = "iwi-bss.fw";
1870 }
1871 error = iwi_cache_firmware(sc);
1872 if (error)
1873 break;
1874 /* FALLTRHOUGH */
1875
1876 default:
1877 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
1878
1879 if (error == ENETRESET) {
1880 if (IS_RUNNING(ifp) &&
1881 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1882 iwi_init(ifp);
1883 error = 0;
1884 }
1885 }
1886
1887 splx(s);
1888 return error;
1889 #undef IS_RUNNING
1890 }
1891
1892 static void
1893 iwi_stop_master(struct iwi_softc *sc)
1894 {
1895 int ntries;
1896
1897 /* Disable interrupts */
1898 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0);
1899
1900 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER);
1901 for (ntries = 0; ntries < 5; ntries++) {
1902 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1903 break;
1904 DELAY(10);
1905 }
1906 if (ntries == 5)
1907 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
1908
1909 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1910 IWI_RST_PRINCETON_RESET);
1911
1912 sc->flags &= ~IWI_FLAG_FW_INITED;
1913 }
1914
1915 static int
1916 iwi_reset(struct iwi_softc *sc)
1917 {
1918 int i, ntries;
1919
1920 iwi_stop_master(sc);
1921
1922 /* Move adapter to D0 state */
1923 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1924 IWI_CTL_INIT);
1925
1926 /* Initialize Phase-Locked Level (PLL) */
1927 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST);
1928
1929 /* Wait for clock stabilization */
1930 for (ntries = 0; ntries < 1000; ntries++) {
1931 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY)
1932 break;
1933 DELAY(200);
1934 }
1935 if (ntries == 1000) {
1936 aprint_error_dev(sc->sc_dev,
1937 "timeout waiting for clock stabilization\n");
1938 return ETIMEDOUT;
1939 }
1940
1941 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1942 IWI_RST_SW_RESET);
1943
1944 DELAY(10);
1945
1946 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
1947 IWI_CTL_INIT);
1948
1949 /* Clear NIC memory */
1950 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0);
1951 for (i = 0; i < 0xc000; i++)
1952 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
1953
1954 return 0;
1955 }
1956
1957 static int
1958 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size)
1959 {
1960 uint16_t *w;
1961 int ntries, i;
1962
1963 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) |
1964 IWI_RST_STOP_MASTER);
1965 for (ntries = 0; ntries < 5; ntries++) {
1966 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED)
1967 break;
1968 DELAY(10);
1969 }
1970 if (ntries == 5) {
1971 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n");
1972 return ETIMEDOUT;
1973 }
1974
1975 MEM_WRITE_4(sc, 0x3000e0, 0x80000000);
1976 DELAY(5000);
1977 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
1978 ~IWI_RST_PRINCETON_RESET);
1979 DELAY(5000);
1980 MEM_WRITE_4(sc, 0x3000e0, 0);
1981 DELAY(1000);
1982 MEM_WRITE_4(sc, 0x300004, 1);
1983 DELAY(1000);
1984 MEM_WRITE_4(sc, 0x300004, 0);
1985 DELAY(1000);
1986 MEM_WRITE_1(sc, 0x200000, 0x00);
1987 MEM_WRITE_1(sc, 0x200000, 0x40);
1988 DELAY(1000);
1989
1990 /* Adapter is buggy, we must set the address for each word */
1991 for (w = uc; size > 0; w++, size -= 2)
1992 MEM_WRITE_2(sc, 0x200010, htole16(*w));
1993
1994 MEM_WRITE_1(sc, 0x200000, 0x00);
1995 MEM_WRITE_1(sc, 0x200000, 0x80);
1996
1997 /* Wait until we get a response in the uc queue */
1998 for (ntries = 0; ntries < 100; ntries++) {
1999 if (MEM_READ_1(sc, 0x200000) & 1)
2000 break;
2001 DELAY(100);
2002 }
2003 if (ntries == 100) {
2004 aprint_error_dev(sc->sc_dev,
2005 "timeout waiting for ucode to initialize\n");
2006 return ETIMEDOUT;
2007 }
2008
2009 /* Empty the uc queue or the firmware will not initialize properly */
2010 for (i = 0; i < 7; i++)
2011 MEM_READ_4(sc, 0x200004);
2012
2013 MEM_WRITE_1(sc, 0x200000, 0x00);
2014
2015 return 0;
2016 }
2017
2018 /* macro to handle unaligned little endian data in firmware image */
2019 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24)
2020 static int
2021 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size)
2022 {
2023 bus_dmamap_t map;
2024 u_char *p, *end;
2025 uint32_t sentinel, ctl, sum;
2026 uint32_t cs, sl, cd, cl;
2027 int ntries, nsegs, error;
2028 int sn;
2029
2030 nsegs = (size + PAGE_SIZE - 1) / PAGE_SIZE;
2031
2032 /* Create a DMA map for the firmware image */
2033 error = bus_dmamap_create(sc->sc_dmat, size, nsegs, size, 0,
2034 BUS_DMA_NOWAIT, &map);
2035 if (error != 0) {
2036 aprint_error_dev(sc->sc_dev,
2037 "could not create firmware DMA map\n");
2038 goto fail1;
2039 }
2040
2041 error = bus_dmamap_load(sc->sc_dmat, map, fw, size, NULL,
2042 BUS_DMA_NOWAIT | BUS_DMA_WRITE);
2043 if (error != 0) {
2044 aprint_error_dev(sc->sc_dev, "could not load fw dma map(%d)\n",
2045 error);
2046 goto fail2;
2047 }
2048
2049 /* Make sure the adapter will get up-to-date values */
2050 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
2051
2052 /* Tell the adapter where the command blocks are stored */
2053 MEM_WRITE_4(sc, 0x3000a0, 0x27000);
2054
2055 /*
2056 * Store command blocks into adapter's internal memory using register
2057 * indirections. The adapter will read the firmware image through DMA
2058 * using information stored in command blocks.
2059 */
2060 p = fw;
2061 end = p + size;
2062 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000);
2063
2064 sn = 0;
2065 sl = cl = 0;
2066 cs = cd = 0;
2067 while (p < end) {
2068 if (sl == 0) {
2069 cs = map->dm_segs[sn].ds_addr;
2070 sl = map->dm_segs[sn].ds_len;
2071 sn++;
2072 }
2073 if (cl == 0) {
2074 cd = GETLE32(p); p += 4; cs += 4; sl -= 4;
2075 cl = GETLE32(p); p += 4; cs += 4; sl -= 4;
2076 }
2077 while (sl > 0 && cl > 0) {
2078 int len = min(cl, sl);
2079
2080 sl -= len;
2081 cl -= len;
2082 p += len;
2083
2084 while (len > 0) {
2085 int mlen = min(len, IWI_CB_MAXDATALEN);
2086
2087 ctl = IWI_CB_DEFAULT_CTL | mlen;
2088 sum = ctl ^ cs ^ cd;
2089
2090 /* Write a command block */
2091 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl);
2092 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cs);
2093 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cd);
2094 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum);
2095
2096 cs += mlen;
2097 cd += mlen;
2098 len -= mlen;
2099 }
2100 }
2101 }
2102
2103 /* Write a fictive final command block (sentinel) */
2104 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR);
2105 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0);
2106
2107 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) &
2108 ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER));
2109
2110 /* Tell the adapter to start processing command blocks */
2111 MEM_WRITE_4(sc, 0x3000a4, 0x540100);
2112
2113 /* Wait until the adapter has processed all command blocks */
2114 for (ntries = 0; ntries < 400; ntries++) {
2115 if (MEM_READ_4(sc, 0x3000d0) >= sentinel)
2116 break;
2117 DELAY(100);
2118 }
2119 if (ntries == 400) {
2120 aprint_error_dev(sc->sc_dev, "timeout processing cb\n");
2121 error = ETIMEDOUT;
2122 goto fail3;
2123 }
2124
2125 /* We're done with command blocks processing */
2126 MEM_WRITE_4(sc, 0x3000a4, 0x540c00);
2127
2128 /* Allow interrupts so we know when the firmware is inited */
2129 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK);
2130
2131 /* Tell the adapter to initialize the firmware */
2132 CSR_WRITE_4(sc, IWI_CSR_RST, 0);
2133 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) |
2134 IWI_CTL_ALLOW_STANDBY);
2135
2136 /* Wait at most one second for firmware initialization to complete */
2137 if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) {
2138 aprint_error_dev(sc->sc_dev,
2139 "timeout waiting for firmware initialization to complete\n");
2140 goto fail3;
2141 }
2142
2143 fail3:
2144 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
2145 bus_dmamap_unload(sc->sc_dmat, map);
2146 fail2:
2147 bus_dmamap_destroy(sc->sc_dmat, map);
2148
2149 fail1:
2150 return error;
2151 }
2152
2153 /*
2154 * Store firmware into kernel memory so we can download it when we need to,
2155 * e.g when the adapter wakes up from suspend mode.
2156 */
2157 static int
2158 iwi_cache_firmware(struct iwi_softc *sc)
2159 {
2160 struct iwi_firmware *kfw = &sc->fw;
2161 firmware_handle_t fwh;
2162 const struct iwi_firmware_hdr *hdr;
2163 off_t size;
2164 char *fw;
2165 int error;
2166
2167 iwi_free_firmware(sc);
2168 error = firmware_open("if_iwi", sc->sc_fwname, &fwh);
2169 if (error != 0) {
2170 aprint_error_dev(sc->sc_dev, "firmware_open failed\n");
2171 goto fail1;
2172 }
2173
2174 size = firmware_get_size(fwh);
2175 if (size < sizeof(struct iwi_firmware_hdr)) {
2176 aprint_error_dev(sc->sc_dev, "image '%s' has no header\n",
2177 sc->sc_fwname);
2178 error = EIO;
2179 goto fail1;
2180 }
2181
2182 sc->sc_blob = firmware_malloc(size);
2183 if (sc->sc_blob == NULL) {
2184 error = ENOMEM;
2185 firmware_close(fwh);
2186 goto fail1;
2187 }
2188
2189 error = firmware_read(fwh, 0, sc->sc_blob, size);
2190 firmware_close(fwh);
2191 if (error != 0)
2192 goto fail2;
2193
2194
2195 hdr = (const struct iwi_firmware_hdr *)sc->sc_blob;
2196 if (size < sizeof(struct iwi_firmware_hdr) + hdr->bsize + hdr->usize + hdr->fsize) {
2197 aprint_error_dev(sc->sc_dev, "image '%s' too small\n",
2198 sc->sc_fwname);
2199 error = EIO;
2200 goto fail2;
2201 }
2202
2203 hdr = (const struct iwi_firmware_hdr *)sc->sc_blob;
2204 DPRINTF(("firmware version = %d\n", le32toh(hdr->version)));
2205 if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) ||
2206 (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) {
2207 aprint_error_dev(sc->sc_dev,
2208 "version for '%s' %d.%d != %d.%d\n", sc->sc_fwname,
2209 IWI_FW_GET_MAJOR(le32toh(hdr->version)),
2210 IWI_FW_GET_MINOR(le32toh(hdr->version)),
2211 IWI_FW_REQ_MAJOR, IWI_FW_REQ_MINOR);
2212 error = EIO;
2213 goto fail2;
2214 }
2215
2216 kfw->boot_size = hdr->bsize;
2217 kfw->ucode_size = hdr->usize;
2218 kfw->main_size = hdr->fsize;
2219
2220 fw = sc->sc_blob + sizeof(struct iwi_firmware_hdr);
2221 kfw->boot = fw;
2222 fw += kfw->boot_size;
2223 kfw->ucode = fw;
2224 fw += kfw->ucode_size;
2225 kfw->main = fw;
2226
2227 DPRINTF(("Firmware cached: boot %p, ucode %p, main %p\n",
2228 kfw->boot, kfw->ucode, kfw->main));
2229 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n",
2230 kfw->boot_size, kfw->ucode_size, kfw->main_size));
2231
2232 sc->flags |= IWI_FLAG_FW_CACHED;
2233
2234 return 0;
2235
2236
2237 fail2: firmware_free(sc->sc_blob, 0);
2238 fail1:
2239 return error;
2240 }
2241
2242 static void
2243 iwi_free_firmware(struct iwi_softc *sc)
2244 {
2245
2246 if (!(sc->flags & IWI_FLAG_FW_CACHED))
2247 return;
2248
2249 firmware_free(sc->sc_blob, 0);
2250
2251 sc->flags &= ~IWI_FLAG_FW_CACHED;
2252 }
2253
2254 static int
2255 iwi_config(struct iwi_softc *sc)
2256 {
2257 struct ieee80211com *ic = &sc->sc_ic;
2258 struct ifnet *ifp = &sc->sc_if;
2259 struct iwi_configuration config;
2260 struct iwi_rateset rs;
2261 struct iwi_txpower power;
2262 struct ieee80211_key *wk;
2263 struct iwi_wep_key wepkey;
2264 uint32_t data;
2265 int error, nchan, i;
2266
2267 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2268 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr)));
2269 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr,
2270 IEEE80211_ADDR_LEN, 0);
2271 if (error != 0)
2272 return error;
2273
2274 memset(&config, 0, sizeof config);
2275 config.bluetooth_coexistence = sc->bluetooth;
2276 config.antenna = sc->antenna;
2277 config.silence_threshold = 0x1e;
2278 config.multicast_enabled = 1;
2279 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2280 config.disable_unicast_decryption = 1;
2281 config.disable_multicast_decryption = 1;
2282 DPRINTF(("Configuring adapter\n"));
2283 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config,
2284 0);
2285 if (error != 0)
2286 return error;
2287
2288 data = htole32(IWI_POWER_MODE_CAM);
2289 DPRINTF(("Setting power mode to %u\n", le32toh(data)));
2290 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0);
2291 if (error != 0)
2292 return error;
2293
2294 data = htole32(ic->ic_rtsthreshold);
2295 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data)));
2296 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0);
2297 if (error != 0)
2298 return error;
2299
2300 data = htole32(ic->ic_fragthreshold);
2301 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data)));
2302 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0);
2303 if (error != 0)
2304 return error;
2305
2306 /*
2307 * Set default Tx power for 802.11b/g and 802.11a channels.
2308 */
2309 nchan = 0;
2310 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2311 if (!IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]))
2312 continue;
2313 power.chan[nchan].chan = i;
2314 power.chan[nchan].power = IWI_TXPOWER_MAX;
2315 nchan++;
2316 }
2317 power.nchan = nchan;
2318
2319 power.mode = IWI_MODE_11G;
2320 DPRINTF(("Setting .11g channels tx power\n"));
2321 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
2322 if (error != 0)
2323 return error;
2324
2325 power.mode = IWI_MODE_11B;
2326 DPRINTF(("Setting .11b channels tx power\n"));
2327 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0);
2328 if (error != 0)
2329 return error;
2330
2331 nchan = 0;
2332 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2333 if (!IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]))
2334 continue;
2335 power.chan[nchan].chan = i;
2336 power.chan[nchan].power = IWI_TXPOWER_MAX;
2337 nchan++;
2338 }
2339 power.nchan = nchan;
2340
2341 if (nchan > 0) { /* 2915ABG only */
2342 power.mode = IWI_MODE_11A;
2343 DPRINTF(("Setting .11a channels tx power\n"));
2344 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power,
2345 0);
2346 if (error != 0)
2347 return error;
2348 }
2349
2350 rs.mode = IWI_MODE_11G;
2351 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2352 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates;
2353 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates,
2354 rs.nrates);
2355 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates));
2356 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2357 if (error != 0)
2358 return error;
2359
2360 rs.mode = IWI_MODE_11A;
2361 rs.type = IWI_RATESET_TYPE_SUPPORTED;
2362 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates;
2363 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates,
2364 rs.nrates);
2365 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates));
2366 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0);
2367 if (error != 0)
2368 return error;
2369
2370 /* if we have a desired ESSID, set it now */
2371 if (ic->ic_des_esslen != 0) {
2372 #ifdef IWI_DEBUG
2373 if (iwi_debug > 0) {
2374 printf("Setting desired ESSID to ");
2375 ieee80211_print_essid(ic->ic_des_essid,
2376 ic->ic_des_esslen);
2377 printf("\n");
2378 }
2379 #endif
2380 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid,
2381 ic->ic_des_esslen, 0);
2382 if (error != 0)
2383 return error;
2384 }
2385
2386 data = htole32(arc4random());
2387 DPRINTF(("Setting initialization vector to %u\n", le32toh(data)));
2388 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0);
2389 if (error != 0)
2390 return error;
2391
2392 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2393 /* XXX iwi_setwepkeys? */
2394 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
2395 wk = &ic->ic_crypto.cs_nw_keys[i];
2396
2397 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY;
2398 wepkey.idx = i;
2399 wepkey.len = wk->wk_keylen;
2400 memset(wepkey.key, 0, sizeof wepkey.key);
2401 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen);
2402 DPRINTF(("Setting wep key index %u len %u\n",
2403 wepkey.idx, wepkey.len));
2404 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey,
2405 sizeof wepkey, 0);
2406 if (error != 0)
2407 return error;
2408 }
2409 }
2410
2411 /* Enable adapter */
2412 DPRINTF(("Enabling adapter\n"));
2413 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0);
2414 }
2415
2416 static int
2417 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan)
2418 {
2419 struct ieee80211com *ic = &sc->sc_ic;
2420 struct iwi_scan_v2 scan;
2421
2422 (void)memset(&scan, 0, sizeof scan);
2423
2424 scan.dwelltime[IWI_SCAN_TYPE_PASSIVE] = htole16(2000);
2425 scan.channels[0] = 1 |
2426 (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ);
2427 scan.channels[1] = ieee80211_chan2ieee(ic, chan);
2428 iwi_scan_type_set(scan, 1, IWI_SCAN_TYPE_PASSIVE);
2429
2430 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan)));
2431 return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
2432 }
2433
2434 static int
2435 iwi_scan(struct iwi_softc *sc)
2436 {
2437 struct ieee80211com *ic = &sc->sc_ic;
2438 struct iwi_scan_v2 scan;
2439 uint32_t type;
2440 uint8_t *p;
2441 int i, count, idx;
2442
2443 (void)memset(&scan, 0, sizeof scan);
2444 scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BROADCAST] =
2445 htole16(sc->dwelltime);
2446 scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BDIRECT] =
2447 htole16(sc->dwelltime);
2448
2449 /* tell the firmware about the desired essid */
2450 if (ic->ic_des_esslen) {
2451 int error;
2452
2453 DPRINTF(("%s: Setting adapter desired ESSID to %s\n",
2454 __func__, ic->ic_des_essid));
2455
2456 error = iwi_cmd(sc, IWI_CMD_SET_ESSID,
2457 ic->ic_des_essid, ic->ic_des_esslen, 1);
2458 if (error)
2459 return error;
2460
2461 type = IWI_SCAN_TYPE_ACTIVE_BDIRECT;
2462 } else {
2463 type = IWI_SCAN_TYPE_ACTIVE_BROADCAST;
2464 }
2465
2466 p = &scan.channels[0];
2467 count = idx = 0;
2468 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2469 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) &&
2470 isset(ic->ic_chan_active, i)) {
2471 *++p = i;
2472 count++;
2473 idx++;
2474 iwi_scan_type_set(scan, idx, type);
2475 }
2476 }
2477 if (count) {
2478 *(p - count) = IWI_CHAN_5GHZ | count;
2479 p++;
2480 }
2481
2482 count = 0;
2483 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
2484 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) &&
2485 isset(ic->ic_chan_active, i)) {
2486 *++p = i;
2487 count++;
2488 idx++;
2489 iwi_scan_type_set(scan, idx, type);
2490 }
2491 }
2492 *(p - count) = IWI_CHAN_2GHZ | count;
2493
2494 DPRINTF(("Start scanning\n"));
2495 return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1);
2496 }
2497
2498 static int
2499 iwi_auth_and_assoc(struct iwi_softc *sc)
2500 {
2501 struct ieee80211com *ic = &sc->sc_ic;
2502 struct ieee80211_node *ni = ic->ic_bss;
2503 struct ifnet *ifp = &sc->sc_if;
2504 struct ieee80211_wme_info wme;
2505 struct iwi_configuration config;
2506 struct iwi_associate assoc;
2507 struct iwi_rateset rs;
2508 uint16_t capinfo;
2509 uint32_t data;
2510 int error;
2511
2512 memset(&config, 0, sizeof config);
2513 config.bluetooth_coexistence = sc->bluetooth;
2514 config.antenna = sc->antenna;
2515 config.multicast_enabled = 1;
2516 config.silence_threshold = 0x1e;
2517 if (ic->ic_curmode == IEEE80211_MODE_11G)
2518 config.use_protection = 1;
2519 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0;
2520 config.disable_unicast_decryption = 1;
2521 config.disable_multicast_decryption = 1;
2522
2523 DPRINTF(("Configuring adapter\n"));
2524 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config,
2525 sizeof config, 1);
2526 if (error != 0)
2527 return error;
2528
2529 #ifdef IWI_DEBUG
2530 if (iwi_debug > 0) {
2531 aprint_debug_dev(sc->sc_dev, "Setting ESSID to ");
2532 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
2533 aprint_debug("\n");
2534 }
2535 #endif
2536 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1);
2537 if (error != 0)
2538 return error;
2539
2540 /* the rate set has already been "negotiated" */
2541 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A :
2542 IWI_MODE_11G;
2543 rs.type = IWI_RATESET_TYPE_NEGOTIATED;
2544 rs.nrates = ni->ni_rates.rs_nrates;
2545
2546 if (rs.nrates > IWI_RATESET_SIZE) {
2547 DPRINTF(("Truncating negotiated rate set from %u\n",
2548 rs.nrates));
2549 rs.nrates = IWI_RATESET_SIZE;
2550 }
2551 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates);
2552 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates));
2553 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1);
2554 if (error != 0)
2555 return error;
2556
2557 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) {
2558 wme.wme_id = IEEE80211_ELEMID_VENDOR;
2559 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2;
2560 wme.wme_oui[0] = 0x00;
2561 wme.wme_oui[1] = 0x50;
2562 wme.wme_oui[2] = 0xf2;
2563 wme.wme_type = WME_OUI_TYPE;
2564 wme.wme_subtype = WME_INFO_OUI_SUBTYPE;
2565 wme.wme_version = WME_VERSION;
2566 wme.wme_info = 0;
2567
2568 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len));
2569 error = iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme, 1);
2570 if (error != 0)
2571 return error;
2572 }
2573
2574 if (ic->ic_opt_ie != NULL) {
2575 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len));
2576 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie,
2577 ic->ic_opt_ie_len, 1);
2578 if (error != 0)
2579 return error;
2580 }
2581 data = htole32(ni->ni_rssi);
2582 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi));
2583 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1);
2584 if (error != 0)
2585 return error;
2586
2587 memset(&assoc, 0, sizeof assoc);
2588 if (IEEE80211_IS_CHAN_A(ni->ni_chan))
2589 assoc.mode = IWI_MODE_11A;
2590 else if (IEEE80211_IS_CHAN_G(ni->ni_chan))
2591 assoc.mode = IWI_MODE_11G;
2592 else if (IEEE80211_IS_CHAN_B(ni->ni_chan))
2593 assoc.mode = IWI_MODE_11B;
2594
2595 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2596
2597 if (ni->ni_authmode == IEEE80211_AUTH_SHARED)
2598 assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED;
2599
2600 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
2601 assoc.plen = IWI_ASSOC_SHPREAMBLE;
2602
2603 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL)
2604 assoc.policy |= htole16(IWI_POLICY_WME);
2605 if (ic->ic_flags & IEEE80211_F_WPA)
2606 assoc.policy |= htole16(IWI_POLICY_WPA);
2607 if (ic->ic_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0)
2608 assoc.type = IWI_HC_IBSS_START;
2609 else
2610 assoc.type = IWI_HC_ASSOC;
2611 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8);
2612
2613 if (ic->ic_opmode == IEEE80211_M_IBSS)
2614 capinfo = IEEE80211_CAPINFO_IBSS;
2615 else
2616 capinfo = IEEE80211_CAPINFO_ESS;
2617 if (ic->ic_flags & IEEE80211_F_PRIVACY)
2618 capinfo |= IEEE80211_CAPINFO_PRIVACY;
2619 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
2620 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan))
2621 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
2622 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2623 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
2624 assoc.capinfo = htole16(capinfo);
2625
2626 assoc.lintval = htole16(ic->ic_lintval);
2627 assoc.intval = htole16(ni->ni_intval);
2628 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid);
2629 if (ic->ic_opmode == IEEE80211_M_IBSS)
2630 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr);
2631 else
2632 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid);
2633
2634 DPRINTF(("%s bssid %s dst %s channel %u policy 0x%x "
2635 "auth %u capinfo 0x%x lintval %u bintval %u\n",
2636 assoc.type == IWI_HC_IBSS_START ? "Start" : "Join",
2637 ether_sprintf(assoc.bssid), ether_sprintf(assoc.dst),
2638 assoc.chan, le16toh(assoc.policy), assoc.auth,
2639 le16toh(assoc.capinfo), le16toh(assoc.lintval),
2640 le16toh(assoc.intval)));
2641
2642 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1);
2643 }
2644
2645 static int
2646 iwi_init(struct ifnet *ifp)
2647 {
2648 struct iwi_softc *sc = ifp->if_softc;
2649 struct ieee80211com *ic = &sc->sc_ic;
2650 struct iwi_firmware *fw = &sc->fw;
2651 int i, error;
2652
2653 /* exit immediately if firmware has not been ioctl'd */
2654 if (!(sc->flags & IWI_FLAG_FW_CACHED)) {
2655 if ((error = iwi_cache_firmware(sc)) != 0) {
2656 aprint_error_dev(sc->sc_dev,
2657 "could not cache the firmware\n");
2658 goto fail;
2659 }
2660 }
2661
2662 iwi_stop(ifp, 0);
2663
2664 if ((error = iwi_reset(sc)) != 0) {
2665 aprint_error_dev(sc->sc_dev, "could not reset adapter\n");
2666 goto fail;
2667 }
2668
2669 if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) {
2670 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n");
2671 goto fail;
2672 }
2673
2674 if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) {
2675 aprint_error_dev(sc->sc_dev, "could not load microcode\n");
2676 goto fail;
2677 }
2678
2679 iwi_stop_master(sc);
2680
2681 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr);
2682 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count);
2683 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur);
2684
2685 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].desc_map->dm_segs[0].ds_addr);
2686 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count);
2687 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur);
2688
2689 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].desc_map->dm_segs[0].ds_addr);
2690 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count);
2691 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur);
2692
2693 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].desc_map->dm_segs[0].ds_addr);
2694 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count);
2695 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur);
2696
2697 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].desc_map->dm_segs[0].ds_addr);
2698 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count);
2699 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur);
2700
2701 for (i = 0; i < sc->rxq.count; i++)
2702 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4,
2703 sc->rxq.data[i].map->dm_segs[0].ds_addr);
2704
2705 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1);
2706
2707 if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) {
2708 aprint_error_dev(sc->sc_dev, "could not load main firmware\n");
2709 goto fail;
2710 }
2711
2712 sc->flags |= IWI_FLAG_FW_INITED;
2713
2714 if ((error = iwi_config(sc)) != 0) {
2715 aprint_error_dev(sc->sc_dev, "device configuration failed\n");
2716 goto fail;
2717 }
2718
2719 ic->ic_state = IEEE80211_S_INIT;
2720
2721 ifp->if_flags &= ~IFF_OACTIVE;
2722 ifp->if_flags |= IFF_RUNNING;
2723
2724 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2725 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2726 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2727 } else
2728 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2729
2730 return 0;
2731
2732 fail: ifp->if_flags &= ~IFF_UP;
2733 iwi_stop(ifp, 0);
2734
2735 return error;
2736 }
2737
2738
2739 /*
2740 * Return whether or not the radio is enabled in hardware
2741 * (i.e. the rfkill switch is "off").
2742 */
2743 static int
2744 iwi_getrfkill(struct iwi_softc *sc)
2745 {
2746 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0;
2747 }
2748
2749 static int
2750 iwi_sysctl_radio(SYSCTLFN_ARGS)
2751 {
2752 struct sysctlnode node;
2753 struct iwi_softc *sc;
2754 int val, error;
2755
2756 node = *rnode;
2757 sc = (struct iwi_softc *)node.sysctl_data;
2758
2759 val = !iwi_getrfkill(sc);
2760
2761 node.sysctl_data = &val;
2762 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2763
2764 if (error || newp == NULL)
2765 return error;
2766
2767 return 0;
2768 }
2769
2770 #ifdef IWI_DEBUG
2771 SYSCTL_SETUP(sysctl_iwi, "sysctl iwi(4) subtree setup")
2772 {
2773 int rc;
2774 const struct sysctlnode *rnode;
2775 const struct sysctlnode *cnode;
2776
2777 if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
2778 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
2779 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
2780 goto err;
2781
2782 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
2783 CTLFLAG_PERMANENT, CTLTYPE_NODE, "iwi",
2784 SYSCTL_DESCR("iwi global controls"),
2785 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
2786 goto err;
2787
2788 /* control debugging printfs */
2789 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2790 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
2791 "debug", SYSCTL_DESCR("Enable debugging output"),
2792 NULL, 0, &iwi_debug, 0, CTL_CREATE, CTL_EOL)) != 0)
2793 goto err;
2794
2795 return;
2796 err:
2797 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
2798 }
2799
2800 #endif /* IWI_DEBUG */
2801
2802 /*
2803 * Add sysctl knobs.
2804 */
2805 static void
2806 iwi_sysctlattach(struct iwi_softc *sc)
2807 {
2808 int rc;
2809 const struct sysctlnode *rnode;
2810 const struct sysctlnode *cnode;
2811
2812 struct sysctllog **clog = &sc->sc_sysctllog;
2813
2814 if ((rc = sysctl_createv(clog, 0, NULL, &rnode,
2815 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
2816 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
2817 goto err;
2818
2819 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode,
2820 CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev),
2821 SYSCTL_DESCR("iwi controls and statistics"),
2822 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0)
2823 goto err;
2824
2825 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2826 CTLFLAG_PERMANENT, CTLTYPE_INT, "radio",
2827 SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"),
2828 iwi_sysctl_radio, 0, sc, 0, CTL_CREATE, CTL_EOL)) != 0)
2829 goto err;
2830
2831 sc->dwelltime = 100;
2832 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2833 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
2834 "dwell", SYSCTL_DESCR("channel dwell time (ms) for AP/station scanning"),
2835 NULL, 0, &sc->dwelltime, 0, CTL_CREATE, CTL_EOL)) != 0)
2836 goto err;
2837
2838 sc->bluetooth = 0;
2839 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2840 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
2841 "bluetooth", SYSCTL_DESCR("bluetooth coexistence"),
2842 NULL, 0, &sc->bluetooth, 0, CTL_CREATE, CTL_EOL)) != 0)
2843 goto err;
2844
2845 sc->antenna = IWI_ANTENNA_AUTO;
2846 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode,
2847 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
2848 "antenna", SYSCTL_DESCR("antenna (0=auto)"),
2849 NULL, 0, &sc->antenna, 0, CTL_CREATE, CTL_EOL)) != 0)
2850 goto err;
2851
2852 return;
2853 err:
2854 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
2855 }
2856
2857 static void
2858 iwi_stop(struct ifnet *ifp, int disable)
2859 {
2860 struct iwi_softc *sc = ifp->if_softc;
2861 struct ieee80211com *ic = &sc->sc_ic;
2862
2863 IWI_LED_OFF(sc);
2864
2865 iwi_stop_master(sc);
2866 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET);
2867
2868 /* reset rings */
2869 iwi_reset_cmd_ring(sc, &sc->cmdq);
2870 iwi_reset_tx_ring(sc, &sc->txq[0]);
2871 iwi_reset_tx_ring(sc, &sc->txq[1]);
2872 iwi_reset_tx_ring(sc, &sc->txq[2]);
2873 iwi_reset_tx_ring(sc, &sc->txq[3]);
2874 iwi_reset_rx_ring(sc, &sc->rxq);
2875
2876 ifp->if_timer = 0;
2877 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2878
2879 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2880 }
2881
2882 static void
2883 iwi_led_set(struct iwi_softc *sc, uint32_t state, int toggle)
2884 {
2885 uint32_t val;
2886
2887 val = MEM_READ_4(sc, IWI_MEM_EVENT_CTL);
2888
2889 switch (sc->nictype) {
2890 case 1:
2891 /* special NIC type: reversed leds */
2892 if (state == IWI_LED_ACTIVITY) {
2893 state &= ~IWI_LED_ACTIVITY;
2894 state |= IWI_LED_ASSOCIATED;
2895 } else if (state == IWI_LED_ASSOCIATED) {
2896 state &= ~IWI_LED_ASSOCIATED;
2897 state |= IWI_LED_ACTIVITY;
2898 }
2899 /* and ignore toggle effect */
2900 val |= state;
2901 break;
2902 case 0:
2903 case 2:
2904 case 3:
2905 case 4:
2906 val = (toggle && (val & state)) ? val & ~state : val | state;
2907 break;
2908 default:
2909 aprint_normal_dev(sc->sc_dev, "unknown NIC type %d\n",
2910 sc->nictype);
2911 return;
2912 break;
2913 }
2914
2915 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, val);
2916
2917 return;
2918 }
2919