GNU Linux-libre 4.14.254-gnu1
[releases.git] / drivers / net / wireless / ath / wil6210 / txrx.c
1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
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
17 #include <linux/etherdevice.h>
18 #include <net/ieee80211_radiotap.h>
19 #include <linux/if_arp.h>
20 #include <linux/moduleparam.h>
21 #include <linux/ip.h>
22 #include <linux/ipv6.h>
23 #include <net/ipv6.h>
24 #include <linux/prefetch.h>
25
26 #include "wil6210.h"
27 #include "wmi.h"
28 #include "txrx.h"
29 #include "trace.h"
30
31 static bool rtap_include_phy_info;
32 module_param(rtap_include_phy_info, bool, 0444);
33 MODULE_PARM_DESC(rtap_include_phy_info,
34                  " Include PHY info in the radiotap header, default - no");
35
36 bool rx_align_2;
37 module_param(rx_align_2, bool, 0444);
38 MODULE_PARM_DESC(rx_align_2, " align Rx buffers on 4*n+2, default - no");
39
40 bool rx_large_buf;
41 module_param(rx_large_buf, bool, 0444);
42 MODULE_PARM_DESC(rx_large_buf, " allocate 8KB RX buffers, default - no");
43
44 static inline uint wil_rx_snaplen(void)
45 {
46         return rx_align_2 ? 6 : 0;
47 }
48
49 static inline int wil_vring_is_empty(struct vring *vring)
50 {
51         return vring->swhead == vring->swtail;
52 }
53
54 static inline u32 wil_vring_next_tail(struct vring *vring)
55 {
56         return (vring->swtail + 1) % vring->size;
57 }
58
59 static inline void wil_vring_advance_head(struct vring *vring, int n)
60 {
61         vring->swhead = (vring->swhead + n) % vring->size;
62 }
63
64 static inline int wil_vring_is_full(struct vring *vring)
65 {
66         return wil_vring_next_tail(vring) == vring->swhead;
67 }
68
69 /* Used space in Tx Vring */
70 static inline int wil_vring_used_tx(struct vring *vring)
71 {
72         u32 swhead = vring->swhead;
73         u32 swtail = vring->swtail;
74         return (vring->size + swhead - swtail) % vring->size;
75 }
76
77 /* Available space in Tx Vring */
78 static inline int wil_vring_avail_tx(struct vring *vring)
79 {
80         return vring->size - wil_vring_used_tx(vring) - 1;
81 }
82
83 /* wil_vring_wmark_low - low watermark for available descriptor space */
84 static inline int wil_vring_wmark_low(struct vring *vring)
85 {
86         return vring->size/8;
87 }
88
89 /* wil_vring_wmark_high - high watermark for available descriptor space */
90 static inline int wil_vring_wmark_high(struct vring *vring)
91 {
92         return vring->size/4;
93 }
94
95 /* returns true if num avail descriptors is lower than wmark_low */
96 static inline int wil_vring_avail_low(struct vring *vring)
97 {
98         return wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring);
99 }
100
101 /* returns true if num avail descriptors is higher than wmark_high */
102 static inline int wil_vring_avail_high(struct vring *vring)
103 {
104         return wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring);
105 }
106
107 /* returns true when all tx vrings are empty */
108 bool wil_is_tx_idle(struct wil6210_priv *wil)
109 {
110         int i;
111         unsigned long data_comp_to;
112
113         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
114                 struct vring *vring = &wil->vring_tx[i];
115                 int vring_index = vring - wil->vring_tx;
116                 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
117
118                 spin_lock(&txdata->lock);
119
120                 if (!vring->va || !txdata->enabled) {
121                         spin_unlock(&txdata->lock);
122                         continue;
123                 }
124
125                 data_comp_to = jiffies + msecs_to_jiffies(
126                                         WIL_DATA_COMPLETION_TO_MS);
127                 if (test_bit(wil_status_napi_en, wil->status)) {
128                         while (!wil_vring_is_empty(vring)) {
129                                 if (time_after(jiffies, data_comp_to)) {
130                                         wil_dbg_pm(wil,
131                                                    "TO waiting for idle tx\n");
132                                         spin_unlock(&txdata->lock);
133                                         return false;
134                                 }
135                                 wil_dbg_ratelimited(wil,
136                                                     "tx vring is not empty -> NAPI\n");
137                                 spin_unlock(&txdata->lock);
138                                 napi_synchronize(&wil->napi_tx);
139                                 msleep(20);
140                                 spin_lock(&txdata->lock);
141                                 if (!vring->va || !txdata->enabled)
142                                         break;
143                         }
144                 }
145
146                 spin_unlock(&txdata->lock);
147         }
148
149         return true;
150 }
151
152 /* wil_val_in_range - check if value in [min,max) */
153 static inline bool wil_val_in_range(int val, int min, int max)
154 {
155         return val >= min && val < max;
156 }
157
158 static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
159 {
160         struct device *dev = wil_to_dev(wil);
161         size_t sz = vring->size * sizeof(vring->va[0]);
162         uint i;
163
164         wil_dbg_misc(wil, "vring_alloc:\n");
165
166         BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
167
168         vring->swhead = 0;
169         vring->swtail = 0;
170         vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
171         if (!vring->ctx) {
172                 vring->va = NULL;
173                 return -ENOMEM;
174         }
175
176         /* vring->va should be aligned on its size rounded up to power of 2
177          * This is granted by the dma_alloc_coherent.
178          *
179          * HW has limitation that all vrings addresses must share the same
180          * upper 16 msb bits part of 48 bits address. To workaround that,
181          * if we are using 48 bit addresses switch to 32 bit allocation
182          * before allocating vring memory.
183          *
184          * There's no check for the return value of dma_set_mask_and_coherent,
185          * since we assume if we were able to set the mask during
186          * initialization in this system it will not fail if we set it again
187          */
188         if (wil->use_extended_dma_addr)
189                 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
190
191         vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
192         if (!vring->va) {
193                 kfree(vring->ctx);
194                 vring->ctx = NULL;
195                 return -ENOMEM;
196         }
197
198         if (wil->use_extended_dma_addr)
199                 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
200
201         /* initially, all descriptors are SW owned
202          * For Tx and Rx, ownership bit is at the same location, thus
203          * we can use any
204          */
205         for (i = 0; i < vring->size; i++) {
206                 volatile struct vring_tx_desc *_d = &vring->va[i].tx;
207
208                 _d->dma.status = TX_DMA_STATUS_DU;
209         }
210
211         wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
212                      vring->va, &vring->pa, vring->ctx);
213
214         return 0;
215 }
216
217 static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d,
218                              struct wil_ctx *ctx)
219 {
220         dma_addr_t pa = wil_desc_addr(&d->dma.addr);
221         u16 dmalen = le16_to_cpu(d->dma.length);
222
223         switch (ctx->mapped_as) {
224         case wil_mapped_as_single:
225                 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
226                 break;
227         case wil_mapped_as_page:
228                 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
229                 break;
230         default:
231                 break;
232         }
233 }
234
235 static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
236                            int tx)
237 {
238         struct device *dev = wil_to_dev(wil);
239         size_t sz = vring->size * sizeof(vring->va[0]);
240
241         lockdep_assert_held(&wil->mutex);
242         if (tx) {
243                 int vring_index = vring - wil->vring_tx;
244
245                 wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
246                              vring_index, vring->size, vring->va,
247                              &vring->pa, vring->ctx);
248         } else {
249                 wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
250                              vring->size, vring->va,
251                              &vring->pa, vring->ctx);
252         }
253
254         while (!wil_vring_is_empty(vring)) {
255                 dma_addr_t pa;
256                 u16 dmalen;
257                 struct wil_ctx *ctx;
258
259                 if (tx) {
260                         struct vring_tx_desc dd, *d = &dd;
261                         volatile struct vring_tx_desc *_d =
262                                         &vring->va[vring->swtail].tx;
263
264                         ctx = &vring->ctx[vring->swtail];
265                         if (!ctx) {
266                                 wil_dbg_txrx(wil,
267                                              "ctx(%d) was already completed\n",
268                                              vring->swtail);
269                                 vring->swtail = wil_vring_next_tail(vring);
270                                 continue;
271                         }
272                         *d = *_d;
273                         wil_txdesc_unmap(dev, d, ctx);
274                         if (ctx->skb)
275                                 dev_kfree_skb_any(ctx->skb);
276                         vring->swtail = wil_vring_next_tail(vring);
277                 } else { /* rx */
278                         struct vring_rx_desc dd, *d = &dd;
279                         volatile struct vring_rx_desc *_d =
280                                         &vring->va[vring->swhead].rx;
281
282                         ctx = &vring->ctx[vring->swhead];
283                         *d = *_d;
284                         pa = wil_desc_addr(&d->dma.addr);
285                         dmalen = le16_to_cpu(d->dma.length);
286                         dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
287                         kfree_skb(ctx->skb);
288                         wil_vring_advance_head(vring, 1);
289                 }
290         }
291         dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
292         kfree(vring->ctx);
293         vring->pa = 0;
294         vring->va = NULL;
295         vring->ctx = NULL;
296 }
297
298 /**
299  * Allocate one skb for Rx VRING
300  *
301  * Safe to call from IRQ
302  */
303 static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
304                                u32 i, int headroom)
305 {
306         struct device *dev = wil_to_dev(wil);
307         unsigned int sz = wil->rx_buf_len + ETH_HLEN + wil_rx_snaplen();
308         struct vring_rx_desc dd, *d = &dd;
309         volatile struct vring_rx_desc *_d = &vring->va[i].rx;
310         dma_addr_t pa;
311         struct sk_buff *skb = dev_alloc_skb(sz + headroom);
312
313         if (unlikely(!skb))
314                 return -ENOMEM;
315
316         skb_reserve(skb, headroom);
317         skb_put(skb, sz);
318
319         pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
320         if (unlikely(dma_mapping_error(dev, pa))) {
321                 kfree_skb(skb);
322                 return -ENOMEM;
323         }
324
325         d->dma.d0 = RX_DMA_D0_CMD_DMA_RT | RX_DMA_D0_CMD_DMA_IT;
326         wil_desc_addr_set(&d->dma.addr, pa);
327         /* ip_length don't care */
328         /* b11 don't care */
329         /* error don't care */
330         d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
331         d->dma.length = cpu_to_le16(sz);
332         *_d = *d;
333         vring->ctx[i].skb = skb;
334
335         return 0;
336 }
337
338 /**
339  * Adds radiotap header
340  *
341  * Any error indicated as "Bad FCS"
342  *
343  * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
344  *  - Rx descriptor: 32 bytes
345  *  - Phy info
346  */
347 static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
348                                        struct sk_buff *skb)
349 {
350         struct wireless_dev *wdev = wil->wdev;
351         struct wil6210_rtap {
352                 struct ieee80211_radiotap_header rthdr;
353                 /* fields should be in the order of bits in rthdr.it_present */
354                 /* flags */
355                 u8 flags;
356                 /* channel */
357                 __le16 chnl_freq __aligned(2);
358                 __le16 chnl_flags;
359                 /* MCS */
360                 u8 mcs_present;
361                 u8 mcs_flags;
362                 u8 mcs_index;
363         } __packed;
364         struct wil6210_rtap_vendor {
365                 struct wil6210_rtap rtap;
366                 /* vendor */
367                 u8 vendor_oui[3] __aligned(2);
368                 u8 vendor_ns;
369                 __le16 vendor_skip;
370                 u8 vendor_data[0];
371         } __packed;
372         struct vring_rx_desc *d = wil_skb_rxdesc(skb);
373         struct wil6210_rtap_vendor *rtap_vendor;
374         int rtap_len = sizeof(struct wil6210_rtap);
375         int phy_length = 0; /* phy info header size, bytes */
376         static char phy_data[128];
377         struct ieee80211_channel *ch = wdev->preset_chandef.chan;
378
379         if (rtap_include_phy_info) {
380                 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
381                 /* calculate additional length */
382                 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
383                         /**
384                          * PHY info starts from 8-byte boundary
385                          * there are 8-byte lines, last line may be partially
386                          * written (HW bug), thus FW configures for last line
387                          * to be excessive. Driver skips this last line.
388                          */
389                         int len = min_t(int, 8 + sizeof(phy_data),
390                                         wil_rxdesc_phy_length(d));
391
392                         if (len > 8) {
393                                 void *p = skb_tail_pointer(skb);
394                                 void *pa = PTR_ALIGN(p, 8);
395
396                                 if (skb_tailroom(skb) >= len + (pa - p)) {
397                                         phy_length = len - 8;
398                                         memcpy(phy_data, pa, phy_length);
399                                 }
400                         }
401                 }
402                 rtap_len += phy_length;
403         }
404
405         if (skb_headroom(skb) < rtap_len &&
406             pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
407                 wil_err(wil, "Unable to expand headroom to %d\n", rtap_len);
408                 return;
409         }
410
411         rtap_vendor = skb_push(skb, rtap_len);
412         memset(rtap_vendor, 0, rtap_len);
413
414         rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
415         rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
416         rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
417                         (1 << IEEE80211_RADIOTAP_FLAGS) |
418                         (1 << IEEE80211_RADIOTAP_CHANNEL) |
419                         (1 << IEEE80211_RADIOTAP_MCS));
420         if (d->dma.status & RX_DMA_STATUS_ERROR)
421                 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
422
423         rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
424         rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
425
426         rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
427         rtap_vendor->rtap.mcs_flags = 0;
428         rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
429
430         if (rtap_include_phy_info) {
431                 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
432                                 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
433                 /* OUI for Wilocity 04:ce:14 */
434                 rtap_vendor->vendor_oui[0] = 0x04;
435                 rtap_vendor->vendor_oui[1] = 0xce;
436                 rtap_vendor->vendor_oui[2] = 0x14;
437                 rtap_vendor->vendor_ns = 1;
438                 /* Rx descriptor + PHY data  */
439                 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
440                                                        phy_length);
441                 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
442                 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
443                        phy_length);
444         }
445 }
446
447 /* similar to ieee80211_ version, but FC contain only 1-st byte */
448 static inline int wil_is_back_req(u8 fc)
449 {
450         return (fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) ==
451                (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK_REQ);
452 }
453
454 bool wil_is_rx_idle(struct wil6210_priv *wil)
455 {
456         struct vring_rx_desc *_d;
457         struct vring *vring = &wil->vring_rx;
458
459         _d = (struct vring_rx_desc *)&vring->va[vring->swhead].rx;
460         if (_d->dma.status & RX_DMA_STATUS_DU)
461                 return false;
462
463         return true;
464 }
465
466 /**
467  * reap 1 frame from @swhead
468  *
469  * Rx descriptor copied to skb->cb
470  *
471  * Safe to call from IRQ
472  */
473 static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
474                                          struct vring *vring)
475 {
476         struct device *dev = wil_to_dev(wil);
477         struct net_device *ndev = wil_to_ndev(wil);
478         volatile struct vring_rx_desc *_d;
479         struct vring_rx_desc *d;
480         struct sk_buff *skb;
481         dma_addr_t pa;
482         unsigned int snaplen = wil_rx_snaplen();
483         unsigned int sz = wil->rx_buf_len + ETH_HLEN + snaplen;
484         u16 dmalen;
485         u8 ftype;
486         int cid;
487         int i;
488         struct wil_net_stats *stats;
489
490         BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
491
492 again:
493         if (unlikely(wil_vring_is_empty(vring)))
494                 return NULL;
495
496         i = (int)vring->swhead;
497         _d = &vring->va[i].rx;
498         if (unlikely(!(_d->dma.status & RX_DMA_STATUS_DU))) {
499                 /* it is not error, we just reached end of Rx done area */
500                 return NULL;
501         }
502
503         skb = vring->ctx[i].skb;
504         vring->ctx[i].skb = NULL;
505         wil_vring_advance_head(vring, 1);
506         if (!skb) {
507                 wil_err(wil, "No Rx skb at [%d]\n", i);
508                 goto again;
509         }
510         d = wil_skb_rxdesc(skb);
511         *d = *_d;
512         pa = wil_desc_addr(&d->dma.addr);
513
514         dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
515         dmalen = le16_to_cpu(d->dma.length);
516
517         trace_wil6210_rx(i, d);
518         wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", i, dmalen);
519         wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
520                           (const void *)d, sizeof(*d), false);
521
522         cid = wil_rxdesc_cid(d);
523         stats = &wil->sta[cid].stats;
524
525         if (unlikely(dmalen > sz)) {
526                 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
527                 stats->rx_large_frame++;
528                 kfree_skb(skb);
529                 goto again;
530         }
531         skb_trim(skb, dmalen);
532
533         prefetch(skb->data);
534
535         wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
536                           skb->data, skb_headlen(skb), false);
537
538         stats->last_mcs_rx = wil_rxdesc_mcs(d);
539         if (stats->last_mcs_rx < ARRAY_SIZE(stats->rx_per_mcs))
540                 stats->rx_per_mcs[stats->last_mcs_rx]++;
541
542         /* use radiotap header only if required */
543         if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
544                 wil_rx_add_radiotap_header(wil, skb);
545
546         /* no extra checks if in sniffer mode */
547         if (ndev->type != ARPHRD_ETHER)
548                 return skb;
549         /* Non-data frames may be delivered through Rx DMA channel (ex: BAR)
550          * Driver should recognize it by frame type, that is found
551          * in Rx descriptor. If type is not data, it is 802.11 frame as is
552          */
553         ftype = wil_rxdesc_ftype(d) << 2;
554         if (unlikely(ftype != IEEE80211_FTYPE_DATA)) {
555                 u8 fc1 = wil_rxdesc_fc1(d);
556                 int mid = wil_rxdesc_mid(d);
557                 int tid = wil_rxdesc_tid(d);
558                 u16 seq = wil_rxdesc_seq(d);
559
560                 wil_dbg_txrx(wil,
561                              "Non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
562                              fc1, mid, cid, tid, seq);
563                 stats->rx_non_data_frame++;
564                 if (wil_is_back_req(fc1)) {
565                         wil_dbg_txrx(wil,
566                                      "BAR: MID %d CID %d TID %d Seq 0x%03x\n",
567                                      mid, cid, tid, seq);
568                         wil_rx_bar(wil, cid, tid, seq);
569                 } else {
570                         /* print again all info. One can enable only this
571                          * without overhead for printing every Rx frame
572                          */
573                         wil_dbg_txrx(wil,
574                                      "Unhandled non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
575                                      fc1, mid, cid, tid, seq);
576                         wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
577                                           (const void *)d, sizeof(*d), false);
578                         wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
579                                           skb->data, skb_headlen(skb), false);
580                 }
581                 kfree_skb(skb);
582                 goto again;
583         }
584
585         if (unlikely(skb->len < ETH_HLEN + snaplen)) {
586                 wil_err(wil, "Short frame, len = %d\n", skb->len);
587                 stats->rx_short_frame++;
588                 kfree_skb(skb);
589                 goto again;
590         }
591
592         /* L4 IDENT is on when HW calculated checksum, check status
593          * and in case of error drop the packet
594          * higher stack layers will handle retransmission (if required)
595          */
596         if (likely(d->dma.status & RX_DMA_STATUS_L4I)) {
597                 /* L4 protocol identified, csum calculated */
598                 if (likely((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0))
599                         skb->ip_summed = CHECKSUM_UNNECESSARY;
600                 /* If HW reports bad checksum, let IP stack re-check it
601                  * For example, HW don't understand Microsoft IP stack that
602                  * mis-calculates TCP checksum - if it should be 0x0,
603                  * it writes 0xffff in violation of RFC 1624
604                  */
605         }
606
607         if (snaplen) {
608                 /* Packet layout
609                  * +-------+-------+---------+------------+------+
610                  * | SA(6) | DA(6) | SNAP(6) | ETHTYPE(2) | DATA |
611                  * +-------+-------+---------+------------+------+
612                  * Need to remove SNAP, shifting SA and DA forward
613                  */
614                 memmove(skb->data + snaplen, skb->data, 2 * ETH_ALEN);
615                 skb_pull(skb, snaplen);
616         }
617
618         return skb;
619 }
620
621 /**
622  * allocate and fill up to @count buffers in rx ring
623  * buffers posted at @swtail
624  */
625 static int wil_rx_refill(struct wil6210_priv *wil, int count)
626 {
627         struct net_device *ndev = wil_to_ndev(wil);
628         struct vring *v = &wil->vring_rx;
629         u32 next_tail;
630         int rc = 0;
631         int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
632                         WIL6210_RTAP_SIZE : 0;
633
634         for (; next_tail = wil_vring_next_tail(v),
635                         (next_tail != v->swhead) && (count-- > 0);
636                         v->swtail = next_tail) {
637                 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
638                 if (unlikely(rc)) {
639                         wil_err_ratelimited(wil, "Error %d in rx refill[%d]\n",
640                                             rc, v->swtail);
641                         break;
642                 }
643         }
644
645         /* make sure all writes to descriptors (shared memory) are done before
646          * committing them to HW
647          */
648         wmb();
649
650         wil_w(wil, v->hwtail, v->swtail);
651
652         return rc;
653 }
654
655 /**
656  * reverse_memcmp - Compare two areas of memory, in reverse order
657  * @cs: One area of memory
658  * @ct: Another area of memory
659  * @count: The size of the area.
660  *
661  * Cut'n'paste from original memcmp (see lib/string.c)
662  * with minimal modifications
663  */
664 static int reverse_memcmp(const void *cs, const void *ct, size_t count)
665 {
666         const unsigned char *su1, *su2;
667         int res = 0;
668
669         for (su1 = cs + count - 1, su2 = ct + count - 1; count > 0;
670              --su1, --su2, count--) {
671                 res = *su1 - *su2;
672                 if (res)
673                         break;
674         }
675         return res;
676 }
677
678 static int wil_rx_crypto_check(struct wil6210_priv *wil, struct sk_buff *skb)
679 {
680         struct vring_rx_desc *d = wil_skb_rxdesc(skb);
681         int cid = wil_rxdesc_cid(d);
682         int tid = wil_rxdesc_tid(d);
683         int key_id = wil_rxdesc_key_id(d);
684         int mc = wil_rxdesc_mcast(d);
685         struct wil_sta_info *s = &wil->sta[cid];
686         struct wil_tid_crypto_rx *c = mc ? &s->group_crypto_rx :
687                                       &s->tid_crypto_rx[tid];
688         struct wil_tid_crypto_rx_single *cc = &c->key_id[key_id];
689         const u8 *pn = (u8 *)&d->mac.pn_15_0;
690
691         if (!cc->key_set) {
692                 wil_err_ratelimited(wil,
693                                     "Key missing. CID %d TID %d MCast %d KEY_ID %d\n",
694                                     cid, tid, mc, key_id);
695                 return -EINVAL;
696         }
697
698         if (reverse_memcmp(pn, cc->pn, IEEE80211_GCMP_PN_LEN) <= 0) {
699                 wil_err_ratelimited(wil,
700                                     "Replay attack. CID %d TID %d MCast %d KEY_ID %d PN %6phN last %6phN\n",
701                                     cid, tid, mc, key_id, pn, cc->pn);
702                 return -EINVAL;
703         }
704         memcpy(cc->pn, pn, IEEE80211_GCMP_PN_LEN);
705
706         return 0;
707 }
708
709 /*
710  * Pass Rx packet to the netif. Update statistics.
711  * Called in softirq context (NAPI poll).
712  */
713 void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
714 {
715         gro_result_t rc = GRO_NORMAL;
716         struct wil6210_priv *wil = ndev_to_wil(ndev);
717         struct wireless_dev *wdev = wil_to_wdev(wil);
718         unsigned int len = skb->len;
719         struct vring_rx_desc *d = wil_skb_rxdesc(skb);
720         int cid = wil_rxdesc_cid(d); /* always 0..7, no need to check */
721         int security = wil_rxdesc_security(d);
722         struct ethhdr *eth = (void *)skb->data;
723         /* here looking for DA, not A1, thus Rxdesc's 'mcast' indication
724          * is not suitable, need to look at data
725          */
726         int mcast = is_multicast_ether_addr(eth->h_dest);
727         struct wil_net_stats *stats = &wil->sta[cid].stats;
728         struct sk_buff *xmit_skb = NULL;
729         static const char * const gro_res_str[] = {
730                 [GRO_MERGED]            = "GRO_MERGED",
731                 [GRO_MERGED_FREE]       = "GRO_MERGED_FREE",
732                 [GRO_HELD]              = "GRO_HELD",
733                 [GRO_NORMAL]            = "GRO_NORMAL",
734                 [GRO_DROP]              = "GRO_DROP",
735                 [GRO_CONSUMED]          = "GRO_CONSUMED",
736         };
737
738         if (ndev->features & NETIF_F_RXHASH)
739                 /* fake L4 to ensure it won't be re-calculated later
740                  * set hash to any non-zero value to activate rps
741                  * mechanism, core will be chosen according
742                  * to user-level rps configuration.
743                  */
744                 skb_set_hash(skb, 1, PKT_HASH_TYPE_L4);
745
746         skb_orphan(skb);
747
748         if (security && (wil_rx_crypto_check(wil, skb) != 0)) {
749                 rc = GRO_DROP;
750                 dev_kfree_skb(skb);
751                 stats->rx_replay++;
752                 goto stats;
753         }
754
755         if (wdev->iftype == NL80211_IFTYPE_AP && !wil->ap_isolate) {
756                 if (mcast) {
757                         /* send multicast frames both to higher layers in
758                          * local net stack and back to the wireless medium
759                          */
760                         xmit_skb = skb_copy(skb, GFP_ATOMIC);
761                 } else {
762                         int xmit_cid = wil_find_cid(wil, eth->h_dest);
763
764                         if (xmit_cid >= 0) {
765                                 /* The destination station is associated to
766                                  * this AP (in this VLAN), so send the frame
767                                  * directly to it and do not pass it to local
768                                  * net stack.
769                                  */
770                                 xmit_skb = skb;
771                                 skb = NULL;
772                         }
773                 }
774         }
775         if (xmit_skb) {
776                 /* Send to wireless media and increase priority by 256 to
777                  * keep the received priority instead of reclassifying
778                  * the frame (see cfg80211_classify8021d).
779                  */
780                 xmit_skb->dev = ndev;
781                 xmit_skb->priority += 256;
782                 xmit_skb->protocol = htons(ETH_P_802_3);
783                 skb_reset_network_header(xmit_skb);
784                 skb_reset_mac_header(xmit_skb);
785                 wil_dbg_txrx(wil, "Rx -> Tx %d bytes\n", len);
786                 dev_queue_xmit(xmit_skb);
787         }
788
789         if (skb) { /* deliver to local stack */
790
791                 skb->protocol = eth_type_trans(skb, ndev);
792                 rc = napi_gro_receive(&wil->napi_rx, skb);
793                 wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
794                              len, gro_res_str[rc]);
795         }
796 stats:
797         /* statistics. rc set to GRO_NORMAL for AP bridging */
798         if (unlikely(rc == GRO_DROP)) {
799                 ndev->stats.rx_dropped++;
800                 stats->rx_dropped++;
801                 wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
802         } else {
803                 ndev->stats.rx_packets++;
804                 stats->rx_packets++;
805                 ndev->stats.rx_bytes += len;
806                 stats->rx_bytes += len;
807                 if (mcast)
808                         ndev->stats.multicast++;
809         }
810 }
811
812 /**
813  * Proceed all completed skb's from Rx VRING
814  *
815  * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
816  */
817 void wil_rx_handle(struct wil6210_priv *wil, int *quota)
818 {
819         struct net_device *ndev = wil_to_ndev(wil);
820         struct vring *v = &wil->vring_rx;
821         struct sk_buff *skb;
822
823         if (unlikely(!v->va)) {
824                 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
825                 return;
826         }
827         wil_dbg_txrx(wil, "rx_handle\n");
828         while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
829                 (*quota)--;
830
831                 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
832                         skb->dev = ndev;
833                         skb_reset_mac_header(skb);
834                         skb->ip_summed = CHECKSUM_UNNECESSARY;
835                         skb->pkt_type = PACKET_OTHERHOST;
836                         skb->protocol = htons(ETH_P_802_2);
837                         wil_netif_rx_any(skb, ndev);
838                 } else {
839                         wil_rx_reorder(wil, skb);
840                 }
841         }
842         wil_rx_refill(wil, v->size);
843 }
844
845 static void wil_rx_buf_len_init(struct wil6210_priv *wil)
846 {
847         wil->rx_buf_len = rx_large_buf ?
848                 WIL_MAX_ETH_MTU : TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
849         if (mtu_max > wil->rx_buf_len) {
850                 /* do not allow RX buffers to be smaller than mtu_max, for
851                  * backward compatibility (mtu_max parameter was also used
852                  * to support receiving large packets)
853                  */
854                 wil_info(wil, "Override RX buffer to mtu_max(%d)\n", mtu_max);
855                 wil->rx_buf_len = mtu_max;
856         }
857 }
858
859 int wil_rx_init(struct wil6210_priv *wil, u16 size)
860 {
861         struct vring *vring = &wil->vring_rx;
862         int rc;
863
864         wil_dbg_misc(wil, "rx_init\n");
865
866         if (vring->va) {
867                 wil_err(wil, "Rx ring already allocated\n");
868                 return -EINVAL;
869         }
870
871         wil_rx_buf_len_init(wil);
872
873         vring->size = size;
874         rc = wil_vring_alloc(wil, vring);
875         if (rc)
876                 return rc;
877
878         rc = wmi_rx_chain_add(wil, vring);
879         if (rc)
880                 goto err_free;
881
882         rc = wil_rx_refill(wil, vring->size);
883         if (rc)
884                 goto err_free;
885
886         return 0;
887  err_free:
888         wil_vring_free(wil, vring, 0);
889
890         return rc;
891 }
892
893 void wil_rx_fini(struct wil6210_priv *wil)
894 {
895         struct vring *vring = &wil->vring_rx;
896
897         wil_dbg_misc(wil, "rx_fini\n");
898
899         if (vring->va)
900                 wil_vring_free(wil, vring, 0);
901 }
902
903 static inline void wil_tx_data_init(struct vring_tx_data *txdata)
904 {
905         spin_lock_bh(&txdata->lock);
906         txdata->dot1x_open = 0;
907         txdata->enabled = 0;
908         txdata->idle = 0;
909         txdata->last_idle = 0;
910         txdata->begin = 0;
911         txdata->agg_wsize = 0;
912         txdata->agg_timeout = 0;
913         txdata->agg_amsdu = 0;
914         txdata->addba_in_progress = false;
915         spin_unlock_bh(&txdata->lock);
916 }
917
918 int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
919                       int cid, int tid)
920 {
921         int rc;
922         struct wmi_vring_cfg_cmd cmd = {
923                 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
924                 .vring_cfg = {
925                         .tx_sw_ring = {
926                                 .max_mpdu_size =
927                                         cpu_to_le16(wil_mtu2macbuf(mtu_max)),
928                                 .ring_size = cpu_to_le16(size),
929                         },
930                         .ringid = id,
931                         .cidxtid = mk_cidxtid(cid, tid),
932                         .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
933                         .mac_ctrl = 0,
934                         .to_resolution = 0,
935                         .agg_max_wsize = 0,
936                         .schd_params = {
937                                 .priority = cpu_to_le16(0),
938                                 .timeslot_us = cpu_to_le16(0xfff),
939                         },
940                 },
941         };
942         struct {
943                 struct wmi_cmd_hdr wmi;
944                 struct wmi_vring_cfg_done_event cmd;
945         } __packed reply;
946         struct vring *vring = &wil->vring_tx[id];
947         struct vring_tx_data *txdata = &wil->vring_tx_data[id];
948
949         wil_dbg_misc(wil, "vring_init_tx: max_mpdu_size %d\n",
950                      cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
951         lockdep_assert_held(&wil->mutex);
952
953         if (vring->va) {
954                 wil_err(wil, "Tx ring [%d] already allocated\n", id);
955                 rc = -EINVAL;
956                 goto out;
957         }
958
959         wil_tx_data_init(txdata);
960         vring->size = size;
961         rc = wil_vring_alloc(wil, vring);
962         if (rc)
963                 goto out;
964
965         wil->vring2cid_tid[id][0] = cid;
966         wil->vring2cid_tid[id][1] = tid;
967
968         cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
969
970         if (!wil->privacy)
971                 txdata->dot1x_open = true;
972         rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
973                       WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
974         if (rc)
975                 goto out_free;
976
977         if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
978                 wil_err(wil, "Tx config failed, status 0x%02x\n",
979                         reply.cmd.status);
980                 rc = -EINVAL;
981                 goto out_free;
982         }
983
984         spin_lock_bh(&txdata->lock);
985         vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
986         txdata->enabled = 1;
987         spin_unlock_bh(&txdata->lock);
988
989         if (txdata->dot1x_open && (agg_wsize >= 0))
990                 wil_addba_tx_request(wil, id, agg_wsize);
991
992         return 0;
993  out_free:
994         spin_lock_bh(&txdata->lock);
995         txdata->dot1x_open = false;
996         txdata->enabled = 0;
997         spin_unlock_bh(&txdata->lock);
998         wil_vring_free(wil, vring, 1);
999         wil->vring2cid_tid[id][0] = WIL6210_MAX_CID;
1000         wil->vring2cid_tid[id][1] = 0;
1001
1002  out:
1003
1004         return rc;
1005 }
1006
1007 int wil_vring_init_bcast(struct wil6210_priv *wil, int id, int size)
1008 {
1009         int rc;
1010         struct wmi_bcast_vring_cfg_cmd cmd = {
1011                 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
1012                 .vring_cfg = {
1013                         .tx_sw_ring = {
1014                                 .max_mpdu_size =
1015                                         cpu_to_le16(wil_mtu2macbuf(mtu_max)),
1016                                 .ring_size = cpu_to_le16(size),
1017                         },
1018                         .ringid = id,
1019                         .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
1020                 },
1021         };
1022         struct {
1023                 struct wmi_cmd_hdr wmi;
1024                 struct wmi_vring_cfg_done_event cmd;
1025         } __packed reply;
1026         struct vring *vring = &wil->vring_tx[id];
1027         struct vring_tx_data *txdata = &wil->vring_tx_data[id];
1028
1029         wil_dbg_misc(wil, "vring_init_bcast: max_mpdu_size %d\n",
1030                      cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
1031         lockdep_assert_held(&wil->mutex);
1032
1033         if (vring->va) {
1034                 wil_err(wil, "Tx ring [%d] already allocated\n", id);
1035                 rc = -EINVAL;
1036                 goto out;
1037         }
1038
1039         wil_tx_data_init(txdata);
1040         vring->size = size;
1041         rc = wil_vring_alloc(wil, vring);
1042         if (rc)
1043                 goto out;
1044
1045         wil->vring2cid_tid[id][0] = WIL6210_MAX_CID; /* CID */
1046         wil->vring2cid_tid[id][1] = 0; /* TID */
1047
1048         cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
1049
1050         if (!wil->privacy)
1051                 txdata->dot1x_open = true;
1052         rc = wmi_call(wil, WMI_BCAST_VRING_CFG_CMDID, &cmd, sizeof(cmd),
1053                       WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
1054         if (rc)
1055                 goto out_free;
1056
1057         if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
1058                 wil_err(wil, "Tx config failed, status 0x%02x\n",
1059                         reply.cmd.status);
1060                 rc = -EINVAL;
1061                 goto out_free;
1062         }
1063
1064         spin_lock_bh(&txdata->lock);
1065         vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
1066         txdata->enabled = 1;
1067         spin_unlock_bh(&txdata->lock);
1068
1069         return 0;
1070  out_free:
1071         spin_lock_bh(&txdata->lock);
1072         txdata->enabled = 0;
1073         txdata->dot1x_open = false;
1074         spin_unlock_bh(&txdata->lock);
1075         wil_vring_free(wil, vring, 1);
1076  out:
1077
1078         return rc;
1079 }
1080
1081 void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
1082 {
1083         struct vring *vring = &wil->vring_tx[id];
1084         struct vring_tx_data *txdata = &wil->vring_tx_data[id];
1085
1086         lockdep_assert_held(&wil->mutex);
1087
1088         if (!vring->va)
1089                 return;
1090
1091         wil_dbg_misc(wil, "vring_fini_tx: id=%d\n", id);
1092
1093         spin_lock_bh(&txdata->lock);
1094         txdata->dot1x_open = false;
1095         txdata->enabled = 0; /* no Tx can be in progress or start anew */
1096         spin_unlock_bh(&txdata->lock);
1097         /* napi_synchronize waits for completion of the current NAPI but will
1098          * not prevent the next NAPI run.
1099          * Add a memory barrier to guarantee that txdata->enabled is zeroed
1100          * before napi_synchronize so that the next scheduled NAPI will not
1101          * handle this vring
1102          */
1103         wmb();
1104         /* make sure NAPI won't touch this vring */
1105         if (test_bit(wil_status_napi_en, wil->status))
1106                 napi_synchronize(&wil->napi_tx);
1107
1108         wil_vring_free(wil, vring, 1);
1109 }
1110
1111 static struct vring *wil_find_tx_ucast(struct wil6210_priv *wil,
1112                                        struct sk_buff *skb)
1113 {
1114         int i;
1115         struct ethhdr *eth = (void *)skb->data;
1116         int cid = wil_find_cid(wil, eth->h_dest);
1117
1118         if (cid < 0)
1119                 return NULL;
1120
1121         /* TODO: fix for multiple TID */
1122         for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
1123                 if (!wil->vring_tx_data[i].dot1x_open &&
1124                     (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1125                         continue;
1126                 if (wil->vring2cid_tid[i][0] == cid) {
1127                         struct vring *v = &wil->vring_tx[i];
1128                         struct vring_tx_data *txdata = &wil->vring_tx_data[i];
1129
1130                         wil_dbg_txrx(wil, "find_tx_ucast: (%pM) -> [%d]\n",
1131                                      eth->h_dest, i);
1132                         if (v->va && txdata->enabled) {
1133                                 return v;
1134                         } else {
1135                                 wil_dbg_txrx(wil,
1136                                              "find_tx_ucast: vring[%d] not valid\n",
1137                                              i);
1138                                 return NULL;
1139                         }
1140                 }
1141         }
1142
1143         return NULL;
1144 }
1145
1146 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1147                         struct sk_buff *skb);
1148
1149 static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil,
1150                                            struct sk_buff *skb)
1151 {
1152         struct vring *v;
1153         int i;
1154         u8 cid;
1155         struct vring_tx_data *txdata;
1156
1157         /* In the STA mode, it is expected to have only 1 VRING
1158          * for the AP we connected to.
1159          * find 1-st vring eligible for this skb and use it.
1160          */
1161         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1162                 v = &wil->vring_tx[i];
1163                 txdata = &wil->vring_tx_data[i];
1164                 if (!v->va || !txdata->enabled)
1165                         continue;
1166
1167                 cid = wil->vring2cid_tid[i][0];
1168                 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1169                         continue;
1170
1171                 if (!wil->vring_tx_data[i].dot1x_open &&
1172                     (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1173                         continue;
1174
1175                 wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
1176
1177                 return v;
1178         }
1179
1180         wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1181
1182         return NULL;
1183 }
1184
1185 /* Use one of 2 strategies:
1186  *
1187  * 1. New (real broadcast):
1188  *    use dedicated broadcast vring
1189  * 2. Old (pseudo-DMS):
1190  *    Find 1-st vring and return it;
1191  *    duplicate skb and send it to other active vrings;
1192  *    in all cases override dest address to unicast peer's address
1193  * Use old strategy when new is not supported yet:
1194  *  - for PBSS
1195  */
1196 static struct vring *wil_find_tx_bcast_1(struct wil6210_priv *wil,
1197                                          struct sk_buff *skb)
1198 {
1199         struct vring *v;
1200         struct vring_tx_data *txdata;
1201         int i = wil->bcast_vring;
1202
1203         if (i < 0)
1204                 return NULL;
1205         v = &wil->vring_tx[i];
1206         txdata = &wil->vring_tx_data[i];
1207         if (!v->va || !txdata->enabled)
1208                 return NULL;
1209         if (!wil->vring_tx_data[i].dot1x_open &&
1210             (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1211                 return NULL;
1212
1213         return v;
1214 }
1215
1216 static void wil_set_da_for_vring(struct wil6210_priv *wil,
1217                                  struct sk_buff *skb, int vring_index)
1218 {
1219         struct ethhdr *eth = (void *)skb->data;
1220         int cid = wil->vring2cid_tid[vring_index][0];
1221
1222         ether_addr_copy(eth->h_dest, wil->sta[cid].addr);
1223 }
1224
1225 static struct vring *wil_find_tx_bcast_2(struct wil6210_priv *wil,
1226                                          struct sk_buff *skb)
1227 {
1228         struct vring *v, *v2;
1229         struct sk_buff *skb2;
1230         int i;
1231         u8 cid;
1232         struct ethhdr *eth = (void *)skb->data;
1233         char *src = eth->h_source;
1234         struct vring_tx_data *txdata;
1235
1236         /* find 1-st vring eligible for data */
1237         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1238                 v = &wil->vring_tx[i];
1239                 txdata = &wil->vring_tx_data[i];
1240                 if (!v->va || !txdata->enabled)
1241                         continue;
1242
1243                 cid = wil->vring2cid_tid[i][0];
1244                 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1245                         continue;
1246                 if (!wil->vring_tx_data[i].dot1x_open &&
1247                     (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1248                         continue;
1249
1250                 /* don't Tx back to source when re-routing Rx->Tx at the AP */
1251                 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1252                         continue;
1253
1254                 goto found;
1255         }
1256
1257         wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1258
1259         return NULL;
1260
1261 found:
1262         wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
1263         wil_set_da_for_vring(wil, skb, i);
1264
1265         /* find other active vrings and duplicate skb for each */
1266         for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
1267                 v2 = &wil->vring_tx[i];
1268                 if (!v2->va)
1269                         continue;
1270                 cid = wil->vring2cid_tid[i][0];
1271                 if (cid >= WIL6210_MAX_CID) /* skip BCAST */
1272                         continue;
1273                 if (!wil->vring_tx_data[i].dot1x_open &&
1274                     (skb->protocol != cpu_to_be16(ETH_P_PAE)))
1275                         continue;
1276
1277                 if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1278                         continue;
1279
1280                 skb2 = skb_copy(skb, GFP_ATOMIC);
1281                 if (skb2) {
1282                         wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
1283                         wil_set_da_for_vring(wil, skb2, i);
1284                         wil_tx_vring(wil, v2, skb2);
1285                 } else {
1286                         wil_err(wil, "skb_copy failed\n");
1287                 }
1288         }
1289
1290         return v;
1291 }
1292
1293 static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
1294                            int vring_index)
1295 {
1296         wil_desc_addr_set(&d->dma.addr, pa);
1297         d->dma.ip_length = 0;
1298         /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
1299         d->dma.b11 = 0/*14 | BIT(7)*/;
1300         d->dma.error = 0;
1301         d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
1302         d->dma.length = cpu_to_le16((u16)len);
1303         d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
1304         d->mac.d[0] = 0;
1305         d->mac.d[1] = 0;
1306         d->mac.d[2] = 0;
1307         d->mac.ucode_cmd = 0;
1308         /* translation type:  0 - bypass; 1 - 802.3; 2 - native wifi */
1309         d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
1310                       (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
1311
1312         return 0;
1313 }
1314
1315 static inline
1316 void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
1317 {
1318         d->mac.d[2] |= (nr_frags << MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
1319 }
1320
1321 /**
1322  * Sets the descriptor @d up for csum and/or TSO offloading. The corresponding
1323  * @skb is used to obtain the protocol and headers length.
1324  * @tso_desc_type is a descriptor type for TSO: 0 - a header, 1 - first data,
1325  * 2 - middle, 3 - last descriptor.
1326  */
1327
1328 static void wil_tx_desc_offload_setup_tso(struct vring_tx_desc *d,
1329                                           struct sk_buff *skb,
1330                                           int tso_desc_type, bool is_ipv4,
1331                                           int tcp_hdr_len, int skb_net_hdr_len)
1332 {
1333         d->dma.b11 = ETH_HLEN; /* MAC header length */
1334         d->dma.b11 |= is_ipv4 << DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS;
1335
1336         d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1337         /* L4 header len: TCP header length */
1338         d->dma.d0 |= (tcp_hdr_len & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1339
1340         /* Setup TSO: bit and desc type */
1341         d->dma.d0 |= (BIT(DMA_CFG_DESC_TX_0_TCP_SEG_EN_POS)) |
1342                 (tso_desc_type << DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS);
1343         d->dma.d0 |= (is_ipv4 << DMA_CFG_DESC_TX_0_IPV4_CHECKSUM_EN_POS);
1344
1345         d->dma.ip_length = skb_net_hdr_len;
1346         /* Enable TCP/UDP checksum */
1347         d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1348         /* Calculate pseudo-header */
1349         d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1350 }
1351
1352 /**
1353  * Sets the descriptor @d up for csum. The corresponding
1354  * @skb is used to obtain the protocol and headers length.
1355  * Returns the protocol: 0 - not TCP, 1 - TCPv4, 2 - TCPv6.
1356  * Note, if d==NULL, the function only returns the protocol result.
1357  *
1358  * It is very similar to previous wil_tx_desc_offload_setup_tso. This
1359  * is "if unrolling" to optimize the critical path.
1360  */
1361
1362 static int wil_tx_desc_offload_setup(struct vring_tx_desc *d,
1363                                      struct sk_buff *skb){
1364         int protocol;
1365
1366         if (skb->ip_summed != CHECKSUM_PARTIAL)
1367                 return 0;
1368
1369         d->dma.b11 = ETH_HLEN; /* MAC header length */
1370
1371         switch (skb->protocol) {
1372         case cpu_to_be16(ETH_P_IP):
1373                 protocol = ip_hdr(skb)->protocol;
1374                 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
1375                 break;
1376         case cpu_to_be16(ETH_P_IPV6):
1377                 protocol = ipv6_hdr(skb)->nexthdr;
1378                 break;
1379         default:
1380                 return -EINVAL;
1381         }
1382
1383         switch (protocol) {
1384         case IPPROTO_TCP:
1385                 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1386                 /* L4 header len: TCP header length */
1387                 d->dma.d0 |=
1388                 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1389                 break;
1390         case IPPROTO_UDP:
1391                 /* L4 header len: UDP header length */
1392                 d->dma.d0 |=
1393                 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1394                 break;
1395         default:
1396                 return -EINVAL;
1397         }
1398
1399         d->dma.ip_length = skb_network_header_len(skb);
1400         /* Enable TCP/UDP checksum */
1401         d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1402         /* Calculate pseudo-header */
1403         d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1404
1405         return 0;
1406 }
1407
1408 static inline void wil_tx_last_desc(struct vring_tx_desc *d)
1409 {
1410         d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS) |
1411               BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS) |
1412               BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
1413 }
1414
1415 static inline void wil_set_tx_desc_last_tso(volatile struct vring_tx_desc *d)
1416 {
1417         d->dma.d0 |= wil_tso_type_lst <<
1418                   DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS;
1419 }
1420
1421 static int __wil_tx_vring_tso(struct wil6210_priv *wil, struct vring *vring,
1422                               struct sk_buff *skb)
1423 {
1424         struct device *dev = wil_to_dev(wil);
1425
1426         /* point to descriptors in shared memory */
1427         volatile struct vring_tx_desc *_desc = NULL, *_hdr_desc,
1428                                       *_first_desc = NULL;
1429
1430         /* pointers to shadow descriptors */
1431         struct vring_tx_desc desc_mem, hdr_desc_mem, first_desc_mem,
1432                              *d = &hdr_desc_mem, *hdr_desc = &hdr_desc_mem,
1433                              *first_desc = &first_desc_mem;
1434
1435         /* pointer to shadow descriptors' context */
1436         struct wil_ctx *hdr_ctx, *first_ctx = NULL;
1437
1438         int descs_used = 0; /* total number of used descriptors */
1439         int sg_desc_cnt = 0; /* number of descriptors for current mss*/
1440
1441         u32 swhead = vring->swhead;
1442         int used, avail = wil_vring_avail_tx(vring);
1443         int nr_frags = skb_shinfo(skb)->nr_frags;
1444         int min_desc_required = nr_frags + 1;
1445         int mss = skb_shinfo(skb)->gso_size;    /* payload size w/o headers */
1446         int f, len, hdrlen, headlen;
1447         int vring_index = vring - wil->vring_tx;
1448         struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1449         uint i = swhead;
1450         dma_addr_t pa;
1451         const skb_frag_t *frag = NULL;
1452         int rem_data = mss;
1453         int lenmss;
1454         int hdr_compensation_need = true;
1455         int desc_tso_type = wil_tso_type_first;
1456         bool is_ipv4;
1457         int tcp_hdr_len;
1458         int skb_net_hdr_len;
1459         int gso_type;
1460         int rc = -EINVAL;
1461
1462         wil_dbg_txrx(wil, "tx_vring_tso: %d bytes to vring %d\n", skb->len,
1463                      vring_index);
1464
1465         if (unlikely(!txdata->enabled))
1466                 return -EINVAL;
1467
1468         /* A typical page 4K is 3-4 payloads, we assume each fragment
1469          * is a full payload, that's how min_desc_required has been
1470          * calculated. In real we might need more or less descriptors,
1471          * this is the initial check only.
1472          */
1473         if (unlikely(avail < min_desc_required)) {
1474                 wil_err_ratelimited(wil,
1475                                     "TSO: Tx ring[%2d] full. No space for %d fragments\n",
1476                                     vring_index, min_desc_required);
1477                 return -ENOMEM;
1478         }
1479
1480         /* Header Length = MAC header len + IP header len + TCP header len*/
1481         hdrlen = ETH_HLEN +
1482                 (int)skb_network_header_len(skb) +
1483                 tcp_hdrlen(skb);
1484
1485         gso_type = skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4);
1486         switch (gso_type) {
1487         case SKB_GSO_TCPV4:
1488                 /* TCP v4, zero out the IP length and IPv4 checksum fields
1489                  * as required by the offloading doc
1490                  */
1491                 ip_hdr(skb)->tot_len = 0;
1492                 ip_hdr(skb)->check = 0;
1493                 is_ipv4 = true;
1494                 break;
1495         case SKB_GSO_TCPV6:
1496                 /* TCP v6, zero out the payload length */
1497                 ipv6_hdr(skb)->payload_len = 0;
1498                 is_ipv4 = false;
1499                 break;
1500         default:
1501                 /* other than TCPv4 or TCPv6 types are not supported for TSO.
1502                  * It is also illegal for both to be set simultaneously
1503                  */
1504                 return -EINVAL;
1505         }
1506
1507         if (skb->ip_summed != CHECKSUM_PARTIAL)
1508                 return -EINVAL;
1509
1510         /* tcp header length and skb network header length are fixed for all
1511          * packet's descriptors - read then once here
1512          */
1513         tcp_hdr_len = tcp_hdrlen(skb);
1514         skb_net_hdr_len = skb_network_header_len(skb);
1515
1516         _hdr_desc = &vring->va[i].tx;
1517
1518         pa = dma_map_single(dev, skb->data, hdrlen, DMA_TO_DEVICE);
1519         if (unlikely(dma_mapping_error(dev, pa))) {
1520                 wil_err(wil, "TSO: Skb head DMA map error\n");
1521                 goto err_exit;
1522         }
1523
1524         wil_tx_desc_map(hdr_desc, pa, hdrlen, vring_index);
1525         wil_tx_desc_offload_setup_tso(hdr_desc, skb, wil_tso_type_hdr, is_ipv4,
1526                                       tcp_hdr_len, skb_net_hdr_len);
1527         wil_tx_last_desc(hdr_desc);
1528
1529         vring->ctx[i].mapped_as = wil_mapped_as_single;
1530         hdr_ctx = &vring->ctx[i];
1531
1532         descs_used++;
1533         headlen = skb_headlen(skb) - hdrlen;
1534
1535         for (f = headlen ? -1 : 0; f < nr_frags; f++)  {
1536                 if (headlen) {
1537                         len = headlen;
1538                         wil_dbg_txrx(wil, "TSO: process skb head, len %u\n",
1539                                      len);
1540                 } else {
1541                         frag = &skb_shinfo(skb)->frags[f];
1542                         len = frag->size;
1543                         wil_dbg_txrx(wil, "TSO: frag[%d]: len %u\n", f, len);
1544                 }
1545
1546                 while (len) {
1547                         wil_dbg_txrx(wil,
1548                                      "TSO: len %d, rem_data %d, descs_used %d\n",
1549                                      len, rem_data, descs_used);
1550
1551                         if (descs_used == avail)  {
1552                                 wil_err_ratelimited(wil, "TSO: ring overflow\n");
1553                                 rc = -ENOMEM;
1554                                 goto mem_error;
1555                         }
1556
1557                         lenmss = min_t(int, rem_data, len);
1558                         i = (swhead + descs_used) % vring->size;
1559                         wil_dbg_txrx(wil, "TSO: lenmss %d, i %d\n", lenmss, i);
1560
1561                         if (!headlen) {
1562                                 pa = skb_frag_dma_map(dev, frag,
1563                                                       frag->size - len, lenmss,
1564                                                       DMA_TO_DEVICE);
1565                                 vring->ctx[i].mapped_as = wil_mapped_as_page;
1566                         } else {
1567                                 pa = dma_map_single(dev,
1568                                                     skb->data +
1569                                                     skb_headlen(skb) - headlen,
1570                                                     lenmss,
1571                                                     DMA_TO_DEVICE);
1572                                 vring->ctx[i].mapped_as = wil_mapped_as_single;
1573                                 headlen -= lenmss;
1574                         }
1575
1576                         if (unlikely(dma_mapping_error(dev, pa))) {
1577                                 wil_err(wil, "TSO: DMA map page error\n");
1578                                 goto mem_error;
1579                         }
1580
1581                         _desc = &vring->va[i].tx;
1582
1583                         if (!_first_desc) {
1584                                 _first_desc = _desc;
1585                                 first_ctx = &vring->ctx[i];
1586                                 d = first_desc;
1587                         } else {
1588                                 d = &desc_mem;
1589                         }
1590
1591                         wil_tx_desc_map(d, pa, lenmss, vring_index);
1592                         wil_tx_desc_offload_setup_tso(d, skb, desc_tso_type,
1593                                                       is_ipv4, tcp_hdr_len,
1594                                                       skb_net_hdr_len);
1595
1596                         /* use tso_type_first only once */
1597                         desc_tso_type = wil_tso_type_mid;
1598
1599                         descs_used++;  /* desc used so far */
1600                         sg_desc_cnt++; /* desc used for this segment */
1601                         len -= lenmss;
1602                         rem_data -= lenmss;
1603
1604                         wil_dbg_txrx(wil,
1605                                      "TSO: len %d, rem_data %d, descs_used %d, sg_desc_cnt %d,\n",
1606                                      len, rem_data, descs_used, sg_desc_cnt);
1607
1608                         /* Close the segment if reached mss size or last frag*/
1609                         if (rem_data == 0 || (f == nr_frags - 1 && len == 0)) {
1610                                 if (hdr_compensation_need) {
1611                                         /* first segment include hdr desc for
1612                                          * release
1613                                          */
1614                                         hdr_ctx->nr_frags = sg_desc_cnt;
1615                                         wil_tx_desc_set_nr_frags(first_desc,
1616                                                                  sg_desc_cnt +
1617                                                                  1);
1618                                         hdr_compensation_need = false;
1619                                 } else {
1620                                         wil_tx_desc_set_nr_frags(first_desc,
1621                                                                  sg_desc_cnt);
1622                                 }
1623                                 first_ctx->nr_frags = sg_desc_cnt - 1;
1624
1625                                 wil_tx_last_desc(d);
1626
1627                                 /* first descriptor may also be the last
1628                                  * for this mss - make sure not to copy
1629                                  * it twice
1630                                  */
1631                                 if (first_desc != d)
1632                                         *_first_desc = *first_desc;
1633
1634                                 /*last descriptor will be copied at the end
1635                                  * of this TS processing
1636                                  */
1637                                 if (f < nr_frags - 1 || len > 0)
1638                                         *_desc = *d;
1639
1640                                 rem_data = mss;
1641                                 _first_desc = NULL;
1642                                 sg_desc_cnt = 0;
1643                         } else if (first_desc != d) /* update mid descriptor */
1644                                         *_desc = *d;
1645                 }
1646         }
1647
1648         /* first descriptor may also be the last.
1649          * in this case d pointer is invalid
1650          */
1651         if (_first_desc == _desc)
1652                 d = first_desc;
1653
1654         /* Last data descriptor */
1655         wil_set_tx_desc_last_tso(d);
1656         *_desc = *d;
1657
1658         /* Fill the total number of descriptors in first desc (hdr)*/
1659         wil_tx_desc_set_nr_frags(hdr_desc, descs_used);
1660         *_hdr_desc = *hdr_desc;
1661
1662         /* hold reference to skb
1663          * to prevent skb release before accounting
1664          * in case of immediate "tx done"
1665          */
1666         vring->ctx[i].skb = skb_get(skb);
1667
1668         /* performance monitoring */
1669         used = wil_vring_used_tx(vring);
1670         if (wil_val_in_range(wil->vring_idle_trsh,
1671                              used, used + descs_used)) {
1672                 txdata->idle += get_cycles() - txdata->last_idle;
1673                 wil_dbg_txrx(wil,  "Ring[%2d] not idle %d -> %d\n",
1674                              vring_index, used, used + descs_used);
1675         }
1676
1677         /* Make sure to advance the head only after descriptor update is done.
1678          * This will prevent a race condition where the completion thread
1679          * will see the DU bit set from previous run and will handle the
1680          * skb before it was completed.
1681          */
1682         wmb();
1683
1684         /* advance swhead */
1685         wil_vring_advance_head(vring, descs_used);
1686         wil_dbg_txrx(wil, "TSO: Tx swhead %d -> %d\n", swhead, vring->swhead);
1687
1688         /* make sure all writes to descriptors (shared memory) are done before
1689          * committing them to HW
1690          */
1691         wmb();
1692
1693         wil_w(wil, vring->hwtail, vring->swhead);
1694         return 0;
1695
1696 mem_error:
1697         while (descs_used > 0) {
1698                 struct wil_ctx *ctx;
1699
1700                 i = (swhead + descs_used - 1) % vring->size;
1701                 d = (struct vring_tx_desc *)&vring->va[i].tx;
1702                 _desc = &vring->va[i].tx;
1703                 *d = *_desc;
1704                 _desc->dma.status = TX_DMA_STATUS_DU;
1705                 ctx = &vring->ctx[i];
1706                 wil_txdesc_unmap(dev, d, ctx);
1707                 memset(ctx, 0, sizeof(*ctx));
1708                 descs_used--;
1709         }
1710 err_exit:
1711         return rc;
1712 }
1713
1714 static int __wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1715                           struct sk_buff *skb)
1716 {
1717         struct device *dev = wil_to_dev(wil);
1718         struct vring_tx_desc dd, *d = &dd;
1719         volatile struct vring_tx_desc *_d;
1720         u32 swhead = vring->swhead;
1721         int avail = wil_vring_avail_tx(vring);
1722         int nr_frags = skb_shinfo(skb)->nr_frags;
1723         uint f = 0;
1724         int vring_index = vring - wil->vring_tx;
1725         struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1726         uint i = swhead;
1727         dma_addr_t pa;
1728         int used;
1729         bool mcast = (vring_index == wil->bcast_vring);
1730         uint len = skb_headlen(skb);
1731
1732         wil_dbg_txrx(wil, "tx_vring: %d bytes to vring %d\n", skb->len,
1733                      vring_index);
1734
1735         if (unlikely(!txdata->enabled))
1736                 return -EINVAL;
1737
1738         if (unlikely(avail < 1 + nr_frags)) {
1739                 wil_err_ratelimited(wil,
1740                                     "Tx ring[%2d] full. No space for %d fragments\n",
1741                                     vring_index, 1 + nr_frags);
1742                 return -ENOMEM;
1743         }
1744         _d = &vring->va[i].tx;
1745
1746         pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
1747
1748         wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index,
1749                      skb_headlen(skb), skb->data, &pa);
1750         wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
1751                           skb->data, skb_headlen(skb), false);
1752
1753         if (unlikely(dma_mapping_error(dev, pa)))
1754                 return -EINVAL;
1755         vring->ctx[i].mapped_as = wil_mapped_as_single;
1756         /* 1-st segment */
1757         wil_tx_desc_map(d, pa, len, vring_index);
1758         if (unlikely(mcast)) {
1759                 d->mac.d[0] |= BIT(MAC_CFG_DESC_TX_0_MCS_EN_POS); /* MCS 0 */
1760                 if (unlikely(len > WIL_BCAST_MCS0_LIMIT)) /* set MCS 1 */
1761                         d->mac.d[0] |= (1 << MAC_CFG_DESC_TX_0_MCS_INDEX_POS);
1762         }
1763         /* Process TCP/UDP checksum offloading */
1764         if (unlikely(wil_tx_desc_offload_setup(d, skb))) {
1765                 wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
1766                         vring_index);
1767                 goto dma_error;
1768         }
1769
1770         vring->ctx[i].nr_frags = nr_frags;
1771         wil_tx_desc_set_nr_frags(d, nr_frags + 1);
1772
1773         /* middle segments */
1774         for (; f < nr_frags; f++) {
1775                 const struct skb_frag_struct *frag =
1776                                 &skb_shinfo(skb)->frags[f];
1777                 int len = skb_frag_size(frag);
1778
1779                 *_d = *d;
1780                 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1781                 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1782                                   (const void *)d, sizeof(*d), false);
1783                 i = (swhead + f + 1) % vring->size;
1784                 _d = &vring->va[i].tx;
1785                 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
1786                                       DMA_TO_DEVICE);
1787                 if (unlikely(dma_mapping_error(dev, pa))) {
1788                         wil_err(wil, "Tx[%2d] failed to map fragment\n",
1789                                 vring_index);
1790                         goto dma_error;
1791                 }
1792                 vring->ctx[i].mapped_as = wil_mapped_as_page;
1793                 wil_tx_desc_map(d, pa, len, vring_index);
1794                 /* no need to check return code -
1795                  * if it succeeded for 1-st descriptor,
1796                  * it will succeed here too
1797                  */
1798                 wil_tx_desc_offload_setup(d, skb);
1799         }
1800         /* for the last seg only */
1801         d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
1802         d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
1803         d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
1804         *_d = *d;
1805         wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i);
1806         wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1807                           (const void *)d, sizeof(*d), false);
1808
1809         /* hold reference to skb
1810          * to prevent skb release before accounting
1811          * in case of immediate "tx done"
1812          */
1813         vring->ctx[i].skb = skb_get(skb);
1814
1815         /* performance monitoring */
1816         used = wil_vring_used_tx(vring);
1817         if (wil_val_in_range(wil->vring_idle_trsh,
1818                              used, used + nr_frags + 1)) {
1819                 txdata->idle += get_cycles() - txdata->last_idle;
1820                 wil_dbg_txrx(wil,  "Ring[%2d] not idle %d -> %d\n",
1821                              vring_index, used, used + nr_frags + 1);
1822         }
1823
1824         /* Make sure to advance the head only after descriptor update is done.
1825          * This will prevent a race condition where the completion thread
1826          * will see the DU bit set from previous run and will handle the
1827          * skb before it was completed.
1828          */
1829         wmb();
1830
1831         /* advance swhead */
1832         wil_vring_advance_head(vring, nr_frags + 1);
1833         wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead,
1834                      vring->swhead);
1835         trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
1836
1837         /* make sure all writes to descriptors (shared memory) are done before
1838          * committing them to HW
1839          */
1840         wmb();
1841
1842         wil_w(wil, vring->hwtail, vring->swhead);
1843
1844         return 0;
1845  dma_error:
1846         /* unmap what we have mapped */
1847         nr_frags = f + 1; /* frags mapped + one for skb head */
1848         for (f = 0; f < nr_frags; f++) {
1849                 struct wil_ctx *ctx;
1850
1851                 i = (swhead + f) % vring->size;
1852                 ctx = &vring->ctx[i];
1853                 _d = &vring->va[i].tx;
1854                 *d = *_d;
1855                 _d->dma.status = TX_DMA_STATUS_DU;
1856                 wil_txdesc_unmap(dev, d, ctx);
1857
1858                 memset(ctx, 0, sizeof(*ctx));
1859         }
1860
1861         return -EINVAL;
1862 }
1863
1864 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
1865                         struct sk_buff *skb)
1866 {
1867         int vring_index = vring - wil->vring_tx;
1868         struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
1869         int rc;
1870
1871         spin_lock(&txdata->lock);
1872
1873         if (test_bit(wil_status_suspending, wil->status) ||
1874             test_bit(wil_status_suspended, wil->status) ||
1875             test_bit(wil_status_resuming, wil->status)) {
1876                 wil_dbg_txrx(wil,
1877                              "suspend/resume in progress. drop packet\n");
1878                 spin_unlock(&txdata->lock);
1879                 return -EINVAL;
1880         }
1881
1882         rc = (skb_is_gso(skb) ? __wil_tx_vring_tso : __wil_tx_vring)
1883              (wil, vring, skb);
1884
1885         spin_unlock(&txdata->lock);
1886
1887         return rc;
1888 }
1889
1890 /**
1891  * Check status of tx vrings and stop/wake net queues if needed
1892  *
1893  * This function does one of two checks:
1894  * In case check_stop is true, will check if net queues need to be stopped. If
1895  * the conditions for stopping are met, netif_tx_stop_all_queues() is called.
1896  * In case check_stop is false, will check if net queues need to be waked. If
1897  * the conditions for waking are met, netif_tx_wake_all_queues() is called.
1898  * vring is the vring which is currently being modified by either adding
1899  * descriptors (tx) into it or removing descriptors (tx complete) from it. Can
1900  * be null when irrelevant (e.g. connect/disconnect events).
1901  *
1902  * The implementation is to stop net queues if modified vring has low
1903  * descriptor availability. Wake if all vrings are not in low descriptor
1904  * availability and modified vring has high descriptor availability.
1905  */
1906 static inline void __wil_update_net_queues(struct wil6210_priv *wil,
1907                                            struct vring *vring,
1908                                            bool check_stop)
1909 {
1910         int i;
1911
1912         if (vring)
1913                 wil_dbg_txrx(wil, "vring %d, check_stop=%d, stopped=%d",
1914                              (int)(vring - wil->vring_tx), check_stop,
1915                              wil->net_queue_stopped);
1916         else
1917                 wil_dbg_txrx(wil, "check_stop=%d, stopped=%d",
1918                              check_stop, wil->net_queue_stopped);
1919
1920         if (check_stop == wil->net_queue_stopped)
1921                 /* net queues already in desired state */
1922                 return;
1923
1924         if (check_stop) {
1925                 if (!vring || unlikely(wil_vring_avail_low(vring))) {
1926                         /* not enough room in the vring */
1927                         netif_tx_stop_all_queues(wil_to_ndev(wil));
1928                         wil->net_queue_stopped = true;
1929                         wil_dbg_txrx(wil, "netif_tx_stop called\n");
1930                 }
1931                 return;
1932         }
1933
1934         /* Do not wake the queues in suspend flow */
1935         if (test_bit(wil_status_suspending, wil->status) ||
1936             test_bit(wil_status_suspended, wil->status))
1937                 return;
1938
1939         /* check wake */
1940         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
1941                 struct vring *cur_vring = &wil->vring_tx[i];
1942                 struct vring_tx_data *txdata = &wil->vring_tx_data[i];
1943
1944                 if (!cur_vring->va || !txdata->enabled || cur_vring == vring)
1945                         continue;
1946
1947                 if (wil_vring_avail_low(cur_vring)) {
1948                         wil_dbg_txrx(wil, "vring %d full, can't wake\n",
1949                                      (int)(cur_vring - wil->vring_tx));
1950                         return;
1951                 }
1952         }
1953
1954         if (!vring || wil_vring_avail_high(vring)) {
1955                 /* enough room in the vring */
1956                 wil_dbg_txrx(wil, "calling netif_tx_wake\n");
1957                 netif_tx_wake_all_queues(wil_to_ndev(wil));
1958                 wil->net_queue_stopped = false;
1959         }
1960 }
1961
1962 void wil_update_net_queues(struct wil6210_priv *wil, struct vring *vring,
1963                            bool check_stop)
1964 {
1965         spin_lock(&wil->net_queue_lock);
1966         __wil_update_net_queues(wil, vring, check_stop);
1967         spin_unlock(&wil->net_queue_lock);
1968 }
1969
1970 void wil_update_net_queues_bh(struct wil6210_priv *wil, struct vring *vring,
1971                               bool check_stop)
1972 {
1973         spin_lock_bh(&wil->net_queue_lock);
1974         __wil_update_net_queues(wil, vring, check_stop);
1975         spin_unlock_bh(&wil->net_queue_lock);
1976 }
1977
1978 netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1979 {
1980         struct wil6210_priv *wil = ndev_to_wil(ndev);
1981         struct ethhdr *eth = (void *)skb->data;
1982         bool bcast = is_multicast_ether_addr(eth->h_dest);
1983         struct vring *vring;
1984         static bool pr_once_fw;
1985         int rc;
1986
1987         wil_dbg_txrx(wil, "start_xmit\n");
1988         if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
1989                 if (!pr_once_fw) {
1990                         wil_err(wil, "FW not ready\n");
1991                         pr_once_fw = true;
1992                 }
1993                 goto drop;
1994         }
1995         if (unlikely(!test_bit(wil_status_fwconnected, wil->status))) {
1996                 wil_dbg_ratelimited(wil, "FW not connected, packet dropped\n");
1997                 goto drop;
1998         }
1999         if (unlikely(wil->wdev->iftype == NL80211_IFTYPE_MONITOR)) {
2000                 wil_err(wil, "Xmit in monitor mode not supported\n");
2001                 goto drop;
2002         }
2003         pr_once_fw = false;
2004
2005         /* find vring */
2006         if (wil->wdev->iftype == NL80211_IFTYPE_STATION && !wil->pbss) {
2007                 /* in STA mode (ESS), all to same VRING (to AP) */
2008                 vring = wil_find_tx_vring_sta(wil, skb);
2009         } else if (bcast) {
2010                 if (wil->pbss)
2011                         /* in pbss, no bcast VRING - duplicate skb in
2012                          * all stations VRINGs
2013                          */
2014                         vring = wil_find_tx_bcast_2(wil, skb);
2015                 else if (wil->wdev->iftype == NL80211_IFTYPE_AP)
2016                         /* AP has a dedicated bcast VRING */
2017                         vring = wil_find_tx_bcast_1(wil, skb);
2018                 else
2019                         /* unexpected combination, fallback to duplicating
2020                          * the skb in all stations VRINGs
2021                          */
2022                         vring = wil_find_tx_bcast_2(wil, skb);
2023         } else {
2024                 /* unicast, find specific VRING by dest. address */
2025                 vring = wil_find_tx_ucast(wil, skb);
2026         }
2027         if (unlikely(!vring)) {
2028                 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
2029                 goto drop;
2030         }
2031         /* set up vring entry */
2032         rc = wil_tx_vring(wil, vring, skb);
2033
2034         switch (rc) {
2035         case 0:
2036                 /* shall we stop net queues? */
2037                 wil_update_net_queues_bh(wil, vring, true);
2038                 /* statistics will be updated on the tx_complete */
2039                 dev_kfree_skb_any(skb);
2040                 return NETDEV_TX_OK;
2041         case -ENOMEM:
2042                 return NETDEV_TX_BUSY;
2043         default:
2044                 break; /* goto drop; */
2045         }
2046  drop:
2047         ndev->stats.tx_dropped++;
2048         dev_kfree_skb_any(skb);
2049
2050         return NET_XMIT_DROP;
2051 }
2052
2053 static inline bool wil_need_txstat(struct sk_buff *skb)
2054 {
2055         struct ethhdr *eth = (void *)skb->data;
2056
2057         return is_unicast_ether_addr(eth->h_dest) && skb->sk &&
2058                (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS);
2059 }
2060
2061 static inline void wil_consume_skb(struct sk_buff *skb, bool acked)
2062 {
2063         if (unlikely(wil_need_txstat(skb)))
2064                 skb_complete_wifi_ack(skb, acked);
2065         else
2066                 acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb);
2067 }
2068
2069 /**
2070  * Clean up transmitted skb's from the Tx VRING
2071  *
2072  * Return number of descriptors cleared
2073  *
2074  * Safe to call from IRQ
2075  */
2076 int wil_tx_complete(struct wil6210_priv *wil, int ringid)
2077 {
2078         struct net_device *ndev = wil_to_ndev(wil);
2079         struct device *dev = wil_to_dev(wil);
2080         struct vring *vring = &wil->vring_tx[ringid];
2081         struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
2082         int done = 0;
2083         int cid = wil->vring2cid_tid[ringid][0];
2084         struct wil_net_stats *stats = NULL;
2085         volatile struct vring_tx_desc *_d;
2086         int used_before_complete;
2087         int used_new;
2088
2089         if (unlikely(!vring->va)) {
2090                 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
2091                 return 0;
2092         }
2093
2094         if (unlikely(!txdata->enabled)) {
2095                 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
2096                 return 0;
2097         }
2098
2099         wil_dbg_txrx(wil, "tx_complete: (%d)\n", ringid);
2100
2101         used_before_complete = wil_vring_used_tx(vring);
2102
2103         if (cid < WIL6210_MAX_CID)
2104                 stats = &wil->sta[cid].stats;
2105
2106         while (!wil_vring_is_empty(vring)) {
2107                 int new_swtail;
2108                 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
2109                 /**
2110                  * For the fragmented skb, HW will set DU bit only for the
2111                  * last fragment. look for it.
2112                  * In TSO the first DU will include hdr desc
2113                  */
2114                 int lf = (vring->swtail + ctx->nr_frags) % vring->size;
2115                 /* TODO: check we are not past head */
2116
2117                 _d = &vring->va[lf].tx;
2118                 if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
2119                         break;
2120
2121                 new_swtail = (lf + 1) % vring->size;
2122                 while (vring->swtail != new_swtail) {
2123                         struct vring_tx_desc dd, *d = &dd;
2124                         u16 dmalen;
2125                         struct sk_buff *skb;
2126
2127                         ctx = &vring->ctx[vring->swtail];
2128                         skb = ctx->skb;
2129                         _d = &vring->va[vring->swtail].tx;
2130
2131                         *d = *_d;
2132
2133                         dmalen = le16_to_cpu(d->dma.length);
2134                         trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
2135                                               d->dma.error);
2136                         wil_dbg_txrx(wil,
2137                                      "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
2138                                      ringid, vring->swtail, dmalen,
2139                                      d->dma.status, d->dma.error);
2140                         wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
2141                                           (const void *)d, sizeof(*d), false);
2142
2143                         wil_txdesc_unmap(dev, d, ctx);
2144
2145                         if (skb) {
2146                                 if (likely(d->dma.error == 0)) {
2147                                         ndev->stats.tx_packets++;
2148                                         ndev->stats.tx_bytes += skb->len;
2149                                         if (stats) {
2150                                                 stats->tx_packets++;
2151                                                 stats->tx_bytes += skb->len;
2152                                         }
2153                                 } else {
2154                                         ndev->stats.tx_errors++;
2155                                         if (stats)
2156                                                 stats->tx_errors++;
2157                                 }
2158                                 wil_consume_skb(skb, d->dma.error == 0);
2159                         }
2160                         memset(ctx, 0, sizeof(*ctx));
2161                         /* Make sure the ctx is zeroed before updating the tail
2162                          * to prevent a case where wil_tx_vring will see
2163                          * this descriptor as used and handle it before ctx zero
2164                          * is completed.
2165                          */
2166                         wmb();
2167                         /* There is no need to touch HW descriptor:
2168                          * - ststus bit TX_DMA_STATUS_DU is set by design,
2169                          *   so hardware will not try to process this desc.,
2170                          * - rest of descriptor will be initialized on Tx.
2171                          */
2172                         vring->swtail = wil_vring_next_tail(vring);
2173                         done++;
2174                 }
2175         }
2176
2177         /* performance monitoring */
2178         used_new = wil_vring_used_tx(vring);
2179         if (wil_val_in_range(wil->vring_idle_trsh,
2180                              used_new, used_before_complete)) {
2181                 wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
2182                              ringid, used_before_complete, used_new);
2183                 txdata->last_idle = get_cycles();
2184         }
2185
2186         /* shall we wake net queues? */
2187         if (done)
2188                 wil_update_net_queues(wil, vring, false);
2189
2190         return done;
2191 }