Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rtlwifi / usb.c
index 8df5836cc99c44f3bb482b6b8dcb5c5a1a75a227..83915dcd0e58505ca8970352e2fbac35dd1d48a8 100644 (file)
@@ -308,6 +308,8 @@ static int _rtl_usb_init_tx(struct ieee80211_hw *hw)
        return 0;
 }
 
+static void _rtl_rx_work(unsigned long param);
+
 static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
 {
        struct rtl_priv *rtlpriv = rtl_priv(hw);
@@ -325,6 +327,11 @@ static int _rtl_usb_init_rx(struct ieee80211_hw *hw)
                rtlusb->rx_max_size, rtlusb->rx_urb_num, rtlusb->in_ep);
        init_usb_anchor(&rtlusb->rx_submitted);
        init_usb_anchor(&rtlusb->rx_cleanup_urbs);
+
+       skb_queue_head_init(&rtlusb->rx_queue);
+       rtlusb->rx_work_tasklet.func = _rtl_rx_work;
+       rtlusb->rx_work_tasklet.data = (unsigned long)rtlusb;
+
        return 0;
 }
 
@@ -515,7 +522,7 @@ static void _rtl_usb_rx_process_noagg(struct ieee80211_hw *hw,
                }
 
                if (likely(rtl_action_proc(hw, skb, false)))
-                       ieee80211_rx_irqsafe(hw, skb);
+                       ieee80211_rx(hw, skb);
                else
                        dev_kfree_skb_any(skb);
        }
@@ -534,10 +541,65 @@ static void _rtl_rx_pre_process(struct ieee80211_hw *hw, struct sk_buff *skb)
        while (!skb_queue_empty(&rx_queue)) {
                _skb = skb_dequeue(&rx_queue);
                _rtl_usb_rx_process_agg(hw, _skb);
-               ieee80211_rx_irqsafe(hw, _skb);
+               ieee80211_rx(hw, _skb);
        }
 }
 
+#define __RX_SKB_MAX_QUEUED    32
+
+static void _rtl_rx_work(unsigned long param)
+{
+       struct rtl_usb *rtlusb = (struct rtl_usb *)param;
+       struct ieee80211_hw *hw = usb_get_intfdata(rtlusb->intf);
+       struct sk_buff *skb;
+
+       while ((skb = skb_dequeue(&rtlusb->rx_queue))) {
+               if (unlikely(IS_USB_STOP(rtlusb))) {
+                       dev_kfree_skb_any(skb);
+                       continue;
+               }
+
+               if (likely(!rtlusb->usb_rx_segregate_hdl)) {
+                       _rtl_usb_rx_process_noagg(hw, skb);
+               } else {
+                       /* TO DO */
+                       _rtl_rx_pre_process(hw, skb);
+                       pr_err("rx agg not supported\n");
+               }
+       }
+}
+
+static unsigned int _rtl_rx_get_padding(struct ieee80211_hdr *hdr,
+                                       unsigned int len)
+{
+       unsigned int padding = 0;
+
+       /* make function no-op when possible */
+       if (NET_IP_ALIGN == 0 || len < sizeof(*hdr))
+               return 0;
+
+       /* alignment calculation as in lbtf_rx() / carl9170_rx_copy_data() */
+       /* TODO: deduplicate common code, define helper function instead? */
+
+       if (ieee80211_is_data_qos(hdr->frame_control)) {
+               u8 *qc = ieee80211_get_qos_ctl(hdr);
+
+               padding ^= NET_IP_ALIGN;
+
+               /* Input might be invalid, avoid accessing memory outside
+                * the buffer.
+                */
+               if ((unsigned long)qc - (unsigned long)hdr < len &&
+                   *qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
+                       padding ^= NET_IP_ALIGN;
+       }
+
+       if (ieee80211_has_a4(hdr->frame_control))
+               padding ^= NET_IP_ALIGN;
+
+       return padding;
+}
+
 #define __RADIO_TAP_SIZE_RSV   32
 
 static void _rtl_rx_completed(struct urb *_urb)
@@ -551,8 +613,11 @@ static void _rtl_rx_completed(struct urb *_urb)
                goto free;
 
        if (likely(0 == _urb->status)) {
+               unsigned int padding;
                struct sk_buff *skb;
+               unsigned int qlen;
                unsigned int size = _urb->actual_length;
+               struct ieee80211_hdr *hdr;
 
                if (size < RTL_RX_DESC_SIZE + sizeof(struct ieee80211_hdr)) {
                        RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
@@ -561,7 +626,18 @@ static void _rtl_rx_completed(struct urb *_urb)
                        goto resubmit;
                }
 
-               skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV);
+               qlen = skb_queue_len(&rtlusb->rx_queue);
+               if (qlen >= __RX_SKB_MAX_QUEUED) {
+                       RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
+                                "Pending RX skbuff queue full! (qlen: %d)\n",
+                                qlen);
+                       goto resubmit;
+               }
+
+               hdr = (void *)(_urb->transfer_buffer + RTL_RX_DESC_SIZE);
+               padding = _rtl_rx_get_padding(hdr, size - RTL_RX_DESC_SIZE);
+
+               skb = dev_alloc_skb(size + __RADIO_TAP_SIZE_RSV + padding);
                if (!skb) {
                        RT_TRACE(rtlpriv, COMP_USB, DBG_EMERG,
                                 "Can't allocate skb for bulk IN!\n");
@@ -570,22 +646,16 @@ static void _rtl_rx_completed(struct urb *_urb)
 
                _rtl_install_trx_info(rtlusb, skb, rtlusb->in_ep);
 
+               /* Make sure the payload data is 4 byte aligned. */
+               skb_reserve(skb, padding);
+
                /* reserve some space for mac80211's radiotap */
                skb_reserve(skb, __RADIO_TAP_SIZE_RSV);
 
                memcpy(skb_put(skb, size), _urb->transfer_buffer, size);
 
-               /* TODO: Do further processing in tasklet (queue skbs,
-                * schedule tasklet)
-                */
-
-               if (likely(!rtlusb->usb_rx_segregate_hdl)) {
-                       _rtl_usb_rx_process_noagg(hw, skb);
-               } else {
-                       /* TO DO */
-                       _rtl_rx_pre_process(hw, skb);
-                       pr_err("rx agg not supported\n");
-               }
+               skb_queue_tail(&rtlusb->rx_queue, skb);
+               tasklet_schedule(&rtlusb->rx_work_tasklet);
 
                goto resubmit;
        }
@@ -626,6 +696,9 @@ static void _rtl_usb_cleanup_rx(struct ieee80211_hw *hw)
 
        usb_kill_anchored_urbs(&rtlusb->rx_submitted);
 
+       tasklet_kill(&rtlusb->rx_work_tasklet);
+       skb_queue_purge(&rtlusb->rx_queue);
+
        while ((urb = usb_get_from_anchor(&rtlusb->rx_cleanup_urbs))) {
                usb_free_coherent(urb->dev, urb->transfer_buffer_length,
                                urb->transfer_buffer, urb->transfer_dma);
@@ -859,6 +932,7 @@ static void _rtl_usb_transmit(struct ieee80211_hw *hw, struct sk_buff *skb,
        if (unlikely(!_urb)) {
                RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
                         "Can't allocate urb. Drop skb!\n");
+               kfree_skb(skb);
                return;
        }
        _rtl_submit_tx_urb(hw, _urb);