xref: /freebsd/sys/dev/ath/ath_hal/ar5212/ar5212_recv.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
16e778a7eSPedro F. Giffuni /*-
26e778a7eSPedro F. Giffuni  * SPDX-License-Identifier: ISC
36e778a7eSPedro F. Giffuni  *
414779705SSam Leffler  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
514779705SSam Leffler  * Copyright (c) 2002-2008 Atheros Communications, Inc.
614779705SSam Leffler  *
714779705SSam Leffler  * Permission to use, copy, modify, and/or distribute this software for any
814779705SSam Leffler  * purpose with or without fee is hereby granted, provided that the above
914779705SSam Leffler  * copyright notice and this permission notice appear in all copies.
1014779705SSam Leffler  *
1114779705SSam Leffler  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1214779705SSam Leffler  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1314779705SSam Leffler  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1414779705SSam Leffler  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1514779705SSam Leffler  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1614779705SSam Leffler  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1714779705SSam Leffler  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1814779705SSam Leffler  */
1914779705SSam Leffler #include "opt_ah.h"
2014779705SSam Leffler 
2114779705SSam Leffler #include "ah.h"
2214779705SSam Leffler #include "ah_internal.h"
2393cad1bdSAdrian Chadd #include "ah_desc.h"
2414779705SSam Leffler 
2514779705SSam Leffler #include "ar5212/ar5212.h"
2614779705SSam Leffler #include "ar5212/ar5212reg.h"
2714779705SSam Leffler #include "ar5212/ar5212desc.h"
2814779705SSam Leffler 
2914779705SSam Leffler /*
3014779705SSam Leffler  * Get the RXDP.
3114779705SSam Leffler  */
3214779705SSam Leffler uint32_t
ar5212GetRxDP(struct ath_hal * ath,HAL_RX_QUEUE qtype)33d60a0680SAdrian Chadd ar5212GetRxDP(struct ath_hal *ath, HAL_RX_QUEUE qtype)
3414779705SSam Leffler {
35d60a0680SAdrian Chadd 
36d60a0680SAdrian Chadd 	HALASSERT(qtype == HAL_RX_QUEUE_HP);
3714779705SSam Leffler 	return OS_REG_READ(ath, AR_RXDP);
3814779705SSam Leffler }
3914779705SSam Leffler 
4014779705SSam Leffler /*
4114779705SSam Leffler  * Set the RxDP.
4214779705SSam Leffler  */
4314779705SSam Leffler void
ar5212SetRxDP(struct ath_hal * ah,uint32_t rxdp,HAL_RX_QUEUE qtype)44d60a0680SAdrian Chadd ar5212SetRxDP(struct ath_hal *ah, uint32_t rxdp, HAL_RX_QUEUE qtype)
4514779705SSam Leffler {
46d60a0680SAdrian Chadd 
47d60a0680SAdrian Chadd 	HALASSERT(qtype == HAL_RX_QUEUE_HP);
4814779705SSam Leffler 	OS_REG_WRITE(ah, AR_RXDP, rxdp);
4914779705SSam Leffler 	HALASSERT(OS_REG_READ(ah, AR_RXDP) == rxdp);
5014779705SSam Leffler }
5114779705SSam Leffler 
5214779705SSam Leffler /*
5314779705SSam Leffler  * Set Receive Enable bits.
5414779705SSam Leffler  */
5514779705SSam Leffler void
ar5212EnableReceive(struct ath_hal * ah)5614779705SSam Leffler ar5212EnableReceive(struct ath_hal *ah)
5714779705SSam Leffler {
5814779705SSam Leffler 	OS_REG_WRITE(ah, AR_CR, AR_CR_RXE);
5914779705SSam Leffler }
6014779705SSam Leffler 
6114779705SSam Leffler /*
6214779705SSam Leffler  * Stop Receive at the DMA engine
6314779705SSam Leffler  */
6414779705SSam Leffler HAL_BOOL
ar5212StopDmaReceive(struct ath_hal * ah)6514779705SSam Leffler ar5212StopDmaReceive(struct ath_hal *ah)
6614779705SSam Leffler {
675e7d0e64SAdrian Chadd 	OS_MARK(ah, AH_MARK_RX_CTL, AH_MARK_RX_CTL_DMA_STOP);
6814779705SSam Leffler 	OS_REG_WRITE(ah, AR_CR, AR_CR_RXD);	/* Set receive disable bit */
6914779705SSam Leffler 	if (!ath_hal_wait(ah, AR_CR, AR_CR_RXE, 0)) {
705e7d0e64SAdrian Chadd 		OS_MARK(ah, AH_MARK_RX_CTL, AH_MARK_RX_CTL_DMA_STOP_ERR);
7114779705SSam Leffler #ifdef AH_DEBUG
7214779705SSam Leffler 		ath_hal_printf(ah, "%s: dma failed to stop in 10ms\n"
7314779705SSam Leffler 			"AR_CR=0x%08x\nAR_DIAG_SW=0x%08x\n",
7414779705SSam Leffler 			__func__,
7514779705SSam Leffler 			OS_REG_READ(ah, AR_CR),
7614779705SSam Leffler 			OS_REG_READ(ah, AR_DIAG_SW));
7714779705SSam Leffler #endif
7814779705SSam Leffler 		return AH_FALSE;
7914779705SSam Leffler 	} else {
8014779705SSam Leffler 		return AH_TRUE;
8114779705SSam Leffler 	}
8214779705SSam Leffler }
8314779705SSam Leffler 
8414779705SSam Leffler /*
8514779705SSam Leffler  * Start Transmit at the PCU engine (unpause receive)
8614779705SSam Leffler  */
8714779705SSam Leffler void
ar5212StartPcuReceive(struct ath_hal * ah,HAL_BOOL is_scanning)88a8083b9cSAdrian Chadd ar5212StartPcuReceive(struct ath_hal *ah, HAL_BOOL is_scanning)
8914779705SSam Leffler {
9014779705SSam Leffler 	struct ath_hal_private *ahp = AH_PRIVATE(ah);
9114779705SSam Leffler 
925e7d0e64SAdrian Chadd 	OS_MARK(ah, AH_MARK_RX_CTL, AH_MARK_RX_CTL_PCU_START);
9314779705SSam Leffler 	OS_REG_WRITE(ah, AR_DIAG_SW,
9414779705SSam Leffler 		OS_REG_READ(ah, AR_DIAG_SW) &~ AR_DIAG_RX_DIS);
9514779705SSam Leffler 	ar5212EnableMibCounters(ah);
96a8083b9cSAdrian Chadd 	/* NB: restore current settings if we're not scanning */
97a8083b9cSAdrian Chadd 	ar5212AniReset(ah, ahp->ah_curchan, ahp->ah_opmode, !is_scanning);
9814779705SSam Leffler }
9914779705SSam Leffler 
10014779705SSam Leffler /*
10114779705SSam Leffler  * Stop Transmit at the PCU engine (pause receive)
10214779705SSam Leffler  */
10314779705SSam Leffler void
ar5212StopPcuReceive(struct ath_hal * ah)10414779705SSam Leffler ar5212StopPcuReceive(struct ath_hal *ah)
10514779705SSam Leffler {
1065e7d0e64SAdrian Chadd 	OS_MARK(ah, AH_MARK_RX_CTL, AH_MARK_RX_CTL_PCU_STOP);
10714779705SSam Leffler 	OS_REG_WRITE(ah, AR_DIAG_SW,
10814779705SSam Leffler 		OS_REG_READ(ah, AR_DIAG_SW) | AR_DIAG_RX_DIS);
10914779705SSam Leffler 	ar5212DisableMibCounters(ah);
11014779705SSam Leffler }
11114779705SSam Leffler 
11214779705SSam Leffler /*
11314779705SSam Leffler  * Set multicast filter 0 (lower 32-bits)
11414779705SSam Leffler  *               filter 1 (upper 32-bits)
11514779705SSam Leffler  */
11614779705SSam Leffler void
ar5212SetMulticastFilter(struct ath_hal * ah,uint32_t filter0,uint32_t filter1)11714779705SSam Leffler ar5212SetMulticastFilter(struct ath_hal *ah, uint32_t filter0, uint32_t filter1)
11814779705SSam Leffler {
11914779705SSam Leffler 	OS_REG_WRITE(ah, AR_MCAST_FIL0, filter0);
12014779705SSam Leffler 	OS_REG_WRITE(ah, AR_MCAST_FIL1, filter1);
12114779705SSam Leffler }
12214779705SSam Leffler 
12314779705SSam Leffler /*
12414779705SSam Leffler  * Clear multicast filter by index
12514779705SSam Leffler  */
12614779705SSam Leffler HAL_BOOL
ar5212ClrMulticastFilterIndex(struct ath_hal * ah,uint32_t ix)12714779705SSam Leffler ar5212ClrMulticastFilterIndex(struct ath_hal *ah, uint32_t ix)
12814779705SSam Leffler {
12914779705SSam Leffler 	uint32_t val;
13014779705SSam Leffler 
13114779705SSam Leffler 	if (ix >= 64)
13214779705SSam Leffler 		return AH_FALSE;
13314779705SSam Leffler 	if (ix >= 32) {
13414779705SSam Leffler 		val = OS_REG_READ(ah, AR_MCAST_FIL1);
13514779705SSam Leffler 		OS_REG_WRITE(ah, AR_MCAST_FIL1, (val &~ (1<<(ix-32))));
13614779705SSam Leffler 	} else {
13714779705SSam Leffler 		val = OS_REG_READ(ah, AR_MCAST_FIL0);
13814779705SSam Leffler 		OS_REG_WRITE(ah, AR_MCAST_FIL0, (val &~ (1<<ix)));
13914779705SSam Leffler 	}
14014779705SSam Leffler 	return AH_TRUE;
14114779705SSam Leffler }
14214779705SSam Leffler 
14314779705SSam Leffler /*
14414779705SSam Leffler  * Set multicast filter by index
14514779705SSam Leffler  */
14614779705SSam Leffler HAL_BOOL
ar5212SetMulticastFilterIndex(struct ath_hal * ah,uint32_t ix)14714779705SSam Leffler ar5212SetMulticastFilterIndex(struct ath_hal *ah, uint32_t ix)
14814779705SSam Leffler {
14914779705SSam Leffler 	uint32_t val;
15014779705SSam Leffler 
15114779705SSam Leffler 	if (ix >= 64)
15214779705SSam Leffler 		return AH_FALSE;
15314779705SSam Leffler 	if (ix >= 32) {
15414779705SSam Leffler 		val = OS_REG_READ(ah, AR_MCAST_FIL1);
15514779705SSam Leffler 		OS_REG_WRITE(ah, AR_MCAST_FIL1, (val | (1<<(ix-32))));
15614779705SSam Leffler 	} else {
15714779705SSam Leffler 		val = OS_REG_READ(ah, AR_MCAST_FIL0);
15814779705SSam Leffler 		OS_REG_WRITE(ah, AR_MCAST_FIL0, (val | (1<<ix)));
15914779705SSam Leffler 	}
16014779705SSam Leffler 	return AH_TRUE;
16114779705SSam Leffler }
16214779705SSam Leffler 
16314779705SSam Leffler /*
16414779705SSam Leffler  * Get the receive filter.
16514779705SSam Leffler  */
16614779705SSam Leffler uint32_t
ar5212GetRxFilter(struct ath_hal * ah)16714779705SSam Leffler ar5212GetRxFilter(struct ath_hal *ah)
16814779705SSam Leffler {
16914779705SSam Leffler 	uint32_t bits = OS_REG_READ(ah, AR_RX_FILTER);
17014779705SSam Leffler 	uint32_t phybits = OS_REG_READ(ah, AR_PHY_ERR);
17114779705SSam Leffler 	if (phybits & AR_PHY_ERR_RADAR)
17214779705SSam Leffler 		bits |= HAL_RX_FILTER_PHYRADAR;
17314779705SSam Leffler 	if (phybits & (AR_PHY_ERR_OFDM_TIMING|AR_PHY_ERR_CCK_TIMING))
17414779705SSam Leffler 		bits |= HAL_RX_FILTER_PHYERR;
1753c3e9d33SSam Leffler 	if (AH_PRIVATE(ah)->ah_caps.halBssidMatchSupport &&
17682cadd5aSSam Leffler 	    (AH5212(ah)->ah_miscMode & AR_MISC_MODE_BSSID_MATCH_FORCE))
1773c3e9d33SSam Leffler 		bits |= HAL_RX_FILTER_BSSID;
17814779705SSam Leffler 	return bits;
17914779705SSam Leffler }
18014779705SSam Leffler 
18114779705SSam Leffler /*
18214779705SSam Leffler  * Set the receive filter.
18314779705SSam Leffler  */
18414779705SSam Leffler void
ar5212SetRxFilter(struct ath_hal * ah,uint32_t bits)18514779705SSam Leffler ar5212SetRxFilter(struct ath_hal *ah, uint32_t bits)
18614779705SSam Leffler {
18782cadd5aSSam Leffler 	struct ath_hal_5212 *ahp = AH5212(ah);
18814779705SSam Leffler 	uint32_t phybits;
18914779705SSam Leffler 
19014779705SSam Leffler 	OS_REG_WRITE(ah, AR_RX_FILTER,
1913c3e9d33SSam Leffler 	    bits &~ (HAL_RX_FILTER_PHYRADAR|HAL_RX_FILTER_PHYERR|
1923c3e9d33SSam Leffler 	    HAL_RX_FILTER_BSSID));
19314779705SSam Leffler 	phybits = 0;
19414779705SSam Leffler 	if (bits & HAL_RX_FILTER_PHYRADAR)
19514779705SSam Leffler 		phybits |= AR_PHY_ERR_RADAR;
19614779705SSam Leffler 	if (bits & HAL_RX_FILTER_PHYERR)
19714779705SSam Leffler 		phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
19814779705SSam Leffler 	OS_REG_WRITE(ah, AR_PHY_ERR, phybits);
19914779705SSam Leffler 	if (phybits) {
20014779705SSam Leffler 		OS_REG_WRITE(ah, AR_RXCFG,
20114779705SSam Leffler 			OS_REG_READ(ah, AR_RXCFG) | AR_RXCFG_ZLFDMA);
20214779705SSam Leffler 	} else {
20314779705SSam Leffler 		OS_REG_WRITE(ah, AR_RXCFG,
20414779705SSam Leffler 			OS_REG_READ(ah, AR_RXCFG) &~ AR_RXCFG_ZLFDMA);
20514779705SSam Leffler 	}
2063c3e9d33SSam Leffler 	if (AH_PRIVATE(ah)->ah_caps.halBssidMatchSupport) {
2073c3e9d33SSam Leffler 		if (bits & HAL_RX_FILTER_BSSID)
20882cadd5aSSam Leffler 			ahp->ah_miscMode |= AR_MISC_MODE_BSSID_MATCH_FORCE;
2093c3e9d33SSam Leffler 		else
21082cadd5aSSam Leffler 			ahp->ah_miscMode &= ~AR_MISC_MODE_BSSID_MATCH_FORCE;
211534f8ec8SAdrian Chadd 		OS_REG_WRITE(ah, AR_MISC_MODE, OS_REG_READ(ah, AR_MISC_MODE) | ahp->ah_miscMode);
2123c3e9d33SSam Leffler 	}
21314779705SSam Leffler }
21414779705SSam Leffler 
21514779705SSam Leffler /*
21614779705SSam Leffler  * Initialize RX descriptor, by clearing the status and setting
21714779705SSam Leffler  * the size (and any other flags).
21814779705SSam Leffler  */
21914779705SSam Leffler HAL_BOOL
ar5212SetupRxDesc(struct ath_hal * ah,struct ath_desc * ds,uint32_t size,u_int flags)22014779705SSam Leffler ar5212SetupRxDesc(struct ath_hal *ah, struct ath_desc *ds,
22114779705SSam Leffler 	uint32_t size, u_int flags)
22214779705SSam Leffler {
22314779705SSam Leffler 	struct ar5212_desc *ads = AR5212DESC(ds);
22414779705SSam Leffler 
22514779705SSam Leffler 	HALASSERT((size &~ AR_BufLen) == 0);
22614779705SSam Leffler 
22714779705SSam Leffler 	ads->ds_ctl0 = 0;
22814779705SSam Leffler 	ads->ds_ctl1 = size & AR_BufLen;
22914779705SSam Leffler 
23014779705SSam Leffler 	if (flags & HAL_RXDESC_INTREQ)
23114779705SSam Leffler 		ads->ds_ctl1 |= AR_RxInterReq;
23214779705SSam Leffler 	ads->ds_rxstatus0 = ads->ds_rxstatus1 = 0;
23314779705SSam Leffler 
23414779705SSam Leffler 	return AH_TRUE;
23514779705SSam Leffler }
23614779705SSam Leffler 
23714779705SSam Leffler /*
23814779705SSam Leffler  * Process an RX descriptor, and return the status to the caller.
23914779705SSam Leffler  * Copy some hardware specific items into the software portion
24014779705SSam Leffler  * of the descriptor.
24114779705SSam Leffler  *
24214779705SSam Leffler  * NB: the caller is responsible for validating the memory contents
24314779705SSam Leffler  *     of the descriptor (e.g. flushing any cached copy).
24414779705SSam Leffler  */
24514779705SSam Leffler HAL_STATUS
ar5212ProcRxDesc(struct ath_hal * ah,struct ath_desc * ds,uint32_t pa,struct ath_desc * nds,uint64_t tsf,struct ath_rx_status * rs)24614779705SSam Leffler ar5212ProcRxDesc(struct ath_hal *ah, struct ath_desc *ds,
24714779705SSam Leffler 	uint32_t pa, struct ath_desc *nds, uint64_t tsf,
24814779705SSam Leffler 	struct ath_rx_status *rs)
24914779705SSam Leffler {
25014779705SSam Leffler 	struct ar5212_desc *ads = AR5212DESC(ds);
25114779705SSam Leffler 	struct ar5212_desc *ands = AR5212DESC(nds);
25214779705SSam Leffler 
25314779705SSam Leffler 	if ((ads->ds_rxstatus1 & AR_Done) == 0)
25414779705SSam Leffler 		return HAL_EINPROGRESS;
25514779705SSam Leffler 	/*
25614779705SSam Leffler 	 * Given the use of a self-linked tail be very sure that the hw is
25714779705SSam Leffler 	 * done with this descriptor; the hw may have done this descriptor
25814779705SSam Leffler 	 * once and picked it up again...make sure the hw has moved on.
25914779705SSam Leffler 	 */
26014779705SSam Leffler 	if ((ands->ds_rxstatus1&AR_Done) == 0 && OS_REG_READ(ah, AR_RXDP) == pa)
26114779705SSam Leffler 		return HAL_EINPROGRESS;
26214779705SSam Leffler 
26314779705SSam Leffler 	rs->rs_datalen = ads->ds_rxstatus0 & AR_DataLen;
26414779705SSam Leffler 	rs->rs_tstamp = MS(ads->ds_rxstatus1, AR_RcvTimestamp);
26514779705SSam Leffler 	rs->rs_status = 0;
26614779705SSam Leffler 	rs->rs_rssi = MS(ads->ds_rxstatus0, AR_RcvSigStrength);
26714779705SSam Leffler 	/* discard invalid h/w rssi data */
26814779705SSam Leffler 	if (rs->rs_rssi == -128)
26914779705SSam Leffler 		rs->rs_rssi = 0;
27014779705SSam Leffler 	if (ads->ds_rxstatus1 & AR_KeyIdxValid)
27114779705SSam Leffler 		rs->rs_keyix = MS(ads->ds_rxstatus1, AR_KeyIdx);
27214779705SSam Leffler 	else
27314779705SSam Leffler 		rs->rs_keyix = HAL_RXKEYIX_INVALID;
274*b5e7ee47SAdrian Chadd 	if (ads->ds_rxstatus1 & AR_KeyCacheMiss)
275*b5e7ee47SAdrian Chadd 		rs->rs_status |= HAL_RXERR_KEYMISS;
27614779705SSam Leffler 	/* NB: caller expected to do rate table mapping */
27714779705SSam Leffler 	rs->rs_rate = MS(ads->ds_rxstatus0, AR_RcvRate);
27814779705SSam Leffler 	rs->rs_antenna  = MS(ads->ds_rxstatus0, AR_RcvAntenna);
27914779705SSam Leffler 	rs->rs_more = (ads->ds_rxstatus0 & AR_More) ? 1 : 0;
28014779705SSam Leffler 
2819c1b9975SAdrian Chadd 	/*
2829c1b9975SAdrian Chadd 	 * The AR5413 (at least) sometimes sets both AR_CRCErr and
2839c1b9975SAdrian Chadd 	 * AR_PHYErr when reporting radar pulses.  In this instance
2849c1b9975SAdrian Chadd 	 * set HAL_RXERR_PHY as well as HAL_RXERR_CRC and
2859c1b9975SAdrian Chadd 	 * let the driver layer figure out what to do.
2869c1b9975SAdrian Chadd 	 *
2879c1b9975SAdrian Chadd 	 * See PR kern/169362.
2889c1b9975SAdrian Chadd 	 */
28914779705SSam Leffler 	if ((ads->ds_rxstatus1 & AR_FrmRcvOK) == 0) {
29014779705SSam Leffler 		/*
29114779705SSam Leffler 		 * These four bits should not be set together.  The
29214779705SSam Leffler 		 * 5212 spec states a Michael error can only occur if
29314779705SSam Leffler 		 * DecryptCRCErr not set (and TKIP is used).  Experience
29414779705SSam Leffler 		 * indicates however that you can also get Michael errors
29514779705SSam Leffler 		 * when a CRC error is detected, but these are specious.
29614779705SSam Leffler 		 * Consequently we filter them out here so we don't
29714779705SSam Leffler 		 * confuse and/or complicate drivers.
29814779705SSam Leffler 		 */
2999c1b9975SAdrian Chadd 		if (ads->ds_rxstatus1 & AR_PHYErr) {
30014779705SSam Leffler 			u_int phyerr;
30114779705SSam Leffler 
30214779705SSam Leffler 			rs->rs_status |= HAL_RXERR_PHY;
30314779705SSam Leffler 			phyerr = MS(ads->ds_rxstatus1, AR_PHYErrCode);
30414779705SSam Leffler 			rs->rs_phyerr = phyerr;
30514779705SSam Leffler 			if (!AH5212(ah)->ah_hasHwPhyCounters &&
30614779705SSam Leffler 			    phyerr != HAL_PHYERR_RADAR)
30714779705SSam Leffler 				ar5212AniPhyErrReport(ah, rs);
3089c1b9975SAdrian Chadd 		}
3099c1b9975SAdrian Chadd 
3109c1b9975SAdrian Chadd 		if (ads->ds_rxstatus1 & AR_CRCErr)
3119c1b9975SAdrian Chadd 			rs->rs_status |= HAL_RXERR_CRC;
3129c1b9975SAdrian Chadd 		else if (ads->ds_rxstatus1 & AR_DecryptCRCErr)
31314779705SSam Leffler 			rs->rs_status |= HAL_RXERR_DECRYPT;
31414779705SSam Leffler 		else if (ads->ds_rxstatus1 & AR_MichaelErr)
31514779705SSam Leffler 			rs->rs_status |= HAL_RXERR_MIC;
31614779705SSam Leffler 	}
31714779705SSam Leffler 	return HAL_OK;
31814779705SSam Leffler }
319