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