pcu.c 28 KB

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