htc_drv_txrx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. /******/
  18. /* TX */
  19. /******/
  20. static const int subtype_txq_to_hwq[] = {
  21. [WME_AC_BE] = ATH_TXQ_AC_BE,
  22. [WME_AC_BK] = ATH_TXQ_AC_BK,
  23. [WME_AC_VI] = ATH_TXQ_AC_VI,
  24. [WME_AC_VO] = ATH_TXQ_AC_VO,
  25. };
  26. #define ATH9K_HTC_INIT_TXQ(subtype) do { \
  27. qi.tqi_subtype = subtype_txq_to_hwq[subtype]; \
  28. qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT; \
  29. qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT; \
  30. qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT; \
  31. qi.tqi_physCompBuf = 0; \
  32. qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE | \
  33. TXQ_FLAG_TXDESCINT_ENABLE; \
  34. } while (0)
  35. int get_hw_qnum(u16 queue, int *hwq_map)
  36. {
  37. switch (queue) {
  38. case 0:
  39. return hwq_map[WME_AC_VO];
  40. case 1:
  41. return hwq_map[WME_AC_VI];
  42. case 2:
  43. return hwq_map[WME_AC_BE];
  44. case 3:
  45. return hwq_map[WME_AC_BK];
  46. default:
  47. return hwq_map[WME_AC_BE];
  48. }
  49. }
  50. void ath9k_htc_check_stop_queues(struct ath9k_htc_priv *priv)
  51. {
  52. spin_lock_bh(&priv->tx.tx_lock);
  53. priv->tx.queued_cnt++;
  54. if ((priv->tx.queued_cnt >= ATH9K_HTC_TX_THRESHOLD) &&
  55. !(priv->tx.flags & ATH9K_HTC_OP_TX_QUEUES_STOP)) {
  56. priv->tx.flags |= ATH9K_HTC_OP_TX_QUEUES_STOP;
  57. ieee80211_stop_queues(priv->hw);
  58. }
  59. spin_unlock_bh(&priv->tx.tx_lock);
  60. }
  61. void ath9k_htc_check_wake_queues(struct ath9k_htc_priv *priv)
  62. {
  63. spin_lock_bh(&priv->tx.tx_lock);
  64. if ((priv->tx.queued_cnt < ATH9K_HTC_TX_THRESHOLD) &&
  65. (priv->tx.flags & ATH9K_HTC_OP_TX_QUEUES_STOP)) {
  66. priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
  67. ieee80211_wake_queues(priv->hw);
  68. }
  69. spin_unlock_bh(&priv->tx.tx_lock);
  70. }
  71. int ath9k_htc_tx_get_slot(struct ath9k_htc_priv *priv)
  72. {
  73. int slot;
  74. spin_lock_bh(&priv->tx.tx_lock);
  75. slot = find_first_zero_bit(priv->tx.tx_slot, MAX_TX_BUF_NUM);
  76. if (slot >= MAX_TX_BUF_NUM) {
  77. spin_unlock_bh(&priv->tx.tx_lock);
  78. return -ENOBUFS;
  79. }
  80. __set_bit(slot, priv->tx.tx_slot);
  81. spin_unlock_bh(&priv->tx.tx_lock);
  82. return slot;
  83. }
  84. void ath9k_htc_tx_clear_slot(struct ath9k_htc_priv *priv, int slot)
  85. {
  86. spin_lock_bh(&priv->tx.tx_lock);
  87. __clear_bit(slot, priv->tx.tx_slot);
  88. spin_unlock_bh(&priv->tx.tx_lock);
  89. }
  90. static enum htc_endpoint_id get_htc_epid(struct ath9k_htc_priv *priv,
  91. u16 qnum)
  92. {
  93. enum htc_endpoint_id epid;
  94. switch (qnum) {
  95. case 0:
  96. TX_QSTAT_INC(WME_AC_VO);
  97. epid = priv->data_vo_ep;
  98. break;
  99. case 1:
  100. TX_QSTAT_INC(WME_AC_VI);
  101. epid = priv->data_vi_ep;
  102. break;
  103. case 2:
  104. TX_QSTAT_INC(WME_AC_BE);
  105. epid = priv->data_be_ep;
  106. break;
  107. case 3:
  108. default:
  109. TX_QSTAT_INC(WME_AC_BK);
  110. epid = priv->data_bk_ep;
  111. break;
  112. }
  113. return epid;
  114. }
  115. /*
  116. * Removes the driver header and returns the TX slot number
  117. */
  118. static inline int strip_drv_header(struct ath9k_htc_priv *priv,
  119. struct sk_buff *skb)
  120. {
  121. struct ath_common *common = ath9k_hw_common(priv->ah);
  122. struct ath9k_htc_tx_ctl *tx_ctl;
  123. int slot;
  124. tx_ctl = HTC_SKB_CB(skb);
  125. if (tx_ctl->epid == priv->mgmt_ep) {
  126. struct tx_mgmt_hdr *tx_mhdr =
  127. (struct tx_mgmt_hdr *)skb->data;
  128. slot = tx_mhdr->cookie;
  129. skb_pull(skb, sizeof(struct tx_mgmt_hdr));
  130. } else if ((tx_ctl->epid == priv->data_bk_ep) ||
  131. (tx_ctl->epid == priv->data_be_ep) ||
  132. (tx_ctl->epid == priv->data_vi_ep) ||
  133. (tx_ctl->epid == priv->data_vo_ep) ||
  134. (tx_ctl->epid == priv->cab_ep)) {
  135. struct tx_frame_hdr *tx_fhdr =
  136. (struct tx_frame_hdr *)skb->data;
  137. slot = tx_fhdr->cookie;
  138. skb_pull(skb, sizeof(struct tx_frame_hdr));
  139. } else {
  140. ath_err(common, "Unsupported EPID: %d\n", tx_ctl->epid);
  141. slot = -EINVAL;
  142. }
  143. return slot;
  144. }
  145. int ath_htc_txq_update(struct ath9k_htc_priv *priv, int qnum,
  146. struct ath9k_tx_queue_info *qinfo)
  147. {
  148. struct ath_hw *ah = priv->ah;
  149. int error = 0;
  150. struct ath9k_tx_queue_info qi;
  151. ath9k_hw_get_txq_props(ah, qnum, &qi);
  152. qi.tqi_aifs = qinfo->tqi_aifs;
  153. qi.tqi_cwmin = qinfo->tqi_cwmin / 2; /* XXX */
  154. qi.tqi_cwmax = qinfo->tqi_cwmax;
  155. qi.tqi_burstTime = qinfo->tqi_burstTime;
  156. qi.tqi_readyTime = qinfo->tqi_readyTime;
  157. if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
  158. ath_err(ath9k_hw_common(ah),
  159. "Unable to update hardware queue %u!\n", qnum);
  160. error = -EIO;
  161. } else {
  162. ath9k_hw_resettxqueue(ah, qnum);
  163. }
  164. return error;
  165. }
  166. int ath9k_htc_tx_start(struct ath9k_htc_priv *priv,
  167. struct sk_buff *skb,
  168. u8 slot, bool is_cab)
  169. {
  170. struct ieee80211_hdr *hdr;
  171. struct ieee80211_mgmt *mgmt;
  172. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  173. struct ieee80211_sta *sta = tx_info->control.sta;
  174. struct ieee80211_vif *vif = tx_info->control.vif;
  175. struct ath9k_htc_sta *ista;
  176. struct ath9k_htc_vif *avp = NULL;
  177. struct ath9k_htc_tx_ctl *tx_ctl;
  178. u16 qnum;
  179. __le16 fc;
  180. u8 *tx_fhdr;
  181. u8 sta_idx, vif_idx;
  182. tx_ctl = HTC_SKB_CB(skb);
  183. memset(tx_ctl, 0, sizeof(*tx_ctl));
  184. hdr = (struct ieee80211_hdr *) skb->data;
  185. fc = hdr->frame_control;
  186. /*
  187. * Find out on which interface this packet has to be
  188. * sent out.
  189. */
  190. if (vif) {
  191. avp = (struct ath9k_htc_vif *) vif->drv_priv;
  192. vif_idx = avp->index;
  193. } else {
  194. if (!priv->ah->is_monitoring) {
  195. ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
  196. "VIF is null, but no monitor interface !\n");
  197. return -EINVAL;
  198. }
  199. vif_idx = priv->mon_vif_idx;
  200. }
  201. /*
  202. * Find out which station this packet is destined for.
  203. */
  204. if (sta) {
  205. ista = (struct ath9k_htc_sta *) sta->drv_priv;
  206. sta_idx = ista->index;
  207. } else {
  208. sta_idx = priv->vif_sta_pos[vif_idx];
  209. }
  210. if (ieee80211_is_data(fc)) {
  211. struct tx_frame_hdr tx_hdr;
  212. u32 flags = 0;
  213. u8 *qc;
  214. memset(&tx_hdr, 0, sizeof(struct tx_frame_hdr));
  215. tx_hdr.node_idx = sta_idx;
  216. tx_hdr.vif_idx = vif_idx;
  217. tx_hdr.cookie = slot;
  218. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  219. tx_ctl->type = ATH9K_HTC_AMPDU;
  220. tx_hdr.data_type = ATH9K_HTC_AMPDU;
  221. } else {
  222. tx_ctl->type = ATH9K_HTC_NORMAL;
  223. tx_hdr.data_type = ATH9K_HTC_NORMAL;
  224. }
  225. if (ieee80211_is_data_qos(fc)) {
  226. qc = ieee80211_get_qos_ctl(hdr);
  227. tx_hdr.tidno = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  228. }
  229. /* Check for RTS protection */
  230. if (priv->hw->wiphy->rts_threshold != (u32) -1)
  231. if (skb->len > priv->hw->wiphy->rts_threshold)
  232. flags |= ATH9K_HTC_TX_RTSCTS;
  233. /* CTS-to-self */
  234. if (!(flags & ATH9K_HTC_TX_RTSCTS) &&
  235. (vif && vif->bss_conf.use_cts_prot))
  236. flags |= ATH9K_HTC_TX_CTSONLY;
  237. tx_hdr.flags = cpu_to_be32(flags);
  238. tx_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
  239. if (tx_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
  240. tx_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
  241. else
  242. tx_hdr.keyix = tx_info->control.hw_key->hw_key_idx;
  243. tx_fhdr = skb_push(skb, sizeof(tx_hdr));
  244. memcpy(tx_fhdr, (u8 *) &tx_hdr, sizeof(tx_hdr));
  245. if (is_cab) {
  246. CAB_STAT_INC;
  247. tx_ctl->epid = priv->cab_ep;
  248. goto send;
  249. }
  250. qnum = skb_get_queue_mapping(skb);
  251. tx_ctl->epid = get_htc_epid(priv, qnum);
  252. } else {
  253. struct tx_mgmt_hdr mgmt_hdr;
  254. memset(&mgmt_hdr, 0, sizeof(struct tx_mgmt_hdr));
  255. /*
  256. * Set the TSF adjust value for probe response
  257. * frame also.
  258. */
  259. if (avp && unlikely(ieee80211_is_probe_resp(fc))) {
  260. mgmt = (struct ieee80211_mgmt *)skb->data;
  261. mgmt->u.probe_resp.timestamp = avp->tsfadjust;
  262. }
  263. tx_ctl->type = ATH9K_HTC_MGMT;
  264. mgmt_hdr.node_idx = sta_idx;
  265. mgmt_hdr.vif_idx = vif_idx;
  266. mgmt_hdr.tidno = 0;
  267. mgmt_hdr.flags = 0;
  268. mgmt_hdr.cookie = slot;
  269. mgmt_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
  270. if (mgmt_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
  271. mgmt_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
  272. else
  273. mgmt_hdr.keyix = tx_info->control.hw_key->hw_key_idx;
  274. tx_fhdr = skb_push(skb, sizeof(mgmt_hdr));
  275. memcpy(tx_fhdr, (u8 *) &mgmt_hdr, sizeof(mgmt_hdr));
  276. tx_ctl->epid = priv->mgmt_ep;
  277. }
  278. send:
  279. return htc_send(priv->htc, skb);
  280. }
  281. static bool ath9k_htc_check_tx_aggr(struct ath9k_htc_priv *priv,
  282. struct ath9k_htc_sta *ista, u8 tid)
  283. {
  284. bool ret = false;
  285. spin_lock_bh(&priv->tx.tx_lock);
  286. if ((tid < ATH9K_HTC_MAX_TID) && (ista->tid_state[tid] == AGGR_STOP))
  287. ret = true;
  288. spin_unlock_bh(&priv->tx.tx_lock);
  289. return ret;
  290. }
  291. void ath9k_tx_tasklet(unsigned long data)
  292. {
  293. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
  294. struct ath9k_htc_tx_ctl *tx_ctl;
  295. struct ieee80211_vif *vif;
  296. struct ieee80211_sta *sta;
  297. struct ieee80211_hdr *hdr;
  298. struct ieee80211_tx_info *tx_info;
  299. struct sk_buff *skb = NULL;
  300. __le16 fc;
  301. bool txok;
  302. int slot;
  303. while ((skb = skb_dequeue(&priv->tx.tx_queue)) != NULL) {
  304. slot = strip_drv_header(priv, skb);
  305. if (slot < 0) {
  306. dev_kfree_skb_any(skb);
  307. continue;
  308. }
  309. tx_ctl = HTC_SKB_CB(skb);
  310. hdr = (struct ieee80211_hdr *) skb->data;
  311. fc = hdr->frame_control;
  312. tx_info = IEEE80211_SKB_CB(skb);
  313. vif = tx_info->control.vif;
  314. txok = tx_ctl->txok;
  315. memset(&tx_info->status, 0, sizeof(tx_info->status));
  316. /*
  317. * URB submission failed for this frame, it never reached
  318. * the target.
  319. */
  320. if (!txok)
  321. goto send_mac80211;
  322. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  323. if (!vif)
  324. goto send_mac80211;
  325. rcu_read_lock();
  326. sta = ieee80211_find_sta(vif, hdr->addr1);
  327. if (!sta) {
  328. rcu_read_unlock();
  329. goto send_mac80211;
  330. }
  331. /* Check if we need to start aggregation */
  332. if (sta && conf_is_ht(&priv->hw->conf) &&
  333. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  334. if (ieee80211_is_data_qos(fc)) {
  335. u8 *qc, tid;
  336. struct ath9k_htc_sta *ista;
  337. qc = ieee80211_get_qos_ctl(hdr);
  338. tid = qc[0] & 0xf;
  339. ista = (struct ath9k_htc_sta *)sta->drv_priv;
  340. if (ath9k_htc_check_tx_aggr(priv, ista, tid)) {
  341. ieee80211_start_tx_ba_session(sta, tid, 0);
  342. spin_lock_bh(&priv->tx.tx_lock);
  343. ista->tid_state[tid] = AGGR_PROGRESS;
  344. spin_unlock_bh(&priv->tx.tx_lock);
  345. }
  346. }
  347. }
  348. rcu_read_unlock();
  349. send_mac80211:
  350. spin_lock_bh(&priv->tx.tx_lock);
  351. if (WARN_ON(--priv->tx.queued_cnt < 0))
  352. priv->tx.queued_cnt = 0;
  353. spin_unlock_bh(&priv->tx.tx_lock);
  354. ath9k_htc_tx_clear_slot(priv, slot);
  355. /* Send status to mac80211 */
  356. ieee80211_tx_status(priv->hw, skb);
  357. }
  358. /* Wake TX queues if needed */
  359. ath9k_htc_check_wake_queues(priv);
  360. }
  361. void ath9k_htc_txep(void *drv_priv, struct sk_buff *skb,
  362. enum htc_endpoint_id ep_id, bool txok)
  363. {
  364. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) drv_priv;
  365. struct ath9k_htc_tx_ctl *tx_ctl;
  366. tx_ctl = HTC_SKB_CB(skb);
  367. tx_ctl->txok = txok;
  368. skb_queue_tail(&priv->tx.tx_queue, skb);
  369. tasklet_schedule(&priv->tx_tasklet);
  370. }
  371. int ath9k_tx_init(struct ath9k_htc_priv *priv)
  372. {
  373. skb_queue_head_init(&priv->tx.tx_queue);
  374. return 0;
  375. }
  376. void ath9k_tx_cleanup(struct ath9k_htc_priv *priv)
  377. {
  378. }
  379. bool ath9k_htc_txq_setup(struct ath9k_htc_priv *priv, int subtype)
  380. {
  381. struct ath_hw *ah = priv->ah;
  382. struct ath_common *common = ath9k_hw_common(ah);
  383. struct ath9k_tx_queue_info qi;
  384. int qnum;
  385. memset(&qi, 0, sizeof(qi));
  386. ATH9K_HTC_INIT_TXQ(subtype);
  387. qnum = ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_DATA, &qi);
  388. if (qnum == -1)
  389. return false;
  390. if (qnum >= ARRAY_SIZE(priv->hwq_map)) {
  391. ath_err(common, "qnum %u out of range, max %zu!\n",
  392. qnum, ARRAY_SIZE(priv->hwq_map));
  393. ath9k_hw_releasetxqueue(ah, qnum);
  394. return false;
  395. }
  396. priv->hwq_map[subtype] = qnum;
  397. return true;
  398. }
  399. int ath9k_htc_cabq_setup(struct ath9k_htc_priv *priv)
  400. {
  401. struct ath9k_tx_queue_info qi;
  402. memset(&qi, 0, sizeof(qi));
  403. ATH9K_HTC_INIT_TXQ(0);
  404. return ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_CAB, &qi);
  405. }
  406. /******/
  407. /* RX */
  408. /******/
  409. /*
  410. * Calculate the RX filter to be set in the HW.
  411. */
  412. u32 ath9k_htc_calcrxfilter(struct ath9k_htc_priv *priv)
  413. {
  414. #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
  415. struct ath_hw *ah = priv->ah;
  416. u32 rfilt;
  417. rfilt = (ath9k_hw_getrxfilter(ah) & RX_FILTER_PRESERVE)
  418. | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  419. | ATH9K_RX_FILTER_MCAST;
  420. if (priv->rxfilter & FIF_PROBE_REQ)
  421. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  422. /*
  423. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  424. * mode interface or when in monitor mode. AP mode does not need this
  425. * since it receives all in-BSS frames anyway.
  426. */
  427. if (((ah->opmode != NL80211_IFTYPE_AP) &&
  428. (priv->rxfilter & FIF_PROMISC_IN_BSS)) ||
  429. ah->is_monitoring)
  430. rfilt |= ATH9K_RX_FILTER_PROM;
  431. if (priv->rxfilter & FIF_CONTROL)
  432. rfilt |= ATH9K_RX_FILTER_CONTROL;
  433. if ((ah->opmode == NL80211_IFTYPE_STATION) &&
  434. !(priv->rxfilter & FIF_BCN_PRBRESP_PROMISC))
  435. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  436. else
  437. rfilt |= ATH9K_RX_FILTER_BEACON;
  438. if (conf_is_ht(&priv->hw->conf)) {
  439. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  440. rfilt |= ATH9K_RX_FILTER_UNCOMP_BA_BAR;
  441. }
  442. if (priv->rxfilter & FIF_PSPOLL)
  443. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  444. return rfilt;
  445. #undef RX_FILTER_PRESERVE
  446. }
  447. /*
  448. * Recv initialization for opmode change.
  449. */
  450. static void ath9k_htc_opmode_init(struct ath9k_htc_priv *priv)
  451. {
  452. struct ath_hw *ah = priv->ah;
  453. u32 rfilt, mfilt[2];
  454. /* configure rx filter */
  455. rfilt = ath9k_htc_calcrxfilter(priv);
  456. ath9k_hw_setrxfilter(ah, rfilt);
  457. /* calculate and install multicast filter */
  458. mfilt[0] = mfilt[1] = ~0;
  459. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  460. }
  461. void ath9k_host_rx_init(struct ath9k_htc_priv *priv)
  462. {
  463. ath9k_hw_rxena(priv->ah);
  464. ath9k_htc_opmode_init(priv);
  465. ath9k_hw_startpcureceive(priv->ah, (priv->op_flags & OP_SCANNING));
  466. priv->rx.last_rssi = ATH_RSSI_DUMMY_MARKER;
  467. }
  468. static void ath9k_process_rate(struct ieee80211_hw *hw,
  469. struct ieee80211_rx_status *rxs,
  470. u8 rx_rate, u8 rs_flags)
  471. {
  472. struct ieee80211_supported_band *sband;
  473. enum ieee80211_band band;
  474. unsigned int i = 0;
  475. if (rx_rate & 0x80) {
  476. /* HT rate */
  477. rxs->flag |= RX_FLAG_HT;
  478. if (rs_flags & ATH9K_RX_2040)
  479. rxs->flag |= RX_FLAG_40MHZ;
  480. if (rs_flags & ATH9K_RX_GI)
  481. rxs->flag |= RX_FLAG_SHORT_GI;
  482. rxs->rate_idx = rx_rate & 0x7f;
  483. return;
  484. }
  485. band = hw->conf.channel->band;
  486. sband = hw->wiphy->bands[band];
  487. for (i = 0; i < sband->n_bitrates; i++) {
  488. if (sband->bitrates[i].hw_value == rx_rate) {
  489. rxs->rate_idx = i;
  490. return;
  491. }
  492. if (sband->bitrates[i].hw_value_short == rx_rate) {
  493. rxs->rate_idx = i;
  494. rxs->flag |= RX_FLAG_SHORTPRE;
  495. return;
  496. }
  497. }
  498. }
  499. static bool ath9k_rx_prepare(struct ath9k_htc_priv *priv,
  500. struct ath9k_htc_rxbuf *rxbuf,
  501. struct ieee80211_rx_status *rx_status)
  502. {
  503. struct ieee80211_hdr *hdr;
  504. struct ieee80211_hw *hw = priv->hw;
  505. struct sk_buff *skb = rxbuf->skb;
  506. struct ath_common *common = ath9k_hw_common(priv->ah);
  507. struct ath_htc_rx_status *rxstatus;
  508. int hdrlen, padpos, padsize;
  509. int last_rssi = ATH_RSSI_DUMMY_MARKER;
  510. __le16 fc;
  511. if (skb->len < HTC_RX_FRAME_HEADER_SIZE) {
  512. ath_err(common, "Corrupted RX frame, dropping (len: %d)\n",
  513. skb->len);
  514. goto rx_next;
  515. }
  516. rxstatus = (struct ath_htc_rx_status *)skb->data;
  517. if (be16_to_cpu(rxstatus->rs_datalen) -
  518. (skb->len - HTC_RX_FRAME_HEADER_SIZE) != 0) {
  519. ath_err(common,
  520. "Corrupted RX data len, dropping (dlen: %d, skblen: %d)\n",
  521. rxstatus->rs_datalen, skb->len);
  522. goto rx_next;
  523. }
  524. ath9k_htc_err_stat_rx(priv, rxstatus);
  525. /* Get the RX status information */
  526. memcpy(&rxbuf->rxstatus, rxstatus, HTC_RX_FRAME_HEADER_SIZE);
  527. skb_pull(skb, HTC_RX_FRAME_HEADER_SIZE);
  528. hdr = (struct ieee80211_hdr *)skb->data;
  529. fc = hdr->frame_control;
  530. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  531. padpos = ath9k_cmn_padpos(fc);
  532. padsize = padpos & 3;
  533. if (padsize && skb->len >= padpos+padsize+FCS_LEN) {
  534. memmove(skb->data + padsize, skb->data, padpos);
  535. skb_pull(skb, padsize);
  536. }
  537. memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
  538. if (rxbuf->rxstatus.rs_status != 0) {
  539. if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_CRC)
  540. rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
  541. if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_PHY)
  542. goto rx_next;
  543. if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT) {
  544. /* FIXME */
  545. } else if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_MIC) {
  546. if (ieee80211_is_ctl(fc))
  547. /*
  548. * Sometimes, we get invalid
  549. * MIC failures on valid control frames.
  550. * Remove these mic errors.
  551. */
  552. rxbuf->rxstatus.rs_status &= ~ATH9K_RXERR_MIC;
  553. else
  554. rx_status->flag |= RX_FLAG_MMIC_ERROR;
  555. }
  556. /*
  557. * Reject error frames with the exception of
  558. * decryption and MIC failures. For monitor mode,
  559. * we also ignore the CRC error.
  560. */
  561. if (priv->ah->opmode == NL80211_IFTYPE_MONITOR) {
  562. if (rxbuf->rxstatus.rs_status &
  563. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  564. ATH9K_RXERR_CRC))
  565. goto rx_next;
  566. } else {
  567. if (rxbuf->rxstatus.rs_status &
  568. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
  569. goto rx_next;
  570. }
  571. }
  572. }
  573. if (!(rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT)) {
  574. u8 keyix;
  575. keyix = rxbuf->rxstatus.rs_keyix;
  576. if (keyix != ATH9K_RXKEYIX_INVALID) {
  577. rx_status->flag |= RX_FLAG_DECRYPTED;
  578. } else if (ieee80211_has_protected(fc) &&
  579. skb->len >= hdrlen + 4) {
  580. keyix = skb->data[hdrlen + 3] >> 6;
  581. if (test_bit(keyix, common->keymap))
  582. rx_status->flag |= RX_FLAG_DECRYPTED;
  583. }
  584. }
  585. ath9k_process_rate(hw, rx_status, rxbuf->rxstatus.rs_rate,
  586. rxbuf->rxstatus.rs_flags);
  587. if (rxbuf->rxstatus.rs_rssi != ATH9K_RSSI_BAD &&
  588. !rxbuf->rxstatus.rs_moreaggr)
  589. ATH_RSSI_LPF(priv->rx.last_rssi,
  590. rxbuf->rxstatus.rs_rssi);
  591. last_rssi = priv->rx.last_rssi;
  592. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  593. rxbuf->rxstatus.rs_rssi = ATH_EP_RND(last_rssi,
  594. ATH_RSSI_EP_MULTIPLIER);
  595. if (rxbuf->rxstatus.rs_rssi < 0)
  596. rxbuf->rxstatus.rs_rssi = 0;
  597. if (ieee80211_is_beacon(fc))
  598. priv->ah->stats.avgbrssi = rxbuf->rxstatus.rs_rssi;
  599. rx_status->mactime = be64_to_cpu(rxbuf->rxstatus.rs_tstamp);
  600. rx_status->band = hw->conf.channel->band;
  601. rx_status->freq = hw->conf.channel->center_freq;
  602. rx_status->signal = rxbuf->rxstatus.rs_rssi + ATH_DEFAULT_NOISE_FLOOR;
  603. rx_status->antenna = rxbuf->rxstatus.rs_antenna;
  604. rx_status->flag |= RX_FLAG_MACTIME_MPDU;
  605. return true;
  606. rx_next:
  607. return false;
  608. }
  609. /*
  610. * FIXME: Handle FLUSH later on.
  611. */
  612. void ath9k_rx_tasklet(unsigned long data)
  613. {
  614. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
  615. struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
  616. struct ieee80211_rx_status rx_status;
  617. struct sk_buff *skb;
  618. unsigned long flags;
  619. struct ieee80211_hdr *hdr;
  620. do {
  621. spin_lock_irqsave(&priv->rx.rxbuflock, flags);
  622. list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
  623. if (tmp_buf->in_process) {
  624. rxbuf = tmp_buf;
  625. break;
  626. }
  627. }
  628. if (rxbuf == NULL) {
  629. spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
  630. break;
  631. }
  632. if (!rxbuf->skb)
  633. goto requeue;
  634. if (!ath9k_rx_prepare(priv, rxbuf, &rx_status)) {
  635. dev_kfree_skb_any(rxbuf->skb);
  636. goto requeue;
  637. }
  638. memcpy(IEEE80211_SKB_RXCB(rxbuf->skb), &rx_status,
  639. sizeof(struct ieee80211_rx_status));
  640. skb = rxbuf->skb;
  641. hdr = (struct ieee80211_hdr *) skb->data;
  642. if (ieee80211_is_beacon(hdr->frame_control) && priv->ps_enabled)
  643. ieee80211_queue_work(priv->hw, &priv->ps_work);
  644. spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
  645. ieee80211_rx(priv->hw, skb);
  646. spin_lock_irqsave(&priv->rx.rxbuflock, flags);
  647. requeue:
  648. rxbuf->in_process = false;
  649. rxbuf->skb = NULL;
  650. list_move_tail(&rxbuf->list, &priv->rx.rxbuf);
  651. rxbuf = NULL;
  652. spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
  653. } while (1);
  654. }
  655. void ath9k_htc_rxep(void *drv_priv, struct sk_buff *skb,
  656. enum htc_endpoint_id ep_id)
  657. {
  658. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)drv_priv;
  659. struct ath_hw *ah = priv->ah;
  660. struct ath_common *common = ath9k_hw_common(ah);
  661. struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
  662. spin_lock(&priv->rx.rxbuflock);
  663. list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
  664. if (!tmp_buf->in_process) {
  665. rxbuf = tmp_buf;
  666. break;
  667. }
  668. }
  669. spin_unlock(&priv->rx.rxbuflock);
  670. if (rxbuf == NULL) {
  671. ath_dbg(common, ATH_DBG_ANY,
  672. "No free RX buffer\n");
  673. goto err;
  674. }
  675. spin_lock(&priv->rx.rxbuflock);
  676. rxbuf->skb = skb;
  677. rxbuf->in_process = true;
  678. spin_unlock(&priv->rx.rxbuflock);
  679. tasklet_schedule(&priv->rx_tasklet);
  680. return;
  681. err:
  682. dev_kfree_skb_any(skb);
  683. }
  684. /* FIXME: Locking for cleanup/init */
  685. void ath9k_rx_cleanup(struct ath9k_htc_priv *priv)
  686. {
  687. struct ath9k_htc_rxbuf *rxbuf, *tbuf;
  688. list_for_each_entry_safe(rxbuf, tbuf, &priv->rx.rxbuf, list) {
  689. list_del(&rxbuf->list);
  690. if (rxbuf->skb)
  691. dev_kfree_skb_any(rxbuf->skb);
  692. kfree(rxbuf);
  693. }
  694. }
  695. int ath9k_rx_init(struct ath9k_htc_priv *priv)
  696. {
  697. struct ath_hw *ah = priv->ah;
  698. struct ath_common *common = ath9k_hw_common(ah);
  699. struct ath9k_htc_rxbuf *rxbuf;
  700. int i = 0;
  701. INIT_LIST_HEAD(&priv->rx.rxbuf);
  702. spin_lock_init(&priv->rx.rxbuflock);
  703. for (i = 0; i < ATH9K_HTC_RXBUF; i++) {
  704. rxbuf = kzalloc(sizeof(struct ath9k_htc_rxbuf), GFP_KERNEL);
  705. if (rxbuf == NULL) {
  706. ath_err(common, "Unable to allocate RX buffers\n");
  707. goto err;
  708. }
  709. list_add_tail(&rxbuf->list, &priv->rx.rxbuf);
  710. }
  711. return 0;
  712. err:
  713. ath9k_rx_cleanup(priv);
  714. return -ENOMEM;
  715. }