Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 May 2016 14:17:58 +0000 (UTC)
From:      Andriy Voskoboinyk <avos@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r300744 - head/sys/dev/rtwn
Message-ID:  <201605261417.u4QEHwPg069936@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: avos
Date: Thu May 26 14:17:57 2016
New Revision: 300744
URL: https://svnweb.freebsd.org/changeset/base/300744

Log:
  rtwn: replace hardcoded rate indices with their names (similar to r289758).
  
  - Use macros to define rate indices; don't hardcode them in code.
  - Add method for 'rate' -> 'rate index' conversion
  (and array for the opposite).
  - Determine if rate is CCK / OFDM via appropriate macro.
  
  Reviewed by:	adrian
  Differential Revision:	https://reviews.freebsd.org/D4837

Modified:
  head/sys/dev/rtwn/if_rtwn.c
  head/sys/dev/rtwn/if_rtwnreg.h

Modified: head/sys/dev/rtwn/if_rtwn.c
==============================================================================
--- head/sys/dev/rtwn/if_rtwn.c	Thu May 26 14:13:09 2016	(r300743)
+++ head/sys/dev/rtwn/if_rtwn.c	Thu May 26 14:17:57 2016	(r300744)
@@ -1044,40 +1044,56 @@ rtwn_read_rom(struct rtwn_softc *sc)
 	IEEE80211_ADDR_COPY(sc->sc_ic.ic_macaddr, rom->macaddr);
 }
 
+static __inline uint8_t
+rate2ridx(uint8_t rate)
+{
+	switch (rate) {
+	case 12:	return 4;
+	case 18:	return 5;
+	case 24:	return 6;
+	case 36:	return 7;
+	case 48:	return 8;
+	case 72:	return 9;
+	case 96:	return 10;
+	case 108:	return 11;
+	case 2:		return 0;
+	case 4:		return 1;
+	case 11:	return 2;
+	case 22:	return 3;
+	default:	return RTWN_RIDX_UNKNOWN;
+	}
+}
+
 /*
  * Initialize rate adaptation in firmware.
  */
 static int
 rtwn_ra_init(struct rtwn_softc *sc)
 {
-	static const uint8_t map[] =
-	    { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
 	struct ieee80211com *ic = &sc->sc_ic;
 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
 	struct ieee80211_node *ni = ieee80211_ref_node(vap->iv_bss);
 	struct ieee80211_rateset *rs = &ni->ni_rates;
 	struct r92c_fw_cmd_macid_cfg cmd;
 	uint32_t rates, basicrates;
-	uint8_t mode;
-	int maxrate, maxbasicrate, error, i, j;
+	uint8_t maxrate, maxbasicrate, mode, ridx;
+	int error, i;
 
 	/* Get normal and basic rates mask. */
 	rates = basicrates = 0;
 	maxrate = maxbasicrate = 0;
 	for (i = 0; i < rs->rs_nrates; i++) {
 		/* Convert 802.11 rate to HW rate index. */
-		for (j = 0; j < nitems(map); j++)
-			if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == map[j])
-				break;
-		if (j == nitems(map))	/* Unknown rate, skip. */
+		ridx = rate2ridx(IEEE80211_RV(rs->rs_rates[i]));
+		if (ridx == RTWN_RIDX_UNKNOWN)	/* Unknown rate, skip. */
 			continue;
-		rates |= 1 << j;
-		if (j > maxrate)
-			maxrate = j;
+		rates |= 1 << ridx;
+		if (ridx > maxrate)
+			maxrate = ridx;
 		if (rs->rs_rates[i] & IEEE80211_RATE_BASIC) {
-			basicrates |= 1 << j;
-			if (j > maxbasicrate)
-				maxbasicrate = j;
+			basicrates |= 1 << ridx;
+			if (ridx > maxbasicrate)
+				maxbasicrate = ridx;
 		}
 	}
 	if (ic->ic_curmode == IEEE80211_MODE_11B)
@@ -1358,7 +1374,7 @@ rtwn_update_avgrssi(struct rtwn_softc *s
 		pwdb = 100;
 	else
 		pwdb = 100 + rssi;
-	if (rate <= 3) {
+	if (RTWN_RATE_IS_CCK(rate)) {
 		/* CCK gain is smaller than OFDM/MCS gain. */
 		pwdb += 6;
 		if (pwdb > 100)
@@ -1390,7 +1406,7 @@ rtwn_get_rssi(struct rtwn_softc *sc, int
 	uint8_t rpt;
 	int8_t rssi;
 
-	if (rate <= 3) {
+	if (RTWN_RATE_IS_CCK(rate)) {
 		cck = (struct r92c_rx_cck *)physt;
 		if (sc->sc_flags & RTWN_FLAG_CCK_HIPWR) {
 			rpt = (cck->agc_rpt >> 5) & 0x3;
@@ -1503,22 +1519,7 @@ rtwn_rx_frame(struct rtwn_softc *sc, str
 
 		tap->wr_flags = 0;
 		if (!(rxdw3 & R92C_RXDW3_HT)) {
-			switch (rate) {
-			/* CCK. */
-			case  0: tap->wr_rate =   2; break;
-			case  1: tap->wr_rate =   4; break;
-			case  2: tap->wr_rate =  11; break;
-			case  3: tap->wr_rate =  22; break;
-			/* OFDM. */
-			case  4: tap->wr_rate =  12; break;
-			case  5: tap->wr_rate =  18; break;
-			case  6: tap->wr_rate =  24; break;
-			case  7: tap->wr_rate =  36; break;
-			case  8: tap->wr_rate =  48; break;
-			case  9: tap->wr_rate =  72; break;
-			case 10: tap->wr_rate =  96; break;
-			case 11: tap->wr_rate = 108; break;
-			}
+			tap->wr_rate = ridx2rate[rate];
 		} else if (rate >= 12) {	/* MCS0~15. */
 			/* Bit 7 set means HT MCS instead of rate. */
 			tap->wr_rate = 0x80 | (rate - 12);
@@ -1644,10 +1645,12 @@ rtwn_tx(struct rtwn_softc *sc, struct mb
 		/* XXX TODO: implement rate control */
 
 		/* Send RTS at OFDM24. */
-		txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE, 8));
+		txd->txdw4 |= htole32(SM(R92C_TXDW4_RTSRATE,
+		    RTWN_RIDX_OFDM24));
 		txd->txdw5 |= htole32(SM(R92C_TXDW5_RTSRATE_FBLIMIT, 0xf));
 		/* Send data at OFDM54. */
-		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 11));
+		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE,
+		    RTWN_RIDX_OFDM54));
 		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE_FBLIMIT, 0x1f));
 
 	} else {
@@ -1658,7 +1661,7 @@ rtwn_tx(struct rtwn_softc *sc, struct mb
 
 		/* Force CCK1. */
 		txd->txdw4 |= htole32(R92C_TXDW4_DRVRATE);
-		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, 0));
+		txd->txdw5 |= htole32(SM(R92C_TXDW5_DATARATE, RTWN_RIDX_CCK1));
 	}
 	/* Set sequence number (already little endian). */
 	txd->txdseq = htole16(M_SEQNO_GET(m) % IEEE80211_SEQ_RANGE);
@@ -2577,10 +2580,10 @@ rtwn_get_txpower(struct rtwn_softc *sc, 
 
 	memset(power, 0, RTWN_RIDX_COUNT * sizeof(power[0]));
 	if (sc->regulatory == 0) {
-		for (ridx = 0; ridx <= 3; ridx++)
+		for (ridx = RTWN_RIDX_CCK1; ridx <= RTWN_RIDX_CCK11; ridx++)
 			power[ridx] = base->pwr[0][ridx];
 	}
-	for (ridx = 4; ridx < RTWN_RIDX_COUNT; ridx++) {
+	for (ridx = RTWN_RIDX_OFDM6; ridx < RTWN_RIDX_COUNT; ridx++) {
 		if (sc->regulatory == 3) {
 			power[ridx] = base->pwr[0][ridx];
 			/* Apply vendor limits. */
@@ -2600,7 +2603,7 @@ rtwn_get_txpower(struct rtwn_softc *sc, 
 
 	/* Compute per-CCK rate Tx power. */
 	cckpow = rom->cck_tx_pwr[chain][group];
-	for (ridx = 0; ridx <= 3; ridx++) {
+	for (ridx = RTWN_RIDX_CCK1; ridx <= RTWN_RIDX_CCK11; ridx++) {
 		power[ridx] += cckpow;
 		if (power[ridx] > R92C_MAX_TX_PWR)
 			power[ridx] = R92C_MAX_TX_PWR;
@@ -2618,7 +2621,7 @@ rtwn_get_txpower(struct rtwn_softc *sc, 
 	diff = rom->ofdm_tx_pwr_diff[group];
 	diff = (diff >> (chain * 4)) & 0xf;
 	ofdmpow = htpow + diff;	/* HT->OFDM correction. */
-	for (ridx = 4; ridx <= 11; ridx++) {
+	for (ridx = RTWN_RIDX_OFDM6; ridx <= RTWN_RIDX_OFDM54; ridx++) {
 		power[ridx] += ofdmpow;
 		if (power[ridx] > R92C_MAX_TX_PWR)
 			power[ridx] = R92C_MAX_TX_PWR;
@@ -2630,7 +2633,7 @@ rtwn_get_txpower(struct rtwn_softc *sc, 
 		diff = (diff >> (chain * 4)) & 0xf;
 		htpow += diff;	/* HT40->HT20 correction. */
 	}
-	for (ridx = 12; ridx <= 27; ridx++) {
+	for (ridx = RTWN_RIDX_MCS0; ridx <= RTWN_RIDX_MCS15; ridx++) {
 		power[ridx] += htpow;
 		if (power[ridx] > R92C_MAX_TX_PWR)
 			power[ridx] = R92C_MAX_TX_PWR;
@@ -2639,7 +2642,7 @@ rtwn_get_txpower(struct rtwn_softc *sc, 
 	if (sc->sc_debug >= 4) {
 		/* Dump per-rate Tx power values. */
 		printf("Tx power for chain %d:\n", chain);
-		for (ridx = 0; ridx < RTWN_RIDX_COUNT; ridx++)
+		for (ridx = RTWN_RIDX_CCK1; ridx < RTWN_RIDX_COUNT; ridx++)
 			printf("Rate %d = %u\n", ridx, power[ridx]);
 	}
 #endif

Modified: head/sys/dev/rtwn/if_rtwnreg.h
==============================================================================
--- head/sys/dev/rtwn/if_rtwnreg.h	Thu May 26 14:13:09 2016	(r300743)
+++ head/sys/dev/rtwn/if_rtwnreg.h	Thu May 26 14:17:57 2016	(r300744)
@@ -1158,6 +1158,25 @@ struct r92c_tx_desc {
 	uint32_t	reserved[4];
 } __packed __attribute__((aligned(4)));
 
+static const uint8_t ridx2rate[] =
+	{ 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
+
+/* HW rate indices. */
+#define RTWN_RIDX_CCK1		0
+#define RTWN_RIDX_CCK11		3
+#define RTWN_RIDX_OFDM6		4
+#define RTWN_RIDX_OFDM24	8
+#define RTWN_RIDX_OFDM54	11
+#define RTWN_RIDX_MCS0		12
+#define RTWN_RIDX_MCS15		27
+
+#define RTWN_RIDX_COUNT		28
+#define RTWN_RIDX_UNKNOWN	(uint8_t)-1
+
+#define RTWN_RATE_IS_CCK(rate)	((rate) <= RTWN_RIDX_CCK11)
+#define RTWN_RATE_IS_OFDM(rate)	((rate) >= RTWN_RIDX_OFDM6 && \
+				 (rate) <= RTWN_RIDX_OFDM54)
+
 
 /*
  * Driver definitions.
@@ -1184,8 +1203,6 @@ struct r92c_tx_desc {
 #define RTWN_RXBUFSZ	(16 * 1024)
 #define RTWN_TXBUFSZ	(sizeof(struct r92c_tx_desc) + IEEE80211_MAX_LEN)
 
-#define RTWN_RIDX_COUNT	28
-
 #define RTWN_TX_TIMEOUT	5000	/* ms */
 
 #define RTWN_LED_LINK	0



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201605261417.u4QEHwPg069936>