pcu.c 26 KB

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