2 * Copyright (c) 2012-2014 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;
57 * Available space in Tx Vring
59 static inline int wil_vring_avail_tx(struct vring *vring)
61 u32 swhead = vring->swhead;
62 u32 swtail = vring->swtail;
63 int used = (vring->size + swhead - swtail) % vring->size;
65 return vring->size - used - 1;
69 * wil_vring_wmark_low - low watermark for available descriptor space
71 static inline int wil_vring_wmark_low(struct vring *vring)
77 * wil_vring_wmark_high - high watermark for available descriptor space
79 static inline int wil_vring_wmark_high(struct vring *vring)
84 static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring)
86 struct device *dev = wil_to_dev(wil);
87 size_t sz = vring->size * sizeof(vring->va[0]);
90 wil_dbg_misc(wil, "%s()\n", __func__);
92 BUILD_BUG_ON(sizeof(vring->va[0]) != 32);
96 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL);
102 * vring->va should be aligned on its size rounded up to power of 2
103 * This is granted by the dma_alloc_coherent
105 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL);
111 /* initially, all descriptors are SW owned
112 * For Tx and Rx, ownership bit is at the same location, thus
115 for (i = 0; i < vring->size; i++) {
116 volatile struct vring_tx_desc *_d = &vring->va[i].tx;
118 _d->dma.status = TX_DMA_STATUS_DU;
121 wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size,
122 vring->va, &vring->pa, vring->ctx);
127 static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d,
130 dma_addr_t pa = wil_desc_addr(&d->dma.addr);
131 u16 dmalen = le16_to_cpu(d->dma.length);
133 switch (ctx->mapped_as) {
134 case wil_mapped_as_single:
135 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE);
137 case wil_mapped_as_page:
138 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE);
145 static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring,
148 struct device *dev = wil_to_dev(wil);
149 size_t sz = vring->size * sizeof(vring->va[0]);
152 int vring_index = vring - wil->vring_tx;
154 wil_dbg_misc(wil, "free Tx vring %d [%d] 0x%p:%pad 0x%p\n",
155 vring_index, vring->size, vring->va,
156 &vring->pa, vring->ctx);
158 wil_dbg_misc(wil, "free Rx vring [%d] 0x%p:%pad 0x%p\n",
159 vring->size, vring->va,
160 &vring->pa, vring->ctx);
163 while (!wil_vring_is_empty(vring)) {
169 struct vring_tx_desc dd, *d = ⅆ
170 volatile struct vring_tx_desc *_d =
171 &vring->va[vring->swtail].tx;
173 ctx = &vring->ctx[vring->swtail];
175 wil_txdesc_unmap(dev, d, ctx);
177 dev_kfree_skb_any(ctx->skb);
178 vring->swtail = wil_vring_next_tail(vring);
180 struct vring_rx_desc dd, *d = ⅆ
181 volatile struct vring_rx_desc *_d =
182 &vring->va[vring->swhead].rx;
184 ctx = &vring->ctx[vring->swhead];
186 pa = wil_desc_addr(&d->dma.addr);
187 dmalen = le16_to_cpu(d->dma.length);
188 dma_unmap_single(dev, pa, dmalen, DMA_FROM_DEVICE);
190 wil_vring_advance_head(vring, 1);
193 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa);
201 * Allocate one skb for Rx VRING
203 * Safe to call from IRQ
205 static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring,
208 struct device *dev = wil_to_dev(wil);
209 unsigned int sz = mtu_max + ETH_HLEN;
210 struct vring_rx_desc dd, *d = ⅆ
211 volatile struct vring_rx_desc *_d = &vring->va[i].rx;
213 struct sk_buff *skb = dev_alloc_skb(sz + headroom);
218 skb_reserve(skb, headroom);
221 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE);
222 if (unlikely(dma_mapping_error(dev, pa))) {
227 d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT;
228 wil_desc_addr_set(&d->dma.addr, pa);
229 /* ip_length don't care */
231 /* error don't care */
232 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
233 d->dma.length = cpu_to_le16(sz);
235 vring->ctx[i].skb = skb;
241 * Adds radiotap header
243 * Any error indicated as "Bad FCS"
245 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of:
246 * - Rx descriptor: 32 bytes
249 static void wil_rx_add_radiotap_header(struct wil6210_priv *wil,
252 struct wireless_dev *wdev = wil->wdev;
253 struct wil6210_rtap {
254 struct ieee80211_radiotap_header rthdr;
255 /* fields should be in the order of bits in rthdr.it_present */
259 __le16 chnl_freq __aligned(2);
266 struct wil6210_rtap_vendor {
267 struct wil6210_rtap rtap;
269 u8 vendor_oui[3] __aligned(2);
274 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
275 struct wil6210_rtap_vendor *rtap_vendor;
276 int rtap_len = sizeof(struct wil6210_rtap);
277 int phy_length = 0; /* phy info header size, bytes */
278 static char phy_data[128];
279 struct ieee80211_channel *ch = wdev->preset_chandef.chan;
281 if (rtap_include_phy_info) {
282 rtap_len = sizeof(*rtap_vendor) + sizeof(*d);
283 /* calculate additional length */
284 if (d->dma.status & RX_DMA_STATUS_PHY_INFO) {
286 * PHY info starts from 8-byte boundary
287 * there are 8-byte lines, last line may be partially
288 * written (HW bug), thus FW configures for last line
289 * to be excessive. Driver skips this last line.
291 int len = min_t(int, 8 + sizeof(phy_data),
292 wil_rxdesc_phy_length(d));
295 void *p = skb_tail_pointer(skb);
296 void *pa = PTR_ALIGN(p, 8);
298 if (skb_tailroom(skb) >= len + (pa - p)) {
299 phy_length = len - 8;
300 memcpy(phy_data, pa, phy_length);
304 rtap_len += phy_length;
307 if (skb_headroom(skb) < rtap_len &&
308 pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) {
309 wil_err(wil, "Unable to expand headrom to %d\n", rtap_len);
313 rtap_vendor = (void *)skb_push(skb, rtap_len);
314 memset(rtap_vendor, 0, rtap_len);
316 rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION;
317 rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len);
318 rtap_vendor->rtap.rthdr.it_present = cpu_to_le32(
319 (1 << IEEE80211_RADIOTAP_FLAGS) |
320 (1 << IEEE80211_RADIOTAP_CHANNEL) |
321 (1 << IEEE80211_RADIOTAP_MCS));
322 if (d->dma.status & RX_DMA_STATUS_ERROR)
323 rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS;
325 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320);
326 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0);
328 rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS;
329 rtap_vendor->rtap.mcs_flags = 0;
330 rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d);
332 if (rtap_include_phy_info) {
333 rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 <<
334 IEEE80211_RADIOTAP_VENDOR_NAMESPACE);
335 /* OUI for Wilocity 04:ce:14 */
336 rtap_vendor->vendor_oui[0] = 0x04;
337 rtap_vendor->vendor_oui[1] = 0xce;
338 rtap_vendor->vendor_oui[2] = 0x14;
339 rtap_vendor->vendor_ns = 1;
340 /* Rx descriptor + PHY data */
341 rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) +
343 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d));
344 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data,
350 * Fast swap in place between 2 registers
352 static void wil_swap_u16(u16 *a, u16 *b)
359 static void wil_swap_ethaddr(void *data)
361 struct ethhdr *eth = data;
362 u16 *s = (u16 *)eth->h_source;
363 u16 *d = (u16 *)eth->h_dest;
365 wil_swap_u16(s++, d++);
366 wil_swap_u16(s++, d++);
371 * reap 1 frame from @swhead
373 * Rx descriptor copied to skb->cb
375 * Safe to call from IRQ
377 static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil,
380 struct device *dev = wil_to_dev(wil);
381 struct net_device *ndev = wil_to_ndev(wil);
382 volatile struct vring_rx_desc *_d;
383 struct vring_rx_desc *d;
386 unsigned int sz = mtu_max + ETH_HLEN;
391 struct wil_net_stats *stats;
393 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb));
395 if (wil_vring_is_empty(vring))
398 _d = &vring->va[vring->swhead].rx;
399 if (!(_d->dma.status & RX_DMA_STATUS_DU)) {
400 /* it is not error, we just reached end of Rx done area */
404 skb = vring->ctx[vring->swhead].skb;
405 d = wil_skb_rxdesc(skb);
407 pa = wil_desc_addr(&d->dma.addr);
408 vring->ctx[vring->swhead].skb = NULL;
409 wil_vring_advance_head(vring, 1);
411 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE);
412 dmalen = le16_to_cpu(d->dma.length);
414 trace_wil6210_rx(vring->swhead, d);
415 wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, dmalen);
416 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4,
417 (const void *)d, sizeof(*d), false);
420 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen);
424 skb_trim(skb, dmalen);
428 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1,
429 skb->data, skb_headlen(skb), false);
431 cid = wil_rxdesc_cid(d);
432 stats = &wil->sta[cid].stats;
433 stats->last_mcs_rx = wil_rxdesc_mcs(d);
435 /* use radiotap header only if required */
436 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP)
437 wil_rx_add_radiotap_header(wil, skb);
439 /* no extra checks if in sniffer mode */
440 if (ndev->type != ARPHRD_ETHER)
443 * Non-data frames may be delivered through Rx DMA channel (ex: BAR)
444 * Driver should recognize it by frame type, that is found
445 * in Rx descriptor. If type is not data, it is 802.11 frame as is
447 ftype = wil_rxdesc_ftype(d) << 2;
448 if (ftype != IEEE80211_FTYPE_DATA) {
449 wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype);
450 /* TODO: process it */
455 if (skb->len < ETH_HLEN) {
456 wil_err(wil, "Short frame, len = %d\n", skb->len);
457 /* TODO: process it (i.e. BAR) */
462 /* L4 IDENT is on when HW calculated checksum, check status
463 * and in case of error drop the packet
464 * higher stack layers will handle retransmission (if required)
466 if (d->dma.status & RX_DMA_STATUS_L4_IDENT) {
467 /* L4 protocol identified, csum calculated */
468 if ((d->dma.error & RX_DMA_ERROR_L4_ERR) == 0)
469 skb->ip_summed = CHECKSUM_UNNECESSARY;
470 /* If HW reports bad checksum, let IP stack re-check it
471 * For example, HW don't understand Microsoft IP stack that
472 * mis-calculates TCP checksum - if it should be 0x0,
473 * it writes 0xffff in violation of RFC 1624
477 ds_bits = wil_rxdesc_ds_bits(d);
480 * HW bug - in ToDS mode, i.e. Rx on AP side,
481 * addresses get swapped
483 wil_swap_ethaddr(skb->data);
490 * allocate and fill up to @count buffers in rx ring
491 * buffers posted at @swtail
493 static int wil_rx_refill(struct wil6210_priv *wil, int count)
495 struct net_device *ndev = wil_to_ndev(wil);
496 struct vring *v = &wil->vring_rx;
499 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ?
500 WIL6210_RTAP_SIZE : 0;
502 for (; next_tail = wil_vring_next_tail(v),
503 (next_tail != v->swhead) && (count-- > 0);
504 v->swtail = next_tail) {
505 rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom);
507 wil_err(wil, "Error %d in wil_rx_refill[%d]\n",
512 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail));
518 * Pass Rx packet to the netif. Update statistics.
519 * Called in softirq context (NAPI poll).
521 void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev)
524 struct wil6210_priv *wil = ndev_to_wil(ndev);
525 unsigned int len = skb->len;
526 struct vring_rx_desc *d = wil_skb_rxdesc(skb);
527 int cid = wil_rxdesc_cid(d);
528 struct wil_net_stats *stats = &wil->sta[cid].stats;
532 rc = napi_gro_receive(&wil->napi_rx, skb);
534 if (unlikely(rc == GRO_DROP)) {
535 ndev->stats.rx_dropped++;
537 wil_dbg_txrx(wil, "Rx drop %d bytes\n", len);
539 ndev->stats.rx_packets++;
541 ndev->stats.rx_bytes += len;
542 stats->rx_bytes += len;
545 static const char * const gro_res_str[] = {
546 [GRO_MERGED] = "GRO_MERGED",
547 [GRO_MERGED_FREE] = "GRO_MERGED_FREE",
548 [GRO_HELD] = "GRO_HELD",
549 [GRO_NORMAL] = "GRO_NORMAL",
550 [GRO_DROP] = "GRO_DROP",
552 wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n",
553 len, gro_res_str[rc]);
558 * Proceed all completed skb's from Rx VRING
560 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled
562 void wil_rx_handle(struct wil6210_priv *wil, int *quota)
564 struct net_device *ndev = wil_to_ndev(wil);
565 struct vring *v = &wil->vring_rx;
569 wil_err(wil, "Rx IRQ while Rx not yet initialized\n");
572 wil_dbg_txrx(wil, "%s()\n", __func__);
573 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) {
576 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
578 skb_reset_mac_header(skb);
579 skb->ip_summed = CHECKSUM_UNNECESSARY;
580 skb->pkt_type = PACKET_OTHERHOST;
581 skb->protocol = htons(ETH_P_802_2);
582 wil_netif_rx_any(skb, ndev);
584 struct ethhdr *eth = (void *)skb->data;
586 skb->protocol = eth_type_trans(skb, ndev);
588 if (is_unicast_ether_addr(eth->h_dest))
589 wil_rx_reorder(wil, skb);
591 wil_netif_rx_any(skb, ndev);
594 wil_rx_refill(wil, v->size);
597 int wil_rx_init(struct wil6210_priv *wil, u16 size)
599 struct vring *vring = &wil->vring_rx;
602 wil_dbg_misc(wil, "%s()\n", __func__);
605 wil_err(wil, "Rx ring already allocated\n");
610 rc = wil_vring_alloc(wil, vring);
614 rc = wmi_rx_chain_add(wil, vring);
618 rc = wil_rx_refill(wil, vring->size);
624 wil_vring_free(wil, vring, 0);
629 void wil_rx_fini(struct wil6210_priv *wil)
631 struct vring *vring = &wil->vring_rx;
633 wil_dbg_misc(wil, "%s()\n", __func__);
636 wil_vring_free(wil, vring, 0);
639 int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size,
643 struct wmi_vring_cfg_cmd cmd = {
644 .action = cpu_to_le32(WMI_VRING_CMD_ADD),
648 cpu_to_le16(wil_mtu2macbuf(mtu_max)),
649 .ring_size = cpu_to_le16(size),
652 .cidxtid = mk_cidxtid(cid, tid),
653 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3,
658 .priority = cpu_to_le16(0),
659 .timeslot_us = cpu_to_le16(0xfff),
664 struct wil6210_mbox_hdr_wmi wmi;
665 struct wmi_vring_cfg_done_event cmd;
667 struct vring *vring = &wil->vring_tx[id];
668 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
670 wil_dbg_misc(wil, "%s() max_mpdu_size %d\n", __func__,
671 cmd.vring_cfg.tx_sw_ring.max_mpdu_size);
674 wil_err(wil, "Tx ring [%d] already allocated\n", id);
679 memset(txdata, 0, sizeof(*txdata));
681 rc = wil_vring_alloc(wil, vring);
685 wil->vring2cid_tid[id][0] = cid;
686 wil->vring2cid_tid[id][1] = tid;
688 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa);
690 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd),
691 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100);
695 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) {
696 wil_err(wil, "Tx config failed, status 0x%02x\n",
701 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr);
704 if (wil->sta[cid].data_port_open && (agg_wsize >= 0))
705 wil_addba_tx_request(wil, id, agg_wsize);
709 wil_vring_free(wil, vring, 1);
715 void wil_vring_fini_tx(struct wil6210_priv *wil, int id)
717 struct vring *vring = &wil->vring_tx[id];
718 struct vring_tx_data *txdata = &wil->vring_tx_data[id];
720 WARN_ON(!mutex_is_locked(&wil->mutex));
725 wil_dbg_misc(wil, "%s() id=%d\n", __func__, id);
727 /* make sure NAPI won't touch this vring */
728 wil->vring_tx_data[id].enabled = 0;
729 if (test_bit(wil_status_napi_en, wil->status))
730 napi_synchronize(&wil->napi_tx);
732 wil_vring_free(wil, vring, 1);
733 memset(txdata, 0, sizeof(*txdata));
736 static struct vring *wil_find_tx_vring(struct wil6210_priv *wil,
740 struct ethhdr *eth = (void *)skb->data;
741 int cid = wil_find_cid(wil, eth->h_dest);
746 if (!wil->sta[cid].data_port_open &&
747 (skb->protocol != cpu_to_be16(ETH_P_PAE)))
750 /* TODO: fix for multiple TID */
751 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) {
752 if (wil->vring2cid_tid[i][0] == cid) {
753 struct vring *v = &wil->vring_tx[i];
755 wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n",
756 __func__, eth->h_dest, i);
760 wil_dbg_txrx(wil, "vring[%d] not valid\n", i);
769 static void wil_set_da_for_vring(struct wil6210_priv *wil,
770 struct sk_buff *skb, int vring_index)
772 struct ethhdr *eth = (void *)skb->data;
773 int cid = wil->vring2cid_tid[vring_index][0];
775 memcpy(eth->h_dest, wil->sta[cid].addr, ETH_ALEN);
778 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
779 struct sk_buff *skb);
781 * Find 1-st vring and return it; set dest address for this vring in skb
782 * duplicate skb and send it to other active vrings
784 static struct vring *wil_tx_bcast(struct wil6210_priv *wil,
787 struct vring *v, *v2;
788 struct sk_buff *skb2;
792 /* find 1-st vring eligible for data */
793 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
794 v = &wil->vring_tx[i];
798 cid = wil->vring2cid_tid[i][0];
799 if (!wil->sta[cid].data_port_open)
805 wil_dbg_txrx(wil, "Tx while no vrings active?\n");
810 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i);
811 wil_set_da_for_vring(wil, skb, i);
813 /* find other active vrings and duplicate skb for each */
814 for (i++; i < WIL6210_MAX_TX_RINGS; i++) {
815 v2 = &wil->vring_tx[i];
818 cid = wil->vring2cid_tid[i][0];
819 if (!wil->sta[cid].data_port_open)
822 skb2 = skb_copy(skb, GFP_ATOMIC);
824 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i);
825 wil_set_da_for_vring(wil, skb2, i);
826 wil_tx_vring(wil, v2, skb2);
828 wil_err(wil, "skb_copy failed\n");
835 static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len,
838 wil_desc_addr_set(&d->dma.addr, pa);
839 d->dma.ip_length = 0;
840 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/
841 d->dma.b11 = 0/*14 | BIT(7)*/;
843 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */
844 d->dma.length = cpu_to_le16((u16)len);
845 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS);
849 d->mac.ucode_cmd = 0;
850 /* use dst index 0 */
851 d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) |
852 (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS);
853 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */
854 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) |
855 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS);
861 void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags)
863 d->mac.d[2] |= ((nr_frags + 1) <<
864 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS);
867 static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil,
868 struct vring_tx_desc *d,
873 if (skb->ip_summed != CHECKSUM_PARTIAL)
876 d->dma.b11 = ETH_HLEN; /* MAC header length */
878 switch (skb->protocol) {
879 case cpu_to_be16(ETH_P_IP):
880 protocol = ip_hdr(skb)->protocol;
881 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS);
883 case cpu_to_be16(ETH_P_IPV6):
884 protocol = ipv6_hdr(skb)->nexthdr;
892 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS);
893 /* L4 header len: TCP header length */
895 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
898 /* L4 header len: UDP header length */
900 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK);
906 d->dma.ip_length = skb_network_header_len(skb);
907 /* Enable TCP/UDP checksum */
908 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS);
909 /* Calculate pseudo-header */
910 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS);
915 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring,
918 struct device *dev = wil_to_dev(wil);
919 struct vring_tx_desc dd, *d = ⅆ
920 volatile struct vring_tx_desc *_d;
921 u32 swhead = vring->swhead;
922 int avail = wil_vring_avail_tx(vring);
923 int nr_frags = skb_shinfo(skb)->nr_frags;
925 int vring_index = vring - wil->vring_tx;
926 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index];
930 wil_dbg_txrx(wil, "%s()\n", __func__);
932 if (avail < 1 + nr_frags) {
933 wil_err_ratelimited(wil,
934 "Tx ring full. No space for %d fragments\n",
938 _d = &vring->va[i].tx;
940 pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
942 wil_dbg_txrx(wil, "Tx skb %d bytes 0x%p -> %pad\n", skb_headlen(skb),
944 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1,
945 skb->data, skb_headlen(skb), false);
947 if (unlikely(dma_mapping_error(dev, pa)))
949 vring->ctx[i].mapped_as = wil_mapped_as_single;
951 wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index);
952 /* Process TCP/UDP checksum offloading */
953 if (wil_tx_desc_offload_cksum_set(wil, d, skb)) {
954 wil_err(wil, "VRING #%d Failed to set cksum, drop packet\n",
959 vring->ctx[i].nr_frags = nr_frags;
960 wil_tx_desc_set_nr_frags(d, nr_frags);
964 /* middle segments */
965 for (; f < nr_frags; f++) {
966 const struct skb_frag_struct *frag =
967 &skb_shinfo(skb)->frags[f];
968 int len = skb_frag_size(frag);
970 i = (swhead + f + 1) % vring->size;
971 _d = &vring->va[i].tx;
972 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag),
974 if (unlikely(dma_mapping_error(dev, pa)))
976 vring->ctx[i].mapped_as = wil_mapped_as_page;
977 wil_tx_desc_map(d, pa, len, vring_index);
978 /* no need to check return code -
979 * if it succeeded for 1-st descriptor,
980 * it will succeed here too
982 wil_tx_desc_offload_cksum_set(wil, d, skb);
985 /* for the last seg only */
986 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS);
987 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS);
988 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS);
991 /* hold reference to skb
992 * to prevent skb release before accounting
993 * in case of immediate "tx done"
995 vring->ctx[i].skb = skb_get(skb);
997 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4,
998 (const void *)d, sizeof(*d), false);
1000 if (wil_vring_is_empty(vring)) /* performance monitoring */
1001 txdata->idle += get_cycles() - txdata->last_idle;
1003 /* advance swhead */
1004 wil_vring_advance_head(vring, nr_frags + 1);
1005 wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead);
1006 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags);
1007 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail));
1011 /* unmap what we have mapped */
1012 nr_frags = f + 1; /* frags mapped + one for skb head */
1013 for (f = 0; f < nr_frags; f++) {
1014 struct wil_ctx *ctx;
1016 i = (swhead + f) % vring->size;
1017 ctx = &vring->ctx[i];
1018 _d = &vring->va[i].tx;
1020 _d->dma.status = TX_DMA_STATUS_DU;
1021 wil_txdesc_unmap(dev, d, ctx);
1024 dev_kfree_skb_any(ctx->skb);
1026 memset(ctx, 0, sizeof(*ctx));
1032 netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1034 struct wil6210_priv *wil = ndev_to_wil(ndev);
1035 struct ethhdr *eth = (void *)skb->data;
1036 struct vring *vring;
1037 static bool pr_once_fw;
1040 wil_dbg_txrx(wil, "%s()\n", __func__);
1041 if (!test_bit(wil_status_fwready, wil->status)) {
1043 wil_err(wil, "FW not ready\n");
1048 if (!test_bit(wil_status_fwconnected, wil->status)) {
1049 wil_err(wil, "FW not connected\n");
1052 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) {
1053 wil_err(wil, "Xmit in monitor mode not supported\n");
1059 if (is_unicast_ether_addr(eth->h_dest))
1060 vring = wil_find_tx_vring(wil, skb);
1062 vring = wil_tx_bcast(wil, skb);
1064 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest);
1068 /* set up vring entry */
1069 rc = wil_tx_vring(wil, vring, skb);
1071 /* do we still have enough room in the vring? */
1072 if (wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring)) {
1073 netif_tx_stop_all_queues(wil_to_ndev(wil));
1074 wil_dbg_txrx(wil, "netif_tx_stop : ring full\n");
1079 /* statistics will be updated on the tx_complete */
1080 dev_kfree_skb_any(skb);
1081 return NETDEV_TX_OK;
1083 return NETDEV_TX_BUSY;
1085 break; /* goto drop; */
1088 ndev->stats.tx_dropped++;
1089 dev_kfree_skb_any(skb);
1091 return NET_XMIT_DROP;
1095 * Clean up transmitted skb's from the Tx VRING
1097 * Return number of descriptors cleared
1099 * Safe to call from IRQ
1101 int wil_tx_complete(struct wil6210_priv *wil, int ringid)
1103 struct net_device *ndev = wil_to_ndev(wil);
1104 struct device *dev = wil_to_dev(wil);
1105 struct vring *vring = &wil->vring_tx[ringid];
1106 struct vring_tx_data *txdata = &wil->vring_tx_data[ringid];
1108 int cid = wil->vring2cid_tid[ringid][0];
1109 struct wil_net_stats *stats = &wil->sta[cid].stats;
1110 volatile struct vring_tx_desc *_d;
1113 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid);
1117 if (!txdata->enabled) {
1118 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid);
1122 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid);
1124 while (!wil_vring_is_empty(vring)) {
1126 struct wil_ctx *ctx = &vring->ctx[vring->swtail];
1128 * For the fragmented skb, HW will set DU bit only for the
1129 * last fragment. look for it
1131 int lf = (vring->swtail + ctx->nr_frags) % vring->size;
1132 /* TODO: check we are not past head */
1134 _d = &vring->va[lf].tx;
1135 if (!(_d->dma.status & TX_DMA_STATUS_DU))
1138 new_swtail = (lf + 1) % vring->size;
1139 while (vring->swtail != new_swtail) {
1140 struct vring_tx_desc dd, *d = ⅆ
1142 struct sk_buff *skb;
1144 ctx = &vring->ctx[vring->swtail];
1146 _d = &vring->va[vring->swtail].tx;
1150 dmalen = le16_to_cpu(d->dma.length);
1151 trace_wil6210_tx_done(ringid, vring->swtail, dmalen,
1154 "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n",
1155 vring->swtail, dmalen, d->dma.status,
1157 wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4,
1158 (const void *)d, sizeof(*d), false);
1160 wil_txdesc_unmap(dev, d, ctx);
1163 if (d->dma.error == 0) {
1164 ndev->stats.tx_packets++;
1165 stats->tx_packets++;
1166 ndev->stats.tx_bytes += skb->len;
1167 stats->tx_bytes += skb->len;
1169 ndev->stats.tx_errors++;
1173 dev_kfree_skb_any(skb);
1175 memset(ctx, 0, sizeof(*ctx));
1176 /* There is no need to touch HW descriptor:
1177 * - ststus bit TX_DMA_STATUS_DU is set by design,
1178 * so hardware will not try to process this desc.,
1179 * - rest of descriptor will be initialized on Tx.
1181 vring->swtail = wil_vring_next_tail(vring);
1186 if (wil_vring_is_empty(vring)) { /* performance monitoring */
1187 wil_dbg_txrx(wil, "Ring[%2d] empty\n", ringid);
1188 txdata->last_idle = get_cycles();
1191 if (wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring)) {
1192 wil_dbg_txrx(wil, "netif_tx_wake : ring not full\n");
1193 netif_tx_wake_all_queues(wil_to_ndev(wil));