blob: b0166d22450897958365eeec625425c3ac9cfefc [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
Maya Erezb729aaf2016-01-17 12:39:09 +02002 * Copyright (c) 2012-2016 Qualcomm Atheros, Inc.
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08003 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080017#include <linux/etherdevice.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080018#include <net/ieee80211_radiotap.h>
19#include <linux/if_arp.h>
20#include <linux/moduleparam.h>
Kirshenbaum Erez504937d2013-07-21 11:34:37 +030021#include <linux/ip.h>
22#include <linux/ipv6.h>
23#include <net/ipv6.h>
Vladimir Kondratiev0786dc42014-01-14 20:31:26 +020024#include <linux/prefetch.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080025
26#include "wil6210.h"
27#include "wmi.h"
28#include "txrx.h"
Vladimir Kondratiev98658092013-05-12 14:43:35 +030029#include "trace.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080030
31static bool rtap_include_phy_info;
32module_param(rtap_include_phy_info, bool, S_IRUGO);
33MODULE_PARM_DESC(rtap_include_phy_info,
34 " Include PHY info in the radiotap header, default - no");
35
Vladimir Kondratievc406ea72015-03-15 16:00:19 +020036bool rx_align_2;
37module_param(rx_align_2, bool, S_IRUGO);
38MODULE_PARM_DESC(rx_align_2, " align Rx buffers on 4*n+2, default - no");
39
40static inline uint wil_rx_snaplen(void)
41{
42 return rx_align_2 ? 6 : 0;
43}
44
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080045static inline int wil_vring_is_empty(struct vring *vring)
46{
47 return vring->swhead == vring->swtail;
48}
49
50static inline u32 wil_vring_next_tail(struct vring *vring)
51{
52 return (vring->swtail + 1) % vring->size;
53}
54
55static inline void wil_vring_advance_head(struct vring *vring, int n)
56{
57 vring->swhead = (vring->swhead + n) % vring->size;
58}
59
60static inline int wil_vring_is_full(struct vring *vring)
61{
62 return wil_vring_next_tail(vring) == vring->swhead;
63}
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +030064
Vladimir Shulman0436fd92015-02-15 14:02:34 +020065/* Used space in Tx Vring */
66static inline int wil_vring_used_tx(struct vring *vring)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080067{
68 u32 swhead = vring->swhead;
69 u32 swtail = vring->swtail;
Vladimir Shulman0436fd92015-02-15 14:02:34 +020070 return (vring->size + swhead - swtail) % vring->size;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080071}
72
Vladimir Shulman0436fd92015-02-15 14:02:34 +020073/* Available space in Tx Vring */
74static inline int wil_vring_avail_tx(struct vring *vring)
75{
76 return vring->size - wil_vring_used_tx(vring) - 1;
77}
78
79/* wil_vring_wmark_low - low watermark for available descriptor space */
Vladimir Kondratiev5bb64232014-05-27 14:45:46 +030080static inline int wil_vring_wmark_low(struct vring *vring)
81{
82 return vring->size/8;
83}
84
Vladimir Shulman0436fd92015-02-15 14:02:34 +020085/* wil_vring_wmark_high - high watermark for available descriptor space */
Vladimir Kondratiev5bb64232014-05-27 14:45:46 +030086static inline int wil_vring_wmark_high(struct vring *vring)
87{
88 return vring->size/4;
89}
90
Dedy Lansky970a98f2016-12-06 08:26:49 +020091/* returns true if num avail descriptors is lower than wmark_low */
92static inline int wil_vring_avail_low(struct vring *vring)
93{
94 return wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring);
95}
96
97/* returns true if num avail descriptors is higher than wmark_high */
98static inline int wil_vring_avail_high(struct vring *vring)
99{
100 return wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring);
101}
102
Vladimir Shulman0436fd92015-02-15 14:02:34 +0200103/* wil_val_in_range - check if value in [min,max) */
104static inline bool wil_val_in_range(int val, int min, int max)
105{
106 return val >= min && val < max;
107}
108
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800109static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
110{
111 struct device *dev = wil_to_dev(wil);
112 size_t sz = vring->size * sizeof(vring->va[0]);
113 uint i;
114
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300115 wil_dbg_misc(wil, "%s()\n", __func__);
116
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800117 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
118
119 vring->swhead = 0;
120 vring->swtail = 0;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300121 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800122 if (!vring->ctx) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800123 vring->va = NULL;
124 return -ENOMEM;
125 }
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200126
Vladimir Shulman0436fd92015-02-15 14:02:34 +0200127 /* vring->va should be aligned on its size rounded up to power of 2
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200128 * This is granted by the dma_alloc_coherent.
129 *
130 * HW has limitation that all vrings addresses must share the same
131 * upper 16 msb bits part of 48 bits address. To workaround that,
132 * if we are using 48 bit addresses switch to 32 bit allocation
133 * before allocating vring memory.
134 *
135 * There's no check for the return value of dma_set_mask_and_coherent,
136 * since we assume if we were able to set the mask during
137 * initialization in this system it will not fail if we set it again
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800138 */
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200139 if (wil->use_extended_dma_addr)
140 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
141
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800142 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
143 if (!vring->va) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800144 kfree(vring->ctx);
145 vring->ctx = NULL;
146 return -ENOMEM;
147 }
Hamad Kadmany8b5dc9c2017-02-16 10:59:10 +0200148
149 if (wil->use_extended_dma_addr)
150 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
151
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800152 /* initially, all descriptors are SW owned
153 * For Tx and Rx, ownership bit is at the same location, thus
154 * we can use any
155 */
156 for (i = 0; i < vring->size; i++) {
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300157 volatile struct vring_tx_desc *_d = &vring->va[i].tx;
158
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300159 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800160 }
161
Vladimir Kondratiev39c52ee2014-05-27 14:45:49 +0300162 wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
163 vring->va, &vring->pa, vring->ctx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800164
165 return 0;
166}
167
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200168static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d,
169 struct wil_ctx *ctx)
170{
171 dma_addr_t pa = wil_desc_addr(&d->dma.addr);
172 u16 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300173
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200174 switch (ctx->mapped_as) {
175 case wil_mapped_as_single:
176 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
177 break;
178 case wil_mapped_as_page:
179 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
180 break;
181 default:
182 break;
183 }
184}
185
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800186static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
187 int tx)
188{
189 struct device *dev = wil_to_dev(wil);
190 size_t sz = vring->size * sizeof(vring->va[0]);
191
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +0200192 lockdep_assert_held(&wil->mutex);
Vladimir Kondratievef772852014-09-10 16:34:31 +0300193 if (tx) {
194 int vring_index = vring - wil->vring_tx;
195
196 wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
197 vring_index, vring->size, vring->va,
198 &vring->pa, vring->ctx);
199 } else {
200 wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
201 vring->size, vring->va,
202 &vring->pa, vring->ctx);
203 }
204
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800205 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300206 dma_addr_t pa;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300207 u16 dmalen;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300208 struct wil_ctx *ctx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300209
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800210 if (tx) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300211 struct vring_tx_desc dd, *d = &dd;
212 volatile struct vring_tx_desc *_d =
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800213 &vring->va[vring->swtail].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300214
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300215 ctx = &vring->ctx[vring->swtail];
Maya Erez34b88862016-05-16 22:23:32 +0300216 if (!ctx) {
217 wil_dbg_txrx(wil,
218 "ctx(%d) was already completed\n",
219 vring->swtail);
220 vring->swtail = wil_vring_next_tail(vring);
221 continue;
222 }
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300223 *d = *_d;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +0200224 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300225 if (ctx->skb)
226 dev_kfree_skb_any(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800227 vring->swtail = wil_vring_next_tail(vring);
228 } else { /* rx */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300229 struct vring_rx_desc dd, *d = &dd;
230 volatile struct vring_rx_desc *_d =
Vladimir Kondratiev4d1ac072013-07-11 18:03:38 +0300231 &vring->va[vring->swhead].rx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300232
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300233 ctx = &vring->ctx[vring->swhead];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300234 *d = *_d;
235 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300236 dmalen = le16_to_cpu(d->dma.length);
237 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300238 kfree_skb(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800239 wil_vring_advance_head(vring, 1);
240 }
241 }
242 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
243 kfree(vring->ctx);
244 vring->pa = 0;
245 vring->va = NULL;
246 vring->ctx = NULL;
247}
248
249/**
250 * Allocate one skb for Rx VRING
251 *
252 * Safe to call from IRQ
253 */
254static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
255 u32 i, int headroom)
256{
257 struct device *dev = wil_to_dev(wil);
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200258 unsigned int sz = mtu_max + ETH_HLEN + wil_rx_snaplen();
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300259 struct vring_rx_desc dd, *d = &dd;
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300260 volatile struct vring_rx_desc *_d = &vring->va[i].rx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800261 dma_addr_t pa;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800262 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300263
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800264 if (unlikely(!skb))
265 return -ENOMEM;
266
267 skb_reserve(skb, headroom);
268 skb_put(skb, sz);
269
270 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
271 if (unlikely(dma_mapping_error(dev, pa))) {
272 kfree_skb(skb);
273 return -ENOMEM;
274 }
275
Vladimir Kondratiev48c963a2015-04-30 16:25:06 +0300276 d->dma.d0 = RX_DMA_D0_CMD_DMA_RT | RX_DMA_D0_CMD_DMA_IT;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300277 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800278 /* ip_length don't care */
279 /* b11 don't care */
280 /* error don't care */
281 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300282 d->dma.length = cpu_to_le16(sz);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300283 *_d = *d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300284 vring->ctx[i].skb = skb;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800285
286 return 0;
287}
288
289/**
290 * Adds radiotap header
291 *
292 * Any error indicated as "Bad FCS"
293 *
294 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
295 * - Rx descriptor: 32 bytes
296 * - Phy info
297 */
298static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300299 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800300{
301 struct wireless_dev *wdev = wil->wdev;
302 struct wil6210_rtap {
303 struct ieee80211_radiotap_header rthdr;
304 /* fields should be in the order of bits in rthdr.it_present */
305 /* flags */
306 u8 flags;
307 /* channel */
308 __le16 chnl_freq __aligned(2);
309 __le16 chnl_flags;
310 /* MCS */
311 u8 mcs_present;
312 u8 mcs_flags;
313 u8 mcs_index;
314 } __packed;
315 struct wil6210_rtap_vendor {
316 struct wil6210_rtap rtap;
317 /* vendor */
318 u8 vendor_oui[3] __aligned(2);
319 u8 vendor_ns;
320 __le16 vendor_skip;
321 u8 vendor_data[0];
322 } __packed;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300323 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800324 struct wil6210_rtap_vendor *rtap_vendor;
325 int rtap_len = sizeof(struct wil6210_rtap);
326 int phy_length = 0; /* phy info header size, bytes */
327 static char phy_data[128];
328 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
329
330 if (rtap_include_phy_info) {
331 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
332 /* calculate additional length */
333 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
334 /**
335 * PHY info starts from 8-byte boundary
336 * there are 8-byte lines, last line may be partially
337 * written (HW bug), thus FW configures for last line
338 * to be excessive. Driver skips this last line.
339 */
340 int len = min_t(int, 8 + sizeof(phy_data),
341 wil_rxdesc_phy_length(d));
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300342
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800343 if (len > 8) {
344 void *p = skb_tail_pointer(skb);
345 void *pa = PTR_ALIGN(p, 8);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +0300346
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800347 if (skb_tailroom(skb) >= len + (pa - p)) {
348 phy_length = len - 8;
349 memcpy(phy_data, pa, phy_length);
350 }
351 }
352 }
353 rtap_len += phy_length;
354 }
355
356 if (skb_headroom(skb) < rtap_len &&
357 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
358 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
359 return;
360 }
361
362 rtap_vendor = (void *)skb_push(skb, rtap_len);
363 memset(rtap_vendor, 0, rtap_len);
364
365 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
366 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
367 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
368 (1 << IEEE80211_RADIOTAP_FLAGS) |
369 (1 << IEEE80211_RADIOTAP_CHANNEL) |
370 (1 << IEEE80211_RADIOTAP_MCS));
371 if (d->dma.status & RX_DMA_STATUS_ERROR)
372 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
373
374 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
375 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
376
377 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
378 rtap_vendor->rtap.mcs_flags = 0;
379 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
380
381 if (rtap_include_phy_info) {
382 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
383 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
384 /* OUI for Wilocity 04:ce:14 */
385 rtap_vendor->vendor_oui[0] = 0x04;
386 rtap_vendor->vendor_oui[1] = 0xce;
387 rtap_vendor->vendor_oui[2] = 0x14;
388 rtap_vendor->vendor_ns = 1;
389 /* Rx descriptor + PHY data */
390 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
391 phy_length);
392 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
393 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
394 phy_length);
395 }
396}
397
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300398/* similar to ieee80211_ version, but FC contain only 1-st byte */
399static inline int wil_is_back_req(u8 fc)
400{
401 return (fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) ==
402 (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK_REQ);
403}
404
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800405/**
406 * reap 1 frame from @swhead
407 *
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300408 * Rx descriptor copied to skb->cb
409 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800410 * Safe to call from IRQ
411 */
412static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
413 struct vring *vring)
414{
415 struct device *dev = wil_to_dev(wil);
416 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300417 volatile struct vring_rx_desc *_d;
418 struct vring_rx_desc *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800419 struct sk_buff *skb;
420 dma_addr_t pa;
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200421 unsigned int snaplen = wil_rx_snaplen();
422 unsigned int sz = mtu_max + ETH_HLEN + snaplen;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300423 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800424 u8 ftype;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200425 int cid;
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300426 int i;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200427 struct wil_net_stats *stats;
428
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300429 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
430
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300431again:
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200432 if (unlikely(wil_vring_is_empty(vring)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800433 return NULL;
434
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300435 i = (int)vring->swhead;
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200436 _d = &vring->va[i].rx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200437 if (unlikely(!(_d->dma.status & RX_DMA_STATUS_DU))) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800438 /* it is not error, we just reached end of Rx done area */
439 return NULL;
440 }
441
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200442 skb = vring->ctx[i].skb;
443 vring->ctx[i].skb = NULL;
444 wil_vring_advance_head(vring, 1);
445 if (!skb) {
446 wil_err(wil, "No Rx skb at [%d]\n", i);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300447 goto again;
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200448 }
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300449 d = wil_skb_rxdesc(skb);
450 *d = *_d;
451 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300452
453 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
454 dmalen = le16_to_cpu(d->dma.length);
455
Vladimir Kondratiev148416a2015-03-15 16:00:16 +0200456 trace_wil6210_rx(i, d);
457 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", i, dmalen);
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300458 wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300459 (const void *)d, sizeof(*d), false);
460
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300461 cid = wil_rxdesc_cid(d);
462 stats = &wil->sta[cid].stats;
463
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200464 if (unlikely(dmalen > sz)) {
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300465 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300466 stats->rx_large_frame++;
Wei Yongjun110dea02013-05-23 17:10:43 +0800467 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300468 goto again;
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300469 }
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300470 skb_trim(skb, dmalen);
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300471
Vladimir Kondratiev1cbbcb02014-01-08 11:50:49 +0200472 prefetch(skb->data);
473
Vladimir Kondratievc0d37712013-05-12 14:43:34 +0300474 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
475 skb->data, skb_headlen(skb), false);
476
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200477 stats->last_mcs_rx = wil_rxdesc_mcs(d);
Vladimir Kondratievc4a110d2015-06-09 14:11:18 +0300478 if (stats->last_mcs_rx < ARRAY_SIZE(stats->rx_per_mcs))
479 stats->rx_per_mcs[stats->last_mcs_rx]++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800480
481 /* use radiotap header only if required */
482 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300483 wil_rx_add_radiotap_header(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800484
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800485 /* no extra checks if in sniffer mode */
486 if (ndev->type != ARPHRD_ETHER)
487 return skb;
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300488 /* Non-data frames may be delivered through Rx DMA channel (ex: BAR)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800489 * Driver should recognize it by frame type, that is found
490 * in Rx descriptor. If type is not data, it is 802.11 frame as is
491 */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300492 ftype = wil_rxdesc_ftype(d) << 2;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200493 if (unlikely(ftype != IEEE80211_FTYPE_DATA)) {
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300494 u8 fc1 = wil_rxdesc_fc1(d);
495 int mid = wil_rxdesc_mid(d);
496 int tid = wil_rxdesc_tid(d);
497 u16 seq = wil_rxdesc_seq(d);
498
499 wil_dbg_txrx(wil,
500 "Non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
501 fc1, mid, cid, tid, seq);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300502 stats->rx_non_data_frame++;
Vladimir Kondratieva8313342015-10-04 10:23:23 +0300503 if (wil_is_back_req(fc1)) {
504 wil_dbg_txrx(wil,
505 "BAR: MID %d CID %d TID %d Seq 0x%03x\n",
506 mid, cid, tid, seq);
507 wil_rx_bar(wil, cid, tid, seq);
508 } else {
509 /* print again all info. One can enable only this
510 * without overhead for printing every Rx frame
511 */
512 wil_dbg_txrx(wil,
513 "Unhandled non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
514 fc1, mid, cid, tid, seq);
515 wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
516 (const void *)d, sizeof(*d), false);
517 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
518 skb->data, skb_headlen(skb), false);
519 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800520 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300521 goto again;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800522 }
523
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200524 if (unlikely(skb->len < ETH_HLEN + snaplen)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800525 wil_err(wil, "Short frame, len = %d\n", skb->len);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300526 stats->rx_short_frame++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800527 kfree_skb(skb);
Vladimir Kondratiev3b282bc2015-10-04 10:23:19 +0300528 goto again;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800529 }
530
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300531 /* L4 IDENT is on when HW calculated checksum, check status
532 * and in case of error drop the packet
533 * higher stack layers will handle retransmission (if required)
534 */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200535 if (likely(d->dma.status & RX_DMA_STATUS_L4I)) {
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300536 /* L4 protocol identified, csum calculated */
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200537 if (likely((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0))
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300538 skb->ip_summed = CHECKSUM_UNNECESSARY;
Vladimir Kondratiev4a68ab102013-08-13 15:25:32 +0300539 /* If HW reports bad checksum, let IP stack re-check it
540 * For example, HW don't understand Microsoft IP stack that
541 * mis-calculates TCP checksum - if it should be 0x0,
542 * it writes 0xffff in violation of RFC 1624
543 */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300544 }
545
Vladimir Kondratievc406ea72015-03-15 16:00:19 +0200546 if (snaplen) {
547 /* Packet layout
548 * +-------+-------+---------+------------+------+
549 * | SA(6) | DA(6) | SNAP(6) | ETHTYPE(2) | DATA |
550 * +-------+-------+---------+------------+------+
551 * Need to remove SNAP, shifting SA and DA forward
552 */
553 memmove(skb->data + snaplen, skb->data, 2 * ETH_ALEN);
554 skb_pull(skb, snaplen);
555 }
556
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800557 return skb;
558}
559
560/**
561 * allocate and fill up to @count buffers in rx ring
562 * buffers posted at @swtail
563 */
564static int wil_rx_refill(struct wil6210_priv *wil, int count)
565{
566 struct net_device *ndev = wil_to_ndev(wil);
567 struct vring *v = &wil->vring_rx;
568 u32 next_tail;
569 int rc = 0;
570 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
571 WIL6210_RTAP_SIZE : 0;
572
573 for (; next_tail = wil_vring_next_tail(v),
574 (next_tail != v->swhead) && (count-- > 0);
575 v->swtail = next_tail) {
576 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200577 if (unlikely(rc)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800578 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
579 rc, v->swtail);
580 break;
581 }
582 }
Maya Erezab6d7cc2016-05-16 22:23:31 +0300583
584 /* make sure all writes to descriptors (shared memory) are done before
585 * committing them to HW
586 */
587 wmb();
588
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +0300589 wil_w(wil, v->hwtail, v->swtail);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800590
591 return rc;
592}
593
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200594/**
595 * reverse_memcmp - Compare two areas of memory, in reverse order
596 * @cs: One area of memory
597 * @ct: Another area of memory
598 * @count: The size of the area.
599 *
600 * Cut'n'paste from original memcmp (see lib/string.c)
601 * with minimal modifications
602 */
603static int reverse_memcmp(const void *cs, const void *ct, size_t count)
604{
605 const unsigned char *su1, *su2;
606 int res = 0;
607
608 for (su1 = cs + count - 1, su2 = ct + count - 1; count > 0;
609 --su1, --su2, count--) {
610 res = *su1 - *su2;
611 if (res)
612 break;
613 }
614 return res;
615}
616
617static int wil_rx_crypto_check(struct wil6210_priv *wil, struct sk_buff *skb)
618{
619 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
620 int cid = wil_rxdesc_cid(d);
621 int tid = wil_rxdesc_tid(d);
622 int key_id = wil_rxdesc_key_id(d);
623 int mc = wil_rxdesc_mcast(d);
624 struct wil_sta_info *s = &wil->sta[cid];
625 struct wil_tid_crypto_rx *c = mc ? &s->group_crypto_rx :
626 &s->tid_crypto_rx[tid];
627 struct wil_tid_crypto_rx_single *cc = &c->key_id[key_id];
628 const u8 *pn = (u8 *)&d->mac.pn_15_0;
629
630 if (!cc->key_set) {
631 wil_err_ratelimited(wil,
632 "Key missing. CID %d TID %d MCast %d KEY_ID %d\n",
633 cid, tid, mc, key_id);
634 return -EINVAL;
635 }
636
637 if (reverse_memcmp(pn, cc->pn, IEEE80211_GCMP_PN_LEN) <= 0) {
638 wil_err_ratelimited(wil,
639 "Replay attack. CID %d TID %d MCast %d KEY_ID %d PN %6phN last %6phN\n",
640 cid, tid, mc, key_id, pn, cc->pn);
641 return -EINVAL;
642 }
643 memcpy(cc->pn, pn, IEEE80211_GCMP_PN_LEN);
644
645 return 0;
646}
647
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800648/*
649 * Pass Rx packet to the netif. Update statistics.
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300650 * Called in softirq context (NAPI poll).
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800651 */
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200652void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800653{
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200654 gro_result_t rc = GRO_NORMAL;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200655 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200656 struct wireless_dev *wdev = wil_to_wdev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800657 unsigned int len = skb->len;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200658 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200659 int cid = wil_rxdesc_cid(d); /* always 0..7, no need to check */
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200660 int security = wil_rxdesc_security(d);
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200661 struct ethhdr *eth = (void *)skb->data;
662 /* here looking for DA, not A1, thus Rxdesc's 'mcast' indication
663 * is not suitable, need to look at data
664 */
665 int mcast = is_multicast_ether_addr(eth->h_dest);
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200666 struct wil_net_stats *stats = &wil->sta[cid].stats;
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200667 struct sk_buff *xmit_skb = NULL;
668 static const char * const gro_res_str[] = {
669 [GRO_MERGED] = "GRO_MERGED",
670 [GRO_MERGED_FREE] = "GRO_MERGED_FREE",
671 [GRO_HELD] = "GRO_HELD",
672 [GRO_NORMAL] = "GRO_NORMAL",
673 [GRO_DROP] = "GRO_DROP",
674 };
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800675
Vladimir Shulman05536402015-07-30 13:52:04 +0300676 if (ndev->features & NETIF_F_RXHASH)
677 /* fake L4 to ensure it won't be re-calculated later
678 * set hash to any non-zero value to activate rps
679 * mechanism, core will be chosen according
680 * to user-level rps configuration.
681 */
682 skb_set_hash(skb, 1, PKT_HASH_TYPE_L4);
683
Vladimir Kondratiev241804c2013-01-28 18:31:02 +0200684 skb_orphan(skb);
685
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200686 if (security && (wil_rx_crypto_check(wil, skb) != 0)) {
687 rc = GRO_DROP;
688 dev_kfree_skb(skb);
689 stats->rx_replay++;
690 goto stats;
691 }
692
Vladimir Kondratiev02beaf12015-03-08 15:42:03 +0200693 if (wdev->iftype == NL80211_IFTYPE_AP && !wil->ap_isolate) {
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200694 if (mcast) {
695 /* send multicast frames both to higher layers in
696 * local net stack and back to the wireless medium
697 */
698 xmit_skb = skb_copy(skb, GFP_ATOMIC);
699 } else {
700 int xmit_cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800701
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200702 if (xmit_cid >= 0) {
703 /* The destination station is associated to
704 * this AP (in this VLAN), so send the frame
705 * directly to it and do not pass it to local
706 * net stack.
707 */
708 xmit_skb = skb;
709 skb = NULL;
710 }
711 }
712 }
713 if (xmit_skb) {
714 /* Send to wireless media and increase priority by 256 to
715 * keep the received priority instead of reclassifying
716 * the frame (see cfg80211_classify8021d).
717 */
718 xmit_skb->dev = ndev;
719 xmit_skb->priority += 256;
720 xmit_skb->protocol = htons(ETH_P_802_3);
721 skb_reset_network_header(xmit_skb);
722 skb_reset_mac_header(xmit_skb);
723 wil_dbg_txrx(wil, "Rx -> Tx %d bytes\n", len);
724 dev_queue_xmit(xmit_skb);
725 }
726
727 if (skb) { /* deliver to local stack */
728
729 skb->protocol = eth_type_trans(skb, ndev);
730 rc = napi_gro_receive(&wil->napi_rx, skb);
731 wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
732 len, gro_res_str[rc]);
733 }
Vladimir Kondratiev58527422016-03-01 19:18:07 +0200734stats:
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200735 /* statistics. rc set to GRO_NORMAL for AP bridging */
Vladimir Kondratievb5998e62014-03-17 15:34:22 +0200736 if (unlikely(rc == GRO_DROP)) {
737 ndev->stats.rx_dropped++;
738 stats->rx_dropped++;
739 wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
740 } else {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800741 ndev->stats.rx_packets++;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200742 stats->rx_packets++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800743 ndev->stats.rx_bytes += len;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +0200744 stats->rx_bytes += len;
Vladimir Kondratievc42da992015-03-08 15:42:02 +0200745 if (mcast)
746 ndev->stats.multicast++;
Vladimir Kondratiev194b4822014-06-16 19:37:14 +0300747 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800748}
749
750/**
751 * Proceed all completed skb's from Rx VRING
752 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300753 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800754 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300755void wil_rx_handle(struct wil6210_priv *wil, int *quota)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800756{
757 struct net_device *ndev = wil_to_ndev(wil);
758 struct vring *v = &wil->vring_rx;
759 struct sk_buff *skb;
760
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +0200761 if (unlikely(!v->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800762 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
763 return;
764 }
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200765 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300766 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
767 (*quota)--;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800768
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800769 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
770 skb->dev = ndev;
771 skb_reset_mac_header(skb);
772 skb->ip_summed = CHECKSUM_UNNECESSARY;
773 skb->pkt_type = PACKET_OTHERHOST;
774 skb->protocol = htons(ETH_P_802_2);
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200775 wil_netif_rx_any(skb, ndev);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800776 } else {
Vladimir Kondratieve4373d82014-12-23 09:47:21 +0200777 wil_rx_reorder(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800778 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800779 }
780 wil_rx_refill(wil, v->size);
781}
782
Vladimir Kondratievd3762b42014-12-01 15:35:02 +0200783int wil_rx_init(struct wil6210_priv *wil, u16 size)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800784{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800785 struct vring *vring = &wil->vring_rx;
786 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800787
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300788 wil_dbg_misc(wil, "%s()\n", __func__);
789
Vladimir Kondratiev8bf6adb2014-03-17 15:34:17 +0200790 if (vring->va) {
791 wil_err(wil, "Rx ring already allocated\n");
792 return -EINVAL;
793 }
794
Vladimir Kondratievd3762b42014-12-01 15:35:02 +0200795 vring->size = size;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800796 rc = wil_vring_alloc(wil, vring);
797 if (rc)
798 return rc;
799
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +0200800 rc = wmi_rx_chain_add(wil, vring);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800801 if (rc)
802 goto err_free;
803
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800804 rc = wil_rx_refill(wil, vring->size);
805 if (rc)
806 goto err_free;
807
808 return 0;
809 err_free:
810 wil_vring_free(wil, vring, 0);
811
812 return rc;
813}
814
815void wil_rx_fini(struct wil6210_priv *wil)
816{
817 struct vring *vring = &wil->vring_rx;
818
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300819 wil_dbg_misc(wil, "%s()\n", __func__);
820
Vladimir Kondratiev2acb4222013-01-28 18:31:08 +0200821 if (vring->va)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800822 wil_vring_free(wil, vring, 0);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800823}
824
Maya Erez875e9432016-01-28 19:24:02 +0200825static inline void wil_tx_data_init(struct vring_tx_data *txdata)
826{
827 spin_lock_bh(&txdata->lock);
828 txdata->dot1x_open = 0;
829 txdata->enabled = 0;
830 txdata->idle = 0;
831 txdata->last_idle = 0;
832 txdata->begin = 0;
833 txdata->agg_wsize = 0;
834 txdata->agg_timeout = 0;
835 txdata->agg_amsdu = 0;
836 txdata->addba_in_progress = false;
837 spin_unlock_bh(&txdata->lock);
838}
839
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800840int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
841 int cid, int tid)
842{
843 int rc;
844 struct wmi_vring_cfg_cmd cmd = {
845 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
846 .vring_cfg = {
847 .tx_sw_ring = {
Vladimir Kondratiev9a06bec2014-10-28 16:51:27 +0200848 .max_mpdu_size =
Vladimir Kondratievc44690a2014-12-23 09:47:11 +0200849 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
Vladimir Kondratievb5d98e92013-04-18 14:33:51 +0300850 .ring_size = cpu_to_le16(size),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800851 },
852 .ringid = id,
Vladimir Kondratieva70abea2014-03-17 15:34:05 +0200853 .cidxtid = mk_cidxtid(cid, tid),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800854 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
855 .mac_ctrl = 0,
856 .to_resolution = 0,
Vladimir Kondratiev32772132014-12-23 09:47:03 +0200857 .agg_max_wsize = 0,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800858 .schd_params = {
859 .priority = cpu_to_le16(0),
860 .timeslot_us = cpu_to_le16(0xfff),
861 },
862 },
863 };
864 struct {
Lior Davidb874dde2016-03-01 19:18:09 +0200865 struct wmi_cmd_hdr wmi;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800866 struct wmi_vring_cfg_done_event cmd;
867 } __packed reply;
868 struct vring *vring = &wil->vring_tx[id];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200869 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800870
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +0300871 wil_dbg_misc(wil, "%s() max_mpdu_size %d\n", __func__,
872 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +0200873 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300874
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800875 if (vring->va) {
876 wil_err(wil, "Tx ring [%d] already allocated\n", id);
877 rc = -EINVAL;
878 goto out;
879 }
880
Maya Erez875e9432016-01-28 19:24:02 +0200881 wil_tx_data_init(txdata);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800882 vring->size = size;
883 rc = wil_vring_alloc(wil, vring);
884 if (rc)
885 goto out;
886
Vladimir Kondratiev93ae6d42014-02-27 16:20:51 +0200887 wil->vring2cid_tid[id][0] = cid;
888 wil->vring2cid_tid[id][1] = tid;
889
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800890 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800891
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300892 if (!wil->privacy)
893 txdata->dot1x_open = true;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800894 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
895 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
896 if (rc)
897 goto out_free;
898
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200899 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800900 wil_err(wil, "Tx config failed, status 0x%02x\n",
901 reply.cmd.status);
Vladimir Kondratievc3319972013-01-28 18:31:09 +0200902 rc = -EINVAL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800903 goto out_free;
904 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800905
Maya Erezdc905062016-08-18 16:52:15 +0300906 spin_lock_bh(&txdata->lock);
907 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200908 txdata->enabled = 1;
Maya Erezdc905062016-08-18 16:52:15 +0300909 spin_unlock_bh(&txdata->lock);
910
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300911 if (txdata->dot1x_open && (agg_wsize >= 0))
Vladimir Kondratiev3a3def82014-12-23 09:47:05 +0200912 wil_addba_tx_request(wil, id, agg_wsize);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200913
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800914 return 0;
915 out_free:
Maya Erez875e9432016-01-28 19:24:02 +0200916 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300917 txdata->dot1x_open = false;
918 txdata->enabled = 0;
Maya Erez875e9432016-01-28 19:24:02 +0200919 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800920 wil_vring_free(wil, vring, 1);
Maya Erez0916d9f2016-01-17 12:39:10 +0200921 wil->vring2cid_tid[id][0] = WIL6210_MAX_CID;
922 wil->vring2cid_tid[id][1] = 0;
923
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800924 out:
925
926 return rc;
927}
928
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200929int wil_vring_init_bcast(struct wil6210_priv *wil, int id, int size)
930{
931 int rc;
932 struct wmi_bcast_vring_cfg_cmd cmd = {
933 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
934 .vring_cfg = {
935 .tx_sw_ring = {
936 .max_mpdu_size =
937 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
938 .ring_size = cpu_to_le16(size),
939 },
940 .ringid = id,
941 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
942 },
943 };
944 struct {
Lior Davidb874dde2016-03-01 19:18:09 +0200945 struct wmi_cmd_hdr wmi;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200946 struct wmi_vring_cfg_done_event cmd;
947 } __packed reply;
948 struct vring *vring = &wil->vring_tx[id];
949 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
950
951 wil_dbg_misc(wil, "%s() max_mpdu_size %d\n", __func__,
952 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +0200953 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200954
955 if (vring->va) {
956 wil_err(wil, "Tx ring [%d] already allocated\n", id);
957 rc = -EINVAL;
958 goto out;
959 }
960
Maya Erez875e9432016-01-28 19:24:02 +0200961 wil_tx_data_init(txdata);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200962 vring->size = size;
963 rc = wil_vring_alloc(wil, vring);
964 if (rc)
965 goto out;
966
967 wil->vring2cid_tid[id][0] = WIL6210_MAX_CID; /* CID */
968 wil->vring2cid_tid[id][1] = 0; /* TID */
969
970 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
971
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300972 if (!wil->privacy)
973 txdata->dot1x_open = true;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200974 rc = wmi_call(wil, WMI_BCAST_VRING_CFG_CMDID, &cmd, sizeof(cmd),
975 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
976 if (rc)
977 goto out_free;
978
979 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
980 wil_err(wil, "Tx config failed, status 0x%02x\n",
981 reply.cmd.status);
982 rc = -EINVAL;
983 goto out_free;
984 }
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200985
Maya Erezdc905062016-08-18 16:52:15 +0300986 spin_lock_bh(&txdata->lock);
987 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200988 txdata->enabled = 1;
Maya Erezdc905062016-08-18 16:52:15 +0300989 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200990
991 return 0;
992 out_free:
Maya Erez875e9432016-01-28 19:24:02 +0200993 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +0300994 txdata->enabled = 0;
995 txdata->dot1x_open = false;
Maya Erez875e9432016-01-28 19:24:02 +0200996 spin_unlock_bh(&txdata->lock);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200997 wil_vring_free(wil, vring, 1);
998 out:
999
1000 return rc;
1001}
1002
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001003void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
1004{
1005 struct vring *vring = &wil->vring_tx[id];
Vladimir Kondratiev3a124ed2014-12-23 09:47:04 +02001006 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001007
Vladimir Kondratiev9b1ba7b2015-11-06 12:50:44 +02001008 lockdep_assert_held(&wil->mutex);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001009
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001010 if (!vring->va)
1011 return;
1012
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +03001013 wil_dbg_misc(wil, "%s() id=%d\n", __func__, id);
1014
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001015 spin_lock_bh(&txdata->lock);
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001016 txdata->dot1x_open = false;
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001017 txdata->enabled = 0; /* no Tx can be in progress or start anew */
1018 spin_unlock_bh(&txdata->lock);
Maya Erez34b88862016-05-16 22:23:32 +03001019 /* napi_synchronize waits for completion of the current NAPI but will
1020 * not prevent the next NAPI run.
1021 * Add a memory barrier to guarantee that txdata->enabled is zeroed
1022 * before napi_synchronize so that the next scheduled NAPI will not
1023 * handle this vring
1024 */
1025 wmb();
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001026 /* make sure NAPI won't touch this vring */
Vladimir Kondratiev9419b6a2014-12-23 09:47:14 +02001027 if (test_bit(wil_status_napi_en, wil->status))
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001028 napi_synchronize(&wil->napi_tx);
1029
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001030 wil_vring_free(wil, vring, 1);
1031}
1032
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001033static struct vring *wil_find_tx_ucast(struct wil6210_priv *wil,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001034 struct sk_buff *skb)
1035{
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001036 int i;
1037 struct ethhdr *eth = (void *)skb->data;
1038 int cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001039
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001040 if (cid < 0)
1041 return NULL;
1042
1043 /* TODO: fix for multiple TID */
1044 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001045 if (!wil->vring_tx_data[i].dot1x_open &&
1046 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1047 continue;
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001048 if (wil->vring2cid_tid[i][0] == cid) {
1049 struct vring *v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001050 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001051
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001052 wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n",
1053 __func__, eth->h_dest, i);
Maya Erezb729aaf2016-01-17 12:39:09 +02001054 if (v->va && txdata->enabled) {
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001055 return v;
1056 } else {
1057 wil_dbg_txrx(wil, "vring[%d] not valid\n", i);
1058 return NULL;
1059 }
1060 }
1061 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001062
1063 return NULL;
1064}
1065
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001066static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1067 struct sk_buff *skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001068
1069static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
1070 struct sk_buff *skb)
1071{
1072 struct vring *v;
1073 int i;
1074 u8 cid;
Maya Erezb729aaf2016-01-17 12:39:09 +02001075 struct vring_tx_data *txdata;
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001076
1077 /* In the STA mode, it is expected to have only 1 VRING
1078 * for the AP we connected to.
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001079 * find 1-st vring eligible for this skb and use it.
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001080 */
1081 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1082 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001083 txdata = &wil->vring_tx_data[i];
1084 if (!v->va || !txdata->enabled)
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001085 continue;
1086
1087 cid = wil->vring2cid_tid[i][0];
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001088 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1089 continue;
1090
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001091 if (!wil->vring_tx_data[i].dot1x_open &&
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001092 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001093 continue;
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001094
1095 wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
1096
1097 return v;
1098 }
1099
1100 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1101
1102 return NULL;
1103}
1104
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001105/* Use one of 2 strategies:
1106 *
1107 * 1. New (real broadcast):
1108 * use dedicated broadcast vring
1109 * 2. Old (pseudo-DMS):
1110 * Find 1-st vring and return it;
1111 * duplicate skb and send it to other active vrings;
1112 * in all cases override dest address to unicast peer's address
1113 * Use old strategy when new is not supported yet:
1114 * - for PBSS
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001115 */
1116static struct vring *wil_find_tx_bcast_1(struct wil6210_priv *wil,
1117 struct sk_buff *skb)
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001118{
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001119 struct vring *v;
Maya Erezb729aaf2016-01-17 12:39:09 +02001120 struct vring_tx_data *txdata;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001121 int i = wil->bcast_vring;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001122
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001123 if (i < 0)
1124 return NULL;
1125 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001126 txdata = &wil->vring_tx_data[i];
1127 if (!v->va || !txdata->enabled)
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001128 return NULL;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001129 if (!wil->vring_tx_data[i].dot1x_open &&
1130 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1131 return NULL;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001132
1133 return v;
1134}
1135
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001136static void wil_set_da_for_vring(struct wil6210_priv *wil,
1137 struct sk_buff *skb, int vring_index)
1138{
1139 struct ethhdr *eth = (void *)skb->data;
1140 int cid = wil->vring2cid_tid[vring_index][0];
1141
1142 ether_addr_copy(eth->h_dest, wil->sta[cid].addr);
1143}
1144
1145static struct vring *wil_find_tx_bcast_2(struct wil6210_priv *wil,
1146 struct sk_buff *skb)
1147{
1148 struct vring *v, *v2;
1149 struct sk_buff *skb2;
1150 int i;
1151 u8 cid;
1152 struct ethhdr *eth = (void *)skb->data;
1153 char *src = eth->h_source;
Maya Erezb729aaf2016-01-17 12:39:09 +02001154 struct vring_tx_data *txdata;
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001155
1156 /* find 1-st vring eligible for data */
1157 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1158 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001159 txdata = &wil->vring_tx_data[i];
1160 if (!v->va || !txdata->enabled)
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001161 continue;
1162
1163 cid = wil->vring2cid_tid[i][0];
1164 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1165 continue;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001166 if (!wil->vring_tx_data[i].dot1x_open &&
1167 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001168 continue;
1169
1170 /* don't Tx back to source when re-routing Rx->Tx at the AP */
1171 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1172 continue;
1173
1174 goto found;
1175 }
1176
1177 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1178
1179 return NULL;
1180
1181found:
1182 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
1183 wil_set_da_for_vring(wil, skb, i);
1184
1185 /* find other active vrings and duplicate skb for each */
1186 for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
1187 v2 = &wil->vring_tx[i];
1188 if (!v2->va)
1189 continue;
1190 cid = wil->vring2cid_tid[i][0];
1191 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1192 continue;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001193 if (!wil->vring_tx_data[i].dot1x_open &&
1194 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001195 continue;
1196
1197 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1198 continue;
1199
1200 skb2 = skb_copy(skb, GFP_ATOMIC);
1201 if (skb2) {
1202 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
1203 wil_set_da_for_vring(wil, skb2, i);
1204 wil_tx_vring(wil, v2, skb2);
1205 } else {
1206 wil_err(wil, "skb_copy failed\n");
1207 }
1208 }
1209
1210 return v;
1211}
1212
1213static struct vring *wil_find_tx_bcast(struct wil6210_priv *wil,
1214 struct sk_buff *skb)
1215{
1216 struct wireless_dev *wdev = wil->wdev;
1217
1218 if (wdev->iftype != NL80211_IFTYPE_AP)
1219 return wil_find_tx_bcast_2(wil, skb);
1220
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001221 return wil_find_tx_bcast_1(wil, skb);
1222}
1223
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001224static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
1225 int vring_index)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001226{
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001227 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001228 d->dma.ip_length = 0;
1229 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
1230 d->dma.b11 = 0/*14 | BIT(7)*/;
1231 d->dma.error = 0;
1232 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001233 d->dma.length = cpu_to_le16((u16)len);
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001234 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001235 d->mac.d[0] = 0;
1236 d->mac.d[1] = 0;
1237 d->mac.d[2] = 0;
1238 d->mac.ucode_cmd = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001239 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
1240 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
1241 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
1242
1243 return 0;
1244}
1245
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001246static inline
1247void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
1248{
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001249 d->mac.d[2] |= (nr_frags << MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001250}
1251
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001252/**
1253 * Sets the descriptor @d up for csum and/or TSO offloading. The corresponding
1254 * @skb is used to obtain the protocol and headers length.
1255 * @tso_desc_type is a descriptor type for TSO: 0 - a header, 1 - first data,
1256 * 2 - middle, 3 - last descriptor.
1257 */
1258
1259static void wil_tx_desc_offload_setup_tso(struct vring_tx_desc *d,
1260 struct sk_buff *skb,
1261 int tso_desc_type, bool is_ipv4,
1262 int tcp_hdr_len, int skb_net_hdr_len)
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001263{
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001264 d->dma.b11 = ETH_HLEN; /* MAC header length */
1265 d->dma.b11 |= is_ipv4 << DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS;
1266
1267 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1268 /* L4 header len: TCP header length */
1269 d->dma.d0 |= (tcp_hdr_len & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1270
1271 /* Setup TSO: bit and desc type */
1272 d->dma.d0 |= (BIT(DMA_CFG_DESC_TX_0_TCP_SEG_EN_POS)) |
1273 (tso_desc_type << DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS);
1274 d->dma.d0 |= (is_ipv4 << DMA_CFG_DESC_TX_0_IPV4_CHECKSUM_EN_POS);
1275
1276 d->dma.ip_length = skb_net_hdr_len;
1277 /* Enable TCP/UDP checksum */
1278 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1279 /* Calculate pseudo-header */
1280 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1281}
1282
1283/**
1284 * Sets the descriptor @d up for csum. The corresponding
1285 * @skb is used to obtain the protocol and headers length.
1286 * Returns the protocol: 0 - not TCP, 1 - TCPv4, 2 - TCPv6.
1287 * Note, if d==NULL, the function only returns the protocol result.
1288 *
1289 * It is very similar to previous wil_tx_desc_offload_setup_tso. This
1290 * is "if unrolling" to optimize the critical path.
1291 */
1292
1293static int wil_tx_desc_offload_setup(struct vring_tx_desc *d,
1294 struct sk_buff *skb){
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001295 int protocol;
1296
1297 if (skb->ip_summed != CHECKSUM_PARTIAL)
1298 return 0;
1299
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +02001300 d->dma.b11 = ETH_HLEN; /* MAC header length */
1301
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001302 switch (skb->protocol) {
1303 case cpu_to_be16(ETH_P_IP):
1304 protocol = ip_hdr(skb)->protocol;
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +02001305 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001306 break;
1307 case cpu_to_be16(ETH_P_IPV6):
1308 protocol = ipv6_hdr(skb)->nexthdr;
1309 break;
1310 default:
1311 return -EINVAL;
1312 }
1313
1314 switch (protocol) {
1315 case IPPROTO_TCP:
1316 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1317 /* L4 header len: TCP header length */
1318 d->dma.d0 |=
1319 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1320 break;
1321 case IPPROTO_UDP:
1322 /* L4 header len: UDP header length */
1323 d->dma.d0 |=
1324 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1325 break;
1326 default:
1327 return -EINVAL;
1328 }
1329
1330 d->dma.ip_length = skb_network_header_len(skb);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001331 /* Enable TCP/UDP checksum */
1332 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1333 /* Calculate pseudo-header */
1334 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1335
1336 return 0;
1337}
1338
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001339static inline void wil_tx_last_desc(struct vring_tx_desc *d)
1340{
1341 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS) |
1342 BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS) |
1343 BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
1344}
1345
1346static inline void wil_set_tx_desc_last_tso(volatile struct vring_tx_desc *d)
1347{
1348 d->dma.d0 |= wil_tso_type_lst <<
1349 DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS;
1350}
1351
1352static int __wil_tx_vring_tso(struct wil6210_priv *wil, struct vring *vring,
1353 struct sk_buff *skb)
1354{
1355 struct device *dev = wil_to_dev(wil);
1356
1357 /* point to descriptors in shared memory */
1358 volatile struct vring_tx_desc *_desc = NULL, *_hdr_desc,
1359 *_first_desc = NULL;
1360
1361 /* pointers to shadow descriptors */
1362 struct vring_tx_desc desc_mem, hdr_desc_mem, first_desc_mem,
1363 *d = &hdr_desc_mem, *hdr_desc = &hdr_desc_mem,
1364 *first_desc = &first_desc_mem;
1365
1366 /* pointer to shadow descriptors' context */
1367 struct wil_ctx *hdr_ctx, *first_ctx = NULL;
1368
1369 int descs_used = 0; /* total number of used descriptors */
1370 int sg_desc_cnt = 0; /* number of descriptors for current mss*/
1371
1372 u32 swhead = vring->swhead;
1373 int used, avail = wil_vring_avail_tx(vring);
1374 int nr_frags = skb_shinfo(skb)->nr_frags;
1375 int min_desc_required = nr_frags + 1;
1376 int mss = skb_shinfo(skb)->gso_size; /* payload size w/o headers */
1377 int f, len, hdrlen, headlen;
1378 int vring_index = vring - wil->vring_tx;
1379 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1380 uint i = swhead;
1381 dma_addr_t pa;
1382 const skb_frag_t *frag = NULL;
1383 int rem_data = mss;
1384 int lenmss;
1385 int hdr_compensation_need = true;
1386 int desc_tso_type = wil_tso_type_first;
1387 bool is_ipv4;
1388 int tcp_hdr_len;
1389 int skb_net_hdr_len;
1390 int gso_type;
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001391 int rc = -EINVAL;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001392
1393 wil_dbg_txrx(wil, "%s() %d bytes to vring %d\n",
1394 __func__, skb->len, vring_index);
1395
1396 if (unlikely(!txdata->enabled))
1397 return -EINVAL;
1398
1399 /* A typical page 4K is 3-4 payloads, we assume each fragment
1400 * is a full payload, that's how min_desc_required has been
1401 * calculated. In real we might need more or less descriptors,
1402 * this is the initial check only.
1403 */
1404 if (unlikely(avail < min_desc_required)) {
1405 wil_err_ratelimited(wil,
1406 "TSO: Tx ring[%2d] full. No space for %d fragments\n",
1407 vring_index, min_desc_required);
1408 return -ENOMEM;
1409 }
1410
1411 /* Header Length = MAC header len + IP header len + TCP header len*/
1412 hdrlen = ETH_HLEN +
1413 (int)skb_network_header_len(skb) +
1414 tcp_hdrlen(skb);
1415
1416 gso_type = skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4);
1417 switch (gso_type) {
1418 case SKB_GSO_TCPV4:
1419 /* TCP v4, zero out the IP length and IPv4 checksum fields
1420 * as required by the offloading doc
1421 */
1422 ip_hdr(skb)->tot_len = 0;
1423 ip_hdr(skb)->check = 0;
1424 is_ipv4 = true;
1425 break;
1426 case SKB_GSO_TCPV6:
1427 /* TCP v6, zero out the payload length */
1428 ipv6_hdr(skb)->payload_len = 0;
1429 is_ipv4 = false;
1430 break;
1431 default:
1432 /* other than TCPv4 or TCPv6 types are not supported for TSO.
1433 * It is also illegal for both to be set simultaneously
1434 */
1435 return -EINVAL;
1436 }
1437
1438 if (skb->ip_summed != CHECKSUM_PARTIAL)
1439 return -EINVAL;
1440
1441 /* tcp header length and skb network header length are fixed for all
1442 * packet's descriptors - read then once here
1443 */
1444 tcp_hdr_len = tcp_hdrlen(skb);
1445 skb_net_hdr_len = skb_network_header_len(skb);
1446
1447 _hdr_desc = &vring->va[i].tx;
1448
1449 pa = dma_map_single(dev, skb->data, hdrlen, DMA_TO_DEVICE);
1450 if (unlikely(dma_mapping_error(dev, pa))) {
1451 wil_err(wil, "TSO: Skb head DMA map error\n");
1452 goto err_exit;
1453 }
1454
1455 wil_tx_desc_map(hdr_desc, pa, hdrlen, vring_index);
1456 wil_tx_desc_offload_setup_tso(hdr_desc, skb, wil_tso_type_hdr, is_ipv4,
1457 tcp_hdr_len, skb_net_hdr_len);
1458 wil_tx_last_desc(hdr_desc);
1459
1460 vring->ctx[i].mapped_as = wil_mapped_as_single;
1461 hdr_ctx = &vring->ctx[i];
1462
1463 descs_used++;
1464 headlen = skb_headlen(skb) - hdrlen;
1465
1466 for (f = headlen ? -1 : 0; f < nr_frags; f++) {
1467 if (headlen) {
1468 len = headlen;
1469 wil_dbg_txrx(wil, "TSO: process skb head, len %u\n",
1470 len);
1471 } else {
1472 frag = &skb_shinfo(skb)->frags[f];
1473 len = frag->size;
1474 wil_dbg_txrx(wil, "TSO: frag[%d]: len %u\n", f, len);
1475 }
1476
1477 while (len) {
1478 wil_dbg_txrx(wil,
1479 "TSO: len %d, rem_data %d, descs_used %d\n",
1480 len, rem_data, descs_used);
1481
1482 if (descs_used == avail) {
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001483 wil_err_ratelimited(wil, "TSO: ring overflow\n");
1484 rc = -ENOMEM;
1485 goto mem_error;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001486 }
1487
1488 lenmss = min_t(int, rem_data, len);
1489 i = (swhead + descs_used) % vring->size;
1490 wil_dbg_txrx(wil, "TSO: lenmss %d, i %d\n", lenmss, i);
1491
1492 if (!headlen) {
1493 pa = skb_frag_dma_map(dev, frag,
1494 frag->size - len, lenmss,
1495 DMA_TO_DEVICE);
1496 vring->ctx[i].mapped_as = wil_mapped_as_page;
1497 } else {
1498 pa = dma_map_single(dev,
1499 skb->data +
1500 skb_headlen(skb) - headlen,
1501 lenmss,
1502 DMA_TO_DEVICE);
1503 vring->ctx[i].mapped_as = wil_mapped_as_single;
1504 headlen -= lenmss;
1505 }
1506
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001507 if (unlikely(dma_mapping_error(dev, pa))) {
1508 wil_err(wil, "TSO: DMA map page error\n");
1509 goto mem_error;
1510 }
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001511
1512 _desc = &vring->va[i].tx;
1513
1514 if (!_first_desc) {
1515 _first_desc = _desc;
1516 first_ctx = &vring->ctx[i];
1517 d = first_desc;
1518 } else {
1519 d = &desc_mem;
1520 }
1521
1522 wil_tx_desc_map(d, pa, lenmss, vring_index);
1523 wil_tx_desc_offload_setup_tso(d, skb, desc_tso_type,
1524 is_ipv4, tcp_hdr_len,
1525 skb_net_hdr_len);
1526
1527 /* use tso_type_first only once */
1528 desc_tso_type = wil_tso_type_mid;
1529
1530 descs_used++; /* desc used so far */
1531 sg_desc_cnt++; /* desc used for this segment */
1532 len -= lenmss;
1533 rem_data -= lenmss;
1534
1535 wil_dbg_txrx(wil,
1536 "TSO: len %d, rem_data %d, descs_used %d, sg_desc_cnt %d,\n",
1537 len, rem_data, descs_used, sg_desc_cnt);
1538
1539 /* Close the segment if reached mss size or last frag*/
1540 if (rem_data == 0 || (f == nr_frags - 1 && len == 0)) {
1541 if (hdr_compensation_need) {
1542 /* first segment include hdr desc for
1543 * release
1544 */
1545 hdr_ctx->nr_frags = sg_desc_cnt;
1546 wil_tx_desc_set_nr_frags(first_desc,
1547 sg_desc_cnt +
1548 1);
1549 hdr_compensation_need = false;
1550 } else {
1551 wil_tx_desc_set_nr_frags(first_desc,
1552 sg_desc_cnt);
1553 }
1554 first_ctx->nr_frags = sg_desc_cnt - 1;
1555
1556 wil_tx_last_desc(d);
1557
1558 /* first descriptor may also be the last
1559 * for this mss - make sure not to copy
1560 * it twice
1561 */
1562 if (first_desc != d)
1563 *_first_desc = *first_desc;
1564
1565 /*last descriptor will be copied at the end
1566 * of this TS processing
1567 */
1568 if (f < nr_frags - 1 || len > 0)
1569 *_desc = *d;
1570
1571 rem_data = mss;
1572 _first_desc = NULL;
1573 sg_desc_cnt = 0;
1574 } else if (first_desc != d) /* update mid descriptor */
1575 *_desc = *d;
1576 }
1577 }
1578
1579 /* first descriptor may also be the last.
1580 * in this case d pointer is invalid
1581 */
1582 if (_first_desc == _desc)
1583 d = first_desc;
1584
1585 /* Last data descriptor */
1586 wil_set_tx_desc_last_tso(d);
1587 *_desc = *d;
1588
1589 /* Fill the total number of descriptors in first desc (hdr)*/
1590 wil_tx_desc_set_nr_frags(hdr_desc, descs_used);
1591 *_hdr_desc = *hdr_desc;
1592
1593 /* hold reference to skb
1594 * to prevent skb release before accounting
1595 * in case of immediate "tx done"
1596 */
1597 vring->ctx[i].skb = skb_get(skb);
1598
1599 /* performance monitoring */
1600 used = wil_vring_used_tx(vring);
1601 if (wil_val_in_range(vring_idle_trsh,
1602 used, used + descs_used)) {
1603 txdata->idle += get_cycles() - txdata->last_idle;
1604 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1605 vring_index, used, used + descs_used);
1606 }
1607
Maya Erezeb26cff2016-05-16 22:23:30 +03001608 /* Make sure to advance the head only after descriptor update is done.
1609 * This will prevent a race condition where the completion thread
1610 * will see the DU bit set from previous run and will handle the
1611 * skb before it was completed.
1612 */
1613 wmb();
1614
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001615 /* advance swhead */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001616 wil_vring_advance_head(vring, descs_used);
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001617 wil_dbg_txrx(wil, "TSO: Tx swhead %d -> %d\n", swhead, vring->swhead);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001618
1619 /* make sure all writes to descriptors (shared memory) are done before
1620 * committing them to HW
1621 */
1622 wmb();
1623
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +03001624 wil_w(wil, vring->hwtail, vring->swhead);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001625 return 0;
1626
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001627mem_error:
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001628 while (descs_used > 0) {
1629 struct wil_ctx *ctx;
1630
Maya Ereza1526f72016-05-16 22:23:33 +03001631 i = (swhead + descs_used - 1) % vring->size;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001632 d = (struct vring_tx_desc *)&vring->va[i].tx;
1633 _desc = &vring->va[i].tx;
1634 *d = *_desc;
1635 _desc->dma.status = TX_DMA_STATUS_DU;
1636 ctx = &vring->ctx[i];
1637 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001638 memset(ctx, 0, sizeof(*ctx));
1639 descs_used--;
1640 }
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001641err_exit:
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001642 return rc;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001643}
1644
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001645static int __wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1646 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001647{
1648 struct device *dev = wil_to_dev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001649 struct vring_tx_desc dd, *d = &dd;
1650 volatile struct vring_tx_desc *_d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001651 u32 swhead = vring->swhead;
1652 int avail = wil_vring_avail_tx(vring);
1653 int nr_frags = skb_shinfo(skb)->nr_frags;
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001654 uint f = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001655 int vring_index = vring - wil->vring_tx;
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001656 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001657 uint i = swhead;
1658 dma_addr_t pa;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001659 int used;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001660 bool mcast = (vring_index == wil->bcast_vring);
1661 uint len = skb_headlen(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001662
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001663 wil_dbg_txrx(wil, "%s() %d bytes to vring %d\n",
1664 __func__, skb->len, vring_index);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001665
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001666 if (unlikely(!txdata->enabled))
1667 return -EINVAL;
1668
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001669 if (unlikely(avail < 1 + nr_frags)) {
Vladimir Kondratiev70801e12014-12-01 15:36:03 +02001670 wil_err_ratelimited(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001671 "Tx ring[%2d] full. No space for %d fragments\n",
1672 vring_index, 1 + nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001673 return -ENOMEM;
1674 }
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001675 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001676
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001677 pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001678
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001679 wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index,
1680 skb_headlen(skb), skb->data, &pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001681 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001682 skb->data, skb_headlen(skb), false);
1683
1684 if (unlikely(dma_mapping_error(dev, pa)))
1685 return -EINVAL;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001686 vring->ctx[i].mapped_as = wil_mapped_as_single;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001687 /* 1-st segment */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001688 wil_tx_desc_map(d, pa, len, vring_index);
1689 if (unlikely(mcast)) {
1690 d->mac.d[0] |= BIT(MAC_CFG_DESC_TX_0_MCS_EN_POS); /* MCS 0 */
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001691 if (unlikely(len > WIL_BCAST_MCS0_LIMIT)) /* set MCS 1 */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001692 d->mac.d[0] |= (1 << MAC_CFG_DESC_TX_0_MCS_INDEX_POS);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001693 }
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001694 /* Process TCP/UDP checksum offloading */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001695 if (unlikely(wil_tx_desc_offload_setup(d, skb))) {
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001696 wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001697 vring_index);
1698 goto dma_error;
1699 }
1700
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001701 vring->ctx[i].nr_frags = nr_frags;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001702 wil_tx_desc_set_nr_frags(d, nr_frags + 1);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001703
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001704 /* middle segments */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001705 for (; f < nr_frags; f++) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001706 const struct skb_frag_struct *frag =
1707 &skb_shinfo(skb)->frags[f];
1708 int len = skb_frag_size(frag);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001709
Vladimir Kondratieve59d16c2015-02-01 10:55:12 +02001710 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001711 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1712 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1713 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001714 i = (swhead + f + 1) % vring->size;
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001715 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001716 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001717 DMA_TO_DEVICE);
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001718 if (unlikely(dma_mapping_error(dev, pa))) {
1719 wil_err(wil, "Tx[%2d] failed to map fragment\n",
1720 vring_index);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001721 goto dma_error;
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001722 }
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001723 vring->ctx[i].mapped_as = wil_mapped_as_page;
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001724 wil_tx_desc_map(d, pa, len, vring_index);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001725 /* no need to check return code -
1726 * if it succeeded for 1-st descriptor,
1727 * it will succeed here too
1728 */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001729 wil_tx_desc_offload_setup(d, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001730 }
1731 /* for the last seg only */
1732 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
Kirshenbaum Erez668b2bb2013-06-23 12:59:35 +03001733 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001734 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001735 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001736 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1737 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1738 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001739
Vladimir Kondratiev6cdadd42013-07-11 18:03:41 +03001740 /* hold reference to skb
1741 * to prevent skb release before accounting
1742 * in case of immediate "tx done"
1743 */
1744 vring->ctx[i].skb = skb_get(skb);
1745
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001746 /* performance monitoring */
1747 used = wil_vring_used_tx(vring);
1748 if (wil_val_in_range(vring_idle_trsh,
1749 used, used + nr_frags + 1)) {
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001750 txdata->idle += get_cycles() - txdata->last_idle;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001751 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1752 vring_index, used, used + nr_frags + 1);
1753 }
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001754
Maya Erezeb26cff2016-05-16 22:23:30 +03001755 /* Make sure to advance the head only after descriptor update is done.
1756 * This will prevent a race condition where the completion thread
1757 * will see the DU bit set from previous run and will handle the
1758 * skb before it was completed.
1759 */
1760 wmb();
1761
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001762 /* advance swhead */
1763 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001764 wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead,
1765 vring->swhead);
Vladimir Kondratiev98658092013-05-12 14:43:35 +03001766 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001767
1768 /* make sure all writes to descriptors (shared memory) are done before
1769 * committing them to HW
1770 */
1771 wmb();
1772
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +03001773 wil_w(wil, vring->hwtail, vring->swhead);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001774
1775 return 0;
1776 dma_error:
1777 /* unmap what we have mapped */
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001778 nr_frags = f + 1; /* frags mapped + one for skb head */
1779 for (f = 0; f < nr_frags; f++) {
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001780 struct wil_ctx *ctx;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001781
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001782 i = (swhead + f) % vring->size;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001783 ctx = &vring->ctx[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001784 _d = &vring->va[i].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001785 *d = *_d;
1786 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001787 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001788
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001789 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001790 }
1791
1792 return -EINVAL;
1793}
1794
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001795static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1796 struct sk_buff *skb)
1797{
1798 int vring_index = vring - wil->vring_tx;
1799 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1800 int rc;
1801
1802 spin_lock(&txdata->lock);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001803
1804 rc = (skb_is_gso(skb) ? __wil_tx_vring_tso : __wil_tx_vring)
1805 (wil, vring, skb);
1806
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001807 spin_unlock(&txdata->lock);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001808
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001809 return rc;
1810}
1811
Dedy Lansky970a98f2016-12-06 08:26:49 +02001812/**
1813 * Check status of tx vrings and stop/wake net queues if needed
1814 *
1815 * This function does one of two checks:
1816 * In case check_stop is true, will check if net queues need to be stopped. If
1817 * the conditions for stopping are met, netif_tx_stop_all_queues() is called.
1818 * In case check_stop is false, will check if net queues need to be waked. If
1819 * the conditions for waking are met, netif_tx_wake_all_queues() is called.
1820 * vring is the vring which is currently being modified by either adding
1821 * descriptors (tx) into it or removing descriptors (tx complete) from it. Can
1822 * be null when irrelevant (e.g. connect/disconnect events).
1823 *
1824 * The implementation is to stop net queues if modified vring has low
1825 * descriptor availability. Wake if all vrings are not in low descriptor
1826 * availability and modified vring has high descriptor availability.
1827 */
1828static inline void __wil_update_net_queues(struct wil6210_priv *wil,
1829 struct vring *vring,
1830 bool check_stop)
1831{
1832 int i;
1833
1834 if (vring)
1835 wil_dbg_txrx(wil, "vring %d, check_stop=%d, stopped=%d",
1836 (int)(vring - wil->vring_tx), check_stop,
1837 wil->net_queue_stopped);
1838 else
1839 wil_dbg_txrx(wil, "check_stop=%d, stopped=%d",
1840 check_stop, wil->net_queue_stopped);
1841
1842 if (check_stop == wil->net_queue_stopped)
1843 /* net queues already in desired state */
1844 return;
1845
1846 if (check_stop) {
1847 if (!vring || unlikely(wil_vring_avail_low(vring))) {
1848 /* not enough room in the vring */
1849 netif_tx_stop_all_queues(wil_to_ndev(wil));
1850 wil->net_queue_stopped = true;
1851 wil_dbg_txrx(wil, "netif_tx_stop called\n");
1852 }
1853 return;
1854 }
1855
1856 /* check wake */
1857 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1858 struct vring *cur_vring = &wil->vring_tx[i];
1859 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
1860
1861 if (!cur_vring->va || !txdata->enabled || cur_vring == vring)
1862 continue;
1863
1864 if (wil_vring_avail_low(cur_vring)) {
1865 wil_dbg_txrx(wil, "vring %d full, can't wake\n",
1866 (int)(cur_vring - wil->vring_tx));
1867 return;
1868 }
1869 }
1870
1871 if (!vring || wil_vring_avail_high(vring)) {
1872 /* enough room in the vring */
1873 wil_dbg_txrx(wil, "calling netif_tx_wake\n");
1874 netif_tx_wake_all_queues(wil_to_ndev(wil));
1875 wil->net_queue_stopped = false;
1876 }
1877}
1878
1879void wil_update_net_queues(struct wil6210_priv *wil, struct vring *vring,
1880 bool check_stop)
1881{
1882 spin_lock(&wil->net_queue_lock);
1883 __wil_update_net_queues(wil, vring, check_stop);
1884 spin_unlock(&wil->net_queue_lock);
1885}
1886
1887void wil_update_net_queues_bh(struct wil6210_priv *wil, struct vring *vring,
1888 bool check_stop)
1889{
1890 spin_lock_bh(&wil->net_queue_lock);
1891 __wil_update_net_queues(wil, vring, check_stop);
1892 spin_unlock_bh(&wil->net_queue_lock);
1893}
1894
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001895netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1896{
1897 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001898 struct ethhdr *eth = (void *)skb->data;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001899 bool bcast = is_multicast_ether_addr(eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001900 struct vring *vring;
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001901 static bool pr_once_fw;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001902 int rc;
1903
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001904 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001905 if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001906 if (!pr_once_fw) {
1907 wil_err(wil, "FW not ready\n");
1908 pr_once_fw = true;
1909 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001910 goto drop;
1911 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001912 if (unlikely(!test_bit(wil_status_fwconnected, wil->status))) {
Maya Erezd8ed0432016-04-26 14:41:39 +03001913 wil_dbg_ratelimited(wil, "FW not connected, packet dropped\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001914 goto drop;
1915 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001916 if (unlikely(wil->wdev->iftype == NL80211_IFTYPE_MONITOR)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001917 wil_err(wil, "Xmit in monitor mode not supported\n");
1918 goto drop;
1919 }
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001920 pr_once_fw = false;
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001921
1922 /* find vring */
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001923 if (wil->wdev->iftype == NL80211_IFTYPE_STATION) {
1924 /* in STA mode (ESS), all to same VRING */
1925 vring = wil_find_tx_vring_sta(wil, skb);
1926 } else { /* direct communication, find matching VRING */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001927 vring = bcast ? wil_find_tx_bcast(wil, skb) :
1928 wil_find_tx_ucast(wil, skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001929 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001930 if (unlikely(!vring)) {
Vladimir Kondratiev5aed1392014-06-16 19:37:15 +03001931 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001932 goto drop;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001933 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001934 /* set up vring entry */
1935 rc = wil_tx_vring(wil, vring, skb);
1936
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001937 switch (rc) {
1938 case 0:
Dedy Lansky970a98f2016-12-06 08:26:49 +02001939 /* shall we stop net queues? */
1940 wil_update_net_queues_bh(wil, vring, true);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02001941 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001942 dev_kfree_skb_any(skb);
1943 return NETDEV_TX_OK;
1944 case -ENOMEM:
1945 return NETDEV_TX_BUSY;
1946 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +02001947 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001948 }
1949 drop:
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001950 ndev->stats.tx_dropped++;
1951 dev_kfree_skb_any(skb);
1952
1953 return NET_XMIT_DROP;
1954}
1955
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02001956static inline bool wil_need_txstat(struct sk_buff *skb)
1957{
1958 struct ethhdr *eth = (void *)skb->data;
1959
1960 return is_unicast_ether_addr(eth->h_dest) && skb->sk &&
1961 (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS);
1962}
1963
1964static inline void wil_consume_skb(struct sk_buff *skb, bool acked)
1965{
1966 if (unlikely(wil_need_txstat(skb)))
1967 skb_complete_wifi_ack(skb, acked);
1968 else
1969 acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb);
1970}
1971
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001972/**
1973 * Clean up transmitted skb's from the Tx VRING
1974 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001975 * Return number of descriptors cleared
1976 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001977 * Safe to call from IRQ
1978 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001979int wil_tx_complete(struct wil6210_priv *wil, int ringid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001980{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02001981 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001982 struct device *dev = wil_to_dev(wil);
1983 struct vring *vring = &wil->vring_tx[ringid];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001984 struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001985 int done = 0;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +02001986 int cid = wil->vring2cid_tid[ringid][0];
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001987 struct wil_net_stats *stats = NULL;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001988 volatile struct vring_tx_desc *_d;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001989 int used_before_complete;
1990 int used_new;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001991
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001992 if (unlikely(!vring->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001993 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001994 return 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001995 }
1996
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001997 if (unlikely(!txdata->enabled)) {
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001998 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
1999 return 0;
2000 }
2001
Vladimir Kondratiev77438822013-01-28 18:31:06 +02002002 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002003
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002004 used_before_complete = wil_vring_used_tx(vring);
2005
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002006 if (cid < WIL6210_MAX_CID)
2007 stats = &wil->sta[cid].stats;
2008
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002009 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002010 int new_swtail;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03002011 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002012 /**
2013 * For the fragmented skb, HW will set DU bit only for the
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03002014 * last fragment. look for it.
2015 * In TSO the first DU will include hdr desc
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002016 */
2017 int lf = (vring->swtail + ctx->nr_frags) % vring->size;
2018 /* TODO: check we are not past head */
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +03002019
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002020 _d = &vring->va[lf].tx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002021 if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002022 break;
2023
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002024 new_swtail = (lf + 1) % vring->size;
2025 while (vring->swtail != new_swtail) {
2026 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002027 u16 dmalen;
Vladimir Kondratiev76dfa4b2014-07-14 09:49:39 +03002028 struct sk_buff *skb;
2029
2030 ctx = &vring->ctx[vring->swtail];
2031 skb = ctx->skb;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002032 _d = &vring->va[vring->swtail].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002033
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002034 *d = *_d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03002035
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002036 dmalen = le16_to_cpu(d->dma.length);
2037 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
2038 d->dma.error);
2039 wil_dbg_txrx(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02002040 "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
2041 ringid, vring->swtail, dmalen,
2042 d->dma.status, d->dma.error);
2043 wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002044 (const void *)d, sizeof(*d), false);
2045
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02002046 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002047
2048 if (skb) {
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002049 if (likely(d->dma.error == 0)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002050 ndev->stats.tx_packets++;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002051 ndev->stats.tx_bytes += skb->len;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002052 if (stats) {
2053 stats->tx_packets++;
2054 stats->tx_bytes += skb->len;
2055 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002056 } else {
2057 ndev->stats.tx_errors++;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002058 if (stats)
2059 stats->tx_errors++;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002060 }
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02002061 wil_consume_skb(skb, d->dma.error == 0);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02002062 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002063 memset(ctx, 0, sizeof(*ctx));
Maya Erezeb26cff2016-05-16 22:23:30 +03002064 /* Make sure the ctx is zeroed before updating the tail
2065 * to prevent a case where wil_tx_vring will see
2066 * this descriptor as used and handle it before ctx zero
2067 * is completed.
2068 */
2069 wmb();
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002070 /* There is no need to touch HW descriptor:
2071 * - ststus bit TX_DMA_STATUS_DU is set by design,
2072 * so hardware will not try to process this desc.,
2073 * - rest of descriptor will be initialized on Tx.
2074 */
2075 vring->swtail = wil_vring_next_tail(vring);
2076 done++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002077 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002078 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03002079
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002080 /* performance monitoring */
2081 used_new = wil_vring_used_tx(vring);
2082 if (wil_val_in_range(vring_idle_trsh,
2083 used_new, used_before_complete)) {
2084 wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
2085 ringid, used_before_complete, used_new);
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03002086 txdata->last_idle = get_cycles();
2087 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03002088
Dedy Lansky970a98f2016-12-06 08:26:49 +02002089 /* shall we wake net queues? */
2090 if (done)
2091 wil_update_net_queues(wil, vring, false);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002092
2093 return done;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002094}