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