blob: 07d7e928eac780af879c651f67373b290da6a787 [file] [log] [blame]
Larry Finger75388ac2007-09-25 16:46:54 -07001/*
2
3 Broadcom B43legacy 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>
Larry Finger75388ac2007-09-25 16:46:54 -07008
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 "b43legacy.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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Larry Finger75388ac2007-09-25 16:46:54 -070041#include <net/dst.h>
42
43/* 32bit DMA ops. */
44static
Pavel Roskin191d6a82011-07-25 17:40:22 -040045struct b43legacy_dmadesc32 *op32_idx2desc(struct b43legacy_dmaring *ring,
46 int slot,
47 struct b43legacy_dmadesc_meta **meta)
Larry Finger75388ac2007-09-25 16:46:54 -070048{
49 struct b43legacy_dmadesc32 *desc;
50
51 *meta = &(ring->meta[slot]);
52 desc = ring->descbase;
53 desc = &(desc[slot]);
54
Joe Perches2c208892012-06-04 12:44:17 +000055 return desc;
Larry Finger75388ac2007-09-25 16:46:54 -070056}
57
58static void op32_fill_descriptor(struct b43legacy_dmaring *ring,
Pavel Roskin191d6a82011-07-25 17:40:22 -040059 struct b43legacy_dmadesc32 *desc,
Larry Finger75388ac2007-09-25 16:46:54 -070060 dma_addr_t dmaaddr, u16 bufsize,
61 int start, int end, int irq)
62{
63 struct b43legacy_dmadesc32 *descbase = ring->descbase;
64 int slot;
65 u32 ctl;
66 u32 addr;
67 u32 addrext;
68
Pavel Roskin191d6a82011-07-25 17:40:22 -040069 slot = (int)(desc - descbase);
Larry Finger75388ac2007-09-25 16:46:54 -070070 B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
71
72 addr = (u32)(dmaaddr & ~SSB_DMA_TRANSLATION_MASK);
73 addrext = (u32)(dmaaddr & SSB_DMA_TRANSLATION_MASK)
74 >> SSB_DMA_TRANSLATION_SHIFT;
Rafał Miłecki8c95b742011-07-22 00:25:00 +020075 addr |= ring->dev->dma.translation;
Larry Finger75388ac2007-09-25 16:46:54 -070076 ctl = (bufsize - ring->frameoffset)
77 & B43legacy_DMA32_DCTL_BYTECNT;
78 if (slot == ring->nr_slots - 1)
79 ctl |= B43legacy_DMA32_DCTL_DTABLEEND;
80 if (start)
81 ctl |= B43legacy_DMA32_DCTL_FRAMESTART;
82 if (end)
83 ctl |= B43legacy_DMA32_DCTL_FRAMEEND;
84 if (irq)
85 ctl |= B43legacy_DMA32_DCTL_IRQ;
86 ctl |= (addrext << B43legacy_DMA32_DCTL_ADDREXT_SHIFT)
87 & B43legacy_DMA32_DCTL_ADDREXT_MASK;
88
Pavel Roskin191d6a82011-07-25 17:40:22 -040089 desc->control = cpu_to_le32(ctl);
90 desc->address = cpu_to_le32(addr);
Larry Finger75388ac2007-09-25 16:46:54 -070091}
92
93static void op32_poke_tx(struct b43legacy_dmaring *ring, int slot)
94{
95 b43legacy_dma_write(ring, B43legacy_DMA32_TXINDEX,
96 (u32)(slot * sizeof(struct b43legacy_dmadesc32)));
97}
98
99static void op32_tx_suspend(struct b43legacy_dmaring *ring)
100{
101 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
102 b43legacy_dma_read(ring, B43legacy_DMA32_TXCTL)
103 | B43legacy_DMA32_TXSUSPEND);
104}
105
106static void op32_tx_resume(struct b43legacy_dmaring *ring)
107{
108 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL,
109 b43legacy_dma_read(ring, B43legacy_DMA32_TXCTL)
110 & ~B43legacy_DMA32_TXSUSPEND);
111}
112
113static int op32_get_current_rxslot(struct b43legacy_dmaring *ring)
114{
115 u32 val;
116
117 val = b43legacy_dma_read(ring, B43legacy_DMA32_RXSTATUS);
118 val &= B43legacy_DMA32_RXDPTR;
119
120 return (val / sizeof(struct b43legacy_dmadesc32));
121}
122
123static void op32_set_current_rxslot(struct b43legacy_dmaring *ring,
124 int slot)
125{
126 b43legacy_dma_write(ring, B43legacy_DMA32_RXINDEX,
127 (u32)(slot * sizeof(struct b43legacy_dmadesc32)));
128}
129
Larry Finger75388ac2007-09-25 16:46:54 -0700130static inline int free_slots(struct b43legacy_dmaring *ring)
131{
132 return (ring->nr_slots - ring->used_slots);
133}
134
135static inline int next_slot(struct b43legacy_dmaring *ring, int slot)
136{
137 B43legacy_WARN_ON(!(slot >= -1 && slot <= ring->nr_slots - 1));
138 if (slot == ring->nr_slots - 1)
139 return 0;
140 return slot + 1;
141}
142
143static inline int prev_slot(struct b43legacy_dmaring *ring, int slot)
144{
145 B43legacy_WARN_ON(!(slot >= 0 && slot <= ring->nr_slots - 1));
146 if (slot == 0)
147 return ring->nr_slots - 1;
148 return slot - 1;
149}
150
151#ifdef CONFIG_B43LEGACY_DEBUG
152static void update_max_used_slots(struct b43legacy_dmaring *ring,
153 int current_used_slots)
154{
155 if (current_used_slots <= ring->max_used_slots)
156 return;
157 ring->max_used_slots = current_used_slots;
158 if (b43legacy_debug(ring->dev, B43legacy_DBG_DMAVERBOSE))
159 b43legacydbg(ring->dev->wl,
160 "max_used_slots increased to %d on %s ring %d\n",
161 ring->max_used_slots,
162 ring->tx ? "TX" : "RX",
163 ring->index);
164}
165#else
166static inline
167void update_max_used_slots(struct b43legacy_dmaring *ring,
168 int current_used_slots)
169{ }
170#endif /* DEBUG */
171
172/* Request a slot for usage. */
173static inline
174int request_slot(struct b43legacy_dmaring *ring)
175{
176 int slot;
177
178 B43legacy_WARN_ON(!ring->tx);
179 B43legacy_WARN_ON(ring->stopped);
180 B43legacy_WARN_ON(free_slots(ring) == 0);
181
182 slot = next_slot(ring, ring->current_slot);
183 ring->current_slot = slot;
184 ring->used_slots++;
185
186 update_max_used_slots(ring, ring->used_slots);
187
188 return slot;
189}
190
191/* Mac80211-queue to b43legacy-ring mapping */
192static struct b43legacy_dmaring *priority_to_txring(
193 struct b43legacy_wldev *dev,
194 int queue_priority)
195{
196 struct b43legacy_dmaring *ring;
197
198/*FIXME: For now we always run on TX-ring-1 */
199return dev->dma.tx_ring1;
200
201 /* 0 = highest priority */
202 switch (queue_priority) {
203 default:
204 B43legacy_WARN_ON(1);
205 /* fallthrough */
206 case 0:
207 ring = dev->dma.tx_ring3;
208 break;
209 case 1:
210 ring = dev->dma.tx_ring2;
211 break;
212 case 2:
213 ring = dev->dma.tx_ring1;
214 break;
215 case 3:
216 ring = dev->dma.tx_ring0;
217 break;
218 case 4:
219 ring = dev->dma.tx_ring4;
220 break;
221 case 5:
222 ring = dev->dma.tx_ring5;
223 break;
224 }
225
226 return ring;
227}
228
229/* Bcm4301-ring to mac80211-queue mapping */
230static inline int txring_to_priority(struct b43legacy_dmaring *ring)
231{
232 static const u8 idx_to_prio[] =
233 { 3, 2, 1, 0, 4, 5, };
234
235/*FIXME: have only one queue, for now */
236return 0;
237
238 return idx_to_prio[ring->index];
239}
240
241
Stefano Brivio8e118f02008-02-08 06:31:53 +0100242static u16 b43legacy_dmacontroller_base(enum b43legacy_dmatype type,
243 int controller_idx)
Larry Finger75388ac2007-09-25 16:46:54 -0700244{
Larry Finger75388ac2007-09-25 16:46:54 -0700245 static const u16 map32[] = {
246 B43legacy_MMIO_DMA32_BASE0,
247 B43legacy_MMIO_DMA32_BASE1,
248 B43legacy_MMIO_DMA32_BASE2,
249 B43legacy_MMIO_DMA32_BASE3,
250 B43legacy_MMIO_DMA32_BASE4,
251 B43legacy_MMIO_DMA32_BASE5,
252 };
253
Larry Finger75388ac2007-09-25 16:46:54 -0700254 B43legacy_WARN_ON(!(controller_idx >= 0 &&
255 controller_idx < ARRAY_SIZE(map32)));
256 return map32[controller_idx];
257}
258
259static inline
260dma_addr_t map_descbuffer(struct b43legacy_dmaring *ring,
261 unsigned char *buf,
262 size_t len,
263 int tx)
264{
265 dma_addr_t dmaaddr;
266
267 if (tx)
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700268 dmaaddr = dma_map_single(ring->dev->dev->dma_dev,
Michael Bueschf2257632008-06-20 11:50:29 +0200269 buf, len,
270 DMA_TO_DEVICE);
Larry Finger75388ac2007-09-25 16:46:54 -0700271 else
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700272 dmaaddr = dma_map_single(ring->dev->dev->dma_dev,
Michael Bueschf2257632008-06-20 11:50:29 +0200273 buf, len,
274 DMA_FROM_DEVICE);
Larry Finger75388ac2007-09-25 16:46:54 -0700275
276 return dmaaddr;
277}
278
279static inline
280void unmap_descbuffer(struct b43legacy_dmaring *ring,
281 dma_addr_t addr,
282 size_t len,
283 int tx)
284{
285 if (tx)
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700286 dma_unmap_single(ring->dev->dev->dma_dev,
Michael Bueschf2257632008-06-20 11:50:29 +0200287 addr, len,
288 DMA_TO_DEVICE);
Larry Finger75388ac2007-09-25 16:46:54 -0700289 else
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700290 dma_unmap_single(ring->dev->dev->dma_dev,
Michael Bueschf2257632008-06-20 11:50:29 +0200291 addr, len,
292 DMA_FROM_DEVICE);
Larry Finger75388ac2007-09-25 16:46:54 -0700293}
294
295static inline
296void sync_descbuffer_for_cpu(struct b43legacy_dmaring *ring,
297 dma_addr_t addr,
298 size_t len)
299{
300 B43legacy_WARN_ON(ring->tx);
301
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700302 dma_sync_single_for_cpu(ring->dev->dev->dma_dev,
303 addr, len, DMA_FROM_DEVICE);
Larry Finger75388ac2007-09-25 16:46:54 -0700304}
305
306static inline
307void sync_descbuffer_for_device(struct b43legacy_dmaring *ring,
308 dma_addr_t addr,
309 size_t len)
310{
311 B43legacy_WARN_ON(ring->tx);
312
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700313 dma_sync_single_for_device(ring->dev->dev->dma_dev,
314 addr, len, DMA_FROM_DEVICE);
Larry Finger75388ac2007-09-25 16:46:54 -0700315}
316
317static inline
318void free_descriptor_buffer(struct b43legacy_dmaring *ring,
319 struct b43legacy_dmadesc_meta *meta,
320 int irq_context)
321{
322 if (meta->skb) {
323 if (irq_context)
324 dev_kfree_skb_irq(meta->skb);
325 else
326 dev_kfree_skb(meta->skb);
327 meta->skb = NULL;
328 }
329}
330
331static int alloc_ringmemory(struct b43legacy_dmaring *ring)
332{
Michael Bueschf2257632008-06-20 11:50:29 +0200333 /* GFP flags must match the flags in free_ringmemory()! */
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700334 ring->descbase = dma_alloc_coherent(ring->dev->dev->dma_dev,
335 B43legacy_DMA_RINGMEMSIZE,
336 &(ring->dmabase),
337 GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +0000338 if (!ring->descbase)
Larry Finger75388ac2007-09-25 16:46:54 -0700339 return -ENOMEM;
Larry Finger75388ac2007-09-25 16:46:54 -0700340 memset(ring->descbase, 0, B43legacy_DMA_RINGMEMSIZE);
341
342 return 0;
343}
344
345static void free_ringmemory(struct b43legacy_dmaring *ring)
346{
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700347 dma_free_coherent(ring->dev->dev->dma_dev, B43legacy_DMA_RINGMEMSIZE,
348 ring->descbase, ring->dmabase);
Larry Finger75388ac2007-09-25 16:46:54 -0700349}
350
351/* Reset the RX DMA channel */
Stefano Brivio8e118f02008-02-08 06:31:53 +0100352static int b43legacy_dmacontroller_rx_reset(struct b43legacy_wldev *dev,
353 u16 mmio_base,
354 enum b43legacy_dmatype type)
Larry Finger75388ac2007-09-25 16:46:54 -0700355{
356 int i;
357 u32 value;
358 u16 offset;
359
360 might_sleep();
361
Pavel Roskin191d6a82011-07-25 17:40:22 -0400362 offset = B43legacy_DMA32_RXCTL;
Larry Finger75388ac2007-09-25 16:46:54 -0700363 b43legacy_write32(dev, mmio_base + offset, 0);
364 for (i = 0; i < 10; i++) {
Pavel Roskin191d6a82011-07-25 17:40:22 -0400365 offset = B43legacy_DMA32_RXSTATUS;
Larry Finger75388ac2007-09-25 16:46:54 -0700366 value = b43legacy_read32(dev, mmio_base + offset);
Pavel Roskin191d6a82011-07-25 17:40:22 -0400367 value &= B43legacy_DMA32_RXSTATE;
368 if (value == B43legacy_DMA32_RXSTAT_DISABLED) {
369 i = -1;
370 break;
Larry Finger75388ac2007-09-25 16:46:54 -0700371 }
372 msleep(1);
373 }
374 if (i != -1) {
375 b43legacyerr(dev->wl, "DMA RX reset timed out\n");
376 return -ENODEV;
377 }
378
379 return 0;
380}
381
382/* Reset the RX DMA channel */
Stefano Brivio8e118f02008-02-08 06:31:53 +0100383static int b43legacy_dmacontroller_tx_reset(struct b43legacy_wldev *dev,
384 u16 mmio_base,
385 enum b43legacy_dmatype type)
Larry Finger75388ac2007-09-25 16:46:54 -0700386{
387 int i;
388 u32 value;
389 u16 offset;
390
391 might_sleep();
392
393 for (i = 0; i < 10; i++) {
Pavel Roskin191d6a82011-07-25 17:40:22 -0400394 offset = B43legacy_DMA32_TXSTATUS;
Larry Finger75388ac2007-09-25 16:46:54 -0700395 value = b43legacy_read32(dev, mmio_base + offset);
Pavel Roskin191d6a82011-07-25 17:40:22 -0400396 value &= B43legacy_DMA32_TXSTATE;
397 if (value == B43legacy_DMA32_TXSTAT_DISABLED ||
398 value == B43legacy_DMA32_TXSTAT_IDLEWAIT ||
399 value == B43legacy_DMA32_TXSTAT_STOPPED)
400 break;
Larry Finger75388ac2007-09-25 16:46:54 -0700401 msleep(1);
402 }
Pavel Roskin191d6a82011-07-25 17:40:22 -0400403 offset = B43legacy_DMA32_TXCTL;
Larry Finger75388ac2007-09-25 16:46:54 -0700404 b43legacy_write32(dev, mmio_base + offset, 0);
405 for (i = 0; i < 10; i++) {
Pavel Roskin191d6a82011-07-25 17:40:22 -0400406 offset = B43legacy_DMA32_TXSTATUS;
Larry Finger75388ac2007-09-25 16:46:54 -0700407 value = b43legacy_read32(dev, mmio_base + offset);
Pavel Roskin191d6a82011-07-25 17:40:22 -0400408 value &= B43legacy_DMA32_TXSTATE;
409 if (value == B43legacy_DMA32_TXSTAT_DISABLED) {
410 i = -1;
411 break;
Larry Finger75388ac2007-09-25 16:46:54 -0700412 }
413 msleep(1);
414 }
415 if (i != -1) {
416 b43legacyerr(dev->wl, "DMA TX reset timed out\n");
417 return -ENODEV;
418 }
419 /* ensure the reset is completed. */
420 msleep(1);
421
422 return 0;
423}
424
Stefano Brivio8e118f02008-02-08 06:31:53 +0100425/* Check if a DMA mapping address is invalid. */
426static bool b43legacy_dma_mapping_error(struct b43legacy_dmaring *ring,
Stefano Briviodc4ae1f2008-04-14 00:59:49 +0200427 dma_addr_t addr,
428 size_t buffersize,
429 bool dma_to_device)
Stefano Brivio8e118f02008-02-08 06:31:53 +0100430{
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700431 if (unlikely(dma_mapping_error(ring->dev->dev->dma_dev, addr)))
Stefano Brivio8e118f02008-02-08 06:31:53 +0100432 return 1;
433
434 switch (ring->type) {
435 case B43legacy_DMA_30BIT:
436 if ((u64)addr + buffersize > (1ULL << 30))
Stefano Briviodc4ae1f2008-04-14 00:59:49 +0200437 goto address_error;
Stefano Brivio8e118f02008-02-08 06:31:53 +0100438 break;
439 case B43legacy_DMA_32BIT:
440 if ((u64)addr + buffersize > (1ULL << 32))
Stefano Briviodc4ae1f2008-04-14 00:59:49 +0200441 goto address_error;
Stefano Brivio8e118f02008-02-08 06:31:53 +0100442 break;
Stefano Brivio8e118f02008-02-08 06:31:53 +0100443 }
444
445 /* The address is OK. */
446 return 0;
Stefano Briviodc4ae1f2008-04-14 00:59:49 +0200447
448address_error:
449 /* We can't support this address. Unmap it again. */
450 unmap_descbuffer(ring, addr, buffersize, dma_to_device);
451
452 return 1;
Stefano Brivio8e118f02008-02-08 06:31:53 +0100453}
454
Larry Finger75388ac2007-09-25 16:46:54 -0700455static int setup_rx_descbuffer(struct b43legacy_dmaring *ring,
Pavel Roskin191d6a82011-07-25 17:40:22 -0400456 struct b43legacy_dmadesc32 *desc,
Larry Finger75388ac2007-09-25 16:46:54 -0700457 struct b43legacy_dmadesc_meta *meta,
458 gfp_t gfp_flags)
459{
460 struct b43legacy_rxhdr_fw3 *rxhdr;
461 struct b43legacy_hwtxstatus *txstat;
462 dma_addr_t dmaaddr;
463 struct sk_buff *skb;
464
465 B43legacy_WARN_ON(ring->tx);
466
467 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
468 if (unlikely(!skb))
469 return -ENOMEM;
470 dmaaddr = map_descbuffer(ring, skb->data,
471 ring->rx_buffersize, 0);
Stefano Briviodc4ae1f2008-04-14 00:59:49 +0200472 if (b43legacy_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
Larry Finger75388ac2007-09-25 16:46:54 -0700473 /* ugh. try to realloc in zone_dma */
474 gfp_flags |= GFP_DMA;
475
476 dev_kfree_skb_any(skb);
477
478 skb = __dev_alloc_skb(ring->rx_buffersize, gfp_flags);
479 if (unlikely(!skb))
480 return -ENOMEM;
481 dmaaddr = map_descbuffer(ring, skb->data,
482 ring->rx_buffersize, 0);
483 }
484
Stefano Briviodc4ae1f2008-04-14 00:59:49 +0200485 if (b43legacy_dma_mapping_error(ring, dmaaddr, ring->rx_buffersize, 0)) {
Larry Finger75388ac2007-09-25 16:46:54 -0700486 dev_kfree_skb_any(skb);
487 return -EIO;
488 }
489
490 meta->skb = skb;
491 meta->dmaaddr = dmaaddr;
Pavel Roskin191d6a82011-07-25 17:40:22 -0400492 op32_fill_descriptor(ring, desc, dmaaddr, ring->rx_buffersize, 0, 0, 0);
Larry Finger75388ac2007-09-25 16:46:54 -0700493
494 rxhdr = (struct b43legacy_rxhdr_fw3 *)(skb->data);
495 rxhdr->frame_len = 0;
496 txstat = (struct b43legacy_hwtxstatus *)(skb->data);
497 txstat->cookie = 0;
498
499 return 0;
500}
501
502/* Allocate the initial descbuffers.
503 * This is used for an RX ring only.
504 */
505static int alloc_initial_descbuffers(struct b43legacy_dmaring *ring)
506{
507 int i;
508 int err = -ENOMEM;
Pavel Roskin191d6a82011-07-25 17:40:22 -0400509 struct b43legacy_dmadesc32 *desc;
Larry Finger75388ac2007-09-25 16:46:54 -0700510 struct b43legacy_dmadesc_meta *meta;
511
512 for (i = 0; i < ring->nr_slots; i++) {
Pavel Roskin191d6a82011-07-25 17:40:22 -0400513 desc = op32_idx2desc(ring, i, &meta);
Larry Finger75388ac2007-09-25 16:46:54 -0700514
515 err = setup_rx_descbuffer(ring, desc, meta, GFP_KERNEL);
516 if (err) {
517 b43legacyerr(ring->dev->wl,
518 "Failed to allocate initial descbuffers\n");
519 goto err_unwind;
520 }
521 }
522 mb(); /* all descbuffer setup before next line */
523 ring->used_slots = ring->nr_slots;
524 err = 0;
525out:
526 return err;
527
528err_unwind:
529 for (i--; i >= 0; i--) {
Pavel Roskin191d6a82011-07-25 17:40:22 -0400530 desc = op32_idx2desc(ring, i, &meta);
Larry Finger75388ac2007-09-25 16:46:54 -0700531
532 unmap_descbuffer(ring, meta->dmaaddr, ring->rx_buffersize, 0);
533 dev_kfree_skb(meta->skb);
534 }
535 goto out;
536}
537
538/* Do initial setup of the DMA controller.
539 * Reset the controller, write the ring busaddress
540 * and switch the "enable" bit on.
541 */
542static int dmacontroller_setup(struct b43legacy_dmaring *ring)
543{
544 int err = 0;
545 u32 value;
546 u32 addrext;
Rafał Miłecki8c95b742011-07-22 00:25:00 +0200547 u32 trans = ring->dev->dma.translation;
Pavel Roskin191d6a82011-07-25 17:40:22 -0400548 u32 ringbase = (u32)(ring->dmabase);
Larry Finger75388ac2007-09-25 16:46:54 -0700549
550 if (ring->tx) {
Pavel Roskin191d6a82011-07-25 17:40:22 -0400551 addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
552 >> SSB_DMA_TRANSLATION_SHIFT;
553 value = B43legacy_DMA32_TXENABLE;
554 value |= (addrext << B43legacy_DMA32_TXADDREXT_SHIFT)
555 & B43legacy_DMA32_TXADDREXT_MASK;
556 b43legacy_dma_write(ring, B43legacy_DMA32_TXCTL, value);
557 b43legacy_dma_write(ring, B43legacy_DMA32_TXRING,
558 (ringbase & ~SSB_DMA_TRANSLATION_MASK)
559 | trans);
Larry Finger75388ac2007-09-25 16:46:54 -0700560 } else {
561 err = alloc_initial_descbuffers(ring);
562 if (err)
563 goto out;
Larry Finger75388ac2007-09-25 16:46:54 -0700564
Pavel Roskin191d6a82011-07-25 17:40:22 -0400565 addrext = (ringbase & SSB_DMA_TRANSLATION_MASK)
566 >> SSB_DMA_TRANSLATION_SHIFT;
567 value = (ring->frameoffset <<
568 B43legacy_DMA32_RXFROFF_SHIFT);
569 value |= B43legacy_DMA32_RXENABLE;
570 value |= (addrext << B43legacy_DMA32_RXADDREXT_SHIFT)
571 & B43legacy_DMA32_RXADDREXT_MASK;
572 b43legacy_dma_write(ring, B43legacy_DMA32_RXCTL, value);
573 b43legacy_dma_write(ring, B43legacy_DMA32_RXRING,
574 (ringbase & ~SSB_DMA_TRANSLATION_MASK)
575 | trans);
576 b43legacy_dma_write(ring, B43legacy_DMA32_RXINDEX, 200);
Larry Finger75388ac2007-09-25 16:46:54 -0700577 }
578
579out:
580 return err;
581}
582
583/* Shutdown the DMA controller. */
584static void dmacontroller_cleanup(struct b43legacy_dmaring *ring)
585{
586 if (ring->tx) {
587 b43legacy_dmacontroller_tx_reset(ring->dev, ring->mmio_base,
Stefano Brivio8e118f02008-02-08 06:31:53 +0100588 ring->type);
Pavel Roskin191d6a82011-07-25 17:40:22 -0400589 b43legacy_dma_write(ring, B43legacy_DMA32_TXRING, 0);
Larry Finger75388ac2007-09-25 16:46:54 -0700590 } else {
591 b43legacy_dmacontroller_rx_reset(ring->dev, ring->mmio_base,
Stefano Brivio8e118f02008-02-08 06:31:53 +0100592 ring->type);
Pavel Roskin191d6a82011-07-25 17:40:22 -0400593 b43legacy_dma_write(ring, B43legacy_DMA32_RXRING, 0);
Larry Finger75388ac2007-09-25 16:46:54 -0700594 }
595}
596
597static void free_all_descbuffers(struct b43legacy_dmaring *ring)
598{
Larry Finger75388ac2007-09-25 16:46:54 -0700599 struct b43legacy_dmadesc_meta *meta;
600 int i;
601
602 if (!ring->used_slots)
603 return;
604 for (i = 0; i < ring->nr_slots; i++) {
Pavel Roskin191d6a82011-07-25 17:40:22 -0400605 op32_idx2desc(ring, i, &meta);
Larry Finger75388ac2007-09-25 16:46:54 -0700606
607 if (!meta->skb) {
608 B43legacy_WARN_ON(!ring->tx);
609 continue;
610 }
611 if (ring->tx)
612 unmap_descbuffer(ring, meta->dmaaddr,
613 meta->skb->len, 1);
614 else
615 unmap_descbuffer(ring, meta->dmaaddr,
616 ring->rx_buffersize, 0);
617 free_descriptor_buffer(ring, meta, 0);
618 }
619}
620
621static u64 supported_dma_mask(struct b43legacy_wldev *dev)
622{
623 u32 tmp;
624 u16 mmio_base;
625
Larry Finger75388ac2007-09-25 16:46:54 -0700626 mmio_base = b43legacy_dmacontroller_base(0, 0);
627 b43legacy_write32(dev,
628 mmio_base + B43legacy_DMA32_TXCTL,
629 B43legacy_DMA32_TXADDREXT_MASK);
630 tmp = b43legacy_read32(dev, mmio_base +
631 B43legacy_DMA32_TXCTL);
632 if (tmp & B43legacy_DMA32_TXADDREXT_MASK)
Yang Hongyang284901a2009-04-06 19:01:15 -0700633 return DMA_BIT_MASK(32);
Larry Finger75388ac2007-09-25 16:46:54 -0700634
Yang Hongyang28b76792009-04-06 19:01:17 -0700635 return DMA_BIT_MASK(30);
Larry Finger75388ac2007-09-25 16:46:54 -0700636}
637
Larry Finger70197ed2008-07-04 08:39:01 -0500638static enum b43legacy_dmatype dma_mask_to_engine_type(u64 dmamask)
639{
Yang Hongyang28b76792009-04-06 19:01:17 -0700640 if (dmamask == DMA_BIT_MASK(30))
Larry Finger70197ed2008-07-04 08:39:01 -0500641 return B43legacy_DMA_30BIT;
Yang Hongyang284901a2009-04-06 19:01:15 -0700642 if (dmamask == DMA_BIT_MASK(32))
Larry Finger70197ed2008-07-04 08:39:01 -0500643 return B43legacy_DMA_32BIT;
Larry Finger70197ed2008-07-04 08:39:01 -0500644 B43legacy_WARN_ON(1);
645 return B43legacy_DMA_30BIT;
646}
647
Larry Finger75388ac2007-09-25 16:46:54 -0700648/* Main initialization function. */
649static
Stefano Brivio8e118f02008-02-08 06:31:53 +0100650struct b43legacy_dmaring *b43legacy_setup_dmaring(struct b43legacy_wldev *dev,
651 int controller_index,
652 int for_tx,
653 enum b43legacy_dmatype type)
Larry Finger75388ac2007-09-25 16:46:54 -0700654{
655 struct b43legacy_dmaring *ring;
656 int err;
657 int nr_slots;
658 dma_addr_t dma_test;
659
660 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
661 if (!ring)
662 goto out;
Stefano Brivio8e118f02008-02-08 06:31:53 +0100663 ring->type = type;
Michael Buesch2f9ec472008-06-20 11:40:46 +0200664 ring->dev = dev;
Larry Finger75388ac2007-09-25 16:46:54 -0700665
666 nr_slots = B43legacy_RXRING_SLOTS;
667 if (for_tx)
668 nr_slots = B43legacy_TXRING_SLOTS;
669
670 ring->meta = kcalloc(nr_slots, sizeof(struct b43legacy_dmadesc_meta),
671 GFP_KERNEL);
672 if (!ring->meta)
673 goto err_kfree_ring;
674 if (for_tx) {
675 ring->txhdr_cache = kcalloc(nr_slots,
676 sizeof(struct b43legacy_txhdr_fw3),
677 GFP_KERNEL);
678 if (!ring->txhdr_cache)
679 goto err_kfree_meta;
680
681 /* test for ability to dma to txhdr_cache */
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700682 dma_test = dma_map_single(dev->dev->dma_dev, ring->txhdr_cache,
Michael Bueschf2257632008-06-20 11:50:29 +0200683 sizeof(struct b43legacy_txhdr_fw3),
684 DMA_TO_DEVICE);
Larry Finger75388ac2007-09-25 16:46:54 -0700685
Stefano Brivio8e118f02008-02-08 06:31:53 +0100686 if (b43legacy_dma_mapping_error(ring, dma_test,
Stefano Briviodc4ae1f2008-04-14 00:59:49 +0200687 sizeof(struct b43legacy_txhdr_fw3), 1)) {
Larry Finger75388ac2007-09-25 16:46:54 -0700688 /* ugh realloc */
689 kfree(ring->txhdr_cache);
690 ring->txhdr_cache = kcalloc(nr_slots,
691 sizeof(struct b43legacy_txhdr_fw3),
692 GFP_KERNEL | GFP_DMA);
693 if (!ring->txhdr_cache)
694 goto err_kfree_meta;
695
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700696 dma_test = dma_map_single(dev->dev->dma_dev,
Larry Finger75388ac2007-09-25 16:46:54 -0700697 ring->txhdr_cache,
698 sizeof(struct b43legacy_txhdr_fw3),
699 DMA_TO_DEVICE);
700
Stefano Brivio8e118f02008-02-08 06:31:53 +0100701 if (b43legacy_dma_mapping_error(ring, dma_test,
Stefano Briviodc4ae1f2008-04-14 00:59:49 +0200702 sizeof(struct b43legacy_txhdr_fw3), 1))
Larry Finger75388ac2007-09-25 16:46:54 -0700703 goto err_kfree_txhdr_cache;
704 }
705
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700706 dma_unmap_single(dev->dev->dma_dev, dma_test,
707 sizeof(struct b43legacy_txhdr_fw3),
708 DMA_TO_DEVICE);
Larry Finger75388ac2007-09-25 16:46:54 -0700709 }
710
Larry Finger75388ac2007-09-25 16:46:54 -0700711 ring->nr_slots = nr_slots;
Stefano Brivio8e118f02008-02-08 06:31:53 +0100712 ring->mmio_base = b43legacy_dmacontroller_base(type, controller_index);
Larry Finger75388ac2007-09-25 16:46:54 -0700713 ring->index = controller_index;
Larry Finger75388ac2007-09-25 16:46:54 -0700714 if (for_tx) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000715 ring->tx = true;
Larry Finger75388ac2007-09-25 16:46:54 -0700716 ring->current_slot = -1;
717 } else {
718 if (ring->index == 0) {
719 ring->rx_buffersize = B43legacy_DMA0_RX_BUFFERSIZE;
720 ring->frameoffset = B43legacy_DMA0_RX_FRAMEOFFSET;
721 } else if (ring->index == 3) {
722 ring->rx_buffersize = B43legacy_DMA3_RX_BUFFERSIZE;
723 ring->frameoffset = B43legacy_DMA3_RX_FRAMEOFFSET;
724 } else
725 B43legacy_WARN_ON(1);
726 }
Larry Finger75388ac2007-09-25 16:46:54 -0700727#ifdef CONFIG_B43LEGACY_DEBUG
728 ring->last_injected_overflow = jiffies;
729#endif
730
731 err = alloc_ringmemory(ring);
732 if (err)
733 goto err_kfree_txhdr_cache;
734 err = dmacontroller_setup(ring);
735 if (err)
736 goto err_free_ringmemory;
737
738out:
739 return ring;
740
741err_free_ringmemory:
742 free_ringmemory(ring);
743err_kfree_txhdr_cache:
744 kfree(ring->txhdr_cache);
745err_kfree_meta:
746 kfree(ring->meta);
747err_kfree_ring:
748 kfree(ring);
749 ring = NULL;
750 goto out;
751}
752
753/* Main cleanup function. */
754static void b43legacy_destroy_dmaring(struct b43legacy_dmaring *ring)
755{
756 if (!ring)
757 return;
758
Stefano Brivio8e118f02008-02-08 06:31:53 +0100759 b43legacydbg(ring->dev->wl, "DMA-%u 0x%04X (%s) max used slots:"
760 " %d/%d\n", (unsigned int)(ring->type), ring->mmio_base,
761 (ring->tx) ? "TX" : "RX", ring->max_used_slots,
762 ring->nr_slots);
Larry Finger75388ac2007-09-25 16:46:54 -0700763 /* Device IRQs are disabled prior entering this function,
764 * so no need to take care of concurrency with rx handler stuff.
765 */
766 dmacontroller_cleanup(ring);
767 free_all_descbuffers(ring);
768 free_ringmemory(ring);
769
770 kfree(ring->txhdr_cache);
771 kfree(ring->meta);
772 kfree(ring);
773}
774
775void b43legacy_dma_free(struct b43legacy_wldev *dev)
776{
777 struct b43legacy_dma *dma;
778
779 if (b43legacy_using_pio(dev))
780 return;
781 dma = &dev->dma;
782
783 b43legacy_destroy_dmaring(dma->rx_ring3);
784 dma->rx_ring3 = NULL;
785 b43legacy_destroy_dmaring(dma->rx_ring0);
786 dma->rx_ring0 = NULL;
787
788 b43legacy_destroy_dmaring(dma->tx_ring5);
789 dma->tx_ring5 = NULL;
790 b43legacy_destroy_dmaring(dma->tx_ring4);
791 dma->tx_ring4 = NULL;
792 b43legacy_destroy_dmaring(dma->tx_ring3);
793 dma->tx_ring3 = NULL;
794 b43legacy_destroy_dmaring(dma->tx_ring2);
795 dma->tx_ring2 = NULL;
796 b43legacy_destroy_dmaring(dma->tx_ring1);
797 dma->tx_ring1 = NULL;
798 b43legacy_destroy_dmaring(dma->tx_ring0);
799 dma->tx_ring0 = NULL;
800}
801
Larry Finger70197ed2008-07-04 08:39:01 -0500802static int b43legacy_dma_set_mask(struct b43legacy_wldev *dev, u64 mask)
803{
804 u64 orig_mask = mask;
Rusty Russell3db1cd52011-12-19 13:56:45 +0000805 bool fallback = false;
Larry Finger70197ed2008-07-04 08:39:01 -0500806 int err;
807
808 /* Try to set the DMA mask. If it fails, try falling back to a
809 * lower mask, as we can always also support a lower one. */
810 while (1) {
FUJITA Tomonori4e803132010-06-03 19:37:33 -0700811 err = dma_set_mask(dev->dev->dma_dev, mask);
812 if (!err) {
813 err = dma_set_coherent_mask(dev->dev->dma_dev, mask);
814 if (!err)
815 break;
816 }
Yang Hongyang6a355282009-04-06 19:01:13 -0700817 if (mask == DMA_BIT_MASK(64)) {
Yang Hongyang284901a2009-04-06 19:01:15 -0700818 mask = DMA_BIT_MASK(32);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000819 fallback = true;
Larry Finger70197ed2008-07-04 08:39:01 -0500820 continue;
821 }
Yang Hongyang284901a2009-04-06 19:01:15 -0700822 if (mask == DMA_BIT_MASK(32)) {
Yang Hongyang28b76792009-04-06 19:01:17 -0700823 mask = DMA_BIT_MASK(30);
Rusty Russell3db1cd52011-12-19 13:56:45 +0000824 fallback = true;
Larry Finger70197ed2008-07-04 08:39:01 -0500825 continue;
826 }
827 b43legacyerr(dev->wl, "The machine/kernel does not support "
828 "the required %u-bit DMA mask\n",
829 (unsigned int)dma_mask_to_engine_type(orig_mask));
830 return -EOPNOTSUPP;
831 }
832 if (fallback) {
833 b43legacyinfo(dev->wl, "DMA mask fallback from %u-bit to %u-"
834 "bit\n",
835 (unsigned int)dma_mask_to_engine_type(orig_mask),
836 (unsigned int)dma_mask_to_engine_type(mask));
837 }
838
839 return 0;
840}
841
Larry Finger75388ac2007-09-25 16:46:54 -0700842int b43legacy_dma_init(struct b43legacy_wldev *dev)
843{
844 struct b43legacy_dma *dma = &dev->dma;
845 struct b43legacy_dmaring *ring;
846 int err;
847 u64 dmamask;
Stefano Brivio8e118f02008-02-08 06:31:53 +0100848 enum b43legacy_dmatype type;
Larry Finger75388ac2007-09-25 16:46:54 -0700849
850 dmamask = supported_dma_mask(dev);
Larry Finger70197ed2008-07-04 08:39:01 -0500851 type = dma_mask_to_engine_type(dmamask);
852 err = b43legacy_dma_set_mask(dev, dmamask);
Larry Finger75388ac2007-09-25 16:46:54 -0700853 if (err) {
Stefano Brivio354807e2007-11-19 20:21:31 +0100854#ifdef CONFIG_B43LEGACY_PIO
Larry Finger75388ac2007-09-25 16:46:54 -0700855 b43legacywarn(dev->wl, "DMA for this device not supported. "
856 "Falling back to PIO\n");
Rusty Russell3db1cd52011-12-19 13:56:45 +0000857 dev->__using_pio = true;
Larry Finger75388ac2007-09-25 16:46:54 -0700858 return -EAGAIN;
859#else
860 b43legacyerr(dev->wl, "DMA for this device not supported and "
861 "no PIO support compiled in\n");
862 return -EOPNOTSUPP;
863#endif
864 }
Rafał Miłecki8c95b742011-07-22 00:25:00 +0200865 dma->translation = ssb_dma_translation(dev->dev);
Larry Finger75388ac2007-09-25 16:46:54 -0700866
867 err = -ENOMEM;
868 /* setup TX DMA channels. */
Stefano Brivio8e118f02008-02-08 06:31:53 +0100869 ring = b43legacy_setup_dmaring(dev, 0, 1, type);
Larry Finger75388ac2007-09-25 16:46:54 -0700870 if (!ring)
871 goto out;
872 dma->tx_ring0 = ring;
873
Stefano Brivio8e118f02008-02-08 06:31:53 +0100874 ring = b43legacy_setup_dmaring(dev, 1, 1, type);
Larry Finger75388ac2007-09-25 16:46:54 -0700875 if (!ring)
876 goto err_destroy_tx0;
877 dma->tx_ring1 = ring;
878
Stefano Brivio8e118f02008-02-08 06:31:53 +0100879 ring = b43legacy_setup_dmaring(dev, 2, 1, type);
Larry Finger75388ac2007-09-25 16:46:54 -0700880 if (!ring)
881 goto err_destroy_tx1;
882 dma->tx_ring2 = ring;
883
Stefano Brivio8e118f02008-02-08 06:31:53 +0100884 ring = b43legacy_setup_dmaring(dev, 3, 1, type);
Larry Finger75388ac2007-09-25 16:46:54 -0700885 if (!ring)
886 goto err_destroy_tx2;
887 dma->tx_ring3 = ring;
888
Stefano Brivio8e118f02008-02-08 06:31:53 +0100889 ring = b43legacy_setup_dmaring(dev, 4, 1, type);
Larry Finger75388ac2007-09-25 16:46:54 -0700890 if (!ring)
891 goto err_destroy_tx3;
892 dma->tx_ring4 = ring;
893
Stefano Brivio8e118f02008-02-08 06:31:53 +0100894 ring = b43legacy_setup_dmaring(dev, 5, 1, type);
Larry Finger75388ac2007-09-25 16:46:54 -0700895 if (!ring)
896 goto err_destroy_tx4;
897 dma->tx_ring5 = ring;
898
899 /* setup RX DMA channels. */
Stefano Brivio8e118f02008-02-08 06:31:53 +0100900 ring = b43legacy_setup_dmaring(dev, 0, 0, type);
Larry Finger75388ac2007-09-25 16:46:54 -0700901 if (!ring)
902 goto err_destroy_tx5;
903 dma->rx_ring0 = ring;
904
905 if (dev->dev->id.revision < 5) {
Stefano Brivio8e118f02008-02-08 06:31:53 +0100906 ring = b43legacy_setup_dmaring(dev, 3, 0, type);
Larry Finger75388ac2007-09-25 16:46:54 -0700907 if (!ring)
908 goto err_destroy_rx0;
909 dma->rx_ring3 = ring;
910 }
911
Stefano Brivio8e118f02008-02-08 06:31:53 +0100912 b43legacydbg(dev->wl, "%u-bit DMA initialized\n", (unsigned int)type);
Larry Finger75388ac2007-09-25 16:46:54 -0700913 err = 0;
914out:
915 return err;
916
917err_destroy_rx0:
918 b43legacy_destroy_dmaring(dma->rx_ring0);
919 dma->rx_ring0 = NULL;
920err_destroy_tx5:
921 b43legacy_destroy_dmaring(dma->tx_ring5);
922 dma->tx_ring5 = NULL;
923err_destroy_tx4:
924 b43legacy_destroy_dmaring(dma->tx_ring4);
925 dma->tx_ring4 = NULL;
926err_destroy_tx3:
927 b43legacy_destroy_dmaring(dma->tx_ring3);
928 dma->tx_ring3 = NULL;
929err_destroy_tx2:
930 b43legacy_destroy_dmaring(dma->tx_ring2);
931 dma->tx_ring2 = NULL;
932err_destroy_tx1:
933 b43legacy_destroy_dmaring(dma->tx_ring1);
934 dma->tx_ring1 = NULL;
935err_destroy_tx0:
936 b43legacy_destroy_dmaring(dma->tx_ring0);
937 dma->tx_ring0 = NULL;
938 goto out;
939}
940
941/* Generate a cookie for the TX header. */
942static u16 generate_cookie(struct b43legacy_dmaring *ring,
943 int slot)
944{
945 u16 cookie = 0x1000;
946
947 /* Use the upper 4 bits of the cookie as
948 * DMA controller ID and store the slot number
949 * in the lower 12 bits.
950 * Note that the cookie must never be 0, as this
951 * is a special value used in RX path.
952 */
953 switch (ring->index) {
954 case 0:
955 cookie = 0xA000;
956 break;
957 case 1:
958 cookie = 0xB000;
959 break;
960 case 2:
961 cookie = 0xC000;
962 break;
963 case 3:
964 cookie = 0xD000;
965 break;
966 case 4:
967 cookie = 0xE000;
968 break;
969 case 5:
970 cookie = 0xF000;
971 break;
972 }
973 B43legacy_WARN_ON(!(((u16)slot & 0xF000) == 0x0000));
974 cookie |= (u16)slot;
975
976 return cookie;
977}
978
979/* Inspect a cookie and find out to which controller/slot it belongs. */
980static
981struct b43legacy_dmaring *parse_cookie(struct b43legacy_wldev *dev,
982 u16 cookie, int *slot)
983{
984 struct b43legacy_dma *dma = &dev->dma;
985 struct b43legacy_dmaring *ring = NULL;
986
987 switch (cookie & 0xF000) {
988 case 0xA000:
989 ring = dma->tx_ring0;
990 break;
991 case 0xB000:
992 ring = dma->tx_ring1;
993 break;
994 case 0xC000:
995 ring = dma->tx_ring2;
996 break;
997 case 0xD000:
998 ring = dma->tx_ring3;
999 break;
1000 case 0xE000:
1001 ring = dma->tx_ring4;
1002 break;
1003 case 0xF000:
1004 ring = dma->tx_ring5;
1005 break;
1006 default:
1007 B43legacy_WARN_ON(1);
1008 }
1009 *slot = (cookie & 0x0FFF);
1010 B43legacy_WARN_ON(!(ring && *slot >= 0 && *slot < ring->nr_slots));
1011
1012 return ring;
1013}
1014
1015static int dma_tx_fragment(struct b43legacy_dmaring *ring,
Larry Fingerd2d8cda2009-10-30 11:58:21 -05001016 struct sk_buff **in_skb)
Larry Finger75388ac2007-09-25 16:46:54 -07001017{
Larry Fingerd2d8cda2009-10-30 11:58:21 -05001018 struct sk_buff *skb = *in_skb;
Johannes Berge039fa42008-05-15 12:55:29 +02001019 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Larry Finger75388ac2007-09-25 16:46:54 -07001020 u8 *header;
Stefano Brivio8dd01002008-02-02 19:16:03 +01001021 int slot, old_top_slot, old_used_slots;
Larry Finger75388ac2007-09-25 16:46:54 -07001022 int err;
Pavel Roskin191d6a82011-07-25 17:40:22 -04001023 struct b43legacy_dmadesc32 *desc;
Larry Finger75388ac2007-09-25 16:46:54 -07001024 struct b43legacy_dmadesc_meta *meta;
1025 struct b43legacy_dmadesc_meta *meta_hdr;
1026 struct sk_buff *bounce_skb;
1027
1028#define SLOTS_PER_PACKET 2
1029 B43legacy_WARN_ON(skb_shinfo(skb)->nr_frags != 0);
1030
Stefano Brivio8dd01002008-02-02 19:16:03 +01001031 old_top_slot = ring->current_slot;
1032 old_used_slots = ring->used_slots;
1033
Larry Finger75388ac2007-09-25 16:46:54 -07001034 /* Get a slot for the header. */
1035 slot = request_slot(ring);
Pavel Roskin191d6a82011-07-25 17:40:22 -04001036 desc = op32_idx2desc(ring, slot, &meta_hdr);
Larry Finger75388ac2007-09-25 16:46:54 -07001037 memset(meta_hdr, 0, sizeof(*meta_hdr));
1038
1039 header = &(ring->txhdr_cache[slot * sizeof(
1040 struct b43legacy_txhdr_fw3)]);
Stefano Brivio9eca9a82008-02-02 19:16:01 +01001041 err = b43legacy_generate_txhdr(ring->dev, header,
Johannes Berge039fa42008-05-15 12:55:29 +02001042 skb->data, skb->len, info,
Larry Finger75388ac2007-09-25 16:46:54 -07001043 generate_cookie(ring, slot));
Stefano Brivio8dd01002008-02-02 19:16:03 +01001044 if (unlikely(err)) {
1045 ring->current_slot = old_top_slot;
1046 ring->used_slots = old_used_slots;
Stefano Brivio9eca9a82008-02-02 19:16:01 +01001047 return err;
Stefano Brivio8dd01002008-02-02 19:16:03 +01001048 }
Larry Finger75388ac2007-09-25 16:46:54 -07001049
1050 meta_hdr->dmaaddr = map_descbuffer(ring, (unsigned char *)header,
Stefano Brivio8e118f02008-02-08 06:31:53 +01001051 sizeof(struct b43legacy_txhdr_fw3), 1);
1052 if (b43legacy_dma_mapping_error(ring, meta_hdr->dmaaddr,
Stefano Briviodc4ae1f2008-04-14 00:59:49 +02001053 sizeof(struct b43legacy_txhdr_fw3), 1)) {
Stefano Brivio8e118f02008-02-08 06:31:53 +01001054 ring->current_slot = old_top_slot;
1055 ring->used_slots = old_used_slots;
Larry Finger75388ac2007-09-25 16:46:54 -07001056 return -EIO;
Stefano Brivio8e118f02008-02-08 06:31:53 +01001057 }
Pavel Roskin191d6a82011-07-25 17:40:22 -04001058 op32_fill_descriptor(ring, desc, meta_hdr->dmaaddr,
Larry Finger75388ac2007-09-25 16:46:54 -07001059 sizeof(struct b43legacy_txhdr_fw3), 1, 0, 0);
1060
1061 /* Get a slot for the payload. */
1062 slot = request_slot(ring);
Pavel Roskin191d6a82011-07-25 17:40:22 -04001063 desc = op32_idx2desc(ring, slot, &meta);
Larry Finger75388ac2007-09-25 16:46:54 -07001064 memset(meta, 0, sizeof(*meta));
1065
Larry Finger75388ac2007-09-25 16:46:54 -07001066 meta->skb = skb;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001067 meta->is_last_fragment = true;
Larry Finger75388ac2007-09-25 16:46:54 -07001068
1069 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
1070 /* create a bounce buffer in zone_dma on mapping failure. */
Stefano Briviodc4ae1f2008-04-14 00:59:49 +02001071 if (b43legacy_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
Eric Dumazetacfa9e92012-07-02 08:36:12 +00001072 bounce_skb = alloc_skb(skb->len, GFP_ATOMIC | GFP_DMA);
Larry Finger75388ac2007-09-25 16:46:54 -07001073 if (!bounce_skb) {
Stefano Brivio8dd01002008-02-02 19:16:03 +01001074 ring->current_slot = old_top_slot;
1075 ring->used_slots = old_used_slots;
Larry Finger75388ac2007-09-25 16:46:54 -07001076 err = -ENOMEM;
1077 goto out_unmap_hdr;
1078 }
1079
1080 memcpy(skb_put(bounce_skb, skb->len), skb->data, skb->len);
Larry Fingerd2d8cda2009-10-30 11:58:21 -05001081 memcpy(bounce_skb->cb, skb->cb, sizeof(skb->cb));
1082 bounce_skb->dev = skb->dev;
1083 skb_set_queue_mapping(bounce_skb, skb_get_queue_mapping(skb));
1084 info = IEEE80211_SKB_CB(bounce_skb);
1085
Larry Finger75388ac2007-09-25 16:46:54 -07001086 dev_kfree_skb_any(skb);
1087 skb = bounce_skb;
Larry Fingerd2d8cda2009-10-30 11:58:21 -05001088 *in_skb = bounce_skb;
Larry Finger75388ac2007-09-25 16:46:54 -07001089 meta->skb = skb;
1090 meta->dmaaddr = map_descbuffer(ring, skb->data, skb->len, 1);
Stefano Briviodc4ae1f2008-04-14 00:59:49 +02001091 if (b43legacy_dma_mapping_error(ring, meta->dmaaddr, skb->len, 1)) {
Stefano Brivio8dd01002008-02-02 19:16:03 +01001092 ring->current_slot = old_top_slot;
1093 ring->used_slots = old_used_slots;
Larry Finger75388ac2007-09-25 16:46:54 -07001094 err = -EIO;
1095 goto out_free_bounce;
1096 }
1097 }
1098
Pavel Roskin191d6a82011-07-25 17:40:22 -04001099 op32_fill_descriptor(ring, desc, meta->dmaaddr,
Larry Finger75388ac2007-09-25 16:46:54 -07001100 skb->len, 0, 1, 1);
1101
1102 wmb(); /* previous stuff MUST be done */
1103 /* Now transfer the whole frame. */
Pavel Roskin191d6a82011-07-25 17:40:22 -04001104 op32_poke_tx(ring, next_slot(ring, slot));
Larry Finger75388ac2007-09-25 16:46:54 -07001105 return 0;
1106
1107out_free_bounce:
1108 dev_kfree_skb_any(skb);
1109out_unmap_hdr:
1110 unmap_descbuffer(ring, meta_hdr->dmaaddr,
1111 sizeof(struct b43legacy_txhdr_fw3), 1);
1112 return err;
1113}
1114
1115static inline
1116int should_inject_overflow(struct b43legacy_dmaring *ring)
1117{
1118#ifdef CONFIG_B43LEGACY_DEBUG
1119 if (unlikely(b43legacy_debug(ring->dev,
1120 B43legacy_DBG_DMAOVERFLOW))) {
1121 /* Check if we should inject another ringbuffer overflow
1122 * to test handling of this situation in the stack. */
1123 unsigned long next_overflow;
1124
1125 next_overflow = ring->last_injected_overflow + HZ;
1126 if (time_after(jiffies, next_overflow)) {
1127 ring->last_injected_overflow = jiffies;
1128 b43legacydbg(ring->dev->wl,
1129 "Injecting TX ring overflow on "
1130 "DMA controller %d\n", ring->index);
1131 return 1;
1132 }
1133 }
1134#endif /* CONFIG_B43LEGACY_DEBUG */
1135 return 0;
1136}
1137
1138int b43legacy_dma_tx(struct b43legacy_wldev *dev,
Johannes Berge039fa42008-05-15 12:55:29 +02001139 struct sk_buff *skb)
Larry Finger75388ac2007-09-25 16:46:54 -07001140{
1141 struct b43legacy_dmaring *ring;
1142 int err = 0;
Larry Finger75388ac2007-09-25 16:46:54 -07001143
Johannes Berge2530082008-05-17 00:57:14 +02001144 ring = priority_to_txring(dev, skb_get_queue_mapping(skb));
Larry Finger75388ac2007-09-25 16:46:54 -07001145 B43legacy_WARN_ON(!ring->tx);
Larry Fingerc1be5152009-08-01 22:32:48 -05001146
1147 if (unlikely(ring->stopped)) {
1148 /* We get here only because of a bug in mac80211.
1149 * Because of a race, one packet may be queued after
1150 * the queue is stopped, thus we got called when we shouldn't.
1151 * For now, just refuse the transmit. */
1152 if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
1153 b43legacyerr(dev->wl, "Packet after queue stopped\n");
Larry Finger5d07a3d2011-12-21 18:47:59 -06001154 return -ENOSPC;
Larry Finger75388ac2007-09-25 16:46:54 -07001155 }
Larry Fingerc1be5152009-08-01 22:32:48 -05001156
1157 if (unlikely(WARN_ON(free_slots(ring) < SLOTS_PER_PACKET))) {
1158 /* If we get here, we have a real error with the queue
1159 * full, but queues not stopped. */
1160 b43legacyerr(dev->wl, "DMA queue overflow\n");
Larry Finger5d07a3d2011-12-21 18:47:59 -06001161 return -ENOSPC;
Larry Fingerc1be5152009-08-01 22:32:48 -05001162 }
Larry Finger75388ac2007-09-25 16:46:54 -07001163
Larry Fingerd2d8cda2009-10-30 11:58:21 -05001164 /* dma_tx_fragment might reallocate the skb, so invalidate pointers pointing
1165 * into the skb data or cb now. */
Larry Fingerd2d8cda2009-10-30 11:58:21 -05001166 err = dma_tx_fragment(ring, &skb);
Stefano Brivio9eca9a82008-02-02 19:16:01 +01001167 if (unlikely(err == -ENOKEY)) {
1168 /* Drop this packet, as we don't have the encryption key
1169 * anymore and must not transmit it unencrypted. */
1170 dev_kfree_skb_any(skb);
Larry Finger5d07a3d2011-12-21 18:47:59 -06001171 return 0;
Stefano Brivio9eca9a82008-02-02 19:16:01 +01001172 }
Larry Finger75388ac2007-09-25 16:46:54 -07001173 if (unlikely(err)) {
1174 b43legacyerr(dev->wl, "DMA tx mapping failure\n");
Larry Finger5d07a3d2011-12-21 18:47:59 -06001175 return err;
Larry Finger75388ac2007-09-25 16:46:54 -07001176 }
Larry Finger75388ac2007-09-25 16:46:54 -07001177 if ((free_slots(ring) < SLOTS_PER_PACKET) ||
1178 should_inject_overflow(ring)) {
1179 /* This TX ring is full. */
Larry Finger5d07a3d2011-12-21 18:47:59 -06001180 unsigned int skb_mapping = skb_get_queue_mapping(skb);
1181 ieee80211_stop_queue(dev->wl->hw, skb_mapping);
1182 dev->wl->tx_queue_stopped[skb_mapping] = 1;
Rusty Russell3db1cd52011-12-19 13:56:45 +00001183 ring->stopped = true;
Larry Finger75388ac2007-09-25 16:46:54 -07001184 if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
1185 b43legacydbg(dev->wl, "Stopped TX ring %d\n",
1186 ring->index);
1187 }
Larry Finger75388ac2007-09-25 16:46:54 -07001188 return err;
1189}
1190
1191void b43legacy_dma_handle_txstatus(struct b43legacy_wldev *dev,
1192 const struct b43legacy_txstatus *status)
1193{
Larry Finger75388ac2007-09-25 16:46:54 -07001194 struct b43legacy_dmaring *ring;
Larry Finger75388ac2007-09-25 16:46:54 -07001195 struct b43legacy_dmadesc_meta *meta;
Johannes Berge6a98542008-10-21 12:40:02 +02001196 int retry_limit;
Larry Finger75388ac2007-09-25 16:46:54 -07001197 int slot;
Larry Finger5d07a3d2011-12-21 18:47:59 -06001198 int firstused;
Larry Finger75388ac2007-09-25 16:46:54 -07001199
1200 ring = parse_cookie(dev, status->cookie, &slot);
1201 if (unlikely(!ring))
1202 return;
Larry Finger75388ac2007-09-25 16:46:54 -07001203 B43legacy_WARN_ON(!ring->tx);
Larry Finger5d07a3d2011-12-21 18:47:59 -06001204
1205 /* Sanity check: TX packets are processed in-order on one ring.
1206 * Check if the slot deduced from the cookie really is the first
1207 * used slot. */
1208 firstused = ring->current_slot - ring->used_slots + 1;
1209 if (firstused < 0)
1210 firstused = ring->nr_slots + firstused;
1211 if (unlikely(slot != firstused)) {
1212 /* This possibly is a firmware bug and will result in
1213 * malfunction, memory leaks and/or stall of DMA functionality.
1214 */
1215 b43legacydbg(dev->wl, "Out of order TX status report on DMA "
1216 "ring %d. Expected %d, but got %d\n",
1217 ring->index, firstused, slot);
1218 return;
1219 }
1220
Larry Finger75388ac2007-09-25 16:46:54 -07001221 while (1) {
1222 B43legacy_WARN_ON(!(slot >= 0 && slot < ring->nr_slots));
Pavel Roskin191d6a82011-07-25 17:40:22 -04001223 op32_idx2desc(ring, slot, &meta);
Larry Finger75388ac2007-09-25 16:46:54 -07001224
1225 if (meta->skb)
1226 unmap_descbuffer(ring, meta->dmaaddr,
1227 meta->skb->len, 1);
1228 else
1229 unmap_descbuffer(ring, meta->dmaaddr,
1230 sizeof(struct b43legacy_txhdr_fw3),
1231 1);
1232
1233 if (meta->is_last_fragment) {
Johannes Berge039fa42008-05-15 12:55:29 +02001234 struct ieee80211_tx_info *info;
1235 BUG_ON(!meta->skb);
1236 info = IEEE80211_SKB_CB(meta->skb);
Johannes Berge6a98542008-10-21 12:40:02 +02001237
1238 /* preserve the confiured retry limit before clearing the status
1239 * The xmit function has overwritten the rc's value with the actual
1240 * retry limit done by the hardware */
1241 retry_limit = info->status.rates[0].count;
1242 ieee80211_tx_info_clear_status(info);
1243
1244 if (status->acked)
1245 info->flags |= IEEE80211_TX_STAT_ACK;
1246
1247 if (status->rts_count > dev->wl->hw->conf.short_frame_max_tx_count) {
1248 /*
1249 * If the short retries (RTS, not data frame) have exceeded
1250 * the limit, the hw will not have tried the selected rate,
1251 * but will have used the fallback rate instead.
1252 * Don't let the rate control count attempts for the selected
1253 * rate in this case, otherwise the statistics will be off.
1254 */
1255 info->status.rates[0].count = 0;
1256 info->status.rates[1].count = status->frame_count;
1257 } else {
1258 if (status->frame_count > retry_limit) {
1259 info->status.rates[0].count = retry_limit;
1260 info->status.rates[1].count = status->frame_count -
1261 retry_limit;
1262
1263 } else {
1264 info->status.rates[0].count = status->frame_count;
1265 info->status.rates[1].idx = -1;
1266 }
1267 }
1268
Larry Finger75388ac2007-09-25 16:46:54 -07001269 /* Call back to inform the ieee80211 subsystem about the
1270 * status of the transmission.
1271 * Some fields of txstat are already filled in dma_tx().
1272 */
Johannes Berge039fa42008-05-15 12:55:29 +02001273 ieee80211_tx_status_irqsafe(dev->wl->hw, meta->skb);
Larry Finger75388ac2007-09-25 16:46:54 -07001274 /* skb is freed by ieee80211_tx_status_irqsafe() */
1275 meta->skb = NULL;
1276 } else {
1277 /* No need to call free_descriptor_buffer here, as
1278 * this is only the txhdr, which is not allocated.
1279 */
1280 B43legacy_WARN_ON(meta->skb != NULL);
1281 }
1282
1283 /* Everything unmapped and free'd. So it's not used anymore. */
1284 ring->used_slots--;
1285
1286 if (meta->is_last_fragment)
1287 break;
1288 slot = next_slot(ring, slot);
1289 }
1290 dev->stats.last_tx = jiffies;
1291 if (ring->stopped) {
1292 B43legacy_WARN_ON(free_slots(ring) < SLOTS_PER_PACKET);
Rusty Russell3db1cd52011-12-19 13:56:45 +00001293 ring->stopped = false;
Larry Finger75388ac2007-09-25 16:46:54 -07001294 }
1295
Larry Finger5d07a3d2011-12-21 18:47:59 -06001296 if (dev->wl->tx_queue_stopped[ring->queue_prio]) {
1297 dev->wl->tx_queue_stopped[ring->queue_prio] = 0;
1298 } else {
1299 /* If the driver queue is running wake the corresponding
1300 * mac80211 queue. */
1301 ieee80211_wake_queue(dev->wl->hw, ring->queue_prio);
1302 if (b43legacy_debug(dev, B43legacy_DBG_DMAVERBOSE))
1303 b43legacydbg(dev->wl, "Woke up TX ring %d\n",
1304 ring->index);
1305 }
1306 /* Add work to the queue. */
1307 ieee80211_queue_work(dev->wl->hw, &dev->wl->tx_work);
Larry Finger75388ac2007-09-25 16:46:54 -07001308}
1309
Larry Finger75388ac2007-09-25 16:46:54 -07001310static void dma_rx(struct b43legacy_dmaring *ring,
1311 int *slot)
1312{
Pavel Roskin191d6a82011-07-25 17:40:22 -04001313 struct b43legacy_dmadesc32 *desc;
Larry Finger75388ac2007-09-25 16:46:54 -07001314 struct b43legacy_dmadesc_meta *meta;
1315 struct b43legacy_rxhdr_fw3 *rxhdr;
1316 struct sk_buff *skb;
1317 u16 len;
1318 int err;
1319 dma_addr_t dmaaddr;
1320
Pavel Roskin191d6a82011-07-25 17:40:22 -04001321 desc = op32_idx2desc(ring, *slot, &meta);
Larry Finger75388ac2007-09-25 16:46:54 -07001322
1323 sync_descbuffer_for_cpu(ring, meta->dmaaddr, ring->rx_buffersize);
1324 skb = meta->skb;
1325
1326 if (ring->index == 3) {
1327 /* We received an xmit status. */
1328 struct b43legacy_hwtxstatus *hw =
1329 (struct b43legacy_hwtxstatus *)skb->data;
1330 int i = 0;
1331
1332 while (hw->cookie == 0) {
1333 if (i > 100)
1334 break;
1335 i++;
1336 udelay(2);
1337 barrier();
1338 }
1339 b43legacy_handle_hwtxstatus(ring->dev, hw);
1340 /* recycle the descriptor buffer. */
1341 sync_descbuffer_for_device(ring, meta->dmaaddr,
1342 ring->rx_buffersize);
1343
1344 return;
1345 }
1346 rxhdr = (struct b43legacy_rxhdr_fw3 *)skb->data;
1347 len = le16_to_cpu(rxhdr->frame_len);
1348 if (len == 0) {
1349 int i = 0;
1350
1351 do {
1352 udelay(2);
1353 barrier();
1354 len = le16_to_cpu(rxhdr->frame_len);
1355 } while (len == 0 && i++ < 5);
1356 if (unlikely(len == 0)) {
1357 /* recycle the descriptor buffer. */
1358 sync_descbuffer_for_device(ring, meta->dmaaddr,
1359 ring->rx_buffersize);
1360 goto drop;
1361 }
1362 }
1363 if (unlikely(len > ring->rx_buffersize)) {
1364 /* The data did not fit into one descriptor buffer
1365 * and is split over multiple buffers.
1366 * This should never happen, as we try to allocate buffers
1367 * big enough. So simply ignore this packet.
1368 */
1369 int cnt = 0;
1370 s32 tmp = len;
1371
1372 while (1) {
Pavel Roskin191d6a82011-07-25 17:40:22 -04001373 desc = op32_idx2desc(ring, *slot, &meta);
Larry Finger75388ac2007-09-25 16:46:54 -07001374 /* recycle the descriptor buffer. */
1375 sync_descbuffer_for_device(ring, meta->dmaaddr,
1376 ring->rx_buffersize);
1377 *slot = next_slot(ring, *slot);
1378 cnt++;
1379 tmp -= ring->rx_buffersize;
1380 if (tmp <= 0)
1381 break;
1382 }
1383 b43legacyerr(ring->dev->wl, "DMA RX buffer too small "
1384 "(len: %u, buffer: %u, nr-dropped: %d)\n",
1385 len, ring->rx_buffersize, cnt);
1386 goto drop;
1387 }
1388
1389 dmaaddr = meta->dmaaddr;
1390 err = setup_rx_descbuffer(ring, desc, meta, GFP_ATOMIC);
1391 if (unlikely(err)) {
1392 b43legacydbg(ring->dev->wl, "DMA RX: setup_rx_descbuffer()"
1393 " failed\n");
1394 sync_descbuffer_for_device(ring, dmaaddr,
1395 ring->rx_buffersize);
1396 goto drop;
1397 }
1398
1399 unmap_descbuffer(ring, dmaaddr, ring->rx_buffersize, 0);
1400 skb_put(skb, len + ring->frameoffset);
1401 skb_pull(skb, ring->frameoffset);
1402
1403 b43legacy_rx(ring->dev, skb, rxhdr);
1404drop:
1405 return;
1406}
1407
1408void b43legacy_dma_rx(struct b43legacy_dmaring *ring)
1409{
Larry Finger75388ac2007-09-25 16:46:54 -07001410 int slot;
1411 int current_slot;
1412 int used_slots = 0;
1413
1414 B43legacy_WARN_ON(ring->tx);
Pavel Roskin191d6a82011-07-25 17:40:22 -04001415 current_slot = op32_get_current_rxslot(ring);
Larry Finger75388ac2007-09-25 16:46:54 -07001416 B43legacy_WARN_ON(!(current_slot >= 0 && current_slot <
1417 ring->nr_slots));
1418
1419 slot = ring->current_slot;
1420 for (; slot != current_slot; slot = next_slot(ring, slot)) {
1421 dma_rx(ring, &slot);
1422 update_max_used_slots(ring, ++used_slots);
1423 }
Pavel Roskin191d6a82011-07-25 17:40:22 -04001424 op32_set_current_rxslot(ring, slot);
Larry Finger75388ac2007-09-25 16:46:54 -07001425 ring->current_slot = slot;
1426}
1427
1428static void b43legacy_dma_tx_suspend_ring(struct b43legacy_dmaring *ring)
1429{
Larry Finger75388ac2007-09-25 16:46:54 -07001430 B43legacy_WARN_ON(!ring->tx);
Pavel Roskin191d6a82011-07-25 17:40:22 -04001431 op32_tx_suspend(ring);
Larry Finger75388ac2007-09-25 16:46:54 -07001432}
1433
1434static void b43legacy_dma_tx_resume_ring(struct b43legacy_dmaring *ring)
1435{
Larry Finger75388ac2007-09-25 16:46:54 -07001436 B43legacy_WARN_ON(!ring->tx);
Pavel Roskin191d6a82011-07-25 17:40:22 -04001437 op32_tx_resume(ring);
Larry Finger75388ac2007-09-25 16:46:54 -07001438}
1439
1440void b43legacy_dma_tx_suspend(struct b43legacy_wldev *dev)
1441{
1442 b43legacy_power_saving_ctl_bits(dev, -1, 1);
1443 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring0);
1444 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring1);
1445 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring2);
1446 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring3);
1447 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring4);
1448 b43legacy_dma_tx_suspend_ring(dev->dma.tx_ring5);
1449}
1450
1451void b43legacy_dma_tx_resume(struct b43legacy_wldev *dev)
1452{
1453 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring5);
1454 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring4);
1455 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring3);
1456 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring2);
1457 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring1);
1458 b43legacy_dma_tx_resume_ring(dev->dma.tx_ring0);
1459 b43legacy_power_saving_ctl_bits(dev, -1, -1);
1460}