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