pcu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 transmission 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 transmission 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_bssid - Set current BSSID on hw
  265. *
  266. * @ah: The &struct ath5k_hw
  267. *
  268. * Sets the current BSSID and BSSID mask we have from the
  269. * common struct into the hardware
  270. */
  271. void ath5k_hw_set_bssid(struct ath5k_hw *ah)
  272. {
  273. struct ath_common *common = ath5k_hw_common(ah);
  274. u16 tim_offset = 0;
  275. /*
  276. * Set BSSID mask on 5212
  277. */
  278. if (ah->ah_version == AR5K_AR5212)
  279. ath_hw_setbssidmask(common);
  280. /*
  281. * Set BSSID
  282. */
  283. ath5k_hw_reg_write(ah,
  284. get_unaligned_le32(common->curbssid),
  285. AR5K_BSS_ID0);
  286. ath5k_hw_reg_write(ah,
  287. get_unaligned_le16(common->curbssid + 4) |
  288. ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
  289. AR5K_BSS_ID1);
  290. if (common->curaid == 0) {
  291. ath5k_hw_disable_pspoll(ah);
  292. return;
  293. }
  294. AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
  295. tim_offset ? tim_offset + 4 : 0);
  296. ath5k_hw_enable_pspoll(ah, NULL, 0);
  297. }
  298. void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
  299. {
  300. struct ath_common *common = ath5k_hw_common(ah);
  301. /* Cache bssid mask so that we can restore it
  302. * on reset */
  303. memcpy(common->bssidmask, mask, ETH_ALEN);
  304. if (ah->ah_version == AR5K_AR5212)
  305. ath_hw_setbssidmask(common);
  306. }
  307. /************\
  308. * RX Control *
  309. \************/
  310. /**
  311. * ath5k_hw_start_rx_pcu - Start RX engine
  312. *
  313. * @ah: The &struct ath5k_hw
  314. *
  315. * Starts RX engine on PCU so that hw can process RXed frames
  316. * (ACK etc).
  317. *
  318. * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
  319. */
  320. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  321. {
  322. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  323. }
  324. /**
  325. * at5k_hw_stop_rx_pcu - Stop RX engine
  326. *
  327. * @ah: The &struct ath5k_hw
  328. *
  329. * Stops RX engine on PCU
  330. *
  331. * TODO: Detach ANI here
  332. */
  333. void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
  334. {
  335. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  336. }
  337. /*
  338. * Set multicast filter
  339. */
  340. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  341. {
  342. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  343. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  344. }
  345. /**
  346. * ath5k_hw_get_rx_filter - Get current rx filter
  347. *
  348. * @ah: The &struct ath5k_hw
  349. *
  350. * Returns the RX filter by reading rx filter and
  351. * phy error filter registers. RX filter is used
  352. * to set the allowed frame types that PCU will accept
  353. * and pass to the driver. For a list of frame types
  354. * check out reg.h.
  355. */
  356. u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
  357. {
  358. u32 data, filter = 0;
  359. filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
  360. /*Radar detection for 5212*/
  361. if (ah->ah_version == AR5K_AR5212) {
  362. data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
  363. if (data & AR5K_PHY_ERR_FIL_RADAR)
  364. filter |= AR5K_RX_FILTER_RADARERR;
  365. if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
  366. filter |= AR5K_RX_FILTER_PHYERR;
  367. }
  368. return filter;
  369. }
  370. /**
  371. * ath5k_hw_set_rx_filter - Set rx filter
  372. *
  373. * @ah: The &struct ath5k_hw
  374. * @filter: RX filter mask (see reg.h)
  375. *
  376. * Sets RX filter register and also handles PHY error filter
  377. * register on 5212 and newer chips so that we have proper PHY
  378. * error reporting.
  379. */
  380. void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
  381. {
  382. u32 data = 0;
  383. /* Set PHY error filter register on 5212*/
  384. if (ah->ah_version == AR5K_AR5212) {
  385. if (filter & AR5K_RX_FILTER_RADARERR)
  386. data |= AR5K_PHY_ERR_FIL_RADAR;
  387. if (filter & AR5K_RX_FILTER_PHYERR)
  388. data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
  389. }
  390. /*
  391. * The AR5210 uses promiscous mode to detect radar activity
  392. */
  393. if (ah->ah_version == AR5K_AR5210 &&
  394. (filter & AR5K_RX_FILTER_RADARERR)) {
  395. filter &= ~AR5K_RX_FILTER_RADARERR;
  396. filter |= AR5K_RX_FILTER_PROM;
  397. }
  398. /*Zero length DMA (phy error reporting) */
  399. if (data)
  400. AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  401. else
  402. AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  403. /*Write RX Filter register*/
  404. ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
  405. /*Write PHY error filter register on 5212*/
  406. if (ah->ah_version == AR5K_AR5212)
  407. ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
  408. }
  409. /****************\
  410. * Beacon control *
  411. \****************/
  412. #define ATH5K_MAX_TSF_READ 10
  413. /**
  414. * ath5k_hw_get_tsf64 - Get the full 64bit TSF
  415. *
  416. * @ah: The &struct ath5k_hw
  417. *
  418. * Returns the current TSF
  419. */
  420. u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
  421. {
  422. u32 tsf_lower, tsf_upper1, tsf_upper2;
  423. int i;
  424. /*
  425. * While reading TSF upper and then lower part, the clock is still
  426. * counting (or jumping in case of IBSS merge) so we might get
  427. * inconsistent values. To avoid this, we read the upper part again
  428. * and check it has not been changed. We make the hypothesis that a
  429. * maximum of 3 changes can happens in a row (we use 10 as a safe
  430. * value).
  431. *
  432. * Impact on performance is pretty small, since in most cases, only
  433. * 3 register reads are needed.
  434. */
  435. tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  436. for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
  437. tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  438. tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  439. if (tsf_upper2 == tsf_upper1)
  440. break;
  441. tsf_upper1 = tsf_upper2;
  442. }
  443. WARN_ON( i == ATH5K_MAX_TSF_READ );
  444. return (((u64)tsf_upper1 << 32) | tsf_lower);
  445. }
  446. /**
  447. * ath5k_hw_set_tsf64 - Set a new 64bit TSF
  448. *
  449. * @ah: The &struct ath5k_hw
  450. * @tsf64: The new 64bit TSF
  451. *
  452. * Sets the new TSF
  453. */
  454. void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
  455. {
  456. ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
  457. ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
  458. }
  459. /**
  460. * ath5k_hw_reset_tsf - Force a TSF reset
  461. *
  462. * @ah: The &struct ath5k_hw
  463. *
  464. * Forces a TSF reset on PCU
  465. */
  466. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  467. {
  468. u32 val;
  469. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  470. /*
  471. * Each write to the RESET_TSF bit toggles a hardware internal
  472. * signal to reset TSF, but if left high it will cause a TSF reset
  473. * on the next chip reset as well. Thus we always write the value
  474. * twice to clear the signal.
  475. */
  476. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  477. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  478. }
  479. /*
  480. * Initialize beacon timers
  481. */
  482. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  483. {
  484. u32 timer1, timer2, timer3;
  485. /*
  486. * Set the additional timers by mode
  487. */
  488. switch (ah->ah_sc->opmode) {
  489. case NL80211_IFTYPE_MONITOR:
  490. case NL80211_IFTYPE_STATION:
  491. /* In STA mode timer1 is used as next wakeup
  492. * timer and timer2 as next CFP duration start
  493. * timer. Both in 1/8TUs. */
  494. /* TODO: PCF handling */
  495. if (ah->ah_version == AR5K_AR5210) {
  496. timer1 = 0xffffffff;
  497. timer2 = 0xffffffff;
  498. } else {
  499. timer1 = 0x0000ffff;
  500. timer2 = 0x0007ffff;
  501. }
  502. /* Mark associated AP as PCF incapable for now */
  503. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
  504. break;
  505. case NL80211_IFTYPE_ADHOC:
  506. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
  507. default:
  508. /* On non-STA modes timer1 is used as next DMA
  509. * beacon alert (DBA) timer and timer2 as next
  510. * software beacon alert. Both in 1/8TUs. */
  511. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  512. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  513. break;
  514. }
  515. /* Timer3 marks the end of our ATIM window
  516. * a zero length window is not allowed because
  517. * we 'll get no beacons */
  518. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  519. /*
  520. * Set the beacon register and enable all timers.
  521. */
  522. /* When in AP or Mesh Point mode zero timer0 to start TSF */
  523. if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
  524. ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
  525. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  526. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  527. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  528. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  529. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  530. /* Force a TSF reset if requested and enable beacons */
  531. if (interval & AR5K_BEACON_RESET_TSF)
  532. ath5k_hw_reset_tsf(ah);
  533. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  534. AR5K_BEACON_ENABLE),
  535. AR5K_BEACON);
  536. /* Flush any pending BMISS interrupts on ISR by
  537. * performing a clear-on-write operation on PISR
  538. * register for the BMISS bit (writing a bit on
  539. * ISR togles a reset for that bit and leaves
  540. * the rest bits intact) */
  541. if (ah->ah_version == AR5K_AR5210)
  542. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
  543. else
  544. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
  545. /* TODO: Set enchanced sleep registers on AR5212
  546. * based on vif->bss_conf params, until then
  547. * disable power save reporting.*/
  548. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
  549. }
  550. /*********************\
  551. * Key table functions *
  552. \*********************/
  553. /*
  554. * Reset a key entry on the table
  555. */
  556. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  557. {
  558. unsigned int i, type;
  559. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  560. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  561. type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
  562. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  563. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  564. /* Reset associated MIC entry if TKIP
  565. * is enabled located at offset (entry + 64) */
  566. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  567. AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
  568. for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
  569. ath5k_hw_reg_write(ah, 0,
  570. AR5K_KEYTABLE_OFF(micentry, i));
  571. }
  572. /*
  573. * Set NULL encryption on AR5212+
  574. *
  575. * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
  576. * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
  577. *
  578. * Note2: Windows driver (ndiswrapper) sets this to
  579. * 0x00000714 instead of 0x00000007
  580. */
  581. if (ah->ah_version >= AR5K_AR5211) {
  582. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  583. AR5K_KEYTABLE_TYPE(entry));
  584. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  585. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  586. AR5K_KEYTABLE_TYPE(micentry));
  587. }
  588. }
  589. return 0;
  590. }
  591. static
  592. int ath5k_keycache_type(const struct ieee80211_key_conf *key)
  593. {
  594. switch (key->cipher) {
  595. case WLAN_CIPHER_SUITE_TKIP:
  596. return AR5K_KEYTABLE_TYPE_TKIP;
  597. case WLAN_CIPHER_SUITE_CCMP:
  598. return AR5K_KEYTABLE_TYPE_CCM;
  599. case WLAN_CIPHER_SUITE_WEP40:
  600. return AR5K_KEYTABLE_TYPE_40;
  601. case WLAN_CIPHER_SUITE_WEP104:
  602. return AR5K_KEYTABLE_TYPE_104;
  603. default:
  604. return -EINVAL;
  605. }
  606. }
  607. /*
  608. * Set a key entry on the table
  609. */
  610. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  611. const struct ieee80211_key_conf *key, const u8 *mac)
  612. {
  613. unsigned int i;
  614. int keylen;
  615. __le32 key_v[5] = {};
  616. __le32 key0 = 0, key1 = 0;
  617. __le32 *rxmic, *txmic;
  618. int keytype;
  619. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  620. bool is_tkip;
  621. const u8 *key_ptr;
  622. is_tkip = (key->cipher == WLAN_CIPHER_SUITE_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. /* Invalid entry (key table overflow) */
  693. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  694. /*
  695. * MAC may be NULL if it's a broadcast key. In this case no need to
  696. * to compute get_unaligned_le32 and get_unaligned_le16 as we
  697. * already know it.
  698. */
  699. if (!mac) {
  700. low_id = 0xffffffff;
  701. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  702. } else {
  703. low_id = get_unaligned_le32(mac);
  704. high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
  705. }
  706. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  707. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  708. return 0;
  709. }
  710. /**
  711. * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
  712. *
  713. * @ah: The &struct ath5k_hw
  714. * @coverage_class: IEEE 802.11 coverage class number
  715. *
  716. * Sets slot time, ACK timeout and CTS timeout for given coverage class.
  717. */
  718. void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
  719. {
  720. /* As defined by IEEE 802.11-2007 17.3.8.6 */
  721. int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
  722. int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
  723. int cts_timeout = ack_timeout;
  724. ath5k_hw_set_slot_time(ah, slot_time);
  725. ath5k_hw_set_ack_timeout(ah, ack_timeout);
  726. ath5k_hw_set_cts_timeout(ah, cts_timeout);
  727. ah->ah_coverage_class = coverage_class;
  728. }