pcu.c 26 KB

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