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) { |
| 412 | dev_kfree_skb_any(meta->skb); |
| 413 | meta->skb = NULL; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | static int alloc_ringmemory(struct b43_dmaring *ring) |
| 418 | { |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 419 | gfp_t flags = GFP_KERNEL; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 420 | |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 421 | /* 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] | 422 | * alignment and 8K buffers for 64-bit DMA with 8K alignment. |
| 423 | * In practice we could use smaller buffers for the latter, but the |
| 424 | * alignment is really important because of the hardware bug. If bit |
| 425 | * 0x00001000 is used in DMA address, some hardware (like BCM4331) |
| 426 | * copies that bit into B43_DMA64_RXSTATUS and we get false values from |
| 427 | * B43_DMA64_RXSTATDPTR. Let's just use 8K buffers even if we don't use |
| 428 | * more than 256 slots for ring. |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 429 | */ |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 430 | u16 ring_mem_size = (ring->type == B43_DMA_64BIT) ? |
| 431 | B43_DMA64_RINGMEMSIZE : B43_DMA32_RINGMEMSIZE; |
| 432 | |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 433 | ring->descbase = dma_alloc_coherent(ring->dev->dev->dma_dev, |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 434 | ring_mem_size, &(ring->dmabase), |
| 435 | flags); |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 436 | if (!ring->descbase) { |
| 437 | b43err(ring->dev->wl, "DMA ringmemory allocation failed\n"); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 438 | return -ENOMEM; |
| 439 | } |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 440 | memset(ring->descbase, 0, ring_mem_size); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static void free_ringmemory(struct b43_dmaring *ring) |
| 446 | { |
Rafał Miłecki | 14a8083 | 2011-08-26 20:41:39 +0200 | [diff] [blame] | 447 | u16 ring_mem_size = (ring->type == B43_DMA_64BIT) ? |
| 448 | B43_DMA64_RINGMEMSIZE : B43_DMA32_RINGMEMSIZE; |
| 449 | dma_free_coherent(ring->dev->dev->dma_dev, ring_mem_size, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 450 | ring->descbase, ring->dmabase); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* Reset the RX DMA channel */ |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 454 | static int b43_dmacontroller_rx_reset(struct b43_wldev *dev, u16 mmio_base, |
| 455 | enum b43_dmatype type) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 456 | { |
| 457 | int i; |
| 458 | u32 value; |
| 459 | u16 offset; |
| 460 | |
| 461 | might_sleep(); |
| 462 | |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 463 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXCTL : B43_DMA32_RXCTL; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 464 | b43_write32(dev, mmio_base + offset, 0); |
| 465 | for (i = 0; i < 10; i++) { |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 466 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXSTATUS : |
| 467 | B43_DMA32_RXSTATUS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 468 | value = b43_read32(dev, mmio_base + offset); |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 469 | if (type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 470 | value &= B43_DMA64_RXSTAT; |
| 471 | if (value == B43_DMA64_RXSTAT_DISABLED) { |
| 472 | i = -1; |
| 473 | break; |
| 474 | } |
| 475 | } else { |
| 476 | value &= B43_DMA32_RXSTATE; |
| 477 | if (value == B43_DMA32_RXSTAT_DISABLED) { |
| 478 | i = -1; |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | msleep(1); |
| 483 | } |
| 484 | if (i != -1) { |
| 485 | b43err(dev->wl, "DMA RX reset timed out\n"); |
| 486 | return -ENODEV; |
| 487 | } |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 492 | /* Reset the TX DMA channel */ |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 493 | static int b43_dmacontroller_tx_reset(struct b43_wldev *dev, u16 mmio_base, |
| 494 | enum b43_dmatype type) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 495 | { |
| 496 | int i; |
| 497 | u32 value; |
| 498 | u16 offset; |
| 499 | |
| 500 | might_sleep(); |
| 501 | |
| 502 | for (i = 0; i < 10; i++) { |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 503 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS : |
| 504 | B43_DMA32_TXSTATUS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 505 | value = b43_read32(dev, mmio_base + offset); |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 506 | if (type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 507 | value &= B43_DMA64_TXSTAT; |
| 508 | if (value == B43_DMA64_TXSTAT_DISABLED || |
| 509 | value == B43_DMA64_TXSTAT_IDLEWAIT || |
| 510 | value == B43_DMA64_TXSTAT_STOPPED) |
| 511 | break; |
| 512 | } else { |
| 513 | value &= B43_DMA32_TXSTATE; |
| 514 | if (value == B43_DMA32_TXSTAT_DISABLED || |
| 515 | value == B43_DMA32_TXSTAT_IDLEWAIT || |
| 516 | value == B43_DMA32_TXSTAT_STOPPED) |
| 517 | break; |
| 518 | } |
| 519 | msleep(1); |
| 520 | } |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 521 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXCTL : B43_DMA32_TXCTL; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 522 | b43_write32(dev, mmio_base + offset, 0); |
| 523 | for (i = 0; i < 10; i++) { |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 524 | offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS : |
| 525 | B43_DMA32_TXSTATUS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 526 | value = b43_read32(dev, mmio_base + offset); |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 527 | if (type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 528 | value &= B43_DMA64_TXSTAT; |
| 529 | if (value == B43_DMA64_TXSTAT_DISABLED) { |
| 530 | i = -1; |
| 531 | break; |
| 532 | } |
| 533 | } else { |
| 534 | value &= B43_DMA32_TXSTATE; |
| 535 | if (value == B43_DMA32_TXSTAT_DISABLED) { |
| 536 | i = -1; |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | msleep(1); |
| 541 | } |
| 542 | if (i != -1) { |
| 543 | b43err(dev->wl, "DMA TX reset timed out\n"); |
| 544 | return -ENODEV; |
| 545 | } |
| 546 | /* ensure the reset is completed. */ |
| 547 | msleep(1); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 552 | /* Check if a DMA mapping address is invalid. */ |
| 553 | static bool b43_dma_mapping_error(struct b43_dmaring *ring, |
| 554 | dma_addr_t addr, |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 555 | size_t buffersize, bool dma_to_device) |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 556 | { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 557 | if (unlikely(dma_mapping_error(ring->dev->dev->dma_dev, addr))) |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 558 | return 1; |
| 559 | |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 560 | switch (ring->type) { |
| 561 | case B43_DMA_30BIT: |
| 562 | if ((u64)addr + buffersize > (1ULL << 30)) |
| 563 | goto address_error; |
| 564 | break; |
| 565 | case B43_DMA_32BIT: |
| 566 | if ((u64)addr + buffersize > (1ULL << 32)) |
| 567 | goto address_error; |
| 568 | break; |
| 569 | case B43_DMA_64BIT: |
| 570 | /* Currently we can't have addresses beyond |
| 571 | * 64bit in the kernel. */ |
| 572 | break; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* The address is OK. */ |
| 576 | return 0; |
John W. Linville | 55afc80 | 2009-12-29 14:07:42 -0500 | [diff] [blame] | 577 | |
| 578 | address_error: |
| 579 | /* We can't support this address. Unmap it again. */ |
| 580 | unmap_descbuffer(ring, addr, buffersize, dma_to_device); |
| 581 | |
| 582 | return 1; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 585 | static bool b43_rx_buffer_is_poisoned(struct b43_dmaring *ring, struct sk_buff *skb) |
| 586 | { |
| 587 | unsigned char *f = skb->data + ring->frameoffset; |
| 588 | |
| 589 | return ((f[0] & f[1] & f[2] & f[3] & f[4] & f[5] & f[6] & f[7]) == 0xFF); |
| 590 | } |
| 591 | |
| 592 | static void b43_poison_rx_buffer(struct b43_dmaring *ring, struct sk_buff *skb) |
| 593 | { |
| 594 | struct b43_rxhdr_fw4 *rxhdr; |
| 595 | unsigned char *frame; |
| 596 | |
| 597 | /* This poisons the RX buffer to detect DMA failures. */ |
| 598 | |
| 599 | rxhdr = (struct b43_rxhdr_fw4 *)(skb->data); |
| 600 | rxhdr->frame_len = 0; |
| 601 | |
| 602 | B43_WARN_ON(ring->rx_buffersize < ring->frameoffset + sizeof(struct b43_plcp_hdr6) + 2); |
| 603 | frame = skb->data + ring->frameoffset; |
| 604 | memset(frame, 0xFF, sizeof(struct b43_plcp_hdr6) + 2 /* padding */); |
| 605 | } |
| 606 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 607 | static int setup_rx_descbuffer(struct b43_dmaring *ring, |
| 608 | struct b43_dmadesc_generic *desc, |
| 609 | struct b43_dmadesc_meta *meta, gfp_t gfp_flags) |
| 610 | { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 611 | dma_addr_t dmaaddr; |
| 612 | struct sk_buff *skb; |
| 613 | |
| 614 | B43_WARN_ON(ring->tx); |
| 615 | |
| 616 | skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags); |
| 617 | if (unlikely(!skb)) |
| 618 | return -ENOMEM; |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 619 | b43_poison_rx_buffer(ring, skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 620 | dmaaddr = map_descbuffer(ring, skb->data, ring->rx_buffersize, 0); |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 621 | if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 622 | /* ugh. try to realloc in zone_dma */ |
| 623 | gfp_flags |= GFP_DMA; |
| 624 | |
| 625 | dev_kfree_skb_any(skb); |
| 626 | |
| 627 | skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags); |
| 628 | if (unlikely(!skb)) |
| 629 | return -ENOMEM; |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 630 | b43_poison_rx_buffer(ring, skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 631 | dmaaddr = map_descbuffer(ring, skb->data, |
| 632 | ring->rx_buffersize, 0); |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 633 | if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) { |
| 634 | b43err(ring->dev->wl, "RX DMA buffer allocation failed\n"); |
| 635 | dev_kfree_skb_any(skb); |
| 636 | return -EIO; |
| 637 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | meta->skb = skb; |
| 641 | meta->dmaaddr = dmaaddr; |
| 642 | ring->ops->fill_descriptor(ring, desc, dmaaddr, |
| 643 | ring->rx_buffersize, 0, 0, 0); |
| 644 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | /* Allocate the initial descbuffers. |
| 649 | * This is used for an RX ring only. |
| 650 | */ |
| 651 | static int alloc_initial_descbuffers(struct b43_dmaring *ring) |
| 652 | { |
| 653 | int i, err = -ENOMEM; |
| 654 | struct b43_dmadesc_generic *desc; |
| 655 | struct b43_dmadesc_meta *meta; |
| 656 | |
| 657 | for (i = 0; i < ring->nr_slots; i++) { |
| 658 | desc = ring->ops->idx2desc(ring, i, &meta); |
| 659 | |
| 660 | err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL); |
| 661 | if (err) { |
| 662 | b43err(ring->dev->wl, |
| 663 | "Failed to allocate initial descbuffers\n"); |
| 664 | goto err_unwind; |
| 665 | } |
| 666 | } |
| 667 | mb(); |
| 668 | ring->used_slots = ring->nr_slots; |
| 669 | err = 0; |
| 670 | out: |
| 671 | return err; |
| 672 | |
| 673 | err_unwind: |
| 674 | for (i--; i >= 0; i--) { |
| 675 | desc = ring->ops->idx2desc(ring, i, &meta); |
| 676 | |
| 677 | unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0); |
| 678 | dev_kfree_skb(meta->skb); |
| 679 | } |
| 680 | goto out; |
| 681 | } |
| 682 | |
| 683 | /* Do initial setup of the DMA controller. |
| 684 | * Reset the controller, write the ring busaddress |
| 685 | * and switch the "enable" bit on. |
| 686 | */ |
| 687 | static int dmacontroller_setup(struct b43_dmaring *ring) |
| 688 | { |
| 689 | int err = 0; |
| 690 | u32 value; |
| 691 | u32 addrext; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 692 | bool parity = ring->dev->dma.parity; |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 693 | u32 addrlo; |
| 694 | u32 addrhi; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 695 | |
| 696 | if (ring->tx) { |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 697 | if (ring->type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 698 | u64 ringbase = (u64) (ring->dmabase); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 699 | addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT); |
| 700 | addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW); |
| 701 | addrhi = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_HIGH); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 702 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 703 | value = B43_DMA64_TXENABLE; |
| 704 | value |= (addrext << B43_DMA64_TXADDREXT_SHIFT) |
| 705 | & B43_DMA64_TXADDREXT_MASK; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 706 | if (!parity) |
| 707 | value |= B43_DMA64_TXPARITYDISABLE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 708 | b43_dma_write(ring, B43_DMA64_TXCTL, value); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 709 | b43_dma_write(ring, B43_DMA64_TXRINGLO, addrlo); |
| 710 | b43_dma_write(ring, B43_DMA64_TXRINGHI, addrhi); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 711 | } else { |
| 712 | u32 ringbase = (u32) (ring->dmabase); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 713 | addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT); |
| 714 | addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 715 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 716 | value = B43_DMA32_TXENABLE; |
| 717 | value |= (addrext << B43_DMA32_TXADDREXT_SHIFT) |
| 718 | & B43_DMA32_TXADDREXT_MASK; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 719 | if (!parity) |
| 720 | value |= B43_DMA32_TXPARITYDISABLE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 721 | b43_dma_write(ring, B43_DMA32_TXCTL, value); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 722 | b43_dma_write(ring, B43_DMA32_TXRING, addrlo); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 723 | } |
| 724 | } else { |
| 725 | err = alloc_initial_descbuffers(ring); |
| 726 | if (err) |
| 727 | goto out; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 728 | if (ring->type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 729 | u64 ringbase = (u64) (ring->dmabase); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 730 | addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT); |
| 731 | addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW); |
| 732 | addrhi = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_HIGH); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 733 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 734 | value = (ring->frameoffset << B43_DMA64_RXFROFF_SHIFT); |
| 735 | value |= B43_DMA64_RXENABLE; |
| 736 | value |= (addrext << B43_DMA64_RXADDREXT_SHIFT) |
| 737 | & B43_DMA64_RXADDREXT_MASK; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 738 | if (!parity) |
| 739 | value |= B43_DMA64_RXPARITYDISABLE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 740 | b43_dma_write(ring, B43_DMA64_RXCTL, value); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 741 | b43_dma_write(ring, B43_DMA64_RXRINGLO, addrlo); |
| 742 | b43_dma_write(ring, B43_DMA64_RXRINGHI, addrhi); |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 743 | b43_dma_write(ring, B43_DMA64_RXINDEX, ring->nr_slots * |
| 744 | sizeof(struct b43_dmadesc64)); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 745 | } else { |
| 746 | u32 ringbase = (u32) (ring->dmabase); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 747 | addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT); |
| 748 | addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 749 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 750 | value = (ring->frameoffset << B43_DMA32_RXFROFF_SHIFT); |
| 751 | value |= B43_DMA32_RXENABLE; |
| 752 | value |= (addrext << B43_DMA32_RXADDREXT_SHIFT) |
| 753 | & B43_DMA32_RXADDREXT_MASK; |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 754 | if (!parity) |
| 755 | value |= B43_DMA32_RXPARITYDISABLE; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 756 | b43_dma_write(ring, B43_DMA32_RXCTL, value); |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 757 | b43_dma_write(ring, B43_DMA32_RXRING, addrlo); |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 758 | b43_dma_write(ring, B43_DMA32_RXINDEX, ring->nr_slots * |
| 759 | sizeof(struct b43_dmadesc32)); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Larry Finger | 013978b | 2007-11-26 10:29:47 -0600 | [diff] [blame] | 763 | out: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 764 | return err; |
| 765 | } |
| 766 | |
| 767 | /* Shutdown the DMA controller. */ |
| 768 | static void dmacontroller_cleanup(struct b43_dmaring *ring) |
| 769 | { |
| 770 | if (ring->tx) { |
| 771 | b43_dmacontroller_tx_reset(ring->dev, ring->mmio_base, |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 772 | ring->type); |
| 773 | if (ring->type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 774 | b43_dma_write(ring, B43_DMA64_TXRINGLO, 0); |
| 775 | b43_dma_write(ring, B43_DMA64_TXRINGHI, 0); |
| 776 | } else |
| 777 | b43_dma_write(ring, B43_DMA32_TXRING, 0); |
| 778 | } else { |
| 779 | b43_dmacontroller_rx_reset(ring->dev, ring->mmio_base, |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 780 | ring->type); |
| 781 | if (ring->type == B43_DMA_64BIT) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 782 | b43_dma_write(ring, B43_DMA64_RXRINGLO, 0); |
| 783 | b43_dma_write(ring, B43_DMA64_RXRINGHI, 0); |
| 784 | } else |
| 785 | b43_dma_write(ring, B43_DMA32_RXRING, 0); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | static void free_all_descbuffers(struct b43_dmaring *ring) |
| 790 | { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 791 | struct b43_dmadesc_meta *meta; |
| 792 | int i; |
| 793 | |
| 794 | if (!ring->used_slots) |
| 795 | return; |
| 796 | for (i = 0; i < ring->nr_slots; i++) { |
Larry Finger | 9c1cacd | 2011-05-22 20:54:25 -0500 | [diff] [blame] | 797 | /* get meta - ignore returned value */ |
| 798 | ring->ops->idx2desc(ring, i, &meta); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 799 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 800 | if (!meta->skb || b43_dma_ptr_is_poisoned(meta->skb)) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 801 | B43_WARN_ON(!ring->tx); |
| 802 | continue; |
| 803 | } |
| 804 | if (ring->tx) { |
| 805 | unmap_descbuffer(ring, meta->dmaaddr, |
| 806 | meta->skb->len, 1); |
| 807 | } else { |
| 808 | unmap_descbuffer(ring, meta->dmaaddr, |
| 809 | ring->rx_buffersize, 0); |
| 810 | } |
| 811 | free_descriptor_buffer(ring, meta); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | static u64 supported_dma_mask(struct b43_wldev *dev) |
| 816 | { |
| 817 | u32 tmp; |
| 818 | u16 mmio_base; |
| 819 | |
Hauke Mehrtens | 5b36c9b | 2011-07-23 13:57:33 +0200 | [diff] [blame] | 820 | switch (dev->dev->bus_type) { |
| 821 | #ifdef CONFIG_B43_BCMA |
| 822 | case B43_BUS_BCMA: |
| 823 | tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST); |
| 824 | if (tmp & BCMA_IOST_DMA64) |
| 825 | return DMA_BIT_MASK(64); |
| 826 | break; |
| 827 | #endif |
| 828 | #ifdef CONFIG_B43_SSB |
| 829 | case B43_BUS_SSB: |
| 830 | tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH); |
| 831 | if (tmp & SSB_TMSHIGH_DMA64) |
| 832 | return DMA_BIT_MASK(64); |
| 833 | break; |
| 834 | #endif |
| 835 | } |
| 836 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 837 | mmio_base = b43_dmacontroller_base(0, 0); |
| 838 | b43_write32(dev, mmio_base + B43_DMA32_TXCTL, B43_DMA32_TXADDREXT_MASK); |
| 839 | tmp = b43_read32(dev, mmio_base + B43_DMA32_TXCTL); |
| 840 | if (tmp & B43_DMA32_TXADDREXT_MASK) |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 841 | return DMA_BIT_MASK(32); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 842 | |
Yang Hongyang | 28b7679 | 2009-04-06 19:01:17 -0700 | [diff] [blame] | 843 | return DMA_BIT_MASK(30); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 844 | } |
| 845 | |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 846 | static enum b43_dmatype dma_mask_to_engine_type(u64 dmamask) |
| 847 | { |
Yang Hongyang | 28b7679 | 2009-04-06 19:01:17 -0700 | [diff] [blame] | 848 | if (dmamask == DMA_BIT_MASK(30)) |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 849 | return B43_DMA_30BIT; |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 850 | if (dmamask == DMA_BIT_MASK(32)) |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 851 | return B43_DMA_32BIT; |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 852 | if (dmamask == DMA_BIT_MASK(64)) |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 853 | return B43_DMA_64BIT; |
| 854 | B43_WARN_ON(1); |
| 855 | return B43_DMA_30BIT; |
| 856 | } |
| 857 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 858 | /* Main initialization function. */ |
| 859 | static |
| 860 | struct b43_dmaring *b43_setup_dmaring(struct b43_wldev *dev, |
| 861 | int controller_index, |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 862 | int for_tx, |
| 863 | enum b43_dmatype type) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 864 | { |
| 865 | struct b43_dmaring *ring; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 866 | int i, err; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 867 | dma_addr_t dma_test; |
| 868 | |
| 869 | ring = kzalloc(sizeof(*ring), GFP_KERNEL); |
| 870 | if (!ring) |
| 871 | goto out; |
| 872 | |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 873 | ring->nr_slots = B43_RXRING_SLOTS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 874 | if (for_tx) |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 875 | ring->nr_slots = B43_TXRING_SLOTS; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 876 | |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 877 | ring->meta = kcalloc(ring->nr_slots, sizeof(struct b43_dmadesc_meta), |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 878 | GFP_KERNEL); |
| 879 | if (!ring->meta) |
| 880 | goto err_kfree_ring; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 881 | for (i = 0; i < ring->nr_slots; i++) |
| 882 | ring->meta->skb = B43_DMA_PTR_POISON; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 883 | |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 884 | ring->type = type; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 885 | ring->dev = dev; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 886 | ring->mmio_base = b43_dmacontroller_base(type, controller_index); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 887 | ring->index = controller_index; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 888 | if (type == B43_DMA_64BIT) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 889 | ring->ops = &dma64_ops; |
| 890 | else |
| 891 | ring->ops = &dma32_ops; |
| 892 | if (for_tx) { |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 893 | ring->tx = true; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 894 | ring->current_slot = -1; |
| 895 | } else { |
| 896 | if (ring->index == 0) { |
Rafał Miłecki | 17030f4 | 2011-08-11 17:16:27 +0200 | [diff] [blame] | 897 | switch (dev->fw.hdr_format) { |
| 898 | case B43_FW_HDR_598: |
| 899 | ring->rx_buffersize = B43_DMA0_RX_FW598_BUFSIZE; |
| 900 | ring->frameoffset = B43_DMA0_RX_FW598_FO; |
| 901 | break; |
| 902 | case B43_FW_HDR_410: |
| 903 | case B43_FW_HDR_351: |
| 904 | ring->rx_buffersize = B43_DMA0_RX_FW351_BUFSIZE; |
| 905 | ring->frameoffset = B43_DMA0_RX_FW351_FO; |
| 906 | break; |
| 907 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 908 | } else |
| 909 | B43_WARN_ON(1); |
| 910 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 911 | #ifdef CONFIG_B43_DEBUG |
| 912 | ring->last_injected_overflow = jiffies; |
| 913 | #endif |
| 914 | |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 915 | if (for_tx) { |
Michael Buesch | 2d071ca | 2009-02-20 12:24:52 +0100 | [diff] [blame] | 916 | /* Assumption: B43_TXRING_SLOTS can be divided by TX_SLOTS_PER_FRAME */ |
| 917 | BUILD_BUG_ON(B43_TXRING_SLOTS % TX_SLOTS_PER_FRAME != 0); |
| 918 | |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 919 | ring->txhdr_cache = kcalloc(ring->nr_slots / TX_SLOTS_PER_FRAME, |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 920 | b43_txhdr_size(dev), |
| 921 | GFP_KERNEL); |
| 922 | if (!ring->txhdr_cache) |
| 923 | goto err_kfree_meta; |
| 924 | |
| 925 | /* test for ability to dma to txhdr_cache */ |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 926 | dma_test = dma_map_single(dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 927 | ring->txhdr_cache, |
| 928 | b43_txhdr_size(dev), |
| 929 | DMA_TO_DEVICE); |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 930 | |
| 931 | if (b43_dma_mapping_error(ring, dma_test, |
| 932 | b43_txhdr_size(dev), 1)) { |
| 933 | /* ugh realloc */ |
| 934 | kfree(ring->txhdr_cache); |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 935 | ring->txhdr_cache = kcalloc(ring->nr_slots / TX_SLOTS_PER_FRAME, |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 936 | b43_txhdr_size(dev), |
| 937 | GFP_KERNEL | GFP_DMA); |
| 938 | if (!ring->txhdr_cache) |
| 939 | goto err_kfree_meta; |
| 940 | |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 941 | dma_test = dma_map_single(dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 942 | ring->txhdr_cache, |
| 943 | b43_txhdr_size(dev), |
| 944 | DMA_TO_DEVICE); |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 945 | |
| 946 | if (b43_dma_mapping_error(ring, dma_test, |
| 947 | b43_txhdr_size(dev), 1)) { |
| 948 | |
| 949 | b43err(dev->wl, |
| 950 | "TXHDR DMA allocation failed\n"); |
| 951 | goto err_kfree_txhdr_cache; |
| 952 | } |
| 953 | } |
| 954 | |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 955 | dma_unmap_single(dev->dev->dma_dev, |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 956 | dma_test, b43_txhdr_size(dev), |
| 957 | DMA_TO_DEVICE); |
Michael Buesch | 028118a | 2008-06-12 11:58:56 +0200 | [diff] [blame] | 958 | } |
| 959 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 960 | err = alloc_ringmemory(ring); |
| 961 | if (err) |
| 962 | goto err_kfree_txhdr_cache; |
| 963 | err = dmacontroller_setup(ring); |
| 964 | if (err) |
| 965 | goto err_free_ringmemory; |
| 966 | |
| 967 | out: |
| 968 | return ring; |
| 969 | |
| 970 | err_free_ringmemory: |
| 971 | free_ringmemory(ring); |
| 972 | err_kfree_txhdr_cache: |
| 973 | kfree(ring->txhdr_cache); |
| 974 | err_kfree_meta: |
| 975 | kfree(ring->meta); |
| 976 | err_kfree_ring: |
| 977 | kfree(ring); |
| 978 | ring = NULL; |
| 979 | goto out; |
| 980 | } |
| 981 | |
Michael Buesch | 57df40d | 2008-03-07 15:50:02 +0100 | [diff] [blame] | 982 | #define divide(a, b) ({ \ |
| 983 | typeof(a) __a = a; \ |
| 984 | do_div(__a, b); \ |
| 985 | __a; \ |
| 986 | }) |
| 987 | |
| 988 | #define modulo(a, b) ({ \ |
| 989 | typeof(a) __a = a; \ |
| 990 | do_div(__a, b); \ |
| 991 | }) |
| 992 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 993 | /* Main cleanup function. */ |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 994 | static void b43_destroy_dmaring(struct b43_dmaring *ring, |
| 995 | const char *ringname) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 996 | { |
| 997 | if (!ring) |
| 998 | return; |
| 999 | |
Michael Buesch | 57df40d | 2008-03-07 15:50:02 +0100 | [diff] [blame] | 1000 | #ifdef CONFIG_B43_DEBUG |
| 1001 | { |
| 1002 | /* Print some statistics. */ |
| 1003 | u64 failed_packets = ring->nr_failed_tx_packets; |
| 1004 | u64 succeed_packets = ring->nr_succeed_tx_packets; |
| 1005 | u64 nr_packets = failed_packets + succeed_packets; |
| 1006 | u64 permille_failed = 0, average_tries = 0; |
| 1007 | |
| 1008 | if (nr_packets) |
| 1009 | permille_failed = divide(failed_packets * 1000, nr_packets); |
| 1010 | if (nr_packets) |
| 1011 | average_tries = divide(ring->nr_total_packet_tries * 100, nr_packets); |
| 1012 | |
| 1013 | b43dbg(ring->dev->wl, "DMA-%u %s: " |
| 1014 | "Used slots %d/%d, Failed frames %llu/%llu = %llu.%01llu%%, " |
| 1015 | "Average tries %llu.%02llu\n", |
| 1016 | (unsigned int)(ring->type), ringname, |
| 1017 | ring->max_used_slots, |
| 1018 | ring->nr_slots, |
| 1019 | (unsigned long long)failed_packets, |
Michael Buesch | 87d9611 | 2008-03-07 19:52:24 +0100 | [diff] [blame] | 1020 | (unsigned long long)nr_packets, |
Michael Buesch | 57df40d | 2008-03-07 15:50:02 +0100 | [diff] [blame] | 1021 | (unsigned long long)divide(permille_failed, 10), |
| 1022 | (unsigned long long)modulo(permille_failed, 10), |
| 1023 | (unsigned long long)divide(average_tries, 100), |
| 1024 | (unsigned long long)modulo(average_tries, 100)); |
| 1025 | } |
| 1026 | #endif /* DEBUG */ |
| 1027 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1028 | /* Device IRQs are disabled prior entering this function, |
| 1029 | * so no need to take care of concurrency with rx handler stuff. |
| 1030 | */ |
| 1031 | dmacontroller_cleanup(ring); |
| 1032 | free_all_descbuffers(ring); |
| 1033 | free_ringmemory(ring); |
| 1034 | |
| 1035 | kfree(ring->txhdr_cache); |
| 1036 | kfree(ring->meta); |
| 1037 | kfree(ring); |
| 1038 | } |
| 1039 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1040 | #define destroy_ring(dma, ring) do { \ |
| 1041 | b43_destroy_dmaring((dma)->ring, __stringify(ring)); \ |
| 1042 | (dma)->ring = NULL; \ |
| 1043 | } while (0) |
| 1044 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1045 | void b43_dma_free(struct b43_wldev *dev) |
| 1046 | { |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1047 | struct b43_dma *dma; |
| 1048 | |
| 1049 | if (b43_using_pio_transfers(dev)) |
| 1050 | return; |
| 1051 | dma = &dev->dma; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1052 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1053 | destroy_ring(dma, rx_ring); |
| 1054 | destroy_ring(dma, tx_ring_AC_BK); |
| 1055 | destroy_ring(dma, tx_ring_AC_BE); |
| 1056 | destroy_ring(dma, tx_ring_AC_VI); |
| 1057 | destroy_ring(dma, tx_ring_AC_VO); |
| 1058 | destroy_ring(dma, tx_ring_mcast); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1059 | } |
| 1060 | |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1061 | static int b43_dma_set_mask(struct b43_wldev *dev, u64 mask) |
| 1062 | { |
| 1063 | u64 orig_mask = mask; |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1064 | bool fallback = false; |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1065 | int err; |
| 1066 | |
| 1067 | /* Try to set the DMA mask. If it fails, try falling back to a |
| 1068 | * lower mask, as we can always also support a lower one. */ |
| 1069 | while (1) { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 1070 | err = dma_set_mask(dev->dev->dma_dev, mask); |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 1071 | if (!err) { |
Rafał Miłecki | a18c715 | 2011-05-18 02:06:40 +0200 | [diff] [blame] | 1072 | err = dma_set_coherent_mask(dev->dev->dma_dev, mask); |
FUJITA Tomonori | 718e889 | 2010-06-03 19:37:36 -0700 | [diff] [blame] | 1073 | if (!err) |
| 1074 | break; |
| 1075 | } |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 1076 | if (mask == DMA_BIT_MASK(64)) { |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 1077 | mask = DMA_BIT_MASK(32); |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1078 | fallback = true; |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1079 | continue; |
| 1080 | } |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 1081 | if (mask == DMA_BIT_MASK(32)) { |
Yang Hongyang | 28b7679 | 2009-04-06 19:01:17 -0700 | [diff] [blame] | 1082 | mask = DMA_BIT_MASK(30); |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1083 | fallback = true; |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1084 | continue; |
| 1085 | } |
| 1086 | b43err(dev->wl, "The machine/kernel does not support " |
| 1087 | "the required %u-bit DMA mask\n", |
| 1088 | (unsigned int)dma_mask_to_engine_type(orig_mask)); |
| 1089 | return -EOPNOTSUPP; |
| 1090 | } |
| 1091 | if (fallback) { |
| 1092 | b43info(dev->wl, "DMA mask fallback from %u-bit to %u-bit\n", |
| 1093 | (unsigned int)dma_mask_to_engine_type(orig_mask), |
| 1094 | (unsigned int)dma_mask_to_engine_type(mask)); |
| 1095 | } |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 1100 | /* Some hardware with 64-bit DMA seems to be bugged and looks for translation |
| 1101 | * bit in low address word instead of high one. |
| 1102 | */ |
| 1103 | static bool b43_dma_translation_in_low_word(struct b43_wldev *dev, |
| 1104 | enum b43_dmatype type) |
| 1105 | { |
| 1106 | if (type != B43_DMA_64BIT) |
| 1107 | return 1; |
| 1108 | |
| 1109 | #ifdef CONFIG_B43_SSB |
| 1110 | if (dev->dev->bus_type == B43_BUS_SSB && |
| 1111 | dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI && |
| 1112 | !(dev->dev->sdev->bus->host_pci->is_pcie && |
| 1113 | ssb_read32(dev->dev->sdev, SSB_TMSHIGH) & SSB_TMSHIGH_DMA64)) |
| 1114 | return 1; |
| 1115 | #endif |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1119 | int b43_dma_init(struct b43_wldev *dev) |
| 1120 | { |
| 1121 | struct b43_dma *dma = &dev->dma; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1122 | int err; |
| 1123 | u64 dmamask; |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 1124 | enum b43_dmatype type; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1125 | |
| 1126 | dmamask = supported_dma_mask(dev); |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1127 | type = dma_mask_to_engine_type(dmamask); |
Michael Buesch | 1033b3e | 2008-04-23 19:13:01 +0200 | [diff] [blame] | 1128 | err = b43_dma_set_mask(dev, dmamask); |
| 1129 | if (err) |
| 1130 | return err; |
Rafał Miłecki | 6cbab0d | 2011-07-06 15:45:26 +0200 | [diff] [blame] | 1131 | |
| 1132 | switch (dev->dev->bus_type) { |
Rafał Miłecki | eb90e9e | 2011-07-20 19:52:16 +0200 | [diff] [blame] | 1133 | #ifdef CONFIG_B43_BCMA |
| 1134 | case B43_BUS_BCMA: |
| 1135 | dma->translation = bcma_core_dma_translation(dev->dev->bdev); |
| 1136 | break; |
| 1137 | #endif |
Rafał Miłecki | 6cbab0d | 2011-07-06 15:45:26 +0200 | [diff] [blame] | 1138 | #ifdef CONFIG_B43_SSB |
| 1139 | case B43_BUS_SSB: |
| 1140 | dma->translation = ssb_dma_translation(dev->dev->sdev); |
| 1141 | break; |
| 1142 | #endif |
| 1143 | } |
Rafał Miłecki | 0cc9772 | 2011-08-14 20:16:37 +0200 | [diff] [blame] | 1144 | dma->translation_in_low = b43_dma_translation_in_low_word(dev, type); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1145 | |
Rafał Miłecki | 78c1ee7 | 2011-07-20 19:47:07 +0200 | [diff] [blame] | 1146 | dma->parity = true; |
| 1147 | #ifdef CONFIG_B43_BCMA |
| 1148 | /* TODO: find out which SSB devices need disabling parity */ |
| 1149 | if (dev->dev->bus_type == B43_BUS_BCMA) |
| 1150 | dma->parity = false; |
| 1151 | #endif |
| 1152 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1153 | err = -ENOMEM; |
| 1154 | /* setup TX DMA channels. */ |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1155 | dma->tx_ring_AC_BK = b43_setup_dmaring(dev, 0, 1, type); |
| 1156 | if (!dma->tx_ring_AC_BK) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1157 | goto out; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1158 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1159 | dma->tx_ring_AC_BE = b43_setup_dmaring(dev, 1, 1, type); |
| 1160 | if (!dma->tx_ring_AC_BE) |
| 1161 | goto err_destroy_bk; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1162 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1163 | dma->tx_ring_AC_VI = b43_setup_dmaring(dev, 2, 1, type); |
| 1164 | if (!dma->tx_ring_AC_VI) |
| 1165 | goto err_destroy_be; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1166 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1167 | dma->tx_ring_AC_VO = b43_setup_dmaring(dev, 3, 1, type); |
| 1168 | if (!dma->tx_ring_AC_VO) |
| 1169 | goto err_destroy_vi; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1170 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1171 | dma->tx_ring_mcast = b43_setup_dmaring(dev, 4, 1, type); |
| 1172 | if (!dma->tx_ring_mcast) |
| 1173 | goto err_destroy_vo; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1174 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1175 | /* setup RX DMA channel. */ |
| 1176 | dma->rx_ring = b43_setup_dmaring(dev, 0, 0, type); |
| 1177 | if (!dma->rx_ring) |
| 1178 | goto err_destroy_mcast; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1179 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1180 | /* No support for the TX status DMA ring. */ |
Rafał Miłecki | 21d889d | 2011-05-18 02:06:38 +0200 | [diff] [blame] | 1181 | B43_WARN_ON(dev->dev->core_rev < 5); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1182 | |
Michael Buesch | b79caa6 | 2008-02-05 12:50:41 +0100 | [diff] [blame] | 1183 | b43dbg(dev->wl, "%u-bit DMA initialized\n", |
| 1184 | (unsigned int)type); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1185 | err = 0; |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1186 | out: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1187 | return err; |
| 1188 | |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1189 | err_destroy_mcast: |
| 1190 | destroy_ring(dma, tx_ring_mcast); |
| 1191 | err_destroy_vo: |
| 1192 | destroy_ring(dma, tx_ring_AC_VO); |
| 1193 | err_destroy_vi: |
| 1194 | destroy_ring(dma, tx_ring_AC_VI); |
| 1195 | err_destroy_be: |
| 1196 | destroy_ring(dma, tx_ring_AC_BE); |
| 1197 | err_destroy_bk: |
| 1198 | destroy_ring(dma, tx_ring_AC_BK); |
| 1199 | return err; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /* Generate a cookie for the TX header. */ |
| 1203 | static u16 generate_cookie(struct b43_dmaring *ring, int slot) |
| 1204 | { |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1205 | u16 cookie; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1206 | |
| 1207 | /* Use the upper 4 bits of the cookie as |
| 1208 | * DMA controller ID and store the slot number |
| 1209 | * in the lower 12 bits. |
| 1210 | * Note that the cookie must never be 0, as this |
| 1211 | * is a special value used in RX path. |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1212 | * It can also not be 0xFFFF because that is special |
| 1213 | * for multicast frames. |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1214 | */ |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1215 | cookie = (((u16)ring->index + 1) << 12); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1216 | B43_WARN_ON(slot & ~0x0FFF); |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1217 | cookie |= (u16)slot; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1218 | |
| 1219 | return cookie; |
| 1220 | } |
| 1221 | |
| 1222 | /* Inspect a cookie and find out to which controller/slot it belongs. */ |
| 1223 | static |
| 1224 | struct b43_dmaring *parse_cookie(struct b43_wldev *dev, u16 cookie, int *slot) |
| 1225 | { |
| 1226 | struct b43_dma *dma = &dev->dma; |
| 1227 | struct b43_dmaring *ring = NULL; |
| 1228 | |
| 1229 | switch (cookie & 0xF000) { |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1230 | case 0x1000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1231 | ring = dma->tx_ring_AC_BK; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1232 | break; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1233 | case 0x2000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1234 | ring = dma->tx_ring_AC_BE; |
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 0x3000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1237 | ring = dma->tx_ring_AC_VI; |
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 0x4000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1240 | ring = dma->tx_ring_AC_VO; |
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 0x5000: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1243 | ring = dma->tx_ring_mcast; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1244 | break; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1245 | } |
| 1246 | *slot = (cookie & 0x0FFF); |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1247 | if (unlikely(!ring || *slot < 0 || *slot >= ring->nr_slots)) { |
| 1248 | b43dbg(dev->wl, "TX-status contains " |
| 1249 | "invalid cookie: 0x%04X\n", cookie); |
| 1250 | return NULL; |
| 1251 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1252 | |
| 1253 | return ring; |
| 1254 | } |
| 1255 | |
| 1256 | static int dma_tx_fragment(struct b43_dmaring *ring, |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1257 | struct sk_buff *skb) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1258 | { |
| 1259 | const struct b43_dma_ops *ops = ring->ops; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1260 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1261 | 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] | 1262 | u8 *header; |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1263 | int slot, old_top_slot, old_used_slots; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1264 | int err; |
| 1265 | struct b43_dmadesc_generic *desc; |
| 1266 | struct b43_dmadesc_meta *meta; |
| 1267 | struct b43_dmadesc_meta *meta_hdr; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1268 | u16 cookie; |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1269 | size_t hdrsize = b43_txhdr_size(ring->dev); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1270 | |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 1271 | /* Important note: If the number of used DMA slots per TX frame |
| 1272 | * is changed here, the TX_SLOTS_PER_FRAME definition at the top of |
| 1273 | * the file has to be updated, too! |
| 1274 | */ |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1275 | |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1276 | old_top_slot = ring->current_slot; |
| 1277 | old_used_slots = ring->used_slots; |
| 1278 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1279 | /* Get a slot for the header. */ |
| 1280 | slot = request_slot(ring); |
| 1281 | desc = ops->idx2desc(ring, slot, &meta_hdr); |
| 1282 | memset(meta_hdr, 0, sizeof(*meta_hdr)); |
| 1283 | |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 1284 | header = &(ring->txhdr_cache[(slot / TX_SLOTS_PER_FRAME) * hdrsize]); |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1285 | cookie = generate_cookie(ring, slot); |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1286 | err = b43_generate_txhdr(ring->dev, header, |
gregor kowski | 035d024 | 2009-08-19 22:35:45 +0200 | [diff] [blame] | 1287 | skb, info, cookie); |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1288 | if (unlikely(err)) { |
| 1289 | ring->current_slot = old_top_slot; |
| 1290 | ring->used_slots = old_used_slots; |
| 1291 | return err; |
| 1292 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1293 | |
| 1294 | meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header, |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1295 | hdrsize, 1); |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 1296 | if (b43_dma_mapping_error(ring, meta_hdr->dmaaddr, hdrsize, 1)) { |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1297 | ring->current_slot = old_top_slot; |
| 1298 | ring->used_slots = old_used_slots; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1299 | return -EIO; |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1300 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1301 | ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr, |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1302 | hdrsize, 1, 0, 0); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1303 | |
| 1304 | /* Get a slot for the payload. */ |
| 1305 | slot = request_slot(ring); |
| 1306 | desc = ops->idx2desc(ring, slot, &meta); |
| 1307 | memset(meta, 0, sizeof(*meta)); |
| 1308 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1309 | meta->skb = skb; |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1310 | meta->is_last_fragment = true; |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1311 | priv_info->bouncebuffer = NULL; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1312 | |
| 1313 | meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1); |
| 1314 | /* create a bounce buffer in zone_dma on mapping failure. */ |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 1315 | if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) { |
Julia Lawall | a61aac7 | 2010-05-15 23:20:26 +0200 | [diff] [blame] | 1316 | priv_info->bouncebuffer = kmemdup(skb->data, skb->len, |
| 1317 | GFP_ATOMIC | GFP_DMA); |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1318 | if (!priv_info->bouncebuffer) { |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1319 | ring->current_slot = old_top_slot; |
| 1320 | ring->used_slots = old_used_slots; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1321 | err = -ENOMEM; |
| 1322 | goto out_unmap_hdr; |
| 1323 | } |
| 1324 | |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1325 | meta->dmaaddr = map_descbuffer(ring, priv_info->bouncebuffer, skb->len, 1); |
Michael Buesch | ffa9256 | 2008-03-22 22:04:45 +0100 | [diff] [blame] | 1326 | if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) { |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1327 | kfree(priv_info->bouncebuffer); |
| 1328 | priv_info->bouncebuffer = NULL; |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1329 | ring->current_slot = old_top_slot; |
| 1330 | ring->used_slots = old_used_slots; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1331 | err = -EIO; |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1332 | goto out_unmap_hdr; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | ops->fill_descriptor(ring, desc, meta->dmaaddr, skb->len, 0, 1, 1); |
| 1337 | |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1338 | if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) { |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1339 | /* Tell the firmware about the cookie of the last |
| 1340 | * mcast frame, so it can clear the more-data bit in it. */ |
| 1341 | b43_shm_write16(ring->dev, B43_SHM_SHARED, |
| 1342 | B43_SHM_SH_MCASTCOOKIE, cookie); |
| 1343 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1344 | /* Now transfer the whole frame. */ |
| 1345 | wmb(); |
| 1346 | ops->poke_tx(ring, next_slot(ring, slot)); |
| 1347 | return 0; |
| 1348 | |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1349 | out_unmap_hdr: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1350 | unmap_descbuffer(ring, meta_hdr->dmaaddr, |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1351 | hdrsize, 1); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1352 | return err; |
| 1353 | } |
| 1354 | |
| 1355 | static inline int should_inject_overflow(struct b43_dmaring *ring) |
| 1356 | { |
| 1357 | #ifdef CONFIG_B43_DEBUG |
| 1358 | if (unlikely(b43_debug(ring->dev, B43_DBG_DMAOVERFLOW))) { |
| 1359 | /* Check if we should inject another ringbuffer overflow |
| 1360 | * to test handling of this situation in the stack. */ |
| 1361 | unsigned long next_overflow; |
| 1362 | |
| 1363 | next_overflow = ring->last_injected_overflow + HZ; |
| 1364 | if (time_after(jiffies, next_overflow)) { |
| 1365 | ring->last_injected_overflow = jiffies; |
| 1366 | b43dbg(ring->dev->wl, |
| 1367 | "Injecting TX ring overflow on " |
| 1368 | "DMA controller %d\n", ring->index); |
| 1369 | return 1; |
| 1370 | } |
| 1371 | } |
| 1372 | #endif /* CONFIG_B43_DEBUG */ |
| 1373 | return 0; |
| 1374 | } |
| 1375 | |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1376 | /* Static mapping of mac80211's queues (priorities) to b43 DMA rings. */ |
John Daiker | 99da185 | 2009-02-24 02:16:42 -0800 | [diff] [blame] | 1377 | static struct b43_dmaring *select_ring_by_priority(struct b43_wldev *dev, |
| 1378 | u8 queue_prio) |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1379 | { |
| 1380 | struct b43_dmaring *ring; |
| 1381 | |
Michael Buesch | 403a3a1 | 2009-06-08 21:04:57 +0200 | [diff] [blame] | 1382 | if (dev->qos_enabled) { |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1383 | /* 0 = highest priority */ |
| 1384 | switch (queue_prio) { |
| 1385 | default: |
| 1386 | B43_WARN_ON(1); |
| 1387 | /* fallthrough */ |
| 1388 | case 0: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1389 | ring = dev->dma.tx_ring_AC_VO; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1390 | break; |
| 1391 | case 1: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1392 | ring = dev->dma.tx_ring_AC_VI; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1393 | break; |
| 1394 | case 2: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1395 | ring = dev->dma.tx_ring_AC_BE; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1396 | break; |
| 1397 | case 3: |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1398 | ring = dev->dma.tx_ring_AC_BK; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1399 | break; |
| 1400 | } |
| 1401 | } else |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1402 | ring = dev->dma.tx_ring_AC_BE; |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1403 | |
| 1404 | return ring; |
| 1405 | } |
| 1406 | |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1407 | int b43_dma_tx(struct b43_wldev *dev, struct sk_buff *skb) |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1408 | { |
| 1409 | struct b43_dmaring *ring; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1410 | struct ieee80211_hdr *hdr; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1411 | int err = 0; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1412 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1413 | |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1414 | hdr = (struct ieee80211_hdr *)skb->data; |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1415 | if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) { |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1416 | /* The multicast ring will be sent after the DTIM */ |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1417 | ring = dev->dma.tx_ring_mcast; |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1418 | /* Set the more-data bit. Ucode will clear it on |
| 1419 | * the last frame for us. */ |
| 1420 | hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA); |
| 1421 | } else { |
| 1422 | /* Decide by priority where to put this frame. */ |
Johannes Berg | e253008 | 2008-05-17 00:57:14 +0200 | [diff] [blame] | 1423 | ring = select_ring_by_priority( |
| 1424 | dev, skb_get_queue_mapping(skb)); |
Michael Buesch | 280d0e1 | 2007-12-26 18:26:17 +0100 | [diff] [blame] | 1425 | } |
| 1426 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1427 | B43_WARN_ON(!ring->tx); |
Michael Buesch | ca2d559 | 2009-02-19 20:17:36 +0100 | [diff] [blame] | 1428 | |
Larry Finger | 18c6951 | 2009-07-29 10:54:06 -0500 | [diff] [blame] | 1429 | if (unlikely(ring->stopped)) { |
| 1430 | /* We get here only because of a bug in mac80211. |
| 1431 | * Because of a race, one packet may be queued after |
| 1432 | * the queue is stopped, thus we got called when we shouldn't. |
| 1433 | * For now, just refuse the transmit. */ |
| 1434 | if (b43_debug(dev, B43_DBG_DMAVERBOSE)) |
| 1435 | b43err(dev->wl, "Packet after queue stopped\n"); |
| 1436 | err = -ENOSPC; |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1437 | goto out; |
Larry Finger | 18c6951 | 2009-07-29 10:54:06 -0500 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | if (unlikely(WARN_ON(free_slots(ring) < TX_SLOTS_PER_FRAME))) { |
| 1441 | /* If we get here, we have a real error with the queue |
| 1442 | * full, but queues not stopped. */ |
| 1443 | b43err(dev->wl, "DMA queue overflow\n"); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1444 | err = -ENOSPC; |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1445 | goto out; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1446 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1447 | |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1448 | /* Assign the queue number to the ring (if not already done before) |
| 1449 | * so TX status handling can use it. The queue to ring mapping is |
| 1450 | * static, so we don't need to store it per frame. */ |
Johannes Berg | e253008 | 2008-05-17 00:57:14 +0200 | [diff] [blame] | 1451 | ring->queue_prio = skb_get_queue_mapping(skb); |
Michael Buesch | e6f5b93 | 2008-03-05 21:18:49 +0100 | [diff] [blame] | 1452 | |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1453 | err = dma_tx_fragment(ring, skb); |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1454 | if (unlikely(err == -ENOKEY)) { |
| 1455 | /* Drop this packet, as we don't have the encryption key |
| 1456 | * anymore and must not transmit it unencrypted. */ |
| 1457 | dev_kfree_skb_any(skb); |
| 1458 | err = 0; |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1459 | goto out; |
Michael Buesch | 09552cc | 2008-01-23 21:44:15 +0100 | [diff] [blame] | 1460 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1461 | if (unlikely(err)) { |
| 1462 | b43err(dev->wl, "DMA tx mapping failure\n"); |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1463 | goto out; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1464 | } |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 1465 | if ((free_slots(ring) < TX_SLOTS_PER_FRAME) || |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1466 | should_inject_overflow(ring)) { |
| 1467 | /* This TX ring is full. */ |
francesco.gringoli@ing.unibs.it | bad6919 | 2011-12-16 18:34:56 +0100 | [diff] [blame] | 1468 | unsigned int skb_mapping = skb_get_queue_mapping(skb); |
| 1469 | ieee80211_stop_queue(dev->wl->hw, skb_mapping); |
| 1470 | dev->wl->tx_queue_stopped[skb_mapping] = 1; |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1471 | ring->stopped = true; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1472 | if (b43_debug(dev, B43_DBG_DMAVERBOSE)) { |
| 1473 | b43dbg(dev->wl, "Stopped TX ring %d\n", ring->index); |
| 1474 | } |
| 1475 | } |
Michael Buesch | 637dae3 | 2009-09-04 22:55:00 +0200 | [diff] [blame] | 1476 | out: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1477 | |
| 1478 | return err; |
| 1479 | } |
| 1480 | |
| 1481 | void b43_dma_handle_txstatus(struct b43_wldev *dev, |
| 1482 | const struct b43_txstatus *status) |
| 1483 | { |
| 1484 | const struct b43_dma_ops *ops; |
| 1485 | struct b43_dmaring *ring; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1486 | struct b43_dmadesc_meta *meta; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1487 | int slot, firstused; |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1488 | bool frame_succeed; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1489 | |
| 1490 | ring = parse_cookie(dev, status->cookie, &slot); |
| 1491 | if (unlikely(!ring)) |
| 1492 | return; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1493 | B43_WARN_ON(!ring->tx); |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1494 | |
| 1495 | /* Sanity check: TX packets are processed in-order on one ring. |
| 1496 | * Check if the slot deduced from the cookie really is the first |
| 1497 | * used slot. */ |
| 1498 | firstused = ring->current_slot - ring->used_slots + 1; |
| 1499 | if (firstused < 0) |
| 1500 | firstused = ring->nr_slots + firstused; |
| 1501 | if (unlikely(slot != firstused)) { |
| 1502 | /* This possibly is a firmware bug and will result in |
| 1503 | * malfunction, memory leaks and/or stall of DMA functionality. */ |
| 1504 | b43dbg(dev->wl, "Out of order TX status report on DMA ring %d. " |
| 1505 | "Expected %d, but got %d\n", |
| 1506 | ring->index, firstused, slot); |
| 1507 | return; |
| 1508 | } |
| 1509 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1510 | ops = ring->ops; |
| 1511 | while (1) { |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1512 | B43_WARN_ON(slot < 0 || slot >= ring->nr_slots); |
Larry Finger | 9c1cacd | 2011-05-22 20:54:25 -0500 | [diff] [blame] | 1513 | /* get meta - ignore returned value */ |
| 1514 | ops->idx2desc(ring, slot, &meta); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1515 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1516 | if (b43_dma_ptr_is_poisoned(meta->skb)) { |
| 1517 | b43dbg(dev->wl, "Poisoned TX slot %d (first=%d) " |
| 1518 | "on ring %d\n", |
| 1519 | slot, firstused, ring->index); |
| 1520 | break; |
| 1521 | } |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1522 | if (meta->skb) { |
| 1523 | struct b43_private_tx_info *priv_info = |
| 1524 | b43_get_priv_tx_info(IEEE80211_SKB_CB(meta->skb)); |
| 1525 | |
| 1526 | unmap_descbuffer(ring, meta->dmaaddr, meta->skb->len, 1); |
| 1527 | kfree(priv_info->bouncebuffer); |
| 1528 | priv_info->bouncebuffer = NULL; |
| 1529 | } else { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1530 | unmap_descbuffer(ring, meta->dmaaddr, |
Michael Buesch | eb189d8b | 2008-01-28 14:47:41 -0800 | [diff] [blame] | 1531 | b43_txhdr_size(dev), 1); |
Michael Buesch | f54a520 | 2009-11-06 18:32:44 +0100 | [diff] [blame] | 1532 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1533 | |
| 1534 | if (meta->is_last_fragment) { |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1535 | struct ieee80211_tx_info *info; |
| 1536 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1537 | if (unlikely(!meta->skb)) { |
| 1538 | /* This is a scatter-gather fragment of a frame, so |
| 1539 | * the skb pointer must not be NULL. */ |
| 1540 | b43dbg(dev->wl, "TX status unexpected NULL skb " |
| 1541 | "at slot %d (first=%d) on ring %d\n", |
| 1542 | slot, firstused, ring->index); |
| 1543 | break; |
| 1544 | } |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1545 | |
| 1546 | info = IEEE80211_SKB_CB(meta->skb); |
| 1547 | |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1548 | /* |
| 1549 | * Call back to inform the ieee80211 subsystem about |
| 1550 | * the status of the transmission. |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1551 | */ |
Johannes Berg | e6a9854 | 2008-10-21 12:40:02 +0200 | [diff] [blame] | 1552 | frame_succeed = b43_fill_txstatus_report(dev, info, status); |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1553 | #ifdef CONFIG_B43_DEBUG |
| 1554 | if (frame_succeed) |
| 1555 | ring->nr_succeed_tx_packets++; |
| 1556 | else |
| 1557 | ring->nr_failed_tx_packets++; |
| 1558 | ring->nr_total_packet_tries += status->frame_count; |
| 1559 | #endif /* DEBUG */ |
Michael Buesch | ce6c4a1 | 2009-09-10 20:22:02 +0200 | [diff] [blame] | 1560 | ieee80211_tx_status(dev->wl->hw, meta->skb); |
Johannes Berg | e039fa4 | 2008-05-15 12:55:29 +0200 | [diff] [blame] | 1561 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1562 | /* skb will be freed by ieee80211_tx_status(). |
| 1563 | * Poison our pointer. */ |
| 1564 | meta->skb = B43_DMA_PTR_POISON; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1565 | } else { |
| 1566 | /* No need to call free_descriptor_buffer here, as |
| 1567 | * this is only the txhdr, which is not allocated. |
| 1568 | */ |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1569 | if (unlikely(meta->skb)) { |
| 1570 | b43dbg(dev->wl, "TX status unexpected non-NULL skb " |
| 1571 | "at slot %d (first=%d) on ring %d\n", |
| 1572 | slot, firstused, ring->index); |
| 1573 | break; |
| 1574 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | /* Everything unmapped and free'd. So it's not used anymore. */ |
| 1578 | ring->used_slots--; |
| 1579 | |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1580 | if (meta->is_last_fragment) { |
| 1581 | /* This is the last scatter-gather |
| 1582 | * fragment of the frame. We are done. */ |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1583 | break; |
Michael Buesch | 07681e2 | 2009-11-19 22:24:29 +0100 | [diff] [blame] | 1584 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1585 | slot = next_slot(ring, slot); |
| 1586 | } |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1587 | if (ring->stopped) { |
Michael Buesch | bdceeb2 | 2009-02-19 23:45:43 +0100 | [diff] [blame] | 1588 | B43_WARN_ON(free_slots(ring) < TX_SLOTS_PER_FRAME); |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 1589 | ring->stopped = false; |
francesco.gringoli@ing.unibs.it | bad6919 | 2011-12-16 18:34:56 +0100 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | if (dev->wl->tx_queue_stopped[ring->queue_prio]) { |
| 1593 | dev->wl->tx_queue_stopped[ring->queue_prio] = 0; |
| 1594 | } else { |
| 1595 | /* If the driver queue is running wake the corresponding |
| 1596 | * mac80211 queue. */ |
| 1597 | ieee80211_wake_queue(dev->wl->hw, ring->queue_prio); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1598 | if (b43_debug(dev, B43_DBG_DMAVERBOSE)) { |
| 1599 | b43dbg(dev->wl, "Woke up TX ring %d\n", ring->index); |
| 1600 | } |
| 1601 | } |
francesco.gringoli@ing.unibs.it | bad6919 | 2011-12-16 18:34:56 +0100 | [diff] [blame] | 1602 | /* Add work to the queue. */ |
| 1603 | ieee80211_queue_work(dev->wl->hw, &dev->wl->tx_work); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1604 | } |
| 1605 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1606 | static void dma_rx(struct b43_dmaring *ring, int *slot) |
| 1607 | { |
| 1608 | const struct b43_dma_ops *ops = ring->ops; |
| 1609 | struct b43_dmadesc_generic *desc; |
| 1610 | struct b43_dmadesc_meta *meta; |
| 1611 | struct b43_rxhdr_fw4 *rxhdr; |
| 1612 | struct sk_buff *skb; |
| 1613 | u16 len; |
| 1614 | int err; |
| 1615 | dma_addr_t dmaaddr; |
| 1616 | |
| 1617 | desc = ops->idx2desc(ring, *slot, &meta); |
| 1618 | |
| 1619 | sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize); |
| 1620 | skb = meta->skb; |
| 1621 | |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1622 | rxhdr = (struct b43_rxhdr_fw4 *)skb->data; |
| 1623 | len = le16_to_cpu(rxhdr->frame_len); |
| 1624 | if (len == 0) { |
| 1625 | int i = 0; |
| 1626 | |
| 1627 | do { |
| 1628 | udelay(2); |
| 1629 | barrier(); |
| 1630 | len = le16_to_cpu(rxhdr->frame_len); |
| 1631 | } while (len == 0 && i++ < 5); |
| 1632 | if (unlikely(len == 0)) { |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1633 | dmaaddr = meta->dmaaddr; |
| 1634 | goto drop_recycle_buffer; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1635 | } |
| 1636 | } |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 1637 | if (unlikely(b43_rx_buffer_is_poisoned(ring, skb))) { |
| 1638 | /* Something went wrong with the DMA. |
| 1639 | * The device did not touch the buffer and did not overwrite the poison. */ |
| 1640 | b43dbg(ring->dev->wl, "DMA RX: Dropping poisoned buffer.\n"); |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1641 | dmaaddr = meta->dmaaddr; |
| 1642 | goto drop_recycle_buffer; |
Michael Buesch | ec9a1d8 | 2009-03-27 22:51:58 +0100 | [diff] [blame] | 1643 | } |
John W. Linville | c85ce65 | 2011-03-30 14:02:46 -0400 | [diff] [blame] | 1644 | if (unlikely(len + ring->frameoffset > ring->rx_buffersize)) { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1645 | /* The data did not fit into one descriptor buffer |
| 1646 | * and is split over multiple buffers. |
| 1647 | * This should never happen, as we try to allocate buffers |
| 1648 | * big enough. So simply ignore this packet. |
| 1649 | */ |
| 1650 | int cnt = 0; |
| 1651 | s32 tmp = len; |
| 1652 | |
| 1653 | while (1) { |
| 1654 | desc = ops->idx2desc(ring, *slot, &meta); |
| 1655 | /* recycle the descriptor buffer. */ |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1656 | b43_poison_rx_buffer(ring, meta->skb); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1657 | sync_descbuffer_for_device(ring, meta->dmaaddr, |
| 1658 | ring->rx_buffersize); |
| 1659 | *slot = next_slot(ring, *slot); |
| 1660 | cnt++; |
| 1661 | tmp -= ring->rx_buffersize; |
| 1662 | if (tmp <= 0) |
| 1663 | break; |
| 1664 | } |
| 1665 | b43err(ring->dev->wl, "DMA RX buffer too small " |
| 1666 | "(len: %u, buffer: %u, nr-dropped: %d)\n", |
| 1667 | len, ring->rx_buffersize, cnt); |
| 1668 | goto drop; |
| 1669 | } |
| 1670 | |
| 1671 | dmaaddr = meta->dmaaddr; |
| 1672 | err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC); |
| 1673 | if (unlikely(err)) { |
| 1674 | b43dbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer() failed\n"); |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1675 | goto drop_recycle_buffer; |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0); |
| 1679 | skb_put(skb, len + ring->frameoffset); |
| 1680 | skb_pull(skb, ring->frameoffset); |
| 1681 | |
| 1682 | b43_rx(ring->dev, skb, rxhdr); |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1683 | drop: |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1684 | return; |
Michael Buesch | cf68636 | 2009-03-28 00:41:25 +0100 | [diff] [blame] | 1685 | |
| 1686 | drop_recycle_buffer: |
| 1687 | /* Poison and recycle the RX buffer. */ |
| 1688 | b43_poison_rx_buffer(ring, skb); |
| 1689 | sync_descbuffer_for_device(ring, dmaaddr, ring->rx_buffersize); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | void b43_dma_rx(struct b43_dmaring *ring) |
| 1693 | { |
| 1694 | const struct b43_dma_ops *ops = ring->ops; |
| 1695 | int slot, current_slot; |
| 1696 | int used_slots = 0; |
| 1697 | |
| 1698 | B43_WARN_ON(ring->tx); |
| 1699 | current_slot = ops->get_current_rxslot(ring); |
| 1700 | B43_WARN_ON(!(current_slot >= 0 && current_slot < ring->nr_slots)); |
| 1701 | |
| 1702 | slot = ring->current_slot; |
| 1703 | for (; slot != current_slot; slot = next_slot(ring, slot)) { |
| 1704 | dma_rx(ring, &slot); |
| 1705 | update_max_used_slots(ring, ++used_slots); |
| 1706 | } |
Michael Büsch | 73e6cdc | 2011-07-04 19:51:11 +0200 | [diff] [blame] | 1707 | wmb(); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1708 | ops->set_current_rxslot(ring, slot); |
| 1709 | ring->current_slot = slot; |
| 1710 | } |
| 1711 | |
| 1712 | static void b43_dma_tx_suspend_ring(struct b43_dmaring *ring) |
| 1713 | { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1714 | B43_WARN_ON(!ring->tx); |
| 1715 | ring->ops->tx_suspend(ring); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | static void b43_dma_tx_resume_ring(struct b43_dmaring *ring) |
| 1719 | { |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1720 | B43_WARN_ON(!ring->tx); |
| 1721 | ring->ops->tx_resume(ring); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | void b43_dma_tx_suspend(struct b43_wldev *dev) |
| 1725 | { |
| 1726 | b43_power_saving_ctl_bits(dev, B43_PS_AWAKE); |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1727 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BK); |
| 1728 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BE); |
| 1729 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VI); |
| 1730 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VO); |
| 1731 | b43_dma_tx_suspend_ring(dev->dma.tx_ring_mcast); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | void b43_dma_tx_resume(struct b43_wldev *dev) |
| 1735 | { |
Michael Buesch | b27faf8 | 2008-03-06 16:32:46 +0100 | [diff] [blame] | 1736 | b43_dma_tx_resume_ring(dev->dma.tx_ring_mcast); |
| 1737 | b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VO); |
| 1738 | b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VI); |
| 1739 | b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BE); |
| 1740 | b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BK); |
Michael Buesch | e4d6b79 | 2007-09-18 15:39:42 -0400 | [diff] [blame] | 1741 | b43_power_saving_ctl_bits(dev, 0); |
| 1742 | } |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1743 | |
Michael Buesch | 5100d5a | 2008-03-29 21:01:16 +0100 | [diff] [blame] | 1744 | static void direct_fifo_rx(struct b43_wldev *dev, enum b43_dmatype type, |
| 1745 | u16 mmio_base, bool enable) |
| 1746 | { |
| 1747 | u32 ctl; |
| 1748 | |
| 1749 | if (type == B43_DMA_64BIT) { |
| 1750 | ctl = b43_read32(dev, mmio_base + B43_DMA64_RXCTL); |
| 1751 | ctl &= ~B43_DMA64_RXDIRECTFIFO; |
| 1752 | if (enable) |
| 1753 | ctl |= B43_DMA64_RXDIRECTFIFO; |
| 1754 | b43_write32(dev, mmio_base + B43_DMA64_RXCTL, ctl); |
| 1755 | } else { |
| 1756 | ctl = b43_read32(dev, mmio_base + B43_DMA32_RXCTL); |
| 1757 | ctl &= ~B43_DMA32_RXDIRECTFIFO; |
| 1758 | if (enable) |
| 1759 | ctl |= B43_DMA32_RXDIRECTFIFO; |
| 1760 | b43_write32(dev, mmio_base + B43_DMA32_RXCTL, ctl); |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | /* Enable/Disable Direct FIFO Receive Mode (PIO) on a RX engine. |
| 1765 | * This is called from PIO code, so DMA structures are not available. */ |
| 1766 | void b43_dma_direct_fifo_rx(struct b43_wldev *dev, |
| 1767 | unsigned int engine_index, bool enable) |
| 1768 | { |
| 1769 | enum b43_dmatype type; |
| 1770 | u16 mmio_base; |
| 1771 | |
| 1772 | type = dma_mask_to_engine_type(supported_dma_mask(dev)); |
| 1773 | |
| 1774 | mmio_base = b43_dmacontroller_base(type, engine_index); |
| 1775 | direct_fifo_rx(dev, type, mmio_base, enable); |
| 1776 | } |