rx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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(wlan_private * priv, struct sk_buff *skb);
  33. /**
  34. * @brief This function computes the avgSNR .
  35. *
  36. * @param priv A pointer to wlan_private structure
  37. * @return avgSNR
  38. */
  39. static u8 wlan_getavgsnr(wlan_private * priv)
  40. {
  41. u8 i;
  42. u16 temp = 0;
  43. wlan_adapter *adapter = priv->adapter;
  44. if (adapter->numSNRNF == 0)
  45. return 0;
  46. for (i = 0; i < adapter->numSNRNF; i++)
  47. temp += adapter->rawSNR[i];
  48. return (u8) (temp / adapter->numSNRNF);
  49. }
  50. /**
  51. * @brief This function computes the AvgNF
  52. *
  53. * @param priv A pointer to wlan_private structure
  54. * @return AvgNF
  55. */
  56. static u8 wlan_getavgnf(wlan_private * priv)
  57. {
  58. u8 i;
  59. u16 temp = 0;
  60. wlan_adapter *adapter = priv->adapter;
  61. if (adapter->numSNRNF == 0)
  62. return 0;
  63. for (i = 0; i < adapter->numSNRNF; i++)
  64. temp += adapter->rawNF[i];
  65. return (u8) (temp / adapter->numSNRNF);
  66. }
  67. /**
  68. * @brief This function save the raw SNR/NF to our internel buffer
  69. *
  70. * @param priv A pointer to wlan_private structure
  71. * @param prxpd A pointer to rxpd structure of received packet
  72. * @return n/a
  73. */
  74. static void wlan_save_rawSNRNF(wlan_private * priv, struct rxpd *p_rx_pd)
  75. {
  76. wlan_adapter *adapter = priv->adapter;
  77. if (adapter->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
  78. adapter->numSNRNF++;
  79. adapter->rawSNR[adapter->nextSNRNF] = p_rx_pd->snr;
  80. adapter->rawNF[adapter->nextSNRNF] = p_rx_pd->nf;
  81. adapter->nextSNRNF++;
  82. if (adapter->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
  83. adapter->nextSNRNF = 0;
  84. return;
  85. }
  86. /**
  87. * @brief This function computes the RSSI in received packet.
  88. *
  89. * @param priv A pointer to wlan_private structure
  90. * @param prxpd A pointer to rxpd structure of received packet
  91. * @return n/a
  92. */
  93. static void wlan_compute_rssi(wlan_private * priv, struct rxpd *p_rx_pd)
  94. {
  95. wlan_adapter *adapter = priv->adapter;
  96. lbs_deb_enter(LBS_DEB_RX);
  97. lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
  98. lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
  99. adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  100. adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  101. adapter->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
  102. adapter->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
  103. wlan_save_rawSNRNF(priv, p_rx_pd);
  104. adapter->SNR[TYPE_RXPD][TYPE_AVG] = wlan_getavgsnr(priv) * AVG_SCALE;
  105. adapter->NF[TYPE_RXPD][TYPE_AVG] = wlan_getavgnf(priv) * AVG_SCALE;
  106. lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
  107. adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  108. adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  109. adapter->RSSI[TYPE_RXPD][TYPE_NOAVG] =
  110. CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_NOAVG],
  111. adapter->NF[TYPE_RXPD][TYPE_NOAVG]);
  112. adapter->RSSI[TYPE_RXPD][TYPE_AVG] =
  113. CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
  114. adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
  115. lbs_deb_leave(LBS_DEB_RX);
  116. }
  117. void libertas_upload_rx_packet(wlan_private * priv, struct sk_buff *skb)
  118. {
  119. lbs_deb_rx("skb->data %p\n", skb->data);
  120. if (priv->adapter->monitormode != WLAN_MONITOR_OFF) {
  121. skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
  122. } else {
  123. if (priv->mesh_dev && IS_MESH_FRAME(skb))
  124. skb->protocol = eth_type_trans(skb, priv->mesh_dev);
  125. else
  126. skb->protocol = eth_type_trans(skb, priv->dev);
  127. }
  128. skb->ip_summed = CHECKSUM_UNNECESSARY;
  129. netif_rx(skb);
  130. }
  131. /**
  132. * @brief This function processes received packet and forwards it
  133. * to kernel/upper layer
  134. *
  135. * @param priv A pointer to wlan_private
  136. * @param skb A pointer to skb which includes the received packet
  137. * @return 0 or -1
  138. */
  139. int libertas_process_rxed_packet(wlan_private * priv, struct sk_buff *skb)
  140. {
  141. wlan_adapter *adapter = priv->adapter;
  142. int ret = 0;
  143. struct rxpackethdr *p_rx_pkt;
  144. struct rxpd *p_rx_pd;
  145. int hdrchop;
  146. struct ethhdr *p_ethhdr;
  147. const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  148. lbs_deb_enter(LBS_DEB_RX);
  149. if (priv->adapter->monitormode != WLAN_MONITOR_OFF)
  150. return process_rxed_802_11_packet(priv, skb);
  151. p_rx_pkt = (struct rxpackethdr *) skb->data;
  152. p_rx_pd = &p_rx_pkt->rx_pd;
  153. if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
  154. SET_MESH_FRAME(skb);
  155. else
  156. UNSET_MESH_FRAME(skb);
  157. lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
  158. min_t(unsigned int, skb->len, 100));
  159. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  160. lbs_deb_rx("rx err: frame received with bad length\n");
  161. priv->stats.rx_length_errors++;
  162. ret = 0;
  163. goto done;
  164. }
  165. /*
  166. * Check rxpd status and update 802.3 stat,
  167. */
  168. if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
  169. lbs_deb_rx("rx err: frame received with bad status\n");
  170. lbs_pr_alert("rxpd not ok\n");
  171. priv->stats.rx_errors++;
  172. ret = 0;
  173. goto done;
  174. }
  175. lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
  176. skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
  177. lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
  178. sizeof(p_rx_pkt->eth803_hdr.dest_addr));
  179. lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
  180. sizeof(p_rx_pkt->eth803_hdr.src_addr));
  181. if (memcmp(&p_rx_pkt->rfc1042_hdr,
  182. rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
  183. /*
  184. * Replace the 803 header and rfc1042 header (llc/snap) with an
  185. * EthernetII header, keep the src/dst and snap_type (ethertype)
  186. *
  187. * The firmware only passes up SNAP frames converting
  188. * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
  189. *
  190. * To create the Ethernet II, just move the src, dst address right
  191. * before the snap_type.
  192. */
  193. p_ethhdr = (struct ethhdr *)
  194. ((u8 *) & p_rx_pkt->eth803_hdr
  195. + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
  196. - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
  197. - sizeof(p_rx_pkt->eth803_hdr.src_addr)
  198. - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
  199. memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
  200. sizeof(p_ethhdr->h_source));
  201. memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
  202. sizeof(p_ethhdr->h_dest));
  203. /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
  204. * that was removed
  205. */
  206. hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
  207. } else {
  208. lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
  209. (u8 *) & p_rx_pkt->rfc1042_hdr,
  210. sizeof(p_rx_pkt->rfc1042_hdr));
  211. /* Chop off the rxpd */
  212. hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
  213. }
  214. /* Chop off the leading header bytes so the skb points to the start of
  215. * either the reconstructed EthII frame or the 802.2/llc/snap frame
  216. */
  217. skb_pull(skb, hdrchop);
  218. /* Take the data rate from the rxpd structure
  219. * only if the rate is auto
  220. */
  221. if (adapter->auto_rate)
  222. adapter->cur_rate = libertas_fw_index_to_data_rate(p_rx_pd->rx_rate);
  223. wlan_compute_rssi(priv, p_rx_pd);
  224. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  225. priv->stats.rx_bytes += skb->len;
  226. priv->stats.rx_packets++;
  227. libertas_upload_rx_packet(priv, skb);
  228. ret = 0;
  229. done:
  230. lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
  231. return ret;
  232. }
  233. EXPORT_SYMBOL_GPL(libertas_process_rxed_packet);
  234. /**
  235. * @brief This function converts Tx/Rx rates from the Marvell WLAN format
  236. * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
  237. *
  238. * @param rate Input rate
  239. * @return Output Rate (0 if invalid)
  240. */
  241. static u8 convert_mv_rate_to_radiotap(u8 rate)
  242. {
  243. switch (rate) {
  244. case 0: /* 1 Mbps */
  245. return 2;
  246. case 1: /* 2 Mbps */
  247. return 4;
  248. case 2: /* 5.5 Mbps */
  249. return 11;
  250. case 3: /* 11 Mbps */
  251. return 22;
  252. /* case 4: reserved */
  253. case 5: /* 6 Mbps */
  254. return 12;
  255. case 6: /* 9 Mbps */
  256. return 18;
  257. case 7: /* 12 Mbps */
  258. return 24;
  259. case 8: /* 18 Mbps */
  260. return 36;
  261. case 9: /* 24 Mbps */
  262. return 48;
  263. case 10: /* 36 Mbps */
  264. return 72;
  265. case 11: /* 48 Mbps */
  266. return 96;
  267. case 12: /* 54 Mbps */
  268. return 108;
  269. }
  270. lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
  271. return 0;
  272. }
  273. /**
  274. * @brief This function processes a received 802.11 packet and forwards it
  275. * to kernel/upper layer
  276. *
  277. * @param priv A pointer to wlan_private
  278. * @param skb A pointer to skb which includes the received packet
  279. * @return 0 or -1
  280. */
  281. static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb)
  282. {
  283. wlan_adapter *adapter = priv->adapter;
  284. int ret = 0;
  285. struct rx80211packethdr *p_rx_pkt;
  286. struct rxpd *prxpd;
  287. struct rx_radiotap_hdr radiotap_hdr;
  288. struct rx_radiotap_hdr *pradiotap_hdr;
  289. lbs_deb_enter(LBS_DEB_RX);
  290. p_rx_pkt = (struct rx80211packethdr *) skb->data;
  291. prxpd = &p_rx_pkt->rx_pd;
  292. // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
  293. if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
  294. lbs_deb_rx("rx err: frame received wit bad length\n");
  295. priv->stats.rx_length_errors++;
  296. ret = 0;
  297. goto done;
  298. }
  299. /*
  300. * Check rxpd status and update 802.3 stat,
  301. */
  302. if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
  303. //lbs_deb_rx("rx err: frame received with bad status\n");
  304. priv->stats.rx_errors++;
  305. }
  306. lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
  307. skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
  308. /* create the exported radio header */
  309. if(priv->adapter->monitormode == WLAN_MONITOR_OFF) {
  310. /* no radio header */
  311. /* chop the rxpd */
  312. skb_pull(skb, sizeof(struct rxpd));
  313. }
  314. else {
  315. /* radiotap header */
  316. radiotap_hdr.hdr.it_version = 0;
  317. /* XXX must check this value for pad */
  318. radiotap_hdr.hdr.it_pad = 0;
  319. radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
  320. radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
  321. /* unknown values */
  322. radiotap_hdr.flags = 0;
  323. radiotap_hdr.chan_freq = 0;
  324. radiotap_hdr.chan_flags = 0;
  325. radiotap_hdr.antenna = 0;
  326. /* known values */
  327. radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
  328. /* XXX must check no carryout */
  329. radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
  330. radiotap_hdr.rx_flags = 0;
  331. if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
  332. radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
  333. //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
  334. /* chop the rxpd */
  335. skb_pull(skb, sizeof(struct rxpd));
  336. /* add space for the new radio header */
  337. if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
  338. pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
  339. GFP_ATOMIC)) {
  340. lbs_pr_alert("%s: couldn't pskb_expand_head\n",
  341. __func__);
  342. }
  343. pradiotap_hdr =
  344. (struct rx_radiotap_hdr *)skb_push(skb,
  345. sizeof(struct
  346. rx_radiotap_hdr));
  347. memcpy(pradiotap_hdr, &radiotap_hdr,
  348. sizeof(struct rx_radiotap_hdr));
  349. }
  350. /* Take the data rate from the rxpd structure
  351. * only if the rate is auto
  352. */
  353. if (adapter->auto_rate)
  354. adapter->cur_rate = libertas_fw_index_to_data_rate(prxpd->rx_rate);
  355. wlan_compute_rssi(priv, prxpd);
  356. lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
  357. priv->stats.rx_bytes += skb->len;
  358. priv->stats.rx_packets++;
  359. libertas_upload_rx_packet(priv, skb);
  360. ret = 0;
  361. done:
  362. lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
  363. return ret;
  364. }