2   * This file contains the handling of RX in wlan driver.
 
   4 #include <linux/etherdevice.h>
 
   5 #include <linux/types.h>
 
  17 } __attribute__ ((packed));
 
  25 } __attribute__ ((packed));
 
  29         struct eth803hdr eth803_hdr;
 
  30         struct rfc1042hdr rfc1042_hdr;
 
  31 } __attribute__ ((packed));
 
  33 struct rx80211packethdr {
 
  36 } __attribute__ ((packed));
 
  38 static int process_rxed_802_11_packet(struct lbs_private *priv,
 
  42  *  @brief This function computes the avgSNR .
 
  44  *  @param priv    A pointer to struct lbs_private structure
 
  47 static u8 lbs_getavgsnr(struct lbs_private *priv)
 
  51         if (priv->numSNRNF == 0)
 
  53         for (i = 0; i < priv->numSNRNF; i++)
 
  54                 temp += priv->rawSNR[i];
 
  55         return (u8) (temp / priv->numSNRNF);
 
  60  *  @brief This function computes the AvgNF
 
  62  *  @param priv    A pointer to struct lbs_private structure
 
  65 static u8 lbs_getavgnf(struct lbs_private *priv)
 
  69         if (priv->numSNRNF == 0)
 
  71         for (i = 0; i < priv->numSNRNF; i++)
 
  72                 temp += priv->rawNF[i];
 
  73         return (u8) (temp / priv->numSNRNF);
 
  78  *  @brief This function save the raw SNR/NF to our internel buffer
 
  80  *  @param priv    A pointer to struct lbs_private structure
 
  81  *  @param prxpd   A pointer to rxpd structure of received packet
 
  84 static void lbs_save_rawSNRNF(struct lbs_private *priv, struct rxpd *p_rx_pd)
 
  86         if (priv->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
 
  88         priv->rawSNR[priv->nextSNRNF] = p_rx_pd->snr;
 
  89         priv->rawNF[priv->nextSNRNF] = p_rx_pd->nf;
 
  91         if (priv->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
 
  97  *  @brief This function computes the RSSI in received packet.
 
  99  *  @param priv    A pointer to struct lbs_private structure
 
 100  *  @param prxpd   A pointer to rxpd structure of received packet
 
 103 static void lbs_compute_rssi(struct lbs_private *priv, struct rxpd *p_rx_pd)
 
 106         lbs_deb_enter(LBS_DEB_RX);
 
 108         lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
 
 109         lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
 
 110                priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
 
 111                priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
 
 113         priv->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
 
 114         priv->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
 
 115         lbs_save_rawSNRNF(priv, p_rx_pd);
 
 117         priv->SNR[TYPE_RXPD][TYPE_AVG] = lbs_getavgsnr(priv) * AVG_SCALE;
 
 118         priv->NF[TYPE_RXPD][TYPE_AVG] = lbs_getavgnf(priv) * AVG_SCALE;
 
 119         lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
 
 120                priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
 
 121                priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
 
 123         priv->RSSI[TYPE_RXPD][TYPE_NOAVG] =
 
 124             CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_NOAVG],
 
 125                      priv->NF[TYPE_RXPD][TYPE_NOAVG]);
 
 127         priv->RSSI[TYPE_RXPD][TYPE_AVG] =
 
 128             CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
 
 129                      priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
 
 131         lbs_deb_leave(LBS_DEB_RX);
 
 135  *  @brief This function processes received packet and forwards it
 
 136  *  to kernel/upper layer
 
 138  *  @param priv    A pointer to struct lbs_private
 
 139  *  @param skb     A pointer to skb which includes the received packet
 
 142 int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
 
 145         struct net_device *dev = priv->dev;
 
 146         struct rxpackethdr *p_rx_pkt;
 
 147         struct rxpd *p_rx_pd;
 
 149         struct ethhdr *p_ethhdr;
 
 150         const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
 
 152         lbs_deb_enter(LBS_DEB_RX);
 
 156         skb->ip_summed = CHECKSUM_NONE;
 
 158         if (priv->monitormode)
 
 159                 return process_rxed_802_11_packet(priv, skb);
 
 161         p_rx_pkt = (struct rxpackethdr *) skb->data;
 
 162         p_rx_pd = &p_rx_pkt->rx_pd;
 
 163         if (priv->mesh_dev && (p_rx_pd->rx_control & RxPD_MESH_FRAME))
 
 164                 dev = priv->mesh_dev;
 
 166         lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
 
 167                  min_t(unsigned int, skb->len, 100));
 
 169         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
 
 170                 lbs_deb_rx("rx err: frame received with bad length\n");
 
 171                 dev->stats.rx_length_errors++;
 
 178          * Check rxpd status and update 802.3 stat,
 
 180         if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
 
 181                 lbs_deb_rx("rx err: frame received with bad status\n");
 
 182                 lbs_pr_alert("rxpd not ok\n");
 
 183                 dev->stats.rx_errors++;
 
 189         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
 
 190                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
 
 192         lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
 
 193                 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
 
 194         lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
 
 195                 sizeof(p_rx_pkt->eth803_hdr.src_addr));
 
 197         if (memcmp(&p_rx_pkt->rfc1042_hdr,
 
 198                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
 
 200                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
 
 201                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
 
 203                  *  The firmware only passes up SNAP frames converting
 
 204                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
 
 206                  *  To create the Ethernet II, just move the src, dst address right
 
 207                  *    before the snap_type.
 
 209                 p_ethhdr = (struct ethhdr *)
 
 210                     ((u8 *) & p_rx_pkt->eth803_hdr
 
 211                      + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
 
 212                      - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
 
 213                      - sizeof(p_rx_pkt->eth803_hdr.src_addr)
 
 214                      - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
 
 216                 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
 
 217                        sizeof(p_ethhdr->h_source));
 
 218                 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
 
 219                        sizeof(p_ethhdr->h_dest));
 
 221                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
 
 224                 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
 
 226                 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
 
 227                         (u8 *) & p_rx_pkt->rfc1042_hdr,
 
 228                         sizeof(p_rx_pkt->rfc1042_hdr));
 
 230                 /* Chop off the rxpd */
 
 231                 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
 
 234         /* Chop off the leading header bytes so the skb points to the start of
 
 235          *   either the reconstructed EthII frame or the 802.2/llc/snap frame
 
 237         skb_pull(skb, hdrchop);
 
 239         /* Take the data rate from the rxpd structure
 
 240          * only if the rate is auto
 
 242         if (priv->enablehwauto)
 
 243                 priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
 
 245         lbs_compute_rssi(priv, p_rx_pd);
 
 247         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
 
 248         dev->stats.rx_bytes += skb->len;
 
 249         dev->stats.rx_packets++;
 
 251         skb->protocol = eth_type_trans(skb, dev);
 
 259         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
 
 262 EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
 
 265  *  @brief This function converts Tx/Rx rates from the Marvell WLAN format
 
 266  *  (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
 
 268  *  @param rate    Input rate
 
 269  *  @return        Output Rate (0 if invalid)
 
 271 static u8 convert_mv_rate_to_radiotap(u8 rate)
 
 278         case 2:         /* 5.5 Mbps */
 
 280         case 3:         /*  11 Mbps */
 
 282         /* case 4: reserved */
 
 287         case 7:         /*  12 Mbps */
 
 289         case 8:         /*  18 Mbps */
 
 291         case 9:         /*  24 Mbps */
 
 293         case 10:                /*  36 Mbps */
 
 295         case 11:                /*  48 Mbps */
 
 297         case 12:                /*  54 Mbps */
 
 300         lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
 
 305  *  @brief This function processes a received 802.11 packet and forwards it
 
 306  *  to kernel/upper layer
 
 308  *  @param priv    A pointer to struct lbs_private
 
 309  *  @param skb     A pointer to skb which includes the received packet
 
 312 static int process_rxed_802_11_packet(struct lbs_private *priv,
 
 316         struct net_device *dev = priv->dev;
 
 317         struct rx80211packethdr *p_rx_pkt;
 
 319         struct rx_radiotap_hdr radiotap_hdr;
 
 320         struct rx_radiotap_hdr *pradiotap_hdr;
 
 322         lbs_deb_enter(LBS_DEB_RX);
 
 324         p_rx_pkt = (struct rx80211packethdr *) skb->data;
 
 325         prxpd = &p_rx_pkt->rx_pd;
 
 327         // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
 
 329         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
 
 330                 lbs_deb_rx("rx err: frame received with bad length\n");
 
 331                 dev->stats.rx_length_errors++;
 
 338          * Check rxpd status and update 802.3 stat,
 
 340         if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
 
 341                 //lbs_deb_rx("rx err: frame received with bad status\n");
 
 342                 dev->stats.rx_errors++;
 
 345         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
 
 346                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
 
 348         /* create the exported radio header */
 
 350         /* radiotap header */
 
 351         radiotap_hdr.hdr.it_version = 0;
 
 352         /* XXX must check this value for pad */
 
 353         radiotap_hdr.hdr.it_pad = 0;
 
 354         radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
 
 355         radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
 
 356         if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
 
 357                 radiotap_hdr.flags |= IEEE80211_RADIOTAP_F_BADFCS;
 
 358         radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
 
 359         /* XXX must check no carryout */
 
 360         radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
 
 363         skb_pull(skb, sizeof(struct rxpd));
 
 365         /* add space for the new radio header */
 
 366         if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
 
 367             pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
 
 368                 lbs_pr_alert("%s: couldn't pskb_expand_head\n", __func__);
 
 374         pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
 
 375         memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
 
 377         /* Take the data rate from the rxpd structure
 
 378          * only if the rate is auto
 
 380         if (priv->enablehwauto)
 
 381                 priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
 
 383         lbs_compute_rssi(priv, prxpd);
 
 385         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
 
 386         dev->stats.rx_bytes += skb->len;
 
 387         dev->stats.rx_packets++;
 
 389         skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
 
 395         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);