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