12be7d22fSVladimir Kondratiev /* 22be7d22fSVladimir Kondratiev * Copyright (c) 2012 Qualcomm Atheros, Inc. 32be7d22fSVladimir Kondratiev * 42be7d22fSVladimir Kondratiev * Permission to use, copy, modify, and/or distribute this software for any 52be7d22fSVladimir Kondratiev * purpose with or without fee is hereby granted, provided that the above 62be7d22fSVladimir Kondratiev * copyright notice and this permission notice appear in all copies. 72be7d22fSVladimir Kondratiev * 82be7d22fSVladimir Kondratiev * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 92be7d22fSVladimir Kondratiev * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 102be7d22fSVladimir Kondratiev * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 112be7d22fSVladimir Kondratiev * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 122be7d22fSVladimir Kondratiev * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 132be7d22fSVladimir Kondratiev * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 142be7d22fSVladimir Kondratiev * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 152be7d22fSVladimir Kondratiev */ 162be7d22fSVladimir Kondratiev 172be7d22fSVladimir Kondratiev #include <linux/etherdevice.h> 182be7d22fSVladimir Kondratiev #include <net/ieee80211_radiotap.h> 192be7d22fSVladimir Kondratiev #include <linux/if_arp.h> 202be7d22fSVladimir Kondratiev #include <linux/moduleparam.h> 21504937d4SKirshenbaum Erez #include <linux/ip.h> 22504937d4SKirshenbaum Erez #include <linux/ipv6.h> 23504937d4SKirshenbaum Erez #include <net/ipv6.h> 240786dc4eSVladimir Kondratiev #include <linux/prefetch.h> 252be7d22fSVladimir Kondratiev 262be7d22fSVladimir Kondratiev #include "wil6210.h" 272be7d22fSVladimir Kondratiev #include "wmi.h" 282be7d22fSVladimir Kondratiev #include "txrx.h" 2998658095SVladimir Kondratiev #include "trace.h" 302be7d22fSVladimir Kondratiev 312be7d22fSVladimir Kondratiev static bool rtap_include_phy_info; 322be7d22fSVladimir Kondratiev module_param(rtap_include_phy_info, bool, S_IRUGO); 332be7d22fSVladimir Kondratiev MODULE_PARM_DESC(rtap_include_phy_info, 342be7d22fSVladimir Kondratiev " Include PHY info in the radiotap header, default - no"); 352be7d22fSVladimir Kondratiev 362be7d22fSVladimir Kondratiev static inline int wil_vring_is_empty(struct vring *vring) 372be7d22fSVladimir Kondratiev { 382be7d22fSVladimir Kondratiev return vring->swhead == vring->swtail; 392be7d22fSVladimir Kondratiev } 402be7d22fSVladimir Kondratiev 412be7d22fSVladimir Kondratiev static inline u32 wil_vring_next_tail(struct vring *vring) 422be7d22fSVladimir Kondratiev { 432be7d22fSVladimir Kondratiev return (vring->swtail + 1) % vring->size; 442be7d22fSVladimir Kondratiev } 452be7d22fSVladimir Kondratiev 462be7d22fSVladimir Kondratiev static inline void wil_vring_advance_head(struct vring *vring, int n) 472be7d22fSVladimir Kondratiev { 482be7d22fSVladimir Kondratiev vring->swhead = (vring->swhead + n) % vring->size; 492be7d22fSVladimir Kondratiev } 502be7d22fSVladimir Kondratiev 512be7d22fSVladimir Kondratiev static inline int wil_vring_is_full(struct vring *vring) 522be7d22fSVladimir Kondratiev { 532be7d22fSVladimir Kondratiev return wil_vring_next_tail(vring) == vring->swhead; 542be7d22fSVladimir Kondratiev } 552be7d22fSVladimir Kondratiev /* 562be7d22fSVladimir Kondratiev * Available space in Tx Vring 572be7d22fSVladimir Kondratiev */ 582be7d22fSVladimir Kondratiev static inline int wil_vring_avail_tx(struct vring *vring) 592be7d22fSVladimir Kondratiev { 602be7d22fSVladimir Kondratiev u32 swhead = vring->swhead; 612be7d22fSVladimir Kondratiev u32 swtail = vring->swtail; 622be7d22fSVladimir Kondratiev int used = (vring->size + swhead - swtail) % vring->size; 632be7d22fSVladimir Kondratiev 642be7d22fSVladimir Kondratiev return vring->size - used - 1; 652be7d22fSVladimir Kondratiev } 662be7d22fSVladimir Kondratiev 672be7d22fSVladimir Kondratiev static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring) 682be7d22fSVladimir Kondratiev { 692be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 702be7d22fSVladimir Kondratiev size_t sz = vring->size * sizeof(vring->va[0]); 712be7d22fSVladimir Kondratiev uint i; 722be7d22fSVladimir Kondratiev 732be7d22fSVladimir Kondratiev BUILD_BUG_ON(sizeof(vring->va[0]) != 32); 742be7d22fSVladimir Kondratiev 752be7d22fSVladimir Kondratiev vring->swhead = 0; 762be7d22fSVladimir Kondratiev vring->swtail = 0; 77f88f113aSVladimir Kondratiev vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL); 782be7d22fSVladimir Kondratiev if (!vring->ctx) { 792be7d22fSVladimir Kondratiev vring->va = NULL; 802be7d22fSVladimir Kondratiev return -ENOMEM; 812be7d22fSVladimir Kondratiev } 822be7d22fSVladimir Kondratiev /* 832be7d22fSVladimir Kondratiev * vring->va should be aligned on its size rounded up to power of 2 842be7d22fSVladimir Kondratiev * This is granted by the dma_alloc_coherent 852be7d22fSVladimir Kondratiev */ 862be7d22fSVladimir Kondratiev vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL); 872be7d22fSVladimir Kondratiev if (!vring->va) { 882be7d22fSVladimir Kondratiev kfree(vring->ctx); 892be7d22fSVladimir Kondratiev vring->ctx = NULL; 902be7d22fSVladimir Kondratiev return -ENOMEM; 912be7d22fSVladimir Kondratiev } 922be7d22fSVladimir Kondratiev /* initially, all descriptors are SW owned 932be7d22fSVladimir Kondratiev * For Tx and Rx, ownership bit is at the same location, thus 942be7d22fSVladimir Kondratiev * we can use any 952be7d22fSVladimir Kondratiev */ 962be7d22fSVladimir Kondratiev for (i = 0; i < vring->size; i++) { 9768ada71eSVladimir Kondratiev volatile struct vring_tx_desc *_d = &(vring->va[i].tx); 9868ada71eSVladimir Kondratiev _d->dma.status = TX_DMA_STATUS_DU; 992be7d22fSVladimir Kondratiev } 1002be7d22fSVladimir Kondratiev 1017743882dSVladimir Kondratiev wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size, 1022be7d22fSVladimir Kondratiev vring->va, (unsigned long long)vring->pa, vring->ctx); 1032be7d22fSVladimir Kondratiev 1042be7d22fSVladimir Kondratiev return 0; 1052be7d22fSVladimir Kondratiev } 1062be7d22fSVladimir Kondratiev 1072232abd5SVladimir Kondratiev static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d, 1082232abd5SVladimir Kondratiev struct wil_ctx *ctx) 1092232abd5SVladimir Kondratiev { 1102232abd5SVladimir Kondratiev dma_addr_t pa = wil_desc_addr(&d->dma.addr); 1112232abd5SVladimir Kondratiev u16 dmalen = le16_to_cpu(d->dma.length); 1122232abd5SVladimir Kondratiev switch (ctx->mapped_as) { 1132232abd5SVladimir Kondratiev case wil_mapped_as_single: 1142232abd5SVladimir Kondratiev dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE); 1152232abd5SVladimir Kondratiev break; 1162232abd5SVladimir Kondratiev case wil_mapped_as_page: 1172232abd5SVladimir Kondratiev dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE); 1182232abd5SVladimir Kondratiev break; 1192232abd5SVladimir Kondratiev default: 1202232abd5SVladimir Kondratiev break; 1212232abd5SVladimir Kondratiev } 1222232abd5SVladimir Kondratiev } 1232232abd5SVladimir Kondratiev 1242be7d22fSVladimir Kondratiev static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring, 1252be7d22fSVladimir Kondratiev int tx) 1262be7d22fSVladimir Kondratiev { 1272be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 1282be7d22fSVladimir Kondratiev size_t sz = vring->size * sizeof(vring->va[0]); 1292be7d22fSVladimir Kondratiev 1302be7d22fSVladimir Kondratiev while (!wil_vring_is_empty(vring)) { 13168ada71eSVladimir Kondratiev dma_addr_t pa; 1327e594444SVladimir Kondratiev u16 dmalen; 133f88f113aSVladimir Kondratiev struct wil_ctx *ctx; 13468ada71eSVladimir Kondratiev 1352be7d22fSVladimir Kondratiev if (tx) { 13668ada71eSVladimir Kondratiev struct vring_tx_desc dd, *d = ⅆ 13768ada71eSVladimir Kondratiev volatile struct vring_tx_desc *_d = 1382be7d22fSVladimir Kondratiev &vring->va[vring->swtail].tx; 13968ada71eSVladimir Kondratiev 140f88f113aSVladimir Kondratiev ctx = &vring->ctx[vring->swtail]; 14168ada71eSVladimir Kondratiev *d = *_d; 1422232abd5SVladimir Kondratiev wil_txdesc_unmap(dev, d, ctx); 143f88f113aSVladimir Kondratiev if (ctx->skb) 144f88f113aSVladimir Kondratiev dev_kfree_skb_any(ctx->skb); 1452be7d22fSVladimir Kondratiev vring->swtail = wil_vring_next_tail(vring); 1462be7d22fSVladimir Kondratiev } else { /* rx */ 14768ada71eSVladimir Kondratiev struct vring_rx_desc dd, *d = ⅆ 14868ada71eSVladimir Kondratiev volatile struct vring_rx_desc *_d = 1494d1ac072SVladimir Kondratiev &vring->va[vring->swhead].rx; 15068ada71eSVladimir Kondratiev 151f88f113aSVladimir Kondratiev ctx = &vring->ctx[vring->swhead]; 15268ada71eSVladimir Kondratiev *d = *_d; 15368ada71eSVladimir Kondratiev pa = wil_desc_addr(&d->dma.addr); 1547e594444SVladimir Kondratiev dmalen = le16_to_cpu(d->dma.length); 1557e594444SVladimir Kondratiev dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE); 156f88f113aSVladimir Kondratiev kfree_skb(ctx->skb); 1572be7d22fSVladimir Kondratiev wil_vring_advance_head(vring, 1); 1582be7d22fSVladimir Kondratiev } 1592be7d22fSVladimir Kondratiev } 1602be7d22fSVladimir Kondratiev dma_free_coherent(dev, sz, (void *)vring->va, vring->pa); 1612be7d22fSVladimir Kondratiev kfree(vring->ctx); 1622be7d22fSVladimir Kondratiev vring->pa = 0; 1632be7d22fSVladimir Kondratiev vring->va = NULL; 1642be7d22fSVladimir Kondratiev vring->ctx = NULL; 1652be7d22fSVladimir Kondratiev } 1662be7d22fSVladimir Kondratiev 1672be7d22fSVladimir Kondratiev /** 1682be7d22fSVladimir Kondratiev * Allocate one skb for Rx VRING 1692be7d22fSVladimir Kondratiev * 1702be7d22fSVladimir Kondratiev * Safe to call from IRQ 1712be7d22fSVladimir Kondratiev */ 1722be7d22fSVladimir Kondratiev static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring, 1732be7d22fSVladimir Kondratiev u32 i, int headroom) 1742be7d22fSVladimir Kondratiev { 1752be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 1762be7d22fSVladimir Kondratiev unsigned int sz = RX_BUF_LEN; 17768ada71eSVladimir Kondratiev struct vring_rx_desc dd, *d = ⅆ 17868ada71eSVladimir Kondratiev volatile struct vring_rx_desc *_d = &(vring->va[i].rx); 1792be7d22fSVladimir Kondratiev dma_addr_t pa; 1802be7d22fSVladimir Kondratiev 1812be7d22fSVladimir Kondratiev /* TODO align */ 1822be7d22fSVladimir Kondratiev struct sk_buff *skb = dev_alloc_skb(sz + headroom); 1832be7d22fSVladimir Kondratiev if (unlikely(!skb)) 1842be7d22fSVladimir Kondratiev return -ENOMEM; 1852be7d22fSVladimir Kondratiev 1862be7d22fSVladimir Kondratiev skb_reserve(skb, headroom); 1872be7d22fSVladimir Kondratiev skb_put(skb, sz); 1882be7d22fSVladimir Kondratiev 1892be7d22fSVladimir Kondratiev pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE); 1902be7d22fSVladimir Kondratiev if (unlikely(dma_mapping_error(dev, pa))) { 1912be7d22fSVladimir Kondratiev kfree_skb(skb); 1922be7d22fSVladimir Kondratiev return -ENOMEM; 1932be7d22fSVladimir Kondratiev } 1942be7d22fSVladimir Kondratiev 1952be7d22fSVladimir Kondratiev d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT; 19668ada71eSVladimir Kondratiev wil_desc_addr_set(&d->dma.addr, pa); 1972be7d22fSVladimir Kondratiev /* ip_length don't care */ 1982be7d22fSVladimir Kondratiev /* b11 don't care */ 1992be7d22fSVladimir Kondratiev /* error don't care */ 2002be7d22fSVladimir Kondratiev d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */ 2017e594444SVladimir Kondratiev d->dma.length = cpu_to_le16(sz); 20268ada71eSVladimir Kondratiev *_d = *d; 203f88f113aSVladimir Kondratiev vring->ctx[i].skb = skb; 2042be7d22fSVladimir Kondratiev 2052be7d22fSVladimir Kondratiev return 0; 2062be7d22fSVladimir Kondratiev } 2072be7d22fSVladimir Kondratiev 2082be7d22fSVladimir Kondratiev /** 2092be7d22fSVladimir Kondratiev * Adds radiotap header 2102be7d22fSVladimir Kondratiev * 2112be7d22fSVladimir Kondratiev * Any error indicated as "Bad FCS" 2122be7d22fSVladimir Kondratiev * 2132be7d22fSVladimir Kondratiev * Vendor data for 04:ce:14-1 (Wilocity-1) consists of: 2142be7d22fSVladimir Kondratiev * - Rx descriptor: 32 bytes 2152be7d22fSVladimir Kondratiev * - Phy info 2162be7d22fSVladimir Kondratiev */ 2172be7d22fSVladimir Kondratiev static void wil_rx_add_radiotap_header(struct wil6210_priv *wil, 21833e61169SVladimir Kondratiev struct sk_buff *skb) 2192be7d22fSVladimir Kondratiev { 2202be7d22fSVladimir Kondratiev struct wireless_dev *wdev = wil->wdev; 2212be7d22fSVladimir Kondratiev struct wil6210_rtap { 2222be7d22fSVladimir Kondratiev struct ieee80211_radiotap_header rthdr; 2232be7d22fSVladimir Kondratiev /* fields should be in the order of bits in rthdr.it_present */ 2242be7d22fSVladimir Kondratiev /* flags */ 2252be7d22fSVladimir Kondratiev u8 flags; 2262be7d22fSVladimir Kondratiev /* channel */ 2272be7d22fSVladimir Kondratiev __le16 chnl_freq __aligned(2); 2282be7d22fSVladimir Kondratiev __le16 chnl_flags; 2292be7d22fSVladimir Kondratiev /* MCS */ 2302be7d22fSVladimir Kondratiev u8 mcs_present; 2312be7d22fSVladimir Kondratiev u8 mcs_flags; 2322be7d22fSVladimir Kondratiev u8 mcs_index; 2332be7d22fSVladimir Kondratiev } __packed; 2342be7d22fSVladimir Kondratiev struct wil6210_rtap_vendor { 2352be7d22fSVladimir Kondratiev struct wil6210_rtap rtap; 2362be7d22fSVladimir Kondratiev /* vendor */ 2372be7d22fSVladimir Kondratiev u8 vendor_oui[3] __aligned(2); 2382be7d22fSVladimir Kondratiev u8 vendor_ns; 2392be7d22fSVladimir Kondratiev __le16 vendor_skip; 2402be7d22fSVladimir Kondratiev u8 vendor_data[0]; 2412be7d22fSVladimir Kondratiev } __packed; 24233e61169SVladimir Kondratiev struct vring_rx_desc *d = wil_skb_rxdesc(skb); 2432be7d22fSVladimir Kondratiev struct wil6210_rtap_vendor *rtap_vendor; 2442be7d22fSVladimir Kondratiev int rtap_len = sizeof(struct wil6210_rtap); 2452be7d22fSVladimir Kondratiev int phy_length = 0; /* phy info header size, bytes */ 2462be7d22fSVladimir Kondratiev static char phy_data[128]; 2472be7d22fSVladimir Kondratiev struct ieee80211_channel *ch = wdev->preset_chandef.chan; 2482be7d22fSVladimir Kondratiev 2492be7d22fSVladimir Kondratiev if (rtap_include_phy_info) { 2502be7d22fSVladimir Kondratiev rtap_len = sizeof(*rtap_vendor) + sizeof(*d); 2512be7d22fSVladimir Kondratiev /* calculate additional length */ 2522be7d22fSVladimir Kondratiev if (d->dma.status & RX_DMA_STATUS_PHY_INFO) { 2532be7d22fSVladimir Kondratiev /** 2542be7d22fSVladimir Kondratiev * PHY info starts from 8-byte boundary 2552be7d22fSVladimir Kondratiev * there are 8-byte lines, last line may be partially 2562be7d22fSVladimir Kondratiev * written (HW bug), thus FW configures for last line 2572be7d22fSVladimir Kondratiev * to be excessive. Driver skips this last line. 2582be7d22fSVladimir Kondratiev */ 2592be7d22fSVladimir Kondratiev int len = min_t(int, 8 + sizeof(phy_data), 2602be7d22fSVladimir Kondratiev wil_rxdesc_phy_length(d)); 2612be7d22fSVladimir Kondratiev if (len > 8) { 2622be7d22fSVladimir Kondratiev void *p = skb_tail_pointer(skb); 2632be7d22fSVladimir Kondratiev void *pa = PTR_ALIGN(p, 8); 2642be7d22fSVladimir Kondratiev if (skb_tailroom(skb) >= len + (pa - p)) { 2652be7d22fSVladimir Kondratiev phy_length = len - 8; 2662be7d22fSVladimir Kondratiev memcpy(phy_data, pa, phy_length); 2672be7d22fSVladimir Kondratiev } 2682be7d22fSVladimir Kondratiev } 2692be7d22fSVladimir Kondratiev } 2702be7d22fSVladimir Kondratiev rtap_len += phy_length; 2712be7d22fSVladimir Kondratiev } 2722be7d22fSVladimir Kondratiev 2732be7d22fSVladimir Kondratiev if (skb_headroom(skb) < rtap_len && 2742be7d22fSVladimir Kondratiev pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) { 2752be7d22fSVladimir Kondratiev wil_err(wil, "Unable to expand headrom to %d\n", rtap_len); 2762be7d22fSVladimir Kondratiev return; 2772be7d22fSVladimir Kondratiev } 2782be7d22fSVladimir Kondratiev 2792be7d22fSVladimir Kondratiev rtap_vendor = (void *)skb_push(skb, rtap_len); 2802be7d22fSVladimir Kondratiev memset(rtap_vendor, 0, rtap_len); 2812be7d22fSVladimir Kondratiev 2822be7d22fSVladimir Kondratiev rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION; 2832be7d22fSVladimir Kondratiev rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len); 2842be7d22fSVladimir Kondratiev rtap_vendor->rtap.rthdr.it_present = cpu_to_le32( 2852be7d22fSVladimir Kondratiev (1 << IEEE80211_RADIOTAP_FLAGS) | 2862be7d22fSVladimir Kondratiev (1 << IEEE80211_RADIOTAP_CHANNEL) | 2872be7d22fSVladimir Kondratiev (1 << IEEE80211_RADIOTAP_MCS)); 2882be7d22fSVladimir Kondratiev if (d->dma.status & RX_DMA_STATUS_ERROR) 2892be7d22fSVladimir Kondratiev rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS; 2902be7d22fSVladimir Kondratiev 2912be7d22fSVladimir Kondratiev rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320); 2922be7d22fSVladimir Kondratiev rtap_vendor->rtap.chnl_flags = cpu_to_le16(0); 2932be7d22fSVladimir Kondratiev 2942be7d22fSVladimir Kondratiev rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS; 2952be7d22fSVladimir Kondratiev rtap_vendor->rtap.mcs_flags = 0; 2962be7d22fSVladimir Kondratiev rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d); 2972be7d22fSVladimir Kondratiev 2982be7d22fSVladimir Kondratiev if (rtap_include_phy_info) { 2992be7d22fSVladimir Kondratiev rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 << 3002be7d22fSVladimir Kondratiev IEEE80211_RADIOTAP_VENDOR_NAMESPACE); 3012be7d22fSVladimir Kondratiev /* OUI for Wilocity 04:ce:14 */ 3022be7d22fSVladimir Kondratiev rtap_vendor->vendor_oui[0] = 0x04; 3032be7d22fSVladimir Kondratiev rtap_vendor->vendor_oui[1] = 0xce; 3042be7d22fSVladimir Kondratiev rtap_vendor->vendor_oui[2] = 0x14; 3052be7d22fSVladimir Kondratiev rtap_vendor->vendor_ns = 1; 3062be7d22fSVladimir Kondratiev /* Rx descriptor + PHY data */ 3072be7d22fSVladimir Kondratiev rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) + 3082be7d22fSVladimir Kondratiev phy_length); 3092be7d22fSVladimir Kondratiev memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d)); 3102be7d22fSVladimir Kondratiev memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data, 3112be7d22fSVladimir Kondratiev phy_length); 3122be7d22fSVladimir Kondratiev } 3132be7d22fSVladimir Kondratiev } 3142be7d22fSVladimir Kondratiev 3152be7d22fSVladimir Kondratiev /* 3162be7d22fSVladimir Kondratiev * Fast swap in place between 2 registers 3172be7d22fSVladimir Kondratiev */ 3182be7d22fSVladimir Kondratiev static void wil_swap_u16(u16 *a, u16 *b) 3192be7d22fSVladimir Kondratiev { 3202be7d22fSVladimir Kondratiev *a ^= *b; 3212be7d22fSVladimir Kondratiev *b ^= *a; 3222be7d22fSVladimir Kondratiev *a ^= *b; 3232be7d22fSVladimir Kondratiev } 3242be7d22fSVladimir Kondratiev 3252be7d22fSVladimir Kondratiev static void wil_swap_ethaddr(void *data) 3262be7d22fSVladimir Kondratiev { 3272be7d22fSVladimir Kondratiev struct ethhdr *eth = data; 3282be7d22fSVladimir Kondratiev u16 *s = (u16 *)eth->h_source; 3292be7d22fSVladimir Kondratiev u16 *d = (u16 *)eth->h_dest; 3302be7d22fSVladimir Kondratiev 3312be7d22fSVladimir Kondratiev wil_swap_u16(s++, d++); 3322be7d22fSVladimir Kondratiev wil_swap_u16(s++, d++); 3332be7d22fSVladimir Kondratiev wil_swap_u16(s, d); 3342be7d22fSVladimir Kondratiev } 3352be7d22fSVladimir Kondratiev 3362be7d22fSVladimir Kondratiev /** 3372be7d22fSVladimir Kondratiev * reap 1 frame from @swhead 3382be7d22fSVladimir Kondratiev * 33933e61169SVladimir Kondratiev * Rx descriptor copied to skb->cb 34033e61169SVladimir Kondratiev * 3412be7d22fSVladimir Kondratiev * Safe to call from IRQ 3422be7d22fSVladimir Kondratiev */ 3432be7d22fSVladimir Kondratiev static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil, 3442be7d22fSVladimir Kondratiev struct vring *vring) 3452be7d22fSVladimir Kondratiev { 3462be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 3472be7d22fSVladimir Kondratiev struct net_device *ndev = wil_to_ndev(wil); 34868ada71eSVladimir Kondratiev volatile struct vring_rx_desc *_d; 34968ada71eSVladimir Kondratiev struct vring_rx_desc *d; 3502be7d22fSVladimir Kondratiev struct sk_buff *skb; 3512be7d22fSVladimir Kondratiev dma_addr_t pa; 3522be7d22fSVladimir Kondratiev unsigned int sz = RX_BUF_LEN; 3537e594444SVladimir Kondratiev u16 dmalen; 3542be7d22fSVladimir Kondratiev u8 ftype; 3552be7d22fSVladimir Kondratiev u8 ds_bits; 356c8b78b5fSVladimir Kondratiev int cid; 357c8b78b5fSVladimir Kondratiev struct wil_net_stats *stats; 358c8b78b5fSVladimir Kondratiev 3592be7d22fSVladimir Kondratiev 36033e61169SVladimir Kondratiev BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb)); 36133e61169SVladimir Kondratiev 3622be7d22fSVladimir Kondratiev if (wil_vring_is_empty(vring)) 3632be7d22fSVladimir Kondratiev return NULL; 3642be7d22fSVladimir Kondratiev 36568ada71eSVladimir Kondratiev _d = &(vring->va[vring->swhead].rx); 36668ada71eSVladimir Kondratiev if (!(_d->dma.status & RX_DMA_STATUS_DU)) { 3672be7d22fSVladimir Kondratiev /* it is not error, we just reached end of Rx done area */ 3682be7d22fSVladimir Kondratiev return NULL; 3692be7d22fSVladimir Kondratiev } 3702be7d22fSVladimir Kondratiev 371f88f113aSVladimir Kondratiev skb = vring->ctx[vring->swhead].skb; 37268ada71eSVladimir Kondratiev d = wil_skb_rxdesc(skb); 37368ada71eSVladimir Kondratiev *d = *_d; 37468ada71eSVladimir Kondratiev pa = wil_desc_addr(&d->dma.addr); 375f88f113aSVladimir Kondratiev vring->ctx[vring->swhead].skb = NULL; 376e270045bSVladimir Kondratiev wil_vring_advance_head(vring, 1); 37768ada71eSVladimir Kondratiev 37868ada71eSVladimir Kondratiev dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE); 37968ada71eSVladimir Kondratiev dmalen = le16_to_cpu(d->dma.length); 38068ada71eSVladimir Kondratiev 38168ada71eSVladimir Kondratiev trace_wil6210_rx(vring->swhead, d); 38268ada71eSVladimir Kondratiev wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen); 38368ada71eSVladimir Kondratiev wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4, 38468ada71eSVladimir Kondratiev (const void *)d, sizeof(*d), false); 38568ada71eSVladimir Kondratiev 386e270045bSVladimir Kondratiev if (dmalen > sz) { 387e270045bSVladimir Kondratiev wil_err(wil, "Rx size too large: %d bytes!\n", dmalen); 388110dea00SWei Yongjun kfree_skb(skb); 389e270045bSVladimir Kondratiev return NULL; 390e270045bSVladimir Kondratiev } 3917e594444SVladimir Kondratiev skb_trim(skb, dmalen); 39233e61169SVladimir Kondratiev 3931cbbcb08SVladimir Kondratiev prefetch(skb->data); 3941cbbcb08SVladimir Kondratiev 395c0d37713SVladimir Kondratiev wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1, 396c0d37713SVladimir Kondratiev skb->data, skb_headlen(skb), false); 397c0d37713SVladimir Kondratiev 398c8b78b5fSVladimir Kondratiev cid = wil_rxdesc_cid(d); 399c8b78b5fSVladimir Kondratiev stats = &wil->sta[cid].stats; 400c8b78b5fSVladimir Kondratiev stats->last_mcs_rx = wil_rxdesc_mcs(d); 401c8b78b5fSVladimir Kondratiev wil->stats.last_mcs_rx = stats->last_mcs_rx; 4022be7d22fSVladimir Kondratiev 4032be7d22fSVladimir Kondratiev /* use radiotap header only if required */ 4042be7d22fSVladimir Kondratiev if (ndev->type == ARPHRD_IEEE80211_RADIOTAP) 40533e61169SVladimir Kondratiev wil_rx_add_radiotap_header(wil, skb); 4062be7d22fSVladimir Kondratiev 4072be7d22fSVladimir Kondratiev /* no extra checks if in sniffer mode */ 4082be7d22fSVladimir Kondratiev if (ndev->type != ARPHRD_ETHER) 4092be7d22fSVladimir Kondratiev return skb; 4102be7d22fSVladimir Kondratiev /* 4112be7d22fSVladimir Kondratiev * Non-data frames may be delivered through Rx DMA channel (ex: BAR) 4122be7d22fSVladimir Kondratiev * Driver should recognize it by frame type, that is found 4132be7d22fSVladimir Kondratiev * in Rx descriptor. If type is not data, it is 802.11 frame as is 4142be7d22fSVladimir Kondratiev */ 41568ada71eSVladimir Kondratiev ftype = wil_rxdesc_ftype(d) << 2; 4162be7d22fSVladimir Kondratiev if (ftype != IEEE80211_FTYPE_DATA) { 4177743882dSVladimir Kondratiev wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype); 4182be7d22fSVladimir Kondratiev /* TODO: process it */ 4192be7d22fSVladimir Kondratiev kfree_skb(skb); 4202be7d22fSVladimir Kondratiev return NULL; 4212be7d22fSVladimir Kondratiev } 4222be7d22fSVladimir Kondratiev 4232be7d22fSVladimir Kondratiev if (skb->len < ETH_HLEN) { 4242be7d22fSVladimir Kondratiev wil_err(wil, "Short frame, len = %d\n", skb->len); 4252be7d22fSVladimir Kondratiev /* TODO: process it (i.e. BAR) */ 4262be7d22fSVladimir Kondratiev kfree_skb(skb); 4272be7d22fSVladimir Kondratiev return NULL; 4282be7d22fSVladimir Kondratiev } 4292be7d22fSVladimir Kondratiev 430504937d4SKirshenbaum Erez /* L4 IDENT is on when HW calculated checksum, check status 431504937d4SKirshenbaum Erez * and in case of error drop the packet 432504937d4SKirshenbaum Erez * higher stack layers will handle retransmission (if required) 433504937d4SKirshenbaum Erez */ 434504937d4SKirshenbaum Erez if (d->dma.status & RX_DMA_STATUS_L4_IDENT) { 435504937d4SKirshenbaum Erez /* L4 protocol identified, csum calculated */ 4364a68ab10SVladimir Kondratiev if ((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0) 437504937d4SKirshenbaum Erez skb->ip_summed = CHECKSUM_UNNECESSARY; 4384a68ab10SVladimir Kondratiev /* If HW reports bad checksum, let IP stack re-check it 4394a68ab10SVladimir Kondratiev * For example, HW don't understand Microsoft IP stack that 4404a68ab10SVladimir Kondratiev * mis-calculates TCP checksum - if it should be 0x0, 4414a68ab10SVladimir Kondratiev * it writes 0xffff in violation of RFC 1624 4424a68ab10SVladimir Kondratiev */ 443504937d4SKirshenbaum Erez } 444504937d4SKirshenbaum Erez 44568ada71eSVladimir Kondratiev ds_bits = wil_rxdesc_ds_bits(d); 4462be7d22fSVladimir Kondratiev if (ds_bits == 1) { 4472be7d22fSVladimir Kondratiev /* 4482be7d22fSVladimir Kondratiev * HW bug - in ToDS mode, i.e. Rx on AP side, 4492be7d22fSVladimir Kondratiev * addresses get swapped 4502be7d22fSVladimir Kondratiev */ 4512be7d22fSVladimir Kondratiev wil_swap_ethaddr(skb->data); 4522be7d22fSVladimir Kondratiev } 4532be7d22fSVladimir Kondratiev 4542be7d22fSVladimir Kondratiev return skb; 4552be7d22fSVladimir Kondratiev } 4562be7d22fSVladimir Kondratiev 4572be7d22fSVladimir Kondratiev /** 4582be7d22fSVladimir Kondratiev * allocate and fill up to @count buffers in rx ring 4592be7d22fSVladimir Kondratiev * buffers posted at @swtail 4602be7d22fSVladimir Kondratiev */ 4612be7d22fSVladimir Kondratiev static int wil_rx_refill(struct wil6210_priv *wil, int count) 4622be7d22fSVladimir Kondratiev { 4632be7d22fSVladimir Kondratiev struct net_device *ndev = wil_to_ndev(wil); 4642be7d22fSVladimir Kondratiev struct vring *v = &wil->vring_rx; 4652be7d22fSVladimir Kondratiev u32 next_tail; 4662be7d22fSVladimir Kondratiev int rc = 0; 4672be7d22fSVladimir Kondratiev int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ? 4682be7d22fSVladimir Kondratiev WIL6210_RTAP_SIZE : 0; 4692be7d22fSVladimir Kondratiev 4702be7d22fSVladimir Kondratiev for (; next_tail = wil_vring_next_tail(v), 4712be7d22fSVladimir Kondratiev (next_tail != v->swhead) && (count-- > 0); 4722be7d22fSVladimir Kondratiev v->swtail = next_tail) { 4732be7d22fSVladimir Kondratiev rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom); 4742be7d22fSVladimir Kondratiev if (rc) { 4752be7d22fSVladimir Kondratiev wil_err(wil, "Error %d in wil_rx_refill[%d]\n", 4762be7d22fSVladimir Kondratiev rc, v->swtail); 4772be7d22fSVladimir Kondratiev break; 4782be7d22fSVladimir Kondratiev } 4792be7d22fSVladimir Kondratiev } 4802be7d22fSVladimir Kondratiev iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail)); 4812be7d22fSVladimir Kondratiev 4822be7d22fSVladimir Kondratiev return rc; 4832be7d22fSVladimir Kondratiev } 4842be7d22fSVladimir Kondratiev 4852be7d22fSVladimir Kondratiev /* 4862be7d22fSVladimir Kondratiev * Pass Rx packet to the netif. Update statistics. 487e0287c4aSVladimir Kondratiev * Called in softirq context (NAPI poll). 4882be7d22fSVladimir Kondratiev */ 489b4490f42SVladimir Kondratiev void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev) 4902be7d22fSVladimir Kondratiev { 4912be7d22fSVladimir Kondratiev int rc; 492c8b78b5fSVladimir Kondratiev struct wil6210_priv *wil = ndev_to_wil(ndev); 4932be7d22fSVladimir Kondratiev unsigned int len = skb->len; 494c8b78b5fSVladimir Kondratiev struct vring_rx_desc *d = wil_skb_rxdesc(skb); 495c8b78b5fSVladimir Kondratiev int cid = wil_rxdesc_cid(d); 496c8b78b5fSVladimir Kondratiev struct wil_net_stats *stats = &wil->sta[cid].stats; 4972be7d22fSVladimir Kondratiev 498241804cbSVladimir Kondratiev skb_orphan(skb); 499241804cbSVladimir Kondratiev 500e0287c4aSVladimir Kondratiev rc = netif_receive_skb(skb); 5012be7d22fSVladimir Kondratiev 5022be7d22fSVladimir Kondratiev if (likely(rc == NET_RX_SUCCESS)) { 5032be7d22fSVladimir Kondratiev ndev->stats.rx_packets++; 504c8b78b5fSVladimir Kondratiev stats->rx_packets++; 5052be7d22fSVladimir Kondratiev ndev->stats.rx_bytes += len; 506c8b78b5fSVladimir Kondratiev stats->rx_bytes += len; 5072be7d22fSVladimir Kondratiev 5082be7d22fSVladimir Kondratiev } else { 5092be7d22fSVladimir Kondratiev ndev->stats.rx_dropped++; 510c8b78b5fSVladimir Kondratiev stats->rx_dropped++; 5112be7d22fSVladimir Kondratiev } 5122be7d22fSVladimir Kondratiev } 5132be7d22fSVladimir Kondratiev 5142be7d22fSVladimir Kondratiev /** 5152be7d22fSVladimir Kondratiev * Proceed all completed skb's from Rx VRING 5162be7d22fSVladimir Kondratiev * 517e0287c4aSVladimir Kondratiev * Safe to call from NAPI poll, i.e. softirq with interrupts enabled 5182be7d22fSVladimir Kondratiev */ 519e0287c4aSVladimir Kondratiev void wil_rx_handle(struct wil6210_priv *wil, int *quota) 5202be7d22fSVladimir Kondratiev { 5212be7d22fSVladimir Kondratiev struct net_device *ndev = wil_to_ndev(wil); 5222be7d22fSVladimir Kondratiev struct vring *v = &wil->vring_rx; 5232be7d22fSVladimir Kondratiev struct sk_buff *skb; 5242be7d22fSVladimir Kondratiev 5252be7d22fSVladimir Kondratiev if (!v->va) { 5262be7d22fSVladimir Kondratiev wil_err(wil, "Rx IRQ while Rx not yet initialized\n"); 5272be7d22fSVladimir Kondratiev return; 5282be7d22fSVladimir Kondratiev } 5297743882dSVladimir Kondratiev wil_dbg_txrx(wil, "%s()\n", __func__); 530e0287c4aSVladimir Kondratiev while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) { 531e0287c4aSVladimir Kondratiev (*quota)--; 5322be7d22fSVladimir Kondratiev 5332be7d22fSVladimir Kondratiev if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { 5342be7d22fSVladimir Kondratiev skb->dev = ndev; 5352be7d22fSVladimir Kondratiev skb_reset_mac_header(skb); 5362be7d22fSVladimir Kondratiev skb->ip_summed = CHECKSUM_UNNECESSARY; 5372be7d22fSVladimir Kondratiev skb->pkt_type = PACKET_OTHERHOST; 5382be7d22fSVladimir Kondratiev skb->protocol = htons(ETH_P_802_2); 539b4490f42SVladimir Kondratiev wil_netif_rx_any(skb, ndev); 5402be7d22fSVladimir Kondratiev } else { 5410bbc4adeSVladimir Kondratiev struct ethhdr *eth = (void *)skb->data; 5420bbc4adeSVladimir Kondratiev 5432be7d22fSVladimir Kondratiev skb->protocol = eth_type_trans(skb, ndev); 5440bbc4adeSVladimir Kondratiev 5450bbc4adeSVladimir Kondratiev if (is_unicast_ether_addr(eth->h_dest)) 546b4490f42SVladimir Kondratiev wil_rx_reorder(wil, skb); 5470bbc4adeSVladimir Kondratiev else 5480bbc4adeSVladimir Kondratiev wil_netif_rx_any(skb, ndev); 5492be7d22fSVladimir Kondratiev } 5502be7d22fSVladimir Kondratiev 5512be7d22fSVladimir Kondratiev } 5522be7d22fSVladimir Kondratiev wil_rx_refill(wil, v->size); 5532be7d22fSVladimir Kondratiev } 5542be7d22fSVladimir Kondratiev 5552be7d22fSVladimir Kondratiev int wil_rx_init(struct wil6210_priv *wil) 5562be7d22fSVladimir Kondratiev { 5572be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_rx; 5582be7d22fSVladimir Kondratiev int rc; 5592be7d22fSVladimir Kondratiev 5602be7d22fSVladimir Kondratiev vring->size = WIL6210_RX_RING_SIZE; 5612be7d22fSVladimir Kondratiev rc = wil_vring_alloc(wil, vring); 5622be7d22fSVladimir Kondratiev if (rc) 5632be7d22fSVladimir Kondratiev return rc; 5642be7d22fSVladimir Kondratiev 56547e19af9SVladimir Kondratiev rc = wmi_rx_chain_add(wil, vring); 5662be7d22fSVladimir Kondratiev if (rc) 5672be7d22fSVladimir Kondratiev goto err_free; 5682be7d22fSVladimir Kondratiev 5692be7d22fSVladimir Kondratiev rc = wil_rx_refill(wil, vring->size); 5702be7d22fSVladimir Kondratiev if (rc) 5712be7d22fSVladimir Kondratiev goto err_free; 5722be7d22fSVladimir Kondratiev 5732be7d22fSVladimir Kondratiev return 0; 5742be7d22fSVladimir Kondratiev err_free: 5752be7d22fSVladimir Kondratiev wil_vring_free(wil, vring, 0); 5762be7d22fSVladimir Kondratiev 5772be7d22fSVladimir Kondratiev return rc; 5782be7d22fSVladimir Kondratiev } 5792be7d22fSVladimir Kondratiev 5802be7d22fSVladimir Kondratiev void wil_rx_fini(struct wil6210_priv *wil) 5812be7d22fSVladimir Kondratiev { 5822be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_rx; 5832be7d22fSVladimir Kondratiev 5842acb4220SVladimir Kondratiev if (vring->va) 5852be7d22fSVladimir Kondratiev wil_vring_free(wil, vring, 0); 5862be7d22fSVladimir Kondratiev } 5872be7d22fSVladimir Kondratiev 5882be7d22fSVladimir Kondratiev int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size, 5892be7d22fSVladimir Kondratiev int cid, int tid) 5902be7d22fSVladimir Kondratiev { 5912be7d22fSVladimir Kondratiev int rc; 5922be7d22fSVladimir Kondratiev struct wmi_vring_cfg_cmd cmd = { 5932be7d22fSVladimir Kondratiev .action = cpu_to_le32(WMI_VRING_CMD_ADD), 5942be7d22fSVladimir Kondratiev .vring_cfg = { 5952be7d22fSVladimir Kondratiev .tx_sw_ring = { 5962be7d22fSVladimir Kondratiev .max_mpdu_size = cpu_to_le16(TX_BUF_LEN), 597b5d98e9dSVladimir Kondratiev .ring_size = cpu_to_le16(size), 5982be7d22fSVladimir Kondratiev }, 5992be7d22fSVladimir Kondratiev .ringid = id, 600a70abea5SVladimir Kondratiev .cidxtid = mk_cidxtid(cid, tid), 6012be7d22fSVladimir Kondratiev .encap_trans_type = WMI_VRING_ENC_TYPE_802_3, 6022be7d22fSVladimir Kondratiev .mac_ctrl = 0, 6032be7d22fSVladimir Kondratiev .to_resolution = 0, 6042be7d22fSVladimir Kondratiev .agg_max_wsize = 16, 6052be7d22fSVladimir Kondratiev .schd_params = { 6062be7d22fSVladimir Kondratiev .priority = cpu_to_le16(0), 6072be7d22fSVladimir Kondratiev .timeslot_us = cpu_to_le16(0xfff), 6082be7d22fSVladimir Kondratiev }, 6092be7d22fSVladimir Kondratiev }, 6102be7d22fSVladimir Kondratiev }; 6112be7d22fSVladimir Kondratiev struct { 6122be7d22fSVladimir Kondratiev struct wil6210_mbox_hdr_wmi wmi; 6132be7d22fSVladimir Kondratiev struct wmi_vring_cfg_done_event cmd; 6142be7d22fSVladimir Kondratiev } __packed reply; 6152be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_tx[id]; 6162be7d22fSVladimir Kondratiev 6172be7d22fSVladimir Kondratiev if (vring->va) { 6182be7d22fSVladimir Kondratiev wil_err(wil, "Tx ring [%d] already allocated\n", id); 6192be7d22fSVladimir Kondratiev rc = -EINVAL; 6202be7d22fSVladimir Kondratiev goto out; 6212be7d22fSVladimir Kondratiev } 6222be7d22fSVladimir Kondratiev 6232be7d22fSVladimir Kondratiev vring->size = size; 6242be7d22fSVladimir Kondratiev rc = wil_vring_alloc(wil, vring); 6252be7d22fSVladimir Kondratiev if (rc) 6262be7d22fSVladimir Kondratiev goto out; 6272be7d22fSVladimir Kondratiev 62893ae6d49SVladimir Kondratiev wil->vring2cid_tid[id][0] = cid; 62993ae6d49SVladimir Kondratiev wil->vring2cid_tid[id][1] = tid; 63093ae6d49SVladimir Kondratiev 6312be7d22fSVladimir Kondratiev cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa); 6322be7d22fSVladimir Kondratiev 6332be7d22fSVladimir Kondratiev rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd), 6342be7d22fSVladimir Kondratiev WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100); 6352be7d22fSVladimir Kondratiev if (rc) 6362be7d22fSVladimir Kondratiev goto out_free; 6372be7d22fSVladimir Kondratiev 638b8023177SVladimir Kondratiev if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) { 6392be7d22fSVladimir Kondratiev wil_err(wil, "Tx config failed, status 0x%02x\n", 6402be7d22fSVladimir Kondratiev reply.cmd.status); 641c331997bSVladimir Kondratiev rc = -EINVAL; 6422be7d22fSVladimir Kondratiev goto out_free; 6432be7d22fSVladimir Kondratiev } 6442be7d22fSVladimir Kondratiev vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr); 6452be7d22fSVladimir Kondratiev 6462be7d22fSVladimir Kondratiev return 0; 6472be7d22fSVladimir Kondratiev out_free: 6482be7d22fSVladimir Kondratiev wil_vring_free(wil, vring, 1); 6492be7d22fSVladimir Kondratiev out: 6502be7d22fSVladimir Kondratiev 6512be7d22fSVladimir Kondratiev return rc; 6522be7d22fSVladimir Kondratiev } 6532be7d22fSVladimir Kondratiev 6542be7d22fSVladimir Kondratiev void wil_vring_fini_tx(struct wil6210_priv *wil, int id) 6552be7d22fSVladimir Kondratiev { 6562be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_tx[id]; 6572be7d22fSVladimir Kondratiev 6582be7d22fSVladimir Kondratiev if (!vring->va) 6592be7d22fSVladimir Kondratiev return; 6602be7d22fSVladimir Kondratiev 6612be7d22fSVladimir Kondratiev wil_vring_free(wil, vring, 1); 6622be7d22fSVladimir Kondratiev } 6632be7d22fSVladimir Kondratiev 6642be7d22fSVladimir Kondratiev static struct vring *wil_find_tx_vring(struct wil6210_priv *wil, 6652be7d22fSVladimir Kondratiev struct sk_buff *skb) 6662be7d22fSVladimir Kondratiev { 6673df2cd36SVladimir Kondratiev int i; 6683df2cd36SVladimir Kondratiev struct ethhdr *eth = (void *)skb->data; 6693df2cd36SVladimir Kondratiev int cid = wil_find_cid(wil, eth->h_dest); 6702be7d22fSVladimir Kondratiev 6713df2cd36SVladimir Kondratiev if (cid < 0) 6723df2cd36SVladimir Kondratiev return NULL; 6733df2cd36SVladimir Kondratiev 674e58c9f70SVladimir Kondratiev if (!wil->sta[cid].data_port_open && 675e58c9f70SVladimir Kondratiev (skb->protocol != cpu_to_be16(ETH_P_PAE))) 676e58c9f70SVladimir Kondratiev return NULL; 677e58c9f70SVladimir Kondratiev 6783df2cd36SVladimir Kondratiev /* TODO: fix for multiple TID */ 6793df2cd36SVladimir Kondratiev for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) { 6803df2cd36SVladimir Kondratiev if (wil->vring2cid_tid[i][0] == cid) { 6813df2cd36SVladimir Kondratiev struct vring *v = &wil->vring_tx[i]; 6823df2cd36SVladimir Kondratiev wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n", 6833df2cd36SVladimir Kondratiev __func__, eth->h_dest, i); 6843df2cd36SVladimir Kondratiev if (v->va) { 6852be7d22fSVladimir Kondratiev return v; 6863df2cd36SVladimir Kondratiev } else { 6873df2cd36SVladimir Kondratiev wil_dbg_txrx(wil, "vring[%d] not valid\n", i); 6883df2cd36SVladimir Kondratiev return NULL; 6893df2cd36SVladimir Kondratiev } 6903df2cd36SVladimir Kondratiev } 6913df2cd36SVladimir Kondratiev } 6922be7d22fSVladimir Kondratiev 6932be7d22fSVladimir Kondratiev return NULL; 6942be7d22fSVladimir Kondratiev } 6952be7d22fSVladimir Kondratiev 696fb3cac57SVladimir Kondratiev static void wil_set_da_for_vring(struct wil6210_priv *wil, 697fb3cac57SVladimir Kondratiev struct sk_buff *skb, int vring_index) 698fb3cac57SVladimir Kondratiev { 699fb3cac57SVladimir Kondratiev struct ethhdr *eth = (void *)skb->data; 700fb3cac57SVladimir Kondratiev int cid = wil->vring2cid_tid[vring_index][0]; 701fb3cac57SVladimir Kondratiev memcpy(eth->h_dest, wil->sta[cid].addr, ETH_ALEN); 702fb3cac57SVladimir Kondratiev } 703fb3cac57SVladimir Kondratiev 704fb3cac57SVladimir Kondratiev static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring, 705fb3cac57SVladimir Kondratiev struct sk_buff *skb); 706fb3cac57SVladimir Kondratiev /* 707fb3cac57SVladimir Kondratiev * Find 1-st vring and return it; set dest address for this vring in skb 708fb3cac57SVladimir Kondratiev * duplicate skb and send it to other active vrings 709fb3cac57SVladimir Kondratiev */ 710fb3cac57SVladimir Kondratiev static struct vring *wil_tx_bcast(struct wil6210_priv *wil, 711fb3cac57SVladimir Kondratiev struct sk_buff *skb) 712fb3cac57SVladimir Kondratiev { 713fb3cac57SVladimir Kondratiev struct vring *v, *v2; 714fb3cac57SVladimir Kondratiev struct sk_buff *skb2; 715fb3cac57SVladimir Kondratiev int i; 716e58c9f70SVladimir Kondratiev u8 cid; 717fb3cac57SVladimir Kondratiev 718e58c9f70SVladimir Kondratiev /* find 1-st vring eligible for data */ 719fb3cac57SVladimir Kondratiev for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) { 720fb3cac57SVladimir Kondratiev v = &wil->vring_tx[i]; 721e58c9f70SVladimir Kondratiev if (!v->va) 722e58c9f70SVladimir Kondratiev continue; 723e58c9f70SVladimir Kondratiev 724e58c9f70SVladimir Kondratiev cid = wil->vring2cid_tid[i][0]; 725e58c9f70SVladimir Kondratiev if (!wil->sta[cid].data_port_open) 726e58c9f70SVladimir Kondratiev continue; 727e58c9f70SVladimir Kondratiev 728fb3cac57SVladimir Kondratiev goto found; 729fb3cac57SVladimir Kondratiev } 730fb3cac57SVladimir Kondratiev 731fb3cac57SVladimir Kondratiev wil_err(wil, "Tx while no vrings active?\n"); 732fb3cac57SVladimir Kondratiev 733fb3cac57SVladimir Kondratiev return NULL; 734fb3cac57SVladimir Kondratiev 735fb3cac57SVladimir Kondratiev found: 736fb3cac57SVladimir Kondratiev wil_dbg_txrx(wil, "BCAST -> ring %d\n", i); 737fb3cac57SVladimir Kondratiev wil_set_da_for_vring(wil, skb, i); 738fb3cac57SVladimir Kondratiev 739fb3cac57SVladimir Kondratiev /* find other active vrings and duplicate skb for each */ 740fb3cac57SVladimir Kondratiev for (i++; i < WIL6210_MAX_TX_RINGS; i++) { 741fb3cac57SVladimir Kondratiev v2 = &wil->vring_tx[i]; 742fb3cac57SVladimir Kondratiev if (!v2->va) 743fb3cac57SVladimir Kondratiev continue; 744e58c9f70SVladimir Kondratiev cid = wil->vring2cid_tid[i][0]; 745e58c9f70SVladimir Kondratiev if (!wil->sta[cid].data_port_open) 746e58c9f70SVladimir Kondratiev continue; 747e58c9f70SVladimir Kondratiev 748fb3cac57SVladimir Kondratiev skb2 = skb_copy(skb, GFP_ATOMIC); 749fb3cac57SVladimir Kondratiev if (skb2) { 750fb3cac57SVladimir Kondratiev wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i); 751fb3cac57SVladimir Kondratiev wil_set_da_for_vring(wil, skb2, i); 752fb3cac57SVladimir Kondratiev wil_tx_vring(wil, v2, skb2); 753fb3cac57SVladimir Kondratiev } else { 754fb3cac57SVladimir Kondratiev wil_err(wil, "skb_copy failed\n"); 755fb3cac57SVladimir Kondratiev } 756fb3cac57SVladimir Kondratiev } 757fb3cac57SVladimir Kondratiev 758fb3cac57SVladimir Kondratiev return v; 759fb3cac57SVladimir Kondratiev } 760fb3cac57SVladimir Kondratiev 76199b55bd2SKirshenbaum Erez static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len, 76299b55bd2SKirshenbaum Erez int vring_index) 7632be7d22fSVladimir Kondratiev { 76468ada71eSVladimir Kondratiev wil_desc_addr_set(&d->dma.addr, pa); 7652be7d22fSVladimir Kondratiev d->dma.ip_length = 0; 7662be7d22fSVladimir Kondratiev /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/ 7672be7d22fSVladimir Kondratiev d->dma.b11 = 0/*14 | BIT(7)*/; 7682be7d22fSVladimir Kondratiev d->dma.error = 0; 7692be7d22fSVladimir Kondratiev d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */ 7707e594444SVladimir Kondratiev d->dma.length = cpu_to_le16((u16)len); 77199b55bd2SKirshenbaum Erez d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS); 7722be7d22fSVladimir Kondratiev d->mac.d[0] = 0; 7732be7d22fSVladimir Kondratiev d->mac.d[1] = 0; 7742be7d22fSVladimir Kondratiev d->mac.d[2] = 0; 7752be7d22fSVladimir Kondratiev d->mac.ucode_cmd = 0; 7762be7d22fSVladimir Kondratiev /* use dst index 0 */ 7772be7d22fSVladimir Kondratiev d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) | 7782be7d22fSVladimir Kondratiev (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS); 7792be7d22fSVladimir Kondratiev /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */ 7802be7d22fSVladimir Kondratiev d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) | 7812be7d22fSVladimir Kondratiev (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS); 7822be7d22fSVladimir Kondratiev 7832be7d22fSVladimir Kondratiev return 0; 7842be7d22fSVladimir Kondratiev } 7852be7d22fSVladimir Kondratiev 786c236658fSVladimir Kondratiev static inline 787c236658fSVladimir Kondratiev void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags) 788c236658fSVladimir Kondratiev { 789c236658fSVladimir Kondratiev d->mac.d[2] |= ((nr_frags + 1) << 790c236658fSVladimir Kondratiev MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS); 791c236658fSVladimir Kondratiev } 792c236658fSVladimir Kondratiev 793504937d4SKirshenbaum Erez static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil, 794504937d4SKirshenbaum Erez struct vring_tx_desc *d, 795504937d4SKirshenbaum Erez struct sk_buff *skb) 796504937d4SKirshenbaum Erez { 797504937d4SKirshenbaum Erez int protocol; 798504937d4SKirshenbaum Erez 799504937d4SKirshenbaum Erez if (skb->ip_summed != CHECKSUM_PARTIAL) 800504937d4SKirshenbaum Erez return 0; 801504937d4SKirshenbaum Erez 802df2d08eeSVladimir Kondratiev d->dma.b11 = ETH_HLEN; /* MAC header length */ 803df2d08eeSVladimir Kondratiev 804504937d4SKirshenbaum Erez switch (skb->protocol) { 805504937d4SKirshenbaum Erez case cpu_to_be16(ETH_P_IP): 806504937d4SKirshenbaum Erez protocol = ip_hdr(skb)->protocol; 807df2d08eeSVladimir Kondratiev d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS); 808504937d4SKirshenbaum Erez break; 809504937d4SKirshenbaum Erez case cpu_to_be16(ETH_P_IPV6): 810504937d4SKirshenbaum Erez protocol = ipv6_hdr(skb)->nexthdr; 811504937d4SKirshenbaum Erez break; 812504937d4SKirshenbaum Erez default: 813504937d4SKirshenbaum Erez return -EINVAL; 814504937d4SKirshenbaum Erez } 815504937d4SKirshenbaum Erez 816504937d4SKirshenbaum Erez switch (protocol) { 817504937d4SKirshenbaum Erez case IPPROTO_TCP: 818504937d4SKirshenbaum Erez d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS); 819504937d4SKirshenbaum Erez /* L4 header len: TCP header length */ 820504937d4SKirshenbaum Erez d->dma.d0 |= 821504937d4SKirshenbaum Erez (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK); 822504937d4SKirshenbaum Erez break; 823504937d4SKirshenbaum Erez case IPPROTO_UDP: 824504937d4SKirshenbaum Erez /* L4 header len: UDP header length */ 825504937d4SKirshenbaum Erez d->dma.d0 |= 826504937d4SKirshenbaum Erez (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK); 827504937d4SKirshenbaum Erez break; 828504937d4SKirshenbaum Erez default: 829504937d4SKirshenbaum Erez return -EINVAL; 830504937d4SKirshenbaum Erez } 831504937d4SKirshenbaum Erez 832504937d4SKirshenbaum Erez d->dma.ip_length = skb_network_header_len(skb); 833504937d4SKirshenbaum Erez /* Enable TCP/UDP checksum */ 834504937d4SKirshenbaum Erez d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS); 835504937d4SKirshenbaum Erez /* Calculate pseudo-header */ 836504937d4SKirshenbaum Erez d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS); 837504937d4SKirshenbaum Erez 838504937d4SKirshenbaum Erez return 0; 839504937d4SKirshenbaum Erez } 840504937d4SKirshenbaum Erez 8412be7d22fSVladimir Kondratiev static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring, 8422be7d22fSVladimir Kondratiev struct sk_buff *skb) 8432be7d22fSVladimir Kondratiev { 8442be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 84568ada71eSVladimir Kondratiev struct vring_tx_desc dd, *d = ⅆ 84668ada71eSVladimir Kondratiev volatile struct vring_tx_desc *_d; 8472be7d22fSVladimir Kondratiev u32 swhead = vring->swhead; 8482be7d22fSVladimir Kondratiev int avail = wil_vring_avail_tx(vring); 8492be7d22fSVladimir Kondratiev int nr_frags = skb_shinfo(skb)->nr_frags; 850504937d4SKirshenbaum Erez uint f = 0; 8512be7d22fSVladimir Kondratiev int vring_index = vring - wil->vring_tx; 8522be7d22fSVladimir Kondratiev uint i = swhead; 8532be7d22fSVladimir Kondratiev dma_addr_t pa; 8542be7d22fSVladimir Kondratiev 8557743882dSVladimir Kondratiev wil_dbg_txrx(wil, "%s()\n", __func__); 8562be7d22fSVladimir Kondratiev 8572be7d22fSVladimir Kondratiev if (avail < 1 + nr_frags) { 8582be7d22fSVladimir Kondratiev wil_err(wil, "Tx ring full. No space for %d fragments\n", 8592be7d22fSVladimir Kondratiev 1 + nr_frags); 8602be7d22fSVladimir Kondratiev return -ENOMEM; 8612be7d22fSVladimir Kondratiev } 86268ada71eSVladimir Kondratiev _d = &(vring->va[i].tx); 8632be7d22fSVladimir Kondratiev 8642be7d22fSVladimir Kondratiev pa = dma_map_single(dev, skb->data, 8652be7d22fSVladimir Kondratiev skb_headlen(skb), DMA_TO_DEVICE); 8662be7d22fSVladimir Kondratiev 8677743882dSVladimir Kondratiev wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb), 8682be7d22fSVladimir Kondratiev skb->data, (unsigned long long)pa); 8697743882dSVladimir Kondratiev wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1, 8702be7d22fSVladimir Kondratiev skb->data, skb_headlen(skb), false); 8712be7d22fSVladimir Kondratiev 8722be7d22fSVladimir Kondratiev if (unlikely(dma_mapping_error(dev, pa))) 8732be7d22fSVladimir Kondratiev return -EINVAL; 8742232abd5SVladimir Kondratiev vring->ctx[i].mapped_as = wil_mapped_as_single; 8752be7d22fSVladimir Kondratiev /* 1-st segment */ 87699b55bd2SKirshenbaum Erez wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index); 877504937d4SKirshenbaum Erez /* Process TCP/UDP checksum offloading */ 878504937d4SKirshenbaum Erez if (wil_tx_desc_offload_cksum_set(wil, d, skb)) { 879504937d4SKirshenbaum Erez wil_err(wil, "VRING #%d Failed to set cksum, drop packet\n", 880504937d4SKirshenbaum Erez vring_index); 881504937d4SKirshenbaum Erez goto dma_error; 882504937d4SKirshenbaum Erez } 883504937d4SKirshenbaum Erez 884c236658fSVladimir Kondratiev vring->ctx[i].nr_frags = nr_frags; 885c236658fSVladimir Kondratiev wil_tx_desc_set_nr_frags(d, nr_frags); 88668ada71eSVladimir Kondratiev if (nr_frags) 88768ada71eSVladimir Kondratiev *_d = *d; 88868ada71eSVladimir Kondratiev 8892be7d22fSVladimir Kondratiev /* middle segments */ 890504937d4SKirshenbaum Erez for (; f < nr_frags; f++) { 8912be7d22fSVladimir Kondratiev const struct skb_frag_struct *frag = 8922be7d22fSVladimir Kondratiev &skb_shinfo(skb)->frags[f]; 8932be7d22fSVladimir Kondratiev int len = skb_frag_size(frag); 8942be7d22fSVladimir Kondratiev i = (swhead + f + 1) % vring->size; 89568ada71eSVladimir Kondratiev _d = &(vring->va[i].tx); 8962be7d22fSVladimir Kondratiev pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), 8972be7d22fSVladimir Kondratiev DMA_TO_DEVICE); 8982be7d22fSVladimir Kondratiev if (unlikely(dma_mapping_error(dev, pa))) 8992be7d22fSVladimir Kondratiev goto dma_error; 9002232abd5SVladimir Kondratiev vring->ctx[i].mapped_as = wil_mapped_as_page; 90199b55bd2SKirshenbaum Erez wil_tx_desc_map(d, pa, len, vring_index); 902c236658fSVladimir Kondratiev /* no need to check return code - 903c236658fSVladimir Kondratiev * if it succeeded for 1-st descriptor, 904c236658fSVladimir Kondratiev * it will succeed here too 905c236658fSVladimir Kondratiev */ 906c236658fSVladimir Kondratiev wil_tx_desc_offload_cksum_set(wil, d, skb); 90768ada71eSVladimir Kondratiev *_d = *d; 9082be7d22fSVladimir Kondratiev } 9092be7d22fSVladimir Kondratiev /* for the last seg only */ 9102be7d22fSVladimir Kondratiev d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS); 911668b2bbdSKirshenbaum Erez d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS); 9122be7d22fSVladimir Kondratiev d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS); 91368ada71eSVladimir Kondratiev *_d = *d; 9142be7d22fSVladimir Kondratiev 9156cdadd4dSVladimir Kondratiev /* hold reference to skb 9166cdadd4dSVladimir Kondratiev * to prevent skb release before accounting 9176cdadd4dSVladimir Kondratiev * in case of immediate "tx done" 9186cdadd4dSVladimir Kondratiev */ 9196cdadd4dSVladimir Kondratiev vring->ctx[i].skb = skb_get(skb); 9206cdadd4dSVladimir Kondratiev 9217743882dSVladimir Kondratiev wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4, 9222be7d22fSVladimir Kondratiev (const void *)d, sizeof(*d), false); 9232be7d22fSVladimir Kondratiev 9242be7d22fSVladimir Kondratiev /* advance swhead */ 9252be7d22fSVladimir Kondratiev wil_vring_advance_head(vring, nr_frags + 1); 9267743882dSVladimir Kondratiev wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead); 92798658095SVladimir Kondratiev trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags); 9282be7d22fSVladimir Kondratiev iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail)); 9292be7d22fSVladimir Kondratiev 9302be7d22fSVladimir Kondratiev return 0; 9312be7d22fSVladimir Kondratiev dma_error: 9322be7d22fSVladimir Kondratiev /* unmap what we have mapped */ 933c2a146f6SVladimir Kondratiev nr_frags = f + 1; /* frags mapped + one for skb head */ 934c2a146f6SVladimir Kondratiev for (f = 0; f < nr_frags; f++) { 935c2a146f6SVladimir Kondratiev struct wil_ctx *ctx; 9367e594444SVladimir Kondratiev 9372be7d22fSVladimir Kondratiev i = (swhead + f) % vring->size; 938c2a146f6SVladimir Kondratiev ctx = &vring->ctx[i]; 93968ada71eSVladimir Kondratiev _d = &(vring->va[i].tx); 94068ada71eSVladimir Kondratiev *d = *_d; 94168ada71eSVladimir Kondratiev _d->dma.status = TX_DMA_STATUS_DU; 9422232abd5SVladimir Kondratiev wil_txdesc_unmap(dev, d, ctx); 943f88f113aSVladimir Kondratiev 944f88f113aSVladimir Kondratiev if (ctx->skb) 945f88f113aSVladimir Kondratiev dev_kfree_skb_any(ctx->skb); 946f88f113aSVladimir Kondratiev 947f88f113aSVladimir Kondratiev memset(ctx, 0, sizeof(*ctx)); 9482be7d22fSVladimir Kondratiev } 9492be7d22fSVladimir Kondratiev 9502be7d22fSVladimir Kondratiev return -EINVAL; 9512be7d22fSVladimir Kondratiev } 9522be7d22fSVladimir Kondratiev 9532be7d22fSVladimir Kondratiev 9542be7d22fSVladimir Kondratiev netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev) 9552be7d22fSVladimir Kondratiev { 9562be7d22fSVladimir Kondratiev struct wil6210_priv *wil = ndev_to_wil(ndev); 9573df2cd36SVladimir Kondratiev struct ethhdr *eth = (void *)skb->data; 9582be7d22fSVladimir Kondratiev struct vring *vring; 9592be7d22fSVladimir Kondratiev int rc; 9602be7d22fSVladimir Kondratiev 9617743882dSVladimir Kondratiev wil_dbg_txrx(wil, "%s()\n", __func__); 9622be7d22fSVladimir Kondratiev if (!test_bit(wil_status_fwready, &wil->status)) { 9632be7d22fSVladimir Kondratiev wil_err(wil, "FW not ready\n"); 9642be7d22fSVladimir Kondratiev goto drop; 9652be7d22fSVladimir Kondratiev } 9662be7d22fSVladimir Kondratiev if (!test_bit(wil_status_fwconnected, &wil->status)) { 9672be7d22fSVladimir Kondratiev wil_err(wil, "FW not connected\n"); 9682be7d22fSVladimir Kondratiev goto drop; 9692be7d22fSVladimir Kondratiev } 9702be7d22fSVladimir Kondratiev if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { 9712be7d22fSVladimir Kondratiev wil_err(wil, "Xmit in monitor mode not supported\n"); 9722be7d22fSVladimir Kondratiev goto drop; 9732be7d22fSVladimir Kondratiev } 974d58db4e4SVladimir Kondratiev 9752be7d22fSVladimir Kondratiev /* find vring */ 9763df2cd36SVladimir Kondratiev if (is_unicast_ether_addr(eth->h_dest)) { 9772be7d22fSVladimir Kondratiev vring = wil_find_tx_vring(wil, skb); 9783df2cd36SVladimir Kondratiev } else { 979fb3cac57SVladimir Kondratiev vring = wil_tx_bcast(wil, skb); 9803df2cd36SVladimir Kondratiev } 9812be7d22fSVladimir Kondratiev if (!vring) { 9823df2cd36SVladimir Kondratiev wil_err(wil, "No Tx VRING found for %pM\n", eth->h_dest); 9832be7d22fSVladimir Kondratiev goto drop; 9842be7d22fSVladimir Kondratiev } 9852be7d22fSVladimir Kondratiev /* set up vring entry */ 9862be7d22fSVladimir Kondratiev rc = wil_tx_vring(wil, vring, skb); 987d58db4e4SVladimir Kondratiev 9882232abd5SVladimir Kondratiev /* do we still have enough room in the vring? */ 9892232abd5SVladimir Kondratiev if (wil_vring_avail_tx(vring) < vring->size/8) 9902232abd5SVladimir Kondratiev netif_tx_stop_all_queues(wil_to_ndev(wil)); 9912232abd5SVladimir Kondratiev 9922be7d22fSVladimir Kondratiev switch (rc) { 9932be7d22fSVladimir Kondratiev case 0: 994795ce734SVladimir Kondratiev /* statistics will be updated on the tx_complete */ 9952be7d22fSVladimir Kondratiev dev_kfree_skb_any(skb); 9962be7d22fSVladimir Kondratiev return NETDEV_TX_OK; 9972be7d22fSVladimir Kondratiev case -ENOMEM: 9982be7d22fSVladimir Kondratiev return NETDEV_TX_BUSY; 9992be7d22fSVladimir Kondratiev default: 1000afda8bb5SVladimir Kondratiev break; /* goto drop; */ 10012be7d22fSVladimir Kondratiev } 10022be7d22fSVladimir Kondratiev drop: 10032be7d22fSVladimir Kondratiev ndev->stats.tx_dropped++; 10042be7d22fSVladimir Kondratiev dev_kfree_skb_any(skb); 10052be7d22fSVladimir Kondratiev 10062be7d22fSVladimir Kondratiev return NET_XMIT_DROP; 10072be7d22fSVladimir Kondratiev } 10082be7d22fSVladimir Kondratiev 10092be7d22fSVladimir Kondratiev /** 10102be7d22fSVladimir Kondratiev * Clean up transmitted skb's from the Tx VRING 10112be7d22fSVladimir Kondratiev * 1012e0287c4aSVladimir Kondratiev * Return number of descriptors cleared 1013e0287c4aSVladimir Kondratiev * 10142be7d22fSVladimir Kondratiev * Safe to call from IRQ 10152be7d22fSVladimir Kondratiev */ 1016e0287c4aSVladimir Kondratiev int wil_tx_complete(struct wil6210_priv *wil, int ringid) 10172be7d22fSVladimir Kondratiev { 1018795ce734SVladimir Kondratiev struct net_device *ndev = wil_to_ndev(wil); 10192be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 10202be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_tx[ringid]; 1021e0287c4aSVladimir Kondratiev int done = 0; 1022c8b78b5fSVladimir Kondratiev int cid = wil->vring2cid_tid[ringid][0]; 1023c8b78b5fSVladimir Kondratiev struct wil_net_stats *stats = &wil->sta[cid].stats; 1024c236658fSVladimir Kondratiev volatile struct vring_tx_desc *_d; 10252be7d22fSVladimir Kondratiev 10262be7d22fSVladimir Kondratiev if (!vring->va) { 10272be7d22fSVladimir Kondratiev wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid); 1028e0287c4aSVladimir Kondratiev return 0; 10292be7d22fSVladimir Kondratiev } 10302be7d22fSVladimir Kondratiev 10317743882dSVladimir Kondratiev wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid); 10322be7d22fSVladimir Kondratiev 10332be7d22fSVladimir Kondratiev while (!wil_vring_is_empty(vring)) { 1034c236658fSVladimir Kondratiev int new_swtail; 1035c236658fSVladimir Kondratiev struct wil_ctx *ctx = &vring->ctx[vring->swtail]; 1036c236658fSVladimir Kondratiev /** 1037c236658fSVladimir Kondratiev * For the fragmented skb, HW will set DU bit only for the 1038c236658fSVladimir Kondratiev * last fragment. look for it 1039c236658fSVladimir Kondratiev */ 1040c236658fSVladimir Kondratiev int lf = (vring->swtail + ctx->nr_frags) % vring->size; 1041c236658fSVladimir Kondratiev /* TODO: check we are not past head */ 1042c236658fSVladimir Kondratiev 1043c236658fSVladimir Kondratiev _d = &vring->va[lf].tx; 1044c236658fSVladimir Kondratiev if (!(_d->dma.status & TX_DMA_STATUS_DU)) 1045c236658fSVladimir Kondratiev break; 1046c236658fSVladimir Kondratiev 1047c236658fSVladimir Kondratiev new_swtail = (lf + 1) % vring->size; 1048c236658fSVladimir Kondratiev while (vring->swtail != new_swtail) { 10494de41befSVladimir Kondratiev struct vring_tx_desc dd, *d = ⅆ 10507e594444SVladimir Kondratiev u16 dmalen; 1051f88f113aSVladimir Kondratiev struct wil_ctx *ctx = &vring->ctx[vring->swtail]; 1052f88f113aSVladimir Kondratiev struct sk_buff *skb = ctx->skb; 1053c236658fSVladimir Kondratiev _d = &vring->va[vring->swtail].tx; 10544de41befSVladimir Kondratiev 105568ada71eSVladimir Kondratiev *d = *_d; 10564de41befSVladimir Kondratiev 10577e594444SVladimir Kondratiev dmalen = le16_to_cpu(d->dma.length); 105898658095SVladimir Kondratiev trace_wil6210_tx_done(ringid, vring->swtail, dmalen, 105998658095SVladimir Kondratiev d->dma.error); 10607743882dSVladimir Kondratiev wil_dbg_txrx(wil, 10612be7d22fSVladimir Kondratiev "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n", 10627e594444SVladimir Kondratiev vring->swtail, dmalen, d->dma.status, 10632be7d22fSVladimir Kondratiev d->dma.error); 10647743882dSVladimir Kondratiev wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4, 10652be7d22fSVladimir Kondratiev (const void *)d, sizeof(*d), false); 10662be7d22fSVladimir Kondratiev 10672232abd5SVladimir Kondratiev wil_txdesc_unmap(dev, d, ctx); 1068f88f113aSVladimir Kondratiev 10692be7d22fSVladimir Kondratiev if (skb) { 1070795ce734SVladimir Kondratiev if (d->dma.error == 0) { 1071795ce734SVladimir Kondratiev ndev->stats.tx_packets++; 1072c8b78b5fSVladimir Kondratiev stats->tx_packets++; 1073795ce734SVladimir Kondratiev ndev->stats.tx_bytes += skb->len; 1074c8b78b5fSVladimir Kondratiev stats->tx_bytes += skb->len; 1075795ce734SVladimir Kondratiev } else { 1076795ce734SVladimir Kondratiev ndev->stats.tx_errors++; 1077c8b78b5fSVladimir Kondratiev stats->tx_errors++; 1078795ce734SVladimir Kondratiev } 1079795ce734SVladimir Kondratiev 10802be7d22fSVladimir Kondratiev dev_kfree_skb_any(skb); 10812be7d22fSVladimir Kondratiev } 1082f88f113aSVladimir Kondratiev memset(ctx, 0, sizeof(*ctx)); 1083c236658fSVladimir Kondratiev /* There is no need to touch HW descriptor: 108403269c65SVladimir Kondratiev * - ststus bit TX_DMA_STATUS_DU is set by design, 108503269c65SVladimir Kondratiev * so hardware will not try to process this desc., 108603269c65SVladimir Kondratiev * - rest of descriptor will be initialized on Tx. 108703269c65SVladimir Kondratiev */ 10882be7d22fSVladimir Kondratiev vring->swtail = wil_vring_next_tail(vring); 1089e0287c4aSVladimir Kondratiev done++; 10902be7d22fSVladimir Kondratiev } 1091c236658fSVladimir Kondratiev } 10922be7d22fSVladimir Kondratiev if (wil_vring_avail_tx(vring) > vring->size/4) 10932be7d22fSVladimir Kondratiev netif_tx_wake_all_queues(wil_to_ndev(wil)); 1094e0287c4aSVladimir Kondratiev 1095e0287c4aSVladimir Kondratiev return done; 10962be7d22fSVladimir Kondratiev } 1097