blob: 92f18215014cec265b2485c840b4a3c9134a958d [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"
25
26static bool rtap_include_phy_info;
27module_param(rtap_include_phy_info, bool, S_IRUGO);
28MODULE_PARM_DESC(rtap_include_phy_info,
29 " Include PHY info in the radiotap header, default - no");
30
31static inline int wil_vring_is_empty(struct vring *vring)
32{
33 return vring->swhead == vring->swtail;
34}
35
36static inline u32 wil_vring_next_tail(struct vring *vring)
37{
38 return (vring->swtail + 1) % vring->size;
39}
40
41static inline void wil_vring_advance_head(struct vring *vring, int n)
42{
43 vring->swhead = (vring->swhead + n) % vring->size;
44}
45
46static inline int wil_vring_is_full(struct vring *vring)
47{
48 return wil_vring_next_tail(vring) == vring->swhead;
49}
50/*
51 * Available space in Tx Vring
52 */
53static inline int wil_vring_avail_tx(struct vring *vring)
54{
55 u32 swhead = vring->swhead;
56 u32 swtail = vring->swtail;
57 int used = (vring->size + swhead - swtail) % vring->size;
58
59 return vring->size - used - 1;
60}
61
62static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
63{
64 struct device *dev = wil_to_dev(wil);
65 size_t sz = vring->size * sizeof(vring->va[0]);
66 uint i;
67
68 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
69
70 vring->swhead = 0;
71 vring->swtail = 0;
72 vring->ctx = kzalloc(vring->size * sizeof(vring->ctx[0]), GFP_KERNEL);
73 if (!vring->ctx) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080074 vring->va = NULL;
75 return -ENOMEM;
76 }
77 /*
78 * vring->va should be aligned on its size rounded up to power of 2
79 * This is granted by the dma_alloc_coherent
80 */
81 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
82 if (!vring->va) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080083 kfree(vring->ctx);
84 vring->ctx = NULL;
85 return -ENOMEM;
86 }
87 /* initially, all descriptors are SW owned
88 * For Tx and Rx, ownership bit is at the same location, thus
89 * we can use any
90 */
91 for (i = 0; i < vring->size; i++) {
92 volatile struct vring_tx_desc *d = &(vring->va[i].tx);
93 d->dma.status = TX_DMA_STATUS_DU;
94 }
95
Vladimir Kondratiev77438822013-01-28 18:31:06 +020096 wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size,
Vladimir Kondratiev2057ebb2013-01-28 18:30:58 +020097 vring->va, (unsigned long long)vring->pa, vring->ctx);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080098
99 return 0;
100}
101
102static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
103 int tx)
104{
105 struct device *dev = wil_to_dev(wil);
106 size_t sz = vring->size * sizeof(vring->va[0]);
107
108 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300109 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800110 if (tx) {
111 volatile struct vring_tx_desc *d =
112 &vring->va[vring->swtail].tx;
113 dma_addr_t pa = d->dma.addr_low |
114 ((u64)d->dma.addr_high << 32);
115 struct sk_buff *skb = vring->ctx[vring->swtail];
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300116 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800117 if (skb) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300118 dma_unmap_single(dev, pa, dmalen,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800119 DMA_TO_DEVICE);
120 dev_kfree_skb_any(skb);
121 vring->ctx[vring->swtail] = NULL;
122 } else {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300123 dma_unmap_page(dev, pa, dmalen,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800124 DMA_TO_DEVICE);
125 }
126 vring->swtail = wil_vring_next_tail(vring);
127 } else { /* rx */
128 volatile struct vring_rx_desc *d =
129 &vring->va[vring->swtail].rx;
130 dma_addr_t pa = d->dma.addr_low |
131 ((u64)d->dma.addr_high << 32);
132 struct sk_buff *skb = vring->ctx[vring->swhead];
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300133 dmalen = le16_to_cpu(d->dma.length);
134 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800135 kfree_skb(skb);
136 wil_vring_advance_head(vring, 1);
137 }
138 }
139 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
140 kfree(vring->ctx);
141 vring->pa = 0;
142 vring->va = NULL;
143 vring->ctx = NULL;
144}
145
146/**
147 * Allocate one skb for Rx VRING
148 *
149 * Safe to call from IRQ
150 */
151static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
152 u32 i, int headroom)
153{
154 struct device *dev = wil_to_dev(wil);
155 unsigned int sz = RX_BUF_LEN;
156 volatile struct vring_rx_desc *d = &(vring->va[i].rx);
157 dma_addr_t pa;
158
159 /* TODO align */
160 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
161 if (unlikely(!skb))
162 return -ENOMEM;
163
164 skb_reserve(skb, headroom);
165 skb_put(skb, sz);
166
167 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
168 if (unlikely(dma_mapping_error(dev, pa))) {
169 kfree_skb(skb);
170 return -ENOMEM;
171 }
172
173 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
174 d->dma.addr_low = lower_32_bits(pa);
175 d->dma.addr_high = (u16)upper_32_bits(pa);
176 /* ip_length don't care */
177 /* b11 don't care */
178 /* error don't care */
179 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300180 d->dma.length = cpu_to_le16(sz);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800181 vring->ctx[i] = skb;
182
183 return 0;
184}
185
186/**
187 * Adds radiotap header
188 *
189 * Any error indicated as "Bad FCS"
190 *
191 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
192 * - Rx descriptor: 32 bytes
193 * - Phy info
194 */
195static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300196 struct sk_buff *skb)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800197{
198 struct wireless_dev *wdev = wil->wdev;
199 struct wil6210_rtap {
200 struct ieee80211_radiotap_header rthdr;
201 /* fields should be in the order of bits in rthdr.it_present */
202 /* flags */
203 u8 flags;
204 /* channel */
205 __le16 chnl_freq __aligned(2);
206 __le16 chnl_flags;
207 /* MCS */
208 u8 mcs_present;
209 u8 mcs_flags;
210 u8 mcs_index;
211 } __packed;
212 struct wil6210_rtap_vendor {
213 struct wil6210_rtap rtap;
214 /* vendor */
215 u8 vendor_oui[3] __aligned(2);
216 u8 vendor_ns;
217 __le16 vendor_skip;
218 u8 vendor_data[0];
219 } __packed;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300220 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800221 struct wil6210_rtap_vendor *rtap_vendor;
222 int rtap_len = sizeof(struct wil6210_rtap);
223 int phy_length = 0; /* phy info header size, bytes */
224 static char phy_data[128];
225 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
226
227 if (rtap_include_phy_info) {
228 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
229 /* calculate additional length */
230 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
231 /**
232 * PHY info starts from 8-byte boundary
233 * there are 8-byte lines, last line may be partially
234 * written (HW bug), thus FW configures for last line
235 * to be excessive. Driver skips this last line.
236 */
237 int len = min_t(int, 8 + sizeof(phy_data),
238 wil_rxdesc_phy_length(d));
239 if (len > 8) {
240 void *p = skb_tail_pointer(skb);
241 void *pa = PTR_ALIGN(p, 8);
242 if (skb_tailroom(skb) >= len + (pa - p)) {
243 phy_length = len - 8;
244 memcpy(phy_data, pa, phy_length);
245 }
246 }
247 }
248 rtap_len += phy_length;
249 }
250
251 if (skb_headroom(skb) < rtap_len &&
252 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
253 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
254 return;
255 }
256
257 rtap_vendor = (void *)skb_push(skb, rtap_len);
258 memset(rtap_vendor, 0, rtap_len);
259
260 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
261 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
262 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
263 (1 << IEEE80211_RADIOTAP_FLAGS) |
264 (1 << IEEE80211_RADIOTAP_CHANNEL) |
265 (1 << IEEE80211_RADIOTAP_MCS));
266 if (d->dma.status & RX_DMA_STATUS_ERROR)
267 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
268
269 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
270 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
271
272 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
273 rtap_vendor->rtap.mcs_flags = 0;
274 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
275
276 if (rtap_include_phy_info) {
277 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
278 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
279 /* OUI for Wilocity 04:ce:14 */
280 rtap_vendor->vendor_oui[0] = 0x04;
281 rtap_vendor->vendor_oui[1] = 0xce;
282 rtap_vendor->vendor_oui[2] = 0x14;
283 rtap_vendor->vendor_ns = 1;
284 /* Rx descriptor + PHY data */
285 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
286 phy_length);
287 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
288 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
289 phy_length);
290 }
291}
292
293/*
294 * Fast swap in place between 2 registers
295 */
296static void wil_swap_u16(u16 *a, u16 *b)
297{
298 *a ^= *b;
299 *b ^= *a;
300 *a ^= *b;
301}
302
303static void wil_swap_ethaddr(void *data)
304{
305 struct ethhdr *eth = data;
306 u16 *s = (u16 *)eth->h_source;
307 u16 *d = (u16 *)eth->h_dest;
308
309 wil_swap_u16(s++, d++);
310 wil_swap_u16(s++, d++);
311 wil_swap_u16(s, d);
312}
313
314/**
315 * reap 1 frame from @swhead
316 *
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300317 * Rx descriptor copied to skb->cb
318 *
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800319 * Safe to call from IRQ
320 */
321static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
322 struct vring *vring)
323{
324 struct device *dev = wil_to_dev(wil);
325 struct net_device *ndev = wil_to_ndev(wil);
326 volatile struct vring_rx_desc *d;
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300327 struct vring_rx_desc *d1;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800328 struct sk_buff *skb;
329 dma_addr_t pa;
330 unsigned int sz = RX_BUF_LEN;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300331 u16 dmalen;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800332 u8 ftype;
333 u8 ds_bits;
334
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300335 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
336
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800337 if (wil_vring_is_empty(vring))
338 return NULL;
339
340 d = &(vring->va[vring->swhead].rx);
341 if (!(d->dma.status & RX_DMA_STATUS_DU)) {
342 /* it is not error, we just reached end of Rx done area */
343 return NULL;
344 }
345
346 pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
347 skb = vring->ctx[vring->swhead];
348 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800349
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300350 d1 = wil_skb_rxdesc(skb);
351 *d1 = *d;
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300352 wil_vring_advance_head(vring, 1);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300353 dmalen = le16_to_cpu(d1->dma.length);
Vladimir Kondratieve2700452013-05-12 14:43:33 +0300354 if (dmalen > sz) {
355 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
356 kfree(skb);
357 return NULL;
358 }
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300359 skb_trim(skb, dmalen);
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300360
361 wil->stats.last_mcs_rx = wil_rxdesc_mcs(d1);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800362
363 /* use radiotap header only if required */
364 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300365 wil_rx_add_radiotap_header(wil, skb);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800366
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200367 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, d->dma.length);
368 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800369 (const void *)d, sizeof(*d), false);
370
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800371 /* no extra checks if in sniffer mode */
372 if (ndev->type != ARPHRD_ETHER)
373 return skb;
374 /*
375 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
376 * Driver should recognize it by frame type, that is found
377 * in Rx descriptor. If type is not data, it is 802.11 frame as is
378 */
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300379 ftype = wil_rxdesc_ftype(d1) << 2;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800380 if (ftype != IEEE80211_FTYPE_DATA) {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200381 wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800382 /* TODO: process it */
383 kfree_skb(skb);
384 return NULL;
385 }
386
387 if (skb->len < ETH_HLEN) {
388 wil_err(wil, "Short frame, len = %d\n", skb->len);
389 /* TODO: process it (i.e. BAR) */
390 kfree_skb(skb);
391 return NULL;
392 }
393
Vladimir Kondratiev33e61162013-04-18 14:33:50 +0300394 ds_bits = wil_rxdesc_ds_bits(d1);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800395 if (ds_bits == 1) {
396 /*
397 * HW bug - in ToDS mode, i.e. Rx on AP side,
398 * addresses get swapped
399 */
400 wil_swap_ethaddr(skb->data);
401 }
402
403 return skb;
404}
405
406/**
407 * allocate and fill up to @count buffers in rx ring
408 * buffers posted at @swtail
409 */
410static int wil_rx_refill(struct wil6210_priv *wil, int count)
411{
412 struct net_device *ndev = wil_to_ndev(wil);
413 struct vring *v = &wil->vring_rx;
414 u32 next_tail;
415 int rc = 0;
416 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
417 WIL6210_RTAP_SIZE : 0;
418
419 for (; next_tail = wil_vring_next_tail(v),
420 (next_tail != v->swhead) && (count-- > 0);
421 v->swtail = next_tail) {
422 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
423 if (rc) {
424 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
425 rc, v->swtail);
426 break;
427 }
428 }
429 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
430
431 return rc;
432}
433
434/*
435 * Pass Rx packet to the netif. Update statistics.
436 */
437static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
438{
439 int rc;
440 unsigned int len = skb->len;
441
Vladimir Kondratiev241804c2013-01-28 18:31:02 +0200442 skb_orphan(skb);
443
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800444 if (in_interrupt())
445 rc = netif_rx(skb);
446 else
447 rc = netif_rx_ni(skb);
448
449 if (likely(rc == NET_RX_SUCCESS)) {
450 ndev->stats.rx_packets++;
451 ndev->stats.rx_bytes += len;
452
453 } else {
454 ndev->stats.rx_dropped++;
455 }
456}
457
458/**
459 * Proceed all completed skb's from Rx VRING
460 *
461 * Safe to call from IRQ
462 */
463void wil_rx_handle(struct wil6210_priv *wil)
464{
465 struct net_device *ndev = wil_to_ndev(wil);
466 struct vring *v = &wil->vring_rx;
467 struct sk_buff *skb;
468
469 if (!v->va) {
470 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
471 return;
472 }
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200473 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800474 while (NULL != (skb = wil_vring_reap_rx(wil, v))) {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200475 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800476 skb->data, skb_headlen(skb), false);
477
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800478 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
479 skb->dev = ndev;
480 skb_reset_mac_header(skb);
481 skb->ip_summed = CHECKSUM_UNNECESSARY;
482 skb->pkt_type = PACKET_OTHERHOST;
483 skb->protocol = htons(ETH_P_802_2);
484
485 } else {
486 skb->protocol = eth_type_trans(skb, ndev);
487 }
488
489 wil_netif_rx_any(skb, ndev);
490 }
491 wil_rx_refill(wil, v->size);
492}
493
494int wil_rx_init(struct wil6210_priv *wil)
495{
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800496 struct vring *vring = &wil->vring_rx;
497 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800498
499 vring->size = WIL6210_RX_RING_SIZE;
500 rc = wil_vring_alloc(wil, vring);
501 if (rc)
502 return rc;
503
Vladimir Kondratiev47e19af2013-01-28 18:30:59 +0200504 rc = wmi_rx_chain_add(wil, vring);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800505 if (rc)
506 goto err_free;
507
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800508 rc = wil_rx_refill(wil, vring->size);
509 if (rc)
510 goto err_free;
511
512 return 0;
513 err_free:
514 wil_vring_free(wil, vring, 0);
515
516 return rc;
517}
518
519void wil_rx_fini(struct wil6210_priv *wil)
520{
521 struct vring *vring = &wil->vring_rx;
522
Vladimir Kondratiev2acb4222013-01-28 18:31:08 +0200523 if (vring->va)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800524 wil_vring_free(wil, vring, 0);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800525}
526
527int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
528 int cid, int tid)
529{
530 int rc;
531 struct wmi_vring_cfg_cmd cmd = {
532 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
533 .vring_cfg = {
534 .tx_sw_ring = {
535 .max_mpdu_size = cpu_to_le16(TX_BUF_LEN),
Vladimir Kondratievb5d98e92013-04-18 14:33:51 +0300536 .ring_size = cpu_to_le16(size),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800537 },
538 .ringid = id,
539 .cidxtid = (cid & 0xf) | ((tid & 0xf) << 4),
540 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
541 .mac_ctrl = 0,
542 .to_resolution = 0,
543 .agg_max_wsize = 16,
544 .schd_params = {
545 .priority = cpu_to_le16(0),
546 .timeslot_us = cpu_to_le16(0xfff),
547 },
548 },
549 };
550 struct {
551 struct wil6210_mbox_hdr_wmi wmi;
552 struct wmi_vring_cfg_done_event cmd;
553 } __packed reply;
554 struct vring *vring = &wil->vring_tx[id];
555
556 if (vring->va) {
557 wil_err(wil, "Tx ring [%d] already allocated\n", id);
558 rc = -EINVAL;
559 goto out;
560 }
561
562 vring->size = size;
563 rc = wil_vring_alloc(wil, vring);
564 if (rc)
565 goto out;
566
567 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800568
569 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
570 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
571 if (rc)
572 goto out_free;
573
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200574 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800575 wil_err(wil, "Tx config failed, status 0x%02x\n",
576 reply.cmd.status);
Vladimir Kondratievc3319972013-01-28 18:31:09 +0200577 rc = -EINVAL;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800578 goto out_free;
579 }
580 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
581
582 return 0;
583 out_free:
584 wil_vring_free(wil, vring, 1);
585 out:
586
587 return rc;
588}
589
590void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
591{
592 struct vring *vring = &wil->vring_tx[id];
593
594 if (!vring->va)
595 return;
596
597 wil_vring_free(wil, vring, 1);
598}
599
600static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
601 struct sk_buff *skb)
602{
603 struct vring *v = &wil->vring_tx[0];
604
605 if (v->va)
606 return v;
607
608 return NULL;
609}
610
611static int wil_tx_desc_map(volatile struct vring_tx_desc *d,
612 dma_addr_t pa, u32 len)
613{
614 d->dma.addr_low = lower_32_bits(pa);
615 d->dma.addr_high = (u16)upper_32_bits(pa);
616 d->dma.ip_length = 0;
617 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
618 d->dma.b11 = 0/*14 | BIT(7)*/;
619 d->dma.error = 0;
620 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300621 d->dma.length = cpu_to_le16((u16)len);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800622 d->dma.d0 = 0;
623 d->mac.d[0] = 0;
624 d->mac.d[1] = 0;
625 d->mac.d[2] = 0;
626 d->mac.ucode_cmd = 0;
627 /* use dst index 0 */
628 d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
629 (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
630 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
631 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
632 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
633
634 return 0;
635}
636
637static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
638 struct sk_buff *skb)
639{
640 struct device *dev = wil_to_dev(wil);
641 volatile struct vring_tx_desc *d;
642 u32 swhead = vring->swhead;
643 int avail = wil_vring_avail_tx(vring);
644 int nr_frags = skb_shinfo(skb)->nr_frags;
645 uint f;
646 int vring_index = vring - wil->vring_tx;
647 uint i = swhead;
648 dma_addr_t pa;
649
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200650 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800651
652 if (avail < vring->size/8)
653 netif_tx_stop_all_queues(wil_to_ndev(wil));
654 if (avail < 1 + nr_frags) {
655 wil_err(wil, "Tx ring full. No space for %d fragments\n",
656 1 + nr_frags);
657 return -ENOMEM;
658 }
659 d = &(vring->va[i].tx);
660
661 /* FIXME FW can accept only unicast frames for the peer */
662 memcpy(skb->data, wil->dst_addr[vring_index], ETH_ALEN);
663
664 pa = dma_map_single(dev, skb->data,
665 skb_headlen(skb), DMA_TO_DEVICE);
666
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200667 wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb),
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800668 skb->data, (unsigned long long)pa);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200669 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800670 skb->data, skb_headlen(skb), false);
671
672 if (unlikely(dma_mapping_error(dev, pa)))
673 return -EINVAL;
674 /* 1-st segment */
675 wil_tx_desc_map(d, pa, skb_headlen(skb));
676 d->mac.d[2] |= ((nr_frags + 1) <<
677 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
678 /* middle segments */
679 for (f = 0; f < nr_frags; f++) {
680 const struct skb_frag_struct *frag =
681 &skb_shinfo(skb)->frags[f];
682 int len = skb_frag_size(frag);
683 i = (swhead + f + 1) % vring->size;
684 d = &(vring->va[i].tx);
685 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
686 DMA_TO_DEVICE);
687 if (unlikely(dma_mapping_error(dev, pa)))
688 goto dma_error;
689 wil_tx_desc_map(d, pa, len);
690 vring->ctx[i] = NULL;
691 }
692 /* for the last seg only */
693 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
694 d->dma.d0 |= BIT(9); /* BUG: undocumented bit */
695 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
696 d->dma.d0 |= (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
697
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200698 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800699 (const void *)d, sizeof(*d), false);
700
701 /* advance swhead */
702 wil_vring_advance_head(vring, nr_frags + 1);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200703 wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800704 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
705 /* hold reference to skb
706 * to prevent skb release before accounting
707 * in case of immediate "tx done"
708 */
709 vring->ctx[i] = skb_get(skb);
710
711 return 0;
712 dma_error:
713 /* unmap what we have mapped */
714 /* Note: increment @f to operate with positive index */
715 for (f++; f > 0; f--) {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300716 u16 dmalen;
717
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800718 i = (swhead + f) % vring->size;
719 d = &(vring->va[i].tx);
720 d->dma.status = TX_DMA_STATUS_DU;
721 pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300722 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800723 if (vring->ctx[i])
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300724 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800725 else
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300726 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800727 }
728
729 return -EINVAL;
730}
731
732
733netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
734{
735 struct wil6210_priv *wil = ndev_to_wil(ndev);
736 struct vring *vring;
737 int rc;
738
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200739 wil_dbg_txrx(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800740 if (!test_bit(wil_status_fwready, &wil->status)) {
741 wil_err(wil, "FW not ready\n");
742 goto drop;
743 }
744 if (!test_bit(wil_status_fwconnected, &wil->status)) {
745 wil_err(wil, "FW not connected\n");
746 goto drop;
747 }
748 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
749 wil_err(wil, "Xmit in monitor mode not supported\n");
750 goto drop;
751 }
752 if (skb->protocol == cpu_to_be16(ETH_P_PAE)) {
753 rc = wmi_tx_eapol(wil, skb);
754 } else {
755 /* find vring */
756 vring = wil_find_tx_vring(wil, skb);
757 if (!vring) {
758 wil_err(wil, "No Tx VRING available\n");
759 goto drop;
760 }
761 /* set up vring entry */
762 rc = wil_tx_vring(wil, vring, skb);
763 }
764 switch (rc) {
765 case 0:
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200766 /* statistics will be updated on the tx_complete */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800767 dev_kfree_skb_any(skb);
768 return NETDEV_TX_OK;
769 case -ENOMEM:
770 return NETDEV_TX_BUSY;
771 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +0200772 break; /* goto drop; */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800773 }
774 drop:
775 netif_tx_stop_all_queues(ndev);
776 ndev->stats.tx_dropped++;
777 dev_kfree_skb_any(skb);
778
779 return NET_XMIT_DROP;
780}
781
782/**
783 * Clean up transmitted skb's from the Tx VRING
784 *
785 * Safe to call from IRQ
786 */
787void wil_tx_complete(struct wil6210_priv *wil, int ringid)
788{
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200789 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800790 struct device *dev = wil_to_dev(wil);
791 struct vring *vring = &wil->vring_tx[ringid];
792
793 if (!vring->va) {
794 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
795 return;
796 }
797
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200798 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800799
800 while (!wil_vring_is_empty(vring)) {
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300801 volatile struct vring_tx_desc *d1 =
802 &vring->va[vring->swtail].tx;
803 struct vring_tx_desc dd, *d = &dd;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800804 dma_addr_t pa;
805 struct sk_buff *skb;
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300806 u16 dmalen;
Vladimir Kondratiev4de41be2013-04-18 14:33:52 +0300807
808 dd = *d1;
809
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800810 if (!(d->dma.status & TX_DMA_STATUS_DU))
811 break;
812
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300813 dmalen = le16_to_cpu(d->dma.length);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200814 wil_dbg_txrx(wil,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800815 "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300816 vring->swtail, dmalen, d->dma.status,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800817 d->dma.error);
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200818 wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800819 (const void *)d, sizeof(*d), false);
820
821 pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
822 skb = vring->ctx[vring->swtail];
823 if (skb) {
Vladimir Kondratiev795ce732013-01-28 18:31:00 +0200824 if (d->dma.error == 0) {
825 ndev->stats.tx_packets++;
826 ndev->stats.tx_bytes += skb->len;
827 } else {
828 ndev->stats.tx_errors++;
829 }
830
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300831 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800832 dev_kfree_skb_any(skb);
833 vring->ctx[vring->swtail] = NULL;
834 } else {
Vladimir Kondratiev7e5944442013-05-12 14:43:32 +0300835 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800836 }
837 d->dma.addr_low = 0;
838 d->dma.addr_high = 0;
839 d->dma.length = 0;
840 d->dma.status = TX_DMA_STATUS_DU;
841 vring->swtail = wil_vring_next_tail(vring);
842 }
843 if (wil_vring_avail_tx(vring) > vring->size/4)
844 netif_tx_wake_all_queues(wil_to_ndev(wil));
845}