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> 242be7d22fSVladimir Kondratiev 252be7d22fSVladimir Kondratiev #include "wil6210.h" 262be7d22fSVladimir Kondratiev #include "wmi.h" 272be7d22fSVladimir Kondratiev #include "txrx.h" 2898658095SVladimir Kondratiev #include "trace.h" 292be7d22fSVladimir Kondratiev 302be7d22fSVladimir Kondratiev static bool rtap_include_phy_info; 312be7d22fSVladimir Kondratiev module_param(rtap_include_phy_info, bool, S_IRUGO); 322be7d22fSVladimir Kondratiev MODULE_PARM_DESC(rtap_include_phy_info, 332be7d22fSVladimir Kondratiev " Include PHY info in the radiotap header, default - no"); 342be7d22fSVladimir Kondratiev 352be7d22fSVladimir Kondratiev static inline int wil_vring_is_empty(struct vring *vring) 362be7d22fSVladimir Kondratiev { 372be7d22fSVladimir Kondratiev return vring->swhead == vring->swtail; 382be7d22fSVladimir Kondratiev } 392be7d22fSVladimir Kondratiev 402be7d22fSVladimir Kondratiev static inline u32 wil_vring_next_tail(struct vring *vring) 412be7d22fSVladimir Kondratiev { 422be7d22fSVladimir Kondratiev return (vring->swtail + 1) % vring->size; 432be7d22fSVladimir Kondratiev } 442be7d22fSVladimir Kondratiev 452be7d22fSVladimir Kondratiev static inline void wil_vring_advance_head(struct vring *vring, int n) 462be7d22fSVladimir Kondratiev { 472be7d22fSVladimir Kondratiev vring->swhead = (vring->swhead + n) % vring->size; 482be7d22fSVladimir Kondratiev } 492be7d22fSVladimir Kondratiev 502be7d22fSVladimir Kondratiev static inline int wil_vring_is_full(struct vring *vring) 512be7d22fSVladimir Kondratiev { 522be7d22fSVladimir Kondratiev return wil_vring_next_tail(vring) == vring->swhead; 532be7d22fSVladimir Kondratiev } 542be7d22fSVladimir Kondratiev /* 552be7d22fSVladimir Kondratiev * Available space in Tx Vring 562be7d22fSVladimir Kondratiev */ 572be7d22fSVladimir Kondratiev static inline int wil_vring_avail_tx(struct vring *vring) 582be7d22fSVladimir Kondratiev { 592be7d22fSVladimir Kondratiev u32 swhead = vring->swhead; 602be7d22fSVladimir Kondratiev u32 swtail = vring->swtail; 612be7d22fSVladimir Kondratiev int used = (vring->size + swhead - swtail) % vring->size; 622be7d22fSVladimir Kondratiev 632be7d22fSVladimir Kondratiev return vring->size - used - 1; 642be7d22fSVladimir Kondratiev } 652be7d22fSVladimir Kondratiev 662be7d22fSVladimir Kondratiev static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring) 672be7d22fSVladimir Kondratiev { 682be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 692be7d22fSVladimir Kondratiev size_t sz = vring->size * sizeof(vring->va[0]); 702be7d22fSVladimir Kondratiev uint i; 712be7d22fSVladimir Kondratiev 722be7d22fSVladimir Kondratiev BUILD_BUG_ON(sizeof(vring->va[0]) != 32); 732be7d22fSVladimir Kondratiev 742be7d22fSVladimir Kondratiev vring->swhead = 0; 752be7d22fSVladimir Kondratiev vring->swtail = 0; 76f88f113aSVladimir Kondratiev vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL); 772be7d22fSVladimir Kondratiev if (!vring->ctx) { 782be7d22fSVladimir Kondratiev vring->va = NULL; 792be7d22fSVladimir Kondratiev return -ENOMEM; 802be7d22fSVladimir Kondratiev } 812be7d22fSVladimir Kondratiev /* 822be7d22fSVladimir Kondratiev * vring->va should be aligned on its size rounded up to power of 2 832be7d22fSVladimir Kondratiev * This is granted by the dma_alloc_coherent 842be7d22fSVladimir Kondratiev */ 852be7d22fSVladimir Kondratiev vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL); 862be7d22fSVladimir Kondratiev if (!vring->va) { 872be7d22fSVladimir Kondratiev kfree(vring->ctx); 882be7d22fSVladimir Kondratiev vring->ctx = NULL; 892be7d22fSVladimir Kondratiev return -ENOMEM; 902be7d22fSVladimir Kondratiev } 912be7d22fSVladimir Kondratiev /* initially, all descriptors are SW owned 922be7d22fSVladimir Kondratiev * For Tx and Rx, ownership bit is at the same location, thus 932be7d22fSVladimir Kondratiev * we can use any 942be7d22fSVladimir Kondratiev */ 952be7d22fSVladimir Kondratiev for (i = 0; i < vring->size; i++) { 9668ada71eSVladimir Kondratiev volatile struct vring_tx_desc *_d = &(vring->va[i].tx); 9768ada71eSVladimir Kondratiev _d->dma.status = TX_DMA_STATUS_DU; 982be7d22fSVladimir Kondratiev } 992be7d22fSVladimir Kondratiev 1007743882dSVladimir Kondratiev wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size, 1012be7d22fSVladimir Kondratiev vring->va, (unsigned long long)vring->pa, vring->ctx); 1022be7d22fSVladimir Kondratiev 1032be7d22fSVladimir Kondratiev return 0; 1042be7d22fSVladimir Kondratiev } 1052be7d22fSVladimir Kondratiev 1062be7d22fSVladimir Kondratiev static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring, 1072be7d22fSVladimir Kondratiev int tx) 1082be7d22fSVladimir Kondratiev { 1092be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 1102be7d22fSVladimir Kondratiev size_t sz = vring->size * sizeof(vring->va[0]); 1112be7d22fSVladimir Kondratiev 1122be7d22fSVladimir Kondratiev while (!wil_vring_is_empty(vring)) { 11368ada71eSVladimir Kondratiev dma_addr_t pa; 1147e594444SVladimir Kondratiev u16 dmalen; 115f88f113aSVladimir Kondratiev struct wil_ctx *ctx; 11668ada71eSVladimir Kondratiev 1172be7d22fSVladimir Kondratiev if (tx) { 11868ada71eSVladimir Kondratiev struct vring_tx_desc dd, *d = ⅆ 11968ada71eSVladimir Kondratiev volatile struct vring_tx_desc *_d = 1202be7d22fSVladimir Kondratiev &vring->va[vring->swtail].tx; 12168ada71eSVladimir Kondratiev 122f88f113aSVladimir Kondratiev ctx = &vring->ctx[vring->swtail]; 12368ada71eSVladimir Kondratiev *d = *_d; 12468ada71eSVladimir Kondratiev pa = wil_desc_addr(&d->dma.addr); 1257e594444SVladimir Kondratiev dmalen = le16_to_cpu(d->dma.length); 126f88f113aSVladimir Kondratiev if (vring->ctx[vring->swtail].mapped_as_page) { 1277e594444SVladimir Kondratiev dma_unmap_page(dev, pa, dmalen, 1282be7d22fSVladimir Kondratiev DMA_TO_DEVICE); 129f88f113aSVladimir Kondratiev } else { 130f88f113aSVladimir Kondratiev dma_unmap_single(dev, pa, dmalen, 131f88f113aSVladimir Kondratiev DMA_TO_DEVICE); 1322be7d22fSVladimir Kondratiev } 133f88f113aSVladimir Kondratiev if (ctx->skb) 134f88f113aSVladimir Kondratiev dev_kfree_skb_any(ctx->skb); 1352be7d22fSVladimir Kondratiev vring->swtail = wil_vring_next_tail(vring); 1362be7d22fSVladimir Kondratiev } else { /* rx */ 13768ada71eSVladimir Kondratiev struct vring_rx_desc dd, *d = ⅆ 13868ada71eSVladimir Kondratiev volatile struct vring_rx_desc *_d = 1394d1ac072SVladimir Kondratiev &vring->va[vring->swhead].rx; 14068ada71eSVladimir Kondratiev 141f88f113aSVladimir Kondratiev ctx = &vring->ctx[vring->swhead]; 14268ada71eSVladimir Kondratiev *d = *_d; 14368ada71eSVladimir Kondratiev pa = wil_desc_addr(&d->dma.addr); 1447e594444SVladimir Kondratiev dmalen = le16_to_cpu(d->dma.length); 1457e594444SVladimir Kondratiev dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE); 146f88f113aSVladimir Kondratiev kfree_skb(ctx->skb); 1472be7d22fSVladimir Kondratiev wil_vring_advance_head(vring, 1); 1482be7d22fSVladimir Kondratiev } 1492be7d22fSVladimir Kondratiev } 1502be7d22fSVladimir Kondratiev dma_free_coherent(dev, sz, (void *)vring->va, vring->pa); 1512be7d22fSVladimir Kondratiev kfree(vring->ctx); 1522be7d22fSVladimir Kondratiev vring->pa = 0; 1532be7d22fSVladimir Kondratiev vring->va = NULL; 1542be7d22fSVladimir Kondratiev vring->ctx = NULL; 1552be7d22fSVladimir Kondratiev } 1562be7d22fSVladimir Kondratiev 1572be7d22fSVladimir Kondratiev /** 1582be7d22fSVladimir Kondratiev * Allocate one skb for Rx VRING 1592be7d22fSVladimir Kondratiev * 1602be7d22fSVladimir Kondratiev * Safe to call from IRQ 1612be7d22fSVladimir Kondratiev */ 1622be7d22fSVladimir Kondratiev static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring, 1632be7d22fSVladimir Kondratiev u32 i, int headroom) 1642be7d22fSVladimir Kondratiev { 1652be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 1662be7d22fSVladimir Kondratiev unsigned int sz = RX_BUF_LEN; 16768ada71eSVladimir Kondratiev struct vring_rx_desc dd, *d = ⅆ 16868ada71eSVladimir Kondratiev volatile struct vring_rx_desc *_d = &(vring->va[i].rx); 1692be7d22fSVladimir Kondratiev dma_addr_t pa; 1702be7d22fSVladimir Kondratiev 1712be7d22fSVladimir Kondratiev /* TODO align */ 1722be7d22fSVladimir Kondratiev struct sk_buff *skb = dev_alloc_skb(sz + headroom); 1732be7d22fSVladimir Kondratiev if (unlikely(!skb)) 1742be7d22fSVladimir Kondratiev return -ENOMEM; 1752be7d22fSVladimir Kondratiev 1762be7d22fSVladimir Kondratiev skb_reserve(skb, headroom); 1772be7d22fSVladimir Kondratiev skb_put(skb, sz); 1782be7d22fSVladimir Kondratiev 1792be7d22fSVladimir Kondratiev pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE); 1802be7d22fSVladimir Kondratiev if (unlikely(dma_mapping_error(dev, pa))) { 1812be7d22fSVladimir Kondratiev kfree_skb(skb); 1822be7d22fSVladimir Kondratiev return -ENOMEM; 1832be7d22fSVladimir Kondratiev } 1842be7d22fSVladimir Kondratiev 1852be7d22fSVladimir Kondratiev d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT; 18668ada71eSVladimir Kondratiev wil_desc_addr_set(&d->dma.addr, pa); 1872be7d22fSVladimir Kondratiev /* ip_length don't care */ 1882be7d22fSVladimir Kondratiev /* b11 don't care */ 1892be7d22fSVladimir Kondratiev /* error don't care */ 1902be7d22fSVladimir Kondratiev d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */ 1917e594444SVladimir Kondratiev d->dma.length = cpu_to_le16(sz); 19268ada71eSVladimir Kondratiev *_d = *d; 193f88f113aSVladimir Kondratiev vring->ctx[i].skb = skb; 1942be7d22fSVladimir Kondratiev 1952be7d22fSVladimir Kondratiev return 0; 1962be7d22fSVladimir Kondratiev } 1972be7d22fSVladimir Kondratiev 1982be7d22fSVladimir Kondratiev /** 1992be7d22fSVladimir Kondratiev * Adds radiotap header 2002be7d22fSVladimir Kondratiev * 2012be7d22fSVladimir Kondratiev * Any error indicated as "Bad FCS" 2022be7d22fSVladimir Kondratiev * 2032be7d22fSVladimir Kondratiev * Vendor data for 04:ce:14-1 (Wilocity-1) consists of: 2042be7d22fSVladimir Kondratiev * - Rx descriptor: 32 bytes 2052be7d22fSVladimir Kondratiev * - Phy info 2062be7d22fSVladimir Kondratiev */ 2072be7d22fSVladimir Kondratiev static void wil_rx_add_radiotap_header(struct wil6210_priv *wil, 20833e61169SVladimir Kondratiev struct sk_buff *skb) 2092be7d22fSVladimir Kondratiev { 2102be7d22fSVladimir Kondratiev struct wireless_dev *wdev = wil->wdev; 2112be7d22fSVladimir Kondratiev struct wil6210_rtap { 2122be7d22fSVladimir Kondratiev struct ieee80211_radiotap_header rthdr; 2132be7d22fSVladimir Kondratiev /* fields should be in the order of bits in rthdr.it_present */ 2142be7d22fSVladimir Kondratiev /* flags */ 2152be7d22fSVladimir Kondratiev u8 flags; 2162be7d22fSVladimir Kondratiev /* channel */ 2172be7d22fSVladimir Kondratiev __le16 chnl_freq __aligned(2); 2182be7d22fSVladimir Kondratiev __le16 chnl_flags; 2192be7d22fSVladimir Kondratiev /* MCS */ 2202be7d22fSVladimir Kondratiev u8 mcs_present; 2212be7d22fSVladimir Kondratiev u8 mcs_flags; 2222be7d22fSVladimir Kondratiev u8 mcs_index; 2232be7d22fSVladimir Kondratiev } __packed; 2242be7d22fSVladimir Kondratiev struct wil6210_rtap_vendor { 2252be7d22fSVladimir Kondratiev struct wil6210_rtap rtap; 2262be7d22fSVladimir Kondratiev /* vendor */ 2272be7d22fSVladimir Kondratiev u8 vendor_oui[3] __aligned(2); 2282be7d22fSVladimir Kondratiev u8 vendor_ns; 2292be7d22fSVladimir Kondratiev __le16 vendor_skip; 2302be7d22fSVladimir Kondratiev u8 vendor_data[0]; 2312be7d22fSVladimir Kondratiev } __packed; 23233e61169SVladimir Kondratiev struct vring_rx_desc *d = wil_skb_rxdesc(skb); 2332be7d22fSVladimir Kondratiev struct wil6210_rtap_vendor *rtap_vendor; 2342be7d22fSVladimir Kondratiev int rtap_len = sizeof(struct wil6210_rtap); 2352be7d22fSVladimir Kondratiev int phy_length = 0; /* phy info header size, bytes */ 2362be7d22fSVladimir Kondratiev static char phy_data[128]; 2372be7d22fSVladimir Kondratiev struct ieee80211_channel *ch = wdev->preset_chandef.chan; 2382be7d22fSVladimir Kondratiev 2392be7d22fSVladimir Kondratiev if (rtap_include_phy_info) { 2402be7d22fSVladimir Kondratiev rtap_len = sizeof(*rtap_vendor) + sizeof(*d); 2412be7d22fSVladimir Kondratiev /* calculate additional length */ 2422be7d22fSVladimir Kondratiev if (d->dma.status & RX_DMA_STATUS_PHY_INFO) { 2432be7d22fSVladimir Kondratiev /** 2442be7d22fSVladimir Kondratiev * PHY info starts from 8-byte boundary 2452be7d22fSVladimir Kondratiev * there are 8-byte lines, last line may be partially 2462be7d22fSVladimir Kondratiev * written (HW bug), thus FW configures for last line 2472be7d22fSVladimir Kondratiev * to be excessive. Driver skips this last line. 2482be7d22fSVladimir Kondratiev */ 2492be7d22fSVladimir Kondratiev int len = min_t(int, 8 + sizeof(phy_data), 2502be7d22fSVladimir Kondratiev wil_rxdesc_phy_length(d)); 2512be7d22fSVladimir Kondratiev if (len > 8) { 2522be7d22fSVladimir Kondratiev void *p = skb_tail_pointer(skb); 2532be7d22fSVladimir Kondratiev void *pa = PTR_ALIGN(p, 8); 2542be7d22fSVladimir Kondratiev if (skb_tailroom(skb) >= len + (pa - p)) { 2552be7d22fSVladimir Kondratiev phy_length = len - 8; 2562be7d22fSVladimir Kondratiev memcpy(phy_data, pa, phy_length); 2572be7d22fSVladimir Kondratiev } 2582be7d22fSVladimir Kondratiev } 2592be7d22fSVladimir Kondratiev } 2602be7d22fSVladimir Kondratiev rtap_len += phy_length; 2612be7d22fSVladimir Kondratiev } 2622be7d22fSVladimir Kondratiev 2632be7d22fSVladimir Kondratiev if (skb_headroom(skb) < rtap_len && 2642be7d22fSVladimir Kondratiev pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) { 2652be7d22fSVladimir Kondratiev wil_err(wil, "Unable to expand headrom to %d\n", rtap_len); 2662be7d22fSVladimir Kondratiev return; 2672be7d22fSVladimir Kondratiev } 2682be7d22fSVladimir Kondratiev 2692be7d22fSVladimir Kondratiev rtap_vendor = (void *)skb_push(skb, rtap_len); 2702be7d22fSVladimir Kondratiev memset(rtap_vendor, 0, rtap_len); 2712be7d22fSVladimir Kondratiev 2722be7d22fSVladimir Kondratiev rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION; 2732be7d22fSVladimir Kondratiev rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len); 2742be7d22fSVladimir Kondratiev rtap_vendor->rtap.rthdr.it_present = cpu_to_le32( 2752be7d22fSVladimir Kondratiev (1 << IEEE80211_RADIOTAP_FLAGS) | 2762be7d22fSVladimir Kondratiev (1 << IEEE80211_RADIOTAP_CHANNEL) | 2772be7d22fSVladimir Kondratiev (1 << IEEE80211_RADIOTAP_MCS)); 2782be7d22fSVladimir Kondratiev if (d->dma.status & RX_DMA_STATUS_ERROR) 2792be7d22fSVladimir Kondratiev rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS; 2802be7d22fSVladimir Kondratiev 2812be7d22fSVladimir Kondratiev rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320); 2822be7d22fSVladimir Kondratiev rtap_vendor->rtap.chnl_flags = cpu_to_le16(0); 2832be7d22fSVladimir Kondratiev 2842be7d22fSVladimir Kondratiev rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS; 2852be7d22fSVladimir Kondratiev rtap_vendor->rtap.mcs_flags = 0; 2862be7d22fSVladimir Kondratiev rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d); 2872be7d22fSVladimir Kondratiev 2882be7d22fSVladimir Kondratiev if (rtap_include_phy_info) { 2892be7d22fSVladimir Kondratiev rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 << 2902be7d22fSVladimir Kondratiev IEEE80211_RADIOTAP_VENDOR_NAMESPACE); 2912be7d22fSVladimir Kondratiev /* OUI for Wilocity 04:ce:14 */ 2922be7d22fSVladimir Kondratiev rtap_vendor->vendor_oui[0] = 0x04; 2932be7d22fSVladimir Kondratiev rtap_vendor->vendor_oui[1] = 0xce; 2942be7d22fSVladimir Kondratiev rtap_vendor->vendor_oui[2] = 0x14; 2952be7d22fSVladimir Kondratiev rtap_vendor->vendor_ns = 1; 2962be7d22fSVladimir Kondratiev /* Rx descriptor + PHY data */ 2972be7d22fSVladimir Kondratiev rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) + 2982be7d22fSVladimir Kondratiev phy_length); 2992be7d22fSVladimir Kondratiev memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d)); 3002be7d22fSVladimir Kondratiev memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data, 3012be7d22fSVladimir Kondratiev phy_length); 3022be7d22fSVladimir Kondratiev } 3032be7d22fSVladimir Kondratiev } 3042be7d22fSVladimir Kondratiev 3052be7d22fSVladimir Kondratiev /* 3062be7d22fSVladimir Kondratiev * Fast swap in place between 2 registers 3072be7d22fSVladimir Kondratiev */ 3082be7d22fSVladimir Kondratiev static void wil_swap_u16(u16 *a, u16 *b) 3092be7d22fSVladimir Kondratiev { 3102be7d22fSVladimir Kondratiev *a ^= *b; 3112be7d22fSVladimir Kondratiev *b ^= *a; 3122be7d22fSVladimir Kondratiev *a ^= *b; 3132be7d22fSVladimir Kondratiev } 3142be7d22fSVladimir Kondratiev 3152be7d22fSVladimir Kondratiev static void wil_swap_ethaddr(void *data) 3162be7d22fSVladimir Kondratiev { 3172be7d22fSVladimir Kondratiev struct ethhdr *eth = data; 3182be7d22fSVladimir Kondratiev u16 *s = (u16 *)eth->h_source; 3192be7d22fSVladimir Kondratiev u16 *d = (u16 *)eth->h_dest; 3202be7d22fSVladimir Kondratiev 3212be7d22fSVladimir Kondratiev wil_swap_u16(s++, d++); 3222be7d22fSVladimir Kondratiev wil_swap_u16(s++, d++); 3232be7d22fSVladimir Kondratiev wil_swap_u16(s, d); 3242be7d22fSVladimir Kondratiev } 3252be7d22fSVladimir Kondratiev 3262be7d22fSVladimir Kondratiev /** 3272be7d22fSVladimir Kondratiev * reap 1 frame from @swhead 3282be7d22fSVladimir Kondratiev * 32933e61169SVladimir Kondratiev * Rx descriptor copied to skb->cb 33033e61169SVladimir Kondratiev * 3312be7d22fSVladimir Kondratiev * Safe to call from IRQ 3322be7d22fSVladimir Kondratiev */ 3332be7d22fSVladimir Kondratiev static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil, 3342be7d22fSVladimir Kondratiev struct vring *vring) 3352be7d22fSVladimir Kondratiev { 3362be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 3372be7d22fSVladimir Kondratiev struct net_device *ndev = wil_to_ndev(wil); 33868ada71eSVladimir Kondratiev volatile struct vring_rx_desc *_d; 33968ada71eSVladimir Kondratiev struct vring_rx_desc *d; 3402be7d22fSVladimir Kondratiev struct sk_buff *skb; 3412be7d22fSVladimir Kondratiev dma_addr_t pa; 3422be7d22fSVladimir Kondratiev unsigned int sz = RX_BUF_LEN; 3437e594444SVladimir Kondratiev u16 dmalen; 3442be7d22fSVladimir Kondratiev u8 ftype; 3452be7d22fSVladimir Kondratiev u8 ds_bits; 3462be7d22fSVladimir Kondratiev 34733e61169SVladimir Kondratiev BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb)); 34833e61169SVladimir Kondratiev 3492be7d22fSVladimir Kondratiev if (wil_vring_is_empty(vring)) 3502be7d22fSVladimir Kondratiev return NULL; 3512be7d22fSVladimir Kondratiev 35268ada71eSVladimir Kondratiev _d = &(vring->va[vring->swhead].rx); 35368ada71eSVladimir Kondratiev if (!(_d->dma.status & RX_DMA_STATUS_DU)) { 3542be7d22fSVladimir Kondratiev /* it is not error, we just reached end of Rx done area */ 3552be7d22fSVladimir Kondratiev return NULL; 3562be7d22fSVladimir Kondratiev } 3572be7d22fSVladimir Kondratiev 358f88f113aSVladimir Kondratiev skb = vring->ctx[vring->swhead].skb; 35968ada71eSVladimir Kondratiev d = wil_skb_rxdesc(skb); 36068ada71eSVladimir Kondratiev *d = *_d; 36168ada71eSVladimir Kondratiev pa = wil_desc_addr(&d->dma.addr); 362f88f113aSVladimir Kondratiev vring->ctx[vring->swhead].skb = NULL; 363e270045bSVladimir Kondratiev wil_vring_advance_head(vring, 1); 36468ada71eSVladimir Kondratiev 36568ada71eSVladimir Kondratiev dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE); 36668ada71eSVladimir Kondratiev dmalen = le16_to_cpu(d->dma.length); 36768ada71eSVladimir Kondratiev 36868ada71eSVladimir Kondratiev trace_wil6210_rx(vring->swhead, d); 36968ada71eSVladimir Kondratiev wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen); 37068ada71eSVladimir Kondratiev wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4, 37168ada71eSVladimir Kondratiev (const void *)d, sizeof(*d), false); 37268ada71eSVladimir Kondratiev 373e270045bSVladimir Kondratiev if (dmalen > sz) { 374e270045bSVladimir Kondratiev wil_err(wil, "Rx size too large: %d bytes!\n", dmalen); 375110dea00SWei Yongjun kfree_skb(skb); 376e270045bSVladimir Kondratiev return NULL; 377e270045bSVladimir Kondratiev } 3787e594444SVladimir Kondratiev skb_trim(skb, dmalen); 37933e61169SVladimir Kondratiev 380c0d37713SVladimir Kondratiev wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1, 381c0d37713SVladimir Kondratiev skb->data, skb_headlen(skb), false); 382c0d37713SVladimir Kondratiev 383c0d37713SVladimir Kondratiev 38468ada71eSVladimir Kondratiev wil->stats.last_mcs_rx = wil_rxdesc_mcs(d); 3852be7d22fSVladimir Kondratiev 3862be7d22fSVladimir Kondratiev /* use radiotap header only if required */ 3872be7d22fSVladimir Kondratiev if (ndev->type == ARPHRD_IEEE80211_RADIOTAP) 38833e61169SVladimir Kondratiev wil_rx_add_radiotap_header(wil, skb); 3892be7d22fSVladimir Kondratiev 3902be7d22fSVladimir Kondratiev /* no extra checks if in sniffer mode */ 3912be7d22fSVladimir Kondratiev if (ndev->type != ARPHRD_ETHER) 3922be7d22fSVladimir Kondratiev return skb; 3932be7d22fSVladimir Kondratiev /* 3942be7d22fSVladimir Kondratiev * Non-data frames may be delivered through Rx DMA channel (ex: BAR) 3952be7d22fSVladimir Kondratiev * Driver should recognize it by frame type, that is found 3962be7d22fSVladimir Kondratiev * in Rx descriptor. If type is not data, it is 802.11 frame as is 3972be7d22fSVladimir Kondratiev */ 39868ada71eSVladimir Kondratiev ftype = wil_rxdesc_ftype(d) << 2; 3992be7d22fSVladimir Kondratiev if (ftype != IEEE80211_FTYPE_DATA) { 4007743882dSVladimir Kondratiev wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype); 4012be7d22fSVladimir Kondratiev /* TODO: process it */ 4022be7d22fSVladimir Kondratiev kfree_skb(skb); 4032be7d22fSVladimir Kondratiev return NULL; 4042be7d22fSVladimir Kondratiev } 4052be7d22fSVladimir Kondratiev 4062be7d22fSVladimir Kondratiev if (skb->len < ETH_HLEN) { 4072be7d22fSVladimir Kondratiev wil_err(wil, "Short frame, len = %d\n", skb->len); 4082be7d22fSVladimir Kondratiev /* TODO: process it (i.e. BAR) */ 4092be7d22fSVladimir Kondratiev kfree_skb(skb); 4102be7d22fSVladimir Kondratiev return NULL; 4112be7d22fSVladimir Kondratiev } 4122be7d22fSVladimir Kondratiev 413504937d4SKirshenbaum Erez /* L4 IDENT is on when HW calculated checksum, check status 414504937d4SKirshenbaum Erez * and in case of error drop the packet 415504937d4SKirshenbaum Erez * higher stack layers will handle retransmission (if required) 416504937d4SKirshenbaum Erez */ 417504937d4SKirshenbaum Erez if (d->dma.status & RX_DMA_STATUS_L4_IDENT) { 418504937d4SKirshenbaum Erez /* L4 protocol identified, csum calculated */ 4194a68ab10SVladimir Kondratiev if ((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0) 420504937d4SKirshenbaum Erez skb->ip_summed = CHECKSUM_UNNECESSARY; 4214a68ab10SVladimir Kondratiev /* If HW reports bad checksum, let IP stack re-check it 4224a68ab10SVladimir Kondratiev * For example, HW don't understand Microsoft IP stack that 4234a68ab10SVladimir Kondratiev * mis-calculates TCP checksum - if it should be 0x0, 4244a68ab10SVladimir Kondratiev * it writes 0xffff in violation of RFC 1624 4254a68ab10SVladimir Kondratiev */ 426504937d4SKirshenbaum Erez } 427504937d4SKirshenbaum Erez 42868ada71eSVladimir Kondratiev ds_bits = wil_rxdesc_ds_bits(d); 4292be7d22fSVladimir Kondratiev if (ds_bits == 1) { 4302be7d22fSVladimir Kondratiev /* 4312be7d22fSVladimir Kondratiev * HW bug - in ToDS mode, i.e. Rx on AP side, 4322be7d22fSVladimir Kondratiev * addresses get swapped 4332be7d22fSVladimir Kondratiev */ 4342be7d22fSVladimir Kondratiev wil_swap_ethaddr(skb->data); 4352be7d22fSVladimir Kondratiev } 4362be7d22fSVladimir Kondratiev 4372be7d22fSVladimir Kondratiev return skb; 4382be7d22fSVladimir Kondratiev } 4392be7d22fSVladimir Kondratiev 4402be7d22fSVladimir Kondratiev /** 4412be7d22fSVladimir Kondratiev * allocate and fill up to @count buffers in rx ring 4422be7d22fSVladimir Kondratiev * buffers posted at @swtail 4432be7d22fSVladimir Kondratiev */ 4442be7d22fSVladimir Kondratiev static int wil_rx_refill(struct wil6210_priv *wil, int count) 4452be7d22fSVladimir Kondratiev { 4462be7d22fSVladimir Kondratiev struct net_device *ndev = wil_to_ndev(wil); 4472be7d22fSVladimir Kondratiev struct vring *v = &wil->vring_rx; 4482be7d22fSVladimir Kondratiev u32 next_tail; 4492be7d22fSVladimir Kondratiev int rc = 0; 4502be7d22fSVladimir Kondratiev int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ? 4512be7d22fSVladimir Kondratiev WIL6210_RTAP_SIZE : 0; 4522be7d22fSVladimir Kondratiev 4532be7d22fSVladimir Kondratiev for (; next_tail = wil_vring_next_tail(v), 4542be7d22fSVladimir Kondratiev (next_tail != v->swhead) && (count-- > 0); 4552be7d22fSVladimir Kondratiev v->swtail = next_tail) { 4562be7d22fSVladimir Kondratiev rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom); 4572be7d22fSVladimir Kondratiev if (rc) { 4582be7d22fSVladimir Kondratiev wil_err(wil, "Error %d in wil_rx_refill[%d]\n", 4592be7d22fSVladimir Kondratiev rc, v->swtail); 4602be7d22fSVladimir Kondratiev break; 4612be7d22fSVladimir Kondratiev } 4622be7d22fSVladimir Kondratiev } 4632be7d22fSVladimir Kondratiev iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail)); 4642be7d22fSVladimir Kondratiev 4652be7d22fSVladimir Kondratiev return rc; 4662be7d22fSVladimir Kondratiev } 4672be7d22fSVladimir Kondratiev 4682be7d22fSVladimir Kondratiev /* 4692be7d22fSVladimir Kondratiev * Pass Rx packet to the netif. Update statistics. 470e0287c4aSVladimir Kondratiev * Called in softirq context (NAPI poll). 4712be7d22fSVladimir Kondratiev */ 4722be7d22fSVladimir Kondratiev static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev) 4732be7d22fSVladimir Kondratiev { 4742be7d22fSVladimir Kondratiev int rc; 4752be7d22fSVladimir Kondratiev unsigned int len = skb->len; 4762be7d22fSVladimir Kondratiev 477241804cbSVladimir Kondratiev skb_orphan(skb); 478241804cbSVladimir Kondratiev 479e0287c4aSVladimir Kondratiev rc = netif_receive_skb(skb); 4802be7d22fSVladimir Kondratiev 4812be7d22fSVladimir Kondratiev if (likely(rc == NET_RX_SUCCESS)) { 4822be7d22fSVladimir Kondratiev ndev->stats.rx_packets++; 4832be7d22fSVladimir Kondratiev ndev->stats.rx_bytes += len; 4842be7d22fSVladimir Kondratiev 4852be7d22fSVladimir Kondratiev } else { 4862be7d22fSVladimir Kondratiev ndev->stats.rx_dropped++; 4872be7d22fSVladimir Kondratiev } 4882be7d22fSVladimir Kondratiev } 4892be7d22fSVladimir Kondratiev 4902be7d22fSVladimir Kondratiev /** 4912be7d22fSVladimir Kondratiev * Proceed all completed skb's from Rx VRING 4922be7d22fSVladimir Kondratiev * 493e0287c4aSVladimir Kondratiev * Safe to call from NAPI poll, i.e. softirq with interrupts enabled 4942be7d22fSVladimir Kondratiev */ 495e0287c4aSVladimir Kondratiev void wil_rx_handle(struct wil6210_priv *wil, int *quota) 4962be7d22fSVladimir Kondratiev { 4972be7d22fSVladimir Kondratiev struct net_device *ndev = wil_to_ndev(wil); 4982be7d22fSVladimir Kondratiev struct vring *v = &wil->vring_rx; 4992be7d22fSVladimir Kondratiev struct sk_buff *skb; 5002be7d22fSVladimir Kondratiev 5012be7d22fSVladimir Kondratiev if (!v->va) { 5022be7d22fSVladimir Kondratiev wil_err(wil, "Rx IRQ while Rx not yet initialized\n"); 5032be7d22fSVladimir Kondratiev return; 5042be7d22fSVladimir Kondratiev } 5057743882dSVladimir Kondratiev wil_dbg_txrx(wil, "%s()\n", __func__); 506e0287c4aSVladimir Kondratiev while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) { 507e0287c4aSVladimir Kondratiev (*quota)--; 5082be7d22fSVladimir Kondratiev 5092be7d22fSVladimir Kondratiev if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { 5102be7d22fSVladimir Kondratiev skb->dev = ndev; 5112be7d22fSVladimir Kondratiev skb_reset_mac_header(skb); 5122be7d22fSVladimir Kondratiev skb->ip_summed = CHECKSUM_UNNECESSARY; 5132be7d22fSVladimir Kondratiev skb->pkt_type = PACKET_OTHERHOST; 5142be7d22fSVladimir Kondratiev skb->protocol = htons(ETH_P_802_2); 5152be7d22fSVladimir Kondratiev 5162be7d22fSVladimir Kondratiev } else { 5172be7d22fSVladimir Kondratiev skb->protocol = eth_type_trans(skb, ndev); 5182be7d22fSVladimir Kondratiev } 5192be7d22fSVladimir Kondratiev 5202be7d22fSVladimir Kondratiev wil_netif_rx_any(skb, ndev); 5212be7d22fSVladimir Kondratiev } 5222be7d22fSVladimir Kondratiev wil_rx_refill(wil, v->size); 5232be7d22fSVladimir Kondratiev } 5242be7d22fSVladimir Kondratiev 5252be7d22fSVladimir Kondratiev int wil_rx_init(struct wil6210_priv *wil) 5262be7d22fSVladimir Kondratiev { 5272be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_rx; 5282be7d22fSVladimir Kondratiev int rc; 5292be7d22fSVladimir Kondratiev 5302be7d22fSVladimir Kondratiev vring->size = WIL6210_RX_RING_SIZE; 5312be7d22fSVladimir Kondratiev rc = wil_vring_alloc(wil, vring); 5322be7d22fSVladimir Kondratiev if (rc) 5332be7d22fSVladimir Kondratiev return rc; 5342be7d22fSVladimir Kondratiev 53547e19af9SVladimir Kondratiev rc = wmi_rx_chain_add(wil, vring); 5362be7d22fSVladimir Kondratiev if (rc) 5372be7d22fSVladimir Kondratiev goto err_free; 5382be7d22fSVladimir Kondratiev 5392be7d22fSVladimir Kondratiev rc = wil_rx_refill(wil, vring->size); 5402be7d22fSVladimir Kondratiev if (rc) 5412be7d22fSVladimir Kondratiev goto err_free; 5422be7d22fSVladimir Kondratiev 5432be7d22fSVladimir Kondratiev return 0; 5442be7d22fSVladimir Kondratiev err_free: 5452be7d22fSVladimir Kondratiev wil_vring_free(wil, vring, 0); 5462be7d22fSVladimir Kondratiev 5472be7d22fSVladimir Kondratiev return rc; 5482be7d22fSVladimir Kondratiev } 5492be7d22fSVladimir Kondratiev 5502be7d22fSVladimir Kondratiev void wil_rx_fini(struct wil6210_priv *wil) 5512be7d22fSVladimir Kondratiev { 5522be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_rx; 5532be7d22fSVladimir Kondratiev 5542acb4220SVladimir Kondratiev if (vring->va) 5552be7d22fSVladimir Kondratiev wil_vring_free(wil, vring, 0); 5562be7d22fSVladimir Kondratiev } 5572be7d22fSVladimir Kondratiev 5582be7d22fSVladimir Kondratiev int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size, 5592be7d22fSVladimir Kondratiev int cid, int tid) 5602be7d22fSVladimir Kondratiev { 5612be7d22fSVladimir Kondratiev int rc; 5622be7d22fSVladimir Kondratiev struct wmi_vring_cfg_cmd cmd = { 5632be7d22fSVladimir Kondratiev .action = cpu_to_le32(WMI_VRING_CMD_ADD), 5642be7d22fSVladimir Kondratiev .vring_cfg = { 5652be7d22fSVladimir Kondratiev .tx_sw_ring = { 5662be7d22fSVladimir Kondratiev .max_mpdu_size = cpu_to_le16(TX_BUF_LEN), 567b5d98e9dSVladimir Kondratiev .ring_size = cpu_to_le16(size), 5682be7d22fSVladimir Kondratiev }, 5692be7d22fSVladimir Kondratiev .ringid = id, 5702be7d22fSVladimir Kondratiev .cidxtid = (cid & 0xf) | ((tid & 0xf) << 4), 5712be7d22fSVladimir Kondratiev .encap_trans_type = WMI_VRING_ENC_TYPE_802_3, 5722be7d22fSVladimir Kondratiev .mac_ctrl = 0, 5732be7d22fSVladimir Kondratiev .to_resolution = 0, 5742be7d22fSVladimir Kondratiev .agg_max_wsize = 16, 5752be7d22fSVladimir Kondratiev .schd_params = { 5762be7d22fSVladimir Kondratiev .priority = cpu_to_le16(0), 5772be7d22fSVladimir Kondratiev .timeslot_us = cpu_to_le16(0xfff), 5782be7d22fSVladimir Kondratiev }, 5792be7d22fSVladimir Kondratiev }, 5802be7d22fSVladimir Kondratiev }; 5812be7d22fSVladimir Kondratiev struct { 5822be7d22fSVladimir Kondratiev struct wil6210_mbox_hdr_wmi wmi; 5832be7d22fSVladimir Kondratiev struct wmi_vring_cfg_done_event cmd; 5842be7d22fSVladimir Kondratiev } __packed reply; 5852be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_tx[id]; 5862be7d22fSVladimir Kondratiev 5872be7d22fSVladimir Kondratiev if (vring->va) { 5882be7d22fSVladimir Kondratiev wil_err(wil, "Tx ring [%d] already allocated\n", id); 5892be7d22fSVladimir Kondratiev rc = -EINVAL; 5902be7d22fSVladimir Kondratiev goto out; 5912be7d22fSVladimir Kondratiev } 5922be7d22fSVladimir Kondratiev 5932be7d22fSVladimir Kondratiev vring->size = size; 5942be7d22fSVladimir Kondratiev rc = wil_vring_alloc(wil, vring); 5952be7d22fSVladimir Kondratiev if (rc) 5962be7d22fSVladimir Kondratiev goto out; 5972be7d22fSVladimir Kondratiev 5982be7d22fSVladimir Kondratiev cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa); 5992be7d22fSVladimir Kondratiev 6002be7d22fSVladimir Kondratiev rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd), 6012be7d22fSVladimir Kondratiev WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100); 6022be7d22fSVladimir Kondratiev if (rc) 6032be7d22fSVladimir Kondratiev goto out_free; 6042be7d22fSVladimir Kondratiev 605b8023177SVladimir Kondratiev if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) { 6062be7d22fSVladimir Kondratiev wil_err(wil, "Tx config failed, status 0x%02x\n", 6072be7d22fSVladimir Kondratiev reply.cmd.status); 608c331997bSVladimir Kondratiev rc = -EINVAL; 6092be7d22fSVladimir Kondratiev goto out_free; 6102be7d22fSVladimir Kondratiev } 6112be7d22fSVladimir Kondratiev vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr); 6122be7d22fSVladimir Kondratiev 6132be7d22fSVladimir Kondratiev return 0; 6142be7d22fSVladimir Kondratiev out_free: 6152be7d22fSVladimir Kondratiev wil_vring_free(wil, vring, 1); 6162be7d22fSVladimir Kondratiev out: 6172be7d22fSVladimir Kondratiev 6182be7d22fSVladimir Kondratiev return rc; 6192be7d22fSVladimir Kondratiev } 6202be7d22fSVladimir Kondratiev 6212be7d22fSVladimir Kondratiev void wil_vring_fini_tx(struct wil6210_priv *wil, int id) 6222be7d22fSVladimir Kondratiev { 6232be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_tx[id]; 6242be7d22fSVladimir Kondratiev 6252be7d22fSVladimir Kondratiev if (!vring->va) 6262be7d22fSVladimir Kondratiev return; 6272be7d22fSVladimir Kondratiev 6282be7d22fSVladimir Kondratiev wil_vring_free(wil, vring, 1); 6292be7d22fSVladimir Kondratiev } 6302be7d22fSVladimir Kondratiev 6312be7d22fSVladimir Kondratiev static struct vring *wil_find_tx_vring(struct wil6210_priv *wil, 6322be7d22fSVladimir Kondratiev struct sk_buff *skb) 6332be7d22fSVladimir Kondratiev { 6342be7d22fSVladimir Kondratiev struct vring *v = &wil->vring_tx[0]; 6352be7d22fSVladimir Kondratiev 6362be7d22fSVladimir Kondratiev if (v->va) 6372be7d22fSVladimir Kondratiev return v; 6382be7d22fSVladimir Kondratiev 6392be7d22fSVladimir Kondratiev return NULL; 6402be7d22fSVladimir Kondratiev } 6412be7d22fSVladimir Kondratiev 64299b55bd2SKirshenbaum Erez static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len, 64399b55bd2SKirshenbaum Erez int vring_index) 6442be7d22fSVladimir Kondratiev { 64568ada71eSVladimir Kondratiev wil_desc_addr_set(&d->dma.addr, pa); 6462be7d22fSVladimir Kondratiev d->dma.ip_length = 0; 6472be7d22fSVladimir Kondratiev /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/ 6482be7d22fSVladimir Kondratiev d->dma.b11 = 0/*14 | BIT(7)*/; 6492be7d22fSVladimir Kondratiev d->dma.error = 0; 6502be7d22fSVladimir Kondratiev d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */ 6517e594444SVladimir Kondratiev d->dma.length = cpu_to_le16((u16)len); 65299b55bd2SKirshenbaum Erez d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS); 6532be7d22fSVladimir Kondratiev d->mac.d[0] = 0; 6542be7d22fSVladimir Kondratiev d->mac.d[1] = 0; 6552be7d22fSVladimir Kondratiev d->mac.d[2] = 0; 6562be7d22fSVladimir Kondratiev d->mac.ucode_cmd = 0; 6572be7d22fSVladimir Kondratiev /* use dst index 0 */ 6582be7d22fSVladimir Kondratiev d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) | 6592be7d22fSVladimir Kondratiev (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS); 6602be7d22fSVladimir Kondratiev /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */ 6612be7d22fSVladimir Kondratiev d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) | 6622be7d22fSVladimir Kondratiev (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS); 6632be7d22fSVladimir Kondratiev 6642be7d22fSVladimir Kondratiev return 0; 6652be7d22fSVladimir Kondratiev } 6662be7d22fSVladimir Kondratiev 667504937d4SKirshenbaum Erez static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil, 668504937d4SKirshenbaum Erez struct vring_tx_desc *d, 669504937d4SKirshenbaum Erez struct sk_buff *skb) 670504937d4SKirshenbaum Erez { 671504937d4SKirshenbaum Erez int protocol; 672504937d4SKirshenbaum Erez 673504937d4SKirshenbaum Erez if (skb->ip_summed != CHECKSUM_PARTIAL) 674504937d4SKirshenbaum Erez return 0; 675504937d4SKirshenbaum Erez 676df2d08eeSVladimir Kondratiev d->dma.b11 = ETH_HLEN; /* MAC header length */ 677df2d08eeSVladimir Kondratiev 678504937d4SKirshenbaum Erez switch (skb->protocol) { 679504937d4SKirshenbaum Erez case cpu_to_be16(ETH_P_IP): 680504937d4SKirshenbaum Erez protocol = ip_hdr(skb)->protocol; 681df2d08eeSVladimir Kondratiev d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS); 682504937d4SKirshenbaum Erez break; 683504937d4SKirshenbaum Erez case cpu_to_be16(ETH_P_IPV6): 684504937d4SKirshenbaum Erez protocol = ipv6_hdr(skb)->nexthdr; 685504937d4SKirshenbaum Erez break; 686504937d4SKirshenbaum Erez default: 687504937d4SKirshenbaum Erez return -EINVAL; 688504937d4SKirshenbaum Erez } 689504937d4SKirshenbaum Erez 690504937d4SKirshenbaum Erez switch (protocol) { 691504937d4SKirshenbaum Erez case IPPROTO_TCP: 692504937d4SKirshenbaum Erez d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS); 693504937d4SKirshenbaum Erez /* L4 header len: TCP header length */ 694504937d4SKirshenbaum Erez d->dma.d0 |= 695504937d4SKirshenbaum Erez (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK); 696504937d4SKirshenbaum Erez break; 697504937d4SKirshenbaum Erez case IPPROTO_UDP: 698504937d4SKirshenbaum Erez /* L4 header len: UDP header length */ 699504937d4SKirshenbaum Erez d->dma.d0 |= 700504937d4SKirshenbaum Erez (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK); 701504937d4SKirshenbaum Erez break; 702504937d4SKirshenbaum Erez default: 703504937d4SKirshenbaum Erez return -EINVAL; 704504937d4SKirshenbaum Erez } 705504937d4SKirshenbaum Erez 706504937d4SKirshenbaum Erez d->dma.ip_length = skb_network_header_len(skb); 707504937d4SKirshenbaum Erez /* Enable TCP/UDP checksum */ 708504937d4SKirshenbaum Erez d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS); 709504937d4SKirshenbaum Erez /* Calculate pseudo-header */ 710504937d4SKirshenbaum Erez d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS); 711504937d4SKirshenbaum Erez 712504937d4SKirshenbaum Erez return 0; 713504937d4SKirshenbaum Erez } 714504937d4SKirshenbaum Erez 7152be7d22fSVladimir Kondratiev static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring, 7162be7d22fSVladimir Kondratiev struct sk_buff *skb) 7172be7d22fSVladimir Kondratiev { 7182be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 71968ada71eSVladimir Kondratiev struct vring_tx_desc dd, *d = ⅆ 72068ada71eSVladimir Kondratiev volatile struct vring_tx_desc *_d; 7212be7d22fSVladimir Kondratiev u32 swhead = vring->swhead; 7222be7d22fSVladimir Kondratiev int avail = wil_vring_avail_tx(vring); 7232be7d22fSVladimir Kondratiev int nr_frags = skb_shinfo(skb)->nr_frags; 724504937d4SKirshenbaum Erez uint f = 0; 7252be7d22fSVladimir Kondratiev int vring_index = vring - wil->vring_tx; 7262be7d22fSVladimir Kondratiev uint i = swhead; 7272be7d22fSVladimir Kondratiev dma_addr_t pa; 7282be7d22fSVladimir Kondratiev 7297743882dSVladimir Kondratiev wil_dbg_txrx(wil, "%s()\n", __func__); 7302be7d22fSVladimir Kondratiev 7312be7d22fSVladimir Kondratiev if (avail < vring->size/8) 7322be7d22fSVladimir Kondratiev netif_tx_stop_all_queues(wil_to_ndev(wil)); 7332be7d22fSVladimir Kondratiev if (avail < 1 + nr_frags) { 7342be7d22fSVladimir Kondratiev wil_err(wil, "Tx ring full. No space for %d fragments\n", 7352be7d22fSVladimir Kondratiev 1 + nr_frags); 7362be7d22fSVladimir Kondratiev return -ENOMEM; 7372be7d22fSVladimir Kondratiev } 73868ada71eSVladimir Kondratiev _d = &(vring->va[i].tx); 7392be7d22fSVladimir Kondratiev 7402be7d22fSVladimir Kondratiev /* FIXME FW can accept only unicast frames for the peer */ 7412be7d22fSVladimir Kondratiev memcpy(skb->data, wil->dst_addr[vring_index], ETH_ALEN); 7422be7d22fSVladimir Kondratiev 7432be7d22fSVladimir Kondratiev pa = dma_map_single(dev, skb->data, 7442be7d22fSVladimir Kondratiev skb_headlen(skb), DMA_TO_DEVICE); 7452be7d22fSVladimir Kondratiev 7467743882dSVladimir Kondratiev wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb), 7472be7d22fSVladimir Kondratiev skb->data, (unsigned long long)pa); 7487743882dSVladimir Kondratiev wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1, 7492be7d22fSVladimir Kondratiev skb->data, skb_headlen(skb), false); 7502be7d22fSVladimir Kondratiev 7512be7d22fSVladimir Kondratiev if (unlikely(dma_mapping_error(dev, pa))) 7522be7d22fSVladimir Kondratiev return -EINVAL; 7532be7d22fSVladimir Kondratiev /* 1-st segment */ 75499b55bd2SKirshenbaum Erez wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index); 755504937d4SKirshenbaum Erez /* Process TCP/UDP checksum offloading */ 756504937d4SKirshenbaum Erez if (wil_tx_desc_offload_cksum_set(wil, d, skb)) { 757504937d4SKirshenbaum Erez wil_err(wil, "VRING #%d Failed to set cksum, drop packet\n", 758504937d4SKirshenbaum Erez vring_index); 759504937d4SKirshenbaum Erez goto dma_error; 760504937d4SKirshenbaum Erez } 761504937d4SKirshenbaum Erez 7622be7d22fSVladimir Kondratiev d->mac.d[2] |= ((nr_frags + 1) << 7632be7d22fSVladimir Kondratiev MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS); 76468ada71eSVladimir Kondratiev if (nr_frags) 76568ada71eSVladimir Kondratiev *_d = *d; 76668ada71eSVladimir Kondratiev 7672be7d22fSVladimir Kondratiev /* middle segments */ 768504937d4SKirshenbaum Erez for (; f < nr_frags; f++) { 7692be7d22fSVladimir Kondratiev const struct skb_frag_struct *frag = 7702be7d22fSVladimir Kondratiev &skb_shinfo(skb)->frags[f]; 7712be7d22fSVladimir Kondratiev int len = skb_frag_size(frag); 7722be7d22fSVladimir Kondratiev i = (swhead + f + 1) % vring->size; 77368ada71eSVladimir Kondratiev _d = &(vring->va[i].tx); 7742be7d22fSVladimir Kondratiev pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), 7752be7d22fSVladimir Kondratiev DMA_TO_DEVICE); 7762be7d22fSVladimir Kondratiev if (unlikely(dma_mapping_error(dev, pa))) 7772be7d22fSVladimir Kondratiev goto dma_error; 77899b55bd2SKirshenbaum Erez wil_tx_desc_map(d, pa, len, vring_index); 779f88f113aSVladimir Kondratiev vring->ctx[i].mapped_as_page = 1; 78068ada71eSVladimir Kondratiev *_d = *d; 7812be7d22fSVladimir Kondratiev } 7822be7d22fSVladimir Kondratiev /* for the last seg only */ 7832be7d22fSVladimir Kondratiev d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS); 784668b2bbdSKirshenbaum Erez d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS); 7852be7d22fSVladimir Kondratiev d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS); 78668ada71eSVladimir Kondratiev *_d = *d; 7872be7d22fSVladimir Kondratiev 7886cdadd4dSVladimir Kondratiev /* hold reference to skb 7896cdadd4dSVladimir Kondratiev * to prevent skb release before accounting 7906cdadd4dSVladimir Kondratiev * in case of immediate "tx done" 7916cdadd4dSVladimir Kondratiev */ 7926cdadd4dSVladimir Kondratiev vring->ctx[i].skb = skb_get(skb); 7936cdadd4dSVladimir Kondratiev 7947743882dSVladimir Kondratiev wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4, 7952be7d22fSVladimir Kondratiev (const void *)d, sizeof(*d), false); 7962be7d22fSVladimir Kondratiev 7972be7d22fSVladimir Kondratiev /* advance swhead */ 7982be7d22fSVladimir Kondratiev wil_vring_advance_head(vring, nr_frags + 1); 7997743882dSVladimir Kondratiev wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead); 80098658095SVladimir Kondratiev trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags); 8012be7d22fSVladimir Kondratiev iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail)); 8022be7d22fSVladimir Kondratiev 8032be7d22fSVladimir Kondratiev return 0; 8042be7d22fSVladimir Kondratiev dma_error: 8052be7d22fSVladimir Kondratiev /* unmap what we have mapped */ 806c2a146f6SVladimir Kondratiev nr_frags = f + 1; /* frags mapped + one for skb head */ 807c2a146f6SVladimir Kondratiev for (f = 0; f < nr_frags; f++) { 8087e594444SVladimir Kondratiev u16 dmalen; 809c2a146f6SVladimir Kondratiev struct wil_ctx *ctx; 8107e594444SVladimir Kondratiev 8112be7d22fSVladimir Kondratiev i = (swhead + f) % vring->size; 812c2a146f6SVladimir Kondratiev ctx = &vring->ctx[i]; 81368ada71eSVladimir Kondratiev _d = &(vring->va[i].tx); 81468ada71eSVladimir Kondratiev *d = *_d; 81568ada71eSVladimir Kondratiev _d->dma.status = TX_DMA_STATUS_DU; 81668ada71eSVladimir Kondratiev pa = wil_desc_addr(&d->dma.addr); 8177e594444SVladimir Kondratiev dmalen = le16_to_cpu(d->dma.length); 818f88f113aSVladimir Kondratiev if (ctx->mapped_as_page) 8197e594444SVladimir Kondratiev dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE); 820f88f113aSVladimir Kondratiev else 821f88f113aSVladimir Kondratiev dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE); 822f88f113aSVladimir Kondratiev 823f88f113aSVladimir Kondratiev if (ctx->skb) 824f88f113aSVladimir Kondratiev dev_kfree_skb_any(ctx->skb); 825f88f113aSVladimir Kondratiev 826f88f113aSVladimir Kondratiev memset(ctx, 0, sizeof(*ctx)); 8272be7d22fSVladimir Kondratiev } 8282be7d22fSVladimir Kondratiev 8292be7d22fSVladimir Kondratiev return -EINVAL; 8302be7d22fSVladimir Kondratiev } 8312be7d22fSVladimir Kondratiev 8322be7d22fSVladimir Kondratiev 8332be7d22fSVladimir Kondratiev netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev) 8342be7d22fSVladimir Kondratiev { 8352be7d22fSVladimir Kondratiev struct wil6210_priv *wil = ndev_to_wil(ndev); 8362be7d22fSVladimir Kondratiev struct vring *vring; 8372be7d22fSVladimir Kondratiev int rc; 8382be7d22fSVladimir Kondratiev 8397743882dSVladimir Kondratiev wil_dbg_txrx(wil, "%s()\n", __func__); 8402be7d22fSVladimir Kondratiev if (!test_bit(wil_status_fwready, &wil->status)) { 8412be7d22fSVladimir Kondratiev wil_err(wil, "FW not ready\n"); 8422be7d22fSVladimir Kondratiev goto drop; 8432be7d22fSVladimir Kondratiev } 8442be7d22fSVladimir Kondratiev if (!test_bit(wil_status_fwconnected, &wil->status)) { 8452be7d22fSVladimir Kondratiev wil_err(wil, "FW not connected\n"); 8462be7d22fSVladimir Kondratiev goto drop; 8472be7d22fSVladimir Kondratiev } 8482be7d22fSVladimir Kondratiev if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { 8492be7d22fSVladimir Kondratiev wil_err(wil, "Xmit in monitor mode not supported\n"); 8502be7d22fSVladimir Kondratiev goto drop; 8512be7d22fSVladimir Kondratiev } 852d58db4e4SVladimir Kondratiev 8532be7d22fSVladimir Kondratiev /* find vring */ 8542be7d22fSVladimir Kondratiev vring = wil_find_tx_vring(wil, skb); 8552be7d22fSVladimir Kondratiev if (!vring) { 8562be7d22fSVladimir Kondratiev wil_err(wil, "No Tx VRING available\n"); 8572be7d22fSVladimir Kondratiev goto drop; 8582be7d22fSVladimir Kondratiev } 8592be7d22fSVladimir Kondratiev /* set up vring entry */ 8602be7d22fSVladimir Kondratiev rc = wil_tx_vring(wil, vring, skb); 861d58db4e4SVladimir Kondratiev 8622be7d22fSVladimir Kondratiev switch (rc) { 8632be7d22fSVladimir Kondratiev case 0: 864795ce734SVladimir Kondratiev /* statistics will be updated on the tx_complete */ 8652be7d22fSVladimir Kondratiev dev_kfree_skb_any(skb); 8662be7d22fSVladimir Kondratiev return NETDEV_TX_OK; 8672be7d22fSVladimir Kondratiev case -ENOMEM: 8682be7d22fSVladimir Kondratiev return NETDEV_TX_BUSY; 8692be7d22fSVladimir Kondratiev default: 870afda8bb5SVladimir Kondratiev break; /* goto drop; */ 8712be7d22fSVladimir Kondratiev } 8722be7d22fSVladimir Kondratiev drop: 8732be7d22fSVladimir Kondratiev ndev->stats.tx_dropped++; 8742be7d22fSVladimir Kondratiev dev_kfree_skb_any(skb); 8752be7d22fSVladimir Kondratiev 8762be7d22fSVladimir Kondratiev return NET_XMIT_DROP; 8772be7d22fSVladimir Kondratiev } 8782be7d22fSVladimir Kondratiev 8792be7d22fSVladimir Kondratiev /** 8802be7d22fSVladimir Kondratiev * Clean up transmitted skb's from the Tx VRING 8812be7d22fSVladimir Kondratiev * 882e0287c4aSVladimir Kondratiev * Return number of descriptors cleared 883e0287c4aSVladimir Kondratiev * 8842be7d22fSVladimir Kondratiev * Safe to call from IRQ 8852be7d22fSVladimir Kondratiev */ 886e0287c4aSVladimir Kondratiev int wil_tx_complete(struct wil6210_priv *wil, int ringid) 8872be7d22fSVladimir Kondratiev { 888795ce734SVladimir Kondratiev struct net_device *ndev = wil_to_ndev(wil); 8892be7d22fSVladimir Kondratiev struct device *dev = wil_to_dev(wil); 8902be7d22fSVladimir Kondratiev struct vring *vring = &wil->vring_tx[ringid]; 891e0287c4aSVladimir Kondratiev int done = 0; 8922be7d22fSVladimir Kondratiev 8932be7d22fSVladimir Kondratiev if (!vring->va) { 8942be7d22fSVladimir Kondratiev wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid); 895e0287c4aSVladimir Kondratiev return 0; 8962be7d22fSVladimir Kondratiev } 8972be7d22fSVladimir Kondratiev 8987743882dSVladimir Kondratiev wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid); 8992be7d22fSVladimir Kondratiev 9002be7d22fSVladimir Kondratiev while (!wil_vring_is_empty(vring)) { 90168ada71eSVladimir Kondratiev volatile struct vring_tx_desc *_d = 9024de41befSVladimir Kondratiev &vring->va[vring->swtail].tx; 9034de41befSVladimir Kondratiev struct vring_tx_desc dd, *d = ⅆ 9042be7d22fSVladimir Kondratiev dma_addr_t pa; 9057e594444SVladimir Kondratiev u16 dmalen; 906f88f113aSVladimir Kondratiev struct wil_ctx *ctx = &vring->ctx[vring->swtail]; 907f88f113aSVladimir Kondratiev struct sk_buff *skb = ctx->skb; 9084de41befSVladimir Kondratiev 90968ada71eSVladimir Kondratiev *d = *_d; 9104de41befSVladimir Kondratiev 9112be7d22fSVladimir Kondratiev if (!(d->dma.status & TX_DMA_STATUS_DU)) 9122be7d22fSVladimir Kondratiev break; 9132be7d22fSVladimir Kondratiev 9147e594444SVladimir Kondratiev dmalen = le16_to_cpu(d->dma.length); 91598658095SVladimir Kondratiev trace_wil6210_tx_done(ringid, vring->swtail, dmalen, 91698658095SVladimir Kondratiev d->dma.error); 9177743882dSVladimir Kondratiev wil_dbg_txrx(wil, 9182be7d22fSVladimir Kondratiev "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n", 9197e594444SVladimir Kondratiev vring->swtail, dmalen, d->dma.status, 9202be7d22fSVladimir Kondratiev d->dma.error); 9217743882dSVladimir Kondratiev wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4, 9222be7d22fSVladimir Kondratiev (const void *)d, sizeof(*d), false); 9232be7d22fSVladimir Kondratiev 92468ada71eSVladimir Kondratiev pa = wil_desc_addr(&d->dma.addr); 925f88f113aSVladimir Kondratiev if (ctx->mapped_as_page) 926f88f113aSVladimir Kondratiev dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE); 927f88f113aSVladimir Kondratiev else 928f88f113aSVladimir Kondratiev dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE); 929f88f113aSVladimir Kondratiev 9302be7d22fSVladimir Kondratiev if (skb) { 931795ce734SVladimir Kondratiev if (d->dma.error == 0) { 932795ce734SVladimir Kondratiev ndev->stats.tx_packets++; 933795ce734SVladimir Kondratiev ndev->stats.tx_bytes += skb->len; 934795ce734SVladimir Kondratiev } else { 935795ce734SVladimir Kondratiev ndev->stats.tx_errors++; 936795ce734SVladimir Kondratiev } 937795ce734SVladimir Kondratiev 9382be7d22fSVladimir Kondratiev dev_kfree_skb_any(skb); 9392be7d22fSVladimir Kondratiev } 940f88f113aSVladimir Kondratiev memset(ctx, 0, sizeof(*ctx)); 94103269c65SVladimir Kondratiev /* 94203269c65SVladimir Kondratiev * There is no need to touch HW descriptor: 94303269c65SVladimir Kondratiev * - ststus bit TX_DMA_STATUS_DU is set by design, 94403269c65SVladimir Kondratiev * so hardware will not try to process this desc., 94503269c65SVladimir Kondratiev * - rest of descriptor will be initialized on Tx. 94603269c65SVladimir Kondratiev */ 9472be7d22fSVladimir Kondratiev vring->swtail = wil_vring_next_tail(vring); 948e0287c4aSVladimir Kondratiev done++; 9492be7d22fSVladimir Kondratiev } 9502be7d22fSVladimir Kondratiev if (wil_vring_avail_tx(vring) > vring->size/4) 9512be7d22fSVladimir Kondratiev netif_tx_wake_all_queues(wil_to_ndev(wil)); 952e0287c4aSVladimir Kondratiev 953e0287c4aSVladimir Kondratiev return done; 9542be7d22fSVladimir Kondratiev } 955