rx.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (c) 1998-2007 Texas Instruments Incorporated
  5. * Copyright (C) 2008 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #include <linux/skbuff.h>
  23. #include <linux/gfp.h>
  24. #include <net/mac80211.h>
  25. #include "wl1251.h"
  26. #include "reg.h"
  27. #include "io.h"
  28. #include "rx.h"
  29. #include "cmd.h"
  30. #include "acx.h"
  31. static void wl1251_rx_header(struct wl1251 *wl,
  32. struct wl1251_rx_descriptor *desc)
  33. {
  34. u32 rx_packet_ring_addr;
  35. rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr;
  36. if (wl->rx_current_buffer)
  37. rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
  38. wl1251_mem_read(wl, rx_packet_ring_addr, desc, sizeof(*desc));
  39. }
  40. static void wl1251_rx_status(struct wl1251 *wl,
  41. struct wl1251_rx_descriptor *desc,
  42. struct ieee80211_rx_status *status,
  43. u8 beacon)
  44. {
  45. u64 mactime;
  46. int ret;
  47. memset(status, 0, sizeof(struct ieee80211_rx_status));
  48. status->band = IEEE80211_BAND_2GHZ;
  49. status->mactime = desc->timestamp;
  50. /*
  51. * The rx status timestamp is a 32 bits value while the TSF is a
  52. * 64 bits one.
  53. * For IBSS merging, TSF is mandatory, so we have to get it
  54. * somehow, so we ask for ACX_TSF_INFO.
  55. * That could be moved to the get_tsf() hook, but unfortunately,
  56. * this one must be atomic, while our SPI routines can sleep.
  57. */
  58. if ((wl->bss_type == BSS_TYPE_IBSS) && beacon) {
  59. ret = wl1251_acx_tsf_info(wl, &mactime);
  60. if (ret == 0)
  61. status->mactime = mactime;
  62. }
  63. status->signal = desc->rssi;
  64. /*
  65. * FIXME: guessing that snr needs to be divided by two, otherwise
  66. * the values don't make any sense
  67. */
  68. wl->noise = desc->rssi - desc->snr / 2;
  69. status->freq = ieee80211_channel_to_frequency(desc->channel,
  70. status->band);
  71. status->flag |= RX_FLAG_MACTIME_START;
  72. if (!wl->monitor_present && (desc->flags & RX_DESC_ENCRYPTION_MASK)) {
  73. status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
  74. if (likely(!(desc->flags & RX_DESC_DECRYPT_FAIL)))
  75. status->flag |= RX_FLAG_DECRYPTED;
  76. if (unlikely(desc->flags & RX_DESC_MIC_FAIL))
  77. status->flag |= RX_FLAG_MMIC_ERROR;
  78. }
  79. if (unlikely(!(desc->flags & RX_DESC_VALID_FCS)))
  80. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  81. switch (desc->rate) {
  82. /* skip 1 and 12 Mbps because they have same value 0x0a */
  83. case RATE_2MBPS:
  84. status->rate_idx = 1;
  85. break;
  86. case RATE_5_5MBPS:
  87. status->rate_idx = 2;
  88. break;
  89. case RATE_11MBPS:
  90. status->rate_idx = 3;
  91. break;
  92. case RATE_6MBPS:
  93. status->rate_idx = 4;
  94. break;
  95. case RATE_9MBPS:
  96. status->rate_idx = 5;
  97. break;
  98. case RATE_18MBPS:
  99. status->rate_idx = 7;
  100. break;
  101. case RATE_24MBPS:
  102. status->rate_idx = 8;
  103. break;
  104. case RATE_36MBPS:
  105. status->rate_idx = 9;
  106. break;
  107. case RATE_48MBPS:
  108. status->rate_idx = 10;
  109. break;
  110. case RATE_54MBPS:
  111. status->rate_idx = 11;
  112. break;
  113. }
  114. /* for 1 and 12 Mbps we have to check the modulation */
  115. if (desc->rate == RATE_1MBPS) {
  116. if (!(desc->mod_pre & OFDM_RATE_BIT))
  117. /* CCK -> RATE_1MBPS */
  118. status->rate_idx = 0;
  119. else
  120. /* OFDM -> RATE_12MBPS */
  121. status->rate_idx = 6;
  122. }
  123. if (desc->mod_pre & SHORT_PREAMBLE_BIT)
  124. status->flag |= RX_FLAG_SHORTPRE;
  125. }
  126. static void wl1251_rx_body(struct wl1251 *wl,
  127. struct wl1251_rx_descriptor *desc)
  128. {
  129. struct sk_buff *skb;
  130. struct ieee80211_rx_status status;
  131. u8 *rx_buffer, beacon = 0;
  132. u16 length, *fc;
  133. u32 curr_id, last_id_inc, rx_packet_ring_addr;
  134. length = WL1251_RX_ALIGN(desc->length - PLCP_HEADER_LENGTH);
  135. curr_id = (desc->flags & RX_DESC_SEQNUM_MASK) >> RX_DESC_PACKETID_SHIFT;
  136. last_id_inc = (wl->rx_last_id + 1) % (RX_MAX_PACKET_ID + 1);
  137. if (last_id_inc != curr_id) {
  138. wl1251_warning("curr ID:%d, last ID inc:%d",
  139. curr_id, last_id_inc);
  140. wl->rx_last_id = curr_id;
  141. } else {
  142. wl->rx_last_id = last_id_inc;
  143. }
  144. rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr +
  145. sizeof(struct wl1251_rx_descriptor) + 20;
  146. if (wl->rx_current_buffer)
  147. rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
  148. skb = __dev_alloc_skb(length, GFP_KERNEL);
  149. if (!skb) {
  150. wl1251_error("Couldn't allocate RX frame");
  151. return;
  152. }
  153. rx_buffer = skb_put(skb, length);
  154. wl1251_mem_read(wl, rx_packet_ring_addr, rx_buffer, length);
  155. /* The actual length doesn't include the target's alignment */
  156. skb_trim(skb, desc->length - PLCP_HEADER_LENGTH);
  157. fc = (u16 *)skb->data;
  158. if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
  159. beacon = 1;
  160. wl1251_rx_status(wl, desc, &status, beacon);
  161. wl1251_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
  162. beacon ? "beacon" : "");
  163. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  164. ieee80211_rx_ni(wl->hw, skb);
  165. }
  166. static void wl1251_rx_ack(struct wl1251 *wl)
  167. {
  168. u32 data, addr;
  169. if (wl->rx_current_buffer) {
  170. addr = ACX_REG_INTERRUPT_TRIG_H;
  171. data = INTR_TRIG_RX_PROC1;
  172. } else {
  173. addr = ACX_REG_INTERRUPT_TRIG;
  174. data = INTR_TRIG_RX_PROC0;
  175. }
  176. wl1251_reg_write32(wl, addr, data);
  177. /* Toggle buffer ring */
  178. wl->rx_current_buffer = !wl->rx_current_buffer;
  179. }
  180. void wl1251_rx(struct wl1251 *wl)
  181. {
  182. struct wl1251_rx_descriptor *rx_desc;
  183. if (wl->state != WL1251_STATE_ON)
  184. return;
  185. rx_desc = wl->rx_descriptor;
  186. /* We first read the frame's header */
  187. wl1251_rx_header(wl, rx_desc);
  188. /* Now we can read the body */
  189. wl1251_rx_body(wl, rx_desc);
  190. /* Finally, we need to ACK the RX */
  191. wl1251_rx_ack(wl);
  192. }