3 Broadcom B43legacy wireless driver
7 Copyright (c) 2005 Michael Buesch <mb@bu3sch.de>
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.
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.
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.
26 #include "b43legacy.h"
31 #include <linux/delay.h>
34 static void tx_start(struct b43legacy_pioqueue
*queue
)
36 b43legacy_pio_write(queue
, B43legacy_PIO_TXCTL
,
37 B43legacy_PIO_TXCTL_INIT
);
40 static void tx_octet(struct b43legacy_pioqueue
*queue
,
43 if (queue
->need_workarounds
) {
44 b43legacy_pio_write(queue
, B43legacy_PIO_TXDATA
, octet
);
45 b43legacy_pio_write(queue
, B43legacy_PIO_TXCTL
,
46 B43legacy_PIO_TXCTL_WRITELO
);
48 b43legacy_pio_write(queue
, B43legacy_PIO_TXCTL
,
49 B43legacy_PIO_TXCTL_WRITELO
);
50 b43legacy_pio_write(queue
, B43legacy_PIO_TXDATA
, octet
);
54 static u16
tx_get_next_word(const u8
*txhdr
,
60 unsigned int i
= *pos
;
69 ret
= le16_to_cpu(*((__le16
*)(source
+ i
)));
75 static void tx_data(struct b43legacy_pioqueue
*queue
,
83 if (queue
->need_workarounds
) {
84 data
= tx_get_next_word(txhdr
, packet
,
85 sizeof(struct b43legacy_txhdr_fw3
), &i
);
86 b43legacy_pio_write(queue
, B43legacy_PIO_TXDATA
, data
);
88 b43legacy_pio_write(queue
, B43legacy_PIO_TXCTL
,
89 B43legacy_PIO_TXCTL_WRITELO
|
90 B43legacy_PIO_TXCTL_WRITEHI
);
91 while (i
< octets
- 1) {
92 data
= tx_get_next_word(txhdr
, packet
,
93 sizeof(struct b43legacy_txhdr_fw3
), &i
);
94 b43legacy_pio_write(queue
, B43legacy_PIO_TXDATA
, data
);
97 tx_octet(queue
, packet
[octets
-
98 sizeof(struct b43legacy_txhdr_fw3
) - 1]);
101 static void tx_complete(struct b43legacy_pioqueue
*queue
,
104 if (queue
->need_workarounds
) {
105 b43legacy_pio_write(queue
, B43legacy_PIO_TXDATA
,
106 skb
->data
[skb
->len
- 1]);
107 b43legacy_pio_write(queue
, B43legacy_PIO_TXCTL
,
108 B43legacy_PIO_TXCTL_WRITELO
|
109 B43legacy_PIO_TXCTL_COMPLETE
);
111 b43legacy_pio_write(queue
, B43legacy_PIO_TXCTL
,
112 B43legacy_PIO_TXCTL_COMPLETE
);
115 static u16
generate_cookie(struct b43legacy_pioqueue
*queue
,
116 struct b43legacy_pio_txpacket
*packet
)
121 /* We use the upper 4 bits for the PIO
122 * controller ID and the lower 12 bits
123 * for the packet index (in the cache).
125 switch (queue
->mmio_base
) {
126 case B43legacy_MMIO_PIO1_BASE
:
128 case B43legacy_MMIO_PIO2_BASE
:
131 case B43legacy_MMIO_PIO3_BASE
:
134 case B43legacy_MMIO_PIO4_BASE
:
138 B43legacy_WARN_ON(1);
140 packetindex
= pio_txpacket_getindex(packet
);
141 B43legacy_WARN_ON(!(((u16
)packetindex
& 0xF000) == 0x0000));
142 cookie
|= (u16
)packetindex
;
148 struct b43legacy_pioqueue
*parse_cookie(struct b43legacy_wldev
*dev
,
150 struct b43legacy_pio_txpacket
**packet
)
152 struct b43legacy_pio
*pio
= &dev
->pio
;
153 struct b43legacy_pioqueue
*queue
= NULL
;
156 switch (cookie
& 0xF000) {
170 B43legacy_WARN_ON(1);
172 packetindex
= (cookie
& 0x0FFF);
173 B43legacy_WARN_ON(!(packetindex
>= 0 && packetindex
174 < B43legacy_PIO_MAXTXPACKETS
));
175 *packet
= &(queue
->tx_packets_cache
[packetindex
]);
181 struct b43legacy_txhdr_fw3 txhdr_fw3
;
184 static int pio_tx_write_fragment(struct b43legacy_pioqueue
*queue
,
186 struct b43legacy_pio_txpacket
*packet
,
189 union txhdr_union txhdr_data
;
194 txhdr
= (u8
*)(&txhdr_data
.txhdr_fw3
);
196 B43legacy_WARN_ON(skb_shinfo(skb
)->nr_frags
!= 0);
197 err
= b43legacy_generate_txhdr(queue
->dev
,
198 txhdr
, skb
->data
, skb
->len
,
199 IEEE80211_SKB_CB(skb
),
200 generate_cookie(queue
, packet
));
205 octets
= skb
->len
+ txhdr_size
;
206 if (queue
->need_workarounds
)
208 tx_data(queue
, txhdr
, (u8
*)skb
->data
, octets
);
209 tx_complete(queue
, skb
);
214 static void free_txpacket(struct b43legacy_pio_txpacket
*packet
,
217 struct b43legacy_pioqueue
*queue
= packet
->queue
;
221 dev_kfree_skb_irq(packet
->skb
);
223 dev_kfree_skb(packet
->skb
);
225 list_move(&packet
->list
, &queue
->txfree
);
229 static int pio_tx_packet(struct b43legacy_pio_txpacket
*packet
)
231 struct b43legacy_pioqueue
*queue
= packet
->queue
;
232 struct sk_buff
*skb
= packet
->skb
;
236 octets
= (u16
)skb
->len
+ sizeof(struct b43legacy_txhdr_fw3
);
237 if (queue
->tx_devq_size
< octets
) {
238 b43legacywarn(queue
->dev
->wl
, "PIO queue too small. "
239 "Dropping packet.\n");
240 /* Drop it silently (return success) */
241 free_txpacket(packet
, 1);
244 B43legacy_WARN_ON(queue
->tx_devq_packets
>
245 B43legacy_PIO_MAXTXDEVQPACKETS
);
246 B43legacy_WARN_ON(queue
->tx_devq_used
> queue
->tx_devq_size
);
247 /* Check if there is sufficient free space on the device
248 * TX queue. If not, return and let the TX tasklet
251 if (queue
->tx_devq_packets
== B43legacy_PIO_MAXTXDEVQPACKETS
)
253 if (queue
->tx_devq_used
+ octets
> queue
->tx_devq_size
)
255 /* Now poke the device. */
256 err
= pio_tx_write_fragment(queue
, skb
, packet
,
257 sizeof(struct b43legacy_txhdr_fw3
));
258 if (unlikely(err
== -ENOKEY
)) {
259 /* Drop this packet, as we don't have the encryption key
260 * anymore and must not transmit it unencrypted. */
261 free_txpacket(packet
, 1);
265 /* Account for the packet size.
266 * (We must not overflow the device TX queue)
268 queue
->tx_devq_packets
++;
269 queue
->tx_devq_used
+= octets
;
271 /* Transmission started, everything ok, move the
272 * packet to the txrunning list.
274 list_move_tail(&packet
->list
, &queue
->txrunning
);
279 static void tx_tasklet(unsigned long d
)
281 struct b43legacy_pioqueue
*queue
= (struct b43legacy_pioqueue
*)d
;
282 struct b43legacy_wldev
*dev
= queue
->dev
;
284 struct b43legacy_pio_txpacket
*packet
, *tmp_packet
;
288 spin_lock_irqsave(&dev
->wl
->irq_lock
, flags
);
289 if (queue
->tx_frozen
)
291 txctl
= b43legacy_pio_read(queue
, B43legacy_PIO_TXCTL
);
292 if (txctl
& B43legacy_PIO_TXCTL_SUSPEND
)
295 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txqueue
, list
) {
296 /* Try to transmit the packet. This can fail, if
297 * the device queue is full. In case of failure, the
298 * packet is left in the txqueue.
299 * If transmission succeed, the packet is moved to txrunning.
300 * If it is impossible to transmit the packet, it
303 err
= pio_tx_packet(packet
);
308 spin_unlock_irqrestore(&dev
->wl
->irq_lock
, flags
);
311 static void setup_txqueues(struct b43legacy_pioqueue
*queue
)
313 struct b43legacy_pio_txpacket
*packet
;
316 queue
->nr_txfree
= B43legacy_PIO_MAXTXPACKETS
;
317 for (i
= 0; i
< B43legacy_PIO_MAXTXPACKETS
; i
++) {
318 packet
= &(queue
->tx_packets_cache
[i
]);
320 packet
->queue
= queue
;
321 INIT_LIST_HEAD(&packet
->list
);
323 list_add(&packet
->list
, &queue
->txfree
);
328 struct b43legacy_pioqueue
*b43legacy_setup_pioqueue(struct b43legacy_wldev
*dev
,
331 struct b43legacy_pioqueue
*queue
;
335 queue
= kzalloc(sizeof(*queue
), GFP_KERNEL
);
340 queue
->mmio_base
= pio_mmio_base
;
341 queue
->need_workarounds
= (dev
->dev
->id
.revision
< 3);
343 INIT_LIST_HEAD(&queue
->txfree
);
344 INIT_LIST_HEAD(&queue
->txqueue
);
345 INIT_LIST_HEAD(&queue
->txrunning
);
346 tasklet_init(&queue
->txtask
, tx_tasklet
,
347 (unsigned long)queue
);
349 value
= b43legacy_read32(dev
, B43legacy_MMIO_MACCTL
);
350 value
&= ~B43legacy_MACCTL_BE
;
351 b43legacy_write32(dev
, B43legacy_MMIO_MACCTL
, value
);
353 qsize
= b43legacy_read16(dev
, queue
->mmio_base
354 + B43legacy_PIO_TXQBUFSIZE
);
356 b43legacyerr(dev
->wl
, "This card does not support PIO "
357 "operation mode. Please use DMA mode "
358 "(module parameter pio=0).\n");
361 if (qsize
<= B43legacy_PIO_TXQADJUST
) {
362 b43legacyerr(dev
->wl
, "PIO tx device-queue too small (%u)\n",
366 qsize
-= B43legacy_PIO_TXQADJUST
;
367 queue
->tx_devq_size
= qsize
;
369 setup_txqueues(queue
);
380 static void cancel_transfers(struct b43legacy_pioqueue
*queue
)
382 struct b43legacy_pio_txpacket
*packet
, *tmp_packet
;
384 tasklet_disable(&queue
->txtask
);
386 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txrunning
, list
)
387 free_txpacket(packet
, 0);
388 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txqueue
, list
)
389 free_txpacket(packet
, 0);
392 static void b43legacy_destroy_pioqueue(struct b43legacy_pioqueue
*queue
)
397 cancel_transfers(queue
);
401 void b43legacy_pio_free(struct b43legacy_wldev
*dev
)
403 struct b43legacy_pio
*pio
;
405 if (!b43legacy_using_pio(dev
))
409 b43legacy_destroy_pioqueue(pio
->queue3
);
411 b43legacy_destroy_pioqueue(pio
->queue2
);
413 b43legacy_destroy_pioqueue(pio
->queue1
);
415 b43legacy_destroy_pioqueue(pio
->queue0
);
419 int b43legacy_pio_init(struct b43legacy_wldev
*dev
)
421 struct b43legacy_pio
*pio
= &dev
->pio
;
422 struct b43legacy_pioqueue
*queue
;
425 queue
= b43legacy_setup_pioqueue(dev
, B43legacy_MMIO_PIO1_BASE
);
430 queue
= b43legacy_setup_pioqueue(dev
, B43legacy_MMIO_PIO2_BASE
);
435 queue
= b43legacy_setup_pioqueue(dev
, B43legacy_MMIO_PIO3_BASE
);
440 queue
= b43legacy_setup_pioqueue(dev
, B43legacy_MMIO_PIO4_BASE
);
445 if (dev
->dev
->id
.revision
< 3)
446 dev
->irq_mask
|= B43legacy_IRQ_PIO_WORKAROUND
;
448 b43legacydbg(dev
->wl
, "PIO initialized\n");
454 b43legacy_destroy_pioqueue(pio
->queue2
);
457 b43legacy_destroy_pioqueue(pio
->queue1
);
460 b43legacy_destroy_pioqueue(pio
->queue0
);
465 int b43legacy_pio_tx(struct b43legacy_wldev
*dev
,
468 struct b43legacy_pioqueue
*queue
= dev
->pio
.queue1
;
469 struct b43legacy_pio_txpacket
*packet
;
471 B43legacy_WARN_ON(queue
->tx_suspended
);
472 B43legacy_WARN_ON(list_empty(&queue
->txfree
));
474 packet
= list_entry(queue
->txfree
.next
, struct b43legacy_pio_txpacket
,
478 list_move_tail(&packet
->list
, &queue
->txqueue
);
480 queue
->nr_tx_packets
++;
481 B43legacy_WARN_ON(queue
->nr_txfree
>= B43legacy_PIO_MAXTXPACKETS
);
483 tasklet_schedule(&queue
->txtask
);
488 void b43legacy_pio_handle_txstatus(struct b43legacy_wldev
*dev
,
489 const struct b43legacy_txstatus
*status
)
491 struct b43legacy_pioqueue
*queue
;
492 struct b43legacy_pio_txpacket
*packet
;
493 struct ieee80211_tx_info
*info
;
496 queue
= parse_cookie(dev
, status
->cookie
, &packet
);
497 B43legacy_WARN_ON(!queue
);
502 queue
->tx_devq_packets
--;
503 queue
->tx_devq_used
-= (packet
->skb
->len
+
504 sizeof(struct b43legacy_txhdr_fw3
));
506 info
= IEEE80211_SKB_CB(packet
->skb
);
508 /* preserve the confiured retry limit before clearing the status
509 * The xmit function has overwritten the rc's value with the actual
510 * retry limit done by the hardware */
511 retry_limit
= info
->status
.rates
[0].count
;
512 ieee80211_tx_info_clear_status(info
);
515 info
->flags
|= IEEE80211_TX_STAT_ACK
;
517 if (status
->rts_count
> dev
->wl
->hw
->conf
.short_frame_max_tx_count
) {
519 * If the short retries (RTS, not data frame) have exceeded
520 * the limit, the hw will not have tried the selected rate,
521 * but will have used the fallback rate instead.
522 * Don't let the rate control count attempts for the selected
523 * rate in this case, otherwise the statistics will be off.
525 info
->status
.rates
[0].count
= 0;
526 info
->status
.rates
[1].count
= status
->frame_count
;
528 if (status
->frame_count
> retry_limit
) {
529 info
->status
.rates
[0].count
= retry_limit
;
530 info
->status
.rates
[1].count
= status
->frame_count
-
534 info
->status
.rates
[0].count
= status
->frame_count
;
535 info
->status
.rates
[1].idx
= -1;
538 ieee80211_tx_status_irqsafe(dev
->wl
->hw
, packet
->skb
);
541 free_txpacket(packet
, 1);
542 /* If there are packets on the txqueue, poke the tasklet
545 if (!list_empty(&queue
->txqueue
))
546 tasklet_schedule(&queue
->txtask
);
549 void b43legacy_pio_get_tx_stats(struct b43legacy_wldev
*dev
,
550 struct ieee80211_tx_queue_stats
*stats
)
552 struct b43legacy_pio
*pio
= &dev
->pio
;
553 struct b43legacy_pioqueue
*queue
;
556 stats
[0].len
= B43legacy_PIO_MAXTXPACKETS
- queue
->nr_txfree
;
557 stats
[0].limit
= B43legacy_PIO_MAXTXPACKETS
;
558 stats
[0].count
= queue
->nr_tx_packets
;
561 static void pio_rx_error(struct b43legacy_pioqueue
*queue
,
567 b43legacyerr(queue
->dev
->wl
, "PIO RX error: %s\n", error
);
568 b43legacy_pio_write(queue
, B43legacy_PIO_RXCTL
,
569 B43legacy_PIO_RXCTL_READY
);
571 B43legacy_WARN_ON(queue
->mmio_base
!= B43legacy_MMIO_PIO1_BASE
);
572 for (i
= 0; i
< 15; i
++) {
574 b43legacy_pio_read(queue
, B43legacy_PIO_RXDATA
);
579 void b43legacy_pio_rx(struct b43legacy_pioqueue
*queue
)
581 __le16 preamble
[21] = { 0 };
582 struct b43legacy_rxhdr_fw3
*rxhdr
;
587 int preamble_readwords
;
590 tmp
= b43legacy_pio_read(queue
, B43legacy_PIO_RXCTL
);
591 if (!(tmp
& B43legacy_PIO_RXCTL_DATAAVAILABLE
))
593 b43legacy_pio_write(queue
, B43legacy_PIO_RXCTL
,
594 B43legacy_PIO_RXCTL_DATAAVAILABLE
);
596 for (i
= 0; i
< 10; i
++) {
597 tmp
= b43legacy_pio_read(queue
, B43legacy_PIO_RXCTL
);
598 if (tmp
& B43legacy_PIO_RXCTL_READY
)
602 b43legacydbg(queue
->dev
->wl
, "PIO RX timed out\n");
606 len
= b43legacy_pio_read(queue
, B43legacy_PIO_RXDATA
);
607 if (unlikely(len
> 0x700)) {
608 pio_rx_error(queue
, 0, "len > 0x700");
611 if (unlikely(len
== 0 && queue
->mmio_base
!=
612 B43legacy_MMIO_PIO4_BASE
)) {
613 pio_rx_error(queue
, 0, "len == 0");
616 preamble
[0] = cpu_to_le16(len
);
617 if (queue
->mmio_base
== B43legacy_MMIO_PIO4_BASE
)
618 preamble_readwords
= 14 / sizeof(u16
);
620 preamble_readwords
= 18 / sizeof(u16
);
621 for (i
= 0; i
< preamble_readwords
; i
++) {
622 tmp
= b43legacy_pio_read(queue
, B43legacy_PIO_RXDATA
);
623 preamble
[i
+ 1] = cpu_to_le16(tmp
);
625 rxhdr
= (struct b43legacy_rxhdr_fw3
*)preamble
;
626 macstat
= le16_to_cpu(rxhdr
->mac_status
);
627 if (macstat
& B43legacy_RX_MAC_FCSERR
) {
629 (queue
->mmio_base
== B43legacy_MMIO_PIO1_BASE
),
633 if (queue
->mmio_base
== B43legacy_MMIO_PIO4_BASE
) {
634 /* We received an xmit status. */
635 struct b43legacy_hwtxstatus
*hw
;
637 hw
= (struct b43legacy_hwtxstatus
*)(preamble
+ 1);
638 b43legacy_handle_hwtxstatus(queue
->dev
, hw
);
643 skb
= dev_alloc_skb(len
);
644 if (unlikely(!skb
)) {
645 pio_rx_error(queue
, 1, "OOM");
649 for (i
= 0; i
< len
- 1; i
+= 2) {
650 tmp
= b43legacy_pio_read(queue
, B43legacy_PIO_RXDATA
);
651 *((__le16
*)(skb
->data
+ i
)) = cpu_to_le16(tmp
);
654 tmp
= b43legacy_pio_read(queue
, B43legacy_PIO_RXDATA
);
655 skb
->data
[len
- 1] = (tmp
& 0x00FF);
657 b43legacy_rx(queue
->dev
, skb
, rxhdr
);
660 void b43legacy_pio_tx_suspend(struct b43legacy_pioqueue
*queue
)
662 b43legacy_power_saving_ctl_bits(queue
->dev
, -1, 1);
663 b43legacy_pio_write(queue
, B43legacy_PIO_TXCTL
,
664 b43legacy_pio_read(queue
, B43legacy_PIO_TXCTL
)
665 | B43legacy_PIO_TXCTL_SUSPEND
);
668 void b43legacy_pio_tx_resume(struct b43legacy_pioqueue
*queue
)
670 b43legacy_pio_write(queue
, B43legacy_PIO_TXCTL
,
671 b43legacy_pio_read(queue
, B43legacy_PIO_TXCTL
)
672 & ~B43legacy_PIO_TXCTL_SUSPEND
);
673 b43legacy_power_saving_ctl_bits(queue
->dev
, -1, -1);
674 tasklet_schedule(&queue
->txtask
);
677 void b43legacy_pio_freeze_txqueues(struct b43legacy_wldev
*dev
)
679 struct b43legacy_pio
*pio
;
681 B43legacy_WARN_ON(!b43legacy_using_pio(dev
));
683 pio
->queue0
->tx_frozen
= 1;
684 pio
->queue1
->tx_frozen
= 1;
685 pio
->queue2
->tx_frozen
= 1;
686 pio
->queue3
->tx_frozen
= 1;
689 void b43legacy_pio_thaw_txqueues(struct b43legacy_wldev
*dev
)
691 struct b43legacy_pio
*pio
;
693 B43legacy_WARN_ON(!b43legacy_using_pio(dev
));
695 pio
->queue0
->tx_frozen
= 0;
696 pio
->queue1
->tx_frozen
= 0;
697 pio
->queue2
->tx_frozen
= 0;
698 pio
->queue3
->tx_frozen
= 0;
699 if (!list_empty(&pio
->queue0
->txqueue
))
700 tasklet_schedule(&pio
->queue0
->txtask
);
701 if (!list_empty(&pio
->queue1
->txqueue
))
702 tasklet_schedule(&pio
->queue1
->txtask
);
703 if (!list_empty(&pio
->queue2
->txqueue
))
704 tasklet_schedule(&pio
->queue2
->txtask
);
705 if (!list_empty(&pio
->queue3
->txqueue
))
706 tasklet_schedule(&pio
->queue3
->txtask
);