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. BUG_ON(!skb);
  133. skb->ip_summed = CHECKSUM_NONE;
  134. if (priv->monitormode)
  135. return process_rxed_802_11_packet(priv, skb);
  136. p_rx_pkt = (struct rxpackethdr *) skb->data;
  137. p_rx_pd = &p_rx_pkt->rx_pd;
  138. if (priv->mesh_dev && (p_rx_pd->rx_control & RxPD_MESH_FRAME))
  139. dev = priv->mesh_dev;
  140. lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
  141. min_t(unsigned int, skb->len, 100));
  142. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  143. lbs_deb_rx("rx err: frame received with bad length\n");
  144. priv->stats.rx_length_errors++;
  145. ret = 0;
  146. goto done;
  147. }
  148. /*
  149. * Check rxpd status and update 802.3 stat,
  150. */
  151. if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
  152. lbs_deb_rx("rx err: frame received with bad status\n");
  153. lbs_pr_alert("rxpd not ok\n");
  154. priv->stats.rx_errors++;
  155. ret = 0;
  156. goto done;
  157. }
  158. lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
  159. skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
  160. lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
  161. sizeof(p_rx_pkt->eth803_hdr.dest_addr));
  162. lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
  163. sizeof(p_rx_pkt->eth803_hdr.src_addr));
  164. if (memcmp(&p_rx_pkt->rfc1042_hdr,
  165. rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
  166. /*
  167. * Replace the 803 header and rfc1042 header (llc/snap) with an
  168. * EthernetII header, keep the src/dst and snap_type (ethertype)
  169. *
  170. * The firmware only passes up SNAP frames converting
  171. * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
  172. *
  173. * To create the Ethernet II, just move the src, dst address right
  174. * before the snap_type.
  175. */
  176. p_ethhdr = (struct ethhdr *)
  177. ((u8 *) & p_rx_pkt->eth803_hdr
  178. + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
  179. - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
  180. - sizeof(p_rx_pkt->eth803_hdr.src_addr)
  181. - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
  182. memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
  183. sizeof(p_ethhdr->h_source));
  184. memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
  185. sizeof(p_ethhdr->h_dest));
  186. /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
  187. * that was removed
  188. */
  189. hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
  190. } else {
  191. lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
  192. (u8 *) & p_rx_pkt->rfc1042_hdr,
  193. sizeof(p_rx_pkt->rfc1042_hdr));
  194. /* Chop off the rxpd */
  195. hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
  196. }
  197. /* Chop off the leading header bytes so the skb points to the start of
  198. * either the reconstructed EthII frame or the 802.2/llc/snap frame
  199. */
  200. skb_pull(skb, hdrchop);
  201. /* Take the data rate from the rxpd structure
  202. * only if the rate is auto
  203. */
  204. if (priv->enablehwauto)
  205. priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
  206. lbs_compute_rssi(priv, p_rx_pd);
  207. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  208. priv->stats.rx_bytes += skb->len;
  209. priv->stats.rx_packets++;
  210. skb->protocol = eth_type_trans(skb, dev);
  211. if (in_interrupt())
  212. netif_rx(skb);
  213. else
  214. netif_rx_ni(skb);
  215. ret = 0;
  216. done:
  217. lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
  218. return ret;
  219. }
  220. EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
  221. /**
  222. * @brief This function converts Tx/Rx rates from the Marvell WLAN format
  223. * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
  224. *
  225. * @param rate Input rate
  226. * @return Output Rate (0 if invalid)
  227. */
  228. static u8 convert_mv_rate_to_radiotap(u8 rate)
  229. {
  230. switch (rate) {
  231. case 0: /* 1 Mbps */
  232. return 2;
  233. case 1: /* 2 Mbps */
  234. return 4;
  235. case 2: /* 5.5 Mbps */
  236. return 11;
  237. case 3: /* 11 Mbps */
  238. return 22;
  239. /* case 4: reserved */
  240. case 5: /* 6 Mbps */
  241. return 12;
  242. case 6: /* 9 Mbps */
  243. return 18;
  244. case 7: /* 12 Mbps */
  245. return 24;
  246. case 8: /* 18 Mbps */
  247. return 36;
  248. case 9: /* 24 Mbps */
  249. return 48;
  250. case 10: /* 36 Mbps */
  251. return 72;
  252. case 11: /* 48 Mbps */
  253. return 96;
  254. case 12: /* 54 Mbps */
  255. return 108;
  256. }
  257. lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
  258. return 0;
  259. }
  260. /**
  261. * @brief This function processes a received 802.11 packet and forwards it
  262. * to kernel/upper layer
  263. *
  264. * @param priv A pointer to struct lbs_private
  265. * @param skb A pointer to skb which includes the received packet
  266. * @return 0 or -1
  267. */
  268. static int process_rxed_802_11_packet(struct lbs_private *priv,
  269. struct sk_buff *skb)
  270. {
  271. int ret = 0;
  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. priv->stats.rx_length_errors++;
  283. ret = -EINVAL;
  284. kfree_skb(skb);
  285. goto done;
  286. }
  287. /*
  288. * Check rxpd status and update 802.3 stat,
  289. */
  290. if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
  291. //lbs_deb_rx("rx err: frame received with bad status\n");
  292. priv->stats.rx_errors++;
  293. }
  294. lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
  295. skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
  296. /* create the exported radio header */
  297. /* radiotap header */
  298. radiotap_hdr.hdr.it_version = 0;
  299. /* XXX must check this value for pad */
  300. radiotap_hdr.hdr.it_pad = 0;
  301. radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
  302. radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
  303. /* unknown values */
  304. radiotap_hdr.flags = 0;
  305. radiotap_hdr.chan_freq = 0;
  306. radiotap_hdr.chan_flags = 0;
  307. radiotap_hdr.antenna = 0;
  308. /* known values */
  309. radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
  310. /* XXX must check no carryout */
  311. radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
  312. radiotap_hdr.rx_flags = 0;
  313. if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
  314. radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
  315. //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
  316. /* chop the rxpd */
  317. skb_pull(skb, sizeof(struct rxpd));
  318. /* add space for the new radio header */
  319. if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
  320. pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
  321. lbs_pr_alert("%s: couldn't pskb_expand_head\n", __func__);
  322. ret = -ENOMEM;
  323. kfree_skb(skb);
  324. goto done;
  325. }
  326. pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
  327. memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
  328. /* Take the data rate from the rxpd structure
  329. * only if the rate is auto
  330. */
  331. if (priv->enablehwauto)
  332. priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
  333. lbs_compute_rssi(priv, prxpd);
  334. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  335. priv->stats.rx_bytes += skb->len;
  336. priv->stats.rx_packets++;
  337. skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
  338. netif_rx(skb);
  339. ret = 0;
  340. done:
  341. lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
  342. return ret;
  343. }