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