pcu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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. #include "base.h"
  30. /*******************\
  31. * Generic functions *
  32. \*******************/
  33. /**
  34. * ath5k_hw_set_opmode - Set PCU operating mode
  35. *
  36. * @ah: The &struct ath5k_hw
  37. * @op_mode: &enum nl80211_iftype operating mode
  38. *
  39. * Initialize PCU for the various operating modes (AP/STA etc)
  40. */
  41. int ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
  42. {
  43. struct ath_common *common = ath5k_hw_common(ah);
  44. u32 pcu_reg, beacon_reg, low_id, high_id;
  45. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
  46. /* Preserve rest settings */
  47. pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
  48. pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
  49. | AR5K_STA_ID1_KEYSRCH_MODE
  50. | (ah->ah_version == AR5K_AR5210 ?
  51. (AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
  52. beacon_reg = 0;
  53. switch (op_mode) {
  54. case NL80211_IFTYPE_ADHOC:
  55. pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
  56. beacon_reg |= AR5K_BCR_ADHOC;
  57. if (ah->ah_version == AR5K_AR5210)
  58. pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
  59. else
  60. AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
  61. break;
  62. case NL80211_IFTYPE_AP:
  63. case NL80211_IFTYPE_MESH_POINT:
  64. pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
  65. beacon_reg |= AR5K_BCR_AP;
  66. if (ah->ah_version == AR5K_AR5210)
  67. pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
  68. else
  69. AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
  70. break;
  71. case NL80211_IFTYPE_STATION:
  72. pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
  73. | (ah->ah_version == AR5K_AR5210 ?
  74. AR5K_STA_ID1_PWR_SV : 0);
  75. case NL80211_IFTYPE_MONITOR:
  76. pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
  77. | (ah->ah_version == AR5K_AR5210 ?
  78. AR5K_STA_ID1_NO_PSPOLL : 0);
  79. break;
  80. default:
  81. return -EINVAL;
  82. }
  83. /*
  84. * Set PCU registers
  85. */
  86. low_id = get_unaligned_le32(common->macaddr);
  87. high_id = get_unaligned_le16(common->macaddr + 4);
  88. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  89. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  90. /*
  91. * Set Beacon Control Register on 5210
  92. */
  93. if (ah->ah_version == AR5K_AR5210)
  94. ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
  95. return 0;
  96. }
  97. /**
  98. * ath5k_hw_update - Update MIB counters (mac layer statistics)
  99. *
  100. * @ah: The &struct ath5k_hw
  101. *
  102. * Reads MIB counters from PCU and updates sw statistics. Is called after a
  103. * MIB interrupt, because one of these counters might have reached their maximum
  104. * and triggered the MIB interrupt, to let us read and clear the counter.
  105. *
  106. * Is called in interrupt context!
  107. */
  108. void ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
  109. {
  110. struct ath5k_statistics *stats = &ah->ah_sc->stats;
  111. /* Read-And-Clear */
  112. stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
  113. stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
  114. stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
  115. stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
  116. stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
  117. }
  118. /**
  119. * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
  120. *
  121. * @ah: The &struct ath5k_hw
  122. * @high: Flag to determine if we want to use high transmission rate
  123. * for ACKs or not
  124. *
  125. * If high flag is set, we tell hw to use a set of control rates based on
  126. * the current transmission rate (check out control_rates array inside reset.c).
  127. * If not hw just uses the lowest rate available for the current modulation
  128. * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
  129. */
  130. void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
  131. {
  132. if (ah->ah_version != AR5K_AR5212)
  133. return;
  134. else {
  135. u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
  136. if (high)
  137. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
  138. else
  139. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
  140. }
  141. }
  142. /******************\
  143. * ACK/CTS Timeouts *
  144. \******************/
  145. /**
  146. * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
  147. *
  148. * @ah: The &struct ath5k_hw
  149. * @timeout: Timeout in usec
  150. */
  151. static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
  152. {
  153. if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
  154. <= timeout)
  155. return -EINVAL;
  156. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
  157. ath5k_hw_htoclock(ah, timeout));
  158. return 0;
  159. }
  160. /**
  161. * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
  162. *
  163. * @ah: The &struct ath5k_hw
  164. * @timeout: Timeout in usec
  165. */
  166. static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
  167. {
  168. if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
  169. <= timeout)
  170. return -EINVAL;
  171. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
  172. ath5k_hw_htoclock(ah, timeout));
  173. return 0;
  174. }
  175. /**
  176. * ath5k_hw_htoclock - Translate usec to hw clock units
  177. *
  178. * @ah: The &struct ath5k_hw
  179. * @usec: value in microseconds
  180. */
  181. unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
  182. {
  183. struct ath_common *common = ath5k_hw_common(ah);
  184. return usec * common->clockrate;
  185. }
  186. /**
  187. * ath5k_hw_clocktoh - Translate hw clock units to usec
  188. * @clock: value in hw clock units
  189. */
  190. unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
  191. {
  192. struct ath_common *common = ath5k_hw_common(ah);
  193. return clock / common->clockrate;
  194. }
  195. /**
  196. * ath5k_hw_set_clockrate - Set common->clockrate for the current channel
  197. *
  198. * @ah: The &struct ath5k_hw
  199. */
  200. void ath5k_hw_set_clockrate(struct ath5k_hw *ah)
  201. {
  202. struct ieee80211_channel *channel = ah->ah_current_channel;
  203. struct ath_common *common = ath5k_hw_common(ah);
  204. int clock;
  205. if (channel->hw_value & CHANNEL_5GHZ)
  206. clock = 40; /* 802.11a */
  207. else if (channel->hw_value & CHANNEL_CCK)
  208. clock = 22; /* 802.11b */
  209. else
  210. clock = 44; /* 802.11g */
  211. /* Clock rate in turbo modes is twice the normal rate */
  212. if (channel->hw_value & CHANNEL_TURBO)
  213. clock *= 2;
  214. common->clockrate = clock;
  215. }
  216. /**
  217. * ath5k_hw_get_default_slottime - Get the default slot time for current mode
  218. *
  219. * @ah: The &struct ath5k_hw
  220. */
  221. static unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
  222. {
  223. struct ieee80211_channel *channel = ah->ah_current_channel;
  224. if (channel->hw_value & CHANNEL_TURBO)
  225. return 6; /* both turbo modes */
  226. if (channel->hw_value & CHANNEL_CCK)
  227. return 20; /* 802.11b */
  228. return 9; /* 802.11 a/g */
  229. }
  230. /**
  231. * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
  232. *
  233. * @ah: The &struct ath5k_hw
  234. */
  235. static unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
  236. {
  237. struct ieee80211_channel *channel = ah->ah_current_channel;
  238. if (channel->hw_value & CHANNEL_TURBO)
  239. return 8; /* both turbo modes */
  240. if (channel->hw_value & CHANNEL_5GHZ)
  241. return 16; /* 802.11a */
  242. return 10; /* 802.11 b/g */
  243. }
  244. /**
  245. * ath5k_hw_set_lladdr - Set station id
  246. *
  247. * @ah: The &struct ath5k_hw
  248. * @mac: The card's mac address
  249. *
  250. * Set station id on hw using the provided mac address
  251. */
  252. int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
  253. {
  254. struct ath_common *common = ath5k_hw_common(ah);
  255. u32 low_id, high_id;
  256. u32 pcu_reg;
  257. /* Set new station ID */
  258. memcpy(common->macaddr, mac, ETH_ALEN);
  259. pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
  260. low_id = get_unaligned_le32(mac);
  261. high_id = get_unaligned_le16(mac + 4);
  262. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  263. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  264. return 0;
  265. }
  266. /**
  267. * ath5k_hw_set_bssid - Set current BSSID on hw
  268. *
  269. * @ah: The &struct ath5k_hw
  270. *
  271. * Sets the current BSSID and BSSID mask we have from the
  272. * common struct into the hardware
  273. */
  274. void ath5k_hw_set_bssid(struct ath5k_hw *ah)
  275. {
  276. struct ath_common *common = ath5k_hw_common(ah);
  277. u16 tim_offset = 0;
  278. /*
  279. * Set BSSID mask on 5212
  280. */
  281. if (ah->ah_version == AR5K_AR5212)
  282. ath_hw_setbssidmask(common);
  283. /*
  284. * Set BSSID
  285. */
  286. ath5k_hw_reg_write(ah,
  287. get_unaligned_le32(common->curbssid),
  288. AR5K_BSS_ID0);
  289. ath5k_hw_reg_write(ah,
  290. get_unaligned_le16(common->curbssid + 4) |
  291. ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
  292. AR5K_BSS_ID1);
  293. if (common->curaid == 0) {
  294. ath5k_hw_disable_pspoll(ah);
  295. return;
  296. }
  297. AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
  298. tim_offset ? tim_offset + 4 : 0);
  299. ath5k_hw_enable_pspoll(ah, NULL, 0);
  300. }
  301. void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
  302. {
  303. struct ath_common *common = ath5k_hw_common(ah);
  304. /* Cache bssid mask so that we can restore it
  305. * on reset */
  306. memcpy(common->bssidmask, mask, ETH_ALEN);
  307. if (ah->ah_version == AR5K_AR5212)
  308. ath_hw_setbssidmask(common);
  309. }
  310. /************\
  311. * RX Control *
  312. \************/
  313. /**
  314. * ath5k_hw_start_rx_pcu - Start RX engine
  315. *
  316. * @ah: The &struct ath5k_hw
  317. *
  318. * Starts RX engine on PCU so that hw can process RXed frames
  319. * (ACK etc).
  320. *
  321. * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
  322. */
  323. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  324. {
  325. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  326. }
  327. /**
  328. * at5k_hw_stop_rx_pcu - Stop RX engine
  329. *
  330. * @ah: The &struct ath5k_hw
  331. *
  332. * Stops RX engine on PCU
  333. *
  334. * TODO: Detach ANI here
  335. */
  336. void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
  337. {
  338. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  339. }
  340. /*
  341. * Set multicast filter
  342. */
  343. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  344. {
  345. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  346. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  347. }
  348. /**
  349. * ath5k_hw_get_rx_filter - Get current rx filter
  350. *
  351. * @ah: The &struct ath5k_hw
  352. *
  353. * Returns the RX filter by reading rx filter and
  354. * phy error filter registers. RX filter is used
  355. * to set the allowed frame types that PCU will accept
  356. * and pass to the driver. For a list of frame types
  357. * check out reg.h.
  358. */
  359. u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
  360. {
  361. u32 data, filter = 0;
  362. filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
  363. /*Radar detection for 5212*/
  364. if (ah->ah_version == AR5K_AR5212) {
  365. data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
  366. if (data & AR5K_PHY_ERR_FIL_RADAR)
  367. filter |= AR5K_RX_FILTER_RADARERR;
  368. if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
  369. filter |= AR5K_RX_FILTER_PHYERR;
  370. }
  371. return filter;
  372. }
  373. /**
  374. * ath5k_hw_set_rx_filter - Set rx filter
  375. *
  376. * @ah: The &struct ath5k_hw
  377. * @filter: RX filter mask (see reg.h)
  378. *
  379. * Sets RX filter register and also handles PHY error filter
  380. * register on 5212 and newer chips so that we have proper PHY
  381. * error reporting.
  382. */
  383. void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
  384. {
  385. u32 data = 0;
  386. /* Set PHY error filter register on 5212*/
  387. if (ah->ah_version == AR5K_AR5212) {
  388. if (filter & AR5K_RX_FILTER_RADARERR)
  389. data |= AR5K_PHY_ERR_FIL_RADAR;
  390. if (filter & AR5K_RX_FILTER_PHYERR)
  391. data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
  392. }
  393. /*
  394. * The AR5210 uses promiscous mode to detect radar activity
  395. */
  396. if (ah->ah_version == AR5K_AR5210 &&
  397. (filter & AR5K_RX_FILTER_RADARERR)) {
  398. filter &= ~AR5K_RX_FILTER_RADARERR;
  399. filter |= AR5K_RX_FILTER_PROM;
  400. }
  401. /*Zero length DMA (phy error reporting) */
  402. if (data)
  403. AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  404. else
  405. AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  406. /*Write RX Filter register*/
  407. ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
  408. /*Write PHY error filter register on 5212*/
  409. if (ah->ah_version == AR5K_AR5212)
  410. ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
  411. }
  412. /****************\
  413. * Beacon control *
  414. \****************/
  415. #define ATH5K_MAX_TSF_READ 10
  416. /**
  417. * ath5k_hw_get_tsf64 - Get the full 64bit TSF
  418. *
  419. * @ah: The &struct ath5k_hw
  420. *
  421. * Returns the current TSF
  422. */
  423. u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
  424. {
  425. u32 tsf_lower, tsf_upper1, tsf_upper2;
  426. int i;
  427. unsigned long flags;
  428. /* This code is time critical - we don't want to be interrupted here */
  429. local_irq_save(flags);
  430. /*
  431. * While reading TSF upper and then lower part, the clock is still
  432. * counting (or jumping in case of IBSS merge) so we might get
  433. * inconsistent values. To avoid this, we read the upper part again
  434. * and check it has not been changed. We make the hypothesis that a
  435. * maximum of 3 changes can happens in a row (we use 10 as a safe
  436. * value).
  437. *
  438. * Impact on performance is pretty small, since in most cases, only
  439. * 3 register reads are needed.
  440. */
  441. tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  442. for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
  443. tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  444. tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  445. if (tsf_upper2 == tsf_upper1)
  446. break;
  447. tsf_upper1 = tsf_upper2;
  448. }
  449. local_irq_restore(flags);
  450. WARN_ON( i == ATH5K_MAX_TSF_READ );
  451. return (((u64)tsf_upper1 << 32) | tsf_lower);
  452. }
  453. /**
  454. * ath5k_hw_set_tsf64 - Set a new 64bit TSF
  455. *
  456. * @ah: The &struct ath5k_hw
  457. * @tsf64: The new 64bit TSF
  458. *
  459. * Sets the new TSF
  460. */
  461. void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
  462. {
  463. ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
  464. ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
  465. }
  466. /**
  467. * ath5k_hw_reset_tsf - Force a TSF reset
  468. *
  469. * @ah: The &struct ath5k_hw
  470. *
  471. * Forces a TSF reset on PCU
  472. */
  473. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  474. {
  475. u32 val;
  476. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  477. /*
  478. * Each write to the RESET_TSF bit toggles a hardware internal
  479. * signal to reset TSF, but if left high it will cause a TSF reset
  480. * on the next chip reset as well. Thus we always write the value
  481. * twice to clear the signal.
  482. */
  483. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  484. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  485. }
  486. /*
  487. * Initialize beacon timers
  488. */
  489. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  490. {
  491. u32 timer1, timer2, timer3;
  492. /*
  493. * Set the additional timers by mode
  494. */
  495. switch (ah->ah_sc->opmode) {
  496. case NL80211_IFTYPE_MONITOR:
  497. case NL80211_IFTYPE_STATION:
  498. /* In STA mode timer1 is used as next wakeup
  499. * timer and timer2 as next CFP duration start
  500. * timer. Both in 1/8TUs. */
  501. /* TODO: PCF handling */
  502. if (ah->ah_version == AR5K_AR5210) {
  503. timer1 = 0xffffffff;
  504. timer2 = 0xffffffff;
  505. } else {
  506. timer1 = 0x0000ffff;
  507. timer2 = 0x0007ffff;
  508. }
  509. /* Mark associated AP as PCF incapable for now */
  510. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
  511. break;
  512. case NL80211_IFTYPE_ADHOC:
  513. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
  514. default:
  515. /* On non-STA modes timer1 is used as next DMA
  516. * beacon alert (DBA) timer and timer2 as next
  517. * software beacon alert. Both in 1/8TUs. */
  518. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  519. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  520. break;
  521. }
  522. /* Timer3 marks the end of our ATIM window
  523. * a zero length window is not allowed because
  524. * we 'll get no beacons */
  525. timer3 = next_beacon + 1;
  526. /*
  527. * Set the beacon register and enable all timers.
  528. */
  529. /* When in AP or Mesh Point mode zero timer0 to start TSF */
  530. if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
  531. ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
  532. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  533. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  534. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  535. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  536. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  537. /* Force a TSF reset if requested and enable beacons */
  538. if (interval & AR5K_BEACON_RESET_TSF)
  539. ath5k_hw_reset_tsf(ah);
  540. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  541. AR5K_BEACON_ENABLE),
  542. AR5K_BEACON);
  543. /* Flush any pending BMISS interrupts on ISR by
  544. * performing a clear-on-write operation on PISR
  545. * register for the BMISS bit (writing a bit on
  546. * ISR togles a reset for that bit and leaves
  547. * the rest bits intact) */
  548. if (ah->ah_version == AR5K_AR5210)
  549. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
  550. else
  551. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
  552. /* TODO: Set enchanced sleep registers on AR5212
  553. * based on vif->bss_conf params, until then
  554. * disable power save reporting.*/
  555. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
  556. }
  557. /**
  558. * ath5k_check_timer_win - Check if timer B is timer A + window
  559. *
  560. * @a: timer a (before b)
  561. * @b: timer b (after a)
  562. * @window: difference between a and b
  563. * @intval: timers are increased by this interval
  564. *
  565. * This helper function checks if timer B is timer A + window and covers
  566. * cases where timer A or B might have already been updated or wrapped
  567. * around (Timers are 16 bit).
  568. *
  569. * Returns true if O.K.
  570. */
  571. static inline bool
  572. ath5k_check_timer_win(int a, int b, int window, int intval)
  573. {
  574. /*
  575. * 1.) usually B should be A + window
  576. * 2.) A already updated, B not updated yet
  577. * 3.) A already updated and has wrapped around
  578. * 4.) B has wrapped around
  579. */
  580. if ((b - a == window) || /* 1.) */
  581. (a - b == intval - window) || /* 2.) */
  582. ((a | 0x10000) - b == intval - window) || /* 3.) */
  583. ((b | 0x10000) - a == window)) /* 4.) */
  584. return true; /* O.K. */
  585. return false;
  586. }
  587. /**
  588. * ath5k_hw_check_beacon_timers - Check if the beacon timers are correct
  589. *
  590. * @ah: The &struct ath5k_hw
  591. * @intval: beacon interval
  592. *
  593. * This is a workaround for IBSS mode:
  594. *
  595. * The need for this function arises from the fact that we have 4 separate
  596. * HW timer registers (TIMER0 - TIMER3), which are closely related to the
  597. * next beacon target time (NBTT), and that the HW updates these timers
  598. * seperately based on the current TSF value. The hardware increments each
  599. * timer by the beacon interval, when the local TSF coverted to TU is equal
  600. * to the value stored in the timer.
  601. *
  602. * The reception of a beacon with the same BSSID can update the local HW TSF
  603. * at any time - this is something we can't avoid. If the TSF jumps to a
  604. * time which is later than the time stored in a timer, this timer will not
  605. * be updated until the TSF in TU wraps around at 16 bit (the size of the
  606. * timers) and reaches the time which is stored in the timer.
  607. *
  608. * The problem is that these timers are closely related to TIMER0 (NBTT) and
  609. * that they define a time "window". When the TSF jumps between two timers
  610. * (e.g. ATIM and NBTT), the one in the past will be left behind (not
  611. * updated), while the one in the future will be updated every beacon
  612. * interval. This causes the window to get larger, until the TSF wraps
  613. * around as described above and the timer which was left behind gets
  614. * updated again. But - because the beacon interval is usually not an exact
  615. * divisor of the size of the timers (16 bit), an unwanted "window" between
  616. * these timers has developed!
  617. *
  618. * This is especially important with the ATIM window, because during
  619. * the ATIM window only ATIM frames and no data frames are allowed to be
  620. * sent, which creates transmission pauses after each beacon. This symptom
  621. * has been described as "ramping ping" because ping times increase linearly
  622. * for some time and then drop down again. A wrong window on the DMA beacon
  623. * timer has the same effect, so we check for these two conditions.
  624. *
  625. * Returns true if O.K.
  626. */
  627. bool
  628. ath5k_hw_check_beacon_timers(struct ath5k_hw *ah, int intval)
  629. {
  630. unsigned int nbtt, atim, dma;
  631. nbtt = ath5k_hw_reg_read(ah, AR5K_TIMER0);
  632. atim = ath5k_hw_reg_read(ah, AR5K_TIMER3);
  633. dma = ath5k_hw_reg_read(ah, AR5K_TIMER1) >> 3;
  634. /* NOTE: SWBA is different. Having a wrong window there does not
  635. * stop us from sending data and this condition is catched thru
  636. * other means (SWBA interrupt) */
  637. if (ath5k_check_timer_win(nbtt, atim, 1, intval) &&
  638. ath5k_check_timer_win(dma, nbtt, AR5K_TUNE_DMA_BEACON_RESP,
  639. intval))
  640. return true; /* O.K. */
  641. return false;
  642. }
  643. /**
  644. * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
  645. *
  646. * @ah: The &struct ath5k_hw
  647. * @coverage_class: IEEE 802.11 coverage class number
  648. *
  649. * Sets slot time, ACK timeout and CTS timeout for given coverage class.
  650. */
  651. void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
  652. {
  653. /* As defined by IEEE 802.11-2007 17.3.8.6 */
  654. int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
  655. int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
  656. int cts_timeout = ack_timeout;
  657. ath5k_hw_set_slot_time(ah, slot_time);
  658. ath5k_hw_set_ack_timeout(ah, ack_timeout);
  659. ath5k_hw_set_cts_timeout(ah, cts_timeout);
  660. ah->ah_coverage_class = coverage_class;
  661. }