blob: d0fecd9f8a4cd1aadc7f35d3292cbb21e636ad7d [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
Alexei Avshalom Lazar6f3c18f2018-05-07 12:23:24 +0300326 d->mac.pn_15_0 = 0;
327 d->mac.pn_47_16 = 0;
Vladimir Kondratiev48c963a2015-04-30 16:25:06 +0300328 d->dma.d0 = RX_DMA_D0_CMD_DMA_RT | RX_DMA_D0_CMD_DMA_IT;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300329 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800330 /* ip_length don't care */
331 /* b11 don't care */
332 /* error don't care */
333 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300334 d->dma.length = cpu_to_le16(sz);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300335 *_d = *d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300336 vring->ctx[i].skb = skb;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800337
338 return 0;
339}
340
341/**
342 * Adds radiotap header
343 *
344 * Any error indicated as "Bad FCS"
345 *
346 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
347 * - Rx descriptor: 32 bytes
348 * - Phy info
349 */
350static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300351 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800352{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800353 struct wil6210_rtap {
354 struct ieee80211_radiotap_header rthdr;
355 /* fields should be in the order of bits in rthdr.it_present */
356 /* flags */
357 u8 flags;
358 /* channel */
359 __le16 chnl_freq __aligned(2);
360 __le16 chnl_flags;
361 /* MCS */
362 u8 mcs_present;
363 u8 mcs_flags;
364 u8 mcs_index;
365 } __packed;
366 struct wil6210_rtap_vendor {
367 struct wil6210_rtap rtap;
368 /* vendor */
369 u8 vendor_oui[3] __aligned(2);
370 u8 vendor_ns;
371 __le16 vendor_skip;
372 u8 vendor_data[0];
373 } __packed;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300374 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800375 struct wil6210_rtap_vendor *rtap_vendor;
376 int rtap_len = sizeof(struct wil6210_rtap);
377 int phy_length = 0; /* phy info header size, bytes */
378 static char phy_data[128];
Lior David043ef9d2017-12-14 18:53:13 +0200379 struct ieee80211_channel *ch = wil->monitor_chandef.chan;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800380
381 if (rtap_include_phy_info) {
382 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
383 /* calculate additional length */
384 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
385 /**
386 * PHY info starts from 8-byte boundary
387 * there are 8-byte lines, last line may be partially
388 * written (HW bug), thus FW configures for last line
389 * to be excessive. Driver skips this last line.
390 */
391 int len = min_t(int, 8 + sizeof(phy_data),
392 wil_rxdesc_phy_length(d));
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300393
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800394 if (len > 8) {
395 void *p = skb_tail_pointer(skb);
396 void *pa = PTR_ALIGN(p, 8);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300397
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800398 if (skb_tailroom(skb) >= len + (pa - p)) {
399 phy_length = len - 8;
400 memcpy(phy_data, pa, phy_length);
401 }
402 }
403 }
404 rtap_len += phy_length;
405 }
406
407 if (skb_headroom(skb) < rtap_len &&
408 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
409 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
410 return;
411 }
412
413 rtap_vendor = (void *)skb_push(skb, rtap_len);
414 memset(rtap_vendor, 0, rtap_len);
415
416 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
417 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
418 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
419 (1 << IEEE80211_RADIOTAP_FLAGS) |
420 (1 << IEEE80211_RADIOTAP_CHANNEL) |
421 (1 << IEEE80211_RADIOTAP_MCS));
422 if (d->dma.status & RX_DMA_STATUS_ERROR)
423 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
424
425 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
426 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
427
428 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
429 rtap_vendor->rtap.mcs_flags = 0;
430 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
431
432 if (rtap_include_phy_info) {
433 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
434 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
435 /* OUI for Wilocity 04:ce:14 */
436 rtap_vendor->vendor_oui[0] = 0x04;
437 rtap_vendor->vendor_oui[1] = 0xce;
438 rtap_vendor->vendor_oui[2] = 0x14;
439 rtap_vendor->vendor_ns = 1;
440 /* Rx descriptor + PHY data */
441 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
442 phy_length);
443 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
444 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
445 phy_length);
446 }
447}
448
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300449/* similar to ieee80211_ version, but FC contain only 1-st byte */
450static inline int wil_is_back_req(u8 fc)
451{
452 return (fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) ==
453 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK_REQ);
454}
455
Maya Ereze3309bf2017-05-15 16:57:46 +0300456bool wil_is_rx_idle(struct wil6210_priv *wil)
457{
458 struct vring_rx_desc *_d;
459 struct vring *vring = &wil->vring_rx;
460
461 _d = (struct vring_rx_desc *)&vring->va[vring->swhead].rx;
462 if (_d->dma.status & RX_DMA_STATUS_DU)
463 return false;
464
465 return true;
466}
467
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800468/**
469 * reap 1 frame from @swhead
470 *
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300471 * Rx descriptor copied to skb->cb
472 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800473 * Safe to call from IRQ
474 */
475static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
476 struct vring *vring)
477{
478 struct device *dev = wil_to_dev(wil);
479 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300480 volatile struct vring_rx_desc *_d;
481 struct vring_rx_desc *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800482 struct sk_buff *skb;
483 dma_addr_t pa;
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200484 unsigned int snaplen = wil_rx_snaplen();
Lior David2e9a3212017-05-04 22:43:18 +0300485 unsigned int sz = wil->rx_buf_len + ETH_HLEN + snaplen;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300486 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800487 u8 ftype;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200488 int cid;
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300489 int i;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200490 struct wil_net_stats *stats;
491
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300492 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
493
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300494again:
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200495 if (unlikely(wil_vring_is_empty(vring)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800496 return NULL;
497
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300498 i = (int)vring->swhead;
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200499 _d = &vring->va[i].rx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200500 if (unlikely(!(_d->dma.status & RX_DMA_STATUS_DU))) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800501 /* it is not error, we just reached end of Rx done area */
502 return NULL;
503 }
504
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200505 skb = vring->ctx[i].skb;
506 vring->ctx[i].skb = NULL;
507 wil_vring_advance_head(vring, 1);
508 if (!skb) {
509 wil_err(wil, "No Rx skb at [%d]\n", i);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300510 goto again;
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200511 }
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300512 d = wil_skb_rxdesc(skb);
513 *d = *_d;
514 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300515
516 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
517 dmalen = le16_to_cpu(d->dma.length);
518
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200519 trace_wil6210_rx(i, d);
520 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", i, dmalen);
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300521 wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300522 (const void *)d, sizeof(*d), false);
523
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300524 cid = wil_rxdesc_cid(d);
525 stats = &wil->sta[cid].stats;
526
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200527 if (unlikely(dmalen > sz)) {
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300528 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300529 stats->rx_large_frame++;
Wei Yongjun110dea02013-05-23 17:10:43 +0800530 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300531 goto again;
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300532 }
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300533 skb_trim(skb, dmalen);
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300534
Vladimir Kondratiev1cbbcb02014-01-08 11:50:49 +0200535 prefetch(skb->data);
536
Vladimir Kondratievc0d37712013-05-12 14:43:34 +0300537 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
538 skb->data, skb_headlen(skb), false);
539
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200540 stats->last_mcs_rx = wil_rxdesc_mcs(d);
Vladimir Kondratievc4a110d2015-06-09 14:11:18 +0300541 if (stats->last_mcs_rx < ARRAY_SIZE(stats->rx_per_mcs))
542 stats->rx_per_mcs[stats->last_mcs_rx]++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800543
544 /* use radiotap header only if required */
545 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300546 wil_rx_add_radiotap_header(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800547
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800548 /* no extra checks if in sniffer mode */
549 if (ndev->type != ARPHRD_ETHER)
550 return skb;
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300551 /* Non-data frames may be delivered through Rx DMA channel (ex: BAR)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800552 * Driver should recognize it by frame type, that is found
553 * in Rx descriptor. If type is not data, it is 802.11 frame as is
554 */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300555 ftype = wil_rxdesc_ftype(d) << 2;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200556 if (unlikely(ftype != IEEE80211_FTYPE_DATA)) {
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300557 u8 fc1 = wil_rxdesc_fc1(d);
558 int mid = wil_rxdesc_mid(d);
559 int tid = wil_rxdesc_tid(d);
560 u16 seq = wil_rxdesc_seq(d);
561
562 wil_dbg_txrx(wil,
563 "Non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
564 fc1, mid, cid, tid, seq);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300565 stats->rx_non_data_frame++;
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300566 if (wil_is_back_req(fc1)) {
567 wil_dbg_txrx(wil,
568 "BAR: MID %d CID %d TID %d Seq 0x%03x\n",
569 mid, cid, tid, seq);
570 wil_rx_bar(wil, cid, tid, seq);
571 } else {
572 /* print again all info. One can enable only this
573 * without overhead for printing every Rx frame
574 */
575 wil_dbg_txrx(wil,
576 "Unhandled non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
577 fc1, mid, cid, tid, seq);
578 wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
579 (const void *)d, sizeof(*d), false);
580 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
581 skb->data, skb_headlen(skb), false);
582 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800583 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300584 goto again;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800585 }
586
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200587 if (unlikely(skb->len < ETH_HLEN + snaplen)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800588 wil_err(wil, "Short frame, len = %d\n", skb->len);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300589 stats->rx_short_frame++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800590 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300591 goto again;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800592 }
593
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300594 /* L4 IDENT is on when HW calculated checksum, check status
595 * and in case of error drop the packet
596 * higher stack layers will handle retransmission (if required)
597 */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200598 if (likely(d->dma.status & RX_DMA_STATUS_L4I)) {
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300599 /* L4 protocol identified, csum calculated */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200600 if (likely((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0))
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300601 skb->ip_summed = CHECKSUM_UNNECESSARY;
Vladimir Kondratiev4a68ab102013-08-13 15:25:32 +0300602 /* If HW reports bad checksum, let IP stack re-check it
603 * For example, HW don't understand Microsoft IP stack that
604 * mis-calculates TCP checksum - if it should be 0x0,
605 * it writes 0xffff in violation of RFC 1624
606 */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300607 }
608
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200609 if (snaplen) {
610 /* Packet layout
611 * +-------+-------+---------+------------+------+
612 * | SA(6) | DA(6) | SNAP(6) | ETHTYPE(2) | DATA |
613 * +-------+-------+---------+------------+------+
614 * Need to remove SNAP, shifting SA and DA forward
615 */
616 memmove(skb->data + snaplen, skb->data, 2 * ETH_ALEN);
617 skb_pull(skb, snaplen);
618 }
619
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800620 return skb;
621}
622
623/**
624 * allocate and fill up to @count buffers in rx ring
625 * buffers posted at @swtail
626 */
627static int wil_rx_refill(struct wil6210_priv *wil, int count)
628{
629 struct net_device *ndev = wil_to_ndev(wil);
630 struct vring *v = &wil->vring_rx;
631 u32 next_tail;
632 int rc = 0;
633 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
634 WIL6210_RTAP_SIZE : 0;
635
636 for (; next_tail = wil_vring_next_tail(v),
637 (next_tail != v->swhead) && (count-- > 0);
638 v->swtail = next_tail) {
639 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200640 if (unlikely(rc)) {
Dedy Lansky01169d12018-03-08 13:21:25 +0200641 wil_err_ratelimited(wil, "Error %d in rx refill[%d]\n",
642 rc, v->swtail);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800643 break;
644 }
645 }
Maya Erezab6d7cc2016-05-16 22:23:31 +0300646
647 /* make sure all writes to descriptors (shared memory) are done before
648 * committing them to HW
649 */
650 wmb();
651
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +0300652 wil_w(wil, v->hwtail, v->swtail);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800653
654 return rc;
655}
656
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200657/**
658 * reverse_memcmp - Compare two areas of memory, in reverse order
659 * @cs: One area of memory
660 * @ct: Another area of memory
661 * @count: The size of the area.
662 *
663 * Cut'n'paste from original memcmp (see lib/string.c)
664 * with minimal modifications
665 */
666static int reverse_memcmp(const void *cs, const void *ct, size_t count)
667{
668 const unsigned char *su1, *su2;
669 int res = 0;
670
671 for (su1 = cs + count - 1, su2 = ct + count - 1; count > 0;
672 --su1, --su2, count--) {
673 res = *su1 - *su2;
674 if (res)
675 break;
676 }
677 return res;
678}
679
680static int wil_rx_crypto_check(struct wil6210_priv *wil, struct sk_buff *skb)
681{
682 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
683 int cid = wil_rxdesc_cid(d);
684 int tid = wil_rxdesc_tid(d);
685 int key_id = wil_rxdesc_key_id(d);
686 int mc = wil_rxdesc_mcast(d);
687 struct wil_sta_info *s = &wil->sta[cid];
688 struct wil_tid_crypto_rx *c = mc ? &s->group_crypto_rx :
689 &s->tid_crypto_rx[tid];
690 struct wil_tid_crypto_rx_single *cc = &c->key_id[key_id];
691 const u8 *pn = (u8 *)&d->mac.pn_15_0;
692
693 if (!cc->key_set) {
694 wil_err_ratelimited(wil,
695 "Key missing. CID %d TID %d MCast %d KEY_ID %d\n",
696 cid, tid, mc, key_id);
697 return -EINVAL;
698 }
699
700 if (reverse_memcmp(pn, cc->pn, IEEE80211_GCMP_PN_LEN) <= 0) {
701 wil_err_ratelimited(wil,
702 "Replay attack. CID %d TID %d MCast %d KEY_ID %d PN %6phN last %6phN\n",
703 cid, tid, mc, key_id, pn, cc->pn);
704 return -EINVAL;
705 }
706 memcpy(cc->pn, pn, IEEE80211_GCMP_PN_LEN);
707
708 return 0;
709}
710
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800711/*
712 * Pass Rx packet to the netif. Update statistics.
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300713 * Called in softirq context (NAPI poll).
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800714 */
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200715void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800716{
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200717 gro_result_t rc = GRO_NORMAL;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200718 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200719 struct wireless_dev *wdev = wil_to_wdev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800720 unsigned int len = skb->len;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200721 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200722 int cid = wil_rxdesc_cid(d); /* always 0..7, no need to check */
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200723 int security = wil_rxdesc_security(d);
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200724 struct ethhdr *eth = (void *)skb->data;
725 /* here looking for DA, not A1, thus Rxdesc's 'mcast' indication
726 * is not suitable, need to look at data
727 */
728 int mcast = is_multicast_ether_addr(eth->h_dest);
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200729 struct wil_net_stats *stats = &wil->sta[cid].stats;
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200730 struct sk_buff *xmit_skb = NULL;
731 static const char * const gro_res_str[] = {
732 [GRO_MERGED] = "GRO_MERGED",
733 [GRO_MERGED_FREE] = "GRO_MERGED_FREE",
734 [GRO_HELD] = "GRO_HELD",
735 [GRO_NORMAL] = "GRO_NORMAL",
736 [GRO_DROP] = "GRO_DROP",
737 };
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800738
Vladimir Shulman05536402015-07-30 13:52:04 +0300739 if (ndev->features & NETIF_F_RXHASH)
740 /* fake L4 to ensure it won't be re-calculated later
741 * set hash to any non-zero value to activate rps
742 * mechanism, core will be chosen according
743 * to user-level rps configuration.
744 */
745 skb_set_hash(skb, 1, PKT_HASH_TYPE_L4);
746
Vladimir Kondratiev241804c2013-01-28 18:31:02 +0200747 skb_orphan(skb);
748
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200749 if (security && (wil_rx_crypto_check(wil, skb) != 0)) {
750 rc = GRO_DROP;
751 dev_kfree_skb(skb);
752 stats->rx_replay++;
753 goto stats;
754 }
755
Vladimir Kondratiev02beaf12015-03-08 15:42:03 +0200756 if (wdev->iftype == NL80211_IFTYPE_AP && !wil->ap_isolate) {
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200757 if (mcast) {
758 /* send multicast frames both to higher layers in
759 * local net stack and back to the wireless medium
760 */
761 xmit_skb = skb_copy(skb, GFP_ATOMIC);
762 } else {
763 int xmit_cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800764
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200765 if (xmit_cid >= 0) {
766 /* The destination station is associated to
767 * this AP (in this VLAN), so send the frame
768 * directly to it and do not pass it to local
769 * net stack.
770 */
771 xmit_skb = skb;
772 skb = NULL;
773 }
774 }
775 }
776 if (xmit_skb) {
777 /* Send to wireless media and increase priority by 256 to
778 * keep the received priority instead of reclassifying
779 * the frame (see cfg80211_classify8021d).
780 */
781 xmit_skb->dev = ndev;
782 xmit_skb->priority += 256;
783 xmit_skb->protocol = htons(ETH_P_802_3);
784 skb_reset_network_header(xmit_skb);
785 skb_reset_mac_header(xmit_skb);
786 wil_dbg_txrx(wil, "Rx -> Tx %d bytes\n", len);
787 dev_queue_xmit(xmit_skb);
788 }
789
790 if (skb) { /* deliver to local stack */
791
792 skb->protocol = eth_type_trans(skb, ndev);
793 rc = napi_gro_receive(&wil->napi_rx, skb);
794 wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
795 len, gro_res_str[rc]);
796 }
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200797stats:
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200798 /* statistics. rc set to GRO_NORMAL for AP bridging */
Vladimir Kondratievb5998e62014-03-17 15:34:22 +0200799 if (unlikely(rc == GRO_DROP)) {
800 ndev->stats.rx_dropped++;
801 stats->rx_dropped++;
802 wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
803 } else {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800804 ndev->stats.rx_packets++;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200805 stats->rx_packets++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800806 ndev->stats.rx_bytes += len;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200807 stats->rx_bytes += len;
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200808 if (mcast)
809 ndev->stats.multicast++;
Vladimir Kondratiev194b4822014-06-16 19:37:14 +0300810 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800811}
812
813/**
814 * Proceed all completed skb's from Rx VRING
815 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300816 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800817 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300818void wil_rx_handle(struct wil6210_priv *wil, int *quota)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800819{
820 struct net_device *ndev = wil_to_ndev(wil);
821 struct vring *v = &wil->vring_rx;
822 struct sk_buff *skb;
823
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200824 if (unlikely(!v->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800825 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
826 return;
827 }
Lazar Alexei3190cc62017-02-23 14:34:14 +0200828 wil_dbg_txrx(wil, "rx_handle\n");
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300829 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
830 (*quota)--;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800831
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800832 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
833 skb->dev = ndev;
834 skb_reset_mac_header(skb);
835 skb->ip_summed = CHECKSUM_UNNECESSARY;
836 skb->pkt_type = PACKET_OTHERHOST;
837 skb->protocol = htons(ETH_P_802_2);
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200838 wil_netif_rx_any(skb, ndev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800839 } else {
Vladimir Kondratieve4373d82014-12-23 09:47:21 +0200840 wil_rx_reorder(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800841 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800842 }
843 wil_rx_refill(wil, v->size);
844}
845
Lior David2e9a3212017-05-04 22:43:18 +0300846static void wil_rx_buf_len_init(struct wil6210_priv *wil)
847{
848 wil->rx_buf_len = rx_large_buf ?
849 WIL_MAX_ETH_MTU : TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
850 if (mtu_max > wil->rx_buf_len) {
851 /* do not allow RX buffers to be smaller than mtu_max, for
852 * backward compatibility (mtu_max parameter was also used
853 * to support receiving large packets)
854 */
855 wil_info(wil, "Override RX buffer to mtu_max(%d)\n", mtu_max);
856 wil->rx_buf_len = mtu_max;
857 }
858}
859
Vladimir Kondratievd3762b42014-12-01 15:35:02 +0200860int wil_rx_init(struct wil6210_priv *wil, u16 size)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800861{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800862 struct vring *vring = &wil->vring_rx;
863 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800864
Lazar Alexei3190cc62017-02-23 14:34:14 +0200865 wil_dbg_misc(wil, "rx_init\n");
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300866
Vladimir Kondratiev8bf6adb2014-03-17 15:34:17 +0200867 if (vring->va) {
868 wil_err(wil, "Rx ring already allocated\n");
869 return -EINVAL;
870 }
871
Lior David2e9a3212017-05-04 22:43:18 +0300872 wil_rx_buf_len_init(wil);
873
Vladimir Kondratievd3762b42014-12-01 15:35:02 +0200874 vring->size = size;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800875 rc = wil_vring_alloc(wil, vring);
876 if (rc)
877 return rc;
878
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +0200879 rc = wmi_rx_chain_add(wil, vring);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800880 if (rc)
881 goto err_free;
882
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800883 rc = wil_rx_refill(wil, vring->size);
884 if (rc)
885 goto err_free;
886
887 return 0;
888 err_free:
889 wil_vring_free(wil, vring, 0);
890
891 return rc;
892}
893
894void wil_rx_fini(struct wil6210_priv *wil)
895{
896 struct vring *vring = &wil->vring_rx;
897
Lazar Alexei3190cc62017-02-23 14:34:14 +0200898 wil_dbg_misc(wil, "rx_fini\n");
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300899
Vladimir Kondratiev2acb4222013-01-28 18:31:08 +0200900 if (vring->va)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800901 wil_vring_free(wil, vring, 0);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800902}
903
Maya Erez875e9432016-01-28 19:24:02 +0200904static inline void wil_tx_data_init(struct vring_tx_data *txdata)
905{
906 spin_lock_bh(&txdata->lock);
907 txdata->dot1x_open = 0;
908 txdata->enabled = 0;
909 txdata->idle = 0;
910 txdata->last_idle = 0;
911 txdata->begin = 0;
912 txdata->agg_wsize = 0;
913 txdata->agg_timeout = 0;
914 txdata->agg_amsdu = 0;
915 txdata->addba_in_progress = false;
916 spin_unlock_bh(&txdata->lock);
917}
918
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800919int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
920 int cid, int tid)
921{
922 int rc;
923 struct wmi_vring_cfg_cmd cmd = {
924 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
925 .vring_cfg = {
926 .tx_sw_ring = {
Vladimir Kondratiev9a06bec2014-10-28 16:51:27 +0200927 .max_mpdu_size =
Vladimir Kondratievc44690a2014-12-23 09:47:11 +0200928 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
Vladimir Kondratievb5d98e92013-04-18 14:33:51 +0300929 .ring_size = cpu_to_le16(size),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800930 },
931 .ringid = id,
Vladimir Kondratieva70abea2014-03-17 15:34:05 +0200932 .cidxtid = mk_cidxtid(cid, tid),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800933 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
934 .mac_ctrl = 0,
935 .to_resolution = 0,
Vladimir Kondratiev32772132014-12-23 09:47:03 +0200936 .agg_max_wsize = 0,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800937 .schd_params = {
938 .priority = cpu_to_le16(0),
939 .timeslot_us = cpu_to_le16(0xfff),
940 },
941 },
942 };
943 struct {
Lior Davidb874dde2016-03-01 19:18:09 +0200944 struct wmi_cmd_hdr wmi;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800945 struct wmi_vring_cfg_done_event cmd;
946 } __packed reply;
947 struct vring *vring = &wil->vring_tx[id];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200948 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800949
Lazar Alexei3190cc62017-02-23 14:34:14 +0200950 wil_dbg_misc(wil, "vring_init_tx: max_mpdu_size %d\n",
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +0300951 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +0200952 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300953
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800954 if (vring->va) {
955 wil_err(wil, "Tx ring [%d] already allocated\n", id);
956 rc = -EINVAL;
957 goto out;
958 }
959
Maya Erez875e9432016-01-28 19:24:02 +0200960 wil_tx_data_init(txdata);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800961 vring->size = size;
962 rc = wil_vring_alloc(wil, vring);
963 if (rc)
964 goto out;
965
Vladimir Kondratiev93ae6d42014-02-27 16:20:51 +0200966 wil->vring2cid_tid[id][0] = cid;
967 wil->vring2cid_tid[id][1] = tid;
968
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800969 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800970
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300971 if (!wil->privacy)
972 txdata->dot1x_open = true;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800973 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
974 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
975 if (rc)
976 goto out_free;
977
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200978 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800979 wil_err(wil, "Tx config failed, status 0x%02x\n",
980 reply.cmd.status);
Vladimir Kondratievc3319972013-01-28 18:31:09 +0200981 rc = -EINVAL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800982 goto out_free;
983 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800984
Maya Erezdc905062016-08-18 16:52:15 +0300985 spin_lock_bh(&txdata->lock);
986 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200987 txdata->enabled = 1;
Maya Erezdc905062016-08-18 16:52:15 +0300988 spin_unlock_bh(&txdata->lock);
989
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300990 if (txdata->dot1x_open && (agg_wsize >= 0))
Vladimir Kondratiev3a3def82014-12-23 09:47:05 +0200991 wil_addba_tx_request(wil, id, agg_wsize);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200992
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800993 return 0;
994 out_free:
Maya Erez875e9432016-01-28 19:24:02 +0200995 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300996 txdata->dot1x_open = false;
997 txdata->enabled = 0;
Maya Erez875e9432016-01-28 19:24:02 +0200998 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800999 wil_vring_free(wil, vring, 1);
Maya Erez0916d9f2016-01-17 12:39:10 +02001000 wil->vring2cid_tid[id][0] = WIL6210_MAX_CID;
1001 wil->vring2cid_tid[id][1] = 0;
1002
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001003 out:
1004
1005 return rc;
1006}
1007
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001008int wil_vring_init_bcast(struct wil6210_priv *wil, int id, int size)
1009{
1010 int rc;
1011 struct wmi_bcast_vring_cfg_cmd cmd = {
1012 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
1013 .vring_cfg = {
1014 .tx_sw_ring = {
1015 .max_mpdu_size =
1016 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
1017 .ring_size = cpu_to_le16(size),
1018 },
1019 .ringid = id,
1020 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
1021 },
1022 };
1023 struct {
Lior Davidb874dde2016-03-01 19:18:09 +02001024 struct wmi_cmd_hdr wmi;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001025 struct wmi_vring_cfg_done_event cmd;
1026 } __packed reply;
1027 struct vring *vring = &wil->vring_tx[id];
1028 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
1029
Lazar Alexei3190cc62017-02-23 14:34:14 +02001030 wil_dbg_misc(wil, "vring_init_bcast: max_mpdu_size %d\n",
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001031 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +02001032 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001033
1034 if (vring->va) {
1035 wil_err(wil, "Tx ring [%d] already allocated\n", id);
1036 rc = -EINVAL;
1037 goto out;
1038 }
1039
Maya Erez875e9432016-01-28 19:24:02 +02001040 wil_tx_data_init(txdata);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001041 vring->size = size;
1042 rc = wil_vring_alloc(wil, vring);
1043 if (rc)
1044 goto out;
1045
1046 wil->vring2cid_tid[id][0] = WIL6210_MAX_CID; /* CID */
1047 wil->vring2cid_tid[id][1] = 0; /* TID */
1048
1049 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
1050
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001051 if (!wil->privacy)
1052 txdata->dot1x_open = true;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001053 rc = wmi_call(wil, WMI_BCAST_VRING_CFG_CMDID, &cmd, sizeof(cmd),
1054 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
1055 if (rc)
1056 goto out_free;
1057
1058 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
1059 wil_err(wil, "Tx config failed, status 0x%02x\n",
1060 reply.cmd.status);
1061 rc = -EINVAL;
1062 goto out_free;
1063 }
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001064
Maya Erezdc905062016-08-18 16:52:15 +03001065 spin_lock_bh(&txdata->lock);
1066 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001067 txdata->enabled = 1;
Maya Erezdc905062016-08-18 16:52:15 +03001068 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001069
1070 return 0;
1071 out_free:
Maya Erez875e9432016-01-28 19:24:02 +02001072 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001073 txdata->enabled = 0;
1074 txdata->dot1x_open = false;
Maya Erez875e9432016-01-28 19:24:02 +02001075 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001076 wil_vring_free(wil, vring, 1);
1077 out:
1078
1079 return rc;
1080}
1081
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001082void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
1083{
1084 struct vring *vring = &wil->vring_tx[id];
Vladimir Kondratiev3a124ed2014-12-23 09:47:04 +02001085 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001086
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +02001087 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001088
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001089 if (!vring->va)
1090 return;
1091
Lazar Alexei3190cc62017-02-23 14:34:14 +02001092 wil_dbg_misc(wil, "vring_fini_tx: id=%d\n", id);
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +03001093
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001094 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001095 txdata->dot1x_open = false;
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001096 txdata->enabled = 0; /* no Tx can be in progress or start anew */
1097 spin_unlock_bh(&txdata->lock);
Maya Erez34b88862016-05-16 22:23:32 +03001098 /* napi_synchronize waits for completion of the current NAPI but will
1099 * not prevent the next NAPI run.
1100 * Add a memory barrier to guarantee that txdata->enabled is zeroed
1101 * before napi_synchronize so that the next scheduled NAPI will not
1102 * handle this vring
1103 */
1104 wmb();
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001105 /* make sure NAPI won't touch this vring */
Vladimir Kondratiev9419b6a2014-12-23 09:47:14 +02001106 if (test_bit(wil_status_napi_en, wil->status))
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001107 napi_synchronize(&wil->napi_tx);
1108
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001109 wil_vring_free(wil, vring, 1);
1110}
1111
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001112static struct vring *wil_find_tx_ucast(struct wil6210_priv *wil,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001113 struct sk_buff *skb)
1114{
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001115 int i;
1116 struct ethhdr *eth = (void *)skb->data;
1117 int cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001118
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001119 if (cid < 0)
1120 return NULL;
1121
1122 /* TODO: fix for multiple TID */
1123 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001124 if (!wil->vring_tx_data[i].dot1x_open &&
1125 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1126 continue;
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001127 if (wil->vring2cid_tid[i][0] == cid) {
1128 struct vring *v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001129 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001130
Lazar Alexei3190cc62017-02-23 14:34:14 +02001131 wil_dbg_txrx(wil, "find_tx_ucast: (%pM) -> [%d]\n",
1132 eth->h_dest, i);
Maya Erezb729aaf2016-01-17 12:39:09 +02001133 if (v->va && txdata->enabled) {
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001134 return v;
1135 } else {
Lazar Alexei3190cc62017-02-23 14:34:14 +02001136 wil_dbg_txrx(wil,
1137 "find_tx_ucast: vring[%d] not valid\n",
1138 i);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001139 return NULL;
1140 }
1141 }
1142 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001143
1144 return NULL;
1145}
1146
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001147static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1148 struct sk_buff *skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001149
1150static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
1151 struct sk_buff *skb)
1152{
1153 struct vring *v;
1154 int i;
1155 u8 cid;
Maya Erezb729aaf2016-01-17 12:39:09 +02001156 struct vring_tx_data *txdata;
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001157
1158 /* In the STA mode, it is expected to have only 1 VRING
1159 * for the AP we connected to.
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001160 * find 1-st vring eligible for this skb and use it.
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001161 */
1162 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1163 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001164 txdata = &wil->vring_tx_data[i];
1165 if (!v->va || !txdata->enabled)
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001166 continue;
1167
1168 cid = wil->vring2cid_tid[i][0];
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001169 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1170 continue;
1171
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001172 if (!wil->vring_tx_data[i].dot1x_open &&
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001173 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001174 continue;
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001175
1176 wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
1177
1178 return v;
1179 }
1180
1181 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1182
1183 return NULL;
1184}
1185
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001186/* Use one of 2 strategies:
1187 *
1188 * 1. New (real broadcast):
1189 * use dedicated broadcast vring
1190 * 2. Old (pseudo-DMS):
1191 * Find 1-st vring and return it;
1192 * duplicate skb and send it to other active vrings;
1193 * in all cases override dest address to unicast peer's address
1194 * Use old strategy when new is not supported yet:
1195 * - for PBSS
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001196 */
1197static struct vring *wil_find_tx_bcast_1(struct wil6210_priv *wil,
1198 struct sk_buff *skb)
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001199{
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001200 struct vring *v;
Maya Erezb729aaf2016-01-17 12:39:09 +02001201 struct vring_tx_data *txdata;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001202 int i = wil->bcast_vring;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001203
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001204 if (i < 0)
1205 return NULL;
1206 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001207 txdata = &wil->vring_tx_data[i];
1208 if (!v->va || !txdata->enabled)
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001209 return NULL;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001210 if (!wil->vring_tx_data[i].dot1x_open &&
1211 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1212 return NULL;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001213
1214 return v;
1215}
1216
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001217static void wil_set_da_for_vring(struct wil6210_priv *wil,
1218 struct sk_buff *skb, int vring_index)
1219{
1220 struct ethhdr *eth = (void *)skb->data;
1221 int cid = wil->vring2cid_tid[vring_index][0];
1222
1223 ether_addr_copy(eth->h_dest, wil->sta[cid].addr);
1224}
1225
1226static struct vring *wil_find_tx_bcast_2(struct wil6210_priv *wil,
1227 struct sk_buff *skb)
1228{
1229 struct vring *v, *v2;
1230 struct sk_buff *skb2;
1231 int i;
1232 u8 cid;
1233 struct ethhdr *eth = (void *)skb->data;
1234 char *src = eth->h_source;
Maya Erezb729aaf2016-01-17 12:39:09 +02001235 struct vring_tx_data *txdata;
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001236
1237 /* find 1-st vring eligible for data */
1238 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1239 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001240 txdata = &wil->vring_tx_data[i];
1241 if (!v->va || !txdata->enabled)
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001242 continue;
1243
1244 cid = wil->vring2cid_tid[i][0];
1245 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1246 continue;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001247 if (!wil->vring_tx_data[i].dot1x_open &&
1248 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001249 continue;
1250
1251 /* don't Tx back to source when re-routing Rx->Tx at the AP */
1252 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1253 continue;
1254
1255 goto found;
1256 }
1257
1258 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1259
1260 return NULL;
1261
1262found:
1263 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
1264 wil_set_da_for_vring(wil, skb, i);
1265
1266 /* find other active vrings and duplicate skb for each */
1267 for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
1268 v2 = &wil->vring_tx[i];
1269 if (!v2->va)
1270 continue;
1271 cid = wil->vring2cid_tid[i][0];
1272 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1273 continue;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001274 if (!wil->vring_tx_data[i].dot1x_open &&
1275 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001276 continue;
1277
1278 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1279 continue;
1280
1281 skb2 = skb_copy(skb, GFP_ATOMIC);
1282 if (skb2) {
1283 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
1284 wil_set_da_for_vring(wil, skb2, i);
1285 wil_tx_vring(wil, v2, skb2);
1286 } else {
1287 wil_err(wil, "skb_copy failed\n");
1288 }
1289 }
1290
1291 return v;
1292}
1293
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001294static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
1295 int vring_index)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001296{
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001297 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001298 d->dma.ip_length = 0;
1299 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
1300 d->dma.b11 = 0/*14 | BIT(7)*/;
1301 d->dma.error = 0;
1302 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001303 d->dma.length = cpu_to_le16((u16)len);
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001304 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001305 d->mac.d[0] = 0;
1306 d->mac.d[1] = 0;
1307 d->mac.d[2] = 0;
1308 d->mac.ucode_cmd = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001309 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
1310 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
1311 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
1312
1313 return 0;
1314}
1315
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001316static inline
1317void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
1318{
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001319 d->mac.d[2] |= (nr_frags << MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001320}
1321
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001322/**
1323 * Sets the descriptor @d up for csum and/or TSO offloading. The corresponding
1324 * @skb is used to obtain the protocol and headers length.
1325 * @tso_desc_type is a descriptor type for TSO: 0 - a header, 1 - first data,
1326 * 2 - middle, 3 - last descriptor.
1327 */
1328
1329static void wil_tx_desc_offload_setup_tso(struct vring_tx_desc *d,
1330 struct sk_buff *skb,
1331 int tso_desc_type, bool is_ipv4,
1332 int tcp_hdr_len, int skb_net_hdr_len)
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001333{
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001334 d->dma.b11 = ETH_HLEN; /* MAC header length */
1335 d->dma.b11 |= is_ipv4 << DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS;
1336
1337 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1338 /* L4 header len: TCP header length */
1339 d->dma.d0 |= (tcp_hdr_len & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1340
1341 /* Setup TSO: bit and desc type */
1342 d->dma.d0 |= (BIT(DMA_CFG_DESC_TX_0_TCP_SEG_EN_POS)) |
1343 (tso_desc_type << DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS);
1344 d->dma.d0 |= (is_ipv4 << DMA_CFG_DESC_TX_0_IPV4_CHECKSUM_EN_POS);
1345
1346 d->dma.ip_length = skb_net_hdr_len;
1347 /* Enable TCP/UDP checksum */
1348 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1349 /* Calculate pseudo-header */
1350 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1351}
1352
1353/**
1354 * Sets the descriptor @d up for csum. The corresponding
1355 * @skb is used to obtain the protocol and headers length.
1356 * Returns the protocol: 0 - not TCP, 1 - TCPv4, 2 - TCPv6.
1357 * Note, if d==NULL, the function only returns the protocol result.
1358 *
1359 * It is very similar to previous wil_tx_desc_offload_setup_tso. This
1360 * is "if unrolling" to optimize the critical path.
1361 */
1362
1363static int wil_tx_desc_offload_setup(struct vring_tx_desc *d,
1364 struct sk_buff *skb){
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001365 int protocol;
1366
1367 if (skb->ip_summed != CHECKSUM_PARTIAL)
1368 return 0;
1369
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +02001370 d->dma.b11 = ETH_HLEN; /* MAC header length */
1371
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001372 switch (skb->protocol) {
1373 case cpu_to_be16(ETH_P_IP):
1374 protocol = ip_hdr(skb)->protocol;
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +02001375 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001376 break;
1377 case cpu_to_be16(ETH_P_IPV6):
1378 protocol = ipv6_hdr(skb)->nexthdr;
1379 break;
1380 default:
1381 return -EINVAL;
1382 }
1383
1384 switch (protocol) {
1385 case IPPROTO_TCP:
1386 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1387 /* L4 header len: TCP header length */
1388 d->dma.d0 |=
1389 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1390 break;
1391 case IPPROTO_UDP:
1392 /* L4 header len: UDP header length */
1393 d->dma.d0 |=
1394 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1395 break;
1396 default:
1397 return -EINVAL;
1398 }
1399
1400 d->dma.ip_length = skb_network_header_len(skb);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001401 /* Enable TCP/UDP checksum */
1402 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1403 /* Calculate pseudo-header */
1404 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1405
1406 return 0;
1407}
1408
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001409static inline void wil_tx_last_desc(struct vring_tx_desc *d)
1410{
1411 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS) |
1412 BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS) |
1413 BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
1414}
1415
1416static inline void wil_set_tx_desc_last_tso(volatile struct vring_tx_desc *d)
1417{
1418 d->dma.d0 |= wil_tso_type_lst <<
1419 DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS;
1420}
1421
1422static int __wil_tx_vring_tso(struct wil6210_priv *wil, struct vring *vring,
1423 struct sk_buff *skb)
1424{
1425 struct device *dev = wil_to_dev(wil);
1426
1427 /* point to descriptors in shared memory */
1428 volatile struct vring_tx_desc *_desc = NULL, *_hdr_desc,
1429 *_first_desc = NULL;
1430
1431 /* pointers to shadow descriptors */
1432 struct vring_tx_desc desc_mem, hdr_desc_mem, first_desc_mem,
1433 *d = &hdr_desc_mem, *hdr_desc = &hdr_desc_mem,
1434 *first_desc = &first_desc_mem;
1435
1436 /* pointer to shadow descriptors' context */
1437 struct wil_ctx *hdr_ctx, *first_ctx = NULL;
1438
1439 int descs_used = 0; /* total number of used descriptors */
1440 int sg_desc_cnt = 0; /* number of descriptors for current mss*/
1441
1442 u32 swhead = vring->swhead;
1443 int used, avail = wil_vring_avail_tx(vring);
1444 int nr_frags = skb_shinfo(skb)->nr_frags;
1445 int min_desc_required = nr_frags + 1;
1446 int mss = skb_shinfo(skb)->gso_size; /* payload size w/o headers */
1447 int f, len, hdrlen, headlen;
1448 int vring_index = vring - wil->vring_tx;
1449 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1450 uint i = swhead;
1451 dma_addr_t pa;
1452 const skb_frag_t *frag = NULL;
1453 int rem_data = mss;
1454 int lenmss;
1455 int hdr_compensation_need = true;
1456 int desc_tso_type = wil_tso_type_first;
1457 bool is_ipv4;
1458 int tcp_hdr_len;
1459 int skb_net_hdr_len;
1460 int gso_type;
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001461 int rc = -EINVAL;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001462
Lazar Alexei3190cc62017-02-23 14:34:14 +02001463 wil_dbg_txrx(wil, "tx_vring_tso: %d bytes to vring %d\n", skb->len,
1464 vring_index);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001465
1466 if (unlikely(!txdata->enabled))
1467 return -EINVAL;
1468
1469 /* A typical page 4K is 3-4 payloads, we assume each fragment
1470 * is a full payload, that's how min_desc_required has been
1471 * calculated. In real we might need more or less descriptors,
1472 * this is the initial check only.
1473 */
1474 if (unlikely(avail < min_desc_required)) {
1475 wil_err_ratelimited(wil,
1476 "TSO: Tx ring[%2d] full. No space for %d fragments\n",
1477 vring_index, min_desc_required);
1478 return -ENOMEM;
1479 }
1480
1481 /* Header Length = MAC header len + IP header len + TCP header len*/
1482 hdrlen = ETH_HLEN +
1483 (int)skb_network_header_len(skb) +
1484 tcp_hdrlen(skb);
1485
1486 gso_type = skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4);
1487 switch (gso_type) {
1488 case SKB_GSO_TCPV4:
1489 /* TCP v4, zero out the IP length and IPv4 checksum fields
1490 * as required by the offloading doc
1491 */
1492 ip_hdr(skb)->tot_len = 0;
1493 ip_hdr(skb)->check = 0;
1494 is_ipv4 = true;
1495 break;
1496 case SKB_GSO_TCPV6:
1497 /* TCP v6, zero out the payload length */
1498 ipv6_hdr(skb)->payload_len = 0;
1499 is_ipv4 = false;
1500 break;
1501 default:
1502 /* other than TCPv4 or TCPv6 types are not supported for TSO.
1503 * It is also illegal for both to be set simultaneously
1504 */
1505 return -EINVAL;
1506 }
1507
1508 if (skb->ip_summed != CHECKSUM_PARTIAL)
1509 return -EINVAL;
1510
1511 /* tcp header length and skb network header length are fixed for all
1512 * packet's descriptors - read then once here
1513 */
1514 tcp_hdr_len = tcp_hdrlen(skb);
1515 skb_net_hdr_len = skb_network_header_len(skb);
1516
1517 _hdr_desc = &vring->va[i].tx;
1518
1519 pa = dma_map_single(dev, skb->data, hdrlen, DMA_TO_DEVICE);
1520 if (unlikely(dma_mapping_error(dev, pa))) {
1521 wil_err(wil, "TSO: Skb head DMA map error\n");
1522 goto err_exit;
1523 }
1524
1525 wil_tx_desc_map(hdr_desc, pa, hdrlen, vring_index);
1526 wil_tx_desc_offload_setup_tso(hdr_desc, skb, wil_tso_type_hdr, is_ipv4,
1527 tcp_hdr_len, skb_net_hdr_len);
1528 wil_tx_last_desc(hdr_desc);
1529
1530 vring->ctx[i].mapped_as = wil_mapped_as_single;
1531 hdr_ctx = &vring->ctx[i];
1532
1533 descs_used++;
1534 headlen = skb_headlen(skb) - hdrlen;
1535
1536 for (f = headlen ? -1 : 0; f < nr_frags; f++) {
1537 if (headlen) {
1538 len = headlen;
1539 wil_dbg_txrx(wil, "TSO: process skb head, len %u\n",
1540 len);
1541 } else {
1542 frag = &skb_shinfo(skb)->frags[f];
1543 len = frag->size;
1544 wil_dbg_txrx(wil, "TSO: frag[%d]: len %u\n", f, len);
1545 }
1546
1547 while (len) {
1548 wil_dbg_txrx(wil,
1549 "TSO: len %d, rem_data %d, descs_used %d\n",
1550 len, rem_data, descs_used);
1551
1552 if (descs_used == avail) {
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001553 wil_err_ratelimited(wil, "TSO: ring overflow\n");
1554 rc = -ENOMEM;
1555 goto mem_error;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001556 }
1557
1558 lenmss = min_t(int, rem_data, len);
1559 i = (swhead + descs_used) % vring->size;
1560 wil_dbg_txrx(wil, "TSO: lenmss %d, i %d\n", lenmss, i);
1561
1562 if (!headlen) {
1563 pa = skb_frag_dma_map(dev, frag,
1564 frag->size - len, lenmss,
1565 DMA_TO_DEVICE);
1566 vring->ctx[i].mapped_as = wil_mapped_as_page;
1567 } else {
1568 pa = dma_map_single(dev,
1569 skb->data +
1570 skb_headlen(skb) - headlen,
1571 lenmss,
1572 DMA_TO_DEVICE);
1573 vring->ctx[i].mapped_as = wil_mapped_as_single;
1574 headlen -= lenmss;
1575 }
1576
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001577 if (unlikely(dma_mapping_error(dev, pa))) {
1578 wil_err(wil, "TSO: DMA map page error\n");
1579 goto mem_error;
1580 }
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001581
1582 _desc = &vring->va[i].tx;
1583
1584 if (!_first_desc) {
1585 _first_desc = _desc;
1586 first_ctx = &vring->ctx[i];
1587 d = first_desc;
1588 } else {
1589 d = &desc_mem;
1590 }
1591
1592 wil_tx_desc_map(d, pa, lenmss, vring_index);
1593 wil_tx_desc_offload_setup_tso(d, skb, desc_tso_type,
1594 is_ipv4, tcp_hdr_len,
1595 skb_net_hdr_len);
1596
1597 /* use tso_type_first only once */
1598 desc_tso_type = wil_tso_type_mid;
1599
1600 descs_used++; /* desc used so far */
1601 sg_desc_cnt++; /* desc used for this segment */
1602 len -= lenmss;
1603 rem_data -= lenmss;
1604
1605 wil_dbg_txrx(wil,
1606 "TSO: len %d, rem_data %d, descs_used %d, sg_desc_cnt %d,\n",
1607 len, rem_data, descs_used, sg_desc_cnt);
1608
1609 /* Close the segment if reached mss size or last frag*/
1610 if (rem_data == 0 || (f == nr_frags - 1 && len == 0)) {
1611 if (hdr_compensation_need) {
1612 /* first segment include hdr desc for
1613 * release
1614 */
1615 hdr_ctx->nr_frags = sg_desc_cnt;
1616 wil_tx_desc_set_nr_frags(first_desc,
1617 sg_desc_cnt +
1618 1);
1619 hdr_compensation_need = false;
1620 } else {
1621 wil_tx_desc_set_nr_frags(first_desc,
1622 sg_desc_cnt);
1623 }
1624 first_ctx->nr_frags = sg_desc_cnt - 1;
1625
1626 wil_tx_last_desc(d);
1627
1628 /* first descriptor may also be the last
1629 * for this mss - make sure not to copy
1630 * it twice
1631 */
1632 if (first_desc != d)
1633 *_first_desc = *first_desc;
1634
1635 /*last descriptor will be copied at the end
1636 * of this TS processing
1637 */
1638 if (f < nr_frags - 1 || len > 0)
1639 *_desc = *d;
1640
1641 rem_data = mss;
1642 _first_desc = NULL;
1643 sg_desc_cnt = 0;
1644 } else if (first_desc != d) /* update mid descriptor */
1645 *_desc = *d;
1646 }
1647 }
1648
1649 /* first descriptor may also be the last.
1650 * in this case d pointer is invalid
1651 */
1652 if (_first_desc == _desc)
1653 d = first_desc;
1654
1655 /* Last data descriptor */
1656 wil_set_tx_desc_last_tso(d);
1657 *_desc = *d;
1658
1659 /* Fill the total number of descriptors in first desc (hdr)*/
1660 wil_tx_desc_set_nr_frags(hdr_desc, descs_used);
1661 *_hdr_desc = *hdr_desc;
1662
1663 /* hold reference to skb
1664 * to prevent skb release before accounting
1665 * in case of immediate "tx done"
1666 */
1667 vring->ctx[i].skb = skb_get(skb);
1668
1669 /* performance monitoring */
1670 used = wil_vring_used_tx(vring);
Gidon Studinski1c9a1932017-08-21 22:55:15 +03001671 if (wil_val_in_range(wil->vring_idle_trsh,
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001672 used, used + descs_used)) {
1673 txdata->idle += get_cycles() - txdata->last_idle;
1674 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1675 vring_index, used, used + descs_used);
1676 }
1677
Maya Erezeb26cff2016-05-16 22:23:30 +03001678 /* Make sure to advance the head only after descriptor update is done.
1679 * This will prevent a race condition where the completion thread
1680 * will see the DU bit set from previous run and will handle the
1681 * skb before it was completed.
1682 */
1683 wmb();
1684
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001685 /* advance swhead */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001686 wil_vring_advance_head(vring, descs_used);
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001687 wil_dbg_txrx(wil, "TSO: Tx swhead %d -> %d\n", swhead, vring->swhead);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001688
1689 /* make sure all writes to descriptors (shared memory) are done before
1690 * committing them to HW
1691 */
1692 wmb();
1693
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +03001694 wil_w(wil, vring->hwtail, vring->swhead);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001695 return 0;
1696
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001697mem_error:
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001698 while (descs_used > 0) {
1699 struct wil_ctx *ctx;
1700
Maya Ereza1526f72016-05-16 22:23:33 +03001701 i = (swhead + descs_used - 1) % vring->size;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001702 d = (struct vring_tx_desc *)&vring->va[i].tx;
1703 _desc = &vring->va[i].tx;
1704 *d = *_desc;
1705 _desc->dma.status = TX_DMA_STATUS_DU;
1706 ctx = &vring->ctx[i];
1707 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001708 memset(ctx, 0, sizeof(*ctx));
1709 descs_used--;
1710 }
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001711err_exit:
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001712 return rc;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001713}
1714
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001715static int __wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1716 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001717{
1718 struct device *dev = wil_to_dev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001719 struct vring_tx_desc dd, *d = &dd;
1720 volatile struct vring_tx_desc *_d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001721 u32 swhead = vring->swhead;
1722 int avail = wil_vring_avail_tx(vring);
1723 int nr_frags = skb_shinfo(skb)->nr_frags;
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001724 uint f = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001725 int vring_index = vring - wil->vring_tx;
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001726 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001727 uint i = swhead;
1728 dma_addr_t pa;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001729 int used;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001730 bool mcast = (vring_index == wil->bcast_vring);
1731 uint len = skb_headlen(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001732
Lazar Alexei3190cc62017-02-23 14:34:14 +02001733 wil_dbg_txrx(wil, "tx_vring: %d bytes to vring %d\n", skb->len,
1734 vring_index);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001735
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001736 if (unlikely(!txdata->enabled))
1737 return -EINVAL;
1738
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001739 if (unlikely(avail < 1 + nr_frags)) {
Vladimir Kondratiev70801e12014-12-01 15:36:03 +02001740 wil_err_ratelimited(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001741 "Tx ring[%2d] full. No space for %d fragments\n",
1742 vring_index, 1 + nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001743 return -ENOMEM;
1744 }
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001745 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001746
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001747 pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001748
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001749 wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index,
1750 skb_headlen(skb), skb->data, &pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001751 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001752 skb->data, skb_headlen(skb), false);
1753
1754 if (unlikely(dma_mapping_error(dev, pa)))
1755 return -EINVAL;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001756 vring->ctx[i].mapped_as = wil_mapped_as_single;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001757 /* 1-st segment */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001758 wil_tx_desc_map(d, pa, len, vring_index);
1759 if (unlikely(mcast)) {
1760 d->mac.d[0] |= BIT(MAC_CFG_DESC_TX_0_MCS_EN_POS); /* MCS 0 */
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001761 if (unlikely(len > WIL_BCAST_MCS0_LIMIT)) /* set MCS 1 */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001762 d->mac.d[0] |= (1 << MAC_CFG_DESC_TX_0_MCS_INDEX_POS);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001763 }
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001764 /* Process TCP/UDP checksum offloading */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001765 if (unlikely(wil_tx_desc_offload_setup(d, skb))) {
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001766 wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001767 vring_index);
1768 goto dma_error;
1769 }
1770
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001771 vring->ctx[i].nr_frags = nr_frags;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001772 wil_tx_desc_set_nr_frags(d, nr_frags + 1);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001773
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001774 /* middle segments */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001775 for (; f < nr_frags; f++) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001776 const struct skb_frag_struct *frag =
1777 &skb_shinfo(skb)->frags[f];
1778 int len = skb_frag_size(frag);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001779
Vladimir Kondratieve59d16c2015-02-01 10:55:12 +02001780 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001781 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1782 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1783 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001784 i = (swhead + f + 1) % vring->size;
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001785 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001786 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001787 DMA_TO_DEVICE);
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001788 if (unlikely(dma_mapping_error(dev, pa))) {
1789 wil_err(wil, "Tx[%2d] failed to map fragment\n",
1790 vring_index);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001791 goto dma_error;
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001792 }
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001793 vring->ctx[i].mapped_as = wil_mapped_as_page;
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001794 wil_tx_desc_map(d, pa, len, vring_index);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001795 /* no need to check return code -
1796 * if it succeeded for 1-st descriptor,
1797 * it will succeed here too
1798 */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001799 wil_tx_desc_offload_setup(d, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001800 }
1801 /* for the last seg only */
1802 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
Kirshenbaum Erez668b2bb2013-06-23 12:59:35 +03001803 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001804 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001805 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001806 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1807 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1808 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001809
Vladimir Kondratiev6cdadd42013-07-11 18:03:41 +03001810 /* hold reference to skb
1811 * to prevent skb release before accounting
1812 * in case of immediate "tx done"
1813 */
1814 vring->ctx[i].skb = skb_get(skb);
1815
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001816 /* performance monitoring */
1817 used = wil_vring_used_tx(vring);
Gidon Studinski1c9a1932017-08-21 22:55:15 +03001818 if (wil_val_in_range(wil->vring_idle_trsh,
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001819 used, used + nr_frags + 1)) {
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001820 txdata->idle += get_cycles() - txdata->last_idle;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001821 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1822 vring_index, used, used + nr_frags + 1);
1823 }
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001824
Maya Erezeb26cff2016-05-16 22:23:30 +03001825 /* Make sure to advance the head only after descriptor update is done.
1826 * This will prevent a race condition where the completion thread
1827 * will see the DU bit set from previous run and will handle the
1828 * skb before it was completed.
1829 */
1830 wmb();
1831
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001832 /* advance swhead */
1833 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001834 wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead,
1835 vring->swhead);
Vladimir Kondratiev98658092013-05-12 14:43:35 +03001836 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001837
1838 /* make sure all writes to descriptors (shared memory) are done before
1839 * committing them to HW
1840 */
1841 wmb();
1842
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +03001843 wil_w(wil, vring->hwtail, vring->swhead);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001844
1845 return 0;
1846 dma_error:
1847 /* unmap what we have mapped */
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001848 nr_frags = f + 1; /* frags mapped + one for skb head */
1849 for (f = 0; f < nr_frags; f++) {
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001850 struct wil_ctx *ctx;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001851
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001852 i = (swhead + f) % vring->size;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001853 ctx = &vring->ctx[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001854 _d = &vring->va[i].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001855 *d = *_d;
1856 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001857 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001858
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001859 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001860 }
1861
1862 return -EINVAL;
1863}
1864
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001865static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1866 struct sk_buff *skb)
1867{
1868 int vring_index = vring - wil->vring_tx;
1869 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1870 int rc;
1871
1872 spin_lock(&txdata->lock);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001873
Maya Ereze3309bf2017-05-15 16:57:46 +03001874 if (test_bit(wil_status_suspending, wil->status) ||
1875 test_bit(wil_status_suspended, wil->status) ||
1876 test_bit(wil_status_resuming, wil->status)) {
1877 wil_dbg_txrx(wil,
1878 "suspend/resume in progress. drop packet\n");
1879 spin_unlock(&txdata->lock);
1880 return -EINVAL;
1881 }
1882
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001883 rc = (skb_is_gso(skb) ? __wil_tx_vring_tso : __wil_tx_vring)
1884 (wil, vring, skb);
1885
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001886 spin_unlock(&txdata->lock);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001887
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001888 return rc;
1889}
1890
Dedy Lansky970a98f2016-12-06 08:26:49 +02001891/**
1892 * Check status of tx vrings and stop/wake net queues if needed
1893 *
1894 * This function does one of two checks:
1895 * In case check_stop is true, will check if net queues need to be stopped. If
1896 * the conditions for stopping are met, netif_tx_stop_all_queues() is called.
1897 * In case check_stop is false, will check if net queues need to be waked. If
1898 * the conditions for waking are met, netif_tx_wake_all_queues() is called.
1899 * vring is the vring which is currently being modified by either adding
1900 * descriptors (tx) into it or removing descriptors (tx complete) from it. Can
1901 * be null when irrelevant (e.g. connect/disconnect events).
1902 *
1903 * The implementation is to stop net queues if modified vring has low
1904 * descriptor availability. Wake if all vrings are not in low descriptor
1905 * availability and modified vring has high descriptor availability.
1906 */
1907static inline void __wil_update_net_queues(struct wil6210_priv *wil,
1908 struct vring *vring,
1909 bool check_stop)
1910{
1911 int i;
1912
1913 if (vring)
1914 wil_dbg_txrx(wil, "vring %d, check_stop=%d, stopped=%d",
1915 (int)(vring - wil->vring_tx), check_stop,
1916 wil->net_queue_stopped);
1917 else
1918 wil_dbg_txrx(wil, "check_stop=%d, stopped=%d",
1919 check_stop, wil->net_queue_stopped);
1920
1921 if (check_stop == wil->net_queue_stopped)
1922 /* net queues already in desired state */
1923 return;
1924
1925 if (check_stop) {
1926 if (!vring || unlikely(wil_vring_avail_low(vring))) {
1927 /* not enough room in the vring */
1928 netif_tx_stop_all_queues(wil_to_ndev(wil));
1929 wil->net_queue_stopped = true;
1930 wil_dbg_txrx(wil, "netif_tx_stop called\n");
1931 }
1932 return;
1933 }
1934
Maya Ereza850af72017-05-29 10:26:13 +03001935 /* Do not wake the queues in suspend flow */
1936 if (test_bit(wil_status_suspending, wil->status) ||
1937 test_bit(wil_status_suspended, wil->status))
1938 return;
1939
Dedy Lansky970a98f2016-12-06 08:26:49 +02001940 /* check wake */
1941 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1942 struct vring *cur_vring = &wil->vring_tx[i];
1943 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
1944
1945 if (!cur_vring->va || !txdata->enabled || cur_vring == vring)
1946 continue;
1947
1948 if (wil_vring_avail_low(cur_vring)) {
1949 wil_dbg_txrx(wil, "vring %d full, can't wake\n",
1950 (int)(cur_vring - wil->vring_tx));
1951 return;
1952 }
1953 }
1954
1955 if (!vring || wil_vring_avail_high(vring)) {
1956 /* enough room in the vring */
1957 wil_dbg_txrx(wil, "calling netif_tx_wake\n");
1958 netif_tx_wake_all_queues(wil_to_ndev(wil));
1959 wil->net_queue_stopped = false;
1960 }
1961}
1962
1963void wil_update_net_queues(struct wil6210_priv *wil, struct vring *vring,
1964 bool check_stop)
1965{
1966 spin_lock(&wil->net_queue_lock);
1967 __wil_update_net_queues(wil, vring, check_stop);
1968 spin_unlock(&wil->net_queue_lock);
1969}
1970
1971void wil_update_net_queues_bh(struct wil6210_priv *wil, struct vring *vring,
1972 bool check_stop)
1973{
1974 spin_lock_bh(&wil->net_queue_lock);
1975 __wil_update_net_queues(wil, vring, check_stop);
1976 spin_unlock_bh(&wil->net_queue_lock);
1977}
1978
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001979netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1980{
1981 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001982 struct ethhdr *eth = (void *)skb->data;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001983 bool bcast = is_multicast_ether_addr(eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001984 struct vring *vring;
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001985 static bool pr_once_fw;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001986 int rc;
1987
Lazar Alexei3190cc62017-02-23 14:34:14 +02001988 wil_dbg_txrx(wil, "start_xmit\n");
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001989 if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001990 if (!pr_once_fw) {
1991 wil_err(wil, "FW not ready\n");
1992 pr_once_fw = true;
1993 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001994 goto drop;
1995 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001996 if (unlikely(!test_bit(wil_status_fwconnected, wil->status))) {
Maya Erezd8ed0432016-04-26 14:41:39 +03001997 wil_dbg_ratelimited(wil, "FW not connected, packet dropped\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001998 goto drop;
1999 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002000 if (unlikely(wil->wdev->iftype == NL80211_IFTYPE_MONITOR)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002001 wil_err(wil, "Xmit in monitor mode not supported\n");
2002 goto drop;
2003 }
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02002004 pr_once_fw = false;
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03002005
2006 /* find vring */
Lior Davidb1c0bbf2017-02-08 13:14:13 +02002007 if (wil->wdev->iftype == NL80211_IFTYPE_STATION && !wil->pbss) {
2008 /* in STA mode (ESS), all to same VRING (to AP) */
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02002009 vring = wil_find_tx_vring_sta(wil, skb);
Lior Davidb1c0bbf2017-02-08 13:14:13 +02002010 } else if (bcast) {
2011 if (wil->pbss)
2012 /* in pbss, no bcast VRING - duplicate skb in
2013 * all stations VRINGs
2014 */
2015 vring = wil_find_tx_bcast_2(wil, skb);
2016 else if (wil->wdev->iftype == NL80211_IFTYPE_AP)
2017 /* AP has a dedicated bcast VRING */
2018 vring = wil_find_tx_bcast_1(wil, skb);
2019 else
2020 /* unexpected combination, fallback to duplicating
2021 * the skb in all stations VRINGs
2022 */
2023 vring = wil_find_tx_bcast_2(wil, skb);
2024 } else {
2025 /* unicast, find specific VRING by dest. address */
2026 vring = wil_find_tx_ucast(wil, skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02002027 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002028 if (unlikely(!vring)) {
Vladimir Kondratiev5aed1392014-06-16 19:37:15 +03002029 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03002030 goto drop;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002031 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03002032 /* set up vring entry */
2033 rc = wil_tx_vring(wil, vring, skb);
2034
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002035 switch (rc) {
2036 case 0:
Dedy Lansky970a98f2016-12-06 08:26:49 +02002037 /* shall we stop net queues? */
2038 wil_update_net_queues_bh(wil, vring, true);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02002039 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002040 dev_kfree_skb_any(skb);
2041 return NETDEV_TX_OK;
2042 case -ENOMEM:
2043 return NETDEV_TX_BUSY;
2044 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +02002045 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002046 }
2047 drop:
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002048 ndev->stats.tx_dropped++;
2049 dev_kfree_skb_any(skb);
2050
2051 return NET_XMIT_DROP;
2052}
2053
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02002054static inline bool wil_need_txstat(struct sk_buff *skb)
2055{
2056 struct ethhdr *eth = (void *)skb->data;
2057
2058 return is_unicast_ether_addr(eth->h_dest) && skb->sk &&
2059 (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS);
2060}
2061
2062static inline void wil_consume_skb(struct sk_buff *skb, bool acked)
2063{
2064 if (unlikely(wil_need_txstat(skb)))
2065 skb_complete_wifi_ack(skb, acked);
2066 else
2067 acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb);
2068}
2069
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002070/**
2071 * Clean up transmitted skb's from the Tx VRING
2072 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002073 * Return number of descriptors cleared
2074 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002075 * Safe to call from IRQ
2076 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002077int wil_tx_complete(struct wil6210_priv *wil, int ringid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002078{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02002079 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002080 struct device *dev = wil_to_dev(wil);
2081 struct vring *vring = &wil->vring_tx[ringid];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02002082 struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002083 int done = 0;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +02002084 int cid = wil->vring2cid_tid[ringid][0];
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002085 struct wil_net_stats *stats = NULL;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002086 volatile struct vring_tx_desc *_d;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002087 int used_before_complete;
2088 int used_new;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002089
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002090 if (unlikely(!vring->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002091 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002092 return 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002093 }
2094
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002095 if (unlikely(!txdata->enabled)) {
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02002096 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
2097 return 0;
2098 }
2099
Lazar Alexei3190cc62017-02-23 14:34:14 +02002100 wil_dbg_txrx(wil, "tx_complete: (%d)\n", ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002101
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002102 used_before_complete = wil_vring_used_tx(vring);
2103
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002104 if (cid < WIL6210_MAX_CID)
2105 stats = &wil->sta[cid].stats;
2106
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002107 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002108 int new_swtail;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03002109 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002110 /**
2111 * For the fragmented skb, HW will set DU bit only for the
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03002112 * last fragment. look for it.
2113 * In TSO the first DU will include hdr desc
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002114 */
2115 int lf = (vring->swtail + ctx->nr_frags) % vring->size;
2116 /* TODO: check we are not past head */
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +03002117
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002118 _d = &vring->va[lf].tx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002119 if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002120 break;
2121
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002122 new_swtail = (lf + 1) % vring->size;
2123 while (vring->swtail != new_swtail) {
2124 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002125 u16 dmalen;
Vladimir Kondratiev76dfa4b2014-07-14 09:49:39 +03002126 struct sk_buff *skb;
2127
2128 ctx = &vring->ctx[vring->swtail];
2129 skb = ctx->skb;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002130 _d = &vring->va[vring->swtail].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002131
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002132 *d = *_d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03002133
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002134 dmalen = le16_to_cpu(d->dma.length);
2135 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
2136 d->dma.error);
2137 wil_dbg_txrx(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02002138 "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
2139 ringid, vring->swtail, dmalen,
2140 d->dma.status, d->dma.error);
2141 wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002142 (const void *)d, sizeof(*d), false);
2143
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02002144 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002145
2146 if (skb) {
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002147 if (likely(d->dma.error == 0)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002148 ndev->stats.tx_packets++;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002149 ndev->stats.tx_bytes += skb->len;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002150 if (stats) {
2151 stats->tx_packets++;
2152 stats->tx_bytes += skb->len;
2153 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002154 } else {
2155 ndev->stats.tx_errors++;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002156 if (stats)
2157 stats->tx_errors++;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002158 }
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02002159 wil_consume_skb(skb, d->dma.error == 0);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02002160 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002161 memset(ctx, 0, sizeof(*ctx));
Maya Erezeb26cff2016-05-16 22:23:30 +03002162 /* Make sure the ctx is zeroed before updating the tail
2163 * to prevent a case where wil_tx_vring will see
2164 * this descriptor as used and handle it before ctx zero
2165 * is completed.
2166 */
2167 wmb();
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002168 /* There is no need to touch HW descriptor:
2169 * - ststus bit TX_DMA_STATUS_DU is set by design,
2170 * so hardware will not try to process this desc.,
2171 * - rest of descriptor will be initialized on Tx.
2172 */
2173 vring->swtail = wil_vring_next_tail(vring);
2174 done++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002175 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002176 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03002177
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002178 /* performance monitoring */
2179 used_new = wil_vring_used_tx(vring);
Gidon Studinski1c9a1932017-08-21 22:55:15 +03002180 if (wil_val_in_range(wil->vring_idle_trsh,
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002181 used_new, used_before_complete)) {
2182 wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
2183 ringid, used_before_complete, used_new);
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03002184 txdata->last_idle = get_cycles();
2185 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03002186
Dedy Lansky970a98f2016-12-06 08:26:49 +02002187 /* shall we wake net queues? */
2188 if (done)
2189 wil_update_net_queues(wil, vring, false);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002190
2191 return done;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002192}