Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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/kernel.h> |
| 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/hardirq.h> |
| 21 | #include <net/ieee80211_radiotap.h> |
| 22 | #include <linux/if_arp.h> |
| 23 | #include <linux/moduleparam.h> |
| 24 | |
| 25 | #include "wil6210.h" |
| 26 | #include "wmi.h" |
| 27 | #include "txrx.h" |
| 28 | |
| 29 | static bool rtap_include_phy_info; |
| 30 | module_param(rtap_include_phy_info, bool, S_IRUGO); |
| 31 | MODULE_PARM_DESC(rtap_include_phy_info, |
| 32 | " Include PHY info in the radiotap header, default - no"); |
| 33 | |
| 34 | static inline int wil_vring_is_empty(struct vring *vring) |
| 35 | { |
| 36 | return vring->swhead == vring->swtail; |
| 37 | } |
| 38 | |
| 39 | static inline u32 wil_vring_next_tail(struct vring *vring) |
| 40 | { |
| 41 | return (vring->swtail + 1) % vring->size; |
| 42 | } |
| 43 | |
| 44 | static inline void wil_vring_advance_head(struct vring *vring, int n) |
| 45 | { |
| 46 | vring->swhead = (vring->swhead + n) % vring->size; |
| 47 | } |
| 48 | |
| 49 | static inline int wil_vring_is_full(struct vring *vring) |
| 50 | { |
| 51 | return wil_vring_next_tail(vring) == vring->swhead; |
| 52 | } |
| 53 | /* |
| 54 | * Available space in Tx Vring |
| 55 | */ |
| 56 | static inline int wil_vring_avail_tx(struct vring *vring) |
| 57 | { |
| 58 | u32 swhead = vring->swhead; |
| 59 | u32 swtail = vring->swtail; |
| 60 | int used = (vring->size + swhead - swtail) % vring->size; |
| 61 | |
| 62 | return vring->size - used - 1; |
| 63 | } |
| 64 | |
| 65 | static int wil_vring_alloc(struct wil6210_priv *wil, struct vring *vring) |
| 66 | { |
| 67 | struct device *dev = wil_to_dev(wil); |
| 68 | size_t sz = vring->size * sizeof(vring->va[0]); |
| 69 | uint i; |
| 70 | |
| 71 | BUILD_BUG_ON(sizeof(vring->va[0]) != 32); |
| 72 | |
| 73 | vring->swhead = 0; |
| 74 | vring->swtail = 0; |
| 75 | vring->ctx = kzalloc(vring->size * sizeof(vring->ctx[0]), GFP_KERNEL); |
| 76 | if (!vring->ctx) { |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 77 | vring->va = NULL; |
| 78 | return -ENOMEM; |
| 79 | } |
| 80 | /* |
| 81 | * vring->va should be aligned on its size rounded up to power of 2 |
| 82 | * This is granted by the dma_alloc_coherent |
| 83 | */ |
| 84 | vring->va = dma_alloc_coherent(dev, sz, &vring->pa, GFP_KERNEL); |
| 85 | if (!vring->va) { |
| 86 | wil_err(wil, "vring_alloc [%d] failed to alloc DMA mem\n", |
| 87 | vring->size); |
| 88 | kfree(vring->ctx); |
| 89 | vring->ctx = NULL; |
| 90 | return -ENOMEM; |
| 91 | } |
| 92 | /* initially, all descriptors are SW owned |
| 93 | * For Tx and Rx, ownership bit is at the same location, thus |
| 94 | * we can use any |
| 95 | */ |
| 96 | for (i = 0; i < vring->size; i++) { |
| 97 | volatile struct vring_tx_desc *d = &(vring->va[i].tx); |
| 98 | d->dma.status = TX_DMA_STATUS_DU; |
| 99 | } |
| 100 | |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 101 | wil_dbg_misc(wil, "vring[%d] 0x%p:0x%016llx 0x%p\n", vring->size, |
Vladimir Kondratiev | 2057ebb | 2013-01-28 18:30:58 +0200 | [diff] [blame] | 102 | vring->va, (unsigned long long)vring->pa, vring->ctx); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static void wil_vring_free(struct wil6210_priv *wil, struct vring *vring, |
| 108 | int tx) |
| 109 | { |
| 110 | struct device *dev = wil_to_dev(wil); |
| 111 | size_t sz = vring->size * sizeof(vring->va[0]); |
| 112 | |
| 113 | while (!wil_vring_is_empty(vring)) { |
| 114 | if (tx) { |
| 115 | volatile struct vring_tx_desc *d = |
| 116 | &vring->va[vring->swtail].tx; |
| 117 | dma_addr_t pa = d->dma.addr_low | |
| 118 | ((u64)d->dma.addr_high << 32); |
| 119 | struct sk_buff *skb = vring->ctx[vring->swtail]; |
| 120 | if (skb) { |
| 121 | dma_unmap_single(dev, pa, d->dma.length, |
| 122 | DMA_TO_DEVICE); |
| 123 | dev_kfree_skb_any(skb); |
| 124 | vring->ctx[vring->swtail] = NULL; |
| 125 | } else { |
| 126 | dma_unmap_page(dev, pa, d->dma.length, |
| 127 | DMA_TO_DEVICE); |
| 128 | } |
| 129 | vring->swtail = wil_vring_next_tail(vring); |
| 130 | } else { /* rx */ |
| 131 | volatile struct vring_rx_desc *d = |
| 132 | &vring->va[vring->swtail].rx; |
| 133 | dma_addr_t pa = d->dma.addr_low | |
| 134 | ((u64)d->dma.addr_high << 32); |
| 135 | struct sk_buff *skb = vring->ctx[vring->swhead]; |
| 136 | dma_unmap_single(dev, pa, d->dma.length, |
| 137 | DMA_FROM_DEVICE); |
| 138 | kfree_skb(skb); |
| 139 | wil_vring_advance_head(vring, 1); |
| 140 | } |
| 141 | } |
| 142 | dma_free_coherent(dev, sz, (void *)vring->va, vring->pa); |
| 143 | kfree(vring->ctx); |
| 144 | vring->pa = 0; |
| 145 | vring->va = NULL; |
| 146 | vring->ctx = NULL; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Allocate one skb for Rx VRING |
| 151 | * |
| 152 | * Safe to call from IRQ |
| 153 | */ |
| 154 | static int wil_vring_alloc_skb(struct wil6210_priv *wil, struct vring *vring, |
| 155 | u32 i, int headroom) |
| 156 | { |
| 157 | struct device *dev = wil_to_dev(wil); |
| 158 | unsigned int sz = RX_BUF_LEN; |
| 159 | volatile struct vring_rx_desc *d = &(vring->va[i].rx); |
| 160 | dma_addr_t pa; |
| 161 | |
| 162 | /* TODO align */ |
| 163 | struct sk_buff *skb = dev_alloc_skb(sz + headroom); |
| 164 | if (unlikely(!skb)) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | skb_reserve(skb, headroom); |
| 168 | skb_put(skb, sz); |
| 169 | |
| 170 | pa = dma_map_single(dev, skb->data, skb->len, DMA_FROM_DEVICE); |
| 171 | if (unlikely(dma_mapping_error(dev, pa))) { |
| 172 | kfree_skb(skb); |
| 173 | return -ENOMEM; |
| 174 | } |
| 175 | |
| 176 | d->dma.d0 = BIT(9) | RX_DMA_D0_CMD_DMA_IT; |
| 177 | d->dma.addr_low = lower_32_bits(pa); |
| 178 | d->dma.addr_high = (u16)upper_32_bits(pa); |
| 179 | /* ip_length don't care */ |
| 180 | /* b11 don't care */ |
| 181 | /* error don't care */ |
| 182 | d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */ |
| 183 | d->dma.length = sz; |
| 184 | vring->ctx[i] = skb; |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Adds radiotap header |
| 191 | * |
| 192 | * Any error indicated as "Bad FCS" |
| 193 | * |
| 194 | * Vendor data for 04:ce:14-1 (Wilocity-1) consists of: |
| 195 | * - Rx descriptor: 32 bytes |
| 196 | * - Phy info |
| 197 | */ |
| 198 | static void wil_rx_add_radiotap_header(struct wil6210_priv *wil, |
| 199 | struct sk_buff *skb, |
| 200 | volatile struct vring_rx_desc *d) |
| 201 | { |
| 202 | struct wireless_dev *wdev = wil->wdev; |
| 203 | struct wil6210_rtap { |
| 204 | struct ieee80211_radiotap_header rthdr; |
| 205 | /* fields should be in the order of bits in rthdr.it_present */ |
| 206 | /* flags */ |
| 207 | u8 flags; |
| 208 | /* channel */ |
| 209 | __le16 chnl_freq __aligned(2); |
| 210 | __le16 chnl_flags; |
| 211 | /* MCS */ |
| 212 | u8 mcs_present; |
| 213 | u8 mcs_flags; |
| 214 | u8 mcs_index; |
| 215 | } __packed; |
| 216 | struct wil6210_rtap_vendor { |
| 217 | struct wil6210_rtap rtap; |
| 218 | /* vendor */ |
| 219 | u8 vendor_oui[3] __aligned(2); |
| 220 | u8 vendor_ns; |
| 221 | __le16 vendor_skip; |
| 222 | u8 vendor_data[0]; |
| 223 | } __packed; |
| 224 | struct wil6210_rtap_vendor *rtap_vendor; |
| 225 | int rtap_len = sizeof(struct wil6210_rtap); |
| 226 | int phy_length = 0; /* phy info header size, bytes */ |
| 227 | static char phy_data[128]; |
| 228 | struct ieee80211_channel *ch = wdev->preset_chandef.chan; |
| 229 | |
| 230 | if (rtap_include_phy_info) { |
| 231 | rtap_len = sizeof(*rtap_vendor) + sizeof(*d); |
| 232 | /* calculate additional length */ |
| 233 | if (d->dma.status & RX_DMA_STATUS_PHY_INFO) { |
| 234 | /** |
| 235 | * PHY info starts from 8-byte boundary |
| 236 | * there are 8-byte lines, last line may be partially |
| 237 | * written (HW bug), thus FW configures for last line |
| 238 | * to be excessive. Driver skips this last line. |
| 239 | */ |
| 240 | int len = min_t(int, 8 + sizeof(phy_data), |
| 241 | wil_rxdesc_phy_length(d)); |
| 242 | if (len > 8) { |
| 243 | void *p = skb_tail_pointer(skb); |
| 244 | void *pa = PTR_ALIGN(p, 8); |
| 245 | if (skb_tailroom(skb) >= len + (pa - p)) { |
| 246 | phy_length = len - 8; |
| 247 | memcpy(phy_data, pa, phy_length); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | rtap_len += phy_length; |
| 252 | } |
| 253 | |
| 254 | if (skb_headroom(skb) < rtap_len && |
| 255 | pskb_expand_head(skb, rtap_len, 0, GFP_ATOMIC)) { |
| 256 | wil_err(wil, "Unable to expand headrom to %d\n", rtap_len); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | rtap_vendor = (void *)skb_push(skb, rtap_len); |
| 261 | memset(rtap_vendor, 0, rtap_len); |
| 262 | |
| 263 | rtap_vendor->rtap.rthdr.it_version = PKTHDR_RADIOTAP_VERSION; |
| 264 | rtap_vendor->rtap.rthdr.it_len = cpu_to_le16(rtap_len); |
| 265 | rtap_vendor->rtap.rthdr.it_present = cpu_to_le32( |
| 266 | (1 << IEEE80211_RADIOTAP_FLAGS) | |
| 267 | (1 << IEEE80211_RADIOTAP_CHANNEL) | |
| 268 | (1 << IEEE80211_RADIOTAP_MCS)); |
| 269 | if (d->dma.status & RX_DMA_STATUS_ERROR) |
| 270 | rtap_vendor->rtap.flags |= IEEE80211_RADIOTAP_F_BADFCS; |
| 271 | |
| 272 | rtap_vendor->rtap.chnl_freq = cpu_to_le16(ch ? ch->center_freq : 58320); |
| 273 | rtap_vendor->rtap.chnl_flags = cpu_to_le16(0); |
| 274 | |
| 275 | rtap_vendor->rtap.mcs_present = IEEE80211_RADIOTAP_MCS_HAVE_MCS; |
| 276 | rtap_vendor->rtap.mcs_flags = 0; |
| 277 | rtap_vendor->rtap.mcs_index = wil_rxdesc_mcs(d); |
| 278 | |
| 279 | if (rtap_include_phy_info) { |
| 280 | rtap_vendor->rtap.rthdr.it_present |= cpu_to_le32(1 << |
| 281 | IEEE80211_RADIOTAP_VENDOR_NAMESPACE); |
| 282 | /* OUI for Wilocity 04:ce:14 */ |
| 283 | rtap_vendor->vendor_oui[0] = 0x04; |
| 284 | rtap_vendor->vendor_oui[1] = 0xce; |
| 285 | rtap_vendor->vendor_oui[2] = 0x14; |
| 286 | rtap_vendor->vendor_ns = 1; |
| 287 | /* Rx descriptor + PHY data */ |
| 288 | rtap_vendor->vendor_skip = cpu_to_le16(sizeof(*d) + |
| 289 | phy_length); |
| 290 | memcpy(rtap_vendor->vendor_data, (void *)d, sizeof(*d)); |
| 291 | memcpy(rtap_vendor->vendor_data + sizeof(*d), phy_data, |
| 292 | phy_length); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Fast swap in place between 2 registers |
| 298 | */ |
| 299 | static void wil_swap_u16(u16 *a, u16 *b) |
| 300 | { |
| 301 | *a ^= *b; |
| 302 | *b ^= *a; |
| 303 | *a ^= *b; |
| 304 | } |
| 305 | |
| 306 | static void wil_swap_ethaddr(void *data) |
| 307 | { |
| 308 | struct ethhdr *eth = data; |
| 309 | u16 *s = (u16 *)eth->h_source; |
| 310 | u16 *d = (u16 *)eth->h_dest; |
| 311 | |
| 312 | wil_swap_u16(s++, d++); |
| 313 | wil_swap_u16(s++, d++); |
| 314 | wil_swap_u16(s, d); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * reap 1 frame from @swhead |
| 319 | * |
| 320 | * Safe to call from IRQ |
| 321 | */ |
| 322 | static struct sk_buff *wil_vring_reap_rx(struct wil6210_priv *wil, |
| 323 | struct vring *vring) |
| 324 | { |
| 325 | struct device *dev = wil_to_dev(wil); |
| 326 | struct net_device *ndev = wil_to_ndev(wil); |
| 327 | volatile struct vring_rx_desc *d; |
| 328 | struct sk_buff *skb; |
| 329 | dma_addr_t pa; |
| 330 | unsigned int sz = RX_BUF_LEN; |
| 331 | u8 ftype; |
| 332 | u8 ds_bits; |
| 333 | |
| 334 | if (wil_vring_is_empty(vring)) |
| 335 | return NULL; |
| 336 | |
| 337 | d = &(vring->va[vring->swhead].rx); |
| 338 | if (!(d->dma.status & RX_DMA_STATUS_DU)) { |
| 339 | /* it is not error, we just reached end of Rx done area */ |
| 340 | return NULL; |
| 341 | } |
| 342 | |
| 343 | pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32); |
| 344 | skb = vring->ctx[vring->swhead]; |
| 345 | dma_unmap_single(dev, pa, sz, DMA_FROM_DEVICE); |
| 346 | skb_trim(skb, d->dma.length); |
| 347 | |
| 348 | wil->stats.last_mcs_rx = wil_rxdesc_mcs(d); |
| 349 | |
| 350 | /* use radiotap header only if required */ |
| 351 | if (ndev->type == ARPHRD_IEEE80211_RADIOTAP) |
| 352 | wil_rx_add_radiotap_header(wil, skb, d); |
| 353 | |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 354 | wil_dbg_txrx(wil, "Rx[%3d] : %d bytes\n", vring->swhead, d->dma.length); |
| 355 | wil_hex_dump_txrx("Rx ", DUMP_PREFIX_NONE, 32, 4, |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 356 | (const void *)d, sizeof(*d), false); |
| 357 | |
| 358 | wil_vring_advance_head(vring, 1); |
| 359 | |
| 360 | /* no extra checks if in sniffer mode */ |
| 361 | if (ndev->type != ARPHRD_ETHER) |
| 362 | return skb; |
| 363 | /* |
| 364 | * Non-data frames may be delivered through Rx DMA channel (ex: BAR) |
| 365 | * Driver should recognize it by frame type, that is found |
| 366 | * in Rx descriptor. If type is not data, it is 802.11 frame as is |
| 367 | */ |
| 368 | ftype = wil_rxdesc_ftype(d) << 2; |
| 369 | if (ftype != IEEE80211_FTYPE_DATA) { |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 370 | wil_dbg_txrx(wil, "Non-data frame ftype 0x%08x\n", ftype); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 371 | /* TODO: process it */ |
| 372 | kfree_skb(skb); |
| 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | if (skb->len < ETH_HLEN) { |
| 377 | wil_err(wil, "Short frame, len = %d\n", skb->len); |
| 378 | /* TODO: process it (i.e. BAR) */ |
| 379 | kfree_skb(skb); |
| 380 | return NULL; |
| 381 | } |
| 382 | |
| 383 | ds_bits = wil_rxdesc_ds_bits(d); |
| 384 | if (ds_bits == 1) { |
| 385 | /* |
| 386 | * HW bug - in ToDS mode, i.e. Rx on AP side, |
| 387 | * addresses get swapped |
| 388 | */ |
| 389 | wil_swap_ethaddr(skb->data); |
| 390 | } |
| 391 | |
| 392 | return skb; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * allocate and fill up to @count buffers in rx ring |
| 397 | * buffers posted at @swtail |
| 398 | */ |
| 399 | static int wil_rx_refill(struct wil6210_priv *wil, int count) |
| 400 | { |
| 401 | struct net_device *ndev = wil_to_ndev(wil); |
| 402 | struct vring *v = &wil->vring_rx; |
| 403 | u32 next_tail; |
| 404 | int rc = 0; |
| 405 | int headroom = ndev->type == ARPHRD_IEEE80211_RADIOTAP ? |
| 406 | WIL6210_RTAP_SIZE : 0; |
| 407 | |
| 408 | for (; next_tail = wil_vring_next_tail(v), |
| 409 | (next_tail != v->swhead) && (count-- > 0); |
| 410 | v->swtail = next_tail) { |
| 411 | rc = wil_vring_alloc_skb(wil, v, v->swtail, headroom); |
| 412 | if (rc) { |
| 413 | wil_err(wil, "Error %d in wil_rx_refill[%d]\n", |
| 414 | rc, v->swtail); |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | iowrite32(v->swtail, wil->csr + HOSTADDR(v->hwtail)); |
| 419 | |
| 420 | return rc; |
| 421 | } |
| 422 | |
| 423 | /* |
| 424 | * Pass Rx packet to the netif. Update statistics. |
| 425 | */ |
| 426 | static void wil_netif_rx_any(struct sk_buff *skb, struct net_device *ndev) |
| 427 | { |
| 428 | int rc; |
| 429 | unsigned int len = skb->len; |
| 430 | |
Vladimir Kondratiev | 241804c | 2013-01-28 18:31:02 +0200 | [diff] [blame] | 431 | skb_orphan(skb); |
| 432 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 433 | if (in_interrupt()) |
| 434 | rc = netif_rx(skb); |
| 435 | else |
| 436 | rc = netif_rx_ni(skb); |
| 437 | |
| 438 | if (likely(rc == NET_RX_SUCCESS)) { |
| 439 | ndev->stats.rx_packets++; |
| 440 | ndev->stats.rx_bytes += len; |
| 441 | |
| 442 | } else { |
| 443 | ndev->stats.rx_dropped++; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Proceed all completed skb's from Rx VRING |
| 449 | * |
| 450 | * Safe to call from IRQ |
| 451 | */ |
| 452 | void wil_rx_handle(struct wil6210_priv *wil) |
| 453 | { |
| 454 | struct net_device *ndev = wil_to_ndev(wil); |
| 455 | struct vring *v = &wil->vring_rx; |
| 456 | struct sk_buff *skb; |
| 457 | |
| 458 | if (!v->va) { |
| 459 | wil_err(wil, "Rx IRQ while Rx not yet initialized\n"); |
| 460 | return; |
| 461 | } |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 462 | wil_dbg_txrx(wil, "%s()\n", __func__); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 463 | while (NULL != (skb = wil_vring_reap_rx(wil, v))) { |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 464 | wil_hex_dump_txrx("Rx ", DUMP_PREFIX_OFFSET, 16, 1, |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 465 | skb->data, skb_headlen(skb), false); |
| 466 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 467 | if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { |
| 468 | skb->dev = ndev; |
| 469 | skb_reset_mac_header(skb); |
| 470 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 471 | skb->pkt_type = PACKET_OTHERHOST; |
| 472 | skb->protocol = htons(ETH_P_802_2); |
| 473 | |
| 474 | } else { |
| 475 | skb->protocol = eth_type_trans(skb, ndev); |
| 476 | } |
| 477 | |
| 478 | wil_netif_rx_any(skb, ndev); |
| 479 | } |
| 480 | wil_rx_refill(wil, v->size); |
| 481 | } |
| 482 | |
| 483 | int wil_rx_init(struct wil6210_priv *wil) |
| 484 | { |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 485 | struct vring *vring = &wil->vring_rx; |
| 486 | int rc; |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 487 | |
| 488 | vring->size = WIL6210_RX_RING_SIZE; |
| 489 | rc = wil_vring_alloc(wil, vring); |
| 490 | if (rc) |
| 491 | return rc; |
| 492 | |
Vladimir Kondratiev | 47e19af | 2013-01-28 18:30:59 +0200 | [diff] [blame] | 493 | rc = wmi_rx_chain_add(wil, vring); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 494 | if (rc) |
| 495 | goto err_free; |
| 496 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 497 | rc = wil_rx_refill(wil, vring->size); |
| 498 | if (rc) |
| 499 | goto err_free; |
| 500 | |
| 501 | return 0; |
| 502 | err_free: |
| 503 | wil_vring_free(wil, vring, 0); |
| 504 | |
| 505 | return rc; |
| 506 | } |
| 507 | |
| 508 | void wil_rx_fini(struct wil6210_priv *wil) |
| 509 | { |
| 510 | struct vring *vring = &wil->vring_rx; |
| 511 | |
Vladimir Kondratiev | 2acb422 | 2013-01-28 18:31:08 +0200 | [diff] [blame] | 512 | if (vring->va) |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 513 | wil_vring_free(wil, vring, 0); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | int wil_vring_init_tx(struct wil6210_priv *wil, int id, int size, |
| 517 | int cid, int tid) |
| 518 | { |
| 519 | int rc; |
| 520 | struct wmi_vring_cfg_cmd cmd = { |
| 521 | .action = cpu_to_le32(WMI_VRING_CMD_ADD), |
| 522 | .vring_cfg = { |
| 523 | .tx_sw_ring = { |
| 524 | .max_mpdu_size = cpu_to_le16(TX_BUF_LEN), |
| 525 | }, |
| 526 | .ringid = id, |
| 527 | .cidxtid = (cid & 0xf) | ((tid & 0xf) << 4), |
| 528 | .encap_trans_type = WMI_VRING_ENC_TYPE_802_3, |
| 529 | .mac_ctrl = 0, |
| 530 | .to_resolution = 0, |
| 531 | .agg_max_wsize = 16, |
| 532 | .schd_params = { |
| 533 | .priority = cpu_to_le16(0), |
| 534 | .timeslot_us = cpu_to_le16(0xfff), |
| 535 | }, |
| 536 | }, |
| 537 | }; |
| 538 | struct { |
| 539 | struct wil6210_mbox_hdr_wmi wmi; |
| 540 | struct wmi_vring_cfg_done_event cmd; |
| 541 | } __packed reply; |
| 542 | struct vring *vring = &wil->vring_tx[id]; |
| 543 | |
| 544 | if (vring->va) { |
| 545 | wil_err(wil, "Tx ring [%d] already allocated\n", id); |
| 546 | rc = -EINVAL; |
| 547 | goto out; |
| 548 | } |
| 549 | |
| 550 | vring->size = size; |
| 551 | rc = wil_vring_alloc(wil, vring); |
| 552 | if (rc) |
| 553 | goto out; |
| 554 | |
| 555 | cmd.vring_cfg.tx_sw_ring.ring_mem_base = cpu_to_le64(vring->pa); |
| 556 | cmd.vring_cfg.tx_sw_ring.ring_size = cpu_to_le16(vring->size); |
| 557 | |
| 558 | rc = wmi_call(wil, WMI_VRING_CFG_CMDID, &cmd, sizeof(cmd), |
| 559 | WMI_VRING_CFG_DONE_EVENTID, &reply, sizeof(reply), 100); |
| 560 | if (rc) |
| 561 | goto out_free; |
| 562 | |
| 563 | if (reply.cmd.status != WMI_VRING_CFG_SUCCESS) { |
| 564 | wil_err(wil, "Tx config failed, status 0x%02x\n", |
| 565 | reply.cmd.status); |
Vladimir Kondratiev | c331997 | 2013-01-28 18:31:09 +0200 | [diff] [blame] | 566 | rc = -EINVAL; |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 567 | goto out_free; |
| 568 | } |
| 569 | vring->hwtail = le32_to_cpu(reply.cmd.tx_vring_tail_ptr); |
| 570 | |
| 571 | return 0; |
| 572 | out_free: |
| 573 | wil_vring_free(wil, vring, 1); |
| 574 | out: |
| 575 | |
| 576 | return rc; |
| 577 | } |
| 578 | |
| 579 | void wil_vring_fini_tx(struct wil6210_priv *wil, int id) |
| 580 | { |
| 581 | struct vring *vring = &wil->vring_tx[id]; |
| 582 | |
| 583 | if (!vring->va) |
| 584 | return; |
| 585 | |
| 586 | wil_vring_free(wil, vring, 1); |
| 587 | } |
| 588 | |
| 589 | static struct vring *wil_find_tx_vring(struct wil6210_priv *wil, |
| 590 | struct sk_buff *skb) |
| 591 | { |
| 592 | struct vring *v = &wil->vring_tx[0]; |
| 593 | |
| 594 | if (v->va) |
| 595 | return v; |
| 596 | |
| 597 | return NULL; |
| 598 | } |
| 599 | |
| 600 | static int wil_tx_desc_map(volatile struct vring_tx_desc *d, |
| 601 | dma_addr_t pa, u32 len) |
| 602 | { |
| 603 | d->dma.addr_low = lower_32_bits(pa); |
| 604 | d->dma.addr_high = (u16)upper_32_bits(pa); |
| 605 | d->dma.ip_length = 0; |
| 606 | /* 0..6: mac_length; 7:ip_version 0-IP6 1-IP4*/ |
| 607 | d->dma.b11 = 0/*14 | BIT(7)*/; |
| 608 | d->dma.error = 0; |
| 609 | d->dma.status = 0; /* BIT(0) should be 0 for HW_OWNED */ |
| 610 | d->dma.length = len; |
| 611 | d->dma.d0 = 0; |
| 612 | d->mac.d[0] = 0; |
| 613 | d->mac.d[1] = 0; |
| 614 | d->mac.d[2] = 0; |
| 615 | d->mac.ucode_cmd = 0; |
| 616 | /* use dst index 0 */ |
| 617 | d->mac.d[1] |= BIT(MAC_CFG_DESC_TX_1_DST_INDEX_EN_POS) | |
| 618 | (0 << MAC_CFG_DESC_TX_1_DST_INDEX_POS); |
| 619 | /* translation type: 0 - bypass; 1 - 802.3; 2 - native wifi */ |
| 620 | d->mac.d[2] = BIT(MAC_CFG_DESC_TX_2_SNAP_HDR_INSERTION_EN_POS) | |
| 621 | (1 << MAC_CFG_DESC_TX_2_L2_TRANSLATION_TYPE_POS); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static int wil_tx_vring(struct wil6210_priv *wil, struct vring *vring, |
| 627 | struct sk_buff *skb) |
| 628 | { |
| 629 | struct device *dev = wil_to_dev(wil); |
| 630 | volatile struct vring_tx_desc *d; |
| 631 | u32 swhead = vring->swhead; |
| 632 | int avail = wil_vring_avail_tx(vring); |
| 633 | int nr_frags = skb_shinfo(skb)->nr_frags; |
| 634 | uint f; |
| 635 | int vring_index = vring - wil->vring_tx; |
| 636 | uint i = swhead; |
| 637 | dma_addr_t pa; |
| 638 | |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 639 | wil_dbg_txrx(wil, "%s()\n", __func__); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 640 | |
| 641 | if (avail < vring->size/8) |
| 642 | netif_tx_stop_all_queues(wil_to_ndev(wil)); |
| 643 | if (avail < 1 + nr_frags) { |
| 644 | wil_err(wil, "Tx ring full. No space for %d fragments\n", |
| 645 | 1 + nr_frags); |
| 646 | return -ENOMEM; |
| 647 | } |
| 648 | d = &(vring->va[i].tx); |
| 649 | |
| 650 | /* FIXME FW can accept only unicast frames for the peer */ |
| 651 | memcpy(skb->data, wil->dst_addr[vring_index], ETH_ALEN); |
| 652 | |
| 653 | pa = dma_map_single(dev, skb->data, |
| 654 | skb_headlen(skb), DMA_TO_DEVICE); |
| 655 | |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 656 | wil_dbg_txrx(wil, "Tx skb %d bytes %p -> %#08llx\n", skb_headlen(skb), |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 657 | skb->data, (unsigned long long)pa); |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 658 | wil_hex_dump_txrx("Tx ", DUMP_PREFIX_OFFSET, 16, 1, |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 659 | skb->data, skb_headlen(skb), false); |
| 660 | |
| 661 | if (unlikely(dma_mapping_error(dev, pa))) |
| 662 | return -EINVAL; |
| 663 | /* 1-st segment */ |
| 664 | wil_tx_desc_map(d, pa, skb_headlen(skb)); |
| 665 | d->mac.d[2] |= ((nr_frags + 1) << |
| 666 | MAC_CFG_DESC_TX_2_NUM_OF_DESCRIPTORS_POS); |
| 667 | /* middle segments */ |
| 668 | for (f = 0; f < nr_frags; f++) { |
| 669 | const struct skb_frag_struct *frag = |
| 670 | &skb_shinfo(skb)->frags[f]; |
| 671 | int len = skb_frag_size(frag); |
| 672 | i = (swhead + f + 1) % vring->size; |
| 673 | d = &(vring->va[i].tx); |
| 674 | pa = skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), |
| 675 | DMA_TO_DEVICE); |
| 676 | if (unlikely(dma_mapping_error(dev, pa))) |
| 677 | goto dma_error; |
| 678 | wil_tx_desc_map(d, pa, len); |
| 679 | vring->ctx[i] = NULL; |
| 680 | } |
| 681 | /* for the last seg only */ |
| 682 | d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_EOP_POS); |
| 683 | d->dma.d0 |= BIT(9); /* BUG: undocumented bit */ |
| 684 | d->dma.d0 |= BIT(DMA_CFG_DESC_TX_0_CMD_DMA_IT_POS); |
| 685 | d->dma.d0 |= (vring_index << DMA_CFG_DESC_TX_0_QID_POS); |
| 686 | |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 687 | wil_hex_dump_txrx("Tx ", DUMP_PREFIX_NONE, 32, 4, |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 688 | (const void *)d, sizeof(*d), false); |
| 689 | |
| 690 | /* advance swhead */ |
| 691 | wil_vring_advance_head(vring, nr_frags + 1); |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 692 | wil_dbg_txrx(wil, "Tx swhead %d -> %d\n", swhead, vring->swhead); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 693 | iowrite32(vring->swhead, wil->csr + HOSTADDR(vring->hwtail)); |
| 694 | /* hold reference to skb |
| 695 | * to prevent skb release before accounting |
| 696 | * in case of immediate "tx done" |
| 697 | */ |
| 698 | vring->ctx[i] = skb_get(skb); |
| 699 | |
| 700 | return 0; |
| 701 | dma_error: |
| 702 | /* unmap what we have mapped */ |
| 703 | /* Note: increment @f to operate with positive index */ |
| 704 | for (f++; f > 0; f--) { |
| 705 | i = (swhead + f) % vring->size; |
| 706 | d = &(vring->va[i].tx); |
| 707 | d->dma.status = TX_DMA_STATUS_DU; |
| 708 | pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32); |
| 709 | if (vring->ctx[i]) |
| 710 | dma_unmap_single(dev, pa, d->dma.length, DMA_TO_DEVICE); |
| 711 | else |
| 712 | dma_unmap_page(dev, pa, d->dma.length, DMA_TO_DEVICE); |
| 713 | } |
| 714 | |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | |
| 718 | |
| 719 | netdev_tx_t wil_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 720 | { |
| 721 | struct wil6210_priv *wil = ndev_to_wil(ndev); |
| 722 | struct vring *vring; |
| 723 | int rc; |
| 724 | |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 725 | wil_dbg_txrx(wil, "%s()\n", __func__); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 726 | if (!test_bit(wil_status_fwready, &wil->status)) { |
| 727 | wil_err(wil, "FW not ready\n"); |
| 728 | goto drop; |
| 729 | } |
| 730 | if (!test_bit(wil_status_fwconnected, &wil->status)) { |
| 731 | wil_err(wil, "FW not connected\n"); |
| 732 | goto drop; |
| 733 | } |
| 734 | if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR) { |
| 735 | wil_err(wil, "Xmit in monitor mode not supported\n"); |
| 736 | goto drop; |
| 737 | } |
| 738 | if (skb->protocol == cpu_to_be16(ETH_P_PAE)) { |
| 739 | rc = wmi_tx_eapol(wil, skb); |
| 740 | } else { |
| 741 | /* find vring */ |
| 742 | vring = wil_find_tx_vring(wil, skb); |
| 743 | if (!vring) { |
| 744 | wil_err(wil, "No Tx VRING available\n"); |
| 745 | goto drop; |
| 746 | } |
| 747 | /* set up vring entry */ |
| 748 | rc = wil_tx_vring(wil, vring, skb); |
| 749 | } |
| 750 | switch (rc) { |
| 751 | case 0: |
Vladimir Kondratiev | 795ce73 | 2013-01-28 18:31:00 +0200 | [diff] [blame] | 752 | /* statistics will be updated on the tx_complete */ |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 753 | dev_kfree_skb_any(skb); |
| 754 | return NETDEV_TX_OK; |
| 755 | case -ENOMEM: |
| 756 | return NETDEV_TX_BUSY; |
| 757 | default: |
Vladimir Kondratiev | afda8bb | 2013-01-28 18:31:07 +0200 | [diff] [blame] | 758 | break; /* goto drop; */ |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 759 | } |
| 760 | drop: |
| 761 | netif_tx_stop_all_queues(ndev); |
| 762 | ndev->stats.tx_dropped++; |
| 763 | dev_kfree_skb_any(skb); |
| 764 | |
| 765 | return NET_XMIT_DROP; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * Clean up transmitted skb's from the Tx VRING |
| 770 | * |
| 771 | * Safe to call from IRQ |
| 772 | */ |
| 773 | void wil_tx_complete(struct wil6210_priv *wil, int ringid) |
| 774 | { |
Vladimir Kondratiev | 795ce73 | 2013-01-28 18:31:00 +0200 | [diff] [blame] | 775 | struct net_device *ndev = wil_to_ndev(wil); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 776 | struct device *dev = wil_to_dev(wil); |
| 777 | struct vring *vring = &wil->vring_tx[ringid]; |
| 778 | |
| 779 | if (!vring->va) { |
| 780 | wil_err(wil, "Tx irq[%d]: vring not initialized\n", ringid); |
| 781 | return; |
| 782 | } |
| 783 | |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 784 | wil_dbg_txrx(wil, "%s(%d)\n", __func__, ringid); |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 785 | |
| 786 | while (!wil_vring_is_empty(vring)) { |
| 787 | volatile struct vring_tx_desc *d = &vring->va[vring->swtail].tx; |
| 788 | dma_addr_t pa; |
| 789 | struct sk_buff *skb; |
| 790 | if (!(d->dma.status & TX_DMA_STATUS_DU)) |
| 791 | break; |
| 792 | |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 793 | wil_dbg_txrx(wil, |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 794 | "Tx[%3d] : %d bytes, status 0x%02x err 0x%02x\n", |
| 795 | vring->swtail, d->dma.length, d->dma.status, |
| 796 | d->dma.error); |
Vladimir Kondratiev | 7743882 | 2013-01-28 18:31:06 +0200 | [diff] [blame] | 797 | wil_hex_dump_txrx("TxC ", DUMP_PREFIX_NONE, 32, 4, |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 798 | (const void *)d, sizeof(*d), false); |
| 799 | |
| 800 | pa = d->dma.addr_low | ((u64)d->dma.addr_high << 32); |
| 801 | skb = vring->ctx[vring->swtail]; |
| 802 | if (skb) { |
Vladimir Kondratiev | 795ce73 | 2013-01-28 18:31:00 +0200 | [diff] [blame] | 803 | if (d->dma.error == 0) { |
| 804 | ndev->stats.tx_packets++; |
| 805 | ndev->stats.tx_bytes += skb->len; |
| 806 | } else { |
| 807 | ndev->stats.tx_errors++; |
| 808 | } |
| 809 | |
Vladimir Kondratiev | 2be7d22 | 2012-12-20 13:13:19 -0800 | [diff] [blame] | 810 | dma_unmap_single(dev, pa, d->dma.length, DMA_TO_DEVICE); |
| 811 | dev_kfree_skb_any(skb); |
| 812 | vring->ctx[vring->swtail] = NULL; |
| 813 | } else { |
| 814 | dma_unmap_page(dev, pa, d->dma.length, DMA_TO_DEVICE); |
| 815 | } |
| 816 | d->dma.addr_low = 0; |
| 817 | d->dma.addr_high = 0; |
| 818 | d->dma.length = 0; |
| 819 | d->dma.status = TX_DMA_STATUS_DU; |
| 820 | vring->swtail = wil_vring_next_tail(vring); |
| 821 | } |
| 822 | if (wil_vring_avail_tx(vring) > vring->size/4) |
| 823 | netif_tx_wake_all_queues(wil_to_ndev(wil)); |
| 824 | } |