rx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 rxpd rx_pd;
  25. struct eth803hdr eth803_hdr;
  26. struct rfc1042hdr rfc1042_hdr;
  27. } __attribute__ ((packed));
  28. struct rx80211packethdr {
  29. struct rxpd rx_pd;
  30. void *eth80211_hdr;
  31. } __attribute__ ((packed));
  32. static int process_rxed_802_11_packet(struct lbs_private *priv,
  33. struct sk_buff *skb);
  34. /**
  35. * @brief This function computes the avgSNR .
  36. *
  37. * @param priv A pointer to struct lbs_private structure
  38. * @return avgSNR
  39. */
  40. static u8 lbs_getavgsnr(struct lbs_private *priv)
  41. {
  42. u8 i;
  43. u16 temp = 0;
  44. if (priv->numSNRNF == 0)
  45. return 0;
  46. for (i = 0; i < priv->numSNRNF; i++)
  47. temp += priv->rawSNR[i];
  48. return (u8) (temp / priv->numSNRNF);
  49. }
  50. /**
  51. * @brief This function computes the AvgNF
  52. *
  53. * @param priv A pointer to struct lbs_private structure
  54. * @return AvgNF
  55. */
  56. static u8 lbs_getavgnf(struct lbs_private *priv)
  57. {
  58. u8 i;
  59. u16 temp = 0;
  60. if (priv->numSNRNF == 0)
  61. return 0;
  62. for (i = 0; i < priv->numSNRNF; i++)
  63. temp += priv->rawNF[i];
  64. return (u8) (temp / priv->numSNRNF);
  65. }
  66. /**
  67. * @brief This function save the raw SNR/NF to our internel buffer
  68. *
  69. * @param priv A pointer to struct lbs_private structure
  70. * @param prxpd A pointer to rxpd structure of received packet
  71. * @return n/a
  72. */
  73. static void lbs_save_rawSNRNF(struct lbs_private *priv, struct rxpd *p_rx_pd)
  74. {
  75. if (priv->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
  76. priv->numSNRNF++;
  77. priv->rawSNR[priv->nextSNRNF] = p_rx_pd->snr;
  78. priv->rawNF[priv->nextSNRNF] = p_rx_pd->nf;
  79. priv->nextSNRNF++;
  80. if (priv->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
  81. priv->nextSNRNF = 0;
  82. return;
  83. }
  84. /**
  85. * @brief This function computes the RSSI in received packet.
  86. *
  87. * @param priv A pointer to struct lbs_private structure
  88. * @param prxpd A pointer to rxpd structure of received packet
  89. * @return n/a
  90. */
  91. static void lbs_compute_rssi(struct lbs_private *priv, struct rxpd *p_rx_pd)
  92. {
  93. lbs_deb_enter(LBS_DEB_RX);
  94. lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
  95. lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
  96. priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  97. priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  98. priv->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
  99. priv->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
  100. lbs_save_rawSNRNF(priv, p_rx_pd);
  101. priv->SNR[TYPE_RXPD][TYPE_AVG] = lbs_getavgsnr(priv) * AVG_SCALE;
  102. priv->NF[TYPE_RXPD][TYPE_AVG] = lbs_getavgnf(priv) * AVG_SCALE;
  103. lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
  104. priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  105. priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  106. priv->RSSI[TYPE_RXPD][TYPE_NOAVG] =
  107. CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_NOAVG],
  108. priv->NF[TYPE_RXPD][TYPE_NOAVG]);
  109. priv->RSSI[TYPE_RXPD][TYPE_AVG] =
  110. CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  111. priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  112. lbs_deb_leave(LBS_DEB_RX);
  113. }
  114. /**
  115. * @brief This function processes received packet and forwards it
  116. * to kernel/upper layer
  117. *
  118. * @param priv A pointer to struct lbs_private
  119. * @param skb A pointer to skb which includes the received packet
  120. * @return 0 or -1
  121. */
  122. int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
  123. {
  124. int ret = 0;
  125. struct net_device *dev = priv->dev;
  126. struct rxpackethdr *p_rx_pkt;
  127. struct rxpd *p_rx_pd;
  128. int hdrchop;
  129. struct ethhdr *p_ethhdr;
  130. const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  131. lbs_deb_enter(LBS_DEB_RX);
  132. skb->ip_summed = CHECKSUM_NONE;
  133. if (priv->monitormode != LBS_MONITOR_OFF)
  134. return process_rxed_802_11_packet(priv, skb);
  135. p_rx_pkt = (struct rxpackethdr *) skb->data;
  136. p_rx_pd = &p_rx_pkt->rx_pd;
  137. if (priv->mesh_dev && (p_rx_pd->rx_control & RxPD_MESH_FRAME))
  138. dev = priv->mesh_dev;
  139. lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
  140. min_t(unsigned int, skb->len, 100));
  141. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  142. lbs_deb_rx("rx err: frame received with bad length\n");
  143. priv->stats.rx_length_errors++;
  144. ret = 0;
  145. goto done;
  146. }
  147. /*
  148. * Check rxpd status and update 802.3 stat,
  149. */
  150. if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
  151. lbs_deb_rx("rx err: frame received with bad status\n");
  152. lbs_pr_alert("rxpd not ok\n");
  153. priv->stats.rx_errors++;
  154. ret = 0;
  155. goto done;
  156. }
  157. lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
  158. skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
  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_pkt;
  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_pkt;
  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->auto_rate)
  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. priv->stats.rx_bytes += skb->len;
  208. priv->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 rx80211packethdr *p_rx_pkt;
  272. struct rxpd *prxpd;
  273. struct rx_radiotap_hdr radiotap_hdr;
  274. struct rx_radiotap_hdr *pradiotap_hdr;
  275. lbs_deb_enter(LBS_DEB_RX);
  276. p_rx_pkt = (struct rx80211packethdr *) skb->data;
  277. prxpd = &p_rx_pkt->rx_pd;
  278. // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
  279. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  280. lbs_deb_rx("rx err: frame received with bad length\n");
  281. priv->stats.rx_length_errors++;
  282. ret = -EINVAL;
  283. kfree(skb);
  284. goto done;
  285. }
  286. /*
  287. * Check rxpd status and update 802.3 stat,
  288. */
  289. if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
  290. //lbs_deb_rx("rx err: frame received with bad status\n");
  291. priv->stats.rx_errors++;
  292. }
  293. lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
  294. skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
  295. /* create the exported radio header */
  296. /* radiotap header */
  297. radiotap_hdr.hdr.it_version = 0;
  298. /* XXX must check this value for pad */
  299. radiotap_hdr.hdr.it_pad = 0;
  300. radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
  301. radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
  302. /* unknown values */
  303. radiotap_hdr.flags = 0;
  304. radiotap_hdr.chan_freq = 0;
  305. radiotap_hdr.chan_flags = 0;
  306. radiotap_hdr.antenna = 0;
  307. /* known values */
  308. radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
  309. /* XXX must check no carryout */
  310. radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
  311. radiotap_hdr.rx_flags = 0;
  312. if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
  313. radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
  314. //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
  315. /* chop the rxpd */
  316. skb_pull(skb, sizeof(struct rxpd));
  317. /* add space for the new radio header */
  318. if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
  319. pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
  320. lbs_pr_alert("%s: couldn't pskb_expand_head\n", __func__);
  321. ret = -ENOMEM;
  322. kfree_skb(skb);
  323. goto done;
  324. }
  325. pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
  326. memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
  327. /* Take the data rate from the rxpd structure
  328. * only if the rate is auto
  329. */
  330. if (priv->auto_rate)
  331. priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
  332. lbs_compute_rssi(priv, prxpd);
  333. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  334. priv->stats.rx_bytes += skb->len;
  335. priv->stats.rx_packets++;
  336. skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
  337. netif_rx(skb);
  338. ret = 0;
  339. done:
  340. lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
  341. return ret;
  342. }