blob: 28ad8099a854333119f34b571019fab25a8ccead [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
Lazar Alexei3190cc62017-02-23 14:34:14 +02002 * Copyright (c) 2012-2017 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;
Maya Erezc2256ec2017-02-08 13:18:42 +020032module_param(rtap_include_phy_info, bool, 0444);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080033MODULE_PARM_DESC(rtap_include_phy_info,
34 " Include PHY info in the radiotap header, default - no");
35
Vladimir Kondratievc406ea72015-03-15 16:00:19 +020036bool rx_align_2;
Maya Erezc2256ec2017-02-08 13:18:42 +020037module_param(rx_align_2, bool, 0444);
Vladimir Kondratievc406ea72015-03-15 16:00:19 +020038MODULE_PARM_DESC(rx_align_2, " align Rx buffers on 4*n+2, default - no");
39
Lior David2e9a3212017-05-04 22:43:18 +030040bool rx_large_buf;
41module_param(rx_large_buf, bool, 0444);
42MODULE_PARM_DESC(rx_large_buf, " allocate 8KB RX buffers, default - no");
43
Vladimir Kondratievc406ea72015-03-15 16:00:19 +020044static inline uint wil_rx_snaplen(void)
45{
46 return rx_align_2 ? 6 : 0;
47}
48
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080049static inline int wil_vring_is_empty(struct vring *vring)
50{
51 return vring->swhead == vring->swtail;
52}
53
54static inline u32 wil_vring_next_tail(struct vring *vring)
55{
56 return (vring->swtail + 1) % vring->size;
57}
58
59static inline void wil_vring_advance_head(struct vring *vring, int n)
60{
61 vring->swhead = (vring->swhead + n) % vring->size;
62}
63
64static inline int wil_vring_is_full(struct vring *vring)
65{
66 return wil_vring_next_tail(vring) == vring->swhead;
67}
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +030068
Vladimir Shulman0436fd92015-02-15 14:02:34 +020069/* Used space in Tx Vring */
70static inline int wil_vring_used_tx(struct vring *vring)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080071{
72 u32 swhead = vring->swhead;
73 u32 swtail = vring->swtail;
Vladimir Shulman0436fd92015-02-15 14:02:34 +020074 return (vring->size + swhead - swtail) % vring->size;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080075}
76
Vladimir Shulman0436fd92015-02-15 14:02:34 +020077/* Available space in Tx Vring */
78static inline int wil_vring_avail_tx(struct vring *vring)
79{
80 return vring->size - wil_vring_used_tx(vring) - 1;
81}
82
83/* wil_vring_wmark_low - low watermark for available descriptor space */
Vladimir Kondratiev5bb64232014-05-27 14:45:46 +030084static inline int wil_vring_wmark_low(struct vring *vring)
85{
86 return vring->size/8;
87}
88
Vladimir Shulman0436fd92015-02-15 14:02:34 +020089/* wil_vring_wmark_high - high watermark for available descriptor space */
Vladimir Kondratiev5bb64232014-05-27 14:45:46 +030090static inline int wil_vring_wmark_high(struct vring *vring)
91{
92 return vring->size/4;
93}
94
Dedy Lansky970a98f2016-12-06 08:26:49 +020095/* returns true if num avail descriptors is lower than wmark_low */
96static inline int wil_vring_avail_low(struct vring *vring)
97{
98 return wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring);
99}
100
101/* returns true if num avail descriptors is higher than wmark_high */
102static inline int wil_vring_avail_high(struct vring *vring)
103{
104 return wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring);
105}
106
Maya Ereze3309bf2017-05-15 16:57:46 +0300107/* returns true when all tx vrings are empty */
108bool wil_is_tx_idle(struct wil6210_priv *wil)
109{
110 int i;
111 unsigned long data_comp_to;
112
113 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
114 struct vring *vring = &wil->vring_tx[i];
115 int vring_index = vring - wil->vring_tx;
116 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
117
118 spin_lock(&txdata->lock);
119
120 if (!vring->va || !txdata->enabled) {
121 spin_unlock(&txdata->lock);
122 continue;
123 }
124
125 data_comp_to = jiffies + msecs_to_jiffies(
126 WIL_DATA_COMPLETION_TO_MS);
127 if (test_bit(wil_status_napi_en, wil->status)) {
128 while (!wil_vring_is_empty(vring)) {
129 if (time_after(jiffies, data_comp_to)) {
130 wil_dbg_pm(wil,
131 "TO waiting for idle tx\n");
132 spin_unlock(&txdata->lock);
133 return false;
134 }
135 wil_dbg_ratelimited(wil,
136 "tx vring is not empty -> NAPI\n");
137 spin_unlock(&txdata->lock);
138 napi_synchronize(&wil->napi_tx);
139 msleep(20);
140 spin_lock(&txdata->lock);
141 if (!vring->va || !txdata->enabled)
142 break;
143 }
144 }
145
146 spin_unlock(&txdata->lock);
147 }
148
149 return true;
150}
151
Vladimir Shulman0436fd92015-02-15 14:02:34 +0200152/* wil_val_in_range - check if value in [min,max) */
153static inline bool wil_val_in_range(int val, int min, int max)
154{
155 return val >= min && val < max;
156}
157
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800158static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
159{
160 struct device *dev = wil_to_dev(wil);
161 size_t sz = vring->size * sizeof(vring->va[0]);
162 uint i;
163
Lazar Alexei3190cc62017-02-23 14:34:14 +0200164 wil_dbg_misc(wil, "vring_alloc:\n");
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300165
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800166 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
167
168 vring->swhead = 0;
169 vring->swtail = 0;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300170 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800171 if (!vring->ctx) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800172 vring->va = NULL;
173 return -ENOMEM;
174 }
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200175
Vladimir Shulman0436fd92015-02-15 14:02:34 +0200176 /* vring->va should be aligned on its size rounded up to power of 2
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200177 * This is granted by the dma_alloc_coherent.
178 *
179 * HW has limitation that all vrings addresses must share the same
180 * upper 16 msb bits part of 48 bits address. To workaround that,
Lazar Alexei6630a102017-12-14 18:53:06 +0200181 * if we are using more than 32 bit addresses switch to 32 bit
182 * allocation before allocating vring memory.
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200183 *
184 * There's no check for the return value of dma_set_mask_and_coherent,
185 * since we assume if we were able to set the mask during
186 * initialization in this system it will not fail if we set it again
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800187 */
Lazar Alexei6630a102017-12-14 18:53:06 +0200188 if (wil->dma_addr_size > 32)
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200189 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
190
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800191 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
192 if (!vring->va) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800193 kfree(vring->ctx);
194 vring->ctx = NULL;
195 return -ENOMEM;
196 }
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200197
Lazar Alexei6630a102017-12-14 18:53:06 +0200198 if (wil->dma_addr_size > 32)
199 dma_set_mask_and_coherent(dev,
200 DMA_BIT_MASK(wil->dma_addr_size));
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200201
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800202 /* initially, all descriptors are SW owned
203 * For Tx and Rx, ownership bit is at the same location, thus
204 * we can use any
205 */
206 for (i = 0; i < vring->size; i++) {
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300207 volatile struct vring_tx_desc *_d = &vring->va[i].tx;
208
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300209 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800210 }
211
Vladimir Kondratiev39c52ee2014-05-27 14:45:49 +0300212 wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
213 vring->va, &vring->pa, vring->ctx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800214
215 return 0;
216}
217
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200218static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d,
219 struct wil_ctx *ctx)
220{
221 dma_addr_t pa = wil_desc_addr(&d->dma.addr);
222 u16 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300223
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200224 switch (ctx->mapped_as) {
225 case wil_mapped_as_single:
226 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
227 break;
228 case wil_mapped_as_page:
229 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
230 break;
231 default:
232 break;
233 }
234}
235
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800236static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
237 int tx)
238{
239 struct device *dev = wil_to_dev(wil);
240 size_t sz = vring->size * sizeof(vring->va[0]);
241
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +0200242 lockdep_assert_held(&wil->mutex);
Vladimir Kondratievef772852014-09-10 16:34:31 +0300243 if (tx) {
244 int vring_index = vring - wil->vring_tx;
245
246 wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
247 vring_index, vring->size, vring->va,
248 &vring->pa, vring->ctx);
249 } else {
250 wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
251 vring->size, vring->va,
252 &vring->pa, vring->ctx);
253 }
254
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800255 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300256 dma_addr_t pa;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300257 u16 dmalen;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300258 struct wil_ctx *ctx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300259
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800260 if (tx) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300261 struct vring_tx_desc dd, *d = &dd;
262 volatile struct vring_tx_desc *_d =
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800263 &vring->va[vring->swtail].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300264
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300265 ctx = &vring->ctx[vring->swtail];
Maya Erez34b88862016-05-16 22:23:32 +0300266 if (!ctx) {
267 wil_dbg_txrx(wil,
268 "ctx(%d) was already completed\n",
269 vring->swtail);
270 vring->swtail = wil_vring_next_tail(vring);
271 continue;
272 }
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300273 *d = *_d;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200274 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300275 if (ctx->skb)
276 dev_kfree_skb_any(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800277 vring->swtail = wil_vring_next_tail(vring);
278 } else { /* rx */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300279 struct vring_rx_desc dd, *d = &dd;
280 volatile struct vring_rx_desc *_d =
Vladimir Kondratiev4d1ac072013-07-11 18:03:38 +0300281 &vring->va[vring->swhead].rx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300282
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300283 ctx = &vring->ctx[vring->swhead];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300284 *d = *_d;
285 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300286 dmalen = le16_to_cpu(d->dma.length);
287 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300288 kfree_skb(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800289 wil_vring_advance_head(vring, 1);
290 }
291 }
292 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
293 kfree(vring->ctx);
294 vring->pa = 0;
295 vring->va = NULL;
296 vring->ctx = NULL;
297}
298
299/**
300 * Allocate one skb for Rx VRING
301 *
302 * Safe to call from IRQ
303 */
304static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
305 u32 i, int headroom)
306{
307 struct device *dev = wil_to_dev(wil);
Lior David2e9a3212017-05-04 22:43:18 +0300308 unsigned int sz = wil->rx_buf_len + ETH_HLEN + wil_rx_snaplen();
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300309 struct vring_rx_desc dd, *d = &dd;
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300310 volatile struct vring_rx_desc *_d = &vring->va[i].rx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800311 dma_addr_t pa;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800312 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300313
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800314 if (unlikely(!skb))
315 return -ENOMEM;
316
317 skb_reserve(skb, headroom);
318 skb_put(skb, sz);
319
320 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
321 if (unlikely(dma_mapping_error(dev, pa))) {
322 kfree_skb(skb);
323 return -ENOMEM;
324 }
325
Vladimir Kondratiev48c963a2015-04-30 16:25:06 +0300326 d->dma.d0 = RX_DMA_D0_CMD_DMA_RT | RX_DMA_D0_CMD_DMA_IT;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300327 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800328 /* ip_length don't care */
329 /* b11 don't care */
330 /* error don't care */
331 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300332 d->dma.length = cpu_to_le16(sz);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300333 *_d = *d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300334 vring->ctx[i].skb = skb;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800335
336 return 0;
337}
338
339/**
340 * Adds radiotap header
341 *
342 * Any error indicated as "Bad FCS"
343 *
344 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
345 * - Rx descriptor: 32 bytes
346 * - Phy info
347 */
348static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300349 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800350{
351 struct wireless_dev *wdev = wil->wdev;
352 struct wil6210_rtap {
353 struct ieee80211_radiotap_header rthdr;
354 /* fields should be in the order of bits in rthdr.it_present */
355 /* flags */
356 u8 flags;
357 /* channel */
358 __le16 chnl_freq __aligned(2);
359 __le16 chnl_flags;
360 /* MCS */
361 u8 mcs_present;
362 u8 mcs_flags;
363 u8 mcs_index;
364 } __packed;
365 struct wil6210_rtap_vendor {
366 struct wil6210_rtap rtap;
367 /* vendor */
368 u8 vendor_oui[3] __aligned(2);
369 u8 vendor_ns;
370 __le16 vendor_skip;
371 u8 vendor_data[0];
372 } __packed;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300373 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800374 struct wil6210_rtap_vendor *rtap_vendor;
375 int rtap_len = sizeof(struct wil6210_rtap);
376 int phy_length = 0; /* phy info header size, bytes */
377 static char phy_data[128];
378 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
379
380 if (rtap_include_phy_info) {
381 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
382 /* calculate additional length */
383 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
384 /**
385 * PHY info starts from 8-byte boundary
386 * there are 8-byte lines, last line may be partially
387 * written (HW bug), thus FW configures for last line
388 * to be excessive. Driver skips this last line.
389 */
390 int len = min_t(int, 8 + sizeof(phy_data),
391 wil_rxdesc_phy_length(d));
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300392
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800393 if (len > 8) {
394 void *p = skb_tail_pointer(skb);
395 void *pa = PTR_ALIGN(p, 8);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300396
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800397 if (skb_tailroom(skb) >= len + (pa - p)) {
398 phy_length = len - 8;
399 memcpy(phy_data, pa, phy_length);
400 }
401 }
402 }
403 rtap_len += phy_length;
404 }
405
406 if (skb_headroom(skb) < rtap_len &&
407 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
408 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
409 return;
410 }
411
412 rtap_vendor = (void *)skb_push(skb, rtap_len);
413 memset(rtap_vendor, 0, rtap_len);
414
415 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
416 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
417 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
418 (1 << IEEE80211_RADIOTAP_FLAGS) |
419 (1 << IEEE80211_RADIOTAP_CHANNEL) |
420 (1 << IEEE80211_RADIOTAP_MCS));
421 if (d->dma.status & RX_DMA_STATUS_ERROR)
422 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
423
424 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
425 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
426
427 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
428 rtap_vendor->rtap.mcs_flags = 0;
429 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
430
431 if (rtap_include_phy_info) {
432 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
433 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
434 /* OUI for Wilocity 04:ce:14 */
435 rtap_vendor->vendor_oui[0] = 0x04;
436 rtap_vendor->vendor_oui[1] = 0xce;
437 rtap_vendor->vendor_oui[2] = 0x14;
438 rtap_vendor->vendor_ns = 1;
439 /* Rx descriptor + PHY data */
440 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
441 phy_length);
442 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
443 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
444 phy_length);
445 }
446}
447
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300448/* similar to ieee80211_ version, but FC contain only 1-st byte */
449static inline int wil_is_back_req(u8 fc)
450{
451 return (fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) ==
452 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK_REQ);
453}
454
Maya Ereze3309bf2017-05-15 16:57:46 +0300455bool wil_is_rx_idle(struct wil6210_priv *wil)
456{
457 struct vring_rx_desc *_d;
458 struct vring *vring = &wil->vring_rx;
459
460 _d = (struct vring_rx_desc *)&vring->va[vring->swhead].rx;
461 if (_d->dma.status & RX_DMA_STATUS_DU)
462 return false;
463
464 return true;
465}
466
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800467/**
468 * reap 1 frame from @swhead
469 *
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300470 * Rx descriptor copied to skb->cb
471 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800472 * Safe to call from IRQ
473 */
474static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
475 struct vring *vring)
476{
477 struct device *dev = wil_to_dev(wil);
478 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300479 volatile struct vring_rx_desc *_d;
480 struct vring_rx_desc *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800481 struct sk_buff *skb;
482 dma_addr_t pa;
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200483 unsigned int snaplen = wil_rx_snaplen();
Lior David2e9a3212017-05-04 22:43:18 +0300484 unsigned int sz = wil->rx_buf_len + ETH_HLEN + snaplen;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300485 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800486 u8 ftype;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200487 int cid;
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300488 int i;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200489 struct wil_net_stats *stats;
490
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300491 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
492
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300493again:
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200494 if (unlikely(wil_vring_is_empty(vring)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800495 return NULL;
496
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300497 i = (int)vring->swhead;
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200498 _d = &vring->va[i].rx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200499 if (unlikely(!(_d->dma.status & RX_DMA_STATUS_DU))) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800500 /* it is not error, we just reached end of Rx done area */
501 return NULL;
502 }
503
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200504 skb = vring->ctx[i].skb;
505 vring->ctx[i].skb = NULL;
506 wil_vring_advance_head(vring, 1);
507 if (!skb) {
508 wil_err(wil, "No Rx skb at [%d]\n", i);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300509 goto again;
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200510 }
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300511 d = wil_skb_rxdesc(skb);
512 *d = *_d;
513 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300514
515 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
516 dmalen = le16_to_cpu(d->dma.length);
517
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200518 trace_wil6210_rx(i, d);
519 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", i, dmalen);
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300520 wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300521 (const void *)d, sizeof(*d), false);
522
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300523 cid = wil_rxdesc_cid(d);
524 stats = &wil->sta[cid].stats;
525
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200526 if (unlikely(dmalen > sz)) {
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300527 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300528 stats->rx_large_frame++;
Wei Yongjun110dea02013-05-23 17:10:43 +0800529 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300530 goto again;
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300531 }
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300532 skb_trim(skb, dmalen);
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300533
Vladimir Kondratiev1cbbcb02014-01-08 11:50:49 +0200534 prefetch(skb->data);
535
Vladimir Kondratievc0d37712013-05-12 14:43:34 +0300536 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
537 skb->data, skb_headlen(skb), false);
538
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200539 stats->last_mcs_rx = wil_rxdesc_mcs(d);
Vladimir Kondratievc4a110d2015-06-09 14:11:18 +0300540 if (stats->last_mcs_rx < ARRAY_SIZE(stats->rx_per_mcs))
541 stats->rx_per_mcs[stats->last_mcs_rx]++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800542
543 /* use radiotap header only if required */
544 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300545 wil_rx_add_radiotap_header(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800546
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800547 /* no extra checks if in sniffer mode */
548 if (ndev->type != ARPHRD_ETHER)
549 return skb;
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300550 /* Non-data frames may be delivered through Rx DMA channel (ex: BAR)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800551 * Driver should recognize it by frame type, that is found
552 * in Rx descriptor. If type is not data, it is 802.11 frame as is
553 */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300554 ftype = wil_rxdesc_ftype(d) << 2;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200555 if (unlikely(ftype != IEEE80211_FTYPE_DATA)) {
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300556 u8 fc1 = wil_rxdesc_fc1(d);
557 int mid = wil_rxdesc_mid(d);
558 int tid = wil_rxdesc_tid(d);
559 u16 seq = wil_rxdesc_seq(d);
560
561 wil_dbg_txrx(wil,
562 "Non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
563 fc1, mid, cid, tid, seq);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300564 stats->rx_non_data_frame++;
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300565 if (wil_is_back_req(fc1)) {
566 wil_dbg_txrx(wil,
567 "BAR: MID %d CID %d TID %d Seq 0x%03x\n",
568 mid, cid, tid, seq);
569 wil_rx_bar(wil, cid, tid, seq);
570 } else {
571 /* print again all info. One can enable only this
572 * without overhead for printing every Rx frame
573 */
574 wil_dbg_txrx(wil,
575 "Unhandled non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
576 fc1, mid, cid, tid, seq);
577 wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
578 (const void *)d, sizeof(*d), false);
579 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
580 skb->data, skb_headlen(skb), false);
581 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800582 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300583 goto again;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800584 }
585
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200586 if (unlikely(skb->len < ETH_HLEN + snaplen)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800587 wil_err(wil, "Short frame, len = %d\n", skb->len);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300588 stats->rx_short_frame++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800589 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300590 goto again;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800591 }
592
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300593 /* L4 IDENT is on when HW calculated checksum, check status
594 * and in case of error drop the packet
595 * higher stack layers will handle retransmission (if required)
596 */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200597 if (likely(d->dma.status & RX_DMA_STATUS_L4I)) {
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300598 /* L4 protocol identified, csum calculated */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200599 if (likely((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0))
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300600 skb->ip_summed = CHECKSUM_UNNECESSARY;
Vladimir Kondratiev4a68ab102013-08-13 15:25:32 +0300601 /* If HW reports bad checksum, let IP stack re-check it
602 * For example, HW don't understand Microsoft IP stack that
603 * mis-calculates TCP checksum - if it should be 0x0,
604 * it writes 0xffff in violation of RFC 1624
605 */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300606 }
607
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200608 if (snaplen) {
609 /* Packet layout
610 * +-------+-------+---------+------------+------+
611 * | SA(6) | DA(6) | SNAP(6) | ETHTYPE(2) | DATA |
612 * +-------+-------+---------+------------+------+
613 * Need to remove SNAP, shifting SA and DA forward
614 */
615 memmove(skb->data + snaplen, skb->data, 2 * ETH_ALEN);
616 skb_pull(skb, snaplen);
617 }
618
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800619 return skb;
620}
621
622/**
623 * allocate and fill up to @count buffers in rx ring
624 * buffers posted at @swtail
625 */
626static int wil_rx_refill(struct wil6210_priv *wil, int count)
627{
628 struct net_device *ndev = wil_to_ndev(wil);
629 struct vring *v = &wil->vring_rx;
630 u32 next_tail;
631 int rc = 0;
632 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
633 WIL6210_RTAP_SIZE : 0;
634
635 for (; next_tail = wil_vring_next_tail(v),
636 (next_tail != v->swhead) && (count-- > 0);
637 v->swtail = next_tail) {
638 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200639 if (unlikely(rc)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800640 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
641 rc, v->swtail);
642 break;
643 }
644 }
Maya Erezab6d7cc2016-05-16 22:23:31 +0300645
646 /* make sure all writes to descriptors (shared memory) are done before
647 * committing them to HW
648 */
649 wmb();
650
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +0300651 wil_w(wil, v->hwtail, v->swtail);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800652
653 return rc;
654}
655
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200656/**
657 * reverse_memcmp - Compare two areas of memory, in reverse order
658 * @cs: One area of memory
659 * @ct: Another area of memory
660 * @count: The size of the area.
661 *
662 * Cut'n'paste from original memcmp (see lib/string.c)
663 * with minimal modifications
664 */
665static int reverse_memcmp(const void *cs, const void *ct, size_t count)
666{
667 const unsigned char *su1, *su2;
668 int res = 0;
669
670 for (su1 = cs + count - 1, su2 = ct + count - 1; count > 0;
671 --su1, --su2, count--) {
672 res = *su1 - *su2;
673 if (res)
674 break;
675 }
676 return res;
677}
678
679static int wil_rx_crypto_check(struct wil6210_priv *wil, struct sk_buff *skb)
680{
681 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
682 int cid = wil_rxdesc_cid(d);
683 int tid = wil_rxdesc_tid(d);
684 int key_id = wil_rxdesc_key_id(d);
685 int mc = wil_rxdesc_mcast(d);
686 struct wil_sta_info *s = &wil->sta[cid];
687 struct wil_tid_crypto_rx *c = mc ? &s->group_crypto_rx :
688 &s->tid_crypto_rx[tid];
689 struct wil_tid_crypto_rx_single *cc = &c->key_id[key_id];
690 const u8 *pn = (u8 *)&d->mac.pn_15_0;
691
692 if (!cc->key_set) {
693 wil_err_ratelimited(wil,
694 "Key missing. CID %d TID %d MCast %d KEY_ID %d\n",
695 cid, tid, mc, key_id);
696 return -EINVAL;
697 }
698
699 if (reverse_memcmp(pn, cc->pn, IEEE80211_GCMP_PN_LEN) <= 0) {
700 wil_err_ratelimited(wil,
701 "Replay attack. CID %d TID %d MCast %d KEY_ID %d PN %6phN last %6phN\n",
702 cid, tid, mc, key_id, pn, cc->pn);
703 return -EINVAL;
704 }
705 memcpy(cc->pn, pn, IEEE80211_GCMP_PN_LEN);
706
707 return 0;
708}
709
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800710/*
711 * Pass Rx packet to the netif. Update statistics.
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300712 * Called in softirq context (NAPI poll).
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800713 */
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200714void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800715{
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200716 gro_result_t rc = GRO_NORMAL;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200717 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200718 struct wireless_dev *wdev = wil_to_wdev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800719 unsigned int len = skb->len;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200720 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200721 int cid = wil_rxdesc_cid(d); /* always 0..7, no need to check */
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200722 int security = wil_rxdesc_security(d);
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200723 struct ethhdr *eth = (void *)skb->data;
724 /* here looking for DA, not A1, thus Rxdesc's 'mcast' indication
725 * is not suitable, need to look at data
726 */
727 int mcast = is_multicast_ether_addr(eth->h_dest);
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200728 struct wil_net_stats *stats = &wil->sta[cid].stats;
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200729 struct sk_buff *xmit_skb = NULL;
730 static const char * const gro_res_str[] = {
731 [GRO_MERGED] = "GRO_MERGED",
732 [GRO_MERGED_FREE] = "GRO_MERGED_FREE",
733 [GRO_HELD] = "GRO_HELD",
734 [GRO_NORMAL] = "GRO_NORMAL",
735 [GRO_DROP] = "GRO_DROP",
736 };
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800737
Vladimir Shulman05536402015-07-30 13:52:04 +0300738 if (ndev->features & NETIF_F_RXHASH)
739 /* fake L4 to ensure it won't be re-calculated later
740 * set hash to any non-zero value to activate rps
741 * mechanism, core will be chosen according
742 * to user-level rps configuration.
743 */
744 skb_set_hash(skb, 1, PKT_HASH_TYPE_L4);
745
Vladimir Kondratiev241804c2013-01-28 18:31:02 +0200746 skb_orphan(skb);
747
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200748 if (security && (wil_rx_crypto_check(wil, skb) != 0)) {
749 rc = GRO_DROP;
750 dev_kfree_skb(skb);
751 stats->rx_replay++;
752 goto stats;
753 }
754
Vladimir Kondratiev02beaf12015-03-08 15:42:03 +0200755 if (wdev->iftype == NL80211_IFTYPE_AP && !wil->ap_isolate) {
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200756 if (mcast) {
757 /* send multicast frames both to higher layers in
758 * local net stack and back to the wireless medium
759 */
760 xmit_skb = skb_copy(skb, GFP_ATOMIC);
761 } else {
762 int xmit_cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800763
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200764 if (xmit_cid >= 0) {
765 /* The destination station is associated to
766 * this AP (in this VLAN), so send the frame
767 * directly to it and do not pass it to local
768 * net stack.
769 */
770 xmit_skb = skb;
771 skb = NULL;
772 }
773 }
774 }
775 if (xmit_skb) {
776 /* Send to wireless media and increase priority by 256 to
777 * keep the received priority instead of reclassifying
778 * the frame (see cfg80211_classify8021d).
779 */
780 xmit_skb->dev = ndev;
781 xmit_skb->priority += 256;
782 xmit_skb->protocol = htons(ETH_P_802_3);
783 skb_reset_network_header(xmit_skb);
784 skb_reset_mac_header(xmit_skb);
785 wil_dbg_txrx(wil, "Rx -> Tx %d bytes\n", len);
786 dev_queue_xmit(xmit_skb);
787 }
788
789 if (skb) { /* deliver to local stack */
790
791 skb->protocol = eth_type_trans(skb, ndev);
792 rc = napi_gro_receive(&wil->napi_rx, skb);
793 wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
794 len, gro_res_str[rc]);
795 }
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200796stats:
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200797 /* statistics. rc set to GRO_NORMAL for AP bridging */
Vladimir Kondratievb5998e62014-03-17 15:34:22 +0200798 if (unlikely(rc == GRO_DROP)) {
799 ndev->stats.rx_dropped++;
800 stats->rx_dropped++;
801 wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
802 } else {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800803 ndev->stats.rx_packets++;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200804 stats->rx_packets++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800805 ndev->stats.rx_bytes += len;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200806 stats->rx_bytes += len;
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200807 if (mcast)
808 ndev->stats.multicast++;
Vladimir Kondratiev194b4822014-06-16 19:37:14 +0300809 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800810}
811
812/**
813 * Proceed all completed skb's from Rx VRING
814 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300815 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800816 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300817void wil_rx_handle(struct wil6210_priv *wil, int *quota)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800818{
819 struct net_device *ndev = wil_to_ndev(wil);
820 struct vring *v = &wil->vring_rx;
821 struct sk_buff *skb;
822
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200823 if (unlikely(!v->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800824 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
825 return;
826 }
Lazar Alexei3190cc62017-02-23 14:34:14 +0200827 wil_dbg_txrx(wil, "rx_handle\n");
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300828 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
829 (*quota)--;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800830
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800831 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
832 skb->dev = ndev;
833 skb_reset_mac_header(skb);
834 skb->ip_summed = CHECKSUM_UNNECESSARY;
835 skb->pkt_type = PACKET_OTHERHOST;
836 skb->protocol = htons(ETH_P_802_2);
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200837 wil_netif_rx_any(skb, ndev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800838 } else {
Vladimir Kondratieve4373d82014-12-23 09:47:21 +0200839 wil_rx_reorder(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800840 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800841 }
842 wil_rx_refill(wil, v->size);
843}
844
Lior David2e9a3212017-05-04 22:43:18 +0300845static void wil_rx_buf_len_init(struct wil6210_priv *wil)
846{
847 wil->rx_buf_len = rx_large_buf ?
848 WIL_MAX_ETH_MTU : TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
849 if (mtu_max > wil->rx_buf_len) {
850 /* do not allow RX buffers to be smaller than mtu_max, for
851 * backward compatibility (mtu_max parameter was also used
852 * to support receiving large packets)
853 */
854 wil_info(wil, "Override RX buffer to mtu_max(%d)\n", mtu_max);
855 wil->rx_buf_len = mtu_max;
856 }
857}
858
Vladimir Kondratievd3762b42014-12-01 15:35:02 +0200859int wil_rx_init(struct wil6210_priv *wil, u16 size)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800860{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800861 struct vring *vring = &wil->vring_rx;
862 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800863
Lazar Alexei3190cc62017-02-23 14:34:14 +0200864 wil_dbg_misc(wil, "rx_init\n");
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300865
Vladimir Kondratiev8bf6adb2014-03-17 15:34:17 +0200866 if (vring->va) {
867 wil_err(wil, "Rx ring already allocated\n");
868 return -EINVAL;
869 }
870
Lior David2e9a3212017-05-04 22:43:18 +0300871 wil_rx_buf_len_init(wil);
872
Vladimir Kondratievd3762b42014-12-01 15:35:02 +0200873 vring->size = size;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800874 rc = wil_vring_alloc(wil, vring);
875 if (rc)
876 return rc;
877
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +0200878 rc = wmi_rx_chain_add(wil, vring);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800879 if (rc)
880 goto err_free;
881
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800882 rc = wil_rx_refill(wil, vring->size);
883 if (rc)
884 goto err_free;
885
886 return 0;
887 err_free:
888 wil_vring_free(wil, vring, 0);
889
890 return rc;
891}
892
893void wil_rx_fini(struct wil6210_priv *wil)
894{
895 struct vring *vring = &wil->vring_rx;
896
Lazar Alexei3190cc62017-02-23 14:34:14 +0200897 wil_dbg_misc(wil, "rx_fini\n");
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300898
Vladimir Kondratiev2acb4222013-01-28 18:31:08 +0200899 if (vring->va)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800900 wil_vring_free(wil, vring, 0);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800901}
902
Maya Erez875e9432016-01-28 19:24:02 +0200903static inline void wil_tx_data_init(struct vring_tx_data *txdata)
904{
905 spin_lock_bh(&txdata->lock);
906 txdata->dot1x_open = 0;
907 txdata->enabled = 0;
908 txdata->idle = 0;
909 txdata->last_idle = 0;
910 txdata->begin = 0;
911 txdata->agg_wsize = 0;
912 txdata->agg_timeout = 0;
913 txdata->agg_amsdu = 0;
914 txdata->addba_in_progress = false;
915 spin_unlock_bh(&txdata->lock);
916}
917
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800918int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
919 int cid, int tid)
920{
921 int rc;
922 struct wmi_vring_cfg_cmd cmd = {
923 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
924 .vring_cfg = {
925 .tx_sw_ring = {
Vladimir Kondratiev9a06bec2014-10-28 16:51:27 +0200926 .max_mpdu_size =
Vladimir Kondratievc44690a2014-12-23 09:47:11 +0200927 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
Vladimir Kondratievb5d98e92013-04-18 14:33:51 +0300928 .ring_size = cpu_to_le16(size),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800929 },
930 .ringid = id,
Vladimir Kondratieva70abea2014-03-17 15:34:05 +0200931 .cidxtid = mk_cidxtid(cid, tid),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800932 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
933 .mac_ctrl = 0,
934 .to_resolution = 0,
Vladimir Kondratiev32772132014-12-23 09:47:03 +0200935 .agg_max_wsize = 0,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800936 .schd_params = {
937 .priority = cpu_to_le16(0),
938 .timeslot_us = cpu_to_le16(0xfff),
939 },
940 },
941 };
942 struct {
Lior Davidb874dde2016-03-01 19:18:09 +0200943 struct wmi_cmd_hdr wmi;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800944 struct wmi_vring_cfg_done_event cmd;
945 } __packed reply;
946 struct vring *vring = &wil->vring_tx[id];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200947 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800948
Lazar Alexei3190cc62017-02-23 14:34:14 +0200949 wil_dbg_misc(wil, "vring_init_tx: max_mpdu_size %d\n",
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +0300950 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +0200951 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300952
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800953 if (vring->va) {
954 wil_err(wil, "Tx ring [%d] already allocated\n", id);
955 rc = -EINVAL;
956 goto out;
957 }
958
Maya Erez875e9432016-01-28 19:24:02 +0200959 wil_tx_data_init(txdata);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800960 vring->size = size;
961 rc = wil_vring_alloc(wil, vring);
962 if (rc)
963 goto out;
964
Vladimir Kondratiev93ae6d42014-02-27 16:20:51 +0200965 wil->vring2cid_tid[id][0] = cid;
966 wil->vring2cid_tid[id][1] = tid;
967
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800968 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800969
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300970 if (!wil->privacy)
971 txdata->dot1x_open = true;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800972 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
973 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
974 if (rc)
975 goto out_free;
976
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200977 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800978 wil_err(wil, "Tx config failed, status 0x%02x\n",
979 reply.cmd.status);
Vladimir Kondratievc3319972013-01-28 18:31:09 +0200980 rc = -EINVAL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800981 goto out_free;
982 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800983
Maya Erezdc905062016-08-18 16:52:15 +0300984 spin_lock_bh(&txdata->lock);
985 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200986 txdata->enabled = 1;
Maya Erezdc905062016-08-18 16:52:15 +0300987 spin_unlock_bh(&txdata->lock);
988
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300989 if (txdata->dot1x_open && (agg_wsize >= 0))
Vladimir Kondratiev3a3def82014-12-23 09:47:05 +0200990 wil_addba_tx_request(wil, id, agg_wsize);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200991
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800992 return 0;
993 out_free:
Maya Erez875e9432016-01-28 19:24:02 +0200994 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300995 txdata->dot1x_open = false;
996 txdata->enabled = 0;
Maya Erez875e9432016-01-28 19:24:02 +0200997 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800998 wil_vring_free(wil, vring, 1);
Maya Erez0916d9f2016-01-17 12:39:10 +0200999 wil->vring2cid_tid[id][0] = WIL6210_MAX_CID;
1000 wil->vring2cid_tid[id][1] = 0;
1001
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001002 out:
1003
1004 return rc;
1005}
1006
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001007int wil_vring_init_bcast(struct wil6210_priv *wil, int id, int size)
1008{
1009 int rc;
1010 struct wmi_bcast_vring_cfg_cmd cmd = {
1011 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
1012 .vring_cfg = {
1013 .tx_sw_ring = {
1014 .max_mpdu_size =
1015 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
1016 .ring_size = cpu_to_le16(size),
1017 },
1018 .ringid = id,
1019 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
1020 },
1021 };
1022 struct {
Lior Davidb874dde2016-03-01 19:18:09 +02001023 struct wmi_cmd_hdr wmi;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001024 struct wmi_vring_cfg_done_event cmd;
1025 } __packed reply;
1026 struct vring *vring = &wil->vring_tx[id];
1027 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
1028
Lazar Alexei3190cc62017-02-23 14:34:14 +02001029 wil_dbg_misc(wil, "vring_init_bcast: max_mpdu_size %d\n",
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001030 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +02001031 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001032
1033 if (vring->va) {
1034 wil_err(wil, "Tx ring [%d] already allocated\n", id);
1035 rc = -EINVAL;
1036 goto out;
1037 }
1038
Maya Erez875e9432016-01-28 19:24:02 +02001039 wil_tx_data_init(txdata);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001040 vring->size = size;
1041 rc = wil_vring_alloc(wil, vring);
1042 if (rc)
1043 goto out;
1044
1045 wil->vring2cid_tid[id][0] = WIL6210_MAX_CID; /* CID */
1046 wil->vring2cid_tid[id][1] = 0; /* TID */
1047
1048 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
1049
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001050 if (!wil->privacy)
1051 txdata->dot1x_open = true;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001052 rc = wmi_call(wil, WMI_BCAST_VRING_CFG_CMDID, &cmd, sizeof(cmd),
1053 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
1054 if (rc)
1055 goto out_free;
1056
1057 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
1058 wil_err(wil, "Tx config failed, status 0x%02x\n",
1059 reply.cmd.status);
1060 rc = -EINVAL;
1061 goto out_free;
1062 }
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001063
Maya Erezdc905062016-08-18 16:52:15 +03001064 spin_lock_bh(&txdata->lock);
1065 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001066 txdata->enabled = 1;
Maya Erezdc905062016-08-18 16:52:15 +03001067 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001068
1069 return 0;
1070 out_free:
Maya Erez875e9432016-01-28 19:24:02 +02001071 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001072 txdata->enabled = 0;
1073 txdata->dot1x_open = false;
Maya Erez875e9432016-01-28 19:24:02 +02001074 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001075 wil_vring_free(wil, vring, 1);
1076 out:
1077
1078 return rc;
1079}
1080
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001081void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
1082{
1083 struct vring *vring = &wil->vring_tx[id];
Vladimir Kondratiev3a124ed2014-12-23 09:47:04 +02001084 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001085
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +02001086 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001087
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001088 if (!vring->va)
1089 return;
1090
Lazar Alexei3190cc62017-02-23 14:34:14 +02001091 wil_dbg_misc(wil, "vring_fini_tx: id=%d\n", id);
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +03001092
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001093 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001094 txdata->dot1x_open = false;
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001095 txdata->enabled = 0; /* no Tx can be in progress or start anew */
1096 spin_unlock_bh(&txdata->lock);
Maya Erez34b88862016-05-16 22:23:32 +03001097 /* napi_synchronize waits for completion of the current NAPI but will
1098 * not prevent the next NAPI run.
1099 * Add a memory barrier to guarantee that txdata->enabled is zeroed
1100 * before napi_synchronize so that the next scheduled NAPI will not
1101 * handle this vring
1102 */
1103 wmb();
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001104 /* make sure NAPI won't touch this vring */
Vladimir Kondratiev9419b6a2014-12-23 09:47:14 +02001105 if (test_bit(wil_status_napi_en, wil->status))
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001106 napi_synchronize(&wil->napi_tx);
1107
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001108 wil_vring_free(wil, vring, 1);
1109}
1110
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001111static struct vring *wil_find_tx_ucast(struct wil6210_priv *wil,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001112 struct sk_buff *skb)
1113{
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001114 int i;
1115 struct ethhdr *eth = (void *)skb->data;
1116 int cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001117
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001118 if (cid < 0)
1119 return NULL;
1120
1121 /* TODO: fix for multiple TID */
1122 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001123 if (!wil->vring_tx_data[i].dot1x_open &&
1124 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1125 continue;
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001126 if (wil->vring2cid_tid[i][0] == cid) {
1127 struct vring *v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001128 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001129
Lazar Alexei3190cc62017-02-23 14:34:14 +02001130 wil_dbg_txrx(wil, "find_tx_ucast: (%pM) -> [%d]\n",
1131 eth->h_dest, i);
Maya Erezb729aaf2016-01-17 12:39:09 +02001132 if (v->va && txdata->enabled) {
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001133 return v;
1134 } else {
Lazar Alexei3190cc62017-02-23 14:34:14 +02001135 wil_dbg_txrx(wil,
1136 "find_tx_ucast: vring[%d] not valid\n",
1137 i);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001138 return NULL;
1139 }
1140 }
1141 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001142
1143 return NULL;
1144}
1145
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001146static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1147 struct sk_buff *skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001148
1149static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
1150 struct sk_buff *skb)
1151{
1152 struct vring *v;
1153 int i;
1154 u8 cid;
Maya Erezb729aaf2016-01-17 12:39:09 +02001155 struct vring_tx_data *txdata;
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001156
1157 /* In the STA mode, it is expected to have only 1 VRING
1158 * for the AP we connected to.
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001159 * find 1-st vring eligible for this skb and use it.
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001160 */
1161 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1162 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001163 txdata = &wil->vring_tx_data[i];
1164 if (!v->va || !txdata->enabled)
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001165 continue;
1166
1167 cid = wil->vring2cid_tid[i][0];
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001168 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1169 continue;
1170
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001171 if (!wil->vring_tx_data[i].dot1x_open &&
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001172 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001173 continue;
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001174
1175 wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
1176
1177 return v;
1178 }
1179
1180 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1181
1182 return NULL;
1183}
1184
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001185/* Use one of 2 strategies:
1186 *
1187 * 1. New (real broadcast):
1188 * use dedicated broadcast vring
1189 * 2. Old (pseudo-DMS):
1190 * Find 1-st vring and return it;
1191 * duplicate skb and send it to other active vrings;
1192 * in all cases override dest address to unicast peer's address
1193 * Use old strategy when new is not supported yet:
1194 * - for PBSS
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001195 */
1196static struct vring *wil_find_tx_bcast_1(struct wil6210_priv *wil,
1197 struct sk_buff *skb)
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001198{
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001199 struct vring *v;
Maya Erezb729aaf2016-01-17 12:39:09 +02001200 struct vring_tx_data *txdata;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001201 int i = wil->bcast_vring;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001202
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001203 if (i < 0)
1204 return NULL;
1205 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001206 txdata = &wil->vring_tx_data[i];
1207 if (!v->va || !txdata->enabled)
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001208 return NULL;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001209 if (!wil->vring_tx_data[i].dot1x_open &&
1210 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1211 return NULL;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001212
1213 return v;
1214}
1215
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001216static void wil_set_da_for_vring(struct wil6210_priv *wil,
1217 struct sk_buff *skb, int vring_index)
1218{
1219 struct ethhdr *eth = (void *)skb->data;
1220 int cid = wil->vring2cid_tid[vring_index][0];
1221
1222 ether_addr_copy(eth->h_dest, wil->sta[cid].addr);
1223}
1224
1225static struct vring *wil_find_tx_bcast_2(struct wil6210_priv *wil,
1226 struct sk_buff *skb)
1227{
1228 struct vring *v, *v2;
1229 struct sk_buff *skb2;
1230 int i;
1231 u8 cid;
1232 struct ethhdr *eth = (void *)skb->data;
1233 char *src = eth->h_source;
Maya Erezb729aaf2016-01-17 12:39:09 +02001234 struct vring_tx_data *txdata;
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001235
1236 /* find 1-st vring eligible for data */
1237 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1238 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001239 txdata = &wil->vring_tx_data[i];
1240 if (!v->va || !txdata->enabled)
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001241 continue;
1242
1243 cid = wil->vring2cid_tid[i][0];
1244 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1245 continue;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001246 if (!wil->vring_tx_data[i].dot1x_open &&
1247 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001248 continue;
1249
1250 /* don't Tx back to source when re-routing Rx->Tx at the AP */
1251 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1252 continue;
1253
1254 goto found;
1255 }
1256
1257 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1258
1259 return NULL;
1260
1261found:
1262 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
1263 wil_set_da_for_vring(wil, skb, i);
1264
1265 /* find other active vrings and duplicate skb for each */
1266 for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
1267 v2 = &wil->vring_tx[i];
1268 if (!v2->va)
1269 continue;
1270 cid = wil->vring2cid_tid[i][0];
1271 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1272 continue;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001273 if (!wil->vring_tx_data[i].dot1x_open &&
1274 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001275 continue;
1276
1277 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1278 continue;
1279
1280 skb2 = skb_copy(skb, GFP_ATOMIC);
1281 if (skb2) {
1282 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
1283 wil_set_da_for_vring(wil, skb2, i);
1284 wil_tx_vring(wil, v2, skb2);
1285 } else {
1286 wil_err(wil, "skb_copy failed\n");
1287 }
1288 }
1289
1290 return v;
1291}
1292
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001293static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
1294 int vring_index)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001295{
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001296 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001297 d->dma.ip_length = 0;
1298 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
1299 d->dma.b11 = 0/*14 | BIT(7)*/;
1300 d->dma.error = 0;
1301 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001302 d->dma.length = cpu_to_le16((u16)len);
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001303 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001304 d->mac.d[0] = 0;
1305 d->mac.d[1] = 0;
1306 d->mac.d[2] = 0;
1307 d->mac.ucode_cmd = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001308 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
1309 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
1310 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
1311
1312 return 0;
1313}
1314
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001315static inline
1316void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
1317{
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001318 d->mac.d[2] |= (nr_frags << MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001319}
1320
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001321/**
1322 * Sets the descriptor @d up for csum and/or TSO offloading. The corresponding
1323 * @skb is used to obtain the protocol and headers length.
1324 * @tso_desc_type is a descriptor type for TSO: 0 - a header, 1 - first data,
1325 * 2 - middle, 3 - last descriptor.
1326 */
1327
1328static void wil_tx_desc_offload_setup_tso(struct vring_tx_desc *d,
1329 struct sk_buff *skb,
1330 int tso_desc_type, bool is_ipv4,
1331 int tcp_hdr_len, int skb_net_hdr_len)
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001332{
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001333 d->dma.b11 = ETH_HLEN; /* MAC header length */
1334 d->dma.b11 |= is_ipv4 << DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS;
1335
1336 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1337 /* L4 header len: TCP header length */
1338 d->dma.d0 |= (tcp_hdr_len & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1339
1340 /* Setup TSO: bit and desc type */
1341 d->dma.d0 |= (BIT(DMA_CFG_DESC_TX_0_TCP_SEG_EN_POS)) |
1342 (tso_desc_type << DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS);
1343 d->dma.d0 |= (is_ipv4 << DMA_CFG_DESC_TX_0_IPV4_CHECKSUM_EN_POS);
1344
1345 d->dma.ip_length = skb_net_hdr_len;
1346 /* Enable TCP/UDP checksum */
1347 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1348 /* Calculate pseudo-header */
1349 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1350}
1351
1352/**
1353 * Sets the descriptor @d up for csum. The corresponding
1354 * @skb is used to obtain the protocol and headers length.
1355 * Returns the protocol: 0 - not TCP, 1 - TCPv4, 2 - TCPv6.
1356 * Note, if d==NULL, the function only returns the protocol result.
1357 *
1358 * It is very similar to previous wil_tx_desc_offload_setup_tso. This
1359 * is "if unrolling" to optimize the critical path.
1360 */
1361
1362static int wil_tx_desc_offload_setup(struct vring_tx_desc *d,
1363 struct sk_buff *skb){
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001364 int protocol;
1365
1366 if (skb->ip_summed != CHECKSUM_PARTIAL)
1367 return 0;
1368
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +02001369 d->dma.b11 = ETH_HLEN; /* MAC header length */
1370
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001371 switch (skb->protocol) {
1372 case cpu_to_be16(ETH_P_IP):
1373 protocol = ip_hdr(skb)->protocol;
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +02001374 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001375 break;
1376 case cpu_to_be16(ETH_P_IPV6):
1377 protocol = ipv6_hdr(skb)->nexthdr;
1378 break;
1379 default:
1380 return -EINVAL;
1381 }
1382
1383 switch (protocol) {
1384 case IPPROTO_TCP:
1385 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1386 /* L4 header len: TCP header length */
1387 d->dma.d0 |=
1388 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1389 break;
1390 case IPPROTO_UDP:
1391 /* L4 header len: UDP header length */
1392 d->dma.d0 |=
1393 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1394 break;
1395 default:
1396 return -EINVAL;
1397 }
1398
1399 d->dma.ip_length = skb_network_header_len(skb);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001400 /* Enable TCP/UDP checksum */
1401 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1402 /* Calculate pseudo-header */
1403 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1404
1405 return 0;
1406}
1407
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001408static inline void wil_tx_last_desc(struct vring_tx_desc *d)
1409{
1410 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS) |
1411 BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS) |
1412 BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
1413}
1414
1415static inline void wil_set_tx_desc_last_tso(volatile struct vring_tx_desc *d)
1416{
1417 d->dma.d0 |= wil_tso_type_lst <<
1418 DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS;
1419}
1420
1421static int __wil_tx_vring_tso(struct wil6210_priv *wil, struct vring *vring,
1422 struct sk_buff *skb)
1423{
1424 struct device *dev = wil_to_dev(wil);
1425
1426 /* point to descriptors in shared memory */
1427 volatile struct vring_tx_desc *_desc = NULL, *_hdr_desc,
1428 *_first_desc = NULL;
1429
1430 /* pointers to shadow descriptors */
1431 struct vring_tx_desc desc_mem, hdr_desc_mem, first_desc_mem,
1432 *d = &hdr_desc_mem, *hdr_desc = &hdr_desc_mem,
1433 *first_desc = &first_desc_mem;
1434
1435 /* pointer to shadow descriptors' context */
1436 struct wil_ctx *hdr_ctx, *first_ctx = NULL;
1437
1438 int descs_used = 0; /* total number of used descriptors */
1439 int sg_desc_cnt = 0; /* number of descriptors for current mss*/
1440
1441 u32 swhead = vring->swhead;
1442 int used, avail = wil_vring_avail_tx(vring);
1443 int nr_frags = skb_shinfo(skb)->nr_frags;
1444 int min_desc_required = nr_frags + 1;
1445 int mss = skb_shinfo(skb)->gso_size; /* payload size w/o headers */
1446 int f, len, hdrlen, headlen;
1447 int vring_index = vring - wil->vring_tx;
1448 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1449 uint i = swhead;
1450 dma_addr_t pa;
1451 const skb_frag_t *frag = NULL;
1452 int rem_data = mss;
1453 int lenmss;
1454 int hdr_compensation_need = true;
1455 int desc_tso_type = wil_tso_type_first;
1456 bool is_ipv4;
1457 int tcp_hdr_len;
1458 int skb_net_hdr_len;
1459 int gso_type;
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001460 int rc = -EINVAL;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001461
Lazar Alexei3190cc62017-02-23 14:34:14 +02001462 wil_dbg_txrx(wil, "tx_vring_tso: %d bytes to vring %d\n", skb->len,
1463 vring_index);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001464
1465 if (unlikely(!txdata->enabled))
1466 return -EINVAL;
1467
1468 /* A typical page 4K is 3-4 payloads, we assume each fragment
1469 * is a full payload, that's how min_desc_required has been
1470 * calculated. In real we might need more or less descriptors,
1471 * this is the initial check only.
1472 */
1473 if (unlikely(avail < min_desc_required)) {
1474 wil_err_ratelimited(wil,
1475 "TSO: Tx ring[%2d] full. No space for %d fragments\n",
1476 vring_index, min_desc_required);
1477 return -ENOMEM;
1478 }
1479
1480 /* Header Length = MAC header len + IP header len + TCP header len*/
1481 hdrlen = ETH_HLEN +
1482 (int)skb_network_header_len(skb) +
1483 tcp_hdrlen(skb);
1484
1485 gso_type = skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4);
1486 switch (gso_type) {
1487 case SKB_GSO_TCPV4:
1488 /* TCP v4, zero out the IP length and IPv4 checksum fields
1489 * as required by the offloading doc
1490 */
1491 ip_hdr(skb)->tot_len = 0;
1492 ip_hdr(skb)->check = 0;
1493 is_ipv4 = true;
1494 break;
1495 case SKB_GSO_TCPV6:
1496 /* TCP v6, zero out the payload length */
1497 ipv6_hdr(skb)->payload_len = 0;
1498 is_ipv4 = false;
1499 break;
1500 default:
1501 /* other than TCPv4 or TCPv6 types are not supported for TSO.
1502 * It is also illegal for both to be set simultaneously
1503 */
1504 return -EINVAL;
1505 }
1506
1507 if (skb->ip_summed != CHECKSUM_PARTIAL)
1508 return -EINVAL;
1509
1510 /* tcp header length and skb network header length are fixed for all
1511 * packet's descriptors - read then once here
1512 */
1513 tcp_hdr_len = tcp_hdrlen(skb);
1514 skb_net_hdr_len = skb_network_header_len(skb);
1515
1516 _hdr_desc = &vring->va[i].tx;
1517
1518 pa = dma_map_single(dev, skb->data, hdrlen, DMA_TO_DEVICE);
1519 if (unlikely(dma_mapping_error(dev, pa))) {
1520 wil_err(wil, "TSO: Skb head DMA map error\n");
1521 goto err_exit;
1522 }
1523
1524 wil_tx_desc_map(hdr_desc, pa, hdrlen, vring_index);
1525 wil_tx_desc_offload_setup_tso(hdr_desc, skb, wil_tso_type_hdr, is_ipv4,
1526 tcp_hdr_len, skb_net_hdr_len);
1527 wil_tx_last_desc(hdr_desc);
1528
1529 vring->ctx[i].mapped_as = wil_mapped_as_single;
1530 hdr_ctx = &vring->ctx[i];
1531
1532 descs_used++;
1533 headlen = skb_headlen(skb) - hdrlen;
1534
1535 for (f = headlen ? -1 : 0; f < nr_frags; f++) {
1536 if (headlen) {
1537 len = headlen;
1538 wil_dbg_txrx(wil, "TSO: process skb head, len %u\n",
1539 len);
1540 } else {
1541 frag = &skb_shinfo(skb)->frags[f];
1542 len = frag->size;
1543 wil_dbg_txrx(wil, "TSO: frag[%d]: len %u\n", f, len);
1544 }
1545
1546 while (len) {
1547 wil_dbg_txrx(wil,
1548 "TSO: len %d, rem_data %d, descs_used %d\n",
1549 len, rem_data, descs_used);
1550
1551 if (descs_used == avail) {
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001552 wil_err_ratelimited(wil, "TSO: ring overflow\n");
1553 rc = -ENOMEM;
1554 goto mem_error;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001555 }
1556
1557 lenmss = min_t(int, rem_data, len);
1558 i = (swhead + descs_used) % vring->size;
1559 wil_dbg_txrx(wil, "TSO: lenmss %d, i %d\n", lenmss, i);
1560
1561 if (!headlen) {
1562 pa = skb_frag_dma_map(dev, frag,
1563 frag->size - len, lenmss,
1564 DMA_TO_DEVICE);
1565 vring->ctx[i].mapped_as = wil_mapped_as_page;
1566 } else {
1567 pa = dma_map_single(dev,
1568 skb->data +
1569 skb_headlen(skb) - headlen,
1570 lenmss,
1571 DMA_TO_DEVICE);
1572 vring->ctx[i].mapped_as = wil_mapped_as_single;
1573 headlen -= lenmss;
1574 }
1575
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001576 if (unlikely(dma_mapping_error(dev, pa))) {
1577 wil_err(wil, "TSO: DMA map page error\n");
1578 goto mem_error;
1579 }
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001580
1581 _desc = &vring->va[i].tx;
1582
1583 if (!_first_desc) {
1584 _first_desc = _desc;
1585 first_ctx = &vring->ctx[i];
1586 d = first_desc;
1587 } else {
1588 d = &desc_mem;
1589 }
1590
1591 wil_tx_desc_map(d, pa, lenmss, vring_index);
1592 wil_tx_desc_offload_setup_tso(d, skb, desc_tso_type,
1593 is_ipv4, tcp_hdr_len,
1594 skb_net_hdr_len);
1595
1596 /* use tso_type_first only once */
1597 desc_tso_type = wil_tso_type_mid;
1598
1599 descs_used++; /* desc used so far */
1600 sg_desc_cnt++; /* desc used for this segment */
1601 len -= lenmss;
1602 rem_data -= lenmss;
1603
1604 wil_dbg_txrx(wil,
1605 "TSO: len %d, rem_data %d, descs_used %d, sg_desc_cnt %d,\n",
1606 len, rem_data, descs_used, sg_desc_cnt);
1607
1608 /* Close the segment if reached mss size or last frag*/
1609 if (rem_data == 0 || (f == nr_frags - 1 && len == 0)) {
1610 if (hdr_compensation_need) {
1611 /* first segment include hdr desc for
1612 * release
1613 */
1614 hdr_ctx->nr_frags = sg_desc_cnt;
1615 wil_tx_desc_set_nr_frags(first_desc,
1616 sg_desc_cnt +
1617 1);
1618 hdr_compensation_need = false;
1619 } else {
1620 wil_tx_desc_set_nr_frags(first_desc,
1621 sg_desc_cnt);
1622 }
1623 first_ctx->nr_frags = sg_desc_cnt - 1;
1624
1625 wil_tx_last_desc(d);
1626
1627 /* first descriptor may also be the last
1628 * for this mss - make sure not to copy
1629 * it twice
1630 */
1631 if (first_desc != d)
1632 *_first_desc = *first_desc;
1633
1634 /*last descriptor will be copied at the end
1635 * of this TS processing
1636 */
1637 if (f < nr_frags - 1 || len > 0)
1638 *_desc = *d;
1639
1640 rem_data = mss;
1641 _first_desc = NULL;
1642 sg_desc_cnt = 0;
1643 } else if (first_desc != d) /* update mid descriptor */
1644 *_desc = *d;
1645 }
1646 }
1647
1648 /* first descriptor may also be the last.
1649 * in this case d pointer is invalid
1650 */
1651 if (_first_desc == _desc)
1652 d = first_desc;
1653
1654 /* Last data descriptor */
1655 wil_set_tx_desc_last_tso(d);
1656 *_desc = *d;
1657
1658 /* Fill the total number of descriptors in first desc (hdr)*/
1659 wil_tx_desc_set_nr_frags(hdr_desc, descs_used);
1660 *_hdr_desc = *hdr_desc;
1661
1662 /* hold reference to skb
1663 * to prevent skb release before accounting
1664 * in case of immediate "tx done"
1665 */
1666 vring->ctx[i].skb = skb_get(skb);
1667
1668 /* performance monitoring */
1669 used = wil_vring_used_tx(vring);
Gidon Studinski1c9a1932017-08-21 22:55:15 +03001670 if (wil_val_in_range(wil->vring_idle_trsh,
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001671 used, used + descs_used)) {
1672 txdata->idle += get_cycles() - txdata->last_idle;
1673 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1674 vring_index, used, used + descs_used);
1675 }
1676
Maya Erezeb26cff2016-05-16 22:23:30 +03001677 /* Make sure to advance the head only after descriptor update is done.
1678 * This will prevent a race condition where the completion thread
1679 * will see the DU bit set from previous run and will handle the
1680 * skb before it was completed.
1681 */
1682 wmb();
1683
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001684 /* advance swhead */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001685 wil_vring_advance_head(vring, descs_used);
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001686 wil_dbg_txrx(wil, "TSO: Tx swhead %d -> %d\n", swhead, vring->swhead);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001687
1688 /* make sure all writes to descriptors (shared memory) are done before
1689 * committing them to HW
1690 */
1691 wmb();
1692
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +03001693 wil_w(wil, vring->hwtail, vring->swhead);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001694 return 0;
1695
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001696mem_error:
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001697 while (descs_used > 0) {
1698 struct wil_ctx *ctx;
1699
Maya Ereza1526f72016-05-16 22:23:33 +03001700 i = (swhead + descs_used - 1) % vring->size;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001701 d = (struct vring_tx_desc *)&vring->va[i].tx;
1702 _desc = &vring->va[i].tx;
1703 *d = *_desc;
1704 _desc->dma.status = TX_DMA_STATUS_DU;
1705 ctx = &vring->ctx[i];
1706 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001707 memset(ctx, 0, sizeof(*ctx));
1708 descs_used--;
1709 }
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001710err_exit:
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001711 return rc;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001712}
1713
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001714static int __wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1715 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001716{
1717 struct device *dev = wil_to_dev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001718 struct vring_tx_desc dd, *d = &dd;
1719 volatile struct vring_tx_desc *_d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001720 u32 swhead = vring->swhead;
1721 int avail = wil_vring_avail_tx(vring);
1722 int nr_frags = skb_shinfo(skb)->nr_frags;
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001723 uint f = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001724 int vring_index = vring - wil->vring_tx;
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001725 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001726 uint i = swhead;
1727 dma_addr_t pa;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001728 int used;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001729 bool mcast = (vring_index == wil->bcast_vring);
1730 uint len = skb_headlen(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001731
Lazar Alexei3190cc62017-02-23 14:34:14 +02001732 wil_dbg_txrx(wil, "tx_vring: %d bytes to vring %d\n", skb->len,
1733 vring_index);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001734
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001735 if (unlikely(!txdata->enabled))
1736 return -EINVAL;
1737
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001738 if (unlikely(avail < 1 + nr_frags)) {
Vladimir Kondratiev70801e12014-12-01 15:36:03 +02001739 wil_err_ratelimited(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001740 "Tx ring[%2d] full. No space for %d fragments\n",
1741 vring_index, 1 + nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001742 return -ENOMEM;
1743 }
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001744 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001745
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001746 pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001747
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001748 wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index,
1749 skb_headlen(skb), skb->data, &pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001750 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001751 skb->data, skb_headlen(skb), false);
1752
1753 if (unlikely(dma_mapping_error(dev, pa)))
1754 return -EINVAL;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001755 vring->ctx[i].mapped_as = wil_mapped_as_single;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001756 /* 1-st segment */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001757 wil_tx_desc_map(d, pa, len, vring_index);
1758 if (unlikely(mcast)) {
1759 d->mac.d[0] |= BIT(MAC_CFG_DESC_TX_0_MCS_EN_POS); /* MCS 0 */
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001760 if (unlikely(len > WIL_BCAST_MCS0_LIMIT)) /* set MCS 1 */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001761 d->mac.d[0] |= (1 << MAC_CFG_DESC_TX_0_MCS_INDEX_POS);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001762 }
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001763 /* Process TCP/UDP checksum offloading */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001764 if (unlikely(wil_tx_desc_offload_setup(d, skb))) {
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001765 wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001766 vring_index);
1767 goto dma_error;
1768 }
1769
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001770 vring->ctx[i].nr_frags = nr_frags;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001771 wil_tx_desc_set_nr_frags(d, nr_frags + 1);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001772
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001773 /* middle segments */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001774 for (; f < nr_frags; f++) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001775 const struct skb_frag_struct *frag =
1776 &skb_shinfo(skb)->frags[f];
1777 int len = skb_frag_size(frag);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001778
Vladimir Kondratieve59d16c2015-02-01 10:55:12 +02001779 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001780 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1781 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1782 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001783 i = (swhead + f + 1) % vring->size;
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001784 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001785 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001786 DMA_TO_DEVICE);
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001787 if (unlikely(dma_mapping_error(dev, pa))) {
1788 wil_err(wil, "Tx[%2d] failed to map fragment\n",
1789 vring_index);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001790 goto dma_error;
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001791 }
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001792 vring->ctx[i].mapped_as = wil_mapped_as_page;
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001793 wil_tx_desc_map(d, pa, len, vring_index);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001794 /* no need to check return code -
1795 * if it succeeded for 1-st descriptor,
1796 * it will succeed here too
1797 */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001798 wil_tx_desc_offload_setup(d, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001799 }
1800 /* for the last seg only */
1801 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
Kirshenbaum Erez668b2bb2013-06-23 12:59:35 +03001802 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001803 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001804 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001805 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1806 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1807 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001808
Vladimir Kondratiev6cdadd42013-07-11 18:03:41 +03001809 /* hold reference to skb
1810 * to prevent skb release before accounting
1811 * in case of immediate "tx done"
1812 */
1813 vring->ctx[i].skb = skb_get(skb);
1814
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001815 /* performance monitoring */
1816 used = wil_vring_used_tx(vring);
Gidon Studinski1c9a1932017-08-21 22:55:15 +03001817 if (wil_val_in_range(wil->vring_idle_trsh,
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001818 used, used + nr_frags + 1)) {
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001819 txdata->idle += get_cycles() - txdata->last_idle;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001820 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1821 vring_index, used, used + nr_frags + 1);
1822 }
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001823
Maya Erezeb26cff2016-05-16 22:23:30 +03001824 /* Make sure to advance the head only after descriptor update is done.
1825 * This will prevent a race condition where the completion thread
1826 * will see the DU bit set from previous run and will handle the
1827 * skb before it was completed.
1828 */
1829 wmb();
1830
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001831 /* advance swhead */
1832 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001833 wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead,
1834 vring->swhead);
Vladimir Kondratiev98658092013-05-12 14:43:35 +03001835 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001836
1837 /* make sure all writes to descriptors (shared memory) are done before
1838 * committing them to HW
1839 */
1840 wmb();
1841
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +03001842 wil_w(wil, vring->hwtail, vring->swhead);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001843
1844 return 0;
1845 dma_error:
1846 /* unmap what we have mapped */
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001847 nr_frags = f + 1; /* frags mapped + one for skb head */
1848 for (f = 0; f < nr_frags; f++) {
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001849 struct wil_ctx *ctx;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001850
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001851 i = (swhead + f) % vring->size;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001852 ctx = &vring->ctx[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001853 _d = &vring->va[i].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001854 *d = *_d;
1855 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001856 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001857
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001858 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001859 }
1860
1861 return -EINVAL;
1862}
1863
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001864static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1865 struct sk_buff *skb)
1866{
1867 int vring_index = vring - wil->vring_tx;
1868 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1869 int rc;
1870
1871 spin_lock(&txdata->lock);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001872
Maya Ereze3309bf2017-05-15 16:57:46 +03001873 if (test_bit(wil_status_suspending, wil->status) ||
1874 test_bit(wil_status_suspended, wil->status) ||
1875 test_bit(wil_status_resuming, wil->status)) {
1876 wil_dbg_txrx(wil,
1877 "suspend/resume in progress. drop packet\n");
1878 spin_unlock(&txdata->lock);
1879 return -EINVAL;
1880 }
1881
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001882 rc = (skb_is_gso(skb) ? __wil_tx_vring_tso : __wil_tx_vring)
1883 (wil, vring, skb);
1884
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001885 spin_unlock(&txdata->lock);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001886
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001887 return rc;
1888}
1889
Dedy Lansky970a98f2016-12-06 08:26:49 +02001890/**
1891 * Check status of tx vrings and stop/wake net queues if needed
1892 *
1893 * This function does one of two checks:
1894 * In case check_stop is true, will check if net queues need to be stopped. If
1895 * the conditions for stopping are met, netif_tx_stop_all_queues() is called.
1896 * In case check_stop is false, will check if net queues need to be waked. If
1897 * the conditions for waking are met, netif_tx_wake_all_queues() is called.
1898 * vring is the vring which is currently being modified by either adding
1899 * descriptors (tx) into it or removing descriptors (tx complete) from it. Can
1900 * be null when irrelevant (e.g. connect/disconnect events).
1901 *
1902 * The implementation is to stop net queues if modified vring has low
1903 * descriptor availability. Wake if all vrings are not in low descriptor
1904 * availability and modified vring has high descriptor availability.
1905 */
1906static inline void __wil_update_net_queues(struct wil6210_priv *wil,
1907 struct vring *vring,
1908 bool check_stop)
1909{
1910 int i;
1911
1912 if (vring)
1913 wil_dbg_txrx(wil, "vring %d, check_stop=%d, stopped=%d",
1914 (int)(vring - wil->vring_tx), check_stop,
1915 wil->net_queue_stopped);
1916 else
1917 wil_dbg_txrx(wil, "check_stop=%d, stopped=%d",
1918 check_stop, wil->net_queue_stopped);
1919
1920 if (check_stop == wil->net_queue_stopped)
1921 /* net queues already in desired state */
1922 return;
1923
1924 if (check_stop) {
1925 if (!vring || unlikely(wil_vring_avail_low(vring))) {
1926 /* not enough room in the vring */
1927 netif_tx_stop_all_queues(wil_to_ndev(wil));
1928 wil->net_queue_stopped = true;
1929 wil_dbg_txrx(wil, "netif_tx_stop called\n");
1930 }
1931 return;
1932 }
1933
Maya Ereza850af72017-05-29 10:26:13 +03001934 /* Do not wake the queues in suspend flow */
1935 if (test_bit(wil_status_suspending, wil->status) ||
1936 test_bit(wil_status_suspended, wil->status))
1937 return;
1938
Dedy Lansky970a98f2016-12-06 08:26:49 +02001939 /* check wake */
1940 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1941 struct vring *cur_vring = &wil->vring_tx[i];
1942 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
1943
1944 if (!cur_vring->va || !txdata->enabled || cur_vring == vring)
1945 continue;
1946
1947 if (wil_vring_avail_low(cur_vring)) {
1948 wil_dbg_txrx(wil, "vring %d full, can't wake\n",
1949 (int)(cur_vring - wil->vring_tx));
1950 return;
1951 }
1952 }
1953
1954 if (!vring || wil_vring_avail_high(vring)) {
1955 /* enough room in the vring */
1956 wil_dbg_txrx(wil, "calling netif_tx_wake\n");
1957 netif_tx_wake_all_queues(wil_to_ndev(wil));
1958 wil->net_queue_stopped = false;
1959 }
1960}
1961
1962void wil_update_net_queues(struct wil6210_priv *wil, struct vring *vring,
1963 bool check_stop)
1964{
1965 spin_lock(&wil->net_queue_lock);
1966 __wil_update_net_queues(wil, vring, check_stop);
1967 spin_unlock(&wil->net_queue_lock);
1968}
1969
1970void wil_update_net_queues_bh(struct wil6210_priv *wil, struct vring *vring,
1971 bool check_stop)
1972{
1973 spin_lock_bh(&wil->net_queue_lock);
1974 __wil_update_net_queues(wil, vring, check_stop);
1975 spin_unlock_bh(&wil->net_queue_lock);
1976}
1977
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001978netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1979{
1980 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001981 struct ethhdr *eth = (void *)skb->data;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001982 bool bcast = is_multicast_ether_addr(eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001983 struct vring *vring;
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001984 static bool pr_once_fw;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001985 int rc;
1986
Lazar Alexei3190cc62017-02-23 14:34:14 +02001987 wil_dbg_txrx(wil, "start_xmit\n");
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001988 if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001989 if (!pr_once_fw) {
1990 wil_err(wil, "FW not ready\n");
1991 pr_once_fw = true;
1992 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001993 goto drop;
1994 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001995 if (unlikely(!test_bit(wil_status_fwconnected, wil->status))) {
Maya Erezd8ed0432016-04-26 14:41:39 +03001996 wil_dbg_ratelimited(wil, "FW not connected, packet dropped\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001997 goto drop;
1998 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001999 if (unlikely(wil->wdev->iftype == NL80211_IFTYPE_MONITOR)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002000 wil_err(wil, "Xmit in monitor mode not supported\n");
2001 goto drop;
2002 }
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02002003 pr_once_fw = false;
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03002004
2005 /* find vring */
Lior Davidb1c0bbf2017-02-08 13:14:13 +02002006 if (wil->wdev->iftype == NL80211_IFTYPE_STATION && !wil->pbss) {
2007 /* in STA mode (ESS), all to same VRING (to AP) */
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02002008 vring = wil_find_tx_vring_sta(wil, skb);
Lior Davidb1c0bbf2017-02-08 13:14:13 +02002009 } else if (bcast) {
2010 if (wil->pbss)
2011 /* in pbss, no bcast VRING - duplicate skb in
2012 * all stations VRINGs
2013 */
2014 vring = wil_find_tx_bcast_2(wil, skb);
2015 else if (wil->wdev->iftype == NL80211_IFTYPE_AP)
2016 /* AP has a dedicated bcast VRING */
2017 vring = wil_find_tx_bcast_1(wil, skb);
2018 else
2019 /* unexpected combination, fallback to duplicating
2020 * the skb in all stations VRINGs
2021 */
2022 vring = wil_find_tx_bcast_2(wil, skb);
2023 } else {
2024 /* unicast, find specific VRING by dest. address */
2025 vring = wil_find_tx_ucast(wil, skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02002026 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002027 if (unlikely(!vring)) {
Vladimir Kondratiev5aed1392014-06-16 19:37:15 +03002028 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03002029 goto drop;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002030 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03002031 /* set up vring entry */
2032 rc = wil_tx_vring(wil, vring, skb);
2033
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002034 switch (rc) {
2035 case 0:
Dedy Lansky970a98f2016-12-06 08:26:49 +02002036 /* shall we stop net queues? */
2037 wil_update_net_queues_bh(wil, vring, true);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02002038 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002039 dev_kfree_skb_any(skb);
2040 return NETDEV_TX_OK;
2041 case -ENOMEM:
2042 return NETDEV_TX_BUSY;
2043 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +02002044 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002045 }
2046 drop:
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002047 ndev->stats.tx_dropped++;
2048 dev_kfree_skb_any(skb);
2049
2050 return NET_XMIT_DROP;
2051}
2052
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02002053static inline bool wil_need_txstat(struct sk_buff *skb)
2054{
2055 struct ethhdr *eth = (void *)skb->data;
2056
2057 return is_unicast_ether_addr(eth->h_dest) && skb->sk &&
2058 (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS);
2059}
2060
2061static inline void wil_consume_skb(struct sk_buff *skb, bool acked)
2062{
2063 if (unlikely(wil_need_txstat(skb)))
2064 skb_complete_wifi_ack(skb, acked);
2065 else
2066 acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb);
2067}
2068
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002069/**
2070 * Clean up transmitted skb's from the Tx VRING
2071 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002072 * Return number of descriptors cleared
2073 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002074 * Safe to call from IRQ
2075 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002076int wil_tx_complete(struct wil6210_priv *wil, int ringid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002077{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02002078 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002079 struct device *dev = wil_to_dev(wil);
2080 struct vring *vring = &wil->vring_tx[ringid];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02002081 struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002082 int done = 0;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +02002083 int cid = wil->vring2cid_tid[ringid][0];
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002084 struct wil_net_stats *stats = NULL;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002085 volatile struct vring_tx_desc *_d;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002086 int used_before_complete;
2087 int used_new;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002088
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002089 if (unlikely(!vring->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002090 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002091 return 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002092 }
2093
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002094 if (unlikely(!txdata->enabled)) {
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02002095 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
2096 return 0;
2097 }
2098
Lazar Alexei3190cc62017-02-23 14:34:14 +02002099 wil_dbg_txrx(wil, "tx_complete: (%d)\n", ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002100
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002101 used_before_complete = wil_vring_used_tx(vring);
2102
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002103 if (cid < WIL6210_MAX_CID)
2104 stats = &wil->sta[cid].stats;
2105
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002106 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002107 int new_swtail;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03002108 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002109 /**
2110 * For the fragmented skb, HW will set DU bit only for the
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03002111 * last fragment. look for it.
2112 * In TSO the first DU will include hdr desc
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002113 */
2114 int lf = (vring->swtail + ctx->nr_frags) % vring->size;
2115 /* TODO: check we are not past head */
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +03002116
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002117 _d = &vring->va[lf].tx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002118 if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002119 break;
2120
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002121 new_swtail = (lf + 1) % vring->size;
2122 while (vring->swtail != new_swtail) {
2123 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002124 u16 dmalen;
Vladimir Kondratiev76dfa4b2014-07-14 09:49:39 +03002125 struct sk_buff *skb;
2126
2127 ctx = &vring->ctx[vring->swtail];
2128 skb = ctx->skb;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002129 _d = &vring->va[vring->swtail].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002130
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002131 *d = *_d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03002132
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002133 dmalen = le16_to_cpu(d->dma.length);
2134 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
2135 d->dma.error);
2136 wil_dbg_txrx(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02002137 "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
2138 ringid, vring->swtail, dmalen,
2139 d->dma.status, d->dma.error);
2140 wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002141 (const void *)d, sizeof(*d), false);
2142
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02002143 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002144
2145 if (skb) {
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002146 if (likely(d->dma.error == 0)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002147 ndev->stats.tx_packets++;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002148 ndev->stats.tx_bytes += skb->len;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002149 if (stats) {
2150 stats->tx_packets++;
2151 stats->tx_bytes += skb->len;
2152 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002153 } else {
2154 ndev->stats.tx_errors++;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002155 if (stats)
2156 stats->tx_errors++;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002157 }
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02002158 wil_consume_skb(skb, d->dma.error == 0);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02002159 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002160 memset(ctx, 0, sizeof(*ctx));
Maya Erezeb26cff2016-05-16 22:23:30 +03002161 /* Make sure the ctx is zeroed before updating the tail
2162 * to prevent a case where wil_tx_vring will see
2163 * this descriptor as used and handle it before ctx zero
2164 * is completed.
2165 */
2166 wmb();
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002167 /* There is no need to touch HW descriptor:
2168 * - ststus bit TX_DMA_STATUS_DU is set by design,
2169 * so hardware will not try to process this desc.,
2170 * - rest of descriptor will be initialized on Tx.
2171 */
2172 vring->swtail = wil_vring_next_tail(vring);
2173 done++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002174 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002175 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03002176
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002177 /* performance monitoring */
2178 used_new = wil_vring_used_tx(vring);
Gidon Studinski1c9a1932017-08-21 22:55:15 +03002179 if (wil_val_in_range(wil->vring_idle_trsh,
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002180 used_new, used_before_complete)) {
2181 wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
2182 ringid, used_before_complete, used_new);
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03002183 txdata->last_idle = get_cycles();
2184 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03002185
Dedy Lansky970a98f2016-12-06 08:26:49 +02002186 /* shall we wake net queues? */
2187 if (done)
2188 wil_update_net_queues(wil, vring, false);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002189
2190 return done;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002191}