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