rx.c 12 KB

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