Commit | Line | Data |
---|---|---|
2be7d22f VK |
1 | /* |
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 | ||
2be7d22f | 17 | #include <linux/etherdevice.h> |
2be7d22f VK |
18 | #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 | ||
26 | static bool rtap_include_phy_info; | |
27 | module_param(rtap_include_phy_info, bool, S_IRUGO); | |
28 | MODULE_PARM_DESC(rtap_include_phy_info, | |
29 | " Include PHY info in the radiotap header, default - no"); | |
30 | ||
31 | static inline int wil_vring_is_empty(struct vring *vring) | |
32 | { | |
33 | return vring->swhead == vring->swtail; | |
34 | } | |
35 | ||
36 | static inline u32 wil_vring_next_tail(struct vring *vring) | |
37 | { | |
38 | return (vring->swtail + 1) % vring->size; | |
39 | } | |
40 | ||
41 | static inline void wil_vring_advance_head(struct vring *vring, int n) | |
42 | { | |
43 | vring->swhead = (vring->swhead + n) % vring->size; | |
44 | } | |
45 | ||
46 | static 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 | */ | |
53 | static 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 | ||
62 | static 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) { | |
2be7d22f VK |
74 | 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) { | |
83 | wil_err(wil, "vring_alloc [%d] failed to alloc DMA mem\n", | |
84 | vring->size); | |
85 | kfree(vring->ctx); | |
86 | vring->ctx = NULL; | |
87 | return -ENOMEM; | |
88 | } | |
89 | /* initially, all descriptors are SW owned | |
90 | * For Tx and Rx, ownership bit is at the same location, thus | |
91 | * we can use any | |
92 | */ | |
93 | for (i = 0; i < vring->size; i++) { | |
94 | volatile struct vring_tx_desc *d = &(vring->va[i].tx); | |
95 | d->dma.status = TX_DMA_STATUS_DU; | |
96 | } | |
97 | ||
7743882d | 98 | wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size, |
2057ebb2 | 99 | vring->va, (unsigned long long)vring->pa, vring->ctx); |
2be7d22f VK |
100 | |
101 | return 0; | |
102 | } | |
103 | ||
104 | static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring, | |
105 | int tx) | |
106 | { | |
107 | struct device *dev = wil_to_dev(wil); | |
108 | size_t sz = vring->size * sizeof(vring->va[0]); | |
109 | ||
110 | while (!wil_vring_is_empty(vring)) { | |
111 | 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]; | |
117 | if (skb) { | |
118 | dma_unmap_single(dev, pa, d->dma.length, | |
119 | DMA_TO_DEVICE); | |
120 | dev_kfree_skb_any(skb); | |
121 | vring->ctx[vring->swtail] = NULL; | |
122 | } else { | |
123 | dma_unmap_page(dev, pa, d->dma.length, | |
124 | 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]; | |
133 | dma_unmap_single(dev, pa, d->dma.length, | |
134 | DMA_FROM_DEVICE); | |
135 | 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 | */ | |
151 | static 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 */ | |
180 | d->dma.length = sz; | |
181 | 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 | */ | |
195 | static void wil_rx_add_radiotap_header(struct wil6210_priv *wil, | |
196 | struct sk_buff *skb, | |
197 | volatile struct vring_rx_desc *d) | |
198 | { | |
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; | |
221 | 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 | */ | |
296 | static void wil_swap_u16(u16 *a, u16 *b) | |
297 | { | |
298 | *a ^= *b; | |
299 | *b ^= *a; | |
300 | *a ^= *b; | |
301 | } | |
302 | ||
303 | static 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 | * | |
317 | * Safe to call from IRQ | |
318 | */ | |
319 | static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil, | |
320 | struct vring *vring) | |
321 | { | |
322 | struct device *dev = wil_to_dev(wil); | |
323 | struct net_device *ndev = wil_to_ndev(wil); | |
324 | volatile struct vring_rx_desc *d; | |
325 | struct sk_buff *skb; | |
326 | dma_addr_t pa; | |
327 | unsigned int sz = RX_BUF_LEN; | |
328 | u8 ftype; | |
329 | u8 ds_bits; | |
330 | ||
331 | if (wil_vring_is_empty(vring)) | |
332 | return NULL; | |
333 | ||
334 | d = &(vring->va[vring->swhead].rx); | |
335 | if (!(d->dma.status & RX_DMA_STATUS_DU)) { | |
336 | /* it is not error, we just reached end of Rx done area */ | |
337 | return NULL; | |
338 | } | |
339 | ||
340 | pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32); | |
341 | skb = vring->ctx[vring->swhead]; | |
342 | dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE); | |
343 | skb_trim(skb, d->dma.length); | |
344 | ||
345 | wil->stats.last_mcs_rx = wil_rxdesc_mcs(d); | |
346 | ||
347 | /* use radiotap header only if required */ | |
348 | if (ndev->type == ARPHRD_IEEE80211_RADIOTAP) | |
349 | wil_rx_add_radiotap_header(wil, skb, d); | |
350 | ||
7743882d VK |
351 | wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, d->dma.length); |
352 | wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4, | |
2be7d22f VK |
353 | (const void *)d, sizeof(*d), false); |
354 | ||
355 | wil_vring_advance_head(vring, 1); | |
356 | ||
357 | /* no extra checks if in sniffer mode */ | |
358 | if (ndev->type != ARPHRD_ETHER) | |
359 | return skb; | |
360 | /* | |
361 | * Non-data frames may be delivered through Rx DMA channel (ex: BAR) | |
362 | * Driver should recognize it by frame type, that is found | |
363 | * in Rx descriptor. If type is not data, it is 802.11 frame as is | |
364 | */ | |
365 | ftype = wil_rxdesc_ftype(d) << 2; | |
366 | if (ftype != IEEE80211_FTYPE_DATA) { | |
7743882d | 367 | wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype); |
2be7d22f VK |
368 | /* TODO: process it */ |
369 | kfree_skb(skb); | |
370 | return NULL; | |
371 | } | |
372 | ||
373 | if (skb->len < ETH_HLEN) { | |
374 | wil_err(wil, "Short frame, len = %d\n", skb->len); | |
375 | /* TODO: process it (i.e. BAR) */ | |
376 | kfree_skb(skb); | |
377 | return NULL; | |
378 | } | |
379 | ||
380 | ds_bits = wil_rxdesc_ds_bits(d); | |
381 | if (ds_bits == 1) { | |
382 | /* | |
383 | * HW bug - in ToDS mode, i.e. Rx on AP side, | |
384 | * addresses get swapped | |
385 | */ | |
386 | wil_swap_ethaddr(skb->data); | |
387 | } | |
388 | ||
389 | return skb; | |
390 | } | |
391 | ||
392 | /** | |
393 | * allocate and fill up to @count buffers in rx ring | |
394 | * buffers posted at @swtail | |
395 | */ | |
396 | static int wil_rx_refill(struct wil6210_priv *wil, int count) | |
397 | { | |
398 | struct net_device *ndev = wil_to_ndev(wil); | |
399 | struct vring *v = &wil->vring_rx; | |
400 | u32 next_tail; | |
401 | int rc = 0; | |
402 | int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ? | |
403 | WIL6210_RTAP_SIZE : 0; | |
404 | ||
405 | for (; next_tail = wil_vring_next_tail(v), | |
406 | (next_tail != v->swhead) && (count-- > 0); | |
407 | v->swtail = next_tail) { | |
408 | rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom); | |
409 | if (rc) { | |
410 | wil_err(wil, "Error %d in wil_rx_refill[%d]\n", | |
411 | rc, v->swtail); | |
412 | break; | |
413 | } | |
414 | } | |
415 | iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail)); | |
416 | ||
417 | return rc; | |
418 | } | |
419 | ||
420 | /* | |
421 | * Pass Rx packet to the netif. Update statistics. | |
422 | */ | |
423 | static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev) | |
424 | { | |
425 | int rc; | |
426 | unsigned int len = skb->len; | |
427 | ||
241804cb VK |
428 | skb_orphan(skb); |
429 | ||
2be7d22f VK |
430 | if (in_interrupt()) |
431 | rc = netif_rx(skb); | |
432 | else | |
433 | rc = netif_rx_ni(skb); | |
434 | ||
435 | if (likely(rc == NET_RX_SUCCESS)) { | |
436 | ndev->stats.rx_packets++; | |
437 | ndev->stats.rx_bytes += len; | |
438 | ||
439 | } else { | |
440 | ndev->stats.rx_dropped++; | |
441 | } | |
442 | } | |
443 | ||
444 | /** | |
445 | * Proceed all completed skb's from Rx VRING | |
446 | * | |
447 | * Safe to call from IRQ | |
448 | */ | |
449 | void wil_rx_handle(struct wil6210_priv *wil) | |
450 | { | |
451 | struct net_device *ndev = wil_to_ndev(wil); | |
452 | struct vring *v = &wil->vring_rx; | |
453 | struct sk_buff *skb; | |
454 | ||
455 | if (!v->va) { | |
456 | wil_err(wil, "Rx IRQ while Rx not yet initialized\n"); | |
457 | return; | |
458 | } | |
7743882d | 459 | wil_dbg_txrx(wil, "%s()\n", __func__); |
2be7d22f | 460 | while (NULL != (skb = wil_vring_reap_rx(wil, v))) { |
7743882d | 461 | wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1, |
2be7d22f VK |
462 | skb->data, skb_headlen(skb), false); |
463 | ||
2be7d22f VK |
464 | if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { |
465 | skb->dev = ndev; | |
466 | skb_reset_mac_header(skb); | |
467 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
468 | skb->pkt_type = PACKET_OTHERHOST; | |
469 | skb->protocol = htons(ETH_P_802_2); | |
470 | ||
471 | } else { | |
472 | skb->protocol = eth_type_trans(skb, ndev); | |
473 | } | |
474 | ||
475 | wil_netif_rx_any(skb, ndev); | |
476 | } | |
477 | wil_rx_refill(wil, v->size); | |
478 | } | |
479 | ||
480 | int wil_rx_init(struct wil6210_priv *wil) | |
481 | { | |
2be7d22f VK |
482 | struct vring *vring = &wil->vring_rx; |
483 | int rc; | |
2be7d22f VK |
484 | |
485 | vring->size = WIL6210_RX_RING_SIZE; | |
486 | rc = wil_vring_alloc(wil, vring); | |
487 | if (rc) | |
488 | return rc; | |
489 | ||
47e19af9 | 490 | rc = wmi_rx_chain_add(wil, vring); |
2be7d22f VK |
491 | if (rc) |
492 | goto err_free; | |
493 | ||
2be7d22f VK |
494 | rc = wil_rx_refill(wil, vring->size); |
495 | if (rc) | |
496 | goto err_free; | |
497 | ||
498 | return 0; | |
499 | err_free: | |
500 | wil_vring_free(wil, vring, 0); | |
501 | ||
502 | return rc; | |
503 | } | |
504 | ||
505 | void wil_rx_fini(struct wil6210_priv *wil) | |
506 | { | |
507 | struct vring *vring = &wil->vring_rx; | |
508 | ||
2acb4220 | 509 | if (vring->va) |
2be7d22f | 510 | wil_vring_free(wil, vring, 0); |
2be7d22f VK |
511 | } |
512 | ||
513 | int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size, | |
514 | int cid, int tid) | |
515 | { | |
516 | int rc; | |
517 | struct wmi_vring_cfg_cmd cmd = { | |
518 | .action = cpu_to_le32(WMI_VRING_CMD_ADD), | |
519 | .vring_cfg = { | |
520 | .tx_sw_ring = { | |
521 | .max_mpdu_size = cpu_to_le16(TX_BUF_LEN), | |
522 | }, | |
523 | .ringid = id, | |
524 | .cidxtid = (cid & 0xf) | ((tid & 0xf) << 4), | |
525 | .encap_trans_type = WMI_VRING_ENC_TYPE_802_3, | |
526 | .mac_ctrl = 0, | |
527 | .to_resolution = 0, | |
528 | .agg_max_wsize = 16, | |
529 | .schd_params = { | |
530 | .priority = cpu_to_le16(0), | |
531 | .timeslot_us = cpu_to_le16(0xfff), | |
532 | }, | |
533 | }, | |
534 | }; | |
535 | struct { | |
536 | struct wil6210_mbox_hdr_wmi wmi; | |
537 | struct wmi_vring_cfg_done_event cmd; | |
538 | } __packed reply; | |
539 | struct vring *vring = &wil->vring_tx[id]; | |
540 | ||
541 | if (vring->va) { | |
542 | wil_err(wil, "Tx ring [%d] already allocated\n", id); | |
543 | rc = -EINVAL; | |
544 | goto out; | |
545 | } | |
546 | ||
547 | vring->size = size; | |
548 | rc = wil_vring_alloc(wil, vring); | |
549 | if (rc) | |
550 | goto out; | |
551 | ||
552 | cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa); | |
553 | cmd.vring_cfg.tx_sw_ring.ring_size = cpu_to_le16(vring->size); | |
554 | ||
555 | rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd), | |
556 | WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100); | |
557 | if (rc) | |
558 | goto out_free; | |
559 | ||
b8023177 | 560 | if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) { |
2be7d22f VK |
561 | wil_err(wil, "Tx config failed, status 0x%02x\n", |
562 | reply.cmd.status); | |
c331997b | 563 | rc = -EINVAL; |
2be7d22f VK |
564 | goto out_free; |
565 | } | |
566 | vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr); | |
567 | ||
568 | return 0; | |
569 | out_free: | |
570 | wil_vring_free(wil, vring, 1); | |
571 | out: | |
572 | ||
573 | return rc; | |
574 | } | |
575 | ||
576 | void wil_vring_fini_tx(struct wil6210_priv *wil, int id) | |
577 | { | |
578 | struct vring *vring = &wil->vring_tx[id]; | |
579 | ||
580 | if (!vring->va) | |
581 | return; | |
582 | ||
583 | wil_vring_free(wil, vring, 1); | |
584 | } | |
585 | ||
586 | static struct vring *wil_find_tx_vring(struct wil6210_priv *wil, | |
587 | struct sk_buff *skb) | |
588 | { | |
589 | struct vring *v = &wil->vring_tx[0]; | |
590 | ||
591 | if (v->va) | |
592 | return v; | |
593 | ||
594 | return NULL; | |
595 | } | |
596 | ||
597 | static int wil_tx_desc_map(volatile struct vring_tx_desc *d, | |
598 | dma_addr_t pa, u32 len) | |
599 | { | |
600 | d->dma.addr_low = lower_32_bits(pa); | |
601 | d->dma.addr_high = (u16)upper_32_bits(pa); | |
602 | d->dma.ip_length = 0; | |
603 | /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/ | |
604 | d->dma.b11 = 0/*14 | BIT(7)*/; | |
605 | d->dma.error = 0; | |
606 | d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */ | |
607 | d->dma.length = len; | |
608 | d->dma.d0 = 0; | |
609 | d->mac.d[0] = 0; | |
610 | d->mac.d[1] = 0; | |
611 | d->mac.d[2] = 0; | |
612 | d->mac.ucode_cmd = 0; | |
613 | /* use dst index 0 */ | |
614 | d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) | | |
615 | (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS); | |
616 | /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */ | |
617 | d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) | | |
618 | (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS); | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring, | |
624 | struct sk_buff *skb) | |
625 | { | |
626 | struct device *dev = wil_to_dev(wil); | |
627 | volatile struct vring_tx_desc *d; | |
628 | u32 swhead = vring->swhead; | |
629 | int avail = wil_vring_avail_tx(vring); | |
630 | int nr_frags = skb_shinfo(skb)->nr_frags; | |
631 | uint f; | |
632 | int vring_index = vring - wil->vring_tx; | |
633 | uint i = swhead; | |
634 | dma_addr_t pa; | |
635 | ||
7743882d | 636 | wil_dbg_txrx(wil, "%s()\n", __func__); |
2be7d22f VK |
637 | |
638 | if (avail < vring->size/8) | |
639 | netif_tx_stop_all_queues(wil_to_ndev(wil)); | |
640 | if (avail < 1 + nr_frags) { | |
641 | wil_err(wil, "Tx ring full. No space for %d fragments\n", | |
642 | 1 + nr_frags); | |
643 | return -ENOMEM; | |
644 | } | |
645 | d = &(vring->va[i].tx); | |
646 | ||
647 | /* FIXME FW can accept only unicast frames for the peer */ | |
648 | memcpy(skb->data, wil->dst_addr[vring_index], ETH_ALEN); | |
649 | ||
650 | pa = dma_map_single(dev, skb->data, | |
651 | skb_headlen(skb), DMA_TO_DEVICE); | |
652 | ||
7743882d | 653 | wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb), |
2be7d22f | 654 | skb->data, (unsigned long long)pa); |
7743882d | 655 | wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1, |
2be7d22f VK |
656 | skb->data, skb_headlen(skb), false); |
657 | ||
658 | if (unlikely(dma_mapping_error(dev, pa))) | |
659 | return -EINVAL; | |
660 | /* 1-st segment */ | |
661 | wil_tx_desc_map(d, pa, skb_headlen(skb)); | |
662 | d->mac.d[2] |= ((nr_frags + 1) << | |
663 | MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS); | |
664 | /* middle segments */ | |
665 | for (f = 0; f < nr_frags; f++) { | |
666 | const struct skb_frag_struct *frag = | |
667 | &skb_shinfo(skb)->frags[f]; | |
668 | int len = skb_frag_size(frag); | |
669 | i = (swhead + f + 1) % vring->size; | |
670 | d = &(vring->va[i].tx); | |
671 | pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), | |
672 | DMA_TO_DEVICE); | |
673 | if (unlikely(dma_mapping_error(dev, pa))) | |
674 | goto dma_error; | |
675 | wil_tx_desc_map(d, pa, len); | |
676 | vring->ctx[i] = NULL; | |
677 | } | |
678 | /* for the last seg only */ | |
679 | d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS); | |
680 | d->dma.d0 |= BIT(9); /* BUG: undocumented bit */ | |
681 | d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS); | |
682 | d->dma.d0 |= (vring_index << DMA_CFG_DESC_TX_0_QID_POS); | |
683 | ||
7743882d | 684 | wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4, |
2be7d22f VK |
685 | (const void *)d, sizeof(*d), false); |
686 | ||
687 | /* advance swhead */ | |
688 | wil_vring_advance_head(vring, nr_frags + 1); | |
7743882d | 689 | wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead); |
2be7d22f VK |
690 | iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail)); |
691 | /* hold reference to skb | |
692 | * to prevent skb release before accounting | |
693 | * in case of immediate "tx done" | |
694 | */ | |
695 | vring->ctx[i] = skb_get(skb); | |
696 | ||
697 | return 0; | |
698 | dma_error: | |
699 | /* unmap what we have mapped */ | |
700 | /* Note: increment @f to operate with positive index */ | |
701 | for (f++; f > 0; f--) { | |
702 | i = (swhead + f) % vring->size; | |
703 | d = &(vring->va[i].tx); | |
704 | d->dma.status = TX_DMA_STATUS_DU; | |
705 | pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32); | |
706 | if (vring->ctx[i]) | |
707 | dma_unmap_single(dev, pa, d->dma.length, DMA_TO_DEVICE); | |
708 | else | |
709 | dma_unmap_page(dev, pa, d->dma.length, DMA_TO_DEVICE); | |
710 | } | |
711 | ||
712 | return -EINVAL; | |
713 | } | |
714 | ||
715 | ||
716 | netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev) | |
717 | { | |
718 | struct wil6210_priv *wil = ndev_to_wil(ndev); | |
719 | struct vring *vring; | |
720 | int rc; | |
721 | ||
7743882d | 722 | wil_dbg_txrx(wil, "%s()\n", __func__); |
2be7d22f VK |
723 | if (!test_bit(wil_status_fwready, &wil->status)) { |
724 | wil_err(wil, "FW not ready\n"); | |
725 | goto drop; | |
726 | } | |
727 | if (!test_bit(wil_status_fwconnected, &wil->status)) { | |
728 | wil_err(wil, "FW not connected\n"); | |
729 | goto drop; | |
730 | } | |
731 | if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { | |
732 | wil_err(wil, "Xmit in monitor mode not supported\n"); | |
733 | goto drop; | |
734 | } | |
735 | if (skb->protocol == cpu_to_be16(ETH_P_PAE)) { | |
736 | rc = wmi_tx_eapol(wil, skb); | |
737 | } else { | |
738 | /* find vring */ | |
739 | vring = wil_find_tx_vring(wil, skb); | |
740 | if (!vring) { | |
741 | wil_err(wil, "No Tx VRING available\n"); | |
742 | goto drop; | |
743 | } | |
744 | /* set up vring entry */ | |
745 | rc = wil_tx_vring(wil, vring, skb); | |
746 | } | |
747 | switch (rc) { | |
748 | case 0: | |
795ce734 | 749 | /* statistics will be updated on the tx_complete */ |
2be7d22f VK |
750 | dev_kfree_skb_any(skb); |
751 | return NETDEV_TX_OK; | |
752 | case -ENOMEM: | |
753 | return NETDEV_TX_BUSY; | |
754 | default: | |
afda8bb5 | 755 | break; /* goto drop; */ |
2be7d22f VK |
756 | } |
757 | drop: | |
758 | netif_tx_stop_all_queues(ndev); | |
759 | ndev->stats.tx_dropped++; | |
760 | dev_kfree_skb_any(skb); | |
761 | ||
762 | return NET_XMIT_DROP; | |
763 | } | |
764 | ||
765 | /** | |
766 | * Clean up transmitted skb's from the Tx VRING | |
767 | * | |
768 | * Safe to call from IRQ | |
769 | */ | |
770 | void wil_tx_complete(struct wil6210_priv *wil, int ringid) | |
771 | { | |
795ce734 | 772 | struct net_device *ndev = wil_to_ndev(wil); |
2be7d22f VK |
773 | struct device *dev = wil_to_dev(wil); |
774 | struct vring *vring = &wil->vring_tx[ringid]; | |
775 | ||
776 | if (!vring->va) { | |
777 | wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid); | |
778 | return; | |
779 | } | |
780 | ||
7743882d | 781 | wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid); |
2be7d22f VK |
782 | |
783 | while (!wil_vring_is_empty(vring)) { | |
784 | volatile struct vring_tx_desc *d = &vring->va[vring->swtail].tx; | |
785 | dma_addr_t pa; | |
786 | struct sk_buff *skb; | |
787 | if (!(d->dma.status & TX_DMA_STATUS_DU)) | |
788 | break; | |
789 | ||
7743882d | 790 | wil_dbg_txrx(wil, |
2be7d22f VK |
791 | "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n", |
792 | vring->swtail, d->dma.length, d->dma.status, | |
793 | d->dma.error); | |
7743882d | 794 | wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4, |
2be7d22f VK |
795 | (const void *)d, sizeof(*d), false); |
796 | ||
797 | pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32); | |
798 | skb = vring->ctx[vring->swtail]; | |
799 | if (skb) { | |
795ce734 VK |
800 | if (d->dma.error == 0) { |
801 | ndev->stats.tx_packets++; | |
802 | ndev->stats.tx_bytes += skb->len; | |
803 | } else { | |
804 | ndev->stats.tx_errors++; | |
805 | } | |
806 | ||
2be7d22f VK |
807 | dma_unmap_single(dev, pa, d->dma.length, DMA_TO_DEVICE); |
808 | dev_kfree_skb_any(skb); | |
809 | vring->ctx[vring->swtail] = NULL; | |
810 | } else { | |
811 | dma_unmap_page(dev, pa, d->dma.length, DMA_TO_DEVICE); | |
812 | } | |
813 | d->dma.addr_low = 0; | |
814 | d->dma.addr_high = 0; | |
815 | d->dma.length = 0; | |
816 | d->dma.status = TX_DMA_STATUS_DU; | |
817 | vring->swtail = wil_vring_next_tail(vring); | |
818 | } | |
819 | if (wil_vring_avail_tx(vring) > vring->size/4) | |
820 | netif_tx_wake_all_queues(wil_to_ndev(wil)); | |
821 | } |