Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
  4 * Copyright (c) 2007-2008 Matthew W. S. Bell  <mentor@madwifi.org>
  5 * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
  6 * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
  7 * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
  8 *
  9 * Permission to use, copy, modify, and distribute this software for any
 10 * purpose with or without fee is hereby granted, provided that the above
 11 * copyright notice and this permission notice appear in all copies.
 12 *
 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 20 *
 21 */
 22
 23/*********************************\
 24* Protocol Control Unit Functions *
 25\*********************************/
 26
 27#include <asm/unaligned.h>
 28
 29#include "ath5k.h"
 30#include "reg.h"
 31#include "debug.h"
 32#include "base.h"
 33
 34/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 35 * AR5212+ can use higher rates for ack transmission
 36 * based on current tx rate instead of the base rate.
 37 * It does this to better utilize channel usage.
 38 * This is a mapping between G rates (that cover both
 39 * CCK and OFDM) and ack rates that we use when setting
 40 * rate -> duration table. This mapping is hw-based so
 41 * don't change anything.
 42 *
 43 * To enable this functionality we must set
 44 * ah->ah_ack_bitrate_high to true else base rate is
 45 * used (1Mb for CCK, 6Mb for OFDM).
 46 */
 47static const unsigned int ack_rates_high[] =
 48/* Tx	-> ACK	*/
 49/* 1Mb	-> 1Mb	*/	{ 0,
 50/* 2MB	-> 2Mb	*/	1,
 51/* 5.5Mb -> 2Mb	*/	1,
 52/* 11Mb	-> 2Mb	*/	1,
 53/* 6Mb	-> 6Mb	*/	4,
 54/* 9Mb	-> 6Mb	*/	4,
 55/* 12Mb	-> 12Mb	*/	6,
 56/* 18Mb	-> 12Mb	*/	6,
 57/* 24Mb	-> 24Mb	*/	8,
 58/* 36Mb	-> 24Mb	*/	8,
 59/* 48Mb	-> 24Mb	*/	8,
 60/* 54Mb	-> 24Mb	*/	8 };
 61
 62/*******************\
 63* Helper functions *
 64\*******************/
 65
 66/**
 67 * ath5k_hw_get_frame_duration - Get tx time of a frame
 68 *
 69 * @ah: The &struct ath5k_hw
 
 70 * @len: Frame's length in bytes
 71 * @rate: The @struct ieee80211_rate
 
 72 *
 73 * Calculate tx duration of a frame given it's rate and length
 74 * It extends ieee80211_generic_frame_duration for non standard
 75 * bwmodes.
 76 */
 77int ath5k_hw_get_frame_duration(struct ath5k_hw *ah,
 
 78		int len, struct ieee80211_rate *rate, bool shortpre)
 79{
 80	int sifs, preamble, plcp_bits, sym_time;
 81	int bitrate, bits, symbols, symbol_bits;
 82	int dur;
 83
 84	/* Fallback */
 85	if (!ah->ah_bwmode) {
 86		__le16 raw_dur = ieee80211_generic_frame_duration(ah->hw,
 87					NULL, len, rate);
 88
 89		/* subtract difference between long and short preamble */
 90		dur = le16_to_cpu(raw_dur);
 91		if (shortpre)
 92			dur -= 96;
 93
 94		return dur;
 95	}
 96
 97	bitrate = rate->bitrate;
 98	preamble = AR5K_INIT_OFDM_PREAMPLE_TIME;
 99	plcp_bits = AR5K_INIT_OFDM_PLCP_BITS;
100	sym_time = AR5K_INIT_OFDM_SYMBOL_TIME;
101
102	switch (ah->ah_bwmode) {
103	case AR5K_BWMODE_40MHZ:
104		sifs = AR5K_INIT_SIFS_TURBO;
105		preamble = AR5K_INIT_OFDM_PREAMBLE_TIME_MIN;
106		break;
107	case AR5K_BWMODE_10MHZ:
108		sifs = AR5K_INIT_SIFS_HALF_RATE;
109		preamble *= 2;
110		sym_time *= 2;
 
111		break;
112	case AR5K_BWMODE_5MHZ:
113		sifs = AR5K_INIT_SIFS_QUARTER_RATE;
114		preamble *= 4;
115		sym_time *= 4;
 
116		break;
117	default:
118		sifs = AR5K_INIT_SIFS_DEFAULT_BG;
119		break;
120	}
121
122	bits = plcp_bits + (len << 3);
123	/* Bit rate is in 100Kbits */
124	symbol_bits = bitrate * sym_time;
125	symbols = DIV_ROUND_UP(bits * 10, symbol_bits);
126
127	dur = sifs + preamble + (sym_time * symbols);
128
129	return dur;
130}
131
132/**
133 * ath5k_hw_get_default_slottime - Get the default slot time for current mode
134 *
135 * @ah: The &struct ath5k_hw
136 */
137unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
 
138{
139	struct ieee80211_channel *channel = ah->ah_current_channel;
140	unsigned int slot_time;
141
142	switch (ah->ah_bwmode) {
143	case AR5K_BWMODE_40MHZ:
144		slot_time = AR5K_INIT_SLOT_TIME_TURBO;
145		break;
146	case AR5K_BWMODE_10MHZ:
147		slot_time = AR5K_INIT_SLOT_TIME_HALF_RATE;
148		break;
149	case AR5K_BWMODE_5MHZ:
150		slot_time = AR5K_INIT_SLOT_TIME_QUARTER_RATE;
151		break;
152	case AR5K_BWMODE_DEFAULT:
153	default:
154		slot_time = AR5K_INIT_SLOT_TIME_DEFAULT;
155		if ((channel->hw_value & CHANNEL_CCK) && !ah->ah_short_slot)
156			slot_time = AR5K_INIT_SLOT_TIME_B;
157		break;
158	}
159
160	return slot_time;
161}
162
163/**
164 * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
165 *
166 * @ah: The &struct ath5k_hw
167 */
168unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
 
169{
170	struct ieee80211_channel *channel = ah->ah_current_channel;
171	unsigned int sifs;
172
173	switch (ah->ah_bwmode) {
174	case AR5K_BWMODE_40MHZ:
175		sifs = AR5K_INIT_SIFS_TURBO;
176		break;
177	case AR5K_BWMODE_10MHZ:
178		sifs = AR5K_INIT_SIFS_HALF_RATE;
179		break;
180	case AR5K_BWMODE_5MHZ:
181		sifs = AR5K_INIT_SIFS_QUARTER_RATE;
182		break;
183	case AR5K_BWMODE_DEFAULT:
184		sifs = AR5K_INIT_SIFS_DEFAULT_BG;
185	default:
186		if (channel->hw_value & CHANNEL_5GHZ)
 
187			sifs = AR5K_INIT_SIFS_DEFAULT_A;
188		break;
189	}
190
191	return sifs;
192}
193
194/**
195 * ath5k_hw_update_mib_counters - Update MIB counters (mac layer statistics)
196 *
197 * @ah: The &struct ath5k_hw
198 *
199 * Reads MIB counters from PCU and updates sw statistics. Is called after a
200 * MIB interrupt, because one of these counters might have reached their maximum
201 * and triggered the MIB interrupt, to let us read and clear the counter.
202 *
203 * Is called in interrupt context!
204 */
205void ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
 
206{
207	struct ath5k_statistics *stats = &ah->stats;
208
209	/* Read-And-Clear */
210	stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
211	stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
212	stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
213	stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
214	stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
215}
216
217
218/******************\
219* ACK/CTS Timeouts *
220\******************/
221
222/**
223 * ath5k_hw_write_rate_duration - fill rate code to duration table
224 *
225 * @ah: the &struct ath5k_hw
226 * @mode: one of enum ath5k_driver_mode
227 *
228 * Write the rate code to duration table upon hw reset. This is a helper for
229 * ath5k_hw_pcu_init(). It seems all this is doing is setting an ACK timeout on
230 * the hardware, based on current mode, for each rate. The rates which are
231 * capable of short preamble (802.11b rates 2Mbps, 5.5Mbps, and 11Mbps) have
232 * different rate code so we write their value twice (one for long preamble
233 * and one for short).
234 *
235 * Note: Band doesn't matter here, if we set the values for OFDM it works
236 * on both a and g modes. So all we have to do is set values for all g rates
237 * that include all OFDM and CCK rates.
238 *
239 */
240static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah)
 
241{
242	struct ieee80211_rate *rate;
243	unsigned int i;
244	/* 802.11g covers both OFDM and CCK */
245	u8 band = IEEE80211_BAND_2GHZ;
246
247	/* Write rate duration table */
248	for (i = 0; i < ah->sbands[band].n_bitrates; i++) {
249		u32 reg;
250		u16 tx_time;
251
252		if (ah->ah_ack_bitrate_high)
253			rate = &ah->sbands[band].bitrates[ack_rates_high[i]];
254		/* CCK -> 1Mb */
255		else if (i < 4)
256			rate = &ah->sbands[band].bitrates[0];
257		/* OFDM -> 6Mb */
258		else
259			rate = &ah->sbands[band].bitrates[4];
260
261		/* Set ACK timeout */
262		reg = AR5K_RATE_DUR(rate->hw_value);
263
264		/* An ACK frame consists of 10 bytes. If you add the FCS,
265		 * which ieee80211_generic_frame_duration() adds,
266		 * its 14 bytes. Note we use the control rate and not the
267		 * actual rate for this rate. See mac80211 tx.c
268		 * ieee80211_duration() for a brief description of
269		 * what rate we should choose to TX ACKs. */
270		tx_time = ath5k_hw_get_frame_duration(ah, 10, rate, false);
 
271
272		ath5k_hw_reg_write(ah, tx_time, reg);
273
274		if (!(rate->flags & IEEE80211_RATE_SHORT_PREAMBLE))
275			continue;
276
277		tx_time = ath5k_hw_get_frame_duration(ah, 10, rate, true);
278		ath5k_hw_reg_write(ah, tx_time,
279			reg + (AR5K_SET_SHORT_PREAMBLE << 2));
280	}
281}
282
283/**
284 * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
285 *
286 * @ah: The &struct ath5k_hw
287 * @timeout: Timeout in usec
288 */
289static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
 
290{
291	if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
292			<= timeout)
293		return -EINVAL;
294
295	AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
296		ath5k_hw_htoclock(ah, timeout));
297
298	return 0;
299}
300
301/**
302 * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
303 *
304 * @ah: The &struct ath5k_hw
305 * @timeout: Timeout in usec
306 */
307static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
 
308{
309	if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
310			<= timeout)
311		return -EINVAL;
312
313	AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
314			ath5k_hw_htoclock(ah, timeout));
315
316	return 0;
317}
318
319
320/*******************\
321* RX filter Control *
322\*******************/
323
324/**
325 * ath5k_hw_set_lladdr - Set station id
326 *
327 * @ah: The &struct ath5k_hw
328 * @mac: The card's mac address
329 *
330 * Set station id on hw using the provided mac address
331 */
332int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
 
333{
334	struct ath_common *common = ath5k_hw_common(ah);
335	u32 low_id, high_id;
336	u32 pcu_reg;
337
338	/* Set new station ID */
339	memcpy(common->macaddr, mac, ETH_ALEN);
340
341	pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
342
343	low_id = get_unaligned_le32(mac);
344	high_id = get_unaligned_le16(mac + 4);
345
346	ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
347	ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
348
349	return 0;
350}
351
352/**
353 * ath5k_hw_set_bssid - Set current BSSID on hw
354 *
355 * @ah: The &struct ath5k_hw
356 *
357 * Sets the current BSSID and BSSID mask we have from the
358 * common struct into the hardware
359 */
360void ath5k_hw_set_bssid(struct ath5k_hw *ah)
 
361{
362	struct ath_common *common = ath5k_hw_common(ah);
363	u16 tim_offset = 0;
364
365	/*
366	 * Set BSSID mask on 5212
367	 */
368	if (ah->ah_version == AR5K_AR5212)
369		ath_hw_setbssidmask(common);
370
371	/*
372	 * Set BSSID
373	 */
374	ath5k_hw_reg_write(ah,
375			   get_unaligned_le32(common->curbssid),
376			   AR5K_BSS_ID0);
377	ath5k_hw_reg_write(ah,
378			   get_unaligned_le16(common->curbssid + 4) |
379			   ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
380			   AR5K_BSS_ID1);
381
382	if (common->curaid == 0) {
383		ath5k_hw_disable_pspoll(ah);
384		return;
385	}
386
387	AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
388			    tim_offset ? tim_offset + 4 : 0);
389
390	ath5k_hw_enable_pspoll(ah, NULL, 0);
391}
392
393void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394{
395	struct ath_common *common = ath5k_hw_common(ah);
396
397	/* Cache bssid mask so that we can restore it
398	 * on reset */
399	memcpy(common->bssidmask, mask, ETH_ALEN);
400	if (ah->ah_version == AR5K_AR5212)
401		ath_hw_setbssidmask(common);
402}
403
404/*
405 * Set multicast filter
 
 
 
406 */
407void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
 
408{
409	ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
410	ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
411}
412
413/**
414 * ath5k_hw_get_rx_filter - Get current rx filter
415 *
416 * @ah: The &struct ath5k_hw
417 *
418 * Returns the RX filter by reading rx filter and
419 * phy error filter registers. RX filter is used
420 * to set the allowed frame types that PCU will accept
421 * and pass to the driver. For a list of frame types
422 * check out reg.h.
423 */
424u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
 
425{
426	u32 data, filter = 0;
427
428	filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
429
430	/*Radar detection for 5212*/
431	if (ah->ah_version == AR5K_AR5212) {
432		data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
433
434		if (data & AR5K_PHY_ERR_FIL_RADAR)
435			filter |= AR5K_RX_FILTER_RADARERR;
436		if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
437			filter |= AR5K_RX_FILTER_PHYERR;
438	}
439
440	return filter;
441}
442
443/**
444 * ath5k_hw_set_rx_filter - Set rx filter
445 *
446 * @ah: The &struct ath5k_hw
447 * @filter: RX filter mask (see reg.h)
448 *
449 * Sets RX filter register and also handles PHY error filter
450 * register on 5212 and newer chips so that we have proper PHY
451 * error reporting.
452 */
453void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
 
454{
455	u32 data = 0;
456
457	/* Set PHY error filter register on 5212*/
458	if (ah->ah_version == AR5K_AR5212) {
459		if (filter & AR5K_RX_FILTER_RADARERR)
460			data |= AR5K_PHY_ERR_FIL_RADAR;
461		if (filter & AR5K_RX_FILTER_PHYERR)
462			data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
463	}
464
465	/*
466	 * The AR5210 uses promiscuous mode to detect radar activity
467	 */
468	if (ah->ah_version == AR5K_AR5210 &&
469			(filter & AR5K_RX_FILTER_RADARERR)) {
470		filter &= ~AR5K_RX_FILTER_RADARERR;
471		filter |= AR5K_RX_FILTER_PROM;
472	}
473
474	/*Zero length DMA (phy error reporting) */
475	if (data)
476		AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
477	else
478		AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
479
480	/*Write RX Filter register*/
481	ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
482
483	/*Write PHY error filter register on 5212*/
484	if (ah->ah_version == AR5K_AR5212)
485		ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
486
487}
488
489
490/****************\
491* Beacon control *
492\****************/
493
494#define ATH5K_MAX_TSF_READ 10
495
496/**
497 * ath5k_hw_get_tsf64 - Get the full 64bit TSF
498 *
499 * @ah: The &struct ath5k_hw
500 *
501 * Returns the current TSF
502 */
503u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
 
504{
505	u32 tsf_lower, tsf_upper1, tsf_upper2;
506	int i;
507	unsigned long flags;
508
509	/* This code is time critical - we don't want to be interrupted here */
510	local_irq_save(flags);
511
512	/*
513	 * While reading TSF upper and then lower part, the clock is still
514	 * counting (or jumping in case of IBSS merge) so we might get
515	 * inconsistent values. To avoid this, we read the upper part again
516	 * and check it has not been changed. We make the hypothesis that a
517	 * maximum of 3 changes can happens in a row (we use 10 as a safe
518	 * value).
519	 *
520	 * Impact on performance is pretty small, since in most cases, only
521	 * 3 register reads are needed.
522	 */
523
524	tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
525	for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
526		tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
527		tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
528		if (tsf_upper2 == tsf_upper1)
529			break;
530		tsf_upper1 = tsf_upper2;
531	}
532
533	local_irq_restore(flags);
534
535	WARN_ON(i == ATH5K_MAX_TSF_READ);
536
537	return ((u64)tsf_upper1 << 32) | tsf_lower;
538}
539
 
 
540/**
541 * ath5k_hw_set_tsf64 - Set a new 64bit TSF
542 *
543 * @ah: The &struct ath5k_hw
544 * @tsf64: The new 64bit TSF
545 *
546 * Sets the new TSF
547 */
548void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
 
549{
550	ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
551	ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
552}
553
554/**
555 * ath5k_hw_reset_tsf - Force a TSF reset
556 *
557 * @ah: The &struct ath5k_hw
558 *
559 * Forces a TSF reset on PCU
560 */
561void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
 
562{
563	u32 val;
564
565	val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
566
567	/*
568	 * Each write to the RESET_TSF bit toggles a hardware internal
569	 * signal to reset TSF, but if left high it will cause a TSF reset
570	 * on the next chip reset as well.  Thus we always write the value
571	 * twice to clear the signal.
572	 */
573	ath5k_hw_reg_write(ah, val, AR5K_BEACON);
574	ath5k_hw_reg_write(ah, val, AR5K_BEACON);
575}
576
577/*
578 * Initialize beacon timers
 
 
 
 
 
 
579 */
580void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
 
581{
582	u32 timer1, timer2, timer3;
583
584	/*
585	 * Set the additional timers by mode
586	 */
587	switch (ah->opmode) {
588	case NL80211_IFTYPE_MONITOR:
589	case NL80211_IFTYPE_STATION:
590		/* In STA mode timer1 is used as next wakeup
591		 * timer and timer2 as next CFP duration start
592		 * timer. Both in 1/8TUs. */
593		/* TODO: PCF handling */
594		if (ah->ah_version == AR5K_AR5210) {
595			timer1 = 0xffffffff;
596			timer2 = 0xffffffff;
597		} else {
598			timer1 = 0x0000ffff;
599			timer2 = 0x0007ffff;
600		}
601		/* Mark associated AP as PCF incapable for now */
602		AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
603		break;
604	case NL80211_IFTYPE_ADHOC:
605		AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
 
606	default:
607		/* On non-STA modes timer1 is used as next DMA
608		 * beacon alert (DBA) timer and timer2 as next
609		 * software beacon alert. Both in 1/8TUs. */
610		timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
611		timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
612		break;
613	}
614
615	/* Timer3 marks the end of our ATIM window
616	 * a zero length window is not allowed because
617	 * we 'll get no beacons */
618	timer3 = next_beacon + 1;
619
620	/*
621	 * Set the beacon register and enable all timers.
622	 */
623	/* When in AP or Mesh Point mode zero timer0 to start TSF */
624	if (ah->opmode == NL80211_IFTYPE_AP ||
625	    ah->opmode == NL80211_IFTYPE_MESH_POINT)
626		ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
627
628	ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
629	ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
630	ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
631	ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
632
633	/* Force a TSF reset if requested and enable beacons */
634	if (interval & AR5K_BEACON_RESET_TSF)
635		ath5k_hw_reset_tsf(ah);
636
637	ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
638					AR5K_BEACON_ENABLE),
639						AR5K_BEACON);
640
641	/* Flush any pending BMISS interrupts on ISR by
642	 * performing a clear-on-write operation on PISR
643	 * register for the BMISS bit (writing a bit on
644	 * ISR toggles a reset for that bit and leaves
645	 * the remaining bits intact) */
646	if (ah->ah_version == AR5K_AR5210)
647		ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
648	else
649		ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
650
651	/* TODO: Set enhanced sleep registers on AR5212
652	 * based on vif->bss_conf params, until then
653	 * disable power save reporting.*/
654	AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
655
656}
657
658/**
659 * ath5k_check_timer_win - Check if timer B is timer A + window
660 *
661 * @a: timer a (before b)
662 * @b: timer b (after a)
663 * @window: difference between a and b
664 * @intval: timers are increased by this interval
665 *
666 * This helper function checks if timer B is timer A + window and covers
667 * cases where timer A or B might have already been updated or wrapped
668 * around (Timers are 16 bit).
669 *
670 * Returns true if O.K.
671 */
672static inline bool
673ath5k_check_timer_win(int a, int b, int window, int intval)
674{
675	/*
676	 * 1.) usually B should be A + window
677	 * 2.) A already updated, B not updated yet
678	 * 3.) A already updated and has wrapped around
679	 * 4.) B has wrapped around
680	 */
681	if ((b - a == window) ||				/* 1.) */
682	    (a - b == intval - window) ||			/* 2.) */
683	    ((a | 0x10000) - b == intval - window) ||		/* 3.) */
684	    ((b | 0x10000) - a == window))			/* 4.) */
685		return true; /* O.K. */
686	return false;
687}
688
689/**
690 * ath5k_hw_check_beacon_timers - Check if the beacon timers are correct
691 *
692 * @ah: The &struct ath5k_hw
693 * @intval: beacon interval
694 *
695 * This is a workaround for IBSS mode:
696 *
697 * The need for this function arises from the fact that we have 4 separate
698 * HW timer registers (TIMER0 - TIMER3), which are closely related to the
699 * next beacon target time (NBTT), and that the HW updates these timers
700 * separately based on the current TSF value. The hardware increments each
701 * timer by the beacon interval, when the local TSF converted to TU is equal
702 * to the value stored in the timer.
703 *
704 * The reception of a beacon with the same BSSID can update the local HW TSF
705 * at any time - this is something we can't avoid. If the TSF jumps to a
706 * time which is later than the time stored in a timer, this timer will not
707 * be updated until the TSF in TU wraps around at 16 bit (the size of the
708 * timers) and reaches the time which is stored in the timer.
709 *
710 * The problem is that these timers are closely related to TIMER0 (NBTT) and
711 * that they define a time "window". When the TSF jumps between two timers
712 * (e.g. ATIM and NBTT), the one in the past will be left behind (not
713 * updated), while the one in the future will be updated every beacon
714 * interval. This causes the window to get larger, until the TSF wraps
715 * around as described above and the timer which was left behind gets
716 * updated again. But - because the beacon interval is usually not an exact
717 * divisor of the size of the timers (16 bit), an unwanted "window" between
718 * these timers has developed!
719 *
720 * This is especially important with the ATIM window, because during
721 * the ATIM window only ATIM frames and no data frames are allowed to be
722 * sent, which creates transmission pauses after each beacon. This symptom
723 * has been described as "ramping ping" because ping times increase linearly
724 * for some time and then drop down again. A wrong window on the DMA beacon
725 * timer has the same effect, so we check for these two conditions.
726 *
727 * Returns true if O.K.
728 */
729bool
730ath5k_hw_check_beacon_timers(struct ath5k_hw *ah, int intval)
731{
732	unsigned int nbtt, atim, dma;
733
734	nbtt = ath5k_hw_reg_read(ah, AR5K_TIMER0);
735	atim = ath5k_hw_reg_read(ah, AR5K_TIMER3);
736	dma = ath5k_hw_reg_read(ah, AR5K_TIMER1) >> 3;
737
738	/* NOTE: SWBA is different. Having a wrong window there does not
739	 * stop us from sending data and this condition is caught by
740	 * other means (SWBA interrupt) */
741
742	if (ath5k_check_timer_win(nbtt, atim, 1, intval) &&
743	    ath5k_check_timer_win(dma, nbtt, AR5K_TUNE_DMA_BEACON_RESP,
744				  intval))
745		return true; /* O.K. */
746	return false;
747}
748
749/**
750 * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
751 *
752 * @ah: The &struct ath5k_hw
753 * @coverage_class: IEEE 802.11 coverage class number
754 *
755 * Sets IFS intervals and ACK/CTS timeouts for given coverage class.
756 */
757void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
 
758{
759	/* As defined by IEEE 802.11-2007 17.3.8.6 */
760	int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
761	int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
762	int cts_timeout = ack_timeout;
763
764	ath5k_hw_set_ifs_intervals(ah, slot_time);
765	ath5k_hw_set_ack_timeout(ah, ack_timeout);
766	ath5k_hw_set_cts_timeout(ah, cts_timeout);
767
768	ah->ah_coverage_class = coverage_class;
769}
770
771/***************************\
772* Init/Start/Stop functions *
773\***************************/
774
775/**
776 * ath5k_hw_start_rx_pcu - Start RX engine
777 *
778 * @ah: The &struct ath5k_hw
779 *
780 * Starts RX engine on PCU so that hw can process RXed frames
781 * (ACK etc).
782 *
783 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
784 */
785void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
 
786{
787	AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
788}
789
790/**
791 * at5k_hw_stop_rx_pcu - Stop RX engine
792 *
793 * @ah: The &struct ath5k_hw
794 *
795 * Stops RX engine on PCU
796 */
797void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
 
798{
799	AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
800}
801
802/**
803 * ath5k_hw_set_opmode - Set PCU operating mode
804 *
805 * @ah: The &struct ath5k_hw
806 * @op_mode: &enum nl80211_iftype operating mode
807 *
808 * Configure PCU for the various operating modes (AP/STA etc)
809 */
810int ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
 
811{
812	struct ath_common *common = ath5k_hw_common(ah);
813	u32 pcu_reg, beacon_reg, low_id, high_id;
814
815	ATH5K_DBG(ah, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
816
817	/* Preserve rest settings */
818	pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
819	pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
820			| AR5K_STA_ID1_KEYSRCH_MODE
821			| (ah->ah_version == AR5K_AR5210 ?
822			(AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
823
824	beacon_reg = 0;
825
826	switch (op_mode) {
827	case NL80211_IFTYPE_ADHOC:
828		pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
829		beacon_reg |= AR5K_BCR_ADHOC;
830		if (ah->ah_version == AR5K_AR5210)
831			pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
832		else
833			AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
834		break;
835
836	case NL80211_IFTYPE_AP:
837	case NL80211_IFTYPE_MESH_POINT:
838		pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
839		beacon_reg |= AR5K_BCR_AP;
840		if (ah->ah_version == AR5K_AR5210)
841			pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
842		else
843			AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
844		break;
845
846	case NL80211_IFTYPE_STATION:
847		pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
848			| (ah->ah_version == AR5K_AR5210 ?
849				AR5K_STA_ID1_PWR_SV : 0);
 
850	case NL80211_IFTYPE_MONITOR:
851		pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
852			| (ah->ah_version == AR5K_AR5210 ?
853				AR5K_STA_ID1_NO_PSPOLL : 0);
854		break;
855
856	default:
857		return -EINVAL;
858	}
859
860	/*
861	 * Set PCU registers
862	 */
863	low_id = get_unaligned_le32(common->macaddr);
864	high_id = get_unaligned_le16(common->macaddr + 4);
865	ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
866	ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
867
868	/*
869	 * Set Beacon Control Register on 5210
870	 */
871	if (ah->ah_version == AR5K_AR5210)
872		ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
873
874	return 0;
875}
876
877void ath5k_hw_pcu_init(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
878								u8 mode)
 
 
 
 
 
 
 
 
879{
880	/* Set bssid and bssid mask */
881	ath5k_hw_set_bssid(ah);
882
883	/* Set PCU config */
884	ath5k_hw_set_opmode(ah, op_mode);
885
886	/* Write rate duration table only on AR5212 and if
887	 * virtual interface has already been brought up
888	 * XXX: rethink this after new mode changes to
889	 * mac80211 are integrated */
890	if (ah->ah_version == AR5K_AR5212 &&
891		ah->nvifs)
892		ath5k_hw_write_rate_duration(ah);
893
894	/* Set RSSI/BRSSI thresholds
895	 *
896	 * Note: If we decide to set this value
897	 * dynamically, have in mind that when AR5K_RSSI_THR
898	 * register is read it might return 0x40 if we haven't
899	 * wrote anything to it plus BMISS RSSI threshold is zeroed.
900	 * So doing a save/restore procedure here isn't the right
901	 * choice. Instead store it on ath5k_hw */
902	ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
903				AR5K_TUNE_BMISS_THRES <<
904				AR5K_RSSI_THR_BMISS_S),
905				AR5K_RSSI_THR);
906
907	/* MIC QoS support */
908	if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
909		ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
910		ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
911	}
912
913	/* QoS NOACK Policy */
914	if (ah->ah_version == AR5K_AR5212) {
915		ath5k_hw_reg_write(ah,
916			AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
917			AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET)  |
918			AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
919			AR5K_QOS_NOACK);
920	}
921
922	/* Restore slot time and ACK timeouts */
923	if (ah->ah_coverage_class > 0)
924		ath5k_hw_set_coverage_class(ah, ah->ah_coverage_class);
925
926	/* Set ACK bitrate mode (see ack_rates_high) */
927	if (ah->ah_version == AR5K_AR5212) {
928		u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
929		if (ah->ah_ack_bitrate_high)
930			AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
931		else
932			AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
933	}
934	return;
935}
v5.14.15
   1/*
   2 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
   3 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
   4 * Copyright (c) 2007-2008 Matthew W. S. Bell  <mentor@madwifi.org>
   5 * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
   6 * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
   7 * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
   8 *
   9 * Permission to use, copy, modify, and distribute this software for any
  10 * purpose with or without fee is hereby granted, provided that the above
  11 * copyright notice and this permission notice appear in all copies.
  12 *
  13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20 *
  21 */
  22
  23/*********************************\
  24* Protocol Control Unit Functions *
  25\*********************************/
  26
  27#include <asm/unaligned.h>
  28
  29#include "ath5k.h"
  30#include "reg.h"
  31#include "debug.h"
 
  32
  33/**
  34 * DOC: Protocol Control Unit (PCU) functions
  35 *
  36 * Protocol control unit is responsible to maintain various protocol
  37 * properties before a frame is send and after a frame is received to/from
  38 * baseband. To be more specific, PCU handles:
  39 *
  40 * - Buffering of RX and TX frames (after QCU/DCUs)
  41 *
  42 * - Encrypting and decrypting (using the built-in engine)
  43 *
  44 * - Generating ACKs, RTS/CTS frames
  45 *
  46 * - Maintaining TSF
  47 *
  48 * - FCS
  49 *
  50 * - Updating beacon data (with TSF etc)
  51 *
  52 * - Generating virtual CCA
  53 *
  54 * - RX/Multicast filtering
  55 *
  56 * - BSSID filtering
  57 *
  58 * - Various statistics
  59 *
  60 * -Different operating modes: AP, STA, IBSS
  61 *
  62 * Note: Most of these functions can be tweaked/bypassed so you can do
  63 * them on sw above for debugging or research. For more infos check out PCU
  64 * registers on reg.h.
  65 */
  66
  67/**
  68 * DOC: ACK rates
  69 *
  70 * AR5212+ can use higher rates for ack transmission
  71 * based on current tx rate instead of the base rate.
  72 * It does this to better utilize channel usage.
  73 * There is a mapping between G rates (that cover both
  74 * CCK and OFDM) and ack rates that we use when setting
  75 * rate -> duration table. This mapping is hw-based so
  76 * don't change anything.
  77 *
  78 * To enable this functionality we must set
  79 * ah->ah_ack_bitrate_high to true else base rate is
  80 * used (1Mb for CCK, 6Mb for OFDM).
  81 */
  82static const unsigned int ack_rates_high[] =
  83/* Tx	-> ACK	*/
  84/* 1Mb	-> 1Mb	*/	{ 0,
  85/* 2MB	-> 2Mb	*/	1,
  86/* 5.5Mb -> 2Mb	*/	1,
  87/* 11Mb	-> 2Mb	*/	1,
  88/* 6Mb	-> 6Mb	*/	4,
  89/* 9Mb	-> 6Mb	*/	4,
  90/* 12Mb	-> 12Mb	*/	6,
  91/* 18Mb	-> 12Mb	*/	6,
  92/* 24Mb	-> 24Mb	*/	8,
  93/* 36Mb	-> 24Mb	*/	8,
  94/* 48Mb	-> 24Mb	*/	8,
  95/* 54Mb	-> 24Mb	*/	8 };
  96
  97/*******************\
  98* Helper functions *
  99\*******************/
 100
 101/**
 102 * ath5k_hw_get_frame_duration() - Get tx time of a frame
 
 103 * @ah: The &struct ath5k_hw
 104 * @band: One of enum nl80211_band
 105 * @len: Frame's length in bytes
 106 * @rate: The @struct ieee80211_rate
 107 * @shortpre: Indicate short preample
 108 *
 109 * Calculate tx duration of a frame given it's rate and length
 110 * It extends ieee80211_generic_frame_duration for non standard
 111 * bwmodes.
 112 */
 113int
 114ath5k_hw_get_frame_duration(struct ath5k_hw *ah, enum nl80211_band band,
 115		int len, struct ieee80211_rate *rate, bool shortpre)
 116{
 117	int sifs, preamble, plcp_bits, sym_time;
 118	int bitrate, bits, symbols, symbol_bits;
 119	int dur;
 120
 121	/* Fallback */
 122	if (!ah->ah_bwmode) {
 123		__le16 raw_dur = ieee80211_generic_frame_duration(ah->hw,
 124					NULL, band, len, rate);
 125
 126		/* subtract difference between long and short preamble */
 127		dur = le16_to_cpu(raw_dur);
 128		if (shortpre)
 129			dur -= 96;
 130
 131		return dur;
 132	}
 133
 134	bitrate = rate->bitrate;
 135	preamble = AR5K_INIT_OFDM_PREAMPLE_TIME;
 136	plcp_bits = AR5K_INIT_OFDM_PLCP_BITS;
 137	sym_time = AR5K_INIT_OFDM_SYMBOL_TIME;
 138
 139	switch (ah->ah_bwmode) {
 140	case AR5K_BWMODE_40MHZ:
 141		sifs = AR5K_INIT_SIFS_TURBO;
 142		preamble = AR5K_INIT_OFDM_PREAMBLE_TIME_MIN;
 143		break;
 144	case AR5K_BWMODE_10MHZ:
 145		sifs = AR5K_INIT_SIFS_HALF_RATE;
 146		preamble *= 2;
 147		sym_time *= 2;
 148		bitrate = DIV_ROUND_UP(bitrate, 2);
 149		break;
 150	case AR5K_BWMODE_5MHZ:
 151		sifs = AR5K_INIT_SIFS_QUARTER_RATE;
 152		preamble *= 4;
 153		sym_time *= 4;
 154		bitrate = DIV_ROUND_UP(bitrate, 4);
 155		break;
 156	default:
 157		sifs = AR5K_INIT_SIFS_DEFAULT_BG;
 158		break;
 159	}
 160
 161	bits = plcp_bits + (len << 3);
 162	/* Bit rate is in 100Kbits */
 163	symbol_bits = bitrate * sym_time;
 164	symbols = DIV_ROUND_UP(bits * 10, symbol_bits);
 165
 166	dur = sifs + preamble + (sym_time * symbols);
 167
 168	return dur;
 169}
 170
 171/**
 172 * ath5k_hw_get_default_slottime() - Get the default slot time for current mode
 
 173 * @ah: The &struct ath5k_hw
 174 */
 175unsigned int
 176ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
 177{
 178	struct ieee80211_channel *channel = ah->ah_current_channel;
 179	unsigned int slot_time;
 180
 181	switch (ah->ah_bwmode) {
 182	case AR5K_BWMODE_40MHZ:
 183		slot_time = AR5K_INIT_SLOT_TIME_TURBO;
 184		break;
 185	case AR5K_BWMODE_10MHZ:
 186		slot_time = AR5K_INIT_SLOT_TIME_HALF_RATE;
 187		break;
 188	case AR5K_BWMODE_5MHZ:
 189		slot_time = AR5K_INIT_SLOT_TIME_QUARTER_RATE;
 190		break;
 191	case AR5K_BWMODE_DEFAULT:
 192	default:
 193		slot_time = AR5K_INIT_SLOT_TIME_DEFAULT;
 194		if ((channel->hw_value == AR5K_MODE_11B) && !ah->ah_short_slot)
 195			slot_time = AR5K_INIT_SLOT_TIME_B;
 196		break;
 197	}
 198
 199	return slot_time;
 200}
 201
 202/**
 203 * ath5k_hw_get_default_sifs() - Get the default SIFS for current mode
 
 204 * @ah: The &struct ath5k_hw
 205 */
 206unsigned int
 207ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
 208{
 209	struct ieee80211_channel *channel = ah->ah_current_channel;
 210	unsigned int sifs;
 211
 212	switch (ah->ah_bwmode) {
 213	case AR5K_BWMODE_40MHZ:
 214		sifs = AR5K_INIT_SIFS_TURBO;
 215		break;
 216	case AR5K_BWMODE_10MHZ:
 217		sifs = AR5K_INIT_SIFS_HALF_RATE;
 218		break;
 219	case AR5K_BWMODE_5MHZ:
 220		sifs = AR5K_INIT_SIFS_QUARTER_RATE;
 221		break;
 222	case AR5K_BWMODE_DEFAULT:
 
 223	default:
 224		sifs = AR5K_INIT_SIFS_DEFAULT_BG;
 225		if (channel->band == NL80211_BAND_5GHZ)
 226			sifs = AR5K_INIT_SIFS_DEFAULT_A;
 227		break;
 228	}
 229
 230	return sifs;
 231}
 232
 233/**
 234 * ath5k_hw_update_mib_counters() - Update MIB counters (mac layer statistics)
 
 235 * @ah: The &struct ath5k_hw
 236 *
 237 * Reads MIB counters from PCU and updates sw statistics. Is called after a
 238 * MIB interrupt, because one of these counters might have reached their maximum
 239 * and triggered the MIB interrupt, to let us read and clear the counter.
 240 *
 241 * NOTE: Is called in interrupt context!
 242 */
 243void
 244ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
 245{
 246	struct ath5k_statistics *stats = &ah->stats;
 247
 248	/* Read-And-Clear */
 249	stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
 250	stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
 251	stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
 252	stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
 253	stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
 254}
 255
 256
 257/******************\
 258* ACK/CTS Timeouts *
 259\******************/
 260
 261/**
 262 * ath5k_hw_write_rate_duration() - Fill rate code to duration table
 263 * @ah: The &struct ath5k_hw
 
 
 264 *
 265 * Write the rate code to duration table upon hw reset. This is a helper for
 266 * ath5k_hw_pcu_init(). It seems all this is doing is setting an ACK timeout on
 267 * the hardware, based on current mode, for each rate. The rates which are
 268 * capable of short preamble (802.11b rates 2Mbps, 5.5Mbps, and 11Mbps) have
 269 * different rate code so we write their value twice (one for long preamble
 270 * and one for short).
 271 *
 272 * Note: Band doesn't matter here, if we set the values for OFDM it works
 273 * on both a and g modes. So all we have to do is set values for all g rates
 274 * that include all OFDM and CCK rates.
 275 *
 276 */
 277static inline void
 278ath5k_hw_write_rate_duration(struct ath5k_hw *ah)
 279{
 280	struct ieee80211_rate *rate;
 281	unsigned int i;
 282	/* 802.11g covers both OFDM and CCK */
 283	u8 band = NL80211_BAND_2GHZ;
 284
 285	/* Write rate duration table */
 286	for (i = 0; i < ah->sbands[band].n_bitrates; i++) {
 287		u32 reg;
 288		u16 tx_time;
 289
 290		if (ah->ah_ack_bitrate_high)
 291			rate = &ah->sbands[band].bitrates[ack_rates_high[i]];
 292		/* CCK -> 1Mb */
 293		else if (i < 4)
 294			rate = &ah->sbands[band].bitrates[0];
 295		/* OFDM -> 6Mb */
 296		else
 297			rate = &ah->sbands[band].bitrates[4];
 298
 299		/* Set ACK timeout */
 300		reg = AR5K_RATE_DUR(rate->hw_value);
 301
 302		/* An ACK frame consists of 10 bytes. If you add the FCS,
 303		 * which ieee80211_generic_frame_duration() adds,
 304		 * its 14 bytes. Note we use the control rate and not the
 305		 * actual rate for this rate. See mac80211 tx.c
 306		 * ieee80211_duration() for a brief description of
 307		 * what rate we should choose to TX ACKs. */
 308		tx_time = ath5k_hw_get_frame_duration(ah, band, 10,
 309					rate, false);
 310
 311		ath5k_hw_reg_write(ah, tx_time, reg);
 312
 313		if (!(rate->flags & IEEE80211_RATE_SHORT_PREAMBLE))
 314			continue;
 315
 316		tx_time = ath5k_hw_get_frame_duration(ah, band, 10, rate, true);
 317		ath5k_hw_reg_write(ah, tx_time,
 318			reg + (AR5K_SET_SHORT_PREAMBLE << 2));
 319	}
 320}
 321
 322/**
 323 * ath5k_hw_set_ack_timeout() - Set ACK timeout on PCU
 
 324 * @ah: The &struct ath5k_hw
 325 * @timeout: Timeout in usec
 326 */
 327static int
 328ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
 329{
 330	if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
 331			<= timeout)
 332		return -EINVAL;
 333
 334	AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
 335		ath5k_hw_htoclock(ah, timeout));
 336
 337	return 0;
 338}
 339
 340/**
 341 * ath5k_hw_set_cts_timeout() - Set CTS timeout on PCU
 
 342 * @ah: The &struct ath5k_hw
 343 * @timeout: Timeout in usec
 344 */
 345static int
 346ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
 347{
 348	if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
 349			<= timeout)
 350		return -EINVAL;
 351
 352	AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
 353			ath5k_hw_htoclock(ah, timeout));
 354
 355	return 0;
 356}
 357
 358
 359/*******************\
 360* RX filter Control *
 361\*******************/
 362
 363/**
 364 * ath5k_hw_set_lladdr() - Set station id
 
 365 * @ah: The &struct ath5k_hw
 366 * @mac: The card's mac address (array of octets)
 367 *
 368 * Set station id on hw using the provided mac address
 369 */
 370int
 371ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
 372{
 373	struct ath_common *common = ath5k_hw_common(ah);
 374	u32 low_id, high_id;
 375	u32 pcu_reg;
 376
 377	/* Set new station ID */
 378	memcpy(common->macaddr, mac, ETH_ALEN);
 379
 380	pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
 381
 382	low_id = get_unaligned_le32(mac);
 383	high_id = get_unaligned_le16(mac + 4);
 384
 385	ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
 386	ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
 387
 388	return 0;
 389}
 390
 391/**
 392 * ath5k_hw_set_bssid() - Set current BSSID on hw
 
 393 * @ah: The &struct ath5k_hw
 394 *
 395 * Sets the current BSSID and BSSID mask we have from the
 396 * common struct into the hardware
 397 */
 398void
 399ath5k_hw_set_bssid(struct ath5k_hw *ah)
 400{
 401	struct ath_common *common = ath5k_hw_common(ah);
 402	u16 tim_offset = 0;
 403
 404	/*
 405	 * Set BSSID mask on 5212
 406	 */
 407	if (ah->ah_version == AR5K_AR5212)
 408		ath_hw_setbssidmask(common);
 409
 410	/*
 411	 * Set BSSID
 412	 */
 413	ath5k_hw_reg_write(ah,
 414			   get_unaligned_le32(common->curbssid),
 415			   AR5K_BSS_ID0);
 416	ath5k_hw_reg_write(ah,
 417			   get_unaligned_le16(common->curbssid + 4) |
 418			   ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
 419			   AR5K_BSS_ID1);
 420
 421	if (common->curaid == 0) {
 422		ath5k_hw_disable_pspoll(ah);
 423		return;
 424	}
 425
 426	AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
 427			    tim_offset ? tim_offset + 4 : 0);
 428
 429	ath5k_hw_enable_pspoll(ah, NULL, 0);
 430}
 431
 432/**
 433 * ath5k_hw_set_bssid_mask() - Filter out bssids we listen
 434 * @ah: The &struct ath5k_hw
 435 * @mask: The BSSID mask to set (array of octets)
 436 *
 437 * BSSID masking is a method used by AR5212 and newer hardware to inform PCU
 438 * which bits of the interface's MAC address should be looked at when trying
 439 * to decide which packets to ACK. In station mode and AP mode with a single
 440 * BSS every bit matters since we lock to only one BSS. In AP mode with
 441 * multiple BSSes (virtual interfaces) not every bit matters because hw must
 442 * accept frames for all BSSes and so we tweak some bits of our mac address
 443 * in order to have multiple BSSes.
 444 *
 445 * For more information check out ../hw.c of the common ath module.
 446 */
 447void
 448ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
 449{
 450	struct ath_common *common = ath5k_hw_common(ah);
 451
 452	/* Cache bssid mask so that we can restore it
 453	 * on reset */
 454	memcpy(common->bssidmask, mask, ETH_ALEN);
 455	if (ah->ah_version == AR5K_AR5212)
 456		ath_hw_setbssidmask(common);
 457}
 458
 459/**
 460 * ath5k_hw_set_mcast_filter() - Set multicast filter
 461 * @ah: The &struct ath5k_hw
 462 * @filter0: Lower 32bits of muticast filter
 463 * @filter1: Higher 16bits of multicast filter
 464 */
 465void
 466ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
 467{
 468	ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
 469	ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
 470}
 471
 472/**
 473 * ath5k_hw_get_rx_filter() - Get current rx filter
 
 474 * @ah: The &struct ath5k_hw
 475 *
 476 * Returns the RX filter by reading rx filter and
 477 * phy error filter registers. RX filter is used
 478 * to set the allowed frame types that PCU will accept
 479 * and pass to the driver. For a list of frame types
 480 * check out reg.h.
 481 */
 482u32
 483ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
 484{
 485	u32 data, filter = 0;
 486
 487	filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
 488
 489	/*Radar detection for 5212*/
 490	if (ah->ah_version == AR5K_AR5212) {
 491		data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
 492
 493		if (data & AR5K_PHY_ERR_FIL_RADAR)
 494			filter |= AR5K_RX_FILTER_RADARERR;
 495		if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
 496			filter |= AR5K_RX_FILTER_PHYERR;
 497	}
 498
 499	return filter;
 500}
 501
 502/**
 503 * ath5k_hw_set_rx_filter() - Set rx filter
 
 504 * @ah: The &struct ath5k_hw
 505 * @filter: RX filter mask (see reg.h)
 506 *
 507 * Sets RX filter register and also handles PHY error filter
 508 * register on 5212 and newer chips so that we have proper PHY
 509 * error reporting.
 510 */
 511void
 512ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
 513{
 514	u32 data = 0;
 515
 516	/* Set PHY error filter register on 5212*/
 517	if (ah->ah_version == AR5K_AR5212) {
 518		if (filter & AR5K_RX_FILTER_RADARERR)
 519			data |= AR5K_PHY_ERR_FIL_RADAR;
 520		if (filter & AR5K_RX_FILTER_PHYERR)
 521			data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
 522	}
 523
 524	/*
 525	 * The AR5210 uses promiscuous mode to detect radar activity
 526	 */
 527	if (ah->ah_version == AR5K_AR5210 &&
 528			(filter & AR5K_RX_FILTER_RADARERR)) {
 529		filter &= ~AR5K_RX_FILTER_RADARERR;
 530		filter |= AR5K_RX_FILTER_PROM;
 531	}
 532
 533	/*Zero length DMA (phy error reporting) */
 534	if (data)
 535		AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
 536	else
 537		AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
 538
 539	/*Write RX Filter register*/
 540	ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
 541
 542	/*Write PHY error filter register on 5212*/
 543	if (ah->ah_version == AR5K_AR5212)
 544		ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
 545
 546}
 547
 548
 549/****************\
 550* Beacon control *
 551\****************/
 552
 553#define ATH5K_MAX_TSF_READ 10
 554
 555/**
 556 * ath5k_hw_get_tsf64() - Get the full 64bit TSF
 
 557 * @ah: The &struct ath5k_hw
 558 *
 559 * Returns the current TSF
 560 */
 561u64
 562ath5k_hw_get_tsf64(struct ath5k_hw *ah)
 563{
 564	u32 tsf_lower, tsf_upper1, tsf_upper2;
 565	int i;
 566	unsigned long flags;
 567
 568	/* This code is time critical - we don't want to be interrupted here */
 569	local_irq_save(flags);
 570
 571	/*
 572	 * While reading TSF upper and then lower part, the clock is still
 573	 * counting (or jumping in case of IBSS merge) so we might get
 574	 * inconsistent values. To avoid this, we read the upper part again
 575	 * and check it has not been changed. We make the hypothesis that a
 576	 * maximum of 3 changes can happens in a row (we use 10 as a safe
 577	 * value).
 578	 *
 579	 * Impact on performance is pretty small, since in most cases, only
 580	 * 3 register reads are needed.
 581	 */
 582
 583	tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
 584	for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
 585		tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
 586		tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
 587		if (tsf_upper2 == tsf_upper1)
 588			break;
 589		tsf_upper1 = tsf_upper2;
 590	}
 591
 592	local_irq_restore(flags);
 593
 594	WARN_ON(i == ATH5K_MAX_TSF_READ);
 595
 596	return ((u64)tsf_upper1 << 32) | tsf_lower;
 597}
 598
 599#undef ATH5K_MAX_TSF_READ
 600
 601/**
 602 * ath5k_hw_set_tsf64() - Set a new 64bit TSF
 
 603 * @ah: The &struct ath5k_hw
 604 * @tsf64: The new 64bit TSF
 605 *
 606 * Sets the new TSF
 607 */
 608void
 609ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
 610{
 611	ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
 612	ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
 613}
 614
 615/**
 616 * ath5k_hw_reset_tsf() - Force a TSF reset
 
 617 * @ah: The &struct ath5k_hw
 618 *
 619 * Forces a TSF reset on PCU
 620 */
 621void
 622ath5k_hw_reset_tsf(struct ath5k_hw *ah)
 623{
 624	u32 val;
 625
 626	val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
 627
 628	/*
 629	 * Each write to the RESET_TSF bit toggles a hardware internal
 630	 * signal to reset TSF, but if left high it will cause a TSF reset
 631	 * on the next chip reset as well.  Thus we always write the value
 632	 * twice to clear the signal.
 633	 */
 634	ath5k_hw_reg_write(ah, val, AR5K_BEACON);
 635	ath5k_hw_reg_write(ah, val, AR5K_BEACON);
 636}
 637
 638/**
 639 * ath5k_hw_init_beacon_timers() - Initialize beacon timers
 640 * @ah: The &struct ath5k_hw
 641 * @next_beacon: Next TBTT
 642 * @interval: Current beacon interval
 643 *
 644 * This function is used to initialize beacon timers based on current
 645 * operation mode and settings.
 646 */
 647void
 648ath5k_hw_init_beacon_timers(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
 649{
 650	u32 timer1, timer2, timer3;
 651
 652	/*
 653	 * Set the additional timers by mode
 654	 */
 655	switch (ah->opmode) {
 656	case NL80211_IFTYPE_MONITOR:
 657	case NL80211_IFTYPE_STATION:
 658		/* In STA mode timer1 is used as next wakeup
 659		 * timer and timer2 as next CFP duration start
 660		 * timer. Both in 1/8TUs. */
 661		/* TODO: PCF handling */
 662		if (ah->ah_version == AR5K_AR5210) {
 663			timer1 = 0xffffffff;
 664			timer2 = 0xffffffff;
 665		} else {
 666			timer1 = 0x0000ffff;
 667			timer2 = 0x0007ffff;
 668		}
 669		/* Mark associated AP as PCF incapable for now */
 670		AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
 671		break;
 672	case NL80211_IFTYPE_ADHOC:
 673		AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
 674		fallthrough;
 675	default:
 676		/* On non-STA modes timer1 is used as next DMA
 677		 * beacon alert (DBA) timer and timer2 as next
 678		 * software beacon alert. Both in 1/8TUs. */
 679		timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
 680		timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
 681		break;
 682	}
 683
 684	/* Timer3 marks the end of our ATIM window
 685	 * a zero length window is not allowed because
 686	 * we 'll get no beacons */
 687	timer3 = next_beacon + 1;
 688
 689	/*
 690	 * Set the beacon register and enable all timers.
 691	 */
 692	/* When in AP or Mesh Point mode zero timer0 to start TSF */
 693	if (ah->opmode == NL80211_IFTYPE_AP ||
 694	    ah->opmode == NL80211_IFTYPE_MESH_POINT)
 695		ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
 696
 697	ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
 698	ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
 699	ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
 700	ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
 701
 702	/* Force a TSF reset if requested and enable beacons */
 703	if (interval & AR5K_BEACON_RESET_TSF)
 704		ath5k_hw_reset_tsf(ah);
 705
 706	ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
 707					AR5K_BEACON_ENABLE),
 708						AR5K_BEACON);
 709
 710	/* Flush any pending BMISS interrupts on ISR by
 711	 * performing a clear-on-write operation on PISR
 712	 * register for the BMISS bit (writing a bit on
 713	 * ISR toggles a reset for that bit and leaves
 714	 * the remaining bits intact) */
 715	if (ah->ah_version == AR5K_AR5210)
 716		ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
 717	else
 718		ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
 719
 720	/* TODO: Set enhanced sleep registers on AR5212
 721	 * based on vif->bss_conf params, until then
 722	 * disable power save reporting.*/
 723	AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
 724
 725}
 726
 727/**
 728 * ath5k_check_timer_win() - Check if timer B is timer A + window
 
 729 * @a: timer a (before b)
 730 * @b: timer b (after a)
 731 * @window: difference between a and b
 732 * @intval: timers are increased by this interval
 733 *
 734 * This helper function checks if timer B is timer A + window and covers
 735 * cases where timer A or B might have already been updated or wrapped
 736 * around (Timers are 16 bit).
 737 *
 738 * Returns true if O.K.
 739 */
 740static inline bool
 741ath5k_check_timer_win(int a, int b, int window, int intval)
 742{
 743	/*
 744	 * 1.) usually B should be A + window
 745	 * 2.) A already updated, B not updated yet
 746	 * 3.) A already updated and has wrapped around
 747	 * 4.) B has wrapped around
 748	 */
 749	if ((b - a == window) ||				/* 1.) */
 750	    (a - b == intval - window) ||			/* 2.) */
 751	    ((a | 0x10000) - b == intval - window) ||		/* 3.) */
 752	    ((b | 0x10000) - a == window))			/* 4.) */
 753		return true; /* O.K. */
 754	return false;
 755}
 756
 757/**
 758 * ath5k_hw_check_beacon_timers() - Check if the beacon timers are correct
 
 759 * @ah: The &struct ath5k_hw
 760 * @intval: beacon interval
 761 *
 762 * This is a workaround for IBSS mode
 763 *
 764 * The need for this function arises from the fact that we have 4 separate
 765 * HW timer registers (TIMER0 - TIMER3), which are closely related to the
 766 * next beacon target time (NBTT), and that the HW updates these timers
 767 * separately based on the current TSF value. The hardware increments each
 768 * timer by the beacon interval, when the local TSF converted to TU is equal
 769 * to the value stored in the timer.
 770 *
 771 * The reception of a beacon with the same BSSID can update the local HW TSF
 772 * at any time - this is something we can't avoid. If the TSF jumps to a
 773 * time which is later than the time stored in a timer, this timer will not
 774 * be updated until the TSF in TU wraps around at 16 bit (the size of the
 775 * timers) and reaches the time which is stored in the timer.
 776 *
 777 * The problem is that these timers are closely related to TIMER0 (NBTT) and
 778 * that they define a time "window". When the TSF jumps between two timers
 779 * (e.g. ATIM and NBTT), the one in the past will be left behind (not
 780 * updated), while the one in the future will be updated every beacon
 781 * interval. This causes the window to get larger, until the TSF wraps
 782 * around as described above and the timer which was left behind gets
 783 * updated again. But - because the beacon interval is usually not an exact
 784 * divisor of the size of the timers (16 bit), an unwanted "window" between
 785 * these timers has developed!
 786 *
 787 * This is especially important with the ATIM window, because during
 788 * the ATIM window only ATIM frames and no data frames are allowed to be
 789 * sent, which creates transmission pauses after each beacon. This symptom
 790 * has been described as "ramping ping" because ping times increase linearly
 791 * for some time and then drop down again. A wrong window on the DMA beacon
 792 * timer has the same effect, so we check for these two conditions.
 793 *
 794 * Returns true if O.K.
 795 */
 796bool
 797ath5k_hw_check_beacon_timers(struct ath5k_hw *ah, int intval)
 798{
 799	unsigned int nbtt, atim, dma;
 800
 801	nbtt = ath5k_hw_reg_read(ah, AR5K_TIMER0);
 802	atim = ath5k_hw_reg_read(ah, AR5K_TIMER3);
 803	dma = ath5k_hw_reg_read(ah, AR5K_TIMER1) >> 3;
 804
 805	/* NOTE: SWBA is different. Having a wrong window there does not
 806	 * stop us from sending data and this condition is caught by
 807	 * other means (SWBA interrupt) */
 808
 809	if (ath5k_check_timer_win(nbtt, atim, 1, intval) &&
 810	    ath5k_check_timer_win(dma, nbtt, AR5K_TUNE_DMA_BEACON_RESP,
 811				  intval))
 812		return true; /* O.K. */
 813	return false;
 814}
 815
 816/**
 817 * ath5k_hw_set_coverage_class() - Set IEEE 802.11 coverage class
 
 818 * @ah: The &struct ath5k_hw
 819 * @coverage_class: IEEE 802.11 coverage class number
 820 *
 821 * Sets IFS intervals and ACK/CTS timeouts for given coverage class.
 822 */
 823void
 824ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
 825{
 826	/* As defined by IEEE 802.11-2007 17.3.8.6 */
 827	int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
 828	int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
 829	int cts_timeout = ack_timeout;
 830
 831	ath5k_hw_set_ifs_intervals(ah, slot_time);
 832	ath5k_hw_set_ack_timeout(ah, ack_timeout);
 833	ath5k_hw_set_cts_timeout(ah, cts_timeout);
 834
 835	ah->ah_coverage_class = coverage_class;
 836}
 837
 838/***************************\
 839* Init/Start/Stop functions *
 840\***************************/
 841
 842/**
 843 * ath5k_hw_start_rx_pcu() - Start RX engine
 
 844 * @ah: The &struct ath5k_hw
 845 *
 846 * Starts RX engine on PCU so that hw can process RXed frames
 847 * (ACK etc).
 848 *
 849 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
 850 */
 851void
 852ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
 853{
 854	AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
 855}
 856
 857/**
 858 * ath5k_hw_stop_rx_pcu() - Stop RX engine
 
 859 * @ah: The &struct ath5k_hw
 860 *
 861 * Stops RX engine on PCU
 862 */
 863void
 864ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
 865{
 866	AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
 867}
 868
 869/**
 870 * ath5k_hw_set_opmode() - Set PCU operating mode
 
 871 * @ah: The &struct ath5k_hw
 872 * @op_mode: One of enum nl80211_iftype
 873 *
 874 * Configure PCU for the various operating modes (AP/STA etc)
 875 */
 876int
 877ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
 878{
 879	struct ath_common *common = ath5k_hw_common(ah);
 880	u32 pcu_reg, beacon_reg, low_id, high_id;
 881
 882	ATH5K_DBG(ah, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
 883
 884	/* Preserve rest settings */
 885	pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
 886	pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
 887			| AR5K_STA_ID1_KEYSRCH_MODE
 888			| (ah->ah_version == AR5K_AR5210 ?
 889			(AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
 890
 891	beacon_reg = 0;
 892
 893	switch (op_mode) {
 894	case NL80211_IFTYPE_ADHOC:
 895		pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
 896		beacon_reg |= AR5K_BCR_ADHOC;
 897		if (ah->ah_version == AR5K_AR5210)
 898			pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
 899		else
 900			AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
 901		break;
 902
 903	case NL80211_IFTYPE_AP:
 904	case NL80211_IFTYPE_MESH_POINT:
 905		pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
 906		beacon_reg |= AR5K_BCR_AP;
 907		if (ah->ah_version == AR5K_AR5210)
 908			pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
 909		else
 910			AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
 911		break;
 912
 913	case NL80211_IFTYPE_STATION:
 914		pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
 915			| (ah->ah_version == AR5K_AR5210 ?
 916				AR5K_STA_ID1_PWR_SV : 0);
 917		fallthrough;
 918	case NL80211_IFTYPE_MONITOR:
 919		pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
 920			| (ah->ah_version == AR5K_AR5210 ?
 921				AR5K_STA_ID1_NO_PSPOLL : 0);
 922		break;
 923
 924	default:
 925		return -EINVAL;
 926	}
 927
 928	/*
 929	 * Set PCU registers
 930	 */
 931	low_id = get_unaligned_le32(common->macaddr);
 932	high_id = get_unaligned_le16(common->macaddr + 4);
 933	ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
 934	ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
 935
 936	/*
 937	 * Set Beacon Control Register on 5210
 938	 */
 939	if (ah->ah_version == AR5K_AR5210)
 940		ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
 941
 942	return 0;
 943}
 944
 945/**
 946 * ath5k_hw_pcu_init() - Initialize PCU
 947 * @ah: The &struct ath5k_hw
 948 * @op_mode: One of enum nl80211_iftype
 949 *
 950 * This function is used to initialize PCU by setting current
 951 * operation mode and various other settings.
 952 */
 953void
 954ath5k_hw_pcu_init(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
 955{
 956	/* Set bssid and bssid mask */
 957	ath5k_hw_set_bssid(ah);
 958
 959	/* Set PCU config */
 960	ath5k_hw_set_opmode(ah, op_mode);
 961
 962	/* Write rate duration table only on AR5212 and if
 963	 * virtual interface has already been brought up
 964	 * XXX: rethink this after new mode changes to
 965	 * mac80211 are integrated */
 966	if (ah->ah_version == AR5K_AR5212 &&
 967		ah->nvifs)
 968		ath5k_hw_write_rate_duration(ah);
 969
 970	/* Set RSSI/BRSSI thresholds
 971	 *
 972	 * Note: If we decide to set this value
 973	 * dynamically, have in mind that when AR5K_RSSI_THR
 974	 * register is read it might return 0x40 if we haven't
 975	 * wrote anything to it plus BMISS RSSI threshold is zeroed.
 976	 * So doing a save/restore procedure here isn't the right
 977	 * choice. Instead store it on ath5k_hw */
 978	ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
 979				AR5K_TUNE_BMISS_THRES <<
 980				AR5K_RSSI_THR_BMISS_S),
 981				AR5K_RSSI_THR);
 982
 983	/* MIC QoS support */
 984	if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
 985		ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
 986		ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
 987	}
 988
 989	/* QoS NOACK Policy */
 990	if (ah->ah_version == AR5K_AR5212) {
 991		ath5k_hw_reg_write(ah,
 992			AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
 993			AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET)  |
 994			AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
 995			AR5K_QOS_NOACK);
 996	}
 997
 998	/* Restore slot time and ACK timeouts */
 999	if (ah->ah_coverage_class > 0)
1000		ath5k_hw_set_coverage_class(ah, ah->ah_coverage_class);
1001
1002	/* Set ACK bitrate mode (see ack_rates_high) */
1003	if (ah->ah_version == AR5K_AR5212) {
1004		u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
1005		if (ah->ah_ack_bitrate_high)
1006			AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
1007		else
1008			AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
1009	}
1010	return;
1011}