htc_drv_txrx.c 20 KB

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