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