xref: /netbsd-src/sys/dev/ic/arn9003.c (revision 481d3881954fd794ca5f2d880b68c53a5db8620e)
1*481d3881Srin /*	$NetBSD: arn9003.c,v 1.17 2024/07/05 04:31:50 rin Exp $	*/
264f89611Schristos /*	$OpenBSD: ar9003.c,v 1.25 2012/10/20 09:53:32 stsp Exp $	*/
364f89611Schristos 
464f89611Schristos /*-
564f89611Schristos  * Copyright (c) 2010 Damien Bergamini <damien.bergamini@free.fr>
664f89611Schristos  * Copyright (c) 2010 Atheros Communications Inc.
764f89611Schristos  *
864f89611Schristos  * Permission to use, copy, modify, and/or distribute this software for any
964f89611Schristos  * purpose with or without fee is hereby granted, provided that the above
1064f89611Schristos  * copyright notice and this permission notice appear in all copies.
1164f89611Schristos  *
1264f89611Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1364f89611Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1464f89611Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1564f89611Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1664f89611Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1764f89611Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1864f89611Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1964f89611Schristos  */
2064f89611Schristos 
2164f89611Schristos /*
2264f89611Schristos  * Driver for Atheros 802.11a/g/n chipsets.
2364f89611Schristos  * Routines for AR9003 family.
2464f89611Schristos  */
2564f89611Schristos 
2664f89611Schristos #include <sys/cdefs.h>
27*481d3881Srin __KERNEL_RCSID(0, "$NetBSD: arn9003.c,v 1.17 2024/07/05 04:31:50 rin Exp $");
2864f89611Schristos 
2964f89611Schristos #include <sys/param.h>
3064f89611Schristos #include <sys/sockio.h>
3164f89611Schristos #include <sys/mbuf.h>
3264f89611Schristos #include <sys/kernel.h>
3364f89611Schristos #include <sys/socket.h>
3464f89611Schristos #include <sys/systm.h>
3564f89611Schristos #include <sys/malloc.h>
3664f89611Schristos #include <sys/queue.h>
3764f89611Schristos #include <sys/callout.h>
3864f89611Schristos #include <sys/conf.h>
3964f89611Schristos #include <sys/device.h>
4064f89611Schristos 
4164f89611Schristos #include <sys/bus.h>
4264f89611Schristos #include <sys/endian.h>
4364f89611Schristos #include <sys/intr.h>
4464f89611Schristos 
4564f89611Schristos #include <net/bpf.h>
4664f89611Schristos #include <net/if.h>
4764f89611Schristos #include <net/if_arp.h>
4864f89611Schristos #include <net/if_dl.h>
4902421171Schristos #include <net/if_ether.h>
5064f89611Schristos #include <net/if_media.h>
5164f89611Schristos #include <net/if_types.h>
5264f89611Schristos 
5364f89611Schristos #include <netinet/in.h>
5464f89611Schristos #include <netinet/in_systm.h>
5564f89611Schristos #include <netinet/in_var.h>
5664f89611Schristos #include <netinet/ip.h>
5764f89611Schristos 
5864f89611Schristos #include <net80211/ieee80211_var.h>
5964f89611Schristos #include <net80211/ieee80211_amrr.h>
6064f89611Schristos #include <net80211/ieee80211_radiotap.h>
6164f89611Schristos 
6264f89611Schristos #include <dev/ic/athnreg.h>
6364f89611Schristos #include <dev/ic/athnvar.h>
6464f89611Schristos #include <dev/ic/arn9003reg.h>
6564f89611Schristos #include <dev/ic/arn9003.h>
6664f89611Schristos 
6764f89611Schristos #define Static static
6864f89611Schristos 
6964f89611Schristos Static void	ar9003_calib_iq(struct athn_softc *);
7064f89611Schristos Static int	ar9003_calib_tx_iq(struct athn_softc *);
7164f89611Schristos Static int	ar9003_compute_predistortion(struct athn_softc *,
7264f89611Schristos 		    const uint32_t *, const uint32_t *);
7364f89611Schristos Static void	ar9003_disable_ofdm_weak_signal(struct athn_softc *);
7464f89611Schristos Static void	ar9003_disable_phy(struct athn_softc *);
7564f89611Schristos Static int	ar9003_dma_alloc(struct athn_softc *);
7664f89611Schristos Static void	ar9003_dma_free(struct athn_softc *);
7764f89611Schristos Static void	ar9003_do_calib(struct athn_softc *);
7864f89611Schristos Static void	ar9003_do_noisefloor_calib(struct athn_softc *);
7964f89611Schristos Static void	ar9003_enable_antenna_diversity(struct athn_softc *);
8064f89611Schristos Static void	ar9003_enable_ofdm_weak_signal(struct athn_softc *);
8164f89611Schristos Static void	ar9003_enable_predistorter(struct athn_softc *, int);
8264f89611Schristos Static int	ar9003_find_rom(struct athn_softc *);
8364f89611Schristos Static void	ar9003_force_txgain(struct athn_softc *, uint32_t);
8464f89611Schristos Static int	ar9003_get_desired_txgain(struct athn_softc *, int, int);
85f8570f8aSmrg Static int	ar9003_get_iq_corr(struct athn_softc *, int32_t[6], int32_t[2]);
8664f89611Schristos Static void	ar9003_gpio_config_input(struct athn_softc *, int);
8764f89611Schristos Static void	ar9003_gpio_config_output(struct athn_softc *, int, int);
8864f89611Schristos Static int	ar9003_gpio_read(struct athn_softc *, int);
8964f89611Schristos Static void	ar9003_gpio_write(struct athn_softc *, int, int);
9064f89611Schristos Static void	ar9003_hw_init(struct athn_softc *, struct ieee80211_channel *,
9164f89611Schristos 		    struct ieee80211_channel *);
9264f89611Schristos Static void	ar9003_init_baseband(struct athn_softc *);
9364f89611Schristos Static void	ar9003_init_chains(struct athn_softc *);
945ffb0503Snonaka Static int	ar9003_intr_status(struct athn_softc *);
9564f89611Schristos Static int	ar9003_intr(struct athn_softc *);
9664f89611Schristos Static void	ar9003_next_calib(struct athn_softc *);
9764f89611Schristos Static void	ar9003_paprd_enable(struct athn_softc *);
9864f89611Schristos Static int	ar9003_paprd_tx_tone(struct athn_softc *);
9964f89611Schristos Static void	ar9003_paprd_tx_tone_done(struct athn_softc *);
10064f89611Schristos Static int	ar9003_read_eep_data(struct athn_softc *, uint32_t, void *,
10164f89611Schristos 		    int);
10264f89611Schristos Static int	ar9003_read_eep_word(struct athn_softc *, uint32_t,
10364f89611Schristos 		    uint16_t *);
10464f89611Schristos Static int	ar9003_read_otp_data(struct athn_softc *, uint32_t, void *,
10564f89611Schristos 		    int);
10664f89611Schristos Static int	ar9003_read_otp_word(struct athn_softc *, uint32_t,
10764f89611Schristos 		    uint32_t *);
10864f89611Schristos Static int	ar9003_read_rom(struct athn_softc *);
10964f89611Schristos Static void	ar9003_reset_rx_gain(struct athn_softc *,
11064f89611Schristos 		    struct ieee80211_channel *);
11164f89611Schristos Static void	ar9003_reset_tx_gain(struct athn_softc *,
11264f89611Schristos 		    struct ieee80211_channel *);
11364f89611Schristos Static int	ar9003_restore_rom_block(struct athn_softc *, uint8_t,
11464f89611Schristos 		    uint8_t, const uint8_t *, size_t);
11564f89611Schristos Static void	ar9003_rf_bus_release(struct athn_softc *);
11664f89611Schristos Static int	ar9003_rf_bus_request(struct athn_softc *);
11764f89611Schristos Static void	ar9003_rfsilent_init(struct athn_softc *);
11864f89611Schristos Static int	ar9003_rx_alloc(struct athn_softc *, int, int);
11964f89611Schristos Static void	ar9003_rx_enable(struct athn_softc *);
12064f89611Schristos Static void	ar9003_rx_free(struct athn_softc *, int);
12164f89611Schristos Static void	ar9003_rx_intr(struct athn_softc *, int);
12264f89611Schristos Static int	ar9003_rx_process(struct athn_softc *, int);
12364f89611Schristos Static void	ar9003_rx_radiotap(struct athn_softc *, struct mbuf *,
12464f89611Schristos 		    struct ar_rx_status *);
12564f89611Schristos Static void	ar9003_set_cck_weak_signal(struct athn_softc *, int);
12664f89611Schristos Static void	ar9003_set_delta_slope(struct athn_softc *,
12764f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
12864f89611Schristos Static void	ar9003_set_firstep_level(struct athn_softc *, int);
12964f89611Schristos Static void	ar9003_set_noise_immunity_level(struct athn_softc *, int);
13064f89611Schristos Static void	ar9003_set_phy(struct athn_softc *, struct ieee80211_channel *,
13164f89611Schristos 		    struct ieee80211_channel *);
13264f89611Schristos Static void	ar9003_set_rf_mode(struct athn_softc *,
13364f89611Schristos 		    struct ieee80211_channel *);
13464f89611Schristos Static void	ar9003_set_rxchains(struct athn_softc *);
13564f89611Schristos Static void	ar9003_set_spur_immunity_level(struct athn_softc *, int);
13664f89611Schristos Static void	ar9003_set_training_gain(struct athn_softc *, int);
13764f89611Schristos Static int	ar9003_swba_intr(struct athn_softc *);
13864f89611Schristos Static int	ar9003_tx(struct athn_softc *, struct mbuf *,
13964f89611Schristos 		    struct ieee80211_node *, int);
14064f89611Schristos Static int	ar9003_tx_alloc(struct athn_softc *);
14164f89611Schristos Static void	ar9003_tx_free(struct athn_softc *);
14264f89611Schristos Static void	ar9003_tx_intr(struct athn_softc *);
14364f89611Schristos Static int	ar9003_tx_process(struct athn_softc *);
14464f89611Schristos 
14564f89611Schristos #ifdef notused
14664f89611Schristos Static void	ar9003_bb_load_noisefloor(struct athn_softc *);
14764f89611Schristos Static void	ar9003_get_noisefloor(struct athn_softc *,
14864f89611Schristos 		    struct ieee80211_channel *);
14964f89611Schristos Static void	ar9003_paprd_calib(struct athn_softc *,
15064f89611Schristos 		    struct ieee80211_channel *);
15164f89611Schristos Static void	ar9003_read_noisefloor(struct athn_softc *, int16_t *,
15264f89611Schristos 		    int16_t *);
15364f89611Schristos Static void	ar9003_write_noisefloor(struct athn_softc *, int16_t *,
15464f89611Schristos 		    int16_t *);
15564f89611Schristos Static void	ar9300_noisefloor_calib(struct athn_softc *);
15664f89611Schristos #endif /* notused */
15764f89611Schristos 
15864f89611Schristos /*
15964f89611Schristos  * XXX: See if_iwn.c:MCLGETIalt() for a better solution.
16064f89611Schristos  * XXX: Put this in a header or in athn.c so it can be shared between
16164f89611Schristos  *      ar5008.c and ar9003.c?
16264f89611Schristos  */
16364f89611Schristos static struct mbuf *
MCLGETI(struct athn_softc * sc __unused,int how,struct ifnet * ifp __unused,u_int size)16464f89611Schristos MCLGETI(struct athn_softc *sc __unused, int how,
16564f89611Schristos     struct ifnet *ifp __unused, u_int size)
16664f89611Schristos {
16764f89611Schristos 	struct mbuf *m;
16864f89611Schristos 
16964f89611Schristos 	MGETHDR(m, how, MT_DATA);
17064f89611Schristos 	if (m == NULL)
17164f89611Schristos 		return NULL;
17264f89611Schristos 
17364f89611Schristos 	MEXTMALLOC(m, size, how);
17464f89611Schristos 	if ((m->m_flags & M_EXT) == 0) {
17564f89611Schristos 		m_freem(m);
17664f89611Schristos 		return NULL;
17764f89611Schristos 	}
17864f89611Schristos 	return m;
17964f89611Schristos }
18064f89611Schristos 
18164f89611Schristos PUBLIC int
ar9003_attach(struct athn_softc * sc)18264f89611Schristos ar9003_attach(struct athn_softc *sc)
18364f89611Schristos {
18464f89611Schristos 	struct athn_ops *ops = &sc->sc_ops;
18564f89611Schristos 	int error;
18664f89611Schristos 
18764f89611Schristos 	/* Set callbacks for AR9003 family. */
18864f89611Schristos 	ops->gpio_read = ar9003_gpio_read;
18964f89611Schristos 	ops->gpio_write = ar9003_gpio_write;
19064f89611Schristos 	ops->gpio_config_input = ar9003_gpio_config_input;
19164f89611Schristos 	ops->gpio_config_output = ar9003_gpio_config_output;
19264f89611Schristos 	ops->rfsilent_init = ar9003_rfsilent_init;
19364f89611Schristos 
19464f89611Schristos 	ops->dma_alloc = ar9003_dma_alloc;
19564f89611Schristos 	ops->dma_free = ar9003_dma_free;
19664f89611Schristos 	ops->rx_enable = ar9003_rx_enable;
1975ffb0503Snonaka 	ops->intr_status = ar9003_intr_status;
19864f89611Schristos 	ops->intr = ar9003_intr;
19964f89611Schristos 	ops->tx = ar9003_tx;
20064f89611Schristos 
20164f89611Schristos 	ops->set_rf_mode = ar9003_set_rf_mode;
20264f89611Schristos 	ops->rf_bus_request = ar9003_rf_bus_request;
20364f89611Schristos 	ops->rf_bus_release = ar9003_rf_bus_release;
20464f89611Schristos 	ops->set_phy = ar9003_set_phy;
20564f89611Schristos 	ops->set_delta_slope = ar9003_set_delta_slope;
20664f89611Schristos 	ops->enable_antenna_diversity = ar9003_enable_antenna_diversity;
20764f89611Schristos 	ops->init_baseband = ar9003_init_baseband;
20864f89611Schristos 	ops->disable_phy = ar9003_disable_phy;
20964f89611Schristos 	ops->set_rxchains = ar9003_set_rxchains;
21064f89611Schristos 	ops->noisefloor_calib = ar9003_do_noisefloor_calib;
21164f89611Schristos 	ops->do_calib = ar9003_do_calib;
21264f89611Schristos 	ops->next_calib = ar9003_next_calib;
21364f89611Schristos 	ops->hw_init = ar9003_hw_init;
21464f89611Schristos 
21564f89611Schristos 	ops->set_noise_immunity_level = ar9003_set_noise_immunity_level;
21664f89611Schristos 	ops->enable_ofdm_weak_signal = ar9003_enable_ofdm_weak_signal;
21764f89611Schristos 	ops->disable_ofdm_weak_signal = ar9003_disable_ofdm_weak_signal;
21864f89611Schristos 	ops->set_cck_weak_signal = ar9003_set_cck_weak_signal;
21964f89611Schristos 	ops->set_firstep_level = ar9003_set_firstep_level;
22064f89611Schristos 	ops->set_spur_immunity_level = ar9003_set_spur_immunity_level;
22164f89611Schristos 
22264f89611Schristos 	/* Set MAC registers offsets. */
22364f89611Schristos 	sc->sc_obs_off = AR_OBS;
22464f89611Schristos 	sc->sc_gpio_input_en_off = AR_GPIO_INPUT_EN_VAL;
22564f89611Schristos 
22664f89611Schristos 	if (!(sc->sc_flags & ATHN_FLAG_PCIE))
22764f89611Schristos 		athn_config_nonpcie(sc);
22864f89611Schristos 	else
22964f89611Schristos 		athn_config_pcie(sc);
23064f89611Schristos 
23164f89611Schristos 	/* Determine ROM type and location. */
23264f89611Schristos 	if ((error = ar9003_find_rom(sc)) != 0) {
233dabffed6Schristos 		aprint_error_dev(sc->sc_dev, "could not find ROM\n");
23464f89611Schristos 		return error;
23564f89611Schristos 	}
23664f89611Schristos 	/* Read entire ROM content in memory. */
23764f89611Schristos 	if ((error = ar9003_read_rom(sc)) != 0) {
238dabffed6Schristos 		aprint_error_dev(sc->sc_dev, "could not read ROM\n");
23964f89611Schristos 		return error;
24064f89611Schristos 	}
24164f89611Schristos 
24264f89611Schristos 	/* Determine if it is a non-enterprise AR9003 card. */
24364f89611Schristos 	if (AR_READ(sc, AR_ENT_OTP) & AR_ENT_OTP_MPSD)
24464f89611Schristos 		sc->sc_flags |= ATHN_FLAG_NON_ENTERPRISE;
24564f89611Schristos 
24664f89611Schristos 	ops->setup(sc);
24764f89611Schristos 	return 0;
24864f89611Schristos }
24964f89611Schristos 
25064f89611Schristos /*
25164f89611Schristos  * Read 16-bit word from EEPROM.
25264f89611Schristos  */
25364f89611Schristos Static int
ar9003_read_eep_word(struct athn_softc * sc,uint32_t addr,uint16_t * val)25464f89611Schristos ar9003_read_eep_word(struct athn_softc *sc, uint32_t addr, uint16_t *val)
25564f89611Schristos {
25664f89611Schristos 	uint32_t reg;
25764f89611Schristos 	int ntries;
25864f89611Schristos 
25964f89611Schristos 	reg = AR_READ(sc, AR_EEPROM_OFFSET(addr));
26064f89611Schristos 	for (ntries = 0; ntries < 1000; ntries++) {
26164f89611Schristos 		reg = AR_READ(sc, AR_EEPROM_STATUS_DATA);
26264f89611Schristos 		if (!(reg & (AR_EEPROM_STATUS_DATA_BUSY |
26364f89611Schristos 		    AR_EEPROM_STATUS_DATA_PROT_ACCESS))) {
26464f89611Schristos 			*val = MS(reg, AR_EEPROM_STATUS_DATA_VAL);
26564f89611Schristos 			return 0;
26664f89611Schristos 		}
26764f89611Schristos 		DELAY(10);
26864f89611Schristos 	}
26964f89611Schristos 	*val = 0xffff;
27064f89611Schristos 	return ETIMEDOUT;
27164f89611Schristos }
27264f89611Schristos 
27364f89611Schristos /*
27464f89611Schristos  * Read an arbitrary number of bytes at a specified address in EEPROM.
27564f89611Schristos  * NB: The address may not be 16-bit aligned.
27664f89611Schristos  */
27764f89611Schristos Static int
ar9003_read_eep_data(struct athn_softc * sc,uint32_t addr,void * buf,int len)27864f89611Schristos ar9003_read_eep_data(struct athn_softc *sc, uint32_t addr, void *buf, int len)
27964f89611Schristos {
28064f89611Schristos 	uint8_t *dst = buf;
28164f89611Schristos 	uint16_t val;
28264f89611Schristos 	int error;
28364f89611Schristos 
28464f89611Schristos 	if (len > 0 && (addr & 1)) {
28564f89611Schristos 		/* Deal with non-aligned reads. */
28664f89611Schristos 		addr >>= 1;
28764f89611Schristos 		error = ar9003_read_eep_word(sc, addr, &val);
28864f89611Schristos 		if (error != 0)
28964f89611Schristos 			return error;
29064f89611Schristos 		*dst++ = val & 0xff;
29164f89611Schristos 		addr--;
29264f89611Schristos 		len--;
29364f89611Schristos 	}
29464f89611Schristos 	else
29564f89611Schristos 		addr >>= 1;
29664f89611Schristos 	for (; len >= 2; addr--, len -= 2) {
29764f89611Schristos 		error = ar9003_read_eep_word(sc, addr, &val);
29864f89611Schristos 		if (error != 0)
29964f89611Schristos 			return error;
30064f89611Schristos 		*dst++ = val >> 8;
30164f89611Schristos 		*dst++ = val & 0xff;
30264f89611Schristos 	}
30364f89611Schristos 	if (len > 0) {
30464f89611Schristos 		error = ar9003_read_eep_word(sc, addr, &val);
30564f89611Schristos 		if (error != 0)
30664f89611Schristos 			return error;
30764f89611Schristos 		*dst++ = val >> 8;
30864f89611Schristos 	}
30964f89611Schristos 	return 0;
31064f89611Schristos }
31164f89611Schristos 
31264f89611Schristos /*
31364f89611Schristos  * Read 32-bit word from OTPROM.
31464f89611Schristos  */
31564f89611Schristos Static int
ar9003_read_otp_word(struct athn_softc * sc,uint32_t addr,uint32_t * val)31664f89611Schristos ar9003_read_otp_word(struct athn_softc *sc, uint32_t addr, uint32_t *val)
31764f89611Schristos {
31864f89611Schristos 	uint32_t reg;
31964f89611Schristos 	int ntries;
32064f89611Schristos 
32164f89611Schristos 	reg = AR_READ(sc, AR_OTP_BASE(addr));
32264f89611Schristos 	for (ntries = 0; ntries < 1000; ntries++) {
32364f89611Schristos 		reg = AR_READ(sc, AR_OTP_STATUS);
32464f89611Schristos 		if (MS(reg, AR_OTP_STATUS_TYPE) == AR_OTP_STATUS_VALID) {
32564f89611Schristos 			*val = AR_READ(sc, AR_OTP_READ_DATA);
32664f89611Schristos 			return 0;
32764f89611Schristos 		}
32864f89611Schristos 		DELAY(10);
32964f89611Schristos 	}
33064f89611Schristos 	return ETIMEDOUT;
33164f89611Schristos }
33264f89611Schristos 
33364f89611Schristos /*
33464f89611Schristos  * Read an arbitrary number of bytes at a specified address in OTPROM.
33564f89611Schristos  * NB: The address may not be 32-bit aligned.
33664f89611Schristos  */
33764f89611Schristos Static int
ar9003_read_otp_data(struct athn_softc * sc,uint32_t addr,void * buf,int len)33864f89611Schristos ar9003_read_otp_data(struct athn_softc *sc, uint32_t addr, void *buf, int len)
33964f89611Schristos {
34064f89611Schristos 	uint8_t *dst = buf;
34164f89611Schristos 	uint32_t val;
34264f89611Schristos 	int error;
34364f89611Schristos 
34464f89611Schristos 	/* NB: not optimal for non-aligned reads, but correct. */
34564f89611Schristos 	for (; len > 0; addr--, len--) {
34664f89611Schristos 		error = ar9003_read_otp_word(sc, addr >> 2, &val);
34764f89611Schristos 		if (error != 0)
34864f89611Schristos 			return error;
34964f89611Schristos 		*dst++ = (val >> ((addr & 3) * 8)) & 0xff;
35064f89611Schristos 	}
35164f89611Schristos 	return 0;
35264f89611Schristos }
35364f89611Schristos 
35464f89611Schristos /*
35564f89611Schristos  * Determine if the chip has an external EEPROM or an OTPROM and its size.
35664f89611Schristos  */
35764f89611Schristos Static int
ar9003_find_rom(struct athn_softc * sc)35864f89611Schristos ar9003_find_rom(struct athn_softc *sc)
35964f89611Schristos {
36064f89611Schristos 	struct athn_ops *ops = &sc->sc_ops;
36164f89611Schristos 	uint32_t hdr;
36264f89611Schristos 	int error;
36364f89611Schristos 
36464f89611Schristos 	/* Try EEPROM. */
36564f89611Schristos 	ops->read_rom_data = ar9003_read_eep_data;
36664f89611Schristos 
36764f89611Schristos 	sc->sc_eep_size = AR_SREV_9485(sc) ? 4096 : 1024;
36864f89611Schristos 	sc->sc_eep_base = sc->sc_eep_size - 1;
36964f89611Schristos 	error = ops->read_rom_data(sc, sc->sc_eep_base, &hdr, sizeof(hdr));
37064f89611Schristos 	if (error == 0 && hdr != 0 && hdr != 0xffffffff)
37164f89611Schristos 		return 0;
37264f89611Schristos 
37364f89611Schristos 	sc->sc_eep_size = 512;
37464f89611Schristos 	sc->sc_eep_base = sc->sc_eep_size - 1;
37564f89611Schristos 	error = ops->read_rom_data(sc, sc->sc_eep_base, &hdr, sizeof(hdr));
37664f89611Schristos 	if (error == 0 && hdr != 0 && hdr != 0xffffffff)
37764f89611Schristos 		return 0;
37864f89611Schristos 
37964f89611Schristos 	/* Try OTPROM. */
38064f89611Schristos 	ops->read_rom_data = ar9003_read_otp_data;
38164f89611Schristos 
38264f89611Schristos 	sc->sc_eep_size = 1024;
38364f89611Schristos 	sc->sc_eep_base = sc->sc_eep_size - 1;
38464f89611Schristos 	error = ops->read_rom_data(sc, sc->sc_eep_base, &hdr, sizeof(hdr));
38564f89611Schristos 	if (error == 0 && hdr != 0 && hdr != 0xffffffff)
38664f89611Schristos 		return 0;
38764f89611Schristos 
38864f89611Schristos 	sc->sc_eep_size = 512;
38964f89611Schristos 	sc->sc_eep_base = sc->sc_eep_size - 1;
39064f89611Schristos 	error = ops->read_rom_data(sc, sc->sc_eep_base, &hdr, sizeof(hdr));
39164f89611Schristos 	if (error == 0 && hdr != 0 && hdr != 0xffffffff)
39264f89611Schristos 		return 0;
39364f89611Schristos 
39464f89611Schristos 	return EIO;	/* Not found. */
39564f89611Schristos }
39664f89611Schristos 
39764f89611Schristos Static int
ar9003_restore_rom_block(struct athn_softc * sc,uint8_t alg,uint8_t ref,const uint8_t * buf,size_t len)39864f89611Schristos ar9003_restore_rom_block(struct athn_softc *sc, uint8_t alg, uint8_t ref,
39964f89611Schristos     const uint8_t *buf, size_t len)
40064f89611Schristos {
40164f89611Schristos 	const uint8_t *def, *ptr, *end;
40264f89611Schristos 	uint8_t *eep = sc->sc_eep;
40364f89611Schristos 	size_t off, clen;
40464f89611Schristos 
40564f89611Schristos 	if (alg == AR_EEP_COMPRESS_BLOCK) {
40664f89611Schristos 		/* Block contains chunks that shadow ROM template. */
40764f89611Schristos 		def = sc->sc_ops.get_rom_template(sc, ref);
40864f89611Schristos 		if (def == NULL) {
40964f89611Schristos 			DPRINTFN(DBG_INIT, sc, "unknown template image %d\n",
41064f89611Schristos 			    ref);
41164f89611Schristos 			return EINVAL;
41264f89611Schristos 		}
41364f89611Schristos 		/* Start with template. */
41464f89611Schristos 		memcpy(eep, def, sc->sc_eep_size);
41564f89611Schristos 		/* Shadow template with chunks. */
41664f89611Schristos 		off = 0;	/* Offset in ROM image. */
41764f89611Schristos 		ptr = buf;	/* Offset in block. */
41864f89611Schristos 		end = buf + len;
41964f89611Schristos 		/* Process chunks. */
42064f89611Schristos 		while (ptr + 2 <= end) {
42164f89611Schristos 			off += *ptr++;	/* Gap with previous chunk. */
42264f89611Schristos 			clen = *ptr++;	/* Chunk length. */
42364f89611Schristos 			/* Make sure block is large enough. */
42464f89611Schristos 			if (ptr + clen > end)
42564f89611Schristos 				return EINVAL;
42664f89611Schristos 			/* Make sure chunk fits in ROM image. */
42764f89611Schristos 			if (off + clen > sc->sc_eep_size)
42864f89611Schristos 				return EINVAL;
42964f89611Schristos 			/* Restore chunk. */
43064f89611Schristos 			DPRINTFN(DBG_INIT, sc, "ROM chunk @%zd/%zd\n",
43164f89611Schristos 			    off, clen);
43264f89611Schristos 			memcpy(&eep[off], ptr, clen);
43364f89611Schristos 			ptr += clen;
43464f89611Schristos 			off += clen;
43564f89611Schristos 		}
43664f89611Schristos 	}
43764f89611Schristos 	else if (alg == AR_EEP_COMPRESS_NONE) {
43864f89611Schristos 		/* Block contains full ROM image. */
43964f89611Schristos 		if (len != sc->sc_eep_size) {
44064f89611Schristos 			DPRINTFN(DBG_INIT, sc, "block length mismatch %zd\n",
44164f89611Schristos 			    len);
44264f89611Schristos 			return EINVAL;
44364f89611Schristos 		}
44464f89611Schristos 		memcpy(eep, buf, len);
44564f89611Schristos 	}
44664f89611Schristos 	return 0;
44764f89611Schristos }
44864f89611Schristos 
44964f89611Schristos Static int
ar9003_read_rom(struct athn_softc * sc)45064f89611Schristos ar9003_read_rom(struct athn_softc *sc)
45164f89611Schristos {
45264f89611Schristos 	struct athn_ops *ops = &sc->sc_ops;
45364f89611Schristos 	uint8_t *buf, *ptr, alg, ref;
45464f89611Schristos 	uint16_t sum, rsum;
45564f89611Schristos 	uint32_t hdr;
45664f89611Schristos 	int error, addr;
45764f89611Schristos 	size_t len, i, j;
45864f89611Schristos 
45964f89611Schristos 	/* Allocate space to store ROM in host memory. */
460d47bcd29Schs 	sc->sc_eep = malloc(sc->sc_eep_size, M_DEVBUF, M_WAITOK);
46164f89611Schristos 
46264f89611Schristos 	/* Allocate temporary buffer to store ROM blocks. */
463d47bcd29Schs 	buf = malloc(2048, M_DEVBUF, M_WAITOK);
46464f89611Schristos 
46564f89611Schristos 	/* Restore vendor-specified ROM blocks. */
46664f89611Schristos 	addr = sc->sc_eep_base;
46764f89611Schristos 	for (i = 0; i < 100; i++) {
46864f89611Schristos 		/* Read block header. */
46964f89611Schristos 		error = ops->read_rom_data(sc, addr, &hdr, sizeof(hdr));
47064f89611Schristos 		if (error != 0)
47164f89611Schristos 			break;
47264f89611Schristos 		if (hdr == 0 || hdr == 0xffffffff)
47364f89611Schristos 			break;
47464f89611Schristos 		addr -= sizeof(hdr);
47564f89611Schristos 
47664f89611Schristos 		/* Extract bits from header. */
47764f89611Schristos 		ptr = (uint8_t *)&hdr;
47864f89611Schristos 		alg = (ptr[0] & 0xe0) >> 5;
47964f89611Schristos 		ref = (ptr[1] & 0x80) >> 2 | (ptr[0] & 0x1f);
48064f89611Schristos 		len = (ptr[1] & 0x7f) << 4 | (ptr[2] & 0xf0) >> 4;
48164f89611Schristos 		DPRINTFN(DBG_INIT, sc,
48264f89611Schristos 		    "ROM block %zd: alg=%d ref=%d len=%zd\n",
48364f89611Schristos 		    i, alg, ref, len);
48464f89611Schristos 
48564f89611Schristos 		/* Read block data (len <= 0x7ff). */
48664f89611Schristos 		error = ops->read_rom_data(sc, addr, buf, len);
48764f89611Schristos 		if (error != 0)
48864f89611Schristos 			break;
48964f89611Schristos 		addr -= len;
49064f89611Schristos 
49164f89611Schristos 		/* Read block checksum. */
49264f89611Schristos 		error = ops->read_rom_data(sc, addr, &sum, sizeof(sum));
49364f89611Schristos 		if (error != 0)
49464f89611Schristos 			break;
49564f89611Schristos 		addr -= sizeof(sum);
49664f89611Schristos 
49764f89611Schristos 		/* Compute block checksum. */
49864f89611Schristos 		rsum = 0;
49964f89611Schristos 		for (j = 0; j < len; j++)
50064f89611Schristos 			rsum += buf[j];
50164f89611Schristos 		/* Compare to that in ROM. */
50264f89611Schristos 		if (le16toh(sum) != rsum) {
50364f89611Schristos 			DPRINTFN(DBG_INIT, sc,
50464f89611Schristos 			    "bad block checksum 0x%x/0x%x\n",
50564f89611Schristos 			    le16toh(sum), rsum);
50664f89611Schristos 			continue;	/* Skip bad block. */
50764f89611Schristos 		}
50864f89611Schristos 		/* Checksum is correct, restore block. */
50964f89611Schristos 		ar9003_restore_rom_block(sc, alg, ref, buf, len);
51064f89611Schristos 	}
51164f89611Schristos #if BYTE_ORDER == BIG_ENDIAN
51264f89611Schristos 	/* NB: ROM is always little endian. */
51364f89611Schristos 	if (error == 0)
51464f89611Schristos 		ops->swap_rom(sc);
51564f89611Schristos #endif
51664f89611Schristos 	free(buf, M_DEVBUF);
51764f89611Schristos 	return error;
51864f89611Schristos }
51964f89611Schristos 
52064f89611Schristos /*
52164f89611Schristos  * Access to General Purpose Input/Output ports.
52264f89611Schristos  */
52364f89611Schristos Static int
ar9003_gpio_read(struct athn_softc * sc,int pin)52464f89611Schristos ar9003_gpio_read(struct athn_softc *sc, int pin)
52564f89611Schristos {
52664f89611Schristos 
52764f89611Schristos 	KASSERT(pin < sc->sc_ngpiopins);
52864f89611Schristos 	return ((AR_READ(sc, AR_GPIO_IN) & AR9300_GPIO_IN_VAL) &
52964f89611Schristos 	    (1 << pin)) != 0;
53064f89611Schristos }
53164f89611Schristos 
53264f89611Schristos Static void
ar9003_gpio_write(struct athn_softc * sc,int pin,int set)53364f89611Schristos ar9003_gpio_write(struct athn_softc *sc, int pin, int set)
53464f89611Schristos {
53564f89611Schristos 	uint32_t reg;
53664f89611Schristos 
53764f89611Schristos 	KASSERT(pin < sc->sc_ngpiopins);
53864f89611Schristos 	reg = AR_READ(sc, AR_GPIO_IN_OUT);
53964f89611Schristos 	if (set)
54064f89611Schristos 		reg |= 1 << pin;
54164f89611Schristos 	else
54264f89611Schristos 		reg &= ~(1 << pin);
54364f89611Schristos 	AR_WRITE(sc, AR_GPIO_IN_OUT, reg);
54464f89611Schristos 	AR_WRITE_BARRIER(sc);
54564f89611Schristos }
54664f89611Schristos 
54764f89611Schristos Static void
ar9003_gpio_config_input(struct athn_softc * sc,int pin)54864f89611Schristos ar9003_gpio_config_input(struct athn_softc *sc, int pin)
54964f89611Schristos {
55064f89611Schristos 	uint32_t reg;
55164f89611Schristos 
55264f89611Schristos 	reg = AR_READ(sc, AR_GPIO_OE_OUT);
55364f89611Schristos 	reg &= ~(AR_GPIO_OE_OUT_DRV_M << (pin * 2));
55464f89611Schristos 	reg |= AR_GPIO_OE_OUT_DRV_NO << (pin * 2);
55564f89611Schristos 	AR_WRITE(sc, AR_GPIO_OE_OUT, reg);
55664f89611Schristos 	AR_WRITE_BARRIER(sc);
55764f89611Schristos }
55864f89611Schristos 
55964f89611Schristos Static void
ar9003_gpio_config_output(struct athn_softc * sc,int pin,int type)56064f89611Schristos ar9003_gpio_config_output(struct athn_softc *sc, int pin, int type)
56164f89611Schristos {
56264f89611Schristos 	uint32_t reg;
56364f89611Schristos 	int mux, off;
56464f89611Schristos 
56564f89611Schristos 	mux = pin / 6;
56664f89611Schristos 	off = pin % 6;
56764f89611Schristos 
56864f89611Schristos 	reg = AR_READ(sc, AR_GPIO_OUTPUT_MUX(mux));
56964f89611Schristos 	reg &= ~(0x1f << (off * 5));
57064f89611Schristos 	reg |= (type & 0x1f) << (off * 5);
57164f89611Schristos 	AR_WRITE(sc, AR_GPIO_OUTPUT_MUX(mux), reg);
57264f89611Schristos 
57364f89611Schristos 	reg = AR_READ(sc, AR_GPIO_OE_OUT);
57464f89611Schristos 	reg &= ~(AR_GPIO_OE_OUT_DRV_M << (pin * 2));
57564f89611Schristos 	reg |= AR_GPIO_OE_OUT_DRV_ALL << (pin * 2);
57664f89611Schristos 	AR_WRITE(sc, AR_GPIO_OE_OUT, reg);
57764f89611Schristos 	AR_WRITE_BARRIER(sc);
57864f89611Schristos }
57964f89611Schristos 
58064f89611Schristos Static void
ar9003_rfsilent_init(struct athn_softc * sc)58164f89611Schristos ar9003_rfsilent_init(struct athn_softc *sc)
58264f89611Schristos {
58364f89611Schristos 	uint32_t reg;
58464f89611Schristos 
58564f89611Schristos 	/* Configure hardware radio switch. */
58664f89611Schristos 	AR_SETBITS(sc, AR_GPIO_INPUT_EN_VAL, AR_GPIO_INPUT_EN_VAL_RFSILENT_BB);
58764f89611Schristos 	reg = AR_READ(sc, AR_GPIO_INPUT_MUX2);
58864f89611Schristos 	reg = RW(reg, AR_GPIO_INPUT_MUX2_RFSILENT, 0);
58964f89611Schristos 	AR_WRITE(sc, AR_GPIO_INPUT_MUX2, reg);
59064f89611Schristos 	ar9003_gpio_config_input(sc, sc->sc_rfsilent_pin);
59164f89611Schristos 	AR_SETBITS(sc, AR_PHY_TEST, AR_PHY_TEST_RFSILENT_BB);
59264f89611Schristos 	if (!(sc->sc_flags & ATHN_FLAG_RFSILENT_REVERSED)) {
59364f89611Schristos 		AR_SETBITS(sc, AR_GPIO_INTR_POL,
59464f89611Schristos 		    AR_GPIO_INTR_POL_PIN(sc->sc_rfsilent_pin));
59564f89611Schristos 	}
59664f89611Schristos 	AR_WRITE_BARRIER(sc);
59764f89611Schristos }
59864f89611Schristos 
59964f89611Schristos Static int
ar9003_dma_alloc(struct athn_softc * sc)60064f89611Schristos ar9003_dma_alloc(struct athn_softc *sc)
60164f89611Schristos {
60264f89611Schristos 	int error;
60364f89611Schristos 
60464f89611Schristos 	error = ar9003_tx_alloc(sc);
60564f89611Schristos 	if (error != 0)
60664f89611Schristos 		return error;
60764f89611Schristos 
60864f89611Schristos 	error = ar9003_rx_alloc(sc, ATHN_QID_LP, AR9003_RX_LP_QDEPTH);
60964f89611Schristos 	if (error != 0)
61064f89611Schristos 		return error;
61164f89611Schristos 
61264f89611Schristos 	error = ar9003_rx_alloc(sc, ATHN_QID_HP, AR9003_RX_HP_QDEPTH);
61364f89611Schristos 	if (error != 0)
61464f89611Schristos 		return error;
61564f89611Schristos 
61664f89611Schristos 	return 0;
61764f89611Schristos }
61864f89611Schristos 
61964f89611Schristos Static void
ar9003_dma_free(struct athn_softc * sc)62064f89611Schristos ar9003_dma_free(struct athn_softc *sc)
62164f89611Schristos {
62264f89611Schristos 
62364f89611Schristos 	ar9003_tx_free(sc);
62464f89611Schristos 	ar9003_rx_free(sc, ATHN_QID_LP);
62564f89611Schristos 	ar9003_rx_free(sc, ATHN_QID_HP);
62664f89611Schristos }
62764f89611Schristos 
62864f89611Schristos Static int
ar9003_tx_alloc(struct athn_softc * sc)62964f89611Schristos ar9003_tx_alloc(struct athn_softc *sc)
63064f89611Schristos {
63164f89611Schristos 	struct athn_tx_buf *bf;
63264f89611Schristos 	bus_size_t size;
63364f89611Schristos 	int error, nsegs, i;
63464f89611Schristos 
63564f89611Schristos 	/*
63664f89611Schristos 	 * Allocate Tx status ring.
63764f89611Schristos 	 */
63864f89611Schristos 	size = AR9003_NTXSTATUS * sizeof(struct ar_tx_status);
63964f89611Schristos 
64064f89611Schristos 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
64164f89611Schristos 	    BUS_DMA_NOWAIT, &sc->sc_txsmap);
64264f89611Schristos 	if (error != 0)
64364f89611Schristos 		goto fail;
64464f89611Schristos 
64564f89611Schristos 	error = bus_dmamem_alloc(sc->sc_dmat, size, 4, 0, &sc->sc_txsseg, 1,
64664f89611Schristos // XXX	    &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
64764f89611Schristos 	    &nsegs, BUS_DMA_NOWAIT);
64864f89611Schristos 	if (error != 0)
64964f89611Schristos 		goto fail;
65064f89611Schristos 
65164f89611Schristos 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_txsseg, 1, size,
65264f89611Schristos 	    (void **)&sc->sc_txsring, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
65364f89611Schristos 	if (error != 0)
65464f89611Schristos 		goto fail;
65564f89611Schristos 
656482fe1eaSmatt 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_txsmap, sc->sc_txsring,
657482fe1eaSmatt 	     size, NULL, BUS_DMA_NOWAIT | BUS_DMA_READ);
65864f89611Schristos 	if (error != 0)
65964f89611Schristos 		goto fail;
66064f89611Schristos 
66164f89611Schristos 	/*
66264f89611Schristos 	 * Allocate a pool of Tx descriptors shared between all Tx queues.
66364f89611Schristos 	 */
66464f89611Schristos 	size = ATHN_NTXBUFS * sizeof(struct ar_tx_desc);
66564f89611Schristos 
66664f89611Schristos 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
66764f89611Schristos 	    BUS_DMA_NOWAIT, &sc->sc_map);
66864f89611Schristos 	if (error != 0)
66964f89611Schristos 		goto fail;
67064f89611Schristos 
67164f89611Schristos 	error = bus_dmamem_alloc(sc->sc_dmat, size, 4, 0, &sc->sc_seg, 1,
67264f89611Schristos // XXX	    &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
67364f89611Schristos 	    &nsegs, BUS_DMA_NOWAIT);
67464f89611Schristos 	if (error != 0)
67564f89611Schristos 		goto fail;
67664f89611Schristos 
67764f89611Schristos 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_seg, 1, size,
67864f89611Schristos 	    (void **)&sc->sc_descs, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
67964f89611Schristos 	if (error != 0)
68064f89611Schristos 		goto fail;
68164f89611Schristos 
682482fe1eaSmatt 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_map, sc->sc_descs, size,
683482fe1eaSmatt 	    NULL, BUS_DMA_NOWAIT | BUS_DMA_WRITE);
68464f89611Schristos 	if (error != 0)
68564f89611Schristos 		goto fail;
68664f89611Schristos 
68764f89611Schristos 	SIMPLEQ_INIT(&sc->sc_txbufs);
68864f89611Schristos 	for (i = 0; i < ATHN_NTXBUFS; i++) {
68964f89611Schristos 		bf = &sc->sc_txpool[i];
69064f89611Schristos 
69164f89611Schristos 		error = bus_dmamap_create(sc->sc_dmat, ATHN_TXBUFSZ,
69264f89611Schristos 		    AR9003_MAX_SCATTER, ATHN_TXBUFSZ, 0, BUS_DMA_NOWAIT,
69364f89611Schristos 		    &bf->bf_map);
69464f89611Schristos 		if (error != 0) {
695dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
696dabffed6Schristos 			    "could not create Tx buf DMA map\n");
69764f89611Schristos 			goto fail;
69864f89611Schristos 		}
69964f89611Schristos 
70064f89611Schristos 		bf->bf_descs = &((struct ar_tx_desc *)sc->sc_descs)[i];
70164f89611Schristos 		bf->bf_daddr = sc->sc_map->dm_segs[0].ds_addr +
70264f89611Schristos 		    i * sizeof(struct ar_tx_desc);
70364f89611Schristos 
70464f89611Schristos 		SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
70564f89611Schristos 	}
70664f89611Schristos 	return 0;
70764f89611Schristos  fail:
70864f89611Schristos 	ar9003_tx_free(sc);
70964f89611Schristos 	return error;
71064f89611Schristos }
71164f89611Schristos 
71264f89611Schristos Static void
ar9003_tx_free(struct athn_softc * sc)71364f89611Schristos ar9003_tx_free(struct athn_softc *sc)
71464f89611Schristos {
71564f89611Schristos 	struct athn_tx_buf *bf;
71664f89611Schristos 	int i;
71764f89611Schristos 
71864f89611Schristos 	for (i = 0; i < ATHN_NTXBUFS; i++) {
71964f89611Schristos 		bf = &sc->sc_txpool[i];
72064f89611Schristos 
72164f89611Schristos 		if (bf->bf_map != NULL)
72264f89611Schristos 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_map);
72364f89611Schristos 	}
72464f89611Schristos 	/* Free Tx descriptors. */
72564f89611Schristos 	if (sc->sc_map != NULL) {
72664f89611Schristos 		if (sc->sc_descs != NULL) {
72764f89611Schristos 			bus_dmamap_unload(sc->sc_dmat, sc->sc_map);
72864f89611Schristos 			bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_descs,
72964f89611Schristos 			    ATHN_NTXBUFS * sizeof(struct ar_tx_desc));
73064f89611Schristos 			bus_dmamem_free(sc->sc_dmat, &sc->sc_seg, 1);
73164f89611Schristos 		}
73264f89611Schristos 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_map);
73364f89611Schristos 	}
73464f89611Schristos 	/* Free Tx status ring. */
73564f89611Schristos 	if (sc->sc_txsmap != NULL) {
73664f89611Schristos 		if (sc->sc_txsring != NULL) {
73764f89611Schristos 			bus_dmamap_unload(sc->sc_dmat, sc->sc_txsmap);
73864f89611Schristos 			bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_txsring,
73964f89611Schristos 			     AR9003_NTXSTATUS * sizeof(struct ar_tx_status));
74064f89611Schristos 			bus_dmamem_free(sc->sc_dmat, &sc->sc_txsseg, 1);
74164f89611Schristos 		}
74264f89611Schristos 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_txsmap);
74364f89611Schristos 	}
74464f89611Schristos }
74564f89611Schristos 
74664f89611Schristos Static int
ar9003_rx_alloc(struct athn_softc * sc,int qid,int count)74764f89611Schristos ar9003_rx_alloc(struct athn_softc *sc, int qid, int count)
74864f89611Schristos {
74964f89611Schristos 	struct athn_rxq *rxq = &sc->sc_rxq[qid];
75064f89611Schristos 	struct athn_rx_buf *bf;
75164f89611Schristos 	struct ar_rx_status *ds;
75264f89611Schristos 	int error, i;
75364f89611Schristos 
754d47bcd29Schs 	rxq->bf = malloc(count * sizeof(*bf), M_DEVBUF, M_WAITOK | M_ZERO);
75564f89611Schristos 	rxq->count = count;
75664f89611Schristos 
75764f89611Schristos 	for (i = 0; i < rxq->count; i++) {
75864f89611Schristos 		bf = &rxq->bf[i];
75964f89611Schristos 
76064f89611Schristos 		error = bus_dmamap_create(sc->sc_dmat, ATHN_RXBUFSZ, 1,
76164f89611Schristos 		    ATHN_RXBUFSZ, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
76264f89611Schristos 		    &bf->bf_map);
76364f89611Schristos 		if (error != 0) {
764dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
765dabffed6Schristos 			    "could not create Rx buf DMA map\n");
76664f89611Schristos 			goto fail;
76764f89611Schristos 		}
76864f89611Schristos 		/*
76964f89611Schristos 		 * Assumes MCLGETI returns cache-line-size aligned buffers.
77064f89611Schristos 		 */
77164f89611Schristos 		bf->bf_m = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
77264f89611Schristos 		if (bf->bf_m == NULL) {
773dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
774dabffed6Schristos 			    "could not allocate Rx mbuf\n");
77564f89611Schristos 			error = ENOBUFS;
77664f89611Schristos 			goto fail;
77764f89611Schristos 		}
77864f89611Schristos 
77964f89611Schristos 		error = bus_dmamap_load(sc->sc_dmat, bf->bf_map,
78064f89611Schristos 		    mtod(bf->bf_m, void *), ATHN_RXBUFSZ, NULL,
78164f89611Schristos 		    BUS_DMA_NOWAIT);
78264f89611Schristos 		if (error != 0) {
783dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
784dabffed6Schristos 			    "could not DMA map Rx buffer\n");
78564f89611Schristos 			goto fail;
78664f89611Schristos 		}
78764f89611Schristos 
78864f89611Schristos 		ds = mtod(bf->bf_m, struct ar_rx_status *);
78964f89611Schristos 		memset(ds, 0, sizeof(*ds));
79064f89611Schristos 		bf->bf_desc = ds;
79164f89611Schristos 		bf->bf_daddr = bf->bf_map->dm_segs[0].ds_addr;
79264f89611Schristos 
79364f89611Schristos 		bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
79464f89611Schristos 		    BUS_DMASYNC_PREREAD);
79564f89611Schristos 	}
79664f89611Schristos 	return 0;
79764f89611Schristos  fail:
79864f89611Schristos 	ar9003_rx_free(sc, qid);
79964f89611Schristos 	return error;
80064f89611Schristos }
80164f89611Schristos 
80264f89611Schristos Static void
ar9003_rx_free(struct athn_softc * sc,int qid)80364f89611Schristos ar9003_rx_free(struct athn_softc *sc, int qid)
80464f89611Schristos {
80564f89611Schristos 	struct athn_rxq *rxq = &sc->sc_rxq[qid];
80664f89611Schristos 	struct athn_rx_buf *bf;
80764f89611Schristos 	int i;
80864f89611Schristos 
80964f89611Schristos 	if (rxq->bf == NULL)
81064f89611Schristos 		return;
81164f89611Schristos 	for (i = 0; i < rxq->count; i++) {
81264f89611Schristos 		bf = &rxq->bf[i];
81364f89611Schristos 
81464f89611Schristos 		if (bf->bf_map != NULL)
81564f89611Schristos 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_map);
81664f89611Schristos 		m_freem(bf->bf_m);
81764f89611Schristos 	}
81864f89611Schristos 	free(rxq->bf, M_DEVBUF);
81964f89611Schristos }
82064f89611Schristos 
82164f89611Schristos PUBLIC void
ar9003_reset_txsring(struct athn_softc * sc)82264f89611Schristos ar9003_reset_txsring(struct athn_softc *sc)
82364f89611Schristos {
82464f89611Schristos 
82564f89611Schristos 	sc->sc_txscur = 0;
82664f89611Schristos 	memset(sc->sc_txsring, 0, AR9003_NTXSTATUS * sizeof(struct ar_tx_status));
82764f89611Schristos 	AR_WRITE(sc, AR_Q_STATUS_RING_START,
82864f89611Schristos 	    sc->sc_txsmap->dm_segs[0].ds_addr);
82964f89611Schristos 	AR_WRITE(sc, AR_Q_STATUS_RING_END,
83064f89611Schristos 	    sc->sc_txsmap->dm_segs[0].ds_addr + sc->sc_txsmap->dm_segs[0].ds_len);
83164f89611Schristos 	AR_WRITE_BARRIER(sc);
83264f89611Schristos }
83364f89611Schristos 
83464f89611Schristos Static void
ar9003_rx_enable(struct athn_softc * sc)83564f89611Schristos ar9003_rx_enable(struct athn_softc *sc)
83664f89611Schristos {
83764f89611Schristos 	struct athn_rxq *rxq;
83864f89611Schristos 	struct athn_rx_buf *bf;
83964f89611Schristos 	struct ar_rx_status *ds;
84064f89611Schristos 	uint32_t reg;
84164f89611Schristos 	int qid, i;
84264f89611Schristos 
84364f89611Schristos 	reg = AR_READ(sc, AR_RXBP_THRESH);
84464f89611Schristos 	reg = RW(reg, AR_RXBP_THRESH_HP, 1);
84564f89611Schristos 	reg = RW(reg, AR_RXBP_THRESH_LP, 1);
84664f89611Schristos 	AR_WRITE(sc, AR_RXBP_THRESH, reg);
84764f89611Schristos 
84864f89611Schristos 	/* Set Rx buffer size. */
84964f89611Schristos 	AR_WRITE(sc, AR_DATABUF_SIZE, ATHN_RXBUFSZ - sizeof(*ds));
85064f89611Schristos 
85164f89611Schristos 	for (qid = 0; qid < 2; qid++) {
85264f89611Schristos 		rxq = &sc->sc_rxq[qid];
85364f89611Schristos 
85464f89611Schristos 		/* Setup Rx status descriptors. */
85564f89611Schristos 		SIMPLEQ_INIT(&rxq->head);
85664f89611Schristos 		for (i = 0; i < rxq->count; i++) {
85764f89611Schristos 			bf = &rxq->bf[i];
85864f89611Schristos 			ds = bf->bf_desc;
85964f89611Schristos 
86064f89611Schristos 			memset(ds, 0, sizeof(*ds));
86164f89611Schristos 			if (qid == ATHN_QID_LP)
86264f89611Schristos 				AR_WRITE(sc, AR_LP_RXDP, bf->bf_daddr);
86364f89611Schristos 			else
86464f89611Schristos 				AR_WRITE(sc, AR_HP_RXDP, bf->bf_daddr);
86564f89611Schristos 			AR_WRITE_BARRIER(sc);
86664f89611Schristos 			SIMPLEQ_INSERT_TAIL(&rxq->head, bf, bf_list);
86764f89611Schristos 		}
86864f89611Schristos 	}
86964f89611Schristos 	/* Enable Rx. */
87064f89611Schristos 	AR_WRITE(sc, AR_CR, 0);
87164f89611Schristos 	AR_WRITE_BARRIER(sc);
87264f89611Schristos }
87364f89611Schristos 
87464f89611Schristos Static void
ar9003_rx_radiotap(struct athn_softc * sc,struct mbuf * m,struct ar_rx_status * ds)87564f89611Schristos ar9003_rx_radiotap(struct athn_softc *sc, struct mbuf *m,
87664f89611Schristos     struct ar_rx_status *ds)
87764f89611Schristos {
87864f89611Schristos 	struct athn_rx_radiotap_header *tap = &sc->sc_rxtap;
87964f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
88064f89611Schristos 	uint64_t tsf;
88164f89611Schristos 	uint32_t tstamp;
88264f89611Schristos 	uint8_t rate;
88364f89611Schristos 
88464f89611Schristos 	/* Extend the 15-bit timestamp from Rx status to 64-bit TSF. */
88564f89611Schristos 	tstamp = ds->ds_status3;
88664f89611Schristos 	tsf = AR_READ(sc, AR_TSF_U32);
88764f89611Schristos 	tsf = tsf << 32 | AR_READ(sc, AR_TSF_L32);
88864f89611Schristos 	if ((tsf & 0x7fff) < tstamp)
88964f89611Schristos 		tsf -= 0x8000;
89064f89611Schristos 	tsf = (tsf & ~0x7fff) | tstamp;
89164f89611Schristos 
89264f89611Schristos 	tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
89364f89611Schristos 	tap->wr_tsft = htole64(tsf);
89464f89611Schristos 	tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
89564f89611Schristos 	tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
89664f89611Schristos 	tap->wr_dbm_antsignal = MS(ds->ds_status5, AR_RXS5_RSSI_COMBINED);
89764f89611Schristos 	/* XXX noise. */
89864f89611Schristos 	tap->wr_antenna = MS(ds->ds_status4, AR_RXS4_ANTENNA);
89964f89611Schristos 	tap->wr_rate = 0;	/* In case it can't be found below. */
90064f89611Schristos 	rate = MS(ds->ds_status1, AR_RXS1_RATE);
90164f89611Schristos 	if (rate & 0x80) {		/* HT. */
90264f89611Schristos 		/* Bit 7 set means HT MCS instead of rate. */
90364f89611Schristos 		tap->wr_rate = rate;
90464f89611Schristos 		if (!(ds->ds_status4 & AR_RXS4_GI))
90564f89611Schristos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
90664f89611Schristos 
90764f89611Schristos 	}
90864f89611Schristos 	else if (rate & 0x10) {	/* CCK. */
90964f89611Schristos 		if (rate & 0x04)
91064f89611Schristos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
91164f89611Schristos 		switch (rate & ~0x14) {
91264f89611Schristos 		case 0xb: tap->wr_rate =   2; break;
91364f89611Schristos 		case 0xa: tap->wr_rate =   4; break;
91464f89611Schristos 		case 0x9: tap->wr_rate =  11; break;
91564f89611Schristos 		case 0x8: tap->wr_rate =  22; break;
91664f89611Schristos 		}
91764f89611Schristos 	}
91864f89611Schristos 	else {			/* OFDM. */
91964f89611Schristos 		switch (rate) {
92064f89611Schristos 		case 0xb: tap->wr_rate =  12; break;
92164f89611Schristos 		case 0xf: tap->wr_rate =  18; break;
92264f89611Schristos 		case 0xa: tap->wr_rate =  24; break;
92364f89611Schristos 		case 0xe: tap->wr_rate =  36; break;
92464f89611Schristos 		case 0x9: tap->wr_rate =  48; break;
92564f89611Schristos 		case 0xd: tap->wr_rate =  72; break;
92664f89611Schristos 		case 0x8: tap->wr_rate =  96; break;
92764f89611Schristos 		case 0xc: tap->wr_rate = 108; break;
92864f89611Schristos 		}
92964f89611Schristos 	}
9303cd62456Smsaitoh 	bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN);
93164f89611Schristos }
93264f89611Schristos 
93364f89611Schristos Static int
ar9003_rx_process(struct athn_softc * sc,int qid)93464f89611Schristos ar9003_rx_process(struct athn_softc *sc, int qid)
93564f89611Schristos {
93664f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
93764f89611Schristos 	struct ifnet *ifp = &sc->sc_if;
93864f89611Schristos 	struct athn_rxq *rxq = &sc->sc_rxq[qid];
93964f89611Schristos 	struct athn_rx_buf *bf;
94064f89611Schristos 	struct ar_rx_status *ds;
94164f89611Schristos 	struct ieee80211_frame *wh;
94264f89611Schristos 	struct ieee80211_node *ni;
94364f89611Schristos 	struct mbuf *m, *m1;
94464f89611Schristos 	size_t len;
94564f89611Schristos 	u_int32_t rstamp;
9465ffb0503Snonaka 	int error, rssi, s;
94764f89611Schristos 
94864f89611Schristos 	bf = SIMPLEQ_FIRST(&rxq->head);
94964f89611Schristos 	if (__predict_false(bf == NULL)) {	/* Should not happen. */
950dabffed6Schristos 		aprint_error_dev(sc->sc_dev, "Rx queue is empty!\n");
95164f89611Schristos 		return ENOENT;
95264f89611Schristos 	}
95364f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
95464f89611Schristos 	    BUS_DMASYNC_POSTREAD);
95564f89611Schristos 
95664f89611Schristos 	ds = mtod(bf->bf_m, struct ar_rx_status *);
95764f89611Schristos 	if (!(ds->ds_status1 & AR_RXS1_DONE))
95864f89611Schristos 		return EBUSY;
95964f89611Schristos 
96064f89611Schristos 	/* Check that it is a valid Rx status descriptor. */
96164f89611Schristos 	if ((ds->ds_info & (AR_RXI_DESC_ID_M | AR_RXI_DESC_TX |
96264f89611Schristos 	    AR_RXI_CTRL_STAT)) != SM(AR_RXI_DESC_ID, AR_VENDOR_ATHEROS))
96364f89611Schristos 		goto skip;
96464f89611Schristos 
96564f89611Schristos 	if (!(ds->ds_status11 & AR_RXS11_FRAME_OK)) {
96664f89611Schristos 		if (ds->ds_status11 & AR_RXS11_CRC_ERR)
96764f89611Schristos 			DPRINTFN(DBG_RX, sc, "CRC error\n");
96864f89611Schristos 		else if (ds->ds_status11 & AR_RXS11_PHY_ERR)
96964f89611Schristos 			DPRINTFN(DBG_RX, sc, "PHY error=0x%x\n",
97064f89611Schristos 			    MS(ds->ds_status11, AR_RXS11_PHY_ERR_CODE));
97164f89611Schristos 		else if (ds->ds_status11 & AR_RXS11_DECRYPT_CRC_ERR)
97264f89611Schristos 			DPRINTFN(DBG_RX, sc, "Decryption CRC error\n");
97364f89611Schristos 		else if (ds->ds_status11 & AR_RXS11_MICHAEL_ERR) {
97464f89611Schristos 			DPRINTFN(DBG_RX, sc, "Michael MIC failure\n");
97564f89611Schristos 			/* Report Michael MIC failures to net80211. */
97664f89611Schristos 
97764f89611Schristos 			len = MS(ds->ds_status2, AR_RXS2_DATA_LEN);
97864f89611Schristos 			m = bf->bf_m;
979d938d837Sozaki-r 			m_set_rcvif(m, ifp);
98064f89611Schristos 			m->m_data = (void *)&ds[1];
98164f89611Schristos 			m->m_pkthdr.len = m->m_len = len;
98264f89611Schristos 			wh = mtod(m, struct ieee80211_frame *);
98364f89611Schristos 
98464f89611Schristos 			ieee80211_notify_michael_failure(ic, wh,
98564f89611Schristos 			    0 /* XXX: keyix */);
98664f89611Schristos 		}
9872d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
98864f89611Schristos 		goto skip;
98964f89611Schristos 	}
99064f89611Schristos 
99164f89611Schristos 	len = MS(ds->ds_status2, AR_RXS2_DATA_LEN);
99264f89611Schristos 	if (__predict_false(len < IEEE80211_MIN_LEN ||
99364f89611Schristos 	    len > ATHN_RXBUFSZ - sizeof(*ds))) {
99464f89611Schristos 		DPRINTFN(DBG_RX, sc, "corrupted descriptor length=%zd\n",
99564f89611Schristos 		    len);
9962d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
99764f89611Schristos 		goto skip;
99864f89611Schristos 	}
99964f89611Schristos 
100064f89611Schristos 	/* Allocate a new Rx buffer. */
100164f89611Schristos 	m1 = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
100264f89611Schristos 	if (__predict_false(m1 == NULL)) {
100364f89611Schristos 		ic->ic_stats.is_rx_nobuf++;
10042d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
100564f89611Schristos 		goto skip;
100664f89611Schristos 	}
100764f89611Schristos 
100864f89611Schristos 	/* Unmap the old Rx buffer. */
100964f89611Schristos 	bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
101064f89611Schristos 
101164f89611Schristos 	/* Map the new Rx buffer. */
101264f89611Schristos 	error = bus_dmamap_load(sc->sc_dmat, bf->bf_map, mtod(m1, void *),
101364f89611Schristos 	    ATHN_RXBUFSZ, NULL, BUS_DMA_NOWAIT | BUS_DMA_READ);
101464f89611Schristos 	if (__predict_false(error != 0)) {
101564f89611Schristos 		m_freem(m1);
101664f89611Schristos 
101764f89611Schristos 		/* Remap the old Rx buffer or panic. */
101864f89611Schristos 		error = bus_dmamap_load(sc->sc_dmat, bf->bf_map,
101964f89611Schristos 		    mtod(bf->bf_m, void *), ATHN_RXBUFSZ, NULL,
102064f89611Schristos 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
102164f89611Schristos 		KASSERT(error != 0);
102264f89611Schristos 		bf->bf_daddr = bf->bf_map->dm_segs[0].ds_addr;
10232d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
102464f89611Schristos 		goto skip;
102564f89611Schristos 	}
102664f89611Schristos 	bf->bf_desc = mtod(m1, struct ar_rx_status *);
102764f89611Schristos 	bf->bf_daddr = bf->bf_map->dm_segs[0].ds_addr;
102864f89611Schristos 
102964f89611Schristos 	m = bf->bf_m;
103064f89611Schristos 	bf->bf_m = m1;
103164f89611Schristos 
103264f89611Schristos 	/* Finalize mbuf. */
1033d938d837Sozaki-r 	m_set_rcvif(m, ifp);
103464f89611Schristos 	/* Strip Rx status descriptor from head. */
103564f89611Schristos 	m->m_data = (void *)&ds[1];
103664f89611Schristos 	m->m_pkthdr.len = m->m_len = len;
103764f89611Schristos 
10385ffb0503Snonaka 	s = splnet();
10395ffb0503Snonaka 
104064f89611Schristos 	/* Grab a reference to the source node. */
104164f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
104264f89611Schristos 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
104364f89611Schristos 
104464f89611Schristos 	/* Remove any HW padding after the 802.11 header. */
104564f89611Schristos 	if (!(wh->i_fc[0] & IEEE80211_FC0_TYPE_CTL)) {
104664f89611Schristos 		u_int hdrlen = ieee80211_anyhdrsize(wh);
104764f89611Schristos 		if (hdrlen & 3) {
1048ef294642Smaxv 			memmove((uint8_t *)wh + 2, wh, hdrlen);
104964f89611Schristos 			m_adj(m, 2);
105064f89611Schristos 		}
105164f89611Schristos 	}
105264f89611Schristos 	if (__predict_false(sc->sc_drvbpf != NULL))
105364f89611Schristos 		ar9003_rx_radiotap(sc, m, ds);
105464f89611Schristos 	/* Trim 802.11 FCS after radiotap. */
105564f89611Schristos 	m_adj(m, -IEEE80211_CRC_LEN);
105664f89611Schristos 
105764f89611Schristos 	/* Send the frame to the 802.11 layer. */
105864f89611Schristos 	rssi = MS(ds->ds_status5, AR_RXS5_RSSI_COMBINED);
105964f89611Schristos 	rstamp = ds->ds_status3;
106064f89611Schristos 	ieee80211_input(ic, m, ni, rssi, rstamp);
106164f89611Schristos 
106264f89611Schristos 	/* Node is no longer needed. */
106364f89611Schristos 	ieee80211_free_node(ni);
106464f89611Schristos 
10655ffb0503Snonaka 	splx(s);
10665ffb0503Snonaka 
106764f89611Schristos  skip:
106864f89611Schristos 	/* Unlink this descriptor from head. */
106964f89611Schristos 	SIMPLEQ_REMOVE_HEAD(&rxq->head, bf_list);
107064f89611Schristos 	memset(bf->bf_desc, 0, sizeof(*ds));
107164f89611Schristos 
107264f89611Schristos 	/* Re-use this descriptor and link it to tail. */
107364f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
107464f89611Schristos 	    BUS_DMASYNC_PREREAD);
107564f89611Schristos 
107664f89611Schristos 	if (qid == ATHN_QID_LP)
107764f89611Schristos 		AR_WRITE(sc, AR_LP_RXDP, bf->bf_daddr);
107864f89611Schristos 	else
107964f89611Schristos 		AR_WRITE(sc, AR_HP_RXDP, bf->bf_daddr);
108064f89611Schristos 	AR_WRITE_BARRIER(sc);
108164f89611Schristos 	SIMPLEQ_INSERT_TAIL(&rxq->head, bf, bf_list);
108264f89611Schristos 
108364f89611Schristos 	/* Re-enable Rx. */
108464f89611Schristos 	AR_WRITE(sc, AR_CR, 0);
108564f89611Schristos 	AR_WRITE_BARRIER(sc);
108664f89611Schristos 	return 0;
108764f89611Schristos }
108864f89611Schristos 
108964f89611Schristos Static void
ar9003_rx_intr(struct athn_softc * sc,int qid)109064f89611Schristos ar9003_rx_intr(struct athn_softc *sc, int qid)
109164f89611Schristos {
109264f89611Schristos 
109364f89611Schristos 	while (ar9003_rx_process(sc, qid) == 0)
109464f89611Schristos 		continue;
109564f89611Schristos }
109664f89611Schristos 
109764f89611Schristos Static int
ar9003_tx_process(struct athn_softc * sc)109864f89611Schristos ar9003_tx_process(struct athn_softc *sc)
109964f89611Schristos {
110064f89611Schristos 	struct ifnet *ifp = &sc->sc_if;
110164f89611Schristos 	struct athn_txq *txq;
110264f89611Schristos 	struct athn_node *an;
110364f89611Schristos 	struct athn_tx_buf *bf;
110464f89611Schristos 	struct ar_tx_status *ds;
110564f89611Schristos 	uint8_t qid, failcnt;
110664f89611Schristos 
110764f89611Schristos 	ds = &((struct ar_tx_status *)sc->sc_txsring)[sc->sc_txscur];
110864f89611Schristos 	if (!(ds->ds_status8 & AR_TXS8_DONE))
110964f89611Schristos 		return EBUSY;
111064f89611Schristos 
111164f89611Schristos 	sc->sc_txscur = (sc->sc_txscur + 1) % AR9003_NTXSTATUS;
111264f89611Schristos 
111364f89611Schristos 	/* Check that it is a valid Tx status descriptor. */
111464f89611Schristos 	if ((ds->ds_info & (AR_TXI_DESC_ID_M | AR_TXI_DESC_TX)) !=
111564f89611Schristos 	    (SM(AR_TXI_DESC_ID, AR_VENDOR_ATHEROS) | AR_TXI_DESC_TX)) {
111664f89611Schristos 		memset(ds, 0, sizeof(*ds));
111764f89611Schristos 		return 0;
111864f89611Schristos 	}
111964f89611Schristos 	/* Retrieve the queue that was used to send this PDU. */
112064f89611Schristos 	qid = MS(ds->ds_info, AR_TXI_QCU_NUM);
112164f89611Schristos 	txq = &sc->sc_txq[qid];
112264f89611Schristos 
112364f89611Schristos 	bf = SIMPLEQ_FIRST(&txq->head);
112464f89611Schristos 	if (bf == NULL || bf == txq->wait) {
112564f89611Schristos 		memset(ds, 0, sizeof(*ds));
112664f89611Schristos 		return 0;
112764f89611Schristos 	}
112864f89611Schristos 	SIMPLEQ_REMOVE_HEAD(&txq->head, bf_list);
11292d501ecfSthorpej 	if_statinc(ifp, if_opackets);
113064f89611Schristos 
113164f89611Schristos 	sc->sc_tx_timer = 0;
113264f89611Schristos 
113364f89611Schristos 	if (ds->ds_status3 & AR_TXS3_EXCESSIVE_RETRIES)
11342d501ecfSthorpej 		if_statinc(ifp, if_oerrors);
113564f89611Schristos 
113664f89611Schristos 	if (ds->ds_status3 & AR_TXS3_UNDERRUN)
113764f89611Schristos 		athn_inc_tx_trigger_level(sc);
113864f89611Schristos 
113964f89611Schristos 	/* Wakeup PA predistortion state machine. */
114064f89611Schristos 	if (bf->bf_txflags & ATHN_TXFLAG_PAPRD)
114164f89611Schristos 		ar9003_paprd_tx_tone_done(sc);
114264f89611Schristos 
114364f89611Schristos 	an = (struct athn_node *)bf->bf_ni;
114464f89611Schristos 	/*
114564f89611Schristos 	 * NB: the data fail count contains the number of un-acked tries
114664f89611Schristos 	 * for the final series used.  We must add the number of tries for
114764f89611Schristos 	 * each series that was fully processed.
114864f89611Schristos 	 */
114964f89611Schristos 	failcnt  = MS(ds->ds_status3, AR_TXS3_DATA_FAIL_CNT);
115064f89611Schristos 	/* NB: Assume two tries per series. */
115164f89611Schristos 	failcnt += MS(ds->ds_status8, AR_TXS8_FINAL_IDX) * 2;
115264f89611Schristos 
115364f89611Schristos 	/* Update rate control statistics. */
115464f89611Schristos 	an->amn.amn_txcnt++;
115564f89611Schristos 	if (failcnt > 0)
115664f89611Schristos 		an->amn.amn_retrycnt++;
115764f89611Schristos 
115864f89611Schristos 	DPRINTFN(DBG_TX, sc, "Tx done qid=%d status3=%d fail count=%d\n",
115964f89611Schristos 	    qid, ds->ds_status3, failcnt);
116064f89611Schristos 
116164f89611Schristos 	/* Reset Tx status descriptor. */
116264f89611Schristos 	memset(ds, 0, sizeof(*ds));
116364f89611Schristos 
116464f89611Schristos 	/* Unmap Tx buffer. */
116564f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
116664f89611Schristos 	    BUS_DMASYNC_POSTWRITE);
116764f89611Schristos 	bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
116864f89611Schristos 
116964f89611Schristos 	m_freem(bf->bf_m);
117064f89611Schristos 	bf->bf_m = NULL;
117164f89611Schristos 	ieee80211_free_node(bf->bf_ni);
117264f89611Schristos 	bf->bf_ni = NULL;
117364f89611Schristos 
117464f89611Schristos 	/* Link Tx buffer back to global free list. */
117564f89611Schristos 	SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
117664f89611Schristos 
117764f89611Schristos 	/* Queue buffers that are waiting if there is new room. */
117864f89611Schristos 	if (--txq->queued < AR9003_TX_QDEPTH && txq->wait != NULL) {
117964f89611Schristos 		AR_WRITE(sc, AR_QTXDP(qid), txq->wait->bf_daddr);
118064f89611Schristos 		AR_WRITE_BARRIER(sc);
118164f89611Schristos 		txq->wait = SIMPLEQ_NEXT(txq->wait, bf_list);
118264f89611Schristos 	}
118364f89611Schristos 	return 0;
118464f89611Schristos }
118564f89611Schristos 
118664f89611Schristos Static void
ar9003_tx_intr(struct athn_softc * sc)118764f89611Schristos ar9003_tx_intr(struct athn_softc *sc)
118864f89611Schristos {
118964f89611Schristos 	struct ifnet *ifp = &sc->sc_if;
11905ffb0503Snonaka 	int s;
119164f89611Schristos 
11925ffb0503Snonaka 	s = splnet();
11935ffb0503Snonaka 
11945ffb0503Snonaka 	while (ar9003_tx_process(sc) == 0)
11955ffb0503Snonaka 		continue;
119664f89611Schristos 
119764f89611Schristos 	if (!SIMPLEQ_EMPTY(&sc->sc_txbufs)) {
119864f89611Schristos 		ifp->if_flags &= ~IFF_OACTIVE;
1199a1d5203dSozaki-r 		ifp->if_start(ifp); /* in softint */
120064f89611Schristos 	}
12015ffb0503Snonaka 
12025ffb0503Snonaka 	splx(s);
120364f89611Schristos }
120464f89611Schristos 
120564f89611Schristos #ifndef IEEE80211_STA_ONLY
120664f89611Schristos /*
120764f89611Schristos  * Process Software Beacon Alert interrupts.
120864f89611Schristos  */
120964f89611Schristos Static int
ar9003_swba_intr(struct athn_softc * sc)121064f89611Schristos ar9003_swba_intr(struct athn_softc *sc)
121164f89611Schristos {
121264f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
121364f89611Schristos 	struct ifnet *ifp = &sc->sc_if;
121464f89611Schristos 	struct ieee80211_node *ni = ic->ic_bss;
121564f89611Schristos 	struct athn_tx_buf *bf = sc->sc_bcnbuf;
121664f89611Schristos 	struct ieee80211_frame *wh;
121764f89611Schristos 	struct ieee80211_beacon_offsets bo;
121864f89611Schristos 	struct ar_tx_desc *ds;
121964f89611Schristos 	struct mbuf *m;
122064f89611Schristos 	uint32_t sum;
122164f89611Schristos 	uint8_t ridx, hwrate;
122264f89611Schristos 	int error, totlen;
122364f89611Schristos 
122464f89611Schristos #if notyet
122564f89611Schristos 	if (ic->ic_tim_mcast_pending &&
122664f89611Schristos 	    IF_IS_EMPTY(&ni->ni_savedq) &&
122764f89611Schristos 	    SIMPLEQ_EMPTY(&sc->sc_txq[ATHN_QID_CAB].head))
122864f89611Schristos 		ic->ic_tim_mcast_pending = 0;
122964f89611Schristos #endif
123064f89611Schristos 	if (ic->ic_dtim_count == 0)
123164f89611Schristos 		ic->ic_dtim_count = ic->ic_dtim_period - 1;
123264f89611Schristos 	else
123364f89611Schristos 		ic->ic_dtim_count--;
123464f89611Schristos 
123564f89611Schristos 	/* Make sure previous beacon has been sent. */
123664f89611Schristos 	if (athn_tx_pending(sc, ATHN_QID_BEACON)) {
123764f89611Schristos 		DPRINTFN(DBG_INTR, sc, "beacon stuck\n");
123864f89611Schristos 		return EBUSY;
123964f89611Schristos 	}
124064f89611Schristos 	/* Get new beacon. */
124164f89611Schristos 	m = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
124264f89611Schristos 	if (__predict_false(m == NULL))
124364f89611Schristos 		return ENOBUFS;
124464f89611Schristos 	/* Assign sequence number. */
124564f89611Schristos 	/* XXX: use non-QoS tid? */
124664f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
124764f89611Schristos 	*(uint16_t *)&wh->i_seq[0] =
124864f89611Schristos 	    htole16(ic->ic_bss->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
124964f89611Schristos 	ic->ic_bss->ni_txseqs[0]++;
125064f89611Schristos 
125164f89611Schristos 	/* Unmap and free old beacon if any. */
125264f89611Schristos 	if (__predict_true(bf->bf_m != NULL)) {
125364f89611Schristos 		bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0,
125464f89611Schristos 		    bf->bf_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
125564f89611Schristos 		bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
125664f89611Schristos 		m_freem(bf->bf_m);
125764f89611Schristos 		bf->bf_m = NULL;
125864f89611Schristos 	}
125964f89611Schristos 	/* DMA map new beacon. */
126064f89611Schristos 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
126164f89611Schristos 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
126264f89611Schristos 	if (__predict_false(error != 0)) {
126364f89611Schristos 		m_freem(m);
126464f89611Schristos 		return error;
126564f89611Schristos 	}
126664f89611Schristos 	bf->bf_m = m;
126764f89611Schristos 
126864f89611Schristos 	/* Setup Tx descriptor (simplified ar9003_tx()). */
126964f89611Schristos 	ds = bf->bf_descs;
127064f89611Schristos 	memset(ds, 0, sizeof(*ds));
127164f89611Schristos 
127264f89611Schristos 	ds->ds_info =
127364f89611Schristos 	    SM(AR_TXI_DESC_ID, AR_VENDOR_ATHEROS) |
127464f89611Schristos 	    SM(AR_TXI_DESC_NDWORDS, 23) |
127564f89611Schristos 	    SM(AR_TXI_QCU_NUM, ATHN_QID_BEACON) |
127664f89611Schristos 	    AR_TXI_DESC_TX | AR_TXI_CTRL_STAT;
127764f89611Schristos 
127864f89611Schristos 	totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
127964f89611Schristos 	ds->ds_ctl11 = SM(AR_TXC11_FRAME_LEN, totlen);
128064f89611Schristos 	ds->ds_ctl11 |= SM(AR_TXC11_XMIT_POWER, AR_MAX_RATE_POWER);
128164f89611Schristos 	ds->ds_ctl12 = SM(AR_TXC12_FRAME_TYPE, AR_FRAME_TYPE_BEACON);
128264f89611Schristos 	ds->ds_ctl12 |= AR_TXC12_NO_ACK;
128364f89611Schristos 	ds->ds_ctl17 = SM(AR_TXC17_ENCR_TYPE, AR_ENCR_TYPE_CLEAR);
128464f89611Schristos 
128564f89611Schristos 	/* Write number of tries. */
128664f89611Schristos 	ds->ds_ctl13 = SM(AR_TXC13_XMIT_DATA_TRIES0, 1);
128764f89611Schristos 
128864f89611Schristos 	/* Write Tx rate. */
128964f89611Schristos 	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
129064f89611Schristos 	    ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
129164f89611Schristos 	hwrate = athn_rates[ridx].hwrate;
129264f89611Schristos 	ds->ds_ctl14 = SM(AR_TXC14_XMIT_RATE0, hwrate);
129364f89611Schristos 
129464f89611Schristos 	/* Write Tx chains. */
129564f89611Schristos 	ds->ds_ctl18 = SM(AR_TXC18_CHAIN_SEL0, sc->sc_txchainmask);
129664f89611Schristos 
129764f89611Schristos 	ds->ds_segs[0].ds_data = bf->bf_map->dm_segs[0].ds_addr;
129864f89611Schristos 	/* Segment length must be a multiple of 4. */
129964f89611Schristos 	ds->ds_segs[0].ds_ctl |= SM(AR_TXC_BUF_LEN,
130064f89611Schristos 	    (bf->bf_map->dm_segs[0].ds_len + 3) & ~3);
130164f89611Schristos 	/* Compute Tx descriptor checksum. */
130264f89611Schristos 	sum = ds->ds_info;
130364f89611Schristos 	sum += ds->ds_segs[0].ds_data;
130464f89611Schristos 	sum += ds->ds_segs[0].ds_ctl;
130564f89611Schristos 	sum = (sum >> 16) + (sum & 0xffff);
130664f89611Schristos 	ds->ds_ctl10 = SM(AR_TXC10_PTR_CHK_SUM, sum);
130764f89611Schristos 
130864f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
130964f89611Schristos 	    BUS_DMASYNC_PREWRITE);
131064f89611Schristos 
131164f89611Schristos 	/* Stop Tx DMA before putting the new beacon on the queue. */
131264f89611Schristos 	athn_stop_tx_dma(sc, ATHN_QID_BEACON);
131364f89611Schristos 
131464f89611Schristos 	AR_WRITE(sc, AR_QTXDP(ATHN_QID_BEACON), bf->bf_daddr);
131564f89611Schristos 
131664f89611Schristos 	for(;;) {
131764f89611Schristos 		if (SIMPLEQ_EMPTY(&sc->sc_txbufs))
131864f89611Schristos 			break;
131964f89611Schristos 
132064f89611Schristos 		IF_DEQUEUE(&ni->ni_savedq, m);
132164f89611Schristos 		if (m == NULL)
132264f89611Schristos 			break;
132364f89611Schristos 		if (!IF_IS_EMPTY(&ni->ni_savedq)) {
132464f89611Schristos 			/* more queued frames, set the more data bit */
132564f89611Schristos 			wh = mtod(m, struct ieee80211_frame *);
132664f89611Schristos 			wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
132764f89611Schristos 		}
132864f89611Schristos 
132964f89611Schristos 		if (sc->sc_ops.tx(sc, m, ni, ATHN_TXFLAG_CAB) != 0) {
133064f89611Schristos 			ieee80211_free_node(ni);
13312d501ecfSthorpej 			if_statinc(ifp, if_oerrors);
133264f89611Schristos 			break;
133364f89611Schristos 		}
133464f89611Schristos 	}
133564f89611Schristos 
133664f89611Schristos 	/* Kick Tx. */
133764f89611Schristos 	AR_WRITE(sc, AR_Q_TXE, 1 << ATHN_QID_BEACON);
133864f89611Schristos 	AR_WRITE_BARRIER(sc);
133964f89611Schristos 	return 0;
134064f89611Schristos }
134164f89611Schristos #endif
134264f89611Schristos 
13435ffb0503Snonaka static int
ar9003_get_intr_status(struct athn_softc * sc,uint32_t * intrp,uint32_t * syncp)13445ffb0503Snonaka ar9003_get_intr_status(struct athn_softc *sc, uint32_t *intrp, uint32_t *syncp)
134564f89611Schristos {
1346b787afbfSchristos 	uint32_t intr, sync;
134764f89611Schristos 
134864f89611Schristos 	/* Get pending interrupts. */
134964f89611Schristos 	intr = AR_READ(sc, AR_INTR_ASYNC_CAUSE);
135064f89611Schristos 	if (!(intr & AR_INTR_MAC_IRQ) || intr == AR_INTR_SPURIOUS) {
135164f89611Schristos 		intr = AR_READ(sc, AR_INTR_SYNC_CAUSE);
135264f89611Schristos 		if (intr == AR_INTR_SPURIOUS || (intr & sc->sc_isync) == 0)
135364f89611Schristos 			return 0;	/* Not for us. */
135464f89611Schristos 	}
135564f89611Schristos 
135664f89611Schristos 	if ((AR_READ(sc, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) &&
135764f89611Schristos 	    (AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) == AR_RTC_STATUS_ON)
135864f89611Schristos 		intr = AR_READ(sc, AR_ISR);
135964f89611Schristos 	else
136064f89611Schristos 		intr = 0;
136164f89611Schristos 	sync = AR_READ(sc, AR_INTR_SYNC_CAUSE) & sc->sc_isync;
136264f89611Schristos 	if (intr == 0 && sync == 0)
136364f89611Schristos 		return 0;	/* Not for us. */
136464f89611Schristos 
13655ffb0503Snonaka 	*intrp = intr;
13665ffb0503Snonaka 	*syncp = sync;
13675ffb0503Snonaka 	return 1;
13685ffb0503Snonaka }
13695ffb0503Snonaka 
13705ffb0503Snonaka Static int
ar9003_intr_status(struct athn_softc * sc)13715ffb0503Snonaka ar9003_intr_status(struct athn_softc *sc)
13725ffb0503Snonaka {
13735ffb0503Snonaka 	uint32_t intr, sync;
13745ffb0503Snonaka 
13755ffb0503Snonaka 	return ar9003_get_intr_status(sc, &intr, &sync);
13765ffb0503Snonaka }
13775ffb0503Snonaka 
13785ffb0503Snonaka Static int
ar9003_intr(struct athn_softc * sc)13795ffb0503Snonaka ar9003_intr(struct athn_softc *sc)
13805ffb0503Snonaka {
13815ffb0503Snonaka 	uint32_t intr, sync;
13825ffb0503Snonaka #ifndef IEEE80211_STA_ONLY
13835ffb0503Snonaka 	int s;
13845ffb0503Snonaka #endif
13855ffb0503Snonaka 
13865ffb0503Snonaka 	if (!ar9003_get_intr_status(sc, &intr, &sync))
13875ffb0503Snonaka 		return 0;
13885ffb0503Snonaka 
138964f89611Schristos 	if (intr != 0) {
139064f89611Schristos 		if (intr & AR_ISR_BCNMISC) {
1391b787afbfSchristos 			uint32_t intr2 = AR_READ(sc, AR_ISR_S2);
139264f89611Schristos #ifdef notyet
139364f89611Schristos 			if (intr2 & AR_ISR_S2_TIM)
139464f89611Schristos 				/* TBD */;
139564f89611Schristos 			if (intr2 & AR_ISR_S2_TSFOOR)
139664f89611Schristos 				/* TBD */;
139764f89611Schristos 			if (intr2 & AR_ISR_S2_BB_WATCHDOG)
139864f89611Schristos 				/* TBD */;
1399b787afbfSchristos #else
1400b787afbfSchristos 			__USE(intr2);
140164f89611Schristos #endif
140264f89611Schristos 		}
140364f89611Schristos 		intr = AR_READ(sc, AR_ISR_RAC);
140464f89611Schristos 		if (intr == AR_INTR_SPURIOUS)
140564f89611Schristos 			return 1;
140664f89611Schristos 
140764f89611Schristos #ifndef IEEE80211_STA_ONLY
14085ffb0503Snonaka 		if (intr & AR_ISR_SWBA) {
14095ffb0503Snonaka 			s = splnet();
141064f89611Schristos 			ar9003_swba_intr(sc);
14115ffb0503Snonaka 			splx(s);
14125ffb0503Snonaka 		}
141364f89611Schristos #endif
141464f89611Schristos 		if (intr & (AR_ISR_RXMINTR | AR_ISR_RXINTM))
141564f89611Schristos 			ar9003_rx_intr(sc, ATHN_QID_LP);
141664f89611Schristos 		if (intr & (AR_ISR_LP_RXOK | AR_ISR_RXERR))
141764f89611Schristos 			ar9003_rx_intr(sc, ATHN_QID_LP);
141864f89611Schristos 		if (intr & AR_ISR_HP_RXOK)
141964f89611Schristos 			ar9003_rx_intr(sc, ATHN_QID_HP);
142064f89611Schristos 
142164f89611Schristos 		if (intr & (AR_ISR_TXMINTR | AR_ISR_TXINTM))
142264f89611Schristos 			ar9003_tx_intr(sc);
142364f89611Schristos 		if (intr & (AR_ISR_TXOK | AR_ISR_TXERR | AR_ISR_TXEOL))
142464f89611Schristos 			ar9003_tx_intr(sc);
142564f89611Schristos 
142664f89611Schristos 		if (intr & AR_ISR_GENTMR) {
1427b787afbfSchristos 			uint32_t intr5 = AR_READ(sc, AR_ISR_S5_S);
1428b787afbfSchristos #ifdef ATHN_DEBUG
142964f89611Schristos 			DPRINTFN(DBG_INTR, sc,
143064f89611Schristos 			    "GENTMR trigger=%d thresh=%d\n",
143164f89611Schristos 			    MS(intr5, AR_ISR_S5_GENTIMER_TRIG),
143264f89611Schristos 			    MS(intr5, AR_ISR_S5_GENTIMER_THRESH));
1433b787afbfSchristos #else
1434b787afbfSchristos 			__USE(intr5);
1435b787afbfSchristos #endif
143664f89611Schristos 		}
143764f89611Schristos 	}
143864f89611Schristos 	if (sync != 0) {
143964f89611Schristos 		if (sync & AR_INTR_SYNC_RADM_CPL_TIMEOUT) {
144064f89611Schristos 			AR_WRITE(sc, AR_RC, AR_RC_HOSTIF);
144164f89611Schristos 			AR_WRITE(sc, AR_RC, 0);
144264f89611Schristos 		}
144364f89611Schristos 
144464f89611Schristos 		if ((sc->sc_flags & ATHN_FLAG_RFSILENT) &&
144564f89611Schristos 		    (sync & AR_INTR_SYNC_GPIO_PIN(sc->sc_rfsilent_pin))) {
14460c2c845fSmartin 			pmf_event_inject(sc->sc_dev, PMFE_RADIO_OFF);
144764f89611Schristos 			return 1;
144864f89611Schristos 		}
144964f89611Schristos 
145064f89611Schristos 		AR_WRITE(sc, AR_INTR_SYNC_CAUSE, sync);
145164f89611Schristos 		(void)AR_READ(sc, AR_INTR_SYNC_CAUSE);
145264f89611Schristos 	}
145364f89611Schristos 	return 1;
145464f89611Schristos }
145564f89611Schristos 
145664f89611Schristos Static int
ar9003_tx(struct athn_softc * sc,struct mbuf * m,struct ieee80211_node * ni,int txflags)145764f89611Schristos ar9003_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
145864f89611Schristos     int txflags)
145964f89611Schristos {
146064f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
146164f89611Schristos 	struct ieee80211_key *k = NULL;
146264f89611Schristos 	struct ieee80211_frame *wh;
146364f89611Schristos 	struct athn_series series[4];
146464f89611Schristos 	struct ar_tx_desc *ds;
146564f89611Schristos 	struct athn_txq *txq;
146664f89611Schristos 	struct athn_tx_buf *bf;
146764f89611Schristos 	struct athn_node *an = (void *)ni;
146864f89611Schristos 	struct mbuf *m1;
146964f89611Schristos 	uint32_t sum;
147064f89611Schristos 	uint16_t qos;
147164f89611Schristos 	uint8_t txpower, type, encrtype, ridx[4];
147264f89611Schristos 	int i, error, totlen, hasqos, qid;
147364f89611Schristos 
147464f89611Schristos 	/* Grab a Tx buffer from our global free list. */
147564f89611Schristos 	bf = SIMPLEQ_FIRST(&sc->sc_txbufs);
147664f89611Schristos 	KASSERT(bf != NULL);
147764f89611Schristos 
147864f89611Schristos 	/* Map 802.11 frame type to hardware frame type. */
147964f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
148064f89611Schristos 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
148164f89611Schristos 	    IEEE80211_FC0_TYPE_MGT) {
148264f89611Schristos 		/* NB: Beacons do not use ar9003_tx(). */
148364f89611Schristos 		if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
148464f89611Schristos 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
148564f89611Schristos 			type = AR_FRAME_TYPE_PROBE_RESP;
148664f89611Schristos 		else if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
148764f89611Schristos 		    IEEE80211_FC0_SUBTYPE_ATIM)
148864f89611Schristos 			type = AR_FRAME_TYPE_ATIM;
148964f89611Schristos 		else
149064f89611Schristos 			type = AR_FRAME_TYPE_NORMAL;
149164f89611Schristos 	}
149264f89611Schristos 	else if ((wh->i_fc[0] &
149364f89611Schristos 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
149464f89611Schristos 	    (IEEE80211_FC0_TYPE_CTL  | IEEE80211_FC0_SUBTYPE_PS_POLL)) {
149564f89611Schristos 		type = AR_FRAME_TYPE_PSPOLL;
149664f89611Schristos 	}
149764f89611Schristos 	else
149864f89611Schristos 		type = AR_FRAME_TYPE_NORMAL;
149964f89611Schristos 
150064f89611Schristos 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
150164f89611Schristos 		k = ieee80211_crypto_encap(ic, ni, m);
150264f89611Schristos 		if (k == NULL)
150364f89611Schristos 			return ENOBUFS;
150464f89611Schristos 
150564f89611Schristos 		/* packet header may have moved, reset our local pointer */
150664f89611Schristos 		wh = mtod(m, struct ieee80211_frame *);
150764f89611Schristos 	}
150864f89611Schristos 
150964f89611Schristos 	/* XXX 2-byte padding for QoS and 4-addr headers. */
151064f89611Schristos 
151164f89611Schristos 	/* Select the HW Tx queue to use for this frame. */
151264f89611Schristos 	if ((hasqos = ieee80211_has_qos(wh))) {
151364f89611Schristos #ifdef notyet_edca
151464f89611Schristos 		uint8_t tid;
151564f89611Schristos 
151664f89611Schristos 		qos = ieee80211_get_qos(wh);
151764f89611Schristos 		tid = qos & IEEE80211_QOS_TID;
151864f89611Schristos 		qid = athn_ac2qid[ieee80211_up_to_ac(ic, tid)];
151964f89611Schristos #else
152064f89611Schristos 		qos = ieee80211_get_qos(wh);
152164f89611Schristos 		qid = ATHN_QID_AC_BE;
152264f89611Schristos #endif /* notyet_edca */
152364f89611Schristos 	}
152464f89611Schristos 	else if (type == AR_FRAME_TYPE_PSPOLL) {
152564f89611Schristos 		qos = 0;
152664f89611Schristos 		qid = ATHN_QID_PSPOLL;
152764f89611Schristos 	}
152864f89611Schristos 	else if (txflags & ATHN_TXFLAG_CAB) {
152964f89611Schristos 		qos = 0;
153064f89611Schristos 		qid = ATHN_QID_CAB;
153164f89611Schristos 	}
153264f89611Schristos 	else {
153364f89611Schristos 		qos = 0;
153464f89611Schristos 		qid = ATHN_QID_AC_BE;
153564f89611Schristos 	}
153664f89611Schristos 	txq = &sc->sc_txq[qid];
153764f89611Schristos 
153864f89611Schristos 	/* Select the transmit rates to use for this frame. */
153964f89611Schristos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
154064f89611Schristos 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
154164f89611Schristos 	    IEEE80211_FC0_TYPE_DATA) {
154264f89611Schristos 		/* Use lowest rate for all tries. */
154364f89611Schristos 		ridx[0] = ridx[1] = ridx[2] = ridx[3] =
154464f89611Schristos 		    (ic->ic_curmode == IEEE80211_MODE_11A) ?
154564f89611Schristos 			ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
154664f89611Schristos 	}
154764f89611Schristos 	else if (ic->ic_fixed_rate != -1) {
154864f89611Schristos 		/* Use same fixed rate for all tries. */
154964f89611Schristos 		ridx[0] = ridx[1] = ridx[2] = ridx[3] =
155064f89611Schristos 		    sc->sc_fixed_ridx;
155164f89611Schristos 	}
155264f89611Schristos 	else {
155364f89611Schristos 		int txrate = ni->ni_txrate;
155464f89611Schristos 		/* Use fallback table of the node. */
155564f89611Schristos 		for (i = 0; i < 4; i++) {
155664f89611Schristos 			ridx[i] = an->ridx[txrate];
155764f89611Schristos 			txrate = an->fallback[txrate];
155864f89611Schristos 		}
155964f89611Schristos 	}
156064f89611Schristos 
156164f89611Schristos 	if (__predict_false(sc->sc_drvbpf != NULL)) {
156264f89611Schristos 		struct athn_tx_radiotap_header *tap = &sc->sc_txtap;
156364f89611Schristos 
156464f89611Schristos 		tap->wt_flags = 0;
156564f89611Schristos 		/* Use initial transmit rate. */
156664f89611Schristos 		tap->wt_rate = athn_rates[ridx[0]].rate;
156764f89611Schristos 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
156864f89611Schristos 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
156964f89611Schristos // XXX		tap->wt_hwqueue = qid;
157064f89611Schristos 		if (ridx[0] != ATHN_RIDX_CCK1 &&
157164f89611Schristos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
157264f89611Schristos 			tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
157364f89611Schristos 
15743cd62456Smsaitoh 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m, BPF_D_OUT);
157564f89611Schristos 	}
157664f89611Schristos 
157764f89611Schristos 	/* DMA map mbuf. */
157864f89611Schristos 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
157964f89611Schristos 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
158064f89611Schristos 	if (__predict_false(error != 0)) {
158164f89611Schristos 		if (error != EFBIG) {
1582dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
1583dabffed6Schristos 			    "can't map mbuf (error %d)\n", error);
158464f89611Schristos 			m_freem(m);
158564f89611Schristos 			return error;
158664f89611Schristos 		}
158764f89611Schristos 		/*
158864f89611Schristos 		 * DMA mapping requires too many DMA segments; linearize
158964f89611Schristos 		 * mbuf in kernel virtual address space and retry.
159064f89611Schristos 		 */
159164f89611Schristos 		MGETHDR(m1, M_DONTWAIT, MT_DATA);
159264f89611Schristos 		if (m1 == NULL) {
159364f89611Schristos 			m_freem(m);
159464f89611Schristos 			return ENOBUFS;
159564f89611Schristos 		}
159664f89611Schristos 		if (m->m_pkthdr.len > (int)MHLEN) {
159764f89611Schristos 			MCLGET(m1, M_DONTWAIT);
159864f89611Schristos 			if (!(m1->m_flags & M_EXT)) {
159964f89611Schristos 				m_freem(m);
160064f89611Schristos 				m_freem(m1);
160164f89611Schristos 				return ENOBUFS;
160264f89611Schristos 			}
160364f89611Schristos 		}
160464f89611Schristos 		m_copydata(m, 0, m->m_pkthdr.len, mtod(m1, void *));
160564f89611Schristos 		m1->m_pkthdr.len = m1->m_len = m->m_pkthdr.len;
160664f89611Schristos 		m_freem(m);
160764f89611Schristos 		m = m1;
160864f89611Schristos 
160964f89611Schristos 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
161064f89611Schristos 		    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
161164f89611Schristos 		if (error != 0) {
1612dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
1613dabffed6Schristos 			    "can't map mbuf (error %d)\n", error);
161464f89611Schristos 			m_freem(m);
161564f89611Schristos 			return error;
161664f89611Schristos 		}
161764f89611Schristos 	}
161864f89611Schristos 	bf->bf_m = m;
161964f89611Schristos 	bf->bf_ni = ni;
162064f89611Schristos 	bf->bf_txflags = txflags;
162164f89611Schristos 
162264f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
162364f89611Schristos 
162464f89611Schristos 	totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
162564f89611Schristos 
162664f89611Schristos 	/* Setup Tx descriptor. */
162764f89611Schristos 	ds = bf->bf_descs;
162864f89611Schristos 	memset(ds, 0, sizeof(*ds));
162964f89611Schristos 
163064f89611Schristos 	ds->ds_info =
163164f89611Schristos 	    SM(AR_TXI_DESC_ID, AR_VENDOR_ATHEROS) |
163264f89611Schristos 	    SM(AR_TXI_DESC_NDWORDS, 23) |
163364f89611Schristos 	    SM(AR_TXI_QCU_NUM, qid) |
163464f89611Schristos 	    AR_TXI_DESC_TX | AR_TXI_CTRL_STAT;
163564f89611Schristos 
163664f89611Schristos 	ds->ds_ctl11 = AR_TXC11_CLR_DEST_MASK;
163764f89611Schristos 	txpower = AR_MAX_RATE_POWER;	/* Get from per-rate registers. */
163864f89611Schristos 	ds->ds_ctl11 |= SM(AR_TXC11_XMIT_POWER, txpower);
163964f89611Schristos 
164064f89611Schristos 	ds->ds_ctl12 = SM(AR_TXC12_FRAME_TYPE, type);
164164f89611Schristos 
164264f89611Schristos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
164364f89611Schristos 	    (hasqos && (qos & IEEE80211_QOS_ACKPOLICY_MASK) ==
164464f89611Schristos 	     IEEE80211_QOS_ACKPOLICY_NOACK))
164564f89611Schristos 		ds->ds_ctl12 |= AR_TXC12_NO_ACK;
164664f89611Schristos 
164764f89611Schristos #if notyet
164864f89611Schristos 	if (0 && k != NULL) {
164964f89611Schristos 		uintptr_t entry;
165064f89611Schristos 
165164f89611Schristos 		/*
165264f89611Schristos 		 * Map 802.11 cipher to hardware encryption type and
165364f89611Schristos 		 * compute MIC+ICV overhead.
165464f89611Schristos 		 */
165564f89611Schristos 		switch (k->k_cipher) {
165664f89611Schristos 		case IEEE80211_CIPHER_WEP40:
165764f89611Schristos 		case IEEE80211_CIPHER_WEP104:
165864f89611Schristos 			encrtype = AR_ENCR_TYPE_WEP;
165964f89611Schristos 			totlen += 4;
166064f89611Schristos 			break;
166164f89611Schristos 		case IEEE80211_CIPHER_TKIP:
166264f89611Schristos 			encrtype = AR_ENCR_TYPE_TKIP;
166364f89611Schristos 			totlen += 12;
166464f89611Schristos 			break;
166564f89611Schristos 		case IEEE80211_CIPHER_CCMP:
166664f89611Schristos 			encrtype = AR_ENCR_TYPE_AES;
166764f89611Schristos 			totlen += 8;
166864f89611Schristos 			break;
166964f89611Schristos 		default:
167064f89611Schristos 			panic("unsupported cipher");
167164f89611Schristos 		}
167264f89611Schristos 		/*
167364f89611Schristos 		 * NB: The key cache entry index is stored in the key
167464f89611Schristos 		 * private field when the key is installed.
167564f89611Schristos 		 */
167664f89611Schristos 		entry = (uintptr_t)k->k_priv;
167764f89611Schristos 		ds->ds_ctl12 |= SM(AR_TXC12_DEST_IDX, entry);
167864f89611Schristos 		ds->ds_ctl11 |= AR_TXC11_DEST_IDX_VALID;
167964f89611Schristos 	}
168064f89611Schristos 	else
168164f89611Schristos #endif
168264f89611Schristos 		encrtype = AR_ENCR_TYPE_CLEAR;
168364f89611Schristos 	ds->ds_ctl17 = SM(AR_TXC17_ENCR_TYPE, encrtype);
168464f89611Schristos 
168564f89611Schristos 	/* Check if frame must be protected using RTS/CTS or CTS-to-self. */
168664f89611Schristos 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
168764f89611Schristos 		/* NB: Group frames are sent using CCK in 802.11b/g. */
168864f89611Schristos 		if (totlen > ic->ic_rtsthreshold) {
168964f89611Schristos 			ds->ds_ctl11 |= AR_TXC11_RTS_ENABLE;
169064f89611Schristos 		}
169164f89611Schristos 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
169264f89611Schristos 		    athn_rates[ridx[0]].phy == IEEE80211_T_OFDM) {
169364f89611Schristos 			if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
169464f89611Schristos 				ds->ds_ctl11 |= AR_TXC11_RTS_ENABLE;
169564f89611Schristos 			else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
169664f89611Schristos 				ds->ds_ctl11 |= AR_TXC11_CTS_ENABLE;
169764f89611Schristos 		}
169864f89611Schristos 	}
169964f89611Schristos 	if (ds->ds_ctl11 & (AR_TXC11_RTS_ENABLE | AR_TXC11_CTS_ENABLE)) {
170064f89611Schristos 		/* Disable multi-rate retries when protection is used. */
170164f89611Schristos 		ridx[1] = ridx[2] = ridx[3] = ridx[0];
170264f89611Schristos 	}
170364f89611Schristos 	/* Setup multi-rate retries. */
170464f89611Schristos 	for (i = 0; i < 4; i++) {
170564f89611Schristos 		series[i].hwrate = athn_rates[ridx[i]].hwrate;
170664f89611Schristos 		if (athn_rates[ridx[i]].phy == IEEE80211_T_DS &&
170764f89611Schristos 		    ridx[i] != ATHN_RIDX_CCK1 &&
170864f89611Schristos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
170964f89611Schristos 			series[i].hwrate |= 0x04;
171064f89611Schristos 		series[i].dur = 0;
171164f89611Schristos 	}
171264f89611Schristos 	if (!(ds->ds_ctl12 & AR_TXC12_NO_ACK)) {
171364f89611Schristos 		/* Compute duration for each series. */
171464f89611Schristos 		for (i = 0; i < 4; i++) {
171564f89611Schristos 			series[i].dur = athn_txtime(sc, IEEE80211_ACK_LEN,
171664f89611Schristos 			    athn_rates[ridx[i]].rspridx, ic->ic_flags);
171764f89611Schristos 		}
171864f89611Schristos 	}
171964f89611Schristos 	/* If this is a PA training frame, select the Tx chain to use. */
172064f89611Schristos 	if (__predict_false(txflags & ATHN_TXFLAG_PAPRD)) {
172164f89611Schristos 		ds->ds_ctl12 |= SM(AR_TXC12_PAPRD_CHAIN_MASK,
172264f89611Schristos 		    1 << sc->sc_paprd_curchain);
172364f89611Schristos 	}
172464f89611Schristos 
172564f89611Schristos 	/* Write number of tries for each series. */
172664f89611Schristos 	ds->ds_ctl13 =
172764f89611Schristos 	    SM(AR_TXC13_XMIT_DATA_TRIES0, 2) |
172864f89611Schristos 	    SM(AR_TXC13_XMIT_DATA_TRIES1, 2) |
172964f89611Schristos 	    SM(AR_TXC13_XMIT_DATA_TRIES2, 2) |
173064f89611Schristos 	    SM(AR_TXC13_XMIT_DATA_TRIES3, 4);
173164f89611Schristos 
173264f89611Schristos 	/* Tell HW to update duration field in 802.11 header. */
173364f89611Schristos 	if (type != AR_FRAME_TYPE_PSPOLL)
173464f89611Schristos 		ds->ds_ctl13 |= AR_TXC13_DUR_UPDATE_ENA;
173564f89611Schristos 
173664f89611Schristos 	/* Write Tx rate for each series. */
173764f89611Schristos 	ds->ds_ctl14 =
173864f89611Schristos 	    SM(AR_TXC14_XMIT_RATE0, series[0].hwrate) |
173964f89611Schristos 	    SM(AR_TXC14_XMIT_RATE1, series[1].hwrate) |
174064f89611Schristos 	    SM(AR_TXC14_XMIT_RATE2, series[2].hwrate) |
174164f89611Schristos 	    SM(AR_TXC14_XMIT_RATE3, series[3].hwrate);
174264f89611Schristos 
174364f89611Schristos 	/* Write duration for each series. */
174464f89611Schristos 	ds->ds_ctl15 =
174564f89611Schristos 	    SM(AR_TXC15_PACKET_DUR0, series[0].dur) |
174664f89611Schristos 	    SM(AR_TXC15_PACKET_DUR1, series[1].dur);
174764f89611Schristos 	ds->ds_ctl16 =
174864f89611Schristos 	    SM(AR_TXC16_PACKET_DUR2, series[2].dur) |
174964f89611Schristos 	    SM(AR_TXC16_PACKET_DUR3, series[3].dur);
175064f89611Schristos 
175164f89611Schristos 	if ((sc->sc_flags & ATHN_FLAG_3TREDUCE_CHAIN) &&
175264f89611Schristos 	    ic->ic_curmode == IEEE80211_MODE_11A) {
175364f89611Schristos 		/*
175464f89611Schristos 		 * In order to not exceed PCIe power requirements, we only
175564f89611Schristos 		 * use two Tx chains for MCS0~15 on 5GHz band on these chips.
175664f89611Schristos 		 */
175764f89611Schristos 		ds->ds_ctl18 =
175864f89611Schristos 		    SM(AR_TXC18_CHAIN_SEL0,
175964f89611Schristos 			(ridx[0] <= ATHN_RIDX_MCS15) ? 0x3 : sc->sc_txchainmask) |
176064f89611Schristos 		    SM(AR_TXC18_CHAIN_SEL1,
176164f89611Schristos 			(ridx[1] <= ATHN_RIDX_MCS15) ? 0x3 : sc->sc_txchainmask) |
176264f89611Schristos 		    SM(AR_TXC18_CHAIN_SEL2,
176364f89611Schristos 			(ridx[2] <= ATHN_RIDX_MCS15) ? 0x3 : sc->sc_txchainmask) |
176464f89611Schristos 		    SM(AR_TXC18_CHAIN_SEL3,
176564f89611Schristos 			(ridx[3] <= ATHN_RIDX_MCS15) ? 0x3 : sc->sc_txchainmask);
176664f89611Schristos 	}
176764f89611Schristos 	else {
176864f89611Schristos 		/* Use the same Tx chains for all tries. */
176964f89611Schristos 		ds->ds_ctl18 =
177064f89611Schristos 		    SM(AR_TXC18_CHAIN_SEL0, sc->sc_txchainmask) |
177164f89611Schristos 		    SM(AR_TXC18_CHAIN_SEL1, sc->sc_txchainmask) |
177264f89611Schristos 		    SM(AR_TXC18_CHAIN_SEL2, sc->sc_txchainmask) |
177364f89611Schristos 		    SM(AR_TXC18_CHAIN_SEL3, sc->sc_txchainmask);
177464f89611Schristos 	}
177564f89611Schristos #ifdef notyet
177664f89611Schristos #ifndef IEEE80211_NO_HT
177764f89611Schristos 	/* Use the same short GI setting for all tries. */
177864f89611Schristos 	if (ic->ic_flags & IEEE80211_F_SHGI)
177964f89611Schristos 		ds->ds_ctl18 |= AR_TXC18_GI0123;
178064f89611Schristos 	/* Use the same channel width for all tries. */
178164f89611Schristos 	if (ic->ic_flags & IEEE80211_F_CBW40)
178264f89611Schristos 		ds->ds_ctl18 |= AR_TXC18_2040_0123;
178364f89611Schristos #endif
178464f89611Schristos #endif
178564f89611Schristos 
178664f89611Schristos 	if (ds->ds_ctl11 & (AR_TXC11_RTS_ENABLE | AR_TXC11_CTS_ENABLE)) {
178764f89611Schristos 		uint8_t protridx, hwrate;
178864f89611Schristos 		uint16_t dur = 0;
178964f89611Schristos 
179064f89611Schristos 		/* Use the same protection mode for all tries. */
179164f89611Schristos 		if (ds->ds_ctl11 & AR_TXC11_RTS_ENABLE) {
179264f89611Schristos 			ds->ds_ctl15 |= AR_TXC15_RTSCTS_QUAL01;
179364f89611Schristos 			ds->ds_ctl16 |= AR_TXC16_RTSCTS_QUAL23;
179464f89611Schristos 		}
179564f89611Schristos 		/* Select protection rate (suboptimal but ok). */
179664f89611Schristos 		protridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
179764f89611Schristos 		    ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK2;
179864f89611Schristos 		if (ds->ds_ctl11 & AR_TXC11_RTS_ENABLE) {
179964f89611Schristos 			/* Account for CTS duration. */
180064f89611Schristos 			dur += athn_txtime(sc, IEEE80211_ACK_LEN,
180164f89611Schristos 			    athn_rates[protridx].rspridx, ic->ic_flags);
180264f89611Schristos 		}
180364f89611Schristos 		dur += athn_txtime(sc, totlen, ridx[0], ic->ic_flags);
180464f89611Schristos 		if (!(ds->ds_ctl12 & AR_TXC12_NO_ACK)) {
180564f89611Schristos 			/* Account for ACK duration. */
180664f89611Schristos 			dur += athn_txtime(sc, IEEE80211_ACK_LEN,
180764f89611Schristos 			    athn_rates[ridx[0]].rspridx, ic->ic_flags);
180864f89611Schristos 		}
180964f89611Schristos 		/* Write protection frame duration and rate. */
181064f89611Schristos 		ds->ds_ctl13 |= SM(AR_TXC13_BURST_DUR, dur);
181164f89611Schristos 		hwrate = athn_rates[protridx].hwrate;
181264f89611Schristos 		if (protridx == ATHN_RIDX_CCK2 &&
181364f89611Schristos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
181464f89611Schristos 			hwrate |= 0x04;
181564f89611Schristos 		ds->ds_ctl18 |= SM(AR_TXC18_RTSCTS_RATE, hwrate);
181664f89611Schristos 	}
181764f89611Schristos 
181864f89611Schristos 	ds->ds_ctl11 |= SM(AR_TXC11_FRAME_LEN, totlen);
181964f89611Schristos 	ds->ds_ctl19 = AR_TXC19_NOT_SOUNDING;
182064f89611Schristos 
182164f89611Schristos 	for (i = 0; i < bf->bf_map->dm_nsegs; i++) {
182264f89611Schristos 		ds->ds_segs[i].ds_data = bf->bf_map->dm_segs[i].ds_addr;
182364f89611Schristos 		ds->ds_segs[i].ds_ctl = SM(AR_TXC_BUF_LEN,
182464f89611Schristos 		    bf->bf_map->dm_segs[i].ds_len);
182564f89611Schristos 	}
182664f89611Schristos 	/* Compute Tx descriptor checksum. */
182764f89611Schristos 	sum = ds->ds_info + ds->ds_link;
182864f89611Schristos 	for (i = 0; i < 4; i++) {
182964f89611Schristos 		sum += ds->ds_segs[i].ds_data;
183064f89611Schristos 		sum += ds->ds_segs[i].ds_ctl;
183164f89611Schristos 	}
183264f89611Schristos 	sum = (sum >> 16) + (sum & 0xffff);
183364f89611Schristos 	ds->ds_ctl10 = SM(AR_TXC10_PTR_CHK_SUM, sum);
183464f89611Schristos 
183564f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
183664f89611Schristos 	    BUS_DMASYNC_PREWRITE);
183764f89611Schristos 
183864f89611Schristos 	DPRINTFN(DBG_TX, sc,
183964f89611Schristos 	    "Tx qid=%d nsegs=%d ctl11=0x%x ctl12=0x%x ctl14=0x%x\n",
184064f89611Schristos 	    qid, bf->bf_map->dm_nsegs, ds->ds_ctl11, ds->ds_ctl12,
184164f89611Schristos 	    ds->ds_ctl14);
184264f89611Schristos 
184364f89611Schristos 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txbufs, bf_list);
184464f89611Schristos 	SIMPLEQ_INSERT_TAIL(&txq->head, bf, bf_list);
184564f89611Schristos 
184664f89611Schristos 	/* Queue buffer unless hardware FIFO is already full. */
184764f89611Schristos 	if (++txq->queued <= AR9003_TX_QDEPTH) {
184864f89611Schristos 		AR_WRITE(sc, AR_QTXDP(qid), bf->bf_daddr);
184964f89611Schristos 		AR_WRITE_BARRIER(sc);
185064f89611Schristos 	}
185164f89611Schristos 	else if (txq->wait == NULL)
185264f89611Schristos 		txq->wait = bf;
185364f89611Schristos 	return 0;
185464f89611Schristos }
185564f89611Schristos 
185664f89611Schristos Static void
ar9003_set_rf_mode(struct athn_softc * sc,struct ieee80211_channel * c)185764f89611Schristos ar9003_set_rf_mode(struct athn_softc *sc, struct ieee80211_channel *c)
185864f89611Schristos {
185964f89611Schristos 	uint32_t reg;
186064f89611Schristos 
186164f89611Schristos 	reg = IEEE80211_IS_CHAN_2GHZ(c) ?
186264f89611Schristos 	    AR_PHY_MODE_DYNAMIC : AR_PHY_MODE_OFDM;
186364f89611Schristos 	if (IEEE80211_IS_CHAN_5GHZ(c) &&
186464f89611Schristos 	    (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
186564f89611Schristos 		reg |= AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE;
186664f89611Schristos 	}
186764f89611Schristos 	AR_WRITE(sc, AR_PHY_MODE, reg);
186864f89611Schristos 	AR_WRITE_BARRIER(sc);
186964f89611Schristos }
187064f89611Schristos 
187164f89611Schristos static __inline uint32_t
ar9003_synth_delay(struct athn_softc * sc)187264f89611Schristos ar9003_synth_delay(struct athn_softc *sc)
187364f89611Schristos {
18745abc6aa5Sskrll 	uint32_t synth_delay;
187564f89611Schristos 
18765abc6aa5Sskrll 	synth_delay = MS(AR_READ(sc, AR_PHY_RX_DELAY), AR_PHY_RX_DELAY_DELAY);
187764f89611Schristos 	if (sc->sc_ic.ic_curmode == IEEE80211_MODE_11B)
18785abc6aa5Sskrll 		synth_delay = (synth_delay * 4) / 22;
187964f89611Schristos 	else
18805abc6aa5Sskrll 		synth_delay = synth_delay / 10;	/* in 100ns steps */
18815abc6aa5Sskrll 	return synth_delay;
188264f89611Schristos }
188364f89611Schristos 
188464f89611Schristos Static int
ar9003_rf_bus_request(struct athn_softc * sc)188564f89611Schristos ar9003_rf_bus_request(struct athn_softc *sc)
188664f89611Schristos {
188764f89611Schristos 	int ntries;
188864f89611Schristos 
188964f89611Schristos 	/* Request RF Bus grant. */
189064f89611Schristos 	AR_WRITE(sc, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
189164f89611Schristos 	for (ntries = 0; ntries < 10000; ntries++) {
189264f89611Schristos 		if (AR_READ(sc, AR_PHY_RFBUS_GRANT) & AR_PHY_RFBUS_GRANT_EN)
189364f89611Schristos 			return 0;
189464f89611Schristos 		DELAY(10);
189564f89611Schristos 	}
189664f89611Schristos 	DPRINTFN(DBG_RF, sc, "could not kill baseband Rx");
189764f89611Schristos 	return ETIMEDOUT;
189864f89611Schristos }
189964f89611Schristos 
190064f89611Schristos Static void
ar9003_rf_bus_release(struct athn_softc * sc)190164f89611Schristos ar9003_rf_bus_release(struct athn_softc *sc)
190264f89611Schristos {
190364f89611Schristos 	/* Wait for the synthesizer to settle. */
190464f89611Schristos 	DELAY(AR_BASE_PHY_ACTIVE_DELAY + ar9003_synth_delay(sc));
190564f89611Schristos 
190664f89611Schristos 	/* Release the RF Bus grant. */
190764f89611Schristos 	AR_WRITE(sc, AR_PHY_RFBUS_REQ, 0);
190864f89611Schristos 	AR_WRITE_BARRIER(sc);
190964f89611Schristos }
191064f89611Schristos 
191164f89611Schristos Static void
ar9003_set_phy(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)191264f89611Schristos ar9003_set_phy(struct athn_softc *sc, struct ieee80211_channel *c,
191364f89611Schristos     struct ieee80211_channel *extc)
191464f89611Schristos {
191564f89611Schristos 	uint32_t phy;
191664f89611Schristos 
191764f89611Schristos 	phy = AR_READ(sc, AR_PHY_GEN_CTRL);
191864f89611Schristos 	phy |= AR_PHY_GC_HT_EN | AR_PHY_GC_SHORT_GI_40 |
191964f89611Schristos 	    AR_PHY_GC_SINGLE_HT_LTF1 | AR_PHY_GC_WALSH;
192064f89611Schristos #ifndef IEEE80211_NO_HT
192164f89611Schristos 	if (extc != NULL) {
192264f89611Schristos 		phy |= AR_PHY_GC_DYN2040_EN;
192364f89611Schristos 		if (extc > c)	/* XXX */
192464f89611Schristos 			phy |= AR_PHY_GC_DYN2040_PRI_CH;
192564f89611Schristos 	}
192664f89611Schristos #endif
192764f89611Schristos 	/* Turn off Green Field detection for now. */
192864f89611Schristos 	phy &= ~AR_PHY_GC_GF_DETECT_EN;
192964f89611Schristos 	AR_WRITE(sc, AR_PHY_GEN_CTRL, phy);
193064f89611Schristos 
193164f89611Schristos 	AR_WRITE(sc, AR_2040_MODE,
193264f89611Schristos 	    (extc != NULL) ? AR_2040_JOINED_RX_CLEAR : 0);
193364f89611Schristos 
193464f89611Schristos 	/* Set global transmit timeout. */
193564f89611Schristos 	AR_WRITE(sc, AR_GTXTO, SM(AR_GTXTO_TIMEOUT_LIMIT, 25));
193664f89611Schristos 	/* Set carrier sense timeout. */
193764f89611Schristos 	AR_WRITE(sc, AR_CST, SM(AR_CST_TIMEOUT_LIMIT, 15));
193864f89611Schristos 	AR_WRITE_BARRIER(sc);
193964f89611Schristos }
194064f89611Schristos 
194164f89611Schristos Static void
ar9003_set_delta_slope(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)194264f89611Schristos ar9003_set_delta_slope(struct athn_softc *sc, struct ieee80211_channel *c,
194364f89611Schristos     struct ieee80211_channel *extc)
194464f89611Schristos {
194564f89611Schristos 	uint32_t coeff, exp, man, reg;
194664f89611Schristos 
194764f89611Schristos 	/* Set Delta Slope (exponent and mantissa). */
194864f89611Schristos 	coeff = (100 << 24) / c->ic_freq;
194964f89611Schristos 	athn_get_delta_slope(coeff, &exp, &man);
195064f89611Schristos 	DPRINTFN(DBG_RF, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
195164f89611Schristos 
195264f89611Schristos 	reg = AR_READ(sc, AR_PHY_TIMING3);
195364f89611Schristos 	reg = RW(reg, AR_PHY_TIMING3_DSC_EXP, exp);
195464f89611Schristos 	reg = RW(reg, AR_PHY_TIMING3_DSC_MAN, man);
195564f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING3, reg);
195664f89611Schristos 
195764f89611Schristos 	/* For Short GI, coeff is 9/10 that of normal coeff. */
195864f89611Schristos 	coeff = (9 * coeff) / 10;
195964f89611Schristos 	athn_get_delta_slope(coeff, &exp, &man);
196064f89611Schristos 	DPRINTFN(DBG_RF, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
196164f89611Schristos 
196264f89611Schristos 	reg = AR_READ(sc, AR_PHY_SGI_DELTA);
196364f89611Schristos 	reg = RW(reg, AR_PHY_SGI_DSC_EXP, exp);
196464f89611Schristos 	reg = RW(reg, AR_PHY_SGI_DSC_MAN, man);
196564f89611Schristos 	AR_WRITE(sc, AR_PHY_SGI_DELTA, reg);
196664f89611Schristos 	AR_WRITE_BARRIER(sc);
196764f89611Schristos }
196864f89611Schristos 
196964f89611Schristos Static void
ar9003_enable_antenna_diversity(struct athn_softc * sc)197064f89611Schristos ar9003_enable_antenna_diversity(struct athn_softc *sc)
197164f89611Schristos {
197264f89611Schristos 	AR_SETBITS(sc, AR_PHY_CCK_DETECT,
197364f89611Schristos 	    AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
197464f89611Schristos 	AR_WRITE_BARRIER(sc);
197564f89611Schristos }
197664f89611Schristos 
197764f89611Schristos Static void
ar9003_init_baseband(struct athn_softc * sc)197864f89611Schristos ar9003_init_baseband(struct athn_softc *sc)
197964f89611Schristos {
198064f89611Schristos 	uint32_t synth_delay;
198164f89611Schristos 
198264f89611Schristos 	synth_delay = ar9003_synth_delay(sc);
198364f89611Schristos 	/* Activate the PHY (includes baseband activate and synthesizer on). */
198464f89611Schristos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
198564f89611Schristos 	AR_WRITE_BARRIER(sc);
198664f89611Schristos 	DELAY(AR_BASE_PHY_ACTIVE_DELAY + synth_delay);
198764f89611Schristos }
198864f89611Schristos 
198964f89611Schristos Static void
ar9003_disable_phy(struct athn_softc * sc)199064f89611Schristos ar9003_disable_phy(struct athn_softc *sc)
199164f89611Schristos {
199264f89611Schristos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
199364f89611Schristos 	AR_WRITE_BARRIER(sc);
199464f89611Schristos }
199564f89611Schristos 
199664f89611Schristos Static void
ar9003_init_chains(struct athn_softc * sc)199764f89611Schristos ar9003_init_chains(struct athn_softc *sc)
199864f89611Schristos {
199964f89611Schristos 	if (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5)
200064f89611Schristos 		AR_SETBITS(sc, AR_PHY_ANALOG_SWAP, AR_PHY_SWAP_ALT_CHAIN);
200164f89611Schristos 
200264f89611Schristos 	/* Setup chain masks. */
200364f89611Schristos 	AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  sc->sc_rxchainmask);
200464f89611Schristos 	AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
200564f89611Schristos 
200664f89611Schristos 	if (sc->sc_flags & ATHN_FLAG_3TREDUCE_CHAIN) {
200764f89611Schristos 		/*
200864f89611Schristos 		 * All self-generated frames are sent using two Tx chains
200964f89611Schristos 		 * on these chips to not exceed PCIe power requirements.
201064f89611Schristos 		 */
201164f89611Schristos 		AR_WRITE(sc, AR_SELFGEN_MASK, 0x3);
201264f89611Schristos 	}
201364f89611Schristos 	else
201464f89611Schristos 		AR_WRITE(sc, AR_SELFGEN_MASK, sc->sc_txchainmask);
201564f89611Schristos 	AR_WRITE_BARRIER(sc);
201664f89611Schristos }
201764f89611Schristos 
201864f89611Schristos Static void
ar9003_set_rxchains(struct athn_softc * sc)201964f89611Schristos ar9003_set_rxchains(struct athn_softc *sc)
202064f89611Schristos {
202164f89611Schristos 	if (sc->sc_rxchainmask == 0x3 || sc->sc_rxchainmask == 0x5) {
202264f89611Schristos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  sc->sc_rxchainmask);
202364f89611Schristos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
202464f89611Schristos 		AR_WRITE_BARRIER(sc);
202564f89611Schristos 	}
202664f89611Schristos }
202764f89611Schristos 
202864f89611Schristos #ifdef notused
202964f89611Schristos Static void
ar9003_read_noisefloor(struct athn_softc * sc,int16_t * nf,int16_t * nf_ext)203064f89611Schristos ar9003_read_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
203164f89611Schristos {
203264f89611Schristos /* Sign-extends 9-bit value (assumes upper bits are zeroes). */
203364f89611Schristos #define SIGN_EXT(v)	(((v) ^ 0x100) - 0x100)
203464f89611Schristos 	uint32_t reg;
203564f89611Schristos 	int i;
203664f89611Schristos 
203764f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
203864f89611Schristos 		reg = AR_READ(sc, AR_PHY_CCA(i));
203964f89611Schristos 		nf[i] = MS(reg, AR_PHY_MINCCA_PWR);
204064f89611Schristos 		nf[i] = SIGN_EXT(nf[i]);
204164f89611Schristos 
204264f89611Schristos 		reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
204364f89611Schristos 		nf_ext[i] = MS(reg, AR_PHY_EXT_MINCCA_PWR);
204464f89611Schristos 		nf_ext[i] = SIGN_EXT(nf_ext[i]);
204564f89611Schristos 	}
204664f89611Schristos #undef SIGN_EXT
204764f89611Schristos }
204864f89611Schristos #endif /* notused */
204964f89611Schristos 
205064f89611Schristos #ifdef notused
205164f89611Schristos Static void
ar9003_write_noisefloor(struct athn_softc * sc,int16_t * nf,int16_t * nf_ext)205264f89611Schristos ar9003_write_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
205364f89611Schristos {
205464f89611Schristos 	uint32_t reg;
205564f89611Schristos 	int i;
205664f89611Schristos 
205764f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
205864f89611Schristos 		reg = AR_READ(sc, AR_PHY_CCA(i));
205964f89611Schristos 		reg = RW(reg, AR_PHY_MAXCCA_PWR, nf[i]);
206064f89611Schristos 		AR_WRITE(sc, AR_PHY_CCA(i), reg);
206164f89611Schristos 
206264f89611Schristos 		reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
206364f89611Schristos 		reg = RW(reg, AR_PHY_EXT_MAXCCA_PWR, nf_ext[i]);
206464f89611Schristos 		AR_WRITE(sc, AR_PHY_EXT_CCA(i), reg);
206564f89611Schristos 	}
206664f89611Schristos 	AR_WRITE_BARRIER(sc);
206764f89611Schristos }
206864f89611Schristos #endif /* notused */
206964f89611Schristos 
207064f89611Schristos #ifdef notused
207164f89611Schristos Static void
ar9003_get_noisefloor(struct athn_softc * sc,struct ieee80211_channel * c)207264f89611Schristos ar9003_get_noisefloor(struct athn_softc *sc, struct ieee80211_channel *c)
207364f89611Schristos {
207464f89611Schristos 	int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
207564f89611Schristos 	int16_t cca_min, cca_max;
207664f89611Schristos 	int i;
207764f89611Schristos 
207864f89611Schristos 	if (AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
207964f89611Schristos 		/* Noisefloor calibration not finished. */
208064f89611Schristos 		return;
208164f89611Schristos 	}
208264f89611Schristos 	/* Noisefloor calibration is finished. */
208364f89611Schristos 	ar9003_read_noisefloor(sc, nf, nf_ext);
208464f89611Schristos 
208564f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
208664f89611Schristos 		cca_min = sc->sc_cca_min_2g;
208764f89611Schristos 		cca_max = sc->sc_cca_max_2g;
208864f89611Schristos 	}
208964f89611Schristos 	else {
209064f89611Schristos 		cca_min = sc->sc_cca_min_5g;
209164f89611Schristos 		cca_max = sc->sc_cca_max_5g;
209264f89611Schristos 	}
209364f89611Schristos 	/* Update noisefloor history. */
209464f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
209564f89611Schristos 		if (nf[i] < cca_min)
209664f89611Schristos 			nf[i] = cca_min;
209764f89611Schristos 		else if (nf[i] > cca_max)
209864f89611Schristos 			nf[i] = cca_max;
209964f89611Schristos 		if (nf_ext[i] < cca_min)
210064f89611Schristos 			nf_ext[i] = cca_min;
210164f89611Schristos 		else if (nf_ext[i] > cca_max)
210264f89611Schristos 			nf_ext[i] = cca_max;
210364f89611Schristos 
210464f89611Schristos 		sc->sc_nf_hist[sc->sc_nf_hist_cur].nf[i] = nf[i];
210564f89611Schristos 		sc->sc_nf_hist[sc->sc_nf_hist_cur].nf_ext[i] = nf_ext[i];
210664f89611Schristos 	}
210764f89611Schristos 	if (++sc->sc_nf_hist_cur >= ATHN_NF_CAL_HIST_MAX)
210864f89611Schristos 		sc->sc_nf_hist_cur = 0;
210964f89611Schristos }
211064f89611Schristos #endif /* notused */
211164f89611Schristos 
211264f89611Schristos #ifdef notused
211364f89611Schristos Static void
ar9003_bb_load_noisefloor(struct athn_softc * sc)211464f89611Schristos ar9003_bb_load_noisefloor(struct athn_softc *sc)
211564f89611Schristos {
211664f89611Schristos 	int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
211764f89611Schristos 	int i, ntries;
211864f89611Schristos 
211964f89611Schristos 	/* Write filtered noisefloor values. */
212064f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
212164f89611Schristos 		nf[i] = sc->sc_nf_priv[i] * 2;
212264f89611Schristos 		nf_ext[i] = sc->sc_nf_ext_priv[i] * 2;
212364f89611Schristos 	}
212464f89611Schristos 	ar9003_write_noisefloor(sc, nf, nf_ext);
212564f89611Schristos 
212664f89611Schristos 	/* Load filtered noisefloor values into baseband. */
212764f89611Schristos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
212864f89611Schristos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
212964f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
213064f89611Schristos 	/* Wait for load to complete. */
213164f89611Schristos 	for (ntries = 0; ntries < 1000; ntries++) {
213264f89611Schristos 		if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
213364f89611Schristos 			break;
213464f89611Schristos 		DELAY(10);
213564f89611Schristos 	}
213664f89611Schristos 	if (ntries == 1000) {
213764f89611Schristos 		DPRINTFN(DBG_RF, sc, "failed to load noisefloor values\n");
213864f89611Schristos 		return;
213964f89611Schristos 	}
214064f89611Schristos 
214164f89611Schristos 	/* Restore noisefloor values to initial (max) values. */
214264f89611Schristos 	for (i = 0; i < AR_MAX_CHAINS; i++)
214364f89611Schristos 		nf[i] = nf_ext[i] = -50 * 2;
214464f89611Schristos 	ar9003_write_noisefloor(sc, nf, nf_ext);
214564f89611Schristos }
214664f89611Schristos #endif /* notused */
214764f89611Schristos 
214864f89611Schristos #ifdef notused
214964f89611Schristos Static void
ar9300_noisefloor_calib(struct athn_softc * sc)215064f89611Schristos ar9300_noisefloor_calib(struct athn_softc *sc)
215164f89611Schristos {
215264f89611Schristos 
215364f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
215464f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
215564f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
215664f89611Schristos }
215764f89611Schristos #endif /* notused */
215864f89611Schristos 
215964f89611Schristos Static void
ar9003_do_noisefloor_calib(struct athn_softc * sc)216064f89611Schristos ar9003_do_noisefloor_calib(struct athn_softc *sc)
216164f89611Schristos {
216264f89611Schristos 
216364f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
216464f89611Schristos }
216564f89611Schristos 
216664f89611Schristos PUBLIC int
ar9003_init_calib(struct athn_softc * sc)216764f89611Schristos ar9003_init_calib(struct athn_softc *sc)
216864f89611Schristos {
216964f89611Schristos 	uint8_t txchainmask, rxchainmask;
217064f89611Schristos 	uint32_t reg;
217164f89611Schristos 	int ntries;
217264f89611Schristos 
217364f89611Schristos 	/* Save chains masks. */
217464f89611Schristos 	txchainmask = sc->sc_txchainmask;
217564f89611Schristos 	rxchainmask = sc->sc_rxchainmask;
217664f89611Schristos 	/* Configure hardware before calibration. */
217764f89611Schristos 	if (AR_READ(sc, AR_ENT_OTP) & AR_ENT_OTP_CHAIN2_DISABLE)
217864f89611Schristos 		txchainmask = rxchainmask = 0x3;
217964f89611Schristos 	else
218064f89611Schristos 		txchainmask = rxchainmask = 0x7;
218164f89611Schristos 	ar9003_init_chains(sc);
218264f89611Schristos 
218364f89611Schristos 	/* Perform Tx IQ calibration. */
218464f89611Schristos 	ar9003_calib_tx_iq(sc);
218564f89611Schristos 	/* Disable and re-enable the PHY chips. */
218664f89611Schristos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
218764f89611Schristos 	AR_WRITE_BARRIER(sc);
218864f89611Schristos 	DELAY(5);
218964f89611Schristos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
219064f89611Schristos 
219164f89611Schristos 	/* Calibrate the AGC. */
219264f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
219364f89611Schristos 	/* Poll for offset calibration completion. */
219464f89611Schristos 	for (ntries = 0; ntries < 10000; ntries++) {
219564f89611Schristos 		reg = AR_READ(sc, AR_PHY_AGC_CONTROL);
219664f89611Schristos 		if (!(reg & AR_PHY_AGC_CONTROL_CAL))
219764f89611Schristos 			break;
219864f89611Schristos 		DELAY(10);
219964f89611Schristos 	}
220064f89611Schristos 	if (ntries == 10000)
220164f89611Schristos 		return ETIMEDOUT;
220264f89611Schristos 
220364f89611Schristos 	/* Restore chains masks. */
220464f89611Schristos 	sc->sc_txchainmask = txchainmask;
220564f89611Schristos 	sc->sc_rxchainmask = rxchainmask;
220664f89611Schristos 	ar9003_init_chains(sc);
220764f89611Schristos 
220864f89611Schristos 	return 0;
220964f89611Schristos }
221064f89611Schristos 
221164f89611Schristos Static void
ar9003_do_calib(struct athn_softc * sc)221264f89611Schristos ar9003_do_calib(struct athn_softc *sc)
221364f89611Schristos {
221464f89611Schristos 	uint32_t reg;
221564f89611Schristos 
221664f89611Schristos 	if (sc->sc_cur_calib_mask & ATHN_CAL_IQ) {
221764f89611Schristos 		reg = AR_READ(sc, AR_PHY_TIMING4);
221864f89611Schristos 		reg = RW(reg, AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX, 10);
221964f89611Schristos 		AR_WRITE(sc, AR_PHY_TIMING4, reg);
222064f89611Schristos 		AR_WRITE(sc, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
222164f89611Schristos 		AR_SETBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
222264f89611Schristos 		AR_WRITE_BARRIER(sc);
222364f89611Schristos 	}
222464f89611Schristos 	else if (sc->sc_cur_calib_mask & ATHN_CAL_TEMP) {
222564f89611Schristos 		AR_SETBITS(sc, AR_PHY_65NM_CH0_THERM,
222664f89611Schristos 		    AR_PHY_65NM_CH0_THERM_LOCAL);
222764f89611Schristos 		AR_SETBITS(sc, AR_PHY_65NM_CH0_THERM,
222864f89611Schristos 		    AR_PHY_65NM_CH0_THERM_START);
222964f89611Schristos 		AR_WRITE_BARRIER(sc);
223064f89611Schristos 	}
223164f89611Schristos }
223264f89611Schristos 
223364f89611Schristos Static void
ar9003_next_calib(struct athn_softc * sc)223464f89611Schristos ar9003_next_calib(struct athn_softc *sc)
223564f89611Schristos {
223664f89611Schristos 	/* Check if we have any calibration in progress. */
223764f89611Schristos 	if (sc->sc_cur_calib_mask != 0) {
223864f89611Schristos 		if (!(AR_READ(sc, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
223964f89611Schristos 			/* Calibration completed for current sample. */
224064f89611Schristos 			ar9003_calib_iq(sc);
224164f89611Schristos 		}
224264f89611Schristos 	}
224364f89611Schristos }
224464f89611Schristos 
224564f89611Schristos Static void
ar9003_calib_iq(struct athn_softc * sc)224664f89611Schristos ar9003_calib_iq(struct athn_softc *sc)
224764f89611Schristos {
224864f89611Schristos 	struct athn_iq_cal *cal;
224964f89611Schristos 	uint32_t reg, i_coff_denom, q_coff_denom;
225064f89611Schristos 	int32_t i_coff, q_coff;
225164f89611Schristos 	int i, iq_corr_neg;
225264f89611Schristos 
225364f89611Schristos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
225464f89611Schristos 		cal = &sc->sc_calib.iq[i];
225564f89611Schristos 
225664f89611Schristos 		/* Read IQ calibration measures (clear on read). */
225764f89611Schristos 		cal->pwr_meas_i = AR_READ(sc, AR_PHY_IQ_ADC_MEAS_0_B(i));
225864f89611Schristos 		cal->pwr_meas_q = AR_READ(sc, AR_PHY_IQ_ADC_MEAS_1_B(i));
225964f89611Schristos 		cal->iq_corr_meas =
226064f89611Schristos 		    (int32_t)AR_READ(sc, AR_PHY_IQ_ADC_MEAS_2_B(i));
226164f89611Schristos 	}
226264f89611Schristos 
226364f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
226464f89611Schristos 		cal = &sc->sc_calib.iq[i];
226564f89611Schristos 
226664f89611Schristos 		if (cal->pwr_meas_q == 0)
226764f89611Schristos 			continue;
226864f89611Schristos 
2269bdb256cbSchristos 		if ((iq_corr_neg = cal->iq_corr_meas) < 0)
227064f89611Schristos 			cal->iq_corr_meas = -cal->iq_corr_meas;
227164f89611Schristos 
227264f89611Schristos 		i_coff_denom =
227364f89611Schristos 		    (cal->pwr_meas_i / 2 + cal->pwr_meas_q / 2) / 256;
227464f89611Schristos 		q_coff_denom = cal->pwr_meas_q / 64;
227564f89611Schristos 
227664f89611Schristos 		if (i_coff_denom == 0 || q_coff_denom == 0)
227764f89611Schristos 			continue;	/* Prevents division by zero. */
227864f89611Schristos 
227964f89611Schristos 		i_coff = cal->iq_corr_meas / i_coff_denom;
228064f89611Schristos 		q_coff = (cal->pwr_meas_i / q_coff_denom) - 64;
228164f89611Schristos 
228264f89611Schristos 		if (i_coff > 63)
228364f89611Schristos 			i_coff = 63;
228464f89611Schristos 		else if (i_coff < -63)
228564f89611Schristos 			i_coff = -63;
228664f89611Schristos 		/* Negate i_coff if iq_corr_meas is positive. */
228764f89611Schristos 		if (!iq_corr_neg)
228864f89611Schristos 			i_coff = -i_coff;
228964f89611Schristos 		if (q_coff > 63)
229064f89611Schristos 			q_coff = 63;
229164f89611Schristos 		else if (q_coff < -63)
229264f89611Schristos 			q_coff = -63;
229364f89611Schristos 
229464f89611Schristos 		DPRINTFN(DBG_RF, sc, "IQ calibration for chain %d\n", i);
229564f89611Schristos 		reg = AR_READ(sc, AR_PHY_RX_IQCAL_CORR_B(i));
229664f89611Schristos 		reg = RW(reg, AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF, i_coff);
229764f89611Schristos 		reg = RW(reg, AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF, q_coff);
229864f89611Schristos 		AR_WRITE(sc, AR_PHY_RX_IQCAL_CORR_B(i), reg);
229964f89611Schristos 	}
230064f89611Schristos 
230164f89611Schristos 	/* Apply new settings. */
230264f89611Schristos 	AR_SETBITS(sc, AR_PHY_RX_IQCAL_CORR_B(0),
230364f89611Schristos 	    AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
230464f89611Schristos 	AR_WRITE_BARRIER(sc);
230564f89611Schristos 
230664f89611Schristos 	/* IQ calibration done. */
230764f89611Schristos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_IQ;
230864f89611Schristos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
230964f89611Schristos }
231064f89611Schristos 
231164f89611Schristos #define DELPT	32
231264f89611Schristos Static int
ar9003_get_iq_corr(struct athn_softc * sc,int32_t res[6],int32_t coeff[2])231364f89611Schristos ar9003_get_iq_corr(struct athn_softc *sc, int32_t res[6], int32_t coeff[2])
231464f89611Schristos {
231564f89611Schristos /* Sign-extends 12-bit value (assumes upper bits are zeroes). */
231664f89611Schristos #define SIGN_EXT(v)	(((v) ^ 0x800) - 0x800)
231764f89611Schristos #define SCALE		(1 << 15)
231864f89611Schristos #define SHIFT		(1 <<  8)
231964f89611Schristos 	struct {
232064f89611Schristos 		int32_t	m, p, c;
232164f89611Schristos 	} val[2][2];
232264f89611Schristos 	int32_t mag[2][2], phs[2][2], cos[2], sin[2];
232364f89611Schristos 	int32_t div, f1, f2, f3, m, p, c;
232464f89611Schristos 	int32_t txmag, txphs, rxmag, rxphs;
232564f89611Schristos 	int32_t q_coff, i_coff;
232664f89611Schristos 	int i, j;
232764f89611Schristos 
232864f89611Schristos 	/* Extract our twelve signed 12-bit values from res[] array. */
232964f89611Schristos 	val[0][0].m = res[0] & 0xfff;
233064f89611Schristos 	val[0][0].p = (res[0] >> 12) & 0xfff;
233164f89611Schristos 	val[0][0].c = ((res[0] >> 24) & 0xff) | (res[1] & 0xf) << 8;
233264f89611Schristos 
233364f89611Schristos 	val[0][1].m = (res[1] >> 4) & 0xfff;
233464f89611Schristos 	val[0][1].p = res[2] & 0xfff;
233564f89611Schristos 	val[0][1].c = (res[2] >> 12) & 0xfff;
233664f89611Schristos 
233764f89611Schristos 	val[1][0].m = ((res[2] >> 24) & 0xff) | (res[3] & 0xf) << 8;
233864f89611Schristos 	val[1][0].p = (res[3] >> 4) & 0xfff;
233964f89611Schristos 	val[1][0].c = res[4] & 0xfff;
234064f89611Schristos 
234164f89611Schristos 	val[1][1].m = (res[4] >> 12) & 0xfff;
234264f89611Schristos 	val[1][1].p = ((res[4] >> 24) & 0xff) | (res[5] & 0xf) << 8;
234364f89611Schristos 	val[1][1].c = (res[5] >> 4) & 0xfff;
234464f89611Schristos 
234564f89611Schristos 	for (i = 0; i < 2; i++) {
234664f89611Schristos 		int32_t ymin, ymax;
234764f89611Schristos 		for (j = 0; j < 2; j++) {
234864f89611Schristos 			m = SIGN_EXT(val[i][j].m);
234964f89611Schristos 			p = SIGN_EXT(val[i][j].p);
235064f89611Schristos 			c = SIGN_EXT(val[i][j].c);
235164f89611Schristos 
235264f89611Schristos 			if (p == 0)
235364f89611Schristos 				return 1;	/* Prevent division by 0. */
235464f89611Schristos 
235564f89611Schristos 			mag[i][j] = (m * SCALE) / p;
235664f89611Schristos 			phs[i][j] = (c * SCALE) / p;
235764f89611Schristos 		}
235864f89611Schristos 		sin[i] = ((mag[i][0] - mag[i][1]) * SHIFT) / DELPT;
235964f89611Schristos 		cos[i] = ((phs[i][0] - phs[i][1]) * SHIFT) / DELPT;
236064f89611Schristos 		/* Find magnitude by approximation. */
236164f89611Schristos 		ymin = MIN(abs(sin[i]), abs(cos[i]));
236264f89611Schristos 		ymax = MAX(abs(sin[i]), abs(cos[i]));
236364f89611Schristos 		div = ymax - (ymax / 32) + (ymin / 8) + (ymin / 4);
236464f89611Schristos 		if (div == 0)
236564f89611Schristos 			return 1;	/* Prevent division by 0. */
236664f89611Schristos 		/* Normalize sin and cos by magnitude. */
236764f89611Schristos 		sin[i] = (sin[i] * SCALE) / div;
236864f89611Schristos 		cos[i] = (cos[i] * SCALE) / div;
236964f89611Schristos 	}
237064f89611Schristos 
237164f89611Schristos 	/* Compute IQ mismatch (solve 4x4 linear equation). */
237264f89611Schristos 	f1 = cos[0] - cos[1];
237364f89611Schristos 	f3 = sin[0] - sin[1];
237464f89611Schristos 	f2 = (f1 * f1 + f3 * f3) / SCALE;
237564f89611Schristos 	if (f2 == 0)
237664f89611Schristos 		return 1;	/* Prevent division by 0. */
237764f89611Schristos 
237864f89611Schristos 	/* Compute Tx magnitude mismatch. */
237964f89611Schristos 	txmag = (f1 * ( mag[0][0] - mag[1][0]) +
238064f89611Schristos 		 f3 * ( phs[0][0] - phs[1][0])) / f2;
238164f89611Schristos 	/* Compute Tx phase mismatch. */
238264f89611Schristos 	txphs = (f3 * (-mag[0][0] + mag[1][0]) +
238364f89611Schristos 		 f1 * ( phs[0][0] - phs[1][0])) / f2;
238464f89611Schristos 
238564f89611Schristos 	if (txmag == SCALE)
238664f89611Schristos 		return 1;	/* Prevent division by 0. */
238764f89611Schristos 
238864f89611Schristos 	/* Compute Rx magnitude mismatch. */
238964f89611Schristos 	rxmag = mag[0][0] - (cos[0] * txmag + sin[0] * txphs) / SCALE;
239064f89611Schristos 	/* Compute Rx phase mismatch. */
239164f89611Schristos 	rxphs = phs[0][0] + (sin[0] * txmag - cos[0] * txphs) / SCALE;
239264f89611Schristos 
239364f89611Schristos 	if (-rxmag == SCALE)
239464f89611Schristos 		return 1;	/* Prevent division by 0. */
239564f89611Schristos 
239664f89611Schristos 	txmag = (txmag * SCALE) / (SCALE - txmag);
239764f89611Schristos 	txphs = -txphs;
239864f89611Schristos 
239964f89611Schristos 	q_coff = (txmag * 128) / SCALE;
240064f89611Schristos 	if (q_coff < -63)
240164f89611Schristos 		q_coff = -63;
240264f89611Schristos 	else if (q_coff > 63)
240364f89611Schristos 		q_coff = 63;
240464f89611Schristos 	i_coff = (txphs * 256) / SCALE;
240564f89611Schristos 	if (i_coff < -63)
240664f89611Schristos 		i_coff = -63;
240764f89611Schristos 	else if (i_coff > 63)
240864f89611Schristos 		i_coff = 63;
240964f89611Schristos 	coeff[0] = q_coff * 128 + i_coff;
241064f89611Schristos 
241164f89611Schristos 	rxmag = (-rxmag * SCALE) / (SCALE + rxmag);
241264f89611Schristos 	rxphs = -rxphs;
241364f89611Schristos 
241464f89611Schristos 	q_coff = (rxmag * 128) / SCALE;
241564f89611Schristos 	if (q_coff < -63)
241664f89611Schristos 		q_coff = -63;
241764f89611Schristos 	else if (q_coff > 63)
241864f89611Schristos 		q_coff = 63;
241964f89611Schristos 	i_coff = (rxphs * 256) / SCALE;
242064f89611Schristos 	if (i_coff < -63)
242164f89611Schristos 		i_coff = -63;
242264f89611Schristos 	else if (i_coff > 63)
242364f89611Schristos 		i_coff = 63;
242464f89611Schristos 	coeff[1] = q_coff * 128 + i_coff;
242564f89611Schristos 
242664f89611Schristos 	return 0;
242764f89611Schristos #undef SHIFT
242864f89611Schristos #undef SCALE
242964f89611Schristos #undef SIGN_EXT
243064f89611Schristos }
243164f89611Schristos 
243264f89611Schristos Static int
ar9003_calib_tx_iq(struct athn_softc * sc)243364f89611Schristos ar9003_calib_tx_iq(struct athn_softc *sc)
243464f89611Schristos {
243564f89611Schristos 	uint32_t reg;
243664f89611Schristos 	int32_t res[6], coeff[2];
243764f89611Schristos 	int i, j, ntries;
243864f89611Schristos 
243964f89611Schristos 	reg = AR_READ(sc, AR_PHY_TX_IQCAL_CONTROL_1);
244064f89611Schristos 	reg = RW(reg, AR_PHY_TX_IQCAQL_CONTROL_1_IQCORR_I_Q_COFF_DELPT, DELPT);
244164f89611Schristos 	AR_WRITE(sc, AR_PHY_TX_IQCAL_CONTROL_1, reg);
244264f89611Schristos 
244364f89611Schristos 	/* Start Tx IQ calibration. */
244464f89611Schristos 	AR_SETBITS(sc, AR_PHY_TX_IQCAL_START, AR_PHY_TX_IQCAL_START_DO_CAL);
244564f89611Schristos 	/* Wait for completion. */
244664f89611Schristos 	for (ntries = 0; ntries < 10000; ntries++) {
244764f89611Schristos 		reg = AR_READ(sc, AR_PHY_TX_IQCAL_START);
244864f89611Schristos 		if (!(reg & AR_PHY_TX_IQCAL_START_DO_CAL))
244964f89611Schristos 			break;
245064f89611Schristos 		DELAY(10);
245164f89611Schristos 	}
245264f89611Schristos 	if (ntries == 10000)
245364f89611Schristos 		return ETIMEDOUT;
245464f89611Schristos 
245564f89611Schristos 	for (i = 0; i < sc->sc_ntxchains; i++) {
245664f89611Schristos 		/* Read Tx IQ calibration status for this chain. */
245764f89611Schristos 		reg = AR_READ(sc, AR_PHY_TX_IQCAL_STATUS_B(i));
245864f89611Schristos 		if (reg & AR_PHY_TX_IQCAL_STATUS_FAILED)
245964f89611Schristos 			return EIO;
246064f89611Schristos 		/*
246164f89611Schristos 		 * Read Tx IQ calibration results for this chain.
246264f89611Schristos 		 * This consists in twelve signed 12-bit values.
246364f89611Schristos 		 */
246464f89611Schristos 		for (j = 0; j < 3; j++) {
246564f89611Schristos 			AR_CLRBITS(sc, AR_PHY_CHAN_INFO_MEMORY,
246664f89611Schristos 			    AR_PHY_CHAN_INFO_TAB_S2_READ);
246764f89611Schristos 			reg = AR_READ(sc, AR_PHY_CHAN_INFO_TAB(i, j));
246864f89611Schristos 			res[j * 2 + 0] = reg;
246964f89611Schristos 
247064f89611Schristos 			AR_SETBITS(sc, AR_PHY_CHAN_INFO_MEMORY,
247164f89611Schristos 			    AR_PHY_CHAN_INFO_TAB_S2_READ);
247264f89611Schristos 			reg = AR_READ(sc, AR_PHY_CHAN_INFO_TAB(i, j));
247364f89611Schristos 			res[j * 2 + 1] = reg & 0xffff;
247464f89611Schristos 		}
247564f89611Schristos 
247664f89611Schristos 		/* Compute Tx IQ correction. */
247764f89611Schristos 		if (ar9003_get_iq_corr(sc, res, coeff) != 0)
247864f89611Schristos 			return EIO;
247964f89611Schristos 
248064f89611Schristos 		/* Write Tx IQ correction coefficients. */
248164f89611Schristos 		reg = AR_READ(sc, AR_PHY_TX_IQCAL_CORR_COEFF_01_B(i));
248264f89611Schristos 		reg = RW(reg, AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
248364f89611Schristos 		    coeff[0]);
248464f89611Schristos 		AR_WRITE(sc, AR_PHY_TX_IQCAL_CORR_COEFF_01_B(i), reg);
248564f89611Schristos 
248664f89611Schristos 		reg = AR_READ(sc, AR_PHY_RX_IQCAL_CORR_B(i));
248764f89611Schristos 		reg = RW(reg, AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_Q_COFF,
248864f89611Schristos 		    coeff[1] >> 7);
248964f89611Schristos 		reg = RW(reg, AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_I_COFF,
249064f89611Schristos 		    coeff[1]);
249164f89611Schristos 		AR_WRITE(sc, AR_PHY_RX_IQCAL_CORR_B(i), reg);
249264f89611Schristos 		AR_WRITE_BARRIER(sc);
249364f89611Schristos 	}
249464f89611Schristos 
249564f89611Schristos 	/* Enable Tx IQ correction. */
249664f89611Schristos 	AR_SETBITS(sc, AR_PHY_TX_IQCAL_CONTROL_3,
249764f89611Schristos 	    AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN);
249864f89611Schristos 	AR_SETBITS(sc, AR_PHY_RX_IQCAL_CORR_B(0),
249964f89611Schristos 	    AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN);
250064f89611Schristos 	AR_WRITE_BARRIER(sc);
250164f89611Schristos 	return 0;
250264f89611Schristos }
250364f89611Schristos #undef DELPT
250464f89611Schristos 
250564f89611Schristos /*-
250664f89611Schristos  * The power amplifier predistortion state machine works as follows:
250764f89611Schristos  * 1) Disable digital predistorters for all Tx chains
250864f89611Schristos  * 2) Repeat steps 3~7 for all Tx chains
250964f89611Schristos  * 3)   Force Tx gain to that of training signal
251064f89611Schristos  * 4)   Send training signal (asynchronous)
251164f89611Schristos  * 5)   Wait for training signal to complete (asynchronous)
251264f89611Schristos  * 6)   Read PA measurements (input power, output power, output phase)
251364f89611Schristos  * 7)   Compute the predistortion function that linearizes PA output
251464f89611Schristos  * 8) Write predistortion functions to hardware tables for all Tx chains
251564f89611Schristos  * 9) Enable digital predistorters for all Tx chains
251664f89611Schristos  */
251764f89611Schristos #ifdef notused
251864f89611Schristos Static void
ar9003_paprd_calib(struct athn_softc * sc,struct ieee80211_channel * c)251964f89611Schristos ar9003_paprd_calib(struct athn_softc *sc, struct ieee80211_channel *c)
252064f89611Schristos {
252164f89611Schristos 	static const int scaling[] = {
252264f89611Schristos 		261376, 248079, 233759, 220464,
252364f89611Schristos 		208194, 196949, 185706, 175487
252464f89611Schristos 	};
252564f89611Schristos 	struct athn_ops *ops = &sc->sc_ops;
252664f89611Schristos 	uint32_t reg, ht20mask, ht40mask;
252764f89611Schristos 	int i;
252864f89611Schristos 
252964f89611Schristos 	/* Read PA predistortion masks from ROM. */
253064f89611Schristos 	ops->get_paprd_masks(sc, c, &ht20mask, &ht40mask);
253164f89611Schristos 
253264f89611Schristos 	/* AM-to-AM: amplifier's amplitude characteristic. */
253364f89611Schristos 	reg = AR_READ(sc, AR_PHY_PAPRD_AM2AM);
253464f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_AM2AM_MASK, ht20mask);
253564f89611Schristos 	AR_WRITE(sc, AR_PHY_PAPRD_AM2AM, reg);
253664f89611Schristos 
253764f89611Schristos 	/* AM-to-PM: amplifier's phase transfer characteristic. */
253864f89611Schristos 	reg = AR_READ(sc, AR_PHY_PAPRD_AM2PM);
253964f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_AM2PM_MASK, ht20mask);
254064f89611Schristos 	AR_WRITE(sc, AR_PHY_PAPRD_AM2PM, reg);
254164f89611Schristos 
254264f89611Schristos 	reg = AR_READ(sc, AR_PHY_PAPRD_HT40);
254364f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_HT40_MASK, ht40mask);
254464f89611Schristos 	AR_WRITE(sc, AR_PHY_PAPRD_HT40, reg);
254564f89611Schristos 
254664f89611Schristos 	for (i = 0; i < AR9003_MAX_CHAINS; i++) {
254764f89611Schristos 		AR_SETBITS(sc, AR_PHY_PAPRD_CTRL0_B(i),
254864f89611Schristos 		    AR_PHY_PAPRD_CTRL0_USE_SINGLE_TABLE);
254964f89611Schristos 
255064f89611Schristos 		reg = AR_READ(sc, AR_PHY_PAPRD_CTRL1_B(i));
255164f89611Schristos 		reg = RW(reg, AR_PHY_PAPRD_CTRL1_PA_GAIN_SCALE_FACT, 181);
255264f89611Schristos 		reg = RW(reg, AR_PHY_PAPRD_CTRL1_MAG_SCALE_FACT, 361);
255364f89611Schristos 		reg &= ~AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA;
255464f89611Schristos 		reg |= AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2AM_ENA;
255564f89611Schristos 		reg |= AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2PM_ENA;
255664f89611Schristos 		AR_WRITE(sc, AR_PHY_PAPRD_CTRL1_B(i), reg);
255764f89611Schristos 
255864f89611Schristos 		reg = AR_READ(sc, AR_PHY_PAPRD_CTRL0_B(i));
255964f89611Schristos 		reg = RW(reg, AR_PHY_PAPRD_CTRL0_PAPRD_MAG_THRSH, 3);
256064f89611Schristos 		AR_WRITE(sc, AR_PHY_PAPRD_CTRL0_B(i), reg);
256164f89611Schristos 	}
256264f89611Schristos 
256364f89611Schristos 	/* Disable all digital predistorters during calibration. */
256464f89611Schristos 	for (i = 0; i < AR9003_MAX_CHAINS; i++) {
256564f89611Schristos 		AR_CLRBITS(sc, AR_PHY_PAPRD_CTRL0_B(i),
256664f89611Schristos 		    AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE);
256764f89611Schristos 	}
256864f89611Schristos 	AR_WRITE_BARRIER(sc);
256964f89611Schristos 
257064f89611Schristos 	/*
257164f89611Schristos 	 * Configure training signal.
257264f89611Schristos 	 */
257364f89611Schristos 	reg = AR_READ(sc, AR_PHY_PAPRD_TRAINER_CNTL1);
257464f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL1_AGC2_SETTLING, 28);
257564f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL1_LB_SKIP, 0x30);
257664f89611Schristos 	reg &= ~AR_PHY_PAPRD_TRAINER_CNTL1_RX_BB_GAIN_FORCE;
257764f89611Schristos 	reg &= ~AR_PHY_PAPRD_TRAINER_CNTL1_IQCORR_ENABLE;
257864f89611Schristos 	reg |= AR_PHY_PAPRD_TRAINER_CNTL1_LB_ENABLE;
257964f89611Schristos 	reg |= AR_PHY_PAPRD_TRAINER_CNTL1_TX_GAIN_FORCE;
258064f89611Schristos 	reg |= AR_PHY_PAPRD_TRAINER_CNTL1_TRAIN_ENABLE;
258164f89611Schristos 	AR_WRITE(sc, AR_PHY_PAPRD_TRAINER_CNTL1, reg);
258264f89611Schristos 
258364f89611Schristos 	AR_WRITE(sc, AR_PHY_PAPRD_TRAINER_CNTL2, 147);
258464f89611Schristos 
258564f89611Schristos 	reg = AR_READ(sc, AR_PHY_PAPRD_TRAINER_CNTL3);
258664f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_FINE_CORR_LEN, 4);
258764f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_COARSE_CORR_LEN, 4);
258864f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_NUM_CORR_STAGES, 7);
258964f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_MIN_LOOPBACK_DEL, 1);
259064f89611Schristos 	if (AR_SREV_9485(sc))
259164f89611Schristos 		reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_QUICK_DROP, -3);
259264f89611Schristos 	else
259364f89611Schristos 		reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_QUICK_DROP, -6);
259464f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_ADC_DESIRED_SIZE, -15);
259564f89611Schristos 	reg |= AR_PHY_PAPRD_TRAINER_CNTL3_BBTXMIX_DISABLE;
259664f89611Schristos 	AR_WRITE(sc, AR_PHY_PAPRD_TRAINER_CNTL3, reg);
259764f89611Schristos 
259864f89611Schristos 	reg = AR_READ(sc, AR_PHY_PAPRD_TRAINER_CNTL4);
259964f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL4_SAFETY_DELTA, 0);
260064f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL4_MIN_CORR, 400);
260164f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL4_NUM_TRAIN_SAMPLES, 100);
260264f89611Schristos 	AR_WRITE(sc, AR_PHY_PAPRD_TRAINER_CNTL4, reg);
260364f89611Schristos 
260464f89611Schristos 	for (i = 0; i < __arraycount(scaling); i++) {
260564f89611Schristos 		reg = AR_READ(sc, AR_PHY_PAPRD_PRE_POST_SCALE_B0(i));
260664f89611Schristos 		reg = RW(reg, AR_PHY_PAPRD_PRE_POST_SCALING, scaling[i]);
260764f89611Schristos 		AR_WRITE(sc, AR_PHY_PAPRD_PRE_POST_SCALE_B0(i), reg);
260864f89611Schristos 	}
260964f89611Schristos 
261064f89611Schristos 	/* Save Tx gain table. */
261164f89611Schristos 	for (i = 0; i < AR9003_TX_GAIN_TABLE_SIZE; i++)
261264f89611Schristos 		sc->sc_txgain[i] = AR_READ(sc, AR_PHY_TXGAIN_TABLE(i));
261364f89611Schristos 
261464f89611Schristos 	/* Set Tx power of training signal (use setting for MCS0). */
261564f89611Schristos 	sc->sc_trainpow = MS(AR_READ(sc, AR_PHY_PWRTX_RATE5),
261664f89611Schristos 	    AR_PHY_PWRTX_RATE5_POWERTXHT20_0) - 4;
261764f89611Schristos 
261864f89611Schristos 	/*
261964f89611Schristos 	 * Start PA predistortion calibration state machine.
262064f89611Schristos 	 */
262164f89611Schristos 	/* Find first available Tx chain. */
262264f89611Schristos 	sc->sc_paprd_curchain = 0;
262364f89611Schristos 	while (!(sc->sc_txchainmask & (1 << sc->sc_paprd_curchain)))
262464f89611Schristos 		sc->sc_paprd_curchain++;
262564f89611Schristos 
262664f89611Schristos 	/* Make sure training done bit is clear. */
262764f89611Schristos 	AR_CLRBITS(sc, AR_PHY_PAPRD_TRAINER_STAT1,
262864f89611Schristos 	    AR_PHY_PAPRD_TRAINER_STAT1_TRAIN_DONE);
262964f89611Schristos 	AR_WRITE_BARRIER(sc);
263064f89611Schristos 
263164f89611Schristos 	/* Transmit training signal. */
263264f89611Schristos 	ar9003_paprd_tx_tone(sc);
263364f89611Schristos }
263464f89611Schristos #endif /* notused */
263564f89611Schristos 
263664f89611Schristos Static int
ar9003_get_desired_txgain(struct athn_softc * sc,int chain,int pow)263764f89611Schristos ar9003_get_desired_txgain(struct athn_softc *sc, int chain, int pow)
263864f89611Schristos {
263964f89611Schristos 	int32_t scale, atemp, avolt, tempcal, voltcal, temp, volt;
264064f89611Schristos 	int32_t tempcorr, voltcorr;
264164f89611Schristos 	uint32_t reg;
264264f89611Schristos 	int8_t delta;
264364f89611Schristos 
264464f89611Schristos 	scale = MS(AR_READ(sc, AR_PHY_TPC_12),
264564f89611Schristos 	    AR_PHY_TPC_12_DESIRED_SCALE_HT40_5);
264664f89611Schristos 
264764f89611Schristos 	reg = AR_READ(sc, AR_PHY_TPC_19);
264864f89611Schristos 	atemp = MS(reg, AR_PHY_TPC_19_ALPHA_THERM);
264964f89611Schristos 	avolt = MS(reg, AR_PHY_TPC_19_ALPHA_VOLT);
265064f89611Schristos 
265164f89611Schristos 	reg = AR_READ(sc, AR_PHY_TPC_18);
265264f89611Schristos 	tempcal = MS(reg, AR_PHY_TPC_18_THERM_CAL);
265364f89611Schristos 	voltcal = MS(reg, AR_PHY_TPC_18_VOLT_CAL);
265464f89611Schristos 
265564f89611Schristos 	reg = AR_READ(sc, AR_PHY_BB_THERM_ADC_4);
265664f89611Schristos 	temp = MS(reg, AR_PHY_BB_THERM_ADC_4_LATEST_THERM);
265764f89611Schristos 	volt = MS(reg, AR_PHY_BB_THERM_ADC_4_LATEST_VOLT);
265864f89611Schristos 
265964f89611Schristos 	delta = (int8_t)MS(AR_READ(sc, AR_PHY_TPC_11_B(chain)),
266064f89611Schristos 	    AR_PHY_TPC_11_OLPC_GAIN_DELTA);
266164f89611Schristos 
266264f89611Schristos 	/* Compute temperature and voltage correction. */
266364f89611Schristos 	tempcorr = (atemp * (temp - tempcal) + 128) / 256;
266464f89611Schristos 	voltcorr = (avolt * (volt - voltcal) + 64) / 128;
266564f89611Schristos 
266664f89611Schristos 	/* Compute desired Tx gain. */
266764f89611Schristos 	return pow - delta - tempcorr - voltcorr + scale;
266864f89611Schristos }
266964f89611Schristos 
267064f89611Schristos Static void
ar9003_force_txgain(struct athn_softc * sc,uint32_t txgain)267164f89611Schristos ar9003_force_txgain(struct athn_softc *sc, uint32_t txgain)
267264f89611Schristos {
267364f89611Schristos 	uint32_t reg;
267464f89611Schristos 
267564f89611Schristos 	reg = AR_READ(sc, AR_PHY_TX_FORCED_GAIN);
267664f89611Schristos 	reg = RW(reg, AR_PHY_TX_FORCED_GAIN_TXBB1DBGAIN,
267764f89611Schristos 	    MS(txgain, AR_PHY_TXGAIN_TXBB1DBGAIN));
267864f89611Schristos 	reg = RW(reg, AR_PHY_TX_FORCED_GAIN_TXBB6DBGAIN,
267964f89611Schristos 	    MS(txgain, AR_PHY_TXGAIN_TXBB6DBGAIN));
268064f89611Schristos 	reg = RW(reg, AR_PHY_TX_FORCED_GAIN_TXMXRGAIN,
268164f89611Schristos 	    MS(txgain, AR_PHY_TXGAIN_TXMXRGAIN));
268264f89611Schristos 	reg = RW(reg, AR_PHY_TX_FORCED_GAIN_PADRVGNA,
268364f89611Schristos 	    MS(txgain, AR_PHY_TXGAIN_PADRVGNA));
268464f89611Schristos 	reg = RW(reg, AR_PHY_TX_FORCED_GAIN_PADRVGNB,
268564f89611Schristos 	    MS(txgain, AR_PHY_TXGAIN_PADRVGNB));
268664f89611Schristos 	reg = RW(reg, AR_PHY_TX_FORCED_GAIN_PADRVGNC,
268764f89611Schristos 	    MS(txgain, AR_PHY_TXGAIN_PADRVGNC));
268864f89611Schristos 	reg = RW(reg, AR_PHY_TX_FORCED_GAIN_PADRVGND,
268964f89611Schristos 	    MS(txgain, AR_PHY_TXGAIN_PADRVGND));
269064f89611Schristos 	reg &= ~AR_PHY_TX_FORCED_GAIN_ENABLE_PAL;
269164f89611Schristos 	reg &= ~AR_PHY_TX_FORCED_GAIN_FORCE_TX_GAIN;
269264f89611Schristos 	AR_WRITE(sc, AR_PHY_TX_FORCED_GAIN, reg);
269364f89611Schristos 
269464f89611Schristos 	reg = AR_READ(sc, AR_PHY_TPC_1);
269564f89611Schristos 	reg = RW(reg, AR_PHY_TPC_1_FORCED_DAC_GAIN, 0);
269664f89611Schristos 	reg &= ~AR_PHY_TPC_1_FORCE_DAC_GAIN;
269764f89611Schristos 	AR_WRITE(sc, AR_PHY_TPC_1, reg);
269864f89611Schristos 	AR_WRITE_BARRIER(sc);
269964f89611Schristos }
270064f89611Schristos 
270164f89611Schristos Static void
ar9003_set_training_gain(struct athn_softc * sc,int chain)270264f89611Schristos ar9003_set_training_gain(struct athn_softc *sc, int chain)
270364f89611Schristos {
270464f89611Schristos 	size_t i;
270564f89611Schristos 	int gain;
270664f89611Schristos 
270764f89611Schristos 	/*
270864f89611Schristos 	 * Get desired gain for training signal power (take into account
270964f89611Schristos 	 * current temperature/voltage).
271064f89611Schristos 	 */
271164f89611Schristos 	gain = ar9003_get_desired_txgain(sc, chain, sc->sc_trainpow);
271264f89611Schristos 	/* Find entry in table. */
271364f89611Schristos 	for (i = 0; i < AR9003_TX_GAIN_TABLE_SIZE - 1; i++)
271464f89611Schristos 		if ((int)MS(sc->sc_txgain[i], AR_PHY_TXGAIN_INDEX) >= gain)
271564f89611Schristos 			break;
271664f89611Schristos 	ar9003_force_txgain(sc, sc->sc_txgain[i]);
271764f89611Schristos }
271864f89611Schristos 
271964f89611Schristos Static int
ar9003_paprd_tx_tone(struct athn_softc * sc)272064f89611Schristos ar9003_paprd_tx_tone(struct athn_softc *sc)
272164f89611Schristos {
272264f89611Schristos #define TONE_LEN	1800
272364f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
272464f89611Schristos 	struct ieee80211_frame *wh;
272564f89611Schristos 	struct ieee80211_node *ni;
272664f89611Schristos 	struct mbuf *m;
272764f89611Schristos 	int error;
272864f89611Schristos 
272964f89611Schristos 	/* Build a Null (no data) frame of TONE_LEN bytes. */
273064f89611Schristos 	m = MCLGETI(NULL, M_DONTWAIT, NULL, TONE_LEN);
273164f89611Schristos 	if (m == NULL)
273264f89611Schristos 		return ENOBUFS;
273364f89611Schristos 	memset(mtod(m, void *), 0, TONE_LEN);
273464f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
273564f89611Schristos 	wh->i_fc[0] = IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA;
273664f89611Schristos 	wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
273764f89611Schristos 	*(uint16_t *)wh->i_dur = htole16(10);	/* XXX */
273864f89611Schristos 	IEEE80211_ADDR_COPY(wh->i_addr1, ic->ic_myaddr);
273964f89611Schristos 	IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
274064f89611Schristos 	IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_myaddr);
274164f89611Schristos 	m->m_pkthdr.len = m->m_len = TONE_LEN;
274264f89611Schristos 
274364f89611Schristos 	/* Set gain of training signal. */
274464f89611Schristos 	ar9003_set_training_gain(sc, sc->sc_paprd_curchain);
274564f89611Schristos 
274664f89611Schristos 	/* Transmit training signal. */
274764f89611Schristos 	ni = ieee80211_ref_node(ic->ic_bss);
274864f89611Schristos 	if ((error = ar9003_tx(sc, m, ni, ATHN_TXFLAG_PAPRD)) != 0)
274964f89611Schristos 		ieee80211_free_node(ni);
275064f89611Schristos 	return error;
275164f89611Schristos #undef TONE_LEN
275264f89611Schristos }
275364f89611Schristos 
275464f89611Schristos static __inline int
get_scale(int val)275564f89611Schristos get_scale(int val)
275664f89611Schristos {
275764f89611Schristos 	int log = 0;
275864f89611Schristos 
275964f89611Schristos 	/* Find the log base 2 (position of highest bit set). */
276064f89611Schristos 	while (val >>= 1)
276164f89611Schristos 		log++;
276264f89611Schristos 
276364f89611Schristos 	return (log > 10) ? log - 10 : 0;
276464f89611Schristos }
276564f89611Schristos 
276664f89611Schristos /*
276764f89611Schristos  * Compute predistortion function to linearize power amplifier output based
276864f89611Schristos  * on feedback from training signal.
276964f89611Schristos  */
277064f89611Schristos Static int
ar9003_compute_predistortion(struct athn_softc * sc,const uint32_t * lo,const uint32_t * hi)277164f89611Schristos ar9003_compute_predistortion(struct athn_softc *sc, const uint32_t *lo,
277264f89611Schristos     const uint32_t *hi)
277364f89611Schristos {
277464f89611Schristos #define NBINS	23
277564f89611Schristos 	int chain = sc->sc_paprd_curchain;
277664f89611Schristos 	int x[NBINS + 1], y[NBINS + 1], t[NBINS + 1];
277764f89611Schristos 	int b1[NBINS + 1], b2[NBINS + 1], xtilde[NBINS + 1];
277864f89611Schristos 	int nsamples, txsum, rxsum, rosum, maxidx;
277964f89611Schristos 	int order, order5x, order5xrem, order3x, order3xrem, y5, y3;
278064f89611Schristos 	int icept, G, I, L, M, angle, xnonlin, y2, y4, sumy2, sumy4;
278164f89611Schristos 	int alpha, beta, scale, Qalpha, Qbeta, Qscale, Qx, Qb1, Qb2;
278264f89611Schristos 	int tavg, ttilde, maxb1abs, maxb2abs, maxxtildeabs, in;
278364f89611Schristos 	int tmp, i;
278464f89611Schristos 
278564f89611Schristos 	/* Set values at origin. */
278664f89611Schristos 	x[0] = y[0] = t[0] = 0;
278764f89611Schristos 
278864f89611Schristos #define SCALE	32
278964f89611Schristos 	maxidx = 0;
279064f89611Schristos 	for (i = 0; i < NBINS; i++) {
279164f89611Schristos 		nsamples = lo[i] & 0xffff;
279264f89611Schristos 		/* Skip bins that contain 16 or less samples. */
279364f89611Schristos 		if (nsamples <= 16) {
279464f89611Schristos 			x[i + 1] = y[i + 1] = t[i + 1] = 0;
279564f89611Schristos 			continue;
279664f89611Schristos 		}
279764f89611Schristos 		txsum = (hi[i] & 0x7ff) << 16 | lo[i] >> 16;
279864f89611Schristos 		rxsum = (lo[i + NBINS] & 0xffff) << 5 |
279964f89611Schristos 		    ((hi[i] >> 11) & 0x1f);
280064f89611Schristos 		rosum = (hi[i + NBINS] & 0x7ff) << 16 | hi[i + NBINS] >> 16;
280164f89611Schristos 		/* Sign-extend 27-bit value. */
280264f89611Schristos 		rosum = (rosum ^ 0x4000000) - 0x4000000;
280364f89611Schristos 
280464f89611Schristos 		txsum *= SCALE;
280564f89611Schristos 		rxsum *= SCALE;
280664f89611Schristos 		rosum *= SCALE;
280764f89611Schristos 
280864f89611Schristos 		x[i + 1] = ((txsum + nsamples) / nsamples + SCALE) / SCALE;
280964f89611Schristos 		y[i + 1] = ((rxsum + nsamples) / nsamples + SCALE) / SCALE +
281064f89611Schristos 		    SCALE * maxidx + SCALE / 2;
281164f89611Schristos 		t[i + 1] = (rosum + nsamples) / nsamples;
281264f89611Schristos 		maxidx++;
281364f89611Schristos 	}
281464f89611Schristos #undef SCALE
281564f89611Schristos 
281664f89611Schristos #define SCALE_LOG	8
281764f89611Schristos #define SCALE		(1 << SCALE_LOG)
281864f89611Schristos 	if (x[6] == x[3])
281964f89611Schristos 		return 1;	/* Prevent division by 0. */
282064f89611Schristos 	G = ((y[6] - y[3]) * SCALE + (x[6] - x[3])) / (x[6] - x[3]);
282164f89611Schristos 	if (G == 0)
282264f89611Schristos 		return 1;	/* Prevent division by 0. */
282364f89611Schristos 
282464f89611Schristos 	sc->sc_gain1[chain] = G;	/* Save low signal gain. */
282564f89611Schristos 
282664f89611Schristos 	/* Find interception point. */
282764f89611Schristos 	icept = (G * (x[0] - x[3]) + SCALE) / SCALE + y[3];
282864f89611Schristos 	for (i = 0; i <= 3; i++) {
282964f89611Schristos 		y[i] = i * 32;
283064f89611Schristos 		x[i] = (y[i] * SCALE + G) / G;
283164f89611Schristos 	}
283264f89611Schristos 	for (i = 4; i <= maxidx; i++)
283364f89611Schristos 		y[i] -= icept;
283464f89611Schristos 
283564f89611Schristos 	xnonlin = x[maxidx] - (y[maxidx] * SCALE + G) / G;
283664f89611Schristos 	order = (xnonlin + y[maxidx]) / y[maxidx];
283764f89611Schristos 	if (order == 0)
283864f89611Schristos 		M = 10;
283964f89611Schristos 	else if (order == 1)
284064f89611Schristos 		M = 9;
284164f89611Schristos 	else
284264f89611Schristos 		M = 8;
284364f89611Schristos 
284464f89611Schristos 	I = (maxidx >= 16) ? 7 : maxidx / 2;
284564f89611Schristos 	L = maxidx - I;
284664f89611Schristos 
284764f89611Schristos 	sumy2 = sumy4 = y2 = y4 = 0;
284864f89611Schristos 	for (i = 0; i <= L; i++) {
284964f89611Schristos 		if (y[i + I] == 0)
285064f89611Schristos 			return 1;	/* Prevent division by 0. */
285164f89611Schristos 
285264f89611Schristos 		xnonlin = x[i + I] - ((y[i + I] * SCALE) + G) / G;
285364f89611Schristos 		xtilde[i] = ((xnonlin << M) + y[i + I]) / y[i + I];
285464f89611Schristos 		xtilde[i] = ((xtilde[i] << M) + y[i + I]) / y[i + I];
285564f89611Schristos 		xtilde[i] = ((xtilde[i] << M) + y[i + I]) / y[i + I];
285664f89611Schristos 
285764f89611Schristos 		y2 = (y[i + I] * y[i + I] + SCALE * SCALE) / (SCALE * SCALE);
285864f89611Schristos 
285964f89611Schristos 		sumy2 += y2;
286064f89611Schristos 		sumy4 += y2 * y2;
286164f89611Schristos 
286264f89611Schristos 		b1[i] = y2 * (L + 1);
286364f89611Schristos 		b2[i] = y2;
286464f89611Schristos 	}
286564f89611Schristos 	for (i = 0; i <= L; i++) {
286664f89611Schristos 		b1[i] -= sumy2;
286764f89611Schristos 		b2[i] = sumy4 - sumy2 * b2[i];
286864f89611Schristos 	}
286964f89611Schristos 
287064f89611Schristos 	maxxtildeabs = maxb1abs = maxb2abs = 0;
287164f89611Schristos 	for (i = 0; i <= L; i++) {
287264f89611Schristos 		tmp = abs(xtilde[i]);
287364f89611Schristos 		if (tmp > maxxtildeabs)
287464f89611Schristos 			maxxtildeabs = tmp;
287564f89611Schristos 
287664f89611Schristos 		tmp = abs(b1[i]);
287764f89611Schristos 		if (tmp > maxb1abs)
287864f89611Schristos 			maxb1abs = tmp;
287964f89611Schristos 
288064f89611Schristos 		tmp = abs(b2[i]);
288164f89611Schristos 		if (tmp > maxb2abs)
288264f89611Schristos 			maxb2abs = tmp;
288364f89611Schristos 	}
288464f89611Schristos 	Qx  = get_scale(maxxtildeabs);
288564f89611Schristos 	Qb1 = get_scale(maxb1abs);
288664f89611Schristos 	Qb2 = get_scale(maxb2abs);
288764f89611Schristos 	for (i = 0; i <= L; i++) {
288864f89611Schristos 		xtilde[i] /= 1 << Qx;
288964f89611Schristos 		b1[i] /= 1 << Qb1;
289064f89611Schristos 		b2[i] /= 1 << Qb2;
289164f89611Schristos 	}
289264f89611Schristos 
289364f89611Schristos 	alpha = beta = 0;
289464f89611Schristos 	for (i = 0; i <= L; i++) {
289564f89611Schristos 		alpha += b1[i] * xtilde[i];
289664f89611Schristos 		beta  += b2[i] * xtilde[i];
289764f89611Schristos 	}
289864f89611Schristos 
289964f89611Schristos 	scale = ((y4 / SCALE_LOG) * (L + 1) -
290064f89611Schristos 		 (y2 / SCALE_LOG) * sumy2) * SCALE_LOG;
290164f89611Schristos 
290264f89611Schristos 	Qscale = get_scale(abs(scale));
290364f89611Schristos 	scale /= 1 << Qscale;
290464f89611Schristos 	Qalpha = get_scale(abs(alpha));
290564f89611Schristos 	alpha /= 1 << Qalpha;
290664f89611Schristos 	Qbeta  = get_scale(abs(beta));
290764f89611Schristos 	beta  /= 1 << Qbeta;
290864f89611Schristos 
290964f89611Schristos 	order = 3 * M - Qx - Qb1 - Qbeta + 10 + Qscale;
291064f89611Schristos 	order5x = 1 << (order / 5);
291164f89611Schristos 	order5xrem = 1 << (order % 5);
291264f89611Schristos 
291364f89611Schristos 	order = 3 * M - Qx - Qb2 - Qalpha + 10 + Qscale;
291464f89611Schristos 	order3x = 1 << (order / 3);
291564f89611Schristos 	order3xrem = 1 << (order % 3);
291664f89611Schristos 
291764f89611Schristos 	for (i = 0; i < AR9003_PAPRD_MEM_TAB_SIZE; i++) {
291864f89611Schristos 		tmp = i * 32;
291964f89611Schristos 
292064f89611Schristos 		/* Fifth order. */
292164f89611Schristos 		y5 = ((beta * tmp) / 64) / order5x;
292264f89611Schristos 		y5 = (y5 * tmp) / order5x;
292364f89611Schristos 		y5 = (y5 * tmp) / order5x;
292464f89611Schristos 		y5 = (y5 * tmp) / order5x;
292564f89611Schristos 		y5 = (y5 * tmp) / order5x;
292664f89611Schristos 		y5 = y5 / order5xrem;
292764f89611Schristos 
292864f89611Schristos 		/* Third oder. */
292964f89611Schristos 		y3 = (alpha * tmp) / order3x;
293064f89611Schristos 		y3 = (y3 * tmp) / order3x;
293164f89611Schristos 		y3 = (y3 * tmp) / order3x;
293264f89611Schristos 		y3 = y3 / order3xrem;
293364f89611Schristos 
293464f89611Schristos 		in = y5 + y3 + (SCALE * tmp) / G;
293564f89611Schristos 		if (i >= 2 && in < sc->sc_pa_in[chain][i - 1]) {
293664f89611Schristos 			in = sc->sc_pa_in[chain][i - 1] +
293764f89611Schristos 			    (sc->sc_pa_in[chain][i - 1] -
293864f89611Schristos 			     sc->sc_pa_in[chain][i - 2]);
293964f89611Schristos 		}
294064f89611Schristos 		if (in > 1400)
294164f89611Schristos 			in = 1400;
294264f89611Schristos 		sc->sc_pa_in[chain][i] = in;
294364f89611Schristos 	}
294464f89611Schristos 
294564f89611Schristos 	/* Compute average theta of first 5 bins (linear region). */
294664f89611Schristos 	tavg = 0;
294764f89611Schristos 	for (i = 1; i <= 5; i++)
294864f89611Schristos 		tavg += t[i];
294964f89611Schristos 	tavg /= 5;
295064f89611Schristos 	for (i = 1; i <= 5; i++)
295164f89611Schristos 		t[i] = 0;
295264f89611Schristos 	for (i = 6; i <= maxidx; i++)
295364f89611Schristos 		t[i] -= tavg;
295464f89611Schristos 
295564f89611Schristos 	alpha = beta = 0;
295664f89611Schristos 	for (i = 0; i <= L; i++) {
295764f89611Schristos 		ttilde = ((t[i + I] << M) + y[i + I]) / y[i + I];
295864f89611Schristos 		ttilde = ((ttilde << M) +  y[i + I]) / y[i + I];
295964f89611Schristos 		ttilde = ((ttilde << M) +  y[i + I]) / y[i + I];
296064f89611Schristos 
296164f89611Schristos 		alpha += b2[i] * ttilde;
296264f89611Schristos 		beta  += b1[i] * ttilde;
296364f89611Schristos 	}
296464f89611Schristos 
296564f89611Schristos 	Qalpha = get_scale(abs(alpha));
296664f89611Schristos 	alpha /= 1 << Qalpha;
296764f89611Schristos 	Qbeta  = get_scale(abs(beta));
296864f89611Schristos 	beta  /= 1 << Qbeta;
296964f89611Schristos 
297064f89611Schristos 	order = 3 * M - Qx - Qb1 - Qbeta + 10 + Qscale + 5;
297164f89611Schristos 	order5x = 1 << (order / 5);
297264f89611Schristos 	order5xrem = 1 << (order % 5);
297364f89611Schristos 
297464f89611Schristos 	order = 3 * M - Qx - Qb2 - Qalpha + 10 + Qscale + 5;
297564f89611Schristos 	order3x = 1 << (order / 3);
297664f89611Schristos 	order3xrem = 1 << (order % 3);
297764f89611Schristos 
297864f89611Schristos 	for (i = 0; i <= 4; i++)
297964f89611Schristos 		sc->sc_angle[chain][i] = 0;	/* Linear at that range. */
298064f89611Schristos 	for (i = 5; i < AR9003_PAPRD_MEM_TAB_SIZE; i++) {
298164f89611Schristos 		tmp = i * 32;
298264f89611Schristos 
298364f89611Schristos 		/* Fifth order. */
298464f89611Schristos 		if (beta > 0)
298564f89611Schristos 			y5 = (((beta * tmp - 64) / 64) - order5x) / order5x;
298664f89611Schristos 		else
298764f89611Schristos 			y5 = (((beta * tmp - 64) / 64) + order5x) / order5x;
298864f89611Schristos 		y5 = (y5 * tmp) / order5x;
298964f89611Schristos 		y5 = (y5 * tmp) / order5x;
299064f89611Schristos 		y5 = (y5 * tmp) / order5x;
299164f89611Schristos 		y5 = (y5 * tmp) / order5x;
299264f89611Schristos 		y5 = y5 / order5xrem;
299364f89611Schristos 
299464f89611Schristos 		/* Third oder. */
299564f89611Schristos 		if (beta > 0)	/* XXX alpha? */
299664f89611Schristos 			y3 = (alpha * tmp - order3x) / order3x;
299764f89611Schristos 		else
299864f89611Schristos 			y3 = (alpha * tmp + order3x) / order3x;
299964f89611Schristos 		y3 = (y3 * tmp) / order3x;
300064f89611Schristos 		y3 = (y3 * tmp) / order3x;
300164f89611Schristos 		y3 = y3 / order3xrem;
300264f89611Schristos 
300364f89611Schristos 		angle = y5 + y3;
300464f89611Schristos 		if (angle < -150)
300564f89611Schristos 			angle = -150;
300664f89611Schristos 		else if (angle > 150)
300764f89611Schristos 			angle = 150;
300864f89611Schristos 		sc->sc_angle[chain][i] = angle;
300964f89611Schristos 	}
301064f89611Schristos 	/* Angle for entry 4 is derived from angle for entry 5. */
301164f89611Schristos 	sc->sc_angle[chain][4] = (sc->sc_angle[chain][5] + 2) / 2;
301264f89611Schristos 
301364f89611Schristos 	return 0;
301464f89611Schristos #undef SCALE
301564f89611Schristos #undef SCALE_LOG
301664f89611Schristos #undef NBINS
301764f89611Schristos }
301864f89611Schristos 
301964f89611Schristos Static void
ar9003_enable_predistorter(struct athn_softc * sc,int chain)302064f89611Schristos ar9003_enable_predistorter(struct athn_softc *sc, int chain)
302164f89611Schristos {
302264f89611Schristos 	uint32_t reg;
302364f89611Schristos 	int i;
302464f89611Schristos 
302564f89611Schristos 	/* Write digital predistorter lookup table. */
302664f89611Schristos 	for (i = 0; i < AR9003_PAPRD_MEM_TAB_SIZE; i++) {
302764f89611Schristos 		AR_WRITE(sc, AR_PHY_PAPRD_MEM_TAB_B(chain, i),
302864f89611Schristos 		    SM(AR_PHY_PAPRD_PA_IN, sc->sc_pa_in[chain][i]) |
302964f89611Schristos 		    SM(AR_PHY_PAPRD_ANGLE, sc->sc_angle[chain][i]));
303064f89611Schristos 	}
303164f89611Schristos 
303264f89611Schristos 	reg = AR_READ(sc, AR_PHY_PA_GAIN123_B(chain));
303364f89611Schristos 	reg = RW(reg, AR_PHY_PA_GAIN123_PA_GAIN1, sc->sc_gain1[chain]);
303464f89611Schristos 	AR_WRITE(sc, AR_PHY_PA_GAIN123_B(chain), reg);
303564f89611Schristos 
303664f89611Schristos 	/* Indicate Tx power used for calibration (training signal). */
303764f89611Schristos 	reg = AR_READ(sc, AR_PHY_PAPRD_CTRL1_B(chain));
303864f89611Schristos 	reg = RW(reg, AR_PHY_PAPRD_CTRL1_POWER_AT_AM2AM_CAL, sc->sc_trainpow);
303964f89611Schristos 	AR_WRITE(sc, AR_PHY_PAPRD_CTRL1_B(chain), reg);
304064f89611Schristos 
304164f89611Schristos 	/* Enable digital predistorter for this chain. */
304264f89611Schristos 	AR_SETBITS(sc, AR_PHY_PAPRD_CTRL0_B(chain),
304364f89611Schristos 	    AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE);
304464f89611Schristos 	AR_WRITE_BARRIER(sc);
304564f89611Schristos }
304664f89611Schristos 
304764f89611Schristos Static void
ar9003_paprd_enable(struct athn_softc * sc)304864f89611Schristos ar9003_paprd_enable(struct athn_softc *sc)
304964f89611Schristos {
305064f89611Schristos 	int i;
305164f89611Schristos 
305264f89611Schristos 	/* Enable digital predistorters for all Tx chains. */
305364f89611Schristos 	for (i = 0; i < AR9003_MAX_CHAINS; i++)
305464f89611Schristos 		if (sc->sc_txchainmask & (1 << i))
305564f89611Schristos 			ar9003_enable_predistorter(sc, i);
305664f89611Schristos }
305764f89611Schristos 
305864f89611Schristos /*
305964f89611Schristos  * This function is called when our training signal has been sent.
306064f89611Schristos  */
306164f89611Schristos Static void
ar9003_paprd_tx_tone_done(struct athn_softc * sc)306264f89611Schristos ar9003_paprd_tx_tone_done(struct athn_softc *sc)
306364f89611Schristos {
306464f89611Schristos 	uint32_t lo[48], hi[48];
306564f89611Schristos 	size_t i;
306664f89611Schristos 
306764f89611Schristos 	/* Make sure training is complete. */
306864f89611Schristos 	if (!(AR_READ(sc, AR_PHY_PAPRD_TRAINER_STAT1) &
306964f89611Schristos 	    AR_PHY_PAPRD_TRAINER_STAT1_TRAIN_DONE))
307064f89611Schristos 		return;
307164f89611Schristos 
307264f89611Schristos 	/* Read feedback from training signal. */
307364f89611Schristos 	AR_CLRBITS(sc, AR_PHY_CHAN_INFO_MEMORY, AR_PHY_CHAN_INFO_TAB_S2_READ);
307464f89611Schristos 	for (i = 0; i < __arraycount(lo); i++)
307564f89611Schristos 		lo[i] = AR_READ(sc, AR_PHY_CHAN_INFO_TAB(0, i));
307664f89611Schristos 	AR_SETBITS(sc, AR_PHY_CHAN_INFO_MEMORY, AR_PHY_CHAN_INFO_TAB_S2_READ);
307764f89611Schristos 	for (i = 0; i < __arraycount(hi); i++)
307864f89611Schristos 		hi[i] = AR_READ(sc, AR_PHY_CHAN_INFO_TAB(0, i));
307964f89611Schristos 
308064f89611Schristos 	AR_CLRBITS(sc, AR_PHY_PAPRD_TRAINER_STAT1,
308164f89611Schristos 	    AR_PHY_PAPRD_TRAINER_STAT1_TRAIN_DONE);
308264f89611Schristos 
308364f89611Schristos 	/* Compute predistortion function based on this feedback. */
308464f89611Schristos 	if (ar9003_compute_predistortion(sc, lo, hi) != 0)
308564f89611Schristos 		return;
308664f89611Schristos 
308764f89611Schristos 	/* Get next available Tx chain. */
308864f89611Schristos 	while (++sc->sc_paprd_curchain < AR9003_MAX_CHAINS)
308964f89611Schristos 		if (sc->sc_txchainmask & (1 << sc->sc_paprd_curchain))
309064f89611Schristos 			break;
309164f89611Schristos 	if (sc->sc_paprd_curchain == AR9003_MAX_CHAINS) {
309264f89611Schristos 		/* All Tx chains measured; enable digital predistortion. */
309364f89611Schristos 		ar9003_paprd_enable(sc);
309464f89611Schristos 	}
309564f89611Schristos 	else	/* Measure next Tx chain. */
309664f89611Schristos 		ar9003_paprd_tx_tone(sc);
309764f89611Schristos }
309864f89611Schristos 
309964f89611Schristos PUBLIC void
ar9003_write_txpower(struct athn_softc * sc,int16_t power[ATHN_POWER_COUNT])310064f89611Schristos ar9003_write_txpower(struct athn_softc *sc, int16_t power[ATHN_POWER_COUNT])
310164f89611Schristos {
310264f89611Schristos 
310364f89611Schristos 	/* Make sure forced gain is disabled. */
310464f89611Schristos 	AR_WRITE(sc, AR_PHY_TX_FORCED_GAIN, 0);
310564f89611Schristos 
310664f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE1,
310764f89611Schristos 	    (power[ATHN_POWER_OFDM18  ] & 0x3f) << 24 |
310864f89611Schristos 	    (power[ATHN_POWER_OFDM12  ] & 0x3f) << 16 |
310964f89611Schristos 	    (power[ATHN_POWER_OFDM9   ] & 0x3f) <<  8 |
311064f89611Schristos 	    (power[ATHN_POWER_OFDM6   ] & 0x3f));
311164f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE2,
311264f89611Schristos 	    (power[ATHN_POWER_OFDM54  ] & 0x3f) << 24 |
311364f89611Schristos 	    (power[ATHN_POWER_OFDM48  ] & 0x3f) << 16 |
311464f89611Schristos 	    (power[ATHN_POWER_OFDM36  ] & 0x3f) <<  8 |
311564f89611Schristos 	    (power[ATHN_POWER_OFDM24  ] & 0x3f));
311664f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE3,
311764f89611Schristos 	    (power[ATHN_POWER_CCK2_SP ] & 0x3f) << 24 |
311864f89611Schristos 	    (power[ATHN_POWER_CCK2_LP ] & 0x3f) << 16 |
311964f89611Schristos 	    /* NB: No eXtended Range for AR9003. */
312064f89611Schristos 	    (power[ATHN_POWER_CCK1_LP ] & 0x3f));
312164f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE4,
312264f89611Schristos 	    (power[ATHN_POWER_CCK11_SP] & 0x3f) << 24 |
312364f89611Schristos 	    (power[ATHN_POWER_CCK11_LP] & 0x3f) << 16 |
312464f89611Schristos 	    (power[ATHN_POWER_CCK55_SP] & 0x3f) <<  8 |
312564f89611Schristos 	    (power[ATHN_POWER_CCK55_LP] & 0x3f));
312664f89611Schristos 	/*
312764f89611Schristos 	 * NB: AR_PHY_PWRTX_RATE5 needs to be written even if HT is disabled
312864f89611Schristos 	 * because it is read by PA predistortion functions.
312964f89611Schristos 	 */
313064f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE5,
313164f89611Schristos 	    (power[ATHN_POWER_HT20( 5)] & 0x3f) << 24 |
313264f89611Schristos 	    (power[ATHN_POWER_HT20( 4)] & 0x3f) << 16 |
313364f89611Schristos 	    (power[ATHN_POWER_HT20( 1)] & 0x3f) <<  8 |
313464f89611Schristos 	    (power[ATHN_POWER_HT20( 0)] & 0x3f));
313564f89611Schristos #ifndef IEEE80211_NO_HT
313664f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE6,
313764f89611Schristos 	    (power[ATHN_POWER_HT20(13)] & 0x3f) << 24 |
313864f89611Schristos 	    (power[ATHN_POWER_HT20(12)] & 0x3f) << 16 |
313964f89611Schristos 	    (power[ATHN_POWER_HT20( 7)] & 0x3f) <<  8 |
314064f89611Schristos 	    (power[ATHN_POWER_HT20( 6)] & 0x3f));
314164f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE7,
314264f89611Schristos 	    (power[ATHN_POWER_HT40( 5)] & 0x3f) << 24 |
314364f89611Schristos 	    (power[ATHN_POWER_HT40( 4)] & 0x3f) << 16 |
314464f89611Schristos 	    (power[ATHN_POWER_HT40( 1)] & 0x3f) <<  8 |
314564f89611Schristos 	    (power[ATHN_POWER_HT40( 0)] & 0x3f));
314664f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE8,
314764f89611Schristos 	    (power[ATHN_POWER_HT40(13)] & 0x3f) << 24 |
314864f89611Schristos 	    (power[ATHN_POWER_HT40(12)] & 0x3f) << 16 |
314964f89611Schristos 	    (power[ATHN_POWER_HT40( 7)] & 0x3f) <<  8 |
315064f89611Schristos 	    (power[ATHN_POWER_HT40( 6)] & 0x3f));
315164f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE10,
315264f89611Schristos 	    (power[ATHN_POWER_HT20(21)] & 0x3f) << 24 |
315364f89611Schristos 	    (power[ATHN_POWER_HT20(20)] & 0x3f) << 16 |
315464f89611Schristos 	    (power[ATHN_POWER_HT20(15)] & 0x3f) <<  8 |
315564f89611Schristos 	    (power[ATHN_POWER_HT20(14)] & 0x3f));
315664f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE11,
315764f89611Schristos 	    (power[ATHN_POWER_HT40(23)] & 0x3f) << 24 |
315864f89611Schristos 	    (power[ATHN_POWER_HT40(22)] & 0x3f) << 16 |
315964f89611Schristos 	    (power[ATHN_POWER_HT20(23)] & 0x3f) <<  8 |
316064f89611Schristos 	    (power[ATHN_POWER_HT20(22)] & 0x3f));
316164f89611Schristos 	AR_WRITE(sc, AR_PHY_PWRTX_RATE12,
316264f89611Schristos 	    (power[ATHN_POWER_HT40(21)] & 0x3f) << 24 |
316364f89611Schristos 	    (power[ATHN_POWER_HT40(20)] & 0x3f) << 16 |
316464f89611Schristos 	    (power[ATHN_POWER_HT40(15)] & 0x3f) <<  8 |
316564f89611Schristos 	    (power[ATHN_POWER_HT40(14)] & 0x3f));
316664f89611Schristos #endif
316764f89611Schristos 	AR_WRITE_BARRIER(sc);
316864f89611Schristos }
316964f89611Schristos 
317064f89611Schristos Static void
ar9003_reset_rx_gain(struct athn_softc * sc,struct ieee80211_channel * c)317164f89611Schristos ar9003_reset_rx_gain(struct athn_softc *sc, struct ieee80211_channel *c)
317264f89611Schristos {
317364f89611Schristos #define X(x)	((uint32_t)(x) << 2)
317464f89611Schristos 	const struct athn_gain *prog = sc->sc_rx_gain;
317564f89611Schristos 	const uint32_t *pvals;
317664f89611Schristos 	int i;
317764f89611Schristos 
317864f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
317964f89611Schristos 		pvals = prog->vals_2g;
318064f89611Schristos 	else
318164f89611Schristos 		pvals = prog->vals_5g;
318264f89611Schristos 	for (i = 0; i < prog->nregs; i++)
318364f89611Schristos 		AR_WRITE(sc, X(prog->regs[i]), pvals[i]);
318464f89611Schristos 	AR_WRITE_BARRIER(sc);
318564f89611Schristos #undef X
318664f89611Schristos }
318764f89611Schristos 
318864f89611Schristos Static void
ar9003_reset_tx_gain(struct athn_softc * sc,struct ieee80211_channel * c)318964f89611Schristos ar9003_reset_tx_gain(struct athn_softc *sc, struct ieee80211_channel *c)
319064f89611Schristos {
319164f89611Schristos #define X(x)	((uint32_t)(x) << 2)
319264f89611Schristos 	const struct athn_gain *prog = sc->sc_tx_gain;
319364f89611Schristos 	const uint32_t *pvals;
319464f89611Schristos 	int i;
319564f89611Schristos 
319664f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
319764f89611Schristos 		pvals = prog->vals_2g;
319864f89611Schristos 	else
319964f89611Schristos 		pvals = prog->vals_5g;
320064f89611Schristos 	for (i = 0; i < prog->nregs; i++)
320164f89611Schristos 		AR_WRITE(sc, X(prog->regs[i]), pvals[i]);
320264f89611Schristos 	AR_WRITE_BARRIER(sc);
320364f89611Schristos #undef X
320464f89611Schristos }
320564f89611Schristos 
320664f89611Schristos Static void
ar9003_hw_init(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)320764f89611Schristos ar9003_hw_init(struct athn_softc *sc, struct ieee80211_channel *c,
320864f89611Schristos     struct ieee80211_channel *extc)
320964f89611Schristos {
321064f89611Schristos #define X(x)	((uint32_t)(x) << 2)
321164f89611Schristos 	struct athn_ops *ops = &sc->sc_ops;
321264f89611Schristos 	const struct athn_ini *ini = sc->sc_ini;
321364f89611Schristos 	const uint32_t *pvals;
321464f89611Schristos 	uint32_t reg;
321564f89611Schristos 	int i;
321664f89611Schristos 
321764f89611Schristos 	/*
321864f89611Schristos 	 * The common init values include the pre and core phases for the
321964f89611Schristos 	 * SoC, MAC, BB and Radio subsystems.
322064f89611Schristos 	 */
322164f89611Schristos 	DPRINTFN(DBG_INIT, sc, "writing pre and core init vals\n");
322264f89611Schristos 	for (i = 0; i < ini->ncmregs; i++) {
322364f89611Schristos 		AR_WRITE(sc, X(ini->cmregs[i]), ini->cmvals[i]);
322464f89611Schristos 		if (AR_IS_ANALOG_REG(X(ini->cmregs[i])))
322564f89611Schristos 			DELAY(100);
322664f89611Schristos 		if ((i & 0x1f) == 0)
322764f89611Schristos 			DELAY(1);
322864f89611Schristos 	}
322964f89611Schristos 
323064f89611Schristos 	/*
323164f89611Schristos 	 * The modal init values include the post phase for the SoC, MAC,
323264f89611Schristos 	 * BB and Radio subsystems.
323364f89611Schristos 	 */
323464f89611Schristos #ifndef IEEE80211_NO_HT
323564f89611Schristos 	if (extc != NULL) {
323664f89611Schristos 		if (IEEE80211_IS_CHAN_2GHZ(c))
323764f89611Schristos 			pvals = ini->vals_2g40;
323864f89611Schristos 		else
323964f89611Schristos 			pvals = ini->vals_5g40;
324064f89611Schristos 	}
324164f89611Schristos 	else
324264f89611Schristos #endif
324364f89611Schristos 	{
324464f89611Schristos 		if (IEEE80211_IS_CHAN_2GHZ(c))
324564f89611Schristos 			pvals = ini->vals_2g20;
324664f89611Schristos 		else
324764f89611Schristos 			pvals = ini->vals_5g20;
324864f89611Schristos 	}
324964f89611Schristos 	DPRINTFN(DBG_INIT, sc, "writing post init vals\n");
325064f89611Schristos 	for (i = 0; i < ini->nregs; i++) {
325164f89611Schristos 		AR_WRITE(sc, X(ini->regs[i]), pvals[i]);
325264f89611Schristos 		if (AR_IS_ANALOG_REG(X(ini->regs[i])))
325364f89611Schristos 			DELAY(100);
325464f89611Schristos 		if ((i & 0x1f) == 0)
325564f89611Schristos 			DELAY(1);
325664f89611Schristos 	}
325764f89611Schristos 
325864f89611Schristos 	if (sc->sc_rx_gain != NULL)
325964f89611Schristos 		ar9003_reset_rx_gain(sc, c);
326064f89611Schristos 	if (sc->sc_tx_gain != NULL)
326164f89611Schristos 		ar9003_reset_tx_gain(sc, c);
326264f89611Schristos 
326364f89611Schristos 	if (IEEE80211_IS_CHAN_5GHZ(c) &&
326464f89611Schristos 	    (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
326564f89611Schristos 		/* Update modal values for fast PLL clock. */
326664f89611Schristos #ifndef IEEE80211_NO_HT
326764f89611Schristos 		if (extc != NULL)
326864f89611Schristos 			pvals = ini->fastvals_5g40;
326964f89611Schristos 		else
327064f89611Schristos #endif
327164f89611Schristos 			pvals = ini->fastvals_5g20;
327264f89611Schristos 		DPRINTFN(DBG_INIT, sc, "writing fast pll clock init vals\n");
327364f89611Schristos 		for (i = 0; i < ini->nfastregs; i++) {
327464f89611Schristos 			AR_WRITE(sc, X(ini->fastregs[i]), pvals[i]);
327564f89611Schristos 			if (AR_IS_ANALOG_REG(X(ini->fastregs[i])))
327664f89611Schristos 				DELAY(100);
327764f89611Schristos 			if ((i & 0x1f) == 0)
327864f89611Schristos 				DELAY(1);
327964f89611Schristos 		}
328064f89611Schristos 	}
328164f89611Schristos 
328264f89611Schristos 	/*
328364f89611Schristos 	 * Set the RX_ABORT and RX_DIS bits to prevent frames with corrupted
328464f89611Schristos 	 * descriptor status.
328564f89611Schristos 	 */
328664f89611Schristos 	AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
328764f89611Schristos 
328864f89611Schristos 	reg = AR_READ(sc, AR_PCU_MISC_MODE2);
328964f89611Schristos 	reg &= ~AR_PCU_MISC_MODE2_ADHOC_MCAST_KEYID_ENABLE;
329064f89611Schristos 	reg |= AR_PCU_MISC_MODE2_AGG_WEP_ENABLE_FIX;
329164f89611Schristos 	reg |= AR_PCU_MISC_MODE2_ENABLE_AGGWEP;
329264f89611Schristos 	AR_WRITE(sc, AR_PCU_MISC_MODE2, reg);
329364f89611Schristos 	AR_WRITE_BARRIER(sc);
329464f89611Schristos 
329564f89611Schristos 	ar9003_set_phy(sc, c, extc);
329664f89611Schristos 	ar9003_init_chains(sc);
329764f89611Schristos 
329864f89611Schristos 	ops->set_txpower(sc, c, extc);
329964f89611Schristos #undef X
330064f89611Schristos }
330164f89611Schristos 
330264f89611Schristos PUBLIC void
ar9003_get_lg_tpow(struct athn_softc * sc,struct ieee80211_channel * c,uint8_t ctl,const uint8_t * fbins,const struct ar_cal_target_power_leg * tgt,int nchans,uint8_t tpow[4])330364f89611Schristos ar9003_get_lg_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
330464f89611Schristos     uint8_t ctl, const uint8_t *fbins,
330564f89611Schristos     const struct ar_cal_target_power_leg *tgt, int nchans, uint8_t tpow[4])
330664f89611Schristos {
330764f89611Schristos 	uint8_t fbin;
330864f89611Schristos 	int i, delta, lo, hi;
330964f89611Schristos 
331064f89611Schristos 	lo = hi = -1;
331164f89611Schristos 	fbin = athn_chan2fbin(c);
331264f89611Schristos 	for (i = 0; i < nchans; i++) {
331364f89611Schristos 		delta = fbin - fbins[i];
331464f89611Schristos 		/* Find the largest sample that is <= our frequency. */
331564f89611Schristos 		if (delta >= 0 && (lo == -1 || delta < fbin - fbins[lo]))
331664f89611Schristos 			lo = i;
331764f89611Schristos 		/* Find the smallest sample that is >= our frequency. */
331864f89611Schristos 		if (delta <= 0 && (hi == -1 || delta > fbin - fbins[hi]))
331964f89611Schristos 			hi = i;
332064f89611Schristos 	}
332164f89611Schristos 	if (lo == -1)
332264f89611Schristos 		lo = hi;
332364f89611Schristos 	else if (hi == -1)
332464f89611Schristos 		hi = lo;
332564f89611Schristos 	/* Interpolate values. */
332664f89611Schristos 	for (i = 0; i < 4; i++) {
332764f89611Schristos 		tpow[i] = athn_interpolate(fbin,
332864f89611Schristos 		    fbins[lo], tgt[lo].tPow2x[i],
332964f89611Schristos 		    fbins[hi], tgt[hi].tPow2x[i]);
333064f89611Schristos 	}
333164f89611Schristos 	/* XXX Apply conformance test limit. */
333264f89611Schristos }
333364f89611Schristos 
333464f89611Schristos PUBLIC void
ar9003_get_ht_tpow(struct athn_softc * sc,struct ieee80211_channel * c,uint8_t ctl,const uint8_t * fbins,const struct ar_cal_target_power_ht * tgt,int nchans,uint8_t tpow[14])333564f89611Schristos ar9003_get_ht_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
333664f89611Schristos     uint8_t ctl, const uint8_t *fbins,
333764f89611Schristos     const struct ar_cal_target_power_ht *tgt, int nchans, uint8_t tpow[14])
333864f89611Schristos {
333964f89611Schristos 	uint8_t fbin;
334064f89611Schristos 	int i, delta, lo, hi;
334164f89611Schristos 
334264f89611Schristos 	lo = hi = -1;
334364f89611Schristos 	fbin = athn_chan2fbin(c);
334464f89611Schristos 	for (i = 0; i < nchans; i++) {
334564f89611Schristos 		delta = fbin - fbins[i];
334664f89611Schristos 		/* Find the largest sample that is <= our frequency. */
334764f89611Schristos 		if (delta >= 0 && (lo == -1 || delta < fbin - fbins[lo]))
334864f89611Schristos 			lo = i;
334964f89611Schristos 		/* Find the smallest sample that is >= our frequency. */
335064f89611Schristos 		if (delta <= 0 && (hi == -1 || delta > fbin - fbins[hi]))
335164f89611Schristos 			hi = i;
335264f89611Schristos 	}
335364f89611Schristos 	if (lo == -1)
335464f89611Schristos 		lo = hi;
335564f89611Schristos 	else if (hi == -1)
335664f89611Schristos 		hi = lo;
335764f89611Schristos 	/* Interpolate values. */
335864f89611Schristos 	for (i = 0; i < 14; i++) {
335964f89611Schristos 		tpow[i] = athn_interpolate(fbin,
336064f89611Schristos 		    fbins[lo], tgt[lo].tPow2x[i],
336164f89611Schristos 		    fbins[hi], tgt[hi].tPow2x[i]);
336264f89611Schristos 	}
336364f89611Schristos 	/* XXX Apply conformance test limit. */
336464f89611Schristos }
336564f89611Schristos 
336664f89611Schristos /*
336764f89611Schristos  * Adaptive noise immunity.
336864f89611Schristos  */
336964f89611Schristos Static void
ar9003_set_noise_immunity_level(struct athn_softc * sc,int level)337064f89611Schristos ar9003_set_noise_immunity_level(struct athn_softc *sc, int level)
337164f89611Schristos {
337264f89611Schristos 	int high = level == 4;
337364f89611Schristos 	uint32_t reg;
337464f89611Schristos 
337564f89611Schristos 	reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
337664f89611Schristos 	reg = RW(reg, AR_PHY_DESIRED_SZ_TOT_DES, high ? -62 : -55);
337764f89611Schristos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
337864f89611Schristos 
337964f89611Schristos 	reg = AR_READ(sc, AR_PHY_AGC);
338064f89611Schristos 	reg = RW(reg, AR_PHY_AGC_COARSE_LOW, high ? -70 : -64);
338164f89611Schristos 	reg = RW(reg, AR_PHY_AGC_COARSE_HIGH, high ? -12 : -14);
338264f89611Schristos 	AR_WRITE(sc, AR_PHY_AGC, reg);
338364f89611Schristos 
338464f89611Schristos 	reg = AR_READ(sc, AR_PHY_FIND_SIG);
338564f89611Schristos 	reg = RW(reg, AR_PHY_FIND_SIG_FIRPWR, high ? -80 : -78);
338664f89611Schristos 	AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
338764f89611Schristos 	AR_WRITE_BARRIER(sc);
338864f89611Schristos }
338964f89611Schristos 
339064f89611Schristos Static void
ar9003_enable_ofdm_weak_signal(struct athn_softc * sc)339164f89611Schristos ar9003_enable_ofdm_weak_signal(struct athn_softc *sc)
339264f89611Schristos {
339364f89611Schristos 	uint32_t reg;
339464f89611Schristos 
339564f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
339664f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 50);
339764f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 40);
339864f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 48);
339964f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
340064f89611Schristos 
340164f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR);
340264f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 77);
340364f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 64);
340464f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 16);
340564f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR, reg);
340664f89611Schristos 
340764f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
340864f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 50);
340964f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 40);
341064f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 77);
341164f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 64);
341264f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
341364f89611Schristos 
341464f89611Schristos 	AR_SETBITS(sc, AR_PHY_SFCORR_LOW,
341564f89611Schristos 	    AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
341664f89611Schristos 	AR_WRITE_BARRIER(sc);
341764f89611Schristos }
341864f89611Schristos 
341964f89611Schristos Static void
ar9003_disable_ofdm_weak_signal(struct athn_softc * sc)342064f89611Schristos ar9003_disable_ofdm_weak_signal(struct athn_softc *sc)
342164f89611Schristos {
342264f89611Schristos 	uint32_t reg;
342364f89611Schristos 
342464f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
342564f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 127);
342664f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 127);
342764f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 63);
342864f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
342964f89611Schristos 
343064f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR);
343164f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 127);
343264f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 127);
343364f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 31);
343464f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR, reg);
343564f89611Schristos 
343664f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
343764f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 127);
343864f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 127);
343964f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 127);
344064f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 127);
344164f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
344264f89611Schristos 
344364f89611Schristos 	AR_CLRBITS(sc, AR_PHY_SFCORR_LOW,
344464f89611Schristos 	    AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
344564f89611Schristos 	AR_WRITE_BARRIER(sc);
344664f89611Schristos }
344764f89611Schristos 
344864f89611Schristos Static void
ar9003_set_cck_weak_signal(struct athn_softc * sc,int high)344964f89611Schristos ar9003_set_cck_weak_signal(struct athn_softc *sc, int high)
345064f89611Schristos {
345164f89611Schristos 	uint32_t reg;
345264f89611Schristos 
345364f89611Schristos 	reg = AR_READ(sc, AR_PHY_CCK_DETECT);
345464f89611Schristos 	reg = RW(reg, AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK, high ? 6 : 8);
345564f89611Schristos 	AR_WRITE(sc, AR_PHY_CCK_DETECT, reg);
345664f89611Schristos 	AR_WRITE_BARRIER(sc);
345764f89611Schristos }
345864f89611Schristos 
345964f89611Schristos Static void
ar9003_set_firstep_level(struct athn_softc * sc,int level)346064f89611Schristos ar9003_set_firstep_level(struct athn_softc *sc, int level)
346164f89611Schristos {
346264f89611Schristos 	uint32_t reg;
346364f89611Schristos 
346464f89611Schristos 	reg = AR_READ(sc, AR_PHY_FIND_SIG);
346564f89611Schristos 	reg = RW(reg, AR_PHY_FIND_SIG_FIRSTEP, level * 4);
346664f89611Schristos 	AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
346764f89611Schristos 	AR_WRITE_BARRIER(sc);
346864f89611Schristos }
346964f89611Schristos 
347064f89611Schristos Static void
ar9003_set_spur_immunity_level(struct athn_softc * sc,int level)347164f89611Schristos ar9003_set_spur_immunity_level(struct athn_softc *sc, int level)
347264f89611Schristos {
347364f89611Schristos 	uint32_t reg;
347464f89611Schristos 
347564f89611Schristos 	reg = AR_READ(sc, AR_PHY_TIMING5);
347664f89611Schristos 	reg = RW(reg, AR_PHY_TIMING5_CYCPWR_THR1, (level + 1) * 2);
347764f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING5, reg);
347864f89611Schristos 	AR_WRITE_BARRIER(sc);
347964f89611Schristos }
3480