summaryrefslogtreecommitdiffstats
path: root/carl9170fw/carlfw/include/wl.h
blob: dd5b0a88f3e4c7c376cf798391ac4e6a5e72df66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * carl9170 firmware - used by the ar9170 wireless device
 *
 * WLAN
 *
 * Copyright (c) 2000-2005 ZyDAS Technology Corporation
 * Copyright (c) 2007-2009 Atheros Communications, Inc.
 * Copyright	2009	Johannes Berg <johannes@sipsolutions.net>
 * Copyright 2009-2011	Christian Lamparter <chunkeey@googlemail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef __CARL9170FW_WLAN_H
#define __CARL9170FW_WLAN_H

#include "config.h"
#include "carl9170.h"
#include "io.h"

struct ieee80211_hdr;

static inline __inline void set_wlan_txq_dma_addr(const unsigned int q, const uint32_t v)
{
	set(AR9170_MAC_REG_DMA_TXQ_ADDR + (q << 3), v);
}

static inline __inline void set_wlan_txq_dma_curr_addr(const unsigned int q, const uint32_t v)
{
	set(AR9170_MAC_REG_DMA_TXQ_CURR_ADDR + (q << 3), v);
}

static inline __inline volatile struct dma_desc *get_wlan_txq_dma_addr(const unsigned int q)
{
	return getp(AR9170_MAC_REG_DMA_TXQ_ADDR + (q << 3));
}

static inline __inline volatile struct dma_desc *get_wlan_txq_addr(const unsigned int q)
{
	return getp(AR9170_MAC_REG_DMA_TXQ_CURR_ADDR + (q << 3));
}

static inline __inline volatile struct dma_desc *get_wlan_txq_last_addr(const unsigned int q)
{
	return getp(AR9170_MAC_REG_DMA_TXQ_LAST_ADDR + (q << 2));
}

static inline __inline void wlan_trigger(const uint32_t queue_bit)
{
	set(AR9170_MAC_REG_DMA_TRIGGER, queue_bit);
}

static inline __inline uint8_t ar9170_get_rx_macstatus_status(struct dma_desc *desc)
{
	return *((uint8_t *) DESC_PAYLOAD_OFF(desc->lastAddr,
		(unsigned int) desc->lastAddr->dataSize - 1));
}

static inline __inline uint8_t ar9170_get_rx_macstatus_error(struct dma_desc *desc)
{
	unsigned int offset;

	if (desc->lastAddr->dataSize == 1) {
		while (desc->lastAddr != desc->nextAddr)
			desc = desc->nextAddr;

		offset = (unsigned int) (desc->dataSize - 1);
	} else {
		desc = desc->lastAddr;
		offset = desc->dataSize -
			(sizeof(struct ar9170_rx_macstatus) -
			 offsetof(struct ar9170_rx_macstatus, error));
	}

	return *((uint8_t *) DESC_PAYLOAD_OFF(desc, offset));
}

static inline __inline struct ieee80211_hdr *ar9170_get_rx_i3e(struct dma_desc *desc)
{
	if (!((ar9170_get_rx_macstatus_status(desc) &
		AR9170_RX_STATUS_MPDU) & AR9170_RX_STATUS_MPDU_LAST)) {
		return (void *)(DESC_PAYLOAD_OFF(desc,
			offsetof(struct ar9170_rx_frame_head, i3e)));
	} else {
		return (void *)(DESC_PAYLOAD_OFF(desc,
			offsetof(struct ar9170_rx_frame_tail, i3e)));
	}
}

static inline __inline struct ar9170_rx_head *ar9170_get_rx_head(struct dma_desc *desc)
{
	if (!((ar9170_get_rx_macstatus_status(desc) &
		AR9170_RX_STATUS_MPDU) & AR9170_RX_STATUS_MPDU_LAST)) {
		return (void *)((uint8_t *)DESC_PAYLOAD(desc) +
			offsetof(struct ar9170_rx_frame_head, phy_head));
	} else {
		return (void *) NULL;
	}
}

static inline __inline uint32_t ar9170_rx_to_phy(struct dma_desc *rx)
{
	struct ar9170_tx_hw_phy_control phy;
	struct ar9170_rx_head *head;
	uint8_t mac_status;

	phy.set = 0;

	head = ar9170_get_rx_head(rx);
	if (!head)
		return le32_to_cpu(phy.set);

	mac_status = ar9170_get_rx_macstatus_status(rx);

	phy.modulation = mac_status & AR9170_RX_STATUS_MODULATION;
	phy.chains = AR9170_TX_PHY_TXCHAIN_1;

	switch (phy.modulation) {
	case AR9170_RX_STATUS_MODULATION_CCK:
		if (mac_status & AR9170_RX_STATUS_SHORT_PREAMBLE)
			phy.preamble = 1;

		switch (head->plcp[0]) {
		case AR9170_RX_PHY_RATE_CCK_2M:
			phy.mcs = AR9170_TX_PHY_RATE_CCK_2M;
			break;

		case AR9170_RX_PHY_RATE_CCK_5M:
			phy.mcs = AR9170_TX_PHY_RATE_CCK_5M;
			break;

		case AR9170_RX_PHY_RATE_CCK_11M:
			phy.mcs = AR9170_TX_PHY_RATE_CCK_11M;
			break;

		case AR9170_RX_PHY_RATE_CCK_1M:
		default:
			phy.mcs = AR9170_TX_PHY_RATE_CCK_1M;
			break;

		}
		break;

	case AR9170_RX_STATUS_MODULATION_DUPOFDM:
	case AR9170_RX_STATUS_MODULATION_OFDM:
		phy.mcs = head->plcp[0] & 0xf;
		break;

	case AR9170_RX_STATUS_MODULATION_HT:
		if (head->plcp[3] & 0x80)
			phy.bandwidth = 2;

		if (head->plcp[6] & 0x80)
			phy.short_gi = 1;

		/* TODO: Enable both chains for MCS > 7 */
		phy.mcs = head->plcp[6] & 0x7;
		break;
	}

	return le32_to_cpu(phy.set);
}

static inline __inline unsigned int ar9170_get_rx_mpdu_len(struct dma_desc *desc)
{
	/*
	 * WARNING: you have to check the error bits in macstatus first!
	 */

	unsigned int mpdu_len = desc->totalLen;

	mpdu_len -= sizeof(struct ar9170_rx_macstatus);

	switch (ar9170_get_rx_macstatus_status(desc) & AR9170_RX_STATUS_MPDU) {
	case AR9170_RX_STATUS_MPDU_LAST:
		mpdu_len -= sizeof(struct ar9170_rx_phystatus);
		break;

	case AR9170_RX_STATUS_MPDU_SINGLE:
		mpdu_len -= sizeof(struct ar9170_rx_phystatus);

	case AR9170_RX_STATUS_MPDU_FIRST:
		mpdu_len -= sizeof(struct ar9170_rx_head);
		break;

	case AR9170_RX_STATUS_MPDU_MIDDLE:
	default:
		break;
	}

	return mpdu_len;
}

static inline __inline bool ar9170_tx_length_check(const uint16_t len)
{
	return len > (sizeof(struct carl9170_tx_superframe) + 24 +
			 FCS_LEN);
}

static inline __inline struct carl9170_tx_superframe *get_super(struct dma_desc *desc)
{
	return container_of(DESC_PAYLOAD(desc), struct carl9170_tx_superframe,
			    f);
}

static inline __inline struct carl9170_tx_superframe *__get_super(struct dma_desc *desc)
{
	return DESC_PAYLOAD(desc);
}

static inline __inline void hide_super(struct dma_desc *desc)
{
	desc->dataAddr = (uint8_t *)
		(((unsigned long)(DESC_PAYLOAD(desc)) +
		offsetof(struct carl9170_tx_superframe, f)));

	desc->dataSize -= sizeof(struct carl9170_tx_superdesc);
	desc->totalLen -= sizeof(struct carl9170_tx_superdesc);
}

static inline __inline void unhide_super(struct dma_desc *desc)
{
	desc->dataAddr = (uint8_t *) get_super(desc);
	desc->dataSize += sizeof(struct carl9170_tx_superdesc);
	desc->totalLen += sizeof(struct carl9170_tx_superdesc);
}

static inline __inline __hot void read_tsf(uint32_t *tsf)
{
	/*
	 * "According to the [hardware] documentation:
	 *  > when TSF_LOW is read, TSF_HI is automatically concurrently
	 *  > copied into a temporary register so that an immediate read
	 *  > of TSF_HI will get the value that was present when TSF_LOW
	 *  > was read. "
	 *
	 * (David H. Lynch Jr. - mail from 2010-05-22)
	 * http://permalink.gmane.org/gmane.linux.kernel.wireless.general/51249
	 */

	tsf[0] = get(AR9170_MAC_REG_TSF_L);
	tsf[1] = get(AR9170_MAC_REG_TSF_H);
}

/* This function will only work on uint32_t-aligned pointers! */
static inline bool compare_ether_address(const void *_d0, const void *_d1)
{
	const uint32_t *d0 = _d0;
	const uint32_t *d1 = _d1;

	/* BUG_ON((unsigned long)d0 & 3 || (unsigned long)d1 & 3)) */
	return !((d0[0] ^ d1[0]) | (unsigned short)(d0[1] ^ d1[1]));
}

void wlan_tx(struct dma_desc *desc);
void wlan_tx_fw(struct carl9170_tx_superdesc *super, fw_desc_callback_t cb);
void wlan_timer(void);
void handle_wlan(void);

void wlan_cab_flush_queue(const unsigned int vif);
void wlan_modify_beacon(const unsigned int vif,
			const unsigned int bcn_addr,
			const unsigned int bcn_len);

void wlan_tx_complete(struct carl9170_tx_superframe *super, bool txs);
void wlan_prepare_wol(void);

static inline void __check_wlantx(void)
{
	BUILD_BUG_ON(CARL9170_TX_SUPERDESC_LEN & 3);
	BUILD_BUG_ON(sizeof(struct carl9170_tx_superdesc) != CARL9170_TX_SUPERDESC_LEN);
	BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) != CARL9170_TX_SUPERDESC_LEN);
	BUILD_BUG_ON(sizeof(struct _carl9170_tx_superframe) != CARL9170_TX_SUPERFRAME_LEN);
	BUILD_BUG_ON((offsetof(struct carl9170_tx_superframe, f) & 3) != 0);
	BUILD_BUG_ON(offsetof(struct _carl9170_tx_superframe, f) !=
		     (offsetof(struct _carl9170_tx_superframe, f)));
	BUILD_BUG_ON(sizeof(struct ar9170_tx_hwdesc) != AR9170_TX_HWDESC_LEN);
	BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) != AR9170_TX_HWDESC_LEN);
	BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != AR9170_RX_HEAD_LEN);
	BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus) != AR9170_RX_PHYSTATUS_LEN);
	BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus) != AR9170_RX_MACSTATUS_LEN);
}

#endif /* __CARL9170FW_WLAN_H */