xref: /linux/drivers/net/wireless/ath/ath5k/pcu.c (revision 2111ac0d888767999c7dd6d1309dcc1fb8012022)
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 * Generic functions *
36 \*******************/
37 
38 /**
39  * ath5k_hw_set_opmode - Set PCU operating mode
40  *
41  * @ah: The &struct ath5k_hw
42  * @op_mode: &enum nl80211_iftype operating mode
43  *
44  * Initialize PCU for the various operating modes (AP/STA etc)
45  */
46 int ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
47 {
48 	struct ath_common *common = ath5k_hw_common(ah);
49 	u32 pcu_reg, beacon_reg, low_id, high_id;
50 
51 	ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
52 
53 	/* Preserve rest settings */
54 	pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
55 	pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
56 			| AR5K_STA_ID1_KEYSRCH_MODE
57 			| (ah->ah_version == AR5K_AR5210 ?
58 			(AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
59 
60 	beacon_reg = 0;
61 
62 	ATH5K_TRACE(ah->ah_sc);
63 
64 	switch (op_mode) {
65 	case NL80211_IFTYPE_ADHOC:
66 		pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
67 		beacon_reg |= AR5K_BCR_ADHOC;
68 		if (ah->ah_version == AR5K_AR5210)
69 			pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
70 		else
71 			AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
72 		break;
73 
74 	case NL80211_IFTYPE_AP:
75 	case NL80211_IFTYPE_MESH_POINT:
76 		pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
77 		beacon_reg |= AR5K_BCR_AP;
78 		if (ah->ah_version == AR5K_AR5210)
79 			pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
80 		else
81 			AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
82 		break;
83 
84 	case NL80211_IFTYPE_STATION:
85 		pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
86 			| (ah->ah_version == AR5K_AR5210 ?
87 				AR5K_STA_ID1_PWR_SV : 0);
88 	case NL80211_IFTYPE_MONITOR:
89 		pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
90 			| (ah->ah_version == AR5K_AR5210 ?
91 				AR5K_STA_ID1_NO_PSPOLL : 0);
92 		break;
93 
94 	default:
95 		return -EINVAL;
96 	}
97 
98 	/*
99 	 * Set PCU registers
100 	 */
101 	low_id = get_unaligned_le32(common->macaddr);
102 	high_id = get_unaligned_le16(common->macaddr + 4);
103 	ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
104 	ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
105 
106 	/*
107 	 * Set Beacon Control Register on 5210
108 	 */
109 	if (ah->ah_version == AR5K_AR5210)
110 		ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
111 
112 	return 0;
113 }
114 
115 /**
116  * ath5k_hw_update - Update MIB counters (mac layer statistics)
117  *
118  * @ah: The &struct ath5k_hw
119  *
120  * Reads MIB counters from PCU and updates sw statistics. Is called after a
121  * MIB interrupt, because one of these counters might have reached their maximum
122  * and triggered the MIB interrupt, to let us read and clear the counter.
123  *
124  * Is called in interrupt context!
125  */
126 void ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
127 {
128 	struct ath5k_statistics *stats = &ah->ah_sc->stats;
129 
130 	/* Read-And-Clear */
131 	stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
132 	stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
133 	stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
134 	stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
135 	stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
136 }
137 
138 /**
139  * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
140  *
141  * @ah: The &struct ath5k_hw
142  * @high: Flag to determine if we want to use high transmition rate
143  * for ACKs or not
144  *
145  * If high flag is set, we tell hw to use a set of control rates based on
146  * the current transmition rate (check out control_rates array inside reset.c).
147  * If not hw just uses the lowest rate available for the current modulation
148  * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
149  */
150 void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
151 {
152 	if (ah->ah_version != AR5K_AR5212)
153 		return;
154 	else {
155 		u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
156 		if (high)
157 			AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
158 		else
159 			AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
160 	}
161 }
162 
163 
164 /******************\
165 * ACK/CTS Timeouts *
166 \******************/
167 
168 /**
169  * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
170  *
171  * @ah: The &struct ath5k_hw
172  * @timeout: Timeout in usec
173  */
174 static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
175 {
176 	ATH5K_TRACE(ah->ah_sc);
177 	if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
178 			<= timeout)
179 		return -EINVAL;
180 
181 	AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
182 		ath5k_hw_htoclock(ah, timeout));
183 
184 	return 0;
185 }
186 
187 /**
188  * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
189  *
190  * @ah: The &struct ath5k_hw
191  * @timeout: Timeout in usec
192  */
193 static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
194 {
195 	ATH5K_TRACE(ah->ah_sc);
196 	if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
197 			<= timeout)
198 		return -EINVAL;
199 
200 	AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
201 			ath5k_hw_htoclock(ah, timeout));
202 
203 	return 0;
204 }
205 
206 /**
207  * ath5k_hw_htoclock - Translate usec to hw clock units
208  *
209  * @ah: The &struct ath5k_hw
210  * @usec: value in microseconds
211  */
212 unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
213 {
214 	return usec * ath5k_hw_get_clockrate(ah);
215 }
216 
217 /**
218  * ath5k_hw_clocktoh - Translate hw clock units to usec
219  * @clock: value in hw clock units
220  */
221 unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
222 {
223 	return clock / ath5k_hw_get_clockrate(ah);
224 }
225 
226 /**
227  * ath5k_hw_get_clockrate - Get the clock rate for current mode
228  *
229  * @ah: The &struct ath5k_hw
230  */
231 unsigned int ath5k_hw_get_clockrate(struct ath5k_hw *ah)
232 {
233 	struct ieee80211_channel *channel = ah->ah_current_channel;
234 	int clock;
235 
236 	if (channel->hw_value & CHANNEL_5GHZ)
237 		clock = 40; /* 802.11a */
238 	else if (channel->hw_value & CHANNEL_CCK)
239 		clock = 22; /* 802.11b */
240 	else
241 		clock = 44; /* 802.11g */
242 
243 	/* Clock rate in turbo modes is twice the normal rate */
244 	if (channel->hw_value & CHANNEL_TURBO)
245 		clock *= 2;
246 
247 	return clock;
248 }
249 
250 /**
251  * ath5k_hw_get_default_slottime - Get the default slot time for current mode
252  *
253  * @ah: The &struct ath5k_hw
254  */
255 static unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
256 {
257 	struct ieee80211_channel *channel = ah->ah_current_channel;
258 
259 	if (channel->hw_value & CHANNEL_TURBO)
260 		return 6; /* both turbo modes */
261 
262 	if (channel->hw_value & CHANNEL_CCK)
263 		return 20; /* 802.11b */
264 
265 	return 9; /* 802.11 a/g */
266 }
267 
268 /**
269  * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
270  *
271  * @ah: The &struct ath5k_hw
272  */
273 static unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
274 {
275 	struct ieee80211_channel *channel = ah->ah_current_channel;
276 
277 	if (channel->hw_value & CHANNEL_TURBO)
278 		return 8; /* both turbo modes */
279 
280 	if (channel->hw_value & CHANNEL_5GHZ)
281 		return 16; /* 802.11a */
282 
283 	return 10; /* 802.11 b/g */
284 }
285 
286 /**
287  * ath5k_hw_set_lladdr - Set station id
288  *
289  * @ah: The &struct ath5k_hw
290  * @mac: The card's mac address
291  *
292  * Set station id on hw using the provided mac address
293  */
294 int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
295 {
296 	struct ath_common *common = ath5k_hw_common(ah);
297 	u32 low_id, high_id;
298 	u32 pcu_reg;
299 
300 	ATH5K_TRACE(ah->ah_sc);
301 	/* Set new station ID */
302 	memcpy(common->macaddr, mac, ETH_ALEN);
303 
304 	pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
305 
306 	low_id = get_unaligned_le32(mac);
307 	high_id = get_unaligned_le16(mac + 4);
308 
309 	ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
310 	ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
311 
312 	return 0;
313 }
314 
315 /**
316  * ath5k_hw_set_associd - Set BSSID for association
317  *
318  * @ah: The &struct ath5k_hw
319  * @bssid: BSSID
320  * @assoc_id: Assoc id
321  *
322  * Sets the BSSID which trigers the "SME Join" operation
323  */
324 void ath5k_hw_set_associd(struct ath5k_hw *ah)
325 {
326 	struct ath_common *common = ath5k_hw_common(ah);
327 	u16 tim_offset = 0;
328 
329 	/*
330 	 * Set simple BSSID mask on 5212
331 	 */
332 	if (ah->ah_version == AR5K_AR5212)
333 		ath_hw_setbssidmask(common);
334 
335 	/*
336 	 * Set BSSID which triggers the "SME Join" operation
337 	 */
338 	ath5k_hw_reg_write(ah,
339 			   get_unaligned_le32(common->curbssid),
340 			   AR5K_BSS_ID0);
341 	ath5k_hw_reg_write(ah,
342 			   get_unaligned_le16(common->curbssid + 4) |
343 			   ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
344 			   AR5K_BSS_ID1);
345 
346 	if (common->curaid == 0) {
347 		ath5k_hw_disable_pspoll(ah);
348 		return;
349 	}
350 
351 	AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
352 			    tim_offset ? tim_offset + 4 : 0);
353 
354 	ath5k_hw_enable_pspoll(ah, NULL, 0);
355 }
356 
357 void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
358 {
359 	struct ath_common *common = ath5k_hw_common(ah);
360 	ATH5K_TRACE(ah->ah_sc);
361 
362 	/* Cache bssid mask so that we can restore it
363 	 * on reset */
364 	memcpy(common->bssidmask, mask, ETH_ALEN);
365 	if (ah->ah_version == AR5K_AR5212)
366 		ath_hw_setbssidmask(common);
367 }
368 
369 /************\
370 * RX Control *
371 \************/
372 
373 /**
374  * ath5k_hw_start_rx_pcu - Start RX engine
375  *
376  * @ah: The &struct ath5k_hw
377  *
378  * Starts RX engine on PCU so that hw can process RXed frames
379  * (ACK etc).
380  *
381  * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
382  */
383 void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
384 {
385 	ATH5K_TRACE(ah->ah_sc);
386 	AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
387 }
388 
389 /**
390  * at5k_hw_stop_rx_pcu - Stop RX engine
391  *
392  * @ah: The &struct ath5k_hw
393  *
394  * Stops RX engine on PCU
395  *
396  * TODO: Detach ANI here
397  */
398 void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
399 {
400 	ATH5K_TRACE(ah->ah_sc);
401 	AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
402 }
403 
404 /*
405  * Set multicast filter
406  */
407 void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
408 {
409 	ATH5K_TRACE(ah->ah_sc);
410 	/* Set the multicat filter */
411 	ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
412 	ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
413 }
414 
415 /**
416  * ath5k_hw_get_rx_filter - Get current rx filter
417  *
418  * @ah: The &struct ath5k_hw
419  *
420  * Returns the RX filter by reading rx filter and
421  * phy error filter registers. RX filter is used
422  * to set the allowed frame types that PCU will accept
423  * and pass to the driver. For a list of frame types
424  * check out reg.h.
425  */
426 u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
427 {
428 	u32 data, filter = 0;
429 
430 	ATH5K_TRACE(ah->ah_sc);
431 	filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
432 
433 	/*Radar detection for 5212*/
434 	if (ah->ah_version == AR5K_AR5212) {
435 		data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
436 
437 		if (data & AR5K_PHY_ERR_FIL_RADAR)
438 			filter |= AR5K_RX_FILTER_RADARERR;
439 		if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
440 			filter |= AR5K_RX_FILTER_PHYERR;
441 	}
442 
443 	return filter;
444 }
445 
446 /**
447  * ath5k_hw_set_rx_filter - Set rx filter
448  *
449  * @ah: The &struct ath5k_hw
450  * @filter: RX filter mask (see reg.h)
451  *
452  * Sets RX filter register and also handles PHY error filter
453  * register on 5212 and newer chips so that we have proper PHY
454  * error reporting.
455  */
456 void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
457 {
458 	u32 data = 0;
459 
460 	ATH5K_TRACE(ah->ah_sc);
461 
462 	/* Set PHY error filter register on 5212*/
463 	if (ah->ah_version == AR5K_AR5212) {
464 		if (filter & AR5K_RX_FILTER_RADARERR)
465 			data |= AR5K_PHY_ERR_FIL_RADAR;
466 		if (filter & AR5K_RX_FILTER_PHYERR)
467 			data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
468 	}
469 
470 	/*
471 	 * The AR5210 uses promiscous mode to detect radar activity
472 	 */
473 	if (ah->ah_version == AR5K_AR5210 &&
474 			(filter & AR5K_RX_FILTER_RADARERR)) {
475 		filter &= ~AR5K_RX_FILTER_RADARERR;
476 		filter |= AR5K_RX_FILTER_PROM;
477 	}
478 
479 	/*Zero length DMA (phy error reporting) */
480 	if (data)
481 		AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
482 	else
483 		AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
484 
485 	/*Write RX Filter register*/
486 	ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
487 
488 	/*Write PHY error filter register on 5212*/
489 	if (ah->ah_version == AR5K_AR5212)
490 		ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
491 
492 }
493 
494 
495 /****************\
496 * Beacon control *
497 \****************/
498 
499 /**
500  * ath5k_hw_get_tsf64 - Get the full 64bit TSF
501  *
502  * @ah: The &struct ath5k_hw
503  *
504  * Returns the current TSF
505  */
506 u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
507 {
508 	u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
509 	ATH5K_TRACE(ah->ah_sc);
510 
511 	return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
512 }
513 
514 /**
515  * ath5k_hw_set_tsf64 - Set a new 64bit TSF
516  *
517  * @ah: The &struct ath5k_hw
518  * @tsf64: The new 64bit TSF
519  *
520  * Sets the new TSF
521  */
522 void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
523 {
524 	ATH5K_TRACE(ah->ah_sc);
525 
526 	ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
527 	ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
528 }
529 
530 /**
531  * ath5k_hw_reset_tsf - Force a TSF reset
532  *
533  * @ah: The &struct ath5k_hw
534  *
535  * Forces a TSF reset on PCU
536  */
537 void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
538 {
539 	u32 val;
540 
541 	ATH5K_TRACE(ah->ah_sc);
542 
543 	val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
544 
545 	/*
546 	 * Each write to the RESET_TSF bit toggles a hardware internal
547 	 * signal to reset TSF, but if left high it will cause a TSF reset
548 	 * on the next chip reset as well.  Thus we always write the value
549 	 * twice to clear the signal.
550 	 */
551 	ath5k_hw_reg_write(ah, val, AR5K_BEACON);
552 	ath5k_hw_reg_write(ah, val, AR5K_BEACON);
553 }
554 
555 /*
556  * Initialize beacon timers
557  */
558 void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
559 {
560 	u32 timer1, timer2, timer3;
561 
562 	ATH5K_TRACE(ah->ah_sc);
563 	/*
564 	 * Set the additional timers by mode
565 	 */
566 	switch (ah->ah_sc->opmode) {
567 	case NL80211_IFTYPE_MONITOR:
568 	case NL80211_IFTYPE_STATION:
569 		/* In STA mode timer1 is used as next wakeup
570 		 * timer and timer2 as next CFP duration start
571 		 * timer. Both in 1/8TUs. */
572 		/* TODO: PCF handling */
573 		if (ah->ah_version == AR5K_AR5210) {
574 			timer1 = 0xffffffff;
575 			timer2 = 0xffffffff;
576 		} else {
577 			timer1 = 0x0000ffff;
578 			timer2 = 0x0007ffff;
579 		}
580 		/* Mark associated AP as PCF incapable for now */
581 		AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
582 		break;
583 	case NL80211_IFTYPE_ADHOC:
584 		AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
585 	default:
586 		/* On non-STA modes timer1 is used as next DMA
587 		 * beacon alert (DBA) timer and timer2 as next
588 		 * software beacon alert. Both in 1/8TUs. */
589 		timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
590 		timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
591 		break;
592 	}
593 
594 	/* Timer3 marks the end of our ATIM window
595 	 * a zero length window is not allowed because
596 	 * we 'll get no beacons */
597 	timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
598 
599 	/*
600 	 * Set the beacon register and enable all timers.
601 	 */
602 	/* When in AP or Mesh Point mode zero timer0 to start TSF */
603 	if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
604 	    ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
605 		ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
606 
607 	ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
608 	ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
609 	ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
610 	ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
611 
612 	/* Force a TSF reset if requested and enable beacons */
613 	if (interval & AR5K_BEACON_RESET_TSF)
614 		ath5k_hw_reset_tsf(ah);
615 
616 	ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
617 					AR5K_BEACON_ENABLE),
618 						AR5K_BEACON);
619 
620 	/* Flush any pending BMISS interrupts on ISR by
621 	 * performing a clear-on-write operation on PISR
622 	 * register for the BMISS bit (writing a bit on
623 	 * ISR togles a reset for that bit and leaves
624 	 * the rest bits intact) */
625 	if (ah->ah_version == AR5K_AR5210)
626 		ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
627 	else
628 		ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
629 
630 	/* TODO: Set enchanced sleep registers on AR5212
631 	 * based on vif->bss_conf params, until then
632 	 * disable power save reporting.*/
633 	AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
634 
635 }
636 
637 
638 /*********************\
639 * Key table functions *
640 \*********************/
641 
642 /*
643  * Reset a key entry on the table
644  */
645 int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
646 {
647 	unsigned int i, type;
648 	u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
649 
650 	ATH5K_TRACE(ah->ah_sc);
651 	AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
652 
653 	type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
654 
655 	for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
656 		ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
657 
658 	/* Reset associated MIC entry if TKIP
659 	 * is enabled located at offset (entry + 64) */
660 	if (type == AR5K_KEYTABLE_TYPE_TKIP) {
661 		AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
662 		for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
663 			ath5k_hw_reg_write(ah, 0,
664 				AR5K_KEYTABLE_OFF(micentry, i));
665 	}
666 
667 	/*
668 	 * Set NULL encryption on AR5212+
669 	 *
670 	 * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
671 	 *       AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
672 	 *
673 	 * Note2: Windows driver (ndiswrapper) sets this to
674 	 *        0x00000714 instead of 0x00000007
675 	 */
676 	if (ah->ah_version >= AR5K_AR5211) {
677 		ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
678 				AR5K_KEYTABLE_TYPE(entry));
679 
680 		if (type == AR5K_KEYTABLE_TYPE_TKIP) {
681 			ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
682 				AR5K_KEYTABLE_TYPE(micentry));
683 		}
684 	}
685 
686 	return 0;
687 }
688 
689 static
690 int ath5k_keycache_type(const struct ieee80211_key_conf *key)
691 {
692 	switch (key->alg) {
693 	case ALG_TKIP:
694 		return AR5K_KEYTABLE_TYPE_TKIP;
695 	case ALG_CCMP:
696 		return AR5K_KEYTABLE_TYPE_CCM;
697 	case ALG_WEP:
698 		if (key->keylen == WLAN_KEY_LEN_WEP40)
699 			return AR5K_KEYTABLE_TYPE_40;
700 		else if (key->keylen == WLAN_KEY_LEN_WEP104)
701 			return AR5K_KEYTABLE_TYPE_104;
702 		return -EINVAL;
703 	default:
704 		return -EINVAL;
705 	}
706 	return -EINVAL;
707 }
708 
709 /*
710  * Set a key entry on the table
711  */
712 int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
713 		const struct ieee80211_key_conf *key, const u8 *mac)
714 {
715 	unsigned int i;
716 	int keylen;
717 	__le32 key_v[5] = {};
718 	__le32 key0 = 0, key1 = 0;
719 	__le32 *rxmic, *txmic;
720 	int keytype;
721 	u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
722 	bool is_tkip;
723 	const u8 *key_ptr;
724 
725 	ATH5K_TRACE(ah->ah_sc);
726 
727 	is_tkip = (key->alg == ALG_TKIP);
728 
729 	/*
730 	 * key->keylen comes in from mac80211 in bytes.
731 	 * TKIP is 128 bit + 128 bit mic
732 	 */
733 	keylen = (is_tkip) ? (128 / 8) : key->keylen;
734 
735 	if (entry > AR5K_KEYTABLE_SIZE ||
736 		(is_tkip && micentry > AR5K_KEYTABLE_SIZE))
737 		return -EOPNOTSUPP;
738 
739 	if (unlikely(keylen > 16))
740 		return -EOPNOTSUPP;
741 
742 	keytype = ath5k_keycache_type(key);
743 	if (keytype < 0)
744 		return keytype;
745 
746 	/*
747 	 * each key block is 6 bytes wide, written as pairs of
748 	 * alternating 32 and 16 bit le values.
749 	 */
750 	key_ptr = key->key;
751 	for (i = 0; keylen >= 6; keylen -= 6) {
752 		memcpy(&key_v[i], key_ptr, 6);
753 		i += 2;
754 		key_ptr += 6;
755 	}
756 	if (keylen)
757 		memcpy(&key_v[i], key_ptr, keylen);
758 
759 	/* intentionally corrupt key until mic is installed */
760 	if (is_tkip) {
761 		key0 = key_v[0] = ~key_v[0];
762 		key1 = key_v[1] = ~key_v[1];
763 	}
764 
765 	for (i = 0; i < ARRAY_SIZE(key_v); i++)
766 		ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
767 				AR5K_KEYTABLE_OFF(entry, i));
768 
769 	ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
770 
771 	if (is_tkip) {
772 		/* Install rx/tx MIC */
773 		rxmic = (__le32 *) &key->key[16];
774 		txmic = (__le32 *) &key->key[24];
775 
776 		if (ah->ah_combined_mic) {
777 			key_v[0] = rxmic[0];
778 			key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
779 			key_v[2] = rxmic[1];
780 			key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
781 			key_v[4] = txmic[1];
782 		} else {
783 			key_v[0] = rxmic[0];
784 			key_v[1] = 0;
785 			key_v[2] = rxmic[1];
786 			key_v[3] = 0;
787 			key_v[4] = 0;
788 		}
789 		for (i = 0; i < ARRAY_SIZE(key_v); i++)
790 			ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
791 				AR5K_KEYTABLE_OFF(micentry, i));
792 
793 		ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
794 			AR5K_KEYTABLE_TYPE(micentry));
795 		ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
796 		ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
797 
798 		/* restore first 2 words of key */
799 		ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
800 			AR5K_KEYTABLE_OFF(entry, 0));
801 		ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
802 			AR5K_KEYTABLE_OFF(entry, 1));
803 	}
804 
805 	return ath5k_hw_set_key_lladdr(ah, entry, mac);
806 }
807 
808 int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
809 {
810 	u32 low_id, high_id;
811 
812 	ATH5K_TRACE(ah->ah_sc);
813 	 /* Invalid entry (key table overflow) */
814 	AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
815 
816 	/*
817 	 * MAC may be NULL if it's a broadcast key. In this case no need to
818 	 * to compute get_unaligned_le32 and get_unaligned_le16 as we
819 	 * already know it.
820 	 */
821 	if (!mac) {
822 		low_id = 0xffffffff;
823 		high_id = 0xffff | AR5K_KEYTABLE_VALID;
824 	} else {
825 		low_id = get_unaligned_le32(mac);
826 		high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
827 	}
828 
829 	ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
830 	ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
831 
832 	return 0;
833 }
834 
835 /**
836  * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
837  *
838  * @ah: The &struct ath5k_hw
839  * @coverage_class: IEEE 802.11 coverage class number
840  *
841  * Sets slot time, ACK timeout and CTS timeout for given coverage class.
842  */
843 void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
844 {
845 	/* As defined by IEEE 802.11-2007 17.3.8.6 */
846 	int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
847 	int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
848 	int cts_timeout = ack_timeout;
849 
850 	ath5k_hw_set_slot_time(ah, slot_time);
851 	ath5k_hw_set_ack_timeout(ah, ack_timeout);
852 	ath5k_hw_set_cts_timeout(ah, cts_timeout);
853 
854 	ah->ah_coverage_class = coverage_class;
855 }
856