rx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * This file is part of wl12xx
  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 "wl12xx.h"
  27. #include "reg.h"
  28. #include "spi.h"
  29. #include "rx.h"
  30. static void wl12xx_rx_header(struct wl12xx *wl,
  31. struct wl12xx_rx_descriptor *desc)
  32. {
  33. u32 rx_packet_ring_addr;
  34. rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr;
  35. if (wl->rx_current_buffer)
  36. rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
  37. wl12xx_spi_mem_read(wl, rx_packet_ring_addr, desc,
  38. sizeof(struct wl12xx_rx_descriptor));
  39. }
  40. static void wl12xx_rx_status(struct wl12xx *wl,
  41. struct wl12xx_rx_descriptor *desc,
  42. struct ieee80211_rx_status *status,
  43. u8 beacon)
  44. {
  45. memset(status, 0, sizeof(struct ieee80211_rx_status));
  46. status->band = IEEE80211_BAND_2GHZ;
  47. status->mactime = desc->timestamp;
  48. /*
  49. * The rx status timestamp is a 32 bits value while the TSF is a
  50. * 64 bits one.
  51. * For IBSS merging, TSF is mandatory, so we have to get it
  52. * somehow, so we ask for ACX_TSF_INFO.
  53. * That could be moved to the get_tsf() hook, but unfortunately,
  54. * this one must be atomic, while our SPI routines can sleep.
  55. */
  56. if ((wl->bss_type == BSS_TYPE_IBSS) && beacon) {
  57. u64 mactime;
  58. int ret;
  59. struct wl12xx_command cmd;
  60. struct acx_tsf_info *tsf_info;
  61. memset(&cmd, 0, sizeof(cmd));
  62. ret = wl12xx_cmd_interrogate(wl, ACX_TSF_INFO,
  63. sizeof(struct acx_tsf_info),
  64. &cmd);
  65. if (ret < 0) {
  66. wl12xx_warning("ACX_FW_REV interrogate failed");
  67. return;
  68. }
  69. tsf_info = (struct acx_tsf_info *)&(cmd.parameters);
  70. mactime = tsf_info->current_tsf_lsb |
  71. (tsf_info->current_tsf_msb << 31);
  72. status->mactime = mactime;
  73. }
  74. status->signal = desc->rssi;
  75. status->qual = (desc->rssi - WL12XX_RX_MIN_RSSI) * 100 /
  76. (WL12XX_RX_MAX_RSSI - WL12XX_RX_MIN_RSSI);
  77. status->qual = min(status->qual, 100);
  78. status->qual = max(status->qual, 0);
  79. /*
  80. * FIXME: guessing that snr needs to be divided by two, otherwise
  81. * the values don't make any sense
  82. */
  83. status->noise = desc->rssi - desc->snr / 2;
  84. status->freq = ieee80211_channel_to_frequency(desc->channel);
  85. status->flag |= RX_FLAG_TSFT;
  86. if (desc->flags & RX_DESC_ENCRYPTION_MASK) {
  87. status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
  88. if (likely(!(desc->flags & RX_DESC_DECRYPT_FAIL)))
  89. status->flag |= RX_FLAG_DECRYPTED;
  90. if (unlikely(desc->flags & RX_DESC_MIC_FAIL))
  91. status->flag |= RX_FLAG_MMIC_ERROR;
  92. }
  93. if (unlikely(!(desc->flags & RX_DESC_VALID_FCS)))
  94. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  95. /* FIXME: set status->rate_idx */
  96. }
  97. static void wl12xx_rx_body(struct wl12xx *wl,
  98. struct wl12xx_rx_descriptor *desc)
  99. {
  100. struct sk_buff *skb;
  101. struct ieee80211_rx_status status;
  102. u8 *rx_buffer, beacon = 0;
  103. u16 length, *fc;
  104. u32 curr_id, last_id_inc, rx_packet_ring_addr;
  105. length = WL12XX_RX_ALIGN(desc->length - PLCP_HEADER_LENGTH);
  106. curr_id = (desc->flags & RX_DESC_SEQNUM_MASK) >> RX_DESC_PACKETID_SHIFT;
  107. last_id_inc = (wl->rx_last_id + 1) % (RX_MAX_PACKET_ID + 1);
  108. if (last_id_inc != curr_id) {
  109. wl12xx_warning("curr ID:%d, last ID inc:%d",
  110. curr_id, last_id_inc);
  111. wl->rx_last_id = curr_id;
  112. } else {
  113. wl->rx_last_id = last_id_inc;
  114. }
  115. rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr +
  116. sizeof(struct wl12xx_rx_descriptor) + 20;
  117. if (wl->rx_current_buffer)
  118. rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
  119. skb = dev_alloc_skb(length);
  120. if (!skb) {
  121. wl12xx_error("Couldn't allocate RX frame");
  122. return;
  123. }
  124. rx_buffer = skb_put(skb, length);
  125. wl12xx_spi_mem_read(wl, rx_packet_ring_addr, rx_buffer, length);
  126. /* The actual lenght doesn't include the target's alignment */
  127. skb->len = desc->length - PLCP_HEADER_LENGTH;
  128. fc = (u16 *)skb->data;
  129. if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
  130. beacon = 1;
  131. wl12xx_rx_status(wl, desc, &status, beacon);
  132. wl12xx_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
  133. beacon ? "beacon" : "");
  134. ieee80211_rx(wl->hw, skb, &status);
  135. }
  136. static void wl12xx_rx_ack(struct wl12xx *wl)
  137. {
  138. u32 data, addr;
  139. if (wl->rx_current_buffer) {
  140. addr = ACX_REG_INTERRUPT_TRIG_H;
  141. data = INTR_TRIG_RX_PROC1;
  142. } else {
  143. addr = ACX_REG_INTERRUPT_TRIG;
  144. data = INTR_TRIG_RX_PROC0;
  145. }
  146. wl12xx_reg_write32(wl, addr, data);
  147. /* Toggle buffer ring */
  148. wl->rx_current_buffer = !wl->rx_current_buffer;
  149. }
  150. void wl12xx_rx(struct wl12xx *wl)
  151. {
  152. struct wl12xx_rx_descriptor rx_desc;
  153. if (wl->state != WL12XX_STATE_ON)
  154. return;
  155. /* We first read the frame's header */
  156. wl12xx_rx_header(wl, &rx_desc);
  157. /* Now we can read the body */
  158. wl12xx_rx_body(wl, &rx_desc);
  159. /* Finally, we need to ACK the RX */
  160. wl12xx_rx_ack(wl);
  161. return;
  162. }