rx.c 11 KB

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