blob: b34e519332574d486ddeea02484f857394398d19 [file] [log] [blame]
Michael Buesche4d6b792007-09-18 15:39:42 -04001/*
2
3 Broadcom B43 wireless driver
4
5 DMA ringbuffer and descriptor allocation/management
6
Michael Büscheb032b92011-07-04 20:50:05 +02007 Copyright (c) 2005, 2006 Michael Buesch <m@bues.ch>
Michael Buesche4d6b792007-09-18 15:39:42 -04008
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 Buesch280d0e12007-12-26 18:26:17 +010040#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Michael Buesch57df40d2008-03-07 15:50:02 +010042#include <asm/div64.h>
Michael Buesch280d0e12007-12-26 18:26:17 +010043
Michael Buesche4d6b792007-09-18 15:39:42 -040044
Michael Bueschbdceeb22009-02-19 23:45:43 +010045/* 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łecki0cc97722011-08-14 20:16:37 +020050static 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 Bueschbdceeb22009-02-19 23:45:43 +010082
Michael Buesche4d6b792007-09-18 15:39:42 -040083/* 32bit DMA ops. */
84static
85struct 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
98static 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łecki0cc97722011-08-14 20:16:37 +0200112 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 Buesch8eccb532009-02-19 23:39:26 +0100115 ctl = bufsize & B43_DMA32_DCTL_BYTECNT;
Michael Buesche4d6b792007-09-18 15:39:42 -0400116 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
131static 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
137static 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
143static 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
149static 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
159static 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
165static 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. */
176static
177struct 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
190static 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łecki0cc97722011-08-14 20:16:37 +0200204 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 Buesche4d6b792007-09-18 15:39:42 -0400208 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 Buesch8eccb532009-02-19 23:39:26 +0100216 ctl1 |= bufsize & B43_DMA64_DCTL1_BYTECNT;
Michael Buesche4d6b792007-09-18 15:39:42 -0400217 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
226static 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
232static 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
238static 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
244static 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
254static 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
260static 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
270static inline int free_slots(struct b43_dmaring *ring)
271{
272 return (ring->nr_slots - ring->used_slots);
273}
274
275static 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
283static 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
292static 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
306static 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. */
313static 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 Bueschb79caa62008-02-05 12:50:41 +0100330static u16 b43_dmacontroller_base(enum b43_dmatype type, int controller_idx)
Michael Buesche4d6b792007-09-18 15:39:42 -0400331{
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 Bueschb79caa62008-02-05 12:50:41 +0100349 if (type == B43_DMA_64BIT) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400350 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
359static 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łeckia18c7152011-05-18 02:06:40 +0200366 dmaaddr = dma_map_single(ring->dev->dev->dma_dev,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700367 buf, len, DMA_TO_DEVICE);
Michael Buesche4d6b792007-09-18 15:39:42 -0400368 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +0200369 dmaaddr = dma_map_single(ring->dev->dev->dma_dev,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700370 buf, len, DMA_FROM_DEVICE);
Michael Buesche4d6b792007-09-18 15:39:42 -0400371 }
372
373 return dmaaddr;
374}
375
376static 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łeckia18c7152011-05-18 02:06:40 +0200381 dma_unmap_single(ring->dev->dev->dma_dev,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700382 addr, len, DMA_TO_DEVICE);
Michael Buesche4d6b792007-09-18 15:39:42 -0400383 } else {
Rafał Miłeckia18c7152011-05-18 02:06:40 +0200384 dma_unmap_single(ring->dev->dev->dma_dev,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700385 addr, len, DMA_FROM_DEVICE);
Michael Buesche4d6b792007-09-18 15:39:42 -0400386 }
387}
388
389static 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łeckia18c7152011-05-18 02:06:40 +0200394 dma_sync_single_for_cpu(ring->dev->dev->dma_dev,
Michael Bueschf2257632008-06-20 11:50:29 +0200395 addr, len, DMA_FROM_DEVICE);
Michael Buesche4d6b792007-09-18 15:39:42 -0400396}
397
398static 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łeckia18c7152011-05-18 02:06:40 +0200403 dma_sync_single_for_device(ring->dev->dev->dma_dev,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700404 addr, len, DMA_FROM_DEVICE);
Michael Buesche4d6b792007-09-18 15:39:42 -0400405}
406
407static inline
408 void free_descriptor_buffer(struct b43_dmaring *ring,
409 struct b43_dmadesc_meta *meta)
410{
411 if (meta->skb) {
Felix Fietkau78f18df2012-12-10 17:40:21 +0100412 if (ring->tx)
413 ieee80211_free_txskb(ring->dev->wl->hw, meta->skb);
414 else
415 dev_kfree_skb_any(meta->skb);
Michael Buesche4d6b792007-09-18 15:39:42 -0400416 meta->skb = NULL;
417 }
418}
419
420static int alloc_ringmemory(struct b43_dmaring *ring)
421{
John W. Linville55afc802009-12-29 14:07:42 -0500422 /* The specs call for 4K buffers for 30- and 32-bit DMA with 4K
Rafał Miłecki14a80832011-08-26 20:41:39 +0200423 * alignment and 8K buffers for 64-bit DMA with 8K alignment.
424 * In practice we could use smaller buffers for the latter, but the
425 * alignment is really important because of the hardware bug. If bit
426 * 0x00001000 is used in DMA address, some hardware (like BCM4331)
427 * copies that bit into B43_DMA64_RXSTATUS and we get false values from
428 * B43_DMA64_RXSTATDPTR. Let's just use 8K buffers even if we don't use
429 * more than 256 slots for ring.
Larry Finger013978b2007-11-26 10:29:47 -0600430 */
Rafał Miłecki14a80832011-08-26 20:41:39 +0200431 u16 ring_mem_size = (ring->type == B43_DMA_64BIT) ?
432 B43_DMA64_RINGMEMSIZE : B43_DMA32_RINGMEMSIZE;
433
Luis Chamberlain750afb02019-01-04 09:23:09 +0100434 ring->descbase = dma_alloc_coherent(ring->dev->dev->dma_dev,
435 ring_mem_size, &(ring->dmabase),
436 GFP_KERNEL);
Joe Perches1f9061d22013-03-15 07:23:58 +0000437 if (!ring->descbase)
Michael Buesche4d6b792007-09-18 15:39:42 -0400438 return -ENOMEM;
Michael Buesche4d6b792007-09-18 15:39:42 -0400439
440 return 0;
441}
442
443static void free_ringmemory(struct b43_dmaring *ring)
444{
Rafał Miłecki14a80832011-08-26 20:41:39 +0200445 u16 ring_mem_size = (ring->type == B43_DMA_64BIT) ?
446 B43_DMA64_RINGMEMSIZE : B43_DMA32_RINGMEMSIZE;
447 dma_free_coherent(ring->dev->dev->dma_dev, ring_mem_size,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700448 ring->descbase, ring->dmabase);
Michael Buesche4d6b792007-09-18 15:39:42 -0400449}
450
451/* Reset the RX DMA channel */
Michael Bueschb79caa62008-02-05 12:50:41 +0100452static int b43_dmacontroller_rx_reset(struct b43_wldev *dev, u16 mmio_base,
453 enum b43_dmatype type)
Michael Buesche4d6b792007-09-18 15:39:42 -0400454{
455 int i;
456 u32 value;
457 u16 offset;
458
459 might_sleep();
460
Michael Bueschb79caa62008-02-05 12:50:41 +0100461 offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXCTL : B43_DMA32_RXCTL;
Michael Buesche4d6b792007-09-18 15:39:42 -0400462 b43_write32(dev, mmio_base + offset, 0);
463 for (i = 0; i < 10; i++) {
Michael Bueschb79caa62008-02-05 12:50:41 +0100464 offset = (type == B43_DMA_64BIT) ? B43_DMA64_RXSTATUS :
465 B43_DMA32_RXSTATUS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400466 value = b43_read32(dev, mmio_base + offset);
Michael Bueschb79caa62008-02-05 12:50:41 +0100467 if (type == B43_DMA_64BIT) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400468 value &= B43_DMA64_RXSTAT;
469 if (value == B43_DMA64_RXSTAT_DISABLED) {
470 i = -1;
471 break;
472 }
473 } else {
474 value &= B43_DMA32_RXSTATE;
475 if (value == B43_DMA32_RXSTAT_DISABLED) {
476 i = -1;
477 break;
478 }
479 }
480 msleep(1);
481 }
482 if (i != -1) {
483 b43err(dev->wl, "DMA RX reset timed out\n");
484 return -ENODEV;
485 }
486
487 return 0;
488}
489
Larry Finger013978b2007-11-26 10:29:47 -0600490/* Reset the TX DMA channel */
Michael Bueschb79caa62008-02-05 12:50:41 +0100491static int b43_dmacontroller_tx_reset(struct b43_wldev *dev, u16 mmio_base,
492 enum b43_dmatype type)
Michael Buesche4d6b792007-09-18 15:39:42 -0400493{
494 int i;
495 u32 value;
496 u16 offset;
497
498 might_sleep();
499
500 for (i = 0; i < 10; i++) {
Michael Bueschb79caa62008-02-05 12:50:41 +0100501 offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS :
502 B43_DMA32_TXSTATUS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400503 value = b43_read32(dev, mmio_base + offset);
Michael Bueschb79caa62008-02-05 12:50:41 +0100504 if (type == B43_DMA_64BIT) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400505 value &= B43_DMA64_TXSTAT;
506 if (value == B43_DMA64_TXSTAT_DISABLED ||
507 value == B43_DMA64_TXSTAT_IDLEWAIT ||
508 value == B43_DMA64_TXSTAT_STOPPED)
509 break;
510 } else {
511 value &= B43_DMA32_TXSTATE;
512 if (value == B43_DMA32_TXSTAT_DISABLED ||
513 value == B43_DMA32_TXSTAT_IDLEWAIT ||
514 value == B43_DMA32_TXSTAT_STOPPED)
515 break;
516 }
517 msleep(1);
518 }
Michael Bueschb79caa62008-02-05 12:50:41 +0100519 offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXCTL : B43_DMA32_TXCTL;
Michael Buesche4d6b792007-09-18 15:39:42 -0400520 b43_write32(dev, mmio_base + offset, 0);
521 for (i = 0; i < 10; i++) {
Michael Bueschb79caa62008-02-05 12:50:41 +0100522 offset = (type == B43_DMA_64BIT) ? B43_DMA64_TXSTATUS :
523 B43_DMA32_TXSTATUS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400524 value = b43_read32(dev, mmio_base + offset);
Michael Bueschb79caa62008-02-05 12:50:41 +0100525 if (type == B43_DMA_64BIT) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400526 value &= B43_DMA64_TXSTAT;
527 if (value == B43_DMA64_TXSTAT_DISABLED) {
528 i = -1;
529 break;
530 }
531 } else {
532 value &= B43_DMA32_TXSTATE;
533 if (value == B43_DMA32_TXSTAT_DISABLED) {
534 i = -1;
535 break;
536 }
537 }
538 msleep(1);
539 }
540 if (i != -1) {
541 b43err(dev->wl, "DMA TX reset timed out\n");
542 return -ENODEV;
543 }
544 /* ensure the reset is completed. */
545 msleep(1);
546
547 return 0;
548}
549
Michael Bueschb79caa62008-02-05 12:50:41 +0100550/* Check if a DMA mapping address is invalid. */
551static bool b43_dma_mapping_error(struct b43_dmaring *ring,
552 dma_addr_t addr,
Michael Bueschffa92562008-03-22 22:04:45 +0100553 size_t buffersize, bool dma_to_device)
Michael Bueschb79caa62008-02-05 12:50:41 +0100554{
Rafał Miłeckia18c7152011-05-18 02:06:40 +0200555 if (unlikely(dma_mapping_error(ring->dev->dev->dma_dev, addr)))
Joe Perches12827fe2015-03-29 18:29:42 -0700556 return true;
Michael Bueschb79caa62008-02-05 12:50:41 +0100557
John W. Linville55afc802009-12-29 14:07:42 -0500558 switch (ring->type) {
559 case B43_DMA_30BIT:
560 if ((u64)addr + buffersize > (1ULL << 30))
561 goto address_error;
562 break;
563 case B43_DMA_32BIT:
564 if ((u64)addr + buffersize > (1ULL << 32))
565 goto address_error;
566 break;
567 case B43_DMA_64BIT:
568 /* Currently we can't have addresses beyond
569 * 64bit in the kernel. */
570 break;
Michael Bueschb79caa62008-02-05 12:50:41 +0100571 }
572
573 /* The address is OK. */
Joe Perches12827fe2015-03-29 18:29:42 -0700574 return false;
John W. Linville55afc802009-12-29 14:07:42 -0500575
576address_error:
577 /* We can't support this address. Unmap it again. */
578 unmap_descbuffer(ring, addr, buffersize, dma_to_device);
579
Joe Perches12827fe2015-03-29 18:29:42 -0700580 return true;
Michael Bueschb79caa62008-02-05 12:50:41 +0100581}
582
Michael Bueschec9a1d82009-03-27 22:51:58 +0100583static bool b43_rx_buffer_is_poisoned(struct b43_dmaring *ring, struct sk_buff *skb)
584{
585 unsigned char *f = skb->data + ring->frameoffset;
586
587 return ((f[0] & f[1] & f[2] & f[3] & f[4] & f[5] & f[6] & f[7]) == 0xFF);
588}
589
590static void b43_poison_rx_buffer(struct b43_dmaring *ring, struct sk_buff *skb)
591{
592 struct b43_rxhdr_fw4 *rxhdr;
593 unsigned char *frame;
594
595 /* This poisons the RX buffer to detect DMA failures. */
596
597 rxhdr = (struct b43_rxhdr_fw4 *)(skb->data);
598 rxhdr->frame_len = 0;
599
600 B43_WARN_ON(ring->rx_buffersize < ring->frameoffset + sizeof(struct b43_plcp_hdr6) + 2);
601 frame = skb->data + ring->frameoffset;
602 memset(frame, 0xFF, sizeof(struct b43_plcp_hdr6) + 2 /* padding */);
603}
604
Michael Buesche4d6b792007-09-18 15:39:42 -0400605static int setup_rx_descbuffer(struct b43_dmaring *ring,
606 struct b43_dmadesc_generic *desc,
607 struct b43_dmadesc_meta *meta, gfp_t gfp_flags)
608{
Michael Buesche4d6b792007-09-18 15:39:42 -0400609 dma_addr_t dmaaddr;
610 struct sk_buff *skb;
611
612 B43_WARN_ON(ring->tx);
613
614 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
615 if (unlikely(!skb))
616 return -ENOMEM;
Michael Bueschec9a1d82009-03-27 22:51:58 +0100617 b43_poison_rx_buffer(ring, skb);
Michael Buesche4d6b792007-09-18 15:39:42 -0400618 dmaaddr = map_descbuffer(ring, skb->data, ring->rx_buffersize, 0);
Michael Bueschffa92562008-03-22 22:04:45 +0100619 if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400620 /* ugh. try to realloc in zone_dma */
621 gfp_flags |= GFP_DMA;
622
623 dev_kfree_skb_any(skb);
624
625 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
626 if (unlikely(!skb))
627 return -ENOMEM;
Michael Bueschec9a1d82009-03-27 22:51:58 +0100628 b43_poison_rx_buffer(ring, skb);
Michael Buesche4d6b792007-09-18 15:39:42 -0400629 dmaaddr = map_descbuffer(ring, skb->data,
630 ring->rx_buffersize, 0);
Michael Bueschbdceeb22009-02-19 23:45:43 +0100631 if (b43_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
632 b43err(ring->dev->wl, "RX DMA buffer allocation failed\n");
633 dev_kfree_skb_any(skb);
634 return -EIO;
635 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400636 }
637
638 meta->skb = skb;
639 meta->dmaaddr = dmaaddr;
640 ring->ops->fill_descriptor(ring, desc, dmaaddr,
641 ring->rx_buffersize, 0, 0, 0);
642
Michael Buesche4d6b792007-09-18 15:39:42 -0400643 return 0;
644}
645
646/* Allocate the initial descbuffers.
647 * This is used for an RX ring only.
648 */
649static int alloc_initial_descbuffers(struct b43_dmaring *ring)
650{
651 int i, err = -ENOMEM;
652 struct b43_dmadesc_generic *desc;
653 struct b43_dmadesc_meta *meta;
654
655 for (i = 0; i < ring->nr_slots; i++) {
656 desc = ring->ops->idx2desc(ring, i, &meta);
657
658 err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
659 if (err) {
660 b43err(ring->dev->wl,
661 "Failed to allocate initial descbuffers\n");
662 goto err_unwind;
663 }
664 }
665 mb();
666 ring->used_slots = ring->nr_slots;
667 err = 0;
668 out:
669 return err;
670
671 err_unwind:
672 for (i--; i >= 0; i--) {
673 desc = ring->ops->idx2desc(ring, i, &meta);
674
675 unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
676 dev_kfree_skb(meta->skb);
677 }
678 goto out;
679}
680
681/* Do initial setup of the DMA controller.
682 * Reset the controller, write the ring busaddress
683 * and switch the "enable" bit on.
684 */
685static int dmacontroller_setup(struct b43_dmaring *ring)
686{
687 int err = 0;
688 u32 value;
689 u32 addrext;
Rafał Miłecki78c1ee72011-07-20 19:47:07 +0200690 bool parity = ring->dev->dma.parity;
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200691 u32 addrlo;
692 u32 addrhi;
Michael Buesche4d6b792007-09-18 15:39:42 -0400693
694 if (ring->tx) {
Michael Bueschb79caa62008-02-05 12:50:41 +0100695 if (ring->type == B43_DMA_64BIT) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400696 u64 ringbase = (u64) (ring->dmabase);
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200697 addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT);
698 addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW);
699 addrhi = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_HIGH);
Michael Buesche4d6b792007-09-18 15:39:42 -0400700
Michael Buesche4d6b792007-09-18 15:39:42 -0400701 value = B43_DMA64_TXENABLE;
702 value |= (addrext << B43_DMA64_TXADDREXT_SHIFT)
703 & B43_DMA64_TXADDREXT_MASK;
Rafał Miłecki78c1ee72011-07-20 19:47:07 +0200704 if (!parity)
705 value |= B43_DMA64_TXPARITYDISABLE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400706 b43_dma_write(ring, B43_DMA64_TXCTL, value);
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200707 b43_dma_write(ring, B43_DMA64_TXRINGLO, addrlo);
708 b43_dma_write(ring, B43_DMA64_TXRINGHI, addrhi);
Michael Buesche4d6b792007-09-18 15:39:42 -0400709 } else {
710 u32 ringbase = (u32) (ring->dmabase);
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200711 addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT);
712 addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW);
Michael Buesche4d6b792007-09-18 15:39:42 -0400713
Michael Buesche4d6b792007-09-18 15:39:42 -0400714 value = B43_DMA32_TXENABLE;
715 value |= (addrext << B43_DMA32_TXADDREXT_SHIFT)
716 & B43_DMA32_TXADDREXT_MASK;
Rafał Miłecki78c1ee72011-07-20 19:47:07 +0200717 if (!parity)
718 value |= B43_DMA32_TXPARITYDISABLE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400719 b43_dma_write(ring, B43_DMA32_TXCTL, value);
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200720 b43_dma_write(ring, B43_DMA32_TXRING, addrlo);
Michael Buesche4d6b792007-09-18 15:39:42 -0400721 }
722 } else {
723 err = alloc_initial_descbuffers(ring);
724 if (err)
725 goto out;
Michael Bueschb79caa62008-02-05 12:50:41 +0100726 if (ring->type == B43_DMA_64BIT) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400727 u64 ringbase = (u64) (ring->dmabase);
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200728 addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT);
729 addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW);
730 addrhi = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_HIGH);
Michael Buesche4d6b792007-09-18 15:39:42 -0400731
Michael Buesche4d6b792007-09-18 15:39:42 -0400732 value = (ring->frameoffset << B43_DMA64_RXFROFF_SHIFT);
733 value |= B43_DMA64_RXENABLE;
734 value |= (addrext << B43_DMA64_RXADDREXT_SHIFT)
735 & B43_DMA64_RXADDREXT_MASK;
Rafał Miłecki78c1ee72011-07-20 19:47:07 +0200736 if (!parity)
737 value |= B43_DMA64_RXPARITYDISABLE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400738 b43_dma_write(ring, B43_DMA64_RXCTL, value);
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200739 b43_dma_write(ring, B43_DMA64_RXRINGLO, addrlo);
740 b43_dma_write(ring, B43_DMA64_RXRINGHI, addrhi);
Larry Finger013978b2007-11-26 10:29:47 -0600741 b43_dma_write(ring, B43_DMA64_RXINDEX, ring->nr_slots *
742 sizeof(struct b43_dmadesc64));
Michael Buesche4d6b792007-09-18 15:39:42 -0400743 } else {
744 u32 ringbase = (u32) (ring->dmabase);
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200745 addrext = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_EXT);
746 addrlo = b43_dma_address(&ring->dev->dma, ringbase, B43_DMA_ADDR_LOW);
Michael Buesche4d6b792007-09-18 15:39:42 -0400747
Michael Buesche4d6b792007-09-18 15:39:42 -0400748 value = (ring->frameoffset << B43_DMA32_RXFROFF_SHIFT);
749 value |= B43_DMA32_RXENABLE;
750 value |= (addrext << B43_DMA32_RXADDREXT_SHIFT)
751 & B43_DMA32_RXADDREXT_MASK;
Rafał Miłecki78c1ee72011-07-20 19:47:07 +0200752 if (!parity)
753 value |= B43_DMA32_RXPARITYDISABLE;
Michael Buesche4d6b792007-09-18 15:39:42 -0400754 b43_dma_write(ring, B43_DMA32_RXCTL, value);
Rafał Miłecki0cc97722011-08-14 20:16:37 +0200755 b43_dma_write(ring, B43_DMA32_RXRING, addrlo);
Larry Finger013978b2007-11-26 10:29:47 -0600756 b43_dma_write(ring, B43_DMA32_RXINDEX, ring->nr_slots *
757 sizeof(struct b43_dmadesc32));
Michael Buesche4d6b792007-09-18 15:39:42 -0400758 }
759 }
760
Larry Finger013978b2007-11-26 10:29:47 -0600761out:
Michael Buesche4d6b792007-09-18 15:39:42 -0400762 return err;
763}
764
765/* Shutdown the DMA controller. */
766static void dmacontroller_cleanup(struct b43_dmaring *ring)
767{
768 if (ring->tx) {
769 b43_dmacontroller_tx_reset(ring->dev, ring->mmio_base,
Michael Bueschb79caa62008-02-05 12:50:41 +0100770 ring->type);
771 if (ring->type == B43_DMA_64BIT) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400772 b43_dma_write(ring, B43_DMA64_TXRINGLO, 0);
773 b43_dma_write(ring, B43_DMA64_TXRINGHI, 0);
774 } else
775 b43_dma_write(ring, B43_DMA32_TXRING, 0);
776 } else {
777 b43_dmacontroller_rx_reset(ring->dev, ring->mmio_base,
Michael Bueschb79caa62008-02-05 12:50:41 +0100778 ring->type);
779 if (ring->type == B43_DMA_64BIT) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400780 b43_dma_write(ring, B43_DMA64_RXRINGLO, 0);
781 b43_dma_write(ring, B43_DMA64_RXRINGHI, 0);
782 } else
783 b43_dma_write(ring, B43_DMA32_RXRING, 0);
784 }
785}
786
787static void free_all_descbuffers(struct b43_dmaring *ring)
788{
Michael Buesche4d6b792007-09-18 15:39:42 -0400789 struct b43_dmadesc_meta *meta;
790 int i;
791
792 if (!ring->used_slots)
793 return;
794 for (i = 0; i < ring->nr_slots; i++) {
Larry Finger9c1cacd2011-05-22 20:54:25 -0500795 /* get meta - ignore returned value */
796 ring->ops->idx2desc(ring, i, &meta);
Michael Buesche4d6b792007-09-18 15:39:42 -0400797
Michael Buesch07681e22009-11-19 22:24:29 +0100798 if (!meta->skb || b43_dma_ptr_is_poisoned(meta->skb)) {
Michael Buesche4d6b792007-09-18 15:39:42 -0400799 B43_WARN_ON(!ring->tx);
800 continue;
801 }
802 if (ring->tx) {
803 unmap_descbuffer(ring, meta->dmaaddr,
804 meta->skb->len, 1);
805 } else {
806 unmap_descbuffer(ring, meta->dmaaddr,
807 ring->rx_buffersize, 0);
808 }
809 free_descriptor_buffer(ring, meta);
810 }
811}
812
813static u64 supported_dma_mask(struct b43_wldev *dev)
814{
815 u32 tmp;
816 u16 mmio_base;
817
Hauke Mehrtens5b36c9b2011-07-23 13:57:33 +0200818 switch (dev->dev->bus_type) {
819#ifdef CONFIG_B43_BCMA
820 case B43_BUS_BCMA:
821 tmp = bcma_aread32(dev->dev->bdev, BCMA_IOST);
822 if (tmp & BCMA_IOST_DMA64)
823 return DMA_BIT_MASK(64);
824 break;
825#endif
826#ifdef CONFIG_B43_SSB
827 case B43_BUS_SSB:
828 tmp = ssb_read32(dev->dev->sdev, SSB_TMSHIGH);
829 if (tmp & SSB_TMSHIGH_DMA64)
830 return DMA_BIT_MASK(64);
831 break;
832#endif
833 }
834
Michael Buesche4d6b792007-09-18 15:39:42 -0400835 mmio_base = b43_dmacontroller_base(0, 0);
836 b43_write32(dev, mmio_base + B43_DMA32_TXCTL, B43_DMA32_TXADDREXT_MASK);
837 tmp = b43_read32(dev, mmio_base + B43_DMA32_TXCTL);
838 if (tmp & B43_DMA32_TXADDREXT_MASK)
Yang Hongyang284901a2009-04-06 19:01:15 -0700839 return DMA_BIT_MASK(32);
Michael Buesche4d6b792007-09-18 15:39:42 -0400840
Yang Hongyang28b76792009-04-06 19:01:17 -0700841 return DMA_BIT_MASK(30);
Michael Buesche4d6b792007-09-18 15:39:42 -0400842}
843
Michael Buesch5100d5a2008-03-29 21:01:16 +0100844static enum b43_dmatype dma_mask_to_engine_type(u64 dmamask)
845{
Yang Hongyang28b76792009-04-06 19:01:17 -0700846 if (dmamask == DMA_BIT_MASK(30))
Michael Buesch5100d5a2008-03-29 21:01:16 +0100847 return B43_DMA_30BIT;
Yang Hongyang284901a2009-04-06 19:01:15 -0700848 if (dmamask == DMA_BIT_MASK(32))
Michael Buesch5100d5a2008-03-29 21:01:16 +0100849 return B43_DMA_32BIT;
Yang Hongyang6a355282009-04-06 19:01:13 -0700850 if (dmamask == DMA_BIT_MASK(64))
Michael Buesch5100d5a2008-03-29 21:01:16 +0100851 return B43_DMA_64BIT;
852 B43_WARN_ON(1);
853 return B43_DMA_30BIT;
854}
855
Michael Buesche4d6b792007-09-18 15:39:42 -0400856/* Main initialization function. */
857static
858struct b43_dmaring *b43_setup_dmaring(struct b43_wldev *dev,
859 int controller_index,
Michael Bueschb79caa62008-02-05 12:50:41 +0100860 int for_tx,
861 enum b43_dmatype type)
Michael Buesche4d6b792007-09-18 15:39:42 -0400862{
863 struct b43_dmaring *ring;
Michael Buesch07681e22009-11-19 22:24:29 +0100864 int i, err;
Michael Buesche4d6b792007-09-18 15:39:42 -0400865 dma_addr_t dma_test;
866
867 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
868 if (!ring)
869 goto out;
870
Michael Buesch028118a2008-06-12 11:58:56 +0200871 ring->nr_slots = B43_RXRING_SLOTS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400872 if (for_tx)
Michael Buesch028118a2008-06-12 11:58:56 +0200873 ring->nr_slots = B43_TXRING_SLOTS;
Michael Buesche4d6b792007-09-18 15:39:42 -0400874
Michael Buesch028118a2008-06-12 11:58:56 +0200875 ring->meta = kcalloc(ring->nr_slots, sizeof(struct b43_dmadesc_meta),
Michael Buesche4d6b792007-09-18 15:39:42 -0400876 GFP_KERNEL);
877 if (!ring->meta)
878 goto err_kfree_ring;
Michael Buesch07681e22009-11-19 22:24:29 +0100879 for (i = 0; i < ring->nr_slots; i++)
880 ring->meta->skb = B43_DMA_PTR_POISON;
Michael Buesche4d6b792007-09-18 15:39:42 -0400881
Michael Buesch028118a2008-06-12 11:58:56 +0200882 ring->type = type;
Michael Buesche4d6b792007-09-18 15:39:42 -0400883 ring->dev = dev;
Michael Bueschb79caa62008-02-05 12:50:41 +0100884 ring->mmio_base = b43_dmacontroller_base(type, controller_index);
Michael Buesche4d6b792007-09-18 15:39:42 -0400885 ring->index = controller_index;
Michael Bueschb79caa62008-02-05 12:50:41 +0100886 if (type == B43_DMA_64BIT)
Michael Buesche4d6b792007-09-18 15:39:42 -0400887 ring->ops = &dma64_ops;
888 else
889 ring->ops = &dma32_ops;
890 if (for_tx) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000891 ring->tx = true;
Michael Buesche4d6b792007-09-18 15:39:42 -0400892 ring->current_slot = -1;
893 } else {
894 if (ring->index == 0) {
Rafał Miłecki17030f42011-08-11 17:16:27 +0200895 switch (dev->fw.hdr_format) {
896 case B43_FW_HDR_598:
897 ring->rx_buffersize = B43_DMA0_RX_FW598_BUFSIZE;
898 ring->frameoffset = B43_DMA0_RX_FW598_FO;
899 break;
900 case B43_FW_HDR_410:
901 case B43_FW_HDR_351:
902 ring->rx_buffersize = B43_DMA0_RX_FW351_BUFSIZE;
903 ring->frameoffset = B43_DMA0_RX_FW351_FO;
904 break;
905 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400906 } else
907 B43_WARN_ON(1);
908 }
Michael Buesche4d6b792007-09-18 15:39:42 -0400909#ifdef CONFIG_B43_DEBUG
910 ring->last_injected_overflow = jiffies;
911#endif
912
Michael Buesch028118a2008-06-12 11:58:56 +0200913 if (for_tx) {
Michael Buesch2d071ca2009-02-20 12:24:52 +0100914 /* Assumption: B43_TXRING_SLOTS can be divided by TX_SLOTS_PER_FRAME */
915 BUILD_BUG_ON(B43_TXRING_SLOTS % TX_SLOTS_PER_FRAME != 0);
916
Michael Bueschbdceeb22009-02-19 23:45:43 +0100917 ring->txhdr_cache = kcalloc(ring->nr_slots / TX_SLOTS_PER_FRAME,
Michael Buesch028118a2008-06-12 11:58:56 +0200918 b43_txhdr_size(dev),
919 GFP_KERNEL);
920 if (!ring->txhdr_cache)
921 goto err_kfree_meta;
922
923 /* test for ability to dma to txhdr_cache */
Rafał Miłeckia18c7152011-05-18 02:06:40 +0200924 dma_test = dma_map_single(dev->dev->dma_dev,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700925 ring->txhdr_cache,
926 b43_txhdr_size(dev),
927 DMA_TO_DEVICE);
Michael Buesch028118a2008-06-12 11:58:56 +0200928
929 if (b43_dma_mapping_error(ring, dma_test,
930 b43_txhdr_size(dev), 1)) {
931 /* ugh realloc */
932 kfree(ring->txhdr_cache);
Michael Bueschbdceeb22009-02-19 23:45:43 +0100933 ring->txhdr_cache = kcalloc(ring->nr_slots / TX_SLOTS_PER_FRAME,
Michael Buesch028118a2008-06-12 11:58:56 +0200934 b43_txhdr_size(dev),
935 GFP_KERNEL | GFP_DMA);
936 if (!ring->txhdr_cache)
937 goto err_kfree_meta;
938
Rafał Miłeckia18c7152011-05-18 02:06:40 +0200939 dma_test = dma_map_single(dev->dev->dma_dev,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700940 ring->txhdr_cache,
941 b43_txhdr_size(dev),
942 DMA_TO_DEVICE);
Michael Buesch028118a2008-06-12 11:58:56 +0200943
944 if (b43_dma_mapping_error(ring, dma_test,
945 b43_txhdr_size(dev), 1)) {
946
947 b43err(dev->wl,
948 "TXHDR DMA allocation failed\n");
949 goto err_kfree_txhdr_cache;
950 }
951 }
952
Rafał Miłeckia18c7152011-05-18 02:06:40 +0200953 dma_unmap_single(dev->dev->dma_dev,
FUJITA Tomonori718e8892010-06-03 19:37:36 -0700954 dma_test, b43_txhdr_size(dev),
955 DMA_TO_DEVICE);
Michael Buesch028118a2008-06-12 11:58:56 +0200956 }
957
Michael Buesche4d6b792007-09-18 15:39:42 -0400958 err = alloc_ringmemory(ring);
959 if (err)
960 goto err_kfree_txhdr_cache;
961 err = dmacontroller_setup(ring);
962 if (err)
963 goto err_free_ringmemory;
964
965 out:
966 return ring;
967
968 err_free_ringmemory:
969 free_ringmemory(ring);
970 err_kfree_txhdr_cache:
971 kfree(ring->txhdr_cache);
972 err_kfree_meta:
973 kfree(ring->meta);
974 err_kfree_ring:
975 kfree(ring);
976 ring = NULL;
977 goto out;
978}
979
Michael Buesch57df40d2008-03-07 15:50:02 +0100980#define divide(a, b) ({ \
981 typeof(a) __a = a; \
982 do_div(__a, b); \
983 __a; \
984 })
985
986#define modulo(a, b) ({ \
987 typeof(a) __a = a; \
988 do_div(__a, b); \
989 })
990
Michael Buesche4d6b792007-09-18 15:39:42 -0400991/* Main cleanup function. */
Michael Bueschb27faf82008-03-06 16:32:46 +0100992static void b43_destroy_dmaring(struct b43_dmaring *ring,
993 const char *ringname)
Michael Buesche4d6b792007-09-18 15:39:42 -0400994{
995 if (!ring)
996 return;
997
Michael Buesch57df40d2008-03-07 15:50:02 +0100998#ifdef CONFIG_B43_DEBUG
999 {
1000 /* Print some statistics. */
1001 u64 failed_packets = ring->nr_failed_tx_packets;
1002 u64 succeed_packets = ring->nr_succeed_tx_packets;
1003 u64 nr_packets = failed_packets + succeed_packets;
1004 u64 permille_failed = 0, average_tries = 0;
1005
1006 if (nr_packets)
1007 permille_failed = divide(failed_packets * 1000, nr_packets);
1008 if (nr_packets)
1009 average_tries = divide(ring->nr_total_packet_tries * 100, nr_packets);
1010
1011 b43dbg(ring->dev->wl, "DMA-%u %s: "
1012 "Used slots %d/%d, Failed frames %llu/%llu = %llu.%01llu%%, "
1013 "Average tries %llu.%02llu\n",
1014 (unsigned int)(ring->type), ringname,
1015 ring->max_used_slots,
1016 ring->nr_slots,
1017 (unsigned long long)failed_packets,
Michael Buesch87d96112008-03-07 19:52:24 +01001018 (unsigned long long)nr_packets,
Michael Buesch57df40d2008-03-07 15:50:02 +01001019 (unsigned long long)divide(permille_failed, 10),
1020 (unsigned long long)modulo(permille_failed, 10),
1021 (unsigned long long)divide(average_tries, 100),
1022 (unsigned long long)modulo(average_tries, 100));
1023 }
1024#endif /* DEBUG */
1025
Michael Buesche4d6b792007-09-18 15:39:42 -04001026 /* Device IRQs are disabled prior entering this function,
1027 * so no need to take care of concurrency with rx handler stuff.
1028 */
1029 dmacontroller_cleanup(ring);
1030 free_all_descbuffers(ring);
1031 free_ringmemory(ring);
1032
1033 kfree(ring->txhdr_cache);
1034 kfree(ring->meta);
1035 kfree(ring);
1036}
1037
Michael Bueschb27faf82008-03-06 16:32:46 +01001038#define destroy_ring(dma, ring) do { \
1039 b43_destroy_dmaring((dma)->ring, __stringify(ring)); \
1040 (dma)->ring = NULL; \
1041 } while (0)
1042
Michael Buesche4d6b792007-09-18 15:39:42 -04001043void b43_dma_free(struct b43_wldev *dev)
1044{
Michael Buesch5100d5a2008-03-29 21:01:16 +01001045 struct b43_dma *dma;
1046
1047 if (b43_using_pio_transfers(dev))
1048 return;
1049 dma = &dev->dma;
Michael Buesche4d6b792007-09-18 15:39:42 -04001050
Michael Bueschb27faf82008-03-06 16:32:46 +01001051 destroy_ring(dma, rx_ring);
1052 destroy_ring(dma, tx_ring_AC_BK);
1053 destroy_ring(dma, tx_ring_AC_BE);
1054 destroy_ring(dma, tx_ring_AC_VI);
1055 destroy_ring(dma, tx_ring_AC_VO);
1056 destroy_ring(dma, tx_ring_mcast);
Michael Buesche4d6b792007-09-18 15:39:42 -04001057}
1058
Michael Buesch1033b3e2008-04-23 19:13:01 +02001059static int b43_dma_set_mask(struct b43_wldev *dev, u64 mask)
1060{
1061 u64 orig_mask = mask;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001062 bool fallback = false;
Michael Buesch1033b3e2008-04-23 19:13:01 +02001063 int err;
1064
1065 /* Try to set the DMA mask. If it fails, try falling back to a
1066 * lower mask, as we can always also support a lower one. */
1067 while (1) {
Russell Kinge1390a22013-06-26 23:49:11 +01001068 err = dma_set_mask_and_coherent(dev->dev->dma_dev, mask);
1069 if (!err)
1070 break;
Yang Hongyang6a355282009-04-06 19:01:13 -07001071 if (mask == DMA_BIT_MASK(64)) {
Yang Hongyang284901a2009-04-06 19:01:15 -07001072 mask = DMA_BIT_MASK(32);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001073 fallback = true;
Michael Buesch1033b3e2008-04-23 19:13:01 +02001074 continue;
1075 }
Yang Hongyang284901a2009-04-06 19:01:15 -07001076 if (mask == DMA_BIT_MASK(32)) {
Yang Hongyang28b76792009-04-06 19:01:17 -07001077 mask = DMA_BIT_MASK(30);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001078 fallback = true;
Michael Buesch1033b3e2008-04-23 19:13:01 +02001079 continue;
1080 }
1081 b43err(dev->wl, "The machine/kernel does not support "
1082 "the required %u-bit DMA mask\n",
1083 (unsigned int)dma_mask_to_engine_type(orig_mask));
1084 return -EOPNOTSUPP;
1085 }
1086 if (fallback) {
1087 b43info(dev->wl, "DMA mask fallback from %u-bit to %u-bit\n",
1088 (unsigned int)dma_mask_to_engine_type(orig_mask),
1089 (unsigned int)dma_mask_to_engine_type(mask));
1090 }
1091
1092 return 0;
1093}
1094
Rafał Miłecki0cc97722011-08-14 20:16:37 +02001095/* Some hardware with 64-bit DMA seems to be bugged and looks for translation
1096 * bit in low address word instead of high one.
1097 */
1098static bool b43_dma_translation_in_low_word(struct b43_wldev *dev,
1099 enum b43_dmatype type)
1100{
1101 if (type != B43_DMA_64BIT)
Joe Perches12827fe2015-03-29 18:29:42 -07001102 return true;
Rafał Miłecki0cc97722011-08-14 20:16:37 +02001103
1104#ifdef CONFIG_B43_SSB
1105 if (dev->dev->bus_type == B43_BUS_SSB &&
1106 dev->dev->sdev->bus->bustype == SSB_BUSTYPE_PCI &&
Bjorn Helgaasdfcfb542012-05-08 17:27:32 -06001107 !(pci_is_pcie(dev->dev->sdev->bus->host_pci) &&
Rafał Miłecki0cc97722011-08-14 20:16:37 +02001108 ssb_read32(dev->dev->sdev, SSB_TMSHIGH) & SSB_TMSHIGH_DMA64))
Joe Perches12827fe2015-03-29 18:29:42 -07001109 return true;
Rafał Miłecki0cc97722011-08-14 20:16:37 +02001110#endif
Joe Perches12827fe2015-03-29 18:29:42 -07001111 return false;
Rafał Miłecki0cc97722011-08-14 20:16:37 +02001112}
1113
Michael Buesche4d6b792007-09-18 15:39:42 -04001114int b43_dma_init(struct b43_wldev *dev)
1115{
1116 struct b43_dma *dma = &dev->dma;
Michael Buesche4d6b792007-09-18 15:39:42 -04001117 int err;
1118 u64 dmamask;
Michael Bueschb79caa62008-02-05 12:50:41 +01001119 enum b43_dmatype type;
Michael Buesche4d6b792007-09-18 15:39:42 -04001120
1121 dmamask = supported_dma_mask(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +01001122 type = dma_mask_to_engine_type(dmamask);
Michael Buesch1033b3e2008-04-23 19:13:01 +02001123 err = b43_dma_set_mask(dev, dmamask);
1124 if (err)
1125 return err;
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001126
1127 switch (dev->dev->bus_type) {
Rafał Miłeckieb90e9e2011-07-20 19:52:16 +02001128#ifdef CONFIG_B43_BCMA
1129 case B43_BUS_BCMA:
1130 dma->translation = bcma_core_dma_translation(dev->dev->bdev);
1131 break;
1132#endif
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02001133#ifdef CONFIG_B43_SSB
1134 case B43_BUS_SSB:
1135 dma->translation = ssb_dma_translation(dev->dev->sdev);
1136 break;
1137#endif
1138 }
Rafał Miłecki0cc97722011-08-14 20:16:37 +02001139 dma->translation_in_low = b43_dma_translation_in_low_word(dev, type);
Michael Buesche4d6b792007-09-18 15:39:42 -04001140
Rafał Miłecki78c1ee72011-07-20 19:47:07 +02001141 dma->parity = true;
1142#ifdef CONFIG_B43_BCMA
1143 /* TODO: find out which SSB devices need disabling parity */
1144 if (dev->dev->bus_type == B43_BUS_BCMA)
1145 dma->parity = false;
1146#endif
1147
Michael Buesche4d6b792007-09-18 15:39:42 -04001148 err = -ENOMEM;
1149 /* setup TX DMA channels. */
Michael Bueschb27faf82008-03-06 16:32:46 +01001150 dma->tx_ring_AC_BK = b43_setup_dmaring(dev, 0, 1, type);
1151 if (!dma->tx_ring_AC_BK)
Michael Buesche4d6b792007-09-18 15:39:42 -04001152 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -04001153
Michael Bueschb27faf82008-03-06 16:32:46 +01001154 dma->tx_ring_AC_BE = b43_setup_dmaring(dev, 1, 1, type);
1155 if (!dma->tx_ring_AC_BE)
1156 goto err_destroy_bk;
Michael Buesche4d6b792007-09-18 15:39:42 -04001157
Michael Bueschb27faf82008-03-06 16:32:46 +01001158 dma->tx_ring_AC_VI = b43_setup_dmaring(dev, 2, 1, type);
1159 if (!dma->tx_ring_AC_VI)
1160 goto err_destroy_be;
Michael Buesche4d6b792007-09-18 15:39:42 -04001161
Michael Bueschb27faf82008-03-06 16:32:46 +01001162 dma->tx_ring_AC_VO = b43_setup_dmaring(dev, 3, 1, type);
1163 if (!dma->tx_ring_AC_VO)
1164 goto err_destroy_vi;
Michael Buesche4d6b792007-09-18 15:39:42 -04001165
Michael Bueschb27faf82008-03-06 16:32:46 +01001166 dma->tx_ring_mcast = b43_setup_dmaring(dev, 4, 1, type);
1167 if (!dma->tx_ring_mcast)
1168 goto err_destroy_vo;
Michael Buesche4d6b792007-09-18 15:39:42 -04001169
Michael Bueschb27faf82008-03-06 16:32:46 +01001170 /* setup RX DMA channel. */
1171 dma->rx_ring = b43_setup_dmaring(dev, 0, 0, type);
1172 if (!dma->rx_ring)
1173 goto err_destroy_mcast;
Michael Buesche4d6b792007-09-18 15:39:42 -04001174
Michael Bueschb27faf82008-03-06 16:32:46 +01001175 /* No support for the TX status DMA ring. */
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001176 B43_WARN_ON(dev->dev->core_rev < 5);
Michael Buesche4d6b792007-09-18 15:39:42 -04001177
Michael Bueschb79caa62008-02-05 12:50:41 +01001178 b43dbg(dev->wl, "%u-bit DMA initialized\n",
1179 (unsigned int)type);
Michael Buesche4d6b792007-09-18 15:39:42 -04001180 err = 0;
Michael Bueschb27faf82008-03-06 16:32:46 +01001181out:
Michael Buesche4d6b792007-09-18 15:39:42 -04001182 return err;
1183
Michael Bueschb27faf82008-03-06 16:32:46 +01001184err_destroy_mcast:
1185 destroy_ring(dma, tx_ring_mcast);
1186err_destroy_vo:
1187 destroy_ring(dma, tx_ring_AC_VO);
1188err_destroy_vi:
1189 destroy_ring(dma, tx_ring_AC_VI);
1190err_destroy_be:
1191 destroy_ring(dma, tx_ring_AC_BE);
1192err_destroy_bk:
1193 destroy_ring(dma, tx_ring_AC_BK);
1194 return err;
Michael Buesche4d6b792007-09-18 15:39:42 -04001195}
1196
1197/* Generate a cookie for the TX header. */
1198static u16 generate_cookie(struct b43_dmaring *ring, int slot)
1199{
Michael Bueschb27faf82008-03-06 16:32:46 +01001200 u16 cookie;
Michael Buesche4d6b792007-09-18 15:39:42 -04001201
1202 /* Use the upper 4 bits of the cookie as
1203 * DMA controller ID and store the slot number
1204 * in the lower 12 bits.
1205 * Note that the cookie must never be 0, as this
1206 * is a special value used in RX path.
Michael Buesch280d0e12007-12-26 18:26:17 +01001207 * It can also not be 0xFFFF because that is special
1208 * for multicast frames.
Michael Buesche4d6b792007-09-18 15:39:42 -04001209 */
Michael Bueschb27faf82008-03-06 16:32:46 +01001210 cookie = (((u16)ring->index + 1) << 12);
Michael Buesche4d6b792007-09-18 15:39:42 -04001211 B43_WARN_ON(slot & ~0x0FFF);
Michael Bueschb27faf82008-03-06 16:32:46 +01001212 cookie |= (u16)slot;
Michael Buesche4d6b792007-09-18 15:39:42 -04001213
1214 return cookie;
1215}
1216
1217/* Inspect a cookie and find out to which controller/slot it belongs. */
1218static
1219struct b43_dmaring *parse_cookie(struct b43_wldev *dev, u16 cookie, int *slot)
1220{
1221 struct b43_dma *dma = &dev->dma;
1222 struct b43_dmaring *ring = NULL;
1223
1224 switch (cookie & 0xF000) {
Michael Buesch280d0e12007-12-26 18:26:17 +01001225 case 0x1000:
Michael Bueschb27faf82008-03-06 16:32:46 +01001226 ring = dma->tx_ring_AC_BK;
Michael Buesche4d6b792007-09-18 15:39:42 -04001227 break;
Michael Buesch280d0e12007-12-26 18:26:17 +01001228 case 0x2000:
Michael Bueschb27faf82008-03-06 16:32:46 +01001229 ring = dma->tx_ring_AC_BE;
Michael Buesche4d6b792007-09-18 15:39:42 -04001230 break;
Michael Buesch280d0e12007-12-26 18:26:17 +01001231 case 0x3000:
Michael Bueschb27faf82008-03-06 16:32:46 +01001232 ring = dma->tx_ring_AC_VI;
Michael Buesche4d6b792007-09-18 15:39:42 -04001233 break;
Michael Buesch280d0e12007-12-26 18:26:17 +01001234 case 0x4000:
Michael Bueschb27faf82008-03-06 16:32:46 +01001235 ring = dma->tx_ring_AC_VO;
Michael Buesche4d6b792007-09-18 15:39:42 -04001236 break;
Michael Buesch280d0e12007-12-26 18:26:17 +01001237 case 0x5000:
Michael Bueschb27faf82008-03-06 16:32:46 +01001238 ring = dma->tx_ring_mcast;
Michael Buesche4d6b792007-09-18 15:39:42 -04001239 break;
Michael Buesche4d6b792007-09-18 15:39:42 -04001240 }
1241 *slot = (cookie & 0x0FFF);
Michael Buesch07681e22009-11-19 22:24:29 +01001242 if (unlikely(!ring || *slot < 0 || *slot >= ring->nr_slots)) {
1243 b43dbg(dev->wl, "TX-status contains "
1244 "invalid cookie: 0x%04X\n", cookie);
1245 return NULL;
1246 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001247
1248 return ring;
1249}
1250
1251static int dma_tx_fragment(struct b43_dmaring *ring,
Michael Bueschf54a5202009-11-06 18:32:44 +01001252 struct sk_buff *skb)
Michael Buesche4d6b792007-09-18 15:39:42 -04001253{
1254 const struct b43_dma_ops *ops = ring->ops;
Johannes Berge039fa42008-05-15 12:55:29 +02001255 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Michael Bueschf54a5202009-11-06 18:32:44 +01001256 struct b43_private_tx_info *priv_info = b43_get_priv_tx_info(info);
Michael Buesche4d6b792007-09-18 15:39:42 -04001257 u8 *header;
Michael Buesch09552cc2008-01-23 21:44:15 +01001258 int slot, old_top_slot, old_used_slots;
Michael Buesche4d6b792007-09-18 15:39:42 -04001259 int err;
1260 struct b43_dmadesc_generic *desc;
1261 struct b43_dmadesc_meta *meta;
1262 struct b43_dmadesc_meta *meta_hdr;
Michael Buesch280d0e12007-12-26 18:26:17 +01001263 u16 cookie;
Michael Buescheb189d8b2008-01-28 14:47:41 -08001264 size_t hdrsize = b43_txhdr_size(ring->dev);
Michael Buesche4d6b792007-09-18 15:39:42 -04001265
Michael Bueschbdceeb22009-02-19 23:45:43 +01001266 /* Important note: If the number of used DMA slots per TX frame
1267 * is changed here, the TX_SLOTS_PER_FRAME definition at the top of
1268 * the file has to be updated, too!
1269 */
Michael Buesche4d6b792007-09-18 15:39:42 -04001270
Michael Buesch09552cc2008-01-23 21:44:15 +01001271 old_top_slot = ring->current_slot;
1272 old_used_slots = ring->used_slots;
1273
Michael Buesche4d6b792007-09-18 15:39:42 -04001274 /* Get a slot for the header. */
1275 slot = request_slot(ring);
1276 desc = ops->idx2desc(ring, slot, &meta_hdr);
1277 memset(meta_hdr, 0, sizeof(*meta_hdr));
1278
Michael Bueschbdceeb22009-02-19 23:45:43 +01001279 header = &(ring->txhdr_cache[(slot / TX_SLOTS_PER_FRAME) * hdrsize]);
Michael Buesch280d0e12007-12-26 18:26:17 +01001280 cookie = generate_cookie(ring, slot);
Michael Buesch09552cc2008-01-23 21:44:15 +01001281 err = b43_generate_txhdr(ring->dev, header,
gregor kowski035d0242009-08-19 22:35:45 +02001282 skb, info, cookie);
Michael Buesch09552cc2008-01-23 21:44:15 +01001283 if (unlikely(err)) {
1284 ring->current_slot = old_top_slot;
1285 ring->used_slots = old_used_slots;
1286 return err;
1287 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001288
1289 meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header,
Michael Buescheb189d8b2008-01-28 14:47:41 -08001290 hdrsize, 1);
Michael Bueschffa92562008-03-22 22:04:45 +01001291 if (b43_dma_mapping_error(ring, meta_hdr->dmaaddr, hdrsize, 1)) {
Michael Buesch09552cc2008-01-23 21:44:15 +01001292 ring->current_slot = old_top_slot;
1293 ring->used_slots = old_used_slots;
Michael Buesche4d6b792007-09-18 15:39:42 -04001294 return -EIO;
Michael Buesch09552cc2008-01-23 21:44:15 +01001295 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001296 ops->fill_descriptor(ring, desc, meta_hdr->dmaaddr,
Michael Buescheb189d8b2008-01-28 14:47:41 -08001297 hdrsize, 1, 0, 0);
Michael Buesche4d6b792007-09-18 15:39:42 -04001298
1299 /* Get a slot for the payload. */
1300 slot = request_slot(ring);
1301 desc = ops->idx2desc(ring, slot, &meta);
1302 memset(meta, 0, sizeof(*meta));
1303
Michael Buesche4d6b792007-09-18 15:39:42 -04001304 meta->skb = skb;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001305 meta->is_last_fragment = true;
Michael Bueschf54a5202009-11-06 18:32:44 +01001306 priv_info->bouncebuffer = NULL;
Michael Buesche4d6b792007-09-18 15:39:42 -04001307
1308 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
1309 /* create a bounce buffer in zone_dma on mapping failure. */
Michael Bueschffa92562008-03-22 22:04:45 +01001310 if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
Julia Lawalla61aac72010-05-15 23:20:26 +02001311 priv_info->bouncebuffer = kmemdup(skb->data, skb->len,
1312 GFP_ATOMIC | GFP_DMA);
Michael Bueschf54a5202009-11-06 18:32:44 +01001313 if (!priv_info->bouncebuffer) {
Michael Buesch09552cc2008-01-23 21:44:15 +01001314 ring->current_slot = old_top_slot;
1315 ring->used_slots = old_used_slots;
Michael Buesche4d6b792007-09-18 15:39:42 -04001316 err = -ENOMEM;
1317 goto out_unmap_hdr;
1318 }
1319
Michael Bueschf54a5202009-11-06 18:32:44 +01001320 meta->dmaaddr = map_descbuffer(ring, priv_info->bouncebuffer, skb->len, 1);
Michael Bueschffa92562008-03-22 22:04:45 +01001321 if (b43_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
Michael Bueschf54a5202009-11-06 18:32:44 +01001322 kfree(priv_info->bouncebuffer);
1323 priv_info->bouncebuffer = NULL;
Michael Buesch09552cc2008-01-23 21:44:15 +01001324 ring->current_slot = old_top_slot;
1325 ring->used_slots = old_used_slots;
Michael Buesche4d6b792007-09-18 15:39:42 -04001326 err = -EIO;
Michael Bueschf54a5202009-11-06 18:32:44 +01001327 goto out_unmap_hdr;
Michael Buesche4d6b792007-09-18 15:39:42 -04001328 }
1329 }
1330
1331 ops->fill_descriptor(ring, desc, meta->dmaaddr, skb->len, 0, 1, 1);
1332
Johannes Berge039fa42008-05-15 12:55:29 +02001333 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
Michael Buesch280d0e12007-12-26 18:26:17 +01001334 /* Tell the firmware about the cookie of the last
1335 * mcast frame, so it can clear the more-data bit in it. */
1336 b43_shm_write16(ring->dev, B43_SHM_SHARED,
1337 B43_SHM_SH_MCASTCOOKIE, cookie);
1338 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001339 /* Now transfer the whole frame. */
1340 wmb();
1341 ops->poke_tx(ring, next_slot(ring, slot));
1342 return 0;
1343
Michael Buesch280d0e12007-12-26 18:26:17 +01001344out_unmap_hdr:
Michael Buesche4d6b792007-09-18 15:39:42 -04001345 unmap_descbuffer(ring, meta_hdr->dmaaddr,
Michael Buescheb189d8b2008-01-28 14:47:41 -08001346 hdrsize, 1);
Michael Buesche4d6b792007-09-18 15:39:42 -04001347 return err;
1348}
1349
1350static inline int should_inject_overflow(struct b43_dmaring *ring)
1351{
1352#ifdef CONFIG_B43_DEBUG
1353 if (unlikely(b43_debug(ring->dev, B43_DBG_DMAOVERFLOW))) {
1354 /* Check if we should inject another ringbuffer overflow
1355 * to test handling of this situation in the stack. */
1356 unsigned long next_overflow;
1357
1358 next_overflow = ring->last_injected_overflow + HZ;
1359 if (time_after(jiffies, next_overflow)) {
1360 ring->last_injected_overflow = jiffies;
1361 b43dbg(ring->dev->wl,
1362 "Injecting TX ring overflow on "
1363 "DMA controller %d\n", ring->index);
1364 return 1;
1365 }
1366 }
1367#endif /* CONFIG_B43_DEBUG */
1368 return 0;
1369}
1370
Michael Buesche6f5b932008-03-05 21:18:49 +01001371/* Static mapping of mac80211's queues (priorities) to b43 DMA rings. */
John Daiker99da1852009-02-24 02:16:42 -08001372static struct b43_dmaring *select_ring_by_priority(struct b43_wldev *dev,
1373 u8 queue_prio)
Michael Buesche6f5b932008-03-05 21:18:49 +01001374{
1375 struct b43_dmaring *ring;
1376
Michael Buesch403a3a12009-06-08 21:04:57 +02001377 if (dev->qos_enabled) {
Michael Buesche6f5b932008-03-05 21:18:49 +01001378 /* 0 = highest priority */
1379 switch (queue_prio) {
1380 default:
1381 B43_WARN_ON(1);
1382 /* fallthrough */
1383 case 0:
Michael Bueschb27faf82008-03-06 16:32:46 +01001384 ring = dev->dma.tx_ring_AC_VO;
Michael Buesche6f5b932008-03-05 21:18:49 +01001385 break;
1386 case 1:
Michael Bueschb27faf82008-03-06 16:32:46 +01001387 ring = dev->dma.tx_ring_AC_VI;
Michael Buesche6f5b932008-03-05 21:18:49 +01001388 break;
1389 case 2:
Michael Bueschb27faf82008-03-06 16:32:46 +01001390 ring = dev->dma.tx_ring_AC_BE;
Michael Buesche6f5b932008-03-05 21:18:49 +01001391 break;
1392 case 3:
Michael Bueschb27faf82008-03-06 16:32:46 +01001393 ring = dev->dma.tx_ring_AC_BK;
Michael Buesche6f5b932008-03-05 21:18:49 +01001394 break;
1395 }
1396 } else
Michael Bueschb27faf82008-03-06 16:32:46 +01001397 ring = dev->dma.tx_ring_AC_BE;
Michael Buesche6f5b932008-03-05 21:18:49 +01001398
1399 return ring;
1400}
1401
Johannes Berge039fa42008-05-15 12:55:29 +02001402int b43_dma_tx(struct b43_wldev *dev, struct sk_buff *skb)
Michael Buesche4d6b792007-09-18 15:39:42 -04001403{
1404 struct b43_dmaring *ring;
Michael Buesch280d0e12007-12-26 18:26:17 +01001405 struct ieee80211_hdr *hdr;
Michael Buesche4d6b792007-09-18 15:39:42 -04001406 int err = 0;
Johannes Berge039fa42008-05-15 12:55:29 +02001407 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Michael Buesche4d6b792007-09-18 15:39:42 -04001408
Michael Buesch280d0e12007-12-26 18:26:17 +01001409 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02001410 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
Michael Buesch280d0e12007-12-26 18:26:17 +01001411 /* The multicast ring will be sent after the DTIM */
Michael Bueschb27faf82008-03-06 16:32:46 +01001412 ring = dev->dma.tx_ring_mcast;
Michael Buesch280d0e12007-12-26 18:26:17 +01001413 /* Set the more-data bit. Ucode will clear it on
1414 * the last frame for us. */
1415 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1416 } else {
1417 /* Decide by priority where to put this frame. */
Johannes Berge2530082008-05-17 00:57:14 +02001418 ring = select_ring_by_priority(
1419 dev, skb_get_queue_mapping(skb));
Michael Buesch280d0e12007-12-26 18:26:17 +01001420 }
1421
Michael Buesche4d6b792007-09-18 15:39:42 -04001422 B43_WARN_ON(!ring->tx);
Michael Bueschca2d5592009-02-19 20:17:36 +01001423
Larry Finger18c69512009-07-29 10:54:06 -05001424 if (unlikely(ring->stopped)) {
1425 /* We get here only because of a bug in mac80211.
1426 * Because of a race, one packet may be queued after
1427 * the queue is stopped, thus we got called when we shouldn't.
1428 * For now, just refuse the transmit. */
1429 if (b43_debug(dev, B43_DBG_DMAVERBOSE))
1430 b43err(dev->wl, "Packet after queue stopped\n");
1431 err = -ENOSPC;
Michael Buesch637dae32009-09-04 22:55:00 +02001432 goto out;
Larry Finger18c69512009-07-29 10:54:06 -05001433 }
1434
Igor Stoppa7e41fb52018-08-31 18:03:00 +03001435 if (WARN_ON(free_slots(ring) < TX_SLOTS_PER_FRAME)) {
Larry Finger18c69512009-07-29 10:54:06 -05001436 /* If we get here, we have a real error with the queue
1437 * full, but queues not stopped. */
1438 b43err(dev->wl, "DMA queue overflow\n");
Michael Buesche4d6b792007-09-18 15:39:42 -04001439 err = -ENOSPC;
Michael Buesch637dae32009-09-04 22:55:00 +02001440 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -04001441 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001442
Michael Buesche6f5b932008-03-05 21:18:49 +01001443 /* Assign the queue number to the ring (if not already done before)
1444 * so TX status handling can use it. The queue to ring mapping is
1445 * static, so we don't need to store it per frame. */
Johannes Berge2530082008-05-17 00:57:14 +02001446 ring->queue_prio = skb_get_queue_mapping(skb);
Michael Buesche6f5b932008-03-05 21:18:49 +01001447
Michael Bueschf54a5202009-11-06 18:32:44 +01001448 err = dma_tx_fragment(ring, skb);
Michael Buesch09552cc2008-01-23 21:44:15 +01001449 if (unlikely(err == -ENOKEY)) {
1450 /* Drop this packet, as we don't have the encryption key
1451 * anymore and must not transmit it unencrypted. */
Felix Fietkau78f18df2012-12-10 17:40:21 +01001452 ieee80211_free_txskb(dev->wl->hw, skb);
Michael Buesch09552cc2008-01-23 21:44:15 +01001453 err = 0;
Michael Buesch637dae32009-09-04 22:55:00 +02001454 goto out;
Michael Buesch09552cc2008-01-23 21:44:15 +01001455 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001456 if (unlikely(err)) {
1457 b43err(dev->wl, "DMA tx mapping failure\n");
Michael Buesch637dae32009-09-04 22:55:00 +02001458 goto out;
Michael Buesche4d6b792007-09-18 15:39:42 -04001459 }
Michael Bueschbdceeb22009-02-19 23:45:43 +01001460 if ((free_slots(ring) < TX_SLOTS_PER_FRAME) ||
Michael Buesche4d6b792007-09-18 15:39:42 -04001461 should_inject_overflow(ring)) {
1462 /* This TX ring is full. */
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01001463 unsigned int skb_mapping = skb_get_queue_mapping(skb);
1464 ieee80211_stop_queue(dev->wl->hw, skb_mapping);
1465 dev->wl->tx_queue_stopped[skb_mapping] = 1;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001466 ring->stopped = true;
Michael Buesche4d6b792007-09-18 15:39:42 -04001467 if (b43_debug(dev, B43_DBG_DMAVERBOSE)) {
1468 b43dbg(dev->wl, "Stopped TX ring %d\n", ring->index);
1469 }
1470 }
Michael Buesch637dae32009-09-04 22:55:00 +02001471out:
Michael Buesche4d6b792007-09-18 15:39:42 -04001472
1473 return err;
1474}
1475
1476void b43_dma_handle_txstatus(struct b43_wldev *dev,
1477 const struct b43_txstatus *status)
1478{
1479 const struct b43_dma_ops *ops;
1480 struct b43_dmaring *ring;
Michael Buesche4d6b792007-09-18 15:39:42 -04001481 struct b43_dmadesc_meta *meta;
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001482 static const struct b43_txstatus fake; /* filled with 0 */
1483 const struct b43_txstatus *txstat;
Michael Buesch07681e22009-11-19 22:24:29 +01001484 int slot, firstused;
Michael Buesch5100d5a2008-03-29 21:01:16 +01001485 bool frame_succeed;
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001486 int skip;
Taketo Kabe66cffd62018-05-13 18:16:40 +09001487 static u8 err_out1;
Michael Buesche4d6b792007-09-18 15:39:42 -04001488
1489 ring = parse_cookie(dev, status->cookie, &slot);
1490 if (unlikely(!ring))
1491 return;
Michael Buesche4d6b792007-09-18 15:39:42 -04001492 B43_WARN_ON(!ring->tx);
Michael Buesch07681e22009-11-19 22:24:29 +01001493
1494 /* Sanity check: TX packets are processed in-order on one ring.
1495 * Check if the slot deduced from the cookie really is the first
1496 * used slot. */
1497 firstused = ring->current_slot - ring->used_slots + 1;
1498 if (firstused < 0)
1499 firstused = ring->nr_slots + firstused;
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001500
1501 skip = 0;
Michael Buesch07681e22009-11-19 22:24:29 +01001502 if (unlikely(slot != firstused)) {
1503 /* This possibly is a firmware bug and will result in
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001504 * malfunction, memory leaks and/or stall of DMA functionality.
1505 */
1506 if (slot == next_slot(ring, next_slot(ring, firstused))) {
1507 /* If a single header/data pair was missed, skip over
1508 * the first two slots in an attempt to recover.
1509 */
1510 slot = firstused;
1511 skip = 2;
1512 if (!err_out1) {
1513 /* Report the error once. */
1514 b43dbg(dev->wl,
1515 "Skip on DMA ring %d slot %d.\n",
1516 ring->index, slot);
1517 err_out1 = 1;
1518 }
1519 } else {
1520 /* More than a single header/data pair were missed.
Larry Finger2823c872018-08-27 10:34:07 -05001521 * Report this error. If running with open-source
1522 * firmware, then reset the controller to
Taketo Kabe66cffd62018-05-13 18:16:40 +09001523 * revive operation.
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001524 */
Taketo Kabe66cffd62018-05-13 18:16:40 +09001525 b43dbg(dev->wl,
1526 "Out of order TX status report on DMA ring %d. Expected %d, but got %d\n",
1527 ring->index, firstused, slot);
Larry Finger2823c872018-08-27 10:34:07 -05001528 if (dev->fw.opensource)
1529 b43_controller_restart(dev, "Out of order TX");
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001530 return;
1531 }
Michael Buesch07681e22009-11-19 22:24:29 +01001532 }
1533
Michael Buesche4d6b792007-09-18 15:39:42 -04001534 ops = ring->ops;
1535 while (1) {
Michael Buesch07681e22009-11-19 22:24:29 +01001536 B43_WARN_ON(slot < 0 || slot >= ring->nr_slots);
Larry Finger9c1cacd2011-05-22 20:54:25 -05001537 /* get meta - ignore returned value */
1538 ops->idx2desc(ring, slot, &meta);
Michael Buesche4d6b792007-09-18 15:39:42 -04001539
Michael Buesch07681e22009-11-19 22:24:29 +01001540 if (b43_dma_ptr_is_poisoned(meta->skb)) {
1541 b43dbg(dev->wl, "Poisoned TX slot %d (first=%d) "
1542 "on ring %d\n",
1543 slot, firstused, ring->index);
1544 break;
1545 }
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001546
Michael Bueschf54a5202009-11-06 18:32:44 +01001547 if (meta->skb) {
1548 struct b43_private_tx_info *priv_info =
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001549 b43_get_priv_tx_info(IEEE80211_SKB_CB(meta->skb));
Michael Bueschf54a5202009-11-06 18:32:44 +01001550
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001551 unmap_descbuffer(ring, meta->dmaaddr,
1552 meta->skb->len, 1);
Michael Bueschf54a5202009-11-06 18:32:44 +01001553 kfree(priv_info->bouncebuffer);
1554 priv_info->bouncebuffer = NULL;
1555 } else {
Michael Buesche4d6b792007-09-18 15:39:42 -04001556 unmap_descbuffer(ring, meta->dmaaddr,
Michael Buescheb189d8b2008-01-28 14:47:41 -08001557 b43_txhdr_size(dev), 1);
Michael Bueschf54a5202009-11-06 18:32:44 +01001558 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001559
1560 if (meta->is_last_fragment) {
Johannes Berge039fa42008-05-15 12:55:29 +02001561 struct ieee80211_tx_info *info;
1562
Michael Buesch07681e22009-11-19 22:24:29 +01001563 if (unlikely(!meta->skb)) {
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001564 /* This is a scatter-gather fragment of a frame,
1565 * so the skb pointer must not be NULL.
1566 */
Michael Buesch07681e22009-11-19 22:24:29 +01001567 b43dbg(dev->wl, "TX status unexpected NULL skb "
1568 "at slot %d (first=%d) on ring %d\n",
1569 slot, firstused, ring->index);
1570 break;
1571 }
Johannes Berge039fa42008-05-15 12:55:29 +02001572
1573 info = IEEE80211_SKB_CB(meta->skb);
1574
Johannes Berge039fa42008-05-15 12:55:29 +02001575 /*
1576 * Call back to inform the ieee80211 subsystem about
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001577 * the status of the transmission. When skipping over
1578 * a missed TX status report, use a status structure
1579 * filled with zeros to indicate that the frame was not
1580 * sent (frame_count 0) and not acknowledged
Michael Buesche4d6b792007-09-18 15:39:42 -04001581 */
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001582 if (unlikely(skip))
1583 txstat = &fake;
1584 else
1585 txstat = status;
1586
1587 frame_succeed = b43_fill_txstatus_report(dev, info,
1588 txstat);
Michael Buesch5100d5a2008-03-29 21:01:16 +01001589#ifdef CONFIG_B43_DEBUG
1590 if (frame_succeed)
1591 ring->nr_succeed_tx_packets++;
1592 else
1593 ring->nr_failed_tx_packets++;
1594 ring->nr_total_packet_tries += status->frame_count;
1595#endif /* DEBUG */
Michael Bueschce6c4a12009-09-10 20:22:02 +02001596 ieee80211_tx_status(dev->wl->hw, meta->skb);
Johannes Berge039fa42008-05-15 12:55:29 +02001597
Michael Buesch07681e22009-11-19 22:24:29 +01001598 /* skb will be freed by ieee80211_tx_status().
1599 * Poison our pointer. */
1600 meta->skb = B43_DMA_PTR_POISON;
Michael Buesche4d6b792007-09-18 15:39:42 -04001601 } else {
1602 /* No need to call free_descriptor_buffer here, as
1603 * this is only the txhdr, which is not allocated.
1604 */
Michael Buesch07681e22009-11-19 22:24:29 +01001605 if (unlikely(meta->skb)) {
1606 b43dbg(dev->wl, "TX status unexpected non-NULL skb "
1607 "at slot %d (first=%d) on ring %d\n",
1608 slot, firstused, ring->index);
1609 break;
1610 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001611 }
1612
1613 /* Everything unmapped and free'd. So it's not used anymore. */
1614 ring->used_slots--;
1615
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001616 if (meta->is_last_fragment && !skip) {
Michael Buesch07681e22009-11-19 22:24:29 +01001617 /* This is the last scatter-gather
1618 * fragment of the frame. We are done. */
Michael Buesche4d6b792007-09-18 15:39:42 -04001619 break;
Michael Buesch07681e22009-11-19 22:24:29 +01001620 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001621 slot = next_slot(ring, slot);
Iestyn C. Elfickb2514122013-03-20 14:02:31 -05001622 if (skip > 0)
1623 --skip;
Michael Buesche4d6b792007-09-18 15:39:42 -04001624 }
Michael Buesche4d6b792007-09-18 15:39:42 -04001625 if (ring->stopped) {
Michael Bueschbdceeb22009-02-19 23:45:43 +01001626 B43_WARN_ON(free_slots(ring) < TX_SLOTS_PER_FRAME);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001627 ring->stopped = false;
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01001628 }
1629
1630 if (dev->wl->tx_queue_stopped[ring->queue_prio]) {
1631 dev->wl->tx_queue_stopped[ring->queue_prio] = 0;
1632 } else {
1633 /* If the driver queue is running wake the corresponding
1634 * mac80211 queue. */
1635 ieee80211_wake_queue(dev->wl->hw, ring->queue_prio);
Michael Buesche4d6b792007-09-18 15:39:42 -04001636 if (b43_debug(dev, B43_DBG_DMAVERBOSE)) {
1637 b43dbg(dev->wl, "Woke up TX ring %d\n", ring->index);
1638 }
1639 }
francesco.gringoli@ing.unibs.itbad69192011-12-16 18:34:56 +01001640 /* Add work to the queue. */
1641 ieee80211_queue_work(dev->wl->hw, &dev->wl->tx_work);
Michael Buesche4d6b792007-09-18 15:39:42 -04001642}
1643
Michael Buesche4d6b792007-09-18 15:39:42 -04001644static void dma_rx(struct b43_dmaring *ring, int *slot)
1645{
1646 const struct b43_dma_ops *ops = ring->ops;
1647 struct b43_dmadesc_generic *desc;
1648 struct b43_dmadesc_meta *meta;
1649 struct b43_rxhdr_fw4 *rxhdr;
1650 struct sk_buff *skb;
1651 u16 len;
1652 int err;
1653 dma_addr_t dmaaddr;
1654
1655 desc = ops->idx2desc(ring, *slot, &meta);
1656
1657 sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
1658 skb = meta->skb;
1659
Michael Buesche4d6b792007-09-18 15:39:42 -04001660 rxhdr = (struct b43_rxhdr_fw4 *)skb->data;
1661 len = le16_to_cpu(rxhdr->frame_len);
1662 if (len == 0) {
1663 int i = 0;
1664
1665 do {
1666 udelay(2);
1667 barrier();
1668 len = le16_to_cpu(rxhdr->frame_len);
1669 } while (len == 0 && i++ < 5);
1670 if (unlikely(len == 0)) {
Michael Bueschcf686362009-03-28 00:41:25 +01001671 dmaaddr = meta->dmaaddr;
1672 goto drop_recycle_buffer;
Michael Buesche4d6b792007-09-18 15:39:42 -04001673 }
1674 }
Michael Bueschec9a1d82009-03-27 22:51:58 +01001675 if (unlikely(b43_rx_buffer_is_poisoned(ring, skb))) {
1676 /* Something went wrong with the DMA.
1677 * The device did not touch the buffer and did not overwrite the poison. */
1678 b43dbg(ring->dev->wl, "DMA RX: Dropping poisoned buffer.\n");
Michael Bueschcf686362009-03-28 00:41:25 +01001679 dmaaddr = meta->dmaaddr;
1680 goto drop_recycle_buffer;
Michael Bueschec9a1d82009-03-27 22:51:58 +01001681 }
John W. Linvillec85ce652011-03-30 14:02:46 -04001682 if (unlikely(len + ring->frameoffset > ring->rx_buffersize)) {
Michael Buesche4d6b792007-09-18 15:39:42 -04001683 /* The data did not fit into one descriptor buffer
1684 * and is split over multiple buffers.
1685 * This should never happen, as we try to allocate buffers
1686 * big enough. So simply ignore this packet.
1687 */
1688 int cnt = 0;
1689 s32 tmp = len;
1690
1691 while (1) {
1692 desc = ops->idx2desc(ring, *slot, &meta);
1693 /* recycle the descriptor buffer. */
Michael Bueschcf686362009-03-28 00:41:25 +01001694 b43_poison_rx_buffer(ring, meta->skb);
Michael Buesche4d6b792007-09-18 15:39:42 -04001695 sync_descbuffer_for_device(ring, meta->dmaaddr,
1696 ring->rx_buffersize);
1697 *slot = next_slot(ring, *slot);
1698 cnt++;
1699 tmp -= ring->rx_buffersize;
1700 if (tmp <= 0)
1701 break;
1702 }
1703 b43err(ring->dev->wl, "DMA RX buffer too small "
1704 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1705 len, ring->rx_buffersize, cnt);
1706 goto drop;
1707 }
1708
1709 dmaaddr = meta->dmaaddr;
1710 err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
1711 if (unlikely(err)) {
1712 b43dbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer() failed\n");
Michael Bueschcf686362009-03-28 00:41:25 +01001713 goto drop_recycle_buffer;
Michael Buesche4d6b792007-09-18 15:39:42 -04001714 }
1715
1716 unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
1717 skb_put(skb, len + ring->frameoffset);
1718 skb_pull(skb, ring->frameoffset);
1719
1720 b43_rx(ring->dev, skb, rxhdr);
Michael Bueschb27faf82008-03-06 16:32:46 +01001721drop:
Michael Buesche4d6b792007-09-18 15:39:42 -04001722 return;
Michael Bueschcf686362009-03-28 00:41:25 +01001723
1724drop_recycle_buffer:
1725 /* Poison and recycle the RX buffer. */
1726 b43_poison_rx_buffer(ring, skb);
1727 sync_descbuffer_for_device(ring, dmaaddr, ring->rx_buffersize);
Michael Buesche4d6b792007-09-18 15:39:42 -04001728}
1729
Thommy Jakobsson73b82bf2013-04-23 21:45:11 +02001730void b43_dma_handle_rx_overflow(struct b43_dmaring *ring)
1731{
1732 int current_slot, previous_slot;
1733
1734 B43_WARN_ON(ring->tx);
1735
1736 /* Device has filled all buffers, drop all packets and let TCP
1737 * decrease speed.
1738 * Decrement RX index by one will let the device to see all slots
1739 * as free again
1740 */
1741 /*
1742 *TODO: How to increase rx_drop in mac80211?
1743 */
1744 current_slot = ring->ops->get_current_rxslot(ring);
1745 previous_slot = prev_slot(ring, current_slot);
1746 ring->ops->set_current_rxslot(ring, previous_slot);
1747}
1748
Michael Buesche4d6b792007-09-18 15:39:42 -04001749void b43_dma_rx(struct b43_dmaring *ring)
1750{
1751 const struct b43_dma_ops *ops = ring->ops;
1752 int slot, current_slot;
1753 int used_slots = 0;
1754
1755 B43_WARN_ON(ring->tx);
1756 current_slot = ops->get_current_rxslot(ring);
1757 B43_WARN_ON(!(current_slot >= 0 && current_slot < ring->nr_slots));
1758
1759 slot = ring->current_slot;
1760 for (; slot != current_slot; slot = next_slot(ring, slot)) {
1761 dma_rx(ring, &slot);
1762 update_max_used_slots(ring, ++used_slots);
1763 }
Michael Büsch73e6cdc2011-07-04 19:51:11 +02001764 wmb();
Michael Buesche4d6b792007-09-18 15:39:42 -04001765 ops->set_current_rxslot(ring, slot);
1766 ring->current_slot = slot;
1767}
1768
1769static void b43_dma_tx_suspend_ring(struct b43_dmaring *ring)
1770{
Michael Buesche4d6b792007-09-18 15:39:42 -04001771 B43_WARN_ON(!ring->tx);
1772 ring->ops->tx_suspend(ring);
Michael Buesche4d6b792007-09-18 15:39:42 -04001773}
1774
1775static void b43_dma_tx_resume_ring(struct b43_dmaring *ring)
1776{
Michael Buesche4d6b792007-09-18 15:39:42 -04001777 B43_WARN_ON(!ring->tx);
1778 ring->ops->tx_resume(ring);
Michael Buesche4d6b792007-09-18 15:39:42 -04001779}
1780
1781void b43_dma_tx_suspend(struct b43_wldev *dev)
1782{
1783 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
Michael Bueschb27faf82008-03-06 16:32:46 +01001784 b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BK);
1785 b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_BE);
1786 b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VI);
1787 b43_dma_tx_suspend_ring(dev->dma.tx_ring_AC_VO);
1788 b43_dma_tx_suspend_ring(dev->dma.tx_ring_mcast);
Michael Buesche4d6b792007-09-18 15:39:42 -04001789}
1790
1791void b43_dma_tx_resume(struct b43_wldev *dev)
1792{
Michael Bueschb27faf82008-03-06 16:32:46 +01001793 b43_dma_tx_resume_ring(dev->dma.tx_ring_mcast);
1794 b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VO);
1795 b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_VI);
1796 b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BE);
1797 b43_dma_tx_resume_ring(dev->dma.tx_ring_AC_BK);
Michael Buesche4d6b792007-09-18 15:39:42 -04001798 b43_power_saving_ctl_bits(dev, 0);
1799}
Michael Buesch5100d5a2008-03-29 21:01:16 +01001800
Michael Buesch5100d5a2008-03-29 21:01:16 +01001801static void direct_fifo_rx(struct b43_wldev *dev, enum b43_dmatype type,
1802 u16 mmio_base, bool enable)
1803{
1804 u32 ctl;
1805
1806 if (type == B43_DMA_64BIT) {
1807 ctl = b43_read32(dev, mmio_base + B43_DMA64_RXCTL);
1808 ctl &= ~B43_DMA64_RXDIRECTFIFO;
1809 if (enable)
1810 ctl |= B43_DMA64_RXDIRECTFIFO;
1811 b43_write32(dev, mmio_base + B43_DMA64_RXCTL, ctl);
1812 } else {
1813 ctl = b43_read32(dev, mmio_base + B43_DMA32_RXCTL);
1814 ctl &= ~B43_DMA32_RXDIRECTFIFO;
1815 if (enable)
1816 ctl |= B43_DMA32_RXDIRECTFIFO;
1817 b43_write32(dev, mmio_base + B43_DMA32_RXCTL, ctl);
1818 }
1819}
1820
1821/* Enable/Disable Direct FIFO Receive Mode (PIO) on a RX engine.
1822 * This is called from PIO code, so DMA structures are not available. */
1823void b43_dma_direct_fifo_rx(struct b43_wldev *dev,
1824 unsigned int engine_index, bool enable)
1825{
1826 enum b43_dmatype type;
1827 u16 mmio_base;
1828
1829 type = dma_mask_to_engine_type(supported_dma_mask(dev));
1830
1831 mmio_base = b43_dmacontroller_base(type, engine_index);
1832 direct_fifo_rx(dev, type, mmio_base, enable);
1833}