pcu.c 22 KB

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