12be7d22fSVladimir Kondratiev /*
202525a79SVladimir Kondratiev  * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
32be7d22fSVladimir Kondratiev  *
42be7d22fSVladimir Kondratiev  * Permission to use, copy, modify, and/or distribute this software for any
52be7d22fSVladimir Kondratiev  * purpose with or without fee is hereby granted, provided that the above
62be7d22fSVladimir Kondratiev  * copyright notice and this permission notice appear in all copies.
72be7d22fSVladimir Kondratiev  *
82be7d22fSVladimir Kondratiev  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
92be7d22fSVladimir Kondratiev  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
102be7d22fSVladimir Kondratiev  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
112be7d22fSVladimir Kondratiev  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
122be7d22fSVladimir Kondratiev  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
132be7d22fSVladimir Kondratiev  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
142be7d22fSVladimir Kondratiev  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
152be7d22fSVladimir Kondratiev  */
162be7d22fSVladimir Kondratiev 
172be7d22fSVladimir Kondratiev #include <linux/etherdevice.h>
182be7d22fSVladimir Kondratiev #include <net/ieee80211_radiotap.h>
192be7d22fSVladimir Kondratiev #include <linux/if_arp.h>
202be7d22fSVladimir Kondratiev #include <linux/moduleparam.h>
21504937d4SKirshenbaum Erez #include <linux/ip.h>
22504937d4SKirshenbaum Erez #include <linux/ipv6.h>
23504937d4SKirshenbaum Erez #include <net/ipv6.h>
240786dc4eSVladimir Kondratiev #include <linux/prefetch.h>
252be7d22fSVladimir Kondratiev 
262be7d22fSVladimir Kondratiev #include "wil6210.h"
272be7d22fSVladimir Kondratiev #include "wmi.h"
282be7d22fSVladimir Kondratiev #include "txrx.h"
2998658095SVladimir Kondratiev #include "trace.h"
302be7d22fSVladimir Kondratiev 
312be7d22fSVladimir Kondratiev static bool rtap_include_phy_info;
322be7d22fSVladimir Kondratiev module_param(rtap_include_phy_info, bool, S_IRUGO);
332be7d22fSVladimir Kondratiev MODULE_PARM_DESC(rtap_include_phy_info,
342be7d22fSVladimir Kondratiev 		 " Include PHY info in the radiotap header, default - no");
352be7d22fSVladimir Kondratiev 
362be7d22fSVladimir Kondratiev static inline int wil_vring_is_empty(struct vring *vring)
372be7d22fSVladimir Kondratiev {
382be7d22fSVladimir Kondratiev 	return vring->swhead == vring->swtail;
392be7d22fSVladimir Kondratiev }
402be7d22fSVladimir Kondratiev 
412be7d22fSVladimir Kondratiev static inline u32 wil_vring_next_tail(struct vring *vring)
422be7d22fSVladimir Kondratiev {
432be7d22fSVladimir Kondratiev 	return (vring->swtail + 1) % vring->size;
442be7d22fSVladimir Kondratiev }
452be7d22fSVladimir Kondratiev 
462be7d22fSVladimir Kondratiev static inline void wil_vring_advance_head(struct vring *vring, int n)
472be7d22fSVladimir Kondratiev {
482be7d22fSVladimir Kondratiev 	vring->swhead = (vring->swhead + n) % vring->size;
492be7d22fSVladimir Kondratiev }
502be7d22fSVladimir Kondratiev 
512be7d22fSVladimir Kondratiev static inline int wil_vring_is_full(struct vring *vring)
522be7d22fSVladimir Kondratiev {
532be7d22fSVladimir Kondratiev 	return wil_vring_next_tail(vring) == vring->swhead;
542be7d22fSVladimir Kondratiev }
558fe59627SVladimir Kondratiev 
562be7d22fSVladimir Kondratiev /*
572be7d22fSVladimir Kondratiev  * Available space in Tx Vring
582be7d22fSVladimir Kondratiev  */
592be7d22fSVladimir Kondratiev static inline int wil_vring_avail_tx(struct vring *vring)
602be7d22fSVladimir Kondratiev {
612be7d22fSVladimir Kondratiev 	u32 swhead = vring->swhead;
622be7d22fSVladimir Kondratiev 	u32 swtail = vring->swtail;
632be7d22fSVladimir Kondratiev 	int used = (vring->size + swhead - swtail) % vring->size;
642be7d22fSVladimir Kondratiev 
652be7d22fSVladimir Kondratiev 	return vring->size - used - 1;
662be7d22fSVladimir Kondratiev }
672be7d22fSVladimir Kondratiev 
685bb6423eSVladimir Kondratiev /**
695bb6423eSVladimir Kondratiev  * wil_vring_wmark_low - low watermark for available descriptor space
705bb6423eSVladimir Kondratiev  */
715bb6423eSVladimir Kondratiev static inline int wil_vring_wmark_low(struct vring *vring)
725bb6423eSVladimir Kondratiev {
735bb6423eSVladimir Kondratiev 	return vring->size/8;
745bb6423eSVladimir Kondratiev }
755bb6423eSVladimir Kondratiev 
765bb6423eSVladimir Kondratiev /**
775bb6423eSVladimir Kondratiev  * wil_vring_wmark_high - high watermark for available descriptor space
785bb6423eSVladimir Kondratiev  */
795bb6423eSVladimir Kondratiev static inline int wil_vring_wmark_high(struct vring *vring)
805bb6423eSVladimir Kondratiev {
815bb6423eSVladimir Kondratiev 	return vring->size/4;
825bb6423eSVladimir Kondratiev }
835bb6423eSVladimir Kondratiev 
842be7d22fSVladimir Kondratiev static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
852be7d22fSVladimir Kondratiev {
862be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
872be7d22fSVladimir Kondratiev 	size_t sz = vring->size * sizeof(vring->va[0]);
882be7d22fSVladimir Kondratiev 	uint i;
892be7d22fSVladimir Kondratiev 
909cf10d62SVladimir Kondratiev 	wil_dbg_misc(wil, "%s()\n", __func__);
919cf10d62SVladimir Kondratiev 
922be7d22fSVladimir Kondratiev 	BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
932be7d22fSVladimir Kondratiev 
942be7d22fSVladimir Kondratiev 	vring->swhead = 0;
952be7d22fSVladimir Kondratiev 	vring->swtail = 0;
96f88f113aSVladimir Kondratiev 	vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
972be7d22fSVladimir Kondratiev 	if (!vring->ctx) {
982be7d22fSVladimir Kondratiev 		vring->va = NULL;
992be7d22fSVladimir Kondratiev 		return -ENOMEM;
1002be7d22fSVladimir Kondratiev 	}
1012be7d22fSVladimir Kondratiev 	/*
1022be7d22fSVladimir Kondratiev 	 * vring->va should be aligned on its size rounded up to power of 2
1032be7d22fSVladimir Kondratiev 	 * This is granted by the dma_alloc_coherent
1042be7d22fSVladimir Kondratiev 	 */
1052be7d22fSVladimir Kondratiev 	vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
1062be7d22fSVladimir Kondratiev 	if (!vring->va) {
1072be7d22fSVladimir Kondratiev 		kfree(vring->ctx);
1082be7d22fSVladimir Kondratiev 		vring->ctx = NULL;
1092be7d22fSVladimir Kondratiev 		return -ENOMEM;
1102be7d22fSVladimir Kondratiev 	}
1112be7d22fSVladimir Kondratiev 	/* initially, all descriptors are SW owned
1122be7d22fSVladimir Kondratiev 	 * For Tx and Rx, ownership bit is at the same location, thus
1132be7d22fSVladimir Kondratiev 	 * we can use any
1142be7d22fSVladimir Kondratiev 	 */
1152be7d22fSVladimir Kondratiev 	for (i = 0; i < vring->size; i++) {
1168fe59627SVladimir Kondratiev 		volatile struct vring_tx_desc *_d = &vring->va[i].tx;
1178fe59627SVladimir Kondratiev 
11868ada71eSVladimir Kondratiev 		_d->dma.status = TX_DMA_STATUS_DU;
1192be7d22fSVladimir Kondratiev 	}
1202be7d22fSVladimir Kondratiev 
12139c52ee8SVladimir Kondratiev 	wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
12239c52ee8SVladimir Kondratiev 		     vring->va, &vring->pa, vring->ctx);
1232be7d22fSVladimir Kondratiev 
1242be7d22fSVladimir Kondratiev 	return 0;
1252be7d22fSVladimir Kondratiev }
1262be7d22fSVladimir Kondratiev 
1272232abd5SVladimir Kondratiev static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d,
1282232abd5SVladimir Kondratiev 			     struct wil_ctx *ctx)
1292232abd5SVladimir Kondratiev {
1302232abd5SVladimir Kondratiev 	dma_addr_t pa = wil_desc_addr(&d->dma.addr);
1312232abd5SVladimir Kondratiev 	u16 dmalen = le16_to_cpu(d->dma.length);
1328fe59627SVladimir Kondratiev 
1332232abd5SVladimir Kondratiev 	switch (ctx->mapped_as) {
1342232abd5SVladimir Kondratiev 	case wil_mapped_as_single:
1352232abd5SVladimir Kondratiev 		dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
1362232abd5SVladimir Kondratiev 		break;
1372232abd5SVladimir Kondratiev 	case wil_mapped_as_page:
1382232abd5SVladimir Kondratiev 		dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
1392232abd5SVladimir Kondratiev 		break;
1402232abd5SVladimir Kondratiev 	default:
1412232abd5SVladimir Kondratiev 		break;
1422232abd5SVladimir Kondratiev 	}
1432232abd5SVladimir Kondratiev }
1442232abd5SVladimir Kondratiev 
1452be7d22fSVladimir Kondratiev static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
1462be7d22fSVladimir Kondratiev 			   int tx)
1472be7d22fSVladimir Kondratiev {
1482be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
1492be7d22fSVladimir Kondratiev 	size_t sz = vring->size * sizeof(vring->va[0]);
1502be7d22fSVladimir Kondratiev 
151ef77285fSVladimir Kondratiev 	if (tx) {
152ef77285fSVladimir Kondratiev 		int vring_index = vring - wil->vring_tx;
153ef77285fSVladimir Kondratiev 
154ef77285fSVladimir Kondratiev 		wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
155ef77285fSVladimir Kondratiev 			     vring_index, vring->size, vring->va,
156ef77285fSVladimir Kondratiev 			     &vring->pa, vring->ctx);
157ef77285fSVladimir Kondratiev 	} else {
158ef77285fSVladimir Kondratiev 		wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
159ef77285fSVladimir Kondratiev 			     vring->size, vring->va,
160ef77285fSVladimir Kondratiev 			     &vring->pa, vring->ctx);
161ef77285fSVladimir Kondratiev 	}
162ef77285fSVladimir Kondratiev 
1632be7d22fSVladimir Kondratiev 	while (!wil_vring_is_empty(vring)) {
16468ada71eSVladimir Kondratiev 		dma_addr_t pa;
1657e594444SVladimir Kondratiev 		u16 dmalen;
166f88f113aSVladimir Kondratiev 		struct wil_ctx *ctx;
16768ada71eSVladimir Kondratiev 
1682be7d22fSVladimir Kondratiev 		if (tx) {
16968ada71eSVladimir Kondratiev 			struct vring_tx_desc dd, *d = &dd;
17068ada71eSVladimir Kondratiev 			volatile struct vring_tx_desc *_d =
1712be7d22fSVladimir Kondratiev 					&vring->va[vring->swtail].tx;
17268ada71eSVladimir Kondratiev 
173f88f113aSVladimir Kondratiev 			ctx = &vring->ctx[vring->swtail];
17468ada71eSVladimir Kondratiev 			*d = *_d;
1752232abd5SVladimir Kondratiev 			wil_txdesc_unmap(dev, d, ctx);
176f88f113aSVladimir Kondratiev 			if (ctx->skb)
177f88f113aSVladimir Kondratiev 				dev_kfree_skb_any(ctx->skb);
1782be7d22fSVladimir Kondratiev 			vring->swtail = wil_vring_next_tail(vring);
1792be7d22fSVladimir Kondratiev 		} else { /* rx */
18068ada71eSVladimir Kondratiev 			struct vring_rx_desc dd, *d = &dd;
18168ada71eSVladimir Kondratiev 			volatile struct vring_rx_desc *_d =
1824d1ac072SVladimir Kondratiev 					&vring->va[vring->swhead].rx;
18368ada71eSVladimir Kondratiev 
184f88f113aSVladimir Kondratiev 			ctx = &vring->ctx[vring->swhead];
18568ada71eSVladimir Kondratiev 			*d = *_d;
18668ada71eSVladimir Kondratiev 			pa = wil_desc_addr(&d->dma.addr);
1877e594444SVladimir Kondratiev 			dmalen = le16_to_cpu(d->dma.length);
1887e594444SVladimir Kondratiev 			dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
189f88f113aSVladimir Kondratiev 			kfree_skb(ctx->skb);
1902be7d22fSVladimir Kondratiev 			wil_vring_advance_head(vring, 1);
1912be7d22fSVladimir Kondratiev 		}
1922be7d22fSVladimir Kondratiev 	}
1932be7d22fSVladimir Kondratiev 	dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
1942be7d22fSVladimir Kondratiev 	kfree(vring->ctx);
1952be7d22fSVladimir Kondratiev 	vring->pa = 0;
1962be7d22fSVladimir Kondratiev 	vring->va = NULL;
1972be7d22fSVladimir Kondratiev 	vring->ctx = NULL;
1982be7d22fSVladimir Kondratiev }
1992be7d22fSVladimir Kondratiev 
2002be7d22fSVladimir Kondratiev /**
2012be7d22fSVladimir Kondratiev  * Allocate one skb for Rx VRING
2022be7d22fSVladimir Kondratiev  *
2032be7d22fSVladimir Kondratiev  * Safe to call from IRQ
2042be7d22fSVladimir Kondratiev  */
2052be7d22fSVladimir Kondratiev static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
2062be7d22fSVladimir Kondratiev 			       u32 i, int headroom)
2072be7d22fSVladimir Kondratiev {
2082be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
2099a06bec9SVladimir Kondratiev 	unsigned int sz = mtu_max + ETH_HLEN;
21068ada71eSVladimir Kondratiev 	struct vring_rx_desc dd, *d = &dd;
2118fe59627SVladimir Kondratiev 	volatile struct vring_rx_desc *_d = &vring->va[i].rx;
2122be7d22fSVladimir Kondratiev 	dma_addr_t pa;
2132be7d22fSVladimir Kondratiev 	struct sk_buff *skb = dev_alloc_skb(sz + headroom);
2148fe59627SVladimir Kondratiev 
2152be7d22fSVladimir Kondratiev 	if (unlikely(!skb))
2162be7d22fSVladimir Kondratiev 		return -ENOMEM;
2172be7d22fSVladimir Kondratiev 
2182be7d22fSVladimir Kondratiev 	skb_reserve(skb, headroom);
2192be7d22fSVladimir Kondratiev 	skb_put(skb, sz);
2202be7d22fSVladimir Kondratiev 
2212be7d22fSVladimir Kondratiev 	pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
2222be7d22fSVladimir Kondratiev 	if (unlikely(dma_mapping_error(dev, pa))) {
2232be7d22fSVladimir Kondratiev 		kfree_skb(skb);
2242be7d22fSVladimir Kondratiev 		return -ENOMEM;
2252be7d22fSVladimir Kondratiev 	}
2262be7d22fSVladimir Kondratiev 
2272be7d22fSVladimir Kondratiev 	d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
22868ada71eSVladimir Kondratiev 	wil_desc_addr_set(&d->dma.addr, pa);
2292be7d22fSVladimir Kondratiev 	/* ip_length don't care */
2302be7d22fSVladimir Kondratiev 	/* b11 don't care */
2312be7d22fSVladimir Kondratiev 	/* error don't care */
2322be7d22fSVladimir Kondratiev 	d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
2337e594444SVladimir Kondratiev 	d->dma.length = cpu_to_le16(sz);
23468ada71eSVladimir Kondratiev 	*_d = *d;
235f88f113aSVladimir Kondratiev 	vring->ctx[i].skb = skb;
2362be7d22fSVladimir Kondratiev 
2372be7d22fSVladimir Kondratiev 	return 0;
2382be7d22fSVladimir Kondratiev }
2392be7d22fSVladimir Kondratiev 
2402be7d22fSVladimir Kondratiev /**
2412be7d22fSVladimir Kondratiev  * Adds radiotap header
2422be7d22fSVladimir Kondratiev  *
2432be7d22fSVladimir Kondratiev  * Any error indicated as "Bad FCS"
2442be7d22fSVladimir Kondratiev  *
2452be7d22fSVladimir Kondratiev  * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
2462be7d22fSVladimir Kondratiev  *  - Rx descriptor: 32 bytes
2472be7d22fSVladimir Kondratiev  *  - Phy info
2482be7d22fSVladimir Kondratiev  */
2492be7d22fSVladimir Kondratiev static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
25033e61169SVladimir Kondratiev 				       struct sk_buff *skb)
2512be7d22fSVladimir Kondratiev {
2522be7d22fSVladimir Kondratiev 	struct wireless_dev *wdev = wil->wdev;
2532be7d22fSVladimir Kondratiev 	struct wil6210_rtap {
2542be7d22fSVladimir Kondratiev 		struct ieee80211_radiotap_header rthdr;
2552be7d22fSVladimir Kondratiev 		/* fields should be in the order of bits in rthdr.it_present */
2562be7d22fSVladimir Kondratiev 		/* flags */
2572be7d22fSVladimir Kondratiev 		u8 flags;
2582be7d22fSVladimir Kondratiev 		/* channel */
2592be7d22fSVladimir Kondratiev 		__le16 chnl_freq __aligned(2);
2602be7d22fSVladimir Kondratiev 		__le16 chnl_flags;
2612be7d22fSVladimir Kondratiev 		/* MCS */
2622be7d22fSVladimir Kondratiev 		u8 mcs_present;
2632be7d22fSVladimir Kondratiev 		u8 mcs_flags;
2642be7d22fSVladimir Kondratiev 		u8 mcs_index;
2652be7d22fSVladimir Kondratiev 	} __packed;
2662be7d22fSVladimir Kondratiev 	struct wil6210_rtap_vendor {
2672be7d22fSVladimir Kondratiev 		struct wil6210_rtap rtap;
2682be7d22fSVladimir Kondratiev 		/* vendor */
2692be7d22fSVladimir Kondratiev 		u8 vendor_oui[3] __aligned(2);
2702be7d22fSVladimir Kondratiev 		u8 vendor_ns;
2712be7d22fSVladimir Kondratiev 		__le16 vendor_skip;
2722be7d22fSVladimir Kondratiev 		u8 vendor_data[0];
2732be7d22fSVladimir Kondratiev 	} __packed;
27433e61169SVladimir Kondratiev 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
2752be7d22fSVladimir Kondratiev 	struct wil6210_rtap_vendor *rtap_vendor;
2762be7d22fSVladimir Kondratiev 	int rtap_len = sizeof(struct wil6210_rtap);
2772be7d22fSVladimir Kondratiev 	int phy_length = 0; /* phy info header size, bytes */
2782be7d22fSVladimir Kondratiev 	static char phy_data[128];
2792be7d22fSVladimir Kondratiev 	struct ieee80211_channel *ch = wdev->preset_chandef.chan;
2802be7d22fSVladimir Kondratiev 
2812be7d22fSVladimir Kondratiev 	if (rtap_include_phy_info) {
2822be7d22fSVladimir Kondratiev 		rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
2832be7d22fSVladimir Kondratiev 		/* calculate additional length */
2842be7d22fSVladimir Kondratiev 		if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
2852be7d22fSVladimir Kondratiev 			/**
2862be7d22fSVladimir Kondratiev 			 * PHY info starts from 8-byte boundary
2872be7d22fSVladimir Kondratiev 			 * there are 8-byte lines, last line may be partially
2882be7d22fSVladimir Kondratiev 			 * written (HW bug), thus FW configures for last line
2892be7d22fSVladimir Kondratiev 			 * to be excessive. Driver skips this last line.
2902be7d22fSVladimir Kondratiev 			 */
2912be7d22fSVladimir Kondratiev 			int len = min_t(int, 8 + sizeof(phy_data),
2922be7d22fSVladimir Kondratiev 					wil_rxdesc_phy_length(d));
2938fe59627SVladimir Kondratiev 
2942be7d22fSVladimir Kondratiev 			if (len > 8) {
2952be7d22fSVladimir Kondratiev 				void *p = skb_tail_pointer(skb);
2962be7d22fSVladimir Kondratiev 				void *pa = PTR_ALIGN(p, 8);
2978fe59627SVladimir Kondratiev 
2982be7d22fSVladimir Kondratiev 				if (skb_tailroom(skb) >= len + (pa - p)) {
2992be7d22fSVladimir Kondratiev 					phy_length = len - 8;
3002be7d22fSVladimir Kondratiev 					memcpy(phy_data, pa, phy_length);
3012be7d22fSVladimir Kondratiev 				}
3022be7d22fSVladimir Kondratiev 			}
3032be7d22fSVladimir Kondratiev 		}
3042be7d22fSVladimir Kondratiev 		rtap_len += phy_length;
3052be7d22fSVladimir Kondratiev 	}
3062be7d22fSVladimir Kondratiev 
3072be7d22fSVladimir Kondratiev 	if (skb_headroom(skb) < rtap_len &&
3082be7d22fSVladimir Kondratiev 	    pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
3092be7d22fSVladimir Kondratiev 		wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
3102be7d22fSVladimir Kondratiev 		return;
3112be7d22fSVladimir Kondratiev 	}
3122be7d22fSVladimir Kondratiev 
3132be7d22fSVladimir Kondratiev 	rtap_vendor = (void *)skb_push(skb, rtap_len);
3142be7d22fSVladimir Kondratiev 	memset(rtap_vendor, 0, rtap_len);
3152be7d22fSVladimir Kondratiev 
3162be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
3172be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
3182be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
3192be7d22fSVladimir Kondratiev 			(1 << IEEE80211_RADIOTAP_FLAGS) |
3202be7d22fSVladimir Kondratiev 			(1 << IEEE80211_RADIOTAP_CHANNEL) |
3212be7d22fSVladimir Kondratiev 			(1 << IEEE80211_RADIOTAP_MCS));
3222be7d22fSVladimir Kondratiev 	if (d->dma.status & RX_DMA_STATUS_ERROR)
3232be7d22fSVladimir Kondratiev 		rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
3242be7d22fSVladimir Kondratiev 
3252be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
3262be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
3272be7d22fSVladimir Kondratiev 
3282be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
3292be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.mcs_flags = 0;
3302be7d22fSVladimir Kondratiev 	rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
3312be7d22fSVladimir Kondratiev 
3322be7d22fSVladimir Kondratiev 	if (rtap_include_phy_info) {
3332be7d22fSVladimir Kondratiev 		rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
3342be7d22fSVladimir Kondratiev 				IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
3352be7d22fSVladimir Kondratiev 		/* OUI for Wilocity 04:ce:14 */
3362be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_oui[0] = 0x04;
3372be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_oui[1] = 0xce;
3382be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_oui[2] = 0x14;
3392be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_ns = 1;
3402be7d22fSVladimir Kondratiev 		/* Rx descriptor + PHY data  */
3412be7d22fSVladimir Kondratiev 		rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
3422be7d22fSVladimir Kondratiev 						       phy_length);
3432be7d22fSVladimir Kondratiev 		memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
3442be7d22fSVladimir Kondratiev 		memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
3452be7d22fSVladimir Kondratiev 		       phy_length);
3462be7d22fSVladimir Kondratiev 	}
3472be7d22fSVladimir Kondratiev }
3482be7d22fSVladimir Kondratiev 
3492be7d22fSVladimir Kondratiev /*
3502be7d22fSVladimir Kondratiev  * Fast swap in place between 2 registers
3512be7d22fSVladimir Kondratiev  */
3522be7d22fSVladimir Kondratiev static void wil_swap_u16(u16 *a, u16 *b)
3532be7d22fSVladimir Kondratiev {
3542be7d22fSVladimir Kondratiev 	*a ^= *b;
3552be7d22fSVladimir Kondratiev 	*b ^= *a;
3562be7d22fSVladimir Kondratiev 	*a ^= *b;
3572be7d22fSVladimir Kondratiev }
3582be7d22fSVladimir Kondratiev 
3592be7d22fSVladimir Kondratiev static void wil_swap_ethaddr(void *data)
3602be7d22fSVladimir Kondratiev {
3612be7d22fSVladimir Kondratiev 	struct ethhdr *eth = data;
3622be7d22fSVladimir Kondratiev 	u16 *s = (u16 *)eth->h_source;
3632be7d22fSVladimir Kondratiev 	u16 *d = (u16 *)eth->h_dest;
3642be7d22fSVladimir Kondratiev 
3652be7d22fSVladimir Kondratiev 	wil_swap_u16(s++, d++);
3662be7d22fSVladimir Kondratiev 	wil_swap_u16(s++, d++);
3672be7d22fSVladimir Kondratiev 	wil_swap_u16(s, d);
3682be7d22fSVladimir Kondratiev }
3692be7d22fSVladimir Kondratiev 
3702be7d22fSVladimir Kondratiev /**
3712be7d22fSVladimir Kondratiev  * reap 1 frame from @swhead
3722be7d22fSVladimir Kondratiev  *
37333e61169SVladimir Kondratiev  * Rx descriptor copied to skb->cb
37433e61169SVladimir Kondratiev  *
3752be7d22fSVladimir Kondratiev  * Safe to call from IRQ
3762be7d22fSVladimir Kondratiev  */
3772be7d22fSVladimir Kondratiev static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
3782be7d22fSVladimir Kondratiev 					 struct vring *vring)
3792be7d22fSVladimir Kondratiev {
3802be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
3812be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
38268ada71eSVladimir Kondratiev 	volatile struct vring_rx_desc *_d;
38368ada71eSVladimir Kondratiev 	struct vring_rx_desc *d;
3842be7d22fSVladimir Kondratiev 	struct sk_buff *skb;
3852be7d22fSVladimir Kondratiev 	dma_addr_t pa;
3869a06bec9SVladimir Kondratiev 	unsigned int sz = mtu_max + ETH_HLEN;
3877e594444SVladimir Kondratiev 	u16 dmalen;
3882be7d22fSVladimir Kondratiev 	u8 ftype;
3892be7d22fSVladimir Kondratiev 	u8 ds_bits;
390c8b78b5fSVladimir Kondratiev 	int cid;
391c8b78b5fSVladimir Kondratiev 	struct wil_net_stats *stats;
392c8b78b5fSVladimir Kondratiev 
39333e61169SVladimir Kondratiev 	BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
39433e61169SVladimir Kondratiev 
3952be7d22fSVladimir Kondratiev 	if (wil_vring_is_empty(vring))
3962be7d22fSVladimir Kondratiev 		return NULL;
3972be7d22fSVladimir Kondratiev 
3988fe59627SVladimir Kondratiev 	_d = &vring->va[vring->swhead].rx;
39968ada71eSVladimir Kondratiev 	if (!(_d->dma.status & RX_DMA_STATUS_DU)) {
4002be7d22fSVladimir Kondratiev 		/* it is not error, we just reached end of Rx done area */
4012be7d22fSVladimir Kondratiev 		return NULL;
4022be7d22fSVladimir Kondratiev 	}
4032be7d22fSVladimir Kondratiev 
404f88f113aSVladimir Kondratiev 	skb = vring->ctx[vring->swhead].skb;
40568ada71eSVladimir Kondratiev 	d = wil_skb_rxdesc(skb);
40668ada71eSVladimir Kondratiev 	*d = *_d;
40768ada71eSVladimir Kondratiev 	pa = wil_desc_addr(&d->dma.addr);
408f88f113aSVladimir Kondratiev 	vring->ctx[vring->swhead].skb = NULL;
409e270045bSVladimir Kondratiev 	wil_vring_advance_head(vring, 1);
41068ada71eSVladimir Kondratiev 
41168ada71eSVladimir Kondratiev 	dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
41268ada71eSVladimir Kondratiev 	dmalen = le16_to_cpu(d->dma.length);
41368ada71eSVladimir Kondratiev 
41468ada71eSVladimir Kondratiev 	trace_wil6210_rx(vring->swhead, d);
41568ada71eSVladimir Kondratiev 	wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen);
41668ada71eSVladimir Kondratiev 	wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
41768ada71eSVladimir Kondratiev 			  (const void *)d, sizeof(*d), false);
41868ada71eSVladimir Kondratiev 
419e270045bSVladimir Kondratiev 	if (dmalen > sz) {
420e270045bSVladimir Kondratiev 		wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
421110dea00SWei Yongjun 		kfree_skb(skb);
422e270045bSVladimir Kondratiev 		return NULL;
423e270045bSVladimir Kondratiev 	}
4247e594444SVladimir Kondratiev 	skb_trim(skb, dmalen);
42533e61169SVladimir Kondratiev 
4261cbbcb08SVladimir Kondratiev 	prefetch(skb->data);
4271cbbcb08SVladimir Kondratiev 
428c0d37713SVladimir Kondratiev 	wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
429c0d37713SVladimir Kondratiev 			  skb->data, skb_headlen(skb), false);
430c0d37713SVladimir Kondratiev 
431c8b78b5fSVladimir Kondratiev 	cid = wil_rxdesc_cid(d);
432c8b78b5fSVladimir Kondratiev 	stats = &wil->sta[cid].stats;
433c8b78b5fSVladimir Kondratiev 	stats->last_mcs_rx = wil_rxdesc_mcs(d);
4342be7d22fSVladimir Kondratiev 
4352be7d22fSVladimir Kondratiev 	/* use radiotap header only if required */
4362be7d22fSVladimir Kondratiev 	if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
43733e61169SVladimir Kondratiev 		wil_rx_add_radiotap_header(wil, skb);
4382be7d22fSVladimir Kondratiev 
4392be7d22fSVladimir Kondratiev 	/* no extra checks if in sniffer mode */
4402be7d22fSVladimir Kondratiev 	if (ndev->type != ARPHRD_ETHER)
4412be7d22fSVladimir Kondratiev 		return skb;
4422be7d22fSVladimir Kondratiev 	/*
4432be7d22fSVladimir Kondratiev 	 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
4442be7d22fSVladimir Kondratiev 	 * Driver should recognize it by frame type, that is found
4452be7d22fSVladimir Kondratiev 	 * in Rx descriptor. If type is not data, it is 802.11 frame as is
4462be7d22fSVladimir Kondratiev 	 */
44768ada71eSVladimir Kondratiev 	ftype = wil_rxdesc_ftype(d) << 2;
4482be7d22fSVladimir Kondratiev 	if (ftype != IEEE80211_FTYPE_DATA) {
4497743882dSVladimir Kondratiev 		wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
4502be7d22fSVladimir Kondratiev 		/* TODO: process it */
4512be7d22fSVladimir Kondratiev 		kfree_skb(skb);
4522be7d22fSVladimir Kondratiev 		return NULL;
4532be7d22fSVladimir Kondratiev 	}
4542be7d22fSVladimir Kondratiev 
4552be7d22fSVladimir Kondratiev 	if (skb->len < ETH_HLEN) {
4562be7d22fSVladimir Kondratiev 		wil_err(wil, "Short frame, len = %d\n", skb->len);
4572be7d22fSVladimir Kondratiev 		/* TODO: process it (i.e. BAR) */
4582be7d22fSVladimir Kondratiev 		kfree_skb(skb);
4592be7d22fSVladimir Kondratiev 		return NULL;
4602be7d22fSVladimir Kondratiev 	}
4612be7d22fSVladimir Kondratiev 
462504937d4SKirshenbaum Erez 	/* L4 IDENT is on when HW calculated checksum, check status
463504937d4SKirshenbaum Erez 	 * and in case of error drop the packet
464504937d4SKirshenbaum Erez 	 * higher stack layers will handle retransmission (if required)
465504937d4SKirshenbaum Erez 	 */
466504937d4SKirshenbaum Erez 	if (d->dma.status & RX_DMA_STATUS_L4_IDENT) {
467504937d4SKirshenbaum Erez 		/* L4 protocol identified, csum calculated */
4684a68ab10SVladimir Kondratiev 		if ((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0)
469504937d4SKirshenbaum Erez 			skb->ip_summed = CHECKSUM_UNNECESSARY;
4704a68ab10SVladimir Kondratiev 		/* If HW reports bad checksum, let IP stack re-check it
4714a68ab10SVladimir Kondratiev 		 * For example, HW don't understand Microsoft IP stack that
4724a68ab10SVladimir Kondratiev 		 * mis-calculates TCP checksum - if it should be 0x0,
4734a68ab10SVladimir Kondratiev 		 * it writes 0xffff in violation of RFC 1624
4744a68ab10SVladimir Kondratiev 		 */
475504937d4SKirshenbaum Erez 	}
476504937d4SKirshenbaum Erez 
47768ada71eSVladimir Kondratiev 	ds_bits = wil_rxdesc_ds_bits(d);
4782be7d22fSVladimir Kondratiev 	if (ds_bits == 1) {
4792be7d22fSVladimir Kondratiev 		/*
4802be7d22fSVladimir Kondratiev 		 * HW bug - in ToDS mode, i.e. Rx on AP side,
4812be7d22fSVladimir Kondratiev 		 * addresses get swapped
4822be7d22fSVladimir Kondratiev 		 */
4832be7d22fSVladimir Kondratiev 		wil_swap_ethaddr(skb->data);
4842be7d22fSVladimir Kondratiev 	}
4852be7d22fSVladimir Kondratiev 
4862be7d22fSVladimir Kondratiev 	return skb;
4872be7d22fSVladimir Kondratiev }
4882be7d22fSVladimir Kondratiev 
4892be7d22fSVladimir Kondratiev /**
4902be7d22fSVladimir Kondratiev  * allocate and fill up to @count buffers in rx ring
4912be7d22fSVladimir Kondratiev  * buffers posted at @swtail
4922be7d22fSVladimir Kondratiev  */
4932be7d22fSVladimir Kondratiev static int wil_rx_refill(struct wil6210_priv *wil, int count)
4942be7d22fSVladimir Kondratiev {
4952be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
4962be7d22fSVladimir Kondratiev 	struct vring *v = &wil->vring_rx;
4972be7d22fSVladimir Kondratiev 	u32 next_tail;
4982be7d22fSVladimir Kondratiev 	int rc = 0;
4992be7d22fSVladimir Kondratiev 	int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
5002be7d22fSVladimir Kondratiev 			WIL6210_RTAP_SIZE : 0;
5012be7d22fSVladimir Kondratiev 
5022be7d22fSVladimir Kondratiev 	for (; next_tail = wil_vring_next_tail(v),
5032be7d22fSVladimir Kondratiev 			(next_tail != v->swhead) && (count-- > 0);
5042be7d22fSVladimir Kondratiev 			v->swtail = next_tail) {
5052be7d22fSVladimir Kondratiev 		rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
5062be7d22fSVladimir Kondratiev 		if (rc) {
5072be7d22fSVladimir Kondratiev 			wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
5082be7d22fSVladimir Kondratiev 				rc, v->swtail);
5092be7d22fSVladimir Kondratiev 			break;
5102be7d22fSVladimir Kondratiev 		}
5112be7d22fSVladimir Kondratiev 	}
5122be7d22fSVladimir Kondratiev 	iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
5132be7d22fSVladimir Kondratiev 
5142be7d22fSVladimir Kondratiev 	return rc;
5152be7d22fSVladimir Kondratiev }
5162be7d22fSVladimir Kondratiev 
5172be7d22fSVladimir Kondratiev /*
5182be7d22fSVladimir Kondratiev  * Pass Rx packet to the netif. Update statistics.
519e0287c4aSVladimir Kondratiev  * Called in softirq context (NAPI poll).
5202be7d22fSVladimir Kondratiev  */
521b4490f42SVladimir Kondratiev void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
5222be7d22fSVladimir Kondratiev {
523b5998e6aSVladimir Kondratiev 	gro_result_t rc;
524c8b78b5fSVladimir Kondratiev 	struct wil6210_priv *wil = ndev_to_wil(ndev);
5252be7d22fSVladimir Kondratiev 	unsigned int len = skb->len;
526c8b78b5fSVladimir Kondratiev 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
527c8b78b5fSVladimir Kondratiev 	int cid = wil_rxdesc_cid(d);
528c8b78b5fSVladimir Kondratiev 	struct wil_net_stats *stats = &wil->sta[cid].stats;
5292be7d22fSVladimir Kondratiev 
530241804cbSVladimir Kondratiev 	skb_orphan(skb);
531241804cbSVladimir Kondratiev 
532b5998e6aSVladimir Kondratiev 	rc = napi_gro_receive(&wil->napi_rx, skb);
5332be7d22fSVladimir Kondratiev 
534b5998e6aSVladimir Kondratiev 	if (unlikely(rc == GRO_DROP)) {
535b5998e6aSVladimir Kondratiev 		ndev->stats.rx_dropped++;
536b5998e6aSVladimir Kondratiev 		stats->rx_dropped++;
537b5998e6aSVladimir Kondratiev 		wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
538b5998e6aSVladimir Kondratiev 	} else {
5392be7d22fSVladimir Kondratiev 		ndev->stats.rx_packets++;
540c8b78b5fSVladimir Kondratiev 		stats->rx_packets++;
5412be7d22fSVladimir Kondratiev 		ndev->stats.rx_bytes += len;
542c8b78b5fSVladimir Kondratiev 		stats->rx_bytes += len;
5432be7d22fSVladimir Kondratiev 	}
544194b482bSVladimir Kondratiev 	{
545194b482bSVladimir Kondratiev 		static const char * const gro_res_str[] = {
546194b482bSVladimir Kondratiev 			[GRO_MERGED]		= "GRO_MERGED",
547194b482bSVladimir Kondratiev 			[GRO_MERGED_FREE]	= "GRO_MERGED_FREE",
548194b482bSVladimir Kondratiev 			[GRO_HELD]		= "GRO_HELD",
549194b482bSVladimir Kondratiev 			[GRO_NORMAL]		= "GRO_NORMAL",
550194b482bSVladimir Kondratiev 			[GRO_DROP]		= "GRO_DROP",
551194b482bSVladimir Kondratiev 		};
5528fe59627SVladimir Kondratiev 		wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
553194b482bSVladimir Kondratiev 			     len, gro_res_str[rc]);
554194b482bSVladimir Kondratiev 	}
5552be7d22fSVladimir Kondratiev }
5562be7d22fSVladimir Kondratiev 
5572be7d22fSVladimir Kondratiev /**
5582be7d22fSVladimir Kondratiev  * Proceed all completed skb's from Rx VRING
5592be7d22fSVladimir Kondratiev  *
560e0287c4aSVladimir Kondratiev  * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
5612be7d22fSVladimir Kondratiev  */
562e0287c4aSVladimir Kondratiev void wil_rx_handle(struct wil6210_priv *wil, int *quota)
5632be7d22fSVladimir Kondratiev {
5642be7d22fSVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
5652be7d22fSVladimir Kondratiev 	struct vring *v = &wil->vring_rx;
5662be7d22fSVladimir Kondratiev 	struct sk_buff *skb;
5672be7d22fSVladimir Kondratiev 
5682be7d22fSVladimir Kondratiev 	if (!v->va) {
5692be7d22fSVladimir Kondratiev 		wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
5702be7d22fSVladimir Kondratiev 		return;
5712be7d22fSVladimir Kondratiev 	}
5727743882dSVladimir Kondratiev 	wil_dbg_txrx(wil, "%s()\n", __func__);
573e0287c4aSVladimir Kondratiev 	while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
574e0287c4aSVladimir Kondratiev 		(*quota)--;
5752be7d22fSVladimir Kondratiev 
5762be7d22fSVladimir Kondratiev 		if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
5772be7d22fSVladimir Kondratiev 			skb->dev = ndev;
5782be7d22fSVladimir Kondratiev 			skb_reset_mac_header(skb);
5792be7d22fSVladimir Kondratiev 			skb->ip_summed = CHECKSUM_UNNECESSARY;
5802be7d22fSVladimir Kondratiev 			skb->pkt_type = PACKET_OTHERHOST;
5812be7d22fSVladimir Kondratiev 			skb->protocol = htons(ETH_P_802_2);
582b4490f42SVladimir Kondratiev 			wil_netif_rx_any(skb, ndev);
5832be7d22fSVladimir Kondratiev 		} else {
5840bbc4adeSVladimir Kondratiev 			struct ethhdr *eth = (void *)skb->data;
5850bbc4adeSVladimir Kondratiev 
5862be7d22fSVladimir Kondratiev 			skb->protocol = eth_type_trans(skb, ndev);
5870bbc4adeSVladimir Kondratiev 
5880bbc4adeSVladimir Kondratiev 			if (is_unicast_ether_addr(eth->h_dest))
589b4490f42SVladimir Kondratiev 				wil_rx_reorder(wil, skb);
5900bbc4adeSVladimir Kondratiev 			else
5910bbc4adeSVladimir Kondratiev 				wil_netif_rx_any(skb, ndev);
5922be7d22fSVladimir Kondratiev 		}
5932be7d22fSVladimir Kondratiev 	}
5942be7d22fSVladimir Kondratiev 	wil_rx_refill(wil, v->size);
5952be7d22fSVladimir Kondratiev }
5962be7d22fSVladimir Kondratiev 
597d3762b40SVladimir Kondratiev int wil_rx_init(struct wil6210_priv *wil, u16 size)
5982be7d22fSVladimir Kondratiev {
5992be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_rx;
6002be7d22fSVladimir Kondratiev 	int rc;
6012be7d22fSVladimir Kondratiev 
6029cf10d62SVladimir Kondratiev 	wil_dbg_misc(wil, "%s()\n", __func__);
6039cf10d62SVladimir Kondratiev 
6048bf6adb9SVladimir Kondratiev 	if (vring->va) {
6058bf6adb9SVladimir Kondratiev 		wil_err(wil, "Rx ring already allocated\n");
6068bf6adb9SVladimir Kondratiev 		return -EINVAL;
6078bf6adb9SVladimir Kondratiev 	}
6088bf6adb9SVladimir Kondratiev 
609d3762b40SVladimir Kondratiev 	vring->size = size;
6102be7d22fSVladimir Kondratiev 	rc = wil_vring_alloc(wil, vring);
6112be7d22fSVladimir Kondratiev 	if (rc)
6122be7d22fSVladimir Kondratiev 		return rc;
6132be7d22fSVladimir Kondratiev 
61447e19af9SVladimir Kondratiev 	rc = wmi_rx_chain_add(wil, vring);
6152be7d22fSVladimir Kondratiev 	if (rc)
6162be7d22fSVladimir Kondratiev 		goto err_free;
6172be7d22fSVladimir Kondratiev 
6182be7d22fSVladimir Kondratiev 	rc = wil_rx_refill(wil, vring->size);
6192be7d22fSVladimir Kondratiev 	if (rc)
6202be7d22fSVladimir Kondratiev 		goto err_free;
6212be7d22fSVladimir Kondratiev 
6222be7d22fSVladimir Kondratiev 	return 0;
6232be7d22fSVladimir Kondratiev  err_free:
6242be7d22fSVladimir Kondratiev 	wil_vring_free(wil, vring, 0);
6252be7d22fSVladimir Kondratiev 
6262be7d22fSVladimir Kondratiev 	return rc;
6272be7d22fSVladimir Kondratiev }
6282be7d22fSVladimir Kondratiev 
6292be7d22fSVladimir Kondratiev void wil_rx_fini(struct wil6210_priv *wil)
6302be7d22fSVladimir Kondratiev {
6312be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_rx;
6322be7d22fSVladimir Kondratiev 
6339cf10d62SVladimir Kondratiev 	wil_dbg_misc(wil, "%s()\n", __func__);
6349cf10d62SVladimir Kondratiev 
6352acb4220SVladimir Kondratiev 	if (vring->va)
6362be7d22fSVladimir Kondratiev 		wil_vring_free(wil, vring, 0);
6372be7d22fSVladimir Kondratiev }
6382be7d22fSVladimir Kondratiev 
6392be7d22fSVladimir Kondratiev int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
6402be7d22fSVladimir Kondratiev 		      int cid, int tid)
6412be7d22fSVladimir Kondratiev {
6422be7d22fSVladimir Kondratiev 	int rc;
6432be7d22fSVladimir Kondratiev 	struct wmi_vring_cfg_cmd cmd = {
6442be7d22fSVladimir Kondratiev 		.action = cpu_to_le32(WMI_VRING_CMD_ADD),
6452be7d22fSVladimir Kondratiev 		.vring_cfg = {
6462be7d22fSVladimir Kondratiev 			.tx_sw_ring = {
6479a06bec9SVladimir Kondratiev 				.max_mpdu_size =
648c44690a1SVladimir Kondratiev 					cpu_to_le16(wil_mtu2macbuf(mtu_max)),
649b5d98e9dSVladimir Kondratiev 				.ring_size = cpu_to_le16(size),
6502be7d22fSVladimir Kondratiev 			},
6512be7d22fSVladimir Kondratiev 			.ringid = id,
652a70abea5SVladimir Kondratiev 			.cidxtid = mk_cidxtid(cid, tid),
6532be7d22fSVladimir Kondratiev 			.encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
6542be7d22fSVladimir Kondratiev 			.mac_ctrl = 0,
6552be7d22fSVladimir Kondratiev 			.to_resolution = 0,
6563277213fSVladimir Kondratiev 			.agg_max_wsize = 0,
6572be7d22fSVladimir Kondratiev 			.schd_params = {
6582be7d22fSVladimir Kondratiev 				.priority = cpu_to_le16(0),
6592be7d22fSVladimir Kondratiev 				.timeslot_us = cpu_to_le16(0xfff),
6602be7d22fSVladimir Kondratiev 			},
6612be7d22fSVladimir Kondratiev 		},
6622be7d22fSVladimir Kondratiev 	};
6632be7d22fSVladimir Kondratiev 	struct {
6642be7d22fSVladimir Kondratiev 		struct wil6210_mbox_hdr_wmi wmi;
6652be7d22fSVladimir Kondratiev 		struct wmi_vring_cfg_done_event cmd;
6662be7d22fSVladimir Kondratiev 	} __packed reply;
6672be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_tx[id];
668097638a0SVladimir Kondratiev 	struct vring_tx_data *txdata = &wil->vring_tx_data[id];
6692be7d22fSVladimir Kondratiev 
670e0106adaSVladimir Kondratiev 	wil_dbg_misc(wil, "%s() max_mpdu_size %d\n", __func__,
671e0106adaSVladimir Kondratiev 		     cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
6729cf10d62SVladimir Kondratiev 
6732be7d22fSVladimir Kondratiev 	if (vring->va) {
6742be7d22fSVladimir Kondratiev 		wil_err(wil, "Tx ring [%d] already allocated\n", id);
6752be7d22fSVladimir Kondratiev 		rc = -EINVAL;
6762be7d22fSVladimir Kondratiev 		goto out;
6772be7d22fSVladimir Kondratiev 	}
6782be7d22fSVladimir Kondratiev 
679097638a0SVladimir Kondratiev 	memset(txdata, 0, sizeof(*txdata));
6802be7d22fSVladimir Kondratiev 	vring->size = size;
6812be7d22fSVladimir Kondratiev 	rc = wil_vring_alloc(wil, vring);
6822be7d22fSVladimir Kondratiev 	if (rc)
6832be7d22fSVladimir Kondratiev 		goto out;
6842be7d22fSVladimir Kondratiev 
68593ae6d49SVladimir Kondratiev 	wil->vring2cid_tid[id][0] = cid;
68693ae6d49SVladimir Kondratiev 	wil->vring2cid_tid[id][1] = tid;
68793ae6d49SVladimir Kondratiev 
6882be7d22fSVladimir Kondratiev 	cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
6892be7d22fSVladimir Kondratiev 
6902be7d22fSVladimir Kondratiev 	rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
6912be7d22fSVladimir Kondratiev 		      WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
6922be7d22fSVladimir Kondratiev 	if (rc)
6932be7d22fSVladimir Kondratiev 		goto out_free;
6942be7d22fSVladimir Kondratiev 
695b8023177SVladimir Kondratiev 	if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
6962be7d22fSVladimir Kondratiev 		wil_err(wil, "Tx config failed, status 0x%02x\n",
6972be7d22fSVladimir Kondratiev 			reply.cmd.status);
698c331997bSVladimir Kondratiev 		rc = -EINVAL;
6992be7d22fSVladimir Kondratiev 		goto out_free;
7002be7d22fSVladimir Kondratiev 	}
7012be7d22fSVladimir Kondratiev 	vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
7022be7d22fSVladimir Kondratiev 
703097638a0SVladimir Kondratiev 	txdata->enabled = 1;
7043a3def8dSVladimir Kondratiev 	if (wil->sta[cid].data_port_open && (agg_wsize >= 0))
7053a3def8dSVladimir Kondratiev 		wil_addba_tx_request(wil, id, agg_wsize);
706097638a0SVladimir Kondratiev 
7072be7d22fSVladimir Kondratiev 	return 0;
7082be7d22fSVladimir Kondratiev  out_free:
7092be7d22fSVladimir Kondratiev 	wil_vring_free(wil, vring, 1);
7102be7d22fSVladimir Kondratiev  out:
7112be7d22fSVladimir Kondratiev 
7122be7d22fSVladimir Kondratiev 	return rc;
7132be7d22fSVladimir Kondratiev }
7142be7d22fSVladimir Kondratiev 
7152be7d22fSVladimir Kondratiev void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
7162be7d22fSVladimir Kondratiev {
7172be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_tx[id];
7183a124ed6SVladimir Kondratiev 	struct vring_tx_data *txdata = &wil->vring_tx_data[id];
7192be7d22fSVladimir Kondratiev 
720097638a0SVladimir Kondratiev 	WARN_ON(!mutex_is_locked(&wil->mutex));
721097638a0SVladimir Kondratiev 
7222be7d22fSVladimir Kondratiev 	if (!vring->va)
7232be7d22fSVladimir Kondratiev 		return;
7242be7d22fSVladimir Kondratiev 
7259cf10d62SVladimir Kondratiev 	wil_dbg_misc(wil, "%s() id=%d\n", __func__, id);
7269cf10d62SVladimir Kondratiev 
727097638a0SVladimir Kondratiev 	/* make sure NAPI won't touch this vring */
728097638a0SVladimir Kondratiev 	wil->vring_tx_data[id].enabled = 0;
7299419b6a2SVladimir Kondratiev 	if (test_bit(wil_status_napi_en, wil->status))
730097638a0SVladimir Kondratiev 		napi_synchronize(&wil->napi_tx);
731097638a0SVladimir Kondratiev 
7322be7d22fSVladimir Kondratiev 	wil_vring_free(wil, vring, 1);
7333a124ed6SVladimir Kondratiev 	memset(txdata, 0, sizeof(*txdata));
7342be7d22fSVladimir Kondratiev }
7352be7d22fSVladimir Kondratiev 
7362be7d22fSVladimir Kondratiev static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
7372be7d22fSVladimir Kondratiev 				       struct sk_buff *skb)
7382be7d22fSVladimir Kondratiev {
7393df2cd36SVladimir Kondratiev 	int i;
7403df2cd36SVladimir Kondratiev 	struct ethhdr *eth = (void *)skb->data;
7413df2cd36SVladimir Kondratiev 	int cid = wil_find_cid(wil, eth->h_dest);
7422be7d22fSVladimir Kondratiev 
7433df2cd36SVladimir Kondratiev 	if (cid < 0)
7443df2cd36SVladimir Kondratiev 		return NULL;
7453df2cd36SVladimir Kondratiev 
746e58c9f70SVladimir Kondratiev 	if (!wil->sta[cid].data_port_open &&
747e58c9f70SVladimir Kondratiev 	    (skb->protocol != cpu_to_be16(ETH_P_PAE)))
748e58c9f70SVladimir Kondratiev 		return NULL;
749e58c9f70SVladimir Kondratiev 
7503df2cd36SVladimir Kondratiev 	/* TODO: fix for multiple TID */
7513df2cd36SVladimir Kondratiev 	for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
7523df2cd36SVladimir Kondratiev 		if (wil->vring2cid_tid[i][0] == cid) {
7533df2cd36SVladimir Kondratiev 			struct vring *v = &wil->vring_tx[i];
7548fe59627SVladimir Kondratiev 
7553df2cd36SVladimir Kondratiev 			wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n",
7563df2cd36SVladimir Kondratiev 				     __func__, eth->h_dest, i);
7573df2cd36SVladimir Kondratiev 			if (v->va) {
7582be7d22fSVladimir Kondratiev 				return v;
7593df2cd36SVladimir Kondratiev 			} else {
7603df2cd36SVladimir Kondratiev 				wil_dbg_txrx(wil, "vring[%d] not valid\n", i);
7613df2cd36SVladimir Kondratiev 				return NULL;
7623df2cd36SVladimir Kondratiev 			}
7633df2cd36SVladimir Kondratiev 		}
7643df2cd36SVladimir Kondratiev 	}
7652be7d22fSVladimir Kondratiev 
7662be7d22fSVladimir Kondratiev 	return NULL;
7672be7d22fSVladimir Kondratiev }
7682be7d22fSVladimir Kondratiev 
769fb3cac57SVladimir Kondratiev static void wil_set_da_for_vring(struct wil6210_priv *wil,
770fb3cac57SVladimir Kondratiev 				 struct sk_buff *skb, int vring_index)
771fb3cac57SVladimir Kondratiev {
772fb3cac57SVladimir Kondratiev 	struct ethhdr *eth = (void *)skb->data;
773fb3cac57SVladimir Kondratiev 	int cid = wil->vring2cid_tid[vring_index][0];
7748fe59627SVladimir Kondratiev 
775fb3cac57SVladimir Kondratiev 	memcpy(eth->h_dest, wil->sta[cid].addr, ETH_ALEN);
776fb3cac57SVladimir Kondratiev }
777fb3cac57SVladimir Kondratiev 
778fb3cac57SVladimir Kondratiev static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
779fb3cac57SVladimir Kondratiev 			struct sk_buff *skb);
78054ed90a8SVladimir Kondratiev 
78154ed90a8SVladimir Kondratiev static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
78254ed90a8SVladimir Kondratiev 					   struct sk_buff *skb)
78354ed90a8SVladimir Kondratiev {
78454ed90a8SVladimir Kondratiev 	struct vring *v;
78554ed90a8SVladimir Kondratiev 	int i;
78654ed90a8SVladimir Kondratiev 	u8 cid;
78754ed90a8SVladimir Kondratiev 
78854ed90a8SVladimir Kondratiev 	/* In the STA mode, it is expected to have only 1 VRING
78954ed90a8SVladimir Kondratiev 	 * for the AP we connected to.
79054ed90a8SVladimir Kondratiev 	 * find 1-st vring and see whether it is eligible for data
79154ed90a8SVladimir Kondratiev 	 */
79254ed90a8SVladimir Kondratiev 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
79354ed90a8SVladimir Kondratiev 		v = &wil->vring_tx[i];
79454ed90a8SVladimir Kondratiev 		if (!v->va)
79554ed90a8SVladimir Kondratiev 			continue;
79654ed90a8SVladimir Kondratiev 
79754ed90a8SVladimir Kondratiev 		cid = wil->vring2cid_tid[i][0];
79854ed90a8SVladimir Kondratiev 		if (!wil->sta[cid].data_port_open &&
79954ed90a8SVladimir Kondratiev 		    (skb->protocol != cpu_to_be16(ETH_P_PAE)))
80054ed90a8SVladimir Kondratiev 			break;
80154ed90a8SVladimir Kondratiev 
80254ed90a8SVladimir Kondratiev 		wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
80354ed90a8SVladimir Kondratiev 
80454ed90a8SVladimir Kondratiev 		return v;
80554ed90a8SVladimir Kondratiev 	}
80654ed90a8SVladimir Kondratiev 
80754ed90a8SVladimir Kondratiev 	wil_dbg_txrx(wil, "Tx while no vrings active?\n");
80854ed90a8SVladimir Kondratiev 
80954ed90a8SVladimir Kondratiev 	return NULL;
81054ed90a8SVladimir Kondratiev }
81154ed90a8SVladimir Kondratiev 
812fb3cac57SVladimir Kondratiev /*
813fb3cac57SVladimir Kondratiev  * Find 1-st vring and return it; set dest address for this vring in skb
814fb3cac57SVladimir Kondratiev  * duplicate skb and send it to other active vrings
815fb3cac57SVladimir Kondratiev  */
816fb3cac57SVladimir Kondratiev static struct vring *wil_tx_bcast(struct wil6210_priv *wil,
817fb3cac57SVladimir Kondratiev 				  struct sk_buff *skb)
818fb3cac57SVladimir Kondratiev {
819fb3cac57SVladimir Kondratiev 	struct vring *v, *v2;
820fb3cac57SVladimir Kondratiev 	struct sk_buff *skb2;
821fb3cac57SVladimir Kondratiev 	int i;
822e58c9f70SVladimir Kondratiev 	u8 cid;
823fb3cac57SVladimir Kondratiev 
824e58c9f70SVladimir Kondratiev 	/* find 1-st vring eligible for data */
825fb3cac57SVladimir Kondratiev 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
826fb3cac57SVladimir Kondratiev 		v = &wil->vring_tx[i];
827e58c9f70SVladimir Kondratiev 		if (!v->va)
828e58c9f70SVladimir Kondratiev 			continue;
829e58c9f70SVladimir Kondratiev 
830e58c9f70SVladimir Kondratiev 		cid = wil->vring2cid_tid[i][0];
831e58c9f70SVladimir Kondratiev 		if (!wil->sta[cid].data_port_open)
832e58c9f70SVladimir Kondratiev 			continue;
833e58c9f70SVladimir Kondratiev 
834fb3cac57SVladimir Kondratiev 		goto found;
835fb3cac57SVladimir Kondratiev 	}
836fb3cac57SVladimir Kondratiev 
8375aed1393SVladimir Kondratiev 	wil_dbg_txrx(wil, "Tx while no vrings active?\n");
838fb3cac57SVladimir Kondratiev 
839fb3cac57SVladimir Kondratiev 	return NULL;
840fb3cac57SVladimir Kondratiev 
841fb3cac57SVladimir Kondratiev found:
842fb3cac57SVladimir Kondratiev 	wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
843fb3cac57SVladimir Kondratiev 	wil_set_da_for_vring(wil, skb, i);
844fb3cac57SVladimir Kondratiev 
845fb3cac57SVladimir Kondratiev 	/* find other active vrings and duplicate skb for each */
846fb3cac57SVladimir Kondratiev 	for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
847fb3cac57SVladimir Kondratiev 		v2 = &wil->vring_tx[i];
848fb3cac57SVladimir Kondratiev 		if (!v2->va)
849fb3cac57SVladimir Kondratiev 			continue;
850e58c9f70SVladimir Kondratiev 		cid = wil->vring2cid_tid[i][0];
851e58c9f70SVladimir Kondratiev 		if (!wil->sta[cid].data_port_open)
852e58c9f70SVladimir Kondratiev 			continue;
853e58c9f70SVladimir Kondratiev 
854fb3cac57SVladimir Kondratiev 		skb2 = skb_copy(skb, GFP_ATOMIC);
855fb3cac57SVladimir Kondratiev 		if (skb2) {
856fb3cac57SVladimir Kondratiev 			wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
857fb3cac57SVladimir Kondratiev 			wil_set_da_for_vring(wil, skb2, i);
858fb3cac57SVladimir Kondratiev 			wil_tx_vring(wil, v2, skb2);
859fb3cac57SVladimir Kondratiev 		} else {
860fb3cac57SVladimir Kondratiev 			wil_err(wil, "skb_copy failed\n");
861fb3cac57SVladimir Kondratiev 		}
862fb3cac57SVladimir Kondratiev 	}
863fb3cac57SVladimir Kondratiev 
864fb3cac57SVladimir Kondratiev 	return v;
865fb3cac57SVladimir Kondratiev }
866fb3cac57SVladimir Kondratiev 
86799b55bd2SKirshenbaum Erez static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
86899b55bd2SKirshenbaum Erez 			   int vring_index)
8692be7d22fSVladimir Kondratiev {
87068ada71eSVladimir Kondratiev 	wil_desc_addr_set(&d->dma.addr, pa);
8712be7d22fSVladimir Kondratiev 	d->dma.ip_length = 0;
8722be7d22fSVladimir Kondratiev 	/* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
8732be7d22fSVladimir Kondratiev 	d->dma.b11 = 0/*14 | BIT(7)*/;
8742be7d22fSVladimir Kondratiev 	d->dma.error = 0;
8752be7d22fSVladimir Kondratiev 	d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
8767e594444SVladimir Kondratiev 	d->dma.length = cpu_to_le16((u16)len);
87799b55bd2SKirshenbaum Erez 	d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
8782be7d22fSVladimir Kondratiev 	d->mac.d[0] = 0;
8792be7d22fSVladimir Kondratiev 	d->mac.d[1] = 0;
8802be7d22fSVladimir Kondratiev 	d->mac.d[2] = 0;
8812be7d22fSVladimir Kondratiev 	d->mac.ucode_cmd = 0;
8822be7d22fSVladimir Kondratiev 	/* use dst index 0 */
8832be7d22fSVladimir Kondratiev 	d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
8842be7d22fSVladimir Kondratiev 		       (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
8852be7d22fSVladimir Kondratiev 	/* translation type:  0 - bypass; 1 - 802.3; 2 - native wifi */
8862be7d22fSVladimir Kondratiev 	d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
8872be7d22fSVladimir Kondratiev 		      (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
8882be7d22fSVladimir Kondratiev 
8892be7d22fSVladimir Kondratiev 	return 0;
8902be7d22fSVladimir Kondratiev }
8912be7d22fSVladimir Kondratiev 
892c236658fSVladimir Kondratiev static inline
893c236658fSVladimir Kondratiev void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
894c236658fSVladimir Kondratiev {
895c236658fSVladimir Kondratiev 	d->mac.d[2] |= ((nr_frags + 1) <<
896c236658fSVladimir Kondratiev 		       MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
897c236658fSVladimir Kondratiev }
898c236658fSVladimir Kondratiev 
899504937d4SKirshenbaum Erez static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil,
900504937d4SKirshenbaum Erez 					 struct vring_tx_desc *d,
901504937d4SKirshenbaum Erez 					 struct sk_buff *skb)
902504937d4SKirshenbaum Erez {
903504937d4SKirshenbaum Erez 	int protocol;
904504937d4SKirshenbaum Erez 
905504937d4SKirshenbaum Erez 	if (skb->ip_summed != CHECKSUM_PARTIAL)
906504937d4SKirshenbaum Erez 		return 0;
907504937d4SKirshenbaum Erez 
908df2d08eeSVladimir Kondratiev 	d->dma.b11 = ETH_HLEN; /* MAC header length */
909df2d08eeSVladimir Kondratiev 
910504937d4SKirshenbaum Erez 	switch (skb->protocol) {
911504937d4SKirshenbaum Erez 	case cpu_to_be16(ETH_P_IP):
912504937d4SKirshenbaum Erez 		protocol = ip_hdr(skb)->protocol;
913df2d08eeSVladimir Kondratiev 		d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
914504937d4SKirshenbaum Erez 		break;
915504937d4SKirshenbaum Erez 	case cpu_to_be16(ETH_P_IPV6):
916504937d4SKirshenbaum Erez 		protocol = ipv6_hdr(skb)->nexthdr;
917504937d4SKirshenbaum Erez 		break;
918504937d4SKirshenbaum Erez 	default:
919504937d4SKirshenbaum Erez 		return -EINVAL;
920504937d4SKirshenbaum Erez 	}
921504937d4SKirshenbaum Erez 
922504937d4SKirshenbaum Erez 	switch (protocol) {
923504937d4SKirshenbaum Erez 	case IPPROTO_TCP:
924504937d4SKirshenbaum Erez 		d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
925504937d4SKirshenbaum Erez 		/* L4 header len: TCP header length */
926504937d4SKirshenbaum Erez 		d->dma.d0 |=
927504937d4SKirshenbaum Erez 		(tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
928504937d4SKirshenbaum Erez 		break;
929504937d4SKirshenbaum Erez 	case IPPROTO_UDP:
930504937d4SKirshenbaum Erez 		/* L4 header len: UDP header length */
931504937d4SKirshenbaum Erez 		d->dma.d0 |=
932504937d4SKirshenbaum Erez 		(sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
933504937d4SKirshenbaum Erez 		break;
934504937d4SKirshenbaum Erez 	default:
935504937d4SKirshenbaum Erez 		return -EINVAL;
936504937d4SKirshenbaum Erez 	}
937504937d4SKirshenbaum Erez 
938504937d4SKirshenbaum Erez 	d->dma.ip_length = skb_network_header_len(skb);
939504937d4SKirshenbaum Erez 	/* Enable TCP/UDP checksum */
940504937d4SKirshenbaum Erez 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
941504937d4SKirshenbaum Erez 	/* Calculate pseudo-header */
942504937d4SKirshenbaum Erez 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
943504937d4SKirshenbaum Erez 
944504937d4SKirshenbaum Erez 	return 0;
945504937d4SKirshenbaum Erez }
946504937d4SKirshenbaum Erez 
9472be7d22fSVladimir Kondratiev static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
9482be7d22fSVladimir Kondratiev 			struct sk_buff *skb)
9492be7d22fSVladimir Kondratiev {
9502be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
95168ada71eSVladimir Kondratiev 	struct vring_tx_desc dd, *d = &dd;
95268ada71eSVladimir Kondratiev 	volatile struct vring_tx_desc *_d;
9532be7d22fSVladimir Kondratiev 	u32 swhead = vring->swhead;
9542be7d22fSVladimir Kondratiev 	int avail = wil_vring_avail_tx(vring);
9552be7d22fSVladimir Kondratiev 	int nr_frags = skb_shinfo(skb)->nr_frags;
956504937d4SKirshenbaum Erez 	uint f = 0;
9572be7d22fSVladimir Kondratiev 	int vring_index = vring - wil->vring_tx;
9587c0acf86SVladimir Kondratiev 	struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
9592be7d22fSVladimir Kondratiev 	uint i = swhead;
9602be7d22fSVladimir Kondratiev 	dma_addr_t pa;
9612be7d22fSVladimir Kondratiev 
9627743882dSVladimir Kondratiev 	wil_dbg_txrx(wil, "%s()\n", __func__);
9632be7d22fSVladimir Kondratiev 
9642be7d22fSVladimir Kondratiev 	if (avail < 1 + nr_frags) {
96570801e1bSVladimir Kondratiev 		wil_err_ratelimited(wil,
96670801e1bSVladimir Kondratiev 				    "Tx ring full. No space for %d fragments\n",
9672be7d22fSVladimir Kondratiev 				    1 + nr_frags);
9682be7d22fSVladimir Kondratiev 		return -ENOMEM;
9692be7d22fSVladimir Kondratiev 	}
9708fe59627SVladimir Kondratiev 	_d = &vring->va[i].tx;
9712be7d22fSVladimir Kondratiev 
9728fe59627SVladimir Kondratiev 	pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
9732be7d22fSVladimir Kondratiev 
97439c52ee8SVladimir Kondratiev 	wil_dbg_txrx(wil, "Tx skb %d bytes 0x%p -> %pad\n", skb_headlen(skb),
97539c52ee8SVladimir Kondratiev 		     skb->data, &pa);
9767743882dSVladimir Kondratiev 	wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
9772be7d22fSVladimir Kondratiev 			  skb->data, skb_headlen(skb), false);
9782be7d22fSVladimir Kondratiev 
9792be7d22fSVladimir Kondratiev 	if (unlikely(dma_mapping_error(dev, pa)))
9802be7d22fSVladimir Kondratiev 		return -EINVAL;
9812232abd5SVladimir Kondratiev 	vring->ctx[i].mapped_as = wil_mapped_as_single;
9822be7d22fSVladimir Kondratiev 	/* 1-st segment */
98399b55bd2SKirshenbaum Erez 	wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index);
984504937d4SKirshenbaum Erez 	/* Process TCP/UDP checksum offloading */
985504937d4SKirshenbaum Erez 	if (wil_tx_desc_offload_cksum_set(wil, d, skb)) {
986504937d4SKirshenbaum Erez 		wil_err(wil, "VRING #%d Failed to set cksum, drop packet\n",
987504937d4SKirshenbaum Erez 			vring_index);
988504937d4SKirshenbaum Erez 		goto dma_error;
989504937d4SKirshenbaum Erez 	}
990504937d4SKirshenbaum Erez 
991c236658fSVladimir Kondratiev 	vring->ctx[i].nr_frags = nr_frags;
992c236658fSVladimir Kondratiev 	wil_tx_desc_set_nr_frags(d, nr_frags);
99368ada71eSVladimir Kondratiev 	if (nr_frags)
99468ada71eSVladimir Kondratiev 		*_d = *d;
99568ada71eSVladimir Kondratiev 
9962be7d22fSVladimir Kondratiev 	/* middle segments */
997504937d4SKirshenbaum Erez 	for (; f < nr_frags; f++) {
9982be7d22fSVladimir Kondratiev 		const struct skb_frag_struct *frag =
9992be7d22fSVladimir Kondratiev 				&skb_shinfo(skb)->frags[f];
10002be7d22fSVladimir Kondratiev 		int len = skb_frag_size(frag);
10018fe59627SVladimir Kondratiev 
10022be7d22fSVladimir Kondratiev 		i = (swhead + f + 1) % vring->size;
10038fe59627SVladimir Kondratiev 		_d = &vring->va[i].tx;
10042be7d22fSVladimir Kondratiev 		pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
10052be7d22fSVladimir Kondratiev 				      DMA_TO_DEVICE);
10062be7d22fSVladimir Kondratiev 		if (unlikely(dma_mapping_error(dev, pa)))
10072be7d22fSVladimir Kondratiev 			goto dma_error;
10082232abd5SVladimir Kondratiev 		vring->ctx[i].mapped_as = wil_mapped_as_page;
100999b55bd2SKirshenbaum Erez 		wil_tx_desc_map(d, pa, len, vring_index);
1010c236658fSVladimir Kondratiev 		/* no need to check return code -
1011c236658fSVladimir Kondratiev 		 * if it succeeded for 1-st descriptor,
1012c236658fSVladimir Kondratiev 		 * it will succeed here too
1013c236658fSVladimir Kondratiev 		 */
1014c236658fSVladimir Kondratiev 		wil_tx_desc_offload_cksum_set(wil, d, skb);
101568ada71eSVladimir Kondratiev 		*_d = *d;
10162be7d22fSVladimir Kondratiev 	}
10172be7d22fSVladimir Kondratiev 	/* for the last seg only */
10182be7d22fSVladimir Kondratiev 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
1019668b2bbdSKirshenbaum Erez 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
10202be7d22fSVladimir Kondratiev 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
102168ada71eSVladimir Kondratiev 	*_d = *d;
10222be7d22fSVladimir Kondratiev 
10236cdadd4dSVladimir Kondratiev 	/* hold reference to skb
10246cdadd4dSVladimir Kondratiev 	 * to prevent skb release before accounting
10256cdadd4dSVladimir Kondratiev 	 * in case of immediate "tx done"
10266cdadd4dSVladimir Kondratiev 	 */
10276cdadd4dSVladimir Kondratiev 	vring->ctx[i].skb = skb_get(skb);
10286cdadd4dSVladimir Kondratiev 
10297743882dSVladimir Kondratiev 	wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4,
10302be7d22fSVladimir Kondratiev 			  (const void *)d, sizeof(*d), false);
10312be7d22fSVladimir Kondratiev 
10327c0acf86SVladimir Kondratiev 	if (wil_vring_is_empty(vring)) /* performance monitoring */
10337c0acf86SVladimir Kondratiev 		txdata->idle += get_cycles() - txdata->last_idle;
10347c0acf86SVladimir Kondratiev 
10352be7d22fSVladimir Kondratiev 	/* advance swhead */
10362be7d22fSVladimir Kondratiev 	wil_vring_advance_head(vring, nr_frags + 1);
10377743882dSVladimir Kondratiev 	wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
103898658095SVladimir Kondratiev 	trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
10392be7d22fSVladimir Kondratiev 	iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
10402be7d22fSVladimir Kondratiev 
10412be7d22fSVladimir Kondratiev 	return 0;
10422be7d22fSVladimir Kondratiev  dma_error:
10432be7d22fSVladimir Kondratiev 	/* unmap what we have mapped */
1044c2a146f6SVladimir Kondratiev 	nr_frags = f + 1; /* frags mapped + one for skb head */
1045c2a146f6SVladimir Kondratiev 	for (f = 0; f < nr_frags; f++) {
1046c2a146f6SVladimir Kondratiev 		struct wil_ctx *ctx;
10477e594444SVladimir Kondratiev 
10482be7d22fSVladimir Kondratiev 		i = (swhead + f) % vring->size;
1049c2a146f6SVladimir Kondratiev 		ctx = &vring->ctx[i];
10508fe59627SVladimir Kondratiev 		_d = &vring->va[i].tx;
105168ada71eSVladimir Kondratiev 		*d = *_d;
105268ada71eSVladimir Kondratiev 		_d->dma.status = TX_DMA_STATUS_DU;
10532232abd5SVladimir Kondratiev 		wil_txdesc_unmap(dev, d, ctx);
1054f88f113aSVladimir Kondratiev 
1055f88f113aSVladimir Kondratiev 		if (ctx->skb)
1056f88f113aSVladimir Kondratiev 			dev_kfree_skb_any(ctx->skb);
1057f88f113aSVladimir Kondratiev 
1058f88f113aSVladimir Kondratiev 		memset(ctx, 0, sizeof(*ctx));
10592be7d22fSVladimir Kondratiev 	}
10602be7d22fSVladimir Kondratiev 
10612be7d22fSVladimir Kondratiev 	return -EINVAL;
10622be7d22fSVladimir Kondratiev }
10632be7d22fSVladimir Kondratiev 
10642be7d22fSVladimir Kondratiev netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
10652be7d22fSVladimir Kondratiev {
10662be7d22fSVladimir Kondratiev 	struct wil6210_priv *wil = ndev_to_wil(ndev);
10673df2cd36SVladimir Kondratiev 	struct ethhdr *eth = (void *)skb->data;
10682be7d22fSVladimir Kondratiev 	struct vring *vring;
1069aa27deaaSVladimir Kondratiev 	static bool pr_once_fw;
10702be7d22fSVladimir Kondratiev 	int rc;
10712be7d22fSVladimir Kondratiev 
10727743882dSVladimir Kondratiev 	wil_dbg_txrx(wil, "%s()\n", __func__);
10739419b6a2SVladimir Kondratiev 	if (!test_bit(wil_status_fwready, wil->status)) {
1074aa27deaaSVladimir Kondratiev 		if (!pr_once_fw) {
10752be7d22fSVladimir Kondratiev 			wil_err(wil, "FW not ready\n");
1076aa27deaaSVladimir Kondratiev 			pr_once_fw = true;
1077aa27deaaSVladimir Kondratiev 		}
10782be7d22fSVladimir Kondratiev 		goto drop;
10792be7d22fSVladimir Kondratiev 	}
10809419b6a2SVladimir Kondratiev 	if (!test_bit(wil_status_fwconnected, wil->status)) {
10812be7d22fSVladimir Kondratiev 		wil_err(wil, "FW not connected\n");
10822be7d22fSVladimir Kondratiev 		goto drop;
10832be7d22fSVladimir Kondratiev 	}
10842be7d22fSVladimir Kondratiev 	if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
10852be7d22fSVladimir Kondratiev 		wil_err(wil, "Xmit in monitor mode not supported\n");
10862be7d22fSVladimir Kondratiev 		goto drop;
10872be7d22fSVladimir Kondratiev 	}
1088aa27deaaSVladimir Kondratiev 	pr_once_fw = false;
1089d58db4e4SVladimir Kondratiev 
10902be7d22fSVladimir Kondratiev 	/* find vring */
109154ed90a8SVladimir Kondratiev 	if (wil->wdev->iftype == NL80211_IFTYPE_STATION) {
109254ed90a8SVladimir Kondratiev 		/* in STA mode (ESS), all to same VRING */
109354ed90a8SVladimir Kondratiev 		vring = wil_find_tx_vring_sta(wil, skb);
109454ed90a8SVladimir Kondratiev 	} else { /* direct communication, find matching VRING */
10958fe59627SVladimir Kondratiev 		if (is_unicast_ether_addr(eth->h_dest))
10962be7d22fSVladimir Kondratiev 			vring = wil_find_tx_vring(wil, skb);
10978fe59627SVladimir Kondratiev 		else
1098fb3cac57SVladimir Kondratiev 			vring = wil_tx_bcast(wil, skb);
109954ed90a8SVladimir Kondratiev 	}
11002be7d22fSVladimir Kondratiev 	if (!vring) {
11015aed1393SVladimir Kondratiev 		wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
11022be7d22fSVladimir Kondratiev 		goto drop;
11032be7d22fSVladimir Kondratiev 	}
11042be7d22fSVladimir Kondratiev 	/* set up vring entry */
11052be7d22fSVladimir Kondratiev 	rc = wil_tx_vring(wil, vring, skb);
1106d58db4e4SVladimir Kondratiev 
11072232abd5SVladimir Kondratiev 	/* do we still have enough room in the vring? */
110855f8f680SVladimir Kondratiev 	if (wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring)) {
11092232abd5SVladimir Kondratiev 		netif_tx_stop_all_queues(wil_to_ndev(wil));
111055f8f680SVladimir Kondratiev 		wil_dbg_txrx(wil, "netif_tx_stop : ring full\n");
111155f8f680SVladimir Kondratiev 	}
11122232abd5SVladimir Kondratiev 
11132be7d22fSVladimir Kondratiev 	switch (rc) {
11142be7d22fSVladimir Kondratiev 	case 0:
1115795ce734SVladimir Kondratiev 		/* statistics will be updated on the tx_complete */
11162be7d22fSVladimir Kondratiev 		dev_kfree_skb_any(skb);
11172be7d22fSVladimir Kondratiev 		return NETDEV_TX_OK;
11182be7d22fSVladimir Kondratiev 	case -ENOMEM:
11192be7d22fSVladimir Kondratiev 		return NETDEV_TX_BUSY;
11202be7d22fSVladimir Kondratiev 	default:
1121afda8bb5SVladimir Kondratiev 		break; /* goto drop; */
11222be7d22fSVladimir Kondratiev 	}
11232be7d22fSVladimir Kondratiev  drop:
11242be7d22fSVladimir Kondratiev 	ndev->stats.tx_dropped++;
11252be7d22fSVladimir Kondratiev 	dev_kfree_skb_any(skb);
11262be7d22fSVladimir Kondratiev 
11272be7d22fSVladimir Kondratiev 	return NET_XMIT_DROP;
11282be7d22fSVladimir Kondratiev }
11292be7d22fSVladimir Kondratiev 
11302be7d22fSVladimir Kondratiev /**
11312be7d22fSVladimir Kondratiev  * Clean up transmitted skb's from the Tx VRING
11322be7d22fSVladimir Kondratiev  *
1133e0287c4aSVladimir Kondratiev  * Return number of descriptors cleared
1134e0287c4aSVladimir Kondratiev  *
11352be7d22fSVladimir Kondratiev  * Safe to call from IRQ
11362be7d22fSVladimir Kondratiev  */
1137e0287c4aSVladimir Kondratiev int wil_tx_complete(struct wil6210_priv *wil, int ringid)
11382be7d22fSVladimir Kondratiev {
1139795ce734SVladimir Kondratiev 	struct net_device *ndev = wil_to_ndev(wil);
11402be7d22fSVladimir Kondratiev 	struct device *dev = wil_to_dev(wil);
11412be7d22fSVladimir Kondratiev 	struct vring *vring = &wil->vring_tx[ringid];
1142097638a0SVladimir Kondratiev 	struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
1143e0287c4aSVladimir Kondratiev 	int done = 0;
1144c8b78b5fSVladimir Kondratiev 	int cid = wil->vring2cid_tid[ringid][0];
1145c8b78b5fSVladimir Kondratiev 	struct wil_net_stats *stats = &wil->sta[cid].stats;
1146c236658fSVladimir Kondratiev 	volatile struct vring_tx_desc *_d;
11472be7d22fSVladimir Kondratiev 
11482be7d22fSVladimir Kondratiev 	if (!vring->va) {
11492be7d22fSVladimir Kondratiev 		wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
1150e0287c4aSVladimir Kondratiev 		return 0;
11512be7d22fSVladimir Kondratiev 	}
11522be7d22fSVladimir Kondratiev 
1153097638a0SVladimir Kondratiev 	if (!txdata->enabled) {
1154097638a0SVladimir Kondratiev 		wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
1155097638a0SVladimir Kondratiev 		return 0;
1156097638a0SVladimir Kondratiev 	}
1157097638a0SVladimir Kondratiev 
11587743882dSVladimir Kondratiev 	wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
11592be7d22fSVladimir Kondratiev 
11602be7d22fSVladimir Kondratiev 	while (!wil_vring_is_empty(vring)) {
1161c236658fSVladimir Kondratiev 		int new_swtail;
1162c236658fSVladimir Kondratiev 		struct wil_ctx *ctx = &vring->ctx[vring->swtail];
1163c236658fSVladimir Kondratiev 		/**
1164c236658fSVladimir Kondratiev 		 * For the fragmented skb, HW will set DU bit only for the
1165c236658fSVladimir Kondratiev 		 * last fragment. look for it
1166c236658fSVladimir Kondratiev 		 */
1167c236658fSVladimir Kondratiev 		int lf = (vring->swtail + ctx->nr_frags) % vring->size;
1168c236658fSVladimir Kondratiev 		/* TODO: check we are not past head */
1169c236658fSVladimir Kondratiev 
1170c236658fSVladimir Kondratiev 		_d = &vring->va[lf].tx;
1171c236658fSVladimir Kondratiev 		if (!(_d->dma.status & TX_DMA_STATUS_DU))
1172c236658fSVladimir Kondratiev 			break;
1173c236658fSVladimir Kondratiev 
1174c236658fSVladimir Kondratiev 		new_swtail = (lf + 1) % vring->size;
1175c236658fSVladimir Kondratiev 		while (vring->swtail != new_swtail) {
11764de41befSVladimir Kondratiev 			struct vring_tx_desc dd, *d = &dd;
11777e594444SVladimir Kondratiev 			u16 dmalen;
117876dfa4b7SVladimir Kondratiev 			struct sk_buff *skb;
117976dfa4b7SVladimir Kondratiev 
118076dfa4b7SVladimir Kondratiev 			ctx = &vring->ctx[vring->swtail];
118176dfa4b7SVladimir Kondratiev 			skb = ctx->skb;
1182c236658fSVladimir Kondratiev 			_d = &vring->va[vring->swtail].tx;
11834de41befSVladimir Kondratiev 
118468ada71eSVladimir Kondratiev 			*d = *_d;
11854de41befSVladimir Kondratiev 
11867e594444SVladimir Kondratiev 			dmalen = le16_to_cpu(d->dma.length);
118798658095SVladimir Kondratiev 			trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
118898658095SVladimir Kondratiev 					      d->dma.error);
11897743882dSVladimir Kondratiev 			wil_dbg_txrx(wil,
11902be7d22fSVladimir Kondratiev 				     "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
11917e594444SVladimir Kondratiev 				     vring->swtail, dmalen, d->dma.status,
11922be7d22fSVladimir Kondratiev 				     d->dma.error);
11937743882dSVladimir Kondratiev 			wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4,
11942be7d22fSVladimir Kondratiev 					  (const void *)d, sizeof(*d), false);
11952be7d22fSVladimir Kondratiev 
11962232abd5SVladimir Kondratiev 			wil_txdesc_unmap(dev, d, ctx);
1197f88f113aSVladimir Kondratiev 
11982be7d22fSVladimir Kondratiev 			if (skb) {
1199795ce734SVladimir Kondratiev 				if (d->dma.error == 0) {
1200795ce734SVladimir Kondratiev 					ndev->stats.tx_packets++;
1201c8b78b5fSVladimir Kondratiev 					stats->tx_packets++;
1202795ce734SVladimir Kondratiev 					ndev->stats.tx_bytes += skb->len;
1203c8b78b5fSVladimir Kondratiev 					stats->tx_bytes += skb->len;
1204795ce734SVladimir Kondratiev 				} else {
1205795ce734SVladimir Kondratiev 					ndev->stats.tx_errors++;
1206c8b78b5fSVladimir Kondratiev 					stats->tx_errors++;
1207795ce734SVladimir Kondratiev 				}
1208795ce734SVladimir Kondratiev 
12092be7d22fSVladimir Kondratiev 				dev_kfree_skb_any(skb);
12102be7d22fSVladimir Kondratiev 			}
1211f88f113aSVladimir Kondratiev 			memset(ctx, 0, sizeof(*ctx));
1212c236658fSVladimir Kondratiev 			/* There is no need to touch HW descriptor:
121303269c65SVladimir Kondratiev 			 * - ststus bit TX_DMA_STATUS_DU is set by design,
121403269c65SVladimir Kondratiev 			 *   so hardware will not try to process this desc.,
121503269c65SVladimir Kondratiev 			 * - rest of descriptor will be initialized on Tx.
121603269c65SVladimir Kondratiev 			 */
12172be7d22fSVladimir Kondratiev 			vring->swtail = wil_vring_next_tail(vring);
1218e0287c4aSVladimir Kondratiev 			done++;
12192be7d22fSVladimir Kondratiev 		}
1220c236658fSVladimir Kondratiev 	}
122167c3e1b4SVladimir Kondratiev 
12227c0acf86SVladimir Kondratiev 	if (wil_vring_is_empty(vring)) { /* performance monitoring */
122367c3e1b4SVladimir Kondratiev 		wil_dbg_txrx(wil, "Ring[%2d] empty\n", ringid);
12247c0acf86SVladimir Kondratiev 		txdata->last_idle = get_cycles();
12257c0acf86SVladimir Kondratiev 	}
122667c3e1b4SVladimir Kondratiev 
122755f8f680SVladimir Kondratiev 	if (wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring)) {
122855f8f680SVladimir Kondratiev 		wil_dbg_txrx(wil, "netif_tx_wake : ring not full\n");
12292be7d22fSVladimir Kondratiev 		netif_tx_wake_all_queues(wil_to_ndev(wil));
123055f8f680SVladimir Kondratiev 	}
1231e0287c4aSVladimir Kondratiev 
1232e0287c4aSVladimir Kondratiev 	return done;
12332be7d22fSVladimir Kondratiev }
1234