pcu.c 30 KB

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