• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <linux/etherdevice.h>
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 
wil_vring_is_empty(struct vring * vring)31 static inline int wil_vring_is_empty(struct vring *vring)
32 {
33 	return vring->swhead == vring->swtail;
34 }
35 
wil_vring_next_tail(struct vring * vring)36 static inline u32 wil_vring_next_tail(struct vring *vring)
37 {
38 	return (vring->swtail + 1) % vring->size;
39 }
40 
wil_vring_advance_head(struct vring * vring,int n)41 static inline void wil_vring_advance_head(struct vring *vring, int n)
42 {
43 	vring->swhead = (vring->swhead + n) % vring->size;
44 }
45 
wil_vring_is_full(struct vring * vring)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  */
wil_vring_avail_tx(struct vring * vring)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 
wil_vring_alloc(struct wil6210_priv * wil,struct vring * vring)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) {
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 		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 
96 	wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size,
97 		     vring->va, (unsigned long long)vring->pa, vring->ctx);
98 
99 	return 0;
100 }
101 
wil_vring_free(struct wil6210_priv * wil,struct vring * vring,int tx)102 static 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)) {
109 		if (tx) {
110 			volatile struct vring_tx_desc *d =
111 					&vring->va[vring->swtail].tx;
112 			dma_addr_t pa = d->dma.addr_low |
113 					((u64)d->dma.addr_high << 32);
114 			struct sk_buff *skb = vring->ctx[vring->swtail];
115 			if (skb) {
116 				dma_unmap_single(dev, pa, d->dma.length,
117 						 DMA_TO_DEVICE);
118 				dev_kfree_skb_any(skb);
119 				vring->ctx[vring->swtail] = NULL;
120 			} else {
121 				dma_unmap_page(dev, pa, d->dma.length,
122 					       DMA_TO_DEVICE);
123 			}
124 			vring->swtail = wil_vring_next_tail(vring);
125 		} else { /* rx */
126 			volatile struct vring_rx_desc *d =
127 					&vring->va[vring->swtail].rx;
128 			dma_addr_t pa = d->dma.addr_low |
129 					((u64)d->dma.addr_high << 32);
130 			struct sk_buff *skb = vring->ctx[vring->swhead];
131 			dma_unmap_single(dev, pa, d->dma.length,
132 					 DMA_FROM_DEVICE);
133 			kfree_skb(skb);
134 			wil_vring_advance_head(vring, 1);
135 		}
136 	}
137 	dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
138 	kfree(vring->ctx);
139 	vring->pa = 0;
140 	vring->va = NULL;
141 	vring->ctx = NULL;
142 }
143 
144 /**
145  * Allocate one skb for Rx VRING
146  *
147  * Safe to call from IRQ
148  */
wil_vring_alloc_skb(struct wil6210_priv * wil,struct vring * vring,u32 i,int headroom)149 static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
150 			       u32 i, int headroom)
151 {
152 	struct device *dev = wil_to_dev(wil);
153 	unsigned int sz = RX_BUF_LEN;
154 	volatile struct vring_rx_desc *d = &(vring->va[i].rx);
155 	dma_addr_t pa;
156 
157 	/* TODO align */
158 	struct sk_buff *skb = dev_alloc_skb(sz + headroom);
159 	if (unlikely(!skb))
160 		return -ENOMEM;
161 
162 	skb_reserve(skb, headroom);
163 	skb_put(skb, sz);
164 
165 	pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
166 	if (unlikely(dma_mapping_error(dev, pa))) {
167 		kfree_skb(skb);
168 		return -ENOMEM;
169 	}
170 
171 	d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
172 	d->dma.addr_low = lower_32_bits(pa);
173 	d->dma.addr_high = (u16)upper_32_bits(pa);
174 	/* ip_length don't care */
175 	/* b11 don't care */
176 	/* error don't care */
177 	d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
178 	d->dma.length = sz;
179 	vring->ctx[i] = skb;
180 
181 	return 0;
182 }
183 
184 /**
185  * Adds radiotap header
186  *
187  * Any error indicated as "Bad FCS"
188  *
189  * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
190  *  - Rx descriptor: 32 bytes
191  *  - Phy info
192  */
wil_rx_add_radiotap_header(struct wil6210_priv * wil,struct sk_buff * skb)193 static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
194 				       struct sk_buff *skb)
195 {
196 	struct wireless_dev *wdev = wil->wdev;
197 	struct wil6210_rtap {
198 		struct ieee80211_radiotap_header rthdr;
199 		/* fields should be in the order of bits in rthdr.it_present */
200 		/* flags */
201 		u8 flags;
202 		/* channel */
203 		__le16 chnl_freq __aligned(2);
204 		__le16 chnl_flags;
205 		/* MCS */
206 		u8 mcs_present;
207 		u8 mcs_flags;
208 		u8 mcs_index;
209 	} __packed;
210 	struct wil6210_rtap_vendor {
211 		struct wil6210_rtap rtap;
212 		/* vendor */
213 		u8 vendor_oui[3] __aligned(2);
214 		u8 vendor_ns;
215 		__le16 vendor_skip;
216 		u8 vendor_data[0];
217 	} __packed;
218 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
219 	struct wil6210_rtap_vendor *rtap_vendor;
220 	int rtap_len = sizeof(struct wil6210_rtap);
221 	int phy_length = 0; /* phy info header size, bytes */
222 	static char phy_data[128];
223 	struct ieee80211_channel *ch = wdev->preset_chandef.chan;
224 
225 	if (rtap_include_phy_info) {
226 		rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
227 		/* calculate additional length */
228 		if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
229 			/**
230 			 * PHY info starts from 8-byte boundary
231 			 * there are 8-byte lines, last line may be partially
232 			 * written (HW bug), thus FW configures for last line
233 			 * to be excessive. Driver skips this last line.
234 			 */
235 			int len = min_t(int, 8 + sizeof(phy_data),
236 					wil_rxdesc_phy_length(d));
237 			if (len > 8) {
238 				void *p = skb_tail_pointer(skb);
239 				void *pa = PTR_ALIGN(p, 8);
240 				if (skb_tailroom(skb) >= len + (pa - p)) {
241 					phy_length = len - 8;
242 					memcpy(phy_data, pa, phy_length);
243 				}
244 			}
245 		}
246 		rtap_len += phy_length;
247 	}
248 
249 	if (skb_headroom(skb) < rtap_len &&
250 	    pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
251 		wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
252 		return;
253 	}
254 
255 	rtap_vendor = (void *)skb_push(skb, rtap_len);
256 	memset(rtap_vendor, 0, rtap_len);
257 
258 	rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
259 	rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
260 	rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
261 			(1 << IEEE80211_RADIOTAP_FLAGS) |
262 			(1 << IEEE80211_RADIOTAP_CHANNEL) |
263 			(1 << IEEE80211_RADIOTAP_MCS));
264 	if (d->dma.status & RX_DMA_STATUS_ERROR)
265 		rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
266 
267 	rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
268 	rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
269 
270 	rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
271 	rtap_vendor->rtap.mcs_flags = 0;
272 	rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
273 
274 	if (rtap_include_phy_info) {
275 		rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
276 				IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
277 		/* OUI for Wilocity 04:ce:14 */
278 		rtap_vendor->vendor_oui[0] = 0x04;
279 		rtap_vendor->vendor_oui[1] = 0xce;
280 		rtap_vendor->vendor_oui[2] = 0x14;
281 		rtap_vendor->vendor_ns = 1;
282 		/* Rx descriptor + PHY data  */
283 		rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
284 						       phy_length);
285 		memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
286 		memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
287 		       phy_length);
288 	}
289 }
290 
291 /*
292  * Fast swap in place between 2 registers
293  */
wil_swap_u16(u16 * a,u16 * b)294 static void wil_swap_u16(u16 *a, u16 *b)
295 {
296 	*a ^= *b;
297 	*b ^= *a;
298 	*a ^= *b;
299 }
300 
wil_swap_ethaddr(void * data)301 static void wil_swap_ethaddr(void *data)
302 {
303 	struct ethhdr *eth = data;
304 	u16 *s = (u16 *)eth->h_source;
305 	u16 *d = (u16 *)eth->h_dest;
306 
307 	wil_swap_u16(s++, d++);
308 	wil_swap_u16(s++, d++);
309 	wil_swap_u16(s, d);
310 }
311 
312 /**
313  * reap 1 frame from @swhead
314  *
315  * Rx descriptor copied to skb->cb
316  *
317  * Safe to call from IRQ
318  */
wil_vring_reap_rx(struct wil6210_priv * wil,struct vring * vring)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 vring_rx_desc *d1;
326 	struct sk_buff *skb;
327 	dma_addr_t pa;
328 	unsigned int sz = RX_BUF_LEN;
329 	u8 ftype;
330 	u8 ds_bits;
331 
332 	BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
333 
334 	if (wil_vring_is_empty(vring))
335 		return NULL;
336 
337 	d = &(vring->va[vring->swhead].rx);
338 	if (!(d->dma.status & RX_DMA_STATUS_DU)) {
339 		/* it is not error, we just reached end of Rx done area */
340 		return NULL;
341 	}
342 
343 	pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
344 	skb = vring->ctx[vring->swhead];
345 	dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
346 	skb_trim(skb, d->dma.length);
347 
348 	d1 = wil_skb_rxdesc(skb);
349 	*d1 = *d;
350 
351 	wil->stats.last_mcs_rx = wil_rxdesc_mcs(d1);
352 
353 	/* use radiotap header only if required */
354 	if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
355 		wil_rx_add_radiotap_header(wil, skb);
356 
357 	wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, d->dma.length);
358 	wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
359 			  (const void *)d, sizeof(*d), false);
360 
361 	wil_vring_advance_head(vring, 1);
362 
363 	/* no extra checks if in sniffer mode */
364 	if (ndev->type != ARPHRD_ETHER)
365 		return skb;
366 	/*
367 	 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
368 	 * Driver should recognize it by frame type, that is found
369 	 * in Rx descriptor. If type is not data, it is 802.11 frame as is
370 	 */
371 	ftype = wil_rxdesc_ftype(d1) << 2;
372 	if (ftype != IEEE80211_FTYPE_DATA) {
373 		wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
374 		/* TODO: process it */
375 		kfree_skb(skb);
376 		return NULL;
377 	}
378 
379 	if (skb->len < ETH_HLEN) {
380 		wil_err(wil, "Short frame, len = %d\n", skb->len);
381 		/* TODO: process it (i.e. BAR) */
382 		kfree_skb(skb);
383 		return NULL;
384 	}
385 
386 	ds_bits = wil_rxdesc_ds_bits(d1);
387 	if (ds_bits == 1) {
388 		/*
389 		 * HW bug - in ToDS mode, i.e. Rx on AP side,
390 		 * addresses get swapped
391 		 */
392 		wil_swap_ethaddr(skb->data);
393 	}
394 
395 	return skb;
396 }
397 
398 /**
399  * allocate and fill up to @count buffers in rx ring
400  * buffers posted at @swtail
401  */
wil_rx_refill(struct wil6210_priv * wil,int count)402 static int wil_rx_refill(struct wil6210_priv *wil, int count)
403 {
404 	struct net_device *ndev = wil_to_ndev(wil);
405 	struct vring *v = &wil->vring_rx;
406 	u32 next_tail;
407 	int rc = 0;
408 	int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
409 			WIL6210_RTAP_SIZE : 0;
410 
411 	for (; next_tail = wil_vring_next_tail(v),
412 			(next_tail != v->swhead) && (count-- > 0);
413 			v->swtail = next_tail) {
414 		rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
415 		if (rc) {
416 			wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
417 				rc, v->swtail);
418 			break;
419 		}
420 	}
421 	iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
422 
423 	return rc;
424 }
425 
426 /*
427  * Pass Rx packet to the netif. Update statistics.
428  */
wil_netif_rx_any(struct sk_buff * skb,struct net_device * ndev)429 static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
430 {
431 	int rc;
432 	unsigned int len = skb->len;
433 
434 	skb_orphan(skb);
435 
436 	if (in_interrupt())
437 		rc = netif_rx(skb);
438 	else
439 		rc = netif_rx_ni(skb);
440 
441 	if (likely(rc == NET_RX_SUCCESS)) {
442 		ndev->stats.rx_packets++;
443 		ndev->stats.rx_bytes += len;
444 
445 	} else {
446 		ndev->stats.rx_dropped++;
447 	}
448 }
449 
450 /**
451  * Proceed all completed skb's from Rx VRING
452  *
453  * Safe to call from IRQ
454  */
wil_rx_handle(struct wil6210_priv * wil)455 void wil_rx_handle(struct wil6210_priv *wil)
456 {
457 	struct net_device *ndev = wil_to_ndev(wil);
458 	struct vring *v = &wil->vring_rx;
459 	struct sk_buff *skb;
460 
461 	if (!v->va) {
462 		wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
463 		return;
464 	}
465 	wil_dbg_txrx(wil, "%s()\n", __func__);
466 	while (NULL != (skb = wil_vring_reap_rx(wil, v))) {
467 		wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
468 				  skb->data, skb_headlen(skb), false);
469 
470 		if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
471 			skb->dev = ndev;
472 			skb_reset_mac_header(skb);
473 			skb->ip_summed = CHECKSUM_UNNECESSARY;
474 			skb->pkt_type = PACKET_OTHERHOST;
475 			skb->protocol = htons(ETH_P_802_2);
476 
477 		} else {
478 			skb->protocol = eth_type_trans(skb, ndev);
479 		}
480 
481 		wil_netif_rx_any(skb, ndev);
482 	}
483 	wil_rx_refill(wil, v->size);
484 }
485 
wil_rx_init(struct wil6210_priv * wil)486 int wil_rx_init(struct wil6210_priv *wil)
487 {
488 	struct vring *vring = &wil->vring_rx;
489 	int rc;
490 
491 	vring->size = WIL6210_RX_RING_SIZE;
492 	rc = wil_vring_alloc(wil, vring);
493 	if (rc)
494 		return rc;
495 
496 	rc = wmi_rx_chain_add(wil, vring);
497 	if (rc)
498 		goto err_free;
499 
500 	rc = wil_rx_refill(wil, vring->size);
501 	if (rc)
502 		goto err_free;
503 
504 	return 0;
505  err_free:
506 	wil_vring_free(wil, vring, 0);
507 
508 	return rc;
509 }
510 
wil_rx_fini(struct wil6210_priv * wil)511 void wil_rx_fini(struct wil6210_priv *wil)
512 {
513 	struct vring *vring = &wil->vring_rx;
514 
515 	if (vring->va)
516 		wil_vring_free(wil, vring, 0);
517 }
518 
wil_vring_init_tx(struct wil6210_priv * wil,int id,int size,int cid,int tid)519 int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
520 		      int cid, int tid)
521 {
522 	int rc;
523 	struct wmi_vring_cfg_cmd cmd = {
524 		.action = cpu_to_le32(WMI_VRING_CMD_ADD),
525 		.vring_cfg = {
526 			.tx_sw_ring = {
527 				.max_mpdu_size = cpu_to_le16(TX_BUF_LEN),
528 				.ring_size = cpu_to_le16(size),
529 			},
530 			.ringid = id,
531 			.cidxtid = (cid & 0xf) | ((tid & 0xf) << 4),
532 			.encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
533 			.mac_ctrl = 0,
534 			.to_resolution = 0,
535 			.agg_max_wsize = 16,
536 			.schd_params = {
537 				.priority = cpu_to_le16(0),
538 				.timeslot_us = cpu_to_le16(0xfff),
539 			},
540 		},
541 	};
542 	struct {
543 		struct wil6210_mbox_hdr_wmi wmi;
544 		struct wmi_vring_cfg_done_event cmd;
545 	} __packed reply;
546 	struct vring *vring = &wil->vring_tx[id];
547 
548 	if (vring->va) {
549 		wil_err(wil, "Tx ring [%d] already allocated\n", id);
550 		rc = -EINVAL;
551 		goto out;
552 	}
553 
554 	vring->size = size;
555 	rc = wil_vring_alloc(wil, vring);
556 	if (rc)
557 		goto out;
558 
559 	cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
560 
561 	rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
562 		      WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
563 	if (rc)
564 		goto out_free;
565 
566 	if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
567 		wil_err(wil, "Tx config failed, status 0x%02x\n",
568 			reply.cmd.status);
569 		rc = -EINVAL;
570 		goto out_free;
571 	}
572 	vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
573 
574 	return 0;
575  out_free:
576 	wil_vring_free(wil, vring, 1);
577  out:
578 
579 	return rc;
580 }
581 
wil_vring_fini_tx(struct wil6210_priv * wil,int id)582 void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
583 {
584 	struct vring *vring = &wil->vring_tx[id];
585 
586 	if (!vring->va)
587 		return;
588 
589 	wil_vring_free(wil, vring, 1);
590 }
591 
wil_find_tx_vring(struct wil6210_priv * wil,struct sk_buff * skb)592 static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
593 				       struct sk_buff *skb)
594 {
595 	struct vring *v = &wil->vring_tx[0];
596 
597 	if (v->va)
598 		return v;
599 
600 	return NULL;
601 }
602 
wil_tx_desc_map(volatile struct vring_tx_desc * d,dma_addr_t pa,u32 len)603 static int wil_tx_desc_map(volatile struct vring_tx_desc *d,
604 			   dma_addr_t pa, u32 len)
605 {
606 	d->dma.addr_low = lower_32_bits(pa);
607 	d->dma.addr_high = (u16)upper_32_bits(pa);
608 	d->dma.ip_length = 0;
609 	/* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
610 	d->dma.b11 = 0/*14 | BIT(7)*/;
611 	d->dma.error = 0;
612 	d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
613 	d->dma.length = len;
614 	d->dma.d0 = 0;
615 	d->mac.d[0] = 0;
616 	d->mac.d[1] = 0;
617 	d->mac.d[2] = 0;
618 	d->mac.ucode_cmd = 0;
619 	/* use dst index 0 */
620 	d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
621 		       (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
622 	/* translation type:  0 - bypass; 1 - 802.3; 2 - native wifi */
623 	d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
624 		      (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
625 
626 	return 0;
627 }
628 
wil_tx_vring(struct wil6210_priv * wil,struct vring * vring,struct sk_buff * skb)629 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
630 			struct sk_buff *skb)
631 {
632 	struct device *dev = wil_to_dev(wil);
633 	volatile struct vring_tx_desc *d;
634 	u32 swhead = vring->swhead;
635 	int avail = wil_vring_avail_tx(vring);
636 	int nr_frags = skb_shinfo(skb)->nr_frags;
637 	uint f;
638 	int vring_index = vring - wil->vring_tx;
639 	uint i = swhead;
640 	dma_addr_t pa;
641 
642 	wil_dbg_txrx(wil, "%s()\n", __func__);
643 
644 	if (avail < vring->size/8)
645 		netif_tx_stop_all_queues(wil_to_ndev(wil));
646 	if (avail < 1 + nr_frags) {
647 		wil_err(wil, "Tx ring full. No space for %d fragments\n",
648 			1 + nr_frags);
649 		return -ENOMEM;
650 	}
651 	d = &(vring->va[i].tx);
652 
653 	/* FIXME FW can accept only unicast frames for the peer */
654 	memcpy(skb->data, wil->dst_addr[vring_index], ETH_ALEN);
655 
656 	pa = dma_map_single(dev, skb->data,
657 			skb_headlen(skb), DMA_TO_DEVICE);
658 
659 	wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb),
660 		     skb->data, (unsigned long long)pa);
661 	wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
662 			  skb->data, skb_headlen(skb), false);
663 
664 	if (unlikely(dma_mapping_error(dev, pa)))
665 		return -EINVAL;
666 	/* 1-st segment */
667 	wil_tx_desc_map(d, pa, skb_headlen(skb));
668 	d->mac.d[2] |= ((nr_frags + 1) <<
669 		       MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
670 	/* middle segments */
671 	for (f = 0; f < nr_frags; f++) {
672 		const struct skb_frag_struct *frag =
673 				&skb_shinfo(skb)->frags[f];
674 		int len = skb_frag_size(frag);
675 		i = (swhead + f + 1) % vring->size;
676 		d = &(vring->va[i].tx);
677 		pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
678 				DMA_TO_DEVICE);
679 		if (unlikely(dma_mapping_error(dev, pa)))
680 			goto dma_error;
681 		wil_tx_desc_map(d, pa, len);
682 		vring->ctx[i] = NULL;
683 	}
684 	/* for the last seg only */
685 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
686 	d->dma.d0 |= BIT(9); /* BUG: undocumented bit */
687 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
688 	d->dma.d0 |= (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
689 
690 	wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4,
691 			  (const void *)d, sizeof(*d), false);
692 
693 	/* advance swhead */
694 	wil_vring_advance_head(vring, nr_frags + 1);
695 	wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
696 	iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
697 	/* hold reference to skb
698 	 * to prevent skb release before accounting
699 	 * in case of immediate "tx done"
700 	 */
701 	vring->ctx[i] = skb_get(skb);
702 
703 	return 0;
704  dma_error:
705 	/* unmap what we have mapped */
706 	/* Note: increment @f to operate with positive index */
707 	for (f++; f > 0; f--) {
708 		i = (swhead + f) % vring->size;
709 		d = &(vring->va[i].tx);
710 		d->dma.status = TX_DMA_STATUS_DU;
711 		pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
712 		if (vring->ctx[i])
713 			dma_unmap_single(dev, pa, d->dma.length, DMA_TO_DEVICE);
714 		else
715 			dma_unmap_page(dev, pa, d->dma.length, DMA_TO_DEVICE);
716 	}
717 
718 	return -EINVAL;
719 }
720 
721 
wil_start_xmit(struct sk_buff * skb,struct net_device * ndev)722 netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
723 {
724 	struct wil6210_priv *wil = ndev_to_wil(ndev);
725 	struct vring *vring;
726 	int rc;
727 
728 	wil_dbg_txrx(wil, "%s()\n", __func__);
729 	if (!test_bit(wil_status_fwready, &wil->status)) {
730 		wil_err(wil, "FW not ready\n");
731 		goto drop;
732 	}
733 	if (!test_bit(wil_status_fwconnected, &wil->status)) {
734 		wil_err(wil, "FW not connected\n");
735 		goto drop;
736 	}
737 	if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
738 		wil_err(wil, "Xmit in monitor mode not supported\n");
739 		goto drop;
740 	}
741 	if (skb->protocol == cpu_to_be16(ETH_P_PAE)) {
742 		rc = wmi_tx_eapol(wil, skb);
743 	} else {
744 		/* find vring */
745 		vring = wil_find_tx_vring(wil, skb);
746 		if (!vring) {
747 			wil_err(wil, "No Tx VRING available\n");
748 			goto drop;
749 		}
750 		/* set up vring entry */
751 		rc = wil_tx_vring(wil, vring, skb);
752 	}
753 	switch (rc) {
754 	case 0:
755 		/* statistics will be updated on the tx_complete */
756 		dev_kfree_skb_any(skb);
757 		return NETDEV_TX_OK;
758 	case -ENOMEM:
759 		return NETDEV_TX_BUSY;
760 	default:
761 		break; /* goto drop; */
762 	}
763  drop:
764 	netif_tx_stop_all_queues(ndev);
765 	ndev->stats.tx_dropped++;
766 	dev_kfree_skb_any(skb);
767 
768 	return NET_XMIT_DROP;
769 }
770 
771 /**
772  * Clean up transmitted skb's from the Tx VRING
773  *
774  * Safe to call from IRQ
775  */
wil_tx_complete(struct wil6210_priv * wil,int ringid)776 void wil_tx_complete(struct wil6210_priv *wil, int ringid)
777 {
778 	struct net_device *ndev = wil_to_ndev(wil);
779 	struct device *dev = wil_to_dev(wil);
780 	struct vring *vring = &wil->vring_tx[ringid];
781 
782 	if (!vring->va) {
783 		wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
784 		return;
785 	}
786 
787 	wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
788 
789 	while (!wil_vring_is_empty(vring)) {
790 		volatile struct vring_tx_desc *d1 =
791 					      &vring->va[vring->swtail].tx;
792 		struct vring_tx_desc dd, *d = &dd;
793 		dma_addr_t pa;
794 		struct sk_buff *skb;
795 
796 		dd = *d1;
797 
798 		if (!(d->dma.status & TX_DMA_STATUS_DU))
799 			break;
800 
801 		wil_dbg_txrx(wil,
802 			     "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
803 			     vring->swtail, d->dma.length, d->dma.status,
804 			     d->dma.error);
805 		wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4,
806 				  (const void *)d, sizeof(*d), false);
807 
808 		pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32);
809 		skb = vring->ctx[vring->swtail];
810 		if (skb) {
811 			if (d->dma.error == 0) {
812 				ndev->stats.tx_packets++;
813 				ndev->stats.tx_bytes += skb->len;
814 			} else {
815 				ndev->stats.tx_errors++;
816 			}
817 
818 			dma_unmap_single(dev, pa, d->dma.length, DMA_TO_DEVICE);
819 			dev_kfree_skb_any(skb);
820 			vring->ctx[vring->swtail] = NULL;
821 		} else {
822 			dma_unmap_page(dev, pa, d->dma.length, DMA_TO_DEVICE);
823 		}
824 		d->dma.addr_low = 0;
825 		d->dma.addr_high = 0;
826 		d->dma.length = 0;
827 		d->dma.status = TX_DMA_STATUS_DU;
828 		vring->swtail = wil_vring_next_tail(vring);
829 	}
830 	if (wil_vring_avail_tx(vring) > vring->size/4)
831 		netif_tx_wake_all_queues(wil_to_ndev(wil));
832 }
833