3 Broadcom B43 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.
31 #include <linux/delay.h>
33 static void tx_start(struct b43_pioqueue
*queue
)
35 b43_pio_write(queue
, B43_PIO_TXCTL
, B43_PIO_TXCTL_INIT
);
38 static void tx_octet(struct b43_pioqueue
*queue
, u8 octet
)
40 if (queue
->need_workarounds
) {
41 b43_pio_write(queue
, B43_PIO_TXDATA
, octet
);
42 b43_pio_write(queue
, B43_PIO_TXCTL
, B43_PIO_TXCTL_WRITELO
);
44 b43_pio_write(queue
, B43_PIO_TXCTL
, B43_PIO_TXCTL_WRITELO
);
45 b43_pio_write(queue
, B43_PIO_TXDATA
, octet
);
49 static u16
tx_get_next_word(const u8
* txhdr
,
51 size_t txhdr_size
, unsigned int *pos
)
54 unsigned int i
= *pos
;
63 ret
= le16_to_cpu(*((__le16
*)(source
+ i
)));
69 static void tx_data(struct b43_pioqueue
*queue
,
70 u8
* txhdr
, const u8
* packet
, unsigned int octets
)
75 if (queue
->need_workarounds
) {
76 data
= tx_get_next_word(txhdr
, packet
,
77 sizeof(struct b43_txhdr_fw4
), &i
);
78 b43_pio_write(queue
, B43_PIO_TXDATA
, data
);
80 b43_pio_write(queue
, B43_PIO_TXCTL
,
81 B43_PIO_TXCTL_WRITELO
| B43_PIO_TXCTL_WRITEHI
);
82 while (i
< octets
- 1) {
83 data
= tx_get_next_word(txhdr
, packet
,
84 sizeof(struct b43_txhdr_fw4
), &i
);
85 b43_pio_write(queue
, B43_PIO_TXDATA
, data
);
89 packet
[octets
- sizeof(struct b43_txhdr_fw4
) - 1]);
92 static void tx_complete(struct b43_pioqueue
*queue
, struct sk_buff
*skb
)
94 if (queue
->need_workarounds
) {
95 b43_pio_write(queue
, B43_PIO_TXDATA
, skb
->data
[skb
->len
- 1]);
96 b43_pio_write(queue
, B43_PIO_TXCTL
,
97 B43_PIO_TXCTL_WRITELO
| B43_PIO_TXCTL_COMPLETE
);
99 b43_pio_write(queue
, B43_PIO_TXCTL
, B43_PIO_TXCTL_COMPLETE
);
103 static u16
generate_cookie(struct b43_pioqueue
*queue
,
104 struct b43_pio_txpacket
*packet
)
109 /* We use the upper 4 bits for the PIO
110 * controller ID and the lower 12 bits
111 * for the packet index (in the cache).
113 switch (queue
->mmio_base
) {
114 case B43_MMIO_PIO1_BASE
:
116 case B43_MMIO_PIO2_BASE
:
119 case B43_MMIO_PIO3_BASE
:
122 case B43_MMIO_PIO4_BASE
:
128 packetindex
= packet
->index
;
129 B43_WARN_ON(packetindex
& ~0x0FFF);
130 cookie
|= (u16
) packetindex
;
136 struct b43_pioqueue
*parse_cookie(struct b43_wldev
*dev
,
137 u16 cookie
, struct b43_pio_txpacket
**packet
)
139 struct b43_pio
*pio
= &dev
->pio
;
140 struct b43_pioqueue
*queue
= NULL
;
143 switch (cookie
& 0xF000) {
159 packetindex
= (cookie
& 0x0FFF);
160 B43_WARN_ON(!(packetindex
>= 0 && packetindex
< B43_PIO_MAXTXPACKETS
));
161 *packet
= &(queue
->tx_packets_cache
[packetindex
]);
167 struct b43_txhdr_fw4 txhdr_fw4
;
170 static void pio_tx_write_fragment(struct b43_pioqueue
*queue
,
172 struct b43_pio_txpacket
*packet
,
175 union txhdr_union txhdr_data
;
179 txhdr
= (u8
*) (&txhdr_data
.txhdr_fw4
);
181 B43_WARN_ON(skb_shinfo(skb
)->nr_frags
);
182 b43_generate_txhdr(queue
->dev
,
183 txhdr
, skb
->data
, skb
->len
,
184 &packet
->txstat
.control
,
185 generate_cookie(queue
, packet
));
188 octets
= skb
->len
+ txhdr_size
;
189 if (queue
->need_workarounds
)
191 tx_data(queue
, txhdr
, (u8
*) skb
->data
, octets
);
192 tx_complete(queue
, skb
);
195 static void free_txpacket(struct b43_pio_txpacket
*packet
)
197 struct b43_pioqueue
*queue
= packet
->queue
;
200 dev_kfree_skb_any(packet
->skb
);
201 list_move(&packet
->list
, &queue
->txfree
);
205 static int pio_tx_packet(struct b43_pio_txpacket
*packet
)
207 struct b43_pioqueue
*queue
= packet
->queue
;
208 struct sk_buff
*skb
= packet
->skb
;
211 octets
= (u16
) skb
->len
+ sizeof(struct b43_txhdr_fw4
);
212 if (queue
->tx_devq_size
< octets
) {
213 b43warn(queue
->dev
->wl
, "PIO queue too small. "
214 "Dropping packet.\n");
215 /* Drop it silently (return success) */
216 free_txpacket(packet
);
219 B43_WARN_ON(queue
->tx_devq_packets
> B43_PIO_MAXTXDEVQPACKETS
);
220 B43_WARN_ON(queue
->tx_devq_used
> queue
->tx_devq_size
);
221 /* Check if there is sufficient free space on the device
222 * TX queue. If not, return and let the TX tasklet
225 if (queue
->tx_devq_packets
== B43_PIO_MAXTXDEVQPACKETS
)
227 if (queue
->tx_devq_used
+ octets
> queue
->tx_devq_size
)
229 /* Now poke the device. */
230 pio_tx_write_fragment(queue
, skb
, packet
, sizeof(struct b43_txhdr_fw4
));
232 /* Account for the packet size.
233 * (We must not overflow the device TX queue)
235 queue
->tx_devq_packets
++;
236 queue
->tx_devq_used
+= octets
;
238 /* Transmission started, everything ok, move the
239 * packet to the txrunning list.
241 list_move_tail(&packet
->list
, &queue
->txrunning
);
246 static void tx_tasklet(unsigned long d
)
248 struct b43_pioqueue
*queue
= (struct b43_pioqueue
*)d
;
249 struct b43_wldev
*dev
= queue
->dev
;
251 struct b43_pio_txpacket
*packet
, *tmp_packet
;
255 spin_lock_irqsave(&dev
->wl
->irq_lock
, flags
);
256 if (queue
->tx_frozen
)
258 txctl
= b43_pio_read(queue
, B43_PIO_TXCTL
);
259 if (txctl
& B43_PIO_TXCTL_SUSPEND
)
262 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txqueue
, list
) {
263 /* Try to transmit the packet. This can fail, if
264 * the device queue is full. In case of failure, the
265 * packet is left in the txqueue.
266 * If transmission succeed, the packet is moved to txrunning.
267 * If it is impossible to transmit the packet, it
270 err
= pio_tx_packet(packet
);
275 spin_unlock_irqrestore(&dev
->wl
->irq_lock
, flags
);
278 static void setup_txqueues(struct b43_pioqueue
*queue
)
280 struct b43_pio_txpacket
*packet
;
283 queue
->nr_txfree
= B43_PIO_MAXTXPACKETS
;
284 for (i
= 0; i
< B43_PIO_MAXTXPACKETS
; i
++) {
285 packet
= &(queue
->tx_packets_cache
[i
]);
287 packet
->queue
= queue
;
288 INIT_LIST_HEAD(&packet
->list
);
291 list_add(&packet
->list
, &queue
->txfree
);
296 struct b43_pioqueue
*b43_setup_pioqueue(struct b43_wldev
*dev
,
299 struct b43_pioqueue
*queue
;
302 queue
= kzalloc(sizeof(*queue
), GFP_KERNEL
);
307 queue
->mmio_base
= pio_mmio_base
;
308 queue
->need_workarounds
= (dev
->dev
->id
.revision
< 3);
310 INIT_LIST_HEAD(&queue
->txfree
);
311 INIT_LIST_HEAD(&queue
->txqueue
);
312 INIT_LIST_HEAD(&queue
->txrunning
);
313 tasklet_init(&queue
->txtask
, tx_tasklet
, (unsigned long)queue
);
315 b43_write32(dev
, B43_MMIO_MACCTL
, b43_read32(dev
, B43_MMIO_MACCTL
)
318 qsize
= b43_read16(dev
, queue
->mmio_base
+ B43_PIO_TXQBUFSIZE
);
320 b43err(dev
->wl
, "This card does not support PIO "
321 "operation mode. Please use DMA mode "
322 "(module parameter pio=0).\n");
325 if (qsize
<= B43_PIO_TXQADJUST
) {
326 b43err(dev
->wl
, "PIO tx device-queue too small (%u)\n", qsize
);
329 qsize
-= B43_PIO_TXQADJUST
;
330 queue
->tx_devq_size
= qsize
;
332 setup_txqueues(queue
);
343 static void cancel_transfers(struct b43_pioqueue
*queue
)
345 struct b43_pio_txpacket
*packet
, *tmp_packet
;
347 tasklet_disable(&queue
->txtask
);
349 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txrunning
, list
)
350 free_txpacket(packet
);
351 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txqueue
, list
)
352 free_txpacket(packet
);
355 static void b43_destroy_pioqueue(struct b43_pioqueue
*queue
)
360 cancel_transfers(queue
);
364 void b43_pio_free(struct b43_wldev
*dev
)
368 if (!b43_using_pio(dev
))
372 b43_destroy_pioqueue(pio
->queue3
);
374 b43_destroy_pioqueue(pio
->queue2
);
376 b43_destroy_pioqueue(pio
->queue1
);
378 b43_destroy_pioqueue(pio
->queue0
);
382 int b43_pio_init(struct b43_wldev
*dev
)
384 struct b43_pio
*pio
= &dev
->pio
;
385 struct b43_pioqueue
*queue
;
388 queue
= b43_setup_pioqueue(dev
, B43_MMIO_PIO1_BASE
);
393 queue
= b43_setup_pioqueue(dev
, B43_MMIO_PIO2_BASE
);
398 queue
= b43_setup_pioqueue(dev
, B43_MMIO_PIO3_BASE
);
403 queue
= b43_setup_pioqueue(dev
, B43_MMIO_PIO4_BASE
);
408 if (dev
->dev
->id
.revision
< 3)
409 dev
->irq_savedstate
|= B43_IRQ_PIO_WORKAROUND
;
411 b43dbg(dev
->wl
, "PIO initialized\n");
417 b43_destroy_pioqueue(pio
->queue2
);
420 b43_destroy_pioqueue(pio
->queue1
);
423 b43_destroy_pioqueue(pio
->queue0
);
428 int b43_pio_tx(struct b43_wldev
*dev
,
429 struct sk_buff
*skb
, struct ieee80211_tx_control
*ctl
)
431 struct b43_pioqueue
*queue
= dev
->pio
.queue1
;
432 struct b43_pio_txpacket
*packet
;
434 B43_WARN_ON(queue
->tx_suspended
);
435 B43_WARN_ON(list_empty(&queue
->txfree
));
437 packet
= list_entry(queue
->txfree
.next
, struct b43_pio_txpacket
, list
);
440 memset(&packet
->txstat
, 0, sizeof(packet
->txstat
));
441 memcpy(&packet
->txstat
.control
, ctl
, sizeof(*ctl
));
443 list_move_tail(&packet
->list
, &queue
->txqueue
);
445 queue
->nr_tx_packets
++;
446 B43_WARN_ON(queue
->nr_txfree
>= B43_PIO_MAXTXPACKETS
);
448 tasklet_schedule(&queue
->txtask
);
453 void b43_pio_handle_txstatus(struct b43_wldev
*dev
,
454 const struct b43_txstatus
*status
)
456 struct b43_pioqueue
*queue
;
457 struct b43_pio_txpacket
*packet
;
459 queue
= parse_cookie(dev
, status
->cookie
, &packet
);
460 if (B43_WARN_ON(!queue
))
463 queue
->tx_devq_packets
--;
464 queue
->tx_devq_used
-=
465 (packet
->skb
->len
+ sizeof(struct b43_txhdr_fw4
));
468 packet
->txstat
.flags
|= IEEE80211_TX_STATUS_ACK
;
470 if (!(packet
->txstat
.control
.flags
& IEEE80211_TXCTL_NO_ACK
))
471 packet
->txstat
.excessive_retries
= 1;
473 if (status
->frame_count
== 0) {
474 /* The frame was not transmitted at all. */
475 packet
->txstat
.retry_count
= 0;
477 packet
->txstat
.retry_count
= status
->frame_count
- 1;
478 ieee80211_tx_status_irqsafe(dev
->wl
->hw
, packet
->skb
,
482 free_txpacket(packet
);
483 /* If there are packets on the txqueue, poke the tasklet
486 if (!list_empty(&queue
->txqueue
))
487 tasklet_schedule(&queue
->txtask
);
490 void b43_pio_get_tx_stats(struct b43_wldev
*dev
,
491 struct ieee80211_tx_queue_stats
*stats
)
493 struct b43_pio
*pio
= &dev
->pio
;
494 struct b43_pioqueue
*queue
;
495 struct ieee80211_tx_queue_stats_data
*data
;
498 data
= &(stats
->data
[0]);
499 data
->len
= B43_PIO_MAXTXPACKETS
- queue
->nr_txfree
;
500 data
->limit
= B43_PIO_MAXTXPACKETS
;
501 data
->count
= queue
->nr_tx_packets
;
504 static void pio_rx_error(struct b43_pioqueue
*queue
,
505 int clear_buffers
, const char *error
)
509 b43err(queue
->dev
->wl
, "PIO RX error: %s\n", error
);
510 b43_pio_write(queue
, B43_PIO_RXCTL
, B43_PIO_RXCTL_READY
);
512 B43_WARN_ON(queue
->mmio_base
!= B43_MMIO_PIO1_BASE
);
513 for (i
= 0; i
< 15; i
++) {
515 b43_pio_read(queue
, B43_PIO_RXDATA
);
520 void b43_pio_rx(struct b43_pioqueue
*queue
)
522 __le16 preamble
[21] = { 0 };
523 struct b43_rxhdr_fw4
*rxhdr
;
526 int i
, preamble_readwords
;
529 tmp
= b43_pio_read(queue
, B43_PIO_RXCTL
);
530 if (!(tmp
& B43_PIO_RXCTL_DATAAVAILABLE
))
532 b43_pio_write(queue
, B43_PIO_RXCTL
, B43_PIO_RXCTL_DATAAVAILABLE
);
534 for (i
= 0; i
< 10; i
++) {
535 tmp
= b43_pio_read(queue
, B43_PIO_RXCTL
);
536 if (tmp
& B43_PIO_RXCTL_READY
)
540 b43dbg(queue
->dev
->wl
, "PIO RX timed out\n");
544 len
= b43_pio_read(queue
, B43_PIO_RXDATA
);
545 if (unlikely(len
> 0x700)) {
546 pio_rx_error(queue
, 0, "len > 0x700");
549 if (unlikely(len
== 0 && queue
->mmio_base
!= B43_MMIO_PIO4_BASE
)) {
550 pio_rx_error(queue
, 0, "len == 0");
553 preamble
[0] = cpu_to_le16(len
);
554 if (queue
->mmio_base
== B43_MMIO_PIO4_BASE
)
555 preamble_readwords
= 14 / sizeof(u16
);
557 preamble_readwords
= 18 / sizeof(u16
);
558 for (i
= 0; i
< preamble_readwords
; i
++) {
559 tmp
= b43_pio_read(queue
, B43_PIO_RXDATA
);
560 preamble
[i
+ 1] = cpu_to_le16(tmp
);
562 rxhdr
= (struct b43_rxhdr_fw4
*)preamble
;
563 macstat
= le32_to_cpu(rxhdr
->mac_status
);
564 if (macstat
& B43_RX_MAC_FCSERR
) {
566 (queue
->mmio_base
== B43_MMIO_PIO1_BASE
),
570 if (queue
->mmio_base
== B43_MMIO_PIO4_BASE
) {
571 /* We received an xmit status. */
572 struct b43_hwtxstatus
*hw
;
574 hw
= (struct b43_hwtxstatus
*)(preamble
+ 1);
575 b43_handle_hwtxstatus(queue
->dev
, hw
);
580 skb
= dev_alloc_skb(len
);
581 if (unlikely(!skb
)) {
582 pio_rx_error(queue
, 1, "OOM");
586 for (i
= 0; i
< len
- 1; i
+= 2) {
587 tmp
= b43_pio_read(queue
, B43_PIO_RXDATA
);
588 *((__le16
*)(skb
->data
+ i
)) = cpu_to_le16(tmp
);
591 tmp
= b43_pio_read(queue
, B43_PIO_RXDATA
);
592 skb
->data
[len
- 1] = (tmp
& 0x00FF);
593 /* The specs say the following is required, but
594 * it is wrong and corrupts the PLCP. If we don't do
595 * this, the PLCP seems to be correct. So ifdef it out for now.
598 if (rxflags2
& B43_RXHDR_FLAGS2_TYPE2FRAME
)
599 skb
->data
[2] = (tmp
& 0xFF00) >> 8;
601 skb
->data
[0] = (tmp
& 0xFF00) >> 8;
604 b43_rx(queue
->dev
, skb
, rxhdr
);
607 void b43_pio_tx_suspend(struct b43_pioqueue
*queue
)
609 b43_power_saving_ctl_bits(queue
->dev
, B43_PS_AWAKE
);
610 b43_pio_write(queue
, B43_PIO_TXCTL
, b43_pio_read(queue
, B43_PIO_TXCTL
)
611 | B43_PIO_TXCTL_SUSPEND
);
614 void b43_pio_tx_resume(struct b43_pioqueue
*queue
)
616 b43_pio_write(queue
, B43_PIO_TXCTL
, b43_pio_read(queue
, B43_PIO_TXCTL
)
617 & ~B43_PIO_TXCTL_SUSPEND
);
618 b43_power_saving_ctl_bits(queue
->dev
, 0);
619 tasklet_schedule(&queue
->txtask
);
622 void b43_pio_freeze_txqueues(struct b43_wldev
*dev
)
626 B43_WARN_ON(!b43_using_pio(dev
));
628 pio
->queue0
->tx_frozen
= 1;
629 pio
->queue1
->tx_frozen
= 1;
630 pio
->queue2
->tx_frozen
= 1;
631 pio
->queue3
->tx_frozen
= 1;
634 void b43_pio_thaw_txqueues(struct b43_wldev
*dev
)
638 B43_WARN_ON(!b43_using_pio(dev
));
640 pio
->queue0
->tx_frozen
= 0;
641 pio
->queue1
->tx_frozen
= 0;
642 pio
->queue2
->tx_frozen
= 0;
643 pio
->queue3
->tx_frozen
= 0;
644 if (!list_empty(&pio
->queue0
->txqueue
))
645 tasklet_schedule(&pio
->queue0
->txtask
);
646 if (!list_empty(&pio
->queue1
->txqueue
))
647 tasklet_schedule(&pio
->queue1
->txtask
);
648 if (!list_empty(&pio
->queue2
->txqueue
))
649 tasklet_schedule(&pio
->queue2
->txtask
);
650 if (!list_empty(&pio
->queue3
->txqueue
))
651 tasklet_schedule(&pio
->queue3
->txtask
);