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