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