athn.c revision 1.2 1 /* $NetBSD: athn.c,v 1.2 2013/04/05 19:14:31 martin Exp $ */
2 /* $OpenBSD: athn.c,v 1.75 2013/01/14 09:50:31 jsing Exp $ */
3
4 /*-
5 * Copyright (c) 2009 Damien Bergamini <damien.bergamini (at) free.fr>
6 * Copyright (c) 2008-2010 Atheros Communications Inc.
7 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
21 /*
22 * Driver for Atheros 802.11a/g/n chipsets.
23 */
24
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: athn.c,v 1.2 2013/04/05 19:14:31 martin Exp $");
27
28 #ifndef _MODULE
29 #include "athn_usb.h" /* for NATHN_USB */
30 #endif
31
32 #include <sys/param.h>
33 #include <sys/sockio.h>
34 #include <sys/mbuf.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/queue.h>
40 #include <sys/callout.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43
44 #include <sys/bus.h>
45 #include <sys/endian.h>
46 #include <sys/intr.h>
47
48 #include <net/bpf.h>
49 #include <net/if.h>
50 #include <net/if_arp.h>
51 #include <net/if_dl.h>
52 #include <net/if_ether.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_amrr.h>
63 #include <net80211/ieee80211_radiotap.h>
64
65 #include <dev/ic/athnreg.h>
66 #include <dev/ic/athnvar.h>
67 #include <dev/ic/arn5008.h>
68 #include <dev/ic/arn5416.h>
69 #include <dev/ic/arn9003.h>
70 #include <dev/ic/arn9280.h>
71 #include <dev/ic/arn9285.h>
72 #include <dev/ic/arn9287.h>
73 #include <dev/ic/arn9380.h>
74
75 #define Static static
76
77 #define IS_UP_AND_RUNNING(ifp) \
78 (((ifp)->if_flags & IFF_UP) && ((ifp)->if_flags & IFF_RUNNING))
79
80 #ifdef ATHN_DEBUG
81 int athn_debug = 0;
82 #endif
83
84 Static int athn_clock_rate(struct athn_softc *);
85 Static const char *
86 athn_get_mac_name(struct athn_softc *);
87 Static const char *
88 athn_get_rf_name(struct athn_softc *);
89 Static int athn_init(struct ifnet *);
90 Static int athn_init_calib(struct athn_softc *,
91 struct ieee80211_channel *, struct ieee80211_channel *);
92 Static int athn_ioctl(struct ifnet *, u_long, void *);
93 Static int athn_media_change(struct ifnet *);
94 Static int athn_newstate(struct ieee80211com *, enum ieee80211_state,
95 int);
96 Static struct ieee80211_node *
97 athn_node_alloc(struct ieee80211_node_table *);
98 Static int athn_reset_power_on(struct athn_softc *);
99 Static int athn_stop_rx_dma(struct athn_softc *);
100 Static int athn_switch_chan(struct athn_softc *,
101 struct ieee80211_channel *, struct ieee80211_channel *);
102 Static void athn_calib_to(void *);
103 Static void athn_disable_interrupts(struct athn_softc *);
104 Static void athn_enable_interrupts(struct athn_softc *);
105 Static void athn_get_chanlist(struct athn_softc *);
106 Static void athn_get_chipid(struct athn_softc *);
107 Static void athn_init_dma(struct athn_softc *);
108 Static void athn_init_qos(struct athn_softc *);
109 Static void athn_init_tx_queues(struct athn_softc *);
110 Static void athn_iter_func(void *, struct ieee80211_node *);
111 Static void athn_newassoc(struct ieee80211_node *, int);
112 Static void athn_next_scan(void *);
113 Static void athn_radiotap_attach(struct athn_softc *);
114 Static void athn_start(struct ifnet *);
115 Static void athn_tx_reclaim(struct athn_softc *, int);
116 Static void athn_watchdog(struct ifnet *);
117 Static void athn_write_serdes(struct athn_softc *,
118 const struct athn_serdes *);
119
120 #ifdef ATHN_BT_COEXISTENCE
121 Static void athn_btcoex_enable(struct athn_softc *);
122 Static void athn_btcoex_disable(struct athn_softc *);
123 #endif
124
125 #ifdef unused
126 Static int32_t athn_ani_get_rssi(struct athn_softc *);
127 Static int athn_rx_abort(struct athn_softc *);
128 #endif
129
130 #ifdef notyet
131 Static void athn_ani_monitor(struct athn_softc *);
132 Static void athn_ani_ofdm_err_trigger(struct athn_softc *);
133 Static void athn_ani_cck_err_trigger(struct athn_softc *);
134 Static void athn_ani_lower_immunity(struct athn_softc *);
135 Static void athn_ani_restart(struct athn_softc *);
136 Static void athn_set_multi(struct athn_softc *);
137 #endif /* notyet */
138
139 PUBLIC int
140 athn_attach(struct athn_softc *sc)
141 {
142 struct ieee80211com *ic = &sc->sc_ic;
143 struct ifnet *ifp = &sc->sc_if;
144 size_t max_nnodes;
145 int error;
146
147 /* Read hardware revision. */
148 athn_get_chipid(sc);
149
150 if ((error = athn_reset_power_on(sc)) != 0) {
151 aprint_error_dev(sc->sc_dev, "could not reset chip\n");
152 return error;
153 }
154
155 if ((error = athn_set_power_awake(sc)) != 0) {
156 aprint_error_dev(sc->sc_dev, "could not wakeup chip\n");
157 return error;
158 }
159
160 if (AR_SREV_5416(sc) || AR_SREV_9160(sc))
161 error = ar5416_attach(sc);
162 else if (AR_SREV_9280(sc))
163 error = ar9280_attach(sc);
164 else if (AR_SREV_9285(sc))
165 error = ar9285_attach(sc);
166 #if NATHN_USB > 0
167 else if (AR_SREV_9271(sc))
168 error = ar9285_attach(sc);
169 #endif
170 else if (AR_SREV_9287(sc))
171 error = ar9287_attach(sc);
172 else if (AR_SREV_9380(sc) || AR_SREV_9485(sc))
173 error = ar9380_attach(sc);
174 else
175 error = ENOTSUP;
176 if (error != 0) {
177 aprint_error_dev(sc->sc_dev, "could not attach chip\n");
178 return error;
179 }
180
181 pmf_self_suspensor_init(sc->sc_dev, &sc->sc_suspensor, &sc->sc_qual);
182
183 /* We can put the chip in sleep state now. */
184 athn_set_power_sleep(sc);
185
186 if (!(sc->sc_flags & ATHN_FLAG_USB)) {
187 error = sc->sc_ops.dma_alloc(sc);
188 if (error != 0) {
189 aprint_error_dev(sc->sc_dev,
190 "could not allocate DMA resources\n");
191 return error;
192 }
193 /* Steal one Tx buffer for beacons. */
194 sc->sc_bcnbuf = SIMPLEQ_FIRST(&sc->sc_txbufs);
195 SIMPLEQ_REMOVE_HEAD(&sc->sc_txbufs, bf_list);
196 }
197
198 if (sc->sc_flags & ATHN_FLAG_RFSILENT) {
199 DPRINTFN(DBG_INIT, sc,
200 "found RF switch connected to GPIO pin %d\n",
201 sc->sc_rfsilent_pin);
202 }
203 DPRINTFN(DBG_INIT, sc, "%zd key cache entries\n", sc->sc_kc_entries);
204
205 /*
206 * In HostAP mode, the number of STAs that we can handle is
207 * limited by the number of entries in the HW key cache.
208 * TKIP keys consume 2 entries in the cache.
209 */
210 KASSERT(sc->sc_kc_entries / 2 > IEEE80211_WEP_NKID);
211 max_nnodes = (sc->sc_kc_entries / 2) - IEEE80211_WEP_NKID;
212 if (sc->sc_max_aid != 0) /* we have an override */
213 ic->ic_max_aid = sc->sc_max_aid;
214 if (ic->ic_max_aid > max_nnodes)
215 ic->ic_max_aid = max_nnodes;
216
217 DPRINTFN(DBG_INIT, sc, "using %s loop power control\n",
218 (sc->sc_flags & ATHN_FLAG_OLPC) ? "open" : "closed");
219 DPRINTFN(DBG_INIT, sc, "txchainmask=0x%x rxchainmask=0x%x\n",
220 sc->sc_txchainmask, sc->sc_rxchainmask);
221
222 /* Count the number of bits set (in lowest 3 bits). */
223 sc->sc_ntxchains =
224 ((sc->sc_txchainmask >> 2) & 1) +
225 ((sc->sc_txchainmask >> 1) & 1) +
226 ((sc->sc_txchainmask >> 0) & 1);
227 sc->sc_nrxchains =
228 ((sc->sc_rxchainmask >> 2) & 1) +
229 ((sc->sc_rxchainmask >> 1) & 1) +
230 ((sc->sc_rxchainmask >> 0) & 1);
231
232 if (AR_SINGLE_CHIP(sc)) {
233 aprint_normal("%s\n", athn_get_mac_name(sc));
234 aprint_verbose_dev(sc->sc_dev,
235 "rev %d (%dT%dR), ROM rev %d, address %s\n",
236 sc->sc_mac_rev,
237 sc->sc_ntxchains, sc->sc_nrxchains, sc->sc_eep_rev,
238 ether_sprintf(ic->ic_myaddr));
239 }
240 else {
241 aprint_normal("%s, RF %s\n", athn_get_mac_name(sc),
242 athn_get_rf_name(sc));
243 aprint_verbose_dev(sc->sc_dev,
244 "rev %d (%dT%dR), ROM rev %d, address %s\n",
245 sc->sc_mac_rev,
246 sc->sc_ntxchains, sc->sc_nrxchains,
247 sc->sc_eep_rev, ether_sprintf(ic->ic_myaddr));
248 }
249
250 callout_init(&sc->sc_scan_to, 0);
251 callout_setfunc(&sc->sc_scan_to, athn_next_scan, sc);
252 callout_init(&sc->sc_calib_to, 0);
253 callout_setfunc(&sc->sc_calib_to, athn_calib_to, sc);
254
255 sc->sc_amrr.amrr_min_success_threshold = 1;
256 sc->sc_amrr.amrr_max_success_threshold = 15;
257
258 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
259 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
260 ic->ic_state = IEEE80211_S_INIT;
261
262 /* Set device capabilities. */
263 ic->ic_caps =
264 IEEE80211_C_WPA | /* 802.11i */
265 #ifndef IEEE80211_STA_ONLY
266 IEEE80211_C_HOSTAP | /* Host AP mode supported. */
267 // XXX? IEEE80211_C_APPMGT | /* Host AP power saving supported. */
268 #endif
269 IEEE80211_C_MONITOR | /* Monitor mode supported. */
270 IEEE80211_C_SHSLOT | /* Short slot time supported. */
271 IEEE80211_C_SHPREAMBLE | /* Short preamble supported. */
272 IEEE80211_C_PMGT; /* Power saving supported. */
273
274 #ifndef IEEE80211_NO_HT
275 if (sc->sc_flags & ATHN_FLAG_11N) {
276 int i, ntxstreams, nrxstreams;
277
278 /* Set HT capabilities. */
279 ic->ic_htcaps =
280 IEEE80211_HTCAP_SMPS_DIS |
281 IEEE80211_HTCAP_CBW20_40 |
282 IEEE80211_HTCAP_SGI40 |
283 IEEE80211_HTCAP_DSSSCCK40;
284 if (AR_SREV_9271(sc) || AR_SREV_9287_10_OR_LATER(sc))
285 ic->ic_htcaps |= IEEE80211_HTCAP_SGI20;
286 if (AR_SREV_9380_10_OR_LATER(sc))
287 ic->ic_htcaps |= IEEE80211_HTCAP_LDPC;
288 if (AR_SREV_9280_10_OR_LATER(sc)) {
289 ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
290 ic->ic_htcaps |= 1 << IEEE80211_HTCAP_RXSTBC_SHIFT;
291 }
292 ntxstreams = sc->sc_ntxchains;
293 nrxstreams = sc->sc_nrxchains;
294 if (!AR_SREV_9380_10_OR_LATER(sc)) {
295 ntxstreams = MIN(ntxstreams, 2);
296 nrxstreams = MIN(nrxstreams, 2);
297 }
298 /* Set supported HT rates. */
299 for (i = 0; i < nrxstreams; i++)
300 ic->ic_sup_mcs[i] = 0xff;
301 /* Set the "Tx MCS Set Defined" bit. */
302 ic->ic_sup_mcs[12] |= 0x01;
303 if (ntxstreams != nrxstreams) {
304 /* Set "Tx Rx MCS Set Not Equal" bit. */
305 ic->ic_sup_mcs[12] |= 0x02;
306 ic->ic_sup_mcs[12] |= (ntxstreams - 1) << 2;
307 }
308 }
309 #endif
310
311 /* Set supported rates. */
312 if (sc->sc_flags & ATHN_FLAG_11G) {
313 ic->ic_sup_rates[IEEE80211_MODE_11B] =
314 ieee80211_std_rateset_11b;
315 ic->ic_sup_rates[IEEE80211_MODE_11G] =
316 ieee80211_std_rateset_11g;
317 }
318 if (sc->sc_flags & ATHN_FLAG_11A) {
319 ic->ic_sup_rates[IEEE80211_MODE_11A] =
320 ieee80211_std_rateset_11a;
321 }
322
323 /* Get the list of authorized/supported channels. */
324 athn_get_chanlist(sc);
325
326 ifp->if_softc = sc;
327 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
328 ifp->if_init = athn_init;
329 ifp->if_ioctl = athn_ioctl;
330 ifp->if_start = athn_start;
331 ifp->if_watchdog = athn_watchdog;
332 IFQ_SET_READY(&ifp->if_snd);
333 memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
334
335 if_attach(ifp);
336 ieee80211_ifattach(ic);
337
338 ic->ic_node_alloc = athn_node_alloc;
339 ic->ic_newassoc = athn_newassoc;
340 if (ic->ic_updateslot == NULL)
341 ic->ic_updateslot = athn_updateslot;
342 #ifdef notyet_edca
343 ic->ic_updateedca = athn_updateedca;
344 #endif
345 #ifdef notyet
346 ic->ic_set_key = athn_set_key;
347 ic->ic_delete_key = athn_delete_key;
348 #endif
349
350 /* Override 802.11 state transition machine. */
351 sc->sc_newstate = ic->ic_newstate;
352 ic->ic_newstate = athn_newstate;
353
354 if (sc->sc_media_change == NULL)
355 sc->sc_media_change = athn_media_change;
356 ieee80211_media_init(ic, sc->sc_media_change, ieee80211_media_status);
357
358 athn_radiotap_attach(sc);
359 return 0;
360 }
361
362 PUBLIC void
363 athn_detach(struct athn_softc *sc)
364 {
365 struct ifnet *ifp = &sc->sc_if;
366 int qid;
367
368 callout_halt(&sc->sc_scan_to, NULL);
369 callout_halt(&sc->sc_calib_to, NULL);
370
371 if (!(sc->sc_flags & ATHN_FLAG_USB)) {
372 for (qid = 0; qid < ATHN_QID_COUNT; qid++)
373 athn_tx_reclaim(sc, qid);
374
375 /* Free Tx/Rx DMA resources. */
376 sc->sc_ops.dma_free(sc);
377 }
378 /* Free ROM copy. */
379 if (sc->sc_eep != NULL) {
380 free(sc->sc_eep, M_DEVBUF);
381 sc->sc_eep = NULL;
382 }
383
384 bpf_detach(ifp);
385 ieee80211_ifdetach(&sc->sc_ic);
386 if_detach(ifp);
387
388 callout_destroy(&sc->sc_scan_to);
389 callout_destroy(&sc->sc_calib_to);
390 }
391
392 /*
393 * Attach the interface to 802.11 radiotap.
394 */
395 Static void
396 athn_radiotap_attach(struct athn_softc *sc)
397 {
398
399 bpf_attach2(&sc->sc_if, DLT_IEEE802_11_RADIO,
400 sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
401 &sc->sc_drvbpf);
402
403 sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
404 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
405 sc->sc_rxtap.wr_ihdr.it_present = htole32(ATHN_RX_RADIOTAP_PRESENT);
406
407 sc->sc_txtap_len = sizeof(sc->sc_txtapu);
408 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
409 sc->sc_txtap.wt_ihdr.it_present = htole32(ATHN_TX_RADIOTAP_PRESENT);
410 }
411
412 Static void
413 athn_get_chanlist(struct athn_softc *sc)
414 {
415 struct ieee80211com *ic = &sc->sc_ic;
416 uint8_t chan;
417 size_t i;
418
419 if (sc->sc_flags & ATHN_FLAG_11G) {
420 for (i = 1; i <= 14; i++) {
421 chan = i;
422 ic->ic_channels[chan].ic_freq =
423 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ);
424 ic->ic_channels[chan].ic_flags =
425 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
426 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
427 }
428 }
429 if (sc->sc_flags & ATHN_FLAG_11A) {
430 for (i = 0; i < __arraycount(athn_5ghz_chans); i++) {
431 chan = athn_5ghz_chans[i];
432 ic->ic_channels[chan].ic_freq =
433 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ);
434 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A;
435 }
436 }
437 }
438
439 PUBLIC void
440 athn_rx_start(struct athn_softc *sc)
441 {
442 struct ieee80211com *ic = &sc->sc_ic;
443 uint32_t rfilt;
444
445 /* Setup Rx DMA descriptors. */
446 sc->sc_ops.rx_enable(sc);
447
448 /* Set Rx filter. */
449 rfilt = AR_RX_FILTER_UCAST | AR_RX_FILTER_BCAST | AR_RX_FILTER_MCAST;
450 #ifndef IEEE80211_NO_HT
451 /* Want Compressed Block Ack Requests. */
452 rfilt |= AR_RX_FILTER_COMPR_BAR;
453 #endif
454 rfilt |= AR_RX_FILTER_BEACON;
455 if (ic->ic_opmode != IEEE80211_M_STA) {
456 rfilt |= AR_RX_FILTER_PROBEREQ;
457 if (ic->ic_opmode == IEEE80211_M_MONITOR)
458 rfilt |= AR_RX_FILTER_PROM;
459 #ifndef IEEE80211_STA_ONLY
460 if (AR_SREV_9280_10_OR_LATER(sc) &&
461 ic->ic_opmode == IEEE80211_M_HOSTAP)
462 rfilt |= AR_RX_FILTER_PSPOLL;
463 #endif
464 }
465 athn_set_rxfilter(sc, rfilt);
466
467 /* Set BSSID mask. */
468 AR_WRITE(sc, AR_BSSMSKL, 0xffffffff);
469 AR_WRITE(sc, AR_BSSMSKU, 0xffff);
470
471 athn_set_opmode(sc);
472
473 /* Set multicast filter. */
474 AR_WRITE(sc, AR_MCAST_FIL0, 0xffffffff);
475 AR_WRITE(sc, AR_MCAST_FIL1, 0xffffffff);
476
477 AR_WRITE(sc, AR_FILT_OFDM, 0);
478 AR_WRITE(sc, AR_FILT_CCK, 0);
479 AR_WRITE(sc, AR_MIBC, 0);
480 AR_WRITE(sc, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
481 AR_WRITE(sc, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
482
483 /* XXX ANI. */
484 AR_WRITE(sc, AR_PHY_ERR_1, 0);
485 AR_WRITE(sc, AR_PHY_ERR_2, 0);
486
487 /* Disable HW crypto for now. */
488 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_ENCRYPT_DIS | AR_DIAG_DECRYPT_DIS);
489
490 /* Start PCU Rx. */
491 AR_CLRBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
492 AR_WRITE_BARRIER(sc);
493 }
494
495 PUBLIC void
496 athn_set_rxfilter(struct athn_softc *sc, uint32_t rfilt)
497 {
498
499 AR_WRITE(sc, AR_RX_FILTER, rfilt);
500 #ifdef notyet
501 reg = AR_READ(sc, AR_PHY_ERR);
502 reg &= (AR_PHY_ERR_RADAR | AR_PHY_ERR_OFDM_TIMING |
503 AR_PHY_ERR_CCK_TIMING);
504 AR_WRITE(sc, AR_PHY_ERR, reg);
505 if (reg != 0)
506 AR_SETBITS(sc, AR_RXCFG, AR_RXCFG_ZLFDMA);
507 else
508 AR_CLRBITS(sc, AR_RXCFG, AR_RXCFG_ZLFDMA);
509 #else
510 AR_WRITE(sc, AR_PHY_ERR, 0);
511 AR_CLRBITS(sc, AR_RXCFG, AR_RXCFG_ZLFDMA);
512 #endif
513 AR_WRITE_BARRIER(sc);
514 }
515
516 PUBLIC int
517 athn_intr(void *xsc)
518 {
519 struct athn_softc *sc = xsc;
520 struct ifnet *ifp = &sc->sc_if;
521
522 if (!IS_UP_AND_RUNNING(ifp))
523 return 0;
524
525 return sc->sc_ops.intr(sc);
526 }
527
528 Static void
529 athn_get_chipid(struct athn_softc *sc)
530 {
531 uint32_t reg;
532
533 reg = AR_READ(sc, AR_SREV);
534 if (MS(reg, AR_SREV_ID) == 0xff) {
535 sc->sc_mac_ver = MS(reg, AR_SREV_VERSION2);
536 sc->sc_mac_rev = MS(reg, AR_SREV_REVISION2);
537 if (!(reg & AR_SREV_TYPE2_HOST_MODE))
538 sc->sc_flags |= ATHN_FLAG_PCIE;
539 }
540 else {
541 sc->sc_mac_ver = MS(reg, AR_SREV_VERSION);
542 sc->sc_mac_rev = MS(reg, AR_SREV_REVISION);
543 if (sc->sc_mac_ver == AR_SREV_VERSION_5416_PCIE)
544 sc->sc_flags |= ATHN_FLAG_PCIE;
545 }
546 }
547
548 Static const char *
549 athn_get_mac_name(struct athn_softc *sc)
550 {
551
552 switch (sc->sc_mac_ver) {
553 case AR_SREV_VERSION_5416_PCI:
554 return "AR5416";
555 case AR_SREV_VERSION_5416_PCIE:
556 return "AR5418";
557 case AR_SREV_VERSION_9160:
558 return "AR9160";
559 case AR_SREV_VERSION_9280:
560 return "AR9280";
561 case AR_SREV_VERSION_9285:
562 return "AR9285";
563 case AR_SREV_VERSION_9271:
564 return "AR9271";
565 case AR_SREV_VERSION_9287:
566 return "AR9287";
567 case AR_SREV_VERSION_9380:
568 return "AR9380";
569 case AR_SREV_VERSION_9485:
570 return "AR9485";
571 default:
572 return "unknown";
573 }
574 }
575
576 /*
577 * Return RF chip name (not for single-chip solutions).
578 */
579 Static const char *
580 athn_get_rf_name(struct athn_softc *sc)
581 {
582
583 KASSERT(!AR_SINGLE_CHIP(sc));
584
585 switch (sc->sc_rf_rev) {
586 case AR_RAD5133_SREV_MAJOR: /* Dual-band 3T3R. */
587 return "AR5133";
588 case AR_RAD2133_SREV_MAJOR: /* Single-band 3T3R. */
589 return "AR2133";
590 case AR_RAD5122_SREV_MAJOR: /* Dual-band 2T2R. */
591 return "AR5122";
592 case AR_RAD2122_SREV_MAJOR: /* Single-band 2T2R. */
593 return "AR2122";
594 default:
595 return "unknown";
596 }
597 }
598
599 PUBLIC int
600 athn_reset_power_on(struct athn_softc *sc)
601 {
602 int ntries;
603
604 /* Set force wake. */
605 AR_WRITE(sc, AR_RTC_FORCE_WAKE,
606 AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
607
608 if (!AR_SREV_9380_10_OR_LATER(sc)) {
609 /* Make sure no DMA is active by doing an AHB reset. */
610 AR_WRITE(sc, AR_RC, AR_RC_AHB);
611 }
612 /* RTC reset and clear. */
613 AR_WRITE(sc, AR_RTC_RESET, 0);
614 AR_WRITE_BARRIER(sc);
615 DELAY(2);
616 if (!AR_SREV_9380_10_OR_LATER(sc))
617 AR_WRITE(sc, AR_RC, 0);
618 AR_WRITE(sc, AR_RTC_RESET, 1);
619
620 /* Poll until RTC is ON. */
621 for (ntries = 0; ntries < 1000; ntries++) {
622 if ((AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) ==
623 AR_RTC_STATUS_ON)
624 break;
625 DELAY(10);
626 }
627 if (ntries == 1000) {
628 DPRINTFN(DBG_INIT, sc, "RTC not waking up\n");
629 return ETIMEDOUT;
630 }
631 return athn_reset(sc, 0);
632 }
633
634 PUBLIC int
635 athn_reset(struct athn_softc *sc, int cold_reset)
636 {
637 int ntries;
638
639 /* Set force wake. */
640 AR_WRITE(sc, AR_RTC_FORCE_WAKE,
641 AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
642
643 if (AR_READ(sc, AR_INTR_SYNC_CAUSE) &
644 (AR_INTR_SYNC_LOCAL_TIMEOUT | AR_INTR_SYNC_RADM_CPL_TIMEOUT)) {
645 AR_WRITE(sc, AR_INTR_SYNC_ENABLE, 0);
646 AR_WRITE(sc, AR_RC, AR_RC_HOSTIF |
647 (!AR_SREV_9380_10_OR_LATER(sc) ? AR_RC_AHB : 0));
648 }
649 else if (!AR_SREV_9380_10_OR_LATER(sc))
650 AR_WRITE(sc, AR_RC, AR_RC_AHB);
651
652 AR_WRITE(sc, AR_RTC_RC, AR_RTC_RC_MAC_WARM |
653 (cold_reset ? AR_RTC_RC_MAC_COLD : 0));
654 AR_WRITE_BARRIER(sc);
655 DELAY(50);
656 AR_WRITE(sc, AR_RTC_RC, 0);
657 for (ntries = 0; ntries < 1000; ntries++) {
658 if (!(AR_READ(sc, AR_RTC_RC) &
659 (AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD)))
660 break;
661 DELAY(10);
662 }
663 if (ntries == 1000) {
664 DPRINTFN(DBG_INIT, sc, "RTC stuck in MAC reset\n");
665 return ETIMEDOUT;
666 }
667 AR_WRITE(sc, AR_RC, 0);
668 AR_WRITE_BARRIER(sc);
669 return 0;
670 }
671
672 PUBLIC int
673 athn_set_power_awake(struct athn_softc *sc)
674 {
675 int ntries, error;
676
677 /* Do a Power-On-Reset if shutdown. */
678 if ((AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) ==
679 AR_RTC_STATUS_SHUTDOWN) {
680 if ((error = athn_reset_power_on(sc)) != 0)
681 return error;
682 if (!AR_SREV_9380_10_OR_LATER(sc))
683 athn_init_pll(sc, NULL);
684 }
685 AR_SETBITS(sc, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
686 AR_WRITE_BARRIER(sc);
687 DELAY(50); /* Give chip the chance to awake. */
688
689 /* Poll until RTC is ON. */
690 for (ntries = 0; ntries < 4000; ntries++) {
691 if ((AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) ==
692 AR_RTC_STATUS_ON)
693 break;
694 DELAY(50);
695 AR_SETBITS(sc, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
696 }
697 if (ntries == 4000) {
698 DPRINTFN(DBG_INIT, sc, "RTC not waking up\n");
699 return ETIMEDOUT;
700 }
701
702 AR_CLRBITS(sc, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
703 AR_WRITE_BARRIER(sc);
704 return 0;
705 }
706
707 PUBLIC void
708 athn_set_power_sleep(struct athn_softc *sc)
709 {
710
711 AR_SETBITS(sc, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
712 /* Allow the MAC to go to sleep. */
713 AR_CLRBITS(sc, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
714 if (!AR_SREV_9380_10_OR_LATER(sc))
715 AR_WRITE(sc, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
716 /*
717 * NB: Clearing RTC_RESET_EN when setting the chip to sleep mode
718 * results in high power consumption on AR5416 chipsets.
719 */
720 if (!AR_SREV_5416(sc) && !AR_SREV_9271(sc))
721 AR_CLRBITS(sc, AR_RTC_RESET, AR_RTC_RESET_EN);
722 AR_WRITE_BARRIER(sc);
723 }
724
725 PUBLIC void
726 athn_init_pll(struct athn_softc *sc, const struct ieee80211_channel *c)
727 {
728 uint32_t pll;
729
730 if (AR_SREV_9380_10_OR_LATER(sc)) {
731 if (AR_SREV_9485(sc))
732 AR_WRITE(sc, AR_RTC_PLL_CONTROL2, 0x886666);
733 pll = SM(AR_RTC_9160_PLL_REFDIV, 0x5);
734 pll |= SM(AR_RTC_9160_PLL_DIV, 0x2c);
735 }
736 else if (AR_SREV_9280_10_OR_LATER(sc)) {
737 pll = SM(AR_RTC_9160_PLL_REFDIV, 0x05);
738 if (c != NULL && IEEE80211_IS_CHAN_5GHZ(c)) {
739 if (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)
740 pll = 0x142c;
741 else if (AR_SREV_9280_20(sc))
742 pll = 0x2850;
743 else
744 pll |= SM(AR_RTC_9160_PLL_DIV, 0x28);
745 }
746 else
747 pll |= SM(AR_RTC_9160_PLL_DIV, 0x2c);
748 }
749 else if (AR_SREV_9160_10_OR_LATER(sc)) {
750 pll = SM(AR_RTC_9160_PLL_REFDIV, 0x05);
751 if (c != NULL && IEEE80211_IS_CHAN_5GHZ(c))
752 pll |= SM(AR_RTC_9160_PLL_DIV, 0x50);
753 else
754 pll |= SM(AR_RTC_9160_PLL_DIV, 0x58);
755 }
756 else {
757 pll = AR_RTC_PLL_REFDIV_5 | AR_RTC_PLL_DIV2;
758 if (c != NULL && IEEE80211_IS_CHAN_5GHZ(c))
759 pll |= SM(AR_RTC_PLL_DIV, 0x0a);
760 else
761 pll |= SM(AR_RTC_PLL_DIV, 0x0b);
762 }
763 DPRINTFN(DBG_INIT, sc, "AR_RTC_PLL_CONTROL=0x%08x\n", pll);
764 AR_WRITE(sc, AR_RTC_PLL_CONTROL, pll);
765 if (AR_SREV_9271(sc)) {
766 /* Switch core clock to 117MHz. */
767 AR_WRITE_BARRIER(sc);
768 DELAY(500);
769 AR_WRITE(sc, 0x50050, 0x304);
770 }
771 AR_WRITE_BARRIER(sc);
772 DELAY(100);
773 AR_WRITE(sc, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
774 AR_WRITE_BARRIER(sc);
775 }
776
777 Static void
778 athn_write_serdes(struct athn_softc *sc, const struct athn_serdes *serdes)
779 {
780 int i;
781
782 /* Write sequence to Serializer/Deserializer. */
783 for (i = 0; i < serdes->nvals; i++)
784 AR_WRITE(sc, serdes->regs[i], serdes->vals[i]);
785 AR_WRITE_BARRIER(sc);
786 }
787
788 PUBLIC void
789 athn_config_pcie(struct athn_softc *sc)
790 {
791
792 /* Disable PLL when in L0s as well as receiver clock when in L1. */
793 athn_write_serdes(sc, sc->sc_serdes);
794
795 DELAY(1000);
796 /* Allow forcing of PCIe core into L1 state. */
797 AR_SETBITS(sc, AR_PCIE_PM_CTRL, AR_PCIE_PM_CTRL_ENA);
798
799 #ifndef ATHN_PCIE_WAEN
800 AR_WRITE(sc, AR_WA, sc->sc_workaround);
801 #else
802 AR_WRITE(sc, AR_WA, ATHN_PCIE_WAEN);
803 #endif
804 AR_WRITE_BARRIER(sc);
805 }
806
807 /*
808 * Serializer/Deserializer programming for non-PCIe devices.
809 */
810 static const uint32_t ar_nonpcie_serdes_regs[] = {
811 AR_PCIE_SERDES,
812 AR_PCIE_SERDES,
813 AR_PCIE_SERDES,
814 AR_PCIE_SERDES,
815 AR_PCIE_SERDES,
816 AR_PCIE_SERDES,
817 AR_PCIE_SERDES,
818 AR_PCIE_SERDES,
819 AR_PCIE_SERDES,
820 AR_PCIE_SERDES2,
821 };
822
823 static const uint32_t ar_nonpcie_serdes_vals[] = {
824 0x9248fc00,
825 0x24924924,
826 0x28000029,
827 0x57160824,
828 0x25980579,
829 0x00000000,
830 0x1aaabe40,
831 0xbe105554,
832 0x000e1007,
833 0x00000000
834 };
835
836 static const struct athn_serdes ar_nonpcie_serdes = {
837 __arraycount(ar_nonpcie_serdes_vals),
838 ar_nonpcie_serdes_regs,
839 ar_nonpcie_serdes_vals
840 };
841
842 PUBLIC void
843 athn_config_nonpcie(struct athn_softc *sc)
844 {
845
846 athn_write_serdes(sc, &ar_nonpcie_serdes);
847 }
848
849 PUBLIC int
850 athn_set_chan(struct athn_softc *sc, struct ieee80211_channel *curchan,
851 struct ieee80211_channel *extchan)
852 {
853 struct athn_ops *ops = &sc->sc_ops;
854 int error, qid;
855
856 /* Check that Tx is stopped, otherwise RF Bus grant will not work. */
857 for (qid = 0; qid < ATHN_QID_COUNT; qid++)
858 if (athn_tx_pending(sc, qid))
859 return EBUSY;
860
861 /* Request RF Bus grant. */
862 if ((error = ops->rf_bus_request(sc)) != 0)
863 return error;
864
865 ops->set_phy(sc, curchan, extchan);
866
867 /* Change the synthesizer. */
868 if ((error = ops->set_synth(sc, curchan, extchan)) != 0)
869 return error;
870
871 sc->sc_curchan = curchan;
872 sc->sc_curchanext = extchan;
873
874 /* Set transmit power values for new channel. */
875 ops->set_txpower(sc, curchan, extchan);
876
877 /* Release the RF Bus grant. */
878 ops->rf_bus_release(sc);
879
880 /* Write delta slope coeffs for modes where OFDM may be used. */
881 if (sc->sc_ic.ic_curmode != IEEE80211_MODE_11B)
882 ops->set_delta_slope(sc, curchan, extchan);
883
884 ops->spur_mitigate(sc, curchan, extchan);
885 /* XXX Load noisefloor values and start calibration. */
886
887 return 0;
888 }
889
890 Static int
891 athn_switch_chan(struct athn_softc *sc, struct ieee80211_channel *curchan,
892 struct ieee80211_channel *extchan)
893 {
894 int error, qid;
895
896 /* Disable interrupts. */
897 athn_disable_interrupts(sc);
898
899 /* Stop all Tx queues. */
900 for (qid = 0; qid < ATHN_QID_COUNT; qid++)
901 athn_stop_tx_dma(sc, qid);
902 for (qid = 0; qid < ATHN_QID_COUNT; qid++)
903 athn_tx_reclaim(sc, qid);
904
905 /* Stop Rx. */
906 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
907 AR_WRITE(sc, AR_MIBC, AR_MIBC_FMC);
908 AR_WRITE(sc, AR_MIBC, AR_MIBC_CMC);
909 AR_WRITE(sc, AR_FILT_OFDM, 0);
910 AR_WRITE(sc, AR_FILT_CCK, 0);
911 athn_set_rxfilter(sc, 0);
912 error = athn_stop_rx_dma(sc);
913 if (error != 0)
914 goto reset;
915
916 #ifdef notyet
917 /* AR9280 needs a full reset. */
918 if (AR_SREV_9280(sc))
919 #endif
920 goto reset;
921
922 /* If band or bandwidth changes, we need to do a full reset. */
923 if (curchan->ic_flags != sc->sc_curchan->ic_flags ||
924 ((extchan != NULL) ^ (sc->sc_curchanext != NULL))) {
925 DPRINTFN(DBG_RF, sc, "channel band switch\n");
926 goto reset;
927 }
928 error = athn_set_power_awake(sc);
929 if (error != 0)
930 goto reset;
931
932 error = athn_set_chan(sc, curchan, extchan);
933 if (error != 0) {
934 reset: /* Error found, try a full reset. */
935 DPRINTFN(DBG_RF, sc, "needs a full reset\n");
936 error = athn_hw_reset(sc, curchan, extchan, 0);
937 if (error != 0) /* Hopeless case. */
938 return error;
939 }
940 athn_rx_start(sc);
941
942 /* Re-enable interrupts. */
943 athn_enable_interrupts(sc);
944 return 0;
945 }
946
947 PUBLIC void
948 athn_get_delta_slope(uint32_t coeff, uint32_t *exponent, uint32_t *mantissa)
949 {
950 #define COEFF_SCALE_SHIFT 24
951 uint32_t exp, man;
952
953 /* exponent = 14 - floor(log2(coeff)) */
954 for (exp = 31; exp > 0; exp--)
955 if (coeff & (1 << exp))
956 break;
957 exp = 14 - (exp - COEFF_SCALE_SHIFT);
958
959 /* mantissa = floor(coeff * 2^exponent + 0.5) */
960 man = coeff + (1 << (COEFF_SCALE_SHIFT - exp - 1));
961
962 *mantissa = man >> (COEFF_SCALE_SHIFT - exp);
963 *exponent = exp - 16;
964 #undef COEFF_SCALE_SHIFT
965 }
966
967 PUBLIC void
968 athn_reset_key(struct athn_softc *sc, int entry)
969 {
970
971 /*
972 * NB: Key cache registers access special memory area that requires
973 * two 32-bit writes to actually update the values in the internal
974 * memory. Consequently, writes must be grouped by pair.
975 */
976 AR_WRITE(sc, AR_KEYTABLE_KEY0(entry), 0);
977 AR_WRITE(sc, AR_KEYTABLE_KEY1(entry), 0);
978
979 AR_WRITE(sc, AR_KEYTABLE_KEY2(entry), 0);
980 AR_WRITE(sc, AR_KEYTABLE_KEY3(entry), 0);
981
982 AR_WRITE(sc, AR_KEYTABLE_KEY4(entry), 0);
983 AR_WRITE(sc, AR_KEYTABLE_TYPE(entry), AR_KEYTABLE_TYPE_CLR);
984
985 AR_WRITE(sc, AR_KEYTABLE_MAC0(entry), 0);
986 AR_WRITE(sc, AR_KEYTABLE_MAC1(entry), 0);
987
988 AR_WRITE_BARRIER(sc);
989 }
990
991 #ifdef notyet
992 Static int
993 athn_set_key(struct ieee80211com *ic, struct ieee80211_node *ni,
994 struct ieee80211_key *k)
995 {
996 struct athn_softc *sc = ic->ic_ifp->if_softc;
997 const uint8_t *txmic, *rxmic, *key, *addr;
998 uintptr_t entry, micentry;
999 uint32_t type, lo, hi;
1000
1001 switch (k->k_cipher) {
1002 case IEEE80211_CIPHER_WEP40:
1003 type = AR_KEYTABLE_TYPE_40;
1004 break;
1005 case IEEE80211_CIPHER_WEP104:
1006 type = AR_KEYTABLE_TYPE_104;
1007 break;
1008 case IEEE80211_CIPHER_TKIP:
1009 type = AR_KEYTABLE_TYPE_TKIP;
1010 break;
1011 case IEEE80211_CIPHER_CCMP:
1012 type = AR_KEYTABLE_TYPE_CCM;
1013 break;
1014 default:
1015 /* Fallback to software crypto for other ciphers. */
1016 return ieee80211_set_key(ic, ni, k);
1017 }
1018
1019 if (!(k->k_flags & IEEE80211_KEY_GROUP))
1020 entry = IEEE80211_WEP_NKID + IEEE80211_AID(ni->ni_associd);
1021 else
1022 entry = k->k_id;
1023 k->k_priv = (void *)entry;
1024
1025 /* NB: See note about key cache registers access above. */
1026 key = k->k_key;
1027 if (type == AR_KEYTABLE_TYPE_TKIP) {
1028 #ifndef IEEE80211_STA_ONLY
1029 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1030 txmic = &key[16];
1031 rxmic = &key[24];
1032 }
1033 else
1034 #endif
1035 {
1036 rxmic = &key[16];
1037 txmic = &key[24];
1038 }
1039 /* Tx+Rx MIC key is at entry + 64. */
1040 micentry = entry + 64;
1041 AR_WRITE(sc, AR_KEYTABLE_KEY0(micentry), LE_READ_4(&rxmic[0]));
1042 AR_WRITE(sc, AR_KEYTABLE_KEY1(micentry), LE_READ_2(&txmic[2]));
1043
1044 AR_WRITE(sc, AR_KEYTABLE_KEY2(micentry), LE_READ_4(&rxmic[4]));
1045 AR_WRITE(sc, AR_KEYTABLE_KEY3(micentry), LE_READ_2(&txmic[0]));
1046
1047 AR_WRITE(sc, AR_KEYTABLE_KEY4(micentry), LE_READ_4(&txmic[4]));
1048 AR_WRITE(sc, AR_KEYTABLE_TYPE(micentry), AR_KEYTABLE_TYPE_CLR);
1049 }
1050 AR_WRITE(sc, AR_KEYTABLE_KEY0(entry), LE_READ_4(&key[ 0]));
1051 AR_WRITE(sc, AR_KEYTABLE_KEY1(entry), LE_READ_2(&key[ 4]));
1052
1053 AR_WRITE(sc, AR_KEYTABLE_KEY2(entry), LE_READ_4(&key[ 6]));
1054 AR_WRITE(sc, AR_KEYTABLE_KEY3(entry), LE_READ_2(&key[10]));
1055
1056 AR_WRITE(sc, AR_KEYTABLE_KEY4(entry), LE_READ_4(&key[12]));
1057 AR_WRITE(sc, AR_KEYTABLE_TYPE(entry), type);
1058
1059 if (!(k->k_flags & IEEE80211_KEY_GROUP)) {
1060 addr = ni->ni_macaddr;
1061 lo = LE_READ_4(&addr[0]);
1062 hi = LE_READ_2(&addr[4]);
1063 lo = lo >> 1 | hi << 31;
1064 hi = hi >> 1;
1065 }
1066 else
1067 lo = hi = 0;
1068 AR_WRITE(sc, AR_KEYTABLE_MAC0(entry), lo);
1069 AR_WRITE(sc, AR_KEYTABLE_MAC1(entry), hi | AR_KEYTABLE_VALID);
1070 AR_WRITE_BARRIER(sc);
1071 return 0;
1072 }
1073
1074 Static void
1075 athn_delete_key(struct ieee80211com *ic, struct ieee80211_node *ni,
1076 struct ieee80211_key *k)
1077 {
1078 struct athn_softc *sc = ic->ic_ifp->if_softc;
1079 uintptr_t entry;
1080
1081 switch (k->k_cipher) {
1082 case IEEE80211_CIPHER_WEP40:
1083 case IEEE80211_CIPHER_WEP104:
1084 case IEEE80211_CIPHER_CCMP:
1085 entry = (uintptr_t)k->k_priv;
1086 athn_reset_key(sc, entry);
1087 break;
1088 case IEEE80211_CIPHER_TKIP:
1089 entry = (uintptr_t)k->k_priv;
1090 athn_reset_key(sc, entry);
1091 athn_reset_key(sc, entry + 64);
1092 break;
1093 default:
1094 /* Fallback to software crypto for other ciphers. */
1095 ieee80211_delete_key(ic, ni, k);
1096 }
1097 }
1098 #endif /* notyet */
1099
1100 PUBLIC void
1101 athn_led_init(struct athn_softc *sc)
1102 {
1103 struct athn_ops *ops = &sc->sc_ops;
1104
1105 ops->gpio_config_output(sc, sc->sc_led_pin, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1106 /* LED off, active low. */
1107 athn_set_led(sc, 0);
1108 }
1109
1110 PUBLIC void
1111 athn_set_led(struct athn_softc *sc, int on)
1112 {
1113 struct athn_ops *ops = &sc->sc_ops;
1114
1115 sc->sc_led_state = on;
1116 ops->gpio_write(sc, sc->sc_led_pin, !sc->sc_led_state);
1117 }
1118
1119 #ifdef ATHN_BT_COEXISTENCE
1120 Static void
1121 athn_btcoex_init(struct athn_softc *sc)
1122 {
1123 struct athn_ops *ops = &sc->sc_ops;
1124 uint32_t reg;
1125
1126 if (sc->sc_flags & ATHN_FLAG_BTCOEX2WIRE) {
1127 /* Connect bt_active to baseband. */
1128 AR_CLRBITS(sc, sc->sc_gpio_input_en_off,
1129 AR_GPIO_INPUT_EN_VAL_BT_PRIORITY_DEF |
1130 AR_GPIO_INPUT_EN_VAL_BT_FREQUENCY_DEF);
1131 AR_SETBITS(sc, sc->sc_gpio_input_en_off,
1132 AR_GPIO_INPUT_EN_VAL_BT_ACTIVE_BB);
1133
1134 reg = AR_READ(sc, AR_GPIO_INPUT_MUX1);
1135 reg = RW(reg, AR_GPIO_INPUT_MUX1_BT_ACTIVE,
1136 AR_GPIO_BTACTIVE_PIN);
1137 AR_WRITE(sc, AR_GPIO_INPUT_MUX1, reg);
1138 AR_WRITE_BARRIER(sc);
1139
1140 ops->gpio_config_input(sc, AR_GPIO_BTACTIVE_PIN);
1141 }
1142 else { /* 3-wire. */
1143 AR_SETBITS(sc, sc->sc_gpio_input_en_off,
1144 AR_GPIO_INPUT_EN_VAL_BT_PRIORITY_BB |
1145 AR_GPIO_INPUT_EN_VAL_BT_ACTIVE_BB);
1146
1147 reg = AR_READ(sc, AR_GPIO_INPUT_MUX1);
1148 reg = RW(reg, AR_GPIO_INPUT_MUX1_BT_ACTIVE,
1149 AR_GPIO_BTACTIVE_PIN);
1150 reg = RW(reg, AR_GPIO_INPUT_MUX1_BT_PRIORITY,
1151 AR_GPIO_BTPRIORITY_PIN);
1152 AR_WRITE(sc, AR_GPIO_INPUT_MUX1, reg);
1153 AR_WRITE_BARRIER(sc);
1154
1155 ops->gpio_config_input(sc, AR_GPIO_BTACTIVE_PIN);
1156 ops->gpio_config_input(sc, AR_GPIO_BTPRIORITY_PIN);
1157 }
1158 }
1159
1160 Static void
1161 athn_btcoex_enable(struct athn_softc *sc)
1162 {
1163 struct athn_ops *ops = &sc->sc_ops;
1164 uint32_t reg;
1165
1166 if (sc->sc_flags & ATHN_FLAG_BTCOEX3WIRE) {
1167 AR_WRITE(sc, AR_BT_COEX_MODE,
1168 SM(AR_BT_MODE, AR_BT_MODE_SLOTTED) |
1169 SM(AR_BT_PRIORITY_TIME, 2) |
1170 SM(AR_BT_FIRST_SLOT_TIME, 5) |
1171 SM(AR_BT_QCU_THRESH, ATHN_QID_AC_BE) |
1172 AR_BT_TXSTATE_EXTEND | AR_BT_TX_FRAME_EXTEND |
1173 AR_BT_QUIET | AR_BT_RX_CLEAR_POLARITY);
1174 AR_WRITE(sc, AR_BT_COEX_WEIGHT,
1175 SM(AR_BTCOEX_BT_WGHT, AR_STOMP_LOW_BT_WGHT) |
1176 SM(AR_BTCOEX_WL_WGHT, AR_STOMP_LOW_WL_WGHT));
1177 AR_WRITE(sc, AR_BT_COEX_MODE2,
1178 SM(AR_BT_BCN_MISS_THRESH, 50) |
1179 AR_BT_HOLD_RX_CLEAR | AR_BT_DISABLE_BT_ANT);
1180
1181 AR_SETBITS(sc, AR_QUIET1, AR_QUIET1_QUIET_ACK_CTS_ENABLE);
1182 AR_CLRBITS(sc, AR_PCU_MISC, AR_PCU_BT_ANT_PREVENT_RX);
1183 AR_WRITE_BARRIER(sc);
1184
1185 ops->gpio_config_output(sc, AR_GPIO_WLANACTIVE_PIN,
1186 AR_GPIO_OUTPUT_MUX_AS_RX_CLEAR_EXTERNAL);
1187
1188 }
1189 else { /* 2-wire. */
1190 ops->gpio_config_output(sc, AR_GPIO_WLANACTIVE_PIN,
1191 AR_GPIO_OUTPUT_MUX_AS_TX_FRAME);
1192 }
1193 reg = AR_READ(sc, AR_GPIO_PDPU);
1194 reg &= ~(0x3 << (AR_GPIO_WLANACTIVE_PIN * 2));
1195 reg |= 0x2 << (AR_GPIO_WLANACTIVE_PIN * 2);
1196 AR_WRITE(sc, AR_GPIO_PDPU, reg);
1197 AR_WRITE_BARRIER(sc);
1198
1199 /* Disable PCIe Active State Power Management (ASPM). */
1200 if (sc->sc_disable_aspm != NULL)
1201 sc->sc_disable_aspm(sc);
1202
1203 /* XXX Start periodic timer. */
1204 }
1205
1206 Static void
1207 athn_btcoex_disable(struct athn_softc *sc)
1208 {
1209 struct athn_ops *ops = &sc->sc_ops;
1210
1211 ops->gpio_write(sc, AR_GPIO_WLANACTIVE_PIN, 0);
1212
1213 ops->gpio_config_output(sc, AR_GPIO_WLANACTIVE_PIN,
1214 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1215
1216 if (sc->sc_flags & ATHN_FLAG_BTCOEX3WIRE) {
1217 AR_WRITE(sc, AR_BT_COEX_MODE,
1218 SM(AR_BT_MODE, AR_BT_MODE_DISABLED) | AR_BT_QUIET);
1219 AR_WRITE(sc, AR_BT_COEX_WEIGHT, 0);
1220 AR_WRITE(sc, AR_BT_COEX_MODE2, 0);
1221 /* XXX Stop periodic timer. */
1222 }
1223 AR_WRITE_BARRIER(sc);
1224 /* XXX Restore ASPM setting? */
1225 }
1226 #endif
1227
1228 Static void
1229 athn_iter_func(void *arg, struct ieee80211_node *ni)
1230 {
1231 struct athn_softc *sc = arg;
1232 struct athn_node *an = (struct athn_node *)ni;
1233
1234 ieee80211_amrr_choose(&sc->sc_amrr, ni, &an->amn);
1235 }
1236
1237 Static void
1238 athn_calib_to(void *arg)
1239 {
1240 extern int ticks;
1241 struct athn_softc *sc = arg;
1242 struct athn_ops *ops = &sc->sc_ops;
1243 struct ieee80211com *ic = &sc->sc_ic;
1244 int s;
1245
1246 s = splnet();
1247
1248 /* Do periodic (every 4 minutes) PA calibration. */
1249 if (AR_SREV_9285_11_OR_LATER(sc) &&
1250 !AR_SREV_9380_10_OR_LATER(sc) &&
1251 ticks >= sc->sc_pa_calib_ticks + 240 * hz) {
1252 sc->sc_pa_calib_ticks = ticks;
1253 if (AR_SREV_9271(sc))
1254 ar9271_pa_calib(sc);
1255 else
1256 ar9285_pa_calib(sc);
1257 }
1258
1259 /* Do periodic (every 30 seconds) temperature compensation. */
1260 if ((sc->sc_flags & ATHN_FLAG_OLPC) &&
1261 ticks >= sc->sc_olpc_ticks + 30 * hz) {
1262 sc->sc_olpc_ticks = ticks;
1263 ops->olpc_temp_compensation(sc);
1264 }
1265
1266 #ifdef notyet
1267 /* XXX ANI. */
1268 athn_ani_monitor(sc);
1269
1270 ops->next_calib(sc);
1271 #endif
1272 if (ic->ic_fixed_rate == -1) {
1273 if (ic->ic_opmode == IEEE80211_M_STA)
1274 athn_iter_func(sc, ic->ic_bss);
1275 else
1276 ieee80211_iterate_nodes(&ic->ic_sta, athn_iter_func, sc);
1277 }
1278 callout_schedule(&sc->sc_calib_to, hz / 2);
1279 splx(s);
1280 }
1281
1282 Static int
1283 athn_init_calib(struct athn_softc *sc, struct ieee80211_channel *curchan,
1284 struct ieee80211_channel *extchan)
1285 {
1286 struct athn_ops *ops = &sc->sc_ops;
1287 int error;
1288
1289 if (AR_SREV_9380_10_OR_LATER(sc))
1290 error = ar9003_init_calib(sc);
1291 else if (AR_SREV_9285_10_OR_LATER(sc))
1292 error = ar9285_init_calib(sc, curchan, extchan);
1293 else
1294 error = ar5416_init_calib(sc, curchan, extchan);
1295 if (error != 0)
1296 return error;
1297
1298 if (!AR_SREV_9380_10_OR_LATER(sc)) {
1299 /* Do PA calibration. */
1300 if (AR_SREV_9285_11_OR_LATER(sc)) {
1301 extern int ticks;
1302 sc->sc_pa_calib_ticks = ticks;
1303 if (AR_SREV_9271(sc))
1304 ar9271_pa_calib(sc);
1305 else
1306 ar9285_pa_calib(sc);
1307 }
1308 /* Do noisefloor calibration. */
1309 ops->noisefloor_calib(sc);
1310 }
1311 if (AR_SREV_9160_10_OR_LATER(sc)) {
1312 /* Support IQ calibration. */
1313 sc->sc_sup_calib_mask = ATHN_CAL_IQ;
1314 if (AR_SREV_9380_10_OR_LATER(sc)) {
1315 /* Support temperature compensation calibration. */
1316 sc->sc_sup_calib_mask |= ATHN_CAL_TEMP;
1317 }
1318 else if (IEEE80211_IS_CHAN_5GHZ(curchan) || extchan != NULL) {
1319 /*
1320 * ADC gain calibration causes uplink throughput
1321 * drops in HT40 mode on AR9287.
1322 */
1323 if (!AR_SREV_9287(sc)) {
1324 /* Support ADC gain calibration. */
1325 sc->sc_sup_calib_mask |= ATHN_CAL_ADC_GAIN;
1326 }
1327 /* Support ADC DC offset calibration. */
1328 sc->sc_sup_calib_mask |= ATHN_CAL_ADC_DC;
1329 }
1330 }
1331 return 0;
1332 }
1333
1334 /*
1335 * Adaptive noise immunity.
1336 */
1337 #ifdef notyet
1338 Static int32_t
1339 athn_ani_get_rssi(struct athn_softc *sc)
1340 {
1341
1342 return 0; /* XXX */
1343 }
1344 #endif /* notyet */
1345
1346 #ifdef notyet
1347 Static void
1348 athn_ani_ofdm_err_trigger(struct athn_softc *sc)
1349 {
1350 struct athn_ani *ani = &sc->sc_ani;
1351 struct athn_ops *ops = &sc->sc_ops;
1352 int32_t rssi;
1353
1354 /* First, raise noise immunity level, up to max. */
1355 if (ani->noise_immunity_level < 4) {
1356 ani->noise_immunity_level++;
1357 ops->set_noise_immunity_level(sc, ani->noise_immunity_level);
1358 return;
1359 }
1360
1361 /* Then, raise our spur immunity level, up to max. */
1362 if (ani->spur_immunity_level < 7) {
1363 ani->spur_immunity_level++;
1364 ops->set_spur_immunity_level(sc, ani->spur_immunity_level);
1365 return;
1366 }
1367
1368 #ifndef IEEE80211_STA_ONLY
1369 if (sc->sc_ic.ic_opmode == IEEE80211_M_HOSTAP) {
1370 if (ani->firstep_level < 2) {
1371 ani->firstep_level++;
1372 ops->set_firstep_level(sc, ani->firstep_level);
1373 }
1374 return;
1375 }
1376 #endif
1377 rssi = athn_ani_get_rssi(sc);
1378 if (rssi > ATHN_ANI_RSSI_THR_HIGH) {
1379 /*
1380 * Beacon RSSI is high, turn off OFDM weak signal detection
1381 * or raise first step level as last resort.
1382 */
1383 if (ani->ofdm_weak_signal) {
1384 ani->ofdm_weak_signal = 0;
1385 ops->disable_ofdm_weak_signal(sc);
1386 ani->spur_immunity_level = 0;
1387 ops->set_spur_immunity_level(sc, 0);
1388 }
1389 else if (ani->firstep_level < 2) {
1390 ani->firstep_level++;
1391 ops->set_firstep_level(sc, ani->firstep_level);
1392 }
1393 }
1394 else if (rssi > ATHN_ANI_RSSI_THR_LOW) {
1395 /*
1396 * Beacon RSSI is in mid range, we need OFDM weak signal
1397 * detection but we can raise first step level.
1398 */
1399 if (!ani->ofdm_weak_signal) {
1400 ani->ofdm_weak_signal = 1;
1401 ops->enable_ofdm_weak_signal(sc);
1402 }
1403 if (ani->firstep_level < 2) {
1404 ani->firstep_level++;
1405 ops->set_firstep_level(sc, ani->firstep_level);
1406 }
1407 }
1408 else if (sc->sc_ic.ic_curmode != IEEE80211_MODE_11A) {
1409 /*
1410 * Beacon RSSI is low, if in b/g mode, turn off OFDM weak
1411 * signal detection and zero first step level to maximize
1412 * CCK sensitivity.
1413 */
1414 if (ani->ofdm_weak_signal) {
1415 ani->ofdm_weak_signal = 0;
1416 ops->disable_ofdm_weak_signal(sc);
1417 }
1418 if (ani->firstep_level > 0) {
1419 ani->firstep_level = 0;
1420 ops->set_firstep_level(sc, 0);
1421 }
1422 }
1423 }
1424 #endif /* notyet */
1425
1426 #ifdef notyet
1427 Static void
1428 athn_ani_cck_err_trigger(struct athn_softc *sc)
1429 {
1430 struct athn_ani *ani = &sc->sc_ani;
1431 struct athn_ops *ops = &sc->sc_ops;
1432 int32_t rssi;
1433
1434 /* Raise noise immunity level, up to max. */
1435 if (ani->noise_immunity_level < 4) {
1436 ani->noise_immunity_level++;
1437 ops->set_noise_immunity_level(sc, ani->noise_immunity_level);
1438 return;
1439 }
1440
1441 #ifndef IEEE80211_STA_ONLY
1442 if (sc->sc_ic.ic_opmode == IEEE80211_M_HOSTAP) {
1443 if (ani->firstep_level < 2) {
1444 ani->firstep_level++;
1445 ops->set_firstep_level(sc, ani->firstep_level);
1446 }
1447 return;
1448 }
1449 #endif
1450 rssi = athn_ani_get_rssi(sc);
1451 if (rssi > ATHN_ANI_RSSI_THR_LOW) {
1452 /*
1453 * Beacon RSSI is in mid or high range, raise first step
1454 * level.
1455 */
1456 if (ani->firstep_level < 2) {
1457 ani->firstep_level++;
1458 ops->set_firstep_level(sc, ani->firstep_level);
1459 }
1460 }
1461 else if (sc->sc_ic.ic_curmode != IEEE80211_MODE_11A) {
1462 /*
1463 * Beacon RSSI is low, zero first step level to maximize
1464 * CCK sensitivity.
1465 */
1466 if (ani->firstep_level > 0) {
1467 ani->firstep_level = 0;
1468 ops->set_firstep_level(sc, 0);
1469 }
1470 }
1471 }
1472 #endif /* notyet */
1473
1474 #ifdef notyet
1475 Static void
1476 athn_ani_lower_immunity(struct athn_softc *sc)
1477 {
1478 struct athn_ani *ani = &sc->sc_ani;
1479 struct athn_ops *ops = &sc->sc_ops;
1480 int32_t rssi;
1481
1482 #ifndef IEEE80211_STA_ONLY
1483 if (sc->sc_ic.ic_opmode == IEEE80211_M_HOSTAP) {
1484 if (ani->firstep_level > 0) {
1485 ani->firstep_level--;
1486 ops->set_firstep_level(sc, ani->firstep_level);
1487 }
1488 return;
1489 }
1490 #endif
1491 rssi = athn_ani_get_rssi(sc);
1492 if (rssi > ATHN_ANI_RSSI_THR_HIGH) {
1493 /*
1494 * Beacon RSSI is high, leave OFDM weak signal detection
1495 * off or it may oscillate.
1496 */
1497 }
1498 else if (rssi > ATHN_ANI_RSSI_THR_LOW) {
1499 /*
1500 * Beacon RSSI is in mid range, turn on OFDM weak signal
1501 * detection or lower first step level.
1502 */
1503 if (!ani->ofdm_weak_signal) {
1504 ani->ofdm_weak_signal = 1;
1505 ops->enable_ofdm_weak_signal(sc);
1506 return;
1507 }
1508 if (ani->firstep_level > 0) {
1509 ani->firstep_level--;
1510 ops->set_firstep_level(sc, ani->firstep_level);
1511 return;
1512 }
1513 }
1514 else {
1515 /* Beacon RSSI is low, lower first step level. */
1516 if (ani->firstep_level > 0) {
1517 ani->firstep_level--;
1518 ops->set_firstep_level(sc, ani->firstep_level);
1519 return;
1520 }
1521 }
1522 /*
1523 * Lower spur immunity level down to zero, or if all else fails,
1524 * lower noise immunity level down to zero.
1525 */
1526 if (ani->spur_immunity_level > 0) {
1527 ani->spur_immunity_level--;
1528 ops->set_spur_immunity_level(sc, ani->spur_immunity_level);
1529 }
1530 else if (ani->noise_immunity_level > 0) {
1531 ani->noise_immunity_level--;
1532 ops->set_noise_immunity_level(sc, ani->noise_immunity_level);
1533 }
1534 }
1535 #endif /* notyet */
1536
1537 #ifdef notyet
1538 Static void
1539 athn_ani_restart(struct athn_softc *sc)
1540 {
1541 struct athn_ani *ani = &sc->sc_ani;
1542
1543 AR_WRITE(sc, AR_PHY_ERR_1, 0);
1544 AR_WRITE(sc, AR_PHY_ERR_2, 0);
1545 AR_WRITE(sc, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
1546 AR_WRITE(sc, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
1547 AR_WRITE_BARRIER(sc);
1548
1549 ani->listen_time = 0;
1550 ani->ofdm_phy_err_count = 0;
1551 ani->cck_phy_err_count = 0;
1552 }
1553 #endif /* notyet */
1554
1555 #ifdef notyet
1556 Static void
1557 athn_ani_monitor(struct athn_softc *sc)
1558 {
1559 struct athn_ani *ani = &sc->sc_ani;
1560 uint32_t cyccnt, txfcnt, rxfcnt, phy1, phy2;
1561 int32_t cycdelta, txfdelta, rxfdelta;
1562 int32_t listen_time;
1563
1564 txfcnt = AR_READ(sc, AR_TFCNT); /* Tx frame count. */
1565 rxfcnt = AR_READ(sc, AR_RFCNT); /* Rx frame count. */
1566 cyccnt = AR_READ(sc, AR_CCCNT); /* Cycle count. */
1567
1568 if (ani->cyccnt != 0 && ani->cyccnt <= cyccnt) {
1569 cycdelta = cyccnt - ani->cyccnt;
1570 txfdelta = txfcnt - ani->txfcnt;
1571 rxfdelta = rxfcnt - ani->rxfcnt;
1572
1573 listen_time = (cycdelta - txfdelta - rxfdelta) /
1574 (athn_clock_rate(sc) * 1000);
1575 }
1576 else
1577 listen_time = 0;
1578
1579 ani->cyccnt = cyccnt;
1580 ani->txfcnt = txfcnt;
1581 ani->rxfcnt = rxfcnt;
1582
1583 if (listen_time < 0) {
1584 athn_ani_restart(sc);
1585 return;
1586 }
1587 ani->listen_time += listen_time;
1588
1589 phy1 = AR_READ(sc, AR_PHY_ERR_1);
1590 phy2 = AR_READ(sc, AR_PHY_ERR_2);
1591
1592 if (phy1 < ani->ofdm_phy_err_base) {
1593 AR_WRITE(sc, AR_PHY_ERR_1, ani->ofdm_phy_err_base);
1594 AR_WRITE(sc, AR_PHY_ERR_MASK_1, AR_PHY_ERR_OFDM_TIMING);
1595 }
1596 if (phy2 < ani->cck_phy_err_base) {
1597 AR_WRITE(sc, AR_PHY_ERR_2, ani->cck_phy_err_base);
1598 AR_WRITE(sc, AR_PHY_ERR_MASK_2, AR_PHY_ERR_CCK_TIMING);
1599 }
1600 if (phy1 < ani->ofdm_phy_err_base || phy2 < ani->cck_phy_err_base) {
1601 AR_WRITE_BARRIER(sc);
1602 return;
1603 }
1604 ani->ofdm_phy_err_count = phy1 - ani->ofdm_phy_err_base;
1605 ani->cck_phy_err_count = phy2 - ani->cck_phy_err_base;
1606
1607 if (ani->listen_time > 5 * ATHN_ANI_PERIOD) {
1608 /* Check to see if we need to lower immunity. */
1609 if (ani->ofdm_phy_err_count <=
1610 ani->listen_time * ani->ofdm_trig_low / 1000 &&
1611 ani->cck_phy_err_count <=
1612 ani->listen_time * ani->cck_trig_low / 1000)
1613 athn_ani_lower_immunity(sc);
1614 athn_ani_restart(sc);
1615
1616 }
1617 else if (ani->listen_time > ATHN_ANI_PERIOD) {
1618 /* Check to see if we need to raise immunity. */
1619 if (ani->ofdm_phy_err_count >
1620 ani->listen_time * ani->ofdm_trig_high / 1000) {
1621 athn_ani_ofdm_err_trigger(sc);
1622 athn_ani_restart(sc);
1623 }
1624 else if (ani->cck_phy_err_count >
1625 ani->listen_time * ani->cck_trig_high / 1000) {
1626 athn_ani_cck_err_trigger(sc);
1627 athn_ani_restart(sc);
1628 }
1629 }
1630 }
1631 #endif /* notyet */
1632
1633 PUBLIC uint8_t
1634 athn_chan2fbin(struct ieee80211_channel *c)
1635 {
1636
1637 if (IEEE80211_IS_CHAN_2GHZ(c))
1638 return c->ic_freq - 2300;
1639 else
1640 return (c->ic_freq - 4800) / 5;
1641 }
1642
1643 PUBLIC int
1644 athn_interpolate(int x, int x1, int y1, int x2, int y2)
1645 {
1646
1647 if (x1 == x2) /* Prevents division by zero. */
1648 return y1;
1649 /* Linear interpolation. */
1650 return y1 + ((x - x1) * (y2 - y1)) / (x2 - x1);
1651 }
1652
1653 PUBLIC void
1654 athn_get_pier_ival(uint8_t fbin, const uint8_t *pierfreq, int npiers,
1655 int *lo, int *hi)
1656 {
1657 int i;
1658
1659 for (i = 0; i < npiers; i++)
1660 if (pierfreq[i] == AR_BCHAN_UNUSED ||
1661 pierfreq[i] > fbin)
1662 break;
1663 *hi = i;
1664 *lo = *hi - 1;
1665 if (*lo == -1)
1666 *lo = *hi;
1667 else if (*hi == npiers || pierfreq[*hi] == AR_BCHAN_UNUSED)
1668 *hi = *lo;
1669 }
1670
1671 Static void
1672 athn_init_dma(struct athn_softc *sc)
1673 {
1674 uint32_t reg;
1675
1676 if (!AR_SREV_9380_10_OR_LATER(sc)) {
1677 /* Set AHB not to do cacheline prefetches. */
1678 AR_SETBITS(sc, AR_AHB_MODE, AR_AHB_PREFETCH_RD_EN);
1679 }
1680 reg = AR_READ(sc, AR_TXCFG);
1681 /* Let MAC DMA reads be in 128-byte chunks. */
1682 reg = RW(reg, AR_TXCFG_DMASZ, AR_DMASZ_128B);
1683
1684 /* Set initial Tx trigger level. */
1685 if (AR_SREV_9285(sc) || AR_SREV_9271(sc))
1686 reg = RW(reg, AR_TXCFG_FTRIG, AR_TXCFG_FTRIG_256B);
1687 else if (!AR_SREV_9380_10_OR_LATER(sc))
1688 reg = RW(reg, AR_TXCFG_FTRIG, AR_TXCFG_FTRIG_512B);
1689 AR_WRITE(sc, AR_TXCFG, reg);
1690
1691 /* Let MAC DMA writes be in 128-byte chunks. */
1692 reg = AR_READ(sc, AR_RXCFG);
1693 reg = RW(reg, AR_RXCFG_DMASZ, AR_DMASZ_128B);
1694 AR_WRITE(sc, AR_RXCFG, reg);
1695
1696 /* Setup Rx FIFO threshold to hold off Tx activities. */
1697 AR_WRITE(sc, AR_RXFIFO_CFG, 512);
1698
1699 /* Reduce the number of entries in PCU TXBUF to avoid wrap around. */
1700 if (AR_SREV_9285(sc)) {
1701 AR_WRITE(sc, AR_PCU_TXBUF_CTRL,
1702 AR9285_PCU_TXBUF_CTRL_USABLE_SIZE);
1703 }
1704 else if (!AR_SREV_9271(sc)) {
1705 AR_WRITE(sc, AR_PCU_TXBUF_CTRL,
1706 AR_PCU_TXBUF_CTRL_USABLE_SIZE);
1707 }
1708 AR_WRITE_BARRIER(sc);
1709
1710 /* Reset Tx status ring. */
1711 if (AR_SREV_9380_10_OR_LATER(sc))
1712 ar9003_reset_txsring(sc);
1713 }
1714
1715 PUBLIC void
1716 athn_inc_tx_trigger_level(struct athn_softc *sc)
1717 {
1718 uint32_t reg, ftrig;
1719
1720 reg = AR_READ(sc, AR_TXCFG);
1721 ftrig = MS(reg, AR_TXCFG_FTRIG);
1722 /*
1723 * NB: The AR9285 and all single-stream parts have an issue that
1724 * limits the size of the PCU Tx FIFO to 2KB instead of 4KB.
1725 */
1726 if (ftrig == ((AR_SREV_9285(sc) || AR_SREV_9271(sc)) ? 0x1f : 0x3f))
1727 return; /* Already at max. */
1728 reg = RW(reg, AR_TXCFG_FTRIG, ftrig + 1);
1729 AR_WRITE(sc, AR_TXCFG, reg);
1730 AR_WRITE_BARRIER(sc);
1731 }
1732
1733 PUBLIC int
1734 athn_stop_rx_dma(struct athn_softc *sc)
1735 {
1736 int ntries;
1737
1738 AR_WRITE(sc, AR_CR, AR_CR_RXD);
1739 /* Wait for Rx enable bit to go low. */
1740 for (ntries = 0; ntries < 100; ntries++) {
1741 if (!(AR_READ(sc, AR_CR) & AR_CR_RXE))
1742 return 0;
1743 DELAY(100);
1744 }
1745 DPRINTFN(DBG_RX, sc, "Rx DMA failed to stop\n");
1746 return ETIMEDOUT;
1747 }
1748
1749 #ifdef unused
1750 Static int
1751 athn_rx_abort(struct athn_softc *sc)
1752 {
1753 int ntries;
1754
1755 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
1756 for (ntries = 0; ntries < 1000; ntries++) {
1757 if (MS(AR_READ(sc, AR_OBS_BUS_1), AR_OBS_BUS_1_RX_STATE) == 0)
1758 return 0;
1759 DELAY(10);
1760 }
1761 DPRINTFN(DBG_RX, sc, "Rx failed to go idle in 10ms\n");
1762 AR_CLRBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
1763 AR_WRITE_BARRIER(sc);
1764 return ETIMEDOUT;
1765 }
1766 #endif /* unused */
1767
1768 Static void
1769 athn_tx_reclaim(struct athn_softc *sc, int qid)
1770 {
1771 struct athn_txq *txq = &sc->sc_txq[qid];
1772 struct athn_tx_buf *bf;
1773
1774 /* Reclaim all buffers queued in the specified Tx queue. */
1775 /* NB: Tx DMA must be stopped. */
1776 while ((bf = SIMPLEQ_FIRST(&txq->head)) != NULL) {
1777 SIMPLEQ_REMOVE_HEAD(&txq->head, bf_list);
1778
1779 bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0,
1780 bf->bf_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1781 bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
1782 m_freem(bf->bf_m);
1783 bf->bf_m = NULL;
1784 bf->bf_ni = NULL; /* Nodes already freed! */
1785
1786 /* Link Tx buffer back to global free list. */
1787 SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
1788 }
1789 }
1790
1791 PUBLIC int
1792 athn_tx_pending(struct athn_softc *sc, int qid)
1793 {
1794
1795 return MS(AR_READ(sc, AR_QSTS(qid)), AR_Q_STS_PEND_FR_CNT) != 0 ||
1796 (AR_READ(sc, AR_Q_TXE) & (1 << qid)) != 0;
1797 }
1798
1799 PUBLIC void
1800 athn_stop_tx_dma(struct athn_softc *sc, int qid)
1801 {
1802 uint32_t tsflo;
1803 int ntries, i;
1804
1805 AR_WRITE(sc, AR_Q_TXD, 1 << qid);
1806 for (ntries = 0; ntries < 40; ntries++) {
1807 if (!athn_tx_pending(sc, qid))
1808 break;
1809 DELAY(100);
1810 }
1811 if (ntries == 40) {
1812 for (i = 0; i < 2; i++) {
1813 tsflo = AR_READ(sc, AR_TSF_L32) / 1024;
1814 AR_WRITE(sc, AR_QUIET2,
1815 SM(AR_QUIET2_QUIET_DUR, 10));
1816 AR_WRITE(sc, AR_QUIET_PERIOD, 100);
1817 AR_WRITE(sc, AR_NEXT_QUIET_TIMER, tsflo);
1818 AR_SETBITS(sc, AR_TIMER_MODE, AR_QUIET_TIMER_EN);
1819 if (AR_READ(sc, AR_TSF_L32) / 1024 == tsflo)
1820 break;
1821 }
1822 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
1823 AR_WRITE_BARRIER(sc);
1824 DELAY(200);
1825 AR_CLRBITS(sc, AR_TIMER_MODE, AR_QUIET_TIMER_EN);
1826 AR_WRITE_BARRIER(sc);
1827
1828 for (ntries = 0; ntries < 40; ntries++) {
1829 if (!athn_tx_pending(sc, qid))
1830 break;
1831 DELAY(100);
1832 }
1833
1834 AR_CLRBITS(sc, AR_DIAG_SW, AR_DIAG_FORCE_CH_IDLE_HIGH);
1835 }
1836 AR_WRITE(sc, AR_Q_TXD, 0);
1837 AR_WRITE_BARRIER(sc);
1838 }
1839
1840 PUBLIC int
1841 athn_txtime(struct athn_softc *sc, int len, int ridx, u_int flags)
1842 {
1843 #define divround(a, b) (((a) + (b) - 1) / (b))
1844 int txtime;
1845
1846 /* XXX HT. */
1847 if (athn_rates[ridx].phy == IEEE80211_T_OFDM) {
1848 txtime = divround(8 + 4 * len + 3, athn_rates[ridx].rate);
1849 /* SIFS is 10us for 11g but Signal Extension adds 6us. */
1850 txtime = 16 + 4 + 4 * txtime + 16;
1851 }
1852 else {
1853 txtime = divround(16 * len, athn_rates[ridx].rate);
1854 if (ridx != ATHN_RIDX_CCK1 && (flags & IEEE80211_F_SHPREAMBLE))
1855 txtime += 72 + 24;
1856 else
1857 txtime += 144 + 48;
1858 txtime += 10; /* 10us SIFS. */
1859 }
1860 return txtime;
1861 #undef divround
1862 }
1863
1864 PUBLIC void
1865 athn_init_tx_queues(struct athn_softc *sc)
1866 {
1867 int qid;
1868
1869 for (qid = 0; qid < ATHN_QID_COUNT; qid++) {
1870 SIMPLEQ_INIT(&sc->sc_txq[qid].head);
1871 sc->sc_txq[qid].lastds = NULL;
1872 sc->sc_txq[qid].wait = NULL;
1873 sc->sc_txq[qid].queued = 0;
1874
1875 AR_WRITE(sc, AR_DRETRY_LIMIT(qid),
1876 SM(AR_D_RETRY_LIMIT_STA_SH, 32) |
1877 SM(AR_D_RETRY_LIMIT_STA_LG, 32) |
1878 SM(AR_D_RETRY_LIMIT_FR_SH, 10));
1879 AR_WRITE(sc, AR_QMISC(qid),
1880 AR_Q_MISC_DCU_EARLY_TERM_REQ);
1881 AR_WRITE(sc, AR_DMISC(qid),
1882 SM(AR_D_MISC_BKOFF_THRESH, 2) |
1883 AR_D_MISC_CW_BKOFF_EN | AR_D_MISC_FRAG_WAIT_EN);
1884 }
1885
1886 /* Init beacon queue. */
1887 AR_SETBITS(sc, AR_QMISC(ATHN_QID_BEACON),
1888 AR_Q_MISC_FSP_DBA_GATED | AR_Q_MISC_BEACON_USE |
1889 AR_Q_MISC_CBR_INCR_DIS1);
1890 AR_SETBITS(sc, AR_DMISC(ATHN_QID_BEACON),
1891 SM(AR_D_MISC_ARB_LOCKOUT_CNTRL,
1892 AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL) |
1893 AR_D_MISC_BEACON_USE |
1894 AR_D_MISC_POST_FR_BKOFF_DIS);
1895 AR_WRITE(sc, AR_DLCL_IFS(ATHN_QID_BEACON),
1896 SM(AR_D_LCL_IFS_CWMIN, 0) |
1897 SM(AR_D_LCL_IFS_CWMAX, 0) |
1898 SM(AR_D_LCL_IFS_AIFS, 1));
1899
1900 /* Init CAB (Content After Beacon) queue. */
1901 AR_SETBITS(sc, AR_QMISC(ATHN_QID_CAB),
1902 AR_Q_MISC_FSP_DBA_GATED | AR_Q_MISC_CBR_INCR_DIS1 |
1903 AR_Q_MISC_CBR_INCR_DIS0);
1904 AR_SETBITS(sc, AR_DMISC(ATHN_QID_CAB),
1905 SM(AR_D_MISC_ARB_LOCKOUT_CNTRL,
1906 AR_D_MISC_ARB_LOCKOUT_CNTRL_GLOBAL));
1907
1908 /* Init PS-Poll queue. */
1909 AR_SETBITS(sc, AR_QMISC(ATHN_QID_PSPOLL),
1910 AR_Q_MISC_CBR_INCR_DIS1);
1911
1912 /* Init UAPSD queue. */
1913 AR_SETBITS(sc, AR_DMISC(ATHN_QID_UAPSD),
1914 AR_D_MISC_POST_FR_BKOFF_DIS);
1915
1916 if (AR_SREV_9380_10_OR_LATER(sc)) {
1917 /* Enable MAC descriptor CRC check. */
1918 AR_WRITE(sc, AR_Q_DESC_CRCCHK, AR_Q_DESC_CRCCHK_EN);
1919 }
1920 /* Enable DESC interrupts for all Tx queues. */
1921 AR_WRITE(sc, AR_IMR_S0, 0x00ff0000);
1922 /* Enable EOL interrupts for all Tx queues except UAPSD. */
1923 AR_WRITE(sc, AR_IMR_S1, 0x00df0000);
1924 AR_WRITE_BARRIER(sc);
1925 }
1926
1927 PUBLIC void
1928 athn_set_sta_timers(struct athn_softc *sc)
1929 {
1930 struct ieee80211com *ic = &sc->sc_ic;
1931 uint32_t tsfhi, tsflo, tsftu, reg;
1932 uint32_t intval, next_tbtt, next_dtim;
1933 int dtim_period, dtim_count, rem_dtim_count;
1934
1935 tsfhi = AR_READ(sc, AR_TSF_U32);
1936 tsflo = AR_READ(sc, AR_TSF_L32);
1937 tsftu = AR_TSF_TO_TU(tsfhi, tsflo) + AR_FUDGE;
1938
1939 /* Beacon interval in TU. */
1940 intval = ic->ic_bss->ni_intval;
1941
1942 next_tbtt = roundup(tsftu, intval);
1943 #ifdef notyet
1944 dtim_period = ic->ic_dtim_period;
1945 if (dtim_period <= 0)
1946 #endif
1947 dtim_period = 1; /* Assume all TIMs are DTIMs. */
1948
1949 #ifdef notyet
1950 dtim_count = ic->ic_dtim_count;
1951 if (dtim_count >= dtim_period) /* Should not happen. */
1952 #endif
1953 dtim_count = 0; /* Assume last TIM was a DTIM. */
1954
1955 /* Compute number of remaining TIMs until next DTIM. */
1956 rem_dtim_count = 0; /* XXX */
1957 next_dtim = next_tbtt + rem_dtim_count * intval;
1958
1959 AR_WRITE(sc, AR_NEXT_TBTT_TIMER, next_tbtt * IEEE80211_DUR_TU);
1960 AR_WRITE(sc, AR_BEACON_PERIOD, intval * IEEE80211_DUR_TU);
1961 AR_WRITE(sc, AR_DMA_BEACON_PERIOD, intval * IEEE80211_DUR_TU);
1962
1963 /*
1964 * Set the number of consecutive beacons to miss before raising
1965 * a BMISS interrupt to 10.
1966 */
1967 reg = AR_READ(sc, AR_RSSI_THR);
1968 reg = RW(reg, AR_RSSI_THR_BM_THR, 10);
1969 AR_WRITE(sc, AR_RSSI_THR, reg);
1970
1971 AR_WRITE(sc, AR_NEXT_DTIM,
1972 (next_dtim - AR_SLEEP_SLOP) * IEEE80211_DUR_TU);
1973 AR_WRITE(sc, AR_NEXT_TIM,
1974 (next_tbtt - AR_SLEEP_SLOP) * IEEE80211_DUR_TU);
1975
1976 /* CAB timeout is in 1/8 TU. */
1977 AR_WRITE(sc, AR_SLEEP1,
1978 SM(AR_SLEEP1_CAB_TIMEOUT, AR_CAB_TIMEOUT_VAL * 8) |
1979 AR_SLEEP1_ASSUME_DTIM);
1980 AR_WRITE(sc, AR_SLEEP2,
1981 SM(AR_SLEEP2_BEACON_TIMEOUT, AR_MIN_BEACON_TIMEOUT_VAL));
1982
1983 AR_WRITE(sc, AR_TIM_PERIOD, intval * IEEE80211_DUR_TU);
1984 AR_WRITE(sc, AR_DTIM_PERIOD, dtim_period * intval * IEEE80211_DUR_TU);
1985
1986 AR_SETBITS(sc, AR_TIMER_MODE,
1987 AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN | AR_DTIM_TIMER_EN);
1988
1989 /* Set TSF out-of-range threshold (fixed at 16k us). */
1990 AR_WRITE(sc, AR_TSFOOR_THRESHOLD, 0x4240);
1991
1992 AR_WRITE_BARRIER(sc);
1993 }
1994
1995 #ifndef IEEE80211_STA_ONLY
1996 PUBLIC void
1997 athn_set_hostap_timers(struct athn_softc *sc)
1998 {
1999 struct ieee80211com *ic = &sc->sc_ic;
2000 uint32_t intval, next_tbtt;
2001
2002 /* Beacon interval in TU. */
2003 intval = ic->ic_bss->ni_intval;
2004 next_tbtt = intval;
2005
2006 AR_WRITE(sc, AR_NEXT_TBTT_TIMER, next_tbtt * IEEE80211_DUR_TU);
2007 AR_WRITE(sc, AR_NEXT_DMA_BEACON_ALERT,
2008 (next_tbtt - AR_BEACON_DMA_DELAY) * IEEE80211_DUR_TU);
2009 AR_WRITE(sc, AR_NEXT_CFP,
2010 (next_tbtt - AR_SWBA_DELAY) * IEEE80211_DUR_TU);
2011
2012 AR_WRITE(sc, AR_BEACON_PERIOD, intval * IEEE80211_DUR_TU);
2013 AR_WRITE(sc, AR_DMA_BEACON_PERIOD, intval * IEEE80211_DUR_TU);
2014 AR_WRITE(sc, AR_SWBA_PERIOD, intval * IEEE80211_DUR_TU);
2015 AR_WRITE(sc, AR_NDP_PERIOD, intval * IEEE80211_DUR_TU);
2016
2017 AR_WRITE(sc, AR_TIMER_MODE,
2018 AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN);
2019
2020 AR_WRITE_BARRIER(sc);
2021 }
2022 #endif
2023
2024 PUBLIC void
2025 athn_set_opmode(struct athn_softc *sc)
2026 {
2027 uint32_t reg;
2028
2029 switch (sc->sc_ic.ic_opmode) {
2030 #ifndef IEEE80211_STA_ONLY
2031 case IEEE80211_M_HOSTAP:
2032 reg = AR_READ(sc, AR_STA_ID1);
2033 reg &= ~AR_STA_ID1_ADHOC;
2034 reg |= AR_STA_ID1_STA_AP | AR_STA_ID1_KSRCH_MODE;
2035 AR_WRITE(sc, AR_STA_ID1, reg);
2036
2037 AR_CLRBITS(sc, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
2038 break;
2039 case IEEE80211_M_IBSS:
2040 case IEEE80211_M_AHDEMO:
2041 reg = AR_READ(sc, AR_STA_ID1);
2042 reg &= ~AR_STA_ID1_STA_AP;
2043 reg |= AR_STA_ID1_ADHOC | AR_STA_ID1_KSRCH_MODE;
2044 AR_WRITE(sc, AR_STA_ID1, reg);
2045
2046 AR_SETBITS(sc, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
2047 break;
2048 #endif
2049 default:
2050 reg = AR_READ(sc, AR_STA_ID1);
2051 reg &= ~(AR_STA_ID1_ADHOC | AR_STA_ID1_STA_AP);
2052 reg |= AR_STA_ID1_KSRCH_MODE;
2053 AR_WRITE(sc, AR_STA_ID1, reg);
2054 break;
2055 }
2056 AR_WRITE_BARRIER(sc);
2057 }
2058
2059 PUBLIC void
2060 athn_set_bss(struct athn_softc *sc, struct ieee80211_node *ni)
2061 {
2062 const uint8_t *bssid = ni->ni_bssid;
2063
2064 AR_WRITE(sc, AR_BSS_ID0, LE_READ_4(&bssid[0]));
2065 AR_WRITE(sc, AR_BSS_ID1, LE_READ_2(&bssid[4]) |
2066 SM(AR_BSS_ID1_AID, IEEE80211_AID(ni->ni_associd)));
2067 AR_WRITE_BARRIER(sc);
2068 }
2069
2070 Static void
2071 athn_enable_interrupts(struct athn_softc *sc)
2072 {
2073 uint32_t mask2;
2074
2075 athn_disable_interrupts(sc); /* XXX */
2076
2077 AR_WRITE(sc, AR_IMR, sc->sc_imask);
2078
2079 mask2 = AR_READ(sc, AR_IMR_S2);
2080 mask2 &= ~(AR_IMR_S2_TIM | AR_IMR_S2_DTIM | AR_IMR_S2_DTIMSYNC |
2081 AR_IMR_S2_CABEND | AR_IMR_S2_CABTO | AR_IMR_S2_TSFOOR);
2082 mask2 |= AR_IMR_S2_GTT | AR_IMR_S2_CST;
2083 AR_WRITE(sc, AR_IMR_S2, mask2);
2084
2085 AR_CLRBITS(sc, AR_IMR_S5, AR_IMR_S5_TIM_TIMER);
2086
2087 AR_WRITE(sc, AR_IER, AR_IER_ENABLE);
2088
2089 AR_WRITE(sc, AR_INTR_ASYNC_ENABLE, AR_INTR_MAC_IRQ);
2090 AR_WRITE(sc, AR_INTR_ASYNC_MASK, AR_INTR_MAC_IRQ);
2091
2092 AR_WRITE(sc, AR_INTR_SYNC_ENABLE, sc->sc_isync);
2093 AR_WRITE(sc, AR_INTR_SYNC_MASK, sc->sc_isync);
2094 AR_WRITE_BARRIER(sc);
2095 }
2096
2097 Static void
2098 athn_disable_interrupts(struct athn_softc *sc)
2099 {
2100
2101 AR_WRITE(sc, AR_IER, 0);
2102 (void)AR_READ(sc, AR_IER);
2103
2104 AR_WRITE(sc, AR_INTR_ASYNC_ENABLE, 0);
2105 (void)AR_READ(sc, AR_INTR_ASYNC_ENABLE);
2106
2107 AR_WRITE(sc, AR_INTR_SYNC_ENABLE, 0);
2108 (void)AR_READ(sc, AR_INTR_SYNC_ENABLE);
2109
2110 AR_WRITE(sc, AR_IMR, 0);
2111
2112 AR_CLRBITS(sc, AR_IMR_S2, AR_IMR_S2_TIM | AR_IMR_S2_DTIM |
2113 AR_IMR_S2_DTIMSYNC | AR_IMR_S2_CABEND | AR_IMR_S2_CABTO |
2114 AR_IMR_S2_TSFOOR | AR_IMR_S2_GTT | AR_IMR_S2_CST);
2115
2116 AR_CLRBITS(sc, AR_IMR_S5, AR_IMR_S5_TIM_TIMER);
2117 AR_WRITE_BARRIER(sc);
2118 }
2119
2120 Static void
2121 athn_init_qos(struct athn_softc *sc)
2122 {
2123
2124 /* Initialize QoS settings. */
2125 AR_WRITE(sc, AR_MIC_QOS_CONTROL, 0x100aa);
2126 AR_WRITE(sc, AR_MIC_QOS_SELECT, 0x3210);
2127 AR_WRITE(sc, AR_QOS_NO_ACK,
2128 SM(AR_QOS_NO_ACK_TWO_BIT, 2) |
2129 SM(AR_QOS_NO_ACK_BIT_OFF, 5) |
2130 SM(AR_QOS_NO_ACK_BYTE_OFF, 0));
2131 AR_WRITE(sc, AR_TXOP_X, AR_TXOP_X_VAL);
2132 /* Initialize TXOP for all TIDs. */
2133 AR_WRITE(sc, AR_TXOP_0_3, 0xffffffff);
2134 AR_WRITE(sc, AR_TXOP_4_7, 0xffffffff);
2135 AR_WRITE(sc, AR_TXOP_8_11, 0xffffffff);
2136 AR_WRITE(sc, AR_TXOP_12_15, 0xffffffff);
2137 AR_WRITE_BARRIER(sc);
2138 }
2139
2140 PUBLIC int
2141 athn_hw_reset(struct athn_softc *sc, struct ieee80211_channel *curchan,
2142 struct ieee80211_channel *extchan, int init)
2143 {
2144 struct ieee80211com *ic = &sc->sc_ic;
2145 struct athn_ops *ops = &sc->sc_ops;
2146 uint32_t reg, def_ant, sta_id1, cfg_led, tsflo, tsfhi;
2147 int i, error;
2148
2149 /* XXX not if already awake */
2150 if ((error = athn_set_power_awake(sc)) != 0) {
2151 aprint_error_dev(sc->sc_dev, "could not wakeup chip\n");
2152 return error;
2153 }
2154
2155 /* Preserve the antenna on a channel switch. */
2156 if ((def_ant = AR_READ(sc, AR_DEF_ANTENNA)) == 0)
2157 def_ant = 1;
2158 /* Preserve other registers. */
2159 sta_id1 = AR_READ(sc, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
2160 cfg_led = AR_READ(sc, AR_CFG_LED) & (AR_CFG_LED_ASSOC_CTL_M |
2161 AR_CFG_LED_MODE_SEL_M | AR_CFG_LED_BLINK_THRESH_SEL_M |
2162 AR_CFG_LED_BLINK_SLOW);
2163
2164 /* Mark PHY as inactive. */
2165 ops->disable_phy(sc);
2166
2167 if (init && AR_SREV_9271(sc)) {
2168 AR_WRITE(sc, AR9271_RESET_POWER_DOWN_CONTROL,
2169 AR9271_RADIO_RF_RST);
2170 DELAY(50);
2171 }
2172 if (AR_SREV_9280(sc) && (sc->sc_flags & ATHN_FLAG_OLPC)) {
2173 /* Save TSF before it gets cleared. */
2174 tsfhi = AR_READ(sc, AR_TSF_U32);
2175 tsflo = AR_READ(sc, AR_TSF_L32);
2176
2177 /* NB: RTC reset clears TSF. */
2178 error = athn_reset_power_on(sc);
2179 }
2180 else {
2181 tsfhi = tsflo = 0; /* XXX: gcc */
2182 error = athn_reset(sc, 0);
2183 }
2184 if (error != 0) {
2185 aprint_error_dev(sc->sc_dev,
2186 "could not reset chip (error=%d)\n", error);
2187 return error;
2188 }
2189
2190 /* XXX not if already awake */
2191 if ((error = athn_set_power_awake(sc)) != 0) {
2192 aprint_error_dev(sc->sc_dev, "could not wakeup chip\n");
2193 return error;
2194 }
2195
2196 athn_init_pll(sc, curchan);
2197 ops->set_rf_mode(sc, curchan);
2198
2199 if (sc->sc_flags & ATHN_FLAG_RFSILENT) {
2200 /* Check that the radio is not disabled by hardware switch. */
2201 reg = ops->gpio_read(sc, sc->sc_rfsilent_pin);
2202 if (sc->sc_flags & ATHN_FLAG_RFSILENT_REVERSED)
2203 reg = !reg;
2204 if (!reg) {
2205 aprint_error_dev(sc->sc_dev,
2206 "radio is disabled by hardware switch\n");
2207 return EPERM;
2208 }
2209 }
2210 if (init && AR_SREV_9271(sc)) {
2211 AR_WRITE(sc, AR9271_RESET_POWER_DOWN_CONTROL,
2212 AR9271_GATE_MAC_CTL);
2213 DELAY(50);
2214 }
2215 if (AR_SREV_9280(sc) && (sc->sc_flags & ATHN_FLAG_OLPC)) {
2216 /* Restore TSF if it got cleared. */
2217 AR_WRITE(sc, AR_TSF_L32, tsflo);
2218 AR_WRITE(sc, AR_TSF_U32, tsfhi);
2219 }
2220
2221 if (AR_SREV_9280_10_OR_LATER(sc))
2222 AR_SETBITS(sc, sc->sc_gpio_input_en_off, AR_GPIO_JTAG_DISABLE);
2223
2224 if (AR_SREV_9287_13_OR_LATER(sc) && !AR_SREV_9380_10_OR_LATER(sc))
2225 ar9287_1_3_enable_async_fifo(sc);
2226
2227 /* Write init values to hardware. */
2228 ops->hw_init(sc, curchan, extchan);
2229
2230 /*
2231 * Only >=AR9280 2.0 parts are capable of encrypting unicast
2232 * management frames using CCMP.
2233 */
2234 if (AR_SREV_9280_20_OR_LATER(sc)) {
2235 reg = AR_READ(sc, AR_AES_MUTE_MASK1);
2236 /* Do not mask the subtype field in management frames. */
2237 reg = RW(reg, AR_AES_MUTE_MASK1_FC0_MGMT, 0xff);
2238 reg = RW(reg, AR_AES_MUTE_MASK1_FC1_MGMT,
2239 ~(IEEE80211_FC1_RETRY | IEEE80211_FC1_PWR_MGT |
2240 IEEE80211_FC1_MORE_DATA));
2241 AR_WRITE(sc, AR_AES_MUTE_MASK1, reg);
2242 }
2243 else if (AR_SREV_9160_10_OR_LATER(sc)) {
2244 /* Disable hardware crypto for management frames. */
2245 AR_CLRBITS(sc, AR_PCU_MISC_MODE2,
2246 AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
2247 AR_SETBITS(sc, AR_PCU_MISC_MODE2,
2248 AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
2249 }
2250
2251 if (ic->ic_curmode != IEEE80211_MODE_11B)
2252 ops->set_delta_slope(sc, curchan, extchan);
2253
2254 ops->spur_mitigate(sc, curchan, extchan);
2255 ops->init_from_rom(sc, curchan, extchan);
2256
2257 /* XXX */
2258 AR_WRITE(sc, AR_STA_ID0, LE_READ_4(&ic->ic_myaddr[0]));
2259 AR_WRITE(sc, AR_STA_ID1, LE_READ_2(&ic->ic_myaddr[4]) |
2260 sta_id1 | AR_STA_ID1_RTS_USE_DEF | AR_STA_ID1_CRPT_MIC_ENABLE);
2261
2262 athn_set_opmode(sc);
2263
2264 AR_WRITE(sc, AR_BSSMSKL, 0xffffffff);
2265 AR_WRITE(sc, AR_BSSMSKU, 0xffff);
2266
2267 /* Restore previous antenna. */
2268 AR_WRITE(sc, AR_DEF_ANTENNA, def_ant);
2269
2270 AR_WRITE(sc, AR_BSS_ID0, 0);
2271 AR_WRITE(sc, AR_BSS_ID1, 0);
2272
2273 AR_WRITE(sc, AR_ISR, 0xffffffff);
2274
2275 AR_WRITE(sc, AR_RSSI_THR, SM(AR_RSSI_THR_BM_THR, 7));
2276
2277 if ((error = ops->set_synth(sc, curchan, extchan)) != 0) {
2278 aprint_error_dev(sc->sc_dev, "could not set channel\n");
2279 return error;
2280 }
2281 sc->sc_curchan = curchan;
2282 sc->sc_curchanext = extchan;
2283
2284 for (i = 0; i < AR_NUM_DCU; i++)
2285 AR_WRITE(sc, AR_DQCUMASK(i), 1 << i);
2286
2287 athn_init_tx_queues(sc);
2288
2289 /* Initialize interrupt mask. */
2290 sc->sc_imask =
2291 AR_IMR_TXDESC | AR_IMR_TXEOL |
2292 AR_IMR_RXERR | AR_IMR_RXEOL | AR_IMR_RXORN |
2293 AR_IMR_RXMINTR | AR_IMR_RXINTM |
2294 AR_IMR_GENTMR | AR_IMR_BCNMISC;
2295 if (AR_SREV_9380_10_OR_LATER(sc))
2296 sc->sc_imask |= AR_IMR_RXERR | AR_IMR_HP_RXOK;
2297 #ifndef IEEE80211_STA_ONLY
2298 if (0 && ic->ic_opmode == IEEE80211_M_HOSTAP)
2299 sc->sc_imask |= AR_IMR_MIB;
2300 #endif
2301 AR_WRITE(sc, AR_IMR, sc->sc_imask);
2302 AR_SETBITS(sc, AR_IMR_S2, AR_IMR_S2_GTT);
2303 AR_WRITE(sc, AR_INTR_SYNC_CAUSE, 0xffffffff);
2304 sc->sc_isync = AR_INTR_SYNC_DEFAULT;
2305 if (sc->sc_flags & ATHN_FLAG_RFSILENT)
2306 sc->sc_isync |= AR_INTR_SYNC_GPIO_PIN(sc->sc_rfsilent_pin);
2307 AR_WRITE(sc, AR_INTR_SYNC_ENABLE, sc->sc_isync);
2308 AR_WRITE(sc, AR_INTR_SYNC_MASK, 0);
2309 if (AR_SREV_9380_10_OR_LATER(sc)) {
2310 AR_WRITE(sc, AR_INTR_PRIO_ASYNC_ENABLE, 0);
2311 AR_WRITE(sc, AR_INTR_PRIO_ASYNC_MASK, 0);
2312 AR_WRITE(sc, AR_INTR_PRIO_SYNC_ENABLE, 0);
2313 AR_WRITE(sc, AR_INTR_PRIO_SYNC_MASK, 0);
2314 }
2315
2316 athn_init_qos(sc);
2317
2318 AR_SETBITS(sc, AR_PCU_MISC, AR_PCU_MIC_NEW_LOC_ENA);
2319
2320 if (AR_SREV_9287_13_OR_LATER(sc) && !AR_SREV_9380_10_OR_LATER(sc))
2321 ar9287_1_3_setup_async_fifo(sc);
2322
2323 /* Disable sequence number generation in hardware. */
2324 AR_SETBITS(sc, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
2325
2326 athn_init_dma(sc);
2327
2328 /* Program observation bus to see MAC interrupts. */
2329 AR_WRITE(sc, sc->sc_obs_off, 8);
2330
2331 /* Setup Rx interrupt mitigation. */
2332 AR_WRITE(sc, AR_RIMT, SM(AR_RIMT_FIRST, 2000) | SM(AR_RIMT_LAST, 500));
2333
2334 ops->init_baseband(sc);
2335
2336 if ((error = athn_init_calib(sc, curchan, extchan)) != 0) {
2337 aprint_error_dev(sc->sc_dev,
2338 "could not initialize calibration\n");
2339 return error;
2340 }
2341
2342 ops->set_rxchains(sc);
2343
2344 AR_WRITE(sc, AR_CFG_LED, cfg_led | AR_CFG_SCLK_32KHZ);
2345
2346 if (sc->sc_flags & ATHN_FLAG_USB) {
2347 if (AR_SREV_9271(sc))
2348 AR_WRITE(sc, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
2349 else
2350 AR_WRITE(sc, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
2351 }
2352 #if BYTE_ORDER == BIG_ENDIAN
2353 else {
2354 /* Default is LE, turn on swapping for BE. */
2355 AR_WRITE(sc, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
2356 }
2357 #endif
2358 AR_WRITE_BARRIER(sc);
2359
2360 return 0;
2361 }
2362
2363 Static struct ieee80211_node *
2364 athn_node_alloc(struct ieee80211_node_table *ntp)
2365 {
2366
2367 return malloc(sizeof(struct athn_node), M_DEVBUF,
2368 M_NOWAIT | M_ZERO);
2369 }
2370
2371 Static void
2372 athn_newassoc(struct ieee80211_node *ni, int isnew)
2373 {
2374 struct ieee80211com *ic = ni->ni_ic;
2375 struct athn_softc *sc = ic->ic_ifp->if_softc;
2376 struct athn_node *an = (void *)ni;
2377 struct ieee80211_rateset *rs = &ni->ni_rates;
2378 uint8_t rate;
2379 int ridx, i, j;
2380
2381 ieee80211_amrr_node_init(&sc->sc_amrr, &an->amn);
2382 /* Start at lowest available bit-rate, AMRR will raise. */
2383 ni->ni_txrate = 0;
2384
2385 for (i = 0; i < rs->rs_nrates; i++) {
2386 rate = rs->rs_rates[i] & IEEE80211_RATE_VAL;
2387
2388 /* Map 802.11 rate to HW rate index. */
2389 for (ridx = 0; ridx <= ATHN_RIDX_MAX; ridx++)
2390 if (athn_rates[ridx].rate == rate)
2391 break;
2392 an->ridx[i] = ridx;
2393 DPRINTFN(DBG_STM, sc, "rate %d index %d\n", rate, ridx);
2394
2395 /* Compute fallback rate for retries. */
2396 an->fallback[i] = i;
2397 for (j = i - 1; j >= 0; j--) {
2398 if (athn_rates[an->ridx[j]].phy ==
2399 athn_rates[an->ridx[i]].phy) {
2400 an->fallback[i] = j;
2401 break;
2402 }
2403 }
2404 DPRINTFN(DBG_STM, sc, "%d fallbacks to %d\n",
2405 i, an->fallback[i]);
2406 }
2407 }
2408
2409 Static int
2410 athn_media_change(struct ifnet *ifp)
2411 {
2412 struct athn_softc *sc = ifp->if_softc;
2413 struct ieee80211com *ic = &sc->sc_ic;
2414 uint8_t rate, ridx;
2415 int error;
2416
2417 error = ieee80211_media_change(ifp);
2418 if (error != ENETRESET)
2419 return error;
2420
2421 if (ic->ic_fixed_rate != -1) {
2422 rate = ic->ic_sup_rates[ic->ic_curmode].
2423 rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
2424 /* Map 802.11 rate to HW rate index. */
2425 for (ridx = 0; ridx <= ATHN_RIDX_MAX; ridx++)
2426 if (athn_rates[ridx].rate == rate)
2427 break;
2428 sc->sc_fixed_ridx = ridx;
2429 }
2430 if (IS_UP_AND_RUNNING(ifp)) {
2431 athn_stop(ifp, 0);
2432 error = athn_init(ifp);
2433 }
2434 return error;
2435 }
2436
2437 Static void
2438 athn_next_scan(void *arg)
2439 {
2440 struct athn_softc *sc = arg;
2441 struct ieee80211com *ic = &sc->sc_ic;
2442 int s;
2443
2444 s = splnet();
2445 if (ic->ic_state == IEEE80211_S_SCAN)
2446 ieee80211_next_scan(ic);
2447 splx(s);
2448 }
2449
2450 Static int
2451 athn_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
2452 {
2453 struct ifnet *ifp = ic->ic_ifp;
2454 struct athn_softc *sc = ifp->if_softc;
2455 uint32_t reg;
2456 int error;
2457
2458 callout_stop(&sc->sc_calib_to);
2459
2460 switch (nstate) {
2461 case IEEE80211_S_INIT:
2462 athn_set_led(sc, 0);
2463 break;
2464 case IEEE80211_S_SCAN:
2465 /* Make the LED blink while scanning. */
2466 athn_set_led(sc, !sc->sc_led_state);
2467 error = athn_switch_chan(sc, ic->ic_curchan, NULL);
2468 if (error != 0)
2469 return error;
2470 callout_schedule(&sc->sc_scan_to, hz / 5);
2471 break;
2472 case IEEE80211_S_AUTH:
2473 athn_set_led(sc, 0);
2474 error = athn_switch_chan(sc, ic->ic_curchan, NULL);
2475 if (error != 0)
2476 return error;
2477 break;
2478 case IEEE80211_S_ASSOC:
2479 break;
2480 case IEEE80211_S_RUN:
2481 athn_set_led(sc, 1);
2482
2483 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2484 break;
2485
2486 /* Fake a join to initialize the Tx rate. */
2487 athn_newassoc(ic->ic_bss, 1);
2488
2489 athn_set_bss(sc, ic->ic_bss);
2490 athn_disable_interrupts(sc);
2491 #ifndef IEEE80211_STA_ONLY
2492 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2493 athn_set_hostap_timers(sc);
2494 /* Enable software beacon alert interrupts. */
2495 sc->sc_imask |= AR_IMR_SWBA;
2496 }
2497 else
2498 #endif
2499 {
2500 athn_set_sta_timers(sc);
2501 /* Enable beacon miss interrupts. */
2502 sc->sc_imask |= AR_IMR_BMISS;
2503
2504 /* Stop receiving beacons from other BSS. */
2505 reg = AR_READ(sc, AR_RX_FILTER);
2506 reg = (reg & ~AR_RX_FILTER_BEACON) |
2507 AR_RX_FILTER_MYBEACON;
2508 AR_WRITE(sc, AR_RX_FILTER, reg);
2509 AR_WRITE_BARRIER(sc);
2510 }
2511 athn_enable_interrupts(sc);
2512
2513 if (sc->sc_sup_calib_mask != 0) {
2514 memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
2515 sc->sc_cur_calib_mask = sc->sc_sup_calib_mask;
2516 /* ops->do_calib(sc); */
2517 }
2518 /* XXX Start ANI. */
2519
2520 callout_schedule(&sc->sc_calib_to, hz / 2);
2521 break;
2522 }
2523
2524 return sc->sc_newstate(ic, nstate, arg);
2525 }
2526
2527 #ifdef notyet_edca
2528 PUBLIC void
2529 athn_updateedca(struct ieee80211com *ic)
2530 {
2531 #define ATHN_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */
2532 struct athn_softc *sc = ic->ic_ifp->if_softc;
2533 const struct ieee80211_edca_ac_params *ac;
2534 int aci, qid;
2535
2536 for (aci = 0; aci < EDCA_NUM_AC; aci++) {
2537 ac = &ic->ic_edca_ac[aci];
2538 qid = athn_ac2qid[aci];
2539
2540 AR_WRITE(sc, AR_DLCL_IFS(qid),
2541 SM(AR_D_LCL_IFS_CWMIN, ATHN_EXP2(ac->ac_ecwmin)) |
2542 SM(AR_D_LCL_IFS_CWMAX, ATHN_EXP2(ac->ac_ecwmax)) |
2543 SM(AR_D_LCL_IFS_AIFS, ac->ac_aifsn));
2544 if (ac->ac_txoplimit != 0) {
2545 AR_WRITE(sc, AR_DCHNTIME(qid),
2546 SM(AR_D_CHNTIME_DUR,
2547 IEEE80211_TXOP_TO_US(ac->ac_txoplimit)) |
2548 AR_D_CHNTIME_EN);
2549 }
2550 else
2551 AR_WRITE(sc, AR_DCHNTIME(qid), 0);
2552 }
2553 AR_WRITE_BARRIER(sc);
2554 #undef ATHN_EXP2
2555 }
2556 #endif /* notyet_edca */
2557
2558 Static int
2559 athn_clock_rate(struct athn_softc *sc)
2560 {
2561 struct ieee80211com *ic = &sc->sc_ic;
2562 int clockrate; /* MHz. */
2563
2564 if (ic->ic_curmode == IEEE80211_MODE_11A) {
2565 if (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)
2566 clockrate = AR_CLOCK_RATE_FAST_5GHZ_OFDM;
2567 else
2568 clockrate = AR_CLOCK_RATE_5GHZ_OFDM;
2569 }
2570 else if (ic->ic_curmode == IEEE80211_MODE_11B) {
2571 clockrate = AR_CLOCK_RATE_CCK;
2572 }
2573 else
2574 clockrate = AR_CLOCK_RATE_2GHZ_OFDM;
2575 #ifndef IEEE80211_NO_HT
2576 if (sc->sc_curchanext != NULL)
2577 clockrate *= 2;
2578 #endif
2579 return clockrate;
2580 }
2581
2582 PUBLIC void
2583 athn_updateslot(struct ifnet *ifp)
2584 {
2585 struct athn_softc *sc = ifp->if_softc;
2586 struct ieee80211com *ic = &sc->sc_ic;
2587 int slot;
2588
2589 slot = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2590 AR_WRITE(sc, AR_D_GBL_IFS_SLOT, slot * athn_clock_rate(sc));
2591 AR_WRITE_BARRIER(sc);
2592 }
2593
2594 Static void
2595 athn_start(struct ifnet *ifp)
2596 {
2597 struct athn_softc *sc = ifp->if_softc;
2598 struct ieee80211com *ic = &sc->sc_ic;
2599 struct ether_header *eh;
2600 struct ieee80211_node *ni;
2601 struct mbuf *m;
2602
2603 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
2604 return;
2605
2606 for (;;) {
2607 if (SIMPLEQ_EMPTY(&sc->sc_txbufs)) {
2608 ifp->if_flags |= IFF_OACTIVE;
2609 break;
2610 }
2611 /* Send pending management frames first. */
2612 IF_DEQUEUE(&ic->ic_mgtq, m);
2613 if (m != NULL) {
2614 ni = (void *)m->m_pkthdr.rcvif;
2615 goto sendit;
2616 }
2617 if (ic->ic_state != IEEE80211_S_RUN)
2618 break;
2619
2620 /* Encapsulate and send data frames. */
2621 IFQ_DEQUEUE(&ifp->if_snd, m);
2622 if (m == NULL)
2623 break;
2624
2625 if (m->m_len < (int)sizeof(*eh) &&
2626 (m = m_pullup(m, sizeof(*eh))) == NULL) {
2627 ifp->if_oerrors++;
2628 continue;
2629 }
2630 eh = mtod(m, struct ether_header *);
2631 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2632 if (ni == NULL) {
2633 m_freem(m);
2634 ifp->if_oerrors++;
2635 continue;
2636 }
2637
2638 bpf_mtap(ifp, m);
2639
2640 if ((m = ieee80211_encap(ic, m, ni)) == NULL)
2641 continue;
2642 sendit:
2643 bpf_mtap3(ic->ic_rawbpf, m);
2644
2645 if (sc->sc_ops.tx(sc, m, ni, 0) != 0) {
2646 ieee80211_free_node(ni);
2647 ifp->if_oerrors++;
2648 continue;
2649 }
2650
2651 sc->sc_tx_timer = 5;
2652 ifp->if_timer = 1;
2653 }
2654 }
2655
2656 Static void
2657 athn_watchdog(struct ifnet *ifp)
2658 {
2659 struct athn_softc *sc = ifp->if_softc;
2660
2661 ifp->if_timer = 0;
2662
2663 if (sc->sc_tx_timer > 0) {
2664 if (--sc->sc_tx_timer == 0) {
2665 aprint_error_dev(sc->sc_dev, "device timeout\n");
2666 athn_stop(ifp, 1);
2667 (void)athn_init(ifp);
2668 ifp->if_oerrors++;
2669 return;
2670 }
2671 ifp->if_timer = 1;
2672 }
2673 ieee80211_watchdog(&sc->sc_ic);
2674 }
2675
2676 #ifdef notyet
2677 Static void
2678 athn_set_multi(struct athn_softc *sc)
2679 {
2680 struct arpcom *ac = &sc->sc_ic.ic_ac;
2681 struct ifnet *ifp = &ac->ac_if;
2682 struct ether_multi *enm;
2683 struct ether_multistep step;
2684 const uint8_t *addr;
2685 uint32_t val, lo, hi;
2686 uint8_t bit;
2687
2688 if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
2689 lo = hi = 0xffffffff;
2690 goto done;
2691 }
2692 lo = hi = 0;
2693 ETHER_FIRST_MULTI(step, ac, enm);
2694 while (enm != NULL) {
2695 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) != 0) {
2696 ifp->if_flags |= IFF_ALLMULTI;
2697 lo = hi = 0xffffffff;
2698 goto done;
2699 }
2700 addr = enm->enm_addrlo;
2701 /* Calculate the XOR value of all eight 6-bit words. */
2702 val = addr[0] | addr[1] << 8 | addr[2] << 16;
2703 bit = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2704 val = addr[3] | addr[4] << 8 | addr[5] << 16;
2705 bit ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2706 bit &= 0x3f;
2707 if (bit < 32)
2708 lo |= 1 << bit;
2709 else
2710 hi |= 1 << (bit - 32);
2711 ETHER_NEXT_MULTI(step, enm);
2712 }
2713 done:
2714 AR_WRITE(sc, AR_MCAST_FIL0, lo);
2715 AR_WRITE(sc, AR_MCAST_FIL1, hi);
2716 AR_WRITE_BARRIER(sc);
2717 }
2718 #endif /* notyet */
2719
2720 Static int
2721 athn_ioctl(struct ifnet *ifp, u_long cmd, void *data)
2722 {
2723 struct athn_softc *sc = ifp->if_softc;
2724 struct ieee80211com *ic = &sc->sc_ic;
2725 int s, error = 0;
2726
2727 s = splnet();
2728
2729 switch (cmd) {
2730 case SIOCSIFFLAGS:
2731 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
2732 break;
2733
2734 switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
2735 case IFF_UP | IFF_RUNNING:
2736 #ifdef notyet
2737 if (((ifp->if_flags ^ sc->sc_if_flags) &
2738 (IFF_ALLMULTI | IFF_PROMISC)) != 0)
2739 /* XXX: setup multi */
2740 #endif
2741 break;
2742 case IFF_UP:
2743 athn_init(ifp);
2744 break;
2745
2746 case IFF_RUNNING:
2747 athn_stop(ifp, 1);
2748 break;
2749 case 0:
2750 default:
2751 break;
2752 }
2753 sc->sc_if_flags = ifp->if_flags;
2754 break;
2755
2756 case SIOCADDMULTI:
2757 case SIOCDELMULTI:
2758 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
2759 /* setup multicast filter, etc */
2760 #ifdef notyet
2761 athn_set_multi(sc);
2762 #endif
2763 error = 0;
2764 }
2765 break;
2766
2767 case SIOCS80211CHANNEL:
2768 error = ieee80211_ioctl(ic, cmd, data);
2769 if (error == ENETRESET &&
2770 ic->ic_opmode == IEEE80211_M_MONITOR) {
2771 if (IS_UP_AND_RUNNING(ifp))
2772 athn_switch_chan(sc, ic->ic_curchan, NULL);
2773 error = 0;
2774 }
2775 break;
2776
2777 default:
2778 error = ieee80211_ioctl(ic, cmd, data);
2779 }
2780
2781 if (error == ENETRESET) {
2782 error = 0;
2783 if (IS_UP_AND_RUNNING(ifp) &&
2784 ic->ic_roaming != IEEE80211_ROAMING_MANUAL) {
2785 athn_stop(ifp, 0);
2786 error = athn_init(ifp);
2787 }
2788 }
2789
2790 splx(s);
2791 return error;
2792 }
2793
2794 Static int
2795 athn_init(struct ifnet *ifp)
2796 {
2797 struct athn_softc *sc = ifp->if_softc;
2798 struct athn_ops *ops = &sc->sc_ops;
2799 struct ieee80211com *ic = &sc->sc_ic;
2800 struct ieee80211_channel *curchan, *extchan;
2801 size_t i;
2802 int error;
2803
2804 if (device_is_active(sc->sc_dev))
2805 athn_stop(ifp, 0); /* XXX: necessary? */
2806 else if (!pmf_device_subtree_resume(sc->sc_dev, &sc->sc_qual) ||
2807 !device_is_active(sc->sc_dev))
2808 return 0;
2809
2810 curchan = ic->ic_curchan;
2811 extchan = NULL;
2812
2813 /* In case a new MAC address has been configured. */
2814 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
2815
2816 #ifdef openbsd_power_management
2817 /* For CardBus, power on the socket. */
2818 if (sc->sc_enable != NULL) {
2819 if ((error = sc->sc_enable(sc)) != 0) {
2820 aprint_error_dev(sc->sc_dev,
2821 "could not enable device\n");
2822 goto fail;
2823 }
2824 if ((error = athn_reset_power_on(sc)) != 0) {
2825 aprint_error_dev(sc->sc_dev,
2826 "could not power on device\n");
2827 goto fail;
2828 }
2829 }
2830 #endif
2831 if (!(sc->sc_flags & ATHN_FLAG_PCIE))
2832 athn_config_nonpcie(sc);
2833 else
2834 athn_config_pcie(sc);
2835
2836 /* Reset HW key cache entries. */
2837 for (i = 0; i < sc->sc_kc_entries; i++)
2838 athn_reset_key(sc, i);
2839
2840 ops->enable_antenna_diversity(sc);
2841
2842 #ifdef ATHN_BT_COEXISTENCE
2843 /* Configure bluetooth coexistence for combo chips. */
2844 if (sc->sc_flags & ATHN_FLAG_BTCOEX)
2845 athn_btcoex_init(sc);
2846 #endif
2847
2848 /* Configure LED. */
2849 athn_led_init(sc);
2850
2851 /* Configure hardware radio switch. */
2852 if (sc->sc_flags & ATHN_FLAG_RFSILENT)
2853 ops->rfsilent_init(sc);
2854
2855 if ((error = athn_hw_reset(sc, curchan, extchan, 1)) != 0) {
2856 aprint_error_dev(sc->sc_dev,
2857 "unable to reset hardware; reset status %d\n", error);
2858 goto fail;
2859 }
2860
2861 /* Enable Rx. */
2862 athn_rx_start(sc);
2863
2864 /* Enable interrupts. */
2865 athn_enable_interrupts(sc);
2866
2867 #ifdef ATHN_BT_COEXISTENCE
2868 /* Enable bluetooth coexistence for combo chips. */
2869 if (sc->sc_flags & ATHN_FLAG_BTCOEX)
2870 athn_btcoex_enable(sc);
2871 #endif
2872
2873 ifp->if_flags &= ~IFF_OACTIVE;
2874 ifp->if_flags |= IFF_RUNNING;
2875
2876 #ifdef notyet
2877 if (ic->ic_flags & IEEE80211_F_WEPON) {
2878 /* Configure WEP keys. */
2879 for (i = 0; i < IEEE80211_WEP_NKID; i++)
2880 athn_set_key(ic, NULL, &ic->ic_nw_keys[i]);
2881 }
2882 #endif
2883 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2884 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2885 else
2886 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2887
2888 return 0;
2889 fail:
2890 athn_stop(ifp, 1);
2891 return error;
2892 }
2893
2894 PUBLIC void
2895 athn_stop(struct ifnet *ifp, int disable)
2896 {
2897 struct athn_softc *sc = ifp->if_softc;
2898 struct ieee80211com *ic = &sc->sc_ic;
2899 int qid;
2900
2901 ifp->if_timer = sc->sc_tx_timer = 0;
2902 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2903
2904 callout_stop(&sc->sc_scan_to);
2905 /* In case we were scanning, release the scan "lock". */
2906 // ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED; /* XXX:??? */
2907
2908 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2909
2910 #ifdef ATHN_BT_COEXISTENCE
2911 /* Disable bluetooth coexistence for combo chips. */
2912 if (sc->sc_flags & ATHN_FLAG_BTCOEX)
2913 athn_btcoex_disable(sc);
2914 #endif
2915
2916 /* Disable interrupts. */
2917 athn_disable_interrupts(sc);
2918 /* Acknowledge interrupts (avoids interrupt storms). */
2919 AR_WRITE(sc, AR_INTR_SYNC_CAUSE, 0xffffffff);
2920 AR_WRITE(sc, AR_INTR_SYNC_MASK, 0);
2921
2922 for (qid = 0; qid < ATHN_QID_COUNT; qid++)
2923 athn_stop_tx_dma(sc, qid);
2924 /* XXX call athn_hw_reset if Tx still pending? */
2925 for (qid = 0; qid < ATHN_QID_COUNT; qid++)
2926 athn_tx_reclaim(sc, qid);
2927
2928 /* Stop Rx. */
2929 AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
2930 AR_WRITE(sc, AR_MIBC, AR_MIBC_FMC);
2931 AR_WRITE(sc, AR_MIBC, AR_MIBC_CMC);
2932 AR_WRITE(sc, AR_FILT_OFDM, 0);
2933 AR_WRITE(sc, AR_FILT_CCK, 0);
2934 AR_WRITE_BARRIER(sc);
2935 athn_set_rxfilter(sc, 0);
2936 athn_stop_rx_dma(sc);
2937
2938 athn_reset(sc, 0);
2939 athn_init_pll(sc, NULL);
2940 athn_set_power_awake(sc);
2941 athn_reset(sc, 1);
2942 athn_init_pll(sc, NULL);
2943
2944 athn_set_power_sleep(sc);
2945
2946 #if 0 /* XXX: shouldn't the pmf stuff take care of this? */
2947 /* For CardBus, power down the socket. */
2948 if (disable && sc->sc_disable != NULL)
2949 sc->sc_disable(sc);
2950 #endif
2951 if (disable)
2952 pmf_device_recursive_suspend(sc->sc_dev, &sc->sc_qual);
2953 }
2954
2955 PUBLIC void
2956 athn_suspend(struct athn_softc *sc)
2957 {
2958 struct ifnet *ifp = &sc->sc_if;
2959
2960 if (ifp->if_flags & IFF_RUNNING)
2961 athn_stop(ifp, 1);
2962 }
2963
2964 PUBLIC int
2965 athn_resume(struct athn_softc *sc)
2966 {
2967 struct ifnet *ifp = &sc->sc_if;
2968
2969 if (ifp->if_flags & IFF_UP)
2970 return athn_init(ifp);
2971 return 0;
2972 }
2973