pcu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 "ath5k.h"
  26. #include "reg.h"
  27. #include "debug.h"
  28. #include "base.h"
  29. /*******************\
  30. * Generic functions *
  31. \*******************/
  32. /**
  33. * ath5k_hw_set_opmode - Set PCU operating mode
  34. *
  35. * @ah: The &struct ath5k_hw
  36. *
  37. * Initialize PCU for the various operating modes (AP/STA etc)
  38. *
  39. * NOTE: ah->ah_op_mode must be set before calling this.
  40. */
  41. int ath5k_hw_set_opmode(struct ath5k_hw *ah)
  42. {
  43. u32 pcu_reg, beacon_reg, low_id, high_id;
  44. pcu_reg = 0;
  45. beacon_reg = 0;
  46. ATH5K_TRACE(ah->ah_sc);
  47. switch (ah->ah_op_mode) {
  48. case NL80211_IFTYPE_ADHOC:
  49. pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_DESC_ANTENNA |
  50. (ah->ah_version == AR5K_AR5210 ?
  51. AR5K_STA_ID1_NO_PSPOLL : 0);
  52. beacon_reg |= AR5K_BCR_ADHOC;
  53. break;
  54. case NL80211_IFTYPE_AP:
  55. case NL80211_IFTYPE_MESH_POINT:
  56. pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_RTS_DEF_ANTENNA |
  57. (ah->ah_version == AR5K_AR5210 ?
  58. AR5K_STA_ID1_NO_PSPOLL : 0);
  59. beacon_reg |= AR5K_BCR_AP;
  60. break;
  61. case NL80211_IFTYPE_STATION:
  62. pcu_reg |= AR5K_STA_ID1_DEFAULT_ANTENNA |
  63. (ah->ah_version == AR5K_AR5210 ?
  64. AR5K_STA_ID1_PWR_SV : 0);
  65. case NL80211_IFTYPE_MONITOR:
  66. pcu_reg |= AR5K_STA_ID1_DEFAULT_ANTENNA |
  67. (ah->ah_version == AR5K_AR5210 ?
  68. AR5K_STA_ID1_NO_PSPOLL : 0);
  69. break;
  70. default:
  71. return -EINVAL;
  72. }
  73. /*
  74. * Set PCU registers
  75. */
  76. low_id = AR5K_LOW_ID(ah->ah_sta_id);
  77. high_id = AR5K_HIGH_ID(ah->ah_sta_id);
  78. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  79. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  80. /*
  81. * Set Beacon Control Register on 5210
  82. */
  83. if (ah->ah_version == AR5K_AR5210)
  84. ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
  85. return 0;
  86. }
  87. /**
  88. * ath5k_hw_update - Update mib counters (mac layer statistics)
  89. *
  90. * @ah: The &struct ath5k_hw
  91. * @stats: The &struct ieee80211_low_level_stats we use to track
  92. * statistics on the driver
  93. *
  94. * Reads MIB counters from PCU and updates sw statistics. Must be
  95. * called after a MIB interrupt.
  96. */
  97. void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
  98. struct ieee80211_low_level_stats *stats)
  99. {
  100. ATH5K_TRACE(ah->ah_sc);
  101. /* Read-And-Clear */
  102. stats->dot11ACKFailureCount += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
  103. stats->dot11RTSFailureCount += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
  104. stats->dot11RTSSuccessCount += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
  105. stats->dot11FCSErrorCount += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
  106. /* XXX: Should we use this to track beacon count ?
  107. * -we read it anyway to clear the register */
  108. ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
  109. /* Reset profile count registers on 5212*/
  110. if (ah->ah_version == AR5K_AR5212) {
  111. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
  112. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
  113. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
  114. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
  115. }
  116. }
  117. /**
  118. * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
  119. *
  120. * @ah: The &struct ath5k_hw
  121. * @high: Flag to determine if we want to use high transmition rate
  122. * for ACKs or not
  123. *
  124. * If high flag is set, we tell hw to use a set of control rates based on
  125. * the current transmition rate (check out control_rates array inside reset.c).
  126. * If not hw just uses the lowest rate available for the current modulation
  127. * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
  128. */
  129. void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
  130. {
  131. if (ah->ah_version != AR5K_AR5212)
  132. return;
  133. else {
  134. u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
  135. if (high)
  136. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
  137. else
  138. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
  139. }
  140. }
  141. /******************\
  142. * ACK/CTS Timeouts *
  143. \******************/
  144. /**
  145. * ath5k_hw_het_ack_timeout - Get ACK timeout from PCU in usec
  146. *
  147. * @ah: The &struct ath5k_hw
  148. */
  149. unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
  150. {
  151. ATH5K_TRACE(ah->ah_sc);
  152. return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
  153. AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
  154. }
  155. /**
  156. * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
  157. *
  158. * @ah: The &struct ath5k_hw
  159. * @timeout: Timeout in usec
  160. */
  161. int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
  162. {
  163. ATH5K_TRACE(ah->ah_sc);
  164. if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
  165. ah->ah_turbo) <= timeout)
  166. return -EINVAL;
  167. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
  168. ath5k_hw_htoclock(timeout, ah->ah_turbo));
  169. return 0;
  170. }
  171. /**
  172. * ath5k_hw_get_cts_timeout - Get CTS timeout from PCU in usec
  173. *
  174. * @ah: The &struct ath5k_hw
  175. */
  176. unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
  177. {
  178. ATH5K_TRACE(ah->ah_sc);
  179. return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
  180. AR5K_TIME_OUT), AR5K_TIME_OUT_CTS), ah->ah_turbo);
  181. }
  182. /**
  183. * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
  184. *
  185. * @ah: The &struct ath5k_hw
  186. * @timeout: Timeout in usec
  187. */
  188. int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
  189. {
  190. ATH5K_TRACE(ah->ah_sc);
  191. if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
  192. ah->ah_turbo) <= timeout)
  193. return -EINVAL;
  194. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
  195. ath5k_hw_htoclock(timeout, ah->ah_turbo));
  196. return 0;
  197. }
  198. /****************\
  199. * BSSID handling *
  200. \****************/
  201. /**
  202. * ath5k_hw_get_lladdr - Get station id
  203. *
  204. * @ah: The &struct ath5k_hw
  205. * @mac: The card's mac address
  206. *
  207. * Initialize ah->ah_sta_id using the mac address provided
  208. * (just a memcpy).
  209. *
  210. * TODO: Remove it once we merge ath5k_softc and ath5k_hw
  211. */
  212. void ath5k_hw_get_lladdr(struct ath5k_hw *ah, u8 *mac)
  213. {
  214. ATH5K_TRACE(ah->ah_sc);
  215. memcpy(mac, ah->ah_sta_id, ETH_ALEN);
  216. }
  217. /**
  218. * ath5k_hw_set_lladdr - Set station id
  219. *
  220. * @ah: The &struct ath5k_hw
  221. * @mac: The card's mac address
  222. *
  223. * Set station id on hw using the provided mac address
  224. */
  225. int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
  226. {
  227. u32 low_id, high_id;
  228. ATH5K_TRACE(ah->ah_sc);
  229. /* Set new station ID */
  230. memcpy(ah->ah_sta_id, mac, ETH_ALEN);
  231. low_id = AR5K_LOW_ID(mac);
  232. high_id = AR5K_HIGH_ID(mac);
  233. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  234. ath5k_hw_reg_write(ah, high_id, AR5K_STA_ID1);
  235. return 0;
  236. }
  237. /**
  238. * ath5k_hw_set_associd - Set BSSID for association
  239. *
  240. * @ah: The &struct ath5k_hw
  241. * @bssid: BSSID
  242. * @assoc_id: Assoc id
  243. *
  244. * Sets the BSSID which trigers the "SME Join" operation
  245. */
  246. void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
  247. {
  248. u32 low_id, high_id;
  249. u16 tim_offset = 0;
  250. /*
  251. * Set simple BSSID mask on 5212
  252. */
  253. if (ah->ah_version == AR5K_AR5212) {
  254. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_BSS_IDM0);
  255. ath5k_hw_reg_write(ah, 0xffffffff, AR5K_BSS_IDM1);
  256. }
  257. /*
  258. * Set BSSID which triggers the "SME Join" operation
  259. */
  260. low_id = AR5K_LOW_ID(bssid);
  261. high_id = AR5K_HIGH_ID(bssid);
  262. ath5k_hw_reg_write(ah, low_id, AR5K_BSS_ID0);
  263. ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
  264. AR5K_BSS_ID1_AID_S), AR5K_BSS_ID1);
  265. if (assoc_id == 0) {
  266. ath5k_hw_disable_pspoll(ah);
  267. return;
  268. }
  269. AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
  270. tim_offset ? tim_offset + 4 : 0);
  271. ath5k_hw_enable_pspoll(ah, NULL, 0);
  272. }
  273. /**
  274. * ath5k_hw_set_bssid_mask - filter out bssids we listen
  275. *
  276. * @ah: the &struct ath5k_hw
  277. * @mask: the bssid_mask, a u8 array of size ETH_ALEN
  278. *
  279. * BSSID masking is a method used by AR5212 and newer hardware to inform PCU
  280. * which bits of the interface's MAC address should be looked at when trying
  281. * to decide which packets to ACK. In station mode and AP mode with a single
  282. * BSS every bit matters since we lock to only one BSS. In AP mode with
  283. * multiple BSSes (virtual interfaces) not every bit matters because hw must
  284. * accept frames for all BSSes and so we tweak some bits of our mac address
  285. * in order to have multiple BSSes.
  286. *
  287. * NOTE: This is a simple filter and does *not* filter out all
  288. * relevant frames. Some frames that are not for us might get ACKed from us
  289. * by PCU because they just match the mask.
  290. *
  291. * When handling multiple BSSes you can get the BSSID mask by computing the
  292. * set of ~ ( MAC XOR BSSID ) for all bssids we handle.
  293. *
  294. * When you do this you are essentially computing the common bits of all your
  295. * BSSes. Later it is assumed the harware will "and" (&) the BSSID mask with
  296. * the MAC address to obtain the relevant bits and compare the result with
  297. * (frame's BSSID & mask) to see if they match.
  298. */
  299. /*
  300. * Simple example: on your card you have have two BSSes you have created with
  301. * BSSID-01 and BSSID-02. Lets assume BSSID-01 will not use the MAC address.
  302. * There is another BSSID-03 but you are not part of it. For simplicity's sake,
  303. * assuming only 4 bits for a mac address and for BSSIDs you can then have:
  304. *
  305. * \
  306. * MAC: 0001 |
  307. * BSSID-01: 0100 | --> Belongs to us
  308. * BSSID-02: 1001 |
  309. * /
  310. * -------------------
  311. * BSSID-03: 0110 | --> External
  312. * -------------------
  313. *
  314. * Our bssid_mask would then be:
  315. *
  316. * On loop iteration for BSSID-01:
  317. * ~(0001 ^ 0100) -> ~(0101)
  318. * -> 1010
  319. * bssid_mask = 1010
  320. *
  321. * On loop iteration for BSSID-02:
  322. * bssid_mask &= ~(0001 ^ 1001)
  323. * bssid_mask = (1010) & ~(0001 ^ 1001)
  324. * bssid_mask = (1010) & ~(1001)
  325. * bssid_mask = (1010) & (0110)
  326. * bssid_mask = 0010
  327. *
  328. * A bssid_mask of 0010 means "only pay attention to the second least
  329. * significant bit". This is because its the only bit common
  330. * amongst the MAC and all BSSIDs we support. To findout what the real
  331. * common bit is we can simply "&" the bssid_mask now with any BSSID we have
  332. * or our MAC address (we assume the hardware uses the MAC address).
  333. *
  334. * Now, suppose there's an incoming frame for BSSID-03:
  335. *
  336. * IFRAME-01: 0110
  337. *
  338. * An easy eye-inspeciton of this already should tell you that this frame
  339. * will not pass our check. This is beacuse the bssid_mask tells the
  340. * hardware to only look at the second least significant bit and the
  341. * common bit amongst the MAC and BSSIDs is 0, this frame has the 2nd LSB
  342. * as 1, which does not match 0.
  343. *
  344. * So with IFRAME-01 we *assume* the hardware will do:
  345. *
  346. * allow = (IFRAME-01 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
  347. * --> allow = (0110 & 0010) == (0010 & 0001) ? 1 : 0;
  348. * --> allow = (0010) == 0000 ? 1 : 0;
  349. * --> allow = 0
  350. *
  351. * Lets now test a frame that should work:
  352. *
  353. * IFRAME-02: 0001 (we should allow)
  354. *
  355. * allow = (0001 & 1010) == 1010
  356. *
  357. * allow = (IFRAME-02 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
  358. * --> allow = (0001 & 0010) == (0010 & 0001) ? 1 :0;
  359. * --> allow = (0010) == (0010)
  360. * --> allow = 1
  361. *
  362. * Other examples:
  363. *
  364. * IFRAME-03: 0100 --> allowed
  365. * IFRAME-04: 1001 --> allowed
  366. * IFRAME-05: 1101 --> allowed but its not for us!!!
  367. *
  368. */
  369. int ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
  370. {
  371. u32 low_id, high_id;
  372. ATH5K_TRACE(ah->ah_sc);
  373. if (ah->ah_version == AR5K_AR5212) {
  374. low_id = AR5K_LOW_ID(mask);
  375. high_id = AR5K_HIGH_ID(mask);
  376. ath5k_hw_reg_write(ah, low_id, AR5K_BSS_IDM0);
  377. ath5k_hw_reg_write(ah, high_id, AR5K_BSS_IDM1);
  378. return 0;
  379. }
  380. return -EIO;
  381. }
  382. /************\
  383. * RX Control *
  384. \************/
  385. /**
  386. * ath5k_hw_start_rx_pcu - Start RX engine
  387. *
  388. * @ah: The &struct ath5k_hw
  389. *
  390. * Starts RX engine on PCU so that hw can process RXed frames
  391. * (ACK etc).
  392. *
  393. * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
  394. * TODO: Init ANI here
  395. */
  396. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  397. {
  398. ATH5K_TRACE(ah->ah_sc);
  399. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  400. }
  401. /**
  402. * at5k_hw_stop_rx_pcu - Stop RX engine
  403. *
  404. * @ah: The &struct ath5k_hw
  405. *
  406. * Stops RX engine on PCU
  407. *
  408. * TODO: Detach ANI here
  409. */
  410. void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
  411. {
  412. ATH5K_TRACE(ah->ah_sc);
  413. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  414. }
  415. /*
  416. * Set multicast filter
  417. */
  418. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  419. {
  420. ATH5K_TRACE(ah->ah_sc);
  421. /* Set the multicat filter */
  422. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  423. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  424. }
  425. /*
  426. * Set multicast filter by index
  427. */
  428. int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
  429. {
  430. ATH5K_TRACE(ah->ah_sc);
  431. if (index >= 64)
  432. return -EINVAL;
  433. else if (index >= 32)
  434. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
  435. (1 << (index - 32)));
  436. else
  437. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  438. return 0;
  439. }
  440. /*
  441. * Clear Multicast filter by index
  442. */
  443. int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
  444. {
  445. ATH5K_TRACE(ah->ah_sc);
  446. if (index >= 64)
  447. return -EINVAL;
  448. else if (index >= 32)
  449. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
  450. (1 << (index - 32)));
  451. else
  452. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  453. return 0;
  454. }
  455. /**
  456. * ath5k_hw_get_rx_filter - Get current rx filter
  457. *
  458. * @ah: The &struct ath5k_hw
  459. *
  460. * Returns the RX filter by reading rx filter and
  461. * phy error filter registers. RX filter is used
  462. * to set the allowed frame types that PCU will accept
  463. * and pass to the driver. For a list of frame types
  464. * check out reg.h.
  465. */
  466. u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
  467. {
  468. u32 data, filter = 0;
  469. ATH5K_TRACE(ah->ah_sc);
  470. filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
  471. /*Radar detection for 5212*/
  472. if (ah->ah_version == AR5K_AR5212) {
  473. data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
  474. if (data & AR5K_PHY_ERR_FIL_RADAR)
  475. filter |= AR5K_RX_FILTER_RADARERR;
  476. if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
  477. filter |= AR5K_RX_FILTER_PHYERR;
  478. }
  479. return filter;
  480. }
  481. /**
  482. * ath5k_hw_set_rx_filter - Set rx filter
  483. *
  484. * @ah: The &struct ath5k_hw
  485. * @filter: RX filter mask (see reg.h)
  486. *
  487. * Sets RX filter register and also handles PHY error filter
  488. * register on 5212 and newer chips so that we have proper PHY
  489. * error reporting.
  490. */
  491. void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
  492. {
  493. u32 data = 0;
  494. ATH5K_TRACE(ah->ah_sc);
  495. /* Set PHY error filter register on 5212*/
  496. if (ah->ah_version == AR5K_AR5212) {
  497. if (filter & AR5K_RX_FILTER_RADARERR)
  498. data |= AR5K_PHY_ERR_FIL_RADAR;
  499. if (filter & AR5K_RX_FILTER_PHYERR)
  500. data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
  501. }
  502. /*
  503. * The AR5210 uses promiscous mode to detect radar activity
  504. */
  505. if (ah->ah_version == AR5K_AR5210 &&
  506. (filter & AR5K_RX_FILTER_RADARERR)) {
  507. filter &= ~AR5K_RX_FILTER_RADARERR;
  508. filter |= AR5K_RX_FILTER_PROM;
  509. }
  510. /*Zero length DMA*/
  511. if (data)
  512. AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  513. else
  514. AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  515. /*Write RX Filter register*/
  516. ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
  517. /*Write PHY error filter register on 5212*/
  518. if (ah->ah_version == AR5K_AR5212)
  519. ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
  520. }
  521. /****************\
  522. * Beacon control *
  523. \****************/
  524. /**
  525. * ath5k_hw_get_tsf32 - Get a 32bit TSF
  526. *
  527. * @ah: The &struct ath5k_hw
  528. *
  529. * Returns lower 32 bits of current TSF
  530. */
  531. u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
  532. {
  533. ATH5K_TRACE(ah->ah_sc);
  534. return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  535. }
  536. /**
  537. * ath5k_hw_get_tsf64 - Get the full 64bit TSF
  538. *
  539. * @ah: The &struct ath5k_hw
  540. *
  541. * Returns the current TSF
  542. */
  543. u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
  544. {
  545. u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  546. ATH5K_TRACE(ah->ah_sc);
  547. return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
  548. }
  549. /**
  550. * ath5k_hw_reset_tsf - Force a TSF reset
  551. *
  552. * @ah: The &struct ath5k_hw
  553. *
  554. * Forces a TSF reset on PCU
  555. */
  556. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  557. {
  558. u32 val;
  559. ATH5K_TRACE(ah->ah_sc);
  560. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  561. /*
  562. * Each write to the RESET_TSF bit toggles a hardware internal
  563. * signal to reset TSF, but if left high it will cause a TSF reset
  564. * on the next chip reset as well. Thus we always write the value
  565. * twice to clear the signal.
  566. */
  567. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  568. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  569. }
  570. /*
  571. * Initialize beacon timers
  572. */
  573. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  574. {
  575. u32 timer1, timer2, timer3;
  576. ATH5K_TRACE(ah->ah_sc);
  577. /*
  578. * Set the additional timers by mode
  579. */
  580. switch (ah->ah_op_mode) {
  581. case NL80211_IFTYPE_STATION:
  582. if (ah->ah_version == AR5K_AR5210) {
  583. timer1 = 0xffffffff;
  584. timer2 = 0xffffffff;
  585. } else {
  586. timer1 = 0x0000ffff;
  587. timer2 = 0x0007ffff;
  588. }
  589. break;
  590. default:
  591. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  592. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  593. }
  594. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  595. /*
  596. * Set the beacon register and enable all timers.
  597. * (next beacon, DMA beacon, software beacon, ATIM window time)
  598. */
  599. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  600. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  601. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  602. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  603. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  604. AR5K_BEACON_RESET_TSF | AR5K_BEACON_ENABLE),
  605. AR5K_BEACON);
  606. }
  607. #if 0
  608. /*
  609. * Set beacon timers
  610. */
  611. int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
  612. const struct ath5k_beacon_state *state)
  613. {
  614. u32 cfp_period, next_cfp, dtim, interval, next_beacon;
  615. /*
  616. * TODO: should be changed through *state
  617. * review struct ath5k_beacon_state struct
  618. *
  619. * XXX: These are used for cfp period bellow, are they
  620. * ok ? Is it O.K. for tsf here to be 0 or should we use
  621. * get_tsf ?
  622. */
  623. u32 dtim_count = 0; /* XXX */
  624. u32 cfp_count = 0; /* XXX */
  625. u32 tsf = 0; /* XXX */
  626. ATH5K_TRACE(ah->ah_sc);
  627. /* Return on an invalid beacon state */
  628. if (state->bs_interval < 1)
  629. return -EINVAL;
  630. interval = state->bs_interval;
  631. dtim = state->bs_dtim_period;
  632. /*
  633. * PCF support?
  634. */
  635. if (state->bs_cfp_period > 0) {
  636. /*
  637. * Enable PCF mode and set the CFP
  638. * (Contention Free Period) and timer registers
  639. */
  640. cfp_period = state->bs_cfp_period * state->bs_dtim_period *
  641. state->bs_interval;
  642. next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
  643. state->bs_interval;
  644. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
  645. AR5K_STA_ID1_DEFAULT_ANTENNA |
  646. AR5K_STA_ID1_PCF);
  647. ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
  648. ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
  649. AR5K_CFP_DUR);
  650. ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
  651. next_cfp)) << 3, AR5K_TIMER2);
  652. } else {
  653. /* Disable PCF mode */
  654. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  655. AR5K_STA_ID1_DEFAULT_ANTENNA |
  656. AR5K_STA_ID1_PCF);
  657. }
  658. /*
  659. * Enable the beacon timer register
  660. */
  661. ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
  662. /*
  663. * Start the beacon timers
  664. */
  665. ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
  666. ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
  667. AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
  668. AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
  669. AR5K_BEACON_PERIOD), AR5K_BEACON);
  670. /*
  671. * Write new beacon miss threshold, if it appears to be valid
  672. * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
  673. * and return if its not in range. We can test this by reading value and
  674. * setting value to a largest value and seeing which values register.
  675. */
  676. AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
  677. state->bs_bmiss_threshold);
  678. /*
  679. * Set sleep control register
  680. * XXX: Didn't find this in 5210 code but since this register
  681. * exists also in ar5k's 5210 headers i leave it as common code.
  682. */
  683. AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
  684. (state->bs_sleep_duration - 3) << 3);
  685. /*
  686. * Set enhanced sleep registers on 5212
  687. */
  688. if (ah->ah_version == AR5K_AR5212) {
  689. if (state->bs_sleep_duration > state->bs_interval &&
  690. roundup(state->bs_sleep_duration, interval) ==
  691. state->bs_sleep_duration)
  692. interval = state->bs_sleep_duration;
  693. if (state->bs_sleep_duration > dtim && (dtim == 0 ||
  694. roundup(state->bs_sleep_duration, dtim) ==
  695. state->bs_sleep_duration))
  696. dtim = state->bs_sleep_duration;
  697. if (interval > dtim)
  698. return -EINVAL;
  699. next_beacon = interval == dtim ? state->bs_next_dtim :
  700. state->bs_next_beacon;
  701. ath5k_hw_reg_write(ah,
  702. AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
  703. AR5K_SLEEP0_NEXT_DTIM) |
  704. AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
  705. AR5K_SLEEP0_ENH_SLEEP_EN |
  706. AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
  707. ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
  708. AR5K_SLEEP1_NEXT_TIM) |
  709. AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
  710. ath5k_hw_reg_write(ah,
  711. AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
  712. AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
  713. }
  714. return 0;
  715. }
  716. /*
  717. * Reset beacon timers
  718. */
  719. void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
  720. {
  721. ATH5K_TRACE(ah->ah_sc);
  722. /*
  723. * Disable beacon timer
  724. */
  725. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  726. /*
  727. * Disable some beacon register values
  728. */
  729. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  730. AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
  731. ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
  732. }
  733. /*
  734. * Wait for beacon queue to finish
  735. */
  736. int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
  737. {
  738. unsigned int i;
  739. int ret;
  740. ATH5K_TRACE(ah->ah_sc);
  741. /* 5210 doesn't have QCU*/
  742. if (ah->ah_version == AR5K_AR5210) {
  743. /*
  744. * Wait for beaconn queue to finish by checking
  745. * Control Register and Beacon Status Register.
  746. */
  747. for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
  748. if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
  749. ||
  750. !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
  751. break;
  752. udelay(10);
  753. }
  754. /* Timeout... */
  755. if (i <= 0) {
  756. /*
  757. * Re-schedule the beacon queue
  758. */
  759. ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
  760. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
  761. AR5K_BCR);
  762. return -EIO;
  763. }
  764. ret = 0;
  765. } else {
  766. /*5211/5212*/
  767. ret = ath5k_hw_register_timeout(ah,
  768. AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
  769. AR5K_QCU_STS_FRMPENDCNT, 0, false);
  770. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
  771. return -EIO;
  772. }
  773. return ret;
  774. }
  775. #endif
  776. /*********************\
  777. * Key table functions *
  778. \*********************/
  779. /*
  780. * Reset a key entry on the table
  781. */
  782. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  783. {
  784. unsigned int i;
  785. ATH5K_TRACE(ah->ah_sc);
  786. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  787. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  788. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  789. /*
  790. * Set NULL encryption on AR5212+
  791. *
  792. * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
  793. * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
  794. *
  795. * Note2: Windows driver (ndiswrapper) sets this to
  796. * 0x00000714 instead of 0x00000007
  797. */
  798. if (ah->ah_version > AR5K_AR5211)
  799. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  800. AR5K_KEYTABLE_TYPE(entry));
  801. return 0;
  802. }
  803. /*
  804. * Check if a table entry is valid
  805. */
  806. int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
  807. {
  808. ATH5K_TRACE(ah->ah_sc);
  809. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  810. /* Check the validation flag at the end of the entry */
  811. return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
  812. AR5K_KEYTABLE_VALID;
  813. }
  814. /*
  815. * Set a key entry on the table
  816. */
  817. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  818. const struct ieee80211_key_conf *key, const u8 *mac)
  819. {
  820. unsigned int i;
  821. __le32 key_v[5] = {};
  822. u32 keytype;
  823. ATH5K_TRACE(ah->ah_sc);
  824. /* key->keylen comes in from mac80211 in bytes */
  825. if (key->keylen > AR5K_KEYTABLE_SIZE / 8)
  826. return -EOPNOTSUPP;
  827. switch (key->keylen) {
  828. /* WEP 40-bit = 40-bit entered key + 24 bit IV = 64-bit */
  829. case 40 / 8:
  830. memcpy(&key_v[0], key->key, 5);
  831. keytype = AR5K_KEYTABLE_TYPE_40;
  832. break;
  833. /* WEP 104-bit = 104-bit entered key + 24-bit IV = 128-bit */
  834. case 104 / 8:
  835. memcpy(&key_v[0], &key->key[0], 6);
  836. memcpy(&key_v[2], &key->key[6], 6);
  837. memcpy(&key_v[4], &key->key[12], 1);
  838. keytype = AR5K_KEYTABLE_TYPE_104;
  839. break;
  840. /* WEP 128-bit = 128-bit entered key + 24 bit IV = 152-bit */
  841. case 128 / 8:
  842. memcpy(&key_v[0], &key->key[0], 6);
  843. memcpy(&key_v[2], &key->key[6], 6);
  844. memcpy(&key_v[4], &key->key[12], 4);
  845. keytype = AR5K_KEYTABLE_TYPE_128;
  846. break;
  847. default:
  848. return -EINVAL; /* shouldn't happen */
  849. }
  850. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  851. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  852. AR5K_KEYTABLE_OFF(entry, i));
  853. ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
  854. return ath5k_hw_set_key_lladdr(ah, entry, mac);
  855. }
  856. int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
  857. {
  858. u32 low_id, high_id;
  859. ATH5K_TRACE(ah->ah_sc);
  860. /* Invalid entry (key table overflow) */
  861. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  862. /* MAC may be NULL if it's a broadcast key. In this case no need to
  863. * to compute AR5K_LOW_ID and AR5K_HIGH_ID as we already know it. */
  864. if (unlikely(mac == NULL)) {
  865. low_id = 0xffffffff;
  866. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  867. } else {
  868. low_id = AR5K_LOW_ID(mac);
  869. high_id = AR5K_HIGH_ID(mac) | AR5K_KEYTABLE_VALID;
  870. }
  871. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  872. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  873. return 0;
  874. }