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