pcu.c 28 KB

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