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