pcu.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. /*
  2. * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
  4. * Copyright (c) 2007-2008 Matthew W. S. Bell <mentor@madwifi.org>
  5. * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
  6. * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
  7. * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
  8. *
  9. * Permission to use, copy, modify, and distribute this software for any
  10. * purpose with or without fee is hereby granted, provided that the above
  11. * copyright notice and this permission notice appear in all copies.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  14. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  16. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  19. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20. *
  21. */
  22. /*********************************\
  23. * Protocol Control Unit Functions *
  24. \*********************************/
  25. #include <asm/unaligned.h>
  26. #include "ath5k.h"
  27. #include "reg.h"
  28. #include "debug.h"
  29. #include "base.h"
  30. /*******************\
  31. * Generic functions *
  32. \*******************/
  33. /**
  34. * ath5k_hw_set_opmode - Set PCU operating mode
  35. *
  36. * @ah: The &struct ath5k_hw
  37. *
  38. * Initialize PCU for the various operating modes (AP/STA etc)
  39. *
  40. * NOTE: ah->ah_op_mode must be set before calling this.
  41. */
  42. int ath5k_hw_set_opmode(struct ath5k_hw *ah)
  43. {
  44. struct ath_common *common = ath5k_hw_common(ah);
  45. u32 pcu_reg, beacon_reg, low_id, high_id;
  46. /* Preserve rest settings */
  47. pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
  48. pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
  49. | AR5K_STA_ID1_KEYSRCH_MODE
  50. | (ah->ah_version == AR5K_AR5210 ?
  51. (AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
  52. beacon_reg = 0;
  53. ATH5K_TRACE(ah->ah_sc);
  54. switch (ah->ah_op_mode) {
  55. case NL80211_IFTYPE_ADHOC:
  56. pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
  57. beacon_reg |= AR5K_BCR_ADHOC;
  58. if (ah->ah_version == AR5K_AR5210)
  59. pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
  60. else
  61. AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
  62. break;
  63. case NL80211_IFTYPE_AP:
  64. case NL80211_IFTYPE_MESH_POINT:
  65. pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
  66. beacon_reg |= AR5K_BCR_AP;
  67. if (ah->ah_version == AR5K_AR5210)
  68. pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
  69. else
  70. AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
  71. break;
  72. case NL80211_IFTYPE_STATION:
  73. pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
  74. | (ah->ah_version == AR5K_AR5210 ?
  75. AR5K_STA_ID1_PWR_SV : 0);
  76. case NL80211_IFTYPE_MONITOR:
  77. pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
  78. | (ah->ah_version == AR5K_AR5210 ?
  79. AR5K_STA_ID1_NO_PSPOLL : 0);
  80. break;
  81. default:
  82. return -EINVAL;
  83. }
  84. /*
  85. * Set PCU registers
  86. */
  87. low_id = get_unaligned_le32(common->macaddr);
  88. high_id = get_unaligned_le16(common->macaddr + 4);
  89. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  90. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  91. /*
  92. * Set Beacon Control Register on 5210
  93. */
  94. if (ah->ah_version == AR5K_AR5210)
  95. ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
  96. return 0;
  97. }
  98. /**
  99. * ath5k_hw_update - Update mib counters (mac layer statistics)
  100. *
  101. * @ah: The &struct ath5k_hw
  102. * @stats: The &struct ieee80211_low_level_stats we use to track
  103. * statistics on the driver
  104. *
  105. * Reads MIB counters from PCU and updates sw statistics. Must be
  106. * called after a MIB interrupt.
  107. */
  108. void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
  109. struct ieee80211_low_level_stats *stats)
  110. {
  111. ATH5K_TRACE(ah->ah_sc);
  112. /* Read-And-Clear */
  113. stats->dot11ACKFailureCount += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
  114. stats->dot11RTSFailureCount += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
  115. stats->dot11RTSSuccessCount += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
  116. stats->dot11FCSErrorCount += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
  117. /* XXX: Should we use this to track beacon count ?
  118. * -we read it anyway to clear the register */
  119. ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
  120. /* Reset profile count registers on 5212*/
  121. if (ah->ah_version == AR5K_AR5212) {
  122. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
  123. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
  124. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
  125. ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
  126. }
  127. /* TODO: Handle ANI stats */
  128. }
  129. /**
  130. * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
  131. *
  132. * @ah: The &struct ath5k_hw
  133. * @high: Flag to determine if we want to use high transmition rate
  134. * for ACKs or not
  135. *
  136. * If high flag is set, we tell hw to use a set of control rates based on
  137. * the current transmition rate (check out control_rates array inside reset.c).
  138. * If not hw just uses the lowest rate available for the current modulation
  139. * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
  140. */
  141. void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
  142. {
  143. if (ah->ah_version != AR5K_AR5212)
  144. return;
  145. else {
  146. u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
  147. if (high)
  148. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
  149. else
  150. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
  151. }
  152. }
  153. /******************\
  154. * ACK/CTS Timeouts *
  155. \******************/
  156. /**
  157. * ath5k_hw_het_ack_timeout - Get ACK timeout from PCU in usec
  158. *
  159. * @ah: The &struct ath5k_hw
  160. */
  161. unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
  162. {
  163. ATH5K_TRACE(ah->ah_sc);
  164. return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
  165. AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
  166. }
  167. /**
  168. * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
  169. *
  170. * @ah: The &struct ath5k_hw
  171. * @timeout: Timeout in usec
  172. */
  173. int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
  174. {
  175. ATH5K_TRACE(ah->ah_sc);
  176. if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
  177. ah->ah_turbo) <= timeout)
  178. return -EINVAL;
  179. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
  180. ath5k_hw_htoclock(timeout, ah->ah_turbo));
  181. return 0;
  182. }
  183. /**
  184. * ath5k_hw_get_cts_timeout - Get CTS timeout from PCU in usec
  185. *
  186. * @ah: The &struct ath5k_hw
  187. */
  188. unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
  189. {
  190. ATH5K_TRACE(ah->ah_sc);
  191. return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
  192. AR5K_TIME_OUT), AR5K_TIME_OUT_CTS), ah->ah_turbo);
  193. }
  194. /**
  195. * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
  196. *
  197. * @ah: The &struct ath5k_hw
  198. * @timeout: Timeout in usec
  199. */
  200. int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
  201. {
  202. ATH5K_TRACE(ah->ah_sc);
  203. if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
  204. ah->ah_turbo) <= timeout)
  205. return -EINVAL;
  206. AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
  207. ath5k_hw_htoclock(timeout, ah->ah_turbo));
  208. return 0;
  209. }
  210. /**
  211. * ath5k_hw_set_lladdr - Set station id
  212. *
  213. * @ah: The &struct ath5k_hw
  214. * @mac: The card's mac address
  215. *
  216. * Set station id on hw using the provided mac address
  217. */
  218. int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
  219. {
  220. struct ath_common *common = ath5k_hw_common(ah);
  221. u32 low_id, high_id;
  222. u32 pcu_reg;
  223. ATH5K_TRACE(ah->ah_sc);
  224. /* Set new station ID */
  225. memcpy(common->macaddr, mac, ETH_ALEN);
  226. pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
  227. low_id = get_unaligned_le32(mac);
  228. high_id = get_unaligned_le16(mac + 4);
  229. ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
  230. ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
  231. return 0;
  232. }
  233. /**
  234. * ath5k_hw_set_associd - Set BSSID for association
  235. *
  236. * @ah: The &struct ath5k_hw
  237. * @bssid: BSSID
  238. * @assoc_id: Assoc id
  239. *
  240. * Sets the BSSID which trigers the "SME Join" operation
  241. */
  242. void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
  243. {
  244. struct ath_common *common = ath5k_hw_common(ah);
  245. u32 low_id, high_id;
  246. u16 tim_offset = 0;
  247. /*
  248. * Set simple BSSID mask on 5212
  249. */
  250. if (ah->ah_version == AR5K_AR5212) {
  251. ath5k_hw_reg_write(ah, get_unaligned_le32(common->bssidmask),
  252. AR5K_BSS_IDM0);
  253. ath5k_hw_reg_write(ah,
  254. get_unaligned_le16(common->curbssid + 4),
  255. AR5K_BSS_IDM1);
  256. }
  257. /*
  258. * Set BSSID which triggers the "SME Join" operation
  259. */
  260. low_id = get_unaligned_le32(bssid);
  261. high_id = get_unaligned_le16(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. struct ath_common *common = ath5k_hw_common(ah);
  372. u32 low_id, high_id;
  373. ATH5K_TRACE(ah->ah_sc);
  374. /* Cache bssid mask so that we can restore it
  375. * on reset */
  376. memcpy(common->bssidmask, mask, ETH_ALEN);
  377. if (ah->ah_version == AR5K_AR5212) {
  378. low_id = get_unaligned_le32(mask);
  379. high_id = get_unaligned_le16(mask + 4);
  380. ath5k_hw_reg_write(ah, low_id, AR5K_BSS_IDM0);
  381. ath5k_hw_reg_write(ah, high_id, AR5K_BSS_IDM1);
  382. return 0;
  383. }
  384. return -EIO;
  385. }
  386. /************\
  387. * RX Control *
  388. \************/
  389. /**
  390. * ath5k_hw_start_rx_pcu - Start RX engine
  391. *
  392. * @ah: The &struct ath5k_hw
  393. *
  394. * Starts RX engine on PCU so that hw can process RXed frames
  395. * (ACK etc).
  396. *
  397. * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
  398. * TODO: Init ANI here
  399. */
  400. void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
  401. {
  402. ATH5K_TRACE(ah->ah_sc);
  403. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  404. }
  405. /**
  406. * at5k_hw_stop_rx_pcu - Stop RX engine
  407. *
  408. * @ah: The &struct ath5k_hw
  409. *
  410. * Stops RX engine on PCU
  411. *
  412. * TODO: Detach ANI here
  413. */
  414. void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
  415. {
  416. ATH5K_TRACE(ah->ah_sc);
  417. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
  418. }
  419. /*
  420. * Set multicast filter
  421. */
  422. void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
  423. {
  424. ATH5K_TRACE(ah->ah_sc);
  425. /* Set the multicat filter */
  426. ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
  427. ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
  428. }
  429. /*
  430. * Set multicast filter by index
  431. */
  432. int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
  433. {
  434. ATH5K_TRACE(ah->ah_sc);
  435. if (index >= 64)
  436. return -EINVAL;
  437. else if (index >= 32)
  438. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
  439. (1 << (index - 32)));
  440. else
  441. AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  442. return 0;
  443. }
  444. /*
  445. * Clear Multicast filter by index
  446. */
  447. int ath5k_hw_clear_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_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
  454. (1 << (index - 32)));
  455. else
  456. AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
  457. return 0;
  458. }
  459. /**
  460. * ath5k_hw_get_rx_filter - Get current rx filter
  461. *
  462. * @ah: The &struct ath5k_hw
  463. *
  464. * Returns the RX filter by reading rx filter and
  465. * phy error filter registers. RX filter is used
  466. * to set the allowed frame types that PCU will accept
  467. * and pass to the driver. For a list of frame types
  468. * check out reg.h.
  469. */
  470. u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
  471. {
  472. u32 data, filter = 0;
  473. ATH5K_TRACE(ah->ah_sc);
  474. filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
  475. /*Radar detection for 5212*/
  476. if (ah->ah_version == AR5K_AR5212) {
  477. data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
  478. if (data & AR5K_PHY_ERR_FIL_RADAR)
  479. filter |= AR5K_RX_FILTER_RADARERR;
  480. if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
  481. filter |= AR5K_RX_FILTER_PHYERR;
  482. }
  483. return filter;
  484. }
  485. /**
  486. * ath5k_hw_set_rx_filter - Set rx filter
  487. *
  488. * @ah: The &struct ath5k_hw
  489. * @filter: RX filter mask (see reg.h)
  490. *
  491. * Sets RX filter register and also handles PHY error filter
  492. * register on 5212 and newer chips so that we have proper PHY
  493. * error reporting.
  494. */
  495. void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
  496. {
  497. u32 data = 0;
  498. ATH5K_TRACE(ah->ah_sc);
  499. /* Set PHY error filter register on 5212*/
  500. if (ah->ah_version == AR5K_AR5212) {
  501. if (filter & AR5K_RX_FILTER_RADARERR)
  502. data |= AR5K_PHY_ERR_FIL_RADAR;
  503. if (filter & AR5K_RX_FILTER_PHYERR)
  504. data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
  505. }
  506. /*
  507. * The AR5210 uses promiscous mode to detect radar activity
  508. */
  509. if (ah->ah_version == AR5K_AR5210 &&
  510. (filter & AR5K_RX_FILTER_RADARERR)) {
  511. filter &= ~AR5K_RX_FILTER_RADARERR;
  512. filter |= AR5K_RX_FILTER_PROM;
  513. }
  514. /*Zero length DMA (phy error reporting) */
  515. if (data)
  516. AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  517. else
  518. AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
  519. /*Write RX Filter register*/
  520. ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
  521. /*Write PHY error filter register on 5212*/
  522. if (ah->ah_version == AR5K_AR5212)
  523. ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
  524. }
  525. /****************\
  526. * Beacon control *
  527. \****************/
  528. /**
  529. * ath5k_hw_get_tsf32 - Get a 32bit TSF
  530. *
  531. * @ah: The &struct ath5k_hw
  532. *
  533. * Returns lower 32 bits of current TSF
  534. */
  535. u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
  536. {
  537. ATH5K_TRACE(ah->ah_sc);
  538. return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
  539. }
  540. /**
  541. * ath5k_hw_get_tsf64 - Get the full 64bit TSF
  542. *
  543. * @ah: The &struct ath5k_hw
  544. *
  545. * Returns the current TSF
  546. */
  547. u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
  548. {
  549. u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
  550. ATH5K_TRACE(ah->ah_sc);
  551. return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
  552. }
  553. /**
  554. * ath5k_hw_set_tsf64 - Set a new 64bit TSF
  555. *
  556. * @ah: The &struct ath5k_hw
  557. * @tsf64: The new 64bit TSF
  558. *
  559. * Sets the new TSF
  560. */
  561. void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
  562. {
  563. ATH5K_TRACE(ah->ah_sc);
  564. ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
  565. ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
  566. }
  567. /**
  568. * ath5k_hw_reset_tsf - Force a TSF reset
  569. *
  570. * @ah: The &struct ath5k_hw
  571. *
  572. * Forces a TSF reset on PCU
  573. */
  574. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  575. {
  576. u32 val;
  577. ATH5K_TRACE(ah->ah_sc);
  578. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  579. /*
  580. * Each write to the RESET_TSF bit toggles a hardware internal
  581. * signal to reset TSF, but if left high it will cause a TSF reset
  582. * on the next chip reset as well. Thus we always write the value
  583. * twice to clear the signal.
  584. */
  585. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  586. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  587. }
  588. /*
  589. * Initialize beacon timers
  590. */
  591. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  592. {
  593. u32 timer1, timer2, timer3;
  594. ATH5K_TRACE(ah->ah_sc);
  595. /*
  596. * Set the additional timers by mode
  597. */
  598. switch (ah->ah_op_mode) {
  599. case NL80211_IFTYPE_MONITOR:
  600. case NL80211_IFTYPE_STATION:
  601. /* In STA mode timer1 is used as next wakeup
  602. * timer and timer2 as next CFP duration start
  603. * timer. Both in 1/8TUs. */
  604. /* TODO: PCF handling */
  605. if (ah->ah_version == AR5K_AR5210) {
  606. timer1 = 0xffffffff;
  607. timer2 = 0xffffffff;
  608. } else {
  609. timer1 = 0x0000ffff;
  610. timer2 = 0x0007ffff;
  611. }
  612. /* Mark associated AP as PCF incapable for now */
  613. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
  614. break;
  615. case NL80211_IFTYPE_ADHOC:
  616. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
  617. default:
  618. /* On non-STA modes timer1 is used as next DMA
  619. * beacon alert (DBA) timer and timer2 as next
  620. * software beacon alert. Both in 1/8TUs. */
  621. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  622. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  623. break;
  624. }
  625. /* Timer3 marks the end of our ATIM window
  626. * a zero length window is not allowed because
  627. * we 'll get no beacons */
  628. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  629. /*
  630. * Set the beacon register and enable all timers.
  631. */
  632. /* When in AP or Mesh Point mode zero timer0 to start TSF */
  633. if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
  634. ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
  635. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  636. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  637. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  638. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  639. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  640. /* Force a TSF reset if requested and enable beacons */
  641. if (interval & AR5K_BEACON_RESET_TSF)
  642. ath5k_hw_reset_tsf(ah);
  643. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  644. AR5K_BEACON_ENABLE),
  645. AR5K_BEACON);
  646. /* Flush any pending BMISS interrupts on ISR by
  647. * performing a clear-on-write operation on PISR
  648. * register for the BMISS bit (writing a bit on
  649. * ISR togles a reset for that bit and leaves
  650. * the rest bits intact) */
  651. if (ah->ah_version == AR5K_AR5210)
  652. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
  653. else
  654. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
  655. /* TODO: Set enchanced sleep registers on AR5212
  656. * based on vif->bss_conf params, until then
  657. * disable power save reporting.*/
  658. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
  659. }
  660. #if 0
  661. /*
  662. * Set beacon timers
  663. */
  664. int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
  665. const struct ath5k_beacon_state *state)
  666. {
  667. u32 cfp_period, next_cfp, dtim, interval, next_beacon;
  668. /*
  669. * TODO: should be changed through *state
  670. * review struct ath5k_beacon_state struct
  671. *
  672. * XXX: These are used for cfp period bellow, are they
  673. * ok ? Is it O.K. for tsf here to be 0 or should we use
  674. * get_tsf ?
  675. */
  676. u32 dtim_count = 0; /* XXX */
  677. u32 cfp_count = 0; /* XXX */
  678. u32 tsf = 0; /* XXX */
  679. ATH5K_TRACE(ah->ah_sc);
  680. /* Return on an invalid beacon state */
  681. if (state->bs_interval < 1)
  682. return -EINVAL;
  683. interval = state->bs_interval;
  684. dtim = state->bs_dtim_period;
  685. /*
  686. * PCF support?
  687. */
  688. if (state->bs_cfp_period > 0) {
  689. /*
  690. * Enable PCF mode and set the CFP
  691. * (Contention Free Period) and timer registers
  692. */
  693. cfp_period = state->bs_cfp_period * state->bs_dtim_period *
  694. state->bs_interval;
  695. next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
  696. state->bs_interval;
  697. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
  698. AR5K_STA_ID1_DEFAULT_ANTENNA |
  699. AR5K_STA_ID1_PCF);
  700. ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
  701. ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
  702. AR5K_CFP_DUR);
  703. ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
  704. next_cfp)) << 3, AR5K_TIMER2);
  705. } else {
  706. /* Disable PCF mode */
  707. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  708. AR5K_STA_ID1_DEFAULT_ANTENNA |
  709. AR5K_STA_ID1_PCF);
  710. }
  711. /*
  712. * Enable the beacon timer register
  713. */
  714. ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
  715. /*
  716. * Start the beacon timers
  717. */
  718. ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
  719. ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
  720. AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
  721. AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
  722. AR5K_BEACON_PERIOD), AR5K_BEACON);
  723. /*
  724. * Write new beacon miss threshold, if it appears to be valid
  725. * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
  726. * and return if its not in range. We can test this by reading value and
  727. * setting value to a largest value and seeing which values register.
  728. */
  729. AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
  730. state->bs_bmiss_threshold);
  731. /*
  732. * Set sleep control register
  733. * XXX: Didn't find this in 5210 code but since this register
  734. * exists also in ar5k's 5210 headers i leave it as common code.
  735. */
  736. AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
  737. (state->bs_sleep_duration - 3) << 3);
  738. /*
  739. * Set enhanced sleep registers on 5212
  740. */
  741. if (ah->ah_version == AR5K_AR5212) {
  742. if (state->bs_sleep_duration > state->bs_interval &&
  743. roundup(state->bs_sleep_duration, interval) ==
  744. state->bs_sleep_duration)
  745. interval = state->bs_sleep_duration;
  746. if (state->bs_sleep_duration > dtim && (dtim == 0 ||
  747. roundup(state->bs_sleep_duration, dtim) ==
  748. state->bs_sleep_duration))
  749. dtim = state->bs_sleep_duration;
  750. if (interval > dtim)
  751. return -EINVAL;
  752. next_beacon = interval == dtim ? state->bs_next_dtim :
  753. state->bs_next_beacon;
  754. ath5k_hw_reg_write(ah,
  755. AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
  756. AR5K_SLEEP0_NEXT_DTIM) |
  757. AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
  758. AR5K_SLEEP0_ENH_SLEEP_EN |
  759. AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
  760. ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
  761. AR5K_SLEEP1_NEXT_TIM) |
  762. AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
  763. ath5k_hw_reg_write(ah,
  764. AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
  765. AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
  766. }
  767. return 0;
  768. }
  769. /*
  770. * Reset beacon timers
  771. */
  772. void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
  773. {
  774. ATH5K_TRACE(ah->ah_sc);
  775. /*
  776. * Disable beacon timer
  777. */
  778. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  779. /*
  780. * Disable some beacon register values
  781. */
  782. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  783. AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
  784. ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
  785. }
  786. /*
  787. * Wait for beacon queue to finish
  788. */
  789. int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
  790. {
  791. unsigned int i;
  792. int ret;
  793. ATH5K_TRACE(ah->ah_sc);
  794. /* 5210 doesn't have QCU*/
  795. if (ah->ah_version == AR5K_AR5210) {
  796. /*
  797. * Wait for beaconn queue to finish by checking
  798. * Control Register and Beacon Status Register.
  799. */
  800. for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
  801. if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
  802. ||
  803. !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
  804. break;
  805. udelay(10);
  806. }
  807. /* Timeout... */
  808. if (i <= 0) {
  809. /*
  810. * Re-schedule the beacon queue
  811. */
  812. ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
  813. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
  814. AR5K_BCR);
  815. return -EIO;
  816. }
  817. ret = 0;
  818. } else {
  819. /*5211/5212*/
  820. ret = ath5k_hw_register_timeout(ah,
  821. AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
  822. AR5K_QCU_STS_FRMPENDCNT, 0, false);
  823. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
  824. return -EIO;
  825. }
  826. return ret;
  827. }
  828. #endif
  829. /*********************\
  830. * Key table functions *
  831. \*********************/
  832. /*
  833. * Reset a key entry on the table
  834. */
  835. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  836. {
  837. unsigned int i, type;
  838. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  839. ATH5K_TRACE(ah->ah_sc);
  840. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  841. type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
  842. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  843. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  844. /* Reset associated MIC entry if TKIP
  845. * is enabled located at offset (entry + 64) */
  846. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  847. AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
  848. for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
  849. ath5k_hw_reg_write(ah, 0,
  850. AR5K_KEYTABLE_OFF(micentry, i));
  851. }
  852. /*
  853. * Set NULL encryption on AR5212+
  854. *
  855. * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
  856. * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
  857. *
  858. * Note2: Windows driver (ndiswrapper) sets this to
  859. * 0x00000714 instead of 0x00000007
  860. */
  861. if (ah->ah_version >= AR5K_AR5211) {
  862. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  863. AR5K_KEYTABLE_TYPE(entry));
  864. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  865. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  866. AR5K_KEYTABLE_TYPE(micentry));
  867. }
  868. }
  869. return 0;
  870. }
  871. /*
  872. * Check if a table entry is valid
  873. */
  874. int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
  875. {
  876. ATH5K_TRACE(ah->ah_sc);
  877. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  878. /* Check the validation flag at the end of the entry */
  879. return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
  880. AR5K_KEYTABLE_VALID;
  881. }
  882. static
  883. int ath5k_keycache_type(const struct ieee80211_key_conf *key)
  884. {
  885. switch (key->alg) {
  886. case ALG_TKIP:
  887. return AR5K_KEYTABLE_TYPE_TKIP;
  888. case ALG_CCMP:
  889. return AR5K_KEYTABLE_TYPE_CCM;
  890. case ALG_WEP:
  891. if (key->keylen == WLAN_KEY_LEN_WEP40)
  892. return AR5K_KEYTABLE_TYPE_40;
  893. else if (key->keylen == WLAN_KEY_LEN_WEP104)
  894. return AR5K_KEYTABLE_TYPE_104;
  895. return -EINVAL;
  896. default:
  897. return -EINVAL;
  898. }
  899. return -EINVAL;
  900. }
  901. /*
  902. * Set a key entry on the table
  903. */
  904. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  905. const struct ieee80211_key_conf *key, const u8 *mac)
  906. {
  907. unsigned int i;
  908. int keylen;
  909. __le32 key_v[5] = {};
  910. __le32 key0 = 0, key1 = 0;
  911. __le32 *rxmic, *txmic;
  912. int keytype;
  913. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  914. bool is_tkip;
  915. const u8 *key_ptr;
  916. ATH5K_TRACE(ah->ah_sc);
  917. is_tkip = (key->alg == ALG_TKIP);
  918. /*
  919. * key->keylen comes in from mac80211 in bytes.
  920. * TKIP is 128 bit + 128 bit mic
  921. */
  922. keylen = (is_tkip) ? (128 / 8) : key->keylen;
  923. if (entry > AR5K_KEYTABLE_SIZE ||
  924. (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
  925. return -EOPNOTSUPP;
  926. if (unlikely(keylen > 16))
  927. return -EOPNOTSUPP;
  928. keytype = ath5k_keycache_type(key);
  929. if (keytype < 0)
  930. return keytype;
  931. /*
  932. * each key block is 6 bytes wide, written as pairs of
  933. * alternating 32 and 16 bit le values.
  934. */
  935. key_ptr = key->key;
  936. for (i = 0; keylen >= 6; keylen -= 6) {
  937. memcpy(&key_v[i], key_ptr, 6);
  938. i += 2;
  939. key_ptr += 6;
  940. }
  941. if (keylen)
  942. memcpy(&key_v[i], key_ptr, keylen);
  943. /* intentionally corrupt key until mic is installed */
  944. if (is_tkip) {
  945. key0 = key_v[0] = ~key_v[0];
  946. key1 = key_v[1] = ~key_v[1];
  947. }
  948. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  949. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  950. AR5K_KEYTABLE_OFF(entry, i));
  951. ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
  952. if (is_tkip) {
  953. /* Install rx/tx MIC */
  954. rxmic = (__le32 *) &key->key[16];
  955. txmic = (__le32 *) &key->key[24];
  956. if (ah->ah_combined_mic) {
  957. key_v[0] = rxmic[0];
  958. key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
  959. key_v[2] = rxmic[1];
  960. key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
  961. key_v[4] = txmic[1];
  962. } else {
  963. key_v[0] = rxmic[0];
  964. key_v[1] = 0;
  965. key_v[2] = rxmic[1];
  966. key_v[3] = 0;
  967. key_v[4] = 0;
  968. }
  969. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  970. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  971. AR5K_KEYTABLE_OFF(micentry, i));
  972. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  973. AR5K_KEYTABLE_TYPE(micentry));
  974. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
  975. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
  976. /* restore first 2 words of key */
  977. ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
  978. AR5K_KEYTABLE_OFF(entry, 0));
  979. ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
  980. AR5K_KEYTABLE_OFF(entry, 1));
  981. }
  982. return ath5k_hw_set_key_lladdr(ah, entry, mac);
  983. }
  984. int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
  985. {
  986. u32 low_id, high_id;
  987. ATH5K_TRACE(ah->ah_sc);
  988. /* Invalid entry (key table overflow) */
  989. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  990. /*
  991. * MAC may be NULL if it's a broadcast key. In this case no need to
  992. * to compute get_unaligned_le32 and get_unaligned_le16 as we
  993. * already know it.
  994. */
  995. if (!mac) {
  996. low_id = 0xffffffff;
  997. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  998. } else {
  999. low_id = get_unaligned_le32(mac);
  1000. high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
  1001. }
  1002. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  1003. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  1004. return 0;
  1005. }