rx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**
  2. * This file contains the handling of RX in wlan driver.
  3. */
  4. #include <linux/etherdevice.h>
  5. #include <linux/types.h>
  6. #include "hostcmd.h"
  7. #include "radiotap.h"
  8. #include "decl.h"
  9. #include "dev.h"
  10. #include "wext.h"
  11. struct eth803hdr {
  12. u8 dest_addr[6];
  13. u8 src_addr[6];
  14. u16 h803_len;
  15. } __attribute__ ((packed));
  16. struct rfc1042hdr {
  17. u8 llc_dsap;
  18. u8 llc_ssap;
  19. u8 llc_ctrl;
  20. u8 snap_oui[3];
  21. u16 snap_type;
  22. } __attribute__ ((packed));
  23. struct rxpackethdr {
  24. struct eth803hdr eth803_hdr;
  25. struct rfc1042hdr rfc1042_hdr;
  26. } __attribute__ ((packed));
  27. struct rx80211packethdr {
  28. struct rxpd rx_pd;
  29. void *eth80211_hdr;
  30. } __attribute__ ((packed));
  31. static int process_rxed_802_11_packet(struct lbs_private *priv,
  32. struct sk_buff *skb);
  33. /**
  34. * @brief This function computes the avgSNR .
  35. *
  36. * @param priv A pointer to struct lbs_private structure
  37. * @return avgSNR
  38. */
  39. static u8 lbs_getavgsnr(struct lbs_private *priv)
  40. {
  41. u8 i;
  42. u16 temp = 0;
  43. if (priv->numSNRNF == 0)
  44. return 0;
  45. for (i = 0; i < priv->numSNRNF; i++)
  46. temp += priv->rawSNR[i];
  47. return (u8) (temp / priv->numSNRNF);
  48. }
  49. /**
  50. * @brief This function computes the AvgNF
  51. *
  52. * @param priv A pointer to struct lbs_private structure
  53. * @return AvgNF
  54. */
  55. static u8 lbs_getavgnf(struct lbs_private *priv)
  56. {
  57. u8 i;
  58. u16 temp = 0;
  59. if (priv->numSNRNF == 0)
  60. return 0;
  61. for (i = 0; i < priv->numSNRNF; i++)
  62. temp += priv->rawNF[i];
  63. return (u8) (temp / priv->numSNRNF);
  64. }
  65. /**
  66. * @brief This function save the raw SNR/NF to our internel buffer
  67. *
  68. * @param priv A pointer to struct lbs_private structure
  69. * @param prxpd A pointer to rxpd structure of received packet
  70. * @return n/a
  71. */
  72. static void lbs_save_rawSNRNF(struct lbs_private *priv, struct rxpd *p_rx_pd)
  73. {
  74. if (priv->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
  75. priv->numSNRNF++;
  76. priv->rawSNR[priv->nextSNRNF] = p_rx_pd->snr;
  77. priv->rawNF[priv->nextSNRNF] = p_rx_pd->nf;
  78. priv->nextSNRNF++;
  79. if (priv->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
  80. priv->nextSNRNF = 0;
  81. return;
  82. }
  83. /**
  84. * @brief This function computes the RSSI in received packet.
  85. *
  86. * @param priv A pointer to struct lbs_private structure
  87. * @param prxpd A pointer to rxpd structure of received packet
  88. * @return n/a
  89. */
  90. static void lbs_compute_rssi(struct lbs_private *priv, struct rxpd *p_rx_pd)
  91. {
  92. lbs_deb_enter(LBS_DEB_RX);
  93. lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
  94. lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
  95. priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  96. priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  97. priv->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
  98. priv->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
  99. lbs_save_rawSNRNF(priv, p_rx_pd);
  100. priv->SNR[TYPE_RXPD][TYPE_AVG] = lbs_getavgsnr(priv) * AVG_SCALE;
  101. priv->NF[TYPE_RXPD][TYPE_AVG] = lbs_getavgnf(priv) * AVG_SCALE;
  102. lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
  103. priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  104. priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  105. priv->RSSI[TYPE_RXPD][TYPE_NOAVG] =
  106. CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_NOAVG],
  107. priv->NF[TYPE_RXPD][TYPE_NOAVG]);
  108. priv->RSSI[TYPE_RXPD][TYPE_AVG] =
  109. CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  110. priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  111. lbs_deb_leave(LBS_DEB_RX);
  112. }
  113. /**
  114. * @brief This function processes received packet and forwards it
  115. * to kernel/upper layer
  116. *
  117. * @param priv A pointer to struct lbs_private
  118. * @param skb A pointer to skb which includes the received packet
  119. * @return 0 or -1
  120. */
  121. int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
  122. {
  123. int ret = 0;
  124. struct net_device *dev = priv->dev;
  125. struct rxpackethdr *p_rx_pkt;
  126. struct rxpd *p_rx_pd;
  127. int hdrchop;
  128. struct ethhdr *p_ethhdr;
  129. const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  130. lbs_deb_enter(LBS_DEB_RX);
  131. BUG_ON(!skb);
  132. skb->ip_summed = CHECKSUM_NONE;
  133. if (priv->monitormode)
  134. return process_rxed_802_11_packet(priv, skb);
  135. p_rx_pd = (struct rxpd *) skb->data;
  136. p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
  137. le32_to_cpu(p_rx_pd->pkt_ptr));
  138. if (priv->mesh_dev) {
  139. if (priv->mesh_fw_ver == MESH_FW_OLD) {
  140. if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
  141. dev = priv->mesh_dev;
  142. } else if (priv->mesh_fw_ver == MESH_FW_NEW) {
  143. if (p_rx_pd->u.bss.bss_num == MESH_IFACE_ID)
  144. dev = priv->mesh_dev;
  145. }
  146. }
  147. lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
  148. min_t(unsigned int, skb->len, 100));
  149. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  150. lbs_deb_rx("rx err: frame received with bad length\n");
  151. dev->stats.rx_length_errors++;
  152. ret = 0;
  153. dev_kfree_skb(skb);
  154. goto done;
  155. }
  156. lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
  157. skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
  158. skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
  159. lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
  160. sizeof(p_rx_pkt->eth803_hdr.dest_addr));
  161. lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
  162. sizeof(p_rx_pkt->eth803_hdr.src_addr));
  163. if (memcmp(&p_rx_pkt->rfc1042_hdr,
  164. rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
  165. /*
  166. * Replace the 803 header and rfc1042 header (llc/snap) with an
  167. * EthernetII header, keep the src/dst and snap_type (ethertype)
  168. *
  169. * The firmware only passes up SNAP frames converting
  170. * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
  171. *
  172. * To create the Ethernet II, just move the src, dst address right
  173. * before the snap_type.
  174. */
  175. p_ethhdr = (struct ethhdr *)
  176. ((u8 *) & p_rx_pkt->eth803_hdr
  177. + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
  178. - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
  179. - sizeof(p_rx_pkt->eth803_hdr.src_addr)
  180. - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
  181. memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
  182. sizeof(p_ethhdr->h_source));
  183. memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
  184. sizeof(p_ethhdr->h_dest));
  185. /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
  186. * that was removed
  187. */
  188. hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
  189. } else {
  190. lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
  191. (u8 *) & p_rx_pkt->rfc1042_hdr,
  192. sizeof(p_rx_pkt->rfc1042_hdr));
  193. /* Chop off the rxpd */
  194. hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
  195. }
  196. /* Chop off the leading header bytes so the skb points to the start of
  197. * either the reconstructed EthII frame or the 802.2/llc/snap frame
  198. */
  199. skb_pull(skb, hdrchop);
  200. /* Take the data rate from the rxpd structure
  201. * only if the rate is auto
  202. */
  203. if (priv->enablehwauto)
  204. priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
  205. lbs_compute_rssi(priv, p_rx_pd);
  206. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  207. dev->stats.rx_bytes += skb->len;
  208. dev->stats.rx_packets++;
  209. skb->protocol = eth_type_trans(skb, dev);
  210. if (in_interrupt())
  211. netif_rx(skb);
  212. else
  213. netif_rx_ni(skb);
  214. ret = 0;
  215. done:
  216. lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
  217. return ret;
  218. }
  219. EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
  220. /**
  221. * @brief This function converts Tx/Rx rates from the Marvell WLAN format
  222. * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
  223. *
  224. * @param rate Input rate
  225. * @return Output Rate (0 if invalid)
  226. */
  227. static u8 convert_mv_rate_to_radiotap(u8 rate)
  228. {
  229. switch (rate) {
  230. case 0: /* 1 Mbps */
  231. return 2;
  232. case 1: /* 2 Mbps */
  233. return 4;
  234. case 2: /* 5.5 Mbps */
  235. return 11;
  236. case 3: /* 11 Mbps */
  237. return 22;
  238. /* case 4: reserved */
  239. case 5: /* 6 Mbps */
  240. return 12;
  241. case 6: /* 9 Mbps */
  242. return 18;
  243. case 7: /* 12 Mbps */
  244. return 24;
  245. case 8: /* 18 Mbps */
  246. return 36;
  247. case 9: /* 24 Mbps */
  248. return 48;
  249. case 10: /* 36 Mbps */
  250. return 72;
  251. case 11: /* 48 Mbps */
  252. return 96;
  253. case 12: /* 54 Mbps */
  254. return 108;
  255. }
  256. lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
  257. return 0;
  258. }
  259. /**
  260. * @brief This function processes a received 802.11 packet and forwards it
  261. * to kernel/upper layer
  262. *
  263. * @param priv A pointer to struct lbs_private
  264. * @param skb A pointer to skb which includes the received packet
  265. * @return 0 or -1
  266. */
  267. static int process_rxed_802_11_packet(struct lbs_private *priv,
  268. struct sk_buff *skb)
  269. {
  270. int ret = 0;
  271. struct net_device *dev = priv->dev;
  272. struct rx80211packethdr *p_rx_pkt;
  273. struct rxpd *prxpd;
  274. struct rx_radiotap_hdr radiotap_hdr;
  275. struct rx_radiotap_hdr *pradiotap_hdr;
  276. lbs_deb_enter(LBS_DEB_RX);
  277. p_rx_pkt = (struct rx80211packethdr *) skb->data;
  278. prxpd = &p_rx_pkt->rx_pd;
  279. // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
  280. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  281. lbs_deb_rx("rx err: frame received with bad length\n");
  282. dev->stats.rx_length_errors++;
  283. ret = -EINVAL;
  284. kfree_skb(skb);
  285. goto done;
  286. }
  287. lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
  288. skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
  289. /* create the exported radio header */
  290. /* radiotap header */
  291. radiotap_hdr.hdr.it_version = 0;
  292. /* XXX must check this value for pad */
  293. radiotap_hdr.hdr.it_pad = 0;
  294. radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
  295. radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
  296. radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
  297. /* XXX must check no carryout */
  298. radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
  299. /* chop the rxpd */
  300. skb_pull(skb, sizeof(struct rxpd));
  301. /* add space for the new radio header */
  302. if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
  303. pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
  304. lbs_pr_alert("%s: couldn't pskb_expand_head\n", __func__);
  305. ret = -ENOMEM;
  306. kfree_skb(skb);
  307. goto done;
  308. }
  309. pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
  310. memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
  311. /* Take the data rate from the rxpd structure
  312. * only if the rate is auto
  313. */
  314. if (priv->enablehwauto)
  315. priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
  316. lbs_compute_rssi(priv, prxpd);
  317. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  318. dev->stats.rx_bytes += skb->len;
  319. dev->stats.rx_packets++;
  320. skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
  321. netif_rx(skb);
  322. ret = 0;
  323. done:
  324. lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
  325. return ret;
  326. }