Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | Broadcom B43 wireless driver |
| 4 | |
| 5 | DMA ringbuffer and descriptor allocation/management |
| 6 | |
Michael Büsch | eb032b9 | 2011-07-04 20:50:05 +0200 | [diff] [blame] | 7 | Copyright (c) 2005, 2006 Michael Buesch <m@bues.ch> |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 8 | |
| 9 | Some code in this file is derived from the b44.c driver |
| 10 | Copyright (C) 2002 David S. Miller |
| 11 | Copyright (C) Pekka Pietikainen |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or modify |
| 14 | it under the terms of the GNU General Public License as published by |
| 15 | the Free Software Foundation; either version 2 of the License, or |
| 16 | (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; see the file COPYING. If not, write to |
| 25 | the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor, |
| 26 | Boston, MA 02110-1301, USA. |
| 27 | |
| 28 | */ |
| 29 | |
| 30 | #include "b43.h" |
| 31 | #include "dma.h" |
| 32 | #include "main.h" |
| 33 | #include "debugfs.h" |
| 34 | #include "xmit.h" |
| 35 | |
| 36 | #include <linux/dma-mapping.h> |
| 37 | #include <linux/pci.h> |
| 38 | #include <linux/delay.h> |
| 39 | #include <linux/skbuff.h> |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 40 | #include <linux/etherdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 41 | #include <linux/slab.h> |
Michael Buesch | 57df40d | 2008-03-07 15:50:02 +0100 | [diff] [blame] | 42 | #include <asm/div64.h> |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 43 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 44 | |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 45 | /* Required number of TX DMA slots per TX frame. |
| 46 | * This currently is 2, because we put the header and the ieee80211 frame |
| 47 | * into separate slots. */ |
| 48 | #define TX_SLOTS_PER_FRAME 2 |
| 49 | |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 50 | static u32 b43_dma_address(struct b43_dma *dma, dma_addr_t dmaaddr, |
| 51 | enum b43_addrtype addrtype) |
| 52 | { |
| 53 | u32 uninitialized_var(addr); |
| 54 | |
| 55 | switch (addrtype) { |
| 56 | case B43_DMA_ADDR_LOW: |
| 57 | addr = lower_32_bits(dmaaddr); |
| 58 | if (dma->translation_in_low) { |
| 59 | addr &= ~SSB_DMA_TRANSLATION_MASK; |
| 60 | addr |= dma->translation; |
| 61 | } |
| 62 | break; |
| 63 | case B43_DMA_ADDR_HIGH: |
| 64 | addr = upper_32_bits(dmaaddr); |
| 65 | if (!dma->translation_in_low) { |
| 66 | addr &= ~SSB_DMA_TRANSLATION_MASK; |
| 67 | addr |= dma->translation; |
| 68 | } |
| 69 | break; |
| 70 | case B43_DMA_ADDR_EXT: |
| 71 | if (dma->translation_in_low) |
| 72 | addr = lower_32_bits(dmaaddr); |
| 73 | else |
| 74 | addr = upper_32_bits(dmaaddr); |
| 75 | addr &= SSB_DMA_TRANSLATION_MASK; |
| 76 | addr >>= SSB_DMA_TRANSLATION_SHIFT; |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | return addr; |
| 81 | } |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 82 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 83 | /* 32bit DMA ops. */ |
| 84 | static |
| 85 | struct b43_dmadesc_generic *op32_idx2desc(struct b43_dmaring *ring, |
| 86 | int slot, |
| 87 | struct b43_dmadesc_meta **meta) |
| 88 | { |
| 89 | struct b43_dmadesc32 *desc; |
| 90 | |
| 91 | *meta = &(ring->meta[slot]); |
| 92 | desc = ring->descbase; |
| 93 | desc = &(desc[slot]); |
| 94 | |
| 95 | return (struct b43_dmadesc_generic *)desc; |
| 96 | } |
| 97 | |
| 98 | static void op32_fill_descriptor(struct b43_dmaring *ring, |
| 99 | struct b43_dmadesc_generic *desc, |
| 100 | dma_addr_t dmaaddr, u16 bufsize, |
| 101 | int start, int end, int irq) |
| 102 | { |
| 103 | struct b43_dmadesc32 *descbase = ring->descbase; |
| 104 | int slot; |
| 105 | u32 ctl; |
| 106 | u32 addr; |
| 107 | u32 addrext; |
| 108 | |
| 109 | slot = (int)(&(desc->dma32) - descbase); |
| 110 | B43_WARN_ON(!(slot >= 0 && slot < ring->nr_slots)); |
| 111 | |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 112 | addr = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_LOW); |
| 113 | addrext = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_EXT); |
| 114 | |
Michael Buesch | 8eccb53 | 2009-02-19 23:39:26 +0100 | [diff] [blame] | 115 | ctl = bufsize & B43_DMA32_DCTL_BYTECNT; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 116 | if (slot == ring->nr_slots - 1) |
| 117 | ctl |= B43_DMA32_DCTL_DTABLEEND; |
| 118 | if (start) |
| 119 | ctl |= B43_DMA32_DCTL_FRAMESTART; |
| 120 | if (end) |
| 121 | ctl |= B43_DMA32_DCTL_FRAMEEND; |
| 122 | if (irq) |
| 123 | ctl |= B43_DMA32_DCTL_IRQ; |
| 124 | ctl |= (addrext << B43_DMA32_DCTL_ADDREXT_SHIFT) |
| 125 | & B43_DMA32_DCTL_ADDREXT_MASK; |
| 126 | |
| 127 | desc->dma32.control = cpu_to_le32(ctl); |
| 128 | desc->dma32.address = cpu_to_le32(addr); |
| 129 | } |
| 130 | |
| 131 | static void op32_poke_tx(struct b43_dmaring *ring, int slot) |
| 132 | { |
| 133 | b43_dma_write(ring, B43_DMA32_TXINDEX, |
| 134 | (u32) (slot * sizeof(struct b43_dmadesc32))); |
| 135 | } |
| 136 | |
| 137 | static void op32_tx_suspend(struct b43_dmaring *ring) |
| 138 | { |
| 139 | b43_dma_write(ring, B43_DMA32_TXCTL, b43_dma_read(ring, B43_DMA32_TXCTL) |
| 140 | | B43_DMA32_TXSUSPEND); |
| 141 | } |
| 142 | |
| 143 | static void op32_tx_resume(struct b43_dmaring *ring) |
| 144 | { |
| 145 | b43_dma_write(ring, B43_DMA32_TXCTL, b43_dma_read(ring, B43_DMA32_TXCTL) |
| 146 | & ~B43_DMA32_TXSUSPEND); |
| 147 | } |
| 148 | |
| 149 | static int op32_get_current_rxslot(struct b43_dmaring *ring) |
| 150 | { |
| 151 | u32 val; |
| 152 | |
| 153 | val = b43_dma_read(ring, B43_DMA32_RXSTATUS); |
| 154 | val &= B43_DMA32_RXDPTR; |
| 155 | |
| 156 | return (val / sizeof(struct b43_dmadesc32)); |
| 157 | } |
| 158 | |
| 159 | static void op32_set_current_rxslot(struct b43_dmaring *ring, int slot) |
| 160 | { |
| 161 | b43_dma_write(ring, B43_DMA32_RXINDEX, |
| 162 | (u32) (slot * sizeof(struct b43_dmadesc32))); |
| 163 | } |
| 164 | |
| 165 | static const struct b43_dma_ops dma32_ops = { |
| 166 | .idx2desc = op32_idx2desc, |
| 167 | .fill_descriptor = op32_fill_descriptor, |
| 168 | .poke_tx = op32_poke_tx, |
| 169 | .tx_suspend = op32_tx_suspend, |
| 170 | .tx_resume = op32_tx_resume, |
| 171 | .get_current_rxslot = op32_get_current_rxslot, |
| 172 | .set_current_rxslot = op32_set_current_rxslot, |
| 173 | }; |
| 174 | |
| 175 | /* 64bit DMA ops. */ |
| 176 | static |
| 177 | struct b43_dmadesc_generic *op64_idx2desc(struct b43_dmaring *ring, |
| 178 | int slot, |
| 179 | struct b43_dmadesc_meta **meta) |
| 180 | { |
| 181 | struct b43_dmadesc64 *desc; |
| 182 | |
| 183 | *meta = &(ring->meta[slot]); |
| 184 | desc = ring->descbase; |
| 185 | desc = &(desc[slot]); |
| 186 | |
| 187 | return (struct b43_dmadesc_generic *)desc; |
| 188 | } |
| 189 | |
| 190 | static void op64_fill_descriptor(struct b43_dmaring *ring, |
| 191 | struct b43_dmadesc_generic *desc, |
| 192 | dma_addr_t dmaaddr, u16 bufsize, |
| 193 | int start, int end, int irq) |
| 194 | { |
| 195 | struct b43_dmadesc64 *descbase = ring->descbase; |
| 196 | int slot; |
| 197 | u32 ctl0 = 0, ctl1 = 0; |
| 198 | u32 addrlo, addrhi; |
| 199 | u32 addrext; |
| 200 | |
| 201 | slot = (int)(&(desc->dma64) - descbase); |
| 202 | B43_WARN_ON(!(slot >= 0 && slot < ring->nr_slots)); |
| 203 | |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 204 | addrlo = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_LOW); |
| 205 | addrhi = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_HIGH); |
| 206 | addrext = b43_dma_address(&ring->dev->dma, dmaaddr, B43_DMA_ADDR_EXT); |
| 207 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 208 | if (slot == ring->nr_slots - 1) |
| 209 | ctl0 |= B43_DMA64_DCTL0_DTABLEEND; |
| 210 | if (start) |
| 211 | ctl0 |= B43_DMA64_DCTL0_FRAMESTART; |
| 212 | if (end) |
| 213 | ctl0 |= B43_DMA64_DCTL0_FRAMEEND; |
| 214 | if (irq) |
| 215 | ctl0 |= B43_DMA64_DCTL0_IRQ; |
Michael Buesch | 8eccb53 | 2009-02-19 23:39:26 +0100 | [diff] [blame] | 216 | ctl1 |= bufsize & B43_DMA64_DCTL1_BYTECNT; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 217 | ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) |
| 218 | & B43_DMA64_DCTL1_ADDREXT_MASK; |
| 219 | |
| 220 | desc->dma64.control0 = cpu_to_le32(ctl0); |
| 221 | desc->dma64.control1 = cpu_to_le32(ctl1); |
| 222 | desc->dma64.address_low = cpu_to_le32(addrlo); |
| 223 | desc->dma64.address_high = cpu_to_le32(addrhi); |
| 224 | } |
| 225 | |
| 226 | static void op64_poke_tx(struct b43_dmaring *ring, int slot) |
| 227 | { |
| 228 | b43_dma_write(ring, B43_DMA64_TXINDEX, |
| 229 | (u32) (slot * sizeof(struct b43_dmadesc64))); |
| 230 | } |
| 231 | |
| 232 | static void op64_tx_suspend(struct b43_dmaring *ring) |
| 233 | { |
| 234 | b43_dma_write(ring, B43_DMA64_TXCTL, b43_dma_read(ring, B43_DMA64_TXCTL) |
| 235 | | B43_DMA64_TXSUSPEND); |
| 236 | } |
| 237 | |
| 238 | static void op64_tx_resume(struct b43_dmaring *ring) |
| 239 | { |
| 240 | b43_dma_write(ring, B43_DMA64_TXCTL, b43_dma_read(ring, B43_DMA64_TXCTL) |
| 241 | & ~B43_DMA64_TXSUSPEND); |
| 242 | } |
| 243 | |
| 244 | static int op64_get_current_rxslot(struct b43_dmaring *ring) |
| 245 | { |
| 246 | u32 val; |
| 247 | |
| 248 | val = b43_dma_read(ring, B43_DMA64_RXSTATUS); |
| 249 | val &= B43_DMA64_RXSTATDPTR; |
| 250 | |
| 251 | return (val / sizeof(struct b43_dmadesc64)); |
| 252 | } |
| 253 | |
| 254 | static void op64_set_current_rxslot(struct b43_dmaring *ring, int slot) |
| 255 | { |
| 256 | b43_dma_write(ring, B43_DMA64_RXINDEX, |
| 257 | (u32) (slot * sizeof(struct b43_dmadesc64))); |
| 258 | } |
| 259 | |
| 260 | static const struct b43_dma_ops dma64_ops = { |
| 261 | .idx2desc = op64_idx2desc, |
| 262 | .fill_descriptor = op64_fill_descriptor, |
| 263 | .poke_tx = op64_poke_tx, |
| 264 | .tx_suspend = op64_tx_suspend, |
| 265 | .tx_resume = op64_tx_resume, |
| 266 | .get_current_rxslot = op64_get_current_rxslot, |
| 267 | .set_current_rxslot = op64_set_current_rxslot, |
| 268 | }; |
| 269 | |
| 270 | static inline int free_slots(struct b43_dmaring *ring) |
| 271 | { |
| 272 | return (ring->nr_slots - ring->used_slots); |
| 273 | } |
| 274 | |
| 275 | static inline int next_slot(struct b43_dmaring *ring, int slot) |
| 276 | { |
| 277 | B43_WARN_ON(!(slot >= -1 && slot <= ring->nr_slots - 1)); |
| 278 | if (slot == ring->nr_slots - 1) |
| 279 | return 0; |
| 280 | return slot + 1; |
| 281 | } |
| 282 | |
| 283 | static inline int prev_slot(struct b43_dmaring *ring, int slot) |
| 284 | { |
| 285 | B43_WARN_ON(!(slot >= 0 && slot <= ring->nr_slots - 1)); |
| 286 | if (slot == 0) |
| 287 | return ring->nr_slots - 1; |
| 288 | return slot - 1; |
| 289 | } |
| 290 | |
| 291 | #ifdef CONFIG_B43_DEBUG |
| 292 | static void update_max_used_slots(struct b43_dmaring *ring, |
| 293 | int current_used_slots) |
| 294 | { |
| 295 | if (current_used_slots <= ring->max_used_slots) |
| 296 | return; |
| 297 | ring->max_used_slots = current_used_slots; |
| 298 | if (b43_debug(ring->dev, B43_DBG_DMAVERBOSE)) { |
| 299 | b43dbg(ring->dev->wl, |
| 300 | "max_used_slots increased to %d on %s ring %d\n", |
| 301 | ring->max_used_slots, |
| 302 | ring->tx ? "TX" : "RX", ring->index); |
| 303 | } |
| 304 | } |
| 305 | #else |
| 306 | static inline |
| 307 | void update_max_used_slots(struct b43_dmaring *ring, int current_used_slots) |
| 308 | { |
| 309 | } |
| 310 | #endif /* DEBUG */ |
| 311 | |
| 312 | /* Request a slot for usage. */ |
| 313 | static inline int request_slot(struct b43_dmaring *ring) |
| 314 | { |
| 315 | int slot; |
| 316 | |
| 317 | B43_WARN_ON(!ring->tx); |
| 318 | B43_WARN_ON(ring->stopped); |
| 319 | B43_WARN_ON(free_slots(ring) == 0); |
| 320 | |
| 321 | slot = next_slot(ring, ring->current_slot); |
| 322 | ring->current_slot = slot; |
| 323 | ring->used_slots++; |
| 324 | |
| 325 | update_max_used_slots(ring, ring->used_slots); |
| 326 | |
| 327 | return slot; |
| 328 | } |
| 329 | |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 330 | static u16 b43_dmacontroller_base(enum b43_dmatype type, int controller_idx) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 331 | { |
| 332 | static const u16 map64[] = { |
| 333 | B43_MMIO_DMA64_BASE0, |
| 334 | B43_MMIO_DMA64_BASE1, |
| 335 | B43_MMIO_DMA64_BASE2, |
| 336 | B43_MMIO_DMA64_BASE3, |
| 337 | B43_MMIO_DMA64_BASE4, |
| 338 | B43_MMIO_DMA64_BASE5, |
| 339 | }; |
| 340 | static const u16 map32[] = { |
| 341 | B43_MMIO_DMA32_BASE0, |
| 342 | B43_MMIO_DMA32_BASE1, |
| 343 | B43_MMIO_DMA32_BASE2, |
| 344 | B43_MMIO_DMA32_BASE3, |
| 345 | B43_MMIO_DMA32_BASE4, |
| 346 | B43_MMIO_DMA32_BASE5, |
| 347 | }; |
| 348 | |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 349 | if (type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 350 | B43_WARN_ON(!(controller_idx >= 0 && |
| 351 | controller_idx < ARRAY_SIZE(map64))); |
| 352 | return map64[controller_idx]; |
| 353 | } |
| 354 | B43_WARN_ON(!(controller_idx >= 0 && |
| 355 | controller_idx < ARRAY_SIZE(map32))); |
| 356 | return map32[controller_idx]; |
| 357 | } |
| 358 | |
| 359 | static inline |
| 360 | dma_addr_t map_descbuffer(struct b43_dmaring *ring, |
| 361 | unsigned char *buf, size_t len, int tx) |
| 362 | { |
| 363 | dma_addr_t dmaaddr; |
| 364 | |
| 365 | if (tx) { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 366 | dmaaddr = dma_map_single(ring->dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 367 | buf, len, DMA_TO_DEVICE); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 368 | } else { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 369 | dmaaddr = dma_map_single(ring->dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 370 | buf, len, DMA_FROM_DEVICE); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | return dmaaddr; |
| 374 | } |
| 375 | |
| 376 | static inline |
| 377 | void unmap_descbuffer(struct b43_dmaring *ring, |
| 378 | dma_addr_t addr, size_t len, int tx) |
| 379 | { |
| 380 | if (tx) { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 381 | dma_unmap_single(ring->dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 382 | addr, len, DMA_TO_DEVICE); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 383 | } else { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 384 | dma_unmap_single(ring->dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 385 | addr, len, DMA_FROM_DEVICE); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | static inline |
| 390 | void sync_descbuffer_for_cpu(struct b43_dmaring *ring, |
| 391 | dma_addr_t addr, size_t len) |
| 392 | { |
| 393 | B43_WARN_ON(ring->tx); |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 394 | dma_sync_single_for_cpu(ring->dev->dev->dma_dev, |
Michael Buesch | f225763 | 2008-06-20 11:50:29 +0200 | [diff] [blame] | 395 | addr, len, DMA_FROM_DEVICE); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static inline |
| 399 | void sync_descbuffer_for_device(struct b43_dmaring *ring, |
| 400 | dma_addr_t addr, size_t len) |
| 401 | { |
| 402 | B43_WARN_ON(ring->tx); |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 403 | dma_sync_single_for_device(ring->dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 404 | addr, len, DMA_FROM_DEVICE); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static inline |
| 408 | void free_descriptor_buffer(struct b43_dmaring *ring, |
| 409 | struct b43_dmadesc_meta *meta) |
| 410 | { |
| 411 | if (meta->skb) { |
Felix Fietkau | 78f18df | 2012-12-10 17:40:21 +0100 | [diff] [blame] | 412 | if (ring->tx) |
| 413 | ieee80211_free_txskb(ring->dev->wl->hw, meta->skb); |
| 414 | else |
| 415 | dev_kfree_skb_any(meta->skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 416 | meta->skb = NULL; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | static int alloc_ringmemory(struct b43_dmaring *ring) |
| 421 | { |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 422 | gfp_t flags = GFP_KERNEL; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 423 | |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 424 | /* The specs call for 4K buffers for 30- and 32-bit DMA with 4K |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 425 | * alignment and 8K buffers for 64-bit DMA with 8K alignment. |
| 426 | * In practice we could use smaller buffers for the latter, but the |
| 427 | * alignment is really important because of the hardware bug. If bit |
| 428 | * 0x00001000 is used in DMA address, some hardware (like BCM4331) |
| 429 | * copies that bit into B43_DMA64_RXSTATUS and we get false values from |
| 430 | * B43_DMA64_RXSTATDPTR. Let's just use 8K buffers even if we don't use |
| 431 | * more than 256 slots for ring. |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 432 | */ |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 433 | u16 ring_mem_size = (ring->type == B43_DMA_64BIT) ? |
| 434 | B43_DMA64_RINGMEMSIZE : B43_DMA32_RINGMEMSIZE; |
| 435 | |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 436 | ring->descbase = dma_alloc_coherent(ring->dev->dev->dma_dev, |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 437 | ring_mem_size, &(ring->dmabase), |
| 438 | flags); |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 439 | if (!ring->descbase) { |
| 440 | b43err(ring->dev->wl, "DMA ringmemory allocation failed\n"); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 441 | return -ENOMEM; |
| 442 | } |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 443 | memset(ring->descbase, 0, ring_mem_size); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static void free_ringmemory(struct b43_dmaring *ring) |
| 449 | { |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 450 | u16 ring_mem_size = (ring->type == B43_DMA_64BIT) ? |
| 451 | B43_DMA64_RINGMEMSIZE : B43_DMA32_RINGMEMSIZE; |
| 452 | dma_free_coherent(ring->dev->dev->dma_dev, ring_mem_size, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 453 | ring->descbase, ring->dmabase); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* Reset the RX DMA channel */ |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 457 | static int b43_dmacontroller_rx_reset(struct b43_wldev *dev, u16 mmio_base, |
| 458 | enum b43_dmatype type) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 459 | { |
| 460 | int i; |
| 461 | u32 value; |
| 462 | u16 offset; |
| 463 | |
| 464 | might_sleep(); |
| 465 | |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 466 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXCTL : B43_DMA32_RXCTL; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 467 | b43_write32(dev, mmio_base + offset, 0); |
| 468 | for (i = 0; i < 10; i++) { |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 469 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXSTATUS : |
| 470 | B43_DMA32_RXSTATUS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 471 | value = b43_read32(dev, mmio_base + offset); |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 472 | if (type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 473 | value &= B43_DMA64_RXSTAT; |
| 474 | if (value == B43_DMA64_RXSTAT_DISABLED) { |
| 475 | i = -1; |
| 476 | break; |
| 477 | } |
| 478 | } else { |
| 479 | value &= B43_DMA32_RXSTATE; |
| 480 | if (value == B43_DMA32_RXSTAT_DISABLED) { |
| 481 | i = -1; |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | msleep(1); |
| 486 | } |
| 487 | if (i != -1) { |
| 488 | b43err(dev->wl, "DMA RX reset timed out\n"); |
| 489 | return -ENODEV; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 495 | /* Reset the TX DMA channel */ |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 496 | static int b43_dmacontroller_tx_reset(struct b43_wldev *dev, u16 mmio_base, |
| 497 | enum b43_dmatype type) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 498 | { |
| 499 | int i; |
| 500 | u32 value; |
| 501 | u16 offset; |
| 502 | |
| 503 | might_sleep(); |
| 504 | |
| 505 | for (i = 0; i < 10; i++) { |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 506 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS : |
| 507 | B43_DMA32_TXSTATUS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 508 | value = b43_read32(dev, mmio_base + offset); |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 509 | if (type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 510 | value &= B43_DMA64_TXSTAT; |
| 511 | if (value == B43_DMA64_TXSTAT_DISABLED || |
| 512 | value == B43_DMA64_TXSTAT_IDLEWAIT || |
| 513 | value == B43_DMA64_TXSTAT_STOPPED) |
| 514 | break; |
| 515 | } else { |
| 516 | value &= B43_DMA32_TXSTATE; |
| 517 | if (value == B43_DMA32_TXSTAT_DISABLED || |
| 518 | value == B43_DMA32_TXSTAT_IDLEWAIT || |
| 519 | value == B43_DMA32_TXSTAT_STOPPED) |
| 520 | break; |
| 521 | } |
| 522 | msleep(1); |
| 523 | } |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 524 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXCTL : B43_DMA32_TXCTL; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 525 | b43_write32(dev, mmio_base + offset, 0); |
| 526 | for (i = 0; i < 10; i++) { |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 527 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS : |
| 528 | B43_DMA32_TXSTATUS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 529 | value = b43_read32(dev, mmio_base + offset); |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 530 | if (type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 531 | value &= B43_DMA64_TXSTAT; |
| 532 | if (value == B43_DMA64_TXSTAT_DISABLED) { |
| 533 | i = -1; |
| 534 | break; |
| 535 | } |
| 536 | } else { |
| 537 | value &= B43_DMA32_TXSTATE; |
| 538 | if (value == B43_DMA32_TXSTAT_DISABLED) { |
| 539 | i = -1; |
| 540 | break; |
| 541 | } |
| 542 | } |
| 543 | msleep(1); |
| 544 | } |
| 545 | if (i != -1) { |
| 546 | b43err(dev->wl, "DMA TX reset timed out\n"); |
| 547 | return -ENODEV; |
| 548 | } |
| 549 | /* ensure the reset is completed. */ |
| 550 | msleep(1); |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 555 | /* Check if a DMA mapping address is invalid. */ |
| 556 | static bool b43_dma_mapping_error(struct b43_dmaring *ring, |
| 557 | dma_addr_t addr, |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 558 | size_t buffersize, bool dma_to_device) |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 559 | { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 560 | if (unlikely(dma_mapping_error(ring->dev->dev->dma_dev, addr))) |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 561 | return 1; |
| 562 | |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 563 | switch (ring->type) { |
| 564 | case B43_DMA_30BIT: |
| 565 | if ((u64)addr + buffersize > (1ULL << 30)) |
| 566 | goto address_error; |
| 567 | break; |
| 568 | case B43_DMA_32BIT: |
| 569 | if ((u64)addr + buffersize > (1ULL << 32)) |
| 570 | goto address_error; |
| 571 | break; |
| 572 | case B43_DMA_64BIT: |
| 573 | /* Currently we can't have addresses beyond |
| 574 | * 64bit in the kernel. */ |
| 575 | break; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | /* The address is OK. */ |
| 579 | return 0; |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 580 | |
| 581 | address_error: |
| 582 | /* We can't support this address. Unmap it again. */ |
| 583 | unmap_descbuffer(ring, addr, buffersize, dma_to_device); |
| 584 | |
| 585 | return 1; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 588 | static bool b43_rx_buffer_is_poisoned(struct b43_dmaring *ring, struct sk_buff *skb) |
| 589 | { |
| 590 | unsigned char *f = skb->data + ring->frameoffset; |
| 591 | |
| 592 | return ((f[0] & f[1] & f[2] & f[3] & f[4] & f[5] & f[6] & f[7]) == 0xFF); |
| 593 | } |
| 594 | |
| 595 | static void b43_poison_rx_buffer(struct b43_dmaring *ring, struct sk_buff *skb) |
| 596 | { |
| 597 | struct b43_rxhdr_fw4 *rxhdr; |
| 598 | unsigned char *frame; |
| 599 | |
| 600 | /* This poisons the RX buffer to detect DMA failures. */ |
| 601 | |
| 602 | rxhdr = (struct b43_rxhdr_fw4 *)(skb->data); |
| 603 | rxhdr->frame_len = 0; |
| 604 | |
| 605 | B43_WARN_ON(ring->rx_buffersize < ring->frameoffset + sizeof(struct b43_plcp_hdr6) + 2); |
| 606 | frame = skb->data + ring->frameoffset; |
| 607 | memset(frame, 0xFF, sizeof(struct b43_plcp_hdr6) + 2 /* padding */); |
| 608 | } |
| 609 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 610 | static int setup_rx_descbuffer(struct b43_dmaring *ring, |
| 611 | struct b43_dmadesc_generic *desc, |
| 612 | struct b43_dmadesc_meta *meta, gfp_t gfp_flags) |
| 613 | { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 614 | dma_addr_t dmaaddr; |
| 615 | struct sk_buff *skb; |
| 616 | |
| 617 | B43_WARN_ON(ring->tx); |
| 618 | |
| 619 | skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags); |
| 620 | if (unlikely(!skb)) |
| 621 | return -ENOMEM; |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 622 | b43_poison_rx_buffer(ring, skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 623 | dmaaddr = map_descbuffer(ring, skb->data, ring->rx_buffersize, 0); |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 624 | if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 625 | /* ugh. try to realloc in zone_dma */ |
| 626 | gfp_flags |= GFP_DMA; |
| 627 | |
| 628 | dev_kfree_skb_any(skb); |
| 629 | |
| 630 | skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags); |
| 631 | if (unlikely(!skb)) |
| 632 | return -ENOMEM; |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 633 | b43_poison_rx_buffer(ring, skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 634 | dmaaddr = map_descbuffer(ring, skb->data, |
| 635 | ring->rx_buffersize, 0); |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 636 | if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) { |
| 637 | b43err(ring->dev->wl, "RX DMA buffer allocation failed\n"); |
| 638 | dev_kfree_skb_any(skb); |
| 639 | return -EIO; |
| 640 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | meta->skb = skb; |
| 644 | meta->dmaaddr = dmaaddr; |
| 645 | ring->ops->fill_descriptor(ring, desc, dmaaddr, |
| 646 | ring->rx_buffersize, 0, 0, 0); |
| 647 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | /* Allocate the initial descbuffers. |
| 652 | * This is used for an RX ring only. |
| 653 | */ |
| 654 | static int alloc_initial_descbuffers(struct b43_dmaring *ring) |
| 655 | { |
| 656 | int i, err = -ENOMEM; |
| 657 | struct b43_dmadesc_generic *desc; |
| 658 | struct b43_dmadesc_meta *meta; |
| 659 | |
| 660 | for (i = 0; i < ring->nr_slots; i++) { |
| 661 | desc = ring->ops->idx2desc(ring, i, &meta); |
| 662 | |
| 663 | err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL); |
| 664 | if (err) { |
| 665 | b43err(ring->dev->wl, |
| 666 | "Failed to allocate initial descbuffers\n"); |
| 667 | goto err_unwind; |
| 668 | } |
| 669 | } |
| 670 | mb(); |
| 671 | ring->used_slots = ring->nr_slots; |
| 672 | err = 0; |
| 673 | out: |
| 674 | return err; |
| 675 | |
| 676 | err_unwind: |
| 677 | for (i--; i >= 0; i--) { |
| 678 | desc = ring->ops->idx2desc(ring, i, &meta); |
| 679 | |
| 680 | unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0); |
| 681 | dev_kfree_skb(meta->skb); |
| 682 | } |
| 683 | goto out; |
| 684 | } |
| 685 | |
| 686 | /* Do initial setup of the DMA controller. |
| 687 | * Reset the controller, write the ring busaddress |
| 688 | * and switch the "enable" bit on. |
| 689 | */ |
| 690 | static int dmacontroller_setup(struct b43_dmaring *ring) |
| 691 | { |
| 692 | int err = 0; |
| 693 | u32 value; |
| 694 | u32 addrext; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 695 | bool parity = ring->dev->dma.parity; |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 696 | u32 addrlo; |
| 697 | u32 addrhi; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 698 | |
| 699 | if (ring->tx) { |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 700 | if (ring->type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 701 | u64 ringbase = (u64) (ring->dmabase); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 702 | addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT); |
| 703 | addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW); |
| 704 | addrhi = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_HIGH); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 705 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 706 | value = B43_DMA64_TXENABLE; |
| 707 | value |= (addrext << B43_DMA64_TXADDREXT_SHIFT) |
| 708 | & B43_DMA64_TXADDREXT_MASK; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 709 | if (!parity) |
| 710 | value |= B43_DMA64_TXPARITYDISABLE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 711 | b43_dma_write(ring, B43_DMA64_TXCTL, value); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 712 | b43_dma_write(ring, B43_DMA64_TXRINGLO, addrlo); |
| 713 | b43_dma_write(ring, B43_DMA64_TXRINGHI, addrhi); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 714 | } else { |
| 715 | u32 ringbase = (u32) (ring->dmabase); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 716 | addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT); |
| 717 | addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 718 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 719 | value = B43_DMA32_TXENABLE; |
| 720 | value |= (addrext << B43_DMA32_TXADDREXT_SHIFT) |
| 721 | & B43_DMA32_TXADDREXT_MASK; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 722 | if (!parity) |
| 723 | value |= B43_DMA32_TXPARITYDISABLE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 724 | b43_dma_write(ring, B43_DMA32_TXCTL, value); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 725 | b43_dma_write(ring, B43_DMA32_TXRING, addrlo); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 726 | } |
| 727 | } else { |
| 728 | err = alloc_initial_descbuffers(ring); |
| 729 | if (err) |
| 730 | goto out; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 731 | if (ring->type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 732 | u64 ringbase = (u64) (ring->dmabase); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 733 | addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT); |
| 734 | addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW); |
| 735 | addrhi = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_HIGH); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 736 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 737 | value = (ring->frameoffset << B43_DMA64_RXFROFF_SHIFT); |
| 738 | value |= B43_DMA64_RXENABLE; |
| 739 | value |= (addrext << B43_DMA64_RXADDREXT_SHIFT) |
| 740 | & B43_DMA64_RXADDREXT_MASK; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 741 | if (!parity) |
| 742 | value |= B43_DMA64_RXPARITYDISABLE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 743 | b43_dma_write(ring, B43_DMA64_RXCTL, value); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 744 | b43_dma_write(ring, B43_DMA64_RXRINGLO, addrlo); |
| 745 | b43_dma_write(ring, B43_DMA64_RXRINGHI, addrhi); |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 746 | b43_dma_write(ring, B43_DMA64_RXINDEX, ring->nr_slots * |
| 747 | sizeof(struct b43_dmadesc64)); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 748 | } else { |
| 749 | u32 ringbase = (u32) (ring->dmabase); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 750 | addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT); |
| 751 | addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 752 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 753 | value = (ring->frameoffset << B43_DMA32_RXFROFF_SHIFT); |
| 754 | value |= B43_DMA32_RXENABLE; |
| 755 | value |= (addrext << B43_DMA32_RXADDREXT_SHIFT) |
| 756 | & B43_DMA32_RXADDREXT_MASK; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 757 | if (!parity) |
| 758 | value |= B43_DMA32_RXPARITYDISABLE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 759 | b43_dma_write(ring, B43_DMA32_RXCTL, value); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 760 | b43_dma_write(ring, B43_DMA32_RXRING, addrlo); |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 761 | b43_dma_write(ring, B43_DMA32_RXINDEX, ring->nr_slots * |
| 762 | sizeof(struct b43_dmadesc32)); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 766 | out: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 767 | return err; |
| 768 | } |
| 769 | |
| 770 | /* Shutdown the DMA controller. */ |
| 771 | static void dmacontroller_cleanup(struct b43_dmaring *ring) |
| 772 | { |
| 773 | if (ring->tx) { |
| 774 | b43_dmacontroller_tx_reset(ring->dev, ring->mmio_base, |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 775 | ring->type); |
| 776 | if (ring->type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 777 | b43_dma_write(ring, B43_DMA64_TXRINGLO, 0); |
| 778 | b43_dma_write(ring, B43_DMA64_TXRINGHI, 0); |
| 779 | } else |
| 780 | b43_dma_write(ring, B43_DMA32_TXRING, 0); |
| 781 | } else { |
| 782 | b43_dmacontroller_rx_reset(ring->dev, ring->mmio_base, |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 783 | ring->type); |
| 784 | if (ring->type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 785 | b43_dma_write(ring, B43_DMA64_RXRINGLO, 0); |
| 786 | b43_dma_write(ring, B43_DMA64_RXRINGHI, 0); |
| 787 | } else |
| 788 | b43_dma_write(ring, B43_DMA32_RXRING, 0); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | static void free_all_descbuffers(struct b43_dmaring *ring) |
| 793 | { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 794 | struct b43_dmadesc_meta *meta; |
| 795 | int i; |
| 796 | |
| 797 | if (!ring->used_slots) |
| 798 | return; |
| 799 | for (i = 0; i < ring->nr_slots; i++) { |
Larry Finger | 9c1cacd | 2011-05-22 20:54:25 -0500 | [diff] [blame] | 800 | /* get meta - ignore returned value */ |
| 801 | ring->ops->idx2desc(ring, i, &meta); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 802 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 803 | if (!meta->skb || b43_dma_ptr_is_poisoned(meta->skb)) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 804 | B43_WARN_ON(!ring->tx); |
| 805 | continue; |
| 806 | } |
| 807 | if (ring->tx) { |
| 808 | unmap_descbuffer(ring, meta->dmaaddr, |
| 809 | meta->skb->len, 1); |
| 810 | } else { |
| 811 | unmap_descbuffer(ring, meta->dmaaddr, |
| 812 | ring->rx_buffersize, 0); |
| 813 | } |
| 814 | free_descriptor_buffer(ring, meta); |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | static u64 supported_dma_mask(struct b43_wldev *dev) |
| 819 | { |
| 820 | u32 tmp; |
| 821 | u16 mmio_base; |
| 822 | |
Hauke Mehrtens | 5b36c9b | 2011-07-23 13:57:33 +0200 | [diff] [blame] | 823 | switch (dev->dev->bus_type) { |
| 824 | #ifdef CONFIG_B43_BCMA |
| 825 | case B43_BUS_BCMA: |
| 826 | tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST); |
| 827 | if (tmp & BCMA_IOST_DMA64) |
| 828 | return DMA_BIT_MASK(64); |
| 829 | break; |
| 830 | #endif |
| 831 | #ifdef CONFIG_B43_SSB |
| 832 | case B43_BUS_SSB: |
| 833 | tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH); |
| 834 | if (tmp & SSB_TMSHIGH_DMA64) |
| 835 | return DMA_BIT_MASK(64); |
| 836 | break; |
| 837 | #endif |
| 838 | } |
| 839 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 840 | mmio_base = b43_dmacontroller_base(0, 0); |
| 841 | b43_write32(dev, mmio_base + B43_DMA32_TXCTL, B43_DMA32_TXADDREXT_MASK); |
| 842 | tmp = b43_read32(dev, mmio_base + B43_DMA32_TXCTL); |
| 843 | if (tmp & B43_DMA32_TXADDREXT_MASK) |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 844 | return DMA_BIT_MASK(32); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 845 | |
Yang Hongyang | 28b7679 | 2009-04-06 19:01:17 -0700 | [diff] [blame] | 846 | return DMA_BIT_MASK(30); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 847 | } |
| 848 | |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 849 | static enum b43_dmatype dma_mask_to_engine_type(u64 dmamask) |
| 850 | { |
Yang Hongyang | 28b7679 | 2009-04-06 19:01:17 -0700 | [diff] [blame] | 851 | if (dmamask == DMA_BIT_MASK(30)) |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 852 | return B43_DMA_30BIT; |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 853 | if (dmamask == DMA_BIT_MASK(32)) |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 854 | return B43_DMA_32BIT; |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 855 | if (dmamask == DMA_BIT_MASK(64)) |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 856 | return B43_DMA_64BIT; |
| 857 | B43_WARN_ON(1); |
| 858 | return B43_DMA_30BIT; |
| 859 | } |
| 860 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 861 | /* Main initialization function. */ |
| 862 | static |
| 863 | struct b43_dmaring *b43_setup_dmaring(struct b43_wldev *dev, |
| 864 | int controller_index, |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 865 | int for_tx, |
| 866 | enum b43_dmatype type) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 867 | { |
| 868 | struct b43_dmaring *ring; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 869 | int i, err; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 870 | dma_addr_t dma_test; |
| 871 | |
| 872 | ring = kzalloc(sizeof(*ring), GFP_KERNEL); |
| 873 | if (!ring) |
| 874 | goto out; |
| 875 | |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 876 | ring->nr_slots = B43_RXRING_SLOTS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 877 | if (for_tx) |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 878 | ring->nr_slots = B43_TXRING_SLOTS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 879 | |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 880 | ring->meta = kcalloc(ring->nr_slots, sizeof(struct b43_dmadesc_meta), |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 881 | GFP_KERNEL); |
| 882 | if (!ring->meta) |
| 883 | goto err_kfree_ring; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 884 | for (i = 0; i < ring->nr_slots; i++) |
| 885 | ring->meta->skb = B43_DMA_PTR_POISON; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 886 | |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 887 | ring->type = type; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 888 | ring->dev = dev; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 889 | ring->mmio_base = b43_dmacontroller_base(type, controller_index); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 890 | ring->index = controller_index; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 891 | if (type == B43_DMA_64BIT) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 892 | ring->ops = &dma64_ops; |
| 893 | else |
| 894 | ring->ops = &dma32_ops; |
| 895 | if (for_tx) { |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 896 | ring->tx = true; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 897 | ring->current_slot = -1; |
| 898 | } else { |
| 899 | if (ring->index == 0) { |
Rafał Miłecki | 17030f4 | 2011-08-11 17:16:27 +0200 | [diff] [blame] | 900 | switch (dev->fw.hdr_format) { |
| 901 | case B43_FW_HDR_598: |
| 902 | ring->rx_buffersize = B43_DMA0_RX_FW598_BUFSIZE; |
| 903 | ring->frameoffset = B43_DMA0_RX_FW598_FO; |
| 904 | break; |
| 905 | case B43_FW_HDR_410: |
| 906 | case B43_FW_HDR_351: |
| 907 | ring->rx_buffersize = B43_DMA0_RX_FW351_BUFSIZE; |
| 908 | ring->frameoffset = B43_DMA0_RX_FW351_FO; |
| 909 | break; |
| 910 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 911 | } else |
| 912 | B43_WARN_ON(1); |
| 913 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 914 | #ifdef CONFIG_B43_DEBUG |
| 915 | ring->last_injected_overflow = jiffies; |
| 916 | #endif |
| 917 | |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 918 | if (for_tx) { |
Michael Buesch | 2d071ca | 2009-02-20 12:24:52 +0100 | [diff] [blame] | 919 | /* Assumption: B43_TXRING_SLOTS can be divided by TX_SLOTS_PER_FRAME */ |
| 920 | BUILD_BUG_ON(B43_TXRING_SLOTS % TX_SLOTS_PER_FRAME != 0); |
| 921 | |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 922 | ring->txhdr_cache = kcalloc(ring->nr_slots / TX_SLOTS_PER_FRAME, |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 923 | b43_txhdr_size(dev), |
| 924 | GFP_KERNEL); |
| 925 | if (!ring->txhdr_cache) |
| 926 | goto err_kfree_meta; |
| 927 | |
| 928 | /* test for ability to dma to txhdr_cache */ |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 929 | dma_test = dma_map_single(dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 930 | ring->txhdr_cache, |
| 931 | b43_txhdr_size(dev), |
| 932 | DMA_TO_DEVICE); |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 933 | |
| 934 | if (b43_dma_mapping_error(ring, dma_test, |
| 935 | b43_txhdr_size(dev), 1)) { |
| 936 | /* ugh realloc */ |
| 937 | kfree(ring->txhdr_cache); |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 938 | ring->txhdr_cache = kcalloc(ring->nr_slots / TX_SLOTS_PER_FRAME, |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 939 | b43_txhdr_size(dev), |
| 940 | GFP_KERNEL | GFP_DMA); |
| 941 | if (!ring->txhdr_cache) |
| 942 | goto err_kfree_meta; |
| 943 | |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 944 | dma_test = dma_map_single(dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 945 | ring->txhdr_cache, |
| 946 | b43_txhdr_size(dev), |
| 947 | DMA_TO_DEVICE); |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 948 | |
| 949 | if (b43_dma_mapping_error(ring, dma_test, |
| 950 | b43_txhdr_size(dev), 1)) { |
| 951 | |
| 952 | b43err(dev->wl, |
| 953 | "TXHDR DMA allocation failed\n"); |
| 954 | goto err_kfree_txhdr_cache; |
| 955 | } |
| 956 | } |
| 957 | |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 958 | dma_unmap_single(dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 959 | dma_test, b43_txhdr_size(dev), |
| 960 | DMA_TO_DEVICE); |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 961 | } |
| 962 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 963 | err = alloc_ringmemory(ring); |
| 964 | if (err) |
| 965 | goto err_kfree_txhdr_cache; |
| 966 | err = dmacontroller_setup(ring); |
| 967 | if (err) |
| 968 | goto err_free_ringmemory; |
| 969 | |
| 970 | out: |
| 971 | return ring; |
| 972 | |
| 973 | err_free_ringmemory: |
| 974 | free_ringmemory(ring); |
| 975 | err_kfree_txhdr_cache: |
| 976 | kfree(ring->txhdr_cache); |
| 977 | err_kfree_meta: |
| 978 | kfree(ring->meta); |
| 979 | err_kfree_ring: |
| 980 | kfree(ring); |
| 981 | ring = NULL; |
| 982 | goto out; |
| 983 | } |
| 984 | |
Michael Buesch | 57df40d | 2008-03-07 15:50:02 +0100 | [diff] [blame] | 985 | #define divide(a, b) ({ \ |
| 986 | typeof(a) __a = a; \ |
| 987 | do_div(__a, b); \ |
| 988 | __a; \ |
| 989 | }) |
| 990 | |
| 991 | #define modulo(a, b) ({ \ |
| 992 | typeof(a) __a = a; \ |
| 993 | do_div(__a, b); \ |
| 994 | }) |
| 995 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 996 | /* Main cleanup function. */ |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 997 | static void b43_destroy_dmaring(struct b43_dmaring *ring, |
| 998 | const char *ringname) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 999 | { |
| 1000 | if (!ring) |
| 1001 | return; |
| 1002 | |
Michael Buesch | 57df40d | 2008-03-07 15:50:02 +0100 | [diff] [blame] | 1003 | #ifdef CONFIG_B43_DEBUG |
| 1004 | { |
| 1005 | /* Print some statistics. */ |
| 1006 | u64 failed_packets = ring->nr_failed_tx_packets; |
| 1007 | u64 succeed_packets = ring->nr_succeed_tx_packets; |
| 1008 | u64 nr_packets = failed_packets + succeed_packets; |
| 1009 | u64 permille_failed = 0, average_tries = 0; |
| 1010 | |
| 1011 | if (nr_packets) |
| 1012 | permille_failed = divide(failed_packets * 1000, nr_packets); |
| 1013 | if (nr_packets) |
| 1014 | average_tries = divide(ring->nr_total_packet_tries * 100, nr_packets); |
| 1015 | |
| 1016 | b43dbg(ring->dev->wl, "DMA-%u %s: " |
| 1017 | "Used slots %d/%d, Failed frames %llu/%llu = %llu.%01llu%%, " |
| 1018 | "Average tries %llu.%02llu\n", |
| 1019 | (unsigned int)(ring->type), ringname, |
| 1020 | ring->max_used_slots, |
| 1021 | ring->nr_slots, |
| 1022 | (unsigned long long)failed_packets, |
Michael Buesch | 87d9611 | 2008-03-07 19:52:24 +0100 | [diff] [blame] | 1023 | (unsigned long long)nr_packets, |
Michael Buesch | 57df40d | 2008-03-07 15:50:02 +0100 | [diff] [blame] | 1024 | (unsigned long long)divide(permille_failed, 10), |
| 1025 | (unsigned long long)modulo(permille_failed, 10), |
| 1026 | (unsigned long long)divide(average_tries, 100), |
| 1027 | (unsigned long long)modulo(average_tries, 100)); |
| 1028 | } |
| 1029 | #endif /* DEBUG */ |
| 1030 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1031 | /* Device IRQs are disabled prior entering this function, |
| 1032 | * so no need to take care of concurrency with rx handler stuff. |
| 1033 | */ |
| 1034 | dmacontroller_cleanup(ring); |
| 1035 | free_all_descbuffers(ring); |
| 1036 | free_ringmemory(ring); |
| 1037 | |
| 1038 | kfree(ring->txhdr_cache); |
| 1039 | kfree(ring->meta); |
| 1040 | kfree(ring); |
| 1041 | } |
| 1042 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1043 | #define destroy_ring(dma, ring) do { \ |
| 1044 | b43_destroy_dmaring((dma)->ring, __stringify(ring)); \ |
| 1045 | (dma)->ring = NULL; \ |
| 1046 | } while (0) |
| 1047 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1048 | void b43_dma_free(struct b43_wldev *dev) |
| 1049 | { |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1050 | struct b43_dma *dma; |
| 1051 | |
| 1052 | if (b43_using_pio_transfers(dev)) |
| 1053 | return; |
| 1054 | dma = &dev->dma; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1055 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1056 | destroy_ring(dma, rx_ring); |
| 1057 | destroy_ring(dma, tx_ring_AC_BK); |
| 1058 | destroy_ring(dma, tx_ring_AC_BE); |
| 1059 | destroy_ring(dma, tx_ring_AC_VI); |
| 1060 | destroy_ring(dma, tx_ring_AC_VO); |
| 1061 | destroy_ring(dma, tx_ring_mcast); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1062 | } |
| 1063 | |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1064 | static int b43_dma_set_mask(struct b43_wldev *dev, u64 mask) |
| 1065 | { |
| 1066 | u64 orig_mask = mask; |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1067 | bool fallback = false; |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1068 | int err; |
| 1069 | |
| 1070 | /* Try to set the DMA mask. If it fails, try falling back to a |
| 1071 | * lower mask, as we can always also support a lower one. */ |
| 1072 | while (1) { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 1073 | err = dma_set_mask(dev->dev->dma_dev, mask); |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 1074 | if (!err) { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 1075 | err = dma_set_coherent_mask(dev->dev->dma_dev, mask); |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 1076 | if (!err) |
| 1077 | break; |
| 1078 | } |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 1079 | if (mask == DMA_BIT_MASK(64)) { |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 1080 | mask = DMA_BIT_MASK(32); |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1081 | fallback = true; |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1082 | continue; |
| 1083 | } |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 1084 | if (mask == DMA_BIT_MASK(32)) { |
Yang Hongyang | 28b7679 | 2009-04-06 19:01:17 -0700 | [diff] [blame] | 1085 | mask = DMA_BIT_MASK(30); |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1086 | fallback = true; |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1087 | continue; |
| 1088 | } |
| 1089 | b43err(dev->wl, "The machine/kernel does not support " |
| 1090 | "the required %u-bit DMA mask\n", |
| 1091 | (unsigned int)dma_mask_to_engine_type(orig_mask)); |
| 1092 | return -EOPNOTSUPP; |
| 1093 | } |
| 1094 | if (fallback) { |
| 1095 | b43info(dev->wl, "DMA mask fallback from %u-bit to %u-bit\n", |
| 1096 | (unsigned int)dma_mask_to_engine_type(orig_mask), |
| 1097 | (unsigned int)dma_mask_to_engine_type(mask)); |
| 1098 | } |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 1103 | /* Some hardware with 64-bit DMA seems to be bugged and looks for translation |
| 1104 | * bit in low address word instead of high one. |
| 1105 | */ |
| 1106 | static bool b43_dma_translation_in_low_word(struct b43_wldev *dev, |
| 1107 | enum b43_dmatype type) |
| 1108 | { |
| 1109 | if (type != B43_DMA_64BIT) |
| 1110 | return 1; |
| 1111 | |
| 1112 | #ifdef CONFIG_B43_SSB |
| 1113 | if (dev->dev->bus_type == B43_BUS_SSB && |
| 1114 | dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI && |
Bjorn Helgaas | dfcfb54 | 2012-05-08 17:27:32 -0600 | [diff] [blame] | 1115 | !(pci_is_pcie(dev->dev->sdev->bus->host_pci) && |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 1116 | ssb_read32(dev->dev->sdev, SSB_TMSHIGH) & SSB_TMSHIGH_DMA64)) |
| 1117 | return 1; |
| 1118 | #endif |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1122 | int b43_dma_init(struct b43_wldev *dev) |
| 1123 | { |
| 1124 | struct b43_dma *dma = &dev->dma; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1125 | int err; |
| 1126 | u64 dmamask; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 1127 | enum b43_dmatype type; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1128 | |
| 1129 | dmamask = supported_dma_mask(dev); |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1130 | type = dma_mask_to_engine_type(dmamask); |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1131 | err = b43_dma_set_mask(dev, dmamask); |
| 1132 | if (err) |
| 1133 | return err; |
Rafał Miłecki | 6cbab0d | 2011-07-06 15:45:26 +0200 | [diff] [blame] | 1134 | |
| 1135 | switch (dev->dev->bus_type) { |
Rafał Miłecki | eb90e9e | 2011-07-20 19:52:16 +0200 | [diff] [blame] | 1136 | #ifdef CONFIG_B43_BCMA |
| 1137 | case B43_BUS_BCMA: |
| 1138 | dma->translation = bcma_core_dma_translation(dev->dev->bdev); |
| 1139 | break; |
| 1140 | #endif |
Rafał Miłecki | 6cbab0d | 2011-07-06 15:45:26 +0200 | [diff] [blame] | 1141 | #ifdef CONFIG_B43_SSB |
| 1142 | case B43_BUS_SSB: |
| 1143 | dma->translation = ssb_dma_translation(dev->dev->sdev); |
| 1144 | break; |
| 1145 | #endif |
| 1146 | } |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 1147 | dma->translation_in_low = b43_dma_translation_in_low_word(dev, type); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1148 | |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 1149 | dma->parity = true; |
| 1150 | #ifdef CONFIG_B43_BCMA |
| 1151 | /* TODO: find out which SSB devices need disabling parity */ |
| 1152 | if (dev->dev->bus_type == B43_BUS_BCMA) |
| 1153 | dma->parity = false; |
| 1154 | #endif |
| 1155 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1156 | err = -ENOMEM; |
| 1157 | /* setup TX DMA channels. */ |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1158 | dma->tx_ring_AC_BK = b43_setup_dmaring(dev, 0, 1, type); |
| 1159 | if (!dma->tx_ring_AC_BK) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1160 | goto out; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1161 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1162 | dma->tx_ring_AC_BE = b43_setup_dmaring(dev, 1, 1, type); |
| 1163 | if (!dma->tx_ring_AC_BE) |
| 1164 | goto err_destroy_bk; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1165 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1166 | dma->tx_ring_AC_VI = b43_setup_dmaring(dev, 2, 1, type); |
| 1167 | if (!dma->tx_ring_AC_VI) |
| 1168 | goto err_destroy_be; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1169 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1170 | dma->tx_ring_AC_VO = b43_setup_dmaring(dev, 3, 1, type); |
| 1171 | if (!dma->tx_ring_AC_VO) |
| 1172 | goto err_destroy_vi; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1173 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1174 | dma->tx_ring_mcast = b43_setup_dmaring(dev, 4, 1, type); |
| 1175 | if (!dma->tx_ring_mcast) |
| 1176 | goto err_destroy_vo; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1177 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1178 | /* setup RX DMA channel. */ |
| 1179 | dma->rx_ring = b43_setup_dmaring(dev, 0, 0, type); |
| 1180 | if (!dma->rx_ring) |
| 1181 | goto err_destroy_mcast; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1182 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1183 | /* No support for the TX status DMA ring. */ |
Rafał Miłecki | 21d889d | 2011-05-18 02:06:38 +0200 | [diff] [blame] | 1184 | B43_WARN_ON(dev->dev->core_rev < 5); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1185 | |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 1186 | b43dbg(dev->wl, "%u-bit DMA initialized\n", |
| 1187 | (unsigned int)type); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1188 | err = 0; |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1189 | out: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1190 | return err; |
| 1191 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1192 | err_destroy_mcast: |
| 1193 | destroy_ring(dma, tx_ring_mcast); |
| 1194 | err_destroy_vo: |
| 1195 | destroy_ring(dma, tx_ring_AC_VO); |
| 1196 | err_destroy_vi: |
| 1197 | destroy_ring(dma, tx_ring_AC_VI); |
| 1198 | err_destroy_be: |
| 1199 | destroy_ring(dma, tx_ring_AC_BE); |
| 1200 | err_destroy_bk: |
| 1201 | destroy_ring(dma, tx_ring_AC_BK); |
| 1202 | return err; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | /* Generate a cookie for the TX header. */ |
| 1206 | static u16 generate_cookie(struct b43_dmaring *ring, int slot) |
| 1207 | { |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1208 | u16 cookie; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1209 | |
| 1210 | /* Use the upper 4 bits of the cookie as |
| 1211 | * DMA controller ID and store the slot number |
| 1212 | * in the lower 12 bits. |
| 1213 | * Note that the cookie must never be 0, as this |
| 1214 | * is a special value used in RX path. |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1215 | * It can also not be 0xFFFF because that is special |
| 1216 | * for multicast frames. |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1217 | */ |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1218 | cookie = (((u16)ring->index + 1) << 12); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1219 | B43_WARN_ON(slot & ~0x0FFF); |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1220 | cookie |= (u16)slot; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1221 | |
| 1222 | return cookie; |
| 1223 | } |
| 1224 | |
| 1225 | /* Inspect a cookie and find out to which controller/slot it belongs. */ |
| 1226 | static |
| 1227 | struct b43_dmaring *parse_cookie(struct b43_wldev *dev, u16 cookie, int *slot) |
| 1228 | { |
| 1229 | struct b43_dma *dma = &dev->dma; |
| 1230 | struct b43_dmaring *ring = NULL; |
| 1231 | |
| 1232 | switch (cookie & 0xF000) { |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1233 | case 0x1000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1234 | ring = dma->tx_ring_AC_BK; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1235 | break; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1236 | case 0x2000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1237 | ring = dma->tx_ring_AC_BE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1238 | break; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1239 | case 0x3000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1240 | ring = dma->tx_ring_AC_VI; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1241 | break; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1242 | case 0x4000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1243 | ring = dma->tx_ring_AC_VO; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1244 | break; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1245 | case 0x5000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1246 | ring = dma->tx_ring_mcast; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1247 | break; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1248 | } |
| 1249 | *slot = (cookie & 0x0FFF); |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1250 | if (unlikely(!ring || *slot < 0 || *slot >= ring->nr_slots)) { |
| 1251 | b43dbg(dev->wl, "TX-status contains " |
| 1252 | "invalid cookie: 0x%04X\n", cookie); |
| 1253 | return NULL; |
| 1254 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1255 | |
| 1256 | return ring; |
| 1257 | } |
| 1258 | |
| 1259 | static int dma_tx_fragment(struct b43_dmaring *ring, |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1260 | struct sk_buff *skb) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1261 | { |
| 1262 | const struct b43_dma_ops *ops = ring->ops; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1263 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1264 | struct b43_private_tx_info *priv_info = b43_get_priv_tx_info(info); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1265 | u8 *header; |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1266 | int slot, old_top_slot, old_used_slots; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1267 | int err; |
| 1268 | struct b43_dmadesc_generic *desc; |
| 1269 | struct b43_dmadesc_meta *meta; |
| 1270 | struct b43_dmadesc_meta *meta_hdr; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1271 | u16 cookie; |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1272 | size_t hdrsize = b43_txhdr_size(ring->dev); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1273 | |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 1274 | /* Important note: If the number of used DMA slots per TX frame |
| 1275 | * is changed here, the TX_SLOTS_PER_FRAME definition at the top of |
| 1276 | * the file has to be updated, too! |
| 1277 | */ |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1278 | |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1279 | old_top_slot = ring->current_slot; |
| 1280 | old_used_slots = ring->used_slots; |
| 1281 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1282 | /* Get a slot for the header. */ |
| 1283 | slot = request_slot(ring); |
| 1284 | desc = ops->idx2desc(ring, slot, &meta_hdr); |
| 1285 | memset(meta_hdr, 0, sizeof(*meta_hdr)); |
| 1286 | |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 1287 | header = &(ring->txhdr_cache[(slot / TX_SLOTS_PER_FRAME) * hdrsize]); |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1288 | cookie = generate_cookie(ring, slot); |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1289 | err = b43_generate_txhdr(ring->dev, header, |
gregor kowski | 035d024 | 2009-08-19 22:35:45 +0200 | [diff] [blame] | 1290 | skb, info, cookie); |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1291 | if (unlikely(err)) { |
| 1292 | ring->current_slot = old_top_slot; |
| 1293 | ring->used_slots = old_used_slots; |
| 1294 | return err; |
| 1295 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1296 | |
| 1297 | meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header, |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1298 | hdrsize, 1); |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 1299 | if (b43_dma_mapping_error(ring, meta_hdr->dmaaddr, hdrsize, 1)) { |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1300 | ring->current_slot = old_top_slot; |
| 1301 | ring->used_slots = old_used_slots; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1302 | return -EIO; |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1303 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1304 | ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr, |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1305 | hdrsize, 1, 0, 0); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1306 | |
| 1307 | /* Get a slot for the payload. */ |
| 1308 | slot = request_slot(ring); |
| 1309 | desc = ops->idx2desc(ring, slot, &meta); |
| 1310 | memset(meta, 0, sizeof(*meta)); |
| 1311 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1312 | meta->skb = skb; |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1313 | meta->is_last_fragment = true; |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1314 | priv_info->bouncebuffer = NULL; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1315 | |
| 1316 | meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1); |
| 1317 | /* create a bounce buffer in zone_dma on mapping failure. */ |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 1318 | if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) { |
Julia Lawall | a61aac7 | 2010-05-15 23:20:26 +0200 | [diff] [blame] | 1319 | priv_info->bouncebuffer = kmemdup(skb->data, skb->len, |
| 1320 | GFP_ATOMIC | GFP_DMA); |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1321 | if (!priv_info->bouncebuffer) { |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1322 | ring->current_slot = old_top_slot; |
| 1323 | ring->used_slots = old_used_slots; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1324 | err = -ENOMEM; |
| 1325 | goto out_unmap_hdr; |
| 1326 | } |
| 1327 | |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1328 | meta->dmaaddr = map_descbuffer(ring, priv_info->bouncebuffer, skb->len, 1); |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 1329 | if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) { |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1330 | kfree(priv_info->bouncebuffer); |
| 1331 | priv_info->bouncebuffer = NULL; |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1332 | ring->current_slot = old_top_slot; |
| 1333 | ring->used_slots = old_used_slots; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1334 | err = -EIO; |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1335 | goto out_unmap_hdr; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | ops->fill_descriptor(ring, desc, meta->dmaaddr, skb->len, 0, 1, 1); |
| 1340 | |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1341 | if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) { |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1342 | /* Tell the firmware about the cookie of the last |
| 1343 | * mcast frame, so it can clear the more-data bit in it. */ |
| 1344 | b43_shm_write16(ring->dev, B43_SHM_SHARED, |
| 1345 | B43_SHM_SH_MCASTCOOKIE, cookie); |
| 1346 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1347 | /* Now transfer the whole frame. */ |
| 1348 | wmb(); |
| 1349 | ops->poke_tx(ring, next_slot(ring, slot)); |
| 1350 | return 0; |
| 1351 | |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1352 | out_unmap_hdr: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1353 | unmap_descbuffer(ring, meta_hdr->dmaaddr, |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1354 | hdrsize, 1); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1355 | return err; |
| 1356 | } |
| 1357 | |
| 1358 | static inline int should_inject_overflow(struct b43_dmaring *ring) |
| 1359 | { |
| 1360 | #ifdef CONFIG_B43_DEBUG |
| 1361 | if (unlikely(b43_debug(ring->dev, B43_DBG_DMAOVERFLOW))) { |
| 1362 | /* Check if we should inject another ringbuffer overflow |
| 1363 | * to test handling of this situation in the stack. */ |
| 1364 | unsigned long next_overflow; |
| 1365 | |
| 1366 | next_overflow = ring->last_injected_overflow + HZ; |
| 1367 | if (time_after(jiffies, next_overflow)) { |
| 1368 | ring->last_injected_overflow = jiffies; |
| 1369 | b43dbg(ring->dev->wl, |
| 1370 | "Injecting TX ring overflow on " |
| 1371 | "DMA controller %d\n", ring->index); |
| 1372 | return 1; |
| 1373 | } |
| 1374 | } |
| 1375 | #endif /* CONFIG_B43_DEBUG */ |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1379 | /* Static mapping of mac80211's queues (priorities) to b43 DMA rings. */ |
John Daiker | 99da185 | 2009-02-24 02:16:42 -0800 | [diff] [blame] | 1380 | static struct b43_dmaring *select_ring_by_priority(struct b43_wldev *dev, |
| 1381 | u8 queue_prio) |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1382 | { |
| 1383 | struct b43_dmaring *ring; |
| 1384 | |
Michael Buesch | 403a3a1 | 2009-06-08 21:04:57 +0200 | [diff] [blame] | 1385 | if (dev->qos_enabled) { |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1386 | /* 0 = highest priority */ |
| 1387 | switch (queue_prio) { |
| 1388 | default: |
| 1389 | B43_WARN_ON(1); |
| 1390 | /* fallthrough */ |
| 1391 | case 0: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1392 | ring = dev->dma.tx_ring_AC_VO; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1393 | break; |
| 1394 | case 1: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1395 | ring = dev->dma.tx_ring_AC_VI; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1396 | break; |
| 1397 | case 2: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1398 | ring = dev->dma.tx_ring_AC_BE; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1399 | break; |
| 1400 | case 3: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1401 | ring = dev->dma.tx_ring_AC_BK; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1402 | break; |
| 1403 | } |
| 1404 | } else |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1405 | ring = dev->dma.tx_ring_AC_BE; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1406 | |
| 1407 | return ring; |
| 1408 | } |
| 1409 | |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1410 | int b43_dma_tx(struct b43_wldev *dev, struct sk_buff *skb) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1411 | { |
| 1412 | struct b43_dmaring *ring; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1413 | struct ieee80211_hdr *hdr; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1414 | int err = 0; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1415 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1416 | |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1417 | hdr = (struct ieee80211_hdr *)skb->data; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1418 | if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) { |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1419 | /* The multicast ring will be sent after the DTIM */ |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1420 | ring = dev->dma.tx_ring_mcast; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1421 | /* Set the more-data bit. Ucode will clear it on |
| 1422 | * the last frame for us. */ |
| 1423 | hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA); |
| 1424 | } else { |
| 1425 | /* Decide by priority where to put this frame. */ |
Johannes Berg | e253008 | 2008-05-17 00:57:14 +0200 | [diff] [blame] | 1426 | ring = select_ring_by_priority( |
| 1427 | dev, skb_get_queue_mapping(skb)); |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1428 | } |
| 1429 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1430 | B43_WARN_ON(!ring->tx); |
Michael Buesch | ca2d559 | 2009-02-19 20:17:36 +0100 | [diff] [blame] | 1431 | |
Larry Finger | 18c6951 | 2009-07-29 10:54:06 -0500 | [diff] [blame] | 1432 | if (unlikely(ring->stopped)) { |
| 1433 | /* We get here only because of a bug in mac80211. |
| 1434 | * Because of a race, one packet may be queued after |
| 1435 | * the queue is stopped, thus we got called when we shouldn't. |
| 1436 | * For now, just refuse the transmit. */ |
| 1437 | if (b43_debug(dev, B43_DBG_DMAVERBOSE)) |
| 1438 | b43err(dev->wl, "Packet after queue stopped\n"); |
| 1439 | err = -ENOSPC; |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1440 | goto out; |
Larry Finger | 18c6951 | 2009-07-29 10:54:06 -0500 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | if (unlikely(WARN_ON(free_slots(ring) < TX_SLOTS_PER_FRAME))) { |
| 1444 | /* If we get here, we have a real error with the queue |
| 1445 | * full, but queues not stopped. */ |
| 1446 | b43err(dev->wl, "DMA queue overflow\n"); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1447 | err = -ENOSPC; |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1448 | goto out; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1449 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1450 | |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1451 | /* Assign the queue number to the ring (if not already done before) |
| 1452 | * so TX status handling can use it. The queue to ring mapping is |
| 1453 | * static, so we don't need to store it per frame. */ |
Johannes Berg | e253008 | 2008-05-17 00:57:14 +0200 | [diff] [blame] | 1454 | ring->queue_prio = skb_get_queue_mapping(skb); |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1455 | |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1456 | err = dma_tx_fragment(ring, skb); |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1457 | if (unlikely(err == -ENOKEY)) { |
| 1458 | /* Drop this packet, as we don't have the encryption key |
| 1459 | * anymore and must not transmit it unencrypted. */ |
Felix Fietkau | 78f18df | 2012-12-10 17:40:21 +0100 | [diff] [blame] | 1460 | ieee80211_free_txskb(dev->wl->hw, skb); |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1461 | err = 0; |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1462 | goto out; |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1463 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1464 | if (unlikely(err)) { |
| 1465 | b43err(dev->wl, "DMA tx mapping failure\n"); |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1466 | goto out; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1467 | } |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 1468 | if ((free_slots(ring) < TX_SLOTS_PER_FRAME) || |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1469 | should_inject_overflow(ring)) { |
| 1470 | /* This TX ring is full. */ |
francesco.gringoli@ing.unibs.it | bad6919 | 2011-12-16 18:34:56 +0100 | [diff] [blame] | 1471 | unsigned int skb_mapping = skb_get_queue_mapping(skb); |
| 1472 | ieee80211_stop_queue(dev->wl->hw, skb_mapping); |
| 1473 | dev->wl->tx_queue_stopped[skb_mapping] = 1; |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1474 | ring->stopped = true; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1475 | if (b43_debug(dev, B43_DBG_DMAVERBOSE)) { |
| 1476 | b43dbg(dev->wl, "Stopped TX ring %d\n", ring->index); |
| 1477 | } |
| 1478 | } |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1479 | out: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1480 | |
| 1481 | return err; |
| 1482 | } |
| 1483 | |
| 1484 | void b43_dma_handle_txstatus(struct b43_wldev *dev, |
| 1485 | const struct b43_txstatus *status) |
| 1486 | { |
| 1487 | const struct b43_dma_ops *ops; |
| 1488 | struct b43_dmaring *ring; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1489 | struct b43_dmadesc_meta *meta; |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1490 | static const struct b43_txstatus fake; /* filled with 0 */ |
| 1491 | const struct b43_txstatus *txstat; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1492 | int slot, firstused; |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1493 | bool frame_succeed; |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1494 | int skip; |
| 1495 | static u8 err_out1, err_out2; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1496 | |
| 1497 | ring = parse_cookie(dev, status->cookie, &slot); |
| 1498 | if (unlikely(!ring)) |
| 1499 | return; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1500 | B43_WARN_ON(!ring->tx); |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1501 | |
| 1502 | /* Sanity check: TX packets are processed in-order on one ring. |
| 1503 | * Check if the slot deduced from the cookie really is the first |
| 1504 | * used slot. */ |
| 1505 | firstused = ring->current_slot - ring->used_slots + 1; |
| 1506 | if (firstused < 0) |
| 1507 | firstused = ring->nr_slots + firstused; |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1508 | |
| 1509 | skip = 0; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1510 | if (unlikely(slot != firstused)) { |
| 1511 | /* This possibly is a firmware bug and will result in |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1512 | * malfunction, memory leaks and/or stall of DMA functionality. |
| 1513 | */ |
| 1514 | if (slot == next_slot(ring, next_slot(ring, firstused))) { |
| 1515 | /* If a single header/data pair was missed, skip over |
| 1516 | * the first two slots in an attempt to recover. |
| 1517 | */ |
| 1518 | slot = firstused; |
| 1519 | skip = 2; |
| 1520 | if (!err_out1) { |
| 1521 | /* Report the error once. */ |
| 1522 | b43dbg(dev->wl, |
| 1523 | "Skip on DMA ring %d slot %d.\n", |
| 1524 | ring->index, slot); |
| 1525 | err_out1 = 1; |
| 1526 | } |
| 1527 | } else { |
| 1528 | /* More than a single header/data pair were missed. |
| 1529 | * Report this error once. |
| 1530 | */ |
| 1531 | if (!err_out2) |
| 1532 | b43dbg(dev->wl, |
| 1533 | "Out of order TX status report on DMA ring %d. Expected %d, but got %d\n", |
| 1534 | ring->index, firstused, slot); |
| 1535 | err_out2 = 1; |
| 1536 | return; |
| 1537 | } |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1538 | } |
| 1539 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1540 | ops = ring->ops; |
| 1541 | while (1) { |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1542 | B43_WARN_ON(slot < 0 || slot >= ring->nr_slots); |
Larry Finger | 9c1cacd | 2011-05-22 20:54:25 -0500 | [diff] [blame] | 1543 | /* get meta - ignore returned value */ |
| 1544 | ops->idx2desc(ring, slot, &meta); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1545 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1546 | if (b43_dma_ptr_is_poisoned(meta->skb)) { |
| 1547 | b43dbg(dev->wl, "Poisoned TX slot %d (first=%d) " |
| 1548 | "on ring %d\n", |
| 1549 | slot, firstused, ring->index); |
| 1550 | break; |
| 1551 | } |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1552 | |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1553 | if (meta->skb) { |
| 1554 | struct b43_private_tx_info *priv_info = |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1555 | b43_get_priv_tx_info(IEEE80211_SKB_CB(meta->skb)); |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1556 | |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1557 | unmap_descbuffer(ring, meta->dmaaddr, |
| 1558 | meta->skb->len, 1); |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1559 | kfree(priv_info->bouncebuffer); |
| 1560 | priv_info->bouncebuffer = NULL; |
| 1561 | } else { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1562 | unmap_descbuffer(ring, meta->dmaaddr, |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1563 | b43_txhdr_size(dev), 1); |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1564 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1565 | |
| 1566 | if (meta->is_last_fragment) { |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1567 | struct ieee80211_tx_info *info; |
| 1568 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1569 | if (unlikely(!meta->skb)) { |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1570 | /* This is a scatter-gather fragment of a frame, |
| 1571 | * so the skb pointer must not be NULL. |
| 1572 | */ |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1573 | b43dbg(dev->wl, "TX status unexpected NULL skb " |
| 1574 | "at slot %d (first=%d) on ring %d\n", |
| 1575 | slot, firstused, ring->index); |
| 1576 | break; |
| 1577 | } |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1578 | |
| 1579 | info = IEEE80211_SKB_CB(meta->skb); |
| 1580 | |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1581 | /* |
| 1582 | * Call back to inform the ieee80211 subsystem about |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1583 | * the status of the transmission. When skipping over |
| 1584 | * a missed TX status report, use a status structure |
| 1585 | * filled with zeros to indicate that the frame was not |
| 1586 | * sent (frame_count 0) and not acknowledged |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1587 | */ |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1588 | if (unlikely(skip)) |
| 1589 | txstat = &fake; |
| 1590 | else |
| 1591 | txstat = status; |
| 1592 | |
| 1593 | frame_succeed = b43_fill_txstatus_report(dev, info, |
| 1594 | txstat); |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1595 | #ifdef CONFIG_B43_DEBUG |
| 1596 | if (frame_succeed) |
| 1597 | ring->nr_succeed_tx_packets++; |
| 1598 | else |
| 1599 | ring->nr_failed_tx_packets++; |
| 1600 | ring->nr_total_packet_tries += status->frame_count; |
| 1601 | #endif /* DEBUG */ |
Michael Buesch | ce6c4a1 | 2009-09-10 20:22:02 +0200 | [diff] [blame] | 1602 | ieee80211_tx_status(dev->wl->hw, meta->skb); |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1603 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1604 | /* skb will be freed by ieee80211_tx_status(). |
| 1605 | * Poison our pointer. */ |
| 1606 | meta->skb = B43_DMA_PTR_POISON; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1607 | } else { |
| 1608 | /* No need to call free_descriptor_buffer here, as |
| 1609 | * this is only the txhdr, which is not allocated. |
| 1610 | */ |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1611 | if (unlikely(meta->skb)) { |
| 1612 | b43dbg(dev->wl, "TX status unexpected non-NULL skb " |
| 1613 | "at slot %d (first=%d) on ring %d\n", |
| 1614 | slot, firstused, ring->index); |
| 1615 | break; |
| 1616 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | /* Everything unmapped and free'd. So it's not used anymore. */ |
| 1620 | ring->used_slots--; |
| 1621 | |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1622 | if (meta->is_last_fragment && !skip) { |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1623 | /* This is the last scatter-gather |
| 1624 | * fragment of the frame. We are done. */ |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1625 | break; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1626 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1627 | slot = next_slot(ring, slot); |
Iestyn C. Elfick | b251412 | 2013-03-20 14:02:31 -0500 | [diff] [blame] | 1628 | if (skip > 0) |
| 1629 | --skip; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1630 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1631 | if (ring->stopped) { |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 1632 | B43_WARN_ON(free_slots(ring) < TX_SLOTS_PER_FRAME); |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1633 | ring->stopped = false; |
francesco.gringoli@ing.unibs.it | bad6919 | 2011-12-16 18:34:56 +0100 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | if (dev->wl->tx_queue_stopped[ring->queue_prio]) { |
| 1637 | dev->wl->tx_queue_stopped[ring->queue_prio] = 0; |
| 1638 | } else { |
| 1639 | /* If the driver queue is running wake the corresponding |
| 1640 | * mac80211 queue. */ |
| 1641 | ieee80211_wake_queue(dev->wl->hw, ring->queue_prio); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1642 | if (b43_debug(dev, B43_DBG_DMAVERBOSE)) { |
| 1643 | b43dbg(dev->wl, "Woke up TX ring %d\n", ring->index); |
| 1644 | } |
| 1645 | } |
francesco.gringoli@ing.unibs.it | bad6919 | 2011-12-16 18:34:56 +0100 | [diff] [blame] | 1646 | /* Add work to the queue. */ |
| 1647 | ieee80211_queue_work(dev->wl->hw, &dev->wl->tx_work); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1648 | } |
| 1649 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1650 | static void dma_rx(struct b43_dmaring *ring, int *slot) |
| 1651 | { |
| 1652 | const struct b43_dma_ops *ops = ring->ops; |
| 1653 | struct b43_dmadesc_generic *desc; |
| 1654 | struct b43_dmadesc_meta *meta; |
| 1655 | struct b43_rxhdr_fw4 *rxhdr; |
| 1656 | struct sk_buff *skb; |
| 1657 | u16 len; |
| 1658 | int err; |
| 1659 | dma_addr_t dmaaddr; |
| 1660 | |
| 1661 | desc = ops->idx2desc(ring, *slot, &meta); |
| 1662 | |
| 1663 | sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize); |
| 1664 | skb = meta->skb; |
| 1665 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1666 | rxhdr = (struct b43_rxhdr_fw4 *)skb->data; |
| 1667 | len = le16_to_cpu(rxhdr->frame_len); |
| 1668 | if (len == 0) { |
| 1669 | int i = 0; |
| 1670 | |
| 1671 | do { |
| 1672 | udelay(2); |
| 1673 | barrier(); |
| 1674 | len = le16_to_cpu(rxhdr->frame_len); |
| 1675 | } while (len == 0 && i++ < 5); |
| 1676 | if (unlikely(len == 0)) { |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1677 | dmaaddr = meta->dmaaddr; |
| 1678 | goto drop_recycle_buffer; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1679 | } |
| 1680 | } |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 1681 | if (unlikely(b43_rx_buffer_is_poisoned(ring, skb))) { |
| 1682 | /* Something went wrong with the DMA. |
| 1683 | * The device did not touch the buffer and did not overwrite the poison. */ |
| 1684 | b43dbg(ring->dev->wl, "DMA RX: Dropping poisoned buffer.\n"); |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1685 | dmaaddr = meta->dmaaddr; |
| 1686 | goto drop_recycle_buffer; |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 1687 | } |
John W. Linville | c85ce65 | 2011-03-30 14:02:46 -0400 | [diff] [blame] | 1688 | if (unlikely(len + ring->frameoffset > ring->rx_buffersize)) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1689 | /* The data did not fit into one descriptor buffer |
| 1690 | * and is split over multiple buffers. |
| 1691 | * This should never happen, as we try to allocate buffers |
| 1692 | * big enough. So simply ignore this packet. |
| 1693 | */ |
| 1694 | int cnt = 0; |
| 1695 | s32 tmp = len; |
| 1696 | |
| 1697 | while (1) { |
| 1698 | desc = ops->idx2desc(ring, *slot, &meta); |
| 1699 | /* recycle the descriptor buffer. */ |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1700 | b43_poison_rx_buffer(ring, meta->skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1701 | sync_descbuffer_for_device(ring, meta->dmaaddr, |
| 1702 | ring->rx_buffersize); |
| 1703 | *slot = next_slot(ring, *slot); |
| 1704 | cnt++; |
| 1705 | tmp -= ring->rx_buffersize; |
| 1706 | if (tmp <= 0) |
| 1707 | break; |
| 1708 | } |
| 1709 | b43err(ring->dev->wl, "DMA RX buffer too small " |
| 1710 | "(len: %u, buffer: %u, nr-dropped: %d)\n", |
| 1711 | len, ring->rx_buffersize, cnt); |
| 1712 | goto drop; |
| 1713 | } |
| 1714 | |
| 1715 | dmaaddr = meta->dmaaddr; |
| 1716 | err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC); |
| 1717 | if (unlikely(err)) { |
| 1718 | b43dbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer() failed\n"); |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1719 | goto drop_recycle_buffer; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0); |
| 1723 | skb_put(skb, len + ring->frameoffset); |
| 1724 | skb_pull(skb, ring->frameoffset); |
| 1725 | |
| 1726 | b43_rx(ring->dev, skb, rxhdr); |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1727 | drop: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1728 | return; |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1729 | |
| 1730 | drop_recycle_buffer: |
| 1731 | /* Poison and recycle the RX buffer. */ |
| 1732 | b43_poison_rx_buffer(ring, skb); |
| 1733 | sync_descbuffer_for_device(ring, dmaaddr, ring->rx_buffersize); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | void b43_dma_rx(struct b43_dmaring *ring) |
| 1737 | { |
| 1738 | const struct b43_dma_ops *ops = ring->ops; |
| 1739 | int slot, current_slot; |
| 1740 | int used_slots = 0; |
| 1741 | |
| 1742 | B43_WARN_ON(ring->tx); |
| 1743 | current_slot = ops->get_current_rxslot(ring); |
| 1744 | B43_WARN_ON(!(current_slot >= 0 && current_slot < ring->nr_slots)); |
| 1745 | |
| 1746 | slot = ring->current_slot; |
| 1747 | for (; slot != current_slot; slot = next_slot(ring, slot)) { |
| 1748 | dma_rx(ring, &slot); |
| 1749 | update_max_used_slots(ring, ++used_slots); |
| 1750 | } |
Michael Büsch | 73e6cdc | 2011-07-04 19:51:11 +0200 | [diff] [blame] | 1751 | wmb(); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1752 | ops->set_current_rxslot(ring, slot); |
| 1753 | ring->current_slot = slot; |
| 1754 | } |
| 1755 | |
| 1756 | static void b43_dma_tx_suspend_ring(struct b43_dmaring *ring) |
| 1757 | { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1758 | B43_WARN_ON(!ring->tx); |
| 1759 | ring->ops->tx_suspend(ring); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | static void b43_dma_tx_resume_ring(struct b43_dmaring *ring) |
| 1763 | { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1764 | B43_WARN_ON(!ring->tx); |
| 1765 | ring->ops->tx_resume(ring); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | void b43_dma_tx_suspend(struct b43_wldev *dev) |
| 1769 | { |
| 1770 | b43_power_saving_ctl_bits(dev, B43_PS_AWAKE); |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1771 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BK); |
| 1772 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BE); |
| 1773 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VI); |
| 1774 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VO); |
| 1775 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_mcast); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | void b43_dma_tx_resume(struct b43_wldev *dev) |
| 1779 | { |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1780 | b43_dma_tx_resume_ring(dev->dma.tx_ring_mcast); |
| 1781 | b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VO); |
| 1782 | b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VI); |
| 1783 | b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BE); |
| 1784 | b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BK); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1785 | b43_power_saving_ctl_bits(dev, 0); |
| 1786 | } |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1787 | |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1788 | static void direct_fifo_rx(struct b43_wldev *dev, enum b43_dmatype type, |
| 1789 | u16 mmio_base, bool enable) |
| 1790 | { |
| 1791 | u32 ctl; |
| 1792 | |
| 1793 | if (type == B43_DMA_64BIT) { |
| 1794 | ctl = b43_read32(dev, mmio_base + B43_DMA64_RXCTL); |
| 1795 | ctl &= ~B43_DMA64_RXDIRECTFIFO; |
| 1796 | if (enable) |
| 1797 | ctl |= B43_DMA64_RXDIRECTFIFO; |
| 1798 | b43_write32(dev, mmio_base + B43_DMA64_RXCTL, ctl); |
| 1799 | } else { |
| 1800 | ctl = b43_read32(dev, mmio_base + B43_DMA32_RXCTL); |
| 1801 | ctl &= ~B43_DMA32_RXDIRECTFIFO; |
| 1802 | if (enable) |
| 1803 | ctl |= B43_DMA32_RXDIRECTFIFO; |
| 1804 | b43_write32(dev, mmio_base + B43_DMA32_RXCTL, ctl); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | /* Enable/Disable Direct FIFO Receive Mode (PIO) on a RX engine. |
| 1809 | * This is called from PIO code, so DMA structures are not available. */ |
| 1810 | void b43_dma_direct_fifo_rx(struct b43_wldev *dev, |
| 1811 | unsigned int engine_index, bool enable) |
| 1812 | { |
| 1813 | enum b43_dmatype type; |
| 1814 | u16 mmio_base; |
| 1815 | |
| 1816 | type = dma_mask_to_engine_type(supported_dma_mask(dev)); |
| 1817 | |
| 1818 | mmio_base = b43_dmacontroller_base(type, engine_index); |
| 1819 | direct_fifo_rx(dev, type, mmio_base, enable); |
| 1820 | } |