rx.c 5.2 KB

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