blob: 689081c2c37b22592bcb7b2a3a6d0d5a321bd844 [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
Vladimir Kondratiev02525a72014-08-06 10:31:51 +03002 * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08003 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080017#include <linux/etherdevice.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080018#include <net/ieee80211_radiotap.h>
19#include <linux/if_arp.h>
20#include <linux/moduleparam.h>
Kirshenbaum Erez504937d2013-07-21 11:34:37 +030021#include <linux/ip.h>
22#include <linux/ipv6.h>
23#include <net/ipv6.h>
Vladimir Kondratiev0786dc42014-01-14 20:31:26 +020024#include <linux/prefetch.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080025
26#include "wil6210.h"
27#include "wmi.h"
28#include "txrx.h"
Vladimir Kondratiev98658092013-05-12 14:43:35 +030029#include "trace.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080030
31static bool rtap_include_phy_info;
32module_param(rtap_include_phy_info, bool, S_IRUGO);
33MODULE_PARM_DESC(rtap_include_phy_info,
34 " Include PHY info in the radiotap header, default - no");
35
36static inline int wil_vring_is_empty(struct vring *vring)
37{
38 return vring->swhead == vring->swtail;
39}
40
41static inline u32 wil_vring_next_tail(struct vring *vring)
42{
43 return (vring->swtail + 1) % vring->size;
44}
45
46static inline void wil_vring_advance_head(struct vring *vring, int n)
47{
48 vring->swhead = (vring->swhead + n) % vring->size;
49}
50
51static inline int wil_vring_is_full(struct vring *vring)
52{
53 return wil_vring_next_tail(vring) == vring->swhead;
54}
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +030055
Vladimir Shulman0436fd92015-02-15 14:02:34 +020056/* Used space in Tx Vring */
57static inline int wil_vring_used_tx(struct vring *vring)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080058{
59 u32 swhead = vring->swhead;
60 u32 swtail = vring->swtail;
Vladimir Shulman0436fd92015-02-15 14:02:34 +020061 return (vring->size + swhead - swtail) % vring->size;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080062}
63
Vladimir Shulman0436fd92015-02-15 14:02:34 +020064/* Available space in Tx Vring */
65static inline int wil_vring_avail_tx(struct vring *vring)
66{
67 return vring->size - wil_vring_used_tx(vring) - 1;
68}
69
70/* wil_vring_wmark_low - low watermark for available descriptor space */
Vladimir Kondratiev5bb64232014-05-27 14:45:46 +030071static inline int wil_vring_wmark_low(struct vring *vring)
72{
73 return vring->size/8;
74}
75
Vladimir Shulman0436fd92015-02-15 14:02:34 +020076/* wil_vring_wmark_high - high watermark for available descriptor space */
Vladimir Kondratiev5bb64232014-05-27 14:45:46 +030077static inline int wil_vring_wmark_high(struct vring *vring)
78{
79 return vring->size/4;
80}
81
Vladimir Shulman0436fd92015-02-15 14:02:34 +020082/* wil_val_in_range - check if value in [min,max) */
83static inline bool wil_val_in_range(int val, int min, int max)
84{
85 return val >= min && val < max;
86}
87
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080088static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
89{
90 struct device *dev = wil_to_dev(wil);
91 size_t sz = vring->size * sizeof(vring->va[0]);
92 uint i;
93
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +030094 wil_dbg_misc(wil, "%s()\n", __func__);
95
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080096 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
97
98 vring->swhead = 0;
99 vring->swtail = 0;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300100 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800101 if (!vring->ctx) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800102 vring->va = NULL;
103 return -ENOMEM;
104 }
Vladimir Shulman0436fd92015-02-15 14:02:34 +0200105 /* vring->va should be aligned on its size rounded up to power of 2
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800106 * This is granted by the dma_alloc_coherent
107 */
108 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
109 if (!vring->va) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800110 kfree(vring->ctx);
111 vring->ctx = NULL;
112 return -ENOMEM;
113 }
114 /* initially, all descriptors are SW owned
115 * For Tx and Rx, ownership bit is at the same location, thus
116 * we can use any
117 */
118 for (i = 0; i < vring->size; i++) {
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300119 volatile struct vring_tx_desc *_d = &vring->va[i].tx;
120
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300121 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800122 }
123
Vladimir Kondratiev39c52ee2014-05-27 14:45:49 +0300124 wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
125 vring->va, &vring->pa, vring->ctx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800126
127 return 0;
128}
129
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200130static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d,
131 struct wil_ctx *ctx)
132{
133 dma_addr_t pa = wil_desc_addr(&d->dma.addr);
134 u16 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300135
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200136 switch (ctx->mapped_as) {
137 case wil_mapped_as_single:
138 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
139 break;
140 case wil_mapped_as_page:
141 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
142 break;
143 default:
144 break;
145 }
146}
147
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800148static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
149 int tx)
150{
151 struct device *dev = wil_to_dev(wil);
152 size_t sz = vring->size * sizeof(vring->va[0]);
153
Vladimir Kondratievef772852014-09-10 16:34:31 +0300154 if (tx) {
155 int vring_index = vring - wil->vring_tx;
156
157 wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
158 vring_index, vring->size, vring->va,
159 &vring->pa, vring->ctx);
160 } else {
161 wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
162 vring->size, vring->va,
163 &vring->pa, vring->ctx);
164 }
165
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800166 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300167 dma_addr_t pa;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300168 u16 dmalen;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300169 struct wil_ctx *ctx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300170
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800171 if (tx) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300172 struct vring_tx_desc dd, *d = &dd;
173 volatile struct vring_tx_desc *_d =
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800174 &vring->va[vring->swtail].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300175
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300176 ctx = &vring->ctx[vring->swtail];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300177 *d = *_d;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200178 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300179 if (ctx->skb)
180 dev_kfree_skb_any(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800181 vring->swtail = wil_vring_next_tail(vring);
182 } else { /* rx */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300183 struct vring_rx_desc dd, *d = &dd;
184 volatile struct vring_rx_desc *_d =
Vladimir Kondratiev4d1ac072013-07-11 18:03:38 +0300185 &vring->va[vring->swhead].rx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300186
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300187 ctx = &vring->ctx[vring->swhead];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300188 *d = *_d;
189 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300190 dmalen = le16_to_cpu(d->dma.length);
191 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300192 kfree_skb(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800193 wil_vring_advance_head(vring, 1);
194 }
195 }
196 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
197 kfree(vring->ctx);
198 vring->pa = 0;
199 vring->va = NULL;
200 vring->ctx = NULL;
201}
202
203/**
204 * Allocate one skb for Rx VRING
205 *
206 * Safe to call from IRQ
207 */
208static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
209 u32 i, int headroom)
210{
211 struct device *dev = wil_to_dev(wil);
Vladimir Kondratiev9a06bec2014-10-28 16:51:27 +0200212 unsigned int sz = mtu_max + ETH_HLEN;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300213 struct vring_rx_desc dd, *d = &dd;
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300214 volatile struct vring_rx_desc *_d = &vring->va[i].rx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800215 dma_addr_t pa;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800216 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300217
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800218 if (unlikely(!skb))
219 return -ENOMEM;
220
221 skb_reserve(skb, headroom);
222 skb_put(skb, sz);
223
224 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
225 if (unlikely(dma_mapping_error(dev, pa))) {
226 kfree_skb(skb);
227 return -ENOMEM;
228 }
229
230 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300231 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800232 /* ip_length don't care */
233 /* b11 don't care */
234 /* error don't care */
235 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300236 d->dma.length = cpu_to_le16(sz);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300237 *_d = *d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300238 vring->ctx[i].skb = skb;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800239
240 return 0;
241}
242
243/**
244 * Adds radiotap header
245 *
246 * Any error indicated as "Bad FCS"
247 *
248 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
249 * - Rx descriptor: 32 bytes
250 * - Phy info
251 */
252static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300253 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800254{
255 struct wireless_dev *wdev = wil->wdev;
256 struct wil6210_rtap {
257 struct ieee80211_radiotap_header rthdr;
258 /* fields should be in the order of bits in rthdr.it_present */
259 /* flags */
260 u8 flags;
261 /* channel */
262 __le16 chnl_freq __aligned(2);
263 __le16 chnl_flags;
264 /* MCS */
265 u8 mcs_present;
266 u8 mcs_flags;
267 u8 mcs_index;
268 } __packed;
269 struct wil6210_rtap_vendor {
270 struct wil6210_rtap rtap;
271 /* vendor */
272 u8 vendor_oui[3] __aligned(2);
273 u8 vendor_ns;
274 __le16 vendor_skip;
275 u8 vendor_data[0];
276 } __packed;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300277 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800278 struct wil6210_rtap_vendor *rtap_vendor;
279 int rtap_len = sizeof(struct wil6210_rtap);
280 int phy_length = 0; /* phy info header size, bytes */
281 static char phy_data[128];
282 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
283
284 if (rtap_include_phy_info) {
285 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
286 /* calculate additional length */
287 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
288 /**
289 * PHY info starts from 8-byte boundary
290 * there are 8-byte lines, last line may be partially
291 * written (HW bug), thus FW configures for last line
292 * to be excessive. Driver skips this last line.
293 */
294 int len = min_t(int, 8 + sizeof(phy_data),
295 wil_rxdesc_phy_length(d));
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300296
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800297 if (len > 8) {
298 void *p = skb_tail_pointer(skb);
299 void *pa = PTR_ALIGN(p, 8);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300300
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800301 if (skb_tailroom(skb) >= len + (pa - p)) {
302 phy_length = len - 8;
303 memcpy(phy_data, pa, phy_length);
304 }
305 }
306 }
307 rtap_len += phy_length;
308 }
309
310 if (skb_headroom(skb) < rtap_len &&
311 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
312 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
313 return;
314 }
315
316 rtap_vendor = (void *)skb_push(skb, rtap_len);
317 memset(rtap_vendor, 0, rtap_len);
318
319 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
320 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
321 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
322 (1 << IEEE80211_RADIOTAP_FLAGS) |
323 (1 << IEEE80211_RADIOTAP_CHANNEL) |
324 (1 << IEEE80211_RADIOTAP_MCS));
325 if (d->dma.status & RX_DMA_STATUS_ERROR)
326 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
327
328 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
329 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
330
331 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
332 rtap_vendor->rtap.mcs_flags = 0;
333 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
334
335 if (rtap_include_phy_info) {
336 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
337 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
338 /* OUI for Wilocity 04:ce:14 */
339 rtap_vendor->vendor_oui[0] = 0x04;
340 rtap_vendor->vendor_oui[1] = 0xce;
341 rtap_vendor->vendor_oui[2] = 0x14;
342 rtap_vendor->vendor_ns = 1;
343 /* Rx descriptor + PHY data */
344 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
345 phy_length);
346 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
347 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
348 phy_length);
349 }
350}
351
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800352/**
353 * reap 1 frame from @swhead
354 *
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300355 * Rx descriptor copied to skb->cb
356 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800357 * Safe to call from IRQ
358 */
359static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
360 struct vring *vring)
361{
362 struct device *dev = wil_to_dev(wil);
363 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300364 volatile struct vring_rx_desc *_d;
365 struct vring_rx_desc *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800366 struct sk_buff *skb;
367 dma_addr_t pa;
Vladimir Kondratiev9a06bec2014-10-28 16:51:27 +0200368 unsigned int sz = mtu_max + ETH_HLEN;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300369 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800370 u8 ftype;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200371 int cid;
372 struct wil_net_stats *stats;
373
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300374 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
375
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200376 if (unlikely(wil_vring_is_empty(vring)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800377 return NULL;
378
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300379 _d = &vring->va[vring->swhead].rx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200380 if (unlikely(!(_d->dma.status & RX_DMA_STATUS_DU))) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800381 /* it is not error, we just reached end of Rx done area */
382 return NULL;
383 }
384
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300385 skb = vring->ctx[vring->swhead].skb;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300386 d = wil_skb_rxdesc(skb);
387 *d = *_d;
388 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300389 vring->ctx[vring->swhead].skb = NULL;
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300390 wil_vring_advance_head(vring, 1);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300391
392 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
393 dmalen = le16_to_cpu(d->dma.length);
394
395 trace_wil6210_rx(vring->swhead, d);
396 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen);
397 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
398 (const void *)d, sizeof(*d), false);
399
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200400 if (unlikely(dmalen > sz)) {
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300401 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
Wei Yongjun110dea02013-05-23 17:10:43 +0800402 kfree_skb(skb);
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300403 return NULL;
404 }
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300405 skb_trim(skb, dmalen);
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300406
Vladimir Kondratiev1cbbcb02014-01-08 11:50:49 +0200407 prefetch(skb->data);
408
Vladimir Kondratievc0d37712013-05-12 14:43:34 +0300409 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
410 skb->data, skb_headlen(skb), false);
411
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200412 cid = wil_rxdesc_cid(d);
413 stats = &wil->sta[cid].stats;
414 stats->last_mcs_rx = wil_rxdesc_mcs(d);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800415
416 /* use radiotap header only if required */
417 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300418 wil_rx_add_radiotap_header(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800419
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800420 /* no extra checks if in sniffer mode */
421 if (ndev->type != ARPHRD_ETHER)
422 return skb;
423 /*
424 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
425 * Driver should recognize it by frame type, that is found
426 * in Rx descriptor. If type is not data, it is 802.11 frame as is
427 */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300428 ftype = wil_rxdesc_ftype(d) << 2;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200429 if (unlikely(ftype != IEEE80211_FTYPE_DATA)) {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200430 wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800431 /* TODO: process it */
432 kfree_skb(skb);
433 return NULL;
434 }
435
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200436 if (unlikely(skb->len < ETH_HLEN)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800437 wil_err(wil, "Short frame, len = %d\n", skb->len);
438 /* TODO: process it (i.e. BAR) */
439 kfree_skb(skb);
440 return NULL;
441 }
442
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300443 /* L4 IDENT is on when HW calculated checksum, check status
444 * and in case of error drop the packet
445 * higher stack layers will handle retransmission (if required)
446 */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200447 if (likely(d->dma.status & RX_DMA_STATUS_L4I)) {
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300448 /* L4 protocol identified, csum calculated */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200449 if (likely((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0))
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300450 skb->ip_summed = CHECKSUM_UNNECESSARY;
Vladimir Kondratiev4a68ab102013-08-13 15:25:32 +0300451 /* If HW reports bad checksum, let IP stack re-check it
452 * For example, HW don't understand Microsoft IP stack that
453 * mis-calculates TCP checksum - if it should be 0x0,
454 * it writes 0xffff in violation of RFC 1624
455 */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300456 }
457
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800458 return skb;
459}
460
461/**
462 * allocate and fill up to @count buffers in rx ring
463 * buffers posted at @swtail
464 */
465static int wil_rx_refill(struct wil6210_priv *wil, int count)
466{
467 struct net_device *ndev = wil_to_ndev(wil);
468 struct vring *v = &wil->vring_rx;
469 u32 next_tail;
470 int rc = 0;
471 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
472 WIL6210_RTAP_SIZE : 0;
473
474 for (; next_tail = wil_vring_next_tail(v),
475 (next_tail != v->swhead) && (count-- > 0);
476 v->swtail = next_tail) {
477 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200478 if (unlikely(rc)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800479 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
480 rc, v->swtail);
481 break;
482 }
483 }
484 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
485
486 return rc;
487}
488
489/*
490 * Pass Rx packet to the netif. Update statistics.
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300491 * Called in softirq context (NAPI poll).
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800492 */
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200493void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800494{
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200495 gro_result_t rc = GRO_NORMAL;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200496 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200497 struct wireless_dev *wdev = wil_to_wdev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800498 unsigned int len = skb->len;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200499 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
500 int cid = wil_rxdesc_cid(d);
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200501 struct ethhdr *eth = (void *)skb->data;
502 /* here looking for DA, not A1, thus Rxdesc's 'mcast' indication
503 * is not suitable, need to look at data
504 */
505 int mcast = is_multicast_ether_addr(eth->h_dest);
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200506 struct wil_net_stats *stats = &wil->sta[cid].stats;
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200507 struct sk_buff *xmit_skb = NULL;
508 static const char * const gro_res_str[] = {
509 [GRO_MERGED] = "GRO_MERGED",
510 [GRO_MERGED_FREE] = "GRO_MERGED_FREE",
511 [GRO_HELD] = "GRO_HELD",
512 [GRO_NORMAL] = "GRO_NORMAL",
513 [GRO_DROP] = "GRO_DROP",
514 };
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800515
Vladimir Kondratiev241804c2013-01-28 18:31:02 +0200516 skb_orphan(skb);
517
Vladimir Kondratiev02beaf12015-03-08 15:42:03 +0200518 if (wdev->iftype == NL80211_IFTYPE_AP && !wil->ap_isolate) {
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200519 if (mcast) {
520 /* send multicast frames both to higher layers in
521 * local net stack and back to the wireless medium
522 */
523 xmit_skb = skb_copy(skb, GFP_ATOMIC);
524 } else {
525 int xmit_cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800526
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200527 if (xmit_cid >= 0) {
528 /* The destination station is associated to
529 * this AP (in this VLAN), so send the frame
530 * directly to it and do not pass it to local
531 * net stack.
532 */
533 xmit_skb = skb;
534 skb = NULL;
535 }
536 }
537 }
538 if (xmit_skb) {
539 /* Send to wireless media and increase priority by 256 to
540 * keep the received priority instead of reclassifying
541 * the frame (see cfg80211_classify8021d).
542 */
543 xmit_skb->dev = ndev;
544 xmit_skb->priority += 256;
545 xmit_skb->protocol = htons(ETH_P_802_3);
546 skb_reset_network_header(xmit_skb);
547 skb_reset_mac_header(xmit_skb);
548 wil_dbg_txrx(wil, "Rx -> Tx %d bytes\n", len);
549 dev_queue_xmit(xmit_skb);
550 }
551
552 if (skb) { /* deliver to local stack */
553
554 skb->protocol = eth_type_trans(skb, ndev);
555 rc = napi_gro_receive(&wil->napi_rx, skb);
556 wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
557 len, gro_res_str[rc]);
558 }
559 /* statistics. rc set to GRO_NORMAL for AP bridging */
Vladimir Kondratievb5998e62014-03-17 15:34:22 +0200560 if (unlikely(rc == GRO_DROP)) {
561 ndev->stats.rx_dropped++;
562 stats->rx_dropped++;
563 wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
564 } else {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800565 ndev->stats.rx_packets++;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200566 stats->rx_packets++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800567 ndev->stats.rx_bytes += len;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200568 stats->rx_bytes += len;
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200569 if (mcast)
570 ndev->stats.multicast++;
Vladimir Kondratiev194b4822014-06-16 19:37:14 +0300571 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800572}
573
574/**
575 * Proceed all completed skb's from Rx VRING
576 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300577 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800578 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300579void wil_rx_handle(struct wil6210_priv *wil, int *quota)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800580{
581 struct net_device *ndev = wil_to_ndev(wil);
582 struct vring *v = &wil->vring_rx;
583 struct sk_buff *skb;
584
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200585 if (unlikely(!v->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800586 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
587 return;
588 }
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200589 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300590 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
591 (*quota)--;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800592
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800593 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
594 skb->dev = ndev;
595 skb_reset_mac_header(skb);
596 skb->ip_summed = CHECKSUM_UNNECESSARY;
597 skb->pkt_type = PACKET_OTHERHOST;
598 skb->protocol = htons(ETH_P_802_2);
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200599 wil_netif_rx_any(skb, ndev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800600 } else {
Vladimir Kondratieve4373d82014-12-23 09:47:21 +0200601 wil_rx_reorder(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800602 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800603 }
604 wil_rx_refill(wil, v->size);
605}
606
Vladimir Kondratievd3762b42014-12-01 15:35:02 +0200607int wil_rx_init(struct wil6210_priv *wil, u16 size)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800608{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800609 struct vring *vring = &wil->vring_rx;
610 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800611
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300612 wil_dbg_misc(wil, "%s()\n", __func__);
613
Vladimir Kondratiev8bf6adb2014-03-17 15:34:17 +0200614 if (vring->va) {
615 wil_err(wil, "Rx ring already allocated\n");
616 return -EINVAL;
617 }
618
Vladimir Kondratievd3762b42014-12-01 15:35:02 +0200619 vring->size = size;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800620 rc = wil_vring_alloc(wil, vring);
621 if (rc)
622 return rc;
623
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +0200624 rc = wmi_rx_chain_add(wil, vring);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800625 if (rc)
626 goto err_free;
627
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800628 rc = wil_rx_refill(wil, vring->size);
629 if (rc)
630 goto err_free;
631
632 return 0;
633 err_free:
634 wil_vring_free(wil, vring, 0);
635
636 return rc;
637}
638
639void wil_rx_fini(struct wil6210_priv *wil)
640{
641 struct vring *vring = &wil->vring_rx;
642
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300643 wil_dbg_misc(wil, "%s()\n", __func__);
644
Vladimir Kondratiev2acb4222013-01-28 18:31:08 +0200645 if (vring->va)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800646 wil_vring_free(wil, vring, 0);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800647}
648
649int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
650 int cid, int tid)
651{
652 int rc;
653 struct wmi_vring_cfg_cmd cmd = {
654 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
655 .vring_cfg = {
656 .tx_sw_ring = {
Vladimir Kondratiev9a06bec2014-10-28 16:51:27 +0200657 .max_mpdu_size =
Vladimir Kondratievc44690a2014-12-23 09:47:11 +0200658 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
Vladimir Kondratievb5d98e92013-04-18 14:33:51 +0300659 .ring_size = cpu_to_le16(size),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800660 },
661 .ringid = id,
Vladimir Kondratieva70abea2014-03-17 15:34:05 +0200662 .cidxtid = mk_cidxtid(cid, tid),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800663 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
664 .mac_ctrl = 0,
665 .to_resolution = 0,
Vladimir Kondratiev32772132014-12-23 09:47:03 +0200666 .agg_max_wsize = 0,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800667 .schd_params = {
668 .priority = cpu_to_le16(0),
669 .timeslot_us = cpu_to_le16(0xfff),
670 },
671 },
672 };
673 struct {
674 struct wil6210_mbox_hdr_wmi wmi;
675 struct wmi_vring_cfg_done_event cmd;
676 } __packed reply;
677 struct vring *vring = &wil->vring_tx[id];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200678 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800679
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +0300680 wil_dbg_misc(wil, "%s() max_mpdu_size %d\n", __func__,
681 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300682
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800683 if (vring->va) {
684 wil_err(wil, "Tx ring [%d] already allocated\n", id);
685 rc = -EINVAL;
686 goto out;
687 }
688
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200689 memset(txdata, 0, sizeof(*txdata));
Vladimir Kondratiev5933a062015-02-01 10:55:13 +0200690 spin_lock_init(&txdata->lock);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800691 vring->size = size;
692 rc = wil_vring_alloc(wil, vring);
693 if (rc)
694 goto out;
695
Vladimir Kondratiev93ae6d42014-02-27 16:20:51 +0200696 wil->vring2cid_tid[id][0] = cid;
697 wil->vring2cid_tid[id][1] = tid;
698
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800699 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800700
701 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
702 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
703 if (rc)
704 goto out_free;
705
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200706 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800707 wil_err(wil, "Tx config failed, status 0x%02x\n",
708 reply.cmd.status);
Vladimir Kondratievc3319972013-01-28 18:31:09 +0200709 rc = -EINVAL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800710 goto out_free;
711 }
712 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
713
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200714 txdata->enabled = 1;
Vladimir Kondratiev3a3def82014-12-23 09:47:05 +0200715 if (wil->sta[cid].data_port_open && (agg_wsize >= 0))
716 wil_addba_tx_request(wil, id, agg_wsize);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200717
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800718 return 0;
719 out_free:
720 wil_vring_free(wil, vring, 1);
721 out:
722
723 return rc;
724}
725
726void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
727{
728 struct vring *vring = &wil->vring_tx[id];
Vladimir Kondratiev3a124ed2014-12-23 09:47:04 +0200729 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800730
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200731 WARN_ON(!mutex_is_locked(&wil->mutex));
732
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800733 if (!vring->va)
734 return;
735
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300736 wil_dbg_misc(wil, "%s() id=%d\n", __func__, id);
737
Vladimir Kondratiev5933a062015-02-01 10:55:13 +0200738 spin_lock_bh(&txdata->lock);
739 txdata->enabled = 0; /* no Tx can be in progress or start anew */
740 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200741 /* make sure NAPI won't touch this vring */
Vladimir Kondratiev9419b6a2014-12-23 09:47:14 +0200742 if (test_bit(wil_status_napi_en, wil->status))
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200743 napi_synchronize(&wil->napi_tx);
744
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800745 wil_vring_free(wil, vring, 1);
Vladimir Kondratiev3a124ed2014-12-23 09:47:04 +0200746 memset(txdata, 0, sizeof(*txdata));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800747}
748
749static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
750 struct sk_buff *skb)
751{
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200752 int i;
753 struct ethhdr *eth = (void *)skb->data;
754 int cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800755
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200756 if (cid < 0)
757 return NULL;
758
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200759 if (!wil->sta[cid].data_port_open &&
760 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
761 return NULL;
762
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200763 /* TODO: fix for multiple TID */
764 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
765 if (wil->vring2cid_tid[i][0] == cid) {
766 struct vring *v = &wil->vring_tx[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300767
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200768 wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n",
769 __func__, eth->h_dest, i);
770 if (v->va) {
771 return v;
772 } else {
773 wil_dbg_txrx(wil, "vring[%d] not valid\n", i);
774 return NULL;
775 }
776 }
777 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800778
779 return NULL;
780}
781
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200782static void wil_set_da_for_vring(struct wil6210_priv *wil,
783 struct sk_buff *skb, int vring_index)
784{
785 struct ethhdr *eth = (void *)skb->data;
786 int cid = wil->vring2cid_tid[vring_index][0];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300787
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200788 memcpy(eth->h_dest, wil->sta[cid].addr, ETH_ALEN);
789}
790
791static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
792 struct sk_buff *skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +0200793
794static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
795 struct sk_buff *skb)
796{
797 struct vring *v;
798 int i;
799 u8 cid;
800
801 /* In the STA mode, it is expected to have only 1 VRING
802 * for the AP we connected to.
803 * find 1-st vring and see whether it is eligible for data
804 */
805 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
806 v = &wil->vring_tx[i];
807 if (!v->va)
808 continue;
809
810 cid = wil->vring2cid_tid[i][0];
811 if (!wil->sta[cid].data_port_open &&
812 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
813 break;
814
815 wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
816
817 return v;
818 }
819
820 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
821
822 return NULL;
823}
824
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200825/*
826 * Find 1-st vring and return it; set dest address for this vring in skb
827 * duplicate skb and send it to other active vrings
828 */
829static struct vring *wil_tx_bcast(struct wil6210_priv *wil,
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300830 struct sk_buff *skb)
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200831{
832 struct vring *v, *v2;
833 struct sk_buff *skb2;
834 int i;
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200835 u8 cid;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200836
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200837 /* find 1-st vring eligible for data */
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200838 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
839 v = &wil->vring_tx[i];
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200840 if (!v->va)
841 continue;
842
843 cid = wil->vring2cid_tid[i][0];
844 if (!wil->sta[cid].data_port_open)
845 continue;
846
847 goto found;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200848 }
849
Vladimir Kondratiev5aed1392014-06-16 19:37:15 +0300850 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200851
852 return NULL;
853
854found:
855 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
856 wil_set_da_for_vring(wil, skb, i);
857
858 /* find other active vrings and duplicate skb for each */
859 for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
860 v2 = &wil->vring_tx[i];
861 if (!v2->va)
862 continue;
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +0200863 cid = wil->vring2cid_tid[i][0];
864 if (!wil->sta[cid].data_port_open)
865 continue;
866
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +0200867 skb2 = skb_copy(skb, GFP_ATOMIC);
868 if (skb2) {
869 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
870 wil_set_da_for_vring(wil, skb2, i);
871 wil_tx_vring(wil, v2, skb2);
872 } else {
873 wil_err(wil, "skb_copy failed\n");
874 }
875 }
876
877 return v;
878}
879
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300880static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
881 int vring_index)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800882{
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300883 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800884 d->dma.ip_length = 0;
885 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
886 d->dma.b11 = 0/*14 | BIT(7)*/;
887 d->dma.error = 0;
888 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300889 d->dma.length = cpu_to_le16((u16)len);
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300890 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800891 d->mac.d[0] = 0;
892 d->mac.d[1] = 0;
893 d->mac.d[2] = 0;
894 d->mac.ucode_cmd = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800895 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
896 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
897 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
898
899 return 0;
900}
901
Vladimir Kondratievc2366582014-03-17 15:34:08 +0200902static inline
903void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
904{
905 d->mac.d[2] |= ((nr_frags + 1) <<
906 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
907}
908
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300909static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil,
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300910 struct vring_tx_desc *d,
911 struct sk_buff *skb)
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300912{
913 int protocol;
914
915 if (skb->ip_summed != CHECKSUM_PARTIAL)
916 return 0;
917
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +0200918 d->dma.b11 = ETH_HLEN; /* MAC header length */
919
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300920 switch (skb->protocol) {
921 case cpu_to_be16(ETH_P_IP):
922 protocol = ip_hdr(skb)->protocol;
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +0200923 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300924 break;
925 case cpu_to_be16(ETH_P_IPV6):
926 protocol = ipv6_hdr(skb)->nexthdr;
927 break;
928 default:
929 return -EINVAL;
930 }
931
932 switch (protocol) {
933 case IPPROTO_TCP:
934 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
935 /* L4 header len: TCP header length */
936 d->dma.d0 |=
937 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
938 break;
939 case IPPROTO_UDP:
940 /* L4 header len: UDP header length */
941 d->dma.d0 |=
942 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
943 break;
944 default:
945 return -EINVAL;
946 }
947
948 d->dma.ip_length = skb_network_header_len(skb);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300949 /* Enable TCP/UDP checksum */
950 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
951 /* Calculate pseudo-header */
952 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
953
954 return 0;
955}
956
Vladimir Kondratiev5933a062015-02-01 10:55:13 +0200957static int __wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
958 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800959{
960 struct device *dev = wil_to_dev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300961 struct vring_tx_desc dd, *d = &dd;
962 volatile struct vring_tx_desc *_d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800963 u32 swhead = vring->swhead;
964 int avail = wil_vring_avail_tx(vring);
965 int nr_frags = skb_shinfo(skb)->nr_frags;
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300966 uint f = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800967 int vring_index = vring - wil->vring_tx;
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +0300968 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800969 uint i = swhead;
970 dma_addr_t pa;
Vladimir Shulman0436fd92015-02-15 14:02:34 +0200971 int used;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800972
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200973 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800974
Vladimir Kondratiev5933a062015-02-01 10:55:13 +0200975 if (unlikely(!txdata->enabled))
976 return -EINVAL;
977
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200978 if (unlikely(avail < 1 + nr_frags)) {
Vladimir Kondratiev70801e12014-12-01 15:36:03 +0200979 wil_err_ratelimited(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +0200980 "Tx ring[%2d] full. No space for %d fragments\n",
981 vring_index, 1 + nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800982 return -ENOMEM;
983 }
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300984 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800985
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300986 pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800987
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +0200988 wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index,
989 skb_headlen(skb), skb->data, &pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200990 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800991 skb->data, skb_headlen(skb), false);
992
993 if (unlikely(dma_mapping_error(dev, pa)))
994 return -EINVAL;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200995 vring->ctx[i].mapped_as = wil_mapped_as_single;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800996 /* 1-st segment */
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300997 wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300998 /* Process TCP/UDP checksum offloading */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200999 if (unlikely(wil_tx_desc_offload_cksum_set(wil, d, skb))) {
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001000 wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001001 vring_index);
1002 goto dma_error;
1003 }
1004
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001005 vring->ctx[i].nr_frags = nr_frags;
1006 wil_tx_desc_set_nr_frags(d, nr_frags);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001007
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001008 /* middle segments */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001009 for (; f < nr_frags; f++) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001010 const struct skb_frag_struct *frag =
1011 &skb_shinfo(skb)->frags[f];
1012 int len = skb_frag_size(frag);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001013
Vladimir Kondratieve59d16c2015-02-01 10:55:12 +02001014 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001015 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1016 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1017 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001018 i = (swhead + f + 1) % vring->size;
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001019 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001020 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001021 DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001022 if (unlikely(dma_mapping_error(dev, pa)))
1023 goto dma_error;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001024 vring->ctx[i].mapped_as = wil_mapped_as_page;
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001025 wil_tx_desc_map(d, pa, len, vring_index);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001026 /* no need to check return code -
1027 * if it succeeded for 1-st descriptor,
1028 * it will succeed here too
1029 */
1030 wil_tx_desc_offload_cksum_set(wil, d, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001031 }
1032 /* for the last seg only */
1033 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
Kirshenbaum Erez668b2bb2013-06-23 12:59:35 +03001034 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001035 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001036 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001037 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1038 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1039 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001040
Vladimir Kondratiev6cdadd42013-07-11 18:03:41 +03001041 /* hold reference to skb
1042 * to prevent skb release before accounting
1043 * in case of immediate "tx done"
1044 */
1045 vring->ctx[i].skb = skb_get(skb);
1046
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001047 /* performance monitoring */
1048 used = wil_vring_used_tx(vring);
1049 if (wil_val_in_range(vring_idle_trsh,
1050 used, used + nr_frags + 1)) {
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001051 txdata->idle += get_cycles() - txdata->last_idle;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001052 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1053 vring_index, used, used + nr_frags + 1);
1054 }
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001055
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001056 /* advance swhead */
1057 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001058 wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead,
1059 vring->swhead);
Vladimir Kondratiev98658092013-05-12 14:43:35 +03001060 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001061 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001062
1063 return 0;
1064 dma_error:
1065 /* unmap what we have mapped */
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001066 nr_frags = f + 1; /* frags mapped + one for skb head */
1067 for (f = 0; f < nr_frags; f++) {
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001068 struct wil_ctx *ctx;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001069
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001070 i = (swhead + f) % vring->size;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001071 ctx = &vring->ctx[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001072 _d = &vring->va[i].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001073 *d = *_d;
1074 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001075 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001076
1077 if (ctx->skb)
1078 dev_kfree_skb_any(ctx->skb);
1079
1080 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001081 }
1082
1083 return -EINVAL;
1084}
1085
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001086static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1087 struct sk_buff *skb)
1088{
1089 int vring_index = vring - wil->vring_tx;
1090 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1091 int rc;
1092
1093 spin_lock(&txdata->lock);
1094 rc = __wil_tx_vring(wil, vring, skb);
1095 spin_unlock(&txdata->lock);
1096 return rc;
1097}
1098
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001099netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1100{
1101 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001102 struct ethhdr *eth = (void *)skb->data;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001103 struct vring *vring;
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001104 static bool pr_once_fw;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001105 int rc;
1106
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001107 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001108 if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001109 if (!pr_once_fw) {
1110 wil_err(wil, "FW not ready\n");
1111 pr_once_fw = true;
1112 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001113 goto drop;
1114 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001115 if (unlikely(!test_bit(wil_status_fwconnected, wil->status))) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001116 wil_err(wil, "FW not connected\n");
1117 goto drop;
1118 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001119 if (unlikely(wil->wdev->iftype == NL80211_IFTYPE_MONITOR)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001120 wil_err(wil, "Xmit in monitor mode not supported\n");
1121 goto drop;
1122 }
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001123 pr_once_fw = false;
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001124
1125 /* find vring */
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001126 if (wil->wdev->iftype == NL80211_IFTYPE_STATION) {
1127 /* in STA mode (ESS), all to same VRING */
1128 vring = wil_find_tx_vring_sta(wil, skb);
1129 } else { /* direct communication, find matching VRING */
1130 if (is_unicast_ether_addr(eth->h_dest))
1131 vring = wil_find_tx_vring(wil, skb);
1132 else
1133 vring = wil_tx_bcast(wil, skb);
1134 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001135 if (unlikely(!vring)) {
Vladimir Kondratiev5aed1392014-06-16 19:37:15 +03001136 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001137 goto drop;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001138 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001139 /* set up vring entry */
1140 rc = wil_tx_vring(wil, vring, skb);
1141
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001142 /* do we still have enough room in the vring? */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001143 if (unlikely(wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring))) {
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001144 netif_tx_stop_all_queues(wil_to_ndev(wil));
Vladimir Kondratiev55f8f682014-06-16 19:37:23 +03001145 wil_dbg_txrx(wil, "netif_tx_stop : ring full\n");
1146 }
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001147
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001148 switch (rc) {
1149 case 0:
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02001150 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001151 dev_kfree_skb_any(skb);
1152 return NETDEV_TX_OK;
1153 case -ENOMEM:
1154 return NETDEV_TX_BUSY;
1155 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +02001156 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001157 }
1158 drop:
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001159 ndev->stats.tx_dropped++;
1160 dev_kfree_skb_any(skb);
1161
1162 return NET_XMIT_DROP;
1163}
1164
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02001165static inline bool wil_need_txstat(struct sk_buff *skb)
1166{
1167 struct ethhdr *eth = (void *)skb->data;
1168
1169 return is_unicast_ether_addr(eth->h_dest) && skb->sk &&
1170 (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS);
1171}
1172
1173static inline void wil_consume_skb(struct sk_buff *skb, bool acked)
1174{
1175 if (unlikely(wil_need_txstat(skb)))
1176 skb_complete_wifi_ack(skb, acked);
1177 else
1178 acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb);
1179}
1180
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001181/**
1182 * Clean up transmitted skb's from the Tx VRING
1183 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001184 * Return number of descriptors cleared
1185 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001186 * Safe to call from IRQ
1187 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001188int wil_tx_complete(struct wil6210_priv *wil, int ringid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001189{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02001190 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001191 struct device *dev = wil_to_dev(wil);
1192 struct vring *vring = &wil->vring_tx[ringid];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001193 struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001194 int done = 0;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +02001195 int cid = wil->vring2cid_tid[ringid][0];
1196 struct wil_net_stats *stats = &wil->sta[cid].stats;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001197 volatile struct vring_tx_desc *_d;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001198 int used_before_complete;
1199 int used_new;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001200
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001201 if (unlikely(!vring->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001202 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001203 return 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001204 }
1205
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001206 if (unlikely(!txdata->enabled)) {
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001207 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
1208 return 0;
1209 }
1210
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001211 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001212
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001213 used_before_complete = wil_vring_used_tx(vring);
1214
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001215 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001216 int new_swtail;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001217 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001218 /**
1219 * For the fragmented skb, HW will set DU bit only for the
1220 * last fragment. look for it
1221 */
1222 int lf = (vring->swtail + ctx->nr_frags) % vring->size;
1223 /* TODO: check we are not past head */
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +03001224
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001225 _d = &vring->va[lf].tx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001226 if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001227 break;
1228
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001229 new_swtail = (lf + 1) % vring->size;
1230 while (vring->swtail != new_swtail) {
1231 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001232 u16 dmalen;
Vladimir Kondratiev76dfa4b2014-07-14 09:49:39 +03001233 struct sk_buff *skb;
1234
1235 ctx = &vring->ctx[vring->swtail];
1236 skb = ctx->skb;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001237 _d = &vring->va[vring->swtail].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001238
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001239 *d = *_d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001240
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001241 dmalen = le16_to_cpu(d->dma.length);
1242 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
1243 d->dma.error);
1244 wil_dbg_txrx(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001245 "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
1246 ringid, vring->swtail, dmalen,
1247 d->dma.status, d->dma.error);
1248 wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001249 (const void *)d, sizeof(*d), false);
1250
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001251 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001252
1253 if (skb) {
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001254 if (likely(d->dma.error == 0)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001255 ndev->stats.tx_packets++;
1256 stats->tx_packets++;
1257 ndev->stats.tx_bytes += skb->len;
1258 stats->tx_bytes += skb->len;
1259 } else {
1260 ndev->stats.tx_errors++;
1261 stats->tx_errors++;
1262 }
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02001263 wil_consume_skb(skb, d->dma.error == 0);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02001264 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001265 memset(ctx, 0, sizeof(*ctx));
1266 /* There is no need to touch HW descriptor:
1267 * - ststus bit TX_DMA_STATUS_DU is set by design,
1268 * so hardware will not try to process this desc.,
1269 * - rest of descriptor will be initialized on Tx.
1270 */
1271 vring->swtail = wil_vring_next_tail(vring);
1272 done++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001273 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001274 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03001275
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001276 /* performance monitoring */
1277 used_new = wil_vring_used_tx(vring);
1278 if (wil_val_in_range(vring_idle_trsh,
1279 used_new, used_before_complete)) {
1280 wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
1281 ringid, used_before_complete, used_new);
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001282 txdata->last_idle = get_cycles();
1283 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03001284
Vladimir Kondratiev55f8f682014-06-16 19:37:23 +03001285 if (wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring)) {
1286 wil_dbg_txrx(wil, "netif_tx_wake : ring not full\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001287 netif_tx_wake_all_queues(wil_to_ndev(wil));
Vladimir Kondratiev55f8f682014-06-16 19:37:23 +03001288 }
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001289
1290 return done;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001291}