pcu.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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, const u8 *bssid, u16 assoc_id)
  243. {
  244. struct ath_common *common = ath5k_hw_common(ah);
  245. u32 low_id, high_id;
  246. u16 tim_offset = 0;
  247. /*
  248. * Set simple BSSID mask on 5212
  249. */
  250. if (ah->ah_version == AR5K_AR5212)
  251. ath_hw_setbssidmask(common);
  252. /*
  253. * Set BSSID which triggers the "SME Join" operation
  254. */
  255. low_id = get_unaligned_le32(bssid);
  256. high_id = get_unaligned_le16(bssid);
  257. ath5k_hw_reg_write(ah, low_id, AR_BSSMSKL);
  258. ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
  259. AR5K_BSS_ID1_AID_S), AR_BSSMSKU);
  260. if (assoc_id == 0) {
  261. ath5k_hw_disable_pspoll(ah);
  262. return;
  263. }
  264. AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
  265. tim_offset ? tim_offset + 4 : 0);
  266. ath5k_hw_enable_pspoll(ah, NULL, 0);
  267. }
  268. void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
  269. {
  270. struct ath_common *common = ath5k_hw_common(ah);
  271. ATH5K_TRACE(ah->ah_sc);
  272. /* Cache bssid mask so that we can restore it
  273. * on reset */
  274. memcpy(common->bssidmask, mask, ETH_ALEN);
  275. if (ah->ah_version == AR5K_AR5212)
  276. ath_hw_setbssidmask(common);
  277. }
  278. /************\
  279. * RX Control *
  280. \************/
  281. /**
  282. * ath5k_hw_start_rx_pcu - Start RX engine
  283. *
  284. * @ah: The &struct ath5k_hw
  285. *
  286. * Starts RX engine on PCU so that hw can process RXed frames
  287. * (ACK etc).
  288. *
  289. * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
  290. * TODO: Init ANI here
  291. */
  292. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  293. {
  294. ATH5K_TRACE(ah->ah_sc);
  295. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  296. }
  297. /**
  298. * at5k_hw_stop_rx_pcu - Stop RX engine
  299. *
  300. * @ah: The &struct ath5k_hw
  301. *
  302. * Stops RX engine on PCU
  303. *
  304. * TODO: Detach ANI here
  305. */
  306. void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
  307. {
  308. ATH5K_TRACE(ah->ah_sc);
  309. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  310. }
  311. /*
  312. * Set multicast filter
  313. */
  314. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  315. {
  316. ATH5K_TRACE(ah->ah_sc);
  317. /* Set the multicat filter */
  318. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  319. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  320. }
  321. /*
  322. * Set multicast filter by index
  323. */
  324. int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
  325. {
  326. ATH5K_TRACE(ah->ah_sc);
  327. if (index >= 64)
  328. return -EINVAL;
  329. else if (index >= 32)
  330. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
  331. (1 << (index - 32)));
  332. else
  333. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  334. return 0;
  335. }
  336. /*
  337. * Clear Multicast filter by index
  338. */
  339. int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
  340. {
  341. ATH5K_TRACE(ah->ah_sc);
  342. if (index >= 64)
  343. return -EINVAL;
  344. else if (index >= 32)
  345. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
  346. (1 << (index - 32)));
  347. else
  348. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  349. return 0;
  350. }
  351. /**
  352. * ath5k_hw_get_rx_filter - Get current rx filter
  353. *
  354. * @ah: The &struct ath5k_hw
  355. *
  356. * Returns the RX filter by reading rx filter and
  357. * phy error filter registers. RX filter is used
  358. * to set the allowed frame types that PCU will accept
  359. * and pass to the driver. For a list of frame types
  360. * check out reg.h.
  361. */
  362. u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
  363. {
  364. u32 data, filter = 0;
  365. ATH5K_TRACE(ah->ah_sc);
  366. filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
  367. /*Radar detection for 5212*/
  368. if (ah->ah_version == AR5K_AR5212) {
  369. data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
  370. if (data & AR5K_PHY_ERR_FIL_RADAR)
  371. filter |= AR5K_RX_FILTER_RADARERR;
  372. if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
  373. filter |= AR5K_RX_FILTER_PHYERR;
  374. }
  375. return filter;
  376. }
  377. /**
  378. * ath5k_hw_set_rx_filter - Set rx filter
  379. *
  380. * @ah: The &struct ath5k_hw
  381. * @filter: RX filter mask (see reg.h)
  382. *
  383. * Sets RX filter register and also handles PHY error filter
  384. * register on 5212 and newer chips so that we have proper PHY
  385. * error reporting.
  386. */
  387. void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
  388. {
  389. u32 data = 0;
  390. ATH5K_TRACE(ah->ah_sc);
  391. /* Set PHY error filter register on 5212*/
  392. if (ah->ah_version == AR5K_AR5212) {
  393. if (filter & AR5K_RX_FILTER_RADARERR)
  394. data |= AR5K_PHY_ERR_FIL_RADAR;
  395. if (filter & AR5K_RX_FILTER_PHYERR)
  396. data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
  397. }
  398. /*
  399. * The AR5210 uses promiscous mode to detect radar activity
  400. */
  401. if (ah->ah_version == AR5K_AR5210 &&
  402. (filter & AR5K_RX_FILTER_RADARERR)) {
  403. filter &= ~AR5K_RX_FILTER_RADARERR;
  404. filter |= AR5K_RX_FILTER_PROM;
  405. }
  406. /*Zero length DMA (phy error reporting) */
  407. if (data)
  408. AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  409. else
  410. AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  411. /*Write RX Filter register*/
  412. ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
  413. /*Write PHY error filter register on 5212*/
  414. if (ah->ah_version == AR5K_AR5212)
  415. ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
  416. }
  417. /****************\
  418. * Beacon control *
  419. \****************/
  420. /**
  421. * ath5k_hw_get_tsf32 - Get a 32bit TSF
  422. *
  423. * @ah: The &struct ath5k_hw
  424. *
  425. * Returns lower 32 bits of current TSF
  426. */
  427. u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
  428. {
  429. ATH5K_TRACE(ah->ah_sc);
  430. return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  431. }
  432. /**
  433. * ath5k_hw_get_tsf64 - Get the full 64bit TSF
  434. *
  435. * @ah: The &struct ath5k_hw
  436. *
  437. * Returns the current TSF
  438. */
  439. u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
  440. {
  441. u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  442. ATH5K_TRACE(ah->ah_sc);
  443. return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
  444. }
  445. /**
  446. * ath5k_hw_set_tsf64 - Set a new 64bit TSF
  447. *
  448. * @ah: The &struct ath5k_hw
  449. * @tsf64: The new 64bit TSF
  450. *
  451. * Sets the new TSF
  452. */
  453. void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
  454. {
  455. ATH5K_TRACE(ah->ah_sc);
  456. ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
  457. ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
  458. }
  459. /**
  460. * ath5k_hw_reset_tsf - Force a TSF reset
  461. *
  462. * @ah: The &struct ath5k_hw
  463. *
  464. * Forces a TSF reset on PCU
  465. */
  466. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  467. {
  468. u32 val;
  469. ATH5K_TRACE(ah->ah_sc);
  470. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  471. /*
  472. * Each write to the RESET_TSF bit toggles a hardware internal
  473. * signal to reset TSF, but if left high it will cause a TSF reset
  474. * on the next chip reset as well. Thus we always write the value
  475. * twice to clear the signal.
  476. */
  477. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  478. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  479. }
  480. /*
  481. * Initialize beacon timers
  482. */
  483. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  484. {
  485. u32 timer1, timer2, timer3;
  486. ATH5K_TRACE(ah->ah_sc);
  487. /*
  488. * Set the additional timers by mode
  489. */
  490. switch (ah->ah_op_mode) {
  491. case NL80211_IFTYPE_MONITOR:
  492. case NL80211_IFTYPE_STATION:
  493. /* In STA mode timer1 is used as next wakeup
  494. * timer and timer2 as next CFP duration start
  495. * timer. Both in 1/8TUs. */
  496. /* TODO: PCF handling */
  497. if (ah->ah_version == AR5K_AR5210) {
  498. timer1 = 0xffffffff;
  499. timer2 = 0xffffffff;
  500. } else {
  501. timer1 = 0x0000ffff;
  502. timer2 = 0x0007ffff;
  503. }
  504. /* Mark associated AP as PCF incapable for now */
  505. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
  506. break;
  507. case NL80211_IFTYPE_ADHOC:
  508. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
  509. default:
  510. /* On non-STA modes timer1 is used as next DMA
  511. * beacon alert (DBA) timer and timer2 as next
  512. * software beacon alert. Both in 1/8TUs. */
  513. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  514. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  515. break;
  516. }
  517. /* Timer3 marks the end of our ATIM window
  518. * a zero length window is not allowed because
  519. * we 'll get no beacons */
  520. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  521. /*
  522. * Set the beacon register and enable all timers.
  523. */
  524. /* When in AP or Mesh Point mode zero timer0 to start TSF */
  525. if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
  526. ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
  527. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  528. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  529. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  530. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  531. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  532. /* Force a TSF reset if requested and enable beacons */
  533. if (interval & AR5K_BEACON_RESET_TSF)
  534. ath5k_hw_reset_tsf(ah);
  535. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  536. AR5K_BEACON_ENABLE),
  537. AR5K_BEACON);
  538. /* Flush any pending BMISS interrupts on ISR by
  539. * performing a clear-on-write operation on PISR
  540. * register for the BMISS bit (writing a bit on
  541. * ISR togles a reset for that bit and leaves
  542. * the rest bits intact) */
  543. if (ah->ah_version == AR5K_AR5210)
  544. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
  545. else
  546. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
  547. /* TODO: Set enchanced sleep registers on AR5212
  548. * based on vif->bss_conf params, until then
  549. * disable power save reporting.*/
  550. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
  551. }
  552. #if 0
  553. /*
  554. * Set beacon timers
  555. */
  556. int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
  557. const struct ath5k_beacon_state *state)
  558. {
  559. u32 cfp_period, next_cfp, dtim, interval, next_beacon;
  560. /*
  561. * TODO: should be changed through *state
  562. * review struct ath5k_beacon_state struct
  563. *
  564. * XXX: These are used for cfp period bellow, are they
  565. * ok ? Is it O.K. for tsf here to be 0 or should we use
  566. * get_tsf ?
  567. */
  568. u32 dtim_count = 0; /* XXX */
  569. u32 cfp_count = 0; /* XXX */
  570. u32 tsf = 0; /* XXX */
  571. ATH5K_TRACE(ah->ah_sc);
  572. /* Return on an invalid beacon state */
  573. if (state->bs_interval < 1)
  574. return -EINVAL;
  575. interval = state->bs_interval;
  576. dtim = state->bs_dtim_period;
  577. /*
  578. * PCF support?
  579. */
  580. if (state->bs_cfp_period > 0) {
  581. /*
  582. * Enable PCF mode and set the CFP
  583. * (Contention Free Period) and timer registers
  584. */
  585. cfp_period = state->bs_cfp_period * state->bs_dtim_period *
  586. state->bs_interval;
  587. next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
  588. state->bs_interval;
  589. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
  590. AR5K_STA_ID1_DEFAULT_ANTENNA |
  591. AR5K_STA_ID1_PCF);
  592. ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
  593. ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
  594. AR5K_CFP_DUR);
  595. ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
  596. next_cfp)) << 3, AR5K_TIMER2);
  597. } else {
  598. /* Disable PCF mode */
  599. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  600. AR5K_STA_ID1_DEFAULT_ANTENNA |
  601. AR5K_STA_ID1_PCF);
  602. }
  603. /*
  604. * Enable the beacon timer register
  605. */
  606. ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
  607. /*
  608. * Start the beacon timers
  609. */
  610. ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
  611. ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
  612. AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
  613. AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
  614. AR5K_BEACON_PERIOD), AR5K_BEACON);
  615. /*
  616. * Write new beacon miss threshold, if it appears to be valid
  617. * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
  618. * and return if its not in range. We can test this by reading value and
  619. * setting value to a largest value and seeing which values register.
  620. */
  621. AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
  622. state->bs_bmiss_threshold);
  623. /*
  624. * Set sleep control register
  625. * XXX: Didn't find this in 5210 code but since this register
  626. * exists also in ar5k's 5210 headers i leave it as common code.
  627. */
  628. AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
  629. (state->bs_sleep_duration - 3) << 3);
  630. /*
  631. * Set enhanced sleep registers on 5212
  632. */
  633. if (ah->ah_version == AR5K_AR5212) {
  634. if (state->bs_sleep_duration > state->bs_interval &&
  635. roundup(state->bs_sleep_duration, interval) ==
  636. state->bs_sleep_duration)
  637. interval = state->bs_sleep_duration;
  638. if (state->bs_sleep_duration > dtim && (dtim == 0 ||
  639. roundup(state->bs_sleep_duration, dtim) ==
  640. state->bs_sleep_duration))
  641. dtim = state->bs_sleep_duration;
  642. if (interval > dtim)
  643. return -EINVAL;
  644. next_beacon = interval == dtim ? state->bs_next_dtim :
  645. state->bs_next_beacon;
  646. ath5k_hw_reg_write(ah,
  647. AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
  648. AR5K_SLEEP0_NEXT_DTIM) |
  649. AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
  650. AR5K_SLEEP0_ENH_SLEEP_EN |
  651. AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
  652. ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
  653. AR5K_SLEEP1_NEXT_TIM) |
  654. AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
  655. ath5k_hw_reg_write(ah,
  656. AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
  657. AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
  658. }
  659. return 0;
  660. }
  661. /*
  662. * Reset beacon timers
  663. */
  664. void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
  665. {
  666. ATH5K_TRACE(ah->ah_sc);
  667. /*
  668. * Disable beacon timer
  669. */
  670. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  671. /*
  672. * Disable some beacon register values
  673. */
  674. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  675. AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
  676. ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
  677. }
  678. /*
  679. * Wait for beacon queue to finish
  680. */
  681. int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
  682. {
  683. unsigned int i;
  684. int ret;
  685. ATH5K_TRACE(ah->ah_sc);
  686. /* 5210 doesn't have QCU*/
  687. if (ah->ah_version == AR5K_AR5210) {
  688. /*
  689. * Wait for beaconn queue to finish by checking
  690. * Control Register and Beacon Status Register.
  691. */
  692. for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
  693. if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
  694. ||
  695. !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
  696. break;
  697. udelay(10);
  698. }
  699. /* Timeout... */
  700. if (i <= 0) {
  701. /*
  702. * Re-schedule the beacon queue
  703. */
  704. ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
  705. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
  706. AR5K_BCR);
  707. return -EIO;
  708. }
  709. ret = 0;
  710. } else {
  711. /*5211/5212*/
  712. ret = ath5k_hw_register_timeout(ah,
  713. AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
  714. AR5K_QCU_STS_FRMPENDCNT, 0, false);
  715. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
  716. return -EIO;
  717. }
  718. return ret;
  719. }
  720. #endif
  721. /*********************\
  722. * Key table functions *
  723. \*********************/
  724. /*
  725. * Reset a key entry on the table
  726. */
  727. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  728. {
  729. unsigned int i, type;
  730. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  731. ATH5K_TRACE(ah->ah_sc);
  732. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  733. type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
  734. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  735. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  736. /* Reset associated MIC entry if TKIP
  737. * is enabled located at offset (entry + 64) */
  738. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  739. AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
  740. for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
  741. ath5k_hw_reg_write(ah, 0,
  742. AR5K_KEYTABLE_OFF(micentry, i));
  743. }
  744. /*
  745. * Set NULL encryption on AR5212+
  746. *
  747. * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
  748. * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
  749. *
  750. * Note2: Windows driver (ndiswrapper) sets this to
  751. * 0x00000714 instead of 0x00000007
  752. */
  753. if (ah->ah_version >= AR5K_AR5211) {
  754. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  755. AR5K_KEYTABLE_TYPE(entry));
  756. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  757. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  758. AR5K_KEYTABLE_TYPE(micentry));
  759. }
  760. }
  761. return 0;
  762. }
  763. /*
  764. * Check if a table entry is valid
  765. */
  766. int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
  767. {
  768. ATH5K_TRACE(ah->ah_sc);
  769. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  770. /* Check the validation flag at the end of the entry */
  771. return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
  772. AR5K_KEYTABLE_VALID;
  773. }
  774. static
  775. int ath5k_keycache_type(const struct ieee80211_key_conf *key)
  776. {
  777. switch (key->alg) {
  778. case ALG_TKIP:
  779. return AR5K_KEYTABLE_TYPE_TKIP;
  780. case ALG_CCMP:
  781. return AR5K_KEYTABLE_TYPE_CCM;
  782. case ALG_WEP:
  783. if (key->keylen == WLAN_KEY_LEN_WEP40)
  784. return AR5K_KEYTABLE_TYPE_40;
  785. else if (key->keylen == WLAN_KEY_LEN_WEP104)
  786. return AR5K_KEYTABLE_TYPE_104;
  787. return -EINVAL;
  788. default:
  789. return -EINVAL;
  790. }
  791. return -EINVAL;
  792. }
  793. /*
  794. * Set a key entry on the table
  795. */
  796. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  797. const struct ieee80211_key_conf *key, const u8 *mac)
  798. {
  799. unsigned int i;
  800. int keylen;
  801. __le32 key_v[5] = {};
  802. __le32 key0 = 0, key1 = 0;
  803. __le32 *rxmic, *txmic;
  804. int keytype;
  805. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  806. bool is_tkip;
  807. const u8 *key_ptr;
  808. ATH5K_TRACE(ah->ah_sc);
  809. is_tkip = (key->alg == ALG_TKIP);
  810. /*
  811. * key->keylen comes in from mac80211 in bytes.
  812. * TKIP is 128 bit + 128 bit mic
  813. */
  814. keylen = (is_tkip) ? (128 / 8) : key->keylen;
  815. if (entry > AR5K_KEYTABLE_SIZE ||
  816. (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
  817. return -EOPNOTSUPP;
  818. if (unlikely(keylen > 16))
  819. return -EOPNOTSUPP;
  820. keytype = ath5k_keycache_type(key);
  821. if (keytype < 0)
  822. return keytype;
  823. /*
  824. * each key block is 6 bytes wide, written as pairs of
  825. * alternating 32 and 16 bit le values.
  826. */
  827. key_ptr = key->key;
  828. for (i = 0; keylen >= 6; keylen -= 6) {
  829. memcpy(&key_v[i], key_ptr, 6);
  830. i += 2;
  831. key_ptr += 6;
  832. }
  833. if (keylen)
  834. memcpy(&key_v[i], key_ptr, keylen);
  835. /* intentionally corrupt key until mic is installed */
  836. if (is_tkip) {
  837. key0 = key_v[0] = ~key_v[0];
  838. key1 = key_v[1] = ~key_v[1];
  839. }
  840. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  841. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  842. AR5K_KEYTABLE_OFF(entry, i));
  843. ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
  844. if (is_tkip) {
  845. /* Install rx/tx MIC */
  846. rxmic = (__le32 *) &key->key[16];
  847. txmic = (__le32 *) &key->key[24];
  848. if (ah->ah_combined_mic) {
  849. key_v[0] = rxmic[0];
  850. key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
  851. key_v[2] = rxmic[1];
  852. key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
  853. key_v[4] = txmic[1];
  854. } else {
  855. key_v[0] = rxmic[0];
  856. key_v[1] = 0;
  857. key_v[2] = rxmic[1];
  858. key_v[3] = 0;
  859. key_v[4] = 0;
  860. }
  861. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  862. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  863. AR5K_KEYTABLE_OFF(micentry, i));
  864. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  865. AR5K_KEYTABLE_TYPE(micentry));
  866. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
  867. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
  868. /* restore first 2 words of key */
  869. ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
  870. AR5K_KEYTABLE_OFF(entry, 0));
  871. ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
  872. AR5K_KEYTABLE_OFF(entry, 1));
  873. }
  874. return ath5k_hw_set_key_lladdr(ah, entry, mac);
  875. }
  876. int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
  877. {
  878. u32 low_id, high_id;
  879. ATH5K_TRACE(ah->ah_sc);
  880. /* Invalid entry (key table overflow) */
  881. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  882. /*
  883. * MAC may be NULL if it's a broadcast key. In this case no need to
  884. * to compute get_unaligned_le32 and get_unaligned_le16 as we
  885. * already know it.
  886. */
  887. if (!mac) {
  888. low_id = 0xffffffff;
  889. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  890. } else {
  891. low_id = get_unaligned_le32(mac);
  892. high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
  893. }
  894. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  895. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  896. return 0;
  897. }