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(*((u16
*) (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
= pio_txpacket_getindex(packet
);
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
);
290 list_add(&packet
->list
, &queue
->txfree
);
295 struct b43_pioqueue
*b43_setup_pioqueue(struct b43_wldev
*dev
,
298 struct b43_pioqueue
*queue
;
301 queue
= kzalloc(sizeof(*queue
), GFP_KERNEL
);
306 queue
->mmio_base
= pio_mmio_base
;
307 queue
->need_workarounds
= (dev
->dev
->id
.revision
< 3);
309 INIT_LIST_HEAD(&queue
->txfree
);
310 INIT_LIST_HEAD(&queue
->txqueue
);
311 INIT_LIST_HEAD(&queue
->txrunning
);
312 tasklet_init(&queue
->txtask
, tx_tasklet
, (unsigned long)queue
);
314 b43_write32(dev
, B43_MMIO_MACCTL
, b43_read32(dev
, B43_MMIO_MACCTL
)
317 qsize
= b43_read16(dev
, queue
->mmio_base
+ B43_PIO_TXQBUFSIZE
);
319 b43err(dev
->wl
, "This card does not support PIO "
320 "operation mode. Please use DMA mode "
321 "(module parameter pio=0).\n");
324 if (qsize
<= B43_PIO_TXQADJUST
) {
325 b43err(dev
->wl
, "PIO tx device-queue too small (%u)\n", qsize
);
328 qsize
-= B43_PIO_TXQADJUST
;
329 queue
->tx_devq_size
= qsize
;
331 setup_txqueues(queue
);
342 static void cancel_transfers(struct b43_pioqueue
*queue
)
344 struct b43_pio_txpacket
*packet
, *tmp_packet
;
346 tasklet_disable(&queue
->txtask
);
348 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txrunning
, list
)
349 free_txpacket(packet
);
350 list_for_each_entry_safe(packet
, tmp_packet
, &queue
->txqueue
, list
)
351 free_txpacket(packet
);
354 static void b43_destroy_pioqueue(struct b43_pioqueue
*queue
)
359 cancel_transfers(queue
);
363 void b43_pio_free(struct b43_wldev
*dev
)
367 if (!b43_using_pio(dev
))
371 b43_destroy_pioqueue(pio
->queue3
);
373 b43_destroy_pioqueue(pio
->queue2
);
375 b43_destroy_pioqueue(pio
->queue1
);
377 b43_destroy_pioqueue(pio
->queue0
);
381 int b43_pio_init(struct b43_wldev
*dev
)
383 struct b43_pio
*pio
= &dev
->pio
;
384 struct b43_pioqueue
*queue
;
387 queue
= b43_setup_pioqueue(dev
, B43_MMIO_PIO1_BASE
);
392 queue
= b43_setup_pioqueue(dev
, B43_MMIO_PIO2_BASE
);
397 queue
= b43_setup_pioqueue(dev
, B43_MMIO_PIO3_BASE
);
402 queue
= b43_setup_pioqueue(dev
, B43_MMIO_PIO4_BASE
);
407 if (dev
->dev
->id
.revision
< 3)
408 dev
->irq_savedstate
|= B43_IRQ_PIO_WORKAROUND
;
410 b43dbg(dev
->wl
, "PIO initialized\n");
416 b43_destroy_pioqueue(pio
->queue2
);
419 b43_destroy_pioqueue(pio
->queue1
);
422 b43_destroy_pioqueue(pio
->queue0
);
427 int b43_pio_tx(struct b43_wldev
*dev
,
428 struct sk_buff
*skb
, struct ieee80211_tx_control
*ctl
)
430 struct b43_pioqueue
*queue
= dev
->pio
.queue1
;
431 struct b43_pio_txpacket
*packet
;
433 B43_WARN_ON(queue
->tx_suspended
);
434 B43_WARN_ON(list_empty(&queue
->txfree
));
436 packet
= list_entry(queue
->txfree
.next
, struct b43_pio_txpacket
, list
);
439 memset(&packet
->txstat
, 0, sizeof(packet
->txstat
));
440 memcpy(&packet
->txstat
.control
, ctl
, sizeof(*ctl
));
442 list_move_tail(&packet
->list
, &queue
->txqueue
);
444 queue
->nr_tx_packets
++;
445 B43_WARN_ON(queue
->nr_txfree
>= B43_PIO_MAXTXPACKETS
);
447 tasklet_schedule(&queue
->txtask
);
452 void b43_pio_handle_txstatus(struct b43_wldev
*dev
,
453 const struct b43_txstatus
*status
)
455 struct b43_pioqueue
*queue
;
456 struct b43_pio_txpacket
*packet
;
458 queue
= parse_cookie(dev
, status
->cookie
, &packet
);
459 if (B43_WARN_ON(!queue
))
462 queue
->tx_devq_packets
--;
463 queue
->tx_devq_used
-=
464 (packet
->skb
->len
+ sizeof(struct b43_txhdr_fw4
));
467 packet
->txstat
.flags
|= IEEE80211_TX_STATUS_ACK
;
469 if (!(packet
->txstat
.control
.flags
& IEEE80211_TXCTL_NO_ACK
))
470 packet
->txstat
.excessive_retries
= 1;
472 if (status
->frame_count
== 0) {
473 /* The frame was not transmitted at all. */
474 packet
->txstat
.retry_count
= 0;
476 packet
->txstat
.retry_count
= status
->frame_count
- 1;
477 ieee80211_tx_status_irqsafe(dev
->wl
->hw
, packet
->skb
,
481 free_txpacket(packet
);
482 /* If there are packets on the txqueue, poke the tasklet
485 if (!list_empty(&queue
->txqueue
))
486 tasklet_schedule(&queue
->txtask
);
489 void b43_pio_get_tx_stats(struct b43_wldev
*dev
,
490 struct ieee80211_tx_queue_stats
*stats
)
492 struct b43_pio
*pio
= &dev
->pio
;
493 struct b43_pioqueue
*queue
;
494 struct ieee80211_tx_queue_stats_data
*data
;
497 data
= &(stats
->data
[0]);
498 data
->len
= B43_PIO_MAXTXPACKETS
- queue
->nr_txfree
;
499 data
->limit
= B43_PIO_MAXTXPACKETS
;
500 data
->count
= queue
->nr_tx_packets
;
503 static void pio_rx_error(struct b43_pioqueue
*queue
,
504 int clear_buffers
, const char *error
)
508 b43err(queue
->dev
->wl
, "PIO RX error: %s\n", error
);
509 b43_pio_write(queue
, B43_PIO_RXCTL
, B43_PIO_RXCTL_READY
);
511 B43_WARN_ON(queue
->mmio_base
!= B43_MMIO_PIO1_BASE
);
512 for (i
= 0; i
< 15; i
++) {
514 b43_pio_read(queue
, B43_PIO_RXDATA
);
519 void b43_pio_rx(struct b43_pioqueue
*queue
)
521 u16 preamble
[21] = { 0 };
522 struct b43_rxhdr_fw4
*rxhdr
;
523 u16 tmp
, len
, macstat
;
524 int i
, preamble_readwords
;
527 tmp
= b43_pio_read(queue
, B43_PIO_RXCTL
);
528 if (!(tmp
& B43_PIO_RXCTL_DATAAVAILABLE
))
530 b43_pio_write(queue
, B43_PIO_RXCTL
, B43_PIO_RXCTL_DATAAVAILABLE
);
532 for (i
= 0; i
< 10; i
++) {
533 tmp
= b43_pio_read(queue
, B43_PIO_RXCTL
);
534 if (tmp
& B43_PIO_RXCTL_READY
)
538 b43dbg(queue
->dev
->wl
, "PIO RX timed out\n");
542 len
= b43_pio_read(queue
, B43_PIO_RXDATA
);
543 if (unlikely(len
> 0x700)) {
544 pio_rx_error(queue
, 0, "len > 0x700");
547 if (unlikely(len
== 0 && queue
->mmio_base
!= B43_MMIO_PIO4_BASE
)) {
548 pio_rx_error(queue
, 0, "len == 0");
551 preamble
[0] = cpu_to_le16(len
);
552 if (queue
->mmio_base
== B43_MMIO_PIO4_BASE
)
553 preamble_readwords
= 14 / sizeof(u16
);
555 preamble_readwords
= 18 / sizeof(u16
);
556 for (i
= 0; i
< preamble_readwords
; i
++) {
557 tmp
= b43_pio_read(queue
, B43_PIO_RXDATA
);
558 preamble
[i
+ 1] = cpu_to_le16(tmp
);
560 rxhdr
= (struct b43_rxhdr_fw4
*)preamble
;
561 macstat
= le16_to_cpu(rxhdr
->mac_status
);
562 if (macstat
& B43_RX_MAC_FCSERR
) {
564 (queue
->mmio_base
== B43_MMIO_PIO1_BASE
),
568 if (queue
->mmio_base
== B43_MMIO_PIO4_BASE
) {
569 /* We received an xmit status. */
570 struct b43_hwtxstatus
*hw
;
572 hw
= (struct b43_hwtxstatus
*)(preamble
+ 1);
573 b43_handle_hwtxstatus(queue
->dev
, hw
);
578 skb
= dev_alloc_skb(len
);
579 if (unlikely(!skb
)) {
580 pio_rx_error(queue
, 1, "OOM");
584 for (i
= 0; i
< len
- 1; i
+= 2) {
585 tmp
= b43_pio_read(queue
, B43_PIO_RXDATA
);
586 *((u16
*) (skb
->data
+ i
)) = cpu_to_le16(tmp
);
589 tmp
= b43_pio_read(queue
, B43_PIO_RXDATA
);
590 skb
->data
[len
- 1] = (tmp
& 0x00FF);
591 /* The specs say the following is required, but
592 * it is wrong and corrupts the PLCP. If we don't do
593 * this, the PLCP seems to be correct. So ifdef it out for now.
596 if (rxflags2
& B43_RXHDR_FLAGS2_TYPE2FRAME
)
597 skb
->data
[2] = (tmp
& 0xFF00) >> 8;
599 skb
->data
[0] = (tmp
& 0xFF00) >> 8;
602 b43_rx(queue
->dev
, skb
, rxhdr
);
605 void b43_pio_tx_suspend(struct b43_pioqueue
*queue
)
607 b43_power_saving_ctl_bits(queue
->dev
, B43_PS_AWAKE
);
608 b43_pio_write(queue
, B43_PIO_TXCTL
, b43_pio_read(queue
, B43_PIO_TXCTL
)
609 | B43_PIO_TXCTL_SUSPEND
);
612 void b43_pio_tx_resume(struct b43_pioqueue
*queue
)
614 b43_pio_write(queue
, B43_PIO_TXCTL
, b43_pio_read(queue
, B43_PIO_TXCTL
)
615 & ~B43_PIO_TXCTL_SUSPEND
);
616 b43_power_saving_ctl_bits(queue
->dev
, 0);
617 tasklet_schedule(&queue
->txtask
);
620 void b43_pio_freeze_txqueues(struct b43_wldev
*dev
)
624 B43_WARN_ON(!b43_using_pio(dev
));
626 pio
->queue0
->tx_frozen
= 1;
627 pio
->queue1
->tx_frozen
= 1;
628 pio
->queue2
->tx_frozen
= 1;
629 pio
->queue3
->tx_frozen
= 1;
632 void b43_pio_thaw_txqueues(struct b43_wldev
*dev
)
636 B43_WARN_ON(!b43_using_pio(dev
));
638 pio
->queue0
->tx_frozen
= 0;
639 pio
->queue1
->tx_frozen
= 0;
640 pio
->queue2
->tx_frozen
= 0;
641 pio
->queue3
->tx_frozen
= 0;
642 if (!list_empty(&pio
->queue0
->txqueue
))
643 tasklet_schedule(&pio
->queue0
->txtask
);
644 if (!list_empty(&pio
->queue1
->txqueue
))
645 tasklet_schedule(&pio
->queue1
->txtask
);
646 if (!list_empty(&pio
->queue2
->txqueue
))
647 tasklet_schedule(&pio
->queue2
->txtask
);
648 if (!list_empty(&pio
->queue3
->txqueue
))
649 tasklet_schedule(&pio
->queue3
->txtask
);