blob: 18003c0d5141f60a30bc67edfa3a43946ddaf8b0 [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
2 * Copyright (c) 2012 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
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080017#include <linux/etherdevice.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080018#include <net/ieee80211_radiotap.h>
19#include <linux/if_arp.h>
20#include <linux/moduleparam.h>
Kirshenbaum Erez504937d2013-07-21 11:34:37 +030021#include <linux/ip.h>
22#include <linux/ipv6.h>
23#include <net/ipv6.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080024
25#include "wil6210.h"
26#include "wmi.h"
27#include "txrx.h"
Vladimir Kondratiev98658092013-05-12 14:43:35 +030028#include "trace.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080029
30static bool rtap_include_phy_info;
31module_param(rtap_include_phy_info, bool, S_IRUGO);
32MODULE_PARM_DESC(rtap_include_phy_info,
33 " Include PHY info in the radiotap header, default - no");
34
35static inline int wil_vring_is_empty(struct vring *vring)
36{
37 return vring->swhead == vring->swtail;
38}
39
40static inline u32 wil_vring_next_tail(struct vring *vring)
41{
42 return (vring->swtail + 1) % vring->size;
43}
44
45static inline void wil_vring_advance_head(struct vring *vring, int n)
46{
47 vring->swhead = (vring->swhead + n) % vring->size;
48}
49
50static inline int wil_vring_is_full(struct vring *vring)
51{
52 return wil_vring_next_tail(vring) == vring->swhead;
53}
54/*
55 * Available space in Tx Vring
56 */
57static inline int wil_vring_avail_tx(struct vring *vring)
58{
59 u32 swhead = vring->swhead;
60 u32 swtail = vring->swtail;
61 int used = (vring->size + swhead - swtail) % vring->size;
62
63 return vring->size - used - 1;
64}
65
66static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
67{
68 struct device *dev = wil_to_dev(wil);
69 size_t sz = vring->size * sizeof(vring->va[0]);
70 uint i;
71
72 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
73
74 vring->swhead = 0;
75 vring->swtail = 0;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +030076 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080077 if (!vring->ctx) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080078 vring->va = NULL;
79 return -ENOMEM;
80 }
81 /*
82 * vring->va should be aligned on its size rounded up to power of 2
83 * This is granted by the dma_alloc_coherent
84 */
85 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
86 if (!vring->va) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080087 kfree(vring->ctx);
88 vring->ctx = NULL;
89 return -ENOMEM;
90 }
91 /* initially, all descriptors are SW owned
92 * For Tx and Rx, ownership bit is at the same location, thus
93 * we can use any
94 */
95 for (i = 0; i < vring->size; i++) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +030096 volatile struct vring_tx_desc *_d = &(vring->va[i].tx);
97 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080098 }
99
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200100 wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size,
Vladimir Kondratiev2057ebb2013-01-28 18:30:58 +0200101 vring->va, (unsigned long long)vring->pa, vring->ctx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800102
103 return 0;
104}
105
106static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
107 int tx)
108{
109 struct device *dev = wil_to_dev(wil);
110 size_t sz = vring->size * sizeof(vring->va[0]);
111
112 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300113 dma_addr_t pa;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300114 u16 dmalen;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300115 struct wil_ctx *ctx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300116
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800117 if (tx) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300118 struct vring_tx_desc dd, *d = &dd;
119 volatile struct vring_tx_desc *_d =
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800120 &vring->va[vring->swtail].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300121
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300122 ctx = &vring->ctx[vring->swtail];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300123 *d = *_d;
124 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300125 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300126 if (vring->ctx[vring->swtail].mapped_as_page) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300127 dma_unmap_page(dev, pa, dmalen,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800128 DMA_TO_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300129 } else {
130 dma_unmap_single(dev, pa, dmalen,
131 DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800132 }
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300133 if (ctx->skb)
134 dev_kfree_skb_any(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800135 vring->swtail = wil_vring_next_tail(vring);
136 } else { /* rx */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300137 struct vring_rx_desc dd, *d = &dd;
138 volatile struct vring_rx_desc *_d =
Vladimir Kondratiev4d1ac072013-07-11 18:03:38 +0300139 &vring->va[vring->swhead].rx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300140
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300141 ctx = &vring->ctx[vring->swhead];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300142 *d = *_d;
143 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300144 dmalen = le16_to_cpu(d->dma.length);
145 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300146 kfree_skb(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800147 wil_vring_advance_head(vring, 1);
148 }
149 }
150 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
151 kfree(vring->ctx);
152 vring->pa = 0;
153 vring->va = NULL;
154 vring->ctx = NULL;
155}
156
157/**
158 * Allocate one skb for Rx VRING
159 *
160 * Safe to call from IRQ
161 */
162static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
163 u32 i, int headroom)
164{
165 struct device *dev = wil_to_dev(wil);
166 unsigned int sz = RX_BUF_LEN;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300167 struct vring_rx_desc dd, *d = &dd;
168 volatile struct vring_rx_desc *_d = &(vring->va[i].rx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800169 dma_addr_t pa;
170
171 /* TODO align */
172 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
173 if (unlikely(!skb))
174 return -ENOMEM;
175
176 skb_reserve(skb, headroom);
177 skb_put(skb, sz);
178
179 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
180 if (unlikely(dma_mapping_error(dev, pa))) {
181 kfree_skb(skb);
182 return -ENOMEM;
183 }
184
185 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300186 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800187 /* ip_length don't care */
188 /* b11 don't care */
189 /* error don't care */
190 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300191 d->dma.length = cpu_to_le16(sz);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300192 *_d = *d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300193 vring->ctx[i].skb = skb;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800194
195 return 0;
196}
197
198/**
199 * Adds radiotap header
200 *
201 * Any error indicated as "Bad FCS"
202 *
203 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
204 * - Rx descriptor: 32 bytes
205 * - Phy info
206 */
207static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300208 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800209{
210 struct wireless_dev *wdev = wil->wdev;
211 struct wil6210_rtap {
212 struct ieee80211_radiotap_header rthdr;
213 /* fields should be in the order of bits in rthdr.it_present */
214 /* flags */
215 u8 flags;
216 /* channel */
217 __le16 chnl_freq __aligned(2);
218 __le16 chnl_flags;
219 /* MCS */
220 u8 mcs_present;
221 u8 mcs_flags;
222 u8 mcs_index;
223 } __packed;
224 struct wil6210_rtap_vendor {
225 struct wil6210_rtap rtap;
226 /* vendor */
227 u8 vendor_oui[3] __aligned(2);
228 u8 vendor_ns;
229 __le16 vendor_skip;
230 u8 vendor_data[0];
231 } __packed;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300232 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800233 struct wil6210_rtap_vendor *rtap_vendor;
234 int rtap_len = sizeof(struct wil6210_rtap);
235 int phy_length = 0; /* phy info header size, bytes */
236 static char phy_data[128];
237 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
238
239 if (rtap_include_phy_info) {
240 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
241 /* calculate additional length */
242 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
243 /**
244 * PHY info starts from 8-byte boundary
245 * there are 8-byte lines, last line may be partially
246 * written (HW bug), thus FW configures for last line
247 * to be excessive. Driver skips this last line.
248 */
249 int len = min_t(int, 8 + sizeof(phy_data),
250 wil_rxdesc_phy_length(d));
251 if (len > 8) {
252 void *p = skb_tail_pointer(skb);
253 void *pa = PTR_ALIGN(p, 8);
254 if (skb_tailroom(skb) >= len + (pa - p)) {
255 phy_length = len - 8;
256 memcpy(phy_data, pa, phy_length);
257 }
258 }
259 }
260 rtap_len += phy_length;
261 }
262
263 if (skb_headroom(skb) < rtap_len &&
264 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
265 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
266 return;
267 }
268
269 rtap_vendor = (void *)skb_push(skb, rtap_len);
270 memset(rtap_vendor, 0, rtap_len);
271
272 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
273 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
274 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
275 (1 << IEEE80211_RADIOTAP_FLAGS) |
276 (1 << IEEE80211_RADIOTAP_CHANNEL) |
277 (1 << IEEE80211_RADIOTAP_MCS));
278 if (d->dma.status & RX_DMA_STATUS_ERROR)
279 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
280
281 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
282 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
283
284 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
285 rtap_vendor->rtap.mcs_flags = 0;
286 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
287
288 if (rtap_include_phy_info) {
289 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
290 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
291 /* OUI for Wilocity 04:ce:14 */
292 rtap_vendor->vendor_oui[0] = 0x04;
293 rtap_vendor->vendor_oui[1] = 0xce;
294 rtap_vendor->vendor_oui[2] = 0x14;
295 rtap_vendor->vendor_ns = 1;
296 /* Rx descriptor + PHY data */
297 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
298 phy_length);
299 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
300 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
301 phy_length);
302 }
303}
304
305/*
306 * Fast swap in place between 2 registers
307 */
308static void wil_swap_u16(u16 *a, u16 *b)
309{
310 *a ^= *b;
311 *b ^= *a;
312 *a ^= *b;
313}
314
315static void wil_swap_ethaddr(void *data)
316{
317 struct ethhdr *eth = data;
318 u16 *s = (u16 *)eth->h_source;
319 u16 *d = (u16 *)eth->h_dest;
320
321 wil_swap_u16(s++, d++);
322 wil_swap_u16(s++, d++);
323 wil_swap_u16(s, d);
324}
325
326/**
327 * reap 1 frame from @swhead
328 *
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300329 * Rx descriptor copied to skb->cb
330 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800331 * Safe to call from IRQ
332 */
333static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
334 struct vring *vring)
335{
336 struct device *dev = wil_to_dev(wil);
337 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300338 volatile struct vring_rx_desc *_d;
339 struct vring_rx_desc *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800340 struct sk_buff *skb;
341 dma_addr_t pa;
342 unsigned int sz = RX_BUF_LEN;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300343 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800344 u8 ftype;
345 u8 ds_bits;
346
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300347 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
348
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800349 if (wil_vring_is_empty(vring))
350 return NULL;
351
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300352 _d = &(vring->va[vring->swhead].rx);
353 if (!(_d->dma.status & RX_DMA_STATUS_DU)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800354 /* it is not error, we just reached end of Rx done area */
355 return NULL;
356 }
357
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300358 skb = vring->ctx[vring->swhead].skb;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300359 d = wil_skb_rxdesc(skb);
360 *d = *_d;
361 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300362 vring->ctx[vring->swhead].skb = NULL;
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300363 wil_vring_advance_head(vring, 1);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300364
365 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
366 dmalen = le16_to_cpu(d->dma.length);
367
368 trace_wil6210_rx(vring->swhead, d);
369 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen);
370 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
371 (const void *)d, sizeof(*d), false);
372
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300373 if (dmalen > sz) {
374 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
Wei Yongjun110dea02013-05-23 17:10:43 +0800375 kfree_skb(skb);
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300376 return NULL;
377 }
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300378 skb_trim(skb, dmalen);
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300379
Vladimir Kondratievc0d37712013-05-12 14:43:34 +0300380 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
381 skb->data, skb_headlen(skb), false);
382
383
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300384 wil->stats.last_mcs_rx = wil_rxdesc_mcs(d);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800385
386 /* use radiotap header only if required */
387 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300388 wil_rx_add_radiotap_header(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800389
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800390 /* no extra checks if in sniffer mode */
391 if (ndev->type != ARPHRD_ETHER)
392 return skb;
393 /*
394 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
395 * Driver should recognize it by frame type, that is found
396 * in Rx descriptor. If type is not data, it is 802.11 frame as is
397 */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300398 ftype = wil_rxdesc_ftype(d) << 2;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800399 if (ftype != IEEE80211_FTYPE_DATA) {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200400 wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800401 /* TODO: process it */
402 kfree_skb(skb);
403 return NULL;
404 }
405
406 if (skb->len < ETH_HLEN) {
407 wil_err(wil, "Short frame, len = %d\n", skb->len);
408 /* TODO: process it (i.e. BAR) */
409 kfree_skb(skb);
410 return NULL;
411 }
412
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300413 /* L4 IDENT is on when HW calculated checksum, check status
414 * and in case of error drop the packet
415 * higher stack layers will handle retransmission (if required)
416 */
417 if (d->dma.status & RX_DMA_STATUS_L4_IDENT) {
418 /* L4 protocol identified, csum calculated */
Vladimir Kondratiev4a68ab102013-08-13 15:25:32 +0300419 if ((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0)
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300420 skb->ip_summed = CHECKSUM_UNNECESSARY;
Vladimir Kondratiev4a68ab102013-08-13 15:25:32 +0300421 /* If HW reports bad checksum, let IP stack re-check it
422 * For example, HW don't understand Microsoft IP stack that
423 * mis-calculates TCP checksum - if it should be 0x0,
424 * it writes 0xffff in violation of RFC 1624
425 */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300426 }
427
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300428 ds_bits = wil_rxdesc_ds_bits(d);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800429 if (ds_bits == 1) {
430 /*
431 * HW bug - in ToDS mode, i.e. Rx on AP side,
432 * addresses get swapped
433 */
434 wil_swap_ethaddr(skb->data);
435 }
436
437 return skb;
438}
439
440/**
441 * allocate and fill up to @count buffers in rx ring
442 * buffers posted at @swtail
443 */
444static int wil_rx_refill(struct wil6210_priv *wil, int count)
445{
446 struct net_device *ndev = wil_to_ndev(wil);
447 struct vring *v = &wil->vring_rx;
448 u32 next_tail;
449 int rc = 0;
450 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
451 WIL6210_RTAP_SIZE : 0;
452
453 for (; next_tail = wil_vring_next_tail(v),
454 (next_tail != v->swhead) && (count-- > 0);
455 v->swtail = next_tail) {
456 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
457 if (rc) {
458 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
459 rc, v->swtail);
460 break;
461 }
462 }
463 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
464
465 return rc;
466}
467
468/*
469 * Pass Rx packet to the netif. Update statistics.
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300470 * Called in softirq context (NAPI poll).
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800471 */
472static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
473{
474 int rc;
475 unsigned int len = skb->len;
476
Vladimir Kondratiev241804c2013-01-28 18:31:02 +0200477 skb_orphan(skb);
478
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300479 rc = netif_receive_skb(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800480
481 if (likely(rc == NET_RX_SUCCESS)) {
482 ndev->stats.rx_packets++;
483 ndev->stats.rx_bytes += len;
484
485 } else {
486 ndev->stats.rx_dropped++;
487 }
488}
489
490/**
491 * Proceed all completed skb's from Rx VRING
492 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300493 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800494 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300495void wil_rx_handle(struct wil6210_priv *wil, int *quota)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800496{
497 struct net_device *ndev = wil_to_ndev(wil);
498 struct vring *v = &wil->vring_rx;
499 struct sk_buff *skb;
500
501 if (!v->va) {
502 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
503 return;
504 }
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200505 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300506 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
507 (*quota)--;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800508
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800509 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
510 skb->dev = ndev;
511 skb_reset_mac_header(skb);
512 skb->ip_summed = CHECKSUM_UNNECESSARY;
513 skb->pkt_type = PACKET_OTHERHOST;
514 skb->protocol = htons(ETH_P_802_2);
515
516 } else {
517 skb->protocol = eth_type_trans(skb, ndev);
518 }
519
520 wil_netif_rx_any(skb, ndev);
521 }
522 wil_rx_refill(wil, v->size);
523}
524
525int wil_rx_init(struct wil6210_priv *wil)
526{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800527 struct vring *vring = &wil->vring_rx;
528 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800529
530 vring->size = WIL6210_RX_RING_SIZE;
531 rc = wil_vring_alloc(wil, vring);
532 if (rc)
533 return rc;
534
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +0200535 rc = wmi_rx_chain_add(wil, vring);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800536 if (rc)
537 goto err_free;
538
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800539 rc = wil_rx_refill(wil, vring->size);
540 if (rc)
541 goto err_free;
542
543 return 0;
544 err_free:
545 wil_vring_free(wil, vring, 0);
546
547 return rc;
548}
549
550void wil_rx_fini(struct wil6210_priv *wil)
551{
552 struct vring *vring = &wil->vring_rx;
553
Vladimir Kondratiev2acb4222013-01-28 18:31:08 +0200554 if (vring->va)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800555 wil_vring_free(wil, vring, 0);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800556}
557
558int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
559 int cid, int tid)
560{
561 int rc;
562 struct wmi_vring_cfg_cmd cmd = {
563 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
564 .vring_cfg = {
565 .tx_sw_ring = {
566 .max_mpdu_size = cpu_to_le16(TX_BUF_LEN),
Vladimir Kondratievb5d98e92013-04-18 14:33:51 +0300567 .ring_size = cpu_to_le16(size),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800568 },
569 .ringid = id,
570 .cidxtid = (cid & 0xf) | ((tid & 0xf) << 4),
571 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
572 .mac_ctrl = 0,
573 .to_resolution = 0,
574 .agg_max_wsize = 16,
575 .schd_params = {
576 .priority = cpu_to_le16(0),
577 .timeslot_us = cpu_to_le16(0xfff),
578 },
579 },
580 };
581 struct {
582 struct wil6210_mbox_hdr_wmi wmi;
583 struct wmi_vring_cfg_done_event cmd;
584 } __packed reply;
585 struct vring *vring = &wil->vring_tx[id];
586
587 if (vring->va) {
588 wil_err(wil, "Tx ring [%d] already allocated\n", id);
589 rc = -EINVAL;
590 goto out;
591 }
592
593 vring->size = size;
594 rc = wil_vring_alloc(wil, vring);
595 if (rc)
596 goto out;
597
598 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800599
600 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
601 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
602 if (rc)
603 goto out_free;
604
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200605 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800606 wil_err(wil, "Tx config failed, status 0x%02x\n",
607 reply.cmd.status);
Vladimir Kondratievc3319972013-01-28 18:31:09 +0200608 rc = -EINVAL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800609 goto out_free;
610 }
611 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
612
613 return 0;
614 out_free:
615 wil_vring_free(wil, vring, 1);
616 out:
617
618 return rc;
619}
620
621void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
622{
623 struct vring *vring = &wil->vring_tx[id];
624
625 if (!vring->va)
626 return;
627
628 wil_vring_free(wil, vring, 1);
629}
630
631static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
632 struct sk_buff *skb)
633{
634 struct vring *v = &wil->vring_tx[0];
635
636 if (v->va)
637 return v;
638
639 return NULL;
640}
641
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300642static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
643 int vring_index)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800644{
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300645 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800646 d->dma.ip_length = 0;
647 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
648 d->dma.b11 = 0/*14 | BIT(7)*/;
649 d->dma.error = 0;
650 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300651 d->dma.length = cpu_to_le16((u16)len);
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300652 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800653 d->mac.d[0] = 0;
654 d->mac.d[1] = 0;
655 d->mac.d[2] = 0;
656 d->mac.ucode_cmd = 0;
657 /* use dst index 0 */
658 d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
659 (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
660 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
661 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
662 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
663
664 return 0;
665}
666
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300667static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil,
668 struct vring_tx_desc *d,
669 struct sk_buff *skb)
670{
671 int protocol;
672
673 if (skb->ip_summed != CHECKSUM_PARTIAL)
674 return 0;
675
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +0200676 d->dma.b11 = ETH_HLEN; /* MAC header length */
677
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300678 switch (skb->protocol) {
679 case cpu_to_be16(ETH_P_IP):
680 protocol = ip_hdr(skb)->protocol;
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +0200681 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300682 break;
683 case cpu_to_be16(ETH_P_IPV6):
684 protocol = ipv6_hdr(skb)->nexthdr;
685 break;
686 default:
687 return -EINVAL;
688 }
689
690 switch (protocol) {
691 case IPPROTO_TCP:
692 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
693 /* L4 header len: TCP header length */
694 d->dma.d0 |=
695 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
696 break;
697 case IPPROTO_UDP:
698 /* L4 header len: UDP header length */
699 d->dma.d0 |=
700 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
701 break;
702 default:
703 return -EINVAL;
704 }
705
706 d->dma.ip_length = skb_network_header_len(skb);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300707 /* Enable TCP/UDP checksum */
708 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
709 /* Calculate pseudo-header */
710 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
711
712 return 0;
713}
714
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800715static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
716 struct sk_buff *skb)
717{
718 struct device *dev = wil_to_dev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300719 struct vring_tx_desc dd, *d = &dd;
720 volatile struct vring_tx_desc *_d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800721 u32 swhead = vring->swhead;
722 int avail = wil_vring_avail_tx(vring);
723 int nr_frags = skb_shinfo(skb)->nr_frags;
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300724 uint f = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800725 int vring_index = vring - wil->vring_tx;
726 uint i = swhead;
727 dma_addr_t pa;
728
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200729 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800730
731 if (avail < vring->size/8)
732 netif_tx_stop_all_queues(wil_to_ndev(wil));
733 if (avail < 1 + nr_frags) {
734 wil_err(wil, "Tx ring full. No space for %d fragments\n",
735 1 + nr_frags);
736 return -ENOMEM;
737 }
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300738 _d = &(vring->va[i].tx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800739
740 /* FIXME FW can accept only unicast frames for the peer */
741 memcpy(skb->data, wil->dst_addr[vring_index], ETH_ALEN);
742
743 pa = dma_map_single(dev, skb->data,
744 skb_headlen(skb), DMA_TO_DEVICE);
745
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200746 wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800747 skb->data, (unsigned long long)pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200748 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800749 skb->data, skb_headlen(skb), false);
750
751 if (unlikely(dma_mapping_error(dev, pa)))
752 return -EINVAL;
753 /* 1-st segment */
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300754 wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300755 /* Process TCP/UDP checksum offloading */
756 if (wil_tx_desc_offload_cksum_set(wil, d, skb)) {
757 wil_err(wil, "VRING #%d Failed to set cksum, drop packet\n",
758 vring_index);
759 goto dma_error;
760 }
761
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800762 d->mac.d[2] |= ((nr_frags + 1) <<
763 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300764 if (nr_frags)
765 *_d = *d;
766
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800767 /* middle segments */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300768 for (; f < nr_frags; f++) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800769 const struct skb_frag_struct *frag =
770 &skb_shinfo(skb)->frags[f];
771 int len = skb_frag_size(frag);
772 i = (swhead + f + 1) % vring->size;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300773 _d = &(vring->va[i].tx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800774 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
775 DMA_TO_DEVICE);
776 if (unlikely(dma_mapping_error(dev, pa)))
777 goto dma_error;
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300778 wil_tx_desc_map(d, pa, len, vring_index);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300779 vring->ctx[i].mapped_as_page = 1;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300780 *_d = *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800781 }
782 /* for the last seg only */
783 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
Kirshenbaum Erez668b2bb2013-06-23 12:59:35 +0300784 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800785 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300786 *_d = *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800787
Vladimir Kondratiev6cdadd42013-07-11 18:03:41 +0300788 /* hold reference to skb
789 * to prevent skb release before accounting
790 * in case of immediate "tx done"
791 */
792 vring->ctx[i].skb = skb_get(skb);
793
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200794 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800795 (const void *)d, sizeof(*d), false);
796
797 /* advance swhead */
798 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200799 wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300800 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800801 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800802
803 return 0;
804 dma_error:
805 /* unmap what we have mapped */
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +0300806 nr_frags = f + 1; /* frags mapped + one for skb head */
807 for (f = 0; f < nr_frags; f++) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300808 u16 dmalen;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +0300809 struct wil_ctx *ctx;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300810
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800811 i = (swhead + f) % vring->size;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +0300812 ctx = &vring->ctx[i];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300813 _d = &(vring->va[i].tx);
814 *d = *_d;
815 _d->dma.status = TX_DMA_STATUS_DU;
816 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300817 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300818 if (ctx->mapped_as_page)
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300819 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300820 else
821 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
822
823 if (ctx->skb)
824 dev_kfree_skb_any(ctx->skb);
825
826 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800827 }
828
829 return -EINVAL;
830}
831
832
833netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
834{
835 struct wil6210_priv *wil = ndev_to_wil(ndev);
836 struct vring *vring;
837 int rc;
838
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200839 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800840 if (!test_bit(wil_status_fwready, &wil->status)) {
841 wil_err(wil, "FW not ready\n");
842 goto drop;
843 }
844 if (!test_bit(wil_status_fwconnected, &wil->status)) {
845 wil_err(wil, "FW not connected\n");
846 goto drop;
847 }
848 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
849 wil_err(wil, "Xmit in monitor mode not supported\n");
850 goto drop;
851 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +0300852
853 /* find vring */
854 vring = wil_find_tx_vring(wil, skb);
855 if (!vring) {
856 wil_err(wil, "No Tx VRING available\n");
857 goto drop;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800858 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +0300859 /* set up vring entry */
860 rc = wil_tx_vring(wil, vring, skb);
861
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800862 switch (rc) {
863 case 0:
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200864 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800865 dev_kfree_skb_any(skb);
866 return NETDEV_TX_OK;
867 case -ENOMEM:
868 return NETDEV_TX_BUSY;
869 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +0200870 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800871 }
872 drop:
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800873 ndev->stats.tx_dropped++;
874 dev_kfree_skb_any(skb);
875
876 return NET_XMIT_DROP;
877}
878
879/**
880 * Clean up transmitted skb's from the Tx VRING
881 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300882 * Return number of descriptors cleared
883 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800884 * Safe to call from IRQ
885 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300886int wil_tx_complete(struct wil6210_priv *wil, int ringid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800887{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200888 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800889 struct device *dev = wil_to_dev(wil);
890 struct vring *vring = &wil->vring_tx[ringid];
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300891 int done = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800892
893 if (!vring->va) {
894 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300895 return 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800896 }
897
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200898 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800899
900 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300901 volatile struct vring_tx_desc *_d =
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300902 &vring->va[vring->swtail].tx;
903 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800904 dma_addr_t pa;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300905 u16 dmalen;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300906 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
907 struct sk_buff *skb = ctx->skb;
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300908
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300909 *d = *_d;
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300910
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800911 if (!(d->dma.status & TX_DMA_STATUS_DU))
912 break;
913
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300914 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300915 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
916 d->dma.error);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200917 wil_dbg_txrx(wil,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800918 "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300919 vring->swtail, dmalen, d->dma.status,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800920 d->dma.error);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200921 wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800922 (const void *)d, sizeof(*d), false);
923
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300924 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300925 if (ctx->mapped_as_page)
926 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
927 else
928 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
929
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800930 if (skb) {
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200931 if (d->dma.error == 0) {
932 ndev->stats.tx_packets++;
933 ndev->stats.tx_bytes += skb->len;
934 } else {
935 ndev->stats.tx_errors++;
936 }
937
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800938 dev_kfree_skb_any(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800939 }
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300940 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev03269c62013-07-11 18:03:39 +0300941 /*
942 * There is no need to touch HW descriptor:
943 * - ststus bit TX_DMA_STATUS_DU is set by design,
944 * so hardware will not try to process this desc.,
945 * - rest of descriptor will be initialized on Tx.
946 */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800947 vring->swtail = wil_vring_next_tail(vring);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300948 done++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800949 }
950 if (wil_vring_avail_tx(vring) > vring->size/4)
951 netif_tx_wake_all_queues(wil_to_ndev(wil));
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300952
953 return done;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800954}