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