desc.c 17.7 KB
Newer Older
N
Nick Kossifidis 已提交
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
/*
 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
 * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

/******************************\
 Hardware Descriptor Functions
\******************************/

#include "ath5k.h"
#include "reg.h"
#include "debug.h"

28 29 30 31

/************************\
* TX Control descriptors *
\************************/
N
Nick Kossifidis 已提交
32 33 34 35 36 37

/*
 * Initialize the 2-word tx control descriptor on 5210/5211
 */
static int
ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
38 39
	unsigned int pkt_len, unsigned int hdr_len, int padsize,
	enum ath5k_pkt_type type,
N
Nick Kossifidis 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
	unsigned int tx_power, unsigned int tx_rate0, unsigned int tx_tries0,
	unsigned int key_index, unsigned int antenna_mode, unsigned int flags,
	unsigned int rtscts_rate, unsigned int rtscts_duration)
{
	u32 frame_type;
	struct ath5k_hw_2w_tx_ctl *tx_ctl;
	unsigned int frame_len;

	tx_ctl = &desc->ud.ds_tx5210.tx_ctl;

	/*
	 * Validate input
	 * - Zero retries don't make sense.
L
Lucas De Marchi 已提交
53
	 * - A zero rate will put the HW into a mode where it continuously sends
N
Nick Kossifidis 已提交
54 55 56
	 *   noise on the channel, so it is important to avoid this.
	 */
	if (unlikely(tx_tries0 == 0)) {
57
		ATH5K_ERR(ah, "zero retries\n");
N
Nick Kossifidis 已提交
58 59 60 61
		WARN_ON(1);
		return -EINVAL;
	}
	if (unlikely(tx_rate0 == 0)) {
62
		ATH5K_ERR(ah, "zero rate\n");
N
Nick Kossifidis 已提交
63 64 65 66 67 68 69 70 71 72 73 74
		WARN_ON(1);
		return -EINVAL;
	}

	/* Clear descriptor */
	memset(&desc->ud.ds_tx5210, 0, sizeof(struct ath5k_hw_5210_tx_desc));

	/* Setup control descriptor */

	/* Verify and set frame length */

	/* remove padding we might have added before */
75
	frame_len = pkt_len - padsize + FCS_LEN;
N
Nick Kossifidis 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

	if (frame_len & ~AR5K_2W_TX_DESC_CTL0_FRAME_LEN)
		return -EINVAL;

	tx_ctl->tx_control_0 = frame_len & AR5K_2W_TX_DESC_CTL0_FRAME_LEN;

	/* Verify and set buffer length */

	/* NB: beacon's BufLen must be a multiple of 4 bytes */
	if (type == AR5K_PKT_TYPE_BEACON)
		pkt_len = roundup(pkt_len, 4);

	if (pkt_len & ~AR5K_2W_TX_DESC_CTL1_BUF_LEN)
		return -EINVAL;

	tx_ctl->tx_control_1 = pkt_len & AR5K_2W_TX_DESC_CTL1_BUF_LEN;

	/*
94
	 * Verify and set header length (only 5210)
N
Nick Kossifidis 已提交
95 96
	 */
	if (ah->ah_version == AR5K_AR5210) {
97
		if (hdr_len & ~AR5K_2W_TX_DESC_CTL0_HEADER_LEN_5210)
N
Nick Kossifidis 已提交
98 99
			return -EINVAL;
		tx_ctl->tx_control_0 |=
100
			AR5K_REG_SM(hdr_len, AR5K_2W_TX_DESC_CTL0_HEADER_LEN_5210);
N
Nick Kossifidis 已提交
101 102
	}

103
	/*Differences between 5210-5211*/
N
Nick Kossifidis 已提交
104 105 106 107 108
	if (ah->ah_version == AR5K_AR5210) {
		switch (type) {
		case AR5K_PKT_TYPE_BEACON:
		case AR5K_PKT_TYPE_PROBE_RESP:
			frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_NO_DELAY;
109
			break;
N
Nick Kossifidis 已提交
110 111
		case AR5K_PKT_TYPE_PIFS:
			frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_PIFS;
112
			break;
N
Nick Kossifidis 已提交
113
		default:
114
			frame_type = type;
115
			break;
N
Nick Kossifidis 已提交
116 117 118
		}

		tx_ctl->tx_control_0 |=
119
		AR5K_REG_SM(frame_type, AR5K_2W_TX_DESC_CTL0_FRAME_TYPE_5210) |
N
Nick Kossifidis 已提交
120 121 122 123 124 125 126 127
		AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE);

	} else {
		tx_ctl->tx_control_0 |=
			AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE) |
			AR5K_REG_SM(antenna_mode,
				AR5K_2W_TX_DESC_CTL0_ANT_MODE_XMIT);
		tx_ctl->tx_control_1 |=
128
			AR5K_REG_SM(type, AR5K_2W_TX_DESC_CTL1_FRAME_TYPE_5211);
N
Nick Kossifidis 已提交
129
	}
130

N
Nick Kossifidis 已提交
131 132 133 134 135
#define _TX_FLAGS(_c, _flag)					\
	if (flags & AR5K_TXDESC_##_flag) {			\
		tx_ctl->tx_control_##_c |=			\
			AR5K_2W_TX_DESC_CTL##_c##_##_flag;	\
	}
136 137 138 139 140
#define _TX_FLAGS_5211(_c, _flag)					\
	if (flags & AR5K_TXDESC_##_flag) {				\
		tx_ctl->tx_control_##_c |=				\
			AR5K_2W_TX_DESC_CTL##_c##_##_flag##_5211;	\
	}
N
Nick Kossifidis 已提交
141 142 143
	_TX_FLAGS(0, CLRDMASK);
	_TX_FLAGS(0, INTREQ);
	_TX_FLAGS(0, RTSENA);
144 145 146 147 148

	if (ah->ah_version == AR5K_AR5211) {
		_TX_FLAGS_5211(0, VEOL);
		_TX_FLAGS_5211(1, NOACK);
	}
N
Nick Kossifidis 已提交
149 150

#undef _TX_FLAGS
151
#undef _TX_FLAGS_5211
N
Nick Kossifidis 已提交
152 153 154 155 156 157 158 159 160

	/*
	 * WEP crap
	 */
	if (key_index != AR5K_TXKEYIX_INVALID) {
		tx_ctl->tx_control_0 |=
			AR5K_2W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
		tx_ctl->tx_control_1 |=
			AR5K_REG_SM(key_index,
161
			AR5K_2W_TX_DESC_CTL1_ENC_KEY_IDX);
N
Nick Kossifidis 已提交
162 163 164 165 166 167 168 169
	}

	/*
	 * RTS/CTS Duration [5210 ?]
	 */
	if ((ah->ah_version == AR5K_AR5210) &&
			(flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)))
		tx_ctl->tx_control_1 |= rtscts_duration &
170
				AR5K_2W_TX_DESC_CTL1_RTS_DURATION_5210;
N
Nick Kossifidis 已提交
171 172 173 174 175 176 177 178 179

	return 0;
}

/*
 * Initialize the 4-word tx control descriptor on 5212
 */
static int ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *ah,
	struct ath5k_desc *desc, unsigned int pkt_len, unsigned int hdr_len,
180
	int padsize,
N
Nick Kossifidis 已提交
181 182 183 184 185 186 187 188 189
	enum ath5k_pkt_type type, unsigned int tx_power, unsigned int tx_rate0,
	unsigned int tx_tries0, unsigned int key_index,
	unsigned int antenna_mode, unsigned int flags,
	unsigned int rtscts_rate,
	unsigned int rtscts_duration)
{
	struct ath5k_hw_4w_tx_ctl *tx_ctl;
	unsigned int frame_len;

190 191 192 193
	/*
	 * Use local variables for these to reduce load/store access on
	 * uncached memory
	 */
194
	u32 txctl0 = 0, txctl1 = 0, txctl2 = 0, txctl3 = 0;
N
Nick Kossifidis 已提交
195 196 197 198 199 200

	tx_ctl = &desc->ud.ds_tx5212.tx_ctl;

	/*
	 * Validate input
	 * - Zero retries don't make sense.
L
Lucas De Marchi 已提交
201
	 * - A zero rate will put the HW into a mode where it continuously sends
N
Nick Kossifidis 已提交
202 203 204
	 *   noise on the channel, so it is important to avoid this.
	 */
	if (unlikely(tx_tries0 == 0)) {
205
		ATH5K_ERR(ah, "zero retries\n");
N
Nick Kossifidis 已提交
206 207 208 209
		WARN_ON(1);
		return -EINVAL;
	}
	if (unlikely(tx_rate0 == 0)) {
210
		ATH5K_ERR(ah, "zero rate\n");
N
Nick Kossifidis 已提交
211 212 213 214
		WARN_ON(1);
		return -EINVAL;
	}

215 216 217 218
	tx_power += ah->ah_txpower.txp_offset;
	if (tx_power > AR5K_TUNE_MAX_TXPOWER)
		tx_power = AR5K_TUNE_MAX_TXPOWER;

219
	/* Clear descriptor status area */
220 221
	memset(&desc->ud.ds_tx5212.tx_stat, 0,
	       sizeof(desc->ud.ds_tx5212.tx_stat));
N
Nick Kossifidis 已提交
222 223 224 225 226 227

	/* Setup control descriptor */

	/* Verify and set frame length */

	/* remove padding we might have added before */
228
	frame_len = pkt_len - padsize + FCS_LEN;
N
Nick Kossifidis 已提交
229 230 231 232

	if (frame_len & ~AR5K_4W_TX_DESC_CTL0_FRAME_LEN)
		return -EINVAL;

233
	txctl0 = frame_len & AR5K_4W_TX_DESC_CTL0_FRAME_LEN;
N
Nick Kossifidis 已提交
234 235 236 237 238 239 240 241 242 243

	/* Verify and set buffer length */

	/* NB: beacon's BufLen must be a multiple of 4 bytes */
	if (type == AR5K_PKT_TYPE_BEACON)
		pkt_len = roundup(pkt_len, 4);

	if (pkt_len & ~AR5K_4W_TX_DESC_CTL1_BUF_LEN)
		return -EINVAL;

244
	txctl1 = pkt_len & AR5K_4W_TX_DESC_CTL1_BUF_LEN;
N
Nick Kossifidis 已提交
245

246 247 248 249 250
	txctl0 |= AR5K_REG_SM(tx_power, AR5K_4W_TX_DESC_CTL0_XMIT_POWER) |
		  AR5K_REG_SM(antenna_mode, AR5K_4W_TX_DESC_CTL0_ANT_MODE_XMIT);
	txctl1 |= AR5K_REG_SM(type, AR5K_4W_TX_DESC_CTL1_FRAME_TYPE);
	txctl2 = AR5K_REG_SM(tx_tries0, AR5K_4W_TX_DESC_CTL2_XMIT_TRIES0);
	txctl3 = tx_rate0 & AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
N
Nick Kossifidis 已提交
251 252 253

#define _TX_FLAGS(_c, _flag)					\
	if (flags & AR5K_TXDESC_##_flag) {			\
254
		txctl##_c |= AR5K_4W_TX_DESC_CTL##_c##_##_flag;	\
N
Nick Kossifidis 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	}

	_TX_FLAGS(0, CLRDMASK);
	_TX_FLAGS(0, VEOL);
	_TX_FLAGS(0, INTREQ);
	_TX_FLAGS(0, RTSENA);
	_TX_FLAGS(0, CTSENA);
	_TX_FLAGS(1, NOACK);

#undef _TX_FLAGS

	/*
	 * WEP crap
	 */
	if (key_index != AR5K_TXKEYIX_INVALID) {
270 271
		txctl0 |= AR5K_4W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
		txctl1 |= AR5K_REG_SM(key_index,
272
				AR5K_4W_TX_DESC_CTL1_ENCRYPT_KEY_IDX);
N
Nick Kossifidis 已提交
273 274 275 276 277 278 279 280 281
	}

	/*
	 * RTS/CTS
	 */
	if (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)) {
		if ((flags & AR5K_TXDESC_RTSENA) &&
				(flags & AR5K_TXDESC_CTSENA))
			return -EINVAL;
282 283
		txctl2 |= rtscts_duration & AR5K_4W_TX_DESC_CTL2_RTS_DURATION;
		txctl3 |= AR5K_REG_SM(rtscts_rate,
N
Nick Kossifidis 已提交
284 285 286
				AR5K_4W_TX_DESC_CTL3_RTS_CTS_RATE);
	}

287 288 289 290 291
	tx_ctl->tx_control_0 = txctl0;
	tx_ctl->tx_control_1 = txctl1;
	tx_ctl->tx_control_2 = txctl2;
	tx_ctl->tx_control_3 = txctl3;

N
Nick Kossifidis 已提交
292 293 294 295 296 297
	return 0;
}

/*
 * Initialize a 4-word multi rate retry tx control descriptor on 5212
 */
298
int
N
Nick Kossifidis 已提交
299 300 301 302 303 304
ath5k_hw_setup_mrr_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
	unsigned int tx_rate1, u_int tx_tries1, u_int tx_rate2,
	u_int tx_tries2, unsigned int tx_rate3, u_int tx_tries3)
{
	struct ath5k_hw_4w_tx_ctl *tx_ctl;

305 306 307 308
	/* no mrr support for cards older than 5212 */
	if (ah->ah_version < AR5K_AR5212)
		return 0;

N
Nick Kossifidis 已提交
309 310 311
	/*
	 * Rates can be 0 as long as the retry count is 0 too.
	 * A zero rate and nonzero retry count will put the HW into a mode where
L
Lucas De Marchi 已提交
312
	 * it continuously sends noise on the channel, so it is important to
N
Nick Kossifidis 已提交
313 314 315 316 317
	 * avoid this.
	 */
	if (unlikely((tx_rate1 == 0 && tx_tries1 != 0) ||
		     (tx_rate2 == 0 && tx_tries2 != 0) ||
		     (tx_rate3 == 0 && tx_tries3 != 0))) {
318
		ATH5K_ERR(ah, "zero rate\n");
N
Nick Kossifidis 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
		WARN_ON(1);
		return -EINVAL;
	}

	if (ah->ah_version == AR5K_AR5212) {
		tx_ctl = &desc->ud.ds_tx5212.tx_ctl;

#define _XTX_TRIES(_n)							\
	if (tx_tries##_n) {						\
		tx_ctl->tx_control_2 |=					\
		    AR5K_REG_SM(tx_tries##_n,				\
		    AR5K_4W_TX_DESC_CTL2_XMIT_TRIES##_n);		\
		tx_ctl->tx_control_3 |=					\
		    AR5K_REG_SM(tx_rate##_n,				\
		    AR5K_4W_TX_DESC_CTL3_XMIT_RATE##_n);		\
	}

		_XTX_TRIES(1);
		_XTX_TRIES(2);
		_XTX_TRIES(3);

#undef _XTX_TRIES

		return 1;
	}

	return 0;
}

348 349 350 351 352

/***********************\
* TX Status descriptors *
\***********************/

N
Nick Kossifidis 已提交
353
/*
L
Lucas De Marchi 已提交
354
 * Process the tx status descriptor on 5210/5211
N
Nick Kossifidis 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
 */
static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *ah,
		struct ath5k_desc *desc, struct ath5k_tx_status *ts)
{
	struct ath5k_hw_2w_tx_ctl *tx_ctl;
	struct ath5k_hw_tx_status *tx_status;

	tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
	tx_status = &desc->ud.ds_tx5210.tx_stat;

	/* No frame has been send or error */
	if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
		return -EINPROGRESS;

	/*
	 * Get descriptor status
	 */
	ts->ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
		AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
	ts->ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
		AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
376
	ts->ts_final_retry = AR5K_REG_MS(tx_status->tx_status_0,
N
Nick Kossifidis 已提交
377 378 379 380 381 382 383 384
		AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
	/*TODO: ts->ts_virtcol + test*/
	ts->ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
		AR5K_DESC_TX_STATUS1_SEQ_NUM);
	ts->ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
		AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
	ts->ts_antenna = 1;
	ts->ts_status = 0;
385
	ts->ts_final_idx = 0;
N
Nick Kossifidis 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

	if (!(tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK)) {
		if (tx_status->tx_status_0 &
				AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
			ts->ts_status |= AR5K_TXERR_XRETRY;

		if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
			ts->ts_status |= AR5K_TXERR_FIFO;

		if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
			ts->ts_status |= AR5K_TXERR_FILT;
	}

	return 0;
}

/*
L
Lucas De Marchi 已提交
403
 * Process a tx status descriptor on 5212
N
Nick Kossifidis 已提交
404 405 406 407 408 409
 */
static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *ah,
		struct ath5k_desc *desc, struct ath5k_tx_status *ts)
{
	struct ath5k_hw_4w_tx_ctl *tx_ctl;
	struct ath5k_hw_tx_status *tx_status;
410
	u32 txstat0, txstat1;
N
Nick Kossifidis 已提交
411 412 413 414

	tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
	tx_status = &desc->ud.ds_tx5212.tx_stat;

415 416
	txstat1 = ACCESS_ONCE(tx_status->tx_status_1);

N
Nick Kossifidis 已提交
417
	/* No frame has been send or error */
418
	if (unlikely(!(txstat1 & AR5K_DESC_TX_STATUS1_DONE)))
N
Nick Kossifidis 已提交
419 420
		return -EINPROGRESS;

421 422
	txstat0 = ACCESS_ONCE(tx_status->tx_status_0);

N
Nick Kossifidis 已提交
423 424 425
	/*
	 * Get descriptor status
	 */
426
	ts->ts_tstamp = AR5K_REG_MS(txstat0,
N
Nick Kossifidis 已提交
427
		AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
428
	ts->ts_shortretry = AR5K_REG_MS(txstat0,
N
Nick Kossifidis 已提交
429
		AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
430
	ts->ts_final_retry = AR5K_REG_MS(txstat0,
N
Nick Kossifidis 已提交
431
		AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
432
	ts->ts_seqnum = AR5K_REG_MS(txstat1,
N
Nick Kossifidis 已提交
433
		AR5K_DESC_TX_STATUS1_SEQ_NUM);
434
	ts->ts_rssi = AR5K_REG_MS(txstat1,
N
Nick Kossifidis 已提交
435
		AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
436
	ts->ts_antenna = (txstat1 &
437
		AR5K_DESC_TX_STATUS1_XMIT_ANTENNA_5212) ? 2 : 1;
N
Nick Kossifidis 已提交
438 439
	ts->ts_status = 0;

440
	ts->ts_final_idx = AR5K_REG_MS(txstat1,
441
			AR5K_DESC_TX_STATUS1_FINAL_TS_IX_5212);
442

N
Nick Kossifidis 已提交
443
	/* TX error */
444 445
	if (!(txstat0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK)) {
		if (txstat0 & AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
N
Nick Kossifidis 已提交
446 447
			ts->ts_status |= AR5K_TXERR_XRETRY;

448
		if (txstat0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
N
Nick Kossifidis 已提交
449 450
			ts->ts_status |= AR5K_TXERR_FIFO;

451
		if (txstat0 & AR5K_DESC_TX_STATUS0_FILTERED)
N
Nick Kossifidis 已提交
452 453 454 455 456 457
			ts->ts_status |= AR5K_TXERR_FILT;
	}

	return 0;
}

458 459 460 461

/****************\
* RX Descriptors *
\****************/
N
Nick Kossifidis 已提交
462 463 464 465

/*
 * Initialize an rx control descriptor
 */
466 467
int ath5k_hw_setup_rx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
			   u32 size, unsigned int flags)
N
Nick Kossifidis 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481
{
	struct ath5k_hw_rx_ctl *rx_ctl;

	rx_ctl = &desc->ud.ds_rx.rx_ctl;

	/*
	 * Clear the descriptor
	 * If we don't clean the status descriptor,
	 * while scanning we get too many results,
	 * most of them virtual, after some secs
	 * of scanning system hangs. M.F.
	*/
	memset(&desc->ud.ds_rx, 0, sizeof(struct ath5k_hw_all_rx_desc));

482 483 484
	if (unlikely(size & ~AR5K_DESC_RX_CTL1_BUF_LEN))
		return -EINVAL;

N
Nick Kossifidis 已提交
485 486 487 488 489 490 491 492 493 494
	/* Setup descriptor */
	rx_ctl->rx_control_1 = size & AR5K_DESC_RX_CTL1_BUF_LEN;

	if (flags & AR5K_RXDESC_INTREQ)
		rx_ctl->rx_control_1 |= AR5K_DESC_RX_CTL1_INTREQ;

	return 0;
}

/*
L
Lucas De Marchi 已提交
495
 * Process the rx status descriptor on 5210/5211
N
Nick Kossifidis 已提交
496 497 498 499 500 501
 */
static int ath5k_hw_proc_5210_rx_status(struct ath5k_hw *ah,
		struct ath5k_desc *desc, struct ath5k_rx_status *rs)
{
	struct ath5k_hw_rx_status *rx_status;

502
	rx_status = &desc->ud.ds_rx.rx_stat;
N
Nick Kossifidis 已提交
503 504 505

	/* No frame received / not ready */
	if (unlikely(!(rx_status->rx_status_1 &
506
			AR5K_5210_RX_DESC_STATUS1_DONE)))
N
Nick Kossifidis 已提交
507 508
		return -EINPROGRESS;

509 510
	memset(rs, 0, sizeof(struct ath5k_rx_status));

N
Nick Kossifidis 已提交
511 512 513 514 515 516 517 518 519
	/*
	 * Frame receive status
	 */
	rs->rs_datalen = rx_status->rx_status_0 &
		AR5K_5210_RX_DESC_STATUS0_DATA_LEN;
	rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
		AR5K_5210_RX_DESC_STATUS0_RECEIVE_SIGNAL);
	rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
		AR5K_5210_RX_DESC_STATUS0_RECEIVE_RATE);
520 521
	rs->rs_more = !!(rx_status->rx_status_0 &
		AR5K_5210_RX_DESC_STATUS0_MORE);
522 523 524 525 526
	/* TODO: this timestamp is 13 bit, later on we assume 15 bit!
	 * also the HAL code for 5210 says the timestamp is bits [10..22] of the
	 * TSF, and extends the timestamp here to 15 bit.
	 * we need to check on 5210...
	 */
N
Nick Kossifidis 已提交
527 528
	rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
		AR5K_5210_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
529 530 531 532 533 534 535 536 537

	if (ah->ah_version == AR5K_AR5211)
		rs->rs_antenna = AR5K_REG_MS(rx_status->rx_status_0,
				AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANT_5211);
	else
		rs->rs_antenna = (rx_status->rx_status_0 &
				AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANT_5210)
				? 2 : 1;

N
Nick Kossifidis 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550
	/*
	 * Key table status
	 */
	if (rx_status->rx_status_1 & AR5K_5210_RX_DESC_STATUS1_KEY_INDEX_VALID)
		rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
			AR5K_5210_RX_DESC_STATUS1_KEY_INDEX);
	else
		rs->rs_keyix = AR5K_RXKEYIX_INVALID;

	/*
	 * Receive/descriptor errors
	 */
	if (!(rx_status->rx_status_1 &
551
			AR5K_5210_RX_DESC_STATUS1_FRAME_RECEIVE_OK)) {
N
Nick Kossifidis 已提交
552 553 554 555
		if (rx_status->rx_status_1 &
				AR5K_5210_RX_DESC_STATUS1_CRC_ERROR)
			rs->rs_status |= AR5K_RXERR_CRC;

556 557 558 559
		/* only on 5210 */
		if ((ah->ah_version == AR5K_AR5210) &&
		    (rx_status->rx_status_1 &
				AR5K_5210_RX_DESC_STATUS1_FIFO_OVERRUN_5210))
N
Nick Kossifidis 已提交
560 561 562 563 564
			rs->rs_status |= AR5K_RXERR_FIFO;

		if (rx_status->rx_status_1 &
				AR5K_5210_RX_DESC_STATUS1_PHY_ERROR) {
			rs->rs_status |= AR5K_RXERR_PHY;
565
			rs->rs_phyerr = AR5K_REG_MS(rx_status->rx_status_1,
N
Nick Kossifidis 已提交
566 567 568 569 570 571 572 573 574 575 576 577
				AR5K_5210_RX_DESC_STATUS1_PHY_ERROR);
		}

		if (rx_status->rx_status_1 &
				AR5K_5210_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
			rs->rs_status |= AR5K_RXERR_DECRYPT;
	}

	return 0;
}

/*
L
Lucas De Marchi 已提交
578
 * Process the rx status descriptor on 5212
N
Nick Kossifidis 已提交
579 580
 */
static int ath5k_hw_proc_5212_rx_status(struct ath5k_hw *ah,
581 582
					struct ath5k_desc *desc,
					struct ath5k_rx_status *rs)
N
Nick Kossifidis 已提交
583 584
{
	struct ath5k_hw_rx_status *rx_status;
585
	u32 rxstat0, rxstat1;
N
Nick Kossifidis 已提交
586

587
	rx_status = &desc->ud.ds_rx.rx_stat;
588
	rxstat1 = ACCESS_ONCE(rx_status->rx_status_1);
N
Nick Kossifidis 已提交
589 590

	/* No frame received / not ready */
591
	if (unlikely(!(rxstat1 & AR5K_5212_RX_DESC_STATUS1_DONE)))
N
Nick Kossifidis 已提交
592 593
		return -EINPROGRESS;

594
	memset(rs, 0, sizeof(struct ath5k_rx_status));
595
	rxstat0 = ACCESS_ONCE(rx_status->rx_status_0);
596

N
Nick Kossifidis 已提交
597 598 599
	/*
	 * Frame receive status
	 */
600 601
	rs->rs_datalen = rxstat0 & AR5K_5212_RX_DESC_STATUS0_DATA_LEN;
	rs->rs_rssi = AR5K_REG_MS(rxstat0,
N
Nick Kossifidis 已提交
602
		AR5K_5212_RX_DESC_STATUS0_RECEIVE_SIGNAL);
603
	rs->rs_rate = AR5K_REG_MS(rxstat0,
N
Nick Kossifidis 已提交
604
		AR5K_5212_RX_DESC_STATUS0_RECEIVE_RATE);
605
	rs->rs_antenna = AR5K_REG_MS(rxstat0,
606
		AR5K_5212_RX_DESC_STATUS0_RECEIVE_ANTENNA);
607 608
	rs->rs_more = !!(rxstat0 & AR5K_5212_RX_DESC_STATUS0_MORE);
	rs->rs_tstamp = AR5K_REG_MS(rxstat1,
N
Nick Kossifidis 已提交
609 610 611 612 613
		AR5K_5212_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);

	/*
	 * Key table status
	 */
614 615
	if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_KEY_INDEX_VALID)
		rs->rs_keyix = AR5K_REG_MS(rxstat1,
616
					   AR5K_5212_RX_DESC_STATUS1_KEY_INDEX);
N
Nick Kossifidis 已提交
617 618 619 620 621 622
	else
		rs->rs_keyix = AR5K_RXKEYIX_INVALID;

	/*
	 * Receive/descriptor errors
	 */
623 624
	if (!(rxstat1 & AR5K_5212_RX_DESC_STATUS1_FRAME_RECEIVE_OK)) {
		if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_CRC_ERROR)
N
Nick Kossifidis 已提交
625 626
			rs->rs_status |= AR5K_RXERR_CRC;

627
		if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_PHY_ERROR) {
N
Nick Kossifidis 已提交
628
			rs->rs_status |= AR5K_RXERR_PHY;
629
			rs->rs_phyerr = AR5K_REG_MS(rxstat1,
630
				AR5K_5212_RX_DESC_STATUS1_PHY_ERROR_CODE);
631 632
			if (!ah->ah_capabilities.cap_has_phyerr_counters)
				ath5k_ani_phy_error_report(ah, rs->rs_phyerr);
N
Nick Kossifidis 已提交
633 634
		}

635
		if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
N
Nick Kossifidis 已提交
636 637
			rs->rs_status |= AR5K_RXERR_DECRYPT;

638
		if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_MIC_ERROR)
N
Nick Kossifidis 已提交
639 640 641 642 643
			rs->rs_status |= AR5K_RXERR_MIC;
	}
	return 0;
}

644 645 646 647 648

/********\
* Attach *
\********/

N
Nick Kossifidis 已提交
649 650 651 652 653 654 655 656
/*
 * Init function pointers inside ath5k_hw struct
 */
int ath5k_hw_init_desc_functions(struct ath5k_hw *ah)
{
	if (ah->ah_version == AR5K_AR5212) {
		ah->ah_setup_tx_desc = ath5k_hw_setup_4word_tx_desc;
		ah->ah_proc_tx_desc = ath5k_hw_proc_4word_tx_status;
657 658
		ah->ah_proc_rx_desc = ath5k_hw_proc_5212_rx_status;
	} else if (ah->ah_version <= AR5K_AR5211) {
N
Nick Kossifidis 已提交
659 660 661
		ah->ah_setup_tx_desc = ath5k_hw_setup_2word_tx_desc;
		ah->ah_proc_tx_desc = ath5k_hw_proc_2word_tx_status;
		ah->ah_proc_rx_desc = ath5k_hw_proc_5210_rx_status;
662 663
	} else
		return -ENOTSUPP;
N
Nick Kossifidis 已提交
664 665
	return 0;
}