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