blob: dc183d573c08920b0ea763acfe115e91837546f7 [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>
21
22#include "wil6210.h"
23#include "wmi.h"
24#include "txrx.h"
Vladimir Kondratiev98658092013-05-12 14:43:35 +030025#include "trace.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080026
27static bool rtap_include_phy_info;
28module_param(rtap_include_phy_info, bool, S_IRUGO);
29MODULE_PARM_DESC(rtap_include_phy_info,
30 " Include PHY info in the radiotap header, default - no");
31
32static inline int wil_vring_is_empty(struct vring *vring)
33{
34 return vring->swhead == vring->swtail;
35}
36
37static inline u32 wil_vring_next_tail(struct vring *vring)
38{
39 return (vring->swtail + 1) % vring->size;
40}
41
42static inline void wil_vring_advance_head(struct vring *vring, int n)
43{
44 vring->swhead = (vring->swhead + n) % vring->size;
45}
46
47static inline int wil_vring_is_full(struct vring *vring)
48{
49 return wil_vring_next_tail(vring) == vring->swhead;
50}
51/*
52 * Available space in Tx Vring
53 */
54static inline int wil_vring_avail_tx(struct vring *vring)
55{
56 u32 swhead = vring->swhead;
57 u32 swtail = vring->swtail;
58 int used = (vring->size + swhead - swtail) % vring->size;
59
60 return vring->size - used - 1;
61}
62
63static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
64{
65 struct device *dev = wil_to_dev(wil);
66 size_t sz = vring->size * sizeof(vring->va[0]);
67 uint i;
68
69 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
70
71 vring->swhead = 0;
72 vring->swtail = 0;
73 vring->ctx = kzalloc(vring->size * sizeof(vring->ctx[0]), GFP_KERNEL);
74 if (!vring->ctx) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080075 vring->va = NULL;
76 return -ENOMEM;
77 }
78 /*
79 * vring->va should be aligned on its size rounded up to power of 2
80 * This is granted by the dma_alloc_coherent
81 */
82 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
83 if (!vring->va) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080084 kfree(vring->ctx);
85 vring->ctx = NULL;
86 return -ENOMEM;
87 }
88 /* initially, all descriptors are SW owned
89 * For Tx and Rx, ownership bit is at the same location, thus
90 * we can use any
91 */
92 for (i = 0; i < vring->size; i++) {
93 volatile struct vring_tx_desc *d = &(vring->va[i].tx);
94 d->dma.status = TX_DMA_STATUS_DU;
95 }
96
Vladimir Kondratiev77438822013-01-28 18:31:06 +020097 wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size,
Vladimir Kondratiev2057ebb2013-01-28 18:30:58 +020098 vring->va, (unsigned long long)vring->pa, vring->ctx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080099
100 return 0;
101}
102
103static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
104 int tx)
105{
106 struct device *dev = wil_to_dev(wil);
107 size_t sz = vring->size * sizeof(vring->va[0]);
108
109 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300110 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800111 if (tx) {
112 volatile struct vring_tx_desc *d =
113 &vring->va[vring->swtail].tx;
114 dma_addr_t pa = d->dma.addr_low |
115 ((u64)d->dma.addr_high << 32);
116 struct sk_buff *skb = vring->ctx[vring->swtail];
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300117 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800118 if (skb) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300119 dma_unmap_single(dev, pa, dmalen,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800120 DMA_TO_DEVICE);
121 dev_kfree_skb_any(skb);
122 vring->ctx[vring->swtail] = NULL;
123 } else {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300124 dma_unmap_page(dev, pa, dmalen,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800125 DMA_TO_DEVICE);
126 }
127 vring->swtail = wil_vring_next_tail(vring);
128 } else { /* rx */
129 volatile struct vring_rx_desc *d =
130 &vring->va[vring->swtail].rx;
131 dma_addr_t pa = d->dma.addr_low |
132 ((u64)d->dma.addr_high << 32);
133 struct sk_buff *skb = vring->ctx[vring->swhead];
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300134 dmalen = le16_to_cpu(d->dma.length);
135 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800136 kfree_skb(skb);
137 wil_vring_advance_head(vring, 1);
138 }
139 }
140 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
141 kfree(vring->ctx);
142 vring->pa = 0;
143 vring->va = NULL;
144 vring->ctx = NULL;
145}
146
147/**
148 * Allocate one skb for Rx VRING
149 *
150 * Safe to call from IRQ
151 */
152static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
153 u32 i, int headroom)
154{
155 struct device *dev = wil_to_dev(wil);
156 unsigned int sz = RX_BUF_LEN;
157 volatile struct vring_rx_desc *d = &(vring->va[i].rx);
158 dma_addr_t pa;
159
160 /* TODO align */
161 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
162 if (unlikely(!skb))
163 return -ENOMEM;
164
165 skb_reserve(skb, headroom);
166 skb_put(skb, sz);
167
168 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
169 if (unlikely(dma_mapping_error(dev, pa))) {
170 kfree_skb(skb);
171 return -ENOMEM;
172 }
173
174 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
175 d->dma.addr_low = lower_32_bits(pa);
176 d->dma.addr_high = (u16)upper_32_bits(pa);
177 /* ip_length don't care */
178 /* b11 don't care */
179 /* error don't care */
180 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300181 d->dma.length = cpu_to_le16(sz);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800182 vring->ctx[i] = skb;
183
184 return 0;
185}
186
187/**
188 * Adds radiotap header
189 *
190 * Any error indicated as "Bad FCS"
191 *
192 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
193 * - Rx descriptor: 32 bytes
194 * - Phy info
195 */
196static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300197 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800198{
199 struct wireless_dev *wdev = wil->wdev;
200 struct wil6210_rtap {
201 struct ieee80211_radiotap_header rthdr;
202 /* fields should be in the order of bits in rthdr.it_present */
203 /* flags */
204 u8 flags;
205 /* channel */
206 __le16 chnl_freq __aligned(2);
207 __le16 chnl_flags;
208 /* MCS */
209 u8 mcs_present;
210 u8 mcs_flags;
211 u8 mcs_index;
212 } __packed;
213 struct wil6210_rtap_vendor {
214 struct wil6210_rtap rtap;
215 /* vendor */
216 u8 vendor_oui[3] __aligned(2);
217 u8 vendor_ns;
218 __le16 vendor_skip;
219 u8 vendor_data[0];
220 } __packed;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300221 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800222 struct wil6210_rtap_vendor *rtap_vendor;
223 int rtap_len = sizeof(struct wil6210_rtap);
224 int phy_length = 0; /* phy info header size, bytes */
225 static char phy_data[128];
226 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
227
228 if (rtap_include_phy_info) {
229 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
230 /* calculate additional length */
231 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
232 /**
233 * PHY info starts from 8-byte boundary
234 * there are 8-byte lines, last line may be partially
235 * written (HW bug), thus FW configures for last line
236 * to be excessive. Driver skips this last line.
237 */
238 int len = min_t(int, 8 + sizeof(phy_data),
239 wil_rxdesc_phy_length(d));
240 if (len > 8) {
241 void *p = skb_tail_pointer(skb);
242 void *pa = PTR_ALIGN(p, 8);
243 if (skb_tailroom(skb) >= len + (pa - p)) {
244 phy_length = len - 8;
245 memcpy(phy_data, pa, phy_length);
246 }
247 }
248 }
249 rtap_len += phy_length;
250 }
251
252 if (skb_headroom(skb) < rtap_len &&
253 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
254 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
255 return;
256 }
257
258 rtap_vendor = (void *)skb_push(skb, rtap_len);
259 memset(rtap_vendor, 0, rtap_len);
260
261 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
262 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
263 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
264 (1 << IEEE80211_RADIOTAP_FLAGS) |
265 (1 << IEEE80211_RADIOTAP_CHANNEL) |
266 (1 << IEEE80211_RADIOTAP_MCS));
267 if (d->dma.status & RX_DMA_STATUS_ERROR)
268 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
269
270 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
271 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
272
273 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
274 rtap_vendor->rtap.mcs_flags = 0;
275 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
276
277 if (rtap_include_phy_info) {
278 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
279 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
280 /* OUI for Wilocity 04:ce:14 */
281 rtap_vendor->vendor_oui[0] = 0x04;
282 rtap_vendor->vendor_oui[1] = 0xce;
283 rtap_vendor->vendor_oui[2] = 0x14;
284 rtap_vendor->vendor_ns = 1;
285 /* Rx descriptor + PHY data */
286 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
287 phy_length);
288 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
289 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
290 phy_length);
291 }
292}
293
294/*
295 * Fast swap in place between 2 registers
296 */
297static void wil_swap_u16(u16 *a, u16 *b)
298{
299 *a ^= *b;
300 *b ^= *a;
301 *a ^= *b;
302}
303
304static void wil_swap_ethaddr(void *data)
305{
306 struct ethhdr *eth = data;
307 u16 *s = (u16 *)eth->h_source;
308 u16 *d = (u16 *)eth->h_dest;
309
310 wil_swap_u16(s++, d++);
311 wil_swap_u16(s++, d++);
312 wil_swap_u16(s, d);
313}
314
315/**
316 * reap 1 frame from @swhead
317 *
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300318 * Rx descriptor copied to skb->cb
319 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800320 * Safe to call from IRQ
321 */
322static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
323 struct vring *vring)
324{
325 struct device *dev = wil_to_dev(wil);
326 struct net_device *ndev = wil_to_ndev(wil);
327 volatile struct vring_rx_desc *d;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300328 struct vring_rx_desc *d1;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800329 struct sk_buff *skb;
330 dma_addr_t pa;
331 unsigned int sz = RX_BUF_LEN;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300332 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800333 u8 ftype;
334 u8 ds_bits;
335
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300336 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
337
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800338 if (wil_vring_is_empty(vring))
339 return NULL;
340
341 d = &(vring->va[vring->swhead].rx);
342 if (!(d->dma.status & RX_DMA_STATUS_DU)) {
343 /* it is not error, we just reached end of Rx done area */
344 return NULL;
345 }
346
347 pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
348 skb = vring->ctx[vring->swhead];
349 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800350
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300351 d1 = wil_skb_rxdesc(skb);
352 *d1 = *d;
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300353 wil_vring_advance_head(vring, 1);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300354 dmalen = le16_to_cpu(d1->dma.length);
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300355 if (dmalen > sz) {
356 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
357 kfree(skb);
358 return NULL;
359 }
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300360 skb_trim(skb, dmalen);
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300361
Vladimir Kondratievc0d37712013-05-12 14:43:34 +0300362 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
363 skb->data, skb_headlen(skb), false);
364
365
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300366 wil->stats.last_mcs_rx = wil_rxdesc_mcs(d1);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800367
368 /* use radiotap header only if required */
369 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300370 wil_rx_add_radiotap_header(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800371
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300372 trace_wil6210_rx(vring->swhead, d1);
373 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead,
374 d1->dma.length);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200375 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300376 (const void *)d1, sizeof(*d1), false);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800377
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800378 /* no extra checks if in sniffer mode */
379 if (ndev->type != ARPHRD_ETHER)
380 return skb;
381 /*
382 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
383 * Driver should recognize it by frame type, that is found
384 * in Rx descriptor. If type is not data, it is 802.11 frame as is
385 */
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300386 ftype = wil_rxdesc_ftype(d1) << 2;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800387 if (ftype != IEEE80211_FTYPE_DATA) {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200388 wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800389 /* TODO: process it */
390 kfree_skb(skb);
391 return NULL;
392 }
393
394 if (skb->len < ETH_HLEN) {
395 wil_err(wil, "Short frame, len = %d\n", skb->len);
396 /* TODO: process it (i.e. BAR) */
397 kfree_skb(skb);
398 return NULL;
399 }
400
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300401 ds_bits = wil_rxdesc_ds_bits(d1);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800402 if (ds_bits == 1) {
403 /*
404 * HW bug - in ToDS mode, i.e. Rx on AP side,
405 * addresses get swapped
406 */
407 wil_swap_ethaddr(skb->data);
408 }
409
410 return skb;
411}
412
413/**
414 * allocate and fill up to @count buffers in rx ring
415 * buffers posted at @swtail
416 */
417static int wil_rx_refill(struct wil6210_priv *wil, int count)
418{
419 struct net_device *ndev = wil_to_ndev(wil);
420 struct vring *v = &wil->vring_rx;
421 u32 next_tail;
422 int rc = 0;
423 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
424 WIL6210_RTAP_SIZE : 0;
425
426 for (; next_tail = wil_vring_next_tail(v),
427 (next_tail != v->swhead) && (count-- > 0);
428 v->swtail = next_tail) {
429 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
430 if (rc) {
431 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
432 rc, v->swtail);
433 break;
434 }
435 }
436 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
437
438 return rc;
439}
440
441/*
442 * Pass Rx packet to the netif. Update statistics.
443 */
444static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
445{
446 int rc;
447 unsigned int len = skb->len;
448
Vladimir Kondratiev241804c2013-01-28 18:31:02 +0200449 skb_orphan(skb);
450
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800451 if (in_interrupt())
452 rc = netif_rx(skb);
453 else
454 rc = netif_rx_ni(skb);
455
456 if (likely(rc == NET_RX_SUCCESS)) {
457 ndev->stats.rx_packets++;
458 ndev->stats.rx_bytes += len;
459
460 } else {
461 ndev->stats.rx_dropped++;
462 }
463}
464
465/**
466 * Proceed all completed skb's from Rx VRING
467 *
468 * Safe to call from IRQ
469 */
470void wil_rx_handle(struct wil6210_priv *wil)
471{
472 struct net_device *ndev = wil_to_ndev(wil);
473 struct vring *v = &wil->vring_rx;
474 struct sk_buff *skb;
475
476 if (!v->va) {
477 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
478 return;
479 }
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200480 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800481 while (NULL != (skb = wil_vring_reap_rx(wil, v))) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800482
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800483 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
484 skb->dev = ndev;
485 skb_reset_mac_header(skb);
486 skb->ip_summed = CHECKSUM_UNNECESSARY;
487 skb->pkt_type = PACKET_OTHERHOST;
488 skb->protocol = htons(ETH_P_802_2);
489
490 } else {
491 skb->protocol = eth_type_trans(skb, ndev);
492 }
493
494 wil_netif_rx_any(skb, ndev);
495 }
496 wil_rx_refill(wil, v->size);
497}
498
499int wil_rx_init(struct wil6210_priv *wil)
500{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800501 struct vring *vring = &wil->vring_rx;
502 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800503
504 vring->size = WIL6210_RX_RING_SIZE;
505 rc = wil_vring_alloc(wil, vring);
506 if (rc)
507 return rc;
508
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +0200509 rc = wmi_rx_chain_add(wil, vring);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800510 if (rc)
511 goto err_free;
512
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800513 rc = wil_rx_refill(wil, vring->size);
514 if (rc)
515 goto err_free;
516
517 return 0;
518 err_free:
519 wil_vring_free(wil, vring, 0);
520
521 return rc;
522}
523
524void wil_rx_fini(struct wil6210_priv *wil)
525{
526 struct vring *vring = &wil->vring_rx;
527
Vladimir Kondratiev2acb4222013-01-28 18:31:08 +0200528 if (vring->va)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800529 wil_vring_free(wil, vring, 0);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800530}
531
532int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
533 int cid, int tid)
534{
535 int rc;
536 struct wmi_vring_cfg_cmd cmd = {
537 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
538 .vring_cfg = {
539 .tx_sw_ring = {
540 .max_mpdu_size = cpu_to_le16(TX_BUF_LEN),
Vladimir Kondratievb5d98e92013-04-18 14:33:51 +0300541 .ring_size = cpu_to_le16(size),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800542 },
543 .ringid = id,
544 .cidxtid = (cid & 0xf) | ((tid & 0xf) << 4),
545 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
546 .mac_ctrl = 0,
547 .to_resolution = 0,
548 .agg_max_wsize = 16,
549 .schd_params = {
550 .priority = cpu_to_le16(0),
551 .timeslot_us = cpu_to_le16(0xfff),
552 },
553 },
554 };
555 struct {
556 struct wil6210_mbox_hdr_wmi wmi;
557 struct wmi_vring_cfg_done_event cmd;
558 } __packed reply;
559 struct vring *vring = &wil->vring_tx[id];
560
561 if (vring->va) {
562 wil_err(wil, "Tx ring [%d] already allocated\n", id);
563 rc = -EINVAL;
564 goto out;
565 }
566
567 vring->size = size;
568 rc = wil_vring_alloc(wil, vring);
569 if (rc)
570 goto out;
571
572 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800573
574 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
575 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
576 if (rc)
577 goto out_free;
578
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200579 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800580 wil_err(wil, "Tx config failed, status 0x%02x\n",
581 reply.cmd.status);
Vladimir Kondratievc3319972013-01-28 18:31:09 +0200582 rc = -EINVAL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800583 goto out_free;
584 }
585 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
586
587 return 0;
588 out_free:
589 wil_vring_free(wil, vring, 1);
590 out:
591
592 return rc;
593}
594
595void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
596{
597 struct vring *vring = &wil->vring_tx[id];
598
599 if (!vring->va)
600 return;
601
602 wil_vring_free(wil, vring, 1);
603}
604
605static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
606 struct sk_buff *skb)
607{
608 struct vring *v = &wil->vring_tx[0];
609
610 if (v->va)
611 return v;
612
613 return NULL;
614}
615
616static int wil_tx_desc_map(volatile struct vring_tx_desc *d,
617 dma_addr_t pa, u32 len)
618{
619 d->dma.addr_low = lower_32_bits(pa);
620 d->dma.addr_high = (u16)upper_32_bits(pa);
621 d->dma.ip_length = 0;
622 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
623 d->dma.b11 = 0/*14 | BIT(7)*/;
624 d->dma.error = 0;
625 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300626 d->dma.length = cpu_to_le16((u16)len);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800627 d->dma.d0 = 0;
628 d->mac.d[0] = 0;
629 d->mac.d[1] = 0;
630 d->mac.d[2] = 0;
631 d->mac.ucode_cmd = 0;
632 /* use dst index 0 */
633 d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
634 (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
635 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
636 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
637 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
638
639 return 0;
640}
641
642static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
643 struct sk_buff *skb)
644{
645 struct device *dev = wil_to_dev(wil);
646 volatile struct vring_tx_desc *d;
647 u32 swhead = vring->swhead;
648 int avail = wil_vring_avail_tx(vring);
649 int nr_frags = skb_shinfo(skb)->nr_frags;
650 uint f;
651 int vring_index = vring - wil->vring_tx;
652 uint i = swhead;
653 dma_addr_t pa;
654
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200655 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800656
657 if (avail < vring->size/8)
658 netif_tx_stop_all_queues(wil_to_ndev(wil));
659 if (avail < 1 + nr_frags) {
660 wil_err(wil, "Tx ring full. No space for %d fragments\n",
661 1 + nr_frags);
662 return -ENOMEM;
663 }
664 d = &(vring->va[i].tx);
665
666 /* FIXME FW can accept only unicast frames for the peer */
667 memcpy(skb->data, wil->dst_addr[vring_index], ETH_ALEN);
668
669 pa = dma_map_single(dev, skb->data,
670 skb_headlen(skb), DMA_TO_DEVICE);
671
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200672 wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800673 skb->data, (unsigned long long)pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200674 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800675 skb->data, skb_headlen(skb), false);
676
677 if (unlikely(dma_mapping_error(dev, pa)))
678 return -EINVAL;
679 /* 1-st segment */
680 wil_tx_desc_map(d, pa, skb_headlen(skb));
681 d->mac.d[2] |= ((nr_frags + 1) <<
682 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
683 /* middle segments */
684 for (f = 0; f < nr_frags; f++) {
685 const struct skb_frag_struct *frag =
686 &skb_shinfo(skb)->frags[f];
687 int len = skb_frag_size(frag);
688 i = (swhead + f + 1) % vring->size;
689 d = &(vring->va[i].tx);
690 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
691 DMA_TO_DEVICE);
692 if (unlikely(dma_mapping_error(dev, pa)))
693 goto dma_error;
694 wil_tx_desc_map(d, pa, len);
695 vring->ctx[i] = NULL;
696 }
697 /* for the last seg only */
698 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
699 d->dma.d0 |= BIT(9); /* BUG: undocumented bit */
700 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
701 d->dma.d0 |= (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
702
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200703 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800704 (const void *)d, sizeof(*d), false);
705
706 /* advance swhead */
707 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200708 wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300709 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800710 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
711 /* hold reference to skb
712 * to prevent skb release before accounting
713 * in case of immediate "tx done"
714 */
715 vring->ctx[i] = skb_get(skb);
716
717 return 0;
718 dma_error:
719 /* unmap what we have mapped */
720 /* Note: increment @f to operate with positive index */
721 for (f++; f > 0; f--) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300722 u16 dmalen;
723
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800724 i = (swhead + f) % vring->size;
725 d = &(vring->va[i].tx);
726 d->dma.status = TX_DMA_STATUS_DU;
727 pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300728 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800729 if (vring->ctx[i])
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300730 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800731 else
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300732 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800733 }
734
735 return -EINVAL;
736}
737
738
739netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
740{
741 struct wil6210_priv *wil = ndev_to_wil(ndev);
742 struct vring *vring;
743 int rc;
744
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200745 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800746 if (!test_bit(wil_status_fwready, &wil->status)) {
747 wil_err(wil, "FW not ready\n");
748 goto drop;
749 }
750 if (!test_bit(wil_status_fwconnected, &wil->status)) {
751 wil_err(wil, "FW not connected\n");
752 goto drop;
753 }
754 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
755 wil_err(wil, "Xmit in monitor mode not supported\n");
756 goto drop;
757 }
758 if (skb->protocol == cpu_to_be16(ETH_P_PAE)) {
759 rc = wmi_tx_eapol(wil, skb);
760 } else {
761 /* find vring */
762 vring = wil_find_tx_vring(wil, skb);
763 if (!vring) {
764 wil_err(wil, "No Tx VRING available\n");
765 goto drop;
766 }
767 /* set up vring entry */
768 rc = wil_tx_vring(wil, vring, skb);
769 }
770 switch (rc) {
771 case 0:
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200772 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800773 dev_kfree_skb_any(skb);
774 return NETDEV_TX_OK;
775 case -ENOMEM:
776 return NETDEV_TX_BUSY;
777 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +0200778 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800779 }
780 drop:
781 netif_tx_stop_all_queues(ndev);
782 ndev->stats.tx_dropped++;
783 dev_kfree_skb_any(skb);
784
785 return NET_XMIT_DROP;
786}
787
788/**
789 * Clean up transmitted skb's from the Tx VRING
790 *
791 * Safe to call from IRQ
792 */
793void wil_tx_complete(struct wil6210_priv *wil, int ringid)
794{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200795 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800796 struct device *dev = wil_to_dev(wil);
797 struct vring *vring = &wil->vring_tx[ringid];
798
799 if (!vring->va) {
800 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
801 return;
802 }
803
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200804 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800805
806 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300807 volatile struct vring_tx_desc *d1 =
808 &vring->va[vring->swtail].tx;
809 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800810 dma_addr_t pa;
811 struct sk_buff *skb;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300812 u16 dmalen;
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300813
814 dd = *d1;
815
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800816 if (!(d->dma.status & TX_DMA_STATUS_DU))
817 break;
818
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300819 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev98658092013-05-12 14:43:35 +0300820 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
821 d->dma.error);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200822 wil_dbg_txrx(wil,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800823 "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300824 vring->swtail, dmalen, d->dma.status,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800825 d->dma.error);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200826 wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800827 (const void *)d, sizeof(*d), false);
828
829 pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
830 skb = vring->ctx[vring->swtail];
831 if (skb) {
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200832 if (d->dma.error == 0) {
833 ndev->stats.tx_packets++;
834 ndev->stats.tx_bytes += skb->len;
835 } else {
836 ndev->stats.tx_errors++;
837 }
838
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300839 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800840 dev_kfree_skb_any(skb);
841 vring->ctx[vring->swtail] = NULL;
842 } else {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300843 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800844 }
845 d->dma.addr_low = 0;
846 d->dma.addr_high = 0;
847 d->dma.length = 0;
848 d->dma.status = TX_DMA_STATUS_DU;
849 vring->swtail = wil_vring_next_tail(vring);
850 }
851 if (wil_vring_avail_tx(vring) > vring->size/4)
852 netif_tx_wake_all_queues(wil_to_ndev(wil));
853}