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