pcu.c 22 KB

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