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