pcu.c 28 KB

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