2 * Copyright (c) 2012 Qualcomm Atheros, Inc.
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.
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.
17 #include <linux/etherdevice.h>
18 #include <net/ieee80211_radiotap.h>
19 #include <linux/if_arp.h>
20 #include <linux/moduleparam.h>
22 #include <linux/ipv6.h>
24 #include <linux/prefetch.h>
31 static bool rtap_include_phy_info;
32 module_param(rtap_include_phy_info, bool, S_IRUGO);
33 MODULE_PARM_DESC(rtap_include_phy_info,
34 " Include PHY info in the radiotap header, default - no");
36 static inline int wil_vring_is_empty(struct vring *vring)
38 return vring->swhead == vring->swtail;
41 static inline u32 wil_vring_next_tail(struct vring *vring)
43 return (vring->swtail + 1) % vring->size;
46 static inline void wil_vring_advance_head(struct vring *vring, int n)
48 vring->swhead = (vring->swhead + n) % vring->size;
51 static inline int wil_vring_is_full(struct vring *vring)
53 return wil_vring_next_tail(vring) == vring->swhead;
56 * Available space in Tx Vring
58 static inline int wil_vring_avail_tx(struct vring *vring)
60 u32 swhead = vring->swhead;
61 u32 swtail = vring->swtail;
62 int used = (vring->size + swhead - swtail) % vring->size;
64 return vring->size - used - 1;
67 static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
69 struct device *dev = wil_to_dev(wil);
70 size_t sz = vring->size * sizeof(vring->va[0]);
73 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
77 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
83 * vring->va should be aligned on its size rounded up to power of 2
84 * This is granted by the dma_alloc_coherent
86 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
92 /* initially, all descriptors are SW owned
93 * For Tx and Rx, ownership bit is at the same location, thus
96 for (i = 0; i < vring->size; i++) {
97 volatile struct vring_tx_desc *_d = &(vring->va[i].tx);
98 _d->dma.status = TX_DMA_STATUS_DU;
101 wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size,
102 vring->va, (unsigned long long)vring->pa, vring->ctx);
107 static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
110 struct device *dev = wil_to_dev(wil);
111 size_t sz = vring->size * sizeof(vring->va[0]);
113 while (!wil_vring_is_empty(vring)) {
119 struct vring_tx_desc dd, *d = ⅆ
120 volatile struct vring_tx_desc *_d =
121 &vring->va[vring->swtail].tx;
123 ctx = &vring->ctx[vring->swtail];
125 pa = wil_desc_addr(&d->dma.addr);
126 dmalen = le16_to_cpu(d->dma.length);
127 if (vring->ctx[vring->swtail].mapped_as_page) {
128 dma_unmap_page(dev, pa, dmalen,
131 dma_unmap_single(dev, pa, dmalen,
135 dev_kfree_skb_any(ctx->skb);
136 vring->swtail = wil_vring_next_tail(vring);
138 struct vring_rx_desc dd, *d = ⅆ
139 volatile struct vring_rx_desc *_d =
140 &vring->va[vring->swhead].rx;
142 ctx = &vring->ctx[vring->swhead];
144 pa = wil_desc_addr(&d->dma.addr);
145 dmalen = le16_to_cpu(d->dma.length);
146 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
148 wil_vring_advance_head(vring, 1);
151 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
159 * Allocate one skb for Rx VRING
161 * Safe to call from IRQ
163 static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
166 struct device *dev = wil_to_dev(wil);
167 unsigned int sz = RX_BUF_LEN;
168 struct vring_rx_desc dd, *d = ⅆ
169 volatile struct vring_rx_desc *_d = &(vring->va[i].rx);
173 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
177 skb_reserve(skb, headroom);
180 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
181 if (unlikely(dma_mapping_error(dev, pa))) {
186 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
187 wil_desc_addr_set(&d->dma.addr, pa);
188 /* ip_length don't care */
190 /* error don't care */
191 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
192 d->dma.length = cpu_to_le16(sz);
194 vring->ctx[i].skb = skb;
200 * Adds radiotap header
202 * Any error indicated as "Bad FCS"
204 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
205 * - Rx descriptor: 32 bytes
208 static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
211 struct wireless_dev *wdev = wil->wdev;
212 struct wil6210_rtap {
213 struct ieee80211_radiotap_header rthdr;
214 /* fields should be in the order of bits in rthdr.it_present */
218 __le16 chnl_freq __aligned(2);
225 struct wil6210_rtap_vendor {
226 struct wil6210_rtap rtap;
228 u8 vendor_oui[3] __aligned(2);
233 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
234 struct wil6210_rtap_vendor *rtap_vendor;
235 int rtap_len = sizeof(struct wil6210_rtap);
236 int phy_length = 0; /* phy info header size, bytes */
237 static char phy_data[128];
238 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
240 if (rtap_include_phy_info) {
241 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
242 /* calculate additional length */
243 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
245 * PHY info starts from 8-byte boundary
246 * there are 8-byte lines, last line may be partially
247 * written (HW bug), thus FW configures for last line
248 * to be excessive. Driver skips this last line.
250 int len = min_t(int, 8 + sizeof(phy_data),
251 wil_rxdesc_phy_length(d));
253 void *p = skb_tail_pointer(skb);
254 void *pa = PTR_ALIGN(p, 8);
255 if (skb_tailroom(skb) >= len + (pa - p)) {
256 phy_length = len - 8;
257 memcpy(phy_data, pa, phy_length);
261 rtap_len += phy_length;
264 if (skb_headroom(skb) < rtap_len &&
265 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
266 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
270 rtap_vendor = (void *)skb_push(skb, rtap_len);
271 memset(rtap_vendor, 0, rtap_len);
273 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
274 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
275 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
276 (1 << IEEE80211_RADIOTAP_FLAGS) |
277 (1 << IEEE80211_RADIOTAP_CHANNEL) |
278 (1 << IEEE80211_RADIOTAP_MCS));
279 if (d->dma.status & RX_DMA_STATUS_ERROR)
280 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
282 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
283 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
285 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
286 rtap_vendor->rtap.mcs_flags = 0;
287 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
289 if (rtap_include_phy_info) {
290 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
291 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
292 /* OUI for Wilocity 04:ce:14 */
293 rtap_vendor->vendor_oui[0] = 0x04;
294 rtap_vendor->vendor_oui[1] = 0xce;
295 rtap_vendor->vendor_oui[2] = 0x14;
296 rtap_vendor->vendor_ns = 1;
297 /* Rx descriptor + PHY data */
298 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
300 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
301 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
307 * Fast swap in place between 2 registers
309 static void wil_swap_u16(u16 *a, u16 *b)
316 static void wil_swap_ethaddr(void *data)
318 struct ethhdr *eth = data;
319 u16 *s = (u16 *)eth->h_source;
320 u16 *d = (u16 *)eth->h_dest;
322 wil_swap_u16(s++, d++);
323 wil_swap_u16(s++, d++);
328 * reap 1 frame from @swhead
330 * Rx descriptor copied to skb->cb
332 * Safe to call from IRQ
334 static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
337 struct device *dev = wil_to_dev(wil);
338 struct net_device *ndev = wil_to_ndev(wil);
339 volatile struct vring_rx_desc *_d;
340 struct vring_rx_desc *d;
343 unsigned int sz = RX_BUF_LEN;
348 struct wil_net_stats *stats;
351 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
353 if (wil_vring_is_empty(vring))
356 _d = &(vring->va[vring->swhead].rx);
357 if (!(_d->dma.status & RX_DMA_STATUS_DU)) {
358 /* it is not error, we just reached end of Rx done area */
362 skb = vring->ctx[vring->swhead].skb;
363 d = wil_skb_rxdesc(skb);
365 pa = wil_desc_addr(&d->dma.addr);
366 vring->ctx[vring->swhead].skb = NULL;
367 wil_vring_advance_head(vring, 1);
369 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
370 dmalen = le16_to_cpu(d->dma.length);
372 trace_wil6210_rx(vring->swhead, d);
373 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen);
374 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
375 (const void *)d, sizeof(*d), false);
378 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
382 skb_trim(skb, dmalen);
386 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
387 skb->data, skb_headlen(skb), false);
389 cid = wil_rxdesc_cid(d);
390 stats = &wil->sta[cid].stats;
391 stats->last_mcs_rx = wil_rxdesc_mcs(d);
392 wil->stats.last_mcs_rx = stats->last_mcs_rx;
394 /* use radiotap header only if required */
395 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
396 wil_rx_add_radiotap_header(wil, skb);
398 /* no extra checks if in sniffer mode */
399 if (ndev->type != ARPHRD_ETHER)
402 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
403 * Driver should recognize it by frame type, that is found
404 * in Rx descriptor. If type is not data, it is 802.11 frame as is
406 ftype = wil_rxdesc_ftype(d) << 2;
407 if (ftype != IEEE80211_FTYPE_DATA) {
408 wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
409 /* TODO: process it */
414 if (skb->len < ETH_HLEN) {
415 wil_err(wil, "Short frame, len = %d\n", skb->len);
416 /* TODO: process it (i.e. BAR) */
421 /* L4 IDENT is on when HW calculated checksum, check status
422 * and in case of error drop the packet
423 * higher stack layers will handle retransmission (if required)
425 if (d->dma.status & RX_DMA_STATUS_L4_IDENT) {
426 /* L4 protocol identified, csum calculated */
427 if ((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0)
428 skb->ip_summed = CHECKSUM_UNNECESSARY;
429 /* If HW reports bad checksum, let IP stack re-check it
430 * For example, HW don't understand Microsoft IP stack that
431 * mis-calculates TCP checksum - if it should be 0x0,
432 * it writes 0xffff in violation of RFC 1624
436 ds_bits = wil_rxdesc_ds_bits(d);
439 * HW bug - in ToDS mode, i.e. Rx on AP side,
440 * addresses get swapped
442 wil_swap_ethaddr(skb->data);
449 * allocate and fill up to @count buffers in rx ring
450 * buffers posted at @swtail
452 static int wil_rx_refill(struct wil6210_priv *wil, int count)
454 struct net_device *ndev = wil_to_ndev(wil);
455 struct vring *v = &wil->vring_rx;
458 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
459 WIL6210_RTAP_SIZE : 0;
461 for (; next_tail = wil_vring_next_tail(v),
462 (next_tail != v->swhead) && (count-- > 0);
463 v->swtail = next_tail) {
464 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
466 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
471 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
477 * Pass Rx packet to the netif. Update statistics.
478 * Called in softirq context (NAPI poll).
480 void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
483 struct wil6210_priv *wil = ndev_to_wil(ndev);
484 unsigned int len = skb->len;
485 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
486 int cid = wil_rxdesc_cid(d);
487 struct wil_net_stats *stats = &wil->sta[cid].stats;
491 rc = netif_receive_skb(skb);
493 if (likely(rc == NET_RX_SUCCESS)) {
494 ndev->stats.rx_packets++;
496 ndev->stats.rx_bytes += len;
497 stats->rx_bytes += len;
500 ndev->stats.rx_dropped++;
506 * Proceed all completed skb's from Rx VRING
508 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
510 void wil_rx_handle(struct wil6210_priv *wil, int *quota)
512 struct net_device *ndev = wil_to_ndev(wil);
513 struct vring *v = &wil->vring_rx;
517 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
520 wil_dbg_txrx(wil, "%s()\n", __func__);
521 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
524 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
526 skb_reset_mac_header(skb);
527 skb->ip_summed = CHECKSUM_UNNECESSARY;
528 skb->pkt_type = PACKET_OTHERHOST;
529 skb->protocol = htons(ETH_P_802_2);
530 wil_netif_rx_any(skb, ndev);
532 struct ethhdr *eth = (void *)skb->data;
534 skb->protocol = eth_type_trans(skb, ndev);
536 if (is_unicast_ether_addr(eth->h_dest))
537 wil_rx_reorder(wil, skb);
539 wil_netif_rx_any(skb, ndev);
543 wil_rx_refill(wil, v->size);
546 int wil_rx_init(struct wil6210_priv *wil)
548 struct vring *vring = &wil->vring_rx;
551 vring->size = WIL6210_RX_RING_SIZE;
552 rc = wil_vring_alloc(wil, vring);
556 rc = wmi_rx_chain_add(wil, vring);
560 rc = wil_rx_refill(wil, vring->size);
566 wil_vring_free(wil, vring, 0);
571 void wil_rx_fini(struct wil6210_priv *wil)
573 struct vring *vring = &wil->vring_rx;
576 wil_vring_free(wil, vring, 0);
579 int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
583 struct wmi_vring_cfg_cmd cmd = {
584 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
587 .max_mpdu_size = cpu_to_le16(TX_BUF_LEN),
588 .ring_size = cpu_to_le16(size),
591 .cidxtid = mk_cidxtid(cid, tid),
592 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
597 .priority = cpu_to_le16(0),
598 .timeslot_us = cpu_to_le16(0xfff),
603 struct wil6210_mbox_hdr_wmi wmi;
604 struct wmi_vring_cfg_done_event cmd;
606 struct vring *vring = &wil->vring_tx[id];
609 wil_err(wil, "Tx ring [%d] already allocated\n", id);
615 rc = wil_vring_alloc(wil, vring);
619 wil->vring2cid_tid[id][0] = cid;
620 wil->vring2cid_tid[id][1] = tid;
622 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
624 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
625 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
629 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
630 wil_err(wil, "Tx config failed, status 0x%02x\n",
635 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
639 wil_vring_free(wil, vring, 1);
645 void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
647 struct vring *vring = &wil->vring_tx[id];
652 wil_vring_free(wil, vring, 1);
655 static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
659 struct ethhdr *eth = (void *)skb->data;
660 int cid = wil_find_cid(wil, eth->h_dest);
665 /* TODO: fix for multiple TID */
666 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
667 if (wil->vring2cid_tid[i][0] == cid) {
668 struct vring *v = &wil->vring_tx[i];
669 wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n",
670 __func__, eth->h_dest, i);
674 wil_dbg_txrx(wil, "vring[%d] not valid\n", i);
683 static void wil_set_da_for_vring(struct wil6210_priv *wil,
684 struct sk_buff *skb, int vring_index)
686 struct ethhdr *eth = (void *)skb->data;
687 int cid = wil->vring2cid_tid[vring_index][0];
688 memcpy(eth->h_dest, wil->sta[cid].addr, ETH_ALEN);
691 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
692 struct sk_buff *skb);
694 * Find 1-st vring and return it; set dest address for this vring in skb
695 * duplicate skb and send it to other active vrings
697 static struct vring *wil_tx_bcast(struct wil6210_priv *wil,
700 struct vring *v, *v2;
701 struct sk_buff *skb2;
704 /* find 1-st vring */
705 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
706 v = &wil->vring_tx[i];
711 wil_err(wil, "Tx while no vrings active?\n");
716 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
717 wil_set_da_for_vring(wil, skb, i);
719 /* find other active vrings and duplicate skb for each */
720 for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
721 v2 = &wil->vring_tx[i];
724 skb2 = skb_copy(skb, GFP_ATOMIC);
726 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
727 wil_set_da_for_vring(wil, skb2, i);
728 wil_tx_vring(wil, v2, skb2);
730 wil_err(wil, "skb_copy failed\n");
737 static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
740 wil_desc_addr_set(&d->dma.addr, pa);
741 d->dma.ip_length = 0;
742 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
743 d->dma.b11 = 0/*14 | BIT(7)*/;
745 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
746 d->dma.length = cpu_to_le16((u16)len);
747 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
751 d->mac.ucode_cmd = 0;
752 /* use dst index 0 */
753 d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
754 (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
755 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
756 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
757 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
762 static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil,
763 struct vring_tx_desc *d,
768 if (skb->ip_summed != CHECKSUM_PARTIAL)
771 d->dma.b11 = ETH_HLEN; /* MAC header length */
773 switch (skb->protocol) {
774 case cpu_to_be16(ETH_P_IP):
775 protocol = ip_hdr(skb)->protocol;
776 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
778 case cpu_to_be16(ETH_P_IPV6):
779 protocol = ipv6_hdr(skb)->nexthdr;
787 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
788 /* L4 header len: TCP header length */
790 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
793 /* L4 header len: UDP header length */
795 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
801 d->dma.ip_length = skb_network_header_len(skb);
802 /* Enable TCP/UDP checksum */
803 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
804 /* Calculate pseudo-header */
805 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
810 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
813 struct device *dev = wil_to_dev(wil);
814 struct vring_tx_desc dd, *d = ⅆ
815 volatile struct vring_tx_desc *_d;
816 u32 swhead = vring->swhead;
817 int avail = wil_vring_avail_tx(vring);
818 int nr_frags = skb_shinfo(skb)->nr_frags;
820 int vring_index = vring - wil->vring_tx;
824 wil_dbg_txrx(wil, "%s()\n", __func__);
826 if (avail < vring->size/8)
827 netif_tx_stop_all_queues(wil_to_ndev(wil));
828 if (avail < 1 + nr_frags) {
829 wil_err(wil, "Tx ring full. No space for %d fragments\n",
833 _d = &(vring->va[i].tx);
835 pa = dma_map_single(dev, skb->data,
836 skb_headlen(skb), DMA_TO_DEVICE);
838 wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb),
839 skb->data, (unsigned long long)pa);
840 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
841 skb->data, skb_headlen(skb), false);
843 if (unlikely(dma_mapping_error(dev, pa)))
846 wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index);
847 /* Process TCP/UDP checksum offloading */
848 if (wil_tx_desc_offload_cksum_set(wil, d, skb)) {
849 wil_err(wil, "VRING #%d Failed to set cksum, drop packet\n",
854 d->mac.d[2] |= ((nr_frags + 1) <<
855 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
859 /* middle segments */
860 for (; f < nr_frags; f++) {
861 const struct skb_frag_struct *frag =
862 &skb_shinfo(skb)->frags[f];
863 int len = skb_frag_size(frag);
864 i = (swhead + f + 1) % vring->size;
865 _d = &(vring->va[i].tx);
866 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
868 if (unlikely(dma_mapping_error(dev, pa)))
870 wil_tx_desc_map(d, pa, len, vring_index);
871 vring->ctx[i].mapped_as_page = 1;
874 /* for the last seg only */
875 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
876 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
877 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
880 /* hold reference to skb
881 * to prevent skb release before accounting
882 * in case of immediate "tx done"
884 vring->ctx[i].skb = skb_get(skb);
886 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4,
887 (const void *)d, sizeof(*d), false);
890 wil_vring_advance_head(vring, nr_frags + 1);
891 wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
892 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
893 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
897 /* unmap what we have mapped */
898 nr_frags = f + 1; /* frags mapped + one for skb head */
899 for (f = 0; f < nr_frags; f++) {
903 i = (swhead + f) % vring->size;
904 ctx = &vring->ctx[i];
905 _d = &(vring->va[i].tx);
907 _d->dma.status = TX_DMA_STATUS_DU;
908 pa = wil_desc_addr(&d->dma.addr);
909 dmalen = le16_to_cpu(d->dma.length);
910 if (ctx->mapped_as_page)
911 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
913 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
916 dev_kfree_skb_any(ctx->skb);
918 memset(ctx, 0, sizeof(*ctx));
925 netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
927 struct wil6210_priv *wil = ndev_to_wil(ndev);
928 struct ethhdr *eth = (void *)skb->data;
932 wil_dbg_txrx(wil, "%s()\n", __func__);
933 if (!test_bit(wil_status_fwready, &wil->status)) {
934 wil_err(wil, "FW not ready\n");
937 if (!test_bit(wil_status_fwconnected, &wil->status)) {
938 wil_err(wil, "FW not connected\n");
941 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
942 wil_err(wil, "Xmit in monitor mode not supported\n");
947 if (is_unicast_ether_addr(eth->h_dest)) {
948 vring = wil_find_tx_vring(wil, skb);
950 vring = wil_tx_bcast(wil, skb);
953 wil_err(wil, "No Tx VRING found for %pM\n", eth->h_dest);
956 /* set up vring entry */
957 rc = wil_tx_vring(wil, vring, skb);
961 /* statistics will be updated on the tx_complete */
962 dev_kfree_skb_any(skb);
965 return NETDEV_TX_BUSY;
967 break; /* goto drop; */
970 ndev->stats.tx_dropped++;
971 dev_kfree_skb_any(skb);
973 return NET_XMIT_DROP;
977 * Clean up transmitted skb's from the Tx VRING
979 * Return number of descriptors cleared
981 * Safe to call from IRQ
983 int wil_tx_complete(struct wil6210_priv *wil, int ringid)
985 struct net_device *ndev = wil_to_ndev(wil);
986 struct device *dev = wil_to_dev(wil);
987 struct vring *vring = &wil->vring_tx[ringid];
989 int cid = wil->vring2cid_tid[ringid][0];
990 struct wil_net_stats *stats = &wil->sta[cid].stats;
993 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
997 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
999 while (!wil_vring_is_empty(vring)) {
1000 volatile struct vring_tx_desc *_d =
1001 &vring->va[vring->swtail].tx;
1002 struct vring_tx_desc dd, *d = ⅆ
1005 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
1006 struct sk_buff *skb = ctx->skb;
1010 if (!(d->dma.status & TX_DMA_STATUS_DU))
1013 dmalen = le16_to_cpu(d->dma.length);
1014 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
1017 "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
1018 vring->swtail, dmalen, d->dma.status,
1020 wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4,
1021 (const void *)d, sizeof(*d), false);
1023 pa = wil_desc_addr(&d->dma.addr);
1024 if (ctx->mapped_as_page)
1025 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
1027 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
1030 if (d->dma.error == 0) {
1031 ndev->stats.tx_packets++;
1032 stats->tx_packets++;
1033 ndev->stats.tx_bytes += skb->len;
1034 stats->tx_bytes += skb->len;
1036 ndev->stats.tx_errors++;
1040 dev_kfree_skb_any(skb);
1042 memset(ctx, 0, sizeof(*ctx));
1044 * There is no need to touch HW descriptor:
1045 * - ststus bit TX_DMA_STATUS_DU is set by design,
1046 * so hardware will not try to process this desc.,
1047 * - rest of descriptor will be initialized on Tx.
1049 vring->swtail = wil_vring_next_tail(vring);
1052 if (wil_vring_avail_tx(vring) > vring->size/4)
1053 netif_tx_wake_all_queues(wil_to_ndev(wil));