blob: c01b8e02412f80b64f13c86cfb812bb3db03051d [file] [log] [blame]
Michael Buesch5100d5a2008-03-29 21:01:16 +01001/*
2
3 Broadcom B43 wireless driver
4
5 PIO data transfer
6
7 Copyright (c) 2005-2008 Michael Buesch <mb@bu3sch.de>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
22 Boston, MA 02110-1301, USA.
23
24*/
25
26#include "b43.h"
27#include "pio.h"
28#include "dma.h"
29#include "main.h"
30#include "xmit.h"
31
32#include <linux/delay.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040033#include <linux/sched.h>
Michael Buesch5100d5a2008-03-29 21:01:16 +010034
35
Michael Buesch5100d5a2008-03-29 21:01:16 +010036static u16 generate_cookie(struct b43_pio_txqueue *q,
37 struct b43_pio_txpacket *pack)
38{
39 u16 cookie;
40
41 /* Use the upper 4 bits of the cookie as
42 * PIO controller ID and store the packet index number
43 * in the lower 12 bits.
44 * Note that the cookie must never be 0, as this
45 * is a special value used in RX path.
46 * It can also not be 0xFFFF because that is special
47 * for multicast frames.
48 */
49 cookie = (((u16)q->index + 1) << 12);
50 cookie |= pack->index;
51
52 return cookie;
53}
54
55static
John Daiker99da1852009-02-24 02:16:42 -080056struct b43_pio_txqueue *parse_cookie(struct b43_wldev *dev,
57 u16 cookie,
Michael Buesch5100d5a2008-03-29 21:01:16 +010058 struct b43_pio_txpacket **pack)
59{
60 struct b43_pio *pio = &dev->pio;
61 struct b43_pio_txqueue *q = NULL;
62 unsigned int pack_index;
63
64 switch (cookie & 0xF000) {
65 case 0x1000:
66 q = pio->tx_queue_AC_BK;
67 break;
68 case 0x2000:
69 q = pio->tx_queue_AC_BE;
70 break;
71 case 0x3000:
72 q = pio->tx_queue_AC_VI;
73 break;
74 case 0x4000:
75 q = pio->tx_queue_AC_VO;
76 break;
77 case 0x5000:
78 q = pio->tx_queue_mcast;
79 break;
80 }
81 if (B43_WARN_ON(!q))
82 return NULL;
83 pack_index = (cookie & 0x0FFF);
84 if (B43_WARN_ON(pack_index >= ARRAY_SIZE(q->packets)))
85 return NULL;
86 *pack = &q->packets[pack_index];
87
88 return q;
89}
90
91static u16 index_to_pioqueue_base(struct b43_wldev *dev,
92 unsigned int index)
93{
94 static const u16 bases[] = {
95 B43_MMIO_PIO_BASE0,
96 B43_MMIO_PIO_BASE1,
97 B43_MMIO_PIO_BASE2,
98 B43_MMIO_PIO_BASE3,
99 B43_MMIO_PIO_BASE4,
100 B43_MMIO_PIO_BASE5,
101 B43_MMIO_PIO_BASE6,
102 B43_MMIO_PIO_BASE7,
103 };
104 static const u16 bases_rev11[] = {
105 B43_MMIO_PIO11_BASE0,
106 B43_MMIO_PIO11_BASE1,
107 B43_MMIO_PIO11_BASE2,
108 B43_MMIO_PIO11_BASE3,
109 B43_MMIO_PIO11_BASE4,
110 B43_MMIO_PIO11_BASE5,
111 };
112
113 if (dev->dev->id.revision >= 11) {
114 B43_WARN_ON(index >= ARRAY_SIZE(bases_rev11));
115 return bases_rev11[index];
116 }
117 B43_WARN_ON(index >= ARRAY_SIZE(bases));
118 return bases[index];
119}
120
121static u16 pio_txqueue_offset(struct b43_wldev *dev)
122{
123 if (dev->dev->id.revision >= 11)
124 return 0x18;
125 return 0;
126}
127
128static u16 pio_rxqueue_offset(struct b43_wldev *dev)
129{
130 if (dev->dev->id.revision >= 11)
131 return 0x38;
132 return 8;
133}
134
John Daiker99da1852009-02-24 02:16:42 -0800135static struct b43_pio_txqueue *b43_setup_pioqueue_tx(struct b43_wldev *dev,
136 unsigned int index)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100137{
138 struct b43_pio_txqueue *q;
139 struct b43_pio_txpacket *p;
140 unsigned int i;
141
142 q = kzalloc(sizeof(*q), GFP_KERNEL);
143 if (!q)
144 return NULL;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100145 q->dev = dev;
146 q->rev = dev->dev->id.revision;
147 q->mmio_base = index_to_pioqueue_base(dev, index) +
148 pio_txqueue_offset(dev);
149 q->index = index;
150
151 q->free_packet_slots = B43_PIO_MAX_NR_TXPACKETS;
152 if (q->rev >= 8) {
153 q->buffer_size = 1920; //FIXME this constant is wrong.
154 } else {
155 q->buffer_size = b43_piotx_read16(q, B43_PIO_TXQBUFSIZE);
156 q->buffer_size -= 80;
157 }
158
159 INIT_LIST_HEAD(&q->packets_list);
160 for (i = 0; i < ARRAY_SIZE(q->packets); i++) {
161 p = &(q->packets[i]);
162 INIT_LIST_HEAD(&p->list);
163 p->index = i;
164 p->queue = q;
165 list_add(&p->list, &q->packets_list);
166 }
167
168 return q;
169}
170
John Daiker99da1852009-02-24 02:16:42 -0800171static struct b43_pio_rxqueue *b43_setup_pioqueue_rx(struct b43_wldev *dev,
172 unsigned int index)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100173{
174 struct b43_pio_rxqueue *q;
175
176 q = kzalloc(sizeof(*q), GFP_KERNEL);
177 if (!q)
178 return NULL;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100179 q->dev = dev;
180 q->rev = dev->dev->id.revision;
181 q->mmio_base = index_to_pioqueue_base(dev, index) +
182 pio_rxqueue_offset(dev);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100183
184 /* Enable Direct FIFO RX (PIO) on the engine. */
185 b43_dma_direct_fifo_rx(dev, index, 1);
186
187 return q;
188}
189
190static void b43_pio_cancel_tx_packets(struct b43_pio_txqueue *q)
191{
192 struct b43_pio_txpacket *pack;
193 unsigned int i;
194
195 for (i = 0; i < ARRAY_SIZE(q->packets); i++) {
196 pack = &(q->packets[i]);
197 if (pack->skb) {
198 dev_kfree_skb_any(pack->skb);
199 pack->skb = NULL;
200 }
201 }
202}
203
204static void b43_destroy_pioqueue_tx(struct b43_pio_txqueue *q,
205 const char *name)
206{
207 if (!q)
208 return;
209 b43_pio_cancel_tx_packets(q);
210 kfree(q);
211}
212
213static void b43_destroy_pioqueue_rx(struct b43_pio_rxqueue *q,
214 const char *name)
215{
216 if (!q)
217 return;
218 kfree(q);
219}
220
221#define destroy_queue_tx(pio, queue) do { \
222 b43_destroy_pioqueue_tx((pio)->queue, __stringify(queue)); \
223 (pio)->queue = NULL; \
224 } while (0)
225
226#define destroy_queue_rx(pio, queue) do { \
227 b43_destroy_pioqueue_rx((pio)->queue, __stringify(queue)); \
228 (pio)->queue = NULL; \
229 } while (0)
230
231void b43_pio_free(struct b43_wldev *dev)
232{
233 struct b43_pio *pio;
234
235 if (!b43_using_pio_transfers(dev))
236 return;
237 pio = &dev->pio;
238
239 destroy_queue_rx(pio, rx_queue);
240 destroy_queue_tx(pio, tx_queue_mcast);
241 destroy_queue_tx(pio, tx_queue_AC_VO);
242 destroy_queue_tx(pio, tx_queue_AC_VI);
243 destroy_queue_tx(pio, tx_queue_AC_BE);
244 destroy_queue_tx(pio, tx_queue_AC_BK);
245}
246
Michael Buesch5100d5a2008-03-29 21:01:16 +0100247int b43_pio_init(struct b43_wldev *dev)
248{
249 struct b43_pio *pio = &dev->pio;
250 int err = -ENOMEM;
251
252 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
253 & ~B43_MACCTL_BE);
254 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_RXPADOFF, 0);
255
256 pio->tx_queue_AC_BK = b43_setup_pioqueue_tx(dev, 0);
257 if (!pio->tx_queue_AC_BK)
258 goto out;
259
260 pio->tx_queue_AC_BE = b43_setup_pioqueue_tx(dev, 1);
261 if (!pio->tx_queue_AC_BE)
262 goto err_destroy_bk;
263
264 pio->tx_queue_AC_VI = b43_setup_pioqueue_tx(dev, 2);
265 if (!pio->tx_queue_AC_VI)
266 goto err_destroy_be;
267
268 pio->tx_queue_AC_VO = b43_setup_pioqueue_tx(dev, 3);
269 if (!pio->tx_queue_AC_VO)
270 goto err_destroy_vi;
271
272 pio->tx_queue_mcast = b43_setup_pioqueue_tx(dev, 4);
273 if (!pio->tx_queue_mcast)
274 goto err_destroy_vo;
275
276 pio->rx_queue = b43_setup_pioqueue_rx(dev, 0);
277 if (!pio->rx_queue)
278 goto err_destroy_mcast;
279
280 b43dbg(dev->wl, "PIO initialized\n");
281 err = 0;
282out:
283 return err;
284
285err_destroy_mcast:
286 destroy_queue_tx(pio, tx_queue_mcast);
287err_destroy_vo:
288 destroy_queue_tx(pio, tx_queue_AC_VO);
289err_destroy_vi:
290 destroy_queue_tx(pio, tx_queue_AC_VI);
291err_destroy_be:
292 destroy_queue_tx(pio, tx_queue_AC_BE);
293err_destroy_bk:
294 destroy_queue_tx(pio, tx_queue_AC_BK);
295 return err;
296}
297
298/* Static mapping of mac80211's queues (priorities) to b43 PIO queues. */
John Daiker99da1852009-02-24 02:16:42 -0800299static struct b43_pio_txqueue *select_queue_by_priority(struct b43_wldev *dev,
300 u8 queue_prio)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100301{
302 struct b43_pio_txqueue *q;
303
Michael Buesch403a3a12009-06-08 21:04:57 +0200304 if (dev->qos_enabled) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100305 /* 0 = highest priority */
306 switch (queue_prio) {
307 default:
308 B43_WARN_ON(1);
309 /* fallthrough */
310 case 0:
311 q = dev->pio.tx_queue_AC_VO;
312 break;
313 case 1:
314 q = dev->pio.tx_queue_AC_VI;
315 break;
316 case 2:
317 q = dev->pio.tx_queue_AC_BE;
318 break;
319 case 3:
320 q = dev->pio.tx_queue_AC_BK;
321 break;
322 }
323 } else
324 q = dev->pio.tx_queue_AC_BE;
325
326 return q;
327}
328
Michael Bueschd8c17e12008-04-02 19:58:20 +0200329static u16 tx_write_2byte_queue(struct b43_pio_txqueue *q,
330 u16 ctl,
331 const void *_data,
332 unsigned int data_len)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100333{
Michael Bueschd8c17e12008-04-02 19:58:20 +0200334 struct b43_wldev *dev = q->dev;
Albert Herranz7e937c62009-10-07 00:07:44 +0200335 struct b43_wl *wl = dev->wl;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100336 const u8 *data = _data;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100337
Michael Bueschd8c17e12008-04-02 19:58:20 +0200338 ctl |= B43_PIO_TXCTL_WRITELO | B43_PIO_TXCTL_WRITEHI;
339 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
340
341 ssb_block_write(dev->dev, data, (data_len & ~1),
342 q->mmio_base + B43_PIO_TXDATA,
343 sizeof(u16));
344 if (data_len & 1) {
Michael Buesch88499ab2009-10-09 20:33:32 +0200345 u8 *tail = wl->pio_tailspace;
346 BUILD_BUG_ON(sizeof(wl->pio_tailspace) < 2);
347
Michael Bueschd8c17e12008-04-02 19:58:20 +0200348 /* Write the last byte. */
349 ctl &= ~B43_PIO_TXCTL_WRITEHI;
350 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
Michael Buesch88499ab2009-10-09 20:33:32 +0200351 tail[0] = data[data_len - 1];
352 tail[1] = 0;
353 ssb_block_write(dev->dev, tail, 2,
Michael Bueschb96ab542009-09-23 18:51:21 +0200354 q->mmio_base + B43_PIO_TXDATA,
355 sizeof(u16));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100356 }
Michael Bueschd8c17e12008-04-02 19:58:20 +0200357
358 return ctl;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100359}
360
361static void pio_tx_frame_2byte_queue(struct b43_pio_txpacket *pack,
362 const u8 *hdr, unsigned int hdrlen)
363{
364 struct b43_pio_txqueue *q = pack->queue;
365 const char *frame = pack->skb->data;
366 unsigned int frame_len = pack->skb->len;
367 u16 ctl;
368
369 ctl = b43_piotx_read16(q, B43_PIO_TXCTL);
370 ctl |= B43_PIO_TXCTL_FREADY;
371 ctl &= ~B43_PIO_TXCTL_EOF;
372
373 /* Transfer the header data. */
Michael Bueschd8c17e12008-04-02 19:58:20 +0200374 ctl = tx_write_2byte_queue(q, ctl, hdr, hdrlen);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100375 /* Transfer the frame data. */
Michael Bueschd8c17e12008-04-02 19:58:20 +0200376 ctl = tx_write_2byte_queue(q, ctl, frame, frame_len);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100377
378 ctl |= B43_PIO_TXCTL_EOF;
379 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
380}
381
Michael Bueschd8c17e12008-04-02 19:58:20 +0200382static u32 tx_write_4byte_queue(struct b43_pio_txqueue *q,
383 u32 ctl,
384 const void *_data,
385 unsigned int data_len)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100386{
Michael Bueschd8c17e12008-04-02 19:58:20 +0200387 struct b43_wldev *dev = q->dev;
Albert Herranz7e937c62009-10-07 00:07:44 +0200388 struct b43_wl *wl = dev->wl;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100389 const u8 *data = _data;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100390
Michael Bueschd8c17e12008-04-02 19:58:20 +0200391 ctl |= B43_PIO8_TXCTL_0_7 | B43_PIO8_TXCTL_8_15 |
392 B43_PIO8_TXCTL_16_23 | B43_PIO8_TXCTL_24_31;
393 b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
394
395 ssb_block_write(dev->dev, data, (data_len & ~3),
396 q->mmio_base + B43_PIO8_TXDATA,
397 sizeof(u32));
398 if (data_len & 3) {
Michael Buesch88499ab2009-10-09 20:33:32 +0200399 u8 *tail = wl->pio_tailspace;
400 BUILD_BUG_ON(sizeof(wl->pio_tailspace) < 4);
401
402 memset(tail, 0, 4);
Michael Bueschd8c17e12008-04-02 19:58:20 +0200403 /* Write the last few bytes. */
404 ctl &= ~(B43_PIO8_TXCTL_8_15 | B43_PIO8_TXCTL_16_23 |
405 B43_PIO8_TXCTL_24_31);
Michael Bueschd8c17e12008-04-02 19:58:20 +0200406 switch (data_len & 3) {
407 case 3:
Michael Bueschb96ab542009-09-23 18:51:21 +0200408 ctl |= B43_PIO8_TXCTL_16_23 | B43_PIO8_TXCTL_8_15;
Michael Buesch88499ab2009-10-09 20:33:32 +0200409 tail[0] = data[data_len - 3];
410 tail[1] = data[data_len - 2];
411 tail[2] = data[data_len - 1];
Michael Bueschb96ab542009-09-23 18:51:21 +0200412 break;
Michael Bueschd8c17e12008-04-02 19:58:20 +0200413 case 2:
414 ctl |= B43_PIO8_TXCTL_8_15;
Michael Buesch88499ab2009-10-09 20:33:32 +0200415 tail[0] = data[data_len - 2];
416 tail[1] = data[data_len - 1];
Michael Bueschb96ab542009-09-23 18:51:21 +0200417 break;
Michael Bueschd8c17e12008-04-02 19:58:20 +0200418 case 1:
Michael Buesch88499ab2009-10-09 20:33:32 +0200419 tail[0] = data[data_len - 1];
Michael Bueschb96ab542009-09-23 18:51:21 +0200420 break;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100421 }
Michael Bueschd8c17e12008-04-02 19:58:20 +0200422 b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
Michael Buesch88499ab2009-10-09 20:33:32 +0200423 ssb_block_write(dev->dev, tail, 4,
Michael Bueschb96ab542009-09-23 18:51:21 +0200424 q->mmio_base + B43_PIO8_TXDATA,
425 sizeof(u32));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100426 }
Michael Bueschd8c17e12008-04-02 19:58:20 +0200427
428 return ctl;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100429}
430
431static void pio_tx_frame_4byte_queue(struct b43_pio_txpacket *pack,
432 const u8 *hdr, unsigned int hdrlen)
433{
434 struct b43_pio_txqueue *q = pack->queue;
435 const char *frame = pack->skb->data;
436 unsigned int frame_len = pack->skb->len;
437 u32 ctl;
438
439 ctl = b43_piotx_read32(q, B43_PIO8_TXCTL);
440 ctl |= B43_PIO8_TXCTL_FREADY;
441 ctl &= ~B43_PIO8_TXCTL_EOF;
442
443 /* Transfer the header data. */
Michael Bueschd8c17e12008-04-02 19:58:20 +0200444 ctl = tx_write_4byte_queue(q, ctl, hdr, hdrlen);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100445 /* Transfer the frame data. */
Michael Bueschd8c17e12008-04-02 19:58:20 +0200446 ctl = tx_write_4byte_queue(q, ctl, frame, frame_len);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100447
448 ctl |= B43_PIO8_TXCTL_EOF;
449 b43_piotx_write32(q, B43_PIO_TXCTL, ctl);
450}
451
452static int pio_tx_frame(struct b43_pio_txqueue *q,
Johannes Berge039fa42008-05-15 12:55:29 +0200453 struct sk_buff *skb)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100454{
Albert Herranz7e937c62009-10-07 00:07:44 +0200455 struct b43_wldev *dev = q->dev;
456 struct b43_wl *wl = dev->wl;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100457 struct b43_pio_txpacket *pack;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100458 u16 cookie;
459 int err;
460 unsigned int hdrlen;
Johannes Berge039fa42008-05-15 12:55:29 +0200461 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Michael Buesch88499ab2009-10-09 20:33:32 +0200462 struct b43_txhdr *txhdr = (struct b43_txhdr *)wl->pio_scratchspace;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100463
464 B43_WARN_ON(list_empty(&q->packets_list));
465 pack = list_entry(q->packets_list.next,
466 struct b43_pio_txpacket, list);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100467
468 cookie = generate_cookie(q, pack);
Albert Herranz7e937c62009-10-07 00:07:44 +0200469 hdrlen = b43_txhdr_size(dev);
Michael Buesch88499ab2009-10-09 20:33:32 +0200470 BUILD_BUG_ON(sizeof(wl->pio_scratchspace) < sizeof(struct b43_txhdr));
471 B43_WARN_ON(sizeof(wl->pio_scratchspace) < hdrlen);
472 err = b43_generate_txhdr(dev, (u8 *)txhdr, skb,
gregor kowski035d0242009-08-19 22:35:45 +0200473 info, cookie);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100474 if (err)
475 return err;
476
Johannes Berge039fa42008-05-15 12:55:29 +0200477 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100478 /* Tell the firmware about the cookie of the last
479 * mcast frame, so it can clear the more-data bit in it. */
Albert Herranz7e937c62009-10-07 00:07:44 +0200480 b43_shm_write16(dev, B43_SHM_SHARED,
Michael Buesch5100d5a2008-03-29 21:01:16 +0100481 B43_SHM_SH_MCASTCOOKIE, cookie);
482 }
483
484 pack->skb = skb;
485 if (q->rev >= 8)
Michael Buesch88499ab2009-10-09 20:33:32 +0200486 pio_tx_frame_4byte_queue(pack, (const u8 *)txhdr, hdrlen);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100487 else
Michael Buesch88499ab2009-10-09 20:33:32 +0200488 pio_tx_frame_2byte_queue(pack, (const u8 *)txhdr, hdrlen);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100489
490 /* Remove it from the list of available packet slots.
491 * It will be put back when we receive the status report. */
492 list_del(&pack->list);
493
494 /* Update the queue statistics. */
495 q->buffer_used += roundup(skb->len + hdrlen, 4);
496 q->free_packet_slots -= 1;
497
498 return 0;
499}
500
Johannes Berge039fa42008-05-15 12:55:29 +0200501int b43_pio_tx(struct b43_wldev *dev, struct sk_buff *skb)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100502{
503 struct b43_pio_txqueue *q;
504 struct ieee80211_hdr *hdr;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100505 unsigned int hdrlen, total_len;
506 int err = 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200507 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100508
509 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200510
511 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100512 /* The multicast queue will be sent after the DTIM. */
513 q = dev->pio.tx_queue_mcast;
514 /* Set the frame More-Data bit. Ucode will clear it
515 * for us on the last frame. */
516 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
517 } else {
518 /* Decide by priority where to put this frame. */
Johannes Berge2530082008-05-17 00:57:14 +0200519 q = select_queue_by_priority(dev, skb_get_queue_mapping(skb));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100520 }
521
Michael Buesch5100d5a2008-03-29 21:01:16 +0100522 hdrlen = b43_txhdr_size(dev);
523 total_len = roundup(skb->len + hdrlen, 4);
524
525 if (unlikely(total_len > q->buffer_size)) {
526 err = -ENOBUFS;
527 b43dbg(dev->wl, "PIO: TX packet longer than queue.\n");
Michael Buesch637dae32009-09-04 22:55:00 +0200528 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100529 }
530 if (unlikely(q->free_packet_slots == 0)) {
531 err = -ENOBUFS;
532 b43warn(dev->wl, "PIO: TX packet overflow.\n");
Michael Buesch637dae32009-09-04 22:55:00 +0200533 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100534 }
535 B43_WARN_ON(q->buffer_used > q->buffer_size);
536
537 if (total_len > (q->buffer_size - q->buffer_used)) {
538 /* Not enough memory on the queue. */
539 err = -EBUSY;
Johannes Berge2530082008-05-17 00:57:14 +0200540 ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100541 q->stopped = 1;
Michael Buesch637dae32009-09-04 22:55:00 +0200542 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100543 }
544
545 /* Assign the queue number to the ring (if not already done before)
546 * so TX status handling can use it. The mac80211-queue to b43-queue
547 * mapping is static, so we don't need to store it per frame. */
Johannes Berge2530082008-05-17 00:57:14 +0200548 q->queue_prio = skb_get_queue_mapping(skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100549
Johannes Berge039fa42008-05-15 12:55:29 +0200550 err = pio_tx_frame(q, skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100551 if (unlikely(err == -ENOKEY)) {
552 /* Drop this packet, as we don't have the encryption key
553 * anymore and must not transmit it unencrypted. */
554 dev_kfree_skb_any(skb);
555 err = 0;
Michael Buesch637dae32009-09-04 22:55:00 +0200556 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100557 }
558 if (unlikely(err)) {
559 b43err(dev->wl, "PIO transmission failure\n");
Michael Buesch637dae32009-09-04 22:55:00 +0200560 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100561 }
562 q->nr_tx_packets++;
563
564 B43_WARN_ON(q->buffer_used > q->buffer_size);
565 if (((q->buffer_size - q->buffer_used) < roundup(2 + 2 + 6, 4)) ||
566 (q->free_packet_slots == 0)) {
567 /* The queue is full. */
Johannes Berge2530082008-05-17 00:57:14 +0200568 ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100569 q->stopped = 1;
570 }
571
Michael Buesch637dae32009-09-04 22:55:00 +0200572out:
Michael Buesch5100d5a2008-03-29 21:01:16 +0100573 return err;
574}
575
Michael Buesch5100d5a2008-03-29 21:01:16 +0100576void b43_pio_handle_txstatus(struct b43_wldev *dev,
577 const struct b43_txstatus *status)
578{
579 struct b43_pio_txqueue *q;
580 struct b43_pio_txpacket *pack = NULL;
581 unsigned int total_len;
Johannes Berge039fa42008-05-15 12:55:29 +0200582 struct ieee80211_tx_info *info;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100583
584 q = parse_cookie(dev, status->cookie, &pack);
585 if (unlikely(!q))
586 return;
587 B43_WARN_ON(!pack);
588
Michael Buesch14a7dd62008-06-24 12:22:05 +0200589 info = IEEE80211_SKB_CB(pack->skb);
Johannes Berge039fa42008-05-15 12:55:29 +0200590
Johannes Berge6a98542008-10-21 12:40:02 +0200591 b43_fill_txstatus_report(dev, info, status);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100592
593 total_len = pack->skb->len + b43_txhdr_size(dev);
594 total_len = roundup(total_len, 4);
595 q->buffer_used -= total_len;
596 q->free_packet_slots += 1;
597
Michael Bueschce6c4a12009-09-10 20:22:02 +0200598 ieee80211_tx_status(dev->wl->hw, pack->skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100599 pack->skb = NULL;
600 list_add(&pack->list, &q->packets_list);
601
602 if (q->stopped) {
603 ieee80211_wake_queue(dev->wl->hw, q->queue_prio);
604 q->stopped = 0;
605 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100606}
607
608void b43_pio_get_tx_stats(struct b43_wldev *dev,
609 struct ieee80211_tx_queue_stats *stats)
610{
611 const int nr_queues = dev->wl->hw->queues;
612 struct b43_pio_txqueue *q;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100613 int i;
614
615 for (i = 0; i < nr_queues; i++) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100616 q = select_queue_by_priority(dev, i);
617
Johannes Berg57ffc582008-04-29 17:18:59 +0200618 stats[i].len = B43_PIO_MAX_NR_TXPACKETS - q->free_packet_slots;
619 stats[i].limit = B43_PIO_MAX_NR_TXPACKETS;
620 stats[i].count = q->nr_tx_packets;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100621 }
622}
623
624/* Returns whether we should fetch another frame. */
625static bool pio_rx_frame(struct b43_pio_rxqueue *q)
626{
Michael Bueschd8c17e12008-04-02 19:58:20 +0200627 struct b43_wldev *dev = q->dev;
Albert Herranz7e937c62009-10-07 00:07:44 +0200628 struct b43_wl *wl = dev->wl;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100629 u16 len;
630 u32 macstat;
631 unsigned int i, padding;
632 struct sk_buff *skb;
633 const char *err_msg = NULL;
Michael Buesch88499ab2009-10-09 20:33:32 +0200634 struct b43_rxhdr_fw4 *rxhdr =
635 (struct b43_rxhdr_fw4 *)wl->pio_scratchspace;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100636
Michael Buesch88499ab2009-10-09 20:33:32 +0200637 BUILD_BUG_ON(sizeof(wl->pio_scratchspace) < sizeof(*rxhdr));
638 memset(rxhdr, 0, sizeof(*rxhdr));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100639
640 /* Check if we have data and wait for it to get ready. */
641 if (q->rev >= 8) {
642 u32 ctl;
643
644 ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
645 if (!(ctl & B43_PIO8_RXCTL_FRAMERDY))
646 return 0;
647 b43_piorx_write32(q, B43_PIO8_RXCTL,
648 B43_PIO8_RXCTL_FRAMERDY);
649 for (i = 0; i < 10; i++) {
650 ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
651 if (ctl & B43_PIO8_RXCTL_DATARDY)
652 goto data_ready;
653 udelay(10);
654 }
655 } else {
656 u16 ctl;
657
658 ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
659 if (!(ctl & B43_PIO_RXCTL_FRAMERDY))
660 return 0;
661 b43_piorx_write16(q, B43_PIO_RXCTL,
662 B43_PIO_RXCTL_FRAMERDY);
663 for (i = 0; i < 10; i++) {
664 ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
665 if (ctl & B43_PIO_RXCTL_DATARDY)
666 goto data_ready;
667 udelay(10);
668 }
669 }
670 b43dbg(q->dev->wl, "PIO RX timed out\n");
671 return 1;
672data_ready:
673
674 /* Get the preamble (RX header) */
675 if (q->rev >= 8) {
Michael Buesch88499ab2009-10-09 20:33:32 +0200676 ssb_block_read(dev->dev, rxhdr, sizeof(*rxhdr),
Michael Bueschd8c17e12008-04-02 19:58:20 +0200677 q->mmio_base + B43_PIO8_RXDATA,
678 sizeof(u32));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100679 } else {
Michael Buesch88499ab2009-10-09 20:33:32 +0200680 ssb_block_read(dev->dev, rxhdr, sizeof(*rxhdr),
Michael Bueschd8c17e12008-04-02 19:58:20 +0200681 q->mmio_base + B43_PIO_RXDATA,
682 sizeof(u16));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100683 }
684 /* Sanity checks. */
Michael Buesch88499ab2009-10-09 20:33:32 +0200685 len = le16_to_cpu(rxhdr->frame_len);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100686 if (unlikely(len > 0x700)) {
687 err_msg = "len > 0x700";
688 goto rx_error;
689 }
690 if (unlikely(len == 0)) {
691 err_msg = "len == 0";
692 goto rx_error;
693 }
694
Michael Buesch88499ab2009-10-09 20:33:32 +0200695 macstat = le32_to_cpu(rxhdr->mac_status);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100696 if (macstat & B43_RX_MAC_FCSERR) {
697 if (!(q->dev->wl->filter_flags & FIF_FCSFAIL)) {
698 /* Drop frames with failed FCS. */
699 err_msg = "Frame FCS error";
700 goto rx_error;
701 }
702 }
703
704 /* We always pad 2 bytes, as that's what upstream code expects
705 * due to the RX-header being 30 bytes. In case the frame is
706 * unaligned, we pad another 2 bytes. */
707 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
708 skb = dev_alloc_skb(len + padding + 2);
709 if (unlikely(!skb)) {
710 err_msg = "Out of memory";
711 goto rx_error;
712 }
713 skb_reserve(skb, 2);
714 skb_put(skb, len + padding);
715 if (q->rev >= 8) {
Michael Bueschd8c17e12008-04-02 19:58:20 +0200716 ssb_block_read(dev->dev, skb->data + padding, (len & ~3),
717 q->mmio_base + B43_PIO8_RXDATA,
718 sizeof(u32));
719 if (len & 3) {
Michael Buesch88499ab2009-10-09 20:33:32 +0200720 u8 *tail = wl->pio_tailspace;
721 BUILD_BUG_ON(sizeof(wl->pio_tailspace) < 4);
722
Michael Bueschd8c17e12008-04-02 19:58:20 +0200723 /* Read the last few bytes. */
Michael Buesch88499ab2009-10-09 20:33:32 +0200724 ssb_block_read(dev->dev, tail, 4,
Michael Bueschb96ab542009-09-23 18:51:21 +0200725 q->mmio_base + B43_PIO8_RXDATA,
726 sizeof(u32));
Michael Bueschd8c17e12008-04-02 19:58:20 +0200727 switch (len & 3) {
728 case 3:
Michael Buesch88499ab2009-10-09 20:33:32 +0200729 skb->data[len + padding - 3] = tail[0];
730 skb->data[len + padding - 2] = tail[1];
731 skb->data[len + padding - 1] = tail[2];
Michael Bueschb96ab542009-09-23 18:51:21 +0200732 break;
Michael Bueschd8c17e12008-04-02 19:58:20 +0200733 case 2:
Michael Buesch88499ab2009-10-09 20:33:32 +0200734 skb->data[len + padding - 2] = tail[0];
735 skb->data[len + padding - 1] = tail[1];
Michael Bueschb96ab542009-09-23 18:51:21 +0200736 break;
Michael Bueschd8c17e12008-04-02 19:58:20 +0200737 case 1:
Michael Buesch88499ab2009-10-09 20:33:32 +0200738 skb->data[len + padding - 1] = tail[0];
Michael Bueschb96ab542009-09-23 18:51:21 +0200739 break;
Michael Bueschd8c17e12008-04-02 19:58:20 +0200740 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100741 }
742 } else {
Michael Bueschd8c17e12008-04-02 19:58:20 +0200743 ssb_block_read(dev->dev, skb->data + padding, (len & ~1),
744 q->mmio_base + B43_PIO_RXDATA,
745 sizeof(u16));
746 if (len & 1) {
Michael Buesch88499ab2009-10-09 20:33:32 +0200747 u8 *tail = wl->pio_tailspace;
748 BUILD_BUG_ON(sizeof(wl->pio_tailspace) < 2);
749
Michael Bueschd8c17e12008-04-02 19:58:20 +0200750 /* Read the last byte. */
Michael Buesch88499ab2009-10-09 20:33:32 +0200751 ssb_block_read(dev->dev, tail, 2,
Michael Bueschb96ab542009-09-23 18:51:21 +0200752 q->mmio_base + B43_PIO_RXDATA,
753 sizeof(u16));
Michael Buesch88499ab2009-10-09 20:33:32 +0200754 skb->data[len + padding - 1] = tail[0];
Michael Buesch5100d5a2008-03-29 21:01:16 +0100755 }
756 }
757
Michael Buesch88499ab2009-10-09 20:33:32 +0200758 b43_rx(q->dev, skb, rxhdr);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100759
760 return 1;
761
762rx_error:
763 if (err_msg)
764 b43dbg(q->dev->wl, "PIO RX error: %s\n", err_msg);
Michael Bueschc2861812009-11-07 18:54:22 +0100765 if (q->rev >= 8)
766 b43_piorx_write32(q, B43_PIO8_RXCTL, B43_PIO8_RXCTL_DATARDY);
767 else
768 b43_piorx_write16(q, B43_PIO_RXCTL, B43_PIO_RXCTL_DATARDY);
769
Michael Buesch5100d5a2008-03-29 21:01:16 +0100770 return 1;
771}
772
Michael Buesch5100d5a2008-03-29 21:01:16 +0100773void b43_pio_rx(struct b43_pio_rxqueue *q)
774{
Michael Buesch77ca07f2009-09-04 22:56:19 +0200775 unsigned int count = 0;
776 bool stop;
777
778 while (1) {
779 stop = (pio_rx_frame(q) == 0);
780 if (stop)
781 break;
782 cond_resched();
783 if (WARN_ON_ONCE(++count > 10000))
784 break;
785 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100786}
787
788static void b43_pio_tx_suspend_queue(struct b43_pio_txqueue *q)
789{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100790 if (q->rev >= 8) {
791 b43_piotx_write32(q, B43_PIO8_TXCTL,
792 b43_piotx_read32(q, B43_PIO8_TXCTL)
793 | B43_PIO8_TXCTL_SUSPREQ);
794 } else {
795 b43_piotx_write16(q, B43_PIO_TXCTL,
796 b43_piotx_read16(q, B43_PIO_TXCTL)
797 | B43_PIO_TXCTL_SUSPREQ);
798 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100799}
800
801static void b43_pio_tx_resume_queue(struct b43_pio_txqueue *q)
802{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100803 if (q->rev >= 8) {
804 b43_piotx_write32(q, B43_PIO8_TXCTL,
805 b43_piotx_read32(q, B43_PIO8_TXCTL)
806 & ~B43_PIO8_TXCTL_SUSPREQ);
807 } else {
808 b43_piotx_write16(q, B43_PIO_TXCTL,
809 b43_piotx_read16(q, B43_PIO_TXCTL)
810 & ~B43_PIO_TXCTL_SUSPREQ);
811 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100812}
813
814void b43_pio_tx_suspend(struct b43_wldev *dev)
815{
816 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
817 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BK);
818 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BE);
819 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VI);
820 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VO);
821 b43_pio_tx_suspend_queue(dev->pio.tx_queue_mcast);
822}
823
824void b43_pio_tx_resume(struct b43_wldev *dev)
825{
826 b43_pio_tx_resume_queue(dev->pio.tx_queue_mcast);
827 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VO);
828 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VI);
829 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BE);
830 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BK);
831 b43_power_saving_ctl_bits(dev, 0);
832}