pcu.c 26 KB

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