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