htc_drv_main.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * Copyright (c) 2010 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "htc.h"
  17. #ifdef CONFIG_ATH9K_HTC_DEBUGFS
  18. static struct dentry *ath9k_debugfs_root;
  19. #endif
  20. /*************/
  21. /* Utilities */
  22. /*************/
  23. static void ath_update_txpow(struct ath9k_htc_priv *priv)
  24. {
  25. struct ath_hw *ah = priv->ah;
  26. u32 txpow;
  27. if (priv->curtxpow != priv->txpowlimit) {
  28. ath9k_hw_set_txpowerlimit(ah, priv->txpowlimit);
  29. /* read back in case value is clamped */
  30. ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
  31. priv->curtxpow = txpow;
  32. }
  33. }
  34. /* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
  35. static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
  36. struct ath9k_channel *ichan)
  37. {
  38. enum htc_phymode mode;
  39. mode = HTC_MODE_AUTO;
  40. switch (ichan->chanmode) {
  41. case CHANNEL_G:
  42. case CHANNEL_G_HT20:
  43. case CHANNEL_G_HT40PLUS:
  44. case CHANNEL_G_HT40MINUS:
  45. mode = HTC_MODE_11NG;
  46. break;
  47. case CHANNEL_A:
  48. case CHANNEL_A_HT20:
  49. case CHANNEL_A_HT40PLUS:
  50. case CHANNEL_A_HT40MINUS:
  51. mode = HTC_MODE_11NA;
  52. break;
  53. default:
  54. break;
  55. }
  56. return mode;
  57. }
  58. static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
  59. struct ieee80211_hw *hw,
  60. struct ath9k_channel *hchan)
  61. {
  62. struct ath_hw *ah = priv->ah;
  63. struct ath_common *common = ath9k_hw_common(ah);
  64. struct ieee80211_conf *conf = &common->hw->conf;
  65. bool fastcc = true;
  66. struct ieee80211_channel *channel = hw->conf.channel;
  67. enum htc_phymode mode;
  68. u16 htc_mode;
  69. u8 cmd_rsp;
  70. int ret;
  71. if (priv->op_flags & OP_INVALID)
  72. return -EIO;
  73. if (priv->op_flags & OP_FULL_RESET)
  74. fastcc = false;
  75. /* Fiddle around with fastcc later on, for now just use full reset */
  76. fastcc = false;
  77. htc_stop(priv->htc);
  78. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  79. WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
  80. WMI_CMD(WMI_STOP_RECV_CMDID);
  81. ath_print(common, ATH_DBG_CONFIG,
  82. "(%u MHz) -> (%u MHz), HT: %d, HT40: %d\n",
  83. priv->ah->curchan->channel,
  84. channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf));
  85. ret = ath9k_hw_reset(ah, hchan, fastcc);
  86. if (ret) {
  87. ath_print(common, ATH_DBG_FATAL,
  88. "Unable to reset channel (%u Mhz) "
  89. "reset status %d\n", channel->center_freq, ret);
  90. goto err;
  91. }
  92. ath_update_txpow(priv);
  93. WMI_CMD(WMI_START_RECV_CMDID);
  94. if (ret)
  95. goto err;
  96. ath9k_host_rx_init(priv);
  97. mode = ath9k_htc_get_curmode(priv, hchan);
  98. htc_mode = cpu_to_be16(mode);
  99. WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
  100. if (ret)
  101. goto err;
  102. WMI_CMD(WMI_ENABLE_INTR_CMDID);
  103. if (ret)
  104. goto err;
  105. htc_start(priv->htc);
  106. priv->op_flags &= ~OP_FULL_RESET;
  107. err:
  108. return ret;
  109. }
  110. static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
  111. {
  112. struct ath_common *common = ath9k_hw_common(priv->ah);
  113. struct ath9k_htc_target_vif hvif;
  114. int ret = 0;
  115. u8 cmd_rsp;
  116. if (priv->nvifs > 0)
  117. return -ENOBUFS;
  118. memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
  119. memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
  120. hvif.opmode = cpu_to_be32(HTC_M_MONITOR);
  121. priv->ah->opmode = NL80211_IFTYPE_MONITOR;
  122. hvif.index = priv->nvifs;
  123. WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
  124. if (ret)
  125. return ret;
  126. priv->nvifs++;
  127. return 0;
  128. }
  129. static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
  130. {
  131. struct ath_common *common = ath9k_hw_common(priv->ah);
  132. struct ath9k_htc_target_vif hvif;
  133. int ret = 0;
  134. u8 cmd_rsp;
  135. memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
  136. memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
  137. hvif.index = 0; /* Should do for now */
  138. WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
  139. priv->nvifs--;
  140. return ret;
  141. }
  142. static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
  143. struct ieee80211_vif *vif,
  144. struct ieee80211_sta *sta)
  145. {
  146. struct ath_common *common = ath9k_hw_common(priv->ah);
  147. struct ath9k_htc_target_sta tsta;
  148. struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
  149. struct ath9k_htc_sta *ista;
  150. int ret;
  151. u8 cmd_rsp;
  152. if (priv->nstations >= ATH9K_HTC_MAX_STA)
  153. return -ENOBUFS;
  154. memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
  155. if (sta) {
  156. ista = (struct ath9k_htc_sta *) sta->drv_priv;
  157. memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
  158. memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
  159. tsta.associd = common->curaid;
  160. tsta.is_vif_sta = 0;
  161. tsta.valid = true;
  162. ista->index = priv->nstations;
  163. } else {
  164. memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
  165. tsta.is_vif_sta = 1;
  166. }
  167. tsta.sta_index = priv->nstations;
  168. tsta.vif_index = avp->index;
  169. tsta.maxampdu = 0xffff;
  170. if (sta && sta->ht_cap.ht_supported)
  171. tsta.flags = cpu_to_be16(ATH_HTC_STA_HT);
  172. WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
  173. if (ret) {
  174. if (sta)
  175. ath_print(common, ATH_DBG_FATAL,
  176. "Unable to add station entry for: %pM\n", sta->addr);
  177. return ret;
  178. }
  179. if (sta)
  180. ath_print(common, ATH_DBG_CONFIG,
  181. "Added a station entry for: %pM (idx: %d)\n",
  182. sta->addr, tsta.sta_index);
  183. priv->nstations++;
  184. return 0;
  185. }
  186. static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
  187. struct ieee80211_vif *vif,
  188. struct ieee80211_sta *sta)
  189. {
  190. struct ath_common *common = ath9k_hw_common(priv->ah);
  191. struct ath9k_htc_sta *ista;
  192. int ret;
  193. u8 cmd_rsp, sta_idx;
  194. if (sta) {
  195. ista = (struct ath9k_htc_sta *) sta->drv_priv;
  196. sta_idx = ista->index;
  197. } else {
  198. sta_idx = 0;
  199. }
  200. WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
  201. if (ret) {
  202. if (sta)
  203. ath_print(common, ATH_DBG_FATAL,
  204. "Unable to remove station entry for: %pM\n",
  205. sta->addr);
  206. return ret;
  207. }
  208. if (sta)
  209. ath_print(common, ATH_DBG_CONFIG,
  210. "Removed a station entry for: %pM (idx: %d)\n",
  211. sta->addr, sta_idx);
  212. priv->nstations--;
  213. return 0;
  214. }
  215. static int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv)
  216. {
  217. struct ath9k_htc_cap_target tcap;
  218. int ret;
  219. u8 cmd_rsp;
  220. memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
  221. /* FIXME: Values are hardcoded */
  222. tcap.flags = 0x240c40;
  223. tcap.flags_ext = 0x80601000;
  224. tcap.ampdu_limit = 0xffff0000;
  225. tcap.ampdu_subframes = 20;
  226. tcap.tx_chainmask_legacy = 1;
  227. tcap.protmode = 1;
  228. tcap.tx_chainmask = 1;
  229. WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
  230. return ret;
  231. }
  232. static int ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
  233. struct ieee80211_vif *vif,
  234. struct ieee80211_sta *sta)
  235. {
  236. struct ath_common *common = ath9k_hw_common(priv->ah);
  237. struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
  238. struct ieee80211_supported_band *sband;
  239. struct ath9k_htc_target_rate trate;
  240. u32 caps = 0;
  241. u8 cmd_rsp;
  242. int i, j, ret;
  243. memset(&trate, 0, sizeof(trate));
  244. /* Only 2GHz is supported */
  245. sband = priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  246. for (i = 0, j = 0; i < sband->n_bitrates; i++) {
  247. if (sta->supp_rates[sband->band] & BIT(i)) {
  248. priv->tgt_rate.rates.legacy_rates.rs_rates[j]
  249. = (sband->bitrates[i].bitrate * 2) / 10;
  250. j++;
  251. }
  252. }
  253. priv->tgt_rate.rates.legacy_rates.rs_nrates = j;
  254. if (sta->ht_cap.ht_supported) {
  255. for (i = 0, j = 0; i < 77; i++) {
  256. if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
  257. priv->tgt_rate.rates.ht_rates.rs_rates[j++] = i;
  258. if (j == ATH_HTC_RATE_MAX)
  259. break;
  260. }
  261. priv->tgt_rate.rates.ht_rates.rs_nrates = j;
  262. caps = WLAN_RC_HT_FLAG;
  263. if (sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
  264. caps |= WLAN_RC_40_FLAG;
  265. if (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40)
  266. caps |= WLAN_RC_SGI_FLAG;
  267. }
  268. priv->tgt_rate.sta_index = ista->index;
  269. priv->tgt_rate.isnew = 1;
  270. trate = priv->tgt_rate;
  271. priv->tgt_rate.capflags = caps;
  272. trate.capflags = cpu_to_be32(caps);
  273. WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, &trate);
  274. if (ret) {
  275. ath_print(common, ATH_DBG_FATAL,
  276. "Unable to initialize Rate information on target\n");
  277. return ret;
  278. }
  279. ath_print(common, ATH_DBG_CONFIG,
  280. "Updated target STA: %pM (caps: 0x%x)\n", sta->addr, caps);
  281. return 0;
  282. }
  283. static bool check_rc_update(struct ieee80211_hw *hw, bool *cw40)
  284. {
  285. struct ath9k_htc_priv *priv = hw->priv;
  286. struct ieee80211_conf *conf = &hw->conf;
  287. if (!conf_is_ht(conf))
  288. return false;
  289. if (!(priv->op_flags & OP_ASSOCIATED) ||
  290. (priv->op_flags & OP_SCANNING))
  291. return false;
  292. if (conf_is_ht40(conf)) {
  293. if (priv->ah->curchan->chanmode &
  294. (CHANNEL_HT40PLUS | CHANNEL_HT40MINUS)) {
  295. return false;
  296. } else {
  297. *cw40 = true;
  298. return true;
  299. }
  300. } else { /* ht20 */
  301. if (priv->ah->curchan->chanmode & CHANNEL_HT20)
  302. return false;
  303. else
  304. return true;
  305. }
  306. }
  307. static void ath9k_htc_rc_update(struct ath9k_htc_priv *priv, bool is_cw40)
  308. {
  309. struct ath9k_htc_target_rate trate;
  310. struct ath_common *common = ath9k_hw_common(priv->ah);
  311. int ret;
  312. u8 cmd_rsp;
  313. memset(&trate, 0, sizeof(trate));
  314. trate = priv->tgt_rate;
  315. if (is_cw40)
  316. priv->tgt_rate.capflags |= WLAN_RC_40_FLAG;
  317. else
  318. priv->tgt_rate.capflags &= ~WLAN_RC_40_FLAG;
  319. trate.capflags = cpu_to_be32(priv->tgt_rate.capflags);
  320. WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, &trate);
  321. if (ret) {
  322. ath_print(common, ATH_DBG_FATAL,
  323. "Unable to update Rate information on target\n");
  324. return;
  325. }
  326. ath_print(common, ATH_DBG_CONFIG, "Rate control updated with "
  327. "caps:0x%x on target\n", priv->tgt_rate.capflags);
  328. }
  329. static int ath9k_htc_aggr_oper(struct ath9k_htc_priv *priv,
  330. struct ieee80211_vif *vif,
  331. u8 *sta_addr, u8 tid, bool oper)
  332. {
  333. struct ath_common *common = ath9k_hw_common(priv->ah);
  334. struct ath9k_htc_target_aggr aggr;
  335. struct ieee80211_sta *sta = NULL;
  336. struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
  337. int ret = 0;
  338. u8 cmd_rsp;
  339. if (tid > ATH9K_HTC_MAX_TID)
  340. return -EINVAL;
  341. rcu_read_lock();
  342. sta = ieee80211_find_sta(vif, sta_addr);
  343. if (sta) {
  344. ista = (struct ath9k_htc_sta *) sta->drv_priv;
  345. } else {
  346. rcu_read_unlock();
  347. return -EINVAL;
  348. }
  349. if (!ista) {
  350. rcu_read_unlock();
  351. return -EINVAL;
  352. }
  353. memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
  354. aggr.sta_index = ista->index;
  355. rcu_read_unlock();
  356. aggr.tidno = tid;
  357. aggr.aggr_enable = oper;
  358. if (oper)
  359. ista->tid_state[tid] = AGGR_START;
  360. else
  361. ista->tid_state[tid] = AGGR_STOP;
  362. WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
  363. if (ret)
  364. ath_print(common, ATH_DBG_CONFIG,
  365. "Unable to %s TX aggregation for (%pM, %d)\n",
  366. (oper) ? "start" : "stop", sta->addr, tid);
  367. else
  368. ath_print(common, ATH_DBG_CONFIG,
  369. "%s aggregation for (%pM, %d)\n",
  370. (oper) ? "Starting" : "Stopping", sta->addr, tid);
  371. return ret;
  372. }
  373. void ath9k_htc_aggr_work(struct work_struct *work)
  374. {
  375. int ret = 0;
  376. struct ath9k_htc_priv *priv =
  377. container_of(work, struct ath9k_htc_priv,
  378. ath9k_aggr_work.work);
  379. struct ath9k_htc_aggr_work *wk = &priv->aggr_work;
  380. mutex_lock(&wk->mutex);
  381. switch (wk->action) {
  382. case IEEE80211_AMPDU_TX_START:
  383. ret = ath9k_htc_aggr_oper(priv, wk->vif, wk->sta_addr,
  384. wk->tid, true);
  385. if (!ret)
  386. ieee80211_start_tx_ba_cb(wk->vif, wk->sta_addr,
  387. wk->tid);
  388. break;
  389. case IEEE80211_AMPDU_TX_STOP:
  390. ath9k_htc_aggr_oper(priv, wk->vif, wk->sta_addr,
  391. wk->tid, false);
  392. ieee80211_stop_tx_ba_cb(wk->vif, wk->sta_addr, wk->tid);
  393. break;
  394. default:
  395. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_FATAL,
  396. "Unknown AMPDU action\n");
  397. }
  398. mutex_unlock(&wk->mutex);
  399. }
  400. /*********/
  401. /* DEBUG */
  402. /*********/
  403. #ifdef CONFIG_ATH9K_HTC_DEBUGFS
  404. static int ath9k_debugfs_open(struct inode *inode, struct file *file)
  405. {
  406. file->private_data = inode->i_private;
  407. return 0;
  408. }
  409. static ssize_t read_file_tgt_stats(struct file *file, char __user *user_buf,
  410. size_t count, loff_t *ppos)
  411. {
  412. struct ath9k_htc_priv *priv =
  413. (struct ath9k_htc_priv *) file->private_data;
  414. struct ath9k_htc_target_stats cmd_rsp;
  415. char buf[512];
  416. unsigned int len = 0;
  417. int ret = 0;
  418. memset(&cmd_rsp, 0, sizeof(cmd_rsp));
  419. WMI_CMD(WMI_TGT_STATS_CMDID);
  420. if (ret)
  421. return -EINVAL;
  422. len += snprintf(buf + len, sizeof(buf) - len,
  423. "%19s : %10u\n", "TX Short Retries",
  424. be32_to_cpu(cmd_rsp.tx_shortretry));
  425. len += snprintf(buf + len, sizeof(buf) - len,
  426. "%19s : %10u\n", "TX Long Retries",
  427. be32_to_cpu(cmd_rsp.tx_longretry));
  428. len += snprintf(buf + len, sizeof(buf) - len,
  429. "%19s : %10u\n", "TX Xretries",
  430. be32_to_cpu(cmd_rsp.tx_xretries));
  431. len += snprintf(buf + len, sizeof(buf) - len,
  432. "%19s : %10u\n", "TX Unaggr. Xretries",
  433. be32_to_cpu(cmd_rsp.ht_txunaggr_xretry));
  434. len += snprintf(buf + len, sizeof(buf) - len,
  435. "%19s : %10u\n", "TX Xretries (HT)",
  436. be32_to_cpu(cmd_rsp.ht_tx_xretries));
  437. len += snprintf(buf + len, sizeof(buf) - len,
  438. "%19s : %10u\n", "TX Rate", priv->debug.txrate);
  439. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  440. }
  441. static const struct file_operations fops_tgt_stats = {
  442. .read = read_file_tgt_stats,
  443. .open = ath9k_debugfs_open,
  444. .owner = THIS_MODULE
  445. };
  446. static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
  447. size_t count, loff_t *ppos)
  448. {
  449. struct ath9k_htc_priv *priv =
  450. (struct ath9k_htc_priv *) file->private_data;
  451. char buf[512];
  452. unsigned int len = 0;
  453. len += snprintf(buf + len, sizeof(buf) - len,
  454. "%20s : %10u\n", "Buffers queued",
  455. priv->debug.tx_stats.buf_queued);
  456. len += snprintf(buf + len, sizeof(buf) - len,
  457. "%20s : %10u\n", "Buffers completed",
  458. priv->debug.tx_stats.buf_completed);
  459. len += snprintf(buf + len, sizeof(buf) - len,
  460. "%20s : %10u\n", "SKBs queued",
  461. priv->debug.tx_stats.skb_queued);
  462. len += snprintf(buf + len, sizeof(buf) - len,
  463. "%20s : %10u\n", "SKBs completed",
  464. priv->debug.tx_stats.skb_completed);
  465. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  466. }
  467. static const struct file_operations fops_xmit = {
  468. .read = read_file_xmit,
  469. .open = ath9k_debugfs_open,
  470. .owner = THIS_MODULE
  471. };
  472. static ssize_t read_file_recv(struct file *file, char __user *user_buf,
  473. size_t count, loff_t *ppos)
  474. {
  475. struct ath9k_htc_priv *priv =
  476. (struct ath9k_htc_priv *) file->private_data;
  477. char buf[512];
  478. unsigned int len = 0;
  479. len += snprintf(buf + len, sizeof(buf) - len,
  480. "%20s : %10u\n", "SKBs allocated",
  481. priv->debug.rx_stats.skb_allocated);
  482. len += snprintf(buf + len, sizeof(buf) - len,
  483. "%20s : %10u\n", "SKBs completed",
  484. priv->debug.rx_stats.skb_completed);
  485. len += snprintf(buf + len, sizeof(buf) - len,
  486. "%20s : %10u\n", "SKBs Dropped",
  487. priv->debug.rx_stats.skb_dropped);
  488. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  489. }
  490. static const struct file_operations fops_recv = {
  491. .read = read_file_recv,
  492. .open = ath9k_debugfs_open,
  493. .owner = THIS_MODULE
  494. };
  495. int ath9k_htc_init_debug(struct ath_hw *ah)
  496. {
  497. struct ath_common *common = ath9k_hw_common(ah);
  498. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  499. if (!ath9k_debugfs_root)
  500. return -ENOENT;
  501. priv->debug.debugfs_phy = debugfs_create_dir(wiphy_name(priv->hw->wiphy),
  502. ath9k_debugfs_root);
  503. if (!priv->debug.debugfs_phy)
  504. goto err;
  505. priv->debug.debugfs_tgt_stats = debugfs_create_file("tgt_stats", S_IRUSR,
  506. priv->debug.debugfs_phy,
  507. priv, &fops_tgt_stats);
  508. if (!priv->debug.debugfs_tgt_stats)
  509. goto err;
  510. priv->debug.debugfs_xmit = debugfs_create_file("xmit", S_IRUSR,
  511. priv->debug.debugfs_phy,
  512. priv, &fops_xmit);
  513. if (!priv->debug.debugfs_xmit)
  514. goto err;
  515. priv->debug.debugfs_recv = debugfs_create_file("recv", S_IRUSR,
  516. priv->debug.debugfs_phy,
  517. priv, &fops_recv);
  518. if (!priv->debug.debugfs_recv)
  519. goto err;
  520. return 0;
  521. err:
  522. ath9k_htc_exit_debug(ah);
  523. return -ENOMEM;
  524. }
  525. void ath9k_htc_exit_debug(struct ath_hw *ah)
  526. {
  527. struct ath_common *common = ath9k_hw_common(ah);
  528. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  529. debugfs_remove(priv->debug.debugfs_recv);
  530. debugfs_remove(priv->debug.debugfs_xmit);
  531. debugfs_remove(priv->debug.debugfs_tgt_stats);
  532. debugfs_remove(priv->debug.debugfs_phy);
  533. }
  534. int ath9k_htc_debug_create_root(void)
  535. {
  536. ath9k_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  537. if (!ath9k_debugfs_root)
  538. return -ENOENT;
  539. return 0;
  540. }
  541. void ath9k_htc_debug_remove_root(void)
  542. {
  543. debugfs_remove(ath9k_debugfs_root);
  544. ath9k_debugfs_root = NULL;
  545. }
  546. #endif /* CONFIG_ATH9K_HTC_DEBUGFS */
  547. /*******/
  548. /* ANI */
  549. /*******/
  550. static void ath_start_ani(struct ath9k_htc_priv *priv)
  551. {
  552. struct ath_common *common = ath9k_hw_common(priv->ah);
  553. unsigned long timestamp = jiffies_to_msecs(jiffies);
  554. common->ani.longcal_timer = timestamp;
  555. common->ani.shortcal_timer = timestamp;
  556. common->ani.checkani_timer = timestamp;
  557. ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
  558. msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  559. }
  560. void ath9k_ani_work(struct work_struct *work)
  561. {
  562. struct ath9k_htc_priv *priv =
  563. container_of(work, struct ath9k_htc_priv,
  564. ath9k_ani_work.work);
  565. struct ath_hw *ah = priv->ah;
  566. struct ath_common *common = ath9k_hw_common(ah);
  567. bool longcal = false;
  568. bool shortcal = false;
  569. bool aniflag = false;
  570. unsigned int timestamp = jiffies_to_msecs(jiffies);
  571. u32 cal_interval, short_cal_interval;
  572. short_cal_interval = ATH_STA_SHORT_CALINTERVAL;
  573. /* Long calibration runs independently of short calibration. */
  574. if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
  575. longcal = true;
  576. ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  577. common->ani.longcal_timer = timestamp;
  578. }
  579. /* Short calibration applies only while caldone is false */
  580. if (!common->ani.caldone) {
  581. if ((timestamp - common->ani.shortcal_timer) >=
  582. short_cal_interval) {
  583. shortcal = true;
  584. ath_print(common, ATH_DBG_ANI,
  585. "shortcal @%lu\n", jiffies);
  586. common->ani.shortcal_timer = timestamp;
  587. common->ani.resetcal_timer = timestamp;
  588. }
  589. } else {
  590. if ((timestamp - common->ani.resetcal_timer) >=
  591. ATH_RESTART_CALINTERVAL) {
  592. common->ani.caldone = ath9k_hw_reset_calvalid(ah);
  593. if (common->ani.caldone)
  594. common->ani.resetcal_timer = timestamp;
  595. }
  596. }
  597. /* Verify whether we must check ANI */
  598. if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
  599. aniflag = true;
  600. common->ani.checkani_timer = timestamp;
  601. }
  602. /* Skip all processing if there's nothing to do. */
  603. if (longcal || shortcal || aniflag) {
  604. /* Call ANI routine if necessary */
  605. if (aniflag)
  606. ath9k_hw_ani_monitor(ah, ah->curchan);
  607. /* Perform calibration if necessary */
  608. if (longcal || shortcal) {
  609. common->ani.caldone =
  610. ath9k_hw_calibrate(ah, ah->curchan,
  611. common->rx_chainmask,
  612. longcal);
  613. if (longcal)
  614. common->ani.noise_floor =
  615. ath9k_hw_getchan_noise(ah, ah->curchan);
  616. ath_print(common, ATH_DBG_ANI,
  617. " calibrate chan %u/%x nf: %d\n",
  618. ah->curchan->channel,
  619. ah->curchan->channelFlags,
  620. common->ani.noise_floor);
  621. }
  622. }
  623. /*
  624. * Set timer interval based on previous results.
  625. * The interval must be the shortest necessary to satisfy ANI,
  626. * short calibration and long calibration.
  627. */
  628. cal_interval = ATH_LONG_CALINTERVAL;
  629. if (priv->ah->config.enable_ani)
  630. cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
  631. if (!common->ani.caldone)
  632. cal_interval = min(cal_interval, (u32)short_cal_interval);
  633. ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
  634. msecs_to_jiffies(cal_interval));
  635. }
  636. /*******/
  637. /* LED */
  638. /*******/
  639. static void ath9k_led_blink_work(struct work_struct *work)
  640. {
  641. struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
  642. ath9k_led_blink_work.work);
  643. if (!(priv->op_flags & OP_LED_ASSOCIATED))
  644. return;
  645. if ((priv->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
  646. (priv->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
  647. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 0);
  648. else
  649. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
  650. (priv->op_flags & OP_LED_ON) ? 1 : 0);
  651. ieee80211_queue_delayed_work(priv->hw,
  652. &priv->ath9k_led_blink_work,
  653. (priv->op_flags & OP_LED_ON) ?
  654. msecs_to_jiffies(priv->led_off_duration) :
  655. msecs_to_jiffies(priv->led_on_duration));
  656. priv->led_on_duration = priv->led_on_cnt ?
  657. max((ATH_LED_ON_DURATION_IDLE - priv->led_on_cnt), 25) :
  658. ATH_LED_ON_DURATION_IDLE;
  659. priv->led_off_duration = priv->led_off_cnt ?
  660. max((ATH_LED_OFF_DURATION_IDLE - priv->led_off_cnt), 10) :
  661. ATH_LED_OFF_DURATION_IDLE;
  662. priv->led_on_cnt = priv->led_off_cnt = 0;
  663. if (priv->op_flags & OP_LED_ON)
  664. priv->op_flags &= ~OP_LED_ON;
  665. else
  666. priv->op_flags |= OP_LED_ON;
  667. }
  668. static void ath9k_led_brightness_work(struct work_struct *work)
  669. {
  670. struct ath_led *led = container_of(work, struct ath_led,
  671. brightness_work.work);
  672. struct ath9k_htc_priv *priv = led->priv;
  673. switch (led->brightness) {
  674. case LED_OFF:
  675. if (led->led_type == ATH_LED_ASSOC ||
  676. led->led_type == ATH_LED_RADIO) {
  677. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
  678. (led->led_type == ATH_LED_RADIO));
  679. priv->op_flags &= ~OP_LED_ASSOCIATED;
  680. if (led->led_type == ATH_LED_RADIO)
  681. priv->op_flags &= ~OP_LED_ON;
  682. } else {
  683. priv->led_off_cnt++;
  684. }
  685. break;
  686. case LED_FULL:
  687. if (led->led_type == ATH_LED_ASSOC) {
  688. priv->op_flags |= OP_LED_ASSOCIATED;
  689. ieee80211_queue_delayed_work(priv->hw,
  690. &priv->ath9k_led_blink_work, 0);
  691. } else if (led->led_type == ATH_LED_RADIO) {
  692. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 0);
  693. priv->op_flags |= OP_LED_ON;
  694. } else {
  695. priv->led_on_cnt++;
  696. }
  697. break;
  698. default:
  699. break;
  700. }
  701. }
  702. static void ath9k_led_brightness(struct led_classdev *led_cdev,
  703. enum led_brightness brightness)
  704. {
  705. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  706. struct ath9k_htc_priv *priv = led->priv;
  707. led->brightness = brightness;
  708. if (!(priv->op_flags & OP_LED_DEINIT))
  709. ieee80211_queue_delayed_work(priv->hw,
  710. &led->brightness_work, 0);
  711. }
  712. static void ath9k_led_stop_brightness(struct ath9k_htc_priv *priv)
  713. {
  714. cancel_delayed_work_sync(&priv->radio_led.brightness_work);
  715. cancel_delayed_work_sync(&priv->assoc_led.brightness_work);
  716. cancel_delayed_work_sync(&priv->tx_led.brightness_work);
  717. cancel_delayed_work_sync(&priv->rx_led.brightness_work);
  718. }
  719. static int ath9k_register_led(struct ath9k_htc_priv *priv, struct ath_led *led,
  720. char *trigger)
  721. {
  722. int ret;
  723. led->priv = priv;
  724. led->led_cdev.name = led->name;
  725. led->led_cdev.default_trigger = trigger;
  726. led->led_cdev.brightness_set = ath9k_led_brightness;
  727. ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &led->led_cdev);
  728. if (ret)
  729. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_FATAL,
  730. "Failed to register led:%s", led->name);
  731. else
  732. led->registered = 1;
  733. INIT_DELAYED_WORK(&led->brightness_work, ath9k_led_brightness_work);
  734. return ret;
  735. }
  736. static void ath9k_unregister_led(struct ath_led *led)
  737. {
  738. if (led->registered) {
  739. led_classdev_unregister(&led->led_cdev);
  740. led->registered = 0;
  741. }
  742. }
  743. void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
  744. {
  745. priv->op_flags |= OP_LED_DEINIT;
  746. ath9k_unregister_led(&priv->assoc_led);
  747. priv->op_flags &= ~OP_LED_ASSOCIATED;
  748. ath9k_unregister_led(&priv->tx_led);
  749. ath9k_unregister_led(&priv->rx_led);
  750. ath9k_unregister_led(&priv->radio_led);
  751. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
  752. }
  753. void ath9k_init_leds(struct ath9k_htc_priv *priv)
  754. {
  755. char *trigger;
  756. int ret;
  757. if (AR_SREV_9287(priv->ah))
  758. priv->ah->led_pin = ATH_LED_PIN_9287;
  759. else if (AR_SREV_9271(priv->ah))
  760. priv->ah->led_pin = ATH_LED_PIN_9271;
  761. else
  762. priv->ah->led_pin = ATH_LED_PIN_DEF;
  763. /* Configure gpio 1 for output */
  764. ath9k_hw_cfg_output(priv->ah, priv->ah->led_pin,
  765. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  766. /* LED off, active low */
  767. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
  768. INIT_DELAYED_WORK(&priv->ath9k_led_blink_work, ath9k_led_blink_work);
  769. trigger = ieee80211_get_radio_led_name(priv->hw);
  770. snprintf(priv->radio_led.name, sizeof(priv->radio_led.name),
  771. "ath9k-%s::radio", wiphy_name(priv->hw->wiphy));
  772. ret = ath9k_register_led(priv, &priv->radio_led, trigger);
  773. priv->radio_led.led_type = ATH_LED_RADIO;
  774. if (ret)
  775. goto fail;
  776. trigger = ieee80211_get_assoc_led_name(priv->hw);
  777. snprintf(priv->assoc_led.name, sizeof(priv->assoc_led.name),
  778. "ath9k-%s::assoc", wiphy_name(priv->hw->wiphy));
  779. ret = ath9k_register_led(priv, &priv->assoc_led, trigger);
  780. priv->assoc_led.led_type = ATH_LED_ASSOC;
  781. if (ret)
  782. goto fail;
  783. trigger = ieee80211_get_tx_led_name(priv->hw);
  784. snprintf(priv->tx_led.name, sizeof(priv->tx_led.name),
  785. "ath9k-%s::tx", wiphy_name(priv->hw->wiphy));
  786. ret = ath9k_register_led(priv, &priv->tx_led, trigger);
  787. priv->tx_led.led_type = ATH_LED_TX;
  788. if (ret)
  789. goto fail;
  790. trigger = ieee80211_get_rx_led_name(priv->hw);
  791. snprintf(priv->rx_led.name, sizeof(priv->rx_led.name),
  792. "ath9k-%s::rx", wiphy_name(priv->hw->wiphy));
  793. ret = ath9k_register_led(priv, &priv->rx_led, trigger);
  794. priv->rx_led.led_type = ATH_LED_RX;
  795. if (ret)
  796. goto fail;
  797. priv->op_flags &= ~OP_LED_DEINIT;
  798. return;
  799. fail:
  800. cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
  801. ath9k_deinit_leds(priv);
  802. }
  803. /*******************/
  804. /* Rfkill */
  805. /*******************/
  806. static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
  807. {
  808. return ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
  809. priv->ah->rfkill_polarity;
  810. }
  811. static void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
  812. {
  813. struct ath9k_htc_priv *priv = hw->priv;
  814. bool blocked = !!ath_is_rfkill_set(priv);
  815. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  816. }
  817. void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
  818. {
  819. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  820. wiphy_rfkill_start_polling(priv->hw->wiphy);
  821. }
  822. /**********************/
  823. /* mac80211 Callbacks */
  824. /**********************/
  825. static int ath9k_htc_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  826. {
  827. struct ieee80211_hdr *hdr;
  828. struct ath9k_htc_priv *priv = hw->priv;
  829. int padpos, padsize;
  830. hdr = (struct ieee80211_hdr *) skb->data;
  831. /* Add the padding after the header if this is not already done */
  832. padpos = ath9k_cmn_padpos(hdr->frame_control);
  833. padsize = padpos & 3;
  834. if (padsize && skb->len > padpos) {
  835. if (skb_headroom(skb) < padsize)
  836. return -1;
  837. skb_push(skb, padsize);
  838. memmove(skb->data, skb->data + padsize, padpos);
  839. }
  840. if (ath9k_htc_tx_start(priv, skb) != 0) {
  841. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_XMIT, "Tx failed");
  842. goto fail_tx;
  843. }
  844. return 0;
  845. fail_tx:
  846. dev_kfree_skb_any(skb);
  847. return 0;
  848. }
  849. static int ath9k_htc_start(struct ieee80211_hw *hw)
  850. {
  851. struct ath9k_htc_priv *priv = hw->priv;
  852. struct ath_hw *ah = priv->ah;
  853. struct ath_common *common = ath9k_hw_common(ah);
  854. struct ieee80211_channel *curchan = hw->conf.channel;
  855. struct ath9k_channel *init_channel;
  856. int ret = 0;
  857. enum htc_phymode mode;
  858. u16 htc_mode;
  859. u8 cmd_rsp;
  860. ath_print(common, ATH_DBG_CONFIG,
  861. "Starting driver with initial channel: %d MHz\n",
  862. curchan->center_freq);
  863. mutex_lock(&priv->mutex);
  864. /* setup initial channel */
  865. init_channel = ath9k_cmn_get_curchannel(hw, ah);
  866. /* Reset SERDES registers */
  867. ath9k_hw_configpcipowersave(ah, 0, 0);
  868. ath9k_hw_htc_resetinit(ah);
  869. ret = ath9k_hw_reset(ah, init_channel, false);
  870. if (ret) {
  871. ath_print(common, ATH_DBG_FATAL,
  872. "Unable to reset hardware; reset status %d "
  873. "(freq %u MHz)\n", ret, curchan->center_freq);
  874. goto mutex_unlock;
  875. }
  876. ath_update_txpow(priv);
  877. mode = ath9k_htc_get_curmode(priv, init_channel);
  878. htc_mode = cpu_to_be16(mode);
  879. WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
  880. if (ret)
  881. goto mutex_unlock;
  882. WMI_CMD(WMI_ATH_INIT_CMDID);
  883. if (ret)
  884. goto mutex_unlock;
  885. WMI_CMD(WMI_START_RECV_CMDID);
  886. if (ret)
  887. goto mutex_unlock;
  888. ath9k_host_rx_init(priv);
  889. priv->op_flags &= ~OP_INVALID;
  890. htc_start(priv->htc);
  891. mutex_unlock:
  892. mutex_unlock(&priv->mutex);
  893. return ret;
  894. }
  895. static void ath9k_htc_stop(struct ieee80211_hw *hw)
  896. {
  897. struct ath9k_htc_priv *priv = hw->priv;
  898. struct ath_hw *ah = priv->ah;
  899. struct ath_common *common = ath9k_hw_common(ah);
  900. int ret = 0;
  901. u8 cmd_rsp;
  902. mutex_lock(&priv->mutex);
  903. if (priv->op_flags & OP_INVALID) {
  904. ath_print(common, ATH_DBG_ANY, "Device not present\n");
  905. mutex_unlock(&priv->mutex);
  906. return;
  907. }
  908. htc_stop(priv->htc);
  909. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  910. WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
  911. WMI_CMD(WMI_STOP_RECV_CMDID);
  912. ath9k_hw_phy_disable(ah);
  913. ath9k_hw_disable(ah);
  914. ath9k_hw_configpcipowersave(ah, 1, 1);
  915. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  916. cancel_delayed_work_sync(&priv->ath9k_ani_work);
  917. cancel_delayed_work_sync(&priv->ath9k_aggr_work);
  918. cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
  919. ath9k_led_stop_brightness(priv);
  920. skb_queue_purge(&priv->tx_queue);
  921. /* Remove monitor interface here */
  922. if (ah->opmode == NL80211_IFTYPE_MONITOR) {
  923. if (ath9k_htc_remove_monitor_interface(priv))
  924. ath_print(common, ATH_DBG_FATAL,
  925. "Unable to remove monitor interface\n");
  926. else
  927. ath_print(common, ATH_DBG_CONFIG,
  928. "Monitor interface removed\n");
  929. }
  930. priv->op_flags |= OP_INVALID;
  931. mutex_unlock(&priv->mutex);
  932. ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
  933. }
  934. static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
  935. struct ieee80211_vif *vif)
  936. {
  937. struct ath9k_htc_priv *priv = hw->priv;
  938. struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
  939. struct ath_common *common = ath9k_hw_common(priv->ah);
  940. struct ath9k_htc_target_vif hvif;
  941. int ret = 0;
  942. u8 cmd_rsp;
  943. mutex_lock(&priv->mutex);
  944. /* Only one interface for now */
  945. if (priv->nvifs > 0) {
  946. ret = -ENOBUFS;
  947. goto out;
  948. }
  949. memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
  950. memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
  951. switch (vif->type) {
  952. case NL80211_IFTYPE_STATION:
  953. hvif.opmode = cpu_to_be32(HTC_M_STA);
  954. break;
  955. case NL80211_IFTYPE_ADHOC:
  956. hvif.opmode = cpu_to_be32(HTC_M_IBSS);
  957. break;
  958. default:
  959. ath_print(common, ATH_DBG_FATAL,
  960. "Interface type %d not yet supported\n", vif->type);
  961. ret = -EOPNOTSUPP;
  962. goto out;
  963. }
  964. ath_print(common, ATH_DBG_CONFIG,
  965. "Attach a VIF of type: %d\n", vif->type);
  966. priv->ah->opmode = vif->type;
  967. /* Index starts from zero on the target */
  968. avp->index = hvif.index = priv->nvifs;
  969. hvif.rtsthreshold = cpu_to_be16(2304);
  970. WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
  971. if (ret)
  972. goto out;
  973. priv->nvifs++;
  974. /*
  975. * We need a node in target to tx mgmt frames
  976. * before association.
  977. */
  978. ret = ath9k_htc_add_station(priv, vif, NULL);
  979. if (ret)
  980. goto out;
  981. ret = ath9k_htc_update_cap_target(priv);
  982. if (ret)
  983. ath_print(common, ATH_DBG_CONFIG, "Failed to update"
  984. " capability in target \n");
  985. priv->vif = vif;
  986. out:
  987. mutex_unlock(&priv->mutex);
  988. return ret;
  989. }
  990. static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
  991. struct ieee80211_vif *vif)
  992. {
  993. struct ath9k_htc_priv *priv = hw->priv;
  994. struct ath_common *common = ath9k_hw_common(priv->ah);
  995. struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
  996. struct ath9k_htc_target_vif hvif;
  997. int ret = 0;
  998. u8 cmd_rsp;
  999. ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
  1000. mutex_lock(&priv->mutex);
  1001. memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
  1002. memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
  1003. hvif.index = avp->index;
  1004. WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
  1005. priv->nvifs--;
  1006. ath9k_htc_remove_station(priv, vif, NULL);
  1007. if (vif->type == NL80211_IFTYPE_ADHOC) {
  1008. spin_lock_bh(&priv->beacon_lock);
  1009. if (priv->beacon)
  1010. dev_kfree_skb_any(priv->beacon);
  1011. priv->beacon = NULL;
  1012. spin_unlock_bh(&priv->beacon_lock);
  1013. }
  1014. priv->vif = NULL;
  1015. mutex_unlock(&priv->mutex);
  1016. }
  1017. static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
  1018. {
  1019. struct ath9k_htc_priv *priv = hw->priv;
  1020. struct ath_common *common = ath9k_hw_common(priv->ah);
  1021. struct ieee80211_conf *conf = &hw->conf;
  1022. mutex_lock(&priv->mutex);
  1023. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1024. struct ieee80211_channel *curchan = hw->conf.channel;
  1025. int pos = curchan->hw_value;
  1026. bool is_cw40 = false;
  1027. ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  1028. curchan->center_freq);
  1029. if (check_rc_update(hw, &is_cw40))
  1030. ath9k_htc_rc_update(priv, is_cw40);
  1031. ath9k_cmn_update_ichannel(hw, &priv->ah->channels[pos]);
  1032. if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
  1033. ath_print(common, ATH_DBG_FATAL,
  1034. "Unable to set channel\n");
  1035. mutex_unlock(&priv->mutex);
  1036. return -EINVAL;
  1037. }
  1038. }
  1039. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  1040. if (conf->flags & IEEE80211_CONF_MONITOR) {
  1041. if (ath9k_htc_add_monitor_interface(priv))
  1042. ath_print(common, ATH_DBG_FATAL,
  1043. "Failed to set monitor mode\n");
  1044. else
  1045. ath_print(common, ATH_DBG_CONFIG,
  1046. "HW opmode set to Monitor mode\n");
  1047. }
  1048. }
  1049. mutex_unlock(&priv->mutex);
  1050. return 0;
  1051. }
  1052. #define SUPPORTED_FILTERS \
  1053. (FIF_PROMISC_IN_BSS | \
  1054. FIF_ALLMULTI | \
  1055. FIF_CONTROL | \
  1056. FIF_PSPOLL | \
  1057. FIF_OTHER_BSS | \
  1058. FIF_BCN_PRBRESP_PROMISC | \
  1059. FIF_FCSFAIL)
  1060. static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
  1061. unsigned int changed_flags,
  1062. unsigned int *total_flags,
  1063. u64 multicast)
  1064. {
  1065. struct ath9k_htc_priv *priv = hw->priv;
  1066. u32 rfilt;
  1067. mutex_lock(&priv->mutex);
  1068. changed_flags &= SUPPORTED_FILTERS;
  1069. *total_flags &= SUPPORTED_FILTERS;
  1070. priv->rxfilter = *total_flags;
  1071. rfilt = ath9k_cmn_calcrxfilter(hw, priv->ah, priv->rxfilter);
  1072. ath9k_hw_setrxfilter(priv->ah, rfilt);
  1073. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_CONFIG,
  1074. "Set HW RX filter: 0x%x\n", rfilt);
  1075. mutex_unlock(&priv->mutex);
  1076. }
  1077. static void ath9k_htc_sta_notify(struct ieee80211_hw *hw,
  1078. struct ieee80211_vif *vif,
  1079. enum sta_notify_cmd cmd,
  1080. struct ieee80211_sta *sta)
  1081. {
  1082. struct ath9k_htc_priv *priv = hw->priv;
  1083. int ret;
  1084. switch (cmd) {
  1085. case STA_NOTIFY_ADD:
  1086. ret = ath9k_htc_add_station(priv, vif, sta);
  1087. if (!ret)
  1088. ath9k_htc_init_rate(priv, vif, sta);
  1089. break;
  1090. case STA_NOTIFY_REMOVE:
  1091. ath9k_htc_remove_station(priv, vif, sta);
  1092. break;
  1093. default:
  1094. break;
  1095. }
  1096. }
  1097. static int ath9k_htc_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1098. const struct ieee80211_tx_queue_params *params)
  1099. {
  1100. struct ath9k_htc_priv *priv = hw->priv;
  1101. struct ath_common *common = ath9k_hw_common(priv->ah);
  1102. struct ath9k_tx_queue_info qi;
  1103. int ret = 0, qnum;
  1104. if (queue >= WME_NUM_AC)
  1105. return 0;
  1106. mutex_lock(&priv->mutex);
  1107. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  1108. qi.tqi_aifs = params->aifs;
  1109. qi.tqi_cwmin = params->cw_min;
  1110. qi.tqi_cwmax = params->cw_max;
  1111. qi.tqi_burstTime = params->txop;
  1112. qnum = get_hw_qnum(queue, priv->hwq_map);
  1113. ath_print(common, ATH_DBG_CONFIG,
  1114. "Configure tx [queue/hwq] [%d/%d], "
  1115. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  1116. queue, qnum, params->aifs, params->cw_min,
  1117. params->cw_max, params->txop);
  1118. ret = ath_htc_txq_update(priv, qnum, &qi);
  1119. if (ret)
  1120. ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
  1121. mutex_unlock(&priv->mutex);
  1122. return ret;
  1123. }
  1124. static int ath9k_htc_set_key(struct ieee80211_hw *hw,
  1125. enum set_key_cmd cmd,
  1126. struct ieee80211_vif *vif,
  1127. struct ieee80211_sta *sta,
  1128. struct ieee80211_key_conf *key)
  1129. {
  1130. struct ath9k_htc_priv *priv = hw->priv;
  1131. struct ath_common *common = ath9k_hw_common(priv->ah);
  1132. int ret = 0;
  1133. if (htc_modparam_nohwcrypt)
  1134. return -ENOSPC;
  1135. mutex_lock(&priv->mutex);
  1136. ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
  1137. switch (cmd) {
  1138. case SET_KEY:
  1139. ret = ath9k_cmn_key_config(common, vif, sta, key);
  1140. if (ret >= 0) {
  1141. key->hw_key_idx = ret;
  1142. /* push IV and Michael MIC generation to stack */
  1143. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1144. if (key->alg == ALG_TKIP)
  1145. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1146. if (priv->ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  1147. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  1148. ret = 0;
  1149. }
  1150. break;
  1151. case DISABLE_KEY:
  1152. ath9k_cmn_key_delete(common, key);
  1153. break;
  1154. default:
  1155. ret = -EINVAL;
  1156. }
  1157. mutex_unlock(&priv->mutex);
  1158. return ret;
  1159. }
  1160. static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
  1161. struct ieee80211_vif *vif,
  1162. struct ieee80211_bss_conf *bss_conf,
  1163. u32 changed)
  1164. {
  1165. struct ath9k_htc_priv *priv = hw->priv;
  1166. struct ath_hw *ah = priv->ah;
  1167. struct ath_common *common = ath9k_hw_common(ah);
  1168. mutex_lock(&priv->mutex);
  1169. if (changed & BSS_CHANGED_ASSOC) {
  1170. common->curaid = bss_conf->assoc ?
  1171. bss_conf->aid : 0;
  1172. ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  1173. bss_conf->assoc);
  1174. if (bss_conf->assoc) {
  1175. priv->op_flags |= OP_ASSOCIATED;
  1176. ath_start_ani(priv);
  1177. } else {
  1178. priv->op_flags &= ~OP_ASSOCIATED;
  1179. cancel_delayed_work_sync(&priv->ath9k_ani_work);
  1180. }
  1181. }
  1182. if (changed & BSS_CHANGED_BSSID) {
  1183. /* Set BSSID */
  1184. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1185. ath9k_hw_write_associd(ah);
  1186. ath_print(common, ATH_DBG_CONFIG,
  1187. "BSSID: %pM aid: 0x%x\n",
  1188. common->curbssid, common->curaid);
  1189. }
  1190. if ((changed & BSS_CHANGED_BEACON_INT) ||
  1191. (changed & BSS_CHANGED_BEACON) ||
  1192. ((changed & BSS_CHANGED_BEACON_ENABLED) &&
  1193. bss_conf->enable_beacon)) {
  1194. priv->op_flags |= OP_ENABLE_BEACON;
  1195. ath9k_htc_beacon_config(priv, vif, bss_conf);
  1196. }
  1197. if (changed & BSS_CHANGED_BEACON)
  1198. ath9k_htc_beacon_update(priv, vif);
  1199. if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
  1200. !bss_conf->enable_beacon) {
  1201. priv->op_flags &= ~OP_ENABLE_BEACON;
  1202. ath9k_htc_beacon_config(priv, vif, bss_conf);
  1203. }
  1204. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1205. ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  1206. bss_conf->use_short_preamble);
  1207. if (bss_conf->use_short_preamble)
  1208. priv->op_flags |= OP_PREAMBLE_SHORT;
  1209. else
  1210. priv->op_flags &= ~OP_PREAMBLE_SHORT;
  1211. }
  1212. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1213. ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  1214. bss_conf->use_cts_prot);
  1215. if (bss_conf->use_cts_prot &&
  1216. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  1217. priv->op_flags |= OP_PROTECT_ENABLE;
  1218. else
  1219. priv->op_flags &= ~OP_PROTECT_ENABLE;
  1220. }
  1221. if (changed & BSS_CHANGED_ERP_SLOT) {
  1222. if (bss_conf->use_short_slot)
  1223. ah->slottime = 9;
  1224. else
  1225. ah->slottime = 20;
  1226. ath9k_hw_init_global_settings(ah);
  1227. }
  1228. mutex_unlock(&priv->mutex);
  1229. }
  1230. static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw)
  1231. {
  1232. struct ath9k_htc_priv *priv = hw->priv;
  1233. u64 tsf;
  1234. mutex_lock(&priv->mutex);
  1235. tsf = ath9k_hw_gettsf64(priv->ah);
  1236. mutex_unlock(&priv->mutex);
  1237. return tsf;
  1238. }
  1239. static void ath9k_htc_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  1240. {
  1241. struct ath9k_htc_priv *priv = hw->priv;
  1242. mutex_lock(&priv->mutex);
  1243. ath9k_hw_settsf64(priv->ah, tsf);
  1244. mutex_unlock(&priv->mutex);
  1245. }
  1246. static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw)
  1247. {
  1248. struct ath9k_htc_priv *priv = hw->priv;
  1249. mutex_lock(&priv->mutex);
  1250. ath9k_hw_reset_tsf(priv->ah);
  1251. mutex_unlock(&priv->mutex);
  1252. }
  1253. static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
  1254. struct ieee80211_vif *vif,
  1255. enum ieee80211_ampdu_mlme_action action,
  1256. struct ieee80211_sta *sta,
  1257. u16 tid, u16 *ssn)
  1258. {
  1259. struct ath9k_htc_priv *priv = hw->priv;
  1260. struct ath9k_htc_aggr_work *work = &priv->aggr_work;
  1261. struct ath9k_htc_sta *ista;
  1262. switch (action) {
  1263. case IEEE80211_AMPDU_RX_START:
  1264. break;
  1265. case IEEE80211_AMPDU_RX_STOP:
  1266. break;
  1267. case IEEE80211_AMPDU_TX_START:
  1268. case IEEE80211_AMPDU_TX_STOP:
  1269. if (!(priv->op_flags & OP_TXAGGR))
  1270. return -ENOTSUPP;
  1271. memcpy(work->sta_addr, sta->addr, ETH_ALEN);
  1272. work->hw = hw;
  1273. work->vif = vif;
  1274. work->action = action;
  1275. work->tid = tid;
  1276. ieee80211_queue_delayed_work(hw, &priv->ath9k_aggr_work, 0);
  1277. break;
  1278. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1279. ista = (struct ath9k_htc_sta *) sta->drv_priv;
  1280. ista->tid_state[tid] = AGGR_OPERATIONAL;
  1281. break;
  1282. default:
  1283. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_FATAL,
  1284. "Unknown AMPDU action\n");
  1285. }
  1286. return 0;
  1287. }
  1288. static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
  1289. {
  1290. struct ath9k_htc_priv *priv = hw->priv;
  1291. mutex_lock(&priv->mutex);
  1292. spin_lock_bh(&priv->beacon_lock);
  1293. priv->op_flags |= OP_SCANNING;
  1294. spin_unlock_bh(&priv->beacon_lock);
  1295. cancel_delayed_work_sync(&priv->ath9k_ani_work);
  1296. mutex_unlock(&priv->mutex);
  1297. }
  1298. static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
  1299. {
  1300. struct ath9k_htc_priv *priv = hw->priv;
  1301. mutex_lock(&priv->mutex);
  1302. spin_lock_bh(&priv->beacon_lock);
  1303. priv->op_flags &= ~OP_SCANNING;
  1304. spin_unlock_bh(&priv->beacon_lock);
  1305. priv->op_flags |= OP_FULL_RESET;
  1306. ath_start_ani(priv);
  1307. mutex_unlock(&priv->mutex);
  1308. }
  1309. static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  1310. {
  1311. return 0;
  1312. }
  1313. static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
  1314. u8 coverage_class)
  1315. {
  1316. struct ath9k_htc_priv *priv = hw->priv;
  1317. mutex_lock(&priv->mutex);
  1318. priv->ah->coverage_class = coverage_class;
  1319. ath9k_hw_init_global_settings(priv->ah);
  1320. mutex_unlock(&priv->mutex);
  1321. }
  1322. struct ieee80211_ops ath9k_htc_ops = {
  1323. .tx = ath9k_htc_tx,
  1324. .start = ath9k_htc_start,
  1325. .stop = ath9k_htc_stop,
  1326. .add_interface = ath9k_htc_add_interface,
  1327. .remove_interface = ath9k_htc_remove_interface,
  1328. .config = ath9k_htc_config,
  1329. .configure_filter = ath9k_htc_configure_filter,
  1330. .sta_notify = ath9k_htc_sta_notify,
  1331. .conf_tx = ath9k_htc_conf_tx,
  1332. .bss_info_changed = ath9k_htc_bss_info_changed,
  1333. .set_key = ath9k_htc_set_key,
  1334. .get_tsf = ath9k_htc_get_tsf,
  1335. .set_tsf = ath9k_htc_set_tsf,
  1336. .reset_tsf = ath9k_htc_reset_tsf,
  1337. .ampdu_action = ath9k_htc_ampdu_action,
  1338. .sw_scan_start = ath9k_htc_sw_scan_start,
  1339. .sw_scan_complete = ath9k_htc_sw_scan_complete,
  1340. .set_rts_threshold = ath9k_htc_set_rts_threshold,
  1341. .rfkill_poll = ath9k_htc_rfkill_poll_state,
  1342. .set_coverage_class = ath9k_htc_set_coverage_class,
  1343. };