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