pcu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. * @op_mode: &enum nl80211_iftype operating mode
  38. *
  39. * Initialize PCU for the various operating modes (AP/STA etc)
  40. */
  41. int ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
  42. {
  43. struct ath_common *common = ath5k_hw_common(ah);
  44. u32 pcu_reg, beacon_reg, low_id, high_id;
  45. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
  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 (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. *
  103. * Reads MIB counters from PCU and updates sw statistics. Is called after a
  104. * MIB interrupt, because one of these counters might have reached their maximum
  105. * and triggered the MIB interrupt, to let us read and clear the counter.
  106. *
  107. * Is called in interrupt context!
  108. */
  109. void ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
  110. {
  111. struct ath5k_statistics *stats = &ah->ah_sc->stats;
  112. /* Read-And-Clear */
  113. stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
  114. stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
  115. stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
  116. stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
  117. stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
  118. }
  119. /**
  120. * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
  121. *
  122. * @ah: The &struct ath5k_hw
  123. * @high: Flag to determine if we want to use high transmition rate
  124. * for ACKs or not
  125. *
  126. * If high flag is set, we tell hw to use a set of control rates based on
  127. * the current transmition rate (check out control_rates array inside reset.c).
  128. * If not hw just uses the lowest rate available for the current modulation
  129. * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
  130. */
  131. void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
  132. {
  133. if (ah->ah_version != AR5K_AR5212)
  134. return;
  135. else {
  136. u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
  137. if (high)
  138. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
  139. else
  140. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
  141. }
  142. }
  143. /******************\
  144. * ACK/CTS Timeouts *
  145. \******************/
  146. /**
  147. * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
  148. *
  149. * @ah: The &struct ath5k_hw
  150. * @timeout: Timeout in usec
  151. */
  152. static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
  153. {
  154. ATH5K_TRACE(ah->ah_sc);
  155. if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
  156. <= timeout)
  157. return -EINVAL;
  158. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
  159. ath5k_hw_htoclock(ah, timeout));
  160. return 0;
  161. }
  162. /**
  163. * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
  164. *
  165. * @ah: The &struct ath5k_hw
  166. * @timeout: Timeout in usec
  167. */
  168. static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
  169. {
  170. ATH5K_TRACE(ah->ah_sc);
  171. if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
  172. <= timeout)
  173. return -EINVAL;
  174. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
  175. ath5k_hw_htoclock(ah, timeout));
  176. return 0;
  177. }
  178. /**
  179. * ath5k_hw_htoclock - Translate usec to hw clock units
  180. *
  181. * @ah: The &struct ath5k_hw
  182. * @usec: value in microseconds
  183. */
  184. unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
  185. {
  186. return usec * ath5k_hw_get_clockrate(ah);
  187. }
  188. /**
  189. * ath5k_hw_clocktoh - Translate hw clock units to usec
  190. * @clock: value in hw clock units
  191. */
  192. unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
  193. {
  194. return clock / ath5k_hw_get_clockrate(ah);
  195. }
  196. /**
  197. * ath5k_hw_get_clockrate - Get the clock rate for current mode
  198. *
  199. * @ah: The &struct ath5k_hw
  200. */
  201. unsigned int ath5k_hw_get_clockrate(struct ath5k_hw *ah)
  202. {
  203. struct ieee80211_channel *channel = ah->ah_current_channel;
  204. int clock;
  205. if (channel->hw_value & CHANNEL_5GHZ)
  206. clock = 40; /* 802.11a */
  207. else if (channel->hw_value & CHANNEL_CCK)
  208. clock = 22; /* 802.11b */
  209. else
  210. clock = 44; /* 802.11g */
  211. /* Clock rate in turbo modes is twice the normal rate */
  212. if (channel->hw_value & CHANNEL_TURBO)
  213. clock *= 2;
  214. return clock;
  215. }
  216. /**
  217. * ath5k_hw_get_default_slottime - Get the default slot time for current mode
  218. *
  219. * @ah: The &struct ath5k_hw
  220. */
  221. static unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
  222. {
  223. struct ieee80211_channel *channel = ah->ah_current_channel;
  224. if (channel->hw_value & CHANNEL_TURBO)
  225. return 6; /* both turbo modes */
  226. if (channel->hw_value & CHANNEL_CCK)
  227. return 20; /* 802.11b */
  228. return 9; /* 802.11 a/g */
  229. }
  230. /**
  231. * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
  232. *
  233. * @ah: The &struct ath5k_hw
  234. */
  235. static unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
  236. {
  237. struct ieee80211_channel *channel = ah->ah_current_channel;
  238. if (channel->hw_value & CHANNEL_TURBO)
  239. return 8; /* both turbo modes */
  240. if (channel->hw_value & CHANNEL_5GHZ)
  241. return 16; /* 802.11a */
  242. return 10; /* 802.11 b/g */
  243. }
  244. /**
  245. * ath5k_hw_set_lladdr - Set station id
  246. *
  247. * @ah: The &struct ath5k_hw
  248. * @mac: The card's mac address
  249. *
  250. * Set station id on hw using the provided mac address
  251. */
  252. int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
  253. {
  254. struct ath_common *common = ath5k_hw_common(ah);
  255. u32 low_id, high_id;
  256. u32 pcu_reg;
  257. ATH5K_TRACE(ah->ah_sc);
  258. /* Set new station ID */
  259. memcpy(common->macaddr, mac, ETH_ALEN);
  260. pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
  261. low_id = get_unaligned_le32(mac);
  262. high_id = get_unaligned_le16(mac + 4);
  263. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  264. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  265. return 0;
  266. }
  267. /**
  268. * ath5k_hw_set_associd - Set BSSID for association
  269. *
  270. * @ah: The &struct ath5k_hw
  271. * @bssid: BSSID
  272. * @assoc_id: Assoc id
  273. *
  274. * Sets the BSSID which trigers the "SME Join" operation
  275. */
  276. void ath5k_hw_set_associd(struct ath5k_hw *ah)
  277. {
  278. struct ath_common *common = ath5k_hw_common(ah);
  279. u16 tim_offset = 0;
  280. /*
  281. * Set simple BSSID mask on 5212
  282. */
  283. if (ah->ah_version == AR5K_AR5212)
  284. ath_hw_setbssidmask(common);
  285. /*
  286. * Set BSSID which triggers the "SME Join" operation
  287. */
  288. ath5k_hw_reg_write(ah,
  289. get_unaligned_le32(common->curbssid),
  290. AR5K_BSS_ID0);
  291. ath5k_hw_reg_write(ah,
  292. get_unaligned_le16(common->curbssid + 4) |
  293. ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
  294. AR5K_BSS_ID1);
  295. if (common->curaid == 0) {
  296. ath5k_hw_disable_pspoll(ah);
  297. return;
  298. }
  299. AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
  300. tim_offset ? tim_offset + 4 : 0);
  301. ath5k_hw_enable_pspoll(ah, NULL, 0);
  302. }
  303. void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
  304. {
  305. struct ath_common *common = ath5k_hw_common(ah);
  306. ATH5K_TRACE(ah->ah_sc);
  307. /* Cache bssid mask so that we can restore it
  308. * on reset */
  309. memcpy(common->bssidmask, mask, ETH_ALEN);
  310. if (ah->ah_version == AR5K_AR5212)
  311. ath_hw_setbssidmask(common);
  312. }
  313. /************\
  314. * RX Control *
  315. \************/
  316. /**
  317. * ath5k_hw_start_rx_pcu - Start RX engine
  318. *
  319. * @ah: The &struct ath5k_hw
  320. *
  321. * Starts RX engine on PCU so that hw can process RXed frames
  322. * (ACK etc).
  323. *
  324. * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
  325. */
  326. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  327. {
  328. ATH5K_TRACE(ah->ah_sc);
  329. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  330. }
  331. /**
  332. * at5k_hw_stop_rx_pcu - Stop RX engine
  333. *
  334. * @ah: The &struct ath5k_hw
  335. *
  336. * Stops RX engine on PCU
  337. *
  338. * TODO: Detach ANI here
  339. */
  340. void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
  341. {
  342. ATH5K_TRACE(ah->ah_sc);
  343. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  344. }
  345. /*
  346. * Set multicast filter
  347. */
  348. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  349. {
  350. ATH5K_TRACE(ah->ah_sc);
  351. /* Set the multicat filter */
  352. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  353. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  354. }
  355. /**
  356. * ath5k_hw_get_rx_filter - Get current rx filter
  357. *
  358. * @ah: The &struct ath5k_hw
  359. *
  360. * Returns the RX filter by reading rx filter and
  361. * phy error filter registers. RX filter is used
  362. * to set the allowed frame types that PCU will accept
  363. * and pass to the driver. For a list of frame types
  364. * check out reg.h.
  365. */
  366. u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
  367. {
  368. u32 data, filter = 0;
  369. ATH5K_TRACE(ah->ah_sc);
  370. filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
  371. /*Radar detection for 5212*/
  372. if (ah->ah_version == AR5K_AR5212) {
  373. data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
  374. if (data & AR5K_PHY_ERR_FIL_RADAR)
  375. filter |= AR5K_RX_FILTER_RADARERR;
  376. if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
  377. filter |= AR5K_RX_FILTER_PHYERR;
  378. }
  379. return filter;
  380. }
  381. /**
  382. * ath5k_hw_set_rx_filter - Set rx filter
  383. *
  384. * @ah: The &struct ath5k_hw
  385. * @filter: RX filter mask (see reg.h)
  386. *
  387. * Sets RX filter register and also handles PHY error filter
  388. * register on 5212 and newer chips so that we have proper PHY
  389. * error reporting.
  390. */
  391. void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
  392. {
  393. u32 data = 0;
  394. ATH5K_TRACE(ah->ah_sc);
  395. /* Set PHY error filter register on 5212*/
  396. if (ah->ah_version == AR5K_AR5212) {
  397. if (filter & AR5K_RX_FILTER_RADARERR)
  398. data |= AR5K_PHY_ERR_FIL_RADAR;
  399. if (filter & AR5K_RX_FILTER_PHYERR)
  400. data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
  401. }
  402. /*
  403. * The AR5210 uses promiscous mode to detect radar activity
  404. */
  405. if (ah->ah_version == AR5K_AR5210 &&
  406. (filter & AR5K_RX_FILTER_RADARERR)) {
  407. filter &= ~AR5K_RX_FILTER_RADARERR;
  408. filter |= AR5K_RX_FILTER_PROM;
  409. }
  410. /*Zero length DMA (phy error reporting) */
  411. if (data)
  412. AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  413. else
  414. AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  415. /*Write RX Filter register*/
  416. ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
  417. /*Write PHY error filter register on 5212*/
  418. if (ah->ah_version == AR5K_AR5212)
  419. ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
  420. }
  421. /****************\
  422. * Beacon control *
  423. \****************/
  424. #define ATH5K_MAX_TSF_READ 10
  425. /**
  426. * ath5k_hw_get_tsf64 - Get the full 64bit TSF
  427. *
  428. * @ah: The &struct ath5k_hw
  429. *
  430. * Returns the current TSF
  431. */
  432. u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
  433. {
  434. u32 tsf_lower, tsf_upper1, tsf_upper2;
  435. int i;
  436. /*
  437. * While reading TSF upper and then lower part, the clock is still
  438. * counting (or jumping in case of IBSS merge) so we might get
  439. * inconsistent values. To avoid this, we read the upper part again
  440. * and check it has not been changed. We make the hypothesis that a
  441. * maximum of 3 changes can happens in a row (we use 10 as a safe
  442. * value).
  443. *
  444. * Impact on performance is pretty small, since in most cases, only
  445. * 3 register reads are needed.
  446. */
  447. tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  448. for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
  449. tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  450. tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  451. if (tsf_upper2 == tsf_upper1)
  452. break;
  453. tsf_upper1 = tsf_upper2;
  454. }
  455. WARN_ON( i == ATH5K_MAX_TSF_READ );
  456. ATH5K_TRACE(ah->ah_sc);
  457. return (((u64)tsf_upper1 << 32) | tsf_lower);
  458. }
  459. /**
  460. * ath5k_hw_set_tsf64 - Set a new 64bit TSF
  461. *
  462. * @ah: The &struct ath5k_hw
  463. * @tsf64: The new 64bit TSF
  464. *
  465. * Sets the new TSF
  466. */
  467. void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
  468. {
  469. ATH5K_TRACE(ah->ah_sc);
  470. ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
  471. ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
  472. }
  473. /**
  474. * ath5k_hw_reset_tsf - Force a TSF reset
  475. *
  476. * @ah: The &struct ath5k_hw
  477. *
  478. * Forces a TSF reset on PCU
  479. */
  480. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  481. {
  482. u32 val;
  483. ATH5K_TRACE(ah->ah_sc);
  484. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  485. /*
  486. * Each write to the RESET_TSF bit toggles a hardware internal
  487. * signal to reset TSF, but if left high it will cause a TSF reset
  488. * on the next chip reset as well. Thus we always write the value
  489. * twice to clear the signal.
  490. */
  491. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  492. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  493. }
  494. /*
  495. * Initialize beacon timers
  496. */
  497. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  498. {
  499. u32 timer1, timer2, timer3;
  500. ATH5K_TRACE(ah->ah_sc);
  501. /*
  502. * Set the additional timers by mode
  503. */
  504. switch (ah->ah_sc->opmode) {
  505. case NL80211_IFTYPE_MONITOR:
  506. case NL80211_IFTYPE_STATION:
  507. /* In STA mode timer1 is used as next wakeup
  508. * timer and timer2 as next CFP duration start
  509. * timer. Both in 1/8TUs. */
  510. /* TODO: PCF handling */
  511. if (ah->ah_version == AR5K_AR5210) {
  512. timer1 = 0xffffffff;
  513. timer2 = 0xffffffff;
  514. } else {
  515. timer1 = 0x0000ffff;
  516. timer2 = 0x0007ffff;
  517. }
  518. /* Mark associated AP as PCF incapable for now */
  519. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
  520. break;
  521. case NL80211_IFTYPE_ADHOC:
  522. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
  523. default:
  524. /* On non-STA modes timer1 is used as next DMA
  525. * beacon alert (DBA) timer and timer2 as next
  526. * software beacon alert. Both in 1/8TUs. */
  527. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  528. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  529. break;
  530. }
  531. /* Timer3 marks the end of our ATIM window
  532. * a zero length window is not allowed because
  533. * we 'll get no beacons */
  534. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  535. /*
  536. * Set the beacon register and enable all timers.
  537. */
  538. /* When in AP or Mesh Point mode zero timer0 to start TSF */
  539. if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
  540. ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
  541. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  542. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  543. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  544. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  545. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  546. /* Force a TSF reset if requested and enable beacons */
  547. if (interval & AR5K_BEACON_RESET_TSF)
  548. ath5k_hw_reset_tsf(ah);
  549. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  550. AR5K_BEACON_ENABLE),
  551. AR5K_BEACON);
  552. /* Flush any pending BMISS interrupts on ISR by
  553. * performing a clear-on-write operation on PISR
  554. * register for the BMISS bit (writing a bit on
  555. * ISR togles a reset for that bit and leaves
  556. * the rest bits intact) */
  557. if (ah->ah_version == AR5K_AR5210)
  558. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
  559. else
  560. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
  561. /* TODO: Set enchanced sleep registers on AR5212
  562. * based on vif->bss_conf params, until then
  563. * disable power save reporting.*/
  564. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
  565. }
  566. /*********************\
  567. * Key table functions *
  568. \*********************/
  569. /*
  570. * Reset a key entry on the table
  571. */
  572. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  573. {
  574. unsigned int i, type;
  575. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  576. ATH5K_TRACE(ah->ah_sc);
  577. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  578. type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
  579. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  580. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  581. /* Reset associated MIC entry if TKIP
  582. * is enabled located at offset (entry + 64) */
  583. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  584. AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
  585. for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
  586. ath5k_hw_reg_write(ah, 0,
  587. AR5K_KEYTABLE_OFF(micentry, i));
  588. }
  589. /*
  590. * Set NULL encryption on AR5212+
  591. *
  592. * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
  593. * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
  594. *
  595. * Note2: Windows driver (ndiswrapper) sets this to
  596. * 0x00000714 instead of 0x00000007
  597. */
  598. if (ah->ah_version >= AR5K_AR5211) {
  599. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  600. AR5K_KEYTABLE_TYPE(entry));
  601. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  602. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  603. AR5K_KEYTABLE_TYPE(micentry));
  604. }
  605. }
  606. return 0;
  607. }
  608. static
  609. int ath5k_keycache_type(const struct ieee80211_key_conf *key)
  610. {
  611. switch (key->alg) {
  612. case ALG_TKIP:
  613. return AR5K_KEYTABLE_TYPE_TKIP;
  614. case ALG_CCMP:
  615. return AR5K_KEYTABLE_TYPE_CCM;
  616. case ALG_WEP:
  617. if (key->keylen == WLAN_KEY_LEN_WEP40)
  618. return AR5K_KEYTABLE_TYPE_40;
  619. else if (key->keylen == WLAN_KEY_LEN_WEP104)
  620. return AR5K_KEYTABLE_TYPE_104;
  621. return -EINVAL;
  622. default:
  623. return -EINVAL;
  624. }
  625. return -EINVAL;
  626. }
  627. /*
  628. * Set a key entry on the table
  629. */
  630. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  631. const struct ieee80211_key_conf *key, const u8 *mac)
  632. {
  633. unsigned int i;
  634. int keylen;
  635. __le32 key_v[5] = {};
  636. __le32 key0 = 0, key1 = 0;
  637. __le32 *rxmic, *txmic;
  638. int keytype;
  639. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  640. bool is_tkip;
  641. const u8 *key_ptr;
  642. ATH5K_TRACE(ah->ah_sc);
  643. is_tkip = (key->alg == ALG_TKIP);
  644. /*
  645. * key->keylen comes in from mac80211 in bytes.
  646. * TKIP is 128 bit + 128 bit mic
  647. */
  648. keylen = (is_tkip) ? (128 / 8) : key->keylen;
  649. if (entry > AR5K_KEYTABLE_SIZE ||
  650. (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
  651. return -EOPNOTSUPP;
  652. if (unlikely(keylen > 16))
  653. return -EOPNOTSUPP;
  654. keytype = ath5k_keycache_type(key);
  655. if (keytype < 0)
  656. return keytype;
  657. /*
  658. * each key block is 6 bytes wide, written as pairs of
  659. * alternating 32 and 16 bit le values.
  660. */
  661. key_ptr = key->key;
  662. for (i = 0; keylen >= 6; keylen -= 6) {
  663. memcpy(&key_v[i], key_ptr, 6);
  664. i += 2;
  665. key_ptr += 6;
  666. }
  667. if (keylen)
  668. memcpy(&key_v[i], key_ptr, keylen);
  669. /* intentionally corrupt key until mic is installed */
  670. if (is_tkip) {
  671. key0 = key_v[0] = ~key_v[0];
  672. key1 = key_v[1] = ~key_v[1];
  673. }
  674. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  675. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  676. AR5K_KEYTABLE_OFF(entry, i));
  677. ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
  678. if (is_tkip) {
  679. /* Install rx/tx MIC */
  680. rxmic = (__le32 *) &key->key[16];
  681. txmic = (__le32 *) &key->key[24];
  682. if (ah->ah_combined_mic) {
  683. key_v[0] = rxmic[0];
  684. key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
  685. key_v[2] = rxmic[1];
  686. key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
  687. key_v[4] = txmic[1];
  688. } else {
  689. key_v[0] = rxmic[0];
  690. key_v[1] = 0;
  691. key_v[2] = rxmic[1];
  692. key_v[3] = 0;
  693. key_v[4] = 0;
  694. }
  695. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  696. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  697. AR5K_KEYTABLE_OFF(micentry, i));
  698. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  699. AR5K_KEYTABLE_TYPE(micentry));
  700. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
  701. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
  702. /* restore first 2 words of key */
  703. ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
  704. AR5K_KEYTABLE_OFF(entry, 0));
  705. ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
  706. AR5K_KEYTABLE_OFF(entry, 1));
  707. }
  708. return ath5k_hw_set_key_lladdr(ah, entry, mac);
  709. }
  710. int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
  711. {
  712. u32 low_id, high_id;
  713. ATH5K_TRACE(ah->ah_sc);
  714. /* Invalid entry (key table overflow) */
  715. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  716. /*
  717. * MAC may be NULL if it's a broadcast key. In this case no need to
  718. * to compute get_unaligned_le32 and get_unaligned_le16 as we
  719. * already know it.
  720. */
  721. if (!mac) {
  722. low_id = 0xffffffff;
  723. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  724. } else {
  725. low_id = get_unaligned_le32(mac);
  726. high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
  727. }
  728. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  729. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  730. return 0;
  731. }
  732. /**
  733. * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
  734. *
  735. * @ah: The &struct ath5k_hw
  736. * @coverage_class: IEEE 802.11 coverage class number
  737. *
  738. * Sets slot time, ACK timeout and CTS timeout for given coverage class.
  739. */
  740. void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
  741. {
  742. /* As defined by IEEE 802.11-2007 17.3.8.6 */
  743. int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
  744. int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
  745. int cts_timeout = ack_timeout;
  746. ath5k_hw_set_slot_time(ah, slot_time);
  747. ath5k_hw_set_ack_timeout(ah, ack_timeout);
  748. ath5k_hw_set_cts_timeout(ah, cts_timeout);
  749. ah->ah_coverage_class = coverage_class;
  750. }