1 /* 2 * Copyright (c) 2012-2014 Qualcomm Atheros, Inc. 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <linux/etherdevice.h> 18 #include <net/ieee80211_radiotap.h> 19 #include <linux/if_arp.h> 20 #include <linux/moduleparam.h> 21 #include <linux/ip.h> 22 #include <linux/ipv6.h> 23 #include <net/ipv6.h> 24 #include <linux/prefetch.h> 25 26 #include "wil6210.h" 27 #include "wmi.h" 28 #include "txrx.h" 29 #include "trace.h" 30 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"); 35 36 static inline int wil_vring_is_empty(struct vring *vring) 37 { 38 return vring->swhead == vring->swtail; 39 } 40 41 static inline u32 wil_vring_next_tail(struct vring *vring) 42 { 43 return (vring->swtail + 1) % vring->size; 44 } 45 46 static inline void wil_vring_advance_head(struct vring *vring, int n) 47 { 48 vring->swhead = (vring->swhead + n) % vring->size; 49 } 50 51 static inline int wil_vring_is_full(struct vring *vring) 52 { 53 return wil_vring_next_tail(vring) == vring->swhead; 54 } 55 56 /* 57 * Available space in Tx Vring 58 */ 59 static inline int wil_vring_avail_tx(struct vring *vring) 60 { 61 u32 swhead = vring->swhead; 62 u32 swtail = vring->swtail; 63 int used = (vring->size + swhead - swtail) % vring->size; 64 65 return vring->size - used - 1; 66 } 67 68 /** 69 * wil_vring_wmark_low - low watermark for available descriptor space 70 */ 71 static inline int wil_vring_wmark_low(struct vring *vring) 72 { 73 return vring->size/8; 74 } 75 76 /** 77 * wil_vring_wmark_high - high watermark for available descriptor space 78 */ 79 static inline int wil_vring_wmark_high(struct vring *vring) 80 { 81 return vring->size/4; 82 } 83 84 static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring) 85 { 86 struct device *dev = wil_to_dev(wil); 87 size_t sz = vring->size * sizeof(vring->va[0]); 88 uint i; 89 90 wil_dbg_misc(wil, "%s()\n", __func__); 91 92 BUILD_BUG_ON(sizeof(vring->va[0]) != 32); 93 94 vring->swhead = 0; 95 vring->swtail = 0; 96 vring->ctx = kcalloc(vring->size, sizeof(vring->ctx[0]), GFP_KERNEL); 97 if (!vring->ctx) { 98 vring->va = NULL; 99 return -ENOMEM; 100 } 101 /* 102 * vring->va should be aligned on its size rounded up to power of 2 103 * This is granted by the dma_alloc_coherent 104 */ 105 vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL); 106 if (!vring->va) { 107 kfree(vring->ctx); 108 vring->ctx = NULL; 109 return -ENOMEM; 110 } 111 /* initially, all descriptors are SW owned 112 * For Tx and Rx, ownership bit is at the same location, thus 113 * we can use any 114 */ 115 for (i = 0; i < vring->size; i++) { 116 volatile struct vring_tx_desc *_d = &vring->va[i].tx; 117 118 _d->dma.status = TX_DMA_STATUS_DU; 119 } 120 121 wil_dbg_misc(wil, "vring[%d] 0x%p:%pad 0x%p\n", vring->size, 122 vring->va, &vring->pa, vring->ctx); 123 124 return 0; 125 } 126 127 static void wil_txdesc_unmap(struct device *dev, struct vring_tx_desc *d, 128 struct wil_ctx *ctx) 129 { 130 dma_addr_t pa = wil_desc_addr(&d->dma.addr); 131 u16 dmalen = le16_to_cpu(d->dma.length); 132 133 switch (ctx->mapped_as) { 134 case wil_mapped_as_single: 135 dma_unmap_single(dev, pa, dmalen, DMA_TO_DEVICE); 136 break; 137 case wil_mapped_as_page: 138 dma_unmap_page(dev, pa, dmalen, DMA_TO_DEVICE); 139 break; 140 default: 141 break; 142 } 143 } 144 145 static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring, 146 int tx) 147 { 148 struct device *dev = wil_to_dev(wil); 149 size_t sz = vring->size * sizeof(vring->va[0]); 150 151 if (tx) { 152 int vring_index = vring - wil->vring_tx; 153 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); 157 } else { 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); 161 } 162 163 while (!wil_vring_is_empty(vring)) { 164 dma_addr_t pa; 165 u16 dmalen; 166 struct wil_ctx *ctx; 167 168 if (tx) { 169 struct vring_tx_desc dd, *d = ⅆ 170 volatile struct vring_tx_desc *_d = 171 &vring->va[vring->swtail].tx; 172 173 ctx = &vring->ctx[vring->swtail]; 174 *d = *_d; 175 wil_txdesc_unmap(dev, d, ctx); 176 if (ctx->skb) 177 dev_kfree_skb_any(ctx->skb); 178 vring->swtail = wil_vring_next_tail(vring); 179 } else { /* rx */ 180 struct vring_rx_desc dd, *d = ⅆ 181 volatile struct vring_rx_desc *_d = 182 &vring->va[vring->swhead].rx; 183 184 ctx = &vring->ctx[vring->swhead]; 185 *d = *_d; 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); 189 kfree_skb(ctx->skb); 190 wil_vring_advance_head(vring, 1); 191 } 192 } 193 dma_free_coherent(dev, sz, (void *)vring->va, vring->pa); 194 kfree(vring->ctx); 195 vring->pa = 0; 196 vring->va = NULL; 197 vring->ctx = NULL; 198 } 199 200 /** 201 * Allocate one skb for Rx VRING 202 * 203 * Safe to call from IRQ 204 */ 205 static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring, 206 u32 i, int headroom) 207 { 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; 212 dma_addr_t pa; 213 struct sk_buff *skb = dev_alloc_skb(sz + headroom); 214 215 if (unlikely(!skb)) 216 return -ENOMEM; 217 218 skb_reserve(skb, headroom); 219 skb_put(skb, sz); 220 221 pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE); 222 if (unlikely(dma_mapping_error(dev, pa))) { 223 kfree_skb(skb); 224 return -ENOMEM; 225 } 226 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 */ 230 /* b11 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); 234 *_d = *d; 235 vring->ctx[i].skb = skb; 236 237 return 0; 238 } 239 240 /** 241 * Adds radiotap header 242 * 243 * Any error indicated as "Bad FCS" 244 * 245 * Vendor data for 04:ce:14-1 (Wilocity-1) consists of: 246 * - Rx descriptor: 32 bytes 247 * - Phy info 248 */ 249 static void wil_rx_add_radiotap_header(struct wil6210_priv *wil, 250 struct sk_buff *skb) 251 { 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 */ 256 /* flags */ 257 u8 flags; 258 /* channel */ 259 __le16 chnl_freq __aligned(2); 260 __le16 chnl_flags; 261 /* MCS */ 262 u8 mcs_present; 263 u8 mcs_flags; 264 u8 mcs_index; 265 } __packed; 266 struct wil6210_rtap_vendor { 267 struct wil6210_rtap rtap; 268 /* vendor */ 269 u8 vendor_oui[3] __aligned(2); 270 u8 vendor_ns; 271 __le16 vendor_skip; 272 u8 vendor_data[0]; 273 } __packed; 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; 280 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) { 285 /** 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. 290 */ 291 int len = min_t(int, 8 + sizeof(phy_data), 292 wil_rxdesc_phy_length(d)); 293 294 if (len > 8) { 295 void *p = skb_tail_pointer(skb); 296 void *pa = PTR_ALIGN(p, 8); 297 298 if (skb_tailroom(skb) >= len + (pa - p)) { 299 phy_length = len - 8; 300 memcpy(phy_data, pa, phy_length); 301 } 302 } 303 } 304 rtap_len += phy_length; 305 } 306 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); 310 return; 311 } 312 313 rtap_vendor = (void *)skb_push(skb, rtap_len); 314 memset(rtap_vendor, 0, rtap_len); 315 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; 324 325 rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320); 326 rtap_vendor->rtap.chnl_flags = cpu_to_le16(0); 327 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); 331 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) + 342 phy_length); 343 memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d)); 344 memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data, 345 phy_length); 346 } 347 } 348 349 /* 350 * Fast swap in place between 2 registers 351 */ 352 static void wil_swap_u16(u16 *a, u16 *b) 353 { 354 *a ^= *b; 355 *b ^= *a; 356 *a ^= *b; 357 } 358 359 static void wil_swap_ethaddr(void *data) 360 { 361 struct ethhdr *eth = data; 362 u16 *s = (u16 *)eth->h_source; 363 u16 *d = (u16 *)eth->h_dest; 364 365 wil_swap_u16(s++, d++); 366 wil_swap_u16(s++, d++); 367 wil_swap_u16(s, d); 368 } 369 370 /** 371 * reap 1 frame from @swhead 372 * 373 * Rx descriptor copied to skb->cb 374 * 375 * Safe to call from IRQ 376 */ 377 static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil, 378 struct vring *vring) 379 { 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; 384 struct sk_buff *skb; 385 dma_addr_t pa; 386 unsigned int sz = mtu_max + ETH_HLEN; 387 u16 dmalen; 388 u8 ftype; 389 u8 ds_bits; 390 int cid; 391 struct wil_net_stats *stats; 392 393 BUILD_BUG_ON(sizeof(struct vring_rx_desc) > sizeof(skb->cb)); 394 395 if (wil_vring_is_empty(vring)) 396 return NULL; 397 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 */ 401 return NULL; 402 } 403 404 skb = vring->ctx[vring->swhead].skb; 405 d = wil_skb_rxdesc(skb); 406 *d = *_d; 407 pa = wil_desc_addr(&d->dma.addr); 408 vring->ctx[vring->swhead].skb = NULL; 409 wil_vring_advance_head(vring, 1); 410 411 dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE); 412 dmalen = le16_to_cpu(d->dma.length); 413 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); 418 419 if (dmalen > sz) { 420 wil_err(wil, "Rx size too large: %d bytes!\n", dmalen); 421 kfree_skb(skb); 422 return NULL; 423 } 424 skb_trim(skb, dmalen); 425 426 prefetch(skb->data); 427 428 wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1, 429 skb->data, skb_headlen(skb), false); 430 431 cid = wil_rxdesc_cid(d); 432 stats = &wil->sta[cid].stats; 433 stats->last_mcs_rx = wil_rxdesc_mcs(d); 434 435 /* use radiotap header only if required */ 436 if (ndev->type == ARPHRD_IEEE80211_RADIOTAP) 437 wil_rx_add_radiotap_header(wil, skb); 438 439 /* no extra checks if in sniffer mode */ 440 if (ndev->type != ARPHRD_ETHER) 441 return skb; 442 /* 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 446 */ 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 */ 451 kfree_skb(skb); 452 return NULL; 453 } 454 455 if (skb->len < ETH_HLEN) { 456 wil_err(wil, "Short frame, len = %d\n", skb->len); 457 /* TODO: process it (i.e. BAR) */ 458 kfree_skb(skb); 459 return NULL; 460 } 461 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) 465 */ 466 if (d->dma.status & RX_DMA_STATUS_L4I) { 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 474 */ 475 } 476 477 ds_bits = wil_rxdesc_ds_bits(d); 478 if (ds_bits == 1) { 479 /* 480 * HW bug - in ToDS mode, i.e. Rx on AP side, 481 * addresses get swapped 482 */ 483 wil_swap_ethaddr(skb->data); 484 } 485 486 return skb; 487 } 488 489 /** 490 * allocate and fill up to @count buffers in rx ring 491 * buffers posted at @swtail 492 */ 493 static int wil_rx_refill(struct wil6210_priv *wil, int count) 494 { 495 struct net_device *ndev = wil_to_ndev(wil); 496 struct vring *v = &wil->vring_rx; 497 u32 next_tail; 498 int rc = 0; 499 int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ? 500 WIL6210_RTAP_SIZE : 0; 501 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); 506 if (rc) { 507 wil_err(wil, "Error %d in wil_rx_refill[%d]\n", 508 rc, v->swtail); 509 break; 510 } 511 } 512 iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail)); 513 514 return rc; 515 } 516 517 /* 518 * Pass Rx packet to the netif. Update statistics. 519 * Called in softirq context (NAPI poll). 520 */ 521 void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev) 522 { 523 gro_result_t rc; 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; 529 530 skb_orphan(skb); 531 532 rc = napi_gro_receive(&wil->napi_rx, skb); 533 534 if (unlikely(rc == GRO_DROP)) { 535 ndev->stats.rx_dropped++; 536 stats->rx_dropped++; 537 wil_dbg_txrx(wil, "Rx drop %d bytes\n", len); 538 } else { 539 ndev->stats.rx_packets++; 540 stats->rx_packets++; 541 ndev->stats.rx_bytes += len; 542 stats->rx_bytes += len; 543 } 544 { 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", 551 }; 552 wil_dbg_txrx(wil, "Rx complete %d bytes => %s\n", 553 len, gro_res_str[rc]); 554 } 555 } 556 557 /** 558 * Proceed all completed skb's from Rx VRING 559 * 560 * Safe to call from NAPI poll, i.e. softirq with interrupts enabled 561 */ 562 void wil_rx_handle(struct wil6210_priv *wil, int *quota) 563 { 564 struct net_device *ndev = wil_to_ndev(wil); 565 struct vring *v = &wil->vring_rx; 566 struct sk_buff *skb; 567 568 if (!v->va) { 569 wil_err(wil, "Rx IRQ while Rx not yet initialized\n"); 570 return; 571 } 572 wil_dbg_txrx(wil, "%s()\n", __func__); 573 while ((*quota > 0) && (NULL != (skb = wil_vring_reap_rx(wil, v)))) { 574 (*quota)--; 575 576 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { 577 skb->dev = ndev; 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); 583 } else { 584 skb->protocol = eth_type_trans(skb, ndev); 585 wil_rx_reorder(wil, skb); 586 } 587 } 588 wil_rx_refill(wil, v->size); 589 } 590 591 int wil_rx_init(struct wil6210_priv *wil, u16 size) 592 { 593 struct vring *vring = &wil->vring_rx; 594 int rc; 595 596 wil_dbg_misc(wil, "%s()\n", __func__); 597 598 if (vring->va) { 599 wil_err(wil, "Rx ring already allocated\n"); 600 return -EINVAL; 601 } 602 603 vring->size = size; 604 rc = wil_vring_alloc(wil, vring); 605 if (rc) 606 return rc; 607 608 rc = wmi_rx_chain_add(wil, vring); 609 if (rc) 610 goto err_free; 611 612 rc = wil_rx_refill(wil, vring->size); 613 if (rc) 614 goto err_free; 615 616 return 0; 617 err_free: 618 wil_vring_free(wil, vring, 0); 619 620 return rc; 621 } 622 623 void wil_rx_fini(struct wil6210_priv *wil) 624 { 625 struct vring *vring = &wil->vring_rx; 626 627 wil_dbg_misc(wil, "%s()\n", __func__); 628 629 if (vring->va) 630 wil_vring_free(wil, vring, 0); 631 } 632 633 int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size, 634 int cid, int tid) 635 { 636 int rc; 637 struct wmi_vring_cfg_cmd cmd = { 638 .action = cpu_to_le32(WMI_VRING_CMD_ADD), 639 .vring_cfg = { 640 .tx_sw_ring = { 641 .max_mpdu_size = 642 cpu_to_le16(wil_mtu2macbuf(mtu_max)), 643 .ring_size = cpu_to_le16(size), 644 }, 645 .ringid = id, 646 .cidxtid = mk_cidxtid(cid, tid), 647 .encap_trans_type = WMI_VRING_ENC_TYPE_802_3, 648 .mac_ctrl = 0, 649 .to_resolution = 0, 650 .agg_max_wsize = 0, 651 .schd_params = { 652 .priority = cpu_to_le16(0), 653 .timeslot_us = cpu_to_le16(0xfff), 654 }, 655 }, 656 }; 657 struct { 658 struct wil6210_mbox_hdr_wmi wmi; 659 struct wmi_vring_cfg_done_event cmd; 660 } __packed reply; 661 struct vring *vring = &wil->vring_tx[id]; 662 struct vring_tx_data *txdata = &wil->vring_tx_data[id]; 663 664 wil_dbg_misc(wil, "%s() max_mpdu_size %d\n", __func__, 665 cmd.vring_cfg.tx_sw_ring.max_mpdu_size); 666 667 if (vring->va) { 668 wil_err(wil, "Tx ring [%d] already allocated\n", id); 669 rc = -EINVAL; 670 goto out; 671 } 672 673 memset(txdata, 0, sizeof(*txdata)); 674 spin_lock_init(&txdata->lock); 675 vring->size = size; 676 rc = wil_vring_alloc(wil, vring); 677 if (rc) 678 goto out; 679 680 wil->vring2cid_tid[id][0] = cid; 681 wil->vring2cid_tid[id][1] = tid; 682 683 cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa); 684 685 rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd), 686 WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100); 687 if (rc) 688 goto out_free; 689 690 if (reply.cmd.status != WMI_FW_STATUS_SUCCESS) { 691 wil_err(wil, "Tx config failed, status 0x%02x\n", 692 reply.cmd.status); 693 rc = -EINVAL; 694 goto out_free; 695 } 696 vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr); 697 698 txdata->enabled = 1; 699 if (wil->sta[cid].data_port_open && (agg_wsize >= 0)) 700 wil_addba_tx_request(wil, id, agg_wsize); 701 702 return 0; 703 out_free: 704 wil_vring_free(wil, vring, 1); 705 out: 706 707 return rc; 708 } 709 710 void wil_vring_fini_tx(struct wil6210_priv *wil, int id) 711 { 712 struct vring *vring = &wil->vring_tx[id]; 713 struct vring_tx_data *txdata = &wil->vring_tx_data[id]; 714 715 WARN_ON(!mutex_is_locked(&wil->mutex)); 716 717 if (!vring->va) 718 return; 719 720 wil_dbg_misc(wil, "%s() id=%d\n", __func__, id); 721 722 spin_lock_bh(&txdata->lock); 723 txdata->enabled = 0; /* no Tx can be in progress or start anew */ 724 spin_unlock_bh(&txdata->lock); 725 /* make sure NAPI won't touch this vring */ 726 if (test_bit(wil_status_napi_en, wil->status)) 727 napi_synchronize(&wil->napi_tx); 728 729 wil_vring_free(wil, vring, 1); 730 memset(txdata, 0, sizeof(*txdata)); 731 } 732 733 static struct vring *wil_find_tx_vring(struct wil6210_priv *wil, 734 struct sk_buff *skb) 735 { 736 int i; 737 struct ethhdr *eth = (void *)skb->data; 738 int cid = wil_find_cid(wil, eth->h_dest); 739 740 if (cid < 0) 741 return NULL; 742 743 if (!wil->sta[cid].data_port_open && 744 (skb->protocol != cpu_to_be16(ETH_P_PAE))) 745 return NULL; 746 747 /* TODO: fix for multiple TID */ 748 for (i = 0; i < ARRAY_SIZE(wil->vring2cid_tid); i++) { 749 if (wil->vring2cid_tid[i][0] == cid) { 750 struct vring *v = &wil->vring_tx[i]; 751 752 wil_dbg_txrx(wil, "%s(%pM) -> [%d]\n", 753 __func__, eth->h_dest, i); 754 if (v->va) { 755 return v; 756 } else { 757 wil_dbg_txrx(wil, "vring[%d] not valid\n", i); 758 return NULL; 759 } 760 } 761 } 762 763 return NULL; 764 } 765 766 static void wil_set_da_for_vring(struct wil6210_priv *wil, 767 struct sk_buff *skb, int vring_index) 768 { 769 struct ethhdr *eth = (void *)skb->data; 770 int cid = wil->vring2cid_tid[vring_index][0]; 771 772 memcpy(eth->h_dest, wil->sta[cid].addr, ETH_ALEN); 773 } 774 775 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring, 776 struct sk_buff *skb); 777 778 static struct vring *wil_find_tx_vring_sta(struct wil6210_priv *wil, 779 struct sk_buff *skb) 780 { 781 struct vring *v; 782 int i; 783 u8 cid; 784 785 /* In the STA mode, it is expected to have only 1 VRING 786 * for the AP we connected to. 787 * find 1-st vring and see whether it is eligible for data 788 */ 789 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) { 790 v = &wil->vring_tx[i]; 791 if (!v->va) 792 continue; 793 794 cid = wil->vring2cid_tid[i][0]; 795 if (!wil->sta[cid].data_port_open && 796 (skb->protocol != cpu_to_be16(ETH_P_PAE))) 797 break; 798 799 wil_dbg_txrx(wil, "Tx -> ring %d\n", i); 800 801 return v; 802 } 803 804 wil_dbg_txrx(wil, "Tx while no vrings active?\n"); 805 806 return NULL; 807 } 808 809 /* 810 * Find 1-st vring and return it; set dest address for this vring in skb 811 * duplicate skb and send it to other active vrings 812 */ 813 static struct vring *wil_tx_bcast(struct wil6210_priv *wil, 814 struct sk_buff *skb) 815 { 816 struct vring *v, *v2; 817 struct sk_buff *skb2; 818 int i; 819 u8 cid; 820 821 /* find 1-st vring eligible for data */ 822 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) { 823 v = &wil->vring_tx[i]; 824 if (!v->va) 825 continue; 826 827 cid = wil->vring2cid_tid[i][0]; 828 if (!wil->sta[cid].data_port_open) 829 continue; 830 831 goto found; 832 } 833 834 wil_dbg_txrx(wil, "Tx while no vrings active?\n"); 835 836 return NULL; 837 838 found: 839 wil_dbg_txrx(wil, "BCAST -> ring %d\n", i); 840 wil_set_da_for_vring(wil, skb, i); 841 842 /* find other active vrings and duplicate skb for each */ 843 for (i++; i < WIL6210_MAX_TX_RINGS; i++) { 844 v2 = &wil->vring_tx[i]; 845 if (!v2->va) 846 continue; 847 cid = wil->vring2cid_tid[i][0]; 848 if (!wil->sta[cid].data_port_open) 849 continue; 850 851 skb2 = skb_copy(skb, GFP_ATOMIC); 852 if (skb2) { 853 wil_dbg_txrx(wil, "BCAST DUP -> ring %d\n", i); 854 wil_set_da_for_vring(wil, skb2, i); 855 wil_tx_vring(wil, v2, skb2); 856 } else { 857 wil_err(wil, "skb_copy failed\n"); 858 } 859 } 860 861 return v; 862 } 863 864 static int wil_tx_desc_map(struct vring_tx_desc *d, dma_addr_t pa, u32 len, 865 int vring_index) 866 { 867 wil_desc_addr_set(&d->dma.addr, pa); 868 d->dma.ip_length = 0; 869 /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/ 870 d->dma.b11 = 0/*14 | BIT(7)*/; 871 d->dma.error = 0; 872 d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */ 873 d->dma.length = cpu_to_le16((u16)len); 874 d->dma.d0 = (vring_index << DMA_CFG_DESC_TX_0_QID_POS); 875 d->mac.d[0] = 0; 876 d->mac.d[1] = 0; 877 d->mac.d[2] = 0; 878 d->mac.ucode_cmd = 0; 879 /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */ 880 d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) | 881 (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS); 882 883 return 0; 884 } 885 886 static inline 887 void wil_tx_desc_set_nr_frags(struct vring_tx_desc *d, int nr_frags) 888 { 889 d->mac.d[2] |= ((nr_frags + 1) << 890 MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS); 891 } 892 893 static int wil_tx_desc_offload_cksum_set(struct wil6210_priv *wil, 894 struct vring_tx_desc *d, 895 struct sk_buff *skb) 896 { 897 int protocol; 898 899 if (skb->ip_summed != CHECKSUM_PARTIAL) 900 return 0; 901 902 d->dma.b11 = ETH_HLEN; /* MAC header length */ 903 904 switch (skb->protocol) { 905 case cpu_to_be16(ETH_P_IP): 906 protocol = ip_hdr(skb)->protocol; 907 d->dma.b11 |= BIT(DMA_CFG_DESC_TX_OFFLOAD_CFG_L3T_IPV4_POS); 908 break; 909 case cpu_to_be16(ETH_P_IPV6): 910 protocol = ipv6_hdr(skb)->nexthdr; 911 break; 912 default: 913 return -EINVAL; 914 } 915 916 switch (protocol) { 917 case IPPROTO_TCP: 918 d->dma.d0 |= (2 << DMA_CFG_DESC_TX_0_L4_TYPE_POS); 919 /* L4 header len: TCP header length */ 920 d->dma.d0 |= 921 (tcp_hdrlen(skb) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK); 922 break; 923 case IPPROTO_UDP: 924 /* L4 header len: UDP header length */ 925 d->dma.d0 |= 926 (sizeof(struct udphdr) & DMA_CFG_DESC_TX_0_L4_LENGTH_MSK); 927 break; 928 default: 929 return -EINVAL; 930 } 931 932 d->dma.ip_length = skb_network_header_len(skb); 933 /* Enable TCP/UDP checksum */ 934 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_TCP_UDP_CHECKSUM_EN_POS); 935 /* Calculate pseudo-header */ 936 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_PSEUDO_HEADER_CALC_EN_POS); 937 938 return 0; 939 } 940 941 static int __wil_tx_vring(struct wil6210_priv *wil, struct vring *vring, 942 struct sk_buff *skb) 943 { 944 struct device *dev = wil_to_dev(wil); 945 struct vring_tx_desc dd, *d = ⅆ 946 volatile struct vring_tx_desc *_d; 947 u32 swhead = vring->swhead; 948 int avail = wil_vring_avail_tx(vring); 949 int nr_frags = skb_shinfo(skb)->nr_frags; 950 uint f = 0; 951 int vring_index = vring - wil->vring_tx; 952 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index]; 953 uint i = swhead; 954 dma_addr_t pa; 955 956 wil_dbg_txrx(wil, "%s()\n", __func__); 957 958 if (unlikely(!txdata->enabled)) 959 return -EINVAL; 960 961 if (avail < 1 + nr_frags) { 962 wil_err_ratelimited(wil, 963 "Tx ring[%2d] full. No space for %d fragments\n", 964 vring_index, 1 + nr_frags); 965 return -ENOMEM; 966 } 967 _d = &vring->va[i].tx; 968 969 pa = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE); 970 971 wil_dbg_txrx(wil, "Tx[%2d] skb %d bytes 0x%p -> %pad\n", vring_index, 972 skb_headlen(skb), skb->data, &pa); 973 wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1, 974 skb->data, skb_headlen(skb), false); 975 976 if (unlikely(dma_mapping_error(dev, pa))) 977 return -EINVAL; 978 vring->ctx[i].mapped_as = wil_mapped_as_single; 979 /* 1-st segment */ 980 wil_tx_desc_map(d, pa, skb_headlen(skb), vring_index); 981 /* Process TCP/UDP checksum offloading */ 982 if (wil_tx_desc_offload_cksum_set(wil, d, skb)) { 983 wil_err(wil, "Tx[%2d] Failed to set cksum, drop packet\n", 984 vring_index); 985 goto dma_error; 986 } 987 988 vring->ctx[i].nr_frags = nr_frags; 989 wil_tx_desc_set_nr_frags(d, nr_frags); 990 991 /* middle segments */ 992 for (; f < nr_frags; f++) { 993 const struct skb_frag_struct *frag = 994 &skb_shinfo(skb)->frags[f]; 995 int len = skb_frag_size(frag); 996 997 *_d = *d; 998 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i); 999 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4, 1000 (const void *)d, sizeof(*d), false); 1001 i = (swhead + f + 1) % vring->size; 1002 _d = &vring->va[i].tx; 1003 pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), 1004 DMA_TO_DEVICE); 1005 if (unlikely(dma_mapping_error(dev, pa))) 1006 goto dma_error; 1007 vring->ctx[i].mapped_as = wil_mapped_as_page; 1008 wil_tx_desc_map(d, pa, len, vring_index); 1009 /* no need to check return code - 1010 * if it succeeded for 1-st descriptor, 1011 * it will succeed here too 1012 */ 1013 wil_tx_desc_offload_cksum_set(wil, d, skb); 1014 } 1015 /* for the last seg only */ 1016 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS); 1017 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_MARK_WB_POS); 1018 d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS); 1019 *_d = *d; 1020 wil_dbg_txrx(wil, "Tx[%2d] desc[%4d]\n", vring_index, i); 1021 wil_hex_dump_txrx("TxD ", DUMP_PREFIX_NONE, 32, 4, 1022 (const void *)d, sizeof(*d), false); 1023 1024 /* hold reference to skb 1025 * to prevent skb release before accounting 1026 * in case of immediate "tx done" 1027 */ 1028 vring->ctx[i].skb = skb_get(skb); 1029 1030 if (wil_vring_is_empty(vring)) /* performance monitoring */ 1031 txdata->idle += get_cycles() - txdata->last_idle; 1032 1033 /* advance swhead */ 1034 wil_vring_advance_head(vring, nr_frags + 1); 1035 wil_dbg_txrx(wil, "Tx[%2d] swhead %d -> %d\n", vring_index, swhead, 1036 vring->swhead); 1037 trace_wil6210_tx(vring_index, swhead, skb->len, nr_frags); 1038 iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail)); 1039 1040 return 0; 1041 dma_error: 1042 /* unmap what we have mapped */ 1043 nr_frags = f + 1; /* frags mapped + one for skb head */ 1044 for (f = 0; f < nr_frags; f++) { 1045 struct wil_ctx *ctx; 1046 1047 i = (swhead + f) % vring->size; 1048 ctx = &vring->ctx[i]; 1049 _d = &vring->va[i].tx; 1050 *d = *_d; 1051 _d->dma.status = TX_DMA_STATUS_DU; 1052 wil_txdesc_unmap(dev, d, ctx); 1053 1054 if (ctx->skb) 1055 dev_kfree_skb_any(ctx->skb); 1056 1057 memset(ctx, 0, sizeof(*ctx)); 1058 } 1059 1060 return -EINVAL; 1061 } 1062 1063 static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring, 1064 struct sk_buff *skb) 1065 { 1066 int vring_index = vring - wil->vring_tx; 1067 struct vring_tx_data *txdata = &wil->vring_tx_data[vring_index]; 1068 int rc; 1069 1070 spin_lock(&txdata->lock); 1071 rc = __wil_tx_vring(wil, vring, skb); 1072 spin_unlock(&txdata->lock); 1073 return rc; 1074 } 1075 1076 netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev) 1077 { 1078 struct wil6210_priv *wil = ndev_to_wil(ndev); 1079 struct ethhdr *eth = (void *)skb->data; 1080 struct vring *vring; 1081 static bool pr_once_fw; 1082 int rc; 1083 1084 wil_dbg_txrx(wil, "%s()\n", __func__); 1085 if (!test_bit(wil_status_fwready, wil->status)) { 1086 if (!pr_once_fw) { 1087 wil_err(wil, "FW not ready\n"); 1088 pr_once_fw = true; 1089 } 1090 goto drop; 1091 } 1092 if (!test_bit(wil_status_fwconnected, wil->status)) { 1093 wil_err(wil, "FW not connected\n"); 1094 goto drop; 1095 } 1096 if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { 1097 wil_err(wil, "Xmit in monitor mode not supported\n"); 1098 goto drop; 1099 } 1100 pr_once_fw = false; 1101 1102 /* find vring */ 1103 if (wil->wdev->iftype == NL80211_IFTYPE_STATION) { 1104 /* in STA mode (ESS), all to same VRING */ 1105 vring = wil_find_tx_vring_sta(wil, skb); 1106 } else { /* direct communication, find matching VRING */ 1107 if (is_unicast_ether_addr(eth->h_dest)) 1108 vring = wil_find_tx_vring(wil, skb); 1109 else 1110 vring = wil_tx_bcast(wil, skb); 1111 } 1112 if (!vring) { 1113 wil_dbg_txrx(wil, "No Tx VRING found for %pM\n", eth->h_dest); 1114 goto drop; 1115 } 1116 /* set up vring entry */ 1117 rc = wil_tx_vring(wil, vring, skb); 1118 1119 /* do we still have enough room in the vring? */ 1120 if (wil_vring_avail_tx(vring) < wil_vring_wmark_low(vring)) { 1121 netif_tx_stop_all_queues(wil_to_ndev(wil)); 1122 wil_dbg_txrx(wil, "netif_tx_stop : ring full\n"); 1123 } 1124 1125 switch (rc) { 1126 case 0: 1127 /* statistics will be updated on the tx_complete */ 1128 dev_kfree_skb_any(skb); 1129 return NETDEV_TX_OK; 1130 case -ENOMEM: 1131 return NETDEV_TX_BUSY; 1132 default: 1133 break; /* goto drop; */ 1134 } 1135 drop: 1136 ndev->stats.tx_dropped++; 1137 dev_kfree_skb_any(skb); 1138 1139 return NET_XMIT_DROP; 1140 } 1141 1142 static inline bool wil_need_txstat(struct sk_buff *skb) 1143 { 1144 struct ethhdr *eth = (void *)skb->data; 1145 1146 return is_unicast_ether_addr(eth->h_dest) && skb->sk && 1147 (skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS); 1148 } 1149 1150 static inline void wil_consume_skb(struct sk_buff *skb, bool acked) 1151 { 1152 if (unlikely(wil_need_txstat(skb))) 1153 skb_complete_wifi_ack(skb, acked); 1154 else 1155 acked ? dev_consume_skb_any(skb) : dev_kfree_skb_any(skb); 1156 } 1157 1158 /** 1159 * Clean up transmitted skb's from the Tx VRING 1160 * 1161 * Return number of descriptors cleared 1162 * 1163 * Safe to call from IRQ 1164 */ 1165 int wil_tx_complete(struct wil6210_priv *wil, int ringid) 1166 { 1167 struct net_device *ndev = wil_to_ndev(wil); 1168 struct device *dev = wil_to_dev(wil); 1169 struct vring *vring = &wil->vring_tx[ringid]; 1170 struct vring_tx_data *txdata = &wil->vring_tx_data[ringid]; 1171 int done = 0; 1172 int cid = wil->vring2cid_tid[ringid][0]; 1173 struct wil_net_stats *stats = &wil->sta[cid].stats; 1174 volatile struct vring_tx_desc *_d; 1175 1176 if (!vring->va) { 1177 wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid); 1178 return 0; 1179 } 1180 1181 if (!txdata->enabled) { 1182 wil_info(wil, "Tx irq[%d]: vring disabled\n", ringid); 1183 return 0; 1184 } 1185 1186 wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid); 1187 1188 while (!wil_vring_is_empty(vring)) { 1189 int new_swtail; 1190 struct wil_ctx *ctx = &vring->ctx[vring->swtail]; 1191 /** 1192 * For the fragmented skb, HW will set DU bit only for the 1193 * last fragment. look for it 1194 */ 1195 int lf = (vring->swtail + ctx->nr_frags) % vring->size; 1196 /* TODO: check we are not past head */ 1197 1198 _d = &vring->va[lf].tx; 1199 if (!(_d->dma.status & TX_DMA_STATUS_DU)) 1200 break; 1201 1202 new_swtail = (lf + 1) % vring->size; 1203 while (vring->swtail != new_swtail) { 1204 struct vring_tx_desc dd, *d = ⅆ 1205 u16 dmalen; 1206 struct sk_buff *skb; 1207 1208 ctx = &vring->ctx[vring->swtail]; 1209 skb = ctx->skb; 1210 _d = &vring->va[vring->swtail].tx; 1211 1212 *d = *_d; 1213 1214 dmalen = le16_to_cpu(d->dma.length); 1215 trace_wil6210_tx_done(ringid, vring->swtail, dmalen, 1216 d->dma.error); 1217 wil_dbg_txrx(wil, 1218 "TxC[%2d][%3d] : %d bytes, status 0x%02x err 0x%02x\n", 1219 ringid, vring->swtail, dmalen, 1220 d->dma.status, d->dma.error); 1221 wil_hex_dump_txrx("TxCD ", DUMP_PREFIX_NONE, 32, 4, 1222 (const void *)d, sizeof(*d), false); 1223 1224 wil_txdesc_unmap(dev, d, ctx); 1225 1226 if (skb) { 1227 if (d->dma.error == 0) { 1228 ndev->stats.tx_packets++; 1229 stats->tx_packets++; 1230 ndev->stats.tx_bytes += skb->len; 1231 stats->tx_bytes += skb->len; 1232 } else { 1233 ndev->stats.tx_errors++; 1234 stats->tx_errors++; 1235 } 1236 wil_consume_skb(skb, d->dma.error == 0); 1237 } 1238 memset(ctx, 0, sizeof(*ctx)); 1239 /* There is no need to touch HW descriptor: 1240 * - ststus bit TX_DMA_STATUS_DU is set by design, 1241 * so hardware will not try to process this desc., 1242 * - rest of descriptor will be initialized on Tx. 1243 */ 1244 vring->swtail = wil_vring_next_tail(vring); 1245 done++; 1246 } 1247 } 1248 1249 if (wil_vring_is_empty(vring)) { /* performance monitoring */ 1250 wil_dbg_txrx(wil, "Ring[%2d] empty\n", ringid); 1251 txdata->last_idle = get_cycles(); 1252 } 1253 1254 if (wil_vring_avail_tx(vring) > wil_vring_wmark_high(vring)) { 1255 wil_dbg_txrx(wil, "netif_tx_wake : ring not full\n"); 1256 netif_tx_wake_all_queues(wil_to_ndev(wil)); 1257 } 1258 1259 return done; 1260 } 1261