pcu.c 28 KB

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