3 Broadcom BCM43xx 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.
27 #include "bcm43xx_pio.h"
28 #include "bcm43xx_main.h"
29 #include "bcm43xx_xmit.h"
30 #include "bcm43xx_power.h"
32 #include <linux/delay.h>
35 static void tx_start(struct bcm43xx_pioqueue
*queue
)
37 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXCTL
,
38 BCM43xx_PIO_TXCTL_INIT
);
41 static void tx_octet(struct bcm43xx_pioqueue
*queue
,
44 if (queue
->need_workarounds
) {
45 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXDATA
,
47 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXCTL
,
48 BCM43xx_PIO_TXCTL_WRITELO
);
50 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXCTL
,
51 BCM43xx_PIO_TXCTL_WRITELO
);
52 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXDATA
,
57 static u16
tx_get_next_word(const u8
*txhdr
,
63 unsigned int i
= *pos
;
72 ret
= le16_to_cpu( *((u16
*)(source
+ i
)) );
78 static void tx_data(struct bcm43xx_pioqueue
*queue
,
86 if (queue
->need_workarounds
) {
87 data
= tx_get_next_word(txhdr
, packet
,
88 sizeof(struct bcm43xx_txhdr_fw4
), &i
);
89 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXDATA
, data
);
91 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXCTL
,
92 BCM43xx_PIO_TXCTL_WRITELO
|
93 BCM43xx_PIO_TXCTL_WRITEHI
);
94 while (i
< octets
- 1) {
95 data
= tx_get_next_word(txhdr
, packet
,
96 sizeof(struct bcm43xx_txhdr_fw4
), &i
);
97 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXDATA
, data
);
100 tx_octet(queue
, packet
[octets
- sizeof(struct bcm43xx_txhdr_fw4
) - 1]);
103 static void tx_complete(struct bcm43xx_pioqueue
*queue
,
106 if (queue
->need_workarounds
) {
107 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXDATA
,
108 skb
->data
[skb
->len
- 1]);
109 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXCTL
,
110 BCM43xx_PIO_TXCTL_WRITELO
|
111 BCM43xx_PIO_TXCTL_COMPLETE
);
113 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXCTL
,
114 BCM43xx_PIO_TXCTL_COMPLETE
);
118 static u16
generate_cookie(struct bcm43xx_pioqueue
*queue
,
119 struct bcm43xx_pio_txpacket
*packet
)
124 /* We use the upper 4 bits for the PIO
125 * controller ID and the lower 12 bits
126 * for the packet index (in the cache).
128 switch (queue
->mmio_base
) {
129 case BCM43xx_MMIO_PIO1_BASE
:
131 case BCM43xx_MMIO_PIO2_BASE
:
134 case BCM43xx_MMIO_PIO3_BASE
:
137 case BCM43xx_MMIO_PIO4_BASE
:
143 packetindex
= pio_txpacket_getindex(packet
);
144 assert(((u16
)packetindex
& 0xF000) == 0x0000);
145 cookie
|= (u16
)packetindex
;
151 struct bcm43xx_pioqueue
* parse_cookie(struct bcm43xx_wldev
*dev
,
153 struct bcm43xx_pio_txpacket
**packet
)
155 struct bcm43xx_pio
*pio
= &dev
->pio
;
156 struct bcm43xx_pioqueue
*queue
= NULL
;
159 switch (cookie
& 0xF000) {
175 packetindex
= (cookie
& 0x0FFF);
176 assert(packetindex
>= 0 && packetindex
< BCM43xx_PIO_MAXTXPACKETS
);
177 *packet
= &(queue
->tx_packets_cache
[packetindex
]);
183 struct bcm43xx_txhdr_fw4 txhdr_fw4
;
186 static void pio_tx_write_fragment(struct bcm43xx_pioqueue
*queue
,
188 struct bcm43xx_pio_txpacket
*packet
,
191 union txhdr_union txhdr_data
;
195 txhdr
= (u8
*)(&txhdr_data
.txhdr_fw4
);
197 assert(skb_shinfo(skb
)->nr_frags
== 0);
198 bcm43xx_generate_txhdr(queue
->dev
,
199 txhdr
, skb
->data
, skb
->len
,
200 &packet
->txstat
.control
,
201 generate_cookie(queue
, packet
));
204 octets
= skb
->len
+ txhdr_size
;
205 if (queue
->need_workarounds
)
207 tx_data(queue
, txhdr
, (u8
*)skb
->data
, octets
);
208 tx_complete(queue
, skb
);
211 static void free_txpacket(struct bcm43xx_pio_txpacket
*packet
,
214 struct bcm43xx_pioqueue
*queue
= packet
->queue
;
218 dev_kfree_skb_irq(packet
->skb
);
220 dev_kfree_skb(packet
->skb
);
222 list_move(&packet
->list
, &queue
->txfree
);
226 static int pio_tx_packet(struct bcm43xx_pio_txpacket
*packet
)
228 struct bcm43xx_pioqueue
*queue
= packet
->queue
;
229 struct sk_buff
*skb
= packet
->skb
;
232 octets
= (u16
)skb
->len
+ sizeof(struct bcm43xx_txhdr_fw4
);
233 if (queue
->tx_devq_size
< octets
) {
234 printkl(KERN_WARNING PFX
"PIO queue too small. "
235 "Dropping packet.\n");
236 /* Drop it silently (return success) */
237 free_txpacket(packet
, 1);
240 assert(queue
->tx_devq_packets
<= BCM43xx_PIO_MAXTXDEVQPACKETS
);
241 assert(queue
->tx_devq_used
<= queue
->tx_devq_size
);
242 /* Check if there is sufficient free space on the device
243 * TX queue. If not, return and let the TX tasklet
246 if (queue
->tx_devq_packets
== BCM43xx_PIO_MAXTXDEVQPACKETS
)
248 if (queue
->tx_devq_used
+ octets
> queue
->tx_devq_size
)
250 /* Now poke the device. */
251 pio_tx_write_fragment(queue
, skb
, packet
, sizeof(struct bcm43xx_txhdr_fw4
));
253 /* Account for the packet size.
254 * (We must not overflow the device TX queue)
256 queue
->tx_devq_packets
++;
257 queue
->tx_devq_used
+= octets
;
259 /* Transmission started, everything ok, move the
260 * packet to the txrunning list.
262 list_move_tail(&packet
->list
, &queue
->txrunning
);
267 static void tx_tasklet(unsigned long d
)
269 struct bcm43xx_pioqueue
*queue
= (struct bcm43xx_pioqueue
*)d
;
270 struct bcm43xx_wldev
*dev
= queue
->dev
;
272 struct bcm43xx_pio_txpacket
*packet
, *tmp_packet
;
276 spin_lock_irqsave(&dev
->wl
->irq_lock
, flags
);
277 if (queue
->tx_frozen
)
279 txctl
= bcm43xx_pio_read(queue
, BCM43xx_PIO_TXCTL
);
280 if (txctl
& BCM43xx_PIO_TXCTL_SUSPEND
)
283 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txqueue
, list
) {
284 /* Try to transmit the packet. This can fail, if
285 * the device queue is full. In case of failure, the
286 * packet is left in the txqueue.
287 * If transmission succeed, the packet is moved to txrunning.
288 * If it is impossible to transmit the packet, it
291 err
= pio_tx_packet(packet
);
296 spin_unlock_irqrestore(&dev
->wl
->irq_lock
, flags
);
299 static void setup_txqueues(struct bcm43xx_pioqueue
*queue
)
301 struct bcm43xx_pio_txpacket
*packet
;
304 queue
->nr_txfree
= BCM43xx_PIO_MAXTXPACKETS
;
305 for (i
= 0; i
< BCM43xx_PIO_MAXTXPACKETS
; i
++) {
306 packet
= &(queue
->tx_packets_cache
[i
]);
308 packet
->queue
= queue
;
309 INIT_LIST_HEAD(&packet
->list
);
311 list_add(&packet
->list
, &queue
->txfree
);
316 struct bcm43xx_pioqueue
* bcm43xx_setup_pioqueue(struct bcm43xx_wldev
*dev
,
319 struct bcm43xx_pioqueue
*queue
;
323 queue
= kzalloc(sizeof(*queue
), GFP_KERNEL
);
328 queue
->mmio_base
= pio_mmio_base
;
329 queue
->need_workarounds
= (dev
->dev
->id
.revision
< 3);
331 INIT_LIST_HEAD(&queue
->txfree
);
332 INIT_LIST_HEAD(&queue
->txqueue
);
333 INIT_LIST_HEAD(&queue
->txrunning
);
334 tasklet_init(&queue
->txtask
, tx_tasklet
,
335 (unsigned long)queue
);
337 value
= bcm43xx_read32(dev
, BCM43xx_MMIO_STATUS_BITFIELD
);
338 value
&= ~BCM43xx_SBF_XFER_REG_BYTESWAP
;
339 bcm43xx_write32(dev
, BCM43xx_MMIO_STATUS_BITFIELD
, value
);
341 qsize
= bcm43xx_read16(dev
, queue
->mmio_base
+ BCM43xx_PIO_TXQBUFSIZE
);
343 printk(KERN_ERR PFX
"ERROR: This card does not support PIO "
344 "operation mode. Please use DMA mode "
345 "(module parameter pio=0).\n");
348 if (qsize
<= BCM43xx_PIO_TXQADJUST
) {
349 printk(KERN_ERR PFX
"PIO tx device-queue too small (%u)\n",
353 qsize
-= BCM43xx_PIO_TXQADJUST
;
354 queue
->tx_devq_size
= qsize
;
356 setup_txqueues(queue
);
367 static void cancel_transfers(struct bcm43xx_pioqueue
*queue
)
369 struct bcm43xx_pio_txpacket
*packet
, *tmp_packet
;
371 tasklet_disable(&queue
->txtask
);
373 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txrunning
, list
)
374 free_txpacket(packet
, 0);
375 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txqueue
, list
)
376 free_txpacket(packet
, 0);
379 static void bcm43xx_destroy_pioqueue(struct bcm43xx_pioqueue
*queue
)
384 cancel_transfers(queue
);
388 void bcm43xx_pio_free(struct bcm43xx_wldev
*dev
)
390 struct bcm43xx_pio
*pio
;
392 if (!bcm43xx_using_pio(dev
))
396 bcm43xx_destroy_pioqueue(pio
->queue3
);
398 bcm43xx_destroy_pioqueue(pio
->queue2
);
400 bcm43xx_destroy_pioqueue(pio
->queue1
);
402 bcm43xx_destroy_pioqueue(pio
->queue0
);
406 int bcm43xx_pio_init(struct bcm43xx_wldev
*dev
)
408 struct bcm43xx_pio
*pio
= &dev
->pio
;
409 struct bcm43xx_pioqueue
*queue
;
412 queue
= bcm43xx_setup_pioqueue(dev
, BCM43xx_MMIO_PIO1_BASE
);
417 queue
= bcm43xx_setup_pioqueue(dev
, BCM43xx_MMIO_PIO2_BASE
);
422 queue
= bcm43xx_setup_pioqueue(dev
, BCM43xx_MMIO_PIO3_BASE
);
427 queue
= bcm43xx_setup_pioqueue(dev
, BCM43xx_MMIO_PIO4_BASE
);
432 if (dev
->dev
->id
.revision
< 3)
433 dev
->irq_savedstate
|= BCM43xx_IRQ_PIO_WORKAROUND
;
435 dprintk(KERN_INFO PFX
"PIO initialized\n");
441 bcm43xx_destroy_pioqueue(pio
->queue2
);
444 bcm43xx_destroy_pioqueue(pio
->queue1
);
447 bcm43xx_destroy_pioqueue(pio
->queue0
);
452 int bcm43xx_pio_tx(struct bcm43xx_wldev
*dev
,
454 struct ieee80211_tx_control
*ctl
)
456 struct bcm43xx_pioqueue
*queue
= dev
->pio
.queue1
;
457 struct bcm43xx_pio_txpacket
*packet
;
459 assert(!queue
->tx_suspended
);
460 assert(!list_empty(&queue
->txfree
));
462 packet
= list_entry(queue
->txfree
.next
, struct bcm43xx_pio_txpacket
, list
);
465 memset(&packet
->txstat
, 0, sizeof(packet
->txstat
));
466 memcpy(&packet
->txstat
.control
, ctl
, sizeof(*ctl
));
468 list_move_tail(&packet
->list
, &queue
->txqueue
);
470 queue
->nr_tx_packets
++;
471 assert(queue
->nr_txfree
< BCM43xx_PIO_MAXTXPACKETS
);
473 tasklet_schedule(&queue
->txtask
);
478 void bcm43xx_pio_handle_txstatus(struct bcm43xx_wldev
*dev
,
479 const struct bcm43xx_txstatus
*status
)
481 struct bcm43xx_pioqueue
*queue
;
482 struct bcm43xx_pio_txpacket
*packet
;
484 queue
= parse_cookie(dev
, status
->cookie
, &packet
);
487 queue
->tx_devq_packets
--;
488 queue
->tx_devq_used
-= (packet
->skb
->len
+ sizeof(struct bcm43xx_txhdr_fw4
));
491 packet
->txstat
.flags
|= IEEE80211_TX_STATUS_ACK
;
492 packet
->txstat
.retry_count
= status
->frame_count
- 1;
493 ieee80211_tx_status_irqsafe(dev
->wl
->hw
, packet
->skb
,
497 free_txpacket(packet
, 1);
498 /* If there are packets on the txqueue, poke the tasklet
501 if (!list_empty(&queue
->txqueue
))
502 tasklet_schedule(&queue
->txtask
);
505 void bcm43xx_pio_get_tx_stats(struct bcm43xx_wldev
*dev
,
506 struct ieee80211_tx_queue_stats
*stats
)
508 struct bcm43xx_pio
*pio
= &dev
->pio
;
509 struct bcm43xx_pioqueue
*queue
;
510 struct ieee80211_tx_queue_stats_data
*data
;
513 data
= &(stats
->data
[0]);
514 data
->len
= BCM43xx_PIO_MAXTXPACKETS
- queue
->nr_txfree
;
515 data
->limit
= BCM43xx_PIO_MAXTXPACKETS
;
516 data
->count
= queue
->nr_tx_packets
;
519 static void pio_rx_error(struct bcm43xx_pioqueue
*queue
,
525 printkl("PIO RX error: %s\n", error
);
526 bcm43xx_pio_write(queue
, BCM43xx_PIO_RXCTL
,
527 BCM43xx_PIO_RXCTL_READY
);
529 assert(queue
->mmio_base
== BCM43xx_MMIO_PIO1_BASE
);
530 for (i
= 0; i
< 15; i
++) {
532 bcm43xx_pio_read(queue
, BCM43xx_PIO_RXDATA
);
537 void bcm43xx_pio_rx(struct bcm43xx_pioqueue
*queue
)
539 u16 preamble
[21] = { 0 };
540 struct bcm43xx_rxhdr_fw4
*rxhdr
;
541 u16 tmp
, len
, macstat
;
542 int i
, preamble_readwords
;
545 tmp
= bcm43xx_pio_read(queue
, BCM43xx_PIO_RXCTL
);
546 if (!(tmp
& BCM43xx_PIO_RXCTL_DATAAVAILABLE
))
548 bcm43xx_pio_write(queue
, BCM43xx_PIO_RXCTL
,
549 BCM43xx_PIO_RXCTL_DATAAVAILABLE
);
551 for (i
= 0; i
< 10; i
++) {
552 tmp
= bcm43xx_pio_read(queue
, BCM43xx_PIO_RXCTL
);
553 if (tmp
& BCM43xx_PIO_RXCTL_READY
)
557 dprintkl(KERN_ERR PFX
"PIO RX timed out\n");
561 len
= bcm43xx_pio_read(queue
, BCM43xx_PIO_RXDATA
);
562 if (unlikely(len
> 0x700)) {
563 pio_rx_error(queue
, 0, "len > 0x700");
566 if (unlikely(len
== 0 && queue
->mmio_base
!= BCM43xx_MMIO_PIO4_BASE
)) {
567 pio_rx_error(queue
, 0, "len == 0");
570 preamble
[0] = cpu_to_le16(len
);
571 if (queue
->mmio_base
== BCM43xx_MMIO_PIO4_BASE
)
572 preamble_readwords
= 14 / sizeof(u16
);
574 preamble_readwords
= 18 / sizeof(u16
);
575 for (i
= 0; i
< preamble_readwords
; i
++) {
576 tmp
= bcm43xx_pio_read(queue
, BCM43xx_PIO_RXDATA
);
577 preamble
[i
+ 1] = cpu_to_le16(tmp
);
579 rxhdr
= (struct bcm43xx_rxhdr_fw4
*)preamble
;
580 macstat
= le16_to_cpu(rxhdr
->mac_status
);
581 if (macstat
& BCM43xx_RX_MAC_FCSERR
) {
583 (queue
->mmio_base
== BCM43xx_MMIO_PIO1_BASE
),
587 if (queue
->mmio_base
== BCM43xx_MMIO_PIO4_BASE
) {
588 /* We received an xmit status. */
589 struct bcm43xx_hwtxstatus
*hw
;
591 hw
= (struct bcm43xx_hwtxstatus
*)(preamble
+ 1);
592 bcm43xx_handle_hwtxstatus(queue
->dev
, hw
);
597 skb
= dev_alloc_skb(len
);
598 if (unlikely(!skb
)) {
599 pio_rx_error(queue
, 1, "OOM");
603 for (i
= 0; i
< len
- 1; i
+= 2) {
604 tmp
= bcm43xx_pio_read(queue
, BCM43xx_PIO_RXDATA
);
605 *((u16
*)(skb
->data
+ i
)) = cpu_to_le16(tmp
);
608 tmp
= bcm43xx_pio_read(queue
, BCM43xx_PIO_RXDATA
);
609 skb
->data
[len
- 1] = (tmp
& 0x00FF);
610 /* The specs say the following is required, but
611 * it is wrong and corrupts the PLCP. If we don't do
612 * this, the PLCP seems to be correct. So ifdef it out for now.
615 if (rxflags2
& BCM43xx_RXHDR_FLAGS2_TYPE2FRAME
)
616 skb
->data
[2] = (tmp
& 0xFF00) >> 8;
618 skb
->data
[0] = (tmp
& 0xFF00) >> 8;
621 bcm43xx_rx(queue
->dev
, skb
, rxhdr
);
624 void bcm43xx_pio_tx_suspend(struct bcm43xx_pioqueue
*queue
)
626 bcm43xx_power_saving_ctl_bits(queue
->dev
, -1, 1);
627 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXCTL
,
628 bcm43xx_pio_read(queue
, BCM43xx_PIO_TXCTL
)
629 | BCM43xx_PIO_TXCTL_SUSPEND
);
632 void bcm43xx_pio_tx_resume(struct bcm43xx_pioqueue
*queue
)
634 bcm43xx_pio_write(queue
, BCM43xx_PIO_TXCTL
,
635 bcm43xx_pio_read(queue
, BCM43xx_PIO_TXCTL
)
636 & ~BCM43xx_PIO_TXCTL_SUSPEND
);
637 bcm43xx_power_saving_ctl_bits(queue
->dev
, -1, -1);
638 tasklet_schedule(&queue
->txtask
);
641 void bcm43xx_pio_freeze_txqueues(struct bcm43xx_wldev
*dev
)
643 struct bcm43xx_pio
*pio
;
645 assert(bcm43xx_using_pio(dev
));
647 pio
->queue0
->tx_frozen
= 1;
648 pio
->queue1
->tx_frozen
= 1;
649 pio
->queue2
->tx_frozen
= 1;
650 pio
->queue3
->tx_frozen
= 1;
653 void bcm43xx_pio_thaw_txqueues(struct bcm43xx_wldev
*dev
)
655 struct bcm43xx_pio
*pio
;
657 assert(bcm43xx_using_pio(dev
));
659 pio
->queue0
->tx_frozen
= 0;
660 pio
->queue1
->tx_frozen
= 0;
661 pio
->queue2
->tx_frozen
= 0;
662 pio
->queue3
->tx_frozen
= 0;
663 if (!list_empty(&pio
->queue0
->txqueue
))
664 tasklet_schedule(&pio
->queue0
->txtask
);
665 if (!list_empty(&pio
->queue1
->txqueue
))
666 tasklet_schedule(&pio
->queue1
->txtask
);
667 if (!list_empty(&pio
->queue2
->txqueue
))
668 tasklet_schedule(&pio
->queue2
->txtask
);
669 if (!list_empty(&pio
->queue3
->txqueue
))
670 tasklet_schedule(&pio
->queue3
->txtask
);