pcu.c 28 KB

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