pcu.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
  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_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
  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_set_tsf64 - Set a new 64bit TSF
  568. *
  569. * @ah: The &struct ath5k_hw
  570. * @tsf64: The new 64bit TSF
  571. *
  572. * Sets the new TSF
  573. */
  574. void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
  575. {
  576. ATH5K_TRACE(ah->ah_sc);
  577. ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
  578. ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
  579. }
  580. /**
  581. * ath5k_hw_reset_tsf - Force a TSF reset
  582. *
  583. * @ah: The &struct ath5k_hw
  584. *
  585. * Forces a TSF reset on PCU
  586. */
  587. void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
  588. {
  589. u32 val;
  590. ATH5K_TRACE(ah->ah_sc);
  591. val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
  592. /*
  593. * Each write to the RESET_TSF bit toggles a hardware internal
  594. * signal to reset TSF, but if left high it will cause a TSF reset
  595. * on the next chip reset as well. Thus we always write the value
  596. * twice to clear the signal.
  597. */
  598. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  599. ath5k_hw_reg_write(ah, val, AR5K_BEACON);
  600. }
  601. /*
  602. * Initialize beacon timers
  603. */
  604. void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
  605. {
  606. u32 timer1, timer2, timer3;
  607. ATH5K_TRACE(ah->ah_sc);
  608. /*
  609. * Set the additional timers by mode
  610. */
  611. switch (ah->ah_op_mode) {
  612. case NL80211_IFTYPE_MONITOR:
  613. case NL80211_IFTYPE_STATION:
  614. /* In STA mode timer1 is used as next wakeup
  615. * timer and timer2 as next CFP duration start
  616. * timer. Both in 1/8TUs. */
  617. /* TODO: PCF handling */
  618. if (ah->ah_version == AR5K_AR5210) {
  619. timer1 = 0xffffffff;
  620. timer2 = 0xffffffff;
  621. } else {
  622. timer1 = 0x0000ffff;
  623. timer2 = 0x0007ffff;
  624. }
  625. /* Mark associated AP as PCF incapable for now */
  626. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
  627. break;
  628. case NL80211_IFTYPE_ADHOC:
  629. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
  630. default:
  631. /* On non-STA modes timer1 is used as next DMA
  632. * beacon alert (DBA) timer and timer2 as next
  633. * software beacon alert. Both in 1/8TUs. */
  634. timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
  635. timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
  636. break;
  637. }
  638. /* Timer3 marks the end of our ATIM window
  639. * a zero length window is not allowed because
  640. * we 'll get no beacons */
  641. timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
  642. /*
  643. * Set the beacon register and enable all timers.
  644. */
  645. /* When in AP or Mesh Point mode zero timer0 to start TSF */
  646. if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
  647. ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
  648. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  649. ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
  650. ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
  651. ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
  652. ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
  653. /* Force a TSF reset if requested and enable beacons */
  654. if (interval & AR5K_BEACON_RESET_TSF)
  655. ath5k_hw_reset_tsf(ah);
  656. ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
  657. AR5K_BEACON_ENABLE),
  658. AR5K_BEACON);
  659. /* Flush any pending BMISS interrupts on ISR by
  660. * performing a clear-on-write operation on PISR
  661. * register for the BMISS bit (writing a bit on
  662. * ISR togles a reset for that bit and leaves
  663. * the rest bits intact) */
  664. if (ah->ah_version == AR5K_AR5210)
  665. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
  666. else
  667. ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
  668. /* TODO: Set enchanced sleep registers on AR5212
  669. * based on vif->bss_conf params, until then
  670. * disable power save reporting.*/
  671. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
  672. }
  673. #if 0
  674. /*
  675. * Set beacon timers
  676. */
  677. int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
  678. const struct ath5k_beacon_state *state)
  679. {
  680. u32 cfp_period, next_cfp, dtim, interval, next_beacon;
  681. /*
  682. * TODO: should be changed through *state
  683. * review struct ath5k_beacon_state struct
  684. *
  685. * XXX: These are used for cfp period bellow, are they
  686. * ok ? Is it O.K. for tsf here to be 0 or should we use
  687. * get_tsf ?
  688. */
  689. u32 dtim_count = 0; /* XXX */
  690. u32 cfp_count = 0; /* XXX */
  691. u32 tsf = 0; /* XXX */
  692. ATH5K_TRACE(ah->ah_sc);
  693. /* Return on an invalid beacon state */
  694. if (state->bs_interval < 1)
  695. return -EINVAL;
  696. interval = state->bs_interval;
  697. dtim = state->bs_dtim_period;
  698. /*
  699. * PCF support?
  700. */
  701. if (state->bs_cfp_period > 0) {
  702. /*
  703. * Enable PCF mode and set the CFP
  704. * (Contention Free Period) and timer registers
  705. */
  706. cfp_period = state->bs_cfp_period * state->bs_dtim_period *
  707. state->bs_interval;
  708. next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
  709. state->bs_interval;
  710. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
  711. AR5K_STA_ID1_DEFAULT_ANTENNA |
  712. AR5K_STA_ID1_PCF);
  713. ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
  714. ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
  715. AR5K_CFP_DUR);
  716. ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
  717. next_cfp)) << 3, AR5K_TIMER2);
  718. } else {
  719. /* Disable PCF mode */
  720. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  721. AR5K_STA_ID1_DEFAULT_ANTENNA |
  722. AR5K_STA_ID1_PCF);
  723. }
  724. /*
  725. * Enable the beacon timer register
  726. */
  727. ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
  728. /*
  729. * Start the beacon timers
  730. */
  731. ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
  732. ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
  733. AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
  734. AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
  735. AR5K_BEACON_PERIOD), AR5K_BEACON);
  736. /*
  737. * Write new beacon miss threshold, if it appears to be valid
  738. * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
  739. * and return if its not in range. We can test this by reading value and
  740. * setting value to a largest value and seeing which values register.
  741. */
  742. AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
  743. state->bs_bmiss_threshold);
  744. /*
  745. * Set sleep control register
  746. * XXX: Didn't find this in 5210 code but since this register
  747. * exists also in ar5k's 5210 headers i leave it as common code.
  748. */
  749. AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
  750. (state->bs_sleep_duration - 3) << 3);
  751. /*
  752. * Set enhanced sleep registers on 5212
  753. */
  754. if (ah->ah_version == AR5K_AR5212) {
  755. if (state->bs_sleep_duration > state->bs_interval &&
  756. roundup(state->bs_sleep_duration, interval) ==
  757. state->bs_sleep_duration)
  758. interval = state->bs_sleep_duration;
  759. if (state->bs_sleep_duration > dtim && (dtim == 0 ||
  760. roundup(state->bs_sleep_duration, dtim) ==
  761. state->bs_sleep_duration))
  762. dtim = state->bs_sleep_duration;
  763. if (interval > dtim)
  764. return -EINVAL;
  765. next_beacon = interval == dtim ? state->bs_next_dtim :
  766. state->bs_next_beacon;
  767. ath5k_hw_reg_write(ah,
  768. AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
  769. AR5K_SLEEP0_NEXT_DTIM) |
  770. AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
  771. AR5K_SLEEP0_ENH_SLEEP_EN |
  772. AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
  773. ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
  774. AR5K_SLEEP1_NEXT_TIM) |
  775. AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
  776. ath5k_hw_reg_write(ah,
  777. AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
  778. AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
  779. }
  780. return 0;
  781. }
  782. /*
  783. * Reset beacon timers
  784. */
  785. void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
  786. {
  787. ATH5K_TRACE(ah->ah_sc);
  788. /*
  789. * Disable beacon timer
  790. */
  791. ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
  792. /*
  793. * Disable some beacon register values
  794. */
  795. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
  796. AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
  797. ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
  798. }
  799. /*
  800. * Wait for beacon queue to finish
  801. */
  802. int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
  803. {
  804. unsigned int i;
  805. int ret;
  806. ATH5K_TRACE(ah->ah_sc);
  807. /* 5210 doesn't have QCU*/
  808. if (ah->ah_version == AR5K_AR5210) {
  809. /*
  810. * Wait for beaconn queue to finish by checking
  811. * Control Register and Beacon Status Register.
  812. */
  813. for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
  814. if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
  815. ||
  816. !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
  817. break;
  818. udelay(10);
  819. }
  820. /* Timeout... */
  821. if (i <= 0) {
  822. /*
  823. * Re-schedule the beacon queue
  824. */
  825. ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
  826. ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
  827. AR5K_BCR);
  828. return -EIO;
  829. }
  830. ret = 0;
  831. } else {
  832. /*5211/5212*/
  833. ret = ath5k_hw_register_timeout(ah,
  834. AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
  835. AR5K_QCU_STS_FRMPENDCNT, 0, false);
  836. if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
  837. return -EIO;
  838. }
  839. return ret;
  840. }
  841. #endif
  842. /*********************\
  843. * Key table functions *
  844. \*********************/
  845. /*
  846. * Reset a key entry on the table
  847. */
  848. int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
  849. {
  850. unsigned int i, type;
  851. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  852. ATH5K_TRACE(ah->ah_sc);
  853. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  854. type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
  855. for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
  856. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
  857. /* Reset associated MIC entry if TKIP
  858. * is enabled located at offset (entry + 64) */
  859. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  860. AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
  861. for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
  862. ath5k_hw_reg_write(ah, 0,
  863. AR5K_KEYTABLE_OFF(micentry, i));
  864. }
  865. /*
  866. * Set NULL encryption on AR5212+
  867. *
  868. * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
  869. * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
  870. *
  871. * Note2: Windows driver (ndiswrapper) sets this to
  872. * 0x00000714 instead of 0x00000007
  873. */
  874. if (ah->ah_version >= AR5K_AR5211) {
  875. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  876. AR5K_KEYTABLE_TYPE(entry));
  877. if (type == AR5K_KEYTABLE_TYPE_TKIP) {
  878. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  879. AR5K_KEYTABLE_TYPE(micentry));
  880. }
  881. }
  882. return 0;
  883. }
  884. /*
  885. * Check if a table entry is valid
  886. */
  887. int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
  888. {
  889. ATH5K_TRACE(ah->ah_sc);
  890. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  891. /* Check the validation flag at the end of the entry */
  892. return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
  893. AR5K_KEYTABLE_VALID;
  894. }
  895. static
  896. int ath5k_keycache_type(const struct ieee80211_key_conf *key)
  897. {
  898. switch (key->alg) {
  899. case ALG_TKIP:
  900. return AR5K_KEYTABLE_TYPE_TKIP;
  901. case ALG_CCMP:
  902. return AR5K_KEYTABLE_TYPE_CCM;
  903. case ALG_WEP:
  904. if (key->keylen == WLAN_KEY_LEN_WEP40)
  905. return AR5K_KEYTABLE_TYPE_40;
  906. else if (key->keylen == WLAN_KEY_LEN_WEP104)
  907. return AR5K_KEYTABLE_TYPE_104;
  908. return -EINVAL;
  909. default:
  910. return -EINVAL;
  911. }
  912. return -EINVAL;
  913. }
  914. /*
  915. * Set a key entry on the table
  916. */
  917. int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
  918. const struct ieee80211_key_conf *key, const u8 *mac)
  919. {
  920. unsigned int i;
  921. int keylen;
  922. __le32 key_v[5] = {};
  923. __le32 key0 = 0, key1 = 0;
  924. __le32 *rxmic, *txmic;
  925. int keytype;
  926. u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
  927. bool is_tkip;
  928. const u8 *key_ptr;
  929. ATH5K_TRACE(ah->ah_sc);
  930. is_tkip = (key->alg == ALG_TKIP);
  931. /*
  932. * key->keylen comes in from mac80211 in bytes.
  933. * TKIP is 128 bit + 128 bit mic
  934. */
  935. keylen = (is_tkip) ? (128 / 8) : key->keylen;
  936. if (entry > AR5K_KEYTABLE_SIZE ||
  937. (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
  938. return -EOPNOTSUPP;
  939. if (unlikely(keylen > 16))
  940. return -EOPNOTSUPP;
  941. keytype = ath5k_keycache_type(key);
  942. if (keytype < 0)
  943. return keytype;
  944. /*
  945. * each key block is 6 bytes wide, written as pairs of
  946. * alternating 32 and 16 bit le values.
  947. */
  948. key_ptr = key->key;
  949. for (i = 0; keylen >= 6; keylen -= 6) {
  950. memcpy(&key_v[i], key_ptr, 6);
  951. i += 2;
  952. key_ptr += 6;
  953. }
  954. if (keylen)
  955. memcpy(&key_v[i], key_ptr, keylen);
  956. /* intentionally corrupt key until mic is installed */
  957. if (is_tkip) {
  958. key0 = key_v[0] = ~key_v[0];
  959. key1 = key_v[1] = ~key_v[1];
  960. }
  961. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  962. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  963. AR5K_KEYTABLE_OFF(entry, i));
  964. ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
  965. if (is_tkip) {
  966. /* Install rx/tx MIC */
  967. rxmic = (__le32 *) &key->key[16];
  968. txmic = (__le32 *) &key->key[24];
  969. if (ah->ah_combined_mic) {
  970. key_v[0] = rxmic[0];
  971. key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
  972. key_v[2] = rxmic[1];
  973. key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
  974. key_v[4] = txmic[1];
  975. } else {
  976. key_v[0] = rxmic[0];
  977. key_v[1] = 0;
  978. key_v[2] = rxmic[1];
  979. key_v[3] = 0;
  980. key_v[4] = 0;
  981. }
  982. for (i = 0; i < ARRAY_SIZE(key_v); i++)
  983. ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
  984. AR5K_KEYTABLE_OFF(micentry, i));
  985. ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
  986. AR5K_KEYTABLE_TYPE(micentry));
  987. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
  988. ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
  989. /* restore first 2 words of key */
  990. ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
  991. AR5K_KEYTABLE_OFF(entry, 0));
  992. ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
  993. AR5K_KEYTABLE_OFF(entry, 1));
  994. }
  995. return ath5k_hw_set_key_lladdr(ah, entry, mac);
  996. }
  997. int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
  998. {
  999. u32 low_id, high_id;
  1000. ATH5K_TRACE(ah->ah_sc);
  1001. /* Invalid entry (key table overflow) */
  1002. AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
  1003. /* MAC may be NULL if it's a broadcast key. In this case no need to
  1004. * to compute AR5K_LOW_ID and AR5K_HIGH_ID as we already know it. */
  1005. if (!mac) {
  1006. low_id = 0xffffffff;
  1007. high_id = 0xffff | AR5K_KEYTABLE_VALID;
  1008. } else {
  1009. low_id = AR5K_LOW_ID(mac);
  1010. high_id = AR5K_HIGH_ID(mac) | AR5K_KEYTABLE_VALID;
  1011. }
  1012. ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
  1013. ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
  1014. return 0;
  1015. }