if_wpi.c revision 1.5 1 /* $NetBSD: if_wpi.c,v 1.5 2006/10/31 21:53:41 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2006
5 * Damien Bergamini <damien.bergamini (at) free.fr>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <sys/cdefs.h>
21 __KERNEL_RCSID(0, "$NetBSD: if_wpi.c,v 1.5 2006/10/31 21:53:41 joerg Exp $");
22
23 /*
24 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters.
25 */
26
27 #include "bpfilter.h"
28
29 #include <sys/param.h>
30 #include <sys/sockio.h>
31 #include <sys/sysctl.h>
32 #include <sys/mbuf.h>
33 #include <sys/kernel.h>
34 #include <sys/socket.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/conf.h>
38 #include <sys/kauth.h>
39
40 #include <machine/bus.h>
41 #include <machine/endian.h>
42 #include <machine/intr.h>
43
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcidevs.h>
47
48 #if NBPFILTER > 0
49 #include <net/bpf.h>
50 #endif
51 #include <net/if.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_ether.h>
55 #include <net/if_media.h>
56 #include <net/if_types.h>
57
58 #include <net80211/ieee80211_var.h>
59 #include <net80211/ieee80211_amrr.h>
60 #include <net80211/ieee80211_radiotap.h>
61
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66
67 #include <dev/firmload.h>
68
69 #include <dev/pci/if_wpireg.h>
70 #include <dev/pci/if_wpivar.h>
71
72 #ifdef WPI_DEBUG
73 #define DPRINTF(x) if (wpi_debug > 0) printf x
74 #define DPRINTFN(n, x) if (wpi_debug >= (n)) printf x
75 int wpi_debug = 1;
76 #else
77 #define DPRINTF(x)
78 #define DPRINTFN(n, x)
79 #endif
80
81 /*
82 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
83 */
84 static const struct ieee80211_rateset wpi_rateset_11a =
85 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
86
87 static const struct ieee80211_rateset wpi_rateset_11b =
88 { 4, { 2, 4, 11, 22 } };
89
90 static const struct ieee80211_rateset wpi_rateset_11g =
91 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
92
93 static const uint8_t wpi_ridx_to_plcp[] = {
94 0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3, /* OFDM R1-R4 */
95 10, 20, 55, 110 /* CCK */
96 };
97
98 static int wpi_match(struct device *, struct cfdata *, void *);
99 static void wpi_attach(struct device *, struct device *, void *);
100 static int wpi_detach(struct device*, int);
101 static void wpi_power(int, void *);
102 static int wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *,
103 void **, bus_size_t, bus_size_t, int);
104 static void wpi_dma_contig_free(struct wpi_softc *, struct wpi_dma_info *);
105 static int wpi_alloc_shared(struct wpi_softc *);
106 static void wpi_free_shared(struct wpi_softc *);
107 static int wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
108 static void wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
109 static void wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *);
110 static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, int,
111 int);
112 static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
113 static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *);
114 static struct ieee80211_node * wpi_node_alloc(struct ieee80211_node_table *);
115 static int wpi_media_change(struct ifnet *);
116 static int wpi_newstate(struct ieee80211com *, enum ieee80211_state, int);
117 static void wpi_mem_lock(struct wpi_softc *);
118 static void wpi_mem_unlock(struct wpi_softc *);
119 static uint32_t wpi_mem_read(struct wpi_softc *, uint16_t);
120 static void wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t);
121 static void wpi_mem_write_region_4(struct wpi_softc *, uint16_t,
122 const uint32_t *, int);
123 static uint16_t wpi_read_prom_word(struct wpi_softc *, uint32_t);
124 static int wpi_load_firmware(struct wpi_softc *, uint32_t, const char *,
125 int);
126 static void wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *,
127 struct wpi_rx_data *);
128 static void wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *);
129 static void wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *);
130 static void wpi_notif_intr(struct wpi_softc *);
131 static int wpi_intr(void *);
132 static uint8_t wpi_plcp_signal(int);
133 static int wpi_tx_data(struct wpi_softc *, struct mbuf *,
134 struct ieee80211_node *, int);
135 static void wpi_start(struct ifnet *);
136 static void wpi_watchdog(struct ifnet *);
137 static int wpi_ioctl(struct ifnet *, u_long, caddr_t);
138 static void wpi_read_eeprom(struct wpi_softc *);
139 static int wpi_cmd(struct wpi_softc *, int, const void *, int, int);
140 static int wpi_wme_update(struct ieee80211com *);
141 static int wpi_mrr_setup(struct wpi_softc *);
142 static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t);
143 static void wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *);
144 static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *);
145 static int wpi_auth(struct wpi_softc *);
146 static int wpi_scan(struct wpi_softc *, uint16_t);
147 static int wpi_config(struct wpi_softc *);
148 static void wpi_stop_master(struct wpi_softc *);
149 static int wpi_power_up(struct wpi_softc *);
150 static int wpi_reset(struct wpi_softc *);
151 static void wpi_hw_config(struct wpi_softc *);
152 static int wpi_init(struct ifnet *);
153 static void wpi_stop(struct ifnet *, int);
154
155 /* rate control algorithm: should be moved to net80211 */
156 static void wpi_iter_func(void *, struct ieee80211_node *);
157 static void wpi_amrr_timeout(void *);
158 static void wpi_newassoc(struct ieee80211_node *,
159 int);
160
161 CFATTACH_DECL(wpi, sizeof (struct wpi_softc), wpi_match, wpi_attach,
162 wpi_detach, NULL);
163
164 static int
165 wpi_match(struct device *parent __unused, struct cfdata *match __unused,
166 void *aux)
167 {
168 struct pci_attach_args *pa = aux;
169
170 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
171 return 0;
172
173 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_1 ||
174 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_3945ABG_2)
175 return 1;
176
177 return 0;
178 }
179
180 /* Base Address Register */
181 #define WPI_PCI_BAR0 0x10
182
183 static void
184 wpi_attach(struct device *parent __unused, struct device *self, void *aux)
185 {
186 struct wpi_softc *sc = (struct wpi_softc *)self;
187 struct ieee80211com *ic = &sc->sc_ic;
188 struct ifnet *ifp = &sc->sc_ec.ec_if;
189 struct pci_attach_args *pa = aux;
190 const char *intrstr;
191 char devinfo[256];
192 bus_space_tag_t memt;
193 bus_space_handle_t memh;
194 bus_addr_t base;
195 pci_intr_handle_t ih;
196 pcireg_t data;
197 int error, ac, revision, i;
198
199 sc->sc_pct = pa->pa_pc;
200 sc->sc_pcitag = pa->pa_tag;
201
202 callout_init(&sc->amrr_ch);
203
204 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo);
205 revision = PCI_REVISION(pa->pa_class);
206 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
207
208 /* clear device specific PCI configuration register 0x41 */
209 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
210 data &= ~0x0000ff00;
211 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
212
213 /* enable bus-mastering */
214 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
215 data |= PCI_COMMAND_MASTER_ENABLE;
216 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data);
217
218 /* map the register window */
219 error = pci_mapreg_map(pa, WPI_PCI_BAR0, PCI_MAPREG_TYPE_MEM |
220 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz);
221 if (error != 0) {
222 aprint_error("%s: could not map memory space\n",
223 sc->sc_dev.dv_xname);
224 return;
225 }
226
227 sc->sc_st = memt;
228 sc->sc_sh = memh;
229 sc->sc_dmat = pa->pa_dmat;
230
231 if (pci_intr_map(pa, &ih) != 0) {
232 aprint_error("%s: could not map interrupt\n",
233 sc->sc_dev.dv_xname);
234 return;
235 }
236
237 intrstr = pci_intr_string(sc->sc_pct, ih);
238 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, wpi_intr, sc);
239 if (sc->sc_ih == NULL) {
240 aprint_error("%s: could not establish interrupt",
241 sc->sc_dev.dv_xname);
242 if (intrstr != NULL)
243 aprint_error(" at %s", intrstr);
244 aprint_error("\n");
245 return;
246 }
247 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
248
249 if (wpi_reset(sc) != 0) {
250 aprint_error("%s: could not reset adapter\n",
251 sc->sc_dev.dv_xname);
252 return;
253 }
254
255 /*
256 * Allocate shared page and Tx/Rx rings.
257 */
258 if ((error = wpi_alloc_shared(sc)) != 0) {
259 aprint_error("%s: could not allocate shared area\n",
260 sc->sc_dev.dv_xname);
261 return;
262 }
263
264 for (ac = 0; ac < 4; ac++) {
265 error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac);
266 if (error != 0) {
267 aprint_error("%s: could not allocate Tx ring %d\n",
268 sc->sc_dev.dv_xname, ac);
269 goto fail1;
270 }
271 }
272
273 error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4);
274 if (error != 0) {
275 aprint_error("%s: could not allocate command ring\n",
276 sc->sc_dev.dv_xname);
277 goto fail1;
278 }
279
280 error = wpi_alloc_tx_ring(sc, &sc->svcq, WPI_SVC_RING_COUNT, 5);
281 if (error != 0) {
282 aprint_error("%s: could not allocate service ring\n",
283 sc->sc_dev.dv_xname);
284 goto fail2;
285 }
286
287 if (wpi_alloc_rx_ring(sc, &sc->rxq) != 0) {
288 aprint_error("%s: could not allocate Rx ring\n",
289 sc->sc_dev.dv_xname);
290 goto fail3;
291 }
292
293
294 ic->ic_ifp = ifp;
295 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
296 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
297 ic->ic_state = IEEE80211_S_INIT;
298
299 /* set device capabilities */
300 ic->ic_caps =
301 IEEE80211_C_IBSS | /* IBSS mode support */
302 IEEE80211_C_WPA | /* 802.11i */
303 IEEE80211_C_MONITOR | /* monitor mode supported */
304 IEEE80211_C_TXPMGT | /* tx power management */
305 IEEE80211_C_SHSLOT | /* short slot time supported */
306 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
307 IEEE80211_C_WME; /* 802.11e */
308
309 wpi_read_eeprom(sc);
310 aprint_normal("%s: 802.11 address %s\n", sc->sc_dev.dv_xname,
311 ether_sprintf(ic->ic_myaddr));
312
313 /* set supported .11a rates */
314 ic->ic_sup_rates[IEEE80211_MODE_11A] = wpi_rateset_11a;
315
316 /* set supported .11a channels */
317 for (i = 36; i <= 64; i += 4) {
318 ic->ic_channels[i].ic_freq =
319 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
320 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
321 }
322 for (i = 100; i <= 140; i += 4) {
323 ic->ic_channels[i].ic_freq =
324 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
325 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
326 }
327 for (i = 149; i <= 165; i += 4) {
328 ic->ic_channels[i].ic_freq =
329 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
330 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
331 }
332
333 /* set supported .11b and .11g rates */
334 ic->ic_sup_rates[IEEE80211_MODE_11B] = wpi_rateset_11b;
335 ic->ic_sup_rates[IEEE80211_MODE_11G] = wpi_rateset_11g;
336
337 /* set supported .11b and .11g channels (1 through 14) */
338 for (i = 1; i <= 14; i++) {
339 ic->ic_channels[i].ic_freq =
340 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
341 ic->ic_channels[i].ic_flags =
342 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
343 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
344 }
345
346 ic->ic_ibss_chan = &ic->ic_channels[0];
347
348 ifp->if_softc = sc;
349 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
350 ifp->if_init = wpi_init;
351 ifp->if_stop = wpi_stop;
352 ifp->if_ioctl = wpi_ioctl;
353 ifp->if_start = wpi_start;
354 ifp->if_watchdog = wpi_watchdog;
355 IFQ_SET_READY(&ifp->if_snd);
356 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
357
358 if_attach(ifp);
359 ieee80211_ifattach(ic);
360 /* override default methods */
361 ic->ic_node_alloc = wpi_node_alloc;
362 ic->ic_newassoc = wpi_newassoc;
363 ic->ic_wme.wme_update = wpi_wme_update;
364
365 /* override state transition machine */
366 sc->sc_newstate = ic->ic_newstate;
367 ic->ic_newstate = wpi_newstate;
368 ieee80211_media_init(ic, wpi_media_change, ieee80211_media_status);
369
370 sc->amrr.amrr_min_success_threshold = 1;
371 sc->amrr.amrr_max_success_threshold = 15;
372
373 /* set powerhook */
374 sc->powerhook = powerhook_establish(sc->sc_dev.dv_xname, wpi_power, sc);
375
376 #if NBPFILTER > 0
377 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
378 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
379 &sc->sc_drvbpf);
380
381 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
382 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
383 sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT);
384
385 sc->sc_txtap_len = sizeof sc->sc_txtapu;
386 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
387 sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT);
388 #endif
389
390 ieee80211_announce(ic);
391
392 return;
393
394 fail3: wpi_free_tx_ring(sc, &sc->svcq);
395 fail2: wpi_free_tx_ring(sc, &sc->cmdq);
396 fail1: while (--ac >= 0)
397 wpi_free_tx_ring(sc, &sc->txq[ac]);
398 wpi_free_shared(sc);
399 }
400
401 static int
402 wpi_detach(struct device* self, int flags __unused)
403 {
404 struct wpi_softc *sc = (struct wpi_softc *)self;
405 struct ifnet *ifp = &sc->sc_ec.ec_if;
406 int ac;
407
408 wpi_stop(ifp, 1);
409
410 #if NBPFILTER > 0
411 if (ifp != NULL)
412 bpfdetach(ifp);
413 #endif
414 ieee80211_ifdetach(&sc->sc_ic);
415 if (ifp != NULL)
416 if_detach(ifp);
417
418 for (ac = 0; ac < 4; ac++)
419 wpi_free_tx_ring(sc, &sc->txq[ac]);
420 wpi_free_tx_ring(sc, &sc->cmdq);
421 wpi_free_tx_ring(sc, &sc->svcq);
422 wpi_free_rx_ring(sc, &sc->rxq);
423 wpi_free_shared(sc);
424
425 if (sc->sc_ih != NULL) {
426 pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
427 sc->sc_ih = NULL;
428 }
429
430 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz);
431
432 return 0;
433 }
434
435 static void
436 wpi_power(int why, void *arg)
437 {
438 struct wpi_softc *sc = arg;
439 struct ifnet *ifp;
440 pcireg_t data;
441 int s;
442
443 if (why != PWR_RESUME)
444 return;
445
446 /* clear device specific PCI configuration register 0x41 */
447 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40);
448 data &= ~0x0000ff00;
449 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data);
450
451 s = splnet();
452 ifp = sc->sc_ic.ic_ifp;
453 if (ifp->if_flags & IFF_UP) {
454 ifp->if_init(ifp);
455 if (ifp->if_flags & IFF_RUNNING)
456 ifp->if_start(ifp);
457 }
458 splx(s);
459 }
460
461 static int
462 wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma,
463 void **kvap, bus_size_t size, bus_size_t alignment, int flags)
464 {
465 int nsegs, error;
466
467 dma->size = size;
468
469 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
470 flags, &dma->map);
471 if (error != 0) {
472 aprint_error("%s: could not create DMA map\n",
473 sc->sc_dev.dv_xname);
474 goto fail;
475 }
476
477 error = bus_dmamem_alloc(sc->sc_dmat, size, alignment, 0, &dma->seg,
478 1, &nsegs, flags);
479 if (error != 0) {
480 aprint_error("%s: could not allocate DMA memory\n",
481 sc->sc_dev.dv_xname);
482 goto fail;
483 }
484
485 error = bus_dmamem_map(sc->sc_dmat, &dma->seg, 1, size,
486 &dma->vaddr, flags);
487 if (error != 0) {
488 aprint_error("%s: could not map DMA memory\n",
489 sc->sc_dev.dv_xname);
490 goto fail;
491 }
492
493 error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->vaddr,
494 size, NULL, flags);
495 if (error != 0) {
496 aprint_error("%s: could not load DMA memory\n",
497 sc->sc_dev.dv_xname);
498 goto fail;
499 }
500
501 memset(dma->vaddr, 0, size);
502
503 dma->paddr = dma->map->dm_segs[0].ds_addr;
504 *kvap = dma->vaddr;
505
506 return 0;
507
508 fail: wpi_dma_contig_free(sc, dma);
509 return error;
510 }
511
512 static void
513 wpi_dma_contig_free(struct wpi_softc *sc, struct wpi_dma_info *dma)
514 {
515 if (dma->map != NULL) {
516 if (dma->vaddr != NULL) {
517 bus_dmamap_unload(sc->sc_dmat, dma->map);
518 bus_dmamem_unmap(sc->sc_dmat, dma->vaddr, dma->size);
519 bus_dmamem_free(sc->sc_dmat, &dma->seg, 1);
520 dma->vaddr = NULL;
521 }
522 bus_dmamap_destroy(sc->sc_dmat, dma->map);
523 dma->map = NULL;
524 }
525 }
526
527 /*
528 * Allocate a shared page between host and NIC.
529 */
530 static int
531 wpi_alloc_shared(struct wpi_softc *sc)
532 {
533 int error;
534 /* must be aligned on a 4K-page boundary */
535 error = wpi_dma_contig_alloc(sc, &sc->shared_dma,
536 (void **)&sc->shared, sizeof (struct wpi_shared), PAGE_SIZE,
537 BUS_DMA_NOWAIT);
538 if (error != 0)
539 aprint_error("%s: could not allocate shared area DMA memory\n",
540 sc->sc_dev.dv_xname);
541
542 return error;
543 }
544
545 static void
546 wpi_free_shared(struct wpi_softc *sc)
547 {
548 wpi_dma_contig_free(sc, &sc->shared_dma);
549 }
550
551 static int
552 wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
553 {
554 struct wpi_rx_data *data;
555 int i, error;
556
557 ring->cur = 0;
558
559 error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
560 (void **)&ring->desc,
561 WPI_RX_RING_COUNT * sizeof (struct wpi_rx_desc),
562 WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
563 if (error != 0) {
564 aprint_error("%s: could not allocate rx ring DMA memory\n",
565 sc->sc_dev.dv_xname);
566 goto fail;
567 }
568
569 /*
570 * Allocate Rx buffers.
571 */
572 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
573 data = &ring->data[i];
574
575 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
576 0, BUS_DMA_NOWAIT, &data->map);
577 if (error != 0) {
578 aprint_error("%s: could not create rx buf DMA map\n",
579 sc->sc_dev.dv_xname);
580 goto fail;
581 }
582
583 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
584 if (data->m == NULL) {
585 aprint_error("%s: could not allocate rx mbuf\n",
586 sc->sc_dev.dv_xname);
587 error = ENOMEM;
588 goto fail;
589 }
590
591 MCLGET(data->m, M_DONTWAIT);
592 if (!(data->m->m_flags & M_EXT)) {
593 m_freem(data->m);
594 data->m = NULL;
595 aprint_error("%s: could not allocate rx mbuf cluster\n",
596 sc->sc_dev.dv_xname);
597 error = ENOMEM;
598 goto fail;
599 }
600
601 error = bus_dmamap_load(sc->sc_dmat, data->map,
602 mtod(data->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT |
603 BUS_DMA_READ);
604 if (error != 0) {
605 aprint_error("%s: could not load rx buf DMA map\n",
606 sc->sc_dev.dv_xname);
607 goto fail;
608 }
609
610 ring->desc[i] = htole32(data->map->dm_segs[0].ds_addr);
611 }
612
613 return 0;
614
615 fail: wpi_free_rx_ring(sc, ring);
616 return error;
617 }
618
619 static void
620 wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
621 {
622 int ntries;
623
624 wpi_mem_lock(sc);
625
626 WPI_WRITE(sc, WPI_RX_CONFIG, 0);
627 for (ntries = 0; ntries < 100; ntries++) {
628 if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE)
629 break;
630 DELAY(10);
631 }
632 #ifdef WPI_DEBUG
633 if (ntries == 100 && wpi_debug > 0)
634 aprint_error("%s: timeout resetting Rx ring\n",
635 sc->sc_dev.dv_xname);
636 #endif
637 wpi_mem_unlock(sc);
638
639 ring->cur = 0;
640 }
641
642 static void
643 wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring)
644 {
645 struct wpi_rx_data *data;
646 int i;
647
648 wpi_dma_contig_free(sc, &ring->desc_dma);
649
650 for (i = 0; i < WPI_RX_RING_COUNT; i++) {
651 data = &ring->data[i];
652
653 if (data->m != NULL) {
654 bus_dmamap_unload(sc->sc_dmat, data->map);
655 m_freem(data->m);
656 }
657 bus_dmamap_destroy(sc->sc_dmat, data->map);
658 }
659 }
660
661 static int
662 wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count,
663 int qid)
664 {
665 struct wpi_tx_data *data;
666 int i, error;
667
668 ring->qid = qid;
669 ring->count = count;
670 ring->queued = 0;
671 ring->cur = 0;
672
673 error = wpi_dma_contig_alloc(sc, &ring->desc_dma,
674 (void **)&ring->desc, count * sizeof (struct wpi_tx_desc),
675 WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT);
676 if (error != 0) {
677 aprint_error("%s: could not allocate tx ring DMA memory\n",
678 sc->sc_dev.dv_xname);
679 goto fail;
680 }
681
682 /* update shared page with ring's base address */
683 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr);
684
685 error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd,
686 count * sizeof (struct wpi_tx_cmd), 4, BUS_DMA_NOWAIT);
687 if (error != 0) {
688 aprint_error("%s: could not allocate tx cmd DMA memory\n",
689 sc->sc_dev.dv_xname);
690 goto fail;
691 }
692
693 ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF,
694 M_NOWAIT);
695 if (ring->data == NULL) {
696 aprint_error("%s: could not allocate tx data slots\n",
697 sc->sc_dev.dv_xname);
698 goto fail;
699 }
700
701 memset(ring->data, 0, count * sizeof (struct wpi_tx_data));
702
703 for (i = 0; i < count; i++) {
704 data = &ring->data[i];
705
706 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
707 WPI_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
708 &data->map);
709 if (error != 0) {
710 aprint_error("%s: could not create tx buf DMA map\n",
711 sc->sc_dev.dv_xname);
712 goto fail;
713 }
714 }
715
716 return 0;
717
718 fail: wpi_free_tx_ring(sc, ring);
719 return error;
720 }
721
722 static void
723 wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
724 {
725 struct wpi_tx_data *data;
726 int i, ntries;
727
728 wpi_mem_lock(sc);
729
730 WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0);
731 for (ntries = 0; ntries < 100; ntries++) {
732 if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid))
733 break;
734 DELAY(10);
735 }
736 #ifdef WPI_DEBUG
737 if (ntries == 100 && wpi_debug > 0) {
738 aprint_error("%s: timeout resetting Tx ring %d\n",
739 sc->sc_dev.dv_xname, ring->qid);
740 }
741 #endif
742 wpi_mem_unlock(sc);
743
744 for (i = 0; i < ring->count; i++) {
745 data = &ring->data[i];
746
747 if (data->m != NULL) {
748 bus_dmamap_unload(sc->sc_dmat, data->map);
749 m_freem(data->m);
750 data->m = NULL;
751 }
752 }
753
754 ring->queued = 0;
755 ring->cur = 0;
756 }
757
758 static void
759 wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring)
760 {
761 struct wpi_tx_data *data;
762 int i;
763
764 wpi_dma_contig_free(sc, &ring->desc_dma);
765 wpi_dma_contig_free(sc, &ring->cmd_dma);
766
767 if (ring->data != NULL) {
768 for (i = 0; i < ring->count; i++) {
769 data = &ring->data[i];
770
771 if (data->m != NULL) {
772 bus_dmamap_unload(sc->sc_dmat, data->map);
773 m_freem(data->m);
774 }
775 }
776 free(ring->data, M_DEVBUF);
777 }
778 }
779
780 /*ARGUSED*/
781 static struct ieee80211_node *
782 wpi_node_alloc(struct ieee80211_node_table *ic __unused)
783 {
784 struct wpi_node *wn;
785
786 wn = malloc(sizeof (struct wpi_node), M_DEVBUF, M_NOWAIT);
787
788 if (wn != NULL)
789 memset(wn, 0, sizeof (struct wpi_node));
790 return (struct ieee80211_node *)wn;
791 }
792
793 static int
794 wpi_media_change(struct ifnet *ifp)
795 {
796 int error;
797
798 error = ieee80211_media_change(ifp);
799 if (error != ENETRESET)
800 return error;
801
802 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
803 wpi_init(ifp);
804
805 return 0;
806 }
807
808 static int
809 wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
810 {
811 struct ifnet *ifp = ic->ic_ifp;
812 struct wpi_softc *sc = ifp->if_softc;
813 int error;
814
815 callout_stop(&sc->amrr_ch);
816
817 switch (nstate) {
818 case IEEE80211_S_SCAN:
819 ieee80211_node_table_reset(&ic->ic_scan);
820 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN;
821
822 /* make the link LED blink while we're scanning */
823 wpi_set_led(sc, WPI_LED_LINK, 20, 2);
824
825 if ((error = wpi_scan(sc, IEEE80211_CHAN_G)) != 0) {
826 aprint_error("%s: could not initiate scan\n",
827 sc->sc_dev.dv_xname);
828 ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN);
829 return error;
830 }
831
832 ic->ic_state = nstate;
833 return 0;
834
835 case IEEE80211_S_AUTH:
836 sc->config.state &= ~htole16(WPI_STATE_ASSOCIATED);
837 sc->config.filter &= ~htole32(WPI_FILTER_BSS);
838 if ((error = wpi_auth(sc)) != 0) {
839 aprint_error("%s: could not send authentication request\n",
840 sc->sc_dev.dv_xname);
841 return error;
842 }
843 break;
844
845 case IEEE80211_S_RUN:
846 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
847 /* link LED blinks while monitoring */
848 wpi_set_led(sc, WPI_LED_LINK, 5, 5);
849 break;
850 }
851
852 if (ic->ic_opmode != IEEE80211_M_STA) {
853 (void) wpi_auth(sc); /* XXX */
854 wpi_setup_beacon(sc, ic->ic_bss);
855 }
856
857 wpi_enable_tsf(sc, ic->ic_bss);
858
859 /* update adapter's configuration */
860 sc->config.state = htole16(WPI_STATE_ASSOCIATED);
861 /* short preamble/slot time are negotiated when associating */
862 sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE |
863 WPI_CONFIG_SHSLOT);
864 if (ic->ic_flags & IEEE80211_F_SHSLOT)
865 sc->config.flags |= htole32(WPI_CONFIG_SHSLOT);
866 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
867 sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE);
868 sc->config.filter |= htole32(WPI_FILTER_BSS);
869 if (ic->ic_opmode != IEEE80211_M_STA)
870 sc->config.filter |= htole32(WPI_FILTER_BEACON);
871
872 /* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */
873
874 DPRINTF(("config chan %d flags %x\n", sc->config.chan,
875 sc->config.flags));
876 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
877 sizeof (struct wpi_config), 1);
878 if (error != 0) {
879 aprint_error("%s: could not update configuration\n",
880 sc->sc_dev.dv_xname);
881 return error;
882 }
883
884 if (ic->ic_opmode == IEEE80211_M_STA) {
885 /* fake a join to init the tx rate */
886 wpi_newassoc(ic->ic_bss, 1);
887 }
888
889 /* enable automatic rate adaptation in STA mode */
890 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
891 callout_reset(&sc->amrr_ch, hz, wpi_amrr_timeout, sc);
892
893 /* link LED always on while associated */
894 wpi_set_led(sc, WPI_LED_LINK, 0, 1);
895 break;
896
897 case IEEE80211_S_ASSOC:
898 case IEEE80211_S_INIT:
899 break;
900 }
901
902 return sc->sc_newstate(ic, nstate, arg);
903 }
904
905 /*
906 * Grab exclusive access to NIC memory.
907 */
908 static void
909 wpi_mem_lock(struct wpi_softc *sc)
910 {
911 uint32_t tmp;
912 int ntries;
913
914 tmp = WPI_READ(sc, WPI_GPIO_CTL);
915 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC);
916
917 /* spin until we actually get the lock */
918 for (ntries = 0; ntries < 1000; ntries++) {
919 if ((WPI_READ(sc, WPI_GPIO_CTL) &
920 (WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK)
921 break;
922 DELAY(10);
923 }
924 if (ntries == 1000)
925 aprint_error("%s: could not lock memory\n", sc->sc_dev.dv_xname);
926 }
927
928 /*
929 * Release lock on NIC memory.
930 */
931 static void
932 wpi_mem_unlock(struct wpi_softc *sc)
933 {
934 uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL);
935 WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC);
936 }
937
938 static uint32_t
939 wpi_mem_read(struct wpi_softc *sc, uint16_t addr)
940 {
941 WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr);
942 return WPI_READ(sc, WPI_READ_MEM_DATA);
943 }
944
945 static void
946 wpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data)
947 {
948 WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr);
949 WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data);
950 }
951
952 static void
953 wpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr,
954 const uint32_t *data, int wlen)
955 {
956 for (; wlen > 0; wlen--, data++, addr += 4)
957 wpi_mem_write(sc, addr, *data);
958 }
959
960 /*
961 * Read 16 bits from the EEPROM. We access EEPROM through the MAC instead of
962 * using the traditional bit-bang method.
963 */
964 static uint16_t
965 wpi_read_prom_word(struct wpi_softc *sc, uint32_t addr)
966 {
967 int ntries;
968 uint32_t val;
969
970 WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2);
971
972 wpi_mem_lock(sc);
973 for (ntries = 0; ntries < 10; ntries++) {
974 if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & WPI_EEPROM_READY)
975 break;
976 DELAY(10);
977 }
978 wpi_mem_unlock(sc);
979
980 if (ntries == 10) {
981 aprint_error("%s: could not read EEPROM\n", sc->sc_dev.dv_xname);
982 return 0xdead;
983 }
984 return val >> 16;
985 }
986
987 /*
988 * The firmware boot code is small and is intended to be copied directly into
989 * the NIC internal memory.
990 */
991 static int
992 wpi_load_microcode(struct wpi_softc *sc, const char *ucode, int size)
993 {
994 /* check that microcode size is a multiple of 4 */
995 if (size & 3)
996 return EINVAL;
997
998 size /= sizeof (uint32_t);
999
1000 wpi_mem_lock(sc);
1001
1002 /* copy microcode image into NIC memory */
1003 wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE, (const uint32_t *)ucode,
1004 size);
1005
1006 wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0);
1007 wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT);
1008 wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size);
1009
1010 /* run microcode */
1011 wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN);
1012
1013 wpi_mem_unlock(sc);
1014
1015 return 0;
1016 }
1017
1018 /*
1019 * The firmware text and data segments are transferred to the NIC using DMA.
1020 * The driver just copies the firmware into DMA-safe memory and tells the NIC
1021 * where to find it. Once the NIC has copied the firmware into its internal
1022 * memory, we can free our local copy in the driver.
1023 */
1024 static int
1025 wpi_load_firmware(struct wpi_softc *sc, uint32_t target, const char *fw,
1026 int size)
1027 {
1028 bus_dmamap_t map;
1029 bus_dma_segment_t seg;
1030 caddr_t virtaddr;
1031 struct wpi_tx_desc desc;
1032 int i, ntries, nsegs, error;
1033
1034 /*
1035 * Allocate DMA-safe memory to store the firmware.
1036 */
1037 error = bus_dmamap_create(sc->sc_dmat, size, WPI_MAX_SCATTER,
1038 WPI_MAX_SEG_LEN, 0, BUS_DMA_NOWAIT, &map);
1039 if (error != 0) {
1040 aprint_error("%s: could not create firmware DMA map\n",
1041 sc->sc_dev.dv_xname);
1042 goto fail1;
1043 }
1044
1045 error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
1046 &nsegs, BUS_DMA_NOWAIT);
1047 if (error != 0) {
1048 aprint_error("%s: could not allocate firmware DMA memory\n",
1049 sc->sc_dev.dv_xname);
1050 goto fail2;
1051 }
1052
1053 error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, &virtaddr,
1054 BUS_DMA_NOWAIT);
1055 if (error != 0) {
1056 aprint_error("%s: could not map firmware DMA memory\n",
1057 sc->sc_dev.dv_xname);
1058 goto fail3;
1059 }
1060
1061 error = bus_dmamap_load(sc->sc_dmat, map, virtaddr, size, NULL,
1062 BUS_DMA_NOWAIT | BUS_DMA_WRITE);
1063 if (error != 0) {
1064 aprint_error("%s: could not load firmware DMA map\n",
1065 sc->sc_dev.dv_xname);
1066 goto fail4;
1067 }
1068
1069 /* copy firmware image to DMA-safe memory */
1070 bcopy(fw, virtaddr, size);
1071
1072 /* make sure the adapter will get up-to-date values */
1073 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE);
1074
1075 bzero(&desc, sizeof desc);
1076 desc.flags = htole32(WPI_PAD32(size) << 28 | map->dm_nsegs << 24);
1077 for (i = 0; i < map->dm_nsegs; i++) {
1078 desc.segs[i].addr = htole32(map->dm_segs[i].ds_addr);
1079 desc.segs[i].len = htole32(map->dm_segs[i].ds_len);
1080 }
1081
1082 wpi_mem_lock(sc);
1083
1084 /* tell adapter where to copy image in its internal memory */
1085 WPI_WRITE(sc, WPI_FW_TARGET, target);
1086
1087 WPI_WRITE(sc, WPI_TX_CONFIG(6), 0);
1088
1089 /* copy firmware descriptor into NIC memory */
1090 WPI_WRITE_REGION_4(sc, WPI_TX_DESC(6), (uint32_t *)&desc,
1091 sizeof desc / sizeof (uint32_t));
1092
1093 WPI_WRITE(sc, WPI_TX_CREDIT(6), 0xfffff);
1094 WPI_WRITE(sc, WPI_TX_STATE(6), 0x4001);
1095 WPI_WRITE(sc, WPI_TX_CONFIG(6), 0x80000001);
1096
1097 /* wait while the adapter is busy copying the firmware */
1098 for (ntries = 0; ntries < 100; ntries++) {
1099 if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(6))
1100 break;
1101 DELAY(1000);
1102 }
1103 if (ntries == 100) {
1104 aprint_error("%s: timeout transferring firmware\n",
1105 sc->sc_dev.dv_xname);
1106 error = ETIMEDOUT;
1107 }
1108
1109 WPI_WRITE(sc, WPI_TX_CREDIT(6), 0);
1110
1111 wpi_mem_unlock(sc);
1112
1113 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE);
1114 bus_dmamap_unload(sc->sc_dmat, map);
1115 fail4: bus_dmamem_unmap(sc->sc_dmat, virtaddr, size);
1116 fail3: bus_dmamem_free(sc->sc_dmat, &seg, 1);
1117 fail2: bus_dmamap_destroy(sc->sc_dmat, map);
1118 fail1: return error;
1119 }
1120
1121 static void
1122 wpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc,
1123 struct wpi_rx_data *data)
1124 {
1125 struct ieee80211com *ic = &sc->sc_ic;
1126 struct ifnet *ifp = ic->ic_ifp;
1127 struct wpi_rx_ring *ring = &sc->rxq;
1128 struct wpi_rx_stat *stat;
1129 struct wpi_rx_head *head;
1130 struct wpi_rx_tail *tail;
1131 struct ieee80211_frame *wh;
1132 struct ieee80211_node *ni;
1133 struct mbuf *m, *mnew;
1134 int error;
1135
1136 stat = (struct wpi_rx_stat *)(desc + 1);
1137
1138 if (stat->len > WPI_STAT_MAXLEN) {
1139 aprint_error("%s: invalid rx statistic header\n",
1140 sc->sc_dev.dv_xname);
1141 ifp->if_ierrors++;
1142 return;
1143 }
1144
1145 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len);
1146 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + le16toh(head->len));
1147
1148 DPRINTFN(4, ("rx intr: idx=%d len=%d stat len=%d rssi=%d rate=%x "
1149 "chan=%d tstamp=%llu\n", ring->cur, le32toh(desc->len),
1150 le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan,
1151 le64toh(tail->tstamp)));
1152
1153 /*
1154 * Discard Rx frames with bad CRC early (XXX we may want to pass them
1155 * to radiotap in monitor mode).
1156 */
1157 if ((le32toh(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) {
1158 DPRINTF(("rx tail flags error %x\n", le32toh(tail->flags)));
1159 ifp->if_ierrors++;
1160 return;
1161 }
1162
1163
1164 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1165 if (mnew == NULL) {
1166 ifp->if_ierrors++;
1167 return;
1168 }
1169
1170 MCLGET(mnew, M_DONTWAIT);
1171 if (!(mnew->m_flags & M_EXT)) {
1172 m_freem(mnew);
1173 ifp->if_ierrors++;
1174 return;
1175 }
1176
1177 bus_dmamap_unload(sc->sc_dmat, data->map);
1178
1179 error = bus_dmamap_load(sc->sc_dmat, data->map, mtod(mnew, void *),
1180 MCLBYTES, NULL, BUS_DMA_NOWAIT);
1181 if (error != 0) {
1182 m_freem(mnew);
1183
1184 /* try to reload the old mbuf */
1185 error = bus_dmamap_load(sc->sc_dmat, data->map,
1186 mtod(data->m, void *), MCLBYTES, NULL, BUS_DMA_NOWAIT);
1187 if (error != 0) {
1188 /* very unlikely that it will fail... */
1189 panic("%s: could not load old rx mbuf",
1190 sc->sc_dev.dv_xname);
1191 }
1192 ifp->if_ierrors++;
1193 return;
1194 }
1195
1196 m = data->m;
1197 data->m = mnew;
1198
1199 /* update Rx descriptor */
1200 ring->desc[ring->cur] = htole32(data->map->dm_segs[0].ds_addr);
1201
1202 /* finalize mbuf */
1203 m->m_pkthdr.rcvif = ifp;
1204 m->m_data = (caddr_t)(head + 1);
1205 m->m_pkthdr.len = m->m_len = le16toh(head->len);
1206
1207 #if NBPFILTER > 0
1208 if (sc->sc_drvbpf != NULL) {
1209 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap;
1210
1211 tap->wr_flags = 0;
1212 tap->wr_chan_freq =
1213 htole16(ic->ic_channels[head->chan].ic_freq);
1214 tap->wr_chan_flags =
1215 htole16(ic->ic_channels[head->chan].ic_flags);
1216 tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET);
1217 tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise);
1218 tap->wr_tsft = tail->tstamp;
1219 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf;
1220 switch (head->rate) {
1221 /* CCK rates */
1222 case 10: tap->wr_rate = 2; break;
1223 case 20: tap->wr_rate = 4; break;
1224 case 55: tap->wr_rate = 11; break;
1225 case 110: tap->wr_rate = 22; break;
1226 /* OFDM rates */
1227 case 0xd: tap->wr_rate = 12; break;
1228 case 0xf: tap->wr_rate = 18; break;
1229 case 0x5: tap->wr_rate = 24; break;
1230 case 0x7: tap->wr_rate = 36; break;
1231 case 0x9: tap->wr_rate = 48; break;
1232 case 0xb: tap->wr_rate = 72; break;
1233 case 0x1: tap->wr_rate = 96; break;
1234 case 0x3: tap->wr_rate = 108; break;
1235 /* unknown rate: should not happen */
1236 default: tap->wr_rate = 0;
1237 }
1238 if (le16toh(head->flags) & 0x4)
1239 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1240
1241 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1242 }
1243 #endif
1244
1245 /* grab a reference to the source node */
1246 wh = mtod(m, struct ieee80211_frame *);
1247 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1248
1249 /* send the frame to the 802.11 layer */
1250 ieee80211_input(ic, m, ni, stat->rssi, 0);
1251
1252 /* release node reference */
1253 ieee80211_free_node(ni);
1254 }
1255
1256 static void
1257 wpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1258 {
1259 struct ifnet *ifp = sc->sc_ic.ic_ifp;
1260 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3];
1261 struct wpi_tx_data *txdata = &ring->data[desc->idx];
1262 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1);
1263 struct wpi_node *wn = (struct wpi_node *)txdata->ni;
1264
1265 DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x "
1266 "duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries,
1267 stat->nkill, stat->rate, le32toh(stat->duration),
1268 le32toh(stat->status)));
1269
1270 /*
1271 * Update rate control statistics for the node.
1272 * XXX we should not count mgmt frames since they're always sent at
1273 * the lowest available bit-rate.
1274 */
1275 wn->amn.amn_txcnt++;
1276 if (stat->ntries > 0) {
1277 DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries));
1278 wn->amn.amn_retrycnt++;
1279 }
1280
1281 if ((le32toh(stat->status) & 0xff) != 1)
1282 ifp->if_oerrors++;
1283 else
1284 ifp->if_opackets++;
1285
1286 bus_dmamap_unload(sc->sc_dmat, txdata->map);
1287 m_freem(txdata->m);
1288 txdata->m = NULL;
1289 ieee80211_free_node(txdata->ni);
1290 txdata->ni = NULL;
1291
1292 ring->queued--;
1293
1294 sc->sc_tx_timer = 0;
1295 ifp->if_flags &= ~IFF_OACTIVE;
1296 wpi_start(ifp);
1297 }
1298
1299 static void
1300 wpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc)
1301 {
1302 struct wpi_tx_ring *ring = &sc->cmdq;
1303 struct wpi_tx_data *data;
1304
1305 if ((desc->qid & 7) != 4)
1306 return; /* not a command ack */
1307
1308 data = &ring->data[desc->idx];
1309
1310 /* if the command was mapped in a mbuf, free it */
1311 if (data->m != NULL) {
1312 bus_dmamap_unload(sc->sc_dmat, data->map);
1313 m_freem(data->m);
1314 data->m = NULL;
1315 }
1316
1317 wakeup(&ring->cmd[desc->idx]);
1318 }
1319
1320 static void
1321 wpi_notif_intr(struct wpi_softc *sc)
1322 {
1323 struct ieee80211com *ic = &sc->sc_ic;
1324 struct wpi_rx_desc *desc;
1325 struct wpi_rx_data *data;
1326 uint32_t hw;
1327
1328 hw = le32toh(sc->shared->next);
1329 while (sc->rxq.cur != hw) {
1330 data = &sc->rxq.data[sc->rxq.cur];
1331
1332 desc = mtod(data->m, struct wpi_rx_desc *);
1333
1334 DPRINTFN(4, ("rx notification qid=%x idx=%d flags=%x type=%d "
1335 "len=%d\n", desc->qid, desc->idx, desc->flags,
1336 desc->type, le32toh(desc->len)));
1337
1338 if (!(desc->qid & 0x80)) /* reply to a command */
1339 wpi_cmd_intr(sc, desc);
1340
1341 switch (desc->type) {
1342 case WPI_RX_DONE:
1343 /* a 802.11 frame was received */
1344 wpi_rx_intr(sc, desc, data);
1345 break;
1346
1347 case WPI_TX_DONE:
1348 /* a 802.11 frame has been transmitted */
1349 wpi_tx_intr(sc, desc);
1350 break;
1351
1352 case WPI_UC_READY:
1353 {
1354 struct wpi_ucode_info *uc =
1355 (struct wpi_ucode_info *)(desc + 1);
1356
1357 /* the microcontroller is ready */
1358 DPRINTF(("microcode alive notification version %x "
1359 "alive %x\n", le32toh(uc->version),
1360 le32toh(uc->valid)));
1361
1362 if (le32toh(uc->valid) != 1) {
1363 aprint_error("%s: microcontroller "
1364 "initialization failed\n",
1365 sc->sc_dev.dv_xname);
1366 }
1367 break;
1368 }
1369 case WPI_STATE_CHANGED:
1370 {
1371 uint32_t *status = (uint32_t *)(desc + 1);
1372
1373 /* enabled/disabled notification */
1374 DPRINTF(("state changed to %x\n", le32toh(*status)));
1375
1376 if (le32toh(*status) & 1) {
1377 /* the radio button has to be pushed */
1378 aprint_error("%s: Radio transmitter is off\n",
1379 sc->sc_dev.dv_xname);
1380 }
1381 break;
1382 }
1383 case WPI_START_SCAN:
1384 {
1385 struct wpi_start_scan *scan =
1386 (struct wpi_start_scan *)(desc + 1);
1387
1388 DPRINTFN(2, ("scanning channel %d status %x\n",
1389 scan->chan, le32toh(scan->status)));
1390
1391 /* fix current channel */
1392 ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan];
1393 break;
1394 }
1395 case WPI_STOP_SCAN:
1396 {
1397 struct wpi_stop_scan *scan =
1398 (struct wpi_stop_scan *)(desc + 1);
1399
1400 DPRINTF(("scan finished nchan=%d status=%d chan=%d\n",
1401 scan->nchan, scan->status, scan->chan));
1402
1403 if (scan->status == 1 && scan->chan <= 14) {
1404 /*
1405 * We just finished scanning 802.11g channels,
1406 * start scanning 802.11a ones.
1407 */
1408 if (wpi_scan(sc, IEEE80211_CHAN_A) == 0)
1409 break;
1410 }
1411 ieee80211_end_scan(ic);
1412 break;
1413 }
1414 }
1415
1416 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT;
1417 }
1418
1419 /* tell the firmware what we have processed */
1420 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1;
1421 WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7);
1422 }
1423
1424 static int
1425 wpi_intr(void *arg)
1426 {
1427 struct wpi_softc *sc = arg;
1428 uint32_t r;
1429
1430 r = WPI_READ(sc, WPI_INTR);
1431 if (r == 0 || r == 0xffffffff)
1432 return 0; /* not for us */
1433
1434 DPRINTFN(5, ("interrupt reg %x\n", r));
1435
1436 /* disable interrupts */
1437 WPI_WRITE(sc, WPI_MASK, 0);
1438 /* ack interrupts */
1439 WPI_WRITE(sc, WPI_INTR, r);
1440
1441 if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) {
1442 /* SYSTEM FAILURE, SYSTEM FAILURE */
1443 aprint_error("%s: fatal firmware error\n", sc->sc_dev.dv_xname);
1444 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP;
1445 wpi_stop(&sc->sc_ec.ec_if, 1);
1446 return 1;
1447 }
1448
1449 if (r & WPI_RX_INTR)
1450 wpi_notif_intr(sc);
1451
1452 if (r & WPI_ALIVE_INTR) /* firmware initialized */
1453 wakeup(sc);
1454
1455 /* re-enable interrupts */
1456 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
1457
1458 return 1;
1459 }
1460
1461 static uint8_t
1462 wpi_plcp_signal(int rate)
1463 {
1464 switch (rate) {
1465 /* CCK rates (returned values are device-dependent) */
1466 case 2: return 10;
1467 case 4: return 20;
1468 case 11: return 55;
1469 case 22: return 110;
1470
1471 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1472 /* R1-R4, (u)ral is R4-R1 */
1473 case 12: return 0xd;
1474 case 18: return 0xf;
1475 case 24: return 0x5;
1476 case 36: return 0x7;
1477 case 48: return 0x9;
1478 case 72: return 0xb;
1479 case 96: return 0x1;
1480 case 108: return 0x3;
1481
1482 /* unsupported rates (should not get there) */
1483 default: return 0;
1484 }
1485 }
1486
1487 /* quickly determine if a given rate is CCK or OFDM */
1488 #define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1489
1490 static int
1491 wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni,
1492 int ac)
1493 {
1494 struct ieee80211com *ic = &sc->sc_ic;
1495 struct wpi_tx_ring *ring = &sc->txq[ac];
1496 struct wpi_tx_desc *desc;
1497 struct wpi_tx_data *data;
1498 struct wpi_tx_cmd *cmd;
1499 struct wpi_cmd_data *tx;
1500 struct ieee80211_frame *wh;
1501 struct ieee80211_key *k;
1502 const struct chanAccParams *cap;
1503 struct mbuf *mnew;
1504 int i, error, rate, hdrlen, noack = 0;
1505
1506 desc = &ring->desc[ring->cur];
1507 data = &ring->data[ring->cur];
1508
1509 wh = mtod(m0, struct ieee80211_frame *);
1510
1511 if (IEEE80211_QOS_HAS_SEQ(wh)) {
1512 hdrlen = sizeof (struct ieee80211_qosframe);
1513 cap = &ic->ic_wme.wme_chanParams;
1514 noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1515 } else
1516 hdrlen = sizeof (struct ieee80211_frame);
1517
1518 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1519 k = ieee80211_crypto_encap(ic, ni, m0);
1520 if (k == NULL) {
1521 m_freem(m0);
1522 return ENOBUFS;
1523 }
1524
1525 /* packet header may have moved, reset our local pointer */
1526 wh = mtod(m0, struct ieee80211_frame *);
1527 }
1528
1529 /* pickup a rate */
1530 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1531 IEEE80211_FC0_TYPE_MGT) {
1532 /* mgmt frames are sent at the lowest available bit-rate */
1533 rate = ni->ni_rates.rs_rates[0];
1534 } else {
1535 if (ic->ic_fixed_rate != -1) {
1536 rate = ic->ic_sup_rates[ic->ic_curmode].
1537 rs_rates[ic->ic_fixed_rate];
1538 } else
1539 rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1540 }
1541 rate &= IEEE80211_RATE_VAL;
1542
1543
1544 #if NBPFILTER > 0
1545 if (sc->sc_drvbpf != NULL) {
1546 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap;
1547
1548 tap->wt_flags = 0;
1549 tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq);
1550 tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags);
1551 tap->wt_rate = rate;
1552 tap->wt_hwqueue = ac;
1553 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
1554 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
1555
1556 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0);
1557 }
1558 #endif
1559
1560 cmd = &ring->cmd[ring->cur];
1561 cmd->code = WPI_CMD_TX_DATA;
1562 cmd->flags = 0;
1563 cmd->qid = ring->qid;
1564 cmd->idx = ring->cur;
1565
1566 tx = (struct wpi_cmd_data *)cmd->data;
1567 tx->flags = 0;
1568
1569 if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1570 tx->id = WPI_ID_BSS;
1571 tx->flags |= htole32(WPI_TX_NEED_ACK);
1572 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN >
1573 ic->ic_rtsthreshold || (WPI_RATE_IS_OFDM(rate) &&
1574 (ic->ic_flags & IEEE80211_F_USEPROT)))
1575 tx->flags |= htole32(WPI_TX_NEED_RTS |
1576 WPI_TX_FULL_TXOP);
1577 } else
1578 tx->id = WPI_ID_BROADCAST;
1579
1580 tx->flags |= htole32(WPI_TX_AUTO_SEQ);
1581
1582 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1583 IEEE80211_FC0_TYPE_MGT) {
1584 /* tell h/w to set timestamp in probe responses */
1585 if ((wh->i_fc[0] &
1586 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1587 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1588 tx->flags |= htole32(WPI_TX_INSERT_TSTAMP);
1589
1590 if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1591 IEEE80211_FC0_SUBTYPE_ASSOC_REQ) ||
1592 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1593 IEEE80211_FC0_SUBTYPE_REASSOC_REQ))
1594 tx->timeout = htole16(3);
1595 else
1596 tx->timeout = htole16(2);
1597 } else
1598 tx->timeout = htole16(0);
1599
1600 tx->rate = wpi_plcp_signal(rate);
1601
1602 /* be very persistant at sending frames out */
1603 tx->rts_ntries = 7;
1604 tx->data_ntries = 15;
1605
1606 tx->ofdm_mask = 0xff;
1607 tx->cck_mask = 0xf;
1608 tx->lifetime = htole32(0xffffffff);
1609
1610 tx->len = htole16(m0->m_pkthdr.len);
1611
1612 /* save and trim IEEE802.11 header */
1613 m_copydata(m0, 0, hdrlen, (caddr_t)&tx->wh);
1614 m_adj(m0, hdrlen);
1615
1616 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1617 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1618 if (error != 0 && error != EFBIG) {
1619 aprint_error("%s: could not map mbuf (error %d)\n",
1620 sc->sc_dev.dv_xname, error);
1621 m_freem(m0);
1622 return error;
1623 }
1624 if (error != 0) {
1625 /* too many fragments, linearize */
1626 MGETHDR(mnew, M_DONTWAIT, MT_DATA);
1627 if (mnew == NULL) {
1628 m_freem(m0);
1629 return ENOMEM;
1630 }
1631
1632 M_COPY_PKTHDR(mnew, m0);
1633 if (m0->m_pkthdr.len > MHLEN) {
1634 MCLGET(mnew, M_DONTWAIT);
1635 if (!(mnew->m_flags & M_EXT)) {
1636 m_freem(m0);
1637 m_freem(mnew);
1638 return ENOMEM;
1639 }
1640 }
1641
1642 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, caddr_t));
1643 m_freem(m0);
1644 mnew->m_len = mnew->m_pkthdr.len;
1645 m0 = mnew;
1646
1647 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
1648 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1649 if (error != 0) {
1650 aprint_error("%s: could not map mbuf (error %d)\n",
1651 sc->sc_dev.dv_xname, error);
1652 m_freem(m0);
1653 return error;
1654 }
1655 }
1656
1657 data->m = m0;
1658 data->ni = ni;
1659
1660 DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
1661 ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs));
1662
1663 /* first scatter/gather segment is used by the tx data command */
1664 desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 |
1665 (1 + data->map->dm_nsegs) << 24);
1666 desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
1667 ring->cur * sizeof (struct wpi_tx_cmd));
1668 /*XXX The next line might be wrong. I don't use hdrlen*/
1669 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_data));
1670
1671 for (i = 1; i <= data->map->dm_nsegs; i++) {
1672 desc->segs[i].addr =
1673 htole32(data->map->dm_segs[i - 1].ds_addr);
1674 desc->segs[i].len =
1675 htole32(data->map->dm_segs[i - 1].ds_len);
1676 }
1677
1678 ring->queued++;
1679
1680 /* kick ring */
1681 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT;
1682 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
1683
1684 return 0;
1685 }
1686
1687 static void
1688 wpi_start(struct ifnet *ifp)
1689 {
1690 struct wpi_softc *sc = ifp->if_softc;
1691 struct ieee80211com *ic = &sc->sc_ic;
1692 struct ieee80211_node *ni;
1693 struct ether_header *eh;
1694 struct mbuf *m0;
1695 int ac;
1696
1697 /*
1698 * net80211 may still try to send management frames even if the
1699 * IFF_RUNNING flag is not set...
1700 */
1701 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1702 return;
1703
1704 for (;;) {
1705 IF_POLL(&ic->ic_mgtq, m0);
1706 if (m0 != NULL) {
1707 IF_DEQUEUE(&ic->ic_mgtq, m0);
1708
1709 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1710 m0->m_pkthdr.rcvif = NULL;
1711
1712 /* management frames go into ring 0 */
1713 if (sc->txq[0].queued > sc->txq[0].count - 8) {
1714 ifp->if_oerrors++;
1715 continue;
1716 }
1717 #if NBPFILTER > 0
1718 if (ic->ic_rawbpf != NULL)
1719 bpf_mtap(ic->ic_rawbpf, m0);
1720 #endif
1721 if (wpi_tx_data(sc, m0, ni, 0) != 0) {
1722 ifp->if_oerrors++;
1723 break;
1724 }
1725 } else {
1726 if (ic->ic_state != IEEE80211_S_RUN)
1727 break;
1728 IF_DEQUEUE(&ifp->if_snd, m0);
1729 if (m0 == NULL)
1730 break;
1731
1732 if (m0->m_len < sizeof (*eh) &&
1733 (m0 = m_pullup(m0, sizeof (*eh))) != NULL) {
1734 ifp->if_oerrors++;
1735 continue;
1736 }
1737 eh = mtod(m0, struct ether_header *);
1738 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1739 if (ni == NULL) {
1740 m_freem(m0);
1741 ifp->if_oerrors++;
1742 continue;
1743 }
1744
1745 /* classify mbuf so we can find which tx ring to use */
1746 if (ieee80211_classify(ic, m0, ni) != 0) {
1747 m_freem(m0);
1748 ieee80211_free_node(ni);
1749 ifp->if_oerrors++;
1750 continue;
1751 }
1752
1753 /* no QoS encapsulation for EAPOL frames */
1754 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1755 M_WME_GETAC(m0) : WME_AC_BE;
1756
1757 if (sc->txq[ac].queued > sc->txq[ac].count - 8) {
1758 /* there is no place left in this ring */
1759 IF_PREPEND(&ifp->if_snd, m0);
1760 ifp->if_flags |= IFF_OACTIVE;
1761 break;
1762 }
1763 #if NBPFILTER > 0
1764 if (ifp->if_bpf != NULL)
1765 bpf_mtap(ifp->if_bpf, m0);
1766 #endif
1767 m0 = ieee80211_encap(ic, m0, ni);
1768 if (m0 == NULL) {
1769 ieee80211_free_node(ni);
1770 ifp->if_oerrors++;
1771 continue;
1772 }
1773 #if NBPFILTER > 0
1774 if (ic->ic_rawbpf != NULL)
1775 bpf_mtap(ic->ic_rawbpf, m0);
1776 #endif
1777 if (wpi_tx_data(sc, m0, ni, ac) != 0) {
1778 ieee80211_free_node(ni);
1779 ifp->if_oerrors++;
1780 break;
1781 }
1782 }
1783
1784 sc->sc_tx_timer = 5;
1785 ifp->if_timer = 1;
1786 }
1787 }
1788
1789 static void
1790 wpi_watchdog(struct ifnet *ifp)
1791 {
1792 struct wpi_softc *sc = ifp->if_softc;
1793
1794 ifp->if_timer = 0;
1795
1796 if (sc->sc_tx_timer > 0) {
1797 if (--sc->sc_tx_timer == 0) {
1798 aprint_error("%s: device timeout\n",
1799 sc->sc_dev.dv_xname);
1800 ifp->if_oerrors++;
1801 ifp->if_flags &= ~IFF_UP;
1802 wpi_stop(ifp, 1);
1803 return;
1804 }
1805 ifp->if_timer = 1;
1806 }
1807
1808 ieee80211_watchdog(&sc->sc_ic);
1809 }
1810
1811 static int
1812 wpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1813 {
1814 #define IS_RUNNING(ifp) \
1815 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
1816
1817 struct wpi_softc *sc = ifp->if_softc;
1818 struct ieee80211com *ic = &sc->sc_ic;
1819 struct ifreq *ifr = (struct ifreq *)data;
1820 int s, error = 0;
1821
1822 s = splnet();
1823
1824 switch (cmd) {
1825 case SIOCSIFFLAGS:
1826 if (ifp->if_flags & IFF_UP) {
1827 if (!(ifp->if_flags & IFF_RUNNING))
1828 wpi_init(ifp);
1829 } else {
1830 if (ifp->if_flags & IFF_RUNNING)
1831 wpi_stop(ifp, 1);
1832 }
1833 break;
1834
1835 case SIOCADDMULTI:
1836 case SIOCDELMULTI:
1837 error = (cmd == SIOCADDMULTI) ?
1838 ether_addmulti(ifr, &sc->sc_ec) :
1839 ether_delmulti(ifr, &sc->sc_ec);
1840 if (error == ENETRESET) {
1841 /* setup multicast filter, etc */
1842 error = 0;
1843 }
1844 break;
1845
1846 default:
1847 error = ieee80211_ioctl(&sc->sc_ic, cmd, data);
1848 }
1849
1850 if (error == ENETRESET) {
1851 if (IS_RUNNING(ifp) &&
1852 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
1853 wpi_init(ifp);
1854 error = 0;
1855 }
1856
1857 splx(s);
1858 return error;
1859
1860 #undef IS_RUNNING
1861 }
1862
1863 /*
1864 * Extract various information from EEPROM.
1865 */
1866 static void
1867 wpi_read_eeprom(struct wpi_softc *sc)
1868 {
1869 struct ieee80211com *ic = &sc->sc_ic;
1870 uint16_t val;
1871 int i;
1872
1873 /* read MAC address */
1874 val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 0);
1875 ic->ic_myaddr[0] = val & 0xff;
1876 ic->ic_myaddr[1] = val >> 8;
1877 val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 1);
1878 ic->ic_myaddr[2] = val & 0xff;
1879 ic->ic_myaddr[3] = val >> 8;
1880 val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 2);
1881 ic->ic_myaddr[4] = val & 0xff;
1882 ic->ic_myaddr[5] = val >> 8;
1883
1884 /* read power settings for 2.4GHz channels */
1885 for (i = 0; i < 14; i++) {
1886 sc->pwr1[i] = wpi_read_prom_word(sc, WPI_EEPROM_PWR1 + i);
1887 sc->pwr2[i] = wpi_read_prom_word(sc, WPI_EEPROM_PWR2 + i);
1888 DPRINTFN(2, ("channel %d pwr1 0x%04x pwr2 0x%04x\n", i + 1,
1889 sc->pwr1[i], sc->pwr2[i]));
1890 }
1891 }
1892
1893 /*
1894 * Send a command to the firmware.
1895 */
1896 static int
1897 wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async)
1898 {
1899 struct wpi_tx_ring *ring = &sc->cmdq;
1900 struct wpi_tx_desc *desc;
1901 struct wpi_tx_cmd *cmd;
1902
1903 KASSERT(size <= sizeof cmd->data);
1904
1905 desc = &ring->desc[ring->cur];
1906 cmd = &ring->cmd[ring->cur];
1907
1908 cmd->code = code;
1909 cmd->flags = 0;
1910 cmd->qid = ring->qid;
1911 cmd->idx = ring->cur;
1912 memcpy(cmd->data, buf, size);
1913
1914 desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24);
1915 desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
1916 ring->cur * sizeof (struct wpi_tx_cmd));
1917 desc->segs[0].len = htole32(4 + size);
1918
1919 /* kick cmd ring */
1920 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
1921 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
1922
1923 return async ? 0 : tsleep(cmd, PCATCH, "wpicmd", hz);
1924 }
1925
1926 static int
1927 wpi_wme_update(struct ieee80211com *ic)
1928 {
1929 #define WPI_EXP2(v) htole16((1 << (v)) - 1)
1930 #define WPI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v))
1931 struct wpi_softc *sc = ic->ic_ifp->if_softc;
1932 const struct wmeParams *wmep;
1933 struct wpi_wme_setup wme;
1934 int ac;
1935
1936 /* don't override default WME values if WME is not actually enabled */
1937 if (!(ic->ic_flags & IEEE80211_F_WME))
1938 return 0;
1939
1940 wme.flags = 0;
1941 for (ac = 0; ac < WME_NUM_AC; ac++) {
1942 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
1943 wme.ac[ac].aifsn = wmep->wmep_aifsn;
1944 wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin);
1945 wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax);
1946 wme.ac[ac].txop = WPI_USEC(wmep->wmep_txopLimit);
1947
1948 DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d "
1949 "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin,
1950 wme.ac[ac].cwmax, wme.ac[ac].txop));
1951 }
1952
1953 return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1);
1954 #undef WPI_USEC
1955 #undef WPI_EXP2
1956 }
1957
1958 /*
1959 * Configure h/w multi-rate retries.
1960 */
1961 static int
1962 wpi_mrr_setup(struct wpi_softc *sc)
1963 {
1964 struct ieee80211com *ic = &sc->sc_ic;
1965 struct wpi_mrr_setup mrr;
1966 int i, error;
1967
1968 /* CCK rates (not used with 802.11a) */
1969 for (i = WPI_CCK1; i <= WPI_CCK11; i++) {
1970 mrr.rates[i].flags = 0;
1971 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
1972 /* fallback to the immediate lower CCK rate (if any) */
1973 mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1;
1974 /* try one time at this rate before falling back to "next" */
1975 mrr.rates[i].ntries = 1;
1976 }
1977
1978 /* OFDM rates (not used with 802.11b) */
1979 for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) {
1980 mrr.rates[i].flags = 0;
1981 mrr.rates[i].plcp = wpi_ridx_to_plcp[i];
1982 /* fallback to the immediate lower rate (if any) */
1983 /* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */
1984 mrr.rates[i].next = (i == WPI_OFDM6) ?
1985 ((ic->ic_curmode == IEEE80211_MODE_11A) ?
1986 WPI_OFDM6 : WPI_CCK2) :
1987 i - 1;
1988 /* try one time at this rate before falling back to "next" */
1989 mrr.rates[i].ntries = 1;
1990 }
1991
1992 /* setup MRR for control frames */
1993 mrr.which = htole32(WPI_MRR_CTL);
1994 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 1);
1995 if (error != 0) {
1996 aprint_error("%s: could not setup MRR for control frames\n",
1997 sc->sc_dev.dv_xname);
1998 return error;
1999 }
2000
2001 /* setup MRR for data frames */
2002 mrr.which = htole32(WPI_MRR_DATA);
2003 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 1);
2004 if (error != 0) {
2005 aprint_error("%s: could not setup MRR for data frames\n",
2006 sc->sc_dev.dv_xname);
2007 return error;
2008 }
2009
2010 return 0;
2011 }
2012
2013 static void
2014 wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on)
2015 {
2016 struct wpi_cmd_led led;
2017
2018 led.which = which;
2019 led.unit = htole32(100000); /* on/off in unit of 100ms */
2020 led.off = off;
2021 led.on = on;
2022
2023 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1);
2024 }
2025
2026 static void
2027 wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni)
2028 {
2029 struct wpi_cmd_tsf tsf;
2030 uint64_t val, mod;
2031
2032 memset(&tsf, 0, sizeof tsf);
2033 memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8);
2034 tsf.bintval = htole16(ni->ni_intval);
2035 tsf.lintval = htole16(10);
2036
2037 /* compute remaining time until next beacon */
2038 val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */
2039 mod = le64toh(tsf.tstamp) % val;
2040 tsf.binitval = htole32((uint32_t)(val - mod));
2041
2042 DPRINTF(("TSF bintval=%u tstamp=%llu, init=%u\n",
2043 ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod)));
2044
2045 if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0)
2046 aprint_error("%s: could not enable TSF\n", sc->sc_dev.dv_xname);
2047 }
2048
2049 /*
2050 * Build a beacon frame that the firmware will broadcast periodically in
2051 * IBSS or HostAP modes.
2052 */
2053 static int
2054 wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni)
2055 {
2056 struct ieee80211com *ic = &sc->sc_ic;
2057 struct wpi_tx_ring *ring = &sc->cmdq;
2058 struct wpi_tx_desc *desc;
2059 struct wpi_tx_data *data;
2060 struct wpi_tx_cmd *cmd;
2061 struct wpi_cmd_beacon *bcn;
2062 struct ieee80211_beacon_offsets bo;
2063 struct mbuf *m0;
2064 int error;
2065
2066 desc = &ring->desc[ring->cur];
2067 data = &ring->data[ring->cur];
2068
2069 m0 = ieee80211_beacon_alloc(ic, ni, &bo);
2070 if (m0 == NULL) {
2071 aprint_error("%s: could not allocate beacon frame\n",
2072 sc->sc_dev.dv_xname);
2073 return ENOMEM;
2074 }
2075
2076 cmd = &ring->cmd[ring->cur];
2077 cmd->code = WPI_CMD_SET_BEACON;
2078 cmd->flags = 0;
2079 cmd->qid = ring->qid;
2080 cmd->idx = ring->cur;
2081
2082 bcn = (struct wpi_cmd_beacon *)cmd->data;
2083 memset(bcn, 0, sizeof (struct wpi_cmd_beacon));
2084 bcn->id = WPI_ID_BROADCAST;
2085 bcn->ofdm_mask = 0xff;
2086 bcn->cck_mask = 0x0f;
2087 bcn->lifetime = htole32(0xffffffff);
2088 bcn->len = htole16(m0->m_pkthdr.len);
2089 bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2090 wpi_plcp_signal(12) : wpi_plcp_signal(2);
2091 bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP);
2092
2093 /* save and trim IEEE802.11 header */
2094 m_copydata(m0, 0, sizeof (struct ieee80211_frame), (caddr_t)&bcn->wh);
2095 m_adj(m0, sizeof (struct ieee80211_frame));
2096
2097 /* assume beacon frame is contiguous */
2098 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0,
2099 BUS_DMA_READ | BUS_DMA_NOWAIT);
2100 if (error) {
2101 aprint_error("%s: could not map beacon\n", sc->sc_dev.dv_xname);
2102 m_freem(m0);
2103 return error;
2104 }
2105
2106 data->m = m0;
2107
2108 /* first scatter/gather segment is used by the beacon command */
2109 desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24);
2110 desc->segs[0].addr = htole32(ring->cmd_dma.paddr +
2111 ring->cur * sizeof (struct wpi_tx_cmd));
2112 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_beacon));
2113 desc->segs[1].addr = htole32(data->map->dm_segs[0].ds_addr);
2114 desc->segs[1].len = htole32(data->map->dm_segs[0].ds_len);
2115
2116 /* kick cmd ring */
2117 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2118 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2119
2120 return 0;
2121 }
2122
2123 static int
2124 wpi_auth(struct wpi_softc *sc)
2125 {
2126 struct ieee80211com *ic = &sc->sc_ic;
2127 struct ieee80211_node *ni = ic->ic_bss;
2128 struct wpi_node_info node;
2129 int error;
2130
2131 /* update adapter's configuration */
2132 IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid);
2133 sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan);
2134 sc->config.flags = htole32(WPI_CONFIG_TSF);
2135 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) {
2136 sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2137 WPI_CONFIG_24GHZ);
2138 }
2139 switch (ic->ic_curmode) {
2140 case IEEE80211_MODE_11A:
2141 sc->config.cck_mask = 0;
2142 sc->config.ofdm_mask = 0x15;
2143 break;
2144 case IEEE80211_MODE_11B:
2145 sc->config.cck_mask = 0x03;
2146 sc->config.ofdm_mask = 0;
2147 break;
2148 default: /* assume 802.11b/g */
2149 sc->config.cck_mask = 0x0f;
2150 sc->config.ofdm_mask = 0x15;
2151 }
2152
2153 DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan,
2154 sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask));
2155 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2156 sizeof (struct wpi_config), 1);
2157 if (error != 0) {
2158 aprint_error("%s: could not configure\n", sc->sc_dev.dv_xname);
2159 return error;
2160 }
2161
2162 /* add default node */
2163 memset(&node, 0, sizeof node);
2164 IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid);
2165 node.id = WPI_ID_BSS;
2166 node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ?
2167 wpi_plcp_signal(12) : wpi_plcp_signal(2);
2168 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1);
2169 if (error != 0) {
2170 aprint_error("%s: could not add BSS node\n", sc->sc_dev.dv_xname);
2171 return error;
2172 }
2173
2174 error = wpi_mrr_setup(sc);
2175 if (error != 0) {
2176 aprint_error("%s: could not setup MRR\n", sc->sc_dev.dv_xname);
2177 return error;
2178 }
2179
2180 return 0;
2181 }
2182
2183 /*
2184 * Send a scan request to the firmware. Since this command is huge, we map it
2185 * into a mbuf instead of using the pre-allocated set of commands.
2186 */
2187 static int
2188 wpi_scan(struct wpi_softc *sc, uint16_t flags)
2189 {
2190 struct ieee80211com *ic = &sc->sc_ic;
2191 struct wpi_tx_ring *ring = &sc->cmdq;
2192 struct wpi_tx_desc *desc;
2193 struct wpi_tx_data *data;
2194 struct wpi_tx_cmd *cmd;
2195 struct wpi_scan_hdr *hdr;
2196 struct wpi_scan_chan *chan;
2197 struct ieee80211_frame *wh;
2198 struct ieee80211_rateset *rs;
2199 struct ieee80211_channel *c;
2200 enum ieee80211_phymode mode;
2201 uint8_t *frm;
2202 int nrates, pktlen, error;
2203
2204 desc = &ring->desc[ring->cur];
2205 data = &ring->data[ring->cur];
2206
2207 MGETHDR(data->m, M_DONTWAIT, MT_DATA);
2208 if (data->m == NULL) {
2209 aprint_error("%s: could not allocate mbuf for scan command\n",
2210 sc->sc_dev.dv_xname);
2211 return ENOMEM;
2212 }
2213
2214 MCLGET(data->m, M_DONTWAIT);
2215 if (!(data->m->m_flags & M_EXT)) {
2216 m_freem(data->m);
2217 data->m = NULL;
2218 aprint_error("%s: could not allocate mbuf for scan command\n",
2219 sc->sc_dev.dv_xname);
2220 return ENOMEM;
2221 }
2222
2223 cmd = mtod(data->m, struct wpi_tx_cmd *);
2224 cmd->code = WPI_CMD_SCAN;
2225 cmd->flags = 0;
2226 cmd->qid = ring->qid;
2227 cmd->idx = ring->cur;
2228
2229 hdr = (struct wpi_scan_hdr *)cmd->data;
2230 memset(hdr, 0, sizeof (struct wpi_scan_hdr));
2231 hdr->first = 1;
2232 /*
2233 * Move to the next channel if no packets are received within 5 msecs
2234 * after sending the probe request (this helps to reduce the duration
2235 * of active scans).
2236 */
2237 hdr->quiet = htole16(5); /* timeout in milliseconds */
2238 hdr->threshold = htole16(1); /* min # of packets */
2239
2240 if (flags & IEEE80211_CHAN_A) {
2241 hdr->band = htole16(WPI_SCAN_5GHZ);
2242 /* send probe requests at 6Mbps */
2243 hdr->rate = wpi_plcp_signal(12);
2244 } else {
2245 hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO);
2246 /* send probe requests at 1Mbps */
2247 hdr->rate = wpi_plcp_signal(2);
2248 }
2249 hdr->id = WPI_ID_BROADCAST;
2250 hdr->mask = htole32(0xffffffff);
2251 hdr->magic1 = htole32(1 << 13);
2252
2253 hdr->esslen = ic->ic_des_esslen;
2254 memcpy(hdr->essid, ic->ic_des_essid, ic->ic_des_esslen);
2255
2256 /*
2257 * Build a probe request frame. Most of the following code is a
2258 * copy & paste of what is done in net80211.
2259 */
2260 wh = (struct ieee80211_frame *)(hdr + 1);
2261 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
2262 IEEE80211_FC0_SUBTYPE_PROBE_REQ;
2263 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2264 IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr);
2265 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
2266 IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr);
2267 *(u_int16_t *)&wh->i_dur[0] = 0; /* filled by h/w */
2268 *(u_int16_t *)&wh->i_seq[0] = 0; /* filled by h/w */
2269
2270 frm = (uint8_t *)(wh + 1);
2271
2272 /* add essid IE */
2273 *frm++ = IEEE80211_ELEMID_SSID;
2274 *frm++ = ic->ic_des_esslen;
2275 memcpy(frm, ic->ic_des_essid, ic->ic_des_esslen);
2276 frm += ic->ic_des_esslen;
2277
2278 mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan);
2279 rs = &ic->ic_sup_rates[mode];
2280
2281 /* add supported rates IE */
2282 *frm++ = IEEE80211_ELEMID_RATES;
2283 nrates = rs->rs_nrates;
2284 if (nrates > IEEE80211_RATE_SIZE)
2285 nrates = IEEE80211_RATE_SIZE;
2286 *frm++ = nrates;
2287 memcpy(frm, rs->rs_rates, nrates);
2288 frm += nrates;
2289
2290 /* add supported xrates IE */
2291 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
2292 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE;
2293 *frm++ = IEEE80211_ELEMID_XRATES;
2294 *frm++ = nrates;
2295 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates);
2296 frm += nrates;
2297 }
2298
2299 /* add optionnal IE (usually an RSN IE) */
2300 if (ic->ic_opt_ie != NULL) {
2301 memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len);
2302 frm += ic->ic_opt_ie_len;
2303 }
2304
2305 /* setup length of probe request */
2306 hdr->pbrlen = htole16(frm - (uint8_t *)wh);
2307
2308 chan = (struct wpi_scan_chan *)frm;
2309 for (c = &ic->ic_channels[1];
2310 c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) {
2311 if ((c->ic_flags & flags) != flags)
2312 continue;
2313
2314 chan->chan = ieee80211_chan2ieee(ic, c);
2315 chan->flags = (c->ic_flags & IEEE80211_CHAN_PASSIVE) ?
2316 0 : WPI_CHAN_ACTIVE;
2317 chan->magic = htole16(0x62ab);
2318 if (IEEE80211_IS_CHAN_5GHZ(c)) {
2319 chan->active = htole16(10);
2320 chan->passive = htole16(110);
2321 } else {
2322 chan->active = htole16(20);
2323 chan->passive = htole16(120);
2324 }
2325 hdr->nchan++;
2326 chan++;
2327
2328 frm += sizeof (struct wpi_scan_chan);
2329 }
2330
2331 hdr->len = hdr->nchan * sizeof (struct wpi_scan_chan);
2332 pktlen = frm - mtod(data->m, uint8_t *);
2333
2334 error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen,
2335 NULL, BUS_DMA_NOWAIT);
2336 if (error) {
2337 aprint_error("%s: could not map scan command\n",
2338 sc->sc_dev.dv_xname);
2339 m_freem(data->m);
2340 data->m = NULL;
2341 return error;
2342 }
2343
2344 desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24);
2345 desc->segs[0].addr = htole32(data->map->dm_segs[0].ds_addr);
2346 desc->segs[0].len = htole32(data->map->dm_segs[0].ds_len);
2347
2348 /* kick cmd ring */
2349 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT;
2350 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur);
2351
2352 return 0; /* will be notified async. of failure/success */
2353 }
2354
2355 static int
2356 wpi_config(struct wpi_softc *sc)
2357 {
2358 struct ieee80211com *ic = &sc->sc_ic;
2359 struct ifnet *ifp = ic->ic_ifp;
2360 struct wpi_txpower txpower;
2361 struct wpi_power power;
2362 struct wpi_bluetooth bluetooth;
2363 struct wpi_node_info node;
2364 int error;
2365
2366 /* set Tx power for 2.4GHz channels (values read from EEPROM) */
2367 memset(&txpower, 0, sizeof txpower);
2368 memcpy(txpower.pwr1, sc->pwr1, 14 * sizeof (uint16_t));
2369 memcpy(txpower.pwr2, sc->pwr2, 14 * sizeof (uint16_t));
2370 error = wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, 0);
2371 if (error != 0) {
2372 aprint_error("%s: could not set txpower\n",
2373 sc->sc_dev.dv_xname);
2374 return error;
2375 }
2376
2377 /* set power mode */
2378 memset(&power, 0, sizeof power);
2379 power.flags = htole32(0x8); /* XXX */
2380 error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0);
2381 if (error != 0) {
2382 aprint_error("%s: could not set power mode\n",
2383 sc->sc_dev.dv_xname);
2384 return error;
2385 }
2386
2387 /* configure bluetooth coexistence */
2388 memset(&bluetooth, 0, sizeof bluetooth);
2389 bluetooth.flags = 3;
2390 bluetooth.lead = 0xaa;
2391 bluetooth.kill = 1;
2392 error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth,
2393 0);
2394 if (error != 0) {
2395 aprint_error(
2396 "%s: could not configure bluetooth coexistence\n",
2397 sc->sc_dev.dv_xname);
2398 return error;
2399 }
2400
2401 /* configure adapter */
2402 memset(&sc->config, 0, sizeof (struct wpi_config));
2403 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl));
2404 IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr);
2405 /*set default channel*/
2406 sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan);
2407 sc->config.flags = htole32(WPI_CONFIG_TSF);
2408 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan)) {
2409 sc->config.flags |= htole32(WPI_CONFIG_AUTO |
2410 WPI_CONFIG_24GHZ);
2411 }
2412 sc->config.filter = 0;
2413 switch (ic->ic_opmode) {
2414 case IEEE80211_M_STA:
2415 sc->config.mode = WPI_MODE_STA;
2416 sc->config.filter |= htole32(WPI_FILTER_MULTICAST);
2417 break;
2418 case IEEE80211_M_IBSS:
2419 case IEEE80211_M_AHDEMO:
2420 sc->config.mode = WPI_MODE_IBSS;
2421 break;
2422 case IEEE80211_M_HOSTAP:
2423 sc->config.mode = WPI_MODE_HOSTAP;
2424 break;
2425 case IEEE80211_M_MONITOR:
2426 sc->config.mode = WPI_MODE_MONITOR;
2427 sc->config.filter |= htole32(WPI_FILTER_MULTICAST |
2428 WPI_FILTER_CTL | WPI_FILTER_PROMISC);
2429 break;
2430 }
2431 sc->config.cck_mask = 0x0f; /* not yet negotiated */
2432 sc->config.ofdm_mask = 0xff; /* not yet negotiated */
2433 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config,
2434 sizeof (struct wpi_config), 0);
2435 if (error != 0) {
2436 aprint_error("%s: configure command failed\n",
2437 sc->sc_dev.dv_xname);
2438 return error;
2439 }
2440
2441 /* add broadcast node */
2442 memset(&node, 0, sizeof node);
2443 IEEE80211_ADDR_COPY(node.bssid, etherbroadcastaddr);
2444 node.id = WPI_ID_BROADCAST;
2445 node.rate = wpi_plcp_signal(2);
2446 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0);
2447 if (error != 0) {
2448 aprint_error("%s: could not add broadcast node\n",
2449 sc->sc_dev.dv_xname);
2450 return error;
2451 }
2452
2453 return 0;
2454 }
2455
2456 static void
2457 wpi_stop_master(struct wpi_softc *sc)
2458 {
2459 uint32_t tmp;
2460 int ntries;
2461
2462 tmp = WPI_READ(sc, WPI_RESET);
2463 WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER);
2464
2465 tmp = WPI_READ(sc, WPI_GPIO_CTL);
2466 if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP)
2467 return; /* already asleep */
2468
2469 for (ntries = 0; ntries < 100; ntries++) {
2470 if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED)
2471 break;
2472 DELAY(10);
2473 }
2474 if (ntries == 100) {
2475 aprint_error("%s: timeout waiting for master\n",
2476 sc->sc_dev.dv_xname);
2477 }
2478 }
2479
2480 static int
2481 wpi_power_up(struct wpi_softc *sc)
2482 {
2483 uint32_t tmp;
2484 int ntries;
2485
2486 wpi_mem_lock(sc);
2487 tmp = wpi_mem_read(sc, WPI_MEM_POWER);
2488 wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000);
2489 wpi_mem_unlock(sc);
2490
2491 for (ntries = 0; ntries < 5000; ntries++) {
2492 if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED)
2493 break;
2494 DELAY(10);
2495 }
2496 if (ntries == 5000) {
2497 aprint_error("%s: timeout waiting for NIC to power up\n",
2498 sc->sc_dev.dv_xname);
2499 return ETIMEDOUT;
2500 }
2501 return 0;
2502 }
2503
2504 static int
2505 wpi_reset(struct wpi_softc *sc)
2506 {
2507 uint32_t tmp;
2508 int ntries;
2509
2510 /* clear any pending interrupts */
2511 WPI_WRITE(sc, WPI_INTR, 0xffffffff);
2512
2513 tmp = WPI_READ(sc, WPI_PLL_CTL);
2514 WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT);
2515
2516 tmp = WPI_READ(sc, WPI_CHICKEN);
2517 WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS);
2518
2519 tmp = WPI_READ(sc, WPI_GPIO_CTL);
2520 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT);
2521
2522 /* wait for clock stabilization */
2523 for (ntries = 0; ntries < 1000; ntries++) {
2524 if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK)
2525 break;
2526 DELAY(10);
2527 }
2528 if (ntries == 1000) {
2529 aprint_error("%s: timeout waiting for clock stabilization\n",
2530 sc->sc_dev.dv_xname);
2531 return ETIMEDOUT;
2532 }
2533
2534 /* initialize EEPROM */
2535 tmp = WPI_READ(sc, WPI_EEPROM_STATUS);
2536 if ((tmp & WPI_EEPROM_VERSION) == 0) {
2537 aprint_error("%s: EEPROM not found\n", sc->sc_dev.dv_xname);
2538 return EIO;
2539 }
2540 WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED);
2541
2542 return 0;
2543 }
2544
2545 static void
2546 wpi_hw_config(struct wpi_softc *sc)
2547 {
2548 uint16_t val;
2549 uint32_t rev, hw;
2550
2551 /* voodoo from the Linux "driver".. */
2552 hw = WPI_READ(sc, WPI_HWCONFIG);
2553
2554 rev = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG);
2555 rev = PCI_REVISION(rev);
2556 if ((rev & 0xc0) == 0x40)
2557 hw |= WPI_HW_ALM_MB;
2558 else if (!(rev & 0x80))
2559 hw |= WPI_HW_ALM_MM;
2560
2561 val = wpi_read_prom_word(sc, WPI_EEPROM_CAPABILITIES);
2562 if ((val & 0xff) == 0x80)
2563 hw |= WPI_HW_SKU_MRC;
2564
2565 val = wpi_read_prom_word(sc, WPI_EEPROM_REVISION);
2566 hw &= ~WPI_HW_REV_D;
2567 if ((val & 0xf0) == 0xd0)
2568 hw |= WPI_HW_REV_D;
2569
2570 val = wpi_read_prom_word(sc, WPI_EEPROM_TYPE);
2571 if ((val & 0xff) > 1)
2572 hw |= WPI_HW_TYPE_B;
2573
2574 DPRINTF(("setting h/w config %x\n", hw));
2575 WPI_WRITE(sc, WPI_HWCONFIG, hw);
2576 }
2577
2578 static int
2579 wpi_init(struct ifnet *ifp)
2580 {
2581 struct wpi_softc *sc = ifp->if_softc;
2582 struct ieee80211com *ic = &sc->sc_ic;
2583 struct wpi_firmware_hdr hdr;
2584 const char *boot, *text, *data;
2585 firmware_handle_t fw;
2586 u_char *dfw;
2587 off_t size;
2588 size_t wsize;
2589 uint32_t tmp;
2590 int qid, ntries, error;
2591
2592 (void)wpi_reset(sc);
2593
2594 wpi_mem_lock(sc);
2595 wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00);
2596 DELAY(20);
2597 tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV);
2598 wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800);
2599 wpi_mem_unlock(sc);
2600
2601 (void)wpi_power_up(sc);
2602 wpi_hw_config(sc);
2603
2604 /* init Rx ring */
2605 wpi_mem_lock(sc);
2606 WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr);
2607 WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr +
2608 offsetof(struct wpi_shared, next));
2609 WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
2610 WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010);
2611 wpi_mem_unlock(sc);
2612
2613 /* init Tx rings */
2614 wpi_mem_lock(sc);
2615 wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */
2616 wpi_mem_write(sc, WPI_MEM_RA, 1); /* enable RA0 */
2617 wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
2618 wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000);
2619 wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002);
2620 wpi_mem_write(sc, WPI_MEM_MAGIC4, 4);
2621 wpi_mem_write(sc, WPI_MEM_MAGIC5, 5);
2622
2623 WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr);
2624 WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5);
2625
2626 for (qid = 0; qid < 6; qid++) {
2627 WPI_WRITE(sc, WPI_TX_CTL(qid), 0);
2628 WPI_WRITE(sc, WPI_TX_BASE(qid), 0);
2629 WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008);
2630 }
2631 wpi_mem_unlock(sc);
2632
2633 /* clear "radio off" and "disable command" bits (reversed logic) */
2634 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF);
2635 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD);
2636
2637 /* clear any pending interrupts */
2638 WPI_WRITE(sc, WPI_INTR, 0xffffffff);
2639 /* enable interrupts */
2640 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK);
2641
2642 if ((error = firmware_open("if_wpi", "ipw3945.ucode", &fw)) != 0) {
2643 aprint_error("%s: could not read firmware file\n",
2644 sc->sc_dev.dv_xname);
2645 goto fail1;
2646 }
2647
2648 size = firmware_get_size(fw);
2649
2650 if (size < sizeof (struct wpi_firmware_hdr)) {
2651 aprint_error("%s: firmware file too short\n",
2652 sc->sc_dev.dv_xname);
2653 error = EINVAL;
2654 goto fail2;
2655 }
2656
2657 if ((error = firmware_read(fw, 0, &hdr,
2658 sizeof (struct wpi_firmware_hdr))) != 0) {
2659 aprint_error("%s: can't get firmware header\n",
2660 sc->sc_dev.dv_xname);
2661 goto fail2;
2662 }
2663
2664 wsize = sizeof (struct wpi_firmware_hdr) + le32toh(hdr.textsz) +
2665 le32toh(hdr.datasz) + le32toh(hdr.bootsz);
2666
2667 if (size < wsize) {
2668 aprint_error("%s: fw file too short: should be %d bytes\n",
2669 sc->sc_dev.dv_xname, wsize);
2670 error = EINVAL;
2671 goto fail2;
2672 }
2673
2674 dfw = firmware_malloc(size);
2675 if (dfw == NULL) {
2676 aprint_error("%s: not enough memory to stock firmware\n",
2677 sc->sc_dev.dv_xname);
2678 error = ENOMEM;
2679 goto fail2;
2680 }
2681
2682 if ((error = firmware_read(fw, 0, dfw, size)) != 0) {
2683 aprint_error("%s: can't get firmware\n",
2684 sc->sc_dev.dv_xname);
2685 goto fail2;
2686 }
2687
2688 /* firmware image layout: |HDR|<--TEXT-->|<--DATA-->|<--BOOT-->| */
2689 text = dfw + sizeof (struct wpi_firmware_hdr);
2690 data = text + le32toh(hdr.textsz);
2691 boot = data + le32toh(hdr.datasz);
2692
2693 /* load firmware boot code into NIC */
2694 error = wpi_load_microcode(sc, boot, le32toh(hdr.bootsz));
2695 if (error != 0) {
2696 aprint_error("%s: could not load microcode\n", sc->sc_dev.dv_xname);
2697 goto fail3;
2698 }
2699
2700 /* load firmware .text segment into NIC */
2701 error = wpi_load_firmware(sc, WPI_FW_TEXT, text, le32toh(hdr.textsz));
2702 if (error != 0) {
2703 aprint_error("%s: could not load firmware\n",
2704 sc->sc_dev.dv_xname);
2705 goto fail3;
2706 }
2707
2708 /* load firmware .data segment into NIC */
2709 error = wpi_load_firmware(sc, WPI_FW_DATA, data, le32toh(hdr.datasz));
2710 if (error != 0) {
2711 aprint_error("%s: could not load firmware\n",
2712 sc->sc_dev.dv_xname);
2713 goto fail3;
2714 }
2715
2716 firmware_free(dfw, 0);
2717 firmware_close(fw);
2718
2719 /* now press "execute" ;-) */
2720 tmp = WPI_READ(sc, WPI_RESET);
2721 tmp &= ~(WPI_MASTER_DISABLED | WPI_STOP_MASTER | WPI_NEVO_RESET);
2722 WPI_WRITE(sc, WPI_RESET, tmp);
2723
2724 /* ..and wait at most one second for adapter to initialize */
2725 if ((error = tsleep(sc, PCATCH, "wpiinit", hz)) != 0) {
2726 /* this isn't what was supposed to happen.. */
2727 aprint_error("%s: timeout waiting for adapter to initialize\n",
2728 sc->sc_dev.dv_xname);
2729 goto fail1;
2730 }
2731
2732 /* wait for thermal sensors to calibrate */
2733 for (ntries = 0; ntries < 1000; ntries++) {
2734 if (WPI_READ(sc, WPI_TEMPERATURE) != 0)
2735 break;
2736 DELAY(10);
2737 }
2738 if (ntries == 1000) {
2739 aprint_error("%s: timeout waiting for thermal sensors calibration\n",
2740 sc->sc_dev.dv_xname);
2741 error = ETIMEDOUT;
2742 goto fail1;
2743 }
2744 DPRINTF(("temperature %d\n", (int)WPI_READ(sc, WPI_TEMPERATURE)));
2745
2746 if ((error = wpi_config(sc)) != 0) {
2747 aprint_error("%s: could not configure device\n",
2748 sc->sc_dev.dv_xname);
2749 goto fail1;
2750 }
2751
2752 ifp->if_flags &= ~IFF_OACTIVE;
2753 ifp->if_flags |= IFF_RUNNING;
2754
2755 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2756 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2757 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2758 }
2759 else
2760 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2761
2762 return 0;
2763
2764 fail3: firmware_free(dfw, 0);
2765 fail2: firmware_close(fw);
2766 fail1: wpi_stop(ifp, 1);
2767 return error;
2768 }
2769
2770
2771 static void
2772 wpi_stop(struct ifnet *ifp, int disable __unused)
2773 {
2774 struct wpi_softc *sc = ifp->if_softc;
2775 struct ieee80211com *ic = &sc->sc_ic;
2776 uint32_t tmp;
2777 int ac;
2778
2779 ifp->if_timer = sc->sc_tx_timer = 0;
2780 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2781
2782 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2783
2784 /* disable interrupts */
2785 WPI_WRITE(sc, WPI_MASK, 0);
2786 WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK);
2787 WPI_WRITE(sc, WPI_INTR_STATUS, 0xff);
2788 WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000);
2789
2790 wpi_mem_lock(sc);
2791 wpi_mem_write(sc, WPI_MEM_MODE, 0);
2792 wpi_mem_unlock(sc);
2793
2794 /* reset all Tx rings */
2795 for (ac = 0; ac < 4; ac++)
2796 wpi_reset_tx_ring(sc, &sc->txq[ac]);
2797 wpi_reset_tx_ring(sc, &sc->cmdq);
2798 wpi_reset_tx_ring(sc, &sc->svcq);
2799
2800 /* reset Rx ring */
2801 wpi_reset_rx_ring(sc, &sc->rxq);
2802
2803 wpi_mem_lock(sc);
2804 wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200);
2805 wpi_mem_unlock(sc);
2806
2807 DELAY(5);
2808
2809 wpi_stop_master(sc);
2810
2811 tmp = WPI_READ(sc, WPI_RESET);
2812 WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET);
2813 }
2814
2815 static void
2816 wpi_iter_func(void *arg, struct ieee80211_node *ni)
2817 {
2818 struct wpi_softc *sc = arg;
2819 struct wpi_node *wn = (struct wpi_node *)ni;
2820
2821 ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn);
2822 }
2823
2824 static void
2825 wpi_amrr_timeout(void *arg)
2826 {
2827 struct wpi_softc *sc = arg;
2828 struct ieee80211com *ic = &sc->sc_ic;
2829
2830 if (ic->ic_opmode == IEEE80211_M_STA)
2831 wpi_iter_func(sc, ic->ic_bss);
2832 else
2833 ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc);
2834
2835 callout_reset(&sc->amrr_ch, hz, wpi_amrr_timeout, sc);
2836 }
2837
2838 static void
2839 wpi_newassoc(struct ieee80211_node *ni, int isnew)
2840 {
2841 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc;
2842 int i;
2843
2844 ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn);
2845 /* set rate to some reasonable initial value */
2846 for (i = ni->ni_rates.rs_nrates - 1;
2847 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72;
2848 i--);
2849 ni->ni_txrate = i;
2850 }
2851