]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/ath/wil6210/txrx.c
wil6210: branch prediction hints
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / ath / wil6210 / txrx.c
CommitLineData
2be7d22f 1/*
02525a79 2 * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
2be7d22f
VK
3 *
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
2be7d22f 17#include <linux/etherdevice.h>
2be7d22f
VK
18#include <net/ieee80211_radiotap.h>
19#include <linux/if_arp.h>
20#include <linux/moduleparam.h>
504937d4
KE
21#include <linux/ip.h>
22#include <linux/ipv6.h>
23#include <net/ipv6.h>
0786dc4e 24#include <linux/prefetch.h>
2be7d22f
VK
25
26#include "wil6210.h"
27#include "wmi.h"
28#include "txrx.h"
98658095 29#include "trace.h"
2be7d22f
VK
30
31static bool rtap_include_phy_info;
32module_param(rtap_include_phy_info, bool, S_IRUGO);
33MODULE_PARM_DESC(rtap_include_phy_info,
34 " Include PHY info in the radiotap header, default - no");
35
36static inline int wil_vring_is_empty(struct vring *vring)
37{
38 return vring->swhead == vring->swtail;
39}
40
41static inline u32 wil_vring_next_tail(struct vring *vring)
42{
43 return (vring->swtail + 1) % vring->size;
44}
45
46static inline void wil_vring_advance_head(struct vring *vring, int n)
47{
48 vring->swhead = (vring->swhead + n) % vring->size;
49}
50
51static inline int wil_vring_is_full(struct vring *vring)
52{
53 return wil_vring_next_tail(vring) == vring->swhead;
54}
8fe59627 55
2be7d22f
VK
56/*
57 * Available space in Tx Vring
58 */
59static inline int wil_vring_avail_tx(struct vring *vring)
60{
61 u32 swhead = vring->swhead;
62 u32 swtail = vring->swtail;
63 int used = (vring->size + swhead - swtail) % vring->size;
64
65 return vring->size - used - 1;
66}
67
5bb6423e
VK
68/**
69 * wil_vring_wmark_low - low watermark for available descriptor space
70 */
71static inline int wil_vring_wmark_low(struct vring *vring)
72{
73 return vring->size/8;
74}
75
76/**
77 * wil_vring_wmark_high - high watermark for available descriptor space
78 */
79static inline int wil_vring_wmark_high(struct vring *vring)
80{
81 return vring->size/4;
82}
83
2be7d22f
VK
84static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
85{
86 struct device *dev = wil_to_dev(wil);
87 size_t sz = vring->size * sizeof(vring->va[0]);
88 uint i;
89
9cf10d62
VK
90 wil_dbg_misc(wil, "%s()\n", __func__);
91
2be7d22f
VK
92 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
93
94 vring->swhead = 0;
95 vring->swtail = 0;
f88f113a 96 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
2be7d22f 97 if (!vring->ctx) {
2be7d22f
VK
98 vring->va = NULL;
99 return -ENOMEM;
100 }
101 /*
102 * vring->va should be aligned on its size rounded up to power of 2
103 * This is granted by the dma_alloc_coherent
104 */
105 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
106 if (!vring->va) {
2be7d22f
VK
107 kfree(vring->ctx);
108 vring->ctx = NULL;
109 return -ENOMEM;
110 }
111 /* initially, all descriptors are SW owned
112 * For Tx and Rx, ownership bit is at the same location, thus
113 * we can use any
114 */
115 for (i = 0; i < vring->size; i++) {
8fe59627
VK
116 volatile struct vring_tx_desc *_d = &vring->va[i].tx;
117
68ada71e 118 _d->dma.status = TX_DMA_STATUS_DU;
2be7d22f
VK
119 }
120
39c52ee8
VK
121 wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
122 vring->va, &vring->pa, vring->ctx);
2be7d22f
VK
123
124 return 0;
125}
126
2232abd5
VK
127static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d,
128 struct wil_ctx *ctx)
129{
130 dma_addr_t pa = wil_desc_addr(&d->dma.addr);
131 u16 dmalen = le16_to_cpu(d->dma.length);
8fe59627 132
2232abd5
VK
133 switch (ctx->mapped_as) {
134 case wil_mapped_as_single:
135 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
136 break;
137 case wil_mapped_as_page:
138 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
139 break;
140 default:
141 break;
142 }
143}
144
2be7d22f
VK
145static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
146 int tx)
147{
148 struct device *dev = wil_to_dev(wil);
149 size_t sz = vring->size * sizeof(vring->va[0]);
150
ef77285f
VK
151 if (tx) {
152 int vring_index = vring - wil->vring_tx;
153
154 wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
155 vring_index, vring->size, vring->va,
156 &vring->pa, vring->ctx);
157 } else {
158 wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
159 vring->size, vring->va,
160 &vring->pa, vring->ctx);
161 }
162
2be7d22f 163 while (!wil_vring_is_empty(vring)) {
68ada71e 164 dma_addr_t pa;
7e594444 165 u16 dmalen;
f88f113a 166 struct wil_ctx *ctx;
68ada71e 167
2be7d22f 168 if (tx) {
68ada71e
VK
169 struct vring_tx_desc dd, *d = &dd;
170 volatile struct vring_tx_desc *_d =
2be7d22f 171 &vring->va[vring->swtail].tx;
68ada71e 172
f88f113a 173 ctx = &vring->ctx[vring->swtail];
68ada71e 174 *d = *_d;
2232abd5 175 wil_txdesc_unmap(dev, d, ctx);
f88f113a
VK
176 if (ctx->skb)
177 dev_kfree_skb_any(ctx->skb);
2be7d22f
VK
178 vring->swtail = wil_vring_next_tail(vring);
179 } else { /* rx */
68ada71e
VK
180 struct vring_rx_desc dd, *d = &dd;
181 volatile struct vring_rx_desc *_d =
4d1ac072 182 &vring->va[vring->swhead].rx;
68ada71e 183
f88f113a 184 ctx = &vring->ctx[vring->swhead];
68ada71e
VK
185 *d = *_d;
186 pa = wil_desc_addr(&d->dma.addr);
7e594444
VK
187 dmalen = le16_to_cpu(d->dma.length);
188 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
f88f113a 189 kfree_skb(ctx->skb);
2be7d22f
VK
190 wil_vring_advance_head(vring, 1);
191 }
192 }
193 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
194 kfree(vring->ctx);
195 vring->pa = 0;
196 vring->va = NULL;
197 vring->ctx = NULL;
198}
199
200/**
201 * Allocate one skb for Rx VRING
202 *
203 * Safe to call from IRQ
204 */
205static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
206 u32 i, int headroom)
207{
208 struct device *dev = wil_to_dev(wil);
9a06bec9 209 unsigned int sz = mtu_max + ETH_HLEN;
68ada71e 210 struct vring_rx_desc dd, *d = &dd;
8fe59627 211 volatile struct vring_rx_desc *_d = &vring->va[i].rx;
2be7d22f 212 dma_addr_t pa;
2be7d22f 213 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
8fe59627 214
2be7d22f
VK
215 if (unlikely(!skb))
216 return -ENOMEM;
217
218 skb_reserve(skb, headroom);
219 skb_put(skb, sz);
220
221 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
222 if (unlikely(dma_mapping_error(dev, pa))) {
223 kfree_skb(skb);
224 return -ENOMEM;
225 }
226
227 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
68ada71e 228 wil_desc_addr_set(&d->dma.addr, pa);
2be7d22f
VK
229 /* ip_length don't care */
230 /* b11 don't care */
231 /* error don't care */
232 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
7e594444 233 d->dma.length = cpu_to_le16(sz);
68ada71e 234 *_d = *d;
f88f113a 235 vring->ctx[i].skb = skb;
2be7d22f
VK
236
237 return 0;
238}
239
240/**
241 * Adds radiotap header
242 *
243 * Any error indicated as "Bad FCS"
244 *
245 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
246 * - Rx descriptor: 32 bytes
247 * - Phy info
248 */
249static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
33e61169 250 struct sk_buff *skb)
2be7d22f
VK
251{
252 struct wireless_dev *wdev = wil->wdev;
253 struct wil6210_rtap {
254 struct ieee80211_radiotap_header rthdr;
255 /* fields should be in the order of bits in rthdr.it_present */
256 /* flags */
257 u8 flags;
258 /* channel */
259 __le16 chnl_freq __aligned(2);
260 __le16 chnl_flags;
261 /* MCS */
262 u8 mcs_present;
263 u8 mcs_flags;
264 u8 mcs_index;
265 } __packed;
266 struct wil6210_rtap_vendor {
267 struct wil6210_rtap rtap;
268 /* vendor */
269 u8 vendor_oui[3] __aligned(2);
270 u8 vendor_ns;
271 __le16 vendor_skip;
272 u8 vendor_data[0];
273 } __packed;
33e61169 274 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
2be7d22f
VK
275 struct wil6210_rtap_vendor *rtap_vendor;
276 int rtap_len = sizeof(struct wil6210_rtap);
277 int phy_length = 0; /* phy info header size, bytes */
278 static char phy_data[128];
279 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
280
281 if (rtap_include_phy_info) {
282 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
283 /* calculate additional length */
284 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
285 /**
286 * PHY info starts from 8-byte boundary
287 * there are 8-byte lines, last line may be partially
288 * written (HW bug), thus FW configures for last line
289 * to be excessive. Driver skips this last line.
290 */
291 int len = min_t(int, 8 + sizeof(phy_data),
292 wil_rxdesc_phy_length(d));
8fe59627 293
2be7d22f
VK
294 if (len > 8) {
295 void *p = skb_tail_pointer(skb);
296 void *pa = PTR_ALIGN(p, 8);
8fe59627 297
2be7d22f
VK
298 if (skb_tailroom(skb) >= len + (pa - p)) {
299 phy_length = len - 8;
300 memcpy(phy_data, pa, phy_length);
301 }
302 }
303 }
304 rtap_len += phy_length;
305 }
306
307 if (skb_headroom(skb) < rtap_len &&
308 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
309 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
310 return;
311 }
312
313 rtap_vendor = (void *)skb_push(skb, rtap_len);
314 memset(rtap_vendor, 0, rtap_len);
315
316 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
317 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
318 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
319 (1 << IEEE80211_RADIOTAP_FLAGS) |
320 (1 << IEEE80211_RADIOTAP_CHANNEL) |
321 (1 << IEEE80211_RADIOTAP_MCS));
322 if (d->dma.status & RX_DMA_STATUS_ERROR)
323 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
324
325 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
326 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
327
328 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
329 rtap_vendor->rtap.mcs_flags = 0;
330 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
331
332 if (rtap_include_phy_info) {
333 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
334 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
335 /* OUI for Wilocity 04:ce:14 */
336 rtap_vendor->vendor_oui[0] = 0x04;
337 rtap_vendor->vendor_oui[1] = 0xce;
338 rtap_vendor->vendor_oui[2] = 0x14;
339 rtap_vendor->vendor_ns = 1;
340 /* Rx descriptor + PHY data */
341 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
342 phy_length);
343 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
344 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
345 phy_length);
346 }
347}
348
2be7d22f
VK
349/**
350 * reap 1 frame from @swhead
351 *
33e61169
VK
352 * Rx descriptor copied to skb->cb
353 *
2be7d22f
VK
354 * Safe to call from IRQ
355 */
356static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
357 struct vring *vring)
358{
359 struct device *dev = wil_to_dev(wil);
360 struct net_device *ndev = wil_to_ndev(wil);
68ada71e
VK
361 volatile struct vring_rx_desc *_d;
362 struct vring_rx_desc *d;
2be7d22f
VK
363 struct sk_buff *skb;
364 dma_addr_t pa;
9a06bec9 365 unsigned int sz = mtu_max + ETH_HLEN;
7e594444 366 u16 dmalen;
2be7d22f 367 u8 ftype;
c8b78b5f
VK
368 int cid;
369 struct wil_net_stats *stats;
370
33e61169
VK
371 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
372
33c477fd 373 if (unlikely(wil_vring_is_empty(vring)))
2be7d22f
VK
374 return NULL;
375
8fe59627 376 _d = &vring->va[vring->swhead].rx;
33c477fd 377 if (unlikely(!(_d->dma.status & RX_DMA_STATUS_DU))) {
2be7d22f
VK
378 /* it is not error, we just reached end of Rx done area */
379 return NULL;
380 }
381
f88f113a 382 skb = vring->ctx[vring->swhead].skb;
68ada71e
VK
383 d = wil_skb_rxdesc(skb);
384 *d = *_d;
385 pa = wil_desc_addr(&d->dma.addr);
f88f113a 386 vring->ctx[vring->swhead].skb = NULL;
68ada71e
VK
387 wil_vring_advance_head(vring, 1);
388
2be7d22f 389 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
68ada71e
VK
390 dmalen = le16_to_cpu(d->dma.length);
391
392 trace_wil6210_rx(vring->swhead, d);
393 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen);
394 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
395 (const void *)d, sizeof(*d), false);
2be7d22f 396
33c477fd 397 if (unlikely(dmalen > sz)) {
e270045b 398 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
110dea00 399 kfree_skb(skb);
e270045b
VK
400 return NULL;
401 }
7e594444 402 skb_trim(skb, dmalen);
33e61169 403
1cbbcb08
VK
404 prefetch(skb->data);
405
c0d37713
VK
406 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
407 skb->data, skb_headlen(skb), false);
408
c8b78b5f
VK
409 cid = wil_rxdesc_cid(d);
410 stats = &wil->sta[cid].stats;
411 stats->last_mcs_rx = wil_rxdesc_mcs(d);
2be7d22f
VK
412
413 /* use radiotap header only if required */
414 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
33e61169 415 wil_rx_add_radiotap_header(wil, skb);
2be7d22f 416
2be7d22f
VK
417 /* no extra checks if in sniffer mode */
418 if (ndev->type != ARPHRD_ETHER)
419 return skb;
420 /*
421 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
422 * Driver should recognize it by frame type, that is found
423 * in Rx descriptor. If type is not data, it is 802.11 frame as is
424 */
68ada71e 425 ftype = wil_rxdesc_ftype(d) << 2;
33c477fd 426 if (unlikely(ftype != IEEE80211_FTYPE_DATA)) {
7743882d 427 wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
2be7d22f
VK
428 /* TODO: process it */
429 kfree_skb(skb);
430 return NULL;
431 }
432
33c477fd 433 if (unlikely(skb->len < ETH_HLEN)) {
2be7d22f
VK
434 wil_err(wil, "Short frame, len = %d\n", skb->len);
435 /* TODO: process it (i.e. BAR) */
436 kfree_skb(skb);
437 return NULL;
438 }
439
504937d4
KE
440 /* L4 IDENT is on when HW calculated checksum, check status
441 * and in case of error drop the packet
442 * higher stack layers will handle retransmission (if required)
443 */
33c477fd 444 if (likely(d->dma.status & RX_DMA_STATUS_L4I)) {
504937d4 445 /* L4 protocol identified, csum calculated */
33c477fd 446 if (likely((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0))
504937d4 447 skb->ip_summed = CHECKSUM_UNNECESSARY;
4a68ab10
VK
448 /* If HW reports bad checksum, let IP stack re-check it
449 * For example, HW don't understand Microsoft IP stack that
450 * mis-calculates TCP checksum - if it should be 0x0,
451 * it writes 0xffff in violation of RFC 1624
452 */
504937d4
KE
453 }
454
2be7d22f
VK
455 return skb;
456}
457
458/**
459 * allocate and fill up to @count buffers in rx ring
460 * buffers posted at @swtail
461 */
462static int wil_rx_refill(struct wil6210_priv *wil, int count)
463{
464 struct net_device *ndev = wil_to_ndev(wil);
465 struct vring *v = &wil->vring_rx;
466 u32 next_tail;
467 int rc = 0;
468 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
469 WIL6210_RTAP_SIZE : 0;
470
471 for (; next_tail = wil_vring_next_tail(v),
472 (next_tail != v->swhead) && (count-- > 0);
473 v->swtail = next_tail) {
474 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
33c477fd 475 if (unlikely(rc)) {
2be7d22f
VK
476 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
477 rc, v->swtail);
478 break;
479 }
480 }
481 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
482
483 return rc;
484}
485
486/*
487 * Pass Rx packet to the netif. Update statistics.
e0287c4a 488 * Called in softirq context (NAPI poll).
2be7d22f 489 */
b4490f42 490void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
2be7d22f 491{
b5998e6a 492 gro_result_t rc;
c8b78b5f 493 struct wil6210_priv *wil = ndev_to_wil(ndev);
2be7d22f 494 unsigned int len = skb->len;
c8b78b5f
VK
495 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
496 int cid = wil_rxdesc_cid(d);
497 struct wil_net_stats *stats = &wil->sta[cid].stats;
2be7d22f 498
241804cb
VK
499 skb_orphan(skb);
500
b5998e6a 501 rc = napi_gro_receive(&wil->napi_rx, skb);
2be7d22f 502
b5998e6a
VK
503 if (unlikely(rc == GRO_DROP)) {
504 ndev->stats.rx_dropped++;
505 stats->rx_dropped++;
506 wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
507 } else {
2be7d22f 508 ndev->stats.rx_packets++;
c8b78b5f 509 stats->rx_packets++;
2be7d22f 510 ndev->stats.rx_bytes += len;
c8b78b5f 511 stats->rx_bytes += len;
2be7d22f 512 }
194b482b
VK
513 {
514 static const char * const gro_res_str[] = {
515 [GRO_MERGED] = "GRO_MERGED",
516 [GRO_MERGED_FREE] = "GRO_MERGED_FREE",
517 [GRO_HELD] = "GRO_HELD",
518 [GRO_NORMAL] = "GRO_NORMAL",
519 [GRO_DROP] = "GRO_DROP",
520 };
8fe59627 521 wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
194b482b
VK
522 len, gro_res_str[rc]);
523 }
2be7d22f
VK
524}
525
526/**
527 * Proceed all completed skb's from Rx VRING
528 *
e0287c4a 529 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
2be7d22f 530 */
e0287c4a 531void wil_rx_handle(struct wil6210_priv *wil, int *quota)
2be7d22f
VK
532{
533 struct net_device *ndev = wil_to_ndev(wil);
534 struct vring *v = &wil->vring_rx;
535 struct sk_buff *skb;
536
33c477fd 537 if (unlikely(!v->va)) {
2be7d22f
VK
538 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
539 return;
540 }
7743882d 541 wil_dbg_txrx(wil, "%s()\n", __func__);
e0287c4a
VK
542 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
543 (*quota)--;
2be7d22f 544
2be7d22f
VK
545 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
546 skb->dev = ndev;
547 skb_reset_mac_header(skb);
548 skb->ip_summed = CHECKSUM_UNNECESSARY;
549 skb->pkt_type = PACKET_OTHERHOST;
550 skb->protocol = htons(ETH_P_802_2);
b4490f42 551 wil_netif_rx_any(skb, ndev);
2be7d22f
VK
552 } else {
553 skb->protocol = eth_type_trans(skb, ndev);
e4373d8e 554 wil_rx_reorder(wil, skb);
2be7d22f 555 }
2be7d22f
VK
556 }
557 wil_rx_refill(wil, v->size);
558}
559
d3762b40 560int wil_rx_init(struct wil6210_priv *wil, u16 size)
2be7d22f 561{
2be7d22f
VK
562 struct vring *vring = &wil->vring_rx;
563 int rc;
2be7d22f 564
9cf10d62
VK
565 wil_dbg_misc(wil, "%s()\n", __func__);
566
8bf6adb9
VK
567 if (vring->va) {
568 wil_err(wil, "Rx ring already allocated\n");
569 return -EINVAL;
570 }
571
d3762b40 572 vring->size = size;
2be7d22f
VK
573 rc = wil_vring_alloc(wil, vring);
574 if (rc)
575 return rc;
576
47e19af9 577 rc = wmi_rx_chain_add(wil, vring);
2be7d22f
VK
578 if (rc)
579 goto err_free;
580
2be7d22f
VK
581 rc = wil_rx_refill(wil, vring->size);
582 if (rc)
583 goto err_free;
584
585 return 0;
586 err_free:
587 wil_vring_free(wil, vring, 0);
588
589 return rc;
590}
591
592void wil_rx_fini(struct wil6210_priv *wil)
593{
594 struct vring *vring = &wil->vring_rx;
595
9cf10d62
VK
596 wil_dbg_misc(wil, "%s()\n", __func__);
597
2acb4220 598 if (vring->va)
2be7d22f 599 wil_vring_free(wil, vring, 0);
2be7d22f
VK
600}
601
602int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
603 int cid, int tid)
604{
605 int rc;
606 struct wmi_vring_cfg_cmd cmd = {
607 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
608 .vring_cfg = {
609 .tx_sw_ring = {
9a06bec9 610 .max_mpdu_size =
c44690a1 611 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
b5d98e9d 612 .ring_size = cpu_to_le16(size),
2be7d22f
VK
613 },
614 .ringid = id,
a70abea5 615 .cidxtid = mk_cidxtid(cid, tid),
2be7d22f
VK
616 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
617 .mac_ctrl = 0,
618 .to_resolution = 0,
3277213f 619 .agg_max_wsize = 0,
2be7d22f
VK
620 .schd_params = {
621 .priority = cpu_to_le16(0),
622 .timeslot_us = cpu_to_le16(0xfff),
623 },
624 },
625 };
626 struct {
627 struct wil6210_mbox_hdr_wmi wmi;
628 struct wmi_vring_cfg_done_event cmd;
629 } __packed reply;
630 struct vring *vring = &wil->vring_tx[id];
097638a0 631 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
2be7d22f 632
e0106ada
VK
633 wil_dbg_misc(wil, "%s() max_mpdu_size %d\n", __func__,
634 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
9cf10d62 635
2be7d22f
VK
636 if (vring->va) {
637 wil_err(wil, "Tx ring [%d] already allocated\n", id);
638 rc = -EINVAL;
639 goto out;
640 }
641
097638a0 642 memset(txdata, 0, sizeof(*txdata));
5933a06d 643 spin_lock_init(&txdata->lock);
2be7d22f
VK
644 vring->size = size;
645 rc = wil_vring_alloc(wil, vring);
646 if (rc)
647 goto out;
648
93ae6d49
VK
649 wil->vring2cid_tid[id][0] = cid;
650 wil->vring2cid_tid[id][1] = tid;
651
2be7d22f 652 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
2be7d22f
VK
653
654 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
655 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
656 if (rc)
657 goto out_free;
658
b8023177 659 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
2be7d22f
VK
660 wil_err(wil, "Tx config failed, status 0x%02x\n",
661 reply.cmd.status);
c331997b 662 rc = -EINVAL;
2be7d22f
VK
663 goto out_free;
664 }
665 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
666
097638a0 667 txdata->enabled = 1;
3a3def8d
VK
668 if (wil->sta[cid].data_port_open && (agg_wsize >= 0))
669 wil_addba_tx_request(wil, id, agg_wsize);
097638a0 670
2be7d22f
VK
671 return 0;
672 out_free:
673 wil_vring_free(wil, vring, 1);
674 out:
675
676 return rc;
677}
678
679void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
680{
681 struct vring *vring = &wil->vring_tx[id];
3a124ed6 682 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
2be7d22f 683
097638a0
VK
684 WARN_ON(!mutex_is_locked(&wil->mutex));
685
2be7d22f
VK
686 if (!vring->va)
687 return;
688
9cf10d62
VK
689 wil_dbg_misc(wil, "%s() id=%d\n", __func__, id);
690
5933a06d
VK
691 spin_lock_bh(&txdata->lock);
692 txdata->enabled = 0; /* no Tx can be in progress or start anew */
693 spin_unlock_bh(&txdata->lock);
097638a0 694 /* make sure NAPI won't touch this vring */
9419b6a2 695 if (test_bit(wil_status_napi_en, wil->status))
097638a0
VK
696 napi_synchronize(&wil->napi_tx);
697
2be7d22f 698 wil_vring_free(wil, vring, 1);
3a124ed6 699 memset(txdata, 0, sizeof(*txdata));
2be7d22f
VK
700}
701
702static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
703 struct sk_buff *skb)
704{
3df2cd36
VK
705 int i;
706 struct ethhdr *eth = (void *)skb->data;
707 int cid = wil_find_cid(wil, eth->h_dest);
708
709 if (cid < 0)
710 return NULL;
2be7d22f 711
e58c9f70
VK
712 if (!wil->sta[cid].data_port_open &&
713 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
714 return NULL;
715
3df2cd36
VK
716 /* TODO: fix for multiple TID */
717 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
718 if (wil->vring2cid_tid[i][0] == cid) {
719 struct vring *v = &wil->vring_tx[i];
8fe59627 720
3df2cd36
VK
721 wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n",
722 __func__, eth->h_dest, i);
723 if (v->va) {
724 return v;
725 } else {
726 wil_dbg_txrx(wil, "vring[%d] not valid\n", i);
727 return NULL;
728 }
729 }
730 }
2be7d22f
VK
731
732 return NULL;
733}
734
fb3cac57
VK
735static void wil_set_da_for_vring(struct wil6210_priv *wil,
736 struct sk_buff *skb, int vring_index)
737{
738 struct ethhdr *eth = (void *)skb->data;
739 int cid = wil->vring2cid_tid[vring_index][0];
8fe59627 740
fb3cac57
VK
741 memcpy(eth->h_dest, wil->sta[cid].addr, ETH_ALEN);
742}
743
744static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
745 struct sk_buff *skb);
54ed90a8
VK
746
747static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
748 struct sk_buff *skb)
749{
750 struct vring *v;
751 int i;
752 u8 cid;
753
754 /* In the STA mode, it is expected to have only 1 VRING
755 * for the AP we connected to.
756 * find 1-st vring and see whether it is eligible for data
757 */
758 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
759 v = &wil->vring_tx[i];
760 if (!v->va)
761 continue;
762
763 cid = wil->vring2cid_tid[i][0];
764 if (!wil->sta[cid].data_port_open &&
765 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
766 break;
767
768 wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
769
770 return v;
771 }
772
773 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
774
775 return NULL;
776}
777
fb3cac57
VK
778/*
779 * Find 1-st vring and return it; set dest address for this vring in skb
780 * duplicate skb and send it to other active vrings
781 */
782static struct vring *wil_tx_bcast(struct wil6210_priv *wil,
8fe59627 783 struct sk_buff *skb)
fb3cac57
VK
784{
785 struct vring *v, *v2;
786 struct sk_buff *skb2;
787 int i;
e58c9f70 788 u8 cid;
fb3cac57 789
e58c9f70 790 /* find 1-st vring eligible for data */
fb3cac57
VK
791 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
792 v = &wil->vring_tx[i];
e58c9f70
VK
793 if (!v->va)
794 continue;
795
796 cid = wil->vring2cid_tid[i][0];
797 if (!wil->sta[cid].data_port_open)
798 continue;
799
800 goto found;
fb3cac57
VK
801 }
802
5aed1393 803 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
fb3cac57
VK
804
805 return NULL;
806
807found:
808 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
809 wil_set_da_for_vring(wil, skb, i);
810
811 /* find other active vrings and duplicate skb for each */
812 for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
813 v2 = &wil->vring_tx[i];
814 if (!v2->va)
815 continue;
e58c9f70
VK
816 cid = wil->vring2cid_tid[i][0];
817 if (!wil->sta[cid].data_port_open)
818 continue;
819
fb3cac57
VK
820 skb2 = skb_copy(skb, GFP_ATOMIC);
821 if (skb2) {
822 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
823 wil_set_da_for_vring(wil, skb2, i);
824 wil_tx_vring(wil, v2, skb2);
825 } else {
826 wil_err(wil, "skb_copy failed\n");
827 }
828 }
829
830 return v;
831}
832
99b55bd2
KE
833static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
834 int vring_index)
2be7d22f 835{
68ada71e 836 wil_desc_addr_set(&d->dma.addr, pa);
2be7d22f
VK
837 d->dma.ip_length = 0;
838 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
839 d->dma.b11 = 0/*14 | BIT(7)*/;
840 d->dma.error = 0;
841 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
7e594444 842 d->dma.length = cpu_to_le16((u16)len);
99b55bd2 843 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
2be7d22f
VK
844 d->mac.d[0] = 0;
845 d->mac.d[1] = 0;
846 d->mac.d[2] = 0;
847 d->mac.ucode_cmd = 0;
2be7d22f
VK
848 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
849 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
850 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
851
852 return 0;
853}
854
c236658f
VK
855static inline
856void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
857{
858 d->mac.d[2] |= ((nr_frags + 1) <<
859 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
860}
861
504937d4 862static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil,
8fe59627
VK
863 struct vring_tx_desc *d,
864 struct sk_buff *skb)
504937d4
KE
865{
866 int protocol;
867
868 if (skb->ip_summed != CHECKSUM_PARTIAL)
869 return 0;
870
df2d08ee
VK
871 d->dma.b11 = ETH_HLEN; /* MAC header length */
872
504937d4
KE
873 switch (skb->protocol) {
874 case cpu_to_be16(ETH_P_IP):
875 protocol = ip_hdr(skb)->protocol;
df2d08ee 876 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
504937d4
KE
877 break;
878 case cpu_to_be16(ETH_P_IPV6):
879 protocol = ipv6_hdr(skb)->nexthdr;
880 break;
881 default:
882 return -EINVAL;
883 }
884
885 switch (protocol) {
886 case IPPROTO_TCP:
887 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
888 /* L4 header len: TCP header length */
889 d->dma.d0 |=
890 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
891 break;
892 case IPPROTO_UDP:
893 /* L4 header len: UDP header length */
894 d->dma.d0 |=
895 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
896 break;
897 default:
898 return -EINVAL;
899 }
900
901 d->dma.ip_length = skb_network_header_len(skb);
504937d4
KE
902 /* Enable TCP/UDP checksum */
903 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
904 /* Calculate pseudo-header */
905 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
906
907 return 0;
908}
909
5933a06d
VK
910static int __wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
911 struct sk_buff *skb)
2be7d22f
VK
912{
913 struct device *dev = wil_to_dev(wil);
68ada71e
VK
914 struct vring_tx_desc dd, *d = &dd;
915 volatile struct vring_tx_desc *_d;
2be7d22f
VK
916 u32 swhead = vring->swhead;
917 int avail = wil_vring_avail_tx(vring);
918 int nr_frags = skb_shinfo(skb)->nr_frags;
504937d4 919 uint f = 0;
2be7d22f 920 int vring_index = vring - wil->vring_tx;
7c0acf86 921 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
2be7d22f
VK
922 uint i = swhead;
923 dma_addr_t pa;
924
7743882d 925 wil_dbg_txrx(wil, "%s()\n", __func__);
2be7d22f 926
5933a06d
VK
927 if (unlikely(!txdata->enabled))
928 return -EINVAL;
929
33c477fd 930 if (unlikely(avail < 1 + nr_frags)) {
70801e1b 931 wil_err_ratelimited(wil,
5b29c573
VK
932 "Tx ring[%2d] full. No space for %d fragments\n",
933 vring_index, 1 + nr_frags);
2be7d22f
VK
934 return -ENOMEM;
935 }
8fe59627 936 _d = &vring->va[i].tx;
2be7d22f 937
8fe59627 938 pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
2be7d22f 939
5b29c573
VK
940 wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index,
941 skb_headlen(skb), skb->data, &pa);
7743882d 942 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
2be7d22f
VK
943 skb->data, skb_headlen(skb), false);
944
945 if (unlikely(dma_mapping_error(dev, pa)))
946 return -EINVAL;
2232abd5 947 vring->ctx[i].mapped_as = wil_mapped_as_single;
2be7d22f 948 /* 1-st segment */
99b55bd2 949 wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index);
504937d4 950 /* Process TCP/UDP checksum offloading */
33c477fd 951 if (unlikely(wil_tx_desc_offload_cksum_set(wil, d, skb))) {
5b29c573 952 wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
504937d4
KE
953 vring_index);
954 goto dma_error;
955 }
956
c236658f
VK
957 vring->ctx[i].nr_frags = nr_frags;
958 wil_tx_desc_set_nr_frags(d, nr_frags);
68ada71e 959
2be7d22f 960 /* middle segments */
504937d4 961 for (; f < nr_frags; f++) {
2be7d22f
VK
962 const struct skb_frag_struct *frag =
963 &skb_shinfo(skb)->frags[f];
964 int len = skb_frag_size(frag);
8fe59627 965
e59d16c0 966 *_d = *d;
5b29c573
VK
967 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
968 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
969 (const void *)d, sizeof(*d), false);
2be7d22f 970 i = (swhead + f + 1) % vring->size;
8fe59627 971 _d = &vring->va[i].tx;
2be7d22f 972 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
8fe59627 973 DMA_TO_DEVICE);
2be7d22f
VK
974 if (unlikely(dma_mapping_error(dev, pa)))
975 goto dma_error;
2232abd5 976 vring->ctx[i].mapped_as = wil_mapped_as_page;
99b55bd2 977 wil_tx_desc_map(d, pa, len, vring_index);
c236658f
VK
978 /* no need to check return code -
979 * if it succeeded for 1-st descriptor,
980 * it will succeed here too
981 */
982 wil_tx_desc_offload_cksum_set(wil, d, skb);
2be7d22f
VK
983 }
984 /* for the last seg only */
985 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
668b2bbd 986 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
2be7d22f 987 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
68ada71e 988 *_d = *d;
5b29c573
VK
989 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
990 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
991 (const void *)d, sizeof(*d), false);
2be7d22f 992
6cdadd4d
VK
993 /* hold reference to skb
994 * to prevent skb release before accounting
995 * in case of immediate "tx done"
996 */
997 vring->ctx[i].skb = skb_get(skb);
998
7c0acf86
VK
999 if (wil_vring_is_empty(vring)) /* performance monitoring */
1000 txdata->idle += get_cycles() - txdata->last_idle;
1001
2be7d22f
VK
1002 /* advance swhead */
1003 wil_vring_advance_head(vring, nr_frags + 1);
5b29c573
VK
1004 wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead,
1005 vring->swhead);
98658095 1006 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
2be7d22f 1007 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
2be7d22f
VK
1008
1009 return 0;
1010 dma_error:
1011 /* unmap what we have mapped */
c2a146f6
VK
1012 nr_frags = f + 1; /* frags mapped + one for skb head */
1013 for (f = 0; f < nr_frags; f++) {
c2a146f6 1014 struct wil_ctx *ctx;
7e594444 1015
2be7d22f 1016 i = (swhead + f) % vring->size;
c2a146f6 1017 ctx = &vring->ctx[i];
8fe59627 1018 _d = &vring->va[i].tx;
68ada71e
VK
1019 *d = *_d;
1020 _d->dma.status = TX_DMA_STATUS_DU;
2232abd5 1021 wil_txdesc_unmap(dev, d, ctx);
f88f113a
VK
1022
1023 if (ctx->skb)
1024 dev_kfree_skb_any(ctx->skb);
1025
1026 memset(ctx, 0, sizeof(*ctx));
2be7d22f
VK
1027 }
1028
1029 return -EINVAL;
1030}
1031
5933a06d
VK
1032static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1033 struct sk_buff *skb)
1034{
1035 int vring_index = vring - wil->vring_tx;
1036 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1037 int rc;
1038
1039 spin_lock(&txdata->lock);
1040 rc = __wil_tx_vring(wil, vring, skb);
1041 spin_unlock(&txdata->lock);
1042 return rc;
1043}
1044
2be7d22f
VK
1045netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1046{
1047 struct wil6210_priv *wil = ndev_to_wil(ndev);
3df2cd36 1048 struct ethhdr *eth = (void *)skb->data;
2be7d22f 1049 struct vring *vring;
aa27deaa 1050 static bool pr_once_fw;
2be7d22f
VK
1051 int rc;
1052
7743882d 1053 wil_dbg_txrx(wil, "%s()\n", __func__);
33c477fd 1054 if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
aa27deaa
VK
1055 if (!pr_once_fw) {
1056 wil_err(wil, "FW not ready\n");
1057 pr_once_fw = true;
1058 }
2be7d22f
VK
1059 goto drop;
1060 }
33c477fd 1061 if (unlikely(!test_bit(wil_status_fwconnected, wil->status))) {
2be7d22f
VK
1062 wil_err(wil, "FW not connected\n");
1063 goto drop;
1064 }
33c477fd 1065 if (unlikely(wil->wdev->iftype == NL80211_IFTYPE_MONITOR)) {
2be7d22f
VK
1066 wil_err(wil, "Xmit in monitor mode not supported\n");
1067 goto drop;
1068 }
aa27deaa 1069 pr_once_fw = false;
d58db4e4
VK
1070
1071 /* find vring */
54ed90a8
VK
1072 if (wil->wdev->iftype == NL80211_IFTYPE_STATION) {
1073 /* in STA mode (ESS), all to same VRING */
1074 vring = wil_find_tx_vring_sta(wil, skb);
1075 } else { /* direct communication, find matching VRING */
1076 if (is_unicast_ether_addr(eth->h_dest))
1077 vring = wil_find_tx_vring(wil, skb);
1078 else
1079 vring = wil_tx_bcast(wil, skb);
1080 }
33c477fd 1081 if (unlikely(!vring)) {
5aed1393 1082 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
d58db4e4 1083 goto drop;
2be7d22f 1084 }
d58db4e4
VK
1085 /* set up vring entry */
1086 rc = wil_tx_vring(wil, vring, skb);
1087
2232abd5 1088 /* do we still have enough room in the vring? */
33c477fd 1089 if (unlikely(wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring))) {
2232abd5 1090 netif_tx_stop_all_queues(wil_to_ndev(wil));
55f8f680
VK
1091 wil_dbg_txrx(wil, "netif_tx_stop : ring full\n");
1092 }
2232abd5 1093
2be7d22f
VK
1094 switch (rc) {
1095 case 0:
795ce734 1096 /* statistics will be updated on the tx_complete */
2be7d22f
VK
1097 dev_kfree_skb_any(skb);
1098 return NETDEV_TX_OK;
1099 case -ENOMEM:
1100 return NETDEV_TX_BUSY;
1101 default:
afda8bb5 1102 break; /* goto drop; */
2be7d22f
VK
1103 }
1104 drop:
2be7d22f
VK
1105 ndev->stats.tx_dropped++;
1106 dev_kfree_skb_any(skb);
1107
1108 return NET_XMIT_DROP;
1109}
1110
713c8a29
VK
1111static inline bool wil_need_txstat(struct sk_buff *skb)
1112{
1113 struct ethhdr *eth = (void *)skb->data;
1114
1115 return is_unicast_ether_addr(eth->h_dest) && skb->sk &&
1116 (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS);
1117}
1118
1119static inline void wil_consume_skb(struct sk_buff *skb, bool acked)
1120{
1121 if (unlikely(wil_need_txstat(skb)))
1122 skb_complete_wifi_ack(skb, acked);
1123 else
1124 acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb);
1125}
1126
2be7d22f
VK
1127/**
1128 * Clean up transmitted skb's from the Tx VRING
1129 *
e0287c4a
VK
1130 * Return number of descriptors cleared
1131 *
2be7d22f
VK
1132 * Safe to call from IRQ
1133 */
e0287c4a 1134int wil_tx_complete(struct wil6210_priv *wil, int ringid)
2be7d22f 1135{
795ce734 1136 struct net_device *ndev = wil_to_ndev(wil);
2be7d22f
VK
1137 struct device *dev = wil_to_dev(wil);
1138 struct vring *vring = &wil->vring_tx[ringid];
097638a0 1139 struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
e0287c4a 1140 int done = 0;
c8b78b5f
VK
1141 int cid = wil->vring2cid_tid[ringid][0];
1142 struct wil_net_stats *stats = &wil->sta[cid].stats;
c236658f 1143 volatile struct vring_tx_desc *_d;
2be7d22f 1144
33c477fd 1145 if (unlikely(!vring->va)) {
2be7d22f 1146 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
e0287c4a 1147 return 0;
2be7d22f
VK
1148 }
1149
33c477fd 1150 if (unlikely(!txdata->enabled)) {
097638a0
VK
1151 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
1152 return 0;
1153 }
1154
7743882d 1155 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
2be7d22f
VK
1156
1157 while (!wil_vring_is_empty(vring)) {
c236658f 1158 int new_swtail;
f88f113a 1159 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
c236658f
VK
1160 /**
1161 * For the fragmented skb, HW will set DU bit only for the
1162 * last fragment. look for it
1163 */
1164 int lf = (vring->swtail + ctx->nr_frags) % vring->size;
1165 /* TODO: check we are not past head */
4de41bef 1166
c236658f 1167 _d = &vring->va[lf].tx;
33c477fd 1168 if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
2be7d22f
VK
1169 break;
1170
c236658f
VK
1171 new_swtail = (lf + 1) % vring->size;
1172 while (vring->swtail != new_swtail) {
1173 struct vring_tx_desc dd, *d = &dd;
c236658f 1174 u16 dmalen;
76dfa4b7
VK
1175 struct sk_buff *skb;
1176
1177 ctx = &vring->ctx[vring->swtail];
1178 skb = ctx->skb;
c236658f 1179 _d = &vring->va[vring->swtail].tx;
2be7d22f 1180
c236658f 1181 *d = *_d;
f88f113a 1182
c236658f
VK
1183 dmalen = le16_to_cpu(d->dma.length);
1184 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
1185 d->dma.error);
1186 wil_dbg_txrx(wil,
5b29c573
VK
1187 "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
1188 ringid, vring->swtail, dmalen,
1189 d->dma.status, d->dma.error);
1190 wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
c236658f 1191 (const void *)d, sizeof(*d), false);
795ce734 1192
2232abd5 1193 wil_txdesc_unmap(dev, d, ctx);
c236658f
VK
1194
1195 if (skb) {
33c477fd 1196 if (likely(d->dma.error == 0)) {
c236658f
VK
1197 ndev->stats.tx_packets++;
1198 stats->tx_packets++;
1199 ndev->stats.tx_bytes += skb->len;
1200 stats->tx_bytes += skb->len;
1201 } else {
1202 ndev->stats.tx_errors++;
1203 stats->tx_errors++;
1204 }
713c8a29 1205 wil_consume_skb(skb, d->dma.error == 0);
c236658f
VK
1206 }
1207 memset(ctx, 0, sizeof(*ctx));
1208 /* There is no need to touch HW descriptor:
1209 * - ststus bit TX_DMA_STATUS_DU is set by design,
1210 * so hardware will not try to process this desc.,
1211 * - rest of descriptor will be initialized on Tx.
1212 */
1213 vring->swtail = wil_vring_next_tail(vring);
1214 done++;
2be7d22f 1215 }
2be7d22f 1216 }
67c3e1b4 1217
7c0acf86 1218 if (wil_vring_is_empty(vring)) { /* performance monitoring */
67c3e1b4 1219 wil_dbg_txrx(wil, "Ring[%2d] empty\n", ringid);
7c0acf86
VK
1220 txdata->last_idle = get_cycles();
1221 }
67c3e1b4 1222
55f8f680
VK
1223 if (wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring)) {
1224 wil_dbg_txrx(wil, "netif_tx_wake : ring not full\n");
2be7d22f 1225 netif_tx_wake_all_queues(wil_to_ndev(wil));
55f8f680 1226 }
e0287c4a
VK
1227
1228 return done;
2be7d22f 1229}