pcu.c 30 KB

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