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