htc_drv_main.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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. memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
  342. rcu_read_lock();
  343. /* Check if we are able to retrieve the station */
  344. sta = ieee80211_find_sta(vif, sta_addr);
  345. if (!sta) {
  346. rcu_read_unlock();
  347. return -EINVAL;
  348. }
  349. ista = (struct ath9k_htc_sta *) sta->drv_priv;
  350. if (oper)
  351. ista->tid_state[tid] = AGGR_START;
  352. else
  353. ista->tid_state[tid] = AGGR_STOP;
  354. aggr.sta_index = ista->index;
  355. rcu_read_unlock();
  356. aggr.tidno = tid;
  357. aggr.aggr_enable = oper;
  358. WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
  359. if (ret)
  360. ath_print(common, ATH_DBG_CONFIG,
  361. "Unable to %s TX aggregation for (%pM, %d)\n",
  362. (oper) ? "start" : "stop", sta->addr, tid);
  363. else
  364. ath_print(common, ATH_DBG_CONFIG,
  365. "%s aggregation for (%pM, %d)\n",
  366. (oper) ? "Starting" : "Stopping", sta->addr, tid);
  367. return ret;
  368. }
  369. void ath9k_htc_aggr_work(struct work_struct *work)
  370. {
  371. int ret = 0;
  372. struct ath9k_htc_priv *priv =
  373. container_of(work, struct ath9k_htc_priv,
  374. ath9k_aggr_work.work);
  375. struct ath9k_htc_aggr_work *wk = &priv->aggr_work;
  376. mutex_lock(&wk->mutex);
  377. switch (wk->action) {
  378. case IEEE80211_AMPDU_TX_START:
  379. ret = ath9k_htc_aggr_oper(priv, wk->vif, wk->sta_addr,
  380. wk->tid, true);
  381. if (!ret)
  382. ieee80211_start_tx_ba_cb(wk->vif, wk->sta_addr,
  383. wk->tid);
  384. break;
  385. case IEEE80211_AMPDU_TX_STOP:
  386. ath9k_htc_aggr_oper(priv, wk->vif, wk->sta_addr,
  387. wk->tid, false);
  388. ieee80211_stop_tx_ba_cb(wk->vif, wk->sta_addr, wk->tid);
  389. break;
  390. default:
  391. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_FATAL,
  392. "Unknown AMPDU action\n");
  393. }
  394. mutex_unlock(&wk->mutex);
  395. }
  396. /*********/
  397. /* DEBUG */
  398. /*********/
  399. #ifdef CONFIG_ATH9K_HTC_DEBUGFS
  400. static int ath9k_debugfs_open(struct inode *inode, struct file *file)
  401. {
  402. file->private_data = inode->i_private;
  403. return 0;
  404. }
  405. static ssize_t read_file_tgt_stats(struct file *file, char __user *user_buf,
  406. size_t count, loff_t *ppos)
  407. {
  408. struct ath9k_htc_priv *priv =
  409. (struct ath9k_htc_priv *) file->private_data;
  410. struct ath9k_htc_target_stats cmd_rsp;
  411. char buf[512];
  412. unsigned int len = 0;
  413. int ret = 0;
  414. memset(&cmd_rsp, 0, sizeof(cmd_rsp));
  415. WMI_CMD(WMI_TGT_STATS_CMDID);
  416. if (ret)
  417. return -EINVAL;
  418. len += snprintf(buf + len, sizeof(buf) - len,
  419. "%19s : %10u\n", "TX Short Retries",
  420. be32_to_cpu(cmd_rsp.tx_shortretry));
  421. len += snprintf(buf + len, sizeof(buf) - len,
  422. "%19s : %10u\n", "TX Long Retries",
  423. be32_to_cpu(cmd_rsp.tx_longretry));
  424. len += snprintf(buf + len, sizeof(buf) - len,
  425. "%19s : %10u\n", "TX Xretries",
  426. be32_to_cpu(cmd_rsp.tx_xretries));
  427. len += snprintf(buf + len, sizeof(buf) - len,
  428. "%19s : %10u\n", "TX Unaggr. Xretries",
  429. be32_to_cpu(cmd_rsp.ht_txunaggr_xretry));
  430. len += snprintf(buf + len, sizeof(buf) - len,
  431. "%19s : %10u\n", "TX Xretries (HT)",
  432. be32_to_cpu(cmd_rsp.ht_tx_xretries));
  433. len += snprintf(buf + len, sizeof(buf) - len,
  434. "%19s : %10u\n", "TX Rate", priv->debug.txrate);
  435. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  436. }
  437. static const struct file_operations fops_tgt_stats = {
  438. .read = read_file_tgt_stats,
  439. .open = ath9k_debugfs_open,
  440. .owner = THIS_MODULE
  441. };
  442. static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
  443. size_t count, loff_t *ppos)
  444. {
  445. struct ath9k_htc_priv *priv =
  446. (struct ath9k_htc_priv *) file->private_data;
  447. char buf[512];
  448. unsigned int len = 0;
  449. len += snprintf(buf + len, sizeof(buf) - len,
  450. "%20s : %10u\n", "Buffers queued",
  451. priv->debug.tx_stats.buf_queued);
  452. len += snprintf(buf + len, sizeof(buf) - len,
  453. "%20s : %10u\n", "Buffers completed",
  454. priv->debug.tx_stats.buf_completed);
  455. len += snprintf(buf + len, sizeof(buf) - len,
  456. "%20s : %10u\n", "SKBs queued",
  457. priv->debug.tx_stats.skb_queued);
  458. len += snprintf(buf + len, sizeof(buf) - len,
  459. "%20s : %10u\n", "SKBs completed",
  460. priv->debug.tx_stats.skb_completed);
  461. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  462. }
  463. static const struct file_operations fops_xmit = {
  464. .read = read_file_xmit,
  465. .open = ath9k_debugfs_open,
  466. .owner = THIS_MODULE
  467. };
  468. static ssize_t read_file_recv(struct file *file, char __user *user_buf,
  469. size_t count, loff_t *ppos)
  470. {
  471. struct ath9k_htc_priv *priv =
  472. (struct ath9k_htc_priv *) file->private_data;
  473. char buf[512];
  474. unsigned int len = 0;
  475. len += snprintf(buf + len, sizeof(buf) - len,
  476. "%20s : %10u\n", "SKBs allocated",
  477. priv->debug.rx_stats.skb_allocated);
  478. len += snprintf(buf + len, sizeof(buf) - len,
  479. "%20s : %10u\n", "SKBs completed",
  480. priv->debug.rx_stats.skb_completed);
  481. len += snprintf(buf + len, sizeof(buf) - len,
  482. "%20s : %10u\n", "SKBs Dropped",
  483. priv->debug.rx_stats.skb_dropped);
  484. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  485. }
  486. static const struct file_operations fops_recv = {
  487. .read = read_file_recv,
  488. .open = ath9k_debugfs_open,
  489. .owner = THIS_MODULE
  490. };
  491. int ath9k_htc_init_debug(struct ath_hw *ah)
  492. {
  493. struct ath_common *common = ath9k_hw_common(ah);
  494. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  495. if (!ath9k_debugfs_root)
  496. return -ENOENT;
  497. priv->debug.debugfs_phy = debugfs_create_dir(wiphy_name(priv->hw->wiphy),
  498. ath9k_debugfs_root);
  499. if (!priv->debug.debugfs_phy)
  500. goto err;
  501. priv->debug.debugfs_tgt_stats = debugfs_create_file("tgt_stats", S_IRUSR,
  502. priv->debug.debugfs_phy,
  503. priv, &fops_tgt_stats);
  504. if (!priv->debug.debugfs_tgt_stats)
  505. goto err;
  506. priv->debug.debugfs_xmit = debugfs_create_file("xmit", S_IRUSR,
  507. priv->debug.debugfs_phy,
  508. priv, &fops_xmit);
  509. if (!priv->debug.debugfs_xmit)
  510. goto err;
  511. priv->debug.debugfs_recv = debugfs_create_file("recv", S_IRUSR,
  512. priv->debug.debugfs_phy,
  513. priv, &fops_recv);
  514. if (!priv->debug.debugfs_recv)
  515. goto err;
  516. return 0;
  517. err:
  518. ath9k_htc_exit_debug(ah);
  519. return -ENOMEM;
  520. }
  521. void ath9k_htc_exit_debug(struct ath_hw *ah)
  522. {
  523. struct ath_common *common = ath9k_hw_common(ah);
  524. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  525. debugfs_remove(priv->debug.debugfs_recv);
  526. debugfs_remove(priv->debug.debugfs_xmit);
  527. debugfs_remove(priv->debug.debugfs_tgt_stats);
  528. debugfs_remove(priv->debug.debugfs_phy);
  529. }
  530. int ath9k_htc_debug_create_root(void)
  531. {
  532. ath9k_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
  533. if (!ath9k_debugfs_root)
  534. return -ENOENT;
  535. return 0;
  536. }
  537. void ath9k_htc_debug_remove_root(void)
  538. {
  539. debugfs_remove(ath9k_debugfs_root);
  540. ath9k_debugfs_root = NULL;
  541. }
  542. #endif /* CONFIG_ATH9K_HTC_DEBUGFS */
  543. /*******/
  544. /* ANI */
  545. /*******/
  546. static void ath_start_ani(struct ath9k_htc_priv *priv)
  547. {
  548. struct ath_common *common = ath9k_hw_common(priv->ah);
  549. unsigned long timestamp = jiffies_to_msecs(jiffies);
  550. common->ani.longcal_timer = timestamp;
  551. common->ani.shortcal_timer = timestamp;
  552. common->ani.checkani_timer = timestamp;
  553. ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
  554. msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  555. }
  556. void ath9k_ani_work(struct work_struct *work)
  557. {
  558. struct ath9k_htc_priv *priv =
  559. container_of(work, struct ath9k_htc_priv,
  560. ath9k_ani_work.work);
  561. struct ath_hw *ah = priv->ah;
  562. struct ath_common *common = ath9k_hw_common(ah);
  563. bool longcal = false;
  564. bool shortcal = false;
  565. bool aniflag = false;
  566. unsigned int timestamp = jiffies_to_msecs(jiffies);
  567. u32 cal_interval, short_cal_interval;
  568. short_cal_interval = ATH_STA_SHORT_CALINTERVAL;
  569. /* Long calibration runs independently of short calibration. */
  570. if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
  571. longcal = true;
  572. ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  573. common->ani.longcal_timer = timestamp;
  574. }
  575. /* Short calibration applies only while caldone is false */
  576. if (!common->ani.caldone) {
  577. if ((timestamp - common->ani.shortcal_timer) >=
  578. short_cal_interval) {
  579. shortcal = true;
  580. ath_print(common, ATH_DBG_ANI,
  581. "shortcal @%lu\n", jiffies);
  582. common->ani.shortcal_timer = timestamp;
  583. common->ani.resetcal_timer = timestamp;
  584. }
  585. } else {
  586. if ((timestamp - common->ani.resetcal_timer) >=
  587. ATH_RESTART_CALINTERVAL) {
  588. common->ani.caldone = ath9k_hw_reset_calvalid(ah);
  589. if (common->ani.caldone)
  590. common->ani.resetcal_timer = timestamp;
  591. }
  592. }
  593. /* Verify whether we must check ANI */
  594. if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
  595. aniflag = true;
  596. common->ani.checkani_timer = timestamp;
  597. }
  598. /* Skip all processing if there's nothing to do. */
  599. if (longcal || shortcal || aniflag) {
  600. /* Call ANI routine if necessary */
  601. if (aniflag)
  602. ath9k_hw_ani_monitor(ah, ah->curchan);
  603. /* Perform calibration if necessary */
  604. if (longcal || shortcal) {
  605. common->ani.caldone =
  606. ath9k_hw_calibrate(ah, ah->curchan,
  607. common->rx_chainmask,
  608. longcal);
  609. if (longcal)
  610. common->ani.noise_floor =
  611. ath9k_hw_getchan_noise(ah, ah->curchan);
  612. ath_print(common, ATH_DBG_ANI,
  613. " calibrate chan %u/%x nf: %d\n",
  614. ah->curchan->channel,
  615. ah->curchan->channelFlags,
  616. common->ani.noise_floor);
  617. }
  618. }
  619. /*
  620. * Set timer interval based on previous results.
  621. * The interval must be the shortest necessary to satisfy ANI,
  622. * short calibration and long calibration.
  623. */
  624. cal_interval = ATH_LONG_CALINTERVAL;
  625. if (priv->ah->config.enable_ani)
  626. cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
  627. if (!common->ani.caldone)
  628. cal_interval = min(cal_interval, (u32)short_cal_interval);
  629. ieee80211_queue_delayed_work(common->hw, &priv->ath9k_ani_work,
  630. msecs_to_jiffies(cal_interval));
  631. }
  632. /*******/
  633. /* LED */
  634. /*******/
  635. static void ath9k_led_blink_work(struct work_struct *work)
  636. {
  637. struct ath9k_htc_priv *priv = container_of(work, struct ath9k_htc_priv,
  638. ath9k_led_blink_work.work);
  639. if (!(priv->op_flags & OP_LED_ASSOCIATED))
  640. return;
  641. if ((priv->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
  642. (priv->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
  643. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 0);
  644. else
  645. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
  646. (priv->op_flags & OP_LED_ON) ? 1 : 0);
  647. ieee80211_queue_delayed_work(priv->hw,
  648. &priv->ath9k_led_blink_work,
  649. (priv->op_flags & OP_LED_ON) ?
  650. msecs_to_jiffies(priv->led_off_duration) :
  651. msecs_to_jiffies(priv->led_on_duration));
  652. priv->led_on_duration = priv->led_on_cnt ?
  653. max((ATH_LED_ON_DURATION_IDLE - priv->led_on_cnt), 25) :
  654. ATH_LED_ON_DURATION_IDLE;
  655. priv->led_off_duration = priv->led_off_cnt ?
  656. max((ATH_LED_OFF_DURATION_IDLE - priv->led_off_cnt), 10) :
  657. ATH_LED_OFF_DURATION_IDLE;
  658. priv->led_on_cnt = priv->led_off_cnt = 0;
  659. if (priv->op_flags & OP_LED_ON)
  660. priv->op_flags &= ~OP_LED_ON;
  661. else
  662. priv->op_flags |= OP_LED_ON;
  663. }
  664. static void ath9k_led_brightness_work(struct work_struct *work)
  665. {
  666. struct ath_led *led = container_of(work, struct ath_led,
  667. brightness_work.work);
  668. struct ath9k_htc_priv *priv = led->priv;
  669. switch (led->brightness) {
  670. case LED_OFF:
  671. if (led->led_type == ATH_LED_ASSOC ||
  672. led->led_type == ATH_LED_RADIO) {
  673. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin,
  674. (led->led_type == ATH_LED_RADIO));
  675. priv->op_flags &= ~OP_LED_ASSOCIATED;
  676. if (led->led_type == ATH_LED_RADIO)
  677. priv->op_flags &= ~OP_LED_ON;
  678. } else {
  679. priv->led_off_cnt++;
  680. }
  681. break;
  682. case LED_FULL:
  683. if (led->led_type == ATH_LED_ASSOC) {
  684. priv->op_flags |= OP_LED_ASSOCIATED;
  685. ieee80211_queue_delayed_work(priv->hw,
  686. &priv->ath9k_led_blink_work, 0);
  687. } else if (led->led_type == ATH_LED_RADIO) {
  688. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 0);
  689. priv->op_flags |= OP_LED_ON;
  690. } else {
  691. priv->led_on_cnt++;
  692. }
  693. break;
  694. default:
  695. break;
  696. }
  697. }
  698. static void ath9k_led_brightness(struct led_classdev *led_cdev,
  699. enum led_brightness brightness)
  700. {
  701. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  702. struct ath9k_htc_priv *priv = led->priv;
  703. led->brightness = brightness;
  704. if (!(priv->op_flags & OP_LED_DEINIT))
  705. ieee80211_queue_delayed_work(priv->hw,
  706. &led->brightness_work, 0);
  707. }
  708. static void ath9k_led_stop_brightness(struct ath9k_htc_priv *priv)
  709. {
  710. cancel_delayed_work_sync(&priv->radio_led.brightness_work);
  711. cancel_delayed_work_sync(&priv->assoc_led.brightness_work);
  712. cancel_delayed_work_sync(&priv->tx_led.brightness_work);
  713. cancel_delayed_work_sync(&priv->rx_led.brightness_work);
  714. }
  715. static int ath9k_register_led(struct ath9k_htc_priv *priv, struct ath_led *led,
  716. char *trigger)
  717. {
  718. int ret;
  719. led->priv = priv;
  720. led->led_cdev.name = led->name;
  721. led->led_cdev.default_trigger = trigger;
  722. led->led_cdev.brightness_set = ath9k_led_brightness;
  723. ret = led_classdev_register(wiphy_dev(priv->hw->wiphy), &led->led_cdev);
  724. if (ret)
  725. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_FATAL,
  726. "Failed to register led:%s", led->name);
  727. else
  728. led->registered = 1;
  729. INIT_DELAYED_WORK(&led->brightness_work, ath9k_led_brightness_work);
  730. return ret;
  731. }
  732. static void ath9k_unregister_led(struct ath_led *led)
  733. {
  734. if (led->registered) {
  735. led_classdev_unregister(&led->led_cdev);
  736. led->registered = 0;
  737. }
  738. }
  739. void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
  740. {
  741. priv->op_flags |= OP_LED_DEINIT;
  742. ath9k_unregister_led(&priv->assoc_led);
  743. priv->op_flags &= ~OP_LED_ASSOCIATED;
  744. ath9k_unregister_led(&priv->tx_led);
  745. ath9k_unregister_led(&priv->rx_led);
  746. ath9k_unregister_led(&priv->radio_led);
  747. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
  748. }
  749. void ath9k_init_leds(struct ath9k_htc_priv *priv)
  750. {
  751. char *trigger;
  752. int ret;
  753. if (AR_SREV_9287(priv->ah))
  754. priv->ah->led_pin = ATH_LED_PIN_9287;
  755. else if (AR_SREV_9271(priv->ah))
  756. priv->ah->led_pin = ATH_LED_PIN_9271;
  757. else
  758. priv->ah->led_pin = ATH_LED_PIN_DEF;
  759. /* Configure gpio 1 for output */
  760. ath9k_hw_cfg_output(priv->ah, priv->ah->led_pin,
  761. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  762. /* LED off, active low */
  763. ath9k_hw_set_gpio(priv->ah, priv->ah->led_pin, 1);
  764. INIT_DELAYED_WORK(&priv->ath9k_led_blink_work, ath9k_led_blink_work);
  765. trigger = ieee80211_get_radio_led_name(priv->hw);
  766. snprintf(priv->radio_led.name, sizeof(priv->radio_led.name),
  767. "ath9k-%s::radio", wiphy_name(priv->hw->wiphy));
  768. ret = ath9k_register_led(priv, &priv->radio_led, trigger);
  769. priv->radio_led.led_type = ATH_LED_RADIO;
  770. if (ret)
  771. goto fail;
  772. trigger = ieee80211_get_assoc_led_name(priv->hw);
  773. snprintf(priv->assoc_led.name, sizeof(priv->assoc_led.name),
  774. "ath9k-%s::assoc", wiphy_name(priv->hw->wiphy));
  775. ret = ath9k_register_led(priv, &priv->assoc_led, trigger);
  776. priv->assoc_led.led_type = ATH_LED_ASSOC;
  777. if (ret)
  778. goto fail;
  779. trigger = ieee80211_get_tx_led_name(priv->hw);
  780. snprintf(priv->tx_led.name, sizeof(priv->tx_led.name),
  781. "ath9k-%s::tx", wiphy_name(priv->hw->wiphy));
  782. ret = ath9k_register_led(priv, &priv->tx_led, trigger);
  783. priv->tx_led.led_type = ATH_LED_TX;
  784. if (ret)
  785. goto fail;
  786. trigger = ieee80211_get_rx_led_name(priv->hw);
  787. snprintf(priv->rx_led.name, sizeof(priv->rx_led.name),
  788. "ath9k-%s::rx", wiphy_name(priv->hw->wiphy));
  789. ret = ath9k_register_led(priv, &priv->rx_led, trigger);
  790. priv->rx_led.led_type = ATH_LED_RX;
  791. if (ret)
  792. goto fail;
  793. priv->op_flags &= ~OP_LED_DEINIT;
  794. return;
  795. fail:
  796. cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
  797. ath9k_deinit_leds(priv);
  798. }
  799. /*******************/
  800. /* Rfkill */
  801. /*******************/
  802. static bool ath_is_rfkill_set(struct ath9k_htc_priv *priv)
  803. {
  804. return ath9k_hw_gpio_get(priv->ah, priv->ah->rfkill_gpio) ==
  805. priv->ah->rfkill_polarity;
  806. }
  807. static void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw)
  808. {
  809. struct ath9k_htc_priv *priv = hw->priv;
  810. bool blocked = !!ath_is_rfkill_set(priv);
  811. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  812. }
  813. void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv)
  814. {
  815. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  816. wiphy_rfkill_start_polling(priv->hw->wiphy);
  817. }
  818. /**********************/
  819. /* mac80211 Callbacks */
  820. /**********************/
  821. static int ath9k_htc_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  822. {
  823. struct ieee80211_hdr *hdr;
  824. struct ath9k_htc_priv *priv = hw->priv;
  825. int padpos, padsize, ret;
  826. hdr = (struct ieee80211_hdr *) skb->data;
  827. /* Add the padding after the header if this is not already done */
  828. padpos = ath9k_cmn_padpos(hdr->frame_control);
  829. padsize = padpos & 3;
  830. if (padsize && skb->len > padpos) {
  831. if (skb_headroom(skb) < padsize)
  832. return -1;
  833. skb_push(skb, padsize);
  834. memmove(skb->data, skb->data + padsize, padpos);
  835. }
  836. ret = ath9k_htc_tx_start(priv, skb);
  837. if (ret != 0) {
  838. if (ret == -ENOMEM) {
  839. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
  840. "Stopping TX queues\n");
  841. ieee80211_stop_queues(hw);
  842. spin_lock_bh(&priv->tx_lock);
  843. priv->tx_queues_stop = true;
  844. spin_unlock_bh(&priv->tx_lock);
  845. } else {
  846. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
  847. "Tx failed");
  848. }
  849. goto fail_tx;
  850. }
  851. return 0;
  852. fail_tx:
  853. dev_kfree_skb_any(skb);
  854. return 0;
  855. }
  856. static int ath9k_htc_start(struct ieee80211_hw *hw)
  857. {
  858. struct ath9k_htc_priv *priv = hw->priv;
  859. struct ath_hw *ah = priv->ah;
  860. struct ath_common *common = ath9k_hw_common(ah);
  861. struct ieee80211_channel *curchan = hw->conf.channel;
  862. struct ath9k_channel *init_channel;
  863. int ret = 0;
  864. enum htc_phymode mode;
  865. u16 htc_mode;
  866. u8 cmd_rsp;
  867. ath_print(common, ATH_DBG_CONFIG,
  868. "Starting driver with initial channel: %d MHz\n",
  869. curchan->center_freq);
  870. mutex_lock(&priv->mutex);
  871. /* setup initial channel */
  872. init_channel = ath9k_cmn_get_curchannel(hw, ah);
  873. /* Reset SERDES registers */
  874. ath9k_hw_configpcipowersave(ah, 0, 0);
  875. ath9k_hw_htc_resetinit(ah);
  876. ret = ath9k_hw_reset(ah, init_channel, false);
  877. if (ret) {
  878. ath_print(common, ATH_DBG_FATAL,
  879. "Unable to reset hardware; reset status %d "
  880. "(freq %u MHz)\n", ret, curchan->center_freq);
  881. goto mutex_unlock;
  882. }
  883. ath_update_txpow(priv);
  884. mode = ath9k_htc_get_curmode(priv, init_channel);
  885. htc_mode = cpu_to_be16(mode);
  886. WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
  887. if (ret)
  888. goto mutex_unlock;
  889. WMI_CMD(WMI_ATH_INIT_CMDID);
  890. if (ret)
  891. goto mutex_unlock;
  892. WMI_CMD(WMI_START_RECV_CMDID);
  893. if (ret)
  894. goto mutex_unlock;
  895. ath9k_host_rx_init(priv);
  896. priv->op_flags &= ~OP_INVALID;
  897. htc_start(priv->htc);
  898. spin_lock_bh(&priv->tx_lock);
  899. priv->tx_queues_stop = false;
  900. spin_unlock_bh(&priv->tx_lock);
  901. ieee80211_wake_queues(hw);
  902. mutex_unlock:
  903. mutex_unlock(&priv->mutex);
  904. return ret;
  905. }
  906. static void ath9k_htc_stop(struct ieee80211_hw *hw)
  907. {
  908. struct ath9k_htc_priv *priv = hw->priv;
  909. struct ath_hw *ah = priv->ah;
  910. struct ath_common *common = ath9k_hw_common(ah);
  911. int ret = 0;
  912. u8 cmd_rsp;
  913. mutex_lock(&priv->mutex);
  914. if (priv->op_flags & OP_INVALID) {
  915. ath_print(common, ATH_DBG_ANY, "Device not present\n");
  916. mutex_unlock(&priv->mutex);
  917. return;
  918. }
  919. htc_stop(priv->htc);
  920. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  921. WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
  922. WMI_CMD(WMI_STOP_RECV_CMDID);
  923. ath9k_hw_phy_disable(ah);
  924. ath9k_hw_disable(ah);
  925. ath9k_hw_configpcipowersave(ah, 1, 1);
  926. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  927. cancel_delayed_work_sync(&priv->ath9k_ani_work);
  928. cancel_delayed_work_sync(&priv->ath9k_aggr_work);
  929. cancel_delayed_work_sync(&priv->ath9k_led_blink_work);
  930. ath9k_led_stop_brightness(priv);
  931. skb_queue_purge(&priv->tx_queue);
  932. /* Remove monitor interface here */
  933. if (ah->opmode == NL80211_IFTYPE_MONITOR) {
  934. if (ath9k_htc_remove_monitor_interface(priv))
  935. ath_print(common, ATH_DBG_FATAL,
  936. "Unable to remove monitor interface\n");
  937. else
  938. ath_print(common, ATH_DBG_CONFIG,
  939. "Monitor interface removed\n");
  940. }
  941. priv->op_flags |= OP_INVALID;
  942. mutex_unlock(&priv->mutex);
  943. ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
  944. }
  945. static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
  946. struct ieee80211_vif *vif)
  947. {
  948. struct ath9k_htc_priv *priv = hw->priv;
  949. struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
  950. struct ath_common *common = ath9k_hw_common(priv->ah);
  951. struct ath9k_htc_target_vif hvif;
  952. int ret = 0;
  953. u8 cmd_rsp;
  954. mutex_lock(&priv->mutex);
  955. /* Only one interface for now */
  956. if (priv->nvifs > 0) {
  957. ret = -ENOBUFS;
  958. goto out;
  959. }
  960. memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
  961. memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
  962. switch (vif->type) {
  963. case NL80211_IFTYPE_STATION:
  964. hvif.opmode = cpu_to_be32(HTC_M_STA);
  965. break;
  966. case NL80211_IFTYPE_ADHOC:
  967. hvif.opmode = cpu_to_be32(HTC_M_IBSS);
  968. break;
  969. default:
  970. ath_print(common, ATH_DBG_FATAL,
  971. "Interface type %d not yet supported\n", vif->type);
  972. ret = -EOPNOTSUPP;
  973. goto out;
  974. }
  975. ath_print(common, ATH_DBG_CONFIG,
  976. "Attach a VIF of type: %d\n", vif->type);
  977. priv->ah->opmode = vif->type;
  978. /* Index starts from zero on the target */
  979. avp->index = hvif.index = priv->nvifs;
  980. hvif.rtsthreshold = cpu_to_be16(2304);
  981. WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
  982. if (ret)
  983. goto out;
  984. priv->nvifs++;
  985. /*
  986. * We need a node in target to tx mgmt frames
  987. * before association.
  988. */
  989. ret = ath9k_htc_add_station(priv, vif, NULL);
  990. if (ret)
  991. goto out;
  992. ret = ath9k_htc_update_cap_target(priv);
  993. if (ret)
  994. ath_print(common, ATH_DBG_CONFIG, "Failed to update"
  995. " capability in target \n");
  996. priv->vif = vif;
  997. out:
  998. mutex_unlock(&priv->mutex);
  999. return ret;
  1000. }
  1001. static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
  1002. struct ieee80211_vif *vif)
  1003. {
  1004. struct ath9k_htc_priv *priv = hw->priv;
  1005. struct ath_common *common = ath9k_hw_common(priv->ah);
  1006. struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
  1007. struct ath9k_htc_target_vif hvif;
  1008. int ret = 0;
  1009. u8 cmd_rsp;
  1010. ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
  1011. mutex_lock(&priv->mutex);
  1012. memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
  1013. memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
  1014. hvif.index = avp->index;
  1015. WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
  1016. priv->nvifs--;
  1017. ath9k_htc_remove_station(priv, vif, NULL);
  1018. if (vif->type == NL80211_IFTYPE_ADHOC) {
  1019. spin_lock_bh(&priv->beacon_lock);
  1020. if (priv->beacon)
  1021. dev_kfree_skb_any(priv->beacon);
  1022. priv->beacon = NULL;
  1023. spin_unlock_bh(&priv->beacon_lock);
  1024. }
  1025. priv->vif = NULL;
  1026. mutex_unlock(&priv->mutex);
  1027. }
  1028. static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
  1029. {
  1030. struct ath9k_htc_priv *priv = hw->priv;
  1031. struct ath_common *common = ath9k_hw_common(priv->ah);
  1032. struct ieee80211_conf *conf = &hw->conf;
  1033. mutex_lock(&priv->mutex);
  1034. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1035. struct ieee80211_channel *curchan = hw->conf.channel;
  1036. int pos = curchan->hw_value;
  1037. bool is_cw40 = false;
  1038. ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  1039. curchan->center_freq);
  1040. if (check_rc_update(hw, &is_cw40))
  1041. ath9k_htc_rc_update(priv, is_cw40);
  1042. ath9k_cmn_update_ichannel(hw, &priv->ah->channels[pos]);
  1043. if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
  1044. ath_print(common, ATH_DBG_FATAL,
  1045. "Unable to set channel\n");
  1046. mutex_unlock(&priv->mutex);
  1047. return -EINVAL;
  1048. }
  1049. }
  1050. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  1051. if (conf->flags & IEEE80211_CONF_MONITOR) {
  1052. if (ath9k_htc_add_monitor_interface(priv))
  1053. ath_print(common, ATH_DBG_FATAL,
  1054. "Failed to set monitor mode\n");
  1055. else
  1056. ath_print(common, ATH_DBG_CONFIG,
  1057. "HW opmode set to Monitor mode\n");
  1058. }
  1059. }
  1060. mutex_unlock(&priv->mutex);
  1061. return 0;
  1062. }
  1063. #define SUPPORTED_FILTERS \
  1064. (FIF_PROMISC_IN_BSS | \
  1065. FIF_ALLMULTI | \
  1066. FIF_CONTROL | \
  1067. FIF_PSPOLL | \
  1068. FIF_OTHER_BSS | \
  1069. FIF_BCN_PRBRESP_PROMISC | \
  1070. FIF_FCSFAIL)
  1071. static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
  1072. unsigned int changed_flags,
  1073. unsigned int *total_flags,
  1074. u64 multicast)
  1075. {
  1076. struct ath9k_htc_priv *priv = hw->priv;
  1077. u32 rfilt;
  1078. mutex_lock(&priv->mutex);
  1079. changed_flags &= SUPPORTED_FILTERS;
  1080. *total_flags &= SUPPORTED_FILTERS;
  1081. priv->rxfilter = *total_flags;
  1082. rfilt = ath9k_htc_calcrxfilter(priv);
  1083. ath9k_hw_setrxfilter(priv->ah, rfilt);
  1084. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_CONFIG,
  1085. "Set HW RX filter: 0x%x\n", rfilt);
  1086. mutex_unlock(&priv->mutex);
  1087. }
  1088. static void ath9k_htc_sta_notify(struct ieee80211_hw *hw,
  1089. struct ieee80211_vif *vif,
  1090. enum sta_notify_cmd cmd,
  1091. struct ieee80211_sta *sta)
  1092. {
  1093. struct ath9k_htc_priv *priv = hw->priv;
  1094. int ret;
  1095. switch (cmd) {
  1096. case STA_NOTIFY_ADD:
  1097. ret = ath9k_htc_add_station(priv, vif, sta);
  1098. if (!ret)
  1099. ath9k_htc_init_rate(priv, vif, sta);
  1100. break;
  1101. case STA_NOTIFY_REMOVE:
  1102. ath9k_htc_remove_station(priv, vif, sta);
  1103. break;
  1104. default:
  1105. break;
  1106. }
  1107. }
  1108. static int ath9k_htc_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1109. const struct ieee80211_tx_queue_params *params)
  1110. {
  1111. struct ath9k_htc_priv *priv = hw->priv;
  1112. struct ath_common *common = ath9k_hw_common(priv->ah);
  1113. struct ath9k_tx_queue_info qi;
  1114. int ret = 0, qnum;
  1115. if (queue >= WME_NUM_AC)
  1116. return 0;
  1117. mutex_lock(&priv->mutex);
  1118. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  1119. qi.tqi_aifs = params->aifs;
  1120. qi.tqi_cwmin = params->cw_min;
  1121. qi.tqi_cwmax = params->cw_max;
  1122. qi.tqi_burstTime = params->txop;
  1123. qnum = get_hw_qnum(queue, priv->hwq_map);
  1124. ath_print(common, ATH_DBG_CONFIG,
  1125. "Configure tx [queue/hwq] [%d/%d], "
  1126. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  1127. queue, qnum, params->aifs, params->cw_min,
  1128. params->cw_max, params->txop);
  1129. ret = ath_htc_txq_update(priv, qnum, &qi);
  1130. if (ret)
  1131. ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
  1132. mutex_unlock(&priv->mutex);
  1133. return ret;
  1134. }
  1135. static int ath9k_htc_set_key(struct ieee80211_hw *hw,
  1136. enum set_key_cmd cmd,
  1137. struct ieee80211_vif *vif,
  1138. struct ieee80211_sta *sta,
  1139. struct ieee80211_key_conf *key)
  1140. {
  1141. struct ath9k_htc_priv *priv = hw->priv;
  1142. struct ath_common *common = ath9k_hw_common(priv->ah);
  1143. int ret = 0;
  1144. if (htc_modparam_nohwcrypt)
  1145. return -ENOSPC;
  1146. mutex_lock(&priv->mutex);
  1147. ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
  1148. switch (cmd) {
  1149. case SET_KEY:
  1150. ret = ath9k_cmn_key_config(common, vif, sta, key);
  1151. if (ret >= 0) {
  1152. key->hw_key_idx = ret;
  1153. /* push IV and Michael MIC generation to stack */
  1154. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1155. if (key->alg == ALG_TKIP)
  1156. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1157. if (priv->ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  1158. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  1159. ret = 0;
  1160. }
  1161. break;
  1162. case DISABLE_KEY:
  1163. ath9k_cmn_key_delete(common, key);
  1164. break;
  1165. default:
  1166. ret = -EINVAL;
  1167. }
  1168. mutex_unlock(&priv->mutex);
  1169. return ret;
  1170. }
  1171. static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
  1172. struct ieee80211_vif *vif,
  1173. struct ieee80211_bss_conf *bss_conf,
  1174. u32 changed)
  1175. {
  1176. struct ath9k_htc_priv *priv = hw->priv;
  1177. struct ath_hw *ah = priv->ah;
  1178. struct ath_common *common = ath9k_hw_common(ah);
  1179. mutex_lock(&priv->mutex);
  1180. if (changed & BSS_CHANGED_ASSOC) {
  1181. common->curaid = bss_conf->assoc ?
  1182. bss_conf->aid : 0;
  1183. ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  1184. bss_conf->assoc);
  1185. if (bss_conf->assoc) {
  1186. priv->op_flags |= OP_ASSOCIATED;
  1187. ath_start_ani(priv);
  1188. } else {
  1189. priv->op_flags &= ~OP_ASSOCIATED;
  1190. cancel_delayed_work_sync(&priv->ath9k_ani_work);
  1191. }
  1192. }
  1193. if (changed & BSS_CHANGED_BSSID) {
  1194. /* Set BSSID */
  1195. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1196. ath9k_hw_write_associd(ah);
  1197. ath_print(common, ATH_DBG_CONFIG,
  1198. "BSSID: %pM aid: 0x%x\n",
  1199. common->curbssid, common->curaid);
  1200. }
  1201. if ((changed & BSS_CHANGED_BEACON_INT) ||
  1202. (changed & BSS_CHANGED_BEACON) ||
  1203. ((changed & BSS_CHANGED_BEACON_ENABLED) &&
  1204. bss_conf->enable_beacon)) {
  1205. priv->op_flags |= OP_ENABLE_BEACON;
  1206. ath9k_htc_beacon_config(priv, vif, bss_conf);
  1207. }
  1208. if (changed & BSS_CHANGED_BEACON)
  1209. ath9k_htc_beacon_update(priv, vif);
  1210. if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
  1211. !bss_conf->enable_beacon) {
  1212. priv->op_flags &= ~OP_ENABLE_BEACON;
  1213. ath9k_htc_beacon_config(priv, vif, bss_conf);
  1214. }
  1215. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1216. ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  1217. bss_conf->use_short_preamble);
  1218. if (bss_conf->use_short_preamble)
  1219. priv->op_flags |= OP_PREAMBLE_SHORT;
  1220. else
  1221. priv->op_flags &= ~OP_PREAMBLE_SHORT;
  1222. }
  1223. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1224. ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  1225. bss_conf->use_cts_prot);
  1226. if (bss_conf->use_cts_prot &&
  1227. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  1228. priv->op_flags |= OP_PROTECT_ENABLE;
  1229. else
  1230. priv->op_flags &= ~OP_PROTECT_ENABLE;
  1231. }
  1232. if (changed & BSS_CHANGED_ERP_SLOT) {
  1233. if (bss_conf->use_short_slot)
  1234. ah->slottime = 9;
  1235. else
  1236. ah->slottime = 20;
  1237. ath9k_hw_init_global_settings(ah);
  1238. }
  1239. mutex_unlock(&priv->mutex);
  1240. }
  1241. static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw)
  1242. {
  1243. struct ath9k_htc_priv *priv = hw->priv;
  1244. u64 tsf;
  1245. mutex_lock(&priv->mutex);
  1246. tsf = ath9k_hw_gettsf64(priv->ah);
  1247. mutex_unlock(&priv->mutex);
  1248. return tsf;
  1249. }
  1250. static void ath9k_htc_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  1251. {
  1252. struct ath9k_htc_priv *priv = hw->priv;
  1253. mutex_lock(&priv->mutex);
  1254. ath9k_hw_settsf64(priv->ah, tsf);
  1255. mutex_unlock(&priv->mutex);
  1256. }
  1257. static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw)
  1258. {
  1259. struct ath9k_htc_priv *priv = hw->priv;
  1260. mutex_lock(&priv->mutex);
  1261. ath9k_hw_reset_tsf(priv->ah);
  1262. mutex_unlock(&priv->mutex);
  1263. }
  1264. static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
  1265. struct ieee80211_vif *vif,
  1266. enum ieee80211_ampdu_mlme_action action,
  1267. struct ieee80211_sta *sta,
  1268. u16 tid, u16 *ssn)
  1269. {
  1270. struct ath9k_htc_priv *priv = hw->priv;
  1271. struct ath9k_htc_aggr_work *work = &priv->aggr_work;
  1272. struct ath9k_htc_sta *ista;
  1273. switch (action) {
  1274. case IEEE80211_AMPDU_RX_START:
  1275. break;
  1276. case IEEE80211_AMPDU_RX_STOP:
  1277. break;
  1278. case IEEE80211_AMPDU_TX_START:
  1279. case IEEE80211_AMPDU_TX_STOP:
  1280. if (!(priv->op_flags & OP_TXAGGR))
  1281. return -ENOTSUPP;
  1282. memcpy(work->sta_addr, sta->addr, ETH_ALEN);
  1283. work->hw = hw;
  1284. work->vif = vif;
  1285. work->action = action;
  1286. work->tid = tid;
  1287. ieee80211_queue_delayed_work(hw, &priv->ath9k_aggr_work, 0);
  1288. break;
  1289. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1290. ista = (struct ath9k_htc_sta *) sta->drv_priv;
  1291. ista->tid_state[tid] = AGGR_OPERATIONAL;
  1292. break;
  1293. default:
  1294. ath_print(ath9k_hw_common(priv->ah), ATH_DBG_FATAL,
  1295. "Unknown AMPDU action\n");
  1296. }
  1297. return 0;
  1298. }
  1299. static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
  1300. {
  1301. struct ath9k_htc_priv *priv = hw->priv;
  1302. mutex_lock(&priv->mutex);
  1303. spin_lock_bh(&priv->beacon_lock);
  1304. priv->op_flags |= OP_SCANNING;
  1305. spin_unlock_bh(&priv->beacon_lock);
  1306. cancel_delayed_work_sync(&priv->ath9k_ani_work);
  1307. mutex_unlock(&priv->mutex);
  1308. }
  1309. static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
  1310. {
  1311. struct ath9k_htc_priv *priv = hw->priv;
  1312. mutex_lock(&priv->mutex);
  1313. spin_lock_bh(&priv->beacon_lock);
  1314. priv->op_flags &= ~OP_SCANNING;
  1315. spin_unlock_bh(&priv->beacon_lock);
  1316. priv->op_flags |= OP_FULL_RESET;
  1317. ath_start_ani(priv);
  1318. mutex_unlock(&priv->mutex);
  1319. }
  1320. static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  1321. {
  1322. return 0;
  1323. }
  1324. static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
  1325. u8 coverage_class)
  1326. {
  1327. struct ath9k_htc_priv *priv = hw->priv;
  1328. mutex_lock(&priv->mutex);
  1329. priv->ah->coverage_class = coverage_class;
  1330. ath9k_hw_init_global_settings(priv->ah);
  1331. mutex_unlock(&priv->mutex);
  1332. }
  1333. struct ieee80211_ops ath9k_htc_ops = {
  1334. .tx = ath9k_htc_tx,
  1335. .start = ath9k_htc_start,
  1336. .stop = ath9k_htc_stop,
  1337. .add_interface = ath9k_htc_add_interface,
  1338. .remove_interface = ath9k_htc_remove_interface,
  1339. .config = ath9k_htc_config,
  1340. .configure_filter = ath9k_htc_configure_filter,
  1341. .sta_notify = ath9k_htc_sta_notify,
  1342. .conf_tx = ath9k_htc_conf_tx,
  1343. .bss_info_changed = ath9k_htc_bss_info_changed,
  1344. .set_key = ath9k_htc_set_key,
  1345. .get_tsf = ath9k_htc_get_tsf,
  1346. .set_tsf = ath9k_htc_set_tsf,
  1347. .reset_tsf = ath9k_htc_reset_tsf,
  1348. .ampdu_action = ath9k_htc_ampdu_action,
  1349. .sw_scan_start = ath9k_htc_sw_scan_start,
  1350. .sw_scan_complete = ath9k_htc_sw_scan_complete,
  1351. .set_rts_threshold = ath9k_htc_set_rts_threshold,
  1352. .rfkill_poll = ath9k_htc_rfkill_poll_state,
  1353. .set_coverage_class = ath9k_htc_set_coverage_class,
  1354. };