blob: eb60023fa21704f13fcb9a2416ee5bb42a65862f [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 Kondratiev0786dc42014-01-14 20:31:26 +020024#include <linux/prefetch.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080025
26#include "wil6210.h"
27#include "wmi.h"
28#include "txrx.h"
Vladimir Kondratiev98658092013-05-12 14:43:35 +030029#include "trace.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080030
31static bool rtap_include_phy_info;
32module_param(rtap_include_phy_info, bool, S_IRUGO);
33MODULE_PARM_DESC(rtap_include_phy_info,
34 " Include PHY info in the radiotap header, default - no");
35
36static inline int wil_vring_is_empty(struct vring *vring)
37{
38 return vring->swhead == vring->swtail;
39}
40
41static inline u32 wil_vring_next_tail(struct vring *vring)
42{
43 return (vring->swtail + 1) % vring->size;
44}
45
46static inline void wil_vring_advance_head(struct vring *vring, int n)
47{
48 vring->swhead = (vring->swhead + n) % vring->size;
49}
50
51static inline int wil_vring_is_full(struct vring *vring)
52{
53 return wil_vring_next_tail(vring) == vring->swhead;
54}
55/*
56 * Available space in Tx Vring
57 */
58static inline int wil_vring_avail_tx(struct vring *vring)
59{
60 u32 swhead = vring->swhead;
61 u32 swtail = vring->swtail;
62 int used = (vring->size + swhead - swtail) % vring->size;
63
64 return vring->size - used - 1;
65}
66
67static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
68{
69 struct device *dev = wil_to_dev(wil);
70 size_t sz = vring->size * sizeof(vring->va[0]);
71 uint i;
72
73 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
74
75 vring->swhead = 0;
76 vring->swtail = 0;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +030077 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080078 if (!vring->ctx) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080079 vring->va = NULL;
80 return -ENOMEM;
81 }
82 /*
83 * vring->va should be aligned on its size rounded up to power of 2
84 * This is granted by the dma_alloc_coherent
85 */
86 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
87 if (!vring->va) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080088 kfree(vring->ctx);
89 vring->ctx = NULL;
90 return -ENOMEM;
91 }
92 /* initially, all descriptors are SW owned
93 * For Tx and Rx, ownership bit is at the same location, thus
94 * we can use any
95 */
96 for (i = 0; i < vring->size; i++) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +030097 volatile struct vring_tx_desc *_d = &(vring->va[i].tx);
98 _d->dma.status = TX_DMA_STATUS_DU;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080099 }
100
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200101 wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size,
Vladimir Kondratiev2057ebb2013-01-28 18:30:58 +0200102 vring->va, (unsigned long long)vring->pa, vring->ctx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800103
104 return 0;
105}
106
107static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
108 int tx)
109{
110 struct device *dev = wil_to_dev(wil);
111 size_t sz = vring->size * sizeof(vring->va[0]);
112
113 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300114 dma_addr_t pa;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300115 u16 dmalen;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300116 struct wil_ctx *ctx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300117
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800118 if (tx) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300119 struct vring_tx_desc dd, *d = &dd;
120 volatile struct vring_tx_desc *_d =
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800121 &vring->va[vring->swtail].tx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300122
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300123 ctx = &vring->ctx[vring->swtail];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300124 *d = *_d;
125 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300126 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300127 if (vring->ctx[vring->swtail].mapped_as_page) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300128 dma_unmap_page(dev, pa, dmalen,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800129 DMA_TO_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300130 } else {
131 dma_unmap_single(dev, pa, dmalen,
132 DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800133 }
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300134 if (ctx->skb)
135 dev_kfree_skb_any(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800136 vring->swtail = wil_vring_next_tail(vring);
137 } else { /* rx */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300138 struct vring_rx_desc dd, *d = &dd;
139 volatile struct vring_rx_desc *_d =
Vladimir Kondratiev4d1ac072013-07-11 18:03:38 +0300140 &vring->va[vring->swhead].rx;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300141
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300142 ctx = &vring->ctx[vring->swhead];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300143 *d = *_d;
144 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300145 dmalen = le16_to_cpu(d->dma.length);
146 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300147 kfree_skb(ctx->skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800148 wil_vring_advance_head(vring, 1);
149 }
150 }
151 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
152 kfree(vring->ctx);
153 vring->pa = 0;
154 vring->va = NULL;
155 vring->ctx = NULL;
156}
157
158/**
159 * Allocate one skb for Rx VRING
160 *
161 * Safe to call from IRQ
162 */
163static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
164 u32 i, int headroom)
165{
166 struct device *dev = wil_to_dev(wil);
167 unsigned int sz = RX_BUF_LEN;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300168 struct vring_rx_desc dd, *d = &dd;
169 volatile struct vring_rx_desc *_d = &(vring->va[i].rx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800170 dma_addr_t pa;
171
172 /* TODO align */
173 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
174 if (unlikely(!skb))
175 return -ENOMEM;
176
177 skb_reserve(skb, headroom);
178 skb_put(skb, sz);
179
180 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
181 if (unlikely(dma_mapping_error(dev, pa))) {
182 kfree_skb(skb);
183 return -ENOMEM;
184 }
185
186 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300187 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800188 /* ip_length don't care */
189 /* b11 don't care */
190 /* error don't care */
191 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300192 d->dma.length = cpu_to_le16(sz);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300193 *_d = *d;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300194 vring->ctx[i].skb = skb;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800195
196 return 0;
197}
198
199/**
200 * Adds radiotap header
201 *
202 * Any error indicated as "Bad FCS"
203 *
204 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
205 * - Rx descriptor: 32 bytes
206 * - Phy info
207 */
208static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300209 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800210{
211 struct wireless_dev *wdev = wil->wdev;
212 struct wil6210_rtap {
213 struct ieee80211_radiotap_header rthdr;
214 /* fields should be in the order of bits in rthdr.it_present */
215 /* flags */
216 u8 flags;
217 /* channel */
218 __le16 chnl_freq __aligned(2);
219 __le16 chnl_flags;
220 /* MCS */
221 u8 mcs_present;
222 u8 mcs_flags;
223 u8 mcs_index;
224 } __packed;
225 struct wil6210_rtap_vendor {
226 struct wil6210_rtap rtap;
227 /* vendor */
228 u8 vendor_oui[3] __aligned(2);
229 u8 vendor_ns;
230 __le16 vendor_skip;
231 u8 vendor_data[0];
232 } __packed;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300233 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800234 struct wil6210_rtap_vendor *rtap_vendor;
235 int rtap_len = sizeof(struct wil6210_rtap);
236 int phy_length = 0; /* phy info header size, bytes */
237 static char phy_data[128];
238 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
239
240 if (rtap_include_phy_info) {
241 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
242 /* calculate additional length */
243 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
244 /**
245 * PHY info starts from 8-byte boundary
246 * there are 8-byte lines, last line may be partially
247 * written (HW bug), thus FW configures for last line
248 * to be excessive. Driver skips this last line.
249 */
250 int len = min_t(int, 8 + sizeof(phy_data),
251 wil_rxdesc_phy_length(d));
252 if (len > 8) {
253 void *p = skb_tail_pointer(skb);
254 void *pa = PTR_ALIGN(p, 8);
255 if (skb_tailroom(skb) >= len + (pa - p)) {
256 phy_length = len - 8;
257 memcpy(phy_data, pa, phy_length);
258 }
259 }
260 }
261 rtap_len += phy_length;
262 }
263
264 if (skb_headroom(skb) < rtap_len &&
265 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
266 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
267 return;
268 }
269
270 rtap_vendor = (void *)skb_push(skb, rtap_len);
271 memset(rtap_vendor, 0, rtap_len);
272
273 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
274 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
275 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
276 (1 << IEEE80211_RADIOTAP_FLAGS) |
277 (1 << IEEE80211_RADIOTAP_CHANNEL) |
278 (1 << IEEE80211_RADIOTAP_MCS));
279 if (d->dma.status & RX_DMA_STATUS_ERROR)
280 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
281
282 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
283 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
284
285 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
286 rtap_vendor->rtap.mcs_flags = 0;
287 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
288
289 if (rtap_include_phy_info) {
290 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
291 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
292 /* OUI for Wilocity 04:ce:14 */
293 rtap_vendor->vendor_oui[0] = 0x04;
294 rtap_vendor->vendor_oui[1] = 0xce;
295 rtap_vendor->vendor_oui[2] = 0x14;
296 rtap_vendor->vendor_ns = 1;
297 /* Rx descriptor + PHY data */
298 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
299 phy_length);
300 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
301 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
302 phy_length);
303 }
304}
305
306/*
307 * Fast swap in place between 2 registers
308 */
309static void wil_swap_u16(u16 *a, u16 *b)
310{
311 *a ^= *b;
312 *b ^= *a;
313 *a ^= *b;
314}
315
316static void wil_swap_ethaddr(void *data)
317{
318 struct ethhdr *eth = data;
319 u16 *s = (u16 *)eth->h_source;
320 u16 *d = (u16 *)eth->h_dest;
321
322 wil_swap_u16(s++, d++);
323 wil_swap_u16(s++, d++);
324 wil_swap_u16(s, d);
325}
326
327/**
328 * reap 1 frame from @swhead
329 *
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300330 * Rx descriptor copied to skb->cb
331 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800332 * Safe to call from IRQ
333 */
334static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
335 struct vring *vring)
336{
337 struct device *dev = wil_to_dev(wil);
338 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300339 volatile struct vring_rx_desc *_d;
340 struct vring_rx_desc *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800341 struct sk_buff *skb;
342 dma_addr_t pa;
343 unsigned int sz = RX_BUF_LEN;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300344 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800345 u8 ftype;
346 u8 ds_bits;
347
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300348 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
349
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800350 if (wil_vring_is_empty(vring))
351 return NULL;
352
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300353 _d = &(vring->va[vring->swhead].rx);
354 if (!(_d->dma.status & RX_DMA_STATUS_DU)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800355 /* it is not error, we just reached end of Rx done area */
356 return NULL;
357 }
358
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300359 skb = vring->ctx[vring->swhead].skb;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300360 d = wil_skb_rxdesc(skb);
361 *d = *_d;
362 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300363 vring->ctx[vring->swhead].skb = NULL;
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300364 wil_vring_advance_head(vring, 1);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300365
366 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
367 dmalen = le16_to_cpu(d->dma.length);
368
369 trace_wil6210_rx(vring->swhead, d);
370 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen);
371 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
372 (const void *)d, sizeof(*d), false);
373
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300374 if (dmalen > sz) {
375 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
Wei Yongjun110dea02013-05-23 17:10:43 +0800376 kfree_skb(skb);
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300377 return NULL;
378 }
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300379 skb_trim(skb, dmalen);
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300380
Vladimir Kondratiev1cbbcb02014-01-08 11:50:49 +0200381 prefetch(skb->data);
382
Vladimir Kondratievc0d37712013-05-12 14:43:34 +0300383 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
384 skb->data, skb_headlen(skb), false);
385
386
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300387 wil->stats.last_mcs_rx = wil_rxdesc_mcs(d);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800388
389 /* use radiotap header only if required */
390 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300391 wil_rx_add_radiotap_header(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800392
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800393 /* no extra checks if in sniffer mode */
394 if (ndev->type != ARPHRD_ETHER)
395 return skb;
396 /*
397 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
398 * Driver should recognize it by frame type, that is found
399 * in Rx descriptor. If type is not data, it is 802.11 frame as is
400 */
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300401 ftype = wil_rxdesc_ftype(d) << 2;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800402 if (ftype != IEEE80211_FTYPE_DATA) {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200403 wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800404 /* TODO: process it */
405 kfree_skb(skb);
406 return NULL;
407 }
408
409 if (skb->len < ETH_HLEN) {
410 wil_err(wil, "Short frame, len = %d\n", skb->len);
411 /* TODO: process it (i.e. BAR) */
412 kfree_skb(skb);
413 return NULL;
414 }
415
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300416 /* L4 IDENT is on when HW calculated checksum, check status
417 * and in case of error drop the packet
418 * higher stack layers will handle retransmission (if required)
419 */
420 if (d->dma.status & RX_DMA_STATUS_L4_IDENT) {
421 /* L4 protocol identified, csum calculated */
Vladimir Kondratiev4a68ab102013-08-13 15:25:32 +0300422 if ((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0)
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300423 skb->ip_summed = CHECKSUM_UNNECESSARY;
Vladimir Kondratiev4a68ab102013-08-13 15:25:32 +0300424 /* If HW reports bad checksum, let IP stack re-check it
425 * For example, HW don't understand Microsoft IP stack that
426 * mis-calculates TCP checksum - if it should be 0x0,
427 * it writes 0xffff in violation of RFC 1624
428 */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300429 }
430
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300431 ds_bits = wil_rxdesc_ds_bits(d);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800432 if (ds_bits == 1) {
433 /*
434 * HW bug - in ToDS mode, i.e. Rx on AP side,
435 * addresses get swapped
436 */
437 wil_swap_ethaddr(skb->data);
438 }
439
440 return skb;
441}
442
443/**
444 * allocate and fill up to @count buffers in rx ring
445 * buffers posted at @swtail
446 */
447static int wil_rx_refill(struct wil6210_priv *wil, int count)
448{
449 struct net_device *ndev = wil_to_ndev(wil);
450 struct vring *v = &wil->vring_rx;
451 u32 next_tail;
452 int rc = 0;
453 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
454 WIL6210_RTAP_SIZE : 0;
455
456 for (; next_tail = wil_vring_next_tail(v),
457 (next_tail != v->swhead) && (count-- > 0);
458 v->swtail = next_tail) {
459 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
460 if (rc) {
461 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
462 rc, v->swtail);
463 break;
464 }
465 }
466 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
467
468 return rc;
469}
470
471/*
472 * Pass Rx packet to the netif. Update statistics.
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300473 * Called in softirq context (NAPI poll).
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800474 */
475static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
476{
477 int rc;
478 unsigned int len = skb->len;
479
Vladimir Kondratiev241804c2013-01-28 18:31:02 +0200480 skb_orphan(skb);
481
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300482 rc = netif_receive_skb(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800483
484 if (likely(rc == NET_RX_SUCCESS)) {
485 ndev->stats.rx_packets++;
486 ndev->stats.rx_bytes += len;
487
488 } else {
489 ndev->stats.rx_dropped++;
490 }
491}
492
493/**
494 * Proceed all completed skb's from Rx VRING
495 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300496 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800497 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300498void wil_rx_handle(struct wil6210_priv *wil, int *quota)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800499{
500 struct net_device *ndev = wil_to_ndev(wil);
501 struct vring *v = &wil->vring_rx;
502 struct sk_buff *skb;
503
504 if (!v->va) {
505 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
506 return;
507 }
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200508 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300509 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
510 (*quota)--;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800511
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800512 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
513 skb->dev = ndev;
514 skb_reset_mac_header(skb);
515 skb->ip_summed = CHECKSUM_UNNECESSARY;
516 skb->pkt_type = PACKET_OTHERHOST;
517 skb->protocol = htons(ETH_P_802_2);
518
519 } else {
520 skb->protocol = eth_type_trans(skb, ndev);
521 }
522
523 wil_netif_rx_any(skb, ndev);
524 }
525 wil_rx_refill(wil, v->size);
526}
527
528int wil_rx_init(struct wil6210_priv *wil)
529{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800530 struct vring *vring = &wil->vring_rx;
531 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800532
533 vring->size = WIL6210_RX_RING_SIZE;
534 rc = wil_vring_alloc(wil, vring);
535 if (rc)
536 return rc;
537
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +0200538 rc = wmi_rx_chain_add(wil, vring);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800539 if (rc)
540 goto err_free;
541
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800542 rc = wil_rx_refill(wil, vring->size);
543 if (rc)
544 goto err_free;
545
546 return 0;
547 err_free:
548 wil_vring_free(wil, vring, 0);
549
550 return rc;
551}
552
553void wil_rx_fini(struct wil6210_priv *wil)
554{
555 struct vring *vring = &wil->vring_rx;
556
Vladimir Kondratiev2acb4222013-01-28 18:31:08 +0200557 if (vring->va)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800558 wil_vring_free(wil, vring, 0);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800559}
560
561int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
562 int cid, int tid)
563{
564 int rc;
565 struct wmi_vring_cfg_cmd cmd = {
566 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
567 .vring_cfg = {
568 .tx_sw_ring = {
569 .max_mpdu_size = cpu_to_le16(TX_BUF_LEN),
Vladimir Kondratievb5d98e92013-04-18 14:33:51 +0300570 .ring_size = cpu_to_le16(size),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800571 },
572 .ringid = id,
573 .cidxtid = (cid & 0xf) | ((tid & 0xf) << 4),
574 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
575 .mac_ctrl = 0,
576 .to_resolution = 0,
577 .agg_max_wsize = 16,
578 .schd_params = {
579 .priority = cpu_to_le16(0),
580 .timeslot_us = cpu_to_le16(0xfff),
581 },
582 },
583 };
584 struct {
585 struct wil6210_mbox_hdr_wmi wmi;
586 struct wmi_vring_cfg_done_event cmd;
587 } __packed reply;
588 struct vring *vring = &wil->vring_tx[id];
589
590 if (vring->va) {
591 wil_err(wil, "Tx ring [%d] already allocated\n", id);
592 rc = -EINVAL;
593 goto out;
594 }
595
596 vring->size = size;
597 rc = wil_vring_alloc(wil, vring);
598 if (rc)
599 goto out;
600
601 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800602
603 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
604 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
605 if (rc)
606 goto out_free;
607
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200608 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800609 wil_err(wil, "Tx config failed, status 0x%02x\n",
610 reply.cmd.status);
Vladimir Kondratievc3319972013-01-28 18:31:09 +0200611 rc = -EINVAL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800612 goto out_free;
613 }
614 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
615
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200616 wil->vring2cid_tid[id][0] = cid;
617 wil->vring2cid_tid[id][1] = tid;
618
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800619 return 0;
620 out_free:
621 wil_vring_free(wil, vring, 1);
622 out:
623
624 return rc;
625}
626
627void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
628{
629 struct vring *vring = &wil->vring_tx[id];
630
631 if (!vring->va)
632 return;
633
634 wil_vring_free(wil, vring, 1);
635}
636
637static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
638 struct sk_buff *skb)
639{
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200640 int i;
641 struct ethhdr *eth = (void *)skb->data;
642 int cid = wil_find_cid(wil, eth->h_dest);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800643
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200644 if (cid < 0)
645 return NULL;
646
647 /* TODO: fix for multiple TID */
648 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
649 if (wil->vring2cid_tid[i][0] == cid) {
650 struct vring *v = &wil->vring_tx[i];
651 wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n",
652 __func__, eth->h_dest, i);
653 if (v->va) {
654 return v;
655 } else {
656 wil_dbg_txrx(wil, "vring[%d] not valid\n", i);
657 return NULL;
658 }
659 }
660 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800661
662 return NULL;
663}
664
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300665static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
666 int vring_index)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800667{
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300668 wil_desc_addr_set(&d->dma.addr, pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800669 d->dma.ip_length = 0;
670 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
671 d->dma.b11 = 0/*14 | BIT(7)*/;
672 d->dma.error = 0;
673 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300674 d->dma.length = cpu_to_le16((u16)len);
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300675 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800676 d->mac.d[0] = 0;
677 d->mac.d[1] = 0;
678 d->mac.d[2] = 0;
679 d->mac.ucode_cmd = 0;
680 /* use dst index 0 */
681 d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
682 (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
683 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
684 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
685 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
686
687 return 0;
688}
689
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300690static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil,
691 struct vring_tx_desc *d,
692 struct sk_buff *skb)
693{
694 int protocol;
695
696 if (skb->ip_summed != CHECKSUM_PARTIAL)
697 return 0;
698
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +0200699 d->dma.b11 = ETH_HLEN; /* MAC header length */
700
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300701 switch (skb->protocol) {
702 case cpu_to_be16(ETH_P_IP):
703 protocol = ip_hdr(skb)->protocol;
Vladimir Kondratievdf2d08e2014-01-08 11:50:48 +0200704 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300705 break;
706 case cpu_to_be16(ETH_P_IPV6):
707 protocol = ipv6_hdr(skb)->nexthdr;
708 break;
709 default:
710 return -EINVAL;
711 }
712
713 switch (protocol) {
714 case IPPROTO_TCP:
715 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
716 /* L4 header len: TCP header length */
717 d->dma.d0 |=
718 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
719 break;
720 case IPPROTO_UDP:
721 /* L4 header len: UDP header length */
722 d->dma.d0 |=
723 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
724 break;
725 default:
726 return -EINVAL;
727 }
728
729 d->dma.ip_length = skb_network_header_len(skb);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300730 /* Enable TCP/UDP checksum */
731 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
732 /* Calculate pseudo-header */
733 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
734
735 return 0;
736}
737
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800738static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
739 struct sk_buff *skb)
740{
741 struct device *dev = wil_to_dev(wil);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300742 struct vring_tx_desc dd, *d = &dd;
743 volatile struct vring_tx_desc *_d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800744 u32 swhead = vring->swhead;
745 int avail = wil_vring_avail_tx(vring);
746 int nr_frags = skb_shinfo(skb)->nr_frags;
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300747 uint f = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800748 int vring_index = vring - wil->vring_tx;
749 uint i = swhead;
750 dma_addr_t pa;
751
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200752 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800753
754 if (avail < vring->size/8)
755 netif_tx_stop_all_queues(wil_to_ndev(wil));
756 if (avail < 1 + nr_frags) {
757 wil_err(wil, "Tx ring full. No space for %d fragments\n",
758 1 + nr_frags);
759 return -ENOMEM;
760 }
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300761 _d = &(vring->va[i].tx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800762
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800763 pa = dma_map_single(dev, skb->data,
764 skb_headlen(skb), DMA_TO_DEVICE);
765
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200766 wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800767 skb->data, (unsigned long long)pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200768 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800769 skb->data, skb_headlen(skb), false);
770
771 if (unlikely(dma_mapping_error(dev, pa)))
772 return -EINVAL;
773 /* 1-st segment */
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300774 wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index);
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300775 /* Process TCP/UDP checksum offloading */
776 if (wil_tx_desc_offload_cksum_set(wil, d, skb)) {
777 wil_err(wil, "VRING #%d Failed to set cksum, drop packet\n",
778 vring_index);
779 goto dma_error;
780 }
781
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800782 d->mac.d[2] |= ((nr_frags + 1) <<
783 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300784 if (nr_frags)
785 *_d = *d;
786
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800787 /* middle segments */
Kirshenbaum Erez504937d2013-07-21 11:34:37 +0300788 for (; f < nr_frags; f++) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800789 const struct skb_frag_struct *frag =
790 &skb_shinfo(skb)->frags[f];
791 int len = skb_frag_size(frag);
792 i = (swhead + f + 1) % vring->size;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300793 _d = &(vring->va[i].tx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800794 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
795 DMA_TO_DEVICE);
796 if (unlikely(dma_mapping_error(dev, pa)))
797 goto dma_error;
Kirshenbaum Erez99b55bd2013-06-23 12:59:34 +0300798 wil_tx_desc_map(d, pa, len, vring_index);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300799 vring->ctx[i].mapped_as_page = 1;
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300800 *_d = *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800801 }
802 /* for the last seg only */
803 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
Kirshenbaum Erez668b2bb2013-06-23 12:59:35 +0300804 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800805 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300806 *_d = *d;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800807
Vladimir Kondratiev6cdadd42013-07-11 18:03:41 +0300808 /* hold reference to skb
809 * to prevent skb release before accounting
810 * in case of immediate "tx done"
811 */
812 vring->ctx[i].skb = skb_get(skb);
813
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200814 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800815 (const void *)d, sizeof(*d), false);
816
817 /* advance swhead */
818 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200819 wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300820 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800821 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800822
823 return 0;
824 dma_error:
825 /* unmap what we have mapped */
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +0300826 nr_frags = f + 1; /* frags mapped + one for skb head */
827 for (f = 0; f < nr_frags; f++) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300828 u16 dmalen;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +0300829 struct wil_ctx *ctx;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300830
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800831 i = (swhead + f) % vring->size;
Vladimir Kondratievc2a146f2013-07-21 11:34:36 +0300832 ctx = &vring->ctx[i];
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300833 _d = &(vring->va[i].tx);
834 *d = *_d;
835 _d->dma.status = TX_DMA_STATUS_DU;
836 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300837 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300838 if (ctx->mapped_as_page)
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300839 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300840 else
841 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
842
843 if (ctx->skb)
844 dev_kfree_skb_any(ctx->skb);
845
846 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800847 }
848
849 return -EINVAL;
850}
851
852
853netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
854{
855 struct wil6210_priv *wil = ndev_to_wil(ndev);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200856 struct ethhdr *eth = (void *)skb->data;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800857 struct vring *vring;
858 int rc;
859
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200860 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800861 if (!test_bit(wil_status_fwready, &wil->status)) {
862 wil_err(wil, "FW not ready\n");
863 goto drop;
864 }
865 if (!test_bit(wil_status_fwconnected, &wil->status)) {
866 wil_err(wil, "FW not connected\n");
867 goto drop;
868 }
869 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
870 wil_err(wil, "Xmit in monitor mode not supported\n");
871 goto drop;
872 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +0300873
874 /* find vring */
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200875 if (is_unicast_ether_addr(eth->h_dest)) {
876 vring = wil_find_tx_vring(wil, skb);
877 } else {
878 int i = 0;
879 /* TODO: duplicate for all CID's */
880 vring = &wil->vring_tx[i];
881 if (vring->va) {
882 int cid = wil->vring2cid_tid[i][0];
883 /* FIXME FW can accept only unicast frames */
884 memcpy(skb->data, wil->sta[cid].addr, ETH_ALEN);
885 } else {
886 vring = NULL;
887 }
888 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +0300889 if (!vring) {
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200890 wil_err(wil, "No Tx VRING found for %pM\n", eth->h_dest);
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +0300891 goto drop;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800892 }
Vladimir Kondratievd58db4e2013-06-09 09:12:54 +0300893 /* set up vring entry */
894 rc = wil_tx_vring(wil, vring, skb);
895
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800896 switch (rc) {
897 case 0:
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200898 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800899 dev_kfree_skb_any(skb);
900 return NETDEV_TX_OK;
901 case -ENOMEM:
902 return NETDEV_TX_BUSY;
903 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +0200904 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800905 }
906 drop:
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800907 ndev->stats.tx_dropped++;
908 dev_kfree_skb_any(skb);
909
910 return NET_XMIT_DROP;
911}
912
913/**
914 * Clean up transmitted skb's from the Tx VRING
915 *
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300916 * Return number of descriptors cleared
917 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800918 * Safe to call from IRQ
919 */
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300920int wil_tx_complete(struct wil6210_priv *wil, int ringid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800921{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200922 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800923 struct device *dev = wil_to_dev(wil);
924 struct vring *vring = &wil->vring_tx[ringid];
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300925 int done = 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800926
927 if (!vring->va) {
928 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300929 return 0;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800930 }
931
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200932 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800933
934 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300935 volatile struct vring_tx_desc *_d =
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300936 &vring->va[vring->swtail].tx;
937 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800938 dma_addr_t pa;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300939 u16 dmalen;
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300940 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
941 struct sk_buff *skb = ctx->skb;
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300942
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300943 *d = *_d;
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300944
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800945 if (!(d->dma.status & TX_DMA_STATUS_DU))
946 break;
947
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300948 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300949 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
950 d->dma.error);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200951 wil_dbg_txrx(wil,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800952 "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300953 vring->swtail, dmalen, d->dma.status,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800954 d->dma.error);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200955 wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800956 (const void *)d, sizeof(*d), false);
957
Vladimir Kondratiev68ada712013-05-12 14:43:37 +0300958 pa = wil_desc_addr(&d->dma.addr);
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300959 if (ctx->mapped_as_page)
960 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
961 else
962 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
963
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800964 if (skb) {
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200965 if (d->dma.error == 0) {
966 ndev->stats.tx_packets++;
967 ndev->stats.tx_bytes += skb->len;
968 } else {
969 ndev->stats.tx_errors++;
970 }
971
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800972 dev_kfree_skb_any(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800973 }
Vladimir Kondratievf88f1132013-07-11 18:03:40 +0300974 memset(ctx, 0, sizeof(*ctx));
Vladimir Kondratiev03269c62013-07-11 18:03:39 +0300975 /*
976 * There is no need to touch HW descriptor:
977 * - ststus bit TX_DMA_STATUS_DU is set by design,
978 * so hardware will not try to process this desc.,
979 * - rest of descriptor will be initialized on Tx.
980 */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800981 vring->swtail = wil_vring_next_tail(vring);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300982 done++;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800983 }
984 if (wil_vring_avail_tx(vring) > vring->size/4)
985 netif_tx_wake_all_queues(wil_to_ndev(wil));
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300986
987 return done;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800988}