rx.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/gfp.h>
  24. #include <linux/sched.h>
  25. #include "wl12xx.h"
  26. #include "acx.h"
  27. #include "reg.h"
  28. #include "rx.h"
  29. #include "io.h"
  30. static u8 wl1271_rx_get_mem_block(struct wl1271_fw_common_status *status,
  31. u32 drv_rx_counter)
  32. {
  33. return le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
  34. RX_MEM_BLOCK_MASK;
  35. }
  36. static u32 wl1271_rx_get_buf_size(struct wl1271_fw_common_status *status,
  37. u32 drv_rx_counter)
  38. {
  39. return (le32_to_cpu(status->rx_pkt_descs[drv_rx_counter]) &
  40. RX_BUF_SIZE_MASK) >> RX_BUF_SIZE_SHIFT_DIV;
  41. }
  42. static void wl1271_rx_status(struct wl1271 *wl,
  43. struct wl1271_rx_descriptor *desc,
  44. struct ieee80211_rx_status *status,
  45. u8 beacon)
  46. {
  47. memset(status, 0, sizeof(struct ieee80211_rx_status));
  48. if ((desc->flags & WL1271_RX_DESC_BAND_MASK) == WL1271_RX_DESC_BAND_BG)
  49. status->band = IEEE80211_BAND_2GHZ;
  50. else
  51. status->band = IEEE80211_BAND_5GHZ;
  52. status->rate_idx = wl1271_rate_to_idx(desc->rate, status->band);
  53. #ifdef CONFIG_WL12XX_HT
  54. /* 11n support */
  55. if (desc->rate <= CONF_HW_RXTX_RATE_MCS0)
  56. status->flag |= RX_FLAG_HT;
  57. #endif
  58. status->signal = desc->rssi;
  59. /*
  60. * FIXME: In wl1251, the SNR should be divided by two. In wl1271 we
  61. * need to divide by two for now, but TI has been discussing about
  62. * changing it. This needs to be rechecked.
  63. */
  64. wl->noise = desc->rssi - (desc->snr >> 1);
  65. status->freq = ieee80211_channel_to_frequency(desc->channel,
  66. status->band);
  67. if (desc->flags & WL1271_RX_DESC_ENCRYPT_MASK) {
  68. u8 desc_err_code = desc->status & WL1271_RX_DESC_STATUS_MASK;
  69. status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED |
  70. RX_FLAG_DECRYPTED;
  71. if (unlikely(desc_err_code == WL1271_RX_DESC_MIC_FAIL)) {
  72. status->flag |= RX_FLAG_MMIC_ERROR;
  73. wl1271_warning("Michael MIC error");
  74. }
  75. }
  76. }
  77. static int wl1271_rx_handle_data(struct wl1271 *wl, u8 *data, u32 length)
  78. {
  79. struct wl1271_rx_descriptor *desc;
  80. struct sk_buff *skb;
  81. struct ieee80211_hdr *hdr;
  82. u8 *buf;
  83. u8 beacon = 0;
  84. u8 is_data = 0;
  85. /*
  86. * In PLT mode we seem to get frames and mac80211 warns about them,
  87. * workaround this by not retrieving them at all.
  88. */
  89. if (unlikely(wl->state == WL1271_STATE_PLT))
  90. return -EINVAL;
  91. /* the data read starts with the descriptor */
  92. desc = (struct wl1271_rx_descriptor *) data;
  93. if (desc->packet_class == WL12XX_RX_CLASS_LOGGER) {
  94. size_t len = length - sizeof(*desc);
  95. wl12xx_copy_fwlog(wl, data + sizeof(*desc), len);
  96. wake_up_interruptible(&wl->fwlog_waitq);
  97. return 0;
  98. }
  99. switch (desc->status & WL1271_RX_DESC_STATUS_MASK) {
  100. /* discard corrupted packets */
  101. case WL1271_RX_DESC_DRIVER_RX_Q_FAIL:
  102. case WL1271_RX_DESC_DECRYPT_FAIL:
  103. wl1271_warning("corrupted packet in RX with status: 0x%x",
  104. desc->status & WL1271_RX_DESC_STATUS_MASK);
  105. return -EINVAL;
  106. case WL1271_RX_DESC_SUCCESS:
  107. case WL1271_RX_DESC_MIC_FAIL:
  108. break;
  109. default:
  110. wl1271_error("invalid RX descriptor status: 0x%x",
  111. desc->status & WL1271_RX_DESC_STATUS_MASK);
  112. return -EINVAL;
  113. }
  114. skb = __dev_alloc_skb(length, GFP_KERNEL);
  115. if (!skb) {
  116. wl1271_error("Couldn't allocate RX frame");
  117. return -ENOMEM;
  118. }
  119. buf = skb_put(skb, length);
  120. memcpy(buf, data, length);
  121. /* now we pull the descriptor out of the buffer */
  122. skb_pull(skb, sizeof(*desc));
  123. hdr = (struct ieee80211_hdr *)skb->data;
  124. if (ieee80211_is_beacon(hdr->frame_control))
  125. beacon = 1;
  126. if (ieee80211_is_data_present(hdr->frame_control))
  127. is_data = 1;
  128. wl1271_rx_status(wl, desc, IEEE80211_SKB_RXCB(skb), beacon);
  129. wl1271_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb,
  130. skb->len - desc->pad_len,
  131. beacon ? "beacon" : "");
  132. skb_trim(skb, skb->len - desc->pad_len);
  133. skb_queue_tail(&wl->deferred_rx_queue, skb);
  134. queue_work(wl->freezable_wq, &wl->netstack_work);
  135. return is_data;
  136. }
  137. void wl1271_rx(struct wl1271 *wl, struct wl1271_fw_common_status *status)
  138. {
  139. struct wl1271_acx_mem_map *wl_mem_map = wl->target_mem_map;
  140. u32 buf_size;
  141. u32 fw_rx_counter = status->fw_rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
  142. u32 drv_rx_counter = wl->rx_counter & NUM_RX_PKT_DESC_MOD_MASK;
  143. u32 rx_counter;
  144. u32 mem_block;
  145. u32 pkt_length;
  146. u32 pkt_offset;
  147. bool is_ap = (wl->bss_type == BSS_TYPE_AP_BSS);
  148. bool had_data = false;
  149. while (drv_rx_counter != fw_rx_counter) {
  150. buf_size = 0;
  151. rx_counter = drv_rx_counter;
  152. while (rx_counter != fw_rx_counter) {
  153. pkt_length = wl1271_rx_get_buf_size(status, rx_counter);
  154. if (buf_size + pkt_length > WL1271_AGGR_BUFFER_SIZE)
  155. break;
  156. buf_size += pkt_length;
  157. rx_counter++;
  158. rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
  159. }
  160. if (buf_size == 0) {
  161. wl1271_warning("received empty data");
  162. break;
  163. }
  164. if (wl->chip.id != CHIP_ID_1283_PG20) {
  165. /*
  166. * Choose the block we want to read
  167. * For aggregated packets, only the first memory block
  168. * should be retrieved. The FW takes care of the rest.
  169. */
  170. mem_block = wl1271_rx_get_mem_block(status,
  171. drv_rx_counter);
  172. wl->rx_mem_pool_addr.addr = (mem_block << 8) +
  173. le32_to_cpu(wl_mem_map->packet_memory_pool_start);
  174. wl->rx_mem_pool_addr.addr_extra =
  175. wl->rx_mem_pool_addr.addr + 4;
  176. wl1271_write(wl, WL1271_SLV_REG_DATA,
  177. &wl->rx_mem_pool_addr,
  178. sizeof(wl->rx_mem_pool_addr), false);
  179. }
  180. /* Read all available packets at once */
  181. wl1271_read(wl, WL1271_SLV_MEM_DATA, wl->aggr_buf,
  182. buf_size, true);
  183. /* Split data into separate packets */
  184. pkt_offset = 0;
  185. while (pkt_offset < buf_size) {
  186. pkt_length = wl1271_rx_get_buf_size(status,
  187. drv_rx_counter);
  188. /*
  189. * the handle data call can only fail in memory-outage
  190. * conditions, in that case the received frame will just
  191. * be dropped.
  192. */
  193. if (wl1271_rx_handle_data(wl,
  194. wl->aggr_buf + pkt_offset,
  195. pkt_length) == 1)
  196. had_data = true;
  197. wl->rx_counter++;
  198. drv_rx_counter++;
  199. drv_rx_counter &= NUM_RX_PKT_DESC_MOD_MASK;
  200. pkt_offset += pkt_length;
  201. }
  202. }
  203. /*
  204. * Write the driver's packet counter to the FW. This is only required
  205. * for older hardware revisions
  206. */
  207. if (wl->quirks & WL12XX_QUIRK_END_OF_TRANSACTION)
  208. wl1271_write32(wl, RX_DRIVER_COUNTER_ADDRESS, wl->rx_counter);
  209. if (!is_ap && wl->conf.rx_streaming.interval && had_data &&
  210. (wl->conf.rx_streaming.always ||
  211. test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags))) {
  212. u32 timeout = wl->conf.rx_streaming.duration;
  213. /* restart rx streaming */
  214. if (!test_bit(WL1271_FLAG_RX_STREAMING_STARTED, &wl->flags))
  215. ieee80211_queue_work(wl->hw,
  216. &wl->rx_streaming_enable_work);
  217. mod_timer(&wl->rx_streaming_timer,
  218. jiffies + msecs_to_jiffies(timeout));
  219. }
  220. }
  221. void wl1271_set_default_filters(struct wl1271 *wl)
  222. {
  223. if (wl->bss_type == BSS_TYPE_AP_BSS) {
  224. wl->rx_config = WL1271_DEFAULT_AP_RX_CONFIG;
  225. wl->rx_filter = WL1271_DEFAULT_AP_RX_FILTER;
  226. } else {
  227. wl->rx_config = WL1271_DEFAULT_STA_RX_CONFIG;
  228. wl->rx_filter = WL1271_DEFAULT_STA_RX_FILTER;
  229. }
  230. }