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/kernel.h>
182be7d22fSVladimir Kondratiev #include <linux/netdevice.h>
192be7d22fSVladimir Kondratiev #include <linux/etherdevice.h>
202be7d22fSVladimir Kondratiev #include <linux/hardirq.h>
212be7d22fSVladimir Kondratiev #include <net/ieee80211_radiotap.h>
222be7d22fSVladimir Kondratiev #include <linux/if_arp.h>
232be7d22fSVladimir Kondratiev #include <linux/moduleparam.h>
242be7d22fSVladimir Kondratiev 
252be7d22fSVladimir Kondratiev #include "wil6210.h"
262be7d22fSVladimir Kondratiev #include "wmi.h"
272be7d22fSVladimir Kondratiev #include "txrx.h"
282be7d22fSVladimir Kondratiev 
292be7d22fSVladimir Kondratiev static bool rtap_include_phy_info;
302be7d22fSVladimir Kondratiev module_param(rtap_include_phy_info, bool, S_IRUGO);
312be7d22fSVladimir Kondratiev MODULE_PARM_DESC(rtap_include_phy_info,
322be7d22fSVladimir Kondratiev 		 " Include PHY info in the radiotap header, default - no");
332be7d22fSVladimir Kondratiev 
342be7d22fSVladimir Kondratiev static inline int wil_vring_is_empty(struct vring *vring)
352be7d22fSVladimir Kondratiev {
362be7d22fSVladimir Kondratiev 	return vring->swhead == vring->swtail;
372be7d22fSVladimir Kondratiev }
382be7d22fSVladimir Kondratiev 
392be7d22fSVladimir Kondratiev static inline u32 wil_vring_next_tail(struct vring *vring)
402be7d22fSVladimir Kondratiev {
412be7d22fSVladimir Kondratiev 	return (vring->swtail + 1) % vring->size;
422be7d22fSVladimir Kondratiev }
432be7d22fSVladimir Kondratiev 
442be7d22fSVladimir Kondratiev static inline void wil_vring_advance_head(struct vring *vring, int n)
452be7d22fSVladimir Kondratiev {
462be7d22fSVladimir Kondratiev 	vring->swhead = (vring->swhead + n) % vring->size;
472be7d22fSVladimir Kondratiev }
482be7d22fSVladimir Kondratiev 
492be7d22fSVladimir Kondratiev static inline int wil_vring_is_full(struct vring *vring)
502be7d22fSVladimir Kondratiev {
512be7d22fSVladimir Kondratiev 	return wil_vring_next_tail(vring) == vring->swhead;
522be7d22fSVladimir Kondratiev }
532be7d22fSVladimir Kondratiev /*
542be7d22fSVladimir Kondratiev  * Available space in Tx Vring
552be7d22fSVladimir Kondratiev  */
562be7d22fSVladimir Kondratiev static inline int wil_vring_avail_tx(struct vring *vring)
572be7d22fSVladimir Kondratiev {
582be7d22fSVladimir Kondratiev 	u32 swhead = vring->swhead;
592be7d22fSVladimir Kondratiev 	u32 swtail = vring->swtail;
602be7d22fSVladimir Kondratiev 	int used = (vring->size + swhead - swtail) % vring->size;
612be7d22fSVladimir Kondratiev 
622be7d22fSVladimir Kondratiev 	return vring->size - used - 1;
632be7d22fSVladimir Kondratiev }
642be7d22fSVladimir Kondratiev 
652be7d22fSVladimir Kondratiev static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
662be7d22fSVladimir Kondratiev {
672be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
682be7d22fSVladimir Kondratiev 	size_t sz = vring->size * sizeof(vring->va[0]);
692be7d22fSVladimir Kondratiev 	uint i;
702be7d22fSVladimir Kondratiev 
712be7d22fSVladimir Kondratiev 	BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
722be7d22fSVladimir Kondratiev 
732be7d22fSVladimir Kondratiev 	vring->swhead = 0;
742be7d22fSVladimir Kondratiev 	vring->swtail = 0;
752be7d22fSVladimir Kondratiev 	vring->ctx = kzalloc(vring->size * sizeof(vring->ctx[0]), GFP_KERNEL);
762be7d22fSVladimir Kondratiev 	if (!vring->ctx) {
772be7d22fSVladimir Kondratiev 		wil_err(wil, "vring_alloc [%d] failed to alloc ctx mem\n",
782be7d22fSVladimir Kondratiev 			vring->size);
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 		wil_err(wil, "vring_alloc [%d] failed to alloc DMA mem\n",
892be7d22fSVladimir Kondratiev 			vring->size);
902be7d22fSVladimir Kondratiev 		kfree(vring->ctx);
912be7d22fSVladimir Kondratiev 		vring->ctx = NULL;
922be7d22fSVladimir Kondratiev 		return -ENOMEM;
932be7d22fSVladimir Kondratiev 	}
942be7d22fSVladimir Kondratiev 	/* initially, all descriptors are SW owned
952be7d22fSVladimir Kondratiev 	 * For Tx and Rx, ownership bit is at the same location, thus
962be7d22fSVladimir Kondratiev 	 * we can use any
972be7d22fSVladimir Kondratiev 	 */
982be7d22fSVladimir Kondratiev 	for (i = 0; i < vring->size; i++) {
992be7d22fSVladimir Kondratiev 		volatile struct vring_tx_desc *d = &(vring->va[i].tx);
1002be7d22fSVladimir Kondratiev 		d->dma.status = TX_DMA_STATUS_DU;
1012be7d22fSVladimir Kondratiev 	}
1022be7d22fSVladimir Kondratiev 
1032057ebb2SVladimir Kondratiev 	wil_dbg_MISC(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size,
1042be7d22fSVladimir Kondratiev 		     vring->va, (unsigned long long)vring->pa, vring->ctx);
1052be7d22fSVladimir Kondratiev 
1062be7d22fSVladimir Kondratiev 	return 0;
1072be7d22fSVladimir Kondratiev }
1082be7d22fSVladimir Kondratiev 
1092be7d22fSVladimir Kondratiev static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
1102be7d22fSVladimir Kondratiev 			   int tx)
1112be7d22fSVladimir Kondratiev {
1122be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
1132be7d22fSVladimir Kondratiev 	size_t sz = vring->size * sizeof(vring->va[0]);
1142be7d22fSVladimir Kondratiev 
1152be7d22fSVladimir Kondratiev 	while (!wil_vring_is_empty(vring)) {
1162be7d22fSVladimir Kondratiev 		if (tx) {
1172be7d22fSVladimir Kondratiev 			volatile struct vring_tx_desc *d =
1182be7d22fSVladimir Kondratiev 					&vring->va[vring->swtail].tx;
1192be7d22fSVladimir Kondratiev 			dma_addr_t pa = d->dma.addr_low |
1202be7d22fSVladimir Kondratiev 					((u64)d->dma.addr_high << 32);
1212be7d22fSVladimir Kondratiev 			struct sk_buff *skb = vring->ctx[vring->swtail];
1222be7d22fSVladimir Kondratiev 			if (skb) {
1232be7d22fSVladimir Kondratiev 				dma_unmap_single(dev, pa, d->dma.length,
1242be7d22fSVladimir Kondratiev 						 DMA_TO_DEVICE);
1252be7d22fSVladimir Kondratiev 				dev_kfree_skb_any(skb);
1262be7d22fSVladimir Kondratiev 				vring->ctx[vring->swtail] = NULL;
1272be7d22fSVladimir Kondratiev 			} else {
1282be7d22fSVladimir Kondratiev 				dma_unmap_page(dev, pa, d->dma.length,
1292be7d22fSVladimir Kondratiev 					       DMA_TO_DEVICE);
1302be7d22fSVladimir Kondratiev 			}
1312be7d22fSVladimir Kondratiev 			vring->swtail = wil_vring_next_tail(vring);
1322be7d22fSVladimir Kondratiev 		} else { /* rx */
1332be7d22fSVladimir Kondratiev 			volatile struct vring_rx_desc *d =
1342be7d22fSVladimir Kondratiev 					&vring->va[vring->swtail].rx;
1352be7d22fSVladimir Kondratiev 			dma_addr_t pa = d->dma.addr_low |
1362be7d22fSVladimir Kondratiev 					((u64)d->dma.addr_high << 32);
1372be7d22fSVladimir Kondratiev 			struct sk_buff *skb = vring->ctx[vring->swhead];
1382be7d22fSVladimir Kondratiev 			dma_unmap_single(dev, pa, d->dma.length,
1392be7d22fSVladimir Kondratiev 					 DMA_FROM_DEVICE);
1402be7d22fSVladimir Kondratiev 			kfree_skb(skb);
1412be7d22fSVladimir Kondratiev 			wil_vring_advance_head(vring, 1);
1422be7d22fSVladimir Kondratiev 		}
1432be7d22fSVladimir Kondratiev 	}
1442be7d22fSVladimir Kondratiev 	dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
1452be7d22fSVladimir Kondratiev 	kfree(vring->ctx);
1462be7d22fSVladimir Kondratiev 	vring->pa = 0;
1472be7d22fSVladimir Kondratiev 	vring->va = NULL;
1482be7d22fSVladimir Kondratiev 	vring->ctx = NULL;
1492be7d22fSVladimir Kondratiev }
1502be7d22fSVladimir Kondratiev 
1512be7d22fSVladimir Kondratiev /**
1522be7d22fSVladimir Kondratiev  * Allocate one skb for Rx VRING
1532be7d22fSVladimir Kondratiev  *
1542be7d22fSVladimir Kondratiev  * Safe to call from IRQ
1552be7d22fSVladimir Kondratiev  */
1562be7d22fSVladimir Kondratiev static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
1572be7d22fSVladimir Kondratiev 			       u32 i, int headroom)
1582be7d22fSVladimir Kondratiev {
1592be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
1602be7d22fSVladimir Kondratiev 	unsigned int sz = RX_BUF_LEN;
1612be7d22fSVladimir Kondratiev 	volatile struct vring_rx_desc *d = &(vring->va[i].rx);
1622be7d22fSVladimir Kondratiev 	dma_addr_t pa;
1632be7d22fSVladimir Kondratiev 
1642be7d22fSVladimir Kondratiev 	/* TODO align */
1652be7d22fSVladimir Kondratiev 	struct sk_buff *skb = dev_alloc_skb(sz + headroom);
1662be7d22fSVladimir Kondratiev 	if (unlikely(!skb))
1672be7d22fSVladimir Kondratiev 		return -ENOMEM;
1682be7d22fSVladimir Kondratiev 
1692be7d22fSVladimir Kondratiev 	skb_reserve(skb, headroom);
1702be7d22fSVladimir Kondratiev 	skb_put(skb, sz);
1712be7d22fSVladimir Kondratiev 
1722be7d22fSVladimir Kondratiev 	pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
1732be7d22fSVladimir Kondratiev 	if (unlikely(dma_mapping_error(dev, pa))) {
1742be7d22fSVladimir Kondratiev 		kfree_skb(skb);
1752be7d22fSVladimir Kondratiev 		return -ENOMEM;
1762be7d22fSVladimir Kondratiev 	}
1772be7d22fSVladimir Kondratiev 
1782be7d22fSVladimir Kondratiev 	d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
1792be7d22fSVladimir Kondratiev 	d->dma.addr_low = lower_32_bits(pa);
1802be7d22fSVladimir Kondratiev 	d->dma.addr_high = (u16)upper_32_bits(pa);
1812be7d22fSVladimir Kondratiev 	/* ip_length don't care */
1822be7d22fSVladimir Kondratiev 	/* b11 don't care */
1832be7d22fSVladimir Kondratiev 	/* error don't care */
1842be7d22fSVladimir Kondratiev 	d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
1852be7d22fSVladimir Kondratiev 	d->dma.length = sz;
1862be7d22fSVladimir Kondratiev 	vring->ctx[i] = skb;
1872be7d22fSVladimir Kondratiev 
1882be7d22fSVladimir Kondratiev 	return 0;
1892be7d22fSVladimir Kondratiev }
1902be7d22fSVladimir Kondratiev 
1912be7d22fSVladimir Kondratiev /**
1922be7d22fSVladimir Kondratiev  * Adds radiotap header
1932be7d22fSVladimir Kondratiev  *
1942be7d22fSVladimir Kondratiev  * Any error indicated as "Bad FCS"
1952be7d22fSVladimir Kondratiev  *
1962be7d22fSVladimir Kondratiev  * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
1972be7d22fSVladimir Kondratiev  *  - Rx descriptor: 32 bytes
1982be7d22fSVladimir Kondratiev  *  - Phy info
1992be7d22fSVladimir Kondratiev  */
2002be7d22fSVladimir Kondratiev static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
2012be7d22fSVladimir Kondratiev 				       struct sk_buff *skb,
2022be7d22fSVladimir Kondratiev 				       volatile struct vring_rx_desc *d)
2032be7d22fSVladimir Kondratiev {
2042be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
2052be7d22fSVladimir Kondratiev 	struct wil6210_rtap {
2062be7d22fSVladimir Kondratiev 		struct ieee80211_radiotap_header rthdr;
2072be7d22fSVladimir Kondratiev 		/* fields should be in the order of bits in rthdr.it_present */
2082be7d22fSVladimir Kondratiev 		/* flags */
2092be7d22fSVladimir Kondratiev 		u8 flags;
2102be7d22fSVladimir Kondratiev 		/* channel */
2112be7d22fSVladimir Kondratiev 		__le16 chnl_freq __aligned(2);
2122be7d22fSVladimir Kondratiev 		__le16 chnl_flags;
2132be7d22fSVladimir Kondratiev 		/* MCS */
2142be7d22fSVladimir Kondratiev 		u8 mcs_present;
2152be7d22fSVladimir Kondratiev 		u8 mcs_flags;
2162be7d22fSVladimir Kondratiev 		u8 mcs_index;
2172be7d22fSVladimir Kondratiev 	} __packed;
2182be7d22fSVladimir Kondratiev 	struct wil6210_rtap_vendor {
2192be7d22fSVladimir Kondratiev 		struct wil6210_rtap rtap;
2202be7d22fSVladimir Kondratiev 		/* vendor */
2212be7d22fSVladimir Kondratiev 		u8 vendor_oui[3] __aligned(2);
2222be7d22fSVladimir Kondratiev 		u8 vendor_ns;
2232be7d22fSVladimir Kondratiev 		__le16 vendor_skip;
2242be7d22fSVladimir Kondratiev 		u8 vendor_data[0];
2252be7d22fSVladimir Kondratiev 	} __packed;
2262be7d22fSVladimir Kondratiev 	struct wil6210_rtap_vendor *rtap_vendor;
2272be7d22fSVladimir Kondratiev 	int rtap_len = sizeof(struct wil6210_rtap);
2282be7d22fSVladimir Kondratiev 	int phy_length = 0; /* phy info header size, bytes */
2292be7d22fSVladimir Kondratiev 	static char phy_data[128];
2302be7d22fSVladimir Kondratiev 	struct ieee80211_channel *ch = wdev->preset_chandef.chan;
2312be7d22fSVladimir Kondratiev 
2322be7d22fSVladimir Kondratiev 	if (rtap_include_phy_info) {
2332be7d22fSVladimir Kondratiev 		rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
2342be7d22fSVladimir Kondratiev 		/* calculate additional length */
2352be7d22fSVladimir Kondratiev 		if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
2362be7d22fSVladimir Kondratiev 			/**
2372be7d22fSVladimir Kondratiev 			 * PHY info starts from 8-byte boundary
2382be7d22fSVladimir Kondratiev 			 * there are 8-byte lines, last line may be partially
2392be7d22fSVladimir Kondratiev 			 * written (HW bug), thus FW configures for last line
2402be7d22fSVladimir Kondratiev 			 * to be excessive. Driver skips this last line.
2412be7d22fSVladimir Kondratiev 			 */
2422be7d22fSVladimir Kondratiev 			int len = min_t(int, 8 + sizeof(phy_data),
2432be7d22fSVladimir Kondratiev 					wil_rxdesc_phy_length(d));
2442be7d22fSVladimir Kondratiev 			if (len > 8) {
2452be7d22fSVladimir Kondratiev 				void *p = skb_tail_pointer(skb);
2462be7d22fSVladimir Kondratiev 				void *pa = PTR_ALIGN(p, 8);
2472be7d22fSVladimir Kondratiev 				if (skb_tailroom(skb) >= len + (pa - p)) {
2482be7d22fSVladimir Kondratiev 					phy_length = len - 8;
2492be7d22fSVladimir Kondratiev 					memcpy(phy_data, pa, phy_length);
2502be7d22fSVladimir Kondratiev 				}
2512be7d22fSVladimir Kondratiev 			}
2522be7d22fSVladimir Kondratiev 		}
2532be7d22fSVladimir Kondratiev 		rtap_len += phy_length;
2542be7d22fSVladimir Kondratiev 	}
2552be7d22fSVladimir Kondratiev 
2562be7d22fSVladimir Kondratiev 	if (skb_headroom(skb) < rtap_len &&
2572be7d22fSVladimir Kondratiev 	    pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
2582be7d22fSVladimir Kondratiev 		wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
2592be7d22fSVladimir Kondratiev 		return;
2602be7d22fSVladimir Kondratiev 	}
2612be7d22fSVladimir Kondratiev 
2622be7d22fSVladimir Kondratiev 	rtap_vendor = (void *)skb_push(skb, rtap_len);
2632be7d22fSVladimir Kondratiev 	memset(rtap_vendor, 0, rtap_len);
2642be7d22fSVladimir Kondratiev 
2652be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
2662be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
2672be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
2682be7d22fSVladimir Kondratiev 			(1 << IEEE80211_RADIOTAP_FLAGS) |
2692be7d22fSVladimir Kondratiev 			(1 << IEEE80211_RADIOTAP_CHANNEL) |
2702be7d22fSVladimir Kondratiev 			(1 << IEEE80211_RADIOTAP_MCS));
2712be7d22fSVladimir Kondratiev 	if (d->dma.status & RX_DMA_STATUS_ERROR)
2722be7d22fSVladimir Kondratiev 		rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
2732be7d22fSVladimir Kondratiev 
2742be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
2752be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
2762be7d22fSVladimir Kondratiev 
2772be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
2782be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.mcs_flags = 0;
2792be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
2802be7d22fSVladimir Kondratiev 
2812be7d22fSVladimir Kondratiev 	if (rtap_include_phy_info) {
2822be7d22fSVladimir Kondratiev 		rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
2832be7d22fSVladimir Kondratiev 				IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
2842be7d22fSVladimir Kondratiev 		/* OUI for Wilocity 04:ce:14 */
2852be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_oui[0] = 0x04;
2862be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_oui[1] = 0xce;
2872be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_oui[2] = 0x14;
2882be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_ns = 1;
2892be7d22fSVladimir Kondratiev 		/* Rx descriptor + PHY data  */
2902be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
2912be7d22fSVladimir Kondratiev 						       phy_length);
2922be7d22fSVladimir Kondratiev 		memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
2932be7d22fSVladimir Kondratiev 		memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
2942be7d22fSVladimir Kondratiev 		       phy_length);
2952be7d22fSVladimir Kondratiev 	}
2962be7d22fSVladimir Kondratiev }
2972be7d22fSVladimir Kondratiev 
2982be7d22fSVladimir Kondratiev /*
2992be7d22fSVladimir Kondratiev  * Fast swap in place between 2 registers
3002be7d22fSVladimir Kondratiev  */
3012be7d22fSVladimir Kondratiev static void wil_swap_u16(u16 *a, u16 *b)
3022be7d22fSVladimir Kondratiev {
3032be7d22fSVladimir Kondratiev 	*a ^= *b;
3042be7d22fSVladimir Kondratiev 	*b ^= *a;
3052be7d22fSVladimir Kondratiev 	*a ^= *b;
3062be7d22fSVladimir Kondratiev }
3072be7d22fSVladimir Kondratiev 
3082be7d22fSVladimir Kondratiev static void wil_swap_ethaddr(void *data)
3092be7d22fSVladimir Kondratiev {
3102be7d22fSVladimir Kondratiev 	struct ethhdr *eth = data;
3112be7d22fSVladimir Kondratiev 	u16 *s = (u16 *)eth->h_source;
3122be7d22fSVladimir Kondratiev 	u16 *d = (u16 *)eth->h_dest;
3132be7d22fSVladimir Kondratiev 
3142be7d22fSVladimir Kondratiev 	wil_swap_u16(s++, d++);
3152be7d22fSVladimir Kondratiev 	wil_swap_u16(s++, d++);
3162be7d22fSVladimir Kondratiev 	wil_swap_u16(s, d);
3172be7d22fSVladimir Kondratiev }
3182be7d22fSVladimir Kondratiev 
3192be7d22fSVladimir Kondratiev /**
3202be7d22fSVladimir Kondratiev  * reap 1 frame from @swhead
3212be7d22fSVladimir Kondratiev  *
3222be7d22fSVladimir Kondratiev  * Safe to call from IRQ
3232be7d22fSVladimir Kondratiev  */
3242be7d22fSVladimir Kondratiev static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
3252be7d22fSVladimir Kondratiev 					 struct vring *vring)
3262be7d22fSVladimir Kondratiev {
3272be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
3282be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
3292be7d22fSVladimir Kondratiev 	volatile struct vring_rx_desc *d;
3302be7d22fSVladimir Kondratiev 	struct sk_buff *skb;
3312be7d22fSVladimir Kondratiev 	dma_addr_t pa;
3322be7d22fSVladimir Kondratiev 	unsigned int sz = RX_BUF_LEN;
3332be7d22fSVladimir Kondratiev 	u8 ftype;
3342be7d22fSVladimir Kondratiev 	u8 ds_bits;
3352be7d22fSVladimir Kondratiev 
3362be7d22fSVladimir Kondratiev 	if (wil_vring_is_empty(vring))
3372be7d22fSVladimir Kondratiev 		return NULL;
3382be7d22fSVladimir Kondratiev 
3392be7d22fSVladimir Kondratiev 	d = &(vring->va[vring->swhead].rx);
3402be7d22fSVladimir Kondratiev 	if (!(d->dma.status & RX_DMA_STATUS_DU)) {
3412be7d22fSVladimir Kondratiev 		/* it is not error, we just reached end of Rx done area */
3422be7d22fSVladimir Kondratiev 		return NULL;
3432be7d22fSVladimir Kondratiev 	}
3442be7d22fSVladimir Kondratiev 
3452be7d22fSVladimir Kondratiev 	pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
3462be7d22fSVladimir Kondratiev 	skb = vring->ctx[vring->swhead];
3472be7d22fSVladimir Kondratiev 	dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
3482be7d22fSVladimir Kondratiev 	skb_trim(skb, d->dma.length);
3492be7d22fSVladimir Kondratiev 
3502be7d22fSVladimir Kondratiev 	wil->stats.last_mcs_rx = wil_rxdesc_mcs(d);
3512be7d22fSVladimir Kondratiev 
3522be7d22fSVladimir Kondratiev 	/* use radiotap header only if required */
3532be7d22fSVladimir Kondratiev 	if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
3542be7d22fSVladimir Kondratiev 		wil_rx_add_radiotap_header(wil, skb, d);
3552be7d22fSVladimir Kondratiev 
3562be7d22fSVladimir Kondratiev 	wil_dbg_TXRX(wil, "Rx[%3d] : %d bytes\n", vring->swhead, d->dma.length);
3572be7d22fSVladimir Kondratiev 	wil_hex_dump_TXRX("Rx ", DUMP_PREFIX_NONE, 32, 4,
3582be7d22fSVladimir Kondratiev 			  (const void *)d, sizeof(*d), false);
3592be7d22fSVladimir Kondratiev 
3602be7d22fSVladimir Kondratiev 	wil_vring_advance_head(vring, 1);
3612be7d22fSVladimir Kondratiev 
3622be7d22fSVladimir Kondratiev 	/* no extra checks if in sniffer mode */
3632be7d22fSVladimir Kondratiev 	if (ndev->type != ARPHRD_ETHER)
3642be7d22fSVladimir Kondratiev 		return skb;
3652be7d22fSVladimir Kondratiev 	/*
3662be7d22fSVladimir Kondratiev 	 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
3672be7d22fSVladimir Kondratiev 	 * Driver should recognize it by frame type, that is found
3682be7d22fSVladimir Kondratiev 	 * in Rx descriptor. If type is not data, it is 802.11 frame as is
3692be7d22fSVladimir Kondratiev 	 */
3702be7d22fSVladimir Kondratiev 	ftype = wil_rxdesc_ftype(d) << 2;
3712be7d22fSVladimir Kondratiev 	if (ftype != IEEE80211_FTYPE_DATA) {
3722be7d22fSVladimir Kondratiev 		wil_dbg_TXRX(wil, "Non-data frame ftype 0x%08x\n", ftype);
3732be7d22fSVladimir Kondratiev 		/* TODO: process it */
3742be7d22fSVladimir Kondratiev 		kfree_skb(skb);
3752be7d22fSVladimir Kondratiev 		return NULL;
3762be7d22fSVladimir Kondratiev 	}
3772be7d22fSVladimir Kondratiev 
3782be7d22fSVladimir Kondratiev 	if (skb->len < ETH_HLEN) {
3792be7d22fSVladimir Kondratiev 		wil_err(wil, "Short frame, len = %d\n", skb->len);
3802be7d22fSVladimir Kondratiev 		/* TODO: process it (i.e. BAR) */
3812be7d22fSVladimir Kondratiev 		kfree_skb(skb);
3822be7d22fSVladimir Kondratiev 		return NULL;
3832be7d22fSVladimir Kondratiev 	}
3842be7d22fSVladimir Kondratiev 
3852be7d22fSVladimir Kondratiev 	ds_bits = wil_rxdesc_ds_bits(d);
3862be7d22fSVladimir Kondratiev 	if (ds_bits == 1) {
3872be7d22fSVladimir Kondratiev 		/*
3882be7d22fSVladimir Kondratiev 		 * HW bug - in ToDS mode, i.e. Rx on AP side,
3892be7d22fSVladimir Kondratiev 		 * addresses get swapped
3902be7d22fSVladimir Kondratiev 		 */
3912be7d22fSVladimir Kondratiev 		wil_swap_ethaddr(skb->data);
3922be7d22fSVladimir Kondratiev 	}
3932be7d22fSVladimir Kondratiev 
3942be7d22fSVladimir Kondratiev 	return skb;
3952be7d22fSVladimir Kondratiev }
3962be7d22fSVladimir Kondratiev 
3972be7d22fSVladimir Kondratiev /**
3982be7d22fSVladimir Kondratiev  * allocate and fill up to @count buffers in rx ring
3992be7d22fSVladimir Kondratiev  * buffers posted at @swtail
4002be7d22fSVladimir Kondratiev  */
4012be7d22fSVladimir Kondratiev static int wil_rx_refill(struct wil6210_priv *wil, int count)
4022be7d22fSVladimir Kondratiev {
4032be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
4042be7d22fSVladimir Kondratiev 	struct vring *v = &wil->vring_rx;
4052be7d22fSVladimir Kondratiev 	u32 next_tail;
4062be7d22fSVladimir Kondratiev 	int rc = 0;
4072be7d22fSVladimir Kondratiev 	int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
4082be7d22fSVladimir Kondratiev 			WIL6210_RTAP_SIZE : 0;
4092be7d22fSVladimir Kondratiev 
4102be7d22fSVladimir Kondratiev 	for (; next_tail = wil_vring_next_tail(v),
4112be7d22fSVladimir Kondratiev 			(next_tail != v->swhead) && (count-- > 0);
4122be7d22fSVladimir Kondratiev 			v->swtail = next_tail) {
4132be7d22fSVladimir Kondratiev 		rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
4142be7d22fSVladimir Kondratiev 		if (rc) {
4152be7d22fSVladimir Kondratiev 			wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
4162be7d22fSVladimir Kondratiev 				rc, v->swtail);
4172be7d22fSVladimir Kondratiev 			break;
4182be7d22fSVladimir Kondratiev 		}
4192be7d22fSVladimir Kondratiev 	}
4202be7d22fSVladimir Kondratiev 	iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
4212be7d22fSVladimir Kondratiev 
4222be7d22fSVladimir Kondratiev 	return rc;
4232be7d22fSVladimir Kondratiev }
4242be7d22fSVladimir Kondratiev 
4252be7d22fSVladimir Kondratiev /*
4262be7d22fSVladimir Kondratiev  * Pass Rx packet to the netif. Update statistics.
4272be7d22fSVladimir Kondratiev  */
4282be7d22fSVladimir Kondratiev static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
4292be7d22fSVladimir Kondratiev {
4302be7d22fSVladimir Kondratiev 	int rc;
4312be7d22fSVladimir Kondratiev 	unsigned int len = skb->len;
4322be7d22fSVladimir Kondratiev 
4332be7d22fSVladimir Kondratiev 	if (in_interrupt())
4342be7d22fSVladimir Kondratiev 		rc = netif_rx(skb);
4352be7d22fSVladimir Kondratiev 	else
4362be7d22fSVladimir Kondratiev 		rc = netif_rx_ni(skb);
4372be7d22fSVladimir Kondratiev 
4382be7d22fSVladimir Kondratiev 	if (likely(rc == NET_RX_SUCCESS)) {
4392be7d22fSVladimir Kondratiev 		ndev->stats.rx_packets++;
4402be7d22fSVladimir Kondratiev 		ndev->stats.rx_bytes += len;
4412be7d22fSVladimir Kondratiev 
4422be7d22fSVladimir Kondratiev 	} else {
4432be7d22fSVladimir Kondratiev 		ndev->stats.rx_dropped++;
4442be7d22fSVladimir Kondratiev 	}
4452be7d22fSVladimir Kondratiev }
4462be7d22fSVladimir Kondratiev 
4472be7d22fSVladimir Kondratiev /**
4482be7d22fSVladimir Kondratiev  * Proceed all completed skb's from Rx VRING
4492be7d22fSVladimir Kondratiev  *
4502be7d22fSVladimir Kondratiev  * Safe to call from IRQ
4512be7d22fSVladimir Kondratiev  */
4522be7d22fSVladimir Kondratiev void wil_rx_handle(struct wil6210_priv *wil)
4532be7d22fSVladimir Kondratiev {
4542be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
4552be7d22fSVladimir Kondratiev 	struct vring *v = &wil->vring_rx;
4562be7d22fSVladimir Kondratiev 	struct sk_buff *skb;
4572be7d22fSVladimir Kondratiev 
4582be7d22fSVladimir Kondratiev 	if (!v->va) {
4592be7d22fSVladimir Kondratiev 		wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
4602be7d22fSVladimir Kondratiev 		return;
4612be7d22fSVladimir Kondratiev 	}
4622be7d22fSVladimir Kondratiev 	wil_dbg_TXRX(wil, "%s()\n", __func__);
4632be7d22fSVladimir Kondratiev 	while (NULL != (skb = wil_vring_reap_rx(wil, v))) {
4642be7d22fSVladimir Kondratiev 		wil_hex_dump_TXRX("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
4652be7d22fSVladimir Kondratiev 				  skb->data, skb_headlen(skb), false);
4662be7d22fSVladimir Kondratiev 
4672be7d22fSVladimir Kondratiev 		skb_orphan(skb);
4682be7d22fSVladimir Kondratiev 
4692be7d22fSVladimir Kondratiev 		if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
4702be7d22fSVladimir Kondratiev 			skb->dev = ndev;
4712be7d22fSVladimir Kondratiev 			skb_reset_mac_header(skb);
4722be7d22fSVladimir Kondratiev 			skb->ip_summed = CHECKSUM_UNNECESSARY;
4732be7d22fSVladimir Kondratiev 			skb->pkt_type = PACKET_OTHERHOST;
4742be7d22fSVladimir Kondratiev 			skb->protocol = htons(ETH_P_802_2);
4752be7d22fSVladimir Kondratiev 
4762be7d22fSVladimir Kondratiev 		} else {
4772be7d22fSVladimir Kondratiev 			skb->protocol = eth_type_trans(skb, ndev);
4782be7d22fSVladimir Kondratiev 		}
4792be7d22fSVladimir Kondratiev 
4802be7d22fSVladimir Kondratiev 		wil_netif_rx_any(skb, ndev);
4812be7d22fSVladimir Kondratiev 	}
4822be7d22fSVladimir Kondratiev 	wil_rx_refill(wil, v->size);
4832be7d22fSVladimir Kondratiev }
4842be7d22fSVladimir Kondratiev 
4852be7d22fSVladimir Kondratiev int wil_rx_init(struct wil6210_priv *wil)
4862be7d22fSVladimir Kondratiev {
4872be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_rx;
4882be7d22fSVladimir Kondratiev 	int rc;
4892be7d22fSVladimir Kondratiev 
4902be7d22fSVladimir Kondratiev 	vring->size = WIL6210_RX_RING_SIZE;
4912be7d22fSVladimir Kondratiev 	rc = wil_vring_alloc(wil, vring);
4922be7d22fSVladimir Kondratiev 	if (rc)
4932be7d22fSVladimir Kondratiev 		return rc;
4942be7d22fSVladimir Kondratiev 
49547e19af9SVladimir Kondratiev 	rc = wmi_rx_chain_add(wil, vring);
4962be7d22fSVladimir Kondratiev 	if (rc)
4972be7d22fSVladimir Kondratiev 		goto err_free;
4982be7d22fSVladimir Kondratiev 
4992be7d22fSVladimir Kondratiev 	rc = wil_rx_refill(wil, vring->size);
5002be7d22fSVladimir Kondratiev 	if (rc)
5012be7d22fSVladimir Kondratiev 		goto err_free;
5022be7d22fSVladimir Kondratiev 
5032be7d22fSVladimir Kondratiev 	return 0;
5042be7d22fSVladimir Kondratiev  err_free:
5052be7d22fSVladimir Kondratiev 	wil_vring_free(wil, vring, 0);
5062be7d22fSVladimir Kondratiev 
5072be7d22fSVladimir Kondratiev 	return rc;
5082be7d22fSVladimir Kondratiev }
5092be7d22fSVladimir Kondratiev 
5102be7d22fSVladimir Kondratiev void wil_rx_fini(struct wil6210_priv *wil)
5112be7d22fSVladimir Kondratiev {
5122be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_rx;
5132be7d22fSVladimir Kondratiev 
5142be7d22fSVladimir Kondratiev 	if (vring->va) {
51547e19af9SVladimir Kondratiev 		wmi_rx_chain_del(wil);
5162be7d22fSVladimir Kondratiev 		wil_vring_free(wil, vring, 0);
5172be7d22fSVladimir Kondratiev 	}
5182be7d22fSVladimir Kondratiev }
5192be7d22fSVladimir Kondratiev 
5202be7d22fSVladimir Kondratiev int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
5212be7d22fSVladimir Kondratiev 		      int cid, int tid)
5222be7d22fSVladimir Kondratiev {
5232be7d22fSVladimir Kondratiev 	int rc;
5242be7d22fSVladimir Kondratiev 	struct wmi_vring_cfg_cmd cmd = {
5252be7d22fSVladimir Kondratiev 		.action = cpu_to_le32(WMI_VRING_CMD_ADD),
5262be7d22fSVladimir Kondratiev 		.vring_cfg = {
5272be7d22fSVladimir Kondratiev 			.tx_sw_ring = {
5282be7d22fSVladimir Kondratiev 				.max_mpdu_size = cpu_to_le16(TX_BUF_LEN),
5292be7d22fSVladimir Kondratiev 			},
5302be7d22fSVladimir Kondratiev 			.ringid = id,
5312be7d22fSVladimir Kondratiev 			.cidxtid = (cid & 0xf) | ((tid & 0xf) << 4),
5322be7d22fSVladimir Kondratiev 			.encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
5332be7d22fSVladimir Kondratiev 			.mac_ctrl = 0,
5342be7d22fSVladimir Kondratiev 			.to_resolution = 0,
5352be7d22fSVladimir Kondratiev 			.agg_max_wsize = 16,
5362be7d22fSVladimir Kondratiev 			.schd_params = {
5372be7d22fSVladimir Kondratiev 				.priority = cpu_to_le16(0),
5382be7d22fSVladimir Kondratiev 				.timeslot_us = cpu_to_le16(0xfff),
5392be7d22fSVladimir Kondratiev 			},
5402be7d22fSVladimir Kondratiev 		},
5412be7d22fSVladimir Kondratiev 	};
5422be7d22fSVladimir Kondratiev 	struct {
5432be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
5442be7d22fSVladimir Kondratiev 		struct wmi_vring_cfg_done_event cmd;
5452be7d22fSVladimir Kondratiev 	} __packed reply;
5462be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_tx[id];
5472be7d22fSVladimir Kondratiev 
5482be7d22fSVladimir Kondratiev 	if (vring->va) {
5492be7d22fSVladimir Kondratiev 		wil_err(wil, "Tx ring [%d] already allocated\n", id);
5502be7d22fSVladimir Kondratiev 		rc = -EINVAL;
5512be7d22fSVladimir Kondratiev 		goto out;
5522be7d22fSVladimir Kondratiev 	}
5532be7d22fSVladimir Kondratiev 
5542be7d22fSVladimir Kondratiev 	vring->size = size;
5552be7d22fSVladimir Kondratiev 	rc = wil_vring_alloc(wil, vring);
5562be7d22fSVladimir Kondratiev 	if (rc)
5572be7d22fSVladimir Kondratiev 		goto out;
5582be7d22fSVladimir Kondratiev 
5592be7d22fSVladimir Kondratiev 	cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
5602be7d22fSVladimir Kondratiev 	cmd.vring_cfg.tx_sw_ring.ring_size = cpu_to_le16(vring->size);
5612be7d22fSVladimir Kondratiev 
5622be7d22fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
5632be7d22fSVladimir Kondratiev 		      WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
5642be7d22fSVladimir Kondratiev 	if (rc)
5652be7d22fSVladimir Kondratiev 		goto out_free;
5662be7d22fSVladimir Kondratiev 
5672be7d22fSVladimir Kondratiev 	if (reply.cmd.status != WMI_VRING_CFG_SUCCESS) {
5682be7d22fSVladimir Kondratiev 		wil_err(wil, "Tx config failed, status 0x%02x\n",
5692be7d22fSVladimir Kondratiev 			reply.cmd.status);
5702be7d22fSVladimir Kondratiev 		goto out_free;
5712be7d22fSVladimir Kondratiev 	}
5722be7d22fSVladimir Kondratiev 	vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
5732be7d22fSVladimir Kondratiev 
5742be7d22fSVladimir Kondratiev 	return 0;
5752be7d22fSVladimir Kondratiev  out_free:
5762be7d22fSVladimir Kondratiev 	wil_vring_free(wil, vring, 1);
5772be7d22fSVladimir Kondratiev  out:
5782be7d22fSVladimir Kondratiev 
5792be7d22fSVladimir Kondratiev 	return rc;
5802be7d22fSVladimir Kondratiev }
5812be7d22fSVladimir Kondratiev 
5822be7d22fSVladimir Kondratiev void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
5832be7d22fSVladimir Kondratiev {
5842be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_tx[id];
5852be7d22fSVladimir Kondratiev 
5862be7d22fSVladimir Kondratiev 	if (!vring->va)
5872be7d22fSVladimir Kondratiev 		return;
5882be7d22fSVladimir Kondratiev 
5892be7d22fSVladimir Kondratiev 	wil_vring_free(wil, vring, 1);
5902be7d22fSVladimir Kondratiev }
5912be7d22fSVladimir Kondratiev 
5922be7d22fSVladimir Kondratiev static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
5932be7d22fSVladimir Kondratiev 				       struct sk_buff *skb)
5942be7d22fSVladimir Kondratiev {
5952be7d22fSVladimir Kondratiev 	struct vring *v = &wil->vring_tx[0];
5962be7d22fSVladimir Kondratiev 
5972be7d22fSVladimir Kondratiev 	if (v->va)
5982be7d22fSVladimir Kondratiev 		return v;
5992be7d22fSVladimir Kondratiev 
6002be7d22fSVladimir Kondratiev 	return NULL;
6012be7d22fSVladimir Kondratiev }
6022be7d22fSVladimir Kondratiev 
6032be7d22fSVladimir Kondratiev static int wil_tx_desc_map(volatile struct vring_tx_desc *d,
6042be7d22fSVladimir Kondratiev 			   dma_addr_t pa, u32 len)
6052be7d22fSVladimir Kondratiev {
6062be7d22fSVladimir Kondratiev 	d->dma.addr_low = lower_32_bits(pa);
6072be7d22fSVladimir Kondratiev 	d->dma.addr_high = (u16)upper_32_bits(pa);
6082be7d22fSVladimir Kondratiev 	d->dma.ip_length = 0;
6092be7d22fSVladimir Kondratiev 	/* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
6102be7d22fSVladimir Kondratiev 	d->dma.b11 = 0/*14 | BIT(7)*/;
6112be7d22fSVladimir Kondratiev 	d->dma.error = 0;
6122be7d22fSVladimir Kondratiev 	d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
6132be7d22fSVladimir Kondratiev 	d->dma.length = len;
6142be7d22fSVladimir Kondratiev 	d->dma.d0 = 0;
6152be7d22fSVladimir Kondratiev 	d->mac.d[0] = 0;
6162be7d22fSVladimir Kondratiev 	d->mac.d[1] = 0;
6172be7d22fSVladimir Kondratiev 	d->mac.d[2] = 0;
6182be7d22fSVladimir Kondratiev 	d->mac.ucode_cmd = 0;
6192be7d22fSVladimir Kondratiev 	/* use dst index 0 */
6202be7d22fSVladimir Kondratiev 	d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
6212be7d22fSVladimir Kondratiev 		       (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
6222be7d22fSVladimir Kondratiev 	/* translation type:  0 - bypass; 1 - 802.3; 2 - native wifi */
6232be7d22fSVladimir Kondratiev 	d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
6242be7d22fSVladimir Kondratiev 		      (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
6252be7d22fSVladimir Kondratiev 
6262be7d22fSVladimir Kondratiev 	return 0;
6272be7d22fSVladimir Kondratiev }
6282be7d22fSVladimir Kondratiev 
6292be7d22fSVladimir Kondratiev static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
6302be7d22fSVladimir Kondratiev 			struct sk_buff *skb)
6312be7d22fSVladimir Kondratiev {
6322be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
6332be7d22fSVladimir Kondratiev 	volatile struct vring_tx_desc *d;
6342be7d22fSVladimir Kondratiev 	u32 swhead = vring->swhead;
6352be7d22fSVladimir Kondratiev 	int avail = wil_vring_avail_tx(vring);
6362be7d22fSVladimir Kondratiev 	int nr_frags = skb_shinfo(skb)->nr_frags;
6372be7d22fSVladimir Kondratiev 	uint f;
6382be7d22fSVladimir Kondratiev 	int vring_index = vring - wil->vring_tx;
6392be7d22fSVladimir Kondratiev 	uint i = swhead;
6402be7d22fSVladimir Kondratiev 	dma_addr_t pa;
6412be7d22fSVladimir Kondratiev 
6422be7d22fSVladimir Kondratiev 	wil_dbg_TXRX(wil, "%s()\n", __func__);
6432be7d22fSVladimir Kondratiev 
6442be7d22fSVladimir Kondratiev 	if (avail < vring->size/8)
6452be7d22fSVladimir Kondratiev 		netif_tx_stop_all_queues(wil_to_ndev(wil));
6462be7d22fSVladimir Kondratiev 	if (avail < 1 + nr_frags) {
6472be7d22fSVladimir Kondratiev 		wil_err(wil, "Tx ring full. No space for %d fragments\n",
6482be7d22fSVladimir Kondratiev 			1 + nr_frags);
6492be7d22fSVladimir Kondratiev 		return -ENOMEM;
6502be7d22fSVladimir Kondratiev 	}
6512be7d22fSVladimir Kondratiev 	d = &(vring->va[i].tx);
6522be7d22fSVladimir Kondratiev 
6532be7d22fSVladimir Kondratiev 	/* FIXME FW can accept only unicast frames for the peer */
6542be7d22fSVladimir Kondratiev 	memcpy(skb->data, wil->dst_addr[vring_index], ETH_ALEN);
6552be7d22fSVladimir Kondratiev 
6562be7d22fSVladimir Kondratiev 	pa = dma_map_single(dev, skb->data,
6572be7d22fSVladimir Kondratiev 			skb_headlen(skb), DMA_TO_DEVICE);
6582be7d22fSVladimir Kondratiev 
6592be7d22fSVladimir Kondratiev 	wil_dbg_TXRX(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb),
6602be7d22fSVladimir Kondratiev 		     skb->data, (unsigned long long)pa);
6612be7d22fSVladimir Kondratiev 	wil_hex_dump_TXRX("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
6622be7d22fSVladimir Kondratiev 			  skb->data, skb_headlen(skb), false);
6632be7d22fSVladimir Kondratiev 
6642be7d22fSVladimir Kondratiev 	if (unlikely(dma_mapping_error(dev, pa)))
6652be7d22fSVladimir Kondratiev 		return -EINVAL;
6662be7d22fSVladimir Kondratiev 	/* 1-st segment */
6672be7d22fSVladimir Kondratiev 	wil_tx_desc_map(d, pa, skb_headlen(skb));
6682be7d22fSVladimir Kondratiev 	d->mac.d[2] |= ((nr_frags + 1) <<
6692be7d22fSVladimir Kondratiev 		       MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
6702be7d22fSVladimir Kondratiev 	/* middle segments */
6712be7d22fSVladimir Kondratiev 	for (f = 0; f < nr_frags; f++) {
6722be7d22fSVladimir Kondratiev 		const struct skb_frag_struct *frag =
6732be7d22fSVladimir Kondratiev 				&skb_shinfo(skb)->frags[f];
6742be7d22fSVladimir Kondratiev 		int len = skb_frag_size(frag);
6752be7d22fSVladimir Kondratiev 		i = (swhead + f + 1) % vring->size;
6762be7d22fSVladimir Kondratiev 		d = &(vring->va[i].tx);
6772be7d22fSVladimir Kondratiev 		pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
6782be7d22fSVladimir Kondratiev 				DMA_TO_DEVICE);
6792be7d22fSVladimir Kondratiev 		if (unlikely(dma_mapping_error(dev, pa)))
6802be7d22fSVladimir Kondratiev 			goto dma_error;
6812be7d22fSVladimir Kondratiev 		wil_tx_desc_map(d, pa, len);
6822be7d22fSVladimir Kondratiev 		vring->ctx[i] = NULL;
6832be7d22fSVladimir Kondratiev 	}
6842be7d22fSVladimir Kondratiev 	/* for the last seg only */
6852be7d22fSVladimir Kondratiev 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
6862be7d22fSVladimir Kondratiev 	d->dma.d0 |= BIT(9); /* BUG: undocumented bit */
6872be7d22fSVladimir Kondratiev 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
6882be7d22fSVladimir Kondratiev 	d->dma.d0 |= (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
6892be7d22fSVladimir Kondratiev 
6902be7d22fSVladimir Kondratiev 	wil_hex_dump_TXRX("Tx ", DUMP_PREFIX_NONE, 32, 4,
6912be7d22fSVladimir Kondratiev 			  (const void *)d, sizeof(*d), false);
6922be7d22fSVladimir Kondratiev 
6932be7d22fSVladimir Kondratiev 	/* advance swhead */
6942be7d22fSVladimir Kondratiev 	wil_vring_advance_head(vring, nr_frags + 1);
6952be7d22fSVladimir Kondratiev 	wil_dbg_TXRX(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
6962be7d22fSVladimir Kondratiev 	iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
6972be7d22fSVladimir Kondratiev 	/* hold reference to skb
6982be7d22fSVladimir Kondratiev 	 * to prevent skb release before accounting
6992be7d22fSVladimir Kondratiev 	 * in case of immediate "tx done"
7002be7d22fSVladimir Kondratiev 	 */
7012be7d22fSVladimir Kondratiev 	vring->ctx[i] = skb_get(skb);
7022be7d22fSVladimir Kondratiev 
7032be7d22fSVladimir Kondratiev 	return 0;
7042be7d22fSVladimir Kondratiev  dma_error:
7052be7d22fSVladimir Kondratiev 	/* unmap what we have mapped */
7062be7d22fSVladimir Kondratiev 	/* Note: increment @f to operate with positive index */
7072be7d22fSVladimir Kondratiev 	for (f++; f > 0; f--) {
7082be7d22fSVladimir Kondratiev 		i = (swhead + f) % vring->size;
7092be7d22fSVladimir Kondratiev 		d = &(vring->va[i].tx);
7102be7d22fSVladimir Kondratiev 		d->dma.status = TX_DMA_STATUS_DU;
7112be7d22fSVladimir Kondratiev 		pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
7122be7d22fSVladimir Kondratiev 		if (vring->ctx[i])
7132be7d22fSVladimir Kondratiev 			dma_unmap_single(dev, pa, d->dma.length, DMA_TO_DEVICE);
7142be7d22fSVladimir Kondratiev 		else
7152be7d22fSVladimir Kondratiev 			dma_unmap_page(dev, pa, d->dma.length, DMA_TO_DEVICE);
7162be7d22fSVladimir Kondratiev 	}
7172be7d22fSVladimir Kondratiev 
7182be7d22fSVladimir Kondratiev 	return -EINVAL;
7192be7d22fSVladimir Kondratiev }
7202be7d22fSVladimir Kondratiev 
7212be7d22fSVladimir Kondratiev 
7222be7d22fSVladimir Kondratiev netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
7232be7d22fSVladimir Kondratiev {
7242be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = ndev_to_wil(ndev);
7252be7d22fSVladimir Kondratiev 	struct vring *vring;
7262be7d22fSVladimir Kondratiev 	int rc;
7272be7d22fSVladimir Kondratiev 
7282be7d22fSVladimir Kondratiev 	wil_dbg_TXRX(wil, "%s()\n", __func__);
7292be7d22fSVladimir Kondratiev 	if (!test_bit(wil_status_fwready, &wil->status)) {
7302be7d22fSVladimir Kondratiev 		wil_err(wil, "FW not ready\n");
7312be7d22fSVladimir Kondratiev 		goto drop;
7322be7d22fSVladimir Kondratiev 	}
7332be7d22fSVladimir Kondratiev 	if (!test_bit(wil_status_fwconnected, &wil->status)) {
7342be7d22fSVladimir Kondratiev 		wil_err(wil, "FW not connected\n");
7352be7d22fSVladimir Kondratiev 		goto drop;
7362be7d22fSVladimir Kondratiev 	}
7372be7d22fSVladimir Kondratiev 	if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
7382be7d22fSVladimir Kondratiev 		wil_err(wil, "Xmit in monitor mode not supported\n");
7392be7d22fSVladimir Kondratiev 		goto drop;
7402be7d22fSVladimir Kondratiev 	}
7412be7d22fSVladimir Kondratiev 	if (skb->protocol == cpu_to_be16(ETH_P_PAE)) {
7422be7d22fSVladimir Kondratiev 		rc = wmi_tx_eapol(wil, skb);
7432be7d22fSVladimir Kondratiev 	} else {
7442be7d22fSVladimir Kondratiev 		/* find vring */
7452be7d22fSVladimir Kondratiev 		vring = wil_find_tx_vring(wil, skb);
7462be7d22fSVladimir Kondratiev 		if (!vring) {
7472be7d22fSVladimir Kondratiev 			wil_err(wil, "No Tx VRING available\n");
7482be7d22fSVladimir Kondratiev 			goto drop;
7492be7d22fSVladimir Kondratiev 		}
7502be7d22fSVladimir Kondratiev 		/* set up vring entry */
7512be7d22fSVladimir Kondratiev 		rc = wil_tx_vring(wil, vring, skb);
7522be7d22fSVladimir Kondratiev 	}
7532be7d22fSVladimir Kondratiev 	switch (rc) {
7542be7d22fSVladimir Kondratiev 	case 0:
755795ce734SVladimir Kondratiev 		/* statistics will be updated on the tx_complete */
7562be7d22fSVladimir Kondratiev 		dev_kfree_skb_any(skb);
7572be7d22fSVladimir Kondratiev 		return NETDEV_TX_OK;
7582be7d22fSVladimir Kondratiev 	case -ENOMEM:
7592be7d22fSVladimir Kondratiev 		return NETDEV_TX_BUSY;
7602be7d22fSVladimir Kondratiev 	default:
7612be7d22fSVladimir Kondratiev 		; /* goto drop; */
7622be7d22fSVladimir Kondratiev 		break;
7632be7d22fSVladimir Kondratiev 	}
7642be7d22fSVladimir Kondratiev  drop:
7652be7d22fSVladimir Kondratiev 	netif_tx_stop_all_queues(ndev);
7662be7d22fSVladimir Kondratiev 	ndev->stats.tx_dropped++;
7672be7d22fSVladimir Kondratiev 	dev_kfree_skb_any(skb);
7682be7d22fSVladimir Kondratiev 
7692be7d22fSVladimir Kondratiev 	return NET_XMIT_DROP;
7702be7d22fSVladimir Kondratiev }
7712be7d22fSVladimir Kondratiev 
7722be7d22fSVladimir Kondratiev /**
7732be7d22fSVladimir Kondratiev  * Clean up transmitted skb's from the Tx VRING
7742be7d22fSVladimir Kondratiev  *
7752be7d22fSVladimir Kondratiev  * Safe to call from IRQ
7762be7d22fSVladimir Kondratiev  */
7772be7d22fSVladimir Kondratiev void wil_tx_complete(struct wil6210_priv *wil, int ringid)
7782be7d22fSVladimir Kondratiev {
779795ce734SVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
7802be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
7812be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_tx[ringid];
7822be7d22fSVladimir Kondratiev 
7832be7d22fSVladimir Kondratiev 	if (!vring->va) {
7842be7d22fSVladimir Kondratiev 		wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
7852be7d22fSVladimir Kondratiev 		return;
7862be7d22fSVladimir Kondratiev 	}
7872be7d22fSVladimir Kondratiev 
7882be7d22fSVladimir Kondratiev 	wil_dbg_TXRX(wil, "%s(%d)\n", __func__, ringid);
7892be7d22fSVladimir Kondratiev 
7902be7d22fSVladimir Kondratiev 	while (!wil_vring_is_empty(vring)) {
7912be7d22fSVladimir Kondratiev 		volatile struct vring_tx_desc *d = &vring->va[vring->swtail].tx;
7922be7d22fSVladimir Kondratiev 		dma_addr_t pa;
7932be7d22fSVladimir Kondratiev 		struct sk_buff *skb;
7942be7d22fSVladimir Kondratiev 		if (!(d->dma.status & TX_DMA_STATUS_DU))
7952be7d22fSVladimir Kondratiev 			break;
7962be7d22fSVladimir Kondratiev 
7972be7d22fSVladimir Kondratiev 		wil_dbg_TXRX(wil,
7982be7d22fSVladimir Kondratiev 			     "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
7992be7d22fSVladimir Kondratiev 			     vring->swtail, d->dma.length, d->dma.status,
8002be7d22fSVladimir Kondratiev 			     d->dma.error);
8012be7d22fSVladimir Kondratiev 		wil_hex_dump_TXRX("TxC ", DUMP_PREFIX_NONE, 32, 4,
8022be7d22fSVladimir Kondratiev 				  (const void *)d, sizeof(*d), false);
8032be7d22fSVladimir Kondratiev 
8042be7d22fSVladimir Kondratiev 		pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
8052be7d22fSVladimir Kondratiev 		skb = vring->ctx[vring->swtail];
8062be7d22fSVladimir Kondratiev 		if (skb) {
807795ce734SVladimir Kondratiev 			if (d->dma.error == 0) {
808795ce734SVladimir Kondratiev 				ndev->stats.tx_packets++;
809795ce734SVladimir Kondratiev 				ndev->stats.tx_bytes += skb->len;
810795ce734SVladimir Kondratiev 			} else {
811795ce734SVladimir Kondratiev 				ndev->stats.tx_errors++;
812795ce734SVladimir Kondratiev 			}
813795ce734SVladimir Kondratiev 
8142be7d22fSVladimir Kondratiev 			dma_unmap_single(dev, pa, d->dma.length, DMA_TO_DEVICE);
8152be7d22fSVladimir Kondratiev 			dev_kfree_skb_any(skb);
8162be7d22fSVladimir Kondratiev 			vring->ctx[vring->swtail] = NULL;
8172be7d22fSVladimir Kondratiev 		} else {
8182be7d22fSVladimir Kondratiev 			dma_unmap_page(dev, pa, d->dma.length, DMA_TO_DEVICE);
8192be7d22fSVladimir Kondratiev 		}
8202be7d22fSVladimir Kondratiev 		d->dma.addr_low = 0;
8212be7d22fSVladimir Kondratiev 		d->dma.addr_high = 0;
8222be7d22fSVladimir Kondratiev 		d->dma.length = 0;
8232be7d22fSVladimir Kondratiev 		d->dma.status = TX_DMA_STATUS_DU;
8242be7d22fSVladimir Kondratiev 		vring->swtail = wil_vring_next_tail(vring);
8252be7d22fSVladimir Kondratiev 	}
8262be7d22fSVladimir Kondratiev 	if (wil_vring_avail_tx(vring) > vring->size/4)
8272be7d22fSVladimir Kondratiev 		netif_tx_wake_all_queues(wil_to_ndev(wil));
8282be7d22fSVladimir Kondratiev }
829