pcu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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_ENABLE_BITS(ah, AR5K_STA_ID1, val);
  139. else
  140. AR5K_REG_DISABLE_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. * TODO: Init ANI here
  326. */
  327. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  328. {
  329. ATH5K_TRACE(ah->ah_sc);
  330. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  331. }
  332. /**
  333. * at5k_hw_stop_rx_pcu - Stop RX engine
  334. *
  335. * @ah: The &struct ath5k_hw
  336. *
  337. * Stops RX engine on PCU
  338. *
  339. * TODO: Detach ANI here
  340. */
  341. void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
  342. {
  343. ATH5K_TRACE(ah->ah_sc);
  344. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  345. }
  346. /*
  347. * Set multicast filter
  348. */
  349. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  350. {
  351. ATH5K_TRACE(ah->ah_sc);
  352. /* Set the multicat filter */
  353. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  354. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  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_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. u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  435. ATH5K_TRACE(ah->ah_sc);
  436. return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
  437. }
  438. /**
  439. * ath5k_hw_set_tsf64 - Set a new 64bit TSF
  440. *
  441. * @ah: The &struct ath5k_hw
  442. * @tsf64: The new 64bit TSF
  443. *
  444. * Sets the new TSF
  445. */
  446. void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
  447. {
  448. ATH5K_TRACE(ah->ah_sc);
  449. ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
  450. ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
  451. }
  452. /**
  453. * ath5k_hw_reset_tsf - Force a TSF reset
  454. *
  455. * @ah: The &struct ath5k_hw
  456. *
  457. * Forces a TSF reset on PCU
  458. */
  459. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  460. {
  461. u32 val;
  462. ATH5K_TRACE(ah->ah_sc);
  463. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  464. /*
  465. * Each write to the RESET_TSF bit toggles a hardware internal
  466. * signal to reset TSF, but if left high it will cause a TSF reset
  467. * on the next chip reset as well. Thus we always write the value
  468. * twice to clear the signal.
  469. */
  470. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  471. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  472. }
  473. /*
  474. * Initialize beacon timers
  475. */
  476. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  477. {
  478. u32 timer1, timer2, timer3;
  479. ATH5K_TRACE(ah->ah_sc);
  480. /*
  481. * Set the additional timers by mode
  482. */
  483. switch (ah->ah_sc->opmode) {
  484. case NL80211_IFTYPE_MONITOR:
  485. case NL80211_IFTYPE_STATION:
  486. /* In STA mode timer1 is used as next wakeup
  487. * timer and timer2 as next CFP duration start
  488. * timer. Both in 1/8TUs. */
  489. /* TODO: PCF handling */
  490. if (ah->ah_version == AR5K_AR5210) {
  491. timer1 = 0xffffffff;
  492. timer2 = 0xffffffff;
  493. } else {
  494. timer1 = 0x0000ffff;
  495. timer2 = 0x0007ffff;
  496. }
  497. /* Mark associated AP as PCF incapable for now */
  498. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
  499. break;
  500. case NL80211_IFTYPE_ADHOC:
  501. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
  502. default:
  503. /* On non-STA modes timer1 is used as next DMA
  504. * beacon alert (DBA) timer and timer2 as next
  505. * software beacon alert. Both in 1/8TUs. */
  506. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  507. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  508. break;
  509. }
  510. /* Timer3 marks the end of our ATIM window
  511. * a zero length window is not allowed because
  512. * we 'll get no beacons */
  513. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  514. /*
  515. * Set the beacon register and enable all timers.
  516. */
  517. /* When in AP or Mesh Point mode zero timer0 to start TSF */
  518. if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
  519. ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
  520. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  521. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  522. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  523. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  524. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  525. /* Force a TSF reset if requested and enable beacons */
  526. if (interval & AR5K_BEACON_RESET_TSF)
  527. ath5k_hw_reset_tsf(ah);
  528. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  529. AR5K_BEACON_ENABLE),
  530. AR5K_BEACON);
  531. /* Flush any pending BMISS interrupts on ISR by
  532. * performing a clear-on-write operation on PISR
  533. * register for the BMISS bit (writing a bit on
  534. * ISR togles a reset for that bit and leaves
  535. * the rest bits intact) */
  536. if (ah->ah_version == AR5K_AR5210)
  537. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
  538. else
  539. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
  540. /* TODO: Set enchanced sleep registers on AR5212
  541. * based on vif->bss_conf params, until then
  542. * disable power save reporting.*/
  543. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
  544. }
  545. /*********************\
  546. * Key table functions *
  547. \*********************/
  548. /*
  549. * Reset a key entry on the table
  550. */
  551. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  552. {
  553. unsigned int i, type;
  554. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  555. ATH5K_TRACE(ah->ah_sc);
  556. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  557. type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
  558. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  559. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  560. /* Reset associated MIC entry if TKIP
  561. * is enabled located at offset (entry + 64) */
  562. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  563. AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
  564. for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
  565. ath5k_hw_reg_write(ah, 0,
  566. AR5K_KEYTABLE_OFF(micentry, i));
  567. }
  568. /*
  569. * Set NULL encryption on AR5212+
  570. *
  571. * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
  572. * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
  573. *
  574. * Note2: Windows driver (ndiswrapper) sets this to
  575. * 0x00000714 instead of 0x00000007
  576. */
  577. if (ah->ah_version >= AR5K_AR5211) {
  578. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  579. AR5K_KEYTABLE_TYPE(entry));
  580. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  581. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  582. AR5K_KEYTABLE_TYPE(micentry));
  583. }
  584. }
  585. return 0;
  586. }
  587. static
  588. int ath5k_keycache_type(const struct ieee80211_key_conf *key)
  589. {
  590. switch (key->alg) {
  591. case ALG_TKIP:
  592. return AR5K_KEYTABLE_TYPE_TKIP;
  593. case ALG_CCMP:
  594. return AR5K_KEYTABLE_TYPE_CCM;
  595. case ALG_WEP:
  596. if (key->keylen == WLAN_KEY_LEN_WEP40)
  597. return AR5K_KEYTABLE_TYPE_40;
  598. else if (key->keylen == WLAN_KEY_LEN_WEP104)
  599. return AR5K_KEYTABLE_TYPE_104;
  600. return -EINVAL;
  601. default:
  602. return -EINVAL;
  603. }
  604. return -EINVAL;
  605. }
  606. /*
  607. * Set a key entry on the table
  608. */
  609. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  610. const struct ieee80211_key_conf *key, const u8 *mac)
  611. {
  612. unsigned int i;
  613. int keylen;
  614. __le32 key_v[5] = {};
  615. __le32 key0 = 0, key1 = 0;
  616. __le32 *rxmic, *txmic;
  617. int keytype;
  618. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  619. bool is_tkip;
  620. const u8 *key_ptr;
  621. ATH5K_TRACE(ah->ah_sc);
  622. is_tkip = (key->alg == ALG_TKIP);
  623. /*
  624. * key->keylen comes in from mac80211 in bytes.
  625. * TKIP is 128 bit + 128 bit mic
  626. */
  627. keylen = (is_tkip) ? (128 / 8) : key->keylen;
  628. if (entry > AR5K_KEYTABLE_SIZE ||
  629. (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
  630. return -EOPNOTSUPP;
  631. if (unlikely(keylen > 16))
  632. return -EOPNOTSUPP;
  633. keytype = ath5k_keycache_type(key);
  634. if (keytype < 0)
  635. return keytype;
  636. /*
  637. * each key block is 6 bytes wide, written as pairs of
  638. * alternating 32 and 16 bit le values.
  639. */
  640. key_ptr = key->key;
  641. for (i = 0; keylen >= 6; keylen -= 6) {
  642. memcpy(&key_v[i], key_ptr, 6);
  643. i += 2;
  644. key_ptr += 6;
  645. }
  646. if (keylen)
  647. memcpy(&key_v[i], key_ptr, keylen);
  648. /* intentionally corrupt key until mic is installed */
  649. if (is_tkip) {
  650. key0 = key_v[0] = ~key_v[0];
  651. key1 = key_v[1] = ~key_v[1];
  652. }
  653. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  654. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  655. AR5K_KEYTABLE_OFF(entry, i));
  656. ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
  657. if (is_tkip) {
  658. /* Install rx/tx MIC */
  659. rxmic = (__le32 *) &key->key[16];
  660. txmic = (__le32 *) &key->key[24];
  661. if (ah->ah_combined_mic) {
  662. key_v[0] = rxmic[0];
  663. key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
  664. key_v[2] = rxmic[1];
  665. key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
  666. key_v[4] = txmic[1];
  667. } else {
  668. key_v[0] = rxmic[0];
  669. key_v[1] = 0;
  670. key_v[2] = rxmic[1];
  671. key_v[3] = 0;
  672. key_v[4] = 0;
  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(micentry, i));
  677. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  678. AR5K_KEYTABLE_TYPE(micentry));
  679. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
  680. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
  681. /* restore first 2 words of key */
  682. ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
  683. AR5K_KEYTABLE_OFF(entry, 0));
  684. ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
  685. AR5K_KEYTABLE_OFF(entry, 1));
  686. }
  687. return ath5k_hw_set_key_lladdr(ah, entry, mac);
  688. }
  689. int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
  690. {
  691. u32 low_id, high_id;
  692. ATH5K_TRACE(ah->ah_sc);
  693. /* Invalid entry (key table overflow) */
  694. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  695. /*
  696. * MAC may be NULL if it's a broadcast key. In this case no need to
  697. * to compute get_unaligned_le32 and get_unaligned_le16 as we
  698. * already know it.
  699. */
  700. if (!mac) {
  701. low_id = 0xffffffff;
  702. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  703. } else {
  704. low_id = get_unaligned_le32(mac);
  705. high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
  706. }
  707. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  708. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  709. return 0;
  710. }
  711. /**
  712. * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
  713. *
  714. * @ah: The &struct ath5k_hw
  715. * @coverage_class: IEEE 802.11 coverage class number
  716. *
  717. * Sets slot time, ACK timeout and CTS timeout for given coverage class.
  718. */
  719. void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
  720. {
  721. /* As defined by IEEE 802.11-2007 17.3.8.6 */
  722. int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
  723. int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
  724. int cts_timeout = ack_timeout;
  725. ath5k_hw_set_slot_time(ah, slot_time);
  726. ath5k_hw_set_ack_timeout(ah, ack_timeout);
  727. ath5k_hw_set_cts_timeout(ah, cts_timeout);
  728. ah->ah_coverage_class = coverage_class;
  729. }