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