1 /*
2  * Copyright (c) 2012-2019 The Linux Foundation. All rights reserved.
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 <linux/moduleparam.h>
19 #include <linux/prefetch.h>
20 #include <linux/types.h>
21 #include <linux/list.h>
22 #include <linux/ip.h>
23 #include <linux/ipv6.h>
24 #include "wil6210.h"
25 #include "txrx_edma.h"
26 #include "txrx.h"
27 #include "trace.h"
28 
29 /* Max number of entries (packets to complete) to update the hwtail of tx
30  * status ring. Should be power of 2
31  */
32 #define WIL_EDMA_TX_SRING_UPDATE_HW_TAIL 128
33 #define WIL_EDMA_MAX_DATA_OFFSET (2)
34 /* RX buffer size must be aligned to 4 bytes */
35 #define WIL_EDMA_RX_BUF_LEN_DEFAULT (2048)
36 #define MAX_INVALID_BUFF_ID_RETRY (3)
37 
38 static void wil_tx_desc_unmap_edma(struct device *dev,
39 				   union wil_tx_desc *desc,
40 				   struct wil_ctx *ctx)
41 {
42 	struct wil_tx_enhanced_desc *d = (struct wil_tx_enhanced_desc *)desc;
43 	dma_addr_t pa = wil_tx_desc_get_addr_edma(&d->dma);
44 	u16 dmalen = le16_to_cpu(d->dma.length);
45 
46 	switch (ctx->mapped_as) {
47 	case wil_mapped_as_single:
48 		dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
49 		break;
50 	case wil_mapped_as_page:
51 		dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
52 		break;
53 	default:
54 		break;
55 	}
56 }
57 
58 static int wil_find_free_sring(struct wil6210_priv *wil)
59 {
60 	int i;
61 
62 	for (i = 0; i < WIL6210_MAX_STATUS_RINGS; i++) {
63 		if (!wil->srings[i].va)
64 			return i;
65 	}
66 
67 	return -EINVAL;
68 }
69 
70 static void wil_sring_free(struct wil6210_priv *wil,
71 			   struct wil_status_ring *sring)
72 {
73 	struct device *dev = wil_to_dev(wil);
74 	size_t sz;
75 
76 	if (!sring || !sring->va)
77 		return;
78 
79 	sz = sring->elem_size * sring->size;
80 
81 	wil_dbg_misc(wil, "status_ring_free, size(bytes)=%zu, 0x%p:%pad\n",
82 		     sz, sring->va, &sring->pa);
83 
84 	dma_free_coherent(dev, sz, (void *)sring->va, sring->pa);
85 	sring->pa = 0;
86 	sring->va = NULL;
87 }
88 
89 static int wil_sring_alloc(struct wil6210_priv *wil,
90 			   struct wil_status_ring *sring)
91 {
92 	struct device *dev = wil_to_dev(wil);
93 	size_t sz = sring->elem_size * sring->size;
94 
95 	wil_dbg_misc(wil, "status_ring_alloc: size=%zu\n", sz);
96 
97 	if (sz == 0) {
98 		wil_err(wil, "Cannot allocate a zero size status ring\n");
99 		return -EINVAL;
100 	}
101 
102 	sring->swhead = 0;
103 
104 	/* Status messages are allocated and initialized to 0. This is necessary
105 	 * since DR bit should be initialized to 0.
106 	 */
107 	sring->va = dma_alloc_coherent(dev, sz, &sring->pa, GFP_KERNEL);
108 	if (!sring->va)
109 		return -ENOMEM;
110 
111 	wil_dbg_misc(wil, "status_ring[%d] 0x%p:%pad\n", sring->size, sring->va,
112 		     &sring->pa);
113 
114 	return 0;
115 }
116 
117 static int wil_tx_init_edma(struct wil6210_priv *wil)
118 {
119 	int ring_id = wil_find_free_sring(wil);
120 	struct wil_status_ring *sring;
121 	int rc;
122 	u16 status_ring_size;
123 
124 	if (wil->tx_status_ring_order < WIL_SRING_SIZE_ORDER_MIN ||
125 	    wil->tx_status_ring_order > WIL_SRING_SIZE_ORDER_MAX)
126 		wil->tx_status_ring_order = WIL_TX_SRING_SIZE_ORDER_DEFAULT;
127 
128 	status_ring_size = 1 << wil->tx_status_ring_order;
129 
130 	wil_dbg_misc(wil, "init TX sring: size=%u, ring_id=%u\n",
131 		     status_ring_size, ring_id);
132 
133 	if (ring_id < 0)
134 		return ring_id;
135 
136 	/* Allocate Tx status ring. Tx descriptor rings will be
137 	 * allocated on WMI connect event
138 	 */
139 	sring = &wil->srings[ring_id];
140 
141 	sring->is_rx = false;
142 	sring->size = status_ring_size;
143 	sring->elem_size = sizeof(struct wil_ring_tx_status);
144 	rc = wil_sring_alloc(wil, sring);
145 	if (rc)
146 		return rc;
147 
148 	rc = wil_wmi_tx_sring_cfg(wil, ring_id);
149 	if (rc)
150 		goto out_free;
151 
152 	sring->desc_rdy_pol = 1;
153 	wil->tx_sring_idx = ring_id;
154 
155 	return 0;
156 out_free:
157 	wil_sring_free(wil, sring);
158 	return rc;
159 }
160 
161 /**
162  * Allocate one skb for Rx descriptor RING
163  */
164 static int wil_ring_alloc_skb_edma(struct wil6210_priv *wil,
165 				   struct wil_ring *ring, u32 i)
166 {
167 	struct device *dev = wil_to_dev(wil);
168 	unsigned int sz = wil->rx_buf_len;
169 	dma_addr_t pa;
170 	u16 buff_id;
171 	struct list_head *active = &wil->rx_buff_mgmt.active;
172 	struct list_head *free = &wil->rx_buff_mgmt.free;
173 	struct wil_rx_buff *rx_buff;
174 	struct wil_rx_buff *buff_arr = wil->rx_buff_mgmt.buff_arr;
175 	struct sk_buff *skb;
176 	struct wil_rx_enhanced_desc dd, *d = &dd;
177 	struct wil_rx_enhanced_desc *_d = (struct wil_rx_enhanced_desc *)
178 		&ring->va[i].rx.enhanced;
179 
180 	if (unlikely(list_empty(free))) {
181 		wil->rx_buff_mgmt.free_list_empty_cnt++;
182 		return -EAGAIN;
183 	}
184 
185 	skb = dev_alloc_skb(sz);
186 	if (unlikely(!skb))
187 		return -ENOMEM;
188 
189 	skb_put(skb, sz);
190 
191 	/**
192 	 * Make sure that the network stack calculates checksum for packets
193 	 * which failed the HW checksum calculation
194 	 */
195 	skb->ip_summed = CHECKSUM_NONE;
196 
197 	pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
198 	if (unlikely(dma_mapping_error(dev, pa))) {
199 		kfree_skb(skb);
200 		return -ENOMEM;
201 	}
202 
203 	/* Get the buffer ID - the index of the rx buffer in the buff_arr */
204 	rx_buff = list_first_entry(free, struct wil_rx_buff, list);
205 	buff_id = rx_buff->id;
206 
207 	/* Move a buffer from the free list to the active list */
208 	list_move(&rx_buff->list, active);
209 
210 	buff_arr[buff_id].skb = skb;
211 
212 	wil_desc_set_addr_edma(&d->dma.addr, &d->dma.addr_high_high, pa);
213 	d->dma.length = cpu_to_le16(sz);
214 	d->mac.buff_id = cpu_to_le16(buff_id);
215 	*_d = *d;
216 
217 	/* Save the physical address in skb->cb for later use in dma_unmap */
218 	memcpy(skb->cb, &pa, sizeof(pa));
219 
220 	return 0;
221 }
222 
223 static inline
224 void wil_get_next_rx_status_msg(struct wil_status_ring *sring, void *msg)
225 {
226 	memcpy(msg, (void *)(sring->va + (sring->elem_size * sring->swhead)),
227 	       sring->elem_size);
228 }
229 
230 static inline void wil_sring_advance_swhead(struct wil_status_ring *sring)
231 {
232 	sring->swhead = (sring->swhead + 1) % sring->size;
233 	if (sring->swhead == 0)
234 		sring->desc_rdy_pol = 1 - sring->desc_rdy_pol;
235 }
236 
237 static int wil_rx_refill_edma(struct wil6210_priv *wil)
238 {
239 	struct wil_ring *ring = &wil->ring_rx;
240 	u32 next_head;
241 	int rc = 0;
242 	ring->swtail = *ring->edma_rx_swtail.va;
243 
244 	for (; next_head = wil_ring_next_head(ring),
245 	     (next_head != ring->swtail);
246 	     ring->swhead = next_head) {
247 		rc = wil_ring_alloc_skb_edma(wil, ring, ring->swhead);
248 		if (unlikely(rc)) {
249 			if (rc == -EAGAIN)
250 				wil_dbg_txrx(wil, "No free buffer ID found\n");
251 			else
252 				wil_err_ratelimited(wil,
253 						    "Error %d in refill desc[%d]\n",
254 						    rc, ring->swhead);
255 			break;
256 		}
257 	}
258 
259 	/* make sure all writes to descriptors (shared memory) are done before
260 	 * committing them to HW
261 	 */
262 	wmb();
263 
264 	wil_w(wil, ring->hwtail, ring->swhead);
265 
266 	return rc;
267 }
268 
269 static void wil_move_all_rx_buff_to_free_list(struct wil6210_priv *wil,
270 					      struct wil_ring *ring)
271 {
272 	struct device *dev = wil_to_dev(wil);
273 	struct list_head *active = &wil->rx_buff_mgmt.active;
274 	dma_addr_t pa;
275 
276 	if (!wil->rx_buff_mgmt.buff_arr)
277 		return;
278 
279 	while (!list_empty(active)) {
280 		struct wil_rx_buff *rx_buff =
281 			list_first_entry(active, struct wil_rx_buff, list);
282 		struct sk_buff *skb = rx_buff->skb;
283 
284 		if (unlikely(!skb)) {
285 			wil_err(wil, "No Rx skb at buff_id %d\n", rx_buff->id);
286 		} else {
287 			rx_buff->skb = NULL;
288 			memcpy(&pa, skb->cb, sizeof(pa));
289 			dma_unmap_single(dev, pa, wil->rx_buf_len,
290 					 DMA_FROM_DEVICE);
291 			kfree_skb(skb);
292 		}
293 
294 		/* Move the buffer from the active to the free list */
295 		list_move(&rx_buff->list, &wil->rx_buff_mgmt.free);
296 	}
297 }
298 
299 static void wil_free_rx_buff_arr(struct wil6210_priv *wil)
300 {
301 	struct wil_ring *ring = &wil->ring_rx;
302 
303 	if (!wil->rx_buff_mgmt.buff_arr)
304 		return;
305 
306 	/* Move all the buffers to the free list in case active list is
307 	 * not empty in order to release all SKBs before deleting the array
308 	 */
309 	wil_move_all_rx_buff_to_free_list(wil, ring);
310 
311 	kfree(wil->rx_buff_mgmt.buff_arr);
312 	wil->rx_buff_mgmt.buff_arr = NULL;
313 }
314 
315 static int wil_init_rx_buff_arr(struct wil6210_priv *wil,
316 				size_t size)
317 {
318 	struct wil_rx_buff *buff_arr;
319 	struct list_head *active = &wil->rx_buff_mgmt.active;
320 	struct list_head *free = &wil->rx_buff_mgmt.free;
321 	int i;
322 
323 	wil->rx_buff_mgmt.buff_arr = kcalloc(size + 1,
324 					     sizeof(struct wil_rx_buff),
325 					     GFP_KERNEL);
326 	if (!wil->rx_buff_mgmt.buff_arr)
327 		return -ENOMEM;
328 
329 	/* Set list heads */
330 	INIT_LIST_HEAD(active);
331 	INIT_LIST_HEAD(free);
332 
333 	/* Linkify the list.
334 	 * buffer id 0 should not be used (marks invalid id).
335 	 */
336 	buff_arr = wil->rx_buff_mgmt.buff_arr;
337 	for (i = 1; i <= size; i++) {
338 		list_add(&buff_arr[i].list, free);
339 		buff_arr[i].id = i;
340 	}
341 
342 	wil->rx_buff_mgmt.size = size + 1;
343 
344 	return 0;
345 }
346 
347 static int wil_init_rx_sring(struct wil6210_priv *wil,
348 			     u16 status_ring_size,
349 			     size_t elem_size,
350 			     u16 ring_id)
351 {
352 	struct wil_status_ring *sring = &wil->srings[ring_id];
353 	int rc;
354 
355 	wil_dbg_misc(wil, "init RX sring: size=%u, ring_id=%u\n",
356 		     status_ring_size, ring_id);
357 
358 	memset(&sring->rx_data, 0, sizeof(sring->rx_data));
359 
360 	sring->is_rx = true;
361 	sring->size = status_ring_size;
362 	sring->elem_size = elem_size;
363 	rc = wil_sring_alloc(wil, sring);
364 	if (rc)
365 		return rc;
366 
367 	rc = wil_wmi_rx_sring_add(wil, ring_id);
368 	if (rc)
369 		goto out_free;
370 
371 	sring->desc_rdy_pol = 1;
372 
373 	return 0;
374 out_free:
375 	wil_sring_free(wil, sring);
376 	return rc;
377 }
378 
379 static int wil_ring_alloc_desc_ring(struct wil6210_priv *wil,
380 				    struct wil_ring *ring)
381 {
382 	struct device *dev = wil_to_dev(wil);
383 	size_t sz = ring->size * sizeof(ring->va[0]);
384 
385 	wil_dbg_misc(wil, "alloc_desc_ring:\n");
386 
387 	BUILD_BUG_ON(sizeof(ring->va[0]) != 32);
388 
389 	ring->swhead = 0;
390 	ring->swtail = 0;
391 	ring->ctx = kcalloc(ring->size, sizeof(ring->ctx[0]), GFP_KERNEL);
392 	if (!ring->ctx)
393 		goto err;
394 
395 	ring->va = dma_alloc_coherent(dev, sz, &ring->pa, GFP_KERNEL);
396 	if (!ring->va)
397 		goto err_free_ctx;
398 
399 	if (ring->is_rx) {
400 		sz = sizeof(*ring->edma_rx_swtail.va);
401 		ring->edma_rx_swtail.va =
402 			dma_alloc_coherent(dev, sz, &ring->edma_rx_swtail.pa,
403 					   GFP_KERNEL);
404 		if (!ring->edma_rx_swtail.va)
405 			goto err_free_va;
406 	}
407 
408 	wil_dbg_misc(wil, "%s ring[%d] 0x%p:%pad 0x%p\n",
409 		     ring->is_rx ? "RX" : "TX",
410 		     ring->size, ring->va, &ring->pa, ring->ctx);
411 
412 	return 0;
413 err_free_va:
414 	dma_free_coherent(dev, ring->size * sizeof(ring->va[0]),
415 			  (void *)ring->va, ring->pa);
416 	ring->va = NULL;
417 err_free_ctx:
418 	kfree(ring->ctx);
419 	ring->ctx = NULL;
420 err:
421 	return -ENOMEM;
422 }
423 
424 static void wil_ring_free_edma(struct wil6210_priv *wil, struct wil_ring *ring)
425 {
426 	struct device *dev = wil_to_dev(wil);
427 	size_t sz;
428 	int ring_index = 0;
429 
430 	if (!ring->va)
431 		return;
432 
433 	sz = ring->size * sizeof(ring->va[0]);
434 
435 	lockdep_assert_held(&wil->mutex);
436 	if (ring->is_rx) {
437 		wil_dbg_misc(wil, "free Rx ring [%d] 0x%p:%pad 0x%p\n",
438 			     ring->size, ring->va,
439 			     &ring->pa, ring->ctx);
440 
441 		wil_move_all_rx_buff_to_free_list(wil, ring);
442 		dma_free_coherent(dev, sizeof(*ring->edma_rx_swtail.va),
443 				  ring->edma_rx_swtail.va,
444 				  ring->edma_rx_swtail.pa);
445 		goto out;
446 	}
447 
448 	/* TX ring */
449 	ring_index = ring - wil->ring_tx;
450 
451 	wil_dbg_misc(wil, "free Tx ring %d [%d] 0x%p:%pad 0x%p\n",
452 		     ring_index, ring->size, ring->va,
453 		     &ring->pa, ring->ctx);
454 
455 	while (!wil_ring_is_empty(ring)) {
456 		struct wil_ctx *ctx;
457 
458 		struct wil_tx_enhanced_desc dd, *d = &dd;
459 		struct wil_tx_enhanced_desc *_d =
460 			(struct wil_tx_enhanced_desc *)
461 			&ring->va[ring->swtail].tx.enhanced;
462 
463 		ctx = &ring->ctx[ring->swtail];
464 		if (!ctx) {
465 			wil_dbg_txrx(wil,
466 				     "ctx(%d) was already completed\n",
467 				     ring->swtail);
468 			ring->swtail = wil_ring_next_tail(ring);
469 			continue;
470 		}
471 		*d = *_d;
472 		wil_tx_desc_unmap_edma(dev, (union wil_tx_desc *)d, ctx);
473 		if (ctx->skb)
474 			dev_kfree_skb_any(ctx->skb);
475 		ring->swtail = wil_ring_next_tail(ring);
476 	}
477 
478 out:
479 	dma_free_coherent(dev, sz, (void *)ring->va, ring->pa);
480 	kfree(ring->ctx);
481 	ring->pa = 0;
482 	ring->va = NULL;
483 	ring->ctx = NULL;
484 }
485 
486 static int wil_init_rx_desc_ring(struct wil6210_priv *wil, u16 desc_ring_size,
487 				 int status_ring_id)
488 {
489 	struct wil_ring *ring = &wil->ring_rx;
490 	int rc;
491 
492 	wil_dbg_misc(wil, "init RX desc ring\n");
493 
494 	ring->size = desc_ring_size;
495 	ring->is_rx = true;
496 	rc = wil_ring_alloc_desc_ring(wil, ring);
497 	if (rc)
498 		return rc;
499 
500 	rc = wil_wmi_rx_desc_ring_add(wil, status_ring_id);
501 	if (rc)
502 		goto out_free;
503 
504 	return 0;
505 out_free:
506 	wil_ring_free_edma(wil, ring);
507 	return rc;
508 }
509 
510 static void wil_get_reorder_params_edma(struct wil6210_priv *wil,
511 					struct sk_buff *skb, int *tid,
512 					int *cid, int *mid, u16 *seq,
513 					int *mcast, int *retry)
514 {
515 	struct wil_rx_status_extended *s = wil_skb_rxstatus(skb);
516 
517 	*tid = wil_rx_status_get_tid(s);
518 	*cid = wil_rx_status_get_cid(s);
519 	*mid = wil_rx_status_get_mid(s);
520 	*seq = le16_to_cpu(wil_rx_status_get_seq(wil, s));
521 	*mcast = wil_rx_status_get_mcast(s);
522 	*retry = wil_rx_status_get_retry(s);
523 }
524 
525 static void wil_get_netif_rx_params_edma(struct sk_buff *skb, int *cid,
526 					 int *security)
527 {
528 	struct wil_rx_status_extended *s = wil_skb_rxstatus(skb);
529 
530 	*cid = wil_rx_status_get_cid(s);
531 	*security = wil_rx_status_get_security(s);
532 }
533 
534 static int wil_rx_crypto_check_edma(struct wil6210_priv *wil,
535 				    struct sk_buff *skb)
536 {
537 	struct wil_rx_status_extended *st;
538 	int cid, tid, key_id, mc;
539 	struct wil_sta_info *s;
540 	struct wil_tid_crypto_rx *c;
541 	struct wil_tid_crypto_rx_single *cc;
542 	const u8 *pn;
543 
544 	/* In HW reorder, HW is responsible for crypto check */
545 	if (wil->use_rx_hw_reordering)
546 		return 0;
547 
548 	st = wil_skb_rxstatus(skb);
549 
550 	cid = wil_rx_status_get_cid(st);
551 	tid = wil_rx_status_get_tid(st);
552 	key_id = wil_rx_status_get_key_id(st);
553 	mc = wil_rx_status_get_mcast(st);
554 	s = &wil->sta[cid];
555 	c = mc ? &s->group_crypto_rx : &s->tid_crypto_rx[tid];
556 	cc = &c->key_id[key_id];
557 	pn = (u8 *)&st->ext.pn_15_0;
558 
559 	if (!cc->key_set) {
560 		wil_err_ratelimited(wil,
561 				    "Key missing. CID %d TID %d MCast %d KEY_ID %d\n",
562 				    cid, tid, mc, key_id);
563 		return -EINVAL;
564 	}
565 
566 	if (reverse_memcmp(pn, cc->pn, IEEE80211_GCMP_PN_LEN) <= 0) {
567 		wil_err_ratelimited(wil,
568 				    "Replay attack. CID %d TID %d MCast %d KEY_ID %d PN %6phN last %6phN\n",
569 				    cid, tid, mc, key_id, pn, cc->pn);
570 		return -EINVAL;
571 	}
572 	memcpy(cc->pn, pn, IEEE80211_GCMP_PN_LEN);
573 
574 	return 0;
575 }
576 
577 static bool wil_is_rx_idle_edma(struct wil6210_priv *wil)
578 {
579 	struct wil_status_ring *sring;
580 	struct wil_rx_status_extended msg1;
581 	void *msg = &msg1;
582 	u8 dr_bit;
583 	int i;
584 
585 	for (i = 0; i < wil->num_rx_status_rings; i++) {
586 		sring = &wil->srings[i];
587 		if (!sring->va)
588 			continue;
589 
590 		wil_get_next_rx_status_msg(sring, msg);
591 		dr_bit = wil_rx_status_get_desc_rdy_bit(msg);
592 
593 		/* Check if there are unhandled RX status messages */
594 		if (dr_bit == sring->desc_rdy_pol)
595 			return false;
596 	}
597 
598 	return true;
599 }
600 
601 static void wil_rx_buf_len_init_edma(struct wil6210_priv *wil)
602 {
603 	/* RX buffer size must be aligned to 4 bytes */
604 	wil->rx_buf_len = rx_large_buf ?
605 		WIL_MAX_ETH_MTU : WIL_EDMA_RX_BUF_LEN_DEFAULT;
606 }
607 
608 static int wil_rx_init_edma(struct wil6210_priv *wil, uint desc_ring_order)
609 {
610 	u16 status_ring_size, desc_ring_size = 1 << desc_ring_order;
611 	struct wil_ring *ring = &wil->ring_rx;
612 	int rc;
613 	size_t elem_size = wil->use_compressed_rx_status ?
614 		sizeof(struct wil_rx_status_compressed) :
615 		sizeof(struct wil_rx_status_extended);
616 	int i;
617 
618 	/* In SW reorder one must use extended status messages */
619 	if (wil->use_compressed_rx_status && !wil->use_rx_hw_reordering) {
620 		wil_err(wil,
621 			"compressed RX status cannot be used with SW reorder\n");
622 		return -EINVAL;
623 	}
624 	if (wil->rx_status_ring_order <= desc_ring_order)
625 		/* make sure sring is larger than desc ring */
626 		wil->rx_status_ring_order = desc_ring_order + 1;
627 	if (wil->rx_buff_id_count <= desc_ring_size)
628 		/* make sure we will not run out of buff_ids */
629 		wil->rx_buff_id_count = desc_ring_size + 512;
630 	if (wil->rx_status_ring_order < WIL_SRING_SIZE_ORDER_MIN ||
631 	    wil->rx_status_ring_order > WIL_SRING_SIZE_ORDER_MAX)
632 		wil->rx_status_ring_order = WIL_RX_SRING_SIZE_ORDER_DEFAULT;
633 
634 	status_ring_size = 1 << wil->rx_status_ring_order;
635 
636 	wil_dbg_misc(wil,
637 		     "rx_init, desc_ring_size=%u, status_ring_size=%u, elem_size=%zu\n",
638 		     desc_ring_size, status_ring_size, elem_size);
639 
640 	wil_rx_buf_len_init_edma(wil);
641 
642 	/* Use debugfs dbg_num_rx_srings if set, reserve one sring for TX */
643 	if (wil->num_rx_status_rings > WIL6210_MAX_STATUS_RINGS - 1)
644 		wil->num_rx_status_rings = WIL6210_MAX_STATUS_RINGS - 1;
645 
646 	wil_dbg_misc(wil, "rx_init: allocate %d status rings\n",
647 		     wil->num_rx_status_rings);
648 
649 	rc = wil_wmi_cfg_def_rx_offload(wil, wil->rx_buf_len);
650 	if (rc)
651 		return rc;
652 
653 	/* Allocate status ring */
654 	for (i = 0; i < wil->num_rx_status_rings; i++) {
655 		int sring_id = wil_find_free_sring(wil);
656 
657 		if (sring_id < 0) {
658 			rc = -EFAULT;
659 			goto err_free_status;
660 		}
661 		rc = wil_init_rx_sring(wil, status_ring_size, elem_size,
662 				       sring_id);
663 		if (rc)
664 			goto err_free_status;
665 	}
666 
667 	/* Allocate descriptor ring */
668 	rc = wil_init_rx_desc_ring(wil, desc_ring_size,
669 				   WIL_DEFAULT_RX_STATUS_RING_ID);
670 	if (rc)
671 		goto err_free_status;
672 
673 	if (wil->rx_buff_id_count >= status_ring_size) {
674 		wil_info(wil,
675 			 "rx_buff_id_count %d exceeds sring_size %d. set it to %d\n",
676 			 wil->rx_buff_id_count, status_ring_size,
677 			 status_ring_size - 1);
678 		wil->rx_buff_id_count = status_ring_size - 1;
679 	}
680 
681 	/* Allocate Rx buffer array */
682 	rc = wil_init_rx_buff_arr(wil, wil->rx_buff_id_count);
683 	if (rc)
684 		goto err_free_desc;
685 
686 	/* Fill descriptor ring with credits */
687 	rc = wil_rx_refill_edma(wil);
688 	if (rc)
689 		goto err_free_rx_buff_arr;
690 
691 	return 0;
692 err_free_rx_buff_arr:
693 	wil_free_rx_buff_arr(wil);
694 err_free_desc:
695 	wil_ring_free_edma(wil, ring);
696 err_free_status:
697 	for (i = 0; i < wil->num_rx_status_rings; i++)
698 		wil_sring_free(wil, &wil->srings[i]);
699 
700 	return rc;
701 }
702 
703 static int wil_ring_init_tx_edma(struct wil6210_vif *vif, int ring_id,
704 				 int size, int cid, int tid)
705 {
706 	struct wil6210_priv *wil = vif_to_wil(vif);
707 	int rc;
708 	struct wil_ring *ring = &wil->ring_tx[ring_id];
709 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ring_id];
710 
711 	lockdep_assert_held(&wil->mutex);
712 
713 	wil_dbg_misc(wil,
714 		     "init TX ring: ring_id=%u, cid=%u, tid=%u, sring_id=%u\n",
715 		     ring_id, cid, tid, wil->tx_sring_idx);
716 
717 	wil_tx_data_init(txdata);
718 	ring->size = size;
719 	rc = wil_ring_alloc_desc_ring(wil, ring);
720 	if (rc)
721 		goto out;
722 
723 	wil->ring2cid_tid[ring_id][0] = cid;
724 	wil->ring2cid_tid[ring_id][1] = tid;
725 	if (!vif->privacy)
726 		txdata->dot1x_open = true;
727 
728 	rc = wil_wmi_tx_desc_ring_add(vif, ring_id, cid, tid);
729 	if (rc) {
730 		wil_err(wil, "WMI_TX_DESC_RING_ADD_CMD failed\n");
731 		goto out_free;
732 	}
733 
734 	if (txdata->dot1x_open && agg_wsize >= 0)
735 		wil_addba_tx_request(wil, ring_id, agg_wsize);
736 
737 	return 0;
738  out_free:
739 	spin_lock_bh(&txdata->lock);
740 	txdata->dot1x_open = false;
741 	txdata->enabled = 0;
742 	spin_unlock_bh(&txdata->lock);
743 	wil_ring_free_edma(wil, ring);
744 	wil->ring2cid_tid[ring_id][0] = wil->max_assoc_sta;
745 	wil->ring2cid_tid[ring_id][1] = 0;
746 
747  out:
748 	return rc;
749 }
750 
751 static int wil_tx_ring_modify_edma(struct wil6210_vif *vif, int ring_id,
752 				   int cid, int tid)
753 {
754 	struct wil6210_priv *wil = vif_to_wil(vif);
755 
756 	wil_err(wil, "ring modify is not supported for EDMA\n");
757 
758 	return -EOPNOTSUPP;
759 }
760 
761 /* This function is used only for RX SW reorder */
762 static int wil_check_bar(struct wil6210_priv *wil, void *msg, int cid,
763 			 struct sk_buff *skb, struct wil_net_stats *stats)
764 {
765 	u8 ftype;
766 	u8 fc1;
767 	int mid;
768 	int tid;
769 	u16 seq;
770 	struct wil6210_vif *vif;
771 
772 	ftype = wil_rx_status_get_frame_type(wil, msg);
773 	if (ftype == IEEE80211_FTYPE_DATA)
774 		return 0;
775 
776 	fc1 = wil_rx_status_get_fc1(wil, msg);
777 	mid = wil_rx_status_get_mid(msg);
778 	tid = wil_rx_status_get_tid(msg);
779 	seq = le16_to_cpu(wil_rx_status_get_seq(wil, msg));
780 	vif = wil->vifs[mid];
781 
782 	if (unlikely(!vif)) {
783 		wil_dbg_txrx(wil, "RX descriptor with invalid mid %d", mid);
784 		return -EAGAIN;
785 	}
786 
787 	wil_dbg_txrx(wil,
788 		     "Non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
789 		     fc1, mid, cid, tid, seq);
790 	if (stats)
791 		stats->rx_non_data_frame++;
792 	if (wil_is_back_req(fc1)) {
793 		wil_dbg_txrx(wil,
794 			     "BAR: MID %d CID %d TID %d Seq 0x%03x\n",
795 			     mid, cid, tid, seq);
796 		wil_rx_bar(wil, vif, cid, tid, seq);
797 	} else {
798 		u32 sz = wil->use_compressed_rx_status ?
799 			sizeof(struct wil_rx_status_compressed) :
800 			sizeof(struct wil_rx_status_extended);
801 
802 		/* print again all info. One can enable only this
803 		 * without overhead for printing every Rx frame
804 		 */
805 		wil_dbg_txrx(wil,
806 			     "Unhandled non-data frame FC[7:0] 0x%02x MID %d CID %d TID %d Seq 0x%03x\n",
807 			     fc1, mid, cid, tid, seq);
808 		wil_hex_dump_txrx("RxS ", DUMP_PREFIX_NONE, 32, 4,
809 				  (const void *)msg, sz, false);
810 		wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
811 				  skb->data, skb_headlen(skb), false);
812 	}
813 
814 	return -EAGAIN;
815 }
816 
817 static int wil_rx_error_check_edma(struct wil6210_priv *wil,
818 				   struct sk_buff *skb,
819 				   struct wil_net_stats *stats)
820 {
821 	int l2_rx_status;
822 	void *msg = wil_skb_rxstatus(skb);
823 
824 	l2_rx_status = wil_rx_status_get_l2_rx_status(msg);
825 	if (l2_rx_status != 0) {
826 		wil_dbg_txrx(wil, "L2 RX error, l2_rx_status=0x%x\n",
827 			     l2_rx_status);
828 		/* Due to HW issue, KEY error will trigger a MIC error */
829 		if (l2_rx_status == WIL_RX_EDMA_ERROR_MIC) {
830 			wil_err_ratelimited(wil,
831 					    "L2 MIC/KEY error, dropping packet\n");
832 			stats->rx_mic_error++;
833 		}
834 		if (l2_rx_status == WIL_RX_EDMA_ERROR_KEY) {
835 			wil_err_ratelimited(wil,
836 					    "L2 KEY error, dropping packet\n");
837 			stats->rx_key_error++;
838 		}
839 		if (l2_rx_status == WIL_RX_EDMA_ERROR_REPLAY) {
840 			wil_err_ratelimited(wil,
841 					    "L2 REPLAY error, dropping packet\n");
842 			stats->rx_replay++;
843 		}
844 		if (l2_rx_status == WIL_RX_EDMA_ERROR_AMSDU) {
845 			wil_err_ratelimited(wil,
846 					    "L2 AMSDU error, dropping packet\n");
847 			stats->rx_amsdu_error++;
848 		}
849 		return -EFAULT;
850 	}
851 
852 	skb->ip_summed = wil_rx_status_get_checksum(msg, stats);
853 
854 	return 0;
855 }
856 
857 static struct sk_buff *wil_sring_reap_rx_edma(struct wil6210_priv *wil,
858 					      struct wil_status_ring *sring)
859 {
860 	struct device *dev = wil_to_dev(wil);
861 	struct wil_rx_status_extended msg1;
862 	void *msg = &msg1;
863 	u16 buff_id;
864 	struct sk_buff *skb;
865 	dma_addr_t pa;
866 	struct wil_ring_rx_data *rxdata = &sring->rx_data;
867 	unsigned int sz = wil->rx_buf_len;
868 	struct wil_net_stats *stats = NULL;
869 	u16 dmalen;
870 	int cid;
871 	bool eop, headstolen;
872 	int delta;
873 	u8 dr_bit;
874 	u8 data_offset;
875 	struct wil_rx_status_extended *s;
876 	u16 sring_idx = sring - wil->srings;
877 
878 	BUILD_BUG_ON(sizeof(struct wil_rx_status_extended) > sizeof(skb->cb));
879 
880 again:
881 	wil_get_next_rx_status_msg(sring, msg);
882 	dr_bit = wil_rx_status_get_desc_rdy_bit(msg);
883 
884 	/* Completed handling all the ready status messages */
885 	if (dr_bit != sring->desc_rdy_pol)
886 		return NULL;
887 
888 	/* Extract the buffer ID from the status message */
889 	buff_id = le16_to_cpu(wil_rx_status_get_buff_id(msg));
890 
891 	while (!buff_id) {
892 		struct wil_rx_status_extended *s;
893 		int invalid_buff_id_retry = 0;
894 
895 		wil_dbg_txrx(wil,
896 			     "buff_id is not updated yet by HW, (swhead 0x%x)\n",
897 			     sring->swhead);
898 		if (++invalid_buff_id_retry > MAX_INVALID_BUFF_ID_RETRY)
899 			break;
900 
901 		/* Read the status message again */
902 		s = (struct wil_rx_status_extended *)
903 			(sring->va + (sring->elem_size * sring->swhead));
904 		*(struct wil_rx_status_extended *)msg = *s;
905 		buff_id = le16_to_cpu(wil_rx_status_get_buff_id(msg));
906 	}
907 
908 	if (unlikely(!wil_val_in_range(buff_id, 1, wil->rx_buff_mgmt.size))) {
909 		wil_err(wil, "Corrupt buff_id=%d, sring->swhead=%d\n",
910 			buff_id, sring->swhead);
911 		wil_rx_status_reset_buff_id(sring);
912 		wil_sring_advance_swhead(sring);
913 		sring->invalid_buff_id_cnt++;
914 		goto again;
915 	}
916 
917 	/* Extract the SKB from the rx_buff management array */
918 	skb = wil->rx_buff_mgmt.buff_arr[buff_id].skb;
919 	wil->rx_buff_mgmt.buff_arr[buff_id].skb = NULL;
920 	if (!skb) {
921 		wil_err(wil, "No Rx skb at buff_id %d\n", buff_id);
922 		wil_rx_status_reset_buff_id(sring);
923 		/* Move the buffer from the active list to the free list */
924 		list_move_tail(&wil->rx_buff_mgmt.buff_arr[buff_id].list,
925 			       &wil->rx_buff_mgmt.free);
926 		wil_sring_advance_swhead(sring);
927 		sring->invalid_buff_id_cnt++;
928 		goto again;
929 	}
930 
931 	wil_rx_status_reset_buff_id(sring);
932 	wil_sring_advance_swhead(sring);
933 
934 	memcpy(&pa, skb->cb, sizeof(pa));
935 	dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
936 	dmalen = le16_to_cpu(wil_rx_status_get_length(msg));
937 
938 	trace_wil6210_rx_status(wil, wil->use_compressed_rx_status, buff_id,
939 				msg);
940 	wil_dbg_txrx(wil, "Rx, buff_id=%u, sring_idx=%u, dmalen=%u bytes\n",
941 		     buff_id, sring_idx, dmalen);
942 	wil_hex_dump_txrx("RxS ", DUMP_PREFIX_NONE, 32, 4,
943 			  (const void *)msg, wil->use_compressed_rx_status ?
944 			  sizeof(struct wil_rx_status_compressed) :
945 			  sizeof(struct wil_rx_status_extended), false);
946 
947 	/* Move the buffer from the active list to the free list */
948 	list_move_tail(&wil->rx_buff_mgmt.buff_arr[buff_id].list,
949 		       &wil->rx_buff_mgmt.free);
950 
951 	eop = wil_rx_status_get_eop(msg);
952 
953 	cid = wil_rx_status_get_cid(msg);
954 	if (unlikely(!wil_val_in_range(cid, 0, wil->max_assoc_sta))) {
955 		wil_err(wil, "Corrupt cid=%d, sring->swhead=%d\n",
956 			cid, sring->swhead);
957 		rxdata->skipping = true;
958 		goto skipping;
959 	}
960 	stats = &wil->sta[cid].stats;
961 
962 	if (unlikely(skb->len < ETH_HLEN)) {
963 		wil_dbg_txrx(wil, "Short frame, len = %d\n", skb->len);
964 		stats->rx_short_frame++;
965 		rxdata->skipping = true;
966 		goto skipping;
967 	}
968 
969 	if (unlikely(dmalen > sz)) {
970 		wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
971 		stats->rx_large_frame++;
972 		rxdata->skipping = true;
973 	}
974 
975 skipping:
976 	/* skipping indicates if a certain SKB should be dropped.
977 	 * It is set in case there is an error on the current SKB or in case
978 	 * of RX chaining: as long as we manage to merge the SKBs it will
979 	 * be false. once we have a bad SKB or we don't manage to merge SKBs
980 	 * it will be set to the !EOP value of the current SKB.
981 	 * This guarantees that all the following SKBs until EOP will also
982 	 * get dropped.
983 	 */
984 	if (unlikely(rxdata->skipping)) {
985 		kfree_skb(skb);
986 		if (rxdata->skb) {
987 			kfree_skb(rxdata->skb);
988 			rxdata->skb = NULL;
989 		}
990 		rxdata->skipping = !eop;
991 		goto again;
992 	}
993 
994 	skb_trim(skb, dmalen);
995 
996 	prefetch(skb->data);
997 
998 	if (!rxdata->skb) {
999 		rxdata->skb = skb;
1000 	} else {
1001 		if (likely(skb_try_coalesce(rxdata->skb, skb, &headstolen,
1002 					    &delta))) {
1003 			kfree_skb_partial(skb, headstolen);
1004 		} else {
1005 			wil_err(wil, "failed to merge skbs!\n");
1006 			kfree_skb(skb);
1007 			kfree_skb(rxdata->skb);
1008 			rxdata->skb = NULL;
1009 			rxdata->skipping = !eop;
1010 			goto again;
1011 		}
1012 	}
1013 
1014 	if (!eop)
1015 		goto again;
1016 
1017 	/* reaching here rxdata->skb always contains a full packet */
1018 	skb = rxdata->skb;
1019 	rxdata->skb = NULL;
1020 	rxdata->skipping = false;
1021 
1022 	if (stats) {
1023 		stats->last_mcs_rx = wil_rx_status_get_mcs(msg);
1024 		if (stats->last_mcs_rx < ARRAY_SIZE(stats->rx_per_mcs))
1025 			stats->rx_per_mcs[stats->last_mcs_rx]++;
1026 
1027 		stats->last_cb_mode_rx  = wil_rx_status_get_cb_mode(msg);
1028 	}
1029 
1030 	if (!wil->use_rx_hw_reordering && !wil->use_compressed_rx_status &&
1031 	    wil_check_bar(wil, msg, cid, skb, stats) == -EAGAIN) {
1032 		kfree_skb(skb);
1033 		goto again;
1034 	}
1035 
1036 	/* Compensate for the HW data alignment according to the status
1037 	 * message
1038 	 */
1039 	data_offset = wil_rx_status_get_data_offset(msg);
1040 	if (data_offset == 0xFF ||
1041 	    data_offset > WIL_EDMA_MAX_DATA_OFFSET) {
1042 		wil_err(wil, "Unexpected data offset %d\n", data_offset);
1043 		kfree_skb(skb);
1044 		goto again;
1045 	}
1046 
1047 	skb_pull(skb, data_offset);
1048 
1049 	wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
1050 			  skb->data, skb_headlen(skb), false);
1051 
1052 	/* Has to be done after dma_unmap_single as skb->cb is also
1053 	 * used for holding the pa
1054 	 */
1055 	s = wil_skb_rxstatus(skb);
1056 	memcpy(s, msg, sring->elem_size);
1057 
1058 	return skb;
1059 }
1060 
1061 void wil_rx_handle_edma(struct wil6210_priv *wil, int *quota)
1062 {
1063 	struct net_device *ndev;
1064 	struct wil_ring *ring = &wil->ring_rx;
1065 	struct wil_status_ring *sring;
1066 	struct sk_buff *skb;
1067 	int i;
1068 
1069 	if (unlikely(!ring->va)) {
1070 		wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
1071 		return;
1072 	}
1073 	wil_dbg_txrx(wil, "rx_handle\n");
1074 
1075 	for (i = 0; i < wil->num_rx_status_rings; i++) {
1076 		sring = &wil->srings[i];
1077 		if (unlikely(!sring->va)) {
1078 			wil_err(wil,
1079 				"Rx IRQ while Rx status ring %d not yet initialized\n",
1080 				i);
1081 			continue;
1082 		}
1083 
1084 		while ((*quota > 0) &&
1085 		       (NULL != (skb =
1086 			wil_sring_reap_rx_edma(wil, sring)))) {
1087 			(*quota)--;
1088 			if (wil->use_rx_hw_reordering) {
1089 				void *msg = wil_skb_rxstatus(skb);
1090 				int mid = wil_rx_status_get_mid(msg);
1091 				struct wil6210_vif *vif = wil->vifs[mid];
1092 
1093 				if (unlikely(!vif)) {
1094 					wil_dbg_txrx(wil,
1095 						     "RX desc invalid mid %d",
1096 						     mid);
1097 					kfree_skb(skb);
1098 					continue;
1099 				}
1100 				ndev = vif_to_ndev(vif);
1101 				wil_netif_rx_any(skb, ndev);
1102 			} else {
1103 				wil_rx_reorder(wil, skb);
1104 			}
1105 		}
1106 
1107 		wil_w(wil, sring->hwtail, (sring->swhead - 1) % sring->size);
1108 	}
1109 
1110 	wil_rx_refill_edma(wil);
1111 }
1112 
1113 static int wil_tx_desc_map_edma(union wil_tx_desc *desc,
1114 				dma_addr_t pa,
1115 				u32 len,
1116 				int ring_index)
1117 {
1118 	struct wil_tx_enhanced_desc *d =
1119 		(struct wil_tx_enhanced_desc *)&desc->enhanced;
1120 
1121 	memset(d, 0, sizeof(struct wil_tx_enhanced_desc));
1122 
1123 	wil_desc_set_addr_edma(&d->dma.addr, &d->dma.addr_high_high, pa);
1124 
1125 	/* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
1126 	d->dma.length = cpu_to_le16((u16)len);
1127 	d->mac.d[0] = (ring_index << WIL_EDMA_DESC_TX_MAC_CFG_0_QID_POS);
1128 	/* translation type:  0 - bypass; 1 - 802.3; 2 - native wifi;
1129 	 * 3 - eth mode
1130 	 */
1131 	d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
1132 		      (0x3 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
1133 
1134 	return 0;
1135 }
1136 
1137 static inline void
1138 wil_get_next_tx_status_msg(struct wil_status_ring *sring,
1139 			   struct wil_ring_tx_status *msg)
1140 {
1141 	struct wil_ring_tx_status *_msg = (struct wil_ring_tx_status *)
1142 		(sring->va + (sring->elem_size * sring->swhead));
1143 
1144 	*msg = *_msg;
1145 }
1146 
1147 /**
1148  * Clean up transmitted skb's from the Tx descriptor RING.
1149  * Return number of descriptors cleared.
1150  */
1151 int wil_tx_sring_handler(struct wil6210_priv *wil,
1152 			 struct wil_status_ring *sring)
1153 {
1154 	struct net_device *ndev;
1155 	struct device *dev = wil_to_dev(wil);
1156 	struct wil_ring *ring = NULL;
1157 	struct wil_ring_tx_data *txdata;
1158 	/* Total number of completed descriptors in all descriptor rings */
1159 	int desc_cnt = 0;
1160 	int cid;
1161 	struct wil_net_stats *stats;
1162 	struct wil_tx_enhanced_desc *_d;
1163 	unsigned int ring_id;
1164 	unsigned int num_descs, num_statuses = 0;
1165 	int i;
1166 	u8 dr_bit; /* Descriptor Ready bit */
1167 	struct wil_ring_tx_status msg;
1168 	struct wil6210_vif *vif;
1169 	int used_before_complete;
1170 	int used_new;
1171 
1172 	wil_get_next_tx_status_msg(sring, &msg);
1173 	dr_bit = msg.desc_ready >> TX_STATUS_DESC_READY_POS;
1174 
1175 	/* Process completion messages while DR bit has the expected polarity */
1176 	while (dr_bit == sring->desc_rdy_pol) {
1177 		num_descs = msg.num_descriptors;
1178 		if (!num_descs) {
1179 			wil_err(wil, "invalid num_descs 0\n");
1180 			goto again;
1181 		}
1182 
1183 		/* Find the corresponding descriptor ring */
1184 		ring_id = msg.ring_id;
1185 
1186 		if (unlikely(ring_id >= WIL6210_MAX_TX_RINGS)) {
1187 			wil_err(wil, "invalid ring id %d\n", ring_id);
1188 			goto again;
1189 		}
1190 		ring = &wil->ring_tx[ring_id];
1191 		if (unlikely(!ring->va)) {
1192 			wil_err(wil, "Tx irq[%d]: ring not initialized\n",
1193 				ring_id);
1194 			goto again;
1195 		}
1196 		txdata = &wil->ring_tx_data[ring_id];
1197 		if (unlikely(!txdata->enabled)) {
1198 			wil_info(wil, "Tx irq[%d]: ring disabled\n", ring_id);
1199 			goto again;
1200 		}
1201 		vif = wil->vifs[txdata->mid];
1202 		if (unlikely(!vif)) {
1203 			wil_dbg_txrx(wil, "invalid MID %d for ring %d\n",
1204 				     txdata->mid, ring_id);
1205 			goto again;
1206 		}
1207 
1208 		ndev = vif_to_ndev(vif);
1209 
1210 		cid = wil->ring2cid_tid[ring_id][0];
1211 		stats = (cid < wil->max_assoc_sta) ? &wil->sta[cid].stats :
1212 						     NULL;
1213 
1214 		wil_dbg_txrx(wil,
1215 			     "tx_status: completed desc_ring (%d), num_descs (%d)\n",
1216 			     ring_id, num_descs);
1217 
1218 		used_before_complete = wil_ring_used_tx(ring);
1219 
1220 		for (i = 0 ; i < num_descs; ++i) {
1221 			struct wil_ctx *ctx = &ring->ctx[ring->swtail];
1222 			struct wil_tx_enhanced_desc dd, *d = &dd;
1223 			u16 dmalen;
1224 			struct sk_buff *skb = ctx->skb;
1225 
1226 			_d = (struct wil_tx_enhanced_desc *)
1227 				&ring->va[ring->swtail].tx.enhanced;
1228 			*d = *_d;
1229 
1230 			dmalen = le16_to_cpu(d->dma.length);
1231 			trace_wil6210_tx_status(&msg, ring->swtail, dmalen);
1232 			wil_dbg_txrx(wil,
1233 				     "TxC[%2d][%3d] : %d bytes, status 0x%02x\n",
1234 				     ring_id, ring->swtail, dmalen,
1235 				     msg.status);
1236 			wil_hex_dump_txrx("TxS ", DUMP_PREFIX_NONE, 32, 4,
1237 					  (const void *)&msg, sizeof(msg),
1238 					  false);
1239 
1240 			wil_tx_desc_unmap_edma(dev,
1241 					       (union wil_tx_desc *)d,
1242 					       ctx);
1243 
1244 			if (skb) {
1245 				if (likely(msg.status == 0)) {
1246 					ndev->stats.tx_packets++;
1247 					ndev->stats.tx_bytes += skb->len;
1248 					if (stats) {
1249 						stats->tx_packets++;
1250 						stats->tx_bytes += skb->len;
1251 
1252 						wil_tx_latency_calc(wil, skb,
1253 							&wil->sta[cid]);
1254 					}
1255 				} else {
1256 					ndev->stats.tx_errors++;
1257 					if (stats)
1258 						stats->tx_errors++;
1259 				}
1260 
1261 				if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1262 					wil_tx_complete_handle_eapol(vif, skb);
1263 
1264 				wil_consume_skb(skb, msg.status == 0);
1265 			}
1266 			memset(ctx, 0, sizeof(*ctx));
1267 			/* Make sure the ctx is zeroed before updating the tail
1268 			 * to prevent a case where wil_tx_ring will see
1269 			 * this descriptor as used and handle it before ctx zero
1270 			 * is completed.
1271 			 */
1272 			wmb();
1273 
1274 			ring->swtail = wil_ring_next_tail(ring);
1275 
1276 			desc_cnt++;
1277 		}
1278 
1279 		/* performance monitoring */
1280 		used_new = wil_ring_used_tx(ring);
1281 		if (wil_val_in_range(wil->ring_idle_trsh,
1282 				     used_new, used_before_complete)) {
1283 			wil_dbg_txrx(wil, "Ring[%2d] idle %d -> %d\n",
1284 				     ring_id, used_before_complete, used_new);
1285 			txdata->last_idle = get_cycles();
1286 		}
1287 
1288 again:
1289 		num_statuses++;
1290 		if (num_statuses % WIL_EDMA_TX_SRING_UPDATE_HW_TAIL == 0)
1291 			/* update HW tail to allow HW to push new statuses */
1292 			wil_w(wil, sring->hwtail, sring->swhead);
1293 
1294 		wil_sring_advance_swhead(sring);
1295 
1296 		wil_get_next_tx_status_msg(sring, &msg);
1297 		dr_bit = msg.desc_ready >> TX_STATUS_DESC_READY_POS;
1298 	}
1299 
1300 	/* shall we wake net queues? */
1301 	if (desc_cnt)
1302 		wil_update_net_queues(wil, vif, NULL, false);
1303 
1304 	if (num_statuses % WIL_EDMA_TX_SRING_UPDATE_HW_TAIL != 0)
1305 		/* Update the HW tail ptr (RD ptr) */
1306 		wil_w(wil, sring->hwtail, (sring->swhead - 1) % sring->size);
1307 
1308 	return desc_cnt;
1309 }
1310 
1311 /**
1312  * Sets the descriptor @d up for csum and/or TSO offloading. The corresponding
1313  * @skb is used to obtain the protocol and headers length.
1314  * @tso_desc_type is a descriptor type for TSO: 0 - a header, 1 - first data,
1315  * 2 - middle, 3 - last descriptor.
1316  */
1317 static void wil_tx_desc_offload_setup_tso_edma(struct wil_tx_enhanced_desc *d,
1318 					       int tso_desc_type, bool is_ipv4,
1319 					       int tcp_hdr_len,
1320 					       int skb_net_hdr_len,
1321 					       int mss)
1322 {
1323 	/* Number of descriptors */
1324 	d->mac.d[2] |= 1;
1325 	/* Maximum Segment Size */
1326 	d->mac.tso_mss |= cpu_to_le16(mss >> 2);
1327 	/* L4 header len: TCP header length */
1328 	d->dma.l4_hdr_len |= tcp_hdr_len & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK;
1329 	/* EOP, TSO desc type, Segmentation enable,
1330 	 * Insert IPv4 and TCP / UDP Checksum
1331 	 */
1332 	d->dma.cmd |= BIT(WIL_EDMA_DESC_TX_CFG_EOP_POS) |
1333 		      tso_desc_type << WIL_EDMA_DESC_TX_CFG_TSO_DESC_TYPE_POS |
1334 		      BIT(WIL_EDMA_DESC_TX_CFG_SEG_EN_POS) |
1335 		      BIT(WIL_EDMA_DESC_TX_CFG_INSERT_IP_CHKSUM_POS) |
1336 		      BIT(WIL_EDMA_DESC_TX_CFG_INSERT_TCP_CHKSUM_POS);
1337 	/* Calculate pseudo-header */
1338 	d->dma.w1 |= BIT(WIL_EDMA_DESC_TX_CFG_PSEUDO_HEADER_CALC_EN_POS) |
1339 		     BIT(WIL_EDMA_DESC_TX_CFG_L4_TYPE_POS);
1340 	/* IP Header Length */
1341 	d->dma.ip_length |= skb_net_hdr_len;
1342 	/* MAC header length and IP address family*/
1343 	d->dma.b11 |= ETH_HLEN |
1344 		      is_ipv4 << DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS;
1345 }
1346 
1347 static int wil_tx_tso_gen_desc(struct wil6210_priv *wil, void *buff_addr,
1348 			       int len, uint i, int tso_desc_type,
1349 			       skb_frag_t *frag, struct wil_ring *ring,
1350 			       struct sk_buff *skb, bool is_ipv4,
1351 			       int tcp_hdr_len, int skb_net_hdr_len,
1352 			       int mss, int *descs_used)
1353 {
1354 	struct device *dev = wil_to_dev(wil);
1355 	struct wil_tx_enhanced_desc *_desc = (struct wil_tx_enhanced_desc *)
1356 		&ring->va[i].tx.enhanced;
1357 	struct wil_tx_enhanced_desc desc_mem, *d = &desc_mem;
1358 	int ring_index = ring - wil->ring_tx;
1359 	dma_addr_t pa;
1360 
1361 	if (len == 0)
1362 		return 0;
1363 
1364 	if (!frag) {
1365 		pa = dma_map_single(dev, buff_addr, len, DMA_TO_DEVICE);
1366 		ring->ctx[i].mapped_as = wil_mapped_as_single;
1367 	} else {
1368 		pa = skb_frag_dma_map(dev, frag, 0, len, DMA_TO_DEVICE);
1369 		ring->ctx[i].mapped_as = wil_mapped_as_page;
1370 	}
1371 	if (unlikely(dma_mapping_error(dev, pa))) {
1372 		wil_err(wil, "TSO: Skb DMA map error\n");
1373 		return -EINVAL;
1374 	}
1375 
1376 	wil->txrx_ops.tx_desc_map((union wil_tx_desc *)d, pa,
1377 				   len, ring_index);
1378 	wil_tx_desc_offload_setup_tso_edma(d, tso_desc_type, is_ipv4,
1379 					   tcp_hdr_len,
1380 					   skb_net_hdr_len, mss);
1381 
1382 	/* hold reference to skb
1383 	 * to prevent skb release before accounting
1384 	 * in case of immediate "tx done"
1385 	 */
1386 	if (tso_desc_type == wil_tso_type_lst)
1387 		ring->ctx[i].skb = skb_get(skb);
1388 
1389 	wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4,
1390 			  (const void *)d, sizeof(*d), false);
1391 
1392 	*_desc = *d;
1393 	(*descs_used)++;
1394 
1395 	return 0;
1396 }
1397 
1398 static int __wil_tx_ring_tso_edma(struct wil6210_priv *wil,
1399 				  struct wil6210_vif *vif,
1400 				  struct wil_ring *ring,
1401 				  struct sk_buff *skb)
1402 {
1403 	int ring_index = ring - wil->ring_tx;
1404 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ring_index];
1405 	int nr_frags = skb_shinfo(skb)->nr_frags;
1406 	int min_desc_required = nr_frags + 2; /* Headers, Head, Fragments */
1407 	int used, avail = wil_ring_avail_tx(ring);
1408 	int f, hdrlen, headlen;
1409 	int gso_type;
1410 	bool is_ipv4;
1411 	u32 swhead = ring->swhead;
1412 	int descs_used = 0; /* total number of used descriptors */
1413 	int rc = -EINVAL;
1414 	int tcp_hdr_len;
1415 	int skb_net_hdr_len;
1416 	int mss = skb_shinfo(skb)->gso_size;
1417 
1418 	wil_dbg_txrx(wil, "tx_ring_tso: %d bytes to ring %d\n", skb->len,
1419 		     ring_index);
1420 
1421 	if (unlikely(!txdata->enabled))
1422 		return -EINVAL;
1423 
1424 	if (unlikely(avail < min_desc_required)) {
1425 		wil_err_ratelimited(wil,
1426 				    "TSO: Tx ring[%2d] full. No space for %d fragments\n",
1427 				    ring_index, min_desc_required);
1428 		return -ENOMEM;
1429 	}
1430 
1431 	gso_type = skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV6 | SKB_GSO_TCPV4);
1432 	switch (gso_type) {
1433 	case SKB_GSO_TCPV4:
1434 		is_ipv4 = true;
1435 		break;
1436 	case SKB_GSO_TCPV6:
1437 		is_ipv4 = false;
1438 		break;
1439 	default:
1440 		return -EINVAL;
1441 	}
1442 
1443 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1444 		return -EINVAL;
1445 
1446 	/* tcp header length and skb network header length are fixed for all
1447 	 * packet's descriptors - read them once here
1448 	 */
1449 	tcp_hdr_len = tcp_hdrlen(skb);
1450 	skb_net_hdr_len = skb_network_header_len(skb);
1451 
1452 	/* First descriptor must contain the header only
1453 	 * Header Length = MAC header len + IP header len + TCP header len
1454 	 */
1455 	hdrlen = ETH_HLEN + tcp_hdr_len + skb_net_hdr_len;
1456 	wil_dbg_txrx(wil, "TSO: process header descriptor, hdrlen %u\n",
1457 		     hdrlen);
1458 	rc = wil_tx_tso_gen_desc(wil, skb->data, hdrlen, swhead,
1459 				 wil_tso_type_hdr, NULL, ring, skb,
1460 				 is_ipv4, tcp_hdr_len, skb_net_hdr_len,
1461 				 mss, &descs_used);
1462 	if (rc)
1463 		return -EINVAL;
1464 
1465 	/* Second descriptor contains the head */
1466 	headlen = skb_headlen(skb) - hdrlen;
1467 	wil_dbg_txrx(wil, "TSO: process skb head, headlen %u\n", headlen);
1468 	rc = wil_tx_tso_gen_desc(wil, skb->data + hdrlen, headlen,
1469 				 (swhead + descs_used) % ring->size,
1470 				 (nr_frags != 0) ? wil_tso_type_first :
1471 				 wil_tso_type_lst, NULL, ring, skb,
1472 				 is_ipv4, tcp_hdr_len, skb_net_hdr_len,
1473 				 mss, &descs_used);
1474 	if (rc)
1475 		goto mem_error;
1476 
1477 	/* Rest of the descriptors are from the SKB fragments */
1478 	for (f = 0; f < nr_frags; f++) {
1479 		skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1480 		int len = skb_frag_size(frag);
1481 
1482 		wil_dbg_txrx(wil, "TSO: frag[%d]: len %u, descs_used %d\n", f,
1483 			     len, descs_used);
1484 
1485 		rc = wil_tx_tso_gen_desc(wil, NULL, len,
1486 					 (swhead + descs_used) % ring->size,
1487 					 (f != nr_frags - 1) ?
1488 					 wil_tso_type_mid : wil_tso_type_lst,
1489 					 frag, ring, skb, is_ipv4,
1490 					 tcp_hdr_len, skb_net_hdr_len,
1491 					 mss, &descs_used);
1492 		if (rc)
1493 			goto mem_error;
1494 	}
1495 
1496 	/* performance monitoring */
1497 	used = wil_ring_used_tx(ring);
1498 	if (wil_val_in_range(wil->ring_idle_trsh,
1499 			     used, used + descs_used)) {
1500 		txdata->idle += get_cycles() - txdata->last_idle;
1501 		wil_dbg_txrx(wil,  "Ring[%2d] not idle %d -> %d\n",
1502 			     ring_index, used, used + descs_used);
1503 	}
1504 
1505 	/* advance swhead */
1506 	wil_ring_advance_head(ring, descs_used);
1507 	wil_dbg_txrx(wil, "TSO: Tx swhead %d -> %d\n", swhead, ring->swhead);
1508 
1509 	/* make sure all writes to descriptors (shared memory) are done before
1510 	 * committing them to HW
1511 	 */
1512 	wmb();
1513 
1514 	if (wil->tx_latency)
1515 		*(ktime_t *)&skb->cb = ktime_get();
1516 	else
1517 		memset(skb->cb, 0, sizeof(ktime_t));
1518 
1519 	wil_w(wil, ring->hwtail, ring->swhead);
1520 
1521 	return 0;
1522 
1523 mem_error:
1524 	while (descs_used > 0) {
1525 		struct device *dev = wil_to_dev(wil);
1526 		struct wil_ctx *ctx;
1527 		int i = (swhead + descs_used - 1) % ring->size;
1528 		struct wil_tx_enhanced_desc dd, *d = &dd;
1529 		struct wil_tx_enhanced_desc *_desc =
1530 			(struct wil_tx_enhanced_desc *)
1531 			&ring->va[i].tx.enhanced;
1532 
1533 		*d = *_desc;
1534 		ctx = &ring->ctx[i];
1535 		wil_tx_desc_unmap_edma(dev, (union wil_tx_desc *)d, ctx);
1536 		memset(ctx, 0, sizeof(*ctx));
1537 		descs_used--;
1538 	}
1539 	return rc;
1540 }
1541 
1542 static int wil_ring_init_bcast_edma(struct wil6210_vif *vif, int ring_id,
1543 				    int size)
1544 {
1545 	struct wil6210_priv *wil = vif_to_wil(vif);
1546 	struct wil_ring *ring = &wil->ring_tx[ring_id];
1547 	int rc;
1548 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ring_id];
1549 
1550 	wil_dbg_misc(wil, "init bcast: ring_id=%d, sring_id=%d\n",
1551 		     ring_id, wil->tx_sring_idx);
1552 
1553 	lockdep_assert_held(&wil->mutex);
1554 
1555 	wil_tx_data_init(txdata);
1556 	ring->size = size;
1557 	ring->is_rx = false;
1558 	rc = wil_ring_alloc_desc_ring(wil, ring);
1559 	if (rc)
1560 		goto out;
1561 
1562 	wil->ring2cid_tid[ring_id][0] = WIL6210_MAX_CID; /* CID */
1563 	wil->ring2cid_tid[ring_id][1] = 0; /* TID */
1564 	if (!vif->privacy)
1565 		txdata->dot1x_open = true;
1566 
1567 	rc = wil_wmi_bcast_desc_ring_add(vif, ring_id);
1568 	if (rc)
1569 		goto out_free;
1570 
1571 	return 0;
1572 
1573  out_free:
1574 	spin_lock_bh(&txdata->lock);
1575 	txdata->enabled = 0;
1576 	txdata->dot1x_open = false;
1577 	spin_unlock_bh(&txdata->lock);
1578 	wil_ring_free_edma(wil, ring);
1579 
1580 out:
1581 	return rc;
1582 }
1583 
1584 static void wil_tx_fini_edma(struct wil6210_priv *wil)
1585 {
1586 	struct wil_status_ring *sring = &wil->srings[wil->tx_sring_idx];
1587 
1588 	wil_dbg_misc(wil, "free TX sring\n");
1589 
1590 	wil_sring_free(wil, sring);
1591 }
1592 
1593 static void wil_rx_data_free(struct wil_status_ring *sring)
1594 {
1595 	if (!sring)
1596 		return;
1597 
1598 	kfree_skb(sring->rx_data.skb);
1599 	sring->rx_data.skb = NULL;
1600 }
1601 
1602 static void wil_rx_fini_edma(struct wil6210_priv *wil)
1603 {
1604 	struct wil_ring *ring = &wil->ring_rx;
1605 	int i;
1606 
1607 	wil_dbg_misc(wil, "rx_fini_edma\n");
1608 
1609 	wil_ring_free_edma(wil, ring);
1610 
1611 	for (i = 0; i < wil->num_rx_status_rings; i++) {
1612 		wil_rx_data_free(&wil->srings[i]);
1613 		wil_sring_free(wil, &wil->srings[i]);
1614 	}
1615 
1616 	wil_free_rx_buff_arr(wil);
1617 }
1618 
1619 void wil_init_txrx_ops_edma(struct wil6210_priv *wil)
1620 {
1621 	wil->txrx_ops.configure_interrupt_moderation =
1622 		wil_configure_interrupt_moderation_edma;
1623 	/* TX ops */
1624 	wil->txrx_ops.ring_init_tx = wil_ring_init_tx_edma;
1625 	wil->txrx_ops.ring_fini_tx = wil_ring_free_edma;
1626 	wil->txrx_ops.ring_init_bcast = wil_ring_init_bcast_edma;
1627 	wil->txrx_ops.tx_init = wil_tx_init_edma;
1628 	wil->txrx_ops.tx_fini = wil_tx_fini_edma;
1629 	wil->txrx_ops.tx_desc_map = wil_tx_desc_map_edma;
1630 	wil->txrx_ops.tx_desc_unmap = wil_tx_desc_unmap_edma;
1631 	wil->txrx_ops.tx_ring_tso = __wil_tx_ring_tso_edma;
1632 	wil->txrx_ops.tx_ring_modify = wil_tx_ring_modify_edma;
1633 	/* RX ops */
1634 	wil->txrx_ops.rx_init = wil_rx_init_edma;
1635 	wil->txrx_ops.wmi_addba_rx_resp = wmi_addba_rx_resp_edma;
1636 	wil->txrx_ops.get_reorder_params = wil_get_reorder_params_edma;
1637 	wil->txrx_ops.get_netif_rx_params = wil_get_netif_rx_params_edma;
1638 	wil->txrx_ops.rx_crypto_check = wil_rx_crypto_check_edma;
1639 	wil->txrx_ops.rx_error_check = wil_rx_error_check_edma;
1640 	wil->txrx_ops.is_rx_idle = wil_is_rx_idle_edma;
1641 	wil->txrx_ops.rx_fini = wil_rx_fini_edma;
1642 }
1643 
1644