blob: 4635baa9b99812a45124d060089b37cfa4b9c20f [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>
33
34
35static void b43_pio_rx_work(struct work_struct *work);
36
37
38static u16 generate_cookie(struct b43_pio_txqueue *q,
39 struct b43_pio_txpacket *pack)
40{
41 u16 cookie;
42
43 /* Use the upper 4 bits of the cookie as
44 * PIO controller ID and store the packet index number
45 * in the lower 12 bits.
46 * Note that the cookie must never be 0, as this
47 * is a special value used in RX path.
48 * It can also not be 0xFFFF because that is special
49 * for multicast frames.
50 */
51 cookie = (((u16)q->index + 1) << 12);
52 cookie |= pack->index;
53
54 return cookie;
55}
56
57static
John Daiker99da1852009-02-24 02:16:42 -080058struct b43_pio_txqueue *parse_cookie(struct b43_wldev *dev,
59 u16 cookie,
Michael Buesch5100d5a2008-03-29 21:01:16 +010060 struct b43_pio_txpacket **pack)
61{
62 struct b43_pio *pio = &dev->pio;
63 struct b43_pio_txqueue *q = NULL;
64 unsigned int pack_index;
65
66 switch (cookie & 0xF000) {
67 case 0x1000:
68 q = pio->tx_queue_AC_BK;
69 break;
70 case 0x2000:
71 q = pio->tx_queue_AC_BE;
72 break;
73 case 0x3000:
74 q = pio->tx_queue_AC_VI;
75 break;
76 case 0x4000:
77 q = pio->tx_queue_AC_VO;
78 break;
79 case 0x5000:
80 q = pio->tx_queue_mcast;
81 break;
82 }
83 if (B43_WARN_ON(!q))
84 return NULL;
85 pack_index = (cookie & 0x0FFF);
86 if (B43_WARN_ON(pack_index >= ARRAY_SIZE(q->packets)))
87 return NULL;
88 *pack = &q->packets[pack_index];
89
90 return q;
91}
92
93static u16 index_to_pioqueue_base(struct b43_wldev *dev,
94 unsigned int index)
95{
96 static const u16 bases[] = {
97 B43_MMIO_PIO_BASE0,
98 B43_MMIO_PIO_BASE1,
99 B43_MMIO_PIO_BASE2,
100 B43_MMIO_PIO_BASE3,
101 B43_MMIO_PIO_BASE4,
102 B43_MMIO_PIO_BASE5,
103 B43_MMIO_PIO_BASE6,
104 B43_MMIO_PIO_BASE7,
105 };
106 static const u16 bases_rev11[] = {
107 B43_MMIO_PIO11_BASE0,
108 B43_MMIO_PIO11_BASE1,
109 B43_MMIO_PIO11_BASE2,
110 B43_MMIO_PIO11_BASE3,
111 B43_MMIO_PIO11_BASE4,
112 B43_MMIO_PIO11_BASE5,
113 };
114
115 if (dev->dev->id.revision >= 11) {
116 B43_WARN_ON(index >= ARRAY_SIZE(bases_rev11));
117 return bases_rev11[index];
118 }
119 B43_WARN_ON(index >= ARRAY_SIZE(bases));
120 return bases[index];
121}
122
123static u16 pio_txqueue_offset(struct b43_wldev *dev)
124{
125 if (dev->dev->id.revision >= 11)
126 return 0x18;
127 return 0;
128}
129
130static u16 pio_rxqueue_offset(struct b43_wldev *dev)
131{
132 if (dev->dev->id.revision >= 11)
133 return 0x38;
134 return 8;
135}
136
John Daiker99da1852009-02-24 02:16:42 -0800137static struct b43_pio_txqueue *b43_setup_pioqueue_tx(struct b43_wldev *dev,
138 unsigned int index)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100139{
140 struct b43_pio_txqueue *q;
141 struct b43_pio_txpacket *p;
142 unsigned int i;
143
144 q = kzalloc(sizeof(*q), GFP_KERNEL);
145 if (!q)
146 return NULL;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100147 q->dev = dev;
148 q->rev = dev->dev->id.revision;
149 q->mmio_base = index_to_pioqueue_base(dev, index) +
150 pio_txqueue_offset(dev);
151 q->index = index;
152
153 q->free_packet_slots = B43_PIO_MAX_NR_TXPACKETS;
154 if (q->rev >= 8) {
155 q->buffer_size = 1920; //FIXME this constant is wrong.
156 } else {
157 q->buffer_size = b43_piotx_read16(q, B43_PIO_TXQBUFSIZE);
158 q->buffer_size -= 80;
159 }
160
161 INIT_LIST_HEAD(&q->packets_list);
162 for (i = 0; i < ARRAY_SIZE(q->packets); i++) {
163 p = &(q->packets[i]);
164 INIT_LIST_HEAD(&p->list);
165 p->index = i;
166 p->queue = q;
167 list_add(&p->list, &q->packets_list);
168 }
169
170 return q;
171}
172
John Daiker99da1852009-02-24 02:16:42 -0800173static struct b43_pio_rxqueue *b43_setup_pioqueue_rx(struct b43_wldev *dev,
174 unsigned int index)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100175{
176 struct b43_pio_rxqueue *q;
177
178 q = kzalloc(sizeof(*q), GFP_KERNEL);
179 if (!q)
180 return NULL;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100181 q->dev = dev;
182 q->rev = dev->dev->id.revision;
183 q->mmio_base = index_to_pioqueue_base(dev, index) +
184 pio_rxqueue_offset(dev);
185 INIT_WORK(&q->rx_work, b43_pio_rx_work);
186
187 /* Enable Direct FIFO RX (PIO) on the engine. */
188 b43_dma_direct_fifo_rx(dev, index, 1);
189
190 return q;
191}
192
193static void b43_pio_cancel_tx_packets(struct b43_pio_txqueue *q)
194{
195 struct b43_pio_txpacket *pack;
196 unsigned int i;
197
198 for (i = 0; i < ARRAY_SIZE(q->packets); i++) {
199 pack = &(q->packets[i]);
200 if (pack->skb) {
201 dev_kfree_skb_any(pack->skb);
202 pack->skb = NULL;
203 }
204 }
205}
206
207static void b43_destroy_pioqueue_tx(struct b43_pio_txqueue *q,
208 const char *name)
209{
210 if (!q)
211 return;
212 b43_pio_cancel_tx_packets(q);
213 kfree(q);
214}
215
216static void b43_destroy_pioqueue_rx(struct b43_pio_rxqueue *q,
217 const char *name)
218{
219 if (!q)
220 return;
221 kfree(q);
222}
223
224#define destroy_queue_tx(pio, queue) do { \
225 b43_destroy_pioqueue_tx((pio)->queue, __stringify(queue)); \
226 (pio)->queue = NULL; \
227 } while (0)
228
229#define destroy_queue_rx(pio, queue) do { \
230 b43_destroy_pioqueue_rx((pio)->queue, __stringify(queue)); \
231 (pio)->queue = NULL; \
232 } while (0)
233
234void b43_pio_free(struct b43_wldev *dev)
235{
236 struct b43_pio *pio;
237
238 if (!b43_using_pio_transfers(dev))
239 return;
240 pio = &dev->pio;
241
242 destroy_queue_rx(pio, rx_queue);
243 destroy_queue_tx(pio, tx_queue_mcast);
244 destroy_queue_tx(pio, tx_queue_AC_VO);
245 destroy_queue_tx(pio, tx_queue_AC_VI);
246 destroy_queue_tx(pio, tx_queue_AC_BE);
247 destroy_queue_tx(pio, tx_queue_AC_BK);
248}
249
250void b43_pio_stop(struct b43_wldev *dev)
251{
252 if (!b43_using_pio_transfers(dev))
253 return;
254 cancel_work_sync(&dev->pio.rx_queue->rx_work);
255}
256
257int b43_pio_init(struct b43_wldev *dev)
258{
259 struct b43_pio *pio = &dev->pio;
260 int err = -ENOMEM;
261
262 b43_write32(dev, B43_MMIO_MACCTL, b43_read32(dev, B43_MMIO_MACCTL)
263 & ~B43_MACCTL_BE);
264 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_RXPADOFF, 0);
265
266 pio->tx_queue_AC_BK = b43_setup_pioqueue_tx(dev, 0);
267 if (!pio->tx_queue_AC_BK)
268 goto out;
269
270 pio->tx_queue_AC_BE = b43_setup_pioqueue_tx(dev, 1);
271 if (!pio->tx_queue_AC_BE)
272 goto err_destroy_bk;
273
274 pio->tx_queue_AC_VI = b43_setup_pioqueue_tx(dev, 2);
275 if (!pio->tx_queue_AC_VI)
276 goto err_destroy_be;
277
278 pio->tx_queue_AC_VO = b43_setup_pioqueue_tx(dev, 3);
279 if (!pio->tx_queue_AC_VO)
280 goto err_destroy_vi;
281
282 pio->tx_queue_mcast = b43_setup_pioqueue_tx(dev, 4);
283 if (!pio->tx_queue_mcast)
284 goto err_destroy_vo;
285
286 pio->rx_queue = b43_setup_pioqueue_rx(dev, 0);
287 if (!pio->rx_queue)
288 goto err_destroy_mcast;
289
290 b43dbg(dev->wl, "PIO initialized\n");
291 err = 0;
292out:
293 return err;
294
295err_destroy_mcast:
296 destroy_queue_tx(pio, tx_queue_mcast);
297err_destroy_vo:
298 destroy_queue_tx(pio, tx_queue_AC_VO);
299err_destroy_vi:
300 destroy_queue_tx(pio, tx_queue_AC_VI);
301err_destroy_be:
302 destroy_queue_tx(pio, tx_queue_AC_BE);
303err_destroy_bk:
304 destroy_queue_tx(pio, tx_queue_AC_BK);
305 return err;
306}
307
308/* Static mapping of mac80211's queues (priorities) to b43 PIO queues. */
John Daiker99da1852009-02-24 02:16:42 -0800309static struct b43_pio_txqueue *select_queue_by_priority(struct b43_wldev *dev,
310 u8 queue_prio)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100311{
312 struct b43_pio_txqueue *q;
313
Michael Buesch403a3a12009-06-08 21:04:57 +0200314 if (dev->qos_enabled) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100315 /* 0 = highest priority */
316 switch (queue_prio) {
317 default:
318 B43_WARN_ON(1);
319 /* fallthrough */
320 case 0:
321 q = dev->pio.tx_queue_AC_VO;
322 break;
323 case 1:
324 q = dev->pio.tx_queue_AC_VI;
325 break;
326 case 2:
327 q = dev->pio.tx_queue_AC_BE;
328 break;
329 case 3:
330 q = dev->pio.tx_queue_AC_BK;
331 break;
332 }
333 } else
334 q = dev->pio.tx_queue_AC_BE;
335
336 return q;
337}
338
Michael Bueschd8c17e12008-04-02 19:58:20 +0200339static u16 tx_write_2byte_queue(struct b43_pio_txqueue *q,
340 u16 ctl,
341 const void *_data,
342 unsigned int data_len)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100343{
Michael Bueschd8c17e12008-04-02 19:58:20 +0200344 struct b43_wldev *dev = q->dev;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100345 const u8 *data = _data;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100346
Michael Bueschd8c17e12008-04-02 19:58:20 +0200347 ctl |= B43_PIO_TXCTL_WRITELO | B43_PIO_TXCTL_WRITEHI;
348 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
349
350 ssb_block_write(dev->dev, data, (data_len & ~1),
351 q->mmio_base + B43_PIO_TXDATA,
352 sizeof(u16));
353 if (data_len & 1) {
354 /* Write the last byte. */
355 ctl &= ~B43_PIO_TXCTL_WRITEHI;
356 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
357 b43_piotx_write16(q, B43_PIO_TXDATA, data[data_len - 1]);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100358 }
Michael Bueschd8c17e12008-04-02 19:58:20 +0200359
360 return ctl;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100361}
362
363static void pio_tx_frame_2byte_queue(struct b43_pio_txpacket *pack,
364 const u8 *hdr, unsigned int hdrlen)
365{
366 struct b43_pio_txqueue *q = pack->queue;
367 const char *frame = pack->skb->data;
368 unsigned int frame_len = pack->skb->len;
369 u16 ctl;
370
371 ctl = b43_piotx_read16(q, B43_PIO_TXCTL);
372 ctl |= B43_PIO_TXCTL_FREADY;
373 ctl &= ~B43_PIO_TXCTL_EOF;
374
375 /* Transfer the header data. */
Michael Bueschd8c17e12008-04-02 19:58:20 +0200376 ctl = tx_write_2byte_queue(q, ctl, hdr, hdrlen);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100377 /* Transfer the frame data. */
Michael Bueschd8c17e12008-04-02 19:58:20 +0200378 ctl = tx_write_2byte_queue(q, ctl, frame, frame_len);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100379
380 ctl |= B43_PIO_TXCTL_EOF;
381 b43_piotx_write16(q, B43_PIO_TXCTL, ctl);
382}
383
Michael Bueschd8c17e12008-04-02 19:58:20 +0200384static u32 tx_write_4byte_queue(struct b43_pio_txqueue *q,
385 u32 ctl,
386 const void *_data,
387 unsigned int data_len)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100388{
Michael Bueschd8c17e12008-04-02 19:58:20 +0200389 struct b43_wldev *dev = q->dev;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100390 const u8 *data = _data;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100391
Michael Bueschd8c17e12008-04-02 19:58:20 +0200392 ctl |= B43_PIO8_TXCTL_0_7 | B43_PIO8_TXCTL_8_15 |
393 B43_PIO8_TXCTL_16_23 | B43_PIO8_TXCTL_24_31;
394 b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
395
396 ssb_block_write(dev->dev, data, (data_len & ~3),
397 q->mmio_base + B43_PIO8_TXDATA,
398 sizeof(u32));
399 if (data_len & 3) {
400 u32 value = 0;
401
402 /* Write the last few bytes. */
403 ctl &= ~(B43_PIO8_TXCTL_8_15 | B43_PIO8_TXCTL_16_23 |
404 B43_PIO8_TXCTL_24_31);
405 data = &(data[data_len - 1]);
406 switch (data_len & 3) {
407 case 3:
408 ctl |= B43_PIO8_TXCTL_16_23;
409 value |= (u32)(*data) << 16;
410 data--;
411 case 2:
412 ctl |= B43_PIO8_TXCTL_8_15;
413 value |= (u32)(*data) << 8;
414 data--;
415 case 1:
416 value |= (u32)(*data);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100417 }
Michael Bueschd8c17e12008-04-02 19:58:20 +0200418 b43_piotx_write32(q, B43_PIO8_TXCTL, ctl);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100419 b43_piotx_write32(q, B43_PIO8_TXDATA, value);
420 }
Michael Bueschd8c17e12008-04-02 19:58:20 +0200421
422 return ctl;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100423}
424
425static void pio_tx_frame_4byte_queue(struct b43_pio_txpacket *pack,
426 const u8 *hdr, unsigned int hdrlen)
427{
428 struct b43_pio_txqueue *q = pack->queue;
429 const char *frame = pack->skb->data;
430 unsigned int frame_len = pack->skb->len;
431 u32 ctl;
432
433 ctl = b43_piotx_read32(q, B43_PIO8_TXCTL);
434 ctl |= B43_PIO8_TXCTL_FREADY;
435 ctl &= ~B43_PIO8_TXCTL_EOF;
436
437 /* Transfer the header data. */
Michael Bueschd8c17e12008-04-02 19:58:20 +0200438 ctl = tx_write_4byte_queue(q, ctl, hdr, hdrlen);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100439 /* Transfer the frame data. */
Michael Bueschd8c17e12008-04-02 19:58:20 +0200440 ctl = tx_write_4byte_queue(q, ctl, frame, frame_len);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100441
442 ctl |= B43_PIO8_TXCTL_EOF;
443 b43_piotx_write32(q, B43_PIO_TXCTL, ctl);
444}
445
446static int pio_tx_frame(struct b43_pio_txqueue *q,
Johannes Berge039fa42008-05-15 12:55:29 +0200447 struct sk_buff *skb)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100448{
449 struct b43_pio_txpacket *pack;
450 struct b43_txhdr txhdr;
451 u16 cookie;
452 int err;
453 unsigned int hdrlen;
Johannes Berge039fa42008-05-15 12:55:29 +0200454 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100455
456 B43_WARN_ON(list_empty(&q->packets_list));
457 pack = list_entry(q->packets_list.next,
458 struct b43_pio_txpacket, list);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100459
460 cookie = generate_cookie(q, pack);
461 hdrlen = b43_txhdr_size(q->dev);
gregor kowski035d0242009-08-19 22:35:45 +0200462 err = b43_generate_txhdr(q->dev, (u8 *)&txhdr, skb,
463 info, cookie);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100464 if (err)
465 return err;
466
Johannes Berge039fa42008-05-15 12:55:29 +0200467 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100468 /* Tell the firmware about the cookie of the last
469 * mcast frame, so it can clear the more-data bit in it. */
470 b43_shm_write16(q->dev, B43_SHM_SHARED,
471 B43_SHM_SH_MCASTCOOKIE, cookie);
472 }
473
474 pack->skb = skb;
475 if (q->rev >= 8)
476 pio_tx_frame_4byte_queue(pack, (const u8 *)&txhdr, hdrlen);
477 else
478 pio_tx_frame_2byte_queue(pack, (const u8 *)&txhdr, hdrlen);
479
480 /* Remove it from the list of available packet slots.
481 * It will be put back when we receive the status report. */
482 list_del(&pack->list);
483
484 /* Update the queue statistics. */
485 q->buffer_used += roundup(skb->len + hdrlen, 4);
486 q->free_packet_slots -= 1;
487
488 return 0;
489}
490
Johannes Berge039fa42008-05-15 12:55:29 +0200491int b43_pio_tx(struct b43_wldev *dev, struct sk_buff *skb)
Michael Buesch5100d5a2008-03-29 21:01:16 +0100492{
493 struct b43_pio_txqueue *q;
494 struct ieee80211_hdr *hdr;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100495 unsigned int hdrlen, total_len;
496 int err = 0;
Johannes Berge039fa42008-05-15 12:55:29 +0200497 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100498
499 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200500
501 if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100502 /* The multicast queue will be sent after the DTIM. */
503 q = dev->pio.tx_queue_mcast;
504 /* Set the frame More-Data bit. Ucode will clear it
505 * for us on the last frame. */
506 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREDATA);
507 } else {
508 /* Decide by priority where to put this frame. */
Johannes Berge2530082008-05-17 00:57:14 +0200509 q = select_queue_by_priority(dev, skb_get_queue_mapping(skb));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100510 }
511
Michael Buesch5100d5a2008-03-29 21:01:16 +0100512 hdrlen = b43_txhdr_size(dev);
513 total_len = roundup(skb->len + hdrlen, 4);
514
515 if (unlikely(total_len > q->buffer_size)) {
516 err = -ENOBUFS;
517 b43dbg(dev->wl, "PIO: TX packet longer than queue.\n");
Michael Buesch637dae32009-09-04 22:55:00 +0200518 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100519 }
520 if (unlikely(q->free_packet_slots == 0)) {
521 err = -ENOBUFS;
522 b43warn(dev->wl, "PIO: TX packet overflow.\n");
Michael Buesch637dae32009-09-04 22:55:00 +0200523 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100524 }
525 B43_WARN_ON(q->buffer_used > q->buffer_size);
526
527 if (total_len > (q->buffer_size - q->buffer_used)) {
528 /* Not enough memory on the queue. */
529 err = -EBUSY;
Johannes Berge2530082008-05-17 00:57:14 +0200530 ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100531 q->stopped = 1;
Michael Buesch637dae32009-09-04 22:55:00 +0200532 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100533 }
534
535 /* Assign the queue number to the ring (if not already done before)
536 * so TX status handling can use it. The mac80211-queue to b43-queue
537 * mapping is static, so we don't need to store it per frame. */
Johannes Berge2530082008-05-17 00:57:14 +0200538 q->queue_prio = skb_get_queue_mapping(skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100539
Johannes Berge039fa42008-05-15 12:55:29 +0200540 err = pio_tx_frame(q, skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100541 if (unlikely(err == -ENOKEY)) {
542 /* Drop this packet, as we don't have the encryption key
543 * anymore and must not transmit it unencrypted. */
544 dev_kfree_skb_any(skb);
545 err = 0;
Michael Buesch637dae32009-09-04 22:55:00 +0200546 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100547 }
548 if (unlikely(err)) {
549 b43err(dev->wl, "PIO transmission failure\n");
Michael Buesch637dae32009-09-04 22:55:00 +0200550 goto out;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100551 }
552 q->nr_tx_packets++;
553
554 B43_WARN_ON(q->buffer_used > q->buffer_size);
555 if (((q->buffer_size - q->buffer_used) < roundup(2 + 2 + 6, 4)) ||
556 (q->free_packet_slots == 0)) {
557 /* The queue is full. */
Johannes Berge2530082008-05-17 00:57:14 +0200558 ieee80211_stop_queue(dev->wl->hw, skb_get_queue_mapping(skb));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100559 q->stopped = 1;
560 }
561
Michael Buesch637dae32009-09-04 22:55:00 +0200562out:
Michael Buesch5100d5a2008-03-29 21:01:16 +0100563 return err;
564}
565
Michael Buesch5100d5a2008-03-29 21:01:16 +0100566void b43_pio_handle_txstatus(struct b43_wldev *dev,
567 const struct b43_txstatus *status)
568{
569 struct b43_pio_txqueue *q;
570 struct b43_pio_txpacket *pack = NULL;
571 unsigned int total_len;
Johannes Berge039fa42008-05-15 12:55:29 +0200572 struct ieee80211_tx_info *info;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100573
574 q = parse_cookie(dev, status->cookie, &pack);
575 if (unlikely(!q))
576 return;
577 B43_WARN_ON(!pack);
578
Michael Buesch14a7dd62008-06-24 12:22:05 +0200579 info = IEEE80211_SKB_CB(pack->skb);
Johannes Berge039fa42008-05-15 12:55:29 +0200580
Johannes Berge6a98542008-10-21 12:40:02 +0200581 b43_fill_txstatus_report(dev, info, status);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100582
583 total_len = pack->skb->len + b43_txhdr_size(dev);
584 total_len = roundup(total_len, 4);
585 q->buffer_used -= total_len;
586 q->free_packet_slots += 1;
587
Johannes Berge039fa42008-05-15 12:55:29 +0200588 ieee80211_tx_status_irqsafe(dev->wl->hw, pack->skb);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100589 pack->skb = NULL;
590 list_add(&pack->list, &q->packets_list);
591
592 if (q->stopped) {
593 ieee80211_wake_queue(dev->wl->hw, q->queue_prio);
594 q->stopped = 0;
595 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100596}
597
598void b43_pio_get_tx_stats(struct b43_wldev *dev,
599 struct ieee80211_tx_queue_stats *stats)
600{
601 const int nr_queues = dev->wl->hw->queues;
602 struct b43_pio_txqueue *q;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100603 int i;
604
605 for (i = 0; i < nr_queues; i++) {
Michael Buesch5100d5a2008-03-29 21:01:16 +0100606 q = select_queue_by_priority(dev, i);
607
Johannes Berg57ffc582008-04-29 17:18:59 +0200608 stats[i].len = B43_PIO_MAX_NR_TXPACKETS - q->free_packet_slots;
609 stats[i].limit = B43_PIO_MAX_NR_TXPACKETS;
610 stats[i].count = q->nr_tx_packets;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100611 }
612}
613
614/* Returns whether we should fetch another frame. */
615static bool pio_rx_frame(struct b43_pio_rxqueue *q)
616{
Michael Bueschd8c17e12008-04-02 19:58:20 +0200617 struct b43_wldev *dev = q->dev;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100618 struct b43_rxhdr_fw4 rxhdr;
619 u16 len;
620 u32 macstat;
621 unsigned int i, padding;
622 struct sk_buff *skb;
623 const char *err_msg = NULL;
624
625 memset(&rxhdr, 0, sizeof(rxhdr));
626
627 /* Check if we have data and wait for it to get ready. */
628 if (q->rev >= 8) {
629 u32 ctl;
630
631 ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
632 if (!(ctl & B43_PIO8_RXCTL_FRAMERDY))
633 return 0;
634 b43_piorx_write32(q, B43_PIO8_RXCTL,
635 B43_PIO8_RXCTL_FRAMERDY);
636 for (i = 0; i < 10; i++) {
637 ctl = b43_piorx_read32(q, B43_PIO8_RXCTL);
638 if (ctl & B43_PIO8_RXCTL_DATARDY)
639 goto data_ready;
640 udelay(10);
641 }
642 } else {
643 u16 ctl;
644
645 ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
646 if (!(ctl & B43_PIO_RXCTL_FRAMERDY))
647 return 0;
648 b43_piorx_write16(q, B43_PIO_RXCTL,
649 B43_PIO_RXCTL_FRAMERDY);
650 for (i = 0; i < 10; i++) {
651 ctl = b43_piorx_read16(q, B43_PIO_RXCTL);
652 if (ctl & B43_PIO_RXCTL_DATARDY)
653 goto data_ready;
654 udelay(10);
655 }
656 }
657 b43dbg(q->dev->wl, "PIO RX timed out\n");
658 return 1;
659data_ready:
660
661 /* Get the preamble (RX header) */
662 if (q->rev >= 8) {
Michael Bueschd8c17e12008-04-02 19:58:20 +0200663 ssb_block_read(dev->dev, &rxhdr, sizeof(rxhdr),
664 q->mmio_base + B43_PIO8_RXDATA,
665 sizeof(u32));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100666 } else {
Michael Bueschd8c17e12008-04-02 19:58:20 +0200667 ssb_block_read(dev->dev, &rxhdr, sizeof(rxhdr),
668 q->mmio_base + B43_PIO_RXDATA,
669 sizeof(u16));
Michael Buesch5100d5a2008-03-29 21:01:16 +0100670 }
671 /* Sanity checks. */
672 len = le16_to_cpu(rxhdr.frame_len);
673 if (unlikely(len > 0x700)) {
674 err_msg = "len > 0x700";
675 goto rx_error;
676 }
677 if (unlikely(len == 0)) {
678 err_msg = "len == 0";
679 goto rx_error;
680 }
681
682 macstat = le32_to_cpu(rxhdr.mac_status);
683 if (macstat & B43_RX_MAC_FCSERR) {
684 if (!(q->dev->wl->filter_flags & FIF_FCSFAIL)) {
685 /* Drop frames with failed FCS. */
686 err_msg = "Frame FCS error";
687 goto rx_error;
688 }
689 }
690
691 /* We always pad 2 bytes, as that's what upstream code expects
692 * due to the RX-header being 30 bytes. In case the frame is
693 * unaligned, we pad another 2 bytes. */
694 padding = (macstat & B43_RX_MAC_PADDING) ? 2 : 0;
695 skb = dev_alloc_skb(len + padding + 2);
696 if (unlikely(!skb)) {
697 err_msg = "Out of memory";
698 goto rx_error;
699 }
700 skb_reserve(skb, 2);
701 skb_put(skb, len + padding);
702 if (q->rev >= 8) {
Michael Bueschd8c17e12008-04-02 19:58:20 +0200703 ssb_block_read(dev->dev, skb->data + padding, (len & ~3),
704 q->mmio_base + B43_PIO8_RXDATA,
705 sizeof(u32));
706 if (len & 3) {
707 u32 value;
708 char *data;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100709
Michael Bueschd8c17e12008-04-02 19:58:20 +0200710 /* Read the last few bytes. */
Michael Buesch5100d5a2008-03-29 21:01:16 +0100711 value = b43_piorx_read32(q, B43_PIO8_RXDATA);
Michael Bueschd8c17e12008-04-02 19:58:20 +0200712 data = &(skb->data[len + padding - 1]);
713 switch (len & 3) {
714 case 3:
715 *data = (value >> 16);
716 data--;
717 case 2:
718 *data = (value >> 8);
719 data--;
720 case 1:
721 *data = value;
722 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100723 }
724 } else {
Michael Bueschd8c17e12008-04-02 19:58:20 +0200725 ssb_block_read(dev->dev, skb->data + padding, (len & ~1),
726 q->mmio_base + B43_PIO_RXDATA,
727 sizeof(u16));
728 if (len & 1) {
729 u16 value;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100730
Michael Bueschd8c17e12008-04-02 19:58:20 +0200731 /* Read the last byte. */
Michael Buesch5100d5a2008-03-29 21:01:16 +0100732 value = b43_piorx_read16(q, B43_PIO_RXDATA);
Michael Bueschd8c17e12008-04-02 19:58:20 +0200733 skb->data[len + padding - 1] = value;
Michael Buesch5100d5a2008-03-29 21:01:16 +0100734 }
735 }
736
737 b43_rx(q->dev, skb, &rxhdr);
738
739 return 1;
740
741rx_error:
742 if (err_msg)
743 b43dbg(q->dev->wl, "PIO RX error: %s\n", err_msg);
744 b43_piorx_write16(q, B43_PIO_RXCTL, B43_PIO_RXCTL_DATARDY);
745 return 1;
746}
747
748/* RX workqueue. We can sleep, yay! */
749static void b43_pio_rx_work(struct work_struct *work)
750{
751 struct b43_pio_rxqueue *q = container_of(work, struct b43_pio_rxqueue,
752 rx_work);
753 unsigned int budget = 50;
754 bool stop;
755
756 do {
Michael Buesch637dae32009-09-04 22:55:00 +0200757 mutex_lock(&q->dev->wl->mutex);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100758 stop = (pio_rx_frame(q) == 0);
Michael Buesch637dae32009-09-04 22:55:00 +0200759 mutex_unlock(&q->dev->wl->mutex);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100760 cond_resched();
761 if (stop)
762 break;
763 } while (--budget);
764}
765
766/* Called with IRQs disabled. */
767void b43_pio_rx(struct b43_pio_rxqueue *q)
768{
769 /* Due to latency issues we must run the RX path in
770 * a workqueue to be able to schedule between packets. */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400771 ieee80211_queue_work(q->dev->wl->hw, &q->rx_work);
Michael Buesch5100d5a2008-03-29 21:01:16 +0100772}
773
774static void b43_pio_tx_suspend_queue(struct b43_pio_txqueue *q)
775{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100776 if (q->rev >= 8) {
777 b43_piotx_write32(q, B43_PIO8_TXCTL,
778 b43_piotx_read32(q, B43_PIO8_TXCTL)
779 | B43_PIO8_TXCTL_SUSPREQ);
780 } else {
781 b43_piotx_write16(q, B43_PIO_TXCTL,
782 b43_piotx_read16(q, B43_PIO_TXCTL)
783 | B43_PIO_TXCTL_SUSPREQ);
784 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100785}
786
787static void b43_pio_tx_resume_queue(struct b43_pio_txqueue *q)
788{
Michael Buesch5100d5a2008-03-29 21:01:16 +0100789 if (q->rev >= 8) {
790 b43_piotx_write32(q, B43_PIO8_TXCTL,
791 b43_piotx_read32(q, B43_PIO8_TXCTL)
792 & ~B43_PIO8_TXCTL_SUSPREQ);
793 } else {
794 b43_piotx_write16(q, B43_PIO_TXCTL,
795 b43_piotx_read16(q, B43_PIO_TXCTL)
796 & ~B43_PIO_TXCTL_SUSPREQ);
797 }
Michael Buesch5100d5a2008-03-29 21:01:16 +0100798}
799
800void b43_pio_tx_suspend(struct b43_wldev *dev)
801{
802 b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
803 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BK);
804 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_BE);
805 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VI);
806 b43_pio_tx_suspend_queue(dev->pio.tx_queue_AC_VO);
807 b43_pio_tx_suspend_queue(dev->pio.tx_queue_mcast);
808}
809
810void b43_pio_tx_resume(struct b43_wldev *dev)
811{
812 b43_pio_tx_resume_queue(dev->pio.tx_queue_mcast);
813 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VO);
814 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_VI);
815 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BE);
816 b43_pio_tx_resume_queue(dev->pio.tx_queue_AC_BK);
817 b43_power_saving_ctl_bits(dev, 0);
818}