pcu.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
  4. * Copyright (c) 2007-2008 Matthew W. S. Bell <mentor@madwifi.org>
  5. * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
  6. * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
  7. * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
  8. *
  9. * Permission to use, copy, modify, and distribute this software for any
  10. * purpose with or without fee is hereby granted, provided that the above
  11. * copyright notice and this permission notice appear in all copies.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  14. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  16. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  19. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20. *
  21. */
  22. /*********************************\
  23. * Protocol Control Unit Functions *
  24. \*********************************/
  25. #include <asm/unaligned.h>
  26. #include "ath5k.h"
  27. #include "reg.h"
  28. #include "debug.h"
  29. #include "base.h"
  30. /*******************\
  31. * Generic functions *
  32. \*******************/
  33. /**
  34. * ath5k_hw_set_opmode - Set PCU operating mode
  35. *
  36. * @ah: The &struct ath5k_hw
  37. *
  38. * Initialize PCU for the various operating modes (AP/STA etc)
  39. *
  40. * NOTE: ah->ah_op_mode must be set before calling this.
  41. */
  42. int ath5k_hw_set_opmode(struct ath5k_hw *ah)
  43. {
  44. struct ath_common *common = ath5k_hw_common(ah);
  45. u32 pcu_reg, beacon_reg, low_id, high_id;
  46. /* Preserve rest settings */
  47. pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
  48. pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
  49. | AR5K_STA_ID1_KEYSRCH_MODE
  50. | (ah->ah_version == AR5K_AR5210 ?
  51. (AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
  52. beacon_reg = 0;
  53. ATH5K_TRACE(ah->ah_sc);
  54. switch (ah->ah_op_mode) {
  55. case NL80211_IFTYPE_ADHOC:
  56. pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
  57. beacon_reg |= AR5K_BCR_ADHOC;
  58. if (ah->ah_version == AR5K_AR5210)
  59. pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
  60. else
  61. AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
  62. break;
  63. case NL80211_IFTYPE_AP:
  64. case NL80211_IFTYPE_MESH_POINT:
  65. pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
  66. beacon_reg |= AR5K_BCR_AP;
  67. if (ah->ah_version == AR5K_AR5210)
  68. pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
  69. else
  70. AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
  71. break;
  72. case NL80211_IFTYPE_STATION:
  73. pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
  74. | (ah->ah_version == AR5K_AR5210 ?
  75. AR5K_STA_ID1_PWR_SV : 0);
  76. case NL80211_IFTYPE_MONITOR:
  77. pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
  78. | (ah->ah_version == AR5K_AR5210 ?
  79. AR5K_STA_ID1_NO_PSPOLL : 0);
  80. break;
  81. default:
  82. return -EINVAL;
  83. }
  84. /*
  85. * Set PCU registers
  86. */
  87. low_id = get_unaligned_le32(common->macaddr);
  88. high_id = get_unaligned_le16(common->macaddr + 4);
  89. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  90. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  91. /*
  92. * Set Beacon Control Register on 5210
  93. */
  94. if (ah->ah_version == AR5K_AR5210)
  95. ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
  96. return 0;
  97. }
  98. /**
  99. * ath5k_hw_update - Update mib counters (mac layer statistics)
  100. *
  101. * @ah: The &struct ath5k_hw
  102. * @stats: The &struct ieee80211_low_level_stats we use to track
  103. * statistics on the driver
  104. *
  105. * Reads MIB counters from PCU and updates sw statistics. Must be
  106. * called after a MIB interrupt.
  107. */
  108. void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
  109. struct ieee80211_low_level_stats *stats)
  110. {
  111. ATH5K_TRACE(ah->ah_sc);
  112. /* Read-And-Clear */
  113. stats->dot11ACKFailureCount += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
  114. stats->dot11RTSFailureCount += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
  115. stats->dot11RTSSuccessCount += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
  116. stats->dot11FCSErrorCount += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
  117. /* XXX: Should we use this to track beacon count ?
  118. * -we read it anyway to clear the register */
  119. ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
  120. /* Reset profile count registers on 5212*/
  121. if (ah->ah_version == AR5K_AR5212) {
  122. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
  123. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
  124. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
  125. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
  126. }
  127. /* TODO: Handle ANI stats */
  128. }
  129. /**
  130. * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
  131. *
  132. * @ah: The &struct ath5k_hw
  133. * @high: Flag to determine if we want to use high transmition rate
  134. * for ACKs or not
  135. *
  136. * If high flag is set, we tell hw to use a set of control rates based on
  137. * the current transmition rate (check out control_rates array inside reset.c).
  138. * If not hw just uses the lowest rate available for the current modulation
  139. * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
  140. */
  141. void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
  142. {
  143. if (ah->ah_version != AR5K_AR5212)
  144. return;
  145. else {
  146. u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
  147. if (high)
  148. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
  149. else
  150. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
  151. }
  152. }
  153. /******************\
  154. * ACK/CTS Timeouts *
  155. \******************/
  156. /**
  157. * ath5k_hw_het_ack_timeout - Get ACK timeout from PCU in usec
  158. *
  159. * @ah: The &struct ath5k_hw
  160. */
  161. unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
  162. {
  163. ATH5K_TRACE(ah->ah_sc);
  164. return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
  165. AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
  166. }
  167. /**
  168. * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
  169. *
  170. * @ah: The &struct ath5k_hw
  171. * @timeout: Timeout in usec
  172. */
  173. int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
  174. {
  175. ATH5K_TRACE(ah->ah_sc);
  176. if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
  177. ah->ah_turbo) <= timeout)
  178. return -EINVAL;
  179. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
  180. ath5k_hw_htoclock(timeout, ah->ah_turbo));
  181. return 0;
  182. }
  183. /**
  184. * ath5k_hw_get_cts_timeout - Get CTS timeout from PCU in usec
  185. *
  186. * @ah: The &struct ath5k_hw
  187. */
  188. unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
  189. {
  190. ATH5K_TRACE(ah->ah_sc);
  191. return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
  192. AR5K_TIME_OUT), AR5K_TIME_OUT_CTS), ah->ah_turbo);
  193. }
  194. /**
  195. * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
  196. *
  197. * @ah: The &struct ath5k_hw
  198. * @timeout: Timeout in usec
  199. */
  200. int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
  201. {
  202. ATH5K_TRACE(ah->ah_sc);
  203. if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
  204. ah->ah_turbo) <= timeout)
  205. return -EINVAL;
  206. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
  207. ath5k_hw_htoclock(timeout, ah->ah_turbo));
  208. return 0;
  209. }
  210. /**
  211. * ath5k_hw_set_lladdr - Set station id
  212. *
  213. * @ah: The &struct ath5k_hw
  214. * @mac: The card's mac address
  215. *
  216. * Set station id on hw using the provided mac address
  217. */
  218. int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
  219. {
  220. struct ath_common *common = ath5k_hw_common(ah);
  221. u32 low_id, high_id;
  222. u32 pcu_reg;
  223. ATH5K_TRACE(ah->ah_sc);
  224. /* Set new station ID */
  225. memcpy(common->macaddr, mac, ETH_ALEN);
  226. pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
  227. low_id = get_unaligned_le32(mac);
  228. high_id = get_unaligned_le16(mac + 4);
  229. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  230. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  231. return 0;
  232. }
  233. /**
  234. * ath5k_hw_set_associd - Set BSSID for association
  235. *
  236. * @ah: The &struct ath5k_hw
  237. * @bssid: BSSID
  238. * @assoc_id: Assoc id
  239. *
  240. * Sets the BSSID which trigers the "SME Join" operation
  241. */
  242. void ath5k_hw_set_associd(struct ath5k_hw *ah)
  243. {
  244. struct ath_common *common = ath5k_hw_common(ah);
  245. u16 tim_offset = 0;
  246. /*
  247. * Set simple BSSID mask on 5212
  248. */
  249. if (ah->ah_version == AR5K_AR5212)
  250. ath_hw_setbssidmask(common);
  251. /*
  252. * Set BSSID which triggers the "SME Join" operation
  253. */
  254. ath5k_hw_reg_write(ah,
  255. get_unaligned_le32(common->curbssid),
  256. AR5K_BSS_ID0);
  257. ath5k_hw_reg_write(ah,
  258. get_unaligned_le16(common->curbssid + 4) |
  259. ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
  260. AR5K_BSS_ID1);
  261. if (common->curaid == 0) {
  262. ath5k_hw_disable_pspoll(ah);
  263. return;
  264. }
  265. AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
  266. tim_offset ? tim_offset + 4 : 0);
  267. ath5k_hw_enable_pspoll(ah, NULL, 0);
  268. }
  269. void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
  270. {
  271. struct ath_common *common = ath5k_hw_common(ah);
  272. ATH5K_TRACE(ah->ah_sc);
  273. /* Cache bssid mask so that we can restore it
  274. * on reset */
  275. memcpy(common->bssidmask, mask, ETH_ALEN);
  276. if (ah->ah_version == AR5K_AR5212)
  277. ath_hw_setbssidmask(common);
  278. }
  279. /************\
  280. * RX Control *
  281. \************/
  282. /**
  283. * ath5k_hw_start_rx_pcu - Start RX engine
  284. *
  285. * @ah: The &struct ath5k_hw
  286. *
  287. * Starts RX engine on PCU so that hw can process RXed frames
  288. * (ACK etc).
  289. *
  290. * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
  291. * TODO: Init ANI here
  292. */
  293. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  294. {
  295. ATH5K_TRACE(ah->ah_sc);
  296. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  297. }
  298. /**
  299. * at5k_hw_stop_rx_pcu - Stop RX engine
  300. *
  301. * @ah: The &struct ath5k_hw
  302. *
  303. * Stops RX engine on PCU
  304. *
  305. * TODO: Detach ANI here
  306. */
  307. void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
  308. {
  309. ATH5K_TRACE(ah->ah_sc);
  310. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  311. }
  312. /*
  313. * Set multicast filter
  314. */
  315. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  316. {
  317. ATH5K_TRACE(ah->ah_sc);
  318. /* Set the multicat filter */
  319. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  320. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  321. }
  322. /*
  323. * Set multicast filter by index
  324. */
  325. int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
  326. {
  327. ATH5K_TRACE(ah->ah_sc);
  328. if (index >= 64)
  329. return -EINVAL;
  330. else if (index >= 32)
  331. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
  332. (1 << (index - 32)));
  333. else
  334. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  335. return 0;
  336. }
  337. /*
  338. * Clear Multicast filter by index
  339. */
  340. int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
  341. {
  342. ATH5K_TRACE(ah->ah_sc);
  343. if (index >= 64)
  344. return -EINVAL;
  345. else if (index >= 32)
  346. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
  347. (1 << (index - 32)));
  348. else
  349. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  350. return 0;
  351. }
  352. /**
  353. * ath5k_hw_get_rx_filter - Get current rx filter
  354. *
  355. * @ah: The &struct ath5k_hw
  356. *
  357. * Returns the RX filter by reading rx filter and
  358. * phy error filter registers. RX filter is used
  359. * to set the allowed frame types that PCU will accept
  360. * and pass to the driver. For a list of frame types
  361. * check out reg.h.
  362. */
  363. u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
  364. {
  365. u32 data, filter = 0;
  366. ATH5K_TRACE(ah->ah_sc);
  367. filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
  368. /*Radar detection for 5212*/
  369. if (ah->ah_version == AR5K_AR5212) {
  370. data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
  371. if (data & AR5K_PHY_ERR_FIL_RADAR)
  372. filter |= AR5K_RX_FILTER_RADARERR;
  373. if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
  374. filter |= AR5K_RX_FILTER_PHYERR;
  375. }
  376. return filter;
  377. }
  378. /**
  379. * ath5k_hw_set_rx_filter - Set rx filter
  380. *
  381. * @ah: The &struct ath5k_hw
  382. * @filter: RX filter mask (see reg.h)
  383. *
  384. * Sets RX filter register and also handles PHY error filter
  385. * register on 5212 and newer chips so that we have proper PHY
  386. * error reporting.
  387. */
  388. void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
  389. {
  390. u32 data = 0;
  391. ATH5K_TRACE(ah->ah_sc);
  392. /* Set PHY error filter register on 5212*/
  393. if (ah->ah_version == AR5K_AR5212) {
  394. if (filter & AR5K_RX_FILTER_RADARERR)
  395. data |= AR5K_PHY_ERR_FIL_RADAR;
  396. if (filter & AR5K_RX_FILTER_PHYERR)
  397. data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
  398. }
  399. /*
  400. * The AR5210 uses promiscous mode to detect radar activity
  401. */
  402. if (ah->ah_version == AR5K_AR5210 &&
  403. (filter & AR5K_RX_FILTER_RADARERR)) {
  404. filter &= ~AR5K_RX_FILTER_RADARERR;
  405. filter |= AR5K_RX_FILTER_PROM;
  406. }
  407. /*Zero length DMA (phy error reporting) */
  408. if (data)
  409. AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  410. else
  411. AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  412. /*Write RX Filter register*/
  413. ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
  414. /*Write PHY error filter register on 5212*/
  415. if (ah->ah_version == AR5K_AR5212)
  416. ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
  417. }
  418. /****************\
  419. * Beacon control *
  420. \****************/
  421. /**
  422. * ath5k_hw_get_tsf32 - Get a 32bit TSF
  423. *
  424. * @ah: The &struct ath5k_hw
  425. *
  426. * Returns lower 32 bits of current TSF
  427. */
  428. u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
  429. {
  430. ATH5K_TRACE(ah->ah_sc);
  431. return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  432. }
  433. /**
  434. * ath5k_hw_get_tsf64 - Get the full 64bit TSF
  435. *
  436. * @ah: The &struct ath5k_hw
  437. *
  438. * Returns the current TSF
  439. */
  440. u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
  441. {
  442. u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  443. ATH5K_TRACE(ah->ah_sc);
  444. return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
  445. }
  446. /**
  447. * ath5k_hw_set_tsf64 - Set a new 64bit TSF
  448. *
  449. * @ah: The &struct ath5k_hw
  450. * @tsf64: The new 64bit TSF
  451. *
  452. * Sets the new TSF
  453. */
  454. void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
  455. {
  456. ATH5K_TRACE(ah->ah_sc);
  457. ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
  458. ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
  459. }
  460. /**
  461. * ath5k_hw_reset_tsf - Force a TSF reset
  462. *
  463. * @ah: The &struct ath5k_hw
  464. *
  465. * Forces a TSF reset on PCU
  466. */
  467. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  468. {
  469. u32 val;
  470. ATH5K_TRACE(ah->ah_sc);
  471. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  472. /*
  473. * Each write to the RESET_TSF bit toggles a hardware internal
  474. * signal to reset TSF, but if left high it will cause a TSF reset
  475. * on the next chip reset as well. Thus we always write the value
  476. * twice to clear the signal.
  477. */
  478. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  479. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  480. }
  481. /*
  482. * Initialize beacon timers
  483. */
  484. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  485. {
  486. u32 timer1, timer2, timer3;
  487. ATH5K_TRACE(ah->ah_sc);
  488. /*
  489. * Set the additional timers by mode
  490. */
  491. switch (ah->ah_op_mode) {
  492. case NL80211_IFTYPE_MONITOR:
  493. case NL80211_IFTYPE_STATION:
  494. /* In STA mode timer1 is used as next wakeup
  495. * timer and timer2 as next CFP duration start
  496. * timer. Both in 1/8TUs. */
  497. /* TODO: PCF handling */
  498. if (ah->ah_version == AR5K_AR5210) {
  499. timer1 = 0xffffffff;
  500. timer2 = 0xffffffff;
  501. } else {
  502. timer1 = 0x0000ffff;
  503. timer2 = 0x0007ffff;
  504. }
  505. /* Mark associated AP as PCF incapable for now */
  506. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
  507. break;
  508. case NL80211_IFTYPE_ADHOC:
  509. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
  510. default:
  511. /* On non-STA modes timer1 is used as next DMA
  512. * beacon alert (DBA) timer and timer2 as next
  513. * software beacon alert. Both in 1/8TUs. */
  514. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  515. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  516. break;
  517. }
  518. /* Timer3 marks the end of our ATIM window
  519. * a zero length window is not allowed because
  520. * we 'll get no beacons */
  521. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  522. /*
  523. * Set the beacon register and enable all timers.
  524. */
  525. /* When in AP or Mesh Point mode zero timer0 to start TSF */
  526. if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
  527. ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
  528. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  529. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  530. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  531. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  532. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  533. /* Force a TSF reset if requested and enable beacons */
  534. if (interval & AR5K_BEACON_RESET_TSF)
  535. ath5k_hw_reset_tsf(ah);
  536. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  537. AR5K_BEACON_ENABLE),
  538. AR5K_BEACON);
  539. /* Flush any pending BMISS interrupts on ISR by
  540. * performing a clear-on-write operation on PISR
  541. * register for the BMISS bit (writing a bit on
  542. * ISR togles a reset for that bit and leaves
  543. * the rest bits intact) */
  544. if (ah->ah_version == AR5K_AR5210)
  545. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
  546. else
  547. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
  548. /* TODO: Set enchanced sleep registers on AR5212
  549. * based on vif->bss_conf params, until then
  550. * disable power save reporting.*/
  551. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
  552. }
  553. #if 0
  554. /*
  555. * Set beacon timers
  556. */
  557. int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
  558. const struct ath5k_beacon_state *state)
  559. {
  560. u32 cfp_period, next_cfp, dtim, interval, next_beacon;
  561. /*
  562. * TODO: should be changed through *state
  563. * review struct ath5k_beacon_state struct
  564. *
  565. * XXX: These are used for cfp period bellow, are they
  566. * ok ? Is it O.K. for tsf here to be 0 or should we use
  567. * get_tsf ?
  568. */
  569. u32 dtim_count = 0; /* XXX */
  570. u32 cfp_count = 0; /* XXX */
  571. u32 tsf = 0; /* XXX */
  572. ATH5K_TRACE(ah->ah_sc);
  573. /* Return on an invalid beacon state */
  574. if (state->bs_interval < 1)
  575. return -EINVAL;
  576. interval = state->bs_interval;
  577. dtim = state->bs_dtim_period;
  578. /*
  579. * PCF support?
  580. */
  581. if (state->bs_cfp_period > 0) {
  582. /*
  583. * Enable PCF mode and set the CFP
  584. * (Contention Free Period) and timer registers
  585. */
  586. cfp_period = state->bs_cfp_period * state->bs_dtim_period *
  587. state->bs_interval;
  588. next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
  589. state->bs_interval;
  590. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
  591. AR5K_STA_ID1_DEFAULT_ANTENNA |
  592. AR5K_STA_ID1_PCF);
  593. ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
  594. ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
  595. AR5K_CFP_DUR);
  596. ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
  597. next_cfp)) << 3, AR5K_TIMER2);
  598. } else {
  599. /* Disable PCF mode */
  600. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  601. AR5K_STA_ID1_DEFAULT_ANTENNA |
  602. AR5K_STA_ID1_PCF);
  603. }
  604. /*
  605. * Enable the beacon timer register
  606. */
  607. ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
  608. /*
  609. * Start the beacon timers
  610. */
  611. ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
  612. ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
  613. AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
  614. AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
  615. AR5K_BEACON_PERIOD), AR5K_BEACON);
  616. /*
  617. * Write new beacon miss threshold, if it appears to be valid
  618. * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
  619. * and return if its not in range. We can test this by reading value and
  620. * setting value to a largest value and seeing which values register.
  621. */
  622. AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
  623. state->bs_bmiss_threshold);
  624. /*
  625. * Set sleep control register
  626. * XXX: Didn't find this in 5210 code but since this register
  627. * exists also in ar5k's 5210 headers i leave it as common code.
  628. */
  629. AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
  630. (state->bs_sleep_duration - 3) << 3);
  631. /*
  632. * Set enhanced sleep registers on 5212
  633. */
  634. if (ah->ah_version == AR5K_AR5212) {
  635. if (state->bs_sleep_duration > state->bs_interval &&
  636. roundup(state->bs_sleep_duration, interval) ==
  637. state->bs_sleep_duration)
  638. interval = state->bs_sleep_duration;
  639. if (state->bs_sleep_duration > dtim && (dtim == 0 ||
  640. roundup(state->bs_sleep_duration, dtim) ==
  641. state->bs_sleep_duration))
  642. dtim = state->bs_sleep_duration;
  643. if (interval > dtim)
  644. return -EINVAL;
  645. next_beacon = interval == dtim ? state->bs_next_dtim :
  646. state->bs_next_beacon;
  647. ath5k_hw_reg_write(ah,
  648. AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
  649. AR5K_SLEEP0_NEXT_DTIM) |
  650. AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
  651. AR5K_SLEEP0_ENH_SLEEP_EN |
  652. AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
  653. ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
  654. AR5K_SLEEP1_NEXT_TIM) |
  655. AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
  656. ath5k_hw_reg_write(ah,
  657. AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
  658. AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
  659. }
  660. return 0;
  661. }
  662. /*
  663. * Reset beacon timers
  664. */
  665. void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
  666. {
  667. ATH5K_TRACE(ah->ah_sc);
  668. /*
  669. * Disable beacon timer
  670. */
  671. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  672. /*
  673. * Disable some beacon register values
  674. */
  675. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  676. AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
  677. ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
  678. }
  679. /*
  680. * Wait for beacon queue to finish
  681. */
  682. int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
  683. {
  684. unsigned int i;
  685. int ret;
  686. ATH5K_TRACE(ah->ah_sc);
  687. /* 5210 doesn't have QCU*/
  688. if (ah->ah_version == AR5K_AR5210) {
  689. /*
  690. * Wait for beaconn queue to finish by checking
  691. * Control Register and Beacon Status Register.
  692. */
  693. for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
  694. if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
  695. ||
  696. !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
  697. break;
  698. udelay(10);
  699. }
  700. /* Timeout... */
  701. if (i <= 0) {
  702. /*
  703. * Re-schedule the beacon queue
  704. */
  705. ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
  706. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
  707. AR5K_BCR);
  708. return -EIO;
  709. }
  710. ret = 0;
  711. } else {
  712. /*5211/5212*/
  713. ret = ath5k_hw_register_timeout(ah,
  714. AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
  715. AR5K_QCU_STS_FRMPENDCNT, 0, false);
  716. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
  717. return -EIO;
  718. }
  719. return ret;
  720. }
  721. #endif
  722. /*********************\
  723. * Key table functions *
  724. \*********************/
  725. /*
  726. * Reset a key entry on the table
  727. */
  728. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  729. {
  730. unsigned int i, type;
  731. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  732. ATH5K_TRACE(ah->ah_sc);
  733. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  734. type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
  735. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  736. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  737. /* Reset associated MIC entry if TKIP
  738. * is enabled located at offset (entry + 64) */
  739. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  740. AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
  741. for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
  742. ath5k_hw_reg_write(ah, 0,
  743. AR5K_KEYTABLE_OFF(micentry, i));
  744. }
  745. /*
  746. * Set NULL encryption on AR5212+
  747. *
  748. * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
  749. * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
  750. *
  751. * Note2: Windows driver (ndiswrapper) sets this to
  752. * 0x00000714 instead of 0x00000007
  753. */
  754. if (ah->ah_version >= AR5K_AR5211) {
  755. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  756. AR5K_KEYTABLE_TYPE(entry));
  757. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  758. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  759. AR5K_KEYTABLE_TYPE(micentry));
  760. }
  761. }
  762. return 0;
  763. }
  764. /*
  765. * Check if a table entry is valid
  766. */
  767. int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
  768. {
  769. ATH5K_TRACE(ah->ah_sc);
  770. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  771. /* Check the validation flag at the end of the entry */
  772. return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
  773. AR5K_KEYTABLE_VALID;
  774. }
  775. static
  776. int ath5k_keycache_type(const struct ieee80211_key_conf *key)
  777. {
  778. switch (key->alg) {
  779. case ALG_TKIP:
  780. return AR5K_KEYTABLE_TYPE_TKIP;
  781. case ALG_CCMP:
  782. return AR5K_KEYTABLE_TYPE_CCM;
  783. case ALG_WEP:
  784. if (key->keylen == WLAN_KEY_LEN_WEP40)
  785. return AR5K_KEYTABLE_TYPE_40;
  786. else if (key->keylen == WLAN_KEY_LEN_WEP104)
  787. return AR5K_KEYTABLE_TYPE_104;
  788. return -EINVAL;
  789. default:
  790. return -EINVAL;
  791. }
  792. return -EINVAL;
  793. }
  794. /*
  795. * Set a key entry on the table
  796. */
  797. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  798. const struct ieee80211_key_conf *key, const u8 *mac)
  799. {
  800. unsigned int i;
  801. int keylen;
  802. __le32 key_v[5] = {};
  803. __le32 key0 = 0, key1 = 0;
  804. __le32 *rxmic, *txmic;
  805. int keytype;
  806. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  807. bool is_tkip;
  808. const u8 *key_ptr;
  809. ATH5K_TRACE(ah->ah_sc);
  810. is_tkip = (key->alg == ALG_TKIP);
  811. /*
  812. * key->keylen comes in from mac80211 in bytes.
  813. * TKIP is 128 bit + 128 bit mic
  814. */
  815. keylen = (is_tkip) ? (128 / 8) : key->keylen;
  816. if (entry > AR5K_KEYTABLE_SIZE ||
  817. (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
  818. return -EOPNOTSUPP;
  819. if (unlikely(keylen > 16))
  820. return -EOPNOTSUPP;
  821. keytype = ath5k_keycache_type(key);
  822. if (keytype < 0)
  823. return keytype;
  824. /*
  825. * each key block is 6 bytes wide, written as pairs of
  826. * alternating 32 and 16 bit le values.
  827. */
  828. key_ptr = key->key;
  829. for (i = 0; keylen >= 6; keylen -= 6) {
  830. memcpy(&key_v[i], key_ptr, 6);
  831. i += 2;
  832. key_ptr += 6;
  833. }
  834. if (keylen)
  835. memcpy(&key_v[i], key_ptr, keylen);
  836. /* intentionally corrupt key until mic is installed */
  837. if (is_tkip) {
  838. key0 = key_v[0] = ~key_v[0];
  839. key1 = key_v[1] = ~key_v[1];
  840. }
  841. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  842. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  843. AR5K_KEYTABLE_OFF(entry, i));
  844. ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
  845. if (is_tkip) {
  846. /* Install rx/tx MIC */
  847. rxmic = (__le32 *) &key->key[16];
  848. txmic = (__le32 *) &key->key[24];
  849. if (ah->ah_combined_mic) {
  850. key_v[0] = rxmic[0];
  851. key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
  852. key_v[2] = rxmic[1];
  853. key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
  854. key_v[4] = txmic[1];
  855. } else {
  856. key_v[0] = rxmic[0];
  857. key_v[1] = 0;
  858. key_v[2] = rxmic[1];
  859. key_v[3] = 0;
  860. key_v[4] = 0;
  861. }
  862. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  863. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  864. AR5K_KEYTABLE_OFF(micentry, i));
  865. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  866. AR5K_KEYTABLE_TYPE(micentry));
  867. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
  868. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
  869. /* restore first 2 words of key */
  870. ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
  871. AR5K_KEYTABLE_OFF(entry, 0));
  872. ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
  873. AR5K_KEYTABLE_OFF(entry, 1));
  874. }
  875. return ath5k_hw_set_key_lladdr(ah, entry, mac);
  876. }
  877. int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
  878. {
  879. u32 low_id, high_id;
  880. ATH5K_TRACE(ah->ah_sc);
  881. /* Invalid entry (key table overflow) */
  882. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  883. /*
  884. * MAC may be NULL if it's a broadcast key. In this case no need to
  885. * to compute get_unaligned_le32 and get_unaligned_le16 as we
  886. * already know it.
  887. */
  888. if (!mac) {
  889. low_id = 0xffffffff;
  890. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  891. } else {
  892. low_id = get_unaligned_le32(mac);
  893. high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
  894. }
  895. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  896. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  897. return 0;
  898. }