blob: 8b5411e4dc343d0074610bc0383cce015c66d144 [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
Lazar Alexei3190cc62017-02-23 14:34:14 +02002 * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08003 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080017#include <linux/etherdevice.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080018#include <net/ieee80211_radiotap.h>
19#include <linux/if_arp.h>
20#include <linux/moduleparam.h>
Kirshenbaum Erez504937d2013-07-21 11:34:37 +030021#include <linux/ip.h>
22#include <linux/ipv6.h>
23#include <net/ipv6.h>
Vladimir Kondratiev0786dc42014-01-14 20:31:26 +020024#include <linux/prefetch.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080025
26#include "wil6210.h"
27#include "wmi.h"
28#include "txrx.h"
Vladimir Kondratiev98658092013-05-12 14:43:35 +030029#include "trace.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080030
31static bool rtap_include_phy_info;
Maya Erezc2256ec2017-02-08 13:18:42 +020032module_param(rtap_include_phy_info, bool, 0444);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080033MODULE_PARM_DESC(rtap_include_phy_info,
34 " Include PHY info in the radiotap header, default - no");
35
Vladimir Kondratievc406ea72015-03-15 16:00:19 +020036bool rx_align_2;
Maya Erezc2256ec2017-02-08 13:18:42 +020037module_param(rx_align_2, bool, 0444);
Vladimir Kondratievc406ea72015-03-15 16:00:19 +020038MODULE_PARM_DESC(rx_align_2, " align Rx buffers on 4*n+2, default - no");
39
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
Lazar Alexei3190cc62017-02-23 14:34:14 +0200115 wil_dbg_misc(wil, "vring_alloc:\n");
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300116
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 }
Lazar Alexei3190cc62017-02-23 14:34:14 +0200765 wil_dbg_txrx(wil, "rx_handle\n");
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
Lazar Alexei3190cc62017-02-23 14:34:14 +0200788 wil_dbg_misc(wil, "rx_init\n");
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300789
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
Lazar Alexei3190cc62017-02-23 14:34:14 +0200819 wil_dbg_misc(wil, "rx_fini\n");
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +0300820
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
Lazar Alexei3190cc62017-02-23 14:34:14 +0200871 wil_dbg_misc(wil, "vring_init_tx: max_mpdu_size %d\n",
Vladimir Kondratieve0106ad2014-09-10 16:34:45 +0300872 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
Lazar Alexei3190cc62017-02-23 14:34:14 +0200951 wil_dbg_misc(wil, "vring_init_bcast: max_mpdu_size %d\n",
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +0200952 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
Lazar Alexei3190cc62017-02-23 14:34:14 +02001013 wil_dbg_misc(wil, "vring_fini_tx: id=%d\n", id);
Vladimir Kondratiev9cf10d62014-09-10 16:34:36 +03001014
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
Lazar Alexei3190cc62017-02-23 14:34:14 +02001052 wil_dbg_txrx(wil, "find_tx_ucast: (%pM) -> [%d]\n",
1053 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 {
Lazar Alexei3190cc62017-02-23 14:34:14 +02001057 wil_dbg_txrx(wil,
1058 "find_tx_ucast: vring[%d] not valid\n",
1059 i);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001060 return NULL;
1061 }
1062 }
1063 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001064
1065 return NULL;
1066}
1067
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001068static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1069 struct sk_buff *skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001070
1071static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
1072 struct sk_buff *skb)
1073{
1074 struct vring *v;
1075 int i;
1076 u8 cid;
Maya Erezb729aaf2016-01-17 12:39:09 +02001077 struct vring_tx_data *txdata;
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001078
1079 /* In the STA mode, it is expected to have only 1 VRING
1080 * for the AP we connected to.
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001081 * find 1-st vring eligible for this skb and use it.
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001082 */
1083 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1084 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001085 txdata = &wil->vring_tx_data[i];
1086 if (!v->va || !txdata->enabled)
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001087 continue;
1088
1089 cid = wil->vring2cid_tid[i][0];
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001090 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1091 continue;
1092
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001093 if (!wil->vring_tx_data[i].dot1x_open &&
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001094 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001095 continue;
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001096
1097 wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
1098
1099 return v;
1100 }
1101
1102 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1103
1104 return NULL;
1105}
1106
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001107/* Use one of 2 strategies:
1108 *
1109 * 1. New (real broadcast):
1110 * use dedicated broadcast vring
1111 * 2. Old (pseudo-DMS):
1112 * Find 1-st vring and return it;
1113 * duplicate skb and send it to other active vrings;
1114 * in all cases override dest address to unicast peer's address
1115 * Use old strategy when new is not supported yet:
1116 * - for PBSS
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001117 */
1118static struct vring *wil_find_tx_bcast_1(struct wil6210_priv *wil,
1119 struct sk_buff *skb)
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001120{
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001121 struct vring *v;
Maya Erezb729aaf2016-01-17 12:39:09 +02001122 struct vring_tx_data *txdata;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001123 int i = wil->bcast_vring;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001124
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001125 if (i < 0)
1126 return NULL;
1127 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001128 txdata = &wil->vring_tx_data[i];
1129 if (!v->va || !txdata->enabled)
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001130 return NULL;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001131 if (!wil->vring_tx_data[i].dot1x_open &&
1132 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1133 return NULL;
Vladimir Kondratievfb3cac52014-02-27 16:20:46 +02001134
1135 return v;
1136}
1137
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001138static void wil_set_da_for_vring(struct wil6210_priv *wil,
1139 struct sk_buff *skb, int vring_index)
1140{
1141 struct ethhdr *eth = (void *)skb->data;
1142 int cid = wil->vring2cid_tid[vring_index][0];
1143
1144 ether_addr_copy(eth->h_dest, wil->sta[cid].addr);
1145}
1146
1147static struct vring *wil_find_tx_bcast_2(struct wil6210_priv *wil,
1148 struct sk_buff *skb)
1149{
1150 struct vring *v, *v2;
1151 struct sk_buff *skb2;
1152 int i;
1153 u8 cid;
1154 struct ethhdr *eth = (void *)skb->data;
1155 char *src = eth->h_source;
Maya Erezb729aaf2016-01-17 12:39:09 +02001156 struct vring_tx_data *txdata;
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001157
1158 /* find 1-st vring eligible for data */
1159 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1160 v = &wil->vring_tx[i];
Maya Erezb729aaf2016-01-17 12:39:09 +02001161 txdata = &wil->vring_tx_data[i];
1162 if (!v->va || !txdata->enabled)
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001163 continue;
1164
1165 cid = wil->vring2cid_tid[i][0];
1166 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1167 continue;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001168 if (!wil->vring_tx_data[i].dot1x_open &&
1169 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001170 continue;
1171
1172 /* don't Tx back to source when re-routing Rx->Tx at the AP */
1173 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1174 continue;
1175
1176 goto found;
1177 }
1178
1179 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1180
1181 return NULL;
1182
1183found:
1184 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
1185 wil_set_da_for_vring(wil, skb, i);
1186
1187 /* find other active vrings and duplicate skb for each */
1188 for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
1189 v2 = &wil->vring_tx[i];
1190 if (!v2->va)
1191 continue;
1192 cid = wil->vring2cid_tid[i][0];
1193 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1194 continue;
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001195 if (!wil->vring_tx_data[i].dot1x_open &&
1196 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
Vladimir Kondratiev70812422015-03-15 16:00:24 +02001197 continue;
1198
1199 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1200 continue;
1201
1202 skb2 = skb_copy(skb, GFP_ATOMIC);
1203 if (skb2) {
1204 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
1205 wil_set_da_for_vring(wil, skb2, i);
1206 wil_tx_vring(wil, v2, skb2);
1207 } else {
1208 wil_err(wil, "skb_copy failed\n");
1209 }
1210 }
1211
1212 return v;
1213}
1214
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001215static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
1216 int vring_index)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001217{
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001218 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001219 d->dma.ip_length = 0;
1220 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
1221 d->dma.b11 = 0/*14 | BIT(7)*/;
1222 d->dma.error = 0;
1223 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001224 d->dma.length = cpu_to_le16((u16)len);
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001225 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001226 d->mac.d[0] = 0;
1227 d->mac.d[1] = 0;
1228 d->mac.d[2] = 0;
1229 d->mac.ucode_cmd = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001230 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
1231 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
1232 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
1233
1234 return 0;
1235}
1236
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001237static inline
1238void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
1239{
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001240 d->mac.d[2] |= (nr_frags << MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001241}
1242
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001243/**
1244 * Sets the descriptor @d up for csum and/or TSO offloading. The corresponding
1245 * @skb is used to obtain the protocol and headers length.
1246 * @tso_desc_type is a descriptor type for TSO: 0 - a header, 1 - first data,
1247 * 2 - middle, 3 - last descriptor.
1248 */
1249
1250static void wil_tx_desc_offload_setup_tso(struct vring_tx_desc *d,
1251 struct sk_buff *skb,
1252 int tso_desc_type, bool is_ipv4,
1253 int tcp_hdr_len, int skb_net_hdr_len)
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001254{
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001255 d->dma.b11 = ETH_HLEN; /* MAC header length */
1256 d->dma.b11 |= is_ipv4 << DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS;
1257
1258 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1259 /* L4 header len: TCP header length */
1260 d->dma.d0 |= (tcp_hdr_len & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1261
1262 /* Setup TSO: bit and desc type */
1263 d->dma.d0 |= (BIT(DMA_CFG_DESC_TX_0_TCP_SEG_EN_POS)) |
1264 (tso_desc_type << DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS);
1265 d->dma.d0 |= (is_ipv4 << DMA_CFG_DESC_TX_0_IPV4_CHECKSUM_EN_POS);
1266
1267 d->dma.ip_length = skb_net_hdr_len;
1268 /* Enable TCP/UDP checksum */
1269 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1270 /* Calculate pseudo-header */
1271 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1272}
1273
1274/**
1275 * Sets the descriptor @d up for csum. The corresponding
1276 * @skb is used to obtain the protocol and headers length.
1277 * Returns the protocol: 0 - not TCP, 1 - TCPv4, 2 - TCPv6.
1278 * Note, if d==NULL, the function only returns the protocol result.
1279 *
1280 * It is very similar to previous wil_tx_desc_offload_setup_tso. This
1281 * is "if unrolling" to optimize the critical path.
1282 */
1283
1284static int wil_tx_desc_offload_setup(struct vring_tx_desc *d,
1285 struct sk_buff *skb){
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001286 int protocol;
1287
1288 if (skb->ip_summed != CHECKSUM_PARTIAL)
1289 return 0;
1290
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +02001291 d->dma.b11 = ETH_HLEN; /* MAC header length */
1292
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001293 switch (skb->protocol) {
1294 case cpu_to_be16(ETH_P_IP):
1295 protocol = ip_hdr(skb)->protocol;
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +02001296 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001297 break;
1298 case cpu_to_be16(ETH_P_IPV6):
1299 protocol = ipv6_hdr(skb)->nexthdr;
1300 break;
1301 default:
1302 return -EINVAL;
1303 }
1304
1305 switch (protocol) {
1306 case IPPROTO_TCP:
1307 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1308 /* L4 header len: TCP header length */
1309 d->dma.d0 |=
1310 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1311 break;
1312 case IPPROTO_UDP:
1313 /* L4 header len: UDP header length */
1314 d->dma.d0 |=
1315 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1316 break;
1317 default:
1318 return -EINVAL;
1319 }
1320
1321 d->dma.ip_length = skb_network_header_len(skb);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001322 /* Enable TCP/UDP checksum */
1323 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1324 /* Calculate pseudo-header */
1325 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1326
1327 return 0;
1328}
1329
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001330static inline void wil_tx_last_desc(struct vring_tx_desc *d)
1331{
1332 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS) |
1333 BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS) |
1334 BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
1335}
1336
1337static inline void wil_set_tx_desc_last_tso(volatile struct vring_tx_desc *d)
1338{
1339 d->dma.d0 |= wil_tso_type_lst <<
1340 DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS;
1341}
1342
1343static int __wil_tx_vring_tso(struct wil6210_priv *wil, struct vring *vring,
1344 struct sk_buff *skb)
1345{
1346 struct device *dev = wil_to_dev(wil);
1347
1348 /* point to descriptors in shared memory */
1349 volatile struct vring_tx_desc *_desc = NULL, *_hdr_desc,
1350 *_first_desc = NULL;
1351
1352 /* pointers to shadow descriptors */
1353 struct vring_tx_desc desc_mem, hdr_desc_mem, first_desc_mem,
1354 *d = &hdr_desc_mem, *hdr_desc = &hdr_desc_mem,
1355 *first_desc = &first_desc_mem;
1356
1357 /* pointer to shadow descriptors' context */
1358 struct wil_ctx *hdr_ctx, *first_ctx = NULL;
1359
1360 int descs_used = 0; /* total number of used descriptors */
1361 int sg_desc_cnt = 0; /* number of descriptors for current mss*/
1362
1363 u32 swhead = vring->swhead;
1364 int used, avail = wil_vring_avail_tx(vring);
1365 int nr_frags = skb_shinfo(skb)->nr_frags;
1366 int min_desc_required = nr_frags + 1;
1367 int mss = skb_shinfo(skb)->gso_size; /* payload size w/o headers */
1368 int f, len, hdrlen, headlen;
1369 int vring_index = vring - wil->vring_tx;
1370 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1371 uint i = swhead;
1372 dma_addr_t pa;
1373 const skb_frag_t *frag = NULL;
1374 int rem_data = mss;
1375 int lenmss;
1376 int hdr_compensation_need = true;
1377 int desc_tso_type = wil_tso_type_first;
1378 bool is_ipv4;
1379 int tcp_hdr_len;
1380 int skb_net_hdr_len;
1381 int gso_type;
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001382 int rc = -EINVAL;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001383
Lazar Alexei3190cc62017-02-23 14:34:14 +02001384 wil_dbg_txrx(wil, "tx_vring_tso: %d bytes to vring %d\n", skb->len,
1385 vring_index);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001386
1387 if (unlikely(!txdata->enabled))
1388 return -EINVAL;
1389
1390 /* A typical page 4K is 3-4 payloads, we assume each fragment
1391 * is a full payload, that's how min_desc_required has been
1392 * calculated. In real we might need more or less descriptors,
1393 * this is the initial check only.
1394 */
1395 if (unlikely(avail < min_desc_required)) {
1396 wil_err_ratelimited(wil,
1397 "TSO: Tx ring[%2d] full. No space for %d fragments\n",
1398 vring_index, min_desc_required);
1399 return -ENOMEM;
1400 }
1401
1402 /* Header Length = MAC header len + IP header len + TCP header len*/
1403 hdrlen = ETH_HLEN +
1404 (int)skb_network_header_len(skb) +
1405 tcp_hdrlen(skb);
1406
1407 gso_type = skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4);
1408 switch (gso_type) {
1409 case SKB_GSO_TCPV4:
1410 /* TCP v4, zero out the IP length and IPv4 checksum fields
1411 * as required by the offloading doc
1412 */
1413 ip_hdr(skb)->tot_len = 0;
1414 ip_hdr(skb)->check = 0;
1415 is_ipv4 = true;
1416 break;
1417 case SKB_GSO_TCPV6:
1418 /* TCP v6, zero out the payload length */
1419 ipv6_hdr(skb)->payload_len = 0;
1420 is_ipv4 = false;
1421 break;
1422 default:
1423 /* other than TCPv4 or TCPv6 types are not supported for TSO.
1424 * It is also illegal for both to be set simultaneously
1425 */
1426 return -EINVAL;
1427 }
1428
1429 if (skb->ip_summed != CHECKSUM_PARTIAL)
1430 return -EINVAL;
1431
1432 /* tcp header length and skb network header length are fixed for all
1433 * packet's descriptors - read then once here
1434 */
1435 tcp_hdr_len = tcp_hdrlen(skb);
1436 skb_net_hdr_len = skb_network_header_len(skb);
1437
1438 _hdr_desc = &vring->va[i].tx;
1439
1440 pa = dma_map_single(dev, skb->data, hdrlen, DMA_TO_DEVICE);
1441 if (unlikely(dma_mapping_error(dev, pa))) {
1442 wil_err(wil, "TSO: Skb head DMA map error\n");
1443 goto err_exit;
1444 }
1445
1446 wil_tx_desc_map(hdr_desc, pa, hdrlen, vring_index);
1447 wil_tx_desc_offload_setup_tso(hdr_desc, skb, wil_tso_type_hdr, is_ipv4,
1448 tcp_hdr_len, skb_net_hdr_len);
1449 wil_tx_last_desc(hdr_desc);
1450
1451 vring->ctx[i].mapped_as = wil_mapped_as_single;
1452 hdr_ctx = &vring->ctx[i];
1453
1454 descs_used++;
1455 headlen = skb_headlen(skb) - hdrlen;
1456
1457 for (f = headlen ? -1 : 0; f < nr_frags; f++) {
1458 if (headlen) {
1459 len = headlen;
1460 wil_dbg_txrx(wil, "TSO: process skb head, len %u\n",
1461 len);
1462 } else {
1463 frag = &skb_shinfo(skb)->frags[f];
1464 len = frag->size;
1465 wil_dbg_txrx(wil, "TSO: frag[%d]: len %u\n", f, len);
1466 }
1467
1468 while (len) {
1469 wil_dbg_txrx(wil,
1470 "TSO: len %d, rem_data %d, descs_used %d\n",
1471 len, rem_data, descs_used);
1472
1473 if (descs_used == avail) {
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001474 wil_err_ratelimited(wil, "TSO: ring overflow\n");
1475 rc = -ENOMEM;
1476 goto mem_error;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001477 }
1478
1479 lenmss = min_t(int, rem_data, len);
1480 i = (swhead + descs_used) % vring->size;
1481 wil_dbg_txrx(wil, "TSO: lenmss %d, i %d\n", lenmss, i);
1482
1483 if (!headlen) {
1484 pa = skb_frag_dma_map(dev, frag,
1485 frag->size - len, lenmss,
1486 DMA_TO_DEVICE);
1487 vring->ctx[i].mapped_as = wil_mapped_as_page;
1488 } else {
1489 pa = dma_map_single(dev,
1490 skb->data +
1491 skb_headlen(skb) - headlen,
1492 lenmss,
1493 DMA_TO_DEVICE);
1494 vring->ctx[i].mapped_as = wil_mapped_as_single;
1495 headlen -= lenmss;
1496 }
1497
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001498 if (unlikely(dma_mapping_error(dev, pa))) {
1499 wil_err(wil, "TSO: DMA map page error\n");
1500 goto mem_error;
1501 }
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001502
1503 _desc = &vring->va[i].tx;
1504
1505 if (!_first_desc) {
1506 _first_desc = _desc;
1507 first_ctx = &vring->ctx[i];
1508 d = first_desc;
1509 } else {
1510 d = &desc_mem;
1511 }
1512
1513 wil_tx_desc_map(d, pa, lenmss, vring_index);
1514 wil_tx_desc_offload_setup_tso(d, skb, desc_tso_type,
1515 is_ipv4, tcp_hdr_len,
1516 skb_net_hdr_len);
1517
1518 /* use tso_type_first only once */
1519 desc_tso_type = wil_tso_type_mid;
1520
1521 descs_used++; /* desc used so far */
1522 sg_desc_cnt++; /* desc used for this segment */
1523 len -= lenmss;
1524 rem_data -= lenmss;
1525
1526 wil_dbg_txrx(wil,
1527 "TSO: len %d, rem_data %d, descs_used %d, sg_desc_cnt %d,\n",
1528 len, rem_data, descs_used, sg_desc_cnt);
1529
1530 /* Close the segment if reached mss size or last frag*/
1531 if (rem_data == 0 || (f == nr_frags - 1 && len == 0)) {
1532 if (hdr_compensation_need) {
1533 /* first segment include hdr desc for
1534 * release
1535 */
1536 hdr_ctx->nr_frags = sg_desc_cnt;
1537 wil_tx_desc_set_nr_frags(first_desc,
1538 sg_desc_cnt +
1539 1);
1540 hdr_compensation_need = false;
1541 } else {
1542 wil_tx_desc_set_nr_frags(first_desc,
1543 sg_desc_cnt);
1544 }
1545 first_ctx->nr_frags = sg_desc_cnt - 1;
1546
1547 wil_tx_last_desc(d);
1548
1549 /* first descriptor may also be the last
1550 * for this mss - make sure not to copy
1551 * it twice
1552 */
1553 if (first_desc != d)
1554 *_first_desc = *first_desc;
1555
1556 /*last descriptor will be copied at the end
1557 * of this TS processing
1558 */
1559 if (f < nr_frags - 1 || len > 0)
1560 *_desc = *d;
1561
1562 rem_data = mss;
1563 _first_desc = NULL;
1564 sg_desc_cnt = 0;
1565 } else if (first_desc != d) /* update mid descriptor */
1566 *_desc = *d;
1567 }
1568 }
1569
1570 /* first descriptor may also be the last.
1571 * in this case d pointer is invalid
1572 */
1573 if (_first_desc == _desc)
1574 d = first_desc;
1575
1576 /* Last data descriptor */
1577 wil_set_tx_desc_last_tso(d);
1578 *_desc = *d;
1579
1580 /* Fill the total number of descriptors in first desc (hdr)*/
1581 wil_tx_desc_set_nr_frags(hdr_desc, descs_used);
1582 *_hdr_desc = *hdr_desc;
1583
1584 /* hold reference to skb
1585 * to prevent skb release before accounting
1586 * in case of immediate "tx done"
1587 */
1588 vring->ctx[i].skb = skb_get(skb);
1589
1590 /* performance monitoring */
1591 used = wil_vring_used_tx(vring);
1592 if (wil_val_in_range(vring_idle_trsh,
1593 used, used + descs_used)) {
1594 txdata->idle += get_cycles() - txdata->last_idle;
1595 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1596 vring_index, used, used + descs_used);
1597 }
1598
Maya Erezeb26cff2016-05-16 22:23:30 +03001599 /* Make sure to advance the head only after descriptor update is done.
1600 * This will prevent a race condition where the completion thread
1601 * will see the DU bit set from previous run and will handle the
1602 * skb before it was completed.
1603 */
1604 wmb();
1605
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001606 /* advance swhead */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001607 wil_vring_advance_head(vring, descs_used);
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001608 wil_dbg_txrx(wil, "TSO: Tx swhead %d -> %d\n", swhead, vring->swhead);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001609
1610 /* make sure all writes to descriptors (shared memory) are done before
1611 * committing them to HW
1612 */
1613 wmb();
1614
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +03001615 wil_w(wil, vring->hwtail, vring->swhead);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001616 return 0;
1617
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001618mem_error:
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001619 while (descs_used > 0) {
1620 struct wil_ctx *ctx;
1621
Maya Ereza1526f72016-05-16 22:23:33 +03001622 i = (swhead + descs_used - 1) % vring->size;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001623 d = (struct vring_tx_desc *)&vring->va[i].tx;
1624 _desc = &vring->va[i].tx;
1625 *d = *_desc;
1626 _desc->dma.status = TX_DMA_STATUS_DU;
1627 ctx = &vring->ctx[i];
1628 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001629 memset(ctx, 0, sizeof(*ctx));
1630 descs_used--;
1631 }
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001632err_exit:
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001633 return rc;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001634}
1635
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001636static int __wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1637 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001638{
1639 struct device *dev = wil_to_dev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001640 struct vring_tx_desc dd, *d = &dd;
1641 volatile struct vring_tx_desc *_d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001642 u32 swhead = vring->swhead;
1643 int avail = wil_vring_avail_tx(vring);
1644 int nr_frags = skb_shinfo(skb)->nr_frags;
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001645 uint f = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001646 int vring_index = vring - wil->vring_tx;
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001647 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001648 uint i = swhead;
1649 dma_addr_t pa;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001650 int used;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001651 bool mcast = (vring_index == wil->bcast_vring);
1652 uint len = skb_headlen(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001653
Lazar Alexei3190cc62017-02-23 14:34:14 +02001654 wil_dbg_txrx(wil, "tx_vring: %d bytes to vring %d\n", skb->len,
1655 vring_index);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001656
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001657 if (unlikely(!txdata->enabled))
1658 return -EINVAL;
1659
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001660 if (unlikely(avail < 1 + nr_frags)) {
Vladimir Kondratiev70801e12014-12-01 15:36:03 +02001661 wil_err_ratelimited(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001662 "Tx ring[%2d] full. No space for %d fragments\n",
1663 vring_index, 1 + nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001664 return -ENOMEM;
1665 }
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001666 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001667
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001668 pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001669
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001670 wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index,
1671 skb_headlen(skb), skb->data, &pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +02001672 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001673 skb->data, skb_headlen(skb), false);
1674
1675 if (unlikely(dma_mapping_error(dev, pa)))
1676 return -EINVAL;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001677 vring->ctx[i].mapped_as = wil_mapped_as_single;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001678 /* 1-st segment */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001679 wil_tx_desc_map(d, pa, len, vring_index);
1680 if (unlikely(mcast)) {
1681 d->mac.d[0] |= BIT(MAC_CFG_DESC_TX_0_MCS_EN_POS); /* MCS 0 */
Vladimir Kondratiev230d8442015-04-30 16:25:10 +03001682 if (unlikely(len > WIL_BCAST_MCS0_LIMIT)) /* set MCS 1 */
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001683 d->mac.d[0] |= (1 << MAC_CFG_DESC_TX_0_MCS_INDEX_POS);
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001684 }
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001685 /* Process TCP/UDP checksum offloading */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001686 if (unlikely(wil_tx_desc_offload_setup(d, skb))) {
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001687 wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001688 vring_index);
1689 goto dma_error;
1690 }
1691
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001692 vring->ctx[i].nr_frags = nr_frags;
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001693 wil_tx_desc_set_nr_frags(d, nr_frags + 1);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001694
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001695 /* middle segments */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +03001696 for (; f < nr_frags; f++) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001697 const struct skb_frag_struct *frag =
1698 &skb_shinfo(skb)->frags[f];
1699 int len = skb_frag_size(frag);
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001700
Vladimir Kondratieve59d16c2015-02-01 10:55:12 +02001701 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001702 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1703 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1704 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001705 i = (swhead + f + 1) % vring->size;
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001706 _d = &vring->va[i].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001707 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001708 DMA_TO_DEVICE);
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001709 if (unlikely(dma_mapping_error(dev, pa))) {
1710 wil_err(wil, "Tx[%2d] failed to map fragment\n",
1711 vring_index);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001712 goto dma_error;
Hamad Kadmanye3d2ed92015-10-25 15:59:22 +02001713 }
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001714 vring->ctx[i].mapped_as = wil_mapped_as_page;
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +03001715 wil_tx_desc_map(d, pa, len, vring_index);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001716 /* no need to check return code -
1717 * if it succeeded for 1-st descriptor,
1718 * it will succeed here too
1719 */
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001720 wil_tx_desc_offload_setup(d, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001721 }
1722 /* for the last seg only */
1723 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
Kirshenbaum Erez668b2bb2013-06-23 12:59:35 +03001724 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001725 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001726 *_d = *d;
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001727 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1728 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1729 (const void *)d, sizeof(*d), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001730
Vladimir Kondratiev6cdadd42013-07-11 18:03:41 +03001731 /* hold reference to skb
1732 * to prevent skb release before accounting
1733 * in case of immediate "tx done"
1734 */
1735 vring->ctx[i].skb = skb_get(skb);
1736
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001737 /* performance monitoring */
1738 used = wil_vring_used_tx(vring);
1739 if (wil_val_in_range(vring_idle_trsh,
1740 used, used + nr_frags + 1)) {
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001741 txdata->idle += get_cycles() - txdata->last_idle;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001742 wil_dbg_txrx(wil, "Ring[%2d] not idle %d -> %d\n",
1743 vring_index, used, used + nr_frags + 1);
1744 }
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03001745
Maya Erezeb26cff2016-05-16 22:23:30 +03001746 /* Make sure to advance the head only after descriptor update is done.
1747 * This will prevent a race condition where the completion thread
1748 * will see the DU bit set from previous run and will handle the
1749 * skb before it was completed.
1750 */
1751 wmb();
1752
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001753 /* advance swhead */
1754 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02001755 wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead,
1756 vring->swhead);
Vladimir Kondratiev98658092013-05-12 14:43:35 +03001757 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001758
1759 /* make sure all writes to descriptors (shared memory) are done before
1760 * committing them to HW
1761 */
1762 wmb();
1763
Vladimir Kondratievb9eeb512015-07-30 13:52:03 +03001764 wil_w(wil, vring->hwtail, vring->swhead);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001765
1766 return 0;
1767 dma_error:
1768 /* unmap what we have mapped */
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001769 nr_frags = f + 1; /* frags mapped + one for skb head */
1770 for (f = 0; f < nr_frags; f++) {
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001771 struct wil_ctx *ctx;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +03001772
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001773 i = (swhead + f) % vring->size;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +03001774 ctx = &vring->ctx[i];
Vladimir Kondratiev8fe59622014-09-10 16:34:34 +03001775 _d = &vring->va[i].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +03001776 *d = *_d;
1777 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02001778 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001779
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03001780 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001781 }
1782
1783 return -EINVAL;
1784}
1785
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001786static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1787 struct sk_buff *skb)
1788{
1789 int vring_index = vring - wil->vring_tx;
1790 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1791 int rc;
1792
1793 spin_lock(&txdata->lock);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001794
1795 rc = (skb_is_gso(skb) ? __wil_tx_vring_tso : __wil_tx_vring)
1796 (wil, vring, skb);
1797
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001798 spin_unlock(&txdata->lock);
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03001799
Vladimir Kondratiev5933a062015-02-01 10:55:13 +02001800 return rc;
1801}
1802
Dedy Lansky970a98f2016-12-06 08:26:49 +02001803/**
1804 * Check status of tx vrings and stop/wake net queues if needed
1805 *
1806 * This function does one of two checks:
1807 * In case check_stop is true, will check if net queues need to be stopped. If
1808 * the conditions for stopping are met, netif_tx_stop_all_queues() is called.
1809 * In case check_stop is false, will check if net queues need to be waked. If
1810 * the conditions for waking are met, netif_tx_wake_all_queues() is called.
1811 * vring is the vring which is currently being modified by either adding
1812 * descriptors (tx) into it or removing descriptors (tx complete) from it. Can
1813 * be null when irrelevant (e.g. connect/disconnect events).
1814 *
1815 * The implementation is to stop net queues if modified vring has low
1816 * descriptor availability. Wake if all vrings are not in low descriptor
1817 * availability and modified vring has high descriptor availability.
1818 */
1819static inline void __wil_update_net_queues(struct wil6210_priv *wil,
1820 struct vring *vring,
1821 bool check_stop)
1822{
1823 int i;
1824
1825 if (vring)
1826 wil_dbg_txrx(wil, "vring %d, check_stop=%d, stopped=%d",
1827 (int)(vring - wil->vring_tx), check_stop,
1828 wil->net_queue_stopped);
1829 else
1830 wil_dbg_txrx(wil, "check_stop=%d, stopped=%d",
1831 check_stop, wil->net_queue_stopped);
1832
1833 if (check_stop == wil->net_queue_stopped)
1834 /* net queues already in desired state */
1835 return;
1836
1837 if (check_stop) {
1838 if (!vring || unlikely(wil_vring_avail_low(vring))) {
1839 /* not enough room in the vring */
1840 netif_tx_stop_all_queues(wil_to_ndev(wil));
1841 wil->net_queue_stopped = true;
1842 wil_dbg_txrx(wil, "netif_tx_stop called\n");
1843 }
1844 return;
1845 }
1846
1847 /* check wake */
1848 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1849 struct vring *cur_vring = &wil->vring_tx[i];
1850 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
1851
1852 if (!cur_vring->va || !txdata->enabled || cur_vring == vring)
1853 continue;
1854
1855 if (wil_vring_avail_low(cur_vring)) {
1856 wil_dbg_txrx(wil, "vring %d full, can't wake\n",
1857 (int)(cur_vring - wil->vring_tx));
1858 return;
1859 }
1860 }
1861
1862 if (!vring || wil_vring_avail_high(vring)) {
1863 /* enough room in the vring */
1864 wil_dbg_txrx(wil, "calling netif_tx_wake\n");
1865 netif_tx_wake_all_queues(wil_to_ndev(wil));
1866 wil->net_queue_stopped = false;
1867 }
1868}
1869
1870void wil_update_net_queues(struct wil6210_priv *wil, struct vring *vring,
1871 bool check_stop)
1872{
1873 spin_lock(&wil->net_queue_lock);
1874 __wil_update_net_queues(wil, vring, check_stop);
1875 spin_unlock(&wil->net_queue_lock);
1876}
1877
1878void wil_update_net_queues_bh(struct wil6210_priv *wil, struct vring *vring,
1879 bool check_stop)
1880{
1881 spin_lock_bh(&wil->net_queue_lock);
1882 __wil_update_net_queues(wil, vring, check_stop);
1883 spin_unlock_bh(&wil->net_queue_lock);
1884}
1885
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001886netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1887{
1888 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +02001889 struct ethhdr *eth = (void *)skb->data;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001890 bool bcast = is_multicast_ether_addr(eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001891 struct vring *vring;
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001892 static bool pr_once_fw;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001893 int rc;
1894
Lazar Alexei3190cc62017-02-23 14:34:14 +02001895 wil_dbg_txrx(wil, "start_xmit\n");
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001896 if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001897 if (!pr_once_fw) {
1898 wil_err(wil, "FW not ready\n");
1899 pr_once_fw = true;
1900 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001901 goto drop;
1902 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001903 if (unlikely(!test_bit(wil_status_fwconnected, wil->status))) {
Maya Erezd8ed0432016-04-26 14:41:39 +03001904 wil_dbg_ratelimited(wil, "FW not connected, packet dropped\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001905 goto drop;
1906 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001907 if (unlikely(wil->wdev->iftype == NL80211_IFTYPE_MONITOR)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001908 wil_err(wil, "Xmit in monitor mode not supported\n");
1909 goto drop;
1910 }
Vladimir Kondratievaa27dea2014-03-17 15:34:11 +02001911 pr_once_fw = false;
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001912
1913 /* find vring */
Lior Davidb1c0bbf2017-02-08 13:14:13 +02001914 if (wil->wdev->iftype == NL80211_IFTYPE_STATION && !wil->pbss) {
1915 /* in STA mode (ESS), all to same VRING (to AP) */
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001916 vring = wil_find_tx_vring_sta(wil, skb);
Lior Davidb1c0bbf2017-02-08 13:14:13 +02001917 } else if (bcast) {
1918 if (wil->pbss)
1919 /* in pbss, no bcast VRING - duplicate skb in
1920 * all stations VRINGs
1921 */
1922 vring = wil_find_tx_bcast_2(wil, skb);
1923 else if (wil->wdev->iftype == NL80211_IFTYPE_AP)
1924 /* AP has a dedicated bcast VRING */
1925 vring = wil_find_tx_bcast_1(wil, skb);
1926 else
1927 /* unexpected combination, fallback to duplicating
1928 * the skb in all stations VRINGs
1929 */
1930 vring = wil_find_tx_bcast_2(wil, skb);
1931 } else {
1932 /* unicast, find specific VRING by dest. address */
1933 vring = wil_find_tx_ucast(wil, skb);
Vladimir Kondratiev54ed90a2014-12-23 09:47:15 +02001934 }
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001935 if (unlikely(!vring)) {
Vladimir Kondratiev5aed1392014-06-16 19:37:15 +03001936 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001937 goto drop;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001938 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +03001939 /* set up vring entry */
1940 rc = wil_tx_vring(wil, vring, skb);
1941
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001942 switch (rc) {
1943 case 0:
Dedy Lansky970a98f2016-12-06 08:26:49 +02001944 /* shall we stop net queues? */
1945 wil_update_net_queues_bh(wil, vring, true);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02001946 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001947 dev_kfree_skb_any(skb);
1948 return NETDEV_TX_OK;
1949 case -ENOMEM:
1950 return NETDEV_TX_BUSY;
1951 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +02001952 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001953 }
1954 drop:
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001955 ndev->stats.tx_dropped++;
1956 dev_kfree_skb_any(skb);
1957
1958 return NET_XMIT_DROP;
1959}
1960
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02001961static inline bool wil_need_txstat(struct sk_buff *skb)
1962{
1963 struct ethhdr *eth = (void *)skb->data;
1964
1965 return is_unicast_ether_addr(eth->h_dest) && skb->sk &&
1966 (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS);
1967}
1968
1969static inline void wil_consume_skb(struct sk_buff *skb, bool acked)
1970{
1971 if (unlikely(wil_need_txstat(skb)))
1972 skb_complete_wifi_ack(skb, acked);
1973 else
1974 acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb);
1975}
1976
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001977/**
1978 * Clean up transmitted skb's from the Tx VRING
1979 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001980 * Return number of descriptors cleared
1981 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001982 * Safe to call from IRQ
1983 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001984int wil_tx_complete(struct wil6210_priv *wil, int ringid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001985{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02001986 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001987 struct device *dev = wil_to_dev(wil);
1988 struct vring *vring = &wil->vring_tx[ringid];
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02001989 struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001990 int done = 0;
Vladimir Kondratievc8b78b52014-02-27 16:20:49 +02001991 int cid = wil->vring2cid_tid[ringid][0];
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02001992 struct wil_net_stats *stats = NULL;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02001993 volatile struct vring_tx_desc *_d;
Vladimir Shulman0436fd92015-02-15 14:02:34 +02001994 int used_before_complete;
1995 int used_new;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001996
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02001997 if (unlikely(!vring->va)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001998 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03001999 return 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002000 }
2001
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002002 if (unlikely(!txdata->enabled)) {
Vladimir Kondratiev097638a2014-03-17 15:34:25 +02002003 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
2004 return 0;
2005 }
2006
Lazar Alexei3190cc62017-02-23 14:34:14 +02002007 wil_dbg_txrx(wil, "tx_complete: (%d)\n", ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002008
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002009 used_before_complete = wil_vring_used_tx(vring);
2010
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002011 if (cid < WIL6210_MAX_CID)
2012 stats = &wil->sta[cid].stats;
2013
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002014 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002015 int new_swtail;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03002016 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002017 /**
2018 * For the fragmented skb, HW will set DU bit only for the
Vladimir Kondratiev3d4bde12015-07-30 13:51:56 +03002019 * last fragment. look for it.
2020 * In TSO the first DU will include hdr desc
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002021 */
2022 int lf = (vring->swtail + ctx->nr_frags) % vring->size;
2023 /* TODO: check we are not past head */
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +03002024
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002025 _d = &vring->va[lf].tx;
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002026 if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002027 break;
2028
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002029 new_swtail = (lf + 1) % vring->size;
2030 while (vring->swtail != new_swtail) {
2031 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002032 u16 dmalen;
Vladimir Kondratiev76dfa4b2014-07-14 09:49:39 +03002033 struct sk_buff *skb;
2034
2035 ctx = &vring->ctx[vring->swtail];
2036 skb = ctx->skb;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002037 _d = &vring->va[vring->swtail].tx;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002038
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002039 *d = *_d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +03002040
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002041 dmalen = le16_to_cpu(d->dma.length);
2042 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
2043 d->dma.error);
2044 wil_dbg_txrx(wil,
Vladimir Kondratiev5b29c572015-02-01 10:55:14 +02002045 "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
2046 ringid, vring->swtail, dmalen,
2047 d->dma.status, d->dma.error);
2048 wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002049 (const void *)d, sizeof(*d), false);
2050
Vladimir Kondratiev2232abd2014-03-17 15:34:09 +02002051 wil_txdesc_unmap(dev, d, ctx);
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002052
2053 if (skb) {
Vladimir Kondratiev33c477f2015-02-15 14:02:33 +02002054 if (likely(d->dma.error == 0)) {
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002055 ndev->stats.tx_packets++;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002056 ndev->stats.tx_bytes += skb->len;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002057 if (stats) {
2058 stats->tx_packets++;
2059 stats->tx_bytes += skb->len;
2060 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002061 } else {
2062 ndev->stats.tx_errors++;
Vladimir Kondratiev41d6b092015-03-15 16:00:23 +02002063 if (stats)
2064 stats->tx_errors++;
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002065 }
Vladimir Kondratiev713c8a22015-01-25 10:52:49 +02002066 wil_consume_skb(skb, d->dma.error == 0);
Vladimir Kondratiev795ce732013-01-28 18:31:00 +02002067 }
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002068 memset(ctx, 0, sizeof(*ctx));
Maya Erezeb26cff2016-05-16 22:23:30 +03002069 /* Make sure the ctx is zeroed before updating the tail
2070 * to prevent a case where wil_tx_vring will see
2071 * this descriptor as used and handle it before ctx zero
2072 * is completed.
2073 */
2074 wmb();
Vladimir Kondratievc2366582014-03-17 15:34:08 +02002075 /* There is no need to touch HW descriptor:
2076 * - ststus bit TX_DMA_STATUS_DU is set by design,
2077 * so hardware will not try to process this desc.,
2078 * - rest of descriptor will be initialized on Tx.
2079 */
2080 vring->swtail = wil_vring_next_tail(vring);
2081 done++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002082 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002083 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03002084
Vladimir Shulman0436fd92015-02-15 14:02:34 +02002085 /* performance monitoring */
2086 used_new = wil_vring_used_tx(vring);
2087 if (wil_val_in_range(vring_idle_trsh,
2088 used_new, used_before_complete)) {
2089 wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
2090 ringid, used_before_complete, used_new);
Vladimir Kondratiev7c0acf82014-06-16 19:37:05 +03002091 txdata->last_idle = get_cycles();
2092 }
Vladimir Kondratiev67c3e1b2014-06-16 19:37:04 +03002093
Dedy Lansky970a98f2016-12-06 08:26:49 +02002094 /* shall we wake net queues? */
2095 if (done)
2096 wil_update_net_queues(wil, vring, false);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +03002097
2098 return done;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08002099}