wl1251_rx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * Contact: Kalle Valo <kalle.valo@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include <linux/skbuff.h>
  25. #include <net/mac80211.h>
  26. #include "wl1251.h"
  27. #include "wl1251_reg.h"
  28. #include "wl1251_io.h"
  29. #include "wl1251_rx.h"
  30. #include "wl1251_cmd.h"
  31. #include "wl1251_acx.h"
  32. static void wl1251_rx_header(struct wl1251 *wl,
  33. struct wl1251_rx_descriptor *desc)
  34. {
  35. u32 rx_packet_ring_addr;
  36. rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr;
  37. if (wl->rx_current_buffer)
  38. rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
  39. wl1251_mem_read(wl, rx_packet_ring_addr, desc, sizeof(*desc));
  40. }
  41. static void wl1251_rx_status(struct wl1251 *wl,
  42. struct wl1251_rx_descriptor *desc,
  43. struct ieee80211_rx_status *status,
  44. u8 beacon)
  45. {
  46. u64 mactime;
  47. int ret;
  48. memset(status, 0, sizeof(struct ieee80211_rx_status));
  49. status->band = IEEE80211_BAND_2GHZ;
  50. status->mactime = desc->timestamp;
  51. /*
  52. * The rx status timestamp is a 32 bits value while the TSF is a
  53. * 64 bits one.
  54. * For IBSS merging, TSF is mandatory, so we have to get it
  55. * somehow, so we ask for ACX_TSF_INFO.
  56. * That could be moved to the get_tsf() hook, but unfortunately,
  57. * this one must be atomic, while our SPI routines can sleep.
  58. */
  59. if ((wl->bss_type == BSS_TYPE_IBSS) && beacon) {
  60. ret = wl1251_acx_tsf_info(wl, &mactime);
  61. if (ret == 0)
  62. status->mactime = mactime;
  63. }
  64. status->signal = desc->rssi;
  65. status->qual = (desc->rssi - WL1251_RX_MIN_RSSI) * 100 /
  66. (WL1251_RX_MAX_RSSI - WL1251_RX_MIN_RSSI);
  67. status->qual = min(status->qual, 100);
  68. status->qual = max(status->qual, 0);
  69. /*
  70. * FIXME: guessing that snr needs to be divided by two, otherwise
  71. * the values don't make any sense
  72. */
  73. status->noise = desc->rssi - desc->snr / 2;
  74. status->freq = ieee80211_channel_to_frequency(desc->channel);
  75. status->flag |= RX_FLAG_TSFT;
  76. if (desc->flags & RX_DESC_ENCRYPTION_MASK) {
  77. status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
  78. if (likely(!(desc->flags & RX_DESC_DECRYPT_FAIL)))
  79. status->flag |= RX_FLAG_DECRYPTED;
  80. if (unlikely(desc->flags & RX_DESC_MIC_FAIL))
  81. status->flag |= RX_FLAG_MMIC_ERROR;
  82. }
  83. if (unlikely(!(desc->flags & RX_DESC_VALID_FCS)))
  84. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  85. /* FIXME: set status->rate_idx */
  86. }
  87. static void wl1251_rx_body(struct wl1251 *wl,
  88. struct wl1251_rx_descriptor *desc)
  89. {
  90. struct sk_buff *skb;
  91. struct ieee80211_rx_status status;
  92. u8 *rx_buffer, beacon = 0;
  93. u16 length, *fc;
  94. u32 curr_id, last_id_inc, rx_packet_ring_addr;
  95. length = WL1251_RX_ALIGN(desc->length - PLCP_HEADER_LENGTH);
  96. curr_id = (desc->flags & RX_DESC_SEQNUM_MASK) >> RX_DESC_PACKETID_SHIFT;
  97. last_id_inc = (wl->rx_last_id + 1) % (RX_MAX_PACKET_ID + 1);
  98. if (last_id_inc != curr_id) {
  99. wl1251_warning("curr ID:%d, last ID inc:%d",
  100. curr_id, last_id_inc);
  101. wl->rx_last_id = curr_id;
  102. } else {
  103. wl->rx_last_id = last_id_inc;
  104. }
  105. rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr +
  106. sizeof(struct wl1251_rx_descriptor) + 20;
  107. if (wl->rx_current_buffer)
  108. rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
  109. skb = dev_alloc_skb(length);
  110. if (!skb) {
  111. wl1251_error("Couldn't allocate RX frame");
  112. return;
  113. }
  114. rx_buffer = skb_put(skb, length);
  115. wl1251_mem_read(wl, rx_packet_ring_addr, rx_buffer, length);
  116. /* The actual lenght doesn't include the target's alignment */
  117. skb->len = desc->length - PLCP_HEADER_LENGTH;
  118. fc = (u16 *)skb->data;
  119. if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
  120. beacon = 1;
  121. wl1251_rx_status(wl, desc, &status, beacon);
  122. wl1251_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
  123. beacon ? "beacon" : "");
  124. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  125. ieee80211_rx(wl->hw, skb);
  126. }
  127. static void wl1251_rx_ack(struct wl1251 *wl)
  128. {
  129. u32 data, addr;
  130. if (wl->rx_current_buffer) {
  131. addr = ACX_REG_INTERRUPT_TRIG_H;
  132. data = INTR_TRIG_RX_PROC1;
  133. } else {
  134. addr = ACX_REG_INTERRUPT_TRIG;
  135. data = INTR_TRIG_RX_PROC0;
  136. }
  137. wl1251_reg_write32(wl, addr, data);
  138. /* Toggle buffer ring */
  139. wl->rx_current_buffer = !wl->rx_current_buffer;
  140. }
  141. void wl1251_rx(struct wl1251 *wl)
  142. {
  143. struct wl1251_rx_descriptor *rx_desc;
  144. if (wl->state != WL1251_STATE_ON)
  145. return;
  146. rx_desc = wl->rx_descriptor;
  147. /* We first read the frame's header */
  148. wl1251_rx_header(wl, rx_desc);
  149. /* Now we can read the body */
  150. wl1251_rx_body(wl, rx_desc);
  151. /* Finally, we need to ACK the RX */
  152. wl1251_rx_ack(wl);
  153. return;
  154. }