mac.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * MAC programming
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; see the file COPYING. If not, see
  20. * http://www.gnu.org/licenses/.
  21. *
  22. * This file incorporates work covered by the following copyright and
  23. * permission notice:
  24. * Copyright (c) 2007-2008 Atheros Communications, Inc.
  25. *
  26. * Permission to use, copy, modify, and/or distribute this software for any
  27. * purpose with or without fee is hereby granted, provided that the above
  28. * copyright notice and this permission notice appear in all copies.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  31. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  32. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  33. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  34. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  35. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  36. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  37. */
  38. #include <asm/unaligned.h>
  39. #include "carl9170.h"
  40. #include "cmd.h"
  41. int carl9170_set_dyn_sifs_ack(struct ar9170 *ar)
  42. {
  43. u32 val;
  44. if (conf_is_ht40(&ar->hw->conf))
  45. val = 0x010a;
  46. else {
  47. if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
  48. val = 0x105;
  49. else
  50. val = 0x104;
  51. }
  52. return carl9170_write_reg(ar, AR9170_MAC_REG_DYNAMIC_SIFS_ACK, val);
  53. }
  54. int carl9170_set_rts_cts_rate(struct ar9170 *ar)
  55. {
  56. u32 rts_rate, cts_rate;
  57. if (conf_is_ht(&ar->hw->conf)) {
  58. /* 12 mbit OFDM */
  59. rts_rate = 0x1da;
  60. cts_rate = 0x10a;
  61. } else {
  62. if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ) {
  63. /* 11 mbit CCK */
  64. rts_rate = 033;
  65. cts_rate = 003;
  66. } else {
  67. /* 6 mbit OFDM */
  68. rts_rate = 0x1bb;
  69. cts_rate = 0x10b;
  70. }
  71. }
  72. return carl9170_write_reg(ar, AR9170_MAC_REG_RTS_CTS_RATE,
  73. rts_rate | (cts_rate) << 16);
  74. }
  75. int carl9170_set_slot_time(struct ar9170 *ar)
  76. {
  77. struct ieee80211_vif *vif;
  78. u32 slottime = 20;
  79. rcu_read_lock();
  80. vif = carl9170_get_main_vif(ar);
  81. if (!vif) {
  82. rcu_read_unlock();
  83. return 0;
  84. }
  85. if ((ar->hw->conf.chandef.chan->band == IEEE80211_BAND_5GHZ) ||
  86. vif->bss_conf.use_short_slot)
  87. slottime = 9;
  88. rcu_read_unlock();
  89. return carl9170_write_reg(ar, AR9170_MAC_REG_SLOT_TIME,
  90. slottime << 10);
  91. }
  92. int carl9170_set_mac_rates(struct ar9170 *ar)
  93. {
  94. struct ieee80211_vif *vif;
  95. u32 basic, mandatory;
  96. rcu_read_lock();
  97. vif = carl9170_get_main_vif(ar);
  98. if (!vif) {
  99. rcu_read_unlock();
  100. return 0;
  101. }
  102. basic = (vif->bss_conf.basic_rates & 0xf);
  103. basic |= (vif->bss_conf.basic_rates & 0xff0) << 4;
  104. rcu_read_unlock();
  105. if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_5GHZ)
  106. mandatory = 0xff00; /* OFDM 6/9/12/18/24/36/48/54 */
  107. else
  108. mandatory = 0xff0f; /* OFDM (6/9../54) + CCK (1/2/5.5/11) */
  109. carl9170_regwrite_begin(ar);
  110. carl9170_regwrite(AR9170_MAC_REG_BASIC_RATE, basic);
  111. carl9170_regwrite(AR9170_MAC_REG_MANDATORY_RATE, mandatory);
  112. carl9170_regwrite_finish();
  113. return carl9170_regwrite_result();
  114. }
  115. int carl9170_set_qos(struct ar9170 *ar)
  116. {
  117. carl9170_regwrite_begin(ar);
  118. carl9170_regwrite(AR9170_MAC_REG_AC0_CW, ar->edcf[0].cw_min |
  119. (ar->edcf[0].cw_max << 16));
  120. carl9170_regwrite(AR9170_MAC_REG_AC1_CW, ar->edcf[1].cw_min |
  121. (ar->edcf[1].cw_max << 16));
  122. carl9170_regwrite(AR9170_MAC_REG_AC2_CW, ar->edcf[2].cw_min |
  123. (ar->edcf[2].cw_max << 16));
  124. carl9170_regwrite(AR9170_MAC_REG_AC3_CW, ar->edcf[3].cw_min |
  125. (ar->edcf[3].cw_max << 16));
  126. carl9170_regwrite(AR9170_MAC_REG_AC4_CW, ar->edcf[4].cw_min |
  127. (ar->edcf[4].cw_max << 16));
  128. carl9170_regwrite(AR9170_MAC_REG_AC2_AC1_AC0_AIFS,
  129. ((ar->edcf[0].aifs * 9 + 10)) |
  130. ((ar->edcf[1].aifs * 9 + 10) << 12) |
  131. ((ar->edcf[2].aifs * 9 + 10) << 24));
  132. carl9170_regwrite(AR9170_MAC_REG_AC4_AC3_AC2_AIFS,
  133. ((ar->edcf[2].aifs * 9 + 10) >> 8) |
  134. ((ar->edcf[3].aifs * 9 + 10) << 4) |
  135. ((ar->edcf[4].aifs * 9 + 10) << 16));
  136. carl9170_regwrite(AR9170_MAC_REG_AC1_AC0_TXOP,
  137. ar->edcf[0].txop | ar->edcf[1].txop << 16);
  138. carl9170_regwrite(AR9170_MAC_REG_AC3_AC2_TXOP,
  139. ar->edcf[2].txop | ar->edcf[3].txop << 16 |
  140. ar->edcf[4].txop << 24);
  141. carl9170_regwrite_finish();
  142. return carl9170_regwrite_result();
  143. }
  144. int carl9170_init_mac(struct ar9170 *ar)
  145. {
  146. carl9170_regwrite_begin(ar);
  147. /* switch MAC to OTUS interface */
  148. carl9170_regwrite(0x1c3600, 0x3);
  149. carl9170_regwrite(AR9170_MAC_REG_ACK_EXTENSION, 0x40);
  150. carl9170_regwrite(AR9170_MAC_REG_RETRY_MAX, 0x0);
  151. carl9170_regwrite(AR9170_MAC_REG_FRAMETYPE_FILTER,
  152. AR9170_MAC_FTF_MONITOR);
  153. /* enable MMIC */
  154. carl9170_regwrite(AR9170_MAC_REG_SNIFFER,
  155. AR9170_MAC_SNIFFER_DEFAULTS);
  156. carl9170_regwrite(AR9170_MAC_REG_RX_THRESHOLD, 0xc1f80);
  157. carl9170_regwrite(AR9170_MAC_REG_RX_PE_DELAY, 0x70);
  158. carl9170_regwrite(AR9170_MAC_REG_EIFS_AND_SIFS, 0xa144000);
  159. carl9170_regwrite(AR9170_MAC_REG_SLOT_TIME, 9 << 10);
  160. /* CF-END & CF-ACK rate => 24M OFDM */
  161. carl9170_regwrite(AR9170_MAC_REG_TID_CFACK_CFEND_RATE, 0x59900000);
  162. /* NAV protects ACK only (in TXOP) */
  163. carl9170_regwrite(AR9170_MAC_REG_TXOP_DURATION, 0x201);
  164. /* Set Beacon PHY CTRL's TPC to 0x7, TA1=1 */
  165. /* OTUS set AM to 0x1 */
  166. carl9170_regwrite(AR9170_MAC_REG_BCN_HT1, 0x8000170);
  167. carl9170_regwrite(AR9170_MAC_REG_BACKOFF_PROTECT, 0x105);
  168. /* Aggregation MAX number and timeout */
  169. carl9170_regwrite(AR9170_MAC_REG_AMPDU_FACTOR, 0x8000a);
  170. carl9170_regwrite(AR9170_MAC_REG_AMPDU_DENSITY, 0x140a07);
  171. carl9170_regwrite(AR9170_MAC_REG_FRAMETYPE_FILTER,
  172. AR9170_MAC_FTF_DEFAULTS);
  173. carl9170_regwrite(AR9170_MAC_REG_RX_CONTROL,
  174. AR9170_MAC_RX_CTRL_DEAGG |
  175. AR9170_MAC_RX_CTRL_SHORT_FILTER);
  176. /* rate sets */
  177. carl9170_regwrite(AR9170_MAC_REG_BASIC_RATE, 0x150f);
  178. carl9170_regwrite(AR9170_MAC_REG_MANDATORY_RATE, 0x150f);
  179. carl9170_regwrite(AR9170_MAC_REG_RTS_CTS_RATE, 0x0030033);
  180. /* MIMO response control */
  181. carl9170_regwrite(AR9170_MAC_REG_ACK_TPC, 0x4003c1e);
  182. carl9170_regwrite(AR9170_MAC_REG_AMPDU_RX_THRESH, 0xffff);
  183. /* set PHY register read timeout (??) */
  184. carl9170_regwrite(AR9170_MAC_REG_MISC_680, 0xf00008);
  185. /* Disable Rx TimeOut, workaround for BB. */
  186. carl9170_regwrite(AR9170_MAC_REG_RX_TIMEOUT, 0x0);
  187. /* Set WLAN DMA interrupt mode: generate int per packet */
  188. carl9170_regwrite(AR9170_MAC_REG_TXRX_MPI, 0x110011);
  189. carl9170_regwrite(AR9170_MAC_REG_FCS_SELECT,
  190. AR9170_MAC_FCS_FIFO_PROT);
  191. /* Disables the CF_END frame, undocumented register */
  192. carl9170_regwrite(AR9170_MAC_REG_TXOP_NOT_ENOUGH_IND,
  193. 0x141e0f48);
  194. /* reset group hash table */
  195. carl9170_regwrite(AR9170_MAC_REG_GROUP_HASH_TBL_L, 0xffffffff);
  196. carl9170_regwrite(AR9170_MAC_REG_GROUP_HASH_TBL_H, 0xffffffff);
  197. /* disable PRETBTT interrupt */
  198. carl9170_regwrite(AR9170_MAC_REG_PRETBTT, 0x0);
  199. carl9170_regwrite(AR9170_MAC_REG_BCN_PERIOD, 0x0);
  200. carl9170_regwrite_finish();
  201. return carl9170_regwrite_result();
  202. }
  203. static int carl9170_set_mac_reg(struct ar9170 *ar,
  204. const u32 reg, const u8 *mac)
  205. {
  206. static const u8 zero[ETH_ALEN] = { 0 };
  207. if (!mac)
  208. mac = zero;
  209. carl9170_regwrite_begin(ar);
  210. carl9170_regwrite(reg, get_unaligned_le32(mac));
  211. carl9170_regwrite(reg + 4, get_unaligned_le16(mac + 4));
  212. carl9170_regwrite_finish();
  213. return carl9170_regwrite_result();
  214. }
  215. int carl9170_mod_virtual_mac(struct ar9170 *ar, const unsigned int id,
  216. const u8 *mac)
  217. {
  218. if (WARN_ON(id >= ar->fw.vif_num))
  219. return -EINVAL;
  220. return carl9170_set_mac_reg(ar,
  221. AR9170_MAC_REG_ACK_TABLE + (id - 1) * 8, mac);
  222. }
  223. int carl9170_update_multicast(struct ar9170 *ar, const u64 mc_hash)
  224. {
  225. int err;
  226. carl9170_regwrite_begin(ar);
  227. carl9170_regwrite(AR9170_MAC_REG_GROUP_HASH_TBL_H, mc_hash >> 32);
  228. carl9170_regwrite(AR9170_MAC_REG_GROUP_HASH_TBL_L, mc_hash);
  229. carl9170_regwrite_finish();
  230. err = carl9170_regwrite_result();
  231. if (err)
  232. return err;
  233. ar->cur_mc_hash = mc_hash;
  234. return 0;
  235. }
  236. int carl9170_set_operating_mode(struct ar9170 *ar)
  237. {
  238. struct ieee80211_vif *vif;
  239. struct ath_common *common = &ar->common;
  240. u8 *mac_addr, *bssid;
  241. u32 cam_mode = AR9170_MAC_CAM_DEFAULTS;
  242. u32 enc_mode = AR9170_MAC_ENCRYPTION_DEFAULTS |
  243. AR9170_MAC_ENCRYPTION_MGMT_RX_SOFTWARE;
  244. u32 rx_ctrl = AR9170_MAC_RX_CTRL_DEAGG |
  245. AR9170_MAC_RX_CTRL_SHORT_FILTER;
  246. u32 sniffer = AR9170_MAC_SNIFFER_DEFAULTS;
  247. int err = 0;
  248. rcu_read_lock();
  249. vif = carl9170_get_main_vif(ar);
  250. if (vif) {
  251. mac_addr = common->macaddr;
  252. bssid = common->curbssid;
  253. switch (vif->type) {
  254. case NL80211_IFTYPE_ADHOC:
  255. cam_mode |= AR9170_MAC_CAM_IBSS;
  256. break;
  257. case NL80211_IFTYPE_MESH_POINT:
  258. case NL80211_IFTYPE_AP:
  259. cam_mode |= AR9170_MAC_CAM_AP;
  260. /* iwlagn 802.11n STA Workaround */
  261. rx_ctrl |= AR9170_MAC_RX_CTRL_PASS_TO_HOST;
  262. break;
  263. case NL80211_IFTYPE_WDS:
  264. cam_mode |= AR9170_MAC_CAM_AP_WDS;
  265. rx_ctrl |= AR9170_MAC_RX_CTRL_PASS_TO_HOST;
  266. break;
  267. case NL80211_IFTYPE_STATION:
  268. cam_mode |= AR9170_MAC_CAM_STA;
  269. rx_ctrl |= AR9170_MAC_RX_CTRL_PASS_TO_HOST;
  270. break;
  271. default:
  272. WARN(1, "Unsupported operation mode %x\n", vif->type);
  273. err = -EOPNOTSUPP;
  274. break;
  275. }
  276. } else {
  277. /*
  278. * Enable monitor mode
  279. *
  280. * rx_ctrl |= AR9170_MAC_RX_CTRL_ACK_IN_SNIFFER;
  281. * sniffer |= AR9170_MAC_SNIFFER_ENABLE_PROMISC;
  282. *
  283. * When the hardware is in SNIFFER_PROMISC mode,
  284. * it generates spurious ACKs for every incoming
  285. * frame. This confuses every peer in the
  286. * vicinity and the network throughput will suffer
  287. * badly.
  288. *
  289. * Hence, the hardware will be put into station
  290. * mode and just the rx filters are disabled.
  291. */
  292. cam_mode |= AR9170_MAC_CAM_STA;
  293. rx_ctrl |= AR9170_MAC_RX_CTRL_PASS_TO_HOST;
  294. mac_addr = common->macaddr;
  295. bssid = NULL;
  296. }
  297. rcu_read_unlock();
  298. if (err)
  299. return err;
  300. if (ar->rx_software_decryption)
  301. enc_mode |= AR9170_MAC_ENCRYPTION_RX_SOFTWARE;
  302. if (ar->sniffer_enabled) {
  303. enc_mode |= AR9170_MAC_ENCRYPTION_RX_SOFTWARE;
  304. }
  305. err = carl9170_set_mac_reg(ar, AR9170_MAC_REG_MAC_ADDR_L, mac_addr);
  306. if (err)
  307. return err;
  308. err = carl9170_set_mac_reg(ar, AR9170_MAC_REG_BSSID_L, bssid);
  309. if (err)
  310. return err;
  311. carl9170_regwrite_begin(ar);
  312. carl9170_regwrite(AR9170_MAC_REG_SNIFFER, sniffer);
  313. carl9170_regwrite(AR9170_MAC_REG_CAM_MODE, cam_mode);
  314. carl9170_regwrite(AR9170_MAC_REG_ENCRYPTION, enc_mode);
  315. carl9170_regwrite(AR9170_MAC_REG_RX_CONTROL, rx_ctrl);
  316. carl9170_regwrite_finish();
  317. return carl9170_regwrite_result();
  318. }
  319. int carl9170_set_hwretry_limit(struct ar9170 *ar, const unsigned int max_retry)
  320. {
  321. u32 tmp = min_t(u32, 0x33333, max_retry * 0x11111);
  322. return carl9170_write_reg(ar, AR9170_MAC_REG_RETRY_MAX, tmp);
  323. }
  324. int carl9170_set_beacon_timers(struct ar9170 *ar)
  325. {
  326. struct ieee80211_vif *vif;
  327. u32 v = 0;
  328. u32 pretbtt = 0;
  329. rcu_read_lock();
  330. vif = carl9170_get_main_vif(ar);
  331. if (vif) {
  332. struct carl9170_vif_info *mvif;
  333. mvif = (void *) vif->drv_priv;
  334. if (mvif->enable_beacon && !WARN_ON(!ar->beacon_enabled)) {
  335. ar->global_beacon_int = vif->bss_conf.beacon_int /
  336. ar->beacon_enabled;
  337. SET_VAL(AR9170_MAC_BCN_DTIM, v,
  338. vif->bss_conf.dtim_period);
  339. switch (vif->type) {
  340. case NL80211_IFTYPE_MESH_POINT:
  341. case NL80211_IFTYPE_ADHOC:
  342. v |= AR9170_MAC_BCN_IBSS_MODE;
  343. break;
  344. case NL80211_IFTYPE_AP:
  345. v |= AR9170_MAC_BCN_AP_MODE;
  346. break;
  347. default:
  348. WARN_ON_ONCE(1);
  349. break;
  350. }
  351. } else if (vif->type == NL80211_IFTYPE_STATION) {
  352. ar->global_beacon_int = vif->bss_conf.beacon_int;
  353. SET_VAL(AR9170_MAC_BCN_DTIM, v,
  354. ar->hw->conf.ps_dtim_period);
  355. v |= AR9170_MAC_BCN_STA_PS |
  356. AR9170_MAC_BCN_PWR_MGT;
  357. }
  358. if (ar->global_beacon_int) {
  359. if (ar->global_beacon_int < 15) {
  360. rcu_read_unlock();
  361. return -ERANGE;
  362. }
  363. ar->global_pretbtt = ar->global_beacon_int -
  364. CARL9170_PRETBTT_KUS;
  365. } else {
  366. ar->global_pretbtt = 0;
  367. }
  368. } else {
  369. ar->global_beacon_int = 0;
  370. ar->global_pretbtt = 0;
  371. }
  372. rcu_read_unlock();
  373. SET_VAL(AR9170_MAC_BCN_PERIOD, v, ar->global_beacon_int);
  374. SET_VAL(AR9170_MAC_PRETBTT, pretbtt, ar->global_pretbtt);
  375. SET_VAL(AR9170_MAC_PRETBTT2, pretbtt, ar->global_pretbtt);
  376. carl9170_regwrite_begin(ar);
  377. carl9170_regwrite(AR9170_MAC_REG_PRETBTT, pretbtt);
  378. carl9170_regwrite(AR9170_MAC_REG_BCN_PERIOD, v);
  379. carl9170_regwrite_finish();
  380. return carl9170_regwrite_result();
  381. }
  382. int carl9170_upload_key(struct ar9170 *ar, const u8 id, const u8 *mac,
  383. const u8 ktype, const u8 keyidx, const u8 *keydata,
  384. const int keylen)
  385. {
  386. struct carl9170_set_key_cmd key = { };
  387. static const u8 bcast[ETH_ALEN] = {
  388. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  389. mac = mac ? : bcast;
  390. key.user = cpu_to_le16(id);
  391. key.keyId = cpu_to_le16(keyidx);
  392. key.type = cpu_to_le16(ktype);
  393. memcpy(&key.macAddr, mac, ETH_ALEN);
  394. if (keydata)
  395. memcpy(&key.key, keydata, keylen);
  396. return carl9170_exec_cmd(ar, CARL9170_CMD_EKEY,
  397. sizeof(key), (u8 *)&key, 0, NULL);
  398. }
  399. int carl9170_disable_key(struct ar9170 *ar, const u8 id)
  400. {
  401. struct carl9170_disable_key_cmd key = { };
  402. key.user = cpu_to_le16(id);
  403. return carl9170_exec_cmd(ar, CARL9170_CMD_DKEY,
  404. sizeof(key), (u8 *)&key, 0, NULL);
  405. }
  406. int carl9170_set_mac_tpc(struct ar9170 *ar, struct ieee80211_channel *channel)
  407. {
  408. unsigned int power, chains;
  409. if (ar->eeprom.tx_mask != 1)
  410. chains = AR9170_TX_PHY_TXCHAIN_2;
  411. else
  412. chains = AR9170_TX_PHY_TXCHAIN_1;
  413. switch (channel->band) {
  414. case IEEE80211_BAND_2GHZ:
  415. power = ar->power_2G_ofdm[0] & 0x3f;
  416. break;
  417. case IEEE80211_BAND_5GHZ:
  418. power = ar->power_5G_leg[0] & 0x3f;
  419. break;
  420. default:
  421. BUG_ON(1);
  422. }
  423. power = min_t(unsigned int, power, ar->hw->conf.power_level * 2);
  424. carl9170_regwrite_begin(ar);
  425. carl9170_regwrite(AR9170_MAC_REG_ACK_TPC,
  426. 0x3c1e | power << 20 | chains << 26);
  427. carl9170_regwrite(AR9170_MAC_REG_RTS_CTS_TPC,
  428. power << 5 | chains << 11 |
  429. power << 21 | chains << 27);
  430. carl9170_regwrite(AR9170_MAC_REG_CFEND_QOSNULL_TPC,
  431. power << 5 | chains << 11 |
  432. power << 21 | chains << 27);
  433. carl9170_regwrite_finish();
  434. return carl9170_regwrite_result();
  435. }