1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
4  * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
5  */
6 
7 #include <linux/etherdevice.h>
8 #include <net/ieee80211_radiotap.h>
9 #include <linux/if_arp.h>
10 #include <linux/moduleparam.h>
11 #include <linux/ip.h>
12 #include <linux/ipv6.h>
13 #include <net/ipv6.h>
14 #include <linux/prefetch.h>
15 
16 #include "wil6210.h"
17 #include "wmi.h"
18 #include "txrx.h"
19 #include "trace.h"
20 #include "txrx_edma.h"
21 
22 bool rx_align_2;
23 module_param(rx_align_2, bool, 0444);
24 MODULE_PARM_DESC(rx_align_2, " align Rx buffers on 4*n+2, default - no");
25 
26 bool rx_large_buf;
27 module_param(rx_large_buf, bool, 0444);
28 MODULE_PARM_DESC(rx_large_buf, " allocate 8KB RX buffers, default - no");
29 
30 /* Drop Tx packets in case Tx ring is full */
31 bool drop_if_ring_full;
32 
33 static inline uint wil_rx_snaplen(void)
34 {
35 	return rx_align_2 ? 6 : 0;
36 }
37 
38 /* wil_ring_wmark_low - low watermark for available descriptor space */
39 static inline int wil_ring_wmark_low(struct wil_ring *ring)
40 {
41 	return ring->size / 8;
42 }
43 
44 /* wil_ring_wmark_high - high watermark for available descriptor space */
45 static inline int wil_ring_wmark_high(struct wil_ring *ring)
46 {
47 	return ring->size / 4;
48 }
49 
50 /* returns true if num avail descriptors is lower than wmark_low */
51 static inline int wil_ring_avail_low(struct wil_ring *ring)
52 {
53 	return wil_ring_avail_tx(ring) < wil_ring_wmark_low(ring);
54 }
55 
56 /* returns true if num avail descriptors is higher than wmark_high */
57 static inline int wil_ring_avail_high(struct wil_ring *ring)
58 {
59 	return wil_ring_avail_tx(ring) > wil_ring_wmark_high(ring);
60 }
61 
62 /* returns true when all tx vrings are empty */
63 bool wil_is_tx_idle(struct wil6210_priv *wil)
64 {
65 	int i;
66 	unsigned long data_comp_to;
67 	int min_ring_id = wil_get_min_tx_ring_id(wil);
68 
69 	for (i = min_ring_id; i < WIL6210_MAX_TX_RINGS; i++) {
70 		struct wil_ring *vring = &wil->ring_tx[i];
71 		int vring_index = vring - wil->ring_tx;
72 		struct wil_ring_tx_data *txdata =
73 			&wil->ring_tx_data[vring_index];
74 
75 		spin_lock(&txdata->lock);
76 
77 		if (!vring->va || !txdata->enabled) {
78 			spin_unlock(&txdata->lock);
79 			continue;
80 		}
81 
82 		data_comp_to = jiffies + msecs_to_jiffies(
83 					WIL_DATA_COMPLETION_TO_MS);
84 		if (test_bit(wil_status_napi_en, wil->status)) {
85 			while (!wil_ring_is_empty(vring)) {
86 				if (time_after(jiffies, data_comp_to)) {
87 					wil_dbg_pm(wil,
88 						   "TO waiting for idle tx\n");
89 					spin_unlock(&txdata->lock);
90 					return false;
91 				}
92 				wil_dbg_ratelimited(wil,
93 						    "tx vring is not empty -> NAPI\n");
94 				spin_unlock(&txdata->lock);
95 				napi_synchronize(&wil->napi_tx);
96 				msleep(20);
97 				spin_lock(&txdata->lock);
98 				if (!vring->va || !txdata->enabled)
99 					break;
100 			}
101 		}
102 
103 		spin_unlock(&txdata->lock);
104 	}
105 
106 	return true;
107 }
108 
109 static int wil_vring_alloc(struct wil6210_priv *wil, struct wil_ring *vring)
110 {
111 	struct device *dev = wil_to_dev(wil);
112 	size_t sz = vring->size * sizeof(vring->va[0]);
113 	uint i;
114 
115 	wil_dbg_misc(wil, "vring_alloc:\n");
116 
117 	BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
118 
119 	vring->swhead = 0;
120 	vring->swtail = 0;
121 	vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
122 	if (!vring->ctx) {
123 		vring->va = NULL;
124 		return -ENOMEM;
125 	}
126 
127 	/* vring->va should be aligned on its size rounded up to power of 2
128 	 * This is granted by the dma_alloc_coherent.
129 	 *
130 	 * HW has limitation that all vrings addresses must share the same
131 	 * upper 16 msb bits part of 48 bits address. To workaround that,
132 	 * if we are using more than 32 bit addresses switch to 32 bit
133 	 * allocation before allocating vring memory.
134 	 *
135 	 * There's no check for the return value of dma_set_mask_and_coherent,
136 	 * since we assume if we were able to set the mask during
137 	 * initialization in this system it will not fail if we set it again
138 	 */
139 	if (wil->dma_addr_size > 32)
140 		dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
141 
142 	vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
143 	if (!vring->va) {
144 		kfree(vring->ctx);
145 		vring->ctx = NULL;
146 		return -ENOMEM;
147 	}
148 
149 	if (wil->dma_addr_size > 32)
150 		dma_set_mask_and_coherent(dev,
151 					  DMA_BIT_MASK(wil->dma_addr_size));
152 
153 	/* initially, all descriptors are SW owned
154 	 * For Tx and Rx, ownership bit is at the same location, thus
155 	 * we can use any
156 	 */
157 	for (i = 0; i < vring->size; i++) {
158 		volatile struct vring_tx_desc *_d =
159 			&vring->va[i].tx.legacy;
160 
161 		_d->dma.status = TX_DMA_STATUS_DU;
162 	}
163 
164 	wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
165 		     vring->va, &vring->pa, vring->ctx);
166 
167 	return 0;
168 }
169 
170 static void wil_txdesc_unmap(struct device *dev, union wil_tx_desc *desc,
171 			     struct wil_ctx *ctx)
172 {
173 	struct vring_tx_desc *d = &desc->legacy;
174 	dma_addr_t pa = wil_desc_addr(&d->dma.addr);
175 	u16 dmalen = le16_to_cpu(d->dma.length);
176 
177 	switch (ctx->mapped_as) {
178 	case wil_mapped_as_single:
179 		dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
180 		break;
181 	case wil_mapped_as_page:
182 		dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
183 		break;
184 	default:
185 		break;
186 	}
187 }
188 
189 static void wil_vring_free(struct wil6210_priv *wil, struct wil_ring *vring)
190 {
191 	struct device *dev = wil_to_dev(wil);
192 	size_t sz = vring->size * sizeof(vring->va[0]);
193 
194 	lockdep_assert_held(&wil->mutex);
195 	if (!vring->is_rx) {
196 		int vring_index = vring - wil->ring_tx;
197 
198 		wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
199 			     vring_index, vring->size, vring->va,
200 			     &vring->pa, vring->ctx);
201 	} else {
202 		wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
203 			     vring->size, vring->va,
204 			     &vring->pa, vring->ctx);
205 	}
206 
207 	while (!wil_ring_is_empty(vring)) {
208 		dma_addr_t pa;
209 		u16 dmalen;
210 		struct wil_ctx *ctx;
211 
212 		if (!vring->is_rx) {
213 			struct vring_tx_desc dd, *d = &dd;
214 			volatile struct vring_tx_desc *_d =
215 					&vring->va[vring->swtail].tx.legacy;
216 
217 			ctx = &vring->ctx[vring->swtail];
218 			if (!ctx) {
219 				wil_dbg_txrx(wil,
220 					     "ctx(%d) was already completed\n",
221 					     vring->swtail);
222 				vring->swtail = wil_ring_next_tail(vring);
223 				continue;
224 			}
225 			*d = *_d;
226 			wil_txdesc_unmap(dev, (union wil_tx_desc *)d, ctx);
227 			if (ctx->skb)
228 				dev_kfree_skb_any(ctx->skb);
229 			vring->swtail = wil_ring_next_tail(vring);
230 		} else { /* rx */
231 			struct vring_rx_desc dd, *d = &dd;
232 			volatile struct vring_rx_desc *_d =
233 				&vring->va[vring->swhead].rx.legacy;
234 
235 			ctx = &vring->ctx[vring->swhead];
236 			*d = *_d;
237 			pa = wil_desc_addr(&d->dma.addr);
238 			dmalen = le16_to_cpu(d->dma.length);
239 			dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
240 			kfree_skb(ctx->skb);
241 			wil_ring_advance_head(vring, 1);
242 		}
243 	}
244 	dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
245 	kfree(vring->ctx);
246 	vring->pa = 0;
247 	vring->va = NULL;
248 	vring->ctx = NULL;
249 }
250 
251 /**
252  * Allocate one skb for Rx VRING
253  *
254  * Safe to call from IRQ
255  */
256 static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct wil_ring *vring,
257 			       u32 i, int headroom)
258 {
259 	struct device *dev = wil_to_dev(wil);
260 	unsigned int sz = wil->rx_buf_len + ETH_HLEN + wil_rx_snaplen();
261 	struct vring_rx_desc dd, *d = &dd;
262 	volatile struct vring_rx_desc *_d = &vring->va[i].rx.legacy;
263 	dma_addr_t pa;
264 	struct sk_buff *skb = dev_alloc_skb(sz + headroom);
265 
266 	if (unlikely(!skb))
267 		return -ENOMEM;
268 
269 	skb_reserve(skb, headroom);
270 	skb_put(skb, sz);
271 
272 	/**
273 	 * Make sure that the network stack calculates checksum for packets
274 	 * which failed the HW checksum calculation
275 	 */
276 	skb->ip_summed = CHECKSUM_NONE;
277 
278 	pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
279 	if (unlikely(dma_mapping_error(dev, pa))) {
280 		kfree_skb(skb);
281 		return -ENOMEM;
282 	}
283 
284 	d->dma.d0 = RX_DMA_D0_CMD_DMA_RT | RX_DMA_D0_CMD_DMA_IT;
285 	wil_desc_addr_set(&d->dma.addr, pa);
286 	/* ip_length don't care */
287 	/* b11 don't care */
288 	/* error don't care */
289 	d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
290 	d->dma.length = cpu_to_le16(sz);
291 	*_d = *d;
292 	vring->ctx[i].skb = skb;
293 
294 	return 0;
295 }
296 
297 /**
298  * Adds radiotap header
299  *
300  * Any error indicated as "Bad FCS"
301  *
302  * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
303  *  - Rx descriptor: 32 bytes
304  *  - Phy info
305  */
306 static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
307 				       struct sk_buff *skb)
308 {
309 	struct wil6210_rtap {
310 		struct ieee80211_radiotap_header rthdr;
311 		/* fields should be in the order of bits in rthdr.it_present */
312 		/* flags */
313 		u8 flags;
314 		/* channel */
315 		__le16 chnl_freq __aligned(2);
316 		__le16 chnl_flags;
317 		/* MCS */
318 		u8 mcs_present;
319 		u8 mcs_flags;
320 		u8 mcs_index;
321 	} __packed;
322 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
323 	struct wil6210_rtap *rtap;
324 	int rtap_len = sizeof(struct wil6210_rtap);
325 	struct ieee80211_channel *ch = wil->monitor_chandef.chan;
326 
327 	if (skb_headroom(skb) < rtap_len &&
328 	    pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
329 		wil_err(wil, "Unable to expand headroom to %d\n", rtap_len);
330 		return;
331 	}
332 
333 	rtap = skb_push(skb, rtap_len);
334 	memset(rtap, 0, rtap_len);
335 
336 	rtap->rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
337 	rtap->rthdr.it_len = cpu_to_le16(rtap_len);
338 	rtap->rthdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
339 			(1 << IEEE80211_RADIOTAP_CHANNEL) |
340 			(1 << IEEE80211_RADIOTAP_MCS));
341 	if (d->dma.status & RX_DMA_STATUS_ERROR)
342 		rtap->flags |= IEEE80211_RADIOTAP_F_BADFCS;
343 
344 	rtap->chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
345 	rtap->chnl_flags = cpu_to_le16(0);
346 
347 	rtap->mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
348 	rtap->mcs_flags = 0;
349 	rtap->mcs_index = wil_rxdesc_mcs(d);
350 }
351 
352 static bool wil_is_rx_idle(struct wil6210_priv *wil)
353 {
354 	struct vring_rx_desc *_d;
355 	struct wil_ring *ring = &wil->ring_rx;
356 
357 	_d = (struct vring_rx_desc *)&ring->va[ring->swhead].rx.legacy;
358 	if (_d->dma.status & RX_DMA_STATUS_DU)
359 		return false;
360 
361 	return true;
362 }
363 
364 static int wil_rx_get_cid_by_skb(struct wil6210_priv *wil, struct sk_buff *skb)
365 {
366 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
367 	int mid = wil_rxdesc_mid(d);
368 	struct wil6210_vif *vif = wil->vifs[mid];
369 	/* cid from DMA descriptor is limited to 3 bits.
370 	 * In case of cid>=8, the value would be cid modulo 8 and we need to
371 	 * find real cid by locating the transmitter (ta) inside sta array
372 	 */
373 	int cid = wil_rxdesc_cid(d);
374 	unsigned int snaplen = wil_rx_snaplen();
375 	struct ieee80211_hdr_3addr *hdr;
376 	int i;
377 	unsigned char *ta;
378 	u8 ftype;
379 
380 	/* in monitor mode there are no connections */
381 	if (vif->wdev.iftype == NL80211_IFTYPE_MONITOR)
382 		return cid;
383 
384 	ftype = wil_rxdesc_ftype(d) << 2;
385 	if (likely(ftype == IEEE80211_FTYPE_DATA)) {
386 		if (unlikely(skb->len < ETH_HLEN + snaplen)) {
387 			wil_err_ratelimited(wil,
388 					    "Short data frame, len = %d\n",
389 					    skb->len);
390 			return -ENOENT;
391 		}
392 		ta = wil_skb_get_sa(skb);
393 	} else {
394 		if (unlikely(skb->len < sizeof(struct ieee80211_hdr_3addr))) {
395 			wil_err_ratelimited(wil, "Short frame, len = %d\n",
396 					    skb->len);
397 			return -ENOENT;
398 		}
399 		hdr = (void *)skb->data;
400 		ta = hdr->addr2;
401 	}
402 
403 	if (wil->max_assoc_sta <= WIL6210_RX_DESC_MAX_CID)
404 		return cid;
405 
406 	/* assuming no concurrency between AP interfaces and STA interfaces.
407 	 * multista is used only in P2P_GO or AP mode. In other modes return
408 	 * cid from the rx descriptor
409 	 */
410 	if (vif->wdev.iftype != NL80211_IFTYPE_P2P_GO &&
411 	    vif->wdev.iftype != NL80211_IFTYPE_AP)
412 		return cid;
413 
414 	/* For Rx packets cid from rx descriptor is limited to 3 bits (0..7),
415 	 * to find the real cid, compare transmitter address with the stored
416 	 * stations mac address in the driver sta array
417 	 */
418 	for (i = cid; i < wil->max_assoc_sta; i += WIL6210_RX_DESC_MAX_CID) {
419 		if (wil->sta[i].status != wil_sta_unused &&
420 		    ether_addr_equal(wil->sta[i].addr, ta)) {
421 			cid = i;
422 			break;
423 		}
424 	}
425 	if (i >= wil->max_assoc_sta) {
426 		wil_err_ratelimited(wil, "Could not find cid for frame with transmit addr = %pM, iftype = %d, frametype = %d, len = %d\n",
427 				    ta, vif->wdev.iftype, ftype, skb->len);
428 		cid = -ENOENT;
429 	}
430 
431 	return cid;
432 }
433 
434 /**
435  * reap 1 frame from @swhead
436  *
437  * Rx descriptor copied to skb->cb
438  *
439  * Safe to call from IRQ
440  */
441 static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
442 					 struct wil_ring *vring)
443 {
444 	struct device *dev = wil_to_dev(wil);
445 	struct wil6210_vif *vif;
446 	struct net_device *ndev;
447 	volatile struct vring_rx_desc *_d;
448 	struct vring_rx_desc *d;
449 	struct sk_buff *skb;
450 	dma_addr_t pa;
451 	unsigned int snaplen = wil_rx_snaplen();
452 	unsigned int sz = wil->rx_buf_len + ETH_HLEN + snaplen;
453 	u16 dmalen;
454 	u8 ftype;
455 	int cid, mid;
456 	int i;
457 	struct wil_net_stats *stats;
458 
459 	BUILD_BUG_ON(sizeof(struct skb_rx_info) > sizeof(skb->cb));
460 
461 again:
462 	if (unlikely(wil_ring_is_empty(vring)))
463 		return NULL;
464 
465 	i = (int)vring->swhead;
466 	_d = &vring->va[i].rx.legacy;
467 	if (unlikely(!(_d->dma.status & RX_DMA_STATUS_DU))) {
468 		/* it is not error, we just reached end of Rx done area */
469 		return NULL;
470 	}
471 
472 	skb = vring->ctx[i].skb;
473 	vring->ctx[i].skb = NULL;
474 	wil_ring_advance_head(vring, 1);
475 	if (!skb) {
476 		wil_err(wil, "No Rx skb at [%d]\n", i);
477 		goto again;
478 	}
479 	d = wil_skb_rxdesc(skb);
480 	*d = *_d;
481 	pa = wil_desc_addr(&d->dma.addr);
482 
483 	dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
484 	dmalen = le16_to_cpu(d->dma.length);
485 
486 	trace_wil6210_rx(i, d);
487 	wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", i, dmalen);
488 	wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
489 			  (const void *)d, sizeof(*d), false);
490 
491 	mid = wil_rxdesc_mid(d);
492 	vif = wil->vifs[mid];
493 
494 	if (unlikely(!vif)) {
495 		wil_dbg_txrx(wil, "skipped RX descriptor with invalid mid %d",
496 			     mid);
497 		kfree_skb(skb);
498 		goto again;
499 	}
500 	ndev = vif_to_ndev(vif);
501 	if (unlikely(dmalen > sz)) {
502 		wil_err_ratelimited(wil, "Rx size too large: %d bytes!\n",
503 				    dmalen);
504 		kfree_skb(skb);
505 		goto again;
506 	}
507 	skb_trim(skb, dmalen);
508 
509 	prefetch(skb->data);
510 
511 	wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
512 			  skb->data, skb_headlen(skb), false);
513 
514 	cid = wil_rx_get_cid_by_skb(wil, skb);
515 	if (cid == -ENOENT) {
516 		kfree_skb(skb);
517 		goto again;
518 	}
519 	wil_skb_set_cid(skb, (u8)cid);
520 	stats = &wil->sta[cid].stats;
521 
522 	stats->last_mcs_rx = wil_rxdesc_mcs(d);
523 	if (stats->last_mcs_rx < ARRAY_SIZE(stats->rx_per_mcs))
524 		stats->rx_per_mcs[stats->last_mcs_rx]++;
525 
526 	/* use radiotap header only if required */
527 	if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
528 		wil_rx_add_radiotap_header(wil, skb);
529 
530 	/* no extra checks if in sniffer mode */
531 	if (ndev->type != ARPHRD_ETHER)
532 		return skb;
533 	/* Non-data frames may be delivered through Rx DMA channel (ex: BAR)
534 	 * Driver should recognize it by frame type, that is found
535 	 * in Rx descriptor. If type is not data, it is 802.11 frame as is
536 	 */
537 	ftype = wil_rxdesc_ftype(d) << 2;
538 	if (unlikely(ftype != IEEE80211_FTYPE_DATA)) {
539 		u8 fc1 = wil_rxdesc_fc1(d);
540 		int tid = wil_rxdesc_tid(d);
541 		u16 seq = wil_rxdesc_seq(d);
542 
543 		wil_dbg_txrx(wil,
544 			     "Non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
545 			     fc1, mid, cid, tid, seq);
546 		stats->rx_non_data_frame++;
547 		if (wil_is_back_req(fc1)) {
548 			wil_dbg_txrx(wil,
549 				     "BAR: MID %d CID %d TID %d Seq 0x%03x\n",
550 				     mid, cid, tid, seq);
551 			wil_rx_bar(wil, vif, cid, tid, seq);
552 		} else {
553 			/* print again all info. One can enable only this
554 			 * without overhead for printing every Rx frame
555 			 */
556 			wil_dbg_txrx(wil,
557 				     "Unhandled non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
558 				     fc1, mid, cid, tid, seq);
559 			wil_hex_dump_txrx("RxD ", DUMP_PREFIX_NONE, 32, 4,
560 					  (const void *)d, sizeof(*d), false);
561 			wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
562 					  skb->data, skb_headlen(skb), false);
563 		}
564 		kfree_skb(skb);
565 		goto again;
566 	}
567 
568 	/* L4 IDENT is on when HW calculated checksum, check status
569 	 * and in case of error drop the packet
570 	 * higher stack layers will handle retransmission (if required)
571 	 */
572 	if (likely(d->dma.status & RX_DMA_STATUS_L4I)) {
573 		/* L4 protocol identified, csum calculated */
574 		if (likely((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0))
575 			skb->ip_summed = CHECKSUM_UNNECESSARY;
576 		/* If HW reports bad checksum, let IP stack re-check it
577 		 * For example, HW don't understand Microsoft IP stack that
578 		 * mis-calculates TCP checksum - if it should be 0x0,
579 		 * it writes 0xffff in violation of RFC 1624
580 		 */
581 		else
582 			stats->rx_csum_err++;
583 	}
584 
585 	if (snaplen) {
586 		/* Packet layout
587 		 * +-------+-------+---------+------------+------+
588 		 * | SA(6) | DA(6) | SNAP(6) | ETHTYPE(2) | DATA |
589 		 * +-------+-------+---------+------------+------+
590 		 * Need to remove SNAP, shifting SA and DA forward
591 		 */
592 		memmove(skb->data + snaplen, skb->data, 2 * ETH_ALEN);
593 		skb_pull(skb, snaplen);
594 	}
595 
596 	return skb;
597 }
598 
599 /**
600  * allocate and fill up to @count buffers in rx ring
601  * buffers posted at @swtail
602  * Note: we have a single RX queue for servicing all VIFs, but we
603  * allocate skbs with headroom according to main interface only. This
604  * means it will not work with monitor interface together with other VIFs.
605  * Currently we only support monitor interface on its own without other VIFs,
606  * and we will need to fix this code once we add support.
607  */
608 static int wil_rx_refill(struct wil6210_priv *wil, int count)
609 {
610 	struct net_device *ndev = wil->main_ndev;
611 	struct wil_ring *v = &wil->ring_rx;
612 	u32 next_tail;
613 	int rc = 0;
614 	int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
615 			WIL6210_RTAP_SIZE : 0;
616 
617 	for (; next_tail = wil_ring_next_tail(v),
618 	     (next_tail != v->swhead) && (count-- > 0);
619 	     v->swtail = next_tail) {
620 		rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
621 		if (unlikely(rc)) {
622 			wil_err_ratelimited(wil, "Error %d in rx refill[%d]\n",
623 					    rc, v->swtail);
624 			break;
625 		}
626 	}
627 
628 	/* make sure all writes to descriptors (shared memory) are done before
629 	 * committing them to HW
630 	 */
631 	wmb();
632 
633 	wil_w(wil, v->hwtail, v->swtail);
634 
635 	return rc;
636 }
637 
638 /**
639  * reverse_memcmp - Compare two areas of memory, in reverse order
640  * @cs: One area of memory
641  * @ct: Another area of memory
642  * @count: The size of the area.
643  *
644  * Cut'n'paste from original memcmp (see lib/string.c)
645  * with minimal modifications
646  */
647 int reverse_memcmp(const void *cs, const void *ct, size_t count)
648 {
649 	const unsigned char *su1, *su2;
650 	int res = 0;
651 
652 	for (su1 = cs + count - 1, su2 = ct + count - 1; count > 0;
653 	     --su1, --su2, count--) {
654 		res = *su1 - *su2;
655 		if (res)
656 			break;
657 	}
658 	return res;
659 }
660 
661 static int wil_rx_crypto_check(struct wil6210_priv *wil, struct sk_buff *skb)
662 {
663 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
664 	int cid = wil_skb_get_cid(skb);
665 	int tid = wil_rxdesc_tid(d);
666 	int key_id = wil_rxdesc_key_id(d);
667 	int mc = wil_rxdesc_mcast(d);
668 	struct wil_sta_info *s = &wil->sta[cid];
669 	struct wil_tid_crypto_rx *c = mc ? &s->group_crypto_rx :
670 				      &s->tid_crypto_rx[tid];
671 	struct wil_tid_crypto_rx_single *cc = &c->key_id[key_id];
672 	const u8 *pn = (u8 *)&d->mac.pn_15_0;
673 
674 	if (!cc->key_set) {
675 		wil_err_ratelimited(wil,
676 				    "Key missing. CID %d TID %d MCast %d KEY_ID %d\n",
677 				    cid, tid, mc, key_id);
678 		return -EINVAL;
679 	}
680 
681 	if (reverse_memcmp(pn, cc->pn, IEEE80211_GCMP_PN_LEN) <= 0) {
682 		wil_err_ratelimited(wil,
683 				    "Replay attack. CID %d TID %d MCast %d KEY_ID %d PN %6phN last %6phN\n",
684 				    cid, tid, mc, key_id, pn, cc->pn);
685 		return -EINVAL;
686 	}
687 	memcpy(cc->pn, pn, IEEE80211_GCMP_PN_LEN);
688 
689 	return 0;
690 }
691 
692 static int wil_rx_error_check(struct wil6210_priv *wil, struct sk_buff *skb,
693 			      struct wil_net_stats *stats)
694 {
695 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
696 
697 	if ((d->dma.status & RX_DMA_STATUS_ERROR) &&
698 	    (d->dma.error & RX_DMA_ERROR_MIC)) {
699 		stats->rx_mic_error++;
700 		wil_dbg_txrx(wil, "MIC error, dropping packet\n");
701 		return -EFAULT;
702 	}
703 
704 	return 0;
705 }
706 
707 static void wil_get_netif_rx_params(struct sk_buff *skb, int *cid,
708 				    int *security)
709 {
710 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
711 
712 	*cid = wil_skb_get_cid(skb);
713 	*security = wil_rxdesc_security(d);
714 }
715 
716 /*
717  * Check if skb is ptk eapol key message
718  *
719  * returns a pointer to the start of the eapol key structure, NULL
720  * if frame is not PTK eapol key
721  */
722 static struct wil_eapol_key *wil_is_ptk_eapol_key(struct wil6210_priv *wil,
723 						  struct sk_buff *skb)
724 {
725 	u8 *buf;
726 	const struct wil_1x_hdr *hdr;
727 	struct wil_eapol_key *key;
728 	u16 key_info;
729 	int len = skb->len;
730 
731 	if (!skb_mac_header_was_set(skb)) {
732 		wil_err(wil, "mac header was not set\n");
733 		return NULL;
734 	}
735 
736 	len -= skb_mac_offset(skb);
737 
738 	if (len < sizeof(struct ethhdr) + sizeof(struct wil_1x_hdr) +
739 	    sizeof(struct wil_eapol_key))
740 		return NULL;
741 
742 	buf = skb_mac_header(skb) + sizeof(struct ethhdr);
743 
744 	hdr = (const struct wil_1x_hdr *)buf;
745 	if (hdr->type != WIL_1X_TYPE_EAPOL_KEY)
746 		return NULL;
747 
748 	key = (struct wil_eapol_key *)(buf + sizeof(struct wil_1x_hdr));
749 	if (key->type != WIL_EAPOL_KEY_TYPE_WPA &&
750 	    key->type != WIL_EAPOL_KEY_TYPE_RSN)
751 		return NULL;
752 
753 	key_info = be16_to_cpu(key->key_info);
754 	if (!(key_info & WIL_KEY_INFO_KEY_TYPE)) /* check if pairwise */
755 		return NULL;
756 
757 	return key;
758 }
759 
760 static bool wil_skb_is_eap_3(struct wil6210_priv *wil, struct sk_buff *skb)
761 {
762 	struct wil_eapol_key *key;
763 	u16 key_info;
764 
765 	key = wil_is_ptk_eapol_key(wil, skb);
766 	if (!key)
767 		return false;
768 
769 	key_info = be16_to_cpu(key->key_info);
770 	if (key_info & (WIL_KEY_INFO_MIC |
771 			WIL_KEY_INFO_ENCR_KEY_DATA)) {
772 		/* 3/4 of 4-Way Handshake */
773 		wil_dbg_misc(wil, "EAPOL key message 3\n");
774 		return true;
775 	}
776 	/* 1/4 of 4-Way Handshake */
777 	wil_dbg_misc(wil, "EAPOL key message 1\n");
778 
779 	return false;
780 }
781 
782 static bool wil_skb_is_eap_4(struct wil6210_priv *wil, struct sk_buff *skb)
783 {
784 	struct wil_eapol_key *key;
785 	u32 *nonce, i;
786 
787 	key = wil_is_ptk_eapol_key(wil, skb);
788 	if (!key)
789 		return false;
790 
791 	nonce = (u32 *)key->key_nonce;
792 	for (i = 0; i < WIL_EAP_NONCE_LEN / sizeof(u32); i++, nonce++) {
793 		if (*nonce != 0) {
794 			/* message 2/4 */
795 			wil_dbg_misc(wil, "EAPOL key message 2\n");
796 			return false;
797 		}
798 	}
799 	wil_dbg_misc(wil, "EAPOL key message 4\n");
800 
801 	return true;
802 }
803 
804 void wil_enable_tx_key_worker(struct work_struct *work)
805 {
806 	struct wil6210_vif *vif = container_of(work,
807 			struct wil6210_vif, enable_tx_key_worker);
808 	struct wil6210_priv *wil = vif_to_wil(vif);
809 	int rc, cid;
810 
811 	rtnl_lock();
812 	if (vif->ptk_rekey_state != WIL_REKEY_WAIT_M4_SENT) {
813 		wil_dbg_misc(wil, "Invalid rekey state = %d\n",
814 			     vif->ptk_rekey_state);
815 		rtnl_unlock();
816 		return;
817 	}
818 
819 	cid =  wil_find_cid_by_idx(wil, vif->mid, 0);
820 	if (!wil_cid_valid(wil, cid)) {
821 		wil_err(wil, "Invalid cid = %d\n", cid);
822 		rtnl_unlock();
823 		return;
824 	}
825 
826 	wil_dbg_misc(wil, "Apply PTK key after eapol was sent out\n");
827 	rc = wmi_add_cipher_key(vif, 0, wil->sta[cid].addr, 0, NULL,
828 				WMI_KEY_USE_APPLY_PTK);
829 
830 	vif->ptk_rekey_state = WIL_REKEY_IDLE;
831 	rtnl_unlock();
832 
833 	if (rc)
834 		wil_err(wil, "Apply PTK key failed %d\n", rc);
835 }
836 
837 void wil_tx_complete_handle_eapol(struct wil6210_vif *vif, struct sk_buff *skb)
838 {
839 	struct wil6210_priv *wil = vif_to_wil(vif);
840 	struct wireless_dev *wdev = vif_to_wdev(vif);
841 	bool q = false;
842 
843 	if (wdev->iftype != NL80211_IFTYPE_STATION ||
844 	    !test_bit(WMI_FW_CAPABILITY_SPLIT_REKEY, wil->fw_capabilities))
845 		return;
846 
847 	/* check if skb is an EAP message 4/4 */
848 	if (!wil_skb_is_eap_4(wil, skb))
849 		return;
850 
851 	spin_lock_bh(&wil->eap_lock);
852 	switch (vif->ptk_rekey_state) {
853 	case WIL_REKEY_IDLE:
854 		/* ignore idle state, can happen due to M4 retransmission */
855 		break;
856 	case WIL_REKEY_M3_RECEIVED:
857 		vif->ptk_rekey_state = WIL_REKEY_IDLE;
858 		break;
859 	case WIL_REKEY_WAIT_M4_SENT:
860 		q = true;
861 		break;
862 	default:
863 		wil_err(wil, "Unknown rekey state = %d",
864 			vif->ptk_rekey_state);
865 	}
866 	spin_unlock_bh(&wil->eap_lock);
867 
868 	if (q) {
869 		q = queue_work(wil->wmi_wq, &vif->enable_tx_key_worker);
870 		wil_dbg_misc(wil, "queue_work of enable_tx_key_worker -> %d\n",
871 			     q);
872 	}
873 }
874 
875 static void wil_rx_handle_eapol(struct wil6210_vif *vif, struct sk_buff *skb)
876 {
877 	struct wil6210_priv *wil = vif_to_wil(vif);
878 	struct wireless_dev *wdev = vif_to_wdev(vif);
879 
880 	if (wdev->iftype != NL80211_IFTYPE_STATION ||
881 	    !test_bit(WMI_FW_CAPABILITY_SPLIT_REKEY, wil->fw_capabilities))
882 		return;
883 
884 	/* check if skb is a EAP message 3/4 */
885 	if (!wil_skb_is_eap_3(wil, skb))
886 		return;
887 
888 	if (vif->ptk_rekey_state == WIL_REKEY_IDLE)
889 		vif->ptk_rekey_state = WIL_REKEY_M3_RECEIVED;
890 }
891 
892 /*
893  * Pass Rx packet to the netif. Update statistics.
894  * Called in softirq context (NAPI poll).
895  */
896 void wil_netif_rx(struct sk_buff *skb, struct net_device *ndev, int cid,
897 		  struct wil_net_stats *stats, bool gro)
898 {
899 	gro_result_t rc = GRO_NORMAL;
900 	struct wil6210_vif *vif = ndev_to_vif(ndev);
901 	struct wil6210_priv *wil = ndev_to_wil(ndev);
902 	struct wireless_dev *wdev = vif_to_wdev(vif);
903 	unsigned int len = skb->len;
904 	u8 *sa, *da = wil_skb_get_da(skb);
905 	/* here looking for DA, not A1, thus Rxdesc's 'mcast' indication
906 	 * is not suitable, need to look at data
907 	 */
908 	int mcast = is_multicast_ether_addr(da);
909 	struct sk_buff *xmit_skb = NULL;
910 	static const char * const gro_res_str[] = {
911 		[GRO_MERGED]		= "GRO_MERGED",
912 		[GRO_MERGED_FREE]	= "GRO_MERGED_FREE",
913 		[GRO_HELD]		= "GRO_HELD",
914 		[GRO_NORMAL]		= "GRO_NORMAL",
915 		[GRO_DROP]		= "GRO_DROP",
916 		[GRO_CONSUMED]		= "GRO_CONSUMED",
917 	};
918 
919 	if (wdev->iftype == NL80211_IFTYPE_STATION) {
920 		sa = wil_skb_get_sa(skb);
921 		if (mcast && ether_addr_equal(sa, ndev->dev_addr)) {
922 			/* mcast packet looped back to us */
923 			rc = GRO_DROP;
924 			dev_kfree_skb(skb);
925 			goto stats;
926 		}
927 	} else if (wdev->iftype == NL80211_IFTYPE_AP && !vif->ap_isolate) {
928 		if (mcast) {
929 			/* send multicast frames both to higher layers in
930 			 * local net stack and back to the wireless medium
931 			 */
932 			xmit_skb = skb_copy(skb, GFP_ATOMIC);
933 		} else {
934 			int xmit_cid = wil_find_cid(wil, vif->mid, da);
935 
936 			if (xmit_cid >= 0) {
937 				/* The destination station is associated to
938 				 * this AP (in this VLAN), so send the frame
939 				 * directly to it and do not pass it to local
940 				 * net stack.
941 				 */
942 				xmit_skb = skb;
943 				skb = NULL;
944 			}
945 		}
946 	}
947 	if (xmit_skb) {
948 		/* Send to wireless media and increase priority by 256 to
949 		 * keep the received priority instead of reclassifying
950 		 * the frame (see cfg80211_classify8021d).
951 		 */
952 		xmit_skb->dev = ndev;
953 		xmit_skb->priority += 256;
954 		xmit_skb->protocol = htons(ETH_P_802_3);
955 		skb_reset_network_header(xmit_skb);
956 		skb_reset_mac_header(xmit_skb);
957 		wil_dbg_txrx(wil, "Rx -> Tx %d bytes\n", len);
958 		dev_queue_xmit(xmit_skb);
959 	}
960 
961 	if (skb) { /* deliver to local stack */
962 		skb->protocol = eth_type_trans(skb, ndev);
963 		skb->dev = ndev;
964 
965 		if (skb->protocol == cpu_to_be16(ETH_P_PAE))
966 			wil_rx_handle_eapol(vif, skb);
967 
968 		if (gro)
969 			rc = napi_gro_receive(&wil->napi_rx, skb);
970 		else
971 			netif_rx_ni(skb);
972 		wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
973 			     len, gro_res_str[rc]);
974 	}
975 stats:
976 	/* statistics. rc set to GRO_NORMAL for AP bridging */
977 	if (unlikely(rc == GRO_DROP)) {
978 		ndev->stats.rx_dropped++;
979 		stats->rx_dropped++;
980 		wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
981 	} else {
982 		ndev->stats.rx_packets++;
983 		stats->rx_packets++;
984 		ndev->stats.rx_bytes += len;
985 		stats->rx_bytes += len;
986 		if (mcast)
987 			ndev->stats.multicast++;
988 	}
989 }
990 
991 void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
992 {
993 	int cid, security;
994 	struct wil6210_priv *wil = ndev_to_wil(ndev);
995 	struct wil_net_stats *stats;
996 
997 	wil->txrx_ops.get_netif_rx_params(skb, &cid, &security);
998 
999 	stats = &wil->sta[cid].stats;
1000 
1001 	skb_orphan(skb);
1002 
1003 	if (security && (wil->txrx_ops.rx_crypto_check(wil, skb) != 0)) {
1004 		wil_dbg_txrx(wil, "Rx drop %d bytes\n", skb->len);
1005 		dev_kfree_skb(skb);
1006 		ndev->stats.rx_dropped++;
1007 		stats->rx_replay++;
1008 		stats->rx_dropped++;
1009 		return;
1010 	}
1011 
1012 	/* check errors reported by HW and update statistics */
1013 	if (unlikely(wil->txrx_ops.rx_error_check(wil, skb, stats))) {
1014 		dev_kfree_skb(skb);
1015 		return;
1016 	}
1017 
1018 	wil_netif_rx(skb, ndev, cid, stats, true);
1019 }
1020 
1021 /**
1022  * Proceed all completed skb's from Rx VRING
1023  *
1024  * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
1025  */
1026 void wil_rx_handle(struct wil6210_priv *wil, int *quota)
1027 {
1028 	struct net_device *ndev = wil->main_ndev;
1029 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
1030 	struct wil_ring *v = &wil->ring_rx;
1031 	struct sk_buff *skb;
1032 
1033 	if (unlikely(!v->va)) {
1034 		wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
1035 		return;
1036 	}
1037 	wil_dbg_txrx(wil, "rx_handle\n");
1038 	while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
1039 		(*quota)--;
1040 
1041 		/* monitor is currently supported on main interface only */
1042 		if (wdev->iftype == NL80211_IFTYPE_MONITOR) {
1043 			skb->dev = ndev;
1044 			skb_reset_mac_header(skb);
1045 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1046 			skb->pkt_type = PACKET_OTHERHOST;
1047 			skb->protocol = htons(ETH_P_802_2);
1048 			wil_netif_rx_any(skb, ndev);
1049 		} else {
1050 			wil_rx_reorder(wil, skb);
1051 		}
1052 	}
1053 	wil_rx_refill(wil, v->size);
1054 }
1055 
1056 static void wil_rx_buf_len_init(struct wil6210_priv *wil)
1057 {
1058 	wil->rx_buf_len = rx_large_buf ?
1059 		WIL_MAX_ETH_MTU : TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
1060 	if (mtu_max > wil->rx_buf_len) {
1061 		/* do not allow RX buffers to be smaller than mtu_max, for
1062 		 * backward compatibility (mtu_max parameter was also used
1063 		 * to support receiving large packets)
1064 		 */
1065 		wil_info(wil, "Override RX buffer to mtu_max(%d)\n", mtu_max);
1066 		wil->rx_buf_len = mtu_max;
1067 	}
1068 }
1069 
1070 static int wil_rx_init(struct wil6210_priv *wil, uint order)
1071 {
1072 	struct wil_ring *vring = &wil->ring_rx;
1073 	int rc;
1074 
1075 	wil_dbg_misc(wil, "rx_init\n");
1076 
1077 	if (vring->va) {
1078 		wil_err(wil, "Rx ring already allocated\n");
1079 		return -EINVAL;
1080 	}
1081 
1082 	wil_rx_buf_len_init(wil);
1083 
1084 	vring->size = 1 << order;
1085 	vring->is_rx = true;
1086 	rc = wil_vring_alloc(wil, vring);
1087 	if (rc)
1088 		return rc;
1089 
1090 	rc = wmi_rx_chain_add(wil, vring);
1091 	if (rc)
1092 		goto err_free;
1093 
1094 	rc = wil_rx_refill(wil, vring->size);
1095 	if (rc)
1096 		goto err_free;
1097 
1098 	return 0;
1099  err_free:
1100 	wil_vring_free(wil, vring);
1101 
1102 	return rc;
1103 }
1104 
1105 static void wil_rx_fini(struct wil6210_priv *wil)
1106 {
1107 	struct wil_ring *vring = &wil->ring_rx;
1108 
1109 	wil_dbg_misc(wil, "rx_fini\n");
1110 
1111 	if (vring->va)
1112 		wil_vring_free(wil, vring);
1113 }
1114 
1115 static int wil_tx_desc_map(union wil_tx_desc *desc, dma_addr_t pa,
1116 			   u32 len, int vring_index)
1117 {
1118 	struct vring_tx_desc *d = &desc->legacy;
1119 
1120 	wil_desc_addr_set(&d->dma.addr, pa);
1121 	d->dma.ip_length = 0;
1122 	/* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
1123 	d->dma.b11 = 0/*14 | BIT(7)*/;
1124 	d->dma.error = 0;
1125 	d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
1126 	d->dma.length = cpu_to_le16((u16)len);
1127 	d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
1128 	d->mac.d[0] = 0;
1129 	d->mac.d[1] = 0;
1130 	d->mac.d[2] = 0;
1131 	d->mac.ucode_cmd = 0;
1132 	/* translation type:  0 - bypass; 1 - 802.3; 2 - native wifi */
1133 	d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
1134 		      (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
1135 
1136 	return 0;
1137 }
1138 
1139 void wil_tx_data_init(struct wil_ring_tx_data *txdata)
1140 {
1141 	spin_lock_bh(&txdata->lock);
1142 	txdata->dot1x_open = 0;
1143 	txdata->enabled = 0;
1144 	txdata->idle = 0;
1145 	txdata->last_idle = 0;
1146 	txdata->begin = 0;
1147 	txdata->agg_wsize = 0;
1148 	txdata->agg_timeout = 0;
1149 	txdata->agg_amsdu = 0;
1150 	txdata->addba_in_progress = false;
1151 	txdata->mid = U8_MAX;
1152 	spin_unlock_bh(&txdata->lock);
1153 }
1154 
1155 static int wil_vring_init_tx(struct wil6210_vif *vif, int id, int size,
1156 			     int cid, int tid)
1157 {
1158 	struct wil6210_priv *wil = vif_to_wil(vif);
1159 	int rc;
1160 	struct wmi_vring_cfg_cmd cmd = {
1161 		.action = cpu_to_le32(WMI_VRING_CMD_ADD),
1162 		.vring_cfg = {
1163 			.tx_sw_ring = {
1164 				.max_mpdu_size =
1165 					cpu_to_le16(wil_mtu2macbuf(mtu_max)),
1166 				.ring_size = cpu_to_le16(size),
1167 			},
1168 			.ringid = id,
1169 			.encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
1170 			.mac_ctrl = 0,
1171 			.to_resolution = 0,
1172 			.agg_max_wsize = 0,
1173 			.schd_params = {
1174 				.priority = cpu_to_le16(0),
1175 				.timeslot_us = cpu_to_le16(0xfff),
1176 			},
1177 		},
1178 	};
1179 	struct {
1180 		struct wmi_cmd_hdr wmi;
1181 		struct wmi_vring_cfg_done_event cmd;
1182 	} __packed reply = {
1183 		.cmd = {.status = WMI_FW_STATUS_FAILURE},
1184 	};
1185 	struct wil_ring *vring = &wil->ring_tx[id];
1186 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[id];
1187 
1188 	if (cid >= WIL6210_RX_DESC_MAX_CID) {
1189 		cmd.vring_cfg.cidxtid = CIDXTID_EXTENDED_CID_TID;
1190 		cmd.vring_cfg.cid = cid;
1191 		cmd.vring_cfg.tid = tid;
1192 	} else {
1193 		cmd.vring_cfg.cidxtid = mk_cidxtid(cid, tid);
1194 	}
1195 
1196 	wil_dbg_misc(wil, "vring_init_tx: max_mpdu_size %d\n",
1197 		     cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
1198 	lockdep_assert_held(&wil->mutex);
1199 
1200 	if (vring->va) {
1201 		wil_err(wil, "Tx ring [%d] already allocated\n", id);
1202 		rc = -EINVAL;
1203 		goto out;
1204 	}
1205 
1206 	wil_tx_data_init(txdata);
1207 	vring->is_rx = false;
1208 	vring->size = size;
1209 	rc = wil_vring_alloc(wil, vring);
1210 	if (rc)
1211 		goto out;
1212 
1213 	wil->ring2cid_tid[id][0] = cid;
1214 	wil->ring2cid_tid[id][1] = tid;
1215 
1216 	cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
1217 
1218 	if (!vif->privacy)
1219 		txdata->dot1x_open = true;
1220 	rc = wmi_call(wil, WMI_VRING_CFG_CMDID, vif->mid, &cmd, sizeof(cmd),
1221 		      WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply),
1222 		      WIL_WMI_CALL_GENERAL_TO_MS);
1223 	if (rc)
1224 		goto out_free;
1225 
1226 	if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
1227 		wil_err(wil, "Tx config failed, status 0x%02x\n",
1228 			reply.cmd.status);
1229 		rc = -EINVAL;
1230 		goto out_free;
1231 	}
1232 
1233 	spin_lock_bh(&txdata->lock);
1234 	vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
1235 	txdata->mid = vif->mid;
1236 	txdata->enabled = 1;
1237 	spin_unlock_bh(&txdata->lock);
1238 
1239 	if (txdata->dot1x_open && (agg_wsize >= 0))
1240 		wil_addba_tx_request(wil, id, agg_wsize);
1241 
1242 	return 0;
1243  out_free:
1244 	spin_lock_bh(&txdata->lock);
1245 	txdata->dot1x_open = false;
1246 	txdata->enabled = 0;
1247 	spin_unlock_bh(&txdata->lock);
1248 	wil_vring_free(wil, vring);
1249 	wil->ring2cid_tid[id][0] = wil->max_assoc_sta;
1250 	wil->ring2cid_tid[id][1] = 0;
1251 
1252  out:
1253 
1254 	return rc;
1255 }
1256 
1257 static int wil_tx_vring_modify(struct wil6210_vif *vif, int ring_id, int cid,
1258 			       int tid)
1259 {
1260 	struct wil6210_priv *wil = vif_to_wil(vif);
1261 	int rc;
1262 	struct wmi_vring_cfg_cmd cmd = {
1263 		.action = cpu_to_le32(WMI_VRING_CMD_MODIFY),
1264 		.vring_cfg = {
1265 			.tx_sw_ring = {
1266 				.max_mpdu_size =
1267 					cpu_to_le16(wil_mtu2macbuf(mtu_max)),
1268 				.ring_size = 0,
1269 			},
1270 			.ringid = ring_id,
1271 			.cidxtid = mk_cidxtid(cid, tid),
1272 			.encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
1273 			.mac_ctrl = 0,
1274 			.to_resolution = 0,
1275 			.agg_max_wsize = 0,
1276 			.schd_params = {
1277 				.priority = cpu_to_le16(0),
1278 				.timeslot_us = cpu_to_le16(0xfff),
1279 			},
1280 		},
1281 	};
1282 	struct {
1283 		struct wmi_cmd_hdr wmi;
1284 		struct wmi_vring_cfg_done_event cmd;
1285 	} __packed reply = {
1286 		.cmd = {.status = WMI_FW_STATUS_FAILURE},
1287 	};
1288 	struct wil_ring *vring = &wil->ring_tx[ring_id];
1289 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ring_id];
1290 
1291 	wil_dbg_misc(wil, "vring_modify: ring %d cid %d tid %d\n", ring_id,
1292 		     cid, tid);
1293 	lockdep_assert_held(&wil->mutex);
1294 
1295 	if (!vring->va) {
1296 		wil_err(wil, "Tx ring [%d] not allocated\n", ring_id);
1297 		return -EINVAL;
1298 	}
1299 
1300 	if (wil->ring2cid_tid[ring_id][0] != cid ||
1301 	    wil->ring2cid_tid[ring_id][1] != tid) {
1302 		wil_err(wil, "ring info does not match cid=%u tid=%u\n",
1303 			wil->ring2cid_tid[ring_id][0],
1304 			wil->ring2cid_tid[ring_id][1]);
1305 	}
1306 
1307 	cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
1308 
1309 	rc = wmi_call(wil, WMI_VRING_CFG_CMDID, vif->mid, &cmd, sizeof(cmd),
1310 		      WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply),
1311 		      WIL_WMI_CALL_GENERAL_TO_MS);
1312 	if (rc)
1313 		goto fail;
1314 
1315 	if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
1316 		wil_err(wil, "Tx modify failed, status 0x%02x\n",
1317 			reply.cmd.status);
1318 		rc = -EINVAL;
1319 		goto fail;
1320 	}
1321 
1322 	/* set BA aggregation window size to 0 to force a new BA with the
1323 	 * new AP
1324 	 */
1325 	txdata->agg_wsize = 0;
1326 	if (txdata->dot1x_open && agg_wsize >= 0)
1327 		wil_addba_tx_request(wil, ring_id, agg_wsize);
1328 
1329 	return 0;
1330 fail:
1331 	spin_lock_bh(&txdata->lock);
1332 	txdata->dot1x_open = false;
1333 	txdata->enabled = 0;
1334 	spin_unlock_bh(&txdata->lock);
1335 	wil->ring2cid_tid[ring_id][0] = wil->max_assoc_sta;
1336 	wil->ring2cid_tid[ring_id][1] = 0;
1337 	return rc;
1338 }
1339 
1340 int wil_vring_init_bcast(struct wil6210_vif *vif, int id, int size)
1341 {
1342 	struct wil6210_priv *wil = vif_to_wil(vif);
1343 	int rc;
1344 	struct wmi_bcast_vring_cfg_cmd cmd = {
1345 		.action = cpu_to_le32(WMI_VRING_CMD_ADD),
1346 		.vring_cfg = {
1347 			.tx_sw_ring = {
1348 				.max_mpdu_size =
1349 					cpu_to_le16(wil_mtu2macbuf(mtu_max)),
1350 				.ring_size = cpu_to_le16(size),
1351 			},
1352 			.ringid = id,
1353 			.encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
1354 		},
1355 	};
1356 	struct {
1357 		struct wmi_cmd_hdr wmi;
1358 		struct wmi_vring_cfg_done_event cmd;
1359 	} __packed reply = {
1360 		.cmd = {.status = WMI_FW_STATUS_FAILURE},
1361 	};
1362 	struct wil_ring *vring = &wil->ring_tx[id];
1363 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[id];
1364 
1365 	wil_dbg_misc(wil, "vring_init_bcast: max_mpdu_size %d\n",
1366 		     cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
1367 	lockdep_assert_held(&wil->mutex);
1368 
1369 	if (vring->va) {
1370 		wil_err(wil, "Tx ring [%d] already allocated\n", id);
1371 		rc = -EINVAL;
1372 		goto out;
1373 	}
1374 
1375 	wil_tx_data_init(txdata);
1376 	vring->is_rx = false;
1377 	vring->size = size;
1378 	rc = wil_vring_alloc(wil, vring);
1379 	if (rc)
1380 		goto out;
1381 
1382 	wil->ring2cid_tid[id][0] = wil->max_assoc_sta; /* CID */
1383 	wil->ring2cid_tid[id][1] = 0; /* TID */
1384 
1385 	cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
1386 
1387 	if (!vif->privacy)
1388 		txdata->dot1x_open = true;
1389 	rc = wmi_call(wil, WMI_BCAST_VRING_CFG_CMDID, vif->mid,
1390 		      &cmd, sizeof(cmd),
1391 		      WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply),
1392 		      WIL_WMI_CALL_GENERAL_TO_MS);
1393 	if (rc)
1394 		goto out_free;
1395 
1396 	if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
1397 		wil_err(wil, "Tx config failed, status 0x%02x\n",
1398 			reply.cmd.status);
1399 		rc = -EINVAL;
1400 		goto out_free;
1401 	}
1402 
1403 	spin_lock_bh(&txdata->lock);
1404 	vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
1405 	txdata->mid = vif->mid;
1406 	txdata->enabled = 1;
1407 	spin_unlock_bh(&txdata->lock);
1408 
1409 	return 0;
1410  out_free:
1411 	spin_lock_bh(&txdata->lock);
1412 	txdata->enabled = 0;
1413 	txdata->dot1x_open = false;
1414 	spin_unlock_bh(&txdata->lock);
1415 	wil_vring_free(wil, vring);
1416  out:
1417 
1418 	return rc;
1419 }
1420 
1421 static struct wil_ring *wil_find_tx_ucast(struct wil6210_priv *wil,
1422 					  struct wil6210_vif *vif,
1423 					  struct sk_buff *skb)
1424 {
1425 	int i, cid;
1426 	const u8 *da = wil_skb_get_da(skb);
1427 	int min_ring_id = wil_get_min_tx_ring_id(wil);
1428 
1429 	cid = wil_find_cid(wil, vif->mid, da);
1430 
1431 	if (cid < 0 || cid >= wil->max_assoc_sta)
1432 		return NULL;
1433 
1434 	/* TODO: fix for multiple TID */
1435 	for (i = min_ring_id; i < ARRAY_SIZE(wil->ring2cid_tid); i++) {
1436 		if (!wil->ring_tx_data[i].dot1x_open &&
1437 		    skb->protocol != cpu_to_be16(ETH_P_PAE))
1438 			continue;
1439 		if (wil->ring2cid_tid[i][0] == cid) {
1440 			struct wil_ring *v = &wil->ring_tx[i];
1441 			struct wil_ring_tx_data *txdata = &wil->ring_tx_data[i];
1442 
1443 			wil_dbg_txrx(wil, "find_tx_ucast: (%pM) -> [%d]\n",
1444 				     da, i);
1445 			if (v->va && txdata->enabled) {
1446 				return v;
1447 			} else {
1448 				wil_dbg_txrx(wil,
1449 					     "find_tx_ucast: vring[%d] not valid\n",
1450 					     i);
1451 				return NULL;
1452 			}
1453 		}
1454 	}
1455 
1456 	return NULL;
1457 }
1458 
1459 static int wil_tx_ring(struct wil6210_priv *wil, struct wil6210_vif *vif,
1460 		       struct wil_ring *ring, struct sk_buff *skb);
1461 
1462 static struct wil_ring *wil_find_tx_ring_sta(struct wil6210_priv *wil,
1463 					     struct wil6210_vif *vif,
1464 					     struct sk_buff *skb)
1465 {
1466 	struct wil_ring *ring;
1467 	int i;
1468 	u8 cid;
1469 	struct wil_ring_tx_data  *txdata;
1470 	int min_ring_id = wil_get_min_tx_ring_id(wil);
1471 
1472 	/* In the STA mode, it is expected to have only 1 VRING
1473 	 * for the AP we connected to.
1474 	 * find 1-st vring eligible for this skb and use it.
1475 	 */
1476 	for (i = min_ring_id; i < WIL6210_MAX_TX_RINGS; i++) {
1477 		ring = &wil->ring_tx[i];
1478 		txdata = &wil->ring_tx_data[i];
1479 		if (!ring->va || !txdata->enabled || txdata->mid != vif->mid)
1480 			continue;
1481 
1482 		cid = wil->ring2cid_tid[i][0];
1483 		if (cid >= wil->max_assoc_sta) /* skip BCAST */
1484 			continue;
1485 
1486 		if (!wil->ring_tx_data[i].dot1x_open &&
1487 		    skb->protocol != cpu_to_be16(ETH_P_PAE))
1488 			continue;
1489 
1490 		wil_dbg_txrx(wil, "Tx -> ring %d\n", i);
1491 
1492 		return ring;
1493 	}
1494 
1495 	wil_dbg_txrx(wil, "Tx while no rings active?\n");
1496 
1497 	return NULL;
1498 }
1499 
1500 /* Use one of 2 strategies:
1501  *
1502  * 1. New (real broadcast):
1503  *    use dedicated broadcast vring
1504  * 2. Old (pseudo-DMS):
1505  *    Find 1-st vring and return it;
1506  *    duplicate skb and send it to other active vrings;
1507  *    in all cases override dest address to unicast peer's address
1508  * Use old strategy when new is not supported yet:
1509  *  - for PBSS
1510  */
1511 static struct wil_ring *wil_find_tx_bcast_1(struct wil6210_priv *wil,
1512 					    struct wil6210_vif *vif,
1513 					    struct sk_buff *skb)
1514 {
1515 	struct wil_ring *v;
1516 	struct wil_ring_tx_data *txdata;
1517 	int i = vif->bcast_ring;
1518 
1519 	if (i < 0)
1520 		return NULL;
1521 	v = &wil->ring_tx[i];
1522 	txdata = &wil->ring_tx_data[i];
1523 	if (!v->va || !txdata->enabled)
1524 		return NULL;
1525 	if (!wil->ring_tx_data[i].dot1x_open &&
1526 	    skb->protocol != cpu_to_be16(ETH_P_PAE))
1527 		return NULL;
1528 
1529 	return v;
1530 }
1531 
1532 static void wil_set_da_for_vring(struct wil6210_priv *wil,
1533 				 struct sk_buff *skb, int vring_index)
1534 {
1535 	u8 *da = wil_skb_get_da(skb);
1536 	int cid = wil->ring2cid_tid[vring_index][0];
1537 
1538 	ether_addr_copy(da, wil->sta[cid].addr);
1539 }
1540 
1541 static struct wil_ring *wil_find_tx_bcast_2(struct wil6210_priv *wil,
1542 					    struct wil6210_vif *vif,
1543 					    struct sk_buff *skb)
1544 {
1545 	struct wil_ring *v, *v2;
1546 	struct sk_buff *skb2;
1547 	int i;
1548 	u8 cid;
1549 	const u8 *src = wil_skb_get_sa(skb);
1550 	struct wil_ring_tx_data *txdata, *txdata2;
1551 	int min_ring_id = wil_get_min_tx_ring_id(wil);
1552 
1553 	/* find 1-st vring eligible for data */
1554 	for (i = min_ring_id; i < WIL6210_MAX_TX_RINGS; i++) {
1555 		v = &wil->ring_tx[i];
1556 		txdata = &wil->ring_tx_data[i];
1557 		if (!v->va || !txdata->enabled || txdata->mid != vif->mid)
1558 			continue;
1559 
1560 		cid = wil->ring2cid_tid[i][0];
1561 		if (cid >= wil->max_assoc_sta) /* skip BCAST */
1562 			continue;
1563 		if (!wil->ring_tx_data[i].dot1x_open &&
1564 		    skb->protocol != cpu_to_be16(ETH_P_PAE))
1565 			continue;
1566 
1567 		/* don't Tx back to source when re-routing Rx->Tx at the AP */
1568 		if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1569 			continue;
1570 
1571 		goto found;
1572 	}
1573 
1574 	wil_dbg_txrx(wil, "Tx while no vrings active?\n");
1575 
1576 	return NULL;
1577 
1578 found:
1579 	wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
1580 	wil_set_da_for_vring(wil, skb, i);
1581 
1582 	/* find other active vrings and duplicate skb for each */
1583 	for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
1584 		v2 = &wil->ring_tx[i];
1585 		txdata2 = &wil->ring_tx_data[i];
1586 		if (!v2->va || txdata2->mid != vif->mid)
1587 			continue;
1588 		cid = wil->ring2cid_tid[i][0];
1589 		if (cid >= wil->max_assoc_sta) /* skip BCAST */
1590 			continue;
1591 		if (!wil->ring_tx_data[i].dot1x_open &&
1592 		    skb->protocol != cpu_to_be16(ETH_P_PAE))
1593 			continue;
1594 
1595 		if (0 == memcmp(wil->sta[cid].addr, src, ETH_ALEN))
1596 			continue;
1597 
1598 		skb2 = skb_copy(skb, GFP_ATOMIC);
1599 		if (skb2) {
1600 			wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
1601 			wil_set_da_for_vring(wil, skb2, i);
1602 			wil_tx_ring(wil, vif, v2, skb2);
1603 			/* successful call to wil_tx_ring takes skb2 ref */
1604 			dev_kfree_skb_any(skb2);
1605 		} else {
1606 			wil_err(wil, "skb_copy failed\n");
1607 		}
1608 	}
1609 
1610 	return v;
1611 }
1612 
1613 static inline
1614 void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
1615 {
1616 	d->mac.d[2] |= (nr_frags << MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
1617 }
1618 
1619 /**
1620  * Sets the descriptor @d up for csum and/or TSO offloading. The corresponding
1621  * @skb is used to obtain the protocol and headers length.
1622  * @tso_desc_type is a descriptor type for TSO: 0 - a header, 1 - first data,
1623  * 2 - middle, 3 - last descriptor.
1624  */
1625 
1626 static void wil_tx_desc_offload_setup_tso(struct vring_tx_desc *d,
1627 					  struct sk_buff *skb,
1628 					  int tso_desc_type, bool is_ipv4,
1629 					  int tcp_hdr_len, int skb_net_hdr_len)
1630 {
1631 	d->dma.b11 = ETH_HLEN; /* MAC header length */
1632 	d->dma.b11 |= is_ipv4 << DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS;
1633 
1634 	d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1635 	/* L4 header len: TCP header length */
1636 	d->dma.d0 |= (tcp_hdr_len & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1637 
1638 	/* Setup TSO: bit and desc type */
1639 	d->dma.d0 |= (BIT(DMA_CFG_DESC_TX_0_TCP_SEG_EN_POS)) |
1640 		(tso_desc_type << DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS);
1641 	d->dma.d0 |= (is_ipv4 << DMA_CFG_DESC_TX_0_IPV4_CHECKSUM_EN_POS);
1642 
1643 	d->dma.ip_length = skb_net_hdr_len;
1644 	/* Enable TCP/UDP checksum */
1645 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1646 	/* Calculate pseudo-header */
1647 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1648 }
1649 
1650 /**
1651  * Sets the descriptor @d up for csum. The corresponding
1652  * @skb is used to obtain the protocol and headers length.
1653  * Returns the protocol: 0 - not TCP, 1 - TCPv4, 2 - TCPv6.
1654  * Note, if d==NULL, the function only returns the protocol result.
1655  *
1656  * It is very similar to previous wil_tx_desc_offload_setup_tso. This
1657  * is "if unrolling" to optimize the critical path.
1658  */
1659 
1660 static int wil_tx_desc_offload_setup(struct vring_tx_desc *d,
1661 				     struct sk_buff *skb){
1662 	int protocol;
1663 
1664 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1665 		return 0;
1666 
1667 	d->dma.b11 = ETH_HLEN; /* MAC header length */
1668 
1669 	switch (skb->protocol) {
1670 	case cpu_to_be16(ETH_P_IP):
1671 		protocol = ip_hdr(skb)->protocol;
1672 		d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
1673 		break;
1674 	case cpu_to_be16(ETH_P_IPV6):
1675 		protocol = ipv6_hdr(skb)->nexthdr;
1676 		break;
1677 	default:
1678 		return -EINVAL;
1679 	}
1680 
1681 	switch (protocol) {
1682 	case IPPROTO_TCP:
1683 		d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
1684 		/* L4 header len: TCP header length */
1685 		d->dma.d0 |=
1686 		(tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1687 		break;
1688 	case IPPROTO_UDP:
1689 		/* L4 header len: UDP header length */
1690 		d->dma.d0 |=
1691 		(sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
1692 		break;
1693 	default:
1694 		return -EINVAL;
1695 	}
1696 
1697 	d->dma.ip_length = skb_network_header_len(skb);
1698 	/* Enable TCP/UDP checksum */
1699 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
1700 	/* Calculate pseudo-header */
1701 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
1702 
1703 	return 0;
1704 }
1705 
1706 static inline void wil_tx_last_desc(struct vring_tx_desc *d)
1707 {
1708 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS) |
1709 	      BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS) |
1710 	      BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
1711 }
1712 
1713 static inline void wil_set_tx_desc_last_tso(volatile struct vring_tx_desc *d)
1714 {
1715 	d->dma.d0 |= wil_tso_type_lst <<
1716 		  DMA_CFG_DESC_TX_0_SEGMENT_BUF_DETAILS_POS;
1717 }
1718 
1719 static int __wil_tx_vring_tso(struct wil6210_priv *wil, struct wil6210_vif *vif,
1720 			      struct wil_ring *vring, struct sk_buff *skb)
1721 {
1722 	struct device *dev = wil_to_dev(wil);
1723 
1724 	/* point to descriptors in shared memory */
1725 	volatile struct vring_tx_desc *_desc = NULL, *_hdr_desc,
1726 				      *_first_desc = NULL;
1727 
1728 	/* pointers to shadow descriptors */
1729 	struct vring_tx_desc desc_mem, hdr_desc_mem, first_desc_mem,
1730 			     *d = &hdr_desc_mem, *hdr_desc = &hdr_desc_mem,
1731 			     *first_desc = &first_desc_mem;
1732 
1733 	/* pointer to shadow descriptors' context */
1734 	struct wil_ctx *hdr_ctx, *first_ctx = NULL;
1735 
1736 	int descs_used = 0; /* total number of used descriptors */
1737 	int sg_desc_cnt = 0; /* number of descriptors for current mss*/
1738 
1739 	u32 swhead = vring->swhead;
1740 	int used, avail = wil_ring_avail_tx(vring);
1741 	int nr_frags = skb_shinfo(skb)->nr_frags;
1742 	int min_desc_required = nr_frags + 1;
1743 	int mss = skb_shinfo(skb)->gso_size;	/* payload size w/o headers */
1744 	int f, len, hdrlen, headlen;
1745 	int vring_index = vring - wil->ring_tx;
1746 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[vring_index];
1747 	uint i = swhead;
1748 	dma_addr_t pa;
1749 	const skb_frag_t *frag = NULL;
1750 	int rem_data = mss;
1751 	int lenmss;
1752 	int hdr_compensation_need = true;
1753 	int desc_tso_type = wil_tso_type_first;
1754 	bool is_ipv4;
1755 	int tcp_hdr_len;
1756 	int skb_net_hdr_len;
1757 	int gso_type;
1758 	int rc = -EINVAL;
1759 
1760 	wil_dbg_txrx(wil, "tx_vring_tso: %d bytes to vring %d\n", skb->len,
1761 		     vring_index);
1762 
1763 	if (unlikely(!txdata->enabled))
1764 		return -EINVAL;
1765 
1766 	/* A typical page 4K is 3-4 payloads, we assume each fragment
1767 	 * is a full payload, that's how min_desc_required has been
1768 	 * calculated. In real we might need more or less descriptors,
1769 	 * this is the initial check only.
1770 	 */
1771 	if (unlikely(avail < min_desc_required)) {
1772 		wil_err_ratelimited(wil,
1773 				    "TSO: Tx ring[%2d] full. No space for %d fragments\n",
1774 				    vring_index, min_desc_required);
1775 		return -ENOMEM;
1776 	}
1777 
1778 	/* Header Length = MAC header len + IP header len + TCP header len*/
1779 	hdrlen = ETH_HLEN +
1780 		(int)skb_network_header_len(skb) +
1781 		tcp_hdrlen(skb);
1782 
1783 	gso_type = skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4);
1784 	switch (gso_type) {
1785 	case SKB_GSO_TCPV4:
1786 		/* TCP v4, zero out the IP length and IPv4 checksum fields
1787 		 * as required by the offloading doc
1788 		 */
1789 		ip_hdr(skb)->tot_len = 0;
1790 		ip_hdr(skb)->check = 0;
1791 		is_ipv4 = true;
1792 		break;
1793 	case SKB_GSO_TCPV6:
1794 		/* TCP v6, zero out the payload length */
1795 		ipv6_hdr(skb)->payload_len = 0;
1796 		is_ipv4 = false;
1797 		break;
1798 	default:
1799 		/* other than TCPv4 or TCPv6 types are not supported for TSO.
1800 		 * It is also illegal for both to be set simultaneously
1801 		 */
1802 		return -EINVAL;
1803 	}
1804 
1805 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1806 		return -EINVAL;
1807 
1808 	/* tcp header length and skb network header length are fixed for all
1809 	 * packet's descriptors - read then once here
1810 	 */
1811 	tcp_hdr_len = tcp_hdrlen(skb);
1812 	skb_net_hdr_len = skb_network_header_len(skb);
1813 
1814 	_hdr_desc = &vring->va[i].tx.legacy;
1815 
1816 	pa = dma_map_single(dev, skb->data, hdrlen, DMA_TO_DEVICE);
1817 	if (unlikely(dma_mapping_error(dev, pa))) {
1818 		wil_err(wil, "TSO: Skb head DMA map error\n");
1819 		goto err_exit;
1820 	}
1821 
1822 	wil->txrx_ops.tx_desc_map((union wil_tx_desc *)hdr_desc, pa,
1823 				  hdrlen, vring_index);
1824 	wil_tx_desc_offload_setup_tso(hdr_desc, skb, wil_tso_type_hdr, is_ipv4,
1825 				      tcp_hdr_len, skb_net_hdr_len);
1826 	wil_tx_last_desc(hdr_desc);
1827 
1828 	vring->ctx[i].mapped_as = wil_mapped_as_single;
1829 	hdr_ctx = &vring->ctx[i];
1830 
1831 	descs_used++;
1832 	headlen = skb_headlen(skb) - hdrlen;
1833 
1834 	for (f = headlen ? -1 : 0; f < nr_frags; f++)  {
1835 		if (headlen) {
1836 			len = headlen;
1837 			wil_dbg_txrx(wil, "TSO: process skb head, len %u\n",
1838 				     len);
1839 		} else {
1840 			frag = &skb_shinfo(skb)->frags[f];
1841 			len = skb_frag_size(frag);
1842 			wil_dbg_txrx(wil, "TSO: frag[%d]: len %u\n", f, len);
1843 		}
1844 
1845 		while (len) {
1846 			wil_dbg_txrx(wil,
1847 				     "TSO: len %d, rem_data %d, descs_used %d\n",
1848 				     len, rem_data, descs_used);
1849 
1850 			if (descs_used == avail)  {
1851 				wil_err_ratelimited(wil, "TSO: ring overflow\n");
1852 				rc = -ENOMEM;
1853 				goto mem_error;
1854 			}
1855 
1856 			lenmss = min_t(int, rem_data, len);
1857 			i = (swhead + descs_used) % vring->size;
1858 			wil_dbg_txrx(wil, "TSO: lenmss %d, i %d\n", lenmss, i);
1859 
1860 			if (!headlen) {
1861 				pa = skb_frag_dma_map(dev, frag,
1862 						      skb_frag_size(frag) - len,
1863 						      lenmss, DMA_TO_DEVICE);
1864 				vring->ctx[i].mapped_as = wil_mapped_as_page;
1865 			} else {
1866 				pa = dma_map_single(dev,
1867 						    skb->data +
1868 						    skb_headlen(skb) - headlen,
1869 						    lenmss,
1870 						    DMA_TO_DEVICE);
1871 				vring->ctx[i].mapped_as = wil_mapped_as_single;
1872 				headlen -= lenmss;
1873 			}
1874 
1875 			if (unlikely(dma_mapping_error(dev, pa))) {
1876 				wil_err(wil, "TSO: DMA map page error\n");
1877 				goto mem_error;
1878 			}
1879 
1880 			_desc = &vring->va[i].tx.legacy;
1881 
1882 			if (!_first_desc) {
1883 				_first_desc = _desc;
1884 				first_ctx = &vring->ctx[i];
1885 				d = first_desc;
1886 			} else {
1887 				d = &desc_mem;
1888 			}
1889 
1890 			wil->txrx_ops.tx_desc_map((union wil_tx_desc *)d,
1891 						  pa, lenmss, vring_index);
1892 			wil_tx_desc_offload_setup_tso(d, skb, desc_tso_type,
1893 						      is_ipv4, tcp_hdr_len,
1894 						      skb_net_hdr_len);
1895 
1896 			/* use tso_type_first only once */
1897 			desc_tso_type = wil_tso_type_mid;
1898 
1899 			descs_used++;  /* desc used so far */
1900 			sg_desc_cnt++; /* desc used for this segment */
1901 			len -= lenmss;
1902 			rem_data -= lenmss;
1903 
1904 			wil_dbg_txrx(wil,
1905 				     "TSO: len %d, rem_data %d, descs_used %d, sg_desc_cnt %d,\n",
1906 				     len, rem_data, descs_used, sg_desc_cnt);
1907 
1908 			/* Close the segment if reached mss size or last frag*/
1909 			if (rem_data == 0 || (f == nr_frags - 1 && len == 0)) {
1910 				if (hdr_compensation_need) {
1911 					/* first segment include hdr desc for
1912 					 * release
1913 					 */
1914 					hdr_ctx->nr_frags = sg_desc_cnt;
1915 					wil_tx_desc_set_nr_frags(first_desc,
1916 								 sg_desc_cnt +
1917 								 1);
1918 					hdr_compensation_need = false;
1919 				} else {
1920 					wil_tx_desc_set_nr_frags(first_desc,
1921 								 sg_desc_cnt);
1922 				}
1923 				first_ctx->nr_frags = sg_desc_cnt - 1;
1924 
1925 				wil_tx_last_desc(d);
1926 
1927 				/* first descriptor may also be the last
1928 				 * for this mss - make sure not to copy
1929 				 * it twice
1930 				 */
1931 				if (first_desc != d)
1932 					*_first_desc = *first_desc;
1933 
1934 				/*last descriptor will be copied at the end
1935 				 * of this TS processing
1936 				 */
1937 				if (f < nr_frags - 1 || len > 0)
1938 					*_desc = *d;
1939 
1940 				rem_data = mss;
1941 				_first_desc = NULL;
1942 				sg_desc_cnt = 0;
1943 			} else if (first_desc != d) /* update mid descriptor */
1944 					*_desc = *d;
1945 		}
1946 	}
1947 
1948 	if (!_desc)
1949 		goto mem_error;
1950 
1951 	/* first descriptor may also be the last.
1952 	 * in this case d pointer is invalid
1953 	 */
1954 	if (_first_desc == _desc)
1955 		d = first_desc;
1956 
1957 	/* Last data descriptor */
1958 	wil_set_tx_desc_last_tso(d);
1959 	*_desc = *d;
1960 
1961 	/* Fill the total number of descriptors in first desc (hdr)*/
1962 	wil_tx_desc_set_nr_frags(hdr_desc, descs_used);
1963 	*_hdr_desc = *hdr_desc;
1964 
1965 	/* hold reference to skb
1966 	 * to prevent skb release before accounting
1967 	 * in case of immediate "tx done"
1968 	 */
1969 	vring->ctx[i].skb = skb_get(skb);
1970 
1971 	/* performance monitoring */
1972 	used = wil_ring_used_tx(vring);
1973 	if (wil_val_in_range(wil->ring_idle_trsh,
1974 			     used, used + descs_used)) {
1975 		txdata->idle += get_cycles() - txdata->last_idle;
1976 		wil_dbg_txrx(wil,  "Ring[%2d] not idle %d -> %d\n",
1977 			     vring_index, used, used + descs_used);
1978 	}
1979 
1980 	/* Make sure to advance the head only after descriptor update is done.
1981 	 * This will prevent a race condition where the completion thread
1982 	 * will see the DU bit set from previous run and will handle the
1983 	 * skb before it was completed.
1984 	 */
1985 	wmb();
1986 
1987 	/* advance swhead */
1988 	wil_ring_advance_head(vring, descs_used);
1989 	wil_dbg_txrx(wil, "TSO: Tx swhead %d -> %d\n", swhead, vring->swhead);
1990 
1991 	/* make sure all writes to descriptors (shared memory) are done before
1992 	 * committing them to HW
1993 	 */
1994 	wmb();
1995 
1996 	if (wil->tx_latency)
1997 		*(ktime_t *)&skb->cb = ktime_get();
1998 	else
1999 		memset(skb->cb, 0, sizeof(ktime_t));
2000 
2001 	wil_w(wil, vring->hwtail, vring->swhead);
2002 	return 0;
2003 
2004 mem_error:
2005 	while (descs_used > 0) {
2006 		struct wil_ctx *ctx;
2007 
2008 		i = (swhead + descs_used - 1) % vring->size;
2009 		d = (struct vring_tx_desc *)&vring->va[i].tx.legacy;
2010 		_desc = &vring->va[i].tx.legacy;
2011 		*d = *_desc;
2012 		_desc->dma.status = TX_DMA_STATUS_DU;
2013 		ctx = &vring->ctx[i];
2014 		wil_txdesc_unmap(dev, (union wil_tx_desc *)d, ctx);
2015 		memset(ctx, 0, sizeof(*ctx));
2016 		descs_used--;
2017 	}
2018 err_exit:
2019 	return rc;
2020 }
2021 
2022 static int __wil_tx_ring(struct wil6210_priv *wil, struct wil6210_vif *vif,
2023 			 struct wil_ring *ring, struct sk_buff *skb)
2024 {
2025 	struct device *dev = wil_to_dev(wil);
2026 	struct vring_tx_desc dd, *d = &dd;
2027 	volatile struct vring_tx_desc *_d;
2028 	u32 swhead = ring->swhead;
2029 	int avail = wil_ring_avail_tx(ring);
2030 	int nr_frags = skb_shinfo(skb)->nr_frags;
2031 	uint f = 0;
2032 	int ring_index = ring - wil->ring_tx;
2033 	struct wil_ring_tx_data  *txdata = &wil->ring_tx_data[ring_index];
2034 	uint i = swhead;
2035 	dma_addr_t pa;
2036 	int used;
2037 	bool mcast = (ring_index == vif->bcast_ring);
2038 	uint len = skb_headlen(skb);
2039 
2040 	wil_dbg_txrx(wil, "tx_ring: %d bytes to ring %d, nr_frags %d\n",
2041 		     skb->len, ring_index, nr_frags);
2042 
2043 	if (unlikely(!txdata->enabled))
2044 		return -EINVAL;
2045 
2046 	if (unlikely(avail < 1 + nr_frags)) {
2047 		wil_err_ratelimited(wil,
2048 				    "Tx ring[%2d] full. No space for %d fragments\n",
2049 				    ring_index, 1 + nr_frags);
2050 		return -ENOMEM;
2051 	}
2052 	_d = &ring->va[i].tx.legacy;
2053 
2054 	pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
2055 
2056 	wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", ring_index,
2057 		     skb_headlen(skb), skb->data, &pa);
2058 	wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
2059 			  skb->data, skb_headlen(skb), false);
2060 
2061 	if (unlikely(dma_mapping_error(dev, pa)))
2062 		return -EINVAL;
2063 	ring->ctx[i].mapped_as = wil_mapped_as_single;
2064 	/* 1-st segment */
2065 	wil->txrx_ops.tx_desc_map((union wil_tx_desc *)d, pa, len,
2066 				   ring_index);
2067 	if (unlikely(mcast)) {
2068 		d->mac.d[0] |= BIT(MAC_CFG_DESC_TX_0_MCS_EN_POS); /* MCS 0 */
2069 		if (unlikely(len > WIL_BCAST_MCS0_LIMIT)) /* set MCS 1 */
2070 			d->mac.d[0] |= (1 << MAC_CFG_DESC_TX_0_MCS_INDEX_POS);
2071 	}
2072 	/* Process TCP/UDP checksum offloading */
2073 	if (unlikely(wil_tx_desc_offload_setup(d, skb))) {
2074 		wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n",
2075 			ring_index);
2076 		goto dma_error;
2077 	}
2078 
2079 	ring->ctx[i].nr_frags = nr_frags;
2080 	wil_tx_desc_set_nr_frags(d, nr_frags + 1);
2081 
2082 	/* middle segments */
2083 	for (; f < nr_frags; f++) {
2084 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
2085 		int len = skb_frag_size(frag);
2086 
2087 		*_d = *d;
2088 		wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", ring_index, i);
2089 		wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
2090 				  (const void *)d, sizeof(*d), false);
2091 		i = (swhead + f + 1) % ring->size;
2092 		_d = &ring->va[i].tx.legacy;
2093 		pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
2094 				      DMA_TO_DEVICE);
2095 		if (unlikely(dma_mapping_error(dev, pa))) {
2096 			wil_err(wil, "Tx[%2d] failed to map fragment\n",
2097 				ring_index);
2098 			goto dma_error;
2099 		}
2100 		ring->ctx[i].mapped_as = wil_mapped_as_page;
2101 		wil->txrx_ops.tx_desc_map((union wil_tx_desc *)d,
2102 					   pa, len, ring_index);
2103 		/* no need to check return code -
2104 		 * if it succeeded for 1-st descriptor,
2105 		 * it will succeed here too
2106 		 */
2107 		wil_tx_desc_offload_setup(d, skb);
2108 	}
2109 	/* for the last seg only */
2110 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
2111 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
2112 	d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
2113 	*_d = *d;
2114 	wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", ring_index, i);
2115 	wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
2116 			  (const void *)d, sizeof(*d), false);
2117 
2118 	/* hold reference to skb
2119 	 * to prevent skb release before accounting
2120 	 * in case of immediate "tx done"
2121 	 */
2122 	ring->ctx[i].skb = skb_get(skb);
2123 
2124 	/* performance monitoring */
2125 	used = wil_ring_used_tx(ring);
2126 	if (wil_val_in_range(wil->ring_idle_trsh,
2127 			     used, used + nr_frags + 1)) {
2128 		txdata->idle += get_cycles() - txdata->last_idle;
2129 		wil_dbg_txrx(wil,  "Ring[%2d] not idle %d -> %d\n",
2130 			     ring_index, used, used + nr_frags + 1);
2131 	}
2132 
2133 	/* Make sure to advance the head only after descriptor update is done.
2134 	 * This will prevent a race condition where the completion thread
2135 	 * will see the DU bit set from previous run and will handle the
2136 	 * skb before it was completed.
2137 	 */
2138 	wmb();
2139 
2140 	/* advance swhead */
2141 	wil_ring_advance_head(ring, nr_frags + 1);
2142 	wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", ring_index, swhead,
2143 		     ring->swhead);
2144 	trace_wil6210_tx(ring_index, swhead, skb->len, nr_frags);
2145 
2146 	/* make sure all writes to descriptors (shared memory) are done before
2147 	 * committing them to HW
2148 	 */
2149 	wmb();
2150 
2151 	if (wil->tx_latency)
2152 		*(ktime_t *)&skb->cb = ktime_get();
2153 	else
2154 		memset(skb->cb, 0, sizeof(ktime_t));
2155 
2156 	wil_w(wil, ring->hwtail, ring->swhead);
2157 
2158 	return 0;
2159  dma_error:
2160 	/* unmap what we have mapped */
2161 	nr_frags = f + 1; /* frags mapped + one for skb head */
2162 	for (f = 0; f < nr_frags; f++) {
2163 		struct wil_ctx *ctx;
2164 
2165 		i = (swhead + f) % ring->size;
2166 		ctx = &ring->ctx[i];
2167 		_d = &ring->va[i].tx.legacy;
2168 		*d = *_d;
2169 		_d->dma.status = TX_DMA_STATUS_DU;
2170 		wil->txrx_ops.tx_desc_unmap(dev,
2171 					    (union wil_tx_desc *)d,
2172 					    ctx);
2173 
2174 		memset(ctx, 0, sizeof(*ctx));
2175 	}
2176 
2177 	return -EINVAL;
2178 }
2179 
2180 static int wil_tx_ring(struct wil6210_priv *wil, struct wil6210_vif *vif,
2181 		       struct wil_ring *ring, struct sk_buff *skb)
2182 {
2183 	int ring_index = ring - wil->ring_tx;
2184 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ring_index];
2185 	int rc;
2186 
2187 	spin_lock(&txdata->lock);
2188 
2189 	if (test_bit(wil_status_suspending, wil->status) ||
2190 	    test_bit(wil_status_suspended, wil->status) ||
2191 	    test_bit(wil_status_resuming, wil->status)) {
2192 		wil_dbg_txrx(wil,
2193 			     "suspend/resume in progress. drop packet\n");
2194 		spin_unlock(&txdata->lock);
2195 		return -EINVAL;
2196 	}
2197 
2198 	rc = (skb_is_gso(skb) ? wil->txrx_ops.tx_ring_tso : __wil_tx_ring)
2199 	     (wil, vif, ring, skb);
2200 
2201 	spin_unlock(&txdata->lock);
2202 
2203 	return rc;
2204 }
2205 
2206 /**
2207  * Check status of tx vrings and stop/wake net queues if needed
2208  * It will start/stop net queues of a specific VIF net_device.
2209  *
2210  * This function does one of two checks:
2211  * In case check_stop is true, will check if net queues need to be stopped. If
2212  * the conditions for stopping are met, netif_tx_stop_all_queues() is called.
2213  * In case check_stop is false, will check if net queues need to be waked. If
2214  * the conditions for waking are met, netif_tx_wake_all_queues() is called.
2215  * vring is the vring which is currently being modified by either adding
2216  * descriptors (tx) into it or removing descriptors (tx complete) from it. Can
2217  * be null when irrelevant (e.g. connect/disconnect events).
2218  *
2219  * The implementation is to stop net queues if modified vring has low
2220  * descriptor availability. Wake if all vrings are not in low descriptor
2221  * availability and modified vring has high descriptor availability.
2222  */
2223 static inline void __wil_update_net_queues(struct wil6210_priv *wil,
2224 					   struct wil6210_vif *vif,
2225 					   struct wil_ring *ring,
2226 					   bool check_stop)
2227 {
2228 	int i;
2229 	int min_ring_id = wil_get_min_tx_ring_id(wil);
2230 
2231 	if (unlikely(!vif))
2232 		return;
2233 
2234 	if (ring)
2235 		wil_dbg_txrx(wil, "vring %d, mid %d, check_stop=%d, stopped=%d",
2236 			     (int)(ring - wil->ring_tx), vif->mid, check_stop,
2237 			     vif->net_queue_stopped);
2238 	else
2239 		wil_dbg_txrx(wil, "check_stop=%d, mid=%d, stopped=%d",
2240 			     check_stop, vif->mid, vif->net_queue_stopped);
2241 
2242 	if (ring && drop_if_ring_full)
2243 		/* no need to stop/wake net queues */
2244 		return;
2245 
2246 	if (check_stop == vif->net_queue_stopped)
2247 		/* net queues already in desired state */
2248 		return;
2249 
2250 	if (check_stop) {
2251 		if (!ring || unlikely(wil_ring_avail_low(ring))) {
2252 			/* not enough room in the vring */
2253 			netif_tx_stop_all_queues(vif_to_ndev(vif));
2254 			vif->net_queue_stopped = true;
2255 			wil_dbg_txrx(wil, "netif_tx_stop called\n");
2256 		}
2257 		return;
2258 	}
2259 
2260 	/* Do not wake the queues in suspend flow */
2261 	if (test_bit(wil_status_suspending, wil->status) ||
2262 	    test_bit(wil_status_suspended, wil->status))
2263 		return;
2264 
2265 	/* check wake */
2266 	for (i = min_ring_id; i < WIL6210_MAX_TX_RINGS; i++) {
2267 		struct wil_ring *cur_ring = &wil->ring_tx[i];
2268 		struct wil_ring_tx_data  *txdata = &wil->ring_tx_data[i];
2269 
2270 		if (txdata->mid != vif->mid || !cur_ring->va ||
2271 		    !txdata->enabled || cur_ring == ring)
2272 			continue;
2273 
2274 		if (wil_ring_avail_low(cur_ring)) {
2275 			wil_dbg_txrx(wil, "ring %d full, can't wake\n",
2276 				     (int)(cur_ring - wil->ring_tx));
2277 			return;
2278 		}
2279 	}
2280 
2281 	if (!ring || wil_ring_avail_high(ring)) {
2282 		/* enough room in the ring */
2283 		wil_dbg_txrx(wil, "calling netif_tx_wake\n");
2284 		netif_tx_wake_all_queues(vif_to_ndev(vif));
2285 		vif->net_queue_stopped = false;
2286 	}
2287 }
2288 
2289 void wil_update_net_queues(struct wil6210_priv *wil, struct wil6210_vif *vif,
2290 			   struct wil_ring *ring, bool check_stop)
2291 {
2292 	spin_lock(&wil->net_queue_lock);
2293 	__wil_update_net_queues(wil, vif, ring, check_stop);
2294 	spin_unlock(&wil->net_queue_lock);
2295 }
2296 
2297 void wil_update_net_queues_bh(struct wil6210_priv *wil, struct wil6210_vif *vif,
2298 			      struct wil_ring *ring, bool check_stop)
2299 {
2300 	spin_lock_bh(&wil->net_queue_lock);
2301 	__wil_update_net_queues(wil, vif, ring, check_stop);
2302 	spin_unlock_bh(&wil->net_queue_lock);
2303 }
2304 
2305 netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2306 {
2307 	struct wil6210_vif *vif = ndev_to_vif(ndev);
2308 	struct wil6210_priv *wil = vif_to_wil(vif);
2309 	const u8 *da = wil_skb_get_da(skb);
2310 	bool bcast = is_multicast_ether_addr(da);
2311 	struct wil_ring *ring;
2312 	static bool pr_once_fw;
2313 	int rc;
2314 
2315 	wil_dbg_txrx(wil, "start_xmit\n");
2316 	if (unlikely(!test_bit(wil_status_fwready, wil->status))) {
2317 		if (!pr_once_fw) {
2318 			wil_err(wil, "FW not ready\n");
2319 			pr_once_fw = true;
2320 		}
2321 		goto drop;
2322 	}
2323 	if (unlikely(!test_bit(wil_vif_fwconnected, vif->status))) {
2324 		wil_dbg_ratelimited(wil,
2325 				    "VIF not connected, packet dropped\n");
2326 		goto drop;
2327 	}
2328 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_MONITOR)) {
2329 		wil_err(wil, "Xmit in monitor mode not supported\n");
2330 		goto drop;
2331 	}
2332 	pr_once_fw = false;
2333 
2334 	/* find vring */
2335 	if (vif->wdev.iftype == NL80211_IFTYPE_STATION && !vif->pbss) {
2336 		/* in STA mode (ESS), all to same VRING (to AP) */
2337 		ring = wil_find_tx_ring_sta(wil, vif, skb);
2338 	} else if (bcast) {
2339 		if (vif->pbss)
2340 			/* in pbss, no bcast VRING - duplicate skb in
2341 			 * all stations VRINGs
2342 			 */
2343 			ring = wil_find_tx_bcast_2(wil, vif, skb);
2344 		else if (vif->wdev.iftype == NL80211_IFTYPE_AP)
2345 			/* AP has a dedicated bcast VRING */
2346 			ring = wil_find_tx_bcast_1(wil, vif, skb);
2347 		else
2348 			/* unexpected combination, fallback to duplicating
2349 			 * the skb in all stations VRINGs
2350 			 */
2351 			ring = wil_find_tx_bcast_2(wil, vif, skb);
2352 	} else {
2353 		/* unicast, find specific VRING by dest. address */
2354 		ring = wil_find_tx_ucast(wil, vif, skb);
2355 	}
2356 	if (unlikely(!ring)) {
2357 		wil_dbg_txrx(wil, "No Tx RING found for %pM\n", da);
2358 		goto drop;
2359 	}
2360 	/* set up vring entry */
2361 	rc = wil_tx_ring(wil, vif, ring, skb);
2362 
2363 	switch (rc) {
2364 	case 0:
2365 		/* shall we stop net queues? */
2366 		wil_update_net_queues_bh(wil, vif, ring, true);
2367 		/* statistics will be updated on the tx_complete */
2368 		dev_kfree_skb_any(skb);
2369 		return NETDEV_TX_OK;
2370 	case -ENOMEM:
2371 		if (drop_if_ring_full)
2372 			goto drop;
2373 		return NETDEV_TX_BUSY;
2374 	default:
2375 		break; /* goto drop; */
2376 	}
2377  drop:
2378 	ndev->stats.tx_dropped++;
2379 	dev_kfree_skb_any(skb);
2380 
2381 	return NET_XMIT_DROP;
2382 }
2383 
2384 void wil_tx_latency_calc(struct wil6210_priv *wil, struct sk_buff *skb,
2385 			 struct wil_sta_info *sta)
2386 {
2387 	int skb_time_us;
2388 	int bin;
2389 
2390 	if (!wil->tx_latency)
2391 		return;
2392 
2393 	if (ktime_to_ms(*(ktime_t *)&skb->cb) == 0)
2394 		return;
2395 
2396 	skb_time_us = ktime_us_delta(ktime_get(), *(ktime_t *)&skb->cb);
2397 	bin = skb_time_us / wil->tx_latency_res;
2398 	bin = min_t(int, bin, WIL_NUM_LATENCY_BINS - 1);
2399 
2400 	wil_dbg_txrx(wil, "skb time %dus => bin %d\n", skb_time_us, bin);
2401 	sta->tx_latency_bins[bin]++;
2402 	sta->stats.tx_latency_total_us += skb_time_us;
2403 	if (skb_time_us < sta->stats.tx_latency_min_us)
2404 		sta->stats.tx_latency_min_us = skb_time_us;
2405 	if (skb_time_us > sta->stats.tx_latency_max_us)
2406 		sta->stats.tx_latency_max_us = skb_time_us;
2407 }
2408 
2409 /**
2410  * Clean up transmitted skb's from the Tx VRING
2411  *
2412  * Return number of descriptors cleared
2413  *
2414  * Safe to call from IRQ
2415  */
2416 int wil_tx_complete(struct wil6210_vif *vif, int ringid)
2417 {
2418 	struct wil6210_priv *wil = vif_to_wil(vif);
2419 	struct net_device *ndev = vif_to_ndev(vif);
2420 	struct device *dev = wil_to_dev(wil);
2421 	struct wil_ring *vring = &wil->ring_tx[ringid];
2422 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ringid];
2423 	int done = 0;
2424 	int cid = wil->ring2cid_tid[ringid][0];
2425 	struct wil_net_stats *stats = NULL;
2426 	volatile struct vring_tx_desc *_d;
2427 	int used_before_complete;
2428 	int used_new;
2429 
2430 	if (unlikely(!vring->va)) {
2431 		wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
2432 		return 0;
2433 	}
2434 
2435 	if (unlikely(!txdata->enabled)) {
2436 		wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
2437 		return 0;
2438 	}
2439 
2440 	wil_dbg_txrx(wil, "tx_complete: (%d)\n", ringid);
2441 
2442 	used_before_complete = wil_ring_used_tx(vring);
2443 
2444 	if (cid < wil->max_assoc_sta)
2445 		stats = &wil->sta[cid].stats;
2446 
2447 	while (!wil_ring_is_empty(vring)) {
2448 		int new_swtail;
2449 		struct wil_ctx *ctx = &vring->ctx[vring->swtail];
2450 		/**
2451 		 * For the fragmented skb, HW will set DU bit only for the
2452 		 * last fragment. look for it.
2453 		 * In TSO the first DU will include hdr desc
2454 		 */
2455 		int lf = (vring->swtail + ctx->nr_frags) % vring->size;
2456 		/* TODO: check we are not past head */
2457 
2458 		_d = &vring->va[lf].tx.legacy;
2459 		if (unlikely(!(_d->dma.status & TX_DMA_STATUS_DU)))
2460 			break;
2461 
2462 		new_swtail = (lf + 1) % vring->size;
2463 		while (vring->swtail != new_swtail) {
2464 			struct vring_tx_desc dd, *d = &dd;
2465 			u16 dmalen;
2466 			struct sk_buff *skb;
2467 
2468 			ctx = &vring->ctx[vring->swtail];
2469 			skb = ctx->skb;
2470 			_d = &vring->va[vring->swtail].tx.legacy;
2471 
2472 			*d = *_d;
2473 
2474 			dmalen = le16_to_cpu(d->dma.length);
2475 			trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
2476 					      d->dma.error);
2477 			wil_dbg_txrx(wil,
2478 				     "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n",
2479 				     ringid, vring->swtail, dmalen,
2480 				     d->dma.status, d->dma.error);
2481 			wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4,
2482 					  (const void *)d, sizeof(*d), false);
2483 
2484 			wil->txrx_ops.tx_desc_unmap(dev,
2485 						    (union wil_tx_desc *)d,
2486 						    ctx);
2487 
2488 			if (skb) {
2489 				if (likely(d->dma.error == 0)) {
2490 					ndev->stats.tx_packets++;
2491 					ndev->stats.tx_bytes += skb->len;
2492 					if (stats) {
2493 						stats->tx_packets++;
2494 						stats->tx_bytes += skb->len;
2495 
2496 						wil_tx_latency_calc(wil, skb,
2497 							&wil->sta[cid]);
2498 					}
2499 				} else {
2500 					ndev->stats.tx_errors++;
2501 					if (stats)
2502 						stats->tx_errors++;
2503 				}
2504 
2505 				if (skb->protocol == cpu_to_be16(ETH_P_PAE))
2506 					wil_tx_complete_handle_eapol(vif, skb);
2507 
2508 				wil_consume_skb(skb, d->dma.error == 0);
2509 			}
2510 			memset(ctx, 0, sizeof(*ctx));
2511 			/* Make sure the ctx is zeroed before updating the tail
2512 			 * to prevent a case where wil_tx_ring will see
2513 			 * this descriptor as used and handle it before ctx zero
2514 			 * is completed.
2515 			 */
2516 			wmb();
2517 			/* There is no need to touch HW descriptor:
2518 			 * - ststus bit TX_DMA_STATUS_DU is set by design,
2519 			 *   so hardware will not try to process this desc.,
2520 			 * - rest of descriptor will be initialized on Tx.
2521 			 */
2522 			vring->swtail = wil_ring_next_tail(vring);
2523 			done++;
2524 		}
2525 	}
2526 
2527 	/* performance monitoring */
2528 	used_new = wil_ring_used_tx(vring);
2529 	if (wil_val_in_range(wil->ring_idle_trsh,
2530 			     used_new, used_before_complete)) {
2531 		wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
2532 			     ringid, used_before_complete, used_new);
2533 		txdata->last_idle = get_cycles();
2534 	}
2535 
2536 	/* shall we wake net queues? */
2537 	if (done)
2538 		wil_update_net_queues(wil, vif, vring, false);
2539 
2540 	return done;
2541 }
2542 
2543 static inline int wil_tx_init(struct wil6210_priv *wil)
2544 {
2545 	return 0;
2546 }
2547 
2548 static inline void wil_tx_fini(struct wil6210_priv *wil) {}
2549 
2550 static void wil_get_reorder_params(struct wil6210_priv *wil,
2551 				   struct sk_buff *skb, int *tid, int *cid,
2552 				   int *mid, u16 *seq, int *mcast, int *retry)
2553 {
2554 	struct vring_rx_desc *d = wil_skb_rxdesc(skb);
2555 
2556 	*tid = wil_rxdesc_tid(d);
2557 	*cid = wil_skb_get_cid(skb);
2558 	*mid = wil_rxdesc_mid(d);
2559 	*seq = wil_rxdesc_seq(d);
2560 	*mcast = wil_rxdesc_mcast(d);
2561 	*retry = wil_rxdesc_retry(d);
2562 }
2563 
2564 void wil_init_txrx_ops_legacy_dma(struct wil6210_priv *wil)
2565 {
2566 	wil->txrx_ops.configure_interrupt_moderation =
2567 		wil_configure_interrupt_moderation;
2568 	/* TX ops */
2569 	wil->txrx_ops.tx_desc_map = wil_tx_desc_map;
2570 	wil->txrx_ops.tx_desc_unmap = wil_txdesc_unmap;
2571 	wil->txrx_ops.tx_ring_tso =  __wil_tx_vring_tso;
2572 	wil->txrx_ops.ring_init_tx = wil_vring_init_tx;
2573 	wil->txrx_ops.ring_fini_tx = wil_vring_free;
2574 	wil->txrx_ops.ring_init_bcast = wil_vring_init_bcast;
2575 	wil->txrx_ops.tx_init = wil_tx_init;
2576 	wil->txrx_ops.tx_fini = wil_tx_fini;
2577 	wil->txrx_ops.tx_ring_modify = wil_tx_vring_modify;
2578 	/* RX ops */
2579 	wil->txrx_ops.rx_init = wil_rx_init;
2580 	wil->txrx_ops.wmi_addba_rx_resp = wmi_addba_rx_resp;
2581 	wil->txrx_ops.get_reorder_params = wil_get_reorder_params;
2582 	wil->txrx_ops.get_netif_rx_params =
2583 		wil_get_netif_rx_params;
2584 	wil->txrx_ops.rx_crypto_check = wil_rx_crypto_check;
2585 	wil->txrx_ops.rx_error_check = wil_rx_error_check;
2586 	wil->txrx_ops.is_rx_idle = wil_is_rx_idle;
2587 	wil->txrx_ops.rx_fini = wil_rx_fini;
2588 }
2589